|
@@ -9,247 +9,251 @@
|
9
|
9
|
|
10
|
10
|
static bool _impl_sysfs_init_leds(light_device_enumerator_t *enumerator)
|
11
|
11
|
{
|
12
|
|
- // Create a new backlight device
|
13
|
|
- light_device_t *leds_device = malloc(sizeof(light_device_t));
|
14
|
|
- snprintf(leds_device->name, sizeof(leds_device->name), "%s", "leds");
|
15
|
|
-
|
16
|
|
- // Add it to the enumerator
|
17
|
|
- light_add_enumerator_device(enumerator, leds_device);
|
18
|
|
-
|
19
|
|
- // Iterate through the led controllers and create a device_target for each controller
|
20
|
|
- DIR *leds_dir;
|
21
|
|
- struct dirent *curr_entry;
|
22
|
|
-
|
23
|
|
- if((leds_dir = opendir("/sys/class/leds")) == NULL)
|
24
|
|
- {
|
25
|
|
- LIGHT_ERR("failed to open leds controller directory for reading");
|
26
|
|
- return false;
|
27
|
|
- }
|
28
|
|
-
|
29
|
|
- while((curr_entry = readdir(leds_dir)) != NULL)
|
30
|
|
- {
|
31
|
|
- // Skip dot entries
|
32
|
|
- if(curr_entry->d_name[0] == '.')
|
33
|
|
- {
|
34
|
|
- continue;
|
35
|
|
- }
|
36
|
|
-
|
37
|
|
- // Create a new device target for the controller
|
38
|
|
- light_device_target_t *led_controller = malloc(sizeof(light_device_target_t));
|
39
|
|
- snprintf(led_controller->name, sizeof(led_controller->name), "%s", curr_entry->d_name);
|
40
|
|
-
|
41
|
|
- // Setup the function bindings
|
42
|
|
- led_controller->set_value = impl_sysfs_set;
|
43
|
|
- led_controller->get_value = impl_sysfs_get;
|
44
|
|
- led_controller->get_max_value = impl_sysfs_getmax;
|
45
|
|
- led_controller->custom_command = impl_sysfs_command;
|
46
|
|
-
|
47
|
|
- // Setup the target data
|
48
|
|
- impl_sysfs_data_t *dev_data = malloc(sizeof(impl_sysfs_data_t));
|
49
|
|
- led_controller->device_target_data = dev_data;
|
50
|
|
- snprintf(dev_data->brightness, sizeof(dev_data->brightness), "/sys/class/leds/%s/brightness", curr_entry->d_name);
|
51
|
|
- snprintf(dev_data->max_brightness, sizeof(dev_data->max_brightness), "/sys/class/leds/%s/max_brightness", curr_entry->d_name);
|
52
|
|
-
|
53
|
|
- // Add it to the device
|
54
|
|
- light_add_device_target(leds_device, led_controller);
|
55
|
|
- }
|
56
|
|
-
|
57
|
|
- return true;
|
|
12
|
+ // Create a new backlight device
|
|
13
|
+ light_device_t *leds_device = malloc(sizeof(light_device_t));
|
|
14
|
+ snprintf(leds_device->name, sizeof(leds_device->name), "%s", "leds");
|
|
15
|
+
|
|
16
|
+ // Add it to the enumerator
|
|
17
|
+ light_add_enumerator_device(enumerator, leds_device);
|
|
18
|
+
|
|
19
|
+ // Iterate through the led controllers and create a device_target for each controller
|
|
20
|
+ DIR *leds_dir;
|
|
21
|
+ struct dirent *curr_entry;
|
|
22
|
+
|
|
23
|
+ if((leds_dir = opendir("/sys/class/leds")) == NULL)
|
|
24
|
+ {
|
|
25
|
+ LIGHT_ERR("failed to open leds controller directory for reading");
|
|
26
|
+ return false;
|
|
27
|
+ }
|
|
28
|
+
|
|
29
|
+ while((curr_entry = readdir(leds_dir)) != NULL)
|
|
30
|
+ {
|
|
31
|
+ // Skip dot entries
|
|
32
|
+ if(curr_entry->d_name[0] == '.')
|
|
33
|
+ {
|
|
34
|
+ continue;
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ // Create a new device target for the controller
|
|
38
|
+ light_device_target_t *led_controller = malloc(sizeof(light_device_target_t));
|
|
39
|
+ snprintf(led_controller->name, sizeof(led_controller->name), "%s", curr_entry->d_name);
|
|
40
|
+
|
|
41
|
+ // Setup the function bindings
|
|
42
|
+ led_controller->set_value = impl_sysfs_set;
|
|
43
|
+ led_controller->get_value = impl_sysfs_get;
|
|
44
|
+ led_controller->get_max_value = impl_sysfs_getmax;
|
|
45
|
+ led_controller->custom_command = impl_sysfs_command;
|
|
46
|
+
|
|
47
|
+ // Setup the target data
|
|
48
|
+ impl_sysfs_data_t *dev_data = malloc(sizeof(impl_sysfs_data_t));
|
|
49
|
+ led_controller->device_target_data = dev_data;
|
|
50
|
+ snprintf(dev_data->brightness, sizeof(dev_data->brightness), "/sys/class/leds/%s/brightness", curr_entry->d_name);
|
|
51
|
+ snprintf(dev_data->max_brightness, sizeof(dev_data->max_brightness), "/sys/class/leds/%s/max_brightness", curr_entry->d_name);
|
|
52
|
+
|
|
53
|
+ // Add it to the device
|
|
54
|
+ light_add_device_target(leds_device, led_controller);
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+ closedir(leds_dir);
|
|
58
|
+
|
|
59
|
+ return true;
|
58
|
60
|
}
|
59
|
61
|
|
60
|
62
|
static bool _impl_sysfs_init_backlight(light_device_enumerator_t *enumerator)
|
61
|
63
|
{
|
62
|
|
- // Create a new backlight device
|
63
|
|
- light_device_t *backlight_device = malloc(sizeof(light_device_t));
|
64
|
|
- snprintf(backlight_device->name, sizeof(backlight_device->name), "%s", "backlight");
|
65
|
|
-
|
66
|
|
- // Add it to the enumerator
|
67
|
|
- light_add_enumerator_device(enumerator, backlight_device);
|
68
|
|
-
|
69
|
|
- // Iterate through the backlight controllers and create a device_target for each controller
|
70
|
|
- DIR *backlight_dir;
|
71
|
|
- struct dirent *curr_entry;
|
72
|
|
-
|
73
|
|
- // Keep track of the best controller, and create an autodevice from that
|
74
|
|
- char best_controller[NAME_MAX];
|
75
|
|
- uint64_t best_value = 0;
|
76
|
|
-
|
77
|
|
- if((backlight_dir = opendir("/sys/class/backlight")) == NULL)
|
78
|
|
- {
|
79
|
|
- LIGHT_ERR("failed to open backlight controller directory for reading");
|
80
|
|
- return false;
|
81
|
|
- }
|
82
|
|
-
|
83
|
|
- while((curr_entry = readdir(backlight_dir)) != NULL)
|
84
|
|
- {
|
85
|
|
- // Skip dot entries
|
86
|
|
- if(curr_entry->d_name[0] == '.')
|
87
|
|
- {
|
88
|
|
- continue;
|
89
|
|
- }
|
90
|
|
-
|
91
|
|
- // Create a new device target for the controller
|
92
|
|
- light_device_target_t *backlight_controller = malloc(sizeof(light_device_target_t));
|
93
|
|
- snprintf(backlight_controller->name, sizeof(backlight_controller->name), "%s", curr_entry->d_name);
|
94
|
|
-
|
95
|
|
- // Setup the function bindings
|
96
|
|
- backlight_controller->set_value = impl_sysfs_set;
|
97
|
|
- backlight_controller->get_value = impl_sysfs_get;
|
98
|
|
- backlight_controller->get_max_value = impl_sysfs_getmax;
|
99
|
|
- backlight_controller->custom_command = impl_sysfs_command;
|
100
|
|
-
|
101
|
|
- // Setup the target data
|
102
|
|
- impl_sysfs_data_t *dev_data = malloc(sizeof(impl_sysfs_data_t));
|
103
|
|
- backlight_controller->device_target_data = dev_data;
|
104
|
|
- snprintf(dev_data->brightness, sizeof(dev_data->brightness), "/sys/class/backlight/%s/brightness", curr_entry->d_name);
|
105
|
|
- snprintf(dev_data->max_brightness, sizeof(dev_data->max_brightness), "/sys/class/backlight/%s/max_brightness", curr_entry->d_name);
|
106
|
|
-
|
107
|
|
- // Read the max brightness to get the best one
|
108
|
|
- uint64_t curr_value = 0;
|
109
|
|
- if(light_file_read_uint64(dev_data->max_brightness, &curr_value))
|
110
|
|
- {
|
111
|
|
- if(curr_value > best_value)
|
112
|
|
- {
|
113
|
|
- best_value = curr_value;
|
114
|
|
- snprintf(best_controller, sizeof(best_controller), "%s", backlight_controller->name);
|
115
|
|
- }
|
116
|
|
- }
|
117
|
|
-
|
118
|
|
- // Add it to the device
|
119
|
|
- light_add_device_target(backlight_device, backlight_controller);
|
120
|
|
- }
|
121
|
|
-
|
122
|
|
- // Create an auto controller
|
123
|
|
- light_device_target_t *auto_controller = malloc(sizeof(light_device_target_t));
|
124
|
|
- snprintf(auto_controller->name, sizeof(auto_controller->name), "%s", "auto");
|
125
|
|
-
|
126
|
|
- // Setup the function bindings
|
127
|
|
- auto_controller->set_value = impl_sysfs_set;
|
128
|
|
- auto_controller->get_value = impl_sysfs_get;
|
129
|
|
- auto_controller->get_max_value = impl_sysfs_getmax;
|
130
|
|
- auto_controller->custom_command = impl_sysfs_command;
|
131
|
|
-
|
132
|
|
- // Setup the target data
|
133
|
|
- impl_sysfs_data_t *dev_data = malloc(sizeof(impl_sysfs_data_t));
|
134
|
|
- auto_controller->device_target_data = dev_data;
|
135
|
|
- snprintf(dev_data->brightness, sizeof(dev_data->brightness), "/sys/class/backlight/%s/brightness", best_controller);
|
136
|
|
- snprintf(dev_data->max_brightness, sizeof(dev_data->max_brightness), "/sys/class/backlight/%s/max_brightness", best_controller);
|
137
|
|
-
|
138
|
|
- // Add it to the device
|
139
|
|
- light_add_device_target(backlight_device, auto_controller);
|
140
|
|
-
|
141
|
|
- return true;
|
|
64
|
+ // Create a new backlight device
|
|
65
|
+ light_device_t *backlight_device = malloc(sizeof(light_device_t));
|
|
66
|
+ snprintf(backlight_device->name, sizeof(backlight_device->name), "%s", "backlight");
|
|
67
|
+
|
|
68
|
+ // Add it to the enumerator
|
|
69
|
+ light_add_enumerator_device(enumerator, backlight_device);
|
|
70
|
+
|
|
71
|
+ // Iterate through the backlight controllers and create a device_target for each controller
|
|
72
|
+ DIR *backlight_dir;
|
|
73
|
+ struct dirent *curr_entry;
|
|
74
|
+
|
|
75
|
+ // Keep track of the best controller, and create an autodevice from that
|
|
76
|
+ char best_controller[NAME_MAX];
|
|
77
|
+ uint64_t best_value = 0;
|
|
78
|
+
|
|
79
|
+ if((backlight_dir = opendir("/sys/class/backlight")) == NULL)
|
|
80
|
+ {
|
|
81
|
+ LIGHT_ERR("failed to open backlight controller directory for reading");
|
|
82
|
+ return false;
|
|
83
|
+ }
|
|
84
|
+
|
|
85
|
+ while((curr_entry = readdir(backlight_dir)) != NULL)
|
|
86
|
+ {
|
|
87
|
+ // Skip dot entries
|
|
88
|
+ if(curr_entry->d_name[0] == '.')
|
|
89
|
+ {
|
|
90
|
+ continue;
|
|
91
|
+ }
|
|
92
|
+
|
|
93
|
+ // Create a new device target for the controller
|
|
94
|
+ light_device_target_t *backlight_controller = malloc(sizeof(light_device_target_t));
|
|
95
|
+ snprintf(backlight_controller->name, sizeof(backlight_controller->name), "%s", curr_entry->d_name);
|
|
96
|
+
|
|
97
|
+ // Setup the function bindings
|
|
98
|
+ backlight_controller->set_value = impl_sysfs_set;
|
|
99
|
+ backlight_controller->get_value = impl_sysfs_get;
|
|
100
|
+ backlight_controller->get_max_value = impl_sysfs_getmax;
|
|
101
|
+ backlight_controller->custom_command = impl_sysfs_command;
|
|
102
|
+
|
|
103
|
+ // Setup the target data
|
|
104
|
+ impl_sysfs_data_t *dev_data = malloc(sizeof(impl_sysfs_data_t));
|
|
105
|
+ backlight_controller->device_target_data = dev_data;
|
|
106
|
+ snprintf(dev_data->brightness, sizeof(dev_data->brightness), "/sys/class/backlight/%s/brightness", curr_entry->d_name);
|
|
107
|
+ snprintf(dev_data->max_brightness, sizeof(dev_data->max_brightness), "/sys/class/backlight/%s/max_brightness", curr_entry->d_name);
|
|
108
|
+
|
|
109
|
+ // Read the max brightness to get the best one
|
|
110
|
+ uint64_t curr_value = 0;
|
|
111
|
+ if(light_file_read_uint64(dev_data->max_brightness, &curr_value))
|
|
112
|
+ {
|
|
113
|
+ if(curr_value > best_value)
|
|
114
|
+ {
|
|
115
|
+ best_value = curr_value;
|
|
116
|
+ snprintf(best_controller, sizeof(best_controller), "%s", backlight_controller->name);
|
|
117
|
+ }
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+ // Add it to the device
|
|
121
|
+ light_add_device_target(backlight_device, backlight_controller);
|
|
122
|
+ }
|
|
123
|
+
|
|
124
|
+ closedir(backlight_dir);
|
|
125
|
+
|
|
126
|
+ // Create an auto controller
|
|
127
|
+ light_device_target_t *auto_controller = malloc(sizeof(light_device_target_t));
|
|
128
|
+ snprintf(auto_controller->name, sizeof(auto_controller->name), "%s", "auto");
|
|
129
|
+
|
|
130
|
+ // Setup the function bindings
|
|
131
|
+ auto_controller->set_value = impl_sysfs_set;
|
|
132
|
+ auto_controller->get_value = impl_sysfs_get;
|
|
133
|
+ auto_controller->get_max_value = impl_sysfs_getmax;
|
|
134
|
+ auto_controller->custom_command = impl_sysfs_command;
|
|
135
|
+
|
|
136
|
+ // Setup the target data
|
|
137
|
+ impl_sysfs_data_t *dev_data = malloc(sizeof(impl_sysfs_data_t));
|
|
138
|
+ auto_controller->device_target_data = dev_data;
|
|
139
|
+ snprintf(dev_data->brightness, sizeof(dev_data->brightness), "/sys/class/backlight/%s/brightness", best_controller);
|
|
140
|
+ snprintf(dev_data->max_brightness, sizeof(dev_data->max_brightness), "/sys/class/backlight/%s/max_brightness", best_controller);
|
|
141
|
+
|
|
142
|
+ // Add it to the device
|
|
143
|
+ light_add_device_target(backlight_device, auto_controller);
|
|
144
|
+
|
|
145
|
+ return true;
|
142
|
146
|
}
|
143
|
147
|
|
144
|
148
|
bool impl_sysfs_init(light_device_enumerator_t *enumerator)
|
145
|
149
|
{
|
146
|
|
- // Create a device for the backlight
|
147
|
|
- _impl_sysfs_init_backlight(enumerator);
|
148
|
|
-
|
149
|
|
- // Create devices for the leds
|
150
|
|
- _impl_sysfs_init_leds(enumerator);
|
151
|
|
-
|
152
|
|
- return true;
|
|
150
|
+ // Create a device for the backlight
|
|
151
|
+ _impl_sysfs_init_backlight(enumerator);
|
|
152
|
+
|
|
153
|
+ // Create devices for the leds
|
|
154
|
+ _impl_sysfs_init_leds(enumerator);
|
|
155
|
+
|
|
156
|
+ return true;
|
153
|
157
|
}
|
154
|
158
|
|
155
|
159
|
bool impl_sysfs_free(light_device_enumerator_t *enumerator)
|
156
|
160
|
{
|
157
|
|
- // Iterate through the devices in the enumerator
|
158
|
|
- for(uint64_t d = 0; d < enumerator->num_devices; d++)
|
159
|
|
- {
|
160
|
|
- light_device_t *curr_device = enumerator->devices[d];
|
161
|
|
-
|
162
|
|
- // If the given device points to NULL, we can safely skip it
|
163
|
|
- if(curr_device == NULL)
|
164
|
|
- {
|
165
|
|
- continue;
|
166
|
|
- }
|
167
|
|
-
|
168
|
|
- // If the given device has a targets array that isnt NULL, iterate through it to free the targets, then free the array
|
169
|
|
- if(curr_device->targets != NULL)
|
170
|
|
- {
|
171
|
|
- for(uint64_t t = 0; t < curr_device->num_targets; t++)
|
172
|
|
- {
|
173
|
|
- light_device_target_t *curr_target = curr_device->targets[t];
|
174
|
|
-
|
175
|
|
- if(curr_target == NULL)
|
176
|
|
- {
|
177
|
|
- continue;
|
178
|
|
- }
|
179
|
|
-
|
180
|
|
- if(curr_target->device_target_data != NULL)
|
181
|
|
- {
|
182
|
|
- free(curr_target->device_target_data);
|
183
|
|
- }
|
184
|
|
-
|
185
|
|
- free(curr_target);
|
186
|
|
- }
|
187
|
|
-
|
188
|
|
- free(curr_device->targets);
|
189
|
|
- }
|
190
|
|
-
|
191
|
|
- // If the given device has any device_data, free it
|
192
|
|
- if(curr_device->device_data != NULL)
|
193
|
|
- {
|
194
|
|
- free(curr_device->device_data);
|
195
|
|
- }
|
196
|
|
-
|
197
|
|
- // Free the device
|
198
|
|
- free(curr_device);
|
199
|
|
- }
|
200
|
|
-
|
201
|
|
- // Free the devices array
|
202
|
|
- free(enumerator->devices);
|
203
|
|
- enumerator->devices = NULL;
|
204
|
|
- enumerator->num_devices = 0;
|
205
|
|
-
|
206
|
|
- return true;
|
|
161
|
+ // Iterate through the devices in the enumerator
|
|
162
|
+ for(uint64_t d = 0; d < enumerator->num_devices; d++)
|
|
163
|
+ {
|
|
164
|
+ light_device_t *curr_device = enumerator->devices[d];
|
|
165
|
+
|
|
166
|
+ // If the given device points to NULL, we can safely skip it
|
|
167
|
+ if(curr_device == NULL)
|
|
168
|
+ {
|
|
169
|
+ continue;
|
|
170
|
+ }
|
|
171
|
+
|
|
172
|
+ // If the given device has a targets array that isnt NULL, iterate through it to free the targets, then free the array
|
|
173
|
+ if(curr_device->targets != NULL)
|
|
174
|
+ {
|
|
175
|
+ for(uint64_t t = 0; t < curr_device->num_targets; t++)
|
|
176
|
+ {
|
|
177
|
+ light_device_target_t *curr_target = curr_device->targets[t];
|
|
178
|
+
|
|
179
|
+ if(curr_target == NULL)
|
|
180
|
+ {
|
|
181
|
+ continue;
|
|
182
|
+ }
|
|
183
|
+
|
|
184
|
+ if(curr_target->device_target_data != NULL)
|
|
185
|
+ {
|
|
186
|
+ free(curr_target->device_target_data);
|
|
187
|
+ }
|
|
188
|
+
|
|
189
|
+ free(curr_target);
|
|
190
|
+ }
|
|
191
|
+
|
|
192
|
+ free(curr_device->targets);
|
|
193
|
+ }
|
|
194
|
+
|
|
195
|
+ // If the given device has any device_data, free it
|
|
196
|
+ if(curr_device->device_data != NULL)
|
|
197
|
+ {
|
|
198
|
+ free(curr_device->device_data);
|
|
199
|
+ }
|
|
200
|
+
|
|
201
|
+ // Free the device
|
|
202
|
+ free(curr_device);
|
|
203
|
+ }
|
|
204
|
+
|
|
205
|
+ // Free the devices array
|
|
206
|
+ free(enumerator->devices);
|
|
207
|
+ enumerator->devices = NULL;
|
|
208
|
+ enumerator->num_devices = 0;
|
|
209
|
+
|
|
210
|
+ return true;
|
207
|
211
|
}
|
208
|
212
|
|
209
|
213
|
bool impl_sysfs_set(light_device_target_t *target, uint64_t in_value)
|
210
|
214
|
{
|
211
|
|
- impl_sysfs_data_t *data = (impl_sysfs_data_t*)target->device_target_data;
|
|
215
|
+ impl_sysfs_data_t *data = (impl_sysfs_data_t*)target->device_target_data;
|
212
|
216
|
|
213
|
|
- if(!light_file_write_uint64(data->brightness, in_value))
|
214
|
|
- {
|
215
|
|
- LIGHT_ERR("failed to write to sysfs device");
|
216
|
|
- return false;
|
217
|
|
- }
|
218
|
|
-
|
219
|
|
- return true;
|
|
217
|
+ if(!light_file_write_uint64(data->brightness, in_value))
|
|
218
|
+ {
|
|
219
|
+ LIGHT_ERR("failed to write to sysfs device");
|
|
220
|
+ return false;
|
|
221
|
+ }
|
|
222
|
+
|
|
223
|
+ return true;
|
220
|
224
|
}
|
221
|
225
|
|
222
|
226
|
bool impl_sysfs_get(light_device_target_t *target, uint64_t *out_value)
|
223
|
227
|
{
|
224
|
|
- impl_sysfs_data_t *data = (impl_sysfs_data_t*)target->device_target_data;
|
|
228
|
+ impl_sysfs_data_t *data = (impl_sysfs_data_t*)target->device_target_data;
|
225
|
229
|
|
226
|
|
- if(!light_file_read_uint64(data->brightness, out_value))
|
227
|
|
- {
|
228
|
|
- LIGHT_ERR("failed to read from sysfs device");
|
229
|
|
- return false;
|
230
|
|
- }
|
231
|
|
-
|
232
|
|
- return true;
|
|
230
|
+ if(!light_file_read_uint64(data->brightness, out_value))
|
|
231
|
+ {
|
|
232
|
+ LIGHT_ERR("failed to read from sysfs device");
|
|
233
|
+ return false;
|
|
234
|
+ }
|
|
235
|
+
|
|
236
|
+ return true;
|
233
|
237
|
}
|
234
|
238
|
|
235
|
239
|
bool impl_sysfs_getmax(light_device_target_t *target, uint64_t *out_value)
|
236
|
240
|
{
|
237
|
|
- impl_sysfs_data_t *data = (impl_sysfs_data_t*)target->device_target_data;
|
|
241
|
+ impl_sysfs_data_t *data = (impl_sysfs_data_t*)target->device_target_data;
|
238
|
242
|
|
239
|
|
- if(!light_file_read_uint64(data->max_brightness, out_value))
|
240
|
|
- {
|
241
|
|
- LIGHT_ERR("failed to read from sysfs device");
|
242
|
|
- return false;
|
243
|
|
- }
|
244
|
|
-
|
245
|
|
- return true;
|
|
243
|
+ if(!light_file_read_uint64(data->max_brightness, out_value))
|
|
244
|
+ {
|
|
245
|
+ LIGHT_ERR("failed to read from sysfs device");
|
|
246
|
+ return false;
|
|
247
|
+ }
|
|
248
|
+
|
|
249
|
+ return true;
|
246
|
250
|
}
|
247
|
251
|
|
248
|
252
|
bool impl_sysfs_command(light_device_target_t *target, char const *command_string)
|
249
|
253
|
{
|
250
|
|
- // No current need for custom commands in sysfs enumerator
|
251
|
|
- // To implement support, simply parse the command string to your liking, and return false on invalid input or results!
|
252
|
|
- return true;
|
|
254
|
+ // No current need for custom commands in sysfs enumerator
|
|
255
|
+ // To implement support, simply parse the command string to your liking, and return false on invalid input or results!
|
|
256
|
+ return true;
|
253
|
257
|
}
|
254
|
258
|
|
255
|
259
|
|