Pārlūkot izejas kodu

Merge pull request #48 from troglobit/coding-style

Coding style
Joachim Nilsson 5 gadus atpakaļ
vecāks
revīzija
e226e2d7d2
Revīzijas autora e-pasts nav piesaistīts nevienam kontam
6 mainītis faili ar 1059 papildinājumiem un 1344 dzēšanām
  1. 1
    1
      src/Makefile.am
  2. 72
    196
      src/helpers.c
  3. 45
    86
      src/helpers.h
  4. 851
    933
      src/light.c
  5. 78
    114
      src/light.h
  6. 12
    14
      src/main.c

+ 1
- 1
src/Makefile.am Parādīt failu

@@ -1,7 +1,7 @@
1 1
 bin_PROGRAMS   = light
2 2
 light_SOURCES  = main.c light.c light.h helpers.c helpers.h
3 3
 light_CPPFLAGS = -I../include -D_XOPEN_SOURCE=500
4
-light_CFLAGS   = -W -Wall -Wextra -pedantic -std=c99
4
+light_CFLAGS   = -W -Wall -Wextra -std=gnu99 -Wno-type-limits
5 5
 
6 6
 install-exec-hook:
7 7
 	chmod 4755 $(DESTDIR)$(bindir)/light

+ 72
- 196
src/helpers.c Parādīt failu

@@ -6,229 +6,105 @@
6 6
 #include <sys/types.h>
7 7
 #include <dirent.h>
8 8
 
9
-LIGHT_BOOL light_readUInt(char const * filename, unsigned int *i)
9
+bool light_file_read_val(char const *filename, unsigned long *val)
10 10
 {
11
-  FILE* fileHandle;
12
-  unsigned int iCopy;
13
-
14
-  fileHandle = fopen(filename, "r");
15
-
16
-  if(!fileHandle)
17
-  {
18
-    LIGHT_PERMERR("reading");
19
-    return FALSE;
20
-  }
21
-
22
-  if(fscanf(fileHandle, "%u", &iCopy) != 1)
23
-  {
24
-    LIGHT_ERR_FMT("Couldn't parse a positive integer number from '%s'", filename);
25
-    fclose(fileHandle);
26
-    return FALSE;
27
-  }
28
-  
29
-  *i = iCopy;
30
-
31
-  fclose(fileHandle);
32
-  return TRUE;
33
-}
34
-
35
-LIGHT_BOOL light_writeUInt(char const * filename, unsigned int i)
36
-{
37
-  FILE* fileHandle;
11
+	FILE *fp;
12
+	unsigned long data;
38 13
 
39
-  fileHandle = fopen(filename, "w");
14
+	fp = fopen(filename, "r");
15
+	if (!fp) {
16
+		LIGHT_PERMERR("reading");
17
+		return false;
18
+	}
40 19
 
41
-  if(!fileHandle)
42
-  {
43
-    LIGHT_PERMERR("writing");
44
-    return FALSE;
45
-  }
20
+	if (fscanf(fp, "%lu", &data) != 1) {
21
+		LIGHT_ERR("Couldn't parse a positive integer number from '%s'", filename);
22
+		fclose(fp);
23
+		return false;
24
+	}
46 25
 
47
-  if(fprintf(fileHandle, "%u", i) < 0)
48
-  {
49
-    LIGHT_ERR("fprintf failed");
50
-    fclose(fileHandle);
51
-    return FALSE;
52
-  }
26
+	*val = data;
53 27
 
54
-  fclose(fileHandle);
55
-  return TRUE;
28
+	fclose(fp);
29
+	return true;
56 30
 }
57 31
 
58
-
59
-LIGHT_BOOL light_readULong(char const * filename, unsigned long *i)
32
+bool light_file_write_val(char const *filename, unsigned long val)
60 33
 {
61
-  FILE* fileHandle;
62
-  unsigned long iCopy;;
63
-
64
-  fileHandle = fopen(filename, "r");
65
-
66
-  if(!fileHandle)
67
-  {
68
-    LIGHT_PERMERR("reading");
69
-    return FALSE;
70
-  }
71
-
72
-  if(fscanf(fileHandle, "%lu", &iCopy) != 1)
73
-  {
74
-    LIGHT_ERR_FMT("Couldn't parse a positive integer number from '%s'", filename);
75
-    fclose(fileHandle);
76
-    return FALSE;
77
-  }
78
-  
79
-  *i = iCopy;
80
-
81
-  fclose(fileHandle);
82
-  return TRUE;
34
+	FILE *fp;
35
+
36
+	fp = fopen(filename, "w");
37
+	if (!fp) {
38
+		LIGHT_PERMERR("writing");
39
+		return false;
40
+	}
41
+
42
+	if (fprintf(fp, "%lu", val) < 0) {
43
+		LIGHT_ERR("fprintf failed");
44
+		fclose(fp);
45
+		return false;
46
+	}
47
+
48
+	fclose(fp);
49
+	return true;
83 50
 }
84 51
 
85
-LIGHT_BOOL light_writeULong(char const * filename, unsigned long i)
52
+/* Returns true if file is writable, false otherwise */
53
+bool light_file_is_writable(char const *filename)
86 54
 {
87
-  FILE* fileHandle;
88
-
89
-  fileHandle = fopen(filename, "w");
55
+	FILE *fp;
90 56
 
91
-  if(!fileHandle)
92
-  {
93
-    LIGHT_PERMERR("writing");
94
-    return FALSE;
95
-  }
57
+	fp = fopen(filename, "r+");
58
+	if (!fp) {
59
+		LIGHT_PERMWARN("writing");
60
+		return false;
61
+	}
96 62
 
97
-  if(fprintf(fileHandle, "%lu", i) < 0)
98
-  {
99
-    LIGHT_ERR("fprintf failed");
100
-    fclose(fileHandle);
101
-    return FALSE;
102
-  }
103
-
104
-  fclose(fileHandle);
105
-  return TRUE;
63
+	fclose(fp);
64
+	return true;
106 65
 }
107 66
 
108
-
109
-
110
-LIGHT_BOOL light_readString(char const * filename, char *buffer, long* size)
67
+/* Returns true if file is readable, false otherwise */
68
+bool light_file_is_readable(char const *filename)
111 69
 {
112
-  FILE *fileHandle;
113
-  long fileSize;
114
-  long readSize;
115
-
116
-  /* Make sure buffer pointer is null */
117
-  if(buffer != NULL)
118
-  {
119
-    LIGHT_ERR("buffer passed to function isn't NULL");
120
-    return FALSE;
121
-  }
122
-
123
-  /* Open file */
124
-  fileHandle = fopen(filename, "r");
125
-
126
-  if(!fileHandle)
127
-  {
128
-    LIGHT_PERMERR("reading");
129
-    return FALSE;
130
-  }
131
-
132
-  /* Get the file size */
133
-  fseek(fileHandle, 0L, SEEK_END);
134
-  fileSize = ftell(fileHandle);
135
-  rewind(fileHandle);
136
-
137
-  /* Allocate the string and null-terminate it */
138
-  buffer = malloc(sizeof(char)*(fileSize+1));
139
-  memset(buffer, '\0', fileSize);
140
-
141
-  if(buffer == NULL)
142
-  {
143
-    LIGHT_MEMERR();
144
-    fclose(fileHandle);
145
-    return FALSE;
146
-  }
147
-
148
-  /* Read the file */
149
-  readSize = fread(buffer, sizeof(char), fileSize, fileHandle);
150
-
151
-  if(readSize != fileSize)
152
-  {
153
-    LIGHT_ERR("read error");
154
-    free(buffer);
155
-    fclose(fileHandle);
156
-    return FALSE;
157
-  }
158
-  
159
-  /* Set the size */
160
-  if(size != NULL)
161
-  {
162
-    *size = readSize;
163
-  }
164
-
165
-  /* All well, close handle and return TRUE */
166
-  fclose(fileHandle);
167
-  return TRUE;
168
-}
70
+	FILE *fp;
169 71
 
170
-LIGHT_BOOL light_isDir(char const * path)
171
-{
172
-  DIR *dirHandle = opendir(path);
72
+	fp = fopen(filename, "r");
73
+	if (!fp) {
74
+		LIGHT_PERMWARN("reading");
75
+		return false;
76
+	}
173 77
 
174
-  if(!dirHandle)
175
-  {
176
-    return FALSE;
177
-  }
178
-
179
-  closedir(dirHandle);
180
-  return TRUE;
78
+	fclose(fp);
79
+	return true;
181 80
 }
182 81
 
183
-LIGHT_BOOL light_isWritable(char const * filename)
82
+/* Prints a notice about a value which was below `x` and was adjusted to it */
83
+unsigned long light_log_clamp_min(unsigned long min)
184 84
 {
185
-  FILE* fileHandle = fopen(filename, "r+");
186
-
187
-  if(!fileHandle)
188
-  {
189
-    LIGHT_PERMWARN("writing");
190
-    return FALSE;
191
-  }
192
-
193
-  fclose(fileHandle);
194
-  return TRUE;
85
+	LIGHT_NOTE("too small value, adjusting to mininum %lu (raw)", min);
86
+	return min;
195 87
 }
196 88
 
197
-LIGHT_BOOL light_isReadable(char const * filename)
89
+/* Prints a notice about a value which was above `x` and was adjusted to it */
90
+unsigned long light_log_clamp_max(unsigned long max)
198 91
 {
199
-  FILE* fileHandle = fopen(filename, "r");
200
-
201
-  if(!fileHandle)
202
-  {
203
-    LIGHT_PERMWARN("reading");
204
-    return FALSE;
205
-  }
206
-
207
-  fclose(fileHandle);
208
-  return TRUE;
92
+	LIGHT_NOTE("too large value, adjusting to mavalimum %lu (raw)", max);
93
+	return max;
209 94
 }
210 95
 
211
-unsigned long light_logInfClamp(unsigned long x)
96
+/* Clamps the `percent` value between 0% and 100% */
97
+double light_percent_clamp(double val)
212 98
 {
213
-  LIGHT_NOTE_FMT("specified value is inferior to %lu (raw), so adjusting it to this mininum value", x);
214
-  return x;
215
-}
99
+	if (val < 0.0) {
100
+		LIGHT_WARN("specified value %g%% is not valid, adjusting it to 0%%", val);
101
+		return 0.0;
102
+	}
216 103
 
217
-unsigned long light_logSupClamp(unsigned long x)
218
-{
219
-  LIGHT_NOTE_FMT("specified value is superior to %lu (raw), so adjusting it to this maximum value", x);
220
-  return x;
221
-}
104
+	if (val > 100.0) {
105
+		LIGHT_WARN("specified value %g%% is not valid, adjusting it to 100%%", val);
106
+		return 100.0;
107
+	}
222 108
 
223
-double light_clampPercent(double p)
224
-{
225
-  if(p < 0.0)
226
-  {
227
-    LIGHT_WARN_FMT("specified value %g%% is not valid, adjusting it to 0%%", p);
228
-    return 0.0;
229
-  }else if(p > 100.0){
230
-    LIGHT_WARN_FMT("specified value %g%% is not valid, adjusting it to 100%%", p);
231
-    return 100.0;
232
-  }
233
-  return p;
109
+	return val;
234 110
 }

+ 45
- 86
src/helpers.h Parādīt failu

@@ -1,96 +1,55 @@
1 1
 #ifndef LIGHT_HELPERS_H
2 2
 #define LIGHT_HELPERS_H
3 3
 
4
-/* Clamps x(value) between y(min) and z(max) in a nested ternary operation. 
5
- * if(x < y)
6
- * {
7
- *  y;
8
- * }else{
9
- *  if(x>z)
10
- *  {
11
- *    z;
12
- *  }else{
13
- *    x;
14
- *  }
15
- * }*/
16
-#define LIGHT_CLAMP(x, y, z) ((x<y) ? (light_logInfClamp(y)) : ((x>z) ? (light_logSupClamp(z)) : x ))
4
+#include <stdbool.h>
5
+
6
+/* Clamps x(value) between y(min) and z(max) in a nested ternary operation */
7
+#define LIGHT_CLAMP(val, min, max)					\
8
+	(val < min							\
9
+	 ? light_log_clamp_min(min)					\
10
+	 : (val > max							\
11
+	    ? light_log_clamp_max(max)					\
12
+	    : val ))
17 13
 
18
-#define LIGHT_LOG_FMT_BUF_SIZE 1024
19 14
 /* Verbosity levels: 
20 15
  * 0 - No output
21 16
  * 1 - Errors
22 17
  * 2 - Errors, warnings 
23
- * 3 - Errors, warnings, notices */
24
-typedef enum LIGHT_LOG_LEVEL {
25
-  LIGHT_ERROR_LEVEL = 1,
26
-  LIGHT_WARN_LEVEL,
27
-  LIGHT_NOTE_LEVEL
28
-} LIGHT_LOG_LEVEL;
29
-
30
-LIGHT_LOG_LEVEL light_verbosity;
31
-char light_log_buffer[LIGHT_LOG_FMT_BUF_SIZE];
32
-
33
-#define LIGHT_LOG(lvl,f,t,x)if(light_verbosity >= lvl){fprintf(f,t": \"%s\", in \"%s\" on line %u.\n", x, __FILE__, __LINE__);}
34
-
35
-#define LIGHT_NOTE(x)LIGHT_LOG(LIGHT_NOTE_LEVEL,stdout,"notice",x)
36
-
37
-#define LIGHT_WARN(x)LIGHT_LOG(LIGHT_WARN_LEVEL,stderr,"warning",x)
38
-
39
-#define LIGHT_ERR(x)LIGHT_LOG(LIGHT_ERROR_LEVEL,stderr,"error",x)
40
-
41
-#define LIGHT_LOG_FMT(x,s,f)if(snprintf(light_log_buffer, LIGHT_LOG_FMT_BUF_SIZE,x,s) > 0){f(light_log_buffer);}
42
-
43
-#define LIGHT_NOTE_FMT(x,s)LIGHT_LOG_FMT(x,s,LIGHT_NOTE);
44
-
45
-#define LIGHT_WARN_FMT(x,s)LIGHT_LOG_FMT(x,s,LIGHT_WARN);
46
-
47
-#define LIGHT_ERR_FMT(x,s)LIGHT_LOG_FMT(x,s,LIGHT_ERR);
48
-
49
-#define LIGHT_MEMERR() LIGHT_ERR("memory error");
50
-
51
-#define LIGHT_PERMLOG(x,f)f##_FMT("could not open '%s' for "x,filename); f("check if this file exists or if you have the right permissions");
52
-
53
-#define LIGHT_PERMERR(x) LIGHT_PERMLOG(x,LIGHT_ERR)
54
-
55
-#define LIGHT_PERMWARN(x) LIGHT_PERMLOG(x,LIGHT_WARN)
56
-
57
-/* Typedef for boolean values */
58
-typedef enum LIGHT_BOOL {
59
-  FALSE = 0,
60
-  TRUE
61
-} LIGHT_BOOL;
62
-
63
-/* Reads an unsigned integer from a file into `i` if able, otherwise returns FALSE and leaves `i` untouched */
64
-LIGHT_BOOL light_readUInt(char const *filename, unsigned int *v);
65
-
66
-/* Writes an unsigned integer `i` into file `filename` if able, otherwise returns FALSE */
67
-LIGHT_BOOL light_writeUInt(char const *filename, unsigned int v);
68
-
69
-LIGHT_BOOL light_writeULong(char const *filename, unsigned long v);
70
-LIGHT_BOOL light_readULong(char const *filename, unsigned long *v);
71
-
72
-/* Reads a file into null-terminated `buffer` if able, otherwise returns FALSE
73
- * If `size` isn't NULL, it will be set to the read size.
74
- *
75
- * WARNING: `buffer` HAS to be freed by the user, also make sure it is NULL before passed */
76
-LIGHT_BOOL light_readString(char const * filename, char * buffer, long * size);
77
-
78
-/* Returns TRUE if `path` is a valid directory, FALSE otherwise */
79
-LIGHT_BOOL light_isDir(char const * path);
80
-
81
-/* Returns TRUE if file is writable, FALSE otherwise */
82
-LIGHT_BOOL light_isWritable(char const * filename);
83
-
84
-/* Returns TRUE if file is readable, FALSE otherwise */
85
-LIGHT_BOOL light_isReadable(char const * filename);
86
-
87
-/* Clamps the `percent` value between 0% and 100% */
88
-double light_clampPercent(double percent);
89
-
90
-/* Prints a notice about a value which was below `x` and was adjusted to it */
91
-unsigned long light_logInfClamp(unsigned long x);
92
-
93
-/* Prints a notice about a value which was above `x` and was adjusted to it */
94
-unsigned long light_logSupClamp(unsigned long x);
18
+ * 3 - Errors, warnings, notices
19
+ */
20
+typedef enum {
21
+	LIGHT_ERROR_LEVEL = 1,
22
+	LIGHT_WARN_LEVEL,
23
+	LIGHT_NOTE_LEVEL
24
+} light_loglevel_t;
25
+
26
+light_loglevel_t light_loglevel;
27
+
28
+#define LIGHT_LOG(lvl, fp, fmt, args...)				\
29
+	if (light_loglevel >= lvl)					\
30
+		fprintf(fp, "%s:%d:" fmt, __FILE__, __LINE__, ##args)
31
+
32
+#define LIGHT_NOTE(fmt, args...) LIGHT_LOG(LIGHT_NOTE_LEVEL,  stdout, "NOTE:" fmt, ##args)
33
+#define LIGHT_WARN(fmt, args...) LIGHT_LOG(LIGHT_WARN_LEVEL,  stderr, "WARN:" fmt, ##args)
34
+#define LIGHT_ERR(fmt, args...)  LIGHT_LOG(LIGHT_ERROR_LEVEL, stderr, "!ERR:" fmt, ##args)
35
+#define LIGHT_MEMERR()           LIGHT_ERR("memory error");
36
+#define LIGHT_PERMLOG(act, log)						\
37
+	do {								\
38
+		log("could not open '%s' for " act, filename);		\
39
+		log("Verify it exists with the right permissions");	\
40
+	} while (0)
41
+#define LIGHT_PERMERR(x)         LIGHT_PERMLOG(x, LIGHT_ERR)
42
+#define LIGHT_PERMWARN(x)        LIGHT_PERMLOG(x, LIGHT_WARN)
43
+
44
+bool light_file_write_val   (char const *filename, unsigned long val);
45
+bool light_file_read_val    (char const *filename, unsigned long *val);
46
+
47
+bool light_file_is_writable (char const *filename);
48
+bool light_file_is_readable (char const *filename);
49
+
50
+unsigned long light_log_clamp_min(unsigned long min);
51
+unsigned long light_log_clamp_max(unsigned long max);
52
+
53
+double light_percent_clamp(double percent);
95 54
 
96 55
 #endif /* LIGHT_HELPERS_H */

+ 851
- 933
src/light.c
Failā izmaiņas netiks attēlotas, jo tās ir par lielu
Parādīt failu


+ 78
- 114
src/light.h Parādīt failu

@@ -4,6 +4,7 @@
4 4
 #include "config.h"
5 5
 #include "helpers.h"
6 6
 
7
+#include <stdbool.h>
7 8
 #include <sys/types.h>
8 9
 #include <dirent.h>
9 10
 #include <linux/limits.h>
@@ -11,128 +12,91 @@
11 12
 #define LIGHT_YEAR   "2012-2018"
12 13
 #define LIGHT_AUTHOR "Fredrik Haikarainen"
13 14
 
14
-#define ASSERT_SET(t,v)							\
15
+#define ASSERT_SET(t, v)						\
15 16
 	if (v) {							\
16
-		fprintf(stderr, t" arguments cannot be used in conjunction.\n"); \
17
-		return FALSE;						\
17
+		fprintf(stderr, t " cannot be used more than once.\n"); \
18
+		return false;						\
18 19
 	}								\
19
-	v = TRUE;
20
-
21
-#define ASSERT_OPSET() ASSERT_SET("Operation", opSet)
22
-#define ASSERT_TARGETSET() ASSERT_SET("Target", targetSet)
23
-#define ASSERT_FIELDSET() ASSERT_SET("Field", fieldSet)
24
-#define ASSERT_CTRLSET() ASSERT_SET("Controller", ctrlSet)
25
-#define ASSERT_VALSET() ASSERT_SET("Value", valSet)
26
-
27
-typedef enum LIGHT_FIELD {
28
-  LIGHT_BRIGHTNESS = 0,
29
-  LIGHT_MAX_BRIGHTNESS,
30
-  LIGHT_MIN_CAP,
31
-  LIGHT_SAVERESTORE
32
-} LIGHT_FIELD;
33
-
34
-typedef enum LIGHT_TARGET {
35
-  LIGHT_BACKLIGHT = 0,
36
-  LIGHT_KEYBOARD
37
-} LIGHT_TARGET;
38
-
39
-typedef enum LIGHT_CTRL_MODE {
40
-  LIGHT_AUTO = 0,
41
-  LIGHT_SPECIFY
42
-} LIGHT_CTRL_MODE;
43
-
44
-typedef enum LIGHT_OP_MODE {
45
-  LIGHT_GET = 0,
46
-  LIGHT_SET,
47
-  LIGHT_ADD,
48
-  LIGHT_SUB,
49
-  LIGHT_PRINT_HELP,   /* Prints help and exits  */
50
-  LIGHT_PRINT_VERSION, /* Prints version info and exits */
51
-  LIGHT_LIST_CTRL,
52
-  LIGHT_RESTORE,
53
-  LIGHT_SAVE
54
-
55
-} LIGHT_OP_MODE;
56
-
57
-typedef enum LIGHT_VAL_MODE {
58
-  LIGHT_RAW = 0,
59
-  LIGHT_PERCENT
60
-} LIGHT_VAL_MODE;
61
-
62
-typedef struct light_runtimeArguments_s {
63
-  /* Which controller to use */
64
-  LIGHT_CTRL_MODE controllerMode;
65
-  char            specifiedController[NAME_MAX + 1];
66
-
67
-  /* What to do with the controller */
68
-  LIGHT_OP_MODE   operationMode;
69
-  LIGHT_VAL_MODE  valueMode;
70
-  unsigned long   specifiedValueRaw; /* The specified value in raw mode */
71
-  double          specifiedValuePercent; /* The specified value in percent */
72
-
73
-  LIGHT_TARGET   target;
74
-  LIGHT_FIELD    field;
75
-
76
-  /* Cache data */
77
-  LIGHT_BOOL      hasCachedMaxBrightness;
78
-  unsigned long   cachedMaxBrightness;
79
-
80
-} light_runtimeArguments, *light_runtimeArguments_p;
20
+	v = true;
21
+
22
+#define ASSERT_CMDSET()    ASSERT_SET("Commands", cmd_set)
23
+#define ASSERT_TARGETSET() ASSERT_SET("Targets", target_set)
24
+#define ASSERT_FIELDSET()  ASSERT_SET("Fields", field_set)
25
+#define ASSERT_CTRLSET()   ASSERT_SET("Controllers", ctrl_set)
26
+#define ASSERT_VALSET()    ASSERT_SET("Values", val_set)
27
+
28
+typedef enum {
29
+	LIGHT_BRIGHTNESS = 0,
30
+	LIGHT_MAX_BRIGHTNESS,
31
+	LIGHT_MIN_CAP,
32
+	LIGHT_SAVERESTORE
33
+} light_field_t;
34
+
35
+typedef enum {
36
+	LIGHT_BACKLIGHT = 0,
37
+	LIGHT_KEYBOARD
38
+} light_target_t;
39
+
40
+typedef enum {
41
+	LIGHT_AUTO = 0,
42
+	LIGHT_SPECIFY
43
+} light_ctrl_mode_t;
44
+
45
+typedef enum {
46
+	LIGHT_GET = 0,
47
+	LIGHT_SET,
48
+	LIGHT_ADD,
49
+	LIGHT_SUB,
50
+	LIGHT_PRINT_HELP,	/* Prints help and exits  */
51
+	LIGHT_PRINT_VERSION,	/* Prints version info and exits */
52
+	LIGHT_LIST_CTRL,
53
+	LIGHT_RESTORE,
54
+	LIGHT_SAVE
55
+} light_cmd_t;
56
+
57
+typedef enum {
58
+	LIGHT_RAW = 0,
59
+	LIGHT_PERCENT
60
+} light_val_mode_t;
61
+
62
+typedef struct {
63
+	/* Which controller to use */
64
+	light_ctrl_mode_t ctrl;
65
+	char              ctrl_name[NAME_MAX + 1];
66
+
67
+	/* What to do with the controller */
68
+	light_cmd_t        cmd;
69
+	light_val_mode_t   val_mode;
70
+	unsigned long      val_raw;	/* The specified value in raw mode */
71
+	double             val_percent;	/* The specified value in percent */
72
+
73
+	light_target_t     target;
74
+	light_field_t      field;
75
+
76
+	/* Cache data */
77
+	bool               has_cached_brightness_max;
78
+	unsigned long      cached_brightness_max;
79
+} light_ctx_t;
81 80
 
82 81
 /* -- Global variable holding the settings for the current run -- */
83
-light_runtimeArguments light_Configuration;
82
+light_ctx_t ctx;
84 83
 
85
-/* Sets default values for the configuration */
86
-void light_defaultConfig();
84
+bool light_initialize              (int argc, char **argv);
85
+bool light_execute                 (void);
86
+void light_free                    (void);
87 87
 
88
+bool light_ctrl_list               (void);
89
+bool light_ctrl_probe              (char *controller);
90
+bool light_ctrl_exist              (char const *controller);
88 91
 
89
-/* Parses the program arguments and sets the configuration accordingly (unsanitized) */
90
-LIGHT_BOOL light_parseArguments(int argc, char** argv);
92
+bool light_ctrl_get_brightness     (char const *controller, unsigned long *v);
93
+bool light_ctrl_set_brightness     (char const *controller, unsigned long v);
94
+bool light_ctrl_get_brightness_max (char const *controller, unsigned long *v);
91 95
 
92
-/* Prints a header if verbosity level > 0 */
93
-void light_printVersion(void);
96
+bool light_ctrl_get_cap_min        (char const *controller, bool *hasMinCap, unsigned long *minCap);
97
+bool light_ctrl_set_cap_min        (char const *controller, unsigned long val);
94 98
 
95
-/* Prints help regardless of verbosity level */
96
-void light_printHelp(void);
97
-
98
-/* -- SECTION: Main code -- */
99
-
100
-/* Initializes the application */
101
-LIGHT_BOOL light_initialize(int argc, char** argv);
102
-
103
-/* Does the work */
104
-LIGHT_BOOL light_execute(void);
105
-
106
-/* Frees up resources */
107
-void light_free();
108
-
109
-/* SECTION: Controller functionality */
110
-
111
-/* WARNING: `buffer` HAS to be freed by the user if not null once returned!
112
- * Size is always NAME_MAX + 1 */
113
-LIGHT_BOOL light_genPath(char const *controller, LIGHT_TARGET target, LIGHT_FIELD type, char **buffer);
114
-
115
-LIGHT_BOOL light_validControllerName(char const *controller);
116
-
117
-LIGHT_BOOL light_getBrightness(char const *controller, unsigned long *v);
118
-
119
-LIGHT_BOOL light_getMaxBrightness(char const *controller, unsigned long *v);
120
-
121
-LIGHT_BOOL light_setBrightness(char const *controller, unsigned long v);
122
-
123
-LIGHT_BOOL light_controllerAccessible(char const *controller);
124
-
125
-/* WARNING: `controller` HAS to be at most NAME_MAX, otherwise fails */
126
-LIGHT_BOOL light_getBestController(char *controller);
127
-
128
-LIGHT_BOOL light_getMinCap(char const *controller, LIGHT_BOOL *hasMinCap, unsigned long *minCap);
129
-
130
-LIGHT_BOOL light_setMinCap(char const *controller, unsigned long v);
131
-
132
-LIGHT_BOOL light_listControllers();
133
-
134
-LIGHT_BOOL light_saveBrightness(char const *controller, unsigned long v);
135
-
136
-LIGHT_BOOL light_restoreBrightness(char const *controller);
99
+bool light_ctrl_save_brightness    (char const *controller, unsigned long val);
100
+bool light_ctrl_restore_brightness (char const *controller);
137 101
 
138 102
 #endif /* LIGHT_H_ */

+ 12
- 14
src/main.c Parādīt failu

@@ -6,21 +6,19 @@
6 6
 #define LIGHT_RETURNVAL_EXECFAIL  1
7 7
 #define LIGHT_RETURNVAL_SUCCESS   0
8 8
 
9
-int main(int argc, char** argv)
9
+int main(int argc, char **argv)
10 10
 {
11
-  if(!light_initialize(argc, argv))
12
-  {
13
-    LIGHT_ERR("Initialization failed");
14
-    return LIGHT_RETURNVAL_INITFAIL;
15
-  }
11
+	if (!light_initialize(argc, argv)) {
12
+		LIGHT_ERR("Initialization failed");
13
+		return LIGHT_RETURNVAL_INITFAIL;
14
+	}
16 15
 
17
-  if(!light_execute())
18
-  {
19
-    LIGHT_ERR("Execution failed");
20
-    light_free();
21
-    return LIGHT_RETURNVAL_EXECFAIL;
22
-  }
16
+	if (!light_execute()) {
17
+		LIGHT_ERR("Execution failed");
18
+		light_free();
19
+		return LIGHT_RETURNVAL_EXECFAIL;
20
+	}
23 21
 
24
-  light_free();
25
-  return LIGHT_RETURNVAL_SUCCESS;
22
+	light_free();
23
+	return LIGHT_RETURNVAL_SUCCESS;
26 24
 }