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