Browse Source

Added save/restore functionality

Fredrik Haikarainen 10 years ago
parent
commit
8dfe104b74
2 changed files with 104 additions and 5 deletions
  1. 10
    2
      include/light.h
  2. 94
    3
      src/light.c

+ 10
- 2
include/light.h View File

48
 typedef enum LIGHT_TARGET {
48
 typedef enum LIGHT_TARGET {
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_TARGET;
53
 } LIGHT_TARGET;
53
 
54
 
54
 typedef enum LIGHT_CTRL_MODE {
55
 typedef enum LIGHT_CTRL_MODE {
63
   LIGHT_SUB,
64
   LIGHT_SUB,
64
   LIGHT_PRINT_HELP,   /* Prints help and exits  */
65
   LIGHT_PRINT_HELP,   /* Prints help and exits  */
65
   LIGHT_PRINT_VERSION, /* Prints version info and exits */
66
   LIGHT_PRINT_VERSION, /* Prints version info and exits */
66
-  LIGHT_LIST_CTRL
67
+  LIGHT_LIST_CTRL,
68
+  LIGHT_RESTORE,
69
+  LIGHT_SAVE
70
+
67
 } LIGHT_OP_MODE;
71
 } LIGHT_OP_MODE;
68
 
72
 
69
 typedef enum LIGHT_VAL_MODE {
73
 typedef enum LIGHT_VAL_MODE {
142
 
146
 
143
 LIGHT_BOOL light_listControllers();
147
 LIGHT_BOOL light_listControllers();
144
 
148
 
149
+LIGHT_BOOL light_saveBrightness(char const *controller, unsigned long v);
150
+
151
+LIGHT_BOOL light_restoreBrightness(char const *controller);
152
+
145
 #endif /* LIGHT_H */
153
 #endif /* LIGHT_H */

+ 94
- 3
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, "HhVGSAULbmcas:prv:")) != -1)
34
+  while((currFlag = getopt(argc, argv, "HhVGSAULIObmcas:prv:")) != -1)
35
   {
35
   {
36
     switch(currFlag)
36
     switch(currFlag)
37
     {
37
     {
65
         ASSERT_OPSET();
65
         ASSERT_OPSET();
66
         light_Configuration.operationMode = LIGHT_LIST_CTRL;
66
         light_Configuration.operationMode = LIGHT_LIST_CTRL;
67
         break;
67
         break;
68
+      case 'I':
69
+        ASSERT_OPSET();
70
+        light_Configuration.operationMode = LIGHT_RESTORE;
71
+        break;
72
+      case 'O':
73
+        ASSERT_OPSET();
74
+        light_Configuration.operationMode = LIGHT_SAVE;
75
+        break;
68
 
76
 
69
       /* -- Targets -- */
77
       /* -- Targets -- */
70
       case 'b':
78
       case 'b':
190
   printf("  -S:\t\tSet value\n");
198
   printf("  -S:\t\tSet value\n");
191
   printf("  -A:\t\tAdd value\n");
199
   printf("  -A:\t\tAdd value\n");
192
   printf("  -U:\t\tSubtract value\n");
200
   printf("  -U:\t\tSubtract value\n");
193
-  printf("  -L:\t\tList controllers\n\n");
201
+  printf("  -L:\t\tList controllers\n");
202
+  printf("  -I:\t\tRestore brightness\n");
203
+  printf("  -O:\t\tSave brightness\n\n");
194
   
204
   
195
   printf("Targets (can not be used in conjunction):\n");
205
   printf("Targets (can not be used in conjunction):\n");
196
   printf("  -b:\t\tBrightness (default)\n  \t\tUsed with [GSAU]\n\n");
206
   printf("  -b:\t\tBrightness (default)\n  \t\tUsed with [GSAU]\n\n");
227
       return TRUE;
237
       return TRUE;
228
   }
238
   }
229
 
239
 
230
-  /* Make sure we have a valid /etc/light directory, as well as /etc/light/mincap */
240
+  /* Make sure we have a valid /etc/light directory, as well as mincap and save */
231
   mkdirVal = mkdir("/etc/light", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
241
   mkdirVal = mkdir("/etc/light", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
232
   if(mkdirVal != 0 && errno != EEXIST)
242
   if(mkdirVal != 0 && errno != EEXIST)
233
   {
243
   {
242
     return FALSE;
252
     return FALSE;
243
   }
253
   }
244
 
254
 
255
+  mkdirVal = mkdir("/etc/light/save", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
256
+  if(mkdirVal != 0 && errno != EEXIST)
257
+  {
258
+    LIGHT_ERR("/etc/light/save does not exist and could not be created");
259
+    return FALSE;
260
+  }
261
+
245
   /* Make sure we have a valid controller before we proceed */
262
   /* Make sure we have a valid controller before we proceed */
246
   if(light_Configuration.controllerMode == LIGHT_AUTO)
263
   if(light_Configuration.controllerMode == LIGHT_AUTO)
247
   {
264
   {
371
       case LIGHT_MIN_CAP:
388
       case LIGHT_MIN_CAP:
372
         (light_Configuration.valueMode == LIGHT_RAW) ? printf("%lu\n", minCap) : printf("%.2f\n", percentMinCap);
389
         (light_Configuration.valueMode == LIGHT_RAW) ? printf("%lu\n", minCap) : printf("%.2f\n", percentMinCap);
373
         break;
390
         break;
391
+      case LIGHT_SAVERESTORE:
392
+        break;
374
     }
393
     }
375
 
394
 
376
     return TRUE;
395
     return TRUE;
426
         case LIGHT_PRINT_HELP:
445
         case LIGHT_PRINT_HELP:
427
         case LIGHT_PRINT_VERSION:
446
         case LIGHT_PRINT_VERSION:
428
         case LIGHT_LIST_CTRL:
447
         case LIGHT_LIST_CTRL:
448
+        case LIGHT_SAVE:
449
+        case LIGHT_RESTORE:
429
           break;
450
           break;
430
       }
451
       }
431
 
452
 
446
     }
467
     }
447
   }
