Browse Source

Add -k flag for keyboard backlight support

Sebastian Müller 7 years ago
parent
commit
f0da610cf1
3 changed files with 55 additions and 21 deletions
  1. 1
    0
      README.md
  2. 4
    2
      include/light.h
  3. 50
    19
      src/light.c

+ 1
- 0
README.md View File

79
 * -b: Current brightness of selected controller
79
 * -b: Current brightness of selected controller
80
 * -m: Maximum brightness of selected controller
80
 * -m: Maximum brightness of selected controller
81
 * -c: Minimum brightness (cap) of selected controller
81
 * -c: Minimum brightness (cap) of selected controller
82
+* -k: Set keyboard brightness instead of display brightness
82
 
83
 
83
 The minimum brightness is a feature implemented as some controllers make the screen go pitch black at 0%, if you have a controller like that, it is recommended to set this value (in either percent or in raw mode). These values will be saved in raw mode though, so if you specify it in percent it might not be too accurate depending on your controller.
84
 The minimum brightness is a feature implemented as some controllers make the screen go pitch black at 0%, if you have a controller like that, it is recommended to set this value (in either percent or in raw mode). These values will be saved in raw mode though, so if you specify it in percent it might not be too accurate depending on your controller.
84
 
85
 

+ 4
- 2
include/light.h View File

49
   LIGHT_BRIGHTNESS = 0,
49
   LIGHT_BRIGHTNESS = 0,
50
   LIGHT_MAX_BRIGHTNESS,
50
   LIGHT_MAX_BRIGHTNESS,
51
   LIGHT_MIN_CAP,
51
   LIGHT_MIN_CAP,
52
-  LIGHT_SAVERESTORE
52
+  LIGHT_SAVERESTORE,
53
+  LIGHT_KEYBOARD,
54
+  LIGHT_KEYBOARD_MAX_BRIGHTNESS
53
 } LIGHT_TARGET;
55
 } LIGHT_TARGET;
54
 
56
 
55
 typedef enum LIGHT_CTRL_MODE {
57
 typedef enum LIGHT_CTRL_MODE {
85
   LIGHT_VAL_MODE  valueMode;
87
   LIGHT_VAL_MODE  valueMode;
86
   unsigned long   specifiedValueRaw; /* The specified value in raw mode */
88
   unsigned long   specifiedValueRaw; /* The specified value in raw mode */
87
   double          specifiedValuePercent; /* The specified value in percent */
89
   double          specifiedValuePercent; /* The specified value in percent */
88
-  
90
+
89
   LIGHT_TARGET    target;
91
   LIGHT_TARGET    target;
90
 } light_runtimeArguments, *light_runtimeArguments_p;
92
 } light_runtimeArguments, *light_runtimeArguments_p;
91
 
93
 

+ 50
- 19
src/light.c View File

31
 
31
 
32
   unsigned long specLen = 0;
32
   unsigned long specLen = 0;
33
 
33
 
