Skip to content

Commit badec8a

Browse files
committed
(#91) Added start checks for requirements on macOS and Linux
1 parent 4452a8d commit badec8a

File tree

2 files changed

+128
-88
lines changed

2 files changed

+128
-88
lines changed

src/main.cc

+96-88
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "screen.h"
88
#include "screengrab.h"
99
#include "window_manager.h"
10+
#include "startup.h"
1011

1112
int mouseDelay = 10;
1213
int keyboardDelay = 10;
@@ -256,85 +257,85 @@ struct KeyNames
256257
};
257258

258259
static KeyNames key_names[] =
259-
{
260-
{"backspace", K_BACKSPACE},
261-
{"delete", K_DELETE},
262-
{"enter", K_RETURN},
263-
{"tab", K_TAB},
264-
{"escape", K_ESCAPE},
265-
{"up", K_UP},
266-
{"down", K_DOWN},
267-
{"right", K_RIGHT},
268-
{"left", K_LEFT},
269-
{"home", K_HOME},
270-
{"end", K_END},
271-
{"pageup", K_PAGEUP},
272-
{"pagedown", K_PAGEDOWN},
273-
{"f1", K_F1},
274-
{"f2", K_F2},
275-
{"f3", K_F3},
276-
{"f4", K_F4},
277-
{"f5", K_F5},
278-
{"f6", K_F6},
279-
{"f7", K_F7},
280-
{"f8", K_F8},
281-
{"f9", K_F9},
282-
{"f10", K_F10},
283-
{"f11", K_F11},
284-
{"f12", K_F12},
285-
{"f13", K_F13},
286-
{"f14", K_F14},
287-
{"f15", K_F15},
288-
{"f16", K_F16},
289-
{"f17", K_F17},
290-
{"f18", K_F18},
291-
{"f19", K_F19},
292-
{"f20", K_F20},
293-
{"f21", K_F21},
294-
{"f22", K_F22},
295-
{"f23", K_F23},
296-
{"f24", K_F24},
297-
{"command", K_META},
298-
{"alt", K_ALT},
299-
{"control", K_CONTROL},
300-
{"shift", K_SHIFT},
301-
{"right_shift", K_RIGHTSHIFT},
302-
{"space", K_SPACE},
303-
{"printscreen", K_PRINTSCREEN},
304-
{"insert", K_INSERT},
305-
{"menu", K_MENU},
306-
307-
{"audio_mute", K_AUDIO_VOLUME_MUTE},
308-
{"audio_vol_down", K_AUDIO_VOLUME_DOWN},
309-
{"audio_vol_up", K_AUDIO_VOLUME_UP},
310-
{"audio_play", K_AUDIO_PLAY},
311-
{"audio_stop", K_AUDIO_STOP},
312-
{"audio_pause", K_AUDIO_PAUSE},
313-
{"audio_prev", K_AUDIO_PREV},
314-
{"audio_next", K_AUDIO_NEXT},
315-
{"audio_rewind", K_AUDIO_REWIND},
316-
{"audio_forward", K_AUDIO_FORWARD},
317-
{"audio_repeat", K_AUDIO_REPEAT},
318-
{"audio_random", K_AUDIO_RANDOM},
319-
320-
{"numpad_0", K_NUMPAD_0},
321-
{"numpad_1", K_NUMPAD_1},
322-
{"numpad_2", K_NUMPAD_2},
323-
{"numpad_3", K_NUMPAD_3},
324-
{"numpad_4", K_NUMPAD_4},
325-
{"numpad_5", K_NUMPAD_5},
326-
{"numpad_6", K_NUMPAD_6},
327-
{"numpad_7", K_NUMPAD_7},
328-
{"numpad_8", K_NUMPAD_8},
329-
{"numpad_9", K_NUMPAD_9},
330-
331-
{"lights_mon_up", K_LIGHTS_MON_UP},
332-
{"lights_mon_down", K_LIGHTS_MON_DOWN},
333-
{"lights_kbd_toggle", K_LIGHTS_KBD_TOGGLE},
334-
{"lights_kbd_up", K_LIGHTS_KBD_UP},
335-
{"lights_kbd_down", K_LIGHTS_KBD_DOWN},
336-
337-
{NULL, K_NOT_A_KEY} /* end marker */
260+
{
261+
{"backspace", K_BACKSPACE},
262+
{"delete", K_DELETE},
263+
{"enter", K_RETURN},
264+
{"tab", K_TAB},
265+
{"escape", K_ESCAPE},
266+
{"up", K_UP},
267+
{"down", K_DOWN},
268+
{"right", K_RIGHT},
269+
{"left", K_LEFT},
270+
{"home", K_HOME},
271+
{"end", K_END},
272+
{"pageup", K_PAGEUP},
273+
{"pagedown", K_PAGEDOWN},
274+
{"f1", K_F1},
275+
{"f2", K_F2},
276+
{"f3", K_F3},
277+
{"f4", K_F4},
278+
{"f5", K_F5},
279+
{"f6", K_F6},
280+
{"f7", K_F7},
281+
{"f8", K_F8},
282+
{"f9", K_F9},
283+
{"f10", K_F10},
284+
{"f11", K_F11},
285+
{"f12", K_F12},
286+
{"f13", K_F13},
287+
{"f14", K_F14},
288+
{"f15", K_F15},
289+
{"f16", K_F16},
290+
{"f17", K_F17},
291+
{"f18", K_F18},
292+
{"f19", K_F19},
293+
{"f20", K_F20},
294+
{"f21", K_F21},
295+
{"f22", K_F22},
296+
{"f23", K_F23},
297+
{"f24", K_F24},
298+
{"command", K_META},
299+
{"alt", K_ALT},
300+
{"control", K_CONTROL},
301+
{"shift", K_SHIFT},
302+
{"right_shift", K_RIGHTSHIFT},
303+
{"space", K_SPACE},
304+
{"printscreen", K_PRINTSCREEN},
305+
{"insert", K_INSERT},
306+
{"menu", K_MENU},
307+
308+
{"audio_mute", K_AUDIO_VOLUME_MUTE},
309+
{"audio_vol_down", K_AUDIO_VOLUME_DOWN},
310+
{"audio_vol_up", K_AUDIO_VOLUME_UP},
311+
{"audio_play", K_AUDIO_PLAY},
312+
{"audio_stop", K_AUDIO_STOP},
313+
{"audio_pause", K_AUDIO_PAUSE},
314+
{"audio_prev", K_AUDIO_PREV},
315+
{"audio_next", K_AUDIO_NEXT},
316+
{"audio_rewind", K_AUDIO_REWIND},
317+
{"audio_forward", K_AUDIO_FORWARD},
318+
{"audio_repeat", K_AUDIO_REPEAT},
319+
{"audio_random", K_AUDIO_RANDOM},
320+
321+
{"numpad_0", K_NUMPAD_0},
322+
{"numpad_1", K_NUMPAD_1},
323+
{"numpad_2", K_NUMPAD_2},
324+
{"numpad_3", K_NUMPAD_3},
325+
{"numpad_4", K_NUMPAD_4},
326+
{"numpad_5", K_NUMPAD_5},
327+
{"numpad_6", K_NUMPAD_6},
328+
{"numpad_7", K_NUMPAD_7},
329+
{"numpad_8", K_NUMPAD_8},
330+
{"numpad_9", K_NUMPAD_9},
331+
332+
{"lights_mon_up", K_LIGHTS_MON_UP},
333+
{"lights_mon_down", K_LIGHTS_MON_DOWN},
334+
{"lights_kbd_toggle", K_LIGHTS_KBD_TOGGLE},
335+
{"lights_kbd_up", K_LIGHTS_KBD_UP},
336+
{"lights_kbd_down", K_LIGHTS_KBD_DOWN},
337+
338+
{NULL, K_NOT_A_KEY} /* end marker */
338339
};
339340

340341
int CheckKeyCodes(std::string &keyName, MMKeyCode *key)
@@ -659,29 +660,33 @@ Napi::Number _highlight(const Napi::CallbackInfo &info)
659660
return Napi::Number::New(env, 1);
660661
}
661662

