Skip to content

Commit 70a9108

Browse files
committed
Mouse Wheel Bug Fixes
1 parent 7afe1a6 commit 70a9108

File tree

7 files changed

+40
-28
lines changed

7 files changed

+40
-28
lines changed

include/uiohook.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ typedef struct _mouse_wheel_event_data {
109109
int16_t x;
110110
int16_t y;
111111
uint8_t type;
112+
uint8_t amount;
112113
int16_t rotation;
113114
uint16_t delta;
114115
uint8_t direction;

src/darwin/post_event.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ static int post_mouse_wheel_event(uiohook_event * const event, CGEventSourceRef
238238

239239
CGEventRef cg_event = CGEventCreateScrollWheelEvent(
240240
src,
241-
kCGScrollEventUnitLine,
241+
scroll_unit,
242242
// TODO Currently only support 1 wheel axis.
243243
(CGWheelCount) 1, // 1 for Y-only, 2 for Y-X, 3 for Y-X-Z
244244
event->data.wheel.rotation // TODO Is this value correct? Do we need PPL?

src/windows/dispatch_event.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,13 +456,15 @@ bool dispatch_mouse_wheel(MSLLHOOKSTRUCT *mshook, uint8_t direction) {
456456

457457
uio_event.data.wheel.type = WHEEL_BLOCK_SCROLL;
458458
uio_event.data.wheel.rotation *= 1;
459+
uio_event.data.wheel.amount = 1;
459460
} else {
460461
/* If this number is 0, no scrolling should occur.
461462
* If the number of lines to scroll is greater than the number of lines viewable, the scroll operation
462463
* should also be interpreted as a page down or page up operation. */
463464

464465
uio_event.data.wheel.type = WHEEL_UNIT_SCROLL;
465466
uio_event.data.wheel.rotation *= wheel_amount;
467+
uio_event.data.wheel.amount = wheel_amount;
466468
}
467469

468470
// Set the direction based on what event was received.

src/windows/post_event.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,9 @@ static int map_mouse_event(uiohook_event * const event, INPUT * const input) {
184184
case EVENT_MOUSE_WHEEL:
185185
input->mi.dwFlags = MOUSEEVENTF_WHEEL;
186186

187-
// type, amount and rotation?
187+
// TODO What are we going to do about delta=? We could normalize with rotation *= (120 / delta)
188+
// TODO What are we going to do about type=WHEEL_BLOCK_SCROLL?
189+
// TODO What are we going to do about amount=? Is this really a setting, not event property?
188190
input->mi.mouseData = event->data.wheel.rotation;
189191
break;
190192

src/x11/dispatch_event.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,14 @@ static void dispatch_mouse_wheel_rotated(XButtonEvent * const x_event) {
242242
/* Some scroll wheel properties are available via the new XInput2 (XI2) extension. Unfortunately the extension is
243243
* not available on my development platform at this time. For the time being we will just use the Windows default
244244
* value of 3. */
245+
uio_event.data.wheel.amount = WHEEL_AMOUNT;
245246
uio_event.data.wheel.delta = 100;
246247
if (x_event->button == WheelDown || x_event->button == WheelLeft) {
247248
// Wheel Rotated Up and Away.
248-
uio_event.data.wheel.rotation = -3 * uio_event.data.wheel.delta;
249+
uio_event.data.wheel.rotation = -1 * uio_event.data.wheel.delta;
249250
} else { // event.button == WheelUp || event.button == WheelRight
250251
// Wheel Rotated Down and Towards.
251-
uio_event.data.wheel.rotation = 3 * uio_event.data.wheel.delta;
252+
uio_event.data.wheel.rotation = uio_event.data.wheel.delta;
252253
}
253254

254255
if (x_event->button == WheelUp || x_event->button == WheelDown) {

src/x11/input_helper.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
#define XButton1 8
3636
#define XButton2 9
3737

38+
// The static number of clicks for X11 because it is a non-configurable amount.
39+
#define WHEEL_AMOUNT 3
40+
3841
// For this struct, refer to libxnee, requires Xlibint.h
3942
typedef union {
4043
unsigned char type;

src/x11/post_event.c

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -119,40 +119,43 @@ static int post_mouse_button_event(uiohook_event * const event) {
119119
static int post_mouse_wheel_event(uiohook_event * const event) {
120120
int status = UIOHOOK_FAILURE;
121121

122-
XButtonEvent btn_event = {
123-
.serial = 0,
124-
.send_event = False,
125-
.display = helper_disp,
122+
if (uio_event.data.wheel.delta > 0) {
123+
XButtonEvent btn_event = {
124+
.serial = 0,
125+
.send_event = False,
126+
.display = helper_disp,
126127

127-
.window = None, /* “event” window it is reported relative to */
128-
.root = None, /* root window that the event occurred on */
129-
.subwindow = XDefaultRootWindow(helper_disp), /* child window */
128+
.window = None, /* “event” window it is reported relative to */
129+
.root = None, /* root window that the event occurred on */
130+
.subwindow = XDefaultRootWindow(helper_disp), /* child window */
130131

131-
.time = CurrentTime,
132+
.time = CurrentTime,
132133

133-
.x = event->data.wheel.x, /* pointer x, y coordinates in event window */
134-
.y = event->data.wheel.y,
134+
.x = event->data.wheel.x, /* pointer x, y coordinates in event window */
135+
.y = event->data.wheel.y,
135136

136-
.x_root = 0, /* coordinates relative to root */
137-
.y_root = 0,
137+
.x_root = 0, /* coordinates relative to root */
138+
.y_root = 0,
138139

139-
.state = 0x00, /* key or button mask */
140-
.same_screen = True
141-
};
140+
.state = 0x00, /* key or button mask */
141+
.same_screen = True
142+
};
142143

143-
// Wheel events should be the same as click events on X11.
144-
// type, amount and rotation
145-
unsigned int button = button_map_lookup(event->data.wheel.rotation < 0 ? WheelUp : WheelDown);
144+
unsigned int button = button_map_lookup(event->data.wheel.rotation < 0 ? WheelUp : WheelDown);
146145

147-
if (XTestFakeButtonEvent(helper_disp, button, True, 0) != 0) {
146+
// X11 does not support a rotation amount so we will emulate the number of rotations based on the static wheel amount.
147+
// TODO What are we going to do about type=WHEEL_BLOCK_SCROLL?
148+
// TODO What are we going to do about amount=? Is this really a setting, not event property?
148149
status = UIOHOOK_SUCCESS;
150+
for (int i = abs(event->data.wheel.rotation) / (event.data.wheel.delta * event.data.wheel.amount); i > 0 && status == UIOHOOK_SUCCESS; i--) {
151+
// Wheel events are be the same as click events on X11.
152+
if (!XTestFakeButtonEvent(helper_disp, button, True, 0) || !XTestFakeButtonEvent(helper_disp, button, False, 0)) {
153+
status = UIOHOOK_FAILURE;
154+
}
155+
}
149156
}
150157

151-
if (status == UIOHOOK_SUCCESS && XTestFakeButtonEvent(helper_disp, button, False, 0) == 0) {
152-
status = UIOHOOK_FAILURE;
153-
}
154-
155-
return UIOHOOK_SUCCESS;
158+
return status;
156159
}
157160

158161
static int post_mouse_motion_event(uiohook_event * const event) {

0 commit comments

Comments
 (0)