34
-  while((currFlag = getopt(argc, argv, "HhVGSAULIObmcas:prv:")) != -1)
34
+  while((currFlag = getopt(argc, argv, "HhVGSAULIObmckas:prv:")) != -1)
35
   {
35
   {
36
     switch(currFlag)
36
     switch(currFlag)
37
     {
37
     {
87
         ASSERT_TARGETSET();
87
         ASSERT_TARGETSET();
88
         light_Configuration.target = LIGHT_MIN_CAP;
88
         light_Configuration.target = LIGHT_MIN_CAP;
89
         break;
89
         break;
90
+      case 'k':
91
+	      ASSERT_TARGETSET();
92
+	      light_Configuration.target = LIGHT_KEYBOARD;
90
 
93
 
91
       /* -- Controller selection -- */
94
       /* -- Controller selection -- */
92
       case 'a':
95
       case 'a':
201
   printf("  -L:\t\tList controllers\n");
204
   printf("  -L:\t\tList controllers\n");
202
   printf("  -I:\t\tRestore brightness\n");
205
   printf("  -I:\t\tRestore brightness\n");
203
   printf("  -O:\t\tSave brightness\n\n");
206
   printf("  -O:\t\tSave brightness\n\n");
204
-  
207
+
205
   printf("Targets (can not be used in conjunction):\n");
208
   printf("Targets (can not be used in conjunction):\n");
206
   printf("  -b:\t\tBrightness (default)\n  \t\tUsed with [GSAU]\n\n");
209
   printf("  -b:\t\tBrightness (default)\n  \t\tUsed with [GSAU]\n\n");
207
   printf("  -m:\t\tMaximum brightness\n  \t\tUsed with [G]\n\n");
210
   printf("  -m:\t\tMaximum brightness\n  \t\tUsed with [G]\n\n");
208
   printf("  -c:\t\tMinimum cap\n  \t\tUsed with [GS]\n");
211
   printf("  -c:\t\tMinimum cap\n  \t\tUsed with [GS]\n");
212
+  printf("  -k:\t\tSet keyboard brightness instead of display brightness \n \t\tUsed with [GSAU]");
209
   printf("  \t\tG returns null if no minimum cap is set.\n\n");
213
   printf("  \t\tG returns null if no minimum cap is set.\n\n");
210
 
214
 
211
   printf("Controller selection (can not be used in conjunction):\n");
215
   printf("Controller selection (can not be used in conjunction):\n");
215
   printf("Value modes (can not be used in conjunction):\n");
219
   printf("Value modes (can not be used in conjunction):\n");
216
   printf("  -p:\t\tInterpret <value> as, and output values in, percent. (default)\n");
220
   printf("  -p:\t\tInterpret <value> as, and output values in, percent. (default)\n");
217
   printf("  -r:\t\tInterpret <value> as, and output values in, raw mode.\n\n");
221
   printf("  -r:\t\tInterpret <value> as, and output values in, raw mode.\n\n");
218
-  
222
+
219
   printf("Other:\n");
223
   printf("Other:\n");
220
   printf("  -v:\t\tSets the verbosity level, (needs argument).\n  \t\t0: Only outputs read values.\n  \t\t1: Read values, Errors.\n  \t\t2: Read values, Errors, Warnings.\n  \t\t3: Read values, Errors, Warnings, Notices.\n\n");
224
   printf("  -v:\t\tSets the verbosity level, (needs argument).\n  \t\t0: Only outputs read values.\n  \t\t1: Read values, Errors.\n  \t\t2: Read values, Errors, Warnings.\n  \t\t3: Read values, Errors, Warnings, Notices.\n\n");
221
 }
225
 }
275
     LIGHT_ERR("selected controller is not valid, make sure this application is run as root.");
279
     LIGHT_ERR("selected controller is not valid, make sure this application is run as root.");
276
     return FALSE;
280
     return FALSE;
277
   }
281
   }
278
-  
282
+
279
 
283
 
280
   return TRUE;
284
   return TRUE;
281
 }
285
 }
285
   unsigned long rawCurr; /* The current brightness, in raw mode */
289
   unsigned long rawCurr; /* The current brightness, in raw mode */
286
   double    percentCurr; /* The current brightness, in percent  */
290
   double    percentCurr; /* The current brightness, in percent  */
287
   unsigned long  rawMax; /* The max brightness, in percent      */
291
   unsigned long  rawMax; /* The max brightness, in percent      */
288
-  
292
+
289
   unsigned long  rawSetP; /* The final value to be set, in raw mode, when setting with percent */
293
   unsigned long  rawSetP; /* The final value to be set, in raw mode, when setting with percent */
290
   unsigned long  rawAddP; /* The final value to be set, in raw mode, when adding with percent */
294
   unsigned long  rawAddP; /* The final value to be set, in raw mode, when adding with percent */
291
   unsigned long  rawSubP; /* The final value to be set, in raw mode, when subtracting with percent */
