Browse Source

Merge branch 'add-multiply-command' of https://github.com/TheJakeSchmidt/light into TheJakeSchmidt-add-multiply-command

Joachim Nilsson 5 years ago
parent
commit
1a959445de
2 changed files with 85 additions and 7 deletions
  1. 81
    6
      src/light.c
  2. 4
    1
      src/light.h

+ 81
- 6
src/light.c View File

191
         
191
         
192
         "  -A          Increase brightness by value\n"
192
         "  -A          Increase brightness by value\n"
193
         "  -U          Decrease brightness by value\n" 
193
         "  -U          Decrease brightness by value\n" 
194
+        "  -T          Multiply brightness by value (can be a non-whole number, ignores raw mode)\n"
194
         "  -S          Set brightness to value\n"
195
         "  -S          Set brightness to value\n"
195
         "  -G          Get brightness\n"
196
         "  -G          Get brightness\n"
196
         "  -N          Set minimum brightness to value\n"
197
         "  -N          Set minimum brightness to value\n"
201
 
202
 
202
         "\n"
203
         "\n"
203
         "Options:\n"
204
         "Options:\n"
204
-        "  -r          Interpret input and output values in raw mode\n"
205
+        "  -r          Interpret input and output values in raw mode (ignored for -T)\n"
205
         "  -s          Specify device target path to use, use -L to list available\n"
206
         "  -s          Specify device target path to use, use -L to list available\n"
206
         "  -v          Specify the verbosity level (default 0)\n"
207
         "  -v          Specify the verbosity level (default 0)\n"
207
         "                 0: Values only\n"
208
         "                 0: Values only\n"
235
     
236
     
236
     char ctrl_name[NAME_MAX];
237
     char ctrl_name[NAME_MAX];
237
     bool need_value = false;
238
     bool need_value = false;
239
+    bool need_float_value = false;
238
     bool need_target = true; // default cmd is get brightness
240
     bool need_target = true; // default cmd is get brightness
239
     bool specified_target = false;
241
     bool specified_target = false;
240
     snprintf(ctrl_name, sizeof(ctrl_name), "%s", "sysfs/backlight/auto");
242
     snprintf(ctrl_name, sizeof(ctrl_name), "%s", "sysfs/backlight/auto");
241
     
243
     
242
-    while((curr_arg = getopt(argc, argv, "HhVGSLMNPAUOIv:s:r")) != -1)
244
+    while((curr_arg = getopt(argc, argv, "HhVGSLMNPAUTOIv:s:r")) != -1)
243
     {
245
     {
244
         switch(curr_arg)
246
         switch(curr_arg)
245
         {
247
         {
313
                 need_target = true;
315
                 need_target = true;
314
                 need_value = true;
316
                 need_value = true;
315
                 break;
317
                 break;
318
+            case 'T':
319
+                _light_set_context_command(ctx, light_cmd_mul_brightness);
320
+                need_target = true;
321
+                need_float_value = true;
322
+                break;
316
             case 'O':
323
             case 'O':
317
                 _light_set_context_command(ctx, light_cmd_save_brightness);
324
                 _light_set_context_command(ctx, light_cmd_save_brightness);
318
                 need_target = true;
325
                 need_target = true;
348
         
355
         
349
         ctx->run_params.device_target = curr_target;
356
         ctx->run_params.device_target = curr_target;
350
     }
357
     }
351
-    
352
-    if(need_value)
358
+
359
+    if(need_value || need_float_value)
353
     {
360
     {
354
         if ( (argc - optind) != 1)
361
         if ( (argc - optind) != 1)
355
         {
362
         {
357
             _light_print_usage();
364
             _light_print_usage();
358
             return false;
365
             return false;
359
         }
366
         }
367
+    }
360
 
368
 
369
+    if (need_value)
370
+    {
361
         if (ctx->run_params.raw_mode)
371
         if (ctx->run_params.raw_mode)
362
         {
372
         {
363
             if (sscanf(argv[optind], "%lu", &ctx->run_params.value) != 1)
373
             if (sscanf(argv[optind], "%lu", &ctx->run_params.value) != 1)
366
                 _light_print_usage();
376
                 _light_print_usage();
367
                 return false;
377
                 return false;
368
             }
378
             }
369
-            
370
         }
379
         }
371
         else
380
         else
372
         {
381
         {
390
             ctx->run_params.value = raw_value;
399
             ctx->run_params.value = raw_value;
391
         }
400
         }
392
     }
401
     }
