window: remove spurious printf
[awesome.git] / widget.c
blob31c981b3a67c9a87b9a3c61101cea1b927254b84
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 "titlebar.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 /** Compute offset for drawing the first pixel of a widget.
41 * \param barwidth The statusbar width.
42 * \param widgetwidth The widget width.
43 * \param alignment The widget alignment on statusbar.
44 * \return The x coordinate to draw at.
46 int
47 widget_calculate_offset(int barwidth, int widgetwidth, int offset, int alignment)
49 switch(alignment)
51 case AlignLeft:
52 case AlignFlex:
53 return offset;
55 return barwidth - offset - widgetwidth;
58 /** Common function for button press event on widget.
59 * It will look into configuration to find the callback function to call.
60 * \param w The widget node.
61 * \param ev The button press event the widget received.
62 * \param screen The screen number.
63 * \param p The object where user clicked.
64 * \param type The object type.
66 static void
67 widget_common_button(widget_node_t *w,
68 xcb_button_press_event_t *ev,
69 int screen __attribute__ ((unused)),
70 void *p,
71 awesome_type_t type)
73 button_array_t *b = &w->widget->buttons;
75 for(int i = 0; i < b->len; i++)
76 if(ev->detail == b->tab[i]->button
77 && XUTIL_MASK_CLEAN(ev->state) == b->tab[i]->mod)
79 luaA_pushpointer(globalconf.L, p, type);
80 luaA_dofunction(globalconf.L,
81 ev->response_type == XCB_BUTTON_PRESS ?
82 b->tab[i]->press : b->tab[i]->release,
83 1, 0);
87 /** Render a list of widgets.
88 * \param wnode The list of widgets.
89 * \param ctx The draw context where to render.
90 * \param rotate_px The rotate pixmap: where to rotate and render the final
91 * \param screen The logical screen used to render.
92 * \param position The object position.
93 * \param x The x coordinates of the object.
94 * \param y The y coordinates of the object.
95 * pixmap when the object position is right or left.
96 * \param object The object pointer.
97 * \param type The object type.
98 * \todo Remove GC.
100 void
101 widget_render(widget_node_t *wnode, draw_context_t *ctx, xcb_gcontext_t gc, xcb_pixmap_t rotate_px,
102 int screen, position_t position,
103 int x, int y, void *object, awesome_type_t type)
105 xcb_pixmap_t rootpix;
106 xcb_screen_t *s;
107 widget_node_t *w;
108 int left = 0, right = 0;
109 char *data;
110 xcb_get_property_reply_t *prop_r;
111 xcb_get_property_cookie_t prop_c;
112 area_t rectangle = { 0, 0, 0, 0 };
114 rectangle.width = ctx->width;
115 rectangle.height = ctx->height;
117 if(ctx->bg.alpha != 0xffff)
119 s = xutil_screen_get(globalconf.connection, ctx->phys_screen);
120 prop_c = xcb_get_property_unchecked(globalconf.connection, false, s->root, _XROOTPMAP_ID,
121 PIXMAP, 0, 1);
122 if((prop_r = xcb_get_property_reply(globalconf.connection, prop_c, NULL)))
124 if(prop_r->value_len
125 && (data = xcb_get_property_value(prop_r))
126 && (rootpix = *(xcb_pixmap_t *) data))
127 switch(position)
129 case Left:
130 draw_rotate(ctx,
131 rootpix, ctx->pixmap,
132 s->width_in_pixels, s->height_in_pixels,
133 ctx->width, ctx->height,
134 M_PI_2,
135 y + ctx->width,
136 - x);
137 break;
138 case Right:
139 draw_rotate(ctx,
140 rootpix, ctx->pixmap,
141 s->width_in_pixels, s->height_in_pixels,
142 ctx->width, ctx->height,
143 - M_PI_2,
144 - y,
145 x + ctx->height);
146 break;
147 default:
148 xcb_copy_area(globalconf.connection, rootpix,
149 rotate_px, gc,
150 x, y,
151 0, 0,
152 ctx->width, ctx->height);
153 break;
155 p_delete(&prop_r);
159 draw_rectangle(ctx, rectangle, 1.0, true, &ctx->bg);
161 for(w = wnode; w; w = w->next)
162 if(w->widget->align == AlignLeft && w->widget->isvisible)
163 left += w->widget->draw(ctx, screen, w, left, (left + right), object, type);
165 /* renders right widget from last to first */
166 for(w = *widget_node_list_last(&wnode); w; w = w->prev)
167 if(w->widget->align == AlignRight && w->widget->isvisible)
168 right += w->widget->draw(ctx, screen, w, right, (left + right), object, type);
170 for(w = wnode; w; w = w->next)
171 if(w->widget->align == AlignFlex && w->widget->isvisible)
172 left += w->widget->draw(ctx, screen, w, left, (left + right), object, type);
174 switch(position)
176 case Right:
177 draw_rotate(ctx, ctx->pixmap, rotate_px,
178 ctx->width, ctx->height,
179 ctx->height, ctx->width,
180 M_PI_2, ctx->height, 0);
181 break;
182 case Left:
183 draw_rotate(ctx, ctx->pixmap, rotate_px,
184 ctx->width, ctx->height,
185 ctx->height, ctx->width,
186 - M_PI_2, 0, ctx->width);
187 break;
188 default:
189 break;
194 /** Common function for creating a widget.
195 * \param widget The allocated widget.
197 void
198 widget_common_new(widget_t *widget)
200 widget->align = AlignLeft;
201 widget->button = widget_common_button;
204 /** Invalidate widgets which should be refresh upon
205 * external modifications. widget_t who watch flags will
206 * be set to be refreshed.
207 * \param screen Virtual screen number.
208 * \param flags Cache flags to invalidate.
210 void
211 widget_invalidate_cache(int screen, int flags)
213 for(int i = 0; i < globalconf.screens[screen].statusbars.len; i++)
215 statusbar_t *statusbar = globalconf.screens[screen].statusbars.tab[i];
216 widget_node_t *widget;
218 for(widget = statusbar->widgets; widget; widget = widget->next)
219 if(widget->widget->cache_flags & flags)
221 statusbar->need_update = true;
222 break;
227 /** Set a statusbar needs update because it has widget, or redraw a titlebar.
228 * \todo Probably needs more optimization.
229 * \param widget The widget to look for.
231 void
232 widget_invalidate_bywidget(widget_t *widget)
234 int screen;
235 widget_node_t *witer;
236 client_t *c;
238 for(screen = 0; screen < globalconf.nscreen; screen++)
239 for(int i = 0; i < globalconf.screens[screen].statusbars.len; i++)
241 statusbar_t *statusbar = globalconf.screens[screen].statusbars.tab[i];
243 if(!statusbar->need_update)
244 for(witer = statusbar->widgets; witer; witer = witer->next)
245 if(witer->widget == widget)
247 statusbar->need_update = true;
248 break;
252 for(c = globalconf.clients; c; c = c->next)
253 if(c->titlebar && !c->titlebar->need_update)
254 for(witer = c->titlebar->widgets; witer; witer = witer->next)
255 if(witer->widget == widget)
256 c->titlebar->need_update = true;
259 /** Create a new widget.
260 * \param L The Lua VM state.
262 * \luastack
263 * \lparam A table with at least a name and a type value. Optional attributes
264 * are: align.
265 * \lreturn A brand new widget.
267 static int
268 luaA_widget_new(lua_State *L)
270 const char *buf, *type;
271 widget_t *w = NULL;
272 widget_constructor_t *wc;
273 alignment_t align;
274 size_t len;
276 luaA_checktable(L, 2);
278 buf = luaA_getopt_lstring(L, 2, "align", "left", &len);
279 align = draw_align_fromstr(buf, len);
281 if(!(buf = luaA_getopt_string(L, 2, "name", NULL)))
282 luaL_error(L, "object widget must have a name");
284 type = luaA_getopt_string(L, 2, "type", NULL);
286 if((wc = name_func_lookup(type, WidgetList)))
287 w = wc(align);
288 else
289 luaL_error(L, "unkown widget type: %s", type);
291 w->type = wc;
293 /* Set visible by default. */
294 w->isvisible = true;
296 w->mouse_enter = w->mouse_leave = LUA_REFNIL;
298 w->name = a_strdup(buf);
300 return luaA_widget_userdata_new(L, w);
303 /** Get or set mouse buttons bindings to a widget.
304 * \param L The Lua VM state.
306 * \luastack
307 * \lvalue A widget.
308 * \lparam An array of mouse button bindings objects, or nothing.
309 * \return The array of mouse button bindings objects of this widget.
311 static int
312 luaA_widget_buttons(lua_State *L)
314 widget_t **widget = luaA_checkudata(L, 1, "widget");
315 button_array_t *buttons = &(*widget)->buttons;
317 if(lua_gettop(L) == 2)
318 luaA_button_array_set(L, 2, buttons);
320 return luaA_button_array_get(L, buttons);
323 /** Convert a widget into a printable string.
324 * \param L The Lua VM state.
326 * \luastack
327 * \lvalue A widget.
329 static int
330 luaA_widget_tostring(lua_State *L)
332 widget_t **p = luaA_checkudata(L, 1, "widget");
333 lua_pushfstring(L, "[widget udata(%p) name(%s)]", *p, (*p)->name);
334 return 1;
337 /** Generic widget.
338 * \param L The Lua VM state.
339 * \return The number of elements pushed on stack.
340 * \luastack
341 * \lfield visible The widget visibility.
342 * \lfield name The widget name.
343 * \lfield mouse_enter A function to execute when the mouse enter the widget.
344 * \lfield mouse_leave A function to execute when the mouse leave the widget.
346 static int
347 luaA_widget_index(lua_State *L)
349 size_t len;
350 widget_t **widget = luaA_checkudata(L, 1, "widget");
351 const char *buf = luaL_checklstring(L, 2, &len);
352 awesome_token_t token;
354 if(luaA_usemetatable(L, 1, 2))
355 return 1;
357 switch((token = a_tokenize(buf, len)))
359 case A_TK_VISIBLE:
360 lua_pushboolean(L, (*widget)->isvisible);
361 return 1;
362 case A_TK_NAME:
363 lua_pushstring(L, (*widget)->name);
364 return 1;
365 case A_TK_MOUSE_ENTER:
366 lua_rawgeti(L, LUA_REGISTRYINDEX, (*widget)->mouse_enter);
367 return 1;
368 case A_TK_MOUSE_LEAVE:
369 lua_rawgeti(L, LUA_REGISTRYINDEX, (*widget)->mouse_leave);
370 return 1;
371 default:
372 break;
375 return (*widget)->index ? (*widget)->index(L, token) : 0;
378 /** Generic widget newindex.
379 * \param L The Lua VM state.
380 * \return The number of elements pushed on stack.
382 static int
383 luaA_widget_newindex(lua_State *L)
385 size_t len;
386 widget_t **widget = luaA_checkudata(L, 1, "widget");
387 const char *buf = luaL_checklstring(L, 2, &len);
388 awesome_token_t token;
390 switch((token = a_tokenize(buf, len)))
392 case A_TK_VISIBLE:
393 (*widget)->isvisible = luaA_checkboolean(L, 3);
394 break;
395 case A_TK_MOUSE_ENTER:
396 luaA_registerfct(L, 3, &(*widget)->mouse_enter);
397 break;
398 case A_TK_MOUSE_LEAVE:
399 luaA_registerfct(L, 3, &(*widget)->mouse_leave);
400 break;
401 default:
402 return (*widget)->newindex ? (*widget)->newindex(L, token) : 0;
405 widget_invalidate_bywidget(*widget);
407 return 0;
410 /** Generic widget set.
411 * \param L The Lua VM state.
412 * \param idx The table of widgets index.
413 * \param object The object the widget will be attached to.
414 * \param widgets The widgets to fill.
415 * \return The number of elements pushed on stack.
417 void
418 luaA_widget_set(lua_State *L, int idx, void *object, widget_node_t **widgets)
420 widget_node_t *witer;
422 luaA_checktable(L, idx);
424 /* remove all widgets */
425 for(witer = *widgets; witer; witer = *widgets)
427 if(witer->widget->detach)
428 witer->widget->detach(witer->widget, object);
429 widget_unref(&witer->widget);
430 widget_node_list_detach(widgets, witer);
431 p_delete(&witer);
434 /* now read all widgets and add them */
435 lua_pushnil(L);
436 while(lua_next(L, idx))
438 widget_t **widget = luaA_checkudata(L, -1, "widget");
439 widget_node_t *w = p_new(widget_node_t, 1);
440 w->widget = *widget;
441 widget_node_list_append(widgets, w);
442 widget_ref(widget);
443 lua_pop(L, 1);
447 /** Generic widget get.
448 * \param L The Lua VM state.
449 * \param widget The widget list.
450 * \return The number of elements pushed on stack.
453 luaA_widget_get(lua_State *L, widget_node_t *widgets)
455 widget_node_t *witer;
456 int i = 0;
458 lua_newtable(L);
460 for(witer = widgets; witer; witer = witer->next)
462 luaA_widget_userdata_new(L, witer->widget);
463 lua_rawseti(L, -2, ++i);
466 return 1;
469 const struct luaL_reg awesome_widget_methods[] =
471 { "__call", luaA_widget_new },
472 { NULL, NULL }
474 const struct luaL_reg awesome_widget_meta[] =
476 { "buttons", luaA_widget_buttons },
477 { "__index", luaA_widget_index },
478 { "__newindex", luaA_widget_newindex },
479 { "__gc", luaA_widget_gc },
480 { "__eq", luaA_widget_eq },
481 { "__tostring", luaA_widget_tostring },
482 { NULL, NULL }
485 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80