Skip to content

Commit 5e3022d

Browse files
authored
Bugfix/377/window title segfault (#134)
* (#377) Moved inline functions from header to source file * (#377) Avoid segfaulting on nil values when fetching window title * (#377) Add checks and requests for both accessibility and screen sharing (for window titles) * (#377) Enable arm64 debug builds * (377) Release references after usage * (#377) Removed unguarded, macOS specific include since it is not required
1 parent db126b9 commit 5e3022d

File tree

5 files changed

+81
-56
lines changed

5 files changed

+81
-56
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"scripts": {
88
"clean": "cmake-js clean",
99
"patch": "node ./patch-packagename.js",
10-
"build:debug": "cmake-js rebuild --debug",
10+
"build:debug": "cmake-js rebuild --CDCMAKE_OSX_ARCHITECTURES=\"arm64;x86_64\" --debug",
1111
"build:release": "cmake-js rebuild --CDCMAKE_OSX_ARCHITECTURES=\"arm64;x86_64\"",
1212
"prepublishOnly": "npm run build:release",
1313
"publish:next": "npm publish --tag next"

src/macos/mouse_utils.h

+5-50
Original file line numberDiff line numberDiff line change
@@ -23,61 +23,16 @@ typedef struct {
2323

2424
double GetDoubleClickTime();
2525

26-
inline bool areSamePoint(MMPoint one, MMPoint other) {
27-
return one.x == other.x && one.y == other.y;
28-
}
26+
bool areSamePoint(MMPoint one, MMPoint other);
2927

30-
inline void recordClickTime(ClickTimer *timer, bool down,
28+
void recordClickTime(ClickTimer *timer, bool down,
3129
MMMouseButton button,
32-
MMPoint currentPoint) {
33-
clock_t milli = clock();
34-
if (button == LEFT_BUTTON) {
35-
if (down) {
36-
timer->left.down = milli;
37-
} else {
38-
timer->left.up = milli;
39-
}
40-
} else if (button == CENTER_BUTTON) {
41-
if (down) {
42-
timer->middle.down = milli;
43-
} else {
44-
timer->middle.up = milli;
45-
}
46-
} else if (button == RIGHT_BUTTON) {
47-
if (down) {
48-
timer->right.down = milli;
49-
} else {
50-
timer->right.up = milli;
51-
}
52-
}
53-
timer->clickLocation = currentPoint;
54-
}
30+
MMPoint currentPoint);
5531

56-
inline clock_t timeSinceLastClick(ClickTimer *timer, bool down, MMMouseButton button, clock_t currentTime) {
57-
if (button == LEFT_BUTTON) {
58-
if (down) {
59-
return currentTime - timer->left.down;
60-
} else {
61-
return currentTime - timer->left.up;
62-
}
63-
} else if (button == CENTER_BUTTON) {
64-
if (down) {
65-
return currentTime - timer->middle.down;
66-
} else {
67-
return currentTime - timer->middle.up;
68-
}
69-
} else if (button == RIGHT_BUTTON) {
70-
if (down) {
71-
return currentTime - timer->right.down;
72-
} else {
73-
return currentTime - timer->right.up;
74-
}
75-
}
76-
return 0;
77-
}
32+
clock_t timeSinceLastClick(ClickTimer *timer, bool down, MMMouseButton button, clock_t currentTime);
7833

7934
#ifdef __cplusplus
8035
}
8136
#endif
8237

83-
#endif
38+
#endif

src/macos/mouse_utils.mm

+54-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,57 @@
44
double GetDoubleClickTime() {
55
double doubleClickInterval = [NSEvent doubleClickInterval];
66
return doubleClickInterval * 1000;
7-
}
7+
}
8+
9+
bool areSamePoint(MMPoint one, MMPoint other) {
10+
return one.x == other.x && one.y == other.y;
11+
}
12+
13+
void recordClickTime(ClickTimer *timer, bool down,
14+
MMMouseButton button,
15+
MMPoint currentPoint) {
16+
clock_t milli = clock();
17+
if (button == LEFT_BUTTON) {
18+
if (down) {
19+
timer->left.down = milli;
20+
} else {
21+
timer->left.up = milli;
22+
}
23+
} else if (button == CENTER_BUTTON) {
24+
if (down) {
25+
timer->middle.down = milli;
26+
} else {
27+
timer->middle.up = milli;
28+
}
29+
} else if (button == RIGHT_BUTTON) {
30+
if (down) {
31+
timer->right.down = milli;
32+
} else {
33+
timer->right.up = milli;
34+
}
35+
}
36+
timer->clickLocation = currentPoint;
37+
}
38+
39+
clock_t timeSinceLastClick(ClickTimer *timer, bool down, MMMouseButton button, clock_t currentTime) {
40+
if (button == LEFT_BUTTON) {
41+
if (down) {
42+
return currentTime - timer->left.down;
43+
} else {
44+
return currentTime - timer->left.up;
45+
}
46+
} else if (button == CENTER_BUTTON) {
47+
if (down) {
48+
return currentTime - timer->middle.down;
49+
} else {
50+
return currentTime - timer->middle.up;
51+
}
52+
} else if (button == RIGHT_BUTTON) {
53+
if (down) {
54+
return currentTime - timer->right.down;
55+
} else {
56+
return currentTime - timer->right.up;
57+
}
58+
}
59+
return 0;
60+
}

src/macos/window_manager.mm

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
#include <CoreGraphics/CGWindow.h>
12
#import <Foundation/Foundation.h>
23
#import <AppKit/AppKit.h>
3-
#import <ApplicationServices/ApplicationServices.h>
44
#include "../window_manager.h"
55

66
NSDictionary* getWindowInfo(int64_t windowHandle) {
@@ -88,7 +88,7 @@ MMRect getWindowRect(const WindowHandle windowHandle) {
8888
auto windowInfo = getWindowInfo(windowHandle);
8989
if (windowInfo != nullptr && windowHandle >= 0) {
9090
NSString *windowName = windowInfo[(id)kCGWindowName];
91-
return [windowName UTF8String];
91+
return std::string([windowName UTF8String], [windowName lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
9292
}
9393
return "";
9494
}

src/startup.h

+19-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,27 @@
88
static void startupCheck(void) __attribute__((constructor));
99
static void startupCheck(void)
1010
{
11-
bool isTrustedProcess = AXIsProcessTrusted();
11+
int showDialog = 1;
12+
CFNumberRef showDialogNumber = CFNumberCreate(0, kCFNumberSInt32Type, &showDialog);
13+
const void *keys[1] = { kAXTrustedCheckOptionPrompt };
14+
const void *values[1] = { showDialogNumber };
15+
16+
CFDictionaryRef opts = CFDictionaryCreate(NULL, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
17+
18+
bool isTrustedProcess = AXIsProcessTrustedWithOptions(opts);
1219
if (!isTrustedProcess) {
1320
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;
1421
}
22+
23+
bool hasScreenCaptureAccess = CGPreflightScreenCaptureAccess();
24+
if (!hasScreenCaptureAccess) {
25+
CGRequestScreenCaptureAccess();
26+
std::cout << "##### WARNING! The application running this script is not allowed to capture screen content! Please visit https://github.com/nut-tree/nut.js#macos #####" << std::endl;
27+
28+
}
29+
30+
CFRelease(showDialogNumber);
31+
CFRelease(opts);
1532
}
1633

17-
#endif
34+
#endif

0 commit comments

Comments
 (0)