-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathwindow_manager.mm
95 lines (75 loc) · 2.81 KB
/
window_manager.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <ApplicationServices/ApplicationServices.h>
#include "../window_manager.h"
NSDictionary* getWindowInfo(int64_t windowHandle) {
CGWindowListOption listOptions = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements;
CFArrayRef windowList = CGWindowListCopyWindowInfo(listOptions, kCGNullWindowID);
for (NSDictionary *info in (NSArray *)windowList) {
NSNumber *windowNumber = info[(id)kCGWindowNumber];
if (windowHandle == [windowNumber intValue]) {
CFRetain(info);
CFRelease(windowList);
return info;
}
}
if (windowList) {
CFRelease(windowList);
}
return nullptr;
}
WindowHandle getActiveWindow() {
CGWindowListOption listOptions = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements;
CFArrayRef windowList = CGWindowListCopyWindowInfo(listOptions, kCGNullWindowID);
for (NSDictionary *info in (NSArray *)windowList) {
NSNumber *ownerPid = info[(id)kCGWindowOwnerPID];
NSNumber *windowNumber = info[(id)kCGWindowNumber];
auto app = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]];
if (![app isActive]) {
continue;
}
CFRelease(windowList);
return [windowNumber intValue];
}
if (windowList) {
CFRelease(windowList);
}
return -1;
}
std::vector<WindowHandle> getWindows() {
CGWindowListOption listOptions = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements;
CFArrayRef windowList = CGWindowListCopyWindowInfo(listOptions, kCGNullWindowID);
std::vector<WindowHandle> windowHandles;
for (NSDictionary *info in (NSArray *)windowList) {
NSNumber *ownerPid = info[(id)kCGWindowOwnerPID];
NSNumber *windowNumber = info[(id)kCGWindowNumber];
auto app = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]];
auto path = app ? [app.bundleURL.path UTF8String] : "";
if (app && path != "") {
windowHandles.push_back([windowNumber intValue]);
}
}
if (windowList) {
CFRelease(windowList);
}
return windowHandles;
}
MMRect getWindowRect(const WindowHandle windowHandle) {
auto windowInfo = getWindowInfo(windowHandle);
if (windowInfo != nullptr) {
CGRect windowRect;
if (CGRectMakeWithDictionaryRepresentation((CFDictionaryRef)windowInfo[(id)kCGWindowBounds], &windowRect)) {
return MMRectMake(windowRect.origin.x, windowRect.origin.y, windowRect.size.width, windowRect.size.height);
}
return MMRectMake(0, 0, 0, 0);
}
return MMRectMake(0, 0, 0, 0);
}
std::string getWindowTitle(const WindowHandle windowHandle) {
auto windowInfo = getWindowInfo(windowHandle);
if (windowInfo != nullptr) {
NSString *windowName = windowInfo[(id)kCGWindowName];
return [windowName UTF8String];
}
return "";
}