ソースを参照

Added save/restore functionality

Fredrik Haikarainen 9 年 前
コミット
8dfe104b74
共有2 個のファイルを変更した104 個の追加5 個の削除を含む
  1. 10
    2
      include/light.h
  2. 94
    3
      src/light.c

+ 10
- 2
include/light.h ファイルの表示

@@ -48,7 +48,8 @@
48 48
 typedef enum LIGHT_TARGET {
49 49
   LIGHT_BRIGHTNESS = 0,
50 50
   LIGHT_MAX_BRIGHTNESS,
51
-  LIGHT_MIN_CAP
51
+  LIGHT_MIN_CAP,
52
+  LIGHT_SAVERESTORE
52 53
 } LIGHT_TARGET;
53 54
 
54 55
 typedef enum LIGHT_CTRL_MODE {
@@ -63,7 +64,10 @@ typedef enum LIGHT_OP_MODE {
63 64
   LIGHT_SUB,
64 65
   LIGHT_PRINT_HELP,   /* Prints help and exits  */
65 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 71
 } LIGHT_OP_MODE;
68 72
 
69 73
 typedef enum LIGHT_VAL_MODE {
@@ -142,4 +146,8 @@ LIGHT_BOOL light_setMinCap(char const *controller, unsigned long v);
142 146
 
143 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 153
 #endif /* LIGHT_H */

+ 94
- 3
src/light.c ファイルの表示

@@ -31,7 +31,7 @@ LIGHT_BOOL light_parseArguments(int argc, char** argv)
31 31
 
32 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 36
     switch(currFlag)
37 37
     {
@@ -65,6 +65,14 @@ LIGHT_BOOL light_parseArguments(int argc, char** argv)
65 65
         ASSERT_OPSET();
66 66
         light_Configuration.operationMode = LIGHT_LIST_CTRL;
67 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 77
       /* -- Targets -- */
70 78
       case 'b':
@@ -190,7 +198,9 @@ void light_printHelp(){
190 198
   printf("  -S:\t\tSet value\n");
191 199
   printf("  -A:\t\tAdd value\n");
192 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 205
   printf("Targets (can not be used in conjunction):\n");
196 206
   printf("  -b:\t\tBrightness (default)\n  \t\tUsed with [GSAU]\n\n");
@@ -227,7 +237,7 @@ LIGHT_BOOL light_initialize(int argc, char** argv)
227 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 241
   mkdirVal = mkdir("/etc/light", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
232 242
   if(mkdirVal != 0 && errno != EEXIST)
233 243
   {
@@ -242,6 +252,13 @@ LIGHT_BOOL light_initialize(int argc, char** argv)
242 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 262
   /* Make sure we have a valid controller before we proceed */
246 263
   if(light_Configuration.controllerMode == LIGHT_AUTO)
247 264
   {
@@ -371,6 +388,8 @@ LIGHT_BOOL light_execute()
371 388
       case LIGHT_MIN_CAP:
372 389
         (light_Configuration.valueMode == LIGHT_RAW) ? printf("%lu\n", minCap) : printf("%.2f\n", percentMinCap);
373 390
         break;
391
+      case LIGHT_SAVERESTORE:
392
+        break;
374 393
     }
375 394
 
376 395
     return TRUE;
@@ -426,6 +445,8 @@ LIGHT_BOOL light_execute()
426 445
         case LIGHT_PRINT_HELP:
427 446
         case LIGHT_PRINT_VERSION:
428 447
         case LIGHT_LIST_CTRL:
448
+        case LIGHT_SAVE:
449
+        case LIGHT_RESTORE:
429 450
           break;
430 451
       }
431 452
 
@@ -446,6 +467,26 @@ LIGHT_BOOL light_execute()
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 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 492
   printf("You did not specify a valid combination of commandline arguments. Have some help: \n");
@@ -483,6 +524,9 @@ LIGHT_BOOL light_genPath(char const *controller, LIGHT_TARGET type, char **buffe
483 524
     case LIGHT_MIN_CAP:
484 525
       spfVal = sprintf(returner, "/etc/light/mincap/%s", controller);
485 526
       break;
527
+    case LIGHT_SAVERESTORE:
528
+      spfVal = sprintf(returner, "/etc/light/save/%s", controller);
529
+      break;
486 530
   }
487 531
 
488 532
   if(spfVal < 0)
@@ -768,3 +812,50 @@ LIGHT_BOOL light_listControllers()
768 812
 
769 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
+}