Repository navigation
Expand file tree
/
Copy pathmac.mm
More file actions
68 lines (53 loc) · 2.05 KB
/
Copy pathmac.mm
File metadata and controls
68 lines (53 loc) · 2.05 KB
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
/*
Mac/Cocoa specific code for obtaining information about the frontmost window
*/
#include "mac.h"
#include "Cocoa/Cocoa.h"
Mac::Mac()
{
}
QList<WindowInfo> Mac::getActiveWindows()
{
QList<WindowInfo> windowTitles;
// get frontmost process for currently active application
ProcessSerialNumber psn = { 0L, 0L };
OSStatus err = GetFrontProcess(&psn);
CFStringRef processName = NULL;
err = CopyProcessName(&psn, &processName);
NSString *pname = (NSString *)processName;
// loop through all application windows
CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
for (NSMutableDictionary* entry in (NSArray*)windowList)
{
NSString* ownerName = [entry objectForKey:(id)kCGWindowOwnerName];
NSString *name = [entry objectForKey:@"kCGWindowName" ];
NSInteger ownerPID = [[entry objectForKey:(id)kCGWindowOwnerPID] integerValue];
NSInteger layer = [[entry objectForKey:@"kCGWindowLayer"] integerValue];
if(layer == 0)
{
if([ownerName isEqualToString:pname])
{
NSRange range;
range.location = 0;
range.length = [ownerName length];
unichar *chars = new unichar[range.length];
[ownerName getCharacters:chars range:range];
QString owner = QString::fromUtf16(chars, range.length);
range.length = [name length];
chars = new unichar[range.length];
[name getCharacters:chars range:range];
QString windowTitle = QString::fromUtf16(chars, range.length);
delete[] chars;
long pid = (long)ownerPID;
WindowInfo wi;
wi.setProcessName(owner);
wi.setWindowTitle(windowTitle);
wi.setPID(pid);
windowTitles.append(wi);
}
}
}
CFRelease(windowList);
CFRelease(processName);
return windowTitles;
}