blob: db07af3c81de29b4f6d5f738fc844c81c3623fa8 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040023#include "../config.h"
24
Kristian Høgsberg61017b12008-11-02 18:51:48 -050025#include <stdint.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050029#include <fcntl.h>
30#include <unistd.h>
31#include <math.h>
32#include <time.h>
33#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050034#include <glib.h>
Kristian Høgsberg478d9262010-06-08 20:34:11 -040035#include <glib-object.h>
Kristian Høgsbergda275dd2010-08-16 17:47:07 -040036#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040037#include <sys/mman.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040038
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -050039#include <wayland-egl.h>
40
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040041#include <GL/gl.h>
42#include <EGL/egl.h>
43#include <EGL/eglext.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040044
Kristian Høgsberg8def2642011-01-14 17:41:33 -050045#ifdef HAVE_CAIRO_EGL
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040046#include <cairo-gl.h>
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040047#endif
Kristian Høgsberg61017b12008-11-02 18:51:48 -050048
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040049#include <X11/extensions/XKBcommon.h>
50
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050051#include <linux/input.h>
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050052#include "wayland-util.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050053#include "wayland-client.h"
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050054#include "wayland-glib.h"
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040055#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050056
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050057#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050058
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050059struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050060 struct wl_display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -050061 struct wl_egl_display *native_dpy;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050062 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040063 struct wl_shell *shell;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -040064 struct wl_shm *shm;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050065 struct wl_output *output;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050066 struct rectangle screen_allocation;
Kristian Høgsberg640609a2010-08-09 22:11:47 -040067 int authenticated;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040068 EGLDisplay dpy;
Benjamin Franzke6693ac22011-02-10 12:04:30 +010069 EGLConfig conf;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040070 EGLContext ctx;
Janusz Lewandowskid923e9d2010-01-31 03:01:26 +010071 cairo_device_t *device;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050072 int fd;
Kristian Høgsberg7824d812010-06-08 14:59:44 -040073 GMainLoop *loop;
74 GSource *source;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040075 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040076 struct wl_list input_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040077 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040078 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040079 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040080 cairo_surface_t **pointer_surfaces;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040081
Kristian Høgsbergb46df052011-01-18 09:17:57 -050082 display_global_handler_t global_handler;
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -050083
84 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
85 PFNEGLCREATEIMAGEKHRPROC create_image;
86 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050087};
88
89struct window {
90 struct display *display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050091 struct window *parent;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050092 struct wl_surface *surface;
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -050093 char *title;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -050094 struct rectangle allocation, saved_allocation, server_allocation;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -050095 int x, y;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040096 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040097 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050098 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050099 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500100 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400101 int decoration;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400102 struct input *grab_device;
103 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500104 uint32_t name;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400105 enum window_buffer_type buffer_type;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500106
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400107 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500108 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500109
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500110 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400111 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500112 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400113 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500114 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400115 window_motion_handler_t motion_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400116
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500117 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400118 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500119};
120
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400121struct input {
122 struct display *display;
123 struct wl_input_device *input_device;
124 struct window *pointer_focus;
125 struct window *keyboard_focus;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500126 struct selection_offer *offer;
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400127 uint32_t current_pointer_image;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400128 uint32_t modifiers;
129 int32_t x, y, sx, sy;
130 struct wl_list link;
131};
132
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400133enum {
134 POINTER_DEFAULT = 100,
135 POINTER_UNSET
136};
137
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500138enum window_location {
139 WINDOW_INTERIOR = 0,
140 WINDOW_RESIZING_TOP = 1,
141 WINDOW_RESIZING_BOTTOM = 2,
142 WINDOW_RESIZING_LEFT = 4,
143 WINDOW_RESIZING_TOP_LEFT = 5,
144 WINDOW_RESIZING_BOTTOM_LEFT = 6,
145 WINDOW_RESIZING_RIGHT = 8,
146 WINDOW_RESIZING_TOP_RIGHT = 9,
147 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
148 WINDOW_RESIZING_MASK = 15,
149 WINDOW_EXTERIOR = 16,
150 WINDOW_TITLEBAR = 17,
151 WINDOW_CLIENT_AREA = 18,
152};
153
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400154const char *option_xkb_layout = "us";
155const char *option_xkb_variant = "";
156const char *option_xkb_options = "";
157
158static const GOptionEntry xkb_option_entries[] = {
159 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
160 &option_xkb_layout, "XKB Layout" },
161 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
162 &option_xkb_variant, "XKB Variant" },
163 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
164 &option_xkb_options, "XKB Options" },
165 { NULL }
166};
167
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500168static void
169rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
170{
171 cairo_move_to(cr, x0, y0 + radius);
172 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
173 cairo_line_to(cr, x1 - radius, y0);
174 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
175 cairo_line_to(cr, x1, y1 - radius);
176 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
177 cairo_line_to(cr, x0 + radius, y1);
178 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
179 cairo_close_path(cr);
180}
181
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400182static const cairo_user_data_key_t surface_data_key;
183struct surface_data {
184 struct wl_buffer *buffer;
185};
186
Kristian Høgsberg1f5d5072010-11-29 08:13:35 -0500187#define MULT(_d,c,a,t) \
188 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
189
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500190#ifdef HAVE_CAIRO_EGL
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400191
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100192struct egl_window_surface_data {
193 struct display *display;
194 struct wl_surface *surface;
195 struct wl_egl_window *window;
196 EGLSurface surf;
197};
198
199static void
200egl_window_surface_data_destroy(void *p)
201{
202 struct egl_window_surface_data *data = p;
203 struct display *d = data->display;
204
205 eglDestroySurface(d->dpy, data->surf);
206 wl_egl_window_destroy(data->window);
207 data->surface = NULL;
208
209 free(p);
210}
211
212static cairo_surface_t *
213display_create_egl_window_surface(struct display *display,
214 struct wl_surface *surface,
215 struct rectangle *rectangle)
216{
217 cairo_surface_t *cairo_surface;
218 struct egl_window_surface_data *data;
219 struct wl_visual *visual;
220
221 data = malloc(sizeof *data);
222 if (data == NULL)
223 return NULL;
224
225 data->display = display;
226 data->surface = surface;
227
228 visual = wl_display_get_premultiplied_argb_visual(display->display);
229
230 data->window = wl_egl_window_create(display->native_dpy,
231 surface,
232 rectangle->width,
233 rectangle->height,
234 visual);
235
236 data->surf = eglCreateWindowSurface(display->dpy, display->conf,
237 data->window, NULL);
238
239 cairo_surface = cairo_gl_surface_create_for_egl(display->device,
240 data->surf,
241 rectangle->width,
242 rectangle->height);
243
244 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
245 data, egl_window_surface_data_destroy);
246
247 return cairo_surface;
248}
249
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500250struct egl_image_surface_data {
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400251 struct surface_data data;
252 EGLImageKHR image;
253 GLuint texture;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800254 struct display *display;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500255 struct wl_egl_pixmap *pixmap;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400256};
257
258static void
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500259egl_image_surface_data_destroy(void *p)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400260{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500261 struct egl_image_surface_data *data = p;
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800262 struct display *d = data->display;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400263
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800264 cairo_device_acquire(d->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400265 glDeleteTextures(1, &data->texture);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800266 cairo_device_release(d->device);
267
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500268 d->destroy_image(d->dpy, data->image);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400269 wl_buffer_destroy(data->data.buffer);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500270 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500271 free(p);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400272}
273
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500274EGLImageKHR
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500275display_get_image_for_egl_image_surface(struct display *display,
276 cairo_surface_t *surface)
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500277{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500278 struct egl_image_surface_data *data;
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500279
280 data = cairo_surface_get_user_data (surface, &surface_data_key);
281
282 return data->image;
283}
284
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500285static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500286display_create_egl_image_surface(struct display *display,
287 struct rectangle *rectangle)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400288{
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500289 struct egl_image_surface_data *data;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400290 EGLDisplay dpy = display->dpy;
291 cairo_surface_t *surface;
292 struct wl_visual *visual;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400293
294 data = malloc(sizeof *data);
295 if (data == NULL)
296 return NULL;
297
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800298 data->display = display;
299
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500300 visual = wl_display_get_premultiplied_argb_visual(display->display);
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500301 data->pixmap =wl_egl_pixmap_create(display->native_dpy,
302 rectangle->width,
303 rectangle->height,
304 visual, 0);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500305 if (data->pixmap == NULL) {
nobled7b87cb02011-02-01 18:51:47 +0000306 free(data);
307 return NULL;
308 }
309
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500310 data->image = display->create_image(dpy, NULL,
311 EGL_NATIVE_PIXMAP_KHR,
312 (EGLClientBuffer) data->pixmap,
313 NULL);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500314 if (data->image == EGL_NO_IMAGE_KHR) {
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500315 wl_egl_pixmap_destroy(data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500316 free(data);
317 return NULL;
318 }
319
320 data->data.buffer =
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -0500321 wl_egl_pixmap_create_buffer(display->native_dpy, data->pixmap);
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500322
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800323 cairo_device_acquire(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400324 glGenTextures(1, &data->texture);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400325 glBindTexture(GL_TEXTURE_2D, data->texture);
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -0500326 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800327 cairo_device_release(display->device);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400328
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400329 surface = cairo_gl_surface_create_for_texture(display->device,
330 CAIRO_CONTENT_COLOR_ALPHA,
331 data->texture,
332 rectangle->width,
333 rectangle->height);
334
335 cairo_surface_set_user_data (surface, &surface_data_key,
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500336 data, egl_image_surface_data_destroy);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400337
338 return surface;
339}
340
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500341static cairo_surface_t *
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500342display_create_egl_image_surface_from_file(struct display *display,
343 const char *filename,
344 struct rectangle *rect)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400345{
346 cairo_surface_t *surface;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400347 GdkPixbuf *pixbuf;
348 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400349 int stride, i;
350 unsigned char *pixels, *p, *end;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500351 struct egl_image_surface_data *data;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400352
353 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400354 rect->width, rect->height,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400355 FALSE, &error);
356 if (error != NULL)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400357 return NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400358
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400359 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
360 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500361 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400362 return NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400363 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400364
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400365
366 stride = gdk_pixbuf_get_rowstride(pixbuf);
367 pixels = gdk_pixbuf_get_pixels(pixbuf);
368
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400369 for (i = 0; i < rect->height; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400370 p = pixels + i * stride;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400371 end = p + rect->width * 4;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400372 while (p < end) {
373 unsigned int t;
374
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400375 MULT(p[0], p[0], p[3], t);
376 MULT(p[1], p[1], p[3], t);
377 MULT(p[2], p[2], p[3], t);
378 p += 4;
379
380 }
381 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400382
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500383 surface = display_create_egl_image_surface(display, rect);
nobled7b87cb02011-02-01 18:51:47 +0000384 if (surface == NULL) {
385 g_object_unref(pixbuf);
386 return NULL;
387 }
388
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800389 data = cairo_surface_get_user_data(surface, &surface_data_key);
390
391 cairo_device_acquire(display->device);
392 glBindTexture(GL_TEXTURE_2D, data->texture);
Chia-I Wu1f411902010-10-29 15:20:16 +0800393 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
394 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Chia-I Wu4d8ba212010-10-29 15:20:17 +0800395 cairo_device_release(display->device);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400396
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500397 g_object_unref(pixbuf);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400398
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400399 return surface;
400}
401
402#endif
403
404struct wl_buffer *
405display_get_buffer_for_surface(struct display *display,
406 cairo_surface_t *surface)
407{
408 struct surface_data *data;
409
410 data = cairo_surface_get_user_data (surface, &surface_data_key);
411
412 return data->buffer;
413}
414
415struct shm_surface_data {
416 struct surface_data data;
417 void *map;
418 size_t length;
419};
420
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500421static void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400422shm_surface_data_destroy(void *p)
423{
424 struct shm_surface_data *data = p;
425
426 wl_buffer_destroy(data->data.buffer);
427 munmap(data->map, data->length);
428}
429
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500430static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400431display_create_shm_surface(struct display *display,
432 struct rectangle *rectangle)
433{
434 struct shm_surface_data *data;
435 cairo_surface_t *surface;
436 struct wl_visual *visual;
Bryce Harrington40269a62010-11-19 12:15:36 -0800437 int stride, fd;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400438 char filename[] = "/tmp/wayland-shm-XXXXXX";
439
440 data = malloc(sizeof *data);
441 if (data == NULL)
442 return NULL;
443
444 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
445 rectangle->width);
446 data->length = stride * rectangle->height;
447 fd = mkstemp(filename);
448 if (fd < 0) {
nobled14d222f2011-02-01 18:48:46 +0000449 fprintf(stderr, "open %s failed: %m\n", filename);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400450 return NULL;
451 }
452 if (ftruncate(fd, data->length) < 0) {
nobled14d222f2011-02-01 18:48:46 +0000453 fprintf(stderr, "ftruncate failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400454 close(fd);
455 return NULL;
456 }
457
458 data->map = mmap(NULL, data->length,
459 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
460 unlink(filename);
461
462 if (data->map == MAP_FAILED) {
nobled14d222f2011-02-01 18:48:46 +0000463 fprintf(stderr, "mmap failed: %m\n");
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400464 close(fd);
465 return NULL;
466 }
467
468 surface = cairo_image_surface_create_for_data (data->map,
469 CAIRO_FORMAT_ARGB32,
470 rectangle->width,
471 rectangle->height,
472 stride);
473
474 cairo_surface_set_user_data (surface, &surface_data_key,
475 data, shm_surface_data_destroy);
476
477 visual = wl_display_get_premultiplied_argb_visual(display->display);
478 data->data.buffer = wl_shm_create_buffer(display->shm,
479 fd,
480 rectangle->width,
481 rectangle->height,
482 stride, visual);
483
484 close(fd);
485
486 return surface;
487}
488
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500489static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400490display_create_shm_surface_from_file(struct display *display,
491 const char *filename,
492 struct rectangle *rect)
493{
494 cairo_surface_t *surface;
495 GdkPixbuf *pixbuf;
496 GError *error = NULL;
497 int stride, i;
498 unsigned char *pixels, *p, *end, *dest_data;
499 int dest_stride;
500 uint32_t *d;
501
502 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
503 rect->width, rect->height,
504 FALSE, &error);
505 if (error != NULL)
506 return NULL;
507
508 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
509 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500510 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400511 return NULL;
512 }
513
514 stride = gdk_pixbuf_get_rowstride(pixbuf);
515 pixels = gdk_pixbuf_get_pixels(pixbuf);
516
517 surface = display_create_shm_surface(display, rect);
nobled7b87cb02011-02-01 18:51:47 +0000518 if (surface == NULL) {
519 g_object_unref(pixbuf);
520 return NULL;
521 }
522
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400523 dest_data = cairo_image_surface_get_data (surface);
524 dest_stride = cairo_image_surface_get_stride (surface);
525
526 for (i = 0; i < rect->height; i++) {
527 d = (uint32_t *) (dest_data + i * dest_stride);
528 p = pixels + i * stride;
529 end = p + rect->width * 4;
530 while (p < end) {
531 unsigned int t;
532 unsigned char a, r, g, b;
533
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400534 a = p[3];
535 MULT(r, p[0], a, t);
536 MULT(g, p[1], a, t);
537 MULT(b, p[2], a, t);
538 p += 4;
539 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
540 }
541 }
542
Darxus@chaosreigns.comc4df99c2011-01-25 15:00:56 -0500543 g_object_unref(pixbuf);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400544
545 return surface;
546}
547
nobled7b87cb02011-02-01 18:51:47 +0000548static int
549check_size(struct rectangle *rect)
550{
551 if (rect->width && rect->height)
552 return 0;
553
554 fprintf(stderr, "tried to create surface of "
555 "width: %d, height: %d\n", rect->width, rect->height);
556 return -1;
557}
558
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400559cairo_surface_t *
560display_create_surface(struct display *display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100561 struct wl_surface *surface,
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400562 struct rectangle *rectangle)
563{
nobled7b87cb02011-02-01 18:51:47 +0000564 if (check_size(rectangle) < 0)
565 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500566#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500567 if (display->dpy) {
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100568 if (surface)
569 return display_create_egl_window_surface(display,
570 surface,
571 rectangle);
572 else
573 return display_create_egl_image_surface(display,
574 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500575 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400576#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500577 return display_create_shm_surface(display, rectangle);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400578}
579
Kristian Høgsberg06bc2642010-12-01 09:50:16 -0500580static cairo_surface_t *
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400581display_create_surface_from_file(struct display *display,
582 const char *filename,
583 struct rectangle *rectangle)
584{
nobled7b87cb02011-02-01 18:51:47 +0000585 if (check_size(rectangle) < 0)
586 return NULL;
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500587#ifdef HAVE_CAIRO_EGL
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500588 if (display->dpy) {
589 return display_create_egl_image_surface_from_file(display,
590 filename,
591 rectangle);
Yuval Fledel45568f62010-12-06 09:18:12 -0500592 }
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400593#endif
Yuval Fledel45568f62010-12-06 09:18:12 -0500594 return display_create_shm_surface_from_file(display, filename, rectangle);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400595}
Yuval Fledel45568f62010-12-06 09:18:12 -0500596 static const struct {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400597 const char *filename;
598 int hotspot_x, hotspot_y;
599} pointer_images[] = {
600 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
601 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
602 { DATADIR "/wayland/bottom_side.png", 16, 20 },
603 { DATADIR "/wayland/grabbing.png", 20, 17 },
604 { DATADIR "/wayland/left_ptr.png", 10, 5 },
605 { DATADIR "/wayland/left_side.png", 10, 20 },
606 { DATADIR "/wayland/right_side.png", 30, 19 },
607 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
608 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
609 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400610 { DATADIR "/wayland/xterm.png", 15, 15 },
611 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400612};
613
614static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400615create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400616{
617 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400618 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400619 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400620
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400621 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400622 display->pointer_surfaces =
623 malloc(count * sizeof *display->pointer_surfaces);
624 rect.width = width;
625 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400626 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400627 display->pointer_surfaces[i] =
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400628 display_create_surface_from_file(display,
629 pointer_images[i].filename,
630 &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400631 }
632
633}
634
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400635cairo_surface_t *
636display_get_pointer_surface(struct display *display, int pointer,
637 int *width, int *height,
638 int *hotspot_x, int *hotspot_y)
639{
640 cairo_surface_t *surface;
641
642 surface = display->pointer_surfaces[pointer];
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500643#if HAVE_CAIRO_EGL
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400644 *width = cairo_gl_surface_get_width(surface);
645 *height = cairo_gl_surface_get_height(surface);
nobledf8475c92011-01-05 17:41:55 +0000646#else
647 *width = cairo_image_surface_get_width(surface);
648 *height = cairo_image_surface_get_height(surface);
649#endif
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400650 *hotspot_x = pointer_images[pointer].hotspot_x;
651 *hotspot_y = pointer_images[pointer].hotspot_y;
652
653 return cairo_surface_reference(surface);
654}
655
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400656
657static void
658window_attach_surface(struct window *window);
659
660static void
661free_surface(void *data)
662{
663 struct window *window = data;
664
665 cairo_surface_destroy(window->pending_surface);
666 window->pending_surface = NULL;
667 if (window->cairo_surface)
668 window_attach_surface(window);
669}
670
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500671static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500672window_attach_surface(struct window *window)
673{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400674 struct display *display = window->display;
675 struct wl_buffer *buffer;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100676 struct egl_window_surface_data *data;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500677 int32_t x, y;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100678 int width = window->allocation.width;
679 int height = window->allocation.height;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500680
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500681 if (window->resize_edges & WINDOW_RESIZING_LEFT)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100682 x = window->server_allocation.width - width;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500683 else
684 x = 0;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500685
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500686 if (window->resize_edges & WINDOW_RESIZING_TOP)
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100687 y = window->server_allocation.height - height;
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500688 else
689 y = 0;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400690
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500691 window->resize_edges = 0;
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100692
693 switch (window->buffer_type) {
694 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
695 data = cairo_surface_get_user_data(window->cairo_surface,
696 &surface_data_key);
697
698 wl_egl_window_resize(data->window, width, height, x, y);
699 cairo_gl_surface_swapbuffers(window->cairo_surface);
700 wl_egl_window_get_attached_size(data->window,
701 &window->server_allocation.width,
702 &window->server_allocation.height);
703 break;
704 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
705 case WINDOW_BUFFER_TYPE_SHM:
706 if (window->pending_surface != NULL)
707 return;
708
709 window->pending_surface = window->cairo_surface;
710 window->cairo_surface = NULL;
711
712 buffer =
713 display_get_buffer_for_surface(display,
714 window->pending_surface);
715
716 wl_surface_attach(window->surface, buffer, x, y);
717 window->server_allocation = window->allocation;
718 wl_display_sync_callback(display->display, free_surface,
719 window);
720 break;
721 }
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500722
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500723 if (window->fullscreen)
724 wl_surface_map_fullscreen(window->surface);
725 else if (!window->parent)
726 wl_surface_map_toplevel(window->surface);
727 else
728 wl_surface_map_transient(window->surface,
729 window->parent->surface,
730 window->x, window->y, 0);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500731
732 wl_surface_damage(window->surface, 0, 0,
733 window->allocation.width,
734 window->allocation.height);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500735}
736
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500737void
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400738window_flush(struct window *window)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500739{
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400740 if (window->cairo_surface)
741 window_attach_surface(window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500742}
743
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400744void
745window_set_surface(struct window *window, cairo_surface_t *surface)
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400746{
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500747 cairo_surface_reference(surface);
748
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400749 if (window->cairo_surface != NULL)
750 cairo_surface_destroy(window->cairo_surface);
751
Kristian Høgsberg2b43bd72010-11-08 15:45:55 -0500752 window->cairo_surface = surface;
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400753}
754
Kristian Høgsberg386857b2011-04-11 13:17:44 -0400755struct wl_surface *
756window_get_wl_surface(struct window *window)
757{
758 return window->surface;
759}
760
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100761static void
762window_resize_cairo_window_surface(struct window *window)
763{
764 struct egl_window_surface_data *data;
765
766 data = cairo_surface_get_user_data(window->cairo_surface,
767 &surface_data_key);
768
769 wl_egl_window_resize(data->window,
770 window->allocation.width,
771 window->allocation.height, 0, 0);
772 cairo_gl_surface_set_size(window->cairo_surface,
773 window->allocation.width,
774 window->allocation.height);
775}
776
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400777void
778window_create_surface(struct window *window)
779{
780 cairo_surface_t *surface;
781
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400782 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500783#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100784 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
785 if (window->cairo_surface) {
786 window_resize_cairo_window_surface(window);
787 return;
788 }
789 surface = display_create_surface(window->display,
790 window->surface,
791 &window->allocation);
792 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500793 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400794 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100795 NULL,
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400796 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400797 break;
798#endif
799 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400800 surface = display_create_shm_surface(window->display,
801 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400802 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800803 default:
804 surface = NULL;
805 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400806 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400807
808 window_set_surface(window, surface);
809 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400810}
811
812static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500813window_draw_menu(struct window *window)
814{
815 cairo_t *cr;
816 int width, height, r = 5;
817
818 window_create_surface(window);
819
820 cr = cairo_create(window->cairo_surface);
821 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
822 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
823 cairo_paint(cr);
824
825 width = window->allocation.width;
826 height = window->allocation.height;
827 rounded_rect(cr, r, r, width - r, height - r, r);
828 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
829 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
830 cairo_fill(cr);
831 cairo_destroy(cr);
832}
833
834static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500835window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500836{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500837 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500838 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400839 cairo_surface_t *frame;
840 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500841
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400842 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400843
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400844 width = window->allocation.width;
845 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500846
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500847 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500848
Kristian Høgsberg09531622010-06-14 23:22:15 -0400849 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400850 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400851 cairo_paint(cr);
852
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400853 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400854 tile_mask(cr, window->display->shadow,
855 shadow_dx, shadow_dy, width, height,
856 window->margin + 10 - shadow_dx,
857 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500858
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400859 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400860 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400861 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400862 frame = window->display->inactive_frame;
863
864 tile_source(cr, frame, 0, 0, width, height,
865 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500866
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500867 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
868 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500869 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400870 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400871 if (window->keyboard_device)
872 cairo_set_source_rgb(cr, 0, 0, 0);
873 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400874 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400875 cairo_show_text(cr, window->title);
876
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500877 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400878
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100879 /* FIXME: this breakes gears, fix cairo? */
880#if 0
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400881 cairo_device_flush (window->display->device);
Benjamin Franzkecff904e2011-02-18 23:00:55 +0100882#endif
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500883}
884
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400885void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500886window_destroy(struct window *window)
887{
888 wl_surface_destroy(window->surface);
889 wl_list_remove(&window->link);
890 free(window);
891}
892
893void
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400894display_flush_cairo_device(struct display *display)
895{
896 cairo_device_flush (display->device);
897}
898
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500899void
900window_draw(struct window *window)
901{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500902 if (window->parent)
903 window_draw_menu(window);
904 else if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400905 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500906 else
907 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500908}
909
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400910cairo_surface_t *
911window_get_surface(struct window *window)
912{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400913 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400914}
915
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400916static int
917get_pointer_location(struct window *window, int32_t x, int32_t y)
918{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400919 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400920 const int grip_size = 8;
921
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500922 if (!window->decoration)
923 return WINDOW_CLIENT_AREA;
924
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400925 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400926 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400927 else if (window->margin <= x && x < window->margin + grip_size)
928 hlocation = WINDOW_RESIZING_LEFT;
929 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400930 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400931 else if (x < window->allocation.width - window->margin)
932 hlocation = WINDOW_RESIZING_RIGHT;
933 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400934 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400935
936 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400937 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400938 else if (window->margin <= y && y < window->margin + grip_size)
939 vlocation = WINDOW_RESIZING_TOP;
940 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400941 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400942 else if (y < window->allocation.height - window->margin)
943 vlocation = WINDOW_RESIZING_BOTTOM;
944 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400945 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400946
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400947 location = vlocation | hlocation;
948 if (location & WINDOW_EXTERIOR)
949 location = WINDOW_EXTERIOR;
950 if (location == WINDOW_INTERIOR && y < window->margin + 50)
951 location = WINDOW_TITLEBAR;
952 else if (location == WINDOW_INTERIOR)
953 location = WINDOW_CLIENT_AREA;
954
955 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400956}
957
958static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400959set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400960{
961 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400962 struct wl_buffer *buffer;
963 cairo_surface_t *surface;
964 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400965
966 location = get_pointer_location(input->pointer_focus,
967 input->sx, input->sy);
968 switch (location) {
969 case WINDOW_RESIZING_TOP:
970 pointer = POINTER_TOP;
971 break;
972 case WINDOW_RESIZING_BOTTOM:
973 pointer = POINTER_BOTTOM;
974 break;
975 case WINDOW_RESIZING_LEFT:
976 pointer = POINTER_LEFT;
977 break;
978 case WINDOW_RESIZING_RIGHT:
979 pointer = POINTER_RIGHT;
980 break;
981 case WINDOW_RESIZING_TOP_LEFT:
982 pointer = POINTER_TOP_LEFT;
983 break;
984 case WINDOW_RESIZING_TOP_RIGHT:
985 pointer = POINTER_TOP_RIGHT;
986 break;
987 case WINDOW_RESIZING_BOTTOM_LEFT:
988 pointer = POINTER_BOTTOM_LEFT;
989 break;
990 case WINDOW_RESIZING_BOTTOM_RIGHT:
991 pointer = POINTER_BOTTOM_RIGHT;
992 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400993 case WINDOW_EXTERIOR:
994 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400995 if (input->current_pointer_image == POINTER_DEFAULT)
996 return;
997
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400998 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400999 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001000 return;
1001 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001002 break;
1003 }
1004
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001005 if (pointer == input->current_pointer_image)
1006 return;
1007
1008 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001009 surface = display->pointer_surfaces[pointer];
1010 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001011 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001012 pointer_images[pointer].hotspot_x,
1013 pointer_images[pointer].hotspot_y);
1014}
1015
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001016static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001017window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001018 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001019 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001020{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001021 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001022 struct window *window = input->pointer_focus;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001023 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001024
1025 input->x = x;
1026 input->y = y;
1027 input->sx = sx;
1028 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001029
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001030 if (window->motion_handler)
1031 pointer = (*window->motion_handler)(window, input, time,
1032 x, y, sx, sy,
1033 window->user_data);
1034
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001035 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001036}
1037
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001038static void
1039window_handle_button(void *data,
1040 struct wl_input_device *input_device,
1041 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001042{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001043 struct input *input = data;
1044 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001045 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001046
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001047 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001048
1049 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001050 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001051 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001052 wl_shell_move(window->display->shell,
1053 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001054 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001055 case WINDOW_RESIZING_TOP:
1056 case WINDOW_RESIZING_BOTTOM:
1057 case WINDOW_RESIZING_LEFT:
1058 case WINDOW_RESIZING_RIGHT:
1059 case WINDOW_RESIZING_TOP_LEFT:
1060 case WINDOW_RESIZING_TOP_RIGHT:
1061 case WINDOW_RESIZING_BOTTOM_LEFT:
1062 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001063 wl_shell_resize(window->display->shell,
1064 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001065 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001066 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001067 case WINDOW_CLIENT_AREA:
1068 if (window->button_handler)
1069 (*window->button_handler)(window,
1070 input, time,
1071 button, state,
1072 window->user_data);
1073 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001074 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001075 } else {
1076 if (window->button_handler)
1077 (*window->button_handler)(window,
1078 input, time,
1079 button, state,
1080 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001081 }
1082}
1083
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001084static void
1085window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001086 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001087{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001088 struct input *input = data;
1089 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001090 struct display *d = window->display;
1091 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001092
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001093 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001094 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001095 return;
1096
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001097 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001098 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001099 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1100 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001101
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001102 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1103
1104 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001105 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001106 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001107 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001108
1109 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001110 (*window->key_handler)(window, input, time, key, sym, state,
1111 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001112}
1113
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001114static void
1115window_handle_pointer_focus(void *data,
1116 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001117 uint32_t time, struct wl_surface *surface,
1118 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001119{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001120 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001121 struct window *window;
1122 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001123
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001124 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001125 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001126 window = input->pointer_focus;
1127
Kristian Høgsberg59826582011-01-20 11:56:57 -05001128 input->x = x;
1129 input->y = y;
1130 input->sx = sx;
1131 input->sy = sy;
1132
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001133 pointer = POINTER_LEFT_PTR;
1134 if (window->motion_handler)
1135 pointer = (*window->motion_handler)(window,
1136 input, time,
1137 x, y, sx, sy,
1138 window->user_data);
1139
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001140 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001141 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001142 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001143 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001144 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001145}
1146
1147static void
1148window_handle_keyboard_focus(void *data,
1149 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001150 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001151 struct wl_surface *surface,
1152 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001153{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001154 struct input *input = data;
1155 struct window *window = input->keyboard_focus;
1156 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001157 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001158
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001159 window = input->keyboard_focus;
1160 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001161 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001162 if (window->keyboard_focus_handler)
1163 (*window->keyboard_focus_handler)(window, NULL,
1164 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001165 }
1166
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001167 if (surface)
1168 input->keyboard_focus = wl_surface_get_user_data(surface);
1169 else
1170 input->keyboard_focus = NULL;
1171
1172 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001173 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001174 for (k = keys->data; k < end; k++)
1175 input->modifiers |= d->xkb->map->modmap[*k];
1176
1177 window = input->keyboard_focus;
1178 if (window) {
1179 window->keyboard_device = input;
1180 if (window->keyboard_focus_handler)
1181 (*window->keyboard_focus_handler)(window,
1182 window->keyboard_device,
1183 window->user_data);
1184 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001185}
1186
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001187static const struct wl_input_device_listener input_device_listener = {
1188 window_handle_motion,
1189 window_handle_button,
1190 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001191 window_handle_pointer_focus,
1192 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001193};
1194
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001195void
1196input_get_position(struct input *input, int32_t *x, int32_t *y)
1197{
1198 *x = input->sx;
1199 *y = input->sy;
1200}
1201
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001202struct wl_input_device *
1203input_get_input_device(struct input *input)
1204{
1205 return input->input_device;
1206}
1207
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001208uint32_t
1209input_get_modifiers(struct input *input)
1210{
1211 return input->modifiers;
1212}
1213
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001214struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001215window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001216{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001217 cairo_device_flush (window->display->device);
1218
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001219 return wl_shell_create_drag(window->display->shell);
1220}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001221
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001222void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001223window_move(struct window *window, struct input *input, uint32_t time)
1224{
1225 wl_shell_move(window->display->shell,
1226 window->surface, input->input_device, time);
1227}
1228
1229void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001230window_activate_drag(struct wl_drag *drag, struct window *window,
1231 struct input *input, uint32_t time)
1232{
1233 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001234}
1235
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001236static void
1237handle_configure(void *data, struct wl_shell *shell,
1238 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001239 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001240{
1241 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001242 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001243
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001244 /* FIXME: this is probably the wrong place to check for width
1245 * or height <= 0, but it prevents the compositor from crashing
1246 */
1247 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001248 return;
1249
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001250 window->resize_edges = edges;
1251
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001252 if (window->resize_handler) {
1253 child_width = width - 20 - window->margin * 2;
1254 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001255
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001256 (*window->resize_handler)(window,
1257 child_width, child_height,
1258 window->user_data);
1259 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001260 window->allocation.width = width;
1261 window->allocation.height = height;
1262
1263 if (window->redraw_handler)
1264 window_schedule_redraw(window);
1265 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001266}
1267
1268static const struct wl_shell_listener shell_listener = {
1269 handle_configure,
1270};
1271
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001272void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001273window_get_allocation(struct window *window,
1274 struct rectangle *allocation)
1275{
1276 *allocation = window->allocation;
1277}
1278
1279void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001280window_get_child_allocation(struct window *window,
1281 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001282{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001283 if (window->fullscreen || !window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001284 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001285 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001286 allocation->x = window->margin + 10;
1287 allocation->y = window->margin + 50;
1288 allocation->width =
1289 window->allocation.width - 20 - window->margin * 2;
1290 allocation->height =
1291 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001292 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001293}
1294
1295void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001296window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001297{
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001298 if (!window->fullscreen) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001299 window->allocation.x = 20 + window->margin;
1300 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001301 window->allocation.width = width + 20 + window->margin * 2;
1302 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001303 } else {
1304 window->allocation.x = 0;
1305 window->allocation.y = 0;
1306 window->allocation.width = width;
1307 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001308 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001309}
1310
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001311static gboolean
1312idle_redraw(void *data)
1313{
1314 struct window *window = data;
1315
1316 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001317
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001318 window->redraw_scheduled = 0;
1319
1320 return FALSE;
1321}
1322
1323void
1324window_schedule_redraw(struct window *window)
1325{
1326 if (!window->redraw_scheduled) {
1327 g_idle_add(idle_redraw, window);
1328 window->redraw_scheduled = 1;
1329 }
1330}
1331
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001332void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001333window_set_fullscreen(struct window *window, int fullscreen)
1334{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001335 int32_t width, height;
1336
1337 if (window->fullscreen == fullscreen)
1338 return;
1339
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001340 window->fullscreen = fullscreen;
1341 if (window->fullscreen) {
1342 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001343 width = window->display->screen_allocation.width;
1344 height = window->display->screen_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001345 } else {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001346 width = window->saved_allocation.width - 20 - window->margin * 2;
1347 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001348 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001349
1350 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001351}
1352
1353void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001354window_set_decoration(struct window *window, int decoration)
1355{
1356 window->decoration = decoration;
1357}
1358
1359void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001360window_set_user_data(struct window *window, void *data)
1361{
1362 window->user_data = data;
1363}
1364
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001365void *
1366window_get_user_data(struct window *window)
1367{
1368 return window->user_data;
1369}
1370
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001371void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001372window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001373 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001374{
1375 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001376}
1377
1378void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001379window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001380 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001381{
1382 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001383}
1384
1385void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001386window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001387 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001388{
1389 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001390}
1391
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001392void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001393window_set_button_handler(struct window *window,
1394 window_button_handler_t handler)
1395{
1396 window->button_handler = handler;
1397}
1398
1399void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001400window_set_motion_handler(struct window *window,
1401 window_motion_handler_t handler)
1402{
1403 window->motion_handler = handler;
1404}
1405
1406void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001407window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001408 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001409{
1410 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001411}
1412
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001413void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001414window_set_title(struct window *window, const char *title)
1415{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001416 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001417 window->title = strdup(title);
1418}
1419
1420const char *
1421window_get_title(struct window *window)
1422{
1423 return window->title;
1424}
1425
1426void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001427window_damage(struct window *window, int32_t x, int32_t y,
1428 int32_t width, int32_t height)
1429{
1430 wl_surface_damage(window->surface, x, y, width, height);
1431}
1432
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001433static struct window *
1434window_create_internal(struct display *display, struct window *parent,
1435 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001436{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001437 struct window *window;
1438
1439 window = malloc(sizeof *window);
1440 if (window == NULL)
1441 return NULL;
1442
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001443 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001444 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001445 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001446 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001447 window->allocation.x = 0;
1448 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001449 window->allocation.width = width;
1450 window->allocation.height = height;
1451 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001452 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001453 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001454
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001455 if (display->dpy)
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001456 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1457 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Yuval Fledel45568f62010-12-06 09:18:12 -05001458 else
1459 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001460
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001461 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001462 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001463
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001464 return window;
1465}
1466
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001467struct window *
1468window_create(struct display *display, int32_t width, int32_t height)
1469{
1470 struct window *window;
1471
1472 window = window_create_internal(display, NULL, width, height);
1473 if (!window)
1474 return NULL;
1475
1476 return window;
1477}
1478
1479struct window *
1480window_create_transient(struct display *display, struct window *parent,
1481 int32_t x, int32_t y, int32_t width, int32_t height)
1482{
1483 struct window *window;
1484
1485 window = window_create_internal(parent->display,
1486 parent, width, height);
1487 if (!window)
1488 return NULL;
1489
1490 window->x = x;
1491 window->y = y;
1492
1493 return window;
1494}
1495
1496void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001497window_set_buffer_type(struct window *window, enum window_buffer_type type)
1498{
1499 window->buffer_type = type;
1500}
1501
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001502static void
1503display_handle_geometry(void *data,
1504 struct wl_output *output,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001505 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001506{
1507 struct display *display = data;
1508
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001509 display->screen_allocation.x = x;
1510 display->screen_allocation.y = y;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001511 display->screen_allocation.width = width;
1512 display->screen_allocation.height = height;
1513}
1514
1515static const struct wl_output_listener output_listener = {
1516 display_handle_geometry,
1517};
1518
1519static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001520display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001521{
1522 struct input *input;
1523
1524 input = malloc(sizeof *input);
1525 if (input == NULL)
1526 return;
1527
1528 memset(input, 0, sizeof *input);
1529 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001530 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001531 input->pointer_focus = NULL;
1532 input->keyboard_focus = NULL;
1533 wl_list_insert(d->input_list.prev, &input->link);
1534
1535 wl_input_device_add_listener(input->input_device,
1536 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001537 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001538}
1539
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001540struct selection_offer {
1541 struct display *display;
1542 struct wl_selection_offer *offer;
1543 struct wl_array types;
1544 struct input *input;
1545};
1546
1547int
1548input_offers_mime_type(struct input *input, const char *type)
1549{
1550 struct selection_offer *offer = input->offer;
1551 char **p, **end;
1552
1553 if (offer == NULL)
1554 return 0;
1555
1556 end = offer->types.data + offer->types.size;
1557 for (p = offer->types.data; p < end; p++)
1558 if (strcmp(*p, type) == 0)
1559 return 1;
1560
1561 return 0;
1562}
1563
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001564void
1565input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001566{
1567 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001568
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001569 /* FIXME: A number of things can go wrong here: the object may
1570 * not be the current selection offer any more (which could
1571 * still work, but the source may have gone away or just
1572 * destroyed its wl_selection) or the offer may not have the
1573 * requested type after all (programmer/client error,
1574 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001575 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001576}
1577
1578static void
1579selection_offer_offer(void *data,
1580 struct wl_selection_offer *selection_offer,
1581 const char *type)
1582{
1583 struct selection_offer *offer = data;
1584
1585 char **p;
1586
1587 p = wl_array_add(&offer->types, sizeof *p);
1588 if (p)
1589 *p = strdup(type);
1590};
1591
1592static void
1593selection_offer_keyboard_focus(void *data,
1594 struct wl_selection_offer *selection_offer,
1595 struct wl_input_device *input_device)
1596{
1597 struct selection_offer *offer = data;
1598 struct input *input;
1599 char **p, **end;
1600
1601 if (input_device == NULL) {
1602 printf("selection offer retracted %p\n", selection_offer);
1603 input = offer->input;
1604 input->offer = NULL;
1605 wl_selection_offer_destroy(selection_offer);
1606 wl_array_release(&offer->types);
1607 free(offer);
1608 return;
1609 }
1610
1611 input = wl_input_device_get_user_data(input_device);
1612 printf("new selection offer %p:", selection_offer);
1613
1614 offer->input = input;
1615 input->offer = offer;
1616 end = offer->types.data + offer->types.size;
1617 for (p = offer->types.data; p < end; p++)
1618 printf(" %s", *p);
1619
1620 printf("\n");
1621}
1622
1623struct wl_selection_offer_listener selection_offer_listener = {
1624 selection_offer_offer,
1625 selection_offer_keyboard_focus
1626};
1627
1628static void
1629add_selection_offer(struct display *d, uint32_t id)
1630{
1631 struct selection_offer *offer;
1632
1633 offer = malloc(sizeof *offer);
1634 if (offer == NULL)
1635 return;
1636
1637 offer->offer = wl_selection_offer_create(d->display, id);
1638 offer->display = d;
1639 wl_array_init(&offer->types);
1640 offer->input = NULL;
1641
1642 wl_selection_offer_add_listener(offer->offer,
1643 &selection_offer_listener, offer);
1644}
1645
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001646static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001647display_handle_global(struct wl_display *display, uint32_t id,
1648 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001649{
1650 struct display *d = data;
1651
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001652 if (strcmp(interface, "compositor") == 0) {
1653 d->compositor = wl_compositor_create(display, id);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001654 } else if (strcmp(interface, "output") == 0) {
1655 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001656 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001657 } else if (strcmp(interface, "input_device") == 0) {
1658 display_add_input(d, id);
1659 } else if (strcmp(interface, "shell") == 0) {
1660 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001661 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001662 } else if (strcmp(interface, "shm") == 0) {
1663 d->shm = wl_shm_create(display, id);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001664 } else if (strcmp(interface, "selection_offer") == 0) {
1665 add_selection_offer(d, id);
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001666 } else if (d->global_handler) {
Kristian Høgsberg943741c2011-01-18 09:23:13 -05001667 d->global_handler(d, interface, id, version);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001668 }
1669}
1670
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001671static void
1672display_render_frame(struct display *d)
1673{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001674 int radius = 8;
1675 cairo_t *cr;
1676
1677 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1678 cr = cairo_create(d->shadow);
1679 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1680 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1681 rounded_rect(cr, 16, 16, 112, 112, radius);
1682 cairo_fill(cr);
1683 cairo_destroy(cr);
1684 blur_surface(d->shadow, 64);
1685
1686 d->active_frame =
1687 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1688 cr = cairo_create(d->active_frame);
1689 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1690 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1691 rounded_rect(cr, 16, 16, 112, 112, radius);
1692 cairo_fill(cr);
1693 cairo_destroy(cr);
1694
1695 d->inactive_frame =
1696 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1697 cr = cairo_create(d->inactive_frame);
1698 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1699 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1700 rounded_rect(cr, 16, 16, 112, 112, radius);
1701 cairo_fill(cr);
1702 cairo_destroy(cr);
1703}
1704
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001705static void
1706init_xkb(struct display *d)
1707{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001708 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001709
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001710 names.rules = "evdev";
1711 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001712 names.layout = option_xkb_layout;
1713 names.variant = option_xkb_variant;
1714 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001715
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001716 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001717 if (!d->xkb) {
1718 fprintf(stderr, "Failed to compile keymap\n");
1719 exit(1);
1720 }
1721}
1722
Yuval Fledel45568f62010-12-06 09:18:12 -05001723static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001724init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05001725{
1726 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001727 EGLint n;
1728 static const EGLint cfg_attribs[] = {
1729 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
1730 EGL_RED_SIZE, 1,
1731 EGL_GREEN_SIZE, 1,
1732 EGL_BLUE_SIZE, 1,
1733 EGL_ALPHA_SIZE, 1,
1734 EGL_DEPTH_SIZE, 1,
1735 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
1736 EGL_NONE
1737 };
Yuval Fledel45568f62010-12-06 09:18:12 -05001738
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -05001739 d->dpy = eglGetDisplay(d->native_dpy);
Yuval Fledel45568f62010-12-06 09:18:12 -05001740 if (!eglInitialize(d->dpy, &major, &minor)) {
1741 fprintf(stderr, "failed to initialize display\n");
1742 return -1;
1743 }
1744
1745 if (!eglBindAPI(EGL_OPENGL_API)) {
1746 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1747 return -1;
1748 }
1749
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001750 if (!eglChooseConfig(d->dpy, cfg_attribs, &d->conf, 1, &n) || n != 1) {
1751 fprintf(stderr, "failed to choose config\n");
1752 return -1;
1753 }
1754
1755 d->ctx = eglCreateContext(d->dpy, d->conf, EGL_NO_CONTEXT, NULL);
Yuval Fledel45568f62010-12-06 09:18:12 -05001756 if (d->ctx == NULL) {
1757 fprintf(stderr, "failed to create context\n");
1758 return -1;
1759 }
1760
1761 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01001762 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001763 return -1;
1764 }
1765
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001766#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05001767 d->device = cairo_egl_device_create(d->dpy, d->ctx);
1768 if (d->device == NULL) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001769 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001770 return -1;
1771 }
1772#endif
1773
1774 return 0;
1775}
1776
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001777struct display *
Tim Wiederhakeb4b67342011-04-11 13:16:33 -04001778display_create(int *argc, char **argv[], const GOptionEntry *option_entries,
1779 display_global_handler_t handler)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001780{
1781 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001782 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001783 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001784 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001785
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001786 g_type_init();
1787
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001788 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001789 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001790 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001791
1792 xkb_option_group = g_option_group_new("xkb",
1793 "Keyboard options",
1794 "Show all XKB options",
1795 NULL, NULL);
1796 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1797 g_option_context_add_group (context, xkb_option_group);
1798
1799 if (!g_option_context_parse(context, argc, argv, &error)) {
1800 fprintf(stderr, "option parsing failed: %s\n", error->message);
1801 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001802 }
1803
Tim Wiederhake748f6722011-01-23 23:25:25 +01001804 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001805
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001806 d = malloc(sizeof *d);
1807 if (d == NULL)
1808 return NULL;
1809
Tim Wiederhake81bd9792011-01-23 23:25:26 +01001810 memset(d, 0, sizeof *d);
1811
Tim Wiederhakeb4b67342011-04-11 13:16:33 -04001812 d->global_handler = handler;
1813
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001814 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001815 if (d->display == NULL) {
1816 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001817 return NULL;
1818 }
1819
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001820 wl_list_init(&d->input_list);
1821
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001822 /* Set up listener so we'll catch all events. */
1823 wl_display_add_global_listener(d->display,
1824 display_handle_global, d);
1825
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -05001826 d->native_dpy = wl_egl_display_create(d->display);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001827
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001828 /* Process connection events. */
1829 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1830
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001831 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001832 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001833
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05001834 d->image_target_texture_2d =
1835 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1836 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1837 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1838
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001839 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001840
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001841 display_render_frame(d);
1842
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001843 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001844 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001845 g_source_attach(d->source, NULL);
1846
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001847 wl_list_init(&d->window_list);
1848
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001849 init_xkb(d);
1850
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001851 return d;
1852}
1853
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001854struct wl_display *
1855display_get_display(struct display *display)
1856{
1857 return display->display;
1858}
1859
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001860struct wl_compositor *
1861display_get_compositor(struct display *display)
1862{
1863 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001864}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001865
1866EGLDisplay
1867display_get_egl_display(struct display *d)
1868{
1869 return d->dpy;
1870}
1871
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001872EGLConfig
1873display_get_egl_config(struct display *d)
1874{
1875 return d->conf;
1876}
1877
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001878struct wl_shell *
1879display_get_shell(struct display *display)
1880{
1881 return display->shell;
1882}
1883
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001884void
Benjamin Franzkecff904e2011-02-18 23:00:55 +01001885display_acquire_window_surface(struct display *display,
1886 struct window *window,
1887 EGLContext ctx)
1888{
1889 struct egl_window_surface_data *data;
1890
1891 if (!window->cairo_surface)
1892 return;
1893
1894 if (!ctx)
1895 ctx = display->ctx;
1896
1897 data = cairo_surface_get_user_data(window->cairo_surface,
1898 &surface_data_key);
1899
1900 cairo_device_acquire(display->device);
1901 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
1902 fprintf(stderr, "failed to make surface current\n");
1903}
1904
1905void
1906display_release(struct display *display)
1907{
1908 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->ctx))
1909 fprintf(stderr, "failed to make context current\n");
1910 cairo_device_release(display->device);
1911}
1912
1913void
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001914display_run(struct display *d)
1915{
1916 g_main_loop_run(d->loop);
1917}