468
   }
448
 
469
 
470
+  /* Handle saves and restores*/
471
+  if(light_Configuration.operationMode == LIGHT_SAVE){
472
+    if(!light_saveBrightness(light_Configuration.specifiedController, rawCurr))
473
+    {
474
+      LIGHT_ERR("could not save brightness");
475
+      return FALSE;
476
+    }
477
+
478
+    return TRUE;
479
+  }
480
+
481
+  if(light_Configuration.operationMode == LIGHT_RESTORE){
482
+    if(!light_restoreBrightness(light_Configuration.specifiedController)){
483
+      LIGHT_ERR("could not restore brightness");
484
+      return FALSE;
485
+    }
486
+
487
+    return TRUE;
488
+  }
489
+
449
   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);
490
   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);
450
 
491
 
451
   printf("You did not specify a valid combination of commandline arguments. Have some help: \n");
492
   printf("You did not specify a valid combination of commandline arguments. Have some help: \n");
483
     case LIGHT_MIN_CAP:
524
     case LIGHT_MIN_CAP:
484
       spfVal = sprintf(returner, "/etc/light/mincap/%s", controller);
525
       spfVal = sprintf(returner, "/etc/light/mincap/%s", controller);
485
       break;
526
       break;
527
+    case LIGHT_SAVERESTORE:
528
+      spfVal = sprintf(returner, "/etc/light/save/%s", controller);
529
+      break;
486
   }
530
   }
487
 
531
 
488
   if(spfVal < 0)
532
   if(spfVal < 0)
768
 
812
 
769
   return TRUE;
813
   return TRUE;
770
 }
814
 }
815
+
816
+LIGHT_BOOL light_saveBrightness(char const *controller, unsigned long v){
817
+  char *savePath = NULL;
818
+  if(!light_genPath(controller, LIGHT_SAVERESTORE, &savePath))
819
+  {
820
+    LIGHT_ERR("could not generate path to save/restore file");
821
+    return FALSE;
822
+  }
823
+
824
+  if(!light_writeULong(savePath, v))
825
+  {
826
+    LIGHT_ERR("could not write to save/restore file");
827
+    free(savePath);
828
+    return FALSE;
829
+  }
830
+
831
+  free(savePath);
832
+  return TRUE;
833
+}
834
+
835
+LIGHT_BOOL light_restoreBrightness(char const *controller){
836
+  char *restorePath = NULL;
837
+  unsigned long v = 0;
838
+
839
+  if(!light_genPath(controller, LIGHT_SAVERESTORE, &restorePath))
840
+  {
841
+    LIGHT_ERR("could not generate path to save/restore file");
842
+    return FALSE;
843
+  }
844
+
845
+  if(!light_readULong(restorePath, &v))
846
+  {
847
+    LIGHT_ERR("could not read saved value");
848
+    free(restorePath);
849
+    return FALSE;
850
+  }
851
+
852
+  if(!light_setBrightness(controller, v))
853
+  {
854
+    LIGHT_ERR("could not set restored brightness");
855
+    free(restorePath);
856
+    return FALSE;
857
+  }
858
+
859
+  free(restorePath);
860
+  return TRUE;
861
+}