forked from finos/ScreenShareIndicatorFrame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SymphonyScreenShareIndicator.m
155 lines (130 loc) · 5.92 KB
/
SymphonyScreenShareIndicator.m
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
#import <Cocoa/Cocoa.h>
#include <stdlib.h>
int trackedWindowId;
@interface View : NSView
@end
@implementation View
- (void)drawRect:(NSRect)rect {
NSColor* color = [NSColor colorWithDeviceRed:(0xee/255.0f) green:(0x3d/255.0f) blue:(0x3d/255.0f) alpha:1.0f];
[color set];
NSRectFill(rect);
rect.origin.x += 5;
rect.origin.y += 5;
rect.size.width -= 10;
rect.size.height -= 10;
[[NSColor clearColor] set];
NSRectFill( rect );
}
@end
@interface Window : NSWindow
@end
@implementation Window
- (void)setContentView:(NSView *)aView {
NSRect bounds = [self frame];
View* view = [[View alloc] initWithFrame:bounds];
[super setContentView:view];
}
-(void)updateWindow {
NSRect windowRect;
CFArrayRef windowArray = CGWindowListCopyWindowInfo( kCGWindowListOptionOnScreenOnly, kCGNullWindowID );
NSMutableArray *windowsInMap = [NSMutableArray arrayWithCapacity:64];
NSArray* windows = (NSArray*) CGWindowListCopyWindowInfo( kCGWindowListOptionOnScreenOnly, kCGNullWindowID );
NSUInteger count = [windows count];
for (NSUInteger i = 0; i < count; i++) {
NSDictionary* nswindowsdescription = [windows objectAtIndex:i];
NSNumber* windowid = (NSNumber*)[nswindowsdescription objectForKey:@"kCGWindowNumber"];
if( [windowid intValue] == trackedWindowId ) {
CGRectMakeWithDictionaryRepresentation((CFDictionaryRef)[nswindowsdescription objectForKey:@"kCGWindowBounds"], &windowRect);
break;
}
}
CFRelease( windowArray );
[self orderWindow:NSWindowAbove relativeTo:trackedWindowId];
windowRect.origin.y = [ NSScreen.screens[ 0 ] frame ].size.height - windowRect.origin.y - windowRect.size.height;
[super setFrame:windowRect display: YES];
}
@end
void closeExistingInstances( void ) {
NSString* processName = NSProcessInfo.processInfo.processName;
CFArrayRef windowArray = CGWindowListCopyWindowInfo( kCGWindowListOptionOnScreenOnly, kCGNullWindowID );
NSMutableArray *windowsInMap = [NSMutableArray arrayWithCapacity:64];
NSArray* windows = (NSArray*) CGWindowListCopyWindowInfo( kCGWindowListOptionOnScreenOnly, kCGNullWindowID );
NSUInteger count = [windows count];
for (NSUInteger i = 0; i < count; i++) {
NSDictionary* nswindowsdescription = [windows objectAtIndex:i];
NSNumber* windowid = (NSNumber*)[nswindowsdescription objectForKey:@"kCGWindowNumber"];
NSString* ownerName = (NSString*)[nswindowsdescription objectForKey:@"kCGWindowOwnerName"];
NSNumber* ownerPID = (NSNumber*)[nswindowsdescription objectForKey:@"kCGWindowOwnerPID"];
if( [ownerName isEqualToString:processName] ) {
kill( [ownerPID intValue], SIGKILL );
}
}
CFRelease( windowArray );
}
int trackWindow( int displayId ) {
NSRect windowRect;
CFArrayRef windowArray = CGWindowListCopyWindowInfo( kCGWindowListOptionOnScreenOnly, kCGNullWindowID );
NSMutableArray *windowsInMap = [NSMutableArray arrayWithCapacity:64];
NSArray* windows = (NSArray*) CGWindowListCopyWindowInfo( kCGWindowListOptionOnScreenOnly, kCGNullWindowID );
NSUInteger count = [windows count];
for (NSUInteger i = 0; i < count; i++) {
NSDictionary* nswindowsdescription = [windows objectAtIndex:i];
NSNumber* windowid = (NSNumber*)[nswindowsdescription objectForKey:@"kCGWindowNumber"];
if( [windowid intValue] == displayId ) {
CGRectMakeWithDictionaryRepresentation((CFDictionaryRef)[nswindowsdescription objectForKey:@"kCGWindowBounds"], &windowRect);
NSNumber* ownerPID = (NSNumber*)[nswindowsdescription objectForKey:@"kCGWindowOwnerPID"];
NSRunningApplication* app = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPID intValue] ];
[app activateWithOptions: NSApplicationActivateIgnoringOtherApps];
}
}
CFRelease( windowArray );
windowRect.origin.y = [[NSScreen mainScreen] frame].size.height - windowRect.origin.y - windowRect.size.height;
[NSApplication sharedApplication];
trackedWindowId = displayId;
NSWindow* window = [[Window alloc] initWithContentRect:windowRect
styleMask:NSWindowStyleMaskBorderless backing:NSBackingStoreBuffered defer:NO
screen:NSScreen.screens[ 0 ]];
[NSTimer scheduledTimerWithTimeInterval:0.1 target:window selector:@selector(updateWindow) userInfo:nil repeats:YES];
[window setReleasedWhenClosed:YES];
[window setOpaque:NO];
[window setBackgroundColor:[NSColor clearColor]];
[window setIgnoresMouseEvents:YES];
[window makeKeyAndOrderFront:nil];
[NSApp activateIgnoringOtherApps:YES];
[NSApp run];
return EXIT_SUCCESS;
}
int trackScreen( NSRect windowRect ) {
[NSApplication sharedApplication];
NSWindow* window = [[Window alloc] initWithContentRect:windowRect
styleMask:NSWindowStyleMaskBorderless backing:NSBackingStoreBuffered defer:NO
screen:NSScreen.screens[ 0 ] ];
[window setReleasedWhenClosed:YES];
[window setLevel:CGShieldingWindowLevel()];
[window setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces];
[window setOpaque:NO];
[window setBackgroundColor:[NSColor clearColor]];
[window setIgnoresMouseEvents:YES];
[window makeKeyAndOrderFront:nil];
[NSApp activateIgnoringOtherApps:YES];
[NSApp run];
return EXIT_SUCCESS;
}
int main( int argc, char** argv ) {
[NSAutoreleasePool new];
closeExistingInstances();
if( argc < 2 ) {
return EXIT_SUCCESS;
}
int displayId = atoi( argv[ 1 ] );
for( int i = 0; i < NSScreen.screens.count; ++i ) {
NSDictionary* screenDictionary = [NSScreen.screens[ i ] deviceDescription];
NSNumber* screenID = [screenDictionary objectForKey:@"NSScreenNumber"];
if( [screenID intValue] == displayId ) {
NSRect windowRect = NSScreen.screens[ i ].frame;
return trackScreen( windowRect );
break;
}
}
return trackWindow( displayId );
}