luaa: add support for meta __ipairs
[awesome.git] / widget.c
blobd59caa7686329094e682c63146f7d142ed36b1ed
1 /*
2 * widget.c - widget managing
4 * Copyright © 2007-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 <math.h>
24 #include <xcb/xcb.h>
25 #include <xcb/xcb_atom.h>
27 #include "mouse.h"
28 #include "widget.h"
29 #include "wibox.h"
30 #include "common/atoms.h"
32 extern awesome_t globalconf;
34 DO_LUA_NEW(extern, widget_t, widget, "widget", widget_ref)
35 DO_LUA_GC(widget_t, widget, "widget", widget_unref)
36 DO_LUA_EQ(widget_t, widget, "widget")
38 #include "widgetgen.h"
40 /** Delete a widget structure.
41 * \param widget The widget to destroy.
43 void
44 widget_delete(widget_t **widget)
46 if((*widget)->destructor)
47 (*widget)->destructor(*widget);
48 button_array_wipe(&(*widget)->buttons);
49 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*widget)->mouse_enter);
50 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*widget)->mouse_leave);
51 p_delete(&(*widget)->name);
52 p_delete(widget);
55 /** Compute offset for drawing the first pixel of a widget.
56 * \param barwidth The wibox width.
57 * \param widgetwidth The widget width.
58 * \param alignment The widget alignment on wibox.
59 * \return The x coordinate to draw at.
61 int
62 widget_calculate_offset(int barwidth, int widgetwidth, int offset, int alignment)
64 switch(alignment)
66 case AlignLeft:
67 case AlignFlex:
68 return offset;
70 return barwidth - offset - widgetwidth;
73 /** Common function for button press event on widget.
74 * It will look into configuration to find the callback function to call.
75 * \param w The widget node.
76 * \param ev The button press event the widget received.
77 * \param screen The screen number.
78 * \param p The object where user clicked.
80 static void
81 widget_common_button(widget_node_t *w,
82 xcb_button_press_event_t *ev,
83 int screen __attribute__ ((unused)),
84 wibox_t *p)
86 button_array_t *b = &w->widget->buttons;
88 for(int i = 0; i < b->len; i++)
89 if(ev->detail == b->tab[i]->button
90 && XUTIL_MASK_CLEAN(ev->state) == b->tab[i]->mod)
92 luaA_wibox_userdata_new(globalconf.L, p);
93 luaA_dofunction(globalconf.L,
94 ev->response_type == XCB_BUTTON_PRESS ?
95 b->tab[i]->press : b->tab[i]->release,
96 1, 0);
100 /** Convert a Lua table to a list of widget nodet.
101 * \param L The Lua VM state.
102 * \param widgets The linked list of widget node.
104 void
105 luaA_table2widgets(lua_State *L, widget_node_array_t *widgets)
107 if(lua_istable(L, -1))
109 lua_pushnil(L);
110 while(luaA_next(L, -2))
112 luaA_table2widgets(L, widgets);
113 lua_pop(L, 1); /* remove value */
116 else
118 widget_t **widget = luaA_toudata(L, -1, "widget");
119 if(widget)
121 widget_node_t w;
122 p_clear(&w, 1);
123 w.widget = widget_ref(widget);
124 widget_node_array_append(widgets, w);
129 /** Render a list of widgets.
130 * \param wnode The list of widgets.
131 * \param ctx The draw context where to render.
132 * \param rotate_px The rotate pixmap: where to rotate and render the final
133 * pixmap when the object oritation is not east.
134 * \param screen The logical screen used to render.
135 * \param orientation The object orientation.
136 * \param x The x coordinates of the object.
137 * \param y The y coordinates of the object.
138 * \param wibox The wibox.
139 * \todo Remove GC.
141 void
142 widget_render(widget_node_array_t *widgets, draw_context_t *ctx, xcb_gcontext_t gc, xcb_pixmap_t rotate_px,
143 int screen, orientation_t orientation,
144 int x, int y, wibox_t *wibox)
146 int left = 0, right = 0;
147 area_t rectangle = { 0, 0, 0, 0 };
149 rectangle.width = ctx->width;
150 rectangle.height = ctx->height;
152 if(ctx->bg.alpha != 0xffff)
154 xcb_get_property_reply_t *prop_r;
155 char *data;
156 xcb_pixmap_t rootpix;
157 xcb_get_property_cookie_t prop_c;
158 xcb_screen_t *s = xutil_screen_get(globalconf.connection, ctx->phys_screen);
159 prop_c = xcb_get_property_unchecked(globalconf.connection, false, s->root, _XROOTPMAP_ID,
160 PIXMAP, 0, 1);
161 if((prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL)))
163 if(prop_r->value_len
164 && (data = xcb_get_property_value(prop_r))
165 && (rootpix = *(xcb_pixmap_t *) data))
166 switch(orientation)
168 case North:
169 draw_rotate(ctx,
170 rootpix, ctx->pixmap,
171 s->width_in_pixels, s->height_in_pixels,
172 ctx->width, ctx->height,
173 M_PI_2,
174 y + ctx->width,
175 - x);
176 break;
177 case South:
178 draw_rotate(ctx,
179 rootpix, ctx->pixmap,
180 s->width_in_pixels, s->height_in_pixels,
181 ctx->width, ctx->height,
182 - M_PI_2,
183 - y,
184 x + ctx->height);
185 break;
186 case East:
187 xcb_copy_area(globalconf.connection, rootpix,
188 rotate_px, gc,
189 x, y,
190 0, 0,
191 ctx->width, ctx->height);
192 break;
194 p_delete(&prop_r);
198 /* compute geometry */
199 for(int i = 0; i < widgets->len; i++)
200 if(widgets->tab[i].widget->align == AlignLeft && widgets->tab[i].widget->isvisible)
202 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
203 screen, ctx->height,
204 ctx->width - (left + right));
205 widgets->tab[i].geometry.x = left;
206 left += widgets->tab[i].geometry.width;
209 for(int i = widgets->len - 1; i >= 0; i--)
210 if(widgets->tab[i].widget->align == AlignRight && widgets->tab[i].widget->isvisible)
212 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
213 screen, ctx->height,
214 ctx->width - (left + right));
215 right += widgets->tab[i].geometry.width;
216 widgets->tab[i].geometry.x = ctx->width - right;
219 /* save left value */
220 int fake_left = left;
222 /* compute width of flex aligned widgets that does not support it */
223 int flex = 0;
224 for(int i = 0; i < widgets->len; i++)
225 if(widgets->tab[i].widget->align == AlignFlex
226 && widgets->tab[i].widget->isvisible)
228 if(widgets->tab[i].widget->align_supported & AlignFlex)
229 flex++;
230 else
231 fake_left += widgets->tab[i].widget->geometry(widgets->tab[i].widget,
232 screen, ctx->height,
233 ctx->width - (fake_left + right)).width;
236 /* now compute everybody together! */
237 int flex_rendered = 0;
238 for(int i = 0; i < widgets->len; i++)
239 if(widgets->tab[i].widget->align == AlignFlex
240 && widgets->tab[i].widget->isvisible)
242 if(widgets->tab[i].widget->align_supported & AlignFlex)
244 int width = (ctx->width - (right + fake_left)) / flex;
245 /* give last pixels to last flex to be rendered */
246 if(flex_rendered == flex - 1)
247 width += (ctx->width - (right + fake_left)) % flex;
248 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
249 screen, ctx->height,
250 width);
251 flex_rendered++;
253 else
254 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
255 screen, ctx->height,
256 ctx->width - (left + right));
257 widgets->tab[i].geometry.x = left;
258 left += widgets->tab[i].geometry.width;
261 /* draw everything! */
262 draw_rectangle(ctx, rectangle, 1.0, true, &ctx->bg);
264 for(int i = 0; i < widgets->len; i++)
265 if(widgets->tab[i].widget->isvisible)
267 widgets->tab[i].geometry.y = 0;
268 widgets->tab[i].widget->draw(widgets->tab[i].widget,
269 ctx, widgets->tab[i].geometry,
270 screen, wibox);
273 switch(orientation)
275 case South:
276 draw_rotate(ctx, ctx->pixmap, rotate_px,
277 ctx->width, ctx->height,
278 ctx->height, ctx->width,
279 M_PI_2, ctx->height, 0);
280 break;
281 case North:
282 draw_rotate(ctx, ctx->pixmap, rotate_px,
283 ctx->width, ctx->height,
284 ctx->height, ctx->width,
285 - M_PI_2, 0, ctx->width);
286 break;
287 case East:
288 break;
292 /** Common function for creating a widget.
293 * \param widget The allocated widget.
295 void
296 widget_common_new(widget_t *widget)
298 widget->button = widget_common_button;
299 widget->align_supported = AlignLeft | AlignRight;
302 /** Invalidate widgets which should be refresh upon
303 * external modifications. widget_t who watch flags will
304 * be set to be refreshed.
305 * \param screen Virtual screen number.
306 * \param flags Cache flags to invalidate.
308 void
309 widget_invalidate_cache(int screen, int flags)
311 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
313 wibox_t *wibox = globalconf.screens[screen].wiboxes.tab[i];
315 for(int j = 0; j < wibox->widgets.len; j++)
316 if(wibox->widgets.tab[j].widget->cache_flags & flags)
318 wibox->need_update = true;
319 break;
324 /** Set a wibox needs update because it has widget, or redraw a titlebar.
325 * \todo Probably needs more optimization.
326 * \param widget The widget to look for.
328 void
329 widget_invalidate_bywidget(widget_t *widget)
331 for(int screen = 0; screen < globalconf.nscreen; screen++)
332 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
334 wibox_t *wibox = globalconf.screens[screen].wiboxes.tab[i];
335 if(!wibox->need_update)
336 for(int j = 0; j < wibox->widgets.len; j++)
337 if(wibox->widgets.tab[j].widget == widget)
339 wibox->need_update = true;
340 break;
344 for(client_t *c = globalconf.clients; c; c = c->next)
345 if(c->titlebar && !c->titlebar->need_update)
346 for(int j = 0; j < c->titlebar->widgets.len; j++)
347 if(c->titlebar->widgets.tab[j].widget == widget)
349 c->titlebar->need_update = true;
350 break;
354 /** Deprecated function to add mouse bindings.
355 * \param L The Lua VM state.
356 * \return The number of elements pushed on stack.
358 static int
359 luaA_widget_mouse_add(lua_State *L)
361 deprecate(L);
362 return 0;
365 /** Create a new widget.
366 * \param L The Lua VM state.
368 * \luastack
369 * \lparam A table with at least a name and a type value. Optional attributes
370 * are: align.
371 * \lreturn A brand new widget.
373 static int
374 luaA_widget_new(lua_State *L)
376 const char *buf, *type;
377 widget_t *w = NULL;
378 widget_constructor_t *wc;
379 alignment_t align;
380 size_t len;
382 luaA_checktable(L, 2);
384 buf = luaA_getopt_lstring(L, 2, "align", "left", &len);
385 align = draw_align_fromstr(buf, len);
387 buf = luaA_getopt_string(L, 2, "name", NULL);
389 type = luaA_getopt_string(L, 2, "type", NULL);
391 /* deprecated, compatibility code */
392 if(!a_strcmp(type, "tasklist")
393 || !a_strcmp(type, "taglist"))
395 deprecate(L);
396 luaA_warn(L, "requesting old widgets, return a table to allow smooth execution.\n");
397 lua_newtable(L);
398 lua_pushcfunction(L, luaA_widget_mouse_add);
399 lua_setfield(L, -2, "mouse_add");
400 return 1;
402 else if((wc = name_func_lookup(type, WidgetList)))
403 w = wc(align);
404 else
406 luaA_warn(L, "unkown widget type: %s", type);
407 return 0;
410 w->type = wc;
412 /* Set visible by default. */
413 w->isvisible = true;
415 w->mouse_enter = w->mouse_leave = LUA_REFNIL;
417 w->name = a_strdup(buf);
419 return luaA_widget_userdata_new(L, w);
422 /** Get or set mouse buttons bindings to a widget.
423 * \param L The Lua VM state.
425 * \luastack
426 * \lvalue A widget.
427 * \lparam An array of mouse button bindings objects, or nothing.
428 * \return The array of mouse button bindings objects of this widget.
430 static int
431 luaA_widget_buttons(lua_State *L)
433 widget_t **widget = luaA_checkudata(L, 1, "widget");
434 button_array_t *buttons = &(*widget)->buttons;
436 if(lua_gettop(L) == 2)
438 luaA_button_array_set(L, 2, buttons);
439 return 1;
442 return luaA_button_array_get(L, buttons);
445 /** Generic widget.
446 * \param L The Lua VM state.
447 * \return The number of elements pushed on stack.
448 * \luastack
449 * \lfield visible The widget visibility.
450 * \lfield name The widget name.
451 * \lfield mouse_enter A function to execute when the mouse enter the widget.
452 * \lfield mouse_leave A function to execute when the mouse leave the widget.
454 static int
455 luaA_widget_index(lua_State *L)
457 size_t len;
458 widget_t **widget = luaA_checkudata(L, 1, "widget");
459 const char *buf = luaL_checklstring(L, 2, &len);
460 awesome_token_t token;
462 if(luaA_usemetatable(L, 1, 2))
463 return 1;
465 switch((token = a_tokenize(buf, len)))
467 case A_TK_VISIBLE:
468 lua_pushboolean(L, (*widget)->isvisible);
469 return 1;
470 case A_TK_NAME:
471 if((*widget)->name)
472 lua_pushstring(L, (*widget)->name);
473 else
474 lua_pushnil(L);
475 return 1;
476 case A_TK_MOUSE_ENTER:
477 if((*widget)->mouse_enter != LUA_REFNIL)
478 lua_rawgeti(L, LUA_REGISTRYINDEX, (*widget)->mouse_enter);
479 else
480 return 0;
481 return 1;
482 case A_TK_MOUSE_LEAVE:
483 if((*widget)->mouse_leave != LUA_REFNIL)
484 lua_rawgeti(L, LUA_REGISTRYINDEX, (*widget)->mouse_leave);
485 else
486 return 0;
487 return 1;
488 default:
489 break;
492 return (*widget)->index ? (*widget)->index(L, token) : 0;
495 /** Generic widget newindex.
496 * \param L The Lua VM state.
497 * \return The number of elements pushed on stack.
499 static int
500 luaA_widget_newindex(lua_State *L)
502 size_t len;
503 widget_t **widget = luaA_checkudata(L, 1, "widget");
504 const char *buf = luaL_checklstring(L, 2, &len);
505 awesome_token_t token;
507 switch((token = a_tokenize(buf, len)))
509 case A_TK_VISIBLE:
510 (*widget)->isvisible = luaA_checkboolean(L, 3);
511 break;
512 case A_TK_MOUSE_ENTER:
513 luaA_registerfct(L, 3, &(*widget)->mouse_enter);
514 break;
515 case A_TK_MOUSE_LEAVE:
516 luaA_registerfct(L, 3, &(*widget)->mouse_leave);
517 break;
518 default:
519 return (*widget)->newindex ? (*widget)->newindex(L, token) : 0;
522 widget_invalidate_bywidget(*widget);
524 return 0;
527 const struct luaL_reg awesome_widget_methods[] =
529 { "__call", luaA_widget_new },
530 { NULL, NULL }
532 const struct luaL_reg awesome_widget_meta[] =
534 { "buttons", luaA_widget_buttons },
535 { "mouse_add", luaA_widget_mouse_add },
536 { "__index", luaA_widget_index },
537 { "__newindex", luaA_widget_newindex },
538 { "__gc", luaA_widget_gc },
539 { "__eq", luaA_widget_eq },
540 { "__tostring", luaA_widget_tostring },
541 { NULL, NULL }
544 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80