wmbiff: Use unsigned data type for port number.
[dockapps.git] / asbeats / asbeats.c
blob14f8075a5b07b4cf8dbe66cc5c4f4b8380914ff3
1 #include <stdio.h>
2 #include <X11/Xlib.h>
3 #include <X11/xpm.h>
4 #include <X11/extensions/shape.h>
5 #include <time.h>
6 #include <X11/Xatom.h>
7 #include <math.h>
9 #include "led.xpm"
10 #include "mask.xbm"
11 #include "mask.xpm"
13 int posx[4]={8,17,26,35};
14 static struct tm *clk;
16 Display *dpy;
17 Window Root;
18 int screen;
19 int d_depth;
20 XSizeHints mysizehints;
21 XWMHints mywmhints;
22 Pixel back_pix,fore_pix;
23 GC NormalGC;
24 Window iconwin,win;
25 char *LedColor="LightSeaGreen";
26 char *ERR_colorcells="not enough free color cells\n";
27 typedef struct _XpmIcon {
28 Pixmap pixmap;
29 Pixmap mask;
30 XpmAttributes attributes;
31 } XpmIcon;
33 XpmIcon wmclock,led;
34 XpmIcon visible;
35 time_t actualtime;
37 #define MW_EVENTS (ExposureMask | ButtonPressMask | StructureNotifyMask)
38 #define FALSE 0
39 void GetXPM(void);
40 static void CreatePixmap(Display *dpy, Window Root, char **data, XpmIcon* icon);
41 Pixel GetColor(char *name);
42 void RedrawWindow( XpmIcon *v);
43 void InsertTime();
45 int main(int argc,char *argv[])
47 int i;
48 unsigned int borderwidth;
49 char *display_name=NULL;
50 char *wname="asbeats";
51 XGCValues gcv;
52 unsigned long gcm;
53 XEvent Event;
54 XTextProperty name;
55 XClassHint classHint;
56 Pixmap pixmask;
57 char *Geometry="";
58 if (!(dpy = XOpenDisplay(display_name)))
60 fprintf(stderr,"asbeats: can't open display %s\n",
61 XDisplayName(display_name));
62 exit (1);
64 screen=DefaultScreen(dpy);
65 Root=RootWindow(dpy,screen);
66 d_depth=DefaultDepth(dpy,screen);
67 XConnectionNumber(dpy);
68 GetXPM();
69 mysizehints.flags=USSize|USPosition;
70 mysizehints.x=0;
71 mysizehints.y=0;
72 back_pix=GetColor("white");
73 fore_pix=GetColor("black");
74 XWMGeometry(dpy,screen,Geometry,NULL,(borderwidth =1),
75 &mysizehints,&mysizehints.x,&mysizehints.y,
76 &mysizehints.width,&mysizehints.height,&i);
77 mysizehints.width=wmclock.attributes.width;
78 mysizehints.height=wmclock.attributes.height;
79 win=XCreateSimpleWindow(dpy,Root,mysizehints.x,mysizehints.y,
80 mysizehints.width,mysizehints.height,borderwidth,
81 fore_pix,back_pix);
82 iconwin=XCreateSimpleWindow(dpy,win,mysizehints.x,mysizehints.y,
83 mysizehints.width,mysizehints.height,borderwidth,fore_pix,
84 back_pix);
85 XSetWMNormalHints(dpy,win,&mysizehints);
86 classHint.res_name="asbeats";
87 classHint.res_class="ASBeats";
88 XSetClassHint(dpy,win,&classHint);
89 XSelectInput(dpy,win,MW_EVENTS);
90 XSelectInput(dpy,iconwin,MW_EVENTS);
91 if(XStringListToTextProperty(&wname,1,&name)==0)
93 fprintf(stderr,"asbeats: can't allocate window name\n");
94 exit(-1);
96 XSetWMName(dpy, win, &name);
97 gcm = GCForeground|GCBackground|GCGraphicsExposures;
98 gcv.foreground = fore_pix;
99 gcv.background = back_pix;
100 gcv.graphics_exposures = FALSE;
101 NormalGC = XCreateGC(dpy, Root, gcm, &gcv);
102 pixmask = XCreateBitmapFromData(dpy, win, mask_bits, mask_width,
103 mask_height);
104 XShapeCombineMask(dpy, win, ShapeBounding, 0, 0, pixmask, ShapeSet);
105 XShapeCombineMask(dpy, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet);
106 mywmhints.initial_state = WithdrawnState;
107 mywmhints.icon_window = iconwin;
108 mywmhints.icon_x = mysizehints.x;
109 mywmhints.icon_y = mysizehints.y;
110 mywmhints.window_group = win;
111 mywmhints.flags = StateHint | IconWindowHint |
112 IconPositionHint | WindowGroupHint;
113 XSetWMHints(dpy, win, &mywmhints);
114 XSetCommand(dpy, win, argv, argc);
115 XMapWindow(dpy,win);
116 InsertTime();
117 RedrawWindow(&visible);
118 while(1)
120 if (actualtime != time(0))
122 InsertTime();
123 RedrawWindow(&visible);
126 while (XPending(dpy))
128 XNextEvent(dpy,&Event);
129 switch(Event.type)
131 case Expose:
132 if(Event.xexpose.count == 0 )
133 RedrawWindow(&visible);
134 break;
135 case DestroyNotify:
136 XCloseDisplay(dpy);
137 exit(0);
138 default:
139 break;
142 XFlush(dpy);
143 usleep(50000L);
145 return 0;
148 void nocolor(char *a, char *b)
150 fprintf(stderr,"asbeats: can't %s %s\n", a,b);
153 void GetXPM(void)
155 XColor col;
156 XWindowAttributes attributes;
157 char led1[22];
158 char led2[22];
159 int ret;
160 XGetWindowAttributes(dpy,Root,&attributes);
161 if(!XParseColor(dpy,attributes.colormap,LedColor,&col))
163 nocolor("parse",LedColor);
165 sprintf(&led1[0],". c #%4X%4X%4X",col.red,col.green,col.blue);
166 for(ret=10;ret<22;ret++)
167 if(led1[ret]==' ')
168 led1[ret]='0';
169 led_xpm[2] = &led1[0];
170 col.red=(col.red/10)*3;
171 col.green=(col.green/10)*3;
172 col.blue=(col.blue/10)*3;
173 sprintf(&led2[0],"X c #%4X%4X%4X",col.red,col.green,col.blue);
174 for(ret=10;ret<22;ret++)
175 if(led2[ret]==' ')
176 led2[ret]='0';
177 led_xpm[3]=&led2[0];
178 CreatePixmap(dpy,Root,mask_xpm,&wmclock);
179 CreatePixmap(dpy,Root,mask_xpm,&visible);
180 CreatePixmap(dpy,Root,led_xpm,&led);
183 static void CreatePixmap(Display *dpy, Window Root, char **data, XpmIcon* icon)
185 int ret;
186 icon->attributes.valuemask |= (XpmReturnPixels |
187 XpmReturnExtensions | XpmExactColors | XpmCloseness);
188 icon->attributes.exactColors=False;
189 icon->attributes.closeness=40000;
190 ret=XpmCreatePixmapFromData(dpy,Root,data,&(icon->pixmap),
191 &(icon->mask),&(icon->attributes));
192 if(ret != XpmSuccess)
194 fprintf(stderr,ERR_colorcells);
195 exit(1);
199 int flush_expose (Window w)
201 XEvent dummy;
202 int i=0;
203 while(XCheckTypedWindowEvent(dpy,w,Expose,&dummy))
204 i++;
205 return i;
208 void RedrawWindow( XpmIcon *v)
210 flush_expose (iconwin);
211 XCopyArea(dpy,v->pixmap,iconwin,NormalGC,0,0,v->attributes.width,
212 v->attributes.height,0,0);
213 flush_expose(win);
214 XCopyArea(dpy,v->pixmap,win,NormalGC,0,0,v->attributes.width,
215 v->attributes.height,0,0);
218 Pixel GetColor(char *name)
220 XColor color;
221 XWindowAttributes attributes;
222 XGetWindowAttributes(dpy,Root,&attributes);
223 color.pixel=0;
224 if (!XParseColor(dpy,attributes.colormap,name,&color))
225 nocolor("parse",name);
226 else if(!XAllocColor (dpy,attributes.colormap,&color))
227 nocolor("alloc",name);
228 return color.pixel;
231 void Beat()
233 float fTime;
235 fTime=(clk->tm_hour*3600+clk->tm_min*60+clk->tm_sec);
236 fTime=fTime+timezone+3600;
237 if (clk->tm_isdst)
238 fTime-=3600;
239 fTime=(fTime*1000)/86400;
240 if (fTime >= 1000)
241 fTime-=1000;
242 else
243 if (fTime < 0)
244 fTime+=1000;
246 XCopyArea(dpy,led.pixmap,visible.pixmap,NormalGC,
247 90,0,9,11,posx[0],15);
248 XCopyArea(dpy,led.pixmap,visible.pixmap,NormalGC,
249 9*(((int)fTime)/100),0,9,11,posx[1],15);
250 XCopyArea(dpy,led.pixmap,visible.pixmap,NormalGC,
251 9*((((int)fTime) / 10) % 10),0,9,11,posx[2],15);
252 XCopyArea(dpy,led.pixmap,visible.pixmap,NormalGC,
253 9*((int)fTime % 10),0,9,11,posx[3],15);
255 fTime = abs(((fTime - abs(fTime)) * 1000) + 0.5);
257 XCopyArea(dpy,led.pixmap,visible.pixmap,NormalGC,
258 99,0,9,11,posx[0],32);
259 XCopyArea(dpy,led.pixmap,visible.pixmap,NormalGC,
260 9*(((int)fTime)/100),0,9,11,posx[1],32);
261 XCopyArea(dpy,led.pixmap,visible.pixmap,NormalGC,
262 9*((((int)fTime) / 10) % 10),0,9,11,posx[2],32);
263 XCopyArea(dpy,led.pixmap,visible.pixmap,NormalGC,
264 9*((int)fTime % 10),0,9,11,posx[3],32);
266 return;
269 void InsertTime()
271 actualtime=time(0);
272 clk=localtime(&actualtime);
273 XCopyArea(dpy,wmclock.pixmap,visible.pixmap,NormalGC,
274 0,0,mysizehints.width,mysizehints.height,0,0);
275 Beat();
276 return;