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>
36 LUA_OBJECT_FUNCS(drawin_class
, drawin_t
, drawin
)
38 /** Kick out systray windows.
41 drawin_systray_kickout(drawin_t
*w
)
43 if(globalconf
.systray
.parent
== w
)
45 /* Who! Check that we're not deleting a drawin with a systray, because it
46 * may be its parent. If so, we reparent to root before, otherwise it will
49 xcb_reparent_window(globalconf
.connection
,
50 globalconf
.systray
.window
,
51 globalconf
.screen
->root
,
54 globalconf
.systray
.parent
= NULL
;
59 drawin_wipe(drawin_t
*w
)
61 /* The drawin must already be unmapped, else it
62 * couldn't be garbage collected -> no unmap needed */
66 /* Make sure that cairo knows that this surface can't be unused anymore.
67 * This is needed since lua could still have a reference to it. */
68 cairo_surface_finish(w
->surface
);
69 cairo_surface_destroy(w
->surface
);
75 client_ignore_enterleave_events();
76 /* Make sure we don't accidentally kill the systray window */
77 drawin_systray_kickout(w
);
78 xcb_destroy_window(globalconf
.connection
, w
->window
);
80 client_restore_enterleave_events();
85 xcb_free_pixmap(globalconf
.connection
, w
->pixmap
);
91 drawin_unref_simplified(drawin_t
**item
)
93 luaA_object_unref(globalconf
.L
, *item
);
97 drawin_update_drawing(drawin_t
*w
)
99 /* If this drawin isn't visible, we don't need an up-to-date cairo surface
100 * for it. (drawin_map() will later make sure we are called again) */
103 /* Clean up old stuff */
106 /* In case lua still got a reference to the surface, it still won't be
107 * able to do anything with it because we finish it. */
108 cairo_surface_finish(w
->surface
);
109 cairo_surface_destroy(w
->surface
);
112 xcb_free_pixmap(globalconf
.connection
, w
->pixmap
);
114 /* Create a pixmap */
115 xcb_screen_t
*s
= globalconf
.screen
;
116 w
->pixmap
= xcb_generate_id(globalconf
.connection
);
117 xcb_create_pixmap(globalconf
.connection
, globalconf
.default_depth
, w
->pixmap
, s
->root
,
118 w
->geometry
.width
, w
->geometry
.height
);
119 /* and create a surface for that pixmap */
120 w
->surface
= cairo_xcb_surface_create(globalconf
.connection
,
121 w
->pixmap
, globalconf
.visual
,
122 w
->geometry
.width
, w
->geometry
.height
);
123 /* Make sure the pixmap doesn't contain garbage by filling it with black */
124 cairo_t
*cr
= cairo_create(w
->surface
);
125 cairo_set_operator(cr
, CAIRO_OPERATOR_CLEAR
);
130 /** Initialize a drawin.
131 * \param w The drawin to initialize.
134 drawin_init(drawin_t
*w
)
136 xcb_screen_t
*s
= globalconf
.screen
;
138 w
->window
= xcb_generate_id(globalconf
.connection
);
139 xcb_create_window(globalconf
.connection
, globalconf
.default_depth
, w
->window
, s
->root
,
140 w
->geometry
.x
, w
->geometry
.y
,
141 w
->geometry
.width
, w
->geometry
.height
,
142 w
->border_width
, XCB_COPY_FROM_PARENT
, globalconf
.visual
->visual_id
,
143 XCB_CW_BORDER_PIXEL
| XCB_CW_BIT_GRAVITY
144 | XCB_CW_OVERRIDE_REDIRECT
| XCB_CW_EVENT_MASK
| XCB_CW_COLORMAP
148 w
->border_color
.pixel
,
149 XCB_GRAVITY_NORTH_WEST
,
151 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
152 | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
| XCB_EVENT_MASK_ENTER_WINDOW
153 | XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_STRUCTURE_NOTIFY
154 | XCB_EVENT_MASK_POINTER_MOTION
| XCB_EVENT_MASK_EXPOSURE
155 | XCB_EVENT_MASK_PROPERTY_CHANGE
,
156 globalconf
.default_cmap
,
157 xcursor_new(globalconf
.connection
, xcursor_font_fromstr(w
->cursor
))
160 /* Set the right properties */
161 ewmh_update_window_type(w
->window
, window_translate_type(w
->type
));
162 ewmh_update_strut(w
->window
, &w
->strut
);
165 /** Refresh the window content by copying its pixmap data to its window.
166 * \param w The drawin to refresh.
169 drawin_refresh_pixmap(drawin_t
*w
)
171 drawin_refresh_pixmap_partial(w
, 0, 0, w
->geometry
.width
, w
->geometry
.height
);
174 /** Move and/or resize a drawin
175 * \param L The Lua VM state.
176 * \param udx The index of the drawin.
177 * \param geometry The new geometry.
180 drawin_moveresize(lua_State
*L
, int udx
, area_t geometry
)
182 drawin_t
*w
= luaA_checkudata(L
, udx
, &drawin_class
);
183 int number_of_vals
= 0;
184 uint32_t moveresize_win_vals
[4], mask_vals
= 0;
186 if(w
->geometry
.x
!= geometry
.x
)
188 w
->geometry
.x
= moveresize_win_vals
[number_of_vals
++] = geometry
.x
;
189 mask_vals
|= XCB_CONFIG_WINDOW_X
;
192 if(w
->geometry
.y
!= geometry
.y
)
194 w
->geometry
.y
= moveresize_win_vals
[number_of_vals
++] = geometry
.y
;
195 mask_vals
|= XCB_CONFIG_WINDOW_Y
;
198 if(geometry
.width
> 0 && w
->geometry
.width
!= geometry
.width
)
200 w
->geometry
.width
= moveresize_win_vals
[number_of_vals
++] = geometry
.width
;
201 mask_vals
|= XCB_CONFIG_WINDOW_WIDTH
;
204 if(geometry
.height
> 0 && w
->geometry
.height
!= geometry
.height
)
206 w
->geometry
.height
= moveresize_win_vals
[number_of_vals
++] = geometry
.height
;
207 mask_vals
|= XCB_CONFIG_WINDOW_HEIGHT
;
210 if(mask_vals
& (XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
))
211 drawin_update_drawing(w
);
214 client_ignore_enterleave_events();
217 xcb_configure_window(globalconf
.connection
, w
->window
, mask_vals
, moveresize_win_vals
);
220 client_restore_enterleave_events();
222 if(mask_vals
& XCB_CONFIG_WINDOW_X
)
223 luaA_object_emit_signal(L
, udx
, "property::x", 0);
224 if(mask_vals
& XCB_CONFIG_WINDOW_Y
)
225 luaA_object_emit_signal(L
, udx
, "property::y", 0);
226 if(mask_vals
& XCB_CONFIG_WINDOW_WIDTH
)
227 luaA_object_emit_signal(L
, udx
, "property::width", 0);
228 if(mask_vals
& XCB_CONFIG_WINDOW_HEIGHT
)
229 luaA_object_emit_signal(L
, udx
, "property::height", 0);
232 /** Refresh the window content by copying its pixmap data to its window.
233 * \param drawin The drawin to refresh.
234 * \param x The copy starting point x component.
235 * \param y The copy starting point y component.
236 * \param w The copy width from the x component.
237 * \param h The copy height from the y component.
240 drawin_refresh_pixmap_partial(drawin_t
*drawin
,
241 int16_t x
, int16_t y
,
242 uint16_t w
, uint16_t h
)
244 /* Make cairo do all pending drawing */
245 cairo_surface_flush(drawin
->surface
);
246 xcb_copy_area(globalconf
.connection
, drawin
->pixmap
,
247 drawin
->window
, globalconf
.gc
, x
, y
, x
, y
,
252 drawin_map(drawin_t
*drawin
)
255 client_ignore_enterleave_events();
257 xcb_map_window(globalconf
.connection
, drawin
->window
);
258 drawin_update_drawing(drawin
);
260 client_restore_enterleave_events();
261 /* Stack this drawin correctly */
263 /* Add it to the list of visible drawins */
264 drawin_array_append(&globalconf
.drawins
, drawin
);
268 drawin_unmap(drawin_t
*drawin
)
270 xcb_unmap_window(globalconf
.connection
, drawin
->window
);
271 foreach(item
, globalconf
.drawins
)
274 drawin_array_remove(&globalconf
.drawins
, item
);
279 /** Get a drawin by its window.
280 * \param win The window id.
281 * \return A drawin if found, NULL otherwise.
284 drawin_getbywin(xcb_window_t win
)
286 foreach(w
, globalconf
.drawins
)
287 if((*w
)->window
== win
)
292 /** Set a drawin visible or not.
293 * \param L The Lua VM state.
294 * \param udx The drawin.
295 * \param v The visible value.
298 drawin_set_visible(lua_State
*L
, int udx
, bool v
)
300 drawin_t
*drawin
= luaA_checkudata(L
, udx
, &drawin_class
);
301 if(v
!= drawin
->visible
)
308 /* duplicate drawin */
309 lua_pushvalue(L
, udx
);
311 luaA_object_ref_class(globalconf
.L
, -1, &drawin_class
);
316 client_ignore_enterleave_events();
318 drawin_unmap(drawin
);
320 client_restore_enterleave_events();
322 luaA_object_unref(globalconf
.L
, drawin
);
325 luaA_object_emit_signal(L
, udx
, "property::visible", 0);
326 if(strut_has_value(&drawin
->strut
))
329 screen_getbycoord(drawin
->geometry
.x
, drawin
->geometry
.y
);
330 screen_emit_signal(globalconf
.L
, screen
, "property::workarea", 0);
336 drawin_allocator(lua_State
*L
)
338 drawin_t
*w
= drawin_new(L
);
343 w
->cursor
= a_strdup("left_ptr");
344 w
->geometry
.width
= 1;
345 w
->geometry
.height
= 1;
346 w
->type
= _NET_WM_WINDOW_TYPE_NORMAL
;
353 /** Create a new drawin.
354 * \param L The Lua VM state.
355 * \return The number of elements pushed on stack.
358 luaA_drawin_new(lua_State
*L
)
360 luaA_class_new(L
, &drawin_class
);
365 /* Set or get the drawin geometry.
366 * \param L The Lua VM state.
367 * \return The number of elements pushed on stack.
369 * \lparam An optional table with drawin geometry.
370 * \lreturn The drawin geometry.
373 luaA_drawin_geometry(lua_State
*L
)
375 drawin_t
*drawin
= luaA_checkudata(L
, 1, &drawin_class
);
377 if(lua_gettop(L
) == 2)
381 luaA_checktable(L
, 2);
382 wingeom
.x
= luaA_getopt_number(L
, 2, "x", drawin
->geometry
.x
);
383 wingeom
.y
= luaA_getopt_number(L
, 2, "y", drawin
->geometry
.y
);
384 wingeom
.width
= luaA_getopt_number(L
, 2, "width", drawin
->geometry
.width
);
385 wingeom
.height
= luaA_getopt_number(L
, 2, "height", drawin
->geometry
.height
);
387 if(wingeom
.width
> 0 && wingeom
.height
> 0)
388 drawin_moveresize(L
, 1, wingeom
);
391 return luaA_pusharea(L
, drawin
->geometry
);
395 LUA_OBJECT_EXPORT_PROPERTY(drawin
, drawin_t
, ontop
, lua_pushboolean
)
396 LUA_OBJECT_EXPORT_PROPERTY(drawin
, drawin_t
, cursor
, lua_pushstring
)
397 LUA_OBJECT_EXPORT_PROPERTY(drawin
, drawin_t
, visible
, lua_pushboolean
)
400 luaA_drawin_set_x(lua_State
*L
, drawin_t
*drawin
)
402 drawin_moveresize(L
, -3, (area_t
) { .x
= luaL_checknumber(L
, -1),
403 .y
= drawin
->geometry
.y
,
404 .width
= drawin
->geometry
.width
,
405 .height
= drawin
->geometry
.height
});
410 luaA_drawin_get_x(lua_State
*L
, drawin_t
*drawin
)
412 lua_pushnumber(L
, drawin
->geometry
.x
);
417 luaA_drawin_set_y(lua_State
*L
, drawin_t
*drawin
)
419 drawin_moveresize(L
, -3, (area_t
) { .x
= drawin
->geometry
.x
,
420 .y
= luaL_checknumber(L
, -1),
421 .width
= drawin
->geometry
.width
,
422 .height
= drawin
->geometry
.height
});
427 luaA_drawin_get_y(lua_State
*L
, drawin_t
*drawin
)
429 lua_pushnumber(L
, drawin
->geometry
.y
);
434 luaA_drawin_set_width(lua_State
*L
, drawin_t
*drawin
)
436 int width
= luaL_checknumber(L
, -1);
438 luaL_error(L
, "invalid width");
439 drawin_moveresize(L
, -3, (area_t
) { .x
= drawin
->geometry
.x
,
440 .y
= drawin
->geometry
.y
,
442 .height
= drawin
->geometry
.height
});
447 luaA_drawin_get_width(lua_State
*L
, drawin_t
*drawin
)
449 lua_pushnumber(L
, drawin
->geometry
.width
);
454 luaA_drawin_set_height(lua_State
*L
, drawin_t
*drawin
)
456 int height
= luaL_checknumber(L
, -1);
458 luaL_error(L
, "invalid height");
459 drawin_moveresize(L
, -3, (area_t
) { .x
= drawin
->geometry
.x
,
460 .y
= drawin
->geometry
.y
,
461 .width
= drawin
->geometry
.width
,
467 luaA_drawin_get_height(lua_State
*L
, drawin_t
*drawin
)
469 lua_pushnumber(L
, drawin
->geometry
.height
);
473 /** Set the drawin on top status.
474 * \param L The Lua VM state.
475 * \param drawin The drawin object.
476 * \return The number of elements pushed on stack.
479 luaA_drawin_set_ontop(lua_State
*L
, drawin_t
*drawin
)
481 bool b
= luaA_checkboolean(L
, -1);
482 if(b
!= drawin
->ontop
)
486 luaA_object_emit_signal(L
, -3, "property::ontop", 0);
491 /** Set the drawin cursor.
492 * \param L The Lua VM state.
493 * \param drawin The drawin object.
494 * \return The number of elements pushed on stack.
497 luaA_drawin_set_cursor(lua_State
*L
, drawin_t
*drawin
)
499 const char *buf
= luaL_checkstring(L
, -1);
502 uint16_t cursor_font
= xcursor_font_fromstr(buf
);
505 xcb_cursor_t cursor
= xcursor_new(globalconf
.connection
, cursor_font
);
506 p_delete(&drawin
->cursor
);
507 drawin
->cursor
= a_strdup(buf
);
508 xwindow_set_cursor(drawin
->window
, cursor
);
509 luaA_object_emit_signal(L
, -3, "property::cursor", 0);
515 /** Set the drawin visibility.
516 * \param L The Lua VM state.
517 * \param drawin The drawin object.
518 * \return The number of elements pushed on stack.
521 luaA_drawin_set_visible(lua_State
*L
, drawin_t
*drawin
)
523 drawin_set_visible(L
, -3, luaA_checkboolean(L
, -1));
527 /** Get a drawin's surface
528 * \param L The Lua VM state.
529 * \param drawin The drawin object.
530 * \return The number of elements pushed on stack.
533 luaA_drawin_get_surface(lua_State
*L
, drawin_t
*drawin
)
535 return oocairo_surface_push(L
, drawin
->surface
);
538 /** Refresh a drawin's content. This has to be called whenever some drawing to
539 * the drawin's surface has been done and should become visible.
540 * \param L The Lua VM state.
541 * \return The number of elements pushed on stack.
544 luaA_drawin_refresh(lua_State
*L
)
546 drawin_t
*drawin
= luaA_checkudata(L
, 1, &drawin_class
);
547 drawin_refresh_pixmap(drawin
);
553 drawin_class_setup(lua_State
*L
)
555 static const struct luaL_reg drawin_methods
[] =
557 LUA_CLASS_METHODS(drawin
)
558 { "__call", luaA_drawin_new
},
562 static const struct luaL_reg drawin_meta
[] =
564 LUA_OBJECT_META(drawin
)
566 { "geometry", luaA_drawin_geometry
},
567 { "refresh", luaA_drawin_refresh
},
571 luaA_class_setup(L
, &drawin_class
, "drawin", &window_class
,
572 (lua_class_allocator_t
) drawin_allocator
,
573 (lua_class_collector_t
) drawin_wipe
,
575 luaA_class_index_miss_property
, luaA_class_newindex_miss_property
,
576 drawin_methods
, drawin_meta
);
577 luaA_class_add_property(&drawin_class
, "surface",
579 (lua_class_propfunc_t
) luaA_drawin_get_surface
,
581 luaA_class_add_property(&drawin_class
, "visible",
582 (lua_class_propfunc_t
) luaA_drawin_set_visible
,
583 (lua_class_propfunc_t
) luaA_drawin_get_visible
,
584 (lua_class_propfunc_t
) luaA_drawin_set_visible
);
585 luaA_class_add_property(&drawin_class
, "ontop",
586 (lua_class_propfunc_t
) luaA_drawin_set_ontop
,
587 (lua_class_propfunc_t
) luaA_drawin_get_ontop
,
588 (lua_class_propfunc_t
) luaA_drawin_set_ontop
);
589 luaA_class_add_property(&drawin_class
, "cursor",
590 (lua_class_propfunc_t
) luaA_drawin_set_cursor
,
591 (lua_class_propfunc_t
) luaA_drawin_get_cursor
,
592 (lua_class_propfunc_t
) luaA_drawin_set_cursor
);
593 luaA_class_add_property(&drawin_class
, "x",
594 (lua_class_propfunc_t
) luaA_drawin_set_x
,
595 (lua_class_propfunc_t
) luaA_drawin_get_x
,
596 (lua_class_propfunc_t
) luaA_drawin_set_x
);
597 luaA_class_add_property(&drawin_class
, "y",
598 (lua_class_propfunc_t
) luaA_drawin_set_y
,
599 (lua_class_propfunc_t
) luaA_drawin_get_y
,
600 (lua_class_propfunc_t
) luaA_drawin_set_y
);
601 luaA_class_add_property(&drawin_class
, "width",
602 (lua_class_propfunc_t
) luaA_drawin_set_width
,
603 (lua_class_propfunc_t
) luaA_drawin_get_width
,
604 (lua_class_propfunc_t
) luaA_drawin_set_width
);
605 luaA_class_add_property(&drawin_class
, "height",
606 (lua_class_propfunc_t
) luaA_drawin_set_height
,
607 (lua_class_propfunc_t
) luaA_drawin_get_height
,
608 (lua_class_propfunc_t
) luaA_drawin_set_height
);
609 luaA_class_add_property(&drawin_class
, "type",
610 (lua_class_propfunc_t
) luaA_window_set_type
,
611 (lua_class_propfunc_t
) luaA_window_get_type
,
612 (lua_class_propfunc_t
) luaA_window_set_type
);
614 signal_add(&drawin_class
.signals
, "mouse::enter");
615 signal_add(&drawin_class
.signals
, "mouse::leave");
616 signal_add(&drawin_class
.signals
, "property::border_width");
617 signal_add(&drawin_class
.signals
, "property::cursor");
618 signal_add(&drawin_class
.signals
, "property::height");
619 signal_add(&drawin_class
.signals
, "property::ontop");
620 signal_add(&drawin_class
.signals
, "property::visible");
621 signal_add(&drawin_class
.signals
, "property::widgets");
622 signal_add(&drawin_class
.signals
, "property::width");
623 signal_add(&drawin_class
.signals
, "property::x");
624 signal_add(&drawin_class
.signals
, "property::y");
627 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80