浏览代码

Create a flag category only for backlight/kbd

Now we can save, restore, get [max] brightness etc. for both screen
and keyboard controllers
Abdullah ibn Nadjo 6 年前
父节点
当前提交
cd70ff5e01
共有 2 个文件被更改,包括 95 次插入111 次删除
  1. 19
    34
      include/light.h
  2. 76
    77
      src/light.c

+ 19
- 34
include/light.h 查看文件

@@ -12,46 +12,30 @@
12 12
 #define LIGHT_YEAR 2014
13 13
 #define LIGHT_AUTHOR "Fredrik Haikarainen"
14 14
 
15
-#define ASSERT_OPSET() \
16
-  if(opSet)\
15
+#define ASSERT_SET(t,v) \
16
+  if(v)\
17 17
   {\
18
-    printf("Operation arguments can not be used in conjunction.\n");\
18
+    printf(t" arguments can not be used in conjunction.\n");\
19 19
     return FALSE;\
20 20
   }\
21
-  opSet = TRUE;
21
+  v = TRUE;
22 22
 
23
-#define ASSERT_TARGETSET() \
24
-  if(targetSet)\
25
-  {\
26
-    printf("Target arguments can not be used in conjunction.\n");\
27
-    return FALSE;\
28
-  }\
29
-  targetSet = TRUE;
30
-
31
-#define ASSERT_CTRLSET()\
32
-  if(ctrlSet)\
33
-  {\
34
-    printf("Controller arguments can not be used in conjunction.\n");\
35
-    return FALSE;\
36
-  }\
37
-  ctrlSet = TRUE;
23
+#define ASSERT_OPSET() ASSERT_SET("Operation", opSet)
24
+#define ASSERT_TARGETSET() ASSERT_SET("Target", targetSet)
25
+#define ASSERT_FIELDSET() ASSERT_SET("Field", fieldSet)
26
+#define ASSERT_CTRLSET() ASSERT_SET("Controller", ctrlSet)
27
+#define ASSERT_VALSET() ASSERT_SET("Value", valSet)
38 28
 
