blob: 352969ff285bc7f384c802a1f2276034daca0ecb [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
26#define _GNU_SOURCE
27
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
42#include "config.h"
43
44#include "compositor.h"
45#include "launcher-util.h"
46#include "pixman-renderer.h"
Kristian Høgsberg7e597f22013-02-18 16:35:26 -050047#include "udev-seat.h"
Philip Withnall4f499172013-02-02 12:02:32 +000048
49struct fbdev_compositor {
50 struct weston_compositor base;
51 uint32_t prev_state;
52
53 struct udev *udev;
54 struct tty *tty;
Rob Bradfordd355b802013-05-31 18:09:55 +010055 struct udev_input input;
Philip Withnall4f499172013-02-02 12:02:32 +000056};
57
58struct fbdev_screeninfo {
59 unsigned int x_resolution; /* pixels, visible area */
60 unsigned int y_resolution; /* pixels, visible area */
61 unsigned int width_mm; /* visible screen width in mm */
62 unsigned int height_mm; /* visible screen height in mm */
63 unsigned int bits_per_pixel;
64
65 size_t buffer_length; /* length of frame buffer memory in bytes */
66 size_t line_length; /* length of a line in bytes */
67 char id[16]; /* screen identifier */
68
69 pixman_format_code_t pixel_format; /* frame buffer pixel format */
70 unsigned int refresh_rate; /* Hertz */
71};
72
73struct fbdev_output {
74 struct fbdev_compositor *compositor;
75 struct weston_output base;
76
77 struct weston_mode mode;
78 struct wl_event_source *finish_frame_timer;
79
80 /* Frame buffer details. */
81 const char *device; /* ownership shared with fbdev_parameters */
82 struct fbdev_screeninfo fb_info;
83 void *fb; /* length is fb_info.buffer_length */
84
85 /* pixman details. */
86 pixman_image_t *hw_surface;
87 pixman_image_t *shadow_surface;
88 void *shadow_buf;
89 uint8_t depth;
90};
91
Philip Withnall4f499172013-02-02 12:02:32 +000092struct fbdev_parameters {
93 int tty;
94 char *device;
95};
96
Kristian Høgsberg7e597f22013-02-18 16:35:26 -050097static const char default_seat[] = "seat0";
98
Philip Withnall4f499172013-02-02 12:02:32 +000099static inline struct fbdev_output *
100to_fbdev_output(struct weston_output *base)
101{
102 return container_of(base, struct fbdev_output, base);
103}
104
Philip Withnall4f499172013-02-02 12:02:32 +0000105static inline struct fbdev_compositor *
106to_fbdev_compositor(struct weston_compositor *base)
107{
108 return container_of(base, struct fbdev_compositor, base);
109}
110
111static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200112fbdev_output_start_repaint_loop(struct weston_output *output)
113{
114 uint32_t msec;
115 struct timeval tv;
116
117 gettimeofday(&tv, NULL);
118 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
119 weston_output_finish_frame(output, msec);
120}
121
122static void
Philip Withnall4f499172013-02-02 12:02:32 +0000123fbdev_output_repaint(struct weston_output *base, pixman_region32_t *damage)
124{
125 struct fbdev_output *output = to_fbdev_output(base);
126 struct weston_compositor *ec = output->base.compositor;
127 pixman_box32_t *rects;
128 int nrects, i, src_x, src_y, x1, y1, x2, y2, width, height;
129
130 /* Repaint the damaged region onto the back buffer. */
131 pixman_renderer_output_set_buffer(base, output->shadow_surface);
132 ec->renderer->repaint_output(base, damage);
133
134 /* Transform and composite onto the frame buffer. */
135 width = pixman_image_get_width(output->shadow_surface);
136 height = pixman_image_get_height(output->shadow_surface);
137 rects = pixman_region32_rectangles(damage, &nrects);
138
139 for (i = 0; i < nrects; i++) {
140 switch (base->transform) {
141 default:
142 case WL_OUTPUT_TRANSFORM_NORMAL:
143 x1 = rects[i].x1;
144 x2 = rects[i].x2;
145 y1 = rects[i].y1;
146 y2 = rects[i].y2;
147 break;
148 case WL_OUTPUT_TRANSFORM_180:
149 x1 = width - rects[i].x2;
150 x2 = width - rects[i].x1;
151 y1 = height - rects[i].y2;
152 y2 = height - rects[i].y1;
153 break;
154 case WL_OUTPUT_TRANSFORM_90:
155 x1 = height - rects[i].y2;
156 x2 = height - rects[i].y1;
157 y1 = rects[i].x1;
158 y2 = rects[i].x2;
159 break;
160 case WL_OUTPUT_TRANSFORM_270:
161 x1 = rects[i].y1;
162 x2 = rects[i].y2;
163 y1 = width - rects[i].x2;
164 y2 = width - rects[i].x1;
165 break;
166 }
167 src_x = x1;
168 src_y = y1;
169
170 pixman_image_composite32(PIXMAN_OP_SRC,
171 output->shadow_surface, /* src */
172 NULL /* mask */,
173 output->hw_surface, /* dest */
174 src_x, src_y, /* src_x, src_y */
175 0, 0, /* mask_x, mask_y */
176 x1, y1, /* dest_x, dest_y */
177 x2 - x1, /* width */
178 y2 - y1 /* height */);
179 }
180
181 /* Update the damage region. */
182 pixman_region32_subtract(&ec->primary_plane.damage,
183 &ec->primary_plane.damage, damage);
184
185 /* Schedule the end of the frame. We do not sync this to the frame
186 * buffer clock because users who want that should be using the DRM
187 * compositor. FBIO_WAITFORVSYNC blocks and FB_ACTIVATE_VBL requires
188 * panning, which is broken in most kernel drivers.
189 *
190 * Finish the frame synchronised to the specified refresh rate. The
191 * refresh rate is given in mHz and the interval in ms. */
192 wl_event_source_timer_update(output->finish_frame_timer,
193 1000000 / output->mode.refresh);
194}
195
196static int
197finish_frame_handler(void *data)
198{
199 struct fbdev_output *output = data;
Philip Withnall4f499172013-02-02 12:02:32 +0000200
Jonas Ådahle5a12252013-04-05 23:07:11 +0200201 fbdev_output_start_repaint_loop(&output->base);
Philip Withnall4f499172013-02-02 12:02:32 +0000202
203 return 1;
204}
205
206static pixman_format_code_t
207calculate_pixman_format(struct fb_var_screeninfo *vinfo,
208 struct fb_fix_screeninfo *finfo)
209{
210 /* Calculate the pixman format supported by the frame buffer from the
211 * buffer's metadata. Return 0 if no known pixman format is supported
212 * (since this has depth 0 it's guaranteed to not conflict with any
213 * actual pixman format).
214 *
215 * Documentation on the vinfo and finfo structures:
216 * http://www.mjmwired.net/kernel/Documentation/fb/api.txt
217 *
218 * TODO: Try a bit harder to support other formats, including setting
219 * the preferred format in the hardware. */
220 int type;
221
222 weston_log("Calculating pixman format from:\n"
223 STAMP_SPACE " - type: %i (aux: %i)\n"
224 STAMP_SPACE " - visual: %i\n"
225 STAMP_SPACE " - bpp: %i (grayscale: %i)\n"
226 STAMP_SPACE " - red: offset: %i, length: %i, MSB: %i\n"
227 STAMP_SPACE " - green: offset: %i, length: %i, MSB: %i\n"
228 STAMP_SPACE " - blue: offset: %i, length: %i, MSB: %i\n"
229 STAMP_SPACE " - transp: offset: %i, length: %i, MSB: %i\n",
230 finfo->type, finfo->type_aux, finfo->visual,
231 vinfo->bits_per_pixel, vinfo->grayscale,
232 vinfo->red.offset, vinfo->red.length, vinfo->red.msb_right,
233 vinfo->green.offset, vinfo->green.length,
234 vinfo->green.msb_right,
235 vinfo->blue.offset, vinfo->blue.length,
236 vinfo->blue.msb_right,
237 vinfo->transp.offset, vinfo->transp.length,
238 vinfo->transp.msb_right);
239
240 /* We only handle packed formats at the moment. */
241 if (finfo->type != FB_TYPE_PACKED_PIXELS)
242 return 0;
243
244 /* We only handle true-colour frame buffers at the moment. */
245 if (finfo->visual != FB_VISUAL_TRUECOLOR || vinfo->grayscale != 0)
246 return 0;
247
248 /* We only support formats with MSBs on the left. */
249 if (vinfo->red.msb_right != 0 || vinfo->green.msb_right != 0 ||
250 vinfo->blue.msb_right != 0)
251 return 0;
252
253 /* Work out the format type from the offsets. We only support RGBA and
254 * ARGB at the moment. */
255 type = PIXMAN_TYPE_OTHER;
256
257 if ((vinfo->transp.offset >= vinfo->red.offset ||
258 vinfo->transp.length == 0) &&
259 vinfo->red.offset >= vinfo->green.offset &&
260 vinfo->green.offset >= vinfo->blue.offset)
261 type = PIXMAN_TYPE_ARGB;
262 else if (vinfo->red.offset >= vinfo->green.offset &&
263 vinfo->green.offset >= vinfo->blue.offset &&
264 vinfo->blue.offset >= vinfo->transp.offset)
265 type = PIXMAN_TYPE_RGBA;
266
267 if (type == PIXMAN_TYPE_OTHER)
268 return 0;
269
270 /* Build the format. */
271 return PIXMAN_FORMAT(vinfo->bits_per_pixel, type,
272 vinfo->transp.length,
273 vinfo->red.length,
274 vinfo->green.length,
275 vinfo->blue.length);
276}
277
278static int
279calculate_refresh_rate(struct fb_var_screeninfo *vinfo)
280{
281 uint64_t quot;
282
283 /* Calculate monitor refresh rate. Default is 60 Hz. Units are mHz. */
284 quot = (vinfo->upper_margin + vinfo->lower_margin + vinfo->yres);
285 quot *= (vinfo->left_margin + vinfo->right_margin + vinfo->xres);
286 quot *= vinfo->pixclock;
287
288 if (quot > 0) {
289 uint64_t refresh_rate;
290
291 refresh_rate = 1000000000000000LLU / quot;
292 if (refresh_rate > 200000)
293 refresh_rate = 200000; /* cap at 200 Hz */
294
295 return refresh_rate;
296 }
297
298 return 60 * 1000; /* default to 60 Hz */
299}
300
301static int
302fbdev_query_screen_info(struct fbdev_output *output, int fd,
303 struct fbdev_screeninfo *info)
304{
305 struct fb_var_screeninfo varinfo;
306 struct fb_fix_screeninfo fixinfo;
307
308 /* Probe the device for screen information. */
309 if (ioctl(fd, FBIOGET_FSCREENINFO, &fixinfo) < 0 ||
310 ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
311 return -1;
312 }
313
314 /* Store the pertinent data. */
315 info->x_resolution = varinfo.xres;
316 info->y_resolution = varinfo.yres;
317 info->width_mm = varinfo.width;
318 info->height_mm = varinfo.height;
319 info->bits_per_pixel = varinfo.bits_per_pixel;
320
321 info->buffer_length = fixinfo.smem_len;
322 info->line_length = fixinfo.line_length;
323 strncpy(info->id, fixinfo.id, sizeof(info->id) / sizeof(*info->id));
324
325 info->pixel_format = calculate_pixman_format(&varinfo, &fixinfo);
326 info->refresh_rate = calculate_refresh_rate(&varinfo);
327
328 if (info->pixel_format == 0) {
329 weston_log("Frame buffer uses an unsupported format.\n");
330 return -1;
331 }
332
333 return 1;
334}
335
336static int
337fbdev_set_screen_info(struct fbdev_output *output, int fd,
338 struct fbdev_screeninfo *info)
339{
340 struct fb_var_screeninfo varinfo;
341
342 /* Grab the current screen information. */
343 if (ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
344 return -1;
345 }
346
347 /* Update the information. */
348 varinfo.xres = info->x_resolution;
349 varinfo.yres = info->y_resolution;
350 varinfo.width = info->width_mm;
351 varinfo.height = info->height_mm;
352 varinfo.bits_per_pixel = info->bits_per_pixel;
353
354 /* Try to set up an ARGB (x8r8g8b8) pixel format. */
355 varinfo.grayscale = 0;
356 varinfo.transp.offset = 24;
357 varinfo.transp.length = 0;
358 varinfo.transp.msb_right = 0;
359 varinfo.red.offset = 16;
360 varinfo.red.length = 8;
361 varinfo.red.msb_right = 0;
362 varinfo.green.offset = 8;
363 varinfo.green.length = 8;
364 varinfo.green.msb_right = 0;
365 varinfo.blue.offset = 0;
366 varinfo.blue.length = 8;
367 varinfo.blue.msb_right = 0;
368
369 /* Set the device's screen information. */
370 if (ioctl(fd, FBIOPUT_VSCREENINFO, &varinfo) < 0) {
371 return -1;
372 }
373
374 return 1;
375}
376
377static void fbdev_frame_buffer_destroy(struct fbdev_output *output);
378
379/* Returns an FD for the frame buffer device. */
380static int
381fbdev_frame_buffer_open(struct fbdev_output *output, const char *fb_dev,
382 struct fbdev_screeninfo *screen_info)
383{
384 int fd = -1;
385
386 weston_log("Opening fbdev frame buffer.\n");
387
388 /* Open the frame buffer device. */
389 fd = open(fb_dev, O_RDWR | O_CLOEXEC);
390 if (fd < 0) {
391 weston_log("Failed to open frame buffer device ‘%s’: %s\n",
392 fb_dev, strerror(errno));
393 return -1;
394 }
395
396 /* Grab the screen info. */
397 if (fbdev_query_screen_info(output, fd, screen_info) < 0) {
398 weston_log("Failed to get frame buffer info: %s\n",
399 strerror(errno));
400
401 close(fd);
402 return -1;
403 }
404
405 return fd;
406}
407
408/* Closes the FD on success or failure. */
409static int
410fbdev_frame_buffer_map(struct fbdev_output *output, int fd)
411{
412 int retval = -1;
413
414 weston_log("Mapping fbdev frame buffer.\n");
415
416 /* Map the frame buffer. Write-only mode, since we don't want to read
417 * anything back (because it's slow). */
418 output->fb = mmap(NULL, output->fb_info.buffer_length,
419 PROT_WRITE, MAP_SHARED, fd, 0);
420 if (output->fb == MAP_FAILED) {
421 weston_log("Failed to mmap frame buffer: %s\n",
422 strerror(errno));
423 goto out_close;
424 }
425
426 /* Create a pixman image to wrap the memory mapped frame buffer. */
427 output->hw_surface =
428 pixman_image_create_bits(output->fb_info.pixel_format,
429 output->fb_info.x_resolution,
430 output->fb_info.y_resolution,
431 output->fb,
432 output->fb_info.line_length);
433 if (output->hw_surface == NULL) {
434 weston_log("Failed to create surface for frame buffer.\n");
435 goto out_unmap;
436 }
437
438 /* Success! */
439 retval = 0;
440
441out_unmap:
442 if (retval != 0 && output->fb != NULL)
443 fbdev_frame_buffer_destroy(output);
444
445out_close:
446 if (fd >= 0)
447 close(fd);
448
449 return retval;
450}
451
452static void
453fbdev_frame_buffer_destroy(struct fbdev_output *output)
454{
455 weston_log("Destroying fbdev frame buffer.\n");
456
457 if (munmap(output->fb, output->fb_info.buffer_length) < 0)
458 weston_log("Failed to munmap frame buffer: %s\n",
459 strerror(errno));
460
461 output->fb = NULL;
462}
463
464static void fbdev_output_destroy(struct weston_output *base);
465static void fbdev_output_disable(struct weston_output *base);
466
467static int
468fbdev_output_create(struct fbdev_compositor *compositor,
469 const char *device)
470{
471 struct fbdev_output *output;
472 pixman_transform_t transform;
473 int fb_fd;
474 int shadow_width, shadow_height;
475 int width, height;
476 unsigned int bytes_per_pixel;
477 struct wl_event_loop *loop;
478
479 weston_log("Creating fbdev output.\n");
480
481 output = calloc(1, sizeof *output);
482 if (!output)
483 return -1;
484
485 output->compositor = compositor;
486 output->device = device;
487
488 /* Create the frame buffer. */
489 fb_fd = fbdev_frame_buffer_open(output, device, &output->fb_info);
490 if (fb_fd < 0) {
491 weston_log("Creating frame buffer failed.\n");
492 goto out_free;
493 }
494
495 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
496 weston_log("Mapping frame buffer failed.\n");
497 goto out_free;
498 }
499
Jonas Ådahle5a12252013-04-05 23:07:11 +0200500 output->base.start_repaint_loop = fbdev_output_start_repaint_loop;
Philip Withnall4f499172013-02-02 12:02:32 +0000501 output->base.repaint = fbdev_output_repaint;
502 output->base.destroy = fbdev_output_destroy;
503 output->base.assign_planes = NULL;
504 output->base.set_backlight = NULL;
505 output->base.set_dpms = NULL;
506 output->base.switch_mode = NULL;
507
508 /* only one static mode in list */
509 output->mode.flags =
510 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
511 output->mode.width = output->fb_info.x_resolution;
512 output->mode.height = output->fb_info.y_resolution;
513 output->mode.refresh = output->fb_info.refresh_rate;
514 wl_list_init(&output->base.mode_list);
515 wl_list_insert(&output->base.mode_list, &output->mode.link);
516
517 output->base.current = &output->mode;
518 output->base.origin = &output->mode;
519 output->base.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
520 output->base.make = "unknown";
521 output->base.model = output->fb_info.id;
522
523 weston_output_init(&output->base, &compositor->base,
524 0, 0, output->fb_info.width_mm,
525 output->fb_info.height_mm,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200526 WL_OUTPUT_TRANSFORM_NORMAL,
527 1);
Philip Withnall4f499172013-02-02 12:02:32 +0000528
529 width = output->fb_info.x_resolution;
530 height = output->fb_info.y_resolution;
531
532 pixman_transform_init_identity(&transform);
533 switch (output->base.transform) {
534 default:
535 case WL_OUTPUT_TRANSFORM_NORMAL:
536 shadow_width = width;
537 shadow_height = height;
538 pixman_transform_rotate(&transform,
539 NULL, 0, 0);
540 pixman_transform_translate(&transform, NULL,
541 0, 0);
542 break;
543 case WL_OUTPUT_TRANSFORM_180:
544 shadow_width = width;
545 shadow_height = height;
546 pixman_transform_rotate(&transform,
547 NULL, -pixman_fixed_1, 0);
548 pixman_transform_translate(NULL, &transform,
549 pixman_int_to_fixed(shadow_width),
550 pixman_int_to_fixed(shadow_height));
551 break;
552 case WL_OUTPUT_TRANSFORM_270:
553 shadow_width = height;
554 shadow_height = width;
555 pixman_transform_rotate(&transform,
556 NULL, 0, pixman_fixed_1);
557 pixman_transform_translate(&transform,
558 NULL,
559 pixman_int_to_fixed(shadow_width),
560 0);
561 break;
562 case WL_OUTPUT_TRANSFORM_90:
563 shadow_width = height;
564 shadow_height = width;
565 pixman_transform_rotate(&transform,
566 NULL, 0, -pixman_fixed_1);
567 pixman_transform_translate(&transform,
568 NULL,
569 0,
570 pixman_int_to_fixed(shadow_height));
571 break;
572 }
573
574 bytes_per_pixel = output->fb_info.bits_per_pixel / 8;
575
576 output->shadow_buf = malloc(width * height * bytes_per_pixel);
577 output->shadow_surface =
578 pixman_image_create_bits(output->fb_info.pixel_format,
579 shadow_width, shadow_height,
580 output->shadow_buf,
581 shadow_width * bytes_per_pixel);
582 if (output->shadow_buf == NULL || output->shadow_surface == NULL) {
583 weston_log("Failed to create surface for frame buffer.\n");
584 goto out_hw_surface;
585 }
586
587 /* No need in transform for normal output */
588 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
589 pixman_image_set_transform(output->shadow_surface, &transform);
590
591 if (pixman_renderer_output_create(&output->base) < 0)
592 goto out_shadow_surface;
593
594 loop = wl_display_get_event_loop(compositor->base.wl_display);
595 output->finish_frame_timer =
596 wl_event_loop_add_timer(loop, finish_frame_handler, output);
597
598 wl_list_insert(compositor->base.output_list.prev, &output->base.link);
599
600 weston_log("fbdev output %d×%d px\n",
601 output->mode.width, output->mode.height);
602 weston_log_continue(STAMP_SPACE "guessing %d Hz and 96 dpi\n",
603 output->mode.refresh / 1000);
604
605 return 0;
606
607out_shadow_surface:
608 pixman_image_unref(output->shadow_surface);
609 output->shadow_surface = NULL;
610out_hw_surface:
611 free(output->shadow_buf);
612 pixman_image_unref(output->hw_surface);
613 output->hw_surface = NULL;
614 weston_output_destroy(&output->base);
615 fbdev_frame_buffer_destroy(output);
616out_free:
617 free(output);
618
619 return -1;
620}
621
622static void
623fbdev_output_destroy(struct weston_output *base)
624{
625 struct fbdev_output *output = to_fbdev_output(base);
626
627 weston_log("Destroying fbdev output.\n");
628
629 /* Close the frame buffer. */
630 fbdev_output_disable(base);
631
632 if (base->renderer_state != NULL)
633 pixman_renderer_output_destroy(base);
634
635 if (output->shadow_surface != NULL) {
636 pixman_image_unref(output->shadow_surface);
637 output->shadow_surface = NULL;
638 }
639
640 if (output->shadow_buf != NULL) {
641 free(output->shadow_buf);
642 output->shadow_buf = NULL;
643 }
644
645 /* Remove the output. */
646 wl_list_remove(&output->base.link);
647 weston_output_destroy(&output->base);
648
649 free(output);
650}
651
652/* strcmp()-style return values. */
653static int
654compare_screen_info (const struct fbdev_screeninfo *a,
655 const struct fbdev_screeninfo *b)
656{
657 if (a->x_resolution == b->x_resolution &&
658 a->y_resolution == b->y_resolution &&
659 a->width_mm == b->width_mm &&
660 a->height_mm == b->height_mm &&
661 a->bits_per_pixel == b->bits_per_pixel &&
662 a->pixel_format == b->pixel_format &&
663 a->refresh_rate == b->refresh_rate)
664 return 0;
665
666 return 1;
667}
668
669static int
670fbdev_output_reenable(struct fbdev_compositor *compositor,
671 struct weston_output *base)
672{
673 struct fbdev_output *output = to_fbdev_output(base);
674 struct fbdev_screeninfo new_screen_info;
675 int fb_fd;
676
677 weston_log("Re-enabling fbdev output.\n");
678
679 /* Create the frame buffer. */
680 fb_fd = fbdev_frame_buffer_open(output, output->device,
681 &new_screen_info);
682 if (fb_fd < 0) {
683 weston_log("Creating frame buffer failed.\n");
684 goto err;
685 }
686
687 /* Check whether the frame buffer details have changed since we were
688 * disabled. */
689 if (compare_screen_info (&output->fb_info, &new_screen_info) != 0) {
690 /* Perform a mode-set to restore the old mode. */
691 if (fbdev_set_screen_info(output, fb_fd,
692 &output->fb_info) < 0) {
693 weston_log("Failed to restore mode settings. "
694 "Attempting to re-open output anyway.\n");
695 }
696
697 /* Remove and re-add the output so that resources depending on
698 * the frame buffer X/Y resolution (such as the shadow buffer)
699 * are re-initialised. */
700 fbdev_output_destroy(base);
701 fbdev_output_create(compositor, output->device);
702
703 return 0;
704 }
705
706 /* Map the device if it has the same details as before. */
707 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
708 weston_log("Mapping frame buffer failed.\n");
709 goto err;
710 }
711
712 return 0;
713
714err:
715 return -1;
716}
717
718/* NOTE: This leaves output->fb_info populated, caching data so that if
719 * fbdev_output_reenable() is called again, it can determine whether a mode-set
720 * is needed. */
721static void
722fbdev_output_disable(struct weston_output *base)
723{
724 struct fbdev_output *output = to_fbdev_output(base);
725
726 weston_log("Disabling fbdev output.\n");
727
728 if (output->hw_surface != NULL) {
729 pixman_image_unref(output->hw_surface);
730 output->hw_surface = NULL;
731 }
732
733 fbdev_frame_buffer_destroy(output);
734}
735
736static void
Philip Withnall4f499172013-02-02 12:02:32 +0000737fbdev_compositor_destroy(struct weston_compositor *base)
738{
739 struct fbdev_compositor *compositor = to_fbdev_compositor(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000740
Rob Bradfordd355b802013-05-31 18:09:55 +0100741 udev_input_destroy(&compositor->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000742
743 /* Destroy the output. */
744 weston_compositor_shutdown(&compositor->base);
745
746 /* Chain up. */
747 compositor->base.renderer->destroy(&compositor->base);
748 tty_destroy(compositor->tty);
749
750 free(compositor);
751}
752
753static void
754vt_func(struct weston_compositor *base, int event)
755{
756 struct fbdev_compositor *compositor = to_fbdev_compositor(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000757 struct weston_output *output;
758
759 switch (event) {
760 case TTY_ENTER_VT:
761 weston_log("entering VT\n");
762 compositor->base.focus = 1;
763 compositor->base.state = compositor->prev_state;
764
765 wl_list_for_each(output, &compositor->base.output_list, link) {
766 fbdev_output_reenable(compositor, output);
767 }
768
769 weston_compositor_damage_all(&compositor->base);
770
Rob Bradfordd355b802013-05-31 18:09:55 +0100771 udev_input_enable(&compositor->input, compositor->udev);
Philip Withnall4f499172013-02-02 12:02:32 +0000772 break;
773 case TTY_LEAVE_VT:
774 weston_log("leaving VT\n");
Rob Bradfordd355b802013-05-31 18:09:55 +0100775 udev_input_disable(&compositor->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000776
777 wl_list_for_each(output, &compositor->base.output_list, link) {
778 fbdev_output_disable(output);
779 }
780
781 compositor->base.focus = 0;
782 compositor->prev_state = compositor->base.state;
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100783 weston_compositor_offscreen(&compositor->base);
Philip Withnall4f499172013-02-02 12:02:32 +0000784
785 /* If we have a repaint scheduled (from the idle handler), make
786 * sure we cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100787 * vt switched away. The OFFSCREEN state will prevent
Philip Withnall4f499172013-02-02 12:02:32 +0000788 * further attemps at repainting. When we switch
789 * back, we schedule a repaint, which will process
790 * pending frame callbacks. */
791
792 wl_list_for_each(output,
793 &compositor->base.output_list, link) {
794 output->repaint_needed = 0;
795 }
796
797 break;
798 };
799}
800
801static void
802fbdev_restore(struct weston_compositor *base)
803{
804 struct fbdev_compositor *compositor = to_fbdev_compositor(base);
805
806 tty_reset(compositor->tty);
807}
808
809static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400810switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Philip Withnall4f499172013-02-02 12:02:32 +0000811{
812 struct fbdev_compositor *ec = data;
813
814 tty_activate_vt(ec->tty, key - KEY_F1 + 1);
815}
816
817static struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500818fbdev_compositor_create(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400819 struct weston_config *config,
820 struct fbdev_parameters *param)
Philip Withnall4f499172013-02-02 12:02:32 +0000821{
822 struct fbdev_compositor *compositor;
Rob Bradford2387fde2013-05-31 18:09:57 +0100823 const char *seat_id = default_seat;
Philip Withnall4f499172013-02-02 12:02:32 +0000824 uint32_t key;
825
826 weston_log("initializing fbdev backend\n");
827
828 compositor = calloc(1, sizeof *compositor);
829 if (compositor == NULL)
830 return NULL;
831
832 if (weston_compositor_init(&compositor->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400833 config) < 0)
Philip Withnall4f499172013-02-02 12:02:32 +0000834 goto out_free;
835
836 compositor->udev = udev_new();
837 if (compositor->udev == NULL) {
838 weston_log("Failed to initialize udev context.\n");
839 goto out_compositor;
840 }
841
842 /* Set up the TTY. */
843 compositor->tty = tty_create(&compositor->base, vt_func, param->tty);
844 if (!compositor->tty) {
845 weston_log("Failed to initialize tty.\n");
846 goto out_udev;
847 }
848
849 compositor->base.destroy = fbdev_compositor_destroy;
850 compositor->base.restore = fbdev_restore;
851
852 compositor->base.focus = 1;
853 compositor->prev_state = WESTON_COMPOSITOR_ACTIVE;
854
855 for (key = KEY_F1; key < KEY_F9; key++)
856 weston_compositor_add_key_binding(&compositor->base, key,
857 MODIFIER_CTRL | MODIFIER_ALT,
858 switch_vt_binding,
859 compositor);
860
861 if (pixman_renderer_init(&compositor->base) < 0)
862 goto out_tty;
863
864 if (fbdev_output_create(compositor, param->device) < 0)
865 goto out_pixman;
866
Rob Bradford2387fde2013-05-31 18:09:57 +0100867 udev_input_init(&compositor->input, &compositor->base, compositor->udev, seat_id);
Philip Withnall4f499172013-02-02 12:02:32 +0000868
869 return &compositor->base;
870
871out_pixman:
872 compositor->base.renderer->destroy(&compositor->base);
873
874out_tty:
875 tty_destroy(compositor->tty);
876
877out_udev:
878 udev_unref(compositor->udev);
879
880out_compositor:
881 weston_compositor_shutdown(&compositor->base);
882
883out_free:
884 free(compositor);
885
886 return NULL;
887}
888
889WL_EXPORT struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500890backend_init(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400891 struct weston_config *config)
Philip Withnall4f499172013-02-02 12:02:32 +0000892{
893 /* TODO: Ideally, available frame buffers should be enumerated using
894 * udev, rather than passing a device node in as a parameter. */
895 struct fbdev_parameters param = {
896 .tty = 0, /* default to current tty */
897 .device = "/dev/fb0", /* default frame buffer */
898 };
899
900 const struct weston_option fbdev_options[] = {
901 { WESTON_OPTION_INTEGER, "tty", 0, &param.tty },
902 { WESTON_OPTION_STRING, "device", 0, &param.device },
903 };
904
905 parse_options(fbdev_options, ARRAY_LENGTH(fbdev_options), argc, argv);
906
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400907 return fbdev_compositor_create(display, argc, argv, config, &param);
Philip Withnall4f499172013-02-02 12:02:32 +0000908}