]> icculus.org git repositories - mikachu/rspanel.git/blob - rspanel.c
8037d460fd89986801176cc5fe29b312ddb95953
[mikachu/rspanel.git] / rspanel.c
1
2 /**********************************************************
3  ** Rather Small Panel 0.8beta1 Copyright (c) 2006       **
4  ** By Mikael Magnusson                                  **
5  ** See file COPYING for license details.                **
6  **********************************************************/
7
8 #include <stdlib.h>
9 #include <string.h>
10 #include <time.h>
11 #include <sys/time.h>
12 #include <sys/types.h>
13 #include <unistd.h>
14
15 #include <X11/Xlib.h>
16 #include <X11/Xutil.h>
17 #include <X11/Xatom.h>
18 #include <iconv.h>
19
20 #undef HAVE_XPM
21 #ifdef HAVE_XPM
22 #include <X11/xpm.h>
23 #include "icon.xpm"
24 #endif
25
26 /* you can edit these */
27 #define MAX_TASK_WIDTH 500
28 #define ICONWIDTH 16
29 #define ICONHEIGHT 16
30 #define WINHEIGHT 24
31 #ifndef MIKACHU
32 #        define WINWIDTH 1280
33 #else
34 int WINWIDTH = 827;
35 #endif
36 #define FONT_NAME "-*-Technical*-m*-r-*-*-14-*-*"
37 #define XFT_FONT "Arial Unicode MS-12"
38 #define PANGO_FONT_PREF "Utopia"
39 //#define PANGO_FONT_PREF "Technical"
40 #define PANGO_FONT_SIZE 13
41 //#define PAGER         /* use a desktop pager? */
42 #define PAGER_DIGIT_WIDTH 6
43 #define PAGER_BUTTON_WIDTH 20
44
45 /* don't edit these */
46 #define TEXTPAD 6
47 #define GRILL_WIDTH 10
48
49 #ifdef PANGO
50 #undef XFT
51 #define XFT
52 #include <pango/pango.h>
53 #include <pango/pangoxft.h>
54 #endif
55 #ifdef XFT
56 #include <X11/Xft/Xft.h>
57 #endif
58
59 #include "rspanel.h"
60
61 Window rspanelwin;
62 Display *dd;
63 Window root_win;
64 Pixmap generic_icon;
65 Pixmap generic_mask;
66 GC fore_gc;
67 taskbar tb;
68 int scr_screen;
69 int scr_depth;
70 int scr_width;
71 int scr_height;
72 int text_y;
73 int pager_size;
74
75 #ifdef XFT
76 XftDraw *xftdraw;
77 #ifdef PANGO
78 PangoLayout *pl;
79 #else
80 XftFont *xfs;
81 #endif
82 #else
83 XFontStruct *xfs;
84 #endif
85
86 struct colors {
87     unsigned short red, green, blue;
88 } cols[] = {
89     {0xd75c, 0xd75c, 0xe75c},        /* 0. light gray */
90     {0xbefb, 0xbaea, 0xcefb},        /* 1. mid gray */
91     {0xaefb, 0xaaea, 0xfefb},        /* 2. dark gray */
92     {0xefbe, 0xefbe, 0xffbe},        /* 3. white */
93     {0x8617, 0x8207, 0x9617},        /* 4. darkest gray */
94     {0x0000, 0x0000, 0x1000},        /* 5. black */
95 };
96
97 #define PALETTE_COUNT (sizeof (cols) / sizeof (cols[0].red) / 3)
98
99 unsigned long palette[PALETTE_COUNT];
100
101 char *atom_names[] = {
102     /* clients */
103     "KWM_WIN_ICON",
104     "WM_STATE",
105     "_MOTIF_WM_HINTS",
106     "_NET_WM_STATE",
107     "_NET_WM_STATE_SKIP_TASKBAR",
108     "_NET_WM_STATE_SHADED",
109     "_NET_WM_STATE_BELOW",
110     "_NET_WM_STATE_HIDDEN",
111     "_NET_WM_DESKTOP",
112     "_NET_WM_WINDOW_TYPE",
113     "_NET_WM_WINDOW_TYPE_DOCK",
114     "_NET_WM_STRUT",
115     "_WIN_HINTS",
116     /* root */
117     "_NET_CLIENT_LIST",
118     "_NET_CLIENT_LIST_STACKING",
119     "_NET_NUMBER_OF_DESKTOPS",
120     "_NET_CURRENT_DESKTOP",
121     "_OB_WM_ACTION",
122     "_NET_WM_NAME",
123     "UTF8_STRING",
124     "_NET_ACTIVE_WINDOW",
125 };
126
127 #define ATOM_COUNT (sizeof (atom_names) / sizeof (atom_names[0]))
128
129 Atom atoms[ATOM_COUNT];
130
131 #define atom_KWM_WIN_ICON atoms[0]
132 #define atom_WM_STATE atoms[1]
133 #define atom__MOTIF_WM_HINTS atoms[2]
134 #define atom__NET_WM_STATE atoms[3]
135 #define atom__NET_WM_STATE_SKIP_TASKBAR atoms[4]
136 #define atom__NET_WM_STATE_SHADED atoms[5]
137 #define atom__NET_WM_STATE_BELOW atoms[6]
138 #define atom__NET_WM_STATE_HIDDEN atoms[7]
139 #define atom__NET_WM_DESKTOP atoms[8]
140 #define atom__NET_WM_WINDOW_TYPE atoms[9]
141 #define atom__NET_WM_WINDOW_TYPE_DOCK atoms[10]
142 #define atom__NET_WM_STRUT atoms[11]
143 #define atom__WIN_HINTS atoms[12]
144 #define atom__NET_CLIENT_LIST atoms[13]
145 #define atom__NET_CLIENT_LIST_STACKING atoms[14]
146 #define atom__NET_NUMBER_OF_DESKTOPS atoms[15]
147 #define atom__NET_CURRENT_DESKTOP atoms[16]
148 #define atom__OB_WM_ACTION atoms[17]
149 #define atom__NET_WM_NAME atoms[18]
150 #define atom_STRING_UTF8 atoms[19]
151 #define atom__NET_ACTIVE_WINDOW atoms[20]
152
153 void *get_prop_data(Window win, Atom prop, Atom type, int *items)
154 {
155     Atom type_ret;
156     int format_ret;
157     unsigned long items_ret;
158     unsigned long after_ret;
159     unsigned char *prop_data;
160
161     prop_data = 0;
162
163     XGetWindowProperty(dd, win, prop, 0, 0x7fffffff, False, type, &type_ret,
164                        &format_ret, &items_ret, &after_ret, &prop_data);
165     if (items)
166         *items = items_ret;
167
168     return prop_data;
169 }
170
171 void set_foreground(int index)
172 {
173     XSetForeground(dd, fore_gc, palette[index]);
174 }
175
176 void draw_line(int x, int y, int a, int b)
177 {
178     XDrawLine(dd, tb.win, fore_gc, x, y, a, b);
179 }
180
181 void fill_rect(int x, int y, int a, int b)
182 {
183     XFillRectangle(dd, tb.win, fore_gc, x, y, a, b);
184 }
185
186 void scale_icon(task * tk)
187 {
188     int xx, yy, x, y;
189     unsigned int w, h, d, bw;
190     Pixmap pix, mk = None;
191     XGCValues gcv;
192     GC mgc = mgc;
193
194     XGetGeometry(dd, tk->icon, &pix, &x, &y, &w, &h, &bw, &d);
195     pix = XCreatePixmap(dd, tk->win, ICONWIDTH, ICONHEIGHT, scr_depth);
196
197     if (tk->mask != None) {
198         mk = XCreatePixmap(dd, tk->win, ICONWIDTH, ICONHEIGHT, 1);
199         gcv.subwindow_mode = IncludeInferiors;
200         gcv.graphics_exposures = False;
201         mgc = XCreateGC(dd, mk, GCGraphicsExposures | GCSubwindowMode, &gcv);
202     }
203
204     set_foreground(3);
205
206     /* this is my simple & dirty scaling routine */
207     for (y = ICONHEIGHT - 1; y >= 0; y--) {
208         yy = (y * h) / ICONHEIGHT;
209         for (x = ICONWIDTH - 1; x >= 0; x--) {
210             xx = (x * w) / ICONWIDTH;
211             if (d != scr_depth)
212                 XCopyPlane(dd, tk->icon, pix, fore_gc, xx, yy, 1, 1, x, y, 1);
213             else
214                 XCopyArea(dd, tk->icon, pix, fore_gc, xx, yy, 1, 1, x, y);
215             if (mk != None)
216                 XCopyArea(dd, tk->mask, mk, mgc, xx, yy, 1, 1, x, y);
217         }
218     }
219
220     if (mk != None) {
221         XFreeGC(dd, mgc);
222         tk->mask = mk;
223     }
224
225     tk->icon = pix;
226 }
227
228 void get_task_hinticon(task * tk)
229 {
230     XWMHints *hin;
231
232     if (tk->icon != None && tk->icon != generic_icon)
233         if (tk->icon_copied) {
234             XFreePixmap(dd, tk->icon);
235             if (tk->mask != None && tk->mask != generic_mask)
236                 XFreePixmap(dd, tk->mask);
237         }
238
239     tk->icon = None;
240     tk->mask = None;
241
242     hin = (XWMHints *)get_prop_data(tk->win, XA_WM_HINTS, XA_WM_HINTS, 0);
243     if (hin) {
244         if ((hin->flags & IconPixmapHint)) {
245             if ((hin->flags & IconMaskHint)) {
246                 tk->mask = hin->icon_mask;
247             }
248
249             tk->icon = hin->icon_pixmap;
250             tk->icon_copied = 1;
251             scale_icon(tk);
252         }
253         XFree(hin);
254     }
255
256     if (tk->icon == None) {
257         tk->icon = generic_icon;
258         if (tk->mask != None)
259             XFreePixmap(dd, tk->mask);
260         tk->mask = generic_mask;
261     }
262 }
263
264 void get_task_kdeicon(task * tk)
265 {
266     unsigned long *data;
267
268     data = get_prop_data(tk->win, atom_KWM_WIN_ICON, atom_KWM_WIN_ICON, 0);
269     if (data) {
270         tk->icon = data[0];
271         tk->mask = data[1];
272         XFree(data);
273     }
274 }
275
276 int generic_get_int(Window win, Atom at)
277 {
278     int num = 0;
279     unsigned long *data;
280
281     data = get_prop_data(win, at, XA_CARDINAL, 0);
282     if (data) {
283         num = *data;
284         XFree(data);
285     }
286     return num;
287 }
288
289 int find_desktop(Window win)
290 {
291     return generic_get_int(win, atom__NET_WM_DESKTOP);
292 }
293
294 int is_hidden(Window win)
295 {
296     unsigned long *data;
297     int ret = 0;
298     int num;
299
300     data = get_prop_data(win, atom__NET_WM_STATE, XA_ATOM, &num);
301     if (data) {
302         while (num) {
303             num--;
304             if ((data[num]) == atom__NET_WM_STATE_SKIP_TASKBAR)
305                 ret = 1;
306         }
307         XFree(data);
308     }
309     return ret;
310 }
311
312 int is_iconified(Window win)
313 {
314     unsigned long *data;
315     int ret = 0;
316     int num;
317
318     data = get_prop_data(win, atom__NET_WM_STATE, XA_ATOM, &num);
319     if (data) {
320         while (num) {
321             num--;
322             if ((data[num]) == atom__NET_WM_STATE_HIDDEN)
323                 ret = 1;
324         }
325         XFree(data);
326     }
327     return ret;
328 }
329
330 int is_shaded(Window win)
331 {
332     unsigned long *data;
333     int ret = 0;
334     int num;
335
336     data = get_prop_data(win, atom__NET_WM_STATE, XA_ATOM, &num);
337     if (data) {
338         while (num) {
339             num--;
340             if ((data[num]) == atom__NET_WM_STATE_SHADED)
341                 ret = 1;
342         }
343         XFree(data);
344     }
345     return ret;
346 }
347
348 int get_current_desktop(void)
349 {
350     return generic_get_int(root_win, atom__NET_CURRENT_DESKTOP);
351 }
352
353 int get_number_of_desktops(void)
354 {
355     return generic_get_int(root_win, atom__NET_NUMBER_OF_DESKTOPS);
356 }
357
358 void add_task(Window win, int focus)
359 {
360     task *tk, *list;
361     int desk;
362
363     if (win == tb.win)
364         return;
365
366     /* is this window on a different desktop? */
367     desk = find_desktop(win);
368     if ((desk != 0xffffffff && tb.my_desktop != desk) || is_hidden(win))
369         return;
370
371     tk = calloc(1, sizeof(task));
372     tk->win = win;
373     tk->focused = focus;
374     tk->name = get_prop_data(tk->win, atom__NET_WM_NAME, atom_STRING_UTF8, 0) ?:
375                get_prop_data(tk->win, XA_WM_NAME, XA_STRING, 0);
376 //    tk->name = get_prop_data(win, XA_WM_NAME, XA_STRING, 0);
377     //tk->locale = get_prop_data(win, XA_WM_LOCALE_NAME, XA_STRING, 0);
378     tk->iconified = is_iconified(win);
379     tk->shaded = is_shaded(win);
380
381     get_task_kdeicon(tk);
382     if (tk->icon == None)
383         get_task_hinticon(tk);
384
385     XSelectInput(dd, win, PropertyChangeMask | FocusChangeMask
386                                              | StructureNotifyMask);
387
388     /* now append it to our linked list */
389     tb.num_tasks++;
390
391     list = tb.task_list;
392     if (!list) {
393         tb.task_list = tk;
394         return;
395     }
396     while (1) {
397         if (!list->next) {
398             list->next = tk;
399             return;
400         }
401         list = list->next;
402     }
403 }
404
405 void gui_sync(void)
406 {
407     XSync(dd, False);
408 }
409
410 void set_prop(Window win, Atom at, Atom type, long val)
411 {
412     XChangeProperty(dd, win, at, type, 32,
413                     PropModeReplace, (unsigned char *)&val, 1);
414 }
415
416 Window gui_create_taskbar(void)
417 {
418     Window win;
419     MWMHints mwm;
420     XSizeHints size_hints;
421     XWMHints wmhints;
422     XSetWindowAttributes att;
423
424     att.background_pixel = palette[0];
425     att.event_mask = ButtonPressMask | ExposureMask;
426
427     win = rspanelwin = XCreateWindow(/* display  */ dd,
428                                      /* parent   */ root_win,
429                                      /* x        */ 0,
430                                      /* y        */ scr_height - WINHEIGHT,
431                                      /* width    */ WINWIDTH,
432                                      /* height   */ WINHEIGHT,
433                                      /* border   */ 0,
434                                      /* depth    */ CopyFromParent,
435                                      /* class    */ InputOutput,
436                                      /* visual   */ CopyFromParent,
437                                      /*value mask*/ CWBackPixel | CWEventMask,
438                                      /* attribs  */ &att);
439
440     /* reside on ALL desktops */
441     set_prop(win, atom__NET_WM_DESKTOP, XA_CARDINAL, 0xFFFFFFFF);
442     set_prop(win, atom__NET_WM_WINDOW_TYPE, XA_ATOM,
443              atom__NET_WM_WINDOW_TYPE_DOCK);
444     set_prop(win, atom__NET_WM_STATE, XA_ATOM, atom__NET_WM_STATE_BELOW);
445     /* use old gnome hint since sawfish doesn't support _NET_WM_STRUT */
446     set_prop(win, atom__WIN_HINTS, XA_CARDINAL,
447              WIN_HINTS_SKIP_FOCUS | WIN_HINTS_SKIP_WINLIST |
448              WIN_HINTS_SKIP_TASKBAR | WIN_HINTS_DO_NOT_COVER);
449     XChangeProperty(dd, win, XA_WM_NAME, XA_STRING, 8, PropModeReplace,
450                     (unsigned char *)"rspanel", 7);
451
452     /* borderless motif hint */
453     bzero(&mwm, sizeof(mwm));
454     mwm.flags = MWM_HINTS_DECORATIONS;
455     XChangeProperty(dd, win, atom__MOTIF_WM_HINTS, atom__MOTIF_WM_HINTS, 32,
456                     PropModeReplace, (unsigned char *)&mwm,
457                     sizeof(MWMHints) / 4);
458
459     /* make sure the WM obays our window position */
460     size_hints.flags = PPosition;
461     /*XSetWMNormalHints (dd, win, &size_hints); */
462     XChangeProperty(dd, win, XA_WM_NORMAL_HINTS, XA_WM_SIZE_HINTS, 32,
463                     PropModeReplace, (unsigned char *)&size_hints,
464                     sizeof(XSizeHints) / 4);
465
466     /* make our window unfocusable */
467     wmhints.flags = InputHint;
468     wmhints.input = False;
469     /*XSetWMHints (dd, win, &wmhints); */
470     XChangeProperty(dd, win, XA_WM_HINTS, XA_WM_HINTS, 32, PropModeReplace,
471                     (unsigned char *)&wmhints, sizeof(XWMHints) / 4);
472
473     XMapWindow(dd, win);
474
475 #ifdef XFT
476     xftdraw = XftDrawCreate(dd, win, DefaultVisual(dd, scr_screen),
477                             DefaultColormap(dd, scr_screen));
478     g_type_init();
479 #endif
480
481     return win;
482 }
483
484 void gui_init(void)
485 {
486     XGCValues gcv;
487     XColor xcl;
488     unsigned int i;
489 #ifndef XFT
490     char *fontname;
491 #endif
492 #ifdef PANGO
493     PangoContext *context;
494     PangoFontDescription *pfd;
495 #endif
496
497     i = 0;
498     do {
499         xcl.red = cols[i].red;
500         xcl.green = cols[i].green;
501         xcl.blue = cols[i].blue;
502         XAllocColor(dd, DefaultColormap(dd, scr_screen), &xcl);
503         palette[i] = xcl.pixel;
504         i++;
505     } while (i < PALETTE_COUNT);
506
507 #ifdef PANGO
508 #elif XFT
509     xfs = XftFontOpenName(dd, scr_screen, XFT_FONT);
510 #else
511     fontname = FONT_NAME;
512     do {
513         xfs = XLoadQueryFont(dd, fontname);
514         fontname = "fixed";
515     } while (!xfs);
516 #endif
517
518     gcv.graphics_exposures = False;
519 #ifndef PANGO
520 # ifdef XFT
521     text_y = xfs->ascent + ((WINHEIGHT - (xfs->ascent + xfs->descent)) / 2);
522     fore_gc = XCreateGC(dd, root_win, GCGraphicsExposures, &gcv);
523 # else
524     text_y = xfs->ascent + ((WINHEIGHT - xfs->ascent) / 2);
525     gcv.font = xfs->fid;
526     fore_gc = XCreateGC(dd, root_win, GCFont | GCGraphicsExposures, &gcv);
527 # endif
528 #else
529     pfd = pango_font_description_new();
530     pango_font_description_set_absolute_size(pfd, PANGO_FONT_SIZE*PANGO_SCALE);
531     pango_font_description_set_family(pfd, PANGO_FONT_PREF);
532     context = pango_xft_get_context(dd, root_win);
533     pango_context_set_font_description(context, pfd);
534     pl = pango_layout_new(context);
535     
536     pango_layout_set_single_paragraph_mode(pl, TRUE);
537     pango_layout_set_ellipsize(pl, PANGO_ELLIPSIZE_END);
538
539     text_y = (WINHEIGHT*PANGO_SCALE-7*PANGO_SCALE);
540     pango_font_description_free(pfd);
541     g_object_unref(context);
542
543     fore_gc = XCreateGC(dd, root_win, GCGraphicsExposures, &gcv);
544 #endif
545
546 #ifdef HAVE_XPM
547     XpmCreatePixmapFromData(dd, root_win, icon_xpm, &generic_icon,
548                             &generic_mask, NULL);
549 #else
550     generic_icon = 0;
551 #endif
552 }
553
554 void gui_draw_vline(int x)
555 {
556     set_foreground(4);
557     draw_line(x, 0, x, WINHEIGHT);
558     set_foreground(3);
559     draw_line(x + 1, 0, x + 1, WINHEIGHT);
560 }
561
562 void draw_box(int x, int width)
563 {
564     set_foreground(1);                /* mid gray */
565     fill_rect(x + 3, 2, width - 2, WINHEIGHT - 4);
566
567     set_foreground(3);                /* white */
568     draw_line(x + 3, WINHEIGHT - 2, x + width - 1, WINHEIGHT - 2);
569     draw_line(x + width - 1, 1, x + width - 1, WINHEIGHT - 2);
570
571     set_foreground(4);                /* darkest gray */
572     draw_line(x + 3, 1, x + width - 1, 1);
573     draw_line(x + 3, 2, x + 3, WINHEIGHT - 3);
574 }
575
576 void gui_draw_task(task * tk)
577 {
578     int x = tk->pos_x;
579     int taskw = tk->width;
580 #ifdef XFT
581 #ifndef PANGO
582     int len;
583     XGlyphInfo ext;
584 #endif
585     XftColor col;
586 #endif
587
588     gui_draw_vline(x);
589
590     if (tk->focused) {
591         draw_box(x, taskw);
592     } else {
593         set_foreground(0);        /* mid gray */
594         fill_rect(x + 2, 0, taskw - 1, WINHEIGHT);
595     }
596
597     if (tk->name) {
598         int text_x = x + TEXTPAD + TEXTPAD + (tk->icon ? ICONWIDTH : -1*ICONWIDTH/4);
599 #define SETCOL(x) col.color.red = cols[x].red;\
600                   col.color.green = cols[x].green;\
601                   col.color.blue = cols[x].blue;
602 #ifdef PANGO
603
604     pango_layout_set_width(pl, /*-1);*/(taskw - text_x + x) * PANGO_SCALE);
605     pango_layout_set_text(pl, tk->name, -1);
606     col.color.alpha = 0xffff;
607
608     if (tk->iconified) {
609         SETCOL(3)
610         pango_xft_render_layout_line(xftdraw, &col, pango_layout_get_line(pl, 0), (text_x+2)*PANGO_SCALE, text_y + 2);
611         SETCOL(4)
612     } else if (tk->shaded) {
613         SETCOL(3)
614         pango_xft_render_layout_line(xftdraw, &col, pango_layout_get_line(pl, 0), (text_x-2)*PANGO_SCALE, text_y - 2);
615         SETCOL(4)
616     } else {
617         SETCOL(5)
618     }
619     pango_xft_render_layout_line(xftdraw, &col, pango_layout_get_line(pl, 0), text_x*PANGO_SCALE, text_y);
620     
621 #elif XFT
622
623         /* check how many chars can fit */
624         len = strlen(tk->name);
625         while (1) {
626             XftTextExtentsUtf8(dd, xfs, tk->name, len, &ext);
627             if (ext.width < taskw - (text_x - x) - 2 || len <= 0)
628                 break;
629             len--;
630         }
631
632         col.color.alpha = 0xffff;
633
634         if (tk->iconified) {
635             /* draw task's name dark (iconified) */
636             SETCOL(3)
637             XftDrawStringUtf8(xftdraw, &col, xfs, text_x, text_y + 1, tk->name,
638                            len);
639             SETCOL(4)
640         } else if (tk->shaded) {
641             /* draw task's name dark (shaded) */
642             SETCOL(3)
643             XftDrawStringUtf8(xftdraw, &col, xfs, text_x, text_y - 1, tk->name,
644                            len);
645             SETCOL(4)
646         } else {
647             SETCOL(5)
648 #undef SETCOL
649         }
650
651         /* draw task's name here */
652         XftDrawStringUtf8(xftdraw, &col, xfs, text_x, text_y, tk->name, len);
653 #else
654
655         /* check how many chars can fit */
656         len = strlen(tk->name);
657
658         while (XTextWidth(xfs, tk->name, len) >= taskw - (text_x - x) - 2
659                && len > 0)
660             len--;
661
662         if (tk->iconified) {
663             /* draw task's name dark (iconified) */
664             set_foreground(3);
665             XDrawString(dd, tb.win, fore_gc, text_x, text_y + 1, tk->name,
666                         len);
667             set_foreground(4);
668         } else if (tk->shaded) {
669             /* draw task's name dark (shaded) */
670             set_foreground(3);
671             XDrawString(dd, tb.win, fore_gc, text_x, text_y - 1, tk->name,
672                         len);
673             set_foreground(4);
674         } else {
675             set_foreground(5);
676         }
677
678         /* draw task's name here */
679         XDrawString(dd, tb.win, fore_gc, text_x, text_y, tk->name, len);
680 #endif
681     }
682
683 #ifndef HAVE_XPM
684     if (!tk->icon)
685         return;
686 #endif
687
688     /* draw the task's icon */
689     XSetClipMask(dd, fore_gc, tk->mask);
690     XSetClipOrigin(dd, fore_gc, x + TEXTPAD, (WINHEIGHT - ICONHEIGHT) / 2);
691     XCopyArea(dd, tk->icon, tb.win, fore_gc, 0, 0, ICONWIDTH, ICONHEIGHT,
692               x + TEXTPAD, (WINHEIGHT - ICONHEIGHT) / 2);
693     XSetClipMask(dd, fore_gc, None);
694 }
695
696 void draw_dot(int x, int y)
697 {
698     set_foreground(4);
699     XDrawPoint(dd, tb.win, fore_gc, x, y);
700     set_foreground(3);
701     XDrawPoint(dd, tb.win, fore_gc, x + 1, y + 1);
702 }
703
704 void draw_grill(int x)
705 {
706     int y = 0;
707     while (y < WINHEIGHT - 4) {
708         y += 3;
709         draw_dot(x + 3, y);
710         draw_dot(x, y);
711     }
712 }
713
714 void ob_action(Window win, char *action)
715 {
716     XClientMessageEvent xev;
717     char act_name[14+strlen(action)];
718     sprintf(act_name, "_OB_WM_ACTION_%s", action);
719
720     xev.type = ClientMessage;
721     xev.window = win;
722     xev.message_type = atom__OB_WM_ACTION;
723     xev.format = 32;
724     /*strncpy(xev.data.b, action, 20);*/
725     xev.data.l[0] = XInternAtom(dd, act_name, False);
726     XSendEvent(dd, root_win, False, SubstructureRedirectMask, (XEvent *)&xev);
727 }
728
729 #ifdef PAGER
730
731 void switch_desk(int new_desk)
732 {
733     XClientMessageEvent xev;
734
735     if (get_number_of_desktops() <= new_desk)
736         return;
737
738     xev.type = ClientMessage;
739     xev.window = root_win;
740     xev.message_type = atom__NET_CURRENT_DESKTOP;
741     xev.format = 32;
742     xev.data.l[0] = new_desk;
743     XSendEvent(dd, root_win, False, SubstructureRedirectMask, (XEvent *)&xev);
744 }
745
746 void pager_draw_button(int x, int num)
747 {
748     char label;
749 #ifdef XFT
750     XftColor col;
751 #endif
752
753     if (num == tb.my_desktop) {
754         /* current desktop */
755         draw_box(x, PAGER_BUTTON_WIDTH);
756     } else {
757         set_foreground(0);
758         fill_rect(x, 1, PAGER_BUTTON_WIDTH + 1, WINHEIGHT - 2);
759     }
760
761     label = '1' + num;
762
763 #ifdef XFT
764     col.color.alpha = 0xffff;
765     col.color.red = cols[5].red;
766     col.color.green = cols[5].green;
767     col.color.blue = cols[5].blue;
768     XftDrawString8(xftdraw, &col, xfs,
769                    x + ((PAGER_BUTTON_WIDTH - PAGER_DIGIT_WIDTH) / 2), text_y,
770                    &label, 1);
771 #else
772     set_foreground(5);
773     XDrawString(dd, tb.win, fore_gc,
774                 x + ((PAGER_BUTTON_WIDTH - PAGER_DIGIT_WIDTH) / 2) - 1,
775                 text_y, &label, 1);
776 #endif
777 }
778
779 void pager_draw(void)
780 {
781     int desks, i, x = GRILL_WIDTH;
782
783     desks = get_number_of_desktops();
784
785     for (i = 0; i < desks; i++) {
786         pager_draw_button(x, i);
787         if (i > 8)
788             break;
789         x += PAGER_BUTTON_WIDTH;
790     }
791
792     pager_size = x;
793 }
794
795 #endif
796
797 void gui_draw_taskbar(void)
798 {
799     task *tk;
800     int x, width, taskw;
801
802 #ifdef PAGER
803     pager_draw();
804 #else
805     pager_size = TEXTPAD;
806 #endif
807
808     width = WINWIDTH - (pager_size + GRILL_WIDTH + GRILL_WIDTH);
809     x = pager_size + 2;
810
811     if (tb.num_tasks == 0)
812         goto clear;
813
814     taskw = width / tb.num_tasks;
815     if (taskw > MAX_TASK_WIDTH)
816         taskw = MAX_TASK_WIDTH;
817
818     tk = tb.task_list;
819     while (tk) {
820         tk->pos_x = x;
821         tk->width = taskw - 1;
822         gui_draw_task(tk);
823         x += taskw;
824         tk = tk->next;
825     }
826
827     if (x < (width + pager_size + 2)) {
828 clear:
829         gui_draw_vline(x);
830         set_foreground(0);
831         fill_rect(x + 2, 0, WINWIDTH, WINHEIGHT);
832     }
833
834     gui_draw_vline(8);
835     gui_draw_vline(WINWIDTH - 8);
836
837     draw_grill(2);
838     draw_grill(WINWIDTH - 6);
839 }
840
841 task *find_task(Window win)
842 {
843     task *list = tb.task_list;
844     while (list) {
845         if (list->win == win)
846             return list;
847         list = list->next;
848     }
849     return 0;
850 }
851
852 void del_task(Window win)
853 {
854     task *next, *prev = 0, *list = tb.task_list;
855
856     while (list) {
857         next = list->next;
858         if (list->win == win) {
859             /* unlink and free this task */
860             tb.num_tasks--;
861             if (list->icon_copied) {
862                 XFreePixmap(dd, list->icon);
863                 if (list->mask != None)
864                     XFreePixmap(dd, list->mask);
865             }
866             if (list->name)
867                 XFree(list->name);
868             free(list);
869             if (prev == 0)
870                 tb.task_list = next;
871             else
872                 prev->next = next;
873             return;
874         }
875         prev = list;
876         list = next;
877     }
878 }
879
880 void move_taskbar(void)
881 {
882     int x, y;
883
884     x = y = 0;
885
886     if (tb.hidden)
887         x = TEXTPAD - WINWIDTH;
888
889     if (!tb.at_top)
890         y = scr_height - WINHEIGHT;
891
892     XMoveWindow(dd, tb.win, x, y);
893 }
894
895 void taskbar_read_clientlist(void)
896 {
897     Window *win, focus_win = 0;
898     int num, i, desk, new_desk = 0;
899     task *list, *next;
900     desk = get_current_desktop();
901 #ifdef MIKACHU
902     if (desk == 0)
903         WINWIDTH = 827;
904     else
905         WINWIDTH = 1280;
906
907     XResizeWindow(dd, rspanelwin, WINWIDTH, WINHEIGHT);
908     move_taskbar();
909 #endif
910     if (desk != tb.my_desktop) {
911         new_desk = 1;
912         tb.my_desktop = desk;
913     }
914
915     win = get_prop_data(root_win, atom__NET_ACTIVE_WINDOW, XA_WINDOW, &num);
916     if (win && num > 0)
917         focus_win = win[0];
918
919     win = get_prop_data(root_win, atom__NET_CLIENT_LIST, XA_WINDOW, &num);
920     if (!win)
921         return;
922
923     /* remove windows that arn't in the _NET_CLIENT_LIST anymore */
924     list = tb.task_list;
925     while (list) {
926         list->focused = (focus_win == list->win);
927         next = list->next;
928
929         if (!new_desk)
930             for (i = num - 1; i >= 0; i--)
931                 if (list->win == win[i] && !is_hidden(win[i]))
932                     goto dontdel;
933         del_task(list->win);
934 dontdel:
935
936         list = next;
937     }
938
939     /* add any new windows */
940     for (i = 0; i < num; i++) {
941         if (!find_task(win[i]))
942             add_task(win[i], (win[i] == focus_win));
943     }
944
945     XFree(win);
946 }
947
948 void handle_press(int x, int y, int button)
949 {
950     task *tk;
951
952 #ifdef PAGER
953     if ((y > 2 && y < WINHEIGHT - 2 && x > GRILL_WIDTH) && button == 1)
954         switch_desk((x - GRILL_WIDTH) / PAGER_BUTTON_WIDTH);
955     else
956 #endif
957     /* clicked left grill */
958     if (x < 6) {
959         if (button == 1) {
960             tb.at_top = 1 - tb.at_top;
961             move_taskbar();
962         } else if (button == 4) {
963             tb.hidden = 1;
964             move_taskbar();
965         }
966     }
967
968     /* clicked right grill */
969     else if (x + TEXTPAD > WINWIDTH) {
970         if (tb.hidden && (button == 1 || button == 5)) {
971             tb.hidden = 0;
972             move_taskbar();
973         } else if (!tb.hidden && (button == 1 || button == 4)) {
974             tb.hidden = 1;
975             move_taskbar();
976         }
977     } else {
978         tk = tb.task_list;
979         while (tk) {
980             /* clicked on a task button */
981             if (x > tk->pos_x && x < tk->pos_x + tk->width) {
982                 switch (button) {
983                     case 1:
984                         if (tk->iconified && tk->shaded)
985                             ob_action(tk->win, "unshade");
986                         ob_action(tk->win, "activate");
987                         break;
988                     case 2:
989                         if (tk->iconified)
990                             ob_action(tk->win, "deiconify");
991                         else
992                             ob_action(tk->win, "iconify");
993                         break;
994                     case 3:
995                         ob_action(tk->win, "raiselower");
996                         break;
997                     case 4:
998                         ob_action(tk->win, "shadelower");
999                         break;
1000                     case 5:
1001                         ob_action(tk->win, "unshaderaise");
1002                         break;
1003                     case 9:
1004                         if (tk->iconified)
1005                             ob_action(tk->win, "deiconify");
1006                         ob_action(tk->win, "focus");
1007                         break;
1008                     case 6:
1009                         ob_action(tk->win, "lower");
1010                         break;
1011                     case 7:
1012                         ob_action(tk->win, "raise");
1013                         break;
1014                 }
1015                 return;
1016             }
1017             tk = tk->next;
1018         } /* clicked on the background */
1019         switch (button) {
1020             case 1:
1021                 ob_action(tb.win, "raise");
1022                 break;
1023             case 2:
1024                 ob_action(tb.win, "lower");
1025                 break;
1026             case 3:
1027                 ob_action(tb.win, "raiselower");
1028                 break;
1029             case 4:
1030                 tb.hidden = 1;
1031                 move_taskbar();
1032                 break;
1033             case 5:
1034                 tb.hidden = 0;
1035                 move_taskbar();
1036                 break;
1037         }
1038     }
1039 }
1040
1041 void handle_focusin(Window win)
1042 {
1043     task *tk;
1044
1045     tk = tb.task_list;
1046     while (tk) {
1047         if (tk->focused) {
1048             if (tk->win != win) {
1049                 tk->focused = 0;
1050                 gui_draw_task(tk);
1051             }
1052         } else {
1053             if (tk->win == win) {
1054                 tk->focused = 1;
1055                 gui_draw_task(tk);
1056             }
1057         }
1058         tk = tk->next;
1059     }
1060 }
1061
1062 void handle_propertynotify(Window win, Atom at)
1063 {
1064     task *tk;
1065
1066     if (win == root_win) {
1067         if (at == atom__NET_CLIENT_LIST || at == atom__NET_CURRENT_DESKTOP || at == atom__NET_CLIENT_LIST_STACKING) {
1068             taskbar_read_clientlist();
1069             gui_draw_taskbar();
1070         }
1071         return;
1072     }
1073
1074     tk = find_task(win);
1075     if (!tk)
1076         return;
1077
1078     if (at == XA_WM_NAME || at == atom__NET_WM_NAME) {
1079         /* window's title changed */
1080         char *newname;
1081         newname = get_prop_data(tk->win, atom__NET_WM_NAME, atom_STRING_UTF8, 0) ?:
1082                   get_prop_data(tk->win, XA_WM_NAME, XA_STRING, 0);
1083         if (newname) {
1084             if (tk->name && !strcmp(newname, tk->name)) {
1085                 XFree(newname);
1086                 return;
1087             }
1088         }
1089         if (tk->name)
1090             XFree(tk->name);
1091         tk->name = newname;
1092         gui_draw_task(tk);
1093     } else if (at == atom__NET_WM_STATE) {
1094         /* iconified state changed? */
1095         if (is_iconified(tk->win) != tk->iconified) {
1096             tk->iconified = !tk->iconified;
1097             gui_draw_task(tk);
1098         }
1099         /* shaded state changed? */
1100         if (is_shaded(tk->win) != tk->shaded) {
1101             tk->shaded = !tk->shaded;
1102             gui_draw_task(tk);
1103         }
1104     } else if (at == XA_WM_HINTS) {
1105         /* some windows set their WM_HINTS icon after mapping */
1106         //if (tk->icon == generic_icon) {
1107             get_task_hinticon(tk);
1108             gui_draw_task(tk);
1109         //}
1110     } else if (at == atom__NET_WM_DESKTOP) {
1111         if (find_desktop(win) != get_current_desktop())
1112             del_task(tk->win);
1113     }
1114 }
1115
1116 void handle_error(Display * d, XErrorEvent * ev)
1117 {
1118 }
1119
1120 int
1121 #ifdef NOSTDLIB
1122 _start(void)
1123 #else
1124 main(int argc, char *argv[])
1125 #endif
1126 {
1127     XEvent ev;
1128     fd_set fd;
1129     int xfd;
1130
1131     dd = XOpenDisplay(NULL);
1132     if (!dd)
1133         return 0;
1134     scr_screen = DefaultScreen(dd);
1135     scr_depth = DefaultDepth(dd, scr_screen);
1136     scr_height = DisplayHeight(dd, scr_screen);
1137     scr_width = DisplayWidth(dd, scr_screen);
1138     root_win = RootWindow(dd, scr_screen);
1139
1140     /* helps us catch windows closing/opening */
1141     XSelectInput(dd, root_win, PropertyChangeMask);
1142
1143     XSetErrorHandler((XErrorHandler) handle_error);
1144
1145     XInternAtoms(dd, atom_names, ATOM_COUNT, False, atoms);
1146
1147     gui_init();
1148     bzero(&tb, sizeof(struct taskbar));
1149     tb.win = gui_create_taskbar();
1150     xfd = ConnectionNumber(dd);
1151     gui_sync();
1152
1153     while (1) {
1154         FD_ZERO(&fd);
1155         FD_SET(xfd, &fd);
1156         select(xfd + 1, &fd, 0, 0, 0);
1157
1158         while (XPending(dd)) {
1159             XNextEvent(dd, &ev);
1160             switch (ev.type) {
1161             case ButtonPress:
1162                 handle_press(ev.xbutton.x, ev.xbutton.y, ev.xbutton.button);
1163                 break;
1164             case DestroyNotify:
1165                 del_task(ev.xdestroywindow.window);
1166                 /* fall through */
1167             case Expose:
1168                 gui_draw_taskbar();
1169                 break;
1170             case PropertyNotify:
1171                 handle_propertynotify(ev.xproperty.window, ev.xproperty.atom);
1172                 break;
1173             case FocusIn:
1174                 if (ev.xfocus.mode != NotifyGrab)
1175                     handle_focusin(ev.xfocus.window);
1176                 break;
1177             /*default:
1178                    printf ("unknown evt type: %d\n", ev.type); */
1179             }
1180         }
1181     }
1182
1183     /*XCloseDisplay (dd);
1184
1185        return 0; */
1186 }
1187