瀏覽代碼

Merge branch 'TheJakeSchmidt-add-multiply-command'

Joachim Nilsson 5 年之前
父節點
當前提交
11b84d6821
共有 2 個檔案被更改,包括 84 行新增7 行删除
  1. 80
    6
      src/light.c
  2. 4
    1
      src/light.h

+ 80
- 6
src/light.c 查看文件

@@ -191,6 +191,7 @@ static void _light_print_usage()
191 191
         
192 192
         "  -A          Increase brightness by value\n"
193 193
         "  -U          Decrease brightness by value\n" 
194
+        "  -T          Multiply brightness by value (can be a non-whole number, ignores raw mode)\n"
194 195
         "  -S          Set brightness to value\n"
195 196
         "  -G          Get brightness\n"
196 197
         "  -N          Set minimum brightness to value\n"
@@ -201,7 +202,7 @@ static void _light_print_usage()
201 202
 
202 203
         "\n"
203 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 206
         "  -s          Specify device target path to use, use -L to list available\n"
206 207
         "  -v          Specify the verbosity level (default 0)\n"
207 208
         "                 0: Values only\n"
@@ -235,11 +236,12 @@ static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv)
235 236
     
236 237
     char ctrl_name[NAME_MAX];
237 238
     bool need_value = false;
239
+    bool need_float_value = false;
238 240
     bool need_target = true; // default cmd is get brightness
239 241
     bool specified_target = false;
240 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 246
         switch(curr_arg)
245 247
         {
@@ -313,6 +315,11 @@ static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv)
313 315
                 need_target = true;
314 316
                 need_value = true;
315 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 323
             case 'O':
317 324
                 _light_set_context_command(ctx, light_cmd_save_brightness);
318 325
                 need_target = true;
@@ -348,8 +355,8 @@ static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv)
348 355
         
349 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 361
         if ( (argc - optind) != 1)
355 362
         {
@@ -357,7 +364,10 @@ static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv)
357 364
             _light_print_usage();
358 365
             return false;
359 366
         }
367
+    }
360 368
 
369
+    if (need_value)
370
+    {
361 371
         if (ctx->run_params.raw_mode)
362 372
         {
363 373
             if (sscanf(argv[optind], "%lu", &ctx->run_params.value) != 1)
@@ -366,7 +376,6 @@ static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv)
366 376
                 _light_print_usage();
367 377
                 return false;
368 378
             }
369
-            
370 379
         }
371 380
         else
372 381
         {
@@ -390,7 +399,17 @@ static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv)
390 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 413
     return true;
395 414
     
396 415
 }
@@ -913,6 +932,61 @@ bool light_cmd_sub_brightness(light_context_t *ctx)
913 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
+    {
964
+        if(ctx->runs_params.float_value > 1)
965
+            value++;
966
+        if(ctx->runs_params.float_value < 1 && value > 0)
967
+            value--;
968
+    }
969
+
970
+    uint64_t mincap = _light_get_min_cap(ctx);
971
+    if(mincap > value)
972
+    {
973
+        value = mincap;
974
+    }
975
+
976
+    if(value > max_value)
977
+    {
978
+        value = max_value;
979
+    }
980
+
981
+    if(!target->set_value(target, value))
982
+    {
983
+        LIGHT_ERR("failed to write to target");
984
+        return false;
985
+    }
986
+
987
+    return true;
988
+}
989
+
916 990
 bool light_cmd_save_brightness(light_context_t *ctx)
917 991
 {
918 992
     char target_path[NAME_MAX];

+ 4
- 1
src/light.h 查看文件

@@ -72,8 +72,10 @@ struct _light_context_t
72 72
 {
73 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 77
         uint64_t                value; // The input value, in raw mode
78
+        float                   float_value; // The input value as a float
77 79
         bool                    raw_mode; // Whether or not we use raw or percentage mode
78 80
         light_device_target_t   *device_target; // The device target to act on
79 81
     } run_params;
@@ -98,6 +100,7 @@ bool light_cmd_set_min_brightness(light_context_t *ctx); // N
98 100
 bool light_cmd_get_min_brightness(light_context_t *ctx); // P
99 101
 bool light_cmd_add_brightness(light_context_t *ctx); // A
100 102
 bool light_cmd_sub_brightness(light_context_t *ctx); // U
103
+bool light_cmd_mul_brightness(light_context_t *ctx); // T
101 104
 bool light_cmd_save_brightness(light_context_t *ctx); // O
102 105
 bool light_cmd_restore_brightness(light_context_t *ctx); // I
103 106