forked from brockgr/websaver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebSaverView.m
394 lines (315 loc) · 12.2 KB
/
WebSaverView.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
//
// WebSaverView.m
//
// WebSaver - A web screen-saver for MacOS-X
// Copyright (C) 2008 Gavin Brock http://brock-family.org/gavin
//
// This program 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 3 of the License, or
// (at your option) any later version.
//
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
//
// Cheesy Debugging toggle
//#define DebugLog //
#define DebugLog NSLog
#import "WebSaverView.h"
static NSString * const MyModuleName = @"org.brock-family.WebSaver";
static NSString * upArrow, *downArrow, *leftArrow, *rightArrow;
@implementation WebSaverView
- (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
{
DebugLog(@"webView:didStartProvisionalLoadForFrame: %@", frame);
}
- (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
{
DebugLog(@"webView:didCommitLoadForFrame");
}
- (void)webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
{
DebugLog(@"webView:didFailLoadWithError: %@", error);
}
- (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
{
DebugLog(@"webView:didFailProvisionalLoadWithError: %@", error);
}
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
DebugLog(@"webView:didFinishLoadForFrame");
}
- (void)webView:(WebView *)sender didReceiveIcon:(NSImage *)image forFrame:(WebFrame *)frame
{
DebugLog(@"webView:didReceiveIcon");
}
- (void)webView:(WebView *)sender serverRedirectedForDataSource:(WebFrame *)frame
{
DebugLog(@"webView:serverRedirectedForDataSource");
}
- (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
{
NSLog(@"webView:didReceiveTitle: %@", title);
}
- (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview
{
DebugLog(@"webView:initWithFrame isPreview:%d", isPreview);
self = [super initWithFrame:frame isPreview:isPreview];
if (self) {
sms_type = detect_sms();
if (sms_type) {
unichar ua = NSUpArrowFunctionKey;
unichar da = NSDownArrowFunctionKey;
unichar la = NSLeftArrowFunctionKey;
unichar ra = NSRightArrowFunctionKey;
upArrow = [[NSString alloc] initWithCharacters:&ua length:1];
downArrow = [[NSString alloc] initWithCharacters:&da length:1];
leftArrow = [[NSString alloc] initWithCharacters:&la length:1];
rightArrow = [[NSString alloc] initWithCharacters:&ra length:1];
}
ScreenSaverDefaults *defaults;
defaults = [ScreenSaverDefaults defaultsForModuleWithName:MyModuleName];
[defaults registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
@"http://www.apple.com/", @"URL",
@"NO", @"EnableReload",
@"300", @"ReloadTime",
@"NO", @"EnableSMS",
@"NO", @"EnableMultiMonitor",
nil ]];
[self setAnimationTimeInterval:0.5];
saverURLString = [defaults stringForKey:@"URL"];
DebugLog(@"URL: %@", saverURLString);
enableReloadBool = [defaults boolForKey:@"EnableReload"];
DebugLog(@"Will Reload: %d", enableReloadBool);
reloadTimeFloat = [defaults floatForKey:@"ReloadTime"];
DebugLog(@"Reload Time: %ld", reloadTimeFloat);
enableSMSBool = [defaults boolForKey:@"EnableSMS"];
DebugLog(@"Will use SMS: %d", enableSMSBool);
enableMultiMonitorBool = [defaults boolForKey:@"EnableMultiMonitor"];
DebugLog(@"Will use MultiMonitor: %d", enableMultiMonitorBool);
webView = [[WKWebView alloc] initWithFrame:frame];
[webView setDrawsBackground:NO];
[webView setCustomUserAgent:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8) AppleWebKit/536.25 (KHTML, like Gecko) Version/6.0 Safari/536.25"];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
WebPreferences *preferences = [webView preferences];
if([preferences respondsToSelector:@selector(setWebGLEnabled:)]){
[preferences performSelector:@selector(setWebGLEnabled:) withObject:[NSNumber numberWithBool:YES]];
}
#pragma clang diagnostic pop
if (isPreview) {
[self scaleUnitSquareToSize:NSMakeSize( 0.25, 0.25 )];
}
[webView setFrameLoadDelegate:self];
[self addSubview:webView];
}
return self;
}
- (void)setFrame:(NSRect)frameRect
{
DebugLog(@"NSView:setFrame");
//DebugLog(@"frameRect %d,%d %dx%d\n", frameRect.origin.x, frameRect.origin.y, frameRect.size.width, frameRect.size.height);
[super setFrame:frameRect];
[webView setFrame:frameRect];
[webView setFrameSize:[webView convertSize:frameRect.size fromView:nil]];
}
- (void)startAnimation
{
NSString *url;
NSScreen *screen;
int srand_time = 15;
DebugLog(@"webView:startAnimation");
[super startAnimation];
// Calibrate 'natural' positon with 20 readings
if (enableSMSBool && sms_type) {
int loop;
for(loop = 0; loop < 20; loop++) {
int x, y, z;
if (read_sms_scaled(sms_type, &x, &y, &z)) {
avgx = avgx + x;
avgy = avgy + y;
avgz = avgz + z;
}
}
avgx = avgx / 20;
avgy = avgy / 20;
avgz = avgz / 20;
DebugLog(@"SMS Average - x = %d, y = %d, z = %d", avgx, avgy, avgz);
}
if (enableMultiMonitorBool) {
screen = [ [webView window] screen ];
DebugLog(@"Screen %@", [screen description]);
url = [NSString stringWithFormat: @"%@?x=%.0f&y=%.0f&w=%.0f&h=%.0f&screen=%i&srand=%i", saverURLString,
[screen frame].origin.x, [screen frame].origin.y,
[screen frame].size.width, [screen frame].size.height,
(int)[[NSScreen screens] indexOfObject: screen],
((int)time(0)+(srand_time/2)/srand_time) ];
DebugLog(@"URL with query: %@", url );
} else {
url = saverURLString;
}
[self loadUrl];
}
- (void)stopAnimation
{
DebugLog(@"webView:stopAnimation");
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:"]]];
[super stopAnimation];
}
/*
- (void)drawRect:(NSRect)rect
{
// TOO VERBOSE // DebugLog(@"webView:drawRect");
[super drawRect:rect];
}
*/
- (void)doKeyUp:(NSTimer*)theTimer {
//DebugLog(@"doKeyUp");
[[NSApplication sharedApplication] sendEvent:[NSEvent keyEventWithType:NSKeyUp
location: NSMakePoint(1,1)
modifierFlags: 0
timestamp: [[NSDate date] timeIntervalSinceReferenceDate]
windowNumber: [[self window] windowNumber]
context: [NSGraphicsContext currentContext]
characters: [theTimer userInfo]
charactersIgnoringModifiers:[theTimer userInfo]
isARepeat: NO
keyCode: 0]];
}
- (void)doKeyDown:(NSTimer*)theTimer {
//DebugLog(@"doKeyDown");
[[NSApplication sharedApplication] sendEvent:[NSEvent keyEventWithType:NSKeyDown
location: NSMakePoint(1,1)
modifierFlags: 0
timestamp: [[NSDate date] timeIntervalSinceReferenceDate]
windowNumber: [[self window] windowNumber]
context: [NSGraphicsContext currentContext]
characters: [theTimer userInfo]
charactersIgnoringModifiers:[theTimer userInfo]
isARepeat: NO
keyCode: 0]];
}
- (void)animateOneFrame
{
// TOO VERBOSE // DebugLog(@"webView:animateOneFrame");
if (enableReloadBool) {
// DebugLog(@"time %f", [lastLoad timeIntervalSinceNow]);
if (reloadTimeFloat + [lastLoad timeIntervalSinceNow] < 0) {
[[webView mainFrame] reload];
lastLoad = [[NSDate alloc] init];
DebugLog(@"reloaded %@",[lastLoad description]);
}
}
if (enableSMSBool && sms_type) {
int x,y,z;
if (read_sms_scaled(sms_type, &x, &y, &z)) {
//DebugLog(@"average - x = %d, y = %d, z = %d Current Reading: x = %d, y = %d, z = %d", avgx, avgy, avgz, x, y, z);
int cx = avgx - x;
int cy = avgy - y;
if (abs(cx) > abs(cy)) {
if(cx > 30) {
DebugLog(@"SMS Send Right Cursor");
[NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)0 target:self selector:@selector(doKeyDown:) userInfo:rightArrow repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval).05 target:self selector:@selector(doKeyUp:) userInfo:rightArrow repeats:NO];
} else if(cx < -30) {
DebugLog(@"SMS Send Left Cursor");
[NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)0 target:self selector:@selector(doKeyDown:) userInfo:leftArrow repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval).05 target:self selector:@selector(doKeyUp:) userInfo:leftArrow repeats:NO];
}
} else {
if(cy > 30) {
DebugLog(@"SMS Send Up Cursor");
[NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)0 target:self selector:@selector(doKeyDown:) userInfo:upArrow repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval).05 target:self selector:@selector(doKeyUp:) userInfo:upArrow repeats:NO];
} else if(cy < -30) {
DebugLog(@"SMS Send Down Cursor");
[NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)0 target:self selector:@selector(doKeyDown:) userInfo:downArrow repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval).5 target:self selector:@selector(doKeyUp:) userInfo:downArrow repeats:NO];
}
}
}
}
return;
}
- (BOOL)hasConfigureSheet
{
DebugLog(@"webView:hasConfigureSheet");
return YES;
}
- (NSWindow*)configureSheet
{
DebugLog(@"webView:configureSheet");
if (!configSheet) {
if (![NSBundle loadNibNamed:@"ConfigureSheet" owner:self]) {
NSLog( @"Failed to load configure sheet." );
NSBeep();
}
}
NSDictionary *infoDictionary;
infoDictionary = [[NSBundle bundleForClass:[self class]] infoDictionary];
NSString *versionString;
versionString = [infoDictionary objectForKey:@"CFBundleVersion"];
[versionLabel setStringValue:versionString];
[saverURL setStringValue:saverURLString];
[enableReload setState:enableReloadBool];
[reloadTime selectItemWithTag:reloadTimeFloat];
[enableSMS setState:enableSMSBool];
[enableMultiMonitor setState:enableMultiMonitorBool];
[reloadTime setEnabled:[enableReload state]];
[enableSMS setEnabled:sms_type ? true : false];
return configSheet;
}
- (IBAction)enableClick:(id)sender
{
DebugLog(@"IBAction:enableClick");
[reloadTime setEnabled:[enableReload state]];
}
- (IBAction)cancelClick:(id)sender
{
DebugLog(@"IBAction:cancelClick");
[[NSApplication sharedApplication] endSheet:configSheet];
}
- (IBAction)okClick:(id)sender
{
DebugLog(@"IBAction:okClick");
// Check what the user set
saverURLString = [saverURL stringValue];
NSLog(@"Set URL: %@", saverURLString);
enableReloadBool = [enableReload state];
NSLog(@"Set Will Reload: %d", enableReloadBool);
reloadTimeFloat = [[reloadTime selectedItem] tag];
NSLog(@"Set Reload Time: %ld", reloadTimeFloat);
enableSMSBool = [enableSMS state];
NSLog(@"Set Use SMS: %d", enableSMSBool);
enableMultiMonitorBool = [enableMultiMonitor state];
NSLog(@"Set Use Multi Monitor: %d", enableMultiMonitorBool);
// Save the Settings
ScreenSaverDefaults *defaults;
defaults = [ScreenSaverDefaults defaultsForModuleWithName:MyModuleName];
[defaults setObject:saverURLString forKey:@"URL"];
[defaults setBool:enableReloadBool forKey:@"EnableReload"];
[defaults setFloat:reloadTimeFloat forKey:@"ReloadTime"];
[defaults setBool:enableSMSBool forKey:@"EnableSMS"];
[defaults setBool:enableMultiMonitorBool forKey:@"EnableMultiMonitor"];
[defaults synchronize];
[self loadUrl];
// Close the window
[[NSApplication sharedApplication] endSheet:configSheet];
}
- (void)loadUrl
{
// Reload the page and reset load time
NSMutableURLRequest *request =
[NSMutableURLRequest requestWithURL:[NSURL URLWithString:saverURLString]];
//TODO - add this to menu
//[request setHTTPMethod:@"POST"];
[[webView mainFrame] loadRequest:request];
lastLoad = [[NSDate alloc] init];
DebugLog(@"reloaded %@",[lastLoad description]);
}
@end