From 9ff975700987c6ec442cef69fe5fb06e2a891862 Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Mon, 7 Nov 2016 20:40:18 +0100 Subject: [PATCH] mac version --- advanced-scene-switcher-osx.mm | 107 +++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 advanced-scene-switcher-osx.mm diff --git a/advanced-scene-switcher-osx.mm b/advanced-scene-switcher-osx.mm new file mode 100644 index 00000000..2eb0a77b --- /dev/null +++ b/advanced-scene-switcher-osx.mm @@ -0,0 +1,107 @@ +#import +#import +#import +#import +#include +#include +#include "advanced-scene-switcher.hpp" + +using namespace std; + +void GetWindowList(vector &windows) +{ + windows.resize(0); + + @autoreleasepool { + NSWorkspace *ws = [NSWorkspace sharedWorkspace]; + NSArray *array = [ws runningApplications]; + for (NSRunningApplication *app in array) { + NSString *name = app.localizedName; + if (!name) + continue; + + const char *str = name.UTF8String; + if (str && *str) + windows.emplace_back(str); + } + } +} + +void GetCurrentWindowTitle(string &title) +{ + title.resize(0); + + @autoreleasepool { + NSWorkspace *ws = [NSWorkspace sharedWorkspace]; + NSRunningApplication *app = [ws frontmostApplication]; + if (app) { + NSString *name = app.localizedName; + if (!name) + return; + + const char *str = name.UTF8String; + if (str && *str) + title = str; + } + } +} + +pair getCursorPos() { + pair pos(0, 0); + CGEventRef event = CGEventCreate(NULL); + CGPoint cursorPos = CGEventGetLocation(event); + CFRelease(event); + pos.first = cursorPos.x; + pos.second = cursorPos.y; + return pos; + } + +//causing crash (see below) +bool isFullscreen() { + @autoreleasepool { + AXValueRef temp; + CGSize windowSize; + CGPoint windowPosition; + AXUIElementRef frontMostApp; + AXUIElementRef frontMostWindow; + + pid_t pid; + ProcessSerialNumber psn; + @try { + GetFrontProcess(&psn); + GetProcessPID(&psn, &pid); + frontMostApp = AXUIElementCreateApplication(pid); + + //frontMostApp = AXUIElementCreateApplication(app); + + AXUIElementCopyAttributeValue( + frontMostApp, kAXFocusedWindowAttribute, (CFTypeRef *)&frontMostWindow); + + // Get the window size and position + AXUIElementCopyAttributeValue( + frontMostWindow, kAXSizeAttribute, (CFTypeRef *)&temp); + AXValueGetValue(temp, kAXValueTypeCGSize, &windowSize); //<-------- crash here + CFRelease(temp); + + AXUIElementCopyAttributeValue( + frontMostWindow, kAXPositionAttribute, (CFTypeRef *)&temp); + AXValueGetValue(temp, kAXValueTypeCGPoint, &windowPosition); //<-------- crash here + CFRelease(temp); + + CGRect screenBound = CGDisplayBounds(CGMainDisplayID()); + CGSize screenSize = screenBound.size; + + if((windowSize.width == screenSize.width) && (windowSize.height == screenSize.height) && + (windowPosition.x == 0) && (windowPosition.y == 0)) + return true; + } + @catch (...) { + // deal with the exception + } + @catch (NSException *exception) { + // deal with the exception + } + + } + return false; +}