-
Notifications
You must be signed in to change notification settings - Fork 3
/
MacSaber.m
345 lines (289 loc) · 9.19 KB
/
MacSaber.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
#import "MacSaber.h"
#define TICKS 6
#define MAXBRIGHT 4079
@implementation MacSaber
-(id)init
{
srandom(time(NULL));
going = TRUE;
error = FALSE;
reportEnabled = TRUE;
sStrike = 3;
sIdle = 2;
sHit = 5;
sOff = 1;
sOn = 1;
sStart = 1;
sSwing = 8;
roll2 = rollD2 = tilt2 = tiltD2 = 0;
timer = [[NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(getpos:)
userInfo:nil
repeats:YES] retain];
idle = [[NSTimer scheduledTimerWithTimeInterval:0.01
target:self
selector:@selector(idle:)
userInfo:nil
repeats:YES] retain];
NSString *macTypeName;
Gestalt('mnam', &macTypeCName);
macTypeName = [[[NSString alloc] initWithCString:macTypeCName] autorelease];
structSize = 57;
if([macTypeName rangeOfString:@"MacBook"].length != 0) {
macType = MACBOOK;
structSize = 37;
} else if([macTypeName rangeOfString:@"PowerBook5,6"].length != 0) {
macType = POWERBOOK;
} else if([macTypeName rangeOfString:@"PowerBook5,7"].length != 0) {
macType = POWERBOOK;
} else if([macTypeName rangeOfString:@"PowerBook5,"].length != 0) {
macType = HIRESPOWERBOOK;
} else if([macTypeName rangeOfString:@"PowerBook"].length != 0) {
macType = POWERBOOK;
} else {
macType = MACBOOK;
structSize = 37;
}
NSLog(@"Specs: %i, %@",macType,macTypeName);
[[NSSound soundNamed:@"start0"] play];
setKbBrightness(0,500);
return self;
}
- (IBAction)debug:(id)sender
{
if ([debugPane isVisible])
{
NSLog(@"Debug off");
[debugPane orderOut:sender];
} else {
NSLog(@"Debug on");
[debugPane orderFront:sender];
}
}
- (IBAction)report:(id)sender
{
if(reportEnabled == FALSE)
{
NSLog(@"Reporting disabled");
return;
}
// NSLog([debugBox stringValue]);
NSURL * url;
NSString * debugBoxValue = [debugBox stringValue];
NSMutableString *urltext = [[NSMutableString alloc] initWithString:@"mailto:[email protected]?subject=MacSaber%20Error%20Report&body="];
[urltext appendString:[debugBoxValue stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
NSLog(urltext);
url = [NSURL URLWithString:urltext];
assert(url != nil);
(void) [[NSWorkspace sharedWorkspace] openURL:url];
[reportButton setTitle:@"Thank you!"];
[reportButton setEnabled:FALSE];
reportEnabled = FALSE;
}
- (IBAction)checkForUpdates:(id)sender
{
[self setVersionURL:@"http://isnoop.net/apps/version.php?current="];
[self checkVersion];
}
- (IBAction)check:(id)sender
{
if(error == true)
return;
if(going == FALSE)
{
[[NSSound soundNamed:[NSString stringWithFormat:@"on%d",`()%sOn]] play];
going = TRUE;
timer = [[NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(getpos:)
userInfo:nil
repeats:YES] retain];
idle = [[NSTimer scheduledTimerWithTimeInterval:0.01
target:self
selector:@selector(idle:)
userInfo:nil
repeats:YES] retain];
} else {
if([[NSSound soundNamed:@"start0"] isPlaying])
[[NSSound soundNamed:@"start0"] stop];
// if([[NSSound soundNamed:@"on0"] isPlaying])
// [[NSSound soundNamed:@"on0"] stop];
[[NSSound soundNamed:@"idle0"] stop];
[[NSSound soundNamed:[NSString stringWithFormat:@"off%d",random()%sOff]] play];
[idle invalidate];
[idle release];
[timer invalidate];
[timer release];
going = FALSE;
}
}
- (void)idle:(NSTimer *)aTimer
{
if(![[NSSound soundNamed:@"idle0"] isPlaying])
[[NSSound soundNamed:@"idle0"] play];
}
- (IBAction)getpos:(NSTimer *)aTimer
{
int hitThresh = 128;
int strikeThresh = 90;
int swingThresh = 20;
int gyro[3];
static BOOL firstRun = TRUE;
//Difference
int rollD, tiltD, deltaD;
// Acceleration
int rollA, tiltA, deltaA;
int mult, result;
// Keyboard brightness
unsigned int bright = 0, fade;
mult = macType == MACBOOK ? 1 : 2.3;
NSMutableString *rollDir;
result = getMotion(macType,structSize,&gyro);
if(firstRun == TRUE)
{
NSBundle *app = [NSBundle mainBundle];
NSString *ident = [app bundleIdentifier];
NSDictionary *infoDict = [app infoDictionary];
NSString *myVersion = (NSString *)[infoDict valueForKey:@"CFBundleVersion"];
if(result != 1)
{
[debugBox setStringValue:[NSString stringWithFormat:@"Version: %@\nSMS Error: %d\n%s\nClass: %d",myVersion,result,macTypeCName+1,macType]];
NSLog(reportData);
[idle invalidate];
[idle release];
[timer invalidate];
[timer release];
error = TRUE;
return;
// [self check];
} else {
[reportButton setTitle:@"Nothing to report"];
[reportButton setEnabled:FALSE];
reportEnabled = FALSE;
[debugBox setStringValue:[NSString stringWithFormat:@"SYSTEM OK\nVersion: %@\n%s\nClass: %d",myVersion,macTypeCName+1,macType]];
}
}
tilt = gyro[2] * 2;
roll = gyro[0];
// Add a little dead zone around the 90 degree points
// Not all motion sensors are created equal
roll = abs(roll) <= 8 ? 0 : roll;
roll = abs(roll) >= 120 ? 120 : roll;
// Calculate difference
rollD = roll - roll2;
tiltD = tilt - tilt2;
deltaD = abs(rollD) < abs(tiltD) ? rollD : tiltD;
// Calculate momentary acceleration
rollA = abs(rollD - rollD2);
tiltA = abs(tiltD - tiltD2);
deltaA = rollA < tiltA ? rollA : tiltA;
if(deltaA * mult > hitThresh)
{
bright = 1;
[[NSSound soundNamed:[NSString stringWithFormat:@"hit%d",random()%sHit]] play];
} else if (deltaA > strikeThresh) {
bright = 1;
[[NSSound soundNamed:[NSString stringWithFormat:@"strike%d",random()%sStrike]] play];
}
if(bright > 0)
{
bright = (deltaA - strikeThresh) * 50 + getKbBrightness();
bright = bright > MAXBRIGHT ? MAXBRIGHT : bright;
fade = 150 + bright / 15;
// printf("%d - %d\n ",bright, fade);
setKbBrightness(bright,10);
setKbBrightness(0,fade);
} else {
setKbBrightness(0,500);
}
if(abs(deltaD) * mult > swingThresh)
{
[[NSSound soundNamed:[NSString stringWithFormat:@"swing%d",random()%sSwing]] play];
}
if(gyro[1] == -1)
{
roll = gyro[0] > 0 ? gyro[0]-256 : gyro[0];
rollDir = @"Right";
}
else{
roll = gyro[0] * -1 > 0 ? gyro[0]+256 : gyro[0];
rollDir = @"Left ";
// [f1 setStringValue:[NSString stringWithFormat:@"Left %d",roll]];
}
[f1 setStringValue:[NSString stringWithFormat:@"%d",roll]];
[f2 setStringValue:[NSString stringWithFormat:@"%d",tilt]];
[f3 setStringValue:[NSString stringWithFormat:@"%d",deltaD]];
[f3b setStringValue:[NSString stringWithFormat:@"%d",deltaA]];
roll2 = roll;
tilt2 = tilt;
rollD2 = rollD;
tiltD2 = tiltD;
firstRun = FALSE;
}
- (void)awakeFromNib
{
[f1 setStringValue:@""];
[f2 setStringValue:@""];
[f3 setStringValue:@""];
[f1b setStringValue:@""];
[f2b setStringValue:@""];
[f3b setStringValue:@""];
}
- (void)setVersionURL:(NSMutableString *)value {
[value retain];
[versionURL release];
versionURL = [[NSMutableString alloc] initWithString:value];
}
- (void)checkVersion {
NSBundle *app = [NSBundle mainBundle];
NSString *ident = [app bundleIdentifier];
NSDictionary *infoDict = [app infoDictionary];
NSString *myVersion = (NSString *)[infoDict valueForKey:@"CFBundleVersion"];
[versionURL appendString:[myVersion stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
NSDictionary *plist = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:versionURL]];
if (!plist) {
NSLog(@"Couldn't access version info");
NSLog(@"versionURL: %@", versionURL);
return;
}
NSDictionary *versionDict = (NSDictionary *)[plist valueForKey:ident];
NSString *currentVersion = (NSString *)[versionDict valueForKey:@"version"];
NSString *downloadURL = (NSString *)[versionDict valueForKey:@"url"];
int diff;
diff = [myVersion compare:currentVersion options:NSCaseInsensitiveSearch];
if (diff < 0) {
NSAlert *alert = [NSAlert alertWithMessageText:@"Version Info"
defaultButton:@"Ignore"
alternateButton:(downloadURL) ? @"Download" : @""
otherButton:nil
informativeTextWithFormat:[NSString stringWithFormat:@"A newer version of this application is available (%@)", currentVersion]];
int result = [alert runModal];
if (result == NSAlertAlternateReturn) {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:downloadURL]];
} else {
NSBeep();
}
} else if(diff == 0) {
NSAlert *alert = [NSAlert alertWithMessageText:@"Version Info"
defaultButton:@"Hooray!"
alternateButton:nil
otherButton:nil
informativeTextWithFormat:[NSString stringWithFormat:@"You have the latest version available (%@)", currentVersion]];
[alert runModal];
} else {
NSAlert *alert = [NSAlert alertWithMessageText:@"Version Info"
defaultButton:@"Ignore"
alternateButton:(downloadURL) ? @"Download" : @""
otherButton:nil
informativeTextWithFormat:[NSString stringWithFormat:@"You are running %@.\nThe latest public release is %@.", myVersion, currentVersion]];
int result = [alert runModal];
if (result == NSAlertAlternateReturn) {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:downloadURL]];
} else {
NSBeep();
}
}
NSLog([NSString stringWithFormat:@"ID: %@\nLatest: %@\nYours: %@\n", ident, currentVersion, myVersion]);
}
@end