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