clients: Introduce xmalloc() and use it a few places
[platform/upstream/weston.git] / clients / window.h
1 /*
2  * Copyright © 2008 Kristian Høgsberg
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #ifndef _WINDOW_H_
24 #define _WINDOW_H_
25
26 #include <xkbcommon/xkbcommon.h>
27 #include <wayland-client.h>
28 #include <cairo.h>
29 #include "../shared/config-parser.h"
30 #include "subsurface-client-protocol.h"
31
32 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
33
34 #define container_of(ptr, type, member) ({                              \
35         const __typeof__( ((type *)0)->member ) *__mptr = (ptr);        \
36         (type *)( (char *)__mptr - offsetof(type,member) );})
37
38 struct window;
39 struct widget;
40 struct display;
41 struct input;
42 struct output;
43
44 struct task {
45         void (*run)(struct task *task, uint32_t events);
46         struct wl_list link;
47 };
48
49 struct rectangle {
50         int32_t x;
51         int32_t y;
52         int32_t width;
53         int32_t height;
54 };
55
56 void *
57 fail_on_null(void *p);
58 void *
59 xmalloc(size_t s);
60 char *
61 xstrdup(const char *s);
62
63 struct display *
64 display_create(int *argc, char *argv[]);
65
66 void
67 display_destroy(struct display *display);
68
69 void
70 display_set_user_data(struct display *display, void *data);
71
72 void *
73 display_get_user_data(struct display *display);
74
75 struct wl_display *
76 display_get_display(struct display *display);
77
78 cairo_device_t *
79 display_get_cairo_device(struct display *display);
80
81 struct wl_compositor *
82 display_get_compositor(struct display *display);
83
84 struct wl_shell *
85 display_get_shell(struct display *display);
86
87 struct output *
88 display_get_output(struct display *display);
89
90 uint32_t
91 display_get_serial(struct display *display);
92
93 typedef void (*display_global_handler_t)(struct display *display,
94                                          uint32_t name,
95                                          const char *interface,
96                                          uint32_t version, void *data);
97
98 void
99 display_set_global_handler(struct display *display,
100                            display_global_handler_t handler);
101 void *
102 display_bind(struct display *display, uint32_t name,
103              const struct wl_interface *interface, uint32_t version);
104
105 typedef void (*display_output_handler_t)(struct output *output, void *data);
106
107 /*
108  * The output configure handler is called, when a new output is connected
109  * and we know its current mode, or when the current mode changes.
110  * Test and set the output user data in your handler to know, if the
111  * output is new. Note: 'data' in the configure handler is the display
112  * user data.
113  */
114 void
115 display_set_output_configure_handler(struct display *display,
116                                      display_output_handler_t handler);
117
118 struct wl_data_source *
119 display_create_data_source(struct display *display);
120
121 #ifdef EGL_NO_DISPLAY
122 EGLDisplay
123 display_get_egl_display(struct display *d);
124
125 EGLConfig
126 display_get_argb_egl_config(struct display *d);
127
128 int
129 display_acquire_window_surface(struct display *display,
130                                struct window *window,
131                                EGLContext ctx);
132 void
133 display_release_window_surface(struct display *display,
134                                struct window *window);
135 #endif
136
137 #define SURFACE_OPAQUE 0x01
138 #define SURFACE_SHM    0x02
139
140 #define SURFACE_HINT_RESIZE 0x10
141
142 cairo_surface_t *
143 display_create_surface(struct display *display,
144                        struct wl_surface *surface,
145                        struct rectangle *rectangle,
146                        uint32_t flags);
147
148 struct wl_buffer *
149 display_get_buffer_for_surface(struct display *display,
150                                cairo_surface_t *surface);
151
152 struct wl_cursor_image *
153 display_get_pointer_image(struct display *display, int pointer);
154
155 void
156 display_defer(struct display *display, struct task *task);
157
158 void
159 display_watch_fd(struct display *display,
160                  int fd, uint32_t events, struct task *task);
161
162 void
163 display_unwatch_fd(struct display *display, int fd);
164
165 void
166 display_run(struct display *d);
167
168 void
169 display_exit(struct display *d);
170
171 enum cursor_type {
172         CURSOR_BOTTOM_LEFT,
173         CURSOR_BOTTOM_RIGHT,
174         CURSOR_BOTTOM,
175         CURSOR_DRAGGING,
176         CURSOR_LEFT_PTR,
177         CURSOR_LEFT,
178         CURSOR_RIGHT,
179         CURSOR_TOP_LEFT,
180         CURSOR_TOP_RIGHT,
181         CURSOR_TOP,
182         CURSOR_IBEAM,
183         CURSOR_HAND1,
184         CURSOR_WATCH,
185
186         CURSOR_BLANK
187 };
188
189 typedef void (*window_key_handler_t)(struct window *window, struct input *input,
190                                      uint32_t time, uint32_t key, uint32_t unicode,
191                                      enum wl_keyboard_key_state state, void *data);
192
193 typedef void (*window_keyboard_focus_handler_t)(struct window *window,
194                                                 struct input *device, void *data);
195
196 typedef void (*window_data_handler_t)(struct window *window,
197                                       struct input *input,
198                                       float x, float y,
199                                       const char **types,
200                                       void *data);
201
202 typedef void (*window_drop_handler_t)(struct window *window,
203                                       struct input *input,
204                                       int32_t x, int32_t y, void *data);
205
206 typedef void (*window_close_handler_t)(struct window *window, void *data);
207 typedef void (*window_fullscreen_handler_t)(struct window *window, void *data);
208
209 typedef void (*window_output_handler_t)(struct window *window, struct output *output,
210                                         int enter, void *data);
211
212 typedef void (*widget_resize_handler_t)(struct widget *widget,
213                                         int32_t width, int32_t height,
214                                         void *data);
215 typedef void (*widget_redraw_handler_t)(struct widget *widget, void *data);
216
217 typedef int (*widget_enter_handler_t)(struct widget *widget,
218                                       struct input *input,
219                                       float x, float y, void *data);
220 typedef void (*widget_leave_handler_t)(struct widget *widget,
221                                        struct input *input, void *data);
222 typedef int (*widget_motion_handler_t)(struct widget *widget,
223                                        struct input *input, uint32_t time,
224                                        float x, float y, void *data);
225 typedef void (*widget_button_handler_t)(struct widget *widget,
226                                         struct input *input, uint32_t time,
227                                         uint32_t button,
228                                         enum wl_pointer_button_state state,
229                                         void *data);
230 typedef void (*widget_axis_handler_t)(struct widget *widget,
231                                       struct input *input, uint32_t time,
232                                       uint32_t axis,
233                                       wl_fixed_t value,
234                                       void *data);
235
236 struct window *
237 window_create(struct display *display);
238 struct window *
239 window_create_transient(struct display *display, struct window *parent,
240                         int32_t x, int32_t y, uint32_t flags);
241 struct window *
242 window_create_custom(struct display *display);
243
244 int
245 window_has_focus(struct window *window);
246
247 typedef void (*menu_func_t)(struct window *window, int index, void *data);
248
249 void
250 window_show_menu(struct display *display,
251                  struct input *input, uint32_t time, struct window *parent,
252                  int32_t x, int32_t y,
253                  menu_func_t func, const char **entries, int count);
254
255 void
256 window_show_frame_menu(struct window *window,
257                        struct input *input, uint32_t time);
258
259 int
260 window_get_buffer_transform(struct window *window);
261
262 void
263 window_set_buffer_transform(struct window *window,
264                             enum wl_output_transform transform);
265
266 uint32_t
267 window_get_buffer_scale(struct window *window);
268
269 void
270 window_set_buffer_scale(struct window *window,
271                         int32_t scale);
272
273 uint32_t
274 window_get_output_scale(struct window *window);
275
276 void
277 window_destroy(struct window *window);
278
279 struct widget *
280 window_add_widget(struct window *window, void *data);
281
282 enum subsurface_mode {
283         SUBSURFACE_SYNCHRONIZED,
284         SUBSURFACE_DESYNCHRONIZED
285 };
286
287 struct widget *
288 window_add_subsurface(struct window *window, void *data,
289                       enum subsurface_mode default_mode);
290
291 typedef void (*data_func_t)(void *data, size_t len,
292                             int32_t x, int32_t y, void *user_data);
293
294 struct display *
295 window_get_display(struct window *window);
296 void
297 window_move(struct window *window, struct input *input, uint32_t time);
298 void
299 window_get_allocation(struct window *window, struct rectangle *allocation);
300 void
301 window_schedule_redraw(struct window *window);
302 void
303 window_schedule_resize(struct window *window, int width, int height);
304
305 void
306 window_damage(struct window *window, int32_t x, int32_t y,
307               int32_t width, int32_t height);
308
309 cairo_surface_t *
310 window_get_surface(struct window *window);
311
312 struct wl_surface *
313 window_get_wl_surface(struct window *window);
314
315 struct wl_shell_surface *
316 window_get_wl_shell_surface(struct window *window);
317
318 enum window_buffer_type {
319         WINDOW_BUFFER_TYPE_EGL_WINDOW,
320         WINDOW_BUFFER_TYPE_SHM,
321 };
322
323 void
324 display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
325                        int32_t x, int32_t y, int32_t width, int32_t height);
326
327 void
328 window_set_buffer_type(struct window *window, enum window_buffer_type type);
329
330 int
331 window_is_fullscreen(struct window *window);
332
333 void
334 window_set_fullscreen(struct window *window, int fullscreen);
335
336 void
337 window_set_fullscreen_method(struct window *window,
338                              enum wl_shell_surface_fullscreen_method method);
339 int
340 window_is_maximized(struct window *window);
341
342 void
343 window_set_maximized(struct window *window, int maximized);
344
345 void
346 window_set_user_data(struct window *window, void *data);
347
348 void *
349 window_get_user_data(struct window *window);
350
351 void
352 window_set_key_handler(struct window *window,
353                        window_key_handler_t handler);
354
355 void
356 window_set_keyboard_focus_handler(struct window *window,
357                                   window_keyboard_focus_handler_t handler);
358
359 void
360 window_set_data_handler(struct window *window,
361                         window_data_handler_t handler);
362
363 void
364 window_set_drop_handler(struct window *window,
365                         window_drop_handler_t handler);
366
367 void
368 window_set_close_handler(struct window *window,
369                          window_close_handler_t handler);
370 void
371 window_set_fullscreen_handler(struct window *window,
372                               window_fullscreen_handler_t handler);
373 void
374 window_set_output_handler(struct window *window,
375                           window_output_handler_t handler);
376
377 void
378 window_set_title(struct window *window, const char *title);
379
380 const char *
381 window_get_title(struct window *window);
382
383 void
384 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y);
385
386 int
387 widget_set_tooltip(struct widget *parent, char *entry, float x, float y);
388
389 void
390 widget_destroy_tooltip(struct widget *parent);
391
392 struct widget *
393 widget_add_widget(struct widget *parent, void *data);
394
395 void
396 widget_destroy(struct widget *widget);
397 void
398 widget_set_default_cursor(struct widget *widget, int cursor);
399 void
400 widget_get_allocation(struct widget *widget, struct rectangle *allocation);
401
402 void
403 widget_set_allocation(struct widget *widget,
404                       int32_t x, int32_t y, int32_t width, int32_t height);
405 void
406 widget_set_size(struct widget *widget, int32_t width, int32_t height);
407 void
408 widget_set_transparent(struct widget *widget, int transparent);
409 void
410 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height);
411
412 void *
413 widget_get_user_data(struct widget *widget);
414
415 cairo_t *
416 widget_cairo_create(struct widget *widget);
417
418 struct wl_surface *
419 widget_get_wl_surface(struct widget *widget);
420
421 uint32_t
422 widget_get_last_time(struct widget *widget);
423
424 void
425 widget_input_region_add(struct widget *widget, const struct rectangle *rect);
426
427 void
428 widget_set_redraw_handler(struct widget *widget,
429                           widget_redraw_handler_t handler);
430 void
431 widget_set_resize_handler(struct widget *widget,
432                           widget_resize_handler_t handler);
433 void
434 widget_set_enter_handler(struct widget *widget,
435                          widget_enter_handler_t handler);
436 void
437 widget_set_leave_handler(struct widget *widget,
438                          widget_leave_handler_t handler);
439 void
440 widget_set_motion_handler(struct widget *widget,
441                           widget_motion_handler_t handler);
442 void
443 widget_set_button_handler(struct widget *widget,
444                           widget_button_handler_t handler);
445 void
446 widget_set_axis_handler(struct widget *widget,
447                         widget_axis_handler_t handler);
448
449 void
450 widget_schedule_redraw(struct widget *widget);
451
452 struct widget *
453 frame_create(struct window *window, void *data);
454
455 void
456 frame_set_child_size(struct widget *widget, int child_width, int child_height);
457
458 void
459 input_set_pointer_image(struct input *input, int pointer);
460
461 void
462 input_get_position(struct input *input, int32_t *x, int32_t *y);
463
464 #define MOD_SHIFT_MASK          0x01
465 #define MOD_ALT_MASK            0x02
466 #define MOD_CONTROL_MASK        0x04
467
468 uint32_t
469 input_get_modifiers(struct input *input);
470
471 void
472 input_grab(struct input *input, struct widget *widget, uint32_t button);
473
474 void
475 input_ungrab(struct input *input);
476
477 struct widget *
478 input_get_focus_widget(struct input *input);
479
480 struct display *
481 input_get_display(struct input *input);
482
483 struct wl_seat *
484 input_get_seat(struct input *input);
485
486 struct wl_data_device *
487 input_get_data_device(struct input *input);
488
489 void
490 input_set_selection(struct input *input,
491                     struct wl_data_source *source, uint32_t time);
492
493 void
494 input_accept(struct input *input, const char *type);
495
496
497 void
498 input_receive_drag_data(struct input *input, const char *mime_type,
499                         data_func_t func, void *user_data);
500
501 int
502 input_receive_selection_data(struct input *input, const char *mime_type,
503                              data_func_t func, void *data);
504 int
505 input_receive_selection_data_to_fd(struct input *input,
506                                    const char *mime_type, int fd);
507
508 void
509 output_set_user_data(struct output *output, void *data);
510
511 void *
512 output_get_user_data(struct output *output);
513
514 void
515 output_set_destroy_handler(struct output *output,
516                            display_output_handler_t handler);
517
518 void
519 output_get_allocation(struct output *output, struct rectangle *allocation);
520
521 struct wl_output *
522 output_get_wl_output(struct output *output);
523
524 enum wl_output_transform
525 output_get_transform(struct output *output);
526
527 uint32_t
528 output_get_scale(struct output *output);
529
530 void
531 keysym_modifiers_add(struct wl_array *modifiers_map,
532                      const char *name);
533
534 xkb_mod_mask_t
535 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
536                           const char *name);
537
538 #endif