xserver: Clean up X server socket on exit
[profile/ivi/weston.git] / compositor / compositor.h
1 /*
2  * Copyright © 2008 Kristian Høgsberg
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 #ifndef _WAYLAND_SYSTEM_COMPOSITOR_H_
20 #define _WAYLAND_SYSTEM_COMPOSITOR_H_
21
22 #include <libudev.h>
23 #include <pixman.h>
24 #include "wayland-server.h"
25 #include "wayland-util.h"
26
27 #include <GLES2/gl2.h>
28 #include <GLES2/gl2ext.h>
29 #include <EGL/egl.h>
30 #include <EGL/eglext.h>
31
32 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
33
34 #define FD_TO_EGL_NATIVE_DPY(fd) ((EGLNativeDisplayType)(intptr_t)(fd))
35
36 struct wlsc_matrix {
37         GLfloat d[16];
38 };
39
40 void
41 wlsc_matrix_init(struct wlsc_matrix *matrix);
42 void
43 wlsc_matrix_scale(struct wlsc_matrix *matrix, GLfloat x, GLfloat y, GLfloat z);
44 void
45 wlsc_matrix_translate(struct wlsc_matrix *matrix,
46                       GLfloat x, GLfloat y, GLfloat z);
47
48 struct wlsc_transform {
49         struct wlsc_matrix matrix;
50         struct wlsc_matrix inverse;
51 };
52
53 struct wlsc_surface;
54 struct wlsc_input_device;
55
56 struct wlsc_mode {
57         uint32_t flags;
58         int32_t width, height;
59         uint32_t refresh;
60         struct wl_list link;
61 };
62
63 struct wlsc_output {
64         struct wl_object object;
65         struct wl_list link;
66         struct wlsc_compositor *compositor;
67         struct wlsc_surface *background;
68         struct wlsc_matrix matrix;
69         int32_t x, y, mm_width, mm_height;
70         pixman_region32_t region;
71         pixman_region32_t previous_damage;
72         uint32_t flags;
73         int repaint_needed;
74         int repaint_scheduled;
75
76         char *make, *model;
77         uint32_t subpixel;
78         
79         struct wlsc_mode *current;
80         struct wl_list mode_list;
81         struct wl_buffer *scanout_buffer;
82         struct wl_listener scanout_buffer_destroy_listener;
83
84         int (*prepare_render)(struct wlsc_output *output);
85         int (*present)(struct wlsc_output *output);
86         int (*prepare_scanout_surface)(struct wlsc_output *output,
87                                        struct wlsc_surface *es);
88         int (*set_hardware_cursor)(struct wlsc_output *output,
89                                    struct wlsc_input_device *input);
90 };
91
92 enum wlsc_pointer_type {
93         WLSC_POINTER_BOTTOM_LEFT,
94         WLSC_POINTER_BOTTOM_RIGHT,
95         WLSC_POINTER_BOTTOM,
96         WLSC_POINTER_DRAGGING,
97         WLSC_POINTER_LEFT_PTR,
98         WLSC_POINTER_LEFT,
99         WLSC_POINTER_RIGHT,
100         WLSC_POINTER_TOP_LEFT,
101         WLSC_POINTER_TOP_RIGHT,
102         WLSC_POINTER_TOP,
103         WLSC_POINTER_IBEAM,
104 };
105
106 struct wlsc_input_device {
107         struct wl_input_device input_device;
108         struct wlsc_surface *sprite;
109         int32_t hotspot_x, hotspot_y;
110         struct wl_list link;
111         uint32_t modifier_state;
112         struct wl_selection *selection;
113 };
114
115 struct wlsc_sprite {
116         GLuint texture;
117         EGLImageKHR image;
118         struct wl_visual *visual;
119         int width;
120         int height;
121 };
122
123 struct wlsc_shader {
124         GLuint program;
125         GLuint vertex_shader, fragment_shader;
126         GLuint proj_uniform;
127         GLuint tex_uniform;
128         GLuint color_uniform;
129 };
130
131 struct wlsc_animation {
132         void (*frame)(struct wlsc_animation *animation,
133                       struct wlsc_output *output, uint32_t msecs);
134         struct wl_list link;
135 };
136
137 struct wlsc_spring {
138         double k;
139         double friction;
140         double current;
141         double target;
142         double previous;
143         uint32_t timestamp;
144 };
145
146 struct wlsc_shell {
147         void (*lock)(struct wlsc_shell *shell);
148         void (*attach)(struct wlsc_shell *shell, struct wlsc_surface *surface);
149         void (*set_selection_focus)(struct wlsc_shell *shell,
150                                     struct wl_selection *selection,
151                                     struct wl_surface *surface, uint32_t time);
152 };
153
154 enum {
155         WLSC_COMPOSITOR_ACTIVE,
156         WLSC_COMPOSITOR_SLEEPING
157 };
158
159 struct wlsc_compositor {
160         struct wl_compositor compositor;
161
162         struct wl_shm *shm;
163         struct wlsc_xserver *wxs;
164
165         EGLDisplay display;
166         EGLContext context;
167         EGLConfig config;
168         GLuint fbo;
169         GLuint proj_uniform, tex_uniform;
170         struct wlsc_sprite **pointer_sprites;
171         struct wlsc_shader texture_shader;
172         struct wlsc_shader solid_shader;
173         struct wl_display *wl_display;
174
175         struct wlsc_shell *shell;
176
177         /* There can be more than one, but not right now... */
178         struct wl_input_device *input_device;
179
180         struct wl_list output_list;
181         struct wl_list input_device_list;
182         struct wl_list surface_list;
183         struct wl_list binding_list;
184         struct wl_list animation_list;
185         struct {
186                 struct wlsc_spring spring;
187                 struct wlsc_animation animation;
188         } fade;
189
190         uint32_t state;
191         struct wl_event_source *idle_source;
192         uint32_t idle_inhibit;
193
194         /* Repaint state. */
195         struct timespec previous_swap;
196         pixman_region32_t damage;
197         struct wl_array vertices, indices;
198
199         struct wlsc_surface *overlay;
200         struct wlsc_switcher *switcher;
201         uint32_t focus;
202
203         PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC
204                 image_target_renderbuffer_storage;
205         PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
206         PFNEGLCREATEIMAGEKHRPROC create_image;
207         PFNEGLDESTROYIMAGEKHRPROC destroy_image;
208         PFNEGLBINDWAYLANDDISPLAYWL bind_display;
209         PFNEGLUNBINDWAYLANDDISPLAYWL unbind_display;
210         int has_bind_display;
211
212         void (*destroy)(struct wlsc_compositor *ec);
213         int (*authenticate)(struct wlsc_compositor *c, uint32_t id);
214         EGLImageKHR (*create_cursor_image)(struct wlsc_compositor *c,
215                                            int32_t width, int32_t height);
216 };
217
218 #define MODIFIER_CTRL   (1 << 8)
219 #define MODIFIER_ALT    (1 << 9)
220 #define MODIFIER_SUPER  (1 << 10)
221
222 enum wlsc_output_flags {
223         WL_OUTPUT_FLIPPED = 0x01
224 };
225
226 struct wlsc_vector {
227         GLfloat f[4];
228 };
229
230 enum wlsc_surface_map_type {
231         WLSC_SURFACE_MAP_UNMAPPED,
232         WLSC_SURFACE_MAP_TOPLEVEL,
233         WLSC_SURFACE_MAP_TRANSIENT,
234         WLSC_SURFACE_MAP_FULLSCREEN
235 };
236
237 struct wlsc_surface {
238         struct wl_surface surface;
239         struct wlsc_compositor *compositor;
240         GLuint texture, saved_texture;
241         pixman_region32_t damage;
242         pixman_region32_t opaque;
243         int32_t x, y, width, height;
244         int32_t pitch;
245         int32_t saved_x, saved_y;
246         struct wl_list link;
247         struct wl_list buffer_link;
248         struct wlsc_transform *transform;
249         struct wl_visual *visual;
250         struct wlsc_output *output;
251         enum wlsc_surface_map_type map_type;
252         struct wlsc_output *fullscreen_output;
253
254         EGLImageKHR image;
255
256         struct wl_buffer *buffer;
257         struct wl_listener buffer_destroy_listener;
258 };
259
260 void
261 wlsc_spring_init(struct wlsc_spring *spring,
262                  double k, double current, double target);
263 void
264 wlsc_spring_update(struct wlsc_spring *spring, uint32_t msec);
265 int
266 wlsc_spring_done(struct wlsc_spring *spring);
267
268 void
269 wlsc_surface_activate(struct wlsc_surface *surface,
270                       struct wlsc_input_device *device, uint32_t time);
271
272 void
273 notify_motion(struct wl_input_device *device,
274               uint32_t time, int x, int y);
275 void
276 notify_button(struct wl_input_device *device,
277               uint32_t time, int32_t button, int32_t state);
278 void
279 notify_key(struct wl_input_device *device,
280            uint32_t time, uint32_t key, uint32_t state);
281
282 void
283 notify_pointer_focus(struct wl_input_device *device,
284                      uint32_t time,
285                      struct wlsc_output *output,
286                      int32_t x, int32_t y);
287
288 void
289 notify_keyboard_focus(struct wl_input_device *device,
290                       uint32_t time, struct wlsc_output *output,
291                       struct wl_array *keys);
292
293 void
294 wlsc_output_finish_frame(struct wlsc_output *output, int msecs);
295 void
296 wlsc_output_damage(struct wlsc_output *output);
297 void
298 wlsc_compositor_schedule_repaint(struct wlsc_compositor *compositor);
299 void
300 wlsc_compositor_fade(struct wlsc_compositor *compositor, float tint);
301 void
302 wlsc_compositor_damage_all(struct wlsc_compositor *compositor);
303 void
304 wlsc_compositor_unlock(struct wlsc_compositor *compositor);
305 void
306 wlsc_compositor_wake(struct wlsc_compositor *compositor);
307
308 struct wlsc_binding;
309 typedef void (*wlsc_binding_handler_t)(struct wl_input_device *device,
310                                        uint32_t time, uint32_t key,
311                                        uint32_t button,
312                                        uint32_t state, void *data);
313 struct wlsc_binding *
314 wlsc_compositor_add_binding(struct wlsc_compositor *compositor,
315                             uint32_t key, uint32_t button, uint32_t modifier,
316                             wlsc_binding_handler_t binding, void *data);
317 void
318 wlsc_binding_destroy(struct wlsc_binding *binding);
319
320 struct wlsc_surface *
321 wlsc_surface_create(struct wlsc_compositor *compositor,
322                     int32_t x, int32_t y, int32_t width, int32_t height);
323
324 void
325 wlsc_surface_configure(struct wlsc_surface *surface,
326                        int x, int y, int width, int height);
327
328 void
329 wlsc_surface_assign_output(struct wlsc_surface *surface);
330
331 void
332 wlsc_surface_damage(struct wlsc_surface *surface);
333
334 void
335 wlsc_surface_damage_below(struct wlsc_surface *surface);
336
337 void
338 wlsc_surface_damage_rectangle(struct wlsc_surface *surface,
339                               int32_t x, int32_t y,
340                               int32_t width, int32_t height);
341
342 void
343 wlsc_input_device_set_pointer_image(struct wlsc_input_device *device,
344                                     enum wlsc_pointer_type type);
345 struct wlsc_surface *
346 pick_surface(struct wl_input_device *device, int32_t *sx, int32_t *sy);
347
348 uint32_t
349 wlsc_compositor_get_time(void);
350
351 int
352 wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display);
353 void
354 wlsc_output_move(struct wlsc_output *output, int x, int y);
355 void
356 wlsc_output_init(struct wlsc_output *output, struct wlsc_compositor *c,
357                  int x, int y, int width, int height, uint32_t flags);
358 void
359 wlsc_output_destroy(struct wlsc_output *output);
360
361 void
362 wlsc_input_device_init(struct wlsc_input_device *device,
363                        struct wlsc_compositor *ec);
364
365 void
366 wlsc_switcher_init(struct wlsc_compositor *compositor);
367
368 void
369 evdev_input_add_devices(struct wlsc_compositor *c, struct udev *udev);
370
371 enum {
372         TTY_ENTER_VT,
373         TTY_LEAVE_VT
374 };
375
376 typedef void (*tty_vt_func_t)(struct wlsc_compositor *compositor, int event);
377
378 struct tty *
379 tty_create(struct wlsc_compositor *compositor, tty_vt_func_t vt_func);
380
381 void
382 tty_destroy(struct tty *tty);
383
384 void
385 screenshooter_create(struct wlsc_compositor *ec);
386
387 uint32_t *
388 wlsc_load_image(const char *filename,
389                 int32_t *width_arg, int32_t *height_arg, uint32_t *stride_arg);
390
391 struct wlsc_process {
392         pid_t pid;
393         void (*cleanup)(struct wlsc_process *process, int status);
394         struct wl_list link;
395 };
396
397 void
398 wlsc_watch_process(struct wlsc_process *process);
399
400 int
401 wlsc_xserver_init(struct wlsc_compositor *compositor);
402 void
403 wlsc_xserver_destroy(struct wlsc_compositor *compositor);
404
405 #endif