blob: 5a2f2475ff776f67539dd74637f2378f4b1e5b09 [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øgsberge9d37bd2010-09-02 20:22:42 -040074
75 display_drag_offer_handler_t drag_offer_handler;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050076};
77
78struct window {
79 struct display *display;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050080 struct wl_surface *surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050081 const char *title;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040082 struct rectangle allocation, saved_allocation, pending_allocation;
83 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040084 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050085 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050086 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050087 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040088 int decoration;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040089 struct input *grab_device;
90 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050091 uint32_t name;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050092
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040093 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -050094 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050095
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050096 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040097 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050098 window_key_handler_t key_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -040099 window_button_handler_t button_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500100 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400101 window_acknowledge_handler_t acknowledge_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400102 window_frame_handler_t frame_handler;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400103 window_motion_handler_t motion_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400104
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500105 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400106 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500107};
108
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400109struct input {
110 struct display *display;
111 struct wl_input_device *input_device;
112 struct window *pointer_focus;
113 struct window *keyboard_focus;
114 uint32_t modifiers;
115 int32_t x, y, sx, sy;
116 struct wl_list link;
117};
118
Kristian Høgsbergc7c60642010-08-29 21:33:39 -0400119const char *option_xkb_layout = "us";
120const char *option_xkb_variant = "";
121const char *option_xkb_options = "";
122
123static const GOptionEntry xkb_option_entries[] = {
124 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
125 &option_xkb_layout, "XKB Layout" },
126 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
127 &option_xkb_variant, "XKB Variant" },
128 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
129 &option_xkb_options, "XKB Options" },
130 { NULL }
131};
132
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500133static void
134rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
135{
136 cairo_move_to(cr, x0, y0 + radius);
137 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
138 cairo_line_to(cr, x1 - radius, y0);
139 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
140 cairo_line_to(cr, x1, y1 - radius);
141 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
142 cairo_line_to(cr, x0 + radius, y1);
143 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
144 cairo_close_path(cr);
145}
146
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400147static int
148texture_from_png(const char *filename, int width, int height)
149{
150 GdkPixbuf *pixbuf;
151 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400152 int stride, i;
153 unsigned char *pixels, *p, *end;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400154
155 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
156 width, height,
157 FALSE, &error);
158 if (error != NULL)
159 return -1;
160
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400161 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
162 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
163 gdk_pixbuf_unref(pixbuf);
164 return -1;
165 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400166
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400167
168 stride = gdk_pixbuf_get_rowstride(pixbuf);
169 pixels = gdk_pixbuf_get_pixels(pixbuf);
170
171 for (i = 0; i < height; i++) {
172 p = pixels + i * stride;
173 end = p + width * 4;
174 while (p < end) {
175 unsigned int t;
176
177#define MULT(d,c,a,t) \
178 do { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } while (0)
179
180 MULT(p[0], p[0], p[3], t);
181 MULT(p[1], p[1], p[3], t);
182 MULT(p[2], p[2], p[3], t);
183 p += 4;
184
185 }
186 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400187
188 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400189 width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400190
191 gdk_pixbuf_unref(pixbuf);
192
193 return 0;
194}
195
196static const struct {
197 const char *filename;
198 int hotspot_x, hotspot_y;
199} pointer_images[] = {
200 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
201 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
202 { DATADIR "/wayland/bottom_side.png", 16, 20 },
203 { DATADIR "/wayland/grabbing.png", 20, 17 },
204 { DATADIR "/wayland/left_ptr.png", 10, 5 },
205 { DATADIR "/wayland/left_side.png", 10, 20 },
206 { DATADIR "/wayland/right_side.png", 30, 19 },
207 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
208 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
209 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400210 { DATADIR "/wayland/xterm.png", 15, 15 },
211 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400212};
213
214static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400215create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400216{
217 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400218 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400219 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400220
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400221 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400222 display->pointer_surfaces =
223 malloc(count * sizeof *display->pointer_surfaces);
224 rect.width = width;
225 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400226 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400227 display->pointer_surfaces[i] =
228 display_create_surface(display, &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400229 texture_from_png(pointer_images[i].filename, width, height);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400230 }
231
232}
233
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400234static const cairo_user_data_key_t surface_data_key;
235struct surface_data {
236 EGLImageKHR image;
237 GLuint texture;
238 EGLDisplay dpy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400239 struct wl_buffer *buffer;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400240};
241
242static void
243surface_data_destroy(void *p)
244{
245 struct surface_data *data = p;
246
247 glDeleteTextures(1, &data->texture);
248 eglDestroyImageKHR(data->dpy, data->image);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400249 if (data->buffer)
250 wl_buffer_destroy(data->buffer);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400251}
252
253cairo_surface_t *
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400254display_create_surface(struct display *display,
255 struct rectangle *rectangle)
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400256{
257 struct surface_data *data;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400258 EGLDisplay dpy = display->dpy;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400259 cairo_surface_t *surface;
260
261 EGLint image_attribs[] = {
262 EGL_WIDTH, 0,
263 EGL_HEIGHT, 0,
Kristian Høgsbergb12fcce2010-08-24 17:34:23 -0400264 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
265 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400266 EGL_NONE
267 };
268
269 data = malloc(sizeof *data);
270 image_attribs[1] = rectangle->width;
271 image_attribs[3] = rectangle->height;
272 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
273 glGenTextures(1, &data->texture);
274 data->dpy = dpy;
275 glBindTexture(GL_TEXTURE_2D, data->texture);
276 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400277 data->buffer = NULL;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400278
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400279 surface = cairo_gl_surface_create_for_texture(display->device,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400280 CAIRO_CONTENT_COLOR_ALPHA,
281 data->texture,
282 rectangle->width,
283 rectangle->height);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400284
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400285 cairo_surface_set_user_data (surface, &surface_data_key,
286 data, surface_data_destroy);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400287
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400288 return surface;
289}
290
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400291struct wl_buffer *
292display_get_buffer_for_surface(struct display *display,
293 cairo_surface_t *surface)
294{
295 struct surface_data *data;
296 struct wl_visual *visual;
297 struct wl_buffer *buffer;
298 EGLint name, stride;
299 int width, height;
300
301 data = cairo_surface_get_user_data (surface, &surface_data_key);
302 if (data->buffer)
303 return data->buffer;
304
305 width = cairo_gl_surface_get_width (surface);
306 height = cairo_gl_surface_get_height (surface);
307
308 eglExportDRMImageMESA(display->dpy, data->image, &name, NULL, &stride);
309
310 visual = wl_display_get_premultiplied_argb_visual(display->display);
311 buffer = wl_drm_create_buffer(display->drm,
312 name, width, height, stride, visual);
313 data->buffer = buffer;
314
315 return buffer;
316}
317
318cairo_surface_t *
319display_get_pointer_surface(struct display *display, int pointer,
320 int *width, int *height,
321 int *hotspot_x, int *hotspot_y)
322{
323 cairo_surface_t *surface;
324
325 surface = display->pointer_surfaces[pointer];
326 *width = cairo_gl_surface_get_width(surface);
327 *height = cairo_gl_surface_get_height(surface);
328 *hotspot_x = pointer_images[pointer].hotspot_x;
329 *hotspot_y = pointer_images[pointer].hotspot_y;
330
331 return cairo_surface_reference(surface);
332}
333
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500334static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500335window_attach_surface(struct window *window)
336{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400337 struct display *display = window->display;
338 struct wl_buffer *buffer;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500339
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500340 if (window->pending_surface != NULL)
341 return;
342
Kristian Høgsberg09531622010-06-14 23:22:15 -0400343 window->pending_surface = window->cairo_surface;
344 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500345
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400346 buffer = display_get_buffer_for_surface(display,
347 window->pending_surface);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400348 wl_surface_attach(window->surface, buffer);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500349
350 wl_surface_map(window->surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400351 window->allocation.x,
352 window->allocation.y,
353 window->allocation.width,
354 window->allocation.height);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400355
356 wl_compositor_commit(window->display->compositor, 0);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500357}
358
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500359void
360window_commit(struct window *window, uint32_t key)
361{
Kristian Høgsberg09531622010-06-14 23:22:15 -0400362 if (window->cairo_surface) {
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500363 window_attach_surface(window);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400364 } else {
365 wl_compositor_commit(window->display->compositor, key);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500366 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500367}
368
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500369static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500370window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500371{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500372 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500373 cairo_text_extents_t extents;
Kristian Høgsberg2d6b7c12010-06-25 16:51:57 -0400374 cairo_pattern_t *outline, *bright, *dim;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400375 cairo_surface_t *frame;
376 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500377
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500378 window->cairo_surface =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400379 display_create_surface(window->display, &window->allocation);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400380 width = window->allocation.width;
381 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500382
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500383 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500384 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500385 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
386
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500387 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500388
Kristian Høgsberg09531622010-06-14 23:22:15 -0400389 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400390 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400391 cairo_paint(cr);
392
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400393 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400394 tile_mask(cr, window->display->shadow,
395 shadow_dx, shadow_dy, width, height,
396 window->margin + 10 - shadow_dx,
397 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500398
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400399 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400400 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400401 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400402 frame = window->display->inactive_frame;
403
404 tile_source(cr, frame, 0, 0, width, height,
405 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500406
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500407 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
408 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500409 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400410 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500411 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
412 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
413 cairo_set_line_width (cr, 4);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400414 if (window->keyboard_device)
415 cairo_set_source_rgb(cr, 0, 0, 0);
416 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400417 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400418 cairo_show_text(cr, window->title);
419
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500420 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400421
422 cairo_device_flush (window->display->device);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500423}
424
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400425void
426display_flush_cairo_device(struct display *display)
427{
428 cairo_device_flush (display->device);
429}
430
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500431static void
432window_draw_fullscreen(struct window *window)
433{
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500434 window->cairo_surface =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400435 display_create_surface(window->display, &window->allocation);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500436}
437
438void
439window_draw(struct window *window)
440{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500441 if (window->cairo_surface != NULL)
442 cairo_surface_destroy(window->cairo_surface);
443
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400444 if (window->fullscreen || !window->decoration)
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500445 window_draw_fullscreen(window);
446 else
447 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500448}
449
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400450cairo_surface_t *
451window_get_surface(struct window *window)
452{
453 return window->cairo_surface;
454}
455
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400456enum window_location {
457 WINDOW_INTERIOR = 0,
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400458 WINDOW_RESIZING_TOP = 1,
459 WINDOW_RESIZING_BOTTOM = 2,
460 WINDOW_RESIZING_LEFT = 4,
461 WINDOW_RESIZING_TOP_LEFT = 5,
462 WINDOW_RESIZING_BOTTOM_LEFT = 6,
463 WINDOW_RESIZING_RIGHT = 8,
464 WINDOW_RESIZING_TOP_RIGHT = 9,
465 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
466 WINDOW_RESIZING_MASK = 15,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400467 WINDOW_EXTERIOR = 16,
468 WINDOW_TITLEBAR = 17,
469 WINDOW_CLIENT_AREA = 18,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400470};
471
472static int
473get_pointer_location(struct window *window, int32_t x, int32_t y)
474{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400475 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400476 const int grip_size = 8;
477
478 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400479 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400480 else if (window->margin <= x && x < window->margin + grip_size)
481 hlocation = WINDOW_RESIZING_LEFT;
482 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400483 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400484 else if (x < window->allocation.width - window->margin)
485 hlocation = WINDOW_RESIZING_RIGHT;
486 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400487 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400488
489 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400490 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400491 else if (window->margin <= y && y < window->margin + grip_size)
492 vlocation = WINDOW_RESIZING_TOP;
493 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400494 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400495 else if (y < window->allocation.height - window->margin)
496 vlocation = WINDOW_RESIZING_BOTTOM;
497 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400498 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400499
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400500 location = vlocation | hlocation;
501 if (location & WINDOW_EXTERIOR)
502 location = WINDOW_EXTERIOR;
503 if (location == WINDOW_INTERIOR && y < window->margin + 50)
504 location = WINDOW_TITLEBAR;
505 else if (location == WINDOW_INTERIOR)
506 location = WINDOW_CLIENT_AREA;
507
508 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400509}
510
511static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400512set_pointer_image(struct input *input, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400513{
514 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400515 struct wl_buffer *buffer;
516 cairo_surface_t *surface;
517 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400518
519 location = get_pointer_location(input->pointer_focus,
520 input->sx, input->sy);
521 switch (location) {
522 case WINDOW_RESIZING_TOP:
523 pointer = POINTER_TOP;
524 break;
525 case WINDOW_RESIZING_BOTTOM:
526 pointer = POINTER_BOTTOM;
527 break;
528 case WINDOW_RESIZING_LEFT:
529 pointer = POINTER_LEFT;
530 break;
531 case WINDOW_RESIZING_RIGHT:
532 pointer = POINTER_RIGHT;
533 break;
534 case WINDOW_RESIZING_TOP_LEFT:
535 pointer = POINTER_TOP_LEFT;
536 break;
537 case WINDOW_RESIZING_TOP_RIGHT:
538 pointer = POINTER_TOP_RIGHT;
539 break;
540 case WINDOW_RESIZING_BOTTOM_LEFT:
541 pointer = POINTER_BOTTOM_LEFT;
542 break;
543 case WINDOW_RESIZING_BOTTOM_RIGHT:
544 pointer = POINTER_BOTTOM_RIGHT;
545 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400546 case WINDOW_EXTERIOR:
547 case WINDOW_TITLEBAR:
548 wl_input_device_attach(input->input_device, NULL, 0, 0);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400549 return;
550 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400551 break;
552 }
553
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400554 surface = display->pointer_surfaces[pointer];
555 buffer = display_get_buffer_for_surface(display, surface);
556 wl_input_device_attach(input->input_device, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400557 pointer_images[pointer].hotspot_x,
558 pointer_images[pointer].hotspot_y);
559}
560
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500561static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500562window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400563 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500564 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500565{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400566 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400567 struct window *window = input->pointer_focus;
568 int location, pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400569
570 input->x = x;
571 input->y = y;
572 input->sx = sx;
573 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400574
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400575 location = get_pointer_location(window, input->sx, input->sy);
576
577 if (window->motion_handler)
578 pointer = (*window->motion_handler)(window, input, time,
579 x, y, sx, sy,
580 window->user_data);
581
582 set_pointer_image(input, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500583}
584
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400585static void
586window_handle_button(void *data,
587 struct wl_input_device *input_device,
588 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500589{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400590 struct input *input = data;
591 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400592 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400593
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400594 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500595
596 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400597 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400598 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400599 wl_shell_move(window->display->shell,
600 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500601 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400602 case WINDOW_RESIZING_TOP:
603 case WINDOW_RESIZING_BOTTOM:
604 case WINDOW_RESIZING_LEFT:
605 case WINDOW_RESIZING_RIGHT:
606 case WINDOW_RESIZING_TOP_LEFT:
607 case WINDOW_RESIZING_TOP_RIGHT:
608 case WINDOW_RESIZING_BOTTOM_LEFT:
609 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400610 wl_shell_resize(window->display->shell,
611 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400612 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500613 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400614 case WINDOW_CLIENT_AREA:
615 if (window->button_handler)
616 (*window->button_handler)(window,
617 input, time,
618 button, state,
619 window->user_data);
620 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500621 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400622 } else {
623 if (window->button_handler)
624 (*window->button_handler)(window,
625 input, time,
626 button, state,
627 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500628 }
629}
630
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500631static void
632window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400633 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500634{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400635 struct input *input = data;
636 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400637 struct display *d = window->display;
638 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500639
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400640 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400641 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500642 return;
643
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400644 level = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400645 if (input->modifiers & WINDOW_MODIFIER_SHIFT &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400646 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
647 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500648
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400649 sym = XkbKeySymEntry(d->xkb, code, level, 0);
650
651 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400652 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400653 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400654 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500655
656 if (window->key_handler)
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400657 (*window->key_handler)(window, key, sym, state,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400658 input->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500659}
660
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500661static void
662window_handle_pointer_focus(void *data,
663 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400664 uint32_t time, struct wl_surface *surface,
665 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500666{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400667 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400668 struct window *window;
669 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400670
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400671 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400672 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400673 window = input->pointer_focus;
674
675 pointer = POINTER_LEFT_PTR;
676 if (window->motion_handler)
677 pointer = (*window->motion_handler)(window,
678 input, time,
679 x, y, sx, sy,
680 window->user_data);
681
682 set_pointer_image(input, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400683 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400684 input->pointer_focus = NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400685 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500686}
687
688static void
689window_handle_keyboard_focus(void *data,
690 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400691 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500692 struct wl_surface *surface,
693 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500694{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400695 struct input *input = data;
696 struct window *window = input->keyboard_focus;
697 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500698 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500699
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400700 window = input->keyboard_focus;
701 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500702 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400703 if (window->keyboard_focus_handler)
704 (*window->keyboard_focus_handler)(window, NULL,
705 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500706 }
707
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400708 if (surface)
709 input->keyboard_focus = wl_surface_get_user_data(surface);
710 else
711 input->keyboard_focus = NULL;
712
713 end = keys->data + keys->size;
714 for (k = keys->data; k < end; k++)
715 input->modifiers |= d->xkb->map->modmap[*k];
716
717 window = input->keyboard_focus;
718 if (window) {
719 window->keyboard_device = input;
720 if (window->keyboard_focus_handler)
721 (*window->keyboard_focus_handler)(window,
722 window->keyboard_device,
723 window->user_data);
724 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500725}
726
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500727static const struct wl_input_device_listener input_device_listener = {
728 window_handle_motion,
729 window_handle_button,
730 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500731 window_handle_pointer_focus,
732 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500733};
734
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400735void
736input_get_position(struct input *input, int32_t *x, int32_t *y)
737{
738 *x = input->sx;
739 *y = input->sy;
740}
741
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400742struct wl_input_device *
743input_get_input_device(struct input *input)
744{
745 return input->input_device;
746}
747
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400748struct wl_drag *
749window_start_drag(struct window *window, struct input *input, uint32_t time,
750 const struct wl_drag_listener *listener, void *data)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400751{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400752 struct wl_drag *drag;
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400753
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400754 cairo_device_flush (window->display->device);
755
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400756 drag = wl_shell_create_drag(window->display->shell);
757 wl_drag_offer(drag, "text/plain");
758 wl_drag_offer(drag, "text/html");
759 wl_drag_activate(drag, window->surface, input->input_device, time);
760 wl_drag_add_listener(drag, listener, data);
761
762 return drag;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400763}
764
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400765static void
766handle_configure(void *data, struct wl_shell *shell,
767 uint32_t time, uint32_t edges,
768 struct wl_surface *surface,
769 int32_t x, int32_t y, int32_t width, int32_t height)
770{
771 struct window *window = wl_surface_get_user_data(surface);
772
773 window->resize_edges = edges;
774 window->pending_allocation.x = x;
775 window->pending_allocation.y = y;
776 window->pending_allocation.width = width;
777 window->pending_allocation.height = height;
778
779 if (!(edges & 15))
780 return;
781
782 if (window->resize_handler)
783 (*window->resize_handler)(window,
784 window->user_data);
785 else if (window->redraw_handler)
786 window_schedule_redraw(window);
787}
788
789static const struct wl_shell_listener shell_listener = {
790 handle_configure,
791};
792
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500793void
794window_get_child_rectangle(struct window *window,
795 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500796{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400797 if (window->fullscreen && !window->decoration) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500798 *rectangle = window->allocation;
799 } else {
800 rectangle->x = window->margin + 10;
801 rectangle->y = window->margin + 50;
802 rectangle->width = window->allocation.width - 20 - window->margin * 2;
803 rectangle->height = window->allocation.height - 60 - window->margin * 2;
804 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500805}
806
807void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500808window_set_child_size(struct window *window,
809 struct rectangle *rectangle)
810{
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400811 int32_t width, height;
812
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500813 if (!window->fullscreen) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400814 width = rectangle->width + 20 + window->margin * 2;
815 height = rectangle->height + 60 + window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400816
817 if (window->resize_edges & WINDOW_RESIZING_LEFT)
818 window->allocation.x +=
819 window->allocation.width - width;
820 if (window->resize_edges & WINDOW_RESIZING_TOP)
821 window->allocation.y +=
822 window->allocation.height - height;
823
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400824 window->allocation.width = width;
825 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500826 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500827}
828
829void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400830window_copy_image(struct window *window,
831 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500832{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400833 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500834}
835
836void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500837window_copy_surface(struct window *window,
838 struct rectangle *rectangle,
839 cairo_surface_t *surface)
840{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500841 cairo_t *cr;
842
843 cr = cairo_create (window->cairo_surface);
844
845 cairo_set_source_surface (cr,
846 surface,
847 rectangle->x, rectangle->y);
848
849 cairo_paint (cr);
850 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500851}
852
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400853static gboolean
854idle_redraw(void *data)
855{
856 struct window *window = data;
857
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400858 if (window->resize_edges)
859 window->allocation = window->pending_allocation;
860
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400861 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400862
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400863 window->redraw_scheduled = 0;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400864 window->resize_edges = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400865
866 return FALSE;
867}
868
869void
870window_schedule_redraw(struct window *window)
871{
872 if (!window->redraw_scheduled) {
873 g_idle_add(idle_redraw, window);
874 window->redraw_scheduled = 1;
875 }
876}
877
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500878void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500879window_set_fullscreen(struct window *window, int fullscreen)
880{
881 window->fullscreen = fullscreen;
882 if (window->fullscreen) {
883 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500884 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500885 } else {
886 window->allocation = window->saved_allocation;
887 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500888}
889
890void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400891window_set_decoration(struct window *window, int decoration)
892{
893 window->decoration = decoration;
894}
895
896void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400897window_set_user_data(struct window *window, void *data)
898{
899 window->user_data = data;
900}
901
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400902void *
903window_get_user_data(struct window *window)
904{
905 return window->user_data;
906}
907
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400908void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500909window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400910 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500911{
912 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500913}
914
915void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400916window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400917 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400918{
919 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400920}
921
922void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500923window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400924 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500925{
926 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500927}
928
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500929void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400930window_set_button_handler(struct window *window,
931 window_button_handler_t handler)
932{
933 window->button_handler = handler;
934}
935
936void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400937window_set_acknowledge_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400938 window_acknowledge_handler_t handler)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400939{
940 window->acknowledge_handler = handler;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400941}
942
943void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400944window_set_frame_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400945 window_frame_handler_t handler)
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400946{
947 window->frame_handler = handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400948}
949
950void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400951window_set_motion_handler(struct window *window,
952 window_motion_handler_t handler)
953{
954 window->motion_handler = handler;
955}
956
957void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500958window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400959 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500960{
961 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500962}
963
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400964void
965window_move(struct window *window, int32_t x, int32_t y)
966{
967 window->allocation.x = x;
968 window->allocation.y = y;
969
970 wl_surface_map(window->surface,
971 window->allocation.x - window->margin,
972 window->allocation.y - window->margin,
973 window->allocation.width,
974 window->allocation.height);
975}
976
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500977struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500978window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500979 int32_t x, int32_t y, int32_t width, int32_t height)
980{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500981 struct window *window;
982
983 window = malloc(sizeof *window);
984 if (window == NULL)
985 return NULL;
986
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500987 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500988 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500989 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500990 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500991 window->allocation.x = x;
992 window->allocation.y = y;
993 window->allocation.width = width;
994 window->allocation.height = height;
995 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500996 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400997 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500998
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400999 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001000 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001001
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001002 return window;
1003}
1004
1005static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001006drm_handle_device(void *data, struct wl_drm *drm, const char *device)
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001007{
1008 struct display *d = data;
1009
1010 d->device_name = strdup(device);
1011}
1012
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001013static void drm_handle_authenticated(void *data, struct wl_drm *drm)
1014{
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001015 struct display *d = data;
1016
1017 d->authenticated = 1;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001018}
1019
1020static const struct wl_drm_listener drm_listener = {
1021 drm_handle_device,
1022 drm_handle_authenticated
1023};
1024
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001025static void
1026display_handle_acknowledge(void *data,
1027 struct wl_compositor *compositor,
1028 uint32_t key, uint32_t frame)
1029{
1030 struct display *d = data;
1031 struct window *window;
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001032
1033 /* The acknowledge event means that the server processed our
1034 * last commit request and we can now safely free the old
1035 * window buffer if we resized and render the next frame into
1036 * our back buffer.. */
1037 wl_list_for_each(window, &d->window_list, link) {
Kristian Høgsberg09531622010-06-14 23:22:15 -04001038 cairo_surface_destroy(window->pending_surface);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001039 window->pending_surface = NULL;
Kristian Høgsberg09531622010-06-14 23:22:15 -04001040 if (window->cairo_surface)
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001041 window_attach_surface(window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001042 if (window->acknowledge_handler)
1043 (*window->acknowledge_handler)(window, key, frame, window->user_data);
1044 }
1045}
1046
1047static void
1048display_handle_frame(void *data,
1049 struct wl_compositor *compositor,
1050 uint32_t frame, uint32_t timestamp)
1051{
1052 struct display *d = data;
1053 struct window *window;
1054
1055 wl_list_for_each(window, &d->window_list, link) {
1056 if (window->frame_handler)
1057 (*window->frame_handler)(window, frame,
1058 timestamp, window->user_data);
1059 }
1060}
1061
1062static const struct wl_compositor_listener compositor_listener = {
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001063 display_handle_acknowledge,
1064 display_handle_frame,
1065};
1066
1067static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001068display_handle_geometry(void *data,
1069 struct wl_output *output,
1070 int32_t width, int32_t height)
1071{
1072 struct display *display = data;
1073
1074 display->screen_allocation.x = 0;
1075 display->screen_allocation.y = 0;
1076 display->screen_allocation.width = width;
1077 display->screen_allocation.height = height;
1078}
1079
1080static const struct wl_output_listener output_listener = {
1081 display_handle_geometry,
1082};
1083
1084static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001085display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001086{
1087 struct input *input;
1088
1089 input = malloc(sizeof *input);
1090 if (input == NULL)
1091 return;
1092
1093 memset(input, 0, sizeof *input);
1094 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001095 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001096 input->pointer_focus = NULL;
1097 input->keyboard_focus = NULL;
1098 wl_list_insert(d->input_list.prev, &input->link);
1099
1100 wl_input_device_add_listener(input->input_device,
1101 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001102 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001103}
1104
1105static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001106display_handle_global(struct wl_display *display, uint32_t id,
1107 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001108{
1109 struct display *d = data;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001110 struct wl_drag_offer *offer;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001111
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001112 if (strcmp(interface, "compositor") == 0) {
1113 d->compositor = wl_compositor_create(display, id);
1114 wl_compositor_add_listener(d->compositor,
1115 &compositor_listener, d);
1116 } else if (strcmp(interface, "output") == 0) {
1117 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001118 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001119 } else if (strcmp(interface, "input_device") == 0) {
1120 display_add_input(d, id);
1121 } else if (strcmp(interface, "shell") == 0) {
1122 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001123 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001124 } else if (strcmp(interface, "drm") == 0) {
1125 d->drm = wl_drm_create(display, id);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001126 wl_drm_add_listener(d->drm, &drm_listener, d);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001127 } else if (strcmp(interface, "drag_offer") == 0) {
1128 offer = wl_drag_offer_create(display, id);
1129 d->drag_offer_handler(offer, d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001130 }
1131}
1132
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001133static const char socket_name[] = "\0wayland";
1134
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001135static void
1136display_render_frame(struct display *d)
1137{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001138 int radius = 8;
1139 cairo_t *cr;
1140
1141 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1142 cr = cairo_create(d->shadow);
1143 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1144 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1145 rounded_rect(cr, 16, 16, 112, 112, radius);
1146 cairo_fill(cr);
1147 cairo_destroy(cr);
1148 blur_surface(d->shadow, 64);
1149
1150 d->active_frame =
1151 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1152 cr = cairo_create(d->active_frame);
1153 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1154 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1155 rounded_rect(cr, 16, 16, 112, 112, radius);
1156 cairo_fill(cr);
1157 cairo_destroy(cr);
1158
1159 d->inactive_frame =
1160 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1161 cr = cairo_create(d->inactive_frame);
1162 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1163 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1164 rounded_rect(cr, 16, 16, 112, 112, radius);
1165 cairo_fill(cr);
1166 cairo_destroy(cr);
1167}
1168
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001169static void
1170init_xkb(struct display *d)
1171{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001172 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001173
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001174 names.rules = "evdev";
1175 names.model = "pc105";
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001176 names.layout = option_xkb_layout;
1177 names.variant = option_xkb_variant;
1178 names.options = option_xkb_options;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001179
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001180 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001181 if (!d->xkb) {
1182 fprintf(stderr, "Failed to compile keymap\n");
1183 exit(1);
1184 }
1185}
1186
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001187struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001188display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001189{
1190 struct display *d;
Kristian Høgsberg379b6782010-07-28 22:52:28 -04001191 EGLint major, minor;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001192 int fd;
1193 GOptionContext *context;
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001194 GOptionGroup *xkb_option_group;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001195 GError *error;
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001196 drm_magic_t magic;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001197
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001198 g_type_init();
1199
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001200 context = g_option_context_new(NULL);
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001201 if (option_entries)
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001202 g_option_context_add_main_entries(context, option_entries, "Wayland View");
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001203
1204 xkb_option_group = g_option_group_new("xkb",
1205 "Keyboard options",
1206 "Show all XKB options",
1207 NULL, NULL);
1208 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1209 g_option_context_add_group (context, xkb_option_group);
1210
1211 if (!g_option_context_parse(context, argc, argv, &error)) {
1212 fprintf(stderr, "option parsing failed: %s\n", error->message);
1213 exit(EXIT_FAILURE);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001214 }
1215
Kristian Høgsbergc7c60642010-08-29 21:33:39 -04001216
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001217 d = malloc(sizeof *d);
1218 if (d == NULL)
1219 return NULL;
1220
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001221 d->display = wl_display_create(socket_name, sizeof socket_name);
1222 if (d->display == NULL) {
1223 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001224 return NULL;
1225 }
1226
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001227 wl_list_init(&d->input_list);
1228
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001229 /* Set up listener so we'll catch all events. */
1230 wl_display_add_global_listener(d->display,
1231 display_handle_global, d);
1232
1233 /* Process connection events. */
1234 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1235
1236 fd = open(d->device_name, O_RDWR);
1237 if (fd < 0) {
1238 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001239 return NULL;
1240 }
1241
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001242 if (drmGetMagic(fd, &magic)) {
1243 fprintf(stderr, "DRI2: failed to get drm magic");
1244 return NULL;
1245 }
1246
1247 /* Wait for authenticated event */
1248 wl_drm_authenticate(d->drm, magic);
1249 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1250 while (!d->authenticated)
1251 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1252
Kristian Høgsbergf252d6a2010-07-08 20:15:10 -04001253 d->dpy = eglGetDRMDisplayMESA(fd);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001254 if (!eglInitialize(d->dpy, &major, &minor)) {
1255 fprintf(stderr, "failed to initialize display\n");
1256 return NULL;
1257 }
1258
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001259 eglBindAPI(EGL_OPENGL_API);
1260
Kristian Høgsberg379b6782010-07-28 22:52:28 -04001261 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001262 if (d->ctx == NULL) {
1263 fprintf(stderr, "failed to create context\n");
1264 return NULL;
1265 }
1266
1267 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1268 fprintf(stderr, "faile to make context current\n");
1269 return NULL;
1270 }
1271
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001272 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsberg26449102009-05-28 20:23:31 -04001273 if (d->device == NULL) {
1274 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001275 return NULL;
1276 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001277
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001278 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001279
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001280 display_render_frame(d);
1281
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001282 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001283 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001284 g_source_attach(d->source, NULL);
1285
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001286 wl_list_init(&d->window_list);
1287
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001288 init_xkb(d);
1289
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001290 return d;
1291}
1292
1293struct wl_compositor *
1294display_get_compositor(struct display *display)
1295{
1296 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001297}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001298
1299EGLDisplay
1300display_get_egl_display(struct display *d)
1301{
1302 return d->dpy;
1303}
1304
1305void
1306display_run(struct display *d)
1307{
1308 g_main_loop_run(d->loop);
1309}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -04001310
1311void
1312display_set_drag_offer_handler(struct display *display,
1313 display_drag_offer_handler_t handler)
1314{
1315 display->drag_offer_handler = handler;
1316}