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