forked from Eun/DisableMonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisableMonitorAppDelegate.m
More file actions
216 lines (172 loc) · 6.35 KB
/
DisableMonitorAppDelegate.m
File metadata and controls
216 lines (172 loc) · 6.35 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//
// DisableMonitorAppDelegate.m
// DisableMonitor
//
// Created by Aravindkumar Rajendiran on 10-04-17.
// Copyright 2010 Grapewave. All rights reserved.
//
#import "DisableMonitorAppDelegate.h"
#import <IOKit/graphics/IOGraphicsLib.h>
@implementation DisableMonitorAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
extern io_service_t IOServicePortFromCGDisplayID(CGDirectDisplayID displayID);
extern CGError CGSConfigureDisplayEnabled(CGDisplayConfigRef, CGDirectDisplayID, bool);
NSString* screenNameForDisplay(CGDirectDisplayID displayID)
{
NSString *screenName = nil;
io_service_t service = IOServicePortFromCGDisplayID(displayID);
if (service)
{
NSDictionary *deviceInfo = (NSDictionary *)IODisplayCreateInfoDictionary(service, kIODisplayOnlyPreferredName);
NSDictionary *localizedNames = [deviceInfo objectForKey:[NSString stringWithUTF8String:kDisplayProductName]];
if ([localizedNames count] > 0) {
screenName = [[localizedNames objectForKey:[[localizedNames allKeys] objectAtIndex:0]] retain];
}
[deviceInfo release];
}
return [screenName autorelease];
}
-(void)ToggleMonitor:(CGDirectDisplayID) display enabled:(Boolean) enabled
{
CGError err;
CGDisplayConfigRef config;
@try {
err = CGBeginDisplayConfiguration (&config);
if (err != 0)
{
NSLog(@"Error in CGBeginDisplayConfiguration: %d\n", err);
return;
}
err = CGSConfigureDisplayEnabled(config, display, enabled);
if (err != 0)
{
NSLog(@"Error in CGSConfigureDisplayEnabled: %d\n", err);
return;
}
err = CGCompleteDisplayConfiguration(config, kCGConfigurePermanently);
if (err != 0)
{
NSLog(@"Error in CGCompleteDisplayConfiguration: %d\n", err);
return;
}
}
@catch (NSException *exception) {
}
}
-(void)MonitorClicked:(id) sender
{
NSMenuItem * item = (NSMenuItem*)sender;
CGDirectDisplayID display = (CGDirectDisplayID)[item tag];
Boolean active = CGDisplayIsActive(display);
if (active == true)
{
CGDirectDisplayID displays[0x10];
CGDisplayCount nDisplays = 0;
CGError err = CGGetActiveDisplayList(0x10, displays, &nDisplays);
if (err == 0 && nDisplays - 1 == 0)
{
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert setInformativeText:@"You are disabling your last active monitor, you wont be able to see anything continue?"];
[alert addButtonWithTitle:@"OK"];
[alert addButtonWithTitle:@"Cancel"];
[alert setAlertStyle:NSCriticalAlertStyle];
[alert setMessageText:@"Warning"];
if ([alert runModal] != NSAlertFirstButtonReturn)
{
[alert release];
return;
}
[alert release];
}
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self ToggleMonitor:display enabled:!active];
});
}
-(void)DetectMonitors:(id) sender
{
CGDirectDisplayID displays[0x10];
CGDisplayCount dspCount = 0;
if (CGSGetDisplayList(0x10, displays, &dspCount) == noErr)
{
for(int i = 0; i < dspCount; i++)
{
io_service_t service = IOServicePortFromCGDisplayID(displays[i]);
if (service)
IOServiceRequestProbe(service, kIOFBUserRequestProbe);
}
}
}
- (void)menuNeedsUpdate:(NSMenu *)menu
{
[statusMenu removeAllItems];
CGDirectDisplayID displays[0x10];
CGDisplayCount nDisplays = 0;
CGDisplayErr err = CGSGetDisplayList(0x10, displays, &nDisplays);
if (err == 0 && nDisplays > 0)
{
NSMutableArray *monitors = [[NSMutableArray alloc] init];
NSMutableArray *monitorIDs = [[NSMutableArray alloc] init];
for (int i = 0; i < nDisplays; i++)
{
NSString *name = screenNameForDisplay(displays[i]);
if (name != nil)
{
[monitors addObject: name];
[monitorIDs addObject:[NSNumber numberWithInt:(CGDirectDisplayID)displays[i]]];
}
}
for (int i = 0; i < monitors.count; i++)
{
int num = 0;
int index = 1;
for (int j = 0; j < monitors.count; j++)
{
if ([monitors[j] caseInsensitiveCompare:monitors[i]] == NSOrderedSame)
{
num++;
if (j < i)
{
index++;
}
}
}
NSString *name;
if (num > 1)
name = [NSString stringWithFormat:@"%@ (%d)", monitors[i], index];
else
name = monitors[i];
NSMenuItem *displayItem = [[NSMenuItem alloc] initWithTitle: name action:@selector(MonitorClicked:) keyEquivalent:@""];
if (CGDisplayIsActive(displays[i]))
[displayItem setState:NSOnState];
else
[displayItem setState:NSOffState];
[displayItem setTag:displays[i]];
[statusMenu addItem:displayItem];
[displayItem release];
}
[monitors release];
[monitorIDs release];
}
else
{
NSMenuItem *noDisplays = [[NSMenuItem alloc] initWithTitle: @"No Displays Detected" action:nil keyEquivalent:@""];
[statusMenu addItem:noDisplays];
[noDisplays release];
}
[statusMenu addItem:[NSMenuItem separatorItem]];
NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle: @"Detect Monitors" action:@selector(DetectMonitors:) keyEquivalent:@""];
[statusMenu addItem:menuItem];
[menuItem release];
}
-(void)awakeFromNib{
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];
[statusMenu setDelegate:self];
[statusItem setMenu:statusMenu];
[statusItem setImage:[NSImage imageNamed:@"status_icon.png"]];
[statusItem setHighlightMode:YES];
}
@end