-
Notifications
You must be signed in to change notification settings - Fork 3
/
lmutracker.c
84 lines (55 loc) · 1.87 KB
/
lmutracker.c
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
// lmutracker.c
//
// gcc -o lmutracker lmutracker.c -framework IOKit -framework CoreFoundation
#include <mach/mach.h>
#include <IOKit/IOKitLib.h>
#include <CoreFoundation/CoreFoundation.h>
#include "lmucommon.h"
static double updateInterval = 0.1;
static io_connect_t dataPort = 0;
void updateTimerCallBack(CFRunLoopTimerRef timer, void *info)
{
kern_return_t kr;
IOItemCount scalarInputCount = 0;
IOItemCount scalarOutputCount = 2;
SInt32 left = 0, right = 0;
kr = IOConnectMethodScalarIScalarO(dataPort, kGetSensorReadingID,
scalarInputCount, scalarOutputCount, &left, &right);
if (kr == KERN_SUCCESS) {
printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b%8ld %8ld", left, right);
return;
}
if (kr == kIOReturnBusy)
return;
mach_error("I/O Kit error:", kr);
exit(kr);
}
int getLightMeterValues(void)
{
kern_return_t kr;
io_service_t serviceObject;
CFRunLoopTimerRef updateTimer;
// Look up a registered IOService object whose class is AppleLMUController
serviceObject = IOServiceGetMatchingService(kIOMasterPortDefault,
IOServiceMatching("AppleLMUController"));
if (!serviceObject) {
fprintf(stderr, "failed to find ambient light sensor\n");
exit(1);
}
// Create a connection to the IOService object
kr = IOServiceOpen(serviceObject, mach_task_self(), 0, &dataPort);
IOObjectRelease(serviceObject);
if (kr != KERN_SUCCESS) {
mach_error("IOServiceOpen:", kr);
exit(kr);
}
setbuf(stdout, NULL);
printf("%8ld %8ld", 0L, 0L);
// Set up the loop and let it run...
updateTimer = CFRunLoopTimerCreate(kCFAllocatorDefault,
CFAbsoluteTimeGetCurrent() + updateInterval,
updateInterval, 0, 0, updateTimerCallBack, NULL);
CFRunLoopAddTimer(CFRunLoopGetCurrent(), updateTimer, kCFRunLoopDefaultMode);
CFRunLoopRun();
exit(0);
}