Skip to content

Commit

Permalink
Fix event data lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
raub committed Apr 5, 2024
1 parent 3c18a14 commit f9ab27c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
25 changes: 25 additions & 0 deletions examples/log-keys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

import iohook from 'iohook-raub';

iohook.on('keypress', (msg) => {
console.log('keypress', msg);
});

iohook.on('keydown', (msg) => {
console.log('keydown', msg);
});

iohook.on('keyup', (msg) => {
console.log('keyup', msg);
});

iohook.start();
// iohook.setDebug(true); // Uncomment this line for see all debug information from iohook

console.log('Hook started.');

setInterval(() => {
console.log('Hook is working...');
}, 5000);

4 changes: 3 additions & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"private": true,
"type": "module",
"scripts": {
"start": "npx ts-node-esm ./main.ts"
"start": "npx ts-node-esm ./log-keys.ts",
"log-keys": "npx ts-node-esm ./log-keys.ts",
"tmp-disable": "npx ts-node-esm ./tmp-disable.ts"
},
"dependencies": {
"iohook-raub": "file:..",
Expand Down
File renamed without changes.
16 changes: 12 additions & 4 deletions src/cpp/hook-worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ bool logger_proc(unsigned int level, const char *format, ...) {
return status;
}

constexpr size_t MAX_STORED_COUNT = 256;
uiohook_event _storedEvents[MAX_STORED_COUNT];
size_t _storedCount = 0;

static inline uiohook_event *_keepEvent(const uiohook_event * const event) {
uiohook_event *ptr = &(_storedEvents[_storedCount]);
_storedCount = (_storedCount + 1) % MAX_STORED_COUNT;
memcpy(ptr, event, sizeof(uiohook_event));
return ptr;
}

// Executes on the same thread that hook_run() is called from.
void dispatch_proc(uiohook_event * const event) {
switch (event->type) {
Expand All @@ -48,10 +59,7 @@ void dispatch_proc(uiohook_event * const event) {
case EVENT_MOUSE_MOVED:
case EVENT_MOUSE_DRAGGED:
case EVENT_MOUSE_WHEEL:
uiohook_event event_copy;
memcpy(&event_copy, event, sizeof(uiohook_event));
// zqueue.push(event_copy);
callTsFn(&event_copy);
callTsFn(_keepEvent(event));
break;
default: break;
}
Expand Down

0 comments on commit f9ab27c

Please sign in to comment.