Browse Source

Remove trailing whitespaces

Removing the trailing whitespaces, that can be quite annoying for
people who use editors which show them. Additionally, it is a good
practice to avoid them.

Signed-off-by: Leonid Bloch <lb.workbox@gmail.com>
Leonid Bloch 5 years ago
parent
commit
a9b957917f
11 changed files with 197 additions and 197 deletions
  1. 21
    21
      DOCUMENTATION.md
  2. 2
    2
      README.md
  3. 2
    2
      src/helpers.c
  4. 2
    2
      src/helpers.h
  5. 13
    13
      src/impl/razer.c
  6. 2
    2
      src/impl/razer.h
  7. 30
    30
      src/impl/sysfs.c
  8. 2
    2
      src/impl/sysfs.h
  9. 1
    1
      src/impl/util.h
  10. 117
    117
      src/light.c
  11. 5
    5
      src/light.h

+ 21
- 21
DOCUMENTATION.md View File

10
 
10
 
11
 The following two sources should be clear enough examples of our coding style:
11
 The following two sources should be clear enough examples of our coding style:
12
 
12
 
13
-### Header files 
13
+### Header files
14
 
14
 
15
 ```c
15
 ```c
16
-    
16
+
17
     #pragma once
17
     #pragma once
18
-    
18
+
19
     #include <stdbool.h>
19
     #include <stdbool.h>
20
     #include <stdint.h>
20
     #include <stdint.h>
21
     #include <stdfoo.h> /* foo_type_t */
21
     #include <stdfoo.h> /* foo_type_t */
22
-    
23
-    
22
+
23
+
24
     typedef struct _some_struct_t some_struct_t;
24
     typedef struct _some_struct_t some_struct_t;
25
-    struct _some_struct_t 
25
+    struct _some_struct_t
26
     {
26
     {
27
         uint64_t    id;
27
         uint64_t    id;
28
         foo_type_t  my_foo_thing;
28
         foo_type_t  my_foo_thing;
29
         foo_type_t  *my_foo_ptr;
29
         foo_type_t  *my_foo_ptr;
30
     }
30
     }
31
-    
31
+
32
     /* Describe what the purpose of this function is, what it does with/to foo_struct, and what it returns. */
32
     /* Describe what the purpose of this function is, what it does with/to foo_struct, and what it returns. */
33
     bool do_some_stuff(some_struct_t *foo_struct);
33
     bool do_some_stuff(some_struct_t *foo_struct);
34
-    
34
+
35
 ```
35
 ```
36
 
36
 
37
 ### Source files
37
 ### Source files
52
 bool do_some_stuff(some_struct_t *foo_struct)
52
 bool do_some_stuff(some_struct_t *foo_struct)
53
 {
53
 {
54
     _increment_one(foo_struct->id);
54
     _increment_one(foo_struct->id);
55
-    
55
+
56
     if(foo_struct->id > 33)
56
     if(foo_struct->id > 33)
57
     {
57
     {
58
         return false;
58
         return false;
59
     }
59
     }
60
-    
60
+
61
     if(foo_struct->my_foo_ptr != NULL)
61
     if(foo_struct->my_foo_ptr != NULL)
62
     {
62
     {
63
         free(foo_struct->my_foo_ptr);
63
         free(foo_struct->my_foo_ptr);
64
     }
64
     }
65
-    
65
+
66
     foo_struct->my_foo_ptr = malloc(sizeof(foo_type_t));
66
     foo_struct->my_foo_ptr = malloc(sizeof(foo_type_t));
67
-    
67
+
68
     return true;
68
     return true;
69
 }
69
 }
70
 
70
 
71
 ```
71
 ```
72
 
72
 
73
-## Implementing an enumerator 
73
+## Implementing an enumerator
74
 
74
 
75
 Implementing your own devices through an enumerator is pretty easy. The required steps are as follows:
75
 Implementing your own devices through an enumerator is pretty easy. The required steps are as follows:
76
 
76
 
84
 
84
 
85
 ```c
85
 ```c
86
 
86
 
87
-#pragma once 
87
+#pragma once
88
 
88
 
89
 #include "light.h"
89
 #include "light.h"
90
 
90
 
91
 // Implementation of the foo enumerator
91
 // Implementation of the foo enumerator
92
 // Enumerates devices for quacking ducks
92
 // Enumerates devices for quacking ducks
93
 
93
 
94
-// Device target data 
94
+// Device target data
95
 struct _impl_foo_data_t
95
 struct _impl_foo_data_t
96
 {
96
 {
97
     int32_t internal_quack_id;
97
     int32_t internal_quack_id;
125
 bool impl_foo_init(light_device_enumerator_t *enumerator)
125
 bool impl_foo_init(light_device_enumerator_t *enumerator)
126
 {
126
 {
127
     /* Lets create a single device, with a single target, for simplicity */
127
     /* Lets create a single device, with a single target, for simplicity */
128
-    
128
+
129
     /* Create a new device called new_device_name, we dont need any userdata so pass NULL to the device_data parameter */
129
     /* Create a new device called new_device_name, we dont need any userdata so pass NULL to the device_data parameter */
130
     light_device_t *new_device = light_create_device(enumerator, "new_device_name", NULL)
130
     light_device_t *new_device = light_create_device(enumerator, "new_device_name", NULL)
131
-    
132
-    /* Setup userdata specific to the target we will create*/ 
131
+
132
+    /* Setup userdata specific to the target we will create*/
133
     /* Useful to for example reference an ID in a third-party API or likewise */
133
     /* Useful to for example reference an ID in a third-party API or likewise */
134
     /* NOTE: The userdata will be free()'d automatically on exit, so you do not need to free it yourself */
134
     /* NOTE: The userdata will be free()'d automatically on exit, so you do not need to free it yourself */
135
     impl_foo_data_t *custom_data = malloc(sizeof(impl_foo_data_t));
135
     impl_foo_data_t *custom_data = malloc(sizeof(impl_foo_data_t));
136
     custom_data->internal_quack_id = 333;
136
     custom_data->internal_quack_id = 333;
137
-    
138
-    
137
+
138
+
139
     /* Create a new device target called new_target_name, and pass in the functions and userdata that we just allocated */
139
     /* Create a new device target called new_target_name, and pass in the functions and userdata that we just allocated */
140
     light_create_device_target(new_device, "new_target_name", impl_foo_set, impl_foo_get, impl_foo_getmax, impl_foo_command, custom_data)
140
     light_create_device_target(new_device, "new_target_name", impl_foo_set, impl_foo_get, impl_foo_getmax, impl_foo_command, custom_data)
141
-    
141
+
142
     /* Return true because we didnt get any errors! */
142
     /* Return true because we didnt get any errors! */
143
     return true;
143
     return true;
144
 }
144
 }

+ 2
- 2
README.md View File

71
 -----
71
 -----
72
 
72
 
73
 Usage follows the following pattern, where options are optional and the neccesity of value depends on the options used
73
 Usage follows the following pattern, where options are optional and the neccesity of value depends on the options used
74
-    
74
+
75
     light [options] <value>
75
     light [options] <value>
76
 
76
 
77
 ### Command options
77
 ### Command options
96
 
96
 
97
 ### Extra options
97
 ### Extra options
98
 
98
 
99
-These can be mixed, combined and matched after convenience. 
99
+These can be mixed, combined and matched after convenience.
100
 
100
 
101
 * `-r` Raw mode, values (printed and interpreted from commandline) will be treated as integers in the controllers native range, instead of in percent.
101
 * `-r` Raw mode, values (printed and interpreted from commandline) will be treated as integers in the controllers native range, instead of in percent.
102
 * `-v <verbosity>` Specifies the verbosity level. 0 is default and prints nothing. 1 prints only errors, 2 prints only errors and warnings, and 3 prints both errors, warnings and notices.
102
 * `-v <verbosity>` Specifies the verbosity level. 0 is default and prints nothing. 1 prints only errors, 2 prints only errors and warnings, and 3 prints both errors, warnings and notices.

+ 2
- 2
src/helpers.c View File

7
 #include <sys/types.h>
7
 #include <sys/types.h>
8
 #include <dirent.h>
8
 #include <dirent.h>
9
 #include <errno.h> // errno
9
 #include <errno.h> // errno
10
-#include <libgen.h> // dirname 
10
+#include <libgen.h> // dirname
11
 
11
 
12
 
12
 
13
 bool light_file_read_uint64(char const *filename, uint64_t *val)
13
 bool light_file_read_uint64(char const *filename, uint64_t *val)
142
     char *tempdir = strdup(dir);
142
     char *tempdir = strdup(dir);
143
     light_mkpath(dirname(tempdir), mode);
143
     light_mkpath(dirname(tempdir), mode);
144
     free(tempdir);
144
     free(tempdir);
145
-    
145
+
146
     return mkdir(dir, mode);
146
     return mkdir(dir, mode);
147
 }
147
 }
