blob: c6c36e98a92acbf61123915599c3614224a37321 [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øgsberge4feb562008-11-08 18:53:37 -0500118static void
119rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
120{
121 cairo_move_to(cr, x0, y0 + radius);
122 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
123 cairo_line_to(cr, x1 - radius, y0);
124 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
125 cairo_line_to(cr, x1, y1 - radius);
126 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
127 cairo_line_to(cr, x0 + radius, y1);
128 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
129 cairo_close_path(cr);
130}
131
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400132static int
133texture_from_png(const char *filename, int width, int height)
134{
135 GdkPixbuf *pixbuf;
136 GError *error = NULL;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400137 int stride, i;
138 unsigned char *pixels, *p, *end;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400139
140 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
141 width, height,
142 FALSE, &error);
143 if (error != NULL)
144 return -1;
145
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400146 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
147 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
148 gdk_pixbuf_unref(pixbuf);
149 return -1;
150 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400151
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400152
153 stride = gdk_pixbuf_get_rowstride(pixbuf);
154 pixels = gdk_pixbuf_get_pixels(pixbuf);
155
156 for (i = 0; i < height; i++) {
157 p = pixels + i * stride;
158 end = p + width * 4;
159 while (p < end) {
160 unsigned int t;
161
162#define MULT(d,c,a,t) \
163 do { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } while (0)
164
165 MULT(p[0], p[0], p[3], t);
166 MULT(p[1], p[1], p[3], t);
167 MULT(p[2], p[2], p[3], t);
168 p += 4;
169
170 }
171 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400172
173 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400174 width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400175
176 gdk_pixbuf_unref(pixbuf);
177
178 return 0;
179}
180
181static const struct {
182 const char *filename;
183 int hotspot_x, hotspot_y;
184} pointer_images[] = {
185 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
186 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
187 { DATADIR "/wayland/bottom_side.png", 16, 20 },
188 { DATADIR "/wayland/grabbing.png", 20, 17 },
189 { DATADIR "/wayland/left_ptr.png", 10, 5 },
190 { DATADIR "/wayland/left_side.png", 10, 20 },
191 { DATADIR "/wayland/right_side.png", 30, 19 },
192 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
193 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
194 { DATADIR "/wayland/top_side.png", 18, 8 },
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400195 { DATADIR "/wayland/xterm.png", 15, 15 },
196 { DATADIR "/wayland/hand1.png", 18, 11 }
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400197};
198
199static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400200create_pointer_surfaces(struct display *display)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400201{
202 int i, count;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400203 const int width = 32, height = 32;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400204 struct rectangle rect;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400205
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400206 count = ARRAY_LENGTH(pointer_images);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400207 display->pointer_surfaces =
208 malloc(count * sizeof *display->pointer_surfaces);
209 rect.width = width;
210 rect.height = height;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400211 for (i = 0; i < count; i++) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400212 display->pointer_surfaces[i] =
213 display_create_surface(display, &rect);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400214 texture_from_png(pointer_images[i].filename, width, height);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400215 }
216
217}
218
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400219static const cairo_user_data_key_t surface_data_key;
220struct surface_data {
221 EGLImageKHR image;
222 GLuint texture;
223 EGLDisplay dpy;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400224 struct wl_buffer *buffer;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400225};
226
227static void
228surface_data_destroy(void *p)
229{
230 struct surface_data *data = p;
231
232 glDeleteTextures(1, &data->texture);
233 eglDestroyImageKHR(data->dpy, data->image);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400234 if (data->buffer)
235 wl_buffer_destroy(data->buffer);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400236}
237
238cairo_surface_t *
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400239display_create_surface(struct display *display,
240 struct rectangle *rectangle)
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400241{
242 struct surface_data *data;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400243 EGLDisplay dpy = display->dpy;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400244 cairo_surface_t *surface;
245
246 EGLint image_attribs[] = {
247 EGL_WIDTH, 0,
248 EGL_HEIGHT, 0,
Kristian Høgsbergb12fcce2010-08-24 17:34:23 -0400249 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
250 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400251 EGL_NONE
252 };
253
254 data = malloc(sizeof *data);
255 image_attribs[1] = rectangle->width;
256 image_attribs[3] = rectangle->height;
257 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
258 glGenTextures(1, &data->texture);
259 data->dpy = dpy;
260 glBindTexture(GL_TEXTURE_2D, data->texture);
261 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400262 data->buffer = NULL;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400263
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400264 surface = cairo_gl_surface_create_for_texture(display->device,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400265 CAIRO_CONTENT_COLOR_ALPHA,
266 data->texture,
267 rectangle->width,
268 rectangle->height);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400269
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400270 cairo_surface_set_user_data (surface, &surface_data_key,
271 data, surface_data_destroy);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400272
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400273 return surface;
274}
275
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400276struct wl_buffer *
277display_get_buffer_for_surface(struct display *display,
278 cairo_surface_t *surface)
279{
280 struct surface_data *data;
281 struct wl_visual *visual;
282 struct wl_buffer *buffer;
283 EGLint name, stride;
284 int width, height;
285
286 data = cairo_surface_get_user_data (surface, &surface_data_key);
287 if (data->buffer)
288 return data->buffer;
289
290 width = cairo_gl_surface_get_width (surface);
291 height = cairo_gl_surface_get_height (surface);
292
293 eglExportDRMImageMESA(display->dpy, data->image, &name, NULL, &stride);
294
295 visual = wl_display_get_premultiplied_argb_visual(display->display);
296 buffer = wl_drm_create_buffer(display->drm,
297 name, width, height, stride, visual);
298 data->buffer = buffer;
299
300 return buffer;
301}
302
303cairo_surface_t *
304display_get_pointer_surface(struct display *display, int pointer,
305 int *width, int *height,
306 int *hotspot_x, int *hotspot_y)
307{
308 cairo_surface_t *surface;
309
310 surface = display->pointer_surfaces[pointer];
311 *width = cairo_gl_surface_get_width(surface);
312 *height = cairo_gl_surface_get_height(surface);
313 *hotspot_x = pointer_images[pointer].hotspot_x;
314 *hotspot_y = pointer_images[pointer].hotspot_y;
315
316 return cairo_surface_reference(surface);
317}
318
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500319static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500320window_attach_surface(struct window *window)
321{
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400322 struct display *display = window->display;
323 struct wl_buffer *buffer;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500324
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500325 if (window->pending_surface != NULL)
326 return;
327
Kristian Høgsberg09531622010-06-14 23:22:15 -0400328 window->pending_surface = window->cairo_surface;
329 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500330
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400331 buffer = display_get_buffer_for_surface(display,
332 window->pending_surface);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400333 wl_surface_attach(window->surface, buffer);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500334
335 wl_surface_map(window->surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400336 window->allocation.x,
337 window->allocation.y,
338 window->allocation.width,
339 window->allocation.height);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400340
341 wl_compositor_commit(window->display->compositor, 0);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500342}
343
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500344void
345window_commit(struct window *window, uint32_t key)
346{
Kristian Høgsberg09531622010-06-14 23:22:15 -0400347 if (window->cairo_surface) {
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500348 window_attach_surface(window);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400349 } else {
350 wl_compositor_commit(window->display->compositor, key);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500351 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500352}
353
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500354static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500355window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500356{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500357 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500358 cairo_text_extents_t extents;
Kristian Høgsberg2d6b7c12010-06-25 16:51:57 -0400359 cairo_pattern_t *outline, *bright, *dim;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400360 cairo_surface_t *frame;
361 int width, height, shadow_dx = 3, shadow_dy = 3;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500362
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500363 window->cairo_surface =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400364 display_create_surface(window->display, &window->allocation);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400365 width = window->allocation.width;
366 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500367
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500368 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500369 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500370 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
371
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500372 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500373
Kristian Høgsberg09531622010-06-14 23:22:15 -0400374 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400375 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400376 cairo_paint(cr);
377
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400378 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400379 tile_mask(cr, window->display->shadow,
380 shadow_dx, shadow_dy, width, height,
381 window->margin + 10 - shadow_dx,
382 window->margin + 10 - shadow_dy);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500383
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400384 if (window->keyboard_device)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400385 frame = window->display->active_frame;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400386 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400387 frame = window->display->inactive_frame;
388
389 tile_source(cr, frame, 0, 0, width, height,
390 window->margin + 10, window->margin + 50);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500391
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500392 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
393 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500394 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400395 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500396 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
397 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
398 cairo_set_line_width (cr, 4);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400399 if (window->keyboard_device)
400 cairo_set_source_rgb(cr, 0, 0, 0);
401 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400402 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400403 cairo_show_text(cr, window->title);
404
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500405 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400406
407 cairo_device_flush (window->display->device);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500408}
409
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400410void
411display_flush_cairo_device(struct display *display)
412{
413 cairo_device_flush (display->device);
414}
415
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500416static void
417window_draw_fullscreen(struct window *window)
418{
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500419 window->cairo_surface =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400420 display_create_surface(window->display, &window->allocation);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500421}
422
423void
424window_draw(struct window *window)
425{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500426 if (window->cairo_surface != NULL)
427 cairo_surface_destroy(window->cairo_surface);
428
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400429 if (window->fullscreen || !window->decoration)
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500430 window_draw_fullscreen(window);
431 else
432 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500433}
434
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400435cairo_surface_t *
436window_get_surface(struct window *window)
437{
438 return window->cairo_surface;
439}
440
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400441enum window_location {
442 WINDOW_INTERIOR = 0,
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400443 WINDOW_RESIZING_TOP = 1,
444 WINDOW_RESIZING_BOTTOM = 2,
445 WINDOW_RESIZING_LEFT = 4,
446 WINDOW_RESIZING_TOP_LEFT = 5,
447 WINDOW_RESIZING_BOTTOM_LEFT = 6,
448 WINDOW_RESIZING_RIGHT = 8,
449 WINDOW_RESIZING_TOP_RIGHT = 9,
450 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
451 WINDOW_RESIZING_MASK = 15,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400452 WINDOW_EXTERIOR = 16,
453 WINDOW_TITLEBAR = 17,
454 WINDOW_CLIENT_AREA = 18,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400455};
456
457static int
458get_pointer_location(struct window *window, int32_t x, int32_t y)
459{
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400460 int vlocation, hlocation, location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400461 const int grip_size = 8;
462
463 if (x < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400464 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400465 else if (window->margin <= x && x < window->margin + grip_size)
466 hlocation = WINDOW_RESIZING_LEFT;
467 else if (x < window->allocation.width - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400468 hlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400469 else if (x < window->allocation.width - window->margin)
470 hlocation = WINDOW_RESIZING_RIGHT;
471 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400472 hlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400473
474 if (y < window->margin)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400475 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400476 else if (window->margin <= y && y < window->margin + grip_size)
477 vlocation = WINDOW_RESIZING_TOP;
478 else if (y < window->allocation.height - window->margin - grip_size)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400479 vlocation = WINDOW_INTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400480 else if (y < window->allocation.height - window->margin)
481 vlocation = WINDOW_RESIZING_BOTTOM;
482 else
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400483 vlocation = WINDOW_EXTERIOR;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400484
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400485 location = vlocation | hlocation;
486 if (location & WINDOW_EXTERIOR)
487 location = WINDOW_EXTERIOR;
488 if (location == WINDOW_INTERIOR && y < window->margin + 50)
489 location = WINDOW_TITLEBAR;
490 else if (location == WINDOW_INTERIOR)
491 location = WINDOW_CLIENT_AREA;
492
493 return location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400494}
495
496static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400497set_pointer_image(struct input *input, int pointer)
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400498{
499 struct display *display = input->display;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400500 struct wl_buffer *buffer;
501 cairo_surface_t *surface;
502 int location;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400503
504 location = get_pointer_location(input->pointer_focus,
505 input->sx, input->sy);
506 switch (location) {
507 case WINDOW_RESIZING_TOP:
508 pointer = POINTER_TOP;
509 break;
510 case WINDOW_RESIZING_BOTTOM:
511 pointer = POINTER_BOTTOM;
512 break;
513 case WINDOW_RESIZING_LEFT:
514 pointer = POINTER_LEFT;
515 break;
516 case WINDOW_RESIZING_RIGHT:
517 pointer = POINTER_RIGHT;
518 break;
519 case WINDOW_RESIZING_TOP_LEFT:
520 pointer = POINTER_TOP_LEFT;
521 break;
522 case WINDOW_RESIZING_TOP_RIGHT:
523 pointer = POINTER_TOP_RIGHT;
524 break;
525 case WINDOW_RESIZING_BOTTOM_LEFT:
526 pointer = POINTER_BOTTOM_LEFT;
527 break;
528 case WINDOW_RESIZING_BOTTOM_RIGHT:
529 pointer = POINTER_BOTTOM_RIGHT;
530 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400531 case WINDOW_EXTERIOR:
532 case WINDOW_TITLEBAR:
533 wl_input_device_attach(input->input_device, NULL, 0, 0);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400534 return;
535 default:
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400536 break;
537 }
538
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400539 surface = display->pointer_surfaces[pointer];
540 buffer = display_get_buffer_for_surface(display, surface);
541 wl_input_device_attach(input->input_device, buffer,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400542 pointer_images[pointer].hotspot_x,
543 pointer_images[pointer].hotspot_y);
544}
545
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500546static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500547window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400548 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500549 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500550{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400551 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400552 struct window *window = input->pointer_focus;
553 int location, pointer = POINTER_LEFT_PTR;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400554
555 input->x = x;
556 input->y = y;
557 input->sx = sx;
558 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400559
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400560 location = get_pointer_location(window, input->sx, input->sy);
561
562 if (window->motion_handler)
563 pointer = (*window->motion_handler)(window, input, time,
564 x, y, sx, sy,
565 window->user_data);
566
567 set_pointer_image(input, pointer);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500568}
569
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400570static void
571window_handle_button(void *data,
572 struct wl_input_device *input_device,
573 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500574{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400575 struct input *input = data;
576 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400577 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400578
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400579 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500580
581 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400582 switch (location) {
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400583 case WINDOW_TITLEBAR:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400584 wl_shell_move(window->display->shell,
585 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500586 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400587 case WINDOW_RESIZING_TOP:
588 case WINDOW_RESIZING_BOTTOM:
589 case WINDOW_RESIZING_LEFT:
590 case WINDOW_RESIZING_RIGHT:
591 case WINDOW_RESIZING_TOP_LEFT:
592 case WINDOW_RESIZING_TOP_RIGHT:
593 case WINDOW_RESIZING_BOTTOM_LEFT:
594 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400595 wl_shell_resize(window->display->shell,
596 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400597 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500598 break;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400599 case WINDOW_CLIENT_AREA:
600 if (window->button_handler)
601 (*window->button_handler)(window,
602 input, time,
603 button, state,
604 window->user_data);
605 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500606 }
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400607 } else {
608 if (window->button_handler)
609 (*window->button_handler)(window,
610 input, time,
611 button, state,
612 window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500613 }
614}
615
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500616static void
617window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400618 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500619{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400620 struct input *input = data;
621 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400622 struct display *d = window->display;
623 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500624
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400625 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400626 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500627 return;
628
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400629 level = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400630 if (input->modifiers & WINDOW_MODIFIER_SHIFT &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400631 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
632 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500633
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400634 sym = XkbKeySymEntry(d->xkb, code, level, 0);
635
636 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400637 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400638 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400639 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500640
641 if (window->key_handler)
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400642 (*window->key_handler)(window, key, sym, state,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400643 input->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500644}
645
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500646static void
647window_handle_pointer_focus(void *data,
648 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400649 uint32_t time, struct wl_surface *surface,
650 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500651{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400652 struct input *input = data;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400653 struct window *window;
654 int pointer;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400655
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400656 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400657 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400658 window = input->pointer_focus;
659
660 pointer = POINTER_LEFT_PTR;
661 if (window->motion_handler)
662 pointer = (*window->motion_handler)(window,
663 input, time,
664 x, y, sx, sy,
665 window->user_data);
666
667 set_pointer_image(input, pointer);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400668 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400669 input->pointer_focus = NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400670 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500671}
672
673static void
674window_handle_keyboard_focus(void *data,
675 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400676 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500677 struct wl_surface *surface,
678 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500679{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400680 struct input *input = data;
681 struct window *window = input->keyboard_focus;
682 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500683 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500684
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400685 window = input->keyboard_focus;
686 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500687 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400688 if (window->keyboard_focus_handler)
689 (*window->keyboard_focus_handler)(window, NULL,
690 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500691 }
692
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400693 if (surface)
694 input->keyboard_focus = wl_surface_get_user_data(surface);
695 else
696 input->keyboard_focus = NULL;
697
698 end = keys->data + keys->size;
699 for (k = keys->data; k < end; k++)
700 input->modifiers |= d->xkb->map->modmap[*k];
701
702 window = input->keyboard_focus;
703 if (window) {
704 window->keyboard_device = input;
705 if (window->keyboard_focus_handler)
706 (*window->keyboard_focus_handler)(window,
707 window->keyboard_device,
708 window->user_data);
709 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500710}
711
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500712static const struct wl_input_device_listener input_device_listener = {
713 window_handle_motion,
714 window_handle_button,
715 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500716 window_handle_pointer_focus,
717 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500718};
719
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400720void
721input_get_position(struct input *input, int32_t *x, int32_t *y)
722{
723 *x = input->sx;
724 *y = input->sy;
725}
726
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400727struct wl_input_device *
728input_get_input_device(struct input *input)
729{
730 return input->input_device;
731}
732
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400733void
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400734display_add_drag_listener(struct display *display,
735 const struct wl_drag_listener *drag_listener,
736 void *data)
737{
738 struct input *input;
739
740 wl_list_for_each(input, &display->input_list, link)
741 wl_drag_add_listener(input->drag, drag_listener, data);
742}
743
744void
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400745window_start_drag(struct window *window, struct input *input, uint32_t time)
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400746{
747 cairo_device_flush (window->display->device);
748
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400749 wl_drag_prepare(input->drag, window->surface, time);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400750 wl_drag_offer(input->drag, "text/plain");
751 wl_drag_activate(input->drag);
752}
753
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400754static void
755handle_configure(void *data, struct wl_shell *shell,
756 uint32_t time, uint32_t edges,
757 struct wl_surface *surface,
758 int32_t x, int32_t y, int32_t width, int32_t height)
759{
760 struct window *window = wl_surface_get_user_data(surface);
761
762 window->resize_edges = edges;
763 window->pending_allocation.x = x;
764 window->pending_allocation.y = y;
765 window->pending_allocation.width = width;
766 window->pending_allocation.height = height;
767
768 if (!(edges & 15))
769 return;
770
771 if (window->resize_handler)
772 (*window->resize_handler)(window,
773 window->user_data);
774 else if (window->redraw_handler)
775 window_schedule_redraw(window);
776}
777
778static const struct wl_shell_listener shell_listener = {
779 handle_configure,
780};
781
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500782void
783window_get_child_rectangle(struct window *window,
784 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500785{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400786 if (window->fullscreen && !window->decoration) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500787 *rectangle = window->allocation;
788 } else {
789 rectangle->x = window->margin + 10;
790 rectangle->y = window->margin + 50;
791 rectangle->width = window->allocation.width - 20 - window->margin * 2;
792 rectangle->height = window->allocation.height - 60 - window->margin * 2;
793 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500794}
795
796void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500797window_set_child_size(struct window *window,
798 struct rectangle *rectangle)
799{
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400800 int32_t width, height;
801
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500802 if (!window->fullscreen) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400803 width = rectangle->width + 20 + window->margin * 2;
804 height = rectangle->height + 60 + window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400805
806 if (window->resize_edges & WINDOW_RESIZING_LEFT)
807 window->allocation.x +=
808 window->allocation.width - width;
809 if (window->resize_edges & WINDOW_RESIZING_TOP)
810 window->allocation.y +=
811 window->allocation.height - height;
812
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400813 window->allocation.width = width;
814 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500815 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500816}
817
818void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400819window_copy_image(struct window *window,
820 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500821{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400822 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500823}
824
825void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500826window_copy_surface(struct window *window,
827 struct rectangle *rectangle,
828 cairo_surface_t *surface)
829{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500830 cairo_t *cr;
831
832 cr = cairo_create (window->cairo_surface);
833
834 cairo_set_source_surface (cr,
835 surface,
836 rectangle->x, rectangle->y);
837
838 cairo_paint (cr);
839 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500840}
841
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400842static gboolean
843idle_redraw(void *data)
844{
845 struct window *window = data;
846
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400847 if (window->resize_edges)
848 window->allocation = window->pending_allocation;
849
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400850 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400851
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400852 window->redraw_scheduled = 0;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400853 window->resize_edges = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400854
855 return FALSE;
856}
857
858void
859window_schedule_redraw(struct window *window)
860{
861 if (!window->redraw_scheduled) {
862 g_idle_add(idle_redraw, window);
863 window->redraw_scheduled = 1;
864 }
865}
866
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500867void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500868window_set_fullscreen(struct window *window, int fullscreen)
869{
870 window->fullscreen = fullscreen;
871 if (window->fullscreen) {
872 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500873 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500874 } else {
875 window->allocation = window->saved_allocation;
876 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500877}
878
879void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400880window_set_decoration(struct window *window, int decoration)
881{
882 window->decoration = decoration;
883}
884
885void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400886window_set_user_data(struct window *window, void *data)
887{
888 window->user_data = data;
889}
890
891void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500892window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400893 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500894{
895 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500896}
897
898void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400899window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400900 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400901{
902 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400903}
904
905void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500906window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400907 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500908{
909 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500910}
911
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500912void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400913window_set_button_handler(struct window *window,
914 window_button_handler_t handler)
915{
916 window->button_handler = handler;
917}
918
919void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400920window_set_acknowledge_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400921 window_acknowledge_handler_t handler)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400922{
923 window->acknowledge_handler = handler;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400924}
925
926void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400927window_set_frame_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400928 window_frame_handler_t handler)
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400929{
930 window->frame_handler = handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400931}
932
933void
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400934window_set_motion_handler(struct window *window,
935 window_motion_handler_t handler)
936{
937 window->motion_handler = handler;
938}
939
940void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500941window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400942 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500943{
944 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500945}
946
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400947void
948window_move(struct window *window, int32_t x, int32_t y)
949{
950 window->allocation.x = x;
951 window->allocation.y = y;
952
953 wl_surface_map(window->surface,
954 window->allocation.x - window->margin,
955 window->allocation.y - window->margin,
956 window->allocation.width,
957 window->allocation.height);
958}
959
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500960struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500961window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500962 int32_t x, int32_t y, int32_t width, int32_t height)
963{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500964 struct window *window;
965
966 window = malloc(sizeof *window);
967 if (window == NULL)
968 return NULL;
969
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500970 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500971 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500972 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500973 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500974 window->allocation.x = x;
975 window->allocation.y = y;
976 window->allocation.width = width;
977 window->allocation.height = height;
978 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500979 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400980 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500981
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400982 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400983 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500984
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500985 return window;
986}
987
988static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400989drm_handle_device(void *data, struct wl_drm *drm, const char *device)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400990{
991 struct display *d = data;
992
993 d->device_name = strdup(device);
994}
995
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400996static void drm_handle_authenticated(void *data, struct wl_drm *drm)
997{
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400998 struct display *d = data;
999
1000 d->authenticated = 1;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001001}
1002
1003static const struct wl_drm_listener drm_listener = {
1004 drm_handle_device,
1005 drm_handle_authenticated
1006};
1007
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001008static void
1009display_handle_acknowledge(void *data,
1010 struct wl_compositor *compositor,
1011 uint32_t key, uint32_t frame)
1012{
1013 struct display *d = data;
1014 struct window *window;
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001015
1016 /* The acknowledge event means that the server processed our
1017 * last commit request and we can now safely free the old
1018 * window buffer if we resized and render the next frame into
1019 * our back buffer.. */
1020 wl_list_for_each(window, &d->window_list, link) {
Kristian Høgsberg09531622010-06-14 23:22:15 -04001021 cairo_surface_destroy(window->pending_surface);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001022 window->pending_surface = NULL;
Kristian Høgsberg09531622010-06-14 23:22:15 -04001023 if (window->cairo_surface)
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001024 window_attach_surface(window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001025 if (window->acknowledge_handler)
1026 (*window->acknowledge_handler)(window, key, frame, window->user_data);
1027 }
1028}
1029
1030static void
1031display_handle_frame(void *data,
1032 struct wl_compositor *compositor,
1033 uint32_t frame, uint32_t timestamp)
1034{
1035 struct display *d = data;
1036 struct window *window;
1037
1038 wl_list_for_each(window, &d->window_list, link) {
1039 if (window->frame_handler)
1040 (*window->frame_handler)(window, frame,
1041 timestamp, window->user_data);
1042 }
1043}
1044
1045static const struct wl_compositor_listener compositor_listener = {
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001046 display_handle_acknowledge,
1047 display_handle_frame,
1048};
1049
1050static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001051display_handle_geometry(void *data,
1052 struct wl_output *output,
1053 int32_t width, int32_t height)
1054{
1055 struct display *display = data;
1056
1057 display->screen_allocation.x = 0;
1058 display->screen_allocation.y = 0;
1059 display->screen_allocation.width = width;
1060 display->screen_allocation.height = height;
1061}
1062
1063static const struct wl_output_listener output_listener = {
1064 display_handle_geometry,
1065};
1066
1067static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001068display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001069{
1070 struct input *input;
1071
1072 input = malloc(sizeof *input);
1073 if (input == NULL)
1074 return;
1075
1076 memset(input, 0, sizeof *input);
1077 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001078 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001079 input->pointer_focus = NULL;
1080 input->keyboard_focus = NULL;
1081 wl_list_insert(d->input_list.prev, &input->link);
1082
1083 wl_input_device_add_listener(input->input_device,
1084 &input_device_listener, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001085 wl_input_device_set_user_data(input->input_device, input);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001086}
1087
1088static void
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001089drag_handle_device(void *data,
1090 struct wl_drag *drag, struct wl_input_device *device)
1091{
1092 struct input *input;
1093 fprintf(stderr, "device for drag %p: %p\n", drag, device);
1094
1095 input = wl_input_device_get_user_data(device);
1096 input->drag = drag;
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001097 wl_drag_set_user_data(drag, input);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001098}
1099
1100static void
1101drag_pointer_focus(void *data,
1102 struct wl_drag *drag,
1103 uint32_t time, struct wl_surface *surface,
1104 int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
1105{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001106}
1107
1108static void
1109drag_offer(void *data,
1110 struct wl_drag *drag, const char *type)
1111{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001112}
1113
1114static void
1115drag_motion(void *data,
1116 struct wl_drag *drag,
1117 uint32_t time,
1118 int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
1119{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001120}
1121
1122static void
1123drag_target(void *data,
1124 struct wl_drag *drag, const char *mime_type)
1125{
1126}
1127
1128static void
Kristian Høgsberg4eb53602010-08-27 20:29:56 -04001129drag_drop(void *data, struct wl_drag *drag)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001130{
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001131}
1132
1133static void
Kristian Høgsberg4eb53602010-08-27 20:29:56 -04001134drag_finish(void *data, struct wl_drag *drag, int fd)
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001135{
1136}
1137
1138static const struct wl_drag_listener drag_listener = {
1139 drag_handle_device,
1140 drag_pointer_focus,
1141 drag_offer,
1142 drag_motion,
1143 drag_target,
Kristian Høgsberg4eb53602010-08-27 20:29:56 -04001144 drag_drop,
1145 drag_finish
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001146};
1147
1148static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001149display_handle_global(struct wl_display *display, uint32_t id,
1150 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001151{
1152 struct display *d = data;
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001153 struct wl_drag *drag;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001154
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001155 if (strcmp(interface, "compositor") == 0) {
1156 d->compositor = wl_compositor_create(display, id);
1157 wl_compositor_add_listener(d->compositor,
1158 &compositor_listener, d);
1159 } else if (strcmp(interface, "output") == 0) {
1160 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001161 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001162 } else if (strcmp(interface, "input_device") == 0) {
1163 display_add_input(d, id);
1164 } else if (strcmp(interface, "shell") == 0) {
1165 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -04001166 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -04001167 } else if (strcmp(interface, "drm") == 0) {
1168 d->drm = wl_drm_create(display, id);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001169 wl_drm_add_listener(d->drm, &drm_listener, d);
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001170 } else if (strcmp(interface, "drag") == 0) {
Kristian Høgsberg506e20e2010-08-19 17:26:02 -04001171 drag = wl_drag_create(display, id);
1172 wl_drag_add_listener(drag, &drag_listener, NULL);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001173 }
1174}
1175
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001176static const char socket_name[] = "\0wayland";
1177
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001178static void
1179display_render_frame(struct display *d)
1180{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001181 int radius = 8;
1182 cairo_t *cr;
1183
1184 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1185 cr = cairo_create(d->shadow);
1186 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1187 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1188 rounded_rect(cr, 16, 16, 112, 112, radius);
1189 cairo_fill(cr);
1190 cairo_destroy(cr);
1191 blur_surface(d->shadow, 64);
1192
1193 d->active_frame =
1194 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1195 cr = cairo_create(d->active_frame);
1196 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1197 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1198 rounded_rect(cr, 16, 16, 112, 112, radius);
1199 cairo_fill(cr);
1200 cairo_destroy(cr);
1201
1202 d->inactive_frame =
1203 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1204 cr = cairo_create(d->inactive_frame);
1205 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1206 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1207 rounded_rect(cr, 16, 16, 112, 112, radius);
1208 cairo_fill(cr);
1209 cairo_destroy(cr);
1210}
1211
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001212static void
1213init_xkb(struct display *d)
1214{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001215 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001216
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001217 names.rules = "evdev";
1218 names.model = "pc105";
1219 names.layout = "us";
1220 names.variant = "";
1221 names.options = "";
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001222
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001223 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001224 if (!d->xkb) {
1225 fprintf(stderr, "Failed to compile keymap\n");
1226 exit(1);
1227 }
1228}
1229
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001230struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001231display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001232{
1233 struct display *d;
Kristian Høgsberg379b6782010-07-28 22:52:28 -04001234 EGLint major, minor;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001235 int fd;
1236 GOptionContext *context;
1237 GError *error;
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001238 drm_magic_t magic;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001239
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001240 g_type_init();
1241
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001242 context = g_option_context_new(NULL);
1243 if (option_entries) {
1244 g_option_context_add_main_entries(context, option_entries, "Wayland View");
1245 if (!g_option_context_parse(context, argc, argv, &error)) {
1246 fprintf(stderr, "option parsing failed: %s\n", error->message);
1247 exit(EXIT_FAILURE);
1248 }
1249 }
1250
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001251 d = malloc(sizeof *d);
1252 if (d == NULL)
1253 return NULL;
1254
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001255 d->display = wl_display_create(socket_name, sizeof socket_name);
1256 if (d->display == NULL) {
1257 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001258 return NULL;
1259 }
1260
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001261 wl_list_init(&d->input_list);
1262
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001263 /* Set up listener so we'll catch all events. */
1264 wl_display_add_global_listener(d->display,
1265 display_handle_global, d);
1266
1267 /* Process connection events. */
1268 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1269
1270 fd = open(d->device_name, O_RDWR);
1271 if (fd < 0) {
1272 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001273 return NULL;
1274 }
1275
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001276 if (drmGetMagic(fd, &magic)) {
1277 fprintf(stderr, "DRI2: failed to get drm magic");
1278 return NULL;
1279 }
1280
1281 /* Wait for authenticated event */
1282 wl_drm_authenticate(d->drm, magic);
1283 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1284 while (!d->authenticated)
1285 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1286
Kristian Høgsbergf252d6a2010-07-08 20:15:10 -04001287 d->dpy = eglGetDRMDisplayMESA(fd);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001288 if (!eglInitialize(d->dpy, &major, &minor)) {
1289 fprintf(stderr, "failed to initialize display\n");
1290 return NULL;
1291 }
1292
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001293 eglBindAPI(EGL_OPENGL_API);
1294
Kristian Høgsberg379b6782010-07-28 22:52:28 -04001295 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001296 if (d->ctx == NULL) {
1297 fprintf(stderr, "failed to create context\n");
1298 return NULL;
1299 }
1300
1301 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1302 fprintf(stderr, "faile to make context current\n");
1303 return NULL;
1304 }
1305
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001306 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsberg26449102009-05-28 20:23:31 -04001307 if (d->device == NULL) {
1308 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001309 return NULL;
1310 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001311
Kristian Høgsberg9a686242010-08-18 15:28:04 -04001312 create_pointer_surfaces(d);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001313
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001314 display_render_frame(d);
1315
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001316 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001317 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001318 g_source_attach(d->source, NULL);
1319
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001320 wl_list_init(&d->window_list);
1321
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001322 init_xkb(d);
1323
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001324 return d;
1325}
1326
1327struct wl_compositor *
1328display_get_compositor(struct display *display)
1329{
1330 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001331}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001332
1333EGLDisplay
1334display_get_egl_display(struct display *d)
1335{
1336 return d->dpy;
1337}
1338
1339void
1340display_run(struct display *d)
1341{
1342 g_main_loop_run(d->loop);
1343}