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