-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFMN.m
326 lines (273 loc) · 8.9 KB
/
FMN.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
313
314
315
316
317
318
319
320
321
322
323
324
325
//
// FMN.m
// FMN
//
// Created by David Noblet on 7/13/06.
// Copyright 2006 __MyCompanyName__. All rights reserved.
//
#import <ApplicationServices/ApplicationServices.h>
#import "FMN.h"
#import "FMNModule.h"
#import "FMNRestorable.h"
#import "CGDisplayConfiguration.h"
#import "FMNModuleLoader.h"
//#import "X11Bridge.h"
//#import "FMNAXModule.h"
static void FMN_CGDisplayReconfigurationCallback (
CGDirectDisplayID display,
CGDisplayChangeSummaryFlags flags,
void* userInfo
);
@implementation FMN
- (void) registerScreenChangeNotificationHandler
{
CGDisplayRegisterReconfigurationCallback(
FMN_CGDisplayReconfigurationCallback, self);
}
- (void) unregisterScreenChangeNotificationHandler
{
CGDisplayRemoveReconfigurationCallback(
FMN_CGDisplayReconfigurationCallback,self);
}
NSString* describeCurrentConfiguration() {
CGDisplayConfiguration *dc = [CGDisplayConfiguration configWithCurrent];
NSString *s = [dc description];
return s;
}
- (void) handlePreDisplayConfigurationChange
{
NSLog(@"******** Screen configuration about to be changed! ********");
NSLog(@"Current configuration: %@", describeCurrentConfiguration());
NSDate *startDate = [NSDate date];
// Save the current restorables
NSMutableSet *restorables = [NSMutableSet setWithCapacity:20];
NSEnumerator* enumerator = [fmnModules objectEnumerator];
FMNModuleRef module;
while (module = [enumerator nextObject]) {
@try {
NSDate *restorableDate = [NSDate date];
NSArray *tmpRestorables = [module getRestorables];
NSLog(@"Got %d restorables in %f seconds", [tmpRestorables count],
-[restorableDate timeIntervalSinceNow]);
[restorables addObjectsFromArray:tmpRestorables];
}
@catch (NSException* ex) {
NSLog([ex reason]);
}
// We need to handle string exceptions because the X11 module needs to
// raise them from C contexts where building objects is painful.
@catch (NSString * ex) {
NSLog(ex);
}
}
[screenConfigurations setObject:restorables
forKey:currentDisplayConfiguration];
NSLog(@"Saved %d restorables in %f seconds", [restorables count],
-[startDate timeIntervalSinceNow]);
}
int restorableCompare(id a, id b, void* c)
{
FMNRestorableRef r1 = (FMNRestorableRef)a;
FMNRestorableRef r2 = (FMNRestorableRef)b;
int p1 = [r1 priority];
int p2 = [r2 priority];
if(p1 > p2)
{
return NSOrderedAscending;
}
else if (p1 == p2)
{
return NSOrderedSame;
}
return NSOrderedDescending;
}
- (NSDictionary*) getRestorationContext : (FMNDisplayConfigurationRef) newDisplayConfiguration
{
NSMutableDictionary* context =
[NSMutableDictionary dictionaryWithCapacity:10];
[context setObject:newDisplayConfiguration
forKey:@"fmn_new_display_configuration"];
return context;
}
- (void) handlePostDisplayConfigurationChange
{
NSLog(@"======== Screen configuration changed! ========");
NSLog(@"New configuration: %@", describeCurrentConfiguration());
NSDate *startDate = [NSDate date];
// Get the new display configuration
//if (currentDisplayConfiguration)
// [currentDisplayConfiguration release];
currentDisplayConfiguration =
[[CGDisplayConfiguration configWithCurrent] retain];
// Try to retrieve the restorables associated with the new config
NSMutableSet* restorableSet =
[screenConfigurations objectForKey : currentDisplayConfiguration];
if (!restorableSet)
{
NSLog(@"Encountered a new display configuration: %@",
currentDisplayConfiguration);
} else {
NSDictionary* restorationContext =
[self getRestorationContext : currentDisplayConfiguration];
NSMutableArray* restorables = [NSMutableArray arrayWithCapacity:[restorableSet count]];
NSEnumerator* set = [restorableSet objectEnumerator];
FMNRestorableRef restorable;
while(restorable = [set nextObject])
{
[restorables addObject:restorable];
}
// Sort the restorables, according to priority
[restorables sortUsingFunction:restorableCompare context:nil];
NSEnumerator* enumerator = [restorables objectEnumerator];
while (restorable = [enumerator nextObject])
{
@try
{
[restorable restoreWithContext : restorationContext];
}
@catch (NSException* ex)
{
NSLog([ex reason]);
}
}
NSLog(@"Restored %d restorables in %f seconds for configuration: %@",
[restorables count], -[startDate timeIntervalSinceNow],
currentDisplayConfiguration);
/* May want to remove the restorables from screenConfigurations at this
point -- it's just going to be discarded at the next config change */
//[restorationContext release];
}
// Do the restoreFinished callbacks
NSEnumerator* enumerator = [fmnModules objectEnumerator];
FMNModuleRef mod;
while (mod = [enumerator nextObject])
{
@try
{
[mod restoreFinished];
}
@catch (NSException* ex)
{
NSLog([ex reason]);
}
}
}
- (BOOL) activateFMN
{
// Register to be notified when the screen configuration changes
NSLog(@"Activating");
[self registerScreenChangeNotificationHandler];
isActive = YES;
return YES;
}
- (BOOL) deactivateFMN
{
// Unregister the screen change handler
NSLog(@"Deactivating");
[self unregisterScreenChangeNotificationHandler];
isActive = NO;
return YES;
}
- (BOOL) isActiveFMN
{
return isActive;
}
- (void) quitFMN
{
NSLog(@"Quitting");
[[NSApplication sharedApplication] terminate:self];
}
- (id) init
{
if (![super init])
{
return nil;
}
// Initialize the current display configuration
currentDisplayConfiguration =
[[CGDisplayConfiguration configWithCurrent] retain];
// Set up the modules.
NSBundle *mainBundle = [NSBundle mainBundle];
fmnModules =
[[FMNModuleLoader allPluginsOfBundle:mainBundle
withProtocol:@protocol(FMNModule)] retain];
// XXX: Deserialize?
// Initialize dictionary of screen configurations
screenConfigurations = [[NSMutableDictionary alloc] init];
[self activateFMN];
return self;
}
- (BOOL) registerAsServer
{
serverConnection = [NSConnection defaultConnection];
[serverConnection setRootObject:self];
return [serverConnection registerName:@"org.metaprl.ForgetMeNot"];
}
- (void) awakeFromNib
{
NSLog(@"Awake!");
if (![self registerAsServer])
NSLog(@"Couldn't register as a distributed object server!");
}
- (void) dealloc
{
[self deactivateFMN];
// Release the dictionary
if (screenConfigurations)
{
[screenConfigurations release];
}
// Release the current display configuration
if (currentDisplayConfiguration)
{
[currentDisplayConfiguration release];
}
if (fmnModules)
[fmnModules release];
if (serverConnection)
[serverConnection release];
[super dealloc];
}
/* Archive the stored configurations */
- (BOOL) archiveDisplayConfigurationsToFile:(NSString *)path
{
return [NSKeyedArchiver archiveRootObject:screenConfigurations toFile:path];
}
/* Load stored configs from a file */
- (void) loadDisplayConfigurationsFromFile:(NSString *)path
{
screenConfigurations = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
}
- (void) postConfigTimerCB:(NSTimer *)timer
{
[self handlePostDisplayConfigurationChange];
}
@end
static void FMN_CGDisplayReconfigurationCallback (
CGDirectDisplayID display,
CGDisplayChangeSummaryFlags flags,
void* userInfo
)
{
FMN* fmn = (FMN*) userInfo;
NSLog(@"Got %@display change notification on 0x%x, is %@main",
(flags & kCGDisplayBeginConfigurationFlag) ? @"pre-" : @"post-",
display, CGDisplayIsMain(display)? @"":@"not ");
// Only want to react once, not once per screen
if (!CGDisplayIsMain(display))
return;
if(flags & kCGDisplayBeginConfigurationFlag)
{
[fmn handlePreDisplayConfigurationChange];
}
else
{
// Our changes seem to be more reliable if we wait a bit before firing.
[NSTimer scheduledTimerWithTimeInterval:0.2
target:fmn
selector:@selector(postConfigTimerCB:)
userInfo:nil
repeats:NO];
//[fmn handlePostDisplayConfigurationChange];
}
}