Skip to content

Commit

Permalink
Merge pull request #311 from Wizek/patch-2
Browse files Browse the repository at this point in the history
(Fixed version to 0.9.0) Support Multiple Apps Running with IOHook
  • Loading branch information
WilixLead authored Apr 6, 2021
2 parents 769daed + 98f258b commit 1c900e2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 32 deletions.
12 changes: 1 addition & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -93,16 +93,6 @@
"11.0.0",
"85"
],
[
"node",
"8.9.3",
"57"
],
[
"node",
"9.2.0",
"59"
],
[
"node",
"10.0.0",
Expand Down
41 changes: 20 additions & 21 deletions src/iohook.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ static std::queue<uiohook_event> 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;

Expand Down Expand Up @@ -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);
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1c900e2

Please sign in to comment.