148
 
148
 

+ 2
- 2
src/helpers.h View File

9
 /* Clamps x(value) between y(min) and z(max) in a nested ternary operation */
9
 /* Clamps x(value) between y(min) and z(max) in a nested ternary operation */
10
 #define LIGHT_CLAMP(val, min, max) (val < min ? light_log_clamp_min(min) : (val > max ? light_log_clamp_max(max) : val))
10
 #define LIGHT_CLAMP(val, min, max) (val < min ? light_log_clamp_min(min) : (val > max ? light_log_clamp_max(max) : val))
11
 
11
 
12
-/* Verbosity levels: 
12
+/* Verbosity levels:
13
 * 0 - No output
13
 * 0 - No output
14
 * 1 - Errors
14
 * 1 - Errors
15
-* 2 - Errors, warnings 
15
+* 2 - Errors, warnings
16
 * 3 - Errors, warnings, notices
16
 * 3 - Errors, warnings, notices
17
 */
17
 */
18
 typedef enum {
18
 typedef enum {

+ 13
- 13
src/impl/razer.c View File

12
     impl_razer_data_t *target_data = malloc(sizeof(impl_razer_data_t));
12
     impl_razer_data_t *target_data = malloc(sizeof(impl_razer_data_t));
13
     snprintf(target_data->brightness, sizeof(target_data->brightness), "/sys/bus/hid/drivers/razerkbd/%s/%s", device->name, filename);
13
     snprintf(target_data->brightness, sizeof(target_data->brightness), "/sys/bus/hid/drivers/razerkbd/%s/%s", device->name, filename);
14
     target_data->max_brightness = max_brightness;
14
     target_data->max_brightness = max_brightness;
15
-    
15
+
16
     // Only add targets that actually exist, as we aren't fully sure exactly what targets exist for a given device
16
     // Only add targets that actually exist, as we aren't fully sure exactly what targets exist for a given device
17
     if(light_file_exists(target_data->brightness))
17
     if(light_file_exists(target_data->brightness))
18
     {
18
     {
19
         light_create_device_target(device, name, impl_razer_set, impl_razer_get, impl_razer_getmax, impl_razer_command, target_data);
19
         light_create_device_target(device, name, impl_razer_set, impl_razer_get, impl_razer_getmax, impl_razer_command, target_data);
20
     }
20
     }
21
-    else 
21
+    else
22
     {
22
     {
23
         LIGHT_WARN("razer: couldn't add target %s to device %s, the file %s doesn't exist", name, device->name, filename);
23
         LIGHT_WARN("razer: couldn't add target %s to device %s, the file %s doesn't exist", name, device->name, filename);
24
         // target_data will not be freed automatically if we dont add a device target with it as userdata, so free it here
24
         // target_data will not be freed automatically if we dont add a device target with it as userdata, so free it here
33
 
33
 
34
     // Setup a target to backlight
34
     // Setup a target to backlight
35
     _impl_razer_add_target(new_device, "backlight", "matrix_brightness", 255);
35
     _impl_razer_add_target(new_device, "backlight", "matrix_brightness", 255);
36
-    
36
+
37
     // Setup targets to different possible leds
37
     // Setup targets to different possible leds
38
     _impl_razer_add_target(new_device, "game_led", "game_led_state", 1);
38
     _impl_razer_add_target(new_device, "game_led", "game_led_state", 1);
39
     _impl_razer_add_target(new_device, "macro_led", "macro_led_state", 1);
39
     _impl_razer_add_target(new_device, "macro_led", "macro_led_state", 1);
45
 
45
 
46
 bool impl_razer_init(light_device_enumerator_t *enumerator)
46
 bool impl_razer_init(light_device_enumerator_t *enumerator)
47
 {
47
 {
48
-    // Iterate through the led controllers and create a device_target for each controller 
48
+    // Iterate through the led controllers and create a device_target for each controller
49
     DIR *razer_dir;
49
     DIR *razer_dir;
50
     struct dirent *curr_entry;
50
     struct dirent *curr_entry;
51
-    
51
+
52
     if((razer_dir = opendir("/sys/bus/hid/drivers/razerkbd/")) == NULL)
52
     if((razer_dir = opendir("/sys/bus/hid/drivers/razerkbd/")) == NULL)
53
     {
53
     {
54
-        // Razer driver isnt properly installed, so we cant add devices in this enumerator 
54
+        // Razer driver isnt properly installed, so we cant add devices in this enumerator
55
         return true;
55
         return true;
56
     }
56
     }
57
-    
57
+
58
     while((curr_entry = readdir(razer_dir)) != NULL)
58
     while((curr_entry = readdir(razer_dir)) != NULL)
59
     {
59
     {
60
         // Skip dot entries
60
         // Skip dot entries
62
         {
62
         {
63
             continue;
63
             continue;
64
         }
64
         }
65
- 
65
+
66
         _impl_razer_add_device(enumerator, curr_entry->d_name);
66
         _impl_razer_add_device(enumerator, curr_entry->d_name);
67
     }
67
     }
68
-    
68
+
69
     closedir(razer_dir);
69
     closedir(razer_dir);
70
-    
70
+
71
     return true;
71
     return true;
72
 }
72
 }
73
 
73
 
85
         LIGHT_ERR("failed to write to razer device");
85
         LIGHT_ERR("failed to write to razer device");
86
         return false;
86
         return false;
87
     }
87
     }
88
-    
88
+
89
     return true;
89
     return true;
90
 }
90
 }
91
 
91
 
98
         LIGHT_ERR("failed to read from razer device");
98
         LIGHT_ERR("failed to read from razer device");
99
         return false;
99
         return false;
100
     }
100
     }
101
-    
101
+
102
     return true;
102
     return true;
103
 }
103
 }
104
 
104
 
107
     impl_razer_data_t *data = (impl_razer_data_t*)target->device_target_data;
107
     impl_razer_data_t *data = (impl_razer_data_t*)target->device_target_data;
108
 
108
 
109
     *out_value = data->max_brightness;
109
     *out_value = data->max_brightness;
110
-    
110
+
111
     return true;
111
     return true;
112
 }
112
 }
113
 
113
 

+ 2
- 2
src/impl/razer.h View File

1
 
1
 
2
-#pragma once 
2
+#pragma once
3
 
3
 
4
 #include "light.h"
4
 #include "light.h"
5
 
5
 
6
 // Implementation of the razer enumerator
6
 // Implementation of the razer enumerator
7
 // Enumerates devices for the openrazer driver https://github.com/openrazer/openrazer
7
 // Enumerates devices for the openrazer driver https://github.com/openrazer/openrazer
8
 
8
 
9
-// Device target data 
9
+// Device target data
10
 struct _impl_razer_data_t
10
 struct _impl_razer_data_t
