annotate dep/animia/src/win/quartz.cc @ 190:2d5823df870f

dep/animia: finalize de-objc-ifying quartz this also fixes up some... rather dumb mistakes in window.cc :) HG Enter commit message. Lines beginning with 'HG:' are removed.
author Paper <mrpapersonic@gmail.com>
date Wed, 06 Dec 2023 21:26:13 -0500
parents 649786bae914
children 0fc126d52de4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
189
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
1 #include "animia/win/quartz.h"
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
2 #include "animia/util/osx.h"
189
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
3 #include "animia.h"
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
4
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
5 #include <objc/runtime.h>
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
6 #include <objc/message.h>
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8 #include <CoreFoundation/CoreFoundation.h>
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
9 #include <CoreGraphics/CoreGraphics.h>
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
10
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
11 #include <iostream>
189
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
12
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
13 namespace animia::internal::quartz {
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
14
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
15 typedef id (*object_message_send)(id, SEL, ...);
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
16 typedef id (*class_message_send)(Class, SEL, ...);
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
17
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
18 static const object_message_send obj_send = reinterpret_cast<object_message_send>(objc_msgSend);
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
19 static const class_message_send cls_send = reinterpret_cast<class_message_send>(objc_msgSend);
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
20
189
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
21 static bool GetWindowTitle(unsigned int wid, std::string& result) {
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
22 // NSApplication* app = [NSApplication sharedApplication];
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
23 id app = cls_send(objc_getClass("NSApplication"), sel_getUid("sharedApplication"));
189
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
24
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
25 // NSWindow* window = [app windowWithWindowNumber: wid];
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
26 id window = obj_send(app, sel_getUid("windowWithWindowNumber:"), wid);
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
27 if (!window)
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
28 return false;
189
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
29
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
30 // NSString* title = [window title];
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
31 // does this have to be freed?
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
32 CFStringRef title = reinterpret_cast<CFStringRef>(obj_send(window, sel_getUid("title")));
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
33 if (!title)
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
34 return false;
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
35
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
36 // return [title UTF8String];
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
37 return osx::util::StringFromCFString(title, result);
189
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
38 }
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
39
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40 bool QuartzWinTools::EnumerateWindows(window_proc_t window_proc) {
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
41 if (!window_proc)
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
42 return false;
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
44 const CFArrayRef windows = CGWindowListCopyWindowInfo(kCGWindowListOptionAll, kCGNullWindowID);
189
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
45 if (!windows)
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46 return false;
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
47
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
48 const CFIndex count = CFArrayGetCount(windows);
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
49 for (CFIndex i = 0; i < count; i++) {
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
50 CFDictionaryRef window = reinterpret_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(windows, i));
189
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
51 if (!window)
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
52 continue;
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
53
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
54 Process proc;
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
55 {
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
56 {
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
57 CFNumberRef num = nullptr;
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
58 if (CFDictionaryGetValueIfPresent(window, CFSTR("kCGWindowOwnerPID"), reinterpret_cast<const void**>(&num)) && num)
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
59 osx::util::GetCFNumber(num, proc.pid);
189
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
60 }
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
61 {
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
62 CFStringRef str = nullptr;
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
63 if (CFDictionaryGetValueIfPresent(window, CFSTR("kCGWindowOwnerName"), reinterpret_cast<const void**>(&str)) && str)
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
64 osx::util::StringFromCFString(str, proc.name);
189
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
65 }
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
66 if (proc.name.empty())
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
67 osx::util::GetProcessName(proc.pid, proc.name);
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
68 }
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
69
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
70 Window win;
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
71 {
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
72 {
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
73 CFNumberRef num = nullptr;
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
74 if (CFDictionaryGetValueIfPresent(window, CFSTR("kCGWindowNumber"), reinterpret_cast<const void**>(&num)) && num)
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
75 osx::util::GetCFNumber(num, win.id);
189
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
76 }
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
77 {
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
78 CFStringRef str = nullptr;
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
79 if (CFDictionaryGetValueIfPresent(window, CFSTR("kCGWindowName"), reinterpret_cast<const void**>(&str)) && str)
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
80 osx::util::StringFromCFString(str, win.class_name);
189
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
81 }
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
82 GetWindowTitle(win.id, win.text);
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
83 }
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
84
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
85 if (!window_proc(proc, win)) {
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
86 CFRelease(windows);
189
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
87 return false;
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
88 }
189
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
89 }
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
90
190
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
91 CFRelease(windows);
2d5823df870f dep/animia: finalize de-objc-ifying quartz
Paper <mrpapersonic@gmail.com>
parents: 189
diff changeset
92
189
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
93 return true;
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
94 }
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
95
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
96 } // namespace animia::win::detail