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