1 /* |
|
2 * Copyright (C) 2010 Google Inc. All rights reserved. |
|
3 * |
|
4 * Redistribution and use in source and binary forms, with or without |
|
5 * modification, are permitted provided that the following conditions are |
|
6 * met: |
|
7 * |
|
8 * * Redistributions of source code must retain the above copyright |
|
9 * notice, this list of conditions and the following disclaimer. |
|
10 * * Redistributions in binary form must reproduce the above |
|
11 * copyright notice, this list of conditions and the following disclaimer |
|
12 * in the documentation and/or other materials provided with the |
|
13 * distribution. |
|
14 * * Neither the name of Google Inc. nor the names of its |
|
15 * contributors may be used to endorse or promote products derived from |
|
16 * this software without specific prior written permission. |
|
17 * |
|
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 */ |
|
30 |
|
31 #import <AppKit/AppKit.h> |
|
32 #include <signal.h> |
|
33 #include <stdio.h> |
|
34 #include <stdlib.h> |
|
35 |
|
36 // This is a simple helper app that changes the color sync profile to the |
|
37 // generic profile and back when done. This program is managed by the layout |
|
38 // test script, so it can do the job for multiple DumpRenderTree while they are |
|
39 // running layout tests. |
|
40 |
|
41 static CMProfileRef userColorProfile = 0; |
|
42 |
|
43 static void saveCurrentColorProfile() |
|
44 { |
|
45 CGDirectDisplayID displayID = CGMainDisplayID(); |
|
46 CMProfileRef previousProfile; |
|
47 CMError error = CMGetProfileByAVID((UInt32)displayID, &previousProfile); |
|
48 if (error) { |
|
49 NSLog(@"failed to get the current color profile, pixmaps won't match. " |
|
50 @"Error: %d", (int)error); |
|
51 } else { |
|
52 userColorProfile = previousProfile; |
|
53 } |
|
54 } |
|
55 |
|
56 static void installLayoutTestColorProfile() |
|
57 { |
|
58 // To make sure we get consistent colors (not dependent on the Main display), |
|
59 // we force the generic rgb color profile. This cases a change the user can |
|
60 // see. |
|
61 |
|
62 CGDirectDisplayID displayID = CGMainDisplayID(); |
|
63 NSColorSpace* genericSpace = [NSColorSpace genericRGBColorSpace]; |
|
64 CMProfileRef genericProfile = (CMProfileRef)[genericSpace colorSyncProfile]; |
|
65 CMError error = CMSetProfileByAVID((UInt32)displayID, genericProfile); |
|
66 if (error) { |
|
67 NSLog(@"failed install the generic color profile, pixmaps won't match. " |
|
68 @"Error: %d", (int)error); |
|
69 } |
|
70 } |
|
71 |
|
72 static void restoreUserColorProfile(void) |
|
73 { |
|
74 if (!userColorProfile) |
|
75 return; |
|
76 CGDirectDisplayID displayID = CGMainDisplayID(); |
|
77 CMError error = CMSetProfileByAVID((UInt32)displayID, userColorProfile); |
|
78 CMCloseProfile(userColorProfile); |
|
79 if (error) { |
|
80 NSLog(@"Failed to restore color profile, use System Preferences -> " |
|
81 @"Displays -> Color to reset. Error: %d", (int)error); |
|
82 } |
|
83 userColorProfile = 0; |
|
84 } |
|
85 |
|
86 static void simpleSignalHandler(int sig) |
|
87 { |
|
88 // Try to restore the color profile and try to go down cleanly |
|
89 restoreUserColorProfile(); |
|
90 exit(128 + sig); |
|
91 } |
|
92 |
|
93 int main(int argc, char* argv[]) |
|
94 { |
|
95 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; |
|
96 |
|
97 // Hooks the ways we might get told to clean up... |
|
98 signal(SIGINT, simpleSignalHandler); |
|
99 signal(SIGHUP, simpleSignalHandler); |
|
100 signal(SIGTERM, simpleSignalHandler); |
|
101 |
|
102 // Save off the current profile, and then install the layout test profile. |
|
103 saveCurrentColorProfile(); |
|
104 installLayoutTestColorProfile(); |
|
105 |
|
106 // Let the script know we're ready |
|
107 printf("ready\n"); |
|
108 fflush(stdout); |
|
109 |
|
110 // Wait for any key (or signal) |
|
111 getchar(); |
|
112 |
|
113 // Restore the profile |
|
114 restoreUserColorProfile(); |
|
115 |
|
116 [pool release]; |
|
117 return 0; |
|
118 } |
|