295
   unsigned long  rawSubP; /* The final value to be set, in raw mode, when subtracting with percent */
351
 
355
 
352
   /* -- Secondly, calculate the rest of the values (Clamp them here as well!) */
356
   /* -- Secondly, calculate the rest of the values (Clamp them here as well!) */
353
   percentCurr = LIGHT_CLAMP( ((double)rawCurr) /  ((double)rawMax) * 100 , 0.00, 100.00 );
357
   percentCurr = LIGHT_CLAMP( ((double)rawCurr) /  ((double)rawMax) * 100 , 0.00, 100.00 );
354
-  percentMinCap = LIGHT_CLAMP( ((double)minCap) / ((double)rawMax) * 100 , 0.00, 100.00 );  
358
+  percentMinCap = LIGHT_CLAMP( ((double)minCap) / ((double)rawMax) * 100 , 0.00, 100.00 );
355
 
359
 
356
   rawSetP     = LIGHT_CLAMP( ((unsigned long) (light_Configuration.specifiedValuePercent * ((double)rawMax) ) / 100) , minCap, rawMax );
360
   rawSetP     = LIGHT_CLAMP( ((unsigned long) (light_Configuration.specifiedValuePercent * ((double)rawMax) ) / 100) , minCap, rawMax );
357
   rawAddP     = LIGHT_CLAMP( ((unsigned long) ( (percentCurr + light_Configuration.specifiedValuePercent) * ((double)rawMax)) / 100) , minCap, rawMax );
361
   rawAddP     = LIGHT_CLAMP( ((unsigned long) ( (percentCurr + light_Configuration.specifiedValuePercent) * ((double)rawMax)) / 100) , minCap, rawMax );
365
 
369
 
366
   rawSetR     = LIGHT_CLAMP( light_Configuration.specifiedValueRaw , minCap, rawMax );
370
   rawSetR     = LIGHT_CLAMP( light_Configuration.specifiedValueRaw , minCap, rawMax );
367
   rawAddR     = LIGHT_CLAMP( rawCurr + light_Configuration.specifiedValueRaw , minCap, rawMax );
371
   rawAddR     = LIGHT_CLAMP( rawCurr + light_Configuration.specifiedValueRaw , minCap, rawMax );
