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