Skip to content

Instantly share code, notes, and snippets.

@oxnz
Created October 16, 2013 06:31
Show Gist options
  • Save oxnz/7003457 to your computer and use it in GitHub Desktop.
Save oxnz/7003457 to your computer and use it in GitHub Desktop.
LDFLAGS = -framework IOKit -framework ApplicationServices
test: test.m
#include <stdio.h>
#include <unistd.h>
#include <IOKit/graphics/IOGraphicsLib.h>
#include <ApplicationServices/ApplicationServices.h>
const int kMaxDisplays = 256;
int get_display_count(CGDirectDisplayID displays[]) {
CGDisplayErr err;
CGDisplayCount count;
err = CGGetActiveDisplayList(kMaxDisplays, displays, &count);
if (err != CGDisplayNoErr) {
return -1;
}
return count;
}
void print_displays(CGDirectDisplayID displays[], int count) {
printf("display count:%d\n", count);
for (CGDisplayCount i=0; i < count; ++i) {
CGDirectDisplayID disp = displays[i];
CFDictionaryRef origMode = CGDisplayCurrentMode(disp);
if (origMode == NULL)
printf("origMode == NULL\n");
printf("display[%d]:", i);
if (CGMainDisplayID() == disp)
printf("(main display)");
printf("ID 0x%x\n", (unsigned int)disp);
}
}
float get_display_brightness(CGDirectDisplayID display) {
float brightness = -1.0;
io_service_t service = CGDisplayIOServicePort(CGMainDisplayID());
CGDisplayErr err = IODisplayGetFloatParameter(service, kNilOptions,
CFSTR(kIODisplayBrightnessKey), &brightness);
if (err != kCGErrorSuccess) {
fprintf(stderr, "error while get display birghtness: %d\n", err);
}
else
printf("display 1 brightness: %f\n", brightness);
return 0;
}
int main(int argc, char *argv[]) {
CGDirectDisplayID displays[kMaxDisplays];
int count = get_display_count(displays);
if (count == -1) {
fprintf(stderr, "error occured while get display count\n");
return 1;
}
else
print_displays(displays, count);
get_display_brightness(displays[0]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment