Ver código fonte

General cleanup & fixes

Fredrik Svantesson 6 anos atrás
pai
commit
b3b9b6086d
8 arquivos alterados com 1183 adições e 1213 exclusões
  1. 1
    1
      src/Makefile.am
  2. 90
    88
      src/helpers.c
  3. 18
    22
      src/helpers.h
  4. 214
    210
      src/impl/sysfs.c
  5. 7
    4
      src/impl/sysfs.h
  6. 803
    809
      src/light.c
  7. 38
    67
      src/light.h
  8. 12
    12
      src/main.c

+ 1
- 1
src/Makefile.am Ver arquivo

@@ -1,7 +1,7 @@
1 1
 bin_PROGRAMS   = light
2 2
 light_SOURCES  = main.c light.c light.h helpers.c helpers.h impl/sysfs.c impl/sysfs.h
3 3
 light_CPPFLAGS = -I../include -D_GNU_SOURCE
4
-light_CFLAGS   = -W -Wall -Wextra -std=gnu99 -Wno-type-limits
4
+light_CFLAGS   = -W -Wall -Wextra -std=gnu18 -Wno-type-limits -Wno-format-truncation -Wno-unused-parameter
5 5
 
6 6
 if CLASSIC
7 7
 install-exec-hook:

+ 90
- 88
src/helpers.c Ver arquivo

@@ -11,130 +11,132 @@
11 11
 
12 12
 bool light_file_read_uint64(char const *filename, uint64_t *val)
13 13
 {
14
-	FILE *fp;
15
-	uint64_t data;
16
-
17
-	fp = fopen(filename, "r");
18
-	if (!fp)
19
-	{
20
-		LIGHT_PERMERR("reading");
21
-		return false;
22
-	}
23
-
24
-	if (fscanf(fp, "%lu", &data) != 1)
25
-	{
26
-		LIGHT_ERR("Couldn't parse an unsigned integer from '%s'", filename);
27
-		fclose(fp);
28
-		return false;
29
-	}
30
-
31
-	*val = data;
32
-
33
-	fclose(fp);
34
-	return true;
14
+    FILE *fp;
15
+    uint64_t data;
16
+
17
+    fp = fopen(filename, "r");
18
+    if (!fp)
19
+    {
20
+        LIGHT_PERMERR("reading");
21
+        return false;
22
+    }
23
+
24
+    if (fscanf(fp, "%lu", &data) != 1)
25
+    {
26
+        LIGHT_ERR("Couldn't parse an unsigned integer from '%s'", filename);
27
+        fclose(fp);
28
+        return false;
29
+    }
30
+
31
+    *val = data;
32
+
33
+    fclose(fp);
34
+    return true;
35 35
 }
36 36
 
37 37
 bool light_file_write_uint64(char const *filename, uint64_t val)
38 38
 {
39
-	FILE *fp;
40
-
41
-	fp = fopen(filename, "w");
42
-	if (!fp)
43
-	{
44
-		LIGHT_PERMERR("writing");
45
-		return false;
46
-	}
47
-
48
-	if (fprintf(fp, "%lu", val) < 0)
49
-	{
50
-		LIGHT_ERR("fprintf failed");
51
-		fclose(fp);
52
-		return false;
53
-	}
54
-
55
-	fclose(fp);
56
-	return true;
39
+    FILE *fp;
40
+
41
+    fp = fopen(filename, "w");
42
+    if (!fp)
43
+    {
44
+        LIGHT_PERMERR("writing");
45
+        return false;
46
+    }
47
+
48
+    if (fprintf(fp, "%lu", val) < 0)
49
+    {
50
+        LIGHT_ERR("fprintf failed");
51
+        fclose(fp);
52
+        return false;
53
+    }
54
+
55
+    fclose(fp);
56
+    return true;
57 57
 }
58 58
 
59 59
 /* Returns true if file is writable, false otherwise */
60 60
 bool light_file_is_writable(char const *filename)
61 61
 {
62
-	FILE *fp;
62
+    FILE *fp;
63 63
 
64
-	fp = fopen(filename, "r+");
65
-	if (!fp)
66
-	{
67
-		LIGHT_PERMWARN("writing");
68
-		return false;
69
-	}
64
+    fp = fopen(filename, "r+");
65
+    if (!fp)
66
+    {
67
+        LIGHT_PERMWARN("writing");
68
+        return false;
69
+    }
70 70
 
71
-	fclose(fp);
72
-	return true;
71
+    fclose(fp);
72
+    return true;
73 73
 }
74 74
 
75 75
 /* Returns true if file is readable, false otherwise */
76 76
 bool light_file_is_readable(char const *filename)
77 77
 {
78
-	FILE *fp;
78
+    FILE *fp;
79 79
 
80
-	fp = fopen(filename, "r");
81
-	if (!fp)
82
-	{
83
-		LIGHT_PERMWARN("reading");
84
-		return false;
85
-	}
80
+    fp = fopen(filename, "r");
81
+    if (!fp)
82
+    {
83
+        LIGHT_PERMWARN("reading");
84
+        return false;
85
+    }
86 86
 
87
-	fclose(fp);
88
-	return true;
87
+    fclose(fp);
88
+    return true;
89 89
 }
90 90
 
91 91
 /* Prints a notice about a value which was below `x` and was adjusted to it */
92 92
 uint64_t light_log_clamp_min(uint64_t min)
93 93
 {
94
-	LIGHT_NOTE("too small value, adjusting to mininum %lu (raw)", min);
95
-	return min;
94
+    LIGHT_NOTE("too small value, adjusting to mininum %lu (raw)", min);
95
+    return min;
96 96
 }
97 97
 
98 98
 /* Prints a notice about a value which was above `x` and was adjusted to it */
99 99
 uint64_t light_log_clamp_max(uint64_t max)
100 100
 {
101
-	LIGHT_NOTE("too large value, adjusting to mavalimum %lu (raw)", max);
102
-	return max;
101
+    LIGHT_NOTE("too large value, adjusting to mavalimum %lu (raw)", max);
102
+    return max;
103 103
 }
104 104
 
105 105
 /* Clamps the `percent` value between 0% and 100% */
106 106
 double light_percent_clamp(double val)
107 107
 {
108
-	if (val < 0.0)
109
-	{
110
-		LIGHT_WARN("specified value %g%% is not valid, adjusting it to 0%%", val);
111
-		return 0.0;
112
-	}
113
-
114
-	if (val > 100.0)
115
-	{
116
-		LIGHT_WARN("specified value %g%% is not valid, adjusting it to 100%%", val);
117
-		return 100.0;
118
-	}
119
-
120
-	return val;
108
+    if (val < 0.0)
109
+    {
110
+        LIGHT_WARN("specified value %g%% is not valid, adjusting it to 0%%", val);
111
+        return 0.0;
112
+    }
113
+
114
+    if (val > 100.0)
115
+    {
116
+        LIGHT_WARN("specified value %g%% is not valid, adjusting it to 100%%", val);
117
+        return 100.0;
118
+    }
119
+
120
+    return val;
121 121
 }
122 122
 
123 123
 int light_mkpath(char *dir, mode_t mode)
124 124
 {
125
-	struct stat sb;
126
-
127
-	if (!dir)
128
-	{
129
-		errno = EINVAL;
130
-		return -1;
131
-	}
132
-
133
-	if (!stat(dir, &sb))
134
-		return 0;
135
-
136
-	light_mkpath(dirname(strdupa(dir)), mode);
137
-
138
-	return mkdir(dir, mode);
125
+    struct stat sb;
126
+
127
+    if (!dir)
128
+    {
129
+        errno = EINVAL;
130
+        return -1;
131
+    }
132
+
133
+    if (!stat(dir, &sb))
134
+        return 0;
135
+
136
+    char *tempdir = strdup(dir);
137
+    light_mkpath(dirname(tempdir), mode);
138
+    free(tempdir);
139
+    
140
+    return mkdir(dir, mode);
139 141
 }
140 142
 

+ 18
- 22
src/helpers.h Ver arquivo

@@ -7,40 +7,36 @@
7 7
 #include <stdio.h>
8 8
 
9 9
 /* Clamps x(value) between y(min) and z(max) in a nested ternary operation */
10
-#define LIGHT_CLAMP(val, min, max)					\
11
-	(val < min							\
12
-	 ? light_log_clamp_min(min)					\
13
-	 : (val > max							\
14
-	    ? light_log_clamp_max(max)					\
15
-	    : val ))
10
+#define LIGHT_CLAMP(val, min, max) (val < min ? light_log_clamp_min(min) : (val > max ? light_log_clamp_max(max) : val))
16 11
 
17 12
 /* Verbosity levels: 
18
- * 0 - No output
19
- * 1 - Errors
20
- * 2 - Errors, warnings 
21
- * 3 - Errors, warnings, notices
22
- */
13
+* 0 - No output
14
+* 1 - Errors
15
+* 2 - Errors, warnings 
16
+* 3 - Errors, warnings, notices
17
+*/
23 18
 typedef enum {
24
-	LIGHT_ERROR_LEVEL = 1,
25
-	LIGHT_WARN_LEVEL,
26
-	LIGHT_NOTE_LEVEL
19
+    LIGHT_ERROR_LEVEL = 1,
20
+    LIGHT_WARN_LEVEL,
21
+    LIGHT_NOTE_LEVEL
27 22
 } light_loglevel_t;
28 23
 
29 24
 light_loglevel_t light_loglevel;
30 25
 
