wlt: toolkit: add helpers to create/manage selections
[platform/upstream/kmscon.git] / src / wlt_toolkit.h
1 /*
2  * wlt - Toolkit Helper
3  *
4  * Copyright (c) 2012 David Herrmann <dh.herrmann@googlemail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files
8  * (the "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 /*
27  * Wayland Terminal toolkit helpers
28  */
29
30 #ifndef WLT_TOOLKIT_H
31 #define WLT_TOOLKIT_H
32
33 #include <stdbool.h>
34 #include <stdlib.h>
35 #include "eloop.h"
36
37 struct wlt_display;
38 struct wlt_window;
39 struct wlt_widget;
40
41 enum wlt_display_event {
42         WLT_DISPLAY_HUP,
43         WLT_DISPLAY_READY,
44 };
45
46 struct wlt_rect {
47         unsigned int x;
48         unsigned int y;
49         unsigned int width;
50         unsigned int height;
51 };
52
53 struct wlt_shm_buffer {
54         uint8_t *data;
55         unsigned int width;
56         unsigned int height;
57         unsigned int stride;
58 };
59
60 enum cursor_type {
61         WLT_CURSOR_NONE,
62         WLT_CURSOR_TOP,
63         WLT_CURSOR_BOTTOM,
64         WLT_CURSOR_LEFT,
65         WLT_CURSOR_RIGHT,
66         WLT_CURSOR_TOP_LEFT,
67         WLT_CURSOR_TOP_RIGHT,
68         WLT_CURSOR_BOTTOM_LEFT,
69         WLT_CURSOR_BOTTOM_RIGHT,
70         WLT_CURSOR_DRAGGING,
71         WLT_CURSOR_LEFT_PTR,
72         WLT_CURSOR_IBEAM,
73         WLT_CURSOR_NUM,
74 };
75
76 #define WLT_WINDOW_MAXIMIZED            0x01
77 #define WLT_WINDOW_FULLSCREEN           0x02
78
79 typedef void (*wlt_display_cb) (struct wlt_display *disp,
80                                 unsigned int event,
81                                 void *data);
82 typedef void (*wlt_window_close_cb) (struct wlt_window *wnd, void *data);
83 typedef void (*wlt_widget_redraw_cb) (struct wlt_widget *widget,
84                                       unsigned int flags,
85                                       void *data);
86 typedef void (*wlt_widget_destroy_cb) (struct wlt_widget *widget,
87                                        void *data);
88 typedef void (*wlt_widget_prepare_resize_cb) (struct wlt_widget *widget,
89                                               unsigned int flags,
90                                               unsigned int width,
91                                               unsigned int height,
92                                               unsigned int *min_width,
93                                               unsigned int *min_height,
94                                               unsigned int *new_width,
95                                               unsigned int *new_height,
96                                               void *data);
97 typedef void (*wlt_widget_resize_cb) (struct wlt_widget *widget,
98                                       unsigned int flags,
99                                       struct wlt_rect *allocation,
100                                       void *data);
101 typedef void (*wlt_widget_pointer_enter_cb) (struct wlt_widget *widget,
102                                              unsigned int x,
103                                              unsigned int y,
104                                              void *data);
105 typedef void (*wlt_widget_pointer_leave_cb) (struct wlt_widget *widget,
106                                              void *data);
107 typedef void (*wlt_widget_pointer_motion_cb) (struct wlt_widget *widget,
108                                               unsigned int x,
109                                               unsigned int y,
110                                               void *data);
111 typedef void (*wlt_widget_pointer_button_cb) (struct wlt_widget *widget,
112                                               uint32_t button,
113                                               uint32_t state,
114                                               void *data);
115 typedef bool (*wlt_widget_keyboard_cb) (struct wlt_widget *widget,
116                                         unsigned int mods,
117                                         uint32_t key,
118                                         uint32_t state,
119                                         bool handled,
120                                         void *data);
121
122 int wlt_display_new(struct wlt_display **out,
123                     struct ev_eloop *eloop);
124 void wlt_display_ref(struct wlt_display *disp);
125 void wlt_display_unref(struct wlt_display *disp);
126
127 int wlt_display_create_window(struct wlt_display *disp,
128                               struct wlt_window **out,
129                               unsigned int width,
130                               unsigned int height,
131                               void *data);
132 int wlt_display_register_cb(struct wlt_display *disp,
133                             wlt_display_cb cb, void *data);
134 void wlt_display_unregister_cb(struct wlt_display *disp,
135                                wlt_display_cb cb, void *data);
136
137 int wlt_display_get_selection_fd(struct wlt_display *disp, const char *mime);
138 int wlt_display_get_selection_to_fd(struct wlt_display *disp, const char *mime,
139                                     int output_fd);
140 int wlt_display_new_data_source(struct wlt_display *disp,
141                                 struct wl_data_source **out);
142 void wlt_display_set_selection(struct wlt_display *disp,
143                                struct wl_data_source *selection);
144
145 void wlt_window_ref(struct wlt_window *wnd);
146 void wlt_window_unref(struct wlt_window *wnd);
147
148 int wlt_window_create_widget(struct wlt_window *wnd,
149                              struct wlt_widget **out,
150                              void *data);
151 void wlt_window_schedule_redraw(struct wlt_window *wnd);
152 void wlt_window_damage(struct wlt_window *wnd,
153                        struct wlt_rect *damage);
154 void wlt_window_get_buffer(struct wlt_window *wnd,
155                            const struct wlt_rect *alloc,
156                            struct wlt_shm_buffer *buf);
157 void wlt_window_move(struct wlt_window *wnd);
158 void wlt_window_resize(struct wlt_window *wnd, uint32_t edges);
159 int wlt_window_set_size(struct wlt_window *wnd,
160                         unsigned int width, unsigned int height);
161 void wlt_window_set_cursor(struct wlt_window *wnd, unsigned int cursor);
162 void wlt_window_set_close_cb(struct wlt_window *wnd,
163                              wlt_window_close_cb cb);
164 void wlt_window_close(struct wlt_window *wnd);
165 void wlt_window_toggle_maximize(struct wlt_window *wnd);
166 void wlt_window_toggle_fullscreen(struct wlt_window *wnd);
167 struct ev_eloop *wlt_window_get_eloop(struct wlt_window *wnd);
168 struct wlt_display *wlt_window_get_display(struct wlt_window *wnd);
169
170 void wlt_widget_destroy(struct wlt_widget *widget);
171 struct wlt_window *wlt_widget_get_window(struct wlt_widget *widget);
172 void wlt_widget_set_redraw_cb(struct wlt_widget *widget,
173                               wlt_widget_redraw_cb cb);
174 void wlt_widget_set_destroy_cb(struct wlt_widget *widget,
175                                wlt_widget_destroy_cb cb);
176 void wlt_widget_set_resize_cb(struct wlt_widget *widget,
177                               wlt_widget_prepare_resize_cb prepare_cb,
178                               wlt_widget_resize_cb cb);
179 void wlt_widget_set_pointer_cb(struct wlt_widget *widget,
180                                wlt_widget_pointer_enter_cb enter_cb,
181                                wlt_widget_pointer_leave_cb leave_cb,
182                                wlt_widget_pointer_motion_cb motion_cb,
183                                wlt_widget_pointer_button_cb button_cb);
184 void wlt_widget_set_keyboard_cb(struct wlt_widget *widget,
185                                 wlt_widget_keyboard_cb cb);
186
187 static inline bool wlt_rect_contains(struct wlt_rect *rect,
188                                      unsigned int x,
189                                      unsigned int y)
190 {
191         if (x < rect->x || y < rect->y)
192                 return false;
193         if (x >= rect->x + rect->width)
194                 return false;
195         if (y >= rect->y + rect->height)
196                 return false;
197         return true;
198 }
199
200 #endif /* WLT_TOOLKIT_H */