blob: 992eadb8abedcae72061d132313e18536470001e [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 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07007 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
Philip Withnall4f499172013-02-02 12:02:32 +000014 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070015 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial
17 * portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
Philip Withnall4f499172013-02-02 12:02:32 +000027 */
28
Daniel Stonec228e232013-05-22 18:03:19 +030029#include "config.h"
Philip Withnall4f499172013-02-02 12:02:32 +000030
31#include <errno.h>
32#include <stdlib.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030033#include <stdint.h>
Philip Withnall4f499172013-02-02 12:02:32 +000034#include <stdio.h>
35#include <string.h>
36#include <math.h>
37#include <sys/mman.h>
38#include <sys/types.h>
39#include <fcntl.h>
40#include <unistd.h>
41#include <linux/fb.h>
Kristian Høgsberg7e597f22013-02-18 16:35:26 -050042#include <linux/input.h>
Philip Withnall4f499172013-02-02 12:02:32 +000043
44#include <libudev.h>
45
Jon Cruz35b2eaa2015-06-15 15:37:08 -070046#include "shared/helpers.h"
Philip Withnall4f499172013-02-02 12:02:32 +000047#include "compositor.h"
Benoit Gschwind934e89a2016-04-27 23:56:42 +020048#include "compositor-fbdev.h"
Philip Withnall4f499172013-02-02 12:02:32 +000049#include "launcher-util.h"
50#include "pixman-renderer.h"
Peter Hutterer823ad332014-11-26 07:06:31 +100051#include "libinput-seat.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020052#include "presentation-time-server-protocol.h"
Philip Withnall4f499172013-02-02 12:02:32 +000053
Giulio Camuffo954f1832014-10-11 18:27:30 +030054struct fbdev_backend {
55 struct weston_backend base;
56 struct weston_compositor *compositor;
Philip Withnall4f499172013-02-02 12:02:32 +000057 uint32_t prev_state;
58
59 struct udev *udev;
Rob Bradfordd355b802013-05-31 18:09:55 +010060 struct udev_input input;
Benoit Gschwind934e89a2016-04-27 23:56:42 +020061 uint32_t output_transform;
Kristian Høgsberg61741a22013-09-17 16:02:57 -070062 struct wl_listener session_listener;
Philip Withnall4f499172013-02-02 12:02:32 +000063};
64
65struct fbdev_screeninfo {
66 unsigned int x_resolution; /* pixels, visible area */
67 unsigned int y_resolution; /* pixels, visible area */
68 unsigned int width_mm; /* visible screen width in mm */
69 unsigned int height_mm; /* visible screen height in mm */
70 unsigned int bits_per_pixel;
71
72 size_t buffer_length; /* length of frame buffer memory in bytes */
73 size_t line_length; /* length of a line in bytes */
74 char id[16]; /* screen identifier */
75
76 pixman_format_code_t pixel_format; /* frame buffer pixel format */
77 unsigned int refresh_rate; /* Hertz */
78};
79
80struct fbdev_output {
Giulio Camuffo954f1832014-10-11 18:27:30 +030081 struct fbdev_backend *backend;
Philip Withnall4f499172013-02-02 12:02:32 +000082 struct weston_output base;
83
84 struct weston_mode mode;
85 struct wl_event_source *finish_frame_timer;
86
87 /* Frame buffer details. */
Pekka Paalanene5b56592016-04-28 15:12:25 +030088 char *device;
Philip Withnall4f499172013-02-02 12:02:32 +000089 struct fbdev_screeninfo fb_info;
90 void *fb; /* length is fb_info.buffer_length */
91
92 /* pixman details. */
93 pixman_image_t *hw_surface;
Philip Withnall4f499172013-02-02 12:02:32 +000094};
95
Kristian Høgsberg7e597f22013-02-18 16:35:26 -050096static const char default_seat[] = "seat0";
97
Philip Withnall4f499172013-02-02 12:02:32 +000098static inline struct fbdev_output *
99to_fbdev_output(struct weston_output *base)
100{
101 return container_of(base, struct fbdev_output, base);
102}
103
Giulio Camuffo954f1832014-10-11 18:27:30 +0300104static inline struct fbdev_backend *
105to_fbdev_backend(struct weston_compositor *base)
Philip Withnall4f499172013-02-02 12:02:32 +0000106{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300107 return container_of(base->backend, struct fbdev_backend, base);
Philip Withnall4f499172013-02-02 12:02:32 +0000108}
109
110static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200111fbdev_output_start_repaint_loop(struct weston_output *output)
112{
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400113 struct timespec ts;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200114
Pekka Paalanen662f3842015-03-18 12:17:26 +0200115 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200116 weston_output_finish_frame(output, &ts, WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200117}
118
Pekka Paalanene77f8ad2016-06-08 17:39:37 +0300119static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +0000120fbdev_output_repaint(struct weston_output *base, pixman_region32_t *damage,
121 void *repaint_data)
Philip Withnall4f499172013-02-02 12:02:32 +0000122{
123 struct fbdev_output *output = to_fbdev_output(base);
124 struct weston_compositor *ec = output->base.compositor;
Philip Withnall4f499172013-02-02 12:02:32 +0000125
126 /* Repaint the damaged region onto the back buffer. */
Sjoerd Simonsc112e0c2015-12-04 19:20:12 -0600127 pixman_renderer_output_set_buffer(base, output->hw_surface);
Philip Withnall4f499172013-02-02 12:02:32 +0000128 ec->renderer->repaint_output(base, damage);
129
Philip Withnall4f499172013-02-02 12:02:32 +0000130 /* Update the damage region. */
131 pixman_region32_subtract(&ec->primary_plane.damage,
132 &ec->primary_plane.damage, damage);
133
134 /* Schedule the end of the frame. We do not sync this to the frame
135 * buffer clock because users who want that should be using the DRM
136 * compositor. FBIO_WAITFORVSYNC blocks and FB_ACTIVATE_VBL requires
137 * panning, which is broken in most kernel drivers.
138 *
139 * Finish the frame synchronised to the specified refresh rate. The
140 * refresh rate is given in mHz and the interval in ms. */
141 wl_event_source_timer_update(output->finish_frame_timer,
142 1000000 / output->mode.refresh);
David Herrmann1edf44c2013-10-22 17:11:26 +0200143
144 return 0;
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300145}
146
Philip Withnall4f499172013-02-02 12:02:32 +0000147static int
148finish_frame_handler(void *data)
149{
150 struct fbdev_output *output = data;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200151 struct timespec ts;
Philip Withnall4f499172013-02-02 12:02:32 +0000152
Pekka Paalanen662f3842015-03-18 12:17:26 +0200153 weston_compositor_read_presentation_clock(output->base.compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200154 weston_output_finish_frame(&output->base, &ts, 0);
Philip Withnall4f499172013-02-02 12:02:32 +0000155
156 return 1;
157}
158
159static pixman_format_code_t
160calculate_pixman_format(struct fb_var_screeninfo *vinfo,
161 struct fb_fix_screeninfo *finfo)
162{
163 /* Calculate the pixman format supported by the frame buffer from the
164 * buffer's metadata. Return 0 if no known pixman format is supported
165 * (since this has depth 0 it's guaranteed to not conflict with any
166 * actual pixman format).
167 *
168 * Documentation on the vinfo and finfo structures:
169 * http://www.mjmwired.net/kernel/Documentation/fb/api.txt
170 *
171 * TODO: Try a bit harder to support other formats, including setting
172 * the preferred format in the hardware. */
173 int type;
174
175 weston_log("Calculating pixman format from:\n"
176 STAMP_SPACE " - type: %i (aux: %i)\n"
177 STAMP_SPACE " - visual: %i\n"
178 STAMP_SPACE " - bpp: %i (grayscale: %i)\n"
179 STAMP_SPACE " - red: offset: %i, length: %i, MSB: %i\n"
180 STAMP_SPACE " - green: offset: %i, length: %i, MSB: %i\n"
181 STAMP_SPACE " - blue: offset: %i, length: %i, MSB: %i\n"
182 STAMP_SPACE " - transp: offset: %i, length: %i, MSB: %i\n",
183 finfo->type, finfo->type_aux, finfo->visual,
184 vinfo->bits_per_pixel, vinfo->grayscale,
185 vinfo->red.offset, vinfo->red.length, vinfo->red.msb_right,
186 vinfo->green.offset, vinfo->green.length,
187 vinfo->green.msb_right,
188 vinfo->blue.offset, vinfo->blue.length,
189 vinfo->blue.msb_right,
190 vinfo->transp.offset, vinfo->transp.length,
191 vinfo->transp.msb_right);
192
193 /* We only handle packed formats at the moment. */
194 if (finfo->type != FB_TYPE_PACKED_PIXELS)
195 return 0;
196
197 /* We only handle true-colour frame buffers at the moment. */
Marc Chalainffbddff2013-09-03 16:47:43 +0200198 switch(finfo->visual) {
199 case FB_VISUAL_TRUECOLOR:
200 case FB_VISUAL_DIRECTCOLOR:
201 if (vinfo->grayscale != 0)
202 return 0;
203 break;
204 default:
205 return 0;
206 }
Philip Withnall4f499172013-02-02 12:02:32 +0000207
208 /* We only support formats with MSBs on the left. */
209 if (vinfo->red.msb_right != 0 || vinfo->green.msb_right != 0 ||
210 vinfo->blue.msb_right != 0)
211 return 0;
212
213 /* Work out the format type from the offsets. We only support RGBA and
214 * ARGB at the moment. */
215 type = PIXMAN_TYPE_OTHER;
216
217 if ((vinfo->transp.offset >= vinfo->red.offset ||
218 vinfo->transp.length == 0) &&
219 vinfo->red.offset >= vinfo->green.offset &&
220 vinfo->green.offset >= vinfo->blue.offset)
221 type = PIXMAN_TYPE_ARGB;
222 else if (vinfo->red.offset >= vinfo->green.offset &&
223 vinfo->green.offset >= vinfo->blue.offset &&
224 vinfo->blue.offset >= vinfo->transp.offset)
225 type = PIXMAN_TYPE_RGBA;
226
227 if (type == PIXMAN_TYPE_OTHER)
228 return 0;
229
230 /* Build the format. */
231 return PIXMAN_FORMAT(vinfo->bits_per_pixel, type,
232 vinfo->transp.length,
233 vinfo->red.length,
234 vinfo->green.length,
235 vinfo->blue.length);
236}
237
238static int
239calculate_refresh_rate(struct fb_var_screeninfo *vinfo)
240{
241 uint64_t quot;
242
243 /* Calculate monitor refresh rate. Default is 60 Hz. Units are mHz. */
244 quot = (vinfo->upper_margin + vinfo->lower_margin + vinfo->yres);
245 quot *= (vinfo->left_margin + vinfo->right_margin + vinfo->xres);
246 quot *= vinfo->pixclock;
247
248 if (quot > 0) {
249 uint64_t refresh_rate;
250
251 refresh_rate = 1000000000000000LLU / quot;
252 if (refresh_rate > 200000)
253 refresh_rate = 200000; /* cap at 200 Hz */
254
Oliver Smitha5066e02017-04-17 11:11:00 +0000255 if (refresh_rate >= 1000) /* at least 1 Hz */
256 return refresh_rate;
Philip Withnall4f499172013-02-02 12:02:32 +0000257 }
258
259 return 60 * 1000; /* default to 60 Hz */
260}
261
262static int
263fbdev_query_screen_info(struct fbdev_output *output, int fd,
264 struct fbdev_screeninfo *info)
265{
266 struct fb_var_screeninfo varinfo;
267 struct fb_fix_screeninfo fixinfo;
268
269 /* Probe the device for screen information. */
270 if (ioctl(fd, FBIOGET_FSCREENINFO, &fixinfo) < 0 ||
271 ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
272 return -1;
273 }
274
275 /* Store the pertinent data. */
276 info->x_resolution = varinfo.xres;
277 info->y_resolution = varinfo.yres;
278 info->width_mm = varinfo.width;
279 info->height_mm = varinfo.height;
280 info->bits_per_pixel = varinfo.bits_per_pixel;
281
282 info->buffer_length = fixinfo.smem_len;
283 info->line_length = fixinfo.line_length;
Derek Foreman61793382015-09-02 13:45:20 -0500284 strncpy(info->id, fixinfo.id, sizeof(info->id));
Bryce Harrington44bbdd02015-09-23 17:39:05 -0700285 info->id[sizeof(info->id)-1] = '\0';
Philip Withnall4f499172013-02-02 12:02:32 +0000286
287 info->pixel_format = calculate_pixman_format(&varinfo, &fixinfo);
288 info->refresh_rate = calculate_refresh_rate(&varinfo);
289
290 if (info->pixel_format == 0) {
291 weston_log("Frame buffer uses an unsupported format.\n");
292 return -1;
293 }
294
295 return 1;
296}
297
298static int
299fbdev_set_screen_info(struct fbdev_output *output, int fd,
300 struct fbdev_screeninfo *info)
301{
302 struct fb_var_screeninfo varinfo;
303
304 /* Grab the current screen information. */
305 if (ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
306 return -1;
307 }
308
309 /* Update the information. */
310 varinfo.xres = info->x_resolution;
311 varinfo.yres = info->y_resolution;
312 varinfo.width = info->width_mm;
313 varinfo.height = info->height_mm;
314 varinfo.bits_per_pixel = info->bits_per_pixel;
315
316 /* Try to set up an ARGB (x8r8g8b8) pixel format. */
317 varinfo.grayscale = 0;
318 varinfo.transp.offset = 24;
319 varinfo.transp.length = 0;
320 varinfo.transp.msb_right = 0;
321 varinfo.red.offset = 16;
322 varinfo.red.length = 8;
323 varinfo.red.msb_right = 0;
324 varinfo.green.offset = 8;
325 varinfo.green.length = 8;
326 varinfo.green.msb_right = 0;
327 varinfo.blue.offset = 0;
328 varinfo.blue.length = 8;
329 varinfo.blue.msb_right = 0;
330
331 /* Set the device's screen information. */
332 if (ioctl(fd, FBIOPUT_VSCREENINFO, &varinfo) < 0) {
333 return -1;
334 }
335
336 return 1;
337}
338
339static void fbdev_frame_buffer_destroy(struct fbdev_output *output);
340
341/* Returns an FD for the frame buffer device. */
342static int
343fbdev_frame_buffer_open(struct fbdev_output *output, const char *fb_dev,
344 struct fbdev_screeninfo *screen_info)
345{
346 int fd = -1;
347
348 weston_log("Opening fbdev frame buffer.\n");
349
350 /* Open the frame buffer device. */
351 fd = open(fb_dev, O_RDWR | O_CLOEXEC);
352 if (fd < 0) {
353 weston_log("Failed to open frame buffer device ‘%s’: %s\n",
354 fb_dev, strerror(errno));
355 return -1;
356 }
357
358 /* Grab the screen info. */
359 if (fbdev_query_screen_info(output, fd, screen_info) < 0) {
360 weston_log("Failed to get frame buffer info: %s\n",
361 strerror(errno));
362
363 close(fd);
364 return -1;
365 }
366
367 return fd;
368}
369
370/* Closes the FD on success or failure. */
371static int
372fbdev_frame_buffer_map(struct fbdev_output *output, int fd)
373{
374 int retval = -1;
375
376 weston_log("Mapping fbdev frame buffer.\n");
377
378 /* Map the frame buffer. Write-only mode, since we don't want to read
379 * anything back (because it's slow). */
380 output->fb = mmap(NULL, output->fb_info.buffer_length,
381 PROT_WRITE, MAP_SHARED, fd, 0);
382 if (output->fb == MAP_FAILED) {
383 weston_log("Failed to mmap frame buffer: %s\n",
384 strerror(errno));
Pekka Paalanen82b8ddf2017-09-13 11:58:00 +0300385 output->fb = NULL;
Philip Withnall4f499172013-02-02 12:02:32 +0000386 goto out_close;
387 }
388
389 /* Create a pixman image to wrap the memory mapped frame buffer. */
390 output->hw_surface =
391 pixman_image_create_bits(output->fb_info.pixel_format,
392 output->fb_info.x_resolution,
393 output->fb_info.y_resolution,
394 output->fb,
395 output->fb_info.line_length);
396 if (output->hw_surface == NULL) {
397 weston_log("Failed to create surface for frame buffer.\n");
398 goto out_unmap;
399 }
400
401 /* Success! */
402 retval = 0;
403
404out_unmap:
405 if (retval != 0 && output->fb != NULL)
406 fbdev_frame_buffer_destroy(output);
407
408out_close:
409 if (fd >= 0)
410 close(fd);
411
412 return retval;
413}
414
415static void
416fbdev_frame_buffer_destroy(struct fbdev_output *output)
417{
418 weston_log("Destroying fbdev frame buffer.\n");
419
420 if (munmap(output->fb, output->fb_info.buffer_length) < 0)
421 weston_log("Failed to munmap frame buffer: %s\n",
422 strerror(errno));
423
424 output->fb = NULL;
425}
426
427static void fbdev_output_destroy(struct weston_output *base);
428static void fbdev_output_disable(struct weston_output *base);
429
430static int
Armin Krezović6ba369d2016-09-30 14:11:06 +0200431fbdev_output_enable(struct weston_output *base)
432{
433 struct fbdev_output *output = to_fbdev_output(base);
434 struct fbdev_backend *backend = to_fbdev_backend(base->compositor);
435 int fb_fd;
436 struct wl_event_loop *loop;
437
438 /* Create the frame buffer. */
439 fb_fd = fbdev_frame_buffer_open(output, output->device, &output->fb_info);
440 if (fb_fd < 0) {
441 weston_log("Creating frame buffer failed.\n");
442 return -1;
443 }
444
445 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
446 weston_log("Mapping frame buffer failed.\n");
447 return -1;
448 }
449
450 output->base.start_repaint_loop = fbdev_output_start_repaint_loop;
451 output->base.repaint = fbdev_output_repaint;
452
453 if (pixman_renderer_output_create(&output->base) < 0)
454 goto out_hw_surface;
455
456 loop = wl_display_get_event_loop(backend->compositor->wl_display);
457 output->finish_frame_timer =
458 wl_event_loop_add_timer(loop, finish_frame_handler, output);
459
460 weston_log("fbdev output %d×%d px\n",
461 output->mode.width, output->mode.height);
462 weston_log_continue(STAMP_SPACE "guessing %d Hz and 96 dpi\n",
463 output->mode.refresh / 1000);
464
465 return 0;
466
467out_hw_surface:
468 pixman_image_unref(output->hw_surface);
469 output->hw_surface = NULL;
470 fbdev_frame_buffer_destroy(output);
471
472 return -1;
473}
474
475static int
Pekka Paalanenacd71fb2017-08-15 10:35:09 +0300476fbdev_output_disable_handler(struct weston_output *base)
477{
478 if (!base->enabled)
479 return 0;
480
481 /* Close the frame buffer. */
482 fbdev_output_disable(base);
483
484 if (base->renderer_state != NULL)
485 pixman_renderer_output_destroy(base);
486
487 return 0;
488}
489
490static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300491fbdev_output_create(struct fbdev_backend *backend,
Philip Withnall4f499172013-02-02 12:02:32 +0000492 const char *device)
493{
494 struct fbdev_output *output;
Philip Withnall4f499172013-02-02 12:02:32 +0000495 int fb_fd;
Philip Withnall4f499172013-02-02 12:02:32 +0000496
497 weston_log("Creating fbdev output.\n");
498
Bryce Harringtonde16d892014-11-20 22:21:57 -0800499 output = zalloc(sizeof *output);
500 if (output == NULL)
Philip Withnall4f499172013-02-02 12:02:32 +0000501 return -1;
502
Giulio Camuffo954f1832014-10-11 18:27:30 +0300503 output->backend = backend;
Pekka Paalanene5b56592016-04-28 15:12:25 +0300504 output->device = strdup(device);
Philip Withnall4f499172013-02-02 12:02:32 +0000505
506 /* Create the frame buffer. */
507 fb_fd = fbdev_frame_buffer_open(output, device, &output->fb_info);
508 if (fb_fd < 0) {
509 weston_log("Creating frame buffer failed.\n");
510 goto out_free;
511 }
Pekka Paalanene77f8ad2016-06-08 17:39:37 +0300512
Pekka Paalanen26ac2e12017-04-03 13:18:13 +0300513 weston_output_init(&output->base, backend->compositor, "fbdev");
514
Philip Withnall4f499172013-02-02 12:02:32 +0000515 output->base.destroy = fbdev_output_destroy;
Pekka Paalanenacd71fb2017-08-15 10:35:09 +0300516 output->base.disable = fbdev_output_disable_handler;
Armin Krezović6ba369d2016-09-30 14:11:06 +0200517 output->base.enable = fbdev_output_enable;
518
Philip Withnall4f499172013-02-02 12:02:32 +0000519 /* only one static mode in list */
520 output->mode.flags =
521 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
522 output->mode.width = output->fb_info.x_resolution;
523 output->mode.height = output->fb_info.y_resolution;
524 output->mode.refresh = output->fb_info.refresh_rate;
Philip Withnall4f499172013-02-02 12:02:32 +0000525 wl_list_insert(&output->base.mode_list, &output->mode.link);
526
Hardeningff39efa2013-09-18 23:56:35 +0200527 output->base.current_mode = &output->mode;
Philip Withnall4f499172013-02-02 12:02:32 +0000528 output->base.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
529 output->base.make = "unknown";
530 output->base.model = output->fb_info.id;
531
Armin Krezović6ba369d2016-09-30 14:11:06 +0200532 output->base.mm_width = output->fb_info.width_mm;
533 output->base.mm_height = output->fb_info.height_mm;
Philip Withnall4f499172013-02-02 12:02:32 +0000534
Armin Krezović6ba369d2016-09-30 14:11:06 +0200535 close(fb_fd);
Adrian Negreanu4aa756d2013-09-06 15:16:09 +0300536
Armin Krezović6ba369d2016-09-30 14:11:06 +0200537 weston_compositor_add_pending_output(&output->base, backend->compositor);
Philip Withnall4f499172013-02-02 12:02:32 +0000538
539 return 0;
540
Philip Withnall4f499172013-02-02 12:02:32 +0000541out_free:
Pekka Paalanene5b56592016-04-28 15:12:25 +0300542 free(output->device);
Philip Withnall4f499172013-02-02 12:02:32 +0000543 free(output);
544
545 return -1;
546}
547
548static void
549fbdev_output_destroy(struct weston_output *base)
550{
551 struct fbdev_output *output = to_fbdev_output(base);
552
553 weston_log("Destroying fbdev output.\n");
554
Pekka Paalanenacd71fb2017-08-15 10:35:09 +0300555 fbdev_output_disable_handler(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000556
557 /* Remove the output. */
Pekka Paalanenae6d35d2017-08-16 12:07:14 +0300558 weston_output_release(&output->base);
Philip Withnall4f499172013-02-02 12:02:32 +0000559
Pekka Paalanene5b56592016-04-28 15:12:25 +0300560 free(output->device);
Philip Withnall4f499172013-02-02 12:02:32 +0000561 free(output);
562}
563
564/* strcmp()-style return values. */
565static int
566compare_screen_info (const struct fbdev_screeninfo *a,
567 const struct fbdev_screeninfo *b)
568{
569 if (a->x_resolution == b->x_resolution &&
570 a->y_resolution == b->y_resolution &&
571 a->width_mm == b->width_mm &&
572 a->height_mm == b->height_mm &&
573 a->bits_per_pixel == b->bits_per_pixel &&
574 a->pixel_format == b->pixel_format &&
575 a->refresh_rate == b->refresh_rate)
576 return 0;
577
578 return 1;
579}
580
581static int
Giulio Camuffo954f1832014-10-11 18:27:30 +0300582fbdev_output_reenable(struct fbdev_backend *backend,
Philip Withnall4f499172013-02-02 12:02:32 +0000583 struct weston_output *base)
584{
585 struct fbdev_output *output = to_fbdev_output(base);
586 struct fbdev_screeninfo new_screen_info;
587 int fb_fd;
Pekka Paalanene5b56592016-04-28 15:12:25 +0300588 char *device;
Philip Withnall4f499172013-02-02 12:02:32 +0000589
590 weston_log("Re-enabling fbdev output.\n");
591
592 /* Create the frame buffer. */
593 fb_fd = fbdev_frame_buffer_open(output, output->device,
594 &new_screen_info);
595 if (fb_fd < 0) {
596 weston_log("Creating frame buffer failed.\n");
597 goto err;
598 }
599
600 /* Check whether the frame buffer details have changed since we were
601 * disabled. */
602 if (compare_screen_info (&output->fb_info, &new_screen_info) != 0) {
603 /* Perform a mode-set to restore the old mode. */
604 if (fbdev_set_screen_info(output, fb_fd,
605 &output->fb_info) < 0) {
606 weston_log("Failed to restore mode settings. "
607 "Attempting to re-open output anyway.\n");
608 }
609
Rob Bradford581b3fd2013-07-26 16:29:39 +0100610 close(fb_fd);
611
Philip Withnall4f499172013-02-02 12:02:32 +0000612 /* Remove and re-add the output so that resources depending on
613 * the frame buffer X/Y resolution (such as the shadow buffer)
614 * are re-initialised. */
Pekka Paalanene5b56592016-04-28 15:12:25 +0300615 device = strdup(output->device);
616 fbdev_output_destroy(&output->base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300617 fbdev_output_create(backend, device);
Pekka Paalanene5b56592016-04-28 15:12:25 +0300618 free(device);
Philip Withnall4f499172013-02-02 12:02:32 +0000619
620 return 0;
621 }
622
623 /* Map the device if it has the same details as before. */
Pekka Paalanene77f8ad2016-06-08 17:39:37 +0300624 if (fbdev_frame_buffer_map(output, fb_fd) < 0) {
625 weston_log("Mapping frame buffer failed.\n");
626 goto err;
Philip Withnall4f499172013-02-02 12:02:32 +0000627 }
628
629 return 0;
630
631err:
632 return -1;
633}
634
635/* NOTE: This leaves output->fb_info populated, caching data so that if
636 * fbdev_output_reenable() is called again, it can determine whether a mode-set
637 * is needed. */
638static void
639fbdev_output_disable(struct weston_output *base)
640{
641 struct fbdev_output *output = to_fbdev_output(base);
642
643 weston_log("Disabling fbdev output.\n");
644
645 if (output->hw_surface != NULL) {
646 pixman_image_unref(output->hw_surface);
647 output->hw_surface = NULL;
648 }
649
650 fbdev_frame_buffer_destroy(output);
651}
652
653static void
Giulio Camuffo954f1832014-10-11 18:27:30 +0300654fbdev_backend_destroy(struct weston_compositor *base)
Philip Withnall4f499172013-02-02 12:02:32 +0000655{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300656 struct fbdev_backend *backend = to_fbdev_backend(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000657
Giulio Camuffo954f1832014-10-11 18:27:30 +0300658 udev_input_destroy(&backend->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000659
660 /* Destroy the output. */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300661 weston_compositor_shutdown(base);
Philip Withnall4f499172013-02-02 12:02:32 +0000662
663 /* Chain up. */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300664 weston_launcher_destroy(base->launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000665
Pekka Paalanen513f9a42017-09-13 16:49:02 +0300666 udev_unref(backend->udev);
667
Giulio Camuffo954f1832014-10-11 18:27:30 +0300668 free(backend);
Philip Withnall4f499172013-02-02 12:02:32 +0000669}
670
671static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700672session_notify(struct wl_listener *listener, void *data)
Philip Withnall4f499172013-02-02 12:02:32 +0000673{
Pekka Paalanen3f897942015-08-19 13:52:47 +0300674 struct weston_compositor *compositor = data;
675 struct fbdev_backend *backend = to_fbdev_backend(compositor);
Philip Withnall4f499172013-02-02 12:02:32 +0000676 struct weston_output *output;
677
Giulio Camuffo954f1832014-10-11 18:27:30 +0300678 if (compositor->session_active) {
Philip Withnall4f499172013-02-02 12:02:32 +0000679 weston_log("entering VT\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300680 compositor->state = backend->prev_state;
Philip Withnall4f499172013-02-02 12:02:32 +0000681
Giulio Camuffo954f1832014-10-11 18:27:30 +0300682 wl_list_for_each(output, &compositor->output_list, link) {
683 fbdev_output_reenable(backend, output);
Philip Withnall4f499172013-02-02 12:02:32 +0000684 }
685
Giulio Camuffo954f1832014-10-11 18:27:30 +0300686 weston_compositor_damage_all(compositor);
Philip Withnall4f499172013-02-02 12:02:32 +0000687
Giulio Camuffo954f1832014-10-11 18:27:30 +0300688 udev_input_enable(&backend->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700689 } else {
Philip Withnall4f499172013-02-02 12:02:32 +0000690 weston_log("leaving VT\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300691 udev_input_disable(&backend->input);
Philip Withnall4f499172013-02-02 12:02:32 +0000692
Giulio Camuffo954f1832014-10-11 18:27:30 +0300693 wl_list_for_each(output, &compositor->output_list, link) {
Philip Withnall4f499172013-02-02 12:02:32 +0000694 fbdev_output_disable(output);
695 }
696
Giulio Camuffo954f1832014-10-11 18:27:30 +0300697 backend->prev_state = compositor->state;
698 weston_compositor_offscreen(compositor);
Philip Withnall4f499172013-02-02 12:02:32 +0000699
700 /* If we have a repaint scheduled (from the idle handler), make
701 * sure we cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100702 * vt switched away. The OFFSCREEN state will prevent
Bryce Harringtonc2be8572015-12-11 13:11:37 -0800703 * further attempts at repainting. When we switch
Philip Withnall4f499172013-02-02 12:02:32 +0000704 * back, we schedule a repaint, which will process
705 * pending frame callbacks. */
706
707 wl_list_for_each(output,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300708 &compositor->output_list, link) {
Daniel Stone09a97e22017-03-01 11:34:06 +0000709 output->repaint_needed = false;
Philip Withnall4f499172013-02-02 12:02:32 +0000710 }
nerdopolis38946702014-12-03 15:53:03 +0000711 }
Philip Withnall4f499172013-02-02 12:02:32 +0000712}
713
714static void
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700715fbdev_restore(struct weston_compositor *compositor)
Philip Withnall4f499172013-02-02 12:02:32 +0000716{
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700717 weston_launcher_restore(compositor->launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000718}
719
Giulio Camuffo954f1832014-10-11 18:27:30 +0300720static struct fbdev_backend *
Pekka Paalanena256c5e2016-06-03 14:56:18 +0300721fbdev_backend_create(struct weston_compositor *compositor,
Benoit Gschwind934e89a2016-04-27 23:56:42 +0200722 struct weston_fbdev_backend_config *param)
Philip Withnall4f499172013-02-02 12:02:32 +0000723{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300724 struct fbdev_backend *backend;
Rob Bradford2387fde2013-05-31 18:09:57 +0100725 const char *seat_id = default_seat;
Philip Withnall4f499172013-02-02 12:02:32 +0000726
727 weston_log("initializing fbdev backend\n");
728
Giulio Camuffo954f1832014-10-11 18:27:30 +0300729 backend = zalloc(sizeof *backend);
730 if (backend == NULL)
Philip Withnall4f499172013-02-02 12:02:32 +0000731 return NULL;
732
Giulio Camuffo954f1832014-10-11 18:27:30 +0300733 backend->compositor = compositor;
Pekka Paalanen7da9a382017-08-30 11:29:49 +0300734 compositor->backend = &backend->base;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400735 if (weston_compositor_set_presentation_clock_software(
Giulio Camuffo954f1832014-10-11 18:27:30 +0300736 compositor) < 0)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400737 goto out_compositor;
738
Giulio Camuffo954f1832014-10-11 18:27:30 +0300739 backend->udev = udev_new();
740 if (backend->udev == NULL) {
Philip Withnall4f499172013-02-02 12:02:32 +0000741 weston_log("Failed to initialize udev context.\n");
742 goto out_compositor;
743 }
744
745 /* Set up the TTY. */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300746 backend->session_listener.notify = session_notify;
747 wl_signal_add(&compositor->session_signal,
748 &backend->session_listener);
749 compositor->launcher =
750 weston_launcher_connect(compositor, param->tty, "seat0", false);
751 if (!compositor->launcher) {
David Herrmann1641d142013-10-15 14:29:57 +0200752 weston_log("fatal: fbdev backend should be run "
753 "using weston-launch binary or as root\n");
Philip Withnall4f499172013-02-02 12:02:32 +0000754 goto out_udev;
755 }
756
Giulio Camuffo954f1832014-10-11 18:27:30 +0300757 backend->base.destroy = fbdev_backend_destroy;
758 backend->base.restore = fbdev_restore;
Philip Withnall4f499172013-02-02 12:02:32 +0000759
Giulio Camuffo954f1832014-10-11 18:27:30 +0300760 backend->prev_state = WESTON_COMPOSITOR_ACTIVE;
Philip Withnall4f499172013-02-02 12:02:32 +0000761
Bob Ham91880f12016-01-12 10:21:47 +0000762 weston_setup_vt_switch_bindings(compositor);
763
Pekka Paalanene77f8ad2016-06-08 17:39:37 +0300764 if (pixman_renderer_init(compositor) < 0)
765 goto out_launcher;
Philip Withnall4f499172013-02-02 12:02:32 +0000766
Giulio Camuffo954f1832014-10-11 18:27:30 +0300767 if (fbdev_output_create(backend, param->device) < 0)
Dawid Gajownik82d49252015-07-31 00:02:28 -0300768 goto out_launcher;
Philip Withnall4f499172013-02-02 12:02:32 +0000769
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300770 udev_input_init(&backend->input, compositor, backend->udev,
771 seat_id, param->configure_device);
Philip Withnall4f499172013-02-02 12:02:32 +0000772
Giulio Camuffo954f1832014-10-11 18:27:30 +0300773 return backend;
Philip Withnall4f499172013-02-02 12:02:32 +0000774
Kristian Høgsberg3f495872013-09-18 23:00:17 -0700775out_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300776 weston_launcher_destroy(compositor->launcher);
Philip Withnall4f499172013-02-02 12:02:32 +0000777
778out_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300779 udev_unref(backend->udev);
Philip Withnall4f499172013-02-02 12:02:32 +0000780
781out_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300782 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300783 free(backend);
Philip Withnall4f499172013-02-02 12:02:32 +0000784
785 return NULL;
786}
787
Benoit Gschwind934e89a2016-04-27 23:56:42 +0200788static void
789config_init_to_defaults(struct weston_fbdev_backend_config *config)
790{
791 /* TODO: Ideally, available frame buffers should be enumerated using
792 * udev, rather than passing a device node in as a parameter. */
793 config->tty = 0; /* default to current tty */
794 config->device = "/dev/fb0"; /* default frame buffer */
Benoit Gschwind934e89a2016-04-27 23:56:42 +0200795}
796
Giulio Camuffo954f1832014-10-11 18:27:30 +0300797WL_EXPORT int
Quentin Glidic23e1d6f2016-12-02 14:08:44 +0100798weston_backend_init(struct weston_compositor *compositor,
799 struct weston_backend_config *config_base)
Philip Withnall4f499172013-02-02 12:02:32 +0000800{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300801 struct fbdev_backend *b;
Benoit Gschwind934e89a2016-04-27 23:56:42 +0200802 struct weston_fbdev_backend_config config = {{ 0, }};
Philip Withnall4f499172013-02-02 12:02:32 +0000803
Benoit Gschwind934e89a2016-04-27 23:56:42 +0200804 if (config_base == NULL ||
805 config_base->struct_version != WESTON_FBDEV_BACKEND_CONFIG_VERSION ||
806 config_base->struct_size > sizeof(struct weston_fbdev_backend_config)) {
807 weston_log("fbdev backend config structure is invalid\n");
808 return -1;
809 }
Philip Withnall4f499172013-02-02 12:02:32 +0000810
Benoit Gschwind934e89a2016-04-27 23:56:42 +0200811 config_init_to_defaults(&config);
812 memcpy(&config, config_base, config_base->struct_size);
Philip Withnall4f499172013-02-02 12:02:32 +0000813
Pekka Paalanena256c5e2016-06-03 14:56:18 +0300814 b = fbdev_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300815 if (b == NULL)
816 return -1;
817 return 0;
Philip Withnall4f499172013-02-02 12:02:32 +0000818}