blob: af493e7f8c009244994f1b071d8d475112af63a5 [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
23#ifdef HAVE_CONFIG_H
24#include <config.h>
25#endif
26
27#include <stddef.h>
28#define _GNU_SOURCE
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <fcntl.h>
33#include <unistd.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010034#include <sys/mman.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010035
Benjamin Franzkebe014562011-02-18 17:04:24 +010036#include <wayland-client.h>
37#include <wayland-egl.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010038
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010039#include "compositor.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010040#include "gl-renderer.h"
Kristian Høgsberg3c2360f2013-01-28 16:01:22 -050041#include "../shared/image-loader.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010042
43struct wayland_compositor {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -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;
52
53 struct {
54 int32_t x, y, width, height;
55 } screen_allocation;
56
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010057 struct wl_event_source *wl_source;
58 uint32_t event_mask;
59 } parent;
60
Kristian Høgsberg546a8122012-02-01 07:45:51 -050061 struct {
62 int32_t top, bottom, left, right;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050063 } border;
64
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010065 struct wl_list input_list;
66};
67
68struct wayland_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050069 struct weston_output base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010070 struct {
71 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
Kristian Høgsberg546a8122012-02-01 07:45:51 -050092static void
93create_border(struct wayland_compositor *c)
94{
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +020095 pixman_image_t *image;
John Kåre Alsaker44154502012-11-13 19:10:20 +010096 int32_t edges[4];
Kristian Høgsberg546a8122012-02-01 07:45:51 -050097
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +020098 image = load_image(DATADIR "/weston/border.png");
99 if (!image) {
Martin Minarik6d118362012-06-07 18:01:59 +0200100 weston_log("could'nt load border image\n");
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500101 return;
102 }
103
John Kåre Alsaker44154502012-11-13 19:10:20 +0100104 edges[0] = c->border.left;
105 edges[1] = c->border.right;
106 edges[2] = c->border.top;
107 edges[3] = c->border.bottom;
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200108
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100109 gl_renderer_set_border(&c->base, pixman_image_get_width(image),
John Kåre Alsaker44154502012-11-13 19:10:20 +0100110 pixman_image_get_height(image),
111 pixman_image_get_data(image), edges);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500112
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200113 pixman_image_unref(image);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500114}
115
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500116static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500117frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400118{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500119 struct weston_output *output = data;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400120
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500121 wl_callback_destroy(callback);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500122 weston_output_finish_frame(output, time);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400123}
124
125static const struct wl_callback_listener frame_listener = {
126 frame_done
127};
128
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500129static void
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400130wayland_output_repaint(struct weston_output *output_base,
131 pixman_region32_t *damage)
132{
133 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400134 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400135 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600136
Kristian Høgsberg33418202011-08-16 23:01:28 -0400137 callback = wl_surface_frame(output->parent.surface);
138 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100139
Pekka Paalanenbc106382012-10-10 12:49:31 +0300140 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200141
142 pixman_region32_subtract(&ec->primary_plane.damage,
143 &ec->primary_plane.damage, damage);
144
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100145}
146
Matt Roper361d2ad2011-08-29 13:52:23 -0700147static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500148wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700149{
150 struct wayland_output *output = (struct wayland_output *) output_base;
Matt Roper361d2ad2011-08-29 13:52:23 -0700151
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100152 gl_renderer_output_destroy(output_base);
John Kåre Alsaker94659272012-11-13 19:10:18 +0100153
Matt Roper361d2ad2011-08-29 13:52:23 -0700154 wl_egl_window_destroy(output->parent.egl_window);
155 free(output);
156
157 return;
158}
159
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300160static const struct wl_shell_surface_listener shell_surface_listener;
161
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200162static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100163wayland_compositor_create_output(struct wayland_compositor *c,
164 int width, int height)
165{
166 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100167
168 output = malloc(sizeof *output);
169 if (output == NULL)
170 return -1;
171 memset(output, 0, sizeof *output);
172
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400173 output->mode.flags =
174 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
175 output->mode.width = width;
176 output->mode.height = height;
177 output->mode.refresh = 60;
178 wl_list_init(&output->base.mode_list);
179 wl_list_insert(&output->base.mode_list, &output->mode.link);
180
181 output->base.current = &output->mode;
Scott Moreau1bad5db2012-08-18 01:04:05 -0600182 weston_output_init(&output->base, &c->base, 0, 0, width, height,
183 WL_OUTPUT_TRANSFORM_NORMAL);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400184
Kristian Høgsberg90bc88c2012-08-13 23:34:04 -0400185 output->base.make = "waywayland";
186 output->base.model = "none";
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500187
188 weston_output_move(&output->base, 0, 0);
189
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100190 output->parent.surface =
191 wl_compositor_create_surface(c->parent.compositor);
192 wl_surface_set_user_data(output->parent.surface, output);
193
Benjamin Franzkebe014562011-02-18 17:04:24 +0100194 output->parent.egl_window =
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500195 wl_egl_window_create(output->parent.surface,
196 width + c->border.left + c->border.right,
197 height + c->border.top + c->border.bottom);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100198 if (!output->parent.egl_window) {
Martin Minarik6d118362012-06-07 18:01:59 +0200199 weston_log("failure to create wl_egl_window\n");
Benjamin Franzkebe014562011-02-18 17:04:24 +0100200 goto cleanup_output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100201 }
202
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100203 if (gl_renderer_output_create(&output->base,
John Kåre Alsaker94659272012-11-13 19:10:18 +0100204 output->parent.egl_window) < 0)
Benjamin Franzkebe014562011-02-18 17:04:24 +0100205 goto cleanup_window;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100206
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200207 output->parent.shell_surface =
208 wl_shell_get_shell_surface(c->parent.shell,
209 output->parent.surface);
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300210 wl_shell_surface_add_listener(output->parent.shell_surface,
211 &shell_surface_listener, output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200212 wl_shell_surface_set_toplevel(output->parent.shell_surface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100213
Alex Wubd3354b2012-04-17 17:20:49 +0800214 output->base.origin = output->base.current;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500215 output->base.repaint = wayland_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -0700216 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800217 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200218 output->base.set_backlight = NULL;
219 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800220 output->base.switch_mode = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100221
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100222 wl_list_insert(c->base.output_list.prev, &output->base.link);
223
224 return 0;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100225
Benjamin Franzkebe014562011-02-18 17:04:24 +0100226cleanup_window:
227 wl_egl_window_destroy(output->parent.egl_window);
228cleanup_output:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500229 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +0100230 free(output);
231
232 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100233}
234
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300235static void
236shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
237 uint32_t serial)
238{
239 wl_shell_surface_pong(shell_surface, serial);
240}
241
242static void
243shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
244 uint32_t edges, int32_t width, int32_t height)
245{
246 /* FIXME: implement resizing */
247}
248
249static void
250shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
251{
252}
253
254static const struct wl_shell_surface_listener shell_surface_listener = {
255 shell_surface_ping,
256 shell_surface_configure,
257 shell_surface_popup_done
258};
259
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100260/* Events received from the wayland-server this compositor is client of: */
261
262/* parent output interface */
263static void
264display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400265 struct wl_output *wl_output,
266 int x,
267 int y,
268 int physical_width,
269 int physical_height,
270 int subpixel,
271 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -0400272 const char *model,
273 int transform)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100274{
275 struct wayland_compositor *c = data;
276
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500277 c->parent.screen_allocation.x = x;
278 c->parent.screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400279}
280
281static void
282display_handle_mode(void *data,
283 struct wl_output *wl_output,
284 uint32_t flags,
285 int width,
286 int height,
287 int refresh)
288{
289 struct wayland_compositor *c = data;
290
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100291 c->parent.screen_allocation.width = width;
292 c->parent.screen_allocation.height = height;
293}
294
295static const struct wl_output_listener output_listener = {
296 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400297 display_handle_mode
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100298};
299
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400300static void
301check_focus(struct wayland_input *input, wl_fixed_t x, wl_fixed_t y)
302{
303 struct wayland_compositor *c = input->compositor;
304 int width, height, inside;
305
306 width = input->output->mode.width;
307 height = input->output->mode.height;
308
309 inside = c->border.left <= wl_fixed_to_int(x) &&
310 wl_fixed_to_int(x) < width + c->border.left &&
311 c->border.top <= wl_fixed_to_int(y) &&
312 wl_fixed_to_int(y) < height + c->border.top;
313
314 if (!input->focus && inside) {
315 notify_pointer_focus(&input->base, &input->output->base,
316 x - wl_fixed_from_int(c->border.left),
317 y = wl_fixed_from_int(c->border.top));
318 wl_pointer_set_cursor(input->pointer,
319 input->enter_serial, NULL, 0, 0);
320 } else if (input->focus && !inside) {
321 notify_pointer_focus(&input->base, NULL, 0, 0);
322 /* FIXME: Should set default cursor here. */
323 }
324
325 input->focus = inside;
326}
327
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100328/* parent input interface */
329static void
Daniel Stone37816df2012-05-16 18:45:18 +0100330input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
331 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400332 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100333{
334 struct wayland_input *input = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100335
Daniel Stone50692802012-06-22 13:21:41 +0100336 /* XXX: If we get a modifier event immediately before the focus,
337 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400338 input->enter_serial = serial;
339 input->output = wl_surface_get_user_data(surface);
340 check_focus(input, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100341}
342
343static void
Daniel Stone37816df2012-05-16 18:45:18 +0100344input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
345 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500346{
347 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500348
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400349 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400350 input->output = NULL;
351 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500352}
353
354static void
Daniel Stone37816df2012-05-16 18:45:18 +0100355input_handle_motion(void *data, struct wl_pointer *pointer,
356 uint32_t time, wl_fixed_t x, wl_fixed_t y)
357{
358 struct wayland_input *input = data;
359 struct wayland_compositor *c = input->compositor;
360
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400361 check_focus(input, x, y);
362 if (input->focus)
363 notify_motion(&input->base, time,
364 x - wl_fixed_from_int(c->border.left),
365 y - wl_fixed_from_int(c->border.top));
Daniel Stone37816df2012-05-16 18:45:18 +0100366}
367
368static void
369input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100370 uint32_t serial, uint32_t time, uint32_t button,
371 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +0100372{
373 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100374 enum wl_pointer_button_state state = state_w;
Daniel Stone37816df2012-05-16 18:45:18 +0100375
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400376 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +0100377}
378
379static void
380input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100381 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +0100382{
383 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100384
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400385 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +0100386}
387
388static const struct wl_pointer_listener pointer_listener = {
389 input_handle_pointer_enter,
390 input_handle_pointer_leave,
391 input_handle_motion,
392 input_handle_button,
393 input_handle_axis,
394};
395
396static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100397input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
398 int fd, uint32_t size)
399{
400 struct wayland_input *input = data;
401 struct xkb_keymap *keymap;
402 char *map_str;
403
404 if (!data) {
405 close(fd);
406 return;
407 }
408
409 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
410 close(fd);
411 return;
412 }
413
414 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
415 if (map_str == MAP_FAILED) {
416 close(fd);
417 return;
418 }
419
420 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
421 map_str,
422 XKB_KEYMAP_FORMAT_TEXT_V1,
423 0);
424 munmap(map_str, size);
425 close(fd);
426
427 if (!keymap) {
Martin Minarik6d118362012-06-07 18:01:59 +0200428 weston_log("failed to compile keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100429 return;
430 }
431
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400432 weston_seat_init_keyboard(&input->base, keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100433 xkb_map_unref(keymap);
434}
435
436static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500437input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +0100438 struct wl_keyboard *keyboard,
439 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500440 struct wl_surface *surface,
441 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100442{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500443 struct wayland_input *input = data;
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500444
Daniel Stone50692802012-06-22 13:21:41 +0100445 /* XXX: If we get a modifier event immediately before the focus,
446 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400447 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +0100448 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500449}
450
451static void
452input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +0100453 struct wl_keyboard *keyboard,
454 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500455 struct wl_surface *surface)
456{
457 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500458
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400459 notify_keyboard_focus_out(&input->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100460}
461
Daniel Stone37816df2012-05-16 18:45:18 +0100462static void
463input_handle_key(void *data, struct wl_keyboard *keyboard,
464 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
465{
466 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100467
Daniel Stone50692802012-06-22 13:21:41 +0100468 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400469 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +0100470 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100471 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stone50692802012-06-22 13:21:41 +0100472 STATE_UPDATE_NONE);
Daniel Stone37816df2012-05-16 18:45:18 +0100473}
474
Daniel Stone351eb612012-05-31 15:27:47 -0400475static void
476input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +0100477 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -0400478 uint32_t mods_latched, uint32_t mods_locked,
479 uint32_t group)
480{
Daniel Stone50692802012-06-22 13:21:41 +0100481 struct wayland_input *input = data;
482 struct wayland_compositor *c = input->compositor;
483 uint32_t serial_out;
484
485 /* If we get a key event followed by a modifier event with the
486 * same serial number, then we try to preserve those semantics by
487 * reusing the same serial number on the way out too. */
488 if (serial_in == input->key_serial)
489 serial_out = wl_display_get_serial(c->base.wl_display);
490 else
491 serial_out = wl_display_next_serial(c->base.wl_display);
492
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400493 xkb_state_update_mask(input->base.xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +0100494 mods_depressed, mods_latched,
495 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400496 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -0400497}
498
Daniel Stone37816df2012-05-16 18:45:18 +0100499static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100500 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500501 input_handle_keyboard_enter,
502 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +0100503 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -0400504 input_handle_modifiers,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100505};
506
507static void
Daniel Stone37816df2012-05-16 18:45:18 +0100508input_handle_capabilities(void *data, struct wl_seat *seat,
509 enum wl_seat_capability caps)
510{
511 struct wayland_input *input = data;
512
513 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
514 input->pointer = wl_seat_get_pointer(seat);
515 wl_pointer_set_user_data(input->pointer, input);
516 wl_pointer_add_listener(input->pointer, &pointer_listener,
517 input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400518 weston_seat_init_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +0100519 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
520 wl_pointer_destroy(input->pointer);
521 input->pointer = NULL;
522 }
523
524 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
525 input->keyboard = wl_seat_get_keyboard(seat);
526 wl_keyboard_set_user_data(input->keyboard, input);
527 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
528 input);
529 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
530 wl_keyboard_destroy(input->keyboard);
531 input->keyboard = NULL;
532 }
533}
534
535static const struct wl_seat_listener seat_listener = {
536 input_handle_capabilities,
537};
538
539static void
540display_add_seat(struct wayland_compositor *c, uint32_t id)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100541{
542 struct wayland_input *input;
543
544 input = malloc(sizeof *input);
545 if (input == NULL)
546 return;
547
548 memset(input, 0, sizeof *input);
549
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400550 weston_seat_init(&input->base, &c->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100551 input->compositor = c;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400552 input->seat = wl_registry_bind(c->parent.registry, id,
553 &wl_seat_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100554 wl_list_insert(c->input_list.prev, &input->link);
555
Daniel Stone37816df2012-05-16 18:45:18 +0100556 wl_seat_add_listener(input->seat, &seat_listener, input);
557 wl_seat_set_user_data(input->seat, input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100558}
559
560static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400561registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
562 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100563{
564 struct wayland_compositor *c = data;
565
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200566 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400567 c->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400568 wl_registry_bind(registry, name,
569 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200570 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400571 c->parent.output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400572 wl_registry_bind(registry, name,
573 &wl_output_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100574 wl_output_add_listener(c->parent.output, &output_listener, c);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200575 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400576 c->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400577 wl_registry_bind(registry, name,
578 &wl_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +0100579 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400580 display_add_seat(c, name);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100581 }
582}
583
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400584static const struct wl_registry_listener registry_listener = {
585 registry_handle_global
586};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100587
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400588static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100589wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
590{
591 struct wayland_compositor *c = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -0400592 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100593
594 if (mask & WL_EVENT_READABLE)
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -0400595 count = wl_display_dispatch(c->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -0500596 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400597 wl_display_flush(c->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400598
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -0400599 if (mask == 0) {
600 count = wl_display_dispatch_pending(c->parent.wl_display);
601 wl_display_flush(c->parent.wl_display);
602 }
603
604 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100605}
606
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500607static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -0400608wayland_restore(struct weston_compositor *ec)
609{
610}
611
612static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500613wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500614{
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +0300615 ec->renderer->destroy(ec);
Kristian Høgsberg3a0de882012-09-06 21:44:24 -0400616
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500617 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -0700618
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500619 free(ec);
620}
621
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500622static struct weston_compositor *
Kristian Høgsberg2e28b102012-02-07 09:57:25 -0500623wayland_compositor_create(struct wl_display *display,
Daniel Stonebaf43592012-06-01 11:11:10 -0400624 int width, int height, const char *display_name,
Daniel Stonec1be8e52012-06-01 11:14:02 -0400625 int argc, char *argv[], const char *config_file)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100626{
627 struct wayland_compositor *c;
628 struct wl_event_loop *loop;
629 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100630
631 c = malloc(sizeof *c);
632 if (c == NULL)
633 return NULL;
634
635 memset(c, 0, sizeof *c);
636
Daniel Stone725c2c32012-06-22 14:04:36 +0100637 if (weston_compositor_init(&c->base, display, argc, argv,
638 config_file) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200639 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +0100640
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400641 c->parent.wl_display = wl_display_connect(display_name);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100642
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400643 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200644 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +0200645 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100646 }
647
648 wl_list_init(&c->input_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400649 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
650 wl_registry_add_listener(c->parent.registry, &registry_listener, c);
651 wl_display_dispatch(c->parent.wl_display);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100652
653 c->base.wl_display = display;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100654 if (gl_renderer_create(&c->base, c->parent.wl_display,
655 gl_renderer_alpha_attribs,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100656 NULL) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200657 goto err_display;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100658
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100659 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -0400660 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100661
John Kåre Alsakerc37b32d2012-10-04 20:42:17 +0200662 c->border.top = 30;
663 c->border.bottom = 24;
John Kåre Alsakerb9b87122012-10-04 20:42:16 +0200664 c->border.left = 25;
John Kåre Alsakerc37b32d2012-10-04 20:42:17 +0200665 c->border.right = 26;
John Kåre Alsakerb9b87122012-10-04 20:42:16 +0200666
667 /* requires border fields */
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100668 if (wayland_compositor_create_output(c, width, height) < 0)
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100669 goto err_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100670
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100671 /* requires gl_renderer_output_state_create called
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100672 * by wayland_compositor_create_output */
John Kåre Alsaker4b3081a2012-10-03 18:02:22 +0200673 create_border(c);
674
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100675 loop = wl_display_get_event_loop(c->base.wl_display);
676
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400677 fd = wl_display_get_fd(c->parent.wl_display);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100678 c->parent.wl_source =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400679 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100680 wayland_compositor_handle_event, c);
681 if (c->parent.wl_source == NULL)
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100682 goto err_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100683
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -0400684 wl_event_source_check(c->parent.wl_source);
685
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100686 return &c->base;
Martin Olssonc5db50f2012-07-08 03:03:43 +0200687
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100688err_gl:
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +0300689 c->base.renderer->destroy(&c->base);
Martin Olssonc5db50f2012-07-08 03:03:43 +0200690err_display:
691 wl_display_disconnect(c->parent.wl_display);
692err_compositor:
693 weston_compositor_shutdown(&c->base);
694err_free:
695 free(c);
696 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100697}
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400698
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500699WL_EXPORT struct weston_compositor *
Daniel Stonec1be8e52012-06-01 11:14:02 -0400700backend_init(struct wl_display *display, int argc, char *argv[],
701 const char *config_file)
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400702{
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400703 int width = 1024, height = 640;
704 char *display_name = NULL;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400705
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400706 const struct weston_option wayland_options[] = {
707 { WESTON_OPTION_INTEGER, "width", 0, &width },
708 { WESTON_OPTION_INTEGER, "height", 0, &height },
709 { WESTON_OPTION_STRING, "display", 0, &display_name },
710 };
711
712 parse_options(wayland_options,
713 ARRAY_LENGTH(wayland_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400714
Daniel Stonebaf43592012-06-01 11:11:10 -0400715 return wayland_compositor_create(display, width, height, display_name,
Daniel Stonec1be8e52012-06-01 11:14:02 -0400716 argc, argv, config_file);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400717}