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