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