368
-  
372
+
369
   if(light_Configuration.specifiedValueRaw > rawCurr){
373
   if(light_Configuration.specifiedValueRaw > rawCurr){
370
     rawSubR     = LIGHT_CLAMP(0, minCap, rawMax)
374
     rawSubR     = LIGHT_CLAMP(0, minCap, rawMax)
371
   }else{
375
   }else{
374
 
378
 
375
   minCapP     = LIGHT_CLAMP(((unsigned long) (light_Configuration.specifiedValuePercent * ((double)rawMax) ) / 100), 0, rawMax);
379
   minCapP     = LIGHT_CLAMP(((unsigned long) (light_Configuration.specifiedValuePercent * ((double)rawMax) ) / 100), 0, rawMax);
376
   minCapR     = LIGHT_CLAMP( light_Configuration.specifiedValueRaw, 0, rawMax );
380
   minCapR     = LIGHT_CLAMP( light_Configuration.specifiedValueRaw, 0, rawMax );
377
-  
381
+
378
   /* Handle get operations */
382
   /* Handle get operations */
379
   if(light_Configuration.operationMode == LIGHT_GET)
383
   if(light_Configuration.operationMode == LIGHT_GET)
380
   {
384
   {
390
         break;
394
         break;
391
       case LIGHT_SAVERESTORE:
395
       case LIGHT_SAVERESTORE:
392
         break;
396
         break;
397
+      case LIGHT_KEYBOARD:
398
+        (light_Configuration.valueMode == LIGHT_RAW) ? printf("%lu\n", rawCurr) : printf("%.2f\n", percentCurr);
399
+        break;
400
+      case LIGHT_KEYBOARD_MAX_BRIGHTNESS:
401
+        (light_Configuration.valueMode == LIGHT_RAW) ? printf("%lu\n", rawMax) : printf("100.00\n"); /* <- I know how stupid it is but it might just make someones life easier */
402
+        break;
393
     }
403
     }
394
 
404
 
395
     return TRUE;
405
     return TRUE;
397
 
407
 
398
   /* Handle set/add/sub operations */
408
   /* Handle set/add/sub operations */
399
   if(light_Configuration.operationMode == LIGHT_SET ||
409
   if(light_Configuration.operationMode == LIGHT_SET ||
400
-     light_Configuration.operationMode == LIGHT_ADD || 
410
+     light_Configuration.operationMode == LIGHT_ADD ||
401
      light_Configuration.operationMode == LIGHT_SUB)
411
      light_Configuration.operationMode == LIGHT_SUB)
402
   {
412
   {
403
 
413
 
426
       /* All good? Return true. */
436
       /* All good? Return true. */
427
       return TRUE;
437
       return TRUE;
428
 
438
 
429
-    }else if(light_Configuration.target == LIGHT_BRIGHTNESS){
439
+    }else if(light_Configuration.target == LIGHT_BRIGHTNESS || light_Configuration.target == LIGHT_KEYBOARD){
430
       /* Handle brightness writing */
440
       /* Handle brightness writing */
431
 
441
 
432
       /* Point our writevalue according to configuration */
442
       /* Point our writevalue according to configuration */
516
   }
526
   }
517
 
527
 
518
   memset(returner, '\0', 256);
528
   memset(returner, '\0', 256);
519
-  
529
+
520
   switch(type)
530
   switch(type)
521
   {
531
   {
522
     case LIGHT_BRIGHTNESS:
532
     case LIGHT_BRIGHTNESS:
531
     case LIGHT_SAVERESTORE:
541
     case LIGHT_SAVERESTORE:
532
       spfVal = sprintf(returner, "/etc/light/save/%s", controller);
542
       spfVal = sprintf(returner, "/etc/light/save/%s", controller);
533
       break;
543
       break;
544
+    case LIGHT_KEYBOARD:
545
+      spfVal = sprintf(returner, "/sys/class/leds/%s/brightness", controller);
546
+      break;
547
+    case LIGHT_KEYBOARD_MAX_BRIGHTNESS:
548
+      spfVal = sprintf(returner, "/sys/class/leds/%s/max_brightness", controller);
549
+      break;
534
   }
550
   }
535
 
551
 
536
   if(spfVal < 0)
552
   if(spfVal < 0)
551
   char *brightnessPath = NULL;
567
   char *brightnessPath = NULL;
552
   LIGHT_BOOL readVal = FALSE;
568
   LIGHT_BOOL readVal = FALSE;
553
 
569
 
554
-  if(!light_genPath(controller, LIGHT_BRIGHTNESS, &brightnessPath))
570
+  if(!light_genPath(controller, light_Configuration.target, &brightnessPath))
555
   {
571
   {
556
     LIGHT_ERR("could not generate path to brightness file");
572
     LIGHT_ERR("could not generate path to brightness file");
557
     return FALSE;
573
     return FALSE;
558
   }
574
   }
559
-
575
+  LIGHT_NOTE(brightnessPath)
560
   readVal = light_readULong( brightnessPath , v);
576
   readVal = light_readULong( brightnessPath , v);
561
   free(brightnessPath);
577
   free(brightnessPath);
562
 
578
 
574
   char *maxPath;
590
   char *maxPath;
575
   LIGHT_BOOL readVal = FALSE;
591
   LIGHT_BOOL readVal = FALSE;
576
 
592
 
577
-  if(!light_genPath(controller, LIGHT_MAX_BRIGHTNESS, &maxPath))
593
+  LIGHT_TARGET target;
594
+  if(light_Configuration.target == LIGHT_KEYBOARD)
595
+  {
596
+    target = LIGHT_KEYBOARD_MAX_BRIGHTNESS;
597
+  }
598
+  else
599
+  {
600
+    target = LIGHT_MAX_BRIGHTNESS;
601
+  }
602
+
603
+  if(!light_genPath(controller, target, &maxPath))
578
   {
604
   {
579
     LIGHT_ERR("could not generate path to maximum brightness file");
605
     LIGHT_ERR("could not generate path to maximum brightness file");
580
     return FALSE;
606
     return FALSE;
603
   char *brightnessPath = NULL;
629
   char *brightnessPath = NULL;
604
   LIGHT_BOOL writeVal = FALSE;
630
   LIGHT_BOOL writeVal = FALSE;
605
 
631
 
606
-  if(!light_genPath(controller, LIGHT_BRIGHTNESS, &brightnessPath))
632
+  if(!light_genPath(controller, light_Configuration.target, &brightnessPath))
607
   {
633
   {
608
     LIGHT_ERR("could not generate path to brightness file");
634
     LIGHT_ERR("could not generate path to brightness file");
609
     return FALSE;
635
     return FALSE;
627
 
653
 
628
   if(!light_getBrightness(controller, &dummy))
654
   if(!light_getBrightness(controller, &dummy))
629
   {
655
   {
656
+    LIGHT_NOTE(controller)
630
     LIGHT_WARN("could not read controllers brightness file, so controller is not accessible")
657
     LIGHT_WARN("could not read controllers brightness file, so controller is not accessible")
631
     return FALSE;
658
     return FALSE;
632
   }
659
   }
637
     return FALSE;
664
     return FALSE;
638
   }
665
   }
639
 
666
 
640
-  if(!light_genPath(controller, LIGHT_BRIGHTNESS, &brightnessPath))
667
+  if(!light_genPath(controller, light_Configuration.target, &brightnessPath))
641
   {
668
   {
642
     LIGHT_ERR("could not generate path to brightness file");
669
     LIGHT_ERR("could not generate path to brightness file");
643
     return FALSE;
670
     return FALSE;
661
   if(light_iteratorDir == NULL)
688
   if(light_iteratorDir == NULL)
662
   {
689
   {
663
     light_iteratorDir = opendir("/sys/class/backlight");
690
     light_iteratorDir = opendir("/sys/class/backlight");
691
+    if(light_Configuration.target == LIGHT_KEYBOARD)
692
+    {
693
+      light_iteratorDir = opendir("/sys/class/leds");
694
+    }
664
     if(light_iteratorDir == NULL)
695
     if(light_iteratorDir == NULL)
665
     {
696
     {
666
-      LIGHT_ERR("could not open /sys/class/backlight directory");
697
+      LIGHT_ERR("could not open backlight or leds directory");
667
       return FALSE;
698
       return FALSE;
668
     }
699
     }
669
   }
700
   }
706
 
737
 
707
     if(light_getMaxBrightness(light_currentController, &currVal))
738
     if(light_getMaxBrightness(light_currentController, &currVal))
708
     {
739
     {
709
-      
740
+
710
       if(light_controllerAccessible(light_currentController))
741
       if(light_controllerAccessible(light_currentController))
711
       {
742
       {
712
         if(currVal > bestValYet)
743
         if(currVal > bestValYet)
747
 LIGHT_BOOL light_getMinCap(char const * controller, LIGHT_BOOL * hasMinCap, unsigned long * minCap)
778
 LIGHT_BOOL light_getMinCap(char const * controller, LIGHT_BOOL * hasMinCap, unsigned long * minCap)
748
 {
779
 {
749
  char * mincapPath = NULL;
780
  char * mincapPath = NULL;
750
- 
781
+
751
  if(!light_genPath(controller, LIGHT_MIN_CAP, &mincapPath))
782
  if(!light_genPath(controller, LIGHT_MIN_CAP, &mincapPath))
752
  {
783
  {
753
     LIGHT_ERR("could not generate path to minimum cap file");
784
     LIGHT_ERR("could not generate path to minimum cap file");