forked from tomcool420/SMFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SMFEventManager.m
280 lines (266 loc) · 9.12 KB
/
SMFEventManager.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
//
// SMFEventManager.m
// SMFramework
//
// Created by Thomas Cool on 10/31/10.
// Copyright 2010 tomcool.org. All rights reserved.
//
#import "SMFEventManager.h"
#import "SMFScreenCapture.h"
#import "SMFEventConfiguration.h"
#import "SMFPhotoMethods.h"
#define eventsPlist (CFStringRef)@"org.tomcool.SMFramework.eventManager"
#define eventString @"org.tomcool.SMFramework.eventManager"
#define remoteActionDict (CFStringRef)@"remoteActions"
#define keyDict (CFStringRef)@"keyboardKeys"
#define kSMFEventDefaultScreenshot @"defaults.screenshot"
#define kSMFEventDefaultRestartLowtide @"defaults.restartLowtide"
#define kSMFEventDefaultSetup @"defaults.setup"
#define kSMFEventDefaultSlideshow @"defaults.slideshow"
@implementation SMFEventManager
SYNTHESIZE_SINGLETON_FOR_CLASS(SMFEventManager,sharedManager)
-(id)init
{
self = [super init];
_delegates=[[NSMutableDictionary alloc] init];
[self loadInformation];
_listeners=[[NSMutableDictionary alloc]init];
_listenerSet=[[NSMutableSet alloc]init];
[SMFScreenCapture sharedInstance];
[SMFDefaultEvents sharedDefaults];
_learning=NO;
return self;
}
-(void)registerListener:(id<SMFEventDelegate>)listener forName:(NSString *)name
{
[_listenerSet addObject:listener];
[_delegates setObject:listener forKey:name];
}
-(void)registerListener:(id<SMFEventDelegate>)listener forNames:(NSArray*)names
{
[_listenerSet addObject:listener];
for(NSString *name in names)
{
[_delegates setObject:listener forKey:name];
}
}
-(void)loadInformation
{
_remoteActionKeys=[ [ (NSDictionary *) CFPreferencesCopyAppValue(remoteActionDict, eventsPlist) autorelease]mutableCopy];
if (_remoteActionKeys==nil) {
_remoteActionKeys=[[NSMutableDictionary alloc]init];
}
_keys=[[[(NSDictionary *)CFPreferencesCopyAppValue(keyDict, eventsPlist) autorelease] mutableCopy] autorelease];
if (_keys==nil) {
_keys=[[NSMutableDictionary alloc]init];
}
}
-(void)startLearning
{
_learning=YES;
}
-(void)stopLearning
{
_learning=NO;
}
-(NSArray *)listeners
{
return [_listenerSet allObjects];
}
-(NSArray *)eventsForListener:(id<SMFEventDelegate>)listener
{
NSMutableArray *a = [NSMutableArray array];
for(NSString *k in [_delegates allKeys])
{
if ([_delegates objectForKey:k]==listener) {
[a addObject:k];
}
}
return a;
}
-(void)setKey:(NSString *)key forName:(NSString *)name
{
[_keys setObject:name forKey:key];
CFPreferencesSetAppValue(keyDict, (CFDictionaryRef)_keys, eventsPlist);
CFPreferencesAppSynchronize(eventsPlist);
}
-(void)setRemoteAction:(int)action forName:(NSString *)name
{
[_remoteActionKeys setObject:name forKey:[NSString stringWithFormat:@"%d",action,nil]];
CFPreferencesSetAppValue(remoteActionDict, (CFDictionaryRef)_remoteActionKeys, eventsPlist);
CFPreferencesAppSynchronize(eventsPlist);
}
-(void)callEventForAction:(NSString *)key
{
if ([[_keys allKeys] containsObject:key])
{
NSString *name = [_keys objectForKey:key];
if ([[_delegates allKeys] containsObject:name])
{
id<SMFEventDelegate>listener = [_delegates objectForKey:name];
SMFEvent *e = [SMFEvent eventWithName:name andKey:key];
[listener actionForEvent:e];
}
}
}
-(void)callEventForBREvent:(BREvent *)event
{
NSDictionary *d = [event eventDictionary];
NSString *s = [d objectForKey:@"kBRKeyEventCharactersKey"];
if (d!=nil && s!=nil) {
[self callEventForAction:s];
}
else if([event value]==0x01)
{
[self callEventForRemoteAction:[event remoteAction]];
}
}
-(BOOL)callEventForRemoteAction:(int)action
{
NSString *key=[NSString stringWithFormat:@"%d",action,nil];
if ([[_remoteActionKeys allKeys] containsObject:key])
{
NSString *name = [_remoteActionKeys objectForKey:key];
if ([[_delegates allKeys] containsObject:name])
{
id<SMFEventDelegate>listener = [_delegates objectForKey:name];
SMFEvent *e = [SMFEvent eventWithName:name andKey:key];
[listener actionForEvent:e];
return YES;
}
else
return NO;
}
else
return NO;
}
-(BOOL)actionDefinedForBREvent:(BREvent *)event
{
if (_learning)
return NO;
if ([event remoteAction]==47) {
NSDictionary *d = [event eventDictionary];
NSString *s = [d objectForKey:@"kBRKeyEventCharactersKey"];
if (d!=nil && s!=nil)
{
if ([s isEqualToString:@" "] && [[BRMediaPlayerManager singleton]_presentedPlayerController]==[[[BRApplicationStackManager singleton] stack]peekController]) {
return YES;
}
else
return [self actionDefinedForKey:s];
}
else
return NO;
}
else if([event value]==0x01)
{
return [self actionDefinedForAction:[event remoteAction]];
}
return NO;
}
-(BOOL)actionDefinedForKey:(NSString *)key
{
if(_learning)
return NO;
return [[_keys allKeys] containsObject:key];
}
-(BOOL)actionDefinedForAction:(int)action
{
if (_learning)
return NO;
NSString *key=[NSString stringWithFormat:@"%d",action,nil];
return [[_remoteActionKeys allKeys]containsObject:key];
}
@end
@implementation SMFDefaultEvents
SYNTHESIZE_SINGLETON_FOR_CLASS(SMFDefaultEvents,sharedDefaults)
-(id)init
{
self=[super init];
[[SMFEventManager sharedManager]registerListener:self
forNames:[NSArray arrayWithObjects:
kSMFEventDefaultScreenshot,
kSMFEventDefaultRestartLowtide,
kSMFEventDefaultSetup,
kSMFEventDefaultSlideshow,
nil]];
// [[SMFEventManager sharedManager]setRemoteAction:63265 forName:kSMFEventDefaultScreenshot];
// [[SMFEventManager sharedManager]setRemoteAction:63266 forName:kSMFEventDefaultRestartLowtide];
// [[SMFEventManager sharedManager]setRemoteAction:63267 forName:kSMFEventDefaultSlideshow];
// [[SMFEventManager sharedManager]setRemoteAction:18 forName:kSMFEventDefaultScreenshot];
// [[SMFEventManager sharedManager]setRemoteAction:15 forName:kSMFEventDefaultScreenshot];
[[SMFEventManager sharedManager]setKey:@"=" forName:kSMFEventDefaultSetup];
return self;
}
-(NSString *)displayName
{
return @"Default Settings";
}
-(void)restartLowtide
{
[[BRApplication sharedApplication] terminate];
}
-(void)actionForEvent:(SMFEvent *)event
{
// NSLog(@"%@ %@ %@ %@",kSMFEventDefaultScreenshot,kSMFEventDefaultRestartLowtide,kSMFEventDefaultSetup,kSMFEventDefaultSlideshow);
// NSLog(@"event: %@ %@",event,[event name]);
if ([[event name] isEqualToString:kSMFEventDefaultScreenshot])
[self takeScreenshot];
else if ([[event name] isEqualToString:kSMFEventDefaultRestartLowtide])
[self restartLowtide];
else if ([[event name] isEqualToString:kSMFEventDefaultSetup])
[self configureEventManager];
else if ([[event name] isEqualToString:kSMFEventDefaultSlideshow])
[self startSlideshowController];
}
-(NSString *)displayNameForEventKey:(NSString *)key
{
if ([key isEqualToString:kSMFEventDefaultScreenshot])
return @"Screenshot";
else if ([key isEqualToString:kSMFEventDefaultRestartLowtide])
return @"Restart Lowtide";
else if ([key isEqualToString:kSMFEventDefaultRestartLowtide])
return @"Configure Events Triggers";
return nil;
}
-(void)takeScreenshot
{
NSFileManager *man = [NSFileManager defaultManager];
BOOL good=TRUE;
NSString *path=@"/screenshot.png";
if ([man fileExistsAtPath:@"/screenshot.png"]) {
good =FALSE;
}
int i =1;
while (!good) {
i++;
path = [NSString stringWithFormat:@"/screenshot_%i.png",i,nil];
if (![man fileExistsAtPath:path])
good=TRUE;
}
[SMFScreenCapture saveScreenToFile:path];
}
-(void)startSlideshowController
{
NSLog(@"slideshow");
//BRDataStore *store = [SMFPhotoMethods dataStoreForPath:@"/System/Library/PrivateFrameworks/AppleTV.framework/DefaultAnimalPhotos/"];
BRDataStore *store = [SMFPhotoMethods dataStoreForPath:@"/nature"];
NSLog(@"store: %@",store);
BRPhotoControlFactory* controlFactory = [BRPhotoControlFactory standardFactory];
SMFPhotoCollectionProvider* provider = [SMFPhotoCollectionProvider providerWithDataStore:store controlFactory:controlFactory];//[[ATVSettingsFacade sharedInstance] providerForScreenSaver];//[collection provider];
SMFPhotoBrowserController* pc = [SMFPhotoBrowserController controllerForProvider:provider];
[pc setTitle:@"DefaultAnimalPhotos"];
[[[BRApplicationStackManager singleton] stack] pushController:pc];
}
-(void)configureEventManager
{
SMFEventConfiguration *c = [[[SMFEventConfiguration alloc] init] autorelease];
[[[BRApplicationStackManager singleton] stack]pushController:c];
}
-(void)test1
{
}
-(void)test2
{
}
@end