Skip to content

Commit

Permalink
fix fullscreen applications quitting randomly
Browse files Browse the repository at this point in the history
  • Loading branch information
directive-unknown committed Jun 9, 2023
1 parent 8667726 commit 3d3aa66
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
8 changes: 8 additions & 0 deletions header/window.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
#pragma once
#include <CoreGraphics/CoreGraphics.h>

struct Bounds
{
int width;
int height;
};

struct Window
{
pid_t processId;
CGWindowID windowId;
Bounds display;
Bounds window;
int length;
};
44 changes: 43 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,45 @@ bool isWindowMinimized(const Window window)
return isMinimized;
}

void updateDisplayBounds(Window &window)
{
CFArrayRef windows = CGWindowListCopyWindowInfo(kCGWindowListOptionAll, kCGNullWindowID);

for (CFIndex i = 0; i < CFArrayGetCount(windows); i++)
{
CFDictionaryRef windowInfo = (CFDictionaryRef)CFArrayGetValueAtIndex(windows, i);
CFNumberRef ownerPIDRef = (CFNumberRef)CFDictionaryGetValue(windowInfo, kCGWindowOwnerPID);

pid_t ownerPID;
if (CFNumberGetValue(ownerPIDRef, kCFNumberSInt32Type, &ownerPID) && ownerPID == window.processId)
{
CGRect windowBounds;
CFDictionaryRef boundsRef = (CFDictionaryRef)CFDictionaryGetValue(windowInfo, kCGWindowBounds);
CGRectMakeWithDictionaryRepresentation(boundsRef, &windowBounds);

int monitorNumber = 0;
CGDirectDisplayID displayIDs[10];
uint32_t displayCount;
CGGetDisplaysWithPoint(windowBounds.origin, 10, displayIDs, &displayCount);

for (uint32_t j = 0; j < displayCount; j++)
{
if (CGDisplayIsActive(displayIDs[j]))
{
CGRect displayBounds = CGDisplayBounds(displayIDs[j]);
window.display.width = displayBounds.size.width;
window.display.height = displayBounds.size.height;
}
}
}
}
}

Hashset getAllWindows()
{
Hashset set;
const CFArrayRef windows = CGWindowListCopyWindowInfo(kCGWindowListOptionAll | kCGWindowListExcludeDesktopElements, kCGNullWindowID);

for (CFIndex i = 0; i < CFArrayGetCount(windows); ++i)
{
CFDictionaryRef windowInfo = (CFDictionaryRef)CFArrayGetValueAtIndex(windows, i);
Expand All @@ -84,11 +118,18 @@ Hashset getAllWindows()
pid_t wid;
CFNumberGetValue(widNumber, kCFNumberSInt32Type, &wid);

CGRect bounds;
CFDictionaryRef boundsRef = (CFDictionaryRef)CFDictionaryGetValue(windowInfo, kCGWindowBounds);
CGRectMakeWithDictionaryRepresentation(boundsRef, &bounds);

if (CFNumberGetValue(pidNumber, kCFNumberSInt32Type, &pid))
{
Window window;
window.processId = pid;
window.windowId = wid;
window.window.width = bounds.size.width;
window.window.height = bounds.size.height;
updateDisplayBounds(window);
set.add(window);
}
}
Expand Down Expand Up @@ -133,6 +174,7 @@ int main()
!isWindowOnScreen(windows[i]) &&
!isWindowMinimized(windows[i]) &&
!isBackgroundWindow(windows[i]) &&
(windows[i].window.width < windows[i].display.width && windows[i].window.height < windows[i].display.height) &&
windows[i].processId > 0 &&
windows[i].windowId > 0)
{
Expand Down

0 comments on commit 3d3aa66

Please sign in to comment.