|
@@ -17,28 +17,27 @@
|
17
|
17
|
|
18
|
18
|
/* Static helper functions for this file only, prefix with _ */
|
19
|
19
|
|
20
|
|
-
|
21
|
20
|
static void _light_add_enumerator_device(light_device_enumerator_t *enumerator, light_device_t *new_device)
|
22
|
21
|
{
|
23
|
22
|
// Create a new device array
|
24
|
23
|
uint64_t new_num_devices = enumerator->num_devices + 1;
|
25
|
24
|
light_device_t **new_devices = malloc(new_num_devices * sizeof(light_device_t*));
|
26
|
|
-
|
|
25
|
+
|
27
|
26
|
// Copy old device array to new one
|
28
|
27
|
for(uint64_t i = 0; i < enumerator->num_devices; i++)
|
29
|
28
|
{
|
30
|
29
|
new_devices[i] = enumerator->devices[i];
|
31
|
30
|
}
|
32
|
|
-
|
|
31
|
+
|
33
|
32
|
// Set the new device
|
34
|
33
|
new_devices[enumerator->num_devices] = new_device;
|
35
|
|
-
|
|
34
|
+
|
36
|
35
|
// Free the old devices array, if needed
|
37
|
36
|
if(enumerator->devices != NULL)
|
38
|
37
|
{
|
39
|
38
|
free(enumerator->devices);
|
40
|
39
|
}
|
41
|
|
-
|
|
40
|
+
|
42
|
41
|
// Replace the devices array with the new one
|
43
|
42
|
enumerator->devices = new_devices;
|
44
|
43
|
enumerator->num_devices = new_num_devices;
|
|
@@ -49,22 +48,22 @@ static void _light_add_device_target(light_device_t *device, light_device_target
|
49
|
48
|
// Create a new targets array
|
50
|
49
|
uint64_t new_num_targets = device->num_targets + 1;
|
51
|
50
|
light_device_target_t **new_targets = malloc(new_num_targets * sizeof(light_device_target_t*));
|
52
|
|
-
|
|
51
|
+
|
53
|
52
|
// Copy old targets array to new one
|
54
|
53
|
for(uint64_t i = 0; i < device->num_targets; i++)
|
55
|
54
|
{
|
56
|
55
|
new_targets[i] = device->targets[i];
|
57
|
56
|
}
|
58
|
|
-
|
|
57
|
+
|
59
|
58
|
// Set the new target
|
60
|
59
|
new_targets[device->num_targets] = new_target;
|
61
|
|
-
|
|
60
|
+
|
62
|
61
|
// Free the old targets array, if needed
|
63
|
62
|
if(device->targets != NULL)
|
64
|
63
|
{
|
65
|
64
|
free(device->targets);
|
66
|
65
|
}
|
67
|
|
-
|
|
66
|
+
|
68
|
67
|
// Replace the targets array with the new one
|
69
|
68
|
device->targets= new_targets;
|
70
|
69
|
device->num_targets = new_num_targets;
|
|
@@ -103,7 +102,7 @@ static uint64_t _light_get_min_cap(light_context_t *ctx)
|
103
|
102
|
{
|
104
|
103
|
return 0;
|
105
|
104
|
}
|
106
|
|
-
|
|
105
|
+
|
107
|
106
|
return minimum_value;
|
108
|
107
|
}
|
109
|
108
|
|
|
@@ -116,7 +115,7 @@ static light_device_enumerator_t* _light_find_enumerator(light_context_t *ctx, c
|
116
|
115
|
return ctx->enumerators[e];
|
117
|
116
|
}
|
118
|
117
|
}
|
119
|
|
-
|
|
118
|
+
|
120
|
119
|
return NULL;
|
121
|
120
|
}
|
122
|
121
|
|
|
@@ -129,7 +128,7 @@ static light_device_t* _light_find_device(light_device_enumerator_t *en, char co
|
129
|
128
|
return en->devices[d];
|
130
|
129
|
}
|
131
|
130
|
}
|
132
|
|
-
|
|
131
|
+
|
133
|
132
|
return NULL;
|
134
|
133
|
}
|
135
|
134
|
|
|
@@ -142,24 +141,24 @@ static light_device_target_t* _light_find_target(light_device_t * dev, char cons
|
142
|
141
|
return dev->targets[t];
|
143
|
142
|
}
|
144
|
143
|
}
|
145
|
|
-
|
|
144
|
+
|
146
|
145
|
return NULL;
|
147
|
146
|
}
|
148
|
147
|
|
149
|
148
|
static bool _light_raw_to_percent(light_device_target_t *target, uint64_t inraw, double *outpercent)
|
150
|
149
|
{
|
151
|
|
- double inraw_d = (double)inraw;
|
152
|
|
- uint64_t max_value = 0;
|
153
|
|
- if(!target->get_max_value(target, &max_value))
|
154
|
|
- {
|
155
|
|
- LIGHT_ERR("couldn't read from target");
|
156
|
|
- return false;
|
157
|
|
- }
|
158
|
|
- double max_value_d = (double)max_value;
|
159
|
|
- double percent = light_percent_clamp((inraw_d / max_value_d) * 100.0);
|
160
|
|
- *outpercent = percent;
|
161
|
|
-
|
162
|
|
- return true;
|
|
150
|
+ double inraw_d = (double)inraw;
|
|
151
|
+ uint64_t max_value = 0;
|
|
152
|
+ if(!target->get_max_value(target, &max_value))
|
|
153
|
+ {
|
|
154
|
+ LIGHT_ERR("couldn't read from target");
|
|
155
|
+ return false;
|
|
156
|
+ }
|
|
157
|
+ double max_value_d = (double)max_value;
|
|
158
|
+ double percent = light_percent_clamp((inraw_d / max_value_d) * 100.0);
|
|
159
|
+ *outpercent = percent;
|
|
160
|
+
|
|
161
|
+ return true;
|
163
|
162
|
}
|
164
|
163
|
|
165
|
164
|
static bool _light_percent_to_raw(light_device_target_t *target, double inpercent, uint64_t *outraw)
|
|
@@ -175,7 +174,7 @@ static bool _light_percent_to_raw(light_device_target_t *target, double inpercen
|
175
|
174
|
double target_value_d = max_value_d * (light_percent_clamp(inpercent) / 100.0);
|
176
|
175
|
uint64_t target_value = LIGHT_CLAMP((uint64_t)target_value_d, 0, max_value);
|
177
|
176
|
*outraw = target_value;
|
178
|
|
-
|
|
177
|
+
|
179
|
178
|
return true;
|
180
|
179
|
}
|
181
|
180
|
|
|
@@ -224,7 +223,7 @@ static bool _light_set_context_command(light_context_t *ctx, LFUNCCOMMAND new_cm
|
224
|
223
|
LIGHT_WARN("a command was already set. ignoring.");
|
225
|
224
|
return false;
|
226
|
225
|
}
|
227
|
|
-
|
|
226
|
+
|
228
|
227
|
ctx->run_params.command = new_cmd;
|
229
|
228
|
return true;
|
230
|
229
|
}
|
|
@@ -233,20 +232,20 @@ static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv)
|
233
|
232
|
{
|
234
|
233
|
int32_t curr_arg = -1;
|
235
|
234
|
int32_t log_level = 0;
|
236
|
|
-
|
|
235
|
+
|
237
|
236
|
char ctrl_name[NAME_MAX];
|
238
|
237
|
bool need_value = false;
|
239
|
238
|
bool need_float_value = false;
|
240
|
239
|
bool need_target = true; // default cmd is get brightness
|
241
|
240
|
bool specified_target = false;
|
242
|
241
|
snprintf(ctrl_name, sizeof(ctrl_name), "%s", "sysfs/backlight/auto");
|
243
|
|
-
|
|
242
|
+
|
244
|
243
|
while((curr_arg = getopt(argc, argv, "HhVGSLMNPAUTOIv:s:r")) != -1)
|
245
|
244
|
{
|
246
|
245
|
switch(curr_arg)
|
247
|
246
|
{
|
248
|
247
|
// Options
|
249
|
|
-
|
|
248
|
+
|
250
|
249
|
case 'v':
|
251
|
250
|
if(sscanf(optarg, "%i", &log_level) != 1)
|
252
|
251
|
{
|
|
@@ -254,14 +253,14 @@ static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv)
|
254
|
253
|
_light_print_usage();
|
255
|
254
|
return false;
|
256
|
255
|
}
|
257
|
|
-
|
|
256
|
+
|
258
|
257
|
if(log_level < 0 || log_level > 3)
|
259
|
258
|
{
|
260
|
259
|
fprintf(stderr, "-v argument must be between 0 and 3.\n\n");
|
261
|
260
|
_light_print_usage();
|
262
|
261
|
return false;
|
263
|
262
|
}
|
264
|
|
-
|
|
263
|
+
|
265
|
264
|
light_loglevel = (light_loglevel_t)log_level;
|
266
|
265
|
break;
|
267
|
266
|
case 's':
|
|
@@ -271,7 +270,7 @@ static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv)
|
271
|
270
|
case 'r':
|
272
|
271
|
ctx->run_params.raw_mode = true;
|
273
|
272
|
break;
|
274
|
|
-
|
|
273
|
+
|
275
|
274
|
// Commands
|
276
|
275
|
case 'H':
|
277
|
276
|
case 'h':
|
|
@@ -335,7 +334,7 @@ static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv)
|
335
|
334
|
{
|
336
|
335
|
_light_set_context_command(ctx, light_cmd_get_brightness);
|
337
|
336
|
}
|
338
|
|
-
|
|
337
|
+
|
339
|
338
|
if(need_target)
|
340
|
339
|
{
|
341
|
340
|
light_device_target_t *curr_target = light_find_device_target(ctx, ctrl_name);
|
|
@@ -352,7 +351,7 @@ static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv)
|
352
|
351
|
curr_target = light_find_device_target(ctx, "util/test/dryrun");
|
353
|
352
|
}
|
354
|
353
|
}
|
355
|
|
-
|
|
354
|
+
|
356
|
355
|
ctx->run_params.device_target = curr_target;
|
357
|
356
|
}
|
358
|
357
|
|
|
@@ -386,7 +385,7 @@ static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv)
|
386
|
385
|
_light_print_usage();
|
387
|
386
|
return false;
|
388
|
387
|
}
|
389
|
|
-
|
|
388
|
+
|
390
|
389
|
percent_value = light_percent_clamp(percent_value);
|
391
|
390
|
|
392
|
391
|
uint64_t raw_value = 0;
|
|
@@ -395,7 +394,7 @@ static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv)
|
395
|
394
|
LIGHT_ERR("failed to convert from percent to raw for device target");
|
396
|
395
|
return false;
|
397
|
396
|
}
|
398
|
|
-
|
|
397
|
+
|
399
|
398
|
ctx->run_params.value = raw_value;
|
400
|
399
|
}
|
401
|
400
|
}
|
|
@@ -411,12 +410,9 @@ static bool _light_parse_arguments(light_context_t *ctx, int argc, char** argv)
|
411
|
410
|
}
|
412
|
411
|
|
413
|
412
|
return true;
|
414
|
|
-
|
415
|
413
|
}
|
416
|
414
|
|
417
|
415
|
|
418
|
|
-
|
419
|
|
-
|
420
|
416
|
/* API function definitions */
|
421
|
417
|
|
422
|
418
|
light_context_t* light_initialize(int argc, char **argv)
|
|
@@ -456,7 +452,6 @@ light_context_t* light_initialize(int argc, char **argv)
|
456
|
452
|
else
|
457
|
453
|
{
|
458
|
454
|
char *xdg_conf = getenv("XDG_CONFIG_HOME");
|
459
|
|
-
|
460
|
455
|
if(xdg_conf != NULL)
|
461
|
456
|
{
|
462
|
457
|
snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s/light", xdg_conf);
|
|
@@ -466,7 +461,7 @@ light_context_t* light_initialize(int argc, char **argv)
|
466
|
461
|
snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s/.config/light", getenv("HOME"));
|
467
|
462
|
}
|
468
|
463
|
}
|
469
|
|
-
|
|
464
|
+
|
470
|
465
|
// Make sure the configuration folder exists, otherwise attempt to create it
|
471
|
466
|
int32_t rc = light_mkpath(new_ctx->sys_params.conf_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
|
472
|
467
|
if(rc && errno != EEXIST)
|
|
@@ -474,7 +469,7 @@ light_context_t* light_initialize(int argc, char **argv)
|
474
|
469
|
LIGHT_WARN("couldn't create configuration directory");
|
475
|
470
|
return false;
|
476
|
471
|
}
|
477
|
|
-
|
|
472
|
+
|
478
|
473
|
// Create the built-in enumerators
|
479
|
474
|
light_create_enumerator(new_ctx, "sysfs", &impl_sysfs_init, &impl_sysfs_free);
|
480
|
475
|
light_create_enumerator(new_ctx, "util", &impl_util_init, &impl_util_free);
|
|
@@ -496,7 +491,7 @@ light_context_t* light_initialize(int argc, char **argv)
|
496
|
491
|
LIGHT_ERR("failed to parse arguments");
|
497
|
492
|
return NULL;
|
498
|
493
|
}
|
499
|
|
-
|
|
494
|
+
|
500
|
495
|
return new_ctx;
|
501
|
496
|
}
|
502
|
497
|
|
|
@@ -507,7 +502,7 @@ bool light_execute(light_context_t *ctx)
|
507
|
502
|
LIGHT_ERR("run parameters command was null, can't execute");
|
508
|
503
|
return false;
|
509
|
504
|
}
|
510
|
|
-
|
|
505
|
+
|
511
|
506
|
return ctx->run_params.command(ctx);
|
512
|
507
|
}
|
513
|
508
|
|
|
@@ -517,7 +512,7 @@ void light_free(light_context_t *ctx)
|
517
|
512
|
{
|
518
|
513
|
LIGHT_WARN("failed to free all enumerators");
|
519
|
514
|
}
|
520
|
|
-
|
|
515
|
+
|
521
|
516
|
free(ctx);
|
522
|
517
|
}
|
523
|
518
|
|
|
@@ -526,29 +521,29 @@ light_device_enumerator_t * light_create_enumerator(light_context_t *ctx, char c
|
526
|
521
|
// Create a new enumerator array
|
527
|
522
|
uint64_t new_num_enumerators = ctx->num_enumerators + 1;
|
528
|
523
|
light_device_enumerator_t **new_enumerators = malloc(new_num_enumerators * sizeof(light_device_enumerator_t*));
|
529
|
|
-
|
|
524
|
+
|
530
|
525
|
// Copy old enumerator array to new one
|
531
|
526
|
for(uint64_t i = 0; i < ctx->num_enumerators; i++)
|
532
|
527
|
{
|
533
|
528
|
new_enumerators[i] = ctx->enumerators[i];
|
534
|
529
|
}
|
535
|
|
-
|
|
530
|
+
|
536
|
531
|
// Allocate the new enumerator
|
537
|
532
|
new_enumerators[ctx->num_enumerators] = malloc(sizeof(light_device_enumerator_t));
|
538
|
533
|
light_device_enumerator_t *returner = new_enumerators[ctx->num_enumerators];
|
539
|
|
-
|
|
534
|
+
|
540
|
535
|
returner->devices = NULL;
|
541
|
536
|
returner->num_devices = 0;
|
542
|
537
|
returner->init = init_func;
|
543
|
538
|
returner->free = free_func;
|
544
|
539
|
snprintf(returner->name, sizeof(returner->name), "%s", name);
|
545
|
|
-
|
|
540
|
+
|
546
|
541
|
// Free the old enumerator array, if needed
|
547
|
542
|
if(ctx->enumerators != NULL)
|
548
|
543
|
{
|
549
|
544
|
free(ctx->enumerators);
|
550
|
545
|
}
|
551
|
|
-
|
|
546
|
+
|
552
|
547
|
// Replace the enumerator array with the new one
|
553
|
548
|
ctx->enumerators = new_enumerators;
|
554
|
549
|
ctx->num_enumerators = new_num_enumerators;
|
|
@@ -568,7 +563,7 @@ bool light_init_enumerators(light_context_t *ctx)
|
568
|
563
|
success = false;
|
569
|
564
|
}
|
570
|
565
|
}
|
571
|
|
-
|
|
566
|
+
|
572
|
567
|
return success;
|
573
|
568
|
}
|
574
|
569
|
|
|
@@ -578,30 +573,29 @@ bool light_free_enumerators(light_context_t *ctx)
|
578
|
573
|
for(uint64_t i = 0; i < ctx->num_enumerators; i++)
|
579
|
574
|
{
|
580
|
575
|
light_device_enumerator_t * curr_enumerator = ctx->enumerators[i];
|
581
|
|
-
|
582
|
576
|
if(!curr_enumerator->free(curr_enumerator))
|
583
|
577
|
{
|
584
|
578
|
success = false;
|
585
|
579
|
}
|
586
|
|
-
|
|
580
|
+
|
587
|
581
|
if(curr_enumerator->devices != NULL)
|
588
|
582
|
{
|
589
|
583
|
for(uint64_t d = 0; d < curr_enumerator->num_devices; d++)
|
590
|
584
|
{
|
591
|
585
|
light_delete_device(curr_enumerator->devices[d]);
|
592
|
586
|
}
|
593
|
|
-
|
|
587
|
+
|
594
|
588
|
free(curr_enumerator->devices);
|
595
|
589
|
curr_enumerator->devices = NULL;
|
596
|
590
|
}
|
597
|
|
-
|
|
591
|
+
|
598
|
592
|
free(curr_enumerator);
|
599
|
593
|
}
|
600
|
|
-
|
|
594
|
+
|
601
|
595
|
free(ctx->enumerators);
|
602
|
596
|
ctx->enumerators = NULL;
|
603
|
597
|
ctx->num_enumerators = 0;
|
604
|
|
-
|
|
598
|
+
|
605
|
599
|
return success;
|
606
|
600
|
}
|
607
|
601
|
|
|
@@ -614,11 +608,11 @@ bool light_split_target_path(char const *in_path, light_target_path_t *out_path)
|
614
|
608
|
LIGHT_WARN("invalid path passed to split_target_path");
|
615
|
609
|
return false;
|
616
|
610
|
}
|
617
|
|
-
|
|
611
|
+
|
618
|
612
|
size_t size = end - begin;
|
619
|
613
|
strncpy(out_path->enumerator, begin, size);
|
620
|
614
|
out_path->enumerator[size] = '\0';
|
621
|
|
-
|
|
615
|
+
|
622
|
616
|
begin = end + 1;
|
623
|
617
|
end = strstr(begin, "/");
|
624
|
618
|
if(end == NULL)
|
|
@@ -626,13 +620,13 @@ bool light_split_target_path(char const *in_path, light_target_path_t *out_path)
|
626
|
620
|
LIGHT_WARN("invalid path passed to split_target_path");
|
627
|
621
|
return false;
|
628
|
622
|
}
|
629
|
|
-
|
|
623
|
+
|
630
|
624
|
size = end - begin;
|
631
|
625
|
strncpy(out_path->device, begin, size);
|
632
|
626
|
out_path->device[size] = '\0';
|
633
|
|
-
|
|
627
|
+
|
634
|
628
|
strcpy(out_path->target, end + 1);
|
635
|
|
-
|
|
629
|
+
|
636
|
630
|
return true;
|
637
|
631
|
}
|
638
|
632
|
|
|
@@ -644,7 +638,7 @@ light_device_target_t* light_find_device_target(light_context_t *ctx, char const
|
644
|
638
|
LIGHT_WARN("light_find_device_target needs a path in the format of \"enumerator/device/target\", the following format is not recognized: \"%s\"", name);
|
645
|
639
|
return NULL;
|
646
|
640
|
}
|
647
|
|
-
|
|
641
|
+
|
648
|
642
|
/*
|
649
|
643
|
Uncomment to debug the split function
|
650
|
644
|
printf("enumerator: %s %u\ndevice: %s %u\ntarget: %s %u\n",
|
|
@@ -652,30 +646,30 @@ light_device_target_t* light_find_device_target(light_context_t *ctx, char const
|
652
|
646
|
new_path.device, strlen(new_path.device),
|
653
|
647
|
new_path.target, strlen(new_path.target));
|
654
|
648
|
*/
|
655
|
|
-
|
|
649
|
+
|
656
|
650
|
// find a matching enumerator
|
657
|
|
-
|
|
651
|
+
|
658
|
652
|
light_device_enumerator_t *enumerator = _light_find_enumerator(ctx, new_path.enumerator);
|
659
|
653
|
if(enumerator == NULL)
|
660
|
654
|
{
|
661
|
655
|
LIGHT_WARN("no such enumerator, \"%s\"", new_path.enumerator);
|
662
|
656
|
return NULL;
|
663
|
657
|
}
|
664
|
|
-
|
|
658
|
+
|
665
|
659
|
light_device_t *device = _light_find_device(enumerator, new_path.device);
|
666
|
660
|
if(device == NULL)
|
667
|
661
|
{
|
668
|
662
|
LIGHT_WARN("no such device, \"%s\"", new_path.device);
|
669
|
663
|
return NULL;
|
670
|
664
|
}
|
671
|
|
-
|
|
665
|
+
|
672
|
666
|
light_device_target_t *target = _light_find_target(device, new_path.target);
|
673
|
667
|
if(target == NULL)
|
674
|
668
|
{
|
675
|
669
|
LIGHT_WARN("no such target, \"%s\"", new_path.target);
|
676
|
670
|
return NULL;
|
677
|
671
|
}
|
678
|
|
-
|
|
672
|
+
|
679
|
673
|
return target;
|
680
|
674
|
}
|
681
|
675
|
|
|
@@ -703,12 +697,11 @@ bool light_cmd_list_devices(light_context_t *ctx)
|
703
|
697
|
for(uint64_t target = 0; target < curr_device->num_targets; target++)
|
704
|
698
|
{
|
705
|
699
|
light_device_target_t *curr_target = curr_device->targets[target];
|
706
|
|
-
|
707
|
700
|
printf("\t%s/%s/%s\n", curr_enumerator->name, curr_device->name, curr_target->name);
|
708
|
701
|
}
|
709
|
702
|
}
|
710
|
703
|
}
|
711
|
|
-
|
|
704
|
+
|
712
|
705
|
return true;
|
713
|
706
|
}
|
714
|
707
|
|
|
@@ -720,21 +713,20 @@ bool light_cmd_set_brightness(light_context_t *ctx)
|
720
|
713
|
LIGHT_ERR("didn't have a valid target, programmer mistake");
|
721
|
714
|
return false;
|
722
|
715
|
}
|
723
|
|
-
|
724
|
|
-
|
|
716
|
+
|
725
|
717
|
uint64_t mincap = _light_get_min_cap(ctx);
|
726
|
718
|
uint64_t value = ctx->run_params.value;
|
727
|
719
|
if(mincap > value)
|
728
|
720
|
{
|
729
|
721
|
value = mincap;
|
730
|
722
|
}
|
731
|
|
-
|
|
723
|
+
|
732
|
724
|
if(!target->set_value(target, value))
|
733
|
725
|
{
|
734
|
726
|
LIGHT_ERR("failed to write to target");
|
735
|
727
|
return false;
|
736
|
728
|
}
|
737
|
|
-
|
|
729
|
+
|
738
|
730
|
return true;
|
739
|
731
|
}
|
740
|
732
|
|
|
@@ -746,14 +738,14 @@ bool light_cmd_get_brightness(light_context_t *ctx)
|
746
|
738
|
LIGHT_ERR("didn't have a valid target, programmer mistake");
|
747
|
739
|
return false;
|
748
|
740
|
}
|
749
|
|
-
|
|
741
|
+
|
750
|
742
|
uint64_t value = 0;
|
751
|
743
|
if(!target->get_value(target, &value))
|
752
|
744
|
{
|
753
|
745
|
LIGHT_ERR("failed to read from target");
|
754
|
746
|
return false;
|
755
|
747
|
}
|
756
|
|
-
|
|
748
|
+
|
757
|
749
|
if(ctx->run_params.raw_mode)
|
758
|
750
|
{
|
759
|
751
|
printf("%" PRIu64 "\n", value);
|
|
@@ -768,7 +760,7 @@ bool light_cmd_get_brightness(light_context_t *ctx)
|
768
|
760
|
}
|
769
|
761
|
printf("%.2f\n", percent);
|
770
|
762
|
}
|
771
|
|
-
|
|
763
|
+
|
772
|
764
|
return true;
|
773
|
765
|
}
|
774
|
766
|
|
|
@@ -780,20 +772,20 @@ bool light_cmd_get_max_brightness(light_context_t *ctx)
|
780
|
772
|
LIGHT_ERR("didn't have a valid target, programmer mistake");
|
781
|
773
|
return false;
|
782
|
774
|
}
|
783
|
|
-
|
|
775
|
+
|
784
|
776
|
if(!ctx->run_params.raw_mode)
|
785
|
777
|
{
|
786
|
778
|
printf("100.0\n");
|
787
|
779
|
return true;
|
788
|
780
|
}
|
789
|
|
-
|
|
781
|
+
|
790
|
782
|
uint64_t max_value = 0;
|
791
|
783
|
if(!target->get_max_value(target, &max_value))
|
792
|
784
|
{
|
793
|
785
|
LIGHT_ERR("failed to read from device target");
|
794
|
786
|
return false;
|
795
|
787
|
}
|
796
|
|
-
|
|
788
|
+
|
797
|
789
|
printf("%" PRIu64 "\n", max_value);
|
798
|
790
|
return true;
|
799
|
791
|
}
|
|
@@ -802,7 +794,7 @@ bool light_cmd_set_min_brightness(light_context_t *ctx)
|
802
|
794
|
{
|
803
|
795
|
char target_path[NAME_MAX];
|
804
|
796
|
_light_get_target_path(ctx, target_path, sizeof(target_path));
|
805
|
|
-
|
|
797
|
+
|
806
|
798
|
// Make sure the target folder exists, otherwise attempt to create it
|
807
|
799
|
int32_t rc = light_mkpath(target_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
|
808
|
800
|
if(rc && errno != EEXIST)
|
|
@@ -810,16 +802,16 @@ bool light_cmd_set_min_brightness(light_context_t *ctx)
|
810
|
802
|
LIGHT_ERR("couldn't create target directory for minimum brightness");
|
811
|
803
|
return false;
|
812
|
804
|
}
|
813
|
|
-
|
|
805
|
+
|
814
|
806
|
char target_filepath[NAME_MAX];
|
815
|
807
|
_light_get_target_file(ctx, target_filepath, sizeof(target_filepath), "minimum");
|
816
|
|
-
|
|
808
|
+
|
817
|
809
|
if(!light_file_write_uint64(target_filepath, ctx->run_params.value))
|
818
|
810
|
{
|
819
|
811
|
LIGHT_ERR("couldn't write value to minimum file");
|
820
|
812
|
return false;
|
821
|
813
|
}
|
822
|
|
-
|
|
814
|
+
|
823
|
815
|
return true;
|
824
|
816
|
}
|
825
|
817
|
|
|
@@ -842,7 +834,7 @@ bool light_cmd_get_min_brightness(light_context_t *ctx)
|
842
|
834
|
|
843
|
835
|
return true;
|
844
|
836
|
}
|
845
|
|
-
|
|
837
|
+
|
846
|
838
|
if(ctx->run_params.raw_mode)
|
847
|
839
|
{
|
848
|
840
|
printf("%" PRIu64 "\n", minimum_value);
|
|
@@ -855,10 +847,10 @@ bool light_cmd_get_min_brightness(light_context_t *ctx)
|
855
|
847
|
LIGHT_ERR("failed to convert value from raw to percent for device target");
|
856
|
848
|
return false;
|
857
|
849
|
}
|
858
|
|
-
|
|
850
|
+
|
859
|
851
|
printf("%.2f\n", minimum_d);
|
860
|
852
|
}
|
861
|
|
-
|
|
853
|
+
|
862
|
854
|
return true;
|
863
|
855
|
}
|
864
|
856
|
|
|
@@ -870,41 +862,39 @@ bool light_cmd_add_brightness(light_context_t *ctx)
|
870
|
862
|
LIGHT_ERR("didn't have a valid target, programmer mistake");
|
871
|
863
|
return false;
|
872
|
864
|
}
|
873
|
|
-
|
|
865
|
+
|
874
|
866
|
uint64_t value = 0;
|
875
|
867
|
if(!target->get_value(target, &value))
|
876
|
868
|
{
|
877
|
869
|
LIGHT_ERR("failed to read from target");
|
878
|
870
|
return false;
|
879
|
871
|
}
|
880
|
|
-
|
|
872
|
+
|
881
|
873
|
uint64_t max_value = 0;
|
882
|
874
|
if(!target->get_max_value(target, &max_value))
|
883
|
875
|
{
|
884
|
876
|
LIGHT_ERR("failed to read from target");
|
885
|
877
|
return false;
|
886
|
878
|
}
|
887
|
|
-
|
|
879
|
+
|
888
|
880
|
value += ctx->run_params.value;
|
889
|
|
-
|
890
|
881
|
uint64_t mincap = _light_get_min_cap(ctx);
|
891
|
882
|
if(mincap > value)
|
892
|
883
|
{
|
893
|
884
|
value = mincap;
|
894
|
885
|
}
|
895
|
|
-
|
896
|
|
-
|
|
886
|
+
|
897
|
887
|
if(value > max_value)
|
898
|
888
|
{
|
899
|
889
|
value = max_value;
|
900
|
890
|
}
|
901
|
|
-
|
|
891
|
+
|
902
|
892
|
if(!target->set_value(target, value))
|
903
|
893
|
{
|
904
|
894
|
LIGHT_ERR("failed to write to target");
|
905
|
895
|
return false;
|
906
|
896
|
}
|
907
|
|
-
|
|
897
|
+
|
908
|
898
|
return true;
|
909
|
899
|
}
|
910
|
900
|
|
|
@@ -916,14 +906,14 @@ bool light_cmd_sub_brightness(light_context_t *ctx)
|
916
|
906
|
LIGHT_ERR("didn't have a valid target, programmer mistake");
|
917
|
907
|
return false;
|
918
|
908
|
}
|
919
|
|
-
|
|
909
|
+
|
920
|
910
|
uint64_t value = 0;
|
921
|
911
|
if(!target->get_value(target, &value))
|
922
|
912
|
{
|
923
|
913
|
LIGHT_ERR("failed to read from target");
|
924
|
914
|
return false;
|
925
|
915
|
}
|
926
|
|
-
|
|
916
|
+
|
927
|
917
|
if(value > ctx->run_params.value)
|
928
|
918
|
{
|
929
|
919
|
value -= ctx->run_params.value;
|
|
@@ -932,7 +922,7 @@ bool light_cmd_sub_brightness(light_context_t *ctx)
|
932
|
922
|
{
|
933
|
923
|
value = 0;
|
934
|
924
|
}
|
935
|
|
-
|
|
925
|
+
|
936
|
926
|
uint64_t mincap = _light_get_min_cap(ctx);
|
937
|
927
|
if(mincap > value)
|
938
|
928
|
{
|
|
@@ -944,7 +934,7 @@ bool light_cmd_sub_brightness(light_context_t *ctx)
|
944
|
934
|
LIGHT_ERR("failed to write to target");
|
945
|
935
|
return false;
|
946
|
936
|
}
|
947
|
|
-
|
|
937
|
+
|
948
|
938
|
return true;
|
949
|
939
|
}
|
950
|
940
|
|
|
@@ -973,7 +963,7 @@ bool light_cmd_mul_brightness(light_context_t *ctx)
|
973
|
963
|
|
974
|
964
|
uint64_t old_value = value;
|
975
|
965
|
value *= ctx->run_params.float_value;
|
976
|
|
-
|
|
966
|
+
|
977
|
967
|
// Check that we actually de/increase value
|
978
|
968
|
if(value == old_value)
|
979
|
969
|
{
|
|
@@ -1007,7 +997,7 @@ bool light_cmd_save_brightness(light_context_t *ctx)
|
1007
|
997
|
{
|
1008
|
998
|
char target_path[NAME_MAX];
|
1009
|
999
|
_light_get_target_path(ctx, target_path, sizeof(target_path));
|
1010
|
|
-
|
|
1000
|
+
|
1011
|
1001
|
// Make sure the target folder exists, otherwise attempt to create it
|
1012
|
1002
|
int32_t rc = light_mkpath(target_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
|
1013
|
1003
|
if(rc && errno != EEXIST)
|
|
@@ -1015,7 +1005,7 @@ bool light_cmd_save_brightness(light_context_t *ctx)
|
1015
|
1005
|
LIGHT_ERR("couldn't create target directory for save brightness");
|
1016
|
1006
|
return false;
|
1017
|
1007
|
}
|
1018
|
|
-
|
|
1008
|
+
|
1019
|
1009
|
char target_filepath[NAME_MAX];
|
1020
|
1010
|
_light_get_target_file(ctx, target_filepath, sizeof(target_filepath), "save");
|
1021
|
1011
|
|
|
@@ -1025,13 +1015,13 @@ bool light_cmd_save_brightness(light_context_t *ctx)
|
1025
|
1015
|
LIGHT_ERR("couldn't read from device target");
|
1026
|
1016
|
return false;
|
1027
|
1017
|
}
|
1028
|
|
-
|
|
1018
|
+
|
1029
|
1019
|
if(!light_file_write_uint64(target_filepath, curr_value))
|
1030
|
1020
|
{
|
1031
|
1021
|
LIGHT_ERR("couldn't write value to savefile");
|
1032
|
1022
|
return false;
|
1033
|
1023
|
}
|
1034
|
|
-
|
|
1024
|
+
|
1035
|
1025
|
return true;
|
1036
|
1026
|
}
|
1037
|
1027
|
|
|
@@ -1046,19 +1036,19 @@ bool light_cmd_restore_brightness(light_context_t *ctx)
|
1046
|
1036
|
LIGHT_ERR("couldn't read value from savefile");
|
1047
|
1037
|
return false;
|
1048
|
1038
|
}
|
1049
|
|
-
|
|
1039
|
+
|
1050
|
1040
|
uint64_t mincap = _light_get_min_cap(ctx);
|
1051
|
1041
|
if(mincap > saved_value)
|
1052
|
1042
|
{
|
1053
|
1043
|
saved_value = mincap;
|
1054
|
1044
|
}
|
1055
|
|
-
|
|
1045
|
+
|
1056
|
1046
|
if(!ctx->run_params.device_target->set_value(ctx->run_params.device_target, saved_value))
|
1057
|
1047
|
{
|
1058
|
1048
|
LIGHT_ERR("couldn't write saved value to device target");
|
1059
|
1049
|
return false;
|
1060
|
1050
|
}
|
1061
|
|
-
|
|
1051
|
+
|
1062
|
1052
|
return true;
|
1063
|
1053
|
}
|
1064
|
1054
|
|
|
@@ -1069,11 +1059,11 @@ light_device_t *light_create_device(light_device_enumerator_t *enumerator, char
|
1069
|
1059
|
new_device->targets = NULL;
|
1070
|
1060
|
new_device->num_targets = 0;
|
1071
|
1061
|
new_device->device_data = device_data;
|
1072
|
|
-
|
|
1062
|
+
|
1073
|
1063
|
snprintf(new_device->name, sizeof(new_device->name), "%s", name);
|
1074
|
|
-
|
|
1064
|
+
|
1075
|
1065
|
_light_add_enumerator_device(enumerator, new_device);
|
1076
|
|
-
|
|
1066
|
+
|
1077
|
1067
|
return new_device;
|
1078
|
1068
|
}
|
1079
|
1069
|
|
|
@@ -1083,21 +1073,27 @@ void light_delete_device(light_device_t *device)
|
1083
|
1073
|
{
|
1084
|
1074
|
light_delete_device_target(device->targets[i]);
|
1085
|
1075
|
}
|
1086
|
|
-
|
|
1076
|
+
|
1087
|
1077
|
if(device->targets != NULL)
|
1088
|
1078
|
{
|
1089
|
1079
|
free(device->targets);
|
1090
|
1080
|
}
|
1091
|
|
-
|
|
1081
|
+
|
1092
|
1082
|
if(device->device_data != NULL)
|
1093
|
1083
|
{
|
1094
|
1084
|
free(device->device_data);
|
1095
|
1085
|
}
|
1096
|
|
-
|
|
1086
|
+
|
1097
|
1087
|
free(device);
|
1098
|
1088
|
}
|
1099
|
1089
|
|
1100
|
|
-light_device_target_t *light_create_device_target(light_device_t *device, char const *name, LFUNCVALSET setfunc, LFUNCVALGET getfunc, LFUNCMAXVALGET getmaxfunc, LFUNCCUSTOMCMD cmdfunc, void *target_data)
|
|
1090
|
+light_device_target_t *light_create_device_target(light_device_t *device,
|
|
1091
|
+ char const *name,
|
|
1092
|
+ LFUNCVALSET setfunc,
|
|
1093
|
+ LFUNCVALGET getfunc,
|
|
1094
|
+ LFUNCMAXVALGET getmaxfunc,
|
|
1095
|
+ LFUNCCUSTOMCMD cmdfunc,
|
|
1096
|
+ void *target_data)
|
1101
|
1097
|
{
|
1102
|
1098
|
light_device_target_t *new_target = malloc(sizeof(light_device_target_t));
|
1103
|
1099
|
new_target->device = device;
|
|
@@ -1106,11 +1102,11 @@ light_device_target_t *light_create_device_target(light_device_t *device, char c
|
1106
|
1102
|
new_target->get_max_value = getmaxfunc;
|
1107
|
1103
|
new_target->custom_command = cmdfunc;
|
1108
|
1104
|
new_target->device_target_data = target_data;
|
1109
|
|
-
|
|
1105
|
+
|
1110
|
1106
|
snprintf(new_target->name, sizeof(new_target->name), "%s", name);
|
1111
|
|
-
|
|
1107
|
+
|
1112
|
1108
|
_light_add_device_target(device, new_target);
|
1113
|
|
-
|
|
1109
|
+
|
1114
|
1110
|
return new_target;
|
1115
|
1111
|
}
|
1116
|
1112
|
|
|
@@ -1120,6 +1116,6 @@ void light_delete_device_target(light_device_target_t *device_target)
|
1120
|
1116
|
{
|
1121
|
1117
|
free(device_target->device_target_data);
|
1122
|
1118
|
}
|
1123
|
|
-
|
|
1119
|
+
|
1124
|
1120
|
free(device_target);
|
1125
|
1121
|
}
|