Base: LCDproc 0.5.2
[lcdproc-de200c.git] / clients / lcdproc / chrono.c
blobbbc10ee77cd03ec5cabd04066368194585c3cbb9
1 #include <sys/types.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7 #include <sys/utsname.h>
8 #include <limits.h>
9 #include <sys/param.h>
10 #include <errno.h>
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
17 #if TIME_WITH_SYS_TIME
18 # include <sys/time.h>
19 # include <time.h>
20 #else
21 # if HAVE_SYS_TIME_H
22 # include <sys/time.h>
23 # else
24 # include <time.h>
25 # endif
26 #endif
28 #include "shared/configfile.h"
29 #include "shared/sockets.h"
31 #include "main.h"
32 #include "mode.h"
33 #include "machine.h"
34 #include "chrono.h"
37 static char *tickTime(char *time, int heartbeat);
40 //////////////////////////////////////////////////////////////////////
41 // TimeDate Screen displays current time and date, uptime, OS ver...
43 //+--------------------+ +--------------------+
44 //|## Linux 2.6.11 ###@| |### TIME: myhost ##@|
45 //|Up xxx days hh:mm:ss| |17.05.2005 11:32:57a|
46 //| Wed May 17, 1998 | +--------------------+
47 //|11:32:57a 100% idle|
48 //+--------------------+
50 int
51 time_screen(int rep, int display, int *flags_ptr)
53 char now[40];
54 char today[40];
55 int xoffs;
56 int days, hour, min, sec;
57 static int heartbeat = 0;
58 static const char *timeFormat = NULL;
59 static const char *dateFormat = NULL;
60 time_t thetime;
61 struct tm *rtime;
62 double uptime, idle;
64 if ((*flags_ptr & INITIALIZED) == 0) {
65 *flags_ptr |= INITIALIZED;
67 /* get config values */
68 timeFormat = config_get_string("TimeDate", "TimeFormat", 0, "%H:%M:%S");
69 dateFormat = config_get_string("TimeDate", "DateFormat", 0, "%b %d %Y");
71 sock_send_string(sock, "screen_add T\n");
72 sock_printf(sock, "screen_set T -name {Time Screen: %s}\n", get_hostname());
73 sock_send_string(sock, "widget_add T title title\n");
74 sock_send_string(sock, "widget_add T one string\n");
75 if (lcd_hgt >= 4) {
76 sock_send_string(sock, "widget_add T two string\n");
77 sock_send_string(sock, "widget_add T three string\n");
79 // write title bar: OS name, OS version, hostname
80 sock_printf(sock, "widget_set T title {%s %s: %s}\n",
81 get_sysname(), get_sysrelease(), get_hostname());
82 } else {
83 // write title bar: hostname
84 sock_printf(sock, "widget_set T title {TIME: %s}\n", get_hostname());
88 // toggle colon display
89 heartbeat ^= 1;
91 time(&thetime);
92 rtime = localtime(&thetime);
94 if (strftime(today, sizeof(today), dateFormat, rtime) == 0)
95 *today = '\0';
96 if (strftime(now, sizeof(now), timeFormat, rtime) == 0)
97 *now = '\0';
98 tickTime(now, heartbeat);
100 if (lcd_hgt >= 4) {
101 machine_get_uptime(&uptime, &idle);
102 char tmp[40]; // should be large enough
104 // display the uptime...
105 days = (int) uptime / 86400;
106 hour = ((int) uptime % 86400) / 3600;
107 min = ((int) uptime % 3600) / 60;
108 sec = ((int) uptime % 60);
110 if (lcd_wid >= 20)
111 sprintf(tmp, "Up %3d day%s %02d:%02d:%02d",
112 days, ((days != 1) ? "s" : ""), hour, min, sec);
113 else
114 sprintf(tmp, "Up %dd %02d:%02d:%02d", days, hour, min, sec);
116 xoffs = (lcd_wid > strlen(tmp)) ? ((lcd_wid - strlen(tmp)) / 2) + 1 : 1;
117 if (display)
118 sock_printf(sock, "widget_set T one %i 2 {%s}\n", xoffs, tmp);
120 // display the date
121 xoffs = (lcd_wid > strlen(today)) ? ((lcd_wid - strlen(today)) / 2) + 1 : 1;
122 if (display)
123 sock_printf(sock, "widget_set T two %i 3 {%s}\n", xoffs, today);
125 // display the time & idle time...
126 sprintf(tmp, "%s %3i%% idle", now, (int) idle);
127 xoffs = (lcd_wid > strlen(tmp)) ? ((lcd_wid - strlen(tmp)) / 2) + 1 : 1;
128 if (display)
129 sock_printf(sock, "widget_set T three %i 4 {%s}\n", xoffs, tmp);
131 else { // 2 line version of the screen
132 xoffs = (lcd_wid > (strlen(today) + strlen(now) + 1))
133 ? ((lcd_wid - ((strlen(today) + strlen(now) + 1))) / 2) + 1 : 1;
134 if (display)
135 sock_printf(sock, "widget_set T one %i 2 {%s %s}\n", xoffs, today, now);
138 return 0;
139 } // End time_screen()
141 //////////////////////////////////////////////////////////////////////
142 // OldTime Screen displays current time and date...
144 //+--------------------+ +--------------------+
145 //|## DATE & TIME ####@| |### TIME: myhost ##@|
146 //| myhost | |2005-05-17 11:32:57a|
147 //|11:32:75a Wednesday,| +--------------------+
148 //| May 17, 2005 |
149 //+--------------------+
152 clock_screen(int rep, int display, int *flags_ptr)
154 char now[40];
155 char today[40];
156 int xoffs;
157 static int heartbeat = 0;
158 static const char *timeFormat = NULL;
159 static const char *dateFormat = NULL;
160 time_t thetime;
161 struct tm *rtime;
163 if ((*flags_ptr & INITIALIZED) == 0) {
164 char tmp[257]; // should be large enough for host name
166 *flags_ptr |= INITIALIZED;
168 /* get config values */
169 timeFormat = config_get_string("OldTime", "TimeFormat", 0, "%H:%M:%S");
170 dateFormat = config_get_string("OldTime", "DateFormat", 0, "%b %d %Y");
172 sock_send_string(sock, "screen_add O\n");
173 sock_printf(sock, "screen_set O -name {Old Clock Screen: %s}\n", get_hostname());
174 sock_send_string(sock, "widget_add O title title\n");
175 sock_send_string(sock, "widget_add O one string\n");
176 if (lcd_hgt >= 4) {
177 sock_send_string(sock, "widget_add O two string\n");
178 sock_send_string(sock, "widget_add O three string\n");
180 sock_printf(sock, "widget_set O title {DATE & TIME}\n");
182 sprintf(tmp, "%s", get_hostname());
183 xoffs = (lcd_wid > strlen(tmp)) ? (((lcd_wid - strlen(tmp)) / 2) + 1) : 1;
184 sock_printf(sock, "widget_set O one %i 2 {%s}\n", xoffs, tmp);
185 } else {
186 sock_printf(sock, "widget_set O title {TIME: %s}\n", get_hostname());
190 // toggle colon display
191 heartbeat ^= 1;
193 time(&thetime);
194 rtime = localtime(&thetime);
196 if (strftime(today, sizeof(today), dateFormat, rtime) == 0)
197 *today = '\0';
198 if (strftime(now, sizeof(now), timeFormat, rtime) == 0)
199 *now = '\0';
200 tickTime(now, heartbeat);
202 if (lcd_hgt >= 4) { // 4-line version of the screen
203 xoffs = (lcd_wid > strlen(today)) ? ((lcd_wid - strlen(today)) / 2) + 1 : 1;
204 if (display)
205 sock_printf(sock, "widget_set O two %i 3 {%s}\n", xoffs, today);
207 xoffs = (lcd_wid > strlen(now)) ? ((lcd_wid - strlen(now)) / 2) + 1 : 1;
208 if (display)
209 sock_printf(sock, "widget_set O three %i 4 {%s}\n", xoffs, now);
211 else { // 2-line version of the screen
212 xoffs = (lcd_wid > (strlen(today) + strlen(now) + 1))
213 ? ((lcd_wid - ((strlen(today) + strlen(now) + 1))) / 2) + 1 : 1;
214 if (display)
215 sock_printf(sock, "widget_set O one %i 2 {%s %s}\n", xoffs, today, now);
218 return 0;
219 } // End clock_screen()
221 ////////////////////////////////////////////////////////////////////
222 // Uptime Screen shows info about system uptime and OS version
224 //+--------------------+ +--------------------+
225 //|## SYSTEM UPTIME ##@| |# Linux 2.6.11: my#@|
226 //| myhost | | xxx days hh:mm:ss |
227 //| xxx days hh:mm:ss | +--------------------+
228 //| Linux 2.6.11 |
229 //+--------------------+
232 uptime_screen(int rep, int display, int *flags_ptr)
234 int xoffs;
235 int days, hour, min, sec;
236 double uptime, idle;
237 static int heartbeat = 0;
238 char tmp[257]; // should be large enough for host name
240 if ((*flags_ptr & INITIALIZED) == 0) {
241 *flags_ptr |= INITIALIZED;
243 sock_send_string(sock, "screen_add U\n");
244 sock_printf(sock, "screen_set U -name {Uptime Screen: %s}\n", get_hostname());
245 sock_send_string(sock, "widget_add U title title\n");
246 if (lcd_hgt >= 4) {
247 sock_send_string(sock, "widget_add U one string\n");
248 sock_send_string(sock, "widget_add U two string\n");
249 sock_send_string(sock, "widget_add U three string\n");
251 sock_send_string(sock, "widget_set U title {SYSTEM UPTIME}\n");
253 sprintf(tmp, "%s", get_hostname());
254 xoffs = (lcd_wid > strlen(tmp)) ? (((lcd_wid - strlen(tmp)) / 2) + 1) : 1;
255 sock_printf(sock, "widget_set U one %i 2 {%s}\n", xoffs, tmp);
257 sprintf(tmp, "%s %s", get_sysname(), get_sysrelease());
258 xoffs = (lcd_wid > strlen(tmp)) ? (((lcd_wid - strlen(tmp)) / 2) + 1) : 1;
259 sock_printf(sock, "widget_set U three %i 4 {%s}\n", xoffs, tmp);
260 } else {
261 sock_send_string(sock, "widget_add U one string\n");
263 sock_printf(sock, "widget_set U title {%s %s: %s}\n",
264 get_sysname(), get_sysrelease(), get_hostname());
268 // toggle colon display
269 heartbeat ^= 1;
271 machine_get_uptime(&uptime, &idle);
272 days = (int) uptime / 86400;
273 hour = ((int) uptime % 86400) / 3600;
274 min = ((int) uptime % 3600) / 60;
275 sec = ((int) uptime % 60);
276 if (lcd_wid >= 20)
277 sprintf(tmp, "%d day%s %02d:%02d:%02d",
278 days, ((days != 1) ? "s" : ""), hour, min, sec);
279 else
280 sprintf(tmp, "%dd %02d:%02d:%02d", days, hour, min, sec);
282 if (display) {
283 xoffs = (lcd_wid > strlen(tmp)) ? (((lcd_wid - strlen(tmp)) / 2) + 1) : 1;
284 if (lcd_hgt >= 4)
285 sock_printf(sock, "widget_set U two %d 3 {%s}\n", xoffs, tmp);
286 else
287 sock_printf(sock, "widget_set U one %d 2 {%s}\n", xoffs, tmp);
290 return 0;
291 } // End uptime_screen()
293 //////////////////////////////////////////////////////////////////////
294 // Big Clock Screen displays current time...
296 // +--------------------+
297 // | _ _ _ _ |
298 // | ||_ . _||_|. _| ||
299 // | ||_|. _| |.|_ ||
300 // | |
301 // +--------------------+
304 big_clock_screen(int rep, int display, int *flags_ptr)
306 time_t thetime;
307 struct tm *rtime;
308 int pos[] = { 1, 4, 8, 11, 15, 18 };
309 char fulltxt[16];
310 static char old_fulltxt[16];
311 static int heartbeat = 0;
312 static int TwentyFourHour = 1;
313 int j = 0;
314 int digits = (lcd_wid >= 20) ? 6 : 4;
315 int xoffs = (lcd_wid + 1 - (pos[digits-1] + 2)) / 2;
317 // toggle colon display
318 heartbeat ^= 1;
320 if ((*flags_ptr & INITIALIZED) == 0) {
321 *flags_ptr |= INITIALIZED;
323 sock_send_string(sock, "screen_add K\n");
324 sock_send_string(sock, "screen_set K -name {Big Clock Screen} -heartbeat off\n");
325 sock_send_string(sock, "widget_add K d0 num\n");
326 sock_send_string(sock, "widget_add K d1 num\n");
327 sock_send_string(sock, "widget_add K d2 num\n");
328 sock_send_string(sock, "widget_add K d3 num\n");
329 sock_send_string(sock, "widget_add K c0 num\n");
331 if (digits > 4) {
332 sock_send_string(sock, "widget_add K d4 num\n");
333 sock_send_string(sock, "widget_add K d5 num\n");
334 sock_send_string(sock, "widget_add K c1 num\n");
337 strcpy(old_fulltxt, " ");
340 time(&thetime);
341 rtime = localtime(&thetime);
343 sprintf(fulltxt, "%02d%02d%02d",
344 ((TwentyFourHour) ? rtime->tm_hour : (((rtime->tm_hour + 11) % 12) + 1)),
345 rtime->tm_min, rtime->tm_sec);
347 for (j = 0; j < digits; j++) {
348 if (fulltxt[j] != old_fulltxt[j]) {
349 sock_printf(sock, "widget_set K d%d %d %c\n", j, xoffs+pos[j], fulltxt[j]);
350 old_fulltxt[j] = fulltxt[j];
354 if (heartbeat) { // 10 means: colon
355 sock_printf(sock, "widget_set K c0 %d 10\n", xoffs + 7);
356 if (digits > 4)
357 sock_printf(sock, "widget_set K c1 %d 10\n", xoffs + 14);
359 else { // kludge: use illegal number to clear colon display
360 sock_printf(sock, "widget_set K c0 %d 11\n", xoffs + 7);
361 if (digits > 4)
362 sock_printf(sock, "widget_set K c1 %d 11\n", xoffs + 14);
365 return 0;
366 } // End big_clock_screen()
369 /////////////////////////////////////////////////////////////////////
370 // MiniClock Screen displays the current time with hours & minutes only
372 //+--------------------+ +--------------------+
373 //| | | 11:32 |
374 //| 11:32 | | |
375 //| | +--------------------+
376 //| |
377 //+--------------------+
380 mini_clock_screen(int rep, int display, int *flags_ptr)
382 char now[40];
383 time_t thetime;
384 struct tm *rtime;
385 static const char *timeFormat = NULL;
386 static int heartbeat = 0;
387 int xoffs;
389 // toggle colon display
390 heartbeat ^= 1;
392 if ((*flags_ptr & INITIALIZED) == 0) {
393 *flags_ptr |= INITIALIZED;
395 /* get config values */
396 timeFormat = config_get_string("MiniClock", "TimeFormat", 0, "%H:%M");
398 sock_send_string(sock, "screen_add N\n");
399 sock_send_string(sock, "screen_set N -name {Mini Clock Screen} -heartbeat off\n");
400 sock_send_string(sock, "widget_add N one string\n");
403 time(&thetime);
404 rtime = localtime(&thetime);
406 if (strftime(now, sizeof(now), timeFormat, rtime) == 0)
407 *now = '\0';
408 tickTime(now, heartbeat);
410 xoffs = (lcd_wid > strlen(now)) ? (((lcd_wid - strlen(now)) / 2) + 1) : 1;
411 sock_printf(sock, "widget_set N one %d %d {%s}\n", xoffs, (lcd_hgt / 2), now);
413 return 0;
414 } // End mini_clock_screen()
417 // helper function: toggle between ':' and ' ' in time strings
418 static char *tickTime(char *time, int heartbeat)
420 if (time != NULL) {
421 static char colon[] = {':', ' '};
422 char *ptr = time;
424 for (heartbeat %= 2; *ptr != '\0'; ptr++) {
425 if (*ptr == colon[0])
426 *ptr = colon[heartbeat];
429 return(time);