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>
26 /* you can edit these */
27 #define MAX_TASK_WIDTH 500
32 # define WINWIDTH 1280
37 /* don't edit these */
39 #define GRILL_WIDTH 10
41 #include <openbox/render.h>
42 #include <openbox/theme.h>
59 RrAppearance *background;
60 RrAppearance *focused_task;
61 RrAppearance *unfocused_task;
62 RrAppearance *normal_text;
63 RrAppearance *iconified_text;
64 RrAppearance *shaded_text;
65 RrAppearance *focused_text;
67 /* we draw stuff to this guy then set him as a window
68 * background pixmap, yay no flickering! */
71 char *atom_names[] = {
77 "_NET_WM_STATE_SKIP_TASKBAR",
78 "_NET_WM_STATE_SHADED",
79 "_NET_WM_STATE_BELOW",
80 "_NET_WM_STATE_HIDDEN",
82 "_NET_WM_WINDOW_TYPE",
83 "_NET_WM_WINDOW_TYPE_DOCK",
88 "_NET_CLIENT_LIST_STACKING",
89 "_NET_NUMBER_OF_DESKTOPS",
90 "_NET_CURRENT_DESKTOP",
95 "_NET_RESTACK_WINDOW",
104 _NET_WM_STATE_SKIP_TASKBAR,
105 _NET_WM_STATE_SHADED,
107 _NET_WM_STATE_HIDDEN,
110 _NET_WM_WINDOW_TYPE_DOCK,
114 _NET_CLIENT_LIST_STACKING,
115 _NET_NUMBER_OF_DESKTOPS,
116 _NET_CURRENT_DESKTOP,
126 Atom atoms[ATOM_COUNT];
134 static void *get_prop_data(Window win, Atom prop, Atom type, int *items)
138 unsigned long items_ret;
139 unsigned long after_ret;
140 unsigned char *prop_data;
144 XGetWindowProperty(dd, win, prop, 0, 0x7fffffff, False, type, &type_ret,
145 &format_ret, &items_ret, &after_ret, &prop_data);
152 static int generic_get_int(Window win, Atom at)
157 data = get_prop_data(win, at, XA_CARDINAL, 0);
165 static int find_desktop(Window win)
167 return generic_get_int(win, atoms[_NET_WM_DESKTOP]);
170 static int find_state(Window win, atom_t atom)
176 data = get_prop_data(win, atoms[_NET_WM_STATE], XA_ATOM, &num);
179 if ((data[num]) == atoms[atom])
187 /* this naming is very confusing :) */
188 static int is_hidden(Window win)
190 return find_state(win, _NET_WM_STATE_SKIP_TASKBAR);
193 static int is_iconified(Window win)
195 return find_state(win, _NET_WM_STATE_HIDDEN);
198 static int is_shaded(Window win)
200 return find_state(win, _NET_WM_STATE_SHADED);
203 static int get_current_desktop(void)
205 return generic_get_int(root_win, atoms[_NET_CURRENT_DESKTOP]);
209 static int get_number_of_desktops(void)
211 return generic_get_int(root_win, atoms[_NET_NUMBER_OF_DESKTOPS]);
215 static void add_task(Window win, int focus)
223 /* is this window on a different desktop? */
224 desk = find_desktop(win);
225 if ((desk != 0xffffffff && tb.my_desktop != desk) || is_hidden(win))
228 tk = calloc(1, sizeof(task));
231 tk->name = get_prop_data(tk->win, atoms[_NET_WM_NAME], atoms[STRING_UTF8], 0) ?:
232 get_prop_data(tk->win, XA_WM_NAME, XA_STRING, 0);
233 // tk->name = get_prop_data(win, XA_WM_NAME, XA_STRING, 0);
234 //tk->locale = get_prop_data(win, XA_WM_LOCALE_NAME, XA_STRING, 0);
235 tk->iconified = is_iconified(win);
236 tk->shaded = is_shaded(win);
238 XSelectInput(dd, win, PropertyChangeMask | StructureNotifyMask);
240 /* now append it to our linked list */
257 static void set_prop(Window win, Atom at, Atom type, long val)
259 XChangeProperty(dd, win, at, type, 32,
260 PropModeReplace, (unsigned char *)&val, 1);
263 static Window gui_create_taskbar(void)
266 XSizeHints size_hints;
267 XSetWindowAttributes att;
270 att.event_mask = ButtonPressMask;
272 win = rspanelwin = XCreateWindow(/* display */ dd,
273 /* parent */ root_win,
275 /* y */ scr_height - WINHEIGHT,
276 /* XXX Maybe just use scr_width here? */
277 /* width */ WINWIDTH,
278 /* height */ WINHEIGHT,
280 /* depth */ CopyFromParent,
281 /* class */ InputOutput,
282 /* visual */ CopyFromParent,
283 /*value mask*/ CWEventMask,
286 /* reside on ALL desktops */
287 set_prop(win, atoms[_NET_WM_DESKTOP], XA_CARDINAL, 0xFFFFFFFF);
288 set_prop(win, atoms[_NET_WM_WINDOW_TYPE], XA_ATOM,
289 atoms[_NET_WM_WINDOW_TYPE_DOCK]);
290 set_prop(win, atoms[_NET_WM_STATE], XA_ATOM, atoms[_NET_WM_STATE_BELOW]);
291 XChangeProperty(dd, win, XA_WM_NAME, XA_STRING, 8, PropModeReplace,
292 (unsigned char *)"rspanel", 7);
294 /* make sure the WM obays our window position */
295 size_hints.flags = PPosition;
296 /*XSetWMNormalHints (dd, win, &size_hints); */
297 XChangeProperty(dd, win, XA_WM_NORMAL_HINTS, XA_WM_SIZE_HINTS, 32,
298 PropModeReplace, (unsigned char *)&size_hints,
299 sizeof(XSizeHints) / 4);
301 xclhints.res_name = "rspanel";
302 xclhints.res_class = "RSPanel";
303 XSetClassHint(dd, win, &xclhints);
310 static void gui_init(void)
314 gcv.graphics_exposures = False;
316 fore_gc = XCreateGC(dd, root_win, GCGraphicsExposures, &gcv);
318 inst = RrInstanceNew(dd, scr_screen);
319 background = RrAppearanceNew(inst, 0);
320 focused_task = RrAppearanceNew(inst, 0);
321 unfocused_task = RrAppearanceNew(inst, 0);
322 normal_text = RrAppearanceNew(inst, 1);
324 background->surface.grad = RR_SURFACE_DIAGONAL;
325 background->surface.primary = RrColorNew(inst, 170, 170, 190);
326 background->surface.secondary = RrColorNew(inst, 100, 100, 160);
328 unfocused_task->surface.parent = background;
329 unfocused_task->surface.grad = RR_SURFACE_PARENTREL;
330 unfocused_task->surface.relief = RR_RELIEF_SUNKEN;
332 focused_task->surface.parent = background;
333 focused_task->surface.grad = RR_SURFACE_CROSS_DIAGONAL;
334 focused_task->surface.secondary = RrColorNew(inst, 70, 80, 110);
335 focused_task->surface.primary = RrColorNew(inst, 130, 160, 250);
336 focused_task->surface.relief = RR_RELIEF_RAISED;
338 normal_text->surface.grad = RR_SURFACE_PARENTREL;
339 normal_text->texture[0].type = RR_TEXTURE_TEXT;
340 normal_text->texture[0].data.text.font = RrFontOpenDefault(inst);
341 normal_text->texture[0].data.text.justify = RR_JUSTIFY_LEFT;
343 iconified_text = RrAppearanceCopy(normal_text);
344 normal_text->texture[0].data.text.color = RrColorNew(inst, 20, 20, 40);
345 focused_text = RrAppearanceCopy(normal_text);
346 iconified_text->texture[0].data.text.shadow_offset_x = 2;
347 iconified_text->texture[0].data.text.shadow_offset_y = 2;
348 iconified_text->texture[0].data.text.shadow_alpha = 100;
349 iconified_text->texture[0].data.text.shadow_color = RrColorNew(inst, 0, 0, 0);
350 shaded_text = RrAppearanceCopy(normal_text);
351 shaded_text->texture[0].data.text.color = RrColorNew(inst, 50, 60, 90);
352 iconified_text->texture[0].data.text.color = RrColorNew(inst, 200, 200, 200);
353 focused_text->texture[0].data.text.color = RrColorNew(inst, 230, 230, 255);
357 static void gui_draw_task(task *tk, int redraw)
365 else if (tk->focused)
369 b = tk->focused ? focused_task : unfocused_task;
370 a->surface.parent = b;
371 a->surface.parentx = PADDING;
372 a->texture[0].data.text.string = tk->name;
373 b->surface.parentx = tk->pos_x;
374 RrPaintPixmap(b, tk->width, WINHEIGHT);
375 RrPaintPixmap(a, tk->width-2*PADDING, WINHEIGHT);
377 XCopyArea(dd, a->pixmap, b->pixmap, fore_gc, 0, 0, tk->width-2*PADDING, WINHEIGHT, PADDING, 0);
378 XCopyArea(dd, b->pixmap, bgpixmap, fore_gc, 0, 0, tk->width, WINHEIGHT, tk->pos_x, 0);
380 XCopyArea(dd, a->pixmap, bgpixmap, fore_gc, 0, 0, tk->width, WINHEIGHT, tk->pos_x, 0);
383 XFreePixmap(dd, a->pixmap);
384 XFreePixmap(dd, b->pixmap);
386 XSetWindowBackgroundPixmap(dd, tb.win, bgpixmap);
387 XClearWindow(dd, tb.win);
391 static void netwm_action(Window win, atom_t atom, Time time, long l)
393 XClientMessageEvent xev = {
394 .type = ClientMessage,
398 .message_type = atoms[atom]
401 if (atom == _NET_ACTIVE_WINDOW) {
403 xev.data.l[1] = time;
405 } else if (atom == _NET_CURRENT_DESKTOP) {
407 xev.data.l[1] = time;
408 } else if (atom == _NET_RESTACK_WINDOW) {
411 } else if (atom == _OB_FOCUS) {
413 xev.message_type = atoms[_NET_WM_STATE];
415 xev.data.l[1] = atoms[atom];
419 XSendEvent(dd, root_win, False, SubstructureNotifyMask
420 |SubstructureRedirectMask, (XEvent *)&xev);
425 static void switch_desk(int new_desk, Time time)
427 if (get_number_of_desktops() <= new_desk)
430 netwm_action(None, _NET_CURRENT_DESKTOP, time, new_desk);
433 /* This doesn't work with obrender yet */
434 static void pager_draw_button(int x, int num)
441 if (num == tb.my_desktop) {
442 /* current desktop */
443 draw_box(x, PAGER_BUTTON_WIDTH);
446 fill_rect(x, 1, PAGER_BUTTON_WIDTH + 1, WINHEIGHT - 2);
452 col.color.alpha = 0xffff;
453 col.color.red = cols[5].red;
454 col.color.green = cols[5].green;
455 col.color.blue = cols[5].blue;
456 XftDrawString8(xftdraw, &col, xfs,
457 x + ((PAGER_BUTTON_WIDTH - PAGER_DIGIT_WIDTH) / 2), text_y,
461 XDrawString(dd, tb.win, fore_gc,
462 x + ((PAGER_BUTTON_WIDTH - PAGER_DIGIT_WIDTH) / 2) - 1,
467 static void pager_draw(void)
469 int desks, i, x = GRILL_WIDTH;
471 desks = get_number_of_desktops();
473 for (i = 0; i < desks; i++) {
474 pager_draw_button(x, i);
477 x += PAGER_BUTTON_WIDTH;
485 static void gui_draw_taskbar(void)
493 pager_size = TEXTPAD;
497 width = WINWIDTH - (pager_size + GRILL_WIDTH);
498 #warning only rerender if width changed!
499 if (bgpixmap) XFreePixmap(dd, bgpixmap);
500 bgpixmap = XCreatePixmap(dd, root_win, WINWIDTH, WINHEIGHT, RrDepth(inst));
502 XFreePixmap(dd, RrPaintPixmap(background, WINWIDTH, WINHEIGHT));
503 XCopyArea(dd, background->pixmap, bgpixmap, fore_gc, 0, 0, WINWIDTH, WINHEIGHT, 0, 0);
504 if (tb.num_tasks == 0)
507 taskw = width / tb.num_tasks;
508 if (taskw > MAX_TASK_WIDTH)
509 taskw = MAX_TASK_WIDTH;
514 tk->width = taskw - 1;
515 gui_draw_task(tk, FALSE);
521 XSetWindowBackgroundPixmap(dd, tb.win, bgpixmap);
522 XClearWindow(dd, tb.win);
525 static task *find_task(Window win)
527 task *list = tb.task_list;
529 if (list->win == win)
536 static void del_task(Window win)
538 task *next, *prev = 0, *list = tb.task_list;
542 if (list->win == win) {
543 /* unlink and free this task */
545 if (list->icon_copied) {
546 XFreePixmap(dd, list->icon);
547 if (list->mask != None)
548 XFreePixmap(dd, list->mask);
564 static void move_taskbar(void)
571 x = TEXTPAD - WINWIDTH;
574 y = scr_height - WINHEIGHT;
576 XMoveWindow(dd, tb.win, x, y);
579 static void taskbar_read_clientlist(void)
581 Window *win, focus_win = 0;
582 int num, i, desk, new_desk = 0;
584 desk = get_current_desktop();
591 XResizeWindow(dd, rspanelwin, WINWIDTH, WINHEIGHT);
594 if (desk != tb.my_desktop) {
596 tb.my_desktop = desk;
599 win = get_prop_data(root_win, atoms[_NET_ACTIVE_WINDOW], XA_WINDOW, &num);
600 if (win && num > 0) {
605 win = get_prop_data(root_win, atoms[_NET_CLIENT_LIST], XA_WINDOW, &num);
609 /* remove windows that arn't in the _NET_CLIENT_LIST anymore */
612 list->focused = (focus_win == list->win);
616 for (i = num - 1; i >= 0; i--)
617 if (list->win == win[i] && !is_hidden(win[i]))
625 /* add any new windows */
626 for (i = 0; i < num; i++) {
627 if (!find_task(win[i]))
628 add_task(win[i], (win[i] == focus_win));
634 static void handle_press(int x, int y, int button, Time time)
639 if ((y > 2 && y < WINHEIGHT - 2 && x > GRILL_WIDTH) && button == 1)
640 switch_desk((x - GRILL_WIDTH) / PAGER_BUTTON_WIDTH, time);
643 /* clicked left grill */
646 tb.at_top = 1 - tb.at_top;
648 } else if (button == 4) {
654 /* clicked right grill */
655 else if (x + TEXTPAD > WINWIDTH) {
656 if (tb.hidden && (button == 1 || button == 5)) {
659 } else if (!tb.hidden && (button == 1 || button == 4)) {
666 /* clicked on a task button */
667 /* XXX Make this configureable */
668 if (x > tk->pos_x && x < tk->pos_x + tk->width) {
671 netwm_action(tk->win, _NET_ACTIVE_WINDOW, time, 0);
675 netwm_action(tk->win, _NET_WM_STATE_HIDDEN, time, REMOVE);
677 netwm_action(tk->win, _NET_WM_STATE_HIDDEN, time, ADD);
680 netwm_action(tk->win, _NET_RESTACK_WINDOW, time, Opposite);
683 if (is_shaded(tk->win))
684 netwm_action(tk->win, _NET_RESTACK_WINDOW, time, Below);
686 netwm_action(tk->win, _NET_WM_STATE_SHADED, time, ADD);
689 if (is_shaded(tk->win))
690 netwm_action(tk->win, _NET_WM_STATE_SHADED, time, REMOVE);
692 netwm_action(tk->win, _NET_RESTACK_WINDOW, time, Above);
696 netwm_action(tk->win, _NET_WM_STATE_HIDDEN, time, REMOVE);
698 netwm_action(tk->win, _OB_FOCUS, 0, 0);
701 netwm_action(tk->win, _NET_RESTACK_WINDOW, time, Below);
704 netwm_action(tk->win, _NET_RESTACK_WINDOW, time, Above);
710 } /* clicked on the background */
713 netwm_action(tb.win, _NET_RESTACK_WINDOW, time, Above);
716 netwm_action(tb.win, _NET_RESTACK_WINDOW, time, Below);
719 netwm_action(tb.win, _NET_RESTACK_WINDOW, time, Opposite);
734 static void handle_focusin(Window win)
741 if (tk->win != win) {
746 if (tk->win == win) {
756 static void handle_propertynotify(Window win, Atom at)
760 if (win == root_win) {
761 /* XXX only redraw the task that got focused/unfocused
762 * when _NET_ACTIVE_WINDOW triggers this */
763 if (at == atoms[_NET_CLIENT_LIST] || at == atoms[_NET_CURRENT_DESKTOP]
764 || at == atoms[_NET_ACTIVE_WINDOW])
766 taskbar_read_clientlist();
772 /* XXX make this work when SKIP_TASKBAR is toggled too */
777 if (at == XA_WM_NAME || at == atoms[_NET_WM_NAME]) {
778 /* window's title changed */
780 newname = get_prop_data(tk->win, atoms[_NET_WM_NAME], atoms[STRING_UTF8], 0) ?:
781 get_prop_data(tk->win, XA_WM_NAME, XA_STRING, 0);
783 /* It didn't change */
784 if (tk->name && !strcmp(newname, tk->name)) {
792 gui_draw_task(tk, TRUE);
793 } else if (at == atoms[_NET_WM_STATE]) {
794 /* iconified state changed? */
795 if (is_iconified(tk->win) != tk->iconified) {
796 tk->iconified = !tk->iconified;
797 gui_draw_task(tk, TRUE);
799 /* shaded state changed? */
800 if (is_shaded(tk->win) != tk->shaded) {
801 tk->shaded = !tk->shaded;
802 gui_draw_task(tk, TRUE);
804 /* XXX use _NET_WM_ICON */
805 // } else if (at == XA_WM_HINTS) {
806 /* some windows set their WM_HINTS icon after mapping */
807 //if (tk->icon == generic_icon) {
808 // get_task_hinticon(tk);
809 // gui_draw_task(tk, TRUE);
811 } else if (at == atoms[_NET_WM_DESKTOP]) {
812 if (find_desktop(win) != get_current_desktop())
817 static void handle_error(Display * d, XErrorEvent * ev)
825 main(int argc, char *argv[])
832 dd = XOpenDisplay(NULL);
835 scr_screen = DefaultScreen(dd);
836 scr_depth = DefaultDepth(dd, scr_screen);
837 scr_height = DisplayHeight(dd, scr_screen);
838 scr_width = DisplayWidth(dd, scr_screen);
839 root_win = RootWindow(dd, scr_screen);
841 /* helps us catch windows closing/opening */
842 XSelectInput(dd, root_win, PropertyChangeMask);
844 XSetErrorHandler((XErrorHandler) handle_error);
846 XInternAtoms(dd, atom_names, ATOM_COUNT, False, atoms);
849 memset(&tb, 0, sizeof(tb));
850 tb.win = gui_create_taskbar();
851 xfd = ConnectionNumber(dd);
857 select(xfd + 1, &fd, 0, 0, 0);
859 while (XPending(dd)) {
863 handle_press(ev.xbutton.x, ev.xbutton.y, ev.xbutton.button, ev.xbutton.time);
866 handle_propertynotify(ev.xproperty.window, ev.xproperty.atom);
869 printf ("unknown evt type: %d\n", ev.type); */
873 /* RrInstanceFree(inst);
874 * XCloseDisplay (dd);