blob: 5d86ca2623157dcfd66a3de4dfe28c400dec26f5 [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 Ekstrand48ce4212013-10-27 22:25:02 -0500710 int32_t x, int32_t y)
711{
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
759 weston_config_section_get_string(config_section,
760 "transform", &t, "normal");
761 transform = WL_OUTPUT_TRANSFORM_NORMAL;
762 for (i = 0; i < ARRAY_LENGTH(transform_names); i++) {
763 if (strcmp(transform_names[i].name, t) == 0) {
764 transform = transform_names[i].token;
765 break;
766 }
767 }
768 if (i >= ARRAY_LENGTH(transform_names))
769 weston_log("Invalid transform \"%s\" for output %s\n", t, name);
770 free(t);
771
772 output = wayland_output_create(c, x, y, width, height, name, transform, scale);
773 free(name);
774
775 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100776}
777
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300778static void
779shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
780 uint32_t serial)
781{
782 wl_shell_surface_pong(shell_surface, serial);
783}
784
785static void
786shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
787 uint32_t edges, int32_t width, int32_t height)
788{
789 /* FIXME: implement resizing */
790}
791
792static void
793shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
794{
795}
796
797static const struct wl_shell_surface_listener shell_surface_listener = {
798 shell_surface_ping,
799 shell_surface_configure,
800 shell_surface_popup_done
801};
802
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100803/* Events received from the wayland-server this compositor is client of: */
804
Jason Ekstrand7744f712013-10-27 22:24:55 -0500805/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400806static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500807input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400808{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400809
Jason Ekstrand7744f712013-10-27 22:24:55 -0500810 struct wl_buffer *buffer;
811 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400812
Jason Ekstrand7744f712013-10-27 22:24:55 -0500813 if (!input->compositor->cursor)
814 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400815
Jason Ekstrand7744f712013-10-27 22:24:55 -0500816 image = input->compositor->cursor->images[0];
817 buffer = wl_cursor_image_get_buffer(image);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400818
Jason Ekstrand7744f712013-10-27 22:24:55 -0500819
820 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
821 input->parent.cursor.surface,
822 image->hotspot_x, image->hotspot_y);
823
824 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
825 wl_surface_damage(input->parent.cursor.surface, 0, 0,
826 image->width, image->height);
827 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400828}
829
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100830static void
Daniel Stone37816df2012-05-16 18:45:18 +0100831input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
832 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400833 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100834{
835 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500836 int32_t fx, fy;
837 enum theme_location location;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100838
Daniel Stone50692802012-06-22 13:21:41 +0100839 /* XXX: If we get a modifier event immediately before the focus,
840 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400841 input->enter_serial = serial;
842 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500843
844 if (input->output->frame) {
845 location = frame_pointer_enter(input->output->frame, input,
846 wl_fixed_to_int(x),
847 wl_fixed_to_int(y));
848 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
849 x -= wl_fixed_from_int(fx);
850 y -= wl_fixed_from_int(fy);
851
852 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
853 weston_output_schedule_repaint(&input->output->base);
854 } else {
855 location = THEME_LOCATION_CLIENT_AREA;
856 }
857
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500858 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500859
860 if (location == THEME_LOCATION_CLIENT_AREA) {
861 input->focus = 1;
862 notify_pointer_focus(&input->base, &input->output->base, x, y);
863 wl_pointer_set_cursor(input->parent.pointer,
864 input->enter_serial, NULL, 0, 0);
865 } else {
866 input->focus = 0;
867 notify_pointer_focus(&input->base, NULL, 0, 0);
868 input_set_cursor(input);
869 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100870}
871
872static void
Daniel Stone37816df2012-05-16 18:45:18 +0100873input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
874 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500875{
876 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500877
Jason Ekstrand7744f712013-10-27 22:24:55 -0500878 if (input->output->frame) {
879 frame_pointer_leave(input->output->frame, input);
880
881 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
882 weston_output_schedule_repaint(&input->output->base);
883 }
884
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400885 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400886 input->output = NULL;
887 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -0500888}
889
890static void
Daniel Stone37816df2012-05-16 18:45:18 +0100891input_handle_motion(void *data, struct wl_pointer *pointer,
892 uint32_t time, wl_fixed_t x, wl_fixed_t y)
893{
894 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500895 int32_t fx, fy;
896 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +0100897
Jason Ekstrand7744f712013-10-27 22:24:55 -0500898 if (input->output->frame) {
899 location = frame_pointer_motion(input->output->frame, input,
900 wl_fixed_to_int(x),
901 wl_fixed_to_int(y));
902 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
903 x -= wl_fixed_from_int(fx);
904 y -= wl_fixed_from_int(fy);
905
906 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
907 weston_output_schedule_repaint(&input->output->base);
908 } else {
909 location = THEME_LOCATION_CLIENT_AREA;
910 }
911
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500912 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500913
914 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
915 input_set_cursor(input);
916 notify_pointer_focus(&input->base, NULL, 0, 0);
917 input->focus = 0;
918 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
919 wl_pointer_set_cursor(input->parent.pointer,
920 input->enter_serial, NULL, 0, 0);
921 notify_pointer_focus(&input->base, &input->output->base, x, y);
922 input->focus = 1;
923 }
924
925 if (location == THEME_LOCATION_CLIENT_AREA)
926 notify_motion_absolute(&input->base, time, x, y);
Daniel Stone37816df2012-05-16 18:45:18 +0100927}
928
929static void
930input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100931 uint32_t serial, uint32_t time, uint32_t button,
932 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +0100933{
934 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +0100935 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500936 enum frame_button_state fstate;
937 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +0100938
Jason Ekstrand7744f712013-10-27 22:24:55 -0500939 if (input->output->frame) {
940 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
941 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
942
943 location = frame_pointer_button(input->output->frame, input,
944 button, fstate);
945
946 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
947
948 wl_shell_surface_move(input->output->parent.shell_surface,
949 input->parent.seat, serial);
950 frame_status_clear(input->output->frame,
951 FRAME_STATUS_MOVE);
952 return;
953 }
954
955 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE)
956 wl_display_terminate(input->compositor->base.wl_display);
957
958 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
959 weston_output_schedule_repaint(&input->output->base);
960 } else {
961 location = THEME_LOCATION_CLIENT_AREA;
962 }
963
964 if (location == THEME_LOCATION_CLIENT_AREA)
965 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +0100966}
967
968static void
969input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100970 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +0100971{
972 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100973
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400974 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +0100975}
976
977static const struct wl_pointer_listener pointer_listener = {
978 input_handle_pointer_enter,
979 input_handle_pointer_leave,
980 input_handle_motion,
981 input_handle_button,
982 input_handle_axis,
983};
984
985static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +0100986input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
987 int fd, uint32_t size)
988{
989 struct wayland_input *input = data;
990 struct xkb_keymap *keymap;
991 char *map_str;
992
993 if (!data) {
994 close(fd);
995 return;
996 }
997
998 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
999 close(fd);
1000 return;
1001 }
1002
1003 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1004 if (map_str == MAP_FAILED) {
1005 close(fd);
1006 return;
1007 }
1008
1009 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
1010 map_str,
1011 XKB_KEYMAP_FORMAT_TEXT_V1,
1012 0);
1013 munmap(map_str, size);
1014 close(fd);
1015
1016 if (!keymap) {
Martin Minarik6d118362012-06-07 18:01:59 +02001017 weston_log("failed to compile keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001018 return;
1019 }
1020
Rui Matos0c194ce2013-10-10 19:44:21 +02001021 if (input->base.keyboard)
1022 weston_seat_update_keymap(&input->base, keymap);
1023 else
1024 weston_seat_init_keyboard(&input->base, keymap);
1025
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001026 xkb_map_unref(keymap);
1027}
1028
1029static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001030input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001031 struct wl_keyboard *keyboard,
1032 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001033 struct wl_surface *surface,
1034 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001035{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001036 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001037 struct wayland_output *focus;
1038
1039 focus = input->keyboard_focus;
1040 if (focus) {
1041 /* This shouldn't happen */
1042 focus->keyboard_count--;
1043 if (!focus->keyboard_count && focus->frame)
1044 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1045 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1046 weston_output_schedule_repaint(&focus->base);
1047 }
1048
1049 input->keyboard_focus = wl_surface_get_user_data(surface);
1050 input->keyboard_focus->keyboard_count++;
1051
1052 focus = input->keyboard_focus;
1053 if (focus->frame) {
1054 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1055 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1056 weston_output_schedule_repaint(&focus->base);
1057 }
1058
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001059
Daniel Stone50692802012-06-22 13:21:41 +01001060 /* XXX: If we get a modifier event immediately before the focus,
1061 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001062 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001063 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001064}
1065
1066static void
1067input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001068 struct wl_keyboard *keyboard,
1069 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001070 struct wl_surface *surface)
1071{
1072 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001073 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001074
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001075 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001076
1077 focus = input->keyboard_focus;
1078 if (!focus)
1079 return; /* This shouldn't happen */
1080
1081 focus->keyboard_count--;
1082 if (!focus->keyboard_count && focus->frame) {
1083 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1084 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1085 weston_output_schedule_repaint(&focus->base);
1086 }
1087
1088 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001089}
1090
Daniel Stone37816df2012-05-16 18:45:18 +01001091static void
1092input_handle_key(void *data, struct wl_keyboard *keyboard,
1093 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1094{
1095 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001096
Daniel Stone50692802012-06-22 13:21:41 +01001097 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001098 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001099 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001100 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stone50692802012-06-22 13:21:41 +01001101 STATE_UPDATE_NONE);
Daniel Stone37816df2012-05-16 18:45:18 +01001102}
1103
Daniel Stone351eb612012-05-31 15:27:47 -04001104static void
1105input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001106 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001107 uint32_t mods_latched, uint32_t mods_locked,
1108 uint32_t group)
1109{
Daniel Stone50692802012-06-22 13:21:41 +01001110 struct wayland_input *input = data;
1111 struct wayland_compositor *c = input->compositor;
1112 uint32_t serial_out;
1113
1114 /* If we get a key event followed by a modifier event with the
1115 * same serial number, then we try to preserve those semantics by
1116 * reusing the same serial number on the way out too. */
1117 if (serial_in == input->key_serial)
1118 serial_out = wl_display_get_serial(c->base.wl_display);
1119 else
1120 serial_out = wl_display_next_serial(c->base.wl_display);
1121
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001122 xkb_state_update_mask(input->base.xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001123 mods_depressed, mods_latched,
1124 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001125 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001126}
1127
Daniel Stone37816df2012-05-16 18:45:18 +01001128static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001129 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001130 input_handle_keyboard_enter,
1131 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001132 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001133 input_handle_modifiers,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001134};
1135
1136static void
Daniel Stone37816df2012-05-16 18:45:18 +01001137input_handle_capabilities(void *data, struct wl_seat *seat,
1138 enum wl_seat_capability caps)
1139{
1140 struct wayland_input *input = data;
1141
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001142 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1143 input->parent.pointer = wl_seat_get_pointer(seat);
1144 wl_pointer_set_user_data(input->parent.pointer, input);
1145 wl_pointer_add_listener(input->parent.pointer,
1146 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001147 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001148 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1149 wl_pointer_destroy(input->parent.pointer);
1150 input->parent.pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001151 }
1152
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001153 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1154 input->parent.keyboard = wl_seat_get_keyboard(seat);
1155 wl_keyboard_set_user_data(input->parent.keyboard, input);
1156 wl_keyboard_add_listener(input->parent.keyboard,
1157 &keyboard_listener, input);
1158 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1159 wl_keyboard_destroy(input->parent.keyboard);
1160 input->parent.keyboard = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001161 }
1162}
1163
1164static const struct wl_seat_listener seat_listener = {
1165 input_handle_capabilities,
1166};
1167
1168static void
1169display_add_seat(struct wayland_compositor *c, uint32_t id)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001170{
1171 struct wayland_input *input;
1172
Peter Huttererf3d62272013-08-08 11:57:05 +10001173 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001174 if (input == NULL)
1175 return;
1176
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001177 weston_seat_init(&input->base, &c->base, "default");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001178 input->compositor = c;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001179 input->parent.seat = wl_registry_bind(c->parent.registry, id,
1180 &wl_seat_interface, 1);
Jason Ekstrand06ced802013-11-07 20:13:29 -06001181 wl_list_insert(c->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001182
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001183 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1184 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001185
1186 input->parent.cursor.surface =
1187 wl_compositor_create_surface(c->parent.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001188}
1189
1190static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001191registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1192 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001193{
1194 struct wayland_compositor *c = data;
1195
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001196 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001197 c->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001198 wl_registry_bind(registry, name,
1199 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001200 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001201 c->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001202 wl_registry_bind(registry, name,
1203 &wl_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01001204 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001205 display_add_seat(c, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001206 } else if (strcmp(interface, "wl_shm") == 0) {
1207 c->parent.shm =
1208 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001209 }
1210}
1211
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001212static const struct wl_registry_listener registry_listener = {
1213 registry_handle_global
1214};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001215
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001216static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001217wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
1218{
1219 struct wayland_compositor *c = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001220 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001221
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001222 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1223 wl_display_terminate(c->base.wl_display);
1224 return 0;
1225 }
1226
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001227 if (mask & WL_EVENT_READABLE)
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001228 count = wl_display_dispatch(c->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05001229 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001230 wl_display_flush(c->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001231
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001232 if (mask == 0) {
1233 count = wl_display_dispatch_pending(c->parent.wl_display);
1234 wl_display_flush(c->parent.wl_display);
1235 }
1236
1237 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001238}
1239
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001240static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001241wayland_restore(struct weston_compositor *ec)
1242{
1243}
1244
1245static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001246wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001247{
Jonas Ådahle5a12252013-04-05 23:07:11 +02001248 struct wayland_compositor *c = (struct wayland_compositor *) ec;
1249
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001250 ec->renderer->destroy(ec);
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001251
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001252 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001253
Jonas Ådahle5a12252013-04-05 23:07:11 +02001254 if (c->parent.shm)
1255 wl_shm_destroy(c->parent.shm);
1256
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001257 free(ec);
1258}
1259
Jason Ekstrand7744f712013-10-27 22:24:55 -05001260static const char *left_ptrs[] = {
1261 "left_ptr",
1262 "default",
1263 "top_left_arrow",
1264 "left-arrow"
1265};
1266
1267static void
1268create_cursor(struct wayland_compositor *c, struct weston_config *config)
1269{
1270 struct weston_config_section *s;
1271 int size;
1272 char *theme = NULL;
1273 unsigned int i;
1274
1275 s = weston_config_get_section(config, "shell", NULL, NULL);
1276 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1277 weston_config_section_get_int(s, "cursor-size", &size, 32);
1278
1279 c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm);
1280
1281 c->cursor = NULL;
1282 for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1283 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
1284 left_ptrs[i]);
1285 if (!c->cursor) {
1286 fprintf(stderr, "could not load left cursor\n");
1287 return;
1288 }
1289}
1290
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001291static struct wayland_compositor *
1292wayland_compositor_create(struct wl_display *display, int use_pixman,
1293 const char *display_name, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001294 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001295{
1296 struct wayland_compositor *c;
1297 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001298 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001299
Peter Huttererf3d62272013-08-08 11:57:05 +10001300 c = zalloc(sizeof *c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001301 if (c == NULL)
1302 return NULL;
1303
Daniel Stone725c2c32012-06-22 14:04:36 +01001304 if (weston_compositor_init(&c->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001305 config) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +02001306 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001307
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001308 c->parent.wl_display = wl_display_connect(display_name);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001309
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001310 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001311 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02001312 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001313 }
1314
Jason Ekstrand06ced802013-11-07 20:13:29 -06001315 wl_list_init(&c->input_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001316 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
1317 wl_registry_add_listener(c->parent.registry, &registry_listener, c);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001318 wl_display_roundtrip(c->parent.wl_display);
1319
1320 create_cursor(c, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001321
1322 c->base.wl_display = display;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001323
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001324 c->use_pixman = use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001325
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001326 if (!c->use_pixman) {
1327 gl_renderer = weston_load_module("gl-renderer.so",
1328 "gl_renderer_interface");
1329 if (!gl_renderer)
1330 c->use_pixman = 1;
1331 }
1332
1333 if (!c->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001334 if (gl_renderer->create(&c->base, c->parent.wl_display,
1335 gl_renderer->alpha_attribs,
1336 NULL) < 0) {
1337 weston_log("Failed to initialize the GL renderer; "
1338 "falling back to pixman.\n");
1339 c->use_pixman = 1;
1340 }
1341 }
1342
1343 if (c->use_pixman) {
1344 if (pixman_renderer_init(&c->base) < 0) {
1345 weston_log("Failed to initialize pixman renderer\n");
1346 goto err_display;
1347 }
1348 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001349
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001350 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001351 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001352
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001353 loop = wl_display_get_event_loop(c->base.wl_display);
1354
1355 fd = wl_display_get_fd(c->parent.wl_display);
1356 c->parent.wl_source =
1357 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1358 wayland_compositor_handle_event, c);
1359 if (c->parent.wl_source == NULL)
1360 goto err_renderer;
1361
1362 wl_event_source_check(c->parent.wl_source);
1363
1364 return c;
1365err_renderer:
1366 c->base.renderer->destroy(&c->base);
1367err_display:
1368 wl_display_disconnect(c->parent.wl_display);
1369err_compositor:
1370 weston_compositor_shutdown(&c->base);
1371err_free:
1372 free(c);
1373 return NULL;
1374}
1375
1376static void
1377wayland_compositor_destroy(struct wayland_compositor *c)
1378{
1379 struct weston_output *output;
1380
1381 wl_list_for_each(output, &c->base.output_list, link)
1382 wayland_output_destroy(output);
1383
1384 c->base.renderer->destroy(&c->base);
1385 wl_display_disconnect(c->parent.wl_display);
1386
1387 if (c->theme)
1388 theme_destroy(c->theme);
1389 if (c->frame_device)
1390 cairo_device_destroy(c->frame_device);
1391 wl_cursor_theme_destroy(c->cursor_theme);
1392
1393 weston_compositor_shutdown(&c->base);
1394 free(c);
1395}
1396
1397WL_EXPORT struct weston_compositor *
1398backend_init(struct wl_display *display, int *argc, char *argv[],
1399 struct weston_config *config)
1400{
1401 struct wayland_compositor *c;
1402 struct wayland_output *output;
1403 struct weston_config_section *section;
1404 int x, count, width, height, use_pixman;
1405 const char *section_name, *display_name;
1406 char *name;
1407
1408 const struct weston_option wayland_options[] = {
1409 { WESTON_OPTION_INTEGER, "width", 0, &width },
1410 { WESTON_OPTION_INTEGER, "height", 0, &height },
1411 { WESTON_OPTION_STRING, "display", 0, &display_name },
1412 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
1413 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
1414 };
1415
1416 width = 0;
1417 height = 0;
1418 display_name = NULL;
1419 use_pixman = 0;
1420 count = 1;
1421
1422 parse_options(wayland_options,
1423 ARRAY_LENGTH(wayland_options), argc, argv);
1424
1425 c = wayland_compositor_create(display, use_pixman, display_name,
1426 argc, argv, config);
1427 if (!c)
1428 return NULL;
1429
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001430 section = NULL;
1431 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001432 while (weston_config_next_section(config, &section, &section_name)) {
1433 if (!section_name || strcmp(section_name, "output") != 0)
1434 continue;
1435 weston_config_section_get_string(section, "name", &name, NULL);
1436 if (name == NULL)
1437 continue;
1438
1439 if (name[0] != 'W' || name[1] != 'L') {
1440 free(name);
1441 continue;
1442 }
1443 free(name);
1444
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001445 output = wayland_output_create_for_config(c, section, width,
1446 height, x, 0);
1447 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001448 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001449
1450 x += output->base.width;
1451 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001452 }
1453
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001454 if (!width)
1455 width = 1024;
1456 if (!height)
1457 height = 640;
1458 while (count > 0) {
1459 output = wayland_output_create(c, x, 0, width, height, NULL, 0, 1);
1460 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001461 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001462
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001463 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001464 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001465 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001466
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001467 return &c->base;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001468
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001469err_outputs:
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001470 wayland_compositor_destroy(c);
Martin Olssonc5db50f2012-07-08 03:03:43 +02001471 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001472}