naughty: notify{run} gets notification object argument (FS#398)
[awesome.git] / widget.c
bloba2d7534a8405c008aa1327e5edd5a93aa04fa7e2
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);
54 /** Compute offset for drawing the first pixel of a widget.
55 * \param barwidth The wibox width.
56 * \param widgetwidth The widget width.
57 * \param alignment The widget alignment on wibox.
58 * \return The x coordinate to draw at.
60 int
61 widget_calculate_offset(int barwidth, int widgetwidth, int offset, int alignment)
63 switch(alignment)
65 case AlignLeft:
66 case AlignFlex:
67 return offset;
69 return barwidth - offset - widgetwidth;
73 /** Get a widget node from a wibox by coords.
74 * \param Container position.
75 * \param widgets The widget list.
76 * \param width The container width.
77 * \param height The container height.
78 * \param x X coordinate of the widget.
79 * \param y Y coordinate of the widget.
80 * \return A widget.
82 widget_t *
83 widget_getbycoords(position_t position, widget_node_array_t *widgets,
84 int width, int height, int16_t *x, int16_t *y)
86 int tmp;
88 /* Need to transform coordinates like it was top/bottom */
89 switch(position)
91 case Right:
92 tmp = *y;
93 *y = width - *x;
94 *x = tmp;
95 break;
96 case Left:
97 tmp = *y;
98 *y = *x;
99 *x = height - tmp;
100 break;
101 default:
102 break;
104 for(int i = 0; i < widgets->len; i++)
106 widget_node_t *w = &widgets->tab[i];
107 if(w->widget->isvisible &&
108 *x >= w->geometry.x && *x < w->geometry.x + w->geometry.width
109 && *y >= w->geometry.y && *y < w->geometry.y + w->geometry.height)
110 return w->widget;
113 return NULL;
116 /** Convert a Lua table to a list of widget nodet.
117 * \param L The Lua VM state.
118 * \param widgets The linked list of widget node.
120 void
121 luaA_table2widgets(lua_State *L, widget_node_array_t *widgets)
123 if(lua_istable(L, -1))
125 lua_pushnil(L);
126 while(luaA_next(L, -2))
128 luaA_table2widgets(L, widgets);
129 lua_pop(L, 1); /* remove value */
132 else
134 widget_t **widget = luaA_toudata(L, -1, "widget");
135 if(widget)
137 widget_node_t w;
138 p_clear(&w, 1);
139 w.widget = widget_ref(widget);
140 widget_node_array_append(widgets, w);
145 /** Render a list of widgets.
146 * \param wnode The list of widgets.
147 * \param ctx The draw context where to render.
148 * \param rotate_px The rotate pixmap: where to rotate and render the final
149 * pixmap when the object oritation is not east.
150 * \param screen The logical screen used to render.
151 * \param orientation The object orientation.
152 * \param x The x coordinates of the object.
153 * \param y The y coordinates of the object.
154 * \param wibox The wibox.
155 * \todo Remove GC.
157 void
158 widget_render(widget_node_array_t *widgets, draw_context_t *ctx, xcb_gcontext_t gc, xcb_pixmap_t rotate_px,
159 int screen, orientation_t orientation,
160 int x, int y, wibox_t *wibox)
162 int left = 0, right = 0;
163 area_t rectangle = { 0, 0, 0, 0 };
165 rectangle.width = ctx->width;
166 rectangle.height = ctx->height;
168 if(ctx->bg.alpha != 0xffff)
170 xcb_get_property_reply_t *prop_r;
171 char *data;
172 xcb_pixmap_t rootpix;
173 xcb_get_property_cookie_t prop_c;
174 xcb_screen_t *s = xutil_screen_get(globalconf.connection, ctx->phys_screen);
175 prop_c = xcb_get_property_unchecked(globalconf.connection, false, s->root, _XROOTPMAP_ID,
176 PIXMAP, 0, 1);
177 if((prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL)))
179 if(prop_r->value_len
180 && (data = xcb_get_property_value(prop_r))
181 && (rootpix = *(xcb_pixmap_t *) data))
182 switch(orientation)
184 case North:
185 draw_rotate(ctx,
186 rootpix, ctx->pixmap,
187 s->width_in_pixels, s->height_in_pixels,
188 ctx->width, ctx->height,
189 M_PI_2,
190 y + ctx->width,
191 - x);
192 break;
193 case South:
194 draw_rotate(ctx,
195 rootpix, ctx->pixmap,
196 s->width_in_pixels, s->height_in_pixels,
197 ctx->width, ctx->height,
198 - M_PI_2,
199 - y,
200 x + ctx->height);
201 break;
202 case East:
203 xcb_copy_area(globalconf.connection, rootpix,
204 rotate_px, gc,
205 x, y,
206 0, 0,
207 ctx->width, ctx->height);
208 break;
210 p_delete(&prop_r);
214 /* compute geometry */
215 for(int i = 0; i < widgets->len; i++)
216 if(widgets->tab[i].widget->align == AlignLeft && widgets->tab[i].widget->isvisible)
218 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
219 screen, ctx->height,
220 ctx->width - (left + right));
221 widgets->tab[i].geometry.x = left;
222 left += widgets->tab[i].geometry.width;
225 for(int i = widgets->len - 1; i >= 0; i--)
226 if(widgets->tab[i].widget->align == AlignRight && widgets->tab[i].widget->isvisible)
228 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
229 screen, ctx->height,
230 ctx->width - (left + right));
231 right += widgets->tab[i].geometry.width;
232 widgets->tab[i].geometry.x = ctx->width - right;
235 /* save left value */
236 int fake_left = left;
238 /* compute width of flex aligned widgets that does not support it */
239 int flex = 0;
240 for(int i = 0; i < widgets->len; i++)
241 if(widgets->tab[i].widget->align == AlignFlex
242 && widgets->tab[i].widget->isvisible)
244 if(widgets->tab[i].widget->align_supported & AlignFlex)
245 flex++;
246 else
247 fake_left += widgets->tab[i].widget->geometry(widgets->tab[i].widget,
248 screen, ctx->height,
249 ctx->width - (fake_left + right)).width;
252 /* now compute everybody together! */
253 int flex_rendered = 0;
254 for(int i = 0; i < widgets->len; i++)
255 if(widgets->tab[i].widget->align == AlignFlex
256 && widgets->tab[i].widget->isvisible)
258 if(widgets->tab[i].widget->align_supported & AlignFlex)
260 int width = (ctx->width - (right + fake_left)) / flex;
261 /* give last pixels to last flex to be rendered */
262 if(flex_rendered == flex - 1)
263 width += (ctx->width - (right + fake_left)) % flex;
264 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
265 screen, ctx->height,
266 width);
267 flex_rendered++;
269 else
270 widgets->tab[i].geometry = widgets->tab[i].widget->geometry(widgets->tab[i].widget,
271 screen, ctx->height,
272 ctx->width - (left + right));
273 widgets->tab[i].geometry.x = left;
274 left += widgets->tab[i].geometry.width;
277 /* draw everything! */
278 draw_rectangle(ctx, rectangle, 1.0, true, &ctx->bg);
280 for(int i = 0; i < widgets->len; i++)
281 if(widgets->tab[i].widget->isvisible)
283 widgets->tab[i].geometry.y = 0;
284 widgets->tab[i].widget->draw(widgets->tab[i].widget,
285 ctx, widgets->tab[i].geometry,
286 screen, wibox);
289 switch(orientation)
291 case South:
292 draw_rotate(ctx, ctx->pixmap, rotate_px,
293 ctx->width, ctx->height,
294 ctx->height, ctx->width,
295 M_PI_2, ctx->height, 0);
296 break;
297 case North:
298 draw_rotate(ctx, ctx->pixmap, rotate_px,
299 ctx->width, ctx->height,
300 ctx->height, ctx->width,
301 - M_PI_2, 0, ctx->width);
302 break;
303 case East:
304 break;
308 /** Invalidate widgets which should be refresh depending on their types.
309 * \param screen Virtual screen number.
310 * \param type Widget type to invalidate.
312 void
313 widget_invalidate_bytype(int screen, widget_constructor_t *type)
315 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
317 wibox_t *wibox = globalconf.screens[screen].wiboxes.tab[i];
319 for(int j = 0; j < wibox->widgets.len; j++)
320 if(wibox->widgets.tab[j].widget->type == type)
322 wibox->need_update = true;
323 break;
328 /** Set a wibox needs update because it has widget, or redraw a titlebar.
329 * \todo Probably needs more optimization.
330 * \param widget The widget to look for.
332 void
333 widget_invalidate_bywidget(widget_t *widget)
335 for(int screen = 0; screen < globalconf.nscreen; screen++)
336 for(int i = 0; i < globalconf.screens[screen].wiboxes.len; i++)
338 wibox_t *wibox = globalconf.screens[screen].wiboxes.tab[i];
339 if(!wibox->need_update)
340 for(int j = 0; j < wibox->widgets.len; j++)
341 if(wibox->widgets.tab[j].widget == widget)
343 wibox->need_update = true;
344 break;
348 for(client_t *c = globalconf.clients; c; c = c->next)
349 if(c->titlebar && !c->titlebar->need_update)
350 for(int j = 0; j < c->titlebar->widgets.len; j++)
351 if(c->titlebar->widgets.tab[j].widget == widget)
353 c->titlebar->need_update = true;
354 break;
358 /** Create a new widget.
359 * \param L The Lua VM state.
361 * \luastack
362 * \lparam A table with at least a type value. Optional attributes
363 * are: align.
364 * \lreturn A brand new widget.
366 static int
367 luaA_widget_new(lua_State *L)
369 const char *align, *type;
370 widget_t *w;
371 widget_constructor_t *wc;
372 size_t len;
374 luaA_checktable(L, 2);
376 type = luaA_getopt_lstring(L, 2, "type", NULL, &len);
378 if((wc = name_func_lookup(type, len, WidgetList)))
380 w = p_new(widget_t, 1);
381 wc(w);
383 else
385 luaA_warn(L, "unkown widget type: %s", type);
386 return 0;
389 w->type = wc;
391 align = luaA_getopt_lstring(L, 2, "align", "left", &len);
392 w->align_supported |= AlignLeft | AlignRight;
393 w->align = draw_align_fromstr(align, len);
395 /* Set visible by default. */
396 w->isvisible = true;
398 w->mouse_enter = w->mouse_leave = LUA_REFNIL;
400 return luaA_widget_userdata_new(L, w);
403 /** Get or set mouse buttons bindings to a widget.
404 * \param L The Lua VM state.
406 * \luastack
407 * \lvalue A widget.
408 * \lparam An array of mouse button bindings objects, or nothing.
409 * \return The array of mouse button bindings objects of this widget.
411 static int
412 luaA_widget_buttons(lua_State *L)
414 widget_t **widget = luaA_checkudata(L, 1, "widget");
415 button_array_t *buttons = &(*widget)->buttons;
417 if(lua_gettop(L) == 2)
419 luaA_button_array_set(L, 2, buttons);
420 return 1;
423 return luaA_button_array_get(L, buttons);
426 /** Generic widget.
427 * \param L The Lua VM state.
428 * \return The number of elements pushed on stack.
429 * \luastack
430 * \lfield visible The widget visibility.
431 * \lfield mouse_enter A function to execute when the mouse enter the widget.
432 * \lfield mouse_leave A function to execute when the mouse leave the widget.
434 static int
435 luaA_widget_index(lua_State *L)
437 size_t len;
438 widget_t **widget = luaA_checkudata(L, 1, "widget");
439 const char *buf = luaL_checklstring(L, 2, &len);
440 awesome_token_t token;
442 if(luaA_usemetatable(L, 1, 2))
443 return 1;
445 switch((token = a_tokenize(buf, len)))
447 case A_TK_VISIBLE:
448 lua_pushboolean(L, (*widget)->isvisible);
449 return 1;
450 case A_TK_MOUSE_ENTER:
451 if((*widget)->mouse_enter != LUA_REFNIL)
452 lua_rawgeti(L, LUA_REGISTRYINDEX, (*widget)->mouse_enter);
453 else
454 return 0;
455 return 1;
456 case A_TK_MOUSE_LEAVE:
457 if((*widget)->mouse_leave != LUA_REFNIL)
458 lua_rawgeti(L, LUA_REGISTRYINDEX, (*widget)->mouse_leave);
459 else
460 return 0;
461 return 1;
462 default:
463 break;
466 return (*widget)->index ? (*widget)->index(L, token) : 0;
469 /** Generic widget newindex.
470 * \param L The Lua VM state.
471 * \return The number of elements pushed on stack.
473 static int
474 luaA_widget_newindex(lua_State *L)
476 size_t len;
477 widget_t **widget = luaA_checkudata(L, 1, "widget");
478 const char *buf = luaL_checklstring(L, 2, &len);
479 awesome_token_t token;
481 switch((token = a_tokenize(buf, len)))
483 case A_TK_VISIBLE:
484 (*widget)->isvisible = luaA_checkboolean(L, 3);
485 break;
486 case A_TK_MOUSE_ENTER:
487 luaA_registerfct(L, 3, &(*widget)->mouse_enter);
488 break;
489 case A_TK_MOUSE_LEAVE:
490 luaA_registerfct(L, 3, &(*widget)->mouse_leave);
491 break;
492 default:
493 return (*widget)->newindex ? (*widget)->newindex(L, token) : 0;
496 widget_invalidate_bywidget(*widget);
498 return 0;
501 const struct luaL_reg awesome_widget_methods[] =
503 { "__call", luaA_widget_new },
504 { NULL, NULL }
506 const struct luaL_reg awesome_widget_meta[] =
508 { "buttons", luaA_widget_buttons },
509 { "__index", luaA_widget_index },
510 { "__newindex", luaA_widget_newindex },
511 { "__gc", luaA_widget_gc },
512 { "__eq", luaA_widget_eq },
513 { "__tostring", luaA_widget_tostring },
514 { NULL, NULL }
517 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80