1 | #include <stdio.h> |
---|
2 | #include <unistd.h> |
---|
3 | #include <string.h> |
---|
4 | #include <fcntl.h> |
---|
5 | #include <sys/time.h> |
---|
6 | #include <termios.h> |
---|
7 | #include <errno.h> |
---|
8 | #include <stdlib.h> |
---|
9 | #include <signal.h> |
---|
10 | #include <ctype.h> |
---|
11 | |
---|
12 | #include "main.h" |
---|
13 | #include "lcd.h" |
---|
14 | #include "mode.h" |
---|
15 | //#include "lock.h" |
---|
16 | #include "sockets.h" |
---|
17 | |
---|
18 | |
---|
19 | // TODO: Commenting... Everything! |
---|
20 | |
---|
21 | |
---|
22 | char version[] = "v0.3.4"; |
---|
23 | char build_date[] = "1998-06-20"; |
---|
24 | int Quit = 0; |
---|
25 | |
---|
26 | /* |
---|
27 | Mode List: |
---|
28 | See below... (default_sequence[]) |
---|
29 | */ |
---|
30 | |
---|
31 | typedef struct mode { |
---|
32 | char which; |
---|
33 | int num_times; |
---|
34 | int delay_time; |
---|
35 | } mode; |
---|
36 | |
---|
37 | void HelpScreen(); |
---|
38 | void exit_program(int val); |
---|
39 | void main_loop(mode *sequence); |
---|
40 | |
---|
41 | #define MAX_SEQUENCE 256 |
---|
42 | // 1/8th second is a single time unit... |
---|
43 | #define TIME_UNIT 125000 |
---|
44 | |
---|
45 | |
---|
46 | // Contains a list of modes to run |
---|
47 | mode default_sequence[] = |
---|
48 | { |
---|
49 | { 'C', 32, 1, },// [C]PU |
---|
50 | { 'M', 8, 4, },// [M]emory |
---|
51 | { 'X', 1, 32, },// [X]-load (load histogram) |
---|
52 | { 'T', 8, 4, },// [T]ime/Date |
---|
53 | { 'D', 32, 1, },// [D]isk stats |
---|
54 | { 'A', 1, 16, },// [A]bout (credits) |
---|
55 | |
---|
56 | { 1 , 0, 0, },// Modes after this line will not be run by default... |
---|
57 | // ... all non-default modes must be in here! |
---|
58 | // ... they will not show up otherwise. |
---|
59 | { 'O', 8, 4, },// [O]ld Timescreen |
---|
60 | { 'U', 8, 4, },// Old [U]ptime Screen |
---|
61 | { 'B', 32, 1, },// [B]attery Status |
---|
62 | { 'G', 32, 1, },// Cpu histogram [G]raph |
---|
63 | { 0, 0, 0, },// No more.. all done. |
---|
64 | }; |
---|
65 | |
---|
66 | // TODO: Clean up main()... It is still way too big. |
---|
67 | |
---|
68 | // TODO: Socket language, client handling... |
---|
69 | // TODO: Config file; not just command line |
---|
70 | |
---|
71 | |
---|
72 | int main(int argc, char **argv) |
---|
73 | { |
---|
74 | char device[256] = "/dev/lcd"; |
---|
75 | char cfgfile[256] = "/etc/lcdproc.cf"; |
---|
76 | mode sequence[MAX_SEQUENCE]; |
---|
77 | char driver[256] = "MtxOrb"; |
---|
78 | int i, j, k; |
---|
79 | int already_running = 0; |
---|
80 | int contrast = 140; |
---|
81 | int tmp; |
---|
82 | |
---|
83 | memset(sequence, 0, sizeof(mode) * MAX_SEQUENCE); |
---|
84 | |
---|
85 | // Ctrl-C will cause a clean exit... |
---|
86 | signal(SIGINT, exit_program); |
---|
87 | // and "kill"... |
---|
88 | signal(SIGTERM, exit_program); |
---|
89 | // and "kill -HUP" (hangup)... |
---|
90 | signal(SIGHUP, exit_program); |
---|
91 | // and just in case, "kill -KILL" (which cannot be trapped; but oh well) |
---|
92 | signal(SIGKILL, exit_program); |
---|
93 | |
---|
94 | // Check to see if we are already running... |
---|
95 | // already_running = CheckForLock(); |
---|
96 | already_running = PingLCDport(); |
---|
97 | if(already_running < 0) |
---|
98 | { |
---|
99 | printf("Error checking for another LCDproc.\n"); |
---|
100 | return -1; |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | // Communicate with the previously running LCDproc |
---|
105 | if(already_running > 0) |
---|
106 | { |
---|
107 | // "already_running" holds the pid of the LCDproc we want to talk to... |
---|
108 | |
---|
109 | // Do the command line ("lcd pet status", or "lcd contrast 50") |
---|
110 | // And stuff... |
---|
111 | // ...Then exit |
---|
112 | printf("Detected another LCDproc. (%i) Exiting...\n", already_running); |
---|
113 | // Remove me ^^^^ |
---|
114 | return 0; |
---|
115 | } |
---|
116 | |
---|
117 | tmp = StartSocketServer(); |
---|
118 | if (tmp <= 0) |
---|
119 | { |
---|
120 | printf("Error starting socket server.\n"); |
---|
121 | return 0; |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | |
---|
126 | // Command line |
---|
127 | memcpy(sequence, default_sequence, sizeof(default_sequence)); |
---|
128 | for(i=1, j=0; i<argc; i++) |
---|
129 | { |
---|
130 | if(argv[i][0] == '-') switch(argv[i][1]) |
---|
131 | { |
---|
132 | // "C is for cookie (erm, contrast), and that is good enough for me..." |
---|
133 | case 'C': |
---|
134 | case 'c': if(argc < i+1) HelpScreen(); |
---|
135 | contrast = atoi(argv[++i]); |
---|
136 | if(contrast <= 0) HelpScreen(); |
---|
137 | break; |
---|
138 | // D for Device... |
---|
139 | case 'D': |
---|
140 | case 'd': if(argc < i+1) HelpScreen(); |
---|
141 | strcpy(device, argv[++i]); |
---|
142 | break; |
---|
143 | case 'L': |
---|
144 | case 'l': if(argc < i+1) HelpScreen(); |
---|
145 | strcpy(driver, argv[++i]); |
---|
146 | break; |
---|
147 | |
---|
148 | // otherwise... Get help! |
---|
149 | default: HelpScreen(); break; |
---|
150 | } |
---|
151 | // Parse command line here... read the man page. |
---|
152 | else if(strlen(argv[i]) == 1) |
---|
153 | { |
---|
154 | // Grab the mode letter... |
---|
155 | sequence[j].which = argv[i][0]; |
---|
156 | |
---|
157 | // If we have just a letter with no numbers... |
---|
158 | // Set the defaults... |
---|
159 | for(tmp=0, k=0; default_sequence[k].which; k++) |
---|
160 | { |
---|
161 | if(toupper(sequence[j].which) == default_sequence[k].which) |
---|
162 | { |
---|
163 | memcpy(&sequence[j], &default_sequence[k], sizeof(mode)); |
---|
164 | tmp=1; |
---|
165 | break; |
---|
166 | } |
---|
167 | } |
---|
168 | if(!tmp) { printf("Invalid Mode: %c\n", argv[i][0]); exit(0); } |
---|
169 | |
---|
170 | j++; |
---|
171 | // Set the last element to 0... |
---|
172 | memset(sequence + j, 0, sizeof(mode)); |
---|
173 | } // End if(strlen(argv == 1)) |
---|
174 | else |
---|
175 | { |
---|
176 | // A multicharacter parameter by itself is assumed to be a config file.. |
---|
177 | strcpy(cfgfile, argv[i]); |
---|
178 | printf("Ignoring config file: %s\n", cfgfile); |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | // Init the com port |
---|
183 | if(lcd_init(device, driver) < 1) |
---|
184 | { |
---|
185 | printf("Cannot initialize %s.\n", device); |
---|
186 | exit(1); |
---|
187 | } |
---|
188 | mode_init(); |
---|
189 | lcd.contrast(contrast); |
---|
190 | |
---|
191 | main_loop(sequence); |
---|
192 | |
---|
193 | // Clean up |
---|
194 | exit_program(0); |
---|
195 | return 0; |
---|
196 | } |
---|
197 | |
---|
198 | |
---|
199 | void HelpScreen() |
---|
200 | { |
---|
201 | printf("LCDproc, %s\n", version); |
---|
202 | printf("Usage: lcdproc [-d device] [-c contrast] [modelist]\n"); |
---|
203 | printf("\tOptions in []'s are optional.\n"); |
---|
204 | printf("\t-l driver is the output driver to use:\n"); |
---|
205 | printf("\t\tMtxOrb, curses, text, debug\n"); |
---|
206 | printf("\t-d device is what the lcd display is hooked to. (/dev/cua0?)\n"); |
---|
207 | printf("\t-c contrast sets the screen contrast (0 - 255)\n"); |
---|
208 | printf("\tmodelist is \"mode [mode mode ...]\"\n"); |
---|
209 | printf("\tMode letters: [C]pu [G]raph [T]ime [M]emory [X]load [D]isk [B]attery [O]ld Time screen [U]ptime [A]bout\n"); |
---|
210 | printf("\n"); |
---|
211 | printf("\tUse \"man lcdproc\" for more info.\n"); |
---|
212 | printf("Example:\n"); |
---|
213 | printf("\tlcdproc -d /dev/cua1 C M X -l MtxOrb\n"); |
---|
214 | printf("\n"); |
---|
215 | exit(0); |
---|
216 | } |
---|
217 | |
---|
218 | |
---|
219 | /////////////////////////////////////////////////////////////////// |
---|
220 | // Called upon TERM and INTR signals... |
---|
221 | // |
---|
222 | void exit_program(int val) |
---|
223 | { |
---|
224 | Quit = 1; |
---|
225 | //unlock(); // Not needed any more... |
---|
226 | CloseAllConnections(); |
---|
227 | goodbye_screen(0); |
---|
228 | lcd.flush(); |
---|
229 | lcd.backlight(1); |
---|
230 | lcd.close(); |
---|
231 | mode_close(); |
---|
232 | exit(0); |
---|
233 | } |
---|
234 | |
---|
235 | |
---|
236 | /////////////////////////////////////////////////////////////////// |
---|
237 | // Main program loop... |
---|
238 | // |
---|
239 | void main_loop(mode *sequence) |
---|
240 | { |
---|
241 | int i, j, k; |
---|
242 | int Quit=0; |
---|
243 | int status=0; |
---|
244 | int timer=0; |
---|
245 | |
---|
246 | int blink=0; |
---|
247 | int hold=0; |
---|
248 | int heartbeat=1; |
---|
249 | |
---|
250 | // Main loop |
---|
251 | // Run whatever screen we want, then wait. Woo-hoo! |
---|
252 | for(i=0; !Quit; ) |
---|
253 | { |
---|
254 | timer=0; |
---|
255 | for(j=0; hold || (j<sequence[i].num_times && !Quit); j++) |
---|
256 | { |
---|
257 | switch(sequence[i].which) |
---|
258 | { |
---|
259 | case 'g': |
---|
260 | case 'G': status = cpu_graph_screen(j); break; |
---|
261 | case 'c': |
---|
262 | case 'C': status = cpu_screen(j); break; |
---|
263 | case 'o': |
---|
264 | case 'O': status = clock_screen(j); break; |
---|
265 | case 'm': |
---|
266 | case 'M': status = mem_screen(j); break; |
---|
267 | case 'u': |
---|
268 | case 'U': status = uptime_screen(j); break; |
---|
269 | case 't': |
---|
270 | case 'T': status = time_screen(j); break; |
---|
271 | case 'd': |
---|
272 | case 'D': status = disk_screen(j); break; |
---|
273 | case 'x': |
---|
274 | case 'X': status = xload_screen(j); break; |
---|
275 | case 'b': |
---|
276 | case 'B': status = battery_screen(j); break; |
---|
277 | case 'a': |
---|
278 | case 'A': status = credit_screen(j); break; |
---|
279 | default: status = dumbass_screen(j); break; |
---|
280 | } |
---|
281 | |
---|
282 | |
---|
283 | |
---|
284 | |
---|
285 | for(k=0; k<sequence[i].delay_time; k++) |
---|
286 | { |
---|
287 | usleep(TIME_UNIT); timer++; |
---|
288 | // Modify lcd status here... (blinking, for example) |
---|
289 | switch(status) |
---|
290 | { |
---|
291 | case BLINK_ON : blink=1; break; |
---|
292 | case BLINK_OFF: blink=0; lcd.backlight(1); break; |
---|
293 | case BACKLIGHT_OFF: blink=0; lcd.backlight(0); break; |
---|
294 | case BACKLIGHT_ON : blink=0; lcd.backlight(1); break; |
---|
295 | case HOLD_SCREEN : hold=1; break; |
---|
296 | case CONTINUE : hold=0; break; |
---|
297 | } |
---|
298 | status=0; |
---|
299 | |
---|
300 | if(blink) lcd.backlight(! ((timer&15) == 15)); |
---|
301 | |
---|
302 | if(heartbeat) |
---|
303 | { |
---|
304 | // Set this to pulsate like a real heart beat... |
---|
305 | // (binary is fun... :) |
---|
306 | lcd.icon(!((timer+4)&5), 0); |
---|
307 | lcd.chr(lcd.wid, 1, 0); |
---|
308 | } |
---|
309 | |
---|
310 | lcd.flush(); |
---|
311 | |
---|
312 | PollSockets(); |
---|
313 | } |
---|
314 | } |
---|
315 | i++; |
---|
316 | if(sequence[i].which < 2) i=0; |
---|
317 | } |
---|
318 | } |
---|