forked from Eun/DisableMonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MonitorDataSource.m
312 lines (263 loc) · 10.3 KB
/
MonitorDataSource.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
* DisableMonitor, Disable Monitors on Mac
*
* Copyright (C) 2014 Tobias Salzmann
*
* DisableMonitor is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* DisableMonitor is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU General Public License for more details. You should have received a copy of the GNU
* General Public License along with DisableMonitor. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Tobias Salzmann
*/
#import "MonitorDataSource.h"
#import <IOKit/i2c/IOI2CInterface.h>
#import <IOKit/graphics/IOGraphicsLib.h>
#import "DisplayIDAndNameCondition.h"
#import "ResolutionDataSource.h"
#include <stdlib.h>
@implementation MonitorDataSource
@synthesize display;
+(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];
}
+(NSMutableArray*) GetSortedDisplays
{
return [self GetSortedDisplays:NULL];
}
+(NSMutableArray*) GetSortedDisplays:(CGDirectDisplayID)skipDisplayID
{
NSArray *displays = nil;
CGDisplayCount nDisplays = 0;
CGDirectDisplayID displayList[0x10];
NSMutableArray *displayArray = [[NSMutableArray alloc] init];
CGDisplayErr err = CGSGetDisplayList(0x10, displayList, &nDisplays);
if (err == 0 && nDisplays > 0)
{
for (int i = 0; i < nDisplays; i++)
{
[displayArray addObject: [NSNumber numberWithUnsignedInt:displayList[i]]];
}
displays = [displayArray sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
CGDirectDisplayID _a = [a unsignedIntValue], _b = [b unsignedIntValue];
if (_a == _b)
return NSOrderedSame;
else if (_a < _b)
return NSOrderedAscending;
else
return NSOrderedDescending;
}];
nDisplays = [displays count];
}
if (nDisplays > 0)
{
NSMutableArray *monitors = [[NSMutableArray alloc] init];
for (int i = 0; i < nDisplays; i++)
{
NSString *name = [MonitorDataSource screenNameForDisplay:[[displays objectAtIndex:i] unsignedIntValue]];
if (name != nil)
{
[monitors addObject: name];
}
else
{
[monitors addObject: [NSString stringWithFormat:@"Display #%d", i + 1]];
}
}
NSMutableArray *retDisplays = [[NSMutableArray alloc] init];
for (int i = 0; i < monitors.count; i++)
{
int num = 0;
int index = 1;
if (!CGDisplayIsOnline([[displays objectAtIndex:i] unsignedIntValue]))
continue;
for (int j = 0; j < monitors.count; j++)
{
if ([[monitors objectAtIndex:j] caseInsensitiveCompare:[monitors objectAtIndex:i]] == NSOrderedSame)
{
num++;
if (j < i)
{
index++;
}
}
}
if ([[displays objectAtIndex:i] unsignedIntValue] == skipDisplayID)
continue;
NSString *name;
if (num > 1)
name = [NSString stringWithFormat:@"%@ (%d)", [monitors objectAtIndex:i], index];
else
name = [monitors objectAtIndex:i];
DisplayIDAndName *idAndName = [[DisplayIDAndName alloc] init];
[idAndName setId:[[displays objectAtIndex:i] unsignedIntValue]];
[idAndName setName:[name retain]];
[retDisplays addObject:idAndName];
}
[monitors release];
return retDisplays;
}
else
{
return nil;
}
}
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
if (item != nil)
return 0;
if (dataItems == nil)
{
NSMutableArray* availableDisplays = [MonitorDataSource GetSortedDisplays:display];
dataItems = [[NSMutableArray alloc] init];
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *dict = [ResolutionDataSource getDictForDisplay:userDefaults display:display];
NSMutableArray *items = nil;
if ([dict count] > 0)
{
items = [dict objectForKey:listToUse];
}
// add all monitors that are available
for (int i = [availableDisplays count] - 1; i>= 0; --i)
{
DisplayIDAndNameCondition *item_display = [[DisplayIDAndNameCondition alloc] initWithDisplayIDAndName:[availableDisplays objectAtIndex:i]];
for (int j = [items count] - 1; j>= 0; --j)
{
DisplayIDAndNameCondition *store_item =[NSKeyedUnarchiver unarchiveObjectWithData:[items objectAtIndex:i]];
if ([item_display id] == [store_item id])
{
[item_display setEnabled:[store_item enabled]];
[item_display setDisabled:[store_item disabled]];
break;
}
}
[dataItems addObject:item_display];
}
// add all monitors that are not connected
for (int i = [items count] - 1; i>= 0; --i)
{
DisplayIDAndNameCondition *store_item =[NSKeyedUnarchiver unarchiveObjectWithData:[items objectAtIndex:i]];
bool isInList = false;
for (int j = [dataItems count] - 1; j>= 0; --j)
{
if ([store_item id] == [[dataItems objectAtIndex:i] id])
{
isInList = true;
break;
}
}
if (!isInList)
{
[store_item setName:[[NSString stringWithFormat:NSLocalizedString(@"PREF_DISCONNECTED_MONITOR", NULL), [store_item id]] retain]];
[dataItems addObject:store_item];
}
}
}
return [dataItems count];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
return NO;
}
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
if (item == nil)
{
return [dataItems objectAtIndex:index];
}
return nil;
}
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
if (item == nil)
return nil;
if ([[tableColumn identifier] isEqualToString:@"Name"])
{
DisplayIDAndNameCondition *idAndNameCondition = (DisplayIDAndNameCondition*)item;
return [idAndNameCondition name];
}
else if ([[tableColumn identifier] isEqualToString:@"CheckBoxEnabled"])
{
DisplayIDAndNameCondition *idAndNameCondition = (DisplayIDAndNameCondition*)item;
return [NSNumber numberWithBool:[idAndNameCondition enabled]];
}
else if ([[tableColumn identifier] isEqualToString:@"CheckBoxDisabled"])
{
DisplayIDAndNameCondition *idAndNameCondition = (DisplayIDAndNameCondition*)item;
return [NSNumber numberWithBool:[idAndNameCondition disabled]];
}
return nil;
}
- (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
if ([[tableColumn identifier] isEqualToString:@"CheckBoxEnabled"] || [[tableColumn identifier] isEqualToString:@"CheckBoxDisabled"])
{
DisplayIDAndNameCondition *idAndNameCondition = [dataItems objectAtIndex:[dataItems indexOfObject:item]];
if ([[tableColumn identifier] isEqualToString:@"CheckBoxEnabled"])
{
[idAndNameCondition setEnabled:[object boolValue]];
if ([idAndNameCondition enabled] && [idAndNameCondition disabled])
{
[idAndNameCondition setEnabled:true];
[idAndNameCondition setDisabled:false];
}
}
else
{
[idAndNameCondition setDisabled:[object boolValue]];
if ([idAndNameCondition enabled] && [idAndNameCondition disabled])
{
[idAndNameCondition setEnabled:false];
[idAndNameCondition setDisabled:true];
}
}
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *dict = [ResolutionDataSource getDictForDisplay:userDefaults display:display];
NSMutableArray *archiveArray = [NSMutableArray arrayWithCapacity:[dataItems count]];
for (DisplayIDAndNameCondition *item in dataItems) {
if ([item enabled] || [item disabled])
{
NSData *encodedItem = [NSKeyedArchiver archivedDataWithRootObject:item];
[archiveArray addObject:encodedItem];
}
}
[dict setObject:archiveArray forKey:listToUse];
[userDefaults setObject:dict forKey:[NSString stringWithFormat:@"%u", display]];
[userDefaults synchronize];
[outlineView reloadData];
}
}
- (id) initWithDisplay:(CGDirectDisplayID)aDisplay useEnableList:(BOOL)useEnableList
{
self = [super init];
if (self) {
dataItems = nil;
if (useEnableList)
listToUse = @"enable_ruleset";
else
listToUse = @"disable_ruleset";
[self setDisplay:aDisplay];
}
return self;
}
- (oneway void) release
{
if (dataItems != nil)
{
[dataItems release];
dataItems = nil;
}
}
@end