blob: a661f40400652ab7c8066c28d3bf045efe6de0a2 [file] [log] [blame]
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Benjamin Franzke
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01003 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04004 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010013 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040014 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010021 */
22
Daniel Stonec228e232013-05-22 18:03:19 +030023#include "config.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010024
25#include <stddef.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <fcntl.h>
30#include <unistd.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010031#include <sys/mman.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010032
Benjamin Franzkebe014562011-02-18 17:04:24 +010033#include <wayland-client.h>
34#include <wayland-egl.h>
Jason Ekstrand7744f712013-10-27 22:24:55 -050035#include <wayland-cursor.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010036
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010037#include "compositor.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010038#include "gl-renderer.h"
Kristian Høgsberg3c2360f2013-01-28 16:01:22 -050039#include "../shared/image-loader.h"
Jonas Ådahle5a12252013-04-05 23:07:11 +020040#include "../shared/os-compatibility.h"
Jason Ekstrand7744f712013-10-27 22:24:55 -050041#include "../shared/cairo-util.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010042
43struct wayland_compositor {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050044 struct weston_compositor base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010045
46 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040047 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040048 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010049 struct wl_compositor *compositor;
50 struct wl_shell *shell;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010051 struct wl_output *output;
Jonas Ådahle5a12252013-04-05 23:07:11 +020052 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010053
54 struct {
55 int32_t x, y, width, height;
56 } screen_allocation;
57
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010058 struct wl_event_source *wl_source;
59 uint32_t event_mask;
60 } parent;
61
Jason Ekstrand7744f712013-10-27 22:24:55 -050062 struct theme *theme;
63 cairo_device_t *frame_device;
64 struct wl_cursor_theme *cursor_theme;
65 struct wl_cursor *cursor;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050066
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050067 struct wl_list inputs;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010068};
69
70struct wayland_output {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050071 struct weston_output base;
72
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010073 struct {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050074 int draw_initial_frame;
75 struct wl_surface *surface;
76 struct wl_shell_surface *shell_surface;
77 struct wl_egl_window *egl_window;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010078 } parent;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050079
Jason Ekstrand7744f712013-10-27 22:24:55 -050080 int keyboard_count;
81
82 struct frame *frame;
83 struct {
84 cairo_surface_t *top;
85 cairo_surface_t *left;
86 cairo_surface_t *right;
87 cairo_surface_t *bottom;
88 } border;
89
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050090 struct weston_mode mode;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010091};
92
93struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -040094 struct weston_seat base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010095 struct wayland_compositor *compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010096 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050097
98 struct {
99 struct wl_seat *seat;
100 struct wl_pointer *pointer;
101 struct wl_keyboard *keyboard;
102 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500103
104 struct {
105 struct wl_surface *surface;
106 int32_t hx, hy;
107 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500108 } parent;
109
Daniel Stone50692802012-06-22 13:21:41 +0100110 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400111 uint32_t enter_serial;
112 int focus;
113 struct wayland_output *output;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500114 struct wayland_output *keyboard_focus;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100115};
116
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300117struct gl_renderer_interface *gl_renderer;
118
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500119static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500120frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400121{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500122 struct weston_output *output = data;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400123
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500124 wl_callback_destroy(callback);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500125 weston_output_finish_frame(output, time);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400126}
127
128static const struct wl_callback_listener frame_listener = {
129 frame_done
130};
131
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500132static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200133buffer_release(void *data, struct wl_buffer *buffer)
134{
135 wl_buffer_destroy(buffer);
136}
137
138static const struct wl_buffer_listener buffer_listener = {
139 buffer_release
140};
141
142static void
143draw_initial_frame(struct wayland_output *output)
144{
145 struct wayland_compositor *c =
146 (struct wayland_compositor *) output->base.compositor;
147 struct wl_shm *shm = c->parent.shm;
148 struct wl_surface *surface = output->parent.surface;
149 struct wl_shm_pool *pool;
150 struct wl_buffer *buffer;
151
152 int width, height, stride;
153 int size;
154 int fd;
155 void *data;
156
Jason Ekstrand7744f712013-10-27 22:24:55 -0500157 if (output->frame) {
158 width = frame_width(output->frame);
159 height = frame_height(output->frame);
160 } else {
161 width = output->mode.width;
162 height = output->mode.height;
163 }
164
Jonas Ådahle5a12252013-04-05 23:07:11 +0200165 stride = width * 4;
166 size = height * stride;
167
168 fd = os_create_anonymous_file(size);
169 if (fd < 0) {
170 perror("os_create_anonymous_file");
171 return;
172 }
173
174 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
175 if (data == MAP_FAILED) {
176 perror("mmap");
177 close(fd);
178 return;
179 }
180
181 pool = wl_shm_create_pool(shm, fd, size);
182
183 buffer = wl_shm_pool_create_buffer(pool, 0,
184 width, height,
185 stride,
186 WL_SHM_FORMAT_ARGB8888);
187 wl_buffer_add_listener(buffer, &buffer_listener, buffer);
188 wl_shm_pool_destroy(pool);
189 close(fd);
190
191 memset(data, 0, size);
192
193 wl_surface_attach(surface, buffer, 0, 0);
194
195 /* We only need to damage some part, as its only transparant
196 * pixels anyway. */
197 wl_surface_damage(surface, 0, 0, 1, 1);
198}
199
200static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500201wayland_output_update_gl_border(struct wayland_output *output)
202{
203 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
204 cairo_t *cr;
205
206 if (!output->frame)
207 return;
208 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
209 return;
210
211 fwidth = frame_width(output->frame);
212 fheight = frame_height(output->frame);
213 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
214
215 if (!output->border.top)
216 output->border.top =
217 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
218 fwidth, iy);
219 cr = cairo_create(output->border.top);
220 frame_repaint(output->frame, cr);
221 cairo_destroy(cr);
222 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
223 fwidth, iy,
224 cairo_image_surface_get_stride(output->border.top) / 4,
225 cairo_image_surface_get_data(output->border.top));
226
227
228 if (!output->border.left)
229 output->border.left =
230 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
231 ix, 1);
232 cr = cairo_create(output->border.left);
233 cairo_translate(cr, 0, -iy);
234 frame_repaint(output->frame, cr);
235 cairo_destroy(cr);
236 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
237 ix, 1,
238 cairo_image_surface_get_stride(output->border.left) / 4,
239 cairo_image_surface_get_data(output->border.left));
240
241
242 if (!output->border.right)
243 output->border.right =
244 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
245 fwidth - (ix + iwidth), 1);
246 cr = cairo_create(output->border.right);
247 cairo_translate(cr, -(iwidth + ix), -iy);
248 frame_repaint(output->frame, cr);
249 cairo_destroy(cr);
250 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
251 fwidth - (ix + iwidth), 1,
252 cairo_image_surface_get_stride(output->border.right) / 4,
253 cairo_image_surface_get_data(output->border.right));
254
255
256 if (!output->border.bottom)
257 output->border.bottom =
258 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
259 fwidth, fheight - (iy + iheight));
260 cr = cairo_create(output->border.bottom);
261 cairo_translate(cr, 0, -(iy + iheight));
262 frame_repaint(output->frame, cr);
263 cairo_destroy(cr);
264 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
265 fwidth, fheight - (iy + iheight),
266 cairo_image_surface_get_stride(output->border.bottom) / 4,
267 cairo_image_surface_get_data(output->border.bottom));
268}
269
270static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200271wayland_output_start_repaint_loop(struct weston_output *output_base)
272{
273 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrand286ff682013-10-27 22:24:57 -0500274 struct wayland_compositor *wc =
275 (struct wayland_compositor *)output->base.compositor;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200276 struct wl_callback *callback;
277
278 /* If this is the initial frame, we need to attach a buffer so that
279 * the compositor can map the surface and include it in its render
280 * loop. If the surface doesn't end up in the render loop, the frame
281 * callback won't be invoked. The buffer is transparent and of the
282 * same size as the future real output buffer. */
283 if (output->parent.draw_initial_frame) {
284 output->parent.draw_initial_frame = 0;
285
286 draw_initial_frame(output);
287 }
288
289 callback = wl_surface_frame(output->parent.surface);
290 wl_callback_add_listener(callback, &frame_listener, output);
291 wl_surface_commit(output->parent.surface);
Jason Ekstrand286ff682013-10-27 22:24:57 -0500292 wl_display_flush(wc->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200293}
294
David Herrmann1edf44c2013-10-22 17:11:26 +0200295static int
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400296wayland_output_repaint(struct weston_output *output_base,
297 pixman_region32_t *damage)
298{
299 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400300 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400301 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600302
Kristian Høgsberg33418202011-08-16 23:01:28 -0400303 callback = wl_surface_frame(output->parent.surface);
304 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100305
Jason Ekstrand7744f712013-10-27 22:24:55 -0500306 wayland_output_update_gl_border(output);
307
Pekka Paalanenbc106382012-10-10 12:49:31 +0300308 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200309
310 pixman_region32_subtract(&ec->primary_plane.damage,
311 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200312 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100313}
314
Matt Roper361d2ad2011-08-29 13:52:23 -0700315static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500316wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700317{
318 struct wayland_output *output = (struct wayland_output *) output_base;
Matt Roper361d2ad2011-08-29 13:52:23 -0700319
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300320 gl_renderer->output_destroy(output_base);
John Kåre Alsaker94659272012-11-13 19:10:18 +0100321
Matt Roper361d2ad2011-08-29 13:52:23 -0700322 wl_egl_window_destroy(output->parent.egl_window);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500323 wl_surface_destroy(output->parent.surface);
324 wl_shell_surface_destroy(output->parent.shell_surface);
325
326 if (output->frame) {
327 frame_destroy(output->frame);
328 cairo_surface_destroy(output->border.top);
329 cairo_surface_destroy(output->border.left);
330 cairo_surface_destroy(output->border.right);
331 cairo_surface_destroy(output->border.bottom);
332 }
333
334 weston_output_destroy(&output->base);
Matt Roper361d2ad2011-08-29 13:52:23 -0700335 free(output);
336
337 return;
338}
339
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300340static const struct wl_shell_surface_listener shell_surface_listener;
341
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200342static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100343wayland_compositor_create_output(struct wayland_compositor *c,
344 int width, int height)
345{
346 struct wayland_output *output;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500347 int32_t fx, fy, fwidth, fheight;
348 struct wl_region *region;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100349
Peter Huttererf3d62272013-08-08 11:57:05 +1000350 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100351 if (output == NULL)
352 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100353
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400354 output->mode.flags =
355 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
356 output->mode.width = width;
357 output->mode.height = height;
358 output->mode.refresh = 60;
359 wl_list_init(&output->base.mode_list);
360 wl_list_insert(&output->base.mode_list, &output->mode.link);
361
Hardeningff39efa2013-09-18 23:56:35 +0200362 output->base.current_mode = &output->mode;
Scott Moreau1bad5db2012-08-18 01:04:05 -0600363 weston_output_init(&output->base, &c->base, 0, 0, width, height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200364 WL_OUTPUT_TRANSFORM_NORMAL, 1);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400365
Kristian Høgsberg90bc88c2012-08-13 23:34:04 -0400366 output->base.make = "waywayland";
367 output->base.model = "none";
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500368
369 weston_output_move(&output->base, 0, 0);
370
Jason Ekstrand7744f712013-10-27 22:24:55 -0500371 if (!c->theme)
372 c->theme = theme_create();
373 output->frame = frame_create(c->theme, width, height,
374 FRAME_BUTTON_CLOSE, "Weston");
375 frame_resize_inside(output->frame, width, height);
376 frame_interior(output->frame, &fx, &fy, NULL, NULL);
377 fwidth = frame_width(output->frame);
378 fheight = frame_height(output->frame);
379
380 weston_log("Creating %dx%d wayland output (%dx%d actual)\n",
381 width, height, fwidth, fheight);
382
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100383 output->parent.surface =
384 wl_compositor_create_surface(c->parent.compositor);
385 wl_surface_set_user_data(output->parent.surface, output);
386
Benjamin Franzkebe014562011-02-18 17:04:24 +0100387 output->parent.egl_window =
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500388 wl_egl_window_create(output->parent.surface,
Jason Ekstrand7744f712013-10-27 22:24:55 -0500389 fwidth, fheight);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100390 if (!output->parent.egl_window) {
Martin Minarik6d118362012-06-07 18:01:59 +0200391 weston_log("failure to create wl_egl_window\n");
Benjamin Franzkebe014562011-02-18 17:04:24 +0100392 goto cleanup_output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100393 }
394
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300395 if (gl_renderer->output_create(&output->base,
John Kåre Alsaker94659272012-11-13 19:10:18 +0100396 output->parent.egl_window) < 0)
Benjamin Franzkebe014562011-02-18 17:04:24 +0100397 goto cleanup_window;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100398
Jason Ekstrand7744f712013-10-27 22:24:55 -0500399 output->base.border.left = fx;
400 output->base.border.top = fy;
401 output->base.border.right = fwidth - width - fx;
402 output->base.border.bottom = fheight - height - fy;
403
404 frame_input_rect(output->frame, &fx, &fy, &fwidth, &fheight);
405 region = wl_compositor_create_region(c->parent.compositor);
406 wl_region_add(region, fx, fy, fwidth, fheight);
407 wl_surface_set_input_region(output->parent.surface, region);
408 wl_region_destroy(region);
409
410 frame_opaque_rect(output->frame, &fx, &fy, &fwidth, &fheight);
411 region = wl_compositor_create_region(c->parent.compositor);
412 wl_region_add(region, fx, fy, fwidth, fheight);
413 wl_surface_set_opaque_region(output->parent.surface, region);
414 wl_region_destroy(region);
415
Jonas Ådahle5a12252013-04-05 23:07:11 +0200416 output->parent.draw_initial_frame = 1;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200417 output->parent.shell_surface =
418 wl_shell_get_shell_surface(c->parent.shell,
419 output->parent.surface);
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300420 wl_shell_surface_add_listener(output->parent.shell_surface,
421 &shell_surface_listener, output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200422 wl_shell_surface_set_toplevel(output->parent.shell_surface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100423
Jonas Ådahle5a12252013-04-05 23:07:11 +0200424 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500425 output->base.repaint = wayland_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -0700426 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800427 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200428 output->base.set_backlight = NULL;
429 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800430 output->base.switch_mode = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100431
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100432 wl_list_insert(c->base.output_list.prev, &output->base.link);
433
434 return 0;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100435
Benjamin Franzkebe014562011-02-18 17:04:24 +0100436cleanup_window:
437 wl_egl_window_destroy(output->parent.egl_window);
438cleanup_output:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500439 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +0100440 free(output);
441
442 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100443}
444
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300445static void
446shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
447 uint32_t serial)
448{
449 wl_shell_surface_pong(shell_surface, serial);
450}
451
452static void
453shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
454 uint32_t edges, int32_t width, int32_t height)
455{
456 /* FIXME: implement resizing */
457}
458
459static void
460shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
461{
462}
463
464static const struct wl_shell_surface_listener shell_surface_listener = {
465 shell_surface_ping,
466 shell_surface_configure,
467 shell_surface_popup_done
468};
469
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100470/* Events received from the wayland-server this compositor is client of: */
471
472/* parent output interface */
473static void
474display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400475 struct wl_output *wl_output,
476 int x,
477 int y,
478 int physical_width,
479 int physical_height,
480 int subpixel,
481 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -0400482 const char *model,
483 int transform)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100484{
485 struct wayland_compositor *c = data;
486
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500487 c->parent.screen_allocation.x = x;
488 c->parent.screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400489}
490
491static void
492display_handle_mode(void *data,
493 struct wl_output *wl_output,
494 uint32_t flags,
495 int width,
496 int height,
497 int refresh)
498{
499 struct wayland_compositor *c = data;
500
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100501 c->parent.screen_allocation.width = width;
502 c->parent.screen_allocation.height = height;
503}
504
505static const struct wl_output_listener output_listener = {
506 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400507 display_handle_mode
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100508};
509
Jason Ekstrand7744f712013-10-27 22:24:55 -0500510/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400511static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500512input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400513{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400514
Jason Ekstrand7744f712013-10-27 22:24:55 -0500515 struct wl_buffer *buffer;
516 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400517
Jason Ekstrand7744f712013-10-27 22:24:55 -0500518 if (!input->compositor->cursor)
519 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400520
Jason Ekstrand7744f712013-10-27 22:24:55 -0500521 image = input->compositor->cursor->images[0];
522 buffer = wl_cursor_image_get_buffer(image);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400523
Jason Ekstrand7744f712013-10-27 22:24:55 -0500524
525 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
526 input->parent.cursor.surface,
527 image->hotspot_x, image->hotspot_y);
528
529 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
530 wl_surface_damage(input->parent.cursor.surface, 0, 0,
531 image->width, image->height);
532 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400533}
534
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100535static void
Daniel Stone37816df2012-05-16 18:45:18 +0100536input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
537 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400538 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100539{
540 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500541 int32_t fx, fy;
542 enum theme_location location;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100543
Daniel Stone50692802012-06-22 13:21:41 +0100544 /* XXX: If we get a modifier event immediately before the focus,
545 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400546 input->enter_serial = serial;
547 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500548
549 if (input->output->frame) {
550 location = frame_pointer_enter(input->output->frame, input,
551 wl_fixed_to_int(x),
552 wl_fixed_to_int(y));
553 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
554 x -= wl_fixed_from_int(fx);
555 y -= wl_fixed_from_int(fy);
556
557 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
558 weston_output_schedule_repaint(&input->output->base);
559 } else {
560 location = THEME_LOCATION_CLIENT_AREA;
561 }
562
563 x += wl_fixed_from_int(input->output->base.x);
564 y += wl_fixed_from_int(input->output->base.y);
565
566 if (location == THEME_LOCATION_CLIENT_AREA) {
567 input->focus = 1;
568 notify_pointer_focus(&input->base, &input->output->base, x, y);
569 wl_pointer_set_cursor(input->parent.pointer,
570 input->enter_serial, NULL, 0, 0);
571 } else {
572 input->focus = 0;
573 notify_pointer_focus(&input->base, NULL, 0, 0);
574 input_set_cursor(input);
575 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100576}
577
578static void
Daniel Stone37816df2012-05-16 18:45:18 +0100579input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
580 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500581{
582 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500583
Jason Ekstrand7744f712013-10-27 22:24:55 -0500584 if (input->output->frame) {
585 frame_pointer_leave(input->output->frame, input);
586
587 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
588 weston_output_schedule_repaint(&input->output->base);
589 }
590
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400591 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400592 input->output = NULL;
593 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500594}
595
596static void
Daniel Stone37816df2012-05-16 18:45:18 +0100597input_handle_motion(void *data, struct wl_pointer *pointer,
598 uint32_t time, wl_fixed_t x, wl_fixed_t y)
599{
600 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500601 int32_t fx, fy;
602 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +0100603
Jason Ekstrand7744f712013-10-27 22:24:55 -0500604 if (input->output->frame) {
605 location = frame_pointer_motion(input->output->frame, input,
606 wl_fixed_to_int(x),
607 wl_fixed_to_int(y));
608 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
609 x -= wl_fixed_from_int(fx);
610 y -= wl_fixed_from_int(fy);
611
612 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
613 weston_output_schedule_repaint(&input->output->base);
614 } else {
615 location = THEME_LOCATION_CLIENT_AREA;
616 }
617
618 x += wl_fixed_from_int(input->output->base.x);
619 y += wl_fixed_from_int(input->output->base.y);
620
621 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
622 input_set_cursor(input);
623 notify_pointer_focus(&input->base, NULL, 0, 0);
624 input->focus = 0;
625 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
626 wl_pointer_set_cursor(input->parent.pointer,
627 input->enter_serial, NULL, 0, 0);
628 notify_pointer_focus(&input->base, &input->output->base, x, y);
629 input->focus = 1;
630 }
631
632 if (location == THEME_LOCATION_CLIENT_AREA)
633 notify_motion_absolute(&input->base, time, x, y);
Daniel Stone37816df2012-05-16 18:45:18 +0100634}
635
636static void
637input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100638 uint32_t serial, uint32_t time, uint32_t button,
639 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +0100640{
641 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100642 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500643 enum frame_button_state fstate;
644 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +0100645
Jason Ekstrand7744f712013-10-27 22:24:55 -0500646 if (input->output->frame) {
647 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
648 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
649
650 location = frame_pointer_button(input->output->frame, input,
651 button, fstate);
652
653 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
654
655 wl_shell_surface_move(input->output->parent.shell_surface,
656 input->parent.seat, serial);
657 frame_status_clear(input->output->frame,
658 FRAME_STATUS_MOVE);
659 return;
660 }
661
662 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE)
663 wl_display_terminate(input->compositor->base.wl_display);
664
665 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
666 weston_output_schedule_repaint(&input->output->base);
667 } else {
668 location = THEME_LOCATION_CLIENT_AREA;
669 }
670
671 if (location == THEME_LOCATION_CLIENT_AREA)
672 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +0100673}
674
675static void
676input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100677 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +0100678{
679 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100680
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400681 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +0100682}
683
684static const struct wl_pointer_listener pointer_listener = {
685 input_handle_pointer_enter,
686 input_handle_pointer_leave,
687 input_handle_motion,
688 input_handle_button,
689 input_handle_axis,
690};
691
692static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100693input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
694 int fd, uint32_t size)
695{
696 struct wayland_input *input = data;
697 struct xkb_keymap *keymap;
698 char *map_str;
699
700 if (!data) {
701 close(fd);
702 return;
703 }
704
705 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
706 close(fd);
707 return;
708 }
709
710 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
711 if (map_str == MAP_FAILED) {
712 close(fd);
713 return;
714 }
715
716 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
717 map_str,
718 XKB_KEYMAP_FORMAT_TEXT_V1,
719 0);
720 munmap(map_str, size);
721 close(fd);
722
723 if (!keymap) {
Martin Minarik6d118362012-06-07 18:01:59 +0200724 weston_log("failed to compile keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100725 return;
726 }
727
Rui Matos0c194ce2013-10-10 19:44:21 +0200728 if (input->base.keyboard)
729 weston_seat_update_keymap(&input->base, keymap);
730 else
731 weston_seat_init_keyboard(&input->base, keymap);
732
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100733 xkb_map_unref(keymap);
734}
735
736static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500737input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +0100738 struct wl_keyboard *keyboard,
739 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500740 struct wl_surface *surface,
741 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100742{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500743 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500744 struct wayland_output *focus;
745
746 focus = input->keyboard_focus;
747 if (focus) {
748 /* This shouldn't happen */
749 focus->keyboard_count--;
750 if (!focus->keyboard_count && focus->frame)
751 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
752 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
753 weston_output_schedule_repaint(&focus->base);
754 }
755
756 input->keyboard_focus = wl_surface_get_user_data(surface);
757 input->keyboard_focus->keyboard_count++;
758
759 focus = input->keyboard_focus;
760 if (focus->frame) {
761 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
762 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
763 weston_output_schedule_repaint(&focus->base);
764 }
765
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500766
Daniel Stone50692802012-06-22 13:21:41 +0100767 /* XXX: If we get a modifier event immediately before the focus,
768 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400769 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +0100770 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500771}
772
773static void
774input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +0100775 struct wl_keyboard *keyboard,
776 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500777 struct wl_surface *surface)
778{
779 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500780 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500781
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400782 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500783
784 focus = input->keyboard_focus;
785 if (!focus)
786 return; /* This shouldn't happen */
787
788 focus->keyboard_count--;
789 if (!focus->keyboard_count && focus->frame) {
790 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
791 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
792 weston_output_schedule_repaint(&focus->base);
793 }
794
795 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100796}
797
Daniel Stone37816df2012-05-16 18:45:18 +0100798static void
799input_handle_key(void *data, struct wl_keyboard *keyboard,
800 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
801{
802 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100803
Daniel Stone50692802012-06-22 13:21:41 +0100804 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400805 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +0100806 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100807 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stone50692802012-06-22 13:21:41 +0100808 STATE_UPDATE_NONE);
Daniel Stone37816df2012-05-16 18:45:18 +0100809}
810
Daniel Stone351eb612012-05-31 15:27:47 -0400811static void
812input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +0100813 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -0400814 uint32_t mods_latched, uint32_t mods_locked,
815 uint32_t group)
816{
Daniel Stone50692802012-06-22 13:21:41 +0100817 struct wayland_input *input = data;
818 struct wayland_compositor *c = input->compositor;
819 uint32_t serial_out;
820
821 /* If we get a key event followed by a modifier event with the
822 * same serial number, then we try to preserve those semantics by
823 * reusing the same serial number on the way out too. */
824 if (serial_in == input->key_serial)
825 serial_out = wl_display_get_serial(c->base.wl_display);
826 else
827 serial_out = wl_display_next_serial(c->base.wl_display);
828
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400829 xkb_state_update_mask(input->base.xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +0100830 mods_depressed, mods_latched,
831 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400832 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -0400833}
834
Daniel Stone37816df2012-05-16 18:45:18 +0100835static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100836 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500837 input_handle_keyboard_enter,
838 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +0100839 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -0400840 input_handle_modifiers,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100841};
842
843static void
Daniel Stone37816df2012-05-16 18:45:18 +0100844input_handle_capabilities(void *data, struct wl_seat *seat,
845 enum wl_seat_capability caps)
846{
847 struct wayland_input *input = data;
848
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500849 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
850 input->parent.pointer = wl_seat_get_pointer(seat);
851 wl_pointer_set_user_data(input->parent.pointer, input);
852 wl_pointer_add_listener(input->parent.pointer,
853 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400854 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500855 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
856 wl_pointer_destroy(input->parent.pointer);
857 input->parent.pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +0100858 }
859
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500860 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
861 input->parent.keyboard = wl_seat_get_keyboard(seat);
862 wl_keyboard_set_user_data(input->parent.keyboard, input);
863 wl_keyboard_add_listener(input->parent.keyboard,
864 &keyboard_listener, input);
865 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
866 wl_keyboard_destroy(input->parent.keyboard);
867 input->parent.keyboard = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +0100868 }
869}
870
871static const struct wl_seat_listener seat_listener = {
872 input_handle_capabilities,
873};
874
875static void
876display_add_seat(struct wayland_compositor *c, uint32_t id)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100877{
878 struct wayland_input *input;
879
Peter Huttererf3d62272013-08-08 11:57:05 +1000880 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100881 if (input == NULL)
882 return;
883
Rob Bradford9af5f9e2013-05-31 18:09:50 +0100884 weston_seat_init(&input->base, &c->base, "default");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100885 input->compositor = c;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500886 input->parent.seat = wl_registry_bind(c->parent.registry, id,
887 &wl_seat_interface, 1);
888 wl_list_insert(c->inputs.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100889
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500890 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
891 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500892
893 input->parent.cursor.surface =
894 wl_compositor_create_surface(c->parent.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100895}
896
897static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400898registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
899 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100900{
901 struct wayland_compositor *c = data;
902
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200903 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400904 c->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400905 wl_registry_bind(registry, name,
906 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200907 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400908 c->parent.output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400909 wl_registry_bind(registry, name,
910 &wl_output_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100911 wl_output_add_listener(c->parent.output, &output_listener, c);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200912 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400913 c->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400914 wl_registry_bind(registry, name,
915 &wl_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +0100916 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400917 display_add_seat(c, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200918 } else if (strcmp(interface, "wl_shm") == 0) {
919 c->parent.shm =
920 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100921 }
922}
923
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400924static const struct wl_registry_listener registry_listener = {
925 registry_handle_global
926};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100927
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400928static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100929wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
930{
931 struct wayland_compositor *c = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -0400932 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100933
Kristian Høgsberg453de7a2013-10-30 23:15:44 -0700934 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
935 wl_display_terminate(c->base.wl_display);
936 return 0;
937 }
938
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100939 if (mask & WL_EVENT_READABLE)
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -0400940 count = wl_display_dispatch(c->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -0500941 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400942 wl_display_flush(c->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400943
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -0400944 if (mask == 0) {
945 count = wl_display_dispatch_pending(c->parent.wl_display);
946 wl_display_flush(c->parent.wl_display);
947 }
948
949 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100950}
951
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500952static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -0400953wayland_restore(struct weston_compositor *ec)
954{
955}
956
957static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500958wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500959{
Jonas Ådahle5a12252013-04-05 23:07:11 +0200960 struct wayland_compositor *c = (struct wayland_compositor *) ec;
961
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +0300962 ec->renderer->destroy(ec);
Kristian Høgsberg3a0de882012-09-06 21:44:24 -0400963
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500964 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -0700965
Jonas Ådahle5a12252013-04-05 23:07:11 +0200966 if (c->parent.shm)
967 wl_shm_destroy(c->parent.shm);
968
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500969 free(ec);
970}
971
Jason Ekstrand7744f712013-10-27 22:24:55 -0500972static const char *left_ptrs[] = {
973 "left_ptr",
974 "default",
975 "top_left_arrow",
976 "left-arrow"
977};
978
979static void
980create_cursor(struct wayland_compositor *c, struct weston_config *config)
981{
982 struct weston_config_section *s;
983 int size;
984 char *theme = NULL;
985 unsigned int i;
986
987 s = weston_config_get_section(config, "shell", NULL, NULL);
988 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
989 weston_config_section_get_int(s, "cursor-size", &size, 32);
990
991 c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm);
992
993 c->cursor = NULL;
994 for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
995 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
996 left_ptrs[i]);
997 if (!c->cursor) {
998 fprintf(stderr, "could not load left cursor\n");
999 return;
1000 }
1001}
1002
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001003static struct weston_compositor *
Kristian Høgsberg2e28b102012-02-07 09:57:25 -05001004wayland_compositor_create(struct wl_display *display,
Daniel Stonebaf43592012-06-01 11:11:10 -04001005 int width, int height, const char *display_name,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001006 int *argc, char *argv[],
1007 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001008{
1009 struct wayland_compositor *c;
1010 struct wl_event_loop *loop;
1011 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001012
Peter Huttererf3d62272013-08-08 11:57:05 +10001013 c = zalloc(sizeof *c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001014 if (c == NULL)
1015 return NULL;
1016
Daniel Stone725c2c32012-06-22 14:04:36 +01001017 if (weston_compositor_init(&c->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001018 config) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +02001019 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001020
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001021 c->parent.wl_display = wl_display_connect(display_name);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001022
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001023 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001024 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02001025 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001026 }
1027
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001028 wl_list_init(&c->inputs);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001029 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
1030 wl_registry_add_listener(c->parent.registry, &registry_listener, c);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001031 wl_display_roundtrip(c->parent.wl_display);
1032
1033 create_cursor(c, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001034
1035 c->base.wl_display = display;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001036
1037 gl_renderer = weston_load_module("gl-renderer.so",
1038 "gl_renderer_interface");
1039 if (!gl_renderer)
1040 goto err_display;
1041
1042 if (gl_renderer->create(&c->base, c->parent.wl_display,
1043 gl_renderer->alpha_attribs,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +01001044 NULL) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +02001045 goto err_display;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001046
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001047 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001048 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001049
John Kåre Alsakerb9b87122012-10-04 20:42:16 +02001050 /* requires border fields */
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001051 if (wayland_compositor_create_output(c, width, height) < 0)
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001052 goto err_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001053
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001054 loop = wl_display_get_event_loop(c->base.wl_display);
1055
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001056 fd = wl_display_get_fd(c->parent.wl_display);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001057 c->parent.wl_source =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001058 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001059 wayland_compositor_handle_event, c);
1060 if (c->parent.wl_source == NULL)
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001061 goto err_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001062
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001063 wl_event_source_check(c->parent.wl_source);
1064
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001065 return &c->base;
Martin Olssonc5db50f2012-07-08 03:03:43 +02001066
John Kåre Alsaker779b52a2012-11-13 19:10:29 +01001067err_gl:
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001068 c->base.renderer->destroy(&c->base);
Martin Olssonc5db50f2012-07-08 03:03:43 +02001069err_display:
1070 wl_display_disconnect(c->parent.wl_display);
1071err_compositor:
1072 weston_compositor_shutdown(&c->base);
1073err_free:
1074 free(c);
1075 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001076}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001077
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001078WL_EXPORT struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05001079backend_init(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001080 struct weston_config *config)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001081{
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001082 int width = 1024, height = 640;
1083 char *display_name = NULL;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001084
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001085 const struct weston_option wayland_options[] = {
1086 { WESTON_OPTION_INTEGER, "width", 0, &width },
1087 { WESTON_OPTION_INTEGER, "height", 0, &height },
1088 { WESTON_OPTION_STRING, "display", 0, &display_name },
1089 };
1090
1091 parse_options(wayland_options,
1092 ARRAY_LENGTH(wayland_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001093
Daniel Stonebaf43592012-06-01 11:11:10 -04001094 return wayland_compositor_create(display, width, height, display_name,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001095 argc, argv, config);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001096}