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