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.
25 #include "objects/client.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.
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
50 xcb_reparent_window(globalconf
.connection
,
51 globalconf
.systray
.window
,
52 globalconf
.screen
->root
,
55 globalconf
.systray
.parent
= NULL
;
60 drawin_wipe(drawin_t
*w
)
62 /* The drawin must already be unmapped, else it
63 * couldn't be garbage collected -> no unmap needed */
65 cairo_surface_finish(w
->drawable
->surface
);
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
);
74 client_restore_enterleave_events();
79 xcb_free_pixmap(globalconf
.connection
, w
->pixmap
);
82 luaA_object_unref_item(globalconf
.L
, -1, w
->drawable
);
87 drawin_unref_simplified(drawin_t
**item
)
89 luaA_object_unref(globalconf
.L
, *item
);
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) */
99 /* Clean up old stuff */
100 luaA_object_push_item(globalconf
.L
, widx
, w
->drawable
);
101 drawable_set_surface(w
->drawable
, -1, NULL
);
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.
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
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.
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.
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
);
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
);
209 client_ignore_enterleave_events();
212 xcb_configure_window(globalconf
.connection
, w
->window
, mask_vals
, moveresize_win_vals
);
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.
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
)
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
,
250 drawin_map(drawin_t
*drawin
, int widx
)
253 client_ignore_enterleave_events();
255 xcb_map_window(globalconf
.connection
, drawin
->window
);
256 drawin_update_drawing(drawin
, widx
);
258 client_restore_enterleave_events();
259 /* Stack this drawin correctly */
261 /* Add it to the list of visible drawins */
262 drawin_array_append(&globalconf
.drawins
, drawin
);
266 drawin_unmap(drawin_t
*drawin
)
268 xcb_unmap_window(globalconf
.connection
, drawin
->window
);
269 foreach(item
, globalconf
.drawins
)
272 drawin_array_remove(&globalconf
.drawins
, item
);
277 /** Get a drawin by its window.
278 * \param win The window id.
279 * \return A drawin if found, NULL otherwise.
282 drawin_getbywin(xcb_window_t win
)
284 foreach(w
, globalconf
.drawins
)
285 if((*w
)->window
== win
)
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.
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
)
305 drawin_map(drawin
, udx
);
306 /* duplicate drawin */
307 lua_pushvalue(L
, udx
);
309 luaA_object_ref_class(globalconf
.L
, -1, &drawin_class
);
314 client_ignore_enterleave_events();
316 drawin_unmap(drawin
);
318 client_restore_enterleave_events();
320 luaA_object_unref(globalconf
.L
, drawin
);
323 luaA_object_emit_signal(L
, udx
, "property::visible", 0);
324 if(strut_has_value(&drawin
->strut
))
327 screen_getbycoord(drawin
->geometry
.x
, drawin
->geometry
.y
);
328 screen_emit_signal(globalconf
.L
, screen
, "property::workarea", 0);
334 drawin_allocator(lua_State
*L
)
336 drawin_t
*w
= drawin_new(L
);
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);
354 /** Create a new drawin.
355 * \param L The Lua VM state.
356 * \return The number of elements pushed on stack.
359 luaA_drawin_new(lua_State
*L
)
361 luaA_class_new(L
, &drawin_class
);
366 /* Set or get the drawin geometry.
367 * \param L The Lua VM state.
368 * \return The number of elements pushed on stack.
370 * \lparam An optional table with drawin geometry.
371 * \lreturn The drawin geometry.
374 luaA_drawin_geometry(lua_State
*L
)
376 drawin_t
*drawin
= luaA_checkudata(L
, 1, &drawin_class
);
378 if(lua_gettop(L
) == 2)
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
)
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
});
411 luaA_drawin_get_x(lua_State
*L
, drawin_t
*drawin
)
413 lua_pushnumber(L
, drawin
->geometry
.x
);
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
});
428 luaA_drawin_get_y(lua_State
*L
, drawin_t
*drawin
)
430 lua_pushnumber(L
, drawin
->geometry
.y
);
435 luaA_drawin_set_width(lua_State
*L
, drawin_t
*drawin
)
437 int width
= luaL_checknumber(L
, -1);
439 luaL_error(L
, "invalid width");
440 drawin_moveresize(L
, -3, (area_t
) { .x
= drawin
->geometry
.x
,
441 .y
= drawin
->geometry
.y
,
443 .height
= drawin
->geometry
.height
});
448 luaA_drawin_get_width(lua_State
*L
, drawin_t
*drawin
)
450 lua_pushnumber(L
, drawin
->geometry
.width
);
455 luaA_drawin_set_height(lua_State
*L
, drawin_t
*drawin
)
457 int height
= luaL_checknumber(L
, -1);
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
,
468 luaA_drawin_get_height(lua_State
*L
, drawin_t
*drawin
)
470 lua_pushnumber(L
, drawin
->geometry
.height
);
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.
480 luaA_drawin_set_ontop(lua_State
*L
, drawin_t
*drawin
)
482 bool b
= luaA_checkboolean(L
, -1);
483 if(b
!= drawin
->ontop
)
487 luaA_object_emit_signal(L
, -3, "property::ontop", 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.
498 luaA_drawin_set_cursor(lua_State
*L
, drawin_t
*drawin
)
500 const char *buf
= luaL_checkstring(L
, -1);
503 uint16_t cursor_font
= xcursor_font_fromstr(buf
);
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);
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.
522 luaA_drawin_set_visible(lua_State
*L
, drawin_t
*drawin
)
524 drawin_set_visible(L
, -3, luaA_checkboolean(L
, -1));
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.
534 luaA_drawin_get_drawable(lua_State
*L
, drawin_t
*drawin
)
536 luaA_object_push_item(L
, -2, drawin
->drawable
);
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.
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
);
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.
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);
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
},
582 static const struct luaL_Reg drawin_meta
[] =
584 LUA_OBJECT_META(drawin
)
586 { "geometry", luaA_drawin_geometry
},
590 luaA_class_setup(L
, &drawin_class
, "drawin", &window_class
,
591 (lua_class_allocator_t
) drawin_allocator
,
592 (lua_class_collector_t
) drawin_wipe
,
594 luaA_class_index_miss_property
, luaA_class_newindex_miss_property
,
595 drawin_methods
, drawin_meta
);
596 luaA_class_add_property(&drawin_class
, "drawable",
598 (lua_class_propfunc_t
) luaA_drawin_get_drawable
,
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
,
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
,
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