diff --git a/package.json b/package.json index ff00baa9..cb154a46 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "iohook", - "version": "0.7.2", + "version": "0.9.0", "description": "Node.js global keyboard and mouse hook", "main": "index.js", "types": "index.d.ts", @@ -93,16 +93,6 @@ "11.0.0", "85" ], - [ - "node", - "8.9.3", - "57" - ], - [ - "node", - "9.2.0", - "59" - ], [ "node", "10.0.0", diff --git a/src/iohook.cc b/src/iohook.cc index d518d3ef..300ee253 100644 --- a/src/iohook.cc +++ b/src/iohook.cc @@ -28,9 +28,9 @@ static std::queue zqueue; #ifdef _WIN32 static HANDLE hook_thread; -static HANDLE hook_running_mutex; -static HANDLE hook_control_mutex; -static HANDLE hook_control_cond; +static CRITICAL_SECTION hook_running_mutex; +static CRITICAL_SECTION hook_control_mutex; +static CONDITION_VARIABLE hook_control_cond; #else static pthread_t hook_thread; @@ -75,15 +75,15 @@ void dispatch_proc(uiohook_event * const event) { case EVENT_HOOK_ENABLED: // Lock the running mutex so we know if the hook is enabled. #ifdef _WIN32 - WaitForSingleObject(hook_running_mutex, INFINITE); + EnterCriticalSection(&hook_running_mutex); #else pthread_mutex_lock(&hook_running_mutex); #endif - + // Unlock the control mutex so hook_enable() can continue. #ifdef _WIN32 - // Signal the control event. - SetEvent(hook_control_cond); + WakeConditionVariable(&hook_control_cond); + LeaveCriticalSection(&hook_control_mutex); #else // Unlock the control mutex so hook_enable() can continue. pthread_cond_signal(&hook_control_cond); @@ -94,15 +94,14 @@ void dispatch_proc(uiohook_event * const event) { case EVENT_HOOK_DISABLED: // Lock the control mutex until we exit. #ifdef _WIN32 - WaitForSingleObject(hook_control_mutex, INFINITE); + EnterCriticalSection(&hook_control_mutex); #else pthread_mutex_lock(&hook_control_mutex); #endif // Unlock the running mutex so we know if the hook is disabled. #ifdef _WIN32 - ReleaseMutex(hook_running_mutex); - ResetEvent(hook_control_cond); + LeaveCriticalSection(&hook_running_mutex); #else #if defined(__APPLE__) && defined(__MACH__) // Stop the main runloop so that this program ends. @@ -148,7 +147,8 @@ void *hook_thread_proc(void *arg) { // Make sure we signal that we have passed any exception throwing code for // the waiting hook_enable(). #ifdef _WIN32 - SetEvent(hook_control_cond); + WakeConditionVariable(&hook_control_cond); + LeaveCriticalSection(&hook_control_mutex); return status; #else @@ -165,7 +165,7 @@ int hook_enable() { // Lock the thread control mutex. This will be unlocked when the // thread has finished starting, or when it has fully stopped. #ifdef _WIN32 - WaitForSingleObject(hook_control_mutex, INFINITE); + EnterCriticalSection(&hook_control_mutex); #else pthread_mutex_lock(&hook_control_mutex); #endif @@ -222,13 +222,13 @@ int hook_enable() { // event is received or the thread terminates. // NOTE This unlocks the hook_control_mutex while we wait. #ifdef _WIN32 - WaitForSingleObject(hook_control_cond, INFINITE); + SleepConditionVariableCS(&hook_control_cond, &hook_control_mutex, INFINITE); #else pthread_cond_wait(&hook_control_cond, &hook_control_mutex); #endif #ifdef _WIN32 - if (WaitForSingleObject(hook_running_mutex, 0) != WAIT_TIMEOUT) { + if (TryEnterCriticalSection(&hook_running_mutex) != FALSE) { #else if (pthread_mutex_trylock(&hook_running_mutex) == 0) { #endif @@ -262,7 +262,7 @@ int hook_enable() { // Make sure the control mutex is unlocked. #ifdef _WIN32 - ReleaseMutex(hook_control_mutex); + LeaveCriticalSection(&hook_control_mutex); #else pthread_mutex_unlock(&hook_control_mutex); #endif @@ -275,9 +275,9 @@ void run() { // thread has finished starting, or when it has fully stopped. #ifdef _WIN32 // Create event handles for the thread hook. - hook_running_mutex = CreateMutex(NULL, FALSE, TEXT("hook_running_mutex")); - hook_control_mutex = CreateMutex(NULL, FALSE, TEXT("hook_control_mutex")); - hook_control_cond = CreateEvent(NULL, TRUE, FALSE, TEXT("hook_control_cond")); + InitializeCriticalSection(&hook_running_mutex); + InitializeCriticalSection(&hook_control_mutex); + InitializeConditionVariable(&hook_control_cond); #else pthread_mutex_init(&hook_running_mutex, NULL); pthread_mutex_init(&hook_control_mutex, NULL); @@ -394,9 +394,8 @@ void stop() { #ifdef _WIN32 // Create event handles for the thread hook. CloseHandle(hook_thread); - CloseHandle(hook_running_mutex); - CloseHandle(hook_control_mutex); - CloseHandle(hook_control_cond); + DeleteCriticalSection(&hook_running_mutex); + DeleteCriticalSection(&hook_control_mutex); #else pthread_mutex_destroy(&hook_running_mutex); pthread_mutex_destroy(&hook_control_mutex);