xgamma.c revision dd77ae96
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/* $XFree86: xc/programs/xgamma/xgamma.c,v 1.3 1999/03/21 07:35:38 dawes Exp $ */ 25 26#include <stdio.h> 27#include <errno.h> 28#include <X11/Xos.h> 29#include <X11/Xlib.h> 30#include <X11/Xutil.h> 31#include <X11/extensions/xf86vmode.h> 32#include <ctype.h> 33#include <stdlib.h> 34 35static char *ProgramName; 36static int MajorVersion, MinorVersion; 37static int EventBase, ErrorBase; 38 39/* Minimum extension version required */ 40#define MINMAJOR 2 41#define MINMINOR 0 42 43/* Maximum and Minimum gamma values */ 44#define GAMMA_MIN 0.1 45#define GAMMA_MAX 10.0 46 47static void 48Syntax(void) 49{ 50 fprintf (stderr, "usage: %s [-options]\n\n", 51 ProgramName); 52 fprintf (stderr, "where the available options are:\n"); 53 fprintf (stderr, " -display host:dpy or -d\n"); 54 fprintf (stderr, " -quiet or -q\n"); 55 fprintf (stderr, " -screen or -s\n"); 56 fprintf (stderr, " -gamma f.f Gamma Value\n"); 57 fprintf (stderr, " -rgamma f.f Red Gamma Value\n"); 58 fprintf (stderr, " -ggamma f.f Green Gamma Value\n"); 59 fprintf (stderr, " -bgamma f.f Blue Gamma Value\n\n"); 60 fprintf (stderr, "If no gamma is specified, returns the current setting\n"); 61 exit (1); 62} 63 64 65/* 66 * The following is a hack until XrmParseCommand is ready. It determines 67 * whether or not the given string is an abbreviation of the arg. 68 */ 69 70static Bool 71isabbreviation(char *arg, char *s, int minslen) 72{ 73 int arglen; 74 int slen; 75 76 /* exact match */ 77 if (strcmp (arg, s) == 0) return (True); 78 79 arglen = strlen (arg); 80 slen = strlen (s); 81 82 /* too long or too short */ 83 if (slen >= arglen || slen < minslen) return (False); 84 85 /* abbreviation */ 86 if (strncmp (arg, s, slen) == 0) return (True); 87 88 /* bad */ 89 return (False); 90} 91 92int 93main(int argc, char *argv[]) 94{ 95 int i, ret; 96 char *displayname = NULL; 97 Display *dpy; 98 float gam = -1., rgam = -1., ggam = -1., bgam = -1.; 99 XF86VidModeGamma gamma; 100 Bool quiet = False; 101 int screen = -1; 102 103 ProgramName = argv[0]; 104 for (i = 1; i < argc; i++) { 105 char *arg = argv[i]; 106 107 if (arg[0] == '-') { 108 if (isabbreviation ("-display", arg, 1)) { 109 if (++i >= argc) Syntax (); 110 displayname = argv[i]; 111 continue; 112 } else if (isabbreviation ("-quiet", arg, 1)) { 113 quiet = True; 114 continue; 115 } else if (isabbreviation ("-screen", arg, 1)) { 116 if (++i >= argc) Syntax (); 117 screen = atoi(argv[i]); 118 continue; 119 } else if (isabbreviation ("-gamma", arg, 2)) { 120 if (++i >= argc) Syntax (); 121 if ((rgam >= 0.) || (ggam >= 0.) || (bgam >= 0.)) 122 Syntax (); 123 gam = (float)atof(argv[i]); 124 if ((gam < GAMMA_MIN) || (gam > GAMMA_MAX)) { 125 fprintf(stderr, 126 "Gamma values must be between %6.3f and %6.3f\n", 127 GAMMA_MIN, GAMMA_MAX); 128 exit(1); 129 } 130 continue; 131 } else if (isabbreviation ("-rgamma", arg, 2)) { 132 if (++i >= argc) Syntax (); 133 if (gam >= 0.) Syntax (); 134 rgam = (float)atof(argv[i]); 135 if ((rgam < GAMMA_MIN) || (rgam > GAMMA_MAX)) { 136 fprintf(stderr, 137 "Gamma values must be between %6.3f and %6.3f\n", 138 GAMMA_MIN, GAMMA_MAX); 139 exit(1); 140 } 141 continue; 142 } else if (isabbreviation ("-ggamma", arg, 2)) { 143 if (++i >= argc) Syntax (); 144 if (gam >= 0.) Syntax (); 145 ggam = (float)atof(argv[i]); 146 if ((ggam < GAMMA_MIN) || (ggam > GAMMA_MAX)) { 147 fprintf(stderr, 148 "Gamma values must be between %6.3f and %6.3f\n", 149 GAMMA_MIN, GAMMA_MAX); 150 exit(1); 151 } 152 continue; 153 } else if (isabbreviation ("-bgamma", arg, 2)) { 154 if (++i >= argc) Syntax (); 155 if (gam >= 0.) Syntax (); 156 bgam = (float)atof(argv[i]); 157 if ((bgam < GAMMA_MIN) || (bgam > GAMMA_MAX)) { 158 fprintf(stderr, 159 "Gamma values must be between %6.3f and %6.3f\n", 160 GAMMA_MIN, GAMMA_MAX); 161 exit(1); 162 } 163 continue; 164 } else 165 Syntax (); 166 } else 167 Syntax (); 168 } 169 170 if ((dpy = XOpenDisplay(displayname)) == NULL) { 171 fprintf (stderr, "%s: unable to open display '%s'\n", 172 ProgramName, XDisplayName (displayname)); 173 exit(1); 174 } else if (screen == -1) 175 screen = DefaultScreen(dpy); 176 177 if (!XF86VidModeQueryVersion(dpy, &MajorVersion, &MinorVersion)) { 178 fprintf(stderr, "Unable to query video extension version\n"); 179 exit(2); 180 } 181 182 if (!XF86VidModeQueryExtension(dpy, &EventBase, &ErrorBase)) { 183 fprintf(stderr, "Unable to query video extension information\n"); 184 exit(2); 185 } 186 187 /* Fail if the extension version in the server is too old */ 188 if (MajorVersion < MINMAJOR || 189 (MajorVersion == MINMAJOR && MinorVersion < MINMINOR)) { 190 fprintf(stderr, 191 "Xserver is running an old XFree86-VidModeExtension version" 192 " (%d.%d)\n", MajorVersion, MinorVersion); 193 fprintf(stderr, "Minimum required version is %d.%d\n", 194 MINMAJOR, MINMINOR); 195 exit(2); 196 } 197 198 if (!XF86VidModeGetGamma(dpy, screen, &gamma)) { 199 fprintf(stderr, "Unable to query gamma correction\n"); 200 XCloseDisplay (dpy); 201 exit (2); 202 } else if (!quiet) 203 fprintf(stderr, "-> Red %6.3f, Green %6.3f, Blue %6.3f\n", gamma.red, 204 gamma.green, gamma.blue); 205 206 ret = 0; 207 if (gam >= 0.) { 208 gamma.red = gam; 209 gamma.green = gam; 210 gamma.blue = gam; 211 if (!XF86VidModeSetGamma(dpy, screen, &gamma)) { 212 fprintf(stderr, "Unable to set gamma correction\n"); 213 ret = 2; 214 } else { 215 if (!XF86VidModeGetGamma(dpy, screen, &gamma)) { 216 fprintf(stderr, "Unable to query gamma correction\n"); 217 ret = 2; 218 } else if (!quiet) 219 fprintf(stderr, "<- Red %6.3f, Green %6.3f, Blue %6.3f\n", 220 gamma.red, gamma.green, gamma.blue); 221 } 222 } else if ((rgam >= 0.) || (ggam >= 0.) || (bgam >= 0.)) { 223 if (rgam >= 0.) gamma.red = rgam; 224 if (ggam >= 0.) gamma.green = ggam; 225 if (bgam >= 0.) gamma.blue = bgam; 226 if (!XF86VidModeSetGamma(dpy, screen, &gamma)) { 227 fprintf(stderr, "Unable to set gamma correction\n"); 228 ret = 2; 229 } else { 230 if (!XF86VidModeGetGamma(dpy, screen, &gamma)) { 231 fprintf(stderr, "Unable to query gamma correction\n"); 232 ret = 2; 233 } else if (!quiet) 234 fprintf(stderr, "<- Red %6.3f, Green %6.3f, Blue %6.3f\n", 235 gamma.red, gamma.green, gamma.blue); 236 } 237 } 238 239 XCloseDisplay (dpy); 240 exit (ret); 241} 242 243