blob: 97f07d99f621f3a6d2453a521ac7f360038ee82d [file] [log] [blame]
Pekka Paalanene8de35c2012-11-07 12:25:14 +02001/*
2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
Pekka Paalanene31e0532013-05-22 18:03:07 +03004 * Copyright © 2012-2013 Raspberry Pi Foundation
Pekka Paalanene8de35c2012-11-07 12:25:14 +02005 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 */
24
Daniel Stonec228e232013-05-22 18:03:19 +030025#include "config.h"
Pekka Paalanene8de35c2012-11-07 12:25:14 +020026
27#include <errno.h>
28#include <stdlib.h>
29#include <stdio.h>
30#include <string.h>
31#include <math.h>
Jonas Ådahle0de3c22014-03-12 22:08:42 +010032#include <sys/time.h>
Pekka Paalanene8de35c2012-11-07 12:25:14 +020033#include <sys/types.h>
34#include <fcntl.h>
35#include <unistd.h>
36
Jonas Ådahle0de3c22014-03-12 22:08:42 +010037#include <linux/input.h>
Pekka Paalanene8de35c2012-11-07 12:25:14 +020038#include <libudev.h>
39
Pekka Paalanene8de35c2012-11-07 12:25:14 +020040#ifdef HAVE_BCM_HOST
41# include <bcm_host.h>
42#else
43# include "rpi-bcm-stubs.h"
44#endif
45
46#include "compositor.h"
Pekka Paalanene31e0532013-05-22 18:03:07 +030047#include "rpi-renderer.h"
Kristian Høgsberg3f495872013-09-18 23:00:17 -070048#include "launcher-util.h"
Jonas Ådahle0de3c22014-03-12 22:08:42 +010049#include "udev-input.h"
Pekka Paalanene8de35c2012-11-07 12:25:14 +020050
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +020051#if 0
52#define DBG(...) \
53 weston_log(__VA_ARGS__)
54#else
55#define DBG(...) do {} while (0)
56#endif
57
Pekka Paalanene8de35c2012-11-07 12:25:14 +020058struct rpi_compositor;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +020059struct rpi_output;
60
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +020061struct rpi_flippipe {
62 int readfd;
63 int writefd;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -040064 clockid_t clk_id;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +020065 struct wl_event_source *source;
66};
Pekka Paalanene8de35c2012-11-07 12:25:14 +020067
68struct rpi_output {
69 struct rpi_compositor *compositor;
70 struct weston_output base;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +020071 int single_buffer;
Pekka Paalanene8de35c2012-11-07 12:25:14 +020072
73 struct weston_mode mode;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +020074 struct rpi_flippipe flippipe;
Pekka Paalanene8de35c2012-11-07 12:25:14 +020075
76 DISPMANX_DISPLAY_HANDLE_T display;
Pekka Paalanene8de35c2012-11-07 12:25:14 +020077};
78
79struct rpi_seat {
80 struct weston_seat base;
81 struct wl_list devices_list;
82
83 struct udev_monitor *udev_monitor;
84 struct wl_event_source *udev_monitor_source;
85 char *seat_id;
86};
87
88struct rpi_compositor {
89 struct weston_compositor base;
90 uint32_t prev_state;
91
92 struct udev *udev;
Jonas Ådahl37d38d92013-12-02 22:05:02 +010093 struct udev_input input;
Kristian Høgsberg61741a22013-09-17 16:02:57 -070094 struct wl_listener session_listener;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +020095
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +020096 int single_buffer;
Pekka Paalanene8de35c2012-11-07 12:25:14 +020097};
98
99static inline struct rpi_output *
100to_rpi_output(struct weston_output *base)
101{
102 return container_of(base, struct rpi_output, base);
103}
104
105static inline struct rpi_seat *
106to_rpi_seat(struct weston_seat *base)
107{
108 return container_of(base, struct rpi_seat, base);
109}
110
111static inline struct rpi_compositor *
112to_rpi_compositor(struct weston_compositor *base)
113{
114 return container_of(base, struct rpi_compositor, base);
115}
116
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200117static void
118rpi_flippipe_update_complete(DISPMANX_UPDATE_HANDLE_T update, void *data)
119{
120 /* This function runs in a different thread. */
121 struct rpi_flippipe *flippipe = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400122 struct timespec ts;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200123 ssize_t ret;
124
125 /* manufacture flip completion timestamp */
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400126 clock_gettime(flippipe->clk_id, &ts);
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200127
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400128 ret = write(flippipe->writefd, &ts, sizeof ts);
129 if (ret != sizeof ts)
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200130 weston_log("ERROR: %s failed to write, ret %zd, errno %d\n",
131 __func__, ret, errno);
132}
133
134static int
135rpi_dispmanx_update_submit(DISPMANX_UPDATE_HANDLE_T update,
136 struct rpi_output *output)
137{
138 /*
139 * The callback registered here will eventually be called
140 * in a different thread context. Therefore we cannot call
141 * the usual functions from rpi_flippipe_update_complete().
142 * Instead, we have a pipe for passing the message from the
143 * thread, waking up the Weston main event loop, calling
144 * rpi_flippipe_handler(), and then ending up in
145 * rpi_output_update_complete() in the main thread context,
146 * where we can do the frame finishing work.
147 */
148 return vc_dispmanx_update_submit(update, rpi_flippipe_update_complete,
149 &output->flippipe);
150}
151
152static void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400153rpi_output_update_complete(struct rpi_output *output,
154 const struct timespec *stamp);
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200155
156static int
157rpi_flippipe_handler(int fd, uint32_t mask, void *data)
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200158{
159 struct rpi_output *output = data;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200160 ssize_t ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400161 struct timespec ts;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200162
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200163 if (mask != WL_EVENT_READABLE)
164 weston_log("ERROR: unexpected mask 0x%x in %s\n",
165 mask, __func__);
166
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400167 ret = read(fd, &ts, sizeof ts);
168 if (ret != sizeof ts) {
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200169 weston_log("ERROR: %s failed to read, ret %zd, errno %d\n",
170 __func__, ret, errno);
171 }
172
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400173 rpi_output_update_complete(output, &ts);
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200174
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200175 return 1;
176}
177
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200178static int
179rpi_flippipe_init(struct rpi_flippipe *flippipe, struct rpi_output *output)
180{
181 struct wl_event_loop *loop;
182 int fd[2];
183
184 if (pipe2(fd, O_CLOEXEC) == -1)
185 return -1;
186
187 flippipe->readfd = fd[0];
188 flippipe->writefd = fd[1];
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400189 flippipe->clk_id = output->compositor->base.presentation_clock;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200190
191 loop = wl_display_get_event_loop(output->compositor->base.wl_display);
192 flippipe->source = wl_event_loop_add_fd(loop, flippipe->readfd,
193 WL_EVENT_READABLE,
194 rpi_flippipe_handler, output);
195
196 if (!flippipe->source) {
197 close(flippipe->readfd);
198 close(flippipe->writefd);
199 return -1;
200 }
201
202 return 0;
203}
204
205static void
206rpi_flippipe_release(struct rpi_flippipe *flippipe)
207{
208 wl_event_source_remove(flippipe->source);
209 close(flippipe->readfd);
210 close(flippipe->writefd);
211}
212
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200213static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200214rpi_output_start_repaint_loop(struct weston_output *output)
215{
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400216 struct timespec ts;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200217
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400218 clock_gettime(output->compositor->presentation_clock, &ts);
219 weston_output_finish_frame(output, &ts);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200220}
221
David Herrmann1edf44c2013-10-22 17:11:26 +0200222static int
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200223rpi_output_repaint(struct weston_output *base, pixman_region32_t *damage)
224{
225 struct rpi_output *output = to_rpi_output(base);
226 struct rpi_compositor *compositor = output->compositor;
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200227 struct weston_plane *primary_plane = &compositor->base.primary_plane;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200228 DISPMANX_UPDATE_HANDLE_T update;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200229
Pekka Paalanene31e0532013-05-22 18:03:07 +0300230 DBG("frame update start\n");
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200231
Pekka Paalanene31e0532013-05-22 18:03:07 +0300232 /* Update priority higher than in rpi-renderer's
233 * output destroy function, see rpi_output_destroy().
234 */
235 update = vc_dispmanx_update_start(1);
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200236
Pekka Paalanene31e0532013-05-22 18:03:07 +0300237 rpi_renderer_set_update_handle(&output->base, update);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200238 compositor->base.renderer->repaint_output(&output->base, damage);
239
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200240 pixman_region32_subtract(&primary_plane->damage,
241 &primary_plane->damage, damage);
242
Pekka Paalanene31e0532013-05-22 18:03:07 +0300243 /* schedule callback to rpi_output_update_complete() */
244 rpi_dispmanx_update_submit(update, output);
245 DBG("frame update submitted\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200246 return 0;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200247}
248
249static void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400250rpi_output_update_complete(struct rpi_output *output,
251 const struct timespec *stamp)
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200252{
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400253 DBG("frame update complete(%ld.%09ld)\n",
254 (long)stamp->tv_sec, (long)stamp->tv_nsec);
Pekka Paalanene31e0532013-05-22 18:03:07 +0300255 rpi_renderer_finish_frame(&output->base);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400256 weston_output_finish_frame(&output->base, stamp);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200257}
258
259static void
260rpi_output_destroy(struct weston_output *base)
261{
262 struct rpi_output *output = to_rpi_output(base);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200263
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200264 DBG("%s\n", __func__);
265
Pekka Paalanene31e0532013-05-22 18:03:07 +0300266 rpi_renderer_output_destroy(base);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200267
Pekka Paalanene31e0532013-05-22 18:03:07 +0300268 /* rpi_renderer_output_destroy() will schedule a removal of
269 * all Dispmanx Elements, and wait for the update to complete.
270 * Assuming updates are sequential, the wait should guarantee,
271 * that any pending rpi_flippipe_update_complete() callbacks
272 * have happened already. Therefore we can destroy the flippipe
273 * now.
274 */
275 rpi_flippipe_release(&output->flippipe);
276
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200277 weston_output_destroy(&output->base);
278
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200279 vc_dispmanx_display_close(output->display);
280
281 free(output);
282}
283
Pekka Paalanene31e0532013-05-22 18:03:07 +0300284static const char *transform_names[] = {
285 [WL_OUTPUT_TRANSFORM_NORMAL] = "normal",
286 [WL_OUTPUT_TRANSFORM_90] = "90",
287 [WL_OUTPUT_TRANSFORM_180] = "180",
288 [WL_OUTPUT_TRANSFORM_270] = "270",
289 [WL_OUTPUT_TRANSFORM_FLIPPED] = "flipped",
290 [WL_OUTPUT_TRANSFORM_FLIPPED_90] = "flipped-90",
291 [WL_OUTPUT_TRANSFORM_FLIPPED_180] = "flipped-180",
292 [WL_OUTPUT_TRANSFORM_FLIPPED_270] = "flipped-270",
293};
294
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200295static int
Pekka Paalanene31e0532013-05-22 18:03:07 +0300296str2transform(const char *name)
297{
298 unsigned i;
299
300 for (i = 0; i < ARRAY_LENGTH(transform_names); i++)
301 if (strcmp(name, transform_names[i]) == 0)
302 return i;
303
304 return -1;
305}
306
307static const char *
308transform2str(uint32_t output_transform)
309{
310 if (output_transform >= ARRAY_LENGTH(transform_names))
311 return "<illegal value>";
312
313 return transform_names[output_transform];
314}
315
316static int
317rpi_output_create(struct rpi_compositor *compositor, uint32_t transform)
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200318{
319 struct rpi_output *output;
320 DISPMANX_MODEINFO_T modeinfo;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200321 int ret;
322 float mm_width, mm_height;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200323
324 output = calloc(1, sizeof *output);
325 if (!output)
326 return -1;
327
328 output->compositor = compositor;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200329 output->single_buffer = compositor->single_buffer;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200330
331 if (rpi_flippipe_init(&output->flippipe, output) < 0) {
332 weston_log("Creating message pipe failed.\n");
333 goto out_free;
334 }
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200335
336 output->display = vc_dispmanx_display_open(DISPMANX_ID_HDMI);
337 if (!output->display) {
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200338 weston_log("Failed to open dispmanx HDMI display.\n");
339 goto out_pipe;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200340 }
341
342 ret = vc_dispmanx_display_get_info(output->display, &modeinfo);
343 if (ret < 0) {
344 weston_log("Failed to get display mode information.\n");
345 goto out_dmx_close;
346 }
347
Jonas Ådahle5a12252013-04-05 23:07:11 +0200348 output->base.start_repaint_loop = rpi_output_start_repaint_loop;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200349 output->base.repaint = rpi_output_repaint;
350 output->base.destroy = rpi_output_destroy;
Pekka Paalanene31e0532013-05-22 18:03:07 +0300351 output->base.assign_planes = NULL;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200352 output->base.set_backlight = NULL;
353 output->base.set_dpms = NULL;
354 output->base.switch_mode = NULL;
355
356 /* XXX: use tvservice to get information from and control the
357 * HDMI and SDTV outputs. See:
358 * /opt/vc/include/interface/vmcs_host/vc_tvservice.h
359 */
360
361 /* only one static mode in list */
362 output->mode.flags =
363 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
364 output->mode.width = modeinfo.width;
365 output->mode.height = modeinfo.height;
366 output->mode.refresh = 60000;
367 wl_list_init(&output->base.mode_list);
368 wl_list_insert(&output->base.mode_list, &output->mode.link);
369
Hardeningff39efa2013-09-18 23:56:35 +0200370 output->base.current_mode = &output->mode;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200371 output->base.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
372 output->base.make = "unknown";
373 output->base.model = "unknown";
374
375 /* guess 96 dpi */
376 mm_width = modeinfo.width * (25.4f / 96.0f);
377 mm_height = modeinfo.height * (25.4f / 96.0f);
378
379 weston_output_init(&output->base, &compositor->base,
380 0, 0, round(mm_width), round(mm_height),
Pekka Paalanene31e0532013-05-22 18:03:07 +0300381 transform, 1);
John Kåre Alsaker94659272012-11-13 19:10:18 +0100382
Pekka Paalanene31e0532013-05-22 18:03:07 +0300383 if (rpi_renderer_output_create(&output->base, output->display) < 0)
John Kåre Alsaker94659272012-11-13 19:10:18 +0100384 goto out_output;
385
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200386 wl_list_insert(compositor->base.output_list.prev, &output->base.link);
387
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200388 weston_log("Raspberry Pi HDMI output %dx%d px\n",
389 output->mode.width, output->mode.height);
390 weston_log_continue(STAMP_SPACE "guessing %d Hz and 96 dpi\n",
391 output->mode.refresh / 1000);
Pekka Paalanene31e0532013-05-22 18:03:07 +0300392 weston_log_continue(STAMP_SPACE "orientation: %s\n",
393 transform2str(output->base.transform));
394
395 if (!strncmp(transform2str(output->base.transform), "flipped", 7))
396 weston_log("warning: flipped output transforms may not work\n");
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200397
398 return 0;
399
John Kåre Alsaker94659272012-11-13 19:10:18 +0100400out_output:
401 weston_output_destroy(&output->base);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200402
403out_dmx_close:
404 vc_dispmanx_display_close(output->display);
405
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200406out_pipe:
407 rpi_flippipe_release(&output->flippipe);
408
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200409out_free:
410 free(output);
411 return -1;
412}
413
414static void
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200415rpi_compositor_destroy(struct weston_compositor *base)
416{
417 struct rpi_compositor *compositor = to_rpi_compositor(base);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200418
Jonas Ådahl37d38d92013-12-02 22:05:02 +0100419 udev_input_destroy(&compositor->input);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200420
421 /* destroys outputs, too */
422 weston_compositor_shutdown(&compositor->base);
423
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700424 weston_launcher_destroy(compositor->base.launcher);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200425
426 bcm_host_deinit();
427 free(compositor);
428}
429
430static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700431session_notify(struct wl_listener *listener, void *data)
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200432{
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700433 struct rpi_compositor *compositor = data;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200434 struct weston_output *output;
435
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700436 if (compositor->base.session_active) {
437 weston_log("activating session\n");
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200438 compositor->base.state = compositor->prev_state;
439 weston_compositor_damage_all(&compositor->base);
Jonas Ådahl0feb32e2014-03-12 22:08:41 +0100440 udev_input_enable(&compositor->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700441 } else {
442 weston_log("deactivating session\n");
Jonas Ådahl37d38d92013-12-02 22:05:02 +0100443 udev_input_disable(&compositor->input);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200444
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200445 compositor->prev_state = compositor->base.state;
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100446 weston_compositor_offscreen(&compositor->base);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200447
448 /* If we have a repaint scheduled (either from a
449 * pending pageflip or the idle handler), make sure we
450 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100451 * vt switched away. The OFFSCREEN state will prevent
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200452 * further attemps at repainting. When we switch
453 * back, we schedule a repaint, which will process
454 * pending frame callbacks. */
455
456 wl_list_for_each(output,
457 &compositor->base.output_list, link) {
458 output->repaint_needed = 0;
459 }
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200460 };
461}
462
463static void
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700464rpi_restore(struct weston_compositor *compositor)
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200465{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700466 weston_launcher_restore(compositor->launcher);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200467}
468
469static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400470switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200471{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700472 struct weston_compositor *compositor = data;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200473
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700474 weston_launcher_activate_vt(compositor->launcher, key - KEY_F1 + 1);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200475}
476
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200477struct rpi_parameters {
478 int tty;
Pekka Paalanene31e0532013-05-22 18:03:07 +0300479 struct rpi_renderer_parameters renderer;
480 uint32_t output_transform;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200481};
482
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200483static struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500484rpi_compositor_create(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400485 struct weston_config *config,
486 struct rpi_parameters *param)
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200487{
488 struct rpi_compositor *compositor;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200489 uint32_t key;
490
491 weston_log("initializing Raspberry Pi backend\n");
492
493 compositor = calloc(1, sizeof *compositor);
494 if (compositor == NULL)
495 return NULL;
496
497 if (weston_compositor_init(&compositor->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400498 config) < 0)
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200499 goto out_free;
500
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400501 if (weston_compositor_set_presentation_clock_software(
502 &compositor->base) < 0)
503 goto out_compositor;
504
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200505 compositor->udev = udev_new();
506 if (compositor->udev == NULL) {
507 weston_log("Failed to initialize udev context.\n");
508 goto out_compositor;
509 }
510
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700511 compositor->session_listener.notify = session_notify;
512 wl_signal_add(&compositor->base.session_signal,
513 &compositor ->session_listener);
Kristian Høgsberg6ff3ff52013-10-02 10:53:33 -0700514 compositor->base.launcher =
David Herrmanncc5b2ed2013-10-22 00:28:09 +0200515 weston_launcher_connect(&compositor->base, param->tty, "seat0");
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700516 if (!compositor->base.launcher) {
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200517 weston_log("Failed to initialize tty.\n");
Pekka Paalanen47733322013-12-04 15:34:48 +0200518 goto out_udev;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200519 }
520
521 compositor->base.destroy = rpi_compositor_destroy;
522 compositor->base.restore = rpi_restore;
523
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200524 compositor->prev_state = WESTON_COMPOSITOR_ACTIVE;
Pekka Paalanene31e0532013-05-22 18:03:07 +0300525 compositor->single_buffer = param->renderer.single_buffer;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200526
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200527 weston_log("Dispmanx planes are %s buffered.\n",
528 compositor->single_buffer ? "single" : "double");
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200529
530 for (key = KEY_F1; key < KEY_F9; key++)
531 weston_compositor_add_key_binding(&compositor->base, key,
532 MODIFIER_CTRL | MODIFIER_ALT,
533 switch_vt_binding, compositor);
534
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200535 /*
536 * bcm_host_init() creates threads.
537 * Therefore we must have all signal handlers set and signals blocked
538 * before calling it. Otherwise the signals may end in the bcm
539 * threads and cause the default behaviour there. For instance,
540 * SIGUSR1 used for VT switching caused Weston to terminate there.
541 */
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200542 bcm_host_init();
543
Pekka Paalanene31e0532013-05-22 18:03:07 +0300544 if (rpi_renderer_create(&compositor->base, &param->renderer) < 0)
Emilio Pozuelo Monfortbf539e32014-02-03 16:57:27 +0100545 goto out_launcher;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200546
Pekka Paalanene31e0532013-05-22 18:03:07 +0300547 if (rpi_output_create(compositor, param->output_transform) < 0)
548 goto out_renderer;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200549
Emilio Pozuelo Monfortbf539e32014-02-03 16:57:27 +0100550 if (udev_input_init(&compositor->input,
551 &compositor->base,
552 compositor->udev, "seat0") != 0) {
553 weston_log("Failed to initialize udev input.\n");
554 goto out_renderer;
555 }
556
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200557 return &compositor->base;
558
Pekka Paalanene31e0532013-05-22 18:03:07 +0300559out_renderer:
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +0300560 compositor->base.renderer->destroy(&compositor->base);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200561
Pekka Paalanen47733322013-12-04 15:34:48 +0200562out_launcher:
563 weston_launcher_destroy(compositor->base.launcher);
564
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200565out_udev:
566 udev_unref(compositor->udev);
567
568out_compositor:
569 weston_compositor_shutdown(&compositor->base);
570
571out_free:
572 bcm_host_deinit();
573 free(compositor);
574
575 return NULL;
576}
577
578WL_EXPORT struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500579backend_init(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400580 struct weston_config *config)
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200581{
Pekka Paalanene31e0532013-05-22 18:03:07 +0300582 const char *transform = "normal";
583 int ret;
584
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200585 struct rpi_parameters param = {
586 .tty = 0, /* default to current tty */
Pekka Paalanene31e0532013-05-22 18:03:07 +0300587 .renderer.single_buffer = 0,
588 .output_transform = WL_OUTPUT_TRANSFORM_NORMAL,
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100589 .renderer.opaque_regions = 0,
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200590 };
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200591
592 const struct weston_option rpi_options[] = {
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200593 { WESTON_OPTION_INTEGER, "tty", 0, &param.tty },
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200594 { WESTON_OPTION_BOOLEAN, "single-buffer", 0,
Pekka Paalanene31e0532013-05-22 18:03:07 +0300595 &param.renderer.single_buffer },
596 { WESTON_OPTION_STRING, "transform", 0, &transform },
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +0100597 { WESTON_OPTION_BOOLEAN, "opaque-regions", 0,
598 &param.renderer.opaque_regions },
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200599 };
600
601 parse_options(rpi_options, ARRAY_LENGTH(rpi_options), argc, argv);
602
Pekka Paalanene31e0532013-05-22 18:03:07 +0300603 ret = str2transform(transform);
604 if (ret < 0)
605 weston_log("invalid transform \"%s\"\n", transform);
606 else
607 param.output_transform = ret;
608
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400609 return rpi_compositor_create(display, argc, argv, config, &param);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200610}