blob: 5f691a7805a33b35f241ed3ffa5db2638287c6f4 [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
Jason Ekstrandff2fd462013-10-27 22:24:58 -05003 * Copyright © 2013 Jason Ekstrand
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01004 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04005 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010014 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040015 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010022 */
23
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010025
26#include <stddef.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <fcntl.h>
31#include <unistd.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010032#include <sys/mman.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010033
Benjamin Franzkebe014562011-02-18 17:04:24 +010034#include <wayland-client.h>
35#include <wayland-egl.h>
Jason Ekstrand7744f712013-10-27 22:24:55 -050036#include <wayland-cursor.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010037
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010038#include "compositor.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010039#include "gl-renderer.h"
Jason Ekstrandff2fd462013-10-27 22:24:58 -050040#include "pixman-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"
Jason Ekstrand7744f712013-10-27 22:24:55 -050043#include "../shared/cairo-util.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010044
Jason Ekstrand48ce4212013-10-27 22:25:02 -050045#define WINDOW_TITLE "Weston Compositor"
46
47struct wayland_backend_options {
48 int width, height;
49 char *display_name;
50 int use_pixman;
51 int count;
52};
53
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010054struct wayland_compositor {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050055 struct weston_compositor base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010056
57 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040058 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040059 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010060 struct wl_compositor *compositor;
61 struct wl_shell *shell;
Jonas Ådahle5a12252013-04-05 23:07:11 +020062 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010063
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010064 struct wl_event_source *wl_source;
65 uint32_t event_mask;
66 } parent;
67
Jason Ekstrandff2fd462013-10-27 22:24:58 -050068 int use_pixman;
69
Jason Ekstrand7744f712013-10-27 22:24:55 -050070 struct theme *theme;
71 cairo_device_t *frame_device;
72 struct wl_cursor_theme *cursor_theme;
73 struct wl_cursor *cursor;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050074
Jason Ekstrand06ced802013-11-07 20:13:29 -060075 struct wl_list input_list;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010076};
77
78struct wayland_output {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050079 struct weston_output base;
80
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010081 struct {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050082 int draw_initial_frame;
83 struct wl_surface *surface;
84 struct wl_shell_surface *shell_surface;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010085 } parent;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050086
Jason Ekstrand7744f712013-10-27 22:24:55 -050087 int keyboard_count;
88
89 struct frame *frame;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050090
Jason Ekstrand7744f712013-10-27 22:24:55 -050091 struct {
Jason Ekstrandff2fd462013-10-27 22:24:58 -050092 struct wl_egl_window *egl_window;
93 struct {
94 cairo_surface_t *top;
95 cairo_surface_t *left;
96 cairo_surface_t *right;
97 cairo_surface_t *bottom;
98 } border;
99 } gl;
100
101 struct {
102 struct wl_list buffers;
103 struct wl_list free_buffers;
104 } shm;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500105
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500106 struct weston_mode mode;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500107 uint32_t scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100108};
109
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500110struct wayland_shm_buffer {
111 struct wayland_output *output;
112 struct wl_list link;
113 struct wl_list free_link;
114
115 struct wl_buffer *buffer;
116 void *data;
117 size_t size;
118 pixman_region32_t damage;
119 int frame_damaged;
120
121 pixman_image_t *pm_image;
122 cairo_surface_t *c_surface;
123};
124
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100125struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400126 struct weston_seat base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100127 struct wayland_compositor *compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100128 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500129
130 struct {
131 struct wl_seat *seat;
132 struct wl_pointer *pointer;
133 struct wl_keyboard *keyboard;
134 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500135
136 struct {
137 struct wl_surface *surface;
138 int32_t hx, hy;
139 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500140 } parent;
141
Daniel Stone50692802012-06-22 13:21:41 +0100142 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400143 uint32_t enter_serial;
144 int focus;
145 struct wayland_output *output;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500146 struct wayland_output *keyboard_focus;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100147};
148
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300149struct gl_renderer_interface *gl_renderer;
150
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500151static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500152wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
153{
154 cairo_surface_destroy(buffer->c_surface);
155 pixman_image_unref(buffer->pm_image);
156
157 wl_buffer_destroy(buffer->buffer);
158 munmap(buffer->data, buffer->size);
159
160 pixman_region32_fini(&buffer->damage);
161
162 wl_list_remove(&buffer->link);
163 wl_list_remove(&buffer->free_link);
164 free(buffer);
165}
166
167static void
168buffer_release(void *data, struct wl_buffer *buffer)
169{
170 struct wayland_shm_buffer *sb = data;
171
172 if (sb->output) {
173 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
174 } else {
175 wayland_shm_buffer_destroy(sb);
176 }
177}
178
179static const struct wl_buffer_listener buffer_listener = {
180 buffer_release
181};
182
183static struct wayland_shm_buffer *
184wayland_output_get_shm_buffer(struct wayland_output *output)
185{
186 struct wayland_compositor *c =
187 (struct wayland_compositor *) output->base.compositor;
188 struct wl_shm *shm = c->parent.shm;
189 struct wayland_shm_buffer *sb;
190
191 struct wl_shm_pool *pool;
192 int width, height, stride;
193 int32_t fx, fy;
194 int fd;
195 unsigned char *data;
196
197 if (!wl_list_empty(&output->shm.free_buffers)) {
198 sb = container_of(output->shm.free_buffers.next,
199 struct wayland_shm_buffer, free_link);
200 wl_list_remove(&sb->free_link);
201 wl_list_init(&sb->free_link);
202
203 return sb;
204 }
205
206 if (output->frame) {
207 width = frame_width(output->frame);
208 height = frame_height(output->frame);
209 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500210 width = output->base.current_mode->width;
211 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500212 }
213
214 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
215
216 fd = os_create_anonymous_file(height * stride);
217 if (fd < 0) {
218 perror("os_create_anonymous_file");
219 return NULL;
220 }
221
222 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
223 if (data == MAP_FAILED) {
224 perror("mmap");
225 close(fd);
226 return NULL;
227 }
228
229 sb = zalloc(sizeof *sb);
230
231 sb->output = output;
232 wl_list_init(&sb->free_link);
233 wl_list_insert(&output->shm.buffers, &sb->link);
234
235 pixman_region32_init_rect(&sb->damage, 0, 0,
236 output->base.width, output->base.height);
237 sb->frame_damaged = 1;
238
239 sb->data = data;
240 sb->size = height * stride;
241
242 pool = wl_shm_create_pool(shm, fd, sb->size);
243
244 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
245 width, height,
246 stride,
247 WL_SHM_FORMAT_ARGB8888);
248 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
249 wl_shm_pool_destroy(pool);
250 close(fd);
251
252 memset(data, 0, sb->size);
253
254 sb->c_surface =
255 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
256 width, height, stride);
257
258 fx = 0;
259 fy = 0;
260 if (output->frame)
261 frame_interior(output->frame, &fx, &fy, 0, 0);
262 sb->pm_image =
263 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
264 (uint32_t *)(data + fy * stride) + fx,
265 stride);
266
267 return sb;
268}
269
270static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500271frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400272{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500273 struct weston_output *output = data;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400274
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500275 wl_callback_destroy(callback);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500276 weston_output_finish_frame(output, time);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400277}
278
279static const struct wl_callback_listener frame_listener = {
280 frame_done
281};
282
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500283static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200284draw_initial_frame(struct wayland_output *output)
285{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500286 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200287
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500288 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200289
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500290 /* If we are rendering with GL, then orphan it so that it gets
291 * destroyed immediately */
292 if (output->gl.egl_window)
293 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500294
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500295 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200296
297 /* We only need to damage some part, as its only transparant
298 * pixels anyway. */
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500299 wl_surface_damage(output->parent.surface, 0, 0, 1, 1);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200300}
301
302static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500303wayland_output_update_gl_border(struct wayland_output *output)
304{
305 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
306 cairo_t *cr;
307
308 if (!output->frame)
309 return;
310 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
311 return;
312
313 fwidth = frame_width(output->frame);
314 fheight = frame_height(output->frame);
315 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
316
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500317 if (!output->gl.border.top)
318 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500319 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
320 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500321 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500322 frame_repaint(output->frame, cr);
323 cairo_destroy(cr);
324 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
325 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500326 cairo_image_surface_get_stride(output->gl.border.top) / 4,
327 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500328
329
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500330 if (!output->gl.border.left)
331 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500332 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
333 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500334 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500335 cairo_translate(cr, 0, -iy);
336 frame_repaint(output->frame, cr);
337 cairo_destroy(cr);
338 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
339 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500340 cairo_image_surface_get_stride(output->gl.border.left) / 4,
341 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500342
343
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500344 if (!output->gl.border.right)
345 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500346 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
347 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500348 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500349 cairo_translate(cr, -(iwidth + ix), -iy);
350 frame_repaint(output->frame, cr);
351 cairo_destroy(cr);
352 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
353 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500354 cairo_image_surface_get_stride(output->gl.border.right) / 4,
355 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500356
357
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500358 if (!output->gl.border.bottom)
359 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500360 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
361 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500362 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500363 cairo_translate(cr, 0, -(iy + iheight));
364 frame_repaint(output->frame, cr);
365 cairo_destroy(cr);
366 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
367 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500368 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
369 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500370}
371
372static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200373wayland_output_start_repaint_loop(struct weston_output *output_base)
374{
375 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrand286ff682013-10-27 22:24:57 -0500376 struct wayland_compositor *wc =
377 (struct wayland_compositor *)output->base.compositor;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200378 struct wl_callback *callback;
379
380 /* If this is the initial frame, we need to attach a buffer so that
381 * the compositor can map the surface and include it in its render
382 * loop. If the surface doesn't end up in the render loop, the frame
383 * callback won't be invoked. The buffer is transparent and of the
384 * same size as the future real output buffer. */
385 if (output->parent.draw_initial_frame) {
386 output->parent.draw_initial_frame = 0;
387
388 draw_initial_frame(output);
389 }
390
391 callback = wl_surface_frame(output->parent.surface);
392 wl_callback_add_listener(callback, &frame_listener, output);
393 wl_surface_commit(output->parent.surface);
Jason Ekstrand286ff682013-10-27 22:24:57 -0500394 wl_display_flush(wc->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200395}
396
David Herrmann1edf44c2013-10-22 17:11:26 +0200397static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500398wayland_output_repaint_gl(struct weston_output *output_base,
399 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400400{
401 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400402 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400403 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600404
Kristian Høgsberg33418202011-08-16 23:01:28 -0400405 callback = wl_surface_frame(output->parent.surface);
406 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100407
Jason Ekstrand7744f712013-10-27 22:24:55 -0500408 wayland_output_update_gl_border(output);
409
Pekka Paalanenbc106382012-10-10 12:49:31 +0300410 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200411
412 pixman_region32_subtract(&ec->primary_plane.damage,
413 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200414 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100415}
416
Matt Roper361d2ad2011-08-29 13:52:23 -0700417static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500418wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
419{
420 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
421 cairo_t *cr;
422
423 if (!buffer->output->frame || !buffer->frame_damaged)
424 return;
425
426 cr = cairo_create(buffer->c_surface);
427
428 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
429 fwidth = frame_width(buffer->output->frame);
430 fheight = frame_height(buffer->output->frame);
431
432 /* Set the clip so we don't unnecisaraly damage the surface */
433 cairo_move_to(cr, ix, iy);
434 cairo_rel_line_to(cr, iwidth, 0);
435 cairo_rel_line_to(cr, 0, iheight);
436 cairo_rel_line_to(cr, -iwidth, 0);
437 cairo_line_to(cr, ix, iy);
438 cairo_line_to(cr, 0, iy);
439 cairo_line_to(cr, 0, fheight);
440 cairo_line_to(cr, fwidth, fheight);
441 cairo_line_to(cr, fwidth, 0);
442 cairo_line_to(cr, 0, 0);
443 cairo_line_to(cr, 0, iy);
444 cairo_close_path(cr);
445 cairo_clip(cr);
446
447 /* Draw using a pattern so that the final result gets clipped */
448 cairo_push_group(cr);
449 frame_repaint(buffer->output->frame, cr);
450 cairo_pop_group_to_source(cr);
451 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
452 cairo_paint(cr);
453
454 cairo_destroy(cr);
455}
456
457static void
458wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
459{
460 pixman_region32_t damage;
461 pixman_box32_t *rects;
462 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
463 int i, n;
464
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500465 pixman_region32_init(&damage);
466 weston_transformed_region(sb->output->base.width,
467 sb->output->base.height,
468 sb->output->base.transform,
469 sb->output->base.current_scale,
470 &sb->damage, &damage);
471
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500472 if (sb->output->frame) {
473 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
474 fwidth = frame_width(sb->output->frame);
475 fheight = frame_height(sb->output->frame);
476
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500477 pixman_region32_translate(&damage, ix, iy);
478
479 if (sb->frame_damaged) {
480 pixman_region32_union_rect(&damage, &damage,
481 0, 0, fwidth, iy);
482 pixman_region32_union_rect(&damage, &damage,
483 0, iy, ix, iheight);
484 pixman_region32_union_rect(&damage, &damage,
485 ix + iwidth, iy,
486 fwidth - (ix + iwidth), iheight);
487 pixman_region32_union_rect(&damage, &damage,
488 0, iy + iheight,
489 fwidth, fheight - (iy + iheight));
490 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500491 }
492
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500493 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500494 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
495 for (i = 0; i < n; ++i)
496 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
497 rects[i].y1, rects[i].x2 - rects[i].x1,
498 rects[i].y2 - rects[i].y1);
499
500 if (sb->output->frame)
501 pixman_region32_fini(&damage);
502}
503
504static int
505wayland_output_repaint_pixman(struct weston_output *output_base,
506 pixman_region32_t *damage)
507{
508 struct wayland_output *output = (struct wayland_output *) output_base;
509 struct wayland_compositor *c =
510 (struct wayland_compositor *)output->base.compositor;
511 struct wl_callback *callback;
512 struct wayland_shm_buffer *sb;
513
514 if (output->frame) {
515 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
516 wl_list_for_each(sb, &output->shm.buffers, link)
517 sb->frame_damaged = 1;
518 }
519
520 wl_list_for_each(sb, &output->shm.buffers, link)
521 pixman_region32_union(&sb->damage, &sb->damage, damage);
522
523 sb = wayland_output_get_shm_buffer(output);
524
525 wayland_output_update_shm_border(sb);
526 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
527 c->base.renderer->repaint_output(output_base, &sb->damage);
528
529 wayland_shm_buffer_attach(sb);
530
531 callback = wl_surface_frame(output->parent.surface);
532 wl_callback_add_listener(callback, &frame_listener, output);
533 wl_surface_commit(output->parent.surface);
534 wl_display_flush(c->parent.wl_display);
535
536 pixman_region32_fini(&sb->damage);
537 pixman_region32_init(&sb->damage);
538 sb->frame_damaged = 0;
539
540 pixman_region32_subtract(&c->base.primary_plane.damage,
541 &c->base.primary_plane.damage, damage);
542 return 0;
543}
544
545static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500546wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700547{
548 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500549 struct wayland_compositor *c =
550 (struct wayland_compositor *) output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700551
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500552 if (c->use_pixman) {
553 pixman_renderer_output_destroy(output_base);
554 } else {
555 gl_renderer->output_destroy(output_base);
556 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100557
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500558 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500559 wl_surface_destroy(output->parent.surface);
560 wl_shell_surface_destroy(output->parent.shell_surface);
561
562 if (output->frame) {
563 frame_destroy(output->frame);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500564 cairo_surface_destroy(output->gl.border.top);
565 cairo_surface_destroy(output->gl.border.left);
566 cairo_surface_destroy(output->gl.border.right);
567 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500568 }
569
570 weston_output_destroy(&output->base);
Matt Roper361d2ad2011-08-29 13:52:23 -0700571 free(output);
572
573 return;
574}
575
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300576static const struct wl_shell_surface_listener shell_surface_listener;
577
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200578static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500579wayland_output_init_gl_renderer(struct wayland_output *output)
580{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500581 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500582
583 if (output->frame) {
584 fwidth = frame_width(output->frame);
585 fheight = frame_height(output->frame);
586 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500587 fwidth = output->base.current_mode->width;
588 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500589 }
590
591 output->gl.egl_window =
592 wl_egl_window_create(output->parent.surface,
593 fwidth, fheight);
594 if (!output->gl.egl_window) {
595 weston_log("failure to create wl_egl_window\n");
596 return -1;
597 }
598
599 if (gl_renderer->output_create(&output->base,
600 output->gl.egl_window) < 0)
601 goto cleanup_window;
602
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500603 return 0;
604
605cleanup_window:
606 wl_egl_window_destroy(output->gl.egl_window);
607 return -1;
608}
609
610static int
611wayland_output_init_pixman_renderer(struct wayland_output *output)
612{
613 return pixman_renderer_output_create(&output->base);
614}
615
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500616static struct wayland_output *
617wayland_output_create(struct wayland_compositor *c, int x, int y,
618 int width, int height, const char *name,
619 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100620{
621 struct wayland_output *output;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500622 int32_t fx, fy, fwidth, fheight;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500623 int output_width, output_height;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500624 struct wl_region *region;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100625
Peter Huttererf3d62272013-08-08 11:57:05 +1000626 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100627 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500628 return NULL;
629
630 output_width = width * scale;
631 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100632
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400633 output->mode.flags =
634 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500635 output->mode.width = output_width;
636 output->mode.height = output_height;
637 output->mode.refresh = 60000;
638 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400639 wl_list_init(&output->base.mode_list);
640 wl_list_insert(&output->base.mode_list, &output->mode.link);
641
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500642 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
643 width, height, x, y);
644
Hardeningff39efa2013-09-18 23:56:35 +0200645 output->base.current_mode = &output->mode;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500646 weston_output_init(&output->base, &c->base, x, y, width, height,
647 transform, scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400648
Kristian Høgsberg90bc88c2012-08-13 23:34:04 -0400649 output->base.make = "waywayland";
650 output->base.model = "none";
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500651
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500652 wl_list_init(&output->shm.buffers);
653 wl_list_init(&output->shm.free_buffers);
654
Jason Ekstrand7744f712013-10-27 22:24:55 -0500655 if (!c->theme)
656 c->theme = theme_create();
657 output->frame = frame_create(c->theme, width, height,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500658 FRAME_BUTTON_CLOSE, name);
659 frame_resize_inside(output->frame, output_width, output_height);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500660
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100661 output->parent.surface =
662 wl_compositor_create_surface(c->parent.compositor);
663 wl_surface_set_user_data(output->parent.surface, output);
664
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500665 if (c->use_pixman) {
666 if (wayland_output_init_pixman_renderer(output) < 0)
667 goto cleanup_output;
668 output->base.repaint = wayland_output_repaint_pixman;
669 } else {
670 if (wayland_output_init_gl_renderer(output) < 0)
671 goto cleanup_output;
672 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100673 }
674
Jason Ekstrand7744f712013-10-27 22:24:55 -0500675 frame_input_rect(output->frame, &fx, &fy, &fwidth, &fheight);
676 region = wl_compositor_create_region(c->parent.compositor);
677 wl_region_add(region, fx, fy, fwidth, fheight);
678 wl_surface_set_input_region(output->parent.surface, region);
679 wl_region_destroy(region);
680
681 frame_opaque_rect(output->frame, &fx, &fy, &fwidth, &fheight);
682 region = wl_compositor_create_region(c->parent.compositor);
683 wl_region_add(region, fx, fy, fwidth, fheight);
684 wl_surface_set_opaque_region(output->parent.surface, region);
685 wl_region_destroy(region);
686
Jonas Ådahle5a12252013-04-05 23:07:11 +0200687 output->parent.draw_initial_frame = 1;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200688 output->parent.shell_surface =
689 wl_shell_get_shell_surface(c->parent.shell,
690 output->parent.surface);
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300691 wl_shell_surface_add_listener(output->parent.shell_surface,
692 &shell_surface_listener, output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200693 wl_shell_surface_set_toplevel(output->parent.shell_surface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100694
Jonas Ådahle5a12252013-04-05 23:07:11 +0200695 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -0700696 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800697 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200698 output->base.set_backlight = NULL;
699 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800700 output->base.switch_mode = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100701
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100702 wl_list_insert(c->base.output_list.prev, &output->base.link);
703
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500704 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100705
Benjamin Franzkebe014562011-02-18 17:04:24 +0100706cleanup_output:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500707 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +0100708 free(output);
709
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500710 return NULL;
711}
712
713static struct wayland_output *
714wayland_output_create_for_config(struct wayland_compositor *c,
715 struct wayland_backend_options *options,
716 struct weston_config_section *config_section,
717 int32_t x, int32_t y)
718{
719 struct wayland_output *output;
720 char *mode, *t, *name, *str;
721 int width, height, scale;
722 uint32_t transform;
723 unsigned int i, slen;
724
725 static const struct { const char *name; uint32_t token; } transform_names[] = {
726 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
727 { "90", WL_OUTPUT_TRANSFORM_90 },
728 { "180", WL_OUTPUT_TRANSFORM_180 },
729 { "270", WL_OUTPUT_TRANSFORM_270 },
730 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
731 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
732 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
733 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
734 };
735
736 weston_config_section_get_string(config_section, "name", &name, NULL);
737 if (name) {
738 slen = strlen(name);
739 slen += strlen(WINDOW_TITLE " - ");
740 str = malloc(slen + 1);
741 if (str)
742 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
743 free(name);
744 name = str;
745 }
746 if (!name)
747 name = WINDOW_TITLE;
748
749 weston_config_section_get_string(config_section,
750 "mode", &mode, "1024x600");
751 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
752 weston_log("Invalid mode \"%s\" for output %s\n",
753 mode, name);
754 width = 1024;
755 height = 640;
756 }
757 free(mode);
758
759 if (options->width)
760 width = options->width;
761 if (options->height)
762 height = options->height;
763
764 weston_config_section_get_int(config_section, "scale", &scale, 1);
765
766 weston_config_section_get_string(config_section,
767 "transform", &t, "normal");
768 transform = WL_OUTPUT_TRANSFORM_NORMAL;
769 for (i = 0; i < ARRAY_LENGTH(transform_names); i++) {
770 if (strcmp(transform_names[i].name, t) == 0) {
771 transform = transform_names[i].token;
772 break;
773 }
774 }
775 if (i >= ARRAY_LENGTH(transform_names))
776 weston_log("Invalid transform \"%s\" for output %s\n", t, name);
777 free(t);
778
779 output = wayland_output_create(c, x, y, width, height, name, transform, scale);
780 free(name);
781
782 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100783}
784
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300785static void
786shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
787 uint32_t serial)
788{
789 wl_shell_surface_pong(shell_surface, serial);
790}
791
792static void
793shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
794 uint32_t edges, int32_t width, int32_t height)
795{
796 /* FIXME: implement resizing */
797}
798
799static void
800shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
801{
802}
803
804static const struct wl_shell_surface_listener shell_surface_listener = {
805 shell_surface_ping,
806 shell_surface_configure,
807 shell_surface_popup_done
808};
809
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100810/* Events received from the wayland-server this compositor is client of: */
811
Jason Ekstrand7744f712013-10-27 22:24:55 -0500812/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400813static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500814input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400815{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400816
Jason Ekstrand7744f712013-10-27 22:24:55 -0500817 struct wl_buffer *buffer;
818 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400819
Jason Ekstrand7744f712013-10-27 22:24:55 -0500820 if (!input->compositor->cursor)
821 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400822
Jason Ekstrand7744f712013-10-27 22:24:55 -0500823 image = input->compositor->cursor->images[0];
824 buffer = wl_cursor_image_get_buffer(image);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400825
Jason Ekstrand7744f712013-10-27 22:24:55 -0500826
827 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
828 input->parent.cursor.surface,
829 image->hotspot_x, image->hotspot_y);
830
831 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
832 wl_surface_damage(input->parent.cursor.surface, 0, 0,
833 image->width, image->height);
834 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400835}
836
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100837static void
Daniel Stone37816df2012-05-16 18:45:18 +0100838input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
839 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400840 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100841{
842 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500843 int32_t fx, fy;
844 enum theme_location location;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100845
Daniel Stone50692802012-06-22 13:21:41 +0100846 /* XXX: If we get a modifier event immediately before the focus,
847 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400848 input->enter_serial = serial;
849 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500850
851 if (input->output->frame) {
852 location = frame_pointer_enter(input->output->frame, input,
853 wl_fixed_to_int(x),
854 wl_fixed_to_int(y));
855 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
856 x -= wl_fixed_from_int(fx);
857 y -= wl_fixed_from_int(fy);
858
859 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
860 weston_output_schedule_repaint(&input->output->base);
861 } else {
862 location = THEME_LOCATION_CLIENT_AREA;
863 }
864
865 x += wl_fixed_from_int(input->output->base.x);
866 y += wl_fixed_from_int(input->output->base.y);
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500867 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500868
869 if (location == THEME_LOCATION_CLIENT_AREA) {
870 input->focus = 1;
871 notify_pointer_focus(&input->base, &input->output->base, x, y);
872 wl_pointer_set_cursor(input->parent.pointer,
873 input->enter_serial, NULL, 0, 0);
874 } else {
875 input->focus = 0;
876 notify_pointer_focus(&input->base, NULL, 0, 0);
877 input_set_cursor(input);
878 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100879}
880
881static void
Daniel Stone37816df2012-05-16 18:45:18 +0100882input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
883 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500884{
885 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500886
Jason Ekstrand7744f712013-10-27 22:24:55 -0500887 if (input->output->frame) {
888 frame_pointer_leave(input->output->frame, input);
889
890 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
891 weston_output_schedule_repaint(&input->output->base);
892 }
893
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400894 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400895 input->output = NULL;
896 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500897}
898
899static void
Daniel Stone37816df2012-05-16 18:45:18 +0100900input_handle_motion(void *data, struct wl_pointer *pointer,
901 uint32_t time, wl_fixed_t x, wl_fixed_t y)
902{
903 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500904 int32_t fx, fy;
905 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +0100906
Jason Ekstrand7744f712013-10-27 22:24:55 -0500907 if (input->output->frame) {
908 location = frame_pointer_motion(input->output->frame, input,
909 wl_fixed_to_int(x),
910 wl_fixed_to_int(y));
911 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
912 x -= wl_fixed_from_int(fx);
913 y -= wl_fixed_from_int(fy);
914
915 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
916 weston_output_schedule_repaint(&input->output->base);
917 } else {
918 location = THEME_LOCATION_CLIENT_AREA;
919 }
920
921 x += wl_fixed_from_int(input->output->base.x);
922 y += wl_fixed_from_int(input->output->base.y);
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500923 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500924
925 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
926 input_set_cursor(input);
927 notify_pointer_focus(&input->base, NULL, 0, 0);
928 input->focus = 0;
929 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
930 wl_pointer_set_cursor(input->parent.pointer,
931 input->enter_serial, NULL, 0, 0);
932 notify_pointer_focus(&input->base, &input->output->base, x, y);
933 input->focus = 1;
934 }
935
936 if (location == THEME_LOCATION_CLIENT_AREA)
937 notify_motion_absolute(&input->base, time, x, y);
Daniel Stone37816df2012-05-16 18:45:18 +0100938}
939
940static void
941input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100942 uint32_t serial, uint32_t time, uint32_t button,
943 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +0100944{
945 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100946 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500947 enum frame_button_state fstate;
948 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +0100949
Jason Ekstrand7744f712013-10-27 22:24:55 -0500950 if (input->output->frame) {
951 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
952 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
953
954 location = frame_pointer_button(input->output->frame, input,
955 button, fstate);
956
957 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
958
959 wl_shell_surface_move(input->output->parent.shell_surface,
960 input->parent.seat, serial);
961 frame_status_clear(input->output->frame,
962 FRAME_STATUS_MOVE);
963 return;
964 }
965
966 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE)
967 wl_display_terminate(input->compositor->base.wl_display);
968
969 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
970 weston_output_schedule_repaint(&input->output->base);
971 } else {
972 location = THEME_LOCATION_CLIENT_AREA;
973 }
974
975 if (location == THEME_LOCATION_CLIENT_AREA)
976 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +0100977}
978
979static void
980input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100981 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +0100982{
983 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100984
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400985 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +0100986}
987
988static const struct wl_pointer_listener pointer_listener = {
989 input_handle_pointer_enter,
990 input_handle_pointer_leave,
991 input_handle_motion,
992 input_handle_button,
993 input_handle_axis,
994};
995
996static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100997input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
998 int fd, uint32_t size)
999{
1000 struct wayland_input *input = data;
1001 struct xkb_keymap *keymap;
1002 char *map_str;
1003
1004 if (!data) {
1005 close(fd);
1006 return;
1007 }
1008
1009 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1010 close(fd);
1011 return;
1012 }
1013
1014 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1015 if (map_str == MAP_FAILED) {
1016 close(fd);
1017 return;
1018 }
1019
1020 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
1021 map_str,
1022 XKB_KEYMAP_FORMAT_TEXT_V1,
1023 0);
1024 munmap(map_str, size);
1025 close(fd);
1026
1027 if (!keymap) {
Martin Minarik6d118362012-06-07 18:01:59 +02001028 weston_log("failed to compile keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001029 return;
1030 }
1031
Rui Matos0c194ce2013-10-10 19:44:21 +02001032 if (input->base.keyboard)
1033 weston_seat_update_keymap(&input->base, keymap);
1034 else
1035 weston_seat_init_keyboard(&input->base, keymap);
1036
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001037 xkb_map_unref(keymap);
1038}
1039
1040static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001041input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001042 struct wl_keyboard *keyboard,
1043 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001044 struct wl_surface *surface,
1045 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001046{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001047 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001048 struct wayland_output *focus;
1049
1050 focus = input->keyboard_focus;
1051 if (focus) {
1052 /* This shouldn't happen */
1053 focus->keyboard_count--;
1054 if (!focus->keyboard_count && focus->frame)
1055 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1056 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1057 weston_output_schedule_repaint(&focus->base);
1058 }
1059
1060 input->keyboard_focus = wl_surface_get_user_data(surface);
1061 input->keyboard_focus->keyboard_count++;
1062
1063 focus = input->keyboard_focus;
1064 if (focus->frame) {
1065 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1066 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1067 weston_output_schedule_repaint(&focus->base);
1068 }
1069
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001070
Daniel Stone50692802012-06-22 13:21:41 +01001071 /* XXX: If we get a modifier event immediately before the focus,
1072 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001073 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001074 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001075}
1076
1077static void
1078input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001079 struct wl_keyboard *keyboard,
1080 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001081 struct wl_surface *surface)
1082{
1083 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001084 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001085
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001086 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001087
1088 focus = input->keyboard_focus;
1089 if (!focus)
1090 return; /* This shouldn't happen */
1091
1092 focus->keyboard_count--;
1093 if (!focus->keyboard_count && focus->frame) {
1094 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1095 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1096 weston_output_schedule_repaint(&focus->base);
1097 }
1098
1099 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001100}
1101
Daniel Stone37816df2012-05-16 18:45:18 +01001102static void
1103input_handle_key(void *data, struct wl_keyboard *keyboard,
1104 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1105{
1106 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001107
Daniel Stone50692802012-06-22 13:21:41 +01001108 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001109 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001110 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001111 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stone50692802012-06-22 13:21:41 +01001112 STATE_UPDATE_NONE);
Daniel Stone37816df2012-05-16 18:45:18 +01001113}
1114
Daniel Stone351eb612012-05-31 15:27:47 -04001115static void
1116input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001117 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001118 uint32_t mods_latched, uint32_t mods_locked,
1119 uint32_t group)
1120{
Daniel Stone50692802012-06-22 13:21:41 +01001121 struct wayland_input *input = data;
1122 struct wayland_compositor *c = input->compositor;
1123 uint32_t serial_out;
1124
1125 /* If we get a key event followed by a modifier event with the
1126 * same serial number, then we try to preserve those semantics by
1127 * reusing the same serial number on the way out too. */
1128 if (serial_in == input->key_serial)
1129 serial_out = wl_display_get_serial(c->base.wl_display);
1130 else
1131 serial_out = wl_display_next_serial(c->base.wl_display);
1132
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001133 xkb_state_update_mask(input->base.xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001134 mods_depressed, mods_latched,
1135 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001136 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001137}
1138
Daniel Stone37816df2012-05-16 18:45:18 +01001139static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001140 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001141 input_handle_keyboard_enter,
1142 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001143 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001144 input_handle_modifiers,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001145};
1146
1147static void
Daniel Stone37816df2012-05-16 18:45:18 +01001148input_handle_capabilities(void *data, struct wl_seat *seat,
1149 enum wl_seat_capability caps)
1150{
1151 struct wayland_input *input = data;
1152
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001153 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1154 input->parent.pointer = wl_seat_get_pointer(seat);
1155 wl_pointer_set_user_data(input->parent.pointer, input);
1156 wl_pointer_add_listener(input->parent.pointer,
1157 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001158 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001159 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1160 wl_pointer_destroy(input->parent.pointer);
1161 input->parent.pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001162 }
1163
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001164 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1165 input->parent.keyboard = wl_seat_get_keyboard(seat);
1166 wl_keyboard_set_user_data(input->parent.keyboard, input);
1167 wl_keyboard_add_listener(input->parent.keyboard,
1168 &keyboard_listener, input);
1169 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1170 wl_keyboard_destroy(input->parent.keyboard);
1171 input->parent.keyboard = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001172 }
1173}
1174
1175static const struct wl_seat_listener seat_listener = {
1176 input_handle_capabilities,
1177};
1178
1179static void
1180display_add_seat(struct wayland_compositor *c, uint32_t id)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001181{
1182 struct wayland_input *input;
1183
Peter Huttererf3d62272013-08-08 11:57:05 +10001184 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001185 if (input == NULL)
1186 return;
1187
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001188 weston_seat_init(&input->base, &c->base, "default");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001189 input->compositor = c;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001190 input->parent.seat = wl_registry_bind(c->parent.registry, id,
1191 &wl_seat_interface, 1);
Jason Ekstrand06ced802013-11-07 20:13:29 -06001192 wl_list_insert(c->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001193
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001194 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1195 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001196
1197 input->parent.cursor.surface =
1198 wl_compositor_create_surface(c->parent.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001199}
1200
1201static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001202registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1203 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001204{
1205 struct wayland_compositor *c = data;
1206
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001207 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001208 c->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001209 wl_registry_bind(registry, name,
1210 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001211 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001212 c->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001213 wl_registry_bind(registry, name,
1214 &wl_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01001215 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001216 display_add_seat(c, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001217 } else if (strcmp(interface, "wl_shm") == 0) {
1218 c->parent.shm =
1219 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001220 }
1221}
1222
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001223static const struct wl_registry_listener registry_listener = {
1224 registry_handle_global
1225};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001226
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001227static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001228wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
1229{
1230 struct wayland_compositor *c = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001231 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001232
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001233 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1234 wl_display_terminate(c->base.wl_display);
1235 return 0;
1236 }
1237
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001238 if (mask & WL_EVENT_READABLE)
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001239 count = wl_display_dispatch(c->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05001240 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001241 wl_display_flush(c->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001242
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001243 if (mask == 0) {
1244 count = wl_display_dispatch_pending(c->parent.wl_display);
1245 wl_display_flush(c->parent.wl_display);
1246 }
1247
1248 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001249}
1250
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001251static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001252wayland_restore(struct weston_compositor *ec)
1253{
1254}
1255
1256static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001257wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001258{
Jonas Ådahle5a12252013-04-05 23:07:11 +02001259 struct wayland_compositor *c = (struct wayland_compositor *) ec;
1260
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001261 ec->renderer->destroy(ec);
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001262
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001263 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001264
Jonas Ådahle5a12252013-04-05 23:07:11 +02001265 if (c->parent.shm)
1266 wl_shm_destroy(c->parent.shm);
1267
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001268 free(ec);
1269}
1270
Jason Ekstrand7744f712013-10-27 22:24:55 -05001271static const char *left_ptrs[] = {
1272 "left_ptr",
1273 "default",
1274 "top_left_arrow",
1275 "left-arrow"
1276};
1277
1278static void
1279create_cursor(struct wayland_compositor *c, struct weston_config *config)
1280{
1281 struct weston_config_section *s;
1282 int size;
1283 char *theme = NULL;
1284 unsigned int i;
1285
1286 s = weston_config_get_section(config, "shell", NULL, NULL);
1287 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1288 weston_config_section_get_int(s, "cursor-size", &size, 32);
1289
1290 c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm);
1291
1292 c->cursor = NULL;
1293 for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1294 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
1295 left_ptrs[i]);
1296 if (!c->cursor) {
1297 fprintf(stderr, "could not load left cursor\n");
1298 return;
1299 }
1300}
1301
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001302static struct weston_compositor *
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001303wayland_compositor_create(struct wl_display *display,
1304 struct wayland_backend_options *options,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001305 int *argc, char *argv[],
1306 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001307{
1308 struct wayland_compositor *c;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001309 struct wayland_output *wo;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001310 struct wl_event_loop *loop;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001311 struct weston_config_section *section;
1312 struct weston_output *output;
1313 char *name;
1314 const char *section_name;
1315 int fd, x, count, width, height;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001316
Peter Huttererf3d62272013-08-08 11:57:05 +10001317 c = zalloc(sizeof *c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001318 if (c == NULL)
1319 return NULL;
1320
Daniel Stone725c2c32012-06-22 14:04:36 +01001321 if (weston_compositor_init(&c->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001322 config) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +02001323 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001324
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001325 c->parent.wl_display = wl_display_connect(options->display_name);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001326
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001327 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001328 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02001329 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001330 }
1331
Jason Ekstrand06ced802013-11-07 20:13:29 -06001332 wl_list_init(&c->input_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001333 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
1334 wl_registry_add_listener(c->parent.registry, &registry_listener, c);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001335 wl_display_roundtrip(c->parent.wl_display);
1336
1337 create_cursor(c, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001338
1339 c->base.wl_display = display;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001340
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001341 c->use_pixman = options->use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001342
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001343 if (!c->use_pixman) {
1344 gl_renderer = weston_load_module("gl-renderer.so",
1345 "gl_renderer_interface");
1346 if (!gl_renderer)
1347 c->use_pixman = 1;
1348 }
1349
1350 if (!c->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001351 if (gl_renderer->create(&c->base, c->parent.wl_display,
1352 gl_renderer->alpha_attribs,
1353 NULL) < 0) {
1354 weston_log("Failed to initialize the GL renderer; "
1355 "falling back to pixman.\n");
1356 c->use_pixman = 1;
1357 }
1358 }
1359
1360 if (c->use_pixman) {
1361 if (pixman_renderer_init(&c->base) < 0) {
1362 weston_log("Failed to initialize pixman renderer\n");
1363 goto err_display;
1364 }
1365 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001366
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001367 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001368 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001369
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001370 section = NULL;
1371 x = 0;
1372 count = 0;
1373 while (weston_config_next_section(config, &section, &section_name)) {
1374 if (!section_name || strcmp(section_name, "output") != 0)
1375 continue;
1376 weston_config_section_get_string(section, "name", &name, NULL);
1377 if (name == NULL)
1378 continue;
1379
1380 if (name[0] != 'W' || name[1] != 'L') {
1381 free(name);
1382 continue;
1383 }
1384 free(name);
1385
1386 wo = wayland_output_create_for_config(c, options, section, x, 0);
1387 if (!wo)
1388 goto err_outputs;
1389 x += wo->base.width;
1390 ++count;
1391 }
1392
1393
1394 width = options->width > 0 ? options->width : 1024;
1395 height = options->height > 0 ? options->height : 640;
1396 while (count < options->count) {
1397 if (!wayland_output_create(c, x, 0, width, height,
1398 WINDOW_TITLE, 0, 1))
1399 goto err_outputs;
1400 x += width;
1401 ++count;
1402 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001403
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001404 loop = wl_display_get_event_loop(c->base.wl_display);
1405
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001406 fd = wl_display_get_fd(c->parent.wl_display);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001407 c->parent.wl_source =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001408 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001409 wayland_compositor_handle_event, c);
1410 if (c->parent.wl_source == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001411 goto err_outputs;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001412
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001413 wl_event_source_check(c->parent.wl_source);
1414
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001415 return &c->base;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001416err_outputs:
1417 wl_list_for_each(output, &c->base.output_list, link)
1418 wayland_output_destroy(output);
Martin Olssonc5db50f2012-07-08 03:03:43 +02001419
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001420 c->base.renderer->destroy(&c->base);
Martin Olssonc5db50f2012-07-08 03:03:43 +02001421err_display:
1422 wl_display_disconnect(c->parent.wl_display);
1423err_compositor:
1424 weston_compositor_shutdown(&c->base);
1425err_free:
1426 free(c);
1427 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001428}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001429
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001430WL_EXPORT struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -05001431backend_init(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001432 struct weston_config *config)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001433{
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001434 struct wayland_backend_options options;
1435 memset(&options, 0, sizeof options);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001436
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001437 const struct weston_option wayland_options[] = {
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001438 { WESTON_OPTION_INTEGER, "width", 0, &options.width },
1439 { WESTON_OPTION_INTEGER, "height", 0, &options.height },
1440 { WESTON_OPTION_STRING, "display", 0, &options.display_name },
1441 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &options.use_pixman },
1442 { WESTON_OPTION_INTEGER, "output-count", 0, &options.count },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001443 };
1444
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001445 options.count = 1;
1446
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001447 parse_options(wayland_options,
1448 ARRAY_LENGTH(wayland_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001449
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001450 return wayland_compositor_create(display, &options,
1451 argc, argv, config);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001452}