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 "wmgeneral/wmgeneral.h"
35 #include "wmgeneral/misc.h"
37 #include "wmitime-master.xpm"
38 char wmitime_mask_bits
[64*64];
39 int wmitime_mask_width
= 64;
40 int wmitime_mask_height
= 64;
42 #define WMITIME_VERSION "0.1"
48 #define BCHAR_HEIGHT 9
50 #define MY_PI (3.14159)
52 extern char **environ
;
66 static struct tm
*clk
;
71 void printversion(void);
72 void BlitString(char *name
, int x
, int y
);
73 void BlitNum(int num
, int x
, int y
);
74 void wmitime_routine(int, char **);
75 int PortWatch( short port
);
76 void DrawInetTime(void);
77 void DrawStdTime(void);
79 void DrawInetWheel(void);
80 void DrawStdWheel(void);
81 void DrawLine(int x1
, int y1
, int x2
, int y2
, int sourcex
, int sourcey
);
83 int main(int argc
, char *argv
[]) {
89 /* Parse Command Line */
92 if (strlen(ProgName
) >= 5)
93 ProgName
+= (strlen(ProgName
) - 5);
95 for (i
=1; i
<argc
; i
++) {
101 if (strcmp(arg
+1, "display")) {
107 if (strcmp(arg
+1, "geometry")) {
119 /* Twelve-hour mode */
126 strcpy(locale
, argv
[i
+1]);
138 if (setlocale(LC_ALL
, locale
) == NULL
)
140 "warning: locale '%s' not recognized; defaulting to '%s'.",
141 locale
, setlocale(LC_ALL
, NULL
));
143 wmitime_routine(argc
, argv
);
148 /*******************************************************************************\
149 |* wmitime_routine *|
150 \*******************************************************************************/
152 void wmitime_routine(int argc
, char **argv
)
159 createXBMfromXPM(wmitime_mask_bits
, wmitime_master_xpm
, wmitime_mask_width
, wmitime_mask_height
);
161 openXwindow(argc
, argv
, wmitime_master_xpm
, wmitime_mask_bits
, wmitime_mask_width
, wmitime_mask_height
);
163 AddMouseRegion(0, 5, 6, 58, 16);
167 prevtime
= time(0) - 1;
173 if ( curtime
> prevtime
)
177 clk
= localtime(&curtime
);
196 while (XPending(display
))
198 XNextEvent(display
, &Event
);
205 XCloseDisplay(display
);
209 i
= CheckMouseRegion(Event
.xbutton
.x
, Event
.xbutton
.y
);
214 i
= CheckMouseRegion(Event
.xbutton
.x
, Event
.xbutton
.y
);
216 if (but_stat
== i
&& but_stat
>= 0)
221 TwelveHour
= (!TwelveHour
);
236 /* RedrawWindow(); */
246 void DrawInetTime(void)
250 /* Compute Inet Time */
251 iTime
=(clk
->tm_hour
*3600+clk
->tm_min
*60+clk
->tm_sec
);
252 iTime
=iTime
+((timezone
-1)+3600);
255 iTime
=(iTime
*1000)/86400;
265 BlitNum(iTime
, 38, 18);
269 void DrawStdTime(void)
287 sprintf(blitstr
, "%2i:%02i:%02i", i
, clk
->tm_min
, clk
->tm_sec
);
291 sprintf(blitstr
, "%02i:%02i:%02i", i
, clk
->tm_min
, clk
->tm_sec
);
296 len
= strlen(blitstr
);
298 /* Set starting co-ordinates... */
303 for( i
=0; i
<len
; i
++)
305 if (blitstr
[i
] == ':')
307 copyXPMArea(138, 23, 4, BCHAR_HEIGHT
, xoff
, yoff
);
310 else if (isdigit(blitstr
[i
]))
312 j
= blitstr
[i
] - '0';
319 srcx
+= (BCHAR_WIDTH
+ 1);
322 copyXPMArea(srcx
, srcy
, BCHAR_WIDTH
+1, BCHAR_HEIGHT
, xoff
, yoff
);
323 xoff
+=(BCHAR_WIDTH
+ 1);
327 copyXPMArea(66, 10, BCHAR_WIDTH
+1, BCHAR_HEIGHT
, xoff
, yoff
);
328 xoff
+=(BCHAR_WIDTH
+ 1);
336 char OrigBlitStr
[20], BlitStr
[20];
337 char *inbuf
, *outbuf
;
338 size_t inbytesleft
, outbytesleft
;
341 cd
= iconv_open("ASCII//TRANSLIT", nl_langinfo(CODESET
));
345 inbytesleft
= sizeof OrigBlitStr
;
346 outbytesleft
= sizeof BlitStr
;
348 sprintf(OrigBlitStr
, "%s", nl_langinfo(ABDAY_1
+ clk
->tm_wday
));
349 iconv(cd
, &inbuf
, &inbytesleft
, &outbuf
, &outbytesleft
);
351 BlitString( BlitStr
, 6, 50);
355 inbytesleft
= sizeof OrigBlitStr
;
356 outbytesleft
= sizeof BlitStr
;
358 sprintf(OrigBlitStr
, "%s", nl_langinfo(ABMON_1
+ clk
->tm_mon
));
359 iconv(cd
, &inbuf
, &inbytesleft
, &outbuf
, &outbytesleft
);
361 BlitString( BlitStr
, 40, 50);
365 sprintf(BlitStr
, "%02i", clk
->tm_mday
);
366 BlitString( BlitStr
, 25, 50);
369 void DrawInetWheel(void)
376 /* Calculate Wheel Position... */
377 iTime
=(clk
->tm_hour
*3600+clk
->tm_min
*60+clk
->tm_sec
);
378 iTime
=iTime
+((timezone
-1)+3600);
381 iTime
=(iTime
*1000)/8640;
391 WheelPos
= floor( (iTime
*8) / 10);
393 /* Draw the Wheel... */
406 copyXPMArea(xoff
, yoff
, 19, 19, 39, 29);
411 void DrawStdWheel(void)
430 DrawLine(cx
, cy
, prevhourx
, prevhoury
, 66, 9); /* erase old line */
432 hr
= (clk
->tm_hour
% 12);
434 psi
= hr
* (M_PI
/ 6.0);
435 psi
+= clk
->tm_min
* (M_PI
/ 360);
437 dx
= floor(sin(psi
) * 22 * 0.5 + 0.5);
438 dy
= floor(-cos(psi
) * 16 * 0.5 + 0.5);
446 DrawLine(cx
, cy
, dx
, dy
, sx
, sy
);
450 DrawLine(cx
, cy
, prevminx
, prevminy
, 66, 9); /* erase old line */
457 psi
= clk
->tm_min
* (M_PI
/ 30.0);
458 psi
+= clk
->tm_sec
* (M_PI
/ 1800);
460 dx
= floor(sin(psi
) * 22 * 0.7 + 0.5);
461 dy
= floor(-cos(psi
) * 16 * 0.7 + 0.5);
470 DrawLine(cx
, cy
, dx
, dy
, sx
, sy
);
473 void DrawLine(int x1
, int y1
, int x2
, int y2
, int sourcex
, int sourcey
)
496 deltax
= abs( x2
- x1
);
497 deltay
= abs( y2
- y1
);
500 xi
= (float) ((float)deltax
/ (float) deltay
);
505 yi
= (float) ((float)deltay
/ (float) deltax
);
509 if ( deltax
> deltay
)
511 for (x
=x1
; x
!= x2
; x
+= xs
)
514 y
+= (int) (yd
* ys
);
516 copyXPMArea(sourcex
, sourcey
, 1, 1, x
, y
);
523 for (y
=y1
; y
!= y2
; y
+= ys
)
526 x
+= (int) (xd
* xs
);
528 copyXPMArea(sourcex
, sourcey
, 1, 1, x
, y
);
537 /* Blits a string at given co-ordinates */
538 void BlitString(char *name
, int x
, int y
)
545 for (i
=0; name
[i
]; i
++)
548 c
= toupper(name
[i
]);
549 if (c
>= 'A' && c
<= 'Z')
552 copyXPMArea(c
* 6, 74, 6, 8, k
, y
);
556 { /* its a number or symbol */
558 copyXPMArea(c
* 6, 64, 6, 8, k
, y
);
565 void BlitNum(int num
, int x
, int y
)
570 sprintf(buf
, "%03i", num
);
572 BlitString(buf
, newx
, y
);
575 /*******************************************************************************\
577 \*******************************************************************************/
581 fprintf(stderr
, "\nWMiTIME - Window Maker Developers Team <wmaker-dev@lists.windowmaker.org>\n");
582 fprintf(stderr
, " original author: Dave Clark <clarkd@skynet.ca>\n\n");
583 fprintf(stderr
, "usage:\n");
584 fprintf(stderr
, " -12 12-hour mode\n");
585 fprintf(stderr
, " -display <display name>\n");
586 fprintf(stderr
, " -geometry +XPOS+YPOS initial window position\n");
587 fprintf(stderr
, " -l <locale> specify locale\n");
588 fprintf(stderr
, " -h this help screen\n");
589 fprintf(stderr
, " -v print the version number\n");
590 fprintf(stderr
, "\n");
593 /*******************************************************************************\
595 \*******************************************************************************/
597 void printversion(void)
599 fprintf(stderr
, "wmitime v%s\n", WMITIME_VERSION
);