662-
Napi::Number _getActiveWindow(const Napi::CallbackInfo &info) {
663+
Napi::Number _getActiveWindow(const Napi::CallbackInfo &info)
664+
{
663665
Napi::Env env = info.Env();
664666

665667
WindowHandle windowHandle = getActiveWindow();
666668
return Napi::Number::New(env, (double)windowHandle);
667669
}
668670

669-
Napi::Array _getWindows(const Napi::CallbackInfo &info) {
671+
Napi::Array _getWindows(const Napi::CallbackInfo &info)
672+
{
670673
Napi::Env env = info.Env();
671674

672675
std::vector<WindowHandle> windowHandles = getWindows();
673676
auto arr = Napi::Array::New(env, windowHandles.size());
674677

675-
for (size_t idx = 0; idx < windowHandles.size(); ++idx) {
678+
for (size_t idx = 0; idx < windowHandles.size(); ++idx)
679+
{
676680
arr[(uint32_t)idx] = windowHandles[idx];
677681
}
678682

679683
return arr;
680684
}
681685

682-
Napi::Object _getWindowRect(const Napi::CallbackInfo &info) {
686+
Napi::Object _getWindowRect(const Napi::CallbackInfo &info)
687+
{
683688
Napi::Env env = info.Env();
684-
689+
685690
WindowHandle windowHandle = info[0].As<Napi::Number>().Int64Value();
686691
MMRect windowRect = getWindowRect(windowHandle);
687692

@@ -694,7 +699,8 @@ Napi::Object _getWindowRect(const Napi::CallbackInfo &info) {
694699
return obj;
695700
}
696701

697-
Napi::String _getWindowTitle(const Napi::CallbackInfo &info) {
702+
Napi::String _getWindowTitle(const Napi::CallbackInfo &info)
703+
{
698704
Napi::Env env = info.Env();
699705

700706
WindowHandle windowHandle = info[0].As<Napi::Number>().Int64Value();
@@ -748,8 +754,9 @@ Napi::Object _captureScreen(const Napi::CallbackInfo &info)
748754
}
749755

750756
MMBitmapRef bitmap = copyMMBitmapFromDisplayInRect(MMRectMake(x, y, w, h));
751-
752-
if (bitmap == NULL) {
757+
758+
if (bitmap == NULL)
759+
{
753760
throw Napi::Error::New(env, "Error: Failed to capture screen");
754761
}
755762

@@ -769,7 +776,8 @@ Napi::Object _captureScreen(const Napi::CallbackInfo &info)
769776
return obj;
770777
}
771778

772-
Napi::Object Init(Napi::Env env, Napi::Object exports) {
779+
Napi::Object Init(Napi::Env env, Napi::Object exports)
780+
{
773781
exports.Set(Napi::String::New(env, "dragMouse"), Napi::Function::New(env, _dragMouse));
774782
exports.Set(Napi::String::New(env, "moveMouse"), Napi::Function::New(env, _moveMouse));
775783
exports.Set(Napi::String::New(env, "getMousePos"), Napi::Function::New(env, _getMousePos));

src/startup.h

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "os.h"
2+
3+
#if defined(IS_MACOSX)
4+
5+
#include <iostream>
6+
#include <ApplicationServices/ApplicationServices.h>
7+
8+
static void startupCheck(void) __attribute__((constructor));
9+
static void startupCheck(void)
10+
{
11+
bool isTrustedProcess = AXIsProcessTrusted();
12+
if (!isTrustedProcess) {
13+
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;
14+
}
15+
}
16+
17+
#elif defined(USE_X11)
18+
19+
#include "xdisplay.h"
20+
21+
Display *display = XGetMainDisplay();
22+
23+
int32_t majorOpcode;
24+
int32_t firstEvent;
25+
int32_t firstError;
26+
bool isXTestAvailable = XQueryExtension(display, "XTEST", &majorOpcode, &firstEvent, &firstEvent);
27+
28+
if (!isXTestAvailable) {
29+
std::cout << "##### WARNING! Your system is missing libXtst! Please visit https://github.com/nut-tree/nut.js#linux #####" << std::endl;
30+
}
31+
32+
#endif

0 commit comments

Comments
 (0)