source: npl/mediabox/lcdproc_edwin/src/clients/lcdproc/chrono.c @ c5c522c

gcc484ntopperl-5.22
Last change on this file since c5c522c was c5c522c, checked in by Edwin Eefting <edwin@datux.nl>, 8 years ago

initial commit, transferred from cleaned syn3 svn tree

  • Property mode set to 100644
File size: 13.2 KB
Line 
1#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
5#include <fcntl.h>
6#include <sys/utsname.h>
7#include <sys/time.h>
8
9#include "../../shared/sockets.h"
10
11#include "main.h"
12#include "mode.h"
13#include "chrono.h"
14
15int TwentyFourHour=1;
16int uptime_fd = 0;
17
18static char kver[SYS_NMLN];
19static char sysname[SYS_NMLN];
20
21static double get_uptime(double *up, double *idle);
22
23
24static double get_uptime(double *up, double *idle)
25{
26  double uptime;
27
28  reread(uptime_fd, "get_uptime:");
29  sscanf(buffer, "%lf %lf", &uptime, idle);
30
31  *up = uptime;
32 
33  return uptime;
34}
35
36
37// A couple of tables for later use...
38static char *days[] = {
39  "Sunday,   ",
40  "Monday,   ",
41  "Tuesday,  ",
42  "Wednesday,",
43  "Thursday, ",
44  "Friday,   ",
45  "Saturday, ",
46};
47static char *shortdays[] = {
48  "Sun",
49  "Mon",
50  "Tue",
51  "Wed",
52  "Thu",
53  "Fri",
54  "Sat",
55};
56
57static char *months[] = {
58  "  January",
59  " February",
60  "    March",
61  "    April",
62  "      May",
63  "     June",
64  "     July",
65  "   August",
66  "September",
67  "  October",
68  " November",
69  " December",
70};
71static char *shortmonths[] = {
72  "Jan",
73  "Feb",
74  "Mar",
75  "Apr",
76  "May",
77  "Jun",
78  "Jul",
79  "Aug",
80  "Sep",
81  "Oct",
82  "Nov",
83  "Dec",
84};
85
86
87
88int chrono_init()
89{
90   struct utsname *unamebuf;
91   
92   if(!uptime_fd)
93   {
94      unamebuf = (struct utsname *) malloc( sizeof(struct utsname) );
95      uptime_fd = open("/proc/uptime",O_RDONLY);
96   
97#if 0
98      kversion_fd = open("/proc/sys/kernel/osrelease",O_RDONLY);
99     
100      reread(kversion_fd, "main:");
101      sscanf(buffer, "%s", kver);
102     
103      close(kversion_fd);
104# endif
105   
106      /* Get OS name and version from uname() */
107      if( uname( unamebuf ) != 0 ) {
108         perror( "Error calling uname:" );
109      }
110      strcpy( kver, unamebuf->release );
111      strcpy( sysname, unamebuf->sysname );
112     
113      free(unamebuf);
114
115
116     
117   }
118   
119   return 0;
120}
121
122
123int chrono_close()
124{
125   if(uptime_fd)
126      close(uptime_fd);
127
128   uptime_fd = 0;
129
130   return 0;
131}
132
133
134
135//////////////////////////////////////////////////////////////////////
136// Time Screen displays current time and date, uptime, OS ver...
137//
138//+--------------------+
139//|## Linux 2.0.33 ###@|
140//|Up xxx days hh:mm:ss|
141//|  Wed May 17, 1998  |
142//|11:32:57a  100% idle|
143//+--------------------+
144//
145
146int time_screen(int rep, int display)
147{
148   char hr[8], min[8], sec[8], ampm[8];
149   char day[16], month[16];
150   static int first = 1;
151   static int colons = 0;
152   time_t thetime;
153   struct tm *rtime;
154   double uptime, idle;
155   int i = 0;
156   char colon;
157
158
159   
160   if(first)
161   {
162      first = 0;
163
164      sock_send_string(sock, "screen_add T\n");
165      sprintf(buffer, "screen_set T name {Time Screen: %s}\n", host);
166      sock_send_string(sock, buffer);
167      sock_send_string(sock, "widget_add T title title\n");
168      sock_send_string(sock, "widget_set T title {Time Screen}\n");
169      sock_send_string(sock, "widget_add T one string\n");
170      if(lcd_hgt >= 4)
171      {
172         sock_send_string(sock, "widget_add T two string\n");
173         sock_send_string(sock, "widget_add T three string\n");
174      }
175      else
176      {
177         sprintf(buffer, "widget_set T title {TIME: %s}\n", host);
178         sock_send_string(sock, buffer);
179      }
180   }
181
182
183   if(colons)
184      colon = ':';
185   else
186      colon = ' ';
187
188
189   time(&thetime);
190   rtime = localtime(&thetime);
191
192
193   if(TwentyFourHour)
194   {
195      sprintf(hr, "%02d", rtime->tm_hour);
196   }
197   else
198   {
199      if (rtime->tm_hour > 12)
200      {
201         i = 1; sprintf(hr, "%02d", (rtime->tm_hour - 12));
202      }
203      else if(rtime->tm_hour == 0)
204      {
205         i = 0; sprintf(hr, "12");
206      }
207      else
208      {
209         i = 0; sprintf(hr, "%02d", rtime->tm_hour);
210      }
211   }
212   if(rtime->tm_hour >= 12) sprintf(ampm, "%s", "P");
213   else sprintf(ampm, "%s", "A");
214   
215   sprintf(min, "%02d", rtime->tm_min);
216   sprintf(sec, "%02d", rtime->tm_sec);
217
218
219   strcpy(day, shortdays[rtime->tm_wday]);
220   strcpy(month, shortmonths[rtime->tm_mon]);
221
222   
223   
224   ///////////////////// Write the title bar (os name and version)
225
226   if(lcd_hgt >= 4)
227   {
228      sprintf(tmp, "widget_set T title {");
229      sprintf(tmp+strlen(tmp), "%s %s: %s}\n",
230              sysname, kver, host);
231      if(display) sock_send_string(sock, tmp);
232   }
233
234   
235   /////////////////////// Display the time...
236   if(lcd_hgt >= 4)
237   {
238      get_uptime(&uptime, &idle);
239      idle = (idle * 100) / uptime;
240
241     
242      if(TwentyFourHour)
243         sprintf(tmp, "%s%c%s%c%s   %2i%% idle",
244                 hr, colon, min, colon, sec, (int)idle);
245      else
246         sprintf(tmp, "%s%c%s%c%s%s  %2i%% idle",
247                 hr, colon, min, colon, sec, ampm, (int)idle);
248      // Center the output line...
249      i = ((lcd_wid - strlen(tmp)) / 2) + 1;
250      sprintf(buffer, "widget_set T three %i 4 {%s}\n", i, tmp);
251      if(display) sock_send_string(sock, buffer);
252
253
254
255      sprintf(tmp, "%s %s %d, %d", day, month,
256              rtime->tm_mday, (rtime->tm_year + 1900));
257      sprintf(buffer, "widget_set T two 3 3 {%s}\n", tmp);
258      if(display) sock_send_string(sock, buffer);
259
260
261      /////////////////////// Display the uptime...
262      i = (int)uptime / 86400;
263      sprintf(day, "Up %d day%s,", i, (i != 1 ? "s" : ""));
264      i = ((int)uptime % 86400) / 60 / 60;
265      sprintf(hr, "%02i",i);
266      i = (((int)uptime % 86400) % 3600) / 60;
267      sprintf(min, "%02i",i);
268      i = ((int)uptime % 60);
269      sprintf(sec, "%02i",i);
270      if (colons)
271         sprintf(tmp, "%s %s:%s:%s", day, hr, min, sec);
272      else
273         sprintf(tmp, "%s %s %s %s", day, hr, min, sec);
274      //// Center this line automatically...
275      //i = ((lcd_wid - strlen(tmp)) / 2) + 1;
276      sprintf(buffer, "widget_set T one 1 2 {%s}\n", tmp);
277      if(display) sock_send_string(sock, buffer);
278
279     
280   }
281   else // 20x2 version of the screen
282   {
283      sprintf(tmp, "widget_set T one 1 2 {");
284      if(lcd_wid >= 20)
285         sprintf(tmp+strlen(tmp), "%d-%02d-%02d ",
286                 rtime->tm_year+1900, rtime->tm_mon+1, rtime->tm_mday);
287      else // 16x2 version...
288         sprintf(tmp+strlen(tmp), "%02d/%02d ",
289                 rtime->tm_mon+1, rtime->tm_mday);
290      sprintf(tmp+strlen(tmp), "%s%c%s%c%s", hr, colon, min, colon, sec);
291      if(! TwentyFourHour)
292         sprintf(tmp+strlen(tmp), "%s", ampm);
293      sprintf(tmp+strlen(tmp), "}\n");
294      if(display) sock_send_string(sock, tmp);
295   }
296
297
298   
299   colons = colons ^ 1;
300   
301   return 0;
302} // End time_screen()
303
304
305
306//////////////////////////////////////////////////////////////////////
307// Clock Screen displays current time and date...
308//
309int clock_screen(int rep, int display)
310{
311   char hr[8], min[8], sec[8], ampm[8];
312   char day[16], month[16];
313   static int first = 1;
314   static int colons = 0;
315   time_t thetime;
316   struct tm *rtime;
317   int i=0;
318   char colon;
319
320   if(lcd_hgt < 4) return 0;
321   
322   if(first)
323   {
324      first = 0;
325     
326      sock_send_string(sock, "screen_add O\n");
327      sprintf(buffer, "screen_set O name {Clock Screen: %s}\n", host);
328      sock_send_string(sock, buffer);
329      sock_send_string(sock, "widget_add O title title\n");
330      sock_send_string(sock, "widget_add O one string\n");
331      if(lcd_hgt >= 4)
332      {
333         sock_send_string(sock, "widget_set O title {DATE & TIME}\n");
334         sock_send_string(sock, "widget_add O two string\n");
335         sock_send_string(sock, "widget_add O three string\n");
336         sprintf(buffer, "widget_set O one 3 2 {%s}\n", host);
337         sock_send_string(sock, buffer);
338      }
339      else
340      {
341         sprintf(buffer, "widget_set O title {TIME: %s}\n", host);
342         sock_send_string(sock, buffer);
343      }
344   }
345   
346
347   if(colons)
348      colon = ':';
349   else
350      colon = ' ';
351   
352   time(&thetime);
353   rtime = localtime(&thetime);
354
355   strcpy(day, days[rtime->tm_wday]);
356
357   if(TwentyFourHour)
358   {
359      sprintf(hr, "%02d", rtime->tm_hour);
360   }
361   else
362   {
363     if (rtime->tm_hour > 12)
364     {
365        i = 1; sprintf(hr, "%02d", (rtime->tm_hour - 12));
366     }
367     else
368     {
369        i = 0; sprintf(hr, "%02d", rtime->tm_hour);
370     }
371   }
372   if(rtime->tm_hour == 12) i=1;
373   sprintf(min, "%02d", rtime->tm_min);
374   sprintf(sec, "%02d", rtime->tm_sec);
375   if (i == 1) { sprintf(ampm, "%s", "P"); }
376   else { sprintf(ampm, "%s", "A"); }
377   strcpy(month, months[rtime->tm_mon]);
378
379
380   if(lcd_hgt >= 4) // 4-line version of the screen
381   {
382      sprintf(tmp,  "widget_set O two 1 3 {");
383      if(TwentyFourHour)
384         sprintf(tmp+strlen(tmp), "%s%c%s%c%s  %s",
385                 hr, colon, min, colon, sec, day);
386      else
387         sprintf(tmp+strlen(tmp), "%s%c%s%c%s%s %s",
388                 hr, colon, min, colon, sec, ampm, day);
389      sprintf(tmp+strlen(tmp),  "}\n");
390      if(display) sock_send_string(sock, tmp);
391     
392     
393      sprintf(tmp,  "widget_set O three 2 4 {");
394      sprintf(tmp+strlen(tmp),
395              "%s %d, %d",
396              month,
397              rtime->tm_mday,
398              (rtime->tm_year + 1900));
399      sprintf(tmp+strlen(tmp),  "}\n");
400      if(display) sock_send_string(sock, tmp);
401   } // end if(lcd_hgt >= 4)
402   else // 20x2 version of the screen
403   {
404      sprintf(tmp, "widget_set O one 1 2 {");
405      sprintf(tmp+strlen(tmp), "%d-%02d-%02d ",
406              rtime->tm_year+1900, rtime->tm_mon+1, rtime->tm_mday);
407      sprintf(tmp+strlen(tmp), "%s%c%s%c%s", hr, colon, min, colon, sec);
408      if(! TwentyFourHour)
409         sprintf(tmp+strlen(tmp), "%s", ampm);
410      sprintf(tmp+strlen(tmp), "}\n");
411      if(display) sock_send_string(sock, tmp);
412   }
413   
414   colons = colons ^ 1;
415   
416   return 0;
417} // End clock_screen()
418
419
420
421////////////////////////////////////////////////////////////////////
422// Uptime Screen shows info about system uptime and OS version
423//
424int uptime_screen(int rep, int display)
425{
426   int i;
427   char date[16], hour[8], min[8], sec[8];
428   double uptime, idle;
429   static int first = 1;
430   static int colons = 0;
431   char colon;
432
433   if(first)
434   {
435      first = 0;
436
437      sock_send_string(sock, "screen_add U\n");
438      sprintf(buffer, "screen_set U name {Uptime Screen: %s}\n", host);
439      sock_send_string(sock, buffer);
440      sock_send_string(sock, "widget_add U title title\n");
441      if(lcd_hgt >= 4)
442      {
443         sock_send_string(sock, "widget_set U title {SYSTEM UPTIME}\n");
444         sock_send_string(sock, "widget_add U one string\n");
445         sock_send_string(sock, "widget_add U two string\n");
446         sock_send_string(sock, "widget_add U three string\n");
447
448         sprintf(buffer, "widget_set U one 3 2 {%s}\n", host);
449         sock_send_string(sock, buffer);
450         sprintf(tmp, "widget_set U three 5 4 {%s %s}\n", sysname, kver);
451         sock_send_string(sock, tmp);
452      }
453      else
454      {
455         sprintf(tmp, "widget_set U title {%s %s: %s}\n",
456                 sysname, kver, host);
457         sock_send_string(sock, tmp);
458         sock_send_string(sock, "widget_add U one string\n");
459      }
460   }
461
462   if(colons)
463      colon = ':';
464   else
465      colon = ' ';
466   
467   get_uptime(&uptime, &idle);
468   i = (int)uptime / 86400;
469   sprintf(date, "%d day%s,", i, (i != 1 ? "s" : ""));
470   i = ((int)uptime % 86400) / 60 / 60;
471   sprintf(hour, "%02i",i);
472   i = (((int)uptime % 86400) % 3600) / 60;
473   sprintf(min, "%02i",i);
474   i = ((int)uptime % 60);
475   sprintf(sec, "%02i",i);
476   sprintf(tmp, "%s %s%c%s%c%s", date, hour, colon, min, colon, sec);
477   i = ((20 - strlen(tmp)) / 2) + 1;
478
479   if(lcd_hgt >= 4)
480      sprintf(buffer, "widget_set U two %i 3 {%s}\n", i, tmp);
481   else
482      sprintf(buffer, "widget_set U one %i 2 {%s}\n", i, tmp);
483   if(display) sock_send_string(sock, buffer);
484
485   colons = colons ^ 1;
486   
487   return 0;
488} // End uptime_screen()
489
490
491//////////////////////////////////////////////////////////////////////
492// Big Clock Screen displays current time...
493//
494//Curses display is ugly, but should look nice on Matrix Orbital.
495//
496//+--------------------+
497//|111555 444222 111333|
498//|111555 444222 111333|
499//|111555 444222 111333|
500//|111555 444222 111333|
501//+--------------------+
502//
503int big_clock_screen(int rep, int display)
504{
505   static int first = 1;
506   static int colons = 0;
507   time_t thetime;
508   struct tm *rtime;
509   int pos[]={1,4,8,11,15,18};
510 //  int i=0;
511
512   char fulltxt[16], old_fulltxt[16];
513   int j=0;
514
515   if(lcd_hgt < 4) return 0;
516   
517   if(first)
518   {
519      first = 0;
520
521      sock_send_string(sock, "screen_add K\n");
522      sock_send_string(sock, "screen_set K name {Big Clock Screen}\n");
523      sock_send_string(sock, "widget_del K heartbeat\n");
524      sock_send_string(sock, "widget_add K d0 num\n");
525      sock_send_string(sock, "widget_add K d1 num\n");
526      sock_send_string(sock, "widget_add K d2 num\n");
527      sock_send_string(sock, "widget_add K d3 num\n");
528      sock_send_string(sock, "widget_add K d4 num\n");
529      sock_send_string(sock, "widget_add K d5 num\n");
530//      sock_send_string(sock, "widget_add K one string\n");
531      sock_send_string(sock, "widget_set K d0 1 0\n");
532      sock_send_string(sock, "widget_set K d1 4 0\n");
533      sock_send_string(sock, "widget_set K d2 8 0\n");
534      sock_send_string(sock, "widget_set K d3 11 0\n");
535      sock_send_string(sock, "widget_set K d4 15 0\n");
536      sock_send_string(sock, "widget_set K d5 18 0\n");
537      old_fulltxt[0]='0';
538      old_fulltxt[1]='0';
539      old_fulltxt[2]='0';
540      old_fulltxt[3]='0';
541      old_fulltxt[4]='0';
542      old_fulltxt[5]='0';
543      old_fulltxt[6]=0;
544//      sock_send_string(sock, "widget_set K one 1 4 {000000}\n");
545   }
546
547   time(&thetime);
548   rtime = localtime(&thetime);
549
550   sprintf(fulltxt, "%02d%02d%02d",
551           rtime->tm_hour, rtime->tm_min, rtime->tm_sec);
552   for(j=0;j<6;j++){
553      if (fulltxt[j]!=old_fulltxt[j]){
554         sprintf(tmp+strlen(tmp), "widget_set K d%d %d %c\n",
555                 j, pos[j], fulltxt[j]);
556         old_fulltxt[j]=fulltxt[j];
557      }
558   }
559
560   if(display)
561      sock_send_string(sock, tmp);
562
563   return 0;
564} // End big_clock_screen()
565
566
Note: See TracBrowser for help on using the repository browser.