-
Notifications
You must be signed in to change notification settings - Fork 1
/
CGDisplayConfiguration.m
157 lines (124 loc) · 3.15 KB
/
CGDisplayConfiguration.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
//
// CGDisplayConfiguration.m
// FMN
//
// Created by David Noblet on 7/19/06.
// Copyright 2006 __MyCompanyName__. All rights reserved.
//
#import "CGDisplayConfiguration.h"
#import "CGDisplay.h"
@implementation CGDisplayConfiguration
- (id) init { [self release]; return nil; }
- (id) initWithCurrent
{
if (![super init])
return nil;
mainDisplay = 0;
displays = 0;
int i;
unsigned int screen_count = CG_MAX_DISPLAYS;
NSMutableData* screenList = [NSMutableData dataWithCapacity: screen_count *
sizeof(CGDirectDisplayID)];
CGDirectDisplayID* screenList_p =
(CGDirectDisplayID*)[screenList mutableBytes];
if(CGGetActiveDisplayList(screen_count, screenList_p, &screen_count))
{
[self release];
return nil;
}
displays = [[NSMutableArray arrayWithCapacity:screen_count] retain];
for(i=0; i<screen_count; ++i)
{
[displays addObject: [[[CGDisplay alloc] initWithDisplayID:
screenList_p[i]] autorelease]];
if(CGDisplayIsMain(screenList_p[i]))
mainDisplay = [[CGDisplay alloc] initWithDisplayID:
screenList_p[i]];
}
[displays sortUsingSelector : @selector(compare:)];
return self;
}
+ (FMNDisplayConfigurationRef) configWithCurrent
{
return [[[CGDisplayConfiguration alloc] initWithCurrent] autorelease];
}
- (unsigned) getDisplayCount
{
return [displays count];
}
- (FMNDisplayRef) getDisplay : (unsigned) i
{
return [displays objectAtIndex : i];
}
- (FMNDisplayRef) getMainDisplay
{
return mainDisplay;
}
- (BOOL) isEqual : (id) obj
{
//return [self hash] == [obj hash];
if (![obj conformsToProtocol: @protocol(FMNDisplayConfiguration)])
{
return NO;
}
FMNDisplayConfigurationRef display = (FMNDisplayConfigurationRef) obj;
int display_count = [self getDisplayCount];
if ([display getDisplayCount] != display_count)
{
return NO;
}
int i;
if(![[self getMainDisplay] isEqual : [display getMainDisplay]])
return NO;
for(i=0; i<display_count; ++i)
{
if(![[self getDisplay : i] isEqual : [display getDisplay : i]])
{
return NO;
}
}
return YES;
}
- (unsigned) hash
{
int display_count = [self getDisplayCount];
int i,ret = 0;
for(i=0; i<display_count; ++i)
{
ret ^= [[self getDisplay : i] hash];
}
return ret;
}
- (NSString*) description
{
return [NSString stringWithFormat:
@"CGDisplayConfiguration with %i displays, displays = %@, hash = %d",
[self getDisplayCount], displays, [self hash]];
}
- (id) copyWithZone : (NSZone *) zone
{
return [self retain];
}
- (void) dealloc
{
if(mainDisplay)
{
[mainDisplay release];
}
if(displays)
{
[displays release];
}
[super dealloc];
}
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:displays forKey:@"CGDCdisplays"];
}
- (id)initWithCoder:(NSCoder *)decoder
{
self = [super init];
displays = [[decoder decodeObjectForKey:@"CGDCdisplays"] retain];
return self;
}
@end