]> git.saurik.com Git - uikittools.git/blob - iomfsetgamma.c
Reorganize calls to fopen, for assert safety.
[uikittools.git] / iomfsetgamma.c
1 #include <mach/mach.h>
2
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 #include <dlfcn.h>
7
8 #include <IOKit/IOKitLib.h>
9
10 #if 0
11 set -e
12
13 sdk=/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk
14
15 /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/g++ \
16 -Wall -fmessage-length=0 \
17 -arch armv6 -miphoneos-version-min=2.0 \
18 -isysroot "${sdk}" -I. -F"${sdk}"/System/Library/PrivateFrameworks \
19 -framework IOKit -framework IOMobileFramebuffer \
20 -o iomfsetgamma iomfsetgamma.c
21
22 ldid iomfsetgamma
23
24 exit 0
25 #endif
26
27 typedef void *IOMobileFramebufferRef;
28
29 kern_return_t IOMobileFramebufferOpen(io_service_t, mach_port_t, void *, IOMobileFramebufferRef *);
30 kern_return_t IOMobileFramebufferSetGammaTable(IOMobileFramebufferRef, void *);
31 kern_return_t (*$IOMobileFramebufferGetGammaTable)(IOMobileFramebufferRef, void *);
32
33 #define _assert(test) \
34 if (!(test)) { \
35 fprintf(stderr, "_assert(%s)\n", #test); \
36 exit(-1); \
37 }
38
39 int main(int argc, char *argv[]) {
40 if (argc != 4) {
41 fprintf(stderr, "usage: iomfsetgamma <red> <green> <blue>\n");
42 fprintf(stderr, " example: 1.00 0.78 0.64\n");
43 return 1;
44 }
45
46 unsigned rs = strtod(argv[1], NULL) * 0x100;
47 _assert(rs <= 0x100);
48
49 unsigned gs = strtod(argv[2], NULL) * 0x100;
50 _assert(gs <= 0x100);
51
52 unsigned bs = strtod(argv[3], NULL) * 0x100;
53 _assert(bs <= 0x100);
54
55 kern_return_t error;
56 mach_port_t self = mach_task_self();
57
58 io_service_t service = 0;
59
60 if (service == 0)
61 service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleCLCD"));
62 if (service == 0)
63 service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleH1CLCD"));
64 if (service == 0)
65 service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleM2CLCD"));
66
67 _assert(service != 0);
68
69 IOMobileFramebufferRef fb;
70 error = IOMobileFramebufferOpen(service, self, 0, &fb);
71 _assert(error == 0);
72
73 uint32_t data[0xc00 / sizeof(uint32_t)];
74 memset(data, 0, sizeof(data));
75
76 FILE *file = fopen("/tmp/.iomfgamma.dat", "r");
77 if (file == NULL) {
78 $IOMobileFramebufferGetGammaTable = dlsym(RTLD_DEFAULT, "IOMobileFramebufferGetGammaTable");
79 _assert($IOMobileFramebufferGetGammaTable != NULL);
80 error = $IOMobileFramebufferGetGammaTable(fb, data);
81 _assert(error == 0);
82
83 file = fopen("/tmp/.iomfgamma.dat", "wb");
84 _assert(file != NULL);
85
86 fwrite(data, 1, sizeof(data), file);
87 fclose(file);
88
89 file = fopen("/tmp/.iomfgamma.dat", "r");
90 _assert(file != NULL);
91 }
92
93 fread(data, 1, sizeof(data), file);
94 fclose(file);
95
96 size_t i;
97 for (i = 0; i != 256; ++i) {
98 int j = 255 - i;
99
100 int r = j * rs >> 8;
101 int g = j * gs >> 8;
102 int b = j * bs >> 8;
103
104 data[j + 0x000] = data[r + 0x000];
105 data[j + 0x100] = data[g + 0x100];
106 data[j + 0x200] = data[b + 0x200];
107 }
108
109 error = IOMobileFramebufferSetGammaTable(fb, data);
110 _assert(error == 0);
111
112 return 0;
113 }