Skip to content

Commit d4539af

Browse files
kakraJacob Essexatar-axis
committed
xpadneo, mouse: Implement mouse support
Co-authored-by: Jacob Essex <git@jacobessex.com> Co-authored-by: Florian Dollinger <dollinger.florian@gmx.de> Closes: atar-axis#160 Closes: atar-axis#105 Closes: atar-axis#99 Signed-off-by: Kai Krakow <kai@kaishome.de>
1 parent 5ab9bcf commit d4539af

File tree

8 files changed

+299
-17
lines changed

8 files changed

+299
-17
lines changed

docs/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ PID 0x0B22 while the other models identify with PID 0x0B13. This has some known
9191
* Supports customization through profiles (work in progress)
9292
* Optional high-precision mode for Wine/Proton users (disables dead zones so games don't apply an additional one)
9393
* Share button support on supported controllers
94+
* Works as a mouse if you're are in couch-mode (press <key>Guide</key>+<key>Select</key>)
9495

9596

9697
## Unavailable Features
@@ -218,6 +219,24 @@ or Y while holding down the Xbox logo button. However, the following caveats app
218219
- If you hold the button for too long, the controller will turn off - we cannot prevent that.
219220

220221

222+
### Mouse Profile Support
223+
224+
The driver can switch to emulating a mouse (and limited keyboard) on all supported controllers. Press
225+
<key>Guide</key>+<key>Select</key> to switch to mouse mode or back to controller mode:
226+
227+
- Left stick moves the mouse pointer
228+
- Right stick can be used as a scrolling wheel/ball
229+
- Triggers for left and right mouse button
230+
- Shoulder buttons for back and forward button
231+
- D-pad for cursor movement
232+
- Menu to show on-screen keyboard (FIXME possible? KEY_KEYBOARD)
233+
- A for <key>Enter</key>
234+
- B for <key>Escape</key>
235+
236+
**Important:** The mouse profile won't work if you disabled the shift-mode of the Xbox logo button (module parameter
237+
`disable_shift_mode`).
238+
239+
221240
## Getting Started
222241

223242
### Distribution Packages

hid-xpadneo/src/Makefile

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

12-
hid-xpadneo-y += xpadneo/core.o xpadneo/consumer.o xpadneo/keyboard.o
12+
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
@@ -709,20 +709,6 @@ static u8 *xpadneo_report_fixup(struct hid_device *hdev, u8 *rdesc, unsigned int
709709
return rdesc;
710710
}
711711

712-
static void xpadneo_toggle_mouse(struct xpadneo_devdata *xdata)
713-
{
714-
if (xdata->mouse_mode) {
715-
xdata->mouse_mode = false;
716-
hid_info(xdata->hdev, "mouse mode disabled\n");
717-
} else {
718-
xdata->mouse_mode = true;
719-
hid_info(xdata->hdev, "mouse mode enabled\n");
720-
}
721-
722-
/* Indicate that a request was made */
723-
xdata->profile_switched = true;
724-
}
725-
726712
static void xpadneo_switch_profile(struct xpadneo_devdata *xdata, const u8 profile,
727713
const bool emulated)
728714
{
@@ -829,6 +815,9 @@ static int xpadneo_raw_event(struct hid_device *hdev, struct hid_report *report,
829815
}
830816
}
831817

818+
if (xpadneo_mouse_raw_event(xdata, report, data, reportsize))
819+
return -1;
820+
832821
return 0;
833822
}
834823

@@ -861,6 +850,7 @@ static int xpadneo_input_configured(struct hid_device *hdev, struct hid_input *h
861850
return 0;
862851
default:
863852
hid_warn(hdev, "unhandled input application 0x%x\n", hi->application);
853+
return 0;
864854
}
865855

