Skip to content

Commit

Permalink
Multiple users support
Browse files Browse the repository at this point in the history
  • Loading branch information
igor725 committed Apr 26, 2024
1 parent 8e05395 commit d3171f8
Show file tree
Hide file tree
Showing 9 changed files with 271 additions and 228 deletions.
2 changes: 1 addition & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/OpenOrbis/PS4Toolchain/include/**"
"C:/OpenOrbis/PS4Toolchain/include/**"
],
"compilerPath": "clang.exe",
"cStandard": "c17",
Expand Down
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"C_Cpp.formatting": "clangFormat",
"C_Cpp.clang_format_fallbackStyle": "LLVM",
"C_Cpp.default.cppStandard": "c++20",
"C_Cpp.autoAddFileAssociations": false,
"editor.tabSize": 2,
"editor.insertSpaces": true,
}
40 changes: 21 additions & 19 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Build",
"type": "shell",
"command": "build.bat",
"options": {
"cwd": "${workspaceRoot}/input"
},
"args": [
"x64\\Debug\\"
],
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
"version": "2.0.0",
"tasks": [
{
"label": "Build",
"type": "shell",
"command": "build.bat",
"options": {
"cwd": "${workspaceRoot}/input"
},
"args": [
"x64\\Debug\\"
],
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Package metadata.
TITLE := OpenOrbis Input Sample
VERSION := 1.02
TITLE_ID := BREW00084
CONTENT_ID := IV0000-BREW00084_00-CONTROLREX000000
TITLE := PS4 Test Input System
VERSION := 1.1
TITLE_ID := DDRM00001
CONTENT_ID := IV0000-DDRM00001_00-PS4INPUTSY000000

# Libraries linked into the ELF.
LIBS := -lc -lkernel -lc++ -lSceVideoOut -lScePad -lSceUserService
Expand Down
60 changes: 8 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,53 +1,13 @@
# PS4 Input

This project contains example code for getting controller input from the DualShock 4 (DS4) controller. If a button or multiple buttons are pushed, they will be highlighted on the screen over the controller graphic. The original project can be found [here](https://github.com/OpenOrbis/OpenOrbis-PS4-Toolchain/tree/master/samples/input).

- **Title ID**: BREW00084
- **Content ID**: IV0000-BREW00084_00-CONTROLREX000000


## Directory structure
```
root
|-- assets
|-- images // Images for drawing in the application
|-- circle.png
|-- controller.png
|-- dpad-down.png
|-- dpad-left.png
|-- dpad-right.png
|-- dpad-up.png
|-- l1.png
|-- l2.png
|-- r1.png
|-- r2.png
|-- square.png
|-- start.png
|-- thumbstick.png
|-- thumbstick_pr.png
|-- touchpad.png
|-- triangle.png
|-- x.png
|-- input
|-- build.bat // Batch file for building on Windows
|-- controller.cpp // Controller class and helper methods
|-- controller.h // Controller class header
|-- main.cpp // main source file
|-- sce_module // Dependency modules for the pkg
|-- libSceFios2.prx
|-- libc.prx
|-- sce_sys // Package materials (metadata)
|-- about
|-- right.prx
|-- icon0.png
|-- param.sfo
|-- eboot.bin // final eboot (not present until built)
|-- Makefile // Make rules for building on Linux
|-- pkg.gp4 // Package project file
```
The ELF, Orbis ELF (OELF), and object files will all be stored in the intermediate directory `x64/Debug`. The final eboot.bin file will be found in the root directory.

This application allows you to test your PS4 gamepads (or your input system implementation if you are making an PS4 emulator). It's based on OpenOrbis Input Sample, original project can be found [here](https://github.com/OpenOrbis/OpenOrbis-PS4-Toolchain/tree/master/samples/input).

## New features
- Left and right stick position tracking
- Haptic feedback on `L2`/`R2` trigger press
- Switching the lightbar color by pressing `Triangle`
- Mulitple users support (Press `Options` button on the gamepad to switch to it)
- TouchPad reading

## Libraries used

Expand All @@ -59,17 +19,14 @@ The ELF, Orbis ELF (OELF), and object files will all be stored in the intermedia
- libSceUserService



## Building

On Linux, a makefile has been included for building.

To build this project, the developer will need clang, which is provided in the toolchain. The `OO_PS4_TOOLCHAIN` environment variable will also need to be set to the root directory of the SDK installation.

__Windows__
Run the batch file from command prompt or powershell with the following command:
```
.\build.bat .\x64\Debug "input"
.\build.bat .\x64\Debug
```

__Linux__
Expand All @@ -79,7 +36,6 @@ make
```



## Author(s)

- Specter
Expand Down
16 changes: 10 additions & 6 deletions input/build.bat
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
SETLOCAL EnableDelayedExpansion

Rem Package information
set PKG_TITLE="OpenOrbis Input Sample"
set PKG_VERSION="1.02"
set PKG_TITLE="PS4 Test Input System"
set PKG_VERSION="1.1"
set PKG_ASSETS="assets"
set PKG_TITLE_ID="BREW00084"
set PKG_CONTENT_ID="IV0000-%PKG_TITLE_ID%_00-CONTROLREX000000"
set PKG_TITLE_ID="DDRM00001"
set PKG_CONTENT_ID="IV0000-%PKG_TITLE_ID%_00-PS4INPUTSY000000"

Rem Libraries to link in
set libraries=-lc -lkernel -lc++ -lSceVideoOut -lScePad -lSceUserService
Expand All @@ -26,12 +26,16 @@ set compilerPP=clang++
Rem Compile object files for all the source files
for %%f in (*.c) do (
%compiler% --target=x86_64-pc-freebsd12-elf -fPIC -funwind-tables -I"%OO_PS4_TOOLCHAIN%\\include" -I"%OO_PS4_TOOLCHAIN%\\include\\c++\\v1" %extra_flags% -c -o %intdir%\%%~nf.o %%~nf.c
if NOT ERRORLEVEL 0 exit 1
set failed=1
if errorlevel 0 if not errorlevel 1 set "failed="
if defined failed exit 1
)

for %%f in (*.cpp) do (
%compilerPP% --target=x86_64-pc-freebsd12-elf -fPIC -funwind-tables -I"%OO_PS4_TOOLCHAIN%\\include" -I"%OO_PS4_TOOLCHAIN%\\include\\c++\\v1" %extra_flags% -c -o %intdir%\%%~nf.o %%~nf.cpp
if NOT ERRORLEVEL 0 exit 1
set failed=1
if errorlevel 0 if not errorlevel 1 set "failed="
if defined failed exit 1
)

Rem Get a list of object files for linking
Expand Down
55 changes: 26 additions & 29 deletions input/controller.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
#include "controller.h"
#include "log.h"

Controller::Controller() {
this->currPadColor = 0;
this->padColors[0] = {0xff, 0xff, 0xff, 0xff};
this->padColors[1] = {0x00, 0xff, 0x00, 0xff};
this->padColors[2] = {0xff, 0x00, 0x00, 0xff};
this->padColors[3] = {0x00, 0x00, 0xff, 0xff};
this->padColors[4] = {0xff, 0xff, 0x00, 0xff};
this->padColors[5] = {0xff, 0x00, 0xff, 0xff};
this->padColors[6] = {0x00, 0xff, 0xff, 0xff};
this->padColors[7] = {0xff, 0xff, 0xff, 0xff};
}
static OrbisPadColor padColors[8] = {
{0xff, 0xff, 0xff, 0xff}, {0x00, 0xff, 0x00, 0xff},
{0xff, 0x00, 0x00, 0xff}, {0x00, 0x00, 0xff, 0xff},
{0xff, 0xff, 0x00, 0xff}, {0xff, 0x00, 0xff, 0xff},
{0x00, 0xff, 0xff, 0xff}, {0xff, 0xff, 0xff, 0xff},
};

Controller::Controller() { this->currPadColor = 0; }

Controller::~Controller() {}

Expand All @@ -22,25 +19,16 @@ bool Controller::Init(int controllerUserID) {
return false;
}

// Get the user ID
if (controllerUserID < 0) {
OrbisUserServiceInitializeParams param;
param.priority = ORBIS_KERNEL_PRIO_FIFO_LOWEST;
sceUserServiceInitialize(&param);
sceUserServiceGetInitialUser(&this->userID);
} else {
this->userID = controllerUserID;
}

// Open a handle for the controller
this->userID = controllerUserID;
this->pad = scePadOpen(this->userID, 0, 0, NULL);

if (this->pad < 0) {
DEBUGLOG << "[DEBUG] [ERROR] Failed to open pad!";
return false;
}

scePadSetLightBar(this->pad, &this->padColors[this->currPadColor]);
scePadSetLightBar(this->pad, &padColors[this->currPadColor]);
scePadGetControllerInformation(this->pad, &padInfo);

if (scePadSetMotionSensorState(this->pad, true) != ORBIS_OK) {
Expand All @@ -55,13 +43,20 @@ void Controller::setButtonState(int state) {
this->buttonState = state;
}

bool Controller::CheckButtonsPressed(int stateToCheck) {
scePadReadState(this->pad, &this->padData);
setButtonState(this->padData.buttons);

void Controller::UpdateTriggersFeedback() {
OrbisPadVibeParam pv{.lgMotor = this->padData.analogButtons.r2,
.smMotor = this->padData.analogButtons.l2};
scePadSetVibration(this->pad, &pv);
}

void Controller::ResetTriggersFeedback() {
OrbisPadVibeParam pv{.lgMotor = 0, .smMotor = 0};
scePadSetVibration(this->pad, &pv);
}

bool Controller::CheckButtonsPressed(int stateToCheck) {
scePadReadState(this->pad, &this->padData);
setButtonState(this->padData.buttons);

if (stateToCheck & ORBIS_PAD_BUTTON_TRIANGLE &&
!(this->buttonState & ORBIS_PAD_BUTTON_TRIANGLE))
Expand Down Expand Up @@ -183,11 +178,13 @@ bool Controller::TouchpadPressed() {
return CheckButtonsPressed(ORBIS_PAD_BUTTON_TOUCH_PAD);
}

OrbisPadColor Controller::GetColor() { return padColors[this->currPadColor]; }

OrbisPadColor Controller::NextColor() {
if (scePadSetLightBar(
this->pad, &this->padColors[this->currPadColor =
(this->currPadColor + 1) % 7]) == 0)
return this->padColors[this->currPadColor];
this->pad,
&padColors[this->currPadColor = (this->currPadColor + 1) % 7]) == 0)
return padColors[this->currPadColor];

return {0x00, 0x00, 0x00, 0x00};
}
Expand Down
4 changes: 3 additions & 1 deletion input/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class Controller {
int buttonState;
int currPadColor;
OrbisPadData padData;
OrbisPadColor padColors[8];
OrbisPadInformation padInfo;

void setButtonState(int state);
Expand All @@ -22,6 +21,8 @@ class Controller {

bool Init(int controllerUserID);

void ResetTriggersFeedback();
void UpdateTriggersFeedback();
bool CheckButtonsPressed(int stateToCheck);

bool TrianglePressed();
Expand All @@ -45,6 +46,7 @@ class Controller {
void ReadSticks(float *leftx, float *lefty, float *rightx, float *righty);
void ReadGyro(vec_float4 *data);
void ResetOrientation();
OrbisPadColor GetColor();
OrbisPadColor NextColor();
};

Expand Down
Loading

0 comments on commit d3171f8

Please sign in to comment.