lightweight X11 utility to dim the screen and/or keyboard backlight when idle
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

add a -k option to dim keyboard backlight, too (openbsd only)

+103 -24
+18 -8
xdimmer.1
··· 1 - .Dd $Mdocdate: December 8 2015$ 1 + .Dd $Mdocdate: July 25 2016$ 2 2 .Dt XDIMMER 1 3 3 .Os 4 4 .Sh NAME 5 5 .Nm xdimmer 6 - .Nd dim the screen when idle 6 + .Nd dim the screen (and possibly the keyboard) backlight when idle 7 7 .Sh SYNOPSIS 8 8 .Nm 9 9 .Op Fl d 10 + .Op Fl k 10 11 .Op Fl p Ar percent 11 12 .Op Fl s Ar steps 12 13 .Op Fl t Ar timeout ··· 15 16 waits 16 17 .Ar timeout 17 18 number of seconds and if no keyboard or mouse input is detected, the screen 18 - is dimmed to 19 + backlight is dimmed to 19 20 .Ar percent 20 21 in 21 22 .Ar steps 22 23 steps. 23 - Once keyboard or mouse input is detected, the screen is restored to its 24 - previous brightness value. 24 + Once keyboard or mouse input is detected, the screen backlight is restored 25 + to its previous brightness value. 26 + .Pp 27 + On OpenBSD, if the 28 + .Ar -k 29 + option is used, the keyboard backlight is also dimmed and restored via the 30 + .Xr wscons 4 31 + interface. 25 32 .Sh OPTIONS 26 33 .Bl -tag -width Ds 27 34 .It Fl d 28 35 Print debugging messages to stdout. 36 + .It Fl k 37 + Affect the keyboard backlight as well as the screen backlight. 38 + Currently only supported on OpenBSD. 29 39 .It Fl p Ar percent 30 - Absolute brightness value to which the screen is dimmed. 40 + Absolute brightness value to which the backlight is dimmed. 31 41 The default is 32 42 .Dv 10 33 43 percent. 34 44 .It Fl t Ar steps 35 - Number of steps to take while decrementing screen brightness. 45 + Number of steps to take while decrementing backlight. 36 46 The default is 37 47 .Dv 20 38 48 steps. 39 49 .It Fl t Ar timeout 40 - Number of seconds to wait without receiving input before dimming screen. 50 + Number of seconds to wait without receiving input before dimming. 41 51 The default is 42 52 .Dv 120 43 53 seconds.
+85 -16
xdimmer.c
··· 63 63 void dim(void); 64 64 void brighten(void); 65 65 void bail(int); 66 - void stepper(int, double, int); 66 + void stepper(int, double, double, int); 67 67 double backlight_op(int, double); 68 + double kbd_backlight_op(int, double); 68 69 void usage(void); 69 70 70 71 extern char *__progname; ··· 74 75 static int dimmed = 0; 75 76 static int debug = 0; 76 77 static int exiting = 0; 78 + static int dimkbd = 0; 79 + static double kbd_backlight = -1; 77 80 78 81 static int dim_timeout = DEFAULT_DIM_TIMEOUT; 79 82 static int dim_pct = DEFAULT_DIM_PERCENTAGE; 80 83 static int dim_steps = DIM_STEPS; 81 84 82 - static int wsconsfd = 0; 85 + static int wsconsdfd = 0; 86 + static int wsconskfd = 0; 83 87 84 88 static Display *dpy; 85 89 ··· 88 92 { 89 93 int ch; 90 94 91 - while ((ch = getopt(argc, argv, "dp:s:t:")) != -1) { 95 + while ((ch = getopt(argc, argv, "dkp:s:t:")) != -1) { 92 96 const char *errstr; 93 97 94 98 switch (ch) { 95 99 case 'd': 96 100 debug = 1; 97 101 break; 102 + case 'k': 103 + #ifndef __OpenBSD__ 104 + errx(1, "keyboard backlight not supported on this " 105 + "platform"); 106 + #endif 107 + dimkbd = 1; 108 + break; 98 109 case 'p': 99 110 dim_pct = strtonum(optarg, 1, 100, &errstr); 100 111 if (errstr) ··· 124 135 if (backlight_a == None) { 125 136 #ifdef __OpenBSD__ 126 137 /* see if wscons display.brightness is available */ 127 - if (!(wsconsfd = open("/dev/ttyC0", O_WRONLY)) || 138 + if (!(wsconsdfd = open("/dev/ttyC0", O_WRONLY)) || 128 139 backlight_op(OP_GET, 0) < 0) 129 140 #endif 130 141 errx(1, "no backlight control"); 131 - } else { 142 + } 132 143 #ifdef __OpenBSD__ 144 + else if (!dimkbd) { 133 145 /* 134 146 * unfortunately we can't pledge() with wscons interface, 135 147 * because the ws*io* ioctls aren't whitelisted 136 148 */ 137 149 if (pledge("stdio", NULL) == -1) 138 150 err(1, "pledge"); 151 + } 152 + 153 + if (dimkbd) 154 + if (!(wsconskfd = open("/dev/wskbd0", O_WRONLY)) || 155 + kbd_backlight_op(OP_GET, 0) < 0) 156 + errx(1, "no keyboard backlight control"); 139 157 #endif 158 + 159 + if (debug) { 160 + printf("dimming screen to %d%% in %d secs\n", dim_pct, 161 + dim_timeout); 162 + 163 + if (dimkbd) 164 + printf("dimming keyboard backlight in %d secs\n", 165 + dim_timeout); 140 166 } 141 - 142 - if (debug) 143 - printf("dimming to %d%% in %d secs\n", dim_pct, dim_timeout); 144 167 145 168 signal(SIGINT, bail); 146 169 signal(SIGTERM, bail); ··· 268 291 dim(void) 269 292 { 270 293 backlight = backlight_op(OP_GET, 0); 294 + if (dimkbd) 295 + kbd_backlight = backlight_op(OP_GET, 0); 271 296 272 297 if (backlight > dim_pct) { 273 298 if (debug) 274 299 printf("dimming to %d\n", dim_pct); 275 300 276 - stepper(OP_SET, dim_pct, dim_steps); 301 + stepper(OP_SET, dim_pct, 0, dim_steps); 277 302 dimmed = 1; 278 303 } 279 304 else if (debug) ··· 288 313 if (debug) 289 314 printf("brightening back to %f\n", backlight); 290 315 291 - stepper(OP_SET, backlight, BRIGHTEN_STEPS); 316 + stepper(OP_SET, backlight, kbd_backlight, BRIGHTEN_STEPS); 292 317 } 293 318 else if (debug) 294 319 printf("no previous backlight setting, not brightening\n"); ··· 297 322 } 298 323 299 324 void 300 - stepper(int op, double new_backlight, int steps) 325 + stepper(int op, double new_backlight, double new_kbd_backlight, int steps) 301 326 { 302 327 double tbacklight = backlight_op(OP_GET, 0); 303 - int step_inc = 0, j; 328 + double tkbdbacklight; 329 + int step_inc = 0, kbd_step_inc = 0, j; 304 330 305 331 if ((int)new_backlight == (int)tbacklight) 306 332 steps = 0; 307 333 else 308 334 step_inc = (new_backlight - tbacklight) / steps; 309 335 336 + if (dimkbd) { 337 + tkbdbacklight = kbd_backlight_op(OP_GET, 0); 338 + 339 + if ((int)new_kbd_backlight != (int)tkbdbacklight) 340 + kbd_step_inc = (new_kbd_backlight - tkbdbacklight) / 341 + steps; 342 + } 343 + 310 344 if (debug) 311 345 printf("stepping from %0.2f to %0.2f in increments of %d " 312 346 "(%d step%s)... ", ··· 316 350 for (j = 1; j <= steps; j++) { 317 351 tbacklight += step_inc; 318 352 319 - if (j == steps) 353 + if (dimkbd) 354 + tkbdbacklight += kbd_step_inc; 355 + 356 + if (j == steps) { 320 357 tbacklight = new_backlight; 321 358 359 + if (dimkbd) 360 + tkbdbacklight = new_kbd_backlight; 361 + } 362 + 322 363 if (debug) 323 364 printf(" %0.2f", tbacklight); 324 365 325 366 backlight_op(OP_SET, tbacklight); 367 + 368 + if (dimkbd) 369 + kbd_backlight_op(OP_SET, tkbdbacklight); 326 370 327 371 if (j < steps && step_inc < 0) 328 372 usleep(15000); ··· 352 396 struct wsdisplay_param param; 353 397 354 398 param.param = WSDISPLAYIO_PARAM_BRIGHTNESS; 355 - if (ioctl(wsconsfd, WSDISPLAYIO_GETPARAM, &param) < 0) 399 + if (ioctl(wsconsdfd, WSDISPLAYIO_GETPARAM, &param) < 0) 356 400 errx(1, "ioctl"); 357 401 358 402 if (op == OP_SET) { ··· 364 408 else if (param.curval < param.min) 365 409 param.curval = param.min; 366 410 367 - if (ioctl(wsconsfd, WSDISPLAYIO_SETPARAM, &param) < 0) 411 + if (ioctl(wsconsdfd, WSDISPLAYIO_SETPARAM, &param) < 0) 368 412 errx(1, "ioctl"); 369 413 } 370 414 ··· 438 482 return cur_backlight; 439 483 } 440 484 485 + double 486 + kbd_backlight_op(int op, double new_backlight) 487 + { 488 + struct wskbd_backlight param; 489 + 490 + if (ioctl(wsconskfd, WSKBDIO_GETBACKLIGHT, &param) < 0) 491 + errx(1, "ioctl"); 492 + 493 + if (op == OP_SET) { 494 + param.curval = (double)(param.max - param.min) * 495 + (new_backlight / 100.0); 496 + 497 + if (param.curval > param.max) 498 + param.curval = param.max; 499 + else if (param.curval < param.min) 500 + param.curval = param.min; 501 + 502 + if (ioctl(wsconsdfd, WSKBDIO_SETBACKLIGHT, &param) < 0) 503 + errx(1, "ioctl"); 504 + } 505 + 506 + return ((double)param.curval / 507 + (double)(param.max - param.min)) * 100; 508 + } 509 + 441 510 void 442 511 usage(void) 443 512 { 444 - fprintf(stderr, "usage: %s [-d] [-p dim pct] [-s dim steps] " 513 + fprintf(stderr, "usage: %s [-d] [-k] [-p dim pct] [-s dim steps] " 445 514 "[-t timeout secs]\n", __progname); 446 515 exit(1); 447 516 }