forked from poliva/lightum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigfile.c
162 lines (136 loc) · 3.96 KB
/
configfile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* MacBook automatic keyboard brightness daemon
* Copyright 2011 Pau Oliva Fora <pof@eslack.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "lightum.h"
#define CONFIG_PATH ".config/lightum/"
#define CONFIG_FILE "lightum.conf"
#define TRUE 1
#define FALSE 0
#define MAXLEN 80
int file_exists(char* file) {
struct stat buf;
if (stat(file, &buf) == 0)
return TRUE;
return FALSE;
}
char* default_config_file() {
char* home = getenv("HOME");
char* file = malloc(strlen(home) + strlen(CONFIG_PATH) + strlen(CONFIG_FILE) + 2);
strcpy(file, "");
strcat(file, home);
strcat(file, "/");
strcat(file, CONFIG_PATH);
strcat(file, CONFIG_FILE);
return file;
}
char* default_config_dir() {
char* home = getenv("HOME");
char* path = malloc(strlen(home) + strlen(CONFIG_PATH) + 2);
strcpy(path, "");
strcat(path, home);
strcat(path, "/");
strcat(path, CONFIG_PATH);
return path;
}
int create_config_dir(char* path) {
if (mkdir(path, 0755) != 0)
return FALSE;
return TRUE;
}
int create_config_file(char* file) {
FILE* fd;
char *path;
path = default_config_dir();
create_config_dir(path);
fd = fopen(file, "w");
if (fd == NULL)
return FALSE;
fprintf(fd, "# lightum configuration file\n\n");
fprintf(fd, "# manualmode\n");
fprintf(fd, "# 0 = automatically adjust keyboard brightness based on light sensor\n");
fprintf(fd, "# 1 = or control keyboard brightness manually using Fn+ F5/F6 keys\n");
fprintf(fd, "manualmode=0\n\n");
fprintf(fd, "# maximum keyboard brightness value (between 4 and 255)\n");
fprintf(fd, "maxbrightness=255\n\n");
fprintf(fd, "# minimum keyboard brightness value (between 0 and 3)\n");
fprintf(fd, "minbrightness=0\n\n");
fprintf(fd, "# poll time in milliseconds (used for light sensor and session idle time)\n");
fprintf(fd, "polltime=300\n\n");
fprintf(fd, "# turn off keyboard brightness if computer unused for X seconds (0 to disable).\n");
fprintf(fd, "idleoff=5\n\n");
fprintf(fd, "# screensaver\n");
fprintf(fd, "# 1 = turn off keyboard brightness when screensaver is active\n");
fprintf(fd, "# 0 = do not monitor screensaver status\n");
fprintf(fd, "queryscreensaver=0\n\n");
fclose(fd);
return TRUE;
}
conf_data config_parse() {
char *file;
char input[MAXLEN], temp[MAXLEN];
FILE *fd;
size_t len;
conf_data config;
file = default_config_file();
if (!file_exists(file)) {
if (!create_config_file(file)) {
printf ("failed to create default config file: %s\n", file);
exit(1);
}
}
fd = fopen (file, "r");
if (fd == NULL) {
printf ("Could not open configuration file: %s\n", file);
exit (1);
}
while ((fgets (input, sizeof (input), fd)) != NULL) {
if ((strncmp ("manualmode=", input, 11)) == 0) {
strncpy (temp, input + 11,MAXLEN-1);
len=strlen(temp);
temp[len+1]='\0';
config.manualmode = atoi(temp);
}
if ((strncmp ("queryscreensaver=", input, 17)) == 0) {
strncpy (temp, input + 17,MAXLEN-1);
len=strlen(temp);
temp[len+1]='\0';
config.queryscreensaver = atoi(temp);
}
if ((strncmp ("maxbrightness=", input, 14)) == 0) {
strncpy (temp, input + 14,MAXLEN-1);
len=strlen(temp);
temp[len+1]='\0';
config.maxbrightness = atoi(temp);
}
if ((strncmp ("minbrightness=", input, 14)) == 0) {
strncpy (temp, input + 14,MAXLEN-1);
len=strlen(temp);
temp[len+1]='\0';
config.minbrightness = atoi(temp);
}
if ((strncmp ("polltime=", input, 9)) == 0) {
strncpy (temp, input + 9,MAXLEN-1);
len=strlen(temp);
temp[len+1]='\0';
config.polltime = atoi(temp);
}
if ((strncmp ("idleoff=", input, 8)) == 0) {
strncpy (temp, input + 8,MAXLEN-1);
len=strlen(temp);
temp[len+1]='\0';
config.idleoff = atoi(temp);
}
}
return config;
}