4 * Copyright (c) 2012 David Herrmann <dh.herrmann@googlemail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files
8 * (the "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * Wayland Terminal toolkit helpers
36 #include <wayland-client.h>
37 #include <wayland-cursor.h>
40 #include "shl_dlist.h"
42 #include "wlt_toolkit.h"
44 #define LOG_SUBSYSTEM "wlt_toolkit"
47 struct wl_shm_pool *w_pool;
60 struct ev_eloop *eloop;
61 struct wl_display *dp;
63 struct wl_global_listener *dp_listener;
64 struct shl_hook *listeners;
67 struct shl_dlist window_list;
69 struct wl_compositor *w_comp;
70 struct wl_seat *w_seat;
71 struct wl_shell *w_shell;
75 uint32_t pointer_enter_serial;
76 struct wl_pointer *w_pointer;
77 struct wlt_window *pointer_focus;
79 uint32_t cursor_serial;
80 unsigned int current_cursor;
81 struct wl_surface *w_cursor_surface;
82 struct wl_cursor_theme *cursor_theme;
83 struct wl_cursor *cursors[WLT_CURSOR_NUM];
88 struct shl_dlist list;
90 wlt_window_close_cb close_cb;
92 struct wlt_display *disp;
93 struct wlt_pool *pool;
95 struct wl_surface *w_surface;
96 struct wl_shell_surface *w_shell_surface;
97 struct wl_buffer *w_buffer;
105 unsigned int new_width;
106 unsigned int new_height;
107 unsigned int resize_edges;
108 struct wlt_shm_buffer buffer;
109 struct wl_callback *w_frame;
111 struct shl_dlist widget_list;
115 struct shl_dlist list;
116 struct wlt_window *wnd;
118 wlt_widget_redraw_cb redraw_cb;
119 wlt_widget_destroy_cb destroy_cb;
120 wlt_widget_prepare_resize_cb prepare_resize_cb;
121 wlt_widget_resize_cb resize_cb;
122 wlt_widget_pointer_enter_cb pointer_enter_cb;
123 wlt_widget_pointer_leave_cb pointer_leave_cb;
124 wlt_widget_pointer_motion_cb pointer_motion_cb;
125 wlt_widget_pointer_button_cb pointer_button_cb;
128 static int wlt_pool_new(struct wlt_pool **out, struct wlt_display *disp,
131 static const char template[] = "/wlterm-shared-XXXXXX";
132 struct wlt_pool *pool;
137 path = getenv("XDG_RUNTIME_DIR");
139 log_error("XDG_RUNTIME_DIR not set");
143 pool = malloc(sizeof(*pool));
146 memset(pool, 0, sizeof(*pool));
149 name = malloc(strlen(path) + sizeof(template));
155 strcat(name, template);
157 fd = mkostemp(name, O_CLOEXEC);
159 log_error("cannot create temporary file %s via mkostemp() (%d): %m",
168 log_warning("cannot unlink temporary file %s (%d): %m",
172 ret = ftruncate(fd, size);
174 log_error("cannot truncate temporary file to length %lu (%d): %m",
180 pool->data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
182 if (pool->data == MAP_FAILED) {
183 log_error("cannot mmap temporary file (%d): %m", errno);
188 pool->w_pool = wl_shm_create_pool(disp->w_shm, fd, size);
190 log_error("cannot create wayland shm pool object");
200 munmap(pool->data, size);
208 static void wlt_pool_free(struct wlt_pool *pool)
210 munmap(pool->data, pool->size);
211 wl_shm_pool_destroy(pool->w_pool);
216 * These cursor-names are based on:
217 * https://bugs.kde.org/attachment.cgi?id=67313
220 static char *(*cursors[])[] = {
221 [WLT_CURSOR_NONE] = &(char*[]) {
224 [WLT_CURSOR_TOP] = &(char*[]) {
229 [WLT_CURSOR_BOTTOM] = &(char*[]) {
234 [WLT_CURSOR_LEFT] = &(char*[]) {
239 [WLT_CURSOR_RIGHT] = &(char*[]) {
244 [WLT_CURSOR_TOP_LEFT] = &(char*[]) {
249 [WLT_CURSOR_TOP_RIGHT] = &(char*[]) {
254 [WLT_CURSOR_BOTTOM_LEFT] = &(char*[]) {
255 "bottom_left_corner",
259 [WLT_CURSOR_BOTTOM_RIGHT] = &(char*[]) {
260 "bottom_right_corner",
264 [WLT_CURSOR_DRAGGING] = &(char*[]) {
267 "208530c400c041818281048008011002",
270 [WLT_CURSOR_LEFT_PTR] = &(char*[]) {
277 [WLT_CURSOR_IBEAM] = &(char*[]) {
285 static int load_cursors(struct wlt_display *disp)
288 size_t size = sizeof(cursors) / sizeof(*cursors);
289 struct wl_cursor *cursor;
291 disp->w_cursor_surface = wl_compositor_create_surface(disp->w_comp);
293 disp->cursor_theme = wl_cursor_theme_load(NULL, 32, disp->w_shm);
294 if (!disp->cursor_theme) {
295 log_warning("cannot load curdor theme");
299 for (i = 0; i < WLT_CURSOR_NUM; ++i) {
301 if (i < size && cursors[i]) {
302 for (j = 0; (*cursors[i])[j]; ++j) {
303 cursor = wl_cursor_theme_get_cursor(
311 if (!cursor && i != WLT_CURSOR_NONE)
312 log_warning("cannot load cursor for ID %d", i);
314 disp->cursors[i] = cursor;
320 static void unload_cursors(struct wlt_display *disp)
322 if (disp->cursor_theme)
323 wl_cursor_theme_destroy(disp->cursor_theme);
324 if (disp->w_cursor_surface)
325 wl_surface_destroy(disp->w_cursor_surface);
328 static void set_cursor(struct wlt_display *disp, unsigned int cursor)
331 struct wl_cursor *cur;
332 struct wl_cursor_image *image;
333 struct wl_buffer *buffer;
335 if (cursor >= WLT_CURSOR_NUM)
336 cursor = WLT_CURSOR_LEFT_PTR;
338 if (disp->pointer_enter_serial > disp->cursor_serial)
341 if (!force && cursor == disp->current_cursor)
344 disp->current_cursor = cursor;
345 disp->cursor_serial = disp->pointer_enter_serial;
347 cur = disp->cursors[cursor];
349 wl_pointer_set_cursor(disp->w_pointer,
350 disp->pointer_enter_serial,
355 image = cur->images[0];
356 buffer = wl_cursor_image_get_buffer(image);
358 log_error("cannot load buffer for cursor image");
362 wl_pointer_set_cursor(disp->w_pointer,
363 disp->pointer_enter_serial,
364 disp->w_cursor_surface,
365 image->hotspot_x, image->hotspot_y);
366 wl_surface_attach(disp->w_cursor_surface, buffer, 0, 0);
367 wl_surface_damage(disp->w_cursor_surface, 0, 0,
368 image->width, image->height);
371 static int dp_mask_update(uint32_t mask, void *data)
373 struct wlt_display *disp = data;
381 if (mask & WL_DISPLAY_READABLE)
383 if (mask & WL_DISPLAY_WRITABLE)
384 mode |= EV_WRITEABLE;
386 ret = ev_fd_update(disp->dp_fd, mode);
388 log_warning("cannot update wayland-fd event-polling modes (%d)",
394 static void dp_event(struct ev_fd *fd, int mask, void *data)
396 struct wlt_display *disp = data;
399 if (mask & (EV_HUP | EV_ERR)) {
400 log_warning("HUP/ERR on wayland socket");
401 disp->state = STATE_HUP;
402 shl_hook_call(disp->listeners, disp, (void*)WLT_DISPLAY_HUP);
403 ev_eloop_rm_fd(disp->dp_fd);
409 if (mask & EV_READABLE)
410 mode |= WL_DISPLAY_READABLE;
411 if (mask & EV_WRITEABLE)
412 mode |= WL_DISPLAY_WRITABLE;
415 wl_display_iterate(disp->dp, mode);
418 static void pointer_enter(void *data, struct wl_pointer *w_pointer,
419 uint32_t serial, struct wl_surface *w_surface,
420 wl_fixed_t x, wl_fixed_t y)
422 struct wlt_display *disp = data;
423 struct shl_dlist *iter;
424 struct wlt_window *wnd;
425 struct wlt_widget *widget;
430 shl_dlist_for_each(iter, &disp->window_list) {
431 wnd = shl_dlist_entry(iter, struct wlt_window, list);
432 if (wnd->w_surface == w_surface)
436 if (iter == &disp->window_list) {
437 log_debug("unknown surface");
441 disp->pointer_enter_serial = serial;
442 disp->last_serial = serial;
443 disp->pointer_focus = wnd;
444 shl_dlist_for_each(iter, &wnd->widget_list) {
445 widget = shl_dlist_entry(iter, struct wlt_widget, list);
446 if (widget->pointer_enter_cb)
447 widget->pointer_enter_cb(widget, x >> 8, y >> 8,
452 static void pointer_leave(void *data, struct wl_pointer *w_pointer,
453 uint32_t serial, struct wl_surface *w_surface)
455 struct wlt_display *disp = data;
456 struct shl_dlist *iter;
457 struct wlt_window *wnd;
458 struct wlt_widget *widget;
460 wnd = disp->pointer_focus;
461 disp->pointer_focus = NULL;
462 disp->last_serial = serial;
467 shl_dlist_for_each(iter, &wnd->widget_list) {
468 widget = shl_dlist_entry(iter, struct wlt_widget, list);
469 if (widget->pointer_leave_cb)
470 widget->pointer_leave_cb(widget, widget->data);
474 static void pointer_motion(void *data, struct wl_pointer *w_pointer,
475 uint32_t time, wl_fixed_t x, wl_fixed_t y)
477 struct wlt_display *disp = data;
478 struct shl_dlist *iter;
479 struct wlt_window *wnd;
480 struct wlt_widget *widget;
482 wnd = disp->pointer_focus;
486 shl_dlist_for_each(iter, &wnd->widget_list) {
487 widget = shl_dlist_entry(iter, struct wlt_widget, list);
488 if (widget->pointer_motion_cb)
489 widget->pointer_motion_cb(widget, x >> 8, y >> 8,
494 static void pointer_button(void *data, struct wl_pointer *w_pointer,
495 uint32_t serial, uint32_t time, uint32_t button,
498 struct wlt_display *disp = data;
499 struct shl_dlist *iter;
500 struct wlt_window *wnd;
501 struct wlt_widget *widget;
503 wnd = disp->pointer_focus;
504 disp->last_serial = serial;
509 shl_dlist_for_each(iter, &wnd->widget_list) {
510 widget = shl_dlist_entry(iter, struct wlt_widget, list);
511 if (widget->pointer_button_cb)
512 widget->pointer_button_cb(widget, button, state,
517 static void pointer_axis(void *data, struct wl_pointer *w_pointer,
518 uint32_t time, uint32_t axis, wl_fixed_t value)
522 static const struct wl_pointer_listener pointer_listener = {
523 .enter = pointer_enter,
524 .leave = pointer_leave,
525 .motion = pointer_motion,
526 .button = pointer_button,
527 .axis = pointer_axis,
530 static void check_ready(struct wlt_display *disp)
532 if (disp->state > STATE_INIT)
540 log_debug("wayland display initialized");
543 disp->state = STATE_RUNNING;
544 shl_hook_call(disp->listeners, disp, (void*)WLT_DISPLAY_READY);
548 static void seat_capabilities(void *data, struct wl_seat *seat,
549 enum wl_seat_capability caps)
551 struct wlt_display *disp = data;
553 if (caps & WL_SEAT_CAPABILITY_POINTER && !disp->w_pointer) {
554 disp->w_pointer = wl_seat_get_pointer(seat);
555 wl_pointer_add_listener(disp->w_pointer, &pointer_listener,
562 static const struct wl_seat_listener seat_listener = {
563 .capabilities = seat_capabilities,
566 static void dp_global(struct wl_display *dp, uint32_t id, const char *iface,
567 uint32_t version, void *data)
569 struct wlt_display *disp = data;
571 if (!strcmp(iface, "wl_display")) {
572 log_debug("new wl_display global");
574 } else if (!strcmp(iface, "wl_compositor")) {
576 log_error("global wl_compositor advertised twice");
579 disp->w_comp = wl_display_bind(disp->dp,
581 &wl_compositor_interface);
583 log_error("cannot bind wl_compositor object");
586 } else if (!strcmp(iface, "wl_seat")) {
588 log_error("global wl_seat advertised twice");
591 disp->w_seat = wl_display_bind(disp->dp,
595 log_error("cannot bind wl_seat object");
599 wl_seat_add_listener(disp->w_seat, &seat_listener, disp);
600 } else if (!strcmp(iface, "wl_shell")) {
602 log_error("global wl_shell advertised twice");
605 disp->w_shell = wl_display_bind(disp->dp,
607 &wl_shell_interface);
608 if (!disp->w_shell) {
609 log_error("cannot bind wl_shell object");
612 } else if (!strcmp(iface, "wl_shm")) {
614 log_error("global wl_shm advertised twice");
617 disp->w_shm = wl_display_bind(disp->dp,
621 log_error("cannot bind wl_shm object");
625 log_debug("ignoring new unknown global %s", iface);
629 log_debug("new global %s", iface);
634 int wlt_display_new(struct wlt_display **out,
635 struct ev_eloop *eloop)
637 struct wlt_display *disp;
643 log_debug("creating new wlt-display");
645 disp = malloc(sizeof(*disp));
648 memset(disp, 0, sizeof(*disp));
651 shl_dlist_init(&disp->window_list);
653 ret = shl_hook_new(&disp->listeners);
655 log_error("cannot create listeners hook");
659 disp->dp = wl_display_connect(NULL);
661 log_error("cannot connect to wayland socket (%d): %m", errno);
666 ret = ev_eloop_new_fd(eloop,
668 wl_display_get_fd(disp->dp,
675 log_error("cannot create event-fd for wayland display (%d)",
680 disp->dp_listener = wl_display_add_global_listener(disp->dp,
683 if (!disp->dp_listener) {
684 log_error("cannot add wayland global listener (%d)");
688 log_debug("wlt-display waiting for globals...");
690 ev_eloop_ref(disp->eloop);
695 ev_eloop_rm_fd(disp->dp_fd);
697 wl_display_disconnect(disp->dp);
699 shl_hook_free(disp->listeners);
705 void wlt_display_ref(struct wlt_display *disp)
707 if (!disp || !disp->ref)
713 void wlt_display_unref(struct wlt_display *disp)
715 if (!disp || !disp->ref || --disp->ref)
718 unload_cursors(disp);
719 wl_display_remove_global_listener(disp->dp, disp->dp_listener);
720 ev_eloop_rm_fd(disp->dp_fd);
721 wl_display_flush(disp->dp);
722 wl_display_disconnect(disp->dp);
723 shl_hook_free(disp->listeners);
724 ev_eloop_unref(disp->eloop);
728 int wlt_display_register_cb(struct wlt_display *disp,
729 wlt_display_cb cb, void *data)
734 return shl_hook_add_cast(disp->listeners, cb, data);
737 void wlt_display_unregister_cb(struct wlt_display *disp,
738 wlt_display_cb cb, void *data)
743 shl_hook_rm_cast(disp->listeners, cb, data);
746 static void shell_surface_ping(void *data, struct wl_shell_surface *s,
749 wl_shell_surface_pong(s, serial);
752 static void wlt_window_do_redraw(struct wlt_window *wnd,
756 struct shl_dlist *iter;
757 struct wlt_widget *widget;
759 struct wlt_rect alloc;
763 alloc.width = wnd->buffer.width;
764 alloc.height = wnd->buffer.height;
765 shl_dlist_for_each(iter, &wnd->widget_list) {
766 widget = shl_dlist_entry(iter, struct wlt_widget, list);
767 if (widget->resize_cb)
768 widget->resize_cb(widget, &alloc, widget->data);
771 memset(wnd->buffer.data, 0,
772 wnd->buffer.stride * wnd->buffer.height);
774 wnd->skip_damage = true;
775 shl_dlist_for_each(iter, &wnd->widget_list) {
776 widget = shl_dlist_entry(iter, struct wlt_widget, list);
777 if (widget->redraw_cb)
778 widget->redraw_cb(widget, widget->data);
780 wnd->skip_damage = false;
782 if (!wnd->buffer_attached) {
783 wnd->buffer_attached = true;
784 if (wnd->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
785 x = (int)oldw - wnd->buffer.width;
788 if (wnd->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
789 y = (int)oldh - wnd->buffer.height;
792 wl_surface_attach(wnd->w_surface, wnd->w_buffer, x, y);
793 wnd->resize_edges = 0;
796 wl_surface_damage(wnd->w_surface, 0, 0, wnd->buffer.width,
800 static int resize_window(struct wlt_window *wnd, unsigned int width,
803 struct shl_dlist *iter;
804 struct wlt_widget *widget;
805 struct wl_buffer *old_buffer = NULL;
806 struct wlt_pool *old_pool = NULL;
809 unsigned int oldw, oldh;
811 if (!wnd || !width || !height)
814 shl_dlist_for_each(iter, &wnd->widget_list) {
815 widget = shl_dlist_entry(iter, struct wlt_widget, list);
816 if (widget->prepare_resize_cb)
817 widget->prepare_resize_cb(widget, &width, &height,
821 if (width == wnd->buffer.width &&
822 height == wnd->buffer.height)
825 oldw = wnd->buffer.width;
826 oldh = wnd->buffer.height;
828 nsize = width * height * 4;
829 if (wnd->pool && wnd->pool->size >= nsize) {
830 old_buffer = wnd->w_buffer;
831 wnd->w_buffer = wl_shm_pool_create_buffer(wnd->pool->w_pool,
834 WL_SHM_FORMAT_ARGB8888);
835 if (!wnd->w_buffer) {
836 log_error("cannot create wayland shm buffer");
837 wnd->w_buffer = old_buffer;
841 old_pool = wnd->pool;
842 ret = wlt_pool_new(&wnd->pool, wnd->disp, nsize);
844 log_error("cannot create memory pool");
845 wnd->pool = old_pool;
849 old_buffer = wnd->w_buffer;
850 wnd->w_buffer = wl_shm_pool_create_buffer(wnd->pool->w_pool,
853 WL_SHM_FORMAT_ARGB8888);
854 if (!wnd->w_buffer) {
855 log_error("cannot create wayland shm buffer");
856 wnd->w_buffer = old_buffer;
857 wlt_pool_free(wnd->pool);
858 wnd->pool = old_pool;
863 wnd->buffer.data = wnd->pool->data;
864 wnd->buffer.width = width;
865 wnd->buffer.height = height;
866 wnd->buffer.stride = width * 4;
867 wnd->buffer_attached = false;
869 wlt_window_do_redraw(wnd, oldw, oldh);
872 wl_buffer_destroy(old_buffer);
874 wlt_pool_free(old_pool);
879 static void idle_frame(struct ev_eloop *eloop, void *unused, void *data);
881 static void do_frame(struct wlt_window *wnd)
883 wnd->idle_pending = false;
884 ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd);
886 if (wnd->need_resize) {
887 wnd->need_resize = false;
888 wnd->need_redraw = false;
889 resize_window(wnd, wnd->new_width, wnd->new_height);
892 if (wnd->need_redraw) {
893 wnd->need_redraw = false;
894 wlt_window_do_redraw(wnd, wnd->buffer.width,
899 static void frame_callback(void *data, struct wl_callback *w_callback,
902 struct wlt_window *wnd = data;
904 wl_callback_destroy(w_callback);
906 wnd->need_frame = false;
911 static void idle_frame(struct ev_eloop *eloop, void *unused, void *data)
913 struct wlt_window *wnd = data;
915 wnd->need_frame = true;
919 static const struct wl_callback_listener frame_callback_listener = {
920 .done = frame_callback,
923 static void schedule_frame(struct wlt_window *wnd)
925 if (!wnd || wnd->w_frame)
928 if (!wnd->need_frame && !wnd->idle_pending) {
929 wnd->idle_pending = true;
930 ev_eloop_register_idle_cb(wnd->disp->eloop,
932 wnd->w_frame = wl_surface_frame(wnd->w_surface);
933 wl_callback_add_listener(wnd->w_frame,
934 &frame_callback_listener, wnd);
938 static void shell_surface_configure(void *data, struct wl_shell_surface *s,
939 uint32_t edges, int32_t width,
942 struct wlt_window *wnd = data;
949 wnd->resize_edges = edges;
950 wlt_window_set_size(wnd, width, height);
953 static void shell_surface_popup_done(void *data, struct wl_shell_surface *s)
957 static const struct wl_shell_surface_listener shell_surface_listener = {
958 .ping = shell_surface_ping,
959 .configure = shell_surface_configure,
960 .popup_done = shell_surface_popup_done,
963 static void close_window(struct ev_eloop *eloop, void *unused, void *data)
965 struct wlt_window *wnd = data;
967 ev_eloop_unregister_idle_cb(eloop, close_window, wnd);
968 wnd->close_pending = false;
971 wnd->close_cb(wnd, wnd->data);
974 int wlt_display_create_window(struct wlt_display *disp,
975 struct wlt_window **out,
980 struct wlt_window *wnd;
983 if (!disp || !out || !width || !height)
986 if (disp->state != STATE_RUNNING) {
987 log_error("cannot create window, display is not running but in state %u",
992 wnd = malloc(sizeof(*wnd));
995 memset(wnd, 0, sizeof(*wnd));
999 shl_dlist_init(&wnd->widget_list);
1001 wnd->w_surface = wl_compositor_create_surface(disp->w_comp);
1002 if (!wnd->w_surface) {
1003 log_error("cannot create wayland surface");
1008 wnd->w_shell_surface = wl_shell_get_shell_surface(disp->w_shell,
1010 if (!wnd->w_shell_surface) {
1011 log_error("cannot retrieve shell-surface for wayland surface");
1016 wl_shell_surface_add_listener(wnd->w_shell_surface,
1017 &shell_surface_listener, wnd);
1018 wl_shell_surface_set_toplevel(wnd->w_shell_surface);
1020 ret = resize_window(wnd, width, height);
1022 goto err_shell_surface;
1024 wlt_display_ref(disp);
1025 shl_dlist_link(&disp->window_list, &wnd->list);
1030 wl_shell_surface_destroy(wnd->w_shell_surface);
1032 wl_surface_destroy(wnd->w_surface);
1038 void wlt_window_ref(struct wlt_window *wnd)
1040 if (!wnd || !wnd->ref)
1046 void wlt_window_unref(struct wlt_window *wnd)
1048 struct wlt_widget *widget;
1050 if (!wnd || !wnd->ref || --wnd->ref)
1053 while (!shl_dlist_empty(&wnd->widget_list)) {
1054 widget = shl_dlist_entry(wnd->widget_list.next,
1055 struct wlt_widget, list);
1056 wlt_widget_destroy(widget);
1059 if (wnd->close_pending)
1060 ev_eloop_unregister_idle_cb(wnd->disp->eloop, close_window,
1062 if (wnd->idle_pending)
1063 ev_eloop_unregister_idle_cb(wnd->disp->eloop, idle_frame, wnd);
1064 shl_dlist_unlink(&wnd->list);
1066 wl_callback_destroy(wnd->w_frame);
1068 wl_buffer_destroy(wnd->w_buffer);
1070 wlt_pool_free(wnd->pool);
1071 wl_shell_surface_destroy(wnd->w_shell_surface);
1072 wl_surface_destroy(wnd->w_surface);
1073 wlt_display_unref(wnd->disp);
1077 int wlt_window_create_widget(struct wlt_window *wnd,
1078 struct wlt_widget **out,
1081 struct wlt_widget *widget;
1086 widget = malloc(sizeof(*widget));
1089 memset(widget, 0, sizeof(*widget));
1091 widget->data = data;
1093 wlt_window_schedule_redraw(wnd);
1094 shl_dlist_link_tail(&wnd->widget_list, &widget->list);
1099 void wlt_window_schedule_redraw(struct wlt_window *wnd)
1104 wnd->need_redraw = true;
1105 schedule_frame(wnd);
1108 void wlt_window_damage(struct wlt_window *wnd,
1109 struct wlt_rect *damage)
1111 if (!wnd || !damage || wnd->skip_damage)
1114 wl_surface_damage(wnd->w_surface, damage->x, damage->y,
1115 damage->width, damage->height);
1118 void wlt_window_get_buffer(struct wlt_window *wnd,
1119 const struct wlt_rect *alloc,
1120 struct wlt_shm_buffer *buf)
1122 struct wlt_shm_buffer *rbuf;
1127 rbuf = &wnd->buffer;
1130 memcpy(buf, rbuf, sizeof(*buf));
1134 if (alloc->x >= rbuf->width ||
1135 alloc->y >= rbuf->height) {
1136 memset(buf, 0, sizeof(*buf));
1141 if (alloc->x + alloc->width > rbuf->width)
1142 buf->width = rbuf->width - alloc->x;
1144 buf->width = alloc->width;
1147 if (alloc->y + alloc->height > rbuf->height)
1148 buf->height = rbuf->height - alloc->y;
1150 buf->height = alloc->height;
1153 buf->stride = rbuf->stride;
1156 buf->data = rbuf->data;
1157 buf->data += alloc->y * rbuf->stride;
1158 buf->data += alloc->x * 4;
1161 void wlt_window_move(struct wlt_window *wnd)
1166 wl_shell_surface_move(wnd->w_shell_surface,
1168 wnd->disp->last_serial);
1171 void wlt_window_resize(struct wlt_window *wnd, uint32_t edges)
1176 wl_shell_surface_resize(wnd->w_shell_surface,
1178 wnd->disp->last_serial,
1182 int wlt_window_set_size(struct wlt_window *wnd,
1183 unsigned int width, unsigned int height)
1185 if (!wnd || !width || !height)
1188 wnd->new_width = width;
1189 wnd->new_height = height;
1190 wnd->need_resize = true;
1191 schedule_frame(wnd);
1196 void wlt_window_set_cursor(struct wlt_window *wnd, unsigned int cursor)
1201 set_cursor(wnd->disp, cursor);
1204 void wlt_window_set_close_cb(struct wlt_window *wnd,
1205 wlt_window_close_cb cb)
1213 void wlt_window_close(struct wlt_window *wnd)
1218 wnd->close_pending = true;
1219 ev_eloop_register_idle_cb(wnd->disp->eloop, close_window, wnd);
1222 struct ev_eloop *wlt_window_get_eloop(struct wlt_window *wnd)
1227 return wnd->disp->eloop;
1230 void wlt_widget_destroy(struct wlt_widget *widget)
1235 if (widget->destroy_cb)
1236 widget->destroy_cb(widget, widget->data);
1237 shl_dlist_unlink(&widget->list);
1241 struct wlt_window *wlt_widget_get_window(struct wlt_widget *widget)
1249 void wlt_widget_set_redraw_cb(struct wlt_widget *widget,
1250 wlt_widget_redraw_cb cb)
1255 widget->redraw_cb = cb;
1258 void wlt_widget_set_destroy_cb(struct wlt_widget *widget,
1259 wlt_widget_destroy_cb cb)
1264 widget->destroy_cb = cb;
1267 void wlt_widget_set_resize_cb(struct wlt_widget *widget,
1268 wlt_widget_prepare_resize_cb prepare_cb,
1269 wlt_widget_resize_cb cb)
1274 widget->prepare_resize_cb = prepare_cb;
1275 widget->resize_cb = cb;
1278 void wlt_widget_set_pointer_cb(struct wlt_widget *widget,
1279 wlt_widget_pointer_enter_cb enter_cb,
1280 wlt_widget_pointer_leave_cb leave_cb,
1281 wlt_widget_pointer_motion_cb motion_cb,
1282 wlt_widget_pointer_button_cb button_cb)
1287 widget->pointer_enter_cb = enter_cb;
1288 widget->pointer_leave_cb = leave_cb;
1289 widget->pointer_motion_cb = motion_cb;
1290 widget->pointer_button_cb = button_cb;