blob: 84586d79b807982095887afc3ba2b1292b3cab7f [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øgsbergda275dd2010-08-16 17:47:07 -040073 EGLImageKHR *pointer_images;
74 struct wl_buffer **pointer_buffers;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050075};
76
77struct window {
78 struct display *display;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050079 struct wl_surface *surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050080 const char *title;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040081 struct rectangle allocation, saved_allocation, pending_allocation;
82 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040083 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050084 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050085 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050086 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040087 int decoration;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040088 struct input *grab_device;
89 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050090 uint32_t name;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050091
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040092 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -050093 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050094
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050095 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040096 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050097 window_key_handler_t key_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;
101
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500102 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400103 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500104};
105
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400106struct input {
107 struct display *display;
108 struct wl_input_device *input_device;
109 struct window *pointer_focus;
110 struct window *keyboard_focus;
111 uint32_t modifiers;
112 int32_t x, y, sx, sy;
113 struct wl_list link;
114};
115
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500116static void
117rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
118{
119 cairo_move_to(cr, x0, y0 + radius);
120 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
121 cairo_line_to(cr, x1 - radius, y0);
122 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
123 cairo_line_to(cr, x1, y1 - radius);
124 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
125 cairo_line_to(cr, x0 + radius, y1);
126 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
127 cairo_close_path(cr);
128}
129
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400130static int
131texture_from_png(const char *filename, int width, int height)
132{
133 GdkPixbuf *pixbuf;
134 GError *error = NULL;
135 void *data;
136 GLenum format;
137
138 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
139 width, height,
140 FALSE, &error);
141 if (error != NULL)
142 return -1;
143
144 data = gdk_pixbuf_get_pixels(pixbuf);
145
146 if (gdk_pixbuf_get_has_alpha(pixbuf))
147 format = GL_RGBA;
148 else
149 format = GL_RGB;
150
151 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
152 width, height, 0, format, GL_UNSIGNED_BYTE, data);
153
154 gdk_pixbuf_unref(pixbuf);
155
156 return 0;
157}
158
159static const struct {
160 const char *filename;
161 int hotspot_x, hotspot_y;
162} pointer_images[] = {
163 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
164 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
165 { DATADIR "/wayland/bottom_side.png", 16, 20 },
166 { DATADIR "/wayland/grabbing.png", 20, 17 },
167 { DATADIR "/wayland/left_ptr.png", 10, 5 },
168 { DATADIR "/wayland/left_side.png", 10, 20 },
169 { DATADIR "/wayland/right_side.png", 30, 19 },
170 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
171 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
172 { DATADIR "/wayland/top_side.png", 18, 8 },
173 { DATADIR "/wayland/xterm.png", 15, 15 }
174};
175
176static void
177create_pointer_images(struct display *d)
178{
179 int i, count;
180 GLuint texture;
181 const int width = 32, height = 32;
182 EGLint name, stride;
183 struct wl_visual *visual;
184
185 EGLint image_attribs[] = {
186 EGL_WIDTH, 0,
187 EGL_HEIGHT, 0,
188 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
189 EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA,
190 EGL_NONE
191 };
192
193 glGenTextures(1, &texture);
194 glBindTexture(GL_TEXTURE_2D, texture);
195 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
196 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
197 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
198 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
199
200 visual = wl_display_get_argb_visual(d->display);
201
202 image_attribs[1] = width;
203 image_attribs[3] = height;
204 count = ARRAY_LENGTH(pointer_images);
205 d->pointer_images = malloc(count * sizeof *d->pointer_images);
206 d->pointer_buffers = malloc(count * sizeof *d->pointer_buffers);
207 for (i = 0; i < count; i++) {
208 d->pointer_images[i] =
209 eglCreateDRMImageMESA(d->dpy, image_attribs);
210 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,
211 d->pointer_images[i]);
212 eglExportDRMImageMESA(d->dpy,
213 d->pointer_images[i],
214 &name, NULL, &stride);
215 texture_from_png(pointer_images[i].filename, width, height);
216
217 d->pointer_buffers[i] =
218 wl_drm_create_buffer(d->drm, name,
219 width, height, stride, visual);
220 }
221
222}
223
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400224static const cairo_user_data_key_t surface_data_key;
225struct surface_data {
226 EGLImageKHR image;
227 GLuint texture;
228 EGLDisplay dpy;
229};
230
231static void
232surface_data_destroy(void *p)
233{
234 struct surface_data *data = p;
235
236 glDeleteTextures(1, &data->texture);
237 eglDestroyImageKHR(data->dpy, data->image);
238}
239
240cairo_surface_t *
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400241display_create_surface(struct display *display,
242 struct rectangle *rectangle)
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400243{
244 struct surface_data *data;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400245 EGLDisplay dpy = display->dpy;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400246 cairo_surface_t *surface;
247
248 EGLint image_attribs[] = {
249 EGL_WIDTH, 0,
250 EGL_HEIGHT, 0,
251 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
252 EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA,
253 EGL_NONE
254 };
255
256 data = malloc(sizeof *data);
257 image_attribs[1] = rectangle->width;
258 image_attribs[3] = rectangle->height;
259 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
260 glGenTextures(1, &data->texture);
261 data->dpy = dpy;
262 glBindTexture(GL_TEXTURE_2D, data->texture);
263 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
264
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400265 surface = cairo_gl_surface_create_for_texture(display->device,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400266 CAIRO_CONTENT_COLOR_ALPHA,
267 data->texture,
268 rectangle->width,
269 rectangle->height);
270
271 cairo_surface_set_user_data (surface, &surface_data_key,
272 data, surface_data_destroy);
273
274 return surface;
275}
276
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500277static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500278window_attach_surface(struct window *window)
279{
280 struct wl_visual *visual;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400281 struct display *display = window->display;
282 struct wl_buffer *buffer;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400283 struct surface_data *data;
284 EGLint name, stride;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500285
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500286 if (window->pending_surface != NULL)
287 return;
288
Kristian Høgsberg09531622010-06-14 23:22:15 -0400289 window->pending_surface = window->cairo_surface;
290 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500291
Kristian Høgsberg09531622010-06-14 23:22:15 -0400292 data = cairo_surface_get_user_data (window->pending_surface,
293 &surface_data_key);
294 eglExportDRMImageMESA(window->display->dpy,
295 data->image, &name, NULL, &stride);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400296
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400297 visual = wl_display_get_premultiplied_argb_visual(display->display);
298 buffer = wl_drm_create_buffer(display->drm,
299 name,
300 window->allocation.width,
301 window->allocation.height,
302 stride,
303 visual);
304
305 wl_surface_attach(window->surface, buffer);
306 wl_buffer_destroy(buffer);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500307
308 wl_surface_map(window->surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400309 window->allocation.x,
310 window->allocation.y,
311 window->allocation.width,
312 window->allocation.height);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400313
314 wl_compositor_commit(window->display->compositor, 0);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500315}
316
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500317void
318window_commit(struct window *window, uint32_t key)
319{
Kristian Høgsberg09531622010-06-14 23:22:15 -0400320 if (window->cairo_surface) {
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500321 window_attach_surface(window);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400322 } else {
323 wl_compositor_commit(window->display->compositor, key);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500324 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500325}
326
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500327static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500328window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500329{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500330 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500331 cairo_text_extents_t extents;
Kristian Høgsberg2d6b7c12010-06-25 16:51:57 -0400332 cairo_pattern_t *outline, *bright, *dim;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500333 int width, height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500334
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500335 window->cairo_surface =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400336 display_create_surface(window->display, &window->allocation);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400337 width = window->allocation.width;
338 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500339
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500340 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500341 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500342 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
343
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500344 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500345
Kristian Høgsberg09531622010-06-14 23:22:15 -0400346 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400347 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400348 cairo_paint(cr);
349
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400350 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
351 tile_mask(cr, window->display->shadow, 3, 3, width, height, 32);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500352
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400353 if (window->keyboard_device)
354 tile_source(cr, window->display->active_frame,
355 0, 0, width, height, 96);
356 else
357 tile_source(cr, window->display->inactive_frame,
358 0, 0, width, height, 96);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500359
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500360 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
361 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500362 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400363 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500364 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
365 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
366 cairo_set_line_width (cr, 4);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400367 if (window->keyboard_device)
368 cairo_set_source_rgb(cr, 0, 0, 0);
369 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400370 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400371 cairo_show_text(cr, window->title);
372
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500373 cairo_destroy(cr);
Kristian Høgsberg10ddbd22010-08-16 21:08:52 -0400374
375 cairo_device_flush (window->display->device);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500376}
377
378static void
379window_draw_fullscreen(struct window *window)
380{
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500381 window->cairo_surface =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400382 display_create_surface(window->display, &window->allocation);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500383}
384
385void
386window_draw(struct window *window)
387{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500388 if (window->cairo_surface != NULL)
389 cairo_surface_destroy(window->cairo_surface);
390
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400391 if (window->fullscreen || !window->decoration)
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500392 window_draw_fullscreen(window);
393 else
394 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500395}
396
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400397cairo_surface_t *
398window_get_surface(struct window *window)
399{
400 return window->cairo_surface;
401}
402
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500403enum window_state {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400404 WINDOW_MOVING = 0,
405 WINDOW_RESIZING_TOP = 1,
406 WINDOW_RESIZING_BOTTOM = 2,
407 WINDOW_RESIZING_LEFT = 4,
408 WINDOW_RESIZING_TOP_LEFT = 5,
409 WINDOW_RESIZING_BOTTOM_LEFT = 6,
410 WINDOW_RESIZING_RIGHT = 8,
411 WINDOW_RESIZING_TOP_RIGHT = 9,
412 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
413 WINDOW_RESIZING_MASK = 15,
414 WINDOW_STABLE = 16,
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500415};
416
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400417enum pointer_type {
418 POINTER_BOTTOM_LEFT,
419 POINTER_BOTTOM_RIGHT,
420 POINTER_BOTTOM,
421 POINTER_DRAGGING,
422 POINTER_LEFT_PTR,
423 POINTER_LEFT,
424 POINTER_RIGHT,
425 POINTER_TOP_LEFT,
426 POINTER_TOP_RIGHT,
427 POINTER_TOP,
428 POINTER_IBEAM,
429};
430
431static int
432get_pointer_location(struct window *window, int32_t x, int32_t y)
433{
434 int vlocation, hlocation;
435 const int grip_size = 8;
436
437 if (x < window->margin)
438 hlocation = WINDOW_STABLE;
439 else if (window->margin <= x && x < window->margin + grip_size)
440 hlocation = WINDOW_RESIZING_LEFT;
441 else if (x < window->allocation.width - window->margin - grip_size)
442 hlocation = WINDOW_MOVING;
443 else if (x < window->allocation.width - window->margin)
444 hlocation = WINDOW_RESIZING_RIGHT;
445 else
446 hlocation = WINDOW_STABLE;
447
448 if (y < window->margin)
449 vlocation = WINDOW_STABLE;
450 else if (window->margin <= y && y < window->margin + grip_size)
451 vlocation = WINDOW_RESIZING_TOP;
452 else if (y < window->allocation.height - window->margin - grip_size)
453 vlocation = WINDOW_MOVING;
454 else if (y < window->allocation.height - window->margin)
455 vlocation = WINDOW_RESIZING_BOTTOM;
456 else
457 vlocation = WINDOW_STABLE;
458
459 return vlocation | hlocation;
460}
461
462static void
463set_pointer_image(struct input *input)
464{
465 struct display *display = input->display;
466 int pointer, location;
467
468 location = get_pointer_location(input->pointer_focus,
469 input->sx, input->sy);
470 switch (location) {
471 case WINDOW_RESIZING_TOP:
472 pointer = POINTER_TOP;
473 break;
474 case WINDOW_RESIZING_BOTTOM:
475 pointer = POINTER_BOTTOM;
476 break;
477 case WINDOW_RESIZING_LEFT:
478 pointer = POINTER_LEFT;
479 break;
480 case WINDOW_RESIZING_RIGHT:
481 pointer = POINTER_RIGHT;
482 break;
483 case WINDOW_RESIZING_TOP_LEFT:
484 pointer = POINTER_TOP_LEFT;
485 break;
486 case WINDOW_RESIZING_TOP_RIGHT:
487 pointer = POINTER_TOP_RIGHT;
488 break;
489 case WINDOW_RESIZING_BOTTOM_LEFT:
490 pointer = POINTER_BOTTOM_LEFT;
491 break;
492 case WINDOW_RESIZING_BOTTOM_RIGHT:
493 pointer = POINTER_BOTTOM_RIGHT;
494 break;
495 case WINDOW_STABLE:
496 wl_input_device_attach(input->input_device,
497 NULL, 0, 0);
498 return;
499 default:
500 /* FIXME: We should ask the application here. */
501 pointer = POINTER_IBEAM;
502 break;
503 }
504
505 wl_input_device_attach(input->input_device,
506 display->pointer_buffers[pointer],
507 pointer_images[pointer].hotspot_x,
508 pointer_images[pointer].hotspot_y);
509}
510
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500511static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500512window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400513 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500514 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500515{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400516 struct input *input = data;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400517
518 input->x = x;
519 input->y = y;
520 input->sx = sx;
521 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400522
523 set_pointer_image(input);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500524}
525
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500526static void window_handle_button(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400527 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500528{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400529 struct input *input = data;
530 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400531 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400532
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400533 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500534
535 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400536 switch (location) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400537 case WINDOW_MOVING:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400538 wl_shell_move(window->display->shell,
539 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500540 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400541 case WINDOW_RESIZING_TOP:
542 case WINDOW_RESIZING_BOTTOM:
543 case WINDOW_RESIZING_LEFT:
544 case WINDOW_RESIZING_RIGHT:
545 case WINDOW_RESIZING_TOP_LEFT:
546 case WINDOW_RESIZING_TOP_RIGHT:
547 case WINDOW_RESIZING_BOTTOM_LEFT:
548 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400549 wl_shell_resize(window->display->shell,
550 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400551 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500552 break;
553 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500554 }
555}
556
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500557static void
558window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400559 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500560{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400561 struct input *input = data;
562 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400563 struct display *d = window->display;
564 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500565
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400566 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400567 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500568 return;
569
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400570 level = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400571 if (input->modifiers & WINDOW_MODIFIER_SHIFT &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400572 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
573 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500574
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400575 sym = XkbKeySymEntry(d->xkb, code, level, 0);
576
577 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400578 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400579 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400580 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500581
582 if (window->key_handler)
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400583 (*window->key_handler)(window, key, sym, state,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400584 input->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500585}
586
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500587static void
588window_handle_pointer_focus(void *data,
589 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400590 uint32_t time, struct wl_surface *surface,
591 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500592{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400593 struct input *input = data;
594
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400595 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400596 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400597 set_pointer_image(input);
598 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400599 input->pointer_focus = NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400600 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500601}
602
603static void
604window_handle_keyboard_focus(void *data,
605 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400606 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500607 struct wl_surface *surface,
608 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500609{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400610 struct input *input = data;
611 struct window *window = input->keyboard_focus;
612 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500613 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500614
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400615 window = input->keyboard_focus;
616 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500617 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400618 if (window->keyboard_focus_handler)
619 (*window->keyboard_focus_handler)(window, NULL,
620 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500621 }
622
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400623 if (surface)
624 input->keyboard_focus = wl_surface_get_user_data(surface);
625 else
626 input->keyboard_focus = NULL;
627
628 end = keys->data + keys->size;
629 for (k = keys->data; k < end; k++)
630 input->modifiers |= d->xkb->map->modmap[*k];
631
632 window = input->keyboard_focus;
633 if (window) {
634 window->keyboard_device = input;
635 if (window->keyboard_focus_handler)
636 (*window->keyboard_focus_handler)(window,
637 window->keyboard_device,
638 window->user_data);
639 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500640}
641
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500642static const struct wl_input_device_listener input_device_listener = {
643 window_handle_motion,
644 window_handle_button,
645 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500646 window_handle_pointer_focus,
647 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500648};
649
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400650static void
651handle_configure(void *data, struct wl_shell *shell,
652 uint32_t time, uint32_t edges,
653 struct wl_surface *surface,
654 int32_t x, int32_t y, int32_t width, int32_t height)
655{
656 struct window *window = wl_surface_get_user_data(surface);
657
658 window->resize_edges = edges;
659 window->pending_allocation.x = x;
660 window->pending_allocation.y = y;
661 window->pending_allocation.width = width;
662 window->pending_allocation.height = height;
663
664 if (!(edges & 15))
665 return;
666
667 if (window->resize_handler)
668 (*window->resize_handler)(window,
669 window->user_data);
670 else if (window->redraw_handler)
671 window_schedule_redraw(window);
672}
673
674static const struct wl_shell_listener shell_listener = {
675 handle_configure,
676};
677
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500678void
679window_get_child_rectangle(struct window *window,
680 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500681{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400682 if (window->fullscreen && !window->decoration) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500683 *rectangle = window->allocation;
684 } else {
685 rectangle->x = window->margin + 10;
686 rectangle->y = window->margin + 50;
687 rectangle->width = window->allocation.width - 20 - window->margin * 2;
688 rectangle->height = window->allocation.height - 60 - window->margin * 2;
689 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500690}
691
692void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500693window_set_child_size(struct window *window,
694 struct rectangle *rectangle)
695{
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400696 int32_t width, height;
697
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500698 if (!window->fullscreen) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400699 width = rectangle->width + 20 + window->margin * 2;
700 height = rectangle->height + 60 + window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400701
702 if (window->resize_edges & WINDOW_RESIZING_LEFT)
703 window->allocation.x +=
704 window->allocation.width - width;
705 if (window->resize_edges & WINDOW_RESIZING_TOP)
706 window->allocation.y +=
707 window->allocation.height - height;
708
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400709 window->allocation.width = width;
710 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500711 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500712}
713
714void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400715window_copy_image(struct window *window,
716 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500717{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400718 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500719}
720
721void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500722window_copy_surface(struct window *window,
723 struct rectangle *rectangle,
724 cairo_surface_t *surface)
725{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500726 cairo_t *cr;
727
728 cr = cairo_create (window->cairo_surface);
729
730 cairo_set_source_surface (cr,
731 surface,
732 rectangle->x, rectangle->y);
733
734 cairo_paint (cr);
735 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500736}
737
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400738static gboolean
739idle_redraw(void *data)
740{
741 struct window *window = data;
742
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400743 if (window->resize_edges)
744 window->allocation = window->pending_allocation;
745
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400746 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400747
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400748 window->redraw_scheduled = 0;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400749 window->resize_edges = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400750
751 return FALSE;
752}
753
754void
755window_schedule_redraw(struct window *window)
756{
757 if (!window->redraw_scheduled) {
758 g_idle_add(idle_redraw, window);
759 window->redraw_scheduled = 1;
760 }
761}
762
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500763void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500764window_set_fullscreen(struct window *window, int fullscreen)
765{
766 window->fullscreen = fullscreen;
767 if (window->fullscreen) {
768 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500769 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500770 } else {
771 window->allocation = window->saved_allocation;
772 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500773}
774
775void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400776window_set_decoration(struct window *window, int decoration)
777{
778 window->decoration = decoration;
779}
780
781void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400782window_set_user_data(struct window *window, void *data)
783{
784 window->user_data = data;
785}
786
787void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500788window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400789 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500790{
791 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500792}
793
794void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400795window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400796 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400797{
798 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400799}
800
801void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500802window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400803 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500804{
805 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500806}
807
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500808void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400809window_set_acknowledge_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400810 window_acknowledge_handler_t handler)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400811{
812 window->acknowledge_handler = handler;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400813}
814
815void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400816window_set_frame_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400817 window_frame_handler_t handler)
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400818{
819 window->frame_handler = handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400820}
821
822void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500823window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400824 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500825{
826 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500827}
828
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400829void
830window_move(struct window *window, int32_t x, int32_t y)
831{
832 window->allocation.x = x;
833 window->allocation.y = y;
834
835 wl_surface_map(window->surface,
836 window->allocation.x - window->margin,
837 window->allocation.y - window->margin,
838 window->allocation.width,
839 window->allocation.height);
840}
841
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500842struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500843window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500844 int32_t x, int32_t y, int32_t width, int32_t height)
845{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500846 struct window *window;
847
848 window = malloc(sizeof *window);
849 if (window == NULL)
850 return NULL;
851
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500852 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500853 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500854 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500855 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500856 window->allocation.x = x;
857 window->allocation.y = y;
858 window->allocation.width = width;
859 window->allocation.height = height;
860 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500861 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400862 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500863
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400864 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400865 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500866
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500867 return window;
868}
869
870static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400871drm_handle_device(void *data, struct wl_drm *drm, const char *device)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400872{
873 struct display *d = data;
874
875 d->device_name = strdup(device);
876}
877
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400878static void drm_handle_authenticated(void *data, struct wl_drm *drm)
879{
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400880 struct display *d = data;
881
882 d->authenticated = 1;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400883}
884
885static const struct wl_drm_listener drm_listener = {
886 drm_handle_device,
887 drm_handle_authenticated
888};
889
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400890static void
891display_handle_acknowledge(void *data,
892 struct wl_compositor *compositor,
893 uint32_t key, uint32_t frame)
894{
895 struct display *d = data;
896 struct window *window;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400897
898 /* The acknowledge event means that the server processed our
899 * last commit request and we can now safely free the old
900 * window buffer if we resized and render the next frame into
901 * our back buffer.. */
902 wl_list_for_each(window, &d->window_list, link) {
Kristian Høgsberg09531622010-06-14 23:22:15 -0400903 cairo_surface_destroy(window->pending_surface);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400904 window->pending_surface = NULL;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400905 if (window->cairo_surface)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400906 window_attach_surface(window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400907 if (window->acknowledge_handler)
908 (*window->acknowledge_handler)(window, key, frame, window->user_data);
909 }
910}
911
912static void
913display_handle_frame(void *data,
914 struct wl_compositor *compositor,
915 uint32_t frame, uint32_t timestamp)
916{
917 struct display *d = data;
918 struct window *window;
919
920 wl_list_for_each(window, &d->window_list, link) {
921 if (window->frame_handler)
922 (*window->frame_handler)(window, frame,
923 timestamp, window->user_data);
924 }
925}
926
927static const struct wl_compositor_listener compositor_listener = {
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400928 display_handle_acknowledge,
929 display_handle_frame,
930};
931
932static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500933display_handle_geometry(void *data,
934 struct wl_output *output,
935 int32_t width, int32_t height)
936{
937 struct display *display = data;
938
939 display->screen_allocation.x = 0;
940 display->screen_allocation.y = 0;
941 display->screen_allocation.width = width;
942 display->screen_allocation.height = height;
943}
944
945static const struct wl_output_listener output_listener = {
946 display_handle_geometry,
947};
948
949static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400950display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400951{
952 struct input *input;
953
954 input = malloc(sizeof *input);
955 if (input == NULL)
956 return;
957
958 memset(input, 0, sizeof *input);
959 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400960 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400961 input->pointer_focus = NULL;
962 input->keyboard_focus = NULL;
963 wl_list_insert(d->input_list.prev, &input->link);
964
965 wl_input_device_add_listener(input->input_device,
966 &input_device_listener, input);
967}
968
969static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400970display_handle_global(struct wl_display *display, uint32_t id,
971 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500972{
973 struct display *d = data;
974
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400975 if (strcmp(interface, "compositor") == 0) {
976 d->compositor = wl_compositor_create(display, id);
977 wl_compositor_add_listener(d->compositor,
978 &compositor_listener, d);
979 } else if (strcmp(interface, "output") == 0) {
980 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500981 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400982 } else if (strcmp(interface, "input_device") == 0) {
983 display_add_input(d, id);
984 } else if (strcmp(interface, "shell") == 0) {
985 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400986 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400987 } else if (strcmp(interface, "drm") == 0) {
988 d->drm = wl_drm_create(display, id);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400989 wl_drm_add_listener(d->drm, &drm_listener, d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500990 }
991}
992
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400993static const char socket_name[] = "\0wayland";
994
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400995static void
996display_render_frame(struct display *d)
997{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400998 int radius = 8;
999 cairo_t *cr;
1000
1001 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1002 cr = cairo_create(d->shadow);
1003 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1004 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1005 rounded_rect(cr, 16, 16, 112, 112, radius);
1006 cairo_fill(cr);
1007 cairo_destroy(cr);
1008 blur_surface(d->shadow, 64);
1009
1010 d->active_frame =
1011 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1012 cr = cairo_create(d->active_frame);
1013 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1014 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1015 rounded_rect(cr, 16, 16, 112, 112, radius);
1016 cairo_fill(cr);
1017 cairo_destroy(cr);
1018
1019 d->inactive_frame =
1020 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1021 cr = cairo_create(d->inactive_frame);
1022 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1023 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1024 rounded_rect(cr, 16, 16, 112, 112, radius);
1025 cairo_fill(cr);
1026 cairo_destroy(cr);
1027}
1028
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001029static void
1030init_xkb(struct display *d)
1031{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001032 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001033
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001034 names.rules = "evdev";
1035 names.model = "pc105";
1036 names.layout = "us";
1037 names.variant = "";
1038 names.options = "";
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001039
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001040 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001041 if (!d->xkb) {
1042 fprintf(stderr, "Failed to compile keymap\n");
1043 exit(1);
1044 }
1045}
1046
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001047struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001048display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001049{
1050 struct display *d;
Kristian Høgsberg379b6782010-07-28 22:52:28 -04001051 EGLint major, minor;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001052 int fd;
1053 GOptionContext *context;
1054 GError *error;
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001055 drm_magic_t magic;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001056
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001057 g_type_init();
1058
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001059 context = g_option_context_new(NULL);
1060 if (option_entries) {
1061 g_option_context_add_main_entries(context, option_entries, "Wayland View");
1062 if (!g_option_context_parse(context, argc, argv, &error)) {
1063 fprintf(stderr, "option parsing failed: %s\n", error->message);
1064 exit(EXIT_FAILURE);
1065 }
1066 }
1067
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001068 d = malloc(sizeof *d);
1069 if (d == NULL)
1070 return NULL;
1071
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001072 d->display = wl_display_create(socket_name, sizeof socket_name);
1073 if (d->display == NULL) {
1074 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001075 return NULL;
1076 }
1077
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001078 wl_list_init(&d->input_list);
1079
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001080 /* Set up listener so we'll catch all events. */
1081 wl_display_add_global_listener(d->display,
1082 display_handle_global, d);
1083
1084 /* Process connection events. */
1085 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1086
1087 fd = open(d->device_name, O_RDWR);
1088 if (fd < 0) {
1089 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001090 return NULL;
1091 }
1092
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001093 if (drmGetMagic(fd, &magic)) {
1094 fprintf(stderr, "DRI2: failed to get drm magic");
1095 return NULL;
1096 }
1097
1098 /* Wait for authenticated event */
1099 wl_drm_authenticate(d->drm, magic);
1100 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1101 while (!d->authenticated)
1102 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1103
Kristian Høgsbergf252d6a2010-07-08 20:15:10 -04001104 d->dpy = eglGetDRMDisplayMESA(fd);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001105 if (!eglInitialize(d->dpy, &major, &minor)) {
1106 fprintf(stderr, "failed to initialize display\n");
1107 return NULL;
1108 }
1109
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001110 eglBindAPI(EGL_OPENGL_API);
1111
Kristian Høgsberg379b6782010-07-28 22:52:28 -04001112 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001113 if (d->ctx == NULL) {
1114 fprintf(stderr, "failed to create context\n");
1115 return NULL;
1116 }
1117
1118 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1119 fprintf(stderr, "faile to make context current\n");
1120 return NULL;
1121 }
1122
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001123 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsberg26449102009-05-28 20:23:31 -04001124 if (d->device == NULL) {
1125 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001126 return NULL;
1127 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001128
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001129 create_pointer_images(d);
1130
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001131 display_render_frame(d);
1132
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001133 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001134 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001135 g_source_attach(d->source, NULL);
1136
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001137 wl_list_init(&d->window_list);
1138
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001139 init_xkb(d);
1140
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001141 return d;
1142}
1143
1144struct wl_compositor *
1145display_get_compositor(struct display *display)
1146{
1147 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001148}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001149
1150EGLDisplay
1151display_get_egl_display(struct display *d)
1152{
1153 return d->dpy;
1154}
1155
1156void
1157display_run(struct display *d)
1158{
1159 g_main_loop_run(d->loop);
1160}