11
 {
11
 {
12
     char brightness[NAME_MAX];
12
     char brightness[NAME_MAX];

+ 30
- 30
src/impl/sysfs.c View File

12
     // Create a new backlight device
12
     // Create a new backlight device
13
     light_device_t *leds_device = light_create_device(enumerator, "leds", NULL);
13
     light_device_t *leds_device = light_create_device(enumerator, "leds", NULL);
14
 
14
 
15
-    // Iterate through the led controllers and create a device_target for each controller 
15
+    // Iterate through the led controllers and create a device_target for each controller
16
     DIR *leds_dir;
16
     DIR *leds_dir;
17
     struct dirent *curr_entry;
17
     struct dirent *curr_entry;
18
-    
18
+
19
     if((leds_dir = opendir("/sys/class/leds")) == NULL)
19
     if((leds_dir = opendir("/sys/class/leds")) == NULL)
20
     {
20
     {
21
         LIGHT_ERR("failed to open leds controller directory for reading");
21
         LIGHT_ERR("failed to open leds controller directory for reading");
22
         return false;
22
         return false;
23
     }
23
     }
24
-    
24
+
25
     while((curr_entry = readdir(leds_dir)) != NULL)
25
     while((curr_entry = readdir(leds_dir)) != NULL)
26
     {
26
     {
27
         // Skip dot entries
27
         // Skip dot entries
29
         {
29
         {
30
             continue;
30
             continue;
31
         }
31
         }
32
-        
33
-        // Setup the target data 
32
+
33
+        // Setup the target data
34
         impl_sysfs_data_t *dev_data = malloc(sizeof(impl_sysfs_data_t));
34
         impl_sysfs_data_t *dev_data = malloc(sizeof(impl_sysfs_data_t));
35
         snprintf(dev_data->brightness, sizeof(dev_data->brightness), "/sys/class/leds/%s/brightness", curr_entry->d_name);
35
         snprintf(dev_data->brightness, sizeof(dev_data->brightness), "/sys/class/leds/%s/brightness", curr_entry->d_name);
36
         snprintf(dev_data->max_brightness, sizeof(dev_data->max_brightness), "/sys/class/leds/%s/max_brightness", curr_entry->d_name);
36
         snprintf(dev_data->max_brightness, sizeof(dev_data->max_brightness), "/sys/class/leds/%s/max_brightness", curr_entry->d_name);
37
-        
38
-        // Create a new device target for the controller 
37
+
38
+        // Create a new device target for the controller
39
         light_create_device_target(leds_device, curr_entry->d_name, impl_sysfs_set, impl_sysfs_get, impl_sysfs_getmax, impl_sysfs_command, dev_data);
39
         light_create_device_target(leds_device, curr_entry->d_name, impl_sysfs_set, impl_sysfs_get, impl_sysfs_getmax, impl_sysfs_command, dev_data);
40
     }
40
     }
41
-    
41
+
42
     closedir(leds_dir);
42
     closedir(leds_dir);
43
-    
43
+
44
     return true;
44
     return true;
45
 }
45
 }
46
 
46
 
49
     // Create a new backlight device
49
     // Create a new backlight device
50
     light_device_t *backlight_device = light_create_device(enumerator, "backlight", NULL);
50
     light_device_t *backlight_device = light_create_device(enumerator, "backlight", NULL);
51
 
51
 
52
-    // Iterate through the backlight controllers and create a device_target for each controller 
52
+    // Iterate through the backlight controllers and create a device_target for each controller
53
     DIR *backlight_dir;
53
     DIR *backlight_dir;
54
     struct dirent *curr_entry;
54
     struct dirent *curr_entry;
55
-    
55
+
56
     // Keep track of the best controller, and create an autodevice from that
56
     // Keep track of the best controller, and create an autodevice from that
57
     char best_controller[NAME_MAX];
57
     char best_controller[NAME_MAX];
58
     uint64_t best_value = 0;
58
     uint64_t best_value = 0;
59
-    
59
+
60
     if((backlight_dir = opendir("/sys/class/backlight")) == NULL)
60
     if((backlight_dir = opendir("/sys/class/backlight")) == NULL)
61
     {
61
     {
62
         LIGHT_ERR("failed to open backlight controller directory for reading");
62
         LIGHT_ERR("failed to open backlight controller directory for reading");
63
         return false;
63
         return false;
64
     }
64
     }
65
-    
65
+
66
     while((curr_entry = readdir(backlight_dir)) != NULL)
66
     while((curr_entry = readdir(backlight_dir)) != NULL)
67
     {
67
     {
68
         // Skip dot entries
68
         // Skip dot entries
70
         {
70
         {
71
             continue;
71
             continue;
72
         }
72
         }
73
-        
74
-        // Setup the target data 
73
+
74
+        // Setup the target data
75
         impl_sysfs_data_t *dev_data = malloc(sizeof(impl_sysfs_data_t));
75
         impl_sysfs_data_t *dev_data = malloc(sizeof(impl_sysfs_data_t));
76
         snprintf(dev_data->brightness, sizeof(dev_data->brightness), "/sys/class/backlight/%s/brightness", curr_entry->d_name);
76
         snprintf(dev_data->brightness, sizeof(dev_data->brightness), "/sys/class/backlight/%s/brightness", curr_entry->d_name);
77
         snprintf(dev_data->max_brightness, sizeof(dev_data->max_brightness), "/sys/class/backlight/%s/max_brightness", curr_entry->d_name);
77
         snprintf(dev_data->max_brightness, sizeof(dev_data->max_brightness), "/sys/class/backlight/%s/max_brightness", curr_entry->d_name);
78
-        
79
-        // Create a new device target for the controller 
78
+
79
+        // Create a new device target for the controller
80
         light_create_device_target(backlight_device, curr_entry->d_name, impl_sysfs_set, impl_sysfs_get, impl_sysfs_getmax, impl_sysfs_command, dev_data);
80
         light_create_device_target(backlight_device, curr_entry->d_name, impl_sysfs_set, impl_sysfs_get, impl_sysfs_getmax, impl_sysfs_command, dev_data);
81
-        
81
+
82
         // Read the max brightness to get the best one
82
         // Read the max brightness to get the best one
83
         uint64_t curr_value = 0;
83
         uint64_t curr_value = 0;
84
         if(light_file_read_uint64(dev_data->max_brightness, &curr_value))
84
         if(light_file_read_uint64(dev_data->max_brightness, &curr_value))
90
             }
90
             }
91
         }
91
         }
92
     }
92
     }
93
-    
93
+
94
     closedir(backlight_dir);
94
     closedir(backlight_dir);
95
-    
95
+
96
     // If we found at least one usable controller, create an auto target mapped to that controller
96
     // If we found at least one usable controller, create an auto target mapped to that controller
97
     if(best_value > 0)
97
     if(best_value > 0)
98
     {
98
     {
99
-        // Setup the target data 
99
+        // Setup the target data
100
         impl_sysfs_data_t *dev_data = malloc(sizeof(impl_sysfs_data_t));
100
         impl_sysfs_data_t *dev_data = malloc(sizeof(impl_sysfs_data_t));
101
         snprintf(dev_data->brightness, sizeof(dev_data->brightness), "/sys/class/backlight/%s/brightness", best_controller);
101
         snprintf(dev_data->brightness, sizeof(dev_data->brightness), "/sys/class/backlight/%s/brightness", best_controller);
102
         snprintf(dev_data->max_brightness, sizeof(dev_data->max_brightness), "/sys/class/backlight/%s/max_brightness", best_controller);
102
         snprintf(dev_data->max_brightness, sizeof(dev_data->max_brightness), "/sys/class/backlight/%s/max_brightness", best_controller);
103
-        
104
-        // Create a new device target for the controller 
103
+
104
+        // Create a new device target for the controller
105
         light_create_device_target(backlight_device, "auto", impl_sysfs_set, impl_sysfs_get, impl_sysfs_getmax, impl_sysfs_command, dev_data);
105
         light_create_device_target(backlight_device, "auto", impl_sysfs_set, impl_sysfs_get, impl_sysfs_getmax, impl_sysfs_command, dev_data);
106
     }
106
     }
107
-    
107
+
108
     return true;
108
     return true;
109
 }
109
 }
110
 
110
 
111
 bool impl_sysfs_init(light_device_enumerator_t *enumerator)
111
 bool impl_sysfs_init(light_device_enumerator_t *enumerator)
112
 {
112
 {
113
-    // Create a device for the backlight 
113
+    // Create a device for the backlight
114
     _impl_sysfs_init_backlight(enumerator);
114
     _impl_sysfs_init_backlight(enumerator);
115
-    
115
+
116
     // Create devices for the leds
116
     // Create devices for the leds
117
     _impl_sysfs_init_leds(enumerator);
117
     _impl_sysfs_init_leds(enumerator);
118
-    
118
+
119
     return true;
119
     return true;
120
 }
120
 }
121
 
121
 
133
         LIGHT_ERR("failed to write to sysfs device");
133
         LIGHT_ERR("failed to write to sysfs device");
134
         return false;
134
         return false;
135
     }
135
     }
136
-    
136
+
137
     return true;
137
     return true;
138
 }
138
 }
139
 
139
 
146
         LIGHT_ERR("failed to read from sysfs device");
146
         LIGHT_ERR("failed to read from sysfs device");
147
         return false;
147
         return false;
148
     }
148
     }
149
-    
149
+
150
     return true;
150
     return true;
151
 }
151
 }
152
 
152
 
159
         LIGHT_ERR("failed to read from sysfs device");
159
         LIGHT_ERR("failed to read from sysfs device");
160
         return false;
160
         return false;
161
     }
161
     }
162
-    
162
+
163
     return true;
163
     return true;
164
 }
164
 }
165
 
165
 

+ 2
- 2
src/impl/sysfs.h View File

1
 
1
 
2
-#pragma once 
2
+#pragma once
3
 
3
 
4
 #include "light.h"
4
 #include "light.h"
5
 
5
 
6
 // Implementation of the sysfs enumerator
