Skip to content

Commit b780ff0

Browse files
author
Simon Hofmann
committedAug 3, 2020
(#17) window_manager MacOS implementation to retrieve list of window handles and window dimensions by handle
1 parent ba301b4 commit b780ff0

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
 

Diff for: ‎src/macos/window_manager.mm

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#import <Foundation/Foundation.h>
2+
#import <AppKit/AppKit.h>
3+
#import <ApplicationServices/ApplicationServices.h>
4+
#include "../window_manager.h"
5+
6+
std::vector<int32_t> getWindows() {
7+
CGWindowListOption listOptions = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements;
8+
CFArrayRef windowList = CGWindowListCopyWindowInfo(listOptions, kCGNullWindowID);
9+
10+
std::vector<int32_t> windowHandles;
11+
12+
for (NSDictionary *info in (NSArray *)windowList) {
13+
NSNumber *ownerPid = info[(id)kCGWindowOwnerPID];
14+
NSNumber *windowNumber = info[(id)kCGWindowNumber];
15+
16+
auto app = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]];
17+
auto path = app ? [app.bundleURL.path UTF8String] : "";
18+
19+
if (app && path != "") {
20+
windowHandles.push_back([windowNumber intValue]);
21+
}
22+
}
23+
24+
if (windowList) {
25+
CFRelease(windowList);
26+
}
27+
28+
return windowHandles;
29+
}
30+
31+
MMRect getWindowRect(const int32_t windowHandle) {
32+
CGWindowListOption listOptions = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements;
33+
CFArrayRef windowList = CGWindowListCopyWindowInfo(listOptions, kCGNullWindowID);
34+
35+
std::vector<std::string> windowNames;
36+
37+
for (NSDictionary *info in (NSArray *)windowList) {
38+
NSNumber *ownerPid = info[(id)kCGWindowOwnerPID];
39+
NSNumber *windowNumber = info[(id)kCGWindowNumber];
40+
41+
auto app = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]];
42+
43+
if (app && [windowNumber intValue] == windowHandle) {
44+
CGRect windowRect;
45+
if (CGRectMakeWithDictionaryRepresentation((CFDictionaryRef)info[(id)kCGWindowBounds], &windowRect)) {
46+
return MMRectMake(windowRect.origin.x, windowRect.origin.y, windowRect.size.height, windowRect.size.width);
47+
}
48+
return MMRectMake(0, 0, 0, 0);
49+
}
50+
}
51+
52+
if (windowList) {
53+
CFRelease(windowList);
54+
}
55+
56+
return MMRectMake(0, 0, 0, 0);
57+
}

0 commit comments

Comments
 (0)
Please sign in to comment.