blob: 21ce5cb266c65f963664017338b246362173fcb8 [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>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010035
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010036#include "compositor.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010037#include "gl-renderer.h"
Kristian Høgsberg3c2360f2013-01-28 16:01:22 -050038#include "../shared/image-loader.h"
Jonas Ådahle5a12252013-04-05 23:07:11 +020039#include "../shared/os-compatibility.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010040
41struct wayland_compositor {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050042 struct weston_compositor base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010043
44 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040045 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040046 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010047 struct wl_compositor *compositor;
48 struct wl_shell *shell;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010049 struct wl_output *output;
Jonas Ådahle5a12252013-04-05 23:07:11 +020050 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010051
52 struct {
53 int32_t x, y, width, height;
54 } screen_allocation;
55
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010056 struct wl_event_source *wl_source;
57 uint32_t event_mask;
58 } parent;
59
Kristian Høgsberg546a8122012-02-01 07:45:51 -050060 struct {
61 int32_t top, bottom, left, right;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050062 } border;
63
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010064 struct wl_list input_list;
65};
66
67struct wayland_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050068 struct weston_output base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010069 struct {
Jonas Ådahle5a12252013-04-05 23:07:11 +020070 int draw_initial_frame;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010071 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020072 struct wl_shell_surface *shell_surface;
Benjamin Franzkebe014562011-02-18 17:04:24 +010073 struct wl_egl_window *egl_window;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010074 } parent;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050075 struct weston_mode mode;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010076};
77
78struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -040079 struct weston_seat base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010080 struct wayland_compositor *compositor;
Daniel Stone37816df2012-05-16 18:45:18 +010081 struct wl_seat *seat;
82 struct wl_pointer *pointer;
83 struct wl_keyboard *keyboard;
84 struct wl_touch *touch;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010085 struct wl_list link;
Daniel Stone50692802012-06-22 13:21:41 +010086 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -040087 uint32_t enter_serial;
88 int focus;
89 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010090};
91
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +030092struct gl_renderer_interface *gl_renderer;
93
Kristian Høgsberg546a8122012-02-01 07:45:51 -050094static void
95create_border(struct wayland_compositor *c)
96{
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +020097 pixman_image_t *image;
John Kåre Alsaker44154502012-11-13 19:10:20 +010098 int32_t edges[4];
Kristian Høgsberg546a8122012-02-01 07:45:51 -050099
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200100 image = load_image(DATADIR "/weston/border.png");
101 if (!image) {
Aaron Faanes9cefc642013-09-30 22:06:39 -0500102 weston_log("couldn't load border image\n");
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500103 return;
104 }
105
John Kåre Alsaker44154502012-11-13 19:10:20 +0100106 edges[0] = c->border.left;
107 edges[1] = c->border.right;
108 edges[2] = c->border.top;
109 edges[3] = c->border.bottom;
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200110
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300111 gl_renderer->set_border(&c->base, pixman_image_get_width(image),
John Kåre Alsaker44154502012-11-13 19:10:20 +0100112 pixman_image_get_height(image),
113 pixman_image_get_data(image), edges);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500114
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200115 pixman_image_unref(image);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500116}
117
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500118static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500119frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400120{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500121 struct weston_output *output = data;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400122
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500123 wl_callback_destroy(callback);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500124 weston_output_finish_frame(output, time);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400125}
126
127static const struct wl_callback_listener frame_listener = {
128 frame_done
129};
130
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500131static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200132buffer_release(void *data, struct wl_buffer *buffer)
133{
134 wl_buffer_destroy(buffer);
135}
136
137static const struct wl_buffer_listener buffer_listener = {
138 buffer_release
139};
140
141static void
142draw_initial_frame(struct wayland_output *output)
143{
144 struct wayland_compositor *c =
145 (struct wayland_compositor *) output->base.compositor;
146 struct wl_shm *shm = c->parent.shm;
147 struct wl_surface *surface = output->parent.surface;
148 struct wl_shm_pool *pool;
149 struct wl_buffer *buffer;
150
151 int width, height, stride;
152 int size;
153 int fd;
154 void *data;
155
156 width = output->mode.width + c->border.left + c->border.right;
157 height = output->mode.height + c->border.top + c->border.bottom;
158 stride = width * 4;
159 size = height * stride;
160
161 fd = os_create_anonymous_file(size);
162 if (fd < 0) {
163 perror("os_create_anonymous_file");
164 return;
165 }
166
167 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
168 if (data == MAP_FAILED) {
169 perror("mmap");
170 close(fd);
171 return;
172 }
173
174 pool = wl_shm_create_pool(shm, fd, size);
175
176 buffer = wl_shm_pool_create_buffer(pool, 0,
177 width, height,
178 stride,
179 WL_SHM_FORMAT_ARGB8888);
180 wl_buffer_add_listener(buffer, &buffer_listener, buffer);
181 wl_shm_pool_destroy(pool);
182 close(fd);
183
184 memset(data, 0, size);
185
186 wl_surface_attach(surface, buffer, 0, 0);
187
188 /* We only need to damage some part, as its only transparant
189 * pixels anyway. */
190 wl_surface_damage(surface, 0, 0, 1, 1);
191}
192
193static void
194wayland_output_start_repaint_loop(struct weston_output *output_base)
195{
196 struct wayland_output *output = (struct wayland_output *) output_base;
197 struct wl_callback *callback;
198
199 /* If this is the initial frame, we need to attach a buffer so that
200 * the compositor can map the surface and include it in its render
201 * loop. If the surface doesn't end up in the render loop, the frame
202 * callback won't be invoked. The buffer is transparent and of the
203 * same size as the future real output buffer. */
204 if (output->parent.draw_initial_frame) {
205 output->parent.draw_initial_frame = 0;
206
207 draw_initial_frame(output);
208 }
209
210 callback = wl_surface_frame(output->parent.surface);
211 wl_callback_add_listener(callback, &frame_listener, output);
212 wl_surface_commit(output->parent.surface);
213}
214
David Herrmann1edf44c2013-10-22 17:11:26 +0200215static int
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400216wayland_output_repaint(struct weston_output *output_base,
217 pixman_region32_t *damage)
218{
219 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400220 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400221 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600222
Kristian Høgsberg33418202011-08-16 23:01:28 -0400223 callback = wl_surface_frame(output->parent.surface);
224 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100225
Pekka Paalanenbc106382012-10-10 12:49:31 +0300226 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200227
228 pixman_region32_subtract(&ec->primary_plane.damage,
229 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200230 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100231}
232
Matt Roper361d2ad2011-08-29 13:52:23 -0700233static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500234wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700235{
236 struct wayland_output *output = (struct wayland_output *) output_base;
Matt Roper361d2ad2011-08-29 13:52:23 -0700237
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300238 gl_renderer->output_destroy(output_base);
John Kåre Alsaker94659272012-11-13 19:10:18 +0100239
Matt Roper361d2ad2011-08-29 13:52:23 -0700240 wl_egl_window_destroy(output->parent.egl_window);
241 free(output);
242
243 return;
244}
245
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300246static const struct wl_shell_surface_listener shell_surface_listener;
247
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200248static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100249wayland_compositor_create_output(struct wayland_compositor *c,
250 int width, int height)
251{
252 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100253
Peter Huttererf3d62272013-08-08 11:57:05 +1000254 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100255 if (output == NULL)
256 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100257
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400258 output->mode.flags =
259 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
260 output->mode.width = width;
261 output->mode.height = height;
262 output->mode.refresh = 60;
263 wl_list_init(&output->base.mode_list);
264 wl_list_insert(&output->base.mode_list, &output->mode.link);
265
Hardeningff39efa2013-09-18 23:56:35 +0200266 output->base.current_mode = &output->mode;
Scott Moreau1bad5db2012-08-18 01:04:05 -0600267 weston_output_init(&output->base, &c->base, 0, 0, width, height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200268 WL_OUTPUT_TRANSFORM_NORMAL, 1);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400269
Kristian Høgsberg90bc88c2012-08-13 23:34:04 -0400270 output->base.make = "waywayland";
271 output->base.model = "none";
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500272
273 weston_output_move(&output->base, 0, 0);
274
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100275 output->parent.surface =
276 wl_compositor_create_surface(c->parent.compositor);
277 wl_surface_set_user_data(output->parent.surface, output);
278
Benjamin Franzkebe014562011-02-18 17:04:24 +0100279 output->parent.egl_window =
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500280 wl_egl_window_create(output->parent.surface,
281 width + c->border.left + c->border.right,
282 height + c->border.top + c->border.bottom);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100283 if (!output->parent.egl_window) {
Martin Minarik6d118362012-06-07 18:01:59 +0200284 weston_log("failure to create wl_egl_window\n");
Benjamin Franzkebe014562011-02-18 17:04:24 +0100285 goto cleanup_output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100286 }
287
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300288 if (gl_renderer->output_create(&output->base,
John Kåre Alsaker94659272012-11-13 19:10:18 +0100289 output->parent.egl_window) < 0)
Benjamin Franzkebe014562011-02-18 17:04:24 +0100290 goto cleanup_window;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100291
Jonas Ådahle5a12252013-04-05 23:07:11 +0200292 output->parent.draw_initial_frame = 1;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200293 output->parent.shell_surface =
294 wl_shell_get_shell_surface(c->parent.shell,
295 output->parent.surface);
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300296 wl_shell_surface_add_listener(output->parent.shell_surface,
297 &shell_surface_listener, output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200298 wl_shell_surface_set_toplevel(output->parent.shell_surface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100299
Jonas Ådahle5a12252013-04-05 23:07:11 +0200300 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500301 output->base.repaint = wayland_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -0700302 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800303 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200304 output->base.set_backlight = NULL;
305 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800306 output->base.switch_mode = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100307
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100308 wl_list_insert(c->base.output_list.prev, &output->base.link);
309
310 return 0;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100311
Benjamin Franzkebe014562011-02-18 17:04:24 +0100312cleanup_window:
313 wl_egl_window_destroy(output->parent.egl_window);
314cleanup_output:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500315 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +0100316 free(output);
317
318 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100319}
320
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300321static void
322shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
323 uint32_t serial)
324{
325 wl_shell_surface_pong(shell_surface, serial);
326}
327
328static void
329shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
330 uint32_t edges, int32_t width, int32_t height)
331{
332 /* FIXME: implement resizing */
333}
334
335static void
336shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
337{
338}
339
340static const struct wl_shell_surface_listener shell_surface_listener = {
341 shell_surface_ping,
342 shell_surface_configure,
343 shell_surface_popup_done
344};
345
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100346/* Events received from the wayland-server this compositor is client of: */
347
348/* parent output interface */
349static void
350display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400351 struct wl_output *wl_output,
352 int x,
353 int y,
354 int physical_width,
355 int physical_height,
356 int subpixel,
357 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -0400358 const char *model,
359 int transform)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100360{
361 struct wayland_compositor *c = data;
362
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500363 c->parent.screen_allocation.x = x;
364 c->parent.screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400365}
366
367static void
368display_handle_mode(void *data,
369 struct wl_output *wl_output,
370 uint32_t flags,
371 int width,
372 int height,
373 int refresh)
374{
375 struct wayland_compositor *c = data;
376
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100377 c->parent.screen_allocation.width = width;
378 c->parent.screen_allocation.height = height;
379}
380
381static const struct wl_output_listener output_listener = {
382 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400383 display_handle_mode
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100384};
385
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400386static void
387check_focus(struct wayland_input *input, wl_fixed_t x, wl_fixed_t y)
388{
389 struct wayland_compositor *c = input->compositor;
390 int width, height, inside;
391
392 width = input->output->mode.width;
393 height = input->output->mode.height;
394
395 inside = c->border.left <= wl_fixed_to_int(x) &&
396 wl_fixed_to_int(x) < width + c->border.left &&
397 c->border.top <= wl_fixed_to_int(y) &&
398 wl_fixed_to_int(y) < height + c->border.top;
399
400 if (!input->focus && inside) {
401 notify_pointer_focus(&input->base, &input->output->base,
402 x - wl_fixed_from_int(c->border.left),
403 y = wl_fixed_from_int(c->border.top));
404 wl_pointer_set_cursor(input->pointer,
405 input->enter_serial, NULL, 0, 0);
406 } else if (input->focus && !inside) {
407 notify_pointer_focus(&input->base, NULL, 0, 0);
408 /* FIXME: Should set default cursor here. */
409 }
410
411 input->focus = inside;
412}
413
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100414/* parent input interface */
415static void
Daniel Stone37816df2012-05-16 18:45:18 +0100416input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
417 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400418 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100419{
420 struct wayland_input *input = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100421
Daniel Stone50692802012-06-22 13:21:41 +0100422 /* XXX: If we get a modifier event immediately before the focus,
423 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400424 input->enter_serial = serial;
425 input->output = wl_surface_get_user_data(surface);
426 check_focus(input, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100427}
428
429static void
Daniel Stone37816df2012-05-16 18:45:18 +0100430input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
431 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500432{
433 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500434
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400435 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400436 input->output = NULL;
437 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500438}
439
440static void
Daniel Stone37816df2012-05-16 18:45:18 +0100441input_handle_motion(void *data, struct wl_pointer *pointer,
442 uint32_t time, wl_fixed_t x, wl_fixed_t y)
443{
444 struct wayland_input *input = data;
445 struct wayland_compositor *c = input->compositor;
446
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400447 check_focus(input, x, y);
448 if (input->focus)
449 notify_motion(&input->base, time,
Kristian Høgsberg068b61c2013-02-25 17:04:47 -0500450 x - wl_fixed_from_int(c->border.left) -
Kristian Høgsberge3148752013-05-06 23:19:49 -0400451 input->base.pointer->x,
Kristian Høgsberg068b61c2013-02-25 17:04:47 -0500452 y - wl_fixed_from_int(c->border.top) -
Kristian Høgsberge3148752013-05-06 23:19:49 -0400453 input->base.pointer->y);
Daniel Stone37816df2012-05-16 18:45:18 +0100454}
455
456static void
457input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100458 uint32_t serial, uint32_t time, uint32_t button,
459 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +0100460{
461 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100462 enum wl_pointer_button_state state = state_w;
Daniel Stone37816df2012-05-16 18:45:18 +0100463
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400464 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +0100465}
466
467static void
468input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100469 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +0100470{
471 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100472
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400473 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +0100474}
475
476static const struct wl_pointer_listener pointer_listener = {
477 input_handle_pointer_enter,
478 input_handle_pointer_leave,
479 input_handle_motion,
480 input_handle_button,
481 input_handle_axis,
482};
483
484static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100485input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
486 int fd, uint32_t size)
487{
488 struct wayland_input *input = data;
489 struct xkb_keymap *keymap;
490 char *map_str;
491
492 if (!data) {
493 close(fd);
494 return;
495 }
496
497 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
498 close(fd);
499 return;
500 }
501
502 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
503 if (map_str == MAP_FAILED) {
504 close(fd);
505 return;
506 }
507
508 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
509 map_str,
510 XKB_KEYMAP_FORMAT_TEXT_V1,
511 0);
512 munmap(map_str, size);
513 close(fd);
514
515 if (!keymap) {
Martin Minarik6d118362012-06-07 18:01:59 +0200516 weston_log("failed to compile keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100517 return;
518 }
519
Rui Matos0c194ce2013-10-10 19:44:21 +0200520 if (input->base.keyboard)
521 weston_seat_update_keymap(&input->base, keymap);
522 else
523 weston_seat_init_keyboard(&input->base, keymap);
524
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100525 xkb_map_unref(keymap);
526}
527
528static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500529input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +0100530 struct wl_keyboard *keyboard,
531 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500532 struct wl_surface *surface,
533 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100534{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500535 struct wayland_input *input = data;
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500536
Daniel Stone50692802012-06-22 13:21:41 +0100537 /* XXX: If we get a modifier event immediately before the focus,
538 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400539 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +0100540 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500541}
542
543static void
544input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +0100545 struct wl_keyboard *keyboard,
546 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500547 struct wl_surface *surface)
548{
549 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500550
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400551 notify_keyboard_focus_out(&input->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100552}
553
Daniel Stone37816df2012-05-16 18:45:18 +0100554static void
555input_handle_key(void *data, struct wl_keyboard *keyboard,
556 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
557{
558 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100559
Daniel Stone50692802012-06-22 13:21:41 +0100560 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400561 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +0100562 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100563 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stone50692802012-06-22 13:21:41 +0100564 STATE_UPDATE_NONE);
Daniel Stone37816df2012-05-16 18:45:18 +0100565}
566
Daniel Stone351eb612012-05-31 15:27:47 -0400567static void
568input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +0100569 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -0400570 uint32_t mods_latched, uint32_t mods_locked,
571 uint32_t group)
572{
Daniel Stone50692802012-06-22 13:21:41 +0100573 struct wayland_input *input = data;
574 struct wayland_compositor *c = input->compositor;
575 uint32_t serial_out;
576
577 /* If we get a key event followed by a modifier event with the
578 * same serial number, then we try to preserve those semantics by
579 * reusing the same serial number on the way out too. */
580 if (serial_in == input->key_serial)
581 serial_out = wl_display_get_serial(c->base.wl_display);
582 else
583 serial_out = wl_display_next_serial(c->base.wl_display);
584
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400585 xkb_state_update_mask(input->base.xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +0100586 mods_depressed, mods_latched,
587 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400588 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -0400589}
590
Daniel Stone37816df2012-05-16 18:45:18 +0100591static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100592 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500593 input_handle_keyboard_enter,
594 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +0100595 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -0400596 input_handle_modifiers,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100597};
598
599static void
Daniel Stone37816df2012-05-16 18:45:18 +0100600input_handle_capabilities(void *data, struct wl_seat *seat,
601 enum wl_seat_capability caps)
602{
603 struct wayland_input *input = data;
604
605 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
606 input->pointer = wl_seat_get_pointer(seat);
607 wl_pointer_set_user_data(input->pointer, input);
608 wl_pointer_add_listener(input->pointer, &pointer_listener,
609 input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400610 weston_seat_init_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +0100611 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
612 wl_pointer_destroy(input->pointer);
613 input->pointer = NULL;
614 }
615
616 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
617 input->keyboard = wl_seat_get_keyboard(seat);
618 wl_keyboard_set_user_data(input->keyboard, input);
619 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
620 input);
621 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
622 wl_keyboard_destroy(input->keyboard);
623 input->keyboard = NULL;
624 }
625}
626
627static const struct wl_seat_listener seat_listener = {
628 input_handle_capabilities,
629};
630
631static void
632display_add_seat(struct wayland_compositor *c, uint32_t id)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100633{
634 struct wayland_input *input;
635
Peter Huttererf3d62272013-08-08 11:57:05 +1000636 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100637 if (input == NULL)
638 return;
639
Rob Bradford9af5f9e2013-05-31 18:09:50 +0100640 weston_seat_init(&input->base, &c->base, "default");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100641 input->compositor = c;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400642 input->seat = wl_registry_bind(c->parent.registry, id,
643 &wl_seat_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100644 wl_list_insert(c->input_list.prev, &input->link);
645
Daniel Stone37816df2012-05-16 18:45:18 +0100646 wl_seat_add_listener(input->seat, &seat_listener, input);
647 wl_seat_set_user_data(input->seat, input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100648}
649
650static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400651registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
652 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100653{
654 struct wayland_compositor *c = data;
655
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200656 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400657 c->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400658 wl_registry_bind(registry, name,
659 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200660 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400661 c->parent.output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400662 wl_registry_bind(registry, name,
663 &wl_output_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100664 wl_output_add_listener(c->parent.output, &output_listener, c);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200665 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400666 c->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400667 wl_registry_bind(registry, name,
668 &wl_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +0100669 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400670 display_add_seat(c, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200671 } else if (strcmp(interface, "wl_shm") == 0) {
672 c->parent.shm =
673 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100674 }
675}
676
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400677static const struct wl_registry_listener registry_listener = {
678 registry_handle_global
679};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100680
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400681static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100682wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
683{
684 struct wayland_compositor *c = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -0400685 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100686
687 if (mask & WL_EVENT_READABLE)
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -0400688 count = wl_display_dispatch(c->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -0500689 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400690 wl_display_flush(c->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400691
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -0400692 if (mask == 0) {
693 count = wl_display_dispatch_pending(c->parent.wl_display);
694 wl_display_flush(c->parent.wl_display);
695 }
696
697 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100698}
699
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500700static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -0400701wayland_restore(struct weston_compositor *ec)
702{
703}
704
705static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500706wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500707{
Jonas Ådahle5a12252013-04-05 23:07:11 +0200708 struct wayland_compositor *c = (struct wayland_compositor *) ec;
709
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +0300710 ec->renderer->destroy(ec);
Kristian Høgsberg3a0de882012-09-06 21:44:24 -0400711
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500712 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -0700713
Jonas Ådahle5a12252013-04-05 23:07:11 +0200714 if (c->parent.shm)
715 wl_shm_destroy(c->parent.shm);
716
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500717 free(ec);
718}
719
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500720static struct weston_compositor *
Kristian Høgsberg2e28b102012-02-07 09:57:25 -0500721wayland_compositor_create(struct wl_display *display,
Daniel Stonebaf43592012-06-01 11:11:10 -0400722 int width, int height, const char *display_name,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400723 int *argc, char *argv[],
724 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100725{
726 struct wayland_compositor *c;
727 struct wl_event_loop *loop;
728 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100729
Peter Huttererf3d62272013-08-08 11:57:05 +1000730 c = zalloc(sizeof *c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100731 if (c == NULL)
732 return NULL;
733
Daniel Stone725c2c32012-06-22 14:04:36 +0100734 if (weston_compositor_init(&c->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400735 config) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200736 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +0100737
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400738 c->parent.wl_display = wl_display_connect(display_name);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100739
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400740 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200741 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +0200742 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100743 }
744
745 wl_list_init(&c->input_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400746 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
747 wl_registry_add_listener(c->parent.registry, &registry_listener, c);
748 wl_display_dispatch(c->parent.wl_display);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100749
750 c->base.wl_display = display;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300751
752 gl_renderer = weston_load_module("gl-renderer.so",
753 "gl_renderer_interface");
754 if (!gl_renderer)
755 goto err_display;
756
757 if (gl_renderer->create(&c->base, c->parent.wl_display,
758 gl_renderer->alpha_attribs,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100759 NULL) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200760 goto err_display;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100761
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100762 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -0400763 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100764
John Kåre Alsakerc37b32d2012-10-04 20:42:17 +0200765 c->border.top = 30;
766 c->border.bottom = 24;
John Kåre Alsakerb9b87122012-10-04 20:42:16 +0200767 c->border.left = 25;
John Kåre Alsakerc37b32d2012-10-04 20:42:17 +0200768 c->border.right = 26;
John Kåre Alsakerb9b87122012-10-04 20:42:16 +0200769
770 /* requires border fields */
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100771 if (wayland_compositor_create_output(c, width, height) < 0)
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100772 goto err_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100773
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300774 /* requires gl_renderer->output_state_create called
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100775 * by wayland_compositor_create_output */
John Kåre Alsaker4b3081a2012-10-03 18:02:22 +0200776 create_border(c);
777
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100778 loop = wl_display_get_event_loop(c->base.wl_display);
779
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400780 fd = wl_display_get_fd(c->parent.wl_display);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100781 c->parent.wl_source =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400782 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100783 wayland_compositor_handle_event, c);
784 if (c->parent.wl_source == NULL)
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100785 goto err_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100786
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -0400787 wl_event_source_check(c->parent.wl_source);
788
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100789 return &c->base;
Martin Olssonc5db50f2012-07-08 03:03:43 +0200790
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100791err_gl:
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +0300792 c->base.renderer->destroy(&c->base);
Martin Olssonc5db50f2012-07-08 03:03:43 +0200793err_display:
794 wl_display_disconnect(c->parent.wl_display);
795err_compositor:
796 weston_compositor_shutdown(&c->base);
797err_free:
798 free(c);
799 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100800}
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400801
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500802WL_EXPORT struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500803backend_init(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400804 struct weston_config *config)
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400805{
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400806 int width = 1024, height = 640;
807 char *display_name = NULL;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400808
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400809 const struct weston_option wayland_options[] = {
810 { WESTON_OPTION_INTEGER, "width", 0, &width },
811 { WESTON_OPTION_INTEGER, "height", 0, &height },
812 { WESTON_OPTION_STRING, "display", 0, &display_name },
813 };
814
815 parse_options(wayland_options,
816 ARRAY_LENGTH(wayland_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400817
Daniel Stonebaf43592012-06-01 11:11:10 -0400818 return wayland_compositor_create(display, width, height, display_name,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400819 argc, argv, config);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400820}