-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
190 lines (165 loc) · 6.21 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <iostream>
#include <unistd.h>
#include <sys/sysctl.h>
#include <ApplicationServices/ApplicationServices.h>
#include "header/hashset.h"
const int m_delay = 1;
Hashset m_windows;
bool isWindowOnScreen(const Window window)
{
const CFArrayRef windows = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
for (CFIndex i = 0; i < CFArrayGetCount(windows); i++)
{
const CFDictionaryRef windowInfo = (CFDictionaryRef)CFArrayGetValueAtIndex(windows, i);
const CFNumberRef pidNumber = (CFNumberRef)CFDictionaryGetValue(windowInfo, kCGWindowOwnerPID);
pid_t pid;
CFNumberGetValue(pidNumber, kCFNumberSInt32Type, &pid);
if (pid == window.processId)
{
CFRelease(windows);
return true;
}
}
CFRelease(windows);
return false;
}
bool isWindowValid(const Window window)
{
const CFArrayRef windows = CGWindowListCopyWindowInfo(kCGWindowListOptionAll, kCGNullWindowID);
for (CFIndex i = 0; i < CFArrayGetCount(windows); i++)
{
const CFDictionaryRef windowInfo = (CFDictionaryRef)CFArrayGetValueAtIndex(windows, i);
const CFNumberRef pidNumber = (CFNumberRef)CFDictionaryGetValue(windowInfo, kCGWindowOwnerPID);
pid_t pid;
CFNumberGetValue(pidNumber, kCFNumberSInt32Type, &pid);
const CFNumberRef widNumber = (CFNumberRef)CFDictionaryGetValue(windowInfo, kCGWindowNumber);
pid_t wid;
CFNumberGetValue(widNumber, kCFNumberSInt32Type, &wid);
if (pid == window.processId && wid == window.windowId)
{
CFRelease(windows);
return true;
}
}
CFRelease(windows);
return false;
}
bool isWindowMinimized(const Window window)
{
const AXUIElementRef appRef = AXUIElementCreateApplication(window.processId);
CFTypeRef minimizedValue;
bool isMinimized = false;
if (AXUIElementCopyAttributeValue(appRef, kAXWindowsAttribute, &minimizedValue) == kAXErrorSuccess)
{
const CFIndex windowCount = CFArrayGetCount(static_cast<CFArrayRef>(minimizedValue));
isMinimized = windowCount > 0;
CFRelease(minimizedValue);
}
CFRelease(appRef);
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;
}
}
}
}
CFRelease(windows);
}
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);
CFNumberRef pidNumber = (CFNumberRef)CFDictionaryGetValue(windowInfo, kCGWindowOwnerPID);
pid_t pid;
CFNumberRef widNumber = (CFNumberRef)CFDictionaryGetValue(windowInfo, kCGWindowNumber);
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);
}
}
CFRelease(windows);
return set;
}
bool isBackgroundWindow(const Window window)
{
ProcessSerialNumber psn = {0, kNoProcess};
while (GetNextProcess(&psn) == noErr)
{
pid_t processPID;
if (GetProcessPID(&psn, &processPID) == noErr && processPID == window.processId)
{
ProcessInfoRec processInfo;
memset(&processInfo, 0, sizeof(ProcessInfoRec));
processInfo.processInfoLength = sizeof(ProcessInfoRec);
if (GetProcessInformation(&psn, &processInfo) == noErr)
{
if (processInfo.processMode & modeOnlyBackground)
{
return true;
}
}
}
}
return false;
}
int main()
{
while (1)
{
m_windows = getAllWindows();
sleep(m_delay);
const Window *windows = m_windows.toArray();
for (int i = 0; i < windows[0].length - 1; i++)
{
if (!isWindowValid(windows[i]) &&
!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)
{
kill(windows[i].processId, SIGTERM);
}
}
delete[] windows;
}
return 0;
}