blob: c41d2f59277eef44fff1dcb82fa92f253aab1514 [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øgsberg640609a2010-08-09 22:11:47 -040034#include <xf86drm.h>
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040035
36#define EGL_EGLEXT_PROTOTYPES 1
37#define GL_GLEXT_PROTOTYPES 1
38#include <GL/gl.h>
39#include <EGL/egl.h>
40#include <EGL/eglext.h>
41#include <cairo-gl.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050042
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040043#include <X11/extensions/XKBcommon.h>
44
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050045#include <linux/input.h>
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -050046#include "wayland-util.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050047#include "wayland-client.h"
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050048#include "wayland-glib.h"
Kristian Høgsbergf88ae452010-06-05 10:17:55 -040049#include "../cairo-util.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050050
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050051#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050052
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050053struct display {
Kristian Høgsberg40979232008-11-25 22:40:39 -050054 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050055 struct wl_compositor *compositor;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040056 struct wl_shell *shell;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -040057 struct wl_drm *drm;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050058 struct wl_output *output;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050059 struct rectangle screen_allocation;
Kristian Høgsberg640609a2010-08-09 22:11:47 -040060 int authenticated;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040061 EGLDisplay dpy;
62 EGLContext ctx;
Janusz Lewandowskid923e9d2010-01-31 03:01:26 +010063 cairo_device_t *device;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050064 int fd;
Kristian Høgsberg7824d812010-06-08 14:59:44 -040065 GMainLoop *loop;
66 GSource *source;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040067 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040068 struct wl_list input_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040069 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040070 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040071 struct xkb_desc *xkb;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050072};
73
74struct window {
75 struct display *display;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050076 struct wl_surface *surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050077 const char *title;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040078 struct rectangle allocation, saved_allocation, pending_allocation;
79 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040080 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050081 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050082 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050083 int fullscreen;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040084 int decoration;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040085 struct input *grab_device;
86 struct input *keyboard_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050087 uint32_t name;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050088
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040089 EGLImageKHR *image;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -050090 cairo_surface_t *cairo_surface, *pending_surface;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050091
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050092 window_resize_handler_t resize_handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040093 window_redraw_handler_t redraw_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050094 window_key_handler_t key_handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050095 window_keyboard_focus_handler_t keyboard_focus_handler;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040096 window_acknowledge_handler_t acknowledge_handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040097 window_frame_handler_t frame_handler;
98
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050099 void *user_data;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400100 struct wl_list link;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500101};
102
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400103struct input {
104 struct display *display;
105 struct wl_input_device *input_device;
106 struct window *pointer_focus;
107 struct window *keyboard_focus;
108 uint32_t modifiers;
109 int32_t x, y, sx, sy;
110 struct wl_list link;
111};
112
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500113static void
114rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
115{
116 cairo_move_to(cr, x0, y0 + radius);
117 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
118 cairo_line_to(cr, x1 - radius, y0);
119 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
120 cairo_line_to(cr, x1, y1 - radius);
121 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
122 cairo_line_to(cr, x0 + radius, y1);
123 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
124 cairo_close_path(cr);
125}
126
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400127static const cairo_user_data_key_t surface_data_key;
128struct surface_data {
129 EGLImageKHR image;
130 GLuint texture;
131 EGLDisplay dpy;
132};
133
134static void
135surface_data_destroy(void *p)
136{
137 struct surface_data *data = p;
138
139 glDeleteTextures(1, &data->texture);
140 eglDestroyImageKHR(data->dpy, data->image);
141}
142
143cairo_surface_t *
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400144display_create_surface(struct display *display,
145 struct rectangle *rectangle)
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400146{
147 struct surface_data *data;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400148 EGLDisplay dpy = display->dpy;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400149 cairo_surface_t *surface;
150
151 EGLint image_attribs[] = {
152 EGL_WIDTH, 0,
153 EGL_HEIGHT, 0,
154 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
155 EGL_IMAGE_USE_MESA, EGL_IMAGE_USE_SCANOUT_MESA,
156 EGL_NONE
157 };
158
159 data = malloc(sizeof *data);
160 image_attribs[1] = rectangle->width;
161 image_attribs[3] = rectangle->height;
162 data->image = eglCreateDRMImageMESA(dpy, image_attribs);
163 glGenTextures(1, &data->texture);
164 data->dpy = dpy;
165 glBindTexture(GL_TEXTURE_2D, data->texture);
166 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
167
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400168 surface = cairo_gl_surface_create_for_texture(display->device,
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400169 CAIRO_CONTENT_COLOR_ALPHA,
170 data->texture,
171 rectangle->width,
172 rectangle->height);
173
174 cairo_surface_set_user_data (surface, &surface_data_key,
175 data, surface_data_destroy);
176
177 return surface;
178}
179
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500180static void
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500181window_attach_surface(struct window *window)
182{
183 struct wl_visual *visual;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400184 struct display *display = window->display;
185 struct wl_buffer *buffer;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400186 struct surface_data *data;
187 EGLint name, stride;
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500188
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500189 if (window->pending_surface != NULL)
190 return;
191
Kristian Høgsberg09531622010-06-14 23:22:15 -0400192 window->pending_surface = window->cairo_surface;
193 window->cairo_surface = NULL;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500194
Kristian Høgsberg09531622010-06-14 23:22:15 -0400195 data = cairo_surface_get_user_data (window->pending_surface,
196 &surface_data_key);
197 eglExportDRMImageMESA(window->display->dpy,
198 data->image, &name, NULL, &stride);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400199
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400200 visual = wl_display_get_premultiplied_argb_visual(display->display);
201 buffer = wl_drm_create_buffer(display->drm,
202 name,
203 window->allocation.width,
204 window->allocation.height,
205 stride,
206 visual);
207
208 wl_surface_attach(window->surface, buffer);
209 wl_buffer_destroy(buffer);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500210
211 wl_surface_map(window->surface,
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400212 window->allocation.x,
213 window->allocation.y,
214 window->allocation.width,
215 window->allocation.height);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400216
217 wl_compositor_commit(window->display->compositor, 0);
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500218}
219
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500220void
221window_commit(struct window *window, uint32_t key)
222{
Kristian Høgsberg09531622010-06-14 23:22:15 -0400223 if (window->cairo_surface) {
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500224 window_attach_surface(window);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400225 } else {
226 wl_compositor_commit(window->display->compositor, key);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500227 }
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500228}
229
Kristian Høgsberg6a1b2012009-12-16 14:43:37 -0500230static void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500231window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500232{
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500233 cairo_t *cr;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500234 cairo_text_extents_t extents;
Kristian Høgsberg2d6b7c12010-06-25 16:51:57 -0400235 cairo_pattern_t *outline, *bright, *dim;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500236 int width, height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500237
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500238 window->cairo_surface =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400239 display_create_surface(window->display, &window->allocation);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400240 width = window->allocation.width;
241 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500242
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500243 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500244 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500245 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
246
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500247 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500248
Kristian Høgsberg09531622010-06-14 23:22:15 -0400249 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400250 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400251 cairo_paint(cr);
252
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400253 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
254 tile_mask(cr, window->display->shadow, 3, 3, width, height, 32);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500255
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400256 if (window->keyboard_device)
257 tile_source(cr, window->display->active_frame,
258 0, 0, width, height, 96);
259 else
260 tile_source(cr, window->display->inactive_frame,
261 0, 0, width, height, 96);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500262
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500263 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
264 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500265 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400266 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500267 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
268 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
269 cairo_set_line_width (cr, 4);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400270 if (window->keyboard_device)
271 cairo_set_source_rgb(cr, 0, 0, 0);
272 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400273 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400274 cairo_show_text(cr, window->title);
275
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500276 cairo_destroy(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500277}
278
279static void
280window_draw_fullscreen(struct window *window)
281{
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500282 window->cairo_surface =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400283 display_create_surface(window->display, &window->allocation);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500284}
285
286void
287window_draw(struct window *window)
288{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500289 if (window->cairo_surface != NULL)
290 cairo_surface_destroy(window->cairo_surface);
291
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400292 if (window->fullscreen || !window->decoration)
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500293 window_draw_fullscreen(window);
294 else
295 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500296}
297
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400298cairo_surface_t *
299window_get_surface(struct window *window)
300{
301 return window->cairo_surface;
302}
303
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500304enum window_state {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400305 WINDOW_MOVING = 0,
306 WINDOW_RESIZING_TOP = 1,
307 WINDOW_RESIZING_BOTTOM = 2,
308 WINDOW_RESIZING_LEFT = 4,
309 WINDOW_RESIZING_TOP_LEFT = 5,
310 WINDOW_RESIZING_BOTTOM_LEFT = 6,
311 WINDOW_RESIZING_RIGHT = 8,
312 WINDOW_RESIZING_TOP_RIGHT = 9,
313 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
314 WINDOW_RESIZING_MASK = 15,
315 WINDOW_STABLE = 16,
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500316};
317
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500318static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500319window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400320 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500321 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500322{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400323 struct input *input = data;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400324
325 input->x = x;
326 input->y = y;
327 input->sx = sx;
328 input->sy = sy;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500329}
330
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500331static void window_handle_button(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400332 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500333{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400334 struct input *input = data;
335 struct window *window = input->pointer_focus;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400336 int grip_size = 8, vlocation, hlocation;
337
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400338 if (window->margin <= input->sx && input->sx < window->margin + grip_size)
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400339 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400340 else if (input->sx < window->allocation.width - window->margin - grip_size)
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400341 hlocation = WINDOW_MOVING;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400342 else if (input->sx < window->allocation.width - window->margin)
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400343 hlocation = WINDOW_RESIZING_RIGHT;
344 else
345 hlocation = WINDOW_STABLE;
346
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400347 if (window->margin <= input->sy && input->sy < window->margin + grip_size)
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400348 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400349 else if (input->sy < window->allocation.height - window->margin - grip_size)
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400350 vlocation = WINDOW_MOVING;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400351 else if (input->sy < window->allocation.height - window->margin)
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400352 vlocation = WINDOW_RESIZING_BOTTOM;
353 else
354 vlocation = WINDOW_STABLE;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500355
356 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400357 switch (hlocation | vlocation) {
358 case WINDOW_MOVING:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400359 wl_shell_move(window->display->shell,
360 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500361 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400362 case WINDOW_RESIZING_TOP:
363 case WINDOW_RESIZING_BOTTOM:
364 case WINDOW_RESIZING_LEFT:
365 case WINDOW_RESIZING_RIGHT:
366 case WINDOW_RESIZING_TOP_LEFT:
367 case WINDOW_RESIZING_TOP_RIGHT:
368 case WINDOW_RESIZING_BOTTOM_LEFT:
369 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400370 wl_shell_resize(window->display->shell,
371 window->surface, input_device, time,
372 hlocation | vlocation);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500373 break;
374 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500375 }
376}
377
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500378static void
379window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400380 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500381{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400382 struct input *input = data;
383 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400384 struct display *d = window->display;
385 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500386
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400387 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400388 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500389 return;
390
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400391 level = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400392 if (input->modifiers & WINDOW_MODIFIER_SHIFT &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400393 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
394 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500395
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400396 sym = XkbKeySymEntry(d->xkb, code, level, 0);
397
398 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400399 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400400 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400401 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500402
403 if (window->key_handler)
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400404 (*window->key_handler)(window, key, sym, state,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400405 input->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500406}
407
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500408static void
409window_handle_pointer_focus(void *data,
410 struct wl_input_device *input_device,
Kristian Høgsberg6d702022010-08-06 15:12:22 -0400411 uint32_t time, struct wl_surface *surface,
412 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500413{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400414 struct input *input = data;
415
416 if (surface)
417 input->pointer_focus = wl_surface_get_user_data(surface);
418 else
419 input->pointer_focus = NULL;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500420}
421
422static void
423window_handle_keyboard_focus(void *data,
424 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400425 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500426 struct wl_surface *surface,
427 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500428{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400429 struct input *input = data;
430 struct window *window = input->keyboard_focus;
431 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500432 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500433
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400434 window = input->keyboard_focus;
435 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500436 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400437 if (window->keyboard_focus_handler)
438 (*window->keyboard_focus_handler)(window, NULL,
439 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500440 }
441
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400442 if (surface)
443 input->keyboard_focus = wl_surface_get_user_data(surface);
444 else
445 input->keyboard_focus = NULL;
446
447 end = keys->data + keys->size;
448 for (k = keys->data; k < end; k++)
449 input->modifiers |= d->xkb->map->modmap[*k];
450
451 window = input->keyboard_focus;
452 if (window) {
453 window->keyboard_device = input;
454 if (window->keyboard_focus_handler)
455 (*window->keyboard_focus_handler)(window,
456 window->keyboard_device,
457 window->user_data);
458 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500459}
460
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500461static const struct wl_input_device_listener input_device_listener = {
462 window_handle_motion,
463 window_handle_button,
464 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500465 window_handle_pointer_focus,
466 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500467};
468
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400469static void
470handle_configure(void *data, struct wl_shell *shell,
471 uint32_t time, uint32_t edges,
472 struct wl_surface *surface,
473 int32_t x, int32_t y, int32_t width, int32_t height)
474{
475 struct window *window = wl_surface_get_user_data(surface);
476
477 window->resize_edges = edges;
478 window->pending_allocation.x = x;
479 window->pending_allocation.y = y;
480 window->pending_allocation.width = width;
481 window->pending_allocation.height = height;
482
483 if (!(edges & 15))
484 return;
485
486 if (window->resize_handler)
487 (*window->resize_handler)(window,
488 window->user_data);
489 else if (window->redraw_handler)
490 window_schedule_redraw(window);
491}
492
493static const struct wl_shell_listener shell_listener = {
494 handle_configure,
495};
496
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500497void
498window_get_child_rectangle(struct window *window,
499 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500500{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400501 if (window->fullscreen && !window->decoration) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500502 *rectangle = window->allocation;
503 } else {
504 rectangle->x = window->margin + 10;
505 rectangle->y = window->margin + 50;
506 rectangle->width = window->allocation.width - 20 - window->margin * 2;
507 rectangle->height = window->allocation.height - 60 - window->margin * 2;
508 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500509}
510
511void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500512window_set_child_size(struct window *window,
513 struct rectangle *rectangle)
514{
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400515 int32_t width, height;
516
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500517 if (!window->fullscreen) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400518 width = rectangle->width + 20 + window->margin * 2;
519 height = rectangle->height + 60 + window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400520
521 if (window->resize_edges & WINDOW_RESIZING_LEFT)
522 window->allocation.x +=
523 window->allocation.width - width;
524 if (window->resize_edges & WINDOW_RESIZING_TOP)
525 window->allocation.y +=
526 window->allocation.height - height;
527
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400528 window->allocation.width = width;
529 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500530 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500531}
532
533void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400534window_copy_image(struct window *window,
535 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500536{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400537 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500538}
539
540void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500541window_copy_surface(struct window *window,
542 struct rectangle *rectangle,
543 cairo_surface_t *surface)
544{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500545 cairo_t *cr;
546
547 cr = cairo_create (window->cairo_surface);
548
549 cairo_set_source_surface (cr,
550 surface,
551 rectangle->x, rectangle->y);
552
553 cairo_paint (cr);
554 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500555}
556
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400557static gboolean
558idle_redraw(void *data)
559{
560 struct window *window = data;
561
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400562 if (window->resize_edges)
563 window->allocation = window->pending_allocation;
564
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400565 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400566
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400567 window->redraw_scheduled = 0;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400568 window->resize_edges = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400569
570 return FALSE;
571}
572
573void
574window_schedule_redraw(struct window *window)
575{
576 if (!window->redraw_scheduled) {
577 g_idle_add(idle_redraw, window);
578 window->redraw_scheduled = 1;
579 }
580}
581
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500582void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500583window_set_fullscreen(struct window *window, int fullscreen)
584{
585 window->fullscreen = fullscreen;
586 if (window->fullscreen) {
587 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500588 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500589 } else {
590 window->allocation = window->saved_allocation;
591 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500592}
593
594void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400595window_set_decoration(struct window *window, int decoration)
596{
597 window->decoration = decoration;
598}
599
600void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400601window_set_user_data(struct window *window, void *data)
602{
603 window->user_data = data;
604}
605
606void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500607window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400608 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500609{
610 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500611}
612
613void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400614window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400615 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400616{
617 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400618}
619
620void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500621window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400622 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500623{
624 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500625}
626
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500627void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400628window_set_acknowledge_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400629 window_acknowledge_handler_t handler)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400630{
631 window->acknowledge_handler = handler;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400632}
633
634void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400635window_set_frame_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400636 window_frame_handler_t handler)
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400637{
638 window->frame_handler = handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400639}
640
641void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500642window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400643 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500644{
645 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500646}
647
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400648void
649window_move(struct window *window, int32_t x, int32_t y)
650{
651 window->allocation.x = x;
652 window->allocation.y = y;
653
654 wl_surface_map(window->surface,
655 window->allocation.x - window->margin,
656 window->allocation.y - window->margin,
657 window->allocation.width,
658 window->allocation.height);
659}
660
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500661struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500662window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500663 int32_t x, int32_t y, int32_t width, int32_t height)
664{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500665 struct window *window;
666
667 window = malloc(sizeof *window);
668 if (window == NULL)
669 return NULL;
670
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500671 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500672 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500673 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500674 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500675 window->allocation.x = x;
676 window->allocation.y = y;
677 window->allocation.width = width;
678 window->allocation.height = height;
679 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500680 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400681 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500682
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400683 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400684 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500685
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500686 return window;
687}
688
689static void
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400690drm_handle_device(void *data, struct wl_drm *drm, const char *device)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400691{
692 struct display *d = data;
693
694 d->device_name = strdup(device);
695}
696
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400697static void drm_handle_authenticated(void *data, struct wl_drm *drm)
698{
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400699 struct display *d = data;
700
701 d->authenticated = 1;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400702}
703
704static const struct wl_drm_listener drm_listener = {
705 drm_handle_device,
706 drm_handle_authenticated
707};
708
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400709static void
710display_handle_acknowledge(void *data,
711 struct wl_compositor *compositor,
712 uint32_t key, uint32_t frame)
713{
714 struct display *d = data;
715 struct window *window;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400716
717 /* The acknowledge event means that the server processed our
718 * last commit request and we can now safely free the old
719 * window buffer if we resized and render the next frame into
720 * our back buffer.. */
721 wl_list_for_each(window, &d->window_list, link) {
Kristian Høgsberg09531622010-06-14 23:22:15 -0400722 cairo_surface_destroy(window->pending_surface);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400723 window->pending_surface = NULL;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400724 if (window->cairo_surface)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400725 window_attach_surface(window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400726 if (window->acknowledge_handler)
727 (*window->acknowledge_handler)(window, key, frame, window->user_data);
728 }
729}
730
731static void
732display_handle_frame(void *data,
733 struct wl_compositor *compositor,
734 uint32_t frame, uint32_t timestamp)
735{
736 struct display *d = data;
737 struct window *window;
738
739 wl_list_for_each(window, &d->window_list, link) {
740 if (window->frame_handler)
741 (*window->frame_handler)(window, frame,
742 timestamp, window->user_data);
743 }
744}
745
746static const struct wl_compositor_listener compositor_listener = {
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400747 display_handle_acknowledge,
748 display_handle_frame,
749};
750
751static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500752display_handle_geometry(void *data,
753 struct wl_output *output,
754 int32_t width, int32_t height)
755{
756 struct display *display = data;
757
758 display->screen_allocation.x = 0;
759 display->screen_allocation.y = 0;
760 display->screen_allocation.width = width;
761 display->screen_allocation.height = height;
762}
763
764static const struct wl_output_listener output_listener = {
765 display_handle_geometry,
766};
767
768static void
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400769display_add_input(struct display *d, struct wl_object *object)
770{
771 struct input *input;
772
773 input = malloc(sizeof *input);
774 if (input == NULL)
775 return;
776
777 memset(input, 0, sizeof *input);
778 input->display = d;
779 input->input_device = (struct wl_input_device *) object;
780 input->pointer_focus = NULL;
781 input->keyboard_focus = NULL;
782 wl_list_insert(d->input_list.prev, &input->link);
783
784 wl_input_device_add_listener(input->input_device,
785 &input_device_listener, input);
786}
787
788static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500789display_handle_global(struct wl_display *display,
790 struct wl_object *object, void *data)
791{
792 struct display *d = data;
793
794 if (wl_object_implements(object, "compositor", 1)) {
795 d->compositor = (struct wl_compositor *) object;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400796 wl_compositor_add_listener(d->compositor, &compositor_listener, d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500797 } else if (wl_object_implements(object, "output", 1)) {
798 d->output = (struct wl_output *) object;
799 wl_output_add_listener(d->output, &output_listener, d);
800 } else if (wl_object_implements(object, "input_device", 1)) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400801 display_add_input(d, object);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400802 } else if (wl_object_implements(object, "shell", 1)) {
803 d->shell = (struct wl_shell *) object;
804 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400805 } else if (wl_object_implements(object, "drm", 1)) {
806 d->drm = (struct wl_drm *) object;
807 wl_drm_add_listener(d->drm, &drm_listener, d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500808 }
809}
810
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400811static const char socket_name[] = "\0wayland";
812
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400813static void
814display_render_frame(struct display *d)
815{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400816 int radius = 8;
817 cairo_t *cr;
818
819 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
820 cr = cairo_create(d->shadow);
821 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
822 cairo_set_source_rgba(cr, 0, 0, 0, 1);
823 rounded_rect(cr, 16, 16, 112, 112, radius);
824 cairo_fill(cr);
825 cairo_destroy(cr);
826 blur_surface(d->shadow, 64);
827
828 d->active_frame =
829 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
830 cr = cairo_create(d->active_frame);
831 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
832 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
833 rounded_rect(cr, 16, 16, 112, 112, radius);
834 cairo_fill(cr);
835 cairo_destroy(cr);
836
837 d->inactive_frame =
838 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
839 cr = cairo_create(d->inactive_frame);
840 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
841 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
842 rounded_rect(cr, 16, 16, 112, 112, radius);
843 cairo_fill(cr);
844 cairo_destroy(cr);
845}
846
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400847static void
848init_xkb(struct display *d)
849{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -0400850 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400851
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -0400852 names.rules = "evdev";
853 names.model = "pc105";
854 names.layout = "us";
855 names.variant = "";
856 names.options = "";
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400857
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -0400858 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400859 if (!d->xkb) {
860 fprintf(stderr, "Failed to compile keymap\n");
861 exit(1);
862 }
863}
864
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500865struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400866display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500867{
868 struct display *d;
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400869 EGLint major, minor;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400870 int fd;
871 GOptionContext *context;
872 GError *error;
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400873 drm_magic_t magic;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400874
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400875 g_type_init();
876
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400877 context = g_option_context_new(NULL);
878 if (option_entries) {
879 g_option_context_add_main_entries(context, option_entries, "Wayland View");
880 if (!g_option_context_parse(context, argc, argv, &error)) {
881 fprintf(stderr, "option parsing failed: %s\n", error->message);
882 exit(EXIT_FAILURE);
883 }
884 }
885
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500886 d = malloc(sizeof *d);
887 if (d == NULL)
888 return NULL;
889
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400890 d->display = wl_display_create(socket_name, sizeof socket_name);
891 if (d->display == NULL) {
892 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400893 return NULL;
894 }
895
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400896 wl_list_init(&d->input_list);
897
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400898 /* Set up listener so we'll catch all events. */
899 wl_display_add_global_listener(d->display,
900 display_handle_global, d);
901
902 /* Process connection events. */
903 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
904
905 fd = open(d->device_name, O_RDWR);
906 if (fd < 0) {
907 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400908 return NULL;
909 }
910
Kristian Høgsberg640609a2010-08-09 22:11:47 -0400911 if (drmGetMagic(fd, &magic)) {
912 fprintf(stderr, "DRI2: failed to get drm magic");
913 return NULL;
914 }
915
916 /* Wait for authenticated event */
917 wl_drm_authenticate(d->drm, magic);
918 wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
919 while (!d->authenticated)
920 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
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
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400928 eglBindAPI(EGL_OPENGL_API);
929
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400930 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400931 if (d->ctx == NULL) {
932 fprintf(stderr, "failed to create context\n");
933 return NULL;
934 }
935
936 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
937 fprintf(stderr, "faile to make context current\n");
938 return NULL;
939 }
940
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400941 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsberg26449102009-05-28 20:23:31 -0400942 if (d->device == NULL) {
943 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500944 return NULL;
945 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500946
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400947 display_render_frame(d);
948
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400949 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400950 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400951 g_source_attach(d->source, NULL);
952
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400953 wl_list_init(&d->window_list);
954
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400955 init_xkb(d);
956
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500957 return d;
958}
959
960struct wl_compositor *
961display_get_compositor(struct display *display)
962{
963 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500964}
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400965
966EGLDisplay
967display_get_egl_display(struct display *d)
968{
969 return d->dpy;
970}
971
972void
973display_run(struct display *d)
974{
975 g_main_loop_run(d->loop);
976}