lua: color pushlstring gets real len
[awesome.git] / wibox.c
blobcc8fc3c56e25c35276de6b9971e8556ba5f2ff47
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"
28 extern awesome_t globalconf;
30 DO_LUA_NEW(extern, wibox_t, wibox, "wibox", wibox_ref)
31 DO_LUA_GC(wibox_t, wibox, "wibox", wibox_unref)
32 DO_LUA_EQ(wibox_t, wibox, "wibox")
34 static void
35 wibox_move(wibox_t *wibox, int16_t x, int16_t y)
37 if(wibox->sw.window)
38 simplewindow_move(&wibox->sw, x, y);
39 else
41 wibox->sw.geometry.x = x;
42 wibox->sw.geometry.y = y;
46 static void
47 wibox_resize(wibox_t *wibox, uint16_t width, uint16_t height)
49 if(wibox->sw.window)
50 simplewindow_resize(&wibox->sw, width, height);
51 else
53 wibox->sw.geometry.width = width;
54 wibox->sw.geometry.height = height;
56 wibox->need_update = true;
59 static void
60 wibox_setposition(wibox_t *wibox, position_t p)
62 if(p != wibox->position)
64 switch((wibox->position = p))
66 case Bottom:
67 case Top:
68 case Floating:
69 simplewindow_orientation_set(&wibox->sw, East);
70 break;
71 case Left:
72 simplewindow_orientation_set(&wibox->sw, North);
73 break;
74 case Right:
75 simplewindow_orientation_set(&wibox->sw, South);
76 break;
78 /* reset width/height to 0 */
79 if(wibox->position != Floating)
80 wibox->sw.geometry.width = wibox->sw.geometry.height = 0;
82 /* recompute position */
83 wibox_position_update(wibox);
85 /* reset all wibox position */
86 wibox_array_t *w = &globalconf.screens[wibox->screen].wiboxes;
87 for(int i = 0; i < w->len; i++)
88 wibox_position_update(w->tab[i]);
90 ewmh_update_workarea(screen_virttophys(wibox->screen));
92 wibox->need_update = true;
96 /** Kick out systray windows.
97 * \param phys_screen Physical screen number.
99 static void
100 wibox_systray_kickout(int phys_screen)
102 xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
104 /* Who! Check that we're not deleting a wibox with a systray, because it
105 * may be its parent. If so, we reparent to root before, otherwise it will
106 * hurt very much. */
107 xcb_reparent_window(globalconf.connection,
108 globalconf.screens[phys_screen].systray.window,
109 s->root, -512, -512);
111 globalconf.screens[phys_screen].systray.parent = s->root;
114 static void
115 wibox_systray_refresh(wibox_t *wibox)
117 if(wibox->screen == SCREEN_UNDEF)
118 return;
120 for(int i = 0; i < wibox->widgets.len; i++)
122 widget_node_t *systray = &wibox->widgets.tab[i];
123 if(systray->widget->type == systray_new)
125 uint32_t config_back[] = { wibox->sw.ctx.bg.pixel };
126 uint32_t config_win_vals[4];
127 uint32_t config_win_vals_off[2] = { -512, -512 };
128 xembed_window_t *em;
129 position_t pos;
130 int phys_screen = wibox->sw.ctx.phys_screen;
132 if(wibox->isvisible
133 && systray->widget->isvisible
134 && systray->geometry.width)
136 pos = wibox->position;
138 /* Set background of the systray window. */
139 xcb_change_window_attributes(globalconf.connection,
140 globalconf.screens[phys_screen].systray.window,
141 XCB_CW_BACK_PIXEL, config_back);
142 /* Map it. */
143 xcb_map_window(globalconf.connection, globalconf.screens[phys_screen].systray.window);
144 /* Move it. */
145 switch(wibox->position)
147 case Left:
148 config_win_vals[0] = systray->geometry.y;
149 config_win_vals[1] = wibox->sw.geometry.height - systray->geometry.x - systray->geometry.width;
150 config_win_vals[2] = systray->geometry.height;
151 config_win_vals[3] = systray->geometry.width;
152 break;
153 case Right:
154 config_win_vals[0] = systray->geometry.y;
155 config_win_vals[1] = systray->geometry.x;
156 config_win_vals[2] = systray->geometry.height;
157 config_win_vals[3] = systray->geometry.width;
158 break;
159 default:
160 config_win_vals[0] = systray->geometry.x;
161 config_win_vals[1] = systray->geometry.y;
162 config_win_vals[2] = systray->geometry.width;
163 config_win_vals[3] = systray->geometry.height;
164 break;
166 /* reparent */
167 if(globalconf.screens[phys_screen].systray.parent != wibox->sw.window)
169 xcb_reparent_window(globalconf.connection,
170 globalconf.screens[phys_screen].systray.window,
171 wibox->sw.window,
172 config_win_vals[0], config_win_vals[1]);
173 globalconf.screens[phys_screen].systray.parent = wibox->sw.window;
175 xcb_configure_window(globalconf.connection,
176 globalconf.screens[phys_screen].systray.window,
177 XCB_CONFIG_WINDOW_X
178 | XCB_CONFIG_WINDOW_Y
179 | XCB_CONFIG_WINDOW_WIDTH
180 | XCB_CONFIG_WINDOW_HEIGHT,
181 config_win_vals);
182 /* width = height = systray height */
183 config_win_vals[2] = config_win_vals[3] = systray->geometry.height;
184 config_win_vals[0] = 0;
186 else
187 return wibox_systray_kickout(phys_screen);
189 switch(pos)
191 case Left:
192 config_win_vals[1] = systray->geometry.width - config_win_vals[3];
193 for(em = globalconf.embedded; em; em = em->next)
194 if(em->phys_screen == phys_screen)
196 if(config_win_vals[1] - config_win_vals[2] >= (uint32_t) wibox->sw.geometry.y)
198 xcb_map_window(globalconf.connection, em->win);
199 xcb_configure_window(globalconf.connection, em->win,
200 XCB_CONFIG_WINDOW_X
201 | XCB_CONFIG_WINDOW_Y
202 | XCB_CONFIG_WINDOW_WIDTH
203 | XCB_CONFIG_WINDOW_HEIGHT,
204 config_win_vals);
205 config_win_vals[1] -= config_win_vals[3];
207 else
208 xcb_configure_window(globalconf.connection, em->win,
209 XCB_CONFIG_WINDOW_X
210 | XCB_CONFIG_WINDOW_Y,
211 config_win_vals_off);
213 break;
214 case Right:
215 config_win_vals[1] = 0;
216 for(em = globalconf.embedded; em; em = em->next)
217 if(em->phys_screen == phys_screen)
219 if(config_win_vals[1] + config_win_vals[3] <= (uint32_t) wibox->sw.geometry.y + wibox->sw.geometry.width)
221 xcb_map_window(globalconf.connection, em->win);
222 xcb_configure_window(globalconf.connection, em->win,
223 XCB_CONFIG_WINDOW_X
224 | XCB_CONFIG_WINDOW_Y
225 | XCB_CONFIG_WINDOW_WIDTH
226 | XCB_CONFIG_WINDOW_HEIGHT,
227 config_win_vals);
228 config_win_vals[1] += config_win_vals[3];
230 else
231 xcb_configure_window(globalconf.connection, em->win,
232 XCB_CONFIG_WINDOW_X
233 | XCB_CONFIG_WINDOW_Y,
234 config_win_vals_off);
236 break;
237 case Floating:
238 case Top:
239 case Bottom:
240 config_win_vals[1] = 0;
241 for(em = globalconf.embedded; em; em = em->next)
242 if(em->phys_screen == phys_screen)
244 /* if(x + width < systray.x + systray.width) */
245 if(config_win_vals[0] + config_win_vals[2] <= (uint32_t) AREA_RIGHT(systray->geometry) + wibox->sw.geometry.x)
247 xcb_map_window(globalconf.connection, em->win);
248 xcb_configure_window(globalconf.connection, em->win,
249 XCB_CONFIG_WINDOW_X
250 | XCB_CONFIG_WINDOW_Y
251 | XCB_CONFIG_WINDOW_WIDTH
252 | XCB_CONFIG_WINDOW_HEIGHT,
253 config_win_vals);
254 config_win_vals[0] += config_win_vals[2];
256 else
257 xcb_configure_window(globalconf.connection, em->win,
258 XCB_CONFIG_WINDOW_X
259 | XCB_CONFIG_WINDOW_Y,
260 config_win_vals_off);
262 break;
264 break;
269 /** Update the wibox position. It deletes every wibox resources and
270 * create them back.
271 * \param wibox The wibox.
273 void
274 wibox_position_update(wibox_t *wibox)
276 area_t area, wingeom = wibox->sw.geometry;
277 bool ignore = false;
279 globalconf.screens[wibox->screen].need_arrange = true;
281 area = screen_area_get(wibox->screen, NULL,
282 &globalconf.screens[wibox->screen].padding, true);
284 /* Top and Bottom wibox_t have prio */
285 for(int i = 0; i < globalconf.screens[wibox->screen].wiboxes.len; i++)
287 wibox_t *w = globalconf.screens[wibox->screen].wiboxes.tab[i];
288 /* Ignore every wibox after me that is in the same position */
289 if(wibox == w)
291 ignore = true;
292 continue;
294 else if((ignore && wibox->position == w->position) || !w->isvisible)
295 continue;
296 switch(w->position)
298 case Floating:
299 break;
300 case Left:
301 switch(wibox->position)
303 case Left:
304 area.x += wibox->sw.geometry.height;
305 break;
306 default:
307 break;
309 break;
310 case Right:
311 switch(wibox->position)
313 case Right:
314 area.x -= wibox->sw.geometry.height;
315 break;
316 default:
317 break;
319 break;
320 case Top:
321 switch(wibox->position)
323 case Top:
324 area.y += w->sw.geometry.height;
325 break;
326 case Left:
327 case Right:
328 area.height -= w->sw.geometry.height;
329 area.y += w->sw.geometry.height;
330 break;
331 default:
332 break;
334 break;
335 case Bottom:
336 switch(wibox->position)
338 case Bottom:
339 area.y -= w->sw.geometry.height;
340 break;
341 case Left:
342 case Right:
343 area.height -= w->sw.geometry.height;
344 break;
345 default:
346 break;
348 break;
352 switch(wibox->position)
354 case Right:
355 wingeom.height = wibox->sw.geometry.height > 0 ?
356 wibox->sw.geometry.height : area.height - 2 * wibox->sw.border.width;
357 wingeom.width = wibox->sw.geometry.width > 0 ? wibox->sw.geometry.width : 1.5 * globalconf.font->height;
358 wingeom.x = area.x + area.width - wingeom.width - 2 * wibox->sw.border.width;
359 switch(wibox->align)
361 default:
362 wingeom.y = area.y;
363 break;
364 case AlignRight:
365 wingeom.y = area.y + area.height - wingeom.height;
366 break;
367 case AlignCenter:
368 wingeom.y = (area.y + area.height - wingeom.height) / 2;
369 break;
371 break;
372 case Left:
373 wingeom.height = wibox->sw.geometry.height > 0 ?
374 wibox->sw.geometry.height : area.height - 2 * wibox->sw.border.width;
375 wingeom.width = wibox->sw.geometry.width > 0 ? wibox->sw.geometry.width : 1.5 * globalconf.font->height;
376 wingeom.x = area.x;
377 switch(wibox->align)
379 default:
380 wingeom.y = (area.y + area.height) - wingeom.height - 2 * wibox->sw.border.width;
381 break;
382 case AlignRight:
383 wingeom.y = area.y;
384 break;
385 case AlignCenter:
386 wingeom.y = (area.y + area.height - wingeom.height) / 2;
388 break;
389 case Bottom:
390 wingeom.height = wibox->sw.geometry.height > 0 ? wibox->sw.geometry.height : 1.5 * globalconf.font->height;
391 wingeom.width = wibox->sw.geometry.width > 0 ?
392 wibox->sw.geometry.width : area.width - 2 * wibox->sw.border.width;
393 wingeom.y = (area.y + area.height) - wingeom.height - 2 * wibox->sw.border.width;
394 wingeom.x = area.x;
395 switch(wibox->align)
397 default:
398 break;
399 case AlignRight:
400 wingeom.x += area.width - wingeom.width;
401 break;
402 case AlignCenter:
403 wingeom.x += (area.width - wingeom.width) / 2;
404 break;
406 break;
407 case Top:
408 wingeom.height = wibox->sw.geometry.height > 0 ? wibox->sw.geometry.height : 1.5 * globalconf.font->height;
409 wingeom.width = wibox->sw.geometry.width > 0 ?
410 wibox->sw.geometry.width : area.width - 2 * wibox->sw.border.width;
411 wingeom.x = area.x;
412 wingeom.y = area.y;
413 switch(wibox->align)
415 default:
416 break;
417 case AlignRight:
418 wingeom.x += area.width - wingeom.width;
419 break;
420 case AlignCenter:
421 wingeom.x += (area.width - wingeom.width) / 2;
422 break;
424 break;
425 case Floating:
426 break;
429 /* same window size and position ? */
430 if(wingeom.width != wibox->sw.geometry.width
431 || wingeom.height != wibox->sw.geometry.height)
432 wibox_resize(wibox, wingeom.width, wingeom.height);
434 if(wingeom.x != wibox->sw.geometry.x
435 || wingeom.y != wibox->sw.geometry.y)
436 wibox_move(wibox, wingeom.x, wingeom.y);
439 /** Delete a wibox.
440 * \param wibox wibox to delete.
442 void
443 wibox_delete(wibox_t **wibox)
445 simplewindow_wipe(&(*wibox)->sw);
446 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*wibox)->widgets_table);
447 widget_node_array_wipe(&(*wibox)->widgets);
448 p_delete(wibox);
451 /** Get a wibox by its window.
452 * \param w The window id.
453 * \return A wibox if found, NULL otherwise.
455 wibox_t *
456 wibox_getbywin(xcb_window_t w)
458 for(int screen = 0; screen < globalconf.nscreen; screen++)
459 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
461 wibox_t *s = globalconf.screens[screen].wiboxes.tab[i];
462 if(s->sw.window == w)
463 return s;
466 for(client_t *c = globalconf.clients; c; c = c->next)
467 if(c->titlebar && c->titlebar->sw.window == w)
468 return c->titlebar;
470 return NULL;
473 /** Draw a wibox.
474 * \param wibox The wibox to draw.
476 static void
477 wibox_draw(wibox_t *wibox)
479 if(wibox->isvisible)
481 widget_render(&wibox->widgets, &wibox->sw.ctx, wibox->sw.gc,
482 wibox->sw.pixmap,
483 wibox->screen, wibox->sw.orientation,
484 wibox->sw.geometry.x, wibox->sw.geometry.y,
485 wibox);
486 simplewindow_refresh_pixmap(&wibox->sw);
488 wibox->need_update = false;
491 wibox_systray_refresh(wibox);
494 /** Refresh all wiboxes.
496 void
497 wibox_refresh(void)
499 for(int screen = 0; screen < globalconf.nscreen; screen++)
500 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
502 wibox_t *s = globalconf.screens[screen].wiboxes.tab[i];
503 if(s->need_update)
504 wibox_draw(s);
507 for(client_t *c = globalconf.clients; c; c = c->next)
508 if(c->titlebar && c->titlebar->need_update)
509 wibox_draw(c->titlebar);
512 /** Set a wibox visible or not.
513 * \param wibox The wibox.
514 * \param v The visible value.
516 static void
517 wibox_setvisible(wibox_t *wibox, bool v)
519 if(v != wibox->isvisible)
521 if((wibox->isvisible = v))
523 xcb_map_window(globalconf.connection, wibox->sw.window);
524 /* stack correctly the wibox */
525 client_stack();
527 else
528 xcb_unmap_window(globalconf.connection, wibox->sw.window);
530 /* kick out systray if needed */
531 wibox_systray_refresh(wibox);
533 /* All the other wibox and ourselves need to be repositioned */
534 wibox_array_t *w = &globalconf.screens[wibox->screen].wiboxes;
535 for(int i = 0; i < w->len; i++)
536 wibox_position_update(w->tab[i]);
540 /** Remove a wibox from a screen.
541 * \param wibox Wibox to detach from screen.
543 void
544 wibox_detach(wibox_t *wibox)
546 if(wibox->screen != SCREEN_UNDEF)
548 bool v;
550 /* save visible state */
551 v = wibox->isvisible;
552 wibox->isvisible = false;
553 wibox_position_update(wibox);
554 /* restore position */
555 wibox->isvisible = v;
557 simplewindow_wipe(&wibox->sw);
559 for(int i = 0; i < globalconf.screens[wibox->screen].wiboxes.len; i++)
560 if(globalconf.screens[wibox->screen].wiboxes.tab[i] == wibox)
562 wibox_array_take(&globalconf.screens[wibox->screen].wiboxes, i);
563 break;
565 globalconf.screens[wibox->screen].need_arrange = true;
566 wibox->screen = SCREEN_UNDEF;
567 wibox_unref(&wibox);
571 /** Attach a wibox.
572 * \param wibox The wibox to attach.
573 * \param s The screen to attach the wibox to.
575 void
576 wibox_attach(wibox_t *wibox, screen_t *s)
578 int phys_screen = screen_virttophys(s->index);
580 wibox_detach(wibox);
582 wibox->screen = s->index;
584 wibox_array_append(&s->wiboxes, wibox_ref(&wibox));
586 /* compute x/y/width/height if not set */
587 wibox_position_update(wibox);
589 simplewindow_init(&wibox->sw, phys_screen,
590 wibox->sw.geometry,
591 wibox->sw.border.width,
592 wibox->sw.orientation,
593 &wibox->sw.ctx.fg, &wibox->sw.ctx.bg);
595 simplewindow_border_color_set(&wibox->sw, &wibox->sw.border.color);
597 /* All the other wibox and ourselves need to be repositioned */
598 for(int i = 0; i < s->wiboxes.len; i++)
599 wibox_position_update(s->wiboxes.tab[i]);
601 ewmh_update_workarea(screen_virttophys(s->index));
603 if(wibox->isvisible)
605 /* draw it right now once to avoid garbage shown */
606 wibox_draw(wibox);
607 xcb_map_window(globalconf.connection, wibox->sw.window);
608 /* stack correctly the wibox */
609 client_stack();
611 else
612 wibox->need_update = true;
615 /** Create a new wibox.
616 * \param L The Lua VM state.
618 * \luastack
619 * \lparam A table with optionaly defined values:
620 * position, align, fg, bg, border_width, border_color, width and height.
621 * \lreturn A brand new wibox.
624 luaA_wibox_new(lua_State *L)
626 wibox_t *w;
627 const char *buf;
628 size_t len;
629 xcolor_init_request_t reqs[3];
630 int8_t i, reqs_nbr = -1;
632 luaA_checktable(L, 2);
634 w = p_new(wibox_t, 1);
635 w->widgets_table = LUA_REFNIL;
637 w->sw.ctx.fg = globalconf.colors.fg;
638 if((buf = luaA_getopt_lstring(L, 2, "fg", NULL, &len)))
639 reqs[++reqs_nbr] = xcolor_init_unchecked(&w->sw.ctx.fg, buf, len);
641 w->sw.ctx.bg = globalconf.colors.bg;
642 if((buf = luaA_getopt_lstring(L, 2, "bg", NULL, &len)))
643 reqs[++reqs_nbr] = xcolor_init_unchecked(&w->sw.ctx.bg, buf, len);
645 w->sw.border.color = globalconf.colors.bg;
646 if((buf = luaA_getopt_lstring(L, 2, "border_color", NULL, &len)))
647 reqs[++reqs_nbr] = xcolor_init_unchecked(&w->sw.border.color, buf, len);
649 buf = luaA_getopt_lstring(L, 2, "align", "left", &len);
650 w->align = draw_align_fromstr(buf, len);
652 w->sw.border.width = luaA_getopt_number(L, 2, "border_width", 0);
654 buf = luaA_getopt_lstring(L, 2, "position", "top", &len);
656 switch((w->position = position_fromstr(buf, len)))
658 case Bottom:
659 case Top:
660 case Floating:
661 simplewindow_orientation_set(&w->sw, East);
662 break;
663 case Left:
664 simplewindow_orientation_set(&w->sw, North);
665 break;
666 case Right:
667 simplewindow_orientation_set(&w->sw, South);
668 break;
671 /* recompute position */
672 wibox_position_update(w);
674 w->sw.geometry.width = luaA_getopt_number(L, 2, "width", 0);
675 w->sw.geometry.height = luaA_getopt_number(L, 2, "height", 0);
677 w->screen = SCREEN_UNDEF;
678 w->isvisible = true;
680 for(i = 0; i <= reqs_nbr; i++)
681 xcolor_init_reply(reqs[i]);
683 return luaA_wibox_userdata_new(L, w);
686 /** Rebuild wibox widgets list.
687 * \param L The Lua VM state.
688 * \param wibox The wibox.
690 static void
691 wibox_widgets_table_build(lua_State *L, wibox_t *wibox)
693 widget_node_array_wipe(&wibox->widgets);
694 widget_node_array_init(&wibox->widgets);
695 luaA_table2widgets(L, &wibox->widgets);
696 wibox->mouse_over = NULL;
697 wibox->need_update = true;
700 /** Check if a wibox widget table has an item.
701 * \param L The Lua VM state.
702 * \param wibox The wibox.
703 * \param item The item to look for.
705 static bool
706 luaA_wibox_hasitem(lua_State *L, wibox_t *wibox, const void *item)
708 bool ret = false;
709 if(wibox->widgets_table != LUA_REFNIL)
711 lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, wibox->widgets_table);
712 if(lua_topointer(L, -1) == item || luaA_hasitem(L, item))
713 ret = true;
715 return ret;
718 /** Invalidate a wibox by a Lua object (table, etc).
719 * \param L The Lua VM state.
720 * \param item The object identifier.
722 void
723 luaA_wibox_invalidate_byitem(lua_State *L, const void *item)
725 for(int screen = 0; screen < globalconf.nscreen; screen++)
726 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
728 wibox_t *wibox = globalconf.screens[screen].wiboxes.tab[i];
729 if(luaA_wibox_hasitem(L, wibox, item))
731 /* recompute widget node list */
732 wibox_widgets_table_build(L, wibox);
733 lua_pop(L, 1); /* remove widgets table */
738 for(client_t *c = globalconf.clients; c; c = c->next)
739 if(c->titlebar && luaA_wibox_hasitem(L, c->titlebar, item))
741 /* recompute widget node list */
742 wibox_widgets_table_build(L, c->titlebar);
743 lua_pop(L, 1); /* remove widgets table */
748 /** Wibox object.
749 * \param L The Lua VM state.
750 * \return The number of elements pushed on stack.
751 * \luastack
752 * \lfield screen Screen number.
753 * \lfield client The client attached to this titlebar.
754 * \lfield border_width Border width.
755 * \lfield border_color Border color.
756 * \lfield align The alignment.
757 * \lfield fg Foreground color.
758 * \lfield bg Background color.
759 * \lfield position The position.
760 * \lfield ontop On top of other windows.
762 static int
763 luaA_wibox_index(lua_State *L)
765 size_t len;
766 wibox_t **wibox = luaA_checkudata(L, 1, "wibox");
767 const char *attr = luaL_checklstring(L, 2, &len);
769 if(luaA_usemetatable(L, 1, 2))
770 return 1;
772 switch(a_tokenize(attr, len))
774 client_t *c;
776 case A_TK_VISIBLE:
777 lua_pushboolean(L, (*wibox)->isvisible);
778 break;
779 case A_TK_CLIENT:
780 if((c = client_getbytitlebar(*wibox)))
781 return luaA_client_userdata_new(L, c);
782 else
783 return 0;
784 case A_TK_SCREEN:
785 if((*wibox)->screen == SCREEN_UNDEF)
786 return 0;
787 lua_pushnumber(L, (*wibox)->screen + 1);
788 break;
789 case A_TK_BORDER_WIDTH:
790 lua_pushnumber(L, (*wibox)->sw.border.width);
791 break;
792 case A_TK_BORDER_COLOR:
793 luaA_pushcolor(L, &(*wibox)->sw.border.color);
794 break;
795 case A_TK_ALIGN:
796 lua_pushstring(L, draw_align_tostr((*wibox)->align));
797 break;
798 case A_TK_FG:
799 luaA_pushcolor(L, &(*wibox)->sw.ctx.fg);
800 break;
801 case A_TK_BG:
802 luaA_pushcolor(L, &(*wibox)->sw.ctx.bg);
803 break;
804 case A_TK_POSITION:
805 lua_pushstring(L, position_tostr((*wibox)->position));
806 break;
807 case A_TK_ONTOP:
808 lua_pushboolean(L, (*wibox)->ontop);
809 break;
810 case A_TK_ORIENTATION:
811 lua_pushstring(L, orientation_tostr((*wibox)->sw.orientation));
812 break;
813 case A_TK_WIDGETS:
814 if((*wibox)->widgets_table != LUA_REFNIL)
815 lua_rawgeti(L, LUA_REGISTRYINDEX, (*wibox)->widgets_table);
816 else
817 lua_pushnil(L);
818 break;
819 default:
820 return 0;
823 return 1;
826 /* Set or get the wibox geometry.
827 * \param L The Lua VM state.
828 * \return The number of elements pushed on stack.
829 * \luastack
830 * \lparam An optional table with wibox geometry.
831 * \lreturn The wibox geometry.
833 static int
834 luaA_wibox_geometry(lua_State *L)
836 wibox_t **wibox = luaA_checkudata(L, 1, "wibox");
838 if(lua_gettop(L) == 2)
840 area_t wingeom;
842 luaA_checktable(L, 2);
843 wingeom.x = luaA_getopt_number(L, 2, "x", (*wibox)->sw.geometry.x);
844 wingeom.y = luaA_getopt_number(L, 2, "y", (*wibox)->sw.geometry.y);
845 wingeom.width = luaA_getopt_number(L, 2, "width", (*wibox)->sw.geometry.width);
846 wingeom.height = luaA_getopt_number(L, 2, "height", (*wibox)->sw.geometry.height);
848 switch((*wibox)->type)
850 case WIBOX_TYPE_TITLEBAR:
851 wibox_resize(*wibox, wingeom.width, wingeom.height);
852 break;
853 case WIBOX_TYPE_NORMAL:
854 if((*wibox)->position == Floating)
855 wibox_moveresize(*wibox, wingeom);
856 else if(wingeom.width != (*wibox)->sw.geometry.width
857 || wingeom.height != (*wibox)->sw.geometry.height)
859 wibox_resize(*wibox, wingeom.width, wingeom.height);
860 globalconf.screens[(*wibox)->screen].need_arrange = true;
862 break;
866 return luaA_pusharea(L, (*wibox)->sw.geometry);
869 /** Wibox newindex.
870 * \param L The Lua VM state.
871 * \return The number of elements pushed on stack.
873 static int
874 luaA_wibox_newindex(lua_State *L)
876 size_t len;
877 wibox_t **wibox = luaA_checkudata(L, 1, "wibox");
878 const char *buf, *attr = luaL_checklstring(L, 2, &len);
879 awesome_token_t tok;
881 switch((tok = a_tokenize(attr, len)))
883 bool b;
885 case A_TK_FG:
886 if((buf = luaL_checklstring(L, 3, &len)))
887 if(xcolor_init_reply(xcolor_init_unchecked(&(*wibox)->sw.ctx.fg, buf, len)))
888 (*wibox)->need_update = true;
889 break;
890 case A_TK_BG:
891 if((buf = luaL_checklstring(L, 3, &len)))
892 if(xcolor_init_reply(xcolor_init_unchecked(&(*wibox)->sw.ctx.bg, buf, len)))
893 (*wibox)->need_update = true;
894 break;
895 case A_TK_ALIGN:
896 buf = luaL_checklstring(L, 3, &len);
897 (*wibox)->align = draw_align_fromstr(buf, len);
898 switch((*wibox)->type)
900 case WIBOX_TYPE_NORMAL:
901 wibox_position_update(*wibox);
902 break;
903 case WIBOX_TYPE_TITLEBAR:
904 titlebar_update_geometry_floating(client_getbytitlebar(*wibox));
905 break;
907 break;
908 case A_TK_POSITION:
909 switch((*wibox)->type)
911 case WIBOX_TYPE_TITLEBAR:
912 return luaA_titlebar_newindex(L, *wibox, tok);
913 case WIBOX_TYPE_NORMAL:
914 if((buf = luaL_checklstring(L, 3, &len)))
915 wibox_setposition(*wibox, position_fromstr(buf, len));
916 break;
918 break;
919 case A_TK_CLIENT:
920 /* first detach */
921 if(lua_isnil(L, 3))
922 titlebar_client_detach(client_getbytitlebar(*wibox));
923 else
925 client_t **c = luaA_checkudata(L, 3, "client");
926 titlebar_client_attach(*c, *wibox);
928 break;
929 case A_TK_SCREEN:
930 if(lua_isnil(L, 3))
932 wibox_detach(*wibox);
933 titlebar_client_detach(client_getbytitlebar(*wibox));
935 else
937 int screen = luaL_checknumber(L, 3) - 1;
938 luaA_checkscreen(screen);
939 if(screen != (*wibox)->screen)
941 titlebar_client_detach(client_getbytitlebar(*wibox));
942 wibox_attach(*wibox, &globalconf.screens[screen]);
945 break;
946 case A_TK_ONTOP:
947 b = luaA_checkboolean(L, 3);
948 if(b != (*wibox)->ontop)
950 (*wibox)->ontop = b;
951 client_stack();
953 break;
954 case A_TK_ORIENTATION:
955 if((buf = luaL_checklstring(L, 3, &len)))
957 simplewindow_orientation_set(&(*wibox)->sw, orientation_fromstr(buf, len));
958 (*wibox)->need_update = true;
960 break;
961 case A_TK_BORDER_COLOR:
962 if((buf = luaL_checklstring(L, 3, &len)))
963 if(xcolor_init_reply(xcolor_init_unchecked(&(*wibox)->sw.border.color, buf, len)))
964 if((*wibox)->sw.window)
965 simplewindow_border_color_set(&(*wibox)->sw, &(*wibox)->sw.border.color);
966 break;
967 case A_TK_VISIBLE:
968 b = luaA_checkboolean(L, 3);
969 if(b != (*wibox)->isvisible)
970 switch((*wibox)->type)
972 case WIBOX_TYPE_NORMAL:
973 wibox_setvisible(*wibox, b);
974 break;
975 case WIBOX_TYPE_TITLEBAR:
976 (*wibox)->isvisible = b;
977 globalconf.screens[(*wibox)->screen].need_arrange = true;
978 break;
980 break;
981 case A_TK_WIDGETS:
982 if(luaA_isloop(L, 3))
983 luaL_error(L, "table is looping, cannot use this as widget table");
984 luaA_register(L, 3, &(*wibox)->widgets_table);
985 /* recompute widget node list */
986 wibox_widgets_table_build(L, *wibox);
987 luaA_table2wtable(L);
988 break;
989 default:
990 switch((*wibox)->type)
992 case WIBOX_TYPE_TITLEBAR:
993 return luaA_titlebar_newindex(L, *wibox, tok);
994 case WIBOX_TYPE_NORMAL:
995 break;
999 return 0;
1002 const struct luaL_reg awesome_wibox_methods[] =
1004 { "__call", luaA_wibox_new },
1005 { NULL, NULL }
1007 const struct luaL_reg awesome_wibox_meta[] =
1009 { "geometry", luaA_wibox_geometry },
1010 { "__index", luaA_wibox_index },
1011 { "__newindex", luaA_wibox_newindex },
1012 { "__gc", luaA_wibox_gc },
1013 { "__eq", luaA_wibox_eq },
1014 { "__tostring", luaA_wibox_tostring },
1015 { NULL, NULL },
1018 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80