blob: 08a0183ad22759a02560f1f8b129edf45ffedec5 [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øgsberg0395f302008-12-22 12:14:50 -0500374}
375
376static void
377window_draw_fullscreen(struct window *window)
378{
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500379 window->cairo_surface =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400380 display_create_surface(window->display, &window->allocation);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500381}
382
383void
384window_draw(struct window *window)
385{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500386 if (window->cairo_surface != NULL)
387 cairo_surface_destroy(window->cairo_surface);
388
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400389 if (window->fullscreen || !window->decoration)
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500390 window_draw_fullscreen(window);
391 else
392 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500393}
394
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400395cairo_surface_t *
396window_get_surface(struct window *window)
397{
398 return window->cairo_surface;
399}
400
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500401enum window_state {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400402 WINDOW_MOVING = 0,
403 WINDOW_RESIZING_TOP = 1,
404 WINDOW_RESIZING_BOTTOM = 2,
405 WINDOW_RESIZING_LEFT = 4,
406 WINDOW_RESIZING_TOP_LEFT = 5,
407 WINDOW_RESIZING_BOTTOM_LEFT = 6,
408 WINDOW_RESIZING_RIGHT = 8,
409 WINDOW_RESIZING_TOP_RIGHT = 9,
410 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
411 WINDOW_RESIZING_MASK = 15,
412 WINDOW_STABLE = 16,
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500413};
414
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400415enum pointer_type {
416 POINTER_BOTTOM_LEFT,
417 POINTER_BOTTOM_RIGHT,
418 POINTER_BOTTOM,
419 POINTER_DRAGGING,
420 POINTER_LEFT_PTR,
421 POINTER_LEFT,
422 POINTER_RIGHT,
423 POINTER_TOP_LEFT,
424 POINTER_TOP_RIGHT,
425 POINTER_TOP,
426 POINTER_IBEAM,
427};
428
429static int
430get_pointer_location(struct window *window, int32_t x, int32_t y)
431{
432 int vlocation, hlocation;
433 const int grip_size = 8;
434
435 if (x < window->margin)
436 hlocation = WINDOW_STABLE;
437 else if (window->margin <= x && x < window->margin + grip_size)
438 hlocation = WINDOW_RESIZING_LEFT;
439 else if (x < window->allocation.width - window->margin - grip_size)
440 hlocation = WINDOW_MOVING;
441 else if (x < window->allocation.width - window->margin)
442 hlocation = WINDOW_RESIZING_RIGHT;
443 else
444 hlocation = WINDOW_STABLE;
445
446 if (y < window->margin)
447 vlocation = WINDOW_STABLE;
448 else if (window->margin <= y && y < window->margin + grip_size)
449 vlocation = WINDOW_RESIZING_TOP;
450 else if (y < window->allocation.height - window->margin - grip_size)
451 vlocation = WINDOW_MOVING;
452 else if (y < window->allocation.height - window->margin)
453 vlocation = WINDOW_RESIZING_BOTTOM;
454 else
455 vlocation = WINDOW_STABLE;
456
457 return vlocation | hlocation;
458}
459
460static void
461set_pointer_image(struct input *input)
462{
463 struct display *display = input->display;
464 int pointer, location;
465
466 location = get_pointer_location(input->pointer_focus,
467 input->sx, input->sy);
468 switch (location) {
469 case WINDOW_RESIZING_TOP:
470 pointer = POINTER_TOP;
471 break;
472 case WINDOW_RESIZING_BOTTOM:
473 pointer = POINTER_BOTTOM;
474 break;
475 case WINDOW_RESIZING_LEFT:
476 pointer = POINTER_LEFT;
477 break;
478 case WINDOW_RESIZING_RIGHT:
479 pointer = POINTER_RIGHT;
480 break;
481 case WINDOW_RESIZING_TOP_LEFT:
482 pointer = POINTER_TOP_LEFT;
483 break;
484 case WINDOW_RESIZING_TOP_RIGHT:
485 pointer = POINTER_TOP_RIGHT;
486 break;
487 case WINDOW_RESIZING_BOTTOM_LEFT:
488 pointer = POINTER_BOTTOM_LEFT;
489 break;
490 case WINDOW_RESIZING_BOTTOM_RIGHT:
491 pointer = POINTER_BOTTOM_RIGHT;
492 break;
493 case WINDOW_STABLE:
494 wl_input_device_attach(input->input_device,
495 NULL, 0, 0);
496 return;
497 default:
498 /* FIXME: We should ask the application here. */
499 pointer = POINTER_IBEAM;
500 break;
501 }
502
503 wl_input_device_attach(input->input_device,
504 display->pointer_buffers[pointer],
505 pointer_images[pointer].hotspot_x,
506 pointer_images[pointer].hotspot_y);
507}
508
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500509static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500510window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400511 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500512 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500513{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400514 struct input *input = data;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400515
516 input->x = x;
517 input->y = y;
518 input->sx = sx;
519 input->sy = sy;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400520
521 set_pointer_image(input);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500522}
523
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500524static void window_handle_button(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400525 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500526{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400527 struct input *input = data;
528 struct window *window = input->pointer_focus;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400529 int location;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400530
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400531 location = get_pointer_location(window, input->sx, input->sy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500532
533 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400534 switch (location) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400535 case WINDOW_MOVING:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400536 wl_shell_move(window->display->shell,
537 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500538 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400539 case WINDOW_RESIZING_TOP:
540 case WINDOW_RESIZING_BOTTOM:
541 case WINDOW_RESIZING_LEFT:
542 case WINDOW_RESIZING_RIGHT:
543 case WINDOW_RESIZING_TOP_LEFT:
544 case WINDOW_RESIZING_TOP_RIGHT:
545 case WINDOW_RESIZING_BOTTOM_LEFT:
546 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400547 wl_shell_resize(window->display->shell,
548 window->surface, input_device, time,
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400549 location);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500550 break;
551 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500552 }
553}
554
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500555static void
556window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400557 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500558{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400559 struct input *input = data;
560 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400561 struct display *d = window->display;
562 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500563
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400564 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400565 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500566 return;
567
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400568 level = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400569 if (input->modifiers & WINDOW_MODIFIER_SHIFT &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400570 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
571 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500572
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400573 sym = XkbKeySymEntry(d->xkb, code, level, 0);
574
575 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400576 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400577 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400578 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500579
580 if (window->key_handler)
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400581 (*window->key_handler)(window, key, sym, state,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400582 input->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500583}
584
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500585static void
586window_handle_pointer_focus(void *data,
587 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400588 uint32_t time, struct wl_surface *surface,
589 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500590{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400591 struct input *input = data;
592
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400593 if (surface) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400594 input->pointer_focus = wl_surface_get_user_data(surface);
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400595 set_pointer_image(input);
596 } else {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400597 input->pointer_focus = NULL;
Kristian Høgsbergda275dd2010-08-16 17:47:07 -0400598 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500599}
600
601static void
602window_handle_keyboard_focus(void *data,
603 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400604 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500605 struct wl_surface *surface,
606 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500607{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400608 struct input *input = data;
609 struct window *window = input->keyboard_focus;
610 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500611 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500612
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400613 window = input->keyboard_focus;
614 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500615 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400616 if (window->keyboard_focus_handler)
617 (*window->keyboard_focus_handler)(window, NULL,
618 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500619 }
620
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400621 if (surface)
622 input->keyboard_focus = wl_surface_get_user_data(surface);
623 else
624 input->keyboard_focus = NULL;
625
626 end = keys->data + keys->size;
627 for (k = keys->data; k < end; k++)
628 input->modifiers |= d->xkb->map->modmap[*k];
629
630 window = input->keyboard_focus;
631 if (window) {
632 window->keyboard_device = input;
633 if (window->keyboard_focus_handler)
634 (*window->keyboard_focus_handler)(window,
635 window->keyboard_device,
636 window->user_data);
637 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500638}
639
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500640static const struct wl_input_device_listener input_device_listener = {
641 window_handle_motion,
642 window_handle_button,
643 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500644 window_handle_pointer_focus,
645 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500646};
647
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400648static void
649handle_configure(void *data, struct wl_shell *shell,
650 uint32_t time, uint32_t edges,
651 struct wl_surface *surface,
652 int32_t x, int32_t y, int32_t width, int32_t height)
653{
654 struct window *window = wl_surface_get_user_data(surface);
655
656 window->resize_edges = edges;
657 window->pending_allocation.x = x;
658 window->pending_allocation.y = y;
659 window->pending_allocation.width = width;
660 window->pending_allocation.height = height;
661
662 if (!(edges & 15))
663 return;
664
665 if (window->resize_handler)
666 (*window->resize_handler)(window,
667 window->user_data);
668 else if (window->redraw_handler)
669 window_schedule_redraw(window);
670}
671
672static const struct wl_shell_listener shell_listener = {
673 handle_configure,
674};
675
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500676void
677window_get_child_rectangle(struct window *window,
678 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500679{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400680 if (window->fullscreen && !window->decoration) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500681 *rectangle = window->allocation;
682 } else {
683 rectangle->x = window->margin + 10;
684 rectangle->y = window->margin + 50;
685 rectangle->width = window->allocation.width - 20 - window->margin * 2;
686 rectangle->height = window->allocation.height - 60 - window->margin * 2;
687 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500688}
689
690void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500691window_set_child_size(struct window *window,
692 struct rectangle *rectangle)
693{
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400694 int32_t width, height;
695
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500696 if (!window->fullscreen) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400697 width = rectangle->width + 20 + window->margin * 2;
698 height = rectangle->height + 60 + window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400699
700 if (window->resize_edges & WINDOW_RESIZING_LEFT)
701 window->allocation.x +=
702 window->allocation.width - width;
703 if (window->resize_edges & WINDOW_RESIZING_TOP)
704 window->allocation.y +=
705 window->allocation.height - height;
706
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400707 window->allocation.width = width;
708 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500709 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500710}
711
712void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400713window_copy_image(struct window *window,
714 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500715{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400716 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500717}
718
719void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500720window_copy_surface(struct window *window,
721 struct rectangle *rectangle,
722 cairo_surface_t *surface)
723{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500724 cairo_t *cr;
725
726 cr = cairo_create (window->cairo_surface);
727
728 cairo_set_source_surface (cr,
729 surface,
730 rectangle->x, rectangle->y);
731
732 cairo_paint (cr);
733 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500734}
735
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400736static gboolean
737idle_redraw(void *data)
738{
739 struct window *window = data;
740
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400741 if (window->resize_edges)
742 window->allocation = window->pending_allocation;
743
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400744 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400745
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400746 window->redraw_scheduled = 0;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400747 window->resize_edges = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400748
749 return FALSE;
750}
751
752void
753window_schedule_redraw(struct window *window)
754{
755 if (!window->redraw_scheduled) {
756 g_idle_add(idle_redraw, window);
757 window->redraw_scheduled = 1;
758 }
759}
760
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500761void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500762window_set_fullscreen(struct window *window, int fullscreen)
763{
764 window->fullscreen = fullscreen;
765 if (window->fullscreen) {
766 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500767 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500768 } else {
769 window->allocation = window->saved_allocation;
770 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500771}
772
773void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400774window_set_decoration(struct window *window, int decoration)
775{
776 window->decoration = decoration;
777}
778
779void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400780window_set_user_data(struct window *window, void *data)
781{
782 window->user_data = data;
783}
784
785void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500786window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400787 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500788{
789 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500790}
791
792void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400793window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400794 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400795{
796 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400797}
798
799void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500800window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400801 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500802{
803 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500804}
805
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500806void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400807window_set_acknowledge_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400808 window_acknowledge_handler_t handler)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400809{
810 window->acknowledge_handler = handler;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400811}
812
813void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400814window_set_frame_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400815 window_frame_handler_t handler)
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400816{
817 window->frame_handler = handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400818}
819
820void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500821window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400822 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500823{
824 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500825}
826
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400827void
828window_move(struct window *window, int32_t x, int32_t y)
829{
830 window->allocation.x = x;
831 window->allocation.y = y;
832
833 wl_surface_map(window->surface,
834 window->allocation.x - window->margin,
835 window->allocation.y - window->margin,
836 window->allocation.width,
837 window->allocation.height);
838}
839
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500840struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500841window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500842 int32_t x, int32_t y, int32_t width, int32_t height)
843{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500844 struct window *window;
845
846 window = malloc(sizeof *window);
847 if (window == NULL)
848 return NULL;
849
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500850 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500851 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500852 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500853 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500854 window->allocation.x = x;
855 window->allocation.y = y;
856 window->allocation.width = width;
857 window->allocation.height = height;
858 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500859 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400860 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500861
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400862 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400863 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500864
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500865 return window;
866}
867
868static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400869drm_handle_device(void *data, struct wl_drm *drm, const char *device)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400870{
871 struct display *d = data;
872
873 d->device_name = strdup(device);
874}
875
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400876static void drm_handle_authenticated(void *data, struct wl_drm *drm)
877{
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400878 struct display *d = data;
879
880 d->authenticated = 1;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400881}
882
883static const struct wl_drm_listener drm_listener = {
884 drm_handle_device,
885 drm_handle_authenticated
886};
887
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400888static void
889display_handle_acknowledge(void *data,
890 struct wl_compositor *compositor,
891 uint32_t key, uint32_t frame)
892{
893 struct display *d = data;
894 struct window *window;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400895
896 /* The acknowledge event means that the server processed our
897 * last commit request and we can now safely free the old
898 * window buffer if we resized and render the next frame into
899 * our back buffer.. */
900 wl_list_for_each(window, &d->window_list, link) {
Kristian Høgsberg09531622010-06-14 23:22:15 -0400901 cairo_surface_destroy(window->pending_surface);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400902 window->pending_surface = NULL;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400903 if (window->cairo_surface)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400904 window_attach_surface(window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400905 if (window->acknowledge_handler)
906 (*window->acknowledge_handler)(window, key, frame, window->user_data);
907 }
908}
909
910static void
911display_handle_frame(void *data,
912 struct wl_compositor *compositor,
913 uint32_t frame, uint32_t timestamp)
914{
915 struct display *d = data;
916 struct window *window;
917
918 wl_list_for_each(window, &d->window_list, link) {
919 if (window->frame_handler)
920 (*window->frame_handler)(window, frame,
921 timestamp, window->user_data);
922 }
923}
924
925static const struct wl_compositor_listener compositor_listener = {
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400926 display_handle_acknowledge,
927 display_handle_frame,
928};
929
930static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500931display_handle_geometry(void *data,
932 struct wl_output *output,
933 int32_t width, int32_t height)
934{
935 struct display *display = data;
936
937 display->screen_allocation.x = 0;
938 display->screen_allocation.y = 0;
939 display->screen_allocation.width = width;
940 display->screen_allocation.height = height;
941}
942
943static const struct wl_output_listener output_listener = {
944 display_handle_geometry,
945};
946
947static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400948display_add_input(struct display *d, uint32_t id)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400949{
950 struct input *input;
951
952 input = malloc(sizeof *input);
953 if (input == NULL)
954 return;
955
956 memset(input, 0, sizeof *input);
957 input->display = d;
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400958 input->input_device = wl_input_device_create(d->display, id);
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400959 input->pointer_focus = NULL;
960 input->keyboard_focus = NULL;
961 wl_list_insert(d->input_list.prev, &input->link);
962
963 wl_input_device_add_listener(input->input_device,
964 &input_device_listener, input);
965}
966
967static void
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400968display_handle_global(struct wl_display *display, uint32_t id,
969 const char *interface, uint32_t version, void *data)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500970{
971 struct display *d = data;
972
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400973 if (strcmp(interface, "compositor") == 0) {
974 d->compositor = wl_compositor_create(display, id);
975 wl_compositor_add_listener(d->compositor,
976 &compositor_listener, d);
977 } else if (strcmp(interface, "output") == 0) {
978 d->output = wl_output_create(display, id);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500979 wl_output_add_listener(d->output, &output_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400980 } else if (strcmp(interface, "input_device") == 0) {
981 display_add_input(d, id);
982 } else if (strcmp(interface, "shell") == 0) {
983 d->shell = wl_shell_create(display, id);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400984 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg4fe1a3e2010-08-10 14:02:48 -0400985 } else if (strcmp(interface, "drm") == 0) {
986 d->drm = wl_drm_create(display, id);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400987 wl_drm_add_listener(d->drm, &drm_listener, d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500988 }
989}
990
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400991static const char socket_name[] = "\0wayland";
992
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400993static void
994display_render_frame(struct display *d)
995{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400996 int radius = 8;
997 cairo_t *cr;
998
999 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1000 cr = cairo_create(d->shadow);
1001 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1002 cairo_set_source_rgba(cr, 0, 0, 0, 1);
1003 rounded_rect(cr, 16, 16, 112, 112, radius);
1004 cairo_fill(cr);
1005 cairo_destroy(cr);
1006 blur_surface(d->shadow, 64);
1007
1008 d->active_frame =
1009 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1010 cr = cairo_create(d->active_frame);
1011 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1012 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1013 rounded_rect(cr, 16, 16, 112, 112, radius);
1014 cairo_fill(cr);
1015 cairo_destroy(cr);
1016
1017 d->inactive_frame =
1018 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1019 cr = cairo_create(d->inactive_frame);
1020 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1021 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1022 rounded_rect(cr, 16, 16, 112, 112, radius);
1023 cairo_fill(cr);
1024 cairo_destroy(cr);
1025}
1026
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001027static void
1028init_xkb(struct display *d)
1029{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001030 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001031
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001032 names.rules = "evdev";
1033 names.model = "pc105";
1034 names.layout = "us";
1035 names.variant = "";
1036 names.options = "";
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001037
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -04001038 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001039 if (!d->xkb) {
1040 fprintf(stderr, "Failed to compile keymap\n");
1041 exit(1);
1042 }
1043}
1044
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001045struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001046display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001047{
1048 struct display *d;
Kristian Høgsberg379b6782010-07-28 22:52:28 -04001049 EGLint major, minor;
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001050 int fd;
1051 GOptionContext *context;
1052 GError *error;
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001053 drm_magic_t magic;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001054
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001055 g_type_init();
1056
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001057 context = g_option_context_new(NULL);
1058 if (option_entries) {
1059 g_option_context_add_main_entries(context, option_entries, "Wayland View");
1060 if (!g_option_context_parse(context, argc, argv, &error)) {
1061 fprintf(stderr, "option parsing failed: %s\n", error->message);
1062 exit(EXIT_FAILURE);
1063 }
1064 }
1065
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001066 d = malloc(sizeof *d);
1067 if (d == NULL)
1068 return NULL;
1069
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001070 d->display = wl_display_create(socket_name, sizeof socket_name);
1071 if (d->display == NULL) {
1072 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001073 return NULL;
1074 }
1075
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001076 wl_list_init(&d->input_list);
1077
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001078 /* Set up listener so we'll catch all events. */
1079 wl_display_add_global_listener(d->display,
1080 display_handle_global, d);
1081
1082 /* Process connection events. */
1083 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1084
1085 fd = open(d->device_name, O_RDWR);
1086 if (fd < 0) {
1087 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001088 return NULL;
1089 }
1090
Kristian Høgsberg640609a2010-08-09 22:11:47 -04001091 if (drmGetMagic(fd, &magic)) {
1092 fprintf(stderr, "DRI2: failed to get drm magic");
1093 return NULL;
1094 }
1095
1096 /* Wait for authenticated event */
1097 wl_drm_authenticate(d->drm, magic);
1098 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1099 while (!d->authenticated)
1100 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1101
Kristian Høgsbergf252d6a2010-07-08 20:15:10 -04001102 d->dpy = eglGetDRMDisplayMESA(fd);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001103 if (!eglInitialize(d->dpy, &major, &minor)) {
1104 fprintf(stderr, "failed to initialize display\n");
1105 return NULL;
1106 }
1107
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001108 eglBindAPI(EGL_OPENGL_API);
1109
Kristian Høgsberg379b6782010-07-28 22:52:28 -04001110 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001111 if (d->ctx == NULL) {
1112 fprintf(stderr, "failed to create context\n");
1113 return NULL;
1114 }
1115
1116 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1117 fprintf(stderr, "faile to make context current\n");
1118 return NULL;
1119 }
1120
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -04001121 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsberg26449102009-05-28 20:23:31 -04001122 if (d->device == NULL) {
1123 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -05001124 return NULL;
1125 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -05001126
Kristian Høgsbergda275dd2010-08-16 17:47:07 -04001127 create_pointer_images(d);
1128
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -04001129 display_render_frame(d);
1130
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001131 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001132 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001133 g_source_attach(d->source, NULL);
1134
Kristian Høgsberg478d9262010-06-08 20:34:11 -04001135 wl_list_init(&d->window_list);
1136
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001137 init_xkb(d);
1138
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001139 return d;
1140}
1141
1142struct wl_compositor *
1143display_get_compositor(struct display *display)
1144{
1145 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001146}
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001147
1148EGLDisplay
1149display_get_egl_display(struct display *d)
1150{
1151 return d->dpy;
1152}
1153
1154void
1155display_run(struct display *d)
1156{
1157 g_main_loop_run(d->loop);
1158}