-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathantipopd.c
More file actions
94 lines (78 loc) · 1.94 KB
/
antipopd.c
File metadata and controls
94 lines (78 loc) · 1.94 KB
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
#include <stdio.h>
#include <unistd.h>
#include <sys/cdefs.h>
#include <AudioToolbox/AudioServices.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/ps/IOPSKeys.h>
#include <IOKit/ps/IOPowerSources.h>
int IsNum(int c)
{
return c >= '0' && c <= '9';
}
typedef int (*FFPred)(int);
int FindFirstOf(FILE *file, FFPred pred)
{
if(!file || !pred)
{
return -1;
}
while(!feof(file))
{
int c = fgetc(file);
if(pred(c))
{
return c;
}
}
return -1;
}
int OnAC()
{
CFTypeRef blob = IOPSCopyPowerSourcesInfo();
CFArrayRef ps = IOPSCopyPowerSourcesList(blob);
CFDictionaryRef theDict = IOPSGetPowerSourceDescription(blob, CFArrayGetValueAtIndex(ps, 0));
CFStringRef isCharging = CFDictionaryGetValue(theDict, CFSTR(kIOPSPowerSourceStateKey));
int onAC = CFStringCompare(isCharging, CFSTR(kIOPSACPowerValue), 0) == 0;
CFRelease(ps);
CFRelease(blob);
return onAC;
}
int main()
{
/* Should be a single integer in the file:
* 1 = AC only
* !1 = AC or Battery
*/
FILE *ac_file = fopen("/usr/local/share/antipop/ac_only", "r");
int ac_only = 0;
if(ac_file)
{
ac_only = FindFirstOf(ac_file, IsNum) - '0';
fclose(ac_file);
}
SystemSoundID soundID;
OSStatus result = AudioServicesCreateSystemSoundID
( CFURLCreateWithFileSystemPath(NULL,
CFSTR("/Extra/share/antipop/silent.aiff"),
kCFURLPOSIXPathStyle, FALSE)
, &soundID );
if(result != noErr)
{
printf("AudioServicesCreateSystemSoundID failed: %d\n", result);
exit(1);
}
while(1)
{
if(!ac_only || OnAC())
{
AudioServicesPlaySystemSound(soundID);
}
sleep(10);
}
result = AudioServicesDisposeSystemSoundID(soundID);
if(result != noErr)
{
printf("AudioServicesDisposeSystemSoundID failed: %d\n", result);
exit(1);
}
}