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.
24 #include "common/atoms.h"
25 #include "common/xcursor.h"
27 #include "objects/client.h"
28 #include "objects/screen.h"
32 #include <cairo-xcb.h>
33 #include <xcb/shape.h>
35 LUA_OBJECT_FUNCS(drawin_class
, drawin_t
, drawin
)
37 /** Kick out systray windows.
40 drawin_systray_kickout(drawin_t
*w
)
42 if(globalconf
.systray
.parent
== w
)
44 /* Who! Check that we're not deleting a drawin with a systray, because it
45 * may be its parent. If so, we reparent to root before, otherwise it will
48 xcb_reparent_window(globalconf
.connection
,
49 globalconf
.systray
.window
,
50 globalconf
.screen
->root
,
53 globalconf
.systray
.parent
= NULL
;
58 drawin_wipe(drawin_t
*w
)
60 /* The drawin must already be unmapped, else it
61 * couldn't be garbage collected -> no unmap needed */
65 /* Make sure we don't accidentally kill the systray window */
66 drawin_systray_kickout(w
);
67 xcb_destroy_window(globalconf
.connection
, w
->window
);
70 /* No unref needed because we are being garbage collected */
75 drawin_update_drawing(drawin_t
*w
, int widx
)
77 /* If this drawin isn't visible, we don't need an up-to-date cairo surface
78 * for it. (drawin_map() will later make sure we are called again) */
82 luaA_object_push_item(globalconf
.L
, widx
, w
->drawable
);
83 drawable_set_geometry(w
->drawable
, -1, w
->geometry
);
84 lua_pop(globalconf
.L
, 1);
87 /** Refresh the window content by copying its pixmap data to its window.
88 * \param w The drawin to refresh.
91 drawin_refresh_pixmap(drawin_t
*w
)
93 drawin_refresh_pixmap_partial(w
, 0, 0, w
->geometry
.width
, w
->geometry
.height
);
96 /** Move and/or resize a drawin
97 * \param L The Lua VM state.
98 * \param udx The index of the drawin.
99 * \param geometry The new geometry.
102 drawin_moveresize(lua_State
*L
, int udx
, area_t geometry
)
104 drawin_t
*w
= luaA_checkudata(L
, udx
, &drawin_class
);
105 int number_of_vals
= 0;
106 uint32_t moveresize_win_vals
[4], mask_vals
= 0;
108 if(w
->geometry
.x
!= geometry
.x
)
110 w
->geometry
.x
= moveresize_win_vals
[number_of_vals
++] = geometry
.x
;
111 mask_vals
|= XCB_CONFIG_WINDOW_X
;
114 if(w
->geometry
.y
!= geometry
.y
)
116 w
->geometry
.y
= moveresize_win_vals
[number_of_vals
++] = geometry
.y
;
117 mask_vals
|= XCB_CONFIG_WINDOW_Y
;
120 if(geometry
.width
> 0 && w
->geometry
.width
!= geometry
.width
)
122 w
->geometry
.width
= moveresize_win_vals
[number_of_vals
++] = geometry
.width
;
123 mask_vals
|= XCB_CONFIG_WINDOW_WIDTH
;
126 if(geometry
.height
> 0 && w
->geometry
.height
!= geometry
.height
)
128 w
->geometry
.height
= moveresize_win_vals
[number_of_vals
++] = geometry
.height
;
129 mask_vals
|= XCB_CONFIG_WINDOW_HEIGHT
;
132 if(mask_vals
& (XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
))
133 drawin_update_drawing(w
, udx
);
135 /* We still have to set x/y */
136 luaA_object_push_item(L
, udx
, w
->drawable
);
137 drawable_set_geometry(w
->drawable
, -1, w
->geometry
);
142 client_ignore_enterleave_events();
145 xcb_configure_window(globalconf
.connection
, w
->window
, mask_vals
, moveresize_win_vals
);
148 client_restore_enterleave_events();
150 if(mask_vals
& XCB_CONFIG_WINDOW_X
)
151 luaA_object_emit_signal(L
, udx
, "property::x", 0);
152 if(mask_vals
& XCB_CONFIG_WINDOW_Y
)
153 luaA_object_emit_signal(L
, udx
, "property::y", 0);
154 if(mask_vals
& XCB_CONFIG_WINDOW_WIDTH
)
155 luaA_object_emit_signal(L
, udx
, "property::width", 0);
156 if(mask_vals
& XCB_CONFIG_WINDOW_HEIGHT
)
157 luaA_object_emit_signal(L
, udx
, "property::height", 0);
160 /** Refresh the window content by copying its pixmap data to its window.
161 * \param drawin The drawin to refresh.
162 * \param x The copy starting point x component.
163 * \param y The copy starting point y component.
164 * \param w The copy width from the x component.
165 * \param h The copy height from the y component.
168 drawin_refresh_pixmap_partial(drawin_t
*drawin
,
169 int16_t x
, int16_t y
,
170 uint16_t w
, uint16_t h
)
172 if (!drawin
->drawable
|| !drawin
->drawable
->pixmap
|| !drawin
->drawable
->refreshed
)
175 /* Make cairo do all pending drawing */
176 cairo_surface_flush(drawin
->drawable
->surface
);
177 xcb_copy_area(globalconf
.connection
, drawin
->drawable
->pixmap
,
178 drawin
->window
, globalconf
.gc
, x
, y
, x
, y
,
183 drawin_map(drawin_t
*drawin
, int widx
)
186 client_ignore_enterleave_events();
188 xcb_map_window(globalconf
.connection
, drawin
->window
);
190 client_restore_enterleave_events();
191 /* Stack this drawin correctly */
193 /* Add it to the list of visible drawins */
194 drawin_array_append(&globalconf
.drawins
, drawin
);
195 /* Make sure it has a surface */
196 if(drawin
->drawable
->surface
== NULL
)
197 drawin_update_drawing(drawin
, widx
);
201 drawin_unmap(drawin_t
*drawin
)
203 xcb_unmap_window(globalconf
.connection
, drawin
->window
);
204 foreach(item
, globalconf
.drawins
)
207 drawin_array_remove(&globalconf
.drawins
, item
);
212 /** Get a drawin by its window.
213 * \param win The window id.
214 * \return A drawin if found, NULL otherwise.
217 drawin_getbywin(xcb_window_t win
)
219 foreach(w
, globalconf
.drawins
)
220 if((*w
)->window
== win
)
225 /** Set a drawin visible or not.
226 * \param L The Lua VM state.
227 * \param udx The drawin.
228 * \param v The visible value.
231 drawin_set_visible(lua_State
*L
, int udx
, bool v
)
233 drawin_t
*drawin
= luaA_checkudata(L
, udx
, &drawin_class
);
234 if(v
!= drawin
->visible
)
240 drawin_map(drawin
, udx
);
241 /* duplicate drawin */
242 lua_pushvalue(L
, udx
);
244 luaA_object_ref_class(globalconf
.L
, -1, &drawin_class
);
249 client_ignore_enterleave_events();
251 drawin_unmap(drawin
);
253 client_restore_enterleave_events();
255 luaA_object_unref(globalconf
.L
, drawin
);
258 luaA_object_emit_signal(L
, udx
, "property::visible", 0);
259 if(strut_has_value(&drawin
->strut
))
261 luaA_object_push(L
, screen_getbycoord(drawin
->geometry
.x
, drawin
->geometry
.y
));
262 luaA_object_emit_signal(L
, -1, "property::workarea", 0);
269 drawin_allocator(lua_State
*L
)
271 xcb_screen_t
*s
= globalconf
.screen
;
272 drawin_t
*w
= drawin_new(L
);
277 w
->cursor
= a_strdup("left_ptr");
278 w
->geometry
.width
= 1;
279 w
->geometry
.height
= 1;
280 w
->type
= _NET_WM_WINDOW_TYPE_NORMAL
;
282 drawable_allocator(L
, (drawable_refresh_callback
*) drawin_refresh_pixmap
, w
);
283 w
->drawable
= luaA_object_ref_item(L
, -2, -1);
285 w
->window
= xcb_generate_id(globalconf
.connection
);
286 xcb_create_window(globalconf
.connection
, globalconf
.default_depth
, w
->window
, s
->root
,
287 w
->geometry
.x
, w
->geometry
.y
,
288 w
->geometry
.width
, w
->geometry
.height
,
289 w
->border_width
, XCB_COPY_FROM_PARENT
, globalconf
.visual
->visual_id
,
290 XCB_CW_BORDER_PIXEL
| XCB_CW_BIT_GRAVITY
291 | XCB_CW_OVERRIDE_REDIRECT
| XCB_CW_EVENT_MASK
| XCB_CW_COLORMAP
295 w
->border_color
.pixel
,
296 XCB_GRAVITY_NORTH_WEST
,
298 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
299 | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
| XCB_EVENT_MASK_ENTER_WINDOW
300 | XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_STRUCTURE_NOTIFY
301 | XCB_EVENT_MASK_POINTER_MOTION
| XCB_EVENT_MASK_EXPOSURE
302 | XCB_EVENT_MASK_PROPERTY_CHANGE
,
303 globalconf
.default_cmap
,
304 xcursor_new(globalconf
.cursor_ctx
, xcursor_font_fromstr(w
->cursor
))
307 /* Set the right properties */
308 ewmh_update_window_type(w
->window
, window_translate_type(w
->type
));
309 ewmh_update_strut(w
->window
, &w
->strut
);
314 /** Create a new drawin.
315 * \param L The Lua VM state.
316 * \return The number of elements pushed on stack.
319 luaA_drawin_new(lua_State
*L
)
321 luaA_class_new(L
, &drawin_class
);
326 /* Set or get the drawin geometry.
327 * \param L The Lua VM state.
328 * \return The number of elements pushed on stack.
330 * \lparam An optional table with drawin geometry.
331 * \lreturn The drawin geometry.
334 luaA_drawin_geometry(lua_State
*L
)
336 drawin_t
*drawin
= luaA_checkudata(L
, 1, &drawin_class
);
338 if(lua_gettop(L
) == 2)
342 luaA_checktable(L
, 2);
343 wingeom
.x
= luaA_getopt_number(L
, 2, "x", drawin
->geometry
.x
);
344 wingeom
.y
= luaA_getopt_number(L
, 2, "y", drawin
->geometry
.y
);
345 wingeom
.width
= luaA_getopt_number(L
, 2, "width", drawin
->geometry
.width
);
346 wingeom
.height
= luaA_getopt_number(L
, 2, "height", drawin
->geometry
.height
);
348 if(wingeom
.width
> 0 && wingeom
.height
> 0)
349 drawin_moveresize(L
, 1, wingeom
);
352 return luaA_pusharea(L
, drawin
->geometry
);
356 LUA_OBJECT_EXPORT_PROPERTY(drawin
, drawin_t
, ontop
, lua_pushboolean
)
357 LUA_OBJECT_EXPORT_PROPERTY(drawin
, drawin_t
, cursor
, lua_pushstring
)
358 LUA_OBJECT_EXPORT_PROPERTY(drawin
, drawin_t
, visible
, lua_pushboolean
)
361 luaA_drawin_set_x(lua_State
*L
, drawin_t
*drawin
)
363 drawin_moveresize(L
, -3, (area_t
) { .x
= luaL_checknumber(L
, -1),
364 .y
= drawin
->geometry
.y
,
365 .width
= drawin
->geometry
.width
,
366 .height
= drawin
->geometry
.height
});
371 luaA_drawin_get_x(lua_State
*L
, drawin_t
*drawin
)
373 lua_pushnumber(L
, drawin
->geometry
.x
);
378 luaA_drawin_set_y(lua_State
*L
, drawin_t
*drawin
)
380 drawin_moveresize(L
, -3, (area_t
) { .x
= drawin
->geometry
.x
,
381 .y
= luaL_checknumber(L
, -1),
382 .width
= drawin
->geometry
.width
,
383 .height
= drawin
->geometry
.height
});
388 luaA_drawin_get_y(lua_State
*L
, drawin_t
*drawin
)
390 lua_pushnumber(L
, drawin
->geometry
.y
);
395 luaA_drawin_set_width(lua_State
*L
, drawin_t
*drawin
)
397 int width
= luaL_checknumber(L
, -1);
399 luaL_error(L
, "invalid width");
400 drawin_moveresize(L
, -3, (area_t
) { .x
= drawin
->geometry
.x
,
401 .y
= drawin
->geometry
.y
,
403 .height
= drawin
->geometry
.height
});
408 luaA_drawin_get_width(lua_State
*L
, drawin_t
*drawin
)
410 lua_pushnumber(L
, drawin
->geometry
.width
);
415 luaA_drawin_set_height(lua_State
*L
, drawin_t
*drawin
)
417 int height
= luaL_checknumber(L
, -1);
419 luaL_error(L
, "invalid height");
420 drawin_moveresize(L
, -3, (area_t
) { .x
= drawin
->geometry
.x
,
421 .y
= drawin
->geometry
.y
,
422 .width
= drawin
->geometry
.width
,
428 luaA_drawin_get_height(lua_State
*L
, drawin_t
*drawin
)
430 lua_pushnumber(L
, drawin
->geometry
.height
);
434 /** Set the drawin on top status.
435 * \param L The Lua VM state.
436 * \param drawin The drawin object.
437 * \return The number of elements pushed on stack.
440 luaA_drawin_set_ontop(lua_State
*L
, drawin_t
*drawin
)
442 bool b
= luaA_checkboolean(L
, -1);
443 if(b
!= drawin
->ontop
)
447 luaA_object_emit_signal(L
, -3, "property::ontop", 0);
452 /** Set the drawin cursor.
453 * \param L The Lua VM state.
454 * \param drawin The drawin object.
455 * \return The number of elements pushed on stack.
458 luaA_drawin_set_cursor(lua_State
*L
, drawin_t
*drawin
)
460 const char *buf
= luaL_checkstring(L
, -1);
463 uint16_t cursor_font
= xcursor_font_fromstr(buf
);
466 xcb_cursor_t cursor
= xcursor_new(globalconf
.cursor_ctx
, cursor_font
);
467 p_delete(&drawin
->cursor
);
468 drawin
->cursor
= a_strdup(buf
);
469 xwindow_set_cursor(drawin
->window
, cursor
);
470 luaA_object_emit_signal(L
, -3, "property::cursor", 0);
476 /** Set the drawin visibility.
477 * \param L The Lua VM state.
478 * \param drawin The drawin object.
479 * \return The number of elements pushed on stack.
482 luaA_drawin_set_visible(lua_State
*L
, drawin_t
*drawin
)
484 drawin_set_visible(L
, -3, luaA_checkboolean(L
, -1));
488 /** Get a drawin's drawable
489 * \param L The Lua VM state.
490 * \param drawin The drawin object.
491 * \return The number of elements pushed on stack.
494 luaA_drawin_get_drawable(lua_State
*L
, drawin_t
*drawin
)
496 luaA_object_push_item(L
, -2, drawin
->drawable
);
500 /** Get the drawin's bounding shape.
501 * \param L The Lua VM state.
502 * \param drawin The drawin object.
503 * \return The number of elements pushed on stack.
506 luaA_drawin_get_shape_bounding(lua_State
*L
, drawin_t
*drawin
)
508 cairo_surface_t
*surf
= xwindow_get_shape(drawin
->window
, XCB_SHAPE_SK_BOUNDING
);
511 /* lua has to make sure to free the ref or we have a leak */
512 lua_pushlightuserdata(L
, surf
);
516 /** Set the drawin's bounding shape.
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_shape_bounding(lua_State
*L
, drawin_t
*drawin
)
524 cairo_surface_t
*surf
= NULL
;
525 if(!lua_isnil(L
, -1))
526 surf
= (cairo_surface_t
*)lua_touserdata(L
, -1);
527 xwindow_set_shape(drawin
->window
,
528 drawin
->geometry
.width
+ 2*drawin
->border_width
,
529 drawin
->geometry
.height
+ 2*drawin
->border_width
,
530 XCB_SHAPE_SK_BOUNDING
, surf
, -drawin
->border_width
);
531 luaA_object_emit_signal(L
, -3, "property::shape_bounding", 0);
535 /** Get the drawin's clip shape.
536 * \param L The Lua VM state.
537 * \param drawin The drawin object.
538 * \return The number of elements pushed on stack.
541 luaA_drawin_get_shape_clip(lua_State
*L
, drawin_t
*drawin
)
543 cairo_surface_t
*surf
= xwindow_get_shape(drawin
->window
, XCB_SHAPE_SK_CLIP
);
546 /* lua has to make sure to free the ref or we have a leak */
547 lua_pushlightuserdata(L
, surf
);
551 /** Set the drawin's clip shape.
552 * \param L The Lua VM state.
553 * \param drawin The drawin object.
554 * \return The number of elements pushed on stack.
557 luaA_drawin_set_shape_clip(lua_State
*L
, drawin_t
*drawin
)
559 cairo_surface_t
*surf
= NULL
;
560 if(!lua_isnil(L
, -1))
561 surf
= (cairo_surface_t
*)lua_touserdata(L
, -1);
562 xwindow_set_shape(drawin
->window
, drawin
->geometry
.width
, drawin
->geometry
.height
,
563 XCB_SHAPE_SK_CLIP
, surf
, 0);
564 luaA_object_emit_signal(L
, -3, "property::shape_clip", 0);
569 drawin_class_setup(lua_State
*L
)
571 static const struct luaL_Reg drawin_methods
[] =
573 LUA_CLASS_METHODS(drawin
)
574 { "__call", luaA_drawin_new
},
578 static const struct luaL_Reg drawin_meta
[] =
580 LUA_OBJECT_META(drawin
)
582 { "geometry", luaA_drawin_geometry
},
586 luaA_class_setup(L
, &drawin_class
, "drawin", &window_class
,
587 (lua_class_allocator_t
) drawin_allocator
,
588 (lua_class_collector_t
) drawin_wipe
,
590 luaA_class_index_miss_property
, luaA_class_newindex_miss_property
,
591 drawin_methods
, drawin_meta
);
592 luaA_class_add_property(&drawin_class
, "drawable",
594 (lua_class_propfunc_t
) luaA_drawin_get_drawable
,
596 luaA_class_add_property(&drawin_class
, "visible",
597 (lua_class_propfunc_t
) luaA_drawin_set_visible
,
598 (lua_class_propfunc_t
) luaA_drawin_get_visible
,
599 (lua_class_propfunc_t
) luaA_drawin_set_visible
);
600 luaA_class_add_property(&drawin_class
, "ontop",
601 (lua_class_propfunc_t
) luaA_drawin_set_ontop
,
602 (lua_class_propfunc_t
) luaA_drawin_get_ontop
,
603 (lua_class_propfunc_t
) luaA_drawin_set_ontop
);
604 luaA_class_add_property(&drawin_class
, "cursor",
605 (lua_class_propfunc_t
) luaA_drawin_set_cursor
,
606 (lua_class_propfunc_t
) luaA_drawin_get_cursor
,
607 (lua_class_propfunc_t
) luaA_drawin_set_cursor
);
608 luaA_class_add_property(&drawin_class
, "x",
609 (lua_class_propfunc_t
) luaA_drawin_set_x
,
610 (lua_class_propfunc_t
) luaA_drawin_get_x
,
611 (lua_class_propfunc_t
) luaA_drawin_set_x
);
612 luaA_class_add_property(&drawin_class
, "y",
613 (lua_class_propfunc_t
) luaA_drawin_set_y
,
614 (lua_class_propfunc_t
) luaA_drawin_get_y
,
615 (lua_class_propfunc_t
) luaA_drawin_set_y
);
616 luaA_class_add_property(&drawin_class
, "width",
617 (lua_class_propfunc_t
) luaA_drawin_set_width
,
618 (lua_class_propfunc_t
) luaA_drawin_get_width
,
619 (lua_class_propfunc_t
) luaA_drawin_set_width
);
620 luaA_class_add_property(&drawin_class
, "height",
621 (lua_class_propfunc_t
) luaA_drawin_set_height
,
622 (lua_class_propfunc_t
) luaA_drawin_get_height
,
623 (lua_class_propfunc_t
) luaA_drawin_set_height
);
624 luaA_class_add_property(&drawin_class
, "type",
625 (lua_class_propfunc_t
) luaA_window_set_type
,
626 (lua_class_propfunc_t
) luaA_window_get_type
,
627 (lua_class_propfunc_t
) luaA_window_set_type
);
628 luaA_class_add_property(&drawin_class
, "shape_bounding",
629 (lua_class_propfunc_t
) luaA_drawin_set_shape_bounding
,
630 (lua_class_propfunc_t
) luaA_drawin_get_shape_bounding
,
631 (lua_class_propfunc_t
) luaA_drawin_set_shape_bounding
);
632 luaA_class_add_property(&drawin_class
, "shape_clip",
633 (lua_class_propfunc_t
) luaA_drawin_set_shape_clip
,
634 (lua_class_propfunc_t
) luaA_drawin_get_shape_clip
,
635 (lua_class_propfunc_t
) luaA_drawin_set_shape_clip
);
637 signal_add(&drawin_class
.signals
, "property::shape_bounding");
638 signal_add(&drawin_class
.signals
, "property::shape_clip");
639 signal_add(&drawin_class
.signals
, "property::border_width");
640 signal_add(&drawin_class
.signals
, "property::cursor");
641 signal_add(&drawin_class
.signals
, "property::height");
642 signal_add(&drawin_class
.signals
, "property::ontop");
643 signal_add(&drawin_class
.signals
, "property::visible");
644 signal_add(&drawin_class
.signals
, "property::width");
645 signal_add(&drawin_class
.signals
, "property::x");
646 signal_add(&drawin_class
.signals
, "property::y");
649 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80