Skip to content

Commit 55466f5

Browse files
kakraJacob Essexatar-axis
committed
xpadneo, mouse: Implement mouse support
Co-authored-by: Jacob Essex <[email protected]> Co-authored-by: Florian Dollinger <[email protected]> Closes: atar-axis#99 Closes: atar-axis#105 Closes: atar-axis#160 Fixes: atar-axis#333 See-also: atar-axis#419 See-also: atar-axis#435 Signed-off-by: Kai Krakow <[email protected]>
1 parent 289d3bf commit 55466f5

File tree

8 files changed

+295
-17
lines changed

8 files changed

+295
-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
@@ -222,6 +223,24 @@ or Y while holding down the Xbox logo button. However, the following caveats app
222223
parameter `disable_shift_mode`).
223224

224225

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

227246
### 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
@@ -719,20 +719,6 @@ static const __u8 *xpadneo_report_fixup(struct hid_device *hdev, __u8 *rdesc, un
719719
return rdesc;
720720
}
721721

722-
static void xpadneo_toggle_mouse(struct xpadneo_devdata *xdata)
723-
{
724-
if (xdata->mouse_mode) {
725-
xdata->mouse_mode = false;
726-
hid_info(xdata->hdev, "mouse mode disabled\n");
727-
} else {
728-
xdata->mouse_mode = true;
729-
hid_info(xdata->hdev, "mouse mode enabled\n");
730-
}
731-
732-
/* Indicate that a request was made */
733-
xdata->profile_switched = true;
734-
}
735-
736722
static void xpadneo_switch_profile(struct xpadneo_devdata *xdata, const u8 profile,
737723
const bool emulated)
738724
{
@@ -839,6 +825,9 @@ static int xpadneo_raw_event(struct hid_device *hdev, struct hid_report *report,
839825
}
840826
}
841827

828+
if (xpadneo_mouse_raw_event(xdata, report, data, reportsize))
829+
return -1;
830+
842831
return 0;
843832
}
844833

@@ -871,6 +860,7 @@ static int xpadneo_input_configured(struct hid_device *hdev, struct hid_input *h
871860
return 0;
872861
default:
873862
hid_warn(hdev, "unhandled input application 0x%x\n", hi->application);
863+
return 0;
874864
}
875865

876866
if (param_disable_deadzones) {
@@ -920,6 +910,9 @@ static int xpadneo_event(struct hid_device *hdev, struct hid_field *field,
920910
struct input_dev *gamepad = xdata->gamepad;
921911
struct input_dev *keyboard = xdata->keyboard;
922912

913+
if (xpadneo_mouse_event(xdata, usage, value))
914+
goto stop_processing;
915+
923916
if ((usage->type == EV_KEY) && (usage->code == BTN_PADDLES(0))) {
924917
if (gamepad && xdata->profile == 0) {
925918
/* report the paddles individually */
@@ -1206,6 +1199,10 @@ static int xpadneo_probe(struct hid_device *hdev, const struct hid_device_id *id
12061199
if (ret)
12071200
return ret;
12081201

1202+
ret = xpadneo_init_mouse(xdata);
1203+
if (ret)
1204+
return ret;
1205+
12091206
ret = xpadneo_init_hw(hdev);
12101207
if (ret) {
12111208
hid_err(hdev, "hw init failed: %d\n", ret);
@@ -1217,6 +1214,9 @@ static int xpadneo_probe(struct hid_device *hdev, const struct hid_device_id *id
12171214
if (ret)
12181215
hid_err(hdev, "could not initialize ff, continuing anyway\n");
12191216

1217+
timer_setup(&xdata->mouse_timer, xpadneo_mouse_report, 0);
1218+
mod_timer(&xdata->mouse_timer, jiffies);
1219+
12201220
hid_info(hdev, "%s connected\n", xdata->battery.name);
12211221

12221222
return 0;
@@ -1252,6 +1252,7 @@ static void xpadneo_remove(struct hid_device *hdev)
12521252
hdev->product = xdata->original_product;
12531253
}
12541254

1255+
del_timer_sync(&xdata->mouse_timer);
12551256
cancel_delayed_work_sync(&xdata->ff_worker);
12561257

12571258
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,8 +202,13 @@ 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 *);
198210
extern void xpadneo_core_missing(struct xpadneo_devdata *, u32);
211+
extern void xpadneo_mouse_report(struct timer_list *);
212+
extern void xpadneo_toggle_mouse(struct xpadneo_devdata *);
199213

200214
#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
@@ -54,6 +54,11 @@ 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
}
5863

5964
extern void xpadneo_core_missing(struct xpadneo_devdata *xdata, u32 flag)
@@ -63,6 +68,9 @@ extern void xpadneo_core_missing(struct xpadneo_devdata *xdata, u32 flag)
6368
if ((xdata->missing_reported & flag) == 0) {
6469
xdata->missing_reported |= flag;
6570
switch (flag) {
71+
case XPADNEO_MISSING_CONSUMER:
72+
hid_err(hdev, "consumer control not detected\n");
73+
break;
6674
case XPADNEO_MISSING_GAMEPAD:
6775
hid_err(hdev, "gamepad not detected\n");
6876
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)