Skip to content

Commit 2f68332

Browse files
kakraJacob Essexatar-axis
committed
xpadneo, mouse: Implement mouse support
Add an optional "mouse profile" that turns supported Xbox controllers into a couch-friendly mouse/keyboard input device. The mode can be toggled via Guide+Select and reuses the existing input infrastructure by registering an additional (synthetic) mouse device. In mouse mode: - left stick moves the pointer (REL_X/REL_Y) - right stick scrolls (REL_WHEEL/REL_HWHEEL) - triggers act as left/right mouse buttons with hysteresis - shoulder buttons provide back/forward - D-pad generates cursor keys (keyboard interface) - A/B map to Enter/Escape (keyboard interface) - X triggers the on-screen keyboard key event (consumer control) Movement and scrolling are emitted periodically via a timer to provide a stable update rate and to keep input processing isolated from HID report timing. Co-authored-by: Jacob Essex <[email protected]> Co-authored-by: Florian Dollinger <[email protected]> Closes: #99 Closes: #105 Closes: #160 Closes: #511 Fixes: #333 See-also: #419 See-also: #435 Signed-off-by: Kai Krakow <[email protected]>
1 parent fc1b13a commit 2f68332

File tree

8 files changed

+305
-17
lines changed

8 files changed

+305
-17
lines changed

docs/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ PID 0x0B22 while the other models identify with PID 0x0B13. This has some known
106106
- Supports grip paddles (as shipped with the Xbox Elite 2 controller)
107107
- Optional high-precision mode for Wine/Proton users (disables dead zones so games don't apply an additional one)
108108
- Share button support on supported controllers
109+
- Works as a mouse if you are in couch-mode (press <key>Guide</key>+<key>Select</key>)
109110

110111

111112
## Unavailable Features
@@ -280,6 +281,24 @@ or Y while holding down the Xbox logo button. However, the following caveats app
280281
parameter `disable_shift_mode`).
281282

282283

284+
### Mouse Profile Support
285+
286+
The driver can switch to emulating a mouse (and limited keyboard) on all supported controllers. Press
287+
<key>Guide</key>+<key>Select</key> to switch to mouse mode or back to controller mode:
288+
289+
- Left stick moves the mouse pointer
290+
- Right stick can be used as a scrolling wheel/ball
291+
- Triggers for left and right mouse button
292+
- Shoulder buttons for back and forward button
293+
- D-pad for cursor movement
294+
- Menu to show on-screen keyboard (FIXME possible? KEY_KEYBOARD)
295+
- A for <key>Enter</key>
296+
- B for <key>Escape</key>
297+
298+
**Important:** The mouse profile won't work if you disabled the shift-mode of the Xbox logo button (module parameter
299+
`disable_shift_mode`).
300+
301+
283302
## Getting Started
284303

285304
### Distribution Packages

hid-xpadneo/src/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ hid-xpadneo-y += xpadneo.o
1010
$(obj)/xpadneo.c: $(src)/hid-xpadneo.c
1111
cp $< $@
1212

13-
hid-xpadneo-y += xpadneo/core.o xpadneo/consumer.o xpadneo/keyboard.o
13+
hid-xpadneo-y += xpadneo/core.o xpadneo/consumer.o xpadneo/keyboard.o xpadneo/mouse.o

hid-xpadneo/src/hid-xpadneo.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -786,20 +786,6 @@ static const __u8 *xpadneo_report_fixup(struct hid_device *hdev, __u8 *rdesc, un
786786
return rdesc;
787787
}
788788

789-
static void xpadneo_toggle_mouse(struct xpadneo_devdata *xdata)
790-
{
791-
if (xdata->mouse_mode) {
792-
xdata->mouse_mode = false;
793-
hid_info(xdata->hdev, "mouse mode disabled\n");
794-
} else {
795-
xdata->mouse_mode = true;
796-
hid_info(xdata->hdev, "mouse mode enabled\n");
797-
}
798-
799-
/* Indicate that a request was made */
800-
xdata->profile_switched = true;
801-
}
802-
803789
static void xpadneo_switch_profile(struct xpadneo_devdata *xdata, const u8 profile,
804790
const bool emulated)
805791
{
@@ -906,6 +892,9 @@ static int xpadneo_raw_event(struct hid_device *hdev, struct hid_report *report,
906892
}
907893
}
908894

