blob: 3338b017a98efdcb43b9fcd9a40f5d81a8a852a3 [file] [log] [blame]
Philip Withnall4f499172013-02-02 12:02:32 +00001/*
2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
4 * Copyright © 2012 Raspberry Pi Foundation
5 * Copyright © 2013 Philip Withnall
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of the copyright holders not be used in
12 * advertising or publicity pertaining to distribution of the software
13 * without specific, written prior permission. The copyright holders make
14 * no representations about the suitability of this software for any
15 * purpose. It is provided "as is" without express or implied warranty.
16 *
17 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
18 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
19 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
21 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
22 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
23 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
Philip Withnall4f499172013-02-02 12:02:32 +000027
28#include <errno.h>
29#include <stdlib.h>
30#include <stdio.h>
31#include <string.h>
32#include <math.h>
33#include <sys/mman.h>
34#include <sys/types.h>
35#include <fcntl.h>
36#include <unistd.h>
37#include <linux/fb.h>
Kristian Høgsberg7e597f22013-02-18 16:35:26 -050038#include <linux/input.h>
Philip Withnall4f499172013-02-02 12:02:32 +000039
40#include <libudev.h>
41
Philip Withnall4f499172013-02-02 12:02:32 +000042#include "compositor.h"
43#include "launcher-util.h"
44#include "pixman-renderer.h"
Kristian Høgsberg7e597f22013-02-18 16:35:26 -050045#include "udev-seat.h"
Philip Withnall4f499172013-02-02 12:02:32 +000046
47struct fbdev_compositor {
48 struct weston_compositor base;
49 uint32_t prev_state;
50
51 struct udev *udev;
52 struct tty *tty;
Rob Bradfordd355b802013-05-31 18:09:55 +010053 struct udev_input input;
Philip Withnall4f499172013-02-02 12:02:32 +000054};
55
56struct fbdev_screeninfo {
57 unsigned int x_resolution; /* pixels, visible area */
58 unsigned int y_resolution; /* pixels, visible area */
59 unsigned int width_mm; /* visible screen width in mm */
60 unsigned int height_mm; /* visible screen height in mm */
61 unsigned int bits_per_pixel;
62
63 size_t buffer_length; /* length of frame buffer memory in bytes */
64 size_t line_length; /* length of a line in bytes */
65 char id[16]; /* screen identifier */
66
67 pixman_format_code_t pixel_format; /* frame buffer pixel format */
68 unsigned int refresh_rate; /* Hertz */
69};
70
71struct fbdev_output {
72 struct fbdev_compositor *compositor;
73 struct weston_output base;
74
75 struct weston_mode mode;
76 struct wl_event_source *finish_frame_timer;
77
78 /* Frame buffer details. */
79 const char *device; /* ownership shared with fbdev_parameters */
80 struct fbdev_screeninfo fb_info;
81 void *fb; /* length is fb_info.buffer_length */
82
83 /* pixman details. */
84 pixman_image_t *hw_surface;
85 pixman_image_t *shadow_surface;
86 void *shadow_buf;
87 uint8_t depth;
88};
89
Philip Withnall4f499172013-02-02 12:02:32 +000090struct fbdev_parameters {
91 int tty;
92 char *device;
93};
94
Kristian Høgsberg7e597f22013-02-18 16:35:26 -050095static const char default_seat[] = "seat0";
96
Philip Withnall4f499172013-02-02 12:02:32 +000097static inline struct fbdev_output *
98to_fbdev_output(struct weston_output *base)
99{
100 return container_of(base, struct fbdev_output, base);
101}
102
Philip Withnall4f499172013-02-02 12:02:32 +0000103static inline struct fbdev_compositor *
104to_fbdev_compositor(struct weston_compositor *base)
105{
106 return container_of(base, struct fbdev_compositor, base);
107}
108
109static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200110fbdev_output_start_repaint_loop(struct weston_output *output)
111{
112 uint32_t msec;
113 struct timeval tv;
114
115 gettimeofday(&tv, NULL);
116 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
117 weston_output_finish_frame(output, msec);
118}
119
120static void
Philip Withnall4f499172013-02-02 12:02:32 +0000121fbdev_output_repaint(struct weston_output *base, pixman_region32_t *damage)
122{
123 struct fbdev_output *output = to_fbdev_output(base);
124 struct weston_compositor *ec = output->base.compositor;
125 pixman_box32_t *rects;
126 int nrects, i, src_x, src_y, x1, y1, x2, y2, width, height;
127
128 /* Repaint the damaged region onto the back buffer. */
129 pixman_renderer_output_set_buffer(base, output->shadow_surface);
130 ec->renderer->repaint_output(base, damage);
131
132 /* Transform and composite onto the frame buffer. */
133 width = pixman_image_get_width(output->shadow_surface);
134 height = pixman_image_get_height(output->shadow_surface);
135 rects = pixman_region32_rectangles(damage, &nrects);
136
137 for (i = 0; i < nrects; i++) {
138 switch (base->transform) {
139 default:
140 case WL_OUTPUT_TRANSFORM_NORMAL:
141 x1 = rects[i].x1;
142 x2 = rects[i].x2;
143 y1 = rects[i].y1;
144 y2 = rects[i].y2;
145 break;
146 case WL_OUTPUT_TRANSFORM_180:
147 x1 = width - rects[i].x2;
148 x2 = width - rects[i].x1;
149 y1 = height - rects[i].y2;
150 y2 = height - rects[i].y1;
151 break;
152 case WL_OUTPUT_TRANSFORM_90:
153 x1 = height - rects[i].y2;
154 x2 = height - rects[i].y1;
155 y1 = rects[i].x1;
156 y2 = rects[i].x2;
157 break;
158 case WL_OUTPUT_TRANSFORM_270:
159 x1 = rects[i].y1;
160 x2 = rects[i].y2;
161 y1 = width - rects[i].x2;
162 y2 = width - rects[i].x1;
163 break;
164 }
165 src_x = x1;
166 src_y = y1;
167
168 pixman_image_composite32(PIXMAN_OP_SRC,
169 output->shadow_surface, /* src */
170 NULL /* mask */,
171 output->hw_surface, /* dest */
172 src_x, src_y, /* src_x, src_y */
173 0, 0, /* mask_x, mask_y */
174 x1, y1, /* dest_x, dest_y */
175 x2 - x1, /* width */
176 y2 - y1 /* height */);
177 }
178
179 /* Update the damage region. */
180 pixman_region32_subtract(&ec->primary_plane.damage,
181 &ec->primary_plane.damage, damage);
182
183 /* Schedule the end of the frame. We do not sync this to the frame
184 * buffer clock because users who want that should be using the DRM
185 * compositor. FBIO_WAITFORVSYNC blocks and FB_ACTIVATE_VBL requires
186 * panning, which is broken in most kernel drivers.
187 *
188 * Finish the frame synchronised to the specified refresh rate. The
189 * refresh rate is given in mHz and the interval in ms. */
190 wl_event_source_timer_update(output->finish_frame_timer,
191 1000000 / output->mode.refresh);
192}
193
194static int
195finish_frame_handler(void *data)
196{
197 struct fbdev_output *output = data;
Philip Withnall4f499172013-02-02 12:02:32 +0000198
Jonas Ådahle5a12252013-04-05 23:07:11 +0200199 fbdev_output_start_repaint_loop(&output->base);
Philip Withnall4f499172013-02-02 12:02:32 +0000200
201 return 1;
202}
203
204static pixman_format_code_t
205calculate_pixman_format(struct fb_var_screeninfo *vinfo,
206 struct fb_fix_screeninfo *finfo)
207{
208 /* Calculate the pixman format supported by the frame buffer from the
209 * buffer's metadata. Return 0 if no known pixman format is supported
210 * (since this has depth 0 it's guaranteed to not conflict with any
211 * actual pixman format).
212 *
213 * Documentation on the vinfo and finfo structures:
214 * http://www.mjmwired.net/kernel/Documentation/fb/api.txt
215 *
216 * TODO: Try a bit harder to support other formats, including setting
217 * the preferred format in the hardware. */
218 int type;
219
220 weston_log("Calculating pixman format from:\n"
221 STAMP_SPACE " - type: %i (aux: %i)\n"
222 STAMP_SPACE " - visual: %i\n"
223 STAMP_SPACE " - bpp: %i (grayscale: %i)\n"
224 STAMP_SPACE " - red: offset: %i, length: %i, MSB: %i\n"
225 STAMP_SPACE " - green: offset: %i, length: %i, MSB: %i\n"
226 STAMP_SPACE " - blue: offset: %i, length: %i, MSB: %i\n"
227 STAMP_SPACE " - transp: offset: %i, length: %i, MSB: %i\n",
228 finfo->type, finfo->type_aux, finfo->visual,
229 vinfo->bits_per_pixel, vinfo->grayscale,
230 vinfo->red.offset, vinfo->red.length, vinfo->red.msb_right,
231 vinfo->green.offset, vinfo->green.length,
232 vinfo->green.msb_right,
233 vinfo->blue.offset, vinfo->blue.length,
234 vinfo->blue.msb_right,
235 vinfo->transp.offset, vinfo->transp.length,
236 vinfo->transp.msb_right);
237
238 /* We only handle packed formats at the moment. */
239 if (finfo->type != FB_TYPE_PACKED_PIXELS)
240 return 0;
241
242 /* We only handle true-colour frame buffers at the moment. */
Marc Chalainffbddff2013-09-03 16:47:43 +0200243 switch(finfo->visual) {
244 case FB_VISUAL_TRUECOLOR:
245 case FB_VISUAL_DIRECTCOLOR:
246 if (vinfo->grayscale != 0)
247 return 0;
248 break;
249 default:
250 return 0;
251 }
Philip Withnall4f499172013-02-02 12:02:32 +0000252
253 /* We only support formats with MSBs on the left. */
254 if (vinfo->red.msb_right != 0 || vinfo->green.msb_right != 0 ||
255 vinfo->blue.msb_right != 0)
256 return 0;
257
258 /* Work out the format type from the offsets. We only support RGBA and
259 * ARGB at the moment. */
260 type = PIXMAN_TYPE_OTHER;
261
262 if ((vinfo->transp.offset >= vinfo->red.offset ||
263 vinfo->transp.length == 0) &&
264 vinfo->red.offset >= vinfo->green.offset &&
265 vinfo->green.offset >= vinfo->blue.offset)
266 type = PIXMAN_TYPE_ARGB;
267 else if (vinfo->red.offset >= vinfo->green.offset &&
268 vinfo->green.offset >= vinfo->blue.offset &&
269 vinfo->blue.offset >= vinfo->transp.offset)
270 type = PIXMAN_TYPE_RGBA;
271
272 if (type == PIXMAN_TYPE_OTHER)
273 return 0;
274
275 /* Build the format. */
276 return PIXMAN_FORMAT(vinfo->bits_per_pixel, type,
277 vinfo->transp.length,
278 vinfo->red.length,
279 vinfo->green.length,
280 vinfo->blue.length);
281}
282
283static int
284calculate_refresh_rate(struct fb_var_screeninfo *vinfo)
285{
286 uint64_t quot;
287
288 /* Calculate monitor refresh rate. Default is 60 Hz. Units are mHz. */
289 quot = (vinfo->upper_margin + vinfo->lower_margin + vinfo->yres);
290 quot *= (vinfo->left_margin + vinfo->right_margin + vinfo->xres);
291 quot *= vinfo->pixclock;
292
293 if (quot > 0) {
294 uint64_t refresh_rate;
295
296 refresh_rate = 1000000000000000LLU / quot;
297 if (refresh_rate > 200000)
298 refresh_rate = 200000; /* cap at 200 Hz */
299
300 return refresh_rate;
301 }
302
303 return 60 * 1000; /* default to 60 Hz */
304}
305
306static int
307fbdev_query_screen_info(struct fbdev_output *output, int fd,
308 struct fbdev_screeninfo *info)
309{
310 struct fb_var_screeninfo varinfo;
311 struct fb_fix_screeninfo fixinfo;
312
313 /* Probe the device for screen information. */
314 if (ioctl(fd, FBIOGET_FSCREENINFO, &fixinfo) < 0 ||
315 ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
316 return -1;
317 }
318
319 /* Store the pertinent data. */
320 info->x_resolution = varinfo.xres;
321 info->y_resolution = varinfo.yres;
322 info->width_mm = varinfo.width;
323 info->height_mm = varinfo.height;
324 info->bits_per_pixel = varinfo.bits_per_pixel;
325
326 info->buffer_length = fixinfo.smem_len;
327 info->line_length = fixinfo.line_length;
328 strncpy(info->id, fixinfo.id, sizeof(info->id) / sizeof(*info->id));
329
330 info->pixel_format = calculate_pixman_format(&varinfo, &fixinfo);
331 info->refresh_rate = calculate_refresh_rate(&varinfo);
332
333 if (info->pixel_format == 0) {
334 weston_log("Frame buffer uses an unsupported format.\n");
335 return -1;
336 }
337
338 return 1;
339}
340
341static int
342fbdev_set_screen_info(struct fbdev_output *output, int fd,
343 struct fbdev_screeninfo *info)
344{
345 struct fb_var_screeninfo varinfo;
346
347 /* Grab the current screen information. */
348 if (ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
349 return -1;
350 }
351
352 /* Update the information. */
353 varinfo.xres = info->x_resolution;
354 varinfo.yres = info->y_resolution;
355 varinfo.width = info->width_mm;
356 varinfo.height = info->height_mm;
357 varinfo.bits_per_pixel = info->bits_per_pixel;
358
359 /* Try to set up an ARGB (x8r8g8b8) pixel format. */
360 varinfo.grayscale = 0;
361 varinfo.transp.offset = 24;
362 varinfo.transp.length = 0;
363 varinfo.transp.msb_right = 0;
364 varinfo.red.offset = 16;
365 varinfo.red.length = 8;
366 varinfo.red.msb_right = 0;
367 varinfo.green.offset = 8;
368 varinfo.green.length = 8;
369 varinfo.green.msb_right = 0;
370 varinfo.blue.offset = 0;
371 varinfo.blue.length = 8;
372 varinfo.blue.msb_right = 0;
373
374 /* Set the device's screen information. */
375 if (ioctl(fd, FBIOPUT_VSCREENINFO, &varinfo) < 0) {
376 return -1;
377 }
378
379 return 1;
380}
381
382static void fbdev_frame_buffer_destroy(struct fbdev_output *output);
383
384/* Returns an FD for the frame buffer device. */
385static int
386fbdev_frame_buffer_open(struct fbdev_output *output, const char *fb_dev,
387 struct fbdev_screeninfo *screen_info)
388{
389 int fd = -1;
390
391 weston_log("Opening fbdev frame buffer.\n");
392
393 /* Open the frame buffer device. */
394 fd = open(fb_dev, O_RDWR | O_CLOEXEC);
395 if (fd < 0) {
396 weston_log("Failed to open frame buffer device ‘%s’: %s\n",
397 fb_dev, strerror(errno));
398 return -1;
399 }
400
401 /* Grab the screen info. */
402 if (fbdev_query_screen_info(output, fd, screen_info) < 0) {
403 weston_log("Failed to get frame buffer info: %s\n",
404 strerror(errno));
405
406 close(fd);
407 return -1;
408 }
409
410 return fd;
411}
412
413/* Closes the FD on success or failure. */
414static int
415fbdev_frame_buffer_map(struct fbdev_output *output, int fd)
416{
417 int retval = -1;
418
419 weston_log("Mapping fbdev frame buffer.\n");
420
421 /* Map the frame buffer. Write-only mode, since we don't want to read
422 * anything back (because it's slow). */
423 output->fb = mmap(NULL, output->fb_info.buffer_length,
424 PROT_WRITE, MAP_SHARED, fd, 0);
425 if (output->fb == MAP_FAILED) {
426 weston_log("Failed to mmap frame buffer: %s\n",
427 strerror(errno));
428 goto out_close;
429 }
430
431 /* Create a pixman image to wrap the memory mapped frame buffer. */
432 output->hw_surface =
433 pixman_image_create_bits(output->fb_info.pixel_format,
434 output->fb_info.x_resolution,
435 output->fb_info.y_resolution,
436 output->fb,
437 output->fb_info.line_length);
438 if (output->hw_surface == NULL) {
439 weston_log("Failed to create surface for frame buffer.\n");
440 goto out_unmap;
441 }
442
443 /* Success! */
444 retval = 0;
445
446out_unmap:
447 if (retval != 0 && output->fb != NULL)
448 fbdev_frame_buffer_destroy(output);
449
450out_close:
451 if (fd >= 0)
452 close(fd);
453
454 return retval;
455}
456
457static void
458fbdev_frame_buffer_destroy(struct fbdev_output *output)
459{
460 weston_log("Destroying fbdev frame buffer.\n");
461
462 if (munmap(output->fb, output->fb_info.buffer_length) < 0)
463 weston_log("Failed to munmap frame buffer: %s\n",
464 strerror(errno));
465
466 output->fb = NULL;
467}
468
469static void fbdev_output_destroy(struct weston_output *base);
470static void fbdev_output_disable(struct weston_output *base);
471
472static int
473fbdev_output_create(struct fbdev_compositor *compositor,
474 const char *device)
475{
476 struct fbdev_output *output;
477 pixman_transform_t transform;
478 int fb_fd;
479 int shadow_width, shadow_height;
480 int width, height;
481 unsigned int bytes_per_pixel;
482 struct wl_event_loop *loop;
483
484 weston_log("Creating fbdev output.\n");
485
486 output = calloc(1, sizeof *output);
487 if (!output)
488 return -1;
489
490 output->compositor = compositor;
491 output->device = device;
492
493 /* Create the frame buffer. */
494 fb_fd = fbdev_frame_buffer_open(output, device, &output->fb_info);
495 if (fb_fd < 0) {
496 weston_log("Creating frame buffer failed.\n");
497 goto out_free;
498 }
499
500 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
501 weston_log("Mapping frame buffer failed.\n");
502 goto out_free;
503 }
504
Jonas Ådahle5a12252013-04-05 23:07:11 +0200505 output->base.start_repaint_loop = fbdev_output_start_repaint_loop;
Philip Withnall4f499172013-02-02 12:02:32 +0000506 output->base.repaint = fbdev_output_repaint;
507 output->base.destroy = fbdev_output_destroy;
508 output->base.assign_planes = NULL;
509 output->base.set_backlight = NULL;
510 output->base.set_dpms = NULL;
511 output->base.switch_mode = NULL;
512
513 /* only one static mode in list */
514 output->mode.flags =
515 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
516 output->mode.width = output->fb_info.x_resolution;
517 output->mode.height = output->fb_info.y_resolution;
518 output->mode.refresh = output->fb_info.refresh_rate;
519 wl_list_init(&output->base.mode_list);
520 wl_list_insert(&output->base.mode_list, &output->mode.link);
521
522 output->base.current = &output->mode;
523 output->base.origin = &output->mode;
524 output->base.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
525 output->base.make = "unknown";
526 output->base.model = output->fb_info.id;
527
528 weston_output_init(&output->base, &compositor->base,
529 0, 0, output->fb_info.width_mm,
530 output->fb_info.height_mm,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200531 WL_OUTPUT_TRANSFORM_NORMAL,
532 1);
Philip Withnall4f499172013-02-02 12:02:32 +0000533
534 width = output->fb_info.x_resolution;
535 height = output->fb_info.y_resolution;
536
537 pixman_transform_init_identity(&transform);
538 switch (output->base.transform) {
539 default:
540 case WL_OUTPUT_TRANSFORM_NORMAL:
541 shadow_width = width;
542 shadow_height = height;
543 pixman_transform_rotate(&transform,
544 NULL, 0, 0);
545 pixman_transform_translate(&transform, NULL,
546 0, 0);
547 break;
548 case WL_OUTPUT_TRANSFORM_180:
549 shadow_width = width;
550 shadow_height = height;
551 pixman_transform_rotate(&transform,
552 NULL, -pixman_fixed_1, 0);
553 pixman_transform_translate(NULL, &transform,
554 pixman_int_to_fixed(shadow_width),
555 pixman_int_to_fixed(shadow_height));
556 break;
557 case WL_OUTPUT_TRANSFORM_270:
558 shadow_width = height;
559 shadow_height = width;
560 pixman_transform_rotate(&transform,
561 NULL, 0, pixman_fixed_1);
562 pixman_transform_translate(&transform,
563 NULL,
564 pixman_int_to_fixed(shadow_width),
565 0);
566 break;
567 case WL_OUTPUT_TRANSFORM_90:
568 shadow_width = height;
569 shadow_height = width;
570 pixman_transform_rotate(&transform,
571 NULL, 0, -pixman_fixed_1);
572 pixman_transform_translate(&transform,
573 NULL,
574 0,
575 pixman_int_to_fixed(shadow_height));
576 break;
577 }
578
579 bytes_per_pixel = output->fb_info.bits_per_pixel / 8;
580
581 output->shadow_buf = malloc(width * height * bytes_per_pixel);
582 output->shadow_surface =
583 pixman_image_create_bits(output->fb_info.pixel_format,
584 shadow_width, shadow_height,
585 output->shadow_buf,
586 shadow_width * bytes_per_pixel);
587 if (output->shadow_buf == NULL || output->shadow_surface == NULL) {
588 weston_log("Failed to create surface for frame buffer.\n");
589 goto out_hw_surface;
590 }
591
592 /* No need in transform for normal output */
593 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
594 pixman_image_set_transform(output->shadow_surface, &transform);
595
596 if (pixman_renderer_output_create(&output->base) < 0)
597 goto out_shadow_surface;
598
599 loop = wl_display_get_event_loop(compositor->base.wl_display);
600 output->finish_frame_timer =
601 wl_event_loop_add_timer(loop, finish_frame_handler, output);
602
603 wl_list_insert(compositor->base.output_list.prev, &output->base.link);
604
605 weston_log("fbdev output %d×%d px\n",
606 output->mode.width, output->mode.height);
607 weston_log_continue(STAMP_SPACE "guessing %d Hz and 96 dpi\n",
608 output->mode.refresh / 1000);
609
610 return 0;
611
612out_shadow_surface:
613 pixman_image_unref(output->shadow_surface);
614 output->shadow_surface = NULL;
615out_hw_surface:
616 free(output->shadow_buf);
617 pixman_image_unref(output->hw_surface);
618 output->hw_surface = NULL;
619 weston_output_destroy(&output->base);
620 fbdev_frame_buffer_destroy(output);
621out_free:
622 free(output);
623
624 return -1;
625}
626
627static void
628fbdev_output_destroy(struct weston_output *base)
629{
630 struct fbdev_output *output = to_fbdev_output(base);
631
632 weston_log("Destroying fbdev output.\n");
633
634 /* Close the frame buffer. */
635 fbdev_output_disable(base);
636
637 if (base->renderer_state != NULL)
638 pixman_renderer_output_destroy(base);
639
640 if (output->shadow_surface != NULL) {
641 pixman_image_unref(output->shadow_surface);
642 output->shadow_surface = NULL;
643 }
644
645 if (output->shadow_buf != NULL) {
646 free(output->shadow_buf);
647 output->shadow_buf = NULL;
648 }
649
650 /* Remove the output. */
651 wl_list_remove(&output->base.link);
652 weston_output_destroy(&output->base);
653
654 free(output);
655}
656
657/* strcmp()-style return values. */
658static int
659compare_screen_info (const struct fbdev_screeninfo *a,
660 const struct fbdev_screeninfo *b)
661{
662 if (a->x_resolution == b->x_resolution &&
663 a->y_resolution == b->y_resolution &&
664 a->width_mm == b->width_mm &&
665 a->height_mm == b->height_mm &&
666 a->bits_per_pixel == b->bits_per_pixel &&
667 a->pixel_format == b->pixel_format &&
668 a->refresh_rate == b->refresh_rate)
669 return 0;
670
671 return 1;
672}
673
674static int
675fbdev_output_reenable(struct fbdev_compositor *compositor,
676 struct weston_output *base)
677{
678 struct fbdev_output *output = to_fbdev_output(base);
679 struct fbdev_screeninfo new_screen_info;
680 int fb_fd;
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100681 const char *device;
Philip Withnall4f499172013-02-02 12:02:32 +0000682
683 weston_log("Re-enabling fbdev output.\n");
684
685 /* Create the frame buffer. */
686 fb_fd = fbdev_frame_buffer_open(output, output->device,
687 &new_screen_info);
688 if (fb_fd < 0) {
689 weston_log("Creating frame buffer failed.\n");
690 goto err;
691 }
692
693 /* Check whether the frame buffer details have changed since we were
694 * disabled. */
695 if (compare_screen_info (&output->fb_info, &new_screen_info) != 0) {
696 /* Perform a mode-set to restore the old mode. */
697 if (fbdev_set_screen_info(output, fb_fd,
698 &output->fb_info) < 0) {
699 weston_log("Failed to restore mode settings. "
700 "Attempting to re-open output anyway.\n");
701 }
702
Rob Bradford581b3fd2013-07-26 16:29:39 +0100703 close(fb_fd);
704
Philip Withnall4f499172013-02-02 12:02:32 +0000705 /* Remove and re-add the output so that resources depending on
706 * the frame buffer X/Y resolution (such as the shadow buffer)
707 * are re-initialised. */
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100708 device = output->device;
Philip Withnall4f499172013-02-02 12:02:32 +0000709 fbdev_output_destroy(base);
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100710 fbdev_output_create(compositor, device);
Philip Withnall4f499172013-02-02 12:02:32 +0000711
712 return 0;
713 }
714
715 /* Map the device if it has the same details as before. */
716 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
717 weston_log("Mapping frame buffer failed.\n");
718 goto err;
719 }
720
721 return 0;
722
723err:
724 return -1;
725}
726
727/* NOTE: This leaves output->fb_info populated, caching data so that if
728 * fbdev_output_reenable() is called again, it can determine whether a mode-set
729 * is needed. */
730static void
731fbdev_output_disable(struct weston_output *base)
732{
733 struct fbdev_output *output = to_fbdev_output(base);
734
735 weston_log("Disabling fbdev output.\n");
736
737 if (output->hw_surface != NULL) {
738 pixman_image_unref(output->hw_surface);
739 output->hw_surface = NULL;
740 }
741
742 fbdev_frame_buffer_destroy(output);
743}
744
745static void
Philip Withnall4f499172013-02-02 12:02:32 +0000746fbdev_compositor_destroy(struct weston_compositor *base)
747{
748 struct fbdev_compositor *compositor = to_fbdev_compositor(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000749
Rob Bradfordd355b802013-05-31 18:09:55 +0100750 udev_input_destroy(&compositor->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000751
752 /* Destroy the output. */
753 weston_compositor_shutdown(&compositor->base);
754
755 /* Chain up. */
756 compositor->base.renderer->destroy(&compositor->base);
757 tty_destroy(compositor->tty);
758
759 free(compositor);
760}
761
762static void
763vt_func(struct weston_compositor *base, int event)
764{
765 struct fbdev_compositor *compositor = to_fbdev_compositor(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000766 struct weston_output *output;
767
768 switch (event) {
769 case TTY_ENTER_VT:
770 weston_log("entering VT\n");
771 compositor->base.focus = 1;
772 compositor->base.state = compositor->prev_state;
773
774 wl_list_for_each(output, &compositor->base.output_list, link) {
775 fbdev_output_reenable(compositor, output);
776 }
777
778 weston_compositor_damage_all(&compositor->base);
779
Rob Bradfordd355b802013-05-31 18:09:55 +0100780 udev_input_enable(&compositor->input, compositor->udev);
Philip Withnall4f499172013-02-02 12:02:32 +0000781 break;
782 case TTY_LEAVE_VT:
783 weston_log("leaving VT\n");
Rob Bradfordd355b802013-05-31 18:09:55 +0100784 udev_input_disable(&compositor->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000785
786 wl_list_for_each(output, &compositor->base.output_list, link) {
787 fbdev_output_disable(output);
788 }
789
790 compositor->base.focus = 0;
791 compositor->prev_state = compositor->base.state;
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100792 weston_compositor_offscreen(&compositor->base);
Philip Withnall4f499172013-02-02 12:02:32 +0000793
794 /* If we have a repaint scheduled (from the idle handler), make
795 * sure we cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100796 * vt switched away. The OFFSCREEN state will prevent
Philip Withnall4f499172013-02-02 12:02:32 +0000797 * further attemps at repainting. When we switch
798 * back, we schedule a repaint, which will process
799 * pending frame callbacks. */
800
801 wl_list_for_each(output,
802 &compositor->base.output_list, link) {
803 output->repaint_needed = 0;
804 }
805
806 break;
807 };
808}
809
810static void
811fbdev_restore(struct weston_compositor *base)
812{
813 struct fbdev_compositor *compositor = to_fbdev_compositor(base);
814
815 tty_reset(compositor->tty);
816}
817
818static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400819switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Philip Withnall4f499172013-02-02 12:02:32 +0000820{
821 struct fbdev_compositor *ec = data;
822
823 tty_activate_vt(ec->tty, key - KEY_F1 + 1);
824}
825
826static struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500827fbdev_compositor_create(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400828 struct weston_config *config,
829 struct fbdev_parameters *param)
Philip Withnall4f499172013-02-02 12:02:32 +0000830{
831 struct fbdev_compositor *compositor;
Rob Bradford2387fde2013-05-31 18:09:57 +0100832 const char *seat_id = default_seat;
Philip Withnall4f499172013-02-02 12:02:32 +0000833 uint32_t key;
834
835 weston_log("initializing fbdev backend\n");
836
837 compositor = calloc(1, sizeof *compositor);
838 if (compositor == NULL)
839 return NULL;
840
841 if (weston_compositor_init(&compositor->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400842 config) < 0)
Philip Withnall4f499172013-02-02 12:02:32 +0000843 goto out_free;
844
Adrian Negreanuc8384232013-07-28 18:27:23 +0300845 /* Check if we run fbdev-backend using weston-launch */
846 compositor->base.launcher_sock =
847 weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
848 if (compositor->base.launcher_sock == -1 && geteuid() != 0) {
849 weston_log("fatal: fbdev backend should be run "
850 "using weston-launch binary or as root\n");
851 goto out_compositor;
852 }
853
Philip Withnall4f499172013-02-02 12:02:32 +0000854 compositor->udev = udev_new();
855 if (compositor->udev == NULL) {
856 weston_log("Failed to initialize udev context.\n");
857 goto out_compositor;
858 }
859
860 /* Set up the TTY. */
861 compositor->tty = tty_create(&compositor->base, vt_func, param->tty);
862 if (!compositor->tty) {
863 weston_log("Failed to initialize tty.\n");
864 goto out_udev;
865 }
866
867 compositor->base.destroy = fbdev_compositor_destroy;
868 compositor->base.restore = fbdev_restore;
869
870 compositor->base.focus = 1;
871 compositor->prev_state = WESTON_COMPOSITOR_ACTIVE;
872
873 for (key = KEY_F1; key < KEY_F9; key++)
874 weston_compositor_add_key_binding(&compositor->base, key,
875 MODIFIER_CTRL | MODIFIER_ALT,
876 switch_vt_binding,
877 compositor);
878
879 if (pixman_renderer_init(&compositor->base) < 0)
880 goto out_tty;
881
882 if (fbdev_output_create(compositor, param->device) < 0)
883 goto out_pixman;
884
Rob Bradford2387fde2013-05-31 18:09:57 +0100885 udev_input_init(&compositor->input, &compositor->base, compositor->udev, seat_id);
Philip Withnall4f499172013-02-02 12:02:32 +0000886
887 return &compositor->base;
888
889out_pixman:
890 compositor->base.renderer->destroy(&compositor->base);
891
892out_tty:
893 tty_destroy(compositor->tty);
894
895out_udev:
896 udev_unref(compositor->udev);
897
898out_compositor:
899 weston_compositor_shutdown(&compositor->base);
900
901out_free:
902 free(compositor);
903
904 return NULL;
905}
906
907WL_EXPORT struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500908backend_init(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400909 struct weston_config *config)
Philip Withnall4f499172013-02-02 12:02:32 +0000910{
911 /* TODO: Ideally, available frame buffers should be enumerated using
912 * udev, rather than passing a device node in as a parameter. */
913 struct fbdev_parameters param = {
914 .tty = 0, /* default to current tty */
915 .device = "/dev/fb0", /* default frame buffer */
916 };
917
918 const struct weston_option fbdev_options[] = {
919 { WESTON_OPTION_INTEGER, "tty", 0, &param.tty },
920 { WESTON_OPTION_STRING, "device", 0, &param.device },
921 };
922
923 parse_options(fbdev_options, ARRAY_LENGTH(fbdev_options), argc, argv);
924
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400925 return fbdev_compositor_create(display, argc, argv, config, &param);
Philip Withnall4f499172013-02-02 12:02:32 +0000926}