|  | @@ -0,0 +1,703 @@
 | 
	
		
			
			|  | 1 | +#include "light.h"
 | 
	
		
			
			|  | 2 | +
 | 
	
		
			
			|  | 3 | +#include <stdlib.h>
 | 
	
		
			
			|  | 4 | +#include <stdio.h>
 | 
	
		
			
			|  | 5 | +#include <string.h>
 | 
	
		
			
			|  | 6 | +#include <unistd.h>
 | 
	
		
			
			|  | 7 | +#include <getopt.h>
 | 
	
		
			
			|  | 8 | +
 | 
	
		
			
			|  | 9 | +void light_defaultConfig()
 | 
	
		
			
			|  | 10 | +{
 | 
	
		
			
			|  | 11 | +  light_Configuration.controllerMode         = LIGHT_AUTO;
 | 
	
		
			
			|  | 12 | +  memset(&light_Configuration.specifiedController, '\0', 256);
 | 
	
		
			
			|  | 13 | +  light_Configuration.operationMode          = LIGHT_GET;
 | 
	
		
			
			|  | 14 | +  light_Configuration.valueMode              = LIGHT_PERCENT;
 | 
	
		
			
			|  | 15 | +  light_Configuration.specifiedValueRaw      = 0;
 | 
	
		
			
			|  | 16 | +  light_Configuration.specifiedValuePercent  = 0.0;
 | 
	
		
			
			|  | 17 | +  light_Configuration.target                 = LIGHT_BRIGHTNESS;
 | 
	
		
			
			|  | 18 | +  light_verbosity                            = 0;
 | 
	
		
			
			|  | 19 | +}
 | 
	
		
			
			|  | 20 | +
 | 
	
		
			
			|  | 21 | +LIGHT_BOOL light_parseArguments(int argc, char** argv)
 | 
	
		
			
			|  | 22 | +{
 | 
	
		
			
			|  | 23 | +  int currFlag;
 | 
	
		
			
			|  | 24 | +
 | 
	
		
			
			|  | 25 | +  LIGHT_BOOL opSet = FALSE;
 | 
	
		
			
			|  | 26 | +  LIGHT_BOOL targetSet = FALSE;
 | 
	
		
			
			|  | 27 | +  LIGHT_BOOL ctrlSet = FALSE;
 | 
	
		
			
			|  | 28 | +  LIGHT_BOOL valSet = FALSE;
 | 
	
		
			
			|  | 29 | +
 | 
	
		
			
			|  | 30 | +  unsigned long specLen = 0;
 | 
	
		
			
			|  | 31 | +
 | 
	
		
			
			|  | 32 | +  while((currFlag = getopt(argc, argv, "HhVGSAUbmcas:prv:")) != -1)
 | 
	
		
			
			|  | 33 | +  {
 | 
	
		
			
			|  | 34 | +    switch(currFlag)
 | 
	
		
			
			|  | 35 | +    {
 | 
	
		
			
			|  | 36 | +      /* -- Operations -- */
 | 
	
		
			
			|  | 37 | +      case 'H':
 | 
	
		
			
			|  | 38 | +      case 'h':
 | 
	
		
			
			|  | 39 | +        ASSERT_OPSET();
 | 
	
		
			
			|  | 40 | +        light_Configuration.operationMode = LIGHT_PRINT_HELP;
 | 
	
		
			
			|  | 41 | +        break;
 | 
	
		
			
			|  | 42 | +      case 'V':
 | 
	
		
			
			|  | 43 | +        ASSERT_OPSET();
 | 
	
		
			
			|  | 44 | +        light_Configuration.operationMode = LIGHT_PRINT_VERSION;
 | 
	
		
			
			|  | 45 | +        break;
 | 
	
		
			
			|  | 46 | +      case 'G':
 | 
	
		
			
			|  | 47 | +        ASSERT_OPSET();
 | 
	
		
			
			|  | 48 | +        light_Configuration.operationMode = LIGHT_GET;
 | 
	
		
			
			|  | 49 | +        break;
 | 
	
		
			
			|  | 50 | +      case 'S':
 | 
	
		
			
			|  | 51 | +        ASSERT_OPSET();
 | 
	
		
			
			|  | 52 | +        light_Configuration.operationMode = LIGHT_SET;
 | 
	
		
			
			|  | 53 | +        break;
 | 
	
		
			
			|  | 54 | +      case 'A':
 | 
	
		
			
			|  | 55 | +        ASSERT_OPSET();
 | 
	
		
			
			|  | 56 | +        light_Configuration.operationMode = LIGHT_ADD;
 | 
	
		
			
			|  | 57 | +        break;
 | 
	
		
			
			|  | 58 | +      case 'U':
 | 
	
		
			
			|  | 59 | +        ASSERT_OPSET();
 | 
	
		
			
			|  | 60 | +        light_Configuration.operationMode = LIGHT_SUB;
 | 
	
		
			
			|  | 61 | +        break;
 | 
	
		
			
			|  | 62 | +
 | 
	
		
			
			|  | 63 | +      /* -- Targets -- */
 | 
	
		
			
			|  | 64 | +      case 'b':
 | 
	
		
			
			|  | 65 | +        ASSERT_TARGETSET();
 | 
	
		
			
			|  | 66 | +        light_Configuration.target = LIGHT_BRIGHTNESS;
 | 
	
		
			
			|  | 67 | +        break;
 | 
	
		
			
			|  | 68 | +      case 'm':
 | 
	
		
			
			|  | 69 | +        ASSERT_TARGETSET();
 | 
	
		
			
			|  | 70 | +        light_Configuration.target = LIGHT_MAX_BRIGHTNESS;
 | 
	
		
			
			|  | 71 | +        break;
 | 
	
		
			
			|  | 72 | +      case 'c':
 | 
	
		
			
			|  | 73 | +        ASSERT_TARGETSET();
 | 
	
		
			
			|  | 74 | +        light_Configuration.target = LIGHT_MIN_CAP;
 | 
	
		
			
			|  | 75 | +        break;
 | 
	
		
			
			|  | 76 | +
 | 
	
		
			
			|  | 77 | +      /* -- Controller selection -- */
 | 
	
		
			
			|  | 78 | +      case 'a':
 | 
	
		
			
			|  | 79 | +        ASSERT_CTRLSET();
 | 
	
		
			
			|  | 80 | +        light_Configuration.controllerMode = LIGHT_AUTO;
 | 
	
		
			
			|  | 81 | +        break;;
 | 
	
		
			
			|  | 82 | +      case 's':
 | 
	
		
			
			|  | 83 | +        ASSERT_CTRLSET();
 | 
	
		
			
			|  | 84 | +        light_Configuration.controllerMode = LIGHT_SPECIFY;
 | 
	
		
			
			|  | 85 | +        if(optarg == NULL)
 | 
	
		
			
			|  | 86 | +        {
 | 
	
		
			
			|  | 87 | +          printf("-s NEEDS an argument.\n\n");
 | 
	
		
			
			|  | 88 | +          light_printHelp();
 | 
	
		
			
			|  | 89 | +        }
 | 
	
		
			
			|  | 90 | +
 | 
	
		
			
			|  | 91 | +        specLen = strlen(optarg);
 | 
	
		
			
			|  | 92 | +        if(specLen > 255)
 | 
	
		
			
			|  | 93 | +        {
 | 
	
		
			
			|  | 94 | +          specLen = 255;
 | 
	
		
			
			|  | 95 | +        }
 | 
	
		
			
			|  | 96 | +
 | 
	
		
			
			|  | 97 | +        strncpy(light_Configuration.specifiedController, optarg, specLen);
 | 
	
		
			
			|  | 98 | +
 | 
	
		
			
			|  | 99 | +        light_Configuration.specifiedController[255] = '\0';
 | 
	
		
			
			|  | 100 | +        break;
 | 
	
		
			
			|  | 101 | +      /* -- Value modes -- */
 | 
	
		
			
			|  | 102 | +      case 'p':
 | 
	
		
			
			|  | 103 | +        ASSERT_VALSET();
 | 
	
		
			
			|  | 104 | +        light_Configuration.valueMode = LIGHT_PERCENT;
 | 
	
		
			
			|  | 105 | +        break;
 | 
	
		
			
			|  | 106 | +      case 'r':
 | 
	
		
			
			|  | 107 | +        ASSERT_VALSET();
 | 
	
		
			
			|  | 108 | +        light_Configuration.valueMode = LIGHT_RAW;
 | 
	
		
			
			|  | 109 | +        break;
 | 
	
		
			
			|  | 110 | +
 | 
	
		
			
			|  | 111 | +      /* -- Other -- */
 | 
	
		
			
			|  | 112 | +      case 'v':
 | 
	
		
			
			|  | 113 | +        if(optarg == NULL)
 | 
	
		
			
			|  | 114 | +        {
 | 
	
		
			
			|  | 115 | +          printf("-v NEEDS an argument.\n\n");
 | 
	
		
			
			|  | 116 | +          light_printHelp();
 | 
	
		
			
			|  | 117 | +          return FALSE;
 | 
	
		
			
			|  | 118 | +        }
 | 
	
		
			
			|  | 119 | +        if(sscanf(optarg, "%i", &light_verbosity) != 1)
 | 
	
		
			
			|  | 120 | +        {
 | 
	
		
			
			|  | 121 | +          printf("-v Verbosity is not specified in a recognizable format.\n\n");
 | 
	
		
			
			|  | 122 | +          light_printHelp();
 | 
	
		
			
			|  | 123 | +          return FALSE;
 | 
	
		
			
			|  | 124 | +        }
 | 
	
		
			
			|  | 125 | +        if(light_verbosity < 0 || light_verbosity > 3)
 | 
	
		
			
			|  | 126 | +        {
 | 
	
		
			
			|  | 127 | +          printf("-v Verbosity has to be between 0 and 3.\n\n");
 | 
	
		
			
			|  | 128 | +          light_printHelp();
 | 
	
		
			
			|  | 129 | +          return FALSE;
 | 
	
		
			
			|  | 130 | +        }
 | 
	
		
			
			|  | 131 | +        break;
 | 
	
		
			
			|  | 132 | +    }
 | 
	
		
			
			|  | 133 | +  }
 | 
	
		
			
			|  | 134 | +
 | 
	
		
			
			|  | 135 | +  /* If we need a <value> (for writing), make sure we have it! */
 | 
	
		
			
			|  | 136 | +  if(light_Configuration.operationMode == LIGHT_SET ||
 | 
	
		
			
			|  | 137 | +     light_Configuration.operationMode == LIGHT_ADD ||
 | 
	
		
			
			|  | 138 | +     light_Configuration.operationMode == LIGHT_SUB)
 | 
	
		
			
			|  | 139 | +  {
 | 
	
		
			
			|  | 140 | +    if(argc - optind != 1)
 | 
	
		
			
			|  | 141 | +    {
 | 
	
		
			
			|  | 142 | +      printf("Light needs an argument for <value>.\n\n");
 | 
	
		
			
			|  | 143 | +      light_printHelp();
 | 
	
		
			
			|  | 144 | +      return FALSE;
 | 
	
		
			
			|  | 145 | +    }
 | 
	
		
			
			|  | 146 | +
 | 
	
		
			
			|  | 147 | +    if(light_Configuration.valueMode == LIGHT_PERCENT)
 | 
	
		
			
			|  | 148 | +    {
 | 
	
		
			
			|  | 149 | +      if(sscanf(argv[optind], "%lf", &light_Configuration.specifiedValuePercent) != 1){
 | 
	
		
			
			|  | 150 | +        printf("<value> is not specified in a recognizable format.\n\n");
 | 
	
		
			
			|  | 151 | +        light_printHelp();
 | 
	
		
			
			|  | 152 | +        return FALSE;
 | 
	
		
			
			|  | 153 | +      }
 | 
	
		
			
			|  | 154 | +      light_Configuration.specifiedValuePercent = LIGHT_CLAMP(light_Configuration.specifiedValuePercent, 0.00, 100.00);
 | 
	
		
			
			|  | 155 | +    }else{
 | 
	
		
			
			|  | 156 | +      if(sscanf(argv[optind], "%lu", &light_Configuration.specifiedValueRaw) != 1){
 | 
	
		
			
			|  | 157 | +        printf("<value> is not specified in a recognizable format.\n\n");
 | 
	
		
			
			|  | 158 | +        light_printHelp();
 | 
	
		
			
			|  | 159 | +        return FALSE;
 | 
	
		
			
			|  | 160 | +      }
 | 
	
		
			
			|  | 161 | +    }
 | 
	
		
			
			|  | 162 | +
 | 
	
		
			
			|  | 163 | +  }
 | 
	
		
			
			|  | 164 | +
 | 
	
		
			
			|  | 165 | +  return TRUE;
 | 
	
		
			
			|  | 166 | +}
 | 
	
		
			
			|  | 167 | +
 | 
	
		
			
			|  | 168 | +void light_printVersion(){
 | 
	
		
			
			|  | 169 | +  printf("Light %u.%u (%s)\n", LIGHT_VER_MAJOR, LIGHT_VER_MINOR, LIGHT_VER_TYPE);
 | 
	
		
			
			|  | 170 | +  printf("Copyright (C) %u %s\n", LIGHT_YEAR, LIGHT_AUTHOR);
 | 
	
		
			
			|  | 171 | +  printf("This is free software, see the source for copying conditions.  There is NO\n");
 | 
	
		
			
			|  | 172 | +  printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE\n\n");
 | 
	
		
			
			|  | 173 | +}
 | 
	
		
			
			|  | 174 | +
 | 
	
		
			
			|  | 175 | +void light_printHelp(){
 | 
	
		
			
			|  | 176 | +  printf("Usage: light <options> <value>\n");
 | 
	
		
			
			|  | 177 | +  printf("<value> has to be either integral(raw mode) or decimal(percent mode) depending on the specified value mode.\n");
 | 
	
		
			
			|  | 178 | +  printf("<options> can be any of the following:\n\n");
 | 
	
		
			
			|  | 179 | +
 | 
	
		
			
			|  | 180 | +  printf("Operations (can not be used in conjunction):\n");
 | 
	
		
			
			|  | 181 | +  printf("  -H -h:\tPrints this help and exits\n");
 | 
	
		
			
			|  | 182 | +  printf("  -V:\t\tPrints version info and exits\n");
 | 
	
		
			
			|  | 183 | +  printf("  -G:\t\tGet value (default)\n");
 | 
	
		
			
			|  | 184 | +  printf("  -S:\t\tSet value\n");
 | 
	
		
			
			|  | 185 | +  printf("  -A:\t\tAdd value\n");
 | 
	
		
			
			|  | 186 | +  printf("  -U:\t\tSubtract value\n\n");
 | 
	
		
			
			|  | 187 | +  
 | 
	
		
			
			|  | 188 | +  printf("Targets (can not be used in conjunction):\n");
 | 
	
		
			
			|  | 189 | +  printf("  -b:\t\tBrightness (default)\n  \t\tUsed with [GSAU]\n\n");
 | 
	
		
			
			|  | 190 | +  printf("  -m:\t\tMaximum brightness\n  \t\tUsed with [G]\n\n");
 | 
	
		
			
			|  | 191 | +  printf("  -c:\t\tMinimum cap\n  \t\tUsed with [GS]\n");
 | 
	
		
			
			|  | 192 | +  printf("  \t\tG returns null if no minimum cap is set.\n\n");
 | 
	
		
			
			|  | 193 | +
 | 
	
		
			
			|  | 194 | +  printf("Controller selection (can not be used in conjunction):\n");
 | 
	
		
			
			|  | 195 | +  printf("  -a:\t\tSelects controller automatically (default).\n");
 | 
	
		
			
			|  | 196 | +  printf("  -s:\t\tSpecify controller to use. (needs argument)\n\n");
 | 
	
		
			
			|  | 197 | +
 | 
	
		
			
			|  | 198 | +  printf("Value modes (can not be used in conjunction):\n");
 | 
	
		
			
			|  | 199 | +  printf("  -p:\t\tInterpret <value> as, and output values in, percent. (default)\n");
 | 
	
		
			
			|  | 200 | +  printf("  -r:\t\tInterpret <value> as, and output values in, raw mode.\n\n");
 | 
	
		
			
			|  | 201 | +  
 | 
	
		
			
			|  | 202 | +  printf("Other:\n");
 | 
	
		
			
			|  | 203 | +  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");
 | 
	
		
			
			|  | 204 | +}
 | 
	
		
			
			|  | 205 | +
 | 
	
		
			
			|  | 206 | +LIGHT_BOOL light_initialize(int argc, char** argv)
 | 
	
		
			
			|  | 207 | +{
 | 
	
		
			
			|  | 208 | +  light_defaultConfig();
 | 
	
		
			
			|  | 209 | +  if(!light_parseArguments(argc, argv))
 | 
	
		
			
			|  | 210 | +  {
 | 
	
		
			
			|  | 211 | +    LIGHT_ERR("could not parse arguments");
 | 
	
		
			
			|  | 212 | +    return FALSE;
 | 
	
		
			
			|  | 213 | +  }
 | 
	
		
			
			|  | 214 | +
 | 
	
		
			
			|  | 215 | +  if(light_Configuration.operationMode == LIGHT_PRINT_HELP || light_Configuration.operationMode == LIGHT_PRINT_VERSION)
 | 
	
		
			
			|  | 216 | +  {
 | 
	
		
			
			|  | 217 | +      return TRUE;
 | 
	
		
			
			|  | 218 | +  }
 | 
	
		
			
			|  | 219 | +  
 | 
	
		
			
			|  | 220 | +  /* Make sure we have a valid controller before we proceed */
 | 
	
		
			
			|  | 221 | +  if(light_Configuration.controllerMode == LIGHT_AUTO)
 | 
	
		
			
			|  | 222 | +  {
 | 
	
		
			
			|  | 223 | +    LIGHT_NOTE("Automatic mode -- finding best controller");
 | 
	
		
			
			|  | 224 | +    if(!light_getBestController(light_Configuration.specifiedController))
 | 
	
		
			
			|  | 225 | +    {
 | 
	
		
			
			|  | 226 | +      LIGHT_ERR("could not find suitable controller");
 | 
	
		
			
			|  | 227 | +      return FALSE;
 | 
	
		
			
			|  | 228 | +    }
 | 
	
		
			
			|  | 229 | +  }
 | 
	
		
			
			|  | 230 | +
 | 
	
		
			
			|  | 231 | +  if(!light_controllerAccessible(light_Configuration.specifiedController))
 | 
	
		
			
			|  | 232 | +  {
 | 
	
		
			
			|  | 233 | +    LIGHT_ERR("selected controller is not valid, make sure this application is run as root.");
 | 
	
		
			
			|  | 234 | +    return FALSE;
 | 
	
		
			
			|  | 235 | +  }
 | 
	
		
			
			|  | 236 | +  
 | 
	
		
			
			|  | 237 | +
 | 
	
		
			
			|  | 238 | +  return TRUE;
 | 
	
		
			
			|  | 239 | +}
 | 
	
		
			
			|  | 240 | +
 | 
	
		
			
			|  | 241 | +LIGHT_BOOL light_execute()
 | 
	
		
			
			|  | 242 | +{
 | 
	
		
			
			|  | 243 | +  unsigned long rawCurr; /* The current brightness, in raw mode */
 | 
	
		
			
			|  | 244 | +  double    percentCurr; /* The current brightness, in percent  */
 | 
	
		
			
			|  | 245 | +  unsigned long  rawMax; /* The max brightness, in percent      */
 | 
	
		
			
			|  | 246 | +  
 | 
	
		
			
			|  | 247 | +  unsigned long  rawSetP; /* The final value to be set, in raw mode, when setting with percent */
 | 
	
		
			
			|  | 248 | +  unsigned long  rawAddP; /* The final value to be set, in raw mode, when adding with percent */
 | 
	
		
			
			|  | 249 | +  unsigned long  rawSubP; /* The final value to be set, in raw mode, when subtracting with percent */
 | 
	
		
			
			|  | 250 | +
 | 
	
		
			
			|  | 251 | +  unsigned long  rawSetR; /* The final value to be set, in raw mode, when setting with raw values */
 | 
	
		
			
			|  | 252 | +  unsigned long  rawAddR; /* The final value to be set, in raw mode, when adding with raw values */
 | 
	
		
			
			|  | 253 | +  unsigned long  rawSubR; /* The final value to be set, in raw mode, when subtracting with raw values */
 | 
	
		
			
			|  | 254 | +
 | 
	
		
			
			|  | 255 | +  unsigned long   minCap; /* The minimum cap, in raw mode */
 | 
	
		
			
			|  | 256 | +  double   percentMinCap; /* The minimum cap, in percent */
 | 
	
		
			
			|  | 257 | +  LIGHT_BOOL   hasMinCap; /* If we have a minimum cap     */
 | 
	
		
			
			|  | 258 | +  unsigned long  minCapP; /* The final value to be set, in raw mode, when setting with percent (This is uncapped and used for minimum cap setting) */
 | 
	
		
			
			|  | 259 | +  unsigned long  minCapR; /* The final value to be set, in raw mode, when setting with raw values (This is uncapped and used for minimum cap setting) */
 | 
	
		
			
			|  | 260 | +
 | 
	
		
			
			|  | 261 | +  unsigned long  *writeVal; /* The actual value used for writing */
 | 
	
		
			
			|  | 262 | +
 | 
	
		
			
			|  | 263 | +  /* Print help and version info */
 | 
	
		
			
			|  | 264 | +  if(light_Configuration.operationMode == LIGHT_PRINT_HELP)
 | 
	
		
			
			|  | 265 | +  {
 | 
	
		
			
			|  | 266 | +    light_printHelp();
 | 
	
		
			
			|  | 267 | +    return TRUE;
 | 
	
		
			
			|  | 268 | +  }
 | 
	
		
			
			|  | 269 | +
 | 
	
		
			
			|  | 270 | +  if(light_Configuration.operationMode == LIGHT_PRINT_VERSION)
 | 
	
		
			
			|  | 271 | +  {
 | 
	
		
			
			|  | 272 | +    light_printVersion();
 | 
	
		
			
			|  | 273 | +    return TRUE;
 | 
	
		
			
			|  | 274 | +  }
 | 
	
		
			
			|  | 275 | +
 | 
	
		
			
			|  | 276 | +  /* Prepare variables */
 | 
	
		
			
			|  | 277 | +
 | 
	
		
			
			|  | 278 | +  /* -- First, get the current, min and max values directly from controller/configuration (raw values) */
 | 
	
		
			
			|  | 279 | +  if(!light_getBrightness(light_Configuration.specifiedController, &rawCurr))
 | 
	
		
			
			|  | 280 | +  {
 | 
	
		
			
			|  | 281 | +    LIGHT_ERR("could not get brightness");
 | 
	
		
			
			|  | 282 | +    return FALSE;
 | 
	
		
			
			|  | 283 | +  }
 | 
	
		
			
			|  | 284 | +
 | 
	
		
			
			|  | 285 | +  if(!light_getMaxBrightness(light_Configuration.specifiedController, &rawMax))
 | 
	
		
			
			|  | 286 | +  {
 | 
	
		
			
			|  | 287 | +    LIGHT_ERR("could not get max brightness");
 | 
	
		
			
			|  | 288 | +    return FALSE;
 | 
	
		
			
			|  | 289 | +  }
 | 
	
		
			
			|  | 290 | +
 | 
	
		
			
			|  | 291 | +  if(!light_getMinCap(light_Configuration.specifiedController, &hasMinCap, &minCap))
 | 
	
		
			
			|  | 292 | +  {
 | 
	
		
			
			|  | 293 | +    LIGHT_ERR("could not get min brightness");
 | 
	
		
			
			|  | 294 | +    return FALSE;
 | 
	
		
			
			|  | 295 | +  }
 | 
	
		
			
			|  | 296 | +
 | 
	
		
			
			|  | 297 | +  if( hasMinCap && ( minCap < 0 || minCap > rawMax ) )
 | 
	
		
			
			|  | 298 | +  {
 | 
	
		
			
			|  | 299 | +    LIGHT_WARN("invalid minimum cap for controller, ignoring and using 0");
 | 
	
		
			
			|  | 300 | +    minCap = 0;
 | 
	
		
			
			|  | 301 | +  }
 | 
	
		
			
			|  | 302 | +
 | 
	
		
			
			|  | 303 | +  /* -- Secondly, calculate the rest of the values (Clamp them here as well!) */
 | 
	
		
			
			|  | 304 | +  percentCurr = LIGHT_CLAMP( ((double)rawCurr) /  ((double)rawMax) * 100 , 0.00, 100.00 );
 | 
	
		
			
			|  | 305 | +  percentMinCap = LIGHT_CLAMP( ((double)minCap) / ((double)rawMax) * 100 , 0.00, 100.00 );  
 | 
	
		
			
			|  | 306 | +
 | 
	
		
			
			|  | 307 | +  rawSetP     = LIGHT_CLAMP( ((unsigned long) (light_Configuration.specifiedValuePercent * ((double)rawMax) ) / 100) , minCap, rawMax );
 | 
	
		
			
			|  | 308 | +  rawAddP     = LIGHT_CLAMP( ((unsigned long) ( (percentCurr + light_Configuration.specifiedValuePercent) * ((double)rawMax)) / 100) , minCap, rawMax );
 | 
	
		
			
			|  | 309 | +  rawSubP     = LIGHT_CLAMP( ((unsigned long) ( (percentCurr - light_Configuration.specifiedValuePercent) * ((double)rawMax)) / 100) , minCap, rawMax );
 | 
	
		
			
			|  | 310 | +
 | 
	
		
			
			|  | 311 | +  rawSetR     = LIGHT_CLAMP( light_Configuration.specifiedValueRaw , minCap, rawMax );
 | 
	
		
			
			|  | 312 | +  rawAddR     = LIGHT_CLAMP( rawCurr + light_Configuration.specifiedValueRaw , minCap, rawMax );
 | 
	
		
			
			|  | 313 | +  rawSubR     = LIGHT_CLAMP( rawCurr - light_Configuration.specifiedValueRaw , minCap, rawMax );
 | 
	
		
			
			|  | 314 | +
 | 
	
		
			
			|  | 315 | +  minCapP     = LIGHT_CLAMP(((unsigned long) (light_Configuration.specifiedValuePercent * ((double)rawMax) ) / 100), 0, rawMax);
 | 
	
		
			
			|  | 316 | +  minCapR     = LIGHT_CLAMP( light_Configuration.specifiedValueRaw, 0, rawMax );
 | 
	
		
			
			|  | 317 | +  
 | 
	
		
			
			|  | 318 | +  /* Handle get operations */
 | 
	
		
			
			|  | 319 | +  if(light_Configuration.operationMode == LIGHT_GET)
 | 
	
		
			
			|  | 320 | +  {
 | 
	
		
			
			|  | 321 | +    switch(light_Configuration.target){
 | 
	
		
			
			|  | 322 | +      case LIGHT_BRIGHTNESS:
 | 
	
		
			
			|  | 323 | +        (light_Configuration.valueMode == LIGHT_RAW) ? printf("%lu\n", rawCurr) : printf("%.2f\n", percentCurr);
 | 
	
		
			
			|  | 324 | +        break;
 | 
	
		
			
			|  | 325 | +      case LIGHT_MAX_BRIGHTNESS:
 | 
	
		
			
			|  | 326 | +        (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 */
 | 
	
		
			
			|  | 327 | +        break;
 | 
	
		
			
			|  | 328 | +      case LIGHT_MIN_CAP:
 | 
	
		
			
			|  | 329 | +        (light_Configuration.valueMode == LIGHT_RAW) ? printf("%lu\n", minCap) : printf("%.2f\n", percentMinCap);
 | 
	
		
			
			|  | 330 | +        break;
 | 
	
		
			
			|  | 331 | +    }
 | 
	
		
			
			|  | 332 | +
 | 
	
		
			
			|  | 333 | +    return TRUE;
 | 
	
		
			
			|  | 334 | +  }
 | 
	
		
			
			|  | 335 | +
 | 
	
		
			
			|  | 336 | +  /* Handle set/add/sub operations */
 | 
	
		
			
			|  | 337 | +  if(light_Configuration.operationMode == LIGHT_SET ||
 | 
	
		
			
			|  | 338 | +     light_Configuration.operationMode == LIGHT_ADD || 
 | 
	
		
			
			|  | 339 | +     light_Configuration.operationMode == LIGHT_SUB)
 | 
	
		
			
			|  | 340 | +  {
 | 
	
		
			
			|  | 341 | +
 | 
	
		
			
			|  | 342 | +    /* Set the pointer we will use for write to a fallback (that shouldn't change anything) */
 | 
	
		
			
			|  | 343 | +    writeVal = &rawCurr;
 | 
	
		
			
			|  | 344 | +
 | 
	
		
			
			|  | 345 | +    if(light_Configuration.target == LIGHT_MIN_CAP)
 | 
	
		
			
			|  | 346 | +    {
 | 
	
		
			
			|  | 347 | +      /* Handle minimum cap files */
 | 
	
		
			
			|  | 348 | +
 | 
	
		
			
			|  | 349 | +      /* If we are not attempting to set, fail! */
 | 
	
		
			
			|  | 350 | +      if(light_Configuration.operationMode != LIGHT_SET)
 | 
	
		
			
			|  | 351 | +      {
 | 
	
		
			
			|  | 352 | +        printf("Minimum cap can only be used with get/set operations.\n");
 | 
	
		
			
			|  | 353 | +        return FALSE;
 | 
	
		
			
			|  | 354 | +      }
 | 
	
		
			
			|  | 355 | +
 | 
	
		
			
			|  | 356 | +      /* Point our writevalue and attempt to write*/
 | 
	
		
			
			|  | 357 | +      writeVal = (light_Configuration.valueMode == LIGHT_RAW) ? &minCapR : &minCapP;
 | 
	
		
			
			|  | 358 | +      if(!light_setMinCap(light_Configuration.specifiedController, *writeVal))
 | 
	
		
			
			|  | 359 | +      {
 | 
	
		
			
			|  | 360 | +        LIGHT_ERR("could not set minimum cap");
 | 
	
		
			
			|  | 361 | +        return FALSE;
 | 
	
		
			
			|  | 362 | +      }
 | 
	
		
			
			|  | 363 | +
 | 
	
		
			
			|  | 364 | +      /* All good? Return true. */
 | 
	
		
			
			|  | 365 | +      return TRUE;
 | 
	
		
			
			|  | 366 | +
 | 
	
		
			
			|  | 367 | +    }else if(light_Configuration.target == LIGHT_BRIGHTNESS){
 | 
	
		
			
			|  | 368 | +      /* Handle brightness writing */
 | 
	
		
			
			|  | 369 | +
 | 
	
		
			
			|  | 370 | +      /* Point our writevalue according to configuration */
 | 
	
		
			
			|  | 371 | +      switch(light_Configuration.operationMode)
 | 
	
		
			
			|  | 372 | +      {
 | 
	
		
			
			|  | 373 | +        case LIGHT_SET:
 | 
	
		
			
			|  | 374 | +          writeVal = (light_Configuration.valueMode == LIGHT_RAW) ? &rawSetR : &rawSetP;
 | 
	
		
			
			|  | 375 | +          break;
 | 
	
		
			
			|  | 376 | +        case LIGHT_ADD:
 | 
	
		
			
			|  | 377 | +          writeVal = (light_Configuration.valueMode == LIGHT_RAW) ? &rawAddR : &rawAddP;
 | 
	
		
			
			|  | 378 | +          break;
 | 
	
		
			
			|  | 379 | +        case LIGHT_SUB:
 | 
	
		
			
			|  | 380 | +          writeVal = (light_Configuration.valueMode == LIGHT_RAW) ? &rawSubR : &rawSubP;
 | 
	
		
			
			|  | 381 | +          break;
 | 
	
		
			
			|  | 382 | +        case LIGHT_GET:
 | 
	
		
			
			|  | 383 | +        case LIGHT_PRINT_HELP:
 | 
	
		
			
			|  | 384 | +        case LIGHT_PRINT_VERSION:
 | 
	
		
			
			|  | 385 | +          break;
 | 
	
		
			
			|  | 386 | +      }
 | 
	
		
			
			|  | 387 | +
 | 
	
		
			
			|  | 388 | +      /* Attempt to write */
 | 
	
		
			
			|  | 389 | +      if(!light_setBrightness(light_Configuration.specifiedController, *writeVal))
 | 
	
		
			
			|  | 390 | +      {
 | 
	
		
			
			|  | 391 | +        LIGHT_ERR("could not set brightness");
 | 
	
		
			
			|  | 392 | +        return FALSE;
 | 
	
		
			
			|  | 393 | +      }
 | 
	
		
			
			|  | 394 | +
 | 
	
		
			
			|  | 395 | +      /* All good? return true. */
 | 
	
		
			
			|  | 396 | +      return TRUE;
 | 
	
		
			
			|  | 397 | +
 | 
	
		
			
			|  | 398 | +    }else{
 | 
	
		
			
			|  | 399 | +      /* If we didn't provide a valid target for write operations, fail. */
 | 
	
		
			
			|  | 400 | +      printf("set/add/subtract operations are only available for brightness and minimum cap files.\n");
 | 
	
		
			
			|  | 401 | +      return FALSE;
 | 
	
		
			
			|  | 402 | +    }
 | 
	
		
			
			|  | 403 | +  }
 | 
	
		
			
			|  | 404 | +
 | 
	
		
			
			|  | 405 | +  printf("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);
 | 
	
		
			
			|  | 406 | +
 | 
	
		
			
			|  | 407 | +  printf("You did not specify a valid combination of commandline arguments. Have some help: \n");
 | 
	
		
			
			|  | 408 | +  light_printHelp();
 | 
	
		
			
			|  | 409 | +  return FALSE;
 | 
	
		
			
			|  | 410 | +}
 | 
	
		
			
			|  | 411 | +
 | 
	
		
			
			|  | 412 | +void light_free()
 | 
	
		
			
			|  | 413 | +{
 | 
	
		
			
			|  | 414 | +
 | 
	
		
			
			|  | 415 | +}
 | 
	
		
			
			|  | 416 | +
 | 
	
		
			
			|  | 417 | +LIGHT_BOOL light_genPath(char const *controller, LIGHT_TARGET type, char **buffer)
 | 
	
		
			
			|  | 418 | +{
 | 
	
		
			
			|  | 419 | +  char* returner = (char*)malloc(256);
 | 
	
		
			
			|  | 420 | +  int spfVal = -1;
 | 
	
		
			
			|  | 421 | +
 | 
	
		
			
			|  | 422 | +  if(returner == NULL)
 | 
	
		
			
			|  | 423 | +  {
 | 
	
		
			
			|  | 424 | +    LIGHT_MEMERR();
 | 
	
		
			
			|  | 425 | +    buffer = NULL;
 | 
	
		
			
			|  | 426 | +    return FALSE;
 | 
	
		
			
			|  | 427 | +  }
 | 
	
		
			
			|  | 428 | +
 | 
	
		
			
			|  | 429 | +  memset(returner, '\0', 256);
 | 
	
		
			
			|  | 430 | +  
 | 
	
		
			
			|  | 431 | +  switch(type)
 | 
	
		
			
			|  | 432 | +  {
 | 
	
		
			
			|  | 433 | +    case LIGHT_BRIGHTNESS:
 | 
	
		
			
			|  | 434 | +      spfVal = sprintf(returner, "/sys/class/backlight/%s/brightness", controller);
 | 
	
		
			
			|  | 435 | +      break;
 | 
	
		
			
			|  | 436 | +    case LIGHT_MAX_BRIGHTNESS:
 | 
	
		
			
			|  | 437 | +      spfVal = sprintf(returner, "/sys/class/backlight/%s/max_brightness", controller);
 | 
	
		
			
			|  | 438 | +      break;
 | 
	
		
			
			|  | 439 | +    case LIGHT_MIN_CAP:
 | 
	
		
			
			|  | 440 | +      spfVal = sprintf(returner, "/etc/light/mincap/%s", controller);
 | 
	
		
			
			|  | 441 | +      break;
 | 
	
		
			
			|  | 442 | +  }
 | 
	
		
			
			|  | 443 | +
 | 
	
		
			
			|  | 444 | +  if(spfVal < 0)
 | 
	
		
			
			|  | 445 | +  {
 | 
	
		
			
			|  | 446 | +    LIGHT_ERR("sprintf failed");
 | 
	
		
			
			|  | 447 | +    free(returner);
 | 
	
		
			
			|  | 448 | +    buffer = NULL;
 | 
	
		
			
			|  | 449 | +    return FALSE;
 | 
	
		
			
			|  | 450 | +  }
 | 
	
		
			
			|  | 451 | +
 | 
	
		
			
			|  | 452 | +  *buffer = returner;
 | 
	
		
			
			|  | 453 | +
 | 
	
		
			
			|  | 454 | +  return TRUE;
 | 
	
		
			
			|  | 455 | +}
 | 
	
		
			
			|  | 456 | +
 | 
	
		
			
			|  | 457 | +LIGHT_BOOL light_getBrightness(char const *controller, unsigned long *v)
 | 
	
		
			
			|  | 458 | +{
 | 
	
		
			
			|  | 459 | +  char *brightnessPath = NULL;
 | 
	
		
			
			|  | 460 | +  LIGHT_BOOL readVal = FALSE;
 | 
	
		
			
			|  | 461 | +
 | 
	
		
			
			|  | 462 | +  if(!light_genPath(controller, LIGHT_BRIGHTNESS, &brightnessPath))
 | 
	
		
			
			|  | 463 | +  {
 | 
	
		
			
			|  | 464 | +    LIGHT_ERR("could not generate path to brightness file");
 | 
	
		
			
			|  | 465 | +    return FALSE;
 | 
	
		
			
			|  | 466 | +  }
 | 
	
		
			
			|  | 467 | +
 | 
	
		
			
			|  | 468 | +  readVal = light_readULong( brightnessPath , v);
 | 
	
		
			
			|  | 469 | +  free(brightnessPath);
 | 
	
		
			
			|  | 470 | +
 | 
	
		
			
			|  | 471 | +  if(!readVal)
 | 
	
		
			
			|  | 472 | +  {
 | 
	
		
			
			|  | 473 | +    LIGHT_ERR("could not read value from brightness file");
 | 
	
		
			
			|  | 474 | +    return FALSE;
 | 
	
		
			
			|  | 475 | +  }
 | 
	
		
			
			|  | 476 | +
 | 
	
		
			
			|  | 477 | +  return TRUE;
 | 
	
		
			
			|  | 478 | +}
 | 
	
		
			
			|  | 479 | +
 | 
	
		
			
			|  | 480 | +LIGHT_BOOL light_getMaxBrightness(char const *controller, unsigned long *v)
 | 
	
		
			
			|  | 481 | +{
 | 
	
		
			
			|  | 482 | +  char *maxPath;
 | 
	
		
			
			|  | 483 | +  LIGHT_BOOL readVal = FALSE;
 | 
	
		
			
			|  | 484 | +
 | 
	
		
			
			|  | 485 | +  if(!light_genPath(controller, LIGHT_MAX_BRIGHTNESS, &maxPath))
 | 
	
		
			
			|  | 486 | +  {
 | 
	
		
			
			|  | 487 | +    LIGHT_ERR("could not generate path to maximum brightness file");
 | 
	
		
			
			|  | 488 | +    return FALSE;
 | 
	
		
			
			|  | 489 | +  }
 | 
	
		
			
			|  | 490 | +
 | 
	
		
			
			|  | 491 | +  readVal = light_readULong(maxPath , v);
 | 
	
		
			
			|  | 492 | +  free(maxPath);
 | 
	
		
			
			|  | 493 | +
 | 
	
		
			
			|  | 494 | +  if(!readVal)
 | 
	
		
			
			|  | 495 | +  {
 | 
	
		
			
			|  | 496 | +    LIGHT_ERR("could not read value from max brightness file");
 | 
	
		
			
			|  | 497 | +    return FALSE;
 | 
	
		
			
			|  | 498 | +  }
 | 
	
		
			
			|  | 499 | +
 | 
	
		
			
			|  | 500 | +  if(*v == 0)
 | 
	
		
			
			|  | 501 | +  {
 | 
	
		
			
			|  | 502 | +    LIGHT_ERR("max brightness is 0, so controller is not valid");
 | 
	
		
			
			|  | 503 | +    return FALSE;
 | 
	
		
			
			|  | 504 | +  }
 | 
	
		
			
			|  | 505 | +
 | 
	
		
			
			|  | 506 | +  return TRUE;
 | 
	
		
			
			|  | 507 | +}
 | 
	
		
			
			|  | 508 | +
 | 
	
		
			
			|  | 509 | +LIGHT_BOOL light_setBrightness(char const *controller, unsigned long v)
 | 
	
		
			
			|  | 510 | +{
 | 
	
		
			
			|  | 511 | +  char *brightnessPath = NULL;
 | 
	
		
			
			|  | 512 | +  LIGHT_BOOL writeVal = FALSE;
 | 
	
		
			
			|  | 513 | +
 | 
	
		
			
			|  | 514 | +  if(!light_genPath(controller, LIGHT_BRIGHTNESS, &brightnessPath))
 | 
	
		
			
			|  | 515 | +  {
 | 
	
		
			
			|  | 516 | +    LIGHT_ERR("could not generate path to brightness file");
 | 
	
		
			
			|  | 517 | +    return FALSE;
 | 
	
		
			
			|  | 518 | +  }
 | 
	
		
			
			|  | 519 | +
 | 
	
		
			
			|  | 520 | +  writeVal = light_writeULong(brightnessPath, v);
 | 
	
		
			
			|  | 521 | +
 | 
	
		
			
			|  | 522 | +  if(!writeVal)
 | 
	
		
			
			|  | 523 | +  {
 | 
	
		
			
			|  | 524 | +    LIGHT_ERR("could not write value to brightness file");
 | 
	
		
			
			|  | 525 | +  }
 | 
	
		
			
			|  | 526 | +
 | 
	
		
			
			|  | 527 | +  free(brightnessPath);
 | 
	
		
			
			|  | 528 | +  return writeVal;
 | 
	
		
			
			|  | 529 | +}
 | 
	
		
			
			|  | 530 | +
 | 
	
		
			
			|  | 531 | +LIGHT_BOOL light_controllerAccessible(char const *controller)
 | 
	
		
			
			|  | 532 | +{
 | 
	
		
			
			|  | 533 | +  char *brightnessPath = NULL;
 | 
	
		
			
			|  | 534 | +  unsigned long dummy;
 | 
	
		
			
			|  | 535 | +
 | 
	
		
			
			|  | 536 | +  if(!light_getBrightness(controller, &dummy))
 | 
	
		
			
			|  | 537 | +  {
 | 
	
		
			
			|  | 538 | +    LIGHT_WARN("could not read controllers brightness file, so controller is not accessible")
 | 
	
		
			
			|  | 539 | +    return FALSE;
 | 
	
		
			
			|  | 540 | +  }
 | 
	
		
			
			|  | 541 | +
 | 
	
		
			
			|  | 542 | +  if(!light_getMaxBrightness(controller, &dummy))
 | 
	
		
			
			|  | 543 | +  {
 | 
	
		
			
			|  | 544 | +    LIGHT_WARN("could not read controller max brightness file, so controller is not accessible")
 | 
	
		
			
			|  | 545 | +    return FALSE;
 | 
	
		
			
			|  | 546 | +  }
 | 
	
		
			
			|  | 547 | +
 | 
	
		
			
			|  | 548 | +  if(!light_genPath(controller, LIGHT_BRIGHTNESS, &brightnessPath))
 | 
	
		
			
			|  | 549 | +  {
 | 
	
		
			
			|  | 550 | +    LIGHT_ERR("could not generate path to brightness file");
 | 
	
		
			
			|  | 551 | +    return FALSE;
 | 
	
		
			
			|  | 552 | +  }
 | 
	
		
			
			|  | 553 | +
 | 
	
		
			
			|  | 554 | +  if(!light_isWritable(brightnessPath))
 | 
	
		
			
			|  | 555 | +  {
 | 
	
		
			
			|  | 556 | +    LIGHT_WARN("could not open controller brightness file for writing, so controller is not accessible");
 | 
	
		
			
			|  | 557 | +    free(brightnessPath);
 | 
	
		
			
			|  | 558 | +    return FALSE;
 | 
	
		
			
			|  | 559 | +  }
 | 
	
		
			
			|  | 560 | +
 | 
	
		
			
			|  | 561 | +  free(brightnessPath);
 | 
	
		
			
			|  | 562 | +  return TRUE;
 | 
	
		
			
			|  | 563 | +}
 | 
	
		
			
			|  | 564 | +
 | 
	
		
			
			|  | 565 | +LIGHT_BOOL light_iterateControllers()
 | 
	
		
			
			|  | 566 | +{
 | 
	
		
			
			|  | 567 | +  LIGHT_BOOL dotsKilled = FALSE;
 | 
	
		
			
			|  | 568 | +
 | 
	
		
			
			|  | 569 | +  if(light_iteratorDir == NULL)
 | 
	
		
			
			|  | 570 | +  {
 | 
	
		
			
			|  | 571 | +    light_iteratorDir = opendir("/sys/class/backlight");
 | 
	
		
			
			|  | 572 | +    if(light_iteratorDir == NULL)
 | 
	
		
			
			|  | 573 | +    {
 | 
	
		
			
			|  | 574 | +      LIGHT_ERR("could not open /sys/class/backlight directory");
 | 
	
		
			
			|  | 575 | +      return FALSE;
 | 
	
		
			
			|  | 576 | +    }
 | 
	
		
			
			|  | 577 | +  }
 | 
	
		
			
			|  | 578 | +
 | 
	
		
			
			|  | 579 | +  while(!dotsKilled)
 | 
	
		
			
			|  | 580 | +  {
 | 
	
		
			
			|  | 581 | +    light_iterator = readdir(light_iteratorDir);
 | 
	
		
			
			|  | 582 | +    if(light_iterator == NULL)
 | 
	
		
			
			|  | 583 | +    {
 | 
	
		
			
			|  | 584 | +      if(light_iteratorDir != NULL)
 | 
	
		
			
			|  | 585 | +      {
 | 
	
		
			
			|  | 586 | +        closedir(light_iteratorDir);
 | 
	
		
			
			|  | 587 | +        light_iteratorDir = NULL;
 | 
	
		
			
			|  | 588 | +      }
 | 
	
		
			
			|  | 589 | +      return FALSE;
 | 
	
		
			
			|  | 590 | +    }
 | 
	
		
			
			|  | 591 | +
 | 
	
		
			
			|  | 592 | +    if(light_iterator->d_name[0] != '.')
 | 
	
		
			
			|  | 593 | +    {
 | 
	
		
			
			|  | 594 | +      dotsKilled = TRUE;
 | 
	
		
			
			|  | 595 | +    }
 | 
	
		
			
			|  | 596 | +  }
 | 
	
		
			
			|  | 597 | +
 | 
	
		
			
			|  | 598 | +  strcpy(light_currentController, light_iterator->d_name);
 | 
	
		
			
			|  | 599 | +
 | 
	
		
			
			|  | 600 | +  return TRUE;
 | 
	
		
			
			|  | 601 | +}
 | 
	
		
			
			|  | 602 | +
 | 
	
		
			
			|  | 603 | +LIGHT_BOOL light_getBestController(char *controller)
 | 
	
		
			
			|  | 604 | +{
 | 
	
		
			
			|  | 605 | +  char bestYet[256];
 | 
	
		
			
			|  | 606 | +  unsigned long bestValYet = 0;
 | 
	
		
			
			|  | 607 | +  LIGHT_BOOL foundOkController = FALSE;
 | 
	
		
			
			|  | 608 | +
 | 
	
		
			
			|  | 609 | +  memset(bestYet, '\0', 256);
 | 
	
		
			
			|  | 610 | +
 | 
	
		
			
			|  | 611 | +  while(light_iterateControllers())
 | 
	
		
			
			|  | 612 | +  {
 | 
	
		
			
			|  | 613 | +    unsigned long currVal = 0;
 | 
	
		
			
			|  | 614 | +
 | 
	
		
			
			|  | 615 | +    if(light_getMaxBrightness(light_currentController, &currVal))
 | 
	
		
			
			|  | 616 | +    {
 | 
	
		
			
			|  | 617 | +      
 | 
	
		
			
			|  | 618 | +      if(light_controllerAccessible(light_currentController))
 | 
	
		
			
			|  | 619 | +      {
 | 
	
		
			
			|  | 620 | +        if(currVal > bestValYet)
 | 
	
		
			
			|  | 621 | +        {
 | 
	
		
			
			|  | 622 | +        foundOkController = TRUE;
 | 
	
		
			
			|  | 623 | +        bestValYet = currVal;
 | 
	
		
			
			|  | 624 | +        memset(bestYet, '\0', 256);
 | 
	
		
			
			|  | 625 | +        strcpy(bestYet, light_currentController);
 | 
	
		
			
			|  | 626 | +        }else{
 | 
	
		
			
			|  | 627 | +          LIGHT_NOTE("ignoring controller as better one already found");
 | 
	
		
			
			|  | 628 | +        }
 | 
	
		
			
			|  | 629 | +      }else{
 | 
	
		
			
			|  | 630 | +        LIGHT_WARN("controller not accessible");
 | 
	
		
			
			|  | 631 | +      }
 | 
	
		
			
			|  | 632 | +    }else{
 | 
	
		
			
			|  | 633 | +      LIGHT_WARN("could not read max brightness from file");
 | 
	
		
			
			|  | 634 | +    }
 | 
	
		
			
			|  | 635 | +  }
 | 
	
		
			
			|  | 636 | +
 | 
	
		
			
			|  | 637 | +  if(!foundOkController)
 | 
	
		
			
			|  | 638 | +  {
 | 
	
		
			
			|  | 639 | +    LIGHT_ERR("could not find an accessible controller");
 | 
	
		
			
			|  | 640 | +    return FALSE;
 | 
	
		
			
			|  | 641 | +  }
 | 
	
		
			
			|  | 642 | +
 | 
	
		
			
			|  | 643 | +  if(bestValYet == 0)
 | 
	
		
			
			|  | 644 | +  {
 | 
	
		
			
			|  | 645 | +    LIGHT_ERR("found accessible controller but it's useless/corrupt");
 | 
	
		
			
			|  | 646 | +    return FALSE;
 | 
	
		
			
			|  | 647 | +  }
 | 
	
		
			
			|  | 648 | +
 | 
	
		
			
			|  | 649 | +  memset(controller, '\0', 256);
 | 
	
		
			
			|  | 650 | +  strcpy(controller, bestYet);
 | 
	
		
			
			|  | 651 | +
 | 
	
		
			
			|  | 652 | +  return TRUE;
 | 
	
		
			
			|  | 653 | +}
 | 
	
		
			
			|  | 654 | +
 | 
	
		
			
			|  | 655 | +LIGHT_BOOL light_getMinCap(char const * controller, LIGHT_BOOL * hasMinCap, unsigned long * minCap)
 | 
	
		
			
			|  | 656 | +{
 | 
	
		
			
			|  | 657 | + char * mincapPath = NULL;
 | 
	
		
			
			|  | 658 | + 
 | 
	
		
			
			|  | 659 | + if(!light_genPath(controller, LIGHT_MIN_CAP, &mincapPath))
 | 
	
		
			
			|  | 660 | + {
 | 
	
		
			
			|  | 661 | +    LIGHT_ERR("could not generate path to minimum cap file");
 | 
	
		
			
			|  | 662 | +    return FALSE;
 | 
	
		
			
			|  | 663 | + }
 | 
	
		
			
			|  | 664 | +
 | 
	
		
			
			|  | 665 | +  if(!light_isReadable(mincapPath)){
 | 
	
		
			
			|  | 666 | +    *hasMinCap = FALSE;
 | 
	
		
			
			|  | 667 | +    *minCap = 0;
 | 
	
		
			
			|  | 668 | +    free(mincapPath);
 | 
	
		
			
			|  | 669 | +    return TRUE;
 | 
	
		
			
			|  | 670 | +  }
 | 
	
		
			
			|  | 671 | +
 | 
	
		
			
			|  | 672 | +  if(!light_readULong(mincapPath, minCap))
 | 
	
		
			
			|  | 673 | +  {
 | 
	
		
			
			|  | 674 | +    LIGHT_ERR("could not read minimum cap from file");
 | 
	
		
			
			|  | 675 | +    free(mincapPath);
 | 
	
		
			
			|  | 676 | +    return FALSE;
 | 
	
		
			
			|  | 677 | +  }
 | 
	
		
			
			|  | 678 | +
 | 
	
		
			
			|  | 679 | +  *hasMinCap = TRUE;
 | 
	
		
			
			|  | 680 | +
 | 
	
		
			
			|  | 681 | +  free(mincapPath);
 | 
	
		
			
			|  | 682 | +  return TRUE;
 | 
	
		
			
			|  | 683 | +}
 | 
	
		
			
			|  | 684 | +
 | 
	
		
			
			|  | 685 | +LIGHT_BOOL light_setMinCap(char const * controller, unsigned long v)
 | 
	
		
			
			|  | 686 | +{
 | 
	
		
			
			|  | 687 | +  char * mincapPath = NULL;
 | 
	
		
			
			|  | 688 | +  if(!light_genPath(controller, LIGHT_MIN_CAP, &mincapPath))
 | 
	
		
			
			|  | 689 | +  {
 | 
	
		
			
			|  | 690 | +    LIGHT_ERR("could not generate path to minimum cap file");
 | 
	
		
			
			|  | 691 | +    return FALSE;
 | 
	
		
			
			|  | 692 | +  }
 | 
	
		
			
			|  | 693 | +
 | 
	
		
			
			|  | 694 | +  if(!light_writeULong(mincapPath, v))
 | 
	
		
			
			|  | 695 | +  {
 | 
	
		
			
			|  | 696 | +    LIGHT_ERR("could not write to minimum cap file");
 | 
	
		
			
			|  | 697 | +    free(mincapPath);
 | 
	
		
			
			|  | 698 | +    return FALSE;
 | 
	
		
			
			|  | 699 | +  }
 | 
	
		
			
			|  | 700 | +
 | 
	
		
			
			|  | 701 | +  free(mincapPath);
 | 
	
		
			
			|  | 702 | +  return TRUE;
 | 
	
		
			
			|  | 703 | +}
 |