Only in xbrightness-0.3-mika-akk: gammatwister diff -u xbrightness-0.3/xbrightness.c xbrightness-0.3-mika-akk/xbrightness.c --- xbrightness-0.3/xbrightness.c 2005-10-15 02:23:21.000000000 -0700 +++ xbrightness-0.3-mika-akk/xbrightness.c 2009-02-28 16:21:29.000000000 -0800 @@ -20,6 +20,9 @@ * SOFTWARE. * * Written by Asher Blum + * + * R, G, B argument options were added by Mikael Magnusson. + * Print current (default) option and +/- options were added by Akkana Peck. */ #include @@ -32,13 +35,19 @@ #include #include +#define MAX_BRIGHTNESS 65535 + char *argv0; static void syntax(void) { fprintf (stderr, "%s builds a brightness ramp for X11.\n\n", argv0); fprintf (stderr, "usage: %s BRIGHTNESS [ GBASE ]\n\n", argv0); - fprintf (stderr, "where BRIGHTNESS is a number from 0 to 65535.\n"); - fprintf (stderr, "where GBASE is a float number from 0.0 to 10.0. (default=1.0)\n"); + fprintf (stderr, "or: %s RBRIGHTNESS GBRIGHTNESS BBRIGHTNESS\n", argv0); + fprintf (stderr, "or: %s [+change|-change]\n\n", argv0); + fprintf (stderr, "where BRIGHTNESS is a number from 0 to 65535\n"); + fprintf (stderr, "GBASE is a float number from 0.0 to 10.0. (default=1.0)\n"); + fprintf (stderr, "R G and B are red, green and blue\n"); + fprintf (stderr, "and change is an increment or decrement.\n"); exit (1); } @@ -47,8 +56,10 @@ Display *dpy; int screen = -1; int ramp_size = 0; - unsigned short *ramp; - unsigned short brightness = 60000; + unsigned short *rramp, *gramp, *bramp; + unsigned short print_only = 0; + unsigned short rinc=0, ginc=0, binc=0; /* do incremental brightness? */ + int rbrightness = 60000, gbrightness=60000, bbrightness=60000; float k; double gbase = 1.0; @@ -56,24 +67,56 @@ argv0 = argv[0]; - if((argc != 2) && (argc !=3)){ - syntax(); + if (argc <= 1) { + print_only = 1; } - i = atoi(argv[1]); - if(!i && (argv[1][1] || !isdigit(argv[1][0]))) { - syntax(); - } - if(i<0 || i>65535) { + else if (argc > 4) { syntax(); } - brightness=i; - if (argc == 3) { - gbase = atof (argv[2]); - if (gbase == 0.0) { - syntax(); - } + else if (argc <= 3) { + if (argv[1][0] == '-' || argv[1][0] == '+') { + int brightness, red, green, blue; + int diff = atoi(argv[1]); + if (!diff && !isdigit(argv[1][1])) + syntax(); + rinc = ginc = binc = 1; + rbrightness = diff; + gbrightness = diff; + bbrightness = diff; + } + else { + i = atoi(argv[1]); + if(!i && (argv[1][1] || !isdigit(argv[1][0]))) { + syntax(); + } + if(i<0 || i>MAX_BRIGHTNESS) { + syntax(); + } + if (argc == 3) { + gbase = atof (argv[2]); + if (gbase == 0.0) { + syntax(); + } + } + rbrightness=gbrightness=bbrightness=i; + } + + } else { + rinc = (argv[1][0] == '+' || argv[1][0] == '-'); + ginc = (argv[2][0] == '+' || argv[2][0] == '-'); + binc = (argv[3][0] == '+' || argv[3][0] == '-'); + rbrightness = atoi(argv[1]); + gbrightness = atoi(argv[2]); + bbrightness = atoi(argv[3]); + printf("%d %d %d (%d %d %d)\n", rbrightness, gbrightness, + bbrightness, rinc, ginc, binc); + + if ((rbrightness<0 && !rinc) || (gbrightness<0 && !ginc) + || (bbrightness<0 && !ginc)) { + syntax(); + } } /**** Setup ****/ @@ -94,25 +137,68 @@ exit (2); } - ramp = (unsigned short *) malloc (ramp_size * sizeof(unsigned short)); - if (ramp == NULL) { - fprintf(stderr, "could not allocate %lu bytes, aborting\n", (unsigned long)(ramp_size * sizeof(unsigned short))); + rramp = (unsigned short *) malloc (ramp_size * sizeof(unsigned short)); + gramp = (unsigned short *) malloc (ramp_size * sizeof(unsigned short)); + bramp = (unsigned short *) malloc (ramp_size * sizeof(unsigned short)); + if (rramp == NULL || gramp == NULL || bramp == NULL) { + fprintf(stderr, "could not allocate %lu bytes x 3, aborting\n", (unsigned long)(ramp_size * sizeof(unsigned short))); XCloseDisplay (dpy); exit (2); } - if (!XF86VidModeGetGammaRamp(dpy, screen, ramp_size, ramp, ramp, ramp)) { + if (!XF86VidModeGetGammaRamp(dpy, screen, ramp_size, rramp, gramp, bramp)) { fprintf(stderr, "Unable to query brightness\n"); XCloseDisplay (dpy); exit (2); } + if (print_only) { + int total, av, maxg; + int r = rramp[ramp_size-1]; + int g = gramp[ramp_size-1]; + int b = bramp[ramp_size-1]; + + /* Make a guess at perceived total based on the three colors */ + /* For now, use the average of the max and the average */ + av = (r + g + b) / 3; + maxg = (r > g ? r : g); + if (b > maxg) maxg = b; + total = (av + maxg) / 2; + + printf("%d: %d %d %d\n", total, r, g, b); + XCloseDisplay (dpy); + exit(0); + } + + /* Adjust for incremental mode */ + if (rinc) rbrightness += rramp[ramp_size-1]; + if (ginc) gbrightness += gramp[ramp_size-1]; + if (binc) bbrightness += bramp[ramp_size-1]; + + /* guard against wrapping around */ + if (rbrightness > MAX_BRIGHTNESS) rbrightness = MAX_BRIGHTNESS; + if (gbrightness > MAX_BRIGHTNESS) gbrightness = MAX_BRIGHTNESS; + if (bbrightness > MAX_BRIGHTNESS) bbrightness = MAX_BRIGHTNESS; + if (rbrightness < 0) rbrightness = 0; + if (gbrightness < 0) gbrightness = 0; + if (bbrightness < 0) bbrightness = 0; + + for(i=0; i