Skip to content

Commit

Permalink
First go update src for libuiohook 1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan Brown committed Mar 4, 2024
1 parent 85a0022 commit 70d9418
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 47 deletions.
2 changes: 2 additions & 0 deletions build_def/darwin/uiohook.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"libuiohook/include/uiohook.h",
"libuiohook/src/logger.c",
"libuiohook/src/logger.h",
"libuiohook/src/darwin/dispatch_event.h",
"libuiohook/src/darwin/dispatch_event.c",
"libuiohook/src/darwin/input_helper.h",
"libuiohook/src/darwin/input_helper.c",
"libuiohook/src/darwin/input_hook.c",
Expand Down
2 changes: 2 additions & 0 deletions build_def/linux/uiohook.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"libuiohook/include/uiohook.h",
"libuiohook/src/logger.c",
"libuiohook/src/logger.h",
"libuiohook/src/x11/dispatch_event.h",
"libuiohook/src/x11/dispatch_event.c",
"libuiohook/src/x11/input_helper.h",
"libuiohook/src/x11/input_helper.c",
"libuiohook/src/x11/input_hook.c",
Expand Down
6 changes: 5 additions & 1 deletion build_def/win32/uiohook.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
"libuiohook/include/uiohook.h",
"libuiohook/src/logger.c",
"libuiohook/src/logger.h",
"libuiohook/src/windows/dispatch_event.h",
"libuiohook/src/windows/dispatch_event.c",
"libuiohook/src/windows/input_helper.h",
"libuiohook/src/windows/input_helper.c",
"libuiohook/src/windows/input_hook.c",
"libuiohook/src/windows/post_event.c",
"libuiohook/src/windows/system_properties.c"
"libuiohook/src/windows/system_properties.c",
"libuiohook/src/windows/monitor_helper.h",
"libuiohook/src/windows/monitor_helper.c"
],
"include_dirs": [
'node_modules/nan',
Expand Down
87 changes: 41 additions & 46 deletions src/iohook.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,25 @@ static pthread_mutex_t hook_control_mutex;
static pthread_cond_t hook_control_cond;
#endif

bool logger_proc(unsigned int level, const char *format, ...) {
if (!sIsDebug) {
return false;
}
bool status = false;
va_list args;
switch (level) {
case LOG_LEVEL_DEBUG:
case LOG_LEVEL_INFO:
va_start(args, format);
status = vfprintf(stdout, format, args) >= 0;
va_end(args);
break;
static void logger_proc(unsigned int level, void *user_data, const char *format, va_list args) {
switch (level) {
case LOG_LEVEL_INFO:
vfprintf(stdout, format, args);
break;

case LOG_LEVEL_WARN:
case LOG_LEVEL_ERROR:
vfprintf(stderr, format, args);
break;
}
}

case LOG_LEVEL_WARN:
case LOG_LEVEL_ERROR:
va_start(args, format);
status = vfprintf(stderr, format, args) >= 0;
va_end(args);
break;
}
static void logger(unsigned int level, const char *format, ...) {
va_list args;

return status;
va_start(args, format);
logger_proc(level, NULL, format, args);
va_end(args);
}

// NOTE: The following callback executes on the same thread that hook_run() is called
Expand All @@ -70,7 +66,7 @@ bool logger_proc(unsigned int level, const char *format, ...) {
// Furthermore, some operating systems may choose to disable your hook if it
// takes to long to process. If you need to do any extended processing, please
// do so by copying the event to your own queued dispatch thread.
void dispatch_proc(uiohook_event * const event) {
void dispatch_proc(uiohook_event * const event, void *user_data) {
switch (event->type) {
case EVENT_HOOK_ENABLED:
// Lock the running mutex so we know if the hook is enabled.
Expand Down Expand Up @@ -196,7 +192,7 @@ int hook_enable() {
#if defined(_WIN32)
// Attempt to set the thread priority to time critical.
if (SetThreadPriority(hook_thread, THREAD_PRIORITY_TIME_CRITICAL) == 0) {
logger_proc(LOG_LEVEL_WARN, "%s [%u]: Could not set thread priority %li for thread %#p! (%#lX)\n",
logger(LOG_LEVEL_WARN, "%s [%u]: Could not set thread priority %li for thread %#p! (%#lX)\n",
__FUNCTION__, __LINE__, (long) THREAD_PRIORITY_TIME_CRITICAL,
hook_thread, (unsigned long) GetLastError());
}
Expand All @@ -205,13 +201,13 @@ int hook_enable() {
// use pthread_setschedparam instead.
struct sched_param param = { .sched_priority = priority };
if (pthread_setschedparam(hook_thread, SCHED_OTHER, &param) != 0) {
logger_proc(LOG_LEVEL_WARN, "%s [%u]: Could not set thread priority %i for thread 0x%lX!\n",
logger(LOG_LEVEL_WARN, "%s [%u]: Could not set thread priority %i for thread 0x%lX!\n",
__FUNCTION__, __LINE__, priority, (unsigned long) hook_thread);
}
#else
// Raise the thread priority using glibc pthread_setschedprio.
if (pthread_setschedprio(hook_thread, priority) != 0) {
logger_proc(LOG_LEVEL_WARN, "%s [%u]: Could not set thread priority %i for thread 0x%lX!\n",
logger(LOG_LEVEL_WARN, "%s [%u]: Could not set thread priority %i for thread 0x%lX!\n",
__FUNCTION__, __LINE__, priority, (unsigned long) hook_thread);
}
#endif
Expand Down Expand Up @@ -253,7 +249,7 @@ int hook_enable() {

free(hook_thread_status);

logger_proc(LOG_LEVEL_DEBUG, "%s [%u]: Thread Result: (%#X).\n",
logger(LOG_LEVEL_DEBUG, "%s [%u]: Thread Result: (%#X).\n",
__FUNCTION__, __LINE__, status);
}
else {
Expand Down Expand Up @@ -285,10 +281,10 @@ void run() {
#endif

// Set the logger callback for library output.
hook_set_logger_proc(&logger_proc);
hook_set_logger_proc(&logger_proc, NULL);

// Set the event callback for uiohook events.
hook_set_dispatch_proc(&dispatch_proc);
hook_set_dispatch_proc(&dispatch_proc, NULL);

// Start the hook and block.
// NOTE If EVENT_HOOK_ENABLED was delivered, the status will always succeed.
Expand All @@ -310,63 +306,63 @@ void run() {

// System level errors.
case UIOHOOK_ERROR_OUT_OF_MEMORY:
logger_proc(LOG_LEVEL_ERROR, "Failed to allocate memory. (%#X)\n", status);
logger(LOG_LEVEL_ERROR, "Failed to allocate memory. (%#X)\n", status);
break;


// X11 specific errors.
case UIOHOOK_ERROR_X_OPEN_DISPLAY:
logger_proc(LOG_LEVEL_ERROR, "Failed to open X11 display. (%#X)\n", status);
logger(LOG_LEVEL_ERROR, "Failed to open X11 display. (%#X)\n", status);
break;

case UIOHOOK_ERROR_X_RECORD_NOT_FOUND:
logger_proc(LOG_LEVEL_ERROR, "Unable to locate XRecord extension. (%#X)\n", status);
logger(LOG_LEVEL_ERROR, "Unable to locate XRecord extension. (%#X)\n", status);
break;

case UIOHOOK_ERROR_X_RECORD_ALLOC_RANGE:
logger_proc(LOG_LEVEL_ERROR, "Unable to allocate XRecord range. (%#X)\n", status);
logger(LOG_LEVEL_ERROR, "Unable to allocate XRecord range. (%#X)\n", status);
break;

case UIOHOOK_ERROR_X_RECORD_CREATE_CONTEXT:
logger_proc(LOG_LEVEL_ERROR, "Unable to allocate XRecord context. (%#X)\n", status);
logger(LOG_LEVEL_ERROR, "Unable to allocate XRecord context. (%#X)\n", status);
break;

case UIOHOOK_ERROR_X_RECORD_ENABLE_CONTEXT:
logger_proc(LOG_LEVEL_ERROR, "Failed to enable XRecord context. (%#X)\n", status);
logger(LOG_LEVEL_ERROR, "Failed to enable XRecord context. (%#X)\n", status);
break;


// Windows specific errors.
case UIOHOOK_ERROR_SET_WINDOWS_HOOK_EX:
logger_proc(LOG_LEVEL_ERROR, "Failed to register low level windows hook. (%#X)\n", status);
logger(LOG_LEVEL_ERROR, "Failed to register low level windows hook. (%#X)\n", status);
break;


// Darwin specific errors.
case UIOHOOK_ERROR_AXAPI_DISABLED:
logger_proc(LOG_LEVEL_ERROR, "Failed to enable access for assistive devices. (%#X)\n", status);
logger(LOG_LEVEL_ERROR, "Failed to enable access for assistive devices. (%#X)\n", status);
break;

case UIOHOOK_ERROR_CREATE_EVENT_PORT:
logger_proc(LOG_LEVEL_ERROR, "Failed to create apple event port. (%#X)\n", status);
logger(LOG_LEVEL_ERROR, "Failed to create apple event port. (%#X)\n", status);
break;

case UIOHOOK_ERROR_CREATE_RUN_LOOP_SOURCE:
logger_proc(LOG_LEVEL_ERROR, "Failed to create apple run loop source. (%#X)\n", status);
logger(LOG_LEVEL_ERROR, "Failed to create apple run loop source. (%#X)\n", status);
break;

case UIOHOOK_ERROR_GET_RUNLOOP:
logger_proc(LOG_LEVEL_ERROR, "Failed to acquire apple run loop. (%#X)\n", status);
logger(LOG_LEVEL_ERROR, "Failed to acquire apple run loop. (%#X)\n", status);
break;

case UIOHOOK_ERROR_CREATE_OBSERVER:
logger_proc(LOG_LEVEL_ERROR, "Failed to create apple run loop observer. (%#X)\n", status);
logger(LOG_LEVEL_ERROR, "Failed to create apple run loop observer. (%#X)\n", status);
break;

// Default error.
case UIOHOOK_FAILURE:
default:
logger_proc(LOG_LEVEL_ERROR, "An unknown hook error occurred. (%#X)\n", status);
logger(LOG_LEVEL_ERROR, "An unknown hook error occurred. (%#X)\n", status);
break;
}
}
Expand All @@ -376,18 +372,18 @@ void stop() {
switch (status) {
// System level errors.
case UIOHOOK_ERROR_OUT_OF_MEMORY:
logger_proc(LOG_LEVEL_ERROR, "Failed to allocate memory. (%#X)", status);
logger(LOG_LEVEL_ERROR, "Failed to allocate memory. (%#X)", status);
break;

case UIOHOOK_ERROR_X_RECORD_GET_CONTEXT:
// NOTE This is the only platform specific error that occurs on hook_stop().
logger_proc(LOG_LEVEL_ERROR, "Failed to get XRecord context. (%#X)", status);
logger(LOG_LEVEL_ERROR, "Failed to get XRecord context. (%#X)", status);
break;

// Default error.
case UIOHOOK_FAILURE:
default:
logger_proc(LOG_LEVEL_ERROR, "An unknown hook error occurred. (%#X)", status);
logger(LOG_LEVEL_ERROR, "An unknown hook error occurred. (%#X)", status);
break;
}

Expand Down Expand Up @@ -465,8 +461,7 @@ v8::Local<v8::Object> fillEventObject(uiohook_event event) {
obj->Set(v8::Isolate::GetCurrent()->GetCurrentContext(), Nan::New("mouse").ToLocalChecked(), mouse);
} else if (event.type == EVENT_MOUSE_WHEEL) {
v8::Local<v8::Object> wheel = Nan::New<v8::Object>();
wheel->Set(v8::Isolate::GetCurrent()->GetCurrentContext(), Nan::New("amount").ToLocalChecked(), Nan::New((uint16_t)event.data.wheel.amount));
wheel->Set(v8::Isolate::GetCurrent()->GetCurrentContext(), Nan::New("clicks").ToLocalChecked(), Nan::New((uint16_t)event.data.wheel.clicks));
wheel->Set(v8::Isolate::GetCurrent()->GetCurrentContext(), Nan::New("delta").ToLocalChecked(), Nan::New((uint16_t)event.data.wheel.delta));
wheel->Set(v8::Isolate::GetCurrent()->GetCurrentContext(), Nan::New("direction").ToLocalChecked(), Nan::New((int16_t)event.data.wheel.direction));
wheel->Set(v8::Isolate::GetCurrent()->GetCurrentContext(), Nan::New("rotation").ToLocalChecked(), Nan::New((int16_t)event.data.wheel.rotation));
wheel->Set(v8::Isolate::GetCurrent()->GetCurrentContext(), Nan::New("type").ToLocalChecked(), Nan::New((int16_t)event.data.wheel.type));
Expand Down

0 comments on commit 70d9418

Please sign in to comment.