Skip to content

Commit c5620e8

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: #99 Closes: #105 Closes: #160 Closes: #511 Fixes: #333 See-also: #419 See-also: #435 Signed-off-by: Kai Krakow <kai@kaishome.de>
1 parent 6ad9eed commit c5620e8

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
@@ -239,6 +240,24 @@ or Y while holding down the Xbox logo button. However, the following caveats app
239240
parameter `disable_shift_mode`).
240241

241242

243+
### Mouse Profile Support
244+
245+
The driver can switch to emulating a mouse (and limited keyboard) on all supported controllers. Press
246+
<key>Guide</key>+<key>Select</key> to switch to mouse mode or back to controller mode:
247+
248+
- Left stick moves the mouse pointer
249+
- Right stick can be used as a scrolling wheel/ball
250+
- Triggers for left and right mouse button
251+
- Shoulder buttons for back and forward button
252+
- D-pad for cursor movement
253+
- Menu to show on-screen keyboard (FIXME possible? KEY_KEYBOARD)
254+
- A for <key>Enter</key>
255+
- B for <key>Escape</key>
256+
257+
**Important:** The mouse profile won't work if you disabled the shift-mode of the Xbox logo button (module parameter
258+
`disable_shift_mode`).
259+
260+
242261
## Getting Started
243262

244263
### 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
@@ -787,20 +787,6 @@ static const __u8 *xpadneo_report_fixup(struct hid_device *hdev, __u8 *rdesc, un
787787
return rdesc;
788788
}
789789

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

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

@@ -939,6 +928,7 @@ static int xpadneo_input_configured(struct hid_device *hdev, struct hid_input *h
939928
return 0;
940929
default:
941930
hid_warn(hdev, "unhandled input application 0x%x\n", hi->application);
931+
return 0;
942932
}
943933

944934
if (param_disable_deadzones) {
@@ -988,6 +978,9 @@ static int xpadneo_event(struct hid_device *hdev, struct hid_field *field,
988978
struct input_dev *gamepad = xdata->gamepad;
989979
struct input_dev *keyboard = xdata->keyboard;
990980

981+
if (xpadneo_mouse_event(xdata, usage, value))
982+
goto stop_processing;
983+
991984
if ((usage->type == EV_KEY) && (usage->code == BTN_PADDLES(0))) {
992985
if (gamepad && xdata->profile == 0) {
993986
/* report the paddles individually */
@@ -1306,6 +1299,10 @@ static int xpadneo_probe(struct hid_device *hdev, const struct hid_device_id *id
13061299
if (ret)
13071300
return ret;
13081301

1302+
ret = xpadneo_init_mouse(xdata);
1303+
if (ret)
1304+
return ret;
1305+
13091306
ret = xpadneo_init_hw(hdev);
13101307
if (ret) {
13111308
hid_err(hdev, "hw init failed: %d\n", ret);
@@ -1317,6 +1314,9 @@ static int xpadneo_probe(struct hid_device *hdev, const struct hid_device_id *id
13171314
if (ret)
13181315
hid_err(hdev, "could not initialize ff, continuing anyway\n");
13191316

1317+
timer_setup(&xdata->mouse_timer, xpadneo_mouse_report, 0);
1318+
mod_timer(&xdata->mouse_timer, jiffies);
1319+
13201320
hid_info(hdev, "%s connected\n", xdata->battery.name);
13211321

13221322
return 0;
@@ -1352,6 +1352,7 @@ static void xpadneo_remove(struct hid_device *hdev)
13521352
hdev->product = xdata->original_product;
13531353
}
13541354

1355+
del_timer_sync(&xdata->mouse_timer);
13551356
cancel_delayed_work_sync(&xdata->ff_worker);
13561357

13571358
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"
@@ -145,8 +146,8 @@ struct xpadneo_devdata {
145146

146147
/* logical device interfaces */
147148
struct hid_device *hdev;
148-
struct input_dev *consumer, *gamepad, *keyboard;
149-
bool consumer_sync, gamepad_sync, keyboard_sync;
149+
struct input_dev *consumer, *gamepad, *keyboard, *mouse;
150+
bool consumer_sync, gamepad_sync, keyboard_sync, mouse_sync;
150151
short int missing_reported;
151152

152153
/* revert fixups on removal */
@@ -163,6 +164,14 @@ struct xpadneo_devdata {
163164

164165
/* mouse mode */
165166
bool mouse_mode;
167+
struct timer_list mouse_timer;
168+
struct {
169+
s32 rel_x, rel_y, wheel_x, wheel_y;
170+
s32 rel_x_err, rel_y_err, wheel_x_err, wheel_y_err;
171+
struct {
172+
bool left, right;
173+
} analog_button;
174+
} mouse_state;
166175

167176
/* trigger scale */
168177
struct {
@@ -200,8 +209,13 @@ struct xpadneo_devdata {
200209

201210
extern int xpadneo_init_consumer(struct xpadneo_devdata *);
202211
extern int xpadneo_init_keyboard(struct xpadneo_devdata *);
212+
extern int xpadneo_init_mouse(struct xpadneo_devdata *);
203213
extern int xpadneo_init_synthetic(struct xpadneo_devdata *, char *, struct input_dev **);
214+
extern int xpadneo_mouse_event(struct xpadneo_devdata *, struct hid_usage *, __s32);
215+
extern int xpadneo_mouse_raw_event(struct xpadneo_devdata *, struct hid_report *, u8 *, int);
204216
extern void xpadneo_report(struct hid_device *, struct hid_report *);
205217
extern void xpadneo_core_missing(struct xpadneo_devdata *, u32);
218+
extern void xpadneo_mouse_report(struct timer_list *);
219+
extern void xpadneo_toggle_mouse(struct xpadneo_devdata *);
206220

207221
#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)