Ander Conselvan de Oliveira | 11f8d40 | 2012-10-29 18:19:24 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010-2011 Benjamin Franzke |
| 3 | * Copyright © 2012 Intel Corporation |
| 4 | * |
| 5 | * Permission to use, copy, modify, distribute, and sell this software and |
| 6 | * its documentation for any purpose is hereby granted without fee, provided |
| 7 | * that the above copyright notice appear in all copies and that both that |
| 8 | * copyright notice and this permission notice appear in supporting |
| 9 | * documentation, and that the name of the copyright holders not be used in |
| 10 | * advertising or publicity pertaining to distribution of the software |
| 11 | * without specific, written prior permission. The copyright holders make |
| 12 | * no representations about the suitability of this software for any |
| 13 | * purpose. It is provided "as is" without express or implied warranty. |
| 14 | * |
| 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 16 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 18 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 19 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 20 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 21 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | */ |
| 23 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 24 | #include "config.h" |
Ander Conselvan de Oliveira | 11f8d40 | 2012-10-29 18:19:24 +0200 | [diff] [blame] | 25 | |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <sys/time.h> |
| 29 | |
| 30 | #include "compositor.h" |
| 31 | |
| 32 | struct headless_compositor { |
| 33 | struct weston_compositor base; |
| 34 | struct weston_seat fake_seat; |
| 35 | }; |
| 36 | |
| 37 | struct headless_output { |
| 38 | struct weston_output base; |
| 39 | struct weston_mode mode; |
| 40 | struct wl_event_source *finish_frame_timer; |
| 41 | }; |
| 42 | |
| 43 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 44 | static void |
| 45 | headless_output_start_repaint_loop(struct weston_output *output) |
Ander Conselvan de Oliveira | 11f8d40 | 2012-10-29 18:19:24 +0200 | [diff] [blame] | 46 | { |
Ander Conselvan de Oliveira | 11f8d40 | 2012-10-29 18:19:24 +0200 | [diff] [blame] | 47 | uint32_t msec; |
| 48 | struct timeval tv; |
| 49 | |
| 50 | gettimeofday(&tv, NULL); |
| 51 | msec = tv.tv_sec * 1000 + tv.tv_usec / 1000; |
| 52 | weston_output_finish_frame(output, msec); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static int |
| 56 | finish_frame_handler(void *data) |
| 57 | { |
| 58 | headless_output_start_repaint_loop(data); |
Ander Conselvan de Oliveira | 11f8d40 | 2012-10-29 18:19:24 +0200 | [diff] [blame] | 59 | |
| 60 | return 1; |
| 61 | } |
| 62 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame^] | 63 | static int |
Ander Conselvan de Oliveira | 11f8d40 | 2012-10-29 18:19:24 +0200 | [diff] [blame] | 64 | headless_output_repaint(struct weston_output *output_base, |
| 65 | pixman_region32_t *damage) |
| 66 | { |
| 67 | struct headless_output *output = (struct headless_output *) output_base; |
| 68 | struct weston_compositor *ec = output->base.compositor; |
| 69 | |
| 70 | ec->renderer->repaint_output(&output->base, damage); |
| 71 | |
Ander Conselvan de Oliveira | 0a88772 | 2012-11-22 15:57:00 +0200 | [diff] [blame] | 72 | pixman_region32_subtract(&ec->primary_plane.damage, |
| 73 | &ec->primary_plane.damage, damage); |
| 74 | |
Ander Conselvan de Oliveira | 11f8d40 | 2012-10-29 18:19:24 +0200 | [diff] [blame] | 75 | wl_event_source_timer_update(output->finish_frame_timer, 16); |
| 76 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame^] | 77 | return 0; |
Ander Conselvan de Oliveira | 11f8d40 | 2012-10-29 18:19:24 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static void |
| 81 | headless_output_destroy(struct weston_output *output_base) |
| 82 | { |
| 83 | struct headless_output *output = (struct headless_output *) output_base; |
| 84 | |
| 85 | wl_event_source_remove(output->finish_frame_timer); |
| 86 | free(output); |
| 87 | |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | static int |
| 92 | headless_compositor_create_output(struct headless_compositor *c, |
| 93 | int width, int height) |
| 94 | { |
| 95 | struct headless_output *output; |
| 96 | struct wl_event_loop *loop; |
| 97 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 98 | output = zalloc(sizeof *output); |
Ander Conselvan de Oliveira | 11f8d40 | 2012-10-29 18:19:24 +0200 | [diff] [blame] | 99 | if (output == NULL) |
| 100 | return -1; |
Ander Conselvan de Oliveira | 11f8d40 | 2012-10-29 18:19:24 +0200 | [diff] [blame] | 101 | |
| 102 | output->mode.flags = |
| 103 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 104 | output->mode.width = width; |
| 105 | output->mode.height = height; |
| 106 | output->mode.refresh = 60; |
| 107 | wl_list_init(&output->base.mode_list); |
| 108 | wl_list_insert(&output->base.mode_list, &output->mode.link); |
| 109 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 110 | output->base.current_mode = &output->mode; |
Ander Conselvan de Oliveira | 11f8d40 | 2012-10-29 18:19:24 +0200 | [diff] [blame] | 111 | weston_output_init(&output->base, &c->base, 0, 0, width, height, |
Alexander Larsson | 4ea9552 | 2013-05-22 14:41:37 +0200 | [diff] [blame] | 112 | WL_OUTPUT_TRANSFORM_NORMAL, 1); |
Ander Conselvan de Oliveira | 11f8d40 | 2012-10-29 18:19:24 +0200 | [diff] [blame] | 113 | |
| 114 | output->base.make = "weston"; |
| 115 | output->base.model = "headless"; |
| 116 | |
| 117 | weston_output_move(&output->base, 0, 0); |
| 118 | |
| 119 | loop = wl_display_get_event_loop(c->base.wl_display); |
| 120 | output->finish_frame_timer = |
| 121 | wl_event_loop_add_timer(loop, finish_frame_handler, output); |
| 122 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 123 | output->base.start_repaint_loop = headless_output_start_repaint_loop; |
Ander Conselvan de Oliveira | 11f8d40 | 2012-10-29 18:19:24 +0200 | [diff] [blame] | 124 | output->base.repaint = headless_output_repaint; |
| 125 | output->base.destroy = headless_output_destroy; |
| 126 | output->base.assign_planes = NULL; |
| 127 | output->base.set_backlight = NULL; |
| 128 | output->base.set_dpms = NULL; |
| 129 | output->base.switch_mode = NULL; |
| 130 | |
| 131 | wl_list_insert(c->base.output_list.prev, &output->base.link); |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static void |
| 137 | headless_restore(struct weston_compositor *ec) |
| 138 | { |
| 139 | } |
| 140 | |
| 141 | static void |
| 142 | headless_destroy(struct weston_compositor *ec) |
| 143 | { |
| 144 | struct headless_compositor *c = (struct headless_compositor *) ec; |
| 145 | |
Vasily Khoruzhick | 52cfd61 | 2013-01-08 19:09:01 +0300 | [diff] [blame] | 146 | ec->renderer->destroy(ec); |
Ander Conselvan de Oliveira | 11f8d40 | 2012-10-29 18:19:24 +0200 | [diff] [blame] | 147 | |
| 148 | weston_seat_release(&c->fake_seat); |
| 149 | weston_compositor_shutdown(ec); |
| 150 | |
| 151 | free(ec); |
| 152 | } |
| 153 | |
| 154 | static struct weston_compositor * |
| 155 | headless_compositor_create(struct wl_display *display, |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 156 | int width, int height, const char *display_name, |
| 157 | int *argc, char *argv[], |
| 158 | struct weston_config *config) |
Ander Conselvan de Oliveira | 11f8d40 | 2012-10-29 18:19:24 +0200 | [diff] [blame] | 159 | { |
| 160 | struct headless_compositor *c; |
| 161 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 162 | c = zalloc(sizeof *c); |
Ander Conselvan de Oliveira | 11f8d40 | 2012-10-29 18:19:24 +0200 | [diff] [blame] | 163 | if (c == NULL) |
| 164 | return NULL; |
| 165 | |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 166 | if (weston_compositor_init(&c->base, display, argc, argv, config) < 0) |
Ander Conselvan de Oliveira | 11f8d40 | 2012-10-29 18:19:24 +0200 | [diff] [blame] | 167 | goto err_free; |
| 168 | |
Rob Bradford | 9af5f9e | 2013-05-31 18:09:50 +0100 | [diff] [blame] | 169 | weston_seat_init(&c->fake_seat, &c->base, "default"); |
Ander Conselvan de Oliveira | 11f8d40 | 2012-10-29 18:19:24 +0200 | [diff] [blame] | 170 | |
| 171 | c->base.destroy = headless_destroy; |
| 172 | c->base.restore = headless_restore; |
| 173 | |
| 174 | if (headless_compositor_create_output(c, width, height) < 0) |
| 175 | goto err_compositor; |
| 176 | |
| 177 | if (noop_renderer_init(&c->base) < 0) |
| 178 | goto err_compositor; |
| 179 | |
| 180 | return &c->base; |
| 181 | |
| 182 | err_compositor: |
| 183 | weston_compositor_shutdown(&c->base); |
| 184 | err_free: |
| 185 | free(c); |
| 186 | return NULL; |
| 187 | } |
| 188 | |
| 189 | WL_EXPORT struct weston_compositor * |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 190 | backend_init(struct wl_display *display, int *argc, char *argv[], |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 191 | struct weston_config *config) |
Ander Conselvan de Oliveira | 11f8d40 | 2012-10-29 18:19:24 +0200 | [diff] [blame] | 192 | { |
| 193 | int width = 1024, height = 640; |
| 194 | char *display_name = NULL; |
| 195 | |
| 196 | const struct weston_option headless_options[] = { |
| 197 | { WESTON_OPTION_INTEGER, "width", 0, &width }, |
| 198 | { WESTON_OPTION_INTEGER, "height", 0, &height }, |
| 199 | }; |
| 200 | |
| 201 | parse_options(headless_options, |
| 202 | ARRAY_LENGTH(headless_options), argc, argv); |
| 203 | |
| 204 | return headless_compositor_create(display, width, height, display_name, |
Kristian Høgsberg | 14e438c | 2013-05-26 21:48:14 -0400 | [diff] [blame] | 205 | argc, argv, config); |
Ander Conselvan de Oliveira | 11f8d40 | 2012-10-29 18:19:24 +0200 | [diff] [blame] | 206 | } |