blob: c01ca05bccdef65acbfad1b309b7a49abfb32906 [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øgsberg83fc0612010-08-04 22:44:55 -040055 struct wl_shell *shell;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050056 struct wl_output *output;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050057 struct rectangle screen_allocation;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -040058 EGLDisplay dpy;
59 EGLContext ctx;
Janusz Lewandowskid923e9d2010-01-31 03:01:26 +010060 cairo_device_t *device;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050061 int fd;
Kristian Høgsberg7824d812010-06-08 14:59:44 -040062 GMainLoop *loop;
63 GSource *source;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040064 struct wl_list window_list;
Kristian Høgsberg808fd412010-07-20 17:06:19 -040065 struct wl_list input_list;
Kristian Høgsberg478d9262010-06-08 20:34:11 -040066 char *device_name;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -040067 cairo_surface_t *active_frame, *inactive_frame, *shadow;
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -040068 struct xkb_desc *xkb;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050069};
70
71struct window {
72 struct display *display;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050073 struct wl_surface *surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050074 const char *title;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -040075 struct rectangle allocation, saved_allocation, pending_allocation;
76 int resize_edges;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -040077 int redraw_scheduled;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050078 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050079 int margin;
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øgsberg83fc0612010-08-04 22:44:55 -0400198 window->allocation.width,
199 window->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øgsberg83fc0612010-08-04 22:44:55 -0400204 window->allocation.x,
205 window->allocation.y,
206 window->allocation.width,
207 window->allocation.height);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400208
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øgsbergdcb71b62010-06-15 17:16:35 -0400232 width = window->allocation.width;
233 height = window->allocation.height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500234
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500235 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500236 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500237 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
238
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500239 cr = cairo_create(window->cairo_surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500240
Kristian Høgsberg09531622010-06-14 23:22:15 -0400241 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400242 cairo_set_source_rgba(cr, 0, 0, 0, 0);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400243 cairo_paint(cr);
244
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400245 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
246 tile_mask(cr, window->display->shadow, 3, 3, width, height, 32);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500247
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400248 if (window->keyboard_device)
249 tile_source(cr, window->display->active_frame,
250 0, 0, width, height, 96);
251 else
252 tile_source(cr, window->display->inactive_frame,
253 0, 0, width, height, 96);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500254
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500255 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
256 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500257 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400258 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500259 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
260 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
261 cairo_set_line_width (cr, 4);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400262 if (window->keyboard_device)
263 cairo_set_source_rgb(cr, 0, 0, 0);
264 else
Kristian Høgsberg7d7b5db2009-09-21 13:43:46 -0400265 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400266 cairo_show_text(cr, window->title);
267
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500268 cairo_destroy(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500269}
270
271static void
272window_draw_fullscreen(struct window *window)
273{
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500274 window->cairo_surface =
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400275 display_create_surface(window->display, &window->allocation);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500276}
277
278void
279window_draw(struct window *window)
280{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500281 if (window->cairo_surface != NULL)
282 cairo_surface_destroy(window->cairo_surface);
283
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400284 if (window->fullscreen || !window->decoration)
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500285 window_draw_fullscreen(window);
286 else
287 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500288}
289
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400290cairo_surface_t *
291window_get_surface(struct window *window)
292{
293 return window->cairo_surface;
294}
295
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500296enum window_state {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400297 WINDOW_MOVING = 0,
298 WINDOW_RESIZING_TOP = 1,
299 WINDOW_RESIZING_BOTTOM = 2,
300 WINDOW_RESIZING_LEFT = 4,
301 WINDOW_RESIZING_TOP_LEFT = 5,
302 WINDOW_RESIZING_BOTTOM_LEFT = 6,
303 WINDOW_RESIZING_RIGHT = 8,
304 WINDOW_RESIZING_TOP_RIGHT = 9,
305 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
306 WINDOW_RESIZING_MASK = 15,
307 WINDOW_STABLE = 16,
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500308};
309
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500310static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500311window_handle_motion(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400312 uint32_t time,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500313 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500314{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400315 struct input *input = data;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400316
317 input->x = x;
318 input->y = y;
319 input->sx = sx;
320 input->sy = sy;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500321}
322
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500323static void window_handle_button(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400324 uint32_t time, uint32_t button, uint32_t state)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500325{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400326 struct input *input = data;
327 struct window *window = input->pointer_focus;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400328 int grip_size = 8, vlocation, hlocation;
329
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400330 if (window->margin <= input->sx && input->sx < window->margin + grip_size)
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400331 hlocation = WINDOW_RESIZING_LEFT;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400332 else if (input->sx < window->allocation.width - window->margin - grip_size)
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400333 hlocation = WINDOW_MOVING;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400334 else if (input->sx < window->allocation.width - window->margin)
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400335 hlocation = WINDOW_RESIZING_RIGHT;
336 else
337 hlocation = WINDOW_STABLE;
338
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400339 if (window->margin <= input->sy && input->sy < window->margin + grip_size)
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400340 vlocation = WINDOW_RESIZING_TOP;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400341 else if (input->sy < window->allocation.height - window->margin - grip_size)
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400342 vlocation = WINDOW_MOVING;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400343 else if (input->sy < window->allocation.height - window->margin)
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400344 vlocation = WINDOW_RESIZING_BOTTOM;
345 else
346 vlocation = WINDOW_STABLE;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500347
348 if (button == BTN_LEFT && state == 1) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400349 switch (hlocation | vlocation) {
350 case WINDOW_MOVING:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400351 wl_shell_move(window->display->shell,
352 window->surface, input_device, time);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500353 break;
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400354 case WINDOW_RESIZING_TOP:
355 case WINDOW_RESIZING_BOTTOM:
356 case WINDOW_RESIZING_LEFT:
357 case WINDOW_RESIZING_RIGHT:
358 case WINDOW_RESIZING_TOP_LEFT:
359 case WINDOW_RESIZING_TOP_RIGHT:
360 case WINDOW_RESIZING_BOTTOM_LEFT:
361 case WINDOW_RESIZING_BOTTOM_RIGHT:
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400362 wl_shell_resize(window->display->shell,
363 window->surface, input_device, time,
364 hlocation | vlocation);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500365 break;
366 }
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500367 }
368}
369
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500370static void
371window_handle_key(void *data, struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400372 uint32_t time, uint32_t key, uint32_t state)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500373{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400374 struct input *input = data;
375 struct window *window = input->keyboard_focus;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400376 struct display *d = window->display;
377 uint32_t code, sym, level;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500378
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400379 code = key + d->xkb->min_key_code;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400380 if (window->keyboard_device != input)
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500381 return;
382
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400383 level = 0;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400384 if (input->modifiers & WINDOW_MODIFIER_SHIFT &&
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400385 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
386 level = 1;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500387
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400388 sym = XkbKeySymEntry(d->xkb, code, level, 0);
389
390 if (state)
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400391 input->modifiers |= d->xkb->map->modmap[code];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400392 else
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400393 input->modifiers &= ~d->xkb->map->modmap[code];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500394
395 if (window->key_handler)
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400396 (*window->key_handler)(window, key, sym, state,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400397 input->modifiers, window->user_data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500398}
399
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500400static void
401window_handle_pointer_focus(void *data,
402 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400403 uint32_t time, struct wl_surface *surface)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500404{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400405 struct input *input = data;
406
407 if (surface)
408 input->pointer_focus = wl_surface_get_user_data(surface);
409 else
410 input->pointer_focus = NULL;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500411}
412
413static void
414window_handle_keyboard_focus(void *data,
415 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400416 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500417 struct wl_surface *surface,
418 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500419{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400420 struct input *input = data;
421 struct window *window = input->keyboard_focus;
422 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500423 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500424
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400425 window = input->keyboard_focus;
426 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500427 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400428 if (window->keyboard_focus_handler)
429 (*window->keyboard_focus_handler)(window, NULL,
430 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500431 }
432
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400433 if (surface)
434 input->keyboard_focus = wl_surface_get_user_data(surface);
435 else
436 input->keyboard_focus = NULL;
437
438 end = keys->data + keys->size;
439 for (k = keys->data; k < end; k++)
440 input->modifiers |= d->xkb->map->modmap[*k];
441
442 window = input->keyboard_focus;
443 if (window) {
444 window->keyboard_device = input;
445 if (window->keyboard_focus_handler)
446 (*window->keyboard_focus_handler)(window,
447 window->keyboard_device,
448 window->user_data);
449 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500450}
451
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500452static const struct wl_input_device_listener input_device_listener = {
453 window_handle_motion,
454 window_handle_button,
455 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500456 window_handle_pointer_focus,
457 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500458};
459
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400460static void
461handle_configure(void *data, struct wl_shell *shell,
462 uint32_t time, uint32_t edges,
463 struct wl_surface *surface,
464 int32_t x, int32_t y, int32_t width, int32_t height)
465{
466 struct window *window = wl_surface_get_user_data(surface);
467
468 window->resize_edges = edges;
469 window->pending_allocation.x = x;
470 window->pending_allocation.y = y;
471 window->pending_allocation.width = width;
472 window->pending_allocation.height = height;
473
474 if (!(edges & 15))
475 return;
476
477 if (window->resize_handler)
478 (*window->resize_handler)(window,
479 window->user_data);
480 else if (window->redraw_handler)
481 window_schedule_redraw(window);
482}
483
484static const struct wl_shell_listener shell_listener = {
485 handle_configure,
486};
487
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500488void
489window_get_child_rectangle(struct window *window,
490 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500491{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400492 if (window->fullscreen && !window->decoration) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500493 *rectangle = window->allocation;
494 } else {
495 rectangle->x = window->margin + 10;
496 rectangle->y = window->margin + 50;
497 rectangle->width = window->allocation.width - 20 - window->margin * 2;
498 rectangle->height = window->allocation.height - 60 - window->margin * 2;
499 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500500}
501
502void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500503window_set_child_size(struct window *window,
504 struct rectangle *rectangle)
505{
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400506 int32_t width, height;
507
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500508 if (!window->fullscreen) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400509 width = rectangle->width + 20 + window->margin * 2;
510 height = rectangle->height + 60 + window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400511
512 if (window->resize_edges & WINDOW_RESIZING_LEFT)
513 window->allocation.x +=
514 window->allocation.width - width;
515 if (window->resize_edges & WINDOW_RESIZING_TOP)
516 window->allocation.y +=
517 window->allocation.height - height;
518
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400519 window->allocation.width = width;
520 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500521 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500522}
523
524void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400525window_copy_image(struct window *window,
526 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500527{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400528 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500529}
530
531void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500532window_copy_surface(struct window *window,
533 struct rectangle *rectangle,
534 cairo_surface_t *surface)
535{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500536 cairo_t *cr;
537
538 cr = cairo_create (window->cairo_surface);
539
540 cairo_set_source_surface (cr,
541 surface,
542 rectangle->x, rectangle->y);
543
544 cairo_paint (cr);
545 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500546}
547
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400548static gboolean
549idle_redraw(void *data)
550{
551 struct window *window = data;
552
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400553 if (window->resize_edges)
554 window->allocation = window->pending_allocation;
555
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400556 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400557
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400558 window->redraw_scheduled = 0;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400559 window->resize_edges = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400560
561 return FALSE;
562}
563
564void
565window_schedule_redraw(struct window *window)
566{
567 if (!window->redraw_scheduled) {
568 g_idle_add(idle_redraw, window);
569 window->redraw_scheduled = 1;
570 }
571}
572
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500573void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500574window_set_fullscreen(struct window *window, int fullscreen)
575{
576 window->fullscreen = fullscreen;
577 if (window->fullscreen) {
578 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500579 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500580 } else {
581 window->allocation = window->saved_allocation;
582 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500583}
584
585void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400586window_set_decoration(struct window *window, int decoration)
587{
588 window->decoration = decoration;
589}
590
591void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400592window_set_user_data(struct window *window, void *data)
593{
594 window->user_data = data;
595}
596
597void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500598window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400599 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500600{
601 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500602}
603
604void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400605window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400606 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400607{
608 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400609}
610
611void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500612window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400613 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500614{
615 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500616}
617
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500618void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400619window_set_acknowledge_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400620 window_acknowledge_handler_t handler)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400621{
622 window->acknowledge_handler = handler;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400623}
624
625void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400626window_set_frame_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400627 window_frame_handler_t handler)
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400628{
629 window->frame_handler = handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400630}
631
632void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500633window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400634 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500635{
636 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500637}
638
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400639void
640window_move(struct window *window, int32_t x, int32_t y)
641{
642 window->allocation.x = x;
643 window->allocation.y = y;
644
645 wl_surface_map(window->surface,
646 window->allocation.x - window->margin,
647 window->allocation.y - window->margin,
648 window->allocation.width,
649 window->allocation.height);
650}
651
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500652struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500653window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500654 int32_t x, int32_t y, int32_t width, int32_t height)
655{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500656 struct window *window;
657
658 window = malloc(sizeof *window);
659 if (window == NULL)
660 return NULL;
661
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500662 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500663 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500664 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500665 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500666 window->allocation.x = x;
667 window->allocation.y = y;
668 window->allocation.width = width;
669 window->allocation.height = height;
670 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500671 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400672 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500673
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400674 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400675 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500676
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500677 return window;
678}
679
680static void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400681display_handle_device(void *data,
682 struct wl_compositor *compositor,
683 const char *device)
684{
685 struct display *d = data;
686
687 d->device_name = strdup(device);
688}
689
690static void
691display_handle_acknowledge(void *data,
692 struct wl_compositor *compositor,
693 uint32_t key, uint32_t frame)
694{
695 struct display *d = data;
696 struct window *window;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400697
698 /* The acknowledge event means that the server processed our
699 * last commit request and we can now safely free the old
700 * window buffer if we resized and render the next frame into
701 * our back buffer.. */
702 wl_list_for_each(window, &d->window_list, link) {
Kristian Høgsberg09531622010-06-14 23:22:15 -0400703 cairo_surface_destroy(window->pending_surface);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400704 window->pending_surface = NULL;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400705 if (window->cairo_surface)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400706 window_attach_surface(window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400707 if (window->acknowledge_handler)
708 (*window->acknowledge_handler)(window, key, frame, window->user_data);
709 }
710}
711
712static void
713display_handle_frame(void *data,
714 struct wl_compositor *compositor,
715 uint32_t frame, uint32_t timestamp)
716{
717 struct display *d = data;
718 struct window *window;
719
720 wl_list_for_each(window, &d->window_list, link) {
721 if (window->frame_handler)
722 (*window->frame_handler)(window, frame,
723 timestamp, window->user_data);
724 }
725}
726
727static const struct wl_compositor_listener compositor_listener = {
728 display_handle_device,
729 display_handle_acknowledge,
730 display_handle_frame,
731};
732
733static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500734display_handle_geometry(void *data,
735 struct wl_output *output,
736 int32_t width, int32_t height)
737{
738 struct display *display = data;
739
740 display->screen_allocation.x = 0;
741 display->screen_allocation.y = 0;
742 display->screen_allocation.width = width;
743 display->screen_allocation.height = height;
744}
745
746static const struct wl_output_listener output_listener = {
747 display_handle_geometry,
748};
749
750static void
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400751display_add_input(struct display *d, struct wl_object *object)
752{
753 struct input *input;
754
755 input = malloc(sizeof *input);
756 if (input == NULL)
757 return;
758
759 memset(input, 0, sizeof *input);
760 input->display = d;
761 input->input_device = (struct wl_input_device *) object;
762 input->pointer_focus = NULL;
763 input->keyboard_focus = NULL;
764 wl_list_insert(d->input_list.prev, &input->link);
765
766 wl_input_device_add_listener(input->input_device,
767 &input_device_listener, input);
768}
769
770static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500771display_handle_global(struct wl_display *display,
772 struct wl_object *object, void *data)
773{
774 struct display *d = data;
775
776 if (wl_object_implements(object, "compositor", 1)) {
777 d->compositor = (struct wl_compositor *) object;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400778 wl_compositor_add_listener(d->compositor, &compositor_listener, d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500779 } else if (wl_object_implements(object, "output", 1)) {
780 d->output = (struct wl_output *) object;
781 wl_output_add_listener(d->output, &output_listener, d);
782 } else if (wl_object_implements(object, "input_device", 1)) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400783 display_add_input(d, object);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400784 } else if (wl_object_implements(object, "shell", 1)) {
785 d->shell = (struct wl_shell *) object;
786 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500787 }
788}
789
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400790static const char socket_name[] = "\0wayland";
791
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400792static void
793display_render_frame(struct display *d)
794{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400795 int radius = 8;
796 cairo_t *cr;
797
798 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
799 cr = cairo_create(d->shadow);
800 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
801 cairo_set_source_rgba(cr, 0, 0, 0, 1);
802 rounded_rect(cr, 16, 16, 112, 112, radius);
803 cairo_fill(cr);
804 cairo_destroy(cr);
805 blur_surface(d->shadow, 64);
806
807 d->active_frame =
808 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
809 cr = cairo_create(d->active_frame);
810 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
811 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
812 rounded_rect(cr, 16, 16, 112, 112, radius);
813 cairo_fill(cr);
814 cairo_destroy(cr);
815
816 d->inactive_frame =
817 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
818 cr = cairo_create(d->inactive_frame);
819 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
820 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
821 rounded_rect(cr, 16, 16, 112, 112, radius);
822 cairo_fill(cr);
823 cairo_destroy(cr);
824}
825
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400826static void
827init_xkb(struct display *d)
828{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -0400829 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400830
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -0400831 names.rules = "evdev";
832 names.model = "pc105";
833 names.layout = "us";
834 names.variant = "";
835 names.options = "";
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400836
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -0400837 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400838 if (!d->xkb) {
839 fprintf(stderr, "Failed to compile keymap\n");
840 exit(1);
841 }
842}
843
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500844struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400845display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500846{
847 struct display *d;
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400848 EGLint major, minor;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400849 int fd;
850 GOptionContext *context;
851 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400852
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400853 g_type_init();
854
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400855 context = g_option_context_new(NULL);
856 if (option_entries) {
857 g_option_context_add_main_entries(context, option_entries, "Wayland View");
858 if (!g_option_context_parse(context, argc, argv, &error)) {
859 fprintf(stderr, "option parsing failed: %s\n", error->message);
860 exit(EXIT_FAILURE);
861 }
862 }
863
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500864 d = malloc(sizeof *d);
865 if (d == NULL)
866 return NULL;
867
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400868 d->display = wl_display_create(socket_name, sizeof socket_name);
869 if (d->display == NULL) {
870 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400871 return NULL;
872 }
873
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400874 wl_list_init(&d->input_list);
875
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400876 /* Set up listener so we'll catch all events. */
877 wl_display_add_global_listener(d->display,
878 display_handle_global, d);
879
880 /* Process connection events. */
881 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
882
883 fd = open(d->device_name, O_RDWR);
884 if (fd < 0) {
885 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400886 return NULL;
887 }
888
Kristian Høgsbergf252d6a2010-07-08 20:15:10 -0400889 d->dpy = eglGetDRMDisplayMESA(fd);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400890 if (!eglInitialize(d->dpy, &major, &minor)) {
891 fprintf(stderr, "failed to initialize display\n");
892 return NULL;
893 }
894
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400895 eglBindAPI(EGL_OPENGL_API);
896
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400897 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400898 if (d->ctx == NULL) {
899 fprintf(stderr, "failed to create context\n");
900 return NULL;
901 }
902
903 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
904 fprintf(stderr, "faile to make context current\n");
905 return NULL;
906 }
907
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400908 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsberg26449102009-05-28 20:23:31 -0400909 if (d->device == NULL) {
910 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500911 return NULL;
912 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500913
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400914 display_render_frame(d);
915
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400916 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400917 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400918 g_source_attach(d->source, NULL);
919
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400920 wl_list_init(&d->window_list);
921
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400922 init_xkb(d);
923
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500924 return d;
925}
926
927struct wl_compositor *
928display_get_compositor(struct display *display)
929{
930 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500931}
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400932
933EGLDisplay
934display_get_egl_display(struct display *d)
935{
936 return d->dpy;
937}
938
939void
940display_run(struct display *d)
941{
942 g_main_loop_run(d->loop);
943}