blob: b2c4f0332c3587a48c844b317a60bcbf404f65f0 [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øgsberg6d702022010-08-06 15:12:22 -0400403 uint32_t time, struct wl_surface *surface,
404 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500405{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400406 struct input *input = data;
407
408 if (surface)
409 input->pointer_focus = wl_surface_get_user_data(surface);
410 else
411 input->pointer_focus = NULL;
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500412}
413
414static void
415window_handle_keyboard_focus(void *data,
416 struct wl_input_device *input_device,
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400417 uint32_t time,
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -0500418 struct wl_surface *surface,
419 struct wl_array *keys)
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500420{
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400421 struct input *input = data;
422 struct window *window = input->keyboard_focus;
423 struct display *d = input->display;
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500424 uint32_t *k, *end;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500425
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400426 window = input->keyboard_focus;
427 if (window) {
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500428 window->keyboard_device = NULL;
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400429 if (window->keyboard_focus_handler)
430 (*window->keyboard_focus_handler)(window, NULL,
431 window->user_data);
Kristian Høgsberg99f090d2009-02-23 22:37:14 -0500432 }
433
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400434 if (surface)
435 input->keyboard_focus = wl_surface_get_user_data(surface);
436 else
437 input->keyboard_focus = NULL;
438
439 end = keys->data + keys->size;
440 for (k = keys->data; k < end; k++)
441 input->modifiers |= d->xkb->map->modmap[*k];
442
443 window = input->keyboard_focus;
444 if (window) {
445 window->keyboard_device = input;
446 if (window->keyboard_focus_handler)
447 (*window->keyboard_focus_handler)(window,
448 window->keyboard_device,
449 window->user_data);
450 }
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500451}
452
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500453static const struct wl_input_device_listener input_device_listener = {
454 window_handle_motion,
455 window_handle_button,
456 window_handle_key,
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -0500457 window_handle_pointer_focus,
458 window_handle_keyboard_focus,
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500459};
460
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400461static void
462handle_configure(void *data, struct wl_shell *shell,
463 uint32_t time, uint32_t edges,
464 struct wl_surface *surface,
465 int32_t x, int32_t y, int32_t width, int32_t height)
466{
467 struct window *window = wl_surface_get_user_data(surface);
468
469 window->resize_edges = edges;
470 window->pending_allocation.x = x;
471 window->pending_allocation.y = y;
472 window->pending_allocation.width = width;
473 window->pending_allocation.height = height;
474
475 if (!(edges & 15))
476 return;
477
478 if (window->resize_handler)
479 (*window->resize_handler)(window,
480 window->user_data);
481 else if (window->redraw_handler)
482 window_schedule_redraw(window);
483}
484
485static const struct wl_shell_listener shell_listener = {
486 handle_configure,
487};
488
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500489void
490window_get_child_rectangle(struct window *window,
491 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500492{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400493 if (window->fullscreen && !window->decoration) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500494 *rectangle = window->allocation;
495 } else {
496 rectangle->x = window->margin + 10;
497 rectangle->y = window->margin + 50;
498 rectangle->width = window->allocation.width - 20 - window->margin * 2;
499 rectangle->height = window->allocation.height - 60 - window->margin * 2;
500 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500501}
502
503void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500504window_set_child_size(struct window *window,
505 struct rectangle *rectangle)
506{
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400507 int32_t width, height;
508
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500509 if (!window->fullscreen) {
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400510 width = rectangle->width + 20 + window->margin * 2;
511 height = rectangle->height + 60 + window->margin * 2;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400512
513 if (window->resize_edges & WINDOW_RESIZING_LEFT)
514 window->allocation.x +=
515 window->allocation.width - width;
516 if (window->resize_edges & WINDOW_RESIZING_TOP)
517 window->allocation.y +=
518 window->allocation.height - height;
519
Kristian Høgsbergbf6ceda2010-06-14 20:25:06 -0400520 window->allocation.width = width;
521 window->allocation.height = height;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500522 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500523}
524
525void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400526window_copy_image(struct window *window,
527 struct rectangle *rectangle, EGLImageKHR image)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500528{
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400529 /* set image as read buffer, copy pixels or something... */
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500530}
531
532void
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500533window_copy_surface(struct window *window,
534 struct rectangle *rectangle,
535 cairo_surface_t *surface)
536{
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500537 cairo_t *cr;
538
539 cr = cairo_create (window->cairo_surface);
540
541 cairo_set_source_surface (cr,
542 surface,
543 rectangle->x, rectangle->y);
544
545 cairo_paint (cr);
546 cairo_destroy (cr);
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500547}
548
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400549static gboolean
550idle_redraw(void *data)
551{
552 struct window *window = data;
553
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400554 if (window->resize_edges)
555 window->allocation = window->pending_allocation;
556
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400557 window->redraw_handler(window, window->user_data);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400558
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400559 window->redraw_scheduled = 0;
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400560 window->resize_edges = 0;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400561
562 return FALSE;
563}
564
565void
566window_schedule_redraw(struct window *window)
567{
568 if (!window->redraw_scheduled) {
569 g_idle_add(idle_redraw, window);
570 window->redraw_scheduled = 1;
571 }
572}
573
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500574void
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500575window_set_fullscreen(struct window *window, int fullscreen)
576{
577 window->fullscreen = fullscreen;
578 if (window->fullscreen) {
579 window->saved_allocation = window->allocation;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500580 window->allocation = window->display->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500581 } else {
582 window->allocation = window->saved_allocation;
583 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500584}
585
586void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400587window_set_decoration(struct window *window, int decoration)
588{
589 window->decoration = decoration;
590}
591
592void
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400593window_set_user_data(struct window *window, void *data)
594{
595 window->user_data = data;
596}
597
598void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500599window_set_resize_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400600 window_resize_handler_t handler)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500601{
602 window->resize_handler = handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500603}
604
605void
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400606window_set_redraw_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400607 window_redraw_handler_t handler)
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400608{
609 window->redraw_handler = handler;
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400610}
611
612void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500613window_set_key_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400614 window_key_handler_t handler)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500615{
616 window->key_handler = handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500617}
618
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500619void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400620window_set_acknowledge_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400621 window_acknowledge_handler_t handler)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400622{
623 window->acknowledge_handler = handler;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400624}
625
626void
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400627window_set_frame_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400628 window_frame_handler_t handler)
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400629{
630 window->frame_handler = handler;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400631}
632
633void
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500634window_set_keyboard_focus_handler(struct window *window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400635 window_keyboard_focus_handler_t handler)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500636{
637 window->keyboard_focus_handler = handler;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500638}
639
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400640void
641window_move(struct window *window, int32_t x, int32_t y)
642{
643 window->allocation.x = x;
644 window->allocation.y = y;
645
646 wl_surface_map(window->surface,
647 window->allocation.x - window->margin,
648 window->allocation.y - window->margin,
649 window->allocation.width,
650 window->allocation.height);
651}
652
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500653struct window *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500654window_create(struct display *display, const char *title,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500655 int32_t x, int32_t y, int32_t width, int32_t height)
656{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500657 struct window *window;
658
659 window = malloc(sizeof *window);
660 if (window == NULL)
661 return NULL;
662
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500663 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500664 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500665 window->title = strdup(title);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500666 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500667 window->allocation.x = x;
668 window->allocation.y = y;
669 window->allocation.width = width;
670 window->allocation.height = height;
671 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500672 window->margin = 16;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400673 window->decoration = 1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500674
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400675 wl_surface_set_user_data(window->surface, window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400676 wl_list_insert(display->window_list.prev, &window->link);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500677
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500678 return window;
679}
680
681static void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400682display_handle_device(void *data,
683 struct wl_compositor *compositor,
684 const char *device)
685{
686 struct display *d = data;
687
688 d->device_name = strdup(device);
689}
690
691static void
692display_handle_acknowledge(void *data,
693 struct wl_compositor *compositor,
694 uint32_t key, uint32_t frame)
695{
696 struct display *d = data;
697 struct window *window;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400698
699 /* The acknowledge event means that the server processed our
700 * last commit request and we can now safely free the old
701 * window buffer if we resized and render the next frame into
702 * our back buffer.. */
703 wl_list_for_each(window, &d->window_list, link) {
Kristian Høgsberg09531622010-06-14 23:22:15 -0400704 cairo_surface_destroy(window->pending_surface);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400705 window->pending_surface = NULL;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400706 if (window->cairo_surface)
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400707 window_attach_surface(window);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400708 if (window->acknowledge_handler)
709 (*window->acknowledge_handler)(window, key, frame, window->user_data);
710 }
711}
712
713static void
714display_handle_frame(void *data,
715 struct wl_compositor *compositor,
716 uint32_t frame, uint32_t timestamp)
717{
718 struct display *d = data;
719 struct window *window;
720
721 wl_list_for_each(window, &d->window_list, link) {
722 if (window->frame_handler)
723 (*window->frame_handler)(window, frame,
724 timestamp, window->user_data);
725 }
726}
727
728static const struct wl_compositor_listener compositor_listener = {
729 display_handle_device,
730 display_handle_acknowledge,
731 display_handle_frame,
732};
733
734static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500735display_handle_geometry(void *data,
736 struct wl_output *output,
737 int32_t width, int32_t height)
738{
739 struct display *display = data;
740
741 display->screen_allocation.x = 0;
742 display->screen_allocation.y = 0;
743 display->screen_allocation.width = width;
744 display->screen_allocation.height = height;
745}
746
747static const struct wl_output_listener output_listener = {
748 display_handle_geometry,
749};
750
751static void
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400752display_add_input(struct display *d, struct wl_object *object)
753{
754 struct input *input;
755
756 input = malloc(sizeof *input);
757 if (input == NULL)
758 return;
759
760 memset(input, 0, sizeof *input);
761 input->display = d;
762 input->input_device = (struct wl_input_device *) object;
763 input->pointer_focus = NULL;
764 input->keyboard_focus = NULL;
765 wl_list_insert(d->input_list.prev, &input->link);
766
767 wl_input_device_add_listener(input->input_device,
768 &input_device_listener, input);
769}
770
771static void
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500772display_handle_global(struct wl_display *display,
773 struct wl_object *object, void *data)
774{
775 struct display *d = data;
776
777 if (wl_object_implements(object, "compositor", 1)) {
778 d->compositor = (struct wl_compositor *) object;
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400779 wl_compositor_add_listener(d->compositor, &compositor_listener, d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500780 } else if (wl_object_implements(object, "output", 1)) {
781 d->output = (struct wl_output *) object;
782 wl_output_add_listener(d->output, &output_listener, d);
783 } else if (wl_object_implements(object, "input_device", 1)) {
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400784 display_add_input(d, object);
Kristian Høgsberg83fc0612010-08-04 22:44:55 -0400785 } else if (wl_object_implements(object, "shell", 1)) {
786 d->shell = (struct wl_shell *) object;
787 wl_shell_add_listener(d->shell, &shell_listener, d);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500788 }
789}
790
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400791static const char socket_name[] = "\0wayland";
792
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400793static void
794display_render_frame(struct display *d)
795{
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400796 int radius = 8;
797 cairo_t *cr;
798
799 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
800 cr = cairo_create(d->shadow);
801 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
802 cairo_set_source_rgba(cr, 0, 0, 0, 1);
803 rounded_rect(cr, 16, 16, 112, 112, radius);
804 cairo_fill(cr);
805 cairo_destroy(cr);
806 blur_surface(d->shadow, 64);
807
808 d->active_frame =
809 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
810 cr = cairo_create(d->active_frame);
811 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
812 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
813 rounded_rect(cr, 16, 16, 112, 112, radius);
814 cairo_fill(cr);
815 cairo_destroy(cr);
816
817 d->inactive_frame =
818 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
819 cr = cairo_create(d->inactive_frame);
820 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
821 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
822 rounded_rect(cr, 16, 16, 112, 112, radius);
823 cairo_fill(cr);
824 cairo_destroy(cr);
825}
826
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400827static void
828init_xkb(struct display *d)
829{
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -0400830 struct xkb_rule_names names;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400831
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -0400832 names.rules = "evdev";
833 names.model = "pc105";
834 names.layout = "us";
835 names.variant = "";
836 names.options = "";
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400837
Kristian Høgsberg3e6e7e62010-07-02 15:12:02 -0400838 d->xkb = xkb_compile_keymap_from_rules(&names);
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400839 if (!d->xkb) {
840 fprintf(stderr, "Failed to compile keymap\n");
841 exit(1);
842 }
843}
844
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500845struct display *
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400846display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500847{
848 struct display *d;
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400849 EGLint major, minor;
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400850 int fd;
851 GOptionContext *context;
852 GError *error;
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400853
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400854 g_type_init();
855
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400856 context = g_option_context_new(NULL);
857 if (option_entries) {
858 g_option_context_add_main_entries(context, option_entries, "Wayland View");
859 if (!g_option_context_parse(context, argc, argv, &error)) {
860 fprintf(stderr, "option parsing failed: %s\n", error->message);
861 exit(EXIT_FAILURE);
862 }
863 }
864
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500865 d = malloc(sizeof *d);
866 if (d == NULL)
867 return NULL;
868
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400869 d->display = wl_display_create(socket_name, sizeof socket_name);
870 if (d->display == NULL) {
871 fprintf(stderr, "failed to create display: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400872 return NULL;
873 }
874
Kristian Høgsberg808fd412010-07-20 17:06:19 -0400875 wl_list_init(&d->input_list);
876
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400877 /* Set up listener so we'll catch all events. */
878 wl_display_add_global_listener(d->display,
879 display_handle_global, d);
880
881 /* Process connection events. */
882 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
883
884 fd = open(d->device_name, O_RDWR);
885 if (fd < 0) {
886 fprintf(stderr, "drm open failed: %m\n");
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400887 return NULL;
888 }
889
Kristian Høgsbergf252d6a2010-07-08 20:15:10 -0400890 d->dpy = eglGetDRMDisplayMESA(fd);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400891 if (!eglInitialize(d->dpy, &major, &minor)) {
892 fprintf(stderr, "failed to initialize display\n");
893 return NULL;
894 }
895
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400896 eglBindAPI(EGL_OPENGL_API);
897
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400898 d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400899 if (d->ctx == NULL) {
900 fprintf(stderr, "failed to create context\n");
901 return NULL;
902 }
903
904 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
905 fprintf(stderr, "faile to make context current\n");
906 return NULL;
907 }
908
Kristian Høgsberga85fe3c2010-06-08 14:08:30 -0400909 d->device = cairo_egl_device_create(d->dpy, d->ctx);
Kristian Høgsberg26449102009-05-28 20:23:31 -0400910 if (d->device == NULL) {
911 fprintf(stderr, "failed to get cairo drm device\n");
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500912 return NULL;
913 }
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500914
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400915 display_render_frame(d);
916
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400917 d->loop = g_main_loop_new(NULL, FALSE);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400918 d->source = wl_glib_source_new(d->display);
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400919 g_source_attach(d->source, NULL);
920
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400921 wl_list_init(&d->window_list);
922
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400923 init_xkb(d);
924
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500925 return d;
926}
927
928struct wl_compositor *
929display_get_compositor(struct display *display)
930{
931 return display->compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500932}
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400933
934EGLDisplay
935display_get_egl_display(struct display *d)
936{
937 return d->dpy;
938}
939
940void
941display_run(struct display *d)
942{
943 g_main_loop_run(d->loop);
944}