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