895+
if (xpadneo_mouse_raw_event(xdata, report, data, reportsize))
896+
return -1;
897+
909898
return 0;
910899
}
911900

@@ -938,6 +927,7 @@ static int xpadneo_input_configured(struct hid_device *hdev, struct hid_input *h
938927
return 0;
939928
default:
940929
hid_warn(hdev, "unhandled input application 0x%x\n", hi->application);
930+
return 0;
941931
}
942932

943933
if (param_disable_deadzones) {
@@ -991,6 +981,9 @@ static int xpadneo_event(struct hid_device *hdev, struct hid_field *field,
991981
struct input_dev *gamepad = xdata->gamepad;
992982
struct input_dev *keyboard = xdata->keyboard;
993983

984+
if (xpadneo_mouse_event(xdata, usage, value))
985+
goto stop_processing;
986+
994987
if ((usage->type == EV_KEY) && (usage->code == BTN_GRIPL)) {
995988
if (gamepad && xdata->profile == 0) {
996989
/* report the paddles individually */
@@ -1321,6 +1314,10 @@ static int xpadneo_probe(struct hid_device *hdev, const struct hid_device_id *id
13211314
if (ret)
13221315
return ret;
13231316

1317+
ret = xpadneo_init_mouse(xdata);
1318+
if (ret)
1319+
return ret;
1320+
13241321
ret = xpadneo_init_hw(hdev);
13251322
if (ret) {
13261323
hid_err(hdev, "hw init failed: %d\n", ret);
@@ -1332,6 +1329,9 @@ static int xpadneo_probe(struct hid_device *hdev, const struct hid_device_id *id
13321329
if (ret)
13331330
hid_err(hdev, "could not initialize ff, continuing anyway\n");
13341331

1332+
timer_setup(&xdata->mouse_timer, xpadneo_mouse_report, 0);
1333+
mod_timer(&xdata->mouse_timer, jiffies);
1334+
13351335
hid_info(hdev, "%s connected\n", xdata->battery.name);
13361336

13371337
return 0;
@@ -1367,6 +1367,7 @@ static void xpadneo_remove(struct hid_device *hdev)
13671367
hdev->product = xdata->original_product;
13681368
}
13691369

1370+
timer_delete_sync(&xdata->mouse_timer);
13701371
cancel_delayed_work_sync(&xdata->ff_worker);
13711372

13721373
kfree(xdata->battery.name);

hid-xpadneo/src/xpadneo.h

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include <linux/hid.h>
1515
#include <linux/input-event-codes.h>
16+
#include <linux/timer.h>
1617
#include <linux/version.h>
1718

1819
#include "hid-ids.h"
@@ -27,6 +28,16 @@
2728
#error "kernel version 4.18.0+ required for HID_QUIRK_INPUT_PER_APP"
2829
#endif
2930

31+
/* v6.15: del_timer_sync() renamed to timer_delete_sync() */
32+
#if KERNEL_VERSION(6, 15, 0) > LINUX_VERSION_CODE
33+
#define timer_delete_sync(t) del_timer_sync(t)
34+
#endif
35+
36+
/* v6.16: from_timer() renamed to timer_container_of() */
37+
#ifndef timer_container_of
38+
#define timer_container_of(v, c, t) from_timer(v, c, t)
39+
#endif
40+
3041
#ifndef VERSION
3142
#error "xpadneo version not defined"
3243
#endif
@@ -165,8 +176,8 @@ struct xpadneo_devdata {
165176

166177
/* logical device interfaces */
167178
struct hid_device *hdev;
168-
struct input_dev *consumer, *gamepad, *keyboard;
169-
bool consumer_sync, gamepad_sync, keyboard_sync;
179+
struct input_dev *consumer, *gamepad, *keyboard, *mouse;
180+
bool consumer_sync, gamepad_sync, keyboard_sync, mouse_sync;
170181
short int missing_reported;
171182

172183
/* revert fixups on removal */
@@ -183,6 +194,14 @@ struct xpadneo_devdata {
183194

184195
/* mouse mode */
185196
bool mouse_mode;
197+
struct timer_list mouse_timer;
198+
struct {
199+
s32 rel_x, rel_y, wheel_x, wheel_y;
200+
s32 rel_x_err, rel_y_err, wheel_x_err, wheel_y_err;
201+
struct {
202+
bool left, right;
203+
} analog_button;
204+
} mouse_state;
186205

187206
/* trigger scale */
188207
struct {
@@ -220,8 +239,13 @@ struct xpadneo_devdata {
220239

221240
extern int xpadneo_init_consumer(struct xpadneo_devdata *);
222241
extern int xpadneo_init_keyboard(struct xpadneo_devdata *);
242+
extern int xpadneo_init_mouse(struct xpadneo_devdata *);
223243
extern int xpadneo_init_synthetic(struct xpadneo_devdata *, char *, struct input_dev **);
244+
extern int xpadneo_mouse_event(struct xpadneo_devdata *, struct hid_usage *, __s32);
245+
extern int xpadneo_mouse_raw_event(struct xpadneo_devdata *, struct hid_report *, u8 *, int);
224246
extern void xpadneo_report(struct hid_device *, struct hid_report *);
225247
extern void xpadneo_core_missing(struct xpadneo_devdata *, u32);
248+
extern void xpadneo_mouse_report(struct timer_list *);
249+
extern void xpadneo_toggle_mouse(struct xpadneo_devdata *);
226250

227251
#endif

hid-xpadneo/src/xpadneo/consumer.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ extern int xpadneo_init_consumer(struct xpadneo_devdata *xdata)
1818
return ret;
1919
}
2020

21+
/* enable consumer events for mouse mode */
22+
input_set_capability(xdata->consumer, EV_KEY, KEY_ONSCREEN_KEYBOARD);
23+
2124
if (synth) {
2225
ret = input_register_device(xdata->consumer);
2326
if (ret) {

hid-xpadneo/src/xpadneo/core.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ extern void xpadneo_report(struct hid_device *hdev, struct hid_report *report)
5555
xdata->keyboard_sync = false;
5656
input_sync(xdata->keyboard);
5757
}
58+
59+
if (xdata->mouse && xdata->mouse_sync) {
60+
xdata->mouse_sync = false;
61+
input_sync(xdata->mouse);
62+
}
5863
}
5964

6065
extern void xpadneo_core_missing(struct xpadneo_devdata *xdata, u32 flag)
@@ -64,6 +69,9 @@ extern void xpadneo_core_missing(struct xpadneo_devdata *xdata, u32 flag)
6469
if ((xdata->missing_reported & flag) == 0) {
6570
xdata->missing_reported |= flag;
6671
switch (flag) {
72+
case XPADNEO_MISSING_CONSUMER:
73+
hid_err(hdev, "consumer control not detected\n");
74+
break;
6775
case XPADNEO_MISSING_GAMEPAD:
6876
hid_err(hdev, "gamepad not detected\n");
6977
break;

hid-xpadneo/src/xpadneo/keyboard.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ extern int xpadneo_init_keyboard(struct xpadneo_devdata *xdata)
2121
/* enable key events for keyboard */
2222
input_set_capability(xdata->keyboard, EV_KEY, BTN_SHARE);
2323

24+
/* enable key events for mouse mode */
25+
input_set_capability(xdata->keyboard, EV_KEY, KEY_ESC);
26+
input_set_capability(xdata->keyboard, EV_KEY, KEY_ENTER);
27+
input_set_capability(xdata->keyboard, EV_KEY, KEY_UP);
28+
input_set_capability(xdata->keyboard, EV_KEY, KEY_LEFT);
29+
input_set_capability(xdata->keyboard, EV_KEY, KEY_RIGHT);
30+
input_set_capability(xdata->keyboard, EV_KEY, KEY_DOWN);
31+
2432
if (synth) {
2533
ret = input_register_device(xdata->keyboard);
2634
if (ret) {

0 commit comments

Comments
 (0)