Skip to content

Commit

Permalink
Distinguish emulated input and user input. Fxies #110
Browse files Browse the repository at this point in the history
  • Loading branch information
kwhat committed Apr 16, 2024
1 parent 19e5b18 commit 362d944
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 75 deletions.
2 changes: 1 addition & 1 deletion demo/demo_hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static void logger(unsigned int level, const char *format, ...) {
void dispatch_proc(uiohook_event * const event, void *user_data) {
char buffer[256] = { 0 };
size_t length = snprintf(buffer, sizeof(buffer),
"id=%i,when=%" PRIu64 ",mask=0x%X",
"id=%i,when=%" PRIu64 ",mask=0x%X",
event->type, event->time, event->mask);

switch (event->type) {
Expand Down
6 changes: 4 additions & 2 deletions include/uiohook.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ typedef struct _mouse_wheel_event_data {
typedef struct _uiohook_event {
event_type type;
uint64_t time;
uint16_t mask;
uint16_t reserved;
uint32_t mask;
union {
keyboard_event_data keyboard;
mouse_event_data mouse;
Expand Down Expand Up @@ -453,6 +452,9 @@ typedef void (*dispatcher_t)(uiohook_event * const, void *);
#define MASK_NUM_LOCK 1 << 13
#define MASK_CAPS_LOCK 1 << 14
#define MASK_SCROLL_LOCK 1 << 15

#define MASK_EMULATED 1 << 30
#define MASK_CONSUMED 1 << 31
/* End Virtual Modifier Masks */


Expand Down
68 changes: 36 additions & 32 deletions src/darwin/dispatch_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,12 @@ bool dispatch_hook_enabled(uint64_t timestamp) {

// Populate the hook start event.
uio_event.time = timestamp;
uio_event.reserved = 0x00;

uio_event.type = EVENT_HOOK_ENABLED;
uio_event.mask = 0x00;

// Fire the hook start event.
dispatch_event(&uio_event);
consumed = uio_event.reserved & 0x01;
consumed = uio_event.mask & MASK_CONSUMED;

return consumed;
}
Expand All @@ -91,8 +89,6 @@ bool dispatch_hook_disabled(uint64_t timestamp) {

// Populate the hook stop event.
uio_event.time = timestamp;
uio_event.reserved = 0x00;

uio_event.type = EVENT_HOOK_DISABLED;
uio_event.mask = 0x00;

Expand All @@ -101,7 +97,7 @@ bool dispatch_hook_disabled(uint64_t timestamp) {

// Deinitialize native input helper functions.
unload_input_helper();
consumed = uio_event.reserved & 0x01;
consumed = uio_event.mask & MASK_CONSUMED;

return consumed;
}
Expand All @@ -113,10 +109,11 @@ bool dispatch_key_press(uint64_t timestamp, CGEventRef event_ref) {

// Populate key pressed event.
uio_event.time = timestamp;
uio_event.reserved = 0x00;

uio_event.type = EVENT_KEY_PRESSED;
uio_event.mask = get_modifiers();
if (CGEventGetIntegerValueField(event_ref, kCGEventSourceUnixProcessID)) {
uio_event.mask |= MASK_EMULATED;
}

uio_event.data.keyboard.keycode = keycode_to_scancode(keycode);
uio_event.data.keyboard.rawcode = keycode;
Expand All @@ -128,7 +125,7 @@ bool dispatch_key_press(uint64_t timestamp, CGEventRef event_ref) {

// Fire key pressed event.
dispatch_event(&uio_event);
consumed = uio_event.reserved & 0x01;
consumed = uio_event.mask & MASK_CONSUMED;

// If the pressed event was not consumed...
if (!consumed) {
Expand All @@ -138,10 +135,11 @@ bool dispatch_key_press(uint64_t timestamp, CGEventRef event_ref) {
for (unsigned int i = 0; i < length; i++) {
// Populate key typed event.
uio_event.time = timestamp;
uio_event.reserved = 0x00;

uio_event.type = EVENT_KEY_TYPED;
uio_event.mask = get_modifiers();
if (CGEventGetIntegerValueField(event_ref, kCGEventSourceUnixProcessID)) {
uio_event.mask |= MASK_EMULATED;
}

uio_event.data.keyboard.keycode = VC_UNDEFINED;
uio_event.data.keyboard.rawcode = keycode;
Expand All @@ -154,7 +152,7 @@ bool dispatch_key_press(uint64_t timestamp, CGEventRef event_ref) {

// Populate key typed event.
dispatch_event(&uio_event);
consumed = uio_event.reserved & 0x01;
consumed = uio_event.mask & MASK_CONSUMED;
}
}

Expand All @@ -168,10 +166,11 @@ bool dispatch_key_release(uint64_t timestamp, CGEventRef event_ref) {

// Populate key released event.
uio_event.time = timestamp;
uio_event.reserved = 0x00;

uio_event.type = EVENT_KEY_RELEASED;
uio_event.mask = get_modifiers();
if (CGEventGetIntegerValueField(event_ref, kCGEventSourceUnixProcessID)) {
uio_event.mask |= MASK_EMULATED;
}

uio_event.data.keyboard.keycode = keycode_to_scancode(keycode);
uio_event.data.keyboard.rawcode = keycode;
Expand All @@ -183,7 +182,7 @@ bool dispatch_key_release(uint64_t timestamp, CGEventRef event_ref) {

// Fire key released event.
dispatch_event(&uio_event);
consumed = uio_event.reserved & 0x01;
consumed = uio_event.mask & MASK_CONSUMED;

return consumed;
}
Expand Down Expand Up @@ -466,10 +465,11 @@ bool dispatch_button_press(uint64_t timestamp, CGEventRef event_ref, uint16_t bu

// Populate mouse pressed event.
uio_event.time = timestamp;
uio_event.reserved = 0x00;

uio_event.type = EVENT_MOUSE_PRESSED;
uio_event.mask = get_modifiers();
if (CGEventGetIntegerValueField(event_ref, kCGEventSourceUnixProcessID)) {
uio_event.mask |= MASK_EMULATED;
}

uio_event.data.mouse.button = button;
uio_event.data.mouse.clicks = click_count;
Expand All @@ -483,7 +483,7 @@ bool dispatch_button_press(uint64_t timestamp, CGEventRef event_ref, uint16_t bu

// Fire mouse pressed event.
dispatch_event(&uio_event);
consumed = uio_event.reserved & 0x01;
consumed = uio_event.mask & MASK_CONSUMED;

return consumed;
}
Expand All @@ -495,10 +495,11 @@ bool dispatch_button_release(uint64_t timestamp, CGEventRef event_ref, uint16_t

// Populate mouse released event.
uio_event.time = timestamp;
uio_event.reserved = 0x00;

uio_event.type = EVENT_MOUSE_RELEASED;
uio_event.mask = get_modifiers();
if (CGEventGetIntegerValueField(event_ref, kCGEventSourceUnixProcessID)) {
uio_event.mask |= MASK_EMULATED;
}

uio_event.data.mouse.button = button;
uio_event.data.mouse.clicks = click_count;
Expand All @@ -512,16 +513,17 @@ bool dispatch_button_release(uint64_t timestamp, CGEventRef event_ref, uint16_t

// Fire mouse released event.
dispatch_event(&uio_event);
consumed = uio_event.reserved & 0x01;
consumed = uio_event.mask & MASK_CONSUMED;

// If the pressed event was not consumed...
if (uio_event.reserved ^ 0x01 && !is_mouse_dragged()) {
if (!consumed && !is_mouse_dragged()) {
// Populate mouse clicked event.
uio_event.time = timestamp;
uio_event.reserved = 0x00;

uio_event.type = EVENT_MOUSE_CLICKED;
uio_event.mask = get_modifiers();
if (CGEventGetIntegerValueField(event_ref, kCGEventSourceUnixProcessID)) {
uio_event.mask |= MASK_EMULATED;
}

uio_event.data.mouse.button = button;
uio_event.data.mouse.clicks = click_count;
Expand All @@ -535,7 +537,7 @@ bool dispatch_button_release(uint64_t timestamp, CGEventRef event_ref, uint16_t

// Fire mouse clicked event.
dispatch_event(&uio_event);
consumed = uio_event.reserved & 0x01; // TODO Should we track this flag for consumed events?
consumed = uio_event.mask & MASK_CONSUMED;
}

// Reset the number of clicks.
Expand All @@ -559,15 +561,16 @@ bool dispatch_mouse_move(uint64_t timestamp, CGEventRef event_ref) {

// Populate mouse motion event.
uio_event.time = timestamp;
uio_event.reserved = 0x00;

if (is_mouse_dragged()) {
uio_event.type = EVENT_MOUSE_DRAGGED;
}
else {
uio_event.type = EVENT_MOUSE_MOVED;
}
uio_event.mask = get_modifiers();
if (CGEventGetIntegerValueField(event_ref, kCGEventSourceUnixProcessID)) {
uio_event.mask |= MASK_EMULATED;
}

uio_event.data.mouse.button = MOUSE_NOBUTTON;
uio_event.data.mouse.clicks = click_count;
Expand All @@ -580,14 +583,14 @@ bool dispatch_mouse_move(uint64_t timestamp, CGEventRef event_ref) {

// Fire mouse motion event.
dispatch_event(&uio_event);
consumed = uio_event.reserved & 0x01;
consumed = uio_event.mask & MASK_CONSUMED;

return consumed;
}

bool dispatch_mouse_wheel(uint64_t timestamp, CGEventRef event_ref) {
bool consumed = false;

// Reset the click count and previous button.
click_count = 0;
click_button = MOUSE_NOBUTTON;
Expand All @@ -600,10 +603,11 @@ bool dispatch_mouse_wheel(uint64_t timestamp, CGEventRef event_ref) {

// Populate mouse wheel event.
uio_event.time = timestamp;
uio_event.reserved = 0x00;

uio_event.type = EVENT_MOUSE_WHEEL;
uio_event.mask = get_modifiers();
if (CGEventGetIntegerValueField(event_ref, kCGEventSourceUnixProcessID)) {
uio_event.mask |= MASK_EMULATED;
}

uio_event.data.wheel.x = event_point.x;
uio_event.data.wheel.y = event_point.y;
Expand Down Expand Up @@ -662,7 +666,7 @@ bool dispatch_mouse_wheel(uint64_t timestamp, CGEventRef event_ref) {

// Fire mouse wheel event.
dispatch_event(&uio_event);
consumed = uio_event.reserved & 0x01;
consumed = uio_event.mask & MASK_CONSUMED;
}

return consumed;
Expand Down
20 changes: 10 additions & 10 deletions src/windows/dispatch_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ bool dispatch_hook_enable(uint64_t timestamp) {

// Fire the hook start event.
dispatch_event(&uio_event);
consumed = uio_event.mask & MASK_CONSUME;
consumed = uio_event.mask & MASK_CONSUMED;

return consumed;
}
Expand All @@ -100,7 +100,7 @@ bool dispatch_hook_disable(uint64_t timestamp) {

// Fire the hook stop event.
dispatch_event(&uio_event);
consumed = uio_event.mask & MASK_CONSUME;
consumed = uio_event.mask & MASK_CONSUMED;

return consumed;
}
Expand Down Expand Up @@ -139,7 +139,7 @@ bool dispatch_key_press(uint64_t timestamp, KBDLLHOOKSTRUCT *kbhook) {

// Populate key pressed event.
dispatch_event(&uio_event);
consumed = uio_event.mask & MASK_CONSUME;
consumed = uio_event.mask & MASK_CONSUMED;

// If the pressed event was not consumed...
if (!consumed) {
Expand Down Expand Up @@ -167,7 +167,7 @@ bool dispatch_key_press(uint64_t timestamp, KBDLLHOOKSTRUCT *kbhook) {

// Fire key typed event.
dispatch_event(&uio_event);
consumed = uio_event.mask & MASK_CONSUME;
consumed = uio_event.mask & MASK_CONSUMED;
}
}

Expand Down Expand Up @@ -208,7 +208,7 @@ bool dispatch_key_release(uint64_t timestamp, KBDLLHOOKSTRUCT *kbhook) {

// Fire key released event.
dispatch_event(&uio_event);
consumed = uio_event.mask & MASK_CONSUME;
consumed = uio_event.mask & MASK_CONSUMED;

return consumed;
}
Expand Down Expand Up @@ -260,7 +260,7 @@ bool dispatch_button_press(uint64_t timestamp, MSLLHOOKSTRUCT *mshook, uint16_t

// Fire mouse pressed event.
dispatch_event(&uio_event);
consumed = uio_event.mask & MASK_CONSUME;
consumed = uio_event.mask & MASK_CONSUMED;

return consumed;
}
Expand Down Expand Up @@ -289,7 +289,7 @@ bool dispatch_button_release(uint64_t timestamp, MSLLHOOKSTRUCT *mshook, uint16_

// Fire mouse released event.
dispatch_event(&uio_event);
consumed = uio_event.mask & MASK_CONSUME;
consumed = uio_event.mask & MASK_CONSUMED;

// If the pressed event was not consumed...
if (!consumed && last_click.x == mshook->pt.x && last_click.y == mshook->pt.y) {
Expand All @@ -313,7 +313,7 @@ bool dispatch_button_release(uint64_t timestamp, MSLLHOOKSTRUCT *mshook, uint16_

// Fire mouse clicked event.
dispatch_event(&uio_event);
consumed = uio_event.mask & MASK_CONSUME;
consumed = uio_event.mask & MASK_CONSUMED;
}

// Reset the number of clicks.
Expand Down Expand Up @@ -366,7 +366,7 @@ bool dispatch_mouse_move(uint64_t timestamp, MSLLHOOKSTRUCT *mshook) {

// Fire mouse move event.
dispatch_event(&uio_event);
consumed = uio_event.mask & MASK_CONSUME;
consumed = uio_event.mask & MASK_CONSUMED;
}

return consumed;
Expand Down Expand Up @@ -432,7 +432,7 @@ bool dispatch_mouse_wheel(uint64_t timestamp, MSLLHOOKSTRUCT *mshook, uint8_t di

// Fire mouse wheel event.
dispatch_event(&uio_event);
consumed = uio_event.mask & MASK_CONSUME;
consumed = uio_event.mask & MASK_CONSUMED;
} else {
logger(LOG_LEVEL_WARN, "%s [%u]: SystemParametersInfo() failed, event will be consumed.\n",
__FUNCTION__, __LINE__);
Expand Down
Loading

0 comments on commit 362d944

Please sign in to comment.