blob: b6074e57449035a3a853de484785b994c9e76050 [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øgsberg61017b12008-11-02 18:51:48 -050023#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <fcntl.h>
28#include <unistd.h>
29#include <math.h>
30#include <time.h>
31#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050032#include <glib.h>
Kristian Høgsberg478d9262010-06-08 20:34:11 -040033#include <glib-object.h>
Kristian Høgsbergda275dd2010-08-16 17:47:07 -040034#include <gdk-pixbuf/gdk-pixbuf.h>
Kristian Høgsberg640609a2010-08-09 22:11:47 -040035#include <xf86drm.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040036
37#define EGL_EGLEXT_PROTOTYPES 1
38#define GL_GLEXT_PROTOTYPES 1
39#include <GL/gl.h>
40#include <EGL/egl.h>
41#include <EGL/eglext.h>
42#include <cairo-gl.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050043
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040044#include <X11/extensions/XKBcommon.h>
45
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050046#include <linux/input.h>
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050047#include "wayland-util.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050048#include "wayland-client.h"
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050049#include "wayland-glib.h"
Kristian Høgsbergb91cd102010-08-16 16:17:42 -040050#include "cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050051
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050052#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050053
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050054struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050055 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050056 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040057 struct wl_shell *shell;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -040058 struct wl_drm *drm;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050059 struct wl_output *output;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050060 struct rectangle screen_allocation;
Kristian Høgsberg640609a2010-08-09 22:11:47 -040061 int authenticated;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040062 EGLDisplay dpy;
63 EGLContext ctx;
Janusz Lewandowskid923e9d2010-01-31 03:01:26 +010064 cairo_device_t *device;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050065 int fd;
Kristian Høgsberg7824d812010-06-08 14:59:44 -040066 GMainLoop *loop;
67 GSource *source;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040068 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040069 struct wl_list input_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040070 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040071 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040072 struct xkb_desc *xkb;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040073 cairo_surface_t **pointer_surfaces;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050074};
75
76struct window {
77 struct display *display;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050078 struct wl_surface *surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050079 const char *title;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040080 struct rectangle allocation, saved_allocation, pending_allocation;
81 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040082 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050083 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050084 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050085 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040086 int decoration;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040087 struct input *grab_device;
88 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050089 uint32_t name;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050090
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040091 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -050092 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050093
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050094 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040095 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050096 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040097 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050098 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040099 window_acknowledge_handler_t acknowledge_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400100 window_frame_handler_t frame_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400101 window_motion_handler_t motion_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400102
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500103 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400104 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500105};
106
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400107struct input {
108 struct display *display;
109 struct wl_input_device *input_device;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400110 struct wl_drag *drag;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400111 struct window *pointer_focus;
112 struct window *keyboard_focus;
113 uint32_t modifiers;
114 int32_t x, y, sx, sy;
115 struct wl_list link;
116};
117
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400118const char *option_xkb_layout = "us";
119const char *option_xkb_variant = "";
120const char *option_xkb_options = "";
121
122static const GOptionEntry xkb_option_entries[] = {
123 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
124 &option_xkb_layout, "XKB Layout" },
125 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
126 &option_xkb_variant, "XKB Variant" },
127 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
128 &option_xkb_options, "XKB Options" },
129 { NULL }
130};
131
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500132static void
133rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
134{
135 cairo_move_to(cr, x0, y0 + radius);
136 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
137 cairo_line_to(cr, x1 - radius, y0);
138 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
139 cairo_line_to(cr, x1, y1 - radius);
140 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
141 cairo_line_to(cr, x0 + radius, y1);
142 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
143 cairo_close_path(cr);
144}
145
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400146static int
147texture_from_png(const char *filename, int width, int height)
148{
149 GdkPixbuf *pixbuf;
150 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400151 int stride, i;
152 unsigned char *pixels, *p, *end;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400153
154 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
155 width, height,
156 FALSE, &error);
157 if (error != NULL)
158 return -1;
159
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400160 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
161 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
162 gdk_pixbuf_unref(pixbuf);
163 return -1;
164 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400165
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400166
167 stride = gdk_pixbuf_get_rowstride(pixbuf);
168 pixels = gdk_pixbuf_get_pixels(pixbuf);
169
170 for (i = 0; i < height; i++) {
171 p = pixels + i * stride;
172 end = p + width * 4;
173 while (p < end) {
174 unsigned int t;
175
176#define MULT(d,c,a,t) \
177 do { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } while (0)
178
179 MULT(p[0], p[0], p[3], t);
180 MULT(p[1], p[1], p[3], t);
181 MULT(p[2], p[2], p[3], t);
182 p += 4;
183
184 }
185 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400186
187 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400188 width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400189
190 gdk_pixbuf_unref(pixbuf);
191
192 return 0;
193}
194
195static const struct {
196 const char *filename;
197 int hotspot_x, hotspot_y;
198} pointer_images[] = {
199 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
200 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
201 { DATADIR "/wayland/bottom_side.png", 16, 20 },
202 { DATADIR "/wayland/grabbing.png", 20, 17 },
203 { DATADIR "/wayland/left_ptr.png", 10, 5 },
204 { DATADIR "/wayland/left_side.png", 10, 20 },
205 { DATADIR "/wayland/right_side.png", 30, 19 },
206 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
207 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
208 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400209 { DATADIR "/wayland/xterm.png", 15, 15 },
210 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400211};
212
213static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400214create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400215{
216 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400217 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400218 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400219
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400220 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400221 display->pointer_surfaces =
222 malloc(count * sizeof *display->pointer_surfaces);
223 rect.width = width;
224 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400225 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400226 display->pointer_surfaces[i] =
227 display_create_surface(display, &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400228 texture_from_png(pointer_images[i].filename, width, height);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400229 }
230
231}
232
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400233static const cairo_user_data_key_t surface_data_key;
234struct surface_data {
235 EGLImageKHR image;
236 GLuint texture;
237 EGLDisplay dpy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400238 struct wl_buffer *buffer;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400239};
240
241static void
242surface_data_destroy(void *p)
243{
244 struct surface_data *data = p;
245
246 glDeleteTextures(1, &data->texture);
247 eglDestroyImageKHR(data->dpy, data->image);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400248 if (data->buffer)
249 wl_buffer_destroy(data->buffer);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400250}
251
252cairo_surface_t *
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400253display_create_surface(struct display *display,
254 struct rectangle *rectangle)
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400255{
256 struct surface_data *data;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400257 EGLDisplay dpy = display->dpy;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400258 cairo_surface_t *surface;
259
260 EGLint image_attribs[] = {
261 EGL_WIDTH, 0,
262 EGL_HEIGHT, 0,
Kristian Høgsbergb12fcce2010-08-24 17:34:23 -0400263 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
264 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400265 EGL_NONE
266 };
267
268 data = malloc(sizeof *data);
269 image_attribs[1] = rectangle->width;
270 image_attribs[3] = rectangle->height;
271 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
272 glGenTextures(1, &data->texture);
273 data->dpy = dpy;
274 glBindTexture(GL_TEXTURE_2D, data->texture);
275 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400276 data->buffer = NULL;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400277
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400278 surface = cairo_gl_surface_create_for_texture(display->device,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400279 CAIRO_CONTENT_COLOR_ALPHA,
280 data->texture,
281 rectangle->width,
282 rectangle->height);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400283
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400284 cairo_surface_set_user_data (surface, &surface_data_key,
285 data, surface_data_destroy);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400286
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400287 return surface;
288}
289
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400290struct wl_buffer *
291display_get_buffer_for_surface(struct display *display,
292 cairo_surface_t *surface)
293{
294 struct surface_data *data;
295 struct wl_visual *visual;
296 struct wl_buffer *buffer;
297 EGLint name, stride;
298 int width, height;
299
300 data = cairo_surface_get_user_data (surface, &surface_data_key);
301 if (data->buffer)
302 return data->buffer;
303
304 width = cairo_gl_surface_get_width (surface);
305 height = cairo_gl_surface_get_height (surface);
306
307 eglExportDRMImageMESA(display->dpy, data->image, &name, NULL, &stride);
308
309 visual = wl_display_get_premultiplied_argb_visual(display->display);
310 buffer = wl_drm_create_buffer(display->drm,
311 name, width, height, stride, visual);
312 data->buffer = buffer;
313
314 return buffer;
315}
316
317cairo_surface_t *
318display_get_pointer_surface(struct display *display, int pointer,
319 int *width, int *height,
320 int *hotspot_x, int *hotspot_y)
321{
322 cairo_surface_t *surface;
323
324 surface = display->pointer_surfaces[pointer];
325 *width = cairo_gl_surface_get_width(surface);
326 *height = cairo_gl_surface_get_height(surface);
327 *hotspot_x = pointer_images[pointer].hotspot_x;
328 *hotspot_y = pointer_images[pointer].hotspot_y;
329
330 return cairo_surface_reference(surface);
331}
332
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500333static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500334window_attach_surface(struct window *window)
335{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400336 struct display *display = window->display;
337 struct wl_buffer *buffer;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500338
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500339 if (window->pending_surface != NULL)
340 return;
341
Kristian Høgsberg09531622010-06-14 23:22:15 -0400342 window->pending_surface = window->cairo_surface;
343 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500344
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400345 buffer = display_get_buffer_for_surface(display,
346 window->pending_surface);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400347 wl_surface_attach(window->surface, buffer);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500348
349 wl_surface_map(window->surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400350 window->allocation.x,
351 window->allocation.y,
352 window->allocation.width,
353 window->allocation.height);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400354
355 wl_compositor_commit(window->display->compositor, 0);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500356}
357
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500358void
359window_commit(struct window *window, uint32_t key)
360{
Kristian Høgsberg09531622010-06-14 23:22:15 -0400361 if (window->cairo_surface) {
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500362 window_attach_surface(window);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400363 } else {
364 wl_compositor_commit(window->display->compositor, key);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500365 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500366}
367
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500368static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500369window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500370{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500371 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500372 cairo_text_extents_t extents;
Kristian Høgsberg2d6b7c12010-06-25 16:51:57 -0400373 cairo_pattern_t *outline, *bright, *dim;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400374 cairo_surface_t *frame;
375 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500376
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500377 window->cairo_surface =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400378 display_create_surface(window->display, &window->allocation);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400379 width = window->allocation.width;
380 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500381
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500382 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500383 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500384 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
385
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500386 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500387
Kristian Høgsberg09531622010-06-14 23:22:15 -0400388 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400389 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400390 cairo_paint(cr);
391
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400392 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400393 tile_mask(cr, window->display->shadow,
394 shadow_dx, shadow_dy, width, height,
395 window->margin + 10 - shadow_dx,
396 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500397
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400398 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400399 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400400 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400401 frame = window->display->inactive_frame;
402
403 tile_source(cr, frame, 0, 0, width, height,
404 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500405
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500406 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
407 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500408 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400409 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500410 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
411 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
412 cairo_set_line_width (cr, 4);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400413 if (window->keyboard_device)
414 cairo_set_source_rgb(cr, 0, 0, 0);
415 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400416 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400417 cairo_show_text(cr, window->title);
418
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500419 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400420
421 cairo_device_flush (window->display->device);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500422}
423
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400424void
425display_flush_cairo_device(struct display *display)
426{
427 cairo_device_flush (display->device);
428}
429
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500430static void
431window_draw_fullscreen(struct window *window)
432{
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500433 window->cairo_surface =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400434 display_create_surface(window->display, &window->allocation);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500435}
436
437void
438window_draw(struct window *window)
439{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500440 if (window->cairo_surface != NULL)
441 cairo_surface_destroy(window->cairo_surface);
442
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400443 if (window->fullscreen || !window->decoration)
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500444 window_draw_fullscreen(window);
445 else
446 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500447}
448
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400449cairo_surface_t *
450window_get_surface(struct window *window)
451{
452 return window->cairo_surface;
453}
454
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400455enum window_location {
456 WINDOW_INTERIOR = 0,
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400457 WINDOW_RESIZING_TOP = 1,
458 WINDOW_RESIZING_BOTTOM = 2,
459 WINDOW_RESIZING_LEFT = 4,
460 WINDOW_RESIZING_TOP_LEFT = 5,
461 WINDOW_RESIZING_BOTTOM_LEFT = 6,
462 WINDOW_RESIZING_RIGHT = 8,
463 WINDOW_RESIZING_TOP_RIGHT = 9,
464 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
465 WINDOW_RESIZING_MASK = 15,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400466 WINDOW_EXTERIOR = 16,
467 WINDOW_TITLEBAR = 17,
468 WINDOW_CLIENT_AREA = 18,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400469};
470
471static int
472get_pointer_location(struct window *window, int32_t x, int32_t y)
473{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400474 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400475 const int grip_size = 8;
476
477 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400478 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400479 else if (window->margin <= x && x < window->margin + grip_size)
480 hlocation = WINDOW_RESIZING_LEFT;
481 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400482 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400483 else if (x < window->allocation.width - window->margin)
484 hlocation = WINDOW_RESIZING_RIGHT;
485 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400486 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400487
488 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400489 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400490 else if (window->margin <= y && y < window->margin + grip_size)
491 vlocation = WINDOW_RESIZING_TOP;
492 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400493 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400494 else if (y < window->allocation.height - window->margin)
495 vlocation = WINDOW_RESIZING_BOTTOM;
496 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400497 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400498
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400499 location = vlocation | hlocation;
500 if (location & WINDOW_EXTERIOR)
501 location = WINDOW_EXTERIOR;
502 if (location == WINDOW_INTERIOR && y < window->margin + 50)
503 location = WINDOW_TITLEBAR;
504 else if (location == WINDOW_INTERIOR)
505 location = WINDOW_CLIENT_AREA;
506
507 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400508}
509
510static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400511set_pointer_image(struct input *input, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400512{
513 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400514 struct wl_buffer *buffer;
515 cairo_surface_t *surface;
516 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400517
518 location = get_pointer_location(input->pointer_focus,
519 input->sx, input->sy);
520 switch (location) {
521 case WINDOW_RESIZING_TOP:
522 pointer = POINTER_TOP;
523 break;
524 case WINDOW_RESIZING_BOTTOM:
525 pointer = POINTER_BOTTOM;
526 break;
527 case WINDOW_RESIZING_LEFT:
528 pointer = POINTER_LEFT;
529 break;
530 case WINDOW_RESIZING_RIGHT:
531 pointer = POINTER_RIGHT;
532 break;
533 case WINDOW_RESIZING_TOP_LEFT:
534 pointer = POINTER_TOP_LEFT;
535 break;
536 case WINDOW_RESIZING_TOP_RIGHT:
537 pointer = POINTER_TOP_RIGHT;
538 break;
539 case WINDOW_RESIZING_BOTTOM_LEFT:
540 pointer = POINTER_BOTTOM_LEFT;
541 break;
542 case WINDOW_RESIZING_BOTTOM_RIGHT:
543 pointer = POINTER_BOTTOM_RIGHT;
544 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400545 case WINDOW_EXTERIOR:
546 case WINDOW_TITLEBAR:
547 wl_input_device_attach(input->input_device, NULL, 0, 0);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400548 return;
549 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400550 break;
551 }
552
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400553 surface = display->pointer_surfaces[pointer];
554 buffer = display_get_buffer_for_surface(display, surface);
555 wl_input_device_attach(input->input_device, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400556 pointer_images[pointer].hotspot_x,
557 pointer_images[pointer].hotspot_y);
558}
559
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500560static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500561window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400562 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500563 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500564{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400565 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400566 struct window *window = input->pointer_focus;
567 int location, pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400568
569 input->x = x;
570 input->y = y;
571 input->sx = sx;
572 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400573
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400574 location = get_pointer_location(window, input->sx, input->sy);
575
576 if (window->motion_handler)
577 pointer = (*window->motion_handler)(window, input, time,
578 x, y, sx, sy,
579 window->user_data);
580
581 set_pointer_image(input, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500582}
583
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400584static void
585window_handle_button(void *data,
586 struct wl_input_device *input_device,
587 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500588{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400589 struct input *input = data;
590 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400591 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400592
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400593 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500594
595 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400596 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400597 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400598 wl_shell_move(window->display->shell,
599 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500600 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400601 case WINDOW_RESIZING_TOP:
602 case WINDOW_RESIZING_BOTTOM:
603 case WINDOW_RESIZING_LEFT:
604 case WINDOW_RESIZING_RIGHT:
605 case WINDOW_RESIZING_TOP_LEFT:
606 case WINDOW_RESIZING_TOP_RIGHT:
607 case WINDOW_RESIZING_BOTTOM_LEFT:
608 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400609 wl_shell_resize(window->display->shell,
610 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400611 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500612 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400613 case WINDOW_CLIENT_AREA:
614 if (window->button_handler)
615 (*window->button_handler)(window,
616 input, time,
617 button, state,
618 window->user_data);
619 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500620 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400621 } else {
622 if (window->button_handler)
623 (*window->button_handler)(window,
624 input, time,
625 button, state,
626 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500627 }
628}
629
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500630static void
631window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400632 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500633{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400634 struct input *input = data;
635 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400636 struct display *d = window->display;
637 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500638
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400639 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400640 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500641 return;
642
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400643 level = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400644 if (input->modifiers & WINDOW_MODIFIER_SHIFT &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400645 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
646 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500647
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400648 sym = XkbKeySymEntry(d->xkb, code, level, 0);
649
650 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400651 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400652 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400653 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500654
655 if (window->key_handler)
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400656 (*window->key_handler)(window, key, sym, state,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400657 input->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500658}
659
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500660static void
661window_handle_pointer_focus(void *data,
662 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400663 uint32_t time, struct wl_surface *surface,
664 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500665{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400666 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400667 struct window *window;
668 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400669
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400670 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400671 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400672 window = input->pointer_focus;
673
674 pointer = POINTER_LEFT_PTR;
675 if (window->motion_handler)
676 pointer = (*window->motion_handler)(window,
677 input, time,
678 x, y, sx, sy,
679 window->user_data);
680
681 set_pointer_image(input, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400682 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400683 input->pointer_focus = NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400684 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500685}
686
687static void
688window_handle_keyboard_focus(void *data,
689 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400690 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500691 struct wl_surface *surface,
692 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500693{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400694 struct input *input = data;
695 struct window *window = input->keyboard_focus;
696 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500697 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500698
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400699 window = input->keyboard_focus;
700 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500701 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400702 if (window->keyboard_focus_handler)
703 (*window->keyboard_focus_handler)(window, NULL,
704 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500705 }
706
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400707 if (surface)
708 input->keyboard_focus = wl_surface_get_user_data(surface);
709 else
710 input->keyboard_focus = NULL;
711
712 end = keys->data + keys->size;
713 for (k = keys->data; k < end; k++)
714 input->modifiers |= d->xkb->map->modmap[*k];
715
716 window = input->keyboard_focus;
717 if (window) {
718 window->keyboard_device = input;
719 if (window->keyboard_focus_handler)
720 (*window->keyboard_focus_handler)(window,
721 window->keyboard_device,
722 window->user_data);
723 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500724}
725
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500726static const struct wl_input_device_listener input_device_listener = {
727 window_handle_motion,
728 window_handle_button,
729 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500730 window_handle_pointer_focus,
731 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500732};
733
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400734void
735input_get_position(struct input *input, int32_t *x, int32_t *y)
736{
737 *x = input->sx;
738 *y = input->sy;
739}
740
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400741struct wl_input_device *
742input_get_input_device(struct input *input)
743{
744 return input->input_device;
745}
746
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400747void
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400748display_add_drag_listener(struct display *display,
749 const struct wl_drag_listener *drag_listener,
750 void *data)
751{
752 struct input *input;
753
754 wl_list_for_each(input, &display->input_list, link)
755 wl_drag_add_listener(input->drag, drag_listener, data);
756}
757
758void
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400759window_start_drag(struct window *window, struct input *input, uint32_t time)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400760{
761 cairo_device_flush (window->display->device);
762
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400763 wl_drag_prepare(input->drag, window->surface, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400764 wl_drag_offer(input->drag, "text/plain");
765 wl_drag_activate(input->drag);
766}
767
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400768static void
769handle_configure(void *data, struct wl_shell *shell,
770 uint32_t time, uint32_t edges,
771 struct wl_surface *surface,
772 int32_t x, int32_t y, int32_t width, int32_t height)
773{
774 struct window *window = wl_surface_get_user_data(surface);
775
776 window->resize_edges = edges;
777 window->pending_allocation.x = x;
778 window->pending_allocation.y = y;
779 window->pending_allocation.width = width;
780 window->pending_allocation.height = height;
781
782 if (!(edges & 15))
783 return;
784
785 if (window->resize_handler)
786 (*window->resize_handler)(window,
787 window->user_data);
788 else if (window->redraw_handler)
789 window_schedule_redraw(window);
790}
791
792static const struct wl_shell_listener shell_listener = {
793 handle_configure,
794};
795
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500796void
797window_get_child_rectangle(struct window *window,
798 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500799{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400800 if (window->fullscreen && !window->decoration) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500801 *rectangle = window->allocation;
802 } else {
803 rectangle->x = window->margin + 10;
804 rectangle->y = window->margin + 50;
805 rectangle->width = window->allocation.width - 20 - window->margin * 2;
806 rectangle->height = window->allocation.height - 60 - window->margin * 2;
807 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500808}
809
810void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500811window_set_child_size(struct window *window,
812 struct rectangle *rectangle)
813{
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400814 int32_t width, height;
815
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500816 if (!window->fullscreen) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400817 width = rectangle->width + 20 + window->margin * 2;
818 height = rectangle->height + 60 + window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400819
820 if (window->resize_edges & WINDOW_RESIZING_LEFT)
821 window->allocation.x +=
822 window->allocation.width - width;
823 if (window->resize_edges & WINDOW_RESIZING_TOP)
824 window->allocation.y +=
825 window->allocation.height - height;
826
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400827 window->allocation.width = width;
828 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500829 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500830}
831
832void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400833window_copy_image(struct window *window,
834 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500835{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400836 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500837}
838
839void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500840window_copy_surface(struct window *window,
841 struct rectangle *rectangle,
842 cairo_surface_t *surface)
843{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500844 cairo_t *cr;
845
846 cr = cairo_create (window->cairo_surface);
847
848 cairo_set_source_surface (cr,
849 surface,
850 rectangle->x, rectangle->y);
851
852 cairo_paint (cr);
853 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500854}
855
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400856static gboolean
857idle_redraw(void *data)
858{
859 struct window *window = data;
860
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400861 if (window->resize_edges)
862 window->allocation = window->pending_allocation;
863
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400864 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400865
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400866 window->redraw_scheduled = 0;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400867 window->resize_edges = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400868
869 return FALSE;
870}
871
872void
873window_schedule_redraw(struct window *window)
874{
875 if (!window->redraw_scheduled) {
876 g_idle_add(idle_redraw, window);
877 window->redraw_scheduled = 1;
878 }
879}
880
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500881void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500882window_set_fullscreen(struct window *window, int fullscreen)
883{
884 window->fullscreen = fullscreen;
885 if (window->fullscreen) {
886 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500887 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500888 } else {
889 window->allocation = window->saved_allocation;
890 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500891}
892
893void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400894window_set_decoration(struct window *window, int decoration)
895{
896 window->decoration = decoration;
897}
898
899void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400900window_set_user_data(struct window *window, void *data)
901{
902 window->user_data = data;
903}
904
905void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500906window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400907 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500908{
909 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500910}
911
912void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400913window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400914 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400915{
916 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400917}
918
919void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500920window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400921 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500922{
923 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500924}
925
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500926void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400927window_set_button_handler(struct window *window,
928 window_button_handler_t handler)
929{
930 window->button_handler = handler;
931}
932
933void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400934window_set_acknowledge_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400935 window_acknowledge_handler_t handler)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400936{
937 window->acknowledge_handler = handler;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400938}
939
940void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400941window_set_frame_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400942 window_frame_handler_t handler)
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400943{
944 window->frame_handler = handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400945}
946
947void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400948window_set_motion_handler(struct window *window,
949 window_motion_handler_t handler)
950{
951 window->motion_handler = handler;
952}
953
954void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500955window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400956 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500957{
958 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500959}
960
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400961void
962window_move(struct window *window, int32_t x, int32_t y)
963{
964 window->allocation.x = x;
965 window->allocation.y = y;
966
967 wl_surface_map(window->surface,
968 window->allocation.x - window->margin,
969 window->allocation.y - window->margin,
970 window->allocation.width,
971 window->allocation.height);
972}
973
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500974struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500975window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500976 int32_t x, int32_t y, int32_t width, int32_t height)
977{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500978 struct window *window;
979
980 window = malloc(sizeof *window);
981 if (window == NULL)
982 return NULL;
983
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500984 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500985 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500986 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500987 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500988 window->allocation.x = x;
989 window->allocation.y = y;
990 window->allocation.width = width;
991 window->allocation.height = height;
992 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500993 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400994 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500995
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400996 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400997 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500998
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500999 return window;
1000}
1001
1002static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001003drm_handle_device(void *data, struct wl_drm *drm, const char *device)
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001004{
1005 struct display *d = data;
1006
1007 d->device_name = strdup(device);
1008}
1009
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001010static void drm_handle_authenticated(void *data, struct wl_drm *drm)
1011{
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001012 struct display *d = data;
1013
1014 d->authenticated = 1;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001015}
1016
1017static const struct wl_drm_listener drm_listener = {
1018 drm_handle_device,
1019 drm_handle_authenticated
1020};
1021
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001022static void
1023display_handle_acknowledge(void *data,
1024 struct wl_compositor *compositor,
1025 uint32_t key, uint32_t frame)
1026{
1027 struct display *d = data;
1028 struct window *window;
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001029
1030 /* The acknowledge event means that the server processed our
1031 * last commit request and we can now safely free the old
1032 * window buffer if we resized and render the next frame into
1033 * our back buffer.. */
1034 wl_list_for_each(window, &d->window_list, link) {
Kristian Høgsberg09531622010-06-14 23:22:15 -04001035 cairo_surface_destroy(window->pending_surface);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001036 window->pending_surface = NULL;
Kristian Høgsberg09531622010-06-14 23:22:15 -04001037 if (window->cairo_surface)
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001038 window_attach_surface(window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001039 if (window->acknowledge_handler)
1040 (*window->acknowledge_handler)(window, key, frame, window->user_data);
1041 }
1042}
1043
1044static void
1045display_handle_frame(void *data,
1046 struct wl_compositor *compositor,
1047 uint32_t frame, uint32_t timestamp)
1048{
1049 struct display *d = data;
1050 struct window *window;
1051
1052 wl_list_for_each(window, &d->window_list, link) {
1053 if (window->frame_handler)
1054 (*window->frame_handler)(window, frame,
1055 timestamp, window->user_data);
1056 }
1057}
1058
1059static const struct wl_compositor_listener compositor_listener = {
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001060 display_handle_acknowledge,
1061 display_handle_frame,
1062};
1063
1064static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001065display_handle_geometry(void *data,
1066 struct wl_output *output,
1067 int32_t width, int32_t height)
1068{
1069 struct display *display = data;
1070
1071 display->screen_allocation.x = 0;
1072 display->screen_allocation.y = 0;
1073 display->screen_allocation.width = width;
1074 display->screen_allocation.height = height;
1075}
1076
1077static const struct wl_output_listener output_listener = {
1078 display_handle_geometry,
1079};
1080
1081static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001082display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001083{
1084 struct input *input;
1085
1086 input = malloc(sizeof *input);
1087 if (input == NULL)
1088 return;
1089
1090 memset(input, 0, sizeof *input);
1091 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001092 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001093 input->pointer_focus = NULL;
1094 input->keyboard_focus = NULL;
1095 wl_list_insert(d->input_list.prev, &input->link);
1096
1097 wl_input_device_add_listener(input->input_device,
1098 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001099 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001100}
1101
1102static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001103drag_handle_device(void *data,
1104 struct wl_drag *drag, struct wl_input_device *device)
1105{
1106 struct input *input;
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001107
1108 input = wl_input_device_get_user_data(device);
1109 input->drag = drag;
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001110 wl_drag_set_user_data(drag, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001111}
1112
1113static void
1114drag_pointer_focus(void *data,
1115 struct wl_drag *drag,
1116 uint32_t time, struct wl_surface *surface,
1117 int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
1118{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001119}
1120
1121static void
1122drag_offer(void *data,
1123 struct wl_drag *drag, const char *type)
1124{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001125}
1126
1127static void
1128drag_motion(void *data,
1129 struct wl_drag *drag,
1130 uint32_t time,
1131 int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
1132{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001133}
1134
1135static void
1136drag_target(void *data,
1137 struct wl_drag *drag, const char *mime_type)
1138{
1139}
1140
1141static void
Kristian Høgsberg4eb53602010-08-27 20:29:56 -04001142drag_drop(void *data, struct wl_drag *drag)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001143{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001144}
1145
1146static void
Kristian Høgsberg4eb53602010-08-27 20:29:56 -04001147drag_finish(void *data, struct wl_drag *drag, int fd)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001148{
1149}
1150
1151static const struct wl_drag_listener drag_listener = {
1152 drag_handle_device,
1153 drag_pointer_focus,
1154 drag_offer,
1155 drag_motion,
1156 drag_target,
Kristian Høgsberg4eb53602010-08-27 20:29:56 -04001157 drag_drop,
1158 drag_finish
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001159};
1160
1161static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001162display_handle_global(struct wl_display *display, uint32_t id,
1163 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001164{
1165 struct display *d = data;
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001166 struct wl_drag *drag;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001167
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001168 if (strcmp(interface, "compositor") == 0) {
1169 d->compositor = wl_compositor_create(display, id);
1170 wl_compositor_add_listener(d->compositor,
1171 &compositor_listener, d);
1172 } else if (strcmp(interface, "output") == 0) {
1173 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001174 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001175 } else if (strcmp(interface, "input_device") == 0) {
1176 display_add_input(d, id);
1177 } else if (strcmp(interface, "shell") == 0) {
1178 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001179 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001180 } else if (strcmp(interface, "drm") == 0) {
1181 d->drm = wl_drm_create(display, id);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001182 wl_drm_add_listener(d->drm, &drm_listener, d);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001183 } else if (strcmp(interface, "drag") == 0) {
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001184 drag = wl_drag_create(display, id);
1185 wl_drag_add_listener(drag, &drag_listener, NULL);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001186 }
1187}
1188
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001189static const char socket_name[] = "\0wayland";
1190
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001191static void
1192display_render_frame(struct display *d)
1193{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001194 int radius = 8;
1195 cairo_t *cr;
1196
1197 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1198 cr = cairo_create(d->shadow);
1199 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1200 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1201 rounded_rect(cr, 16, 16, 112, 112, radius);
1202 cairo_fill(cr);
1203 cairo_destroy(cr);
1204 blur_surface(d->shadow, 64);
1205
1206 d->active_frame =
1207 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1208 cr = cairo_create(d->active_frame);
1209 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1210 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1211 rounded_rect(cr, 16, 16, 112, 112, radius);
1212 cairo_fill(cr);
1213 cairo_destroy(cr);
1214
1215 d->inactive_frame =
1216 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1217 cr = cairo_create(d->inactive_frame);
1218 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1219 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1220 rounded_rect(cr, 16, 16, 112, 112, radius);
1221 cairo_fill(cr);
1222 cairo_destroy(cr);
1223}
1224
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001225static void
1226init_xkb(struct display *d)
1227{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001228 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001229
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001230 names.rules = "evdev";
1231 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001232 names.layout = option_xkb_layout;
1233 names.variant = option_xkb_variant;
1234 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001235
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001236 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001237 if (!d->xkb) {
1238 fprintf(stderr, "Failed to compile keymap\n");
1239 exit(1);
1240 }
1241}
1242
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001243struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001244display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001245{
1246 struct display *d;
Kristian Høgsberg379b6782010-07-28 22:52:28 -04001247 EGLint major, minor;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001248 int fd;
1249 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001250 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001251 GError *error;
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001252 drm_magic_t magic;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001253
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001254 g_type_init();
1255
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001256 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001257 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001258 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001259
1260 xkb_option_group = g_option_group_new("xkb",
1261 "Keyboard options",
1262 "Show all XKB options",
1263 NULL, NULL);
1264 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1265 g_option_context_add_group (context, xkb_option_group);
1266
1267 if (!g_option_context_parse(context, argc, argv, &error)) {
1268 fprintf(stderr, "option parsing failed: %s\n", error->message);
1269 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001270 }
1271
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001272
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001273 d = malloc(sizeof *d);
1274 if (d == NULL)
1275 return NULL;
1276
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001277 d->display = wl_display_create(socket_name, sizeof socket_name);
1278 if (d->display == NULL) {
1279 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001280 return NULL;
1281 }
1282
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001283 wl_list_init(&d->input_list);
1284
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001285 /* Set up listener so we'll catch all events. */
1286 wl_display_add_global_listener(d->display,
1287 display_handle_global, d);
1288
1289 /* Process connection events. */
1290 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1291
1292 fd = open(d->device_name, O_RDWR);
1293 if (fd < 0) {
1294 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001295 return NULL;
1296 }
1297
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001298 if (drmGetMagic(fd, &magic)) {
1299 fprintf(stderr, "DRI2: failed to get drm magic");
1300 return NULL;
1301 }
1302
1303 /* Wait for authenticated event */
1304 wl_drm_authenticate(d->drm, magic);
1305 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1306 while (!d->authenticated)
1307 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1308
Kristian Høgsbergf252d6a2010-07-08 20:15:10 -04001309 d->dpy = eglGetDRMDisplayMESA(fd);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001310 if (!eglInitialize(d->dpy, &major, &minor)) {
1311 fprintf(stderr, "failed to initialize display\n");
1312 return NULL;
1313 }
1314
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001315 eglBindAPI(EGL_OPENGL_API);
1316
Kristian Høgsberg379b6782010-07-28 22:52:28 -04001317 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001318 if (d->ctx == NULL) {
1319 fprintf(stderr, "failed to create context\n");
1320 return NULL;
1321 }
1322
1323 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1324 fprintf(stderr, "faile to make context current\n");
1325 return NULL;
1326 }
1327
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001328 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsberg26449102009-05-28 20:23:31 -04001329 if (d->device == NULL) {
1330 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001331 return NULL;
1332 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001333
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001334 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001335
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001336 display_render_frame(d);
1337
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001338 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001339 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001340 g_source_attach(d->source, NULL);
1341
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001342 wl_list_init(&d->window_list);
1343
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001344 init_xkb(d);
1345
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001346 return d;
1347}
1348
1349struct wl_compositor *
1350display_get_compositor(struct display *display)
1351{
1352 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001353}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001354
1355EGLDisplay
1356display_get_egl_display(struct display *d)
1357{
1358 return d->dpy;
1359}
1360
1361void
1362display_run(struct display *d)
1363{
1364 g_main_loop_run(d->loop);
1365}