annotate dep/animia/src/win/quartz.cc @ 189:649786bae914

*: etc. code cleanup I've removed most macros and stuff dep/animia: [UNTESTED] use raw C++ instead of Objective-C++
author Paper <mrpapersonic@gmail.com>
date Wed, 06 Dec 2023 19:42:33 -0500
parents
children 2d5823df870f
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 /* We actually DON'T need Objective-C for most of this file.
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
2 * GetWindowTitle() is the only function that really needs it.
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
3 * (and even then, we can use the C bindings for it...)
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
4 *
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
5 * However, being able to use the Foundation classes makes things
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
6 * so, so, so much easier, and so I've decided to make this file
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7 * in Objective-C++.
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8 */
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
9 #include "animia/win/quartz.h"
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10 #include "animia.h"
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
11
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
12 #include <objc/runtime.h>
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
13 #include <objc/message.h>
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
14
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
15 #include <CoreFoundation/CoreFoundation.h>
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
16 #include <CoreGraphics/CoreGraphics.h>
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
17 #include <Carbon/Carbon.h>
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
18
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
19 namespace animia::internal::quartz {
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
20
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
21 static bool GetWindowTitle(unsigned int wid, std::string& result) {
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
22 // id app = [NSApplication sharedApplication];
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
23 id app = cls_msg(cls("NSApplication"), sel("sharedApplication"));
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
24
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
25 // id window = [app windowWithWindowNumber: wid];
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
26 id window = msg(app, sel("windowWithWindowNumber:"), wid);
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
27
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
28 // return [[window title] UTF8String];
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
29 return StringFromCFString(reinterpret_cast<CFStringRef>(msg(window, "title")), result);
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
30 }
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
31
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
32 bool QuartzWinTools::EnumerateWindows(window_proc_t window_proc) {
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
33 if (!window_proc)
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
34 return false;
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
35
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
36 CFArrayRef windows = CGWindowListCopyWindowInfo(kCGWindowListOptionAll, kCGNullWindowID);
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
37 if (!windows)
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
38 return false;
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
39
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40 CFIndex i = 0; count = CFArrayGetCount(windows);
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
41 for (; i < count; i++) {
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
42 CFDictionaryRef window = CFArrayGetValueAtIndex(windows, i);
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43 if (!window)
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
44 continue;
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
45
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46 Process proc;
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
47 {
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
48 {
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
49 CFNumber num;
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
50 CFDictionaryGetValueIfPresent(window, "kCGWindowOwnerPID", &num);
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
51 osx::util::GetCFNumber(num, proc.pid);
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
52 }
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
53 {
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
54 CFStringRef str;
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
55 CFDictionaryGetValueIfPresent(window, "kCGWindowOwnerName", &str);
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
56 osx::util::StringFromCFString(str, proc.name);
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
57 }
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
58 if (proc.name.empty())
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
59 osx::util::GetProcessName(proc.pid, proc.name);
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
60 }
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
61
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
62 Window win;
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
63 {
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
64 {
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
65 CFNumber num;
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
66 CFDictionaryGetValueIfPresent(window, "kCGWindowNumber", &num);
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
67 osx::util::GetCFNumber(num, win.id);
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 CFStringRef str;
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
71 CFDictionaryGetValueIfPresent(window, "kCGWindowName", &str);
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
72 osx::util::GetCFNumber(str, win.class_name);
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
73 }
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
74 GetWindowTitle(win.id, win.text);
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
75 }
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
76
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
77 if (!window_proc(proc, win))
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
78 return false;
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
79 }
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
80
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
81 return true;
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
82 }
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
83
649786bae914 *: etc. code cleanup
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
84 } // namespace animia::win::detail