Kristian Høgsberg | ffd710e | 2008-12-02 15:15:01 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008 Kristian Høgsberg |
Pekka Paalanen | 4e37374 | 2013-02-13 16:17:13 +0200 | [diff] [blame] | 3 | * Copyright © 2012-2013 Collabora, Ltd. |
Kristian Høgsberg | ffd710e | 2008-12-02 15:15:01 -0500 | [diff] [blame] | 4 | * |
| 5 | * Permission to use, copy, modify, distribute, and sell this software and its |
| 6 | * documentation for any purpose is hereby granted without fee, provided that |
| 7 | * the above copyright notice appear in all copies and that both that copyright |
| 8 | * notice and this permission notice appear in supporting documentation, and |
| 9 | * that the name of the copyright holders not be used in advertising or |
| 10 | * publicity pertaining to distribution of the software without specific, |
| 11 | * written prior permission. The copyright holders make no representations |
| 12 | * about the suitability of this software for any purpose. It is provided "as |
| 13 | * is" without express or implied warranty. |
| 14 | * |
| 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 21 | * OF THIS SOFTWARE. |
| 22 | */ |
| 23 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 24 | #include "config.h" |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 25 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 26 | #include <stdint.h> |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
Pekka Paalanen | 7123388 | 2013-04-25 13:57:53 +0300 | [diff] [blame] | 29 | #include <stdarg.h> |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 30 | #include <string.h> |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 31 | #include <fcntl.h> |
| 32 | #include <unistd.h> |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 33 | #include <errno.h> |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 34 | #include <math.h> |
Benjamin Franzke | 0c99163 | 2011-09-27 21:57:31 +0200 | [diff] [blame] | 35 | #include <assert.h> |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 36 | #include <time.h> |
| 37 | #include <cairo.h> |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 38 | #include <sys/mman.h> |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 39 | #include <sys/epoll.h> |
Tiago Vignatti | 82db9d8 | 2012-05-23 22:06:27 +0300 | [diff] [blame] | 40 | #include <sys/timerfd.h> |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 41 | |
Pekka Paalanen | fb39d8d | 2012-10-16 17:27:19 +0300 | [diff] [blame] | 42 | #ifdef HAVE_CAIRO_EGL |
Kristian Høgsberg | 297d6dd | 2011-02-09 10:51:15 -0500 | [diff] [blame] | 43 | #include <wayland-egl.h> |
| 44 | |
Rob Clark | 6396ed3 | 2012-03-11 19:48:41 -0500 | [diff] [blame] | 45 | #ifdef USE_CAIRO_GLESV2 |
| 46 | #include <GLES2/gl2.h> |
| 47 | #include <GLES2/gl2ext.h> |
| 48 | #else |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 49 | #include <GL/gl.h> |
Rob Clark | 6396ed3 | 2012-03-11 19:48:41 -0500 | [diff] [blame] | 50 | #endif |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 51 | #include <EGL/egl.h> |
| 52 | #include <EGL/eglext.h> |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 53 | |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 54 | #include <cairo-gl.h> |
Pekka Paalanen | fb39d8d | 2012-10-16 17:27:19 +0300 | [diff] [blame] | 55 | #else /* HAVE_CAIRO_EGL */ |
| 56 | typedef void *EGLDisplay; |
| 57 | typedef void *EGLConfig; |
| 58 | typedef void *EGLContext; |
| 59 | #define EGL_NO_DISPLAY ((EGLDisplay)0) |
| 60 | #endif /* no HAVE_CAIRO_EGL */ |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 61 | |
Daniel Stone | 9d4f030 | 2012-02-15 16:33:21 +0000 | [diff] [blame] | 62 | #include <xkbcommon/xkbcommon.h> |
Ander Conselvan de Oliveira | 1042dc1 | 2012-05-22 15:39:42 +0300 | [diff] [blame] | 63 | #include <wayland-cursor.h> |
Kristian Høgsberg | 94adf6c | 2010-06-25 16:50:05 -0400 | [diff] [blame] | 64 | |
Kristian Høgsberg | 5ee1a60 | 2008-12-11 23:18:45 -0500 | [diff] [blame] | 65 | #include <linux/input.h> |
Pekka Paalanen | 50719bc | 2011-11-22 14:18:50 +0200 | [diff] [blame] | 66 | #include <wayland-client.h> |
Kristian Høgsberg | 5a315bc | 2012-05-15 22:33:43 -0400 | [diff] [blame] | 67 | #include "../shared/cairo-util.h" |
Scott Moreau | 7a1b32a | 2012-05-27 14:25:02 -0600 | [diff] [blame] | 68 | #include "text-cursor-position-client-protocol.h" |
Jonas Ådahl | 14c92ff | 2012-08-29 22:13:02 +0200 | [diff] [blame] | 69 | #include "workspaces-client-protocol.h" |
Pekka Paalanen | 647f2bf | 2012-05-30 15:53:43 +0300 | [diff] [blame] | 70 | #include "../shared/os-compatibility.h" |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 71 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 72 | #include "window.h" |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 73 | |
Ander Conselvan de Oliveira | 001de54 | 2012-05-07 11:34:26 -0700 | [diff] [blame] | 74 | struct shm_pool; |
Ander Conselvan de Oliveira | 1493d2a | 2012-05-03 12:29:46 +0300 | [diff] [blame] | 75 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 76 | struct global { |
| 77 | uint32_t name; |
| 78 | char *interface; |
| 79 | uint32_t version; |
| 80 | struct wl_list link; |
| 81 | }; |
| 82 | |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 83 | struct display { |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 84 | struct wl_display *display; |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 85 | struct wl_registry *registry; |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 86 | struct wl_compositor *compositor; |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 87 | struct wl_subcompositor *subcompositor; |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 88 | struct wl_shell *shell; |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 89 | struct wl_shm *shm; |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 90 | struct wl_data_device_manager *data_device_manager; |
Scott Moreau | 7a1b32a | 2012-05-27 14:25:02 -0600 | [diff] [blame] | 91 | struct text_cursor_position *text_cursor_position; |
Jonas Ådahl | 14c92ff | 2012-08-29 22:13:02 +0200 | [diff] [blame] | 92 | struct workspace_manager *workspace_manager; |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 93 | EGLDisplay dpy; |
Kristian Høgsberg | 8e81df4 | 2012-01-11 14:24:46 -0500 | [diff] [blame] | 94 | EGLConfig argb_config; |
Benjamin Franzke | 0c99163 | 2011-09-27 21:57:31 +0200 | [diff] [blame] | 95 | EGLContext argb_ctx; |
Benjamin Franzke | 0c99163 | 2011-09-27 21:57:31 +0200 | [diff] [blame] | 96 | cairo_device_t *argb_device; |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 97 | uint32_t serial; |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 98 | |
| 99 | int display_fd; |
U. Artie Eoff | 44874d9 | 2012-10-02 21:12:35 -0700 | [diff] [blame] | 100 | uint32_t display_fd_events; |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 101 | struct task display_task; |
| 102 | |
| 103 | int epoll_fd; |
| 104 | struct wl_list deferred_list; |
| 105 | |
Pekka Paalanen | 826d795 | 2011-12-15 10:14:07 +0200 | [diff] [blame] | 106 | int running; |
| 107 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 108 | struct wl_list global_list; |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 109 | struct wl_list window_list; |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 110 | struct wl_list input_list; |
Kristian Høgsberg | 53ff2f6 | 2011-11-26 17:27:37 -0500 | [diff] [blame] | 111 | struct wl_list output_list; |
Kristian Høgsberg | 291c69c | 2012-05-15 22:12:54 -0400 | [diff] [blame] | 112 | |
Kristian Høgsberg | 5adb480 | 2012-05-15 22:25:28 -0400 | [diff] [blame] | 113 | struct theme *theme; |
Kristian Høgsberg | 7016313 | 2012-05-08 15:55:39 -0400 | [diff] [blame] | 114 | |
Ander Conselvan de Oliveira | 1042dc1 | 2012-05-22 15:39:42 +0300 | [diff] [blame] | 115 | struct wl_cursor_theme *cursor_theme; |
Ander Conselvan de Oliveira | d8f527c | 2012-05-25 09:30:02 +0300 | [diff] [blame] | 116 | struct wl_cursor **cursors; |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 117 | |
Pekka Paalanen | 999c5b5 | 2011-11-30 10:52:38 +0200 | [diff] [blame] | 118 | display_output_handler_t output_configure_handler; |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 119 | display_global_handler_t global_handler; |
Xiong Zhang | 83d8ee7 | 2013-10-23 13:58:35 +0800 | [diff] [blame] | 120 | display_global_handler_t global_handler_remove; |
Pekka Paalanen | 999c5b5 | 2011-11-30 10:52:38 +0200 | [diff] [blame] | 121 | |
| 122 | void *user_data; |
Daniel Stone | 97f6854 | 2012-05-30 16:32:01 +0100 | [diff] [blame] | 123 | |
| 124 | struct xkb_context *xkb_context; |
Jonas Ådahl | 14c92ff | 2012-08-29 22:13:02 +0200 | [diff] [blame] | 125 | |
| 126 | uint32_t workspace; |
| 127 | uint32_t workspace_count; |
Pekka Paalanen | 3cbb089 | 2012-11-19 17:16:01 +0200 | [diff] [blame] | 128 | |
| 129 | /* A hack to get text extents for tooltips */ |
| 130 | cairo_surface_t *dummy_surface; |
| 131 | void *dummy_surface_data; |
Tomeu Vizoso | bee45a1 | 2013-08-06 20:05:54 +0200 | [diff] [blame] | 132 | |
| 133 | int has_rgb565; |
Rob Bradford | 0803118 | 2013-08-13 20:11:03 +0100 | [diff] [blame] | 134 | int seat_version; |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 135 | }; |
| 136 | |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 137 | enum { |
Kristian Høgsberg | 407ef64 | 2012-04-27 17:17:12 -0400 | [diff] [blame] | 138 | TYPE_NONE, |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 139 | TYPE_TOPLEVEL, |
Kristian Høgsberg | f8ab46e | 2011-09-08 16:56:38 -0400 | [diff] [blame] | 140 | TYPE_FULLSCREEN, |
Kristian Høgsberg | d6bcd7d | 2012-02-16 15:53:46 -0500 | [diff] [blame] | 141 | TYPE_MAXIMIZED, |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 142 | TYPE_TRANSIENT, |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 143 | TYPE_MENU, |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 144 | TYPE_CUSTOM |
| 145 | }; |
Rob Bradford | 7507b57 | 2012-05-15 17:55:34 +0100 | [diff] [blame] | 146 | |
| 147 | struct window_output { |
| 148 | struct output *output; |
| 149 | struct wl_list link; |
| 150 | }; |
| 151 | |
Pekka Paalanen | 03fc316 | 2012-11-19 17:15:58 +0200 | [diff] [blame] | 152 | struct toysurface { |
| 153 | /* |
| 154 | * Prepare the surface for drawing. Makes sure there is a surface |
| 155 | * of the right size available for rendering, and returns it. |
| 156 | * dx,dy are the x,y of wl_surface.attach. |
Alexander Larsson | 5e9b652 | 2013-05-22 14:41:28 +0200 | [diff] [blame] | 157 | * width,height are the new buffer size. |
Pekka Paalanen | ec07669 | 2012-11-30 13:37:27 +0200 | [diff] [blame] | 158 | * If flags has SURFACE_HINT_RESIZE set, the user is |
| 159 | * doing continuous resizing. |
Pekka Paalanen | 03fc316 | 2012-11-19 17:15:58 +0200 | [diff] [blame] | 160 | * Returns the Cairo surface to draw to. |
| 161 | */ |
| 162 | cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy, |
Alexander Larsson | 1818e31 | 2013-05-22 14:41:31 +0200 | [diff] [blame] | 163 | int32_t width, int32_t height, uint32_t flags, |
Alexander Larsson | edddbd1 | 2013-05-24 13:09:43 +0200 | [diff] [blame] | 164 | enum wl_output_transform buffer_transform, int32_t buffer_scale); |
Pekka Paalanen | 03fc316 | 2012-11-19 17:15:58 +0200 | [diff] [blame] | 165 | |
| 166 | /* |
| 167 | * Post the surface to the server, returning the server allocation |
| 168 | * rectangle. The Cairo surface from prepare() must be destroyed |
| 169 | * after calling this. |
| 170 | */ |
| 171 | void (*swap)(struct toysurface *base, |
Alexander Larsson | edddbd1 | 2013-05-24 13:09:43 +0200 | [diff] [blame] | 172 | enum wl_output_transform buffer_transform, int32_t buffer_scale, |
Pekka Paalanen | 03fc316 | 2012-11-19 17:15:58 +0200 | [diff] [blame] | 173 | struct rectangle *server_allocation); |
| 174 | |
| 175 | /* |
| 176 | * Make the toysurface current with the given EGL context. |
| 177 | * Returns 0 on success, and negative of failure. |
| 178 | */ |
| 179 | int (*acquire)(struct toysurface *base, EGLContext ctx); |
| 180 | |
| 181 | /* |
| 182 | * Release the toysurface from the EGL context, returning control |
| 183 | * to Cairo. |
| 184 | */ |
| 185 | void (*release)(struct toysurface *base); |
| 186 | |
| 187 | /* |
| 188 | * Destroy the toysurface, including the Cairo surface, any |
| 189 | * backing storage, and the Wayland protocol objects. |
| 190 | */ |
| 191 | void (*destroy)(struct toysurface *base); |
| 192 | }; |
| 193 | |
Pekka Paalanen | 4e37374 | 2013-02-13 16:17:13 +0200 | [diff] [blame] | 194 | struct surface { |
| 195 | struct window *window; |
| 196 | |
| 197 | struct wl_surface *surface; |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 198 | struct wl_subsurface *subsurface; |
| 199 | int synchronized; |
| 200 | int synchronized_default; |
Pekka Paalanen | 811ec4f | 2013-02-13 16:17:14 +0200 | [diff] [blame] | 201 | struct toysurface *toysurface; |
Pekka Paalanen | ac95f3e | 2013-02-13 16:17:17 +0200 | [diff] [blame] | 202 | struct widget *widget; |
Pekka Paalanen | 40cb67b | 2013-04-25 13:57:49 +0300 | [diff] [blame] | 203 | int redraw_needed; |
| 204 | struct wl_callback *frame_cb; |
Pekka Paalanen | 7ff7a80 | 2013-04-25 13:57:50 +0300 | [diff] [blame] | 205 | uint32_t last_time; |
Pekka Paalanen | 811ec4f | 2013-02-13 16:17:14 +0200 | [diff] [blame] | 206 | |
| 207 | struct rectangle allocation; |
| 208 | struct rectangle server_allocation; |
Pekka Paalanen | 02bba7c | 2013-02-13 16:17:15 +0200 | [diff] [blame] | 209 | |
Pekka Paalanen | b1cd9b1 | 2013-02-13 16:17:18 +0200 | [diff] [blame] | 210 | struct wl_region *input_region; |
| 211 | struct wl_region *opaque_region; |
| 212 | |
Pekka Paalanen | 02bba7c | 2013-02-13 16:17:15 +0200 | [diff] [blame] | 213 | enum window_buffer_type buffer_type; |
| 214 | enum wl_output_transform buffer_transform; |
Alexander Larsson | edddbd1 | 2013-05-24 13:09:43 +0200 | [diff] [blame] | 215 | int32_t buffer_scale; |
Pekka Paalanen | 89dee00 | 2013-02-13 16:17:20 +0200 | [diff] [blame] | 216 | |
| 217 | cairo_surface_t *cairo_surface; |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 218 | |
| 219 | struct wl_list link; |
Pekka Paalanen | 4e37374 | 2013-02-13 16:17:13 +0200 | [diff] [blame] | 220 | }; |
| 221 | |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 222 | struct window { |
| 223 | struct display *display; |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 224 | struct window *parent; |
Rob Bradford | 7507b57 | 2012-05-15 17:55:34 +0100 | [diff] [blame] | 225 | struct wl_list window_output_list; |
Kristian Høgsberg | d5fb9cc | 2011-01-25 12:45:37 -0500 | [diff] [blame] | 226 | char *title; |
Pekka Paalanen | 811ec4f | 2013-02-13 16:17:14 +0200 | [diff] [blame] | 227 | struct rectangle saved_allocation; |
Kristian Høgsberg | d3a1965 | 2012-07-20 11:32:51 -0400 | [diff] [blame] | 228 | struct rectangle min_allocation; |
Kristian Høgsberg | 0d1c062 | 2012-01-31 15:30:47 -0500 | [diff] [blame] | 229 | struct rectangle pending_allocation; |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 230 | int x, y; |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 231 | int resize_edges; |
Kristian Høgsberg | 6bd4d97 | 2012-03-24 14:42:09 -0400 | [diff] [blame] | 232 | int redraw_needed; |
Pekka Paalanen | 40cb67b | 2013-04-25 13:57:49 +0300 | [diff] [blame] | 233 | int redraw_task_scheduled; |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 234 | struct task redraw_task; |
Kristian Høgsberg | 42b4f80 | 2012-03-26 13:49:29 -0400 | [diff] [blame] | 235 | int resize_needed; |
Rafal Mielniczuk | c1a3cd1 | 2013-03-11 19:26:56 +0100 | [diff] [blame] | 236 | int saved_type; |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 237 | int type; |
Kristian Høgsberg | 86adef9 | 2012-08-13 22:25:53 -0400 | [diff] [blame] | 238 | int focus_count; |
| 239 | |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 240 | int resizing; |
Ander Conselvan de Oliveira | 5403f52 | 2012-12-14 13:37:23 -0200 | [diff] [blame] | 241 | int fullscreen_method; |
MoD | 063a882 | 2013-03-02 23:43:57 +0000 | [diff] [blame] | 242 | int configure_requests; |
Kristian Høgsberg | 1103a1a | 2012-04-03 12:00:48 -0400 | [diff] [blame] | 243 | |
Tomeu Vizoso | bee45a1 | 2013-08-06 20:05:54 +0200 | [diff] [blame] | 244 | enum preferred_format preferred_format; |
| 245 | |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 246 | window_key_handler_t key_handler; |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 247 | window_keyboard_focus_handler_t keyboard_focus_handler; |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 248 | window_data_handler_t data_handler; |
| 249 | window_drop_handler_t drop_handler; |
Kristian Høgsberg | 4f7dcd6 | 2012-01-06 21:59:05 -0500 | [diff] [blame] | 250 | window_close_handler_t close_handler; |
Kristian Høgsberg | 67ace20 | 2012-07-23 21:56:31 -0400 | [diff] [blame] | 251 | window_fullscreen_handler_t fullscreen_handler; |
Ander Conselvan de Oliveira | 15256f6 | 2012-11-30 17:34:25 +0200 | [diff] [blame] | 252 | window_output_handler_t output_handler; |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 253 | |
Pekka Paalanen | 4e37374 | 2013-02-13 16:17:13 +0200 | [diff] [blame] | 254 | struct surface *main_surface; |
| 255 | struct wl_shell_surface *shell_surface; |
Pekka Vuorela | 6e1e385 | 2012-09-17 22:15:57 +0300 | [diff] [blame] | 256 | |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 257 | struct window_frame *frame; |
Kristian Høgsberg | 4f7dcd6 | 2012-01-06 21:59:05 -0500 | [diff] [blame] | 258 | |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 259 | /* struct surface::link, contains also main_surface */ |
| 260 | struct wl_list subsurface_list; |
| 261 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 262 | void *user_data; |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 263 | struct wl_list link; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 264 | }; |
| 265 | |
Kristian Høgsberg | c51f799 | 2012-01-08 15:09:53 -0500 | [diff] [blame] | 266 | struct widget { |
Kristian Høgsberg | 9a13dab | 2012-01-08 15:18:19 -0500 | [diff] [blame] | 267 | struct window *window; |
Pekka Paalanen | 2d8a32a | 2013-02-13 16:17:19 +0200 | [diff] [blame] | 268 | struct surface *surface; |
Tiago Vignatti | 82db9d8 | 2012-05-23 22:06:27 +0300 | [diff] [blame] | 269 | struct tooltip *tooltip; |
Kristian Høgsberg | 441338c | 2012-01-10 13:52:34 -0500 | [diff] [blame] | 270 | struct wl_list child_list; |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 271 | struct wl_list link; |
| 272 | struct rectangle allocation; |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 273 | widget_resize_handler_t resize_handler; |
| 274 | widget_redraw_handler_t redraw_handler; |
Kristian Høgsberg | ee14323 | 2012-01-09 08:42:24 -0500 | [diff] [blame] | 275 | widget_enter_handler_t enter_handler; |
| 276 | widget_leave_handler_t leave_handler; |
Kristian Høgsberg | 04e9834 | 2012-01-09 09:36:16 -0500 | [diff] [blame] | 277 | widget_motion_handler_t motion_handler; |
Kristian Høgsberg | a8a0db3 | 2012-01-09 11:12:05 -0500 | [diff] [blame] | 278 | widget_button_handler_t button_handler; |
Rusty Lynch | 041815a | 2013-08-08 21:20:38 -0700 | [diff] [blame] | 279 | widget_touch_down_handler_t touch_down_handler; |
| 280 | widget_touch_up_handler_t touch_up_handler; |
| 281 | widget_touch_motion_handler_t touch_motion_handler; |
| 282 | widget_touch_frame_handler_t touch_frame_handler; |
| 283 | widget_touch_cancel_handler_t touch_cancel_handler; |
Philipp Brüschweiler | 7e0cc54 | 2012-08-14 11:02:41 +0200 | [diff] [blame] | 284 | widget_axis_handler_t axis_handler; |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 285 | void *user_data; |
Kristian Høgsberg | 010f98b | 2012-02-23 17:30:45 -0500 | [diff] [blame] | 286 | int opaque; |
Tiago Vignatti | 82db9d8 | 2012-05-23 22:06:27 +0300 | [diff] [blame] | 287 | int tooltip_count; |
Kristian Høgsberg | bf74d52 | 2012-11-30 14:54:35 -0500 | [diff] [blame] | 288 | int default_cursor; |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 289 | }; |
| 290 | |
Rusty Lynch | 041815a | 2013-08-08 21:20:38 -0700 | [diff] [blame] | 291 | struct touch_point { |
| 292 | int32_t id; |
| 293 | struct widget *widget; |
| 294 | struct wl_list link; |
| 295 | }; |
| 296 | |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 297 | struct input { |
| 298 | struct display *display; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 299 | struct wl_seat *seat; |
| 300 | struct wl_pointer *pointer; |
| 301 | struct wl_keyboard *keyboard; |
Rusty Lynch | 041815a | 2013-08-08 21:20:38 -0700 | [diff] [blame] | 302 | struct wl_touch *touch; |
| 303 | struct wl_list touch_point_list; |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 304 | struct window *pointer_focus; |
| 305 | struct window *keyboard_focus; |
Rusty Lynch | 041815a | 2013-08-08 21:20:38 -0700 | [diff] [blame] | 306 | struct window *touch_focus; |
Ander Conselvan de Oliveira | 1493d2a | 2012-05-03 12:29:46 +0300 | [diff] [blame] | 307 | int current_cursor; |
Ander Conselvan de Oliveira | 8062007 | 2012-06-15 17:27:36 +0300 | [diff] [blame] | 308 | uint32_t cursor_anim_start; |
| 309 | struct wl_callback *cursor_frame_cb; |
Ander Conselvan de Oliveira | 37ffc3c | 2012-06-15 17:27:35 +0300 | [diff] [blame] | 310 | struct wl_surface *pointer_surface; |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 311 | uint32_t modifiers; |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 312 | uint32_t pointer_enter_serial; |
Kristian Høgsberg | 11f600d | 2012-06-22 10:52:58 -0400 | [diff] [blame] | 313 | uint32_t cursor_serial; |
Kristian Høgsberg | 80680c7 | 2012-05-10 12:21:37 -0400 | [diff] [blame] | 314 | float sx, sy; |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 315 | struct wl_list link; |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 316 | |
Kristian Høgsberg | b632351 | 2012-01-11 00:04:42 -0500 | [diff] [blame] | 317 | struct widget *focus_widget; |
Kristian Høgsberg | 831dd52 | 2012-01-10 23:46:33 -0500 | [diff] [blame] | 318 | struct widget *grab; |
| 319 | uint32_t grab_button; |
| 320 | |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 321 | struct wl_data_device *data_device; |
| 322 | struct data_offer *drag_offer; |
| 323 | struct data_offer *selection_offer; |
Daniel Stone | 97f6854 | 2012-05-30 16:32:01 +0100 | [diff] [blame] | 324 | |
| 325 | struct { |
Daniel Stone | 97f6854 | 2012-05-30 16:32:01 +0100 | [diff] [blame] | 326 | struct xkb_keymap *keymap; |
| 327 | struct xkb_state *state; |
| 328 | xkb_mod_mask_t control_mask; |
| 329 | xkb_mod_mask_t alt_mask; |
| 330 | xkb_mod_mask_t shift_mask; |
| 331 | } xkb; |
Kristian Høgsberg | cf4d244 | 2012-06-20 14:52:12 -0400 | [diff] [blame] | 332 | |
| 333 | struct task repeat_task; |
| 334 | int repeat_timer_fd; |
| 335 | uint32_t repeat_sym; |
| 336 | uint32_t repeat_key; |
| 337 | uint32_t repeat_time; |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 338 | }; |
| 339 | |
Kristian Høgsberg | 53ff2f6 | 2011-11-26 17:27:37 -0500 | [diff] [blame] | 340 | struct output { |
| 341 | struct display *display; |
| 342 | struct wl_output *output; |
Xiong Zhang | 83d8ee7 | 2013-10-23 13:58:35 +0800 | [diff] [blame] | 343 | uint32_t server_output_id; |
Kristian Høgsberg | 53ff2f6 | 2011-11-26 17:27:37 -0500 | [diff] [blame] | 344 | struct rectangle allocation; |
| 345 | struct wl_list link; |
Scott Moreau | 4e07236 | 2012-09-29 02:03:11 -0600 | [diff] [blame] | 346 | int transform; |
Alexander Larsson | afd319a | 2013-05-22 14:41:27 +0200 | [diff] [blame] | 347 | int scale; |
Pekka Paalanen | 999c5b5 | 2011-11-30 10:52:38 +0200 | [diff] [blame] | 348 | |
| 349 | display_output_handler_t destroy_handler; |
| 350 | void *user_data; |
Kristian Høgsberg | 53ff2f6 | 2011-11-26 17:27:37 -0500 | [diff] [blame] | 351 | }; |
| 352 | |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 353 | struct window_frame { |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 354 | struct widget *widget; |
| 355 | struct widget *child; |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 356 | struct frame *frame; |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 357 | }; |
| 358 | |
Kristian Høgsberg | 4f7dcd6 | 2012-01-06 21:59:05 -0500 | [diff] [blame] | 359 | struct menu { |
| 360 | struct window *window; |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 361 | struct widget *widget; |
Kristian Høgsberg | 831dd52 | 2012-01-10 23:46:33 -0500 | [diff] [blame] | 362 | struct input *input; |
Kristian Høgsberg | c680e90 | 2013-10-23 21:49:30 -0700 | [diff] [blame] | 363 | struct frame *frame; |
Kristian Høgsberg | 4f7dcd6 | 2012-01-06 21:59:05 -0500 | [diff] [blame] | 364 | const char **entries; |
| 365 | uint32_t time; |
| 366 | int current; |
| 367 | int count; |
Kristian Høgsberg | d2fbb38 | 2012-10-30 13:45:22 -0400 | [diff] [blame] | 368 | int release_count; |
Kristian Høgsberg | 4f7dcd6 | 2012-01-06 21:59:05 -0500 | [diff] [blame] | 369 | menu_func_t func; |
| 370 | }; |
| 371 | |
Tiago Vignatti | 82db9d8 | 2012-05-23 22:06:27 +0300 | [diff] [blame] | 372 | struct tooltip { |
| 373 | struct widget *parent; |
| 374 | struct window *window; |
| 375 | struct widget *widget; |
| 376 | char *entry; |
| 377 | struct task tooltip_task; |
| 378 | int tooltip_fd; |
| 379 | float x, y; |
| 380 | }; |
| 381 | |
Ander Conselvan de Oliveira | 001de54 | 2012-05-07 11:34:26 -0700 | [diff] [blame] | 382 | struct shm_pool { |
| 383 | struct wl_shm_pool *pool; |
| 384 | size_t size; |
| 385 | size_t used; |
| 386 | void *data; |
| 387 | }; |
| 388 | |
Kristian Høgsberg | 7d80406 | 2010-09-07 21:50:06 -0400 | [diff] [blame] | 389 | enum { |
Ander Conselvan de Oliveira | dc8c8fc | 2012-05-25 16:01:41 +0300 | [diff] [blame] | 390 | CURSOR_DEFAULT = 100, |
| 391 | CURSOR_UNSET |
Kristian Høgsberg | 7d80406 | 2010-09-07 21:50:06 -0400 | [diff] [blame] | 392 | }; |
| 393 | |
Kristian Høgsberg | 82da52b | 2010-12-17 09:53:12 -0500 | [diff] [blame] | 394 | enum window_location { |
| 395 | WINDOW_INTERIOR = 0, |
| 396 | WINDOW_RESIZING_TOP = 1, |
| 397 | WINDOW_RESIZING_BOTTOM = 2, |
| 398 | WINDOW_RESIZING_LEFT = 4, |
| 399 | WINDOW_RESIZING_TOP_LEFT = 5, |
| 400 | WINDOW_RESIZING_BOTTOM_LEFT = 6, |
| 401 | WINDOW_RESIZING_RIGHT = 8, |
| 402 | WINDOW_RESIZING_TOP_RIGHT = 9, |
| 403 | WINDOW_RESIZING_BOTTOM_RIGHT = 10, |
| 404 | WINDOW_RESIZING_MASK = 15, |
| 405 | WINDOW_EXTERIOR = 16, |
| 406 | WINDOW_TITLEBAR = 17, |
| 407 | WINDOW_CLIENT_AREA = 18, |
| 408 | }; |
| 409 | |
Pekka Paalanen | 32127ca | 2012-11-19 15:32:51 +0200 | [diff] [blame] | 410 | static const cairo_user_data_key_t shm_surface_data_key; |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 411 | |
Pekka Paalanen | 9777744 | 2013-05-22 10:20:05 +0300 | [diff] [blame] | 412 | /* #define DEBUG */ |
| 413 | |
| 414 | #ifdef DEBUG |
Pekka Paalanen | 7123388 | 2013-04-25 13:57:53 +0300 | [diff] [blame] | 415 | |
| 416 | static void |
| 417 | debug_print(void *proxy, int line, const char *func, const char *fmt, ...) |
| 418 | __attribute__ ((format (printf, 4, 5))); |
| 419 | |
| 420 | static void |
| 421 | debug_print(void *proxy, int line, const char *func, const char *fmt, ...) |
| 422 | { |
| 423 | va_list ap; |
| 424 | struct timeval tv; |
| 425 | |
| 426 | gettimeofday(&tv, NULL); |
| 427 | fprintf(stderr, "%8ld.%03ld ", |
| 428 | (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000); |
| 429 | |
| 430 | if (proxy) |
| 431 | fprintf(stderr, "%s@%d ", |
| 432 | wl_proxy_get_class(proxy), wl_proxy_get_id(proxy)); |
| 433 | |
| 434 | /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/ |
| 435 | fprintf(stderr, "%s ", func); |
| 436 | |
| 437 | va_start(ap, fmt); |
| 438 | vfprintf(stderr, fmt, ap); |
| 439 | va_end(ap); |
| 440 | } |
| 441 | |
| 442 | #define DBG(fmt, ...) \ |
| 443 | debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__) |
| 444 | |
| 445 | #define DBG_OBJ(obj, fmt, ...) \ |
| 446 | debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__) |
| 447 | |
| 448 | #else |
| 449 | |
| 450 | #define DBG(...) do {} while (0) |
| 451 | #define DBG_OBJ(...) do {} while (0) |
| 452 | |
| 453 | #endif |
| 454 | |
Alexander Larsson | 1818e31 | 2013-05-22 14:41:31 +0200 | [diff] [blame] | 455 | static void |
Alexander Larsson | edddbd1 | 2013-05-24 13:09:43 +0200 | [diff] [blame] | 456 | surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height) |
Alexander Larsson | 1818e31 | 2013-05-22 14:41:31 +0200 | [diff] [blame] | 457 | { |
| 458 | int32_t tmp; |
| 459 | |
| 460 | switch (buffer_transform) { |
| 461 | case WL_OUTPUT_TRANSFORM_90: |
| 462 | case WL_OUTPUT_TRANSFORM_270: |
| 463 | case WL_OUTPUT_TRANSFORM_FLIPPED_90: |
| 464 | case WL_OUTPUT_TRANSFORM_FLIPPED_270: |
| 465 | tmp = *width; |
| 466 | *width = *height; |
| 467 | *height = tmp; |
| 468 | break; |
| 469 | default: |
| 470 | break; |
| 471 | } |
| 472 | |
| 473 | *width *= buffer_scale; |
| 474 | *height *= buffer_scale; |
| 475 | } |
| 476 | |
| 477 | static void |
Alexander Larsson | edddbd1 | 2013-05-24 13:09:43 +0200 | [diff] [blame] | 478 | buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height) |
Alexander Larsson | 1818e31 | 2013-05-22 14:41:31 +0200 | [diff] [blame] | 479 | { |
| 480 | int32_t tmp; |
| 481 | |
| 482 | switch (buffer_transform) { |
| 483 | case WL_OUTPUT_TRANSFORM_90: |
| 484 | case WL_OUTPUT_TRANSFORM_270: |
| 485 | case WL_OUTPUT_TRANSFORM_FLIPPED_90: |
| 486 | case WL_OUTPUT_TRANSFORM_FLIPPED_270: |
| 487 | tmp = *width; |
| 488 | *width = *height; |
| 489 | *height = tmp; |
| 490 | break; |
| 491 | default: |
| 492 | break; |
| 493 | } |
| 494 | |
| 495 | *width /= buffer_scale; |
| 496 | *height /= buffer_scale; |
| 497 | } |
| 498 | |
Kristian Høgsberg | 8def264 | 2011-01-14 17:41:33 -0500 | [diff] [blame] | 499 | #ifdef HAVE_CAIRO_EGL |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 500 | |
Pekka Paalanen | 03fc316 | 2012-11-19 17:15:58 +0200 | [diff] [blame] | 501 | struct egl_window_surface { |
| 502 | struct toysurface base; |
| 503 | cairo_surface_t *cairo_surface; |
Benjamin Franzke | 6693ac2 | 2011-02-10 12:04:30 +0100 | [diff] [blame] | 504 | struct display *display; |
| 505 | struct wl_surface *surface; |
Pekka Paalanen | 03fc316 | 2012-11-19 17:15:58 +0200 | [diff] [blame] | 506 | struct wl_egl_window *egl_window; |
| 507 | EGLSurface egl_surface; |
Benjamin Franzke | 6693ac2 | 2011-02-10 12:04:30 +0100 | [diff] [blame] | 508 | }; |
| 509 | |
Pekka Paalanen | 03fc316 | 2012-11-19 17:15:58 +0200 | [diff] [blame] | 510 | static struct egl_window_surface * |
| 511 | to_egl_window_surface(struct toysurface *base) |
Benjamin Franzke | 6693ac2 | 2011-02-10 12:04:30 +0100 | [diff] [blame] | 512 | { |
Pekka Paalanen | 03fc316 | 2012-11-19 17:15:58 +0200 | [diff] [blame] | 513 | return container_of(base, struct egl_window_surface, base); |
Benjamin Franzke | 6693ac2 | 2011-02-10 12:04:30 +0100 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | static cairo_surface_t * |
Pekka Paalanen | 03fc316 | 2012-11-19 17:15:58 +0200 | [diff] [blame] | 517 | egl_window_surface_prepare(struct toysurface *base, int dx, int dy, |
Alexander Larsson | 1818e31 | 2013-05-22 14:41:31 +0200 | [diff] [blame] | 518 | int32_t width, int32_t height, uint32_t flags, |
Alexander Larsson | edddbd1 | 2013-05-24 13:09:43 +0200 | [diff] [blame] | 519 | enum wl_output_transform buffer_transform, int32_t buffer_scale) |
Benjamin Franzke | 6693ac2 | 2011-02-10 12:04:30 +0100 | [diff] [blame] | 520 | { |
Pekka Paalanen | 03fc316 | 2012-11-19 17:15:58 +0200 | [diff] [blame] | 521 | struct egl_window_surface *surface = to_egl_window_surface(base); |
| 522 | |
Alexander Larsson | 1818e31 | 2013-05-22 14:41:31 +0200 | [diff] [blame] | 523 | surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height); |
| 524 | |
Pekka Paalanen | 03fc316 | 2012-11-19 17:15:58 +0200 | [diff] [blame] | 525 | wl_egl_window_resize(surface->egl_window, width, height, dx, dy); |
| 526 | cairo_gl_surface_set_size(surface->cairo_surface, width, height); |
| 527 | |
| 528 | return cairo_surface_reference(surface->cairo_surface); |
| 529 | } |
| 530 | |
| 531 | static void |
| 532 | egl_window_surface_swap(struct toysurface *base, |
Alexander Larsson | edddbd1 | 2013-05-24 13:09:43 +0200 | [diff] [blame] | 533 | enum wl_output_transform buffer_transform, int32_t buffer_scale, |
Pekka Paalanen | 03fc316 | 2012-11-19 17:15:58 +0200 | [diff] [blame] | 534 | struct rectangle *server_allocation) |
| 535 | { |
| 536 | struct egl_window_surface *surface = to_egl_window_surface(base); |
| 537 | |
| 538 | cairo_gl_surface_swapbuffers(surface->cairo_surface); |
| 539 | wl_egl_window_get_attached_size(surface->egl_window, |
| 540 | &server_allocation->width, |
| 541 | &server_allocation->height); |
Alexander Larsson | 1818e31 | 2013-05-22 14:41:31 +0200 | [diff] [blame] | 542 | |
| 543 | buffer_to_surface_size (buffer_transform, buffer_scale, |
| 544 | &server_allocation->width, |
| 545 | &server_allocation->height); |
Pekka Paalanen | 03fc316 | 2012-11-19 17:15:58 +0200 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | static int |
| 549 | egl_window_surface_acquire(struct toysurface *base, EGLContext ctx) |
| 550 | { |
| 551 | struct egl_window_surface *surface = to_egl_window_surface(base); |
Benjamin Franzke | 0c99163 | 2011-09-27 21:57:31 +0200 | [diff] [blame] | 552 | cairo_device_t *device; |
Benjamin Franzke | 6693ac2 | 2011-02-10 12:04:30 +0100 | [diff] [blame] | 553 | |
Pekka Paalanen | 03fc316 | 2012-11-19 17:15:58 +0200 | [diff] [blame] | 554 | device = cairo_surface_get_device(surface->cairo_surface); |
| 555 | if (!device) |
| 556 | return -1; |
| 557 | |
| 558 | if (!ctx) { |
| 559 | if (device == surface->display->argb_device) |
| 560 | ctx = surface->display->argb_ctx; |
| 561 | else |
| 562 | assert(0); |
| 563 | } |
| 564 | |
| 565 | cairo_device_flush(device); |
| 566 | cairo_device_acquire(device); |
| 567 | if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface, |
| 568 | surface->egl_surface, ctx)) |
| 569 | fprintf(stderr, "failed to make surface current\n"); |
| 570 | |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | static void |
| 575 | egl_window_surface_release(struct toysurface *base) |
| 576 | { |
| 577 | struct egl_window_surface *surface = to_egl_window_surface(base); |
| 578 | cairo_device_t *device; |
| 579 | |
| 580 | device = cairo_surface_get_device(surface->cairo_surface); |
| 581 | if (!device) |
| 582 | return; |
| 583 | |
| 584 | if (!eglMakeCurrent(surface->display->dpy, NULL, NULL, |
| 585 | surface->display->argb_ctx)) |
| 586 | fprintf(stderr, "failed to make context current\n"); |
| 587 | |
| 588 | cairo_device_release(device); |
| 589 | } |
| 590 | |
| 591 | static void |
| 592 | egl_window_surface_destroy(struct toysurface *base) |
| 593 | { |
| 594 | struct egl_window_surface *surface = to_egl_window_surface(base); |
| 595 | struct display *d = surface->display; |
| 596 | |
| 597 | cairo_surface_destroy(surface->cairo_surface); |
| 598 | eglDestroySurface(d->dpy, surface->egl_surface); |
| 599 | wl_egl_window_destroy(surface->egl_window); |
| 600 | surface->surface = NULL; |
| 601 | |
| 602 | free(surface); |
| 603 | } |
| 604 | |
| 605 | static struct toysurface * |
| 606 | egl_window_surface_create(struct display *display, |
| 607 | struct wl_surface *wl_surface, |
| 608 | uint32_t flags, |
| 609 | struct rectangle *rectangle) |
| 610 | { |
| 611 | struct egl_window_surface *surface; |
| 612 | |
Pekka Paalanen | b362736 | 2012-11-19 17:16:00 +0200 | [diff] [blame] | 613 | if (display->dpy == EGL_NO_DISPLAY) |
| 614 | return NULL; |
| 615 | |
Pekka Paalanen | 03fc316 | 2012-11-19 17:15:58 +0200 | [diff] [blame] | 616 | surface = calloc(1, sizeof *surface); |
| 617 | if (!surface) |
Benjamin Franzke | 6693ac2 | 2011-02-10 12:04:30 +0100 | [diff] [blame] | 618 | return NULL; |
| 619 | |
Pekka Paalanen | 03fc316 | 2012-11-19 17:15:58 +0200 | [diff] [blame] | 620 | surface->base.prepare = egl_window_surface_prepare; |
| 621 | surface->base.swap = egl_window_surface_swap; |
| 622 | surface->base.acquire = egl_window_surface_acquire; |
| 623 | surface->base.release = egl_window_surface_release; |
| 624 | surface->base.destroy = egl_window_surface_destroy; |
Benjamin Franzke | 6693ac2 | 2011-02-10 12:04:30 +0100 | [diff] [blame] | 625 | |
Pekka Paalanen | 03fc316 | 2012-11-19 17:15:58 +0200 | [diff] [blame] | 626 | surface->display = display; |
| 627 | surface->surface = wl_surface; |
Kristian Høgsberg | f389cac | 2011-08-31 16:21:38 -0400 | [diff] [blame] | 628 | |
Pekka Paalanen | 03fc316 | 2012-11-19 17:15:58 +0200 | [diff] [blame] | 629 | surface->egl_window = wl_egl_window_create(surface->surface, |
| 630 | rectangle->width, |
| 631 | rectangle->height); |
Benjamin Franzke | 6693ac2 | 2011-02-10 12:04:30 +0100 | [diff] [blame] | 632 | |
Pekka Paalanen | 03fc316 | 2012-11-19 17:15:58 +0200 | [diff] [blame] | 633 | surface->egl_surface = eglCreateWindowSurface(display->dpy, |
| 634 | display->argb_config, |
| 635 | surface->egl_window, |
| 636 | NULL); |
Benjamin Franzke | 6693ac2 | 2011-02-10 12:04:30 +0100 | [diff] [blame] | 637 | |
Pekka Paalanen | 03fc316 | 2012-11-19 17:15:58 +0200 | [diff] [blame] | 638 | surface->cairo_surface = |
| 639 | cairo_gl_surface_create_for_egl(display->argb_device, |
| 640 | surface->egl_surface, |
| 641 | rectangle->width, |
| 642 | rectangle->height); |
Benjamin Franzke | 6693ac2 | 2011-02-10 12:04:30 +0100 | [diff] [blame] | 643 | |
Pekka Paalanen | 03fc316 | 2012-11-19 17:15:58 +0200 | [diff] [blame] | 644 | return &surface->base; |
Benjamin Franzke | 6693ac2 | 2011-02-10 12:04:30 +0100 | [diff] [blame] | 645 | } |
| 646 | |
Pekka Paalanen | b362736 | 2012-11-19 17:16:00 +0200 | [diff] [blame] | 647 | #else |
| 648 | |
| 649 | static struct toysurface * |
| 650 | egl_window_surface_create(struct display *display, |
| 651 | struct wl_surface *wl_surface, |
| 652 | uint32_t flags, |
| 653 | struct rectangle *rectangle) |
| 654 | { |
| 655 | return NULL; |
| 656 | } |
| 657 | |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 658 | #endif |
| 659 | |
Pekka Paalanen | 0c6a343 | 2012-11-19 15:32:50 +0200 | [diff] [blame] | 660 | struct shm_surface_data { |
| 661 | struct wl_buffer *buffer; |
| 662 | struct shm_pool *pool; |
| 663 | }; |
| 664 | |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 665 | struct wl_buffer * |
| 666 | display_get_buffer_for_surface(struct display *display, |
| 667 | cairo_surface_t *surface) |
| 668 | { |
Pekka Paalanen | 0c6a343 | 2012-11-19 15:32:50 +0200 | [diff] [blame] | 669 | struct shm_surface_data *data; |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 670 | |
Pekka Paalanen | 32127ca | 2012-11-19 15:32:51 +0200 | [diff] [blame] | 671 | data = cairo_surface_get_user_data(surface, &shm_surface_data_key); |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 672 | |
| 673 | return data->buffer; |
| 674 | } |
| 675 | |
Kristian Høgsberg | 06bc264 | 2010-12-01 09:50:16 -0500 | [diff] [blame] | 676 | static void |
Ander Conselvan de Oliveira | 001de54 | 2012-05-07 11:34:26 -0700 | [diff] [blame] | 677 | shm_pool_destroy(struct shm_pool *pool); |
| 678 | |
| 679 | static void |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 680 | shm_surface_data_destroy(void *p) |
| 681 | { |
| 682 | struct shm_surface_data *data = p; |
| 683 | |
Pekka Paalanen | 0c6a343 | 2012-11-19 15:32:50 +0200 | [diff] [blame] | 684 | wl_buffer_destroy(data->buffer); |
Ander Conselvan de Oliveira | 001de54 | 2012-05-07 11:34:26 -0700 | [diff] [blame] | 685 | if (data->pool) |
| 686 | shm_pool_destroy(data->pool); |
Ander Conselvan de Oliveira | 2a3cd28 | 2012-06-19 13:45:55 +0300 | [diff] [blame] | 687 | |
| 688 | free(data); |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 689 | } |
| 690 | |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 691 | static struct wl_shm_pool * |
| 692 | make_shm_pool(struct display *display, int size, void **data) |
| 693 | { |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 694 | struct wl_shm_pool *pool; |
| 695 | int fd; |
| 696 | |
Pekka Paalanen | 1da1b8f | 2012-06-06 16:59:43 +0300 | [diff] [blame] | 697 | fd = os_create_anonymous_file(size); |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 698 | if (fd < 0) { |
Pekka Paalanen | 1da1b8f | 2012-06-06 16:59:43 +0300 | [diff] [blame] | 699 | fprintf(stderr, "creating a buffer file for %d B failed: %m\n", |
| 700 | size); |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 701 | return NULL; |
| 702 | } |
| 703 | |
| 704 | *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
Kristian Høgsberg | 1662628 | 2012-04-03 11:21:27 -0400 | [diff] [blame] | 705 | if (*data == MAP_FAILED) { |
| 706 | fprintf(stderr, "mmap failed: %m\n"); |
| 707 | close(fd); |
| 708 | return NULL; |
| 709 | } |
| 710 | |
| 711 | pool = wl_shm_create_pool(display->shm, fd, size); |
| 712 | |
| 713 | close(fd); |
| 714 | |
| 715 | return pool; |
| 716 | } |
| 717 | |
Ander Conselvan de Oliveira | 001de54 | 2012-05-07 11:34:26 -0700 | [diff] [blame] | 718 | static struct shm_pool * |
| 719 | shm_pool_create(struct display *display, size_t size) |
| 720 | { |
| 721 | struct shm_pool *pool = malloc(sizeof *pool); |
| 722 | |
| 723 | if (!pool) |
| 724 | return NULL; |
| 725 | |
| 726 | pool->pool = make_shm_pool(display, size, &pool->data); |
| 727 | if (!pool->pool) { |
| 728 | free(pool); |
| 729 | return NULL; |
| 730 | } |
| 731 | |
| 732 | pool->size = size; |
| 733 | pool->used = 0; |
| 734 | |
| 735 | return pool; |
| 736 | } |
| 737 | |
| 738 | static void * |
| 739 | shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset) |
| 740 | { |
| 741 | if (pool->used + size > pool->size) |
| 742 | return NULL; |
| 743 | |
| 744 | *offset = pool->used; |
| 745 | pool->used += size; |
| 746 | |
| 747 | return (char *) pool->data + *offset; |
| 748 | } |
| 749 | |
| 750 | /* destroy the pool. this does not unmap the memory though */ |
| 751 | static void |
| 752 | shm_pool_destroy(struct shm_pool *pool) |
| 753 | { |
| 754 | munmap(pool->data, pool->size); |
| 755 | wl_shm_pool_destroy(pool->pool); |
| 756 | free(pool); |
| 757 | } |
| 758 | |
| 759 | /* Start allocating from the beginning of the pool again */ |
| 760 | static void |
| 761 | shm_pool_reset(struct shm_pool *pool) |
| 762 | { |
| 763 | pool->used = 0; |
| 764 | } |
| 765 | |
| 766 | static int |
| 767 | data_length_for_shm_surface(struct rectangle *rect) |
| 768 | { |
| 769 | int stride; |
| 770 | |
| 771 | stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, |
| 772 | rect->width); |
| 773 | return stride * rect->height; |
| 774 | } |
| 775 | |
Kristian Høgsberg | 06bc264 | 2010-12-01 09:50:16 -0500 | [diff] [blame] | 776 | static cairo_surface_t * |
Ander Conselvan de Oliveira | 001de54 | 2012-05-07 11:34:26 -0700 | [diff] [blame] | 777 | display_create_shm_surface_from_pool(struct display *display, |
| 778 | struct rectangle *rectangle, |
| 779 | uint32_t flags, struct shm_pool *pool) |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 780 | { |
| 781 | struct shm_surface_data *data; |
Kristian Høgsberg | f389cac | 2011-08-31 16:21:38 -0400 | [diff] [blame] | 782 | uint32_t format; |
Kristian Høgsberg | 3be87d1 | 2011-05-13 13:45:17 -0400 | [diff] [blame] | 783 | cairo_surface_t *surface; |
Tomeu Vizoso | bee45a1 | 2013-08-06 20:05:54 +0200 | [diff] [blame] | 784 | cairo_format_t cairo_format; |
Ander Conselvan de Oliveira | 001de54 | 2012-05-07 11:34:26 -0700 | [diff] [blame] | 785 | int stride, length, offset; |
Kristian Høgsberg | 1103a1a | 2012-04-03 12:00:48 -0400 | [diff] [blame] | 786 | void *map; |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 787 | |
| 788 | data = malloc(sizeof *data); |
| 789 | if (data == NULL) |
| 790 | return NULL; |
| 791 | |
Tomeu Vizoso | bee45a1 | 2013-08-06 20:05:54 +0200 | [diff] [blame] | 792 | if (flags & SURFACE_HINT_RGB565 && display->has_rgb565) |
| 793 | cairo_format = CAIRO_FORMAT_RGB16_565; |
| 794 | else |
| 795 | cairo_format = CAIRO_FORMAT_ARGB32; |
| 796 | |
| 797 | stride = cairo_format_stride_for_width (cairo_format, rectangle->width); |
Ander Conselvan de Oliveira | 001de54 | 2012-05-07 11:34:26 -0700 | [diff] [blame] | 798 | length = stride * rectangle->height; |
| 799 | data->pool = NULL; |
| 800 | map = shm_pool_allocate(pool, length, &offset); |
| 801 | |
| 802 | if (!map) { |
| 803 | free(data); |
| 804 | return NULL; |
Kristian Høgsberg | 1103a1a | 2012-04-03 12:00:48 -0400 | [diff] [blame] | 805 | } |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 806 | |
Kristian Høgsberg | 1103a1a | 2012-04-03 12:00:48 -0400 | [diff] [blame] | 807 | surface = cairo_image_surface_create_for_data (map, |
Tomeu Vizoso | bee45a1 | 2013-08-06 20:05:54 +0200 | [diff] [blame] | 808 | cairo_format, |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 809 | rectangle->width, |
| 810 | rectangle->height, |
| 811 | stride); |
| 812 | |
Pekka Paalanen | 32127ca | 2012-11-19 15:32:51 +0200 | [diff] [blame] | 813 | cairo_surface_set_user_data(surface, &shm_surface_data_key, |
| 814 | data, shm_surface_data_destroy); |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 815 | |
Tomeu Vizoso | bee45a1 | 2013-08-06 20:05:54 +0200 | [diff] [blame] | 816 | if (flags & SURFACE_HINT_RGB565 && display->has_rgb565) |
| 817 | format = WL_SHM_FORMAT_RGB565; |
| 818 | else { |
| 819 | if (flags & SURFACE_OPAQUE) |
| 820 | format = WL_SHM_FORMAT_XRGB8888; |
| 821 | else |
| 822 | format = WL_SHM_FORMAT_ARGB8888; |
| 823 | } |
Kristian Høgsberg | 3be87d1 | 2011-05-13 13:45:17 -0400 | [diff] [blame] | 824 | |
Pekka Paalanen | 0c6a343 | 2012-11-19 15:32:50 +0200 | [diff] [blame] | 825 | data->buffer = wl_shm_pool_create_buffer(pool->pool, offset, |
| 826 | rectangle->width, |
| 827 | rectangle->height, |
| 828 | stride, format); |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 829 | |
Ander Conselvan de Oliveira | 001de54 | 2012-05-07 11:34:26 -0700 | [diff] [blame] | 830 | return surface; |
| 831 | } |
| 832 | |
| 833 | static cairo_surface_t * |
| 834 | display_create_shm_surface(struct display *display, |
| 835 | struct rectangle *rectangle, uint32_t flags, |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 836 | struct shm_pool *alternate_pool, |
| 837 | struct shm_surface_data **data_ret) |
Ander Conselvan de Oliveira | 001de54 | 2012-05-07 11:34:26 -0700 | [diff] [blame] | 838 | { |
| 839 | struct shm_surface_data *data; |
| 840 | struct shm_pool *pool; |
| 841 | cairo_surface_t *surface; |
| 842 | |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 843 | if (alternate_pool) { |
| 844 | shm_pool_reset(alternate_pool); |
Ander Conselvan de Oliveira | 001de54 | 2012-05-07 11:34:26 -0700 | [diff] [blame] | 845 | surface = display_create_shm_surface_from_pool(display, |
| 846 | rectangle, |
| 847 | flags, |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 848 | alternate_pool); |
| 849 | if (surface) { |
| 850 | data = cairo_surface_get_user_data(surface, |
| 851 | &shm_surface_data_key); |
| 852 | goto out; |
| 853 | } |
Ander Conselvan de Oliveira | 001de54 | 2012-05-07 11:34:26 -0700 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | pool = shm_pool_create(display, |
| 857 | data_length_for_shm_surface(rectangle)); |
| 858 | if (!pool) |
| 859 | return NULL; |
| 860 | |
| 861 | surface = |
| 862 | display_create_shm_surface_from_pool(display, rectangle, |
| 863 | flags, pool); |
| 864 | |
| 865 | if (!surface) { |
| 866 | shm_pool_destroy(pool); |
| 867 | return NULL; |
| 868 | } |
| 869 | |
| 870 | /* make sure we destroy the pool when the surface is destroyed */ |
Pekka Paalanen | 32127ca | 2012-11-19 15:32:51 +0200 | [diff] [blame] | 871 | data = cairo_surface_get_user_data(surface, &shm_surface_data_key); |
Ander Conselvan de Oliveira | 001de54 | 2012-05-07 11:34:26 -0700 | [diff] [blame] | 872 | data->pool = pool; |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 873 | |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 874 | out: |
| 875 | if (data_ret) |
| 876 | *data_ret = data; |
| 877 | |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 878 | return surface; |
| 879 | } |
| 880 | |
nobled | 7b87cb0 | 2011-02-01 18:51:47 +0000 | [diff] [blame] | 881 | static int |
| 882 | check_size(struct rectangle *rect) |
| 883 | { |
| 884 | if (rect->width && rect->height) |
| 885 | return 0; |
| 886 | |
| 887 | fprintf(stderr, "tried to create surface of " |
| 888 | "width: %d, height: %d\n", rect->width, rect->height); |
| 889 | return -1; |
| 890 | } |
| 891 | |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 892 | cairo_surface_t * |
| 893 | display_create_surface(struct display *display, |
Benjamin Franzke | 6693ac2 | 2011-02-10 12:04:30 +0100 | [diff] [blame] | 894 | struct wl_surface *surface, |
Kristian Høgsberg | 3be87d1 | 2011-05-13 13:45:17 -0400 | [diff] [blame] | 895 | struct rectangle *rectangle, |
| 896 | uint32_t flags) |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 897 | { |
nobled | 7b87cb0 | 2011-02-01 18:51:47 +0000 | [diff] [blame] | 898 | if (check_size(rectangle) < 0) |
| 899 | return NULL; |
Pekka Paalanen | 768117f | 2012-11-19 17:15:57 +0200 | [diff] [blame] | 900 | |
| 901 | assert(flags & SURFACE_SHM); |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 902 | return display_create_shm_surface(display, rectangle, flags, |
| 903 | NULL, NULL); |
| 904 | } |
| 905 | |
Pekka Paalanen | a4eda73 | 2012-11-19 17:16:02 +0200 | [diff] [blame] | 906 | struct shm_surface_leaf { |
| 907 | cairo_surface_t *cairo_surface; |
| 908 | /* 'data' is automatically destroyed, when 'cairo_surface' is */ |
| 909 | struct shm_surface_data *data; |
| 910 | |
| 911 | struct shm_pool *resize_pool; |
| 912 | int busy; |
| 913 | }; |
| 914 | |
| 915 | static void |
| 916 | shm_surface_leaf_release(struct shm_surface_leaf *leaf) |
| 917 | { |
| 918 | if (leaf->cairo_surface) |
| 919 | cairo_surface_destroy(leaf->cairo_surface); |
| 920 | /* leaf->data already destroyed via cairo private */ |
| 921 | |
| 922 | if (leaf->resize_pool) |
| 923 | shm_pool_destroy(leaf->resize_pool); |
Pekka Paalanen | 550d66b | 2013-02-13 16:17:12 +0200 | [diff] [blame] | 924 | |
| 925 | memset(leaf, 0, sizeof *leaf); |
Pekka Paalanen | a4eda73 | 2012-11-19 17:16:02 +0200 | [diff] [blame] | 926 | } |
| 927 | |
Pekka Paalanen | aef0254 | 2013-04-25 13:57:47 +0300 | [diff] [blame] | 928 | #define MAX_LEAVES 3 |
| 929 | |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 930 | struct shm_surface { |
| 931 | struct toysurface base; |
| 932 | struct display *display; |
| 933 | struct wl_surface *surface; |
| 934 | uint32_t flags; |
| 935 | int dx, dy; |
| 936 | |
Pekka Paalanen | aef0254 | 2013-04-25 13:57:47 +0300 | [diff] [blame] | 937 | struct shm_surface_leaf leaf[MAX_LEAVES]; |
Pekka Paalanen | a4eda73 | 2012-11-19 17:16:02 +0200 | [diff] [blame] | 938 | struct shm_surface_leaf *current; |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 939 | }; |
| 940 | |
| 941 | static struct shm_surface * |
| 942 | to_shm_surface(struct toysurface *base) |
| 943 | { |
| 944 | return container_of(base, struct shm_surface, base); |
| 945 | } |
| 946 | |
Pekka Paalanen | a4eda73 | 2012-11-19 17:16:02 +0200 | [diff] [blame] | 947 | static void |
Pekka Paalanen | 9777744 | 2013-05-22 10:20:05 +0300 | [diff] [blame] | 948 | shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg) |
| 949 | { |
| 950 | #ifdef DEBUG |
| 951 | struct shm_surface_leaf *leaf; |
| 952 | char bufs[MAX_LEAVES + 1]; |
| 953 | int i; |
| 954 | |
| 955 | for (i = 0; i < MAX_LEAVES; i++) { |
| 956 | leaf = &surface->leaf[i]; |
| 957 | |
| 958 | if (leaf->busy) |
| 959 | bufs[i] = 'b'; |
| 960 | else if (leaf->cairo_surface) |
| 961 | bufs[i] = 'a'; |
| 962 | else |
| 963 | bufs[i] = ' '; |
| 964 | } |
| 965 | |
| 966 | bufs[MAX_LEAVES] = '\0'; |
| 967 | DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs); |
| 968 | #endif |
| 969 | } |
| 970 | |
| 971 | static void |
Pekka Paalanen | a4eda73 | 2012-11-19 17:16:02 +0200 | [diff] [blame] | 972 | shm_surface_buffer_release(void *data, struct wl_buffer *buffer) |
| 973 | { |
Pekka Paalanen | 550d66b | 2013-02-13 16:17:12 +0200 | [diff] [blame] | 974 | struct shm_surface *surface = data; |
Pekka Paalanen | aef0254 | 2013-04-25 13:57:47 +0300 | [diff] [blame] | 975 | struct shm_surface_leaf *leaf; |
| 976 | int i; |
| 977 | int free_found; |
Pekka Paalanen | 9777744 | 2013-05-22 10:20:05 +0300 | [diff] [blame] | 978 | |
| 979 | shm_surface_buffer_state_debug(surface, "buffer_release before"); |
Pekka Paalanen | a4eda73 | 2012-11-19 17:16:02 +0200 | [diff] [blame] | 980 | |
Pekka Paalanen | aef0254 | 2013-04-25 13:57:47 +0300 | [diff] [blame] | 981 | for (i = 0; i < MAX_LEAVES; i++) { |
| 982 | leaf = &surface->leaf[i]; |
| 983 | if (leaf->data && leaf->data->buffer == buffer) { |
| 984 | leaf->busy = 0; |
| 985 | break; |
| 986 | } |
| 987 | } |
| 988 | assert(i < MAX_LEAVES && "unknown buffer released"); |
Pekka Paalanen | 4dd0c41 | 2012-12-04 16:01:16 +0200 | [diff] [blame] | 989 | |
Pekka Paalanen | aef0254 | 2013-04-25 13:57:47 +0300 | [diff] [blame] | 990 | /* Leave one free leaf with storage, release others */ |
| 991 | free_found = 0; |
| 992 | for (i = 0; i < MAX_LEAVES; i++) { |
| 993 | leaf = &surface->leaf[i]; |
| 994 | |
| 995 | if (!leaf->cairo_surface || leaf->busy) |
| 996 | continue; |
| 997 | |
| 998 | if (!free_found) |
| 999 | free_found = 1; |
Pekka Paalanen | 9777744 | 2013-05-22 10:20:05 +0300 | [diff] [blame] | 1000 | else |
Pekka Paalanen | aef0254 | 2013-04-25 13:57:47 +0300 | [diff] [blame] | 1001 | shm_surface_leaf_release(leaf); |
| 1002 | } |
Pekka Paalanen | 7123388 | 2013-04-25 13:57:53 +0300 | [diff] [blame] | 1003 | |
Pekka Paalanen | 9777744 | 2013-05-22 10:20:05 +0300 | [diff] [blame] | 1004 | shm_surface_buffer_state_debug(surface, "buffer_release after"); |
Pekka Paalanen | a4eda73 | 2012-11-19 17:16:02 +0200 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | static const struct wl_buffer_listener shm_surface_buffer_listener = { |
| 1008 | shm_surface_buffer_release |
| 1009 | }; |
| 1010 | |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 1011 | static cairo_surface_t * |
| 1012 | shm_surface_prepare(struct toysurface *base, int dx, int dy, |
Alexander Larsson | 1818e31 | 2013-05-22 14:41:31 +0200 | [diff] [blame] | 1013 | int32_t width, int32_t height, uint32_t flags, |
Alexander Larsson | edddbd1 | 2013-05-24 13:09:43 +0200 | [diff] [blame] | 1014 | enum wl_output_transform buffer_transform, int32_t buffer_scale) |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 1015 | { |
Pekka Paalanen | ec07669 | 2012-11-30 13:37:27 +0200 | [diff] [blame] | 1016 | int resize_hint = !!(flags & SURFACE_HINT_RESIZE); |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 1017 | struct shm_surface *surface = to_shm_surface(base); |
Alexander Larsson | 1818e31 | 2013-05-22 14:41:31 +0200 | [diff] [blame] | 1018 | struct rectangle rect = { 0}; |
Pekka Paalanen | aef0254 | 2013-04-25 13:57:47 +0300 | [diff] [blame] | 1019 | struct shm_surface_leaf *leaf = NULL; |
| 1020 | int i; |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 1021 | |
| 1022 | surface->dx = dx; |
| 1023 | surface->dy = dy; |
| 1024 | |
Pekka Paalanen | aef0254 | 2013-04-25 13:57:47 +0300 | [diff] [blame] | 1025 | /* pick a free buffer, preferrably one that already has storage */ |
| 1026 | for (i = 0; i < MAX_LEAVES; i++) { |
| 1027 | if (surface->leaf[i].busy) |
| 1028 | continue; |
Pekka Paalanen | 4dd0c41 | 2012-12-04 16:01:16 +0200 | [diff] [blame] | 1029 | |
Pekka Paalanen | aef0254 | 2013-04-25 13:57:47 +0300 | [diff] [blame] | 1030 | if (!leaf || surface->leaf[i].cairo_surface) |
| 1031 | leaf = &surface->leaf[i]; |
| 1032 | } |
Pekka Paalanen | 7123388 | 2013-04-25 13:57:53 +0300 | [diff] [blame] | 1033 | DBG_OBJ(surface->surface, "pick leaf %d\n", |
| 1034 | (int)(leaf - &surface->leaf[0])); |
| 1035 | |
Pekka Paalanen | aef0254 | 2013-04-25 13:57:47 +0300 | [diff] [blame] | 1036 | if (!leaf) { |
| 1037 | fprintf(stderr, "%s: all buffers are held by the server.\n", |
Pekka Paalanen | a4eda73 | 2012-11-19 17:16:02 +0200 | [diff] [blame] | 1038 | __func__); |
Pekka Paalanen | 7123388 | 2013-04-25 13:57:53 +0300 | [diff] [blame] | 1039 | exit(1); |
Pekka Paalanen | a4eda73 | 2012-11-19 17:16:02 +0200 | [diff] [blame] | 1040 | return NULL; |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 1041 | } |
| 1042 | |
Pekka Paalanen | a4eda73 | 2012-11-19 17:16:02 +0200 | [diff] [blame] | 1043 | if (!resize_hint && leaf->resize_pool) { |
| 1044 | cairo_surface_destroy(leaf->cairo_surface); |
| 1045 | leaf->cairo_surface = NULL; |
| 1046 | shm_pool_destroy(leaf->resize_pool); |
| 1047 | leaf->resize_pool = NULL; |
| 1048 | } |
| 1049 | |
Alexander Larsson | 1818e31 | 2013-05-22 14:41:31 +0200 | [diff] [blame] | 1050 | surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height); |
| 1051 | |
Pekka Paalanen | a4eda73 | 2012-11-19 17:16:02 +0200 | [diff] [blame] | 1052 | if (leaf->cairo_surface && |
| 1053 | cairo_image_surface_get_width(leaf->cairo_surface) == width && |
| 1054 | cairo_image_surface_get_height(leaf->cairo_surface) == height) |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 1055 | goto out; |
| 1056 | |
Pekka Paalanen | a4eda73 | 2012-11-19 17:16:02 +0200 | [diff] [blame] | 1057 | if (leaf->cairo_surface) |
| 1058 | cairo_surface_destroy(leaf->cairo_surface); |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 1059 | |
Louis-Francis Ratté-Boulianne | 6cd1de3 | 2013-05-22 18:03:11 +0300 | [diff] [blame] | 1060 | #ifdef USE_RESIZE_POOL |
Pekka Paalanen | a4eda73 | 2012-11-19 17:16:02 +0200 | [diff] [blame] | 1061 | if (resize_hint && !leaf->resize_pool) { |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 1062 | /* Create a big pool to allocate from, while continuously |
| 1063 | * resizing. Mmapping a new pool in the server |
| 1064 | * is relatively expensive, so reusing a pool performs |
| 1065 | * better, but may temporarily reserve unneeded memory. |
| 1066 | */ |
| 1067 | /* We should probably base this number on the output size. */ |
Pekka Paalanen | a4eda73 | 2012-11-19 17:16:02 +0200 | [diff] [blame] | 1068 | leaf->resize_pool = shm_pool_create(surface->display, |
| 1069 | 6 * 1024 * 1024); |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 1070 | } |
Louis-Francis Ratté-Boulianne | 6cd1de3 | 2013-05-22 18:03:11 +0300 | [diff] [blame] | 1071 | #endif |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 1072 | |
Alexander Larsson | 1818e31 | 2013-05-22 14:41:31 +0200 | [diff] [blame] | 1073 | rect.width = width; |
| 1074 | rect.height = height; |
| 1075 | |
Pekka Paalanen | a4eda73 | 2012-11-19 17:16:02 +0200 | [diff] [blame] | 1076 | leaf->cairo_surface = |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 1077 | display_create_shm_surface(surface->display, &rect, |
| 1078 | surface->flags, |
Pekka Paalanen | a4eda73 | 2012-11-19 17:16:02 +0200 | [diff] [blame] | 1079 | leaf->resize_pool, |
| 1080 | &leaf->data); |
| 1081 | wl_buffer_add_listener(leaf->data->buffer, |
Pekka Paalanen | 550d66b | 2013-02-13 16:17:12 +0200 | [diff] [blame] | 1082 | &shm_surface_buffer_listener, surface); |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 1083 | |
| 1084 | out: |
Pekka Paalanen | a4eda73 | 2012-11-19 17:16:02 +0200 | [diff] [blame] | 1085 | surface->current = leaf; |
| 1086 | |
| 1087 | return cairo_surface_reference(leaf->cairo_surface); |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | static void |
| 1091 | shm_surface_swap(struct toysurface *base, |
Alexander Larsson | edddbd1 | 2013-05-24 13:09:43 +0200 | [diff] [blame] | 1092 | enum wl_output_transform buffer_transform, int32_t buffer_scale, |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 1093 | struct rectangle *server_allocation) |
| 1094 | { |
| 1095 | struct shm_surface *surface = to_shm_surface(base); |
Pekka Paalanen | a4eda73 | 2012-11-19 17:16:02 +0200 | [diff] [blame] | 1096 | struct shm_surface_leaf *leaf = surface->current; |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 1097 | |
| 1098 | server_allocation->width = |
Pekka Paalanen | a4eda73 | 2012-11-19 17:16:02 +0200 | [diff] [blame] | 1099 | cairo_image_surface_get_width(leaf->cairo_surface); |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 1100 | server_allocation->height = |
Pekka Paalanen | a4eda73 | 2012-11-19 17:16:02 +0200 | [diff] [blame] | 1101 | cairo_image_surface_get_height(leaf->cairo_surface); |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 1102 | |
Alexander Larsson | 1818e31 | 2013-05-22 14:41:31 +0200 | [diff] [blame] | 1103 | buffer_to_surface_size (buffer_transform, buffer_scale, |
| 1104 | &server_allocation->width, |
| 1105 | &server_allocation->height); |
| 1106 | |
Pekka Paalanen | a4eda73 | 2012-11-19 17:16:02 +0200 | [diff] [blame] | 1107 | wl_surface_attach(surface->surface, leaf->data->buffer, |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 1108 | surface->dx, surface->dy); |
| 1109 | wl_surface_damage(surface->surface, 0, 0, |
| 1110 | server_allocation->width, server_allocation->height); |
| 1111 | wl_surface_commit(surface->surface); |
Pekka Paalanen | a4eda73 | 2012-11-19 17:16:02 +0200 | [diff] [blame] | 1112 | |
Pekka Paalanen | 7123388 | 2013-04-25 13:57:53 +0300 | [diff] [blame] | 1113 | DBG_OBJ(surface->surface, "leaf %d busy\n", |
| 1114 | (int)(leaf - &surface->leaf[0])); |
| 1115 | |
Pekka Paalanen | a4eda73 | 2012-11-19 17:16:02 +0200 | [diff] [blame] | 1116 | leaf->busy = 1; |
| 1117 | surface->current = NULL; |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | static int |
| 1121 | shm_surface_acquire(struct toysurface *base, EGLContext ctx) |
| 1122 | { |
| 1123 | return -1; |
| 1124 | } |
| 1125 | |
| 1126 | static void |
| 1127 | shm_surface_release(struct toysurface *base) |
| 1128 | { |
| 1129 | } |
| 1130 | |
| 1131 | static void |
| 1132 | shm_surface_destroy(struct toysurface *base) |
| 1133 | { |
| 1134 | struct shm_surface *surface = to_shm_surface(base); |
Pekka Paalanen | aef0254 | 2013-04-25 13:57:47 +0300 | [diff] [blame] | 1135 | int i; |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 1136 | |
Pekka Paalanen | aef0254 | 2013-04-25 13:57:47 +0300 | [diff] [blame] | 1137 | for (i = 0; i < MAX_LEAVES; i++) |
| 1138 | shm_surface_leaf_release(&surface->leaf[i]); |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 1139 | |
| 1140 | free(surface); |
| 1141 | } |
| 1142 | |
| 1143 | static struct toysurface * |
| 1144 | shm_surface_create(struct display *display, struct wl_surface *wl_surface, |
| 1145 | uint32_t flags, struct rectangle *rectangle) |
| 1146 | { |
| 1147 | struct shm_surface *surface; |
Pekka Paalanen | 7123388 | 2013-04-25 13:57:53 +0300 | [diff] [blame] | 1148 | DBG_OBJ(wl_surface, "\n"); |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 1149 | |
Brian Lovin | bc91926 | 2013-08-07 15:34:59 -0700 | [diff] [blame] | 1150 | surface = xmalloc(sizeof *surface); |
| 1151 | memset(surface, 0, sizeof *surface); |
| 1152 | |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 1153 | if (!surface) |
| 1154 | return NULL; |
| 1155 | |
| 1156 | surface->base.prepare = shm_surface_prepare; |
| 1157 | surface->base.swap = shm_surface_swap; |
| 1158 | surface->base.acquire = shm_surface_acquire; |
| 1159 | surface->base.release = shm_surface_release; |
| 1160 | surface->base.destroy = shm_surface_destroy; |
| 1161 | |
| 1162 | surface->display = display; |
| 1163 | surface->surface = wl_surface; |
| 1164 | surface->flags = flags; |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 1165 | |
| 1166 | return &surface->base; |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 1167 | } |
| 1168 | |
Philipp Brüschweiler | bd3f219 | 2012-08-21 20:36:16 +0200 | [diff] [blame] | 1169 | /* |
| 1170 | * The following correspondences between file names and cursors was copied |
| 1171 | * from: https://bugs.kde.org/attachment.cgi?id=67313 |
| 1172 | */ |
| 1173 | |
| 1174 | static const char *bottom_left_corners[] = { |
Ander Conselvan de Oliveira | d8f527c | 2012-05-25 09:30:02 +0300 | [diff] [blame] | 1175 | "bottom_left_corner", |
Dima Ryazanov | f6128fc | 2013-05-13 23:51:11 -0700 | [diff] [blame] | 1176 | "sw-resize", |
| 1177 | "size_bdiag" |
Philipp Brüschweiler | bd3f219 | 2012-08-21 20:36:16 +0200 | [diff] [blame] | 1178 | }; |
| 1179 | |
| 1180 | static const char *bottom_right_corners[] = { |
Ander Conselvan de Oliveira | d8f527c | 2012-05-25 09:30:02 +0300 | [diff] [blame] | 1181 | "bottom_right_corner", |
Dima Ryazanov | f6128fc | 2013-05-13 23:51:11 -0700 | [diff] [blame] | 1182 | "se-resize", |
| 1183 | "size_fdiag" |
Philipp Brüschweiler | bd3f219 | 2012-08-21 20:36:16 +0200 | [diff] [blame] | 1184 | }; |
| 1185 | |
| 1186 | static const char *bottom_sides[] = { |
Ander Conselvan de Oliveira | d8f527c | 2012-05-25 09:30:02 +0300 | [diff] [blame] | 1187 | "bottom_side", |
Dima Ryazanov | f6128fc | 2013-05-13 23:51:11 -0700 | [diff] [blame] | 1188 | "s-resize", |
| 1189 | "size_ver" |
Philipp Brüschweiler | bd3f219 | 2012-08-21 20:36:16 +0200 | [diff] [blame] | 1190 | }; |
| 1191 | |
| 1192 | static const char *grabbings[] = { |
Ander Conselvan de Oliveira | d8f527c | 2012-05-25 09:30:02 +0300 | [diff] [blame] | 1193 | "grabbing", |
Philipp Brüschweiler | bd3f219 | 2012-08-21 20:36:16 +0200 | [diff] [blame] | 1194 | "closedhand", |
| 1195 | "208530c400c041818281048008011002" |
| 1196 | }; |
| 1197 | |
| 1198 | static const char *left_ptrs[] = { |
Ander Conselvan de Oliveira | d8f527c | 2012-05-25 09:30:02 +0300 | [diff] [blame] | 1199 | "left_ptr", |
Philipp Brüschweiler | bd3f219 | 2012-08-21 20:36:16 +0200 | [diff] [blame] | 1200 | "default", |
| 1201 | "top_left_arrow", |
| 1202 | "left-arrow" |
| 1203 | }; |
| 1204 | |
| 1205 | static const char *left_sides[] = { |
Ander Conselvan de Oliveira | d8f527c | 2012-05-25 09:30:02 +0300 | [diff] [blame] | 1206 | "left_side", |
Dima Ryazanov | f6128fc | 2013-05-13 23:51:11 -0700 | [diff] [blame] | 1207 | "w-resize", |
| 1208 | "size_hor" |
Philipp Brüschweiler | bd3f219 | 2012-08-21 20:36:16 +0200 | [diff] [blame] | 1209 | }; |
| 1210 | |
| 1211 | static const char *right_sides[] = { |
Ander Conselvan de Oliveira | d8f527c | 2012-05-25 09:30:02 +0300 | [diff] [blame] | 1212 | "right_side", |
Dima Ryazanov | f6128fc | 2013-05-13 23:51:11 -0700 | [diff] [blame] | 1213 | "e-resize", |
| 1214 | "size_hor" |
Philipp Brüschweiler | bd3f219 | 2012-08-21 20:36:16 +0200 | [diff] [blame] | 1215 | }; |
| 1216 | |
| 1217 | static const char *top_left_corners[] = { |
Ander Conselvan de Oliveira | d8f527c | 2012-05-25 09:30:02 +0300 | [diff] [blame] | 1218 | "top_left_corner", |
Dima Ryazanov | f6128fc | 2013-05-13 23:51:11 -0700 | [diff] [blame] | 1219 | "nw-resize", |
| 1220 | "size_fdiag" |
Philipp Brüschweiler | bd3f219 | 2012-08-21 20:36:16 +0200 | [diff] [blame] | 1221 | }; |
| 1222 | |
| 1223 | static const char *top_right_corners[] = { |
Ander Conselvan de Oliveira | d8f527c | 2012-05-25 09:30:02 +0300 | [diff] [blame] | 1224 | "top_right_corner", |
Dima Ryazanov | f6128fc | 2013-05-13 23:51:11 -0700 | [diff] [blame] | 1225 | "ne-resize", |
| 1226 | "size_bdiag" |
Philipp Brüschweiler | bd3f219 | 2012-08-21 20:36:16 +0200 | [diff] [blame] | 1227 | }; |
| 1228 | |
| 1229 | static const char *top_sides[] = { |
Ander Conselvan de Oliveira | d8f527c | 2012-05-25 09:30:02 +0300 | [diff] [blame] | 1230 | "top_side", |
Dima Ryazanov | f6128fc | 2013-05-13 23:51:11 -0700 | [diff] [blame] | 1231 | "n-resize", |
| 1232 | "size_ver" |
Philipp Brüschweiler | bd3f219 | 2012-08-21 20:36:16 +0200 | [diff] [blame] | 1233 | }; |
| 1234 | |
| 1235 | static const char *xterms[] = { |
Ander Conselvan de Oliveira | d8f527c | 2012-05-25 09:30:02 +0300 | [diff] [blame] | 1236 | "xterm", |
Philipp Brüschweiler | bd3f219 | 2012-08-21 20:36:16 +0200 | [diff] [blame] | 1237 | "ibeam", |
| 1238 | "text" |
| 1239 | }; |
| 1240 | |
| 1241 | static const char *hand1s[] = { |
Ander Conselvan de Oliveira | d8f527c | 2012-05-25 09:30:02 +0300 | [diff] [blame] | 1242 | "hand1", |
Philipp Brüschweiler | bd3f219 | 2012-08-21 20:36:16 +0200 | [diff] [blame] | 1243 | "pointer", |
| 1244 | "pointing_hand", |
| 1245 | "e29285e634086352946a0e7090d73106" |
| 1246 | }; |
| 1247 | |
| 1248 | static const char *watches[] = { |
Kristian Høgsberg | 8591dbf | 2012-06-04 16:10:40 -0400 | [diff] [blame] | 1249 | "watch", |
Philipp Brüschweiler | bd3f219 | 2012-08-21 20:36:16 +0200 | [diff] [blame] | 1250 | "wait", |
| 1251 | "0426c94ea35c87780ff01dc239897213" |
| 1252 | }; |
| 1253 | |
| 1254 | struct cursor_alternatives { |
| 1255 | const char **names; |
| 1256 | size_t count; |
| 1257 | }; |
| 1258 | |
| 1259 | static const struct cursor_alternatives cursors[] = { |
| 1260 | {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)}, |
| 1261 | {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)}, |
| 1262 | {bottom_sides, ARRAY_LENGTH(bottom_sides)}, |
| 1263 | {grabbings, ARRAY_LENGTH(grabbings)}, |
| 1264 | {left_ptrs, ARRAY_LENGTH(left_ptrs)}, |
| 1265 | {left_sides, ARRAY_LENGTH(left_sides)}, |
| 1266 | {right_sides, ARRAY_LENGTH(right_sides)}, |
| 1267 | {top_left_corners, ARRAY_LENGTH(top_left_corners)}, |
| 1268 | {top_right_corners, ARRAY_LENGTH(top_right_corners)}, |
| 1269 | {top_sides, ARRAY_LENGTH(top_sides)}, |
| 1270 | {xterms, ARRAY_LENGTH(xterms)}, |
| 1271 | {hand1s, ARRAY_LENGTH(hand1s)}, |
| 1272 | {watches, ARRAY_LENGTH(watches)}, |
Ander Conselvan de Oliveira | d8f527c | 2012-05-25 09:30:02 +0300 | [diff] [blame] | 1273 | }; |
| 1274 | |
| 1275 | static void |
| 1276 | create_cursors(struct display *display) |
| 1277 | { |
Kristian Høgsberg | 8c079ae | 2013-09-21 22:26:10 -0700 | [diff] [blame] | 1278 | struct weston_config *config; |
| 1279 | struct weston_config_section *s; |
Kristian Høgsberg | 1abe048 | 2013-09-21 23:02:31 -0700 | [diff] [blame] | 1280 | int size; |
Christopher Michael | ac3e5f2 | 2012-08-11 15:12:09 +0100 | [diff] [blame] | 1281 | char *theme = NULL; |
Philipp Brüschweiler | bd3f219 | 2012-08-21 20:36:16 +0200 | [diff] [blame] | 1282 | unsigned int i, j; |
| 1283 | struct wl_cursor *cursor; |
Ander Conselvan de Oliveira | d8f527c | 2012-05-25 09:30:02 +0300 | [diff] [blame] | 1284 | |
Kristian Høgsberg | 1abe048 | 2013-09-21 23:02:31 -0700 | [diff] [blame] | 1285 | config = weston_config_parse("weston.ini"); |
Kristian Høgsberg | 8c079ae | 2013-09-21 22:26:10 -0700 | [diff] [blame] | 1286 | s = weston_config_get_section(config, "shell", NULL, NULL); |
| 1287 | weston_config_section_get_string(s, "cursor-theme", &theme, NULL); |
| 1288 | weston_config_section_get_int(s, "cursor-size", &size, 32); |
| 1289 | weston_config_destroy(config); |
| 1290 | |
Emilio Pozuelo Monfort | ab44b0c | 2013-03-14 17:23:37 +0100 | [diff] [blame] | 1291 | display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm); |
Kristian Høgsberg | b5c973c | 2013-10-09 13:02:04 -0700 | [diff] [blame] | 1292 | free(theme); |
Ander Conselvan de Oliveira | d8f527c | 2012-05-25 09:30:02 +0300 | [diff] [blame] | 1293 | display->cursors = |
Brian Lovin | bc91926 | 2013-08-07 15:34:59 -0700 | [diff] [blame] | 1294 | xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]); |
Ander Conselvan de Oliveira | d8f527c | 2012-05-25 09:30:02 +0300 | [diff] [blame] | 1295 | |
Pekka Paalanen | e288a0f | 2012-07-31 13:21:09 +0300 | [diff] [blame] | 1296 | for (i = 0; i < ARRAY_LENGTH(cursors); i++) { |
Philipp Brüschweiler | bd3f219 | 2012-08-21 20:36:16 +0200 | [diff] [blame] | 1297 | cursor = NULL; |
| 1298 | for (j = 0; !cursor && j < cursors[i].count; ++j) |
| 1299 | cursor = wl_cursor_theme_get_cursor( |
| 1300 | display->cursor_theme, cursors[i].names[j]); |
| 1301 | |
| 1302 | if (!cursor) |
Pekka Paalanen | e288a0f | 2012-07-31 13:21:09 +0300 | [diff] [blame] | 1303 | fprintf(stderr, "could not load cursor '%s'\n", |
Philipp Brüschweiler | bd3f219 | 2012-08-21 20:36:16 +0200 | [diff] [blame] | 1304 | cursors[i].names[0]); |
| 1305 | |
| 1306 | display->cursors[i] = cursor; |
Pekka Paalanen | e288a0f | 2012-07-31 13:21:09 +0300 | [diff] [blame] | 1307 | } |
Ander Conselvan de Oliveira | d8f527c | 2012-05-25 09:30:02 +0300 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | static void |
| 1311 | destroy_cursors(struct display *display) |
| 1312 | { |
| 1313 | wl_cursor_theme_destroy(display->cursor_theme); |
Yan Wang | a261f7e | 2012-05-28 14:07:25 +0800 | [diff] [blame] | 1314 | free(display->cursors); |
Ander Conselvan de Oliveira | d8f527c | 2012-05-25 09:30:02 +0300 | [diff] [blame] | 1315 | } |
| 1316 | |
Ander Conselvan de Oliveira | 1042dc1 | 2012-05-22 15:39:42 +0300 | [diff] [blame] | 1317 | struct wl_cursor_image * |
| 1318 | display_get_pointer_image(struct display *display, int pointer) |
Kristian Høgsberg | da275dd | 2010-08-16 17:47:07 -0400 | [diff] [blame] | 1319 | { |
Philipp Brüschweiler | bd3f219 | 2012-08-21 20:36:16 +0200 | [diff] [blame] | 1320 | struct wl_cursor *cursor = display->cursors[pointer]; |
Kristian Høgsberg | da275dd | 2010-08-16 17:47:07 -0400 | [diff] [blame] | 1321 | |
Ander Conselvan de Oliveira | 1042dc1 | 2012-05-22 15:39:42 +0300 | [diff] [blame] | 1322 | return cursor ? cursor->images[0] : NULL; |
Kristian Høgsberg | 9a68624 | 2010-08-18 15:28:04 -0400 | [diff] [blame] | 1323 | } |
| 1324 | |
Kristian Høgsberg | 9d69f8e | 2010-09-03 14:46:38 -0400 | [diff] [blame] | 1325 | static void |
Pekka Paalanen | 89dee00 | 2013-02-13 16:17:20 +0200 | [diff] [blame] | 1326 | surface_flush(struct surface *surface) |
Pekka Paalanen | 811ec4f | 2013-02-13 16:17:14 +0200 | [diff] [blame] | 1327 | { |
Pekka Paalanen | 89dee00 | 2013-02-13 16:17:20 +0200 | [diff] [blame] | 1328 | if (!surface->cairo_surface) |
| 1329 | return; |
| 1330 | |
Pekka Paalanen | b1cd9b1 | 2013-02-13 16:17:18 +0200 | [diff] [blame] | 1331 | if (surface->opaque_region) { |
| 1332 | wl_surface_set_opaque_region(surface->surface, |
| 1333 | surface->opaque_region); |
| 1334 | wl_region_destroy(surface->opaque_region); |
| 1335 | surface->opaque_region = NULL; |
| 1336 | } |
| 1337 | |
| 1338 | if (surface->input_region) { |
| 1339 | wl_surface_set_input_region(surface->surface, |
| 1340 | surface->input_region); |
| 1341 | wl_region_destroy(surface->input_region); |
| 1342 | surface->input_region = NULL; |
| 1343 | } |
| 1344 | |
Pekka Paalanen | 811ec4f | 2013-02-13 16:17:14 +0200 | [diff] [blame] | 1345 | surface->toysurface->swap(surface->toysurface, |
Alexander Larsson | 1818e31 | 2013-05-22 14:41:31 +0200 | [diff] [blame] | 1346 | surface->buffer_transform, surface->buffer_scale, |
Pekka Paalanen | 811ec4f | 2013-02-13 16:17:14 +0200 | [diff] [blame] | 1347 | &surface->server_allocation); |
Pekka Paalanen | 811ec4f | 2013-02-13 16:17:14 +0200 | [diff] [blame] | 1348 | |
Pekka Paalanen | 89dee00 | 2013-02-13 16:17:20 +0200 | [diff] [blame] | 1349 | cairo_surface_destroy(surface->cairo_surface); |
| 1350 | surface->cairo_surface = NULL; |
Kristian Høgsberg | 6a1b201 | 2009-12-16 14:43:37 -0500 | [diff] [blame] | 1351 | } |
| 1352 | |
Kristian Høgsberg | 86adef9 | 2012-08-13 22:25:53 -0400 | [diff] [blame] | 1353 | int |
| 1354 | window_has_focus(struct window *window) |
| 1355 | { |
| 1356 | return window->focus_count > 0; |
| 1357 | } |
| 1358 | |
Pekka Paalanen | a8d4c84 | 2012-11-19 15:32:48 +0200 | [diff] [blame] | 1359 | static void |
Kristian Høgsberg | 9d69f8e | 2010-09-03 14:46:38 -0400 | [diff] [blame] | 1360 | window_flush(struct window *window) |
Kristian Høgsberg | a341fa0 | 2010-01-24 18:10:15 -0500 | [diff] [blame] | 1361 | { |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 1362 | struct surface *surface; |
| 1363 | |
Pekka Paalanen | 89dee00 | 2013-02-13 16:17:20 +0200 | [diff] [blame] | 1364 | if (window->type == TYPE_NONE) { |
| 1365 | window->type = TYPE_TOPLEVEL; |
| 1366 | if (window->shell_surface) |
| 1367 | wl_shell_surface_set_toplevel(window->shell_surface); |
| 1368 | } |
Pekka Paalanen | 03fc316 | 2012-11-19 17:15:58 +0200 | [diff] [blame] | 1369 | |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 1370 | wl_list_for_each(surface, &window->subsurface_list, link) { |
| 1371 | if (surface == window->main_surface) |
| 1372 | continue; |
| 1373 | |
| 1374 | surface_flush(surface); |
| 1375 | } |
| 1376 | |
Pekka Paalanen | 89dee00 | 2013-02-13 16:17:20 +0200 | [diff] [blame] | 1377 | surface_flush(window->main_surface); |
Kristian Høgsberg | a341fa0 | 2010-01-24 18:10:15 -0500 | [diff] [blame] | 1378 | } |
| 1379 | |
Kristian Høgsberg | bcee9a4 | 2011-10-12 00:36:16 -0400 | [diff] [blame] | 1380 | struct display * |
| 1381 | window_get_display(struct window *window) |
| 1382 | { |
| 1383 | return window->display; |
| 1384 | } |
| 1385 | |
Pekka Paalanen | 89dee00 | 2013-02-13 16:17:20 +0200 | [diff] [blame] | 1386 | static void |
Pekka Paalanen | 02bba7c | 2013-02-13 16:17:15 +0200 | [diff] [blame] | 1387 | surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags) |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 1388 | { |
Pekka Paalanen | 02bba7c | 2013-02-13 16:17:15 +0200 | [diff] [blame] | 1389 | struct display *display = surface->window->display; |
Pekka Paalanen | 811ec4f | 2013-02-13 16:17:14 +0200 | [diff] [blame] | 1390 | struct rectangle allocation = surface->allocation; |
Pekka Paalanen | 03fc316 | 2012-11-19 17:15:58 +0200 | [diff] [blame] | 1391 | |
Pekka Paalanen | 02bba7c | 2013-02-13 16:17:15 +0200 | [diff] [blame] | 1392 | if (!surface->toysurface && display->dpy && |
| 1393 | surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) { |
Pekka Paalanen | 811ec4f | 2013-02-13 16:17:14 +0200 | [diff] [blame] | 1394 | surface->toysurface = |
Pekka Paalanen | 02bba7c | 2013-02-13 16:17:15 +0200 | [diff] [blame] | 1395 | egl_window_surface_create(display, |
Pekka Paalanen | 811ec4f | 2013-02-13 16:17:14 +0200 | [diff] [blame] | 1396 | surface->surface, |
Pekka Paalanen | 4e37374 | 2013-02-13 16:17:13 +0200 | [diff] [blame] | 1397 | flags, |
Ander Conselvan de Oliveira | 6d4cb4e | 2012-11-30 17:34:24 +0200 | [diff] [blame] | 1398 | &allocation); |
Pekka Paalanen | b362736 | 2012-11-19 17:16:00 +0200 | [diff] [blame] | 1399 | } |
Pekka Paalanen | 03fc316 | 2012-11-19 17:15:58 +0200 | [diff] [blame] | 1400 | |
Pekka Paalanen | 811ec4f | 2013-02-13 16:17:14 +0200 | [diff] [blame] | 1401 | if (!surface->toysurface) |
Pekka Paalanen | 02bba7c | 2013-02-13 16:17:15 +0200 | [diff] [blame] | 1402 | surface->toysurface = shm_surface_create(display, |
Pekka Paalanen | 811ec4f | 2013-02-13 16:17:14 +0200 | [diff] [blame] | 1403 | surface->surface, |
| 1404 | flags, &allocation); |
Kristian Høgsberg | 012a007 | 2010-10-26 00:02:20 -0400 | [diff] [blame] | 1405 | |
Pekka Paalanen | 89dee00 | 2013-02-13 16:17:20 +0200 | [diff] [blame] | 1406 | surface->cairo_surface = surface->toysurface->prepare( |
| 1407 | surface->toysurface, dx, dy, |
Alexander Larsson | 1818e31 | 2013-05-22 14:41:31 +0200 | [diff] [blame] | 1408 | allocation.width, allocation.height, flags, |
| 1409 | surface->buffer_transform, surface->buffer_scale); |
Pekka Paalanen | 02bba7c | 2013-02-13 16:17:15 +0200 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | static void |
Pekka Paalanen | 0c4445b | 2013-02-13 16:17:23 +0200 | [diff] [blame] | 1413 | window_create_main_surface(struct window *window) |
Pekka Paalanen | 02bba7c | 2013-02-13 16:17:15 +0200 | [diff] [blame] | 1414 | { |
Pekka Paalanen | 7bcfead | 2013-02-13 16:17:16 +0200 | [diff] [blame] | 1415 | struct surface *surface = window->main_surface; |
Pekka Paalanen | 02bba7c | 2013-02-13 16:17:15 +0200 | [diff] [blame] | 1416 | uint32_t flags = 0; |
Pekka Paalanen | 7bcfead | 2013-02-13 16:17:16 +0200 | [diff] [blame] | 1417 | int dx = 0; |
| 1418 | int dy = 0; |
Pekka Paalanen | 02bba7c | 2013-02-13 16:17:15 +0200 | [diff] [blame] | 1419 | |
Pekka Paalanen | ec07669 | 2012-11-30 13:37:27 +0200 | [diff] [blame] | 1420 | if (window->resizing) |
Pekka Paalanen | 02bba7c | 2013-02-13 16:17:15 +0200 | [diff] [blame] | 1421 | flags |= SURFACE_HINT_RESIZE; |
Pekka Paalanen | ec07669 | 2012-11-30 13:37:27 +0200 | [diff] [blame] | 1422 | |
Tomeu Vizoso | bee45a1 | 2013-08-06 20:05:54 +0200 | [diff] [blame] | 1423 | if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565) |
| 1424 | flags |= SURFACE_HINT_RGB565; |
| 1425 | |
Pekka Paalanen | 7bcfead | 2013-02-13 16:17:16 +0200 | [diff] [blame] | 1426 | if (window->resize_edges & WINDOW_RESIZING_LEFT) |
| 1427 | dx = surface->server_allocation.width - |
| 1428 | surface->allocation.width; |
| 1429 | |
| 1430 | if (window->resize_edges & WINDOW_RESIZING_TOP) |
| 1431 | dy = surface->server_allocation.height - |
| 1432 | surface->allocation.height; |
| 1433 | |
| 1434 | window->resize_edges = 0; |
| 1435 | |
Pekka Paalanen | 89dee00 | 2013-02-13 16:17:20 +0200 | [diff] [blame] | 1436 | surface_create_surface(surface, dx, dy, flags); |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 1437 | } |
| 1438 | |
Ander Conselvan de Oliveira | 6d4cb4e | 2012-11-30 17:34:24 +0200 | [diff] [blame] | 1439 | int |
| 1440 | window_get_buffer_transform(struct window *window) |
| 1441 | { |
Pekka Paalanen | 02bba7c | 2013-02-13 16:17:15 +0200 | [diff] [blame] | 1442 | return window->main_surface->buffer_transform; |
Ander Conselvan de Oliveira | 6d4cb4e | 2012-11-30 17:34:24 +0200 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | void |
| 1446 | window_set_buffer_transform(struct window *window, |
| 1447 | enum wl_output_transform transform) |
| 1448 | { |
Pekka Paalanen | 02bba7c | 2013-02-13 16:17:15 +0200 | [diff] [blame] | 1449 | window->main_surface->buffer_transform = transform; |
Pekka Paalanen | 4e37374 | 2013-02-13 16:17:13 +0200 | [diff] [blame] | 1450 | wl_surface_set_buffer_transform(window->main_surface->surface, |
| 1451 | transform); |
Ander Conselvan de Oliveira | 6d4cb4e | 2012-11-30 17:34:24 +0200 | [diff] [blame] | 1452 | } |
| 1453 | |
Alexander Larsson | 5e9b652 | 2013-05-22 14:41:28 +0200 | [diff] [blame] | 1454 | void |
| 1455 | window_set_buffer_scale(struct window *window, |
Alexander Larsson | edddbd1 | 2013-05-24 13:09:43 +0200 | [diff] [blame] | 1456 | int32_t scale) |
Alexander Larsson | 5e9b652 | 2013-05-22 14:41:28 +0200 | [diff] [blame] | 1457 | { |
| 1458 | window->main_surface->buffer_scale = scale; |
| 1459 | wl_surface_set_buffer_scale(window->main_surface->surface, |
| 1460 | scale); |
| 1461 | } |
| 1462 | |
| 1463 | uint32_t |
| 1464 | window_get_buffer_scale(struct window *window) |
| 1465 | { |
| 1466 | return window->main_surface->buffer_scale; |
| 1467 | } |
| 1468 | |
Alexander Larsson | d68f523 | 2013-05-22 14:41:33 +0200 | [diff] [blame] | 1469 | uint32_t |
| 1470 | window_get_output_scale(struct window *window) |
| 1471 | { |
| 1472 | struct window_output *window_output; |
| 1473 | struct window_output *window_output_tmp; |
| 1474 | int scale = 1; |
| 1475 | |
| 1476 | wl_list_for_each_safe(window_output, window_output_tmp, |
| 1477 | &window->window_output_list, link) { |
| 1478 | if (window_output->output->scale > scale) |
| 1479 | scale = window_output->output->scale; |
| 1480 | } |
| 1481 | |
| 1482 | return scale; |
| 1483 | } |
| 1484 | |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 1485 | static void window_frame_destroy(struct window_frame *frame); |
Pekka Paalanen | 4dde2fc | 2012-01-19 13:33:50 +0200 | [diff] [blame] | 1486 | |
Pekka Paalanen | 4e37374 | 2013-02-13 16:17:13 +0200 | [diff] [blame] | 1487 | static void |
| 1488 | surface_destroy(struct surface *surface) |
| 1489 | { |
Pekka Paalanen | 40cb67b | 2013-04-25 13:57:49 +0300 | [diff] [blame] | 1490 | if (surface->frame_cb) |
| 1491 | wl_callback_destroy(surface->frame_cb); |
| 1492 | |
Pekka Paalanen | b1cd9b1 | 2013-02-13 16:17:18 +0200 | [diff] [blame] | 1493 | if (surface->input_region) |
| 1494 | wl_region_destroy(surface->input_region); |
| 1495 | |
| 1496 | if (surface->opaque_region) |
| 1497 | wl_region_destroy(surface->opaque_region); |
| 1498 | |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 1499 | if (surface->subsurface) |
| 1500 | wl_subsurface_destroy(surface->subsurface); |
| 1501 | |
Pekka Paalanen | 4e37374 | 2013-02-13 16:17:13 +0200 | [diff] [blame] | 1502 | wl_surface_destroy(surface->surface); |
Pekka Paalanen | 811ec4f | 2013-02-13 16:17:14 +0200 | [diff] [blame] | 1503 | |
| 1504 | if (surface->toysurface) |
| 1505 | surface->toysurface->destroy(surface->toysurface); |
| 1506 | |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 1507 | wl_list_remove(&surface->link); |
Pekka Paalanen | 4e37374 | 2013-02-13 16:17:13 +0200 | [diff] [blame] | 1508 | free(surface); |
| 1509 | } |
| 1510 | |
Kristian Høgsberg | e968f9c | 2010-08-27 22:18:00 -0400 | [diff] [blame] | 1511 | void |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 1512 | window_destroy(struct window *window) |
| 1513 | { |
Pekka Paalanen | 77cbc95 | 2011-11-15 13:34:55 +0200 | [diff] [blame] | 1514 | struct display *display = window->display; |
| 1515 | struct input *input; |
Rob Bradford | 7507b57 | 2012-05-15 17:55:34 +0100 | [diff] [blame] | 1516 | struct window_output *window_output; |
| 1517 | struct window_output *window_output_tmp; |
Pekka Paalanen | 77cbc95 | 2011-11-15 13:34:55 +0200 | [diff] [blame] | 1518 | |
Pekka Paalanen | 40cb67b | 2013-04-25 13:57:49 +0300 | [diff] [blame] | 1519 | wl_list_remove(&window->redraw_task.link); |
Pekka Paalanen | 77cbc95 | 2011-11-15 13:34:55 +0200 | [diff] [blame] | 1520 | |
Rusty Lynch | 1084da5 | 2013-08-15 09:10:08 -0700 | [diff] [blame] | 1521 | wl_list_for_each(input, &display->input_list, link) { |
| 1522 | if (input->touch_focus == window) |
| 1523 | input->touch_focus = NULL; |
Pekka Paalanen | 77cbc95 | 2011-11-15 13:34:55 +0200 | [diff] [blame] | 1524 | if (input->pointer_focus == window) |
| 1525 | input->pointer_focus = NULL; |
| 1526 | if (input->keyboard_focus == window) |
| 1527 | input->keyboard_focus = NULL; |
Kristian Høgsberg | ae6e271 | 2012-01-26 11:09:20 -0500 | [diff] [blame] | 1528 | if (input->focus_widget && |
| 1529 | input->focus_widget->window == window) |
| 1530 | input->focus_widget = NULL; |
Pekka Paalanen | 77cbc95 | 2011-11-15 13:34:55 +0200 | [diff] [blame] | 1531 | } |
| 1532 | |
Rob Bradford | 7507b57 | 2012-05-15 17:55:34 +0100 | [diff] [blame] | 1533 | wl_list_for_each_safe(window_output, window_output_tmp, |
| 1534 | &window->window_output_list, link) { |
| 1535 | free (window_output); |
| 1536 | } |
| 1537 | |
Pekka Paalanen | 4dde2fc | 2012-01-19 13:33:50 +0200 | [diff] [blame] | 1538 | if (window->frame) |
Jason Ekstrand | ee7fefc | 2013-10-13 19:08:38 -0500 | [diff] [blame] | 1539 | window_frame_destroy(window->frame); |
Pekka Paalanen | 4dde2fc | 2012-01-19 13:33:50 +0200 | [diff] [blame] | 1540 | |
Pekka Paalanen | 6b2dc91 | 2011-11-29 10:25:08 +0200 | [diff] [blame] | 1541 | if (window->shell_surface) |
| 1542 | wl_shell_surface_destroy(window->shell_surface); |
Pekka Paalanen | 4e37374 | 2013-02-13 16:17:13 +0200 | [diff] [blame] | 1543 | |
| 1544 | surface_destroy(window->main_surface); |
| 1545 | |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 1546 | wl_list_remove(&window->link); |
Pekka Paalanen | 5ec6585 | 2011-12-16 10:09:29 +0200 | [diff] [blame] | 1547 | |
Pekka Paalanen | 5ec6585 | 2011-12-16 10:09:29 +0200 | [diff] [blame] | 1548 | free(window->title); |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 1549 | free(window); |
| 1550 | } |
| 1551 | |
Kristian Høgsberg | c51f799 | 2012-01-08 15:09:53 -0500 | [diff] [blame] | 1552 | static struct widget * |
Kristian Høgsberg | 441338c | 2012-01-10 13:52:34 -0500 | [diff] [blame] | 1553 | widget_find_widget(struct widget *widget, int32_t x, int32_t y) |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 1554 | { |
Kristian Høgsberg | 441338c | 2012-01-10 13:52:34 -0500 | [diff] [blame] | 1555 | struct widget *child, *target; |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 1556 | |
Kristian Høgsberg | 441338c | 2012-01-10 13:52:34 -0500 | [diff] [blame] | 1557 | wl_list_for_each(child, &widget->child_list, link) { |
| 1558 | target = widget_find_widget(child, x, y); |
| 1559 | if (target) |
| 1560 | return target; |
| 1561 | } |
| 1562 | |
| 1563 | if (widget->allocation.x <= x && |
| 1564 | x < widget->allocation.x + widget->allocation.width && |
| 1565 | widget->allocation.y <= y && |
| 1566 | y < widget->allocation.y + widget->allocation.height) { |
| 1567 | return widget; |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | return NULL; |
| 1571 | } |
| 1572 | |
Kristian Høgsberg | 441338c | 2012-01-10 13:52:34 -0500 | [diff] [blame] | 1573 | static struct widget * |
Pekka Paalanen | ac95f3e | 2013-02-13 16:17:17 +0200 | [diff] [blame] | 1574 | window_find_widget(struct window *window, int32_t x, int32_t y) |
| 1575 | { |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 1576 | struct surface *surface; |
| 1577 | struct widget *widget; |
| 1578 | |
| 1579 | wl_list_for_each(surface, &window->subsurface_list, link) { |
| 1580 | widget = widget_find_widget(surface->widget, x, y); |
| 1581 | if (widget) |
| 1582 | return widget; |
| 1583 | } |
| 1584 | |
| 1585 | return NULL; |
Pekka Paalanen | ac95f3e | 2013-02-13 16:17:17 +0200 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | static struct widget * |
Pekka Paalanen | 2d8a32a | 2013-02-13 16:17:19 +0200 | [diff] [blame] | 1589 | widget_create(struct window *window, struct surface *surface, void *data) |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 1590 | { |
Kristian Høgsberg | c51f799 | 2012-01-08 15:09:53 -0500 | [diff] [blame] | 1591 | struct widget *widget; |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 1592 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 1593 | widget = xzalloc(sizeof *widget); |
Kristian Høgsberg | 9a13dab | 2012-01-08 15:18:19 -0500 | [diff] [blame] | 1594 | widget->window = window; |
Pekka Paalanen | 2d8a32a | 2013-02-13 16:17:19 +0200 | [diff] [blame] | 1595 | widget->surface = surface; |
Kristian Høgsberg | c51f799 | 2012-01-08 15:09:53 -0500 | [diff] [blame] | 1596 | widget->user_data = data; |
Pekka Paalanen | 2d8a32a | 2013-02-13 16:17:19 +0200 | [diff] [blame] | 1597 | widget->allocation = surface->allocation; |
Kristian Høgsberg | 441338c | 2012-01-10 13:52:34 -0500 | [diff] [blame] | 1598 | wl_list_init(&widget->child_list); |
Kristian Høgsberg | 010f98b | 2012-02-23 17:30:45 -0500 | [diff] [blame] | 1599 | widget->opaque = 0; |
Tiago Vignatti | 82db9d8 | 2012-05-23 22:06:27 +0300 | [diff] [blame] | 1600 | widget->tooltip = NULL; |
| 1601 | widget->tooltip_count = 0; |
Kristian Høgsberg | bf74d52 | 2012-11-30 14:54:35 -0500 | [diff] [blame] | 1602 | widget->default_cursor = CURSOR_LEFT_PTR; |
Kristian Høgsberg | 441338c | 2012-01-10 13:52:34 -0500 | [diff] [blame] | 1603 | |
| 1604 | return widget; |
| 1605 | } |
| 1606 | |
| 1607 | struct widget * |
| 1608 | window_add_widget(struct window *window, void *data) |
| 1609 | { |
Pekka Paalanen | ac95f3e | 2013-02-13 16:17:17 +0200 | [diff] [blame] | 1610 | struct widget *widget; |
Kristian Høgsberg | 441338c | 2012-01-10 13:52:34 -0500 | [diff] [blame] | 1611 | |
Pekka Paalanen | 2d8a32a | 2013-02-13 16:17:19 +0200 | [diff] [blame] | 1612 | widget = widget_create(window, window->main_surface, data); |
Pekka Paalanen | ac95f3e | 2013-02-13 16:17:17 +0200 | [diff] [blame] | 1613 | wl_list_init(&widget->link); |
| 1614 | window->main_surface->widget = widget; |
| 1615 | |
| 1616 | return widget; |
Kristian Høgsberg | 441338c | 2012-01-10 13:52:34 -0500 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | struct widget * |
| 1620 | widget_add_widget(struct widget *parent, void *data) |
| 1621 | { |
| 1622 | struct widget *widget; |
| 1623 | |
Pekka Paalanen | 2d8a32a | 2013-02-13 16:17:19 +0200 | [diff] [blame] | 1624 | widget = widget_create(parent->window, parent->surface, data); |
Kristian Høgsberg | 441338c | 2012-01-10 13:52:34 -0500 | [diff] [blame] | 1625 | wl_list_insert(parent->child_list.prev, &widget->link); |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 1626 | |
Kristian Høgsberg | c51f799 | 2012-01-08 15:09:53 -0500 | [diff] [blame] | 1627 | return widget; |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | void |
Kristian Høgsberg | 441338c | 2012-01-10 13:52:34 -0500 | [diff] [blame] | 1631 | widget_destroy(struct widget *widget) |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 1632 | { |
Pekka Paalanen | e156fb6 | 2012-01-19 13:51:38 +0200 | [diff] [blame] | 1633 | struct display *display = widget->window->display; |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 1634 | struct surface *surface = widget->surface; |
Pekka Paalanen | e156fb6 | 2012-01-19 13:51:38 +0200 | [diff] [blame] | 1635 | struct input *input; |
| 1636 | |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 1637 | /* Destroy the sub-surface along with the root widget */ |
| 1638 | if (surface->widget == widget && surface->subsurface) |
| 1639 | surface_destroy(widget->surface); |
| 1640 | |
Tiago Vignatti | 82db9d8 | 2012-05-23 22:06:27 +0300 | [diff] [blame] | 1641 | if (widget->tooltip) { |
| 1642 | free(widget->tooltip); |
| 1643 | widget->tooltip = NULL; |
| 1644 | } |
| 1645 | |
Pekka Paalanen | e156fb6 | 2012-01-19 13:51:38 +0200 | [diff] [blame] | 1646 | wl_list_for_each(input, &display->input_list, link) { |
| 1647 | if (input->focus_widget == widget) |
| 1648 | input->focus_widget = NULL; |
| 1649 | } |
| 1650 | |
Kristian Høgsberg | 441338c | 2012-01-10 13:52:34 -0500 | [diff] [blame] | 1651 | wl_list_remove(&widget->link); |
| 1652 | free(widget); |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 1653 | } |
| 1654 | |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 1655 | void |
Kristian Høgsberg | bf74d52 | 2012-11-30 14:54:35 -0500 | [diff] [blame] | 1656 | widget_set_default_cursor(struct widget *widget, int cursor) |
| 1657 | { |
| 1658 | widget->default_cursor = cursor; |
| 1659 | } |
| 1660 | |
| 1661 | void |
Kristian Høgsberg | c51f799 | 2012-01-08 15:09:53 -0500 | [diff] [blame] | 1662 | widget_get_allocation(struct widget *widget, struct rectangle *allocation) |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 1663 | { |
Kristian Høgsberg | c51f799 | 2012-01-08 15:09:53 -0500 | [diff] [blame] | 1664 | *allocation = widget->allocation; |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 1665 | } |
| 1666 | |
| 1667 | void |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 1668 | widget_set_size(struct widget *widget, int32_t width, int32_t height) |
| 1669 | { |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 1670 | widget->allocation.width = width; |
| 1671 | widget->allocation.height = height; |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | void |
Kristian Høgsberg | c51f799 | 2012-01-08 15:09:53 -0500 | [diff] [blame] | 1675 | widget_set_allocation(struct widget *widget, |
| 1676 | int32_t x, int32_t y, int32_t width, int32_t height) |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 1677 | { |
Kristian Høgsberg | c51f799 | 2012-01-08 15:09:53 -0500 | [diff] [blame] | 1678 | widget->allocation.x = x; |
| 1679 | widget->allocation.y = y; |
Tiago Vignatti | c5528d8 | 2012-02-09 19:06:55 +0200 | [diff] [blame] | 1680 | widget_set_size(widget, width, height); |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 1681 | } |
| 1682 | |
Kristian Høgsberg | 010f98b | 2012-02-23 17:30:45 -0500 | [diff] [blame] | 1683 | void |
| 1684 | widget_set_transparent(struct widget *widget, int transparent) |
| 1685 | { |
| 1686 | widget->opaque = !transparent; |
| 1687 | } |
| 1688 | |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 1689 | void * |
Kristian Høgsberg | c51f799 | 2012-01-08 15:09:53 -0500 | [diff] [blame] | 1690 | widget_get_user_data(struct widget *widget) |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 1691 | { |
Kristian Høgsberg | c51f799 | 2012-01-08 15:09:53 -0500 | [diff] [blame] | 1692 | return widget->user_data; |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 1693 | } |
| 1694 | |
Pekka Paalanen | 0c4445b | 2013-02-13 16:17:23 +0200 | [diff] [blame] | 1695 | static cairo_surface_t * |
| 1696 | widget_get_cairo_surface(struct widget *widget) |
| 1697 | { |
| 1698 | struct surface *surface = widget->surface; |
| 1699 | struct window *window = widget->window; |
| 1700 | |
| 1701 | if (!surface->cairo_surface) { |
| 1702 | if (surface == window->main_surface) |
| 1703 | window_create_main_surface(window); |
| 1704 | else |
| 1705 | surface_create_surface(surface, 0, 0, 0); |
| 1706 | } |
| 1707 | |
| 1708 | return surface->cairo_surface; |
| 1709 | } |
| 1710 | |
Alexander Larsson | 15901f0 | 2013-05-22 14:41:25 +0200 | [diff] [blame] | 1711 | static void |
| 1712 | widget_cairo_update_transform(struct widget *widget, cairo_t *cr) |
| 1713 | { |
| 1714 | struct surface *surface = widget->surface; |
| 1715 | double angle; |
| 1716 | cairo_matrix_t m; |
| 1717 | enum wl_output_transform transform; |
| 1718 | int surface_width, surface_height; |
| 1719 | int translate_x, translate_y; |
Alexander Larsson | edddbd1 | 2013-05-24 13:09:43 +0200 | [diff] [blame] | 1720 | int32_t scale; |
Alexander Larsson | 15901f0 | 2013-05-22 14:41:25 +0200 | [diff] [blame] | 1721 | |
| 1722 | surface_width = surface->allocation.width; |
| 1723 | surface_height = surface->allocation.height; |
| 1724 | |
Alexander Larsson | 5e9b652 | 2013-05-22 14:41:28 +0200 | [diff] [blame] | 1725 | transform = surface->buffer_transform; |
Alexander Larsson | 2aaa8b7 | 2013-05-22 14:41:29 +0200 | [diff] [blame] | 1726 | scale = surface->buffer_scale; |
Alexander Larsson | 5e9b652 | 2013-05-22 14:41:28 +0200 | [diff] [blame] | 1727 | |
Alexander Larsson | 15901f0 | 2013-05-22 14:41:25 +0200 | [diff] [blame] | 1728 | switch (transform) { |
| 1729 | case WL_OUTPUT_TRANSFORM_FLIPPED: |
| 1730 | case WL_OUTPUT_TRANSFORM_FLIPPED_90: |
| 1731 | case WL_OUTPUT_TRANSFORM_FLIPPED_180: |
| 1732 | case WL_OUTPUT_TRANSFORM_FLIPPED_270: |
| 1733 | cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0); |
| 1734 | break; |
| 1735 | default: |
| 1736 | cairo_matrix_init_identity(&m); |
| 1737 | break; |
| 1738 | } |
| 1739 | |
| 1740 | switch (transform) { |
| 1741 | case WL_OUTPUT_TRANSFORM_NORMAL: |
| 1742 | default: |
| 1743 | angle = 0; |
| 1744 | translate_x = 0; |
| 1745 | translate_y = 0; |
| 1746 | break; |
| 1747 | case WL_OUTPUT_TRANSFORM_FLIPPED: |
| 1748 | angle = 0; |
| 1749 | translate_x = surface_width; |
| 1750 | translate_y = 0; |
| 1751 | break; |
| 1752 | case WL_OUTPUT_TRANSFORM_90: |
| 1753 | angle = M_PI_2; |
| 1754 | translate_x = surface_height; |
| 1755 | translate_y = 0; |
| 1756 | break; |
| 1757 | case WL_OUTPUT_TRANSFORM_FLIPPED_90: |
| 1758 | angle = M_PI_2; |
| 1759 | translate_x = surface_height; |
| 1760 | translate_y = surface_width; |
| 1761 | break; |
| 1762 | case WL_OUTPUT_TRANSFORM_180: |
| 1763 | angle = M_PI; |
| 1764 | translate_x = surface_width; |
| 1765 | translate_y = surface_height; |
| 1766 | break; |
| 1767 | case WL_OUTPUT_TRANSFORM_FLIPPED_180: |
| 1768 | angle = M_PI; |
| 1769 | translate_x = 0; |
| 1770 | translate_y = surface_height; |
| 1771 | break; |
| 1772 | case WL_OUTPUT_TRANSFORM_270: |
| 1773 | angle = M_PI + M_PI_2; |
| 1774 | translate_x = 0; |
| 1775 | translate_y = surface_width; |
| 1776 | break; |
| 1777 | case WL_OUTPUT_TRANSFORM_FLIPPED_270: |
| 1778 | angle = M_PI + M_PI_2; |
| 1779 | translate_x = 0; |
| 1780 | translate_y = 0; |
| 1781 | break; |
| 1782 | } |
| 1783 | |
Alexander Larsson | 2aaa8b7 | 2013-05-22 14:41:29 +0200 | [diff] [blame] | 1784 | cairo_scale(cr, scale, scale); |
Alexander Larsson | 15901f0 | 2013-05-22 14:41:25 +0200 | [diff] [blame] | 1785 | cairo_translate(cr, translate_x, translate_y); |
| 1786 | cairo_rotate(cr, angle); |
| 1787 | cairo_transform(cr, &m); |
| 1788 | } |
| 1789 | |
Pekka Paalanen | 0c4445b | 2013-02-13 16:17:23 +0200 | [diff] [blame] | 1790 | cairo_t * |
| 1791 | widget_cairo_create(struct widget *widget) |
| 1792 | { |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 1793 | struct surface *surface = widget->surface; |
Pekka Paalanen | 0c4445b | 2013-02-13 16:17:23 +0200 | [diff] [blame] | 1794 | cairo_surface_t *cairo_surface; |
| 1795 | cairo_t *cr; |
| 1796 | |
| 1797 | cairo_surface = widget_get_cairo_surface(widget); |
| 1798 | cr = cairo_create(cairo_surface); |
| 1799 | |
Alexander Larsson | 15901f0 | 2013-05-22 14:41:25 +0200 | [diff] [blame] | 1800 | widget_cairo_update_transform(widget, cr); |
| 1801 | |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 1802 | cairo_translate(cr, -surface->allocation.x, -surface->allocation.y); |
| 1803 | |
Pekka Paalanen | 0c4445b | 2013-02-13 16:17:23 +0200 | [diff] [blame] | 1804 | return cr; |
| 1805 | } |
| 1806 | |
Pekka Paalanen | 7ff7a80 | 2013-04-25 13:57:50 +0300 | [diff] [blame] | 1807 | struct wl_surface * |
| 1808 | widget_get_wl_surface(struct widget *widget) |
| 1809 | { |
| 1810 | return widget->surface->surface; |
| 1811 | } |
| 1812 | |
| 1813 | uint32_t |
| 1814 | widget_get_last_time(struct widget *widget) |
| 1815 | { |
| 1816 | return widget->surface->last_time; |
| 1817 | } |
| 1818 | |
| 1819 | void |
| 1820 | widget_input_region_add(struct widget *widget, const struct rectangle *rect) |
| 1821 | { |
| 1822 | struct wl_compositor *comp = widget->window->display->compositor; |
| 1823 | struct surface *surface = widget->surface; |
| 1824 | |
| 1825 | if (!surface->input_region) |
| 1826 | surface->input_region = wl_compositor_create_region(comp); |
| 1827 | |
| 1828 | if (rect) { |
| 1829 | wl_region_add(surface->input_region, |
| 1830 | rect->x, rect->y, rect->width, rect->height); |
| 1831 | } |
| 1832 | } |
| 1833 | |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 1834 | void |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 1835 | widget_set_resize_handler(struct widget *widget, |
| 1836 | widget_resize_handler_t handler) |
| 1837 | { |
| 1838 | widget->resize_handler = handler; |
| 1839 | } |
| 1840 | |
| 1841 | void |
| 1842 | widget_set_redraw_handler(struct widget *widget, |
| 1843 | widget_redraw_handler_t handler) |
| 1844 | { |
| 1845 | widget->redraw_handler = handler; |
| 1846 | } |
| 1847 | |
| 1848 | void |
Kristian Høgsberg | ee14323 | 2012-01-09 08:42:24 -0500 | [diff] [blame] | 1849 | widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler) |
Kristian Høgsberg | 9a13dab | 2012-01-08 15:18:19 -0500 | [diff] [blame] | 1850 | { |
Kristian Høgsberg | ee14323 | 2012-01-09 08:42:24 -0500 | [diff] [blame] | 1851 | widget->enter_handler = handler; |
| 1852 | } |
| 1853 | |
| 1854 | void |
| 1855 | widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler) |
| 1856 | { |
| 1857 | widget->leave_handler = handler; |
Kristian Høgsberg | 9a13dab | 2012-01-08 15:18:19 -0500 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | void |
Kristian Høgsberg | 04e9834 | 2012-01-09 09:36:16 -0500 | [diff] [blame] | 1861 | widget_set_motion_handler(struct widget *widget, |
| 1862 | widget_motion_handler_t handler) |
| 1863 | { |
| 1864 | widget->motion_handler = handler; |
| 1865 | } |
| 1866 | |
| 1867 | void |
Kristian Høgsberg | a8a0db3 | 2012-01-09 11:12:05 -0500 | [diff] [blame] | 1868 | widget_set_button_handler(struct widget *widget, |
| 1869 | widget_button_handler_t handler) |
| 1870 | { |
| 1871 | widget->button_handler = handler; |
| 1872 | } |
| 1873 | |
| 1874 | void |
Rusty Lynch | 041815a | 2013-08-08 21:20:38 -0700 | [diff] [blame] | 1875 | widget_set_touch_up_handler(struct widget *widget, |
| 1876 | widget_touch_up_handler_t handler) |
| 1877 | { |
| 1878 | widget->touch_up_handler = handler; |
| 1879 | } |
| 1880 | |
| 1881 | void |
| 1882 | widget_set_touch_down_handler(struct widget *widget, |
| 1883 | widget_touch_down_handler_t handler) |
| 1884 | { |
| 1885 | widget->touch_down_handler = handler; |
| 1886 | } |
| 1887 | |
| 1888 | void |
| 1889 | widget_set_touch_motion_handler(struct widget *widget, |
| 1890 | widget_touch_motion_handler_t handler) |
| 1891 | { |
| 1892 | widget->touch_motion_handler = handler; |
| 1893 | } |
| 1894 | |
| 1895 | void |
| 1896 | widget_set_touch_frame_handler(struct widget *widget, |
| 1897 | widget_touch_frame_handler_t handler) |
| 1898 | { |
| 1899 | widget->touch_frame_handler = handler; |
| 1900 | } |
| 1901 | |
| 1902 | void |
| 1903 | widget_set_touch_cancel_handler(struct widget *widget, |
| 1904 | widget_touch_cancel_handler_t handler) |
| 1905 | { |
| 1906 | widget->touch_cancel_handler = handler; |
| 1907 | } |
| 1908 | |
| 1909 | void |
Philipp Brüschweiler | 7e0cc54 | 2012-08-14 11:02:41 +0200 | [diff] [blame] | 1910 | widget_set_axis_handler(struct widget *widget, |
| 1911 | widget_axis_handler_t handler) |
| 1912 | { |
| 1913 | widget->axis_handler = handler; |
| 1914 | } |
| 1915 | |
Pekka Paalanen | 40cb67b | 2013-04-25 13:57:49 +0300 | [diff] [blame] | 1916 | static void |
| 1917 | window_schedule_redraw_task(struct window *window); |
| 1918 | |
Philipp Brüschweiler | 7e0cc54 | 2012-08-14 11:02:41 +0200 | [diff] [blame] | 1919 | void |
Kristian Høgsberg | 9a13dab | 2012-01-08 15:18:19 -0500 | [diff] [blame] | 1920 | widget_schedule_redraw(struct widget *widget) |
| 1921 | { |
Pekka Paalanen | 7123388 | 2013-04-25 13:57:53 +0300 | [diff] [blame] | 1922 | DBG_OBJ(widget->surface->surface, "widget %p\n", widget); |
Pekka Paalanen | 40cb67b | 2013-04-25 13:57:49 +0300 | [diff] [blame] | 1923 | widget->surface->redraw_needed = 1; |
| 1924 | window_schedule_redraw_task(widget->window); |
Kristian Høgsberg | 9a13dab | 2012-01-08 15:18:19 -0500 | [diff] [blame] | 1925 | } |
| 1926 | |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 1927 | cairo_surface_t * |
| 1928 | window_get_surface(struct window *window) |
| 1929 | { |
Pekka Paalanen | 0c4445b | 2013-02-13 16:17:23 +0200 | [diff] [blame] | 1930 | cairo_surface_t *cairo_surface; |
| 1931 | |
| 1932 | cairo_surface = widget_get_cairo_surface(window->main_surface->widget); |
| 1933 | |
| 1934 | return cairo_surface_reference(cairo_surface); |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 1935 | } |
| 1936 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 1937 | struct wl_surface * |
| 1938 | window_get_wl_surface(struct window *window) |
| 1939 | { |
Pekka Paalanen | 4e37374 | 2013-02-13 16:17:13 +0200 | [diff] [blame] | 1940 | return window->main_surface->surface; |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 1941 | } |
| 1942 | |
Pekka Paalanen | 9d1613e | 2011-11-25 12:09:16 +0200 | [diff] [blame] | 1943 | struct wl_shell_surface * |
| 1944 | window_get_wl_shell_surface(struct window *window) |
| 1945 | { |
| 1946 | return window->shell_surface; |
| 1947 | } |
| 1948 | |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 1949 | static void |
Tiago Vignatti | 82db9d8 | 2012-05-23 22:06:27 +0300 | [diff] [blame] | 1950 | tooltip_redraw_handler(struct widget *widget, void *data) |
| 1951 | { |
| 1952 | cairo_t *cr; |
| 1953 | const int32_t r = 3; |
| 1954 | struct tooltip *tooltip = data; |
| 1955 | int32_t width, height; |
Tiago Vignatti | 82db9d8 | 2012-05-23 22:06:27 +0300 | [diff] [blame] | 1956 | |
Pekka Paalanen | 0c4445b | 2013-02-13 16:17:23 +0200 | [diff] [blame] | 1957 | cr = widget_cairo_create(widget); |
Tiago Vignatti | 82db9d8 | 2012-05-23 22:06:27 +0300 | [diff] [blame] | 1958 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 1959 | cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0); |
| 1960 | cairo_paint(cr); |
| 1961 | |
Pekka Paalanen | 0a9686f | 2013-02-13 16:17:22 +0200 | [diff] [blame] | 1962 | width = widget->allocation.width; |
| 1963 | height = widget->allocation.height; |
Tiago Vignatti | 82db9d8 | 2012-05-23 22:06:27 +0300 | [diff] [blame] | 1964 | rounded_rect(cr, 0, 0, width, height, r); |
| 1965 | |
| 1966 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 1967 | cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8); |
| 1968 | cairo_fill(cr); |
| 1969 | |
| 1970 | cairo_set_source_rgb(cr, 1.0, 1.0, 1.0); |
| 1971 | cairo_move_to(cr, 10, 16); |
| 1972 | cairo_show_text(cr, tooltip->entry); |
| 1973 | cairo_destroy(cr); |
| 1974 | } |
| 1975 | |
| 1976 | static cairo_text_extents_t |
| 1977 | get_text_extents(struct tooltip *tooltip) |
| 1978 | { |
Tiago Vignatti | 82db9d8 | 2012-05-23 22:06:27 +0300 | [diff] [blame] | 1979 | cairo_t *cr; |
| 1980 | cairo_text_extents_t extents; |
| 1981 | |
Pekka Paalanen | 3cbb089 | 2012-11-19 17:16:01 +0200 | [diff] [blame] | 1982 | /* Use the dummy_surface because tooltip's surface was not |
| 1983 | * created yet, and parent does not have a valid surface |
| 1984 | * outside repaint, either. |
| 1985 | */ |
| 1986 | cr = cairo_create(tooltip->window->display->dummy_surface); |
Tiago Vignatti | 82db9d8 | 2012-05-23 22:06:27 +0300 | [diff] [blame] | 1987 | cairo_text_extents(cr, tooltip->entry, &extents); |
| 1988 | cairo_destroy(cr); |
| 1989 | |
| 1990 | return extents; |
| 1991 | } |
| 1992 | |
| 1993 | static int |
| 1994 | window_create_tooltip(struct tooltip *tooltip) |
| 1995 | { |
| 1996 | struct widget *parent = tooltip->parent; |
| 1997 | struct display *display = parent->window->display; |
| 1998 | struct window *window; |
| 1999 | const int offset_y = 27; |
| 2000 | const int margin = 3; |
| 2001 | cairo_text_extents_t extents; |
| 2002 | |
| 2003 | if (tooltip->widget) |
| 2004 | return 0; |
| 2005 | |
| 2006 | window = window_create_transient(display, parent->window, tooltip->x, |
| 2007 | tooltip->y + offset_y, |
| 2008 | WL_SHELL_SURFACE_TRANSIENT_INACTIVE); |
| 2009 | if (!window) |
| 2010 | return -1; |
| 2011 | |
| 2012 | tooltip->window = window; |
| 2013 | tooltip->widget = window_add_widget(tooltip->window, tooltip); |
| 2014 | |
| 2015 | extents = get_text_extents(tooltip); |
| 2016 | widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler); |
| 2017 | window_schedule_resize(window, extents.width + 20, 20 + margin * 2); |
| 2018 | |
| 2019 | return 0; |
| 2020 | } |
| 2021 | |
| 2022 | void |
| 2023 | widget_destroy_tooltip(struct widget *parent) |
| 2024 | { |
| 2025 | struct tooltip *tooltip = parent->tooltip; |
| 2026 | |
| 2027 | parent->tooltip_count = 0; |
| 2028 | if (!tooltip) |
| 2029 | return; |
| 2030 | |
| 2031 | if (tooltip->widget) { |
| 2032 | widget_destroy(tooltip->widget); |
| 2033 | window_destroy(tooltip->window); |
| 2034 | tooltip->widget = NULL; |
| 2035 | tooltip->window = NULL; |
| 2036 | } |
| 2037 | |
| 2038 | close(tooltip->tooltip_fd); |
| 2039 | free(tooltip->entry); |
| 2040 | free(tooltip); |
| 2041 | parent->tooltip = NULL; |
| 2042 | } |
| 2043 | |
| 2044 | static void |
| 2045 | tooltip_func(struct task *task, uint32_t events) |
| 2046 | { |
| 2047 | struct tooltip *tooltip = |
| 2048 | container_of(task, struct tooltip, tooltip_task); |
| 2049 | uint64_t exp; |
| 2050 | |
Martin Olsson | 8df662a | 2012-07-08 03:03:47 +0200 | [diff] [blame] | 2051 | if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t)) |
| 2052 | abort(); |
Tiago Vignatti | 82db9d8 | 2012-05-23 22:06:27 +0300 | [diff] [blame] | 2053 | window_create_tooltip(tooltip); |
| 2054 | } |
| 2055 | |
| 2056 | #define TOOLTIP_TIMEOUT 500 |
| 2057 | static int |
| 2058 | tooltip_timer_reset(struct tooltip *tooltip) |
| 2059 | { |
| 2060 | struct itimerspec its; |
| 2061 | |
| 2062 | its.it_interval.tv_sec = 0; |
| 2063 | its.it_interval.tv_nsec = 0; |
| 2064 | its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000; |
| 2065 | its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000; |
| 2066 | if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) { |
| 2067 | fprintf(stderr, "could not set timerfd\n: %m"); |
| 2068 | return -1; |
| 2069 | } |
| 2070 | |
| 2071 | return 0; |
| 2072 | } |
| 2073 | |
| 2074 | int |
| 2075 | widget_set_tooltip(struct widget *parent, char *entry, float x, float y) |
| 2076 | { |
| 2077 | struct tooltip *tooltip = parent->tooltip; |
| 2078 | |
| 2079 | parent->tooltip_count++; |
| 2080 | if (tooltip) { |
| 2081 | tooltip->x = x; |
| 2082 | tooltip->y = y; |
| 2083 | tooltip_timer_reset(tooltip); |
| 2084 | return 0; |
| 2085 | } |
| 2086 | |
| 2087 | /* the handler might be triggered too fast via input device motion, so |
| 2088 | * we need this check here to make sure tooltip is fully initialized */ |
| 2089 | if (parent->tooltip_count > 1) |
| 2090 | return 0; |
| 2091 | |
| 2092 | tooltip = malloc(sizeof *tooltip); |
| 2093 | if (!tooltip) |
| 2094 | return -1; |
| 2095 | |
| 2096 | parent->tooltip = tooltip; |
| 2097 | tooltip->parent = parent; |
| 2098 | tooltip->widget = NULL; |
| 2099 | tooltip->window = NULL; |
| 2100 | tooltip->x = x; |
| 2101 | tooltip->y = y; |
| 2102 | tooltip->entry = strdup(entry); |
| 2103 | tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC); |
| 2104 | if (tooltip->tooltip_fd < 0) { |
| 2105 | fprintf(stderr, "could not create timerfd\n: %m"); |
| 2106 | return -1; |
| 2107 | } |
| 2108 | |
| 2109 | tooltip->tooltip_task.run = tooltip_func; |
| 2110 | display_watch_fd(parent->window->display, tooltip->tooltip_fd, |
| 2111 | EPOLLIN, &tooltip->tooltip_task); |
| 2112 | tooltip_timer_reset(tooltip); |
| 2113 | |
| 2114 | return 0; |
| 2115 | } |
| 2116 | |
| 2117 | static void |
Jonas Ådahl | 14c92ff | 2012-08-29 22:13:02 +0200 | [diff] [blame] | 2118 | workspace_manager_state(void *data, |
| 2119 | struct workspace_manager *workspace_manager, |
| 2120 | uint32_t current, |
| 2121 | uint32_t count) |
| 2122 | { |
| 2123 | struct display *display = data; |
| 2124 | |
| 2125 | display->workspace = current; |
| 2126 | display->workspace_count = count; |
| 2127 | } |
| 2128 | |
| 2129 | static const struct workspace_manager_listener workspace_manager_listener = { |
| 2130 | workspace_manager_state |
| 2131 | }; |
| 2132 | |
| 2133 | static void |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2134 | frame_resize_handler(struct widget *widget, |
| 2135 | int32_t width, int32_t height, void *data) |
Kristian Høgsberg | da275dd | 2010-08-16 17:47:07 -0400 | [diff] [blame] | 2136 | { |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2137 | struct window_frame *frame = data; |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2138 | struct widget *child = frame->child; |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2139 | struct rectangle interior; |
| 2140 | struct rectangle input; |
| 2141 | struct rectangle opaque; |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2142 | |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2143 | if (widget->window->type == TYPE_FULLSCREEN) { |
| 2144 | interior.x = 0; |
| 2145 | interior.y = 0; |
| 2146 | interior.width = width; |
| 2147 | interior.height = height; |
| 2148 | } else { |
| 2149 | if (widget->window->type == TYPE_MAXIMIZED) { |
| 2150 | frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED); |
| 2151 | } else { |
| 2152 | frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED); |
| 2153 | } |
Scott Moreau | c6a7e4b | 2012-09-28 02:45:06 -0600 | [diff] [blame] | 2154 | |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2155 | frame_resize(frame->frame, width, height); |
| 2156 | frame_interior(frame->frame, &interior.x, &interior.y, |
| 2157 | &interior.width, &interior.height); |
Kristian Høgsberg | 010f98b | 2012-02-23 17:30:45 -0500 | [diff] [blame] | 2158 | } |
| 2159 | |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2160 | widget_set_allocation(child, interior.x, interior.y, |
| 2161 | interior.width, interior.height); |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2162 | |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2163 | if (child->resize_handler) { |
| 2164 | child->resize_handler(child, interior.width, interior.height, |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2165 | child->user_data); |
| 2166 | |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2167 | if (widget->window->type == TYPE_FULLSCREEN) { |
| 2168 | width = child->allocation.width; |
| 2169 | height = child->allocation.height; |
| 2170 | } else { |
| 2171 | frame_resize_inside(frame->frame, |
| 2172 | child->allocation.width, |
| 2173 | child->allocation.height); |
| 2174 | width = frame_width(frame->frame); |
| 2175 | height = frame_height(frame->frame); |
| 2176 | } |
Kristian Høgsberg | 023be10 | 2012-07-31 11:59:12 -0400 | [diff] [blame] | 2177 | } |
| 2178 | |
Scott Moreau | f7e498c | 2012-05-14 11:39:29 -0600 | [diff] [blame] | 2179 | widget_set_allocation(widget, 0, 0, width, height); |
Kristian Høgsberg | f10df85 | 2012-02-28 21:52:12 -0500 | [diff] [blame] | 2180 | |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2181 | widget->surface->input_region = |
| 2182 | wl_compositor_create_region(widget->window->display->compositor); |
| 2183 | if (widget->window->type != TYPE_FULLSCREEN) { |
| 2184 | frame_input_rect(frame->frame, &input.x, &input.y, |
| 2185 | &input.width, &input.height); |
| 2186 | wl_region_add(widget->surface->input_region, |
| 2187 | input.x, input.y, input.width, input.height); |
Pekka Vuorela | 4e363d2 | 2012-09-26 15:05:45 +0300 | [diff] [blame] | 2188 | } else { |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2189 | wl_region_add(widget->surface->input_region, 0, 0, width, height); |
Pekka Vuorela | 4e363d2 | 2012-09-26 15:05:45 +0300 | [diff] [blame] | 2190 | } |
| 2191 | |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2192 | widget_set_allocation(widget, 0, 0, width, height); |
Pekka Vuorela | 4e363d2 | 2012-09-26 15:05:45 +0300 | [diff] [blame] | 2193 | |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2194 | if (child->opaque) { |
Kristian Høgsberg | 598477d | 2013-10-16 16:06:18 -0700 | [diff] [blame] | 2195 | if (widget->window->type != TYPE_FULLSCREEN) { |
| 2196 | frame_opaque_rect(frame->frame, &opaque.x, &opaque.y, |
| 2197 | &opaque.width, &opaque.height); |
| 2198 | |
| 2199 | wl_region_add(widget->surface->opaque_region, |
| 2200 | opaque.x, opaque.y, |
| 2201 | opaque.width, opaque.height); |
| 2202 | } else { |
| 2203 | wl_region_add(widget->surface->opaque_region, |
| 2204 | 0, 0, width, height); |
| 2205 | } |
Martin Minarik | 1998b15 | 2012-05-10 02:04:35 +0200 | [diff] [blame] | 2206 | } |
| 2207 | |
Martin Minarik | 1998b15 | 2012-05-10 02:04:35 +0200 | [diff] [blame] | 2208 | |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2209 | widget_schedule_redraw(widget); |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | static void |
| 2213 | frame_redraw_handler(struct widget *widget, void *data) |
| 2214 | { |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2215 | cairo_t *cr; |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2216 | struct window_frame *frame = data; |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2217 | struct window *window = widget->window; |
| 2218 | |
Kristian Høgsberg | 2675dc1 | 2012-02-16 22:57:21 -0500 | [diff] [blame] | 2219 | if (window->type == TYPE_FULLSCREEN) |
| 2220 | return; |
| 2221 | |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2222 | if (window->focus_count) { |
| 2223 | frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE); |
| 2224 | } else { |
| 2225 | frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE); |
| 2226 | } |
| 2227 | |
Pekka Paalanen | 0c4445b | 2013-02-13 16:17:23 +0200 | [diff] [blame] | 2228 | cr = widget_cairo_create(widget); |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2229 | |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2230 | frame_repaint(frame->frame, cr); |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2231 | |
| 2232 | cairo_destroy(cr); |
| 2233 | } |
| 2234 | |
| 2235 | static int |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2236 | frame_get_pointer_image_for_location(struct window_frame *frame, |
| 2237 | enum theme_location location) |
Kristian Høgsberg | da275dd | 2010-08-16 17:47:07 -0400 | [diff] [blame] | 2238 | { |
Scott Moreau | c6a7e4b | 2012-09-28 02:45:06 -0600 | [diff] [blame] | 2239 | struct window *window = frame->widget->window; |
Kristian Høgsberg | da275dd | 2010-08-16 17:47:07 -0400 | [diff] [blame] | 2240 | |
Kristian Høgsberg | e994edd | 2013-02-14 16:31:42 -0500 | [diff] [blame] | 2241 | if (window->type != TYPE_TOPLEVEL) |
| 2242 | return CURSOR_LEFT_PTR; |
| 2243 | |
Kristian Høgsberg | da275dd | 2010-08-16 17:47:07 -0400 | [diff] [blame] | 2244 | switch (location) { |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 2245 | case THEME_LOCATION_RESIZING_TOP: |
Ander Conselvan de Oliveira | dc8c8fc | 2012-05-25 16:01:41 +0300 | [diff] [blame] | 2246 | return CURSOR_TOP; |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 2247 | case THEME_LOCATION_RESIZING_BOTTOM: |
Ander Conselvan de Oliveira | dc8c8fc | 2012-05-25 16:01:41 +0300 | [diff] [blame] | 2248 | return CURSOR_BOTTOM; |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 2249 | case THEME_LOCATION_RESIZING_LEFT: |
Ander Conselvan de Oliveira | dc8c8fc | 2012-05-25 16:01:41 +0300 | [diff] [blame] | 2250 | return CURSOR_LEFT; |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 2251 | case THEME_LOCATION_RESIZING_RIGHT: |
Ander Conselvan de Oliveira | dc8c8fc | 2012-05-25 16:01:41 +0300 | [diff] [blame] | 2252 | return CURSOR_RIGHT; |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 2253 | case THEME_LOCATION_RESIZING_TOP_LEFT: |
Ander Conselvan de Oliveira | dc8c8fc | 2012-05-25 16:01:41 +0300 | [diff] [blame] | 2254 | return CURSOR_TOP_LEFT; |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 2255 | case THEME_LOCATION_RESIZING_TOP_RIGHT: |
Ander Conselvan de Oliveira | dc8c8fc | 2012-05-25 16:01:41 +0300 | [diff] [blame] | 2256 | return CURSOR_TOP_RIGHT; |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 2257 | case THEME_LOCATION_RESIZING_BOTTOM_LEFT: |
Ander Conselvan de Oliveira | dc8c8fc | 2012-05-25 16:01:41 +0300 | [diff] [blame] | 2258 | return CURSOR_BOTTOM_LEFT; |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 2259 | case THEME_LOCATION_RESIZING_BOTTOM_RIGHT: |
Ander Conselvan de Oliveira | dc8c8fc | 2012-05-25 16:01:41 +0300 | [diff] [blame] | 2260 | return CURSOR_BOTTOM_RIGHT; |
Kristian Høgsberg | f96e6c0 | 2012-05-22 16:38:53 -0400 | [diff] [blame] | 2261 | case THEME_LOCATION_EXTERIOR: |
| 2262 | case THEME_LOCATION_TITLEBAR: |
Kristian Høgsberg | da275dd | 2010-08-16 17:47:07 -0400 | [diff] [blame] | 2263 | default: |
Ander Conselvan de Oliveira | dc8c8fc | 2012-05-25 16:01:41 +0300 | [diff] [blame] | 2264 | return CURSOR_LEFT_PTR; |
Kristian Høgsberg | da275dd | 2010-08-16 17:47:07 -0400 | [diff] [blame] | 2265 | } |
Kristian Høgsberg | cd9ac1d | 2011-12-15 09:14:34 -0500 | [diff] [blame] | 2266 | } |
| 2267 | |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2268 | static void |
Kristian Høgsberg | 67b8215 | 2013-10-23 16:52:05 -0700 | [diff] [blame] | 2269 | frame_menu_func(struct window *window, |
| 2270 | struct input *input, int index, void *data) |
Kristian Høgsberg | cd9ac1d | 2011-12-15 09:14:34 -0500 | [diff] [blame] | 2271 | { |
Jonas Ådahl | 14c92ff | 2012-08-29 22:13:02 +0200 | [diff] [blame] | 2272 | struct display *display; |
| 2273 | |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2274 | switch (index) { |
| 2275 | case 0: /* close */ |
| 2276 | if (window->close_handler) |
| 2277 | window->close_handler(window->parent, |
| 2278 | window->user_data); |
| 2279 | else |
| 2280 | display_exit(window->display); |
| 2281 | break; |
Kristian Høgsberg | efb9488 | 2012-10-30 18:07:02 -0400 | [diff] [blame] | 2282 | case 1: /* move to workspace above */ |
Jonas Ådahl | 14c92ff | 2012-08-29 22:13:02 +0200 | [diff] [blame] | 2283 | display = window->display; |
| 2284 | if (display->workspace > 0) |
Pekka Paalanen | 4e37374 | 2013-02-13 16:17:13 +0200 | [diff] [blame] | 2285 | workspace_manager_move_surface( |
| 2286 | display->workspace_manager, |
| 2287 | window->main_surface->surface, |
| 2288 | display->workspace - 1); |
Jonas Ådahl | 14c92ff | 2012-08-29 22:13:02 +0200 | [diff] [blame] | 2289 | break; |
Kristian Høgsberg | efb9488 | 2012-10-30 18:07:02 -0400 | [diff] [blame] | 2290 | case 2: /* move to workspace below */ |
Jonas Ådahl | 14c92ff | 2012-08-29 22:13:02 +0200 | [diff] [blame] | 2291 | display = window->display; |
Philipp Brüschweiler | 067abf6 | 2012-09-01 16:03:05 +0200 | [diff] [blame] | 2292 | if (display->workspace < display->workspace_count - 1) |
Pekka Paalanen | 4e37374 | 2013-02-13 16:17:13 +0200 | [diff] [blame] | 2293 | workspace_manager_move_surface( |
| 2294 | display->workspace_manager, |
| 2295 | window->main_surface->surface, |
| 2296 | display->workspace + 1); |
Kristian Høgsberg | 0f7a285 | 2012-11-05 20:20:53 -0500 | [diff] [blame] | 2297 | break; |
Kristian Høgsberg | efb9488 | 2012-10-30 18:07:02 -0400 | [diff] [blame] | 2298 | case 3: /* fullscreen */ |
| 2299 | /* we don't have a way to get out of fullscreen for now */ |
| 2300 | if (window->fullscreen_handler) |
| 2301 | window->fullscreen_handler(window, window->user_data); |
| 2302 | break; |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2303 | } |
| 2304 | } |
Kristian Høgsberg | da275dd | 2010-08-16 17:47:07 -0400 | [diff] [blame] | 2305 | |
Kristian Høgsberg | d31fcab | 2012-01-31 09:53:44 -0500 | [diff] [blame] | 2306 | void |
| 2307 | window_show_frame_menu(struct window *window, |
| 2308 | struct input *input, uint32_t time) |
| 2309 | { |
| 2310 | int32_t x, y; |
Kristian Høgsberg | efb9488 | 2012-10-30 18:07:02 -0400 | [diff] [blame] | 2311 | int count; |
Kristian Høgsberg | d31fcab | 2012-01-31 09:53:44 -0500 | [diff] [blame] | 2312 | |
| 2313 | static const char *entries[] = { |
Kristian Høgsberg | efb9488 | 2012-10-30 18:07:02 -0400 | [diff] [blame] | 2314 | "Close", |
| 2315 | "Move to workspace above", "Move to workspace below", |
| 2316 | "Fullscreen" |
Kristian Høgsberg | d31fcab | 2012-01-31 09:53:44 -0500 | [diff] [blame] | 2317 | }; |
| 2318 | |
Kristian Høgsberg | efb9488 | 2012-10-30 18:07:02 -0400 | [diff] [blame] | 2319 | if (window->fullscreen_handler) |
| 2320 | count = ARRAY_LENGTH(entries); |
| 2321 | else |
| 2322 | count = ARRAY_LENGTH(entries) - 1; |
| 2323 | |
Kristian Høgsberg | d31fcab | 2012-01-31 09:53:44 -0500 | [diff] [blame] | 2324 | input_get_position(input, &x, &y); |
| 2325 | window_show_menu(window->display, input, time, window, |
Kristian Høgsberg | efb9488 | 2012-10-30 18:07:02 -0400 | [diff] [blame] | 2326 | x - 10, y - 10, frame_menu_func, entries, count); |
Kristian Høgsberg | d31fcab | 2012-01-31 09:53:44 -0500 | [diff] [blame] | 2327 | } |
| 2328 | |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2329 | static int |
| 2330 | frame_enter_handler(struct widget *widget, |
Kristian Høgsberg | 80680c7 | 2012-05-10 12:21:37 -0400 | [diff] [blame] | 2331 | struct input *input, float x, float y, void *data) |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2332 | { |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2333 | struct window_frame *frame = data; |
| 2334 | enum theme_location location; |
| 2335 | |
| 2336 | location = frame_pointer_enter(frame->frame, input, x, y); |
| 2337 | if (frame_status(frame->frame) & FRAME_STATUS_REPAINT) |
| 2338 | widget_schedule_redraw(frame->widget); |
| 2339 | |
| 2340 | return frame_get_pointer_image_for_location(data, location); |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2341 | } |
Kristian Høgsberg | 7d80406 | 2010-09-07 21:50:06 -0400 | [diff] [blame] | 2342 | |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2343 | static int |
| 2344 | frame_motion_handler(struct widget *widget, |
| 2345 | struct input *input, uint32_t time, |
Kristian Høgsberg | 80680c7 | 2012-05-10 12:21:37 -0400 | [diff] [blame] | 2346 | float x, float y, void *data) |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2347 | { |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2348 | struct window_frame *frame = data; |
| 2349 | enum theme_location location; |
| 2350 | |
| 2351 | location = frame_pointer_motion(frame->frame, input, x, y); |
| 2352 | if (frame_status(frame->frame) & FRAME_STATUS_REPAINT) |
| 2353 | widget_schedule_redraw(frame->widget); |
| 2354 | |
| 2355 | return frame_get_pointer_image_for_location(data, location); |
| 2356 | } |
| 2357 | |
| 2358 | static void |
| 2359 | frame_leave_handler(struct widget *widget, |
| 2360 | struct input *input, void *data) |
| 2361 | { |
| 2362 | struct window_frame *frame = data; |
| 2363 | |
| 2364 | frame_pointer_leave(frame->frame, input); |
| 2365 | if (frame_status(frame->frame) & FRAME_STATUS_REPAINT) |
| 2366 | widget_schedule_redraw(frame->widget); |
| 2367 | } |
| 2368 | |
| 2369 | static void |
| 2370 | frame_handle_status(struct window_frame *frame, struct input *input, |
| 2371 | uint32_t time, enum theme_location location) |
| 2372 | { |
| 2373 | struct window *window = frame->widget->window; |
| 2374 | uint32_t status; |
| 2375 | |
| 2376 | status = frame_status(frame->frame); |
| 2377 | if (status & FRAME_STATUS_REPAINT) |
| 2378 | widget_schedule_redraw(frame->widget); |
| 2379 | |
| 2380 | if (status & FRAME_STATUS_MINIMIZE) |
| 2381 | fprintf(stderr,"Minimize stub\n"); |
| 2382 | |
| 2383 | if (status & FRAME_STATUS_MENU) { |
| 2384 | window_show_frame_menu(window, input, time); |
| 2385 | frame_status_clear(frame->frame, FRAME_STATUS_MENU); |
| 2386 | } |
| 2387 | |
| 2388 | if (status & FRAME_STATUS_MAXIMIZE) { |
| 2389 | window_set_maximized(window, window->type != TYPE_MAXIMIZED); |
| 2390 | frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE); |
| 2391 | } |
| 2392 | |
| 2393 | if (status & FRAME_STATUS_CLOSE) { |
| 2394 | if (window->close_handler) |
| 2395 | window->close_handler(window->parent, |
| 2396 | window->user_data); |
| 2397 | else |
| 2398 | display_exit(window->display); |
Jason Ekstrand | 4a7409a | 2013-10-27 21:32:54 -0500 | [diff] [blame^] | 2399 | return; |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | if ((status & FRAME_STATUS_MOVE) && window->shell_surface) { |
| 2403 | input_ungrab(input); |
| 2404 | wl_shell_surface_move(window->shell_surface, |
| 2405 | input_get_seat(input), |
| 2406 | window->display->serial); |
| 2407 | |
| 2408 | frame_status_clear(frame->frame, FRAME_STATUS_MOVE); |
| 2409 | } |
| 2410 | |
| 2411 | if ((status & FRAME_STATUS_RESIZE) && window->shell_surface) { |
| 2412 | input_ungrab(input); |
| 2413 | |
| 2414 | window->resizing = 1; |
| 2415 | wl_shell_surface_resize(window->shell_surface, |
| 2416 | input_get_seat(input), |
| 2417 | window->display->serial, |
| 2418 | location); |
| 2419 | |
| 2420 | frame_status_clear(frame->frame, FRAME_STATUS_RESIZE); |
| 2421 | } |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2422 | } |
Rob Bradford | 8bd35c7 | 2011-10-25 12:20:51 +0100 | [diff] [blame] | 2423 | |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2424 | static void |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2425 | frame_button_handler(struct widget *widget, |
| 2426 | struct input *input, uint32_t time, |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 2427 | uint32_t button, enum wl_pointer_button_state state, |
| 2428 | void *data) |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2429 | |
| 2430 | { |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2431 | struct window_frame *frame = data; |
| 2432 | enum theme_location location; |
Kristian Høgsberg | 1103a1a | 2012-04-03 12:00:48 -0400 | [diff] [blame] | 2433 | |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2434 | location = frame_pointer_button(frame->frame, input, button, state); |
| 2435 | frame_handle_status(frame, input, time, location); |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2436 | } |
| 2437 | |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2438 | static void |
Rusty Lynch | 1084da5 | 2013-08-15 09:10:08 -0700 | [diff] [blame] | 2439 | frame_touch_down_handler(struct widget *widget, struct input *input, |
| 2440 | uint32_t serial, uint32_t time, int32_t id, |
| 2441 | float x, float y, void *data) |
| 2442 | { |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2443 | struct window_frame *frame = data; |
| 2444 | |
| 2445 | frame_touch_down(frame->frame, input, id, x, y); |
| 2446 | frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA); |
| 2447 | } |
| 2448 | |
| 2449 | static void |
| 2450 | frame_touch_up_handler(struct widget *widget, |
| 2451 | struct input *input, uint32_t serial, uint32_t time, |
| 2452 | int32_t id, void *data) |
| 2453 | { |
| 2454 | struct window_frame *frame = data; |
| 2455 | |
| 2456 | frame_touch_up(frame->frame, input, id); |
| 2457 | frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA); |
Rusty Lynch | 1084da5 | 2013-08-15 09:10:08 -0700 | [diff] [blame] | 2458 | } |
| 2459 | |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2460 | struct widget * |
Jason Ekstrand | ee7fefc | 2013-10-13 19:08:38 -0500 | [diff] [blame] | 2461 | window_frame_create(struct window *window, void *data) |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2462 | { |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2463 | struct window_frame *frame; |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2464 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 2465 | frame = xzalloc(sizeof *frame); |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2466 | frame->frame = frame_create(window->display->theme, 0, 0, |
| 2467 | FRAME_BUTTON_ALL, window->title); |
| 2468 | |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2469 | frame->widget = window_add_widget(window, frame); |
| 2470 | frame->child = widget_add_widget(frame->widget, data); |
Martin Minarik | 1998b15 | 2012-05-10 02:04:35 +0200 | [diff] [blame] | 2471 | |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2472 | widget_set_redraw_handler(frame->widget, frame_redraw_handler); |
| 2473 | widget_set_resize_handler(frame->widget, frame_resize_handler); |
| 2474 | widget_set_enter_handler(frame->widget, frame_enter_handler); |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2475 | widget_set_leave_handler(frame->widget, frame_leave_handler); |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2476 | widget_set_motion_handler(frame->widget, frame_motion_handler); |
| 2477 | widget_set_button_handler(frame->widget, frame_button_handler); |
Rusty Lynch | 1084da5 | 2013-08-15 09:10:08 -0700 | [diff] [blame] | 2478 | widget_set_touch_down_handler(frame->widget, frame_touch_down_handler); |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2479 | widget_set_touch_up_handler(frame->widget, frame_touch_up_handler); |
Martin Minarik | 1998b15 | 2012-05-10 02:04:35 +0200 | [diff] [blame] | 2480 | |
Pekka Paalanen | 4dde2fc | 2012-01-19 13:33:50 +0200 | [diff] [blame] | 2481 | window->frame = frame; |
| 2482 | |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2483 | return frame->child; |
Kristian Høgsberg | da275dd | 2010-08-16 17:47:07 -0400 | [diff] [blame] | 2484 | } |
| 2485 | |
Kristian Høgsberg | a162792 | 2012-06-20 17:30:03 -0400 | [diff] [blame] | 2486 | void |
Jason Ekstrand | ee7fefc | 2013-10-13 19:08:38 -0500 | [diff] [blame] | 2487 | window_frame_set_child_size(struct widget *widget, int child_width, |
| 2488 | int child_height) |
Kristian Høgsberg | a162792 | 2012-06-20 17:30:03 -0400 | [diff] [blame] | 2489 | { |
| 2490 | struct display *display = widget->window->display; |
| 2491 | struct theme *t = display->theme; |
| 2492 | int decoration_width, decoration_height; |
| 2493 | int width, height; |
Scott Moreau | c6a7e4b | 2012-09-28 02:45:06 -0600 | [diff] [blame] | 2494 | int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin; |
Kristian Høgsberg | a162792 | 2012-06-20 17:30:03 -0400 | [diff] [blame] | 2495 | |
| 2496 | if (widget->window->type != TYPE_FULLSCREEN) { |
Scott Moreau | c6a7e4b | 2012-09-28 02:45:06 -0600 | [diff] [blame] | 2497 | decoration_width = (t->width + margin) * 2; |
Kristian Høgsberg | a162792 | 2012-06-20 17:30:03 -0400 | [diff] [blame] | 2498 | decoration_height = t->width + |
Scott Moreau | c6a7e4b | 2012-09-28 02:45:06 -0600 | [diff] [blame] | 2499 | t->titlebar_height + margin * 2; |
Kristian Høgsberg | a162792 | 2012-06-20 17:30:03 -0400 | [diff] [blame] | 2500 | |
| 2501 | width = child_width + decoration_width; |
| 2502 | height = child_height + decoration_height; |
| 2503 | } else { |
| 2504 | width = child_width; |
| 2505 | height = child_height; |
| 2506 | } |
| 2507 | |
| 2508 | window_schedule_resize(widget->window, width, height); |
| 2509 | } |
| 2510 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame] | 2511 | static void |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2512 | window_frame_destroy(struct window_frame *frame) |
Pekka Paalanen | 4dde2fc | 2012-01-19 13:33:50 +0200 | [diff] [blame] | 2513 | { |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 2514 | frame_destroy(frame->frame); |
Martin Minarik | 1998b15 | 2012-05-10 02:04:35 +0200 | [diff] [blame] | 2515 | |
Pekka Paalanen | 4dde2fc | 2012-01-19 13:33:50 +0200 | [diff] [blame] | 2516 | /* frame->child must be destroyed by the application */ |
| 2517 | widget_destroy(frame->widget); |
| 2518 | free(frame); |
| 2519 | } |
| 2520 | |
| 2521 | static void |
Kristian Høgsberg | b632351 | 2012-01-11 00:04:42 -0500 | [diff] [blame] | 2522 | input_set_focus_widget(struct input *input, struct widget *focus, |
Kristian Høgsberg | 80680c7 | 2012-05-10 12:21:37 -0400 | [diff] [blame] | 2523 | float x, float y) |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 2524 | { |
Kristian Høgsberg | 831dd52 | 2012-01-10 23:46:33 -0500 | [diff] [blame] | 2525 | struct widget *old, *widget; |
Kristian Høgsberg | bf74d52 | 2012-11-30 14:54:35 -0500 | [diff] [blame] | 2526 | int cursor; |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 2527 | |
Kristian Høgsberg | b632351 | 2012-01-11 00:04:42 -0500 | [diff] [blame] | 2528 | if (focus == input->focus_widget) |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 2529 | return; |
| 2530 | |
Kristian Høgsberg | b632351 | 2012-01-11 00:04:42 -0500 | [diff] [blame] | 2531 | old = input->focus_widget; |
Kristian Høgsberg | ee14323 | 2012-01-09 08:42:24 -0500 | [diff] [blame] | 2532 | if (old) { |
Kristian Høgsberg | 831dd52 | 2012-01-10 23:46:33 -0500 | [diff] [blame] | 2533 | widget = old; |
| 2534 | if (input->grab) |
| 2535 | widget = input->grab; |
| 2536 | if (widget->leave_handler) |
| 2537 | widget->leave_handler(old, input, widget->user_data); |
Kristian Høgsberg | b632351 | 2012-01-11 00:04:42 -0500 | [diff] [blame] | 2538 | input->focus_widget = NULL; |
Kristian Høgsberg | ee14323 | 2012-01-09 08:42:24 -0500 | [diff] [blame] | 2539 | } |
| 2540 | |
| 2541 | if (focus) { |
Kristian Høgsberg | 831dd52 | 2012-01-10 23:46:33 -0500 | [diff] [blame] | 2542 | widget = focus; |
| 2543 | if (input->grab) |
| 2544 | widget = input->grab; |
Kristian Høgsberg | f33984e | 2012-06-04 23:36:32 -0400 | [diff] [blame] | 2545 | input->focus_widget = focus; |
Kristian Høgsberg | 831dd52 | 2012-01-10 23:46:33 -0500 | [diff] [blame] | 2546 | if (widget->enter_handler) |
Kristian Høgsberg | bf74d52 | 2012-11-30 14:54:35 -0500 | [diff] [blame] | 2547 | cursor = widget->enter_handler(focus, input, x, y, |
| 2548 | widget->user_data); |
| 2549 | else |
| 2550 | cursor = widget->default_cursor; |
Kristian Høgsberg | bb901fa | 2012-01-09 11:22:32 -0500 | [diff] [blame] | 2551 | |
Kristian Høgsberg | bf74d52 | 2012-11-30 14:54:35 -0500 | [diff] [blame] | 2552 | input_set_pointer_image(input, cursor); |
Kristian Høgsberg | ee14323 | 2012-01-09 08:42:24 -0500 | [diff] [blame] | 2553 | } |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 2554 | } |
| 2555 | |
Kristian Høgsberg | b29798b | 2012-08-11 14:56:08 -0400 | [diff] [blame] | 2556 | void |
| 2557 | input_grab(struct input *input, struct widget *widget, uint32_t button) |
| 2558 | { |
| 2559 | input->grab = widget; |
| 2560 | input->grab_button = button; |
| 2561 | } |
| 2562 | |
| 2563 | void |
| 2564 | input_ungrab(struct input *input) |
| 2565 | { |
| 2566 | struct widget *widget; |
| 2567 | |
| 2568 | input->grab = NULL; |
| 2569 | if (input->pointer_focus) { |
Pekka Paalanen | ac95f3e | 2013-02-13 16:17:17 +0200 | [diff] [blame] | 2570 | widget = window_find_widget(input->pointer_focus, |
Kristian Høgsberg | b29798b | 2012-08-11 14:56:08 -0400 | [diff] [blame] | 2571 | input->sx, input->sy); |
| 2572 | input_set_focus_widget(input, widget, input->sx, input->sy); |
| 2573 | } |
| 2574 | } |
| 2575 | |
| 2576 | static void |
| 2577 | input_remove_pointer_focus(struct input *input) |
| 2578 | { |
| 2579 | struct window *window = input->pointer_focus; |
| 2580 | |
| 2581 | if (!window) |
| 2582 | return; |
| 2583 | |
| 2584 | input_set_focus_widget(input, NULL, 0, 0); |
| 2585 | |
| 2586 | input->pointer_focus = NULL; |
| 2587 | input->current_cursor = CURSOR_UNSET; |
| 2588 | } |
| 2589 | |
| 2590 | static void |
| 2591 | pointer_handle_enter(void *data, struct wl_pointer *pointer, |
| 2592 | uint32_t serial, struct wl_surface *surface, |
| 2593 | wl_fixed_t sx_w, wl_fixed_t sy_w) |
| 2594 | { |
| 2595 | struct input *input = data; |
| 2596 | struct window *window; |
| 2597 | struct widget *widget; |
| 2598 | float sx = wl_fixed_to_double(sx_w); |
| 2599 | float sy = wl_fixed_to_double(sy_w); |
| 2600 | |
| 2601 | if (!surface) { |
| 2602 | /* enter event for a window we've just destroyed */ |
| 2603 | return; |
| 2604 | } |
| 2605 | |
| 2606 | input->display->serial = serial; |
| 2607 | input->pointer_enter_serial = serial; |
| 2608 | input->pointer_focus = wl_surface_get_user_data(surface); |
| 2609 | window = input->pointer_focus; |
| 2610 | |
Pekka Paalanen | 9943686 | 2012-11-19 17:15:59 +0200 | [diff] [blame] | 2611 | if (window->resizing) { |
| 2612 | window->resizing = 0; |
Kristian Høgsberg | b29798b | 2012-08-11 14:56:08 -0400 | [diff] [blame] | 2613 | /* Schedule a redraw to free the pool */ |
| 2614 | window_schedule_redraw(window); |
| 2615 | } |
| 2616 | |
| 2617 | input->sx = sx; |
| 2618 | input->sy = sy; |
| 2619 | |
Pekka Paalanen | ac95f3e | 2013-02-13 16:17:17 +0200 | [diff] [blame] | 2620 | widget = window_find_widget(window, sx, sy); |
Kristian Høgsberg | b29798b | 2012-08-11 14:56:08 -0400 | [diff] [blame] | 2621 | input_set_focus_widget(input, widget, sx, sy); |
| 2622 | } |
| 2623 | |
| 2624 | static void |
| 2625 | pointer_handle_leave(void *data, struct wl_pointer *pointer, |
| 2626 | uint32_t serial, struct wl_surface *surface) |
| 2627 | { |
| 2628 | struct input *input = data; |
| 2629 | |
| 2630 | input->display->serial = serial; |
| 2631 | input_remove_pointer_focus(input); |
| 2632 | } |
| 2633 | |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 2634 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 2635 | pointer_handle_motion(void *data, struct wl_pointer *pointer, |
| 2636 | uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 2637 | { |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 2638 | struct input *input = data; |
Kristian Høgsberg | 9a68624 | 2010-08-18 15:28:04 -0400 | [diff] [blame] | 2639 | struct window *window = input->pointer_focus; |
Kristian Høgsberg | c51f799 | 2012-01-08 15:09:53 -0500 | [diff] [blame] | 2640 | struct widget *widget; |
Kristian Høgsberg | bf74d52 | 2012-11-30 14:54:35 -0500 | [diff] [blame] | 2641 | int cursor; |
Kristian Høgsberg | 80680c7 | 2012-05-10 12:21:37 -0400 | [diff] [blame] | 2642 | float sx = wl_fixed_to_double(sx_w); |
| 2643 | float sy = wl_fixed_to_double(sy_w); |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 2644 | |
Paul Winwood | b22bf57 | 2013-08-29 10:52:54 +0100 | [diff] [blame] | 2645 | input->sx = sx; |
| 2646 | input->sy = sy; |
| 2647 | |
| 2648 | if (!window) |
| 2649 | return; |
| 2650 | |
Rob Bradford | 5f08774 | 2013-07-11 19:41:27 +0100 | [diff] [blame] | 2651 | /* when making the window smaller - e.g. after a unmaximise we might |
| 2652 | * still have a pending motion event that the compositor has picked |
| 2653 | * based on the old surface dimensions |
| 2654 | */ |
| 2655 | if (sx > window->main_surface->allocation.width || |
| 2656 | sy > window->main_surface->allocation.height) |
| 2657 | return; |
| 2658 | |
Kristian Høgsberg | 831dd52 | 2012-01-10 23:46:33 -0500 | [diff] [blame] | 2659 | if (!(input->grab && input->grab_button)) { |
Pekka Paalanen | ac95f3e | 2013-02-13 16:17:17 +0200 | [diff] [blame] | 2660 | widget = window_find_widget(window, sx, sy); |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 2661 | input_set_focus_widget(input, widget, sx, sy); |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 2662 | } |
| 2663 | |
Kristian Høgsberg | 831dd52 | 2012-01-10 23:46:33 -0500 | [diff] [blame] | 2664 | if (input->grab) |
| 2665 | widget = input->grab; |
| 2666 | else |
Kristian Høgsberg | b632351 | 2012-01-11 00:04:42 -0500 | [diff] [blame] | 2667 | widget = input->focus_widget; |
Kristian Høgsberg | 1a5f0c3 | 2013-08-15 14:15:18 -0700 | [diff] [blame] | 2668 | if (widget) { |
| 2669 | if (widget->motion_handler) |
| 2670 | cursor = widget->motion_handler(input->focus_widget, |
| 2671 | input, time, sx, sy, |
| 2672 | widget->user_data); |
| 2673 | else |
| 2674 | cursor = widget->default_cursor; |
| 2675 | } else |
| 2676 | cursor = CURSOR_LEFT_PTR; |
Kristian Høgsberg | 9a68624 | 2010-08-18 15:28:04 -0400 | [diff] [blame] | 2677 | |
Kristian Høgsberg | 5a4e9ff | 2012-06-04 16:04:07 -0400 | [diff] [blame] | 2678 | input_set_pointer_image(input, cursor); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 2679 | } |
| 2680 | |
Kristian Høgsberg | 1d7ffd3 | 2010-08-25 16:34:05 -0400 | [diff] [blame] | 2681 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 2682 | pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial, |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 2683 | uint32_t time, uint32_t button, uint32_t state_w) |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 2684 | { |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 2685 | struct input *input = data; |
Kristian Høgsberg | c51f799 | 2012-01-08 15:09:53 -0500 | [diff] [blame] | 2686 | struct widget *widget; |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 2687 | enum wl_pointer_button_state state = state_w; |
Kristian Høgsberg | bf6ceda | 2010-06-14 20:25:06 -0400 | [diff] [blame] | 2688 | |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 2689 | input->display->serial = serial; |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 2690 | if (input->focus_widget && input->grab == NULL && |
| 2691 | state == WL_POINTER_BUTTON_STATE_PRESSED) |
Kristian Høgsberg | b632351 | 2012-01-11 00:04:42 -0500 | [diff] [blame] | 2692 | input_grab(input, input->focus_widget, button); |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 2693 | |
Neil Roberts | 6b28aad | 2012-01-23 19:11:18 +0000 | [diff] [blame] | 2694 | widget = input->grab; |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 2695 | if (widget && widget->button_handler) |
Neil Roberts | 6b28aad | 2012-01-23 19:11:18 +0000 | [diff] [blame] | 2696 | (*widget->button_handler)(widget, |
| 2697 | input, time, |
| 2698 | button, state, |
| 2699 | input->grab->user_data); |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 2700 | |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 2701 | if (input->grab && input->grab_button == button && |
| 2702 | state == WL_POINTER_BUTTON_STATE_RELEASED) |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 2703 | input_ungrab(input); |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 2704 | } |
| 2705 | |
Kristian Høgsberg | 99f090d | 2009-02-23 22:37:14 -0500 | [diff] [blame] | 2706 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 2707 | pointer_handle_axis(void *data, struct wl_pointer *pointer, |
Daniel Stone | 2fce402 | 2012-05-30 16:32:00 +0100 | [diff] [blame] | 2708 | uint32_t time, uint32_t axis, wl_fixed_t value) |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 2709 | { |
Philipp Brüschweiler | 7e0cc54 | 2012-08-14 11:02:41 +0200 | [diff] [blame] | 2710 | struct input *input = data; |
| 2711 | struct widget *widget; |
| 2712 | |
| 2713 | widget = input->focus_widget; |
| 2714 | if (input->grab) |
| 2715 | widget = input->grab; |
| 2716 | if (widget && widget->axis_handler) |
| 2717 | (*widget->axis_handler)(widget, |
| 2718 | input, time, |
| 2719 | axis, value, |
| 2720 | widget->user_data); |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 2721 | } |
| 2722 | |
Kristian Høgsberg | b29798b | 2012-08-11 14:56:08 -0400 | [diff] [blame] | 2723 | static const struct wl_pointer_listener pointer_listener = { |
| 2724 | pointer_handle_enter, |
| 2725 | pointer_handle_leave, |
| 2726 | pointer_handle_motion, |
| 2727 | pointer_handle_button, |
| 2728 | pointer_handle_axis, |
| 2729 | }; |
| 2730 | |
| 2731 | static void |
| 2732 | input_remove_keyboard_focus(struct input *input) |
| 2733 | { |
| 2734 | struct window *window = input->keyboard_focus; |
| 2735 | struct itimerspec its; |
| 2736 | |
| 2737 | its.it_interval.tv_sec = 0; |
| 2738 | its.it_interval.tv_nsec = 0; |
| 2739 | its.it_value.tv_sec = 0; |
| 2740 | its.it_value.tv_nsec = 0; |
| 2741 | timerfd_settime(input->repeat_timer_fd, 0, &its, NULL); |
| 2742 | |
| 2743 | if (!window) |
| 2744 | return; |
| 2745 | |
Kristian Høgsberg | 86adef9 | 2012-08-13 22:25:53 -0400 | [diff] [blame] | 2746 | window->focus_count--; |
Kristian Høgsberg | b29798b | 2012-08-11 14:56:08 -0400 | [diff] [blame] | 2747 | if (window->keyboard_focus_handler) |
| 2748 | (*window->keyboard_focus_handler)(window, NULL, |
| 2749 | window->user_data); |
| 2750 | |
| 2751 | input->keyboard_focus = NULL; |
| 2752 | } |
| 2753 | |
| 2754 | static void |
| 2755 | keyboard_repeat_func(struct task *task, uint32_t events) |
| 2756 | { |
| 2757 | struct input *input = |
| 2758 | container_of(task, struct input, repeat_task); |
| 2759 | struct window *window = input->keyboard_focus; |
| 2760 | uint64_t exp; |
| 2761 | |
| 2762 | if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp) |
| 2763 | /* If we change the timer between the fd becoming |
| 2764 | * readable and getting here, there'll be nothing to |
| 2765 | * read and we get EAGAIN. */ |
| 2766 | return; |
| 2767 | |
| 2768 | if (window && window->key_handler) { |
| 2769 | (*window->key_handler)(window, input, input->repeat_time, |
| 2770 | input->repeat_key, input->repeat_sym, |
| 2771 | WL_KEYBOARD_KEY_STATE_PRESSED, |
| 2772 | window->user_data); |
| 2773 | } |
| 2774 | } |
| 2775 | |
| 2776 | static void |
| 2777 | keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard, |
| 2778 | uint32_t format, int fd, uint32_t size) |
| 2779 | { |
| 2780 | struct input *input = data; |
Rui Matos | 3eccb86 | 2013-10-10 19:44:22 +0200 | [diff] [blame] | 2781 | struct xkb_keymap *keymap; |
| 2782 | struct xkb_state *state; |
Kristian Høgsberg | b29798b | 2012-08-11 14:56:08 -0400 | [diff] [blame] | 2783 | char *map_str; |
| 2784 | |
| 2785 | if (!data) { |
| 2786 | close(fd); |
| 2787 | return; |
| 2788 | } |
| 2789 | |
| 2790 | if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { |
| 2791 | close(fd); |
| 2792 | return; |
| 2793 | } |
| 2794 | |
| 2795 | map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); |
| 2796 | if (map_str == MAP_FAILED) { |
| 2797 | close(fd); |
| 2798 | return; |
| 2799 | } |
| 2800 | |
Rui Matos | 3eccb86 | 2013-10-10 19:44:22 +0200 | [diff] [blame] | 2801 | keymap = xkb_map_new_from_string(input->display->xkb_context, |
| 2802 | map_str, |
| 2803 | XKB_KEYMAP_FORMAT_TEXT_V1, |
| 2804 | 0); |
Kristian Høgsberg | b29798b | 2012-08-11 14:56:08 -0400 | [diff] [blame] | 2805 | munmap(map_str, size); |
| 2806 | close(fd); |
| 2807 | |
Rui Matos | 3eccb86 | 2013-10-10 19:44:22 +0200 | [diff] [blame] | 2808 | if (!keymap) { |
Kristian Høgsberg | b29798b | 2012-08-11 14:56:08 -0400 | [diff] [blame] | 2809 | fprintf(stderr, "failed to compile keymap\n"); |
| 2810 | return; |
| 2811 | } |
| 2812 | |
Rui Matos | 3eccb86 | 2013-10-10 19:44:22 +0200 | [diff] [blame] | 2813 | state = xkb_state_new(keymap); |
| 2814 | if (!state) { |
Kristian Høgsberg | b29798b | 2012-08-11 14:56:08 -0400 | [diff] [blame] | 2815 | fprintf(stderr, "failed to create XKB state\n"); |
Rui Matos | 3eccb86 | 2013-10-10 19:44:22 +0200 | [diff] [blame] | 2816 | xkb_map_unref(keymap); |
Kristian Høgsberg | b29798b | 2012-08-11 14:56:08 -0400 | [diff] [blame] | 2817 | return; |
| 2818 | } |
| 2819 | |
Rui Matos | 3eccb86 | 2013-10-10 19:44:22 +0200 | [diff] [blame] | 2820 | xkb_keymap_unref(input->xkb.keymap); |
| 2821 | xkb_state_unref(input->xkb.state); |
| 2822 | input->xkb.keymap = keymap; |
| 2823 | input->xkb.state = state; |
| 2824 | |
Kristian Høgsberg | b29798b | 2012-08-11 14:56:08 -0400 | [diff] [blame] | 2825 | input->xkb.control_mask = |
| 2826 | 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control"); |
| 2827 | input->xkb.alt_mask = |
| 2828 | 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1"); |
| 2829 | input->xkb.shift_mask = |
| 2830 | 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift"); |
| 2831 | } |
| 2832 | |
| 2833 | static void |
| 2834 | keyboard_handle_enter(void *data, struct wl_keyboard *keyboard, |
| 2835 | uint32_t serial, struct wl_surface *surface, |
| 2836 | struct wl_array *keys) |
| 2837 | { |
| 2838 | struct input *input = data; |
| 2839 | struct window *window; |
| 2840 | |
| 2841 | input->display->serial = serial; |
| 2842 | input->keyboard_focus = wl_surface_get_user_data(surface); |
| 2843 | |
| 2844 | window = input->keyboard_focus; |
Kristian Høgsberg | 86adef9 | 2012-08-13 22:25:53 -0400 | [diff] [blame] | 2845 | window->focus_count++; |
Kristian Høgsberg | b29798b | 2012-08-11 14:56:08 -0400 | [diff] [blame] | 2846 | if (window->keyboard_focus_handler) |
| 2847 | (*window->keyboard_focus_handler)(window, |
Kristian Høgsberg | 86adef9 | 2012-08-13 22:25:53 -0400 | [diff] [blame] | 2848 | input, window->user_data); |
Kristian Høgsberg | b29798b | 2012-08-11 14:56:08 -0400 | [diff] [blame] | 2849 | } |
| 2850 | |
| 2851 | static void |
| 2852 | keyboard_handle_leave(void *data, struct wl_keyboard *keyboard, |
| 2853 | uint32_t serial, struct wl_surface *surface) |
| 2854 | { |
| 2855 | struct input *input = data; |
| 2856 | |
| 2857 | input->display->serial = serial; |
| 2858 | input_remove_keyboard_focus(input); |
| 2859 | } |
| 2860 | |
Scott Moreau | 210d079 | 2012-03-22 10:47:01 -0600 | [diff] [blame] | 2861 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 2862 | keyboard_handle_key(void *data, struct wl_keyboard *keyboard, |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 2863 | uint32_t serial, uint32_t time, uint32_t key, |
| 2864 | uint32_t state_w) |
Kristian Høgsberg | 99f090d | 2009-02-23 22:37:14 -0500 | [diff] [blame] | 2865 | { |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 2866 | struct input *input = data; |
| 2867 | struct window *window = input->keyboard_focus; |
Kristian Høgsberg | 7016313 | 2012-05-08 15:55:39 -0400 | [diff] [blame] | 2868 | uint32_t code, num_syms; |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 2869 | enum wl_keyboard_key_state state = state_w; |
Kristian Høgsberg | 7016313 | 2012-05-08 15:55:39 -0400 | [diff] [blame] | 2870 | const xkb_keysym_t *syms; |
| 2871 | xkb_keysym_t sym; |
Kristian Høgsberg | cf4d244 | 2012-06-20 14:52:12 -0400 | [diff] [blame] | 2872 | struct itimerspec its; |
Kristian Høgsberg | 99f090d | 2009-02-23 22:37:14 -0500 | [diff] [blame] | 2873 | |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 2874 | input->display->serial = serial; |
Daniel Stone | 0d5a509 | 2012-02-16 12:48:00 +0000 | [diff] [blame] | 2875 | code = key + 8; |
Kristian Høgsberg | 86adef9 | 2012-08-13 22:25:53 -0400 | [diff] [blame] | 2876 | if (!window || !input->xkb.state) |
Kristian Høgsberg | 99f090d | 2009-02-23 22:37:14 -0500 | [diff] [blame] | 2877 | return; |
| 2878 | |
Daniel Stone | 97f6854 | 2012-05-30 16:32:01 +0100 | [diff] [blame] | 2879 | num_syms = xkb_key_get_syms(input->xkb.state, code, &syms); |
Kristian Høgsberg | 99f090d | 2009-02-23 22:37:14 -0500 | [diff] [blame] | 2880 | |
Kristian Høgsberg | cf4d244 | 2012-06-20 14:52:12 -0400 | [diff] [blame] | 2881 | sym = XKB_KEY_NoSymbol; |
| 2882 | if (num_syms == 1) |
| 2883 | sym = syms[0]; |
| 2884 | |
| 2885 | if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) { |
Daniel Stone | c9785ea | 2012-05-30 16:31:52 +0100 | [diff] [blame] | 2886 | if (state == WL_KEYBOARD_KEY_STATE_PRESSED) |
Kristian Høgsberg | d6bcd7d | 2012-02-16 15:53:46 -0500 | [diff] [blame] | 2887 | window_set_maximized(window, |
| 2888 | window->type != TYPE_MAXIMIZED); |
Kristian Høgsberg | 67ace20 | 2012-07-23 21:56:31 -0400 | [diff] [blame] | 2889 | } else if (sym == XKB_KEY_F11 && |
| 2890 | window->fullscreen_handler && |
| 2891 | state == WL_KEYBOARD_KEY_STATE_PRESSED) { |
| 2892 | window->fullscreen_handler(window, window->user_data); |
Kristian Høgsberg | 4fc1535 | 2012-07-25 16:35:28 -0400 | [diff] [blame] | 2893 | } else if (sym == XKB_KEY_F4 && |
| 2894 | input->modifiers == MOD_ALT_MASK && |
| 2895 | state == WL_KEYBOARD_KEY_STATE_PRESSED) { |
| 2896 | if (window->close_handler) |
| 2897 | window->close_handler(window->parent, |
| 2898 | window->user_data); |
| 2899 | else |
| 2900 | display_exit(window->display); |
Kristian Høgsberg | d6bcd7d | 2012-02-16 15:53:46 -0500 | [diff] [blame] | 2901 | } else if (window->key_handler) { |
| 2902 | (*window->key_handler)(window, input, time, key, |
| 2903 | sym, state, window->user_data); |
| 2904 | } |
Kristian Høgsberg | cf4d244 | 2012-06-20 14:52:12 -0400 | [diff] [blame] | 2905 | |
| 2906 | if (state == WL_KEYBOARD_KEY_STATE_RELEASED && |
| 2907 | key == input->repeat_key) { |
| 2908 | its.it_interval.tv_sec = 0; |
| 2909 | its.it_interval.tv_nsec = 0; |
| 2910 | its.it_value.tv_sec = 0; |
| 2911 | its.it_value.tv_nsec = 0; |
| 2912 | timerfd_settime(input->repeat_timer_fd, 0, &its, NULL); |
| 2913 | } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { |
| 2914 | input->repeat_sym = sym; |
| 2915 | input->repeat_key = key; |
| 2916 | input->repeat_time = time; |
| 2917 | its.it_interval.tv_sec = 0; |
| 2918 | its.it_interval.tv_nsec = 25 * 1000 * 1000; |
| 2919 | its.it_value.tv_sec = 0; |
| 2920 | its.it_value.tv_nsec = 400 * 1000 * 1000; |
| 2921 | timerfd_settime(input->repeat_timer_fd, 0, &its, NULL); |
| 2922 | } |
| 2923 | } |
| 2924 | |
| 2925 | static void |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 2926 | keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard, |
| 2927 | uint32_t serial, uint32_t mods_depressed, |
| 2928 | uint32_t mods_latched, uint32_t mods_locked, |
| 2929 | uint32_t group) |
| 2930 | { |
| 2931 | struct input *input = data; |
Jonas Ådahl | d9f6b07 | 2012-09-27 18:40:45 +0200 | [diff] [blame] | 2932 | xkb_mod_mask_t mask; |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 2933 | |
Matt Roper | e61561f | 2013-06-24 16:52:43 +0100 | [diff] [blame] | 2934 | /* If we're not using a keymap, then we don't handle PC-style modifiers */ |
| 2935 | if (!input->xkb.keymap) |
| 2936 | return; |
| 2937 | |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 2938 | xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched, |
| 2939 | mods_locked, 0, 0, group); |
Jonas Ådahl | d9f6b07 | 2012-09-27 18:40:45 +0200 | [diff] [blame] | 2940 | mask = xkb_state_serialize_mods(input->xkb.state, |
| 2941 | XKB_STATE_DEPRESSED | |
| 2942 | XKB_STATE_LATCHED); |
| 2943 | input->modifiers = 0; |
| 2944 | if (mask & input->xkb.control_mask) |
| 2945 | input->modifiers |= MOD_CONTROL_MASK; |
| 2946 | if (mask & input->xkb.alt_mask) |
| 2947 | input->modifiers |= MOD_ALT_MASK; |
| 2948 | if (mask & input->xkb.shift_mask) |
| 2949 | input->modifiers |= MOD_SHIFT_MASK; |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 2950 | } |
| 2951 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 2952 | static const struct wl_keyboard_listener keyboard_listener = { |
Daniel Stone | b7452fe | 2012-06-01 12:14:06 +0100 | [diff] [blame] | 2953 | keyboard_handle_keymap, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 2954 | keyboard_handle_enter, |
| 2955 | keyboard_handle_leave, |
| 2956 | keyboard_handle_key, |
Daniel Stone | 351eb61 | 2012-05-31 15:27:47 -0400 | [diff] [blame] | 2957 | keyboard_handle_modifiers, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 2958 | }; |
Kristian Høgsberg | e04ad57 | 2011-12-21 17:14:54 -0500 | [diff] [blame] | 2959 | |
| 2960 | static void |
Rusty Lynch | 041815a | 2013-08-08 21:20:38 -0700 | [diff] [blame] | 2961 | touch_handle_down(void *data, struct wl_touch *wl_touch, |
| 2962 | uint32_t serial, uint32_t time, struct wl_surface *surface, |
| 2963 | int32_t id, wl_fixed_t x_w, wl_fixed_t y_w) |
| 2964 | { |
| 2965 | struct input *input = data; |
| 2966 | struct widget *widget; |
| 2967 | float sx = wl_fixed_to_double(x_w); |
| 2968 | float sy = wl_fixed_to_double(y_w); |
| 2969 | |
Rusty Lynch | 1084da5 | 2013-08-15 09:10:08 -0700 | [diff] [blame] | 2970 | input->display->serial = serial; |
Rusty Lynch | 041815a | 2013-08-08 21:20:38 -0700 | [diff] [blame] | 2971 | input->touch_focus = wl_surface_get_user_data(surface); |
| 2972 | if (!input->touch_focus) { |
| 2973 | DBG("Failed to find to touch focus for surface %p\n", surface); |
| 2974 | return; |
| 2975 | } |
| 2976 | |
| 2977 | widget = window_find_widget(input->touch_focus, |
| 2978 | wl_fixed_to_double(x_w), |
| 2979 | wl_fixed_to_double(y_w)); |
| 2980 | if (widget) { |
| 2981 | struct touch_point *tp = xmalloc(sizeof *tp); |
| 2982 | if (tp) { |
| 2983 | tp->id = id; |
| 2984 | tp->widget = widget; |
| 2985 | wl_list_insert(&input->touch_point_list, &tp->link); |
| 2986 | |
| 2987 | if (widget->touch_down_handler) |
Rusty Lynch | 1084da5 | 2013-08-15 09:10:08 -0700 | [diff] [blame] | 2988 | (*widget->touch_down_handler)(widget, input, |
| 2989 | serial, time, id, |
Rusty Lynch | 041815a | 2013-08-08 21:20:38 -0700 | [diff] [blame] | 2990 | sx, sy, |
| 2991 | widget->user_data); |
| 2992 | } |
| 2993 | } |
| 2994 | } |
| 2995 | |
| 2996 | static void |
| 2997 | touch_handle_up(void *data, struct wl_touch *wl_touch, |
| 2998 | uint32_t serial, uint32_t time, int32_t id) |
| 2999 | { |
| 3000 | struct input *input = data; |
| 3001 | struct touch_point *tp, *tmp; |
| 3002 | |
Rusty Lynch | 041815a | 2013-08-08 21:20:38 -0700 | [diff] [blame] | 3003 | if (!input->touch_focus) { |
| 3004 | DBG("No touch focus found for touch up event!\n"); |
| 3005 | return; |
| 3006 | } |
| 3007 | |
| 3008 | wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) { |
| 3009 | if (tp->id != id) |
| 3010 | continue; |
| 3011 | |
| 3012 | if (tp->widget->touch_up_handler) |
Rusty Lynch | 1084da5 | 2013-08-15 09:10:08 -0700 | [diff] [blame] | 3013 | (*tp->widget->touch_up_handler)(tp->widget, input, serial, |
Rusty Lynch | 041815a | 2013-08-08 21:20:38 -0700 | [diff] [blame] | 3014 | time, id, |
| 3015 | tp->widget->user_data); |
| 3016 | |
| 3017 | wl_list_remove(&tp->link); |
| 3018 | free(tp); |
| 3019 | |
| 3020 | return; |
| 3021 | } |
| 3022 | } |
| 3023 | |
| 3024 | static void |
| 3025 | touch_handle_motion(void *data, struct wl_touch *wl_touch, |
| 3026 | uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w) |
| 3027 | { |
| 3028 | struct input *input = data; |
| 3029 | struct touch_point *tp; |
| 3030 | float sx = wl_fixed_to_double(x_w); |
| 3031 | float sy = wl_fixed_to_double(y_w); |
| 3032 | |
| 3033 | DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list)); |
| 3034 | |
| 3035 | if (!input->touch_focus) { |
| 3036 | DBG("No touch focus found for touch motion event!\n"); |
| 3037 | return; |
| 3038 | } |
| 3039 | |
| 3040 | wl_list_for_each(tp, &input->touch_point_list, link) { |
| 3041 | if (tp->id != id) |
| 3042 | continue; |
| 3043 | |
| 3044 | if (tp->widget->touch_motion_handler) |
Rusty Lynch | 1084da5 | 2013-08-15 09:10:08 -0700 | [diff] [blame] | 3045 | (*tp->widget->touch_motion_handler)(tp->widget, input, time, |
Rusty Lynch | 041815a | 2013-08-08 21:20:38 -0700 | [diff] [blame] | 3046 | id, sx, sy, |
| 3047 | tp->widget->user_data); |
| 3048 | return; |
| 3049 | } |
| 3050 | } |
| 3051 | |
| 3052 | static void |
| 3053 | touch_handle_frame(void *data, struct wl_touch *wl_touch) |
| 3054 | { |
| 3055 | struct input *input = data; |
| 3056 | struct touch_point *tp, *tmp; |
| 3057 | |
| 3058 | DBG("touch_handle_frame\n"); |
| 3059 | |
| 3060 | if (!input->touch_focus) { |
| 3061 | DBG("No touch focus found for touch frame event!\n"); |
| 3062 | return; |
| 3063 | } |
| 3064 | |
| 3065 | wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) { |
| 3066 | if (tp->widget->touch_frame_handler) |
Rusty Lynch | 1084da5 | 2013-08-15 09:10:08 -0700 | [diff] [blame] | 3067 | (*tp->widget->touch_frame_handler)(tp->widget, input, |
| 3068 | tp->widget->user_data); |
Rusty Lynch | 041815a | 2013-08-08 21:20:38 -0700 | [diff] [blame] | 3069 | |
| 3070 | wl_list_remove(&tp->link); |
| 3071 | free(tp); |
| 3072 | } |
| 3073 | } |
| 3074 | |
| 3075 | static void |
| 3076 | touch_handle_cancel(void *data, struct wl_touch *wl_touch) |
| 3077 | { |
| 3078 | struct input *input = data; |
| 3079 | struct touch_point *tp, *tmp; |
| 3080 | |
| 3081 | DBG("touch_handle_cancel\n"); |
| 3082 | |
| 3083 | if (!input->touch_focus) { |
| 3084 | DBG("No touch focus found for touch cancel event!\n"); |
| 3085 | return; |
| 3086 | } |
| 3087 | |
| 3088 | wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) { |
| 3089 | if (tp->widget->touch_cancel_handler) |
Rusty Lynch | 1084da5 | 2013-08-15 09:10:08 -0700 | [diff] [blame] | 3090 | (*tp->widget->touch_cancel_handler)(tp->widget, input, |
| 3091 | tp->widget->user_data); |
Rusty Lynch | 041815a | 2013-08-08 21:20:38 -0700 | [diff] [blame] | 3092 | |
| 3093 | wl_list_remove(&tp->link); |
| 3094 | free(tp); |
| 3095 | } |
| 3096 | } |
| 3097 | |
| 3098 | static const struct wl_touch_listener touch_listener = { |
| 3099 | touch_handle_down, |
| 3100 | touch_handle_up, |
| 3101 | touch_handle_motion, |
| 3102 | touch_handle_frame, |
| 3103 | touch_handle_cancel, |
| 3104 | }; |
| 3105 | |
| 3106 | static void |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 3107 | seat_handle_capabilities(void *data, struct wl_seat *seat, |
| 3108 | enum wl_seat_capability caps) |
Kristian Høgsberg | e04ad57 | 2011-12-21 17:14:54 -0500 | [diff] [blame] | 3109 | { |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 3110 | struct input *input = data; |
| 3111 | |
| 3112 | if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) { |
| 3113 | input->pointer = wl_seat_get_pointer(seat); |
| 3114 | wl_pointer_set_user_data(input->pointer, input); |
| 3115 | wl_pointer_add_listener(input->pointer, &pointer_listener, |
| 3116 | input); |
| 3117 | } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) { |
| 3118 | wl_pointer_destroy(input->pointer); |
| 3119 | input->pointer = NULL; |
| 3120 | } |
| 3121 | |
| 3122 | if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) { |
| 3123 | input->keyboard = wl_seat_get_keyboard(seat); |
| 3124 | wl_keyboard_set_user_data(input->keyboard, input); |
| 3125 | wl_keyboard_add_listener(input->keyboard, &keyboard_listener, |
| 3126 | input); |
| 3127 | } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) { |
| 3128 | wl_keyboard_destroy(input->keyboard); |
| 3129 | input->keyboard = NULL; |
| 3130 | } |
Rusty Lynch | 041815a | 2013-08-08 21:20:38 -0700 | [diff] [blame] | 3131 | |
| 3132 | if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) { |
| 3133 | input->touch = wl_seat_get_touch(seat); |
| 3134 | wl_touch_set_user_data(input->touch, input); |
| 3135 | wl_touch_add_listener(input->touch, &touch_listener, input); |
| 3136 | } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) { |
| 3137 | wl_touch_destroy(input->touch); |
| 3138 | input->touch = NULL; |
| 3139 | } |
Kristian Høgsberg | e04ad57 | 2011-12-21 17:14:54 -0500 | [diff] [blame] | 3140 | } |
| 3141 | |
Rob Bradford | 0803118 | 2013-08-13 20:11:03 +0100 | [diff] [blame] | 3142 | static void |
| 3143 | seat_handle_name(void *data, struct wl_seat *seat, |
| 3144 | const char *name) |
| 3145 | { |
| 3146 | |
| 3147 | } |
| 3148 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 3149 | static const struct wl_seat_listener seat_listener = { |
| 3150 | seat_handle_capabilities, |
Rob Bradford | 0803118 | 2013-08-13 20:11:03 +0100 | [diff] [blame] | 3151 | seat_handle_name |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 3152 | }; |
| 3153 | |
Kristian Høgsberg | 9a68624 | 2010-08-18 15:28:04 -0400 | [diff] [blame] | 3154 | void |
| 3155 | input_get_position(struct input *input, int32_t *x, int32_t *y) |
| 3156 | { |
| 3157 | *x = input->sx; |
| 3158 | *y = input->sy; |
| 3159 | } |
| 3160 | |
Kristian Høgsberg | d56bd90 | 2012-06-05 09:58:51 -0400 | [diff] [blame] | 3161 | struct display * |
| 3162 | input_get_display(struct input *input) |
| 3163 | { |
| 3164 | return input->display; |
| 3165 | } |
| 3166 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 3167 | struct wl_seat * |
| 3168 | input_get_seat(struct input *input) |
Kristian Høgsberg | 1d7ffd3 | 2010-08-25 16:34:05 -0400 | [diff] [blame] | 3169 | { |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 3170 | return input->seat; |
Kristian Høgsberg | 1d7ffd3 | 2010-08-25 16:34:05 -0400 | [diff] [blame] | 3171 | } |
| 3172 | |
Kristian Høgsberg | 67cac8a | 2011-01-19 14:20:33 -0500 | [diff] [blame] | 3173 | uint32_t |
| 3174 | input_get_modifiers(struct input *input) |
| 3175 | { |
| 3176 | return input->modifiers; |
| 3177 | } |
| 3178 | |
Kristian Høgsberg | b632351 | 2012-01-11 00:04:42 -0500 | [diff] [blame] | 3179 | struct widget * |
| 3180 | input_get_focus_widget(struct input *input) |
| 3181 | { |
| 3182 | return input->focus_widget; |
| 3183 | } |
| 3184 | |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 3185 | struct data_offer { |
| 3186 | struct wl_data_offer *offer; |
| 3187 | struct input *input; |
| 3188 | struct wl_array types; |
| 3189 | int refcount; |
Kristian Høgsberg | 9a68624 | 2010-08-18 15:28:04 -0400 | [diff] [blame] | 3190 | |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 3191 | struct task io_task; |
| 3192 | int fd; |
| 3193 | data_func_t func; |
| 3194 | int32_t x, y; |
| 3195 | void *user_data; |
| 3196 | }; |
| 3197 | |
| 3198 | static void |
| 3199 | data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type) |
| 3200 | { |
| 3201 | struct data_offer *offer = data; |
| 3202 | char **p; |
| 3203 | |
| 3204 | p = wl_array_add(&offer->types, sizeof *p); |
| 3205 | *p = strdup(type); |
| 3206 | } |
| 3207 | |
| 3208 | static const struct wl_data_offer_listener data_offer_listener = { |
| 3209 | data_offer_offer, |
| 3210 | }; |
| 3211 | |
| 3212 | static void |
| 3213 | data_offer_destroy(struct data_offer *offer) |
| 3214 | { |
| 3215 | char **p; |
| 3216 | |
| 3217 | offer->refcount--; |
| 3218 | if (offer->refcount == 0) { |
| 3219 | wl_data_offer_destroy(offer->offer); |
| 3220 | for (p = offer->types.data; *p; p++) |
| 3221 | free(*p); |
| 3222 | wl_array_release(&offer->types); |
| 3223 | free(offer); |
| 3224 | } |
| 3225 | } |
| 3226 | |
| 3227 | static void |
| 3228 | data_device_data_offer(void *data, |
Kristian Høgsberg | 8733b33 | 2012-06-28 22:04:06 -0400 | [diff] [blame] | 3229 | struct wl_data_device *data_device, |
| 3230 | struct wl_data_offer *_offer) |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 3231 | { |
| 3232 | struct data_offer *offer; |
| 3233 | |
Brian Lovin | bc91926 | 2013-08-07 15:34:59 -0700 | [diff] [blame] | 3234 | offer = xmalloc(sizeof *offer); |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 3235 | |
| 3236 | wl_array_init(&offer->types); |
| 3237 | offer->refcount = 1; |
| 3238 | offer->input = data; |
Kristian Høgsberg | 8733b33 | 2012-06-28 22:04:06 -0400 | [diff] [blame] | 3239 | offer->offer = _offer; |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 3240 | wl_data_offer_add_listener(offer->offer, |
| 3241 | &data_offer_listener, offer); |
| 3242 | } |
| 3243 | |
| 3244 | static void |
| 3245 | data_device_enter(void *data, struct wl_data_device *data_device, |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 3246 | uint32_t serial, struct wl_surface *surface, |
Daniel Stone | 103db7f | 2012-05-08 17:17:55 +0100 | [diff] [blame] | 3247 | wl_fixed_t x_w, wl_fixed_t y_w, |
| 3248 | struct wl_data_offer *offer) |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 3249 | { |
| 3250 | struct input *input = data; |
| 3251 | struct window *window; |
Ander Conselvan de Oliveira | 08bcf14 | 2012-05-29 10:58:27 +0300 | [diff] [blame] | 3252 | void *types_data; |
Kristian Høgsberg | 80680c7 | 2012-05-10 12:21:37 -0400 | [diff] [blame] | 3253 | float x = wl_fixed_to_double(x_w); |
| 3254 | float y = wl_fixed_to_double(y_w); |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 3255 | char **p; |
| 3256 | |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 3257 | input->pointer_enter_serial = serial; |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 3258 | window = wl_surface_get_user_data(surface); |
| 3259 | input->pointer_focus = window; |
| 3260 | |
Ander Conselvan de Oliveira | 08bcf14 | 2012-05-29 10:58:27 +0300 | [diff] [blame] | 3261 | if (offer) { |
| 3262 | input->drag_offer = wl_data_offer_get_user_data(offer); |
| 3263 | |
| 3264 | p = wl_array_add(&input->drag_offer->types, sizeof *p); |
| 3265 | *p = NULL; |
| 3266 | |
| 3267 | types_data = input->drag_offer->types.data; |
| 3268 | } else { |
| 3269 | input->drag_offer = NULL; |
| 3270 | types_data = NULL; |
| 3271 | } |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 3272 | |
| 3273 | window = input->pointer_focus; |
| 3274 | if (window->data_handler) |
Ander Conselvan de Oliveira | 08bcf14 | 2012-05-29 10:58:27 +0300 | [diff] [blame] | 3275 | window->data_handler(window, input, x, y, types_data, |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 3276 | window->user_data); |
| 3277 | } |
| 3278 | |
| 3279 | static void |
| 3280 | data_device_leave(void *data, struct wl_data_device *data_device) |
| 3281 | { |
| 3282 | struct input *input = data; |
| 3283 | |
Ander Conselvan de Oliveira | 08bcf14 | 2012-05-29 10:58:27 +0300 | [diff] [blame] | 3284 | if (input->drag_offer) { |
| 3285 | data_offer_destroy(input->drag_offer); |
| 3286 | input->drag_offer = NULL; |
| 3287 | } |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 3288 | } |
| 3289 | |
| 3290 | static void |
| 3291 | data_device_motion(void *data, struct wl_data_device *data_device, |
Daniel Stone | 103db7f | 2012-05-08 17:17:55 +0100 | [diff] [blame] | 3292 | uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w) |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 3293 | { |
| 3294 | struct input *input = data; |
| 3295 | struct window *window = input->pointer_focus; |
Kristian Høgsberg | 80680c7 | 2012-05-10 12:21:37 -0400 | [diff] [blame] | 3296 | float x = wl_fixed_to_double(x_w); |
| 3297 | float y = wl_fixed_to_double(y_w); |
Ander Conselvan de Oliveira | 08bcf14 | 2012-05-29 10:58:27 +0300 | [diff] [blame] | 3298 | void *types_data; |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 3299 | |
| 3300 | input->sx = x; |
| 3301 | input->sy = y; |
| 3302 | |
Ander Conselvan de Oliveira | 08bcf14 | 2012-05-29 10:58:27 +0300 | [diff] [blame] | 3303 | if (input->drag_offer) |
| 3304 | types_data = input->drag_offer->types.data; |
| 3305 | else |
| 3306 | types_data = NULL; |
| 3307 | |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 3308 | if (window->data_handler) |
Ander Conselvan de Oliveira | 08bcf14 | 2012-05-29 10:58:27 +0300 | [diff] [blame] | 3309 | window->data_handler(window, input, x, y, types_data, |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 3310 | window->user_data); |
| 3311 | } |
| 3312 | |
| 3313 | static void |
| 3314 | data_device_drop(void *data, struct wl_data_device *data_device) |
| 3315 | { |
| 3316 | struct input *input = data; |
| 3317 | struct window *window = input->pointer_focus; |
| 3318 | |
| 3319 | if (window->drop_handler) |
| 3320 | window->drop_handler(window, input, |
| 3321 | input->sx, input->sy, window->user_data); |
| 3322 | } |
| 3323 | |
| 3324 | static void |
| 3325 | data_device_selection(void *data, |
| 3326 | struct wl_data_device *wl_data_device, |
| 3327 | struct wl_data_offer *offer) |
| 3328 | { |
| 3329 | struct input *input = data; |
| 3330 | char **p; |
| 3331 | |
| 3332 | if (input->selection_offer) |
| 3333 | data_offer_destroy(input->selection_offer); |
| 3334 | |
Kristian Høgsberg | 42c8f60 | 2012-01-27 11:04:18 -0500 | [diff] [blame] | 3335 | if (offer) { |
| 3336 | input->selection_offer = wl_data_offer_get_user_data(offer); |
| 3337 | p = wl_array_add(&input->selection_offer->types, sizeof *p); |
| 3338 | *p = NULL; |
Kristian Høgsberg | a4b3d0e | 2012-05-31 23:30:32 -0400 | [diff] [blame] | 3339 | } else { |
| 3340 | input->selection_offer = NULL; |
Kristian Høgsberg | 42c8f60 | 2012-01-27 11:04:18 -0500 | [diff] [blame] | 3341 | } |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 3342 | } |
| 3343 | |
| 3344 | static const struct wl_data_device_listener data_device_listener = { |
| 3345 | data_device_data_offer, |
| 3346 | data_device_enter, |
| 3347 | data_device_leave, |
| 3348 | data_device_motion, |
| 3349 | data_device_drop, |
| 3350 | data_device_selection |
| 3351 | }; |
| 3352 | |
Ander Conselvan de Oliveira | 8062007 | 2012-06-15 17:27:36 +0300 | [diff] [blame] | 3353 | static void |
| 3354 | input_set_pointer_image_index(struct input *input, int index) |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 3355 | { |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 3356 | struct wl_buffer *buffer; |
Ander Conselvan de Oliveira | 1042dc1 | 2012-05-22 15:39:42 +0300 | [diff] [blame] | 3357 | struct wl_cursor *cursor; |
| 3358 | struct wl_cursor_image *image; |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 3359 | |
Daniel Stone | 8097274 | 2012-11-07 17:51:39 +1100 | [diff] [blame] | 3360 | if (!input->pointer) |
| 3361 | return; |
| 3362 | |
Ander Conselvan de Oliveira | 8062007 | 2012-06-15 17:27:36 +0300 | [diff] [blame] | 3363 | cursor = input->display->cursors[input->current_cursor]; |
Ander Conselvan de Oliveira | 1042dc1 | 2012-05-22 15:39:42 +0300 | [diff] [blame] | 3364 | if (!cursor) |
Dima Ryazanov | ff1c2d7 | 2012-05-08 21:02:33 -0700 | [diff] [blame] | 3365 | return; |
| 3366 | |
Kristian Høgsberg | 7cee197 | 2012-06-04 23:37:42 -0400 | [diff] [blame] | 3367 | if (index >= (int) cursor->image_count) { |
| 3368 | fprintf(stderr, "cursor index out of range\n"); |
| 3369 | return; |
| 3370 | } |
| 3371 | |
| 3372 | image = cursor->images[index]; |
Ander Conselvan de Oliveira | 1042dc1 | 2012-05-22 15:39:42 +0300 | [diff] [blame] | 3373 | buffer = wl_cursor_image_get_buffer(image); |
| 3374 | if (!buffer) |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 3375 | return; |
| 3376 | |
Kristian Høgsberg | ae27737 | 2012-08-01 09:41:08 -0400 | [diff] [blame] | 3377 | wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial, |
Ander Conselvan de Oliveira | 37ffc3c | 2012-06-15 17:27:35 +0300 | [diff] [blame] | 3378 | input->pointer_surface, |
| 3379 | image->hotspot_x, image->hotspot_y); |
| 3380 | wl_surface_attach(input->pointer_surface, buffer, 0, 0); |
| 3381 | wl_surface_damage(input->pointer_surface, 0, 0, |
| 3382 | image->width, image->height); |
Pekka Paalanen | c9e00c0 | 2012-10-10 12:49:24 +0300 | [diff] [blame] | 3383 | wl_surface_commit(input->pointer_surface); |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 3384 | } |
| 3385 | |
Ander Conselvan de Oliveira | 8062007 | 2012-06-15 17:27:36 +0300 | [diff] [blame] | 3386 | static const struct wl_callback_listener pointer_surface_listener; |
| 3387 | |
| 3388 | static void |
| 3389 | pointer_surface_frame_callback(void *data, struct wl_callback *callback, |
| 3390 | uint32_t time) |
| 3391 | { |
| 3392 | struct input *input = data; |
Daniel Stone | a494f1d | 2012-06-18 19:31:12 +0100 | [diff] [blame] | 3393 | struct wl_cursor *cursor; |
Ander Conselvan de Oliveira | 8062007 | 2012-06-15 17:27:36 +0300 | [diff] [blame] | 3394 | int i; |
| 3395 | |
| 3396 | if (callback) { |
| 3397 | assert(callback == input->cursor_frame_cb); |
| 3398 | wl_callback_destroy(callback); |
| 3399 | input->cursor_frame_cb = NULL; |
| 3400 | } |
| 3401 | |
Daniel Stone | 8097274 | 2012-11-07 17:51:39 +1100 | [diff] [blame] | 3402 | if (!input->pointer) |
| 3403 | return; |
| 3404 | |
Kristian Høgsberg | f337052 | 2012-06-20 23:04:41 -0400 | [diff] [blame] | 3405 | if (input->current_cursor == CURSOR_BLANK) { |
Kristian Høgsberg | ae27737 | 2012-08-01 09:41:08 -0400 | [diff] [blame] | 3406 | wl_pointer_set_cursor(input->pointer, |
| 3407 | input->pointer_enter_serial, |
Kristian Høgsberg | f337052 | 2012-06-20 23:04:41 -0400 | [diff] [blame] | 3408 | NULL, 0, 0); |
| 3409 | return; |
| 3410 | } |
| 3411 | |
Ander Conselvan de Oliveira | 8062007 | 2012-06-15 17:27:36 +0300 | [diff] [blame] | 3412 | if (input->current_cursor == CURSOR_UNSET) |
| 3413 | return; |
Daniel Stone | a494f1d | 2012-06-18 19:31:12 +0100 | [diff] [blame] | 3414 | cursor = input->display->cursors[input->current_cursor]; |
| 3415 | if (!cursor) |
| 3416 | return; |
Ander Conselvan de Oliveira | 8062007 | 2012-06-15 17:27:36 +0300 | [diff] [blame] | 3417 | |
| 3418 | /* FIXME We don't have the current time on the first call so we set |
| 3419 | * the animation start to the time of the first frame callback. */ |
| 3420 | if (time == 0) |
| 3421 | input->cursor_anim_start = 0; |
| 3422 | else if (input->cursor_anim_start == 0) |
| 3423 | input->cursor_anim_start = time; |
| 3424 | |
| 3425 | if (time == 0 || input->cursor_anim_start == 0) |
| 3426 | i = 0; |
| 3427 | else |
| 3428 | i = wl_cursor_frame(cursor, time - input->cursor_anim_start); |
| 3429 | |
Pekka Paalanen | bc10638 | 2012-10-10 12:49:31 +0300 | [diff] [blame] | 3430 | if (cursor->image_count > 1) { |
| 3431 | input->cursor_frame_cb = |
| 3432 | wl_surface_frame(input->pointer_surface); |
| 3433 | wl_callback_add_listener(input->cursor_frame_cb, |
| 3434 | &pointer_surface_listener, input); |
| 3435 | } |
| 3436 | |
Ander Conselvan de Oliveira | 8062007 | 2012-06-15 17:27:36 +0300 | [diff] [blame] | 3437 | input_set_pointer_image_index(input, i); |
Ander Conselvan de Oliveira | 8062007 | 2012-06-15 17:27:36 +0300 | [diff] [blame] | 3438 | } |
| 3439 | |
| 3440 | static const struct wl_callback_listener pointer_surface_listener = { |
| 3441 | pointer_surface_frame_callback |
| 3442 | }; |
| 3443 | |
Kristian Høgsberg | 7cee197 | 2012-06-04 23:37:42 -0400 | [diff] [blame] | 3444 | void |
| 3445 | input_set_pointer_image(struct input *input, int pointer) |
| 3446 | { |
Ander Conselvan de Oliveira | ddca496 | 2012-07-16 14:15:49 +0300 | [diff] [blame] | 3447 | int force = 0; |
| 3448 | |
Kristian Høgsberg | e530a0a | 2012-10-29 16:42:26 -0400 | [diff] [blame] | 3449 | if (!input->pointer) |
| 3450 | return; |
| 3451 | |
Ander Conselvan de Oliveira | ddca496 | 2012-07-16 14:15:49 +0300 | [diff] [blame] | 3452 | if (input->pointer_enter_serial > input->cursor_serial) |
| 3453 | force = 1; |
| 3454 | |
| 3455 | if (!force && pointer == input->current_cursor) |
Kristian Høgsberg | 7cee197 | 2012-06-04 23:37:42 -0400 | [diff] [blame] | 3456 | return; |
| 3457 | |
Ander Conselvan de Oliveira | 8062007 | 2012-06-15 17:27:36 +0300 | [diff] [blame] | 3458 | input->current_cursor = pointer; |
Kristian Høgsberg | 11f600d | 2012-06-22 10:52:58 -0400 | [diff] [blame] | 3459 | input->cursor_serial = input->pointer_enter_serial; |
Ander Conselvan de Oliveira | 8062007 | 2012-06-15 17:27:36 +0300 | [diff] [blame] | 3460 | if (!input->cursor_frame_cb) |
| 3461 | pointer_surface_frame_callback(input, NULL, 0); |
Ander Conselvan de Oliveira | ddca496 | 2012-07-16 14:15:49 +0300 | [diff] [blame] | 3462 | else if (force) { |
| 3463 | /* The current frame callback may be stuck if, for instance, |
| 3464 | * the set cursor request was processed by the server after |
| 3465 | * this client lost the focus. In this case the cursor surface |
| 3466 | * might not be mapped and the frame callback wouldn't ever |
| 3467 | * complete. Send a set_cursor and attach to try to map the |
| 3468 | * cursor surface again so that the callback will finish */ |
| 3469 | input_set_pointer_image_index(input, 0); |
| 3470 | } |
Kristian Høgsberg | 7cee197 | 2012-06-04 23:37:42 -0400 | [diff] [blame] | 3471 | } |
| 3472 | |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 3473 | struct wl_data_device * |
| 3474 | input_get_data_device(struct input *input) |
| 3475 | { |
| 3476 | return input->data_device; |
| 3477 | } |
| 3478 | |
| 3479 | void |
| 3480 | input_set_selection(struct input *input, |
| 3481 | struct wl_data_source *source, uint32_t time) |
| 3482 | { |
| 3483 | wl_data_device_set_selection(input->data_device, source, time); |
| 3484 | } |
| 3485 | |
| 3486 | void |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 3487 | input_accept(struct input *input, const char *type) |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 3488 | { |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 3489 | wl_data_offer_accept(input->drag_offer->offer, |
| 3490 | input->pointer_enter_serial, type); |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | static void |
| 3494 | offer_io_func(struct task *task, uint32_t events) |
| 3495 | { |
| 3496 | struct data_offer *offer = |
| 3497 | container_of(task, struct data_offer, io_task); |
| 3498 | unsigned int len; |
| 3499 | char buffer[4096]; |
| 3500 | |
| 3501 | len = read(offer->fd, buffer, sizeof buffer); |
| 3502 | offer->func(buffer, len, |
| 3503 | offer->x, offer->y, offer->user_data); |
| 3504 | |
| 3505 | if (len == 0) { |
| 3506 | close(offer->fd); |
| 3507 | data_offer_destroy(offer); |
| 3508 | } |
| 3509 | } |
| 3510 | |
| 3511 | static void |
| 3512 | data_offer_receive_data(struct data_offer *offer, const char *mime_type, |
| 3513 | data_func_t func, void *user_data) |
| 3514 | { |
| 3515 | int p[2]; |
| 3516 | |
Jonas Ådahl | 3685c3a | 2012-03-30 23:10:27 +0200 | [diff] [blame] | 3517 | if (pipe2(p, O_CLOEXEC) == -1) |
| 3518 | return; |
| 3519 | |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 3520 | wl_data_offer_receive(offer->offer, mime_type, p[1]); |
| 3521 | close(p[1]); |
| 3522 | |
| 3523 | offer->io_task.run = offer_io_func; |
| 3524 | offer->fd = p[0]; |
| 3525 | offer->func = func; |
| 3526 | offer->refcount++; |
| 3527 | offer->user_data = user_data; |
| 3528 | |
| 3529 | display_watch_fd(offer->input->display, |
| 3530 | offer->fd, EPOLLIN, &offer->io_task); |
| 3531 | } |
| 3532 | |
| 3533 | void |
| 3534 | input_receive_drag_data(struct input *input, const char *mime_type, |
| 3535 | data_func_t func, void *data) |
| 3536 | { |
| 3537 | data_offer_receive_data(input->drag_offer, mime_type, func, data); |
| 3538 | input->drag_offer->x = input->sx; |
| 3539 | input->drag_offer->y = input->sy; |
| 3540 | } |
| 3541 | |
| 3542 | int |
Kristian Høgsberg | 0749e3f | 2013-09-04 20:41:06 -0700 | [diff] [blame] | 3543 | input_receive_drag_data_to_fd(struct input *input, |
| 3544 | const char *mime_type, int fd) |
| 3545 | { |
| 3546 | if (input->drag_offer) |
| 3547 | wl_data_offer_receive(input->drag_offer->offer, mime_type, fd); |
| 3548 | |
| 3549 | return 0; |
| 3550 | } |
| 3551 | |
| 3552 | int |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 3553 | input_receive_selection_data(struct input *input, const char *mime_type, |
| 3554 | data_func_t func, void *data) |
| 3555 | { |
| 3556 | char **p; |
| 3557 | |
| 3558 | if (input->selection_offer == NULL) |
| 3559 | return -1; |
| 3560 | |
| 3561 | for (p = input->selection_offer->types.data; *p; p++) |
| 3562 | if (strcmp(mime_type, *p) == 0) |
| 3563 | break; |
| 3564 | |
| 3565 | if (*p == NULL) |
| 3566 | return -1; |
| 3567 | |
| 3568 | data_offer_receive_data(input->selection_offer, |
| 3569 | mime_type, func, data); |
| 3570 | return 0; |
Kristian Høgsberg | 41da908 | 2010-11-30 14:01:07 -0500 | [diff] [blame] | 3571 | } |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 3572 | |
Kristian Høgsberg | e7aaec3 | 2011-12-27 13:50:04 -0500 | [diff] [blame] | 3573 | int |
| 3574 | input_receive_selection_data_to_fd(struct input *input, |
| 3575 | const char *mime_type, int fd) |
| 3576 | { |
Kristian Høgsberg | a4b3d0e | 2012-05-31 23:30:32 -0400 | [diff] [blame] | 3577 | if (input->selection_offer) |
| 3578 | wl_data_offer_receive(input->selection_offer->offer, |
| 3579 | mime_type, fd); |
Kristian Høgsberg | e7aaec3 | 2011-12-27 13:50:04 -0500 | [diff] [blame] | 3580 | |
| 3581 | return 0; |
| 3582 | } |
| 3583 | |
Kristian Høgsberg | 41da908 | 2010-11-30 14:01:07 -0500 | [diff] [blame] | 3584 | void |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 3585 | window_move(struct window *window, struct input *input, uint32_t serial) |
Kristian Høgsberg | 82da52b | 2010-12-17 09:53:12 -0500 | [diff] [blame] | 3586 | { |
Pekka Paalanen | 6b2dc91 | 2011-11-29 10:25:08 +0200 | [diff] [blame] | 3587 | if (!window->shell_surface) |
| 3588 | return; |
| 3589 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 3590 | wl_shell_surface_move(window->shell_surface, input->seat, serial); |
Kristian Høgsberg | 82da52b | 2010-12-17 09:53:12 -0500 | [diff] [blame] | 3591 | } |
| 3592 | |
Rusty Lynch | 1084da5 | 2013-08-15 09:10:08 -0700 | [diff] [blame] | 3593 | void |
| 3594 | window_touch_move(struct window *window, struct input *input, uint32_t serial) |
| 3595 | { |
| 3596 | if (!window->shell_surface) |
| 3597 | return; |
| 3598 | |
| 3599 | wl_shell_surface_move(window->shell_surface, input->seat, |
| 3600 | window->display->serial); |
| 3601 | } |
| 3602 | |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 3603 | static void |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 3604 | surface_set_synchronized(struct surface *surface) |
| 3605 | { |
| 3606 | if (!surface->subsurface) |
| 3607 | return; |
| 3608 | |
| 3609 | if (surface->synchronized) |
| 3610 | return; |
| 3611 | |
| 3612 | wl_subsurface_set_sync(surface->subsurface); |
| 3613 | surface->synchronized = 1; |
| 3614 | } |
| 3615 | |
| 3616 | static void |
| 3617 | surface_set_synchronized_default(struct surface *surface) |
| 3618 | { |
| 3619 | if (!surface->subsurface) |
| 3620 | return; |
| 3621 | |
| 3622 | if (surface->synchronized == surface->synchronized_default) |
| 3623 | return; |
| 3624 | |
| 3625 | if (surface->synchronized_default) |
| 3626 | wl_subsurface_set_sync(surface->subsurface); |
| 3627 | else |
| 3628 | wl_subsurface_set_desync(surface->subsurface); |
| 3629 | |
| 3630 | surface->synchronized = surface->synchronized_default; |
| 3631 | } |
| 3632 | |
| 3633 | static void |
Pekka Paalanen | b1cd9b1 | 2013-02-13 16:17:18 +0200 | [diff] [blame] | 3634 | surface_resize(struct surface *surface) |
Pekka Paalanen | 811ec4f | 2013-02-13 16:17:14 +0200 | [diff] [blame] | 3635 | { |
Pekka Paalanen | b1cd9b1 | 2013-02-13 16:17:18 +0200 | [diff] [blame] | 3636 | struct widget *widget = surface->widget; |
| 3637 | struct wl_compositor *compositor = widget->window->display->compositor; |
Pekka Paalanen | 811ec4f | 2013-02-13 16:17:14 +0200 | [diff] [blame] | 3638 | |
Pekka Paalanen | b1cd9b1 | 2013-02-13 16:17:18 +0200 | [diff] [blame] | 3639 | if (surface->input_region) { |
| 3640 | wl_region_destroy(surface->input_region); |
| 3641 | surface->input_region = NULL; |
Kristian Høgsberg | 010f98b | 2012-02-23 17:30:45 -0500 | [diff] [blame] | 3642 | } |
| 3643 | |
Pekka Paalanen | b1cd9b1 | 2013-02-13 16:17:18 +0200 | [diff] [blame] | 3644 | if (surface->opaque_region) |
| 3645 | wl_region_destroy(surface->opaque_region); |
Ander Conselvan de Oliveira | ddd3e27 | 2012-11-30 17:34:23 +0200 | [diff] [blame] | 3646 | |
Pekka Paalanen | b1cd9b1 | 2013-02-13 16:17:18 +0200 | [diff] [blame] | 3647 | surface->opaque_region = wl_compositor_create_region(compositor); |
Kristian Høgsberg | 010f98b | 2012-02-23 17:30:45 -0500 | [diff] [blame] | 3648 | |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 3649 | if (widget->resize_handler) |
| 3650 | widget->resize_handler(widget, |
Kristian Høgsberg | 0d1c062 | 2012-01-31 15:30:47 -0500 | [diff] [blame] | 3651 | widget->allocation.width, |
| 3652 | widget->allocation.height, |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 3653 | widget->user_data); |
| 3654 | |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 3655 | if (surface->subsurface && |
| 3656 | (surface->allocation.x != widget->allocation.x || |
| 3657 | surface->allocation.y != widget->allocation.y)) { |
| 3658 | wl_subsurface_set_position(surface->subsurface, |
| 3659 | widget->allocation.x, |
| 3660 | widget->allocation.y); |
| 3661 | } |
Pekka Paalanen | b1cd9b1 | 2013-02-13 16:17:18 +0200 | [diff] [blame] | 3662 | if (surface->allocation.width != widget->allocation.width || |
| 3663 | surface->allocation.height != widget->allocation.height) { |
Pekka Paalanen | b1cd9b1 | 2013-02-13 16:17:18 +0200 | [diff] [blame] | 3664 | window_schedule_redraw(widget->window); |
| 3665 | } |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 3666 | surface->allocation = widget->allocation; |
Ander Conselvan de Oliveira | ddd3e27 | 2012-11-30 17:34:23 +0200 | [diff] [blame] | 3667 | |
| 3668 | if (widget->opaque) |
Pekka Paalanen | b1cd9b1 | 2013-02-13 16:17:18 +0200 | [diff] [blame] | 3669 | wl_region_add(surface->opaque_region, 0, 0, |
Ander Conselvan de Oliveira | ddd3e27 | 2012-11-30 17:34:23 +0200 | [diff] [blame] | 3670 | widget->allocation.width, |
| 3671 | widget->allocation.height); |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 3672 | } |
| 3673 | |
Pekka Paalanen | b1cd9b1 | 2013-02-13 16:17:18 +0200 | [diff] [blame] | 3674 | static void |
Pekka Paalanen | e9297f8 | 2013-04-25 13:57:51 +0300 | [diff] [blame] | 3675 | hack_prevent_EGL_sub_surface_deadlock(struct window *window) |
| 3676 | { |
| 3677 | /* |
| 3678 | * This hack should be removed, when EGL respects |
| 3679 | * eglSwapInterval(0). |
| 3680 | * |
| 3681 | * If this window has sub-surfaces, especially a free-running |
| 3682 | * EGL-widget, we need to post the parent surface once with |
| 3683 | * all the old state to guarantee, that the EGL-widget will |
| 3684 | * receive its frame callback soon. Otherwise, a forced call |
| 3685 | * to eglSwapBuffers may end up blocking, waiting for a frame |
| 3686 | * event that will never come, because we will commit the parent |
| 3687 | * surface with all new state only after eglSwapBuffers returns. |
| 3688 | * |
| 3689 | * This assumes, that: |
| 3690 | * 1. When the EGL widget's resize hook is called, it pauses. |
| 3691 | * 2. When the EGL widget's redraw hook is called, it forces a |
| 3692 | * repaint and a call to eglSwapBuffers(), and maybe resumes. |
| 3693 | * In a single threaded application condition 1 is a no-op. |
| 3694 | * |
| 3695 | * XXX: This should actually be after the surface_resize() calls, |
| 3696 | * but cannot, because then it would commit the incomplete state |
| 3697 | * accumulated from the widget resize hooks. |
| 3698 | */ |
| 3699 | if (window->subsurface_list.next != &window->main_surface->link || |
| 3700 | window->subsurface_list.prev != &window->main_surface->link) |
| 3701 | wl_surface_commit(window->main_surface->surface); |
| 3702 | } |
| 3703 | |
| 3704 | static void |
Pekka Paalanen | b1cd9b1 | 2013-02-13 16:17:18 +0200 | [diff] [blame] | 3705 | idle_resize(struct window *window) |
| 3706 | { |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 3707 | struct surface *surface; |
| 3708 | |
Pekka Paalanen | b1cd9b1 | 2013-02-13 16:17:18 +0200 | [diff] [blame] | 3709 | window->resize_needed = 0; |
Pekka Paalanen | 40cb67b | 2013-04-25 13:57:49 +0300 | [diff] [blame] | 3710 | window->redraw_needed = 1; |
Pekka Paalanen | b1cd9b1 | 2013-02-13 16:17:18 +0200 | [diff] [blame] | 3711 | |
Pekka Paalanen | 7123388 | 2013-04-25 13:57:53 +0300 | [diff] [blame] | 3712 | DBG("from %dx%d to %dx%d\n", |
| 3713 | window->main_surface->server_allocation.width, |
| 3714 | window->main_surface->server_allocation.height, |
| 3715 | window->pending_allocation.width, |
| 3716 | window->pending_allocation.height); |
| 3717 | |
Pekka Paalanen | e9297f8 | 2013-04-25 13:57:51 +0300 | [diff] [blame] | 3718 | hack_prevent_EGL_sub_surface_deadlock(window); |
| 3719 | |
Pekka Paalanen | b1cd9b1 | 2013-02-13 16:17:18 +0200 | [diff] [blame] | 3720 | widget_set_allocation(window->main_surface->widget, |
| 3721 | window->pending_allocation.x, |
| 3722 | window->pending_allocation.y, |
| 3723 | window->pending_allocation.width, |
| 3724 | window->pending_allocation.height); |
| 3725 | |
| 3726 | surface_resize(window->main_surface); |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 3727 | |
| 3728 | /* The main surface is in the list, too. Main surface's |
| 3729 | * resize_handler is responsible for calling widget_set_allocation() |
| 3730 | * on all sub-surface root widgets, so they will be resized |
| 3731 | * properly. |
| 3732 | */ |
| 3733 | wl_list_for_each(surface, &window->subsurface_list, link) { |
| 3734 | if (surface == window->main_surface) |
| 3735 | continue; |
| 3736 | |
| 3737 | surface_set_synchronized(surface); |
| 3738 | surface_resize(surface); |
| 3739 | } |
Pekka Paalanen | b1cd9b1 | 2013-02-13 16:17:18 +0200 | [diff] [blame] | 3740 | } |
| 3741 | |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 3742 | void |
| 3743 | window_schedule_resize(struct window *window, int width, int height) |
| 3744 | { |
Kristian Høgsberg | 53ec560 | 2013-10-21 15:05:49 -0700 | [diff] [blame] | 3745 | /* We should probably get these numbers from the theme. */ |
| 3746 | const int min_width = 200, min_height = 200; |
| 3747 | |
Kristian Høgsberg | 0d1c062 | 2012-01-31 15:30:47 -0500 | [diff] [blame] | 3748 | window->pending_allocation.x = 0; |
| 3749 | window->pending_allocation.y = 0; |
| 3750 | window->pending_allocation.width = width; |
| 3751 | window->pending_allocation.height = height; |
| 3752 | |
Kristian Høgsberg | 53ec560 | 2013-10-21 15:05:49 -0700 | [diff] [blame] | 3753 | if (window->min_allocation.width == 0) { |
Kristian Høgsberg | afb9828 | 2013-10-21 15:23:17 -0700 | [diff] [blame] | 3754 | if (width < min_width && window->frame) |
Kristian Høgsberg | 53ec560 | 2013-10-21 15:05:49 -0700 | [diff] [blame] | 3755 | window->min_allocation.width = min_width; |
| 3756 | else |
| 3757 | window->min_allocation.width = width; |
Kristian Høgsberg | afb9828 | 2013-10-21 15:23:17 -0700 | [diff] [blame] | 3758 | if (height < min_height && window->frame) |
Kristian Høgsberg | 53ec560 | 2013-10-21 15:05:49 -0700 | [diff] [blame] | 3759 | window->min_allocation.height = min_height; |
| 3760 | else |
Kristian Høgsberg | afb9828 | 2013-10-21 15:23:17 -0700 | [diff] [blame] | 3761 | window->min_allocation.height = height; |
Kristian Høgsberg | 53ec560 | 2013-10-21 15:05:49 -0700 | [diff] [blame] | 3762 | } |
| 3763 | |
Kristian Høgsberg | d3a1965 | 2012-07-20 11:32:51 -0400 | [diff] [blame] | 3764 | if (window->pending_allocation.width < window->min_allocation.width) |
| 3765 | window->pending_allocation.width = window->min_allocation.width; |
| 3766 | if (window->pending_allocation.height < window->min_allocation.height) |
| 3767 | window->pending_allocation.height = window->min_allocation.height; |
| 3768 | |
Kristian Høgsberg | 42b4f80 | 2012-03-26 13:49:29 -0400 | [diff] [blame] | 3769 | window->resize_needed = 1; |
| 3770 | window_schedule_redraw(window); |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 3771 | } |
| 3772 | |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 3773 | void |
| 3774 | widget_schedule_resize(struct widget *widget, int32_t width, int32_t height) |
| 3775 | { |
Kristian Høgsberg | 29af3eb | 2012-01-10 22:41:05 -0500 | [diff] [blame] | 3776 | window_schedule_resize(widget->window, width, height); |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 3777 | } |
| 3778 | |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 3779 | static void |
Scott Moreau | ff1db4a | 2012-04-17 19:06:18 -0600 | [diff] [blame] | 3780 | handle_ping(void *data, struct wl_shell_surface *shell_surface, |
| 3781 | uint32_t serial) |
| 3782 | { |
| 3783 | wl_shell_surface_pong(shell_surface, serial); |
| 3784 | } |
| 3785 | |
| 3786 | static void |
Pekka Paalanen | 9d1613e | 2011-11-25 12:09:16 +0200 | [diff] [blame] | 3787 | handle_configure(void *data, struct wl_shell_surface *shell_surface, |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 3788 | uint32_t edges, int32_t width, int32_t height) |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 3789 | { |
Pekka Paalanen | 9d1613e | 2011-11-25 12:09:16 +0200 | [diff] [blame] | 3790 | struct window *window = data; |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 3791 | |
Tim Wiederhake | b6761dc | 2011-01-17 17:50:07 +0100 | [diff] [blame] | 3792 | window->resize_edges = edges; |
Kristian Høgsberg | 0d1c062 | 2012-01-31 15:30:47 -0500 | [diff] [blame] | 3793 | window_schedule_resize(window, width, height); |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 3794 | } |
| 3795 | |
Kristian Høgsberg | b3cca0a | 2012-01-04 22:19:14 -0500 | [diff] [blame] | 3796 | static void |
Pekka Paalanen | 6d174cf | 2012-01-19 15:17:59 +0200 | [diff] [blame] | 3797 | menu_destroy(struct menu *menu) |
| 3798 | { |
| 3799 | widget_destroy(menu->widget); |
| 3800 | window_destroy(menu->window); |
Kristian Høgsberg | c680e90 | 2013-10-23 21:49:30 -0700 | [diff] [blame] | 3801 | frame_destroy(menu->frame); |
Pekka Paalanen | 6d174cf | 2012-01-19 15:17:59 +0200 | [diff] [blame] | 3802 | free(menu); |
| 3803 | } |
| 3804 | |
| 3805 | static void |
Kristian Høgsberg | b3cca0a | 2012-01-04 22:19:14 -0500 | [diff] [blame] | 3806 | handle_popup_done(void *data, struct wl_shell_surface *shell_surface) |
| 3807 | { |
| 3808 | struct window *window = data; |
Pekka Paalanen | ac95f3e | 2013-02-13 16:17:17 +0200 | [diff] [blame] | 3809 | struct menu *menu = window->main_surface->widget->user_data; |
Kristian Høgsberg | 831dd52 | 2012-01-10 23:46:33 -0500 | [diff] [blame] | 3810 | |
Kristian Høgsberg | b3cca0a | 2012-01-04 22:19:14 -0500 | [diff] [blame] | 3811 | /* FIXME: Need more context in this event, at least the input |
Kristian Høgsberg | 831dd52 | 2012-01-10 23:46:33 -0500 | [diff] [blame] | 3812 | * device. Or just use wl_callback. And this really needs to |
| 3813 | * be a window vfunc that the menu can set. And we need the |
| 3814 | * time. */ |
Kristian Høgsberg | b3cca0a | 2012-01-04 22:19:14 -0500 | [diff] [blame] | 3815 | |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 3816 | input_ungrab(menu->input); |
Pekka Paalanen | 6d174cf | 2012-01-19 15:17:59 +0200 | [diff] [blame] | 3817 | menu_destroy(menu); |
Kristian Høgsberg | b3cca0a | 2012-01-04 22:19:14 -0500 | [diff] [blame] | 3818 | } |
| 3819 | |
Pekka Paalanen | 9d1613e | 2011-11-25 12:09:16 +0200 | [diff] [blame] | 3820 | static const struct wl_shell_surface_listener shell_surface_listener = { |
Scott Moreau | ff1db4a | 2012-04-17 19:06:18 -0600 | [diff] [blame] | 3821 | handle_ping, |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 3822 | handle_configure, |
Kristian Høgsberg | b3cca0a | 2012-01-04 22:19:14 -0500 | [diff] [blame] | 3823 | handle_popup_done |
Kristian Høgsberg | 83fc061 | 2010-08-04 22:44:55 -0400 | [diff] [blame] | 3824 | }; |
| 3825 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 3826 | void |
Benjamin Franzke | cff904e | 2011-02-18 23:00:55 +0100 | [diff] [blame] | 3827 | window_get_allocation(struct window *window, |
| 3828 | struct rectangle *allocation) |
| 3829 | { |
Pekka Paalanen | 811ec4f | 2013-02-13 16:17:14 +0200 | [diff] [blame] | 3830 | *allocation = window->main_surface->allocation; |
Benjamin Franzke | cff904e | 2011-02-18 23:00:55 +0100 | [diff] [blame] | 3831 | } |
| 3832 | |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 3833 | static void |
Kristian Høgsberg | 441338c | 2012-01-10 13:52:34 -0500 | [diff] [blame] | 3834 | widget_redraw(struct widget *widget) |
| 3835 | { |
| 3836 | struct widget *child; |
| 3837 | |
| 3838 | if (widget->redraw_handler) |
| 3839 | widget->redraw_handler(widget, widget->user_data); |
| 3840 | wl_list_for_each(child, &widget->child_list, link) |
| 3841 | widget_redraw(child); |
| 3842 | } |
| 3843 | |
| 3844 | static void |
Kristian Høgsberg | 6bd4d97 | 2012-03-24 14:42:09 -0400 | [diff] [blame] | 3845 | frame_callback(void *data, struct wl_callback *callback, uint32_t time) |
| 3846 | { |
Pekka Paalanen | 40cb67b | 2013-04-25 13:57:49 +0300 | [diff] [blame] | 3847 | struct surface *surface = data; |
Kristian Høgsberg | 6bd4d97 | 2012-03-24 14:42:09 -0400 | [diff] [blame] | 3848 | |
Pekka Paalanen | 40cb67b | 2013-04-25 13:57:49 +0300 | [diff] [blame] | 3849 | assert(callback == surface->frame_cb); |
Pekka Paalanen | 7123388 | 2013-04-25 13:57:53 +0300 | [diff] [blame] | 3850 | DBG_OBJ(callback, "done\n"); |
Kristian Høgsberg | 6bd4d97 | 2012-03-24 14:42:09 -0400 | [diff] [blame] | 3851 | wl_callback_destroy(callback); |
Pekka Paalanen | 40cb67b | 2013-04-25 13:57:49 +0300 | [diff] [blame] | 3852 | surface->frame_cb = NULL; |
| 3853 | |
Pekka Paalanen | 7ff7a80 | 2013-04-25 13:57:50 +0300 | [diff] [blame] | 3854 | surface->last_time = time; |
| 3855 | |
Pekka Paalanen | 7123388 | 2013-04-25 13:57:53 +0300 | [diff] [blame] | 3856 | if (surface->redraw_needed || surface->window->redraw_needed) { |
| 3857 | DBG_OBJ(surface->surface, "window_schedule_redraw_task\n"); |
Pekka Paalanen | 40cb67b | 2013-04-25 13:57:49 +0300 | [diff] [blame] | 3858 | window_schedule_redraw_task(surface->window); |
Pekka Paalanen | 7123388 | 2013-04-25 13:57:53 +0300 | [diff] [blame] | 3859 | } |
Kristian Høgsberg | 6bd4d97 | 2012-03-24 14:42:09 -0400 | [diff] [blame] | 3860 | } |
| 3861 | |
| 3862 | static const struct wl_callback_listener listener = { |
| 3863 | frame_callback |
| 3864 | }; |
| 3865 | |
| 3866 | static void |
Pekka Paalanen | 40cb67b | 2013-04-25 13:57:49 +0300 | [diff] [blame] | 3867 | surface_redraw(struct surface *surface) |
| 3868 | { |
Pekka Paalanen | 7123388 | 2013-04-25 13:57:53 +0300 | [diff] [blame] | 3869 | DBG_OBJ(surface->surface, "begin\n"); |
| 3870 | |
Pekka Paalanen | 40cb67b | 2013-04-25 13:57:49 +0300 | [diff] [blame] | 3871 | if (!surface->window->redraw_needed && !surface->redraw_needed) |
| 3872 | return; |
| 3873 | |
| 3874 | /* Whole-window redraw forces a redraw even if the previous has |
| 3875 | * not yet hit the screen. |
| 3876 | */ |
| 3877 | if (surface->frame_cb) { |
| 3878 | if (!surface->window->redraw_needed) |
| 3879 | return; |
| 3880 | |
Pekka Paalanen | 7123388 | 2013-04-25 13:57:53 +0300 | [diff] [blame] | 3881 | DBG_OBJ(surface->frame_cb, "cancelled\n"); |
Pekka Paalanen | 40cb67b | 2013-04-25 13:57:49 +0300 | [diff] [blame] | 3882 | wl_callback_destroy(surface->frame_cb); |
| 3883 | } |
| 3884 | |
| 3885 | surface->frame_cb = wl_surface_frame(surface->surface); |
| 3886 | wl_callback_add_listener(surface->frame_cb, &listener, surface); |
Pekka Paalanen | 7123388 | 2013-04-25 13:57:53 +0300 | [diff] [blame] | 3887 | DBG_OBJ(surface->frame_cb, "new\n"); |
Pekka Paalanen | 40cb67b | 2013-04-25 13:57:49 +0300 | [diff] [blame] | 3888 | |
| 3889 | surface->redraw_needed = 0; |
Pekka Paalanen | 7123388 | 2013-04-25 13:57:53 +0300 | [diff] [blame] | 3890 | DBG_OBJ(surface->surface, "-> widget_redraw\n"); |
Pekka Paalanen | 40cb67b | 2013-04-25 13:57:49 +0300 | [diff] [blame] | 3891 | widget_redraw(surface->widget); |
Pekka Paalanen | 7123388 | 2013-04-25 13:57:53 +0300 | [diff] [blame] | 3892 | DBG_OBJ(surface->surface, "done\n"); |
Pekka Paalanen | 40cb67b | 2013-04-25 13:57:49 +0300 | [diff] [blame] | 3893 | } |
| 3894 | |
| 3895 | static void |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 3896 | idle_redraw(struct task *task, uint32_t events) |
Kristian Høgsberg | 80d746f | 2010-06-14 23:52:50 -0400 | [diff] [blame] | 3897 | { |
Kristian Høgsberg | 42b4f80 | 2012-03-26 13:49:29 -0400 | [diff] [blame] | 3898 | struct window *window = container_of(task, struct window, redraw_task); |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 3899 | struct surface *surface; |
Kristian Høgsberg | 80d746f | 2010-06-14 23:52:50 -0400 | [diff] [blame] | 3900 | |
Pekka Paalanen | 7123388 | 2013-04-25 13:57:53 +0300 | [diff] [blame] | 3901 | DBG(" --------- \n"); |
| 3902 | |
Pekka Paalanen | eebff54 | 2013-04-25 13:57:52 +0300 | [diff] [blame] | 3903 | wl_list_init(&window->redraw_task.link); |
| 3904 | window->redraw_task_scheduled = 0; |
| 3905 | |
| 3906 | if (window->resize_needed) { |
| 3907 | /* throttle resizing to the main surface display */ |
Pekka Paalanen | 7123388 | 2013-04-25 13:57:53 +0300 | [diff] [blame] | 3908 | if (window->main_surface->frame_cb) { |
| 3909 | DBG_OBJ(window->main_surface->frame_cb, "pending\n"); |
Pekka Paalanen | eebff54 | 2013-04-25 13:57:52 +0300 | [diff] [blame] | 3910 | return; |
Pekka Paalanen | 7123388 | 2013-04-25 13:57:53 +0300 | [diff] [blame] | 3911 | } |
Pekka Paalanen | eebff54 | 2013-04-25 13:57:52 +0300 | [diff] [blame] | 3912 | |
Kristian Høgsberg | 42b4f80 | 2012-03-26 13:49:29 -0400 | [diff] [blame] | 3913 | idle_resize(window); |
Pekka Paalanen | eebff54 | 2013-04-25 13:57:52 +0300 | [diff] [blame] | 3914 | } |
Kristian Høgsberg | 42b4f80 | 2012-03-26 13:49:29 -0400 | [diff] [blame] | 3915 | |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 3916 | wl_list_for_each(surface, &window->subsurface_list, link) |
Pekka Paalanen | 40cb67b | 2013-04-25 13:57:49 +0300 | [diff] [blame] | 3917 | surface_redraw(surface); |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 3918 | |
Kristian Høgsberg | 6bd4d97 | 2012-03-24 14:42:09 -0400 | [diff] [blame] | 3919 | window->redraw_needed = 0; |
Pekka Paalanen | bc10638 | 2012-10-10 12:49:31 +0300 | [diff] [blame] | 3920 | window_flush(window); |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 3921 | |
| 3922 | wl_list_for_each(surface, &window->subsurface_list, link) |
| 3923 | surface_set_synchronized_default(surface); |
Kristian Høgsberg | 80d746f | 2010-06-14 23:52:50 -0400 | [diff] [blame] | 3924 | } |
| 3925 | |
Pekka Paalanen | 40cb67b | 2013-04-25 13:57:49 +0300 | [diff] [blame] | 3926 | static void |
| 3927 | window_schedule_redraw_task(struct window *window) |
| 3928 | { |
| 3929 | if (window->configure_requests) |
| 3930 | return; |
| 3931 | if (!window->redraw_task_scheduled) { |
| 3932 | window->redraw_task.run = idle_redraw; |
| 3933 | display_defer(window->display, &window->redraw_task); |
| 3934 | window->redraw_task_scheduled = 1; |
| 3935 | } |
| 3936 | } |
| 3937 | |
Kristian Høgsberg | 80d746f | 2010-06-14 23:52:50 -0400 | [diff] [blame] | 3938 | void |
| 3939 | window_schedule_redraw(struct window *window) |
| 3940 | { |
Pekka Paalanen | 40cb67b | 2013-04-25 13:57:49 +0300 | [diff] [blame] | 3941 | struct surface *surface; |
| 3942 | |
Pekka Paalanen | 7123388 | 2013-04-25 13:57:53 +0300 | [diff] [blame] | 3943 | DBG_OBJ(window->main_surface->surface, "window %p\n", window); |
| 3944 | |
Pekka Paalanen | 40cb67b | 2013-04-25 13:57:49 +0300 | [diff] [blame] | 3945 | wl_list_for_each(surface, &window->subsurface_list, link) |
| 3946 | surface->redraw_needed = 1; |
| 3947 | |
| 3948 | window_schedule_redraw_task(window); |
Kristian Høgsberg | 80d746f | 2010-06-14 23:52:50 -0400 | [diff] [blame] | 3949 | } |
| 3950 | |
Kristian Høgsberg | 1671e11 | 2012-10-10 11:36:24 -0400 | [diff] [blame] | 3951 | int |
| 3952 | window_is_fullscreen(struct window *window) |
| 3953 | { |
| 3954 | return window->type == TYPE_FULLSCREEN; |
| 3955 | } |
| 3956 | |
MoD | 063a882 | 2013-03-02 23:43:57 +0000 | [diff] [blame] | 3957 | static void |
| 3958 | configure_request_completed(void *data, struct wl_callback *callback, uint32_t time) |
| 3959 | { |
| 3960 | struct window *window = data; |
| 3961 | |
| 3962 | wl_callback_destroy(callback); |
| 3963 | window->configure_requests--; |
| 3964 | |
| 3965 | if (!window->configure_requests) |
| 3966 | window_schedule_redraw(window); |
| 3967 | } |
| 3968 | |
| 3969 | static struct wl_callback_listener configure_request_listener = { |
| 3970 | configure_request_completed, |
| 3971 | }; |
| 3972 | |
| 3973 | static void |
| 3974 | window_defer_redraw_until_configure(struct window* window) |
| 3975 | { |
| 3976 | struct wl_callback *callback; |
| 3977 | |
Pekka Paalanen | 40cb67b | 2013-04-25 13:57:49 +0300 | [diff] [blame] | 3978 | if (window->redraw_task_scheduled) { |
MoD | 063a882 | 2013-03-02 23:43:57 +0000 | [diff] [blame] | 3979 | wl_list_remove(&window->redraw_task.link); |
Pekka Paalanen | 40cb67b | 2013-04-25 13:57:49 +0300 | [diff] [blame] | 3980 | window->redraw_task_scheduled = 0; |
MoD | 063a882 | 2013-03-02 23:43:57 +0000 | [diff] [blame] | 3981 | } |
| 3982 | |
| 3983 | callback = wl_display_sync(window->display->display); |
| 3984 | wl_callback_add_listener(callback, &configure_request_listener, window); |
| 3985 | window->configure_requests++; |
| 3986 | } |
| 3987 | |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 3988 | void |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 3989 | window_set_fullscreen(struct window *window, int fullscreen) |
| 3990 | { |
Kristian Høgsberg | 1517def | 2012-02-16 22:56:12 -0500 | [diff] [blame] | 3991 | if (!window->display->shell) |
| 3992 | return; |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 3993 | |
Kristian Høgsberg | 547da5a | 2011-09-13 20:58:00 -0400 | [diff] [blame] | 3994 | if ((window->type == TYPE_FULLSCREEN) == fullscreen) |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 3995 | return; |
| 3996 | |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 3997 | if (fullscreen) { |
Rafal Mielniczuk | c1a3cd1 | 2013-03-11 19:26:56 +0100 | [diff] [blame] | 3998 | window->saved_type = window->type; |
Rafal Mielniczuk | fc22be0 | 2013-03-11 19:26:55 +0100 | [diff] [blame] | 3999 | if (window->type == TYPE_TOPLEVEL) { |
| 4000 | window->saved_allocation = window->main_surface->allocation; |
| 4001 | } |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 4002 | window->type = TYPE_FULLSCREEN; |
Kristian Høgsberg | 1517def | 2012-02-16 22:56:12 -0500 | [diff] [blame] | 4003 | wl_shell_surface_set_fullscreen(window->shell_surface, |
Ander Conselvan de Oliveira | 5403f52 | 2012-12-14 13:37:23 -0200 | [diff] [blame] | 4004 | window->fullscreen_method, |
Kristian Høgsberg | 1517def | 2012-02-16 22:56:12 -0500 | [diff] [blame] | 4005 | 0, NULL); |
MoD | 063a882 | 2013-03-02 23:43:57 +0000 | [diff] [blame] | 4006 | window_defer_redraw_until_configure (window); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 4007 | } else { |
Rafal Mielniczuk | c1a3cd1 | 2013-03-11 19:26:56 +0100 | [diff] [blame] | 4008 | if (window->saved_type == TYPE_MAXIMIZED) { |
| 4009 | window_set_maximized(window, 1); |
| 4010 | } else { |
| 4011 | window->type = TYPE_TOPLEVEL; |
| 4012 | wl_shell_surface_set_toplevel(window->shell_surface); |
| 4013 | window_schedule_resize(window, |
| 4014 | window->saved_allocation.width, |
| 4015 | window->saved_allocation.height); |
| 4016 | } |
| 4017 | |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 4018 | } |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 4019 | } |
| 4020 | |
Ander Conselvan de Oliveira | 5403f52 | 2012-12-14 13:37:23 -0200 | [diff] [blame] | 4021 | void |
| 4022 | window_set_fullscreen_method(struct window *window, |
| 4023 | enum wl_shell_surface_fullscreen_method method) |
| 4024 | { |
| 4025 | window->fullscreen_method = method; |
| 4026 | } |
| 4027 | |
Kristian Høgsberg | 1671e11 | 2012-10-10 11:36:24 -0400 | [diff] [blame] | 4028 | int |
| 4029 | window_is_maximized(struct window *window) |
| 4030 | { |
| 4031 | return window->type == TYPE_MAXIMIZED; |
| 4032 | } |
| 4033 | |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 4034 | void |
Kristian Høgsberg | d6bcd7d | 2012-02-16 15:53:46 -0500 | [diff] [blame] | 4035 | window_set_maximized(struct window *window, int maximized) |
| 4036 | { |
| 4037 | if (!window->display->shell) |
| 4038 | return; |
| 4039 | |
| 4040 | if ((window->type == TYPE_MAXIMIZED) == maximized) |
| 4041 | return; |
| 4042 | |
| 4043 | if (window->type == TYPE_TOPLEVEL) { |
Pekka Paalanen | 811ec4f | 2013-02-13 16:17:14 +0200 | [diff] [blame] | 4044 | window->saved_allocation = window->main_surface->allocation; |
Kristian Høgsberg | d6bcd7d | 2012-02-16 15:53:46 -0500 | [diff] [blame] | 4045 | wl_shell_surface_set_maximized(window->shell_surface, NULL); |
| 4046 | window->type = TYPE_MAXIMIZED; |
MoD | 063a882 | 2013-03-02 23:43:57 +0000 | [diff] [blame] | 4047 | window_defer_redraw_until_configure(window); |
Rafal Mielniczuk | c1a3cd1 | 2013-03-11 19:26:56 +0100 | [diff] [blame] | 4048 | } else if (window->type == TYPE_FULLSCREEN) { |
| 4049 | wl_shell_surface_set_maximized(window->shell_surface, NULL); |
| 4050 | window->type = TYPE_MAXIMIZED; |
MoD | 063a882 | 2013-03-02 23:43:57 +0000 | [diff] [blame] | 4051 | window_defer_redraw_until_configure(window); |
Kristian Høgsberg | d6bcd7d | 2012-02-16 15:53:46 -0500 | [diff] [blame] | 4052 | } else { |
| 4053 | wl_shell_surface_set_toplevel(window->shell_surface); |
| 4054 | window->type = TYPE_TOPLEVEL; |
| 4055 | window_schedule_resize(window, |
| 4056 | window->saved_allocation.width, |
| 4057 | window->saved_allocation.height); |
| 4058 | } |
| 4059 | } |
| 4060 | |
| 4061 | void |
Kristian Høgsberg | c8c3734 | 2010-06-25 11:19:22 -0400 | [diff] [blame] | 4062 | window_set_user_data(struct window *window, void *data) |
| 4063 | { |
| 4064 | window->user_data = data; |
| 4065 | } |
| 4066 | |
Kristian Høgsberg | e9d37bd | 2010-09-02 20:22:42 -0400 | [diff] [blame] | 4067 | void * |
| 4068 | window_get_user_data(struct window *window) |
| 4069 | { |
| 4070 | return window->user_data; |
| 4071 | } |
| 4072 | |
Kristian Høgsberg | c8c3734 | 2010-06-25 11:19:22 -0400 | [diff] [blame] | 4073 | void |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 4074 | window_set_key_handler(struct window *window, |
Kristian Høgsberg | c8c3734 | 2010-06-25 11:19:22 -0400 | [diff] [blame] | 4075 | window_key_handler_t handler) |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 4076 | { |
| 4077 | window->key_handler = handler; |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 4078 | } |
| 4079 | |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 4080 | void |
| 4081 | window_set_keyboard_focus_handler(struct window *window, |
Kristian Høgsberg | c8c3734 | 2010-06-25 11:19:22 -0400 | [diff] [blame] | 4082 | window_keyboard_focus_handler_t handler) |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 4083 | { |
| 4084 | window->keyboard_focus_handler = handler; |
Kristian Høgsberg | 3c248cc | 2009-02-22 23:01:35 -0500 | [diff] [blame] | 4085 | } |
| 4086 | |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 4087 | void |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 4088 | window_set_data_handler(struct window *window, window_data_handler_t handler) |
| 4089 | { |
| 4090 | window->data_handler = handler; |
| 4091 | } |
| 4092 | |
| 4093 | void |
| 4094 | window_set_drop_handler(struct window *window, window_drop_handler_t handler) |
| 4095 | { |
| 4096 | window->drop_handler = handler; |
| 4097 | } |
| 4098 | |
| 4099 | void |
Kristian Høgsberg | 4f7dcd6 | 2012-01-06 21:59:05 -0500 | [diff] [blame] | 4100 | window_set_close_handler(struct window *window, |
| 4101 | window_close_handler_t handler) |
| 4102 | { |
| 4103 | window->close_handler = handler; |
| 4104 | } |
| 4105 | |
| 4106 | void |
Kristian Høgsberg | 67ace20 | 2012-07-23 21:56:31 -0400 | [diff] [blame] | 4107 | window_set_fullscreen_handler(struct window *window, |
| 4108 | window_fullscreen_handler_t handler) |
| 4109 | { |
| 4110 | window->fullscreen_handler = handler; |
| 4111 | } |
| 4112 | |
| 4113 | void |
Ander Conselvan de Oliveira | 15256f6 | 2012-11-30 17:34:25 +0200 | [diff] [blame] | 4114 | window_set_output_handler(struct window *window, |
| 4115 | window_output_handler_t handler) |
| 4116 | { |
| 4117 | window->output_handler = handler; |
| 4118 | } |
| 4119 | |
| 4120 | void |
Callum Lowcay | ef57a9b | 2011-01-14 20:46:23 +1300 | [diff] [blame] | 4121 | window_set_title(struct window *window, const char *title) |
| 4122 | { |
Kristian Høgsberg | d5fb9cc | 2011-01-25 12:45:37 -0500 | [diff] [blame] | 4123 | free(window->title); |
Callum Lowcay | ef57a9b | 2011-01-14 20:46:23 +1300 | [diff] [blame] | 4124 | window->title = strdup(title); |
Jason Ekstrand | 3f66cf9 | 2013-10-13 19:08:40 -0500 | [diff] [blame] | 4125 | if (window->frame) { |
| 4126 | frame_set_title(window->frame->frame, title); |
| 4127 | widget_schedule_redraw(window->frame->widget); |
| 4128 | } |
Kristian Høgsberg | 3e0fe5c | 2012-05-02 09:47:55 -0400 | [diff] [blame] | 4129 | if (window->shell_surface) |
| 4130 | wl_shell_surface_set_title(window->shell_surface, title); |
Callum Lowcay | ef57a9b | 2011-01-14 20:46:23 +1300 | [diff] [blame] | 4131 | } |
| 4132 | |
| 4133 | const char * |
| 4134 | window_get_title(struct window *window) |
| 4135 | { |
| 4136 | return window->title; |
| 4137 | } |
| 4138 | |
| 4139 | void |
Scott Moreau | 7a1b32a | 2012-05-27 14:25:02 -0600 | [diff] [blame] | 4140 | window_set_text_cursor_position(struct window *window, int32_t x, int32_t y) |
| 4141 | { |
| 4142 | struct text_cursor_position *text_cursor_position = |
| 4143 | window->display->text_cursor_position; |
| 4144 | |
Scott Moreau | 9295ce0 | 2012-06-01 12:46:10 -0600 | [diff] [blame] | 4145 | if (!text_cursor_position) |
Scott Moreau | 7a1b32a | 2012-05-27 14:25:02 -0600 | [diff] [blame] | 4146 | return; |
| 4147 | |
| 4148 | text_cursor_position_notify(text_cursor_position, |
Pekka Paalanen | 4e37374 | 2013-02-13 16:17:13 +0200 | [diff] [blame] | 4149 | window->main_surface->surface, |
| 4150 | wl_fixed_from_int(x), |
| 4151 | wl_fixed_from_int(y)); |
Scott Moreau | 7a1b32a | 2012-05-27 14:25:02 -0600 | [diff] [blame] | 4152 | } |
| 4153 | |
| 4154 | void |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 4155 | window_damage(struct window *window, int32_t x, int32_t y, |
| 4156 | int32_t width, int32_t height) |
| 4157 | { |
Pekka Paalanen | 4e37374 | 2013-02-13 16:17:13 +0200 | [diff] [blame] | 4158 | wl_surface_damage(window->main_surface->surface, x, y, width, height); |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 4159 | } |
| 4160 | |
Casey Dahlin | 9074db5 | 2012-04-19 22:50:09 -0400 | [diff] [blame] | 4161 | static void |
| 4162 | surface_enter(void *data, |
Rob Bradford | 7507b57 | 2012-05-15 17:55:34 +0100 | [diff] [blame] | 4163 | struct wl_surface *wl_surface, struct wl_output *wl_output) |
Casey Dahlin | 9074db5 | 2012-04-19 22:50:09 -0400 | [diff] [blame] | 4164 | { |
Rob Bradford | 7507b57 | 2012-05-15 17:55:34 +0100 | [diff] [blame] | 4165 | struct window *window = data; |
| 4166 | struct output *output; |
| 4167 | struct output *output_found = NULL; |
| 4168 | struct window_output *window_output; |
| 4169 | |
| 4170 | wl_list_for_each(output, &window->display->output_list, link) { |
| 4171 | if (output->output == wl_output) { |
| 4172 | output_found = output; |
| 4173 | break; |
| 4174 | } |
| 4175 | } |
| 4176 | |
| 4177 | if (!output_found) |
| 4178 | return; |
| 4179 | |
Brian Lovin | bc91926 | 2013-08-07 15:34:59 -0700 | [diff] [blame] | 4180 | window_output = xmalloc(sizeof *window_output); |
Rob Bradford | 7507b57 | 2012-05-15 17:55:34 +0100 | [diff] [blame] | 4181 | window_output->output = output_found; |
| 4182 | |
| 4183 | wl_list_insert (&window->window_output_list, &window_output->link); |
Ander Conselvan de Oliveira | 15256f6 | 2012-11-30 17:34:25 +0200 | [diff] [blame] | 4184 | |
| 4185 | if (window->output_handler) |
| 4186 | window->output_handler(window, output_found, 1, |
| 4187 | window->user_data); |
Casey Dahlin | 9074db5 | 2012-04-19 22:50:09 -0400 | [diff] [blame] | 4188 | } |
| 4189 | |
| 4190 | static void |
| 4191 | surface_leave(void *data, |
| 4192 | struct wl_surface *wl_surface, struct wl_output *output) |
| 4193 | { |
Rob Bradford | 7507b57 | 2012-05-15 17:55:34 +0100 | [diff] [blame] | 4194 | struct window *window = data; |
| 4195 | struct window_output *window_output; |
| 4196 | struct window_output *window_output_found = NULL; |
| 4197 | |
| 4198 | wl_list_for_each(window_output, &window->window_output_list, link) { |
| 4199 | if (window_output->output->output == output) { |
| 4200 | window_output_found = window_output; |
| 4201 | break; |
| 4202 | } |
| 4203 | } |
| 4204 | |
| 4205 | if (window_output_found) { |
| 4206 | wl_list_remove(&window_output_found->link); |
Ander Conselvan de Oliveira | 15256f6 | 2012-11-30 17:34:25 +0200 | [diff] [blame] | 4207 | |
| 4208 | if (window->output_handler) |
| 4209 | window->output_handler(window, window_output->output, |
| 4210 | 0, window->user_data); |
| 4211 | |
Rob Bradford | 7507b57 | 2012-05-15 17:55:34 +0100 | [diff] [blame] | 4212 | free(window_output_found); |
| 4213 | } |
Casey Dahlin | 9074db5 | 2012-04-19 22:50:09 -0400 | [diff] [blame] | 4214 | } |
| 4215 | |
| 4216 | static const struct wl_surface_listener surface_listener = { |
| 4217 | surface_enter, |
| 4218 | surface_leave |
| 4219 | }; |
| 4220 | |
Pekka Paalanen | 4e37374 | 2013-02-13 16:17:13 +0200 | [diff] [blame] | 4221 | static struct surface * |
| 4222 | surface_create(struct window *window) |
| 4223 | { |
| 4224 | struct display *display = window->display; |
| 4225 | struct surface *surface; |
| 4226 | |
Brian Lovin | bc91926 | 2013-08-07 15:34:59 -0700 | [diff] [blame] | 4227 | surface = xmalloc(sizeof *surface); |
| 4228 | memset(surface, 0, sizeof *surface); |
Pekka Paalanen | 4e37374 | 2013-02-13 16:17:13 +0200 | [diff] [blame] | 4229 | if (!surface) |
| 4230 | return NULL; |
| 4231 | |
| 4232 | surface->window = window; |
| 4233 | surface->surface = wl_compositor_create_surface(display->compositor); |
Alexander Larsson | 5e9b652 | 2013-05-22 14:41:28 +0200 | [diff] [blame] | 4234 | surface->buffer_scale = 1; |
Pekka Paalanen | 4e37374 | 2013-02-13 16:17:13 +0200 | [diff] [blame] | 4235 | wl_surface_add_listener(surface->surface, &surface_listener, window); |
| 4236 | |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 4237 | wl_list_insert(&window->subsurface_list, &surface->link); |
| 4238 | |
Pekka Paalanen | 4e37374 | 2013-02-13 16:17:13 +0200 | [diff] [blame] | 4239 | return surface; |
| 4240 | } |
| 4241 | |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 4242 | static struct window * |
Kristian Høgsberg | 962342c | 2012-06-26 16:29:50 -0400 | [diff] [blame] | 4243 | window_create_internal(struct display *display, |
| 4244 | struct window *parent, int type) |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 4245 | { |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 4246 | struct window *window; |
Pekka Paalanen | 02bba7c | 2013-02-13 16:17:15 +0200 | [diff] [blame] | 4247 | struct surface *surface; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 4248 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 4249 | window = xzalloc(sizeof *window); |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 4250 | wl_list_init(&window->subsurface_list); |
Kristian Høgsberg | 4097923 | 2008-11-25 22:40:39 -0500 | [diff] [blame] | 4251 | window->display = display; |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 4252 | window->parent = parent; |
Pekka Paalanen | 02bba7c | 2013-02-13 16:17:15 +0200 | [diff] [blame] | 4253 | |
| 4254 | surface = surface_create(window); |
| 4255 | window->main_surface = surface; |
| 4256 | |
Kristian Høgsberg | 962342c | 2012-06-26 16:29:50 -0400 | [diff] [blame] | 4257 | if (type != TYPE_CUSTOM && display->shell) { |
Pekka Paalanen | 6b2dc91 | 2011-11-29 10:25:08 +0200 | [diff] [blame] | 4258 | window->shell_surface = |
| 4259 | wl_shell_get_shell_surface(display->shell, |
Pekka Paalanen | 02bba7c | 2013-02-13 16:17:15 +0200 | [diff] [blame] | 4260 | surface->surface); |
Kristian Høgsberg | ce27841 | 2013-07-25 15:20:20 -0700 | [diff] [blame] | 4261 | fail_on_null(window->shell_surface); |
Pekka Paalanen | 6b2dc91 | 2011-11-29 10:25:08 +0200 | [diff] [blame] | 4262 | } |
Pekka Paalanen | 811ec4f | 2013-02-13 16:17:14 +0200 | [diff] [blame] | 4263 | |
Kristian Høgsberg | 962342c | 2012-06-26 16:29:50 -0400 | [diff] [blame] | 4264 | window->type = type; |
Ander Conselvan de Oliveira | 5403f52 | 2012-12-14 13:37:23 -0200 | [diff] [blame] | 4265 | window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT; |
MoD | 063a882 | 2013-03-02 23:43:57 +0000 | [diff] [blame] | 4266 | window->configure_requests = 0; |
Tomeu Vizoso | bee45a1 | 2013-08-06 20:05:54 +0200 | [diff] [blame] | 4267 | window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE; |
Kristian Høgsberg | 87a57bb | 2012-01-09 10:34:35 -0500 | [diff] [blame] | 4268 | |
Kristian Høgsberg | 4e51b44 | 2013-01-07 15:47:14 -0500 | [diff] [blame] | 4269 | if (display->argb_device) |
Benjamin Franzke | 22d5481 | 2011-07-16 19:50:32 +0000 | [diff] [blame] | 4270 | #ifdef HAVE_CAIRO_EGL |
Pekka Paalanen | 02bba7c | 2013-02-13 16:17:15 +0200 | [diff] [blame] | 4271 | surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW; |
Benjamin Franzke | 22d5481 | 2011-07-16 19:50:32 +0000 | [diff] [blame] | 4272 | #else |
Pekka Paalanen | 02bba7c | 2013-02-13 16:17:15 +0200 | [diff] [blame] | 4273 | surface->buffer_type = WINDOW_BUFFER_TYPE_SHM; |
Benjamin Franzke | 22d5481 | 2011-07-16 19:50:32 +0000 | [diff] [blame] | 4274 | #endif |
Yuval Fledel | 45568f6 | 2010-12-06 09:18:12 -0500 | [diff] [blame] | 4275 | else |
Pekka Paalanen | 02bba7c | 2013-02-13 16:17:15 +0200 | [diff] [blame] | 4276 | surface->buffer_type = WINDOW_BUFFER_TYPE_SHM; |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 4277 | |
Pekka Paalanen | 02bba7c | 2013-02-13 16:17:15 +0200 | [diff] [blame] | 4278 | wl_surface_set_user_data(surface->surface, window); |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 4279 | wl_list_insert(display->window_list.prev, &window->link); |
Kristian Høgsberg | 84b76c7 | 2012-04-13 12:01:18 -0400 | [diff] [blame] | 4280 | wl_list_init(&window->redraw_task.link); |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 4281 | |
Pekka Paalanen | 6b2dc91 | 2011-11-29 10:25:08 +0200 | [diff] [blame] | 4282 | if (window->shell_surface) { |
| 4283 | wl_shell_surface_set_user_data(window->shell_surface, window); |
| 4284 | wl_shell_surface_add_listener(window->shell_surface, |
| 4285 | &shell_surface_listener, window); |
| 4286 | } |
Pekka Paalanen | 9d1613e | 2011-11-25 12:09:16 +0200 | [diff] [blame] | 4287 | |
Rob Bradford | 7507b57 | 2012-05-15 17:55:34 +0100 | [diff] [blame] | 4288 | wl_list_init (&window->window_output_list); |
| 4289 | |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 4290 | return window; |
| 4291 | } |
| 4292 | |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 4293 | struct window * |
Kristian Høgsberg | 009ac0a | 2012-01-31 15:24:48 -0500 | [diff] [blame] | 4294 | window_create(struct display *display) |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 4295 | { |
Kristian Høgsberg | 4162483 | 2013-07-25 16:53:10 -0700 | [diff] [blame] | 4296 | return window_create_internal(display, NULL, TYPE_NONE); |
Kristian Høgsberg | 962342c | 2012-06-26 16:29:50 -0400 | [diff] [blame] | 4297 | } |
| 4298 | |
| 4299 | struct window * |
| 4300 | window_create_custom(struct display *display) |
| 4301 | { |
Kristian Høgsberg | ce27841 | 2013-07-25 15:20:20 -0700 | [diff] [blame] | 4302 | return window_create_internal(display, NULL, TYPE_CUSTOM); |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 4303 | } |
| 4304 | |
| 4305 | struct window * |
| 4306 | window_create_transient(struct display *display, struct window *parent, |
Tiago Vignatti | dec7658 | 2012-05-21 16:47:46 +0300 | [diff] [blame] | 4307 | int32_t x, int32_t y, uint32_t flags) |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 4308 | { |
| 4309 | struct window *window; |
| 4310 | |
Kristian Høgsberg | 962342c | 2012-06-26 16:29:50 -0400 | [diff] [blame] | 4311 | window = window_create_internal(parent->display, |
| 4312 | parent, TYPE_TRANSIENT); |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 4313 | |
| 4314 | window->x = x; |
| 4315 | window->y = y; |
| 4316 | |
Kristian Høgsberg | 1517def | 2012-02-16 22:56:12 -0500 | [diff] [blame] | 4317 | if (display->shell) |
Pekka Paalanen | 4e37374 | 2013-02-13 16:17:13 +0200 | [diff] [blame] | 4318 | wl_shell_surface_set_transient( |
| 4319 | window->shell_surface, |
| 4320 | window->parent->main_surface->surface, |
| 4321 | window->x, window->y, flags); |
Kristian Høgsberg | 1517def | 2012-02-16 22:56:12 -0500 | [diff] [blame] | 4322 | |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 4323 | return window; |
| 4324 | } |
| 4325 | |
Kristian Høgsberg | 831dd52 | 2012-01-10 23:46:33 -0500 | [diff] [blame] | 4326 | static void |
Kristian Høgsberg | 19dd1d7 | 2012-01-09 10:42:41 -0500 | [diff] [blame] | 4327 | menu_set_item(struct menu *menu, int sy) |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4328 | { |
Kristian Høgsberg | c680e90 | 2013-10-23 21:49:30 -0700 | [diff] [blame] | 4329 | int32_t x, y, width, height; |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4330 | int next; |
| 4331 | |
Kristian Høgsberg | c680e90 | 2013-10-23 21:49:30 -0700 | [diff] [blame] | 4332 | frame_interior(menu->frame, &x, &y, &width, &height); |
| 4333 | next = (sy - y) / 20; |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4334 | if (menu->current != next) { |
| 4335 | menu->current = next; |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 4336 | widget_schedule_redraw(menu->widget); |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4337 | } |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4338 | } |
| 4339 | |
| 4340 | static int |
Kristian Høgsberg | 5f190ef | 2012-01-09 09:44:45 -0500 | [diff] [blame] | 4341 | menu_motion_handler(struct widget *widget, |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4342 | struct input *input, uint32_t time, |
Kristian Høgsberg | 80680c7 | 2012-05-10 12:21:37 -0400 | [diff] [blame] | 4343 | float x, float y, void *data) |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4344 | { |
Kristian Høgsberg | 831dd52 | 2012-01-10 23:46:33 -0500 | [diff] [blame] | 4345 | struct menu *menu = data; |
| 4346 | |
| 4347 | if (widget == menu->widget) |
| 4348 | menu_set_item(data, y); |
| 4349 | |
Ander Conselvan de Oliveira | dc8c8fc | 2012-05-25 16:01:41 +0300 | [diff] [blame] | 4350 | return CURSOR_LEFT_PTR; |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4351 | } |
| 4352 | |
Kristian Høgsberg | bb901fa | 2012-01-09 11:22:32 -0500 | [diff] [blame] | 4353 | static int |
Kristian Høgsberg | 391649b | 2012-01-09 09:22:30 -0500 | [diff] [blame] | 4354 | menu_enter_handler(struct widget *widget, |
Kristian Høgsberg | 80680c7 | 2012-05-10 12:21:37 -0400 | [diff] [blame] | 4355 | struct input *input, float x, float y, void *data) |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4356 | { |
Kristian Høgsberg | 831dd52 | 2012-01-10 23:46:33 -0500 | [diff] [blame] | 4357 | struct menu *menu = data; |
| 4358 | |
| 4359 | if (widget == menu->widget) |
| 4360 | menu_set_item(data, y); |
| 4361 | |
Ander Conselvan de Oliveira | dc8c8fc | 2012-05-25 16:01:41 +0300 | [diff] [blame] | 4362 | return CURSOR_LEFT_PTR; |
Kristian Høgsberg | 391649b | 2012-01-09 09:22:30 -0500 | [diff] [blame] | 4363 | } |
| 4364 | |
| 4365 | static void |
| 4366 | menu_leave_handler(struct widget *widget, struct input *input, void *data) |
| 4367 | { |
Kristian Høgsberg | 831dd52 | 2012-01-10 23:46:33 -0500 | [diff] [blame] | 4368 | struct menu *menu = data; |
| 4369 | |
| 4370 | if (widget == menu->widget) |
| 4371 | menu_set_item(data, -200); |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4372 | } |
| 4373 | |
| 4374 | static void |
Kristian Høgsberg | a8a0db3 | 2012-01-09 11:12:05 -0500 | [diff] [blame] | 4375 | menu_button_handler(struct widget *widget, |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4376 | struct input *input, uint32_t time, |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 4377 | uint32_t button, enum wl_pointer_button_state state, |
| 4378 | void *data) |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4379 | |
| 4380 | { |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 4381 | struct menu *menu = data; |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4382 | |
Kristian Høgsberg | d2fbb38 | 2012-10-30 13:45:22 -0400 | [diff] [blame] | 4383 | if (state == WL_POINTER_BUTTON_STATE_RELEASED && |
| 4384 | (menu->release_count > 0 || time - menu->time > 500)) { |
Kristian Høgsberg | 831dd52 | 2012-01-10 23:46:33 -0500 | [diff] [blame] | 4385 | /* Either relase after press-drag-release or |
| 4386 | * click-motion-click. */ |
Kristian Høgsberg | 67b8215 | 2013-10-23 16:52:05 -0700 | [diff] [blame] | 4387 | menu->func(menu->window->parent, input, |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 4388 | menu->current, menu->window->parent->user_data); |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 4389 | input_ungrab(input); |
Pekka Paalanen | 6d174cf | 2012-01-19 15:17:59 +0200 | [diff] [blame] | 4390 | menu_destroy(menu); |
Kristian Høgsberg | e77d757 | 2012-10-30 18:10:30 -0400 | [diff] [blame] | 4391 | } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) { |
Kristian Høgsberg | d2fbb38 | 2012-10-30 13:45:22 -0400 | [diff] [blame] | 4392 | menu->release_count++; |
Kristian Høgsberg | e77d757 | 2012-10-30 18:10:30 -0400 | [diff] [blame] | 4393 | } |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4394 | } |
| 4395 | |
| 4396 | static void |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 4397 | menu_redraw_handler(struct widget *widget, void *data) |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4398 | { |
| 4399 | cairo_t *cr; |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4400 | struct menu *menu = data; |
Kristian Høgsberg | c680e90 | 2013-10-23 21:49:30 -0700 | [diff] [blame] | 4401 | int32_t x, y, width, height, i; |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4402 | |
Pekka Paalanen | 0c4445b | 2013-02-13 16:17:23 +0200 | [diff] [blame] | 4403 | cr = widget_cairo_create(widget); |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4404 | |
Kristian Høgsberg | c680e90 | 2013-10-23 21:49:30 -0700 | [diff] [blame] | 4405 | frame_repaint(menu->frame, cr); |
| 4406 | frame_interior(menu->frame, &x, &y, &width, &height); |
Kristian Høgsberg | 824c6d0 | 2012-01-19 13:54:09 -0500 | [diff] [blame] | 4407 | |
Kristian Høgsberg | c680e90 | 2013-10-23 21:49:30 -0700 | [diff] [blame] | 4408 | theme_set_background_source(menu->window->display->theme, |
| 4409 | cr, THEME_FRAME_ACTIVE); |
| 4410 | cairo_rectangle(cr, x, y, width, height); |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4411 | cairo_fill(cr); |
| 4412 | |
Kristian Høgsberg | c680e90 | 2013-10-23 21:49:30 -0700 | [diff] [blame] | 4413 | cairo_select_font_face(cr, "sans", |
| 4414 | CAIRO_FONT_SLANT_NORMAL, |
| 4415 | CAIRO_FONT_WEIGHT_NORMAL); |
| 4416 | cairo_set_font_size(cr, 12); |
| 4417 | |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4418 | for (i = 0; i < menu->count; i++) { |
| 4419 | if (i == menu->current) { |
| 4420 | cairo_set_source_rgb(cr, 1.0, 1.0, 1.0); |
Kristian Høgsberg | c680e90 | 2013-10-23 21:49:30 -0700 | [diff] [blame] | 4421 | cairo_rectangle(cr, x, y + i * 20, width, 20); |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4422 | cairo_fill(cr); |
| 4423 | cairo_set_source_rgb(cr, 0.0, 0.0, 0.0); |
Kristian Høgsberg | c680e90 | 2013-10-23 21:49:30 -0700 | [diff] [blame] | 4424 | cairo_move_to(cr, x + 10, y + i * 20 + 16); |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4425 | cairo_show_text(cr, menu->entries[i]); |
| 4426 | } else { |
Kristian Høgsberg | c680e90 | 2013-10-23 21:49:30 -0700 | [diff] [blame] | 4427 | cairo_set_source_rgb(cr, 0.0, 0.0, 0.0); |
| 4428 | cairo_move_to(cr, x + 10, y + i * 20 + 16); |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4429 | cairo_show_text(cr, menu->entries[i]); |
| 4430 | } |
| 4431 | } |
| 4432 | |
| 4433 | cairo_destroy(cr); |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4434 | } |
| 4435 | |
Pekka Paalanen | 6d174cf | 2012-01-19 15:17:59 +0200 | [diff] [blame] | 4436 | void |
| 4437 | window_show_menu(struct display *display, |
| 4438 | struct input *input, uint32_t time, struct window *parent, |
| 4439 | int32_t x, int32_t y, |
| 4440 | menu_func_t func, const char **entries, int count) |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4441 | { |
| 4442 | struct window *window; |
| 4443 | struct menu *menu; |
Kristian Høgsberg | c680e90 | 2013-10-23 21:49:30 -0700 | [diff] [blame] | 4444 | int32_t ix, iy; |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4445 | |
| 4446 | menu = malloc(sizeof *menu); |
| 4447 | if (!menu) |
Pekka Paalanen | 6d174cf | 2012-01-19 15:17:59 +0200 | [diff] [blame] | 4448 | return; |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4449 | |
Kristian Høgsberg | 962342c | 2012-06-26 16:29:50 -0400 | [diff] [blame] | 4450 | window = window_create_internal(parent->display, parent, TYPE_MENU); |
Martin Olsson | 444799a | 2012-07-08 03:03:40 +0200 | [diff] [blame] | 4451 | if (!window) { |
| 4452 | free(menu); |
Pekka Paalanen | 6d174cf | 2012-01-19 15:17:59 +0200 | [diff] [blame] | 4453 | return; |
Martin Olsson | 444799a | 2012-07-08 03:03:40 +0200 | [diff] [blame] | 4454 | } |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4455 | |
| 4456 | menu->window = window; |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 4457 | menu->widget = window_add_widget(menu->window, menu); |
Alexander Larsson | d68f523 | 2013-05-22 14:41:33 +0200 | [diff] [blame] | 4458 | window_set_buffer_scale (menu->window, window_get_buffer_scale (parent)); |
| 4459 | window_set_buffer_transform (menu->window, window_get_buffer_transform (parent)); |
Kristian Høgsberg | c680e90 | 2013-10-23 21:49:30 -0700 | [diff] [blame] | 4460 | menu->frame = frame_create(window->display->theme, 0, 0, |
Kristian Høgsberg | 89f4bc4 | 2013-10-23 22:12:13 -0700 | [diff] [blame] | 4461 | FRAME_BUTTON_NONE, NULL); |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4462 | menu->entries = entries; |
| 4463 | menu->count = count; |
Kristian Høgsberg | d2fbb38 | 2012-10-30 13:45:22 -0400 | [diff] [blame] | 4464 | menu->release_count = 0; |
Kristian Høgsberg | 831dd52 | 2012-01-10 23:46:33 -0500 | [diff] [blame] | 4465 | menu->current = -1; |
Kristian Høgsberg | b3cca0a | 2012-01-04 22:19:14 -0500 | [diff] [blame] | 4466 | menu->time = time; |
Kristian Høgsberg | 4f7dcd6 | 2012-01-06 21:59:05 -0500 | [diff] [blame] | 4467 | menu->func = func; |
Kristian Høgsberg | 831dd52 | 2012-01-10 23:46:33 -0500 | [diff] [blame] | 4468 | menu->input = input; |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4469 | window->type = TYPE_MENU; |
| 4470 | window->x = x; |
| 4471 | window->y = y; |
| 4472 | |
Kristian Høgsberg | 8ae6385 | 2013-10-28 22:06:11 -0700 | [diff] [blame] | 4473 | input_ungrab(input); |
| 4474 | |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 4475 | widget_set_redraw_handler(menu->widget, menu_redraw_handler); |
Kristian Høgsberg | 75bc667 | 2012-01-10 09:43:58 -0500 | [diff] [blame] | 4476 | widget_set_enter_handler(menu->widget, menu_enter_handler); |
| 4477 | widget_set_leave_handler(menu->widget, menu_leave_handler); |
| 4478 | widget_set_motion_handler(menu->widget, menu_motion_handler); |
| 4479 | widget_set_button_handler(menu->widget, menu_button_handler); |
Kristian Høgsberg | 391649b | 2012-01-09 09:22:30 -0500 | [diff] [blame] | 4480 | |
Kristian Høgsberg | 831dd52 | 2012-01-10 23:46:33 -0500 | [diff] [blame] | 4481 | input_grab(input, menu->widget, 0); |
Kristian Høgsberg | c680e90 | 2013-10-23 21:49:30 -0700 | [diff] [blame] | 4482 | frame_resize_inside(menu->frame, 200, count * 20); |
| 4483 | frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE); |
| 4484 | window_schedule_resize(window, frame_width(menu->frame), |
| 4485 | frame_height(menu->frame)); |
| 4486 | |
Kristian Høgsberg | c680e90 | 2013-10-23 21:49:30 -0700 | [diff] [blame] | 4487 | frame_interior(menu->frame, &ix, &iy, NULL, NULL); |
| 4488 | wl_shell_surface_set_popup(window->shell_surface, input->seat, |
| 4489 | display_get_serial(window->display), |
| 4490 | window->parent->main_surface->surface, |
| 4491 | window->x - ix, window->y - iy, 0); |
Kristian Høgsberg | bbedd7e | 2011-12-19 15:40:10 -0500 | [diff] [blame] | 4492 | } |
| 4493 | |
Kristian Høgsberg | 248c1b6 | 2011-01-21 18:03:15 -0500 | [diff] [blame] | 4494 | void |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 4495 | window_set_buffer_type(struct window *window, enum window_buffer_type type) |
| 4496 | { |
Pekka Paalanen | 02bba7c | 2013-02-13 16:17:15 +0200 | [diff] [blame] | 4497 | window->main_surface->buffer_type = type; |
Kristian Høgsberg | d0c3b9d | 2010-10-25 11:40:03 -0400 | [diff] [blame] | 4498 | } |
| 4499 | |
Tomeu Vizoso | bee45a1 | 2013-08-06 20:05:54 +0200 | [diff] [blame] | 4500 | void |
| 4501 | window_set_preferred_format(struct window *window, |
| 4502 | enum preferred_format format) |
| 4503 | { |
| 4504 | window->preferred_format = format; |
| 4505 | } |
| 4506 | |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 4507 | struct widget * |
| 4508 | window_add_subsurface(struct window *window, void *data, |
| 4509 | enum subsurface_mode default_mode) |
| 4510 | { |
| 4511 | struct widget *widget; |
| 4512 | struct surface *surface; |
| 4513 | struct wl_surface *parent; |
| 4514 | struct wl_subcompositor *subcompo = window->display->subcompositor; |
| 4515 | |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 4516 | surface = surface_create(window); |
| 4517 | widget = widget_create(window, surface, data); |
| 4518 | wl_list_init(&widget->link); |
| 4519 | surface->widget = widget; |
| 4520 | |
| 4521 | parent = window->main_surface->surface; |
| 4522 | surface->subsurface = wl_subcompositor_get_subsurface(subcompo, |
| 4523 | surface->surface, |
| 4524 | parent); |
| 4525 | surface->synchronized = 1; |
| 4526 | |
| 4527 | switch (default_mode) { |
| 4528 | case SUBSURFACE_SYNCHRONIZED: |
| 4529 | surface->synchronized_default = 1; |
| 4530 | break; |
| 4531 | case SUBSURFACE_DESYNCHRONIZED: |
| 4532 | surface->synchronized_default = 0; |
| 4533 | break; |
| 4534 | default: |
| 4535 | assert(!"bad enum subsurface_mode"); |
| 4536 | } |
| 4537 | |
| 4538 | return widget; |
| 4539 | } |
Kristian Høgsberg | 8357cd6 | 2011-05-13 13:24:56 -0400 | [diff] [blame] | 4540 | |
| 4541 | static void |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 4542 | display_handle_geometry(void *data, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 4543 | struct wl_output *wl_output, |
| 4544 | int x, int y, |
| 4545 | int physical_width, |
| 4546 | int physical_height, |
| 4547 | int subpixel, |
| 4548 | const char *make, |
Kristian Høgsberg | 0e69647 | 2012-07-22 15:49:57 -0400 | [diff] [blame] | 4549 | const char *model, |
| 4550 | int transform) |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 4551 | { |
Kristian Høgsberg | 53ff2f6 | 2011-11-26 17:27:37 -0500 | [diff] [blame] | 4552 | struct output *output = data; |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 4553 | |
Kristian Høgsberg | 53ff2f6 | 2011-11-26 17:27:37 -0500 | [diff] [blame] | 4554 | output->allocation.x = x; |
| 4555 | output->allocation.y = y; |
Scott Moreau | 4e07236 | 2012-09-29 02:03:11 -0600 | [diff] [blame] | 4556 | output->transform = transform; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 4557 | } |
| 4558 | |
| 4559 | static void |
Alexander Larsson | afd319a | 2013-05-22 14:41:27 +0200 | [diff] [blame] | 4560 | display_handle_done(void *data, |
| 4561 | struct wl_output *wl_output) |
| 4562 | { |
| 4563 | } |
| 4564 | |
| 4565 | static void |
| 4566 | display_handle_scale(void *data, |
| 4567 | struct wl_output *wl_output, |
Alexander Larsson | edddbd1 | 2013-05-24 13:09:43 +0200 | [diff] [blame] | 4568 | int32_t scale) |
Alexander Larsson | afd319a | 2013-05-22 14:41:27 +0200 | [diff] [blame] | 4569 | { |
| 4570 | struct output *output = data; |
| 4571 | |
| 4572 | output->scale = scale; |
| 4573 | } |
| 4574 | |
| 4575 | static void |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 4576 | display_handle_mode(void *data, |
| 4577 | struct wl_output *wl_output, |
| 4578 | uint32_t flags, |
| 4579 | int width, |
| 4580 | int height, |
| 4581 | int refresh) |
| 4582 | { |
Kristian Høgsberg | 53ff2f6 | 2011-11-26 17:27:37 -0500 | [diff] [blame] | 4583 | struct output *output = data; |
Pekka Paalanen | 999c5b5 | 2011-11-30 10:52:38 +0200 | [diff] [blame] | 4584 | struct display *display = output->display; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 4585 | |
Kristian Høgsberg | 53ff2f6 | 2011-11-26 17:27:37 -0500 | [diff] [blame] | 4586 | if (flags & WL_OUTPUT_MODE_CURRENT) { |
| 4587 | output->allocation.width = width; |
| 4588 | output->allocation.height = height; |
Pekka Paalanen | 999c5b5 | 2011-11-30 10:52:38 +0200 | [diff] [blame] | 4589 | if (display->output_configure_handler) |
| 4590 | (*display->output_configure_handler)( |
| 4591 | output, display->user_data); |
Kristian Høgsberg | 53ff2f6 | 2011-11-26 17:27:37 -0500 | [diff] [blame] | 4592 | } |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 4593 | } |
| 4594 | |
| 4595 | static const struct wl_output_listener output_listener = { |
| 4596 | display_handle_geometry, |
Alexander Larsson | afd319a | 2013-05-22 14:41:27 +0200 | [diff] [blame] | 4597 | display_handle_mode, |
| 4598 | display_handle_done, |
| 4599 | display_handle_scale |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 4600 | }; |
| 4601 | |
| 4602 | static void |
Kristian Høgsberg | 53ff2f6 | 2011-11-26 17:27:37 -0500 | [diff] [blame] | 4603 | display_add_output(struct display *d, uint32_t id) |
| 4604 | { |
| 4605 | struct output *output; |
| 4606 | |
Kristian Høgsberg | 69594cc | 2013-08-15 14:28:25 -0700 | [diff] [blame] | 4607 | output = xzalloc(sizeof *output); |
Kristian Høgsberg | 53ff2f6 | 2011-11-26 17:27:37 -0500 | [diff] [blame] | 4608 | output->display = d; |
Alexander Larsson | afd319a | 2013-05-22 14:41:27 +0200 | [diff] [blame] | 4609 | output->scale = 1; |
Kristian Høgsberg | 53ff2f6 | 2011-11-26 17:27:37 -0500 | [diff] [blame] | 4610 | output->output = |
Alexander Larsson | afd319a | 2013-05-22 14:41:27 +0200 | [diff] [blame] | 4611 | wl_registry_bind(d->registry, id, &wl_output_interface, 2); |
Xiong Zhang | 83d8ee7 | 2013-10-23 13:58:35 +0800 | [diff] [blame] | 4612 | output->server_output_id = id; |
Kristian Høgsberg | 53ff2f6 | 2011-11-26 17:27:37 -0500 | [diff] [blame] | 4613 | wl_list_insert(d->output_list.prev, &output->link); |
| 4614 | |
| 4615 | wl_output_add_listener(output->output, &output_listener, output); |
| 4616 | } |
| 4617 | |
Pekka Paalanen | 2c1426a | 2011-12-16 11:35:34 +0200 | [diff] [blame] | 4618 | static void |
| 4619 | output_destroy(struct output *output) |
| 4620 | { |
| 4621 | if (output->destroy_handler) |
| 4622 | (*output->destroy_handler)(output, output->user_data); |
| 4623 | |
| 4624 | wl_output_destroy(output->output); |
| 4625 | wl_list_remove(&output->link); |
| 4626 | free(output); |
| 4627 | } |
| 4628 | |
Xiong Zhang | 83d8ee7 | 2013-10-23 13:58:35 +0800 | [diff] [blame] | 4629 | static void |
| 4630 | display_destroy_output(struct display *d, uint32_t id) |
| 4631 | { |
| 4632 | struct output *output; |
| 4633 | |
| 4634 | wl_list_for_each(output, &d->output_list, link) { |
| 4635 | if (output->server_output_id == id) { |
| 4636 | output_destroy(output); |
| 4637 | break; |
| 4638 | } |
| 4639 | } |
| 4640 | } |
| 4641 | |
Kristian Høgsberg | 53ff2f6 | 2011-11-26 17:27:37 -0500 | [diff] [blame] | 4642 | void |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 4643 | display_set_global_handler(struct display *display, |
| 4644 | display_global_handler_t handler) |
| 4645 | { |
| 4646 | struct global *global; |
| 4647 | |
| 4648 | display->global_handler = handler; |
| 4649 | if (!handler) |
| 4650 | return; |
| 4651 | |
| 4652 | wl_list_for_each(global, &display->global_list, link) |
| 4653 | display->global_handler(display, |
| 4654 | global->name, global->interface, |
| 4655 | global->version, display->user_data); |
| 4656 | } |
| 4657 | |
| 4658 | void |
Xiong Zhang | 83d8ee7 | 2013-10-23 13:58:35 +0800 | [diff] [blame] | 4659 | display_set_global_handler_remove(struct display *display, |
| 4660 | display_global_handler_t remove_handler) |
| 4661 | { |
| 4662 | display->global_handler_remove = remove_handler; |
| 4663 | if (!remove_handler) |
| 4664 | return; |
| 4665 | } |
| 4666 | |
| 4667 | void |
Pekka Paalanen | 999c5b5 | 2011-11-30 10:52:38 +0200 | [diff] [blame] | 4668 | display_set_output_configure_handler(struct display *display, |
| 4669 | display_output_handler_t handler) |
| 4670 | { |
| 4671 | struct output *output; |
| 4672 | |
| 4673 | display->output_configure_handler = handler; |
| 4674 | if (!handler) |
| 4675 | return; |
| 4676 | |
Pekka Paalanen | b2f957a | 2012-10-15 12:06:53 +0300 | [diff] [blame] | 4677 | wl_list_for_each(output, &display->output_list, link) { |
| 4678 | if (output->allocation.width == 0 && |
| 4679 | output->allocation.height == 0) |
| 4680 | continue; |
| 4681 | |
Pekka Paalanen | 999c5b5 | 2011-11-30 10:52:38 +0200 | [diff] [blame] | 4682 | (*display->output_configure_handler)(output, |
| 4683 | display->user_data); |
Pekka Paalanen | b2f957a | 2012-10-15 12:06:53 +0300 | [diff] [blame] | 4684 | } |
Pekka Paalanen | 999c5b5 | 2011-11-30 10:52:38 +0200 | [diff] [blame] | 4685 | } |
| 4686 | |
| 4687 | void |
| 4688 | output_set_user_data(struct output *output, void *data) |
| 4689 | { |
| 4690 | output->user_data = data; |
| 4691 | } |
| 4692 | |
| 4693 | void * |
| 4694 | output_get_user_data(struct output *output) |
| 4695 | { |
| 4696 | return output->user_data; |
| 4697 | } |
| 4698 | |
| 4699 | void |
| 4700 | output_set_destroy_handler(struct output *output, |
| 4701 | display_output_handler_t handler) |
| 4702 | { |
| 4703 | output->destroy_handler = handler; |
| 4704 | /* FIXME: implement this, once we have way to remove outputs */ |
| 4705 | } |
| 4706 | |
| 4707 | void |
Scott Moreau | 4e07236 | 2012-09-29 02:03:11 -0600 | [diff] [blame] | 4708 | output_get_allocation(struct output *output, struct rectangle *base) |
Kristian Høgsberg | 53ff2f6 | 2011-11-26 17:27:37 -0500 | [diff] [blame] | 4709 | { |
Scott Moreau | 4e07236 | 2012-09-29 02:03:11 -0600 | [diff] [blame] | 4710 | struct rectangle allocation = output->allocation; |
| 4711 | |
| 4712 | switch (output->transform) { |
| 4713 | case WL_OUTPUT_TRANSFORM_90: |
| 4714 | case WL_OUTPUT_TRANSFORM_270: |
| 4715 | case WL_OUTPUT_TRANSFORM_FLIPPED_90: |
| 4716 | case WL_OUTPUT_TRANSFORM_FLIPPED_270: |
| 4717 | /* Swap width and height */ |
| 4718 | allocation.width = output->allocation.height; |
| 4719 | allocation.height = output->allocation.width; |
| 4720 | break; |
| 4721 | } |
| 4722 | |
| 4723 | *base = allocation; |
Kristian Høgsberg | 53ff2f6 | 2011-11-26 17:27:37 -0500 | [diff] [blame] | 4724 | } |
| 4725 | |
Pekka Paalanen | 999c5b5 | 2011-11-30 10:52:38 +0200 | [diff] [blame] | 4726 | struct wl_output * |
| 4727 | output_get_wl_output(struct output *output) |
| 4728 | { |
| 4729 | return output->output; |
| 4730 | } |
| 4731 | |
Ander Conselvan de Oliveira | 15256f6 | 2012-11-30 17:34:25 +0200 | [diff] [blame] | 4732 | enum wl_output_transform |
| 4733 | output_get_transform(struct output *output) |
| 4734 | { |
| 4735 | return output->transform; |
| 4736 | } |
| 4737 | |
Alexander Larsson | afd319a | 2013-05-22 14:41:27 +0200 | [diff] [blame] | 4738 | uint32_t |
| 4739 | output_get_scale(struct output *output) |
| 4740 | { |
| 4741 | return output->scale; |
| 4742 | } |
| 4743 | |
Kristian Høgsberg | 53ff2f6 | 2011-11-26 17:27:37 -0500 | [diff] [blame] | 4744 | static void |
Daniel Stone | 97f6854 | 2012-05-30 16:32:01 +0100 | [diff] [blame] | 4745 | fini_xkb(struct input *input) |
| 4746 | { |
| 4747 | xkb_state_unref(input->xkb.state); |
| 4748 | xkb_map_unref(input->xkb.keymap); |
| 4749 | } |
| 4750 | |
Rob Bradford | 0803118 | 2013-08-13 20:11:03 +0100 | [diff] [blame] | 4751 | #define MAX(a,b) ((a) > (b) ? a : b) |
| 4752 | |
Daniel Stone | 97f6854 | 2012-05-30 16:32:01 +0100 | [diff] [blame] | 4753 | static void |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame] | 4754 | display_add_input(struct display *d, uint32_t id) |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 4755 | { |
| 4756 | struct input *input; |
| 4757 | |
Kristian Høgsberg | adcd54b | 2013-08-15 14:17:13 -0700 | [diff] [blame] | 4758 | input = xzalloc(sizeof *input); |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 4759 | input->display = d; |
Rob Bradford | 0803118 | 2013-08-13 20:11:03 +0100 | [diff] [blame] | 4760 | input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, |
| 4761 | MAX(d->seat_version, 3)); |
Rusty Lynch | 1084da5 | 2013-08-15 09:10:08 -0700 | [diff] [blame] | 4762 | input->touch_focus = NULL; |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 4763 | input->pointer_focus = NULL; |
| 4764 | input->keyboard_focus = NULL; |
Rusty Lynch | 041815a | 2013-08-08 21:20:38 -0700 | [diff] [blame] | 4765 | wl_list_init(&input->touch_point_list); |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 4766 | wl_list_insert(d->input_list.prev, &input->link); |
| 4767 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 4768 | wl_seat_add_listener(input->seat, &seat_listener, input); |
| 4769 | wl_seat_set_user_data(input->seat, input); |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 4770 | |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 4771 | input->data_device = |
| 4772 | wl_data_device_manager_get_data_device(d->data_device_manager, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 4773 | input->seat); |
| 4774 | wl_data_device_add_listener(input->data_device, &data_device_listener, |
| 4775 | input); |
Ander Conselvan de Oliveira | 37ffc3c | 2012-06-15 17:27:35 +0300 | [diff] [blame] | 4776 | |
| 4777 | input->pointer_surface = wl_compositor_create_surface(d->compositor); |
Kristian Høgsberg | cf4d244 | 2012-06-20 14:52:12 -0400 | [diff] [blame] | 4778 | |
Kristian Høgsberg | 8b19c64 | 2012-06-20 18:00:13 -0400 | [diff] [blame] | 4779 | input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC, |
| 4780 | TFD_CLOEXEC | TFD_NONBLOCK); |
Kristian Høgsberg | cf4d244 | 2012-06-20 14:52:12 -0400 | [diff] [blame] | 4781 | input->repeat_task.run = keyboard_repeat_func; |
| 4782 | display_watch_fd(d, input->repeat_timer_fd, |
| 4783 | EPOLLIN, &input->repeat_task); |
Kristian Høgsberg | 58eec36 | 2011-01-19 14:27:42 -0500 | [diff] [blame] | 4784 | } |
| 4785 | |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 4786 | static void |
Pekka Paalanen | e1207c7 | 2011-12-16 12:02:09 +0200 | [diff] [blame] | 4787 | input_destroy(struct input *input) |
| 4788 | { |
Kristian Høgsberg | 8a1d10d | 2011-12-21 17:11:45 -0500 | [diff] [blame] | 4789 | input_remove_keyboard_focus(input); |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 4790 | input_remove_pointer_focus(input); |
Pekka Paalanen | e1207c7 | 2011-12-16 12:02:09 +0200 | [diff] [blame] | 4791 | |
| 4792 | if (input->drag_offer) |
| 4793 | data_offer_destroy(input->drag_offer); |
| 4794 | |
| 4795 | if (input->selection_offer) |
| 4796 | data_offer_destroy(input->selection_offer); |
| 4797 | |
| 4798 | wl_data_device_destroy(input->data_device); |
Rob Bradford | 0803118 | 2013-08-13 20:11:03 +0100 | [diff] [blame] | 4799 | |
| 4800 | if (input->display->seat_version >= 3) { |
| 4801 | if (input->pointer) |
| 4802 | wl_pointer_release(input->pointer); |
| 4803 | if (input->keyboard) |
| 4804 | wl_keyboard_release(input->keyboard); |
| 4805 | } |
| 4806 | |
Daniel Stone | 97f6854 | 2012-05-30 16:32:01 +0100 | [diff] [blame] | 4807 | fini_xkb(input); |
| 4808 | |
Ander Conselvan de Oliveira | 37ffc3c | 2012-06-15 17:27:35 +0300 | [diff] [blame] | 4809 | wl_surface_destroy(input->pointer_surface); |
| 4810 | |
Pekka Paalanen | e1207c7 | 2011-12-16 12:02:09 +0200 | [diff] [blame] | 4811 | wl_list_remove(&input->link); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 4812 | wl_seat_destroy(input->seat); |
Kristian Høgsberg | cf4d244 | 2012-06-20 14:52:12 -0400 | [diff] [blame] | 4813 | close(input->repeat_timer_fd); |
Pekka Paalanen | e1207c7 | 2011-12-16 12:02:09 +0200 | [diff] [blame] | 4814 | free(input); |
| 4815 | } |
| 4816 | |
| 4817 | static void |
Jonas Ådahl | 14c92ff | 2012-08-29 22:13:02 +0200 | [diff] [blame] | 4818 | init_workspace_manager(struct display *d, uint32_t id) |
| 4819 | { |
| 4820 | d->workspace_manager = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 4821 | wl_registry_bind(d->registry, id, |
| 4822 | &workspace_manager_interface, 1); |
Jonas Ådahl | 14c92ff | 2012-08-29 22:13:02 +0200 | [diff] [blame] | 4823 | if (d->workspace_manager != NULL) |
| 4824 | workspace_manager_add_listener(d->workspace_manager, |
| 4825 | &workspace_manager_listener, |
| 4826 | d); |
| 4827 | } |
| 4828 | |
| 4829 | static void |
Tomeu Vizoso | bee45a1 | 2013-08-06 20:05:54 +0200 | [diff] [blame] | 4830 | shm_format(void *data, struct wl_shm *wl_shm, uint32_t format) |
| 4831 | { |
| 4832 | struct display *d = data; |
| 4833 | |
| 4834 | if (format == WL_SHM_FORMAT_RGB565) |
| 4835 | d->has_rgb565 = 1; |
| 4836 | } |
| 4837 | |
| 4838 | struct wl_shm_listener shm_listener = { |
| 4839 | shm_format |
| 4840 | }; |
| 4841 | |
| 4842 | static void |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 4843 | registry_handle_global(void *data, struct wl_registry *registry, uint32_t id, |
| 4844 | const char *interface, uint32_t version) |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 4845 | { |
| 4846 | struct display *d = data; |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 4847 | struct global *global; |
| 4848 | |
Brian Lovin | bc91926 | 2013-08-07 15:34:59 -0700 | [diff] [blame] | 4849 | global = xmalloc(sizeof *global); |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 4850 | global->name = id; |
| 4851 | global->interface = strdup(interface); |
| 4852 | global->version = version; |
| 4853 | wl_list_insert(d->global_list.prev, &global->link); |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 4854 | |
Kristian Høgsberg | 7cbdb64 | 2011-04-20 18:53:07 -0400 | [diff] [blame] | 4855 | if (strcmp(interface, "wl_compositor") == 0) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 4856 | d->compositor = wl_registry_bind(registry, id, |
Jason Ekstrand | d27cb09 | 2013-06-26 22:20:31 -0500 | [diff] [blame] | 4857 | &wl_compositor_interface, 3); |
Kristian Høgsberg | 7cbdb64 | 2011-04-20 18:53:07 -0400 | [diff] [blame] | 4858 | } else if (strcmp(interface, "wl_output") == 0) { |
Kristian Høgsberg | 53ff2f6 | 2011-11-26 17:27:37 -0500 | [diff] [blame] | 4859 | display_add_output(d, id); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 4860 | } else if (strcmp(interface, "wl_seat") == 0) { |
Rob Bradford | 0803118 | 2013-08-13 20:11:03 +0100 | [diff] [blame] | 4861 | d->seat_version = version; |
Kristian Høgsberg | 4fe1a3e | 2010-08-10 14:02:48 -0400 | [diff] [blame] | 4862 | display_add_input(d, id); |
Kristian Høgsberg | 7cbdb64 | 2011-04-20 18:53:07 -0400 | [diff] [blame] | 4863 | } else if (strcmp(interface, "wl_shell") == 0) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 4864 | d->shell = wl_registry_bind(registry, |
| 4865 | id, &wl_shell_interface, 1); |
Kristian Høgsberg | 7cbdb64 | 2011-04-20 18:53:07 -0400 | [diff] [blame] | 4866 | } else if (strcmp(interface, "wl_shm") == 0) { |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 4867 | d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1); |
Tomeu Vizoso | bee45a1 | 2013-08-06 20:05:54 +0200 | [diff] [blame] | 4868 | wl_shm_add_listener(d->shm, &shm_listener, d); |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 4869 | } else if (strcmp(interface, "wl_data_device_manager") == 0) { |
| 4870 | d->data_device_manager = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 4871 | wl_registry_bind(registry, id, |
| 4872 | &wl_data_device_manager_interface, 1); |
Scott Moreau | 7a1b32a | 2012-05-27 14:25:02 -0600 | [diff] [blame] | 4873 | } else if (strcmp(interface, "text_cursor_position") == 0) { |
| 4874 | d->text_cursor_position = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 4875 | wl_registry_bind(registry, id, |
| 4876 | &text_cursor_position_interface, 1); |
Jonas Ådahl | 14c92ff | 2012-08-29 22:13:02 +0200 | [diff] [blame] | 4877 | } else if (strcmp(interface, "workspace_manager") == 0) { |
| 4878 | init_workspace_manager(d, id); |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 4879 | } else if (strcmp(interface, "wl_subcompositor") == 0) { |
| 4880 | d->subcompositor = |
| 4881 | wl_registry_bind(registry, id, |
| 4882 | &wl_subcompositor_interface, 1); |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 4883 | } |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 4884 | |
| 4885 | if (d->global_handler) |
| 4886 | d->global_handler(d, id, interface, version, d->user_data); |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 4887 | } |
| 4888 | |
Pekka Paalanen | 0eab05d | 2013-01-22 14:53:55 +0200 | [diff] [blame] | 4889 | static void |
| 4890 | registry_handle_global_remove(void *data, struct wl_registry *registry, |
| 4891 | uint32_t name) |
| 4892 | { |
| 4893 | struct display *d = data; |
| 4894 | struct global *global; |
| 4895 | struct global *tmp; |
| 4896 | |
| 4897 | wl_list_for_each_safe(global, tmp, &d->global_list, link) { |
| 4898 | if (global->name != name) |
| 4899 | continue; |
| 4900 | |
Xiong Zhang | 83d8ee7 | 2013-10-23 13:58:35 +0800 | [diff] [blame] | 4901 | if (strcmp(global->interface, "wl_output") == 0) |
| 4902 | display_destroy_output(d, name); |
| 4903 | |
| 4904 | /* XXX: Should destroy remaining bound globals */ |
| 4905 | |
| 4906 | if (d->global_handler_remove) |
| 4907 | d->global_handler_remove(d, name, global->interface, |
| 4908 | global->version, d->user_data); |
| 4909 | |
Pekka Paalanen | 0eab05d | 2013-01-22 14:53:55 +0200 | [diff] [blame] | 4910 | wl_list_remove(&global->link); |
| 4911 | free(global->interface); |
| 4912 | free(global); |
| 4913 | } |
| 4914 | } |
| 4915 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 4916 | void * |
| 4917 | display_bind(struct display *display, uint32_t name, |
| 4918 | const struct wl_interface *interface, uint32_t version) |
| 4919 | { |
| 4920 | return wl_registry_bind(display->registry, name, interface, version); |
| 4921 | } |
| 4922 | |
| 4923 | static const struct wl_registry_listener registry_listener = { |
Pekka Paalanen | 0eab05d | 2013-01-22 14:53:55 +0200 | [diff] [blame] | 4924 | registry_handle_global, |
| 4925 | registry_handle_global_remove |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 4926 | }; |
| 4927 | |
Kristian Høgsberg | 8f64ed0 | 2012-04-03 11:57:44 -0400 | [diff] [blame] | 4928 | #ifdef HAVE_CAIRO_EGL |
Yuval Fledel | 45568f6 | 2010-12-06 09:18:12 -0500 | [diff] [blame] | 4929 | static int |
Kristian Høgsberg | 297c631 | 2011-02-04 14:11:33 -0500 | [diff] [blame] | 4930 | init_egl(struct display *d) |
Yuval Fledel | 45568f6 | 2010-12-06 09:18:12 -0500 | [diff] [blame] | 4931 | { |
| 4932 | EGLint major, minor; |
Benjamin Franzke | 6693ac2 | 2011-02-10 12:04:30 +0100 | [diff] [blame] | 4933 | EGLint n; |
Kristian Høgsberg | f389cac | 2011-08-31 16:21:38 -0400 | [diff] [blame] | 4934 | |
Rob Clark | 6396ed3 | 2012-03-11 19:48:41 -0500 | [diff] [blame] | 4935 | #ifdef USE_CAIRO_GLESV2 |
| 4936 | # define GL_BIT EGL_OPENGL_ES2_BIT |
| 4937 | #else |
| 4938 | # define GL_BIT EGL_OPENGL_BIT |
| 4939 | #endif |
| 4940 | |
Kristian Høgsberg | 8e81df4 | 2012-01-11 14:24:46 -0500 | [diff] [blame] | 4941 | static const EGLint argb_cfg_attribs[] = { |
Kristian Høgsberg | 3146756 | 2012-10-16 15:31:31 -0400 | [diff] [blame] | 4942 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
Benjamin Franzke | 6693ac2 | 2011-02-10 12:04:30 +0100 | [diff] [blame] | 4943 | EGL_RED_SIZE, 1, |
| 4944 | EGL_GREEN_SIZE, 1, |
| 4945 | EGL_BLUE_SIZE, 1, |
| 4946 | EGL_ALPHA_SIZE, 1, |
| 4947 | EGL_DEPTH_SIZE, 1, |
Rob Clark | 6396ed3 | 2012-03-11 19:48:41 -0500 | [diff] [blame] | 4948 | EGL_RENDERABLE_TYPE, GL_BIT, |
Benjamin Franzke | 6693ac2 | 2011-02-10 12:04:30 +0100 | [diff] [blame] | 4949 | EGL_NONE |
| 4950 | }; |
Yuval Fledel | 45568f6 | 2010-12-06 09:18:12 -0500 | [diff] [blame] | 4951 | |
Kristian Høgsberg | 2d57439 | 2012-01-18 14:50:58 -0500 | [diff] [blame] | 4952 | #ifdef USE_CAIRO_GLESV2 |
| 4953 | static const EGLint context_attribs[] = { |
| 4954 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 4955 | EGL_NONE |
| 4956 | }; |
| 4957 | EGLint api = EGL_OPENGL_ES_API; |
| 4958 | #else |
| 4959 | EGLint *context_attribs = NULL; |
| 4960 | EGLint api = EGL_OPENGL_API; |
| 4961 | #endif |
| 4962 | |
Kristian Høgsberg | 91342c6 | 2011-04-14 14:44:58 -0400 | [diff] [blame] | 4963 | d->dpy = eglGetDisplay(d->display); |
Yuval Fledel | 45568f6 | 2010-12-06 09:18:12 -0500 | [diff] [blame] | 4964 | if (!eglInitialize(d->dpy, &major, &minor)) { |
Pekka Paalanen | 4e10654 | 2013-02-14 11:49:12 +0200 | [diff] [blame] | 4965 | fprintf(stderr, "failed to initialize EGL\n"); |
Yuval Fledel | 45568f6 | 2010-12-06 09:18:12 -0500 | [diff] [blame] | 4966 | return -1; |
| 4967 | } |
| 4968 | |
Kristian Høgsberg | 2d57439 | 2012-01-18 14:50:58 -0500 | [diff] [blame] | 4969 | if (!eglBindAPI(api)) { |
Pekka Paalanen | 4e10654 | 2013-02-14 11:49:12 +0200 | [diff] [blame] | 4970 | fprintf(stderr, "failed to bind EGL client API\n"); |
Yuval Fledel | 45568f6 | 2010-12-06 09:18:12 -0500 | [diff] [blame] | 4971 | return -1; |
| 4972 | } |
| 4973 | |
Kristian Høgsberg | 8e81df4 | 2012-01-11 14:24:46 -0500 | [diff] [blame] | 4974 | if (!eglChooseConfig(d->dpy, argb_cfg_attribs, |
| 4975 | &d->argb_config, 1, &n) || n != 1) { |
Pekka Paalanen | 4e10654 | 2013-02-14 11:49:12 +0200 | [diff] [blame] | 4976 | fprintf(stderr, "failed to choose argb EGL config\n"); |
Benjamin Franzke | 6693ac2 | 2011-02-10 12:04:30 +0100 | [diff] [blame] | 4977 | return -1; |
| 4978 | } |
| 4979 | |
Kristian Høgsberg | 8e81df4 | 2012-01-11 14:24:46 -0500 | [diff] [blame] | 4980 | d->argb_ctx = eglCreateContext(d->dpy, d->argb_config, |
Kristian Høgsberg | 2d57439 | 2012-01-18 14:50:58 -0500 | [diff] [blame] | 4981 | EGL_NO_CONTEXT, context_attribs); |
Benjamin Franzke | 0c99163 | 2011-09-27 21:57:31 +0200 | [diff] [blame] | 4982 | if (d->argb_ctx == NULL) { |
Pekka Paalanen | 4e10654 | 2013-02-14 11:49:12 +0200 | [diff] [blame] | 4983 | fprintf(stderr, "failed to create EGL context\n"); |
Yuval Fledel | 45568f6 | 2010-12-06 09:18:12 -0500 | [diff] [blame] | 4984 | return -1; |
| 4985 | } |
| 4986 | |
Benjamin Franzke | 0c99163 | 2011-09-27 21:57:31 +0200 | [diff] [blame] | 4987 | d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx); |
| 4988 | if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) { |
Pekka Paalanen | 4e10654 | 2013-02-14 11:49:12 +0200 | [diff] [blame] | 4989 | fprintf(stderr, "failed to get cairo EGL argb device\n"); |
Benjamin Franzke | 0c99163 | 2011-09-27 21:57:31 +0200 | [diff] [blame] | 4990 | return -1; |
| 4991 | } |
Yuval Fledel | 45568f6 | 2010-12-06 09:18:12 -0500 | [diff] [blame] | 4992 | |
| 4993 | return 0; |
| 4994 | } |
| 4995 | |
Pekka Paalanen | fe6079a | 2011-12-15 15:20:04 +0200 | [diff] [blame] | 4996 | static void |
| 4997 | fini_egl(struct display *display) |
| 4998 | { |
Pekka Paalanen | fe6079a | 2011-12-15 15:20:04 +0200 | [diff] [blame] | 4999 | cairo_device_destroy(display->argb_device); |
Pekka Paalanen | fe6079a | 2011-12-15 15:20:04 +0200 | [diff] [blame] | 5000 | |
| 5001 | eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, |
| 5002 | EGL_NO_CONTEXT); |
| 5003 | |
| 5004 | eglTerminate(display->dpy); |
| 5005 | eglReleaseThread(); |
| 5006 | } |
Kristian Høgsberg | 8f64ed0 | 2012-04-03 11:57:44 -0400 | [diff] [blame] | 5007 | #endif |
Pekka Paalanen | fe6079a | 2011-12-15 15:20:04 +0200 | [diff] [blame] | 5008 | |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 5009 | static void |
Pekka Paalanen | 3cbb089 | 2012-11-19 17:16:01 +0200 | [diff] [blame] | 5010 | init_dummy_surface(struct display *display) |
| 5011 | { |
| 5012 | int len; |
| 5013 | void *data; |
| 5014 | |
| 5015 | len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1); |
| 5016 | data = malloc(len); |
| 5017 | display->dummy_surface = |
| 5018 | cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, |
| 5019 | 1, 1, len); |
| 5020 | display->dummy_surface_data = data; |
| 5021 | } |
| 5022 | |
| 5023 | static void |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 5024 | handle_display_data(struct task *task, uint32_t events) |
| 5025 | { |
| 5026 | struct display *display = |
| 5027 | container_of(task, struct display, display_task); |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 5028 | struct epoll_event ep; |
| 5029 | int ret; |
U. Artie Eoff | 44874d9 | 2012-10-02 21:12:35 -0700 | [diff] [blame] | 5030 | |
| 5031 | display->display_fd_events = events; |
| 5032 | |
| 5033 | if (events & EPOLLERR || events & EPOLLHUP) { |
| 5034 | display_exit(display); |
| 5035 | return; |
| 5036 | } |
| 5037 | |
Kristian Høgsberg | a17f7a1 | 2012-10-16 13:16:10 -0400 | [diff] [blame] | 5038 | if (events & EPOLLIN) { |
| 5039 | ret = wl_display_dispatch(display->display); |
| 5040 | if (ret == -1) { |
| 5041 | display_exit(display); |
| 5042 | return; |
| 5043 | } |
| 5044 | } |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 5045 | |
| 5046 | if (events & EPOLLOUT) { |
| 5047 | ret = wl_display_flush(display->display); |
| 5048 | if (ret == 0) { |
| 5049 | ep.events = EPOLLIN | EPOLLERR | EPOLLHUP; |
| 5050 | ep.data.ptr = &display->display_task; |
| 5051 | epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD, |
| 5052 | display->display_fd, &ep); |
| 5053 | } else if (ret == -1 && errno != EAGAIN) { |
| 5054 | display_exit(display); |
| 5055 | return; |
| 5056 | } |
| 5057 | } |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 5058 | } |
| 5059 | |
Kristian Høgsberg | 2e43720 | 2013-04-16 11:21:48 -0400 | [diff] [blame] | 5060 | static void |
| 5061 | log_handler(const char *format, va_list args) |
| 5062 | { |
| 5063 | vfprintf(stderr, format, args); |
| 5064 | } |
| 5065 | |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 5066 | struct display * |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 5067 | display_create(int *argc, char *argv[]) |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 5068 | { |
| 5069 | struct display *d; |
Kristian Høgsberg | a85fe3c | 2010-06-08 14:08:30 -0400 | [diff] [blame] | 5070 | |
Kristian Høgsberg | 2e43720 | 2013-04-16 11:21:48 -0400 | [diff] [blame] | 5071 | wl_log_set_handler_client(log_handler); |
| 5072 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 5073 | d = zalloc(sizeof *d); |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 5074 | if (d == NULL) |
| 5075 | return NULL; |
| 5076 | |
Kristian Høgsberg | 2bb3ebe | 2010-12-01 15:36:20 -0500 | [diff] [blame] | 5077 | d->display = wl_display_connect(NULL); |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 5078 | if (d->display == NULL) { |
Pekka Paalanen | 4e10654 | 2013-02-14 11:49:12 +0200 | [diff] [blame] | 5079 | fprintf(stderr, "failed to connect to Wayland display: %m\n"); |
Rob Bradford | f0a1af9 | 2013-01-10 19:48:54 +0000 | [diff] [blame] | 5080 | free(d); |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 5081 | return NULL; |
| 5082 | } |
| 5083 | |
Rob Bradford | 5ab9c75 | 2013-07-26 16:29:43 +0100 | [diff] [blame] | 5084 | d->xkb_context = xkb_context_new(0); |
| 5085 | if (d->xkb_context == NULL) { |
| 5086 | fprintf(stderr, "Failed to create XKB context\n"); |
| 5087 | free(d); |
| 5088 | return NULL; |
| 5089 | } |
| 5090 | |
Pekka Paalanen | 647f2bf | 2012-05-30 15:53:43 +0300 | [diff] [blame] | 5091 | d->epoll_fd = os_epoll_create_cloexec(); |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 5092 | d->display_fd = wl_display_get_fd(d->display); |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 5093 | d->display_task.run = handle_display_data; |
U. Artie Eoff | 44874d9 | 2012-10-02 21:12:35 -0700 | [diff] [blame] | 5094 | display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP, |
| 5095 | &d->display_task); |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 5096 | |
| 5097 | wl_list_init(&d->deferred_list); |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 5098 | wl_list_init(&d->input_list); |
Kristian Høgsberg | 53ff2f6 | 2011-11-26 17:27:37 -0500 | [diff] [blame] | 5099 | wl_list_init(&d->output_list); |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 5100 | wl_list_init(&d->global_list); |
Kristian Høgsberg | 808fd41 | 2010-07-20 17:06:19 -0400 | [diff] [blame] | 5101 | |
Jonas Ådahl | f77beeb | 2012-10-08 22:49:04 +0200 | [diff] [blame] | 5102 | d->workspace = 0; |
| 5103 | d->workspace_count = 1; |
| 5104 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 5105 | d->registry = wl_display_get_registry(d->display); |
| 5106 | wl_registry_add_listener(d->registry, ®istry_listener, d); |
Pekka Paalanen | 33a68ea | 2013-02-14 12:18:00 +0200 | [diff] [blame] | 5107 | |
| 5108 | if (wl_display_dispatch(d->display) < 0) { |
| 5109 | fprintf(stderr, "Failed to process Wayland connection: %m\n"); |
| 5110 | return NULL; |
| 5111 | } |
| 5112 | |
Kristian Høgsberg | 8f64ed0 | 2012-04-03 11:57:44 -0400 | [diff] [blame] | 5113 | #ifdef HAVE_CAIRO_EGL |
Pekka Paalanen | 4e10654 | 2013-02-14 11:49:12 +0200 | [diff] [blame] | 5114 | if (init_egl(d) < 0) |
| 5115 | fprintf(stderr, "EGL does not seem to work, " |
| 5116 | "falling back to software rendering and wl_shm.\n"); |
Kristian Høgsberg | 8f64ed0 | 2012-04-03 11:57:44 -0400 | [diff] [blame] | 5117 | #endif |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 5118 | |
Ander Conselvan de Oliveira | d8f527c | 2012-05-25 09:30:02 +0300 | [diff] [blame] | 5119 | create_cursors(d); |
Kristian Høgsberg | da275dd | 2010-08-16 17:47:07 -0400 | [diff] [blame] | 5120 | |
Kristian Høgsberg | 5adb480 | 2012-05-15 22:25:28 -0400 | [diff] [blame] | 5121 | d->theme = theme_create(); |
Kristian Høgsberg | dcb71b6 | 2010-06-15 17:16:35 -0400 | [diff] [blame] | 5122 | |
Kristian Høgsberg | 478d926 | 2010-06-08 20:34:11 -0400 | [diff] [blame] | 5123 | wl_list_init(&d->window_list); |
| 5124 | |
Pekka Paalanen | 3cbb089 | 2012-11-19 17:16:01 +0200 | [diff] [blame] | 5125 | init_dummy_surface(d); |
| 5126 | |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 5127 | return d; |
| 5128 | } |
| 5129 | |
Pekka Paalanen | 2c1426a | 2011-12-16 11:35:34 +0200 | [diff] [blame] | 5130 | static void |
| 5131 | display_destroy_outputs(struct display *display) |
| 5132 | { |
| 5133 | struct output *tmp; |
| 5134 | struct output *output; |
| 5135 | |
| 5136 | wl_list_for_each_safe(output, tmp, &display->output_list, link) |
| 5137 | output_destroy(output); |
| 5138 | } |
| 5139 | |
Pekka Paalanen | e1207c7 | 2011-12-16 12:02:09 +0200 | [diff] [blame] | 5140 | static void |
| 5141 | display_destroy_inputs(struct display *display) |
| 5142 | { |
| 5143 | struct input *tmp; |
| 5144 | struct input *input; |
| 5145 | |
| 5146 | wl_list_for_each_safe(input, tmp, &display->input_list, link) |
| 5147 | input_destroy(input); |
| 5148 | } |
| 5149 | |
Pekka Paalanen | 999c5b5 | 2011-11-30 10:52:38 +0200 | [diff] [blame] | 5150 | void |
Pekka Paalanen | fe6079a | 2011-12-15 15:20:04 +0200 | [diff] [blame] | 5151 | display_destroy(struct display *display) |
| 5152 | { |
Pekka Paalanen | c205298 | 2011-12-16 11:41:32 +0200 | [diff] [blame] | 5153 | if (!wl_list_empty(&display->window_list)) |
U. Artie Eoff | 44874d9 | 2012-10-02 21:12:35 -0700 | [diff] [blame] | 5154 | fprintf(stderr, "toytoolkit warning: %d windows exist.\n", |
| 5155 | wl_list_length(&display->window_list)); |
Pekka Paalanen | c205298 | 2011-12-16 11:41:32 +0200 | [diff] [blame] | 5156 | |
| 5157 | if (!wl_list_empty(&display->deferred_list)) |
| 5158 | fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n"); |
| 5159 | |
Pekka Paalanen | 3cbb089 | 2012-11-19 17:16:01 +0200 | [diff] [blame] | 5160 | cairo_surface_destroy(display->dummy_surface); |
| 5161 | free(display->dummy_surface_data); |
| 5162 | |
Pekka Paalanen | 2c1426a | 2011-12-16 11:35:34 +0200 | [diff] [blame] | 5163 | display_destroy_outputs(display); |
Pekka Paalanen | e1207c7 | 2011-12-16 12:02:09 +0200 | [diff] [blame] | 5164 | display_destroy_inputs(display); |
Pekka Paalanen | 2c1426a | 2011-12-16 11:35:34 +0200 | [diff] [blame] | 5165 | |
Daniel Stone | 97f6854 | 2012-05-30 16:32:01 +0100 | [diff] [blame] | 5166 | xkb_context_unref(display->xkb_context); |
Pekka Paalanen | 325bb60 | 2011-12-19 10:31:45 +0200 | [diff] [blame] | 5167 | |
Kristian Høgsberg | 5adb480 | 2012-05-15 22:25:28 -0400 | [diff] [blame] | 5168 | theme_destroy(display->theme); |
Ander Conselvan de Oliveira | d8f527c | 2012-05-25 09:30:02 +0300 | [diff] [blame] | 5169 | destroy_cursors(display); |
Pekka Paalanen | 325bb60 | 2011-12-19 10:31:45 +0200 | [diff] [blame] | 5170 | |
Kristian Høgsberg | 8f64ed0 | 2012-04-03 11:57:44 -0400 | [diff] [blame] | 5171 | #ifdef HAVE_CAIRO_EGL |
Kristian Høgsberg | 4e51b44 | 2013-01-07 15:47:14 -0500 | [diff] [blame] | 5172 | if (display->argb_device) |
| 5173 | fini_egl(display); |
Kristian Høgsberg | 8f64ed0 | 2012-04-03 11:57:44 -0400 | [diff] [blame] | 5174 | #endif |
Pekka Paalanen | fe6079a | 2011-12-15 15:20:04 +0200 | [diff] [blame] | 5175 | |
Pekka Paalanen | 35e8263 | 2013-04-25 13:57:48 +0300 | [diff] [blame] | 5176 | if (display->subcompositor) |
| 5177 | wl_subcompositor_destroy(display->subcompositor); |
| 5178 | |
Pekka Paalanen | c205298 | 2011-12-16 11:41:32 +0200 | [diff] [blame] | 5179 | if (display->shell) |
| 5180 | wl_shell_destroy(display->shell); |
| 5181 | |
| 5182 | if (display->shm) |
| 5183 | wl_shm_destroy(display->shm); |
| 5184 | |
| 5185 | if (display->data_device_manager) |
| 5186 | wl_data_device_manager_destroy(display->data_device_manager); |
| 5187 | |
| 5188 | wl_compositor_destroy(display->compositor); |
Pekka Paalanen | aac1c13 | 2012-12-04 16:01:15 +0200 | [diff] [blame] | 5189 | wl_registry_destroy(display->registry); |
Pekka Paalanen | c205298 | 2011-12-16 11:41:32 +0200 | [diff] [blame] | 5190 | |
| 5191 | close(display->epoll_fd); |
| 5192 | |
U. Artie Eoff | 44874d9 | 2012-10-02 21:12:35 -0700 | [diff] [blame] | 5193 | if (!(display->display_fd_events & EPOLLERR) && |
| 5194 | !(display->display_fd_events & EPOLLHUP)) |
| 5195 | wl_display_flush(display->display); |
| 5196 | |
Kristian Høgsberg | fcfc83f | 2012-02-28 14:29:19 -0500 | [diff] [blame] | 5197 | wl_display_disconnect(display->display); |
Pekka Paalanen | fe6079a | 2011-12-15 15:20:04 +0200 | [diff] [blame] | 5198 | free(display); |
| 5199 | } |
| 5200 | |
| 5201 | void |
Pekka Paalanen | 999c5b5 | 2011-11-30 10:52:38 +0200 | [diff] [blame] | 5202 | display_set_user_data(struct display *display, void *data) |
| 5203 | { |
| 5204 | display->user_data = data; |
| 5205 | } |
| 5206 | |
| 5207 | void * |
| 5208 | display_get_user_data(struct display *display) |
| 5209 | { |
| 5210 | return display->user_data; |
| 5211 | } |
| 5212 | |
Kristian Høgsberg | 9d69f8e | 2010-09-03 14:46:38 -0400 | [diff] [blame] | 5213 | struct wl_display * |
| 5214 | display_get_display(struct display *display) |
| 5215 | { |
| 5216 | return display->display; |
| 5217 | } |
| 5218 | |
Kristian Høgsberg | b20b009 | 2013-08-15 11:54:03 -0700 | [diff] [blame] | 5219 | int |
| 5220 | display_has_subcompositor(struct display *display) |
| 5221 | { |
| 5222 | if (display->subcompositor) |
| 5223 | return 1; |
| 5224 | |
| 5225 | wl_display_roundtrip(display->display); |
| 5226 | |
| 5227 | return display->subcompositor != NULL; |
| 5228 | } |
| 5229 | |
Kristian Høgsberg | 1cc5ac3 | 2013-04-11 21:47:41 -0400 | [diff] [blame] | 5230 | cairo_device_t * |
| 5231 | display_get_cairo_device(struct display *display) |
| 5232 | { |
| 5233 | return display->argb_device; |
| 5234 | } |
| 5235 | |
Kristian Høgsberg | 53ff2f6 | 2011-11-26 17:27:37 -0500 | [diff] [blame] | 5236 | struct output * |
| 5237 | display_get_output(struct display *display) |
| 5238 | { |
| 5239 | return container_of(display->output_list.next, struct output, link); |
| 5240 | } |
| 5241 | |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 5242 | struct wl_compositor * |
| 5243 | display_get_compositor(struct display *display) |
| 5244 | { |
| 5245 | return display->compositor; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 5246 | } |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 5247 | |
Kristian Høgsberg | eae5de7 | 2012-04-11 22:42:15 -0400 | [diff] [blame] | 5248 | uint32_t |
| 5249 | display_get_serial(struct display *display) |
| 5250 | { |
| 5251 | return display->serial; |
| 5252 | } |
| 5253 | |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 5254 | EGLDisplay |
| 5255 | display_get_egl_display(struct display *d) |
| 5256 | { |
| 5257 | return d->dpy; |
| 5258 | } |
| 5259 | |
Kristian Høgsberg | 47fe08a | 2011-10-28 12:26:06 -0400 | [diff] [blame] | 5260 | struct wl_data_source * |
| 5261 | display_create_data_source(struct display *display) |
| 5262 | { |
| 5263 | return wl_data_device_manager_create_data_source(display->data_device_manager); |
| 5264 | } |
| 5265 | |
Benjamin Franzke | cff904e | 2011-02-18 23:00:55 +0100 | [diff] [blame] | 5266 | EGLConfig |
Benjamin Franzke | 0c99163 | 2011-09-27 21:57:31 +0200 | [diff] [blame] | 5267 | display_get_argb_egl_config(struct display *d) |
| 5268 | { |
Kristian Høgsberg | 8e81df4 | 2012-01-11 14:24:46 -0500 | [diff] [blame] | 5269 | return d->argb_config; |
Benjamin Franzke | 0c99163 | 2011-09-27 21:57:31 +0200 | [diff] [blame] | 5270 | } |
| 5271 | |
Kristian Høgsberg | 58eec36 | 2011-01-19 14:27:42 -0500 | [diff] [blame] | 5272 | struct wl_shell * |
| 5273 | display_get_shell(struct display *display) |
| 5274 | { |
| 5275 | return display->shell; |
| 5276 | } |
| 5277 | |
Benjamin Franzke | 1a89f28 | 2011-10-07 09:33:06 +0200 | [diff] [blame] | 5278 | int |
Benjamin Franzke | cff904e | 2011-02-18 23:00:55 +0100 | [diff] [blame] | 5279 | display_acquire_window_surface(struct display *display, |
| 5280 | struct window *window, |
| 5281 | EGLContext ctx) |
| 5282 | { |
Pekka Paalanen | 811ec4f | 2013-02-13 16:17:14 +0200 | [diff] [blame] | 5283 | struct surface *surface = window->main_surface; |
| 5284 | |
Pekka Paalanen | 02bba7c | 2013-02-13 16:17:15 +0200 | [diff] [blame] | 5285 | if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW) |
Benjamin Franzke | 1a89f28 | 2011-10-07 09:33:06 +0200 | [diff] [blame] | 5286 | return -1; |
Benjamin Franzke | cff904e | 2011-02-18 23:00:55 +0100 | [diff] [blame] | 5287 | |
Pekka Paalanen | 6f41b07 | 2013-02-20 13:39:17 +0200 | [diff] [blame] | 5288 | widget_get_cairo_surface(window->main_surface->widget); |
Pekka Paalanen | 811ec4f | 2013-02-13 16:17:14 +0200 | [diff] [blame] | 5289 | return surface->toysurface->acquire(surface->toysurface, ctx); |
Benjamin Franzke | cff904e | 2011-02-18 23:00:55 +0100 | [diff] [blame] | 5290 | } |
| 5291 | |
| 5292 | void |
Benjamin Franzke | 0c99163 | 2011-09-27 21:57:31 +0200 | [diff] [blame] | 5293 | display_release_window_surface(struct display *display, |
| 5294 | struct window *window) |
Benjamin Franzke | cff904e | 2011-02-18 23:00:55 +0100 | [diff] [blame] | 5295 | { |
Pekka Paalanen | 811ec4f | 2013-02-13 16:17:14 +0200 | [diff] [blame] | 5296 | struct surface *surface = window->main_surface; |
| 5297 | |
Pekka Paalanen | 02bba7c | 2013-02-13 16:17:15 +0200 | [diff] [blame] | 5298 | if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW) |
Benjamin Franzke | 0c99163 | 2011-09-27 21:57:31 +0200 | [diff] [blame] | 5299 | return; |
| 5300 | |
Pekka Paalanen | 811ec4f | 2013-02-13 16:17:14 +0200 | [diff] [blame] | 5301 | surface->toysurface->release(surface->toysurface); |
Benjamin Franzke | cff904e | 2011-02-18 23:00:55 +0100 | [diff] [blame] | 5302 | } |
| 5303 | |
| 5304 | void |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 5305 | display_defer(struct display *display, struct task *task) |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 5306 | { |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 5307 | wl_list_insert(&display->deferred_list, &task->link); |
| 5308 | } |
| 5309 | |
| 5310 | void |
| 5311 | display_watch_fd(struct display *display, |
| 5312 | int fd, uint32_t events, struct task *task) |
| 5313 | { |
| 5314 | struct epoll_event ep; |
| 5315 | |
| 5316 | ep.events = events; |
| 5317 | ep.data.ptr = task; |
| 5318 | epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep); |
| 5319 | } |
| 5320 | |
| 5321 | void |
Dima Ryazanov | a85292e | 2012-11-29 00:27:09 -0800 | [diff] [blame] | 5322 | display_unwatch_fd(struct display *display, int fd) |
| 5323 | { |
| 5324 | epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL); |
| 5325 | } |
| 5326 | |
| 5327 | void |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 5328 | display_run(struct display *display) |
| 5329 | { |
| 5330 | struct task *task; |
| 5331 | struct epoll_event ep[16]; |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 5332 | int i, count, ret; |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 5333 | |
Pekka Paalanen | 826d795 | 2011-12-15 10:14:07 +0200 | [diff] [blame] | 5334 | display->running = 1; |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 5335 | while (1) { |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 5336 | while (!wl_list_empty(&display->deferred_list)) { |
Tiago Vignatti | 6f09338 | 2012-09-27 14:46:23 +0300 | [diff] [blame] | 5337 | task = container_of(display->deferred_list.prev, |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 5338 | struct task, link); |
| 5339 | wl_list_remove(&task->link); |
| 5340 | task->run(task, 0); |
| 5341 | } |
Kristian Høgsberg | 9ca2d08 | 2012-01-09 18:48:14 -0500 | [diff] [blame] | 5342 | |
Kristian Høgsberg | feb3c1d | 2012-10-15 12:56:11 -0400 | [diff] [blame] | 5343 | wl_display_dispatch_pending(display->display); |
| 5344 | |
Kristian Høgsberg | 9ca2d08 | 2012-01-09 18:48:14 -0500 | [diff] [blame] | 5345 | if (!display->running) |
| 5346 | break; |
| 5347 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 5348 | ret = wl_display_flush(display->display); |
| 5349 | if (ret < 0 && errno == EAGAIN) { |
| 5350 | ep[0].events = |
| 5351 | EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP; |
| 5352 | ep[0].data.ptr = &display->display_task; |
| 5353 | |
| 5354 | epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD, |
| 5355 | display->display_fd, &ep[0]); |
| 5356 | } else if (ret < 0) { |
| 5357 | break; |
| 5358 | } |
Kristian Høgsberg | 9ca2d08 | 2012-01-09 18:48:14 -0500 | [diff] [blame] | 5359 | |
| 5360 | count = epoll_wait(display->epoll_fd, |
| 5361 | ep, ARRAY_LENGTH(ep), -1); |
| 5362 | for (i = 0; i < count; i++) { |
| 5363 | task = ep[i].data.ptr; |
| 5364 | task->run(task, ep[i].events); |
| 5365 | } |
Kristian Høgsberg | 3a69627 | 2011-09-14 17:33:48 -0400 | [diff] [blame] | 5366 | } |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 5367 | } |
Pekka Paalanen | 826d795 | 2011-12-15 10:14:07 +0200 | [diff] [blame] | 5368 | |
| 5369 | void |
| 5370 | display_exit(struct display *display) |
| 5371 | { |
| 5372 | display->running = 0; |
| 5373 | } |
Jan Arne Petersen | cd99706 | 2012-11-18 19:06:44 +0100 | [diff] [blame] | 5374 | |
| 5375 | void |
| 5376 | keysym_modifiers_add(struct wl_array *modifiers_map, |
| 5377 | const char *name) |
| 5378 | { |
| 5379 | size_t len = strlen(name) + 1; |
| 5380 | char *p; |
| 5381 | |
| 5382 | p = wl_array_add(modifiers_map, len); |
| 5383 | |
| 5384 | if (p == NULL) |
| 5385 | return; |
| 5386 | |
| 5387 | strncpy(p, name, len); |
| 5388 | } |
| 5389 | |
| 5390 | static xkb_mod_index_t |
| 5391 | keysym_modifiers_get_index(struct wl_array *modifiers_map, |
| 5392 | const char *name) |
| 5393 | { |
| 5394 | xkb_mod_index_t index = 0; |
| 5395 | char *p = modifiers_map->data; |
| 5396 | |
| 5397 | while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) { |
| 5398 | if (strcmp(p, name) == 0) |
| 5399 | return index; |
| 5400 | |
| 5401 | index++; |
| 5402 | p += strlen(p) + 1; |
| 5403 | } |
| 5404 | |
| 5405 | return XKB_MOD_INVALID; |
| 5406 | } |
| 5407 | |
| 5408 | xkb_mod_mask_t |
| 5409 | keysym_modifiers_get_mask(struct wl_array *modifiers_map, |
| 5410 | const char *name) |
| 5411 | { |
| 5412 | xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name); |
| 5413 | |
| 5414 | if (index == XKB_MOD_INVALID) |
| 5415 | return XKB_MOD_INVALID; |
| 5416 | |
| 5417 | return 1 << index; |
| 5418 | } |
Kristian Høgsberg | ce27841 | 2013-07-25 15:20:20 -0700 | [diff] [blame] | 5419 | |
| 5420 | void * |
| 5421 | fail_on_null(void *p) |
| 5422 | { |
| 5423 | if (p == NULL) { |
Peter Hutterer | 3ca59d3 | 2013-08-08 17:13:47 +1000 | [diff] [blame] | 5424 | fprintf(stderr, "%s: out of memory\n", program_invocation_short_name); |
Kristian Høgsberg | ce27841 | 2013-07-25 15:20:20 -0700 | [diff] [blame] | 5425 | exit(EXIT_FAILURE); |
| 5426 | } |
| 5427 | |
| 5428 | return p; |
| 5429 | } |
| 5430 | |
| 5431 | void * |
| 5432 | xmalloc(size_t s) |
| 5433 | { |
| 5434 | return fail_on_null(malloc(s)); |
| 5435 | } |
| 5436 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 5437 | void * |
| 5438 | xzalloc(size_t s) |
| 5439 | { |
| 5440 | return fail_on_null(zalloc(s)); |
| 5441 | } |
| 5442 | |
Kristian Høgsberg | ce27841 | 2013-07-25 15:20:20 -0700 | [diff] [blame] | 5443 | char * |
| 5444 | xstrdup(const char *s) |
| 5445 | { |
| 5446 | return fail_on_null(strdup(s)); |
| 5447 | } |