annotate dep/animia/src/win/quartz.mm @ 155:d2bbb5773616

dep/animia: add quartz backend for windows
author Paper <mrpapersonic@gmail.com>
date Wed, 15 Nov 2023 15:24:39 -0500
parents
children cdf79282d647
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
155
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
1 #include "animia/win/quartz.h"
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
2 #include "animia.h"
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
3
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
4 #include <Foundation/Foundation.h>
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
5 #include <CoreGraphics/CoreGraphics.h>
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
6 #include <AppKit/AppKit.h>
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8 namespace animia::internal::quartz {
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
9
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10 template<typename T>
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
11 static bool IntegerFromNSNumber(NSNumber* num, T& result) {
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
12 if (!num)
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
13 return false;
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
14 result = [num intValue];
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
15 return true;
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
16 }
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
17
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
18 static bool GetWindowTitle(unsigned int wid, std::string& result) {
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
19 NSWindow* window = [NSApp windowWithWindowNumber: wid];
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
20 if (!window)
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
21 return false;
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
22
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
23 NSString* title = [window title];
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
24 if (!title)
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
25 return false;
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
26
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
27 result = [title UTF8String];
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
28
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
29 return true;
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
30 }
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
31
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
32 static bool StringFromNSString(NSString* string, std::string& result) {
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
33 if (!string)
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
34 return false;
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
35 result = [string UTF8String];
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
36 return true;
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
37 }
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
38
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
39 bool QuartzWinTools::EnumerateWindows(window_proc_t window_proc) {
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40 if (!window_proc)
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
41 return false;
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
42
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43 NSMutableArray* windows = (NSMutableArray*)CGWindowListCopyWindowInfo(kCGWindowListOptionAll, kCGNullWindowID);
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
44 if (!windows)
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
45 return false;
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
47 for (NSDictionary* window in windows) {
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
48 if (!window)
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
49 continue;
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
50
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
51 Process proc;
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
52 {
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
53 IntegerFromNSNumber([window objectForKey:@"kCGWindowOwnerPID"], proc.pid);
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
54 StringFromNSString([window objectForKey:@"kCGWindowOwnerName"], proc.name);
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
55 }
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
56
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
57 Window win;
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
58 {
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
59 IntegerFromNSNumber([window objectForKey:@"kCGWindowNumber"], win.id);
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
60 StringFromNSString([window objectForKey:@"kCGWindowName"], win.class_name);
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
61 GetWindowTitle(win.id, win.text);
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
62 }
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
63
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
64 if (!window_proc(proc, win))
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
65 return false;
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
66 }
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
67
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
68 return true;
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
69 }
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
70
d2bbb5773616 dep/animia: add quartz backend for windows
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
71 } // namespace animia::win::detail