Remove trailing whitespace.
[dockapps.git] / wmtime / wmtime / wmtime.c
blob2c4f6e8de071852ef5d10f984dd19eb0408c15c9
1 /*
2 Code based on wmppp/wmifs
4 [Orig WMMON comments]
6 This code was mainly put together by looking at the
7 following programs:
9 asclock
10 A neat piece of equip, used to display the date
11 and time on the screen.
12 Comes with every AfterStep installation.
14 Source used:
15 How do I create a not so solid window?
16 How do I open a window?
17 How do I use pixmaps?
19 ------------------------------------------------------------
21 Author: Martijn Pieterse (pieterse@xs4all.nl)
23 This program is distributed under the GPL license.
24 (as were asclock and pppstats)
26 ----
27 Changes:
28 ----
29 15/07/2008 (Paul Harris, harris.pc@gmail.com)
30 * Minor changes to correct build warnings
31 09/10/2003 (Simon Law, sfllaw@debian.org)
32 * Add -geometry support
33 * Add -noseconds support
34 * Make the digital clock fill the space provided
35 * Eliminated exploitable static buffers
36 17/05/1998 (Antoine Nulle, warp@xs4all.nl)
37 * Updated version number and some other minor stuff
38 16/05/1998 (Antoine Nulle, warp@xs4all.nl)
39 * Added Locale support, based on original diff supplied
40 by Alen Salamun (snowman@hal9000.medinet.si)
41 04/05/1998 (Martijn Pieterse, pieterse@xs4all.nl)
42 * Moved the hands one pixel down.
43 * Removed the RedrawWindow out of the main loop
44 02/05/1998 (Martijn Pieterse, pieterse@xs4all.nl)
45 * Removed a lot of code that was in the wmgeneral dir.
46 02/05/1998 (Antoine Nulle, warp@xs4all.nl)
47 * Updated master-xpm, hour dots where a bit 'off'
48 30/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
49 * Added anti-aliased hands
50 23/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
51 * Changed the hand lengths.. again! ;)
52 * Zombies were created, so added wait code
53 21/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
54 * Added digital/analog switching support
55 18/04/1998 (Martijn Pieterse, pieterse@xs4all.nl)
56 * Started this project.
57 * Copied the source from wmmon.
60 #define _GNU_SOURCE
61 #include <stdlib.h>
62 #include <stdio.h>
63 #include <time.h>
64 #include <string.h>
65 #include <fcntl.h>
66 #include <unistd.h>
67 #include <math.h>
68 #include <locale.h>
69 #include <langinfo.h>
70 #include <iconv.h>
71 #include <ctype.h>
73 #include <sys/wait.h>
74 #include <sys/param.h>
75 #include <sys/types.h>
77 #include <X11/Xlib.h>
78 #include <X11/xpm.h>
79 #include <X11/extensions/shape.h>
81 #include "../wmgeneral/wmgeneral.h"
82 #include "../wmgeneral/misc.h"
84 #include "wmtime-master.xpm"
85 #include "wmtime-mask.xbm"
87 /***********/
88 /* Defines */
89 /***********/
91 const char* default_left_action = NULL;
92 const char* default_middle_action = NULL;
93 const char* default_right_action = NULL;
95 #define WMMON_VERSION "1.1"
97 /********************/
98 /* Global Variables */
99 /********************/
101 int digital = 0;
102 int noseconds = 0;
103 char day_of_week[7][3] = { "SU", "MO", "TU", "WE", "TH", "FR", "SA" };
104 char mon_of_year[12][4] = { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };
106 /* functions */
107 void usage(char *);
108 void printversion(void);
110 void wmtime_routine(int, char **);
111 void get_lang();
113 int main(int argc, char *argv[]) {
115 int i;
116 char *name = argv[0];
118 for (i=1; i<argc; i++) {
119 char *arg = argv[i];
121 if (*arg=='-') {
122 switch (arg[1]) {
123 case 'd' :
124 if (strcmp(arg+1, "display")
125 && strcmp(arg+1, "digital") && strcmp(arg+1, "d")) {
126 usage(name);
127 return 1;
129 if (!strcmp(arg+1, "digital") || !(strcmp(arg+1, "d")))
130 digital = 1;
131 break;
132 case 'g' :
133 if (strcmp(arg+1, "geometry")) {
134 usage(name);
135 return 1;
137 break;
138 case 'n' :
139 if (strcmp(arg+1, "noseconds") && strcmp(arg+1, "n")) {
140 usage(name);
141 return 1;
142 } else {
143 noseconds = 1;
145 break;
146 case 'v' :
147 printversion();
148 return 0;
149 default:
150 usage(name);
151 return 1;
156 if (setlocale(LC_ALL, "") != NULL)
157 get_lang();
159 wmtime_routine(argc, argv);
160 return 0;
163 /************/
164 /* get_lang */
165 /************/
166 void get_lang(void)
168 char langbuf[10], outbuf[10];
169 char *inp, *outp;
170 iconv_t icd;
171 int i, ret;
172 size_t insize, outsize;
174 icd = iconv_open("ASCII//TRANSLIT", nl_langinfo(CODESET));
175 if (icd < 0)
176 return;
178 for (i = 0; i < 7; i++) {
179 strncpy(langbuf, nl_langinfo(ABDAY_1 + i), 10);
180 insize = outsize = 10;
181 inp = langbuf;
182 outp = outbuf;
183 do {
184 ret = iconv(icd, &inp, &insize, &outp, &outsize);
185 } while (outsize > 0 && ret > 0);
186 if (strstr(outbuf,"?") != NULL) return;
187 for (outp = outbuf, outsize = 0; *outp != 0 && outsize < 2;
188 outp++, outsize++)
189 day_of_week[i][outsize] = toupper(*outp);
191 for (i = 0; i < 12; i++) {
192 strncpy(langbuf, nl_langinfo(ABMON_1 + i), 10);
193 insize = outsize = 10;
194 inp = langbuf;
195 outp = outbuf;
196 do {
197 ret = iconv(icd, &inp, &insize, &outp, &outsize);
198 } while (outsize > 0 && ret > 0);
199 if (strstr(outbuf,"?") != NULL) return;
200 for (outp = outbuf, outsize = 0; *outp != 0 && outsize < 3;
201 outp++, outsize++)
202 mon_of_year[i][outsize] = toupper(*outp);
205 iconv_close(icd);
208 /*******************************************************************************\
209 |* wmtime_routine *|
210 \*******************************************************************************/
212 char *left_action = NULL;
213 char *right_action = NULL;
214 char *middle_action = NULL;
216 void DrawTime(int, int, int);
217 void DrawWijzer(int, int, int);
218 void DrawDate(int, int, int);
220 void wmtime_routine(int argc, char **argv) {
222 rckeys wmtime_keys[] = {
223 { "left", &left_action },
224 { "right", &right_action },
225 { "middle", &middle_action },
226 { NULL, NULL }
229 int i;
230 XEvent Event;
231 int but_stat = -1;
233 struct tm *time_struct;
235 long starttime;
236 long curtime;
238 char *conffile = NULL;
240 /* Scan through ~/.wmtimerc for the mouse button actions. */
241 if (default_left_action) left_action = strdup(default_left_action);
242 if (default_middle_action) middle_action = strdup(default_middle_action);
243 if (default_right_action) right_action = strdup(default_right_action);
245 /* Scan through the .rc files */
246 if (asprintf(&conffile, "/etc/wmtimerc") >= 0) {
247 parse_rcfile(conffile, wmtime_keys);
248 free(conffile);
251 if (asprintf(&conffile, "%s/.wmtimerc", getenv("HOME")) >= 0) {
252 parse_rcfile(conffile, wmtime_keys);
253 free(conffile);
256 if (asprintf(&conffile, "/etc/wmtimerc.fixed") >= 0) {
257 parse_rcfile(conffile, wmtime_keys);
258 free(conffile);
261 openXwindow(argc, argv, wmtime_master_xpm, wmtime_mask_bits, 128, 64);
263 /* Mask out the right parts of the clock */
264 copyXPMArea(0, 0, 128, 64, 0, 98); /* Draw the borders */
265 copyXPMArea(0, 0, 64, 64, 64, 0); /* Draw the clock face */
266 copyXPMArea(64, 98, 64, 64, 0, 0); /* Draw the LCD background */
267 setMaskXY(0, 0);
269 /* add mouse region */
270 AddMouseRegion(0, 5, 48, 58, 60);
271 AddMouseRegion(1, 5, 5, 58, 46);
273 starttime = time(0);
275 curtime = time(0);
276 time_struct = localtime(&curtime);
278 while (1) {
279 curtime = time(0);
281 waitpid(0, NULL, WNOHANG);
283 time_struct = localtime(&curtime);
286 if (curtime >= starttime) {
287 if (!digital) {
288 /* Now to update the seconds */
290 DrawWijzer(time_struct->tm_hour, time_struct->tm_min, time_struct->tm_sec);
292 DrawDate(time_struct->tm_wday, time_struct->tm_mday, time_struct->tm_mon);
294 } else {
296 DrawTime(time_struct->tm_hour, time_struct->tm_min, time_struct->tm_sec);
298 DrawDate(time_struct->tm_wday, time_struct->tm_mday, time_struct->tm_mon);
300 RedrawWindow();
304 while (XPending(display)) {
305 XNextEvent(display, &Event);
306 switch (Event.type) {
307 case Expose:
308 RedrawWindow();
309 break;
310 case DestroyNotify:
311 XCloseDisplay(display);
312 exit(0);
313 break;
314 case ButtonPress:
315 but_stat = CheckMouseRegion(Event.xbutton.x, Event.xbutton.y);
316 break;
317 case ButtonRelease:
318 i = CheckMouseRegion(Event.xbutton.x, Event.xbutton.y);
319 if (but_stat == i && but_stat >= 0) {
320 switch (but_stat) {
321 case 0:
322 digital = 1-digital;
324 if (digital) {
325 copyXPMArea(64, 98, 64, 64, 0, 0);
326 DrawTime(time_struct->tm_hour, time_struct->tm_min, time_struct->tm_sec);
327 DrawDate(time_struct->tm_wday, time_struct->tm_mday, time_struct->tm_mon);
328 } else {
329 copyXPMArea(0, 98, 64, 64, 0, 0);
330 DrawWijzer(time_struct->tm_hour, time_struct->tm_min, time_struct->tm_sec);
331 DrawDate(time_struct->tm_wday, time_struct->tm_mday, time_struct->tm_mon);
333 RedrawWindow();
334 break;
335 case 1:
336 switch (Event.xbutton.button) {
337 case 1:
338 if (left_action)
339 execCommand(left_action);
340 break;
341 case 2:
342 if (middle_action)
343 execCommand(middle_action);
344 break;
345 case 3:
346 if (right_action)
347 execCommand(right_action);
348 break;
352 break;
356 /* Sleep 0.3 seconds */
357 usleep(300000L);
361 /*******************************************************************************\
362 |* DrawTime *|
363 \*******************************************************************************/
365 void DrawTime(int hr, int min, int sec) {
366 const int time_size = 16;
367 char time[time_size];
368 char *p = time;
369 int i,j,k=6;
370 int numfields;
372 /* 7x13 */
374 if (noseconds) {
375 snprintf(time, time_size, "%02d:%02d ", hr, min);
376 numfields = 2;
378 else {
379 snprintf(time, time_size, "%02d:%02d:%02d ", hr, min, sec);
380 numfields = 3;
383 for (i=0; i < numfields; i++) {
384 for (j=0; j<2; j++) {
385 copyXPMArea((*p-'0')*7 + 1, 84, 8, 13, k, 18);
386 k += 7;
387 p++;
389 if (*p == ':') {
390 copyXPMArea(71, 84, 5, 13, k, 18);
391 k += 4;
392 p++;
397 /*******************************************************************************\
398 |* DrawDate *|
399 \*******************************************************************************/
401 void DrawDate(int wkday, int dom, int month) {
402 const int date_size = 16;
403 char date[date_size];
404 char *p = date;
405 int i,k;
407 /* 7x13 */
409 snprintf(date, date_size,
410 "%.2s%02d%.3s ", day_of_week[wkday], dom, mon_of_year[month]);
412 k = 5;
413 for (i=0; i<2; i++) {
414 if (*p < 'A')
415 copyXPMArea((*p-'0')*6, 64, 6, 9, k, 49);
416 else
417 copyXPMArea((*p-'A')*6, 74, 6, 9, k, 49);
418 k += 6;
419 p++;
421 k = 23;
422 for (i=0; i<2; i++) {
423 copyXPMArea((*p-'0')*6, 64, 6, 9, k, 49);
424 k += 6;
425 p++;
427 copyXPMArea(61, 64, 4, 9, k, 49);
428 k += 4;
429 for (i=0; i<3; i++) {
430 if (*p < 'A')
431 copyXPMArea((*p-'0')*6, 64, 6, 9, k, 49);
432 else
433 copyXPMArea((*p-'A')*6, 74, 6, 9, k, 49);
434 k += 6;
435 p++;
439 /*******************************************************************************\
440 |* DrawWijzer *|
441 \*******************************************************************************/
443 void DrawWijzer(int hr, int min, int sec) {
445 double psi;
446 int dx,dy;
447 int x,y;
448 int ddx,ddy;
449 int adder;
450 int k;
452 int i;
454 hr %= 12;
456 copyXPMArea(5+64, 5, 54, 40, 5, 5);
458 /**********************************************************************/
459 psi = hr * (M_PI / 6.0);
460 psi += min * (M_PI / 360);
462 dx = floor(sin(psi) * 22 * 0.7 + 0.5);
463 dy = floor(-cos(psi) * 16 * 0.7 + 0.5);
465 // dx, dy is het punt waar we naar toe moeten.
466 // Zoek alle punten die ECHT op de lijn liggen:
468 ddx = 1;
469 ddy = 1;
470 if (dx < 0) ddx = -1;
471 if (dy < 0) ddy = -1;
473 x = 0;
474 y = 0;
476 if (abs(dx) > abs(dy)) {
477 if (dy != 0)
478 adder = abs(dx) / 2;
479 else
480 adder = 0;
481 for (i=0; i<abs(dx); i++) {
482 // laat de kleur afhangen van de adder.
483 // adder loopt van abs(dx) tot 0
485 k = 12 - adder / (abs(dx) / 12.0);
486 copyXPMArea(79+k, 67, 1, 1, x + 31, y + 24 - ddy);
488 copyXPMArea(79, 67, 1, 1, x + 31, y + 24);
490 k = 12-k;
491 copyXPMArea(79+k, 67, 1, 1, x + 31, y + 24 + ddy);
494 x += ddx;
496 adder -= abs(dy);
497 if (adder < 0) {
498 adder += abs(dx);
499 y += ddy;
502 } else {
503 if (dx != 0)
504 adder = abs(dy) / 2;
505 else
506 adder = 0;
507 for (i=0; i<abs(dy); i++) {
508 k = 12 - adder / (abs(dy) / 12.0);
509 copyXPMArea(79+k, 67, 1, 1, x + 31 - ddx, y + 24);
511 copyXPMArea(79, 67, 1, 1, x + 31, y + 24);
513 k = 12-k;
514 copyXPMArea(79+k, 67, 1, 1, x + 31 + ddx, y + 24);
516 y += ddy;
518 adder -= abs(dx);
519 if (adder < 0) {
520 adder += abs(dy);
521 x += ddx;
525 /**********************************************************************/
526 psi = min * (M_PI / 30.0);
527 psi += sec * (M_PI / 1800);
529 dx = floor(sin(psi) * 22 * 0.55 + 0.5);
530 dy = floor(-cos(psi) * 16 * 0.55 + 0.5);
532 // dx, dy is het punt waar we naar toe moeten.
533 // Zoek alle punten die ECHT op de lijn liggen:
535 dx += dx;
536 dy += dy;
538 ddx = 1;
539 ddy = 1;
540 if (dx < 0) ddx = -1;
541 if (dy < 0) ddy = -1;
543 x = 0;
544 y = 0;
546 if (abs(dx) > abs(dy)) {
547 if (dy != 0)
548 adder = abs(dx) / 2;
549 else
550 adder = 0;
551 for (i=0; i<abs(dx); i++) {
552 // laat de kleur afhangen van de adder.
553 // adder loopt van abs(dx) tot 0
555 k = 12 - adder / (abs(dx) / 12.0);
556 copyXPMArea(79+k, 67, 1, 1, x + 31, y + 24 - ddy);
558 copyXPMArea(79, 67, 1, 1, x + 31, y + 24);
560 k = 12-k;
561 copyXPMArea(79+k, 67, 1, 1, x + 31, y + 24 + ddy);
564 x += ddx;
566 adder -= abs(dy);
567 if (adder < 0) {
568 adder += abs(dx);
569 y += ddy;
572 } else {
573 if (dx != 0)
574 adder = abs(dy) / 2;
575 else
576 adder = 0;
577 for (i=0; i<abs(dy); i++) {
578 k = 12 - adder / (abs(dy) / 12.0);
579 copyXPMArea(79+k, 67, 1, 1, x + 31 - ddx, y + 24);
581 copyXPMArea(79, 67, 1, 1, x + 31, y + 24);
583 k = 12-k;
584 copyXPMArea(79+k, 67, 1, 1, x + 31 + ddx, y + 24);
586 y += ddy;
588 adder -= abs(dx);
589 if (adder < 0) {
590 adder += abs(dy);
591 x += ddx;
595 /**********************************************************************/
596 if (noseconds)
597 return; /* Skip drawing the seconds. */
599 psi = sec * (M_PI / 30.0);
601 dx = floor(sin(psi) * 22 * 0.9 + 0.5);
602 dy = floor(-cos(psi) * 16 * 0.9 + 0.5);
604 // dx, dy is het punt waar we naar toe moeten.
605 // Zoek alle punten die ECHT op de lijn liggen:
607 ddx = 1;
608 ddy = 1;
609 if (dx < 0) ddx = -1;
610 if (dy < 0) ddy = -1;
612 if (dx == 0) ddx = 0;
613 if (dy == 0) ddy = 0;
615 x = 0;
616 y = 0;
619 if (abs(dx) > abs(dy)) {
620 if (dy != 0)
621 adder = abs(dx) / 2;
622 else
623 adder = 0;
624 for (i=0; i<abs(dx); i++) {
625 // laat de kleur afhangen van de adder.
626 // adder loopt van abs(dx) tot 0
628 k = 12 - adder / (abs(dx) / 12.0);
629 copyXPMArea(79+k, 70, 1, 1, x + 31, y + 24 - ddy);
631 k = 12-k;
632 copyXPMArea(79+k, 70, 1, 1, x + 31, y + 24);
635 x += ddx;
637 adder -= abs(dy);
638 if (adder < 0) {
639 adder += abs(dx);
640 y += ddy;
643 } else {
644 if (dx != 0)
645 adder = abs(dy) / 2;
646 else
647 adder = 0;
648 for (i=0; i<abs(dy); i++) {
649 k = 12 - adder / (abs(dy) / 12.0);
650 copyXPMArea(79+k, 70, 1, 1, x + 31 - ddx, y + 24);
652 k = 12-k;
653 copyXPMArea(79+k, 70, 1, 1, x + 31, y + 24);
655 y += ddy;
657 adder -= abs(dx);
658 if (adder < 0) {
659 adder += abs(dy);
660 x += ddx;
666 /*******************************************************************************\
667 |* usage *|
668 \*******************************************************************************/
670 void usage(char *name) {
671 printf("Usage: %s [OPTION]...\n", name);
672 printf("WindowMaker dockapp that displays the time and date.\n");
673 printf("\n");
674 printf(" -d, -digital display the digital clock\n");
675 printf(" -display DISPLAY contact the DISPLAY X server\n");
676 printf(" -geometry GEOMETRY position the clock at GEOMETRY\n");
677 printf(" -n, -noseconds disables the second hand\n");
678 printf(" -h display this help and exit\n");
679 printf(" -v output version information and exit\n");
682 /*******************************************************************************\
683 |* printversion *|
684 \*******************************************************************************/
686 void printversion(void) {
687 printf("WMTime version %s\n", WMMON_VERSION);
690 /* vim: sw=4 ts=4 columns=82