blob: 7a6f2556974fc3d876da522bda0e8e146fdd1dac [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. */
243 if (finfo->visual != FB_VISUAL_TRUECOLOR || vinfo->grayscale != 0)
244 return 0;
245
246 /* We only support formats with MSBs on the left. */
247 if (vinfo->red.msb_right != 0 || vinfo->green.msb_right != 0 ||
248 vinfo->blue.msb_right != 0)
249 return 0;
250
251 /* Work out the format type from the offsets. We only support RGBA and
252 * ARGB at the moment. */
253 type = PIXMAN_TYPE_OTHER;
254
255 if ((vinfo->transp.offset >= vinfo->red.offset ||
256 vinfo->transp.length == 0) &&
257 vinfo->red.offset >= vinfo->green.offset &&
258 vinfo->green.offset >= vinfo->blue.offset)
259 type = PIXMAN_TYPE_ARGB;
260 else if (vinfo->red.offset >= vinfo->green.offset &&
261 vinfo->green.offset >= vinfo->blue.offset &&
262 vinfo->blue.offset >= vinfo->transp.offset)
263 type = PIXMAN_TYPE_RGBA;
264
265 if (type == PIXMAN_TYPE_OTHER)
266 return 0;
267
268 /* Build the format. */
269 return PIXMAN_FORMAT(vinfo->bits_per_pixel, type,
270 vinfo->transp.length,
271 vinfo->red.length,
272 vinfo->green.length,
273 vinfo->blue.length);
274}
275
276static int
277calculate_refresh_rate(struct fb_var_screeninfo *vinfo)
278{
279 uint64_t quot;
280
281 /* Calculate monitor refresh rate. Default is 60 Hz. Units are mHz. */
282 quot = (vinfo->upper_margin + vinfo->lower_margin + vinfo->yres);
283 quot *= (vinfo->left_margin + vinfo->right_margin + vinfo->xres);
284 quot *= vinfo->pixclock;
285
286 if (quot > 0) {
287 uint64_t refresh_rate;
288
289 refresh_rate = 1000000000000000LLU / quot;
290 if (refresh_rate > 200000)
291 refresh_rate = 200000; /* cap at 200 Hz */
292
293 return refresh_rate;
294 }
295
296 return 60 * 1000; /* default to 60 Hz */
297}
298
299static int
300fbdev_query_screen_info(struct fbdev_output *output, int fd,
301 struct fbdev_screeninfo *info)
302{
303 struct fb_var_screeninfo varinfo;
304 struct fb_fix_screeninfo fixinfo;
305
306 /* Probe the device for screen information. */
307 if (ioctl(fd, FBIOGET_FSCREENINFO, &fixinfo) < 0 ||
308 ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
309 return -1;
310 }
311
312 /* Store the pertinent data. */
313 info->x_resolution = varinfo.xres;
314 info->y_resolution = varinfo.yres;
315 info->width_mm = varinfo.width;
316 info->height_mm = varinfo.height;
317 info->bits_per_pixel = varinfo.bits_per_pixel;
318
319 info->buffer_length = fixinfo.smem_len;
320 info->line_length = fixinfo.line_length;
321 strncpy(info->id, fixinfo.id, sizeof(info->id) / sizeof(*info->id));
322
323 info->pixel_format = calculate_pixman_format(&varinfo, &fixinfo);
324 info->refresh_rate = calculate_refresh_rate(&varinfo);
325
326 if (info->pixel_format == 0) {
327 weston_log("Frame buffer uses an unsupported format.\n");
328 return -1;
329 }
330
331 return 1;
332}
333
334static int
335fbdev_set_screen_info(struct fbdev_output *output, int fd,
336 struct fbdev_screeninfo *info)
337{
338 struct fb_var_screeninfo varinfo;
339
340 /* Grab the current screen information. */
341 if (ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
342 return -1;
343 }
344
345 /* Update the information. */
346 varinfo.xres = info->x_resolution;
347 varinfo.yres = info->y_resolution;
348 varinfo.width = info->width_mm;
349 varinfo.height = info->height_mm;
350 varinfo.bits_per_pixel = info->bits_per_pixel;
351
352 /* Try to set up an ARGB (x8r8g8b8) pixel format. */
353 varinfo.grayscale = 0;
354 varinfo.transp.offset = 24;
355 varinfo.transp.length = 0;
356 varinfo.transp.msb_right = 0;
357 varinfo.red.offset = 16;
358 varinfo.red.length = 8;
359 varinfo.red.msb_right = 0;
360 varinfo.green.offset = 8;
361 varinfo.green.length = 8;
362 varinfo.green.msb_right = 0;
363 varinfo.blue.offset = 0;
364 varinfo.blue.length = 8;
365 varinfo.blue.msb_right = 0;
366
367 /* Set the device's screen information. */
368 if (ioctl(fd, FBIOPUT_VSCREENINFO, &varinfo) < 0) {
369 return -1;
370 }
371
372 return 1;
373}
374
375static void fbdev_frame_buffer_destroy(struct fbdev_output *output);
376
377/* Returns an FD for the frame buffer device. */
378static int
379fbdev_frame_buffer_open(struct fbdev_output *output, const char *fb_dev,
380 struct fbdev_screeninfo *screen_info)
381{
382 int fd = -1;
383
384 weston_log("Opening fbdev frame buffer.\n");
385
386 /* Open the frame buffer device. */
387 fd = open(fb_dev, O_RDWR | O_CLOEXEC);
388 if (fd < 0) {
389 weston_log("Failed to open frame buffer device ‘%s’: %s\n",
390 fb_dev, strerror(errno));
391 return -1;
392 }
393
394 /* Grab the screen info. */
395 if (fbdev_query_screen_info(output, fd, screen_info) < 0) {
396 weston_log("Failed to get frame buffer info: %s\n",
397 strerror(errno));
398
399 close(fd);
400 return -1;
401 }
402
403 return fd;
404}
405
406/* Closes the FD on success or failure. */
407static int
408fbdev_frame_buffer_map(struct fbdev_output *output, int fd)
409{
410 int retval = -1;
411
412 weston_log("Mapping fbdev frame buffer.\n");
413
414 /* Map the frame buffer. Write-only mode, since we don't want to read
415 * anything back (because it's slow). */
416 output->fb = mmap(NULL, output->fb_info.buffer_length,
417 PROT_WRITE, MAP_SHARED, fd, 0);
418 if (output->fb == MAP_FAILED) {
419 weston_log("Failed to mmap frame buffer: %s\n",
420 strerror(errno));
421 goto out_close;
422 }
423
424 /* Create a pixman image to wrap the memory mapped frame buffer. */
425 output->hw_surface =
426 pixman_image_create_bits(output->fb_info.pixel_format,
427 output->fb_info.x_resolution,
428 output->fb_info.y_resolution,
429 output->fb,
430 output->fb_info.line_length);
431 if (output->hw_surface == NULL) {
432 weston_log("Failed to create surface for frame buffer.\n");
433 goto out_unmap;
434 }
435
436 /* Success! */
437 retval = 0;
438
439out_unmap:
440 if (retval != 0 && output->fb != NULL)
441 fbdev_frame_buffer_destroy(output);
442
443out_close:
444 if (fd >= 0)
445 close(fd);
446
447 return retval;
448}
449
450static void
451fbdev_frame_buffer_destroy(struct fbdev_output *output)
452{
453 weston_log("Destroying fbdev frame buffer.\n");
454
455 if (munmap(output->fb, output->fb_info.buffer_length) < 0)
456 weston_log("Failed to munmap frame buffer: %s\n",
457 strerror(errno));
458
459 output->fb = NULL;
460}
461
462static void fbdev_output_destroy(struct weston_output *base);
463static void fbdev_output_disable(struct weston_output *base);
464
465static int
466fbdev_output_create(struct fbdev_compositor *compositor,
467 const char *device)
468{
469 struct fbdev_output *output;
470 pixman_transform_t transform;
471 int fb_fd;
472 int shadow_width, shadow_height;
473 int width, height;
474 unsigned int bytes_per_pixel;
475 struct wl_event_loop *loop;
476
477 weston_log("Creating fbdev output.\n");
478
479 output = calloc(1, sizeof *output);
480 if (!output)
481 return -1;
482
483 output->compositor = compositor;
484 output->device = device;
485
486 /* Create the frame buffer. */
487 fb_fd = fbdev_frame_buffer_open(output, device, &output->fb_info);
488 if (fb_fd < 0) {
489 weston_log("Creating frame buffer failed.\n");
490 goto out_free;
491 }
492
493 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
494 weston_log("Mapping frame buffer failed.\n");
495 goto out_free;
496 }
497
Jonas Ådahle5a12252013-04-05 23:07:11 +0200498 output->base.start_repaint_loop = fbdev_output_start_repaint_loop;
Philip Withnall4f499172013-02-02 12:02:32 +0000499 output->base.repaint = fbdev_output_repaint;
500 output->base.destroy = fbdev_output_destroy;
501 output->base.assign_planes = NULL;
502 output->base.set_backlight = NULL;
503 output->base.set_dpms = NULL;
504 output->base.switch_mode = NULL;
505
506 /* only one static mode in list */
507 output->mode.flags =
508 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
509 output->mode.width = output->fb_info.x_resolution;
510 output->mode.height = output->fb_info.y_resolution;
511 output->mode.refresh = output->fb_info.refresh_rate;
512 wl_list_init(&output->base.mode_list);
513 wl_list_insert(&output->base.mode_list, &output->mode.link);
514
515 output->base.current = &output->mode;
516 output->base.origin = &output->mode;
517 output->base.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
518 output->base.make = "unknown";
519 output->base.model = output->fb_info.id;
520
521 weston_output_init(&output->base, &compositor->base,
522 0, 0, output->fb_info.width_mm,
523 output->fb_info.height_mm,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200524 WL_OUTPUT_TRANSFORM_NORMAL,
525 1);
Philip Withnall4f499172013-02-02 12:02:32 +0000526
527 width = output->fb_info.x_resolution;
528 height = output->fb_info.y_resolution;
529
530 pixman_transform_init_identity(&transform);
531 switch (output->base.transform) {
532 default:
533 case WL_OUTPUT_TRANSFORM_NORMAL:
534 shadow_width = width;
535 shadow_height = height;
536 pixman_transform_rotate(&transform,
537 NULL, 0, 0);
538 pixman_transform_translate(&transform, NULL,
539 0, 0);
540 break;
541 case WL_OUTPUT_TRANSFORM_180:
542 shadow_width = width;
543 shadow_height = height;
544 pixman_transform_rotate(&transform,
545 NULL, -pixman_fixed_1, 0);
546 pixman_transform_translate(NULL, &transform,
547 pixman_int_to_fixed(shadow_width),
548 pixman_int_to_fixed(shadow_height));
549 break;
550 case WL_OUTPUT_TRANSFORM_270:
551 shadow_width = height;
552 shadow_height = width;
553 pixman_transform_rotate(&transform,
554 NULL, 0, pixman_fixed_1);
555 pixman_transform_translate(&transform,
556 NULL,
557 pixman_int_to_fixed(shadow_width),
558 0);
559 break;
560 case WL_OUTPUT_TRANSFORM_90:
561 shadow_width = height;
562 shadow_height = width;
563 pixman_transform_rotate(&transform,
564 NULL, 0, -pixman_fixed_1);
565 pixman_transform_translate(&transform,
566 NULL,
567 0,
568 pixman_int_to_fixed(shadow_height));
569 break;
570 }
571
572 bytes_per_pixel = output->fb_info.bits_per_pixel / 8;
573
574 output->shadow_buf = malloc(width * height * bytes_per_pixel);
575 output->shadow_surface =
576 pixman_image_create_bits(output->fb_info.pixel_format,
577 shadow_width, shadow_height,
578 output->shadow_buf,
579 shadow_width * bytes_per_pixel);
580 if (output->shadow_buf == NULL || output->shadow_surface == NULL) {
581 weston_log("Failed to create surface for frame buffer.\n");
582 goto out_hw_surface;
583 }
584
585 /* No need in transform for normal output */
586 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
587 pixman_image_set_transform(output->shadow_surface, &transform);
588
589 if (pixman_renderer_output_create(&output->base) < 0)
590 goto out_shadow_surface;
591
592 loop = wl_display_get_event_loop(compositor->base.wl_display);
593 output->finish_frame_timer =
594 wl_event_loop_add_timer(loop, finish_frame_handler, output);
595
596 wl_list_insert(compositor->base.output_list.prev, &output->base.link);
597
598 weston_log("fbdev output %d×%d px\n",
599 output->mode.width, output->mode.height);
600 weston_log_continue(STAMP_SPACE "guessing %d Hz and 96 dpi\n",
601 output->mode.refresh / 1000);
602
603 return 0;
604
605out_shadow_surface:
606 pixman_image_unref(output->shadow_surface);
607 output->shadow_surface = NULL;
608out_hw_surface:
609 free(output->shadow_buf);
610 pixman_image_unref(output->hw_surface);
611 output->hw_surface = NULL;
612 weston_output_destroy(&output->base);
613 fbdev_frame_buffer_destroy(output);
614out_free:
615 free(output);
616
617 return -1;
618}
619
620static void
621fbdev_output_destroy(struct weston_output *base)
622{
623 struct fbdev_output *output = to_fbdev_output(base);
624
625 weston_log("Destroying fbdev output.\n");
626
627 /* Close the frame buffer. */
628 fbdev_output_disable(base);
629
630 if (base->renderer_state != NULL)
631 pixman_renderer_output_destroy(base);
632
633 if (output->shadow_surface != NULL) {
634 pixman_image_unref(output->shadow_surface);
635 output->shadow_surface = NULL;
636 }
637
638 if (output->shadow_buf != NULL) {
639 free(output->shadow_buf);
640 output->shadow_buf = NULL;
641 }
642
643 /* Remove the output. */
644 wl_list_remove(&output->base.link);
645 weston_output_destroy(&output->base);
646
647 free(output);
648}
649
650/* strcmp()-style return values. */
651static int
652compare_screen_info (const struct fbdev_screeninfo *a,
653 const struct fbdev_screeninfo *b)
654{
655 if (a->x_resolution == b->x_resolution &&
656 a->y_resolution == b->y_resolution &&
657 a->width_mm == b->width_mm &&
658 a->height_mm == b->height_mm &&
659 a->bits_per_pixel == b->bits_per_pixel &&
660 a->pixel_format == b->pixel_format &&
661 a->refresh_rate == b->refresh_rate)
662 return 0;
663
664 return 1;
665}
666
667static int
668fbdev_output_reenable(struct fbdev_compositor *compositor,
669 struct weston_output *base)
670{
671 struct fbdev_output *output = to_fbdev_output(base);
672 struct fbdev_screeninfo new_screen_info;
673 int fb_fd;
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100674 const char *device;
Philip Withnall4f499172013-02-02 12:02:32 +0000675
676 weston_log("Re-enabling fbdev output.\n");
677
678 /* Create the frame buffer. */
679 fb_fd = fbdev_frame_buffer_open(output, output->device,
680 &new_screen_info);
681 if (fb_fd < 0) {
682 weston_log("Creating frame buffer failed.\n");
683 goto err;
684 }
685
686 /* Check whether the frame buffer details have changed since we were
687 * disabled. */
688 if (compare_screen_info (&output->fb_info, &new_screen_info) != 0) {
689 /* Perform a mode-set to restore the old mode. */
690 if (fbdev_set_screen_info(output, fb_fd,
691 &output->fb_info) < 0) {
692 weston_log("Failed to restore mode settings. "
693 "Attempting to re-open output anyway.\n");
694 }
695
Rob Bradford581b3fd2013-07-26 16:29:39 +0100696 close(fb_fd);
697
Philip Withnall4f499172013-02-02 12:02:32 +0000698 /* Remove and re-add the output so that resources depending on
699 * the frame buffer X/Y resolution (such as the shadow buffer)
700 * are re-initialised. */
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100701 device = output->device;
Philip Withnall4f499172013-02-02 12:02:32 +0000702 fbdev_output_destroy(base);
Rob Bradfordf8ef42f2013-07-26 16:29:38 +0100703 fbdev_output_create(compositor, device);
Philip Withnall4f499172013-02-02 12:02:32 +0000704
705 return 0;
706 }
707
708 /* Map the device if it has the same details as before. */
709 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
710 weston_log("Mapping frame buffer failed.\n");
711 goto err;
712 }
713
714 return 0;
715
716err:
717 return -1;
718}
719
720/* NOTE: This leaves output->fb_info populated, caching data so that if
721 * fbdev_output_reenable() is called again, it can determine whether a mode-set
722 * is needed. */
723static void
724fbdev_output_disable(struct weston_output *base)
725{
726 struct fbdev_output *output = to_fbdev_output(base);
727
728 weston_log("Disabling fbdev output.\n");
729
730 if (output->hw_surface != NULL) {
731 pixman_image_unref(output->hw_surface);
732 output->hw_surface = NULL;
733 }
734
735 fbdev_frame_buffer_destroy(output);
736}
737
738static void
Philip Withnall4f499172013-02-02 12:02:32 +0000739fbdev_compositor_destroy(struct weston_compositor *base)
740{
741 struct fbdev_compositor *compositor = to_fbdev_compositor(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000742
Rob Bradfordd355b802013-05-31 18:09:55 +0100743 udev_input_destroy(&compositor->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000744
745 /* Destroy the output. */
746 weston_compositor_shutdown(&compositor->base);
747
748 /* Chain up. */
749 compositor->base.renderer->destroy(&compositor->base);
750 tty_destroy(compositor->tty);
751
752 free(compositor);
753}
754
755static void
756vt_func(struct weston_compositor *base, int event)
757{
758 struct fbdev_compositor *compositor = to_fbdev_compositor(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000759 struct weston_output *output;
760
761 switch (event) {
762 case TTY_ENTER_VT:
763 weston_log("entering VT\n");
764 compositor->base.focus = 1;
765 compositor->base.state = compositor->prev_state;
766
767 wl_list_for_each(output, &compositor->base.output_list, link) {
768 fbdev_output_reenable(compositor, output);
769 }
770
771 weston_compositor_damage_all(&compositor->base);
772
Rob Bradfordd355b802013-05-31 18:09:55 +0100773 udev_input_enable(&compositor->input, compositor->udev);
Philip Withnall4f499172013-02-02 12:02:32 +0000774 break;
775 case TTY_LEAVE_VT:
776 weston_log("leaving VT\n");
Rob Bradfordd355b802013-05-31 18:09:55 +0100777 udev_input_disable(&compositor->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000778
779 wl_list_for_each(output, &compositor->base.output_list, link) {
780 fbdev_output_disable(output);
781 }
782
783 compositor->base.focus = 0;
784 compositor->prev_state = compositor->base.state;
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100785 weston_compositor_offscreen(&compositor->base);
Philip Withnall4f499172013-02-02 12:02:32 +0000786
787 /* If we have a repaint scheduled (from the idle handler), make
788 * sure we cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100789 * vt switched away. The OFFSCREEN state will prevent
Philip Withnall4f499172013-02-02 12:02:32 +0000790 * further attemps at repainting. When we switch
791 * back, we schedule a repaint, which will process
792 * pending frame callbacks. */
793
794 wl_list_for_each(output,
795 &compositor->base.output_list, link) {
796 output->repaint_needed = 0;
797 }
798
799 break;
800 };
801}
802
803static void
804fbdev_restore(struct weston_compositor *base)
805{
806 struct fbdev_compositor *compositor = to_fbdev_compositor(base);
807
808 tty_reset(compositor->tty);
809}
810
811static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400812switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Philip Withnall4f499172013-02-02 12:02:32 +0000813{
814 struct fbdev_compositor *ec = data;
815
816 tty_activate_vt(ec->tty, key - KEY_F1 + 1);
817}
818
819static struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500820fbdev_compositor_create(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400821 struct weston_config *config,
822 struct fbdev_parameters *param)
Philip Withnall4f499172013-02-02 12:02:32 +0000823{
824 struct fbdev_compositor *compositor;
Rob Bradford2387fde2013-05-31 18:09:57 +0100825 const char *seat_id = default_seat;
Philip Withnall4f499172013-02-02 12:02:32 +0000826 uint32_t key;
827
828 weston_log("initializing fbdev backend\n");
829
830 compositor = calloc(1, sizeof *compositor);
831 if (compositor == NULL)
832 return NULL;
833
834 if (weston_compositor_init(&compositor->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400835 config) < 0)
Philip Withnall4f499172013-02-02 12:02:32 +0000836 goto out_free;
837
Adrian Negreanuc8384232013-07-28 18:27:23 +0300838 /* Check if we run fbdev-backend using weston-launch */
839 compositor->base.launcher_sock =
840 weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
841 if (compositor->base.launcher_sock == -1 && geteuid() != 0) {
842 weston_log("fatal: fbdev backend should be run "
843 "using weston-launch binary or as root\n");
844 goto out_compositor;
845 }
846
Philip Withnall4f499172013-02-02 12:02:32 +0000847 compositor->udev = udev_new();
848 if (compositor->udev == NULL) {
849 weston_log("Failed to initialize udev context.\n");
850 goto out_compositor;
851 }
852
853 /* Set up the TTY. */
854 compositor->tty = tty_create(&compositor->base, vt_func, param->tty);
855 if (!compositor->tty) {
856 weston_log("Failed to initialize tty.\n");
857 goto out_udev;
858 }
859
860 compositor->base.destroy = fbdev_compositor_destroy;
861 compositor->base.restore = fbdev_restore;
862
863 compositor->base.focus = 1;
864 compositor->prev_state = WESTON_COMPOSITOR_ACTIVE;
865
866 for (key = KEY_F1; key < KEY_F9; key++)
867 weston_compositor_add_key_binding(&compositor->base, key,
868 MODIFIER_CTRL | MODIFIER_ALT,
869 switch_vt_binding,
870 compositor);
871
872 if (pixman_renderer_init(&compositor->base) < 0)
873 goto out_tty;
874
875 if (fbdev_output_create(compositor, param->device) < 0)
876 goto out_pixman;
877
Rob Bradford2387fde2013-05-31 18:09:57 +0100878 udev_input_init(&compositor->input, &compositor->base, compositor->udev, seat_id);
Philip Withnall4f499172013-02-02 12:02:32 +0000879
880 return &compositor->base;
881
882out_pixman:
883 compositor->base.renderer->destroy(&compositor->base);
884
885out_tty:
886 tty_destroy(compositor->tty);
887
888out_udev:
889 udev_unref(compositor->udev);
890
891out_compositor:
892 weston_compositor_shutdown(&compositor->base);
893
894out_free:
895 free(compositor);
896
897 return NULL;
898}
899
900WL_EXPORT struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500901backend_init(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400902 struct weston_config *config)
Philip Withnall4f499172013-02-02 12:02:32 +0000903{
904 /* TODO: Ideally, available frame buffers should be enumerated using
905 * udev, rather than passing a device node in as a parameter. */
906 struct fbdev_parameters param = {
907 .tty = 0, /* default to current tty */
908 .device = "/dev/fb0", /* default frame buffer */
909 };
910
911 const struct weston_option fbdev_options[] = {
912 { WESTON_OPTION_INTEGER, "tty", 0, &param.tty },
913 { WESTON_OPTION_STRING, "device", 0, &param.device },
914 };
915
916 parse_options(fbdev_options, ARRAY_LENGTH(fbdev_options), argc, argv);
917
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400918 return fbdev_compositor_create(display, argc, argv, config, &param);
Philip Withnall4f499172013-02-02 12:02:32 +0000919}