Quellcode durchsuchen

Rewrite light_execute for readability and avoiding unnecessary oper.

Abdullah ibn Nadjo vor 6 Jahren
Ursprung
Commit
f288657500
1 geänderte Dateien mit 91 neuen und 88 gelöschten Zeilen
  1. 91
    88
      src/light.c

+ 91
- 88
src/light.c Datei anzeigen

289
   return TRUE;
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
   if(light_Configuration.operationMode == LIGHT_PRINT_HELP)
295
   if(light_Configuration.operationMode == LIGHT_PRINT_HELP)
316
   {
296
   {
317
     light_printHelp();
297
     light_printHelp();
330
     light_listControllers();
310
     light_listControllers();
331
     return TRUE;
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
     return FALSE;
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
     return FALSE;
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
     LIGHT_ERR("could not get min brightness");
342
     LIGHT_ERR("could not get min brightness");
357
     return FALSE;
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
     minCap = 0;
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
   /* Handle get operations */
386
   /* Handle get operations */
394
   if(light_Configuration.operationMode == LIGHT_GET)
387
   if(light_Configuration.operationMode == LIGHT_GET)
395
   {
388
   {
396
     switch(light_Configuration.target){
389
     switch(light_Configuration.target){
397
       case LIGHT_BRIGHTNESS:
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
         break;
392
         break;
400
       case LIGHT_MAX_BRIGHTNESS:
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
         break;
395
         break;
403
       case LIGHT_MIN_CAP:
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
         break;
398
         break;
406
       case LIGHT_SAVERESTORE:
399
       case LIGHT_SAVERESTORE:
407
         break;
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
     return TRUE;
422
     return TRUE;
421
      light_Configuration.operationMode == LIGHT_ADD ||
427
      light_Configuration.operationMode == LIGHT_ADD ||
422
      light_Configuration.operationMode == LIGHT_SUB)
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
     if(light_Configuration.target == LIGHT_MIN_CAP)
430
     if(light_Configuration.target == LIGHT_MIN_CAP)
429
     {
431
     {
430
       /* Handle minimum cap files */
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
       /* If we are not attempting to set, fail! */
436
       /* If we are not attempting to set, fail! */
433
       if(light_Configuration.operationMode != LIGHT_SET)
437
       if(light_Configuration.operationMode != LIGHT_SET)
434
       {
438
       {
436
         return FALSE;
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
         LIGHT_ERR("could not set minimum cap");
445
         LIGHT_ERR("could not set minimum cap");
444
         return FALSE;
446
         return FALSE;
450
     }else if(light_Configuration.target == LIGHT_BRIGHTNESS || light_Configuration.target == LIGHT_KEYBOARD){
452
     }else if(light_Configuration.target == LIGHT_BRIGHTNESS || light_Configuration.target == LIGHT_KEYBOARD){
451
       /* Handle brightness writing */
453
       /* Handle brightness writing */
452
 
454
 
453
-      /* Point our writevalue according to configuration */
454
       switch(light_Configuration.operationMode)
455
       switch(light_Configuration.operationMode)
455
       {
456
       {
456
         case LIGHT_SET:
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
           break;
461
           break;
459
         case LIGHT_ADD:
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
           break;
466
           break;
466
         case LIGHT_SUB:
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
       /* Attempt to write */
481
       /* Attempt to write */
479
-      if(!light_setBrightness(light_Configuration.specifiedController, *writeVal))
482
+      if(!light_setBrightness(light_Configuration.specifiedController, writeVal))
480
       {
483
       {
481
         LIGHT_ERR("could not set brightness");
484
         LIGHT_ERR("could not set brightness");
482
         return FALSE;
485
         return FALSE;