blob: 8f8b9e109571a0ef63d9043ad2fefacd7ffb3dcb [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øgsberga85fe3c2010-06-08 14:08:30 -040034
35#define EGL_EGLEXT_PROTOTYPES 1
36#define GL_GLEXT_PROTOTYPES 1
37#include <GL/gl.h>
38#include <EGL/egl.h>
39#include <EGL/eglext.h>
40#include <cairo-gl.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050041
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040042#include <X11/extensions/XKBcommon.h>
43
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050044#include <linux/input.h>
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050045#include "wayland-util.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050046#include "wayland-client.h"
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050047#include "wayland-glib.h"
Kristian Høgsbergf88ae452010-06-05 10:17:55 -040048#include "../cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050049
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050050#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050051
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050052struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050053 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050054 struct wl_compositor *compositor;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050055 struct wl_output *output;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050056 struct rectangle screen_allocation;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040057 EGLDisplay dpy;
58 EGLContext ctx;
Janusz Lewandowskid923e9d2010-01-31 03:01:26 +010059 cairo_device_t *device;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050060 int fd;
Kristian Høgsberg7824d812010-06-08 14:59:44 -040061 GMainLoop *loop;
62 GSource *source;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040063 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040064 struct wl_list input_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040065 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040066 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040067 struct xkb_desc *xkb;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050068};
69
70struct window {
71 struct display *display;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050072 struct wl_surface *surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050073 const char *title;
Kristian Høgsberg09531622010-06-14 23:22:15 -040074 struct rectangle allocation, saved_allocation, surface_allocation;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040075 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050076 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050077 int margin;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050078 int drag_x, drag_y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050079 int state;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050080 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040081 int decoration;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040082 struct input *grab_device;
83 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050084 uint32_t name;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050085
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040086 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -050087 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050088
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050089 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040090 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050091 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050092 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040093 window_acknowledge_handler_t acknowledge_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040094 window_frame_handler_t frame_handler;
95
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050096 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040097 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050098};
99
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400100struct input {
101 struct display *display;
102 struct wl_input_device *input_device;
103 struct window *pointer_focus;
104 struct window *keyboard_focus;
105 uint32_t modifiers;
106 int32_t x, y, sx, sy;
107 struct wl_list link;
108};
109
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500110static void
111rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
112{
113 cairo_move_to(cr, x0, y0 + radius);
114 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
115 cairo_line_to(cr, x1 - radius, y0);
116 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
117 cairo_line_to(cr, x1, y1 - radius);
118 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
119 cairo_line_to(cr, x0 + radius, y1);
120 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
121 cairo_close_path(cr);
122}
123
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400124static const cairo_user_data_key_t surface_data_key;
125struct surface_data {
126 EGLImageKHR image;
127 GLuint texture;
128 EGLDisplay dpy;
129};
130
131static void
132surface_data_destroy(void *p)
133{
134 struct surface_data *data = p;
135
136 glDeleteTextures(1, &data->texture);
137 eglDestroyImageKHR(data->dpy, data->image);
138}
139
140cairo_surface_t *
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400141display_create_surface(struct display *display,
142 struct rectangle *rectangle)
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400143{
144 struct surface_data *data;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400145 EGLDisplay dpy = display->dpy;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400146 cairo_surface_t *surface;
147
148 EGLint image_attribs[] = {
149 EGL_WIDTH, 0,
150 EGL_HEIGHT, 0,
151 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
152 EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA,
153 EGL_NONE
154 };
155
156 data = malloc(sizeof *data);
157 image_attribs[1] = rectangle->width;
158 image_attribs[3] = rectangle->height;
159 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
160 glGenTextures(1, &data->texture);
161 data->dpy = dpy;
162 glBindTexture(GL_TEXTURE_2D, data->texture);
163 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
164
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400165 surface = cairo_gl_surface_create_for_texture(display->device,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400166 CAIRO_CONTENT_COLOR_ALPHA,
167 data->texture,
168 rectangle->width,
169 rectangle->height);
170
171 cairo_surface_set_user_data (surface, &surface_data_key,
172 data, surface_data_destroy);
173
174 return surface;
175}
176
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500177static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500178window_attach_surface(struct window *window)
179{
180 struct wl_visual *visual;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400181 struct surface_data *data;
182 EGLint name, stride;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500183
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500184 if (window->pending_surface != NULL)
185 return;
186
Kristian Høgsberg09531622010-06-14 23:22:15 -0400187 window->pending_surface = window->cairo_surface;
188 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500189
Kristian Høgsberg09531622010-06-14 23:22:15 -0400190 data = cairo_surface_get_user_data (window->pending_surface,
191 &surface_data_key);
192 eglExportDRMImageMESA(window->display->dpy,
193 data->image, &name, NULL, &stride);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400194
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500195 visual = wl_display_get_premultiplied_argb_visual(window->display->display);
196 wl_surface_attach(window->surface,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400197 name,
Kristian Høgsberg09531622010-06-14 23:22:15 -0400198 window->surface_allocation.width,
199 window->surface_allocation.height,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400200 stride,
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500201 visual);
202
203 wl_surface_map(window->surface,
Kristian Høgsberg09531622010-06-14 23:22:15 -0400204 window->surface_allocation.x - window->margin,
205 window->surface_allocation.y - window->margin,
206 window->surface_allocation.width,
207 window->surface_allocation.height);
208
209 wl_compositor_commit(window->display->compositor, 0);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500210}
211
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500212void
213window_commit(struct window *window, uint32_t key)
214{
Kristian Høgsberg09531622010-06-14 23:22:15 -0400215 if (window->cairo_surface) {
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500216 window_attach_surface(window);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400217 } else {
218 wl_compositor_commit(window->display->compositor, key);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500219 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500220}
221
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500222static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500223window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500224{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500225 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500226 cairo_text_extents_t extents;
Kristian Høgsberg2d6b7c12010-06-25 16:51:57 -0400227 cairo_pattern_t *outline, *bright, *dim;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500228 int width, height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500229
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500230 window->cairo_surface =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400231 display_create_surface(window->display, &window->allocation);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400232 window->surface_allocation = window->allocation;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400233 width = window->allocation.width;
234 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500235
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500236 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500237 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500238 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
239
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500240 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500241
Kristian Høgsberg09531622010-06-14 23:22:15 -0400242 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400243 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400244 cairo_paint(cr);
245
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400246 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
247 tile_mask(cr, window->display->shadow, 3, 3, width, height, 32);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500248
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400249 if (window->keyboard_device)
250 tile_source(cr, window->display->active_frame,
251 0, 0, width, height, 96);
252 else
253 tile_source(cr, window->display->inactive_frame,
254 0, 0, width, height, 96);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500255
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500256 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
257 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500258 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400259 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500260 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
261 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
262 cairo_set_line_width (cr, 4);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400263 if (window->keyboard_device)
264 cairo_set_source_rgb(cr, 0, 0, 0);
265 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400266 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400267 cairo_show_text(cr, window->title);
268
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500269 cairo_destroy(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500270}
271
272static void
273window_draw_fullscreen(struct window *window)
274{
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500275 window->cairo_surface =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400276 display_create_surface(window->display, &window->allocation);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400277 window->surface_allocation = window->allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500278}
279
280void
281window_draw(struct window *window)
282{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500283 if (window->cairo_surface != NULL)
284 cairo_surface_destroy(window->cairo_surface);
285
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400286 if (window->fullscreen || !window->decoration)
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500287 window_draw_fullscreen(window);
288 else
289 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500290}
291
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400292cairo_surface_t *
293window_get_surface(struct window *window)
294{
295 return window->cairo_surface;
296}
297
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500298enum window_state {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400299 WINDOW_MOVING = 0,
300 WINDOW_RESIZING_TOP = 1,
301 WINDOW_RESIZING_BOTTOM = 2,
302 WINDOW_RESIZING_LEFT = 4,
303 WINDOW_RESIZING_TOP_LEFT = 5,
304 WINDOW_RESIZING_BOTTOM_LEFT = 6,
305 WINDOW_RESIZING_RIGHT = 8,
306 WINDOW_RESIZING_TOP_RIGHT = 9,
307 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
308 WINDOW_RESIZING_MASK = 15,
309 WINDOW_STABLE = 16,
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500310};
311
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500312static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500313window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400314 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500315 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500316{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400317 struct input *input = data;
318 struct window *window = input->pointer_focus;
319
320 input->x = x;
321 input->y = y;
322 input->sx = sx;
323 input->sy = sy;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500324
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500325 switch (window->state) {
326 case WINDOW_MOVING:
327 if (window->fullscreen)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500328 break;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400329 if (window->grab_device != input)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500330 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500331 window->allocation.x = window->drag_x + x;
332 window->allocation.y = window->drag_y + y;
333 wl_surface_map(window->surface,
334 window->allocation.x - window->margin,
335 window->allocation.y - window->margin,
336 window->allocation.width,
337 window->allocation.height);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500338 wl_compositor_commit(window->display->compositor, 1);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500339 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400340
341 case WINDOW_RESIZING_TOP:
342 case WINDOW_RESIZING_BOTTOM:
343 case WINDOW_RESIZING_LEFT:
344 case WINDOW_RESIZING_RIGHT:
345 case WINDOW_RESIZING_TOP_LEFT:
346 case WINDOW_RESIZING_TOP_RIGHT:
347 case WINDOW_RESIZING_BOTTOM_LEFT:
348 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500349 if (window->fullscreen)
350 break;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400351 if (window->grab_device != input)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500352 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400353 if (window->state & WINDOW_RESIZING_LEFT) {
354 window->allocation.x = x - window->drag_x + window->saved_allocation.x;
355 window->allocation.width = window->drag_x - x + window->saved_allocation.width;
356 }
357 if (window->state & WINDOW_RESIZING_RIGHT)
358 window->allocation.width = x - window->drag_x + window->saved_allocation.width;
359 if (window->state & WINDOW_RESIZING_TOP) {
360 window->allocation.y = y - window->drag_y + window->saved_allocation.y;
361 window->allocation.height = window->drag_y - y + window->saved_allocation.height;
362 }
363 if (window->state & WINDOW_RESIZING_BOTTOM)
364 window->allocation.height = y - window->drag_y + window->saved_allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500365
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500366 if (window->resize_handler)
367 (*window->resize_handler)(window,
368 window->user_data);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400369 else if (window->redraw_handler)
370 window_schedule_redraw(window);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500371 break;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500372 }
373}
374
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500375static void window_handle_button(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400376 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500377{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400378 struct input *input = data;
379 struct window *window = input->pointer_focus;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400380 int grip_size = 8, vlocation, hlocation;
381
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400382 if (window->margin <= input->sx && input->sx < window->margin + grip_size)
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400383 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400384 else if (input->sx < window->allocation.width - window->margin - grip_size)
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400385 hlocation = WINDOW_MOVING;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400386 else if (input->sx < window->allocation.width - window->margin)
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400387 hlocation = WINDOW_RESIZING_RIGHT;
388 else
389 hlocation = WINDOW_STABLE;
390
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400391 if (window->margin <= input->sy && input->sy < window->margin + grip_size)
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400392 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400393 else if (input->sy < window->allocation.height - window->margin - grip_size)
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400394 vlocation = WINDOW_MOVING;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400395 else if (input->sy < window->allocation.height - window->margin)
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400396 vlocation = WINDOW_RESIZING_BOTTOM;
397 else
398 vlocation = WINDOW_STABLE;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500399
400 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400401 switch (hlocation | vlocation) {
402 case WINDOW_MOVING:
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400403 window->drag_x = window->allocation.x - input->x;
404 window->drag_y = window->allocation.y - input->y;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500405 window->state = WINDOW_MOVING;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400406 window->grab_device = input;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500407 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400408 case WINDOW_RESIZING_TOP:
409 case WINDOW_RESIZING_BOTTOM:
410 case WINDOW_RESIZING_LEFT:
411 case WINDOW_RESIZING_RIGHT:
412 case WINDOW_RESIZING_TOP_LEFT:
413 case WINDOW_RESIZING_TOP_RIGHT:
414 case WINDOW_RESIZING_BOTTOM_LEFT:
415 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400416 window->drag_x = input->x;
417 window->drag_y = input->y;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400418 window->saved_allocation = window->allocation;
419 window->state = hlocation | vlocation;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400420 window->grab_device = input;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500421 break;
422 default:
423 window->state = WINDOW_STABLE;
424 break;
425 }
426 } else if (button == BTN_LEFT &&
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400427 state == 0 && window->grab_device == input) {
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500428 window->state = WINDOW_STABLE;
429 }
430}
431
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500432static void
433window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400434 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500435{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400436 struct input *input = data;
437 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400438 struct display *d = window->display;
439 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500440
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400441 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400442 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500443 return;
444
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400445 level = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400446 if (input->modifiers & WINDOW_MODIFIER_SHIFT &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400447 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
448 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500449
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400450 sym = XkbKeySymEntry(d->xkb, code, level, 0);
451
452 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400453 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400454 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400455 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500456
457 if (window->key_handler)
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400458 (*window->key_handler)(window, key, sym, state,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400459 input->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500460}
461
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500462static void
463window_handle_pointer_focus(void *data,
464 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400465 uint32_t time, struct wl_surface *surface)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500466{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400467 struct input *input = data;
468
469 if (surface)
470 input->pointer_focus = wl_surface_get_user_data(surface);
471 else
472 input->pointer_focus = NULL;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500473}
474
475static void
476window_handle_keyboard_focus(void *data,
477 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400478 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500479 struct wl_surface *surface,
480 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500481{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400482 struct input *input = data;
483 struct window *window = input->keyboard_focus;
484 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500485 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500486
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400487 window = input->keyboard_focus;
488 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500489 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400490 if (window->keyboard_focus_handler)
491 (*window->keyboard_focus_handler)(window, NULL,
492 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500493 }
494
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400495 if (surface)
496 input->keyboard_focus = wl_surface_get_user_data(surface);
497 else
498 input->keyboard_focus = NULL;
499
500 end = keys->data + keys->size;
501 for (k = keys->data; k < end; k++)
502 input->modifiers |= d->xkb->map->modmap[*k];
503
504 window = input->keyboard_focus;
505 if (window) {
506 window->keyboard_device = input;
507 if (window->keyboard_focus_handler)
508 (*window->keyboard_focus_handler)(window,
509 window->keyboard_device,
510 window->user_data);
511 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500512}
513
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500514static const struct wl_input_device_listener input_device_listener = {
515 window_handle_motion,
516 window_handle_button,
517 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500518 window_handle_pointer_focus,
519 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500520};
521
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500522void
523window_get_child_rectangle(struct window *window,
524 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500525{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400526 if (window->fullscreen && !window->decoration) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500527 *rectangle = window->allocation;
528 } else {
529 rectangle->x = window->margin + 10;
530 rectangle->y = window->margin + 50;
531 rectangle->width = window->allocation.width - 20 - window->margin * 2;
532 rectangle->height = window->allocation.height - 60 - window->margin * 2;
533 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500534}
535
536void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500537window_set_child_size(struct window *window,
538 struct rectangle *rectangle)
539{
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400540 int32_t width, height;
541
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500542 if (!window->fullscreen) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400543 width = rectangle->width + 20 + window->margin * 2;
544 height = rectangle->height + 60 + window->margin * 2;
545 if (window->state & WINDOW_RESIZING_LEFT)
546 window->allocation.x -=
547 width - window->allocation.width;
548 if (window->state & WINDOW_RESIZING_TOP)
549 window->allocation.y -=
550 height - window->allocation.height;
551 window->allocation.width = width;
552 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500553 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500554}
555
556void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400557window_copy_image(struct window *window,
558 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500559{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400560 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500561}
562
563void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500564window_copy_surface(struct window *window,
565 struct rectangle *rectangle,
566 cairo_surface_t *surface)
567{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500568 cairo_t *cr;
569
570 cr = cairo_create (window->cairo_surface);
571
572 cairo_set_source_surface (cr,
573 surface,
574 rectangle->x, rectangle->y);
575
576 cairo_paint (cr);
577 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500578}
579
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400580static gboolean
581idle_redraw(void *data)
582{
583 struct window *window = data;
584
585 window->redraw_handler(window, window->user_data);
586 window->redraw_scheduled = 0;
587
588 return FALSE;
589}
590
591void
592window_schedule_redraw(struct window *window)
593{
594 if (!window->redraw_scheduled) {
595 g_idle_add(idle_redraw, window);
596 window->redraw_scheduled = 1;
597 }
598}
599
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500600void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500601window_set_fullscreen(struct window *window, int fullscreen)
602{
603 window->fullscreen = fullscreen;
604 if (window->fullscreen) {
605 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500606 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500607 } else {
608 window->allocation = window->saved_allocation;
609 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500610}
611
612void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400613window_set_decoration(struct window *window, int decoration)
614{
615 window->decoration = decoration;
616}
617
618void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400619window_set_user_data(struct window *window, void *data)
620{
621 window->user_data = data;
622}
623
624void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500625window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400626 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500627{
628 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500629}
630
631void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400632window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400633 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400634{
635 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400636}
637
638void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500639window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400640 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500641{
642 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500643}
644
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500645void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400646window_set_acknowledge_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400647 window_acknowledge_handler_t handler)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400648{
649 window->acknowledge_handler = handler;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400650}
651
652void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400653window_set_frame_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400654 window_frame_handler_t handler)
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400655{
656 window->frame_handler = handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400657}
658
659void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500660window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400661 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500662{
663 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500664}
665
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400666void
667window_move(struct window *window, int32_t x, int32_t y)
668{
669 window->allocation.x = x;
670 window->allocation.y = y;
671
672 wl_surface_map(window->surface,
673 window->allocation.x - window->margin,
674 window->allocation.y - window->margin,
675 window->allocation.width,
676 window->allocation.height);
677}
678
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500679struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500680window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500681 int32_t x, int32_t y, int32_t width, int32_t height)
682{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500683 struct window *window;
684
685 window = malloc(sizeof *window);
686 if (window == NULL)
687 return NULL;
688
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500689 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500690 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500691 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500692 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500693 window->allocation.x = x;
694 window->allocation.y = y;
695 window->allocation.width = width;
696 window->allocation.height = height;
697 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500698 window->margin = 16;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500699 window->state = WINDOW_STABLE;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400700 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500701
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400702 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400703 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500704
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500705 return window;
706}
707
708static void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400709display_handle_device(void *data,
710 struct wl_compositor *compositor,
711 const char *device)
712{
713 struct display *d = data;
714
715 d->device_name = strdup(device);
716}
717
718static void
719display_handle_acknowledge(void *data,
720 struct wl_compositor *compositor,
721 uint32_t key, uint32_t frame)
722{
723 struct display *d = data;
724 struct window *window;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400725
726 /* The acknowledge event means that the server processed our
727 * last commit request and we can now safely free the old
728 * window buffer if we resized and render the next frame into
729 * our back buffer.. */
730 wl_list_for_each(window, &d->window_list, link) {
Kristian Høgsberg09531622010-06-14 23:22:15 -0400731 cairo_surface_destroy(window->pending_surface);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400732 window->pending_surface = NULL;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400733 if (window->cairo_surface)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400734 window_attach_surface(window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400735 if (window->acknowledge_handler)
736 (*window->acknowledge_handler)(window, key, frame, window->user_data);
737 }
738}
739
740static void
741display_handle_frame(void *data,
742 struct wl_compositor *compositor,
743 uint32_t frame, uint32_t timestamp)
744{
745 struct display *d = data;
746 struct window *window;
747
748 wl_list_for_each(window, &d->window_list, link) {
749 if (window->frame_handler)
750 (*window->frame_handler)(window, frame,
751 timestamp, window->user_data);
752 }
753}
754
755static const struct wl_compositor_listener compositor_listener = {
756 display_handle_device,
757 display_handle_acknowledge,
758 display_handle_frame,
759};
760
761static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500762display_handle_geometry(void *data,
763 struct wl_output *output,
764 int32_t width, int32_t height)
765{
766 struct display *display = data;
767
768 display->screen_allocation.x = 0;
769 display->screen_allocation.y = 0;
770 display->screen_allocation.width = width;
771 display->screen_allocation.height = height;
772}
773
774static const struct wl_output_listener output_listener = {
775 display_handle_geometry,
776};
777
778static void
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400779display_add_input(struct display *d, struct wl_object *object)
780{
781 struct input *input;
782
783 input = malloc(sizeof *input);
784 if (input == NULL)
785 return;
786
787 memset(input, 0, sizeof *input);
788 input->display = d;
789 input->input_device = (struct wl_input_device *) object;
790 input->pointer_focus = NULL;
791 input->keyboard_focus = NULL;
792 wl_list_insert(d->input_list.prev, &input->link);
793
794 wl_input_device_add_listener(input->input_device,
795 &input_device_listener, input);
796}
797
798static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500799display_handle_global(struct wl_display *display,
800 struct wl_object *object, void *data)
801{
802 struct display *d = data;
803
804 if (wl_object_implements(object, "compositor", 1)) {
805 d->compositor = (struct wl_compositor *) object;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400806 wl_compositor_add_listener(d->compositor, &compositor_listener, d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500807 } else if (wl_object_implements(object, "output", 1)) {
808 d->output = (struct wl_output *) object;
809 wl_output_add_listener(d->output, &output_listener, d);
810 } else if (wl_object_implements(object, "input_device", 1)) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400811 display_add_input(d, object);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500812 }
813}
814
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400815static const char socket_name[] = "\0wayland";
816
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400817static void
818display_render_frame(struct display *d)
819{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400820 int radius = 8;
821 cairo_t *cr;
822
823 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
824 cr = cairo_create(d->shadow);
825 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
826 cairo_set_source_rgba(cr, 0, 0, 0, 1);
827 rounded_rect(cr, 16, 16, 112, 112, radius);
828 cairo_fill(cr);
829 cairo_destroy(cr);
830 blur_surface(d->shadow, 64);
831
832 d->active_frame =
833 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
834 cr = cairo_create(d->active_frame);
835 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
836 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
837 rounded_rect(cr, 16, 16, 112, 112, radius);
838 cairo_fill(cr);
839 cairo_destroy(cr);
840
841 d->inactive_frame =
842 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
843 cr = cairo_create(d->inactive_frame);
844 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
845 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
846 rounded_rect(cr, 16, 16, 112, 112, radius);
847 cairo_fill(cr);
848 cairo_destroy(cr);
849}
850
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400851static void
852init_xkb(struct display *d)
853{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -0400854 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400855
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -0400856 names.rules = "evdev";
857 names.model = "pc105";
858 names.layout = "us";
859 names.variant = "";
860 names.options = "";
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400861
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -0400862 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400863 if (!d->xkb) {
864 fprintf(stderr, "Failed to compile keymap\n");
865 exit(1);
866 }
867}
868
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500869struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400870display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500871{
872 struct display *d;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400873 EGLint major, minor, count;
874 EGLConfig config;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400875 int fd;
876 GOptionContext *context;
877 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400878
879 static const EGLint config_attribs[] = {
880 EGL_SURFACE_TYPE, 0,
881 EGL_NO_SURFACE_CAPABLE_MESA, EGL_OPENGL_BIT,
882 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
883 EGL_NONE
884 };
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500885
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400886 g_type_init();
887
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400888 context = g_option_context_new(NULL);
889 if (option_entries) {
890 g_option_context_add_main_entries(context, option_entries, "Wayland View");
891 if (!g_option_context_parse(context, argc, argv, &error)) {
892 fprintf(stderr, "option parsing failed: %s\n", error->message);
893 exit(EXIT_FAILURE);
894 }
895 }
896
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500897 d = malloc(sizeof *d);
898 if (d == NULL)
899 return NULL;
900
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400901 d->display = wl_display_create(socket_name, sizeof socket_name);
902 if (d->display == NULL) {
903 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400904 return NULL;
905 }
906
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400907 wl_list_init(&d->input_list);
908
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400909 /* Set up listener so we'll catch all events. */
910 wl_display_add_global_listener(d->display,
911 display_handle_global, d);
912
913 /* Process connection events. */
914 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
915
916 fd = open(d->device_name, O_RDWR);
917 if (fd < 0) {
918 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400919 return NULL;
920 }
921
Kristian Høgsbergf252d6a2010-07-08 20:15:10 -0400922 d->dpy = eglGetDRMDisplayMESA(fd);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400923 if (!eglInitialize(d->dpy, &major, &minor)) {
924 fprintf(stderr, "failed to initialize display\n");
925 return NULL;
926 }
927
928 if (!eglChooseConfig(d->dpy, config_attribs, &config, 1, &count) ||
929 count == 0) {
930 fprintf(stderr, "eglChooseConfig() failed\n");
931 return NULL;
932 }
933
934 eglBindAPI(EGL_OPENGL_API);
935
936 d->ctx = eglCreateContext(d->dpy, config, EGL_NO_CONTEXT, NULL);
937 if (d->ctx == NULL) {
938 fprintf(stderr, "failed to create context\n");
939 return NULL;
940 }
941
942 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
943 fprintf(stderr, "faile to make context current\n");
944 return NULL;
945 }
946
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400947 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsberg26449102009-05-28 20:23:31 -0400948 if (d->device == NULL) {
949 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500950 return NULL;
951 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500952
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400953 display_render_frame(d);
954
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400955 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400956 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400957 g_source_attach(d->source, NULL);
958
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400959 wl_list_init(&d->window_list);
960
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400961 init_xkb(d);
962
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500963 return d;
964}
965
966struct wl_compositor *
967display_get_compositor(struct display *display)
968{
969 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500970}
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400971
972EGLDisplay
973display_get_egl_display(struct display *d)
974{
975 return d->dpy;
976}
977
978void
979display_run(struct display *d)
980{
981 g_main_loop_run(d->loop);
982}