6
 // Implementation of the sysfs enumerator
7
 // Enumerates devices for backlights and leds
7
 // Enumerates devices for backlights and leds
8
 
8
 
9
-// Device target data 
9
+// Device target data
10
 struct _impl_sysfs_data_t
10
 struct _impl_sysfs_data_t
11
 {
11
 {
12
     char brightness[NAME_MAX];
12
     char brightness[NAME_MAX];

+ 1
- 1
src/impl/util.h View File

1
 
1
 
2
-#pragma once 
2
+#pragma once
3
 
3
 
4
 #include "light.h"
4
 #include "light.h"
5
 
5
 

+ 117
- 117
src/light.c View File

20
 
20
 
21
 static void _light_add_enumerator_device(light_device_enumerator_t *enumerator, light_device_t *new_device)
21
 static void _light_add_enumerator_device(light_device_enumerator_t *enumerator, light_device_t *new_device)
22
 {
22
 {
23
-    // Create a new device array 
23
+    // Create a new device array
24
     uint64_t new_num_devices = enumerator->num_devices + 1;
24
     uint64_t new_num_devices = enumerator->num_devices + 1;
25
     light_device_t **new_devices = malloc(new_num_devices * sizeof(light_device_t*));
25
     light_device_t **new_devices = malloc(new_num_devices * sizeof(light_device_t*));
26
-    
26
+
27
     // Copy old device array to new one
27
     // Copy old device array to new one
28
     for(uint64_t i = 0; i < enumerator->num_devices; i++)
28
     for(uint64_t i = 0; i < enumerator->num_devices; i++)
29
     {
29
     {
30
         new_devices[i] = enumerator->devices[i];
30
         new_devices[i] = enumerator->devices[i];
31
     }
31
     }
32
-    
32
+
33
     // Set the new device
33
     // Set the new device
34
     new_devices[enumerator->num_devices] = new_device;
34
     new_devices[enumerator->num_devices] = new_device;
35
-    
35
+
36
     // Free the old devices array, if needed
36
     // Free the old devices array, if needed
37
     if(enumerator->devices != NULL)
37
     if(enumerator->devices != NULL)
38
     {
38
     {
39
         free(enumerator->devices);
39
         free(enumerator->devices);
40
     }
40
     }
41
-    
41
+
42
     // Replace the devices array with the new one
42
     // Replace the devices array with the new one
43
     enumerator->devices = new_devices;
43
     enumerator->devices = new_devices;
44
     enumerator->num_devices = new_num_devices;
44
     enumerator->num_devices = new_num_devices;
46
 
46
 
47
 static void _light_add_device_target(light_device_t *device, light_device_target_t *new_target)
47
 static void _light_add_device_target(light_device_t *device, light_device_target_t *new_target)
48
 {
48
 {
49
-    // Create a new targets array 
49
+    // Create a new targets array
50
     uint64_t new_num_targets = device->num_targets + 1;
50
     uint64_t new_num_targets = device->num_targets + 1;
51
     light_device_target_t **new_targets = malloc(new_num_targets * sizeof(light_device_target_t*));
51
     light_device_target_t **new_targets = malloc(new_num_targets * sizeof(light_device_target_t*));
52
-    
52
+
53
     // Copy old targets array to new one
53
     // Copy old targets array to new one
54
     for(uint64_t i = 0; i < device->num_targets; i++)
54
     for(uint64_t i = 0; i < device->num_targets; i++)
55
     {
55
     {
56
         new_targets[i] = device->targets[i];
56
         new_targets[i] = device->targets[i];
57
     }
57
     }
58
-    
58
+
59
     // Set the new target
59
     // Set the new target
60
     new_targets[device->num_targets] = new_target;
60
     new_targets[device->num_targets] = new_target;
61
-    
61
+
62
     // Free the old targets array, if needed
62
     // Free the old targets array, if needed
63
     if(device->targets != NULL)
63
     if(device->targets != NULL)
64
     {
64
     {
65
         free(device->targets);
65
         free(device->targets);
66
     }
66
     }
67
-    
67
+
68
     // Replace the targets array with the new one
68
     // Replace the targets array with the new one
69
     device->targets= new_targets;
69
     device->targets= new_targets;
70
     device->num_targets = new_num_targets;
70
     device->num_targets = new_num_targets;
103
     {
103
     {
104
         return 0;
104
         return 0;
105
     }
105
     }
106
-    
106
+
107
     return minimum_value;
107
     return minimum_value;
108
 }
108
 }
109
 
109
 
116
             return ctx->enumerators[e];
116
             return ctx->enumerators[e];
117
         }
117
         }
118
     }
118
     }
119
-    
119
+
120
     return NULL;
120
     return NULL;
121
 }
121
 }
122
 
122
 
129
             return en->devices[d];
129
             return en->devices[d];
130
         }
130
         }
131
     }
131
     }
132
-    
132
+
133
     return NULL;
133
     return NULL;
134
 }
134
 }
135
 
135
 
142
             return dev->targets[t];
142
             return dev->targets[t];
143
         }
143
         }
144
     }
144
     }
145
-    
145
+
146
     return NULL;
146
     return NULL;
147
 }
147
 }
148
 
148
 
158
         double max_value_d = (double)max_value;
158
         double max_value_d = (double)max_value;
159
         double percent = light_percent_clamp((inraw_d / max_value_d) * 100.0);
159
         double percent = light_percent_clamp((inraw_d / max_value_d) * 100.0);
160
         *outpercent = percent;
160
         *outpercent = percent;
161
-        
161
+
162
         return true;
162
         return true;
163
 }
163
 }
164
 
164
 
175
     double target_value_d = max_value_d * (light_percent_clamp(inpercent) / 100.0);
175
     double target_value_d = max_value_d * (light_percent_clamp(inpercent) / 100.0);
176
     uint64_t target_value = LIGHT_CLAMP((uint64_t)target_value_d, 0, max_value);
176
     uint64_t target_value = LIGHT_CLAMP((uint64_t)target_value_d, 0, max_value);
177
     *outraw = target_value;
177
     *outraw = target_value;
178
-    
178
+
179
     return true;
179
     return true;
180
 }
180
 }
181
 
181
 
188
         "  -H, -h      Show this help and exit\n"
188
         "  -H, -h      Show this help and exit\n"
189
         "  -V          Show program version and exit\n"
189
         "  -V          Show program version and exit\n"
190
         "  -L          List available devices\n"
190
         "  -L          List available devices\n"
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
         "  -T          Multiply brightness by value (can be a non-whole number, ignores raw mode)\n"
195
         "  -S          Set brightness to value\n"
195
         "  -S          Set brightness to value\n"
196
         "  -G          Get brightness\n"
196
         "  -G          Get brightness\n"
224
         LIGHT_WARN("a command was already set. ignoring.");
224
         LIGHT_WARN("a command was already set. ignoring.");
225
         return false;
225
         return false;
226
     }
226
     }
227
-    
227
+
228
     ctx->run_params.command = new_cmd;
228
     ctx->run_params.command = new_cmd;
229
     return true;
229
     return true;
230
 }
230
 }
