blob: d9d7530c8e3d8d8ebf9d201c6a87d8ed6b413ca5 [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"
Jonas Ådahle5a12252013-04-05 23:07:11 +020042#include "../shared/os-compatibility.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010043
44struct wayland_compositor {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050045 struct weston_compositor base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010046
47 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040048 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040049 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010050 struct wl_compositor *compositor;
51 struct wl_shell *shell;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010052 struct wl_output *output;
Jonas Ådahle5a12252013-04-05 23:07:11 +020053 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010054
55 struct {
56 int32_t x, y, width, height;
57 } screen_allocation;
58
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010059 struct wl_event_source *wl_source;
60 uint32_t event_mask;
61 } parent;
62
Kristian Høgsberg546a8122012-02-01 07:45:51 -050063 struct {
64 int32_t top, bottom, left, right;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050065 } border;
66
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010067 struct wl_list input_list;
68};
69
70struct wayland_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050071 struct weston_output base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010072 struct {
Jonas Ådahle5a12252013-04-05 23:07:11 +020073 int draw_initial_frame;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010074 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020075 struct wl_shell_surface *shell_surface;
Benjamin Franzkebe014562011-02-18 17:04:24 +010076 struct wl_egl_window *egl_window;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010077 } parent;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050078 struct weston_mode mode;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010079};
80
81struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -040082 struct weston_seat base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010083 struct wayland_compositor *compositor;
Daniel Stone37816df2012-05-16 18:45:18 +010084 struct wl_seat *seat;
85 struct wl_pointer *pointer;
86 struct wl_keyboard *keyboard;
87 struct wl_touch *touch;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010088 struct wl_list link;
Daniel Stone50692802012-06-22 13:21:41 +010089 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -040090 uint32_t enter_serial;
91 int focus;
92 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010093};
94
Kristian Høgsberg546a8122012-02-01 07:45:51 -050095static void
96create_border(struct wayland_compositor *c)
97{
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +020098 pixman_image_t *image;
John Kåre Alsaker44154502012-11-13 19:10:20 +010099 int32_t edges[4];
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500100
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200101 image = load_image(DATADIR "/weston/border.png");
102 if (!image) {
Martin Minarik6d118362012-06-07 18:01:59 +0200103 weston_log("could'nt load border image\n");
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500104 return;
105 }
106
John Kåre Alsaker44154502012-11-13 19:10:20 +0100107 edges[0] = c->border.left;
108 edges[1] = c->border.right;
109 edges[2] = c->border.top;
110 edges[3] = c->border.bottom;
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200111
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100112 gl_renderer_set_border(&c->base, pixman_image_get_width(image),
John Kåre Alsaker44154502012-11-13 19:10:20 +0100113 pixman_image_get_height(image),
114 pixman_image_get_data(image), edges);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500115
Ander Conselvan de Oliveirae2d21e82012-03-16 17:25:09 +0200116 pixman_image_unref(image);
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500117}
118
Kristian Høgsberg68c479a2012-01-25 23:32:28 -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
157 width = output->mode.width + c->border.left + c->border.right;
158 height = output->mode.height + c->border.top + c->border.bottom;
159 stride = width * 4;
160 size = height * stride;
161
162 fd = os_create_anonymous_file(size);
163 if (fd < 0) {
164 perror("os_create_anonymous_file");
165 return;
166 }
167
168 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
169 if (data == MAP_FAILED) {
170 perror("mmap");
171 close(fd);
172 return;
173 }
174
175 pool = wl_shm_create_pool(shm, fd, size);
176
177 buffer = wl_shm_pool_create_buffer(pool, 0,
178 width, height,
179 stride,
180 WL_SHM_FORMAT_ARGB8888);
181 wl_buffer_add_listener(buffer, &buffer_listener, buffer);
182 wl_shm_pool_destroy(pool);
183 close(fd);
184
185 memset(data, 0, size);
186
187 wl_surface_attach(surface, buffer, 0, 0);
188
189 /* We only need to damage some part, as its only transparant
190 * pixels anyway. */
191 wl_surface_damage(surface, 0, 0, 1, 1);
192}
193
194static void
195wayland_output_start_repaint_loop(struct weston_output *output_base)
196{
197 struct wayland_output *output = (struct wayland_output *) output_base;
198 struct wl_callback *callback;
199
200 /* If this is the initial frame, we need to attach a buffer so that
201 * the compositor can map the surface and include it in its render
202 * loop. If the surface doesn't end up in the render loop, the frame
203 * callback won't be invoked. The buffer is transparent and of the
204 * same size as the future real output buffer. */
205 if (output->parent.draw_initial_frame) {
206 output->parent.draw_initial_frame = 0;
207
208 draw_initial_frame(output);
209 }
210
211 callback = wl_surface_frame(output->parent.surface);
212 wl_callback_add_listener(callback, &frame_listener, output);
213 wl_surface_commit(output->parent.surface);
214}
215
216static void
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400217wayland_output_repaint(struct weston_output *output_base,
218 pixman_region32_t *damage)
219{
220 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400221 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400222 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600223
Kristian Høgsberg33418202011-08-16 23:01:28 -0400224 callback = wl_surface_frame(output->parent.surface);
225 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100226
Pekka Paalanenbc106382012-10-10 12:49:31 +0300227 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200228
229 pixman_region32_subtract(&ec->primary_plane.damage,
230 &ec->primary_plane.damage, damage);
231
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100232}
233
Matt Roper361d2ad2011-08-29 13:52:23 -0700234static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500235wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700236{
237 struct wayland_output *output = (struct wayland_output *) output_base;
Matt Roper361d2ad2011-08-29 13:52:23 -0700238
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100239 gl_renderer_output_destroy(output_base);
John Kåre Alsaker94659272012-11-13 19:10:18 +0100240
Matt Roper361d2ad2011-08-29 13:52:23 -0700241 wl_egl_window_destroy(output->parent.egl_window);
242 free(output);
243
244 return;
245}
246
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300247static const struct wl_shell_surface_listener shell_surface_listener;
248
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200249static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100250wayland_compositor_create_output(struct wayland_compositor *c,
251 int width, int height)
252{
253 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100254
255 output = malloc(sizeof *output);
256 if (output == NULL)
257 return -1;
258 memset(output, 0, sizeof *output);
259
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400260 output->mode.flags =
261 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
262 output->mode.width = width;
263 output->mode.height = height;
264 output->mode.refresh = 60;
265 wl_list_init(&output->base.mode_list);
266 wl_list_insert(&output->base.mode_list, &output->mode.link);
267
268 output->base.current = &output->mode;
Scott Moreau1bad5db2012-08-18 01:04:05 -0600269 weston_output_init(&output->base, &c->base, 0, 0, width, height,
270 WL_OUTPUT_TRANSFORM_NORMAL);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400271
Kristian Høgsberg90bc88c2012-08-13 23:34:04 -0400272 output->base.make = "waywayland";
273 output->base.model = "none";
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500274
275 weston_output_move(&output->base, 0, 0);
276
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100277 output->parent.surface =
278 wl_compositor_create_surface(c->parent.compositor);
279 wl_surface_set_user_data(output->parent.surface, output);
280
Benjamin Franzkebe014562011-02-18 17:04:24 +0100281 output->parent.egl_window =
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500282 wl_egl_window_create(output->parent.surface,
283 width + c->border.left + c->border.right,
284 height + c->border.top + c->border.bottom);
Benjamin Franzkebe014562011-02-18 17:04:24 +0100285 if (!output->parent.egl_window) {
Martin Minarik6d118362012-06-07 18:01:59 +0200286 weston_log("failure to create wl_egl_window\n");
Benjamin Franzkebe014562011-02-18 17:04:24 +0100287 goto cleanup_output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100288 }
289
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100290 if (gl_renderer_output_create(&output->base,
John Kåre Alsaker94659272012-11-13 19:10:18 +0100291 output->parent.egl_window) < 0)
Benjamin Franzkebe014562011-02-18 17:04:24 +0100292 goto cleanup_window;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100293
Jonas Ådahle5a12252013-04-05 23:07:11 +0200294 output->parent.draw_initial_frame = 1;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200295 output->parent.shell_surface =
296 wl_shell_get_shell_surface(c->parent.shell,
297 output->parent.surface);
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300298 wl_shell_surface_add_listener(output->parent.shell_surface,
299 &shell_surface_listener, output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200300 wl_shell_surface_set_toplevel(output->parent.shell_surface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100301
Alex Wubd3354b2012-04-17 17:20:49 +0800302 output->base.origin = output->base.current;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200303 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500304 output->base.repaint = wayland_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -0700305 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800306 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200307 output->base.set_backlight = NULL;
308 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800309 output->base.switch_mode = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100310
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100311 wl_list_insert(c->base.output_list.prev, &output->base.link);
312
313 return 0;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100314
Benjamin Franzkebe014562011-02-18 17:04:24 +0100315cleanup_window:
316 wl_egl_window_destroy(output->parent.egl_window);
317cleanup_output:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500318 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +0100319 free(output);
320
321 return -1;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100322}
323
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300324static void
325shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
326 uint32_t serial)
327{
328 wl_shell_surface_pong(shell_surface, serial);
329}
330
331static void
332shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
333 uint32_t edges, int32_t width, int32_t height)
334{
335 /* FIXME: implement resizing */
336}
337
338static void
339shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
340{
341}
342
343static const struct wl_shell_surface_listener shell_surface_listener = {
344 shell_surface_ping,
345 shell_surface_configure,
346 shell_surface_popup_done
347};
348
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100349/* Events received from the wayland-server this compositor is client of: */
350
351/* parent output interface */
352static void
353display_handle_geometry(void *data,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400354 struct wl_output *wl_output,
355 int x,
356 int y,
357 int physical_width,
358 int physical_height,
359 int subpixel,
360 const char *make,
Kristian Høgsberg0e696472012-07-22 15:49:57 -0400361 const char *model,
362 int transform)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100363{
364 struct wayland_compositor *c = data;
365
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500366 c->parent.screen_allocation.x = x;
367 c->parent.screen_allocation.y = y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400368}
369
370static void
371display_handle_mode(void *data,
372 struct wl_output *wl_output,
373 uint32_t flags,
374 int width,
375 int height,
376 int refresh)
377{
378 struct wayland_compositor *c = data;
379
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100380 c->parent.screen_allocation.width = width;
381 c->parent.screen_allocation.height = height;
382}
383
384static const struct wl_output_listener output_listener = {
385 display_handle_geometry,
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400386 display_handle_mode
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100387};
388
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400389static void
390check_focus(struct wayland_input *input, wl_fixed_t x, wl_fixed_t y)
391{
392 struct wayland_compositor *c = input->compositor;
393 int width, height, inside;
394
395 width = input->output->mode.width;
396 height = input->output->mode.height;
397
398 inside = c->border.left <= wl_fixed_to_int(x) &&
399 wl_fixed_to_int(x) < width + c->border.left &&
400 c->border.top <= wl_fixed_to_int(y) &&
401 wl_fixed_to_int(y) < height + c->border.top;
402
403 if (!input->focus && inside) {
404 notify_pointer_focus(&input->base, &input->output->base,
405 x - wl_fixed_from_int(c->border.left),
406 y = wl_fixed_from_int(c->border.top));
407 wl_pointer_set_cursor(input->pointer,
408 input->enter_serial, NULL, 0, 0);
409 } else if (input->focus && !inside) {
410 notify_pointer_focus(&input->base, NULL, 0, 0);
411 /* FIXME: Should set default cursor here. */
412 }
413
414 input->focus = inside;
415}
416
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100417/* parent input interface */
418static void
Daniel Stone37816df2012-05-16 18:45:18 +0100419input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
420 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400421 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100422{
423 struct wayland_input *input = data;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100424
Daniel Stone50692802012-06-22 13:21:41 +0100425 /* XXX: If we get a modifier event immediately before the focus,
426 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400427 input->enter_serial = serial;
428 input->output = wl_surface_get_user_data(surface);
429 check_focus(input, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100430}
431
432static void
Daniel Stone37816df2012-05-16 18:45:18 +0100433input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
434 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500435{
436 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500437
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400438 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400439 input->output = NULL;
440 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500441}
442
443static void
Daniel Stone37816df2012-05-16 18:45:18 +0100444input_handle_motion(void *data, struct wl_pointer *pointer,
445 uint32_t time, wl_fixed_t x, wl_fixed_t y)
446{
447 struct wayland_input *input = data;
448 struct wayland_compositor *c = input->compositor;
449
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400450 check_focus(input, x, y);
451 if (input->focus)
452 notify_motion(&input->base, time,
Kristian Høgsberg068b61c2013-02-25 17:04:47 -0500453 x - wl_fixed_from_int(c->border.left) -
454 input->base.seat.pointer->x,
455 y - wl_fixed_from_int(c->border.top) -
456 input->base.seat.pointer->y);
Daniel Stone37816df2012-05-16 18:45:18 +0100457}
458
459static void
460input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100461 uint32_t serial, uint32_t time, uint32_t button,
462 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +0100463{
464 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100465 enum wl_pointer_button_state state = state_w;
Daniel Stone37816df2012-05-16 18:45:18 +0100466
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400467 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +0100468}
469
470static void
471input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100472 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +0100473{
474 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100475
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400476 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +0100477}
478
479static const struct wl_pointer_listener pointer_listener = {
480 input_handle_pointer_enter,
481 input_handle_pointer_leave,
482 input_handle_motion,
483 input_handle_button,
484 input_handle_axis,
485};
486
487static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100488input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
489 int fd, uint32_t size)
490{
491 struct wayland_input *input = data;
492 struct xkb_keymap *keymap;
493 char *map_str;
494
495 if (!data) {
496 close(fd);
497 return;
498 }
499
500 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
501 close(fd);
502 return;
503 }
504
505 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
506 if (map_str == MAP_FAILED) {
507 close(fd);
508 return;
509 }
510
511 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
512 map_str,
513 XKB_KEYMAP_FORMAT_TEXT_V1,
514 0);
515 munmap(map_str, size);
516 close(fd);
517
518 if (!keymap) {
Martin Minarik6d118362012-06-07 18:01:59 +0200519 weston_log("failed to compile keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100520 return;
521 }
522
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400523 weston_seat_init_keyboard(&input->base, keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100524 xkb_map_unref(keymap);
525}
526
527static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500528input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +0100529 struct wl_keyboard *keyboard,
530 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500531 struct wl_surface *surface,
532 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100533{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500534 struct wayland_input *input = data;
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -0500535
Daniel Stone50692802012-06-22 13:21:41 +0100536 /* XXX: If we get a modifier event immediately before the focus,
537 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400538 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +0100539 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500540}
541
542static void
543input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +0100544 struct wl_keyboard *keyboard,
545 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500546 struct wl_surface *surface)
547{
548 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500549
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400550 notify_keyboard_focus_out(&input->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100551}
552
Daniel Stone37816df2012-05-16 18:45:18 +0100553static void
554input_handle_key(void *data, struct wl_keyboard *keyboard,
555 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
556{
557 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100558
Daniel Stone50692802012-06-22 13:21:41 +0100559 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400560 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +0100561 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100562 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stone50692802012-06-22 13:21:41 +0100563 STATE_UPDATE_NONE);
Daniel Stone37816df2012-05-16 18:45:18 +0100564}
565
Daniel Stone351eb612012-05-31 15:27:47 -0400566static void
567input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +0100568 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -0400569 uint32_t mods_latched, uint32_t mods_locked,
570 uint32_t group)
571{
Daniel Stone50692802012-06-22 13:21:41 +0100572 struct wayland_input *input = data;
573 struct wayland_compositor *c = input->compositor;
574 uint32_t serial_out;
575
576 /* If we get a key event followed by a modifier event with the
577 * same serial number, then we try to preserve those semantics by
578 * reusing the same serial number on the way out too. */
579 if (serial_in == input->key_serial)
580 serial_out = wl_display_get_serial(c->base.wl_display);
581 else
582 serial_out = wl_display_next_serial(c->base.wl_display);
583
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400584 xkb_state_update_mask(input->base.xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +0100585 mods_depressed, mods_latched,
586 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400587 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -0400588}
589
Daniel Stone37816df2012-05-16 18:45:18 +0100590static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100591 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500592 input_handle_keyboard_enter,
593 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +0100594 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -0400595 input_handle_modifiers,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100596};
597
598static void
Daniel Stone37816df2012-05-16 18:45:18 +0100599input_handle_capabilities(void *data, struct wl_seat *seat,
600 enum wl_seat_capability caps)
601{
602 struct wayland_input *input = data;
603
604 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
605 input->pointer = wl_seat_get_pointer(seat);
606 wl_pointer_set_user_data(input->pointer, input);
607 wl_pointer_add_listener(input->pointer, &pointer_listener,
608 input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400609 weston_seat_init_pointer(&input->base);
Daniel Stone37816df2012-05-16 18:45:18 +0100610 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
611 wl_pointer_destroy(input->pointer);
612 input->pointer = NULL;
613 }
614
615 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
616 input->keyboard = wl_seat_get_keyboard(seat);
617 wl_keyboard_set_user_data(input->keyboard, input);
618 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
619 input);
620 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
621 wl_keyboard_destroy(input->keyboard);
622 input->keyboard = NULL;
623 }
624}
625
626static const struct wl_seat_listener seat_listener = {
627 input_handle_capabilities,
628};
629
630static void
631display_add_seat(struct wayland_compositor *c, uint32_t id)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100632{
633 struct wayland_input *input;
634
635 input = malloc(sizeof *input);
636 if (input == NULL)
637 return;
638
639 memset(input, 0, sizeof *input);
640
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400641 weston_seat_init(&input->base, &c->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100642 input->compositor = c;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400643 input->seat = wl_registry_bind(c->parent.registry, id,
644 &wl_seat_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100645 wl_list_insert(c->input_list.prev, &input->link);
646
Daniel Stone37816df2012-05-16 18:45:18 +0100647 wl_seat_add_listener(input->seat, &seat_listener, input);
648 wl_seat_set_user_data(input->seat, input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100649}
650
651static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400652registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
653 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100654{
655 struct wayland_compositor *c = data;
656
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200657 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400658 c->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400659 wl_registry_bind(registry, name,
660 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200661 } else if (strcmp(interface, "wl_output") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400662 c->parent.output =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400663 wl_registry_bind(registry, name,
664 &wl_output_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100665 wl_output_add_listener(c->parent.output, &output_listener, c);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +0200666 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400667 c->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400668 wl_registry_bind(registry, name,
669 &wl_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +0100670 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400671 display_add_seat(c, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200672 } else if (strcmp(interface, "wl_shm") == 0) {
673 c->parent.shm =
674 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100675 }
676}
677
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400678static const struct wl_registry_listener registry_listener = {
679 registry_handle_global
680};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100681
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400682static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100683wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
684{
685 struct wayland_compositor *c = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -0400686 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100687
688 if (mask & WL_EVENT_READABLE)
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -0400689 count = wl_display_dispatch(c->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -0500690 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400691 wl_display_flush(c->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -0400692
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -0400693 if (mask == 0) {
694 count = wl_display_dispatch_pending(c->parent.wl_display);
695 wl_display_flush(c->parent.wl_display);
696 }
697
698 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100699}
700
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500701static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -0400702wayland_restore(struct weston_compositor *ec)
703{
704}
705
706static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500707wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500708{
Jonas Ådahle5a12252013-04-05 23:07:11 +0200709 struct wayland_compositor *c = (struct wayland_compositor *) ec;
710
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +0300711 ec->renderer->destroy(ec);
Kristian Høgsberg3a0de882012-09-06 21:44:24 -0400712
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500713 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -0700714
Jonas Ådahle5a12252013-04-05 23:07:11 +0200715 if (c->parent.shm)
716 wl_shm_destroy(c->parent.shm);
717
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500718 free(ec);
719}
720
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500721static struct weston_compositor *
Kristian Høgsberg2e28b102012-02-07 09:57:25 -0500722wayland_compositor_create(struct wl_display *display,
Daniel Stonebaf43592012-06-01 11:11:10 -0400723 int width, int height, const char *display_name,
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500724 int *argc, char *argv[], const char *config_file)
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
730 c = malloc(sizeof *c);
731 if (c == NULL)
732 return NULL;
733
734 memset(c, 0, sizeof *c);
735
Daniel Stone725c2c32012-06-22 14:04:36 +0100736 if (weston_compositor_init(&c->base, display, argc, argv,
737 config_file) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200738 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +0100739
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400740 c->parent.wl_display = wl_display_connect(display_name);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100741
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400742 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200743 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +0200744 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100745 }
746
747 wl_list_init(&c->input_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400748 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
749 wl_registry_add_listener(c->parent.registry, &registry_listener, c);
750 wl_display_dispatch(c->parent.wl_display);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100751
752 c->base.wl_display = display;
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100753 if (gl_renderer_create(&c->base, c->parent.wl_display,
754 gl_renderer_alpha_attribs,
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100755 NULL) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +0200756 goto err_display;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100757
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100758 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -0400759 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +0100760
John Kåre Alsakerc37b32d2012-10-04 20:42:17 +0200761 c->border.top = 30;
762 c->border.bottom = 24;
John Kåre Alsakerb9b87122012-10-04 20:42:16 +0200763 c->border.left = 25;
John Kåre Alsakerc37b32d2012-10-04 20:42:17 +0200764 c->border.right = 26;
John Kåre Alsakerb9b87122012-10-04 20:42:16 +0200765
766 /* requires border fields */
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100767 if (wayland_compositor_create_output(c, width, height) < 0)
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100768 goto err_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100769
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100770 /* requires gl_renderer_output_state_create called
John Kåre Alsaker1b7ad162012-11-13 19:10:19 +0100771 * by wayland_compositor_create_output */
John Kåre Alsaker4b3081a2012-10-03 18:02:22 +0200772 create_border(c);
773
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100774 loop = wl_display_get_event_loop(c->base.wl_display);
775
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400776 fd = wl_display_get_fd(c->parent.wl_display);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100777 c->parent.wl_source =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400778 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100779 wayland_compositor_handle_event, c);
780 if (c->parent.wl_source == NULL)
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100781 goto err_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100782
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -0400783 wl_event_source_check(c->parent.wl_source);
784
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100785 return &c->base;
Martin Olssonc5db50f2012-07-08 03:03:43 +0200786
John Kåre Alsaker779b52a2012-11-13 19:10:29 +0100787err_gl:
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +0300788 c->base.renderer->destroy(&c->base);
Martin Olssonc5db50f2012-07-08 03:03:43 +0200789err_display:
790 wl_display_disconnect(c->parent.wl_display);
791err_compositor:
792 weston_compositor_shutdown(&c->base);
793err_free:
794 free(c);
795 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100796}
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400797
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500798WL_EXPORT struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500799backend_init(struct wl_display *display, int *argc, char *argv[],
Daniel Stonec1be8e52012-06-01 11:14:02 -0400800 const char *config_file)
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400801{
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400802 int width = 1024, height = 640;
803 char *display_name = NULL;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400804
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400805 const struct weston_option wayland_options[] = {
806 { WESTON_OPTION_INTEGER, "width", 0, &width },
807 { WESTON_OPTION_INTEGER, "height", 0, &height },
808 { WESTON_OPTION_STRING, "display", 0, &display_name },
809 };
810
811 parse_options(wayland_options,
812 ARRAY_LENGTH(wayland_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400813
Daniel Stonebaf43592012-06-01 11:11:10 -0400814 return wayland_compositor_create(display, width, height, display_name,
Daniel Stonec1be8e52012-06-01 11:14:02 -0400815 argc, argv, config_file);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400816}