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