233
 {
233
 {
234
     int32_t curr_arg = -1;
234
     int32_t curr_arg = -1;
235
     int32_t log_level = 0;
235
     int32_t log_level = 0;
236
-    
236
+
237
     char ctrl_name[NAME_MAX];
237
     char ctrl_name[NAME_MAX];
238
     bool need_value = false;
238
     bool need_value = false;
239
     bool need_float_value = false;
239
     bool need_float_value = false;
240
     bool need_target = true; // default cmd is get brightness
240
     bool need_target = true; // default cmd is get brightness
241
     bool specified_target = false;
241
     bool specified_target = false;
242
     snprintf(ctrl_name, sizeof(ctrl_name), "%s", "sysfs/backlight/auto");
242
     snprintf(ctrl_name, sizeof(ctrl_name), "%s", "sysfs/backlight/auto");
243
-    
243
+
244
     while((curr_arg = getopt(argc, argv, "HhVGSLMNPAUTOIv:s:r")) != -1)
244
     while((curr_arg = getopt(argc, argv, "HhVGSLMNPAUTOIv:s:r")) != -1)
245
     {
245
     {
246
         switch(curr_arg)
246
         switch(curr_arg)
247
         {
247
         {
248
             // Options
248
             // Options
249
-            
249
+
250
             case 'v':
250
             case 'v':
251
                 if (sscanf(optarg, "%i", &log_level) != 1)
251
                 if (sscanf(optarg, "%i", &log_level) != 1)
252
                 {
252
                 {
254
                     _light_print_usage();
254
                     _light_print_usage();
255
                     return false;
255
                     return false;
256
                 }
256
                 }
257
-                
257
+
258
                 if (log_level < 0 || log_level > 3)
258
                 if (log_level < 0 || log_level > 3)
259
                 {
259
                 {
260
                     fprintf(stderr, "-v argument must be between 0 and 3.\n\n");
260
                     fprintf(stderr, "-v argument must be between 0 and 3.\n\n");
261
                     _light_print_usage();
261
                     _light_print_usage();
262
                     return false;
262
                     return false;
263
                 }
263
                 }
264
-                
264
+
265
                 light_loglevel = (light_loglevel_t)log_level;
265
                 light_loglevel = (light_loglevel_t)log_level;
266
                 break;
266
                 break;
267
             case 's':
267
             case 's':
271
             case 'r':
271
             case 'r':
272
                 ctx->run_params.raw_mode = true;
272
                 ctx->run_params.raw_mode = true;
273
                 break;
273
                 break;
274
-            
274
+
275
             // Commands
275
             // Commands
276
             case 'H':
276
             case 'H':
277
             case 'h':
277
             case 'h':
335
     {
335
     {
336
         _light_set_context_command(ctx, light_cmd_get_brightness);
336
         _light_set_context_command(ctx, light_cmd_get_brightness);
337
     }
337
     }
338
-    
338
+
339
     if(need_target)
339
     if(need_target)
340
     {
340
     {
341
         light_device_target_t *curr_target = light_find_device_target(ctx, ctrl_name);
341
         light_device_target_t *curr_target = light_find_device_target(ctx, ctrl_name);
346
                 fprintf(stderr, "We couldn't find the specified device target at the path \"%s\". Use -L to find one.\n\n", ctrl_name);
346
                 fprintf(stderr, "We couldn't find the specified device target at the path \"%s\". Use -L to find one.\n\n", ctrl_name);
347
                 return false;
347
                 return false;
348
             }
348
             }
349
-            else 
349
+            else
350
             {
350
             {
351
                 fprintf(stderr, "No backlight controller was found, so we could not decide an automatic target. The current command will have no effect. Please use -L to find a target and then specify it with -s.\n\n");
351
                 fprintf(stderr, "No backlight controller was found, so we could not decide an automatic target. The current command will have no effect. Please use -L to find a target and then specify it with -s.\n\n");
352
                 curr_target = light_find_device_target(ctx, "util/test/dryrun");
352
                 curr_target = light_find_device_target(ctx, "util/test/dryrun");
353
             }
353
             }
354
         }
354
         }
355
-        
355
+
356
         ctx->run_params.device_target = curr_target;
356
         ctx->run_params.device_target = curr_target;
357
     }
357
     }
358
 
358
 
386
                 _light_print_usage();
386
                 _light_print_usage();
387
                 return false;
387
                 return false;
388
             }
388
             }
389
-            
389
+
390
             percent_value = light_percent_clamp(percent_value);
390
             percent_value = light_percent_clamp(percent_value);
391
-            
391
+
392
             uint64_t raw_value = 0;
392
             uint64_t raw_value = 0;
393
             if(!_light_percent_to_raw(ctx->run_params.device_target, percent_value, &raw_value))
393
             if(!_light_percent_to_raw(ctx->run_params.device_target, percent_value, &raw_value))
394
             {
394
             {
395
                 LIGHT_ERR("failed to convert from percent to raw for device target");
395
                 LIGHT_ERR("failed to convert from percent to raw for device target");
396
                 return false;
396
                 return false;
397
             }
397
             }
398
-            
398
+
399
             ctx->run_params.value = raw_value;
399
             ctx->run_params.value = raw_value;
400
         }
400
         }
401
     }
401
     }
411
     }
411
     }
412
 
412
 
413
     return true;
413
     return true;
414
-    
414
+
415
 }
415
 }
416
 
416
 
417
 
417
 
440
     else
440
     else
441
     {
441
     {
442
         char *xdg_conf = getenv("XDG_CONFIG_HOME");
442
         char *xdg_conf = getenv("XDG_CONFIG_HOME");
443
-        
443
+
444
         if (xdg_conf != NULL)
444
         if (xdg_conf != NULL)
445
         {
445
         {
446
             snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s/light", xdg_conf);
446
             snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s/light", xdg_conf);
450
             snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s/.config/light", getenv("HOME"));
450
             snprintf(new_ctx->sys_params.conf_dir, sizeof(new_ctx->sys_params.conf_dir), "%s/.config/light", getenv("HOME"));
451
         }
451
         }
452
     }
452
     }
453
-    
453
+
454
     // Make sure the configuration folder exists, otherwise attempt to create it
454
     // Make sure the configuration folder exists, otherwise attempt to create it
455
     int32_t rc = light_mkpath(new_ctx->sys_params.conf_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
455
     int32_t rc = light_mkpath(new_ctx->sys_params.conf_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
456
     if (rc && errno != EEXIST)
456
     if (rc && errno != EEXIST)
458
         LIGHT_WARN("couldn't create configuration directory");
458
         LIGHT_WARN("couldn't create configuration directory");
459
         return false;
459
         return false;
460
     }
460
     }
461
-    
461
+
462
     // Create the built-in enumerators
462
     // Create the built-in enumerators
463
     light_create_enumerator(new_ctx, "sysfs", &impl_sysfs_init, &impl_sysfs_free);
463
     light_create_enumerator(new_ctx, "sysfs", &impl_sysfs_init, &impl_sysfs_free);
464
     light_create_enumerator(new_ctx, "util", &impl_util_init, &impl_util_free);
464
     light_create_enumerator(new_ctx, "util", &impl_util_init, &impl_util_free);
480
         LIGHT_ERR("failed to parse arguments");
480
         LIGHT_ERR("failed to parse arguments");
481
         return NULL;
481
         return NULL;
482
     }
482
     }
483
-    
483
+
484
     return new_ctx;
484
     return new_ctx;
485
 }
485
 }
486
 
486
 
491
         LIGHT_ERR("run parameters command was null, can't execute");
491
         LIGHT_ERR("run parameters command was null, can't execute");
492
         return false;
492
         return false;
493
     }
493
     }
494
-    
494
+
495
     return ctx->run_params.command(ctx);
495
     return ctx->run_params.command(ctx);
496
 }
496
 }
497
 
497
 
501
     {
501
     {
502
         LIGHT_WARN("failed to free all enumerators");
502
         LIGHT_WARN("failed to free all enumerators");
503
     }
503
     }
504
-    
504
+
505
     free(ctx);
505
     free(ctx);
506
 }
506
 }
507
 
507
 
508
 light_device_enumerator_t * light_create_enumerator(light_context_t *ctx, char const * name, LFUNCENUMINIT init_func, LFUNCENUMFREE free_func)
508
 light_device_enumerator_t * light_create_enumerator(light_context_t *ctx, char const * name, LFUNCENUMINIT init_func, LFUNCENUMFREE free_func)
509
 {
509
 {
510
-    // Create a new enumerator array 
510
+    // Create a new enumerator array
511
     uint64_t new_num_enumerators = ctx->num_enumerators + 1;
511
     uint64_t new_num_enumerators = ctx->num_enumerators + 1;
512
     light_device_enumerator_t **new_enumerators = malloc(new_num_enumerators * sizeof(light_device_enumerator_t*));
512
     light_device_enumerator_t **new_enumerators = malloc(new_num_enumerators * sizeof(light_device_enumerator_t*));
513
-    
513
+
514
     // Copy old enumerator array to new one
514
     // Copy old enumerator array to new one
515
     for(uint64_t i = 0; i < ctx->num_enumerators; i++)
515
     for(uint64_t i = 0; i < ctx->num_enumerators; i++)
516
     {
516
     {
517
         new_enumerators[i] = ctx->enumerators[i];
517
         new_enumerators[i] = ctx->enumerators[i];
518
     }
518
     }
519
-    
519
+
520
     // Allocate the new enumerator
520
     // Allocate the new enumerator
521
     new_enumerators[ctx->num_enumerators] = malloc(sizeof(light_device_enumerator_t));
521
     new_enumerators[ctx->num_enumerators] = malloc(sizeof(light_device_enumerator_t));
522
     light_device_enumerator_t *returner = new_enumerators[ctx->num_enumerators];
522
     light_device_enumerator_t *returner = new_enumerators[ctx->num_enumerators];
523
-    
523
+
524
     returner->devices = NULL;
524
     returner->devices = NULL;
525
     returner->num_devices = 0;
525
     returner->num_devices = 0;
526
     returner->init = init_func;
526
     returner->init = init_func;
527
     returner->free = free_func;
527
     returner->free = free_func;
528
     snprintf(returner->name, sizeof(returner->name), "%s", name);
528
     snprintf(returner->name, sizeof(returner->name), "%s", name);
529
-    
529
+
530
     // Free the old enumerator array, if needed
530
     // Free the old enumerator array, if needed
531
     if(ctx->enumerators != NULL)
531
     if(ctx->enumerators != NULL)
532
     {
532
     {
533
         free(ctx->enumerators);
533
         free(ctx->enumerators);
534
     }
534
     }
535
-    
535
+
536
     // Replace the enumerator array with the new one
536
     // Replace the enumerator array with the new one
537
     ctx->enumerators = new_enumerators;
537
     ctx->enumerators = new_enumerators;
538
     ctx->num_enumerators = new_num_enumerators;
538
     ctx->num_enumerators = new_num_enumerators;
539
-    
539
+
540
     // Return newly created device
540
     // Return newly created device
541
     return returner;
541
     return returner;
542
 }
542
 }
