Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 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 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 7 | * 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 Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 14 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 15 | * 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 Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 27 | */ |
| 28 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 29 | #include "config.h" |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 30 | |
| 31 | #include <errno.h> |
| 32 | #include <stdlib.h> |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame] | 33 | #include <stdint.h> |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 34 | #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øgsberg | 7e597f2 | 2013-02-18 16:35:26 -0500 | [diff] [blame] | 42 | #include <linux/input.h> |
Pekka Paalanen | a51e71f | 2017-09-13 17:14:19 +0300 | [diff] [blame] | 43 | #include <assert.h> |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 44 | |
| 45 | #include <libudev.h> |
| 46 | |
Jon Cruz | 35b2eaa | 2015-06-15 15:37:08 -0700 | [diff] [blame] | 47 | #include "shared/helpers.h" |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 48 | #include "compositor.h" |
Benoit Gschwind | 934e89a | 2016-04-27 23:56:42 +0200 | [diff] [blame] | 49 | #include "compositor-fbdev.h" |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 50 | #include "launcher-util.h" |
| 51 | #include "pixman-renderer.h" |
Peter Hutterer | 823ad33 | 2014-11-26 07:06:31 +1000 | [diff] [blame] | 52 | #include "libinput-seat.h" |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 53 | #include "presentation-time-server-protocol.h" |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 54 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 55 | struct fbdev_backend { |
| 56 | struct weston_backend base; |
| 57 | struct weston_compositor *compositor; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 58 | uint32_t prev_state; |
| 59 | |
| 60 | struct udev *udev; |
Rob Bradford | d355b80 | 2013-05-31 18:09:55 +0100 | [diff] [blame] | 61 | struct udev_input input; |
Benoit Gschwind | 934e89a | 2016-04-27 23:56:42 +0200 | [diff] [blame] | 62 | uint32_t output_transform; |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 63 | struct wl_listener session_listener; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | struct fbdev_screeninfo { |
| 67 | unsigned int x_resolution; /* pixels, visible area */ |
| 68 | unsigned int y_resolution; /* pixels, visible area */ |
| 69 | unsigned int width_mm; /* visible screen width in mm */ |
| 70 | unsigned int height_mm; /* visible screen height in mm */ |
| 71 | unsigned int bits_per_pixel; |
| 72 | |
| 73 | size_t buffer_length; /* length of frame buffer memory in bytes */ |
| 74 | size_t line_length; /* length of a line in bytes */ |
| 75 | char id[16]; /* screen identifier */ |
| 76 | |
| 77 | pixman_format_code_t pixel_format; /* frame buffer pixel format */ |
| 78 | unsigned int refresh_rate; /* Hertz */ |
| 79 | }; |
| 80 | |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 81 | struct fbdev_head { |
| 82 | struct weston_head base; |
| 83 | |
| 84 | /* Frame buffer details. */ |
| 85 | char *device; |
| 86 | struct fbdev_screeninfo fb_info; |
| 87 | }; |
| 88 | |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 89 | struct fbdev_output { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 90 | struct fbdev_backend *backend; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 91 | struct weston_output base; |
| 92 | |
| 93 | struct weston_mode mode; |
| 94 | struct wl_event_source *finish_frame_timer; |
| 95 | |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 96 | /* framebuffer mmap details */ |
| 97 | size_t buffer_length; |
| 98 | void *fb; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 99 | |
| 100 | /* pixman details. */ |
| 101 | pixman_image_t *hw_surface; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
Kristian Høgsberg | 7e597f2 | 2013-02-18 16:35:26 -0500 | [diff] [blame] | 104 | static const char default_seat[] = "seat0"; |
| 105 | |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 106 | static inline struct fbdev_head * |
| 107 | to_fbdev_head(struct weston_head *base) |
| 108 | { |
| 109 | return container_of(base, struct fbdev_head, base); |
| 110 | } |
| 111 | |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 112 | static inline struct fbdev_output * |
| 113 | to_fbdev_output(struct weston_output *base) |
| 114 | { |
| 115 | return container_of(base, struct fbdev_output, base); |
| 116 | } |
| 117 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 118 | static inline struct fbdev_backend * |
| 119 | to_fbdev_backend(struct weston_compositor *base) |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 120 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 121 | return container_of(base->backend, struct fbdev_backend, base); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 124 | static struct fbdev_head * |
| 125 | fbdev_output_get_head(struct fbdev_output *output) |
| 126 | { |
| 127 | if (wl_list_length(&output->base.head_list) != 1) |
| 128 | return NULL; |
| 129 | |
| 130 | return container_of(output->base.head_list.next, |
| 131 | struct fbdev_head, base.output_link); |
| 132 | } |
| 133 | |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 134 | static void |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 135 | fbdev_output_start_repaint_loop(struct weston_output *output) |
| 136 | { |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 137 | struct timespec ts; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 138 | |
Pekka Paalanen | 662f384 | 2015-03-18 12:17:26 +0200 | [diff] [blame] | 139 | weston_compositor_read_presentation_clock(output->compositor, &ts); |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 140 | weston_output_finish_frame(output, &ts, WP_PRESENTATION_FEEDBACK_INVALID); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Pekka Paalanen | e77f8ad | 2016-06-08 17:39:37 +0300 | [diff] [blame] | 143 | static int |
Daniel Stone | b1f166d | 2017-03-01 11:34:10 +0000 | [diff] [blame] | 144 | fbdev_output_repaint(struct weston_output *base, pixman_region32_t *damage, |
| 145 | void *repaint_data) |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 146 | { |
| 147 | struct fbdev_output *output = to_fbdev_output(base); |
| 148 | struct weston_compositor *ec = output->base.compositor; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 149 | |
| 150 | /* Repaint the damaged region onto the back buffer. */ |
Sjoerd Simons | c112e0c | 2015-12-04 19:20:12 -0600 | [diff] [blame] | 151 | pixman_renderer_output_set_buffer(base, output->hw_surface); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 152 | ec->renderer->repaint_output(base, damage); |
| 153 | |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 154 | /* Update the damage region. */ |
| 155 | pixman_region32_subtract(&ec->primary_plane.damage, |
| 156 | &ec->primary_plane.damage, damage); |
| 157 | |
| 158 | /* Schedule the end of the frame. We do not sync this to the frame |
| 159 | * buffer clock because users who want that should be using the DRM |
| 160 | * compositor. FBIO_WAITFORVSYNC blocks and FB_ACTIVATE_VBL requires |
| 161 | * panning, which is broken in most kernel drivers. |
| 162 | * |
| 163 | * Finish the frame synchronised to the specified refresh rate. The |
| 164 | * refresh rate is given in mHz and the interval in ms. */ |
| 165 | wl_event_source_timer_update(output->finish_frame_timer, |
| 166 | 1000000 / output->mode.refresh); |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 167 | |
| 168 | return 0; |
Adrian Negreanu | 4aa756d | 2013-09-06 15:16:09 +0300 | [diff] [blame] | 169 | } |
| 170 | |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 171 | static int |
| 172 | finish_frame_handler(void *data) |
| 173 | { |
| 174 | struct fbdev_output *output = data; |
Pekka Paalanen | 363aa7b | 2014-12-17 16:20:40 +0200 | [diff] [blame] | 175 | struct timespec ts; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 176 | |
Pekka Paalanen | 662f384 | 2015-03-18 12:17:26 +0200 | [diff] [blame] | 177 | weston_compositor_read_presentation_clock(output->base.compositor, &ts); |
Pekka Paalanen | 363aa7b | 2014-12-17 16:20:40 +0200 | [diff] [blame] | 178 | weston_output_finish_frame(&output->base, &ts, 0); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 179 | |
| 180 | return 1; |
| 181 | } |
| 182 | |
| 183 | static pixman_format_code_t |
| 184 | calculate_pixman_format(struct fb_var_screeninfo *vinfo, |
| 185 | struct fb_fix_screeninfo *finfo) |
| 186 | { |
| 187 | /* Calculate the pixman format supported by the frame buffer from the |
| 188 | * buffer's metadata. Return 0 if no known pixman format is supported |
| 189 | * (since this has depth 0 it's guaranteed to not conflict with any |
| 190 | * actual pixman format). |
| 191 | * |
| 192 | * Documentation on the vinfo and finfo structures: |
| 193 | * http://www.mjmwired.net/kernel/Documentation/fb/api.txt |
| 194 | * |
| 195 | * TODO: Try a bit harder to support other formats, including setting |
| 196 | * the preferred format in the hardware. */ |
| 197 | int type; |
| 198 | |
| 199 | weston_log("Calculating pixman format from:\n" |
| 200 | STAMP_SPACE " - type: %i (aux: %i)\n" |
| 201 | STAMP_SPACE " - visual: %i\n" |
| 202 | STAMP_SPACE " - bpp: %i (grayscale: %i)\n" |
| 203 | STAMP_SPACE " - red: offset: %i, length: %i, MSB: %i\n" |
| 204 | STAMP_SPACE " - green: offset: %i, length: %i, MSB: %i\n" |
| 205 | STAMP_SPACE " - blue: offset: %i, length: %i, MSB: %i\n" |
| 206 | STAMP_SPACE " - transp: offset: %i, length: %i, MSB: %i\n", |
| 207 | finfo->type, finfo->type_aux, finfo->visual, |
| 208 | vinfo->bits_per_pixel, vinfo->grayscale, |
| 209 | vinfo->red.offset, vinfo->red.length, vinfo->red.msb_right, |
| 210 | vinfo->green.offset, vinfo->green.length, |
| 211 | vinfo->green.msb_right, |
| 212 | vinfo->blue.offset, vinfo->blue.length, |
| 213 | vinfo->blue.msb_right, |
| 214 | vinfo->transp.offset, vinfo->transp.length, |
| 215 | vinfo->transp.msb_right); |
| 216 | |
| 217 | /* We only handle packed formats at the moment. */ |
| 218 | if (finfo->type != FB_TYPE_PACKED_PIXELS) |
| 219 | return 0; |
| 220 | |
| 221 | /* We only handle true-colour frame buffers at the moment. */ |
Marc Chalain | ffbddff | 2013-09-03 16:47:43 +0200 | [diff] [blame] | 222 | switch(finfo->visual) { |
| 223 | case FB_VISUAL_TRUECOLOR: |
| 224 | case FB_VISUAL_DIRECTCOLOR: |
| 225 | if (vinfo->grayscale != 0) |
| 226 | return 0; |
| 227 | break; |
| 228 | default: |
| 229 | return 0; |
| 230 | } |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 231 | |
| 232 | /* We only support formats with MSBs on the left. */ |
| 233 | if (vinfo->red.msb_right != 0 || vinfo->green.msb_right != 0 || |
| 234 | vinfo->blue.msb_right != 0) |
| 235 | return 0; |
| 236 | |
| 237 | /* Work out the format type from the offsets. We only support RGBA and |
| 238 | * ARGB at the moment. */ |
| 239 | type = PIXMAN_TYPE_OTHER; |
| 240 | |
| 241 | if ((vinfo->transp.offset >= vinfo->red.offset || |
| 242 | vinfo->transp.length == 0) && |
| 243 | vinfo->red.offset >= vinfo->green.offset && |
| 244 | vinfo->green.offset >= vinfo->blue.offset) |
| 245 | type = PIXMAN_TYPE_ARGB; |
| 246 | else if (vinfo->red.offset >= vinfo->green.offset && |
| 247 | vinfo->green.offset >= vinfo->blue.offset && |
| 248 | vinfo->blue.offset >= vinfo->transp.offset) |
| 249 | type = PIXMAN_TYPE_RGBA; |
| 250 | |
| 251 | if (type == PIXMAN_TYPE_OTHER) |
| 252 | return 0; |
| 253 | |
| 254 | /* Build the format. */ |
| 255 | return PIXMAN_FORMAT(vinfo->bits_per_pixel, type, |
| 256 | vinfo->transp.length, |
| 257 | vinfo->red.length, |
| 258 | vinfo->green.length, |
| 259 | vinfo->blue.length); |
| 260 | } |
| 261 | |
| 262 | static int |
| 263 | calculate_refresh_rate(struct fb_var_screeninfo *vinfo) |
| 264 | { |
| 265 | uint64_t quot; |
| 266 | |
| 267 | /* Calculate monitor refresh rate. Default is 60 Hz. Units are mHz. */ |
| 268 | quot = (vinfo->upper_margin + vinfo->lower_margin + vinfo->yres); |
| 269 | quot *= (vinfo->left_margin + vinfo->right_margin + vinfo->xres); |
| 270 | quot *= vinfo->pixclock; |
| 271 | |
| 272 | if (quot > 0) { |
| 273 | uint64_t refresh_rate; |
| 274 | |
| 275 | refresh_rate = 1000000000000000LLU / quot; |
| 276 | if (refresh_rate > 200000) |
| 277 | refresh_rate = 200000; /* cap at 200 Hz */ |
| 278 | |
Oliver Smith | a5066e0 | 2017-04-17 11:11:00 +0000 | [diff] [blame] | 279 | if (refresh_rate >= 1000) /* at least 1 Hz */ |
| 280 | return refresh_rate; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | return 60 * 1000; /* default to 60 Hz */ |
| 284 | } |
| 285 | |
| 286 | static int |
Pekka Paalanen | 82db6b7 | 2017-09-12 17:15:42 +0300 | [diff] [blame] | 287 | fbdev_query_screen_info(int fd, struct fbdev_screeninfo *info) |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 288 | { |
| 289 | struct fb_var_screeninfo varinfo; |
| 290 | struct fb_fix_screeninfo fixinfo; |
| 291 | |
| 292 | /* Probe the device for screen information. */ |
| 293 | if (ioctl(fd, FBIOGET_FSCREENINFO, &fixinfo) < 0 || |
| 294 | ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) { |
| 295 | return -1; |
| 296 | } |
| 297 | |
| 298 | /* Store the pertinent data. */ |
| 299 | info->x_resolution = varinfo.xres; |
| 300 | info->y_resolution = varinfo.yres; |
| 301 | info->width_mm = varinfo.width; |
| 302 | info->height_mm = varinfo.height; |
| 303 | info->bits_per_pixel = varinfo.bits_per_pixel; |
| 304 | |
| 305 | info->buffer_length = fixinfo.smem_len; |
| 306 | info->line_length = fixinfo.line_length; |
Derek Foreman | 6179338 | 2015-09-02 13:45:20 -0500 | [diff] [blame] | 307 | strncpy(info->id, fixinfo.id, sizeof(info->id)); |
Bryce Harrington | 44bbdd0 | 2015-09-23 17:39:05 -0700 | [diff] [blame] | 308 | info->id[sizeof(info->id)-1] = '\0'; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 309 | |
| 310 | info->pixel_format = calculate_pixman_format(&varinfo, &fixinfo); |
| 311 | info->refresh_rate = calculate_refresh_rate(&varinfo); |
| 312 | |
| 313 | if (info->pixel_format == 0) { |
| 314 | weston_log("Frame buffer uses an unsupported format.\n"); |
| 315 | return -1; |
| 316 | } |
| 317 | |
| 318 | return 1; |
| 319 | } |
| 320 | |
| 321 | static int |
Pekka Paalanen | 82db6b7 | 2017-09-12 17:15:42 +0300 | [diff] [blame] | 322 | fbdev_set_screen_info(int fd, struct fbdev_screeninfo *info) |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 323 | { |
| 324 | struct fb_var_screeninfo varinfo; |
| 325 | |
| 326 | /* Grab the current screen information. */ |
| 327 | if (ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0) { |
| 328 | return -1; |
| 329 | } |
| 330 | |
| 331 | /* Update the information. */ |
| 332 | varinfo.xres = info->x_resolution; |
| 333 | varinfo.yres = info->y_resolution; |
| 334 | varinfo.width = info->width_mm; |
| 335 | varinfo.height = info->height_mm; |
| 336 | varinfo.bits_per_pixel = info->bits_per_pixel; |
| 337 | |
| 338 | /* Try to set up an ARGB (x8r8g8b8) pixel format. */ |
| 339 | varinfo.grayscale = 0; |
| 340 | varinfo.transp.offset = 24; |
| 341 | varinfo.transp.length = 0; |
| 342 | varinfo.transp.msb_right = 0; |
| 343 | varinfo.red.offset = 16; |
| 344 | varinfo.red.length = 8; |
| 345 | varinfo.red.msb_right = 0; |
| 346 | varinfo.green.offset = 8; |
| 347 | varinfo.green.length = 8; |
| 348 | varinfo.green.msb_right = 0; |
| 349 | varinfo.blue.offset = 0; |
| 350 | varinfo.blue.length = 8; |
| 351 | varinfo.blue.msb_right = 0; |
| 352 | |
| 353 | /* Set the device's screen information. */ |
| 354 | if (ioctl(fd, FBIOPUT_VSCREENINFO, &varinfo) < 0) { |
| 355 | return -1; |
| 356 | } |
| 357 | |
| 358 | return 1; |
| 359 | } |
| 360 | |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 361 | /* Returns an FD for the frame buffer device. */ |
| 362 | static int |
Pekka Paalanen | 82db6b7 | 2017-09-12 17:15:42 +0300 | [diff] [blame] | 363 | fbdev_frame_buffer_open(const char *fb_dev, |
| 364 | struct fbdev_screeninfo *screen_info) |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 365 | { |
| 366 | int fd = -1; |
| 367 | |
| 368 | weston_log("Opening fbdev frame buffer.\n"); |
| 369 | |
| 370 | /* Open the frame buffer device. */ |
| 371 | fd = open(fb_dev, O_RDWR | O_CLOEXEC); |
| 372 | if (fd < 0) { |
| 373 | weston_log("Failed to open frame buffer device ‘%s’: %s\n", |
| 374 | fb_dev, strerror(errno)); |
| 375 | return -1; |
| 376 | } |
| 377 | |
| 378 | /* Grab the screen info. */ |
Pekka Paalanen | 82db6b7 | 2017-09-12 17:15:42 +0300 | [diff] [blame] | 379 | if (fbdev_query_screen_info(fd, screen_info) < 0) { |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 380 | weston_log("Failed to get frame buffer info: %s\n", |
| 381 | strerror(errno)); |
| 382 | |
| 383 | close(fd); |
| 384 | return -1; |
| 385 | } |
| 386 | |
nerdopolis | 92a06a9 | 2018-06-29 08:17:49 -0400 | [diff] [blame^] | 387 | /* Attempt to wake up the framebuffer device, needed for secondary |
| 388 | * framebuffer devices */ |
| 389 | if (fbdev_set_screen_info(fd, screen_info) < 0) { |
| 390 | weston_log("Failed to set mode settings. " |
| 391 | "Attempting to open output anyway.\n"); |
| 392 | } |
| 393 | |
| 394 | |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 395 | return fd; |
| 396 | } |
| 397 | |
| 398 | /* Closes the FD on success or failure. */ |
| 399 | static int |
| 400 | fbdev_frame_buffer_map(struct fbdev_output *output, int fd) |
| 401 | { |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 402 | struct fbdev_head *head; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 403 | int retval = -1; |
| 404 | |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 405 | head = fbdev_output_get_head(output); |
| 406 | |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 407 | weston_log("Mapping fbdev frame buffer.\n"); |
| 408 | |
| 409 | /* Map the frame buffer. Write-only mode, since we don't want to read |
| 410 | * anything back (because it's slow). */ |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 411 | output->buffer_length = head->fb_info.buffer_length; |
| 412 | output->fb = mmap(NULL, output->buffer_length, |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 413 | PROT_WRITE, MAP_SHARED, fd, 0); |
| 414 | if (output->fb == MAP_FAILED) { |
| 415 | weston_log("Failed to mmap frame buffer: %s\n", |
| 416 | strerror(errno)); |
Pekka Paalanen | 82b8ddf | 2017-09-13 11:58:00 +0300 | [diff] [blame] | 417 | output->fb = NULL; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 418 | goto out_close; |
| 419 | } |
| 420 | |
| 421 | /* Create a pixman image to wrap the memory mapped frame buffer. */ |
| 422 | output->hw_surface = |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 423 | pixman_image_create_bits(head->fb_info.pixel_format, |
| 424 | head->fb_info.x_resolution, |
| 425 | head->fb_info.y_resolution, |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 426 | output->fb, |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 427 | head->fb_info.line_length); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 428 | if (output->hw_surface == NULL) { |
| 429 | weston_log("Failed to create surface for frame buffer.\n"); |
| 430 | goto out_unmap; |
| 431 | } |
| 432 | |
| 433 | /* Success! */ |
| 434 | retval = 0; |
| 435 | |
| 436 | out_unmap: |
Pekka Paalanen | a51e71f | 2017-09-13 17:14:19 +0300 | [diff] [blame] | 437 | if (retval != 0 && output->fb != NULL) { |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 438 | munmap(output->fb, output->buffer_length); |
Pekka Paalanen | a51e71f | 2017-09-13 17:14:19 +0300 | [diff] [blame] | 439 | output->fb = NULL; |
| 440 | } |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 441 | |
| 442 | out_close: |
| 443 | if (fd >= 0) |
| 444 | close(fd); |
| 445 | |
| 446 | return retval; |
| 447 | } |
| 448 | |
| 449 | static void |
Pekka Paalanen | a51e71f | 2017-09-13 17:14:19 +0300 | [diff] [blame] | 450 | fbdev_frame_buffer_unmap(struct fbdev_output *output) |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 451 | { |
Pekka Paalanen | a51e71f | 2017-09-13 17:14:19 +0300 | [diff] [blame] | 452 | if (!output->fb) { |
| 453 | assert(!output->hw_surface); |
| 454 | return; |
| 455 | } |
| 456 | |
| 457 | weston_log("Unmapping fbdev frame buffer.\n"); |
| 458 | |
| 459 | if (output->hw_surface) |
| 460 | pixman_image_unref(output->hw_surface); |
| 461 | output->hw_surface = NULL; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 462 | |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 463 | if (munmap(output->fb, output->buffer_length) < 0) |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 464 | weston_log("Failed to munmap frame buffer: %s\n", |
| 465 | strerror(errno)); |
| 466 | |
| 467 | output->fb = NULL; |
| 468 | } |
| 469 | |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 470 | |
| 471 | static int |
| 472 | fbdev_output_attach_head(struct weston_output *output_base, |
| 473 | struct weston_head *head_base) |
| 474 | { |
| 475 | struct fbdev_output *output = to_fbdev_output(output_base); |
| 476 | struct fbdev_head *head = to_fbdev_head(head_base); |
| 477 | |
| 478 | /* Clones not supported. */ |
| 479 | if (!wl_list_empty(&output->base.head_list)) |
| 480 | return -1; |
| 481 | |
| 482 | /* only one static mode in list */ |
| 483 | output->mode.flags = WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 484 | output->mode.width = head->fb_info.x_resolution; |
| 485 | output->mode.height = head->fb_info.y_resolution; |
| 486 | output->mode.refresh = head->fb_info.refresh_rate; |
| 487 | wl_list_init(&output->base.mode_list); |
| 488 | wl_list_insert(&output->base.mode_list, &output->mode.link); |
| 489 | output->base.current_mode = &output->mode; |
| 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 494 | static void fbdev_output_destroy(struct weston_output *base); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 495 | |
| 496 | static int |
Armin Krezović | 6ba369d | 2016-09-30 14:11:06 +0200 | [diff] [blame] | 497 | fbdev_output_enable(struct weston_output *base) |
| 498 | { |
| 499 | struct fbdev_output *output = to_fbdev_output(base); |
| 500 | struct fbdev_backend *backend = to_fbdev_backend(base->compositor); |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 501 | struct fbdev_head *head; |
Armin Krezović | 6ba369d | 2016-09-30 14:11:06 +0200 | [diff] [blame] | 502 | int fb_fd; |
| 503 | struct wl_event_loop *loop; |
| 504 | |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 505 | head = fbdev_output_get_head(output); |
| 506 | |
Armin Krezović | 6ba369d | 2016-09-30 14:11:06 +0200 | [diff] [blame] | 507 | /* Create the frame buffer. */ |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 508 | fb_fd = fbdev_frame_buffer_open(head->device, &head->fb_info); |
Armin Krezović | 6ba369d | 2016-09-30 14:11:06 +0200 | [diff] [blame] | 509 | if (fb_fd < 0) { |
| 510 | weston_log("Creating frame buffer failed.\n"); |
| 511 | return -1; |
| 512 | } |
| 513 | |
| 514 | if (fbdev_frame_buffer_map(output, fb_fd) < 0) { |
| 515 | weston_log("Mapping frame buffer failed.\n"); |
| 516 | return -1; |
| 517 | } |
| 518 | |
| 519 | output->base.start_repaint_loop = fbdev_output_start_repaint_loop; |
| 520 | output->base.repaint = fbdev_output_repaint; |
| 521 | |
Pekka Paalanen | 26ded94 | 2018-04-23 11:44:57 +0200 | [diff] [blame] | 522 | if (pixman_renderer_output_create(&output->base, |
| 523 | PIXMAN_RENDERER_OUTPUT_USE_SHADOW) < 0) |
Armin Krezović | 6ba369d | 2016-09-30 14:11:06 +0200 | [diff] [blame] | 524 | goto out_hw_surface; |
| 525 | |
| 526 | loop = wl_display_get_event_loop(backend->compositor->wl_display); |
| 527 | output->finish_frame_timer = |
| 528 | wl_event_loop_add_timer(loop, finish_frame_handler, output); |
| 529 | |
| 530 | weston_log("fbdev output %d×%d px\n", |
| 531 | output->mode.width, output->mode.height); |
| 532 | weston_log_continue(STAMP_SPACE "guessing %d Hz and 96 dpi\n", |
| 533 | output->mode.refresh / 1000); |
| 534 | |
| 535 | return 0; |
| 536 | |
| 537 | out_hw_surface: |
Pekka Paalanen | a51e71f | 2017-09-13 17:14:19 +0300 | [diff] [blame] | 538 | fbdev_frame_buffer_unmap(output); |
Armin Krezović | 6ba369d | 2016-09-30 14:11:06 +0200 | [diff] [blame] | 539 | |
| 540 | return -1; |
| 541 | } |
| 542 | |
| 543 | static int |
Pekka Paalanen | 82ffe79 | 2017-09-13 17:25:41 +0300 | [diff] [blame] | 544 | fbdev_output_disable(struct weston_output *base) |
Pekka Paalanen | acd71fb | 2017-08-15 10:35:09 +0300 | [diff] [blame] | 545 | { |
Pekka Paalanen | a51e71f | 2017-09-13 17:14:19 +0300 | [diff] [blame] | 546 | struct fbdev_output *output = to_fbdev_output(base); |
| 547 | |
Pekka Paalanen | acd71fb | 2017-08-15 10:35:09 +0300 | [diff] [blame] | 548 | if (!base->enabled) |
| 549 | return 0; |
| 550 | |
Pekka Paalanen | cbe7fb0 | 2017-09-14 11:43:29 +0300 | [diff] [blame] | 551 | wl_event_source_remove(output->finish_frame_timer); |
| 552 | output->finish_frame_timer = NULL; |
| 553 | |
Pekka Paalanen | 61e5a27 | 2017-09-13 17:22:38 +0300 | [diff] [blame] | 554 | pixman_renderer_output_destroy(&output->base); |
Pekka Paalanen | a51e71f | 2017-09-13 17:14:19 +0300 | [diff] [blame] | 555 | fbdev_frame_buffer_unmap(output); |
Pekka Paalanen | acd71fb | 2017-08-15 10:35:09 +0300 | [diff] [blame] | 556 | |
Pekka Paalanen | acd71fb | 2017-08-15 10:35:09 +0300 | [diff] [blame] | 557 | return 0; |
| 558 | } |
| 559 | |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 560 | static struct fbdev_head * |
| 561 | fbdev_head_create(struct fbdev_backend *backend, const char *device) |
| 562 | { |
| 563 | struct fbdev_head *head; |
| 564 | int fb_fd; |
| 565 | |
| 566 | head = zalloc(sizeof *head); |
| 567 | if (!head) |
| 568 | return NULL; |
| 569 | |
| 570 | head->device = strdup(device); |
| 571 | |
| 572 | /* Create the frame buffer. */ |
| 573 | fb_fd = fbdev_frame_buffer_open(head->device, &head->fb_info); |
| 574 | if (fb_fd < 0) { |
| 575 | weston_log("Creating frame buffer head failed.\n"); |
| 576 | goto out_free; |
| 577 | } |
| 578 | close(fb_fd); |
| 579 | |
| 580 | weston_head_init(&head->base, "fbdev"); |
| 581 | weston_head_set_connection_status(&head->base, true); |
| 582 | weston_head_set_monitor_strings(&head->base, "unknown", |
| 583 | head->fb_info.id, NULL); |
| 584 | weston_head_set_subpixel(&head->base, WL_OUTPUT_SUBPIXEL_UNKNOWN); |
| 585 | weston_head_set_physical_size(&head->base, head->fb_info.width_mm, |
| 586 | head->fb_info.height_mm); |
| 587 | |
| 588 | weston_compositor_add_head(backend->compositor, &head->base); |
| 589 | |
| 590 | weston_log("Created head '%s' for device %s (%s)\n", |
| 591 | head->base.name, head->device, head->base.model); |
| 592 | |
| 593 | return head; |
| 594 | |
| 595 | out_free: |
| 596 | free(head->device); |
| 597 | free(head); |
| 598 | |
| 599 | return NULL; |
| 600 | } |
| 601 | |
| 602 | static void |
| 603 | fbdev_head_destroy(struct fbdev_head *head) |
| 604 | { |
| 605 | weston_head_release(&head->base); |
| 606 | free(head->device); |
| 607 | free(head); |
| 608 | } |
| 609 | |
| 610 | static struct weston_output * |
| 611 | fbdev_output_create(struct weston_compositor *compositor, |
| 612 | const char *name) |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 613 | { |
| 614 | struct fbdev_output *output; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 615 | |
| 616 | weston_log("Creating fbdev output.\n"); |
| 617 | |
Bryce Harrington | de16d89 | 2014-11-20 22:21:57 -0800 | [diff] [blame] | 618 | output = zalloc(sizeof *output); |
| 619 | if (output == NULL) |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 620 | return NULL; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 621 | |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 622 | output->backend = to_fbdev_backend(compositor); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 623 | |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 624 | weston_output_init(&output->base, compositor, name); |
Pekka Paalanen | 26ac2e1 | 2017-04-03 13:18:13 +0300 | [diff] [blame] | 625 | |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 626 | output->base.destroy = fbdev_output_destroy; |
Pekka Paalanen | 82ffe79 | 2017-09-13 17:25:41 +0300 | [diff] [blame] | 627 | output->base.disable = fbdev_output_disable; |
Armin Krezović | 6ba369d | 2016-09-30 14:11:06 +0200 | [diff] [blame] | 628 | output->base.enable = fbdev_output_enable; |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 629 | output->base.attach_head = fbdev_output_attach_head; |
Armin Krezović | 6ba369d | 2016-09-30 14:11:06 +0200 | [diff] [blame] | 630 | |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 631 | weston_compositor_add_pending_output(&output->base, compositor); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 632 | |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 633 | return &output->base; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | static void |
| 637 | fbdev_output_destroy(struct weston_output *base) |
| 638 | { |
| 639 | struct fbdev_output *output = to_fbdev_output(base); |
| 640 | |
| 641 | weston_log("Destroying fbdev output.\n"); |
| 642 | |
Pekka Paalanen | 82ffe79 | 2017-09-13 17:25:41 +0300 | [diff] [blame] | 643 | fbdev_output_disable(base); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 644 | |
| 645 | /* Remove the output. */ |
Pekka Paalanen | ae6d35d | 2017-08-16 12:07:14 +0300 | [diff] [blame] | 646 | weston_output_release(&output->base); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 647 | |
| 648 | free(output); |
| 649 | } |
| 650 | |
| 651 | /* strcmp()-style return values. */ |
| 652 | static int |
| 653 | compare_screen_info (const struct fbdev_screeninfo *a, |
| 654 | const struct fbdev_screeninfo *b) |
| 655 | { |
| 656 | if (a->x_resolution == b->x_resolution && |
| 657 | a->y_resolution == b->y_resolution && |
| 658 | a->width_mm == b->width_mm && |
| 659 | a->height_mm == b->height_mm && |
| 660 | a->bits_per_pixel == b->bits_per_pixel && |
| 661 | a->pixel_format == b->pixel_format && |
| 662 | a->refresh_rate == b->refresh_rate) |
| 663 | return 0; |
| 664 | |
| 665 | return 1; |
| 666 | } |
| 667 | |
| 668 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 669 | fbdev_output_reenable(struct fbdev_backend *backend, |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 670 | struct weston_output *base) |
| 671 | { |
| 672 | struct fbdev_output *output = to_fbdev_output(base); |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 673 | struct fbdev_head *head; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 674 | struct fbdev_screeninfo new_screen_info; |
| 675 | int fb_fd; |
| 676 | |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 677 | head = fbdev_output_get_head(output); |
| 678 | |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 679 | weston_log("Re-enabling fbdev output.\n"); |
Pekka Paalanen | 55916d5 | 2017-09-13 16:19:02 +0300 | [diff] [blame] | 680 | assert(output->base.enabled); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 681 | |
| 682 | /* Create the frame buffer. */ |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 683 | fb_fd = fbdev_frame_buffer_open(head->device, &new_screen_info); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 684 | if (fb_fd < 0) { |
| 685 | weston_log("Creating frame buffer failed.\n"); |
Pekka Paalanen | 55916d5 | 2017-09-13 16:19:02 +0300 | [diff] [blame] | 686 | return -1; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /* Check whether the frame buffer details have changed since we were |
| 690 | * disabled. */ |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 691 | if (compare_screen_info(&head->fb_info, &new_screen_info) != 0) { |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 692 | /* Perform a mode-set to restore the old mode. */ |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 693 | if (fbdev_set_screen_info(fb_fd, &head->fb_info) < 0) { |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 694 | weston_log("Failed to restore mode settings. " |
| 695 | "Attempting to re-open output anyway.\n"); |
| 696 | } |
| 697 | |
Rob Bradford | 581b3fd | 2013-07-26 16:29:39 +0100 | [diff] [blame] | 698 | close(fb_fd); |
| 699 | |
Pekka Paalanen | 55916d5 | 2017-09-13 16:19:02 +0300 | [diff] [blame] | 700 | /* Disable and enable the output so that resources depending on |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 701 | * the frame buffer X/Y resolution (such as the shadow buffer) |
| 702 | * are re-initialised. */ |
Pekka Paalanen | 55916d5 | 2017-09-13 16:19:02 +0300 | [diff] [blame] | 703 | fbdev_output_disable(&output->base); |
| 704 | return fbdev_output_enable(&output->base); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | /* Map the device if it has the same details as before. */ |
Pekka Paalanen | e77f8ad | 2016-06-08 17:39:37 +0300 | [diff] [blame] | 708 | if (fbdev_frame_buffer_map(output, fb_fd) < 0) { |
| 709 | weston_log("Mapping frame buffer failed.\n"); |
Pekka Paalanen | 55916d5 | 2017-09-13 16:19:02 +0300 | [diff] [blame] | 710 | return -1; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | return 0; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 716 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 717 | fbdev_backend_destroy(struct weston_compositor *base) |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 718 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 719 | struct fbdev_backend *backend = to_fbdev_backend(base); |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 720 | struct weston_head *head, *next; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 721 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 722 | udev_input_destroy(&backend->input); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 723 | |
| 724 | /* Destroy the output. */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 725 | weston_compositor_shutdown(base); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 726 | |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 727 | wl_list_for_each_safe(head, next, &base->head_list, compositor_link) |
| 728 | fbdev_head_destroy(to_fbdev_head(head)); |
| 729 | |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 730 | /* Chain up. */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 731 | weston_launcher_destroy(base->launcher); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 732 | |
Pekka Paalanen | 513f9a4 | 2017-09-13 16:49:02 +0300 | [diff] [blame] | 733 | udev_unref(backend->udev); |
| 734 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 735 | free(backend); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | static void |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 739 | session_notify(struct wl_listener *listener, void *data) |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 740 | { |
Pekka Paalanen | 3f89794 | 2015-08-19 13:52:47 +0300 | [diff] [blame] | 741 | struct weston_compositor *compositor = data; |
| 742 | struct fbdev_backend *backend = to_fbdev_backend(compositor); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 743 | struct weston_output *output; |
| 744 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 745 | if (compositor->session_active) { |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 746 | weston_log("entering VT\n"); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 747 | compositor->state = backend->prev_state; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 748 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 749 | wl_list_for_each(output, &compositor->output_list, link) { |
| 750 | fbdev_output_reenable(backend, output); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 753 | weston_compositor_damage_all(compositor); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 754 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 755 | udev_input_enable(&backend->input); |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 756 | } else { |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 757 | weston_log("leaving VT\n"); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 758 | udev_input_disable(&backend->input); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 759 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 760 | wl_list_for_each(output, &compositor->output_list, link) { |
Pekka Paalanen | a51e71f | 2017-09-13 17:14:19 +0300 | [diff] [blame] | 761 | fbdev_frame_buffer_unmap(to_fbdev_output(output)); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 764 | backend->prev_state = compositor->state; |
| 765 | weston_compositor_offscreen(compositor); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 766 | |
| 767 | /* If we have a repaint scheduled (from the idle handler), make |
| 768 | * sure we cancel that so we don't try to pageflip when we're |
Philipp Brüschweiler | 57edf7f | 2013-03-29 13:01:56 +0100 | [diff] [blame] | 769 | * vt switched away. The OFFSCREEN state will prevent |
Bryce Harrington | c2be857 | 2015-12-11 13:11:37 -0800 | [diff] [blame] | 770 | * further attempts at repainting. When we switch |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 771 | * back, we schedule a repaint, which will process |
| 772 | * pending frame callbacks. */ |
| 773 | |
| 774 | wl_list_for_each(output, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 775 | &compositor->output_list, link) { |
Daniel Stone | 09a97e2 | 2017-03-01 11:34:06 +0000 | [diff] [blame] | 776 | output->repaint_needed = false; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 777 | } |
nerdopolis | 3894670 | 2014-12-03 15:53:03 +0000 | [diff] [blame] | 778 | } |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 781 | static struct fbdev_backend * |
Pekka Paalanen | a256c5e | 2016-06-03 14:56:18 +0300 | [diff] [blame] | 782 | fbdev_backend_create(struct weston_compositor *compositor, |
Benoit Gschwind | 934e89a | 2016-04-27 23:56:42 +0200 | [diff] [blame] | 783 | struct weston_fbdev_backend_config *param) |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 784 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 785 | struct fbdev_backend *backend; |
Rob Bradford | 2387fde | 2013-05-31 18:09:57 +0100 | [diff] [blame] | 786 | const char *seat_id = default_seat; |
nerdopolis | d68109b | 2018-06-29 08:17:47 -0400 | [diff] [blame] | 787 | const char *session_seat; |
| 788 | |
| 789 | session_seat = getenv("XDG_SEAT"); |
| 790 | if (session_seat) |
| 791 | seat_id = session_seat; |
| 792 | if (param->seat_id) |
| 793 | seat_id = param->seat_id; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 794 | |
| 795 | weston_log("initializing fbdev backend\n"); |
| 796 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 797 | backend = zalloc(sizeof *backend); |
| 798 | if (backend == NULL) |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 799 | return NULL; |
| 800 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 801 | backend->compositor = compositor; |
Pekka Paalanen | 7da9a38 | 2017-08-30 11:29:49 +0300 | [diff] [blame] | 802 | compositor->backend = &backend->base; |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 803 | if (weston_compositor_set_presentation_clock_software( |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 804 | compositor) < 0) |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 805 | goto out_compositor; |
| 806 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 807 | backend->udev = udev_new(); |
| 808 | if (backend->udev == NULL) { |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 809 | weston_log("Failed to initialize udev context.\n"); |
| 810 | goto out_compositor; |
| 811 | } |
| 812 | |
| 813 | /* Set up the TTY. */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 814 | backend->session_listener.notify = session_notify; |
| 815 | wl_signal_add(&compositor->session_signal, |
| 816 | &backend->session_listener); |
| 817 | compositor->launcher = |
nerdopolis | d68109b | 2018-06-29 08:17:47 -0400 | [diff] [blame] | 818 | weston_launcher_connect(compositor, param->tty, seat_id, false); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 819 | if (!compositor->launcher) { |
Pekka Paalanen | a453f4d | 2017-10-31 10:19:48 +0200 | [diff] [blame] | 820 | weston_log("fatal: fbdev backend should be run using " |
| 821 | "weston-launch binary, or your system should " |
| 822 | "provide the logind D-Bus API.\n"); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 823 | goto out_udev; |
| 824 | } |
| 825 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 826 | backend->base.destroy = fbdev_backend_destroy; |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 827 | backend->base.create_output = fbdev_output_create; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 828 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 829 | backend->prev_state = WESTON_COMPOSITOR_ACTIVE; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 830 | |
Bob Ham | 91880f1 | 2016-01-12 10:21:47 +0000 | [diff] [blame] | 831 | weston_setup_vt_switch_bindings(compositor); |
| 832 | |
Pekka Paalanen | e77f8ad | 2016-06-08 17:39:37 +0300 | [diff] [blame] | 833 | if (pixman_renderer_init(compositor) < 0) |
| 834 | goto out_launcher; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 835 | |
Pekka Paalanen | 2305812 | 2017-09-14 16:50:44 +0300 | [diff] [blame] | 836 | if (!fbdev_head_create(backend, param->device)) |
Dawid Gajownik | 82d4925 | 2015-07-31 00:02:28 -0300 | [diff] [blame] | 837 | goto out_launcher; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 838 | |
Giulio Camuffo | 8aedf7b | 2016-06-02 21:48:12 +0300 | [diff] [blame] | 839 | udev_input_init(&backend->input, compositor, backend->udev, |
| 840 | seat_id, param->configure_device); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 841 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 842 | return backend; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 843 | |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 844 | out_launcher: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 845 | weston_launcher_destroy(compositor->launcher); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 846 | |
| 847 | out_udev: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 848 | udev_unref(backend->udev); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 849 | |
| 850 | out_compositor: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 851 | weston_compositor_shutdown(compositor); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 852 | free(backend); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 853 | |
| 854 | return NULL; |
| 855 | } |
| 856 | |
Benoit Gschwind | 934e89a | 2016-04-27 23:56:42 +0200 | [diff] [blame] | 857 | static void |
| 858 | config_init_to_defaults(struct weston_fbdev_backend_config *config) |
| 859 | { |
| 860 | /* TODO: Ideally, available frame buffers should be enumerated using |
| 861 | * udev, rather than passing a device node in as a parameter. */ |
| 862 | config->tty = 0; /* default to current tty */ |
| 863 | config->device = "/dev/fb0"; /* default frame buffer */ |
nerdopolis | d68109b | 2018-06-29 08:17:47 -0400 | [diff] [blame] | 864 | config->seat_id = NULL; |
Benoit Gschwind | 934e89a | 2016-04-27 23:56:42 +0200 | [diff] [blame] | 865 | } |
| 866 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 867 | WL_EXPORT int |
Quentin Glidic | 23e1d6f | 2016-12-02 14:08:44 +0100 | [diff] [blame] | 868 | weston_backend_init(struct weston_compositor *compositor, |
| 869 | struct weston_backend_config *config_base) |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 870 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 871 | struct fbdev_backend *b; |
Benoit Gschwind | 934e89a | 2016-04-27 23:56:42 +0200 | [diff] [blame] | 872 | struct weston_fbdev_backend_config config = {{ 0, }}; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 873 | |
Benoit Gschwind | 934e89a | 2016-04-27 23:56:42 +0200 | [diff] [blame] | 874 | if (config_base == NULL || |
| 875 | config_base->struct_version != WESTON_FBDEV_BACKEND_CONFIG_VERSION || |
| 876 | config_base->struct_size > sizeof(struct weston_fbdev_backend_config)) { |
| 877 | weston_log("fbdev backend config structure is invalid\n"); |
| 878 | return -1; |
| 879 | } |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 880 | |
Benoit Gschwind | 934e89a | 2016-04-27 23:56:42 +0200 | [diff] [blame] | 881 | config_init_to_defaults(&config); |
| 882 | memcpy(&config, config_base, config_base->struct_size); |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 883 | |
Pekka Paalanen | a256c5e | 2016-06-03 14:56:18 +0300 | [diff] [blame] | 884 | b = fbdev_backend_create(compositor, &config); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 885 | if (b == NULL) |
| 886 | return -1; |
| 887 | return 0; |
Philip Withnall | 4f49917 | 2013-02-02 12:02:32 +0000 | [diff] [blame] | 888 | } |