Skip to content

Commit

Permalink
Add experimental support for pro controllers
Browse files Browse the repository at this point in the history
Bump version to 0.1.2
  • Loading branch information
v1993 committed Jan 30, 2022
1 parent 948ec11 commit f638634
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 24 deletions.
1 change: 1 addition & 0 deletions ExampleConfig.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ Orientation=normal
SendButtons=true
NunchuckStickCalibration=0,2,95,83
ClassicControllerStickCalibration=0,0,25,25,0,0,25,25
ProControllerStickCalibration=0,0,1000,1000,0,0,1000,1000
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

## Current features

* Support for Wiimotes and Nunchucks
* Support for calibration of gyro and nunchuck stick
* Support for Wiimotes, Nunchucks, Classic and Pro Controllers
* Support for calibration of gyro and sticks
* Support for buttons/sticks as well as motion
* Support for changing device orientation

Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
project('linuxmotehook2', ['c', 'vala'],
version: '0.1.1',
version: '0.1.2',
meson_version: '>= 0.50.0',
default_options: [ 'warning_level=2',
],
Expand Down
2 changes: 1 addition & 1 deletion src/ClassicController.vala
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ namespace Linuxmotehook {
break;

case HOME:
btn = PS;
btn = HOME;
break;
case MINUS:
btn = SHARE;
Expand Down
6 changes: 6 additions & 0 deletions src/Config.vala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace Linuxmotehook {
public bool send_buttons;
public int nunchuck_stick_calibration[4];
public int classic_controller_stick_calibration[8];
public int pro_controller_stick_calibration[8];

// TODO: initialize fields directly once initializing from arrays is fixed
// Also don't incherit from Object once that's done
Expand All @@ -34,6 +35,7 @@ namespace Linuxmotehook {
send_buttons = false;
nunchuck_stick_calibration = {0, 0, 80, 80};
classic_controller_stick_calibration = {0, 0, 25, 25, 0, 0, 25, 25};
pro_controller_stick_calibration = {0, 0, 1000, 1000, 0, 0, 1000, 1000};
}
}

Expand Down Expand Up @@ -168,6 +170,9 @@ namespace Linuxmotehook {
case "ClassicControllerStickCalibration":
conf.classic_controller_stick_calibration = kfile_get_integer_list_checked(kfile, group, key, 8);
break;
case "ProControllerStickCalibration":
conf.pro_controller_stick_calibration = kfile_get_integer_list_checked(kfile, group, key, 8);
break;
default:
warning("Unknown configuration key %s", key);
break;
Expand Down Expand Up @@ -197,6 +202,7 @@ namespace Linuxmotehook {
kfile.set_boolean(group, "SendButtons", conf.send_buttons);
kfile.set_integer_list(group, "NunchuckStickCalibration", conf.nunchuck_stick_calibration);
kfile.set_integer_list(group, "ClassicControllerStickCalibration", conf.classic_controller_stick_calibration);
kfile.set_integer_list(group, "ProControllerStickCalibration", conf.pro_controller_stick_calibration);
}
}
}
147 changes: 128 additions & 19 deletions src/MainDevice.vala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ namespace Linuxmotehook {
private PollableSource dev_source;

private Cemuhook.DeviceType devtype = NO_MOTION;
private Cemuhook.Buttons buttons = 0;
private Cemuhook.BaseData base_data = Cemuhook.BaseData() {
buttons = 0,
left_x = Cemuhook.STICK_NEUTRAL,
left_y = Cemuhook.STICK_NEUTRAL,
right_x = Cemuhook.STICK_NEUTRAL,
right_y = Cemuhook.STICK_NEUTRAL
};
private uint64 motion_timestamp = 0;
private Cemuhook.MotionData accelerometer = {0f, 0f, 0f};
private Cemuhook.MotionData gyroscope = {0f, 0f, 0f};
Expand Down Expand Up @@ -81,6 +87,14 @@ namespace Linuxmotehook {
} catch(IOError e) {
warning("Failed to open buttons interface: %s\n", e.message);
}
} else if (PRO_CONTROLLER in unopened && conf.send_buttons) {
// Pro controller is a separate device rather than extension, so handle it here
try {
info("Opening pro controller interface");
dev.open(PRO_CONTROLLER);
} catch(IOError e) {
warning("Failed to open pro controller's interface: %s\n", e.message);
}
}

/*if (IR in unopened && conf.send_ir) {
Expand Down Expand Up @@ -113,8 +127,15 @@ namespace Linuxmotehook {
update_motion_status(dev.opened());

// Clear unavailable interfaces
if (!(CORE in opened)) {
buttons = 0;
if (!((CORE | PRO_CONTROLLER) in opened)) {
base_data.buttons = 0;
}

if (!(PRO_CONTROLLER in opened)) {
base_data.left_x = Cemuhook.STICK_NEUTRAL;
base_data.left_y = Cemuhook.STICK_NEUTRAL;
base_data.right_x = Cemuhook.STICK_NEUTRAL;
base_data.right_y = Cemuhook.STICK_NEUTRAL;
}

if ((extension != null) && !(extension.implements_interface in opened)) {
Expand Down Expand Up @@ -160,13 +181,16 @@ namespace Linuxmotehook {
}
}

if (CLASSIC_CONTROLLER in unopened) {
try {
info("Opening classic controller interface");
dev.open(CLASSIC_CONTROLLER);
extension = new ClassicController(this);
} catch(IOError e) {
warning("Failed to open classic controller interface: %s\n", e.message);
// Extensions that have no motion are only connected if send_buttons is enabled
if (conf.send_buttons) {
if (CLASSIC_CONTROLLER in unopened) {
try {
info("Opening classic controller interface");
dev.open(CLASSIC_CONTROLLER);
extension = new ClassicController(this);
} catch(IOError e) {
warning("Failed to open classic controller interface: %s\n", e.message);
}
}
}

Expand Down Expand Up @@ -240,6 +264,14 @@ namespace Linuxmotehook {
process_key(ev.key.code, ev.key.state);
needs_update = true;
break;
case PRO_CONTROLLER_KEY:
process_pro_controller_key(ev.key.code, ev.key.state);
needs_update = true;
break;
case PRO_CONTROLLER_MOVE:
process_pro_controller_movement(ev.abs[0], ev.abs[1]);
needs_update = true;
break;
case ACCEL:
motion_timestamp = ev.time_sec * 1000000 + ev.time_usec;
process_accelerometer(ev.abs[0]);
Expand Down Expand Up @@ -330,16 +362,99 @@ namespace Linuxmotehook {

switch (state) {
case DOWN:
buttons |= btn;
base_data.buttons |= btn;
break;
case UP:
base_data.buttons &= ~btn;
break;
case AUTOREPEAT:
break;
}
}

private void process_pro_controller_key(XWiimote.EventKeyCode code, XWiimote.EventKeyState state) {
Cemuhook.Buttons btn;
switch (code) {
case UP:
btn = UP;
break;
case RIGHT:
btn = RIGHT;
break;
case DOWN:
btn = DOWN;
break;
case LEFT:
btn = LEFT;
break;

case A:
btn = A;
break;
case B:
btn = B;
break;
case X:
btn = X;
break;
case Y:
btn = Y;
break;

case TR:
btn = R1;
break;
case TL:
btn = L1;
break;
case ZR:
btn = R2;
break;
case ZL:
btn = L2;
break;
case THUMBL:
btn = L3;
break;
case THUMBR:
btn = R3;
break;

case HOME:
btn = HOME;
break;
case MINUS:
btn = SHARE;
break;
case PLUS:
btn = OPTIONS;
break;

default:
warn_if_reached();
return;
}

switch (state) {
case DOWN:
base_data.buttons |= btn;
break;
case UP:
buttons &= ~btn;
base_data.buttons &= ~btn;
break;
case AUTOREPEAT:
break;
}
}

private void process_pro_controller_movement(XWiimote.EventAbs left, XWiimote.EventAbs right) {
unowned var calibr = conf.pro_controller_stick_calibration;
base_data.left_x = apply_stick_calibration(left.x, calibr[0], calibr[2]);
base_data.left_y = apply_stick_calibration(left.y, calibr[1], calibr[3]);
base_data.right_x = apply_stick_calibration(right.x, calibr[4], calibr[6]);
base_data.right_y = apply_stick_calibration(right.y, calibr[5], calibr[7]);
}

private void process_accelerometer(XWiimote.EventAbs inp) {
accelerometer = Cemuhook.MotionData() {
x = ((float)inp.x) / ACCEL_UNITS_PER_G,
Expand Down Expand Up @@ -396,13 +511,7 @@ namespace Linuxmotehook {
public uint64 get_mac() { return mac; }

public Cemuhook.BaseData get_base_inputs() {
return Cemuhook.BaseData() {
buttons = buttons,
left_x = Cemuhook.STICK_NEUTRAL,
left_y = Cemuhook.STICK_NEUTRAL,
right_x = Cemuhook.STICK_NEUTRAL,
right_y = Cemuhook.STICK_NEUTRAL
};
return base_data;
}

public uint64 get_motion_timestamp() {
Expand Down
2 changes: 1 addition & 1 deletion subprojects/gcemuhook

0 comments on commit f638634

Please sign in to comment.