blob: 10544d12b601986ffb5d92a24a3ce68e8078cd9f [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>
38
39#include <libudev.h>
40
41#include "config.h"
42
43#include "compositor.h"
44#include "launcher-util.h"
45#include "pixman-renderer.h"
46#include "evdev.h"
47
48struct fbdev_compositor {
49 struct weston_compositor base;
50 uint32_t prev_state;
51
52 struct udev *udev;
53 struct tty *tty;
54};
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
90struct fbdev_seat {
91 struct weston_seat base;
92 struct wl_list devices_list;
93
94 struct udev_monitor *udev_monitor;
95 struct wl_event_source *udev_monitor_source;
96 char *seat_id;
97};
98
99struct fbdev_parameters {
100 int tty;
101 char *device;
102};
103
104static inline struct fbdev_output *
105to_fbdev_output(struct weston_output *base)
106{
107 return container_of(base, struct fbdev_output, base);
108}
109
110static inline struct fbdev_seat *
111to_fbdev_seat(struct weston_seat *base)
112{
113 return container_of(base, struct fbdev_seat, base);
114}
115
116static inline struct fbdev_compositor *
117to_fbdev_compositor(struct weston_compositor *base)
118{
119 return container_of(base, struct fbdev_compositor, base);
120}
121
122static void
123fbdev_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;
200 uint32_t msec;
201 struct timeval tv;
202
203 gettimeofday(&tv, NULL);
204 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
205 weston_output_finish_frame(&output->base, msec);
206
207 return 1;
208}
209
210static pixman_format_code_t
211calculate_pixman_format(struct fb_var_screeninfo *vinfo,
212 struct fb_fix_screeninfo *finfo)
213{
214 /* Calculate the pixman format supported by the frame buffer from the
215 * buffer's metadata. Return 0 if no known pixman format is supported
216 * (since this has depth 0 it's guaranteed to not conflict with any
217 * actual pixman format).
218 *
219 * Documentation on the vinfo and finfo structures:
220 * http://www.mjmwired.net/kernel/Documentation/fb/api.txt
221 *
222 * TODO: Try a bit harder to support other formats, including setting
223 * the preferred format in the hardware. */
224 int type;
225
226 weston_log("Calculating pixman format from:\n"
227 STAMP_SPACE " - type: %i (aux: %i)\n"
228 STAMP_SPACE " - visual: %i\n"
229 STAMP_SPACE " - bpp: %i (grayscale: %i)\n"
230 STAMP_SPACE " - red: offset: %i, length: %i, MSB: %i\n"
231 STAMP_SPACE " - green: offset: %i, length: %i, MSB: %i\n"
232 STAMP_SPACE " - blue: offset: %i, length: %i, MSB: %i\n"
233 STAMP_SPACE " - transp: offset: %i, length: %i, MSB: %i\n",
234 finfo->type, finfo->type_aux, finfo->visual,
235 vinfo->bits_per_pixel, vinfo->grayscale,
236 vinfo->red.offset, vinfo->red.length, vinfo->red.msb_right,
237 vinfo->green.offset, vinfo->green.length,
238 vinfo->green.msb_right,
239 vinfo->blue.offset, vinfo->blue.length,
240 vinfo->blue.msb_right,
241 vinfo->transp.offset, vinfo->transp.length,
242 vinfo->transp.msb_right);
243
244 /* We only handle packed formats at the moment. */
245 if (finfo->type != FB_TYPE_PACKED_PIXELS)
246 return 0;
247
248 /* We only handle true-colour frame buffers at the moment. */
249 if (finfo->visual != FB_VISUAL_TRUECOLOR || vinfo->grayscale != 0)
250 return 0;
251
252 /* We only support formats with MSBs on the left. */
253 if (vinfo->red.msb_right != 0 || vinfo->green.msb_right != 0 ||
254 vinfo->blue.msb_right != 0)
255 return 0;
256
257 /* Work out the format type from the offsets. We only support RGBA and
258 * ARGB at the moment. */
259 type = PIXMAN_TYPE_OTHER;
260
261 if ((vinfo->transp.offset >= vinfo->red.offset ||
262 vinfo->transp.length == 0) &&
263 vinfo->red.offset >= vinfo->green.offset &&
264 vinfo->green.offset >= vinfo->blue.offset)
265 type = PIXMAN_TYPE_ARGB;
266 else if (vinfo->red.offset >= vinfo->green.offset &&
267 vinfo->green.offset >= vinfo->blue.offset &&
268 vinfo->blue.offset >= vinfo->transp.offset)
269 type = PIXMAN_TYPE_RGBA;
270
271 if (type == PIXMAN_TYPE_OTHER)
272 return 0;
273
274 /* Build the format. */
275 return PIXMAN_FORMAT(vinfo->bits_per_pixel, type,
276 vinfo->transp.length,
277 vinfo->red.length,
278 vinfo->green.length,
279 vinfo->blue.length);
280}
281
282static int
283calculate_refresh_rate(struct fb_var_screeninfo *vinfo)
284{
285 uint64_t quot;
286
287 /* Calculate monitor refresh rate. Default is 60 Hz. Units are mHz. */
288 quot = (vinfo->upper_margin + vinfo->lower_margin + vinfo->yres);
289 quot *= (vinfo->left_margin + vinfo->right_margin + vinfo->xres);
290 quot *= vinfo->pixclock;
291
292 if (quot > 0) {
293 uint64_t refresh_rate;
294
295 refresh_rate = 1000000000000000LLU / quot;
296 if (refresh_rate > 200000)
297 refresh_rate = 200000; /* cap at 200 Hz */
298
299 return refresh_rate;
300 }
301
302 return 60 * 1000; /* default to 60 Hz */
303}
304
305static int
306fbdev_query_screen_info(struct fbdev_output *output, int fd,
307 struct fbdev_screeninfo *info)
308{
309 struct fb_var_screeninfo varinfo;
310 struct fb_fix_screeninfo fixinfo;
311
312 /* Probe the device for screen information. */
313 if (ioctl(fd, FBIOGET_FSCREENINFO, &fixinfo) < 0 ||
314 ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
315 return -1;
316 }
317
318 /* Store the pertinent data. */
319 info->x_resolution = varinfo.xres;
320 info->y_resolution = varinfo.yres;
321 info->width_mm = varinfo.width;
322 info->height_mm = varinfo.height;
323 info->bits_per_pixel = varinfo.bits_per_pixel;
324
325 info->buffer_length = fixinfo.smem_len;
326 info->line_length = fixinfo.line_length;
327 strncpy(info->id, fixinfo.id, sizeof(info->id) / sizeof(*info->id));
328
329 info->pixel_format = calculate_pixman_format(&varinfo, &fixinfo);
330 info->refresh_rate = calculate_refresh_rate(&varinfo);
331
332 if (info->pixel_format == 0) {
333 weston_log("Frame buffer uses an unsupported format.\n");
334 return -1;
335 }
336
337 return 1;
338}
339
340static int
341fbdev_set_screen_info(struct fbdev_output *output, int fd,
342 struct fbdev_screeninfo *info)
343{
344 struct fb_var_screeninfo varinfo;
345
346 /* Grab the current screen information. */
347 if (ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
348 return -1;
349 }
350
351 /* Update the information. */
352 varinfo.xres = info->x_resolution;
353 varinfo.yres = info->y_resolution;
354 varinfo.width = info->width_mm;
355 varinfo.height = info->height_mm;
356 varinfo.bits_per_pixel = info->bits_per_pixel;
357
358 /* Try to set up an ARGB (x8r8g8b8) pixel format. */
359 varinfo.grayscale = 0;
360 varinfo.transp.offset = 24;
361 varinfo.transp.length = 0;
362 varinfo.transp.msb_right = 0;
363 varinfo.red.offset = 16;
364 varinfo.red.length = 8;
365 varinfo.red.msb_right = 0;
366 varinfo.green.offset = 8;
367 varinfo.green.length = 8;
368 varinfo.green.msb_right = 0;
369 varinfo.blue.offset = 0;
370 varinfo.blue.length = 8;
371 varinfo.blue.msb_right = 0;
372
373 /* Set the device's screen information. */
374 if (ioctl(fd, FBIOPUT_VSCREENINFO, &varinfo) < 0) {
375 return -1;
376 }
377
378 return 1;
379}
380
381static void fbdev_frame_buffer_destroy(struct fbdev_output *output);
382
383/* Returns an FD for the frame buffer device. */
384static int
385fbdev_frame_buffer_open(struct fbdev_output *output, const char *fb_dev,
386 struct fbdev_screeninfo *screen_info)
387{
388 int fd = -1;
389
390 weston_log("Opening fbdev frame buffer.\n");
391
392 /* Open the frame buffer device. */
393 fd = open(fb_dev, O_RDWR | O_CLOEXEC);
394 if (fd < 0) {
395 weston_log("Failed to open frame buffer device ‘%s’: %s\n",
396 fb_dev, strerror(errno));
397 return -1;
398 }
399
400 /* Grab the screen info. */
401 if (fbdev_query_screen_info(output, fd, screen_info) < 0) {
402 weston_log("Failed to get frame buffer info: %s\n",
403 strerror(errno));
404
405 close(fd);
406 return -1;
407 }
408
409 return fd;
410}
411
412/* Closes the FD on success or failure. */
413static int
414fbdev_frame_buffer_map(struct fbdev_output *output, int fd)
415{
416 int retval = -1;
417
418 weston_log("Mapping fbdev frame buffer.\n");
419
420 /* Map the frame buffer. Write-only mode, since we don't want to read
421 * anything back (because it's slow). */
422 output->fb = mmap(NULL, output->fb_info.buffer_length,
423 PROT_WRITE, MAP_SHARED, fd, 0);
424 if (output->fb == MAP_FAILED) {
425 weston_log("Failed to mmap frame buffer: %s\n",
426 strerror(errno));
427 goto out_close;
428 }
429
430 /* Create a pixman image to wrap the memory mapped frame buffer. */
431 output->hw_surface =
432 pixman_image_create_bits(output->fb_info.pixel_format,
433 output->fb_info.x_resolution,
434 output->fb_info.y_resolution,
435 output->fb,
436 output->fb_info.line_length);
437 if (output->hw_surface == NULL) {
438 weston_log("Failed to create surface for frame buffer.\n");
439 goto out_unmap;
440 }
441
442 /* Success! */
443 retval = 0;
444
445out_unmap:
446 if (retval != 0 && output->fb != NULL)
447 fbdev_frame_buffer_destroy(output);
448
449out_close:
450 if (fd >= 0)
451 close(fd);
452
453 return retval;
454}
455
456static void
457fbdev_frame_buffer_destroy(struct fbdev_output *output)
458{
459 weston_log("Destroying fbdev frame buffer.\n");
460
461 if (munmap(output->fb, output->fb_info.buffer_length) < 0)
462 weston_log("Failed to munmap frame buffer: %s\n",
463 strerror(errno));
464
465 output->fb = NULL;
466}
467
468static void fbdev_output_destroy(struct weston_output *base);
469static void fbdev_output_disable(struct weston_output *base);
470
471static int
472fbdev_output_create(struct fbdev_compositor *compositor,
473 const char *device)
474{
475 struct fbdev_output *output;
476 pixman_transform_t transform;
477 int fb_fd;
478 int shadow_width, shadow_height;
479 int width, height;
480 unsigned int bytes_per_pixel;
481 struct wl_event_loop *loop;
482
483 weston_log("Creating fbdev output.\n");
484
485 output = calloc(1, sizeof *output);
486 if (!output)
487 return -1;
488
489 output->compositor = compositor;
490 output->device = device;
491
492 /* Create the frame buffer. */
493 fb_fd = fbdev_frame_buffer_open(output, device, &output->fb_info);
494 if (fb_fd < 0) {
495 weston_log("Creating frame buffer failed.\n");
496 goto out_free;
497 }
498
499 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
500 weston_log("Mapping frame buffer failed.\n");
501 goto out_free;
502 }
503
504 output->base.repaint = fbdev_output_repaint;
505 output->base.destroy = fbdev_output_destroy;
506 output->base.assign_planes = NULL;
507 output->base.set_backlight = NULL;
508 output->base.set_dpms = NULL;
509 output->base.switch_mode = NULL;
510
511 /* only one static mode in list */
512 output->mode.flags =
513 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
514 output->mode.width = output->fb_info.x_resolution;
515 output->mode.height = output->fb_info.y_resolution;
516 output->mode.refresh = output->fb_info.refresh_rate;
517 wl_list_init(&output->base.mode_list);
518 wl_list_insert(&output->base.mode_list, &output->mode.link);
519
520 output->base.current = &output->mode;
521 output->base.origin = &output->mode;
522 output->base.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
523 output->base.make = "unknown";
524 output->base.model = output->fb_info.id;
525
526 weston_output_init(&output->base, &compositor->base,
527 0, 0, output->fb_info.width_mm,
528 output->fb_info.height_mm,
529 WL_OUTPUT_TRANSFORM_NORMAL);
530
531 width = output->fb_info.x_resolution;
532 height = output->fb_info.y_resolution;
533
534 pixman_transform_init_identity(&transform);
535 switch (output->base.transform) {
536 default:
537 case WL_OUTPUT_TRANSFORM_NORMAL:
538 shadow_width = width;
539 shadow_height = height;
540 pixman_transform_rotate(&transform,
541 NULL, 0, 0);
542 pixman_transform_translate(&transform, NULL,
543 0, 0);
544 break;
545 case WL_OUTPUT_TRANSFORM_180:
546 shadow_width = width;
547 shadow_height = height;
548 pixman_transform_rotate(&transform,
549 NULL, -pixman_fixed_1, 0);
550 pixman_transform_translate(NULL, &transform,
551 pixman_int_to_fixed(shadow_width),
552 pixman_int_to_fixed(shadow_height));
553 break;
554 case WL_OUTPUT_TRANSFORM_270:
555 shadow_width = height;
556 shadow_height = width;
557 pixman_transform_rotate(&transform,
558 NULL, 0, pixman_fixed_1);
559 pixman_transform_translate(&transform,
560 NULL,
561 pixman_int_to_fixed(shadow_width),
562 0);
563 break;
564 case WL_OUTPUT_TRANSFORM_90:
565 shadow_width = height;
566 shadow_height = width;
567 pixman_transform_rotate(&transform,
568 NULL, 0, -pixman_fixed_1);
569 pixman_transform_translate(&transform,
570 NULL,
571 0,
572 pixman_int_to_fixed(shadow_height));
573 break;
574 }
575
576 bytes_per_pixel = output->fb_info.bits_per_pixel / 8;
577
578 output->shadow_buf = malloc(width * height * bytes_per_pixel);
579 output->shadow_surface =
580 pixman_image_create_bits(output->fb_info.pixel_format,
581 shadow_width, shadow_height,
582 output->shadow_buf,
583 shadow_width * bytes_per_pixel);
584 if (output->shadow_buf == NULL || output->shadow_surface == NULL) {
585 weston_log("Failed to create surface for frame buffer.\n");
586 goto out_hw_surface;
587 }
588
589 /* No need in transform for normal output */
590 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
591 pixman_image_set_transform(output->shadow_surface, &transform);
592
593 if (pixman_renderer_output_create(&output->base) < 0)
594 goto out_shadow_surface;
595
596 loop = wl_display_get_event_loop(compositor->base.wl_display);
597 output->finish_frame_timer =
598 wl_event_loop_add_timer(loop, finish_frame_handler, output);
599
600 wl_list_insert(compositor->base.output_list.prev, &output->base.link);
601
602 weston_log("fbdev output %d×%d px\n",
603 output->mode.width, output->mode.height);
604 weston_log_continue(STAMP_SPACE "guessing %d Hz and 96 dpi\n",
605 output->mode.refresh / 1000);
606
607 return 0;
608
609out_shadow_surface:
610 pixman_image_unref(output->shadow_surface);
611 output->shadow_surface = NULL;
612out_hw_surface:
613 free(output->shadow_buf);
614 pixman_image_unref(output->hw_surface);
615 output->hw_surface = NULL;
616 weston_output_destroy(&output->base);
617 fbdev_frame_buffer_destroy(output);
618out_free:
619 free(output);
620
621 return -1;
622}
623
624static void
625fbdev_output_destroy(struct weston_output *base)
626{
627 struct fbdev_output *output = to_fbdev_output(base);
628
629 weston_log("Destroying fbdev output.\n");
630
631 /* Close the frame buffer. */
632 fbdev_output_disable(base);
633
634 if (base->renderer_state != NULL)
635 pixman_renderer_output_destroy(base);
636
637 if (output->shadow_surface != NULL) {
638 pixman_image_unref(output->shadow_surface);
639 output->shadow_surface = NULL;
640 }
641
642 if (output->shadow_buf != NULL) {
643 free(output->shadow_buf);
644 output->shadow_buf = NULL;
645 }
646
647 /* Remove the output. */
648 wl_list_remove(&output->base.link);
649 weston_output_destroy(&output->base);
650
651 free(output);
652}
653
654/* strcmp()-style return values. */
655static int
656compare_screen_info (const struct fbdev_screeninfo *a,
657 const struct fbdev_screeninfo *b)
658{
659 if (a->x_resolution == b->x_resolution &&
660 a->y_resolution == b->y_resolution &&
661 a->width_mm == b->width_mm &&
662 a->height_mm == b->height_mm &&
663 a->bits_per_pixel == b->bits_per_pixel &&
664 a->pixel_format == b->pixel_format &&
665 a->refresh_rate == b->refresh_rate)
666 return 0;
667
668 return 1;
669}
670
671static int
672fbdev_output_reenable(struct fbdev_compositor *compositor,
673 struct weston_output *base)
674{
675 struct fbdev_output *output = to_fbdev_output(base);
676 struct fbdev_screeninfo new_screen_info;
677 int fb_fd;
678
679 weston_log("Re-enabling fbdev output.\n");
680
681 /* Create the frame buffer. */
682 fb_fd = fbdev_frame_buffer_open(output, output->device,
683 &new_screen_info);
684 if (fb_fd < 0) {
685 weston_log("Creating frame buffer failed.\n");
686 goto err;
687 }
688
689 /* Check whether the frame buffer details have changed since we were
690 * disabled. */
691 if (compare_screen_info (&output->fb_info, &new_screen_info) != 0) {
692 /* Perform a mode-set to restore the old mode. */
693 if (fbdev_set_screen_info(output, fb_fd,
694 &output->fb_info) < 0) {
695 weston_log("Failed to restore mode settings. "
696 "Attempting to re-open output anyway.\n");
697 }
698
699 /* Remove and re-add the output so that resources depending on
700 * the frame buffer X/Y resolution (such as the shadow buffer)
701 * are re-initialised. */
702 fbdev_output_destroy(base);
703 fbdev_output_create(compositor, output->device);
704
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
739fbdev_led_update(struct weston_seat *seat_base, enum weston_led leds)
740{
741 struct fbdev_seat *seat = to_fbdev_seat(seat_base);
742 struct evdev_device *device;
743
744 wl_list_for_each(device, &seat->devices_list, link)
745 evdev_led_update(device, leds);
746}
747
748static const char default_seat[] = "seat0";
749
750static void
751device_added(struct udev_device *udev_device, struct fbdev_seat *master)
752{
753 struct evdev_device *device;
754 const char *devnode;
755 const char *device_seat;
756 int fd;
757
758 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
759 if (!device_seat)
760 device_seat = default_seat;
761
762 if (strcmp(device_seat, master->seat_id))
763 return;
764
765 devnode = udev_device_get_devnode(udev_device);
766 if (devnode == NULL) {
767 weston_log("Getting devnode for device on seat ‘%s’ failed.\n",
768 device_seat);
769 return;
770 }
771
772 /* Use non-blocking mode so that we can loop on read on
773 * evdev_device_data() until all events on the fd are
774 * read. mtdev_get() also expects this.
775 * O_CLOEXEC is added by weston_launcher_open(). */
776 fd = weston_launcher_open(master->base.compositor, devnode,
777 O_RDWR | O_NONBLOCK);
778 if (fd < 0) {
779 weston_log("opening input device '%s' failed.\n", devnode);
780 return;
781 }
782
783 device = evdev_device_create(&master->base, devnode, fd);
784 if (!device) {
785 close(fd);
786 weston_log("not using input device '%s'.\n", devnode);
787 return;
788 }
789
790 wl_list_insert(master->devices_list.prev, &device->link);
791}
792
793static void
794evdev_add_devices(struct udev *udev, struct weston_seat *seat_base)
795{
796 struct fbdev_seat *seat = to_fbdev_seat(seat_base);
797 struct udev_enumerate *e;
798 struct udev_list_entry *entry;
799 struct udev_device *device;
800 const char *path, *sysname;
801
802 e = udev_enumerate_new(udev);
803 if (e == NULL)
804 return;
805
806 if (udev_enumerate_add_match_subsystem(e, "input") < 0 ||
807 udev_enumerate_scan_devices(e) < 0)
808 goto out_enumerate;
809
810 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
811 path = udev_list_entry_get_name(entry);
812 device = udev_device_new_from_syspath(udev, path);
813
814 if (device == NULL)
815 continue;
816
817 sysname = udev_device_get_sysname(device);
818 if (strncmp("event", sysname, 5) != 0) {
819 udev_device_unref(device);
820 continue;
821 }
822
823 device_added(device, seat);
824
825 udev_device_unref(device);
826 }
827 udev_enumerate_unref(e);
828
829 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
830
831 if (wl_list_empty(&seat->devices_list)) {
832 weston_log(
833 "warning: no input devices on entering Weston. "
834 "Possible causes:\n"
835 "\t- no permissions to read /dev/input/event*\n"
836 "\t- seats misconfigured "
837 "(Weston backend option 'seat', "
838 "udev device property ID_SEAT)\n");
839 }
840
841 return;
842
843out_enumerate:
844 udev_enumerate_unref(e);
845
846 weston_log("Failed to enumerate and add evdev devices.\n");
847}
848
849static int
850evdev_udev_handler(int fd, uint32_t mask, void *data)
851{
852 struct fbdev_seat *seat = data;
853 struct udev_device *udev_device;
854 struct evdev_device *device, *next;
855 const char *action;
856 const char *devnode;
857
858 udev_device = udev_monitor_receive_device(seat->udev_monitor);
859 if (!udev_device)
860 return 1;
861
862 action = udev_device_get_action(udev_device);
863 if (!action)
864 goto out;
865
866 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
867 goto out;
868
869 if (!strcmp(action, "add")) {
870 device_added(udev_device, seat);
871 } else if (!strcmp(action, "remove")) {
872 devnode = udev_device_get_devnode(udev_device);
873 wl_list_for_each_safe(device, next, &seat->devices_list, link)
874 if (!strcmp(device->devnode, devnode)) {
875 weston_log("input device %s, %s removed\n",
876 device->devname, device->devnode);
877 evdev_device_destroy(device);
878 break;
879 }
880 }
881
882out:
883 udev_device_unref(udev_device);
884
885 return 0;
886}
887
888static int
889evdev_enable_udev_monitor(struct udev *udev, struct weston_seat *seat_base)
890{
891 struct fbdev_seat *master = to_fbdev_seat(seat_base);
892 struct wl_event_loop *loop;
893 struct weston_compositor *c = master->base.compositor;
894 int fd;
895
896 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
897 if (!master->udev_monitor) {
898 weston_log("udev: failed to create the udev monitor\n");
899 goto out;
900 }
901
902 if (udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
903 "input",
904 NULL) < 0) {
905 weston_log("udev: failed to add filter\n");
906 goto out_monitor;
907 }
908
909 if (udev_monitor_enable_receiving(master->udev_monitor)) {
910 weston_log("udev: failed to bind the udev monitor\n");
911 goto out_monitor;
912 }
913
914 loop = wl_display_get_event_loop(c->wl_display);
915 fd = udev_monitor_get_fd(master->udev_monitor);
916 master->udev_monitor_source =
917 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
918 evdev_udev_handler, master);
919 if (!master->udev_monitor_source)
920 goto out_monitor;
921
922 return 1;
923
924out_monitor:
925 udev_monitor_unref(master->udev_monitor);
926out:
927 return 0;
928}
929
930static void
931evdev_disable_udev_monitor(struct weston_seat *seat_base)
932{
933 struct fbdev_seat *seat = to_fbdev_seat(seat_base);
934
935 if (!seat->udev_monitor)
936 return;
937
938 udev_monitor_unref(seat->udev_monitor);
939 seat->udev_monitor = NULL;
940 wl_event_source_remove(seat->udev_monitor_source);
941 seat->udev_monitor_source = NULL;
942}
943
944static void
945evdev_input_create(struct weston_compositor *c, struct udev *udev,
946 const char *seat_id)
947{
948 struct fbdev_seat *seat;
949
950 seat = malloc(sizeof *seat);
951 if (seat == NULL)
952 return;
953
954 memset(seat, 0, sizeof *seat);
955 weston_seat_init(&seat->base, c);
956 seat->base.led_update = fbdev_led_update;
957
958 wl_list_init(&seat->devices_list);
959 seat->seat_id = strdup(seat_id);
960 if (seat->seat_id == NULL ||
961 !evdev_enable_udev_monitor(udev, &seat->base)) {
962 free(seat->seat_id);
963 free(seat);
964 return;
965 }
966
967 evdev_add_devices(udev, &seat->base);
968}
969
970static void
971evdev_remove_devices(struct weston_seat *seat_base)
972{
973 struct fbdev_seat *seat = to_fbdev_seat(seat_base);
974 struct evdev_device *device, *next;
975
976 wl_list_for_each_safe(device, next, &seat->devices_list, link)
977 evdev_device_destroy(device);
978
979 if (seat->base.seat.keyboard)
980 notify_keyboard_focus_out(&seat->base);
981}
982
983static void
984evdev_input_destroy(struct weston_seat *seat_base)
985{
986 struct fbdev_seat *seat = to_fbdev_seat(seat_base);
987
988 evdev_remove_devices(seat_base);
989 evdev_disable_udev_monitor(&seat->base);
990
991 weston_seat_release(seat_base);
992 free(seat->seat_id);
993 free(seat);
994}
995
996static void
997fbdev_compositor_destroy(struct weston_compositor *base)
998{
999 struct fbdev_compositor *compositor = to_fbdev_compositor(base);
1000 struct weston_seat *seat, *next;
1001
1002 /* Destroy all inputs. */
1003 wl_list_for_each_safe(seat, next, &compositor->base.seat_list, link)
1004 evdev_input_destroy(seat);
1005
1006 /* Destroy the output. */
1007 weston_compositor_shutdown(&compositor->base);
1008
1009 /* Chain up. */
1010 compositor->base.renderer->destroy(&compositor->base);
1011 tty_destroy(compositor->tty);
1012
1013 free(compositor);
1014}
1015
1016static void
1017vt_func(struct weston_compositor *base, int event)
1018{
1019 struct fbdev_compositor *compositor = to_fbdev_compositor(base);
1020 struct weston_seat *seat;
1021 struct weston_output *output;
1022
1023 switch (event) {
1024 case TTY_ENTER_VT:
1025 weston_log("entering VT\n");
1026 compositor->base.focus = 1;
1027 compositor->base.state = compositor->prev_state;
1028
1029 wl_list_for_each(output, &compositor->base.output_list, link) {
1030 fbdev_output_reenable(compositor, output);
1031 }
1032
1033 weston_compositor_damage_all(&compositor->base);
1034
1035 wl_list_for_each(seat, &compositor->base.seat_list, link) {
1036 evdev_add_devices(compositor->udev, seat);
1037 evdev_enable_udev_monitor(compositor->udev, seat);
1038 }
1039 break;
1040 case TTY_LEAVE_VT:
1041 weston_log("leaving VT\n");
1042 wl_list_for_each(seat, &compositor->base.seat_list, link) {
1043 evdev_disable_udev_monitor(seat);
1044 evdev_remove_devices(seat);
1045 }
1046
1047 wl_list_for_each(output, &compositor->base.output_list, link) {
1048 fbdev_output_disable(output);
1049 }
1050
1051 compositor->base.focus = 0;
1052 compositor->prev_state = compositor->base.state;
1053 compositor->base.state = WESTON_COMPOSITOR_SLEEPING;
1054
1055 /* If we have a repaint scheduled (from the idle handler), make
1056 * sure we cancel that so we don't try to pageflip when we're
1057 * vt switched away. The SLEEPING state will prevent
1058 * further attemps at repainting. When we switch
1059 * back, we schedule a repaint, which will process
1060 * pending frame callbacks. */
1061
1062 wl_list_for_each(output,
1063 &compositor->base.output_list, link) {
1064 output->repaint_needed = 0;
1065 }
1066
1067 break;
1068 };
1069}
1070
1071static void
1072fbdev_restore(struct weston_compositor *base)
1073{
1074 struct fbdev_compositor *compositor = to_fbdev_compositor(base);
1075
1076 tty_reset(compositor->tty);
1077}
1078
1079static void
1080switch_vt_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
1081{
1082 struct fbdev_compositor *ec = data;
1083
1084 tty_activate_vt(ec->tty, key - KEY_F1 + 1);
1085}
1086
1087static struct weston_compositor *
1088fbdev_compositor_create(struct wl_display *display, int argc, char *argv[],
1089 const char *config_file, struct fbdev_parameters *param)
1090{
1091 struct fbdev_compositor *compositor;
1092 const char *seat = default_seat;
1093 uint32_t key;
1094
1095 weston_log("initializing fbdev backend\n");
1096
1097 compositor = calloc(1, sizeof *compositor);
1098 if (compositor == NULL)
1099 return NULL;
1100
1101 if (weston_compositor_init(&compositor->base, display, argc, argv,
1102 config_file) < 0)
1103 goto out_free;
1104
1105 compositor->udev = udev_new();
1106 if (compositor->udev == NULL) {
1107 weston_log("Failed to initialize udev context.\n");
1108 goto out_compositor;
1109 }
1110
1111 /* Set up the TTY. */
1112 compositor->tty = tty_create(&compositor->base, vt_func, param->tty);
1113 if (!compositor->tty) {
1114 weston_log("Failed to initialize tty.\n");
1115 goto out_udev;
1116 }
1117
1118 compositor->base.destroy = fbdev_compositor_destroy;
1119 compositor->base.restore = fbdev_restore;
1120
1121 compositor->base.focus = 1;
1122 compositor->prev_state = WESTON_COMPOSITOR_ACTIVE;
1123
1124 for (key = KEY_F1; key < KEY_F9; key++)
1125 weston_compositor_add_key_binding(&compositor->base, key,
1126 MODIFIER_CTRL | MODIFIER_ALT,
1127 switch_vt_binding,
1128 compositor);
1129
1130 if (pixman_renderer_init(&compositor->base) < 0)
1131 goto out_tty;
1132
1133 if (fbdev_output_create(compositor, param->device) < 0)
1134 goto out_pixman;
1135
1136 evdev_input_create(&compositor->base, compositor->udev, seat);
1137
1138 return &compositor->base;
1139
1140out_pixman:
1141 compositor->base.renderer->destroy(&compositor->base);
1142
1143out_tty:
1144 tty_destroy(compositor->tty);
1145
1146out_udev:
1147 udev_unref(compositor->udev);
1148
1149out_compositor:
1150 weston_compositor_shutdown(&compositor->base);
1151
1152out_free:
1153 free(compositor);
1154
1155 return NULL;
1156}
1157
1158WL_EXPORT struct weston_compositor *
1159backend_init(struct wl_display *display, int argc, char *argv[],
1160 const char *config_file)
1161{
1162 /* TODO: Ideally, available frame buffers should be enumerated using
1163 * udev, rather than passing a device node in as a parameter. */
1164 struct fbdev_parameters param = {
1165 .tty = 0, /* default to current tty */
1166 .device = "/dev/fb0", /* default frame buffer */
1167 };
1168
1169 const struct weston_option fbdev_options[] = {
1170 { WESTON_OPTION_INTEGER, "tty", 0, &param.tty },
1171 { WESTON_OPTION_STRING, "device", 0, &param.device },
1172 };
1173
1174 parse_options(fbdev_options, ARRAY_LENGTH(fbdev_options), argc, argv);
1175
1176 return fbdev_compositor_create(display, argc, argv, config_file,
1177 &param);
1178}