1/*
2cc -g -o /tmp/setdparam setdparam.c -framework ApplicationServices -framework IOKit -Wall -arch i386
3*/
4
5#include <CoreFoundation/CoreFoundation.h>
6#include <ApplicationServices/ApplicationServices.h>
7#include <IOKit/graphics/IOGraphicsLib.h>
8#include <stdlib.h>
9#include <stdio.h>
10
11
12int main(int argc, char * argv[])
13{
14    io_service_t        service;
15    io_string_t         path;
16    CGError             err;
17    int                 i;
18    CGDisplayCount      max;
19    CGDirectDisplayID   displayIDs[8];
20    CFStringRef         key;
21    float               value;
22    SInt32              ivalue, imin, imax;
23
24    err = CGGetOnlineDisplayList(8, displayIDs, &max);
25    if(err != kCGErrorSuccess)
26        exit(1);
27    if(max > 8)
28        max = 8;
29
30    if( argc < 2)
31        key = CFSTR(kIODisplayBrightnessKey);
32    else
33        key = CFStringCreateWithCString( kCFAllocatorDefault, argv[1],
34                                        kCFStringEncodingMacRoman );
35
36
37    for(i = 0; i < max; i++, IOObjectRelease(service) )
38    {
39        service = CGDisplayIOServicePort(displayIDs[i]);
40        if(MACH_PORT_NULL == service)
41            continue;
42
43        err = IORegistryEntryGetPath(service, kIOServicePlane, path);
44        if( kIOReturnSuccess != err)
45        {
46            printf("IORegistryEntryGetPath(err 0x%x, %d)\n", err, service);
47            continue;
48        }
49        printf("framebuffer: %s\n", path);
50        service = IODisplayForFramebuffer(service, kNilOptions);
51        if(MACH_PORT_NULL == service)
52        {
53            printf("no display there\n", err, service);
54            continue;
55        }
56        err = IORegistryEntryGetPath(service, kIOServicePlane, path);
57        if( kIOReturnSuccess != err)
58        {
59            printf("IORegistryEntryGetPath(err 0x%x, %d)\n", err, service);
60            continue;
61        }
62        printf("display: %s\n", path);
63
64        err = IODisplayGetIntegerRangeParameter(service, kNilOptions, key,
65                                                &ivalue, &imin, &imax);
66        if( kIOReturnSuccess != err)
67            continue;
68
69        err = IODisplayGetFloatParameter(service, kNilOptions, key, &value);
70        printf("Display %x: %f == 0x%x / [0x%x - 0x%x]\n",
71                    displayIDs[i], value, (int) ivalue, (int) imin, (int) imax);
72        if( kIOReturnSuccess != err)
73            continue;
74
75        if (argc < 3)
76            continue;
77
78        if (strchr(argv[argc - 1], '.'))
79        {
80            sscanf( argv[argc - 1], "%f", &value );
81            err = IODisplaySetFloatParameter(service, kNilOptions, key, value);
82            printf("IODisplaySetFloatParameter(0x%x, %f)\n", err, value);
83        }
84        else
85        {
86            ivalue = strtol(argv[argc - 1], 0, 0);
87            err = IODisplaySetIntegerParameter(service, kNilOptions, key, ivalue);
88            printf("IODisplaySetIntegerParameter(0x%x, 0x%x)\n", err, (int) ivalue);
89        }
90    }
91
92    exit(0);
93    return(0);
94}
95
96