7 #include <sys/utsname.h>
17 #if TIME_WITH_SYS_TIME
18 # include <sys/time.h>
22 # include <sys/time.h>
28 #include "shared/configfile.h"
29 #include "shared/sockets.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 //+--------------------+
51 time_screen(int rep
, int display
, int *flags_ptr
)
56 int days
, hour
, min
, sec
;
57 static int heartbeat
= 0;
58 static const char *timeFormat
= NULL
;
59 static const char *dateFormat
= NULL
;
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");
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());
83 // write title bar: hostname
84 sock_printf(sock
, "widget_set T title {TIME: %s}\n", get_hostname());
88 // toggle colon display
92 rtime
= localtime(&thetime
);
94 if (strftime(today
, sizeof(today
), dateFormat
, rtime
) == 0)
96 if (strftime(now
, sizeof(now
), timeFormat
, rtime
) == 0)
98 tickTime(now
, heartbeat
);
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);
111 sprintf(tmp
, "Up %3d day%s %02d:%02d:%02d",
112 days
, ((days
!= 1) ? "s" : ""), hour
, min
, sec
);
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;
118 sock_printf(sock
, "widget_set T one %i 2 {%s}\n", xoffs
, tmp
);
121 xoffs
= (lcd_wid
> strlen(today
)) ? ((lcd_wid
- strlen(today
)) / 2) + 1 : 1;
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;
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;
135 sock_printf(sock
, "widget_set T one %i 2 {%s %s}\n", xoffs
, today
, now
);
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,| +--------------------+
149 //+--------------------+
152 clock_screen(int rep
, int display
, int *flags_ptr
)
157 static int heartbeat
= 0;
158 static const char *timeFormat
= NULL
;
159 static const char *dateFormat
= NULL
;
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");
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
);
186 sock_printf(sock
, "widget_set O title {TIME: %s}\n", get_hostname());
190 // toggle colon display
194 rtime
= localtime(&thetime
);
196 if (strftime(today
, sizeof(today
), dateFormat
, rtime
) == 0)
198 if (strftime(now
, sizeof(now
), timeFormat
, rtime
) == 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;
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;
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;
215 sock_printf(sock
, "widget_set O one %i 2 {%s %s}\n", xoffs
, today
, now
);
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 | +--------------------+
229 //+--------------------+
232 uptime_screen(int rep
, int display
, int *flags_ptr
)
235 int days
, hour
, min
, sec
;
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");
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
);
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
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);
277 sprintf(tmp
, "%d day%s %02d:%02d:%02d",
278 days
, ((days
!= 1) ? "s" : ""), hour
, min
, sec
);
280 sprintf(tmp
, "%dd %02d:%02d:%02d", days
, hour
, min
, sec
);
283 xoffs
= (lcd_wid
> strlen(tmp
)) ? (((lcd_wid
- strlen(tmp
)) / 2) + 1) : 1;
285 sock_printf(sock
, "widget_set U two %d 3 {%s}\n", xoffs
, tmp
);
287 sock_printf(sock
, "widget_set U one %d 2 {%s}\n", xoffs
, tmp
);
291 } // End uptime_screen()
293 //////////////////////////////////////////////////////////////////////
294 // Big Clock Screen displays current time...
296 // +--------------------+
298 // | ||_ . _||_|. _| ||
299 // | ||_|. _| |.|_ ||
301 // +--------------------+
304 big_clock_screen(int rep
, int display
, int *flags_ptr
)
308 int pos
[] = { 1, 4, 8, 11, 15, 18 };
310 static char old_fulltxt
[16];
311 static int heartbeat
= 0;
312 static int TwentyFourHour
= 1;
314 int digits
= (lcd_wid
>= 20) ? 6 : 4;
315 int xoffs
= (lcd_wid
+ 1 - (pos
[digits
-1] + 2)) / 2;
317 // toggle colon display
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");
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
, " ");
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);
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);
362 sock_printf(sock
, "widget_set K c1 %d 11\n", xoffs
+ 14);
366 } // End big_clock_screen()
369 /////////////////////////////////////////////////////////////////////
370 // MiniClock Screen displays the current time with hours & minutes only
372 //+--------------------+ +--------------------+
375 //| | +--------------------+
377 //+--------------------+
380 mini_clock_screen(int rep
, int display
, int *flags_ptr
)
385 static const char *timeFormat
= NULL
;
386 static int heartbeat
= 0;
389 // toggle colon display
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");
404 rtime
= localtime(&thetime
);
406 if (strftime(now
, sizeof(now
), timeFormat
, rtime
) == 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
);
414 } // End mini_clock_screen()
417 // helper function: toggle between ':' and ' ' in time strings
418 static char *tickTime(char *time
, int heartbeat
)
421 static char colon
[] = {':', ' '};
424 for (heartbeat
%= 2; *ptr
!= '\0'; ptr
++) {
425 if (*ptr
== colon
[0])
426 *ptr
= colon
[heartbeat
];