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.
22 #include <sys/param.h>
23 #include <sys/types.h>
24 #include <sys/ioctl.h>
25 #include <sys/socket.h>
29 #include <X11/extensions/shape.h>
31 #include "../wmgeneral/wmgeneral.h"
32 #include "../wmgeneral/misc.h"
44 #include "wmitime-master.xpm"
45 char wmitime_mask_bits
[64*64];
46 int wmitime_mask_width
= 64;
47 int wmitime_mask_height
= 64;
49 #define WMITIME_VERSION "0.1"
55 #define BCHAR_HEIGHT 9
57 #define MY_PI (3.14159)
59 extern char **environ
;
63 char uconfig_file
[256];
73 static struct tm
*clk
;
78 void printversion(void);
79 void BlitString(char *name
, int x
, int y
);
80 void BlitNum(int num
, int x
, int y
);
81 void wmitime_routine(int, char **);
82 int PortWatch( short port
);
83 int ReadConfigInt(FILE *fp
, char *setting
, int *value
);
84 int ReadConfigString(FILE *fp
, char *setting
, char *value
);
85 int Read_Config_File( char *filename
);
86 void DrawInetTime(void);
87 void DrawStdTime(void);
89 void DrawInetWheel(void);
90 void DrawStdWheel(void);
91 void DrawLine(int x1
, int y1
, int x2
, int y2
, int sourcex
, int sourcey
);
93 int main(int argc
, char *argv
[]) {
99 /* Parse Command Line */
102 if (strlen(ProgName
) >= 5)
103 ProgName
+= (strlen(ProgName
) - 5);
105 for (i
=1; i
<argc
; i
++) {
111 if (strcmp(arg
+1, "display")) {
117 if (strcmp(arg
+1, "geometry")) {
129 strcpy(uconfig_file
, argv
[i
+1]);
148 wmitime_routine(argc
, argv
);
153 /*******************************************************************************\
154 |* wmitime_routine *|
155 \*******************************************************************************/
157 void wmitime_routine(int argc
, char **argv
)
164 // char config_file[512];
166 createXBMfromXPM(wmitime_mask_bits
, wmitime_master_xpm
, wmitime_mask_width
, wmitime_mask_height
);
168 openXwindow(argc
, argv
, wmitime_master_xpm
, wmitime_mask_bits
, wmitime_mask_width
, wmitime_mask_height
);
170 AddMouseRegion(0, 5, 6, 58, 16);
172 // We don't need a config file (yet)...
177 if (uconfig_file
[0] != 0)
179 // user-specified config file
180 fprintf(stderr
, "Using user-specified config file '%s'.\n", uconfig_file
);
181 Read_Config_File(uconfig_file
);
185 sprintf(config_file
, "%s/.wmitimerc", getenv("HOME"));
187 if (!Read_Config_File(config_file
))
189 // Fall back to /etc/wminetrc
190 sprintf(config_file
, "/etc/wmitimerc");
192 Read_Config_File(config_file
);
199 prevtime
= time(0) - 1;
205 if ( curtime
> prevtime
)
209 clk
= localtime(&curtime
);
228 while (XPending(display
))
230 XNextEvent(display
, &Event
);
237 XCloseDisplay(display
);
241 i
= CheckMouseRegion(Event
.xbutton
.x
, Event
.xbutton
.y
);
246 i
= CheckMouseRegion(Event
.xbutton
.x
, Event
.xbutton
.y
);
248 if (but_stat
== i
&& but_stat
>= 0)
253 TwelveHour
= (!TwelveHour
);
278 void DrawInetTime(void)
283 iTime
=(clk
->tm_hour
*3600+clk
->tm_min
*60+clk
->tm_sec
);
284 iTime
=iTime
+((timezone
-1)+3600);
287 iTime
=(iTime
*1000)/86400;
297 BlitNum(iTime
, 38, 18);
301 void DrawStdTime(void)
319 sprintf(blitstr
, "%2i:%02i:%02i", i
, clk
->tm_min
, clk
->tm_sec
);
323 sprintf(blitstr
, "%02i:%02i:%02i", i
, clk
->tm_min
, clk
->tm_sec
);
328 len
= strlen(blitstr
);
330 // Set starting co-ordinates...
335 for( i
=0; i
<len
; i
++)
337 if (blitstr
[i
] == ':')
339 copyXPMArea(138, 23, 4, BCHAR_HEIGHT
, xoff
, yoff
);
342 else if (isdigit(blitstr
[i
]))
344 j
= blitstr
[i
] - '0';
351 srcx
+= (BCHAR_WIDTH
+ 1);
354 copyXPMArea(srcx
, srcy
, BCHAR_WIDTH
+1, BCHAR_HEIGHT
, xoff
, yoff
);
355 xoff
+=(BCHAR_WIDTH
+ 1);
359 copyXPMArea(66, 10, BCHAR_WIDTH
+1, BCHAR_HEIGHT
, xoff
, yoff
);
360 xoff
+=(BCHAR_WIDTH
+ 1);
370 sprintf(BlitStr
, "%s", daynames
[clk
->tm_wday
]);
371 BlitString( BlitStr
, 6, 50);
376 sprintf(BlitStr
, "%s", monthnames
[clk
->tm_mon
]);
377 BlitString( BlitStr
, 40, 50);
379 sprintf(BlitStr
, "%02i", clk
->tm_mday
);
380 BlitString( BlitStr
, 25, 50);
383 sprintf(BlitStr
, "%s", monthnames
[clk
->tm_mon
]);
384 BlitString( BlitStr
, 25, 50);
386 sprintf(BlitStr
, "%02i", clk
->tm_mday
);
387 BlitString( BlitStr
, 45, 50);
392 void DrawInetWheel(void)
399 // Calculate Wheel Position...
400 iTime
=(clk
->tm_hour
*3600+clk
->tm_min
*60+clk
->tm_sec
);
401 iTime
=iTime
+((timezone
-1)+3600);
404 iTime
=(iTime
*1000)/8640;
414 WheelPos
= floor( (iTime
*8) / 10);
429 copyXPMArea(xoff
, yoff
, 19, 19, 39, 29);
434 void DrawStdWheel(void)
453 DrawLine(cx
, cy
, prevhourx
, prevhoury
, 66, 9); // erase old line
455 hr
= (clk
->tm_hour
% 12);
457 psi
= hr
* (M_PI
/ 6.0);
458 psi
+= clk
->tm_min
* (M_PI
/ 360);
460 dx
= floor(sin(psi
) * 22 * 0.5 + 0.5);
461 dy
= floor(-cos(psi
) * 16 * 0.5 + 0.5);
469 DrawLine(cx
, cy
, dx
, dy
, sx
, sy
);
473 DrawLine(cx
, cy
, prevminx
, prevminy
, 66, 9); // erase old line
480 psi
= clk
->tm_min
* (M_PI
/ 30.0);
481 psi
+= clk
->tm_sec
* (M_PI
/ 1800);
483 dx
= floor(sin(psi
) * 22 * 0.7 + 0.5);
484 dy
= floor(-cos(psi
) * 16 * 0.7 + 0.5);
493 DrawLine(cx
, cy
, dx
, dy
, sx
, sy
);
496 void DrawLine(int x1
, int y1
, int x2
, int y2
, int sourcex
, int sourcey
)
519 deltax
= abs( x2
- x1
);
520 deltay
= abs( y2
- y1
);
523 xi
= (float) ((float)deltax
/ (float) deltay
);
528 yi
= (float) ((float)deltay
/ (float) deltax
);
532 if ( deltax
> deltay
)
534 for (x
=x1
; x
!= x2
; x
+= xs
)
537 y
+= (int) (yd
* ys
);
539 copyXPMArea(sourcex
, sourcey
, 1, 1, x
, y
);
546 for (y
=y1
; y
!= y2
; y
+= ys
)
549 x
+= (int) (xd
* xs
);
551 copyXPMArea(sourcex
, sourcey
, 1, 1, x
, y
);
560 // Blits a string at given co-ordinates
561 void BlitString(char *name
, int x
, int y
)
568 for (i
=0; name
[i
]; i
++)
571 c
= toupper(name
[i
]);
572 if (c
>= 'A' && c
<= 'Z')
575 copyXPMArea(c
* 6, 74, 6, 8, k
, y
);
579 { // its a number or symbol
581 copyXPMArea(c
* 6, 64, 6, 8, k
, y
);
588 void BlitNum(int num
, int x
, int y
)
593 sprintf(buf
, "%03i", num
);
595 BlitString(buf
, newx
, y
);
600 int ReadConfigString(FILE *fp
, char *setting
, char *value
)
615 sprintf(str
, "%s=", setting
);
618 fseek(fp
, 0, SEEK_SET
);
623 if (!fgets(buf
, 512, fp
))
629 for (i
=0; i
!=len
; i
++)
637 //printf("Scanning '%s'...\n", buf);
638 if ( strncmp(buf
, str
, strlen(str
)) == 0)
642 for(i
=0; i
!=slen
; i
++)
658 int ReadConfigInt(FILE *fp
, char *setting
, int *value
)
662 if (ReadConfigString(fp
, setting
, (char *) &buf
))
671 int Read_Config_File( char *filename
)
675 fp
= fopen(filename
, "r");
684 perror("Read_Config_File");
685 fprintf(stderr
, "Unable to open %s, no settings read.\n", filename
);
697 /*******************************************************************************\
699 \*******************************************************************************/
703 fprintf(stderr
, "\nWMiTIME - illusion <clarkd@skynet.ca> http://www.neotokyo.org/illusion\n\n");
704 fprintf(stderr
, "usage:\n");
705 fprintf(stderr
, " -12 12-hour mode\n");
706 fprintf(stderr
, " -display <display name>\n");
707 fprintf(stderr
, " -geometry +XPOS+YPOS initial window position\n");
708 // fprintf(stderr, " -c <filename> use specified config file\n");
709 fprintf(stderr
, " -h this help screen\n");
710 fprintf(stderr
, " -v print the version number\n");
711 fprintf(stderr
, "\n");
714 /*******************************************************************************\
716 \*******************************************************************************/
718 void printversion(void)
720 fprintf(stderr
, "wmitime v%s\n", WMITIME_VERSION
);