Tizen 2.0 Release
[framework/uifw/xorg/util/x11-xserver-utils.git] / xgamma / xgamma.c
1 /*
2  * Copyright 1999  The XFree86 Project
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a 
5  * copy of this software and associated documentation files (the "Software"), 
6  * to deal in the Software without restriction, including without limitation 
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
8  * and/or sell copies of the Software, and to permit persons to whom the 
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  * 
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL 
17  * THE XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
18  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF 
19  * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
20  * SOFTWARE.
21  * 
22  * Written by David Bateman
23  */
24
25 #include <stdio.h>
26 #include <errno.h>
27 #include <X11/Xos.h>
28 #include <X11/Xlib.h>
29 #include <X11/Xutil.h>
30 #include <X11/extensions/xf86vmode.h>
31 #include <ctype.h>
32 #include <stdlib.h>
33
34 static char *ProgramName;
35 static int MajorVersion, MinorVersion;
36 static int EventBase, ErrorBase;
37
38 /* Minimum extension version required */
39 #define MINMAJOR 2
40 #define MINMINOR 0
41
42 /* Maximum and Minimum gamma values */
43 #define GAMMA_MIN 0.1
44 #define GAMMA_MAX 10.0
45
46 static void 
47 Syntax(void)
48 {
49     fprintf (stderr, "usage:  %s [-options]\n\n", 
50              ProgramName);
51     fprintf (stderr, "where the available options are:\n");
52     fprintf (stderr, "    -display host:dpy       or -d\n");
53     fprintf (stderr, "    -quiet                  or -q\n");
54     fprintf (stderr, "    -screen                 or -s\n");
55     fprintf (stderr, "    -gamma f.f              Gamma Value\n");
56     fprintf (stderr, "    -rgamma f.f             Red Gamma Value\n");
57     fprintf (stderr, "    -ggamma f.f             Green Gamma Value\n");
58     fprintf (stderr, "    -bgamma f.f             Blue Gamma Value\n\n");
59     fprintf (stderr, "If no gamma is specified, returns the current setting\n");
60     exit (1);
61 }
62
63
64 /*
65  * The following is a hack until XrmParseCommand is ready.  It determines
66  * whether or not the given string is an abbreviation of the arg.
67  */
68
69 static Bool 
70 isabbreviation(char *arg, char *s, int minslen)
71 {
72     int arglen;
73     int slen;
74
75     /* exact match */
76     if (strcmp (arg, s) == 0) return (True);
77
78     arglen = strlen (arg);
79     slen = strlen (s);
80
81     /* too long or too short */
82     if (slen >= arglen || slen < minslen) return (False);
83
84     /* abbreviation */
85     if (strncmp (arg, s, slen) == 0) return (True);
86
87     /* bad */
88     return (False);
89 }
90
91 int
92 main(int argc, char *argv[])
93 {
94     int i, ret;
95     char *displayname = NULL;
96     Display *dpy;
97     float gam = -1., rgam = -1., ggam = -1., bgam = -1.;
98     XF86VidModeGamma gamma;
99     Bool quiet = False;
100     int screen = -1;
101
102     ProgramName = argv[0];
103     for (i = 1; i < argc; i++) {
104         char *arg = argv[i];
105
106         if (arg[0] == '-') {
107             if (isabbreviation ("-display", arg, 1)) {
108                 if (++i >= argc) Syntax ();
109                 displayname = argv[i];
110                 continue;
111             } else if (isabbreviation ("-quiet", arg, 1)) {
112                 quiet = True;
113                 continue;
114             } else if (isabbreviation ("-screen", arg, 1)) {
115                 if (++i >= argc) Syntax ();
116                 screen = atoi(argv[i]);
117                 continue;
118             } else if (isabbreviation ("-gamma", arg, 2)) {
119                 if (++i >= argc) Syntax ();
120                 if ((rgam >= 0.) || (ggam >= 0.) || (bgam >= 0.))
121                     Syntax ();
122                 gam = (float)atof(argv[i]);
123                 if ((gam < GAMMA_MIN) || (gam > GAMMA_MAX)) {
124                     fprintf(stderr,
125                             "Gamma values must be between %6.3f and %6.3f\n",
126                             GAMMA_MIN, GAMMA_MAX);
127                     exit(1);
128                 }
129                 continue;
130             } else if (isabbreviation ("-rgamma", arg, 2)) {
131                 if (++i >= argc) Syntax ();
132                 if (gam >= 0.) Syntax ();
133                 rgam = (float)atof(argv[i]);
134                 if ((rgam < GAMMA_MIN) || (rgam > GAMMA_MAX)) {
135                     fprintf(stderr,
136                             "Gamma values must be between %6.3f and %6.3f\n",
137                             GAMMA_MIN, GAMMA_MAX);
138                     exit(1);
139                 }
140                 continue;
141             } else if (isabbreviation ("-ggamma", arg, 2)) {
142                 if (++i >= argc) Syntax ();
143                 if (gam >= 0.) Syntax ();
144                 ggam = (float)atof(argv[i]);
145                 if ((ggam < GAMMA_MIN) || (ggam > GAMMA_MAX)) {
146                     fprintf(stderr,
147                             "Gamma values must be between %6.3f and %6.3f\n",
148                             GAMMA_MIN, GAMMA_MAX);
149                     exit(1);
150                 }
151                 continue;
152             } else if (isabbreviation ("-bgamma", arg, 2)) {
153                 if (++i >= argc) Syntax ();
154                 if (gam >= 0.) Syntax ();
155                 bgam = (float)atof(argv[i]);
156                 if ((bgam < GAMMA_MIN) || (bgam > GAMMA_MAX)) {
157                     fprintf(stderr,
158                             "Gamma values must be between %6.3f and %6.3f\n",
159                             GAMMA_MIN, GAMMA_MAX);
160                     exit(1);
161                 }
162                 continue;
163             } else 
164                 Syntax ();
165         } else 
166             Syntax ();
167     }
168
169     if ((dpy = XOpenDisplay(displayname)) == NULL) {
170         fprintf (stderr, "%s:  unable to open display '%s'\n",
171                  ProgramName, XDisplayName (displayname));
172         exit(1);
173     } else if (screen == -1)
174         screen = DefaultScreen(dpy);
175
176     if (!XF86VidModeQueryVersion(dpy, &MajorVersion, &MinorVersion)) {
177         fprintf(stderr, "Unable to query video extension version\n");
178         exit(2);
179     }
180
181     if (!XF86VidModeQueryExtension(dpy, &EventBase, &ErrorBase)) {
182         fprintf(stderr, "Unable to query video extension information\n");
183         exit(2);
184     }
185
186     /* Fail if the extension version in the server is too old */
187     if (MajorVersion < MINMAJOR || 
188         (MajorVersion == MINMAJOR && MinorVersion < MINMINOR)) {
189         fprintf(stderr,
190                 "Xserver is running an old XFree86-VidModeExtension version"
191                 " (%d.%d)\n", MajorVersion, MinorVersion);
192         fprintf(stderr, "Minimum required version is %d.%d\n",
193                 MINMAJOR, MINMINOR);
194         exit(2);
195     }
196
197     if (!XF86VidModeGetGamma(dpy, screen, &gamma)) {
198         fprintf(stderr, "Unable to query gamma correction\n");
199         XCloseDisplay (dpy);
200         exit (2);
201     } else if (!quiet)
202         fprintf(stderr, "-> Red %6.3f, Green %6.3f, Blue %6.3f\n", gamma.red,
203                 gamma.green, gamma.blue);
204
205     ret = 0;
206     if (gam >= 0.) {
207         gamma.red = gam;
208         gamma.green = gam;
209         gamma.blue = gam;
210         if (!XF86VidModeSetGamma(dpy, screen, &gamma)) {
211             fprintf(stderr, "Unable to set gamma correction\n");
212             ret = 2;
213         } else {
214             if (!XF86VidModeGetGamma(dpy, screen, &gamma)) {
215                 fprintf(stderr, "Unable to query gamma correction\n");
216                 ret = 2;
217             } else if (!quiet)
218                 fprintf(stderr, "<- Red %6.3f, Green %6.3f, Blue %6.3f\n",
219                         gamma.red, gamma.green, gamma.blue);
220         }
221     } else if ((rgam >= 0.) || (ggam >= 0.) || (bgam >= 0.)) {
222         if (rgam >= 0.) gamma.red = rgam;
223         if (ggam >= 0.) gamma.green = ggam;
224         if (bgam >= 0.) gamma.blue = bgam;
225         if (!XF86VidModeSetGamma(dpy, screen, &gamma)) {
226             fprintf(stderr, "Unable to set gamma correction\n");
227             ret = 2;
228         } else {
229             if (!XF86VidModeGetGamma(dpy, screen, &gamma)) {
230                 fprintf(stderr, "Unable to query gamma correction\n");
231                 ret = 2;
232             } else if (!quiet)
233                 fprintf(stderr, "<- Red %6.3f, Green %6.3f, Blue %6.3f\n",
234                         gamma.red, gamma.green, gamma.blue);
235         }
236     }
237
238     XCloseDisplay (dpy);
239     exit (ret);
240 }
241