31
-#define LIGHT_LOG(lvl, fp, fmt, args...)				\
32
-	if (light_loglevel >= lvl)					\
33
-		fprintf(fp, "%s:%d:" fmt "\n", __FILE__, __LINE__, ##args)
26
+#define LIGHT_LOG(lvl, fp, fmt, args...)\
27
+    if (light_loglevel >= lvl)\
28
+        fprintf(fp, "%s:%d:" fmt "\n", __FILE__, __LINE__, ##args)
34 29
 
35 30
 #define LIGHT_NOTE(fmt, args...) LIGHT_LOG(LIGHT_NOTE_LEVEL,  stdout, " Notice: " fmt, ##args)
36 31
 #define LIGHT_WARN(fmt, args...) LIGHT_LOG(LIGHT_WARN_LEVEL,  stderr, " Warning: " fmt, ##args)
37 32
 #define LIGHT_ERR(fmt, args...)  LIGHT_LOG(LIGHT_ERROR_LEVEL, stderr, " Error: " fmt, ##args)
38 33
 #define LIGHT_MEMERR()           LIGHT_ERR("memory error");
39
-#define LIGHT_PERMLOG(act, log)						\
40
-	do {								\
41
-		log("could not open '%s' for " act, filename);		\
42
-		log("Verify it exists with the right permissions");	\
43
-	} while (0)
34
+#define LIGHT_PERMLOG(act, log)\
35
+    do {\
36
+        log("could not open '%s' for " act, filename);\
37
+        log("Verify it exists with the right permissions");\
38
+    } while (0)
39
+
44 40
 #define LIGHT_PERMERR(x)         LIGHT_PERMLOG(x, LIGHT_ERR)
45 41
 #define LIGHT_PERMWARN(x)        LIGHT_PERMLOG(x, LIGHT_WARN)
46 42
 

+ 214
- 210
src/impl/sysfs.c Ver arquivo

@@ -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
 

+ 7
- 4
src/impl/sysfs.h Ver arquivo

@@ -7,10 +7,13 @@
7 7
 // Enumerates devices for backlights and leds
8 8
 
9 9
 // Device target data 
10
-typedef struct {
11
-	char brightness[NAME_MAX];
12
-	char max_brightness[NAME_MAX];
13
-} impl_sysfs_data_t;
10
+struct _impl_sysfs_data_t
11
+{
12
+    char brightness[NAME_MAX];
13
+    char max_brightness[NAME_MAX];
14
+};
15
+
16
+typedef struct _impl_sysfs_data_t impl_sysfs_data_t;
14 17
 
15 18
 bool impl_sysfs_init(light_device_enumerator_t *enumerator);
16 19
 bool impl_sysfs_free(light_device_enumerator_t *enumerator);

+ 803
- 809
src/light.c
Diferenças do arquivo suprimidas por serem muito extensas
Ver arquivo


+ 38
- 67
src/light.h Ver arquivo

@@ -29,24 +29,23 @@ typedef bool (*LFUNCCUSTOMCMD)(light_device_target_t*, char const *);
29 29
 /* Describes a target within a device (for example a led on a keyboard, or a controller for a backlight) */
30 30
 struct _light_device_target_t
31 31
 {
32
-	char           name[256];
33
-	LFUNCVALSET    set_value;
34
-	LFUNCVALGET    get_value;
35
-	LFUNCMAXVALGET get_max_value;
36
-	LFUNCCUSTOMCMD custom_command;
37
-	void           *device_target_data;
38
-	light_device_t *device;
32
+    char           name[256];
33
+    LFUNCVALSET    set_value;
34
+    LFUNCVALGET    get_value;
35
+    LFUNCMAXVALGET get_max_value;
36
+    LFUNCCUSTOMCMD custom_command;
37
+    void           *device_target_data;
38
+    light_device_t *device;
39 39
 };
40 40
 
41
-
42 41
 /* Describes a device (a backlight, a keyboard, a led-strip) */
43 42
 struct _light_device_t
44 43
 {
45
-	char                  name[256];
46
-	light_device_target_t **targets;
47
-	uint64_t              num_targets;
48
-	void                  *device_data;
49
-	light_device_enumerator_t *enumerator;
44
+    char                  name[256];
45
+    light_device_target_t **targets;
46
+    uint64_t              num_targets;
47
+    void                  *device_data;
48
+    light_device_enumerator_t *enumerator;
50 49
 };
51 50
 
52 51
 
@@ -56,12 +55,12 @@ typedef bool (*LFUNCENUMFREE)(light_device_enumerator_t*);
56 55
 /* An enumerator that is responsible for creating and freeing devices as well as their targets */
57 56
 struct _light_device_enumerator_t
58 57
 {
59
-	char          name[256];
60
-	LFUNCENUMINIT init;
61
-	LFUNCENUMFREE free;
58
+    char            name[256];
59
+    LFUNCENUMINIT   init;
60
+    LFUNCENUMFREE   free;
62 61
 
63
-	light_device_t **devices;
64
-	uint64_t num_devices;
62
+    light_device_t  **devices;
63
+    uint64_t        num_devices;
65 64
 };
66 65
 
67 66
 typedef struct _light_context_t light_context_t;
@@ -69,29 +68,24 @@ typedef struct _light_context_t light_context_t;
69 68
 // A command that can be run (set, get, add, subtract, print help, print version, list devices etc.)
70 69
 typedef bool (*LFUNCCOMMAND)(light_context_t *);
71 70
 
72
-
73
-/*
74
-	Options
75
-	v		Specify verbosity, defaults to o
76
-	s		Specify target, defaults to sysfs/backlight/auto
77
-	r		Use raw values instead of percentage
78
-	
79
-	Operations
80
-	H,h 	Print help and exit
81
-	V		Print version and exit
82
-	L		List devices
83
-	
84
-	G		Get brigthness 
85
-	M		Get max brightness
86
-	N		Set minimum cap
87
-	P		Get minimum cap
88
-	S		Set brigthness
89
-	
90
-	A		Increase brightness
91
-	U		Decrease brightness
92
-	O		Save brightness
93
-	I		Restore brightness
94
- */
71
+struct _light_context_t
72
+{
73
+    struct 
74
+    {
75
+        LFUNCCOMMAND            command; // What command was issued 
76
+        uint64_t                value; // The input value, in raw mode
77
+        bool                    raw_mode; // Whether or not we use raw or percentage mode
78
+        light_device_target_t   *device_target; // The device target to act on
79
+    } run_params;
80
+
81
+    struct
82
+    {
83
+        char                    conf_dir[NAME_MAX]; // The path to the application cache directory 
84
+    } sys_params;
85
+    
86
+    light_device_enumerator_t   **enumerators;
87
+    uint64_t                    num_enumerators;
88
+};
95 89
 
96 90
 // The different available commands
97 91
 bool light_cmd_print_help(light_context_t *ctx); // H,h 
@@ -107,29 +101,6 @@ bool light_cmd_sub_brightness(light_context_t *ctx); // U
107 101
 bool light_cmd_save_brightness(light_context_t *ctx); // O
108 102
 bool light_cmd_restore_brightness(light_context_t *ctx); // I
109 103
 
110
-
111
-// CONFDIR/targets/<enumerator>/<device>/<target>/ minimum|saved
112
-
113
-struct _light_context_t
114
-{
115
-	struct 
116
-	{
117
-		LFUNCCOMMAND			command; // What command was issued 
118
-		uint64_t				value; // The input value, in raw mode
119
-		bool					raw_mode; // Whether or not we use raw or percentage mode
120
-		light_device_target_t	*device_target; // The device target to act on
121
-	} run_params;
122
-
123
-	struct
124
-	{
125
-		char					conf_dir[NAME_MAX]; // The path to the application cache directory 
126
-	} sys_params;
127
-	
128
-	light_device_enumerator_t **enumerators;
129
-	uint64_t num_enumerators;
130
-
131
-};
132
-
133 104
 /* Initializes the application, given the command-line. Returns a context. */
134 105
 light_context_t* light_initialize(int argc, char **argv);
135 106
 
@@ -155,9 +126,9 @@ void light_add_device_target(light_device_t *device, light_device_target_t *new_
155 126
 typedef struct _light_target_path_t light_target_path_t;
156 127
 struct _light_target_path_t 
157 128
 {
158
-	char enumerator[NAME_MAX];
159
-	char device[NAME_MAX];
160
-	char target[NAME_MAX];
129
+    char enumerator[NAME_MAX];
130
+    char device[NAME_MAX];
131
+    char target[NAME_MAX];
161 132
 };
162 133
 
163 134
 bool light_split_target_path(char const * in_path, light_target_path_t *out_path);

+ 12
- 12
src/main.c Ver arquivo

@@ -10,18 +10,18 @@
10 10
 
11 11
 int main(int argc, char **argv)
12 12
 {
13
-	light_context_t *light_ctx = light_initialize(argc, argv);
14
-	if (light_ctx == NULL) {
15
-		LIGHT_ERR("Initialization failed");
16
-		return LIGHT_RETURNVAL_INITFAIL;
17
-	}
13
+    light_context_t *light_ctx = light_initialize(argc, argv);
14
+    if (light_ctx == NULL) {
15
+        LIGHT_ERR("Initialization failed");
16
+        return LIGHT_RETURNVAL_INITFAIL;
17
+    }
18 18
 
19
-	if (!light_execute(light_ctx)) {
20
-		LIGHT_ERR("Execution failed");
21
-		light_free(light_ctx);
22
-		return LIGHT_RETURNVAL_EXECFAIL;
23
-	}
19
+    if (!light_execute(light_ctx)) {
20
+        LIGHT_ERR("Execution failed");
21
+        light_free(light_ctx);
22
+        return LIGHT_RETURNVAL_EXECFAIL;
23
+    }
24 24
 
25
-	light_free(light_ctx);
26
-	return LIGHT_RETURNVAL_SUCCESS;
25
+    light_free(light_ctx);
26
+    return LIGHT_RETURNVAL_SUCCESS;
27 27
 }