Fix a crash when opening two terminal windows and closing the first one.
[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
31 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
32
33 #define container_of(ptr, type, member) ({                              \
34         const __typeof__( ((type *)0)->member ) *__mptr = (ptr);        \
35         (type *)( (char *)__mptr - offsetof(type,member) );})
36
37 struct window;
38 struct widget;
39 struct display;
40 struct input;
41 struct output;
42
43 struct task {
44         void (*run)(struct task *task, uint32_t events);
45         struct wl_list link;
46 };
47
48 struct rectangle {
49         int32_t x;
50         int32_t y;
51         int32_t width;
52         int32_t height;
53 };
54
55 struct display *
56 display_create(int argc, char *argv[]);
57
58 void
59 display_destroy(struct display *display);
60
61 void
62 display_set_user_data(struct display *display, void *data);
63
64 void *
65 display_get_user_data(struct display *display);
66
67 struct wl_display *
68 display_get_display(struct display *display);
69
70 struct wl_compositor *
71 display_get_compositor(struct display *display);
72
73 struct wl_shell *
74 display_get_shell(struct display *display);
75
76 struct output *
77 display_get_output(struct display *display);
78
79 uint32_t
80 display_get_serial(struct display *display);
81
82 typedef void (*display_global_handler_t)(struct display *display,
83                                          uint32_t name,
84                                          const char *interface,
85                                          uint32_t version, void *data);
86
87 void
88 display_set_global_handler(struct display *display,
89                            display_global_handler_t handler);
90 void *
91 display_bind(struct display *display, uint32_t name,
92              const struct wl_interface *interface, uint32_t version);
93
94 typedef void (*display_output_handler_t)(struct output *output, void *data);
95
96 /*
97  * The output configure handler is called, when a new output is connected
98  * and we know its current mode, or when the current mode changes.
99  * Test and set the output user data in your handler to know, if the
100  * output is new. Note: 'data' in the configure handler is the display
101  * user data.
102  */
103 void
104 display_set_output_configure_handler(struct display *display,
105                                      display_output_handler_t handler);
106
107 struct wl_data_source *
108 display_create_data_source(struct display *display);
109
110 #ifdef EGL_NO_DISPLAY
111 EGLDisplay
112 display_get_egl_display(struct display *d);
113
114 EGLConfig
115 display_get_argb_egl_config(struct display *d);
116
117 int
118 display_acquire_window_surface(struct display *display,
119                                struct window *window,
120                                EGLContext ctx);
121 void
122 display_release_window_surface(struct display *display,
123                                struct window *window);
124 #endif
125
126 #define SURFACE_OPAQUE 0x01
127 #define SURFACE_SHM    0x02
128
129 cairo_surface_t *
130 display_create_surface(struct display *display,
131                        struct wl_surface *surface,
132                        struct rectangle *rectangle,
133                        uint32_t flags);
134
135 struct wl_buffer *
136 display_get_buffer_for_surface(struct display *display,
137                                cairo_surface_t *surface);
138
139 struct wl_cursor_image *
140 display_get_pointer_image(struct display *display, int pointer);
141
142 void
143 display_defer(struct display *display, struct task *task);
144
145 void
146 display_watch_fd(struct display *display,
147                  int fd, uint32_t events, struct task *task);
148
149 void
150 display_unwatch_fd(struct display *display, int fd);
151
152 void
153 display_run(struct display *d);
154
155 void
156 display_exit(struct display *d);
157
158 enum cursor_type {
159         CURSOR_BOTTOM_LEFT,
160         CURSOR_BOTTOM_RIGHT,
161         CURSOR_BOTTOM,
162         CURSOR_DRAGGING,
163         CURSOR_LEFT_PTR,
164         CURSOR_LEFT,
165         CURSOR_RIGHT,
166         CURSOR_TOP_LEFT,
167         CURSOR_TOP_RIGHT,
168         CURSOR_TOP,
169         CURSOR_IBEAM,
170         CURSOR_HAND1,
171         CURSOR_WATCH,
172
173         CURSOR_BLANK
174 };
175
176 typedef void (*window_key_handler_t)(struct window *window, struct input *input,
177                                      uint32_t time, uint32_t key, uint32_t unicode,
178                                      enum wl_keyboard_key_state state, void *data);
179
180 typedef void (*window_keyboard_focus_handler_t)(struct window *window,
181                                                 struct input *device, void *data);
182
183 typedef void (*window_data_handler_t)(struct window *window,
184                                       struct input *input,
185                                       float x, float y,
186                                       const char **types,
187                                       void *data);
188
189 typedef void (*window_drop_handler_t)(struct window *window,
190                                       struct input *input,
191                                       int32_t x, int32_t y, void *data);
192
193 typedef void (*window_close_handler_t)(struct window *window, void *data);
194 typedef void (*window_fullscreen_handler_t)(struct window *window, void *data);
195
196 typedef void (*widget_resize_handler_t)(struct widget *widget,
197                                         int32_t width, int32_t height,
198                                         void *data);
199 typedef void (*widget_redraw_handler_t)(struct widget *widget, void *data);
200
201 typedef int (*widget_enter_handler_t)(struct widget *widget,
202                                       struct input *input,
203                                       float x, float y, void *data);
204 typedef void (*widget_leave_handler_t)(struct widget *widget,
205                                        struct input *input, void *data);
206 typedef int (*widget_motion_handler_t)(struct widget *widget,
207                                        struct input *input, uint32_t time,
208                                        float x, float y, void *data);
209 typedef void (*widget_button_handler_t)(struct widget *widget,
210                                         struct input *input, uint32_t time,
211                                         uint32_t button,
212                                         enum wl_pointer_button_state state,
213                                         void *data);
214 typedef void (*widget_axis_handler_t)(struct widget *widget,
215                                       struct input *input, uint32_t time,
216                                       uint32_t axis,
217                                       wl_fixed_t value,
218                                       void *data);
219
220 struct window *
221 window_create(struct display *display);
222 struct window *
223 window_create_transient(struct display *display, struct window *parent,
224                         int32_t x, int32_t y, uint32_t flags);
225 struct window *
226 window_create_custom(struct display *display);
227
228 int
229 window_has_focus(struct window *window);
230
231 typedef void (*menu_func_t)(struct window *window, int index, void *data);
232
233 void
234 window_show_menu(struct display *display,
235                  struct input *input, uint32_t time, struct window *parent,
236                  int32_t x, int32_t y,
237                  menu_func_t func, const char **entries, int count);
238
239 void
240 window_show_frame_menu(struct window *window,
241                        struct input *input, uint32_t time);
242
243 void
244 window_destroy(struct window *window);
245
246 struct widget *
247 window_add_widget(struct window *window, void *data);
248
249 typedef void (*data_func_t)(void *data, size_t len,
250                             int32_t x, int32_t y, void *user_data);
251
252 struct display *
253 window_get_display(struct window *window);
254 void
255 window_move(struct window *window, struct input *input, uint32_t time);
256 void
257 window_get_allocation(struct window *window, struct rectangle *allocation);
258 void
259 window_schedule_redraw(struct window *window);
260 void
261 window_schedule_resize(struct window *window, int width, int height);
262
263 void
264 window_damage(struct window *window, int32_t x, int32_t y,
265               int32_t width, int32_t height);
266
267 cairo_surface_t *
268 window_get_surface(struct window *window);
269
270 struct wl_surface *
271 window_get_wl_surface(struct window *window);
272
273 struct wl_shell_surface *
274 window_get_wl_shell_surface(struct window *window);
275
276 enum window_buffer_type {
277         WINDOW_BUFFER_TYPE_EGL_WINDOW,
278         WINDOW_BUFFER_TYPE_SHM,
279 };
280
281 void
282 display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
283                        int32_t x, int32_t y, int32_t width, int32_t height);
284
285 void
286 window_set_buffer_type(struct window *window, enum window_buffer_type type);
287
288 int
289 window_is_fullscreen(struct window *window);
290
291 void
292 window_set_fullscreen(struct window *window, int fullscreen);
293
294 int
295 window_is_maximized(struct window *window);
296
297 void
298 window_set_maximized(struct window *window, int maximized);
299
300 void
301 window_set_user_data(struct window *window, void *data);
302
303 void *
304 window_get_user_data(struct window *window);
305
306 void
307 window_set_key_handler(struct window *window,
308                        window_key_handler_t handler);
309
310 void
311 window_set_keyboard_focus_handler(struct window *window,
312                                   window_keyboard_focus_handler_t handler);
313
314 void
315 window_set_data_handler(struct window *window,
316                         window_data_handler_t handler);
317
318 void
319 window_set_drop_handler(struct window *window,
320                         window_drop_handler_t handler);
321
322 void
323 window_set_close_handler(struct window *window,
324                          window_close_handler_t handler);
325 void
326 window_set_fullscreen_handler(struct window *window,
327                               window_fullscreen_handler_t handler);
328
329 void
330 window_set_title(struct window *window, const char *title);
331
332 const char *
333 window_get_title(struct window *window);
334
335 void
336 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y);
337
338 int
339 widget_set_tooltip(struct widget *parent, char *entry, float x, float y);
340
341 void
342 widget_destroy_tooltip(struct widget *parent);
343
344 struct widget *
345 widget_add_widget(struct widget *parent, void *data);
346
347 void
348 widget_destroy(struct widget *widget);
349 void
350 widget_get_allocation(struct widget *widget, struct rectangle *allocation);
351
352 void
353 widget_set_allocation(struct widget *widget,
354                       int32_t x, int32_t y, int32_t width, int32_t height);
355 void
356 widget_set_size(struct widget *widget, int32_t width, int32_t height);
357 void
358 widget_set_transparent(struct widget *widget, int transparent);
359 void
360 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height);
361
362 void *
363 widget_get_user_data(struct widget *widget);
364
365 void
366 widget_set_redraw_handler(struct widget *widget,
367                           widget_redraw_handler_t handler);
368 void
369 widget_set_resize_handler(struct widget *widget,
370                           widget_resize_handler_t handler);
371 void
372 widget_set_enter_handler(struct widget *widget,
373                          widget_enter_handler_t handler);
374 void
375 widget_set_leave_handler(struct widget *widget,
376                          widget_leave_handler_t handler);
377 void
378 widget_set_motion_handler(struct widget *widget,
379                           widget_motion_handler_t handler);
380 void
381 widget_set_button_handler(struct widget *widget,
382                           widget_button_handler_t handler);
383 void
384 widget_set_axis_handler(struct widget *widget,
385                         widget_axis_handler_t handler);
386
387 void
388 widget_schedule_redraw(struct widget *widget);
389
390 struct widget *
391 frame_create(struct window *window, void *data);
392 void
393 frame_set_child_size(struct widget *widget, int child_width, int child_height);
394
395 void
396 input_set_pointer_image(struct input *input, int pointer);
397
398 void
399 input_get_position(struct input *input, int32_t *x, int32_t *y);
400
401 #define MOD_SHIFT_MASK          0x01
402 #define MOD_ALT_MASK            0x02
403 #define MOD_CONTROL_MASK        0x04
404
405 uint32_t
406 input_get_modifiers(struct input *input);
407
408 void
409 input_grab(struct input *input, struct widget *widget, uint32_t button);
410
411 void
412 input_ungrab(struct input *input);
413
414 struct widget *
415 input_get_focus_widget(struct input *input);
416
417 struct display *
418 input_get_display(struct input *input);
419
420 struct wl_seat *
421 input_get_seat(struct input *input);
422
423 struct wl_data_device *
424 input_get_data_device(struct input *input);
425
426 void
427 input_set_selection(struct input *input,
428                     struct wl_data_source *source, uint32_t time);
429
430 void
431 input_accept(struct input *input, const char *type);
432
433
434 void
435 input_receive_drag_data(struct input *input, const char *mime_type,
436                         data_func_t func, void *user_data);
437
438 int
439 input_receive_selection_data(struct input *input, const char *mime_type,
440                              data_func_t func, void *data);
441 int
442 input_receive_selection_data_to_fd(struct input *input,
443                                    const char *mime_type, int fd);
444
445 void
446 output_set_user_data(struct output *output, void *data);
447
448 void *
449 output_get_user_data(struct output *output);
450
451 void
452 output_set_destroy_handler(struct output *output,
453                            display_output_handler_t handler);
454
455 void
456 output_get_allocation(struct output *output, struct rectangle *allocation);
457
458 struct wl_output *
459 output_get_wl_output(struct output *output);
460
461 void
462 keysym_modifiers_add(struct wl_array *modifiers_map,
463                      const char *name);
464
465 xkb_mod_mask_t
466 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
467                           const char *name);
468
469 #endif