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