1 /*
2 * Copyright 2003 Asher Blum <asher@wildspark.com>
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 AUTHOR 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 Asher Blum <asher@wildspark.com>
23 */
24
25 #include <ctype.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <X11/Xos.h>
30 #include <X11/Xlib.h>
31 #include <X11/Xutil.h>
32 #include <X11/extensions/xf86vmode.h>
33 #include <math.h>
34
35 char *argv0;
36
syntax(void)37 static void syntax(void) {
38 fprintf (stderr, "%s builds a brightness ramp for X11.\n\n", argv0);
39 fprintf (stderr, "usage: %s BRIGHTNESS [ GBASE ]\n\n", argv0);
40 fprintf (stderr, "where BRIGHTNESS is a number from 0 to 65535.\n");
41 fprintf (stderr, "where GBASE is a float number from 0.0 to 10.0. (default=1.0)\n");
42 exit (1);
43 }
44
main(int argc,char * argv[])45 int main(int argc, char *argv[]) {
46 int i;
47 Display *dpy;
48 int screen = -1;
49 int ramp_size = 0;
50 unsigned short *ramp;
51 unsigned short brightness = 60000;
52 float k;
53 double gbase = 1.0;
54
55 /**** Read arguments ****/
56
57 argv0 = argv[0];
58
59 if((argc != 2) && (argc !=3)){
60 syntax();
61 }
62
63 i = atoi(argv[1]);
64 if(!i && (argv[1][1] || !isdigit(argv[1][0]))) {
65 syntax();
66 }
67 if(i<0 || i>65535) {
68 syntax();
69 }
70 brightness=i;
71
72 if (argc == 3) {
73 gbase = atof (argv[2]);
74 if (gbase == 0.0) {
75 syntax();
76 }
77 }
78
79 /**** Setup ****/
80
81 if ((dpy = XOpenDisplay(NULL)) == NULL) {
82 fprintf (stderr, "%s: unable to open default display.\n",
83 argv0);
84 exit(1);
85 }
86
87 screen = DefaultScreen(dpy);
88
89 /**** main portion ****/
90
91 if (!XF86VidModeGetGammaRampSize(dpy, screen, &ramp_size)) {
92 fprintf(stderr, "Unable to query gamma ramp size\n");
93 XCloseDisplay (dpy);
94 exit (2);
95 }
96
97 ramp = (unsigned short *) malloc (ramp_size * sizeof(unsigned short));
98 if (ramp == NULL) {
99 fprintf(stderr, "could not allocate %lu bytes, aborting\n", (unsigned long)(ramp_size * sizeof(unsigned short)));
100 XCloseDisplay (dpy);
101 exit (2);
102 }
103
104 if (!XF86VidModeGetGammaRamp(dpy, screen, ramp_size, ramp, ramp, ramp)) {
105 fprintf(stderr, "Unable to query brightness\n");
106 XCloseDisplay (dpy);
107 exit (2);
108 }
109
110 for(i=0; i<ramp_size; i++) {
111 double p = ((double) i)/ramp_size;
112 ramp[i] = (unsigned short)(brightness * pow (p, gbase));
113 }
114
115 if(!XF86VidModeSetGammaRamp(dpy, screen, ramp_size, ramp, ramp, ramp)) {
116 fprintf(stderr, "Unable to set brightness");
117 XCloseDisplay (dpy);
118 exit (2);
119 }
120
121 XCloseDisplay (dpy);
122 exit (0);
123 }
124
125