blob: 55206b3dcc84680580a616545d96bdbc86417bb6 [file] [log] [blame]
Ander Conselvan de Oliveira11f8d402012-10-29 18:19:24 +02001/*
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
24#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27
28#include <stdlib.h>
29#include <string.h>
30#include <sys/time.h>
31
32#include "compositor.h"
33
34struct headless_compositor {
35 struct weston_compositor base;
36 struct weston_seat fake_seat;
37};
38
39struct headless_output {
40 struct weston_output base;
41 struct weston_mode mode;
42 struct wl_event_source *finish_frame_timer;
43};
44
45
46static int
47finish_frame_handler(void *data)
48{
49 struct weston_output *output = data;
50 uint32_t msec;
51 struct timeval tv;
52
53 gettimeofday(&tv, NULL);
54 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
55 weston_output_finish_frame(output, msec);
56
57 return 1;
58}
59
60static void
61headless_output_repaint(struct weston_output *output_base,
62 pixman_region32_t *damage)
63{
64 struct headless_output *output = (struct headless_output *) output_base;
65 struct weston_compositor *ec = output->base.compositor;
66
67 ec->renderer->repaint_output(&output->base, damage);
68
69 wl_event_source_timer_update(output->finish_frame_timer, 16);
70
71 return;
72}
73
74static void
75headless_output_destroy(struct weston_output *output_base)
76{
77 struct headless_output *output = (struct headless_output *) output_base;
78
79 wl_event_source_remove(output->finish_frame_timer);
80 free(output);
81
82 return;
83}
84
85static int
86headless_compositor_create_output(struct headless_compositor *c,
87 int width, int height)
88{
89 struct headless_output *output;
90 struct wl_event_loop *loop;
91
92 output = malloc(sizeof *output);
93 if (output == NULL)
94 return -1;
95 memset(output, 0, sizeof *output);
96
97 output->mode.flags =
98 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
99 output->mode.width = width;
100 output->mode.height = height;
101 output->mode.refresh = 60;
102 wl_list_init(&output->base.mode_list);
103 wl_list_insert(&output->base.mode_list, &output->mode.link);
104
105 output->base.current = &output->mode;
106 weston_output_init(&output->base, &c->base, 0, 0, width, height,
107 WL_OUTPUT_TRANSFORM_NORMAL);
108
109 output->base.make = "weston";
110 output->base.model = "headless";
111
112 weston_output_move(&output->base, 0, 0);
113
114 loop = wl_display_get_event_loop(c->base.wl_display);
115 output->finish_frame_timer =
116 wl_event_loop_add_timer(loop, finish_frame_handler, output);
117
118 output->base.origin = output->base.current;
119 output->base.repaint = headless_output_repaint;
120 output->base.destroy = headless_output_destroy;
121 output->base.assign_planes = NULL;
122 output->base.set_backlight = NULL;
123 output->base.set_dpms = NULL;
124 output->base.switch_mode = NULL;
125
126 wl_list_insert(c->base.output_list.prev, &output->base.link);
127
128 return 0;
129}
130
131static void
132headless_restore(struct weston_compositor *ec)
133{
134}
135
136static void
137headless_destroy(struct weston_compositor *ec)
138{
139 struct headless_compositor *c = (struct headless_compositor *) ec;
140
141 noop_renderer_destroy(ec);
142
143 weston_seat_release(&c->fake_seat);
144 weston_compositor_shutdown(ec);
145
146 free(ec);
147}
148
149static struct weston_compositor *
150headless_compositor_create(struct wl_display *display,
151 int width, int height, const char *display_name,
152 int argc, char *argv[], const char *config_file)
153{
154 struct headless_compositor *c;
155
156 c = calloc(1, sizeof *c);
157 if (c == NULL)
158 return NULL;
159
160 memset(c, 0, sizeof *c);
161
162 if (weston_compositor_init(&c->base, display, argc, argv,
163 config_file) < 0)
164 goto err_free;
165
166 weston_seat_init(&c->fake_seat, &c->base);
167
168 c->base.destroy = headless_destroy;
169 c->base.restore = headless_restore;
170
171 if (headless_compositor_create_output(c, width, height) < 0)
172 goto err_compositor;
173
174 if (noop_renderer_init(&c->base) < 0)
175 goto err_compositor;
176
177 return &c->base;
178
179err_compositor:
180 weston_compositor_shutdown(&c->base);
181err_free:
182 free(c);
183 return NULL;
184}
185
186WL_EXPORT struct weston_compositor *
187backend_init(struct wl_display *display, int argc, char *argv[],
188 const char *config_file)
189{
190 int width = 1024, height = 640;
191 char *display_name = NULL;
192
193 const struct weston_option headless_options[] = {
194 { WESTON_OPTION_INTEGER, "width", 0, &width },
195 { WESTON_OPTION_INTEGER, "height", 0, &height },
196 };
197
198 parse_options(headless_options,
199 ARRAY_LENGTH(headless_options), argc, argv);
200
201 return headless_compositor_create(display, width, height, display_name,
202 argc, argv, config_file);
203}