-
Notifications
You must be signed in to change notification settings - Fork 7
/
ResolutionDataSource.m
executable file
·246 lines (203 loc) · 8.68 KB
/
ResolutionDataSource.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
/**
* 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 "ResolutionDataSource.h"
#import "ResolutionDataItem.h"
#import "DisplayData.h"
@implementation ResolutionDataSource
@synthesize display;
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
if (item != nil)
return 0;
if (dataItems == nil)
{
int numberOfDisplayModes;
CGSGetNumberOfDisplayModes(display, &numberOfDisplayModes);
if (numberOfDisplayModes <= 0)
{
return 0;
}
NSMutableArray *items = [[NSMutableArray alloc] init];
for (int j = 0; j < numberOfDisplayModes; j++) {
CGSDisplayMode mode;
CGSGetDisplayModeDescriptionOfLength(display, j, &mode, sizeof(mode));
// filter 60hz & HiDpi
if (mode.freq==60 && mode.density>1.5 && mode.depth==8) {
[items addObject: [[ResolutionDataItem alloc] initWithMode:mode]];
}
}
[self loadData:items];
dataItems = [[items sortedArrayUsingComparator:^NSComparisonResult(ResolutionDataItem* a, ResolutionDataItem* b) {
CGSDisplayMode mymode = [a mode];
CGSDisplayMode othermode = [b mode];
if (mymode.width > othermode.width)
return NSOrderedAscending;
else if (mymode.width < othermode.width)
return NSOrderedDescending;
else
return NSOrderedSame;
}] mutableCopy];
[items release];
}
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"])
{
CGSDisplayMode mode = [(ResolutionDataItem*)item mode];
size_t w = mode.width;
size_t h = mode.height;
size_t d = [self bitDepth:display];
// size_t d = mode.depth;
int r = (int)mode.freq;
int gcd = [ResolutionDataItem gcd:w height:h];
int fontSize = [NSFont systemFontSize];
//NSMutableString *res = [NSMutableString stringWithFormat:@"%lux%lux%lu", w, h, d];
NSMutableString *res = [NSMutableString stringWithFormat:@"%lux%lu", w, h];
/*if (r > 0)
[res appendFormat:@"@%d", r];*/
int lres = [res length];
//[res appendFormat:@" [%d:%d]", (int)w/gcd, (int)h/gcd];
int lrat = [res length] - lres;
NSMutableAttributedString *titleText = [[NSMutableAttributedString alloc] initWithString:res];
[titleText addAttributes:[NSDictionary dictionaryWithObject:[NSFont systemFontOfSize:fontSize] forKey:NSFontAttributeName] range:NSMakeRange(0, lres)];
[titleText addAttributes:[NSDictionary dictionaryWithObject:[NSFont boldSystemFontOfSize:fontSize*.7] forKey:NSFontAttributeName] range:NSMakeRange(lres, lrat)];
[titleText addAttribute:NSBaselineOffsetAttributeName value:[NSNumber numberWithFloat:fontSize/(fontSize*.7)] range:NSMakeRange(lres, lrat)];
return titleText;
//return [NSString stringWithFormat:@"%lux%lux%lu@%d", w, h, d, r];
}
else if ([[tableColumn identifier] isEqualToString:@"CheckBox"])
{
return [NSNumber numberWithBool:[item visible]];
}
return nil;
}
- (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
if ([[tableColumn identifier] isEqualToString:@"CheckBox"])
{
[[dataItems objectAtIndex:[dataItems indexOfObject:item]] setVisible:[object boolValue]];
[self saveData];
[outlineView reloadData];
}
}
-(size_t) bitDepth:(CGDirectDisplayID) displayId
{
size_t depth = 0;
char *buffer = (char*)calloc(33, sizeof(char*));
CGSGetDisplayPixelEncodingOfLength(displayId, buffer, 32);
CFStringRef pixelEncoding = (CFStringRef)[NSString stringWithFormat:@"%s", buffer];
// my numerical representation for kIO16BitFloatPixels and kIO32bitFloatPixels
// are made up and possibly non-sensical
if (kCFCompareEqualTo == CFStringCompare(pixelEncoding, CFSTR(kIO32BitFloatPixels), kCFCompareCaseInsensitive)) {
depth = 96;
} else if (kCFCompareEqualTo == CFStringCompare(pixelEncoding, CFSTR(kIO64BitDirectPixels), kCFCompareCaseInsensitive)) {
depth = 64;
} else if (kCFCompareEqualTo == CFStringCompare(pixelEncoding, CFSTR(kIO16BitFloatPixels), kCFCompareCaseInsensitive)) {
depth = 48;
} else if (kCFCompareEqualTo == CFStringCompare(pixelEncoding, CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive)) {
depth = 32;
} else if (kCFCompareEqualTo == CFStringCompare(pixelEncoding, CFSTR(kIO30BitDirectPixels), kCFCompareCaseInsensitive)) {
depth = 30;
} else if (kCFCompareEqualTo == CFStringCompare(pixelEncoding, CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive)) {
depth = 16;
} else if (kCFCompareEqualTo == CFStringCompare(pixelEncoding, CFSTR(IO8BitIndexedPixels), kCFCompareCaseInsensitive)) {
depth = 8;
}
free(buffer);
return depth;
}
- (id) initWithDisplay:(CGDirectDisplayID)aDisplay
{
self = [super init];
if (self) {
dataItems = nil;
[self setDisplay:aDisplay];
}
return self;
}
+ (NSMutableDictionary*) getDictForDisplay:(NSUserDefaults*)userDefaults display:(CGDirectDisplayID)display
{
NSMutableDictionary *dict;
NSObject *data = [userDefaults dictionaryForKey:[NSString stringWithFormat:@"%u", display]];
if (data == nil)
dict = [[[[NSMutableDictionary alloc] init] autorelease] retain];
else
dict = [[[(NSDictionary*)data mutableCopy] autorelease] retain];
return dict;
}
- (void) saveData
{
if (dataItems != nil)
{
// encode data
NSMutableArray *archiveArray = [[NSMutableArray alloc]init];
for (ResolutionDataItem *item in dataItems) {
NSData *encodedItem = [NSKeyedArchiver archivedDataWithRootObject:item];
[archiveArray addObject:encodedItem];
}
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *dict = [ResolutionDataSource getDictForDisplay:userDefaults display:display];
[dict setObject:archiveArray forKey:@"resolutions"];
[userDefaults setObject:dict forKey:[NSString stringWithFormat:@"%u", display]];
[userDefaults synchronize];
}
}
- (void) loadData:(NSMutableArray*)system_items
{
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *dict = [ResolutionDataSource getDictForDisplay:userDefaults display:display];
if ([dict count] <= 0)
return;
NSMutableArray *items = [dict objectForKey:@"resolutions"];
if (items == nil)
return;
for (int i = [items count] - 1; i>= 0; --i)
{
NSData *encodedItem = [items objectAtIndex:i];
ResolutionDataItem *item =[NSKeyedUnarchiver unarchiveObjectWithData:encodedItem];
for (int j = [system_items count] - 1; j>= 0; --j)
{
ResolutionDataItem *sysitem = [system_items objectAtIndex:j];
if (sysitem.mode.width == item.mode.width && sysitem.mode.height == item.mode.height && sysitem.mode.depth == item.mode.depth && sysitem.mode.freq == item.mode.freq)
{
[sysitem setVisible:[item visible]];
break;
}
}
}
}
- (oneway void) release
{
if (dataItems != nil)
{
[dataItems release];
dataItems = nil;
}
}
@end