552
             success = false;
552
             success = false;
553
         }
553
         }
554
     }
554
     }
555
-    
555
+
556
     return success;
556
     return success;
557
 }
557
 }
558
 
558
 
562
     for(uint64_t i = 0; i < ctx->num_enumerators; i++)
562
     for(uint64_t i = 0; i < ctx->num_enumerators; i++)
563
     {
563
     {
564
         light_device_enumerator_t * curr_enumerator = ctx->enumerators[i];
564
         light_device_enumerator_t * curr_enumerator = ctx->enumerators[i];
565
-        
565
+
566
         if(!curr_enumerator->free(curr_enumerator))
566
         if(!curr_enumerator->free(curr_enumerator))
567
         {
567
         {
568
             success = false;
568
             success = false;
569
         }
569
         }
570
-        
570
+
571
         if(curr_enumerator->devices != NULL)
571
         if(curr_enumerator->devices != NULL)
572
         {
572
         {
573
             for(uint64_t d = 0; d < curr_enumerator->num_devices; d++)
573
             for(uint64_t d = 0; d < curr_enumerator->num_devices; d++)
574
             {
574
             {
575
                 light_delete_device(curr_enumerator->devices[d]);
575
                 light_delete_device(curr_enumerator->devices[d]);
576
             }
576
             }
577
-            
577
+
578
             free(curr_enumerator->devices);
578
             free(curr_enumerator->devices);
579
             curr_enumerator->devices = NULL;
579
             curr_enumerator->devices = NULL;
580
         }
580
         }
581
-        
581
+
582
         free(curr_enumerator);
582
         free(curr_enumerator);
583
     }
583
     }
584
-    
584
+
585
     free(ctx->enumerators);
585
     free(ctx->enumerators);
586
     ctx->enumerators = NULL;
586
     ctx->enumerators = NULL;
587
     ctx->num_enumerators = 0;
587
     ctx->num_enumerators = 0;
588
-    
588
+
589
     return success;
589
     return success;
590
 }
590
 }
591
 
591
 
598
         LIGHT_WARN("invalid path passed to split_target_path");
598
         LIGHT_WARN("invalid path passed to split_target_path");
599
         return false;
599
         return false;
600
     }
600
     }
601
-    
601
+
602
     size_t size = end - begin;
602
     size_t size = end - begin;
603
     strncpy(out_path->enumerator, begin, size);
603
     strncpy(out_path->enumerator, begin, size);
604
     out_path->enumerator[size] = '\0';
604
     out_path->enumerator[size] = '\0';
605
-    
605
+
606
     begin = end + 1;
606
     begin = end + 1;
607
     end = strstr(begin, "/");
607
     end = strstr(begin, "/");
608
     if(end == NULL)
608
     if(end == NULL)
610
         LIGHT_WARN("invalid path passed to split_target_path");
610
         LIGHT_WARN("invalid path passed to split_target_path");
611
         return false;
611
         return false;
612
     }
612
     }
613
-    
613
+
614
     size = end - begin;
614
     size = end - begin;
615
     strncpy(out_path->device, begin, size);
615
     strncpy(out_path->device, begin, size);
616
     out_path->device[size] = '\0';
616
     out_path->device[size] = '\0';
617
-    
617
+
618
     strcpy(out_path->target, end + 1);
618
     strcpy(out_path->target, end + 1);
619
-    
619
+
620
     return true;
620
     return true;
621
 }
621
 }
622
 
622
 
628
         LIGHT_WARN("light_find_device_target needs a path in the format of \"enumerator/device/target\", the following format is not recognized:  \"%s\"", name);
628
         LIGHT_WARN("light_find_device_target needs a path in the format of \"enumerator/device/target\", the following format is not recognized:  \"%s\"", name);
629
         return NULL;
629
         return NULL;
630
     }
630
     }
631
-    
631
+
632
     /*
632
     /*
633
     Uncomment to debug the split function
633
     Uncomment to debug the split function
634
     printf("enumerator: %s %u\ndevice: %s %u\ntarget: %s %u\n",
634
     printf("enumerator: %s %u\ndevice: %s %u\ntarget: %s %u\n",
636
             new_path.device, strlen(new_path.device),
636
             new_path.device, strlen(new_path.device),
637
             new_path.target, strlen(new_path.target));
637
             new_path.target, strlen(new_path.target));
638
     */
638
     */
639
-    
640
-    // find a matching enumerator 
641
-    
639
+
640
+    // find a matching enumerator
641
+
642
     light_device_enumerator_t *enumerator = _light_find_enumerator(ctx, new_path.enumerator);
642
     light_device_enumerator_t *enumerator = _light_find_enumerator(ctx, new_path.enumerator);
643
     if(enumerator == NULL)
643
     if(enumerator == NULL)
644
     {
644
     {
645
         LIGHT_WARN("no such enumerator, \"%s\"", new_path.enumerator);
645
         LIGHT_WARN("no such enumerator, \"%s\"", new_path.enumerator);
646
         return NULL;
646
         return NULL;
647
     }
647
     }
648
-    
648
+
649
     light_device_t *device = _light_find_device(enumerator, new_path.device);
649
     light_device_t *device = _light_find_device(enumerator, new_path.device);
650
     if(device == NULL)
650
     if(device == NULL)
651
     {
651
     {
652
         LIGHT_WARN("no such device, \"%s\"", new_path.device);
652
         LIGHT_WARN("no such device, \"%s\"", new_path.device);
653
         return NULL;
653
         return NULL;
654
     }
654
     }
655
-    
655
+
656
     light_device_target_t *target = _light_find_target(device, new_path.target);
656
     light_device_target_t *target = _light_find_target(device, new_path.target);
657
     if(target == NULL)
657
     if(target == NULL)
658
     {
658
     {
659
         LIGHT_WARN("no such target, \"%s\"", new_path.target);
659
         LIGHT_WARN("no such target, \"%s\"", new_path.target);
660
         return NULL;
660
         return NULL;
661
     }
661
     }
662
-    
662
+
663
     return target;
663
     return target;
664
 }
664
 }
665
 
665
 
687
             for(uint64_t target = 0; target < curr_device->num_targets; target++)
687
             for(uint64_t target = 0; target < curr_device->num_targets; target++)
688
             {
688
             {
689
                 light_device_target_t *curr_target = curr_device->targets[target];
689
                 light_device_target_t *curr_target = curr_device->targets[target];
690
-                
690
+
691
                 printf("\t%s/%s/%s\n", curr_enumerator->name, curr_device->name, curr_target->name);
691
                 printf("\t%s/%s/%s\n", curr_enumerator->name, curr_device->name, curr_target->name);
692
             }
692
             }
693
         }
693
         }
694
     }
694
     }
695
-    
695
+
696
     return true;
696
     return true;
697
 }
697
 }
698
 
698
 
704
         LIGHT_ERR("didn't have a valid target, programmer mistake");
704
         LIGHT_ERR("didn't have a valid target, programmer mistake");
705
         return false;
705
         return false;
706
     }
706
     }
707
-    
708
-    
707
+
708
+
709
     uint64_t mincap = _light_get_min_cap(ctx);
709
     uint64_t mincap = _light_get_min_cap(ctx);
710
     uint64_t value = ctx->run_params.value;
710
     uint64_t value = ctx->run_params.value;
711
     if(mincap > value)
711
     if(mincap > value)
712
     {
712
     {
713
         value = mincap;
713
         value = mincap;
714
     }
714
     }
