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