39
-#define ASSERT_VALSET()\
40
-  if(valSet)\
41
-  {\
42
-    printf("Value arguments can not be used in conjunction.\n");\
43
-    return FALSE;\
44
-  }\
45
-  valSet = TRUE;
46
-
47
-
48
-typedef enum LIGHT_TARGET {
29
+typedef enum LIGHT_FIELD {
49 30
   LIGHT_BRIGHTNESS = 0,
50 31
   LIGHT_MAX_BRIGHTNESS,
51 32
   LIGHT_MIN_CAP,
52
-  LIGHT_SAVERESTORE,
53
-  LIGHT_KEYBOARD,
54
-  LIGHT_KEYBOARD_MAX_BRIGHTNESS
33
+  LIGHT_SAVERESTORE
34
+} LIGHT_FIELD;
35
+
36
+typedef enum LIGHT_TARGET {
37
+  LIGHT_BACKLIGHT = 0,
38
+  LIGHT_KEYBOARD
55 39
 } LIGHT_TARGET;
56 40
 
57 41
 typedef enum LIGHT_CTRL_MODE {
@@ -88,7 +72,8 @@ typedef struct light_runtimeArguments_s {
88 72
   unsigned long   specifiedValueRaw; /* The specified value in raw mode */
89 73
   double          specifiedValuePercent; /* The specified value in percent */
90 74
 
91
-  LIGHT_TARGET    target;
75
+  LIGHT_TARGET   target;
76
+  LIGHT_FIELD    field;
92 77
 
93 78
   /* Cache data */
94 79
   LIGHT_BOOL      hasCachedMaxBrightness;
@@ -132,7 +117,7 @@ void light_free();
132 117
 
133 118
 /* WARNING: `buffer` HAS to be freed by the user if not null once returned!
134 119
  * Size is always 256 */
135
-LIGHT_BOOL light_genPath(char const *controller, LIGHT_TARGET type, char **buffer);
120
+LIGHT_BOOL light_genPath(char const *controller, LIGHT_TARGET target, LIGHT_FIELD type, char **buffer);
136 121
 
137 122
 LIGHT_BOOL light_getBrightness(char const *controller, unsigned long *v);
138 123
 

+ 76
- 77
src/light.c 查看文件

@@ -16,7 +16,8 @@ void light_defaultConfig()
16 16
   light_Configuration.valueMode              = LIGHT_PERCENT;
17 17
   light_Configuration.specifiedValueRaw      = 0;
18 18
   light_Configuration.specifiedValuePercent  = 0.0;
19
-  light_Configuration.target                 = LIGHT_BRIGHTNESS;
19
+  light_Configuration.target                 = LIGHT_BACKLIGHT;
20
+  light_Configuration.field                  = LIGHT_BRIGHTNESS;
20 21
   light_Configuration.hasCachedMaxBrightness = FALSE;
21 22
   light_Configuration.cachedMaxBrightness    = 0;
22 23
   light_verbosity                            = 0;
@@ -29,12 +30,13 @@ LIGHT_BOOL light_parseArguments(int argc, char** argv)
29 30
 
30 31
   LIGHT_BOOL opSet = FALSE;
31 32
   LIGHT_BOOL targetSet = FALSE;
33
+  LIGHT_BOOL fieldSet = FALSE;
32 34
   LIGHT_BOOL ctrlSet = FALSE;
33 35
   LIGHT_BOOL valSet = FALSE;
34 36
 
35 37
   unsigned long specLen = 0;
36 38
 
37
-  while((currFlag = getopt(argc, argv, "HhVGSAULIObmckas:prv:")) != -1)
39
+  while((currFlag = getopt(argc, argv, "HhVGSAULIObmclkas:prv:")) != -1)
38 40
   {
39 41
     switch(currFlag)
40 42
     {
@@ -78,21 +80,28 @@ LIGHT_BOOL light_parseArguments(int argc, char** argv)
78 80
         break;
79 81
 
80 82
       /* -- Targets -- */
81
-      case 'b':
83
+      case 'l':
82 84
         ASSERT_TARGETSET();
83
-        light_Configuration.target = LIGHT_BRIGHTNESS;
85
+        light_Configuration.target = LIGHT_BACKLIGHT;
84 86
         break;
85
-      case 'm':
87
+      case 'k':
86 88
         ASSERT_TARGETSET();
87
-        light_Configuration.target = LIGHT_MAX_BRIGHTNESS;
89
+        light_Configuration.target = LIGHT_KEYBOARD;
90
+        break;
91
+
92
+      /* -- Fields -- */
93
+      case 'b':
94
+        ASSERT_FIELDSET();
95
+        light_Configuration.field = LIGHT_BRIGHTNESS;
96
+        break;
97
+      case 'm':
98
+        ASSERT_FIELDSET();
99
+        light_Configuration.field = LIGHT_MAX_BRIGHTNESS;
88 100
         break;
89 101
       case 'c':
90
-        ASSERT_TARGETSET();
91
-        light_Configuration.target = LIGHT_MIN_CAP;
102
+        ASSERT_FIELDSET();
103
+        light_Configuration.field = LIGHT_MIN_CAP;
92 104
         break;
93
-      case 'k':
94
-	      ASSERT_TARGETSET();
95
-	      light_Configuration.target = LIGHT_KEYBOARD;
96 105
 
97 106
       /* -- Controller selection -- */
98 107
       case 'a':
@@ -210,10 +219,13 @@ void light_printHelp(){
210 219
   printf("  -O:\t\tSave brightness\n\n");
211 220
 
212 221
   printf("Targets (can not be used in conjunction):\n");
222
+  printf("  -l:\t\tAct on screen backlight (default)\n");
223
+  printf("  -k:\t\tAct on keyboard backlight\n\n");
224
+
225
+  printf("Fields (can not be used in conjunction):\n");
213 226
   printf("  -b:\t\tBrightness (default)\n  \t\tUsed with [GSAU]\n\n");
214 227
   printf("  -m:\t\tMaximum brightness\n  \t\tUsed with [G]\n\n");
215 228
   printf("  -c:\t\tMinimum cap\n  \t\tUsed with [GS]\n");
216
-  printf("  -k:\t\tSet keyboard brightness instead of display brightness \n \t\tUsed with [GSAU]");
217 229
   printf("  \t\tG returns null if no minimum cap is set.\n\n");
218 230
 
219 231
   printf("Controller selection (can not be used in conjunction):\n");
@@ -250,14 +262,14 @@ LIGHT_BOOL light_initialize(int argc, char** argv)
250 262
   }
251 263
 
252 264
   if(mode == LIGHT_SAVE ||
253
-     (mode == LIGHT_SET && light_Configuration.target == LIGHT_MIN_CAP))
265
+     (mode == LIGHT_SET && light_Configuration.field == LIGHT_MIN_CAP))
254 266
   {
255 267
     /* Make sure we have a valid /etc/light directory, as well as mincap and save */
256
-    char const * const dirs[3] = {"/etc/light", "/etc/light/mincap", "/etc/light/save"};
268
+    char const * const dirs[5] = {"/etc/light", "/etc/light/mincap", "/etc/light/save", "/etc/light/mincap/kbd", "/etc/light/save/kbd"};
257 269
     char const * const *dir = dirs;
258 270
     char const * const direrr = "'%s' does not exist and could not be created, make sure this application is run as root.";
259 271
 
260
-    while (dir < dirs + 3)
272
+    while (dir < dirs + 5)
261 273
     {
262 274
       mkdirVal = mkdir(*dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
263 275
       if(mkdirVal != 0 && errno != EEXIST)
@@ -326,7 +338,7 @@ LIGHT_BOOL light_initExecution(unsigned long *rawCurr, unsigned long *rawMax, LI
326 338
   }
327 339
 
328 340
   /* No need to go further if targetting mincap */
329
-  if(light_Configuration.target == LIGHT_MIN_CAP)
341
+  if(light_Configuration.field == LIGHT_MIN_CAP)
330 342
   {
331 343
     return TRUE;
332 344
   }
@@ -386,7 +398,7 @@ LIGHT_BOOL light_execute()
386 398
   /* Handle get operations */
387 399
   if(light_Configuration.operationMode == LIGHT_GET)
388 400
   {
389
-    switch(light_Configuration.target){
401
+    switch(light_Configuration.field){
390 402
       case LIGHT_BRIGHTNESS:
391 403
         (valueMode == LIGHT_RAW) ? printf("%lu\n", rawCurr) : printf("%.2f\n", percentCurr);
392 404
         break;
@@ -427,7 +439,7 @@ LIGHT_BOOL light_execute()
427 439
      light_Configuration.operationMode == LIGHT_ADD ||
428 440
      light_Configuration.operationMode == LIGHT_SUB)
429 441
   {
430
-    if(light_Configuration.target == LIGHT_MIN_CAP)
442
+    if(light_Configuration.field == LIGHT_MIN_CAP)
431 443
     {
432 444
       /* Handle minimum cap files */
433 445
       writeVal = valueMode == LIGHT_RAW ?
@@ -449,7 +461,7 @@ LIGHT_BOOL light_execute()
449 461
       /* All good? Return true. */
450 462
       return TRUE;
451 463
 
452
-    }else if(light_Configuration.target == LIGHT_BRIGHTNESS || light_Configuration.target == LIGHT_KEYBOARD){
464
+    }else if(light_Configuration.field == LIGHT_BRIGHTNESS){
453 465
       /* Handle brightness writing */
454 466
 
455 467
       switch(light_Configuration.operationMode)
@@ -489,7 +501,7 @@ LIGHT_BOOL light_execute()
489 501
       return TRUE;
490 502
 
491 503
     }else{
492
-      /* If we didn't provide a valid target for write operations, fail. */
504
+      /* If we didn't provide a valid field for write operations, fail. */
493 505
       fprintf(stderr, "set/add/subtract operations are only available for brightness and minimum cap files.\n");
494 506
       return FALSE;
495 507
     }
@@ -515,7 +527,7 @@ LIGHT_BOOL light_execute()
515 527
     return TRUE;
516 528
   }
517 529
 
518
-  fprintf(stderr, "Controller: %s\nValueRaw: %lu\nValuePercent: %.2f\nOpMode: %u\nValMode: %u\nTarget: %u\n\n", light_Configuration.specifiedController, light_Configuration.specifiedValueRaw, light_Configuration.specifiedValuePercent, light_Configuration.operationMode, light_Configuration.valueMode, light_Configuration.target);
530
+  fprintf(stderr, "Controller: %s\nValueRaw: %lu\nValuePercent: %.2f\nOpMode: %u\nValMode: %u\nField: %u\n\n", light_Configuration.specifiedController, light_Configuration.specifiedValueRaw, light_Configuration.specifiedValuePercent, light_Configuration.operationMode, valueMode, light_Configuration.field);
519 531
 
520 532
   fprintf(stderr, "You did not specify a valid combination of commandline arguments. Have some help: \n");
521 533
   light_printHelp();
@@ -527,7 +539,7 @@ void light_free()
527 539
 
528 540
 }
529 541
 
530
-LIGHT_BOOL light_genPath(char const *controller, LIGHT_TARGET type, char **buffer)
542
+LIGHT_BOOL light_genPath(char const *controller, LIGHT_TARGET target, LIGHT_FIELD type, char **buffer)
531 543
 {
532 544
   char* returner = malloc(256);
533 545
   int spfVal = -1;
@@ -540,29 +552,40 @@ LIGHT_BOOL light_genPath(char const *controller, LIGHT_TARGET type, char **buffe
540 552
   }
541 553
 
542 554
   memset(returner, '\0', 256);
543
-
544
-  switch(type)
545
-  {
546
-    case LIGHT_BRIGHTNESS:
547
-      spfVal = sprintf(returner, "/sys/class/backlight/%s/brightness", controller);
548
-      break;
549
-    case LIGHT_MAX_BRIGHTNESS:
550
-      spfVal = sprintf(returner, "/sys/class/backlight/%s/max_brightness", controller);
551
-      break;
552
-    case LIGHT_MIN_CAP:
553
-      spfVal = sprintf(returner, "/etc/light/mincap/%s", controller);
554
-      break;
555
-    case LIGHT_SAVERESTORE:
556
-      spfVal = sprintf(returner, "/etc/light/save/%s", controller);
557
-      break;
558
-    case LIGHT_KEYBOARD:
559
-      spfVal = sprintf(returner, "/sys/class/leds/%s/brightness", controller);
560
-      break;
561
-    case LIGHT_KEYBOARD_MAX_BRIGHTNESS:
562
-      spfVal = sprintf(returner, "/sys/class/leds/%s/max_brightness", controller);
563
-      break;
555
+  if(target == LIGHT_BACKLIGHT)
556
+  {
557
+    switch(type)
558
+    {
559
+      case LIGHT_BRIGHTNESS:
560
+        spfVal = sprintf(returner, "/sys/class/backlight/%s/brightness", controller);
561
+        break;
562
+      case LIGHT_MAX_BRIGHTNESS:
563
+        spfVal = sprintf(returner, "/sys/class/backlight/%s/max_brightness", controller);
564
+        break;
565
+      case LIGHT_MIN_CAP:
566
+        spfVal = sprintf(returner, "/etc/light/mincap/%s", controller);
567
+        break;
568
+      case LIGHT_SAVERESTORE:
569
+        spfVal = sprintf(returner, "/etc/light/save/%s", controller);
570
+        break;
571
+    }
572
+  }else{
573
+    switch(type)
574
+    {
575
+      case LIGHT_BRIGHTNESS:
576
+        spfVal = sprintf(returner, "/sys/class/leds/%s/brightness", controller);
577
+        break;
578
+      case LIGHT_MAX_BRIGHTNESS:
579
+        spfVal = sprintf(returner, "/sys/class/leds/%s/max_brightness", controller);
580
+        break;
581
+      case LIGHT_MIN_CAP:
582
+        spfVal = sprintf(returner, "/etc/light/mincap/kbd/%s", controller);
583
+        break;
584
+      case LIGHT_SAVERESTORE:
585
+        spfVal = sprintf(returner, "/etc/light/save/kbd/%s", controller);
586
+        break;
587
+    }
564 588
   }
565
-
566 589
   if(spfVal < 0)
567 590
   {
568 591
     LIGHT_ERR("sprintf failed");
@@ -572,24 +595,12 @@ LIGHT_BOOL light_genPath(char const *controller, LIGHT_TARGET type, char **buffe
572 595
   }
573 596
 
574 597
   *buffer = returner;
575
-
576 598
   return TRUE;
577 599
 }
578 600
 
579 601
 LIGHT_BOOL light_getBrightnessPath(char const *controller, char **path)
580 602
 {
581
-  LIGHT_TARGET target;
582
-
583
-  if(light_Configuration.target == LIGHT_MIN_CAP)
584
-  {
585
-    target = LIGHT_BRIGHTNESS;
586
-  }
587
-  else
588
-  {
589
-    target = light_Configuration.target;
590
-  }
591
-
592
-  if(!light_genPath(controller, target, path))
603
+  if(!light_genPath(controller, light_Configuration.target, LIGHT_BRIGHTNESS, path))
593 604
   {
594 605
     LIGHT_ERR("could not generate path to brightness file");
595 606
     return FALSE;
@@ -615,24 +626,12 @@ LIGHT_BOOL light_getBrightness(char const *controller, unsigned long *v)
615 626
     LIGHT_ERR("could not read value from brightness file");
616 627
     return FALSE;
617 628
   }
618
-
619 629
   return TRUE;
620 630
 }
621 631
 
622 632
 LIGHT_BOOL light_getMaxBrightnessPath(char const *controller, char **path)
623 633
 {
624
-  LIGHT_TARGET target;
625
-
626
-  if(light_Configuration.target == LIGHT_KEYBOARD)
627
-  {
628
-    target = LIGHT_KEYBOARD_MAX_BRIGHTNESS;
629
-  }
630
-  else
631
-  {
632
-    target = LIGHT_MAX_BRIGHTNESS;
633
-  }
634
-
635
-  if(!light_genPath(controller, target, path))
634
+  if(!light_genPath(controller, light_Configuration.target, LIGHT_MAX_BRIGHTNESS, path))
636 635
   {
637 636
     LIGHT_ERR("could not generate path to maximum brightness file");
638 637
     return FALSE;
@@ -672,7 +671,7 @@ LIGHT_BOOL light_setBrightness(char const *controller, unsigned long v)
672 671
   char *brightnessPath = NULL;
673 672
   LIGHT_BOOL writeVal = FALSE;
674 673
 
675
-  if(!light_genPath(controller, light_Configuration.target, &brightnessPath))
674
+  if(!light_genPath(controller, light_Configuration.target, light_Configuration.field, &brightnessPath))
676 675
   {
677 676
     LIGHT_ERR("could not generate path to brightness file");
678 677
     return FALSE;
@@ -697,7 +696,7 @@ LIGHT_BOOL light_controllerAccessible(char const *controller)
697 696
   /* On auto mode, we need to check if we can read the max brightness value
698 697
      of the controller for later computation */
699 698
   if(light_Configuration.controllerMode == LIGHT_AUTO ||
700
-     light_Configuration.target == LIGHT_MAX_BRIGHTNESS)
699
+     light_Configuration.field == LIGHT_MAX_BRIGHTNESS)
701 700
   {
702 701
     if(!light_getMaxBrightnessPath(controller, &brightnessPath))
703 702
     {
@@ -718,7 +717,7 @@ LIGHT_BOOL light_controllerAccessible(char const *controller)
718 717
   }
719 718
 
720 719
   if(light_Configuration.operationMode != LIGHT_GET &&
721
-     light_Configuration.target != LIGHT_MIN_CAP &&
720
+     light_Configuration.field != LIGHT_MIN_CAP &&
722 721
      !light_isWritable(brightnessPath))
723 722
   {
724 723
     LIGHT_WARN("could not open controller brightness file for writing, so controller is not accessible");
@@ -840,7 +839,7 @@ LIGHT_BOOL light_getMinCap(char const * controller, LIGHT_BOOL * hasMinCap, unsi
840 839
 {
841 840
  char * mincapPath = NULL;
842 841
 
843
- if(!light_genPath(controller, LIGHT_MIN_CAP, &mincapPath))
842
+ if(!light_genPath(controller, light_Configuration.target, LIGHT_MIN_CAP, &mincapPath))
844 843
  {
845 844
     LIGHT_ERR("could not generate path to minimum cap file");
846 845
     return FALSE;
@@ -870,7 +869,7 @@ LIGHT_BOOL light_getMinCap(char const * controller, LIGHT_BOOL * hasMinCap, unsi
870 869
 LIGHT_BOOL light_setMinCap(char const * controller, unsigned long v)
871 870
 {
872 871
   char * mincapPath = NULL;
873
-  if(!light_genPath(controller, LIGHT_MIN_CAP, &mincapPath))
872
+  if(!light_genPath(controller, light_Configuration.target, LIGHT_MIN_CAP, &mincapPath))
874 873
   {
875 874
     LIGHT_ERR("could not generate path to minimum cap file");
876 875
     return FALSE;
@@ -913,7 +912,7 @@ LIGHT_BOOL light_listControllers()
913 912
 
914 913
 LIGHT_BOOL light_saveBrightness(char const *controller, unsigned long v){
915 914
   char *savePath = NULL;
916
-  if(!light_genPath(controller, LIGHT_SAVERESTORE, &savePath))
915
+  if(!light_genPath(controller, light_Configuration.target, LIGHT_SAVERESTORE, &savePath))
917 916
   {
918 917
     LIGHT_ERR("could not generate path to save/restore file");
919 918
     return FALSE;
@@ -935,7 +934,7 @@ LIGHT_BOOL light_restoreBrightness(char const *controller){
935 934
   char *restorePath = NULL;
936 935
   unsigned long v = 0;
937 936
 
938
-  if(!light_genPath(controller, LIGHT_SAVERESTORE, &restorePath))
937
+  if(!light_genPath(controller, light_Configuration.target, LIGHT_SAVERESTORE, &restorePath))
939 938
   {
940 939
     LIGHT_ERR("could not generate path to save/restore file");
941 940
     return FALSE;