Browse Source

Fix wrong behavior of 'light_controllerAccessible'

This function was:
- Reading values from the controller
- Checking write permission even when we just want reading values
- Checking the mincap file instead of the actual controller

The last one is also true for other functions using
'light_getBrightness'.
This commit fixes that.
Abdullah ibn Nadjo 7 years ago
parent
commit
9f9d6ae447
1 changed files with 59 additions and 20 deletions
  1. 59
    20
      src/light.c

+ 59
- 20
src/light.c View File

569
   return TRUE;
569
   return TRUE;
570
 }
570
 }
571
 
571
 
572
+LIGHT_BOOL light_getBrightnessPath(char const *controller, char **path)
573
+{
574
+  LIGHT_TARGET target;
575
+
576
+  if(light_Configuration.target == LIGHT_MIN_CAP)
577
+  {
578
+    target = LIGHT_BRIGHTNESS;
579
+  }
580
+  else
581
+  {
582
+    target = light_Configuration.target;
583
+  }
584
+
585
+  if(!light_genPath(controller, target, path))
586
+  {
587
+    LIGHT_ERR("could not generate path to brightness file");
588
+    return FALSE;
589
+  }
590
+  return TRUE;
591
+}
592
+
572
 LIGHT_BOOL light_getBrightness(char const *controller, unsigned long *v)
593
 LIGHT_BOOL light_getBrightness(char const *controller, unsigned long *v)
573
 {
594
 {
574
   char *brightnessPath = NULL;
595
   char *brightnessPath = NULL;
575
   LIGHT_BOOL readVal = FALSE;
596
   LIGHT_BOOL readVal = FALSE;
576
 
597
 
577
-  if(!light_genPath(controller, light_Configuration.target, &brightnessPath))
598
+  if(!light_getBrightnessPath(controller, &brightnessPath))
578
   {
599
   {
579
-    LIGHT_ERR("could not generate path to brightness file");
580
     return FALSE;
600
     return FALSE;
581
   }
601
   }
582
   LIGHT_NOTE(brightnessPath)
602
   LIGHT_NOTE(brightnessPath)
592
   return TRUE;
612
   return TRUE;
593
 }
613
 }
594
 
614
 
595
-LIGHT_BOOL light_getMaxBrightness(char const *controller, unsigned long *v)
615
+LIGHT_BOOL light_getMaxBrightnessPath(char const *controller, char **path)
596
 {
616
 {
597
-  char *maxPath;
598
-  LIGHT_BOOL readVal = FALSE;
599
-
600
   LIGHT_TARGET target;
617
   LIGHT_TARGET target;
618
+
601
   if(light_Configuration.target == LIGHT_KEYBOARD)
619
   if(light_Configuration.target == LIGHT_KEYBOARD)
602
   {
620
   {
603
     target = LIGHT_KEYBOARD_MAX_BRIGHTNESS;
621
     target = LIGHT_KEYBOARD_MAX_BRIGHTNESS;
607
     target = LIGHT_MAX_BRIGHTNESS;
625
     target = LIGHT_MAX_BRIGHTNESS;
608
   }
626
   }
609
 
627
 
610
-  if(!light_genPath(controller, target, &maxPath))
628
+  if(!light_genPath(controller, target, path))
611
   {
629
   {
612
     LIGHT_ERR("could not generate path to maximum brightness file");
630
     LIGHT_ERR("could not generate path to maximum brightness file");
613
     return FALSE;
631
     return FALSE;
614
   }
632
   }
633
+  return TRUE;
634
+}
615
 
635
 
636
+LIGHT_BOOL light_getMaxBrightness(char const *controller, unsigned long *v)
637
+{
638
+  char *maxPath = NULL;
639
+  LIGHT_BOOL readVal = FALSE;
640
+
641
+  if (!light_getMaxBrightnessPath(controller, &maxPath))
642
+  {
643
+    return FALSE;
644
+  }
616
   readVal = light_readULong(maxPath , v);
645
   readVal = light_readULong(maxPath , v);
617
   free(maxPath);
646
   free(maxPath);
618
 
647
 
656
 LIGHT_BOOL light_controllerAccessible(char const *controller)
685
 LIGHT_BOOL light_controllerAccessible(char const *controller)
657
 {
686
 {
658
   char *brightnessPath = NULL;
687
   char *brightnessPath = NULL;
659
-  unsigned long dummy;
660
 
688
 
661
-  if(!light_getBrightness(controller, &dummy))
689
+  /* On auto mode, we need to check if we can read the max brightness value
690
+     of the controller for later computation */
691
+  if(light_Configuration.controllerMode == LIGHT_AUTO ||
692
+     light_Configuration.target == LIGHT_MAX_BRIGHTNESS)
662
   {
693
   {
663
-    LIGHT_NOTE(controller)
664
-    LIGHT_WARN("could not read controllers brightness file, so controller is not accessible")
665
-    return FALSE;
666
-  }
667
-
668
-  if(!light_getMaxBrightness(controller, &dummy))
669
-  {
670
-    LIGHT_WARN("could not read controller max brightness file, so controller is not accessible")
671
-    return FALSE;
694
+    if(!light_getMaxBrightnessPath(controller, &brightnessPath))
695
+    {
696
+      return FALSE;
697
+    }
698
+    if(!light_isReadable(brightnessPath))
699
+    {
700
+      LIGHT_WARN("could not open controller max brightness file for reading, so controller is not accessible");
701
+      free(brightnessPath);
702
+      return FALSE;
703
+    }
704
+    free(brightnessPath);
672
   }
705
   }
673
 
706
 
674
-  if(!light_genPath(controller, light_Configuration.target, &brightnessPath))
707
+  if(!light_getBrightnessPath(controller, &brightnessPath))
675
   {
708
   {
676
-    LIGHT_ERR("could not generate path to brightness file");
677
     return FALSE;
709
     return FALSE;
678
   }
710
   }
679
 
711
 
680
   if(light_Configuration.operationMode != LIGHT_GET &&
712
   if(light_Configuration.operationMode != LIGHT_GET &&
713
+     light_Configuration.target != LIGHT_MIN_CAP &&
681
      !light_isWritable(brightnessPath))
714
      !light_isWritable(brightnessPath))
682
   {
715
   {
683
     LIGHT_WARN("could not open controller brightness file for writing, so controller is not accessible");
716
     LIGHT_WARN("could not open controller brightness file for writing, so controller is not accessible");
684
     free(brightnessPath);
717
     free(brightnessPath);
685
     return FALSE;
718
     return FALSE;
686
   }
719
   }
720
+  else if (!light_isReadable(brightnessPath))
721
+  {
722
+    LIGHT_WARN("could not open controller brightness file for reading, so controller is not accessible");
723
+    free(brightnessPath);
724
+    return FALSE;
725
+  }
687
 
726
 
688
   free(brightnessPath);
727
   free(brightnessPath);
689
   return TRUE;
728
   return TRUE;