blob: 19f0e806846ddd7b6f61bba8e018947cd6710897 [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
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010047struct wayland_compositor {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050048 struct weston_compositor base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010049
50 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040051 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040052 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010053 struct wl_compositor *compositor;
54 struct wl_shell *shell;
Jonas Ådahle5a12252013-04-05 23:07:11 +020055 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010056
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010057 struct wl_event_source *wl_source;
58 uint32_t event_mask;
59 } parent;
60
Jason Ekstrandff2fd462013-10-27 22:24:58 -050061 int use_pixman;
62
Jason Ekstrand7744f712013-10-27 22:24:55 -050063 struct theme *theme;
64 cairo_device_t *frame_device;
65 struct wl_cursor_theme *cursor_theme;
66 struct wl_cursor *cursor;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050067
Jason Ekstrand06ced802013-11-07 20:13:29 -060068 struct wl_list input_list;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010069};
70
71struct wayland_output {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050072 struct weston_output base;
73
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010074 struct {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050075 int draw_initial_frame;
76 struct wl_surface *surface;
77 struct wl_shell_surface *shell_surface;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010078 } parent;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050079
Jason Ekstrand7744f712013-10-27 22:24:55 -050080 int keyboard_count;
81
82 struct frame *frame;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050083
Jason Ekstrand7744f712013-10-27 22:24:55 -050084 struct {
Jason Ekstrandff2fd462013-10-27 22:24:58 -050085 struct wl_egl_window *egl_window;
86 struct {
87 cairo_surface_t *top;
88 cairo_surface_t *left;
89 cairo_surface_t *right;
90 cairo_surface_t *bottom;
91 } border;
92 } gl;
93
94 struct {
95 struct wl_list buffers;
96 struct wl_list free_buffers;
97 } shm;
Jason Ekstrand7744f712013-10-27 22:24:55 -050098
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050099 struct weston_mode mode;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500100 uint32_t scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100101};
102
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500103struct wayland_shm_buffer {
104 struct wayland_output *output;
105 struct wl_list link;
106 struct wl_list free_link;
107
108 struct wl_buffer *buffer;
109 void *data;
110 size_t size;
111 pixman_region32_t damage;
112 int frame_damaged;
113
114 pixman_image_t *pm_image;
115 cairo_surface_t *c_surface;
116};
117
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100118struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400119 struct weston_seat base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100120 struct wayland_compositor *compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100121 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500122
123 struct {
124 struct wl_seat *seat;
125 struct wl_pointer *pointer;
126 struct wl_keyboard *keyboard;
127 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500128
129 struct {
130 struct wl_surface *surface;
131 int32_t hx, hy;
132 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500133 } parent;
134
Daniel Stone50692802012-06-22 13:21:41 +0100135 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400136 uint32_t enter_serial;
137 int focus;
138 struct wayland_output *output;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500139 struct wayland_output *keyboard_focus;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100140};
141
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300142struct gl_renderer_interface *gl_renderer;
143
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500144static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500145wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
146{
147 cairo_surface_destroy(buffer->c_surface);
148 pixman_image_unref(buffer->pm_image);
149
150 wl_buffer_destroy(buffer->buffer);
151 munmap(buffer->data, buffer->size);
152
153 pixman_region32_fini(&buffer->damage);
154
155 wl_list_remove(&buffer->link);
156 wl_list_remove(&buffer->free_link);
157 free(buffer);
158}
159
160static void
161buffer_release(void *data, struct wl_buffer *buffer)
162{
163 struct wayland_shm_buffer *sb = data;
164
165 if (sb->output) {
166 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
167 } else {
168 wayland_shm_buffer_destroy(sb);
169 }
170}
171
172static const struct wl_buffer_listener buffer_listener = {
173 buffer_release
174};
175
176static struct wayland_shm_buffer *
177wayland_output_get_shm_buffer(struct wayland_output *output)
178{
179 struct wayland_compositor *c =
180 (struct wayland_compositor *) output->base.compositor;
181 struct wl_shm *shm = c->parent.shm;
182 struct wayland_shm_buffer *sb;
183
184 struct wl_shm_pool *pool;
185 int width, height, stride;
186 int32_t fx, fy;
187 int fd;
188 unsigned char *data;
189
190 if (!wl_list_empty(&output->shm.free_buffers)) {
191 sb = container_of(output->shm.free_buffers.next,
192 struct wayland_shm_buffer, free_link);
193 wl_list_remove(&sb->free_link);
194 wl_list_init(&sb->free_link);
195
196 return sb;
197 }
198
199 if (output->frame) {
200 width = frame_width(output->frame);
201 height = frame_height(output->frame);
202 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500203 width = output->base.current_mode->width;
204 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500205 }
206
207 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
208
209 fd = os_create_anonymous_file(height * stride);
210 if (fd < 0) {
211 perror("os_create_anonymous_file");
212 return NULL;
213 }
214
215 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
216 if (data == MAP_FAILED) {
217 perror("mmap");
218 close(fd);
219 return NULL;
220 }
221
222 sb = zalloc(sizeof *sb);
223
224 sb->output = output;
225 wl_list_init(&sb->free_link);
226 wl_list_insert(&output->shm.buffers, &sb->link);
227
228 pixman_region32_init_rect(&sb->damage, 0, 0,
229 output->base.width, output->base.height);
230 sb->frame_damaged = 1;
231
232 sb->data = data;
233 sb->size = height * stride;
234
235 pool = wl_shm_create_pool(shm, fd, sb->size);
236
237 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
238 width, height,
239 stride,
240 WL_SHM_FORMAT_ARGB8888);
241 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
242 wl_shm_pool_destroy(pool);
243 close(fd);
244
245 memset(data, 0, sb->size);
246
247 sb->c_surface =
248 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
249 width, height, stride);
250
251 fx = 0;
252 fy = 0;
253 if (output->frame)
254 frame_interior(output->frame, &fx, &fy, 0, 0);
255 sb->pm_image =
256 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
257 (uint32_t *)(data + fy * stride) + fx,
258 stride);
259
260 return sb;
261}
262
263static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500264frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400265{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500266 struct weston_output *output = data;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400267
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500268 wl_callback_destroy(callback);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500269 weston_output_finish_frame(output, time);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400270}
271
272static const struct wl_callback_listener frame_listener = {
273 frame_done
274};
275
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500276static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200277draw_initial_frame(struct wayland_output *output)
278{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500279 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200280
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500281 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200282
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500283 /* If we are rendering with GL, then orphan it so that it gets
284 * destroyed immediately */
285 if (output->gl.egl_window)
286 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500287
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500288 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200289
290 /* We only need to damage some part, as its only transparant
291 * pixels anyway. */
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500292 wl_surface_damage(output->parent.surface, 0, 0, 1, 1);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200293}
294
295static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500296wayland_output_update_gl_border(struct wayland_output *output)
297{
298 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
299 cairo_t *cr;
300
301 if (!output->frame)
302 return;
303 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
304 return;
305
306 fwidth = frame_width(output->frame);
307 fheight = frame_height(output->frame);
308 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
309
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500310 if (!output->gl.border.top)
311 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500312 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
313 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500314 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500315 frame_repaint(output->frame, cr);
316 cairo_destroy(cr);
317 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
318 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500319 cairo_image_surface_get_stride(output->gl.border.top) / 4,
320 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500321
322
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500323 if (!output->gl.border.left)
324 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500325 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
326 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500327 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500328 cairo_translate(cr, 0, -iy);
329 frame_repaint(output->frame, cr);
330 cairo_destroy(cr);
331 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
332 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500333 cairo_image_surface_get_stride(output->gl.border.left) / 4,
334 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500335
336
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500337 if (!output->gl.border.right)
338 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500339 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
340 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500341 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500342 cairo_translate(cr, -(iwidth + ix), -iy);
343 frame_repaint(output->frame, cr);
344 cairo_destroy(cr);
345 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
346 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500347 cairo_image_surface_get_stride(output->gl.border.right) / 4,
348 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500349
350
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500351 if (!output->gl.border.bottom)
352 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500353 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
354 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500355 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500356 cairo_translate(cr, 0, -(iy + iheight));
357 frame_repaint(output->frame, cr);
358 cairo_destroy(cr);
359 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
360 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500361 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
362 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500363}
364
365static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200366wayland_output_start_repaint_loop(struct weston_output *output_base)
367{
368 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrand286ff682013-10-27 22:24:57 -0500369 struct wayland_compositor *wc =
370 (struct wayland_compositor *)output->base.compositor;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200371 struct wl_callback *callback;
372
373 /* If this is the initial frame, we need to attach a buffer so that
374 * the compositor can map the surface and include it in its render
375 * loop. If the surface doesn't end up in the render loop, the frame
376 * callback won't be invoked. The buffer is transparent and of the
377 * same size as the future real output buffer. */
378 if (output->parent.draw_initial_frame) {
379 output->parent.draw_initial_frame = 0;
380
381 draw_initial_frame(output);
382 }
383
384 callback = wl_surface_frame(output->parent.surface);
385 wl_callback_add_listener(callback, &frame_listener, output);
386 wl_surface_commit(output->parent.surface);
Jason Ekstrand286ff682013-10-27 22:24:57 -0500387 wl_display_flush(wc->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200388}
389
David Herrmann1edf44c2013-10-22 17:11:26 +0200390static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500391wayland_output_repaint_gl(struct weston_output *output_base,
392 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400393{
394 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400395 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400396 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600397
Kristian Høgsberg33418202011-08-16 23:01:28 -0400398 callback = wl_surface_frame(output->parent.surface);
399 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100400
Jason Ekstrand7744f712013-10-27 22:24:55 -0500401 wayland_output_update_gl_border(output);
402
Pekka Paalanenbc106382012-10-10 12:49:31 +0300403 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200404
405 pixman_region32_subtract(&ec->primary_plane.damage,
406 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200407 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100408}
409
Matt Roper361d2ad2011-08-29 13:52:23 -0700410static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500411wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
412{
413 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
414 cairo_t *cr;
415
416 if (!buffer->output->frame || !buffer->frame_damaged)
417 return;
418
419 cr = cairo_create(buffer->c_surface);
420
421 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
422 fwidth = frame_width(buffer->output->frame);
423 fheight = frame_height(buffer->output->frame);
424
425 /* Set the clip so we don't unnecisaraly damage the surface */
426 cairo_move_to(cr, ix, iy);
427 cairo_rel_line_to(cr, iwidth, 0);
428 cairo_rel_line_to(cr, 0, iheight);
429 cairo_rel_line_to(cr, -iwidth, 0);
430 cairo_line_to(cr, ix, iy);
431 cairo_line_to(cr, 0, iy);
432 cairo_line_to(cr, 0, fheight);
433 cairo_line_to(cr, fwidth, fheight);
434 cairo_line_to(cr, fwidth, 0);
435 cairo_line_to(cr, 0, 0);
436 cairo_line_to(cr, 0, iy);
437 cairo_close_path(cr);
438 cairo_clip(cr);
439
440 /* Draw using a pattern so that the final result gets clipped */
441 cairo_push_group(cr);
442 frame_repaint(buffer->output->frame, cr);
443 cairo_pop_group_to_source(cr);
444 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
445 cairo_paint(cr);
446
447 cairo_destroy(cr);
448}
449
450static void
451wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
452{
453 pixman_region32_t damage;
454 pixman_box32_t *rects;
455 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
456 int i, n;
457
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500458 pixman_region32_init(&damage);
459 weston_transformed_region(sb->output->base.width,
460 sb->output->base.height,
461 sb->output->base.transform,
462 sb->output->base.current_scale,
463 &sb->damage, &damage);
464
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500465 if (sb->output->frame) {
466 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
467 fwidth = frame_width(sb->output->frame);
468 fheight = frame_height(sb->output->frame);
469
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500470 pixman_region32_translate(&damage, ix, iy);
471
472 if (sb->frame_damaged) {
473 pixman_region32_union_rect(&damage, &damage,
474 0, 0, fwidth, iy);
475 pixman_region32_union_rect(&damage, &damage,
476 0, iy, ix, iheight);
477 pixman_region32_union_rect(&damage, &damage,
478 ix + iwidth, iy,
479 fwidth - (ix + iwidth), iheight);
480 pixman_region32_union_rect(&damage, &damage,
481 0, iy + iheight,
482 fwidth, fheight - (iy + iheight));
483 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500484 }
485
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500486 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500487 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
488 for (i = 0; i < n; ++i)
489 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
490 rects[i].y1, rects[i].x2 - rects[i].x1,
491 rects[i].y2 - rects[i].y1);
492
493 if (sb->output->frame)
494 pixman_region32_fini(&damage);
495}
496
497static int
498wayland_output_repaint_pixman(struct weston_output *output_base,
499 pixman_region32_t *damage)
500{
501 struct wayland_output *output = (struct wayland_output *) output_base;
502 struct wayland_compositor *c =
503 (struct wayland_compositor *)output->base.compositor;
504 struct wl_callback *callback;
505 struct wayland_shm_buffer *sb;
506
507 if (output->frame) {
508 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
509 wl_list_for_each(sb, &output->shm.buffers, link)
510 sb->frame_damaged = 1;
511 }
512
513 wl_list_for_each(sb, &output->shm.buffers, link)
514 pixman_region32_union(&sb->damage, &sb->damage, damage);
515
516 sb = wayland_output_get_shm_buffer(output);
517
518 wayland_output_update_shm_border(sb);
519 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
520 c->base.renderer->repaint_output(output_base, &sb->damage);
521
522 wayland_shm_buffer_attach(sb);
523
524 callback = wl_surface_frame(output->parent.surface);
525 wl_callback_add_listener(callback, &frame_listener, output);
526 wl_surface_commit(output->parent.surface);
527 wl_display_flush(c->parent.wl_display);
528
529 pixman_region32_fini(&sb->damage);
530 pixman_region32_init(&sb->damage);
531 sb->frame_damaged = 0;
532
533 pixman_region32_subtract(&c->base.primary_plane.damage,
534 &c->base.primary_plane.damage, damage);
535 return 0;
536}
537
538static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500539wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700540{
541 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500542 struct wayland_compositor *c =
543 (struct wayland_compositor *) output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700544
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500545 if (c->use_pixman) {
546 pixman_renderer_output_destroy(output_base);
547 } else {
548 gl_renderer->output_destroy(output_base);
549 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100550
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500551 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500552 wl_surface_destroy(output->parent.surface);
553 wl_shell_surface_destroy(output->parent.shell_surface);
554
555 if (output->frame) {
556 frame_destroy(output->frame);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500557 cairo_surface_destroy(output->gl.border.top);
558 cairo_surface_destroy(output->gl.border.left);
559 cairo_surface_destroy(output->gl.border.right);
560 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500561 }
562
563 weston_output_destroy(&output->base);
Matt Roper361d2ad2011-08-29 13:52:23 -0700564 free(output);
565
566 return;
567}
568
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300569static const struct wl_shell_surface_listener shell_surface_listener;
570
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200571static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500572wayland_output_init_gl_renderer(struct wayland_output *output)
573{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500574 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500575
576 if (output->frame) {
577 fwidth = frame_width(output->frame);
578 fheight = frame_height(output->frame);
579 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500580 fwidth = output->base.current_mode->width;
581 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500582 }
583
584 output->gl.egl_window =
585 wl_egl_window_create(output->parent.surface,
586 fwidth, fheight);
587 if (!output->gl.egl_window) {
588 weston_log("failure to create wl_egl_window\n");
589 return -1;
590 }
591
592 if (gl_renderer->output_create(&output->base,
593 output->gl.egl_window) < 0)
594 goto cleanup_window;
595
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500596 return 0;
597
598cleanup_window:
599 wl_egl_window_destroy(output->gl.egl_window);
600 return -1;
601}
602
603static int
604wayland_output_init_pixman_renderer(struct wayland_output *output)
605{
606 return pixman_renderer_output_create(&output->base);
607}
608
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500609static struct wayland_output *
610wayland_output_create(struct wayland_compositor *c, int x, int y,
611 int width, int height, const char *name,
612 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100613{
614 struct wayland_output *output;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500615 int32_t fx, fy, fwidth, fheight;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500616 int output_width, output_height;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500617 struct wl_region *region;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100618
Peter Huttererf3d62272013-08-08 11:57:05 +1000619 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100620 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500621 return NULL;
622
623 output_width = width * scale;
624 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100625
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400626 output->mode.flags =
627 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500628 output->mode.width = output_width;
629 output->mode.height = output_height;
630 output->mode.refresh = 60000;
631 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400632 wl_list_init(&output->base.mode_list);
633 wl_list_insert(&output->base.mode_list, &output->mode.link);
634
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500635 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
636 width, height, x, y);
637
Hardeningff39efa2013-09-18 23:56:35 +0200638 output->base.current_mode = &output->mode;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500639 weston_output_init(&output->base, &c->base, x, y, width, height,
640 transform, scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400641
Kristian Høgsberg90bc88c2012-08-13 23:34:04 -0400642 output->base.make = "waywayland";
643 output->base.model = "none";
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500644
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500645 wl_list_init(&output->shm.buffers);
646 wl_list_init(&output->shm.free_buffers);
647
Jason Ekstrand7744f712013-10-27 22:24:55 -0500648 if (!c->theme)
649 c->theme = theme_create();
650 output->frame = frame_create(c->theme, width, height,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500651 FRAME_BUTTON_CLOSE, name);
652 frame_resize_inside(output->frame, output_width, output_height);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500653
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100654 output->parent.surface =
655 wl_compositor_create_surface(c->parent.compositor);
656 wl_surface_set_user_data(output->parent.surface, output);
657
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500658 if (c->use_pixman) {
659 if (wayland_output_init_pixman_renderer(output) < 0)
660 goto cleanup_output;
661 output->base.repaint = wayland_output_repaint_pixman;
662 } else {
663 if (wayland_output_init_gl_renderer(output) < 0)
664 goto cleanup_output;
665 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100666 }
667
Jason Ekstrand7744f712013-10-27 22:24:55 -0500668 frame_input_rect(output->frame, &fx, &fy, &fwidth, &fheight);
669 region = wl_compositor_create_region(c->parent.compositor);
670 wl_region_add(region, fx, fy, fwidth, fheight);
671 wl_surface_set_input_region(output->parent.surface, region);
672 wl_region_destroy(region);
673
674 frame_opaque_rect(output->frame, &fx, &fy, &fwidth, &fheight);
675 region = wl_compositor_create_region(c->parent.compositor);
676 wl_region_add(region, fx, fy, fwidth, fheight);
677 wl_surface_set_opaque_region(output->parent.surface, region);
678 wl_region_destroy(region);
679
Jonas Ådahle5a12252013-04-05 23:07:11 +0200680 output->parent.draw_initial_frame = 1;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200681 output->parent.shell_surface =
682 wl_shell_get_shell_surface(c->parent.shell,
683 output->parent.surface);
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300684 wl_shell_surface_add_listener(output->parent.shell_surface,
685 &shell_surface_listener, output);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200686 wl_shell_surface_set_toplevel(output->parent.shell_surface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100687
Jonas Ådahle5a12252013-04-05 23:07:11 +0200688 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -0700689 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800690 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200691 output->base.set_backlight = NULL;
692 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800693 output->base.switch_mode = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100694
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100695 wl_list_insert(c->base.output_list.prev, &output->base.link);
696
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500697 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100698
Benjamin Franzkebe014562011-02-18 17:04:24 +0100699cleanup_output:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500700 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +0100701 free(output);
702
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500703 return NULL;
704}
705
706static struct wayland_output *
707wayland_output_create_for_config(struct wayland_compositor *c,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500708 struct weston_config_section *config_section,
Jason Ekstrand0cf39352013-11-07 20:13:31 -0600709 int option_width, int option_height,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -0600710 int option_scale, int32_t x, int32_t y)
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500711{
712 struct wayland_output *output;
713 char *mode, *t, *name, *str;
714 int width, height, scale;
715 uint32_t transform;
716 unsigned int i, slen;
717
718 static const struct { const char *name; uint32_t token; } transform_names[] = {
719 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
720 { "90", WL_OUTPUT_TRANSFORM_90 },
721 { "180", WL_OUTPUT_TRANSFORM_180 },
722 { "270", WL_OUTPUT_TRANSFORM_270 },
723 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
724 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
725 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
726 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
727 };
728
729 weston_config_section_get_string(config_section, "name", &name, NULL);
730 if (name) {
731 slen = strlen(name);
732 slen += strlen(WINDOW_TITLE " - ");
733 str = malloc(slen + 1);
734 if (str)
735 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
736 free(name);
737 name = str;
738 }
739 if (!name)
740 name = WINDOW_TITLE;
741
742 weston_config_section_get_string(config_section,
743 "mode", &mode, "1024x600");
744 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
745 weston_log("Invalid mode \"%s\" for output %s\n",
746 mode, name);
747 width = 1024;
748 height = 640;
749 }
750 free(mode);
751
Jason Ekstrand0cf39352013-11-07 20:13:31 -0600752 if (option_width)
753 width = option_width;
754 if (option_height)
755 height = option_height;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500756
757 weston_config_section_get_int(config_section, "scale", &scale, 1);
758
Jason Ekstrand12c6dd92013-11-07 20:13:32 -0600759 if (option_scale)
760 scale = option_scale;
761
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500762 weston_config_section_get_string(config_section,
763 "transform", &t, "normal");
764 transform = WL_OUTPUT_TRANSFORM_NORMAL;
765 for (i = 0; i < ARRAY_LENGTH(transform_names); i++) {
766 if (strcmp(transform_names[i].name, t) == 0) {
767 transform = transform_names[i].token;
768 break;
769 }
770 }
771 if (i >= ARRAY_LENGTH(transform_names))
772 weston_log("Invalid transform \"%s\" for output %s\n", t, name);
773 free(t);
774
775 output = wayland_output_create(c, x, y, width, height, name, transform, scale);
776 free(name);
777
778 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100779}
780
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300781static void
782shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
783 uint32_t serial)
784{
785 wl_shell_surface_pong(shell_surface, serial);
786}
787
788static void
789shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
790 uint32_t edges, int32_t width, int32_t height)
791{
792 /* FIXME: implement resizing */
793}
794
795static void
796shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
797{
798}
799
800static const struct wl_shell_surface_listener shell_surface_listener = {
801 shell_surface_ping,
802 shell_surface_configure,
803 shell_surface_popup_done
804};
805
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100806/* Events received from the wayland-server this compositor is client of: */
807
Jason Ekstrand7744f712013-10-27 22:24:55 -0500808/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400809static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500810input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400811{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400812
Jason Ekstrand7744f712013-10-27 22:24:55 -0500813 struct wl_buffer *buffer;
814 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400815
Jason Ekstrand7744f712013-10-27 22:24:55 -0500816 if (!input->compositor->cursor)
817 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400818
Jason Ekstrand7744f712013-10-27 22:24:55 -0500819 image = input->compositor->cursor->images[0];
820 buffer = wl_cursor_image_get_buffer(image);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400821
Jason Ekstrand7744f712013-10-27 22:24:55 -0500822
823 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
824 input->parent.cursor.surface,
825 image->hotspot_x, image->hotspot_y);
826
827 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
828 wl_surface_damage(input->parent.cursor.surface, 0, 0,
829 image->width, image->height);
830 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400831}
832
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100833static void
Daniel Stone37816df2012-05-16 18:45:18 +0100834input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
835 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400836 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100837{
838 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500839 int32_t fx, fy;
840 enum theme_location location;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100841
Daniel Stone50692802012-06-22 13:21:41 +0100842 /* XXX: If we get a modifier event immediately before the focus,
843 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400844 input->enter_serial = serial;
845 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500846
847 if (input->output->frame) {
848 location = frame_pointer_enter(input->output->frame, input,
849 wl_fixed_to_int(x),
850 wl_fixed_to_int(y));
851 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
852 x -= wl_fixed_from_int(fx);
853 y -= wl_fixed_from_int(fy);
854
855 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
856 weston_output_schedule_repaint(&input->output->base);
857 } else {
858 location = THEME_LOCATION_CLIENT_AREA;
859 }
860
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500861 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500862
863 if (location == THEME_LOCATION_CLIENT_AREA) {
864 input->focus = 1;
865 notify_pointer_focus(&input->base, &input->output->base, x, y);
866 wl_pointer_set_cursor(input->parent.pointer,
867 input->enter_serial, NULL, 0, 0);
868 } else {
869 input->focus = 0;
870 notify_pointer_focus(&input->base, NULL, 0, 0);
871 input_set_cursor(input);
872 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100873}
874
875static void
Daniel Stone37816df2012-05-16 18:45:18 +0100876input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
877 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500878{
879 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500880
Jason Ekstrand7744f712013-10-27 22:24:55 -0500881 if (input->output->frame) {
882 frame_pointer_leave(input->output->frame, input);
883
884 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
885 weston_output_schedule_repaint(&input->output->base);
886 }
887
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400888 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400889 input->output = NULL;
890 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500891}
892
893static void
Daniel Stone37816df2012-05-16 18:45:18 +0100894input_handle_motion(void *data, struct wl_pointer *pointer,
895 uint32_t time, wl_fixed_t x, wl_fixed_t y)
896{
897 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500898 int32_t fx, fy;
899 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +0100900
Jason Ekstrand7744f712013-10-27 22:24:55 -0500901 if (input->output->frame) {
902 location = frame_pointer_motion(input->output->frame, input,
903 wl_fixed_to_int(x),
904 wl_fixed_to_int(y));
905 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
906 x -= wl_fixed_from_int(fx);
907 y -= wl_fixed_from_int(fy);
908
909 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
910 weston_output_schedule_repaint(&input->output->base);
911 } else {
912 location = THEME_LOCATION_CLIENT_AREA;
913 }
914
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500915 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500916
917 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
918 input_set_cursor(input);
919 notify_pointer_focus(&input->base, NULL, 0, 0);
920 input->focus = 0;
921 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
922 wl_pointer_set_cursor(input->parent.pointer,
923 input->enter_serial, NULL, 0, 0);
924 notify_pointer_focus(&input->base, &input->output->base, x, y);
925 input->focus = 1;
926 }
927
928 if (location == THEME_LOCATION_CLIENT_AREA)
929 notify_motion_absolute(&input->base, time, x, y);
Daniel Stone37816df2012-05-16 18:45:18 +0100930}
931
932static void
933input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100934 uint32_t serial, uint32_t time, uint32_t button,
935 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +0100936{
937 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100938 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500939 enum frame_button_state fstate;
940 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +0100941
Jason Ekstrand7744f712013-10-27 22:24:55 -0500942 if (input->output->frame) {
943 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
944 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
945
946 location = frame_pointer_button(input->output->frame, input,
947 button, fstate);
948
949 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
950
951 wl_shell_surface_move(input->output->parent.shell_surface,
952 input->parent.seat, serial);
953 frame_status_clear(input->output->frame,
954 FRAME_STATUS_MOVE);
955 return;
956 }
957
958 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE)
959 wl_display_terminate(input->compositor->base.wl_display);
960
961 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
962 weston_output_schedule_repaint(&input->output->base);
963 } else {
964 location = THEME_LOCATION_CLIENT_AREA;
965 }
966
967 if (location == THEME_LOCATION_CLIENT_AREA)
968 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +0100969}
970
971static void
972input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100973 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +0100974{
975 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100976
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400977 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +0100978}
979
980static const struct wl_pointer_listener pointer_listener = {
981 input_handle_pointer_enter,
982 input_handle_pointer_leave,
983 input_handle_motion,
984 input_handle_button,
985 input_handle_axis,
986};
987
988static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100989input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
990 int fd, uint32_t size)
991{
992 struct wayland_input *input = data;
993 struct xkb_keymap *keymap;
994 char *map_str;
995
996 if (!data) {
997 close(fd);
998 return;
999 }
1000
1001 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1002 close(fd);
1003 return;
1004 }
1005
1006 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1007 if (map_str == MAP_FAILED) {
1008 close(fd);
1009 return;
1010 }
1011
1012 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
1013 map_str,
1014 XKB_KEYMAP_FORMAT_TEXT_V1,
1015 0);
1016 munmap(map_str, size);
1017 close(fd);
1018
1019 if (!keymap) {
Martin Minarik6d118362012-06-07 18:01:59 +02001020 weston_log("failed to compile keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001021 return;
1022 }
1023
Rui Matos0c194ce2013-10-10 19:44:21 +02001024 if (input->base.keyboard)
1025 weston_seat_update_keymap(&input->base, keymap);
1026 else
1027 weston_seat_init_keyboard(&input->base, keymap);
1028
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001029 xkb_map_unref(keymap);
1030}
1031
1032static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001033input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001034 struct wl_keyboard *keyboard,
1035 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001036 struct wl_surface *surface,
1037 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001038{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001039 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001040 struct wayland_output *focus;
1041
1042 focus = input->keyboard_focus;
1043 if (focus) {
1044 /* This shouldn't happen */
1045 focus->keyboard_count--;
1046 if (!focus->keyboard_count && focus->frame)
1047 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1048 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1049 weston_output_schedule_repaint(&focus->base);
1050 }
1051
1052 input->keyboard_focus = wl_surface_get_user_data(surface);
1053 input->keyboard_focus->keyboard_count++;
1054
1055 focus = input->keyboard_focus;
1056 if (focus->frame) {
1057 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1058 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1059 weston_output_schedule_repaint(&focus->base);
1060 }
1061
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001062
Daniel Stone50692802012-06-22 13:21:41 +01001063 /* XXX: If we get a modifier event immediately before the focus,
1064 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001065 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001066 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001067}
1068
1069static void
1070input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001071 struct wl_keyboard *keyboard,
1072 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001073 struct wl_surface *surface)
1074{
1075 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001076 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001077
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001078 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001079
1080 focus = input->keyboard_focus;
1081 if (!focus)
1082 return; /* This shouldn't happen */
1083
1084 focus->keyboard_count--;
1085 if (!focus->keyboard_count && focus->frame) {
1086 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1087 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1088 weston_output_schedule_repaint(&focus->base);
1089 }
1090
1091 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001092}
1093
Daniel Stone37816df2012-05-16 18:45:18 +01001094static void
1095input_handle_key(void *data, struct wl_keyboard *keyboard,
1096 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1097{
1098 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001099
Daniel Stone50692802012-06-22 13:21:41 +01001100 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001101 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001102 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001103 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stone50692802012-06-22 13:21:41 +01001104 STATE_UPDATE_NONE);
Daniel Stone37816df2012-05-16 18:45:18 +01001105}
1106
Daniel Stone351eb612012-05-31 15:27:47 -04001107static void
1108input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001109 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001110 uint32_t mods_latched, uint32_t mods_locked,
1111 uint32_t group)
1112{
Daniel Stone50692802012-06-22 13:21:41 +01001113 struct wayland_input *input = data;
1114 struct wayland_compositor *c = input->compositor;
1115 uint32_t serial_out;
1116
1117 /* If we get a key event followed by a modifier event with the
1118 * same serial number, then we try to preserve those semantics by
1119 * reusing the same serial number on the way out too. */
1120 if (serial_in == input->key_serial)
1121 serial_out = wl_display_get_serial(c->base.wl_display);
1122 else
1123 serial_out = wl_display_next_serial(c->base.wl_display);
1124
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001125 xkb_state_update_mask(input->base.xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001126 mods_depressed, mods_latched,
1127 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001128 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001129}
1130
Daniel Stone37816df2012-05-16 18:45:18 +01001131static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001132 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001133 input_handle_keyboard_enter,
1134 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001135 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001136 input_handle_modifiers,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001137};
1138
1139static void
Daniel Stone37816df2012-05-16 18:45:18 +01001140input_handle_capabilities(void *data, struct wl_seat *seat,
1141 enum wl_seat_capability caps)
1142{
1143 struct wayland_input *input = data;
1144
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001145 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1146 input->parent.pointer = wl_seat_get_pointer(seat);
1147 wl_pointer_set_user_data(input->parent.pointer, input);
1148 wl_pointer_add_listener(input->parent.pointer,
1149 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001150 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001151 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1152 wl_pointer_destroy(input->parent.pointer);
1153 input->parent.pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001154 }
1155
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001156 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1157 input->parent.keyboard = wl_seat_get_keyboard(seat);
1158 wl_keyboard_set_user_data(input->parent.keyboard, input);
1159 wl_keyboard_add_listener(input->parent.keyboard,
1160 &keyboard_listener, input);
1161 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1162 wl_keyboard_destroy(input->parent.keyboard);
1163 input->parent.keyboard = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001164 }
1165}
1166
1167static const struct wl_seat_listener seat_listener = {
1168 input_handle_capabilities,
1169};
1170
1171static void
1172display_add_seat(struct wayland_compositor *c, uint32_t id)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001173{
1174 struct wayland_input *input;
1175
Peter Huttererf3d62272013-08-08 11:57:05 +10001176 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001177 if (input == NULL)
1178 return;
1179
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001180 weston_seat_init(&input->base, &c->base, "default");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001181 input->compositor = c;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001182 input->parent.seat = wl_registry_bind(c->parent.registry, id,
1183 &wl_seat_interface, 1);
Jason Ekstrand06ced802013-11-07 20:13:29 -06001184 wl_list_insert(c->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001185
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001186 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1187 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001188
1189 input->parent.cursor.surface =
1190 wl_compositor_create_surface(c->parent.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001191}
1192
1193static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001194registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1195 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001196{
1197 struct wayland_compositor *c = data;
1198
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001199 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001200 c->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001201 wl_registry_bind(registry, name,
1202 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001203 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001204 c->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001205 wl_registry_bind(registry, name,
1206 &wl_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01001207 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001208 display_add_seat(c, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001209 } else if (strcmp(interface, "wl_shm") == 0) {
1210 c->parent.shm =
1211 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001212 }
1213}
1214
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001215static const struct wl_registry_listener registry_listener = {
1216 registry_handle_global
1217};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001218
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001219static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001220wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
1221{
1222 struct wayland_compositor *c = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001223 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001224
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001225 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1226 wl_display_terminate(c->base.wl_display);
1227 return 0;
1228 }
1229
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001230 if (mask & WL_EVENT_READABLE)
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001231 count = wl_display_dispatch(c->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05001232 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001233 wl_display_flush(c->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001234
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001235 if (mask == 0) {
1236 count = wl_display_dispatch_pending(c->parent.wl_display);
1237 wl_display_flush(c->parent.wl_display);
1238 }
1239
1240 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001241}
1242
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001243static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001244wayland_restore(struct weston_compositor *ec)
1245{
1246}
1247
1248static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001249wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001250{
Jonas Ådahle5a12252013-04-05 23:07:11 +02001251 struct wayland_compositor *c = (struct wayland_compositor *) ec;
1252
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001253 ec->renderer->destroy(ec);
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001254
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001255 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001256
Jonas Ådahle5a12252013-04-05 23:07:11 +02001257 if (c->parent.shm)
1258 wl_shm_destroy(c->parent.shm);
1259
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001260 free(ec);
1261}
1262
Jason Ekstrand7744f712013-10-27 22:24:55 -05001263static const char *left_ptrs[] = {
1264 "left_ptr",
1265 "default",
1266 "top_left_arrow",
1267 "left-arrow"
1268};
1269
1270static void
1271create_cursor(struct wayland_compositor *c, struct weston_config *config)
1272{
1273 struct weston_config_section *s;
1274 int size;
1275 char *theme = NULL;
1276 unsigned int i;
1277
1278 s = weston_config_get_section(config, "shell", NULL, NULL);
1279 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1280 weston_config_section_get_int(s, "cursor-size", &size, 32);
1281
1282 c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm);
1283
1284 c->cursor = NULL;
1285 for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1286 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
1287 left_ptrs[i]);
1288 if (!c->cursor) {
1289 fprintf(stderr, "could not load left cursor\n");
1290 return;
1291 }
1292}
1293
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001294static struct wayland_compositor *
1295wayland_compositor_create(struct wl_display *display, int use_pixman,
1296 const char *display_name, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001297 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001298{
1299 struct wayland_compositor *c;
1300 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001301 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001302
Peter Huttererf3d62272013-08-08 11:57:05 +10001303 c = zalloc(sizeof *c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001304 if (c == NULL)
1305 return NULL;
1306
Daniel Stone725c2c32012-06-22 14:04:36 +01001307 if (weston_compositor_init(&c->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001308 config) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +02001309 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001310
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001311 c->parent.wl_display = wl_display_connect(display_name);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001312
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001313 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001314 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02001315 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001316 }
1317
Jason Ekstrand06ced802013-11-07 20:13:29 -06001318 wl_list_init(&c->input_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001319 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
1320 wl_registry_add_listener(c->parent.registry, &registry_listener, c);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001321 wl_display_roundtrip(c->parent.wl_display);
1322
1323 create_cursor(c, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001324
1325 c->base.wl_display = display;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001326
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001327 c->use_pixman = use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001328
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001329 if (!c->use_pixman) {
1330 gl_renderer = weston_load_module("gl-renderer.so",
1331 "gl_renderer_interface");
1332 if (!gl_renderer)
1333 c->use_pixman = 1;
1334 }
1335
1336 if (!c->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001337 if (gl_renderer->create(&c->base, c->parent.wl_display,
1338 gl_renderer->alpha_attribs,
1339 NULL) < 0) {
1340 weston_log("Failed to initialize the GL renderer; "
1341 "falling back to pixman.\n");
1342 c->use_pixman = 1;
1343 }
1344 }
1345
1346 if (c->use_pixman) {
1347 if (pixman_renderer_init(&c->base) < 0) {
1348 weston_log("Failed to initialize pixman renderer\n");
1349 goto err_display;
1350 }
1351 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001352
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001353 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001354 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001355
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001356 loop = wl_display_get_event_loop(c->base.wl_display);
1357
1358 fd = wl_display_get_fd(c->parent.wl_display);
1359 c->parent.wl_source =
1360 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1361 wayland_compositor_handle_event, c);
1362 if (c->parent.wl_source == NULL)
1363 goto err_renderer;
1364
1365 wl_event_source_check(c->parent.wl_source);
1366
1367 return c;
1368err_renderer:
1369 c->base.renderer->destroy(&c->base);
1370err_display:
1371 wl_display_disconnect(c->parent.wl_display);
1372err_compositor:
1373 weston_compositor_shutdown(&c->base);
1374err_free:
1375 free(c);
1376 return NULL;
1377}
1378
1379static void
1380wayland_compositor_destroy(struct wayland_compositor *c)
1381{
1382 struct weston_output *output;
1383
1384 wl_list_for_each(output, &c->base.output_list, link)
1385 wayland_output_destroy(output);
1386
1387 c->base.renderer->destroy(&c->base);
1388 wl_display_disconnect(c->parent.wl_display);
1389
1390 if (c->theme)
1391 theme_destroy(c->theme);
1392 if (c->frame_device)
1393 cairo_device_destroy(c->frame_device);
1394 wl_cursor_theme_destroy(c->cursor_theme);
1395
1396 weston_compositor_shutdown(&c->base);
1397 free(c);
1398}
1399
1400WL_EXPORT struct weston_compositor *
1401backend_init(struct wl_display *display, int *argc, char *argv[],
1402 struct weston_config *config)
1403{
1404 struct wayland_compositor *c;
1405 struct wayland_output *output;
1406 struct weston_config_section *section;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001407 int x, count, width, height, scale, use_pixman;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001408 const char *section_name, *display_name;
1409 char *name;
1410
1411 const struct weston_option wayland_options[] = {
1412 { WESTON_OPTION_INTEGER, "width", 0, &width },
1413 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001414 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001415 { WESTON_OPTION_STRING, "display", 0, &display_name },
1416 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
1417 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
1418 };
1419
1420 width = 0;
1421 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001422 scale = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001423 display_name = NULL;
1424 use_pixman = 0;
1425 count = 1;
1426
1427 parse_options(wayland_options,
1428 ARRAY_LENGTH(wayland_options), argc, argv);
1429
1430 c = wayland_compositor_create(display, use_pixman, display_name,
1431 argc, argv, config);
1432 if (!c)
1433 return NULL;
1434
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001435 section = NULL;
1436 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001437 while (weston_config_next_section(config, &section, &section_name)) {
1438 if (!section_name || strcmp(section_name, "output") != 0)
1439 continue;
1440 weston_config_section_get_string(section, "name", &name, NULL);
1441 if (name == NULL)
1442 continue;
1443
1444 if (name[0] != 'W' || name[1] != 'L') {
1445 free(name);
1446 continue;
1447 }
1448 free(name);
1449
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001450 output = wayland_output_create_for_config(c, section, width,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001451 height, scale, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001452 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001453 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001454
1455 x += output->base.width;
1456 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001457 }
1458
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001459 if (!width)
1460 width = 1024;
1461 if (!height)
1462 height = 640;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001463 if (!scale)
1464 scale = 1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001465 while (count > 0) {
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001466 output = wayland_output_create(c, x, 0, width, height,
1467 NULL, 0, scale);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001468 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001469 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001470
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001471 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001472 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001473 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001474
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001475 return &c->base;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001476
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001477err_outputs:
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001478 wayland_compositor_destroy(c);
Martin Olssonc5db50f2012-07-08 03:03:43 +02001479 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001480}