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