393
-    
402
+
403
+    if (need_float_value)
404
+    {
405
+        if (sscanf(argv[optind], "%f", &ctx->run_params.float_value) != 1)
406
+        {
407
+            fprintf(stderr, "<value> is not a float.\n\n");
408
+            _light_print_usage();
409
+            return false;
410
+        }
411
+    }
412
+
394
     return true;
413
     return true;
395
     
414
     
396
 }
415
 }
913
     return true;
932
     return true;
914
 }
933
 }
915
 
934
 
935
+bool light_cmd_mul_brightness(light_context_t *ctx)
936
+{
937
+    light_device_target_t *target = ctx->run_params.device_target;
938
+    if(target == NULL)
939
+    {
940
+        LIGHT_ERR("didn't have a valid target, programmer mistake");
941
+        return false;
942
+    }
943
+
944
+    uint64_t value = 0;
945
+    if(!target->get_value(target, &value))
946
+    {
947
+        LIGHT_ERR("failed to read from target");
948
+        return false;
949
+    }
950
+
951
+    uint64_t max_value = 0;
952
+    if(!target->get_max_value(target, &max_value))
953
+    {
954
+        LIGHT_ERR("failed to read from target");
955
+        return false;
956
+    }
957
+
958
+    uint64_t old_value = value;
959
+    value *= ctx->run_params.float_value;
960
+    
961
+    // Check that we actually de/increase value
962
+    if( value == old_value ) {
963
+        if( ctx->runs_params.float_value > 1 ) {
964
+            value++;
965
+        }
966
+        if( ctx->runs_params.float_value < 1 && value > 0 ) {
967
+            value--;
968
+        }
969
+    }
970
+
971
+    uint64_t mincap = _light_get_min_cap(ctx);
972
+    if(mincap > value)
973
+    {
974
+        value = mincap;
975
+    }
976
+
977
+    if(value > max_value)
978
+    {
979
+        value = max_value;
980
+    }
981
+
982
+    if(!target->set_value(target, value))
983
+    {
984
+        LIGHT_ERR("failed to write to target");
985
+        return false;
986
+    }
987
+
988
+    return true;
989
+}
990
+
916
 bool light_cmd_save_brightness(light_context_t *ctx)
991
 bool light_cmd_save_brightness(light_context_t *ctx)
917
 {
992
 {
918
     char target_path[NAME_MAX];
993
     char target_path[NAME_MAX];

+ 4
- 1
src/light.h View File

72
 {
72
 {
73
     struct 
73
     struct 
74
     {
74
     {
75
-        LFUNCCOMMAND            command; // What command was issued 
75
+        LFUNCCOMMAND            command; // What command was issued
76
+        // Only one of value and raw_value is populated; which one depends on the command
76
         uint64_t                value; // The input value, in raw mode
77
         uint64_t                value; // The input value, in raw mode
78
+        float                   float_value; // The input value as a float
77
         bool                    raw_mode; // Whether or not we use raw or percentage mode
79
         bool                    raw_mode; // Whether or not we use raw or percentage mode
78
         light_device_target_t   *device_target; // The device target to act on
80
         light_device_target_t   *device_target; // The device target to act on
79
     } run_params;
81
     } run_params;
98
 bool light_cmd_get_min_brightness(light_context_t *ctx); // P
100
 bool light_cmd_get_min_brightness(light_context_t *ctx); // P
99
 bool light_cmd_add_brightness(light_context_t *ctx); // A
101
 bool light_cmd_add_brightness(light_context_t *ctx); // A
100
 bool light_cmd_sub_brightness(light_context_t *ctx); // U
102
 bool light_cmd_sub_brightness(light_context_t *ctx); // U
103
+bool light_cmd_mul_brightness(light_context_t *ctx); // T
101
 bool light_cmd_save_brightness(light_context_t *ctx); // O
104
 bool light_cmd_save_brightness(light_context_t *ctx); // O
102
 bool light_cmd_restore_brightness(light_context_t *ctx); // I
105
 bool light_cmd_restore_brightness(light_context_t *ctx); // I
103
 
106