Mercurial > minori
changeset 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 |
files | dep/animia/include/animia/util/osx.h dep/animia/src/animia.cc dep/animia/src/util/osx.cc dep/animia/src/win.cc dep/animia/src/win/quartz.cc src/gui/window.cc |
diffstat | 6 files changed, 96 insertions(+), 73 deletions(-) [+] |
line wrap: on
line diff
--- a/dep/animia/include/animia/util/osx.h Wed Dec 06 19:42:33 2023 -0500 +++ b/dep/animia/include/animia/util/osx.h Wed Dec 06 21:26:13 2023 -0500 @@ -3,6 +3,7 @@ #include "animia/types.h" #include <string> +#include <cstdint> #ifdef HAVE_COREFOUNDATION #include <CoreFoundation/CoreFoundation.h> @@ -12,25 +13,16 @@ #ifdef HAVE_COREFOUNDATION -/* I don't want to have to call CFRelease */ template<typename T> -struct CFDeleter { - using pointer = T; - void operator()(pointer p) { CFRelease(p); } -} - -template<typename T> -typedef CFReference = std::unique_ptr<T, CFDeleter>; - -template<typename T> -bool GetCFNumber(const CFNumber& num, T& result) { +bool GetCFNumber(CFNumberRef num, T& result) { if (!num) return false; - CFNumberType type = CFNumberGetType(num); - if (!CFNumberGetValue(num, type, result)) - return false; + int64_t res; + if (CFNumberGetValue(num, static_cast<CFNumberType>(4), &res)) + return true; + result = static_cast<T>(res); return true; }
--- a/dep/animia/src/animia.cc Wed Dec 06 19:42:33 2023 -0500 +++ b/dep/animia/src/animia.cc Wed Dec 06 21:26:13 2023 -0500 @@ -9,6 +9,8 @@ #include <string> #include <vector> +#include <iostream> + namespace animia { namespace internal { @@ -70,6 +72,7 @@ We should set the PID of the process if we can get it, but that'll be for when I can actually be arsed to implement the X11 backend. */ auto window_proc = [&](const Process& process, const Window& window) -> bool { + std::cout << process.name << std::endl; for (const auto& player : players) { if (!internal::PlayerHasStrategy(player, Strategy::WindowTitle)) continue;
--- a/dep/animia/src/util/osx.cc Wed Dec 06 19:42:33 2023 -0500 +++ b/dep/animia/src/util/osx.cc Wed Dec 06 21:26:13 2023 -0500 @@ -3,6 +3,9 @@ #include <string> #include <memory> +#include <sys/sysctl.h> +#include <libproc.h> + namespace animia::internal::osx::util { #ifdef HAVE_COREFOUNDATION @@ -54,26 +57,37 @@ if (!GetLaunchServicesPrivateSymbols()) return false; - CFReference<CFTypeRef> asn = LSASNCreateWithPid(kCFAllocatorDefault, pid); - - CFReference<CFArrayRef> request_array = CFArrayCreate(NULL, (const void **)kLSDisplayNameKey, 1, NULL); - - CFReference<CFDictionaryRef> dictionary = LSCopyApplicationInformation(kLaunchServicesMagicConstant, asn, request_array.get()); - if (!dictionary.get()) + CFTypeRef asn = LSASNCreateWithPid(kCFAllocatorDefault, pid); + if (!asn) return false; - CFReference<CFStringRef> str; + CFArrayRef request_array = CFArrayCreate(NULL, (const void **)kLSDisplayNameKey, 1, NULL); + if (!request_array) { + CFRelease(asn); + return false; + } + + CFDictionaryRef dictionary = LSCopyApplicationInformation(kLSDefaultSessionID, asn, request_array); + + CFRelease(request_array); + CFRelease(asn); + + if (!dictionary) + return false; + { CFStringRef rstr; + if (!CFDictionaryGetValueIfPresent(dictionary, kLSDisplayNameKey, (CFTypeRef*)&rstr) || !rstr) return false; - str.reset(rstr); - } - result.resize(CFStringGetMaximumSizeForEncoding(CFStringGetLength(str.get()), kCFStringEncodingUTF8) + 1); + if (!StringFromCFString(rstr, result)) { + CFRelease(rstr); + return false; + } - if (!CFStringGetCString(str.get(), &result.front(), result.length(), result.length())) - return false; + CFRelease(rstr); + } result.resize(result.find('\0')); @@ -84,8 +98,8 @@ if (!string) return false; - result.resize(CFStringGetMaximumSizeForEncoding(CFStringGetLength(str.get()), kCFStringEncodingUTF8) + 1); - if (!CFStringGetCString(str.get(), &result.front(), result.length(), result.length())) + result.resize(CFStringGetMaximumSizeForEncoding(CFStringGetLength(string), kCFStringEncodingUTF8) + 1); + if (!CFStringGetCString(string, &result.front(), result.length(), result.length())) return false; return true; @@ -124,6 +138,7 @@ return false; args.resize(size); + return true; } static bool GetProcessNameFromArgs(pid_t pid, std::string& result) { @@ -132,24 +147,24 @@ /* Get argc using memcpy */ int argc; - memcpy(&argc, &args.front(), sizeof(argc)); + memcpy(&result, &result.front(), sizeof(argc)); /* Do we even have argv[0]? */ if (argc < 1) return false; /* Find the first null character */ - size_t null_pos = args.find('\0', sizeof(argc)); + size_t null_pos = result.find('\0', sizeof(argc)); if (null_pos == std::string::npos) return false; /* Find the last slash */ - size_t last_slash = args.rfind('/', null_pos); + size_t last_slash = result.rfind('/', null_pos); if (last_slash == std::string::npos) return false; /* Return our result */ - result = args.substr(last_slash + 1, null_pos - last_slash - 1); + result = result.substr(last_slash + 1, null_pos - last_slash - 1); return true; } @@ -164,7 +179,7 @@ return true; } -static bool GetProcessName(pid_t pid, std::string& result) { +bool GetProcessName(pid_t pid, std::string& result) { #ifdef HAVE_COREFOUNDATION if (LaunchServicesGetProcessName(pid, result)) return true;
--- a/dep/animia/src/win.cc Wed Dec 06 19:42:33 2023 -0500 +++ b/dep/animia/src/win.cc Wed Dec 06 21:26:13 2023 -0500 @@ -2,9 +2,9 @@ #ifdef WIN32 # include "animia/win/win32.h" -#elif MACOSX +#elif defined(MACOSX) # include "animia/win/quartz.h" -#elif X11 +#elif defined(X11) # include "animia/win/x11.h" #endif @@ -12,9 +12,9 @@ #ifdef WIN32 win32::Win32WinTools os_win; -#elif MACOSX +#elif defined(MACOSX) quartz::QuartzWinTools os_win; -#elif X11 +#elif defined(X11) x11::X11WinTools os_win; #else BaseWinTools os_win;
--- a/dep/animia/src/win/quartz.cc Wed Dec 06 19:42:33 2023 -0500 +++ b/dep/animia/src/win/quartz.cc Wed Dec 06 21:26:13 2023 -0500 @@ -1,12 +1,5 @@ -/* We actually DON'T need Objective-C for most of this file. - * GetWindowTitle() is the only function that really needs it. - * (and even then, we can use the C bindings for it...) - * - * However, being able to use the Foundation classes makes things - * so, so, so much easier, and so I've decided to make this file - * in Objective-C++. -*/ #include "animia/win/quartz.h" +#include "animia/util/osx.h" #include "animia.h" #include <objc/runtime.h> @@ -14,46 +7,61 @@ #include <CoreFoundation/CoreFoundation.h> #include <CoreGraphics/CoreGraphics.h> -#include <Carbon/Carbon.h> + +#include <iostream> namespace animia::internal::quartz { +typedef id (*object_message_send)(id, SEL, ...); +typedef id (*class_message_send)(Class, SEL, ...); + +static const object_message_send obj_send = reinterpret_cast<object_message_send>(objc_msgSend); +static const class_message_send cls_send = reinterpret_cast<class_message_send>(objc_msgSend); + static bool GetWindowTitle(unsigned int wid, std::string& result) { - // id app = [NSApplication sharedApplication]; - id app = cls_msg(cls("NSApplication"), sel("sharedApplication")); + // NSApplication* app = [NSApplication sharedApplication]; + id app = cls_send(objc_getClass("NSApplication"), sel_getUid("sharedApplication")); - // id window = [app windowWithWindowNumber: wid]; - id window = msg(app, sel("windowWithWindowNumber:"), wid); + // NSWindow* window = [app windowWithWindowNumber: wid]; + id window = obj_send(app, sel_getUid("windowWithWindowNumber:"), wid); + if (!window) + return false; - // return [[window title] UTF8String]; - return StringFromCFString(reinterpret_cast<CFStringRef>(msg(window, "title")), result); + // NSString* title = [window title]; + // does this have to be freed? + CFStringRef title = reinterpret_cast<CFStringRef>(obj_send(window, sel_getUid("title"))); + if (!title) + return false; + + // return [title UTF8String]; + return osx::util::StringFromCFString(title, result); } bool QuartzWinTools::EnumerateWindows(window_proc_t window_proc) { if (!window_proc) return false; - CFArrayRef windows = CGWindowListCopyWindowInfo(kCGWindowListOptionAll, kCGNullWindowID); + const CFArrayRef windows = CGWindowListCopyWindowInfo(kCGWindowListOptionAll, kCGNullWindowID); if (!windows) return false; - CFIndex i = 0; count = CFArrayGetCount(windows); - for (; i < count; i++) { - CFDictionaryRef window = CFArrayGetValueAtIndex(windows, i); + const CFIndex count = CFArrayGetCount(windows); + for (CFIndex i = 0; i < count; i++) { + CFDictionaryRef window = reinterpret_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(windows, i)); if (!window) continue; Process proc; { { - CFNumber num; - CFDictionaryGetValueIfPresent(window, "kCGWindowOwnerPID", &num); - osx::util::GetCFNumber(num, proc.pid); + CFNumberRef num = nullptr; + if (CFDictionaryGetValueIfPresent(window, CFSTR("kCGWindowOwnerPID"), reinterpret_cast<const void**>(&num)) && num) + osx::util::GetCFNumber(num, proc.pid); } { - CFStringRef str; - CFDictionaryGetValueIfPresent(window, "kCGWindowOwnerName", &str); - osx::util::StringFromCFString(str, proc.name); + CFStringRef str = nullptr; + if (CFDictionaryGetValueIfPresent(window, CFSTR("kCGWindowOwnerName"), reinterpret_cast<const void**>(&str)) && str) + osx::util::StringFromCFString(str, proc.name); } if (proc.name.empty()) osx::util::GetProcessName(proc.pid, proc.name); @@ -62,22 +70,26 @@ Window win; { { - CFNumber num; - CFDictionaryGetValueIfPresent(window, "kCGWindowNumber", &num); - osx::util::GetCFNumber(num, win.id); + CFNumberRef num = nullptr; + if (CFDictionaryGetValueIfPresent(window, CFSTR("kCGWindowNumber"), reinterpret_cast<const void**>(&num)) && num) + osx::util::GetCFNumber(num, win.id); } { - CFStringRef str; - CFDictionaryGetValueIfPresent(window, "kCGWindowName", &str); - osx::util::GetCFNumber(str, win.class_name); + CFStringRef str = nullptr; + if (CFDictionaryGetValueIfPresent(window, CFSTR("kCGWindowName"), reinterpret_cast<const void**>(&str)) && str) + osx::util::StringFromCFString(str, win.class_name); } GetWindowTitle(win.id, win.text); } - if (!window_proc(proc, win)) + if (!window_proc(proc, win)) { + CFRelease(windows); return false; + } } + CFRelease(windows); + return true; }
--- a/src/gui/window.cc Wed Dec 06 19:42:33 2023 -0500 +++ b/src/gui/window.cc Wed Dec 06 21:26:13 2023 -0500 @@ -36,6 +36,8 @@ #include <QToolBar> #include <QToolButton> +#include <iostream> + #ifdef MACOSX # include "sys/osx/dark_theme.h" #elif defined(WIN32) @@ -64,7 +66,7 @@ setWindowIcon(QIcon(":/favicon.png")); main_widget.reset(new QWidget(this)); - /*QHBoxLayout* layout = */new QHBoxLayout(main_widget.get()); + new QHBoxLayout(main_widget.get()); AddMainWidgets(); @@ -78,12 +80,12 @@ thread.reset(new PlayingThread(this)); - QTimer* timer = new QTimer; + QTimer* timer = new QTimer(this); connect(timer, &QTimer::timeout, this, [this, page] { if (!thread.get() || thread->isRunning()) return; - connect(thread.get(), &QThread::finished, thread.get(), &QThread::deleteLater); + connect(thread.get(), &PlayingThread::Done, this, [page](const std::vector<std::string>& files) { for (const auto& file : files) { anitomy::Anitomy anitomy; @@ -103,7 +105,6 @@ }); timer->start(5000); - timer->moveToThread(thread.get()); } void MainWindow::AddMainWidgets() {