715
-    
715
+
716
     if(!target->set_value(target, value))
716
     if(!target->set_value(target, value))
717
     {
717
     {
718
         LIGHT_ERR("failed to write to target");
718
         LIGHT_ERR("failed to write to target");
719
         return false;
719
         return false;
720
     }
720
     }
721
-    
721
+
722
     return true;
722
     return true;
723
 }
723
 }
724
 
724
 
730
         LIGHT_ERR("didn't have a valid target, programmer mistake");
730
         LIGHT_ERR("didn't have a valid target, programmer mistake");
731
         return false;
731
         return false;
732
     }
732
     }
733
-    
733
+
734
     uint64_t value = 0;
734
     uint64_t value = 0;
735
     if(!target->get_value(target, &value))
735
     if(!target->get_value(target, &value))
736
     {
736
     {
737
         LIGHT_ERR("failed to read from target");
737
         LIGHT_ERR("failed to read from target");
738
         return false;
738
         return false;
739
     }
739
     }
740
-    
740
+
741
     if(ctx->run_params.raw_mode)
741
     if(ctx->run_params.raw_mode)
742
     {
742
     {
743
         printf("%" PRIu64 "\n", value);
743
         printf("%" PRIu64 "\n", value);
744
     }
744
     }
745
-    else 
745
+    else
746
     {
746
     {
747
         double percent = 0.0;
747
         double percent = 0.0;
748
         if(!_light_raw_to_percent(target, value, &percent))
748
         if(!_light_raw_to_percent(target, value, &percent))
752
         }
752
         }
753
         printf("%.2f\n", percent);
753
         printf("%.2f\n", percent);
754
     }
754
     }
755
-    
755
+
756
     return true;
756
     return true;
757
 }
757
 }
758
 
758
 
764
         LIGHT_ERR("didn't have a valid target, programmer mistake");
764
         LIGHT_ERR("didn't have a valid target, programmer mistake");
765
         return false;
765
         return false;
766
     }
766
     }
767
-    
767
+
768
     if(!ctx->run_params.raw_mode)
768
     if(!ctx->run_params.raw_mode)
769
     {
769
     {
770
         printf("100.0\n");
770
         printf("100.0\n");
771
         return true;
771
         return true;
772
     }
772
     }
773
-    
773
+
774
     uint64_t max_value = 0;
774
     uint64_t max_value = 0;
775
     if(!target->get_max_value(target, &max_value))
775
     if(!target->get_max_value(target, &max_value))
776
     {
776
     {
777
         LIGHT_ERR("failed to read from device target");
777
         LIGHT_ERR("failed to read from device target");
778
         return false;
778
         return false;
779
     }
779
     }
780
-    
780
+
781
     printf("%" PRIu64 "\n", max_value);
781
     printf("%" PRIu64 "\n", max_value);
782
     return true;
782
     return true;
783
 }
783
 }
786
 {
786
 {
787
     char target_path[NAME_MAX];
787
     char target_path[NAME_MAX];
788
     _light_get_target_path(ctx, target_path, sizeof(target_path));
788
     _light_get_target_path(ctx, target_path, sizeof(target_path));
789
-    
789
+
790
     // Make sure the target folder exists, otherwise attempt to create it
790
     // Make sure the target folder exists, otherwise attempt to create it
791
     int32_t rc = light_mkpath(target_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
791
     int32_t rc = light_mkpath(target_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
792
     if (rc && errno != EEXIST)
792
     if (rc && errno != EEXIST)
794
         LIGHT_ERR("couldn't create target directory for minimum brightness");
794
         LIGHT_ERR("couldn't create target directory for minimum brightness");
795
         return false;
795
         return false;
796
     }
796
     }
797
-    
797
+
798
     char target_filepath[NAME_MAX];
798
     char target_filepath[NAME_MAX];
799
     _light_get_target_file(ctx, target_filepath, sizeof(target_filepath), "minimum");
799
     _light_get_target_file(ctx, target_filepath, sizeof(target_filepath), "minimum");
800
-    
800
+
801
     if(!light_file_write_uint64(target_filepath, ctx->run_params.value))
801
     if(!light_file_write_uint64(target_filepath, ctx->run_params.value))
802
     {
802
     {
803
         LIGHT_ERR("couldn't write value to minimum file");
803
         LIGHT_ERR("couldn't write value to minimum file");
804
         return false;
804
         return false;
805
     }
805
     }
806
-    
806
+
807
     return true;
807
     return true;
808
 }
808
 }
809
 
809
 
819
         {
819
         {
820
             printf("0\n");
820
             printf("0\n");
821
         }
821
         }
822
-        else 
822
+        else
823
         {
823
         {
824
             printf("0.00\n");
824
             printf("0.00\n");
825
         }
825
         }
826
 
826
 
827
         return true;
827
         return true;
828
     }
828
     }
829
-    
829
+
830
     if(ctx->run_params.raw_mode)
830
     if(ctx->run_params.raw_mode)
831
     {
831
     {
832
         printf("%" PRIu64 "\n", minimum_value);
832
         printf("%" PRIu64 "\n", minimum_value);
833
     }
833
     }
834
-    else 
834
+    else
835
     {
835
     {
836
         double minimum_d = 0.0;
836
         double minimum_d = 0.0;
837
         if(!_light_raw_to_percent(ctx->run_params.device_target, minimum_value, &minimum_d))
837
         if(!_light_raw_to_percent(ctx->run_params.device_target, minimum_value, &minimum_d))
839
             LIGHT_ERR("failed to convert value from raw to percent for device target");
839
             LIGHT_ERR("failed to convert value from raw to percent for device target");
840
             return false;
840
             return false;
841
         }
841
         }
842
-        
842
+
843
         printf("%.2f\n", minimum_d);
843
         printf("%.2f\n", minimum_d);
844
     }
844
     }
845
-    
845
+
846
     return true;
846
     return true;
847
 }
847
 }
848
 
848
 
854
         LIGHT_ERR("didn't have a valid target, programmer mistake");
854
         LIGHT_ERR("didn't have a valid target, programmer mistake");
855
         return false;
855
         return false;
856
     }
856
     }
857
-    
857
+
858
     uint64_t value = 0;
858
     uint64_t value = 0;
859
     if(!target->get_value(target, &value))
859
     if(!target->get_value(target, &value))
860
     {
860
     {
861
         LIGHT_ERR("failed to read from target");
861
         LIGHT_ERR("failed to read from target");
862
         return false;
862
         return false;
863
     }
863
     }
864
-    
864
+
865
     uint64_t max_value = 0;
865
     uint64_t max_value = 0;
866
     if(!target->get_max_value(target, &max_value))
866
     if(!target->get_max_value(target, &max_value))
867
     {
867
     {
868
         LIGHT_ERR("failed to read from target");
868
         LIGHT_ERR("failed to read from target");
869
         return false;
869
         return false;
870
     }
870
     }
871
-    
871
+
872
     value += ctx->run_params.value;
872
     value += ctx->run_params.value;
873
-    
873
+
874
     uint64_t mincap = _light_get_min_cap(ctx);
874
     uint64_t mincap = _light_get_min_cap(ctx);
875
     if(mincap > value)
875
     if(mincap > value)
876
     {
876
     {
877
         value = mincap;
877
         value = mincap;
878
     }
878
     }
879
-    
880
-    
879
+
880
+
881
     if(value > max_value)
881
     if(value > max_value)
882
     {
882
     {
883
         value = max_value;
883
         value = max_value;
884
     }
884
     }
885
-    
885
+
886
     if(!target->set_value(target, value))
886
     if(!target->set_value(target, value))
887
     {
887
     {
888
         LIGHT_ERR("failed to write to target");
888
         LIGHT_ERR("failed to write to target");
889
         return false;
889
         return false;
890
     }
890
     }
891
-    
891
+
892
     return true;
892
     return true;
893
 }
893
 }
894
 
894
 
900
         LIGHT_ERR("didn't have a valid target, programmer mistake");
900
         LIGHT_ERR("didn't have a valid target, programmer mistake");
901
         return false;
901
         return false;
902
     }
902
     }
903
-    
903
+
904
     uint64_t value = 0;
904
     uint64_t value = 0;
905
     if(!target->get_value(target, &value))
905
     if(!target->get_value(target, &value))
906
     {
906
     {
907
         LIGHT_ERR("failed to read from target");
907
         LIGHT_ERR("failed to read from target");
908
         return false;
908
         return false;
909
     }
909
     }
910
-    
910
+
911
     if(value > ctx->run_params.value)
911
     if(value > ctx->run_params.value)
