Create a gist now

Instantly share code, notes, and snippets.

/*
* Heavily based on https://bug793728.bugzilla.mozilla.org/attachment.cgi?id=664102
*
* clang -o ../holylight-checker holylight-checker.m -framework IOKit -framework CoreFoundation
*/
#include <mach/mach.h>
#import <IOKit/IOKitLib.h>
#import <CoreFoundation/CoreFoundation.h>
static io_connect_t dataPort = 0;
int main(void) {
kern_return_t kr;
io_service_t serviceObject;
serviceObject = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleLMUController"));
if (!serviceObject) {
fprintf(stderr, "Failed to find ambient light sensors.\n");
exit(1);
}
kr = IOServiceOpen(serviceObject, mach_task_self(), 0, &dataPort);
IOObjectRelease(serviceObject);
if (kr != KERN_SUCCESS) {
mach_error("IOServiceOpen:", kr);
exit(kr);
}
uint32_t outputs = 2;
uint64_t values[outputs];
kr = IOConnectCallMethod(dataPort, 0, nil, 0, nil, 0, values, &outputs, nil, 0);
if (kr == KERN_SUCCESS) {
printf("%lld\n", values[0]);
exit(0);
}
if (kr == kIOReturnBusy) exit(0);
mach_error("I/O Kit error: ", kr);
exit(kr);
exit(0);
}
/*
* Taken from https://bug793728.bugzilla.mozilla.org/attachment.cgi?id=664102
*
* clang -o ../realtime-checker realtime-checker.m -framework IOKit -framework CoreFoundation
*/
#include <mach/mach.h>
#import <IOKit/IOKitLib.h>
#import <CoreFoundation/CoreFoundation.h>
static double updateInterval = 0.1;
static io_connect_t dataPort = 0;
void updateTimerCallBack(CFRunLoopTimerRef timer, void *info) {
kern_return_t kr;
uint32_t outputs = 2;
uint64_t values[outputs];
kr = IOConnectCallMethod(dataPort, 0, nil, 0, nil, 0, values, &outputs, nil, 0);
if (kr == KERN_SUCCESS) {
printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b%lld", values[0]);
return;
}
if (kr == kIOReturnBusy) {
return;
}
mach_error("I/O Kit error:", kr);
exit(kr);
}
int main(void) {
kern_return_t kr;
io_service_t serviceObject;
CFRunLoopTimerRef updateTimer;
serviceObject = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleLMUController"));
if (!serviceObject) {
fprintf(stderr, "failed to find ambient light sensors\n");
exit(1);
}
kr = IOServiceOpen(serviceObject, mach_task_self(), 0, &dataPort);
IOObjectRelease(serviceObject);
if (kr != KERN_SUCCESS) {
mach_error("IOServiceOpen:", kr);
exit(kr);
}
setbuf(stdout, NULL);
printf("%ld", 0L);
updateTimer = CFRunLoopTimerCreate(kCFAllocatorDefault,
CFAbsoluteTimeGetCurrent() + updateInterval, updateInterval,
0, 0, updateTimerCallBack, NULL);
CFRunLoopAddTimer(CFRunLoopGetCurrent(), updateTimer, kCFRunLoopDefaultMode);
CFRunLoopRun();
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment