-
Notifications
You must be signed in to change notification settings - Fork 2
/
RipHoodController.m
588 lines (459 loc) · 14.8 KB
/
RipHoodController.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
//
// RipHoodController.m
// Hood
//
// Created by Slava Karpenko on 11/23/08.
// Copyright 2008 Ripdev. All rights reserved.
//
#include <mach/mach.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#import "BluetoothManager.h"
#import "RipHoodController.h"
#import "PSSystemConfiguration.h"
#import "RipHoodProcessListController.h"
extern NSString* SBSCopyFrontmostApplicationDisplayIdentifier();
extern pid_t SBGetPIDForBundleIdentifier(const char* bundleIdentifier, pid_t* outPid);
extern int SBSProcessIDForDisplayIdentifier(NSString* bundleIdentifier, pid_t* outPid);
extern void CTRegistrationSetDataContextActive(int inContext, int inActive);
extern void WiFiDeviceClientSetPower(void* device, int powerState);
extern int WiFiDeviceClientGetPower(void* device);
extern void* WiFiManagerClientCreate(CFAllocatorRef allocator, void* null);
extern CFArrayRef WiFiManagerClientCopyDevices(void* manager);
#define kHoodAppID CFSTR("com.ripdev.hood")
#define kHoodEnableProcessList CFSTR("EnableProcessList")
#define kHoodButton1 CFSTR("Button1")
#define kHoodButton2 CFSTR("Button2")
#define kHoodButton3 CFSTR("Button3")
#define kHoodButton4 CFSTR("Button4")
struct RipHoodButtonActions {
CFStringRef actionTag;
NSString* sel;
NSString* selectedImageName;
NSString* imageName;
} gRipHoodButtonActions[] = {
{
CFSTR(""),
@"doNothing:",
nil,
nil
},
{
CFSTR("quit"),
@"doRespring:",
@"RipHoodRespringIcon.png",
@"RipHoodRespringIconOff.png",
},
{
CFSTR("wifi"),
@"doAirport:",
@"RipHoodAirportIcon.png",
@"RipHoodAirportIconOff.png",
},
{
CFSTR("bluetooth"),
@"doBluetooth:",
@"RipHoodBluetoothIcon.png",
@"RipHoodBluetoothIconOff.png",
},
{
CFSTR("edge"),
@"doEDGE:",
@"RipHoodEDGEIcon.png",
@"RipHoodEDGEIconOff.png",
},
{
NULL,
nil,
nil,
nil
}
};
static CFStringRef gRipHoodButtonDefaults[4] = {
CFSTR("wifi"),
CFSTR("bluetooth"),
CFSTR("edge"),
CFSTR("quit")
};
@implementation RipHoodController
@synthesize overlayView;
// Implement viewDidLoad to do additional setup after loading the view.
- (void)viewDidLoad {
mHost = mach_host_self();
if (host_page_size(mHost, &mPageSize) != KERN_SUCCESS)
mPageSize = 4096;
mTotalPages = 0;
size_t bufSize = sizeof(mTotalPages);
if (sysctlbyname("hw.physmem", &mTotalPages, &bufSize, NULL, 0) == 0)
{
mTotalPages /= mPageSize;
}
[BluetoothManager initialize];
mWiFiManagerClient = WiFiManagerClientCreate(kCFAllocatorDefault, NULL);
mPRViewOrigin = prView.frame.origin;
mAirportButtons = [[NSMutableArray arrayWithCapacity:0] retain];
mEDGEButtons = [[NSMutableArray arrayWithCapacity:0] retain];
mBluetoothButtons = [[NSMutableArray arrayWithCapacity:0] retain];
mQuitButtons = [[NSMutableArray arrayWithCapacity:0] retain];
mButtonsIndex = [[NSDictionary dictionaryWithObjectsAndKeys: mAirportButtons, @"wifi",
mEDGEButtons, @"edge",
mBluetoothButtons, @"bluetooth",
mQuitButtons, @"quit",
nil] retain];
// read in prefs and set up buttons
[self _setupFromPrefs];
[super viewDidLoad];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return YES;
// NSLog(@"shouldAutorotateToInterfaceOrientation: %d", interfaceOrientation);
// return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[(id)mWiFiManagerClient release];
[super dealloc];
}
- (IBAction)dismissController:(id)sender
{
[self dismissController:sender animated:YES];
}
- (void)dismissController:(id)sender animated:(BOOL)animated
{
if (animated)
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
CGRect r = self.view.frame;
r.origin.y = -r.size.height;
self.view.frame = r;
self.overlayView.alpha = .0;
[UIView commitAnimations];
}
else
{
CGRect r = self.view.frame;
r.origin.y = -r.size.height;
self.view.frame = r;
self.overlayView.alpha = .0;
UIWindow* windowToDispose = self.view.window;
[self.view removeFromSuperview];
[self.overlayView removeFromSuperview];
windowToDispose.hidden = YES;
[windowToDispose release];
}
}
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
UIWindow* windowToDispose = self.view.window;
[self.view removeFromSuperview];
[self.overlayView removeFromSuperview];
windowToDispose.hidden = YES;
[windowToDispose release];
}
- (void)viewDidAppear:(BOOL)animated // Called when the view is about to made visible. Default does nothing
{
[self _updateMemoryPieChart];
[self _updateButtonStates];
}
#pragma mark -
- (void)killApplication
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSString* frontmostAppIdentifier = SBSCopyFrontmostApplicationDisplayIdentifier();
if (frontmostAppIdentifier != nil)
{
pid_t pid = 0;
if (SBSProcessIDForDisplayIdentifier(frontmostAppIdentifier, &pid))
{
NSLog(@"kill %d", pid);
kill(pid, SIGTERM);
}
else
{
Class SBUIControllerClass = NSClassFromString(@"SBUIController");
if (SBUIControllerClass)
{
id sharedController = objc_msgSend(SBUIControllerClass, NSSelectorFromString(@"sharedInstance"));
if ([sharedController respondsToSelector:NSSelectorFromString(@"quitTopApplication")])
{
objc_msgSend(sharedController, NSSelectorFromString(@"quitTopApplication"));
}
}
}
[frontmostAppIdentifier release];
}
else
{
[[UIApplication sharedApplication] performSelectorOnMainThread:@selector(relaunchSpringBoard) withObject:nil waitUntilDone:NO];
//objc_msgSend([UIApplication sharedApplication], @selector(relaunchSpringBoard));
}
[pool release];
}
- (IBAction)doRespring:(UIButton*)sender
{
[self performSelectorInBackground:@selector(killApplication) withObject:nil];
}
- (IBAction)doNothing:(UIButton*)sender
{
}
- (IBAction)doAirport:(UIButton*)sender
{
char newPower = !self.airportEnabled;
if (!mWiFiManagerClient)
return;
NSArray* devices = (NSArray*)WiFiManagerClientCopyDevices(mWiFiManagerClient);
if (devices && [devices count])
{
void* device = [devices objectAtIndex:0];
WiFiDeviceClientSetPower(device, (int)newPower);
}
[devices release];
for (UIButton* button in mAirportButtons)
button.selected = newPower;
}
- (IBAction)doBluetooth:(UIButton*)sender
{
BOOL enabled = !sender.selected;
[[BluetoothManager sharedInstance] setEnabled:enabled];
for (UIButton* button in mBluetoothButtons)
button.selected = enabled;
}
- (IBAction)doEDGE:(UIButton*)sender
{
BOOL enabled = !sender.selected;
self.edgeEnabled = enabled;
for (UIButton* button in mEDGEButtons)
button.selected = enabled;
}
#pragma mark -
- (void)updateLabel:(UILabel*)label withPages:(natural_t)pages name:(NSString*)name
{
label.text = [NSString stringWithFormat:@"%@: %@", name, [self byteSizeDescription:pages*mPageSize]];
}
- (NSString *)byteSizeDescription:(double)dBytes {
if(dBytes == 0) {
return @"0 bytes";
} else if(dBytes <= pow(2, 10)) {
return [NSString stringWithFormat:@"%0.0f bytes", dBytes];
} else if(dBytes <= pow(2, 20)) {
return [NSString stringWithFormat:@"%0.1f KB", dBytes / pow(1024, 1)];
} else if(dBytes <= pow(2, 30)) {
return [NSString stringWithFormat:@"%0.1f MB", dBytes / pow(1024, 2)];
} else if(dBytes <= pow(2, 40)) {
return [NSString stringWithFormat:@"%0.1f GB", dBytes / pow(1024, 3)];
} else {
return [NSString stringWithFormat:@"%0.1f TB", dBytes / pow(1024, 4)];
}
}
- (void)_updateMemoryPieChart
{
if (!self.view.superview || !pieView.alpha)
return;
[self _fetchVMStatistics];
[pieChart.slices removeAllObjects];
double total_pages = mVMStat.free_count + mVMStat.active_count + mVMStat.inactive_count + mVMStat.wire_count;
double free = (double)mVMStat.free_count / (double)total_pages;
double active = (double)mVMStat.active_count / (double)total_pages;
double inactive = (double)mVMStat.inactive_count / (double)total_pages;
double wired = (double)mVMStat.wire_count / (double)total_pages;
[pieChart.slices addObject:[RipPieChartSlice sliceWithPercent:free color:[UIColor greenColor]]];
[pieChart.slices addObject:[RipPieChartSlice sliceWithPercent:active color:[UIColor redColor]]];
[pieChart.slices addObject:[RipPieChartSlice sliceWithPercent:inactive color:[UIColor orangeColor]]];
[pieChart.slices addObject:[RipPieChartSlice sliceWithPercent:wired color:[UIColor blueColor]]];
[pieChart setNeedsDisplay];
[self updateLabel:freeLabel withPages:mVMStat.free_count name:@"Free"];
[self updateLabel:activeLabel withPages:mVMStat.active_count name:@"Active"];
[self updateLabel:inactiveLabel withPages:mVMStat.inactive_count name:@"Inactive"];
[self updateLabel:wiredLabel withPages:mVMStat.wire_count name:@"Wired"];
[self updateLabel:usedLabel withPages:(mTotalPages - mVMStat.free_count) name:@"Used"];
[self performSelector:@selector(_updateMemoryPieChart) withObject:nil afterDelay:0.5];
}
- (void)_fetchVMStatistics
{
unsigned int count = HOST_VM_INFO_COUNT;
if (host_statistics(mHost, HOST_VM_INFO, (host_info_t)&mVMStat, &count) != KERN_SUCCESS) {
bzero(&mVMStat, sizeof(mVMStat));
}
}
#pragma mark -
- (BOOL)airportEnabled
{
char power = NO;
if (!mWiFiManagerClient)
return NO;
NSArray* devices = (NSArray*)WiFiManagerClientCopyDevices(mWiFiManagerClient);
if (devices && [devices count])
{
void* device = [devices objectAtIndex:0];
power = WiFiDeviceClientGetPower(device);
}
[devices release];
return power;
}
- (BOOL)edgeEnabled
{
NSString* dataServiceID = [[PSSystemConfiguration sharedInstance] dataServiceID];
if (dataServiceID)
{
id val = [[PSSystemConfiguration sharedInstance] interfaceConfigurationValueForKey:@"Available" serviceID:dataServiceID];
return [val boolValue];
}
return NO;
}
- (void)setEdgeEnabled:(BOOL)e
{
if (self.edgeEnabled == e)
return;
NSNumber* enabled = [NSNumber numberWithInt:e?1:0];
[[PSSystemConfiguration sharedInstance] setInterfaceConfigurationValue:enabled forKey:@"Available" serviceID:[[PSSystemConfiguration sharedInstance] dataServiceID]];
CTRegistrationSetDataContextActive(0, e ? 1 : 0);
[self performSelector:@selector(_edgeEnabledChanged) withObject:nil afterDelay:.5];
}
- (void)_edgeEnabledChanged
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"SBDataConnectionTypeChangedNotification" object:nil userInfo:nil];
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("com.apple.springboard.edgeEnabledChanged"), NULL, NULL, TRUE);
}
#pragma mark -
- (IBAction)doSwitchToProcessList:(id)sender
{
BOOL prIsVisible = (prView.frame.origin.y != mPRViewOrigin.y);
CGRect r = prView.frame;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.2];
if (prIsVisible)
{
prController.shouldTrack = NO;
r.origin.y = mPRViewOrigin.y;
pieView.alpha = 1.;
[self performSelector:@selector(_updateMemoryPieChart) withObject:nil afterDelay:0.5];
}
else
{
prController.shouldTrack = YES;
r.origin.y = pieView.frame.origin.y;
pieView.alpha = .0;
}
[prController beginTracking];
prView.frame = r;
[UIView commitAnimations];
}
#pragma mark -
- (void)_setupFromPrefs
{
CFPreferencesAppSynchronize(kHoodAppID);
int i;
[mBluetoothButtons removeAllObjects];
[mAirportButtons removeAllObjects];
[mEDGEButtons removeAllObjects];
[mQuitButtons removeAllObjects];
for (i=1;i<=4;i++)
{
NSString* prefname = [NSString stringWithFormat:@"Button%d", i];
UIButton* button = nil;
if (1 == i)
button = button1;
else if (2 == i)
button = button2;
else if (3 == i)
button = button3;
else
button = button4;
CFStringRef value = (CFStringRef)CFPreferencesCopyAppValue((CFStringRef)prefname, kHoodAppID);
if (value)
{
[self _setupButton:button withID:value];
CFRelease(value);
}
else
[self _setupButton:button withID:gRipHoodButtonDefaults[i-1]];
}
Boolean keyExists = FALSE;
if (CFPreferencesGetAppBooleanValue(kHoodEnableProcessList, kHoodAppID, &keyExists) && keyExists)
{
prView.hidden = NO;
}
else
{
prView.hidden = YES;
// check if it was visible before
if ((prView.frame.origin.y != mPRViewOrigin.y))
{
[self doSwitchToProcessList:nil];
}
}
[self _updateButtonStates];
}
- (void)_setupButton:(UIButton*)button withID:(CFStringRef)ID
{
// find an appropriate match
struct RipHoodButtonActions* act = NULL;
int i;
for (i=0; gRipHoodButtonActions[i].actionTag; i++)
{
if ([(NSString*)gRipHoodButtonActions[i].actionTag isEqualToString:(NSString*)ID])
{
act = &gRipHoodButtonActions[i];
break;
}
}
// first, set up a dull button action
NSArray* actions = [button actionsForTarget:self forControlEvent:UIControlEventTouchDown];
for (NSString* sel in actions)
[button removeTarget:self action:NSSelectorFromString(sel) forControlEvents:UIControlEventTouchDown];
[button setImage:nil forState:UIControlStateNormal];
[button setImage:nil forState:UIControlStateHighlighted];
[button setImage:nil forState:UIControlStateSelected];
// now set up the actions, if any
if (act)
{
UIImage* normalImage = nil;
UIImage* selectedImage = nil;
if (act->imageName)
{
NSString* path = [[NSBundle bundleForClass:[self class]] pathForResource:[act->imageName stringByDeletingPathExtension] ofType:[act->imageName pathExtension]];
if (path)
normalImage = [UIImage imageWithContentsOfFile:path];
}
if (act->selectedImageName)
{
NSString* path = [[NSBundle bundleForClass:[self class]] pathForResource:[act->selectedImageName stringByDeletingPathExtension] ofType:[act->selectedImageName pathExtension]];
if (path)
selectedImage = [UIImage imageWithContentsOfFile:path];
}
[button setImage:normalImage forState:UIControlStateNormal];
[button setImage:selectedImage forState:UIControlStateHighlighted];
[button setImage:selectedImage forState:UIControlStateSelected];
if (act->sel)
{
SEL sel = NSSelectorFromString(act->sel);
if (sel)
{
[button addTarget:self action:sel forControlEvents:UIControlEventTouchDown];
}
}
[[mButtonsIndex objectForKey:(NSString*)act->actionTag] addObject:button];
}
}
- (void)_updateButtonStates
{
BOOL enabled = [[BluetoothManager sharedInstance] enabled];
for (UIButton* button in mBluetoothButtons)
button.selected = enabled;
enabled = self.airportEnabled;
for (UIButton* button in mAirportButtons)
button.selected = enabled;
enabled = self.edgeEnabled;
for (UIButton* button in mEDGEButtons)
button.selected = enabled;
for (UIButton* button in mQuitButtons)
button.selected = NO;
}
@end