blob: 8d956aed94fb2a5c610352d2fcbdb8886f8407d8 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
Pekka Paalanen4e373742013-02-13 16:17:13 +02003 * Copyright © 2012-2013 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05004 *
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
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040025
Kristian Høgsberg61017b12008-11-02 18:51:48 -050026#include <stdint.h>
27#include <stdio.h>
28#include <stdlib.h>
Pekka Paalanen71233882013-04-25 13:57:53 +030029#include <stdarg.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050030#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050031#include <fcntl.h>
32#include <unistd.h>
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040033#include <errno.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050034#include <math.h>
Benjamin Franzke0c991632011-09-27 21:57:31 +020035#include <assert.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050036#include <time.h>
37#include <cairo.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040038#include <sys/mman.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040039#include <sys/epoll.h>
Tiago Vignatti82db9d82012-05-23 22:06:27 +030040#include <sys/timerfd.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030042#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050043#include <wayland-egl.h>
44
Rob Clark6396ed32012-03-11 19:48:41 -050045#ifdef USE_CAIRO_GLESV2
46#include <GLES2/gl2.h>
47#include <GLES2/gl2ext.h>
48#else
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040049#include <GL/gl.h>
Rob Clark6396ed32012-03-11 19:48:41 -050050#endif
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040051#include <EGL/egl.h>
52#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040053
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040054#include <cairo-gl.h>
Pekka Paalanenfb39d8d2012-10-16 17:27:19 +030055#else /* HAVE_CAIRO_EGL */
56typedef void *EGLDisplay;
57typedef void *EGLConfig;
58typedef void *EGLContext;
59#define EGL_NO_DISPLAY ((EGLDisplay)0)
60#endif /* no HAVE_CAIRO_EGL */
Kristian Høgsberg61017b12008-11-02 18:51:48 -050061
Daniel Stone9d4f0302012-02-15 16:33:21 +000062#include <xkbcommon/xkbcommon.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030063#include <wayland-cursor.h>
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040064
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050065#include <linux/input.h>
Pekka Paalanen50719bc2011-11-22 14:18:50 +020066#include <wayland-client.h>
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040067#include "../shared/cairo-util.h"
Scott Moreau7a1b32a2012-05-27 14:25:02 -060068#include "text-cursor-position-client-protocol.h"
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020069#include "workspaces-client-protocol.h"
Pekka Paalanen647f2bf2012-05-30 15:53:43 +030070#include "../shared/os-compatibility.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050071
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050072#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050073
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -070074struct shm_pool;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +030075
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040076struct global {
77 uint32_t name;
78 char *interface;
79 uint32_t version;
80 struct wl_list link;
81};
82
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050083struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050084 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040085 struct wl_registry *registry;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050086 struct wl_compositor *compositor;
Pekka Paalanen35e82632013-04-25 13:57:48 +030087 struct wl_subcompositor *subcompositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040088 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040089 struct wl_shm *shm;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040090 struct wl_data_device_manager *data_device_manager;
Scott Moreau7a1b32a2012-05-27 14:25:02 -060091 struct text_cursor_position *text_cursor_position;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +020092 struct workspace_manager *workspace_manager;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040093 EGLDisplay dpy;
Kristian Høgsberg8e81df42012-01-11 14:24:46 -050094 EGLConfig argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +020095 EGLContext argb_ctx;
Benjamin Franzke0c991632011-09-27 21:57:31 +020096 cairo_device_t *argb_device;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040097 uint32_t serial;
Kristian Høgsberg3a696272011-09-14 17:33:48 -040098
99 int display_fd;
U. Artie Eoff44874d92012-10-02 21:12:35 -0700100 uint32_t display_fd_events;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400101 struct task display_task;
102
103 int epoll_fd;
104 struct wl_list deferred_list;
105
Pekka Paalanen826d7952011-12-15 10:14:07 +0200106 int running;
107
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400108 struct wl_list global_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400109 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400110 struct wl_list input_list;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500111 struct wl_list output_list;
Kristian Høgsberg291c69c2012-05-15 22:12:54 -0400112
Kristian Høgsberg5adb4802012-05-15 22:25:28 -0400113 struct theme *theme;
Kristian Høgsberg70163132012-05-08 15:55:39 -0400114
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300115 struct wl_cursor_theme *cursor_theme;
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300116 struct wl_cursor **cursors;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400117
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200118 display_output_handler_t output_configure_handler;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400119 display_global_handler_t global_handler;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200120
121 void *user_data;
Daniel Stone97f68542012-05-30 16:32:01 +0100122
123 struct xkb_context *xkb_context;
Jonas Ådahl14c92ff2012-08-29 22:13:02 +0200124
125 uint32_t workspace;
126 uint32_t workspace_count;
Pekka Paalanen3cbb0892012-11-19 17:16:01 +0200127
128 /* A hack to get text extents for tooltips */
129 cairo_surface_t *dummy_surface;
130 void *dummy_surface_data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500131};
132
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400133enum {
Kristian Høgsberg407ef642012-04-27 17:17:12 -0400134 TYPE_NONE,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400135 TYPE_TOPLEVEL,
Kristian Høgsbergf8ab46e2011-09-08 16:56:38 -0400136 TYPE_FULLSCREEN,
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -0500137 TYPE_MAXIMIZED,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400138 TYPE_TRANSIENT,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -0500139 TYPE_MENU,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400140 TYPE_CUSTOM
141};
Rob Bradford7507b572012-05-15 17:55:34 +0100142
143struct window_output {
144 struct output *output;
145 struct wl_list link;
146};
147
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200148struct 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.
Alexander Larsson5e9b6522013-05-22 14:41:28 +0200153 * width,height are the new buffer size.
Pekka Paalanenec076692012-11-30 13:37:27 +0200154 * If flags has SURFACE_HINT_RESIZE set, the user is
155 * doing continuous resizing.
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200156 * Returns the Cairo surface to draw to.
157 */
158 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200159 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200160 enum wl_output_transform buffer_transform, int32_t buffer_scale);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200161
162 /*
163 * Post the surface to the server, returning the server allocation
164 * rectangle. The Cairo surface from prepare() must be destroyed
165 * after calling this.
166 */
167 void (*swap)(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200168 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200169 struct rectangle *server_allocation);
170
171 /*
172 * Make the toysurface current with the given EGL context.
173 * Returns 0 on success, and negative of failure.
174 */
175 int (*acquire)(struct toysurface *base, EGLContext ctx);
176
177 /*
178 * Release the toysurface from the EGL context, returning control
179 * to Cairo.
180 */
181 void (*release)(struct toysurface *base);
182
183 /*
184 * Destroy the toysurface, including the Cairo surface, any
185 * backing storage, and the Wayland protocol objects.
186 */
187 void (*destroy)(struct toysurface *base);
188};
189
Pekka Paalanen4e373742013-02-13 16:17:13 +0200190struct surface {
191 struct window *window;
192
193 struct wl_surface *surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300194 struct wl_subsurface *subsurface;
195 int synchronized;
196 int synchronized_default;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200197 struct toysurface *toysurface;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +0200198 struct widget *widget;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300199 int redraw_needed;
200 struct wl_callback *frame_cb;
Pekka Paalanen7ff7a802013-04-25 13:57:50 +0300201 uint32_t last_time;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200202
203 struct rectangle allocation;
204 struct rectangle server_allocation;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200205
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +0200206 struct wl_region *input_region;
207 struct wl_region *opaque_region;
208
Pekka Paalanen02bba7c2013-02-13 16:17:15 +0200209 enum window_buffer_type buffer_type;
210 enum wl_output_transform buffer_transform;
Alexander Larssonedddbd12013-05-24 13:09:43 +0200211 int32_t buffer_scale;
Pekka Paalanen89dee002013-02-13 16:17:20 +0200212
213 cairo_surface_t *cairo_surface;
Pekka Paalanen35e82632013-04-25 13:57:48 +0300214
215 struct wl_list link;
Pekka Paalanen4e373742013-02-13 16:17:13 +0200216};
217
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500218struct window {
219 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500220 struct window *parent;
Rob Bradford7507b572012-05-15 17:55:34 +0100221 struct wl_list window_output_list;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -0500222 char *title;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +0200223 struct rectangle saved_allocation;
Kristian Høgsbergd3a19652012-07-20 11:32:51 -0400224 struct rectangle min_allocation;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -0500225 struct rectangle pending_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500226 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400227 int resize_edges;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -0400228 int redraw_needed;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +0300229 int redraw_task_scheduled;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400230 struct task redraw_task;
Kristian Høgsberg42b4f802012-03-26 13:49:29 -0400231 int resize_needed;
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +0100232 int saved_type;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400233 int type;
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400234 int focus_count;
235
Pekka Paalanen99436862012-11-19 17:15:59 +0200236 int resizing;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -0200237 int fullscreen_method;
MoD063a8822013-03-02 23:43:57 +0000238 int configure_requests;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400239
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500240 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500241 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400242 window_data_handler_t data_handler;
243 window_drop_handler_t drop_handler;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500244 window_close_handler_t close_handler;
Kristian Høgsberg67ace202012-07-23 21:56:31 -0400245 window_fullscreen_handler_t fullscreen_handler;
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +0200246 window_output_handler_t output_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400247
Pekka Paalanen4e373742013-02-13 16:17:13 +0200248 struct surface *main_surface;
249 struct wl_shell_surface *shell_surface;
Pekka Vuorela6e1e3852012-09-17 22:15:57 +0300250
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +0200251 struct frame *frame;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500252
Pekka Paalanen35e82632013-04-25 13:57:48 +0300253 /* struct surface::link, contains also main_surface */
254 struct wl_list subsurface_list;
255
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500256 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400257 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500258};
259
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500260struct widget {
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500261 struct window *window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +0200262 struct surface *surface;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300263 struct tooltip *tooltip;
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500264 struct wl_list child_list;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400265 struct wl_list link;
266 struct rectangle allocation;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500267 widget_resize_handler_t resize_handler;
268 widget_redraw_handler_t redraw_handler;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500269 widget_enter_handler_t enter_handler;
270 widget_leave_handler_t leave_handler;
Kristian Høgsberg04e98342012-01-09 09:36:16 -0500271 widget_motion_handler_t motion_handler;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500272 widget_button_handler_t button_handler;
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +0200273 widget_axis_handler_t axis_handler;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400274 void *user_data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -0500275 int opaque;
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300276 int tooltip_count;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -0500277 int default_cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400278};
279
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400280struct input {
281 struct display *display;
Daniel Stone37816df2012-05-16 18:45:18 +0100282 struct wl_seat *seat;
283 struct wl_pointer *pointer;
284 struct wl_keyboard *keyboard;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400285 struct window *pointer_focus;
286 struct window *keyboard_focus;
Ander Conselvan de Oliveira1493d2a2012-05-03 12:29:46 +0300287 int current_cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +0300288 uint32_t cursor_anim_start;
289 struct wl_callback *cursor_frame_cb;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300290 struct wl_surface *pointer_surface;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400291 uint32_t modifiers;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400292 uint32_t pointer_enter_serial;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -0400293 uint32_t cursor_serial;
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400294 float sx, sy;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400295 struct wl_list link;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400296
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500297 struct widget *focus_widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500298 struct widget *grab;
299 uint32_t grab_button;
300
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400301 struct wl_data_device *data_device;
302 struct data_offer *drag_offer;
303 struct data_offer *selection_offer;
Daniel Stone97f68542012-05-30 16:32:01 +0100304
305 struct {
Daniel Stone97f68542012-05-30 16:32:01 +0100306 struct xkb_keymap *keymap;
307 struct xkb_state *state;
308 xkb_mod_mask_t control_mask;
309 xkb_mod_mask_t alt_mask;
310 xkb_mod_mask_t shift_mask;
311 } xkb;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -0400312
313 struct task repeat_task;
314 int repeat_timer_fd;
315 uint32_t repeat_sym;
316 uint32_t repeat_key;
317 uint32_t repeat_time;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400318};
319
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500320struct output {
321 struct display *display;
322 struct wl_output *output;
323 struct rectangle allocation;
324 struct wl_list link;
Scott Moreau4e072362012-09-29 02:03:11 -0600325 int transform;
Alexander Larssonafd319a2013-05-22 14:41:27 +0200326 int scale;
Pekka Paalanen999c5b52011-11-30 10:52:38 +0200327
328 display_output_handler_t destroy_handler;
329 void *user_data;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -0500330};
331
Martin Minarik1998b152012-05-10 02:04:35 +0200332enum frame_button_action {
333 FRAME_BUTTON_NULL = 0,
334 FRAME_BUTTON_ICON = 1,
335 FRAME_BUTTON_CLOSE = 2,
336 FRAME_BUTTON_MINIMIZE = 3,
337 FRAME_BUTTON_MAXIMIZE = 4,
338};
339
340enum frame_button_pointer {
341 FRAME_BUTTON_DEFAULT = 0,
342 FRAME_BUTTON_OVER = 1,
343 FRAME_BUTTON_ACTIVE = 2,
344};
345
346enum frame_button_align {
347 FRAME_BUTTON_RIGHT = 0,
348 FRAME_BUTTON_LEFT = 1,
349};
350
351enum frame_button_decoration {
352 FRAME_BUTTON_NONE = 0,
353 FRAME_BUTTON_FANCY = 1,
354};
355
356struct frame_button {
357 struct widget *widget;
358 struct frame *frame;
359 cairo_surface_t *icon;
360 enum frame_button_action type;
361 enum frame_button_pointer state;
362 struct wl_list link; /* buttons_list */
363 enum frame_button_align align;
364 enum frame_button_decoration decoration;
365};
366
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500367struct frame {
368 struct widget *widget;
369 struct widget *child;
Martin Minarik1998b152012-05-10 02:04:35 +0200370 struct wl_list buttons_list;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500371};
372
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500373struct menu {
374 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500375 struct widget *widget;
Kristian Høgsberg831dd522012-01-10 23:46:33 -0500376 struct input *input;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500377 const char **entries;
378 uint32_t time;
379 int current;
380 int count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -0400381 int release_count;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500382 menu_func_t func;
383};
384
Tiago Vignatti82db9d82012-05-23 22:06:27 +0300385struct tooltip {
386 struct widget *parent;
387 struct window *window;
388 struct widget *widget;
389 char *entry;
390 struct task tooltip_task;
391 int tooltip_fd;
392 float x, y;
393};
394
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700395struct shm_pool {
396 struct wl_shm_pool *pool;
397 size_t size;
398 size_t used;
399 void *data;
400};
401
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400402enum {
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300403 CURSOR_DEFAULT = 100,
404 CURSOR_UNSET
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400405};
406
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500407enum window_location {
408 WINDOW_INTERIOR = 0,
409 WINDOW_RESIZING_TOP = 1,
410 WINDOW_RESIZING_BOTTOM = 2,
411 WINDOW_RESIZING_LEFT = 4,
412 WINDOW_RESIZING_TOP_LEFT = 5,
413 WINDOW_RESIZING_BOTTOM_LEFT = 6,
414 WINDOW_RESIZING_RIGHT = 8,
415 WINDOW_RESIZING_TOP_RIGHT = 9,
416 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
417 WINDOW_RESIZING_MASK = 15,
418 WINDOW_EXTERIOR = 16,
419 WINDOW_TITLEBAR = 17,
420 WINDOW_CLIENT_AREA = 18,
421};
422
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200423static const cairo_user_data_key_t shm_surface_data_key;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400424
Pekka Paalanen97777442013-05-22 10:20:05 +0300425/* #define DEBUG */
426
427#ifdef DEBUG
Pekka Paalanen71233882013-04-25 13:57:53 +0300428
429static void
430debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
431__attribute__ ((format (printf, 4, 5)));
432
433static void
434debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
435{
436 va_list ap;
437 struct timeval tv;
438
439 gettimeofday(&tv, NULL);
440 fprintf(stderr, "%8ld.%03ld ",
441 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
442
443 if (proxy)
444 fprintf(stderr, "%s@%d ",
445 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
446
447 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
448 fprintf(stderr, "%s ", func);
449
450 va_start(ap, fmt);
451 vfprintf(stderr, fmt, ap);
452 va_end(ap);
453}
454
455#define DBG(fmt, ...) \
456 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
457
458#define DBG_OBJ(obj, fmt, ...) \
459 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
460
461#else
462
463#define DBG(...) do {} while (0)
464#define DBG_OBJ(...) do {} while (0)
465
466#endif
467
Alexander Larsson1818e312013-05-22 14:41:31 +0200468static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200469surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
Alexander Larsson1818e312013-05-22 14:41:31 +0200470{
471 int32_t tmp;
472
473 switch (buffer_transform) {
474 case WL_OUTPUT_TRANSFORM_90:
475 case WL_OUTPUT_TRANSFORM_270:
476 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
477 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
478 tmp = *width;
479 *width = *height;
480 *height = tmp;
481 break;
482 default:
483 break;
484 }
485
486 *width *= buffer_scale;
487 *height *= buffer_scale;
488}
489
490static void
Alexander Larssonedddbd12013-05-24 13:09:43 +0200491buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
Alexander Larsson1818e312013-05-22 14:41:31 +0200492{
493 int32_t tmp;
494
495 switch (buffer_transform) {
496 case WL_OUTPUT_TRANSFORM_90:
497 case WL_OUTPUT_TRANSFORM_270:
498 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
499 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
500 tmp = *width;
501 *width = *height;
502 *height = tmp;
503 break;
504 default:
505 break;
506 }
507
508 *width /= buffer_scale;
509 *height /= buffer_scale;
510}
511
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500512#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400513
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200514struct egl_window_surface {
515 struct toysurface base;
516 cairo_surface_t *cairo_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100517 struct display *display;
518 struct wl_surface *surface;
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200519 struct wl_egl_window *egl_window;
520 EGLSurface egl_surface;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100521};
522
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200523static struct egl_window_surface *
524to_egl_window_surface(struct toysurface *base)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100525{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200526 return container_of(base, struct egl_window_surface, base);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100527}
528
529static cairo_surface_t *
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200530egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +0200531 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200532 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100533{
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200534 struct egl_window_surface *surface = to_egl_window_surface(base);
535
Alexander Larsson1818e312013-05-22 14:41:31 +0200536 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
537
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200538 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
539 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
540
541 return cairo_surface_reference(surface->cairo_surface);
542}
543
544static void
545egl_window_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200546 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200547 struct rectangle *server_allocation)
548{
549 struct egl_window_surface *surface = to_egl_window_surface(base);
550
551 cairo_gl_surface_swapbuffers(surface->cairo_surface);
552 wl_egl_window_get_attached_size(surface->egl_window,
553 &server_allocation->width,
554 &server_allocation->height);
Alexander Larsson1818e312013-05-22 14:41:31 +0200555
556 buffer_to_surface_size (buffer_transform, buffer_scale,
557 &server_allocation->width,
558 &server_allocation->height);
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200559}
560
561static int
562egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
563{
564 struct egl_window_surface *surface = to_egl_window_surface(base);
Benjamin Franzke0c991632011-09-27 21:57:31 +0200565 cairo_device_t *device;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100566
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200567 device = cairo_surface_get_device(surface->cairo_surface);
568 if (!device)
569 return -1;
570
571 if (!ctx) {
572 if (device == surface->display->argb_device)
573 ctx = surface->display->argb_ctx;
574 else
575 assert(0);
576 }
577
578 cairo_device_flush(device);
579 cairo_device_acquire(device);
580 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
581 surface->egl_surface, ctx))
582 fprintf(stderr, "failed to make surface current\n");
583
584 return 0;
585}
586
587static void
588egl_window_surface_release(struct toysurface *base)
589{
590 struct egl_window_surface *surface = to_egl_window_surface(base);
591 cairo_device_t *device;
592
593 device = cairo_surface_get_device(surface->cairo_surface);
594 if (!device)
595 return;
596
597 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
598 surface->display->argb_ctx))
599 fprintf(stderr, "failed to make context current\n");
600
601 cairo_device_release(device);
602}
603
604static void
605egl_window_surface_destroy(struct toysurface *base)
606{
607 struct egl_window_surface *surface = to_egl_window_surface(base);
608 struct display *d = surface->display;
609
610 cairo_surface_destroy(surface->cairo_surface);
611 eglDestroySurface(d->dpy, surface->egl_surface);
612 wl_egl_window_destroy(surface->egl_window);
613 surface->surface = NULL;
614
615 free(surface);
616}
617
618static struct toysurface *
619egl_window_surface_create(struct display *display,
620 struct wl_surface *wl_surface,
621 uint32_t flags,
622 struct rectangle *rectangle)
623{
624 struct egl_window_surface *surface;
625
Pekka Paalanenb3627362012-11-19 17:16:00 +0200626 if (display->dpy == EGL_NO_DISPLAY)
627 return NULL;
628
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200629 surface = calloc(1, sizeof *surface);
630 if (!surface)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100631 return NULL;
632
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200633 surface->base.prepare = egl_window_surface_prepare;
634 surface->base.swap = egl_window_surface_swap;
635 surface->base.acquire = egl_window_surface_acquire;
636 surface->base.release = egl_window_surface_release;
637 surface->base.destroy = egl_window_surface_destroy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100638
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200639 surface->display = display;
640 surface->surface = wl_surface;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400641
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200642 surface->egl_window = wl_egl_window_create(surface->surface,
643 rectangle->width,
644 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100645
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200646 surface->egl_surface = eglCreateWindowSurface(display->dpy,
647 display->argb_config,
648 surface->egl_window,
649 NULL);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100650
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200651 surface->cairo_surface =
652 cairo_gl_surface_create_for_egl(display->argb_device,
653 surface->egl_surface,
654 rectangle->width,
655 rectangle->height);
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100656
Pekka Paalanen03fc3162012-11-19 17:15:58 +0200657 return &surface->base;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100658}
659
Pekka Paalanenb3627362012-11-19 17:16:00 +0200660#else
661
662static struct toysurface *
663egl_window_surface_create(struct display *display,
664 struct wl_surface *wl_surface,
665 uint32_t flags,
666 struct rectangle *rectangle)
667{
668 return NULL;
669}
670
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400671#endif
672
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200673struct shm_surface_data {
674 struct wl_buffer *buffer;
675 struct shm_pool *pool;
676};
677
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400678struct wl_buffer *
679display_get_buffer_for_surface(struct display *display,
680 cairo_surface_t *surface)
681{
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200682 struct shm_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400683
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200684 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400685
686 return data->buffer;
687}
688
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500689static void
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700690shm_pool_destroy(struct shm_pool *pool);
691
692static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400693shm_surface_data_destroy(void *p)
694{
695 struct shm_surface_data *data = p;
696
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200697 wl_buffer_destroy(data->buffer);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700698 if (data->pool)
699 shm_pool_destroy(data->pool);
Ander Conselvan de Oliveira2a3cd282012-06-19 13:45:55 +0300700
701 free(data);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400702}
703
Kristian Høgsberg16626282012-04-03 11:21:27 -0400704static struct wl_shm_pool *
705make_shm_pool(struct display *display, int size, void **data)
706{
Kristian Høgsberg16626282012-04-03 11:21:27 -0400707 struct wl_shm_pool *pool;
708 int fd;
709
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300710 fd = os_create_anonymous_file(size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400711 if (fd < 0) {
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +0300712 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
713 size);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400714 return NULL;
715 }
716
717 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Kristian Høgsberg16626282012-04-03 11:21:27 -0400718 if (*data == MAP_FAILED) {
719 fprintf(stderr, "mmap failed: %m\n");
720 close(fd);
721 return NULL;
722 }
723
724 pool = wl_shm_create_pool(display->shm, fd, size);
725
726 close(fd);
727
728 return pool;
729}
730
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700731static struct shm_pool *
732shm_pool_create(struct display *display, size_t size)
733{
734 struct shm_pool *pool = malloc(sizeof *pool);
735
736 if (!pool)
737 return NULL;
738
739 pool->pool = make_shm_pool(display, size, &pool->data);
740 if (!pool->pool) {
741 free(pool);
742 return NULL;
743 }
744
745 pool->size = size;
746 pool->used = 0;
747
748 return pool;
749}
750
751static void *
752shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
753{
754 if (pool->used + size > pool->size)
755 return NULL;
756
757 *offset = pool->used;
758 pool->used += size;
759
760 return (char *) pool->data + *offset;
761}
762
763/* destroy the pool. this does not unmap the memory though */
764static void
765shm_pool_destroy(struct shm_pool *pool)
766{
767 munmap(pool->data, pool->size);
768 wl_shm_pool_destroy(pool->pool);
769 free(pool);
770}
771
772/* Start allocating from the beginning of the pool again */
773static void
774shm_pool_reset(struct shm_pool *pool)
775{
776 pool->used = 0;
777}
778
779static int
780data_length_for_shm_surface(struct rectangle *rect)
781{
782 int stride;
783
784 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
785 rect->width);
786 return stride * rect->height;
787}
788
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500789static cairo_surface_t *
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700790display_create_shm_surface_from_pool(struct display *display,
791 struct rectangle *rectangle,
792 uint32_t flags, struct shm_pool *pool)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400793{
794 struct shm_surface_data *data;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400795 uint32_t format;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400796 cairo_surface_t *surface;
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700797 int stride, length, offset;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400798 void *map;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400799
800 data = malloc(sizeof *data);
801 if (data == NULL)
802 return NULL;
803
804 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
805 rectangle->width);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700806 length = stride * rectangle->height;
807 data->pool = NULL;
808 map = shm_pool_allocate(pool, length, &offset);
809
810 if (!map) {
811 free(data);
812 return NULL;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400813 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400814
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -0400815 surface = cairo_image_surface_create_for_data (map,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400816 CAIRO_FORMAT_ARGB32,
817 rectangle->width,
818 rectangle->height,
819 stride);
820
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200821 cairo_surface_set_user_data(surface, &shm_surface_data_key,
822 data, shm_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400823
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400824 if (flags & SURFACE_OPAQUE)
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500825 format = WL_SHM_FORMAT_XRGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400826 else
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500827 format = WL_SHM_FORMAT_ARGB8888;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400828
Pekka Paalanen0c6a3432012-11-19 15:32:50 +0200829 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
830 rectangle->width,
831 rectangle->height,
832 stride, format);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400833
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700834 return surface;
835}
836
837static cairo_surface_t *
838display_create_shm_surface(struct display *display,
839 struct rectangle *rectangle, uint32_t flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200840 struct shm_pool *alternate_pool,
841 struct shm_surface_data **data_ret)
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700842{
843 struct shm_surface_data *data;
844 struct shm_pool *pool;
845 cairo_surface_t *surface;
846
Pekka Paalanen99436862012-11-19 17:15:59 +0200847 if (alternate_pool) {
848 shm_pool_reset(alternate_pool);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700849 surface = display_create_shm_surface_from_pool(display,
850 rectangle,
851 flags,
Pekka Paalanen99436862012-11-19 17:15:59 +0200852 alternate_pool);
853 if (surface) {
854 data = cairo_surface_get_user_data(surface,
855 &shm_surface_data_key);
856 goto out;
857 }
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700858 }
859
860 pool = shm_pool_create(display,
861 data_length_for_shm_surface(rectangle));
862 if (!pool)
863 return NULL;
864
865 surface =
866 display_create_shm_surface_from_pool(display, rectangle,
867 flags, pool);
868
869 if (!surface) {
870 shm_pool_destroy(pool);
871 return NULL;
872 }
873
874 /* make sure we destroy the pool when the surface is destroyed */
Pekka Paalanen32127ca2012-11-19 15:32:51 +0200875 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
Ander Conselvan de Oliveira001de542012-05-07 11:34:26 -0700876 data->pool = pool;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400877
Pekka Paalanen99436862012-11-19 17:15:59 +0200878out:
879 if (data_ret)
880 *data_ret = data;
881
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400882 return surface;
883}
884
nobled7b87cb02011-02-01 18:51:47 +0000885static int
886check_size(struct rectangle *rect)
887{
888 if (rect->width && rect->height)
889 return 0;
890
891 fprintf(stderr, "tried to create surface of "
892 "width: %d, height: %d\n", rect->width, rect->height);
893 return -1;
894}
895
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400896cairo_surface_t *
897display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100898 struct wl_surface *surface,
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400899 struct rectangle *rectangle,
900 uint32_t flags)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400901{
nobled7b87cb02011-02-01 18:51:47 +0000902 if (check_size(rectangle) < 0)
903 return NULL;
Pekka Paalanen768117f2012-11-19 17:15:57 +0200904
905 assert(flags & SURFACE_SHM);
Pekka Paalanen99436862012-11-19 17:15:59 +0200906 return display_create_shm_surface(display, rectangle, flags,
907 NULL, NULL);
908}
909
Pekka Paalanena4eda732012-11-19 17:16:02 +0200910struct shm_surface_leaf {
911 cairo_surface_t *cairo_surface;
912 /* 'data' is automatically destroyed, when 'cairo_surface' is */
913 struct shm_surface_data *data;
914
915 struct shm_pool *resize_pool;
916 int busy;
917};
918
919static void
920shm_surface_leaf_release(struct shm_surface_leaf *leaf)
921{
922 if (leaf->cairo_surface)
923 cairo_surface_destroy(leaf->cairo_surface);
924 /* leaf->data already destroyed via cairo private */
925
926 if (leaf->resize_pool)
927 shm_pool_destroy(leaf->resize_pool);
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200928
929 memset(leaf, 0, sizeof *leaf);
Pekka Paalanena4eda732012-11-19 17:16:02 +0200930}
931
Pekka Paalanenaef02542013-04-25 13:57:47 +0300932#define MAX_LEAVES 3
933
Pekka Paalanen99436862012-11-19 17:15:59 +0200934struct shm_surface {
935 struct toysurface base;
936 struct display *display;
937 struct wl_surface *surface;
938 uint32_t flags;
939 int dx, dy;
940
Pekka Paalanenaef02542013-04-25 13:57:47 +0300941 struct shm_surface_leaf leaf[MAX_LEAVES];
Pekka Paalanena4eda732012-11-19 17:16:02 +0200942 struct shm_surface_leaf *current;
Pekka Paalanen99436862012-11-19 17:15:59 +0200943};
944
945static struct shm_surface *
946to_shm_surface(struct toysurface *base)
947{
948 return container_of(base, struct shm_surface, base);
949}
950
Pekka Paalanena4eda732012-11-19 17:16:02 +0200951static void
Pekka Paalanen97777442013-05-22 10:20:05 +0300952shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
953{
954#ifdef DEBUG
955 struct shm_surface_leaf *leaf;
956 char bufs[MAX_LEAVES + 1];
957 int i;
958
959 for (i = 0; i < MAX_LEAVES; i++) {
960 leaf = &surface->leaf[i];
961
962 if (leaf->busy)
963 bufs[i] = 'b';
964 else if (leaf->cairo_surface)
965 bufs[i] = 'a';
966 else
967 bufs[i] = ' ';
968 }
969
970 bufs[MAX_LEAVES] = '\0';
971 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
972#endif
973}
974
975static void
Pekka Paalanena4eda732012-11-19 17:16:02 +0200976shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
977{
Pekka Paalanen550d66b2013-02-13 16:17:12 +0200978 struct shm_surface *surface = data;
Pekka Paalanenaef02542013-04-25 13:57:47 +0300979 struct shm_surface_leaf *leaf;
980 int i;
981 int free_found;
Pekka Paalanen97777442013-05-22 10:20:05 +0300982
983 shm_surface_buffer_state_debug(surface, "buffer_release before");
Pekka Paalanena4eda732012-11-19 17:16:02 +0200984
Pekka Paalanenaef02542013-04-25 13:57:47 +0300985 for (i = 0; i < MAX_LEAVES; i++) {
986 leaf = &surface->leaf[i];
987 if (leaf->data && leaf->data->buffer == buffer) {
988 leaf->busy = 0;
989 break;
990 }
991 }
992 assert(i < MAX_LEAVES && "unknown buffer released");
Pekka Paalanen4dd0c412012-12-04 16:01:16 +0200993
Pekka Paalanenaef02542013-04-25 13:57:47 +0300994 /* Leave one free leaf with storage, release others */
995 free_found = 0;
996 for (i = 0; i < MAX_LEAVES; i++) {
997 leaf = &surface->leaf[i];
998
999 if (!leaf->cairo_surface || leaf->busy)
1000 continue;
1001
1002 if (!free_found)
1003 free_found = 1;
Pekka Paalanen97777442013-05-22 10:20:05 +03001004 else
Pekka Paalanenaef02542013-04-25 13:57:47 +03001005 shm_surface_leaf_release(leaf);
1006 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001007
Pekka Paalanen97777442013-05-22 10:20:05 +03001008 shm_surface_buffer_state_debug(surface, "buffer_release after");
Pekka Paalanena4eda732012-11-19 17:16:02 +02001009}
1010
1011static const struct wl_buffer_listener shm_surface_buffer_listener = {
1012 shm_surface_buffer_release
1013};
1014
Pekka Paalanen99436862012-11-19 17:15:59 +02001015static cairo_surface_t *
1016shm_surface_prepare(struct toysurface *base, int dx, int dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001017 int32_t width, int32_t height, uint32_t flags,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001018 enum wl_output_transform buffer_transform, int32_t buffer_scale)
Pekka Paalanen99436862012-11-19 17:15:59 +02001019{
Pekka Paalanenec076692012-11-30 13:37:27 +02001020 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
Pekka Paalanen99436862012-11-19 17:15:59 +02001021 struct shm_surface *surface = to_shm_surface(base);
Alexander Larsson1818e312013-05-22 14:41:31 +02001022 struct rectangle rect = { 0};
Pekka Paalanenaef02542013-04-25 13:57:47 +03001023 struct shm_surface_leaf *leaf = NULL;
1024 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001025
1026 surface->dx = dx;
1027 surface->dy = dy;
1028
Pekka Paalanenaef02542013-04-25 13:57:47 +03001029 /* pick a free buffer, preferrably one that already has storage */
1030 for (i = 0; i < MAX_LEAVES; i++) {
1031 if (surface->leaf[i].busy)
1032 continue;
Pekka Paalanen4dd0c412012-12-04 16:01:16 +02001033
Pekka Paalanenaef02542013-04-25 13:57:47 +03001034 if (!leaf || surface->leaf[i].cairo_surface)
1035 leaf = &surface->leaf[i];
1036 }
Pekka Paalanen71233882013-04-25 13:57:53 +03001037 DBG_OBJ(surface->surface, "pick leaf %d\n",
1038 (int)(leaf - &surface->leaf[0]));
1039
Pekka Paalanenaef02542013-04-25 13:57:47 +03001040 if (!leaf) {
1041 fprintf(stderr, "%s: all buffers are held by the server.\n",
Pekka Paalanena4eda732012-11-19 17:16:02 +02001042 __func__);
Pekka Paalanen71233882013-04-25 13:57:53 +03001043 exit(1);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001044 return NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001045 }
1046
Pekka Paalanena4eda732012-11-19 17:16:02 +02001047 if (!resize_hint && leaf->resize_pool) {
1048 cairo_surface_destroy(leaf->cairo_surface);
1049 leaf->cairo_surface = NULL;
1050 shm_pool_destroy(leaf->resize_pool);
1051 leaf->resize_pool = NULL;
1052 }
1053
Alexander Larsson1818e312013-05-22 14:41:31 +02001054 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1055
Pekka Paalanena4eda732012-11-19 17:16:02 +02001056 if (leaf->cairo_surface &&
1057 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1058 cairo_image_surface_get_height(leaf->cairo_surface) == height)
Pekka Paalanen99436862012-11-19 17:15:59 +02001059 goto out;
1060
Pekka Paalanena4eda732012-11-19 17:16:02 +02001061 if (leaf->cairo_surface)
1062 cairo_surface_destroy(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001063
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001064#ifdef USE_RESIZE_POOL
Pekka Paalanena4eda732012-11-19 17:16:02 +02001065 if (resize_hint && !leaf->resize_pool) {
Pekka Paalanen99436862012-11-19 17:15:59 +02001066 /* Create a big pool to allocate from, while continuously
1067 * resizing. Mmapping a new pool in the server
1068 * is relatively expensive, so reusing a pool performs
1069 * better, but may temporarily reserve unneeded memory.
1070 */
1071 /* We should probably base this number on the output size. */
Pekka Paalanena4eda732012-11-19 17:16:02 +02001072 leaf->resize_pool = shm_pool_create(surface->display,
1073 6 * 1024 * 1024);
Pekka Paalanen99436862012-11-19 17:15:59 +02001074 }
Louis-Francis Ratté-Boulianne6cd1de32013-05-22 18:03:11 +03001075#endif
Pekka Paalanen99436862012-11-19 17:15:59 +02001076
Alexander Larsson1818e312013-05-22 14:41:31 +02001077 rect.width = width;
1078 rect.height = height;
1079
Pekka Paalanena4eda732012-11-19 17:16:02 +02001080 leaf->cairo_surface =
Pekka Paalanen99436862012-11-19 17:15:59 +02001081 display_create_shm_surface(surface->display, &rect,
1082 surface->flags,
Pekka Paalanena4eda732012-11-19 17:16:02 +02001083 leaf->resize_pool,
1084 &leaf->data);
1085 wl_buffer_add_listener(leaf->data->buffer,
Pekka Paalanen550d66b2013-02-13 16:17:12 +02001086 &shm_surface_buffer_listener, surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001087
1088out:
Pekka Paalanena4eda732012-11-19 17:16:02 +02001089 surface->current = leaf;
1090
1091 return cairo_surface_reference(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001092}
1093
1094static void
1095shm_surface_swap(struct toysurface *base,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001096 enum wl_output_transform buffer_transform, int32_t buffer_scale,
Pekka Paalanen99436862012-11-19 17:15:59 +02001097 struct rectangle *server_allocation)
1098{
1099 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001100 struct shm_surface_leaf *leaf = surface->current;
Pekka Paalanen99436862012-11-19 17:15:59 +02001101
1102 server_allocation->width =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001103 cairo_image_surface_get_width(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001104 server_allocation->height =
Pekka Paalanena4eda732012-11-19 17:16:02 +02001105 cairo_image_surface_get_height(leaf->cairo_surface);
Pekka Paalanen99436862012-11-19 17:15:59 +02001106
Alexander Larsson1818e312013-05-22 14:41:31 +02001107 buffer_to_surface_size (buffer_transform, buffer_scale,
1108 &server_allocation->width,
1109 &server_allocation->height);
1110
Pekka Paalanena4eda732012-11-19 17:16:02 +02001111 wl_surface_attach(surface->surface, leaf->data->buffer,
Pekka Paalanen99436862012-11-19 17:15:59 +02001112 surface->dx, surface->dy);
1113 wl_surface_damage(surface->surface, 0, 0,
1114 server_allocation->width, server_allocation->height);
1115 wl_surface_commit(surface->surface);
Pekka Paalanena4eda732012-11-19 17:16:02 +02001116
Pekka Paalanen71233882013-04-25 13:57:53 +03001117 DBG_OBJ(surface->surface, "leaf %d busy\n",
1118 (int)(leaf - &surface->leaf[0]));
1119
Pekka Paalanena4eda732012-11-19 17:16:02 +02001120 leaf->busy = 1;
1121 surface->current = NULL;
Pekka Paalanen99436862012-11-19 17:15:59 +02001122}
1123
1124static int
1125shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1126{
1127 return -1;
1128}
1129
1130static void
1131shm_surface_release(struct toysurface *base)
1132{
1133}
1134
1135static void
1136shm_surface_destroy(struct toysurface *base)
1137{
1138 struct shm_surface *surface = to_shm_surface(base);
Pekka Paalanenaef02542013-04-25 13:57:47 +03001139 int i;
Pekka Paalanen99436862012-11-19 17:15:59 +02001140
Pekka Paalanenaef02542013-04-25 13:57:47 +03001141 for (i = 0; i < MAX_LEAVES; i++)
1142 shm_surface_leaf_release(&surface->leaf[i]);
Pekka Paalanen99436862012-11-19 17:15:59 +02001143
1144 free(surface);
1145}
1146
1147static struct toysurface *
1148shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1149 uint32_t flags, struct rectangle *rectangle)
1150{
1151 struct shm_surface *surface;
Pekka Paalanen71233882013-04-25 13:57:53 +03001152 DBG_OBJ(wl_surface, "\n");
Pekka Paalanen99436862012-11-19 17:15:59 +02001153
1154 surface = calloc(1, sizeof *surface);
1155 if (!surface)
1156 return NULL;
1157
1158 surface->base.prepare = shm_surface_prepare;
1159 surface->base.swap = shm_surface_swap;
1160 surface->base.acquire = shm_surface_acquire;
1161 surface->base.release = shm_surface_release;
1162 surface->base.destroy = shm_surface_destroy;
1163
1164 surface->display = display;
1165 surface->surface = wl_surface;
1166 surface->flags = flags;
Pekka Paalanen99436862012-11-19 17:15:59 +02001167
1168 return &surface->base;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001169}
1170
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001171/*
1172 * The following correspondences between file names and cursors was copied
1173 * from: https://bugs.kde.org/attachment.cgi?id=67313
1174 */
1175
1176static const char *bottom_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001177 "bottom_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001178 "sw-resize",
1179 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001180};
1181
1182static const char *bottom_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001183 "bottom_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001184 "se-resize",
1185 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001186};
1187
1188static const char *bottom_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001189 "bottom_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001190 "s-resize",
1191 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001192};
1193
1194static const char *grabbings[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001195 "grabbing",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001196 "closedhand",
1197 "208530c400c041818281048008011002"
1198};
1199
1200static const char *left_ptrs[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001201 "left_ptr",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001202 "default",
1203 "top_left_arrow",
1204 "left-arrow"
1205};
1206
1207static const char *left_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001208 "left_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001209 "w-resize",
1210 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001211};
1212
1213static const char *right_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001214 "right_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001215 "e-resize",
1216 "size_hor"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001217};
1218
1219static const char *top_left_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001220 "top_left_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001221 "nw-resize",
1222 "size_fdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001223};
1224
1225static const char *top_right_corners[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001226 "top_right_corner",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001227 "ne-resize",
1228 "size_bdiag"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001229};
1230
1231static const char *top_sides[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001232 "top_side",
Dima Ryazanovf6128fc2013-05-13 23:51:11 -07001233 "n-resize",
1234 "size_ver"
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001235};
1236
1237static const char *xterms[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001238 "xterm",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001239 "ibeam",
1240 "text"
1241};
1242
1243static const char *hand1s[] = {
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001244 "hand1",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001245 "pointer",
1246 "pointing_hand",
1247 "e29285e634086352946a0e7090d73106"
1248};
1249
1250static const char *watches[] = {
Kristian Høgsberg8591dbf2012-06-04 16:10:40 -04001251 "watch",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001252 "wait",
1253 "0426c94ea35c87780ff01dc239897213"
1254};
1255
1256struct cursor_alternatives {
1257 const char **names;
1258 size_t count;
1259};
1260
1261static const struct cursor_alternatives cursors[] = {
1262 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1263 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1264 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1265 {grabbings, ARRAY_LENGTH(grabbings)},
1266 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1267 {left_sides, ARRAY_LENGTH(left_sides)},
1268 {right_sides, ARRAY_LENGTH(right_sides)},
1269 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1270 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1271 {top_sides, ARRAY_LENGTH(top_sides)},
1272 {xterms, ARRAY_LENGTH(xterms)},
1273 {hand1s, ARRAY_LENGTH(hand1s)},
1274 {watches, ARRAY_LENGTH(watches)},
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001275};
1276
1277static void
1278create_cursors(struct display *display)
1279{
Ossama Othmana50e6e42013-05-14 09:48:26 -07001280 int config_fd;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001281 char *theme = NULL;
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001282 unsigned int size = 32;
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001283 unsigned int i, j;
1284 struct wl_cursor *cursor;
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001285 struct config_key shell_keys[] = {
1286 { "cursor-theme", CONFIG_KEY_STRING, &theme },
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001287 { "cursor-size", CONFIG_KEY_UNSIGNED_INTEGER, &size },
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001288 };
1289 struct config_section cs[] = {
1290 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1291 };
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001292
Ossama Othmana50e6e42013-05-14 09:48:26 -07001293 config_fd = open_config_file("weston.ini");
1294 parse_config_file(config_fd, cs, ARRAY_LENGTH(cs), NULL);
1295 close(config_fd);
Christopher Michaelac3e5f22012-08-11 15:12:09 +01001296
Emilio Pozuelo Monfortab44b0c2013-03-14 17:23:37 +01001297 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001298 display->cursors =
1299 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1300
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001301 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001302 cursor = NULL;
1303 for (j = 0; !cursor && j < cursors[i].count; ++j)
1304 cursor = wl_cursor_theme_get_cursor(
1305 display->cursor_theme, cursors[i].names[j]);
1306
1307 if (!cursor)
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001308 fprintf(stderr, "could not load cursor '%s'\n",
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001309 cursors[i].names[0]);
1310
1311 display->cursors[i] = cursor;
Pekka Paalanene288a0f2012-07-31 13:21:09 +03001312 }
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001313}
1314
1315static void
1316destroy_cursors(struct display *display)
1317{
1318 wl_cursor_theme_destroy(display->cursor_theme);
Yan Wanga261f7e2012-05-28 14:07:25 +08001319 free(display->cursors);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03001320}
1321
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001322struct wl_cursor_image *
1323display_get_pointer_image(struct display *display, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001324{
Philipp Brüschweilerbd3f2192012-08-21 20:36:16 +02001325 struct wl_cursor *cursor = display->cursors[pointer];
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001326
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03001327 return cursor ? cursor->images[0] : NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001328}
1329
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001330static void
Pekka Paalanen89dee002013-02-13 16:17:20 +02001331surface_flush(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001332{
Pekka Paalanen89dee002013-02-13 16:17:20 +02001333 if (!surface->cairo_surface)
1334 return;
1335
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001336 if (surface->opaque_region) {
1337 wl_surface_set_opaque_region(surface->surface,
1338 surface->opaque_region);
1339 wl_region_destroy(surface->opaque_region);
1340 surface->opaque_region = NULL;
1341 }
1342
1343 if (surface->input_region) {
1344 wl_surface_set_input_region(surface->surface,
1345 surface->input_region);
1346 wl_region_destroy(surface->input_region);
1347 surface->input_region = NULL;
1348 }
1349
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001350 surface->toysurface->swap(surface->toysurface,
Alexander Larsson1818e312013-05-22 14:41:31 +02001351 surface->buffer_transform, surface->buffer_scale,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001352 &surface->server_allocation);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001353
Pekka Paalanen89dee002013-02-13 16:17:20 +02001354 cairo_surface_destroy(surface->cairo_surface);
1355 surface->cairo_surface = NULL;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -05001356}
1357
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001358int
1359window_has_focus(struct window *window)
1360{
1361 return window->focus_count > 0;
1362}
1363
Pekka Paalanena8d4c842012-11-19 15:32:48 +02001364static void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001365window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001366{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001367 struct surface *surface;
1368
Pekka Paalanen89dee002013-02-13 16:17:20 +02001369 if (window->type == TYPE_NONE) {
1370 window->type = TYPE_TOPLEVEL;
1371 if (window->shell_surface)
1372 wl_shell_surface_set_toplevel(window->shell_surface);
1373 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001374
Pekka Paalanen35e82632013-04-25 13:57:48 +03001375 wl_list_for_each(surface, &window->subsurface_list, link) {
1376 if (surface == window->main_surface)
1377 continue;
1378
1379 surface_flush(surface);
1380 }
1381
Pekka Paalanen89dee002013-02-13 16:17:20 +02001382 surface_flush(window->main_surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -05001383}
1384
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -04001385struct display *
1386window_get_display(struct window *window)
1387{
1388 return window->display;
1389}
1390
Pekka Paalanen89dee002013-02-13 16:17:20 +02001391static void
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001392surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001393{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001394 struct display *display = surface->window->display;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001395 struct rectangle allocation = surface->allocation;
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001396
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001397 if (!surface->toysurface && display->dpy &&
1398 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001399 surface->toysurface =
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001400 egl_window_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001401 surface->surface,
Pekka Paalanen4e373742013-02-13 16:17:13 +02001402 flags,
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001403 &allocation);
Pekka Paalanenb3627362012-11-19 17:16:00 +02001404 }
Pekka Paalanen03fc3162012-11-19 17:15:58 +02001405
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001406 if (!surface->toysurface)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001407 surface->toysurface = shm_surface_create(display,
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001408 surface->surface,
1409 flags, &allocation);
Kristian Høgsberg012a0072010-10-26 00:02:20 -04001410
Pekka Paalanen89dee002013-02-13 16:17:20 +02001411 surface->cairo_surface = surface->toysurface->prepare(
1412 surface->toysurface, dx, dy,
Alexander Larsson1818e312013-05-22 14:41:31 +02001413 allocation.width, allocation.height, flags,
1414 surface->buffer_transform, surface->buffer_scale);
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001415}
1416
1417static void
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001418window_create_main_surface(struct window *window)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001419{
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001420 struct surface *surface = window->main_surface;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001421 uint32_t flags = 0;
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001422 int dx = 0;
1423 int dy = 0;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001424
Pekka Paalanenec076692012-11-30 13:37:27 +02001425 if (window->resizing)
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001426 flags |= SURFACE_HINT_RESIZE;
Pekka Paalanenec076692012-11-30 13:37:27 +02001427
Pekka Paalanen7bcfead2013-02-13 16:17:16 +02001428 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1429 dx = surface->server_allocation.width -
1430 surface->allocation.width;
1431
1432 if (window->resize_edges & WINDOW_RESIZING_TOP)
1433 dy = surface->server_allocation.height -
1434 surface->allocation.height;
1435
1436 window->resize_edges = 0;
1437
Pekka Paalanen89dee002013-02-13 16:17:20 +02001438 surface_create_surface(surface, dx, dy, flags);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001439}
1440
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001441int
1442window_get_buffer_transform(struct window *window)
1443{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001444 return window->main_surface->buffer_transform;
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001445}
1446
1447void
1448window_set_buffer_transform(struct window *window,
1449 enum wl_output_transform transform)
1450{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02001451 window->main_surface->buffer_transform = transform;
Pekka Paalanen4e373742013-02-13 16:17:13 +02001452 wl_surface_set_buffer_transform(window->main_surface->surface,
1453 transform);
Ander Conselvan de Oliveira6d4cb4e2012-11-30 17:34:24 +02001454}
1455
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001456void
1457window_set_buffer_scale(struct window *window,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001458 int32_t scale)
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001459{
1460 window->main_surface->buffer_scale = scale;
1461 wl_surface_set_buffer_scale(window->main_surface->surface,
1462 scale);
1463}
1464
1465uint32_t
1466window_get_buffer_scale(struct window *window)
1467{
1468 return window->main_surface->buffer_scale;
1469}
1470
Alexander Larssond68f5232013-05-22 14:41:33 +02001471uint32_t
1472window_get_output_scale(struct window *window)
1473{
1474 struct window_output *window_output;
1475 struct window_output *window_output_tmp;
1476 int scale = 1;
1477
1478 wl_list_for_each_safe(window_output, window_output_tmp,
1479 &window->window_output_list, link) {
1480 if (window_output->output->scale > scale)
1481 scale = window_output->output->scale;
1482 }
1483
1484 return scale;
1485}
1486
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001487static void frame_destroy(struct frame *frame);
1488
Pekka Paalanen4e373742013-02-13 16:17:13 +02001489static void
1490surface_destroy(struct surface *surface)
1491{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001492 if (surface->frame_cb)
1493 wl_callback_destroy(surface->frame_cb);
1494
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02001495 if (surface->input_region)
1496 wl_region_destroy(surface->input_region);
1497
1498 if (surface->opaque_region)
1499 wl_region_destroy(surface->opaque_region);
1500
Pekka Paalanen35e82632013-04-25 13:57:48 +03001501 if (surface->subsurface)
1502 wl_subsurface_destroy(surface->subsurface);
1503
Pekka Paalanen4e373742013-02-13 16:17:13 +02001504 wl_surface_destroy(surface->surface);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02001505
1506 if (surface->toysurface)
1507 surface->toysurface->destroy(surface->toysurface);
1508
Pekka Paalanen35e82632013-04-25 13:57:48 +03001509 wl_list_remove(&surface->link);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001510 free(surface);
1511}
1512
Kristian Høgsberge968f9c2010-08-27 22:18:00 -04001513void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001514window_destroy(struct window *window)
1515{
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001516 struct display *display = window->display;
1517 struct input *input;
Rob Bradford7507b572012-05-15 17:55:34 +01001518 struct window_output *window_output;
1519 struct window_output *window_output_tmp;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001520
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001521 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001522
1523 wl_list_for_each(input, &display->input_list, link) {
1524 if (input->pointer_focus == window)
1525 input->pointer_focus = NULL;
1526 if (input->keyboard_focus == window)
1527 input->keyboard_focus = NULL;
Kristian Høgsbergae6e2712012-01-26 11:09:20 -05001528 if (input->focus_widget &&
1529 input->focus_widget->window == window)
1530 input->focus_widget = NULL;
Pekka Paalanen77cbc952011-11-15 13:34:55 +02001531 }
1532
Rob Bradford7507b572012-05-15 17:55:34 +01001533 wl_list_for_each_safe(window_output, window_output_tmp,
1534 &window->window_output_list, link) {
1535 free (window_output);
1536 }
1537
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02001538 if (window->frame)
1539 frame_destroy(window->frame);
1540
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02001541 if (window->shell_surface)
1542 wl_shell_surface_destroy(window->shell_surface);
Pekka Paalanen4e373742013-02-13 16:17:13 +02001543
1544 surface_destroy(window->main_surface);
1545
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001546 wl_list_remove(&window->link);
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001547
Pekka Paalanen5ec65852011-12-16 10:09:29 +02001548 free(window->title);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001549 free(window);
1550}
1551
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001552static struct widget *
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001553widget_find_widget(struct widget *widget, int32_t x, int32_t y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001554{
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001555 struct widget *child, *target;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001556
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001557 wl_list_for_each(child, &widget->child_list, link) {
1558 target = widget_find_widget(child, x, y);
1559 if (target)
1560 return target;
1561 }
1562
1563 if (widget->allocation.x <= x &&
1564 x < widget->allocation.x + widget->allocation.width &&
1565 widget->allocation.y <= y &&
1566 y < widget->allocation.y + widget->allocation.height) {
1567 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001568 }
1569
1570 return NULL;
1571}
1572
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001573static struct widget *
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001574window_find_widget(struct window *window, int32_t x, int32_t y)
1575{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001576 struct surface *surface;
1577 struct widget *widget;
1578
1579 wl_list_for_each(surface, &window->subsurface_list, link) {
1580 widget = widget_find_widget(surface->widget, x, y);
1581 if (widget)
1582 return widget;
1583 }
1584
1585 return NULL;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001586}
1587
1588static struct widget *
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001589widget_create(struct window *window, struct surface *surface, void *data)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001590{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001591 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001592
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001593 widget = malloc(sizeof *widget);
1594 memset(widget, 0, sizeof *widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001595 widget->window = window;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001596 widget->surface = surface;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001597 widget->user_data = data;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001598 widget->allocation = surface->allocation;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001599 wl_list_init(&widget->child_list);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001600 widget->opaque = 0;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001601 widget->tooltip = NULL;
1602 widget->tooltip_count = 0;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001603 widget->default_cursor = CURSOR_LEFT_PTR;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001604
1605 return widget;
1606}
1607
1608struct widget *
1609window_add_widget(struct window *window, void *data)
1610{
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001611 struct widget *widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001612
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001613 widget = widget_create(window, window->main_surface, data);
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02001614 wl_list_init(&widget->link);
1615 window->main_surface->widget = widget;
1616
1617 return widget;
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001618}
1619
1620struct widget *
1621widget_add_widget(struct widget *parent, void *data)
1622{
1623 struct widget *widget;
1624
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02001625 widget = widget_create(parent->window, parent->surface, data);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001626 wl_list_insert(parent->child_list.prev, &widget->link);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001627
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001628 return widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001629}
1630
1631void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001632widget_destroy(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001633{
Pekka Paalanene156fb62012-01-19 13:51:38 +02001634 struct display *display = widget->window->display;
Pekka Paalanen35e82632013-04-25 13:57:48 +03001635 struct surface *surface = widget->surface;
Pekka Paalanene156fb62012-01-19 13:51:38 +02001636 struct input *input;
1637
Pekka Paalanen35e82632013-04-25 13:57:48 +03001638 /* Destroy the sub-surface along with the root widget */
1639 if (surface->widget == widget && surface->subsurface)
1640 surface_destroy(widget->surface);
1641
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001642 if (widget->tooltip) {
1643 free(widget->tooltip);
1644 widget->tooltip = NULL;
1645 }
1646
Pekka Paalanene156fb62012-01-19 13:51:38 +02001647 wl_list_for_each(input, &display->input_list, link) {
1648 if (input->focus_widget == widget)
1649 input->focus_widget = NULL;
1650 }
1651
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001652 wl_list_remove(&widget->link);
1653 free(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001654}
1655
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001656void
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05001657widget_set_default_cursor(struct widget *widget, int cursor)
1658{
1659 widget->default_cursor = cursor;
1660}
1661
1662void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001663widget_get_allocation(struct widget *widget, struct rectangle *allocation)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001664{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001665 *allocation = widget->allocation;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001666}
1667
1668void
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001669widget_set_size(struct widget *widget, int32_t width, int32_t height)
1670{
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001671 widget->allocation.width = width;
1672 widget->allocation.height = height;
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001673}
1674
1675void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001676widget_set_allocation(struct widget *widget,
1677 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001678{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001679 widget->allocation.x = x;
1680 widget->allocation.y = y;
Tiago Vignattic5528d82012-02-09 19:06:55 +02001681 widget_set_size(widget, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001682}
1683
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001684void
1685widget_set_transparent(struct widget *widget, int transparent)
1686{
1687 widget->opaque = !transparent;
1688}
1689
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001690void *
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001691widget_get_user_data(struct widget *widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001692{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05001693 return widget->user_data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04001694}
1695
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001696static cairo_surface_t *
1697widget_get_cairo_surface(struct widget *widget)
1698{
1699 struct surface *surface = widget->surface;
1700 struct window *window = widget->window;
1701
1702 if (!surface->cairo_surface) {
1703 if (surface == window->main_surface)
1704 window_create_main_surface(window);
1705 else
1706 surface_create_surface(surface, 0, 0, 0);
1707 }
1708
1709 return surface->cairo_surface;
1710}
1711
Alexander Larsson15901f02013-05-22 14:41:25 +02001712static void
1713widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1714{
1715 struct surface *surface = widget->surface;
1716 double angle;
1717 cairo_matrix_t m;
1718 enum wl_output_transform transform;
1719 int surface_width, surface_height;
1720 int translate_x, translate_y;
Alexander Larssonedddbd12013-05-24 13:09:43 +02001721 int32_t scale;
Alexander Larsson15901f02013-05-22 14:41:25 +02001722
1723 surface_width = surface->allocation.width;
1724 surface_height = surface->allocation.height;
1725
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001726 transform = surface->buffer_transform;
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001727 scale = surface->buffer_scale;
Alexander Larsson5e9b6522013-05-22 14:41:28 +02001728
Alexander Larsson15901f02013-05-22 14:41:25 +02001729 switch (transform) {
1730 case WL_OUTPUT_TRANSFORM_FLIPPED:
1731 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1732 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1733 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1734 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1735 break;
1736 default:
1737 cairo_matrix_init_identity(&m);
1738 break;
1739 }
1740
1741 switch (transform) {
1742 case WL_OUTPUT_TRANSFORM_NORMAL:
1743 default:
1744 angle = 0;
1745 translate_x = 0;
1746 translate_y = 0;
1747 break;
1748 case WL_OUTPUT_TRANSFORM_FLIPPED:
1749 angle = 0;
1750 translate_x = surface_width;
1751 translate_y = 0;
1752 break;
1753 case WL_OUTPUT_TRANSFORM_90:
1754 angle = M_PI_2;
1755 translate_x = surface_height;
1756 translate_y = 0;
1757 break;
1758 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1759 angle = M_PI_2;
1760 translate_x = surface_height;
1761 translate_y = surface_width;
1762 break;
1763 case WL_OUTPUT_TRANSFORM_180:
1764 angle = M_PI;
1765 translate_x = surface_width;
1766 translate_y = surface_height;
1767 break;
1768 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1769 angle = M_PI;
1770 translate_x = 0;
1771 translate_y = surface_height;
1772 break;
1773 case WL_OUTPUT_TRANSFORM_270:
1774 angle = M_PI + M_PI_2;
1775 translate_x = 0;
1776 translate_y = surface_width;
1777 break;
1778 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1779 angle = M_PI + M_PI_2;
1780 translate_x = 0;
1781 translate_y = 0;
1782 break;
1783 }
1784
Alexander Larsson2aaa8b72013-05-22 14:41:29 +02001785 cairo_scale(cr, scale, scale);
Alexander Larsson15901f02013-05-22 14:41:25 +02001786 cairo_translate(cr, translate_x, translate_y);
1787 cairo_rotate(cr, angle);
1788 cairo_transform(cr, &m);
1789}
1790
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001791cairo_t *
1792widget_cairo_create(struct widget *widget)
1793{
Pekka Paalanen35e82632013-04-25 13:57:48 +03001794 struct surface *surface = widget->surface;
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001795 cairo_surface_t *cairo_surface;
1796 cairo_t *cr;
1797
1798 cairo_surface = widget_get_cairo_surface(widget);
1799 cr = cairo_create(cairo_surface);
1800
Alexander Larsson15901f02013-05-22 14:41:25 +02001801 widget_cairo_update_transform(widget, cr);
1802
Pekka Paalanen35e82632013-04-25 13:57:48 +03001803 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1804
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001805 return cr;
1806}
1807
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03001808struct wl_surface *
1809widget_get_wl_surface(struct widget *widget)
1810{
1811 return widget->surface->surface;
1812}
1813
1814uint32_t
1815widget_get_last_time(struct widget *widget)
1816{
1817 return widget->surface->last_time;
1818}
1819
1820void
1821widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1822{
1823 struct wl_compositor *comp = widget->window->display->compositor;
1824 struct surface *surface = widget->surface;
1825
1826 if (!surface->input_region)
1827 surface->input_region = wl_compositor_create_region(comp);
1828
1829 if (rect) {
1830 wl_region_add(surface->input_region,
1831 rect->x, rect->y, rect->width, rect->height);
1832 }
1833}
1834
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001835void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001836widget_set_resize_handler(struct widget *widget,
1837 widget_resize_handler_t handler)
1838{
1839 widget->resize_handler = handler;
1840}
1841
1842void
1843widget_set_redraw_handler(struct widget *widget,
1844 widget_redraw_handler_t handler)
1845{
1846 widget->redraw_handler = handler;
1847}
1848
1849void
Kristian Høgsbergee143232012-01-09 08:42:24 -05001850widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001851{
Kristian Høgsbergee143232012-01-09 08:42:24 -05001852 widget->enter_handler = handler;
1853}
1854
1855void
1856widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1857{
1858 widget->leave_handler = handler;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001859}
1860
1861void
Kristian Høgsberg04e98342012-01-09 09:36:16 -05001862widget_set_motion_handler(struct widget *widget,
1863 widget_motion_handler_t handler)
1864{
1865 widget->motion_handler = handler;
1866}
1867
1868void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001869widget_set_button_handler(struct widget *widget,
1870 widget_button_handler_t handler)
1871{
1872 widget->button_handler = handler;
1873}
1874
1875void
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001876widget_set_axis_handler(struct widget *widget,
1877 widget_axis_handler_t handler)
1878{
1879 widget->axis_handler = handler;
1880}
1881
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001882static void
1883window_schedule_redraw_task(struct window *window);
1884
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02001885void
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001886widget_schedule_redraw(struct widget *widget)
1887{
Pekka Paalanen71233882013-04-25 13:57:53 +03001888 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03001889 widget->surface->redraw_needed = 1;
1890 window_schedule_redraw_task(widget->window);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -05001891}
1892
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001893cairo_surface_t *
1894window_get_surface(struct window *window)
1895{
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001896 cairo_surface_t *cairo_surface;
1897
1898 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1899
1900 return cairo_surface_reference(cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001901}
1902
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001903struct wl_surface *
1904window_get_wl_surface(struct window *window)
1905{
Pekka Paalanen4e373742013-02-13 16:17:13 +02001906 return window->main_surface->surface;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001907}
1908
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001909struct wl_shell_surface *
1910window_get_wl_shell_surface(struct window *window)
1911{
1912 return window->shell_surface;
1913}
1914
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05001915static void
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001916tooltip_redraw_handler(struct widget *widget, void *data)
1917{
1918 cairo_t *cr;
1919 const int32_t r = 3;
1920 struct tooltip *tooltip = data;
1921 int32_t width, height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001922
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02001923 cr = widget_cairo_create(widget);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001924 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1925 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1926 cairo_paint(cr);
1927
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02001928 width = widget->allocation.width;
1929 height = widget->allocation.height;
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001930 rounded_rect(cr, 0, 0, width, height, r);
1931
1932 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1933 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1934 cairo_fill(cr);
1935
1936 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1937 cairo_move_to(cr, 10, 16);
1938 cairo_show_text(cr, tooltip->entry);
1939 cairo_destroy(cr);
1940}
1941
1942static cairo_text_extents_t
1943get_text_extents(struct tooltip *tooltip)
1944{
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001945 cairo_t *cr;
1946 cairo_text_extents_t extents;
1947
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02001948 /* Use the dummy_surface because tooltip's surface was not
1949 * created yet, and parent does not have a valid surface
1950 * outside repaint, either.
1951 */
1952 cr = cairo_create(tooltip->window->display->dummy_surface);
Tiago Vignatti82db9d82012-05-23 22:06:27 +03001953 cairo_text_extents(cr, tooltip->entry, &extents);
1954 cairo_destroy(cr);
1955
1956 return extents;
1957}
1958
1959static int
1960window_create_tooltip(struct tooltip *tooltip)
1961{
1962 struct widget *parent = tooltip->parent;
1963 struct display *display = parent->window->display;
1964 struct window *window;
1965 const int offset_y = 27;
1966 const int margin = 3;
1967 cairo_text_extents_t extents;
1968
1969 if (tooltip->widget)
1970 return 0;
1971
1972 window = window_create_transient(display, parent->window, tooltip->x,
1973 tooltip->y + offset_y,
1974 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1975 if (!window)
1976 return -1;
1977
1978 tooltip->window = window;
1979 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1980
1981 extents = get_text_extents(tooltip);
1982 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1983 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1984
1985 return 0;
1986}
1987
1988void
1989widget_destroy_tooltip(struct widget *parent)
1990{
1991 struct tooltip *tooltip = parent->tooltip;
1992
1993 parent->tooltip_count = 0;
1994 if (!tooltip)
1995 return;
1996
1997 if (tooltip->widget) {
1998 widget_destroy(tooltip->widget);
1999 window_destroy(tooltip->window);
2000 tooltip->widget = NULL;
2001 tooltip->window = NULL;
2002 }
2003
2004 close(tooltip->tooltip_fd);
2005 free(tooltip->entry);
2006 free(tooltip);
2007 parent->tooltip = NULL;
2008}
2009
2010static void
2011tooltip_func(struct task *task, uint32_t events)
2012{
2013 struct tooltip *tooltip =
2014 container_of(task, struct tooltip, tooltip_task);
2015 uint64_t exp;
2016
Martin Olsson8df662a2012-07-08 03:03:47 +02002017 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2018 abort();
Tiago Vignatti82db9d82012-05-23 22:06:27 +03002019 window_create_tooltip(tooltip);
2020}
2021
2022#define TOOLTIP_TIMEOUT 500
2023static int
2024tooltip_timer_reset(struct tooltip *tooltip)
2025{
2026 struct itimerspec its;
2027
2028 its.it_interval.tv_sec = 0;
2029 its.it_interval.tv_nsec = 0;
2030 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2031 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2032 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2033 fprintf(stderr, "could not set timerfd\n: %m");
2034 return -1;
2035 }
2036
2037 return 0;
2038}
2039
2040int
2041widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2042{
2043 struct tooltip *tooltip = parent->tooltip;
2044
2045 parent->tooltip_count++;
2046 if (tooltip) {
2047 tooltip->x = x;
2048 tooltip->y = y;
2049 tooltip_timer_reset(tooltip);
2050 return 0;
2051 }
2052
2053 /* the handler might be triggered too fast via input device motion, so
2054 * we need this check here to make sure tooltip is fully initialized */
2055 if (parent->tooltip_count > 1)
2056 return 0;
2057
2058 tooltip = malloc(sizeof *tooltip);
2059 if (!tooltip)
2060 return -1;
2061
2062 parent->tooltip = tooltip;
2063 tooltip->parent = parent;
2064 tooltip->widget = NULL;
2065 tooltip->window = NULL;
2066 tooltip->x = x;
2067 tooltip->y = y;
2068 tooltip->entry = strdup(entry);
2069 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2070 if (tooltip->tooltip_fd < 0) {
2071 fprintf(stderr, "could not create timerfd\n: %m");
2072 return -1;
2073 }
2074
2075 tooltip->tooltip_task.run = tooltip_func;
2076 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2077 EPOLLIN, &tooltip->tooltip_task);
2078 tooltip_timer_reset(tooltip);
2079
2080 return 0;
2081}
2082
2083static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002084workspace_manager_state(void *data,
2085 struct workspace_manager *workspace_manager,
2086 uint32_t current,
2087 uint32_t count)
2088{
2089 struct display *display = data;
2090
2091 display->workspace = current;
2092 display->workspace_count = count;
2093}
2094
2095static const struct workspace_manager_listener workspace_manager_listener = {
2096 workspace_manager_state
2097};
2098
2099static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002100frame_resize_handler(struct widget *widget,
2101 int32_t width, int32_t height, void *data)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002102{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002103 struct frame *frame = data;
2104 struct widget *child = frame->child;
2105 struct rectangle allocation;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002106 struct display *display = widget->window->display;
Pekka Paalanen2d8a32a2013-02-13 16:17:19 +02002107 struct surface *surface = widget->surface;
Martin Minarik1998b152012-05-10 02:04:35 +02002108 struct frame_button * button;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002109 struct theme *t = display->theme;
Martin Minarik1998b152012-05-10 02:04:35 +02002110 int x_l, x_r, y, w, h;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002111 int decoration_width, decoration_height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002112 int opaque_margin, shadow_margin;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002113
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002114 switch (widget->window->type) {
2115 case TYPE_FULLSCREEN:
2116 decoration_width = 0;
2117 decoration_height = 0;
2118
2119 allocation.x = 0;
2120 allocation.y = 0;
2121 allocation.width = width;
2122 allocation.height = height;
2123 opaque_margin = 0;
2124
2125 wl_list_for_each(button, &frame->buttons_list, link)
2126 button->widget->opaque = 1;
2127 break;
2128 case TYPE_MAXIMIZED:
2129 decoration_width = t->width * 2;
2130 decoration_height = t->width + t->titlebar_height;
2131
2132 allocation.x = t->width;
2133 allocation.y = t->titlebar_height;
2134 allocation.width = width - decoration_width;
2135 allocation.height = height - decoration_height;
2136
2137 opaque_margin = 0;
2138
2139 wl_list_for_each(button, &frame->buttons_list, link)
2140 button->widget->opaque = 0;
2141 break;
2142 default:
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002143 decoration_width = (t->width + t->margin) * 2;
2144 decoration_height = t->width +
2145 t->titlebar_height + t->margin * 2;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002146
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002147 allocation.x = t->width + t->margin;
2148 allocation.y = t->titlebar_height + t->margin;
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002149 allocation.width = width - decoration_width;
2150 allocation.height = height - decoration_height;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002151
Kristian Høgsberg291c69c2012-05-15 22:12:54 -04002152 opaque_margin = t->margin + t->frame_radius;
Martin Minarik1998b152012-05-10 02:04:35 +02002153
2154 wl_list_for_each(button, &frame->buttons_list, link)
2155 button->widget->opaque = 0;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002156 break;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002157 }
2158
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002159 widget_set_allocation(child, allocation.x, allocation.y,
2160 allocation.width, allocation.height);
2161
2162 if (child->resize_handler)
2163 child->resize_handler(child,
2164 allocation.width,
2165 allocation.height,
2166 child->user_data);
2167
Scott Moreauf7e498c2012-05-14 11:39:29 -06002168 width = child->allocation.width + decoration_width;
2169 height = child->allocation.height + decoration_height;
2170
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002171 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2172
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002173 surface->input_region =
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04002174 wl_compositor_create_region(display->compositor);
Kristian Høgsberg023be102012-07-31 11:59:12 -04002175 if (widget->window->type != TYPE_FULLSCREEN) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002176 wl_region_add(surface->input_region,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002177 shadow_margin, shadow_margin,
2178 width - 2 * shadow_margin,
2179 height - 2 * shadow_margin);
Kristian Høgsberg3fb613e2012-10-15 15:01:24 -04002180 } else {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002181 wl_region_add(surface->input_region, 0, 0, width, height);
Kristian Høgsberg023be102012-07-31 11:59:12 -04002182 }
2183
Scott Moreauf7e498c2012-05-14 11:39:29 -06002184 widget_set_allocation(widget, 0, 0, width, height);
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002185
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02002186 if (child->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02002187 wl_region_add(surface->opaque_region,
Kristian Høgsbergf10df852012-02-28 21:52:12 -05002188 opaque_margin, opaque_margin,
2189 widget->allocation.width - 2 * opaque_margin,
2190 widget->allocation.height - 2 * opaque_margin);
Martin Minarik1998b152012-05-10 02:04:35 +02002191
2192 /* frame internal buttons */
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002193 x_r = frame->widget->allocation.width - t->width - shadow_margin;
2194 x_l = t->width + shadow_margin;
2195 y = t->width + shadow_margin;
Martin Minarik1998b152012-05-10 02:04:35 +02002196 wl_list_for_each(button, &frame->buttons_list, link) {
2197 const int button_padding = 4;
2198 w = cairo_image_surface_get_width(button->icon);
2199 h = cairo_image_surface_get_height(button->icon);
2200
2201 if (button->decoration == FRAME_BUTTON_FANCY)
2202 w += 10;
2203
2204 if (button->align == FRAME_BUTTON_LEFT) {
2205 widget_set_allocation(button->widget,
2206 x_l, y , w + 1, h + 1);
2207 x_l += w;
2208 x_l += button_padding;
2209 } else {
2210 x_r -= w;
2211 widget_set_allocation(button->widget,
2212 x_r, y , w + 1, h + 1);
2213 x_r -= button_padding;
2214 }
2215 }
2216}
2217
2218static int
2219frame_button_enter_handler(struct widget *widget,
2220 struct input *input, float x, float y, void *data)
2221{
2222 struct frame_button *frame_button = data;
2223
2224 widget_schedule_redraw(frame_button->widget);
2225 frame_button->state = FRAME_BUTTON_OVER;
2226
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002227 return CURSOR_LEFT_PTR;
Martin Minarik1998b152012-05-10 02:04:35 +02002228}
2229
2230static void
2231frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
2232{
2233 struct frame_button *frame_button = data;
2234
2235 widget_schedule_redraw(frame_button->widget);
2236 frame_button->state = FRAME_BUTTON_DEFAULT;
2237}
2238
2239static void
2240frame_button_button_handler(struct widget *widget,
2241 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002242 uint32_t button,
2243 enum wl_pointer_button_state state, void *data)
Martin Minarik1998b152012-05-10 02:04:35 +02002244{
2245 struct frame_button *frame_button = data;
2246 struct window *window = widget->window;
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002247 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
Martin Minarik1998b152012-05-10 02:04:35 +02002248
2249 if (button != BTN_LEFT)
2250 return;
2251
2252 switch (state) {
Daniel Stone4dbadb12012-05-30 16:31:51 +01002253 case WL_POINTER_BUTTON_STATE_PRESSED:
Martin Minarik1998b152012-05-10 02:04:35 +02002254 frame_button->state = FRAME_BUTTON_ACTIVE;
2255 widget_schedule_redraw(frame_button->widget);
2256
2257 if (frame_button->type == FRAME_BUTTON_ICON)
2258 window_show_frame_menu(window, input, time);
2259 return;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002260 case WL_POINTER_BUTTON_STATE_RELEASED:
Martin Minarik1998b152012-05-10 02:04:35 +02002261 frame_button->state = FRAME_BUTTON_DEFAULT;
2262 widget_schedule_redraw(frame_button->widget);
2263 break;
2264 }
2265
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002266 if (!was_pressed)
2267 return;
2268
Martin Minarik1998b152012-05-10 02:04:35 +02002269 switch (frame_button->type) {
2270 case FRAME_BUTTON_CLOSE:
2271 if (window->close_handler)
2272 window->close_handler(window->parent,
2273 window->user_data);
2274 else
2275 display_exit(window->display);
2276 break;
2277 case FRAME_BUTTON_MINIMIZE:
2278 fprintf(stderr,"Minimize stub\n");
2279 break;
2280 case FRAME_BUTTON_MAXIMIZE:
2281 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2282 break;
2283 default:
2284 /* Unknown operation */
2285 break;
2286 }
2287}
2288
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002289static int
2290frame_button_motion_handler(struct widget *widget,
2291 struct input *input, uint32_t time,
2292 float x, float y, void *data)
2293{
2294 struct frame_button *frame_button = data;
2295 enum frame_button_pointer previous_button_state = frame_button->state;
2296
2297 /* only track state for a pressed button */
2298 if (input->grab != widget)
2299 return CURSOR_LEFT_PTR;
2300
2301 if (x > widget->allocation.x &&
2302 x < (widget->allocation.x + widget->allocation.width) &&
2303 y > widget->allocation.y &&
2304 y < (widget->allocation.y + widget->allocation.height)) {
2305 frame_button->state = FRAME_BUTTON_ACTIVE;
2306 } else {
2307 frame_button->state = FRAME_BUTTON_DEFAULT;
2308 }
2309
2310 if (frame_button->state != previous_button_state)
2311 widget_schedule_redraw(frame_button->widget);
2312
2313 return CURSOR_LEFT_PTR;
2314}
2315
Martin Minarik1998b152012-05-10 02:04:35 +02002316static void
2317frame_button_redraw_handler(struct widget *widget, void *data)
2318{
2319 struct frame_button *frame_button = data;
2320 cairo_t *cr;
2321 int width, height, x, y;
Martin Minarik1998b152012-05-10 02:04:35 +02002322
2323 x = widget->allocation.x;
2324 y = widget->allocation.y;
2325 width = widget->allocation.width;
2326 height = widget->allocation.height;
2327
2328 if (!width)
2329 return;
2330 if (!height)
2331 return;
2332 if (widget->opaque)
2333 return;
2334
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002335 cr = widget_cairo_create(widget);
Martin Minarik1998b152012-05-10 02:04:35 +02002336
2337 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
2338 cairo_set_line_width(cr, 1);
2339
2340 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2341 cairo_rectangle (cr, x, y, 25, 16);
2342
2343 cairo_stroke_preserve(cr);
2344
2345 switch (frame_button->state) {
2346 case FRAME_BUTTON_DEFAULT:
2347 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
2348 break;
2349 case FRAME_BUTTON_OVER:
2350 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2351 break;
2352 case FRAME_BUTTON_ACTIVE:
2353 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
2354 break;
2355 }
2356
2357 cairo_fill (cr);
2358
2359 x += 4;
2360 }
2361
2362 cairo_set_source_surface(cr, frame_button->icon, x, y);
2363 cairo_paint(cr);
2364
2365 cairo_destroy(cr);
2366}
2367
2368static struct widget *
2369frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2370 enum frame_button_align align, enum frame_button_decoration style)
2371{
2372 struct frame_button *frame_button;
2373 const char *icon = data;
2374
2375 frame_button = malloc (sizeof *frame_button);
2376 memset(frame_button, 0, sizeof *frame_button);
2377
2378 frame_button->icon = cairo_image_surface_create_from_png(icon);
2379 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2380 frame_button->frame = frame;
2381 frame_button->type = type;
2382 frame_button->align = align;
2383 frame_button->decoration = style;
2384
2385 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2386
2387 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2388 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2389 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2390 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
Pekka Vuorela4e363d22012-09-26 15:05:45 +03002391 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
Martin Minarik1998b152012-05-10 02:04:35 +02002392 return frame_button->widget;
2393}
2394
2395static void
2396frame_button_destroy(struct frame_button *frame_button)
2397{
2398 widget_destroy(frame_button->widget);
2399 wl_list_remove(&frame_button->link);
2400 cairo_surface_destroy(frame_button->icon);
2401 free(frame_button);
2402
2403 return;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002404}
2405
2406static void
2407frame_redraw_handler(struct widget *widget, void *data)
2408{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002409 cairo_t *cr;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002410 struct window *window = widget->window;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002411 struct theme *t = window->display->theme;
2412 uint32_t flags = 0;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002413
Kristian Høgsberg2675dc12012-02-16 22:57:21 -05002414 if (window->type == TYPE_FULLSCREEN)
2415 return;
2416
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02002417 cr = widget_cairo_create(widget);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002418
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002419 if (window->focus_count)
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002420 flags |= THEME_FRAME_ACTIVE;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002421 if (window->type == TYPE_MAXIMIZED)
2422 flags |= THEME_FRAME_MAXIMIZED;
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04002423 theme_render_frame(t, cr, widget->allocation.width,
2424 widget->allocation.height, window->title, flags);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002425
2426 cairo_destroy(cr);
2427}
2428
2429static int
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002430frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002431{
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002432 struct theme *t = frame->widget->window->display->theme;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002433 struct window *window = frame->widget->window;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002434 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002435
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002436 if (window->type != TYPE_TOPLEVEL)
2437 return CURSOR_LEFT_PTR;
2438
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002439 location = theme_get_location(t, input->sx, input->sy,
2440 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002441 frame->widget->allocation.height,
2442 window->type == TYPE_MAXIMIZED ?
2443 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002444
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002445 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002446 case THEME_LOCATION_RESIZING_TOP:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002447 return CURSOR_TOP;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002448 case THEME_LOCATION_RESIZING_BOTTOM:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002449 return CURSOR_BOTTOM;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002450 case THEME_LOCATION_RESIZING_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002451 return CURSOR_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002452 case THEME_LOCATION_RESIZING_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002453 return CURSOR_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002454 case THEME_LOCATION_RESIZING_TOP_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002455 return CURSOR_TOP_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002456 case THEME_LOCATION_RESIZING_TOP_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002457 return CURSOR_TOP_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002458 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002459 return CURSOR_BOTTOM_LEFT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002460 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002461 return CURSOR_BOTTOM_RIGHT;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002462 case THEME_LOCATION_EXTERIOR:
2463 case THEME_LOCATION_TITLEBAR:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002464 default:
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002465 return CURSOR_LEFT_PTR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002466 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002467}
2468
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002469static void
2470frame_menu_func(struct window *window, int index, void *data)
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002471{
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002472 struct display *display;
2473
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002474 switch (index) {
2475 case 0: /* close */
2476 if (window->close_handler)
2477 window->close_handler(window->parent,
2478 window->user_data);
2479 else
2480 display_exit(window->display);
2481 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002482 case 1: /* move to workspace above */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002483 display = window->display;
2484 if (display->workspace > 0)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002485 workspace_manager_move_surface(
2486 display->workspace_manager,
2487 window->main_surface->surface,
2488 display->workspace - 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002489 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002490 case 2: /* move to workspace below */
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02002491 display = window->display;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +02002492 if (display->workspace < display->workspace_count - 1)
Pekka Paalanen4e373742013-02-13 16:17:13 +02002493 workspace_manager_move_surface(
2494 display->workspace_manager,
2495 window->main_surface->surface,
2496 display->workspace + 1);
Kristian Høgsberg0f7a2852012-11-05 20:20:53 -05002497 break;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002498 case 3: /* fullscreen */
2499 /* we don't have a way to get out of fullscreen for now */
2500 if (window->fullscreen_handler)
2501 window->fullscreen_handler(window, window->user_data);
2502 break;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002503 }
2504}
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002505
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002506void
2507window_show_frame_menu(struct window *window,
2508 struct input *input, uint32_t time)
2509{
2510 int32_t x, y;
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002511 int count;
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002512
2513 static const char *entries[] = {
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002514 "Close",
2515 "Move to workspace above", "Move to workspace below",
2516 "Fullscreen"
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002517 };
2518
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002519 if (window->fullscreen_handler)
2520 count = ARRAY_LENGTH(entries);
2521 else
2522 count = ARRAY_LENGTH(entries) - 1;
2523
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002524 input_get_position(input, &x, &y);
2525 window_show_menu(window->display, input, time, window,
Kristian Høgsbergefb94882012-10-30 18:07:02 -04002526 x - 10, y - 10, frame_menu_func, entries, count);
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002527}
2528
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002529static int
2530frame_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002531 struct input *input, float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002532{
2533 return frame_get_pointer_image_for_location(data, input);
2534}
Kristian Høgsberg7d804062010-09-07 21:50:06 -04002535
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002536static int
2537frame_motion_handler(struct widget *widget,
2538 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002539 float x, float y, void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002540{
2541 return frame_get_pointer_image_for_location(data, input);
2542}
Rob Bradford8bd35c72011-10-25 12:20:51 +01002543
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002544static void
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002545frame_button_handler(struct widget *widget,
2546 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002547 uint32_t button, enum wl_pointer_button_state state,
2548 void *data)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002549
2550{
2551 struct frame *frame = data;
2552 struct window *window = widget->window;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002553 struct display *display = window->display;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002554 int location;
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002555
Rob Bradford3c9d9672013-07-11 10:35:55 +01002556 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsberge994edd2013-02-14 16:31:42 -05002557 return;
2558
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002559 location = theme_get_location(display->theme, input->sx, input->sy,
2560 frame->widget->allocation.width,
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002561 frame->widget->allocation.height,
2562 window->type == TYPE_MAXIMIZED ?
2563 THEME_FRAME_MAXIMIZED : 0);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002564
Daniel Stone4dbadb12012-05-30 16:31:51 +01002565 if (window->display->shell && button == BTN_LEFT &&
Rob Bradford3c9d9672013-07-11 10:35:55 +01002566 window->type == TYPE_TOPLEVEL) {
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002567 switch (location) {
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002568 case THEME_LOCATION_TITLEBAR:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002569 if (!window->shell_surface)
2570 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002571 input_ungrab(input);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002572 wl_shell_surface_move(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002573 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002574 display->serial);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002575 break;
Kristian Høgsbergf96e6c02012-05-22 16:38:53 -04002576 case THEME_LOCATION_RESIZING_TOP:
2577 case THEME_LOCATION_RESIZING_BOTTOM:
2578 case THEME_LOCATION_RESIZING_LEFT:
2579 case THEME_LOCATION_RESIZING_RIGHT:
2580 case THEME_LOCATION_RESIZING_TOP_LEFT:
2581 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2582 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2583 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002584 if (!window->shell_surface)
2585 break;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002586 input_ungrab(input);
Kristian Høgsberg1103a1a2012-04-03 12:00:48 -04002587
Pekka Paalanen99436862012-11-19 17:15:59 +02002588 window->resizing = 1;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002589 wl_shell_surface_resize(window->shell_surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002590 input_get_seat(input),
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002591 display->serial, location);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002592 break;
2593 }
Daniel Stone4dbadb12012-05-30 16:31:51 +01002594 } else if (button == BTN_RIGHT &&
Rob Bradford3c9d9672013-07-11 10:35:55 +01002595 (window->type == TYPE_TOPLEVEL ||
2596 window->type == TYPE_MAXIMIZED)) {
Kristian Høgsbergd31fcab2012-01-31 09:53:44 -05002597 window_show_frame_menu(window, input, time);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002598 }
2599}
2600
2601struct widget *
2602frame_create(struct window *window, void *data)
2603{
2604 struct frame *frame;
2605
Kristian Høgsberg8eeb30b2013-07-25 16:25:15 -07002606 frame = xmalloc(sizeof *frame);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002607 memset(frame, 0, sizeof *frame);
2608
2609 frame->widget = window_add_widget(window, frame);
2610 frame->child = widget_add_widget(frame->widget, data);
Martin Minarik1998b152012-05-10 02:04:35 +02002611
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002612 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2613 widget_set_resize_handler(frame->widget, frame_resize_handler);
2614 widget_set_enter_handler(frame->widget, frame_enter_handler);
2615 widget_set_motion_handler(frame->widget, frame_motion_handler);
2616 widget_set_button_handler(frame->widget, frame_button_handler);
2617
Martin Minarik1998b152012-05-10 02:04:35 +02002618 /* Create empty list for frame buttons */
2619 wl_list_init(&frame->buttons_list);
2620
2621 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2622 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2623
2624 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2625 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2626
2627 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2628 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2629
2630 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2631 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2632
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002633 window->frame = frame;
2634
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002635 return frame->child;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002636}
2637
Kristian Høgsberga1627922012-06-20 17:30:03 -04002638void
2639frame_set_child_size(struct widget *widget, int child_width, int child_height)
2640{
2641 struct display *display = widget->window->display;
2642 struct theme *t = display->theme;
2643 int decoration_width, decoration_height;
2644 int width, height;
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002645 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002646
2647 if (widget->window->type != TYPE_FULLSCREEN) {
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002648 decoration_width = (t->width + margin) * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002649 decoration_height = t->width +
Scott Moreauc6a7e4b2012-09-28 02:45:06 -06002650 t->titlebar_height + margin * 2;
Kristian Høgsberga1627922012-06-20 17:30:03 -04002651
2652 width = child_width + decoration_width;
2653 height = child_height + decoration_height;
2654 } else {
2655 width = child_width;
2656 height = child_height;
2657 }
2658
2659 window_schedule_resize(widget->window, width, height);
2660}
2661
Kristian Høgsberge4feb562008-11-08 18:53:37 -05002662static void
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002663frame_destroy(struct frame *frame)
2664{
Martin Minarik1998b152012-05-10 02:04:35 +02002665 struct frame_button *button, *tmp;
2666
2667 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2668 frame_button_destroy(button);
2669
Pekka Paalanen4dde2fc2012-01-19 13:33:50 +02002670 /* frame->child must be destroyed by the application */
2671 widget_destroy(frame->widget);
2672 free(frame);
2673}
2674
2675static void
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002676input_set_focus_widget(struct input *input, struct widget *focus,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002677 float x, float y)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002678{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002679 struct widget *old, *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002680 int cursor;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002681
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002682 if (focus == input->focus_widget)
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002683 return;
2684
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002685 old = input->focus_widget;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002686 if (old) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002687 widget = old;
2688 if (input->grab)
2689 widget = input->grab;
2690 if (widget->leave_handler)
2691 widget->leave_handler(old, input, widget->user_data);
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002692 input->focus_widget = NULL;
Kristian Høgsbergee143232012-01-09 08:42:24 -05002693 }
2694
2695 if (focus) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002696 widget = focus;
2697 if (input->grab)
2698 widget = input->grab;
Kristian Høgsbergf33984e2012-06-04 23:36:32 -04002699 input->focus_widget = focus;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002700 if (widget->enter_handler)
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002701 cursor = widget->enter_handler(focus, input, x, y,
2702 widget->user_data);
2703 else
2704 cursor = widget->default_cursor;
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05002705
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002706 input_set_pointer_image(input, cursor);
Kristian Høgsbergee143232012-01-09 08:42:24 -05002707 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002708}
2709
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002710void
2711input_grab(struct input *input, struct widget *widget, uint32_t button)
2712{
2713 input->grab = widget;
2714 input->grab_button = button;
2715}
2716
2717void
2718input_ungrab(struct input *input)
2719{
2720 struct widget *widget;
2721
2722 input->grab = NULL;
2723 if (input->pointer_focus) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002724 widget = window_find_widget(input->pointer_focus,
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002725 input->sx, input->sy);
2726 input_set_focus_widget(input, widget, input->sx, input->sy);
2727 }
2728}
2729
2730static void
2731input_remove_pointer_focus(struct input *input)
2732{
2733 struct window *window = input->pointer_focus;
2734
2735 if (!window)
2736 return;
2737
2738 input_set_focus_widget(input, NULL, 0, 0);
2739
2740 input->pointer_focus = NULL;
2741 input->current_cursor = CURSOR_UNSET;
2742}
2743
2744static void
2745pointer_handle_enter(void *data, struct wl_pointer *pointer,
2746 uint32_t serial, struct wl_surface *surface,
2747 wl_fixed_t sx_w, wl_fixed_t sy_w)
2748{
2749 struct input *input = data;
2750 struct window *window;
2751 struct widget *widget;
2752 float sx = wl_fixed_to_double(sx_w);
2753 float sy = wl_fixed_to_double(sy_w);
2754
2755 if (!surface) {
2756 /* enter event for a window we've just destroyed */
2757 return;
2758 }
2759
2760 input->display->serial = serial;
2761 input->pointer_enter_serial = serial;
2762 input->pointer_focus = wl_surface_get_user_data(surface);
2763 window = input->pointer_focus;
2764
Pekka Paalanen99436862012-11-19 17:15:59 +02002765 if (window->resizing) {
2766 window->resizing = 0;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002767 /* Schedule a redraw to free the pool */
2768 window_schedule_redraw(window);
2769 }
2770
2771 input->sx = sx;
2772 input->sy = sy;
2773
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002774 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002775 input_set_focus_widget(input, widget, sx, sy);
2776}
2777
2778static void
2779pointer_handle_leave(void *data, struct wl_pointer *pointer,
2780 uint32_t serial, struct wl_surface *surface)
2781{
2782 struct input *input = data;
2783
2784 input->display->serial = serial;
2785 input_remove_pointer_focus(input);
2786}
2787
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002788static void
Daniel Stone37816df2012-05-16 18:45:18 +01002789pointer_handle_motion(void *data, struct wl_pointer *pointer,
2790 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002791{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002792 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002793 struct window *window = input->pointer_focus;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002794 struct widget *widget;
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002795 int cursor;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002796 float sx = wl_fixed_to_double(sx_w);
2797 float sy = wl_fixed_to_double(sy_w);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002798
Rob Bradford5f087742013-07-11 19:41:27 +01002799 /* when making the window smaller - e.g. after a unmaximise we might
2800 * still have a pending motion event that the compositor has picked
2801 * based on the old surface dimensions
2802 */
2803 if (sx > window->main_surface->allocation.width ||
2804 sy > window->main_surface->allocation.height)
2805 return;
2806
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002807 input->sx = sx;
2808 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04002809
Pekka Vuorela6e1e3852012-09-17 22:15:57 +03002810 if (!window)
2811 return;
2812
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002813 if (!(input->grab && input->grab_button)) {
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02002814 widget = window_find_widget(window, sx, sy);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002815 input_set_focus_widget(input, widget, sx, sy);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002816 }
2817
Kristian Høgsberg831dd522012-01-10 23:46:33 -05002818 if (input->grab)
2819 widget = input->grab;
2820 else
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002821 widget = input->focus_widget;
Kristian Høgsberg04e98342012-01-09 09:36:16 -05002822 if (widget && widget->motion_handler)
Jonas Ådahlf461eee2012-10-19 19:56:02 +02002823 cursor = widget->motion_handler(input->focus_widget,
Daniel Stone37816df2012-05-16 18:45:18 +01002824 input, time, sx, sy,
2825 widget->user_data);
Kristian Høgsbergbf74d522012-11-30 14:54:35 -05002826 else
2827 cursor = input->focus_widget->default_cursor;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04002828
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -04002829 input_set_pointer_image(input, cursor);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05002830}
2831
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04002832static void
Daniel Stone37816df2012-05-16 18:45:18 +01002833pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002834 uint32_t time, uint32_t button, uint32_t state_w)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002835{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002836 struct input *input = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -05002837 struct widget *widget;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002838 enum wl_pointer_button_state state = state_w;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04002839
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002840 input->display->serial = serial;
Daniel Stone4dbadb12012-05-30 16:31:51 +01002841 if (input->focus_widget && input->grab == NULL &&
2842 state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergb6323512012-01-11 00:04:42 -05002843 input_grab(input, input->focus_widget, button);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002844
Neil Roberts6b28aad2012-01-23 19:11:18 +00002845 widget = input->grab;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002846 if (widget && widget->button_handler)
Neil Roberts6b28aad2012-01-23 19:11:18 +00002847 (*widget->button_handler)(widget,
2848 input, time,
2849 button, state,
2850 input->grab->user_data);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -04002851
Daniel Stone4dbadb12012-05-30 16:31:51 +01002852 if (input->grab && input->grab_button == button &&
2853 state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002854 input_ungrab(input);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002855}
2856
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05002857static void
Daniel Stone37816df2012-05-16 18:45:18 +01002858pointer_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01002859 uint32_t time, uint32_t axis, wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002860{
Philipp Brüschweiler7e0cc542012-08-14 11:02:41 +02002861 struct input *input = data;
2862 struct widget *widget;
2863
2864 widget = input->focus_widget;
2865 if (input->grab)
2866 widget = input->grab;
2867 if (widget && widget->axis_handler)
2868 (*widget->axis_handler)(widget,
2869 input, time,
2870 axis, value,
2871 widget->user_data);
Scott Moreau210d0792012-03-22 10:47:01 -06002872}
2873
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002874static const struct wl_pointer_listener pointer_listener = {
2875 pointer_handle_enter,
2876 pointer_handle_leave,
2877 pointer_handle_motion,
2878 pointer_handle_button,
2879 pointer_handle_axis,
2880};
2881
2882static void
2883input_remove_keyboard_focus(struct input *input)
2884{
2885 struct window *window = input->keyboard_focus;
2886 struct itimerspec its;
2887
2888 its.it_interval.tv_sec = 0;
2889 its.it_interval.tv_nsec = 0;
2890 its.it_value.tv_sec = 0;
2891 its.it_value.tv_nsec = 0;
2892 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2893
2894 if (!window)
2895 return;
2896
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002897 window->focus_count--;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002898 if (window->keyboard_focus_handler)
2899 (*window->keyboard_focus_handler)(window, NULL,
2900 window->user_data);
2901
2902 input->keyboard_focus = NULL;
2903}
2904
2905static void
2906keyboard_repeat_func(struct task *task, uint32_t events)
2907{
2908 struct input *input =
2909 container_of(task, struct input, repeat_task);
2910 struct window *window = input->keyboard_focus;
2911 uint64_t exp;
2912
2913 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2914 /* If we change the timer between the fd becoming
2915 * readable and getting here, there'll be nothing to
2916 * read and we get EAGAIN. */
2917 return;
2918
2919 if (window && window->key_handler) {
2920 (*window->key_handler)(window, input, input->repeat_time,
2921 input->repeat_key, input->repeat_sym,
2922 WL_KEYBOARD_KEY_STATE_PRESSED,
2923 window->user_data);
2924 }
2925}
2926
2927static void
2928keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2929 uint32_t format, int fd, uint32_t size)
2930{
2931 struct input *input = data;
2932 char *map_str;
2933
2934 if (!data) {
2935 close(fd);
2936 return;
2937 }
2938
2939 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2940 close(fd);
2941 return;
2942 }
2943
2944 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2945 if (map_str == MAP_FAILED) {
2946 close(fd);
2947 return;
2948 }
2949
2950 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2951 map_str,
2952 XKB_KEYMAP_FORMAT_TEXT_V1,
2953 0);
2954 munmap(map_str, size);
2955 close(fd);
2956
2957 if (!input->xkb.keymap) {
2958 fprintf(stderr, "failed to compile keymap\n");
2959 return;
2960 }
2961
2962 input->xkb.state = xkb_state_new(input->xkb.keymap);
2963 if (!input->xkb.state) {
2964 fprintf(stderr, "failed to create XKB state\n");
2965 xkb_map_unref(input->xkb.keymap);
2966 input->xkb.keymap = NULL;
2967 return;
2968 }
2969
2970 input->xkb.control_mask =
2971 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2972 input->xkb.alt_mask =
2973 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2974 input->xkb.shift_mask =
2975 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2976}
2977
2978static void
2979keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2980 uint32_t serial, struct wl_surface *surface,
2981 struct wl_array *keys)
2982{
2983 struct input *input = data;
2984 struct window *window;
2985
2986 input->display->serial = serial;
2987 input->keyboard_focus = wl_surface_get_user_data(surface);
2988
2989 window = input->keyboard_focus;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002990 window->focus_count++;
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002991 if (window->keyboard_focus_handler)
2992 (*window->keyboard_focus_handler)(window,
Kristian Høgsberg86adef92012-08-13 22:25:53 -04002993 input, window->user_data);
Kristian Høgsbergb29798b2012-08-11 14:56:08 -04002994}
2995
2996static void
2997keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2998 uint32_t serial, struct wl_surface *surface)
2999{
3000 struct input *input = data;
3001
3002 input->display->serial = serial;
3003 input_remove_keyboard_focus(input);
3004}
3005
Scott Moreau210d0792012-03-22 10:47:01 -06003006static void
Daniel Stone37816df2012-05-16 18:45:18 +01003007keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
Daniel Stonec9785ea2012-05-30 16:31:52 +01003008 uint32_t serial, uint32_t time, uint32_t key,
3009 uint32_t state_w)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003010{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04003011 struct input *input = data;
3012 struct window *window = input->keyboard_focus;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003013 uint32_t code, num_syms;
Daniel Stonec9785ea2012-05-30 16:31:52 +01003014 enum wl_keyboard_key_state state = state_w;
Kristian Høgsberg70163132012-05-08 15:55:39 -04003015 const xkb_keysym_t *syms;
3016 xkb_keysym_t sym;
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003017 struct itimerspec its;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003018
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003019 input->display->serial = serial;
Daniel Stone0d5a5092012-02-16 12:48:00 +00003020 code = key + 8;
Kristian Høgsberg86adef92012-08-13 22:25:53 -04003021 if (!window || !input->xkb.state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003022 return;
3023
Daniel Stone97f68542012-05-30 16:32:01 +01003024 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05003025
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003026 sym = XKB_KEY_NoSymbol;
3027 if (num_syms == 1)
3028 sym = syms[0];
3029
3030 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
Daniel Stonec9785ea2012-05-30 16:31:52 +01003031 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003032 window_set_maximized(window,
3033 window->type != TYPE_MAXIMIZED);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04003034 } else if (sym == XKB_KEY_F11 &&
3035 window->fullscreen_handler &&
3036 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3037 window->fullscreen_handler(window, window->user_data);
Kristian Høgsberg4fc15352012-07-25 16:35:28 -04003038 } else if (sym == XKB_KEY_F4 &&
3039 input->modifiers == MOD_ALT_MASK &&
3040 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3041 if (window->close_handler)
3042 window->close_handler(window->parent,
3043 window->user_data);
3044 else
3045 display_exit(window->display);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003046 } else if (window->key_handler) {
3047 (*window->key_handler)(window, input, time, key,
3048 sym, state, window->user_data);
3049 }
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04003050
3051 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3052 key == input->repeat_key) {
3053 its.it_interval.tv_sec = 0;
3054 its.it_interval.tv_nsec = 0;
3055 its.it_value.tv_sec = 0;
3056 its.it_value.tv_nsec = 0;
3057 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3058 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3059 input->repeat_sym = sym;
3060 input->repeat_key = key;
3061 input->repeat_time = time;
3062 its.it_interval.tv_sec = 0;
3063 its.it_interval.tv_nsec = 25 * 1000 * 1000;
3064 its.it_value.tv_sec = 0;
3065 its.it_value.tv_nsec = 400 * 1000 * 1000;
3066 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3067 }
3068}
3069
3070static void
Daniel Stone351eb612012-05-31 15:27:47 -04003071keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3072 uint32_t serial, uint32_t mods_depressed,
3073 uint32_t mods_latched, uint32_t mods_locked,
3074 uint32_t group)
3075{
3076 struct input *input = data;
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003077 xkb_mod_mask_t mask;
Daniel Stone351eb612012-05-31 15:27:47 -04003078
Matt Ropere61561f2013-06-24 16:52:43 +01003079 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3080 if (!input->xkb.keymap)
3081 return;
3082
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003083 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3084 mods_locked, 0, 0, group);
Jonas Ådahld9f6b072012-09-27 18:40:45 +02003085 mask = xkb_state_serialize_mods(input->xkb.state,
3086 XKB_STATE_DEPRESSED |
3087 XKB_STATE_LATCHED);
3088 input->modifiers = 0;
3089 if (mask & input->xkb.control_mask)
3090 input->modifiers |= MOD_CONTROL_MASK;
3091 if (mask & input->xkb.alt_mask)
3092 input->modifiers |= MOD_ALT_MASK;
3093 if (mask & input->xkb.shift_mask)
3094 input->modifiers |= MOD_SHIFT_MASK;
Daniel Stone351eb612012-05-31 15:27:47 -04003095}
3096
Daniel Stone37816df2012-05-16 18:45:18 +01003097static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01003098 keyboard_handle_keymap,
Daniel Stone37816df2012-05-16 18:45:18 +01003099 keyboard_handle_enter,
3100 keyboard_handle_leave,
3101 keyboard_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04003102 keyboard_handle_modifiers,
Daniel Stone37816df2012-05-16 18:45:18 +01003103};
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003104
3105static void
Daniel Stone37816df2012-05-16 18:45:18 +01003106seat_handle_capabilities(void *data, struct wl_seat *seat,
3107 enum wl_seat_capability caps)
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003108{
Daniel Stone37816df2012-05-16 18:45:18 +01003109 struct input *input = data;
3110
3111 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3112 input->pointer = wl_seat_get_pointer(seat);
3113 wl_pointer_set_user_data(input->pointer, input);
3114 wl_pointer_add_listener(input->pointer, &pointer_listener,
3115 input);
3116 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3117 wl_pointer_destroy(input->pointer);
3118 input->pointer = NULL;
3119 }
3120
3121 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3122 input->keyboard = wl_seat_get_keyboard(seat);
3123 wl_keyboard_set_user_data(input->keyboard, input);
3124 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3125 input);
3126 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3127 wl_keyboard_destroy(input->keyboard);
3128 input->keyboard = NULL;
3129 }
Kristian Høgsberge04ad572011-12-21 17:14:54 -05003130}
3131
Daniel Stone37816df2012-05-16 18:45:18 +01003132static const struct wl_seat_listener seat_listener = {
3133 seat_handle_capabilities,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05003134};
3135
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003136void
3137input_get_position(struct input *input, int32_t *x, int32_t *y)
3138{
3139 *x = input->sx;
3140 *y = input->sy;
3141}
3142
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003143struct display *
3144input_get_display(struct input *input)
3145{
3146 return input->display;
3147}
3148
Daniel Stone37816df2012-05-16 18:45:18 +01003149struct wl_seat *
3150input_get_seat(struct input *input)
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003151{
Daniel Stone37816df2012-05-16 18:45:18 +01003152 return input->seat;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04003153}
3154
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05003155uint32_t
3156input_get_modifiers(struct input *input)
3157{
3158 return input->modifiers;
3159}
3160
Kristian Høgsbergb6323512012-01-11 00:04:42 -05003161struct widget *
3162input_get_focus_widget(struct input *input)
3163{
3164 return input->focus_widget;
3165}
3166
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003167struct data_offer {
3168 struct wl_data_offer *offer;
3169 struct input *input;
3170 struct wl_array types;
3171 int refcount;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04003172
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003173 struct task io_task;
3174 int fd;
3175 data_func_t func;
3176 int32_t x, y;
3177 void *user_data;
3178};
3179
3180static void
3181data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3182{
3183 struct data_offer *offer = data;
3184 char **p;
3185
3186 p = wl_array_add(&offer->types, sizeof *p);
3187 *p = strdup(type);
3188}
3189
3190static const struct wl_data_offer_listener data_offer_listener = {
3191 data_offer_offer,
3192};
3193
3194static void
3195data_offer_destroy(struct data_offer *offer)
3196{
3197 char **p;
3198
3199 offer->refcount--;
3200 if (offer->refcount == 0) {
3201 wl_data_offer_destroy(offer->offer);
3202 for (p = offer->types.data; *p; p++)
3203 free(*p);
3204 wl_array_release(&offer->types);
3205 free(offer);
3206 }
3207}
3208
3209static void
3210data_device_data_offer(void *data,
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003211 struct wl_data_device *data_device,
3212 struct wl_data_offer *_offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003213{
3214 struct data_offer *offer;
3215
3216 offer = malloc(sizeof *offer);
3217
3218 wl_array_init(&offer->types);
3219 offer->refcount = 1;
3220 offer->input = data;
Kristian Høgsberg8733b332012-06-28 22:04:06 -04003221 offer->offer = _offer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003222 wl_data_offer_add_listener(offer->offer,
3223 &data_offer_listener, offer);
3224}
3225
3226static void
3227data_device_enter(void *data, struct wl_data_device *data_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003228 uint32_t serial, struct wl_surface *surface,
Daniel Stone103db7f2012-05-08 17:17:55 +01003229 wl_fixed_t x_w, wl_fixed_t y_w,
3230 struct wl_data_offer *offer)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003231{
3232 struct input *input = data;
3233 struct window *window;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003234 void *types_data;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003235 float x = wl_fixed_to_double(x_w);
3236 float y = wl_fixed_to_double(y_w);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003237 char **p;
3238
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003239 input->pointer_enter_serial = serial;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003240 window = wl_surface_get_user_data(surface);
3241 input->pointer_focus = window;
3242
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003243 if (offer) {
3244 input->drag_offer = wl_data_offer_get_user_data(offer);
3245
3246 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3247 *p = NULL;
3248
3249 types_data = input->drag_offer->types.data;
3250 } else {
3251 input->drag_offer = NULL;
3252 types_data = NULL;
3253 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003254
3255 window = input->pointer_focus;
3256 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003257 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003258 window->user_data);
3259}
3260
3261static void
3262data_device_leave(void *data, struct wl_data_device *data_device)
3263{
3264 struct input *input = data;
3265
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003266 if (input->drag_offer) {
3267 data_offer_destroy(input->drag_offer);
3268 input->drag_offer = NULL;
3269 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003270}
3271
3272static void
3273data_device_motion(void *data, struct wl_data_device *data_device,
Daniel Stone103db7f2012-05-08 17:17:55 +01003274 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003275{
3276 struct input *input = data;
3277 struct window *window = input->pointer_focus;
Kristian Høgsberg80680c72012-05-10 12:21:37 -04003278 float x = wl_fixed_to_double(x_w);
3279 float y = wl_fixed_to_double(y_w);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003280 void *types_data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003281
3282 input->sx = x;
3283 input->sy = y;
3284
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003285 if (input->drag_offer)
3286 types_data = input->drag_offer->types.data;
3287 else
3288 types_data = NULL;
3289
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003290 if (window->data_handler)
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +03003291 window->data_handler(window, input, x, y, types_data,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003292 window->user_data);
3293}
3294
3295static void
3296data_device_drop(void *data, struct wl_data_device *data_device)
3297{
3298 struct input *input = data;
3299 struct window *window = input->pointer_focus;
3300
3301 if (window->drop_handler)
3302 window->drop_handler(window, input,
3303 input->sx, input->sy, window->user_data);
3304}
3305
3306static void
3307data_device_selection(void *data,
3308 struct wl_data_device *wl_data_device,
3309 struct wl_data_offer *offer)
3310{
3311 struct input *input = data;
3312 char **p;
3313
3314 if (input->selection_offer)
3315 data_offer_destroy(input->selection_offer);
3316
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003317 if (offer) {
3318 input->selection_offer = wl_data_offer_get_user_data(offer);
3319 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3320 *p = NULL;
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003321 } else {
3322 input->selection_offer = NULL;
Kristian Høgsberg42c8f602012-01-27 11:04:18 -05003323 }
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003324}
3325
3326static const struct wl_data_device_listener data_device_listener = {
3327 data_device_data_offer,
3328 data_device_enter,
3329 data_device_leave,
3330 data_device_motion,
3331 data_device_drop,
3332 data_device_selection
3333};
3334
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003335static void
3336input_set_pointer_image_index(struct input *input, int index)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003337{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003338 struct wl_buffer *buffer;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003339 struct wl_cursor *cursor;
3340 struct wl_cursor_image *image;
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003341
Daniel Stone80972742012-11-07 17:51:39 +11003342 if (!input->pointer)
3343 return;
3344
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003345 cursor = input->display->cursors[input->current_cursor];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003346 if (!cursor)
Dima Ryazanovff1c2d72012-05-08 21:02:33 -07003347 return;
3348
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003349 if (index >= (int) cursor->image_count) {
3350 fprintf(stderr, "cursor index out of range\n");
3351 return;
3352 }
3353
3354 image = cursor->images[index];
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03003355 buffer = wl_cursor_image_get_buffer(image);
3356 if (!buffer)
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003357 return;
3358
Kristian Høgsbergae277372012-08-01 09:41:08 -04003359 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003360 input->pointer_surface,
3361 image->hotspot_x, image->hotspot_y);
3362 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3363 wl_surface_damage(input->pointer_surface, 0, 0,
3364 image->width, image->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +03003365 wl_surface_commit(input->pointer_surface);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003366}
3367
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003368static const struct wl_callback_listener pointer_surface_listener;
3369
3370static void
3371pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3372 uint32_t time)
3373{
3374 struct input *input = data;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003375 struct wl_cursor *cursor;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003376 int i;
3377
3378 if (callback) {
3379 assert(callback == input->cursor_frame_cb);
3380 wl_callback_destroy(callback);
3381 input->cursor_frame_cb = NULL;
3382 }
3383
Daniel Stone80972742012-11-07 17:51:39 +11003384 if (!input->pointer)
3385 return;
3386
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003387 if (input->current_cursor == CURSOR_BLANK) {
Kristian Høgsbergae277372012-08-01 09:41:08 -04003388 wl_pointer_set_cursor(input->pointer,
3389 input->pointer_enter_serial,
Kristian Høgsbergf3370522012-06-20 23:04:41 -04003390 NULL, 0, 0);
3391 return;
3392 }
3393
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003394 if (input->current_cursor == CURSOR_UNSET)
3395 return;
Daniel Stonea494f1d2012-06-18 19:31:12 +01003396 cursor = input->display->cursors[input->current_cursor];
3397 if (!cursor)
3398 return;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003399
3400 /* FIXME We don't have the current time on the first call so we set
3401 * the animation start to the time of the first frame callback. */
3402 if (time == 0)
3403 input->cursor_anim_start = 0;
3404 else if (input->cursor_anim_start == 0)
3405 input->cursor_anim_start = time;
3406
3407 if (time == 0 || input->cursor_anim_start == 0)
3408 i = 0;
3409 else
3410 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3411
Pekka Paalanenbc106382012-10-10 12:49:31 +03003412 if (cursor->image_count > 1) {
3413 input->cursor_frame_cb =
3414 wl_surface_frame(input->pointer_surface);
3415 wl_callback_add_listener(input->cursor_frame_cb,
3416 &pointer_surface_listener, input);
3417 }
3418
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003419 input_set_pointer_image_index(input, i);
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003420}
3421
3422static const struct wl_callback_listener pointer_surface_listener = {
3423 pointer_surface_frame_callback
3424};
3425
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003426void
3427input_set_pointer_image(struct input *input, int pointer)
3428{
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003429 int force = 0;
3430
Kristian Høgsberge530a0a2012-10-29 16:42:26 -04003431 if (!input->pointer)
3432 return;
3433
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003434 if (input->pointer_enter_serial > input->cursor_serial)
3435 force = 1;
3436
3437 if (!force && pointer == input->current_cursor)
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003438 return;
3439
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003440 input->current_cursor = pointer;
Kristian Høgsberg11f600d2012-06-22 10:52:58 -04003441 input->cursor_serial = input->pointer_enter_serial;
Ander Conselvan de Oliveira80620072012-06-15 17:27:36 +03003442 if (!input->cursor_frame_cb)
3443 pointer_surface_frame_callback(input, NULL, 0);
Ander Conselvan de Oliveiraddca4962012-07-16 14:15:49 +03003444 else if (force) {
3445 /* The current frame callback may be stuck if, for instance,
3446 * the set cursor request was processed by the server after
3447 * this client lost the focus. In this case the cursor surface
3448 * might not be mapped and the frame callback wouldn't ever
3449 * complete. Send a set_cursor and attach to try to map the
3450 * cursor surface again so that the callback will finish */
3451 input_set_pointer_image_index(input, 0);
3452 }
Kristian Høgsberg7cee1972012-06-04 23:37:42 -04003453}
3454
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003455struct wl_data_device *
3456input_get_data_device(struct input *input)
3457{
3458 return input->data_device;
3459}
3460
3461void
3462input_set_selection(struct input *input,
3463 struct wl_data_source *source, uint32_t time)
3464{
3465 wl_data_device_set_selection(input->data_device, source, time);
3466}
3467
3468void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003469input_accept(struct input *input, const char *type)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003470{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003471 wl_data_offer_accept(input->drag_offer->offer,
3472 input->pointer_enter_serial, type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003473}
3474
3475static void
3476offer_io_func(struct task *task, uint32_t events)
3477{
3478 struct data_offer *offer =
3479 container_of(task, struct data_offer, io_task);
3480 unsigned int len;
3481 char buffer[4096];
3482
3483 len = read(offer->fd, buffer, sizeof buffer);
3484 offer->func(buffer, len,
3485 offer->x, offer->y, offer->user_data);
3486
3487 if (len == 0) {
3488 close(offer->fd);
3489 data_offer_destroy(offer);
3490 }
3491}
3492
3493static void
3494data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3495 data_func_t func, void *user_data)
3496{
3497 int p[2];
3498
Jonas Ådahl3685c3a2012-03-30 23:10:27 +02003499 if (pipe2(p, O_CLOEXEC) == -1)
3500 return;
3501
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003502 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3503 close(p[1]);
3504
3505 offer->io_task.run = offer_io_func;
3506 offer->fd = p[0];
3507 offer->func = func;
3508 offer->refcount++;
3509 offer->user_data = user_data;
3510
3511 display_watch_fd(offer->input->display,
3512 offer->fd, EPOLLIN, &offer->io_task);
3513}
3514
3515void
3516input_receive_drag_data(struct input *input, const char *mime_type,
3517 data_func_t func, void *data)
3518{
3519 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3520 input->drag_offer->x = input->sx;
3521 input->drag_offer->y = input->sy;
3522}
3523
3524int
3525input_receive_selection_data(struct input *input, const char *mime_type,
3526 data_func_t func, void *data)
3527{
3528 char **p;
3529
3530 if (input->selection_offer == NULL)
3531 return -1;
3532
3533 for (p = input->selection_offer->types.data; *p; p++)
3534 if (strcmp(mime_type, *p) == 0)
3535 break;
3536
3537 if (*p == NULL)
3538 return -1;
3539
3540 data_offer_receive_data(input->selection_offer,
3541 mime_type, func, data);
3542 return 0;
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003543}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04003544
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003545int
3546input_receive_selection_data_to_fd(struct input *input,
3547 const char *mime_type, int fd)
3548{
Kristian Høgsberga4b3d0e2012-05-31 23:30:32 -04003549 if (input->selection_offer)
3550 wl_data_offer_receive(input->selection_offer->offer,
3551 mime_type, fd);
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05003552
3553 return 0;
3554}
3555
Kristian Høgsberg41da9082010-11-30 14:01:07 -05003556void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003557window_move(struct window *window, struct input *input, uint32_t serial)
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003558{
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02003559 if (!window->shell_surface)
3560 return;
3561
Daniel Stone37816df2012-05-16 18:45:18 +01003562 wl_shell_surface_move(window->shell_surface, input->seat, serial);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05003563}
3564
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003565static void
Pekka Paalanen35e82632013-04-25 13:57:48 +03003566surface_set_synchronized(struct surface *surface)
3567{
3568 if (!surface->subsurface)
3569 return;
3570
3571 if (surface->synchronized)
3572 return;
3573
3574 wl_subsurface_set_sync(surface->subsurface);
3575 surface->synchronized = 1;
3576}
3577
3578static void
3579surface_set_synchronized_default(struct surface *surface)
3580{
3581 if (!surface->subsurface)
3582 return;
3583
3584 if (surface->synchronized == surface->synchronized_default)
3585 return;
3586
3587 if (surface->synchronized_default)
3588 wl_subsurface_set_sync(surface->subsurface);
3589 else
3590 wl_subsurface_set_desync(surface->subsurface);
3591
3592 surface->synchronized = surface->synchronized_default;
3593}
3594
3595static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003596surface_resize(struct surface *surface)
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003597{
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003598 struct widget *widget = surface->widget;
3599 struct wl_compositor *compositor = widget->window->display->compositor;
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003600
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003601 if (surface->input_region) {
3602 wl_region_destroy(surface->input_region);
3603 surface->input_region = NULL;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003604 }
3605
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003606 if (surface->opaque_region)
3607 wl_region_destroy(surface->opaque_region);
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003608
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003609 surface->opaque_region = wl_compositor_create_region(compositor);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05003610
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003611 if (widget->resize_handler)
3612 widget->resize_handler(widget,
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003613 widget->allocation.width,
3614 widget->allocation.height,
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003615 widget->user_data);
3616
Pekka Paalanen35e82632013-04-25 13:57:48 +03003617 if (surface->subsurface &&
3618 (surface->allocation.x != widget->allocation.x ||
3619 surface->allocation.y != widget->allocation.y)) {
3620 wl_subsurface_set_position(surface->subsurface,
3621 widget->allocation.x,
3622 widget->allocation.y);
3623 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003624 if (surface->allocation.width != widget->allocation.width ||
3625 surface->allocation.height != widget->allocation.height) {
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003626 window_schedule_redraw(widget->window);
3627 }
Pekka Paalanen35e82632013-04-25 13:57:48 +03003628 surface->allocation = widget->allocation;
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003629
3630 if (widget->opaque)
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003631 wl_region_add(surface->opaque_region, 0, 0,
Ander Conselvan de Oliveiraddd3e272012-11-30 17:34:23 +02003632 widget->allocation.width,
3633 widget->allocation.height);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003634}
3635
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003636static void
Pekka Paalanene9297f82013-04-25 13:57:51 +03003637hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3638{
3639 /*
3640 * This hack should be removed, when EGL respects
3641 * eglSwapInterval(0).
3642 *
3643 * If this window has sub-surfaces, especially a free-running
3644 * EGL-widget, we need to post the parent surface once with
3645 * all the old state to guarantee, that the EGL-widget will
3646 * receive its frame callback soon. Otherwise, a forced call
3647 * to eglSwapBuffers may end up blocking, waiting for a frame
3648 * event that will never come, because we will commit the parent
3649 * surface with all new state only after eglSwapBuffers returns.
3650 *
3651 * This assumes, that:
3652 * 1. When the EGL widget's resize hook is called, it pauses.
3653 * 2. When the EGL widget's redraw hook is called, it forces a
3654 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3655 * In a single threaded application condition 1 is a no-op.
3656 *
3657 * XXX: This should actually be after the surface_resize() calls,
3658 * but cannot, because then it would commit the incomplete state
3659 * accumulated from the widget resize hooks.
3660 */
3661 if (window->subsurface_list.next != &window->main_surface->link ||
3662 window->subsurface_list.prev != &window->main_surface->link)
3663 wl_surface_commit(window->main_surface->surface);
3664}
3665
3666static void
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003667idle_resize(struct window *window)
3668{
Pekka Paalanen35e82632013-04-25 13:57:48 +03003669 struct surface *surface;
3670
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003671 window->resize_needed = 0;
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003672 window->redraw_needed = 1;
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003673
Pekka Paalanen71233882013-04-25 13:57:53 +03003674 DBG("from %dx%d to %dx%d\n",
3675 window->main_surface->server_allocation.width,
3676 window->main_surface->server_allocation.height,
3677 window->pending_allocation.width,
3678 window->pending_allocation.height);
3679
Pekka Paalanene9297f82013-04-25 13:57:51 +03003680 hack_prevent_EGL_sub_surface_deadlock(window);
3681
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003682 widget_set_allocation(window->main_surface->widget,
3683 window->pending_allocation.x,
3684 window->pending_allocation.y,
3685 window->pending_allocation.width,
3686 window->pending_allocation.height);
3687
3688 surface_resize(window->main_surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003689
3690 /* The main surface is in the list, too. Main surface's
3691 * resize_handler is responsible for calling widget_set_allocation()
3692 * on all sub-surface root widgets, so they will be resized
3693 * properly.
3694 */
3695 wl_list_for_each(surface, &window->subsurface_list, link) {
3696 if (surface == window->main_surface)
3697 continue;
3698
3699 surface_set_synchronized(surface);
3700 surface_resize(surface);
3701 }
Pekka Paalanenb1cd9b12013-02-13 16:17:18 +02003702}
3703
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003704void
3705window_schedule_resize(struct window *window, int width, int height)
3706{
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003707 window->pending_allocation.x = 0;
3708 window->pending_allocation.y = 0;
3709 window->pending_allocation.width = width;
3710 window->pending_allocation.height = height;
3711
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003712 if (window->min_allocation.width == 0)
3713 window->min_allocation = window->pending_allocation;
3714 if (window->pending_allocation.width < window->min_allocation.width)
3715 window->pending_allocation.width = window->min_allocation.width;
3716 if (window->pending_allocation.height < window->min_allocation.height)
3717 window->pending_allocation.height = window->min_allocation.height;
3718
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003719 window->resize_needed = 1;
3720 window_schedule_redraw(window);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003721}
3722
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003723void
3724widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3725{
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05003726 window_schedule_resize(widget->window, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05003727}
3728
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05003729static void
Scott Moreauff1db4a2012-04-17 19:06:18 -06003730handle_ping(void *data, struct wl_shell_surface *shell_surface,
3731 uint32_t serial)
3732{
3733 wl_shell_surface_pong(shell_surface, serial);
3734}
3735
3736static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003737handle_configure(void *data, struct wl_shell_surface *shell_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003738 uint32_t edges, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003739{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003740 struct window *window = data;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003741
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01003742 window->resize_edges = edges;
Kristian Høgsberg0d1c0622012-01-31 15:30:47 -05003743 window_schedule_resize(window, width, height);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003744}
3745
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003746static void
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003747menu_destroy(struct menu *menu)
3748{
3749 widget_destroy(menu->widget);
3750 window_destroy(menu->window);
3751 free(menu);
3752}
3753
3754static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003755handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3756{
3757 struct window *window = data;
Pekka Paalanenac95f3e2013-02-13 16:17:17 +02003758 struct menu *menu = window->main_surface->widget->user_data;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003759
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003760 /* FIXME: Need more context in this event, at least the input
Kristian Høgsberg831dd522012-01-10 23:46:33 -05003761 * device. Or just use wl_callback. And this really needs to
3762 * be a window vfunc that the menu can set. And we need the
3763 * time. */
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003764
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003765 input_ungrab(menu->input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02003766 menu_destroy(menu);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003767}
3768
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003769static const struct wl_shell_surface_listener shell_surface_listener = {
Scott Moreauff1db4a2012-04-17 19:06:18 -06003770 handle_ping,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003771 handle_configure,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05003772 handle_popup_done
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04003773};
3774
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003775void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003776window_get_allocation(struct window *window,
3777 struct rectangle *allocation)
3778{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003779 *allocation = window->main_surface->allocation;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01003780}
3781
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003782static void
Kristian Høgsberg441338c2012-01-10 13:52:34 -05003783widget_redraw(struct widget *widget)
3784{
3785 struct widget *child;
3786
3787 if (widget->redraw_handler)
3788 widget->redraw_handler(widget, widget->user_data);
3789 wl_list_for_each(child, &widget->child_list, link)
3790 widget_redraw(child);
3791}
3792
3793static void
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003794frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3795{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003796 struct surface *surface = data;
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003797
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003798 assert(callback == surface->frame_cb);
Pekka Paalanen71233882013-04-25 13:57:53 +03003799 DBG_OBJ(callback, "done\n");
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003800 wl_callback_destroy(callback);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003801 surface->frame_cb = NULL;
3802
Pekka Paalanen7ff7a802013-04-25 13:57:50 +03003803 surface->last_time = time;
3804
Pekka Paalanen71233882013-04-25 13:57:53 +03003805 if (surface->redraw_needed || surface->window->redraw_needed) {
3806 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003807 window_schedule_redraw_task(surface->window);
Pekka Paalanen71233882013-04-25 13:57:53 +03003808 }
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003809}
3810
3811static const struct wl_callback_listener listener = {
3812 frame_callback
3813};
3814
3815static void
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003816surface_redraw(struct surface *surface)
3817{
Pekka Paalanen71233882013-04-25 13:57:53 +03003818 DBG_OBJ(surface->surface, "begin\n");
3819
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003820 if (!surface->window->redraw_needed && !surface->redraw_needed)
3821 return;
3822
3823 /* Whole-window redraw forces a redraw even if the previous has
3824 * not yet hit the screen.
3825 */
3826 if (surface->frame_cb) {
3827 if (!surface->window->redraw_needed)
3828 return;
3829
Pekka Paalanen71233882013-04-25 13:57:53 +03003830 DBG_OBJ(surface->frame_cb, "cancelled\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003831 wl_callback_destroy(surface->frame_cb);
3832 }
3833
3834 surface->frame_cb = wl_surface_frame(surface->surface);
3835 wl_callback_add_listener(surface->frame_cb, &listener, surface);
Pekka Paalanen71233882013-04-25 13:57:53 +03003836 DBG_OBJ(surface->frame_cb, "new\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003837
3838 surface->redraw_needed = 0;
Pekka Paalanen71233882013-04-25 13:57:53 +03003839 DBG_OBJ(surface->surface, "-> widget_redraw\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003840 widget_redraw(surface->widget);
Pekka Paalanen71233882013-04-25 13:57:53 +03003841 DBG_OBJ(surface->surface, "done\n");
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003842}
3843
3844static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003845idle_redraw(struct task *task, uint32_t events)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003846{
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003847 struct window *window = container_of(task, struct window, redraw_task);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003848 struct surface *surface;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003849
Pekka Paalanen71233882013-04-25 13:57:53 +03003850 DBG(" --------- \n");
3851
Pekka Paalaneneebff542013-04-25 13:57:52 +03003852 wl_list_init(&window->redraw_task.link);
3853 window->redraw_task_scheduled = 0;
3854
3855 if (window->resize_needed) {
3856 /* throttle resizing to the main surface display */
Pekka Paalanen71233882013-04-25 13:57:53 +03003857 if (window->main_surface->frame_cb) {
3858 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
Pekka Paalaneneebff542013-04-25 13:57:52 +03003859 return;
Pekka Paalanen71233882013-04-25 13:57:53 +03003860 }
Pekka Paalaneneebff542013-04-25 13:57:52 +03003861
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003862 idle_resize(window);
Pekka Paalaneneebff542013-04-25 13:57:52 +03003863 }
Kristian Høgsberg42b4f802012-03-26 13:49:29 -04003864
Pekka Paalanen35e82632013-04-25 13:57:48 +03003865 wl_list_for_each(surface, &window->subsurface_list, link)
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003866 surface_redraw(surface);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003867
Kristian Høgsberg6bd4d972012-03-24 14:42:09 -04003868 window->redraw_needed = 0;
Pekka Paalanenbc106382012-10-10 12:49:31 +03003869 window_flush(window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03003870
3871 wl_list_for_each(surface, &window->subsurface_list, link)
3872 surface_set_synchronized_default(surface);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003873}
3874
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003875static void
3876window_schedule_redraw_task(struct window *window)
3877{
3878 if (window->configure_requests)
3879 return;
3880 if (!window->redraw_task_scheduled) {
3881 window->redraw_task.run = idle_redraw;
3882 display_defer(window->display, &window->redraw_task);
3883 window->redraw_task_scheduled = 1;
3884 }
3885}
3886
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003887void
3888window_schedule_redraw(struct window *window)
3889{
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003890 struct surface *surface;
3891
Pekka Paalanen71233882013-04-25 13:57:53 +03003892 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
3893
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003894 wl_list_for_each(surface, &window->subsurface_list, link)
3895 surface->redraw_needed = 1;
3896
3897 window_schedule_redraw_task(window);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04003898}
3899
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003900int
3901window_is_fullscreen(struct window *window)
3902{
3903 return window->type == TYPE_FULLSCREEN;
3904}
3905
MoD063a8822013-03-02 23:43:57 +00003906static void
3907configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
3908{
3909 struct window *window = data;
3910
3911 wl_callback_destroy(callback);
3912 window->configure_requests--;
3913
3914 if (!window->configure_requests)
3915 window_schedule_redraw(window);
3916}
3917
3918static struct wl_callback_listener configure_request_listener = {
3919 configure_request_completed,
3920};
3921
3922static void
3923window_defer_redraw_until_configure(struct window* window)
3924{
3925 struct wl_callback *callback;
3926
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003927 if (window->redraw_task_scheduled) {
MoD063a8822013-03-02 23:43:57 +00003928 wl_list_remove(&window->redraw_task.link);
Pekka Paalanen40cb67b2013-04-25 13:57:49 +03003929 window->redraw_task_scheduled = 0;
MoD063a8822013-03-02 23:43:57 +00003930 }
3931
3932 callback = wl_display_sync(window->display->display);
3933 wl_callback_add_listener(callback, &configure_request_listener, window);
3934 window->configure_requests++;
3935}
3936
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05003937void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003938window_set_fullscreen(struct window *window, int fullscreen)
3939{
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003940 if (!window->display->shell)
3941 return;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003942
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003943 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05003944 return;
3945
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003946 if (fullscreen) {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003947 window->saved_type = window->type;
Rafal Mielniczukfc22be02013-03-11 19:26:55 +01003948 if (window->type == TYPE_TOPLEVEL) {
3949 window->saved_allocation = window->main_surface->allocation;
3950 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04003951 window->type = TYPE_FULLSCREEN;
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003952 wl_shell_surface_set_fullscreen(window->shell_surface,
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003953 window->fullscreen_method,
Kristian Høgsberg1517def2012-02-16 22:56:12 -05003954 0, NULL);
MoD063a8822013-03-02 23:43:57 +00003955 window_defer_redraw_until_configure (window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003956 } else {
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003957 if (window->saved_type == TYPE_MAXIMIZED) {
3958 window_set_maximized(window, 1);
3959 } else {
3960 window->type = TYPE_TOPLEVEL;
3961 wl_shell_surface_set_toplevel(window->shell_surface);
3962 window_schedule_resize(window,
3963 window->saved_allocation.width,
3964 window->saved_allocation.height);
3965 }
3966
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003967 }
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003968}
3969
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02003970void
3971window_set_fullscreen_method(struct window *window,
3972 enum wl_shell_surface_fullscreen_method method)
3973{
3974 window->fullscreen_method = method;
3975}
3976
Kristian Høgsberg1671e112012-10-10 11:36:24 -04003977int
3978window_is_maximized(struct window *window)
3979{
3980 return window->type == TYPE_MAXIMIZED;
3981}
3982
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04003983void
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003984window_set_maximized(struct window *window, int maximized)
3985{
3986 if (!window->display->shell)
3987 return;
3988
3989 if ((window->type == TYPE_MAXIMIZED) == maximized)
3990 return;
3991
3992 if (window->type == TYPE_TOPLEVEL) {
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02003993 window->saved_allocation = window->main_surface->allocation;
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05003994 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3995 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00003996 window_defer_redraw_until_configure(window);
Rafal Mielniczukc1a3cd12013-03-11 19:26:56 +01003997 } else if (window->type == TYPE_FULLSCREEN) {
3998 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3999 window->type = TYPE_MAXIMIZED;
MoD063a8822013-03-02 23:43:57 +00004000 window_defer_redraw_until_configure(window);
Kristian Høgsbergd6bcd7d2012-02-16 15:53:46 -05004001 } else {
4002 wl_shell_surface_set_toplevel(window->shell_surface);
4003 window->type = TYPE_TOPLEVEL;
4004 window_schedule_resize(window,
4005 window->saved_allocation.width,
4006 window->saved_allocation.height);
4007 }
4008}
4009
4010void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004011window_set_user_data(struct window *window, void *data)
4012{
4013 window->user_data = data;
4014}
4015
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04004016void *
4017window_get_user_data(struct window *window)
4018{
4019 return window->user_data;
4020}
4021
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004022void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004023window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004024 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004025{
4026 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05004027}
4028
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004029void
4030window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04004031 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004032{
4033 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05004034}
4035
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004036void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004037window_set_data_handler(struct window *window, window_data_handler_t handler)
4038{
4039 window->data_handler = handler;
4040}
4041
4042void
4043window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4044{
4045 window->drop_handler = handler;
4046}
4047
4048void
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004049window_set_close_handler(struct window *window,
4050 window_close_handler_t handler)
4051{
4052 window->close_handler = handler;
4053}
4054
4055void
Kristian Høgsberg67ace202012-07-23 21:56:31 -04004056window_set_fullscreen_handler(struct window *window,
4057 window_fullscreen_handler_t handler)
4058{
4059 window->fullscreen_handler = handler;
4060}
4061
4062void
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004063window_set_output_handler(struct window *window,
4064 window_output_handler_t handler)
4065{
4066 window->output_handler = handler;
4067}
4068
4069void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004070window_set_title(struct window *window, const char *title)
4071{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05004072 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004073 window->title = strdup(title);
Kristian Høgsberg3e0fe5c2012-05-02 09:47:55 -04004074 if (window->shell_surface)
4075 wl_shell_surface_set_title(window->shell_surface, title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13004076}
4077
4078const char *
4079window_get_title(struct window *window)
4080{
4081 return window->title;
4082}
4083
4084void
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004085window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4086{
4087 struct text_cursor_position *text_cursor_position =
4088 window->display->text_cursor_position;
4089
Scott Moreau9295ce02012-06-01 12:46:10 -06004090 if (!text_cursor_position)
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004091 return;
4092
4093 text_cursor_position_notify(text_cursor_position,
Pekka Paalanen4e373742013-02-13 16:17:13 +02004094 window->main_surface->surface,
4095 wl_fixed_from_int(x),
4096 wl_fixed_from_int(y));
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004097}
4098
4099void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004100window_damage(struct window *window, int32_t x, int32_t y,
4101 int32_t width, int32_t height)
4102{
Pekka Paalanen4e373742013-02-13 16:17:13 +02004103 wl_surface_damage(window->main_surface->surface, x, y, width, height);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004104}
4105
Casey Dahlin9074db52012-04-19 22:50:09 -04004106static void
4107surface_enter(void *data,
Rob Bradford7507b572012-05-15 17:55:34 +01004108 struct wl_surface *wl_surface, struct wl_output *wl_output)
Casey Dahlin9074db52012-04-19 22:50:09 -04004109{
Rob Bradford7507b572012-05-15 17:55:34 +01004110 struct window *window = data;
4111 struct output *output;
4112 struct output *output_found = NULL;
4113 struct window_output *window_output;
4114
4115 wl_list_for_each(output, &window->display->output_list, link) {
4116 if (output->output == wl_output) {
4117 output_found = output;
4118 break;
4119 }
4120 }
4121
4122 if (!output_found)
4123 return;
4124
4125 window_output = malloc (sizeof *window_output);
4126 window_output->output = output_found;
4127
4128 wl_list_insert (&window->window_output_list, &window_output->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004129
4130 if (window->output_handler)
4131 window->output_handler(window, output_found, 1,
4132 window->user_data);
Casey Dahlin9074db52012-04-19 22:50:09 -04004133}
4134
4135static void
4136surface_leave(void *data,
4137 struct wl_surface *wl_surface, struct wl_output *output)
4138{
Rob Bradford7507b572012-05-15 17:55:34 +01004139 struct window *window = data;
4140 struct window_output *window_output;
4141 struct window_output *window_output_found = NULL;
4142
4143 wl_list_for_each(window_output, &window->window_output_list, link) {
4144 if (window_output->output->output == output) {
4145 window_output_found = window_output;
4146 break;
4147 }
4148 }
4149
4150 if (window_output_found) {
4151 wl_list_remove(&window_output_found->link);
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004152
4153 if (window->output_handler)
4154 window->output_handler(window, window_output->output,
4155 0, window->user_data);
4156
Rob Bradford7507b572012-05-15 17:55:34 +01004157 free(window_output_found);
4158 }
Casey Dahlin9074db52012-04-19 22:50:09 -04004159}
4160
4161static const struct wl_surface_listener surface_listener = {
4162 surface_enter,
4163 surface_leave
4164};
4165
Pekka Paalanen4e373742013-02-13 16:17:13 +02004166static struct surface *
4167surface_create(struct window *window)
4168{
4169 struct display *display = window->display;
4170 struct surface *surface;
4171
4172 surface = calloc(1, sizeof *surface);
4173 if (!surface)
4174 return NULL;
4175
4176 surface->window = window;
4177 surface->surface = wl_compositor_create_surface(display->compositor);
Alexander Larsson5e9b6522013-05-22 14:41:28 +02004178 surface->buffer_scale = 1;
Pekka Paalanen4e373742013-02-13 16:17:13 +02004179 wl_surface_add_listener(surface->surface, &surface_listener, window);
4180
Pekka Paalanen35e82632013-04-25 13:57:48 +03004181 wl_list_insert(&window->subsurface_list, &surface->link);
4182
Pekka Paalanen4e373742013-02-13 16:17:13 +02004183 return surface;
4184}
4185
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004186static struct window *
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004187window_create_internal(struct display *display,
4188 struct window *parent, int type)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05004189{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004190 struct window *window;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004191 struct surface *surface;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05004192
Kristian Høgsbergce278412013-07-25 15:20:20 -07004193 window = xmalloc(sizeof *window);
Kristian Høgsberg78231c82008-11-08 15:06:01 -05004194 memset(window, 0, sizeof *window);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004195 wl_list_init(&window->subsurface_list);
Kristian Høgsberg40979232008-11-25 22:40:39 -05004196 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004197 window->parent = parent;
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004198
4199 surface = surface_create(window);
4200 window->main_surface = surface;
4201
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004202 if (type != TYPE_CUSTOM && display->shell) {
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004203 window->shell_surface =
4204 wl_shell_get_shell_surface(display->shell,
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004205 surface->surface);
Kristian Høgsbergce278412013-07-25 15:20:20 -07004206 fail_on_null(window->shell_surface);
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004207 }
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02004208
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004209 window->type = type;
Ander Conselvan de Oliveira5403f522012-12-14 13:37:23 -02004210 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
MoD063a8822013-03-02 23:43:57 +00004211 window->configure_requests = 0;
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05004212
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05004213 if (display->argb_device)
Benjamin Franzke22d54812011-07-16 19:50:32 +00004214#ifdef HAVE_CAIRO_EGL
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004215 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004216#else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004217 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Benjamin Franzke22d54812011-07-16 19:50:32 +00004218#endif
Yuval Fledel45568f62010-12-06 09:18:12 -05004219 else
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004220 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004221
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004222 wl_surface_set_user_data(surface->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004223 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg84b76c72012-04-13 12:01:18 -04004224 wl_list_init(&window->redraw_task.link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004225
Pekka Paalanen6b2dc912011-11-29 10:25:08 +02004226 if (window->shell_surface) {
4227 wl_shell_surface_set_user_data(window->shell_surface, window);
4228 wl_shell_surface_add_listener(window->shell_surface,
4229 &shell_surface_listener, window);
4230 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02004231
Rob Bradford7507b572012-05-15 17:55:34 +01004232 wl_list_init (&window->window_output_list);
4233
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004234 return window;
4235}
4236
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004237struct window *
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004238window_create(struct display *display)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004239{
Kristian Høgsberg41624832013-07-25 16:53:10 -07004240 return window_create_internal(display, NULL, TYPE_NONE);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004241}
4242
4243struct window *
4244window_create_custom(struct display *display)
4245{
Kristian Høgsbergce278412013-07-25 15:20:20 -07004246 return window_create_internal(display, NULL, TYPE_CUSTOM);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004247}
4248
4249struct window *
4250window_create_transient(struct display *display, struct window *parent,
Tiago Vignattidec76582012-05-21 16:47:46 +03004251 int32_t x, int32_t y, uint32_t flags)
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004252{
4253 struct window *window;
4254
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004255 window = window_create_internal(parent->display,
4256 parent, TYPE_TRANSIENT);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004257
4258 window->x = x;
4259 window->y = y;
4260
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004261 if (display->shell)
Pekka Paalanen4e373742013-02-13 16:17:13 +02004262 wl_shell_surface_set_transient(
4263 window->shell_surface,
4264 window->parent->main_surface->surface,
4265 window->x, window->y, flags);
Kristian Høgsberg1517def2012-02-16 22:56:12 -05004266
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004267 return window;
4268}
4269
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004270static void
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -05004271menu_set_item(struct menu *menu, int sy)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004272{
4273 int next;
4274
4275 next = (sy - 8) / 20;
4276 if (menu->current != next) {
4277 menu->current = next;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004278 widget_schedule_redraw(menu->widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004279 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004280}
4281
4282static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05004283menu_motion_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004284 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004285 float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004286{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004287 struct menu *menu = data;
4288
4289 if (widget == menu->widget)
4290 menu_set_item(data, y);
4291
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004292 return CURSOR_LEFT_PTR;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004293}
4294
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -05004295static int
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004296menu_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04004297 struct input *input, float x, float y, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004298{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004299 struct menu *menu = data;
4300
4301 if (widget == menu->widget)
4302 menu_set_item(data, y);
4303
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03004304 return CURSOR_LEFT_PTR;
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004305}
4306
4307static void
4308menu_leave_handler(struct widget *widget, struct input *input, void *data)
4309{
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004310 struct menu *menu = data;
4311
4312 if (widget == menu->widget)
4313 menu_set_item(data, -200);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004314}
4315
4316static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05004317menu_button_handler(struct widget *widget,
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004318 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01004319 uint32_t button, enum wl_pointer_button_state state,
4320 void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004321
4322{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004323 struct menu *menu = data;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004324
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004325 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4326 (menu->release_count > 0 || time - menu->time > 500)) {
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004327 /* Either relase after press-drag-release or
4328 * click-motion-click. */
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004329 menu->func(menu->window->parent,
4330 menu->current, menu->window->parent->user_data);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004331 input_ungrab(input);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004332 menu_destroy(menu);
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004333 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004334 menu->release_count++;
Kristian Høgsberge77d7572012-10-30 18:10:30 -04004335 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004336}
4337
4338static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004339menu_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004340{
4341 cairo_t *cr;
4342 const int32_t r = 3, margin = 3;
4343 struct menu *menu = data;
4344 int32_t width, height, i;
4345
Pekka Paalanen0c4445b2013-02-13 16:17:23 +02004346 cr = widget_cairo_create(widget);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004347 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
4348 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
4349 cairo_paint(cr);
4350
Pekka Paalanen0a9686f2013-02-13 16:17:22 +02004351 width = widget->allocation.width;
4352 height = widget->allocation.height;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004353 rounded_rect(cr, 0, 0, width, height, r);
Kristian Høgsberg824c6d02012-01-19 13:54:09 -05004354
4355 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004356 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
4357 cairo_fill(cr);
4358
4359 for (i = 0; i < menu->count; i++) {
4360 if (i == menu->current) {
4361 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4362 cairo_rectangle(cr, margin, i * 20 + margin,
4363 width - 2 * margin, 20);
4364 cairo_fill(cr);
4365 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4366 cairo_move_to(cr, 10, i * 20 + 16);
4367 cairo_show_text(cr, menu->entries[i]);
4368 } else {
4369 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4370 cairo_move_to(cr, 10, i * 20 + 16);
4371 cairo_show_text(cr, menu->entries[i]);
4372 }
4373 }
4374
4375 cairo_destroy(cr);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004376}
4377
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004378void
4379window_show_menu(struct display *display,
4380 struct input *input, uint32_t time, struct window *parent,
4381 int32_t x, int32_t y,
4382 menu_func_t func, const char **entries, int count)
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004383{
4384 struct window *window;
4385 struct menu *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004386 const int32_t margin = 3;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004387
4388 menu = malloc(sizeof *menu);
4389 if (!menu)
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004390 return;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004391
Kristian Høgsberg962342c2012-06-26 16:29:50 -04004392 window = window_create_internal(parent->display, parent, TYPE_MENU);
Martin Olsson444799a2012-07-08 03:03:40 +02004393 if (!window) {
4394 free(menu);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +02004395 return;
Martin Olsson444799a2012-07-08 03:03:40 +02004396 }
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004397
4398 menu->window = window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004399 menu->widget = window_add_widget(menu->window, menu);
Alexander Larssond68f5232013-05-22 14:41:33 +02004400 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4401 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004402 menu->entries = entries;
4403 menu->count = count;
Kristian Høgsbergd2fbb382012-10-30 13:45:22 -04004404 menu->release_count = 0;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004405 menu->current = -1;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004406 menu->time = time;
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -05004407 menu->func = func;
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004408 menu->input = input;
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004409 window->type = TYPE_MENU;
4410 window->x = x;
4411 window->y = y;
4412
Kristian Høgsberga6c8b002012-04-13 12:55:45 -04004413 input_ungrab(input);
Daniel Stone37816df2012-05-16 18:45:18 +01004414 wl_shell_surface_set_popup(window->shell_surface, input->seat,
Kristian Høgsbergf2eb68a2012-04-13 12:37:19 -04004415 display_get_serial(window->display),
Pekka Paalanen4e373742013-02-13 16:17:13 +02004416 window->parent->main_surface->surface,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05004417 window->x, window->y, 0);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004418
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05004419 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05004420 widget_set_enter_handler(menu->widget, menu_enter_handler);
4421 widget_set_leave_handler(menu->widget, menu_leave_handler);
4422 widget_set_motion_handler(menu->widget, menu_motion_handler);
4423 widget_set_button_handler(menu->widget, menu_button_handler);
Kristian Høgsberg391649b2012-01-09 09:22:30 -05004424
Kristian Høgsberg831dd522012-01-10 23:46:33 -05004425 input_grab(input, menu->widget, 0);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05004426 window_schedule_resize(window, 200, count * 20 + margin * 2);
Kristian Høgsbergbbedd7e2011-12-19 15:40:10 -05004427}
4428
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05004429void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004430window_set_buffer_type(struct window *window, enum window_buffer_type type)
4431{
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02004432 window->main_surface->buffer_type = type;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04004433}
4434
Pekka Paalanen35e82632013-04-25 13:57:48 +03004435struct widget *
4436window_add_subsurface(struct window *window, void *data,
4437 enum subsurface_mode default_mode)
4438{
4439 struct widget *widget;
4440 struct surface *surface;
4441 struct wl_surface *parent;
4442 struct wl_subcompositor *subcompo = window->display->subcompositor;
4443
4444 if (!subcompo)
4445 return NULL;
4446
4447 surface = surface_create(window);
4448 widget = widget_create(window, surface, data);
4449 wl_list_init(&widget->link);
4450 surface->widget = widget;
4451
4452 parent = window->main_surface->surface;
4453 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4454 surface->surface,
4455 parent);
4456 surface->synchronized = 1;
4457
4458 switch (default_mode) {
4459 case SUBSURFACE_SYNCHRONIZED:
4460 surface->synchronized_default = 1;
4461 break;
4462 case SUBSURFACE_DESYNCHRONIZED:
4463 surface->synchronized_default = 0;
4464 break;
4465 default:
4466 assert(!"bad enum subsurface_mode");
4467 }
4468
4469 return widget;
4470}
Kristian Høgsberg8357cd62011-05-13 13:24:56 -04004471
4472static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004473display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004474 struct wl_output *wl_output,
4475 int x, int y,
4476 int physical_width,
4477 int physical_height,
4478 int subpixel,
4479 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004480 const char *model,
4481 int transform)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004482{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004483 struct output *output = data;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004484
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004485 output->allocation.x = x;
4486 output->allocation.y = y;
Scott Moreau4e072362012-09-29 02:03:11 -06004487 output->transform = transform;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004488}
4489
4490static void
Alexander Larssonafd319a2013-05-22 14:41:27 +02004491display_handle_done(void *data,
4492 struct wl_output *wl_output)
4493{
4494}
4495
4496static void
4497display_handle_scale(void *data,
4498 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004499 int32_t scale)
Alexander Larssonafd319a2013-05-22 14:41:27 +02004500{
4501 struct output *output = data;
4502
4503 output->scale = scale;
4504}
4505
4506static void
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004507display_handle_mode(void *data,
4508 struct wl_output *wl_output,
4509 uint32_t flags,
4510 int width,
4511 int height,
4512 int refresh)
4513{
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004514 struct output *output = data;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004515 struct display *display = output->display;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004516
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004517 if (flags & WL_OUTPUT_MODE_CURRENT) {
4518 output->allocation.width = width;
4519 output->allocation.height = height;
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004520 if (display->output_configure_handler)
4521 (*display->output_configure_handler)(
4522 output, display->user_data);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004523 }
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004524}
4525
4526static const struct wl_output_listener output_listener = {
4527 display_handle_geometry,
Alexander Larssonafd319a2013-05-22 14:41:27 +02004528 display_handle_mode,
4529 display_handle_done,
4530 display_handle_scale
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004531};
4532
4533static void
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004534display_add_output(struct display *d, uint32_t id)
4535{
4536 struct output *output;
4537
4538 output = malloc(sizeof *output);
4539 if (output == NULL)
4540 return;
4541
4542 memset(output, 0, sizeof *output);
4543 output->display = d;
Alexander Larssonafd319a2013-05-22 14:41:27 +02004544 output->scale = 1;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004545 output->output =
Alexander Larssonafd319a2013-05-22 14:41:27 +02004546 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004547 wl_list_insert(d->output_list.prev, &output->link);
4548
4549 wl_output_add_listener(output->output, &output_listener, output);
4550}
4551
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02004552static void
4553output_destroy(struct output *output)
4554{
4555 if (output->destroy_handler)
4556 (*output->destroy_handler)(output, output->user_data);
4557
4558 wl_output_destroy(output->output);
4559 wl_list_remove(&output->link);
4560 free(output);
4561}
4562
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004563void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004564display_set_global_handler(struct display *display,
4565 display_global_handler_t handler)
4566{
4567 struct global *global;
4568
4569 display->global_handler = handler;
4570 if (!handler)
4571 return;
4572
4573 wl_list_for_each(global, &display->global_list, link)
4574 display->global_handler(display,
4575 global->name, global->interface,
4576 global->version, display->user_data);
4577}
4578
4579void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004580display_set_output_configure_handler(struct display *display,
4581 display_output_handler_t handler)
4582{
4583 struct output *output;
4584
4585 display->output_configure_handler = handler;
4586 if (!handler)
4587 return;
4588
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004589 wl_list_for_each(output, &display->output_list, link) {
4590 if (output->allocation.width == 0 &&
4591 output->allocation.height == 0)
4592 continue;
4593
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004594 (*display->output_configure_handler)(output,
4595 display->user_data);
Pekka Paalanenb2f957a2012-10-15 12:06:53 +03004596 }
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004597}
4598
4599void
4600output_set_user_data(struct output *output, void *data)
4601{
4602 output->user_data = data;
4603}
4604
4605void *
4606output_get_user_data(struct output *output)
4607{
4608 return output->user_data;
4609}
4610
4611void
4612output_set_destroy_handler(struct output *output,
4613 display_output_handler_t handler)
4614{
4615 output->destroy_handler = handler;
4616 /* FIXME: implement this, once we have way to remove outputs */
4617}
4618
4619void
Scott Moreau4e072362012-09-29 02:03:11 -06004620output_get_allocation(struct output *output, struct rectangle *base)
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004621{
Scott Moreau4e072362012-09-29 02:03:11 -06004622 struct rectangle allocation = output->allocation;
4623
4624 switch (output->transform) {
4625 case WL_OUTPUT_TRANSFORM_90:
4626 case WL_OUTPUT_TRANSFORM_270:
4627 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4628 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4629 /* Swap width and height */
4630 allocation.width = output->allocation.height;
4631 allocation.height = output->allocation.width;
4632 break;
4633 }
4634
4635 *base = allocation;
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004636}
4637
Pekka Paalanen999c5b52011-11-30 10:52:38 +02004638struct wl_output *
4639output_get_wl_output(struct output *output)
4640{
4641 return output->output;
4642}
4643
Ander Conselvan de Oliveira15256f62012-11-30 17:34:25 +02004644enum wl_output_transform
4645output_get_transform(struct output *output)
4646{
4647 return output->transform;
4648}
4649
Alexander Larssonafd319a2013-05-22 14:41:27 +02004650uint32_t
4651output_get_scale(struct output *output)
4652{
4653 return output->scale;
4654}
4655
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004656static void
Daniel Stone97f68542012-05-30 16:32:01 +01004657fini_xkb(struct input *input)
4658{
4659 xkb_state_unref(input->xkb.state);
4660 xkb_map_unref(input->xkb.keymap);
4661}
4662
4663static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004664display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004665{
4666 struct input *input;
4667
4668 input = malloc(sizeof *input);
4669 if (input == NULL)
4670 return;
4671
4672 memset(input, 0, sizeof *input);
4673 input->display = d;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004674 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004675 input->pointer_focus = NULL;
4676 input->keyboard_focus = NULL;
4677 wl_list_insert(d->input_list.prev, &input->link);
4678
Daniel Stone37816df2012-05-16 18:45:18 +01004679 wl_seat_add_listener(input->seat, &seat_listener, input);
4680 wl_seat_set_user_data(input->seat, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004681
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004682 input->data_device =
4683 wl_data_device_manager_get_data_device(d->data_device_manager,
Daniel Stone37816df2012-05-16 18:45:18 +01004684 input->seat);
4685 wl_data_device_add_listener(input->data_device, &data_device_listener,
4686 input);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004687
4688 input->pointer_surface = wl_compositor_create_surface(d->compositor);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004689
Kristian Høgsberg8b19c642012-06-20 18:00:13 -04004690 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4691 TFD_CLOEXEC | TFD_NONBLOCK);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004692 input->repeat_task.run = keyboard_repeat_func;
4693 display_watch_fd(d, input->repeat_timer_fd,
4694 EPOLLIN, &input->repeat_task);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05004695}
4696
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004697static void
Pekka Paalanene1207c72011-12-16 12:02:09 +02004698input_destroy(struct input *input)
4699{
Kristian Høgsberg8a1d10d2011-12-21 17:11:45 -05004700 input_remove_keyboard_focus(input);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04004701 input_remove_pointer_focus(input);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004702
4703 if (input->drag_offer)
4704 data_offer_destroy(input->drag_offer);
4705
4706 if (input->selection_offer)
4707 data_offer_destroy(input->selection_offer);
4708
4709 wl_data_device_destroy(input->data_device);
Daniel Stone97f68542012-05-30 16:32:01 +01004710 fini_xkb(input);
4711
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03004712 wl_surface_destroy(input->pointer_surface);
4713
Pekka Paalanene1207c72011-12-16 12:02:09 +02004714 wl_list_remove(&input->link);
Daniel Stone37816df2012-05-16 18:45:18 +01004715 wl_seat_destroy(input->seat);
Kristian Høgsbergcf4d2442012-06-20 14:52:12 -04004716 close(input->repeat_timer_fd);
Pekka Paalanene1207c72011-12-16 12:02:09 +02004717 free(input);
4718}
4719
4720static void
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004721init_workspace_manager(struct display *d, uint32_t id)
4722{
4723 d->workspace_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004724 wl_registry_bind(d->registry, id,
4725 &workspace_manager_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004726 if (d->workspace_manager != NULL)
4727 workspace_manager_add_listener(d->workspace_manager,
4728 &workspace_manager_listener,
4729 d);
4730}
4731
4732static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004733registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4734 const char *interface, uint32_t version)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004735{
4736 struct display *d = data;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004737 struct global *global;
4738
4739 global = malloc(sizeof *global);
4740 global->name = id;
4741 global->interface = strdup(interface);
4742 global->version = version;
4743 wl_list_insert(d->global_list.prev, &global->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004744
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004745 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004746 d->compositor = wl_registry_bind(registry, id,
Jason Ekstrandd27cb092013-06-26 22:20:31 -05004747 &wl_compositor_interface, 3);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004748 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004749 display_add_output(d, id);
Daniel Stone37816df2012-05-16 18:45:18 +01004750 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04004751 display_add_input(d, id);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004752 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004753 d->shell = wl_registry_bind(registry,
4754 id, &wl_shell_interface, 1);
Kristian Høgsberg7cbdb642011-04-20 18:53:07 -04004755 } else if (strcmp(interface, "wl_shm") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004756 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04004757 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4758 d->data_device_manager =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004759 wl_registry_bind(registry, id,
4760 &wl_data_device_manager_interface, 1);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06004761 } else if (strcmp(interface, "text_cursor_position") == 0) {
4762 d->text_cursor_position =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004763 wl_registry_bind(registry, id,
4764 &text_cursor_position_interface, 1);
Jonas Ådahl14c92ff2012-08-29 22:13:02 +02004765 } else if (strcmp(interface, "workspace_manager") == 0) {
4766 init_workspace_manager(d, id);
Pekka Paalanen35e82632013-04-25 13:57:48 +03004767 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4768 d->subcompositor =
4769 wl_registry_bind(registry, id,
4770 &wl_subcompositor_interface, 1);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004771 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004772
4773 if (d->global_handler)
4774 d->global_handler(d, id, interface, version, d->user_data);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004775}
4776
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004777static void
4778registry_handle_global_remove(void *data, struct wl_registry *registry,
4779 uint32_t name)
4780{
4781 struct display *d = data;
4782 struct global *global;
4783 struct global *tmp;
4784
4785 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4786 if (global->name != name)
4787 continue;
4788
4789 /* XXX: Should destroy bound globals, and call
4790 * the counterpart of display::global_handler
4791 */
4792 wl_list_remove(&global->link);
4793 free(global->interface);
4794 free(global);
4795 }
4796}
4797
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004798void *
4799display_bind(struct display *display, uint32_t name,
4800 const struct wl_interface *interface, uint32_t version)
4801{
4802 return wl_registry_bind(display->registry, name, interface, version);
4803}
4804
4805static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +02004806 registry_handle_global,
4807 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004808};
4809
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004810#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05004811static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05004812init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05004813{
4814 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004815 EGLint n;
Kristian Høgsbergf389cac2011-08-31 16:21:38 -04004816
Rob Clark6396ed32012-03-11 19:48:41 -05004817#ifdef USE_CAIRO_GLESV2
4818# define GL_BIT EGL_OPENGL_ES2_BIT
4819#else
4820# define GL_BIT EGL_OPENGL_BIT
4821#endif
4822
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004823 static const EGLint argb_cfg_attribs[] = {
Kristian Høgsberg31467562012-10-16 15:31:31 -04004824 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004825 EGL_RED_SIZE, 1,
4826 EGL_GREEN_SIZE, 1,
4827 EGL_BLUE_SIZE, 1,
4828 EGL_ALPHA_SIZE, 1,
4829 EGL_DEPTH_SIZE, 1,
Rob Clark6396ed32012-03-11 19:48:41 -05004830 EGL_RENDERABLE_TYPE, GL_BIT,
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004831 EGL_NONE
4832 };
Yuval Fledel45568f62010-12-06 09:18:12 -05004833
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004834#ifdef USE_CAIRO_GLESV2
4835 static const EGLint context_attribs[] = {
4836 EGL_CONTEXT_CLIENT_VERSION, 2,
4837 EGL_NONE
4838 };
4839 EGLint api = EGL_OPENGL_ES_API;
4840#else
4841 EGLint *context_attribs = NULL;
4842 EGLint api = EGL_OPENGL_API;
4843#endif
4844
Kristian Høgsberg91342c62011-04-14 14:44:58 -04004845 d->dpy = eglGetDisplay(d->display);
Yuval Fledel45568f62010-12-06 09:18:12 -05004846 if (!eglInitialize(d->dpy, &major, &minor)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004847 fprintf(stderr, "failed to initialize EGL\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004848 return -1;
4849 }
4850
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004851 if (!eglBindAPI(api)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004852 fprintf(stderr, "failed to bind EGL client API\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004853 return -1;
4854 }
4855
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004856 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4857 &d->argb_config, 1, &n) || n != 1) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004858 fprintf(stderr, "failed to choose argb EGL config\n");
Benjamin Franzke6693ac22011-02-10 12:04:30 +01004859 return -1;
4860 }
4861
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05004862 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
Kristian Høgsberg2d574392012-01-18 14:50:58 -05004863 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzke0c991632011-09-27 21:57:31 +02004864 if (d->argb_ctx == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004865 fprintf(stderr, "failed to create EGL context\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004866 return -1;
4867 }
4868
Kristian Høgsberg067fd602012-02-29 16:15:53 -05004869 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004870 fprintf(stderr, "failed to make EGL context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05004871 return -1;
4872 }
4873
Benjamin Franzke0c991632011-09-27 21:57:31 +02004874 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4875 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004876 fprintf(stderr, "failed to get cairo EGL argb device\n");
Benjamin Franzke0c991632011-09-27 21:57:31 +02004877 return -1;
4878 }
Yuval Fledel45568f62010-12-06 09:18:12 -05004879
4880 return 0;
4881}
4882
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004883static void
4884fini_egl(struct display *display)
4885{
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004886 cairo_device_destroy(display->argb_device);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004887
4888 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4889 EGL_NO_CONTEXT);
4890
4891 eglTerminate(display->dpy);
4892 eglReleaseThread();
4893}
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04004894#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02004895
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004896static void
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02004897init_dummy_surface(struct display *display)
4898{
4899 int len;
4900 void *data;
4901
4902 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4903 data = malloc(len);
4904 display->dummy_surface =
4905 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4906 1, 1, len);
4907 display->dummy_surface_data = data;
4908}
4909
4910static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004911handle_display_data(struct task *task, uint32_t events)
4912{
4913 struct display *display =
4914 container_of(task, struct display, display_task);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004915 struct epoll_event ep;
4916 int ret;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004917
4918 display->display_fd_events = events;
4919
4920 if (events & EPOLLERR || events & EPOLLHUP) {
4921 display_exit(display);
4922 return;
4923 }
4924
Kristian Høgsberga17f7a12012-10-16 13:16:10 -04004925 if (events & EPOLLIN) {
4926 ret = wl_display_dispatch(display->display);
4927 if (ret == -1) {
4928 display_exit(display);
4929 return;
4930 }
4931 }
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004932
4933 if (events & EPOLLOUT) {
4934 ret = wl_display_flush(display->display);
4935 if (ret == 0) {
4936 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4937 ep.data.ptr = &display->display_task;
4938 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4939 display->display_fd, &ep);
4940 } else if (ret == -1 && errno != EAGAIN) {
4941 display_exit(display);
4942 return;
4943 }
4944 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004945}
4946
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004947static void
4948log_handler(const char *format, va_list args)
4949{
4950 vfprintf(stderr, format, args);
4951}
4952
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004953struct display *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004954display_create(int *argc, char *argv[])
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004955{
4956 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04004957
Kristian Høgsberg2e437202013-04-16 11:21:48 -04004958 wl_log_set_handler_client(log_handler);
4959
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05004960 d = malloc(sizeof *d);
4961 if (d == NULL)
4962 return NULL;
4963
Tim Wiederhake81bd9792011-01-23 23:25:26 +01004964 memset(d, 0, sizeof *d);
4965
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004966 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04004967 if (d->display == NULL) {
Pekka Paalanen4e106542013-02-14 11:49:12 +02004968 fprintf(stderr, "failed to connect to Wayland display: %m\n");
Rob Bradfordf0a1af92013-01-10 19:48:54 +00004969 free(d);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04004970 return NULL;
4971 }
4972
Pekka Paalanen647f2bf2012-05-30 15:53:43 +03004973 d->epoll_fd = os_epoll_create_cloexec();
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004974 d->display_fd = wl_display_get_fd(d->display);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004975 d->display_task.run = handle_display_data;
U. Artie Eoff44874d92012-10-02 21:12:35 -07004976 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4977 &d->display_task);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04004978
4979 wl_list_init(&d->deferred_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004980 wl_list_init(&d->input_list);
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05004981 wl_list_init(&d->output_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004982 wl_list_init(&d->global_list);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04004983
Daniel Stone97f68542012-05-30 16:32:01 +01004984 d->xkb_context = xkb_context_new(0);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01004985 if (d->xkb_context == NULL) {
4986 fprintf(stderr, "Failed to create XKB context\n");
Daniel Stone97f68542012-05-30 16:32:01 +01004987 return NULL;
4988 }
4989
Jonas Ådahlf77beeb2012-10-08 22:49:04 +02004990 d->workspace = 0;
4991 d->workspace_count = 1;
4992
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04004993 d->registry = wl_display_get_registry(d->display);
4994 wl_registry_add_listener(d->registry, &registry_listener, d);
Pekka Paalanen33a68ea2013-02-14 12:18:00 +02004995
4996 if (wl_display_dispatch(d->display) < 0) {
4997 fprintf(stderr, "Failed to process Wayland connection: %m\n");
4998 return NULL;
4999 }
5000
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005001#ifdef HAVE_CAIRO_EGL
Pekka Paalanen4e106542013-02-14 11:49:12 +02005002 if (init_egl(d) < 0)
5003 fprintf(stderr, "EGL does not seem to work, "
5004 "falling back to software rendering and wl_shm.\n");
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005005#endif
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05005006
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005007 create_cursors(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04005008
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005009 d->theme = theme_create();
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04005010
Kristian Høgsberg478d9262010-06-08 20:34:11 -04005011 wl_list_init(&d->window_list);
5012
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005013 init_dummy_surface(d);
5014
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005015 return d;
5016}
5017
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005018static void
5019display_destroy_outputs(struct display *display)
5020{
5021 struct output *tmp;
5022 struct output *output;
5023
5024 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5025 output_destroy(output);
5026}
5027
Pekka Paalanene1207c72011-12-16 12:02:09 +02005028static void
5029display_destroy_inputs(struct display *display)
5030{
5031 struct input *tmp;
5032 struct input *input;
5033
5034 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5035 input_destroy(input);
5036}
5037
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005038void
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005039display_destroy(struct display *display)
5040{
Pekka Paalanenc2052982011-12-16 11:41:32 +02005041 if (!wl_list_empty(&display->window_list))
U. Artie Eoff44874d92012-10-02 21:12:35 -07005042 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5043 wl_list_length(&display->window_list));
Pekka Paalanenc2052982011-12-16 11:41:32 +02005044
5045 if (!wl_list_empty(&display->deferred_list))
5046 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5047
Pekka Paalanen3cbb0892012-11-19 17:16:01 +02005048 cairo_surface_destroy(display->dummy_surface);
5049 free(display->dummy_surface_data);
5050
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005051 display_destroy_outputs(display);
Pekka Paalanene1207c72011-12-16 12:02:09 +02005052 display_destroy_inputs(display);
Pekka Paalanen2c1426a2011-12-16 11:35:34 +02005053
Daniel Stone97f68542012-05-30 16:32:01 +01005054 xkb_context_unref(display->xkb_context);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005055
Kristian Høgsberg5adb4802012-05-15 22:25:28 -04005056 theme_destroy(display->theme);
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +03005057 destroy_cursors(display);
Pekka Paalanen325bb602011-12-19 10:31:45 +02005058
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005059#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg4e51b442013-01-07 15:47:14 -05005060 if (display->argb_device)
5061 fini_egl(display);
Kristian Høgsberg8f64ed02012-04-03 11:57:44 -04005062#endif
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005063
Pekka Paalanen35e82632013-04-25 13:57:48 +03005064 if (display->subcompositor)
5065 wl_subcompositor_destroy(display->subcompositor);
5066
Pekka Paalanenc2052982011-12-16 11:41:32 +02005067 if (display->shell)
5068 wl_shell_destroy(display->shell);
5069
5070 if (display->shm)
5071 wl_shm_destroy(display->shm);
5072
5073 if (display->data_device_manager)
5074 wl_data_device_manager_destroy(display->data_device_manager);
5075
5076 wl_compositor_destroy(display->compositor);
Pekka Paalanenaac1c132012-12-04 16:01:15 +02005077 wl_registry_destroy(display->registry);
Pekka Paalanenc2052982011-12-16 11:41:32 +02005078
5079 close(display->epoll_fd);
5080
U. Artie Eoff44874d92012-10-02 21:12:35 -07005081 if (!(display->display_fd_events & EPOLLERR) &&
5082 !(display->display_fd_events & EPOLLHUP))
5083 wl_display_flush(display->display);
5084
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -05005085 wl_display_disconnect(display->display);
Pekka Paalanenfe6079a2011-12-15 15:20:04 +02005086 free(display);
5087}
5088
5089void
Pekka Paalanen999c5b52011-11-30 10:52:38 +02005090display_set_user_data(struct display *display, void *data)
5091{
5092 display->user_data = data;
5093}
5094
5095void *
5096display_get_user_data(struct display *display)
5097{
5098 return display->user_data;
5099}
5100
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04005101struct wl_display *
5102display_get_display(struct display *display)
5103{
5104 return display->display;
5105}
5106
Kristian Høgsberg1cc5ac32013-04-11 21:47:41 -04005107cairo_device_t *
5108display_get_cairo_device(struct display *display)
5109{
5110 return display->argb_device;
5111}
5112
Kristian Høgsberg53ff2f62011-11-26 17:27:37 -05005113struct output *
5114display_get_output(struct display *display)
5115{
5116 return container_of(display->output_list.next, struct output, link);
5117}
5118
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05005119struct wl_compositor *
5120display_get_compositor(struct display *display)
5121{
5122 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05005123}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005124
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04005125uint32_t
5126display_get_serial(struct display *display)
5127{
5128 return display->serial;
5129}
5130
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005131EGLDisplay
5132display_get_egl_display(struct display *d)
5133{
5134 return d->dpy;
5135}
5136
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04005137struct wl_data_source *
5138display_create_data_source(struct display *display)
5139{
5140 return wl_data_device_manager_create_data_source(display->data_device_manager);
5141}
5142
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005143EGLConfig
Benjamin Franzke0c991632011-09-27 21:57:31 +02005144display_get_argb_egl_config(struct display *d)
5145{
Kristian Høgsberg8e81df42012-01-11 14:24:46 -05005146 return d->argb_config;
Benjamin Franzke0c991632011-09-27 21:57:31 +02005147}
5148
Kristian Høgsberg58eec362011-01-19 14:27:42 -05005149struct wl_shell *
5150display_get_shell(struct display *display)
5151{
5152 return display->shell;
5153}
5154
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005155int
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005156display_acquire_window_surface(struct display *display,
5157 struct window *window,
5158 EGLContext ctx)
5159{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005160 struct surface *surface = window->main_surface;
5161
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005162 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke1a89f282011-10-07 09:33:06 +02005163 return -1;
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005164
Pekka Paalanen6f41b072013-02-20 13:39:17 +02005165 widget_get_cairo_surface(window->main_surface->widget);
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005166 return surface->toysurface->acquire(surface->toysurface, ctx);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005167}
5168
5169void
Benjamin Franzke0c991632011-09-27 21:57:31 +02005170display_release_window_surface(struct display *display,
5171 struct window *window)
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005172{
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005173 struct surface *surface = window->main_surface;
5174
Pekka Paalanen02bba7c2013-02-13 16:17:15 +02005175 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
Benjamin Franzke0c991632011-09-27 21:57:31 +02005176 return;
5177
Pekka Paalanen811ec4f2013-02-13 16:17:14 +02005178 surface->toysurface->release(surface->toysurface);
Benjamin Franzkecff904e2011-02-18 23:00:55 +01005179}
5180
5181void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005182display_defer(struct display *display, struct task *task)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005183{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005184 wl_list_insert(&display->deferred_list, &task->link);
5185}
5186
5187void
5188display_watch_fd(struct display *display,
5189 int fd, uint32_t events, struct task *task)
5190{
5191 struct epoll_event ep;
5192
5193 ep.events = events;
5194 ep.data.ptr = task;
5195 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5196}
5197
5198void
Dima Ryazanova85292e2012-11-29 00:27:09 -08005199display_unwatch_fd(struct display *display, int fd)
5200{
5201 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5202}
5203
5204void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005205display_run(struct display *display)
5206{
5207 struct task *task;
5208 struct epoll_event ep[16];
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005209 int i, count, ret;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005210
Pekka Paalanen826d7952011-12-15 10:14:07 +02005211 display->running = 1;
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005212 while (1) {
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005213 while (!wl_list_empty(&display->deferred_list)) {
Tiago Vignatti6f093382012-09-27 14:46:23 +03005214 task = container_of(display->deferred_list.prev,
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005215 struct task, link);
5216 wl_list_remove(&task->link);
5217 task->run(task, 0);
5218 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005219
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04005220 wl_display_dispatch_pending(display->display);
5221
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005222 if (!display->running)
5223 break;
5224
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04005225 ret = wl_display_flush(display->display);
5226 if (ret < 0 && errno == EAGAIN) {
5227 ep[0].events =
5228 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5229 ep[0].data.ptr = &display->display_task;
5230
5231 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5232 display->display_fd, &ep[0]);
5233 } else if (ret < 0) {
5234 break;
5235 }
Kristian Høgsberg9ca2d082012-01-09 18:48:14 -05005236
5237 count = epoll_wait(display->epoll_fd,
5238 ep, ARRAY_LENGTH(ep), -1);
5239 for (i = 0; i < count; i++) {
5240 task = ep[i].data.ptr;
5241 task->run(task, ep[i].events);
5242 }
Kristian Høgsberg3a696272011-09-14 17:33:48 -04005243 }
Kristian Høgsberg7824d812010-06-08 14:59:44 -04005244}
Pekka Paalanen826d7952011-12-15 10:14:07 +02005245
5246void
5247display_exit(struct display *display)
5248{
5249 display->running = 0;
5250}
Jan Arne Petersencd997062012-11-18 19:06:44 +01005251
5252void
5253keysym_modifiers_add(struct wl_array *modifiers_map,
5254 const char *name)
5255{
5256 size_t len = strlen(name) + 1;
5257 char *p;
5258
5259 p = wl_array_add(modifiers_map, len);
5260
5261 if (p == NULL)
5262 return;
5263
5264 strncpy(p, name, len);
5265}
5266
5267static xkb_mod_index_t
5268keysym_modifiers_get_index(struct wl_array *modifiers_map,
5269 const char *name)
5270{
5271 xkb_mod_index_t index = 0;
5272 char *p = modifiers_map->data;
5273
5274 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5275 if (strcmp(p, name) == 0)
5276 return index;
5277
5278 index++;
5279 p += strlen(p) + 1;
5280 }
5281
5282 return XKB_MOD_INVALID;
5283}
5284
5285xkb_mod_mask_t
5286keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5287 const char *name)
5288{
5289 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5290
5291 if (index == XKB_MOD_INVALID)
5292 return XKB_MOD_INVALID;
5293
5294 return 1 << index;
5295}
Kristian Høgsbergce278412013-07-25 15:20:20 -07005296
5297void *
5298fail_on_null(void *p)
5299{
5300 if (p == NULL) {
5301 fprintf(stderr, "wayland-scanner: out of memory\n");
5302 exit(EXIT_FAILURE);
5303 }
5304
5305 return p;
5306}
5307
5308void *
5309xmalloc(size_t s)
5310{
5311 return fail_on_null(malloc(s));
5312}
5313
5314char *
5315xstrdup(const char *s)
5316{
5317 return fail_on_null(strdup(s));
5318}