2 /**********************************************************
3 ** Rather Small Panel 0.8beta1 Copyright (c) 2006 **
4 ** By Mikael Magnusson **
5 ** See file COPYING for license details. **
6 **********************************************************/
12 #include <sys/types.h>
16 #include <X11/Xutil.h>
17 #include <X11/Xatom.h>
20 /* you can edit these */
21 #define MAX_TASK_WIDTH 500
24 /* XXX get this from the font */
27 # define WINWIDTH scr_width
32 /* don't edit these */
34 #define GRILL_WIDTH 10
36 #include <openbox/render.h>
37 #include <openbox/theme.h>
54 RrAppearance *background;
55 RrAppearance *focused_task;
56 RrAppearance *iconified_task;
57 RrAppearance *unfocused_task;
58 RrAppearance *normal_text;
59 RrAppearance *iconified_text;
60 RrAppearance *shaded_text;
61 RrAppearance *focused_text;
64 /* we draw stuff to this guy then set him as a window
65 * background pixmap, yay no flickering! */
68 char *atom_names[] = {
74 "_NET_WM_STATE_SKIP_TASKBAR",
75 "_NET_WM_STATE_SHADED",
76 "_NET_WM_STATE_BELOW",
77 "_NET_WM_STATE_HIDDEN",
79 "_NET_WM_WINDOW_TYPE",
80 "_NET_WM_WINDOW_TYPE_DOCK",
85 "_NET_CLIENT_LIST_STACKING",
86 "_NET_NUMBER_OF_DESKTOPS",
87 "_NET_CURRENT_DESKTOP",
92 "_NET_RESTACK_WINDOW",
94 "_NET_WM_ICON_GEOMETRY",
103 _NET_WM_STATE_SKIP_TASKBAR,
104 _NET_WM_STATE_SHADED,
106 _NET_WM_STATE_HIDDEN,
109 _NET_WM_WINDOW_TYPE_DOCK,
113 _NET_CLIENT_LIST_STACKING,
114 _NET_NUMBER_OF_DESKTOPS,
115 _NET_CURRENT_DESKTOP,
122 _NET_WM_ICON_GEOMETRY,
127 Atom atoms[ATOM_COUNT];
135 static void *get_prop_data(Window win, Atom prop, Atom type, int *items)
139 unsigned long items_ret;
140 unsigned long after_ret;
141 unsigned char *prop_data;
145 XGetWindowProperty(dd, win, prop, 0, 0x7fffffff, False, type, &type_ret,
146 &format_ret, &items_ret, &after_ret, &prop_data);
153 static int generic_get_int(Window win, Atom at)
158 data = get_prop_data(win, at, XA_CARDINAL, 0);
166 static int find_desktop(Window win)
168 return generic_get_int(win, atoms[_NET_WM_DESKTOP]);
171 static int find_state(Window win, atom_t atom)
177 data = get_prop_data(win, atoms[_NET_WM_STATE], XA_ATOM, &num);
180 if ((data[num]) == atoms[atom])
188 /* this naming is very confusing :) */
189 static int is_hidden(Window win)
191 return find_state(win, _NET_WM_STATE_SKIP_TASKBAR);
194 static int is_iconified(Window win)
196 return find_state(win, _NET_WM_STATE_HIDDEN);
199 static int is_shaded(Window win)
201 return find_state(win, _NET_WM_STATE_SHADED);
204 static int get_current_desktop(void)
206 return generic_get_int(root_win, atoms[_NET_CURRENT_DESKTOP]);
210 static int get_number_of_desktops(void)
212 return generic_get_int(root_win, atoms[_NET_NUMBER_OF_DESKTOPS]);
216 static void free_icons(task *tk)
220 for (i = 0; i < tk->nicons; ++i)
221 free(tk->icons[i].data);
227 static void task_update_icon(task *tk)
229 /* these bits are borrowed from openbox' client.c::client_update_icons */
232 unsigned int w, h, i, j;
236 data = get_prop_data(tk->win, atoms[_NET_WM_ICON], XA_CARDINAL, &num);
238 /* figure out how many valid icons are in here */
240 while (num - i > 2) {
244 if (i > num || w*h == 0) break;
248 tk->icons = calloc(tk->nicons, sizeof(icon));
250 /* store the icons */
252 for (j = 0; j < tk->nicons; ++j) {
255 w = tk->icons[j].width = data[i++];
256 h = tk->icons[j].height = data[i++];
258 if (w*h == 0) continue;
260 tk->icons[j].data = g_new(RrPixel32, w * h);
261 for (x = 0, y = 0, t = 0; t < w * h; ++t, ++x, ++i) {
266 tk->icons[j].data[t] =
267 (((data[i] >> 24) & 0xff) << RrDefaultAlphaOffset) +
268 (((data[i] >> 16) & 0xff) << RrDefaultRedOffset) +
269 (((data[i] >> 8) & 0xff) << RrDefaultGreenOffset) +
270 (((data[i] >> 0) & 0xff) << RrDefaultBlueOffset);
279 if ((hints = XGetWMHints(dd, tk->win))) {
280 if (hints->flags & IconPixmapHint) {
282 tk->icons = g_new(icon, tk->nicons);
283 // xerror_set_ignore(TRUE);
284 if (!RrPixmapToRGBA(inst,
286 (hints->flags & IconMaskHint ?
287 hints->icon_mask : None),
288 &tk->icons[tk->nicons-1].width,
289 &tk->icons[tk->nicons-1].height,
290 &tk->icons[tk->nicons-1].data)){
291 g_free(&tk->icons[tk->nicons-1]);
294 // xerror_set_ignore(FALSE);
301 static void add_task(Window win, int focus)
309 /* is this window on a different desktop? */
310 /* XXX add even if is_hidden, but don't show it later, so we can
311 * unhide it when the prop is removed */
312 desk = find_desktop(win);
313 if ((desk != 0xffffffff && tb.my_desktop != desk) || is_hidden(win))
316 XSelectInput(dd, win, PropertyChangeMask | StructureNotifyMask);
318 tk = calloc(1, sizeof(task));
321 tk->name = get_prop_data(tk->win, atoms[_NET_WM_NAME], atoms[STRING_UTF8], 0) ?:
322 get_prop_data(tk->win, XA_WM_NAME, XA_STRING, 0);
324 //tk->locale = get_prop_data(win, XA_WM_LOCALE_NAME, XA_STRING, 0);
325 tk->iconified = is_iconified(win);
326 tk->shaded = is_shaded(win);
327 task_update_icon(tk);
329 /* now append it to our linked list */
346 static void set_prop(Window win, Atom at, Atom type, long val)
348 XChangeProperty(dd, win, at, type, 32,
349 PropModeReplace, (unsigned char *)&val, 1);
352 static Window gui_create_taskbar(void)
355 XSizeHints size_hints;
356 XSetWindowAttributes att;
359 att.event_mask = ButtonPressMask;
362 tb.y = scr_height - WINHEIGHT;
364 win = rspanelwin = XCreateWindow(/* display */ dd,
365 /* parent */ root_win,
368 /* XXX Maybe just use scr_width here? */
369 /* width */ WINWIDTH,
370 /* height */ WINHEIGHT,
372 /* depth */ CopyFromParent,
373 /* class */ InputOutput,
374 /* visual */ CopyFromParent,
375 /*value mask*/ CWEventMask,
378 /* reside on ALL desktops */
379 set_prop(win, atoms[_NET_WM_DESKTOP], XA_CARDINAL, 0xFFFFFFFF);
380 set_prop(win, atoms[_NET_WM_WINDOW_TYPE], XA_ATOM,
381 atoms[_NET_WM_WINDOW_TYPE_DOCK]);
382 set_prop(win, atoms[_NET_WM_STATE], XA_ATOM, atoms[_NET_WM_STATE_BELOW]);
383 XChangeProperty(dd, win, XA_WM_NAME, XA_STRING, 8, PropModeReplace,
384 (unsigned char *)"rspanel", 7);
386 /* make sure the WM obays our window position */
387 size_hints.flags = PPosition;
388 /*XSetWMNormalHints (dd, win, &size_hints); */
389 XChangeProperty(dd, win, XA_WM_NORMAL_HINTS, XA_WM_SIZE_HINTS, 32,
390 PropModeReplace, (unsigned char *)&size_hints,
391 sizeof(XSizeHints) / 4);
393 xclhints.res_name = "rspanel";
394 xclhints.res_class = "RSPanel";
395 XSetClassHint(dd, win, &xclhints);
402 static void gui_init(void)
406 gcv.graphics_exposures = False;
408 fore_gc = XCreateGC(dd, root_win, GCGraphicsExposures, &gcv);
410 inst = RrInstanceNew(dd, scr_screen);
411 background = RrAppearanceNew(inst, 0);
412 focused_task = RrAppearanceNew(inst, 0);
413 unfocused_task = RrAppearanceNew(inst, 0);
414 normal_text = RrAppearanceNew(inst, 1);
415 a_icon = RrAppearanceNew(inst, 1);
417 a_icon->surface.grad = RR_SURFACE_PARENTREL;
419 background->surface.grad = RR_SURFACE_DIAGONAL;
420 background->surface.primary = RrColorNew(inst, 170, 170, 190);
421 background->surface.secondary = RrColorNew(inst, 100, 100, 160);
423 unfocused_task->surface.parent = background;
424 unfocused_task->surface.grad = RR_SURFACE_PARENTREL;
426 iconified_task = RrAppearanceCopy(unfocused_task);
427 iconified_task->surface.relief = RR_RELIEF_SUNKEN;
428 iconified_task->surface.parent = background;
430 focused_task->surface.parent = background;
431 focused_task->surface.grad = RR_SURFACE_CROSS_DIAGONAL;
432 focused_task->surface.secondary = RrColorNew(inst, 70, 80, 110);
433 focused_task->surface.primary = RrColorNew(inst, 130, 160, 250);
434 focused_task->surface.relief = RR_RELIEF_RAISED;
436 normal_text->surface.grad = RR_SURFACE_PARENTREL;
437 normal_text->texture[0].type = RR_TEXTURE_TEXT;
438 normal_text->texture[0].data.text.font
439 = RrFontOpen(inst, "Candara, sans", 10, RR_FONTWEIGHT_NORMAL, RR_FONTSLANT_NORMAL);
440 normal_text->texture[0].data.text.justify = RR_JUSTIFY_LEFT;
441 normal_text->texture[0].data.text.ellipsize = RR_ELLIPSIZE_END;
443 iconified_text = RrAppearanceCopy(normal_text);
444 normal_text->texture[0].data.text.color = RrColorNew(inst, 20, 20, 40);
445 focused_text = RrAppearanceCopy(normal_text);
446 iconified_text->texture[0].data.text.shadow_offset_x = 2;
447 iconified_text->texture[0].data.text.shadow_offset_y = 2;
448 iconified_text->texture[0].data.text.shadow_alpha = 100;
449 iconified_text->texture[0].data.text.shadow_color = RrColorNew(inst, 0, 0, 0);
450 shaded_text = RrAppearanceCopy(normal_text);
451 shaded_text->texture[0].data.text.color = RrColorNew(inst, 50, 60, 90);
452 iconified_text->texture[0].data.text.color = RrColorNew(inst, 200, 200, 200);
453 focused_text->texture[0].data.text.color = RrColorNew(inst, 230, 230, 255);
456 const icon* best_task_icon(task *tk, gint w, gint h)
460 gulong min_diff, min_i;
462 min_diff = ABS(tk->icons[0].width - w) + ABS(tk->icons[0].height - h);
465 for (i = 1; i < tk->nicons; ++i) {
468 diff = ABS(tk->icons[i].width - w) + ABS(tk->icons[i].height - h);
469 if (diff < min_diff) {
474 return &tk->icons[min_i];
478 #define ICON_SIZE WINHEIGHT-2*PADDING
479 static void gui_draw_task(task *tk, int redraw)
487 else if (tk->focused)
498 else if (tk->focused)
503 i = best_task_icon(tk, ICON_SIZE, ICON_SIZE);
506 RrTexture *c = &a_icon->texture[0];
507 RrTextureRGBA *d = &c->data.rgba;
508 c->type = RR_TEXTURE_RGBA;
509 a_icon->surface.parent = b;
510 a_icon->surface.parentx = PADDING;
511 a_icon->surface.parenty = PADDING;
513 d->height = i->height;
514 d->alpha = tk->iconified ? 0x80 : tk->shaded ? 0xB0 : 0xff;
518 a->surface.parent = b;
519 a->surface.parentx = ICON_SIZE+2*PADDING;
520 a->texture[0].data.text.string = tk->name;
521 b->surface.parentx = tk->pos_x;
523 RrPaintPixmap(b, tk->width, WINHEIGHT);
524 RrPaintPixmap(a, tk->width-3*PADDING-ICON_SIZE, WINHEIGHT);
525 RrPaintPixmap(a_icon, ICON_SIZE, ICON_SIZE);
527 XCopyArea(dd, a_icon->pixmap, b->pixmap, fore_gc, 0, 0,
528 ICON_SIZE, ICON_SIZE, PADDING, PADDING);
529 XCopyArea(dd, a->pixmap, b->pixmap, fore_gc, 0, 0,
530 tk->width-3*PADDING-ICON_SIZE, WINHEIGHT, ICON_SIZE+2*PADDING, 0);
531 XCopyArea(dd, b->pixmap, bgpixmap, fore_gc, 0, 0,
532 tk->width, WINHEIGHT, tk->pos_x, 0);
534 a_icon->texture[1].type = RR_TEXTURE_NONE;
535 XFreePixmap(dd, a->pixmap);
536 XFreePixmap(dd, b->pixmap);
537 XFreePixmap(dd, a_icon->pixmap);
539 XSetWindowBackgroundPixmap(dd, tb.win, bgpixmap);
540 XClearWindow(dd, tb.win);
544 static void netwm_action(Window win, atom_t atom, Time time, long l)
546 XClientMessageEvent xev = {
547 .type = ClientMessage,
551 .message_type = atoms[atom]
554 if (atom == _NET_ACTIVE_WINDOW) {
556 xev.data.l[1] = time;
558 } else if (atom == _NET_CURRENT_DESKTOP) {
560 xev.data.l[1] = time;
561 } else if (atom == _NET_RESTACK_WINDOW) {
564 } else if (atom == _OB_FOCUS) {
566 xev.message_type = atoms[_NET_WM_STATE];
568 xev.data.l[1] = atoms[atom];
572 XSendEvent(dd, root_win, False, SubstructureNotifyMask
573 |SubstructureRedirectMask, (XEvent *)&xev);
576 static void set_icon_geometry(task *tk)
580 coords[0] = tb.x + tk->pos_x;
582 coords[2] = MAX(tk->width, 1);
583 coords[3] = WINHEIGHT;
585 XChangeProperty(dd, tk->win,
586 atoms[_NET_WM_ICON_GEOMETRY], XA_CARDINAL,
587 32, PropModeReplace, (unsigned char*) &coords, 4);
593 static void switch_desk(int new_desk, Time time)
595 if (get_number_of_desktops() <= new_desk)
598 netwm_action(None, _NET_CURRENT_DESKTOP, time, new_desk);
601 /* This doesn't work with obrender yet */
602 static void pager_draw_button(int x, int num)
609 if (num == tb.my_desktop) {
610 /* current desktop */
611 draw_box(x, PAGER_BUTTON_WIDTH);
614 fill_rect(x, 1, PAGER_BUTTON_WIDTH + 1, WINHEIGHT - 2);
620 col.color.alpha = 0xffff;
621 col.color.red = cols[5].red;
622 col.color.green = cols[5].green;
623 col.color.blue = cols[5].blue;
624 XftDrawString8(xftdraw, &col, xfs,
625 x + ((PAGER_BUTTON_WIDTH - PAGER_DIGIT_WIDTH) / 2), text_y,
629 XDrawString(dd, tb.win, fore_gc,
630 x + ((PAGER_BUTTON_WIDTH - PAGER_DIGIT_WIDTH) / 2) - 1,
635 static void pager_draw(void)
637 int desks, i, x = GRILL_WIDTH;
639 desks = get_number_of_desktops();
641 for (i = 0; i < desks; i++) {
642 pager_draw_button(x, i);
645 x += PAGER_BUTTON_WIDTH;
653 static void gui_draw_taskbar(void)
661 pager_size = TEXTPAD;
665 width = WINWIDTH - (pager_size + GRILL_WIDTH);
666 #warning only rerender if width changed!
667 if (bgpixmap) XFreePixmap(dd, bgpixmap);
668 bgpixmap = XCreatePixmap(dd, root_win, WINWIDTH, WINHEIGHT, RrDepth(inst));
670 XFreePixmap(dd, RrPaintPixmap(background, WINWIDTH, WINHEIGHT));
671 XCopyArea(dd, background->pixmap, bgpixmap, fore_gc, 0, 0, WINWIDTH, WINHEIGHT, 0, 0);
672 if (tb.num_tasks == 0)
675 taskw = width / tb.num_tasks;
676 if (taskw > MAX_TASK_WIDTH)
677 taskw = MAX_TASK_WIDTH;
682 tk->width = taskw - 1;
683 gui_draw_task(tk, FALSE);
684 set_icon_geometry(tk);
690 XSetWindowBackgroundPixmap(dd, tb.win, bgpixmap);
691 XClearWindow(dd, tb.win);
694 static task *find_task(Window win)
696 task *list = tb.task_list;
698 if (list->win == win)
705 static void del_task(Window win)
707 task *next, *prev = 0, *list = tb.task_list;
711 if (list->win == win) {
712 /* unlink and free this task */
729 static void move_taskbar(void)
734 tb.x = TEXTPAD - WINWIDTH;
737 tb.y = scr_height - WINHEIGHT;
739 XMoveWindow(dd, tb.win, tb.x, tb.y);
742 static void taskbar_read_clientlist(void)
744 Window *win, focus_win = 0;
745 int num, i, desk, new_desk = 0;
747 desk = get_current_desktop();
755 XResizeWindow(dd, rspanelwin, WINWIDTH, WINHEIGHT);
758 if (desk != tb.my_desktop) {
760 tb.my_desktop = desk;
763 win = get_prop_data(root_win, atoms[_NET_ACTIVE_WINDOW], XA_WINDOW, &num);
764 if (win && num > 0) {
769 win = get_prop_data(root_win, atoms[_NET_CLIENT_LIST], XA_WINDOW, &num);
773 /* remove windows that arn't in the _NET_CLIENT_LIST anymore */
776 list->focused = (focus_win == list->win);
780 for (i = num - 1; i >= 0; i--)
781 if (list->win == win[i] && !is_hidden(win[i]))
789 /* add any new windows */
790 for (i = 0; i < num; i++) {
791 if (!find_task(win[i]))
792 add_task(win[i], (win[i] == focus_win));
798 static void handle_press(int x, int y, int button, Time time)
803 if ((y > 2 && y < WINHEIGHT - 2 && x > GRILL_WIDTH) && button == 1)
804 switch_desk((x - GRILL_WIDTH) / PAGER_BUTTON_WIDTH, time);
807 /* clicked left grill */
810 tb.at_top = 1 - tb.at_top;
812 } else if (button == 4) {
818 /* clicked right grill */
819 else if (x + TEXTPAD > WINWIDTH) {
820 if (tb.hidden && (button == 1 || button == 5)) {
823 } else if (!tb.hidden && (button == 1 || button == 4)) {
830 /* clicked on a task button */
831 /* XXX Make this configureable */
832 if (x > tk->pos_x && x < tk->pos_x + tk->width) {
835 netwm_action(tk->win, _NET_ACTIVE_WINDOW, time, 0);
839 netwm_action(tk->win, _NET_WM_STATE_HIDDEN, time, REMOVE);
841 netwm_action(tk->win, _NET_WM_STATE_HIDDEN, time, ADD);
844 netwm_action(tk->win, _NET_RESTACK_WINDOW, time, Opposite);
847 if (is_shaded(tk->win))
848 netwm_action(tk->win, _NET_RESTACK_WINDOW, time, Below);
850 netwm_action(tk->win, _NET_WM_STATE_SHADED, time, ADD);
853 if (is_shaded(tk->win))
854 netwm_action(tk->win, _NET_WM_STATE_SHADED, time, REMOVE);
856 netwm_action(tk->win, _NET_RESTACK_WINDOW, time, Above);
860 netwm_action(tk->win, _NET_WM_STATE_HIDDEN, time, REMOVE);
862 netwm_action(tk->win, _OB_FOCUS, 0, 0);
865 netwm_action(tk->win, _NET_RESTACK_WINDOW, time, Below);
868 netwm_action(tk->win, _NET_RESTACK_WINDOW, time, Above);
874 } /* clicked on the background */
877 netwm_action(tb.win, _NET_RESTACK_WINDOW, time, Above);
880 netwm_action(tb.win, _NET_RESTACK_WINDOW, time, Below);
883 netwm_action(tb.win, _NET_RESTACK_WINDOW, time, Opposite);
898 static void handle_focusin(Window win)
905 if (tk->win != win) {
910 if (tk->win == win) {
920 static Bool look_for_duplicate_property(Display *d, XEvent *e, XPointer arg)
922 Atom at = *(Atom*)arg;
923 return (e->type == PropertyNotify && e->xproperty.atom == at);
926 static void handle_propertynotify(Window win, Atom at)
930 if (win == root_win) {
931 /* XXX only redraw the task that got focused/unfocused
932 * when _NET_ACTIVE_WINDOW triggers this */
933 if (at == atoms[_NET_CLIENT_LIST] || at == atoms[_NET_CURRENT_DESKTOP]
934 || at == atoms[_NET_ACTIVE_WINDOW])
939 check = atoms[_NET_CLIENT_LIST];
940 while (XCheckIfEvent(dd, &ce, look_for_duplicate_property, (XPointer)&check));
941 check = atoms[_NET_CURRENT_DESKTOP];
942 while (XCheckIfEvent(dd, &ce, look_for_duplicate_property, (XPointer)&check));
943 check = atoms[_NET_ACTIVE_WINDOW];
944 while (XCheckIfEvent(dd, &ce, look_for_duplicate_property, (XPointer)&check));
946 taskbar_read_clientlist();
952 /* XXX make this work when SKIP_TASKBAR is toggled too */
957 if (at == XA_WM_NAME || at == atoms[_NET_WM_NAME]) {
958 /* window's title changed */
959 /* XXX make a function for this and use from here and add_task */
961 newname = get_prop_data(tk->win, atoms[_NET_WM_NAME], atoms[STRING_UTF8], 0) ?:
962 get_prop_data(tk->win, XA_WM_NAME, XA_STRING, 0);
964 /* It didn't change */
965 if (tk->name && !strcmp(newname, tk->name)) {
973 gui_draw_task(tk, TRUE);
974 } else if (at == atoms[_NET_WM_ICON]) {
975 task_update_icon(tk);
976 gui_draw_task(tk, TRUE);
977 } else if (at == atoms[_NET_WM_STATE]) {
978 /* iconified state changed? */
979 if (is_iconified(tk->win) != tk->iconified) {
980 tk->iconified = !tk->iconified;
981 gui_draw_task(tk, TRUE);
983 /* shaded state changed? */
984 if (is_shaded(tk->win) != tk->shaded) {
985 tk->shaded = !tk->shaded;
986 gui_draw_task(tk, TRUE);
988 } else if (at == atoms[_NET_WM_DESKTOP]) {
989 if (find_desktop(win) != get_current_desktop())
994 static void handle_error(Display * d, XErrorEvent * ev)
1002 main(int argc, char *argv[])
1009 dd = XOpenDisplay(NULL);
1012 scr_screen = DefaultScreen(dd);
1013 scr_depth = DefaultDepth(dd, scr_screen);
1014 scr_height = DisplayHeight(dd, scr_screen);
1015 scr_width = DisplayWidth(dd, scr_screen);
1016 root_win = RootWindow(dd, scr_screen);
1018 /* helps us catch windows closing/opening */
1019 XSelectInput(dd, root_win, PropertyChangeMask);
1021 XSetErrorHandler((XErrorHandler) handle_error);
1023 XInternAtoms(dd, atom_names, ATOM_COUNT, False, atoms);
1026 memset(&tb, 0, sizeof(tb));
1027 tb.win = gui_create_taskbar();
1028 xfd = ConnectionNumber(dd);
1030 taskbar_read_clientlist();
1036 select(xfd + 1, &fd, 0, 0, 0);
1038 while (XPending(dd)) {
1039 XNextEvent(dd, &ev);
1042 handle_press(ev.xbutton.x, ev.xbutton.y, ev.xbutton.button, ev.xbutton.time);
1044 case PropertyNotify:
1045 handle_propertynotify(ev.xproperty.window, ev.xproperty.atom);
1048 printf ("unknown evt type: %d\n", ev.type); */
1052 /* RrInstanceFree(inst);
1053 * XCloseDisplay (dd);