drawin: Correctly set window shape
[awesome.git] / objects / drawin.c
blobf290c372f56d198a99eb809816ababdb2c641f69
1 /*
2 * drawin.c - drawin functions
4 * Copyright © 2008-2009 Julien Danjou <julien@danjou.info>
5 * Copyright © 2010 Uli Schlachter <psychon@znc.in>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "screen.h"
24 #include "drawin.h"
25 #include "objects/client.h"
26 #include "screen.h"
27 #include "xwindow.h"
28 #include "luaa.h"
29 #include "ewmh.h"
30 #include "systray.h"
31 #include "common/xcursor.h"
32 #include "common/xutil.h"
34 #include <cairo-xcb.h>
35 #include <xcb/shape.h>
37 LUA_OBJECT_FUNCS(drawin_class, drawin_t, drawin)
39 /** Kick out systray windows.
41 static void
42 drawin_systray_kickout(drawin_t *w)
44 if(globalconf.systray.parent == w)
46 /* Who! Check that we're not deleting a drawin with a systray, because it
47 * may be its parent. If so, we reparent to root before, otherwise it will
48 * hurt very much. */
49 systray_cleanup();
50 xcb_reparent_window(globalconf.connection,
51 globalconf.systray.window,
52 globalconf.screen->root,
53 -512, -512);
55 globalconf.systray.parent = NULL;
59 static void
60 drawin_wipe(drawin_t *w)
62 /* The drawin must already be unmapped, else it
63 * couldn't be garbage collected -> no unmap needed */
64 p_delete(&w->cursor);
65 cairo_surface_finish(w->drawable->surface);
66 if(w->window)
68 /* Activate BMA */
69 client_ignore_enterleave_events();
70 /* Make sure we don't accidentally kill the systray window */
71 drawin_systray_kickout(w);
72 xcb_destroy_window(globalconf.connection, w->window);
73 /* Deactivate BMA */
74 client_restore_enterleave_events();
75 w->window = XCB_NONE;
77 if(w->pixmap)
79 xcb_free_pixmap(globalconf.connection, w->pixmap);
80 w->pixmap = XCB_NONE;
82 luaA_object_unref_item(globalconf.L, -1, w->drawable);
83 w->drawable = NULL;
86 void
87 drawin_unref_simplified(drawin_t **item)
89 luaA_object_unref(globalconf.L, *item);
92 static void
93 drawin_update_drawing(drawin_t *w, int widx)
95 /* If this drawin isn't visible, we don't need an up-to-date cairo surface
96 * for it. (drawin_map() will later make sure we are called again) */
97 if(!w->visible)
98 return;
99 /* Clean up old stuff */
100 luaA_object_push_item(globalconf.L, widx, w->drawable);
101 drawable_set_surface(w->drawable, -1, NULL);
102 if(w->pixmap)
103 xcb_free_pixmap(globalconf.connection, w->pixmap);
105 /* Create a pixmap */
106 xcb_screen_t *s = globalconf.screen;
107 w->pixmap = xcb_generate_id(globalconf.connection);
108 xcb_create_pixmap(globalconf.connection, globalconf.default_depth, w->pixmap, s->root,
109 w->geometry.width, w->geometry.height);
110 /* and create a surface for that pixmap */
111 cairo_surface_t *surface = cairo_xcb_surface_create(globalconf.connection,
112 w->pixmap, globalconf.visual,
113 w->geometry.width, w->geometry.height);
114 drawable_set_surface(w->drawable, -1, surface);
115 drawable_set_geometry(w->drawable, -1, w->geometry);
116 lua_pop(globalconf.L, 1);
119 /** Initialize a drawin.
120 * \param w The drawin to initialize.
122 static void
123 drawin_init(drawin_t *w)
125 xcb_screen_t *s = globalconf.screen;
127 w->window = xcb_generate_id(globalconf.connection);
128 xcb_create_window(globalconf.connection, globalconf.default_depth, w->window, s->root,
129 w->geometry.x, w->geometry.y,
130 w->geometry.width, w->geometry.height,
131 w->border_width, XCB_COPY_FROM_PARENT, globalconf.visual->visual_id,
132 XCB_CW_BORDER_PIXEL | XCB_CW_BIT_GRAVITY
133 | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK | XCB_CW_COLORMAP
134 | XCB_CW_CURSOR,
135 (const uint32_t [])
137 w->border_color.pixel,
138 XCB_GRAVITY_NORTH_WEST,
140 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
141 | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_ENTER_WINDOW
142 | XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_STRUCTURE_NOTIFY
143 | XCB_EVENT_MASK_POINTER_MOTION | XCB_EVENT_MASK_EXPOSURE
144 | XCB_EVENT_MASK_PROPERTY_CHANGE,
145 globalconf.default_cmap,
146 xcursor_new(globalconf.display, xcursor_font_fromstr(w->cursor))
149 /* Set the right properties */
150 ewmh_update_window_type(w->window, window_translate_type(w->type));
151 ewmh_update_strut(w->window, &w->strut);
154 /** Refresh the window content by copying its pixmap data to its window.
155 * \param w The drawin to refresh.
157 static inline void
158 drawin_refresh_pixmap(drawin_t *w)
160 drawin_refresh_pixmap_partial(w, 0, 0, w->geometry.width, w->geometry.height);
163 /** Move and/or resize a drawin
164 * \param L The Lua VM state.
165 * \param udx The index of the drawin.
166 * \param geometry The new geometry.
168 static void
169 drawin_moveresize(lua_State *L, int udx, area_t geometry)
171 drawin_t *w = luaA_checkudata(L, udx, &drawin_class);
172 int number_of_vals = 0;
173 uint32_t moveresize_win_vals[4], mask_vals = 0;
175 if(w->geometry.x != geometry.x)
177 w->geometry.x = moveresize_win_vals[number_of_vals++] = geometry.x;
178 mask_vals |= XCB_CONFIG_WINDOW_X;
181 if(w->geometry.y != geometry.y)
183 w->geometry.y = moveresize_win_vals[number_of_vals++] = geometry.y;
184 mask_vals |= XCB_CONFIG_WINDOW_Y;
187 if(geometry.width > 0 && w->geometry.width != geometry.width)
189 w->geometry.width = moveresize_win_vals[number_of_vals++] = geometry.width;
190 mask_vals |= XCB_CONFIG_WINDOW_WIDTH;
193 if(geometry.height > 0 && w->geometry.height != geometry.height)
195 w->geometry.height = moveresize_win_vals[number_of_vals++] = geometry.height;
196 mask_vals |= XCB_CONFIG_WINDOW_HEIGHT;
199 if(mask_vals & (XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT))
200 drawin_update_drawing(w, udx);
201 else {
202 /* We still have to set x/y */
203 luaA_object_push_item(L, udx, w->drawable);
204 drawable_set_geometry(w->drawable, -1, w->geometry);
205 lua_pop(L, 1);
208 /* Activate BMA */
209 client_ignore_enterleave_events();
211 if(mask_vals)
212 xcb_configure_window(globalconf.connection, w->window, mask_vals, moveresize_win_vals);
214 /* Deactivate BMA */
215 client_restore_enterleave_events();
217 if(mask_vals & XCB_CONFIG_WINDOW_X)
218 luaA_object_emit_signal(L, udx, "property::x", 0);
219 if(mask_vals & XCB_CONFIG_WINDOW_Y)
220 luaA_object_emit_signal(L, udx, "property::y", 0);
221 if(mask_vals & XCB_CONFIG_WINDOW_WIDTH)
222 luaA_object_emit_signal(L, udx, "property::width", 0);
223 if(mask_vals & XCB_CONFIG_WINDOW_HEIGHT)
224 luaA_object_emit_signal(L, udx, "property::height", 0);
227 /** Refresh the window content by copying its pixmap data to its window.
228 * \param drawin The drawin to refresh.
229 * \param x The copy starting point x component.
230 * \param y The copy starting point y component.
231 * \param w The copy width from the x component.
232 * \param h The copy height from the y component.
234 void
235 drawin_refresh_pixmap_partial(drawin_t *drawin,
236 int16_t x, int16_t y,
237 uint16_t w, uint16_t h)
239 if (!drawin->visible)
240 return;
242 /* Make cairo do all pending drawing */
243 cairo_surface_flush(drawin->drawable->surface);
244 xcb_copy_area(globalconf.connection, drawin->pixmap,
245 drawin->window, globalconf.gc, x, y, x, y,
246 w, h);
249 static void
250 drawin_map(drawin_t *drawin, int widx)
252 /* Activate BMA */
253 client_ignore_enterleave_events();
254 /* Map the drawin */
255 xcb_map_window(globalconf.connection, drawin->window);
256 drawin_update_drawing(drawin, widx);
257 /* Deactivate BMA */
258 client_restore_enterleave_events();
259 /* Stack this drawin correctly */
260 stack_windows();
261 /* Add it to the list of visible drawins */
262 drawin_array_append(&globalconf.drawins, drawin);
265 static void
266 drawin_unmap(drawin_t *drawin)
268 xcb_unmap_window(globalconf.connection, drawin->window);
269 foreach(item, globalconf.drawins)
270 if(*item == drawin)
272 drawin_array_remove(&globalconf.drawins, item);
273 break;
277 /** Get a drawin by its window.
278 * \param win The window id.
279 * \return A drawin if found, NULL otherwise.
281 drawin_t *
282 drawin_getbywin(xcb_window_t win)
284 foreach(w, globalconf.drawins)
285 if((*w)->window == win)
286 return *w;
287 return NULL;
290 /** Set a drawin visible or not.
291 * \param L The Lua VM state.
292 * \param udx The drawin.
293 * \param v The visible value.
295 static void
296 drawin_set_visible(lua_State *L, int udx, bool v)
298 drawin_t *drawin = luaA_checkudata(L, udx, &drawin_class);
299 if(v != drawin->visible)
301 drawin->visible = v;
303 if(drawin->visible)
305 drawin_map(drawin, udx);
306 /* duplicate drawin */
307 lua_pushvalue(L, udx);
308 /* ref it */
309 luaA_object_ref_class(globalconf.L, -1, &drawin_class);
311 else
313 /* Active BMA */
314 client_ignore_enterleave_events();
315 /* Unmap window */
316 drawin_unmap(drawin);
317 /* Active BMA */
318 client_restore_enterleave_events();
319 /* unref it */
320 luaA_object_unref(globalconf.L, drawin);
323 luaA_object_emit_signal(L, udx, "property::visible", 0);
324 if(strut_has_value(&drawin->strut))
326 screen_t *screen =
327 screen_getbycoord(drawin->geometry.x, drawin->geometry.y);
328 screen_emit_signal(globalconf.L, screen, "property::workarea", 0);
333 static drawin_t *
334 drawin_allocator(lua_State *L)
336 drawin_t *w = drawin_new(L);
338 w->visible = false;
340 w->opacity = -1;
341 w->cursor = a_strdup("left_ptr");
342 w->geometry.width = 1;
343 w->geometry.height = 1;
344 w->type = _NET_WM_WINDOW_TYPE_NORMAL;
346 drawable_allocator(L, (drawable_refresh_callback *) drawin_refresh_pixmap, w);
347 w->drawable = luaA_object_ref_item(L, -2, -1);
349 drawin_init(w);
351 return w;
354 /** Create a new drawin.
355 * \param L The Lua VM state.
356 * \return The number of elements pushed on stack.
358 static int
359 luaA_drawin_new(lua_State *L)
361 luaA_class_new(L, &drawin_class);
363 return 1;
366 /* Set or get the drawin geometry.
367 * \param L The Lua VM state.
368 * \return The number of elements pushed on stack.
369 * \luastack
370 * \lparam An optional table with drawin geometry.
371 * \lreturn The drawin geometry.
373 static int
374 luaA_drawin_geometry(lua_State *L)
376 drawin_t *drawin = luaA_checkudata(L, 1, &drawin_class);
378 if(lua_gettop(L) == 2)
380 area_t wingeom;
382 luaA_checktable(L, 2);
383 wingeom.x = luaA_getopt_number(L, 2, "x", drawin->geometry.x);
384 wingeom.y = luaA_getopt_number(L, 2, "y", drawin->geometry.y);
385 wingeom.width = luaA_getopt_number(L, 2, "width", drawin->geometry.width);
386 wingeom.height = luaA_getopt_number(L, 2, "height", drawin->geometry.height);
388 if(wingeom.width > 0 && wingeom.height > 0)
389 drawin_moveresize(L, 1, wingeom);
392 return luaA_pusharea(L, drawin->geometry);
396 LUA_OBJECT_EXPORT_PROPERTY(drawin, drawin_t, ontop, lua_pushboolean)
397 LUA_OBJECT_EXPORT_PROPERTY(drawin, drawin_t, cursor, lua_pushstring)
398 LUA_OBJECT_EXPORT_PROPERTY(drawin, drawin_t, visible, lua_pushboolean)
400 static int
401 luaA_drawin_set_x(lua_State *L, drawin_t *drawin)
403 drawin_moveresize(L, -3, (area_t) { .x = luaL_checknumber(L, -1),
404 .y = drawin->geometry.y,
405 .width = drawin->geometry.width,
406 .height = drawin->geometry.height });
407 return 0;
410 static int
411 luaA_drawin_get_x(lua_State *L, drawin_t *drawin)
413 lua_pushnumber(L, drawin->geometry.x);
414 return 1;
417 static int
418 luaA_drawin_set_y(lua_State *L, drawin_t *drawin)
420 drawin_moveresize(L, -3, (area_t) { .x = drawin->geometry.x,
421 .y = luaL_checknumber(L, -1),
422 .width = drawin->geometry.width,
423 .height = drawin->geometry.height });
424 return 0;
427 static int
428 luaA_drawin_get_y(lua_State *L, drawin_t *drawin)
430 lua_pushnumber(L, drawin->geometry.y);
431 return 1;
434 static int
435 luaA_drawin_set_width(lua_State *L, drawin_t *drawin)
437 int width = luaL_checknumber(L, -1);
438 if(width <= 0)
439 luaL_error(L, "invalid width");
440 drawin_moveresize(L, -3, (area_t) { .x = drawin->geometry.x,
441 .y = drawin->geometry.y,
442 .width = width,
443 .height = drawin->geometry.height });
444 return 0;
447 static int
448 luaA_drawin_get_width(lua_State *L, drawin_t *drawin)
450 lua_pushnumber(L, drawin->geometry.width);
451 return 1;
454 static int
455 luaA_drawin_set_height(lua_State *L, drawin_t *drawin)
457 int height = luaL_checknumber(L, -1);
458 if(height <= 0)
459 luaL_error(L, "invalid height");
460 drawin_moveresize(L, -3, (area_t) { .x = drawin->geometry.x,
461 .y = drawin->geometry.y,
462 .width = drawin->geometry.width,
463 .height = height });
464 return 0;
467 static int
468 luaA_drawin_get_height(lua_State *L, drawin_t *drawin)
470 lua_pushnumber(L, drawin->geometry.height);
471 return 1;
474 /** Set the drawin on top status.
475 * \param L The Lua VM state.
476 * \param drawin The drawin object.
477 * \return The number of elements pushed on stack.
479 static int
480 luaA_drawin_set_ontop(lua_State *L, drawin_t *drawin)
482 bool b = luaA_checkboolean(L, -1);
483 if(b != drawin->ontop)
485 drawin->ontop = b;
486 stack_windows();
487 luaA_object_emit_signal(L, -3, "property::ontop", 0);
489 return 0;
492 /** Set the drawin cursor.
493 * \param L The Lua VM state.
494 * \param drawin The drawin object.
495 * \return The number of elements pushed on stack.
497 static int
498 luaA_drawin_set_cursor(lua_State *L, drawin_t *drawin)
500 const char *buf = luaL_checkstring(L, -1);
501 if(buf)
503 uint16_t cursor_font = xcursor_font_fromstr(buf);
504 if(cursor_font)
506 xcb_cursor_t cursor = xcursor_new(globalconf.display, cursor_font);
507 p_delete(&drawin->cursor);
508 drawin->cursor = a_strdup(buf);
509 xwindow_set_cursor(drawin->window, cursor);
510 luaA_object_emit_signal(L, -3, "property::cursor", 0);
513 return 0;
516 /** Set the drawin visibility.
517 * \param L The Lua VM state.
518 * \param drawin The drawin object.
519 * \return The number of elements pushed on stack.
521 static int
522 luaA_drawin_set_visible(lua_State *L, drawin_t *drawin)
524 drawin_set_visible(L, -3, luaA_checkboolean(L, -1));
525 return 0;
528 /** Get a drawin's drawable
529 * \param L The Lua VM state.
530 * \param drawin The drawin object.
531 * \return The number of elements pushed on stack.
533 static int
534 luaA_drawin_get_drawable(lua_State *L, drawin_t *drawin)
536 luaA_object_push_item(L, -2, drawin->drawable);
537 return 1;
540 /** Set the drawin's bounding shape.
541 * \param L The Lua VM state.
542 * \param drawin The drawin object.
543 * \return The number of elements pushed on stack.
545 static int
546 luaA_drawin_set_shape_bounding(lua_State *L, drawin_t *drawin)
548 cairo_surface_t *surf = NULL;
549 if(!lua_isnil(L, -1))
550 surf = (cairo_surface_t *)lua_touserdata(L, -1);
551 xwindow_set_shape(drawin->window, drawin->geometry.width, drawin->geometry.height,
552 XCB_SHAPE_SK_BOUNDING, surf, -drawin->border_width);
553 return 0;
556 /** Set the drawin's clip shape.
557 * \param L The Lua VM state.
558 * \param drawin The drawin object.
559 * \return The number of elements pushed on stack.
561 static int
562 luaA_drawin_set_shape_clip(lua_State *L, drawin_t *drawin)
564 cairo_surface_t *surf = NULL;
565 if(!lua_isnil(L, -1))
566 surf = (cairo_surface_t *)lua_touserdata(L, -1);
567 xwindow_set_shape(drawin->window, drawin->geometry.width, drawin->geometry.height,
568 XCB_SHAPE_SK_CLIP, surf, 0);
569 return 0;
572 void
573 drawin_class_setup(lua_State *L)
575 static const struct luaL_Reg drawin_methods[] =
577 LUA_CLASS_METHODS(drawin)
578 { "__call", luaA_drawin_new },
579 { NULL, NULL }
582 static const struct luaL_Reg drawin_meta[] =
584 LUA_OBJECT_META(drawin)
585 LUA_CLASS_META
586 { "geometry", luaA_drawin_geometry },
587 { NULL, NULL },
590 luaA_class_setup(L, &drawin_class, "drawin", &window_class,
591 (lua_class_allocator_t) drawin_allocator,
592 (lua_class_collector_t) drawin_wipe,
593 NULL,
594 luaA_class_index_miss_property, luaA_class_newindex_miss_property,
595 drawin_methods, drawin_meta);
596 luaA_class_add_property(&drawin_class, "drawable",
597 NULL,
598 (lua_class_propfunc_t) luaA_drawin_get_drawable,
599 NULL);
600 luaA_class_add_property(&drawin_class, "visible",
601 (lua_class_propfunc_t) luaA_drawin_set_visible,
602 (lua_class_propfunc_t) luaA_drawin_get_visible,
603 (lua_class_propfunc_t) luaA_drawin_set_visible);
604 luaA_class_add_property(&drawin_class, "ontop",
605 (lua_class_propfunc_t) luaA_drawin_set_ontop,
606 (lua_class_propfunc_t) luaA_drawin_get_ontop,
607 (lua_class_propfunc_t) luaA_drawin_set_ontop);
608 luaA_class_add_property(&drawin_class, "cursor",
609 (lua_class_propfunc_t) luaA_drawin_set_cursor,
610 (lua_class_propfunc_t) luaA_drawin_get_cursor,
611 (lua_class_propfunc_t) luaA_drawin_set_cursor);
612 luaA_class_add_property(&drawin_class, "x",
613 (lua_class_propfunc_t) luaA_drawin_set_x,
614 (lua_class_propfunc_t) luaA_drawin_get_x,
615 (lua_class_propfunc_t) luaA_drawin_set_x);
616 luaA_class_add_property(&drawin_class, "y",
617 (lua_class_propfunc_t) luaA_drawin_set_y,
618 (lua_class_propfunc_t) luaA_drawin_get_y,
619 (lua_class_propfunc_t) luaA_drawin_set_y);
620 luaA_class_add_property(&drawin_class, "width",
621 (lua_class_propfunc_t) luaA_drawin_set_width,
622 (lua_class_propfunc_t) luaA_drawin_get_width,
623 (lua_class_propfunc_t) luaA_drawin_set_width);
624 luaA_class_add_property(&drawin_class, "height",
625 (lua_class_propfunc_t) luaA_drawin_set_height,
626 (lua_class_propfunc_t) luaA_drawin_get_height,
627 (lua_class_propfunc_t) luaA_drawin_set_height);
628 luaA_class_add_property(&drawin_class, "type",
629 (lua_class_propfunc_t) luaA_window_set_type,
630 (lua_class_propfunc_t) luaA_window_get_type,
631 (lua_class_propfunc_t) luaA_window_set_type);
632 luaA_class_add_property(&drawin_class, "shape_bounding",
633 (lua_class_propfunc_t) luaA_drawin_set_shape_bounding,
634 NULL,
635 (lua_class_propfunc_t) luaA_drawin_set_shape_bounding);
636 luaA_class_add_property(&drawin_class, "shape_clip",
637 (lua_class_propfunc_t) luaA_drawin_set_shape_clip,
638 NULL,
639 (lua_class_propfunc_t) luaA_drawin_set_shape_clip);
641 signal_add(&drawin_class.signals, "property::border_width");
642 signal_add(&drawin_class.signals, "property::cursor");
643 signal_add(&drawin_class.signals, "property::height");
644 signal_add(&drawin_class.signals, "property::ontop");
645 signal_add(&drawin_class.signals, "property::visible");
646 signal_add(&drawin_class.signals, "property::width");
647 signal_add(&drawin_class.signals, "property::x");
648 signal_add(&drawin_class.signals, "property::y");
651 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80