Skip to content

Commit 89b24ac

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

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

223224

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

226245
### 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
@@ -715,20 +715,6 @@ static u8 *xpadneo_report_fixup(struct hid_device *hdev, u8 *rdesc, unsigned int
715715
return rdesc;
716716
}
717717

718-
static void xpadneo_toggle_mouse(struct xpadneo_devdata *xdata)
719-
{
720-
if (xdata->mouse_mode) {
721-
xdata->mouse_mode = false;
722-
hid_info(xdata->hdev, "mouse mode disabled\n");
723-
} else {
724-
xdata->mouse_mode = true;
725-
hid_info(xdata->hdev, "mouse mode enabled\n");
726-
}
727-
728-
/* Indicate that a request was made */
729-
xdata->profile_switched = true;
730-
}
731-
732718
static void xpadneo_switch_profile(struct xpadneo_devdata *xdata, const u8 profile,
733719
const bool emulated)
734720
{
@@ -835,6 +821,9 @@ static int xpadneo_raw_event(struct hid_device *hdev, struct hid_report *report,
835821
}
836822
}
837823

824+
if (xpadneo_mouse_raw_event(xdata, report, data, reportsize))
825+
return -1;
826+
838827
return 0;
839828
}
840829

@@ -867,6 +856,7 @@ static int xpadneo_input_configured(struct hid_device *hdev, struct hid_input *h
867856
return 0;
868857
default:
869858
hid_warn(hdev, "unhandled input application 0x%x\n", hi->application);
859+
return 0;
870860
}
871861

872862
if (param_disable_deadzones) {
@@ -916,6 +906,9 @@ static int xpadneo_event(struct hid_device *hdev, struct hid_field *field,
916906
struct input_dev *gamepad = xdata->gamepad;
917907
struct input_dev *keyboard = xdata->keyboard;
918908

909+
if (xpadneo_mouse_event(xdata, usage, value))
910+
goto stop_processing;
911+
919912
if ((usage->type == EV_KEY) && (usage->code == BTN_PADDLES(0))) {
920913
if (gamepad && xdata->profile == 0) {
921914
/* report the paddles individually */
@@ -1208,6 +1201,10 @@ static int xpadneo_probe(struct hid_device *hdev, const struct hid_device_id *id
12081201
if (ret)
12091202
return ret;
12101203

1204+
ret = xpadneo_init_mouse(xdata);
1205+
if (ret)
1206+
return ret;
1207+
12111208
ret = xpadneo_init_hw(hdev);
12121209
if (ret) {
12131210
hid_err(hdev, "hw init failed: %d\n", ret);
@@ -1219,6 +1216,9 @@ static int xpadneo_probe(struct hid_device *hdev, const struct hid_device_id *id
12191216
if (ret)
12201217
hid_err(hdev, "could not initialize ff, continuing anyway\n");
12211218

1219+
timer_setup(&xdata->mouse_timer, xpadneo_mouse_report, 0);
1220+
mod_timer(&xdata->mouse_timer, jiffies);
1221+
12221222
hid_info(hdev, "%s connected\n", xdata->battery.name);
12231223

12241224
return 0;
@@ -1254,6 +1254,7 @@ static void xpadneo_remove(struct hid_device *hdev)
12541254
hdev->product = xdata->original_product;
12551255
}
12561256

1257+
del_timer_sync(&xdata->mouse_timer);
12571258
cancel_delayed_work_sync(&xdata->ff_worker);
12581259

12591260
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)