]> git.saurik.com Git - uikittools.git/blob - iomfsetgamma.c
Port to Xcode 4.6.3: newer versions of Xcode fail.
[uikittools.git] / iomfsetgamma.c
1 /* UIKit Tools - command-line utilities for UIKit
2 * Copyright (C) 2008-2012 Jay Freeman (saurik)
3 */
4
5 /* Modified BSD License {{{ */
6 /*
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the
12 * above copyright notice, this list of conditions
13 * and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the
15 * above copyright notice, this list of conditions
16 * and the following disclaimer in the documentation
17 * and/or other materials provided with the
18 * distribution.
19 * 3. The name of the author may not be used to endorse
20 * or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
25 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38 /* }}} */
39
40 #include <mach/mach.h>
41
42 #include <stdio.h>
43 #include <stdlib.h>
44
45 #include <dlfcn.h>
46
47 #include <IOKit/IOKitLib.h>
48
49 #if 0
50 set -e
51
52 sdk=/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk
53
54 /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/g++ \
55 -Wall -fmessage-length=0 \
56 -arch armv6 -miphoneos-version-min=2.0 \
57 -isysroot "${sdk}" -I. -F"${sdk}"/System/Library/PrivateFrameworks \
58 -framework IOKit -framework IOMobileFramebuffer \
59 -o iomfsetgamma iomfsetgamma.c
60
61 ldid iomfsetgamma
62
63 exit 0
64 #endif
65
66 typedef void *IOMobileFramebufferRef;
67
68 kern_return_t IOMobileFramebufferOpen(io_service_t, mach_port_t, void *, IOMobileFramebufferRef *);
69 kern_return_t IOMobileFramebufferSetGammaTable(IOMobileFramebufferRef, void *);
70 kern_return_t (*$IOMobileFramebufferGetGammaTable)(IOMobileFramebufferRef, void *);
71
72 #define _assert(test) \
73 if (!(test)) { \
74 fprintf(stderr, "_assert(%s)\n", #test); \
75 exit(-1); \
76 }
77
78 int main(int argc, char *argv[]) {
79 if (argc != 4) {
80 fprintf(stderr, "usage: iomfsetgamma <red> <green> <blue>\n");
81 fprintf(stderr, " example: 1.00 0.78 0.64\n");
82 return 1;
83 }
84
85 unsigned rs = strtod(argv[1], NULL) * 0x100;
86 _assert(rs <= 0x100);
87
88 unsigned gs = strtod(argv[2], NULL) * 0x100;
89 _assert(gs <= 0x100);
90
91 unsigned bs = strtod(argv[3], NULL) * 0x100;
92 _assert(bs <= 0x100);
93
94 kern_return_t error;
95 mach_port_t self = mach_task_self();
96
97 io_service_t service = 0;
98
99 if (service == 0)
100 service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleCLCD"));
101 if (service == 0)
102 service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleH1CLCD"));
103 if (service == 0)
104 service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleM2CLCD"));
105
106 _assert(service != 0);
107
108 IOMobileFramebufferRef fb;
109 error = IOMobileFramebufferOpen(service, self, 0, &fb);
110 _assert(error == 0);
111
112 uint32_t data[0xc00 / sizeof(uint32_t)];
113 memset(data, 0, sizeof(data));
114
115 FILE *file = fopen("/tmp/.iomfgamma.dat", "rb");
116 if (file == NULL) {
117 $IOMobileFramebufferGetGammaTable = dlsym(RTLD_DEFAULT, "IOMobileFramebufferGetGammaTable");
118 _assert($IOMobileFramebufferGetGammaTable != NULL);
119 error = $IOMobileFramebufferGetGammaTable(fb, data);
120 _assert(error == 0);
121
122 file = fopen("/tmp/.iomfgamma.dat", "wb");
123 _assert(file != NULL);
124
125 fwrite(data, 1, sizeof(data), file);
126 fclose(file);
127
128 file = fopen("/tmp/.iomfgamma.dat", "rb");
129 _assert(file != NULL);
130 }
131
132 fread(data, 1, sizeof(data), file);
133 fclose(file);
134
135 size_t i;
136 for (i = 0; i != 256; ++i) {
137 int j = 255 - i;
138
139 int r = j * rs >> 8;
140 int g = j * gs >> 8;
141 int b = j * bs >> 8;
142
143 data[j + 0x000] = data[r + 0x000];
144 data[j + 0x100] = data[g + 0x100];
145 data[j + 0x200] = data[b + 0x200];
146 }
147
148 error = IOMobileFramebufferSetGammaTable(fb, data);
149 _assert(error == 0);
150
151 return 0;
152 }