change codename
[awesome.git] / wibox.c
blobfb4b51c27c4bf296c4b55bc0d545a1e2008abd91
1 /*
2 * wibox.c - wibox functions
4 * Copyright © 2008 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "screen.h"
23 #include "wibox.h"
24 #include "titlebar.h"
25 #include "client.h"
26 #include "ewmh.h"
27 #include "screen.h"
28 #include "window.h"
29 #include "common/xcursor.h"
30 #include "common/xutil.h"
32 DO_LUA_TOSTRING(wibox_t, wibox, "wibox")
34 /** Take care of garbage collecting a wibox.
35 * \param L The Lua VM state.
36 * \return The number of elements pushed on stack, 0!
38 static int
39 luaA_wibox_gc(lua_State *L)
41 wibox_t *wibox = luaL_checkudata(L, 1, "wibox");
42 luaA_ref_array_wipe(&wibox->refs);
43 p_delete(&wibox->cursor);
44 simplewindow_wipe(&wibox->sw);
45 button_array_wipe(&wibox->buttons);
46 image_unref(L, wibox->bg_image);
47 luaL_unref(L, LUA_REGISTRYINDEX, wibox->widgets_table);
48 luaL_unref(L, LUA_REGISTRYINDEX, wibox->mouse_enter);
49 luaL_unref(L, LUA_REGISTRYINDEX, wibox->mouse_leave);
50 widget_node_array_wipe(&wibox->widgets);
51 return 0;
54 void
55 wibox_unref_simplified(wibox_t **item)
57 wibox_unref(globalconf.L, *item);
60 static void
61 wibox_need_update(wibox_t *wibox)
63 wibox->need_update = true;
64 wibox->mouse_over = NULL;
67 static void
68 wibox_map(wibox_t *wibox)
70 xcb_map_window(globalconf.connection, wibox->sw.window);
71 /* We must make sure the wibox does not display garbage */
72 wibox_need_update(wibox);
73 /* Stack this wibox correctly */
74 client_stack();
77 static void
78 wibox_move(wibox_t *wibox, int16_t x, int16_t y)
80 if(wibox->sw.window)
82 simplewindow_move(&wibox->sw, x, y);
83 wibox->screen = screen_getbycoord(wibox->screen, x, y);
85 else
87 wibox->sw.geometry.x = x;
88 wibox->sw.geometry.y = y;
92 static void
93 wibox_resize(wibox_t *wibox, uint16_t width, uint16_t height)
95 if(wibox->sw.window)
96 simplewindow_resize(&wibox->sw, width, height);
97 else
99 wibox->sw.geometry.width = width;
100 wibox->sw.geometry.height = height;
102 wibox_need_update(wibox);
105 static void
106 wibox_setposition(wibox_t *wibox, position_t p)
108 if(p != wibox->position)
110 switch((wibox->position = p))
112 case Bottom:
113 case Top:
114 case Floating:
115 simplewindow_orientation_set(&wibox->sw, East);
116 break;
117 case Left:
118 simplewindow_orientation_set(&wibox->sw, North);
119 break;
120 case Right:
121 simplewindow_orientation_set(&wibox->sw, South);
122 break;
124 /* reset width/height to 0 */
125 if(wibox->position != Floating)
126 wibox->sw.geometry.width = wibox->sw.geometry.height = 0;
128 /* recompute position */
129 wibox_position_update(wibox);
131 /* reset all wibox position */
132 foreach(w, wibox->screen->wiboxes)
133 wibox_position_update(*w);
135 ewmh_update_workarea(screen_virttophys(screen_array_indexof(&globalconf.screens, wibox->screen)));
137 wibox_need_update(wibox);
141 /** Kick out systray windows.
142 * \param phys_screen Physical screen number.
144 static void
145 wibox_systray_kickout(int phys_screen)
147 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
149 if(globalconf.screens.tab[phys_screen].systray.parent != s->root)
151 /* Who! Check that we're not deleting a wibox with a systray, because it
152 * may be its parent. If so, we reparent to root before, otherwise it will
153 * hurt very much. */
154 xcb_reparent_window(globalconf.connection,
155 globalconf.screens.tab[phys_screen].systray.window,
156 s->root, -512, -512);
158 globalconf.screens.tab[phys_screen].systray.parent = s->root;
162 static void
163 wibox_systray_refresh(wibox_t *wibox)
165 if(!wibox->screen)
166 return;
168 for(int i = 0; i < wibox->widgets.len; i++)
170 widget_node_t *systray = &wibox->widgets.tab[i];
171 if(systray->widget->type == widget_systray)
173 uint32_t config_back[] = { wibox->sw.ctx.bg.pixel };
174 uint32_t config_win_vals[4];
175 uint32_t config_win_vals_off[2] = { -512, -512 };
176 xembed_window_t *em;
177 int phys_screen = wibox->sw.ctx.phys_screen;
179 if(wibox->isvisible
180 && systray->widget->isvisible
181 && systray->geometry.width)
183 /* Set background of the systray window. */
184 xcb_change_window_attributes(globalconf.connection,
185 globalconf.screens.tab[phys_screen].systray.window,
186 XCB_CW_BACK_PIXEL, config_back);
187 /* Map it. */
188 xcb_map_window(globalconf.connection, globalconf.screens.tab[phys_screen].systray.window);
189 /* Move it. */
190 switch(wibox->sw.orientation)
192 case North:
193 config_win_vals[0] = systray->geometry.y;
194 config_win_vals[1] = wibox->sw.geometry.height - systray->geometry.x - systray->geometry.width;
195 config_win_vals[2] = systray->geometry.height;
196 config_win_vals[3] = systray->geometry.width;
197 break;
198 case South:
199 config_win_vals[0] = systray->geometry.y;
200 config_win_vals[1] = systray->geometry.x;
201 config_win_vals[2] = systray->geometry.height;
202 config_win_vals[3] = systray->geometry.width;
203 break;
204 case East:
205 config_win_vals[0] = systray->geometry.x;
206 config_win_vals[1] = systray->geometry.y;
207 config_win_vals[2] = systray->geometry.width;
208 config_win_vals[3] = systray->geometry.height;
209 break;
211 /* reparent */
212 if(globalconf.screens.tab[phys_screen].systray.parent != wibox->sw.window)
214 xcb_reparent_window(globalconf.connection,
215 globalconf.screens.tab[phys_screen].systray.window,
216 wibox->sw.window,
217 config_win_vals[0], config_win_vals[1]);
218 globalconf.screens.tab[phys_screen].systray.parent = wibox->sw.window;
220 xcb_configure_window(globalconf.connection,
221 globalconf.screens.tab[phys_screen].systray.window,
222 XCB_CONFIG_WINDOW_X
223 | XCB_CONFIG_WINDOW_Y
224 | XCB_CONFIG_WINDOW_WIDTH
225 | XCB_CONFIG_WINDOW_HEIGHT,
226 config_win_vals);
227 /* width = height = systray height */
228 config_win_vals[2] = config_win_vals[3] = systray->geometry.height;
229 config_win_vals[0] = 0;
231 else
232 return wibox_systray_kickout(phys_screen);
234 switch(wibox->sw.orientation)
236 case North:
237 config_win_vals[1] = systray->geometry.width - config_win_vals[3];
238 for(int j = 0; j < globalconf.embedded.len; j++)
240 em = &globalconf.embedded.tab[j];
241 if(em->phys_screen == phys_screen)
243 if(config_win_vals[1] - config_win_vals[2] >= (uint32_t) wibox->sw.geometry.y)
245 xcb_map_window(globalconf.connection, em->win);
246 xcb_configure_window(globalconf.connection, em->win,
247 XCB_CONFIG_WINDOW_X
248 | XCB_CONFIG_WINDOW_Y
249 | XCB_CONFIG_WINDOW_WIDTH
250 | XCB_CONFIG_WINDOW_HEIGHT,
251 config_win_vals);
252 config_win_vals[1] -= config_win_vals[3];
254 else
255 xcb_configure_window(globalconf.connection, em->win,
256 XCB_CONFIG_WINDOW_X
257 | XCB_CONFIG_WINDOW_Y,
258 config_win_vals_off);
261 break;
262 case South:
263 config_win_vals[1] = 0;
264 for(int j = 0; j < globalconf.embedded.len; j++)
266 em = &globalconf.embedded.tab[j];
267 if(em->phys_screen == phys_screen)
269 /* if(y + width <= wibox.y + systray.right) */
270 if(config_win_vals[1] + config_win_vals[3] <= (uint32_t) wibox->sw.geometry.y + AREA_RIGHT(systray->geometry))
272 xcb_map_window(globalconf.connection, em->win);
273 xcb_configure_window(globalconf.connection, em->win,
274 XCB_CONFIG_WINDOW_X
275 | XCB_CONFIG_WINDOW_Y
276 | XCB_CONFIG_WINDOW_WIDTH
277 | XCB_CONFIG_WINDOW_HEIGHT,
278 config_win_vals);
279 config_win_vals[1] += config_win_vals[3];
281 else
282 xcb_configure_window(globalconf.connection, em->win,
283 XCB_CONFIG_WINDOW_X
284 | XCB_CONFIG_WINDOW_Y,
285 config_win_vals_off);
288 break;
289 case East:
290 config_win_vals[1] = 0;
291 for(int j = 0; j < globalconf.embedded.len; j++)
293 em = &globalconf.embedded.tab[j];
294 if(em->phys_screen == phys_screen)
296 /* if(x + width < systray.x + systray.width) */
297 if(config_win_vals[0] + config_win_vals[2] <= (uint32_t) AREA_RIGHT(systray->geometry) + wibox->sw.geometry.x)
299 xcb_map_window(globalconf.connection, em->win);
300 xcb_configure_window(globalconf.connection, em->win,
301 XCB_CONFIG_WINDOW_X
302 | XCB_CONFIG_WINDOW_Y
303 | XCB_CONFIG_WINDOW_WIDTH
304 | XCB_CONFIG_WINDOW_HEIGHT,
305 config_win_vals);
306 config_win_vals[0] += config_win_vals[2];
308 else
309 xcb_configure_window(globalconf.connection, em->win,
310 XCB_CONFIG_WINDOW_X
311 | XCB_CONFIG_WINDOW_Y,
312 config_win_vals_off);
315 break;
317 break;
322 /* Only called by wibox_position_update() */
323 static void
324 wibox_position_update_floating(wibox_t *wibox)
326 area_t wingeom = wibox->sw.geometry;
328 /* We only make sure the wibox is at least 1x1 pixel big. */
329 wingeom.width = MAX(1, wibox->sw.geometry.width);
330 wingeom.height = MAX(1, wibox->sw.geometry.height);
332 if(wingeom.width != wibox->sw.geometry.width
333 || wingeom.height != wibox->sw.geometry.height)
334 wibox_resize(wibox, wingeom.width, wingeom.height);
337 /* Only called by wibox_position_update() */
338 static void
339 wibox_position_update_non_floating(wibox_t *wibox)
341 area_t area, wingeom = wibox->sw.geometry;
342 bool ignore = false;
344 /* Everything we do below needs the wibox' screen.
345 * No screen, nothing to do.
347 if (!wibox->screen)
348 return;
350 /* This wibox limits the space available to clients and thus clients
351 * need to be repositioned.
353 wibox->screen->need_arrange = true;
355 area = screen_area_get(wibox->screen, NULL,
356 &wibox->screen->padding, true);
358 /* Top and Bottom wibox_t have prio */
359 foreach(_w, wibox->screen->wiboxes)
361 wibox_t *w = *_w;
362 /* Ignore every wibox after me that is in the same position */
363 if(wibox == w)
365 ignore = true;
366 continue;
368 else if((ignore && wibox->position == w->position) || !w->isvisible)
369 continue;
370 switch(w->position)
372 case Floating:
373 break;
374 case Left:
375 if(wibox->position == Left)
376 area.x += wibox->sw.geometry.width;
377 break;
378 case Right:
379 if(wibox->position == Right)
380 area.x -= wibox->sw.geometry.width;
381 break;
382 case Top:
383 switch(wibox->position)
385 case Top:
386 area.y += w->sw.geometry.height;
387 break;
388 case Left:
389 case Right:
390 area.height -= w->sw.geometry.height;
391 area.y += w->sw.geometry.height;
392 break;
393 default:
394 break;
396 break;
397 case Bottom:
398 switch(wibox->position)
400 case Bottom:
401 area.y -= w->sw.geometry.height;
402 break;
403 case Left:
404 case Right:
405 area.height -= w->sw.geometry.height;
406 break;
407 default:
408 break;
410 break;
414 /* The "length" of a wibox is always chosen to be the optimal size (non-floating).
415 * The "width" of a wibox is kept if it exists.
417 switch(wibox->position)
419 case Right:
420 wingeom.height = area.height - 2 * wibox->sw.border.width;
421 wingeom.width = wibox->sw.geometry.width > 0 ? wibox->sw.geometry.width : 1.5 * globalconf.font->height;
422 wingeom.x = area.x + area.width - wingeom.width;
423 switch(wibox->align)
425 default:
426 wingeom.y = area.y;
427 break;
428 case AlignRight:
429 wingeom.y = area.y + area.height - wingeom.height;
430 break;
431 case AlignCenter:
432 wingeom.y = (area.y + area.height - wingeom.height) / 2;
433 break;
435 break;
436 case Left:
437 wingeom.height = area.height - 2 * wibox->sw.border.width;
438 wingeom.width = wibox->sw.geometry.width > 0 ? wibox->sw.geometry.width : 1.5 * globalconf.font->height;
439 wingeom.x = area.x;
440 switch(wibox->align)
442 default:
443 wingeom.y = (area.y + area.height) - wingeom.height;
444 break;
445 case AlignRight:
446 wingeom.y = area.y;
447 break;
448 case AlignCenter:
449 wingeom.y = (area.y + area.height - wingeom.height) / 2;
451 break;
452 case Bottom:
453 wingeom.height = wibox->sw.geometry.height > 0 ? wibox->sw.geometry.height : 1.5 * globalconf.font->height;
454 wingeom.width = area.width - 2 * wibox->sw.border.width;
455 wingeom.y = (area.y + area.height) - wingeom.height;
456 wingeom.x = area.x;
457 switch(wibox->align)
459 default:
460 break;
461 case AlignRight:
462 wingeom.x += area.width - wingeom.width;
463 break;
464 case AlignCenter:
465 wingeom.x += (area.width - wingeom.width) / 2;
466 break;
468 break;
469 case Top:
470 wingeom.height = wibox->sw.geometry.height > 0 ? wibox->sw.geometry.height : 1.5 * globalconf.font->height;
471 wingeom.width = area.width - 2 * wibox->sw.border.width;
472 wingeom.x = area.x;
473 wingeom.y = area.y;
474 switch(wibox->align)
476 default:
477 break;
478 case AlignRight:
479 wingeom.x += area.width - wingeom.width;
480 break;
481 case AlignCenter:
482 wingeom.x += (area.width - wingeom.width) / 2;
483 break;
485 break;
486 case Floating:
487 /* Floating wiboxes are not handled here, but in
488 * wibox_position_update_floating(), but the compiler insists...
490 break;
493 /* same window size and position ? */
494 if(wingeom.width != wibox->sw.geometry.width
495 || wingeom.height != wibox->sw.geometry.height)
496 wibox_resize(wibox, wingeom.width, wingeom.height);
498 if(wingeom.x != wibox->sw.geometry.x
499 || wingeom.y != wibox->sw.geometry.y)
500 wibox_move(wibox, wingeom.x, wingeom.y);
503 /** Update the wibox position. It deletes every wibox resources and
504 * create them back.
505 * \param wibox The wibox.
507 void
508 wibox_position_update(wibox_t *wibox)
510 if(wibox->position == Floating)
511 wibox_position_update_floating(wibox);
512 else
513 wibox_position_update_non_floating(wibox);
516 /** Get a wibox by its window.
517 * \param w The window id.
518 * \return A wibox if found, NULL otherwise.
520 wibox_t *
521 wibox_getbywin(xcb_window_t win)
523 foreach(screen, globalconf.screens)
524 foreach(w, screen->wiboxes)
525 if((*w)->sw.window == win)
526 return *w;
528 foreach(_c, globalconf.clients)
530 client_t *c = *_c;
531 if(c->titlebar && c->titlebar->sw.window == win)
532 return c->titlebar;
535 return NULL;
538 /** Draw a wibox.
539 * \param wibox The wibox to draw.
541 static void
542 wibox_draw(wibox_t *wibox)
544 if(wibox->isvisible)
546 widget_render(wibox);
547 simplewindow_refresh_pixmap(&wibox->sw);
549 wibox->need_update = false;
552 wibox_systray_refresh(wibox);
555 /** Refresh all wiboxes.
557 void
558 wibox_refresh(void)
560 foreach(screen, globalconf.screens)
561 foreach(w, screen->wiboxes)
562 if((*w)->need_update)
563 wibox_draw(*w);
565 foreach(_c, globalconf.clients)
567 client_t *c = *_c;
568 if(c->titlebar && c->titlebar->need_update)
569 wibox_draw(c->titlebar);
573 /** Reposition all wiboxes.
575 void
576 wibox_update_positions(void)
578 foreach(screen, globalconf.screens)
579 foreach(w, screen->wiboxes)
580 wibox_position_update(*w);
583 /** Set a wibox visible or not.
584 * \param wibox The wibox.
585 * \param v The visible value.
587 static void
588 wibox_setvisible(wibox_t *wibox, bool v)
590 if(v != wibox->isvisible)
592 wibox->isvisible = v;
593 wibox->mouse_over = NULL;
595 if(wibox->screen != NULL)
597 if(wibox->isvisible)
598 wibox_map(wibox);
599 else
600 xcb_unmap_window(globalconf.connection, wibox->sw.window);
602 /* kick out systray if needed */
603 wibox_systray_refresh(wibox);
605 /* All the other wibox and ourselves need to be repositioned */
606 foreach(w, wibox->screen->wiboxes)
607 wibox_position_update(*w);
612 /** Remove a wibox from a screen.
613 * \param wibox Wibox to detach from screen.
615 void
616 wibox_detach(wibox_t *wibox)
618 if(wibox->screen != NULL)
620 bool v;
622 /* save visible state */
623 v = wibox->isvisible;
624 wibox->isvisible = false;
625 wibox_systray_refresh(wibox);
626 wibox_position_update(wibox);
627 /* restore position */
628 wibox->isvisible = v;
630 wibox->mouse_over = NULL;
632 simplewindow_wipe(&wibox->sw);
634 wibox->screen->need_arrange = true;
636 foreach(item, wibox->screen->wiboxes)
637 if(*item == wibox)
639 wibox_array_remove(&wibox->screen->wiboxes, item);
640 break;
643 wibox->screen = NULL;
644 wibox_unref(globalconf.L, wibox);
648 /** Attach a wibox that is on top of the stack.
649 * \param s The screen to attach the wibox to.
651 static void
652 wibox_attach(screen_t *s)
654 int phys_screen = screen_virttophys(screen_array_indexof(&globalconf.screens, s));
656 wibox_t *wibox = wibox_ref(globalconf.L);
658 wibox_detach(wibox);
660 /* Set the wibox screen */
661 wibox->screen = s;
663 /* Check that the wibox coordinates matches the screen. */
664 screen_t *cscreen =
665 screen_getbycoord(wibox->screen, wibox->sw.geometry.x, wibox->sw.geometry.y);
667 /* If it does not match, move it to the screen coordinates */
668 if(cscreen != wibox->screen)
669 wibox_move(wibox, s->geometry.x, s->geometry.y);
671 wibox_array_append(&s->wiboxes, wibox);
673 /* compute x/y/width/height if not set */
674 wibox_position_update(wibox);
676 simplewindow_init(&wibox->sw, phys_screen,
677 wibox->sw.geometry,
678 wibox->sw.border.width,
679 wibox->sw.orientation,
680 &wibox->sw.ctx.fg, &wibox->sw.ctx.bg);
682 simplewindow_border_color_set(&wibox->sw, &wibox->sw.border.color);
684 simplewindow_cursor_set(&wibox->sw,
685 xcursor_new(globalconf.connection, xcursor_font_fromstr(wibox->cursor)));
687 /* All the other wibox and ourselves need to be repositioned */
688 foreach(w, s->wiboxes)
689 wibox_position_update(*w);
691 ewmh_update_workarea(phys_screen);
693 if(wibox->isvisible)
694 wibox_map(wibox);
695 else
696 wibox_need_update(wibox);
699 /** Create a new wibox.
700 * \param L The Lua VM state.
702 * \luastack
703 * \lparam A table with optionaly defined values:
704 * position, align, fg, bg, border_width, border_color, ontop, width and height.
705 * \lreturn A brand new wibox.
707 static int
708 luaA_wibox_new(lua_State *L)
710 wibox_t *w;
711 const char *buf;
712 size_t len;
713 xcolor_init_request_t reqs[3];
714 int i, reqs_nbr = -1;
716 luaA_checktable(L, 2);
718 w = wibox_new(L);
719 w->widgets_table = LUA_REFNIL;
721 w->sw.ctx.fg = globalconf.colors.fg;
722 if((buf = luaA_getopt_lstring(L, 2, "fg", NULL, &len)))
723 reqs[++reqs_nbr] = xcolor_init_unchecked(&w->sw.ctx.fg, buf, len);
725 w->sw.ctx.bg = globalconf.colors.bg;
726 if((buf = luaA_getopt_lstring(L, 2, "bg", NULL, &len)))
727 reqs[++reqs_nbr] = xcolor_init_unchecked(&w->sw.ctx.bg, buf, len);
729 w->sw.border.color = globalconf.colors.bg;
730 if((buf = luaA_getopt_lstring(L, 2, "border_color", NULL, &len)))
731 reqs[++reqs_nbr] = xcolor_init_unchecked(&w->sw.border.color, buf, len);
733 w->ontop = luaA_getopt_boolean(L, 2, "ontop", false);
735 buf = luaA_getopt_lstring(L, 2, "align", "left", &len);
736 w->align = draw_align_fromstr(buf, len);
738 w->sw.border.width = luaA_getopt_number(L, 2, "border_width", 0);
740 buf = luaA_getopt_lstring(L, 2, "position", "top", &len);
742 switch((w->position = position_fromstr(buf, len)))
744 case Bottom:
745 case Top:
746 case Floating:
747 w->sw.orientation = East;
748 break;
749 case Left:
750 w->sw.orientation = North;
751 break;
752 case Right:
753 w->sw.orientation = South;
754 break;
757 w->sw.geometry.x = luaA_getopt_number(L, 2, "x", 0);
758 w->sw.geometry.y = luaA_getopt_number(L, 2, "y", 0);
759 w->sw.geometry.width = luaA_getopt_number(L, 2, "width", 0);
760 w->sw.geometry.height = luaA_getopt_number(L, 2, "height", 0);
762 w->isvisible = true;
763 w->cursor = a_strdup("left_ptr");
765 w->mouse_enter = w->mouse_leave = LUA_REFNIL;
767 for(i = 0; i <= reqs_nbr; i++)
768 xcolor_init_reply(reqs[i]);
770 return 1;
773 /** Rebuild wibox widgets list.
774 * \param L The Lua VM state.
775 * \param wibox The wibox.
777 static void
778 wibox_widgets_table_build(lua_State *L, wibox_t *wibox)
780 widget_node_array_wipe(&wibox->widgets);
781 widget_node_array_init(&wibox->widgets);
782 luaA_table2widgets(L, &wibox->widgets);
783 wibox_need_update(wibox);
786 /** Check if a wibox widget table has an item.
787 * \param L The Lua VM state.
788 * \param wibox The wibox.
789 * \param item The item to look for.
791 static bool
792 luaA_wibox_hasitem(lua_State *L, wibox_t *wibox, const void *item)
794 if(wibox->widgets_table != LUA_REFNIL)
796 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, wibox->widgets_table);
797 if(lua_topointer(L, -1) == item || luaA_hasitem(L, item))
798 return true;
800 return false;
803 /** Invalidate a wibox by a Lua object (table, etc).
804 * \param L The Lua VM state.
805 * \param item The object identifier.
807 void
808 luaA_wibox_invalidate_byitem(lua_State *L, const void *item)
810 foreach(screen, globalconf.screens)
811 foreach(w, screen->wiboxes)
813 wibox_t *wibox = *w;
814 if(luaA_wibox_hasitem(L, wibox, item))
816 /* recompute widget node list */
817 wibox_widgets_table_build(L, wibox);
818 lua_pop(L, 1); /* remove widgets table */
823 foreach(_c, globalconf.clients)
825 client_t *c = *_c;
826 if(c->titlebar && luaA_wibox_hasitem(L, c->titlebar, item))
828 /* recompute widget node list */
829 wibox_widgets_table_build(L, c->titlebar);
830 lua_pop(L, 1); /* remove widgets table */
835 /** Wibox object.
836 * \param L The Lua VM state.
837 * \return The number of elements pushed on stack.
838 * \luastack
839 * \lfield screen Screen number.
840 * \lfield client The client attached to (titlebar).
841 * \lfield border_width Border width.
842 * \lfield border_color Border color.
843 * \lfield align The alignment.
844 * \lfield fg Foreground color.
845 * \lfield bg Background color.
846 * \lfield bg_image Background image.
847 * \lfield position The position.
848 * \lfield ontop On top of other windows.
849 * \lfield cursor The mouse cursor.
850 * \lfield visible Visibility.
851 * \lfield orientation The drawing orientation: east, north or south.
852 * \lfield widgets An array with all widgets drawn on this wibox.
853 * \lfield opacity The opacity of the wibox, between 0 and 1.
854 * \lfield mouse_enter A function to execute when the mouse enter the widget.
855 * \lfield mouse_leave A function to execute when the mouse leave the widget.
857 static int
858 luaA_wibox_index(lua_State *L)
860 size_t len;
861 wibox_t *wibox = luaL_checkudata(L, 1, "wibox");
862 const char *attr = luaL_checklstring(L, 2, &len);
864 if(luaA_usemetatable(L, 1, 2))
865 return 1;
867 switch(a_tokenize(attr, len))
869 case A_TK_VISIBLE:
870 lua_pushboolean(L, wibox->isvisible);
871 break;
872 case A_TK_CLIENT:
873 return client_push(L, client_getbytitlebar(wibox));
874 case A_TK_SCREEN:
875 if(!wibox->screen)
876 return 0;
877 lua_pushnumber(L, screen_array_indexof(&globalconf.screens, wibox->screen) + 1);
878 break;
879 case A_TK_BORDER_WIDTH:
880 lua_pushnumber(L, wibox->sw.border.width);
881 break;
882 case A_TK_BORDER_COLOR:
883 luaA_pushxcolor(L, &wibox->sw.border.color);
884 break;
885 case A_TK_ALIGN:
886 lua_pushstring(L, draw_align_tostr(wibox->align));
887 break;
888 case A_TK_FG:
889 luaA_pushxcolor(L, &wibox->sw.ctx.fg);
890 break;
891 case A_TK_BG:
892 luaA_pushxcolor(L, &wibox->sw.ctx.bg);
893 break;
894 case A_TK_BG_IMAGE:
895 image_push(L, wibox->bg_image);
896 break;
897 case A_TK_POSITION:
898 lua_pushstring(L, position_tostr(wibox->position));
899 break;
900 case A_TK_ONTOP:
901 lua_pushboolean(L, wibox->ontop);
902 break;
903 case A_TK_ORIENTATION:
904 lua_pushstring(L, orientation_tostr(wibox->sw.orientation));
905 break;
906 case A_TK_WIDGETS:
907 if(wibox->widgets_table != LUA_REFNIL)
908 lua_rawgeti(L, LUA_REGISTRYINDEX, wibox->widgets_table);
909 else
910 lua_pushnil(L);
911 break;
912 case A_TK_CURSOR:
913 lua_pushstring(L, wibox->cursor);
914 break;
915 case A_TK_OPACITY:
917 double d;
918 if ((d = window_opacity_get(wibox->sw.window)) >= 0)
919 lua_pushnumber(L, d);
920 else
921 return 0;
923 break;
924 case A_TK_MOUSE_ENTER:
925 if(wibox->mouse_enter != LUA_REFNIL)
926 lua_rawgeti(L, LUA_REGISTRYINDEX, wibox->mouse_enter);
927 else
928 return 0;
929 return 1;
930 case A_TK_MOUSE_LEAVE:
931 if(wibox->mouse_leave != LUA_REFNIL)
932 lua_rawgeti(L, LUA_REGISTRYINDEX, wibox->mouse_leave);
933 else
934 return 0;
935 return 1;
936 default:
937 return 0;
940 return 1;
943 /* Set or get the wibox geometry.
944 * \param L The Lua VM state.
945 * \return The number of elements pushed on stack.
946 * \luastack
947 * \lparam An optional table with wibox geometry.
948 * \lreturn The wibox geometry.
950 static int
951 luaA_wibox_geometry(lua_State *L)
953 wibox_t *wibox = luaL_checkudata(L, 1, "wibox");
955 if(lua_gettop(L) == 2)
957 area_t wingeom;
959 luaA_checktable(L, 2);
960 wingeom.x = luaA_getopt_number(L, 2, "x", wibox->sw.geometry.x);
961 wingeom.y = luaA_getopt_number(L, 2, "y", wibox->sw.geometry.y);
962 wingeom.width = luaA_getopt_number(L, 2, "width", wibox->sw.geometry.width);
963 wingeom.height = luaA_getopt_number(L, 2, "height", wibox->sw.geometry.height);
965 switch(wibox->type)
967 case WIBOX_TYPE_TITLEBAR:
968 wibox_resize(wibox, wingeom.width, wingeom.height);
969 break;
970 case WIBOX_TYPE_NORMAL:
971 if(wibox->position == Floating)
972 wibox_moveresize(wibox, wingeom);
973 else if(wingeom.width != wibox->sw.geometry.width
974 || wingeom.height != wibox->sw.geometry.height)
976 wibox_resize(wibox, wingeom.width, wingeom.height);
977 wibox->screen->need_arrange = true;
979 break;
983 return luaA_pusharea(L, wibox->sw.geometry);
986 /** Wibox newindex.
987 * \param L The Lua VM state.
988 * \return The number of elements pushed on stack.
990 static int
991 luaA_wibox_newindex(lua_State *L)
993 size_t len;
994 wibox_t *wibox = luaL_checkudata(L, 1, "wibox");
995 const char *buf, *attr = luaL_checklstring(L, 2, &len);
996 awesome_token_t tok;
998 switch((tok = a_tokenize(attr, len)))
1000 bool b;
1002 case A_TK_FG:
1003 if((buf = luaL_checklstring(L, 3, &len)))
1004 if(xcolor_init_reply(xcolor_init_unchecked(&wibox->sw.ctx.fg, buf, len)))
1005 wibox->need_update = true;
1006 break;
1007 case A_TK_BG:
1008 if((buf = luaL_checklstring(L, 3, &len)))
1009 if(xcolor_init_reply(xcolor_init_unchecked(&wibox->sw.ctx.bg, buf, len)))
1010 wibox->need_update = true;
1011 break;
1012 case A_TK_BG_IMAGE:
1013 image_unref(L, wibox->bg_image);
1014 wibox->bg_image = image_ref(L);
1015 wibox->need_update = true;
1016 break;
1017 case A_TK_ALIGN:
1018 buf = luaL_checklstring(L, 3, &len);
1019 wibox->align = draw_align_fromstr(buf, len);
1020 switch(wibox->type)
1022 case WIBOX_TYPE_NORMAL:
1023 wibox_position_update(wibox);
1024 break;
1025 case WIBOX_TYPE_TITLEBAR:
1026 titlebar_update_geometry(client_getbytitlebar(wibox));
1027 break;
1029 break;
1030 case A_TK_POSITION:
1031 switch(wibox->type)
1033 case WIBOX_TYPE_TITLEBAR:
1034 return luaA_titlebar_newindex(L, wibox, tok);
1035 case WIBOX_TYPE_NORMAL:
1036 if((buf = luaL_checklstring(L, 3, &len)))
1037 wibox_setposition(wibox, position_fromstr(buf, len));
1038 break;
1040 break;
1041 case A_TK_CLIENT:
1042 /* first detach */
1043 if(lua_isnil(L, 3))
1044 titlebar_client_detach(client_getbytitlebar(wibox));
1045 else
1047 client_t *c = luaA_client_checkudata(L, -1);
1048 lua_pushvalue(L, 1);
1049 titlebar_client_attach(c);
1051 break;
1052 case A_TK_CURSOR:
1053 if((buf = luaL_checkstring(L, 3)))
1055 uint16_t cursor_font = xcursor_font_fromstr(buf);
1056 if(cursor_font)
1058 xcb_cursor_t cursor = xcursor_new(globalconf.connection, cursor_font);
1059 p_delete(&wibox->cursor);
1060 wibox->cursor = a_strdup(buf);
1061 simplewindow_cursor_set(&wibox->sw, cursor);
1064 break;
1065 case A_TK_SCREEN:
1066 if(lua_isnil(L, 3))
1068 wibox_detach(wibox);
1069 titlebar_client_detach(client_getbytitlebar(wibox));
1071 else
1073 int screen = luaL_checknumber(L, 3) - 1;
1074 luaA_checkscreen(screen);
1075 if(!wibox->screen || screen != screen_array_indexof(&globalconf.screens, wibox->screen))
1077 titlebar_client_detach(client_getbytitlebar(wibox));
1078 lua_pushvalue(L, 1);
1079 wibox_attach(&globalconf.screens.tab[screen]);
1082 break;
1083 case A_TK_ONTOP:
1084 b = luaA_checkboolean(L, 3);
1085 if(b != wibox->ontop)
1087 wibox->ontop = b;
1088 client_stack();
1090 break;
1091 case A_TK_ORIENTATION:
1092 if((buf = luaL_checklstring(L, 3, &len)))
1094 simplewindow_orientation_set(&wibox->sw, orientation_fromstr(buf, len));
1095 wibox_need_update(wibox);
1097 break;
1098 case A_TK_BORDER_COLOR:
1099 if((buf = luaL_checklstring(L, 3, &len)))
1100 if(xcolor_init_reply(xcolor_init_unchecked(&wibox->sw.border.color, buf, len)))
1101 if(wibox->sw.window)
1102 simplewindow_border_color_set(&wibox->sw, &wibox->sw.border.color);
1103 break;
1104 case A_TK_VISIBLE:
1105 b = luaA_checkboolean(L, 3);
1106 if(b != wibox->isvisible)
1107 switch(wibox->type)
1109 case WIBOX_TYPE_NORMAL:
1110 wibox_setvisible(wibox, b);
1111 break;
1112 case WIBOX_TYPE_TITLEBAR:
1113 titlebar_set_visible(wibox, b);
1114 break;
1116 break;
1117 case A_TK_WIDGETS:
1118 if(luaA_isloop(L, 3))
1120 luaA_warn(L, "table is looping, cannot use this as widget table");
1121 return 0;
1123 /* register object */
1124 luaA_register(L, 3, &wibox->widgets_table);
1125 /* duplicate table because next function will eat it */
1126 lua_pushvalue(L, -1);
1127 /* recompute widget node list */
1128 wibox_widgets_table_build(L, wibox);
1129 luaA_table2wtable(L);
1130 break;
1131 case A_TK_OPACITY:
1132 if(lua_isnil(L, 3))
1133 window_opacity_set(wibox->sw.window, -1);
1134 else
1136 double d = luaL_checknumber(L, 3);
1137 if(d >= 0 && d <= 1)
1138 window_opacity_set(wibox->sw.window, d);
1140 break;
1141 case A_TK_MOUSE_ENTER:
1142 luaA_registerfct(L, 3, &wibox->mouse_enter);
1143 return 0;
1144 case A_TK_MOUSE_LEAVE:
1145 luaA_registerfct(L, 3, &wibox->mouse_leave);
1146 return 0;
1147 default:
1148 switch(wibox->type)
1150 case WIBOX_TYPE_TITLEBAR:
1151 return luaA_titlebar_newindex(L, wibox, tok);
1152 case WIBOX_TYPE_NORMAL:
1153 break;
1157 return 0;
1160 /** Get or set mouse buttons bindings to a wibox.
1161 * \param L The Lua VM state.
1162 * \luastack
1163 * \lvalue A wibox.
1164 * \lparam An array of mouse button bindings objects, or nothing.
1165 * \return The array of mouse button bindings objects of this wibox.
1167 static int
1168 luaA_wibox_buttons(lua_State *L)
1170 wibox_t *wibox = luaL_checkudata(L, 1, "wibox");
1171 button_array_t *buttons = &wibox->buttons;
1173 if(lua_gettop(L) == 2)
1175 luaA_button_array_set(L, 2, buttons);
1176 return 1;
1179 return luaA_button_array_get(L, buttons);
1182 const struct luaL_reg awesome_wibox_methods[] =
1184 { "__call", luaA_wibox_new },
1185 { NULL, NULL }
1187 const struct luaL_reg awesome_wibox_meta[] =
1189 { "buttons", luaA_wibox_buttons },
1190 { "geometry", luaA_wibox_geometry },
1191 { "__index", luaA_wibox_index },
1192 { "__newindex", luaA_wibox_newindex },
1193 { "__gc", luaA_wibox_gc },
1194 { "__tostring", luaA_wibox_tostring },
1195 { NULL, NULL },
1198 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80