From badec8ae20718a144d9882ffd9d59b67aa3dcf26 Mon Sep 17 00:00:00 2001 From: Simon Hofmann Date: Wed, 22 Dec 2021 21:36:12 +0100 Subject: [PATCH 1/7] (#91) Added start checks for requirements on macOS and Linux --- src/main.cc | 184 ++++++++++++++++++++++++++------------------------ src/startup.h | 32 +++++++++ 2 files changed, 128 insertions(+), 88 deletions(-) create mode 100644 src/startup.h diff --git a/src/main.cc b/src/main.cc index 47562f9..93d04de 100644 --- a/src/main.cc +++ b/src/main.cc @@ -7,6 +7,7 @@ #include "screen.h" #include "screengrab.h" #include "window_manager.h" +#include "startup.h" int mouseDelay = 10; int keyboardDelay = 10; @@ -256,85 +257,85 @@ struct KeyNames }; static KeyNames key_names[] = - { - {"backspace", K_BACKSPACE}, - {"delete", K_DELETE}, - {"enter", K_RETURN}, - {"tab", K_TAB}, - {"escape", K_ESCAPE}, - {"up", K_UP}, - {"down", K_DOWN}, - {"right", K_RIGHT}, - {"left", K_LEFT}, - {"home", K_HOME}, - {"end", K_END}, - {"pageup", K_PAGEUP}, - {"pagedown", K_PAGEDOWN}, - {"f1", K_F1}, - {"f2", K_F2}, - {"f3", K_F3}, - {"f4", K_F4}, - {"f5", K_F5}, - {"f6", K_F6}, - {"f7", K_F7}, - {"f8", K_F8}, - {"f9", K_F9}, - {"f10", K_F10}, - {"f11", K_F11}, - {"f12", K_F12}, - {"f13", K_F13}, - {"f14", K_F14}, - {"f15", K_F15}, - {"f16", K_F16}, - {"f17", K_F17}, - {"f18", K_F18}, - {"f19", K_F19}, - {"f20", K_F20}, - {"f21", K_F21}, - {"f22", K_F22}, - {"f23", K_F23}, - {"f24", K_F24}, - {"command", K_META}, - {"alt", K_ALT}, - {"control", K_CONTROL}, - {"shift", K_SHIFT}, - {"right_shift", K_RIGHTSHIFT}, - {"space", K_SPACE}, - {"printscreen", K_PRINTSCREEN}, - {"insert", K_INSERT}, - {"menu", K_MENU}, - - {"audio_mute", K_AUDIO_VOLUME_MUTE}, - {"audio_vol_down", K_AUDIO_VOLUME_DOWN}, - {"audio_vol_up", K_AUDIO_VOLUME_UP}, - {"audio_play", K_AUDIO_PLAY}, - {"audio_stop", K_AUDIO_STOP}, - {"audio_pause", K_AUDIO_PAUSE}, - {"audio_prev", K_AUDIO_PREV}, - {"audio_next", K_AUDIO_NEXT}, - {"audio_rewind", K_AUDIO_REWIND}, - {"audio_forward", K_AUDIO_FORWARD}, - {"audio_repeat", K_AUDIO_REPEAT}, - {"audio_random", K_AUDIO_RANDOM}, - - {"numpad_0", K_NUMPAD_0}, - {"numpad_1", K_NUMPAD_1}, - {"numpad_2", K_NUMPAD_2}, - {"numpad_3", K_NUMPAD_3}, - {"numpad_4", K_NUMPAD_4}, - {"numpad_5", K_NUMPAD_5}, - {"numpad_6", K_NUMPAD_6}, - {"numpad_7", K_NUMPAD_7}, - {"numpad_8", K_NUMPAD_8}, - {"numpad_9", K_NUMPAD_9}, - - {"lights_mon_up", K_LIGHTS_MON_UP}, - {"lights_mon_down", K_LIGHTS_MON_DOWN}, - {"lights_kbd_toggle", K_LIGHTS_KBD_TOGGLE}, - {"lights_kbd_up", K_LIGHTS_KBD_UP}, - {"lights_kbd_down", K_LIGHTS_KBD_DOWN}, - - {NULL, K_NOT_A_KEY} /* end marker */ + { + {"backspace", K_BACKSPACE}, + {"delete", K_DELETE}, + {"enter", K_RETURN}, + {"tab", K_TAB}, + {"escape", K_ESCAPE}, + {"up", K_UP}, + {"down", K_DOWN}, + {"right", K_RIGHT}, + {"left", K_LEFT}, + {"home", K_HOME}, + {"end", K_END}, + {"pageup", K_PAGEUP}, + {"pagedown", K_PAGEDOWN}, + {"f1", K_F1}, + {"f2", K_F2}, + {"f3", K_F3}, + {"f4", K_F4}, + {"f5", K_F5}, + {"f6", K_F6}, + {"f7", K_F7}, + {"f8", K_F8}, + {"f9", K_F9}, + {"f10", K_F10}, + {"f11", K_F11}, + {"f12", K_F12}, + {"f13", K_F13}, + {"f14", K_F14}, + {"f15", K_F15}, + {"f16", K_F16}, + {"f17", K_F17}, + {"f18", K_F18}, + {"f19", K_F19}, + {"f20", K_F20}, + {"f21", K_F21}, + {"f22", K_F22}, + {"f23", K_F23}, + {"f24", K_F24}, + {"command", K_META}, + {"alt", K_ALT}, + {"control", K_CONTROL}, + {"shift", K_SHIFT}, + {"right_shift", K_RIGHTSHIFT}, + {"space", K_SPACE}, + {"printscreen", K_PRINTSCREEN}, + {"insert", K_INSERT}, + {"menu", K_MENU}, + + {"audio_mute", K_AUDIO_VOLUME_MUTE}, + {"audio_vol_down", K_AUDIO_VOLUME_DOWN}, + {"audio_vol_up", K_AUDIO_VOLUME_UP}, + {"audio_play", K_AUDIO_PLAY}, + {"audio_stop", K_AUDIO_STOP}, + {"audio_pause", K_AUDIO_PAUSE}, + {"audio_prev", K_AUDIO_PREV}, + {"audio_next", K_AUDIO_NEXT}, + {"audio_rewind", K_AUDIO_REWIND}, + {"audio_forward", K_AUDIO_FORWARD}, + {"audio_repeat", K_AUDIO_REPEAT}, + {"audio_random", K_AUDIO_RANDOM}, + + {"numpad_0", K_NUMPAD_0}, + {"numpad_1", K_NUMPAD_1}, + {"numpad_2", K_NUMPAD_2}, + {"numpad_3", K_NUMPAD_3}, + {"numpad_4", K_NUMPAD_4}, + {"numpad_5", K_NUMPAD_5}, + {"numpad_6", K_NUMPAD_6}, + {"numpad_7", K_NUMPAD_7}, + {"numpad_8", K_NUMPAD_8}, + {"numpad_9", K_NUMPAD_9}, + + {"lights_mon_up", K_LIGHTS_MON_UP}, + {"lights_mon_down", K_LIGHTS_MON_DOWN}, + {"lights_kbd_toggle", K_LIGHTS_KBD_TOGGLE}, + {"lights_kbd_up", K_LIGHTS_KBD_UP}, + {"lights_kbd_down", K_LIGHTS_KBD_DOWN}, + + {NULL, K_NOT_A_KEY} /* end marker */ }; int CheckKeyCodes(std::string &keyName, MMKeyCode *key) @@ -659,29 +660,33 @@ Napi::Number _highlight(const Napi::CallbackInfo &info) return Napi::Number::New(env, 1); } -Napi::Number _getActiveWindow(const Napi::CallbackInfo &info) { +Napi::Number _getActiveWindow(const Napi::CallbackInfo &info) +{ Napi::Env env = info.Env(); WindowHandle windowHandle = getActiveWindow(); return Napi::Number::New(env, (double)windowHandle); } -Napi::Array _getWindows(const Napi::CallbackInfo &info) { +Napi::Array _getWindows(const Napi::CallbackInfo &info) +{ Napi::Env env = info.Env(); std::vector windowHandles = getWindows(); auto arr = Napi::Array::New(env, windowHandles.size()); - for (size_t idx = 0; idx < windowHandles.size(); ++idx) { + for (size_t idx = 0; idx < windowHandles.size(); ++idx) + { arr[(uint32_t)idx] = windowHandles[idx]; } return arr; } -Napi::Object _getWindowRect(const Napi::CallbackInfo &info) { +Napi::Object _getWindowRect(const Napi::CallbackInfo &info) +{ Napi::Env env = info.Env(); - + WindowHandle windowHandle = info[0].As().Int64Value(); MMRect windowRect = getWindowRect(windowHandle); @@ -694,7 +699,8 @@ Napi::Object _getWindowRect(const Napi::CallbackInfo &info) { return obj; } -Napi::String _getWindowTitle(const Napi::CallbackInfo &info) { +Napi::String _getWindowTitle(const Napi::CallbackInfo &info) +{ Napi::Env env = info.Env(); WindowHandle windowHandle = info[0].As().Int64Value(); @@ -748,8 +754,9 @@ Napi::Object _captureScreen(const Napi::CallbackInfo &info) } MMBitmapRef bitmap = copyMMBitmapFromDisplayInRect(MMRectMake(x, y, w, h)); - - if (bitmap == NULL) { + + if (bitmap == NULL) + { throw Napi::Error::New(env, "Error: Failed to capture screen"); } @@ -769,7 +776,8 @@ Napi::Object _captureScreen(const Napi::CallbackInfo &info) return obj; } -Napi::Object Init(Napi::Env env, Napi::Object exports) { +Napi::Object Init(Napi::Env env, Napi::Object exports) +{ exports.Set(Napi::String::New(env, "dragMouse"), Napi::Function::New(env, _dragMouse)); exports.Set(Napi::String::New(env, "moveMouse"), Napi::Function::New(env, _moveMouse)); exports.Set(Napi::String::New(env, "getMousePos"), Napi::Function::New(env, _getMousePos)); diff --git a/src/startup.h b/src/startup.h new file mode 100644 index 0000000..f9ea4a6 --- /dev/null +++ b/src/startup.h @@ -0,0 +1,32 @@ +#include "os.h" + +#if defined(IS_MACOSX) + +#include +#include + +static void startupCheck(void) __attribute__((constructor)); +static void startupCheck(void) +{ + bool isTrustedProcess = AXIsProcessTrusted(); + if (!isTrustedProcess) { + std::cout << "##### WARNING! The application running this script is not a trusted process! Please visit https://github.com/nut-tree/nut.js#macos #####" << std::endl; + } +} + +#elif defined(USE_X11) + + #include "xdisplay.h" + + Display *display = XGetMainDisplay(); + + int32_t majorOpcode; + int32_t firstEvent; + int32_t firstError; + bool isXTestAvailable = XQueryExtension(display, "XTEST", &majorOpcode, &firstEvent, &firstEvent); + + if (!isXTestAvailable) { + std::cout << "##### WARNING! Your system is missing libXtst! Please visit https://github.com/nut-tree/nut.js#linux #####" << std::endl; + } + +#endif \ No newline at end of file From fff98849dd39d86d1abbebf269b4cc799fa26978 Mon Sep 17 00:00:00 2001 From: Simon Hofmann Date: Wed, 22 Dec 2021 21:45:20 +0100 Subject: [PATCH 2/7] (#91) Forgot to wrap the Linux startup code in a proper function --- src/startup.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/startup.h b/src/startup.h index f9ea4a6..723f905 100644 --- a/src/startup.h +++ b/src/startup.h @@ -18,6 +18,9 @@ static void startupCheck(void) #include "xdisplay.h" +static void startupCheck(void) __attribute__((constructor)); +static void startupCheck(void) +{ Display *display = XGetMainDisplay(); int32_t majorOpcode; @@ -28,5 +31,6 @@ static void startupCheck(void) if (!isXTestAvailable) { std::cout << "##### WARNING! Your system is missing libXtst! Please visit https://github.com/nut-tree/nut.js#linux #####" << std::endl; } +} #endif \ No newline at end of file From 1936725fdd822e3894222f9b23fb42238fac3a37 Mon Sep 17 00:00:00 2001 From: Simon Hofmann Date: Wed, 22 Dec 2021 21:46:08 +0100 Subject: [PATCH 3/7] (#91) Fixed unused variable --- src/startup.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/startup.h b/src/startup.h index 723f905..380aa58 100644 --- a/src/startup.h +++ b/src/startup.h @@ -26,7 +26,7 @@ static void startupCheck(void) int32_t majorOpcode; int32_t firstEvent; int32_t firstError; - bool isXTestAvailable = XQueryExtension(display, "XTEST", &majorOpcode, &firstEvent, &firstEvent); + bool isXTestAvailable = XQueryExtension(display, "XTEST", &majorOpcode, &firstEvent, &firstError); if (!isXTestAvailable) { std::cout << "##### WARNING! Your system is missing libXtst! Please visit https://github.com/nut-tree/nut.js#linux #####" << std::endl; From 348ed15410cdbc36e9adf4faf208760a21dffeaa Mon Sep 17 00:00:00 2001 From: Simon Hofmann Date: Wed, 22 Dec 2021 21:47:29 +0100 Subject: [PATCH 4/7] (#91) Fixed include --- src/startup.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/startup.h b/src/startup.h index 380aa58..933e652 100644 --- a/src/startup.h +++ b/src/startup.h @@ -1,8 +1,8 @@ #include "os.h" +#include #if defined(IS_MACOSX) -#include #include static void startupCheck(void) __attribute__((constructor)); From 9cc21f6287efa77a1077fe36b96cf993eced4386 Mon Sep 17 00:00:00 2001 From: Simon Hofmann Date: Fri, 31 Dec 2021 12:33:12 +0100 Subject: [PATCH 5/7] (#91) Use XOpenDisplay directly for startup checks --- src/startup.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/startup.h b/src/startup.h index 933e652..02a05ec 100644 --- a/src/startup.h +++ b/src/startup.h @@ -21,7 +21,7 @@ static void startupCheck(void) static void startupCheck(void) __attribute__((constructor)); static void startupCheck(void) { - Display *display = XGetMainDisplay(); + Display *display = XOpenDisplay(); int32_t majorOpcode; int32_t firstEvent; From 535c3ed644d1f466a779654f0bb8efdefc9b63e8 Mon Sep 17 00:00:00 2001 From: Simon Hofmann Date: Fri, 31 Dec 2021 12:34:59 +0100 Subject: [PATCH 6/7] (#91) Added missing parameter to XOpenDisplay --- src/startup.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/startup.h b/src/startup.h index 02a05ec..5508aed 100644 --- a/src/startup.h +++ b/src/startup.h @@ -21,7 +21,7 @@ static void startupCheck(void) static void startupCheck(void) __attribute__((constructor)); static void startupCheck(void) { - Display *display = XOpenDisplay(); + Display *display = XOpenDisplay(NULL); int32_t majorOpcode; int32_t firstEvent; From 7efb57f5909d8bb0d2be9696d3f0880ab24fc5f6 Mon Sep 17 00:00:00 2001 From: Simon Hofmann Date: Fri, 31 Dec 2021 12:46:40 +0100 Subject: [PATCH 7/7] (#91) Adjusted test constant to new execution environment on macOS where the default menu bar height increased to 25 --- test/window-integration-tests/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/window-integration-tests/test.js b/test/window-integration-tests/test.js index 4a8cb76..aae1a00 100644 --- a/test/window-integration-tests/test.js +++ b/test/window-integration-tests/test.js @@ -66,7 +66,7 @@ describe("getActiveWindow", () => { it("should determine correct coordinates for our application after moving the window", async () => { // GIVEN const xPosition = 42; - const yPosition = 23; + const yPosition = 25; await app.browserWindow.setPosition(xPosition, yPosition); await sleep(1000);