blob: 4792710c3c44463cc79943c2225f670326c379d3 [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
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100755static void
756window_resize_cairo_window_surface(struct window *window)
757{
758 struct egl_window_surface_data *data;
759
760 data = cairo_surface_get_user_data(window->cairo_surface,
761 &surface_data_key);
762
763 wl_egl_window_resize(data->window,
764 window->allocation.width,
765 window->allocation.height, 0, 0);
766 cairo_gl_surface_set_size(window->cairo_surface,
767 window->allocation.width,
768 window->allocation.height);
769}
770
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400771void
772window_create_surface(struct window *window)
773{
774 cairo_surface_t *surface;
775
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400776 switch (window->buffer_type) {
Kristian Høgsberg8def2642011-01-14 17:41:33 -0500777#ifdef HAVE_CAIRO_EGL
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100778 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
779 if (window->cairo_surface) {
780 window_resize_cairo_window_surface(window);
781 return;
782 }
783 surface = display_create_surface(window->display,
784 window->surface,
785 &window->allocation);
786 break;
Kristian Høgsberg297c6312011-02-04 14:11:33 -0500787 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400788 surface = display_create_surface(window->display,
Benjamin Franzke6693ac22011-02-10 12:04:30 +0100789 NULL,
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400790 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400791 break;
792#endif
793 case WINDOW_BUFFER_TYPE_SHM:
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400794 surface = display_create_shm_surface(window->display,
795 &window->allocation);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400796 break;
Bryce Harrington515f63a2010-11-19 12:14:55 -0800797 default:
798 surface = NULL;
799 break;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400800 }
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400801
802 window_set_surface(window, surface);
803 cairo_surface_destroy(surface);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400804}
805
806static void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500807window_draw_menu(struct window *window)
808{
809 cairo_t *cr;
810 int width, height, r = 5;
811
812 window_create_surface(window);
813
814 cr = cairo_create(window->cairo_surface);
815 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
816 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
817 cairo_paint(cr);
818
819 width = window->allocation.width;
820 height = window->allocation.height;
821 rounded_rect(cr, r, r, width - r, height - r, r);
822 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
823 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
824 cairo_fill(cr);
825 cairo_destroy(cr);
826}
827
828static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500829window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500830{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500831 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500832 cairo_text_extents_t extents;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400833 cairo_surface_t *frame;
834 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500835
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400836 window_create_surface(window);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -0400837
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400838 width = window->allocation.width;
839 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500840
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500841 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500842
Kristian Høgsberg09531622010-06-14 23:22:15 -0400843 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400844 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400845 cairo_paint(cr);
846
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400847 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400848 tile_mask(cr, window->display->shadow,
849 shadow_dx, shadow_dy, width, height,
850 window->margin + 10 - shadow_dx,
851 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500852
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400853 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400854 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400855 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400856 frame = window->display->inactive_frame;
857
858 tile_source(cr, frame, 0, 0, width, height,
859 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500860
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500861 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
862 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500863 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400864 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400865 if (window->keyboard_device)
866 cairo_set_source_rgb(cr, 0, 0, 0);
867 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400868 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400869 cairo_show_text(cr, window->title);
870
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500871 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400872
873 cairo_device_flush (window->display->device);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500874}
875
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400876void
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500877window_destroy(struct window *window)
878{
879 wl_surface_destroy(window->surface);
880 wl_list_remove(&window->link);
881 free(window);
882}
883
884void
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400885display_flush_cairo_device(struct display *display)
886{
887 cairo_device_flush (display->device);
888}
889
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500890void
891window_draw(struct window *window)
892{
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500893 if (window->parent)
894 window_draw_menu(window);
895 else if (window->fullscreen || !window->decoration)
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400896 window_create_surface(window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500897 else
898 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500899}
900
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400901cairo_surface_t *
902window_get_surface(struct window *window)
903{
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400904 return cairo_surface_reference(window->cairo_surface);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400905}
906
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400907static int
908get_pointer_location(struct window *window, int32_t x, int32_t y)
909{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400910 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400911 const int grip_size = 8;
912
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500913 if (!window->decoration)
914 return WINDOW_CLIENT_AREA;
915
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400916 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400917 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400918 else if (window->margin <= x && x < window->margin + grip_size)
919 hlocation = WINDOW_RESIZING_LEFT;
920 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400921 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400922 else if (x < window->allocation.width - window->margin)
923 hlocation = WINDOW_RESIZING_RIGHT;
924 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400925 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400926
927 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400928 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400929 else if (window->margin <= y && y < window->margin + grip_size)
930 vlocation = WINDOW_RESIZING_TOP;
931 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400932 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400933 else if (y < window->allocation.height - window->margin)
934 vlocation = WINDOW_RESIZING_BOTTOM;
935 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400936 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400937
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400938 location = vlocation | hlocation;
939 if (location & WINDOW_EXTERIOR)
940 location = WINDOW_EXTERIOR;
941 if (location == WINDOW_INTERIOR && y < window->margin + 50)
942 location = WINDOW_TITLEBAR;
943 else if (location == WINDOW_INTERIOR)
944 location = WINDOW_CLIENT_AREA;
945
946 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400947}
948
949static void
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400950set_pointer_image(struct input *input, uint32_t time, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400951{
952 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400953 struct wl_buffer *buffer;
954 cairo_surface_t *surface;
955 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400956
957 location = get_pointer_location(input->pointer_focus,
958 input->sx, input->sy);
959 switch (location) {
960 case WINDOW_RESIZING_TOP:
961 pointer = POINTER_TOP;
962 break;
963 case WINDOW_RESIZING_BOTTOM:
964 pointer = POINTER_BOTTOM;
965 break;
966 case WINDOW_RESIZING_LEFT:
967 pointer = POINTER_LEFT;
968 break;
969 case WINDOW_RESIZING_RIGHT:
970 pointer = POINTER_RIGHT;
971 break;
972 case WINDOW_RESIZING_TOP_LEFT:
973 pointer = POINTER_TOP_LEFT;
974 break;
975 case WINDOW_RESIZING_TOP_RIGHT:
976 pointer = POINTER_TOP_RIGHT;
977 break;
978 case WINDOW_RESIZING_BOTTOM_LEFT:
979 pointer = POINTER_BOTTOM_LEFT;
980 break;
981 case WINDOW_RESIZING_BOTTOM_RIGHT:
982 pointer = POINTER_BOTTOM_RIGHT;
983 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400984 case WINDOW_EXTERIOR:
985 case WINDOW_TITLEBAR:
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400986 if (input->current_pointer_image == POINTER_DEFAULT)
987 return;
988
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400989 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400990 input->current_pointer_image = POINTER_DEFAULT;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400991 return;
992 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400993 break;
994 }
995
Kristian Høgsberg7d804062010-09-07 21:50:06 -0400996 if (pointer == input->current_pointer_image)
997 return;
998
999 input->current_pointer_image = pointer;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001000 surface = display->pointer_surfaces[pointer];
1001 buffer = display_get_buffer_for_surface(display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001002 wl_input_device_attach(input->input_device, time, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001003 pointer_images[pointer].hotspot_x,
1004 pointer_images[pointer].hotspot_y);
1005}
1006
Kristian Høgsberge4feb562008-11-08 18:53:37 -05001007static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001008window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001009 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001010 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001011{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001012 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001013 struct window *window = input->pointer_focus;
Kristian Høgsberg00439612011-01-25 15:16:01 -05001014 int pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001015
1016 input->x = x;
1017 input->y = y;
1018 input->sx = sx;
1019 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001020
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001021 if (window->motion_handler)
1022 pointer = (*window->motion_handler)(window, input, time,
1023 x, y, sx, sy,
1024 window->user_data);
1025
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001026 set_pointer_image(input, time, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001027}
1028
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001029static void
1030window_handle_button(void *data,
1031 struct wl_input_device *input_device,
1032 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001033{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001034 struct input *input = data;
1035 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001036 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001037
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001038 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001039
1040 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001041 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001042 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001043 wl_shell_move(window->display->shell,
1044 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001045 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -04001046 case WINDOW_RESIZING_TOP:
1047 case WINDOW_RESIZING_BOTTOM:
1048 case WINDOW_RESIZING_LEFT:
1049 case WINDOW_RESIZING_RIGHT:
1050 case WINDOW_RESIZING_TOP_LEFT:
1051 case WINDOW_RESIZING_TOP_RIGHT:
1052 case WINDOW_RESIZING_BOTTOM_LEFT:
1053 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001054 wl_shell_resize(window->display->shell,
1055 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001056 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001057 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001058 case WINDOW_CLIENT_AREA:
1059 if (window->button_handler)
1060 (*window->button_handler)(window,
1061 input, time,
1062 button, state,
1063 window->user_data);
1064 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001065 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001066 } else {
1067 if (window->button_handler)
1068 (*window->button_handler)(window,
1069 input, time,
1070 button, state,
1071 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001072 }
1073}
1074
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001075static void
1076window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001077 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001078{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001079 struct input *input = data;
1080 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001081 struct display *d = window->display;
1082 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001083
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001084 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001085 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001086 return;
1087
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001088 level = 0;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001089 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001090 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1091 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001092
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001093 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1094
1095 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001096 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001097 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001098 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001099
1100 if (window->key_handler)
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001101 (*window->key_handler)(window, input, time, key, sym, state,
1102 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001103}
1104
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001105static void
1106window_handle_pointer_focus(void *data,
1107 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -04001108 uint32_t time, struct wl_surface *surface,
1109 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001110{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001111 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001112 struct window *window;
1113 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001114
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001115 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001116 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001117 window = input->pointer_focus;
1118
Kristian Høgsberg59826582011-01-20 11:56:57 -05001119 input->x = x;
1120 input->y = y;
1121 input->sx = sx;
1122 input->sy = sy;
1123
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001124 pointer = POINTER_LEFT_PTR;
1125 if (window->motion_handler)
1126 pointer = (*window->motion_handler)(window,
1127 input, time,
1128 x, y, sx, sy,
1129 window->user_data);
1130
Kristian Høgsbergce457ba2010-09-14 15:39:45 -04001131 set_pointer_image(input, time, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001132 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001133 input->pointer_focus = NULL;
Kristian Høgsberg7d804062010-09-07 21:50:06 -04001134 input->current_pointer_image = POINTER_UNSET;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001135 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001136}
1137
1138static void
1139window_handle_keyboard_focus(void *data,
1140 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001141 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001142 struct wl_surface *surface,
1143 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001144{
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001145 struct input *input = data;
1146 struct window *window = input->keyboard_focus;
1147 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001148 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001149
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001150 window = input->keyboard_focus;
1151 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001152 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001153 if (window->keyboard_focus_handler)
1154 (*window->keyboard_focus_handler)(window, NULL,
1155 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -05001156 }
1157
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001158 if (surface)
1159 input->keyboard_focus = wl_surface_get_user_data(surface);
1160 else
1161 input->keyboard_focus = NULL;
1162
1163 end = keys->data + keys->size;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001164 input->modifiers = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001165 for (k = keys->data; k < end; k++)
1166 input->modifiers |= d->xkb->map->modmap[*k];
1167
1168 window = input->keyboard_focus;
1169 if (window) {
1170 window->keyboard_device = input;
1171 if (window->keyboard_focus_handler)
1172 (*window->keyboard_focus_handler)(window,
1173 window->keyboard_device,
1174 window->user_data);
1175 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001176}
1177
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001178static const struct wl_input_device_listener input_device_listener = {
1179 window_handle_motion,
1180 window_handle_button,
1181 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001182 window_handle_pointer_focus,
1183 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001184};
1185
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001186void
1187input_get_position(struct input *input, int32_t *x, int32_t *y)
1188{
1189 *x = input->sx;
1190 *y = input->sy;
1191}
1192
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -04001193struct wl_input_device *
1194input_get_input_device(struct input *input)
1195{
1196 return input->input_device;
1197}
1198
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001199uint32_t
1200input_get_modifiers(struct input *input)
1201{
1202 return input->modifiers;
1203}
1204
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001205struct wl_drag *
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001206window_create_drag(struct window *window)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001207{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001208 cairo_device_flush (window->display->device);
1209
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001210 return wl_shell_create_drag(window->display->shell);
1211}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001212
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001213void
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001214window_move(struct window *window, struct input *input, uint32_t time)
1215{
1216 wl_shell_move(window->display->shell,
1217 window->surface, input->input_device, time);
1218}
1219
1220void
Kristian Høgsberg41da9082010-11-30 14:01:07 -05001221window_activate_drag(struct wl_drag *drag, struct window *window,
1222 struct input *input, uint32_t time)
1223{
1224 wl_drag_activate(drag, window->surface, input->input_device, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001225}
1226
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001227static void
1228handle_configure(void *data, struct wl_shell *shell,
1229 uint32_t time, uint32_t edges,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001230 struct wl_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001231{
1232 struct window *window = wl_surface_get_user_data(surface);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001233 int32_t child_width, child_height;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001234
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001235 /* FIXME: this is probably the wrong place to check for width
1236 * or height <= 0, but it prevents the compositor from crashing
1237 */
1238 if (width <= 0 || height <= 0)
Tim Wiederhake8a6f7e32011-01-17 12:40:01 +01001239 return;
1240
Tim Wiederhakeb6761dc2011-01-17 17:50:07 +01001241 window->resize_edges = edges;
1242
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001243 if (window->resize_handler) {
1244 child_width = width - 20 - window->margin * 2;
1245 child_height = height - 60 - window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001246
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001247 (*window->resize_handler)(window,
1248 child_width, child_height,
1249 window->user_data);
1250 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001251 window->allocation.width = width;
1252 window->allocation.height = height;
1253
1254 if (window->redraw_handler)
1255 window_schedule_redraw(window);
1256 }
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001257}
1258
1259static const struct wl_shell_listener shell_listener = {
1260 handle_configure,
1261};
1262
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001263void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001264window_get_child_allocation(struct window *window,
1265 struct rectangle *allocation)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001266{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001267 if (window->fullscreen || !window->decoration) {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001268 *allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001269 } else {
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001270 allocation->x = window->margin + 10;
1271 allocation->y = window->margin + 50;
1272 allocation->width =
1273 window->allocation.width - 20 - window->margin * 2;
1274 allocation->height =
1275 window->allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001276 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001277}
1278
1279void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001280window_set_child_size(struct window *window, int32_t width, int32_t height)
Kristian Høgsberg22106762008-12-08 13:50:07 -05001281{
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001282 if (!window->fullscreen) {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001283 window->allocation.x = 20 + window->margin;
1284 window->allocation.y = 60 + window->margin;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001285 window->allocation.width = width + 20 + window->margin * 2;
1286 window->allocation.height = height + 60 + window->margin * 2;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001287 } else {
1288 window->allocation.x = 0;
1289 window->allocation.y = 0;
1290 window->allocation.width = width;
1291 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001292 }
Kristian Høgsberg22106762008-12-08 13:50:07 -05001293}
1294
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001295static gboolean
1296idle_redraw(void *data)
1297{
1298 struct window *window = data;
1299
1300 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001301
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001302 window->redraw_scheduled = 0;
1303
1304 return FALSE;
1305}
1306
1307void
1308window_schedule_redraw(struct window *window)
1309{
1310 if (!window->redraw_scheduled) {
1311 g_idle_add(idle_redraw, window);
1312 window->redraw_scheduled = 1;
1313 }
1314}
1315
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001316void
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001317window_set_fullscreen(struct window *window, int fullscreen)
1318{
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001319 int32_t width, height;
1320
1321 if (window->fullscreen == fullscreen)
1322 return;
1323
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001324 window->fullscreen = fullscreen;
1325 if (window->fullscreen) {
1326 window->saved_allocation = window->allocation;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001327 width = window->display->screen_allocation.width;
1328 height = window->display->screen_allocation.height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001329 } else {
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001330 width = window->saved_allocation.width - 20 - window->margin * 2;
1331 height = window->saved_allocation.height - 60 - window->margin * 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001332 }
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001333
1334 (*window->resize_handler)(window, width, height, window->user_data);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001335}
1336
1337void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001338window_set_decoration(struct window *window, int decoration)
1339{
1340 window->decoration = decoration;
1341}
1342
1343void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001344window_set_user_data(struct window *window, void *data)
1345{
1346 window->user_data = data;
1347}
1348
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001349void *
1350window_get_user_data(struct window *window)
1351{
1352 return window->user_data;
1353}
1354
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001355void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001356window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001357 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001358{
1359 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001360}
1361
1362void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001363window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001364 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001365{
1366 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001367}
1368
1369void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001370window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001371 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001372{
1373 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001374}
1375
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001376void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001377window_set_button_handler(struct window *window,
1378 window_button_handler_t handler)
1379{
1380 window->button_handler = handler;
1381}
1382
1383void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001384window_set_motion_handler(struct window *window,
1385 window_motion_handler_t handler)
1386{
1387 window->motion_handler = handler;
1388}
1389
1390void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001391window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001392 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001393{
1394 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001395}
1396
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001397void
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001398window_set_title(struct window *window, const char *title)
1399{
Kristian Høgsbergd5fb9cc2011-01-25 12:45:37 -05001400 free(window->title);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001401 window->title = strdup(title);
1402}
1403
1404const char *
1405window_get_title(struct window *window)
1406{
1407 return window->title;
1408}
1409
1410void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001411window_damage(struct window *window, int32_t x, int32_t y,
1412 int32_t width, int32_t height)
1413{
1414 wl_surface_damage(window->surface, x, y, width, height);
1415}
1416
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001417static struct window *
1418window_create_internal(struct display *display, struct window *parent,
1419 int32_t width, int32_t height)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001420{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -05001421 struct window *window;
1422
1423 window = malloc(sizeof *window);
1424 if (window == NULL)
1425 return NULL;
1426
Kristian Høgsberg78231c82008-11-08 15:06:01 -05001427 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -05001428 window->display = display;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001429 window->parent = parent;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001430 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001431 window->allocation.x = 0;
1432 window->allocation.y = 0;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001433 window->allocation.width = width;
1434 window->allocation.height = height;
1435 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -05001436 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001437 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001438
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001439 if (display->dpy)
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001440 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1441 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
Yuval Fledel45568f62010-12-06 09:18:12 -05001442 else
1443 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001444
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001445 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001446 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001447
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001448 return window;
1449}
1450
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05001451struct window *
1452window_create(struct display *display, int32_t width, int32_t height)
1453{
1454 struct window *window;
1455
1456 window = window_create_internal(display, NULL, width, height);
1457 if (!window)
1458 return NULL;
1459
1460 return window;
1461}
1462
1463struct window *
1464window_create_transient(struct display *display, struct window *parent,
1465 int32_t x, int32_t y, int32_t width, int32_t height)
1466{
1467 struct window *window;
1468
1469 window = window_create_internal(parent->display,
1470 parent, width, height);
1471 if (!window)
1472 return NULL;
1473
1474 window->x = x;
1475 window->y = y;
1476
1477 return window;
1478}
1479
1480void
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001481window_set_buffer_type(struct window *window, enum window_buffer_type type)
1482{
1483 window->buffer_type = type;
1484}
1485
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001486static void
1487display_handle_geometry(void *data,
1488 struct wl_output *output,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001489 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001490{
1491 struct display *display = data;
1492
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -05001493 display->screen_allocation.x = x;
1494 display->screen_allocation.y = y;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001495 display->screen_allocation.width = width;
1496 display->screen_allocation.height = height;
1497}
1498
1499static const struct wl_output_listener output_listener = {
1500 display_handle_geometry,
1501};
1502
1503static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001504display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001505{
1506 struct input *input;
1507
1508 input = malloc(sizeof *input);
1509 if (input == NULL)
1510 return;
1511
1512 memset(input, 0, sizeof *input);
1513 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001514 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001515 input->pointer_focus = NULL;
1516 input->keyboard_focus = NULL;
1517 wl_list_insert(d->input_list.prev, &input->link);
1518
1519 wl_input_device_add_listener(input->input_device,
1520 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001521 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001522}
1523
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001524struct selection_offer {
1525 struct display *display;
1526 struct wl_selection_offer *offer;
1527 struct wl_array types;
1528 struct input *input;
1529};
1530
1531int
1532input_offers_mime_type(struct input *input, const char *type)
1533{
1534 struct selection_offer *offer = input->offer;
1535 char **p, **end;
1536
1537 if (offer == NULL)
1538 return 0;
1539
1540 end = offer->types.data + offer->types.size;
1541 for (p = offer->types.data; p < end; p++)
1542 if (strcmp(*p, type) == 0)
1543 return 1;
1544
1545 return 0;
1546}
1547
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001548void
1549input_receive_mime_type(struct input *input, const char *type, int fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001550{
1551 struct selection_offer *offer = input->offer;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001552
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001553 /* FIXME: A number of things can go wrong here: the object may
1554 * not be the current selection offer any more (which could
1555 * still work, but the source may have gone away or just
1556 * destroyed its wl_selection) or the offer may not have the
1557 * requested type after all (programmer/client error,
1558 * typically) */
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001559 wl_selection_offer_receive(offer->offer, type, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001560}
1561
1562static void
1563selection_offer_offer(void *data,
1564 struct wl_selection_offer *selection_offer,
1565 const char *type)
1566{
1567 struct selection_offer *offer = data;
1568
1569 char **p;
1570
1571 p = wl_array_add(&offer->types, sizeof *p);
1572 if (p)
1573 *p = strdup(type);
1574};
1575
1576static void
1577selection_offer_keyboard_focus(void *data,
1578 struct wl_selection_offer *selection_offer,
1579 struct wl_input_device *input_device)
1580{
1581 struct selection_offer *offer = data;
1582 struct input *input;
1583 char **p, **end;
1584
1585 if (input_device == NULL) {
1586 printf("selection offer retracted %p\n", selection_offer);
1587 input = offer->input;
1588 input->offer = NULL;
1589 wl_selection_offer_destroy(selection_offer);
1590 wl_array_release(&offer->types);
1591 free(offer);
1592 return;
1593 }
1594
1595 input = wl_input_device_get_user_data(input_device);
1596 printf("new selection offer %p:", selection_offer);
1597
1598 offer->input = input;
1599 input->offer = offer;
1600 end = offer->types.data + offer->types.size;
1601 for (p = offer->types.data; p < end; p++)
1602 printf(" %s", *p);
1603
1604 printf("\n");
1605}
1606
1607struct wl_selection_offer_listener selection_offer_listener = {
1608 selection_offer_offer,
1609 selection_offer_keyboard_focus
1610};
1611
1612static void
1613add_selection_offer(struct display *d, uint32_t id)
1614{
1615 struct selection_offer *offer;
1616
1617 offer = malloc(sizeof *offer);
1618 if (offer == NULL)
1619 return;
1620
1621 offer->offer = wl_selection_offer_create(d->display, id);
1622 offer->display = d;
1623 wl_array_init(&offer->types);
1624 offer->input = NULL;
1625
1626 wl_selection_offer_add_listener(offer->offer,
1627 &selection_offer_listener, offer);
1628}
1629
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001630static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001631display_handle_global(struct wl_display *display, uint32_t id,
1632 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001633{
1634 struct display *d = data;
1635
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001636 if (strcmp(interface, "compositor") == 0) {
1637 d->compositor = wl_compositor_create(display, id);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001638 } else if (strcmp(interface, "output") == 0) {
1639 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001640 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001641 } else if (strcmp(interface, "input_device") == 0) {
1642 display_add_input(d, id);
1643 } else if (strcmp(interface, "shell") == 0) {
1644 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001645 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsbergd0c3b9d2010-10-25 11:40:03 -04001646 } else if (strcmp(interface, "shm") == 0) {
1647 d->shm = wl_shm_create(display, id);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001648 } else if (strcmp(interface, "selection_offer") == 0) {
1649 add_selection_offer(d, id);
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001650 } else if (d->global_handler) {
Kristian Høgsberg943741c2011-01-18 09:23:13 -05001651 d->global_handler(d, interface, id, version);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001652 }
1653}
1654
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001655static void
1656display_render_frame(struct display *d)
1657{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001658 int radius = 8;
1659 cairo_t *cr;
1660
1661 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1662 cr = cairo_create(d->shadow);
1663 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1664 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1665 rounded_rect(cr, 16, 16, 112, 112, radius);
1666 cairo_fill(cr);
1667 cairo_destroy(cr);
1668 blur_surface(d->shadow, 64);
1669
1670 d->active_frame =
1671 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1672 cr = cairo_create(d->active_frame);
1673 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1674 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1675 rounded_rect(cr, 16, 16, 112, 112, radius);
1676 cairo_fill(cr);
1677 cairo_destroy(cr);
1678
1679 d->inactive_frame =
1680 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1681 cr = cairo_create(d->inactive_frame);
1682 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1683 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1684 rounded_rect(cr, 16, 16, 112, 112, radius);
1685 cairo_fill(cr);
1686 cairo_destroy(cr);
1687}
1688
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001689static void
1690init_xkb(struct display *d)
1691{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001692 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001693
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001694 names.rules = "evdev";
1695 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001696 names.layout = option_xkb_layout;
1697 names.variant = option_xkb_variant;
1698 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001699
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001700 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001701 if (!d->xkb) {
1702 fprintf(stderr, "Failed to compile keymap\n");
1703 exit(1);
1704 }
1705}
1706
Yuval Fledel45568f62010-12-06 09:18:12 -05001707static int
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001708init_egl(struct display *d)
Yuval Fledel45568f62010-12-06 09:18:12 -05001709{
1710 EGLint major, minor;
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001711 EGLint n;
1712 static const EGLint cfg_attribs[] = {
1713 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
1714 EGL_RED_SIZE, 1,
1715 EGL_GREEN_SIZE, 1,
1716 EGL_BLUE_SIZE, 1,
1717 EGL_ALPHA_SIZE, 1,
1718 EGL_DEPTH_SIZE, 1,
1719 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
1720 EGL_NONE
1721 };
Yuval Fledel45568f62010-12-06 09:18:12 -05001722
Kristian Høgsberg297d6dd2011-02-09 10:51:15 -05001723 d->dpy = eglGetDisplay(d->native_dpy);
Yuval Fledel45568f62010-12-06 09:18:12 -05001724 if (!eglInitialize(d->dpy, &major, &minor)) {
1725 fprintf(stderr, "failed to initialize display\n");
1726 return -1;
1727 }
1728
1729 if (!eglBindAPI(EGL_OPENGL_API)) {
1730 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1731 return -1;
1732 }
1733
Benjamin Franzke6693ac22011-02-10 12:04:30 +01001734 if (!eglChooseConfig(d->dpy, cfg_attribs, &d->conf, 1, &n) || n != 1) {
1735 fprintf(stderr, "failed to choose config\n");
1736 return -1;
1737 }
1738
1739 d->ctx = eglCreateContext(d->dpy, d->conf, EGL_NO_CONTEXT, NULL);
Yuval Fledel45568f62010-12-06 09:18:12 -05001740 if (d->ctx == NULL) {
1741 fprintf(stderr, "failed to create context\n");
1742 return -1;
1743 }
1744
1745 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
Tim Wiederhake9c7a8cc2011-02-11 19:37:40 +01001746 fprintf(stderr, "failed to make context current\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001747 return -1;
1748 }
1749
Kristian Høgsberg8def2642011-01-14 17:41:33 -05001750#ifdef HAVE_CAIRO_EGL
Yuval Fledel45568f62010-12-06 09:18:12 -05001751 d->device = cairo_egl_device_create(d->dpy, d->ctx);
1752 if (d->device == NULL) {
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001753 fprintf(stderr, "failed to get cairo egl device\n");
Yuval Fledel45568f62010-12-06 09:18:12 -05001754 return -1;
1755 }
1756#endif
1757
1758 return 0;
1759}
1760
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001761struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001762display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001763{
1764 struct display *d;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001765 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001766 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001767 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001768
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001769 g_type_init();
1770
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001771 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001772 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001773 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001774
1775 xkb_option_group = g_option_group_new("xkb",
1776 "Keyboard options",
1777 "Show all XKB options",
1778 NULL, NULL);
1779 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1780 g_option_context_add_group (context, xkb_option_group);
1781
1782 if (!g_option_context_parse(context, argc, argv, &error)) {
1783 fprintf(stderr, "option parsing failed: %s\n", error->message);
1784 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001785 }
1786
Tim Wiederhake748f6722011-01-23 23:25:25 +01001787 g_option_context_free(context);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001788
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001789 d = malloc(sizeof *d);
1790 if (d == NULL)
1791 return NULL;
1792
Tim Wiederhake81bd9792011-01-23 23:25:26 +01001793 memset(d, 0, sizeof *d);
1794
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05001795 d->display = wl_display_connect(NULL);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001796 if (d->display == NULL) {
1797 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001798 return NULL;
1799 }
1800
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001801 wl_list_init(&d->input_list);
1802
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001803 /* Set up listener so we'll catch all events. */
1804 wl_display_add_global_listener(d->display,
1805 display_handle_global, d);
1806
Kristian Høgsbergbfb8e612011-02-07 10:30:38 -05001807 d->native_dpy = wl_egl_display_create(d->display);
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001808
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001809 /* Process connection events. */
1810 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1811
Kristian Høgsberg297c6312011-02-04 14:11:33 -05001812 if (init_egl(d) < 0)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001813 return NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001814
Kristian Høgsberg0d5007a2011-02-09 10:57:44 -05001815 d->image_target_texture_2d =
1816 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1817 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1818 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1819
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001820 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001821
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001822 display_render_frame(d);
1823
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001824 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001825 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001826 g_source_attach(d->source, NULL);
1827
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001828 wl_list_init(&d->window_list);
1829
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001830 init_xkb(d);
1831
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001832 return d;
1833}
1834
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001835struct wl_display *
1836display_get_display(struct display *display)
1837{
1838 return display->display;
1839}
1840
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001841struct wl_compositor *
1842display_get_compositor(struct display *display)
1843{
1844 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001845}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001846
1847EGLDisplay
1848display_get_egl_display(struct display *d)
1849{
1850 return d->dpy;
1851}
1852
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001853struct wl_shell *
1854display_get_shell(struct display *display)
1855{
1856 return display->shell;
1857}
1858
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001859void
1860display_run(struct display *d)
1861{
1862 g_main_loop_run(d->loop);
1863}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001864
1865void
Kristian Høgsbergb46df052011-01-18 09:17:57 -05001866display_set_global_handler(struct display *display,
1867 display_global_handler_t handler)
1868{
1869 display->global_handler = handler;
1870}