瀏覽代碼

Rewrite light_execute for readability and avoiding unnecessary oper.

Abdullah ibn Nadjo 7 年之前
父節點
當前提交
f288657500
共有 1 個文件被更改,包括 91 次插入88 次删除
  1. 91
    88
      src/light.c

+ 91
- 88
src/light.c 查看文件

@@ -289,29 +289,9 @@ LIGHT_BOOL light_initialize(int argc, char** argv)
289 289
   return TRUE;
290 290
 }
291 291
 
292
-LIGHT_BOOL light_execute()
292
+/* Print help and version info */
293
+LIGHT_BOOL light_handleInfo()
293 294
 {
294
-  unsigned long rawCurr; /* The current brightness, in raw mode */
295
-  double    percentCurr; /* The current brightness, in percent  */
296
-  unsigned long  rawMax; /* The max brightness, in percent      */
297
-
298
-  unsigned long  rawSetP; /* The final value to be set, in raw mode, when setting with percent */
299
-  unsigned long  rawAddP; /* The final value to be set, in raw mode, when adding with percent */
300
-  unsigned long  rawSubP; /* The final value to be set, in raw mode, when subtracting with percent */
301
-
302
-  unsigned long  rawSetR; /* The final value to be set, in raw mode, when setting with raw values */
303
-  unsigned long  rawAddR; /* The final value to be set, in raw mode, when adding with raw values */
304
-  unsigned long  rawSubR; /* The final value to be set, in raw mode, when subtracting with raw values */
305
-
306
-  unsigned long   minCap; /* The minimum cap, in raw mode */
307
-  double   percentMinCap; /* The minimum cap, in percent */
308
-  LIGHT_BOOL   hasMinCap; /* If we have a minimum cap     */
309
-  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) */
310
-  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) */
311
-
312
-  unsigned long  *writeVal; /* The actual value used for writing */
313
-
314
-  /* Print help and version info */
315 295
   if(light_Configuration.operationMode == LIGHT_PRINT_HELP)
316 296
   {
317 297
     light_printHelp();
@@ -330,87 +310,113 @@ LIGHT_BOOL light_execute()
330 310
     light_listControllers();
331 311
     return TRUE;
332 312
   }
313
+  return FALSE;
314
+}
333 315
 
334
-  /* Prepare variables */
335
-  LIGHT_NOTE_FMT("Executing light on '%s' controller", light_Configuration.specifiedController);
336
-
337
-  /* -- First, get the current, min and max values directly from controller/configuration (raw values) */
338
-  if(!light_getBrightness(light_Configuration.specifiedController, &rawCurr))
316
+LIGHT_BOOL light_initExecution(unsigned long *rawCurr, unsigned long *rawMax, LIGHT_BOOL *hasMinCap, unsigned long *minCap)
317
+{
318
+  if(light_Configuration.hasCachedMaxBrightness)
339 319
   {
340
-    LIGHT_ERR("could not get brightness");
320
+    *rawMax = light_Configuration.cachedMaxBrightness;
321
+  }
322
+  else if(!light_getMaxBrightness(light_Configuration.specifiedController, rawMax))
323
+  {
324
+    LIGHT_ERR("could not get max brightness");
341 325
     return FALSE;
342 326
   }
343 327
 
344
-  if(light_Configuration.hasCachedMaxBrightness)
328
+  /* No need to go further if targetting mincap */
329
+  if(light_Configuration.target == LIGHT_MIN_CAP)
345 330
   {
346
-    rawMax = light_Configuration.cachedMaxBrightness;
331
+    return TRUE;
347 332
   }
348
-  else if(!light_getMaxBrightness(light_Configuration.specifiedController, &rawMax))
333
+
334
+  if(!light_getBrightness(light_Configuration.specifiedController, rawCurr))
349 335
   {
350
-    LIGHT_ERR("could not get max brightness");
336
+    LIGHT_ERR("could not get brightness");
351 337
     return FALSE;
352 338
   }
353 339
 
354
-  if(!light_getMinCap(light_Configuration.specifiedController, &hasMinCap, &minCap))
340
+  if(!light_getMinCap(light_Configuration.specifiedController, hasMinCap, minCap))
355 341
   {
356 342
     LIGHT_ERR("could not get min brightness");
357 343
     return FALSE;
358 344
   }
359 345
 
360
-  if( hasMinCap && minCap > rawMax )
346
+  if( *hasMinCap && *minCap > *rawMax )
361 347
   {
362
-    LIGHT_WARN_FMT("invalid minimum cap (raw) value of '%lu' for controller, ignoring and using 0", minCap);
363
-    LIGHT_WARN_FMT("minimum cap must be inferior to '%lu'", rawMax);
348
+    LIGHT_WARN_FMT("invalid minimum cap (raw) value of '%lu' for controller, ignoring and using 0", *minCap);
349
+    LIGHT_WARN_FMT("minimum cap must be inferior to '%lu'", *rawMax);
364 350
     minCap = 0;
365 351
   }
352
+  return TRUE;
353
+}
366 354
 
