3 wmitime.c -- internet time clock
5 by Dave Clark (clarkd@skynet.ca) (http://www.neotokyo.org/illusion)
7 This software is licensed through the GNU General Public Lisence.
25 #include <sys/param.h>
26 #include <sys/types.h>
27 #include <sys/ioctl.h>
28 #include <sys/socket.h>
32 #include <X11/extensions/shape.h>
34 #include <libdockapp/wmgeneral.h>
36 #include "wmitime-master.xpm"
37 char wmitime_mask_bits
[64*64];
38 int wmitime_mask_width
= 64;
39 int wmitime_mask_height
= 64;
41 #define WMITIME_VERSION "0.5"
47 #define BCHAR_HEIGHT 9
49 #define MY_PI (3.14159)
51 extern char **environ
;
65 static struct tm
*clk
;
70 void printversion(void);
71 void BlitString(char *name
, int x
, int y
);
72 void BlitNum(int num
, int x
, int y
);
73 void wmitime_routine(int, char **);
74 int PortWatch( short port
);
75 void DrawInetTime(void);
76 void DrawStdTime(void);
78 void DrawInetWheel(void);
79 void DrawStdWheel(void);
80 void DrawLine(int x1
, int y1
, int x2
, int y2
, int sourcex
, int sourcey
);
82 int main(int argc
, char *argv
[]) {
88 /* Parse Command Line */
91 if (strlen(ProgName
) >= 5)
92 ProgName
+= (strlen(ProgName
) - 5);
94 for (i
=1; i
<argc
; i
++) {
100 if (strcmp(arg
+1, "display")) {
106 if (strcmp(arg
+1, "geometry")) {
118 /* Twelve-hour mode */
125 strcpy(locale
, argv
[i
+1]);
137 if (setlocale(LC_ALL
, locale
) == NULL
)
139 "warning: locale '%s' not recognized; defaulting to '%s'.",
140 locale
, setlocale(LC_ALL
, NULL
));
142 wmitime_routine(argc
, argv
);
147 /*******************************************************************************\
148 |* wmitime_routine *|
149 \*******************************************************************************/
151 void wmitime_routine(int argc
, char **argv
)
158 createXBMfromXPM(wmitime_mask_bits
, wmitime_master_xpm
, wmitime_mask_width
, wmitime_mask_height
);
160 openXwindow(argc
, argv
, wmitime_master_xpm
, wmitime_mask_bits
, wmitime_mask_width
, wmitime_mask_height
);
162 AddMouseRegion(0, 5, 6, 58, 16);
166 prevtime
= time(0) - 1;
172 if ( curtime
> prevtime
)
176 clk
= localtime(&curtime
);
195 while (XPending(display
))
197 XNextEvent(display
, &Event
);
204 XCloseDisplay(display
);
208 i
= CheckMouseRegion(Event
.xbutton
.x
, Event
.xbutton
.y
);
213 i
= CheckMouseRegion(Event
.xbutton
.x
, Event
.xbutton
.y
);
215 if (but_stat
== i
&& but_stat
>= 0)
220 TwelveHour
= (!TwelveHour
);
235 /* RedrawWindow(); */
245 void DrawInetTime(void)
249 /* Compute Inet Time */
250 iTime
=(clk
->tm_hour
*3600+clk
->tm_min
*60+clk
->tm_sec
);
251 iTime
=iTime
+((timezone
-1)+3600);
254 iTime
=(iTime
*1000)/86400;
264 BlitNum(iTime
, 38, 18);
268 void DrawStdTime(void)
286 sprintf(blitstr
, "%2i:%02i:%02i", i
, clk
->tm_min
, clk
->tm_sec
);
290 sprintf(blitstr
, "%02i:%02i:%02i", i
, clk
->tm_min
, clk
->tm_sec
);
295 len
= strlen(blitstr
);
297 /* Set starting co-ordinates... */
302 for( i
=0; i
<len
; i
++)
304 if (blitstr
[i
] == ':')
306 copyXPMArea(138, 23, 4, BCHAR_HEIGHT
, xoff
, yoff
);
309 else if (isdigit(blitstr
[i
]))
311 j
= blitstr
[i
] - '0';
318 srcx
+= (BCHAR_WIDTH
+ 1);
321 copyXPMArea(srcx
, srcy
, BCHAR_WIDTH
+1, BCHAR_HEIGHT
, xoff
, yoff
);
322 xoff
+=(BCHAR_WIDTH
+ 1);
326 copyXPMArea(66, 10, BCHAR_WIDTH
+1, BCHAR_HEIGHT
, xoff
, yoff
);
327 xoff
+=(BCHAR_WIDTH
+ 1);
335 char OrigBlitStr
[20], BlitStr
[20];
336 char *inbuf
, *outbuf
;
337 size_t inbytesleft
, outbytesleft
;
340 cd
= iconv_open("ASCII//TRANSLIT", nl_langinfo(CODESET
));
344 inbytesleft
= sizeof OrigBlitStr
;
345 outbytesleft
= sizeof BlitStr
;
347 sprintf(OrigBlitStr
, "%s", nl_langinfo(ABDAY_1
+ clk
->tm_wday
));
348 iconv(cd
, &inbuf
, &inbytesleft
, &outbuf
, &outbytesleft
);
350 BlitString( BlitStr
, 6, 50);
354 inbytesleft
= sizeof OrigBlitStr
;
355 outbytesleft
= sizeof BlitStr
;
357 sprintf(OrigBlitStr
, "%s", nl_langinfo(ABMON_1
+ clk
->tm_mon
));
358 iconv(cd
, &inbuf
, &inbytesleft
, &outbuf
, &outbytesleft
);
360 BlitString( BlitStr
, 40, 50);
364 sprintf(BlitStr
, "%02i", clk
->tm_mday
);
365 BlitString( BlitStr
, 25, 50);
368 void DrawInetWheel(void)
375 /* Calculate Wheel Position... */
376 iTime
=(clk
->tm_hour
*3600+clk
->tm_min
*60+clk
->tm_sec
);
377 iTime
=iTime
+((timezone
-1)+3600);
380 iTime
=(iTime
*1000)/8640;
390 WheelPos
= floor( (iTime
*8) / 10);
392 /* Draw the Wheel... */
405 copyXPMArea(xoff
, yoff
, 19, 19, 39, 29);
410 void DrawStdWheel(void)
429 DrawLine(cx
, cy
, prevhourx
, prevhoury
, 66, 9); /* erase old line */
431 hr
= (clk
->tm_hour
% 12);
433 psi
= hr
* (M_PI
/ 6.0);
434 psi
+= clk
->tm_min
* (M_PI
/ 360);
436 dx
= floor(sin(psi
) * 22 * 0.5 + 0.5);
437 dy
= floor(-cos(psi
) * 16 * 0.5 + 0.5);
445 DrawLine(cx
, cy
, dx
, dy
, sx
, sy
);
449 DrawLine(cx
, cy
, prevminx
, prevminy
, 66, 9); /* erase old line */
456 psi
= clk
->tm_min
* (M_PI
/ 30.0);
457 psi
+= clk
->tm_sec
* (M_PI
/ 1800);
459 dx
= floor(sin(psi
) * 22 * 0.7 + 0.5);
460 dy
= floor(-cos(psi
) * 16 * 0.7 + 0.5);
469 DrawLine(cx
, cy
, dx
, dy
, sx
, sy
);
472 void DrawLine(int x1
, int y1
, int x2
, int y2
, int sourcex
, int sourcey
)
495 deltax
= abs( x2
- x1
);
496 deltay
= abs( y2
- y1
);
499 xi
= (float) ((float)deltax
/ (float) deltay
);
504 yi
= (float) ((float)deltay
/ (float) deltax
);
508 if ( deltax
> deltay
)
510 for (x
=x1
; x
!= x2
; x
+= xs
)
513 y
+= (int) (yd
* ys
);
515 copyXPMArea(sourcex
, sourcey
, 1, 1, x
, y
);
522 for (y
=y1
; y
!= y2
; y
+= ys
)
525 x
+= (int) (xd
* xs
);
527 copyXPMArea(sourcex
, sourcey
, 1, 1, x
, y
);
536 /* Blits a string at given co-ordinates */
537 void BlitString(char *name
, int x
, int y
)
544 for (i
=0; name
[i
]; i
++)
547 c
= toupper(name
[i
]);
548 if (c
>= 'A' && c
<= 'Z')
551 copyXPMArea(c
* 6, 74, 6, 8, k
, y
);
555 { /* its a number or symbol */
557 copyXPMArea(c
* 6, 64, 6, 8, k
, y
);
564 void BlitNum(int num
, int x
, int y
)
569 sprintf(buf
, "%03i", num
);
571 BlitString(buf
, newx
, y
);
574 /*******************************************************************************\
576 \*******************************************************************************/
580 fprintf(stderr
, "\nWMiTIME - Window Maker Developers Team <wmaker-dev@googlegroups.com>\n");
581 fprintf(stderr
, " original author: Dave Clark <clarkd@skynet.ca>\n\n");
582 fprintf(stderr
, "usage:\n");
583 fprintf(stderr
, " -12 12-hour mode\n");
584 fprintf(stderr
, " -display <display name>\n");
585 fprintf(stderr
, " -geometry +XPOS+YPOS initial window position\n");
586 fprintf(stderr
, " -l <locale> specify locale\n");
587 fprintf(stderr
, " -h this help screen\n");
588 fprintf(stderr
, " -v print the version number\n");
589 fprintf(stderr
, "\n");
592 /*******************************************************************************\
594 \*******************************************************************************/
596 void printversion(void)
598 fprintf(stderr
, "wmitime v%s\n", WMITIME_VERSION
);