/
QSPowerSourceEvents.m
44 lines (38 loc) · 1.23 KB
/
QSPowerSourceEvents.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
//
// QSPowerSourceEvents.m
// QSEventTriggersElement
//
// Created by Rob McBroom on 2014/02/20.
//
//
#import "QSPowerSourceEvents.h"
#import <IOKit/ps/IOPowerSources.h>
void handlePowerSourceChange(void *context)
{
NSDictionary *powerEvents = @{
@"AC Power": @"QSPowerSourceACEvent",
@"Battery Power": @"QSPowerSourceBatteryEvent",
@"UPS Power": @"QSPowerSourceUPSEvent",
};
CFTypeRef powerInfo = IOPSCopyPowerSourcesInfo();
NSString *powerSource = (__bridge NSString*)IOPSGetProvidingPowerSourceType(powerInfo);
NSString *event = [powerEvents objectForKey:powerSource];
[[QSEventTriggerManager sharedInstance] handleTriggerEvent:event withObject:nil];
}
@implementation QSPowerSourceEvents
- (void)enableEventProvider
{
// at least one event needs power state information
rls = IOPSNotificationCreateRunLoopSource(handlePowerSourceChange, NULL);
CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
}
- (void)disableEventProvider
{
// test for NULL first, because Quicksilver tends to disable triggers over and over
if (rls) {
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
CFRelease(rls);
rls = NULL;
}
}
@end