367
-  /* -- Secondly, calculate the rest of the values (Clamp them here as well!) */
368
-  percentCurr = LIGHT_CLAMP( ((double)rawCurr) /  ((double)rawMax) * 100 , 0.00, 100.00 );
369
-  percentMinCap = LIGHT_CLAMP( ((double)minCap) / ((double)rawMax) * 100 , 0.00, 100.00 );
355
+LIGHT_BOOL light_execute()
356
+{
357
+  unsigned long rawCurr; /* The current brightness, in raw units */
358
+  double    percentCurr; /* The current brightness, in percent  */
359
+  unsigned long  rawMax; /* The max brightness, in percent      */
360
+
361
+  unsigned long   minCap; /* The minimum cap, in raw units */
362
+  double   percentMinCap; /* The minimum cap, in percent */
363
+  LIGHT_BOOL   hasMinCap; /* If we have a minimum cap     */
364
+  unsigned long writeVal;
370 365
 
371
-  rawSetP     = LIGHT_CLAMP( ((unsigned long) (light_Configuration.specifiedValuePercent * ((double)rawMax) ) / 100) , minCap, rawMax );
372
-  rawAddP     = LIGHT_CLAMP( ((unsigned long) ( (percentCurr + light_Configuration.specifiedValuePercent) * ((double)rawMax)) / 100) , minCap, rawMax );
366
+  LIGHT_VAL_MODE valueMode;
373 367
 
374
-  if( light_Configuration.specifiedValuePercent > percentCurr)
368
+  if(light_handleInfo())
375 369
   {
376
-    rawSubP   = LIGHT_CLAMP(0, minCap, rawMax);
377
-  }else{
378
-    rawSubP     = LIGHT_CLAMP( ((unsigned long) ( (percentCurr - light_Configuration.specifiedValuePercent) * ((double)rawMax)) / 100) , minCap, rawMax );
370
+    return TRUE;
379 371
   }
380 372
 
381
-  rawSetR     = LIGHT_CLAMP( light_Configuration.specifiedValueRaw , minCap, rawMax );
382
-  rawAddR     = LIGHT_CLAMP( rawCurr + light_Configuration.specifiedValueRaw , minCap, rawMax );
383
-
384
-  if(light_Configuration.specifiedValueRaw > rawCurr){
385
-    rawSubR     = LIGHT_CLAMP(0, minCap, rawMax)
386
-  }else{
387
-    rawSubR     = LIGHT_CLAMP( rawCurr - light_Configuration.specifiedValueRaw , minCap, rawMax );
373
+  /* -- First, get the current, min and max values directly from controller/configuration (raw values) */
374
+  if(!light_initExecution(&rawCurr, &rawMax, &hasMinCap, &minCap))
375
+  {
376
+    return FALSE;
388 377
   }
389 378
 
390
-  minCapP     = LIGHT_CLAMP(((unsigned long) (light_Configuration.specifiedValuePercent * ((double)rawMax) ) / 100), 0, rawMax);
391
-  minCapR     = LIGHT_CLAMP( light_Configuration.specifiedValueRaw, 0, rawMax );
379
+  /* -- Secondly, calculate the rest of the values (Clamp them here as well!) */
380
+  valueMode = light_Configuration.valueMode;
381
+  percentCurr = LIGHT_CLAMP( ((double)rawCurr) /  ((double)rawMax) * 100 , 0.00, 100.00 );
382
+  percentMinCap = LIGHT_CLAMP( ((double)minCap) / ((double)rawMax) * 100 , 0.00, 100.00 );
383
+
384
+  LIGHT_NOTE_FMT("executing light on '%s' controller", light_Configuration.specifiedController);
392 385
 
393 386
   /* Handle get operations */
394 387
   if(light_Configuration.operationMode == LIGHT_GET)
395 388
   {
396 389
     switch(light_Configuration.target){
397 390
       case LIGHT_BRIGHTNESS:
398
-        (light_Configuration.valueMode == LIGHT_RAW) ? printf("%lu\n", rawCurr) : printf("%.2f\n", percentCurr);
391
+        (valueMode == LIGHT_RAW) ? printf("%lu\n", rawCurr) : printf("%.2f\n", percentCurr);
399 392
         break;
400 393
       case LIGHT_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 */
394
+        (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 395
         break;
403 396
       case LIGHT_MIN_CAP:
404
-        (light_Configuration.valueMode == LIGHT_RAW) ? printf("%lu\n", minCap) : printf("%.2f\n", percentMinCap);
397
+        (valueMode == LIGHT_RAW) ? printf("%lu\n", minCap) : printf("%.2f\n", percentMinCap);
405 398
         break;
406 399
       case LIGHT_SAVERESTORE:
407 400
         break;
408
-      case LIGHT_KEYBOARD:
409
-        (light_Configuration.valueMode == LIGHT_RAW) ? printf("%lu\n", rawCurr) : printf("%.2f\n", percentCurr);
410
-        break;
411
-      case LIGHT_KEYBOARD_MAX_BRIGHTNESS:
412
-        (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 */
413
-        break;
401
+    }
402
+    return TRUE;
403
+  }
404
+
405
+  /* Handle saves and restores*/
406
+  if(light_Configuration.operationMode == LIGHT_SAVE){
407
+    if(!light_saveBrightness(light_Configuration.specifiedController, rawCurr))
408
+    {
409
+      LIGHT_ERR("could not save brightness");
410
+      return FALSE;
411
+    }
412
+
413
+    return TRUE;
414
+  }
415
+
416
+  if(light_Configuration.operationMode == LIGHT_RESTORE){
417
+    if(!light_restoreBrightness(light_Configuration.specifiedController)){
418
+      LIGHT_ERR("could not restore brightness");
419
+      return FALSE;
414 420
     }
415 421
 
416 422
     return TRUE;
@@ -421,14 +427,12 @@ LIGHT_BOOL light_execute()
421 427
      light_Configuration.operationMode == LIGHT_ADD ||
422 428
      light_Configuration.operationMode == LIGHT_SUB)
423 429
   {
424
-
425
-    /* Set the pointer we will use for write to a fallback (that shouldn't change anything) */
426
-    writeVal = &rawCurr;
427
-
428 430
     if(light_Configuration.target == LIGHT_MIN_CAP)
429 431
     {
430 432
       /* Handle minimum cap files */
431
-
433
+      writeVal = valueMode == LIGHT_RAW ?
434
+        LIGHT_CLAMP( light_Configuration.specifiedValueRaw, 0, rawMax ) :
435
+        LIGHT_CLAMP(((unsigned long) (light_Configuration.specifiedValuePercent * ((double)rawMax) ) / 100), 0, rawMax);
432 436
       /* If we are not attempting to set, fail! */
433 437
       if(light_Configuration.operationMode != LIGHT_SET)
434 438
       {
@@ -436,9 +440,7 @@ LIGHT_BOOL light_execute()
436 440
         return FALSE;
437 441
       }
438 442
 
439
-      /* Point our writevalue and attempt to write*/
440
-      writeVal = (light_Configuration.valueMode == LIGHT_RAW) ? &minCapR : &minCapP;
441
-      if(!light_setMinCap(light_Configuration.specifiedController, *writeVal))
443
+      if(!light_setMinCap(light_Configuration.specifiedController, writeVal))
442 444
       {
443 445
         LIGHT_ERR("could not set minimum cap");
444 446
         return FALSE;
@@ -450,33 +452,34 @@ LIGHT_BOOL light_execute()
450 452
     }else if(light_Configuration.target == LIGHT_BRIGHTNESS || light_Configuration.target == LIGHT_KEYBOARD){
451 453
       /* Handle brightness writing */
452 454
 
453
-      /* Point our writevalue according to configuration */
454 455
       switch(light_Configuration.operationMode)
455 456
       {
456 457
         case LIGHT_SET:
457
-          writeVal = (light_Configuration.valueMode == LIGHT_RAW) ? &rawSetR : &rawSetP;
458
+          writeVal = valueMode == LIGHT_RAW ?
459
+            LIGHT_CLAMP( light_Configuration.specifiedValueRaw , minCap, rawMax ) :
460
+            LIGHT_CLAMP( ((unsigned long) (light_Configuration.specifiedValuePercent * ((double)rawMax) ) / 100) , minCap, rawMax );
458 461
           break;
459 462
         case LIGHT_ADD:
460
-          if(rawCurr == rawAddP && rawAddP != rawMax)
461
-          {
462
-            rawAddP+=1;
463
-          }
464
-          writeVal = (light_Configuration.valueMode == LIGHT_RAW) ? &rawAddR : &rawAddP;
463
+          writeVal = valueMode == LIGHT_RAW ?
464
+            LIGHT_CLAMP( rawCurr + light_Configuration.specifiedValueRaw , minCap, rawMax ) :
465
+            LIGHT_CLAMP( ((unsigned long) ( (percentCurr + light_Configuration.specifiedValuePercent) * ((double)rawMax)) / 100) , minCap, rawMax );
465 466
           break;
466 467
         case LIGHT_SUB:
467
-          writeVal = (light_Configuration.valueMode == LIGHT_RAW) ? &rawSubR : &rawSubP;
468
-          break;
469
-        case LIGHT_GET:
470
-        case LIGHT_PRINT_HELP:
471
-        case LIGHT_PRINT_VERSION:
472
-        case LIGHT_LIST_CTRL:
473
-        case LIGHT_SAVE:
474
-        case LIGHT_RESTORE:
475
-          break;
468
+         if(light_Configuration.specifiedValueRaw > rawCurr){
469
+           writeVal = LIGHT_CLAMP(0, minCap, rawMax);
470
+         }else{
471
+          writeVal = valueMode == LIGHT_RAW ?
472
+            LIGHT_CLAMP( rawCurr - light_Configuration.specifiedValueRaw , minCap, rawMax ):
473
+            LIGHT_CLAMP( ((unsigned long) ( (percentCurr - light_Configuration.specifiedValuePercent) * ((double)rawMax)) / 100) , minCap, rawMax );
474
+         }
475
+         break;
476
+        /* we have already taken other possibilities, so we shouldn't get here */
477
+        default:
478
+          return FALSE;
476 479
       }
477 480
 
478 481
       /* Attempt to write */
479
-      if(!light_setBrightness(light_Configuration.specifiedController, *writeVal))
482
+      if(!light_setBrightness(light_Configuration.specifiedController, writeVal))
480 483
       {
481 484
         LIGHT_ERR("could not set brightness");
482 485
         return FALSE;