common: move draw, xscreen and swindow out
[awesome.git] / widget.c
blob83a9d9db80bce927fe69946a5113a9f8c677acfe
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 statusbar_t *statusbar;
214 widget_node_t *widget;
216 for(statusbar = globalconf.screens[screen].statusbar;
217 statusbar;
218 statusbar = statusbar->next)
219 for(widget = statusbar->widgets; widget; widget = widget->next)
220 if(widget->widget->cache_flags & flags)
222 statusbar->need_update = true;
223 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 statusbar_t *statusbar;
236 widget_node_t *witer;
237 client_t *c;
239 for(screen = 0; screen < globalconf.screens_info->nscreen; screen++)
240 for(statusbar = globalconf.screens[screen].statusbar;
241 statusbar;
242 statusbar = statusbar->next)
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;
251 for(c = globalconf.clients; c; c = c->next)
252 if(c->titlebar && !c->titlebar->need_update)
253 for(witer = c->titlebar->widgets; witer; witer = witer->next)
254 if(witer->widget == widget)
255 c->titlebar->need_update = true;
258 /** Create a new widget.
259 * \param L The Lua VM state.
261 * \luastack
262 * \lparam A table with at least a name and a type value. Optional attributes
263 * are: align.
264 * \lreturn A brand new widget.
266 static int
267 luaA_widget_new(lua_State *L)
269 const char *buf, *type;
270 widget_t *w = NULL;
271 widget_constructor_t *wc;
272 alignment_t align;
273 size_t len;
275 luaA_checktable(L, 2);
277 buf = luaA_getopt_lstring(L, 2, "align", "left", &len);
278 align = draw_align_fromstr(buf, len);
280 if(!(buf = luaA_getopt_string(L, 2, "name", NULL)))
281 luaL_error(L, "object widget must have a name");
283 type = luaA_getopt_string(L, 2, "type", NULL);
285 if((wc = name_func_lookup(type, WidgetList)))
286 w = wc(align);
287 else
288 luaL_error(L, "unkown widget type: %s", type);
290 w->type = wc;
292 /* Set visible by default. */
293 w->isvisible = true;
295 w->mouse_enter = w->mouse_leave = LUA_REFNIL;
297 w->name = a_strdup(buf);
299 return luaA_widget_userdata_new(L, w);
302 /** Get or set mouse buttons bindings to a widget.
303 * \param L The Lua VM state.
305 * \luastack
306 * \lvalue A widget.
307 * \lparam An array of mouse button bindings objects, or nothing.
308 * \return The array of mouse button bindings objects of this widget.
310 static int
311 luaA_widget_buttons(lua_State *L)
313 widget_t **widget = luaA_checkudata(L, 1, "widget");
314 button_array_t *buttons = &(*widget)->buttons;
316 if(lua_gettop(L) == 2)
317 luaA_button_array_set(L, 2, buttons);
319 return luaA_button_array_get(L, buttons);
322 /** Convert a widget into a printable string.
323 * \param L The Lua VM state.
325 * \luastack
326 * \lvalue A widget.
328 static int
329 luaA_widget_tostring(lua_State *L)
331 widget_t **p = luaA_checkudata(L, 1, "widget");
332 lua_pushfstring(L, "[widget udata(%p) name(%s)]", *p, (*p)->name);
333 return 1;
336 /** Generic widget.
337 * \param L The Lua VM state.
338 * \return The number of elements pushed on stack.
339 * \luastack
340 * \lfield visible The widget visibility.
341 * \lfield name The widget name.
342 * \lfield mouse_enter A function to execute when the mouse enter the widget.
343 * \lfield mouse_leave A function to execute when the mouse leave the widget.
345 static int
346 luaA_widget_index(lua_State *L)
348 size_t len;
349 widget_t **widget = luaA_checkudata(L, 1, "widget");
350 const char *buf = luaL_checklstring(L, 2, &len);
351 awesome_token_t token;
353 if(luaA_usemetatable(L, 1, 2))
354 return 1;
356 switch((token = a_tokenize(buf, len)))
358 case A_TK_VISIBLE:
359 lua_pushboolean(L, (*widget)->isvisible);
360 return 1;
361 case A_TK_NAME:
362 lua_pushstring(L, (*widget)->name);
363 return 1;
364 case A_TK_MOUSE_ENTER:
365 lua_rawgeti(L, LUA_REGISTRYINDEX, (*widget)->mouse_enter);
366 return 1;
367 case A_TK_MOUSE_LEAVE:
368 lua_rawgeti(L, LUA_REGISTRYINDEX, (*widget)->mouse_leave);
369 return 1;
370 default:
371 break;
374 return (*widget)->index ? (*widget)->index(L, token) : 0;
377 /** Generic widget newindex.
378 * \param L The Lua VM state.
379 * \return The number of elements pushed on stack.
381 static int
382 luaA_widget_newindex(lua_State *L)
384 size_t len;
385 widget_t **widget = luaA_checkudata(L, 1, "widget");
386 const char *buf = luaL_checklstring(L, 2, &len);
387 awesome_token_t token;
389 switch((token = a_tokenize(buf, len)))
391 case A_TK_VISIBLE:
392 (*widget)->isvisible = luaA_checkboolean(L, 3);
393 break;
394 case A_TK_MOUSE_ENTER:
395 luaA_registerfct(L, 3, &(*widget)->mouse_enter);
396 break;
397 case A_TK_MOUSE_LEAVE:
398 luaA_registerfct(L, 3, &(*widget)->mouse_leave);
399 break;
400 default:
401 return (*widget)->newindex ? (*widget)->newindex(L, token) : 0;
404 widget_invalidate_bywidget(*widget);
406 return 0;
409 /** Generic widget set.
410 * \param L The Lua VM state.
411 * \param idx The table of widgets index.
412 * \param object The object the widget will be attached to.
413 * \param widgets The widgets to fill.
414 * \return The number of elements pushed on stack.
416 void
417 luaA_widget_set(lua_State *L, int idx, void *object, widget_node_t **widgets)
419 widget_node_t *witer;
421 luaA_checktable(L, idx);
423 /* remove all widgets */
424 for(witer = *widgets; witer; witer = *widgets)
426 if(witer->widget->detach)
427 witer->widget->detach(witer->widget, object);
428 widget_unref(&witer->widget);
429 widget_node_list_detach(widgets, witer);
430 p_delete(&witer);
433 /* now read all widgets and add them */
434 lua_pushnil(L);
435 while(lua_next(L, idx))
437 widget_t **widget = luaA_checkudata(L, -1, "widget");
438 widget_node_t *w = p_new(widget_node_t, 1);
439 w->widget = *widget;
440 widget_node_list_append(widgets, w);
441 widget_ref(widget);
442 lua_pop(L, 1);
446 /** Generic widget get.
447 * \param L The Lua VM state.
448 * \param widget The widget list.
449 * \return The number of elements pushed on stack.
452 luaA_widget_get(lua_State *L, widget_node_t *widgets)
454 widget_node_t *witer;
455 int i = 0;
457 lua_newtable(L);
459 for(witer = widgets; witer; witer = witer->next)
461 luaA_widget_userdata_new(L, witer->widget);
462 lua_rawseti(L, -2, ++i);
465 return 1;
468 const struct luaL_reg awesome_widget_methods[] =
470 { "__call", luaA_widget_new },
471 { NULL, NULL }
473 const struct luaL_reg awesome_widget_meta[] =
475 { "buttons", luaA_widget_buttons },
476 { "__index", luaA_widget_index },
477 { "__newindex", luaA_widget_newindex },
478 { "__gc", luaA_widget_gc },
479 { "__eq", luaA_widget_eq },
480 { "__tostring", luaA_widget_tostring },
481 { NULL, NULL }
484 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80