This repository has been archived by the owner on Jan 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
AppController.m
416 lines (366 loc) · 16.7 KB
/
AppController.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
#import "AppController.h"
#include <IOKit/IOKitLib.h>
#include <IOKit/IOMessage.h>
#include <IOKit/pwr_mgt/IOPMLib.h>
#include "FireWireNotifier.h"
#include "USBNotifier.h"
#include "BluetoothNotifier.h"
#include "VolumeNotifier.h"
#include "NetworkNotifier.h"
#include "SyncNotifier.h"
#include "PowerNotifier.h"
#define NotifierUSBConnectionNotification @"USB Device Connected"
#define NotifierUSBDisconnectionNotification @"USB Device Disconnected"
#define NotifierVolumeMountedNotification @"Volume Mounted"
#define NotifierVolumeUnmountedNotification @"Volume Unmounted"
#define NotifierBluetoothConnectionNotification @"Bluetooth Device Connected"
#define NotifierBluetoothDisconnectionNotification @"Bluetooth Device Disconnected"
#define NotifierFireWireConnectionNotification @"FireWire Device Connected"
#define NotifierFireWireDisconnectionNotification @"FireWire Device Disconnected"
#define NotifierNetworkLinkUpNotification @"Network Link Up"
#define NotifierNetworkLinkDownNotification @"Network Link Down"
#define NotifierNetworkIpAcquiredNotification @"IP Acquired"
#define NotifierNetworkIpReleasedNotification @"IP Released"
#define NotifierNetworkAirportConnectNotification @"AirPort Connected"
#define NotifierNetworkAirportDisconnectNotification @"AirPort Disconnected"
#define NotifierSyncStartedNotification @"Sync started"
#define NotifierSyncFinishedNotification @"Sync finished"
#define NotifierPowerOnACNotification @"Switched to A/C Power"
#define NotifierPowerOnBatteryNotification @"Switched to Battery Power"
#define NotifierPowerOnUPSNotification @"Switched to UPS Power"
#define NotifierUSBConnectionHumanReadableDescription NSLocalizedString(@"USB Device Connected", "")
#define NotifierUSBDisconnectionHumanReadableDescription NSLocalizedString(@"USB Device Disconnected", "")
#define NotifierVolumeMountedHumanReadableDescription NSLocalizedString(@"Volume Mounted", "")
#define NotifierVolumeUnmountedHumanReadableDescription NSLocalizedString(@"Volume Unmounted", "")
#define NotifierBluetoothConnectionHumanReadableDescription NSLocalizedString(@"Bluetooth Device Connected", "")
#define NotifierBluetoothDisconnectionHumanReadableDescription NSLocalizedString(@"Bluetooth Device Disconnected", "")
#define NotifierFireWireConnectionHumanReadableDescription NSLocalizedString(@"FireWire Device Connected", "")
#define NotifierFireWireDisconnectionHumanReadableDescription NSLocalizedString(@"FireWire Device Disconnected", "")
#define NotifierNetworkLinkUpHumanReadableDescription NSLocalizedString(@"Network Link Up", "")
#define NotifierNetworkLinkDownHumanReadableDescription NSLocalizedString(@"Network Link Down", "")
#define NotifierNetworkIpAcquiredHumanReadableDescription NSLocalizedString(@"IP Acquired", "")
#define NotifierNetworkIpReleasedHumanReadableDescription NSLocalizedString(@"IP Released", "")
#define NotifierNetworkAirportConnectHumanReadableDescription NSLocalizedString(@"AirPort Connected", "")
#define NotifierNetworkAirportDisconnectHumanReadableDescription NSLocalizedString(@"AirPort Disconnected", "")
#define NotifierSyncStartedHumanReadableDescription NSLocalizedString(@"Sync started", "")
#define NotifierSyncFinishedHumanReadableDescription NSLocalizedString(@"Sync finished", "")
#define NotifierPowerOnACHumanReadableDescription NSLocalizedString(@"Switched to A/C Power", "")
#define NotifierPowerOnBatteryHumanReadableDescription NSLocalizedString(@"Switched to Battery Power", "")
#define NotifierPowerOnUPSHumanReadableDescription NSLocalizedString(@"Switched to UPS Power", "")
#define NotifierFireWireConnectionTitle() CFCopyLocalizedString(CFSTR("FireWire Connection"), "")
#define NotifierFireWireDisconnectionTitle() CFCopyLocalizedString(CFSTR("FireWire Disconnection"), "")
#define NotifierUSBConnectionTitle() CFCopyLocalizedString(CFSTR("USB Connection"), "")
#define NotifierUSBDisconnectionTitle() CFCopyLocalizedString(CFSTR("USB Disconnection"), "")
#define NotifierBluetoothConnectionTitle() CFCopyLocalizedString(CFSTR("Bluetooth Connection"), "")
#define NotifierBluetoothDisconnectionTitle() CFCopyLocalizedString(CFSTR("Bluetooth Disconnection"), "")
#define NotifierVolumeMountedTitle() CFCopyLocalizedString(CFSTR("Volume Mounted"), "")
#define NotifierVolumeUnmountedTitle() CFCopyLocalizedString(CFSTR("Volume Unmounted"), "")
#define NotifierNetworkAirportConnectTitle() CFCopyLocalizedString(CFSTR("Airport connected"), "")
#define NotifierNetworkAirportDisconnectTitle() CFCopyLocalizedString(CFSTR("Airport disconnected"), "")
#define NotifierNetworkLinkUpTitle() CFCopyLocalizedString(CFSTR("Ethernet activated"), "")
#define NotifierNetworkLinkDownTitle() CFCopyLocalizedString(CFSTR("Ethernet deactivated"), "")
#define NotifierNetworkIpAcquiredTitle() CFCopyLocalizedString(CFSTR("IP address acquired"), "")
#define NotifierNetworkIpReleasedTitle() CFCopyLocalizedString(CFSTR("IP address released"), "")
#define NotifierSyncStartedTitle() CFCopyLocalizedString(CFSTR("Sync started"), "")
#define NotifierSyncFinishedTitle() CFCopyLocalizedString(CFSTR("Sync finished"), "")
#define NotifierNetworkAirportDisconnectDescription() CFCopyLocalizedString(CFSTR("Left network %@."), "")
#define NotifierNetworkIpAcquiredDescription() CFCopyLocalizedString(CFSTR("New primary IP: %@ (%@)"), "")
#define NotifierNetworkIpReleasedDescription() CFCopyLocalizedString(CFSTR("No IP address now"), "")
static io_connect_t powerConnection;
static io_object_t powerNotifier;
static CFRunLoopSourceRef powerRunLoopSource;
static BOOL sleeping;
#ifdef __OBJC__
# define DATA_TYPE NSData *
# define DATE_TYPE NSDate *
# define DICTIONARY_TYPE NSDictionary *
# define MUTABLE_DICTIONARY_TYPE NSMutableDictionary *
# define STRING_TYPE NSString *
# define ARRAY_TYPE NSArray *
# define URL_TYPE NSURL *
# define PLIST_TYPE NSObject *
# define OBJECT_TYPE id
# define BOOL_TYPE BOOL
#else
# include <CoreFoundation/CoreFoundation.h>
# define DATA_TYPE CFDataRef
# define DATE_TYPE CFDateRef
# define DICTIONARY_TYPE CFDictionaryRef
# define MUTABLE_DICTIONARY_TYPE CFMutableDictionaryRef
# define STRING_TYPE CFStringRef
# define ARRAY_TYPE CFArrayRef
# define URL_TYPE CFURLRef
# define PLIST_TYPE CFPropertyListRef
# define OBJECT_TYPE CFTypeRef
# define BOOL_TYPE Boolean
#endif
NSUserNotificationCenter *center;
static void sendNotification(NSString *title, NSString *text) {
NSUserNotification *notification = [[NSUserNotification alloc] init];
[notification setTitle:title];
[notification setInformativeText:text];
[notification setDeliveryDate:[NSDate dateWithTimeInterval:1 sinceDate:[NSDate date]]];
[notification setSoundName:NSUserNotificationDefaultSoundName];
[notification setHasActionButton:false];
[center scheduleNotification:notification];
[notification release];
}
#pragma mark Firewire
void AppController_fwDidConnect(CFStringRef deviceName) {
NSLog(@"FireWire Connect: %@", deviceName);
CFStringRef title = NotifierFireWireConnectionTitle();
sendNotification((NSString *)title, (NSString *)deviceName);
CFRelease(title);
}
void AppController_fwDidDisconnect(CFStringRef deviceName) {
NSLog(@"FireWire Disconnect: %@", deviceName);
CFStringRef title = NotifierFireWireDisconnectionTitle();
sendNotification((NSString *)title, (NSString *)deviceName);
CFRelease(title);
}
#pragma mark USB
void AppController_usbDidConnect(CFStringRef deviceName) {
NSLog(@"USB Connect: %@", deviceName);
CFStringRef title = NotifierUSBConnectionTitle();
sendNotification((NSString *)title, (NSString *)deviceName);
CFRelease(title);
}
void AppController_usbDidDisconnect(CFStringRef deviceName) {
NSLog(@"USB Disconnect: %@", deviceName);
CFStringRef title = NotifierUSBDisconnectionTitle();
sendNotification((NSString *)title, (NSString *)deviceName);
CFRelease(title);
}
#pragma mark Bluetooth
void AppController_bluetoothDidConnect(CFStringRef device) {
NSLog(@"Bluetooth Connect: %@", device);
CFStringRef title = NotifierBluetoothConnectionTitle();
sendNotification((NSString *)title, (NSString *)device);
CFRelease(title);
}
void AppController_bluetoothDidDisconnect(CFStringRef device) {
NSLog(@"Bluetooth Disconnect: %@", device);
CFStringRef title = NotifierBluetoothDisconnectionTitle();
sendNotification((NSString *)title, (NSString *)device);
CFRelease(title);
}
#pragma mark Volumes
void AppController_volumeDidMount(VolumeInfo *info) {
NSLog(@"volume Mount: %@", info);
CFStringRef title = NotifierVolumeMountedTitle();
NSDictionary *context = nil;
if ([info path]) {
context = [NSDictionary dictionaryWithObjectsAndKeys:
(NSString *)NotifierVolumeMountedNotification, @"notification",
[info path], @"path",
nil];
}
sendNotification((NSString *)title, (NSString *)[info name]);
CFRelease(title);
}
void AppController_volumeDidUnmount(VolumeInfo *info) {
NSLog(@"volume Unmount: %@", info);
CFStringRef title = NotifierVolumeUnmountedTitle();
sendNotification((NSString *)title, (NSString *)[info name]);
CFRelease(title);
}
#pragma mark Network
void AppController_airportConnect(CFStringRef networkName, const unsigned char *bssidBytes) {
if (sleeping)
return;
CFStringRef title = NotifierNetworkAirportConnectTitle();
NSString *bssid = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X",
bssidBytes[0],
bssidBytes[1],
bssidBytes[2],
bssidBytes[3],
bssidBytes[4],
bssidBytes[5]];
NSString *description = [NSString stringWithFormat:NSLocalizedString(@"Joined network.\nSSID:\t\t%@\nBSSID:\t%@", ""),
networkName,
bssid];
NSLog(@"AirPort connect: %@", description);
sendNotification((NSString *)title, (NSString *)description);
CFRelease(title);
}
void AppController_airportDisconnect(CFStringRef networkName) {
if (sleeping)
return;
CFStringRef title = NotifierNetworkAirportDisconnectTitle();
CFStringRef format = NotifierNetworkAirportDisconnectDescription();
CFStringRef description = CFStringCreateWithFormat(kCFAllocatorDefault,
NULL,
format,
networkName);
NSLog(@"AirPort disconnect: %@", description);
CFRelease(format);
sendNotification((NSString *)title, (NSString *)description);
CFRelease(title);
CFRelease(description);
}
void AppController_linkUp(CFStringRef description) {
NSLog(@"Link up: %@", description);
if (sleeping)
return;
CFStringRef title = NotifierNetworkLinkUpTitle();
sendNotification((NSString *)title, (NSString *)description);
CFRelease(title);
}
void AppController_linkDown(CFStringRef description) {
NSLog(@"Link down: %@", description);
if (sleeping)
return;
CFStringRef title = NotifierNetworkLinkDownTitle();
sendNotification((NSString *)title, (NSString *)description);
CFRelease(title);
}
void AppController_ipAcquired(CFStringRef ip, CFStringRef type) {
NSLog(@"IP acquired: %@", ip);
if (sleeping)
return;
CFStringRef title = NotifierNetworkIpAcquiredTitle();
CFStringRef format = NotifierNetworkIpAcquiredDescription();
CFStringRef description = CFStringCreateWithFormat(kCFAllocatorDefault,
NULL,
format,
ip,
type);
CFRelease(format);
sendNotification((NSString *)title, (NSString *)description);
CFRelease(title);
CFRelease(description);
}
void AppController_ipReleased(void) {
NSLog(@"IP released");
if (sleeping)
return;
CFStringRef title = NotifierNetworkIpReleasedTitle();
CFStringRef description = NotifierNetworkIpReleasedDescription();
sendNotification((NSString *)title, (NSString *)description);
CFRelease(title);
CFRelease(description);
}
#pragma mark Sync
void AppController_syncStarted(void) {
NSLog(@"Sync started");
CFStringRef title = NotifierSyncStartedTitle();
sendNotification((NSString *)title, (NSString *)title);
CFRelease(title);
}
void AppController_syncFinished(void) {
NSLog(@"Sync finished");
CFStringRef title = NotifierSyncFinishedTitle();
sendNotification((NSString *)title, (NSString *)title);
CFRelease(title);
}
#pragma mark Power
void AppController_powerSwitched(HGPowerSource powerSource, CFBooleanRef isCharging,
CFIndex batteryTime, CFIndex batteryPercentage)
{
NSString *title = nil;
NSMutableString *description = [NSMutableString string];
NSString *notificationName = nil;
BOOL haveBatteryTime = (batteryTime != -1);
BOOL haveBatteryPercentage = (batteryPercentage != -1);
if (powerSource == HGACPower) {
title = NSLocalizedString(@"On A/C power", nil);
if (isCharging == kCFBooleanTrue) {
[description appendString:NSLocalizedString(@"Battery charging...", nil)];
if (haveBatteryTime || haveBatteryPercentage) [description appendString:@"\n"];
if (haveBatteryTime) [description appendFormat:NSLocalizedString(@"Time to charge: %i minutes", nil), batteryTime];
if (haveBatteryTime && haveBatteryPercentage) [description appendString:@"\n"];
if (haveBatteryPercentage) [description appendFormat:NSLocalizedString(@"Current charge: %d%%", nil), batteryPercentage];
} else {
}
notificationName = (NSString *)NotifierPowerOnACNotification;
} else if (powerSource == HGBatteryPower) {
title = NSLocalizedString(@"On battery power", nil);
if (haveBatteryTime) [description appendFormat:NSLocalizedString(@"Time remaining: %i minutes", nil), batteryTime];
if (haveBatteryTime && haveBatteryPercentage) [description appendString:@"\n"];
if (haveBatteryPercentage) [description appendFormat:NSLocalizedString(@"Current charge: %d%%", nil), batteryPercentage];
notificationName = (NSString *)NotifierPowerOnBatteryNotification;
} else if (powerSource == HGUPSPower) {
title = NSLocalizedString(@"On UPS power", nil);
notificationName = (NSString *)NotifierPowerOnUPSNotification;
}
if (notificationName) {
sendNotification((NSString *)title, (NSString *)description);
}
}
static void powerCallback(void *refcon, io_service_t service, natural_t messageType, void *messageArgument) {
#pragma unused(refcon,service)
switch (messageType) {
case kIOMessageSystemWillRestart:
case kIOMessageSystemWillPowerOff:
case kIOMessageSystemWillSleep:
case kIOMessageDeviceWillPowerOff:
sleeping = YES;
IOAllowPowerChange(powerConnection, (long)messageArgument);
break;
case kIOMessageCanSystemPowerOff:
case kIOMessageCanSystemSleep:
case kIOMessageCanDevicePowerOff:
IOAllowPowerChange(powerConnection, (long)messageArgument);
break;
case kIOMessageSystemWillNotSleep:
case kIOMessageSystemWillNotPowerOff:
case kIOMessageSystemHasPoweredOn:
case kIOMessageDeviceWillNotPowerOff:
case kIOMessageDeviceHasPoweredOn:
sleeping = NO;
default:
break;
}
}
@implementation AppController
- (void) userNotificationCenter: (NSUserNotificationCenter *) center didActivateNotification: (NSUserNotification *) notification
{
NSLog(@"Notficiation clicked");
CFStringRef title = (CFStringRef)notification.title;
CFStringRef informativeText = (CFStringRef)notification.informativeText;
[center removeDeliveredNotification:notification];
CFOptionFlags options = kCFUserNotificationNoteAlertLevel;
CFOptionFlags responseFlags = 0;
CFUserNotificationDisplayAlert(0, options, NULL, NULL, NULL,
title,
informativeText, NULL,
NULL,NULL, &responseFlags);
}
- (void) awakeFromNib {
NSLog(@"Starting...");
center = [NSUserNotificationCenter defaultUserNotificationCenter];
[center setDelegate:self];
sendNotification(@"HardwareGrowler", @"Starting...");
// Register for sleep and wake notifications so we can suppress various notifications during sleep
IONotificationPortRef ioNotificationPort;
powerConnection = IORegisterForSystemPower(NULL, &ioNotificationPort, powerCallback, &powerNotifier);
if (powerConnection) {
powerRunLoopSource = IONotificationPortGetRunLoopSource(ioNotificationPort);
CFRunLoopAddSource(CFRunLoopGetCurrent(), powerRunLoopSource, kCFRunLoopDefaultMode);
}
FireWireNotifier_init();
USBNotifier_init();
VolumeNotifier_init();
SyncNotifier_init();
BluetoothNotifier_init();
networkNotifier = [[NetworkNotifier alloc] init];
PowerNotifier_init();
}
- (void) dealloc {
FireWireNotifier_dealloc();
USBNotifier_dealloc();
VolumeNotifier_dealloc();
SyncNotifier_dealloc();
BluetoothNotifier_dealloc();
[networkNotifier release];
if (powerConnection) {
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), powerRunLoopSource, kCFRunLoopDefaultMode);
IODeregisterForSystemPower(&powerNotifier);
}
[super dealloc];
}
- (IBAction) doSimpleHelp:(id)sender {
#pragma unused(sender)
[[NSWorkspace sharedWorkspace] openFile:[[NSBundle mainBundle] pathForResource:@"readme" ofType:@"txt"]];
}
@end