912
     {
912
     {
913
         value -= ctx->run_params.value;
913
         value -= ctx->run_params.value;
914
     }
914
     }
915
-    else 
915
+    else
916
     {
916
     {
917
         value = 0;
917
         value = 0;
918
     }
918
     }
919
-    
919
+
920
     uint64_t mincap = _light_get_min_cap(ctx);
920
     uint64_t mincap = _light_get_min_cap(ctx);
921
     if(mincap > value)
921
     if(mincap > value)
922
     {
922
     {
928
         LIGHT_ERR("failed to write to target");
928
         LIGHT_ERR("failed to write to target");
929
         return false;
929
         return false;
930
     }
930
     }
931
-    
931
+
932
     return true;
932
     return true;
933
 }
933
 }
934
 
934
 
957
 
957
 
958
     uint64_t old_value = value;
958
     uint64_t old_value = value;
959
     value *= ctx->run_params.float_value;
959
     value *= ctx->run_params.float_value;
960
- 
960
+
961
     // Check that we actually de/increase value
961
     // Check that we actually de/increase value
962
     if(value == old_value)
962
     if(value == old_value)
963
     {
963
     {
991
 {
991
 {
992
     char target_path[NAME_MAX];
992
     char target_path[NAME_MAX];
993
     _light_get_target_path(ctx, target_path, sizeof(target_path));
993
     _light_get_target_path(ctx, target_path, sizeof(target_path));
994
-    
994
+
995
     // Make sure the target folder exists, otherwise attempt to create it
995
     // Make sure the target folder exists, otherwise attempt to create it
996
     int32_t rc = light_mkpath(target_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
996
     int32_t rc = light_mkpath(target_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
997
     if (rc && errno != EEXIST)
997
     if (rc && errno != EEXIST)
999
         LIGHT_ERR("couldn't create target directory for save brightness");
999
         LIGHT_ERR("couldn't create target directory for save brightness");
1000
         return false;
1000
         return false;
1001
     }
1001
     }
1002
-    
1002
+
1003
     char target_filepath[NAME_MAX];
1003
     char target_filepath[NAME_MAX];
1004
     _light_get_target_file(ctx, target_filepath, sizeof(target_filepath), "save");
1004
     _light_get_target_file(ctx, target_filepath, sizeof(target_filepath), "save");
1005
 
1005
 
1009
         LIGHT_ERR("couldn't read from device target");
1009
         LIGHT_ERR("couldn't read from device target");
1010
         return false;
1010
         return false;
1011
     }
1011
     }
1012
-    
1012
+
1013
     if(!light_file_write_uint64(target_filepath, curr_value))
1013
     if(!light_file_write_uint64(target_filepath, curr_value))
1014
     {
1014
     {
1015
         LIGHT_ERR("couldn't write value to savefile");
1015
         LIGHT_ERR("couldn't write value to savefile");
1016
         return false;
1016
         return false;
1017
     }
1017
     }
1018
-    
1018
+
1019
     return true;
1019
     return true;
1020
 }
1020
 }
1021
 
1021
 
1030
         LIGHT_ERR("couldn't read value from savefile");
1030
         LIGHT_ERR("couldn't read value from savefile");
1031
         return false;
1031
         return false;
1032
     }
1032
     }
1033
-    
1033
+
1034
     uint64_t mincap = _light_get_min_cap(ctx);
1034
     uint64_t mincap = _light_get_min_cap(ctx);
1035
     if(mincap > saved_value)
1035
     if(mincap > saved_value)
1036
     {
1036
     {
1037
         saved_value = mincap;
1037
         saved_value = mincap;
1038
     }
1038
     }
1039
-    
1039
+
1040
     if(!ctx->run_params.device_target->set_value(ctx->run_params.device_target, saved_value))
1040
     if(!ctx->run_params.device_target->set_value(ctx->run_params.device_target, saved_value))
1041
     {
1041
     {
1042
         LIGHT_ERR("couldn't write saved value to device target");
1042
         LIGHT_ERR("couldn't write saved value to device target");
1043
         return false;
1043
         return false;
1044
     }
1044
     }
1045
-    
1045
+
1046
     return true;
1046
     return true;
1047
 }
1047
 }
1048
 
1048
 
1053
     new_device->targets = NULL;
1053
     new_device->targets = NULL;
1054
     new_device->num_targets = 0;
1054
     new_device->num_targets = 0;
1055
     new_device->device_data = device_data;
1055
     new_device->device_data = device_data;
1056
-    
1056
+
1057
     snprintf(new_device->name, sizeof(new_device->name), "%s", name);
1057
     snprintf(new_device->name, sizeof(new_device->name), "%s", name);
1058
-    
1058
+
1059
     _light_add_enumerator_device(enumerator, new_device);
1059
     _light_add_enumerator_device(enumerator, new_device);
1060
-    
1060
+
1061
     return new_device;
1061
     return new_device;
1062
 }
1062
 }
1063
 
1063
 
1067
     {
1067
     {
1068
         light_delete_device_target(device->targets[i]);
1068
         light_delete_device_target(device->targets[i]);
1069
     }
1069
     }
1070
-    
1070
+
1071
     if(device->targets != NULL)
1071
     if(device->targets != NULL)
1072
     {
1072
     {
1073
         free(device->targets);
1073
         free(device->targets);
1074
     }
1074
     }
1075
-    
1075
+
1076
     if(device->device_data != NULL)
1076
     if(device->device_data != NULL)
1077
     {
1077
     {
1078
         free(device->device_data);
1078
         free(device->device_data);
1079
     }
1079
     }
1080
-    
1080
+
1081
     free(device);
1081
     free(device);
1082
 }
1082
 }
1083
 
1083
 
1090
     new_target->get_max_value = getmaxfunc;
1090
     new_target->get_max_value = getmaxfunc;
1091
     new_target->custom_command = cmdfunc;
1091
     new_target->custom_command = cmdfunc;
1092
     new_target->device_target_data = target_data;
1092
     new_target->device_target_data = target_data;
1093
-    
1093
+
1094
     snprintf(new_target->name, sizeof(new_target->name), "%s", name);
1094
     snprintf(new_target->name, sizeof(new_target->name), "%s", name);
1095
-    
1095
+
1096
     _light_add_device_target(device, new_target);
1096
     _light_add_device_target(device, new_target);
1097
-    
1097
+
1098
     return new_target;
1098
     return new_target;
1099
 }
1099
 }
1100
 
1100
 
1104
     {
1104
     {
1105
         free(device_target->device_target_data);
1105
         free(device_target->device_target_data);
1106
     }
1106
     }
1107
-    
1107
+
1108
     free(device_target);
1108
     free(device_target);
1109
 }
1109
 }

+ 5
- 5
src/light.h View File

70
 
70
 
71
 struct _light_context_t
71
 struct _light_context_t
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
         // Only one of value and raw_value is populated; which one depends on the command
82
 
82
 
83
     struct
83
     struct
84
     {
84
     {
85
-        char                    conf_dir[NAME_MAX]; // The path to the application cache directory 
85
+        char                    conf_dir[NAME_MAX]; // The path to the application cache directory
86
     } sys_params;
86
     } sys_params;
87
-    
87
+
88
     light_device_enumerator_t   **enumerators;
88
     light_device_enumerator_t   **enumerators;
89
     uint64_t                    num_enumerators;
89
     uint64_t                    num_enumerators;
90
 };
90
 };
91
 
91
 
92
 // The different available commands
92
 // The different available commands
93
-bool light_cmd_print_help(light_context_t *ctx); // H,h 
93
+bool light_cmd_print_help(light_context_t *ctx); // H,h
94
 bool light_cmd_print_version(light_context_t *ctx); // V
94
 bool light_cmd_print_version(light_context_t *ctx); // V
95
 bool light_cmd_list_devices(light_context_t *ctx); // L
95
 bool light_cmd_list_devices(light_context_t *ctx); // L
96
 bool light_cmd_set_brightness(light_context_t *ctx); // S
96
 bool light_cmd_set_brightness(light_context_t *ctx); // S
135
 void light_delete_device_target(light_device_target_t *device_target);
135
 void light_delete_device_target(light_device_target_t *device_target);
136
 
136
 
137
 typedef struct _light_target_path_t light_target_path_t;
137
 typedef struct _light_target_path_t light_target_path_t;
138
-struct _light_target_path_t 
138
+struct _light_target_path_t
139
 {
139
 {
140
     char enumerator[NAME_MAX];
140
     char enumerator[NAME_MAX];
141
     char device[NAME_MAX];
141
     char device[NAME_MAX];