866856
if (param_disable_deadzones) {
@@ -910,6 +900,9 @@ static int xpadneo_event(struct hid_device *hdev, struct hid_field *field,
910900
struct input_dev *gamepad = xdata->gamepad;
911901
struct input_dev *keyboard = xdata->keyboard;
912902

903+
if (xpadneo_mouse_event(xdata, usage, value))
904+
goto stop_processing;
905+
913906
if ((usage->type == EV_KEY) && (usage->code == BTN_PADDLES(0))) {
914907
if (gamepad && xdata->profile == 0) {
915908
/* report the paddles individually */
@@ -1201,6 +1194,10 @@ static int xpadneo_probe(struct hid_device *hdev, const struct hid_device_id *id
12011194
if (ret)
12021195
return ret;
12031196

1197+
ret = xpadneo_init_mouse(xdata);
1198+
if (ret)
1199+
return ret;
1200+
12041201
ret = xpadneo_init_hw(hdev);
12051202
if (ret) {
12061203
hid_err(hdev, "hw init failed: %d\n", ret);
@@ -1212,6 +1209,9 @@ static int xpadneo_probe(struct hid_device *hdev, const struct hid_device_id *id
12121209
if (ret)
12131210
hid_err(hdev, "could not initialize ff, continuing anyway\n");
12141211

1212+
timer_setup(&xdata->mouse_timer, xpadneo_mouse_report, 0);
1213+
mod_timer(&xdata->mouse_timer, jiffies);
1214+
12151215
hid_info(hdev, "%s connected\n", xdata->battery.name);
12161216

12171217
return 0;
@@ -1247,6 +1247,7 @@ static void xpadneo_remove(struct hid_device *hdev)
12471247
hdev->product = xdata->original_product;
12481248
}
12491249

1250+
del_timer_sync(&xdata->mouse_timer);
12501251
cancel_delayed_work_sync(&xdata->ff_worker);
12511252

12521253
kfree(xdata->battery.name);

hid-xpadneo/src/xpadneo.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#define XPADNEO_H_FILE
1313

1414
#include <linux/hid.h>
15+
#include <linux/timer.h>
1516
#include <linux/version.h>
1617

1718
#include "hid-ids.h"
@@ -138,8 +139,8 @@ struct xpadneo_devdata {
138139

139140
/* logical device interfaces */
140141
struct hid_device *hdev;
141-
struct input_dev *consumer, *gamepad, *keyboard;
142-
bool consumer_sync, gamepad_sync, keyboard_sync;
142+
struct input_dev *consumer, *gamepad, *keyboard, *mouse;
143+
bool consumer_sync, gamepad_sync, keyboard_sync, mouse_sync;
143144
short int missing_reported;
144145

145146
/* revert fixups on removal */
@@ -156,6 +157,14 @@ struct xpadneo_devdata {
156157

157158
/* mouse mode */
158159
bool mouse_mode;
160+
struct timer_list mouse_timer;
161+
struct {
162+
s32 rel_x, rel_y, wheel_x, wheel_y;
163+
s32 rel_x_err, rel_y_err, wheel_x_err, wheel_y_err;
164+
struct {
165+
bool left, right;
166+
} analog_button;
167+
} mouse_state;
159168

160169
/* trigger scale */
161170
struct {
@@ -193,7 +202,12 @@ struct xpadneo_devdata {
193202

194203
extern int xpadneo_init_consumer(struct xpadneo_devdata *);
195204
extern int xpadneo_init_keyboard(struct xpadneo_devdata *);
205+
extern int xpadneo_init_mouse(struct xpadneo_devdata *);
196206
extern int xpadneo_init_synthetic(struct xpadneo_devdata *, char *, struct input_dev **);
207+
extern int xpadneo_mouse_event(struct xpadneo_devdata *, struct hid_usage *, __s32);
208+
extern int xpadneo_mouse_raw_event(struct xpadneo_devdata *, struct hid_report *, u8 *, int);
197209
extern void xpadneo_report(struct hid_device *, struct hid_report *);
210+
extern void xpadneo_mouse_report(struct timer_list *);
211+
extern void xpadneo_toggle_mouse(struct xpadneo_devdata *);
198212

199213
#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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,9 @@ extern void xpadneo_report(struct hid_device *hdev, struct hid_report *report)
5454
xdata->keyboard_sync = false;
5555
input_sync(xdata->keyboard);
5656
}
57+
58+
if (xdata->mouse && xdata->mouse_sync) {
59+
xdata->mouse_sync = false;
60+
input_sync(xdata->mouse);
61+
}
5762
}

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)