droid4-sys

Droid4 tools
git clone git://parazyd.org/droid4-sys.git | https://git.parazyd.org/droid4-sys
Log | Files | Refs | LICENSE

backlight.c (1079B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 #include "arg.h"
      6 
      7 char *argv0;
      8 
      9 void usage(void) {
     10 	fprintf(stderr, "usage: %s [-l|-k] [on|off]\n", argv0);
     11 	exit(1);
     12 }
     13 
     14 int main(int argc, char *argv[]) {
     15 	const char *kbd_backlight = "/sys/class/leds/kbd_backlight/brightness";
     16 	const char *lcd_backlight = "/sys/class/backlight/lcd/brightness";
     17 	int iskbd = 0, islcd = 0, state = -1;
     18 	char *act = NULL;
     19 	FILE *fd = NULL;
     20 
     21 	ARGBEGIN {
     22 	case 'l':
     23 		islcd = 1;
     24 		act = EARGF(usage());
     25 		break;
     26 	case 'k':
     27 		iskbd = 1;
     28 		act = EARGF(usage());
     29 		break;
     30 	default:
     31 		usage();
     32 	} ARGEND;
     33 
     34 	if ((islcd && iskbd) || (!islcd && !iskbd))
     35 		usage();
     36 
     37 	if (!strcmp(act, "on"))
     38 		state = 1;
     39 	else if (!strcmp(act, "off"))
     40 		state = 0;
     41 	if (state < 0)
     42 		usage();
     43 
     44 	if (islcd)
     45 		fd = fopen(lcd_backlight, "w");
     46 	if (iskbd)
     47 		fd = fopen(kbd_backlight, "w");
     48 
     49 	if (fd == NULL) {
     50 		fprintf(stderr, "Unable to open file for writing.\n");
     51 		return 1;
     52 	}
     53 
     54 	if (islcd)
     55 		fprintf(fd, "%d", state==0 ? 0 : 200);
     56 	if (iskbd)
     57 		fprintf(fd, "%d", state==0 ? 0 : 255);
     58 
     59 	fclose(fd);
     60 
     61 	return 0;
     62 }