mirror of
https://github.com/WarmUpTill/SceneSwitcher.git
synced 2026-03-21 17:34:57 -05:00
mac version
This commit is contained in:
parent
1d6bb4b0a0
commit
9ff9757009
107
advanced-scene-switcher-osx.mm
Normal file
107
advanced-scene-switcher-osx.mm
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
#import <AppKit/AppKit.h>
|
||||
#import <CoreFoundation/CoreFoundation.h>
|
||||
#import <CoreGraphics/CGEvent.h>
|
||||
#import <Carbon/Carbon.h>
|
||||
#include <Carbon/Carbon.h>
|
||||
#include <util/platform.h>
|
||||
#include "advanced-scene-switcher.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
void GetWindowList(vector<string> &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<int, int> getCursorPos() {
|
||||
pair<int, int> 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;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user