client: Fix shell unstable version check
[profile/ivi/weston-ivi-shell.git] / clients / stacking.c
1 /*
2  * Copyright © 2013 Collabora Ltd.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #include "config.h"
24
25 #include <assert.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdbool.h>
30
31 #include <linux/input.h>
32 #include <cairo.h>
33
34 #include "window.h"
35
36 struct stacking {
37         struct display *display;
38         struct window *root_window;
39 };
40
41 static void
42 button_handler(struct widget *widget,
43                struct input *input, uint32_t time,
44                uint32_t button,
45                enum wl_pointer_button_state state, void *data);
46 static void
47 key_handler(struct window *window,
48             struct input *input, uint32_t time,
49             uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
50             void *data);
51 static void
52 keyboard_focus_handler(struct window *window,
53                        struct input *device, void *data);
54 static void
55 fullscreen_handler(struct window *window, void *data);
56 static void
57 redraw_handler(struct widget *widget, void *data);
58
59 /* Iff parent_window is set, the new window will be transient. */
60 static struct window *
61 new_window(struct stacking *stacking, struct window *parent_window)
62 {
63         struct window *new_window;
64         struct widget *new_widget;
65
66         new_window = window_create(stacking->display);
67         window_set_transient_for(new_window, parent_window);
68
69         new_widget = window_frame_create(new_window, new_window);
70
71         window_set_title(new_window, "Stacking Test");
72         window_set_key_handler(new_window, key_handler);
73         window_set_keyboard_focus_handler(new_window, keyboard_focus_handler);
74         window_set_fullscreen_handler(new_window, fullscreen_handler);
75         widget_set_button_handler(new_widget, button_handler);
76         widget_set_redraw_handler(new_widget, redraw_handler);
77         window_set_user_data(new_window, stacking);
78
79         window_schedule_resize(new_window, 300, 300);
80
81         return new_window;
82 }
83
84 static void
85 show_popup_cb(struct window *window, struct input *input, int index, void *data)
86 {
87         /* Ignore the selected menu item. */
88 }
89
90 static void
91 show_popup(struct stacking *stacking, struct input *input, uint32_t time,
92            struct window *window)
93 {
94         int32_t x, y;
95         static const char *entries[] = {
96                 "Test Entry",
97                 "Another Test Entry",
98         };
99
100         input_get_position(input, &x, &y);
101         window_show_menu(stacking->display, input, time, window, x, y,
102                          show_popup_cb, entries, ARRAY_LENGTH(entries));
103 }
104
105 static void
106 button_handler(struct widget *widget,
107                struct input *input, uint32_t time,
108                uint32_t button,
109                enum wl_pointer_button_state state, void *data)
110 {
111         struct stacking *stacking = data;
112
113         switch (button) {
114         case BTN_RIGHT:
115                 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
116                         show_popup(stacking, input, time,
117                                    widget_get_user_data(widget));
118                 break;
119
120         case BTN_LEFT:
121         default:
122                 break;
123         }
124 }
125
126 static void
127 key_handler(struct window *window,
128             struct input *input, uint32_t time,
129             uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
130             void *data)
131 {
132         struct stacking *stacking = data;
133
134         if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
135                 return;
136
137         switch (sym) {
138         case XKB_KEY_f:
139                 fullscreen_handler(window, data);
140                 break;
141
142         case XKB_KEY_m:
143                 window_set_maximized(window, !window_is_maximized(window));
144                 break;
145
146         case XKB_KEY_n:
147                 /* New top-level window. */
148                 new_window(stacking, NULL);
149                 break;
150
151         case XKB_KEY_p:
152                 show_popup(stacking, input, time, window);
153                 break;
154
155         case XKB_KEY_q:
156                 exit (0);
157                 break;
158
159         case XKB_KEY_t:
160                 /* New transient window. */
161                 new_window(stacking, window);
162                 break;
163
164         default:
165                 break;
166         }
167 }
168
169 static void
170 keyboard_focus_handler(struct window *window,
171                        struct input *device, void *data)
172 {
173         window_schedule_redraw(window);
174 }
175
176 static void
177 fullscreen_handler(struct window *window, void *data)
178 {
179         window_set_fullscreen(window, !window_is_fullscreen(window));
180 }
181
182 static void
183 draw_string(cairo_t *cr,
184             const char *fmt, ...) __attribute__((format (gnu_printf, 2, 3)));
185
186 static void
187 draw_string(cairo_t *cr,
188             const char *fmt, ...)
189 {
190         char buffer[4096];
191         char *p, *end;
192         va_list argp;
193         cairo_text_extents_t text_extents;
194         cairo_font_extents_t font_extents;
195
196         cairo_save(cr);
197
198         cairo_select_font_face(cr, "sans",
199                                CAIRO_FONT_SLANT_NORMAL,
200                                CAIRO_FONT_WEIGHT_NORMAL);
201         cairo_set_font_size(cr, 14);
202
203         cairo_font_extents(cr, &font_extents);
204
205         va_start(argp, fmt);
206
207         vsnprintf(buffer, sizeof(buffer), fmt, argp);
208
209         p = buffer;
210         while (*p) {
211                 end = strchr(p, '\n');
212                 if (end)
213                         *end = 0;
214
215                 cairo_show_text(cr, p);
216                 cairo_text_extents(cr, p, &text_extents);
217                 cairo_rel_move_to(cr, -text_extents.x_advance, font_extents.height);
218
219                 if (end)
220                         p = end + 1;
221                 else
222                         break;
223         }
224
225         va_end(argp);
226
227         cairo_restore(cr);
228 }
229
230 static void
231 set_window_background_colour(cairo_t *cr, struct window *window)
232 {
233         if (window_get_transient_for(window))
234                 cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 0.4);
235         else if (window_is_maximized(window))
236                 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.6);
237         else if (window_is_fullscreen(window))
238                 cairo_set_source_rgba(cr, 0.0, 1.0, 1.0, 0.6);
239         else
240                 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
241 }
242
243 static void
244 redraw_handler(struct widget *widget, void *data)
245 {
246         struct window *window;
247         struct rectangle allocation;
248         cairo_t *cr;
249
250         widget_get_allocation(widget, &allocation);
251         window = widget_get_user_data(widget);
252
253         cr = widget_cairo_create(widget);
254         cairo_translate(cr, allocation.x, allocation.y);
255
256         /* Draw background. */
257         cairo_push_group(cr);
258         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
259         set_window_background_colour(cr, window);
260         cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
261         cairo_fill(cr);
262
263         cairo_pop_group_to_source(cr);
264         cairo_paint(cr);
265
266         /* Print the instructions. */
267         cairo_move_to(cr, 5, 15);
268         cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
269
270         draw_string(cr,
271                     "Window: %p\n"
272                     "Fullscreen? %u\n"
273                     "Maximized? %u\n"
274                     "Transient? %u\n"
275                     "Keys: (f)ullscreen, (m)aximize,\n"
276                     "      (n)ew window, (p)opup,\n"
277                     "      (q)uit, (t)ransient window\n",
278                     window, window_is_fullscreen(window),
279                     window_is_maximized(window), window_get_transient_for(window) ? 1 : 0);
280
281         cairo_destroy(cr);
282 }
283
284 int
285 main(int argc, char *argv[])
286 {
287         struct stacking stacking;
288
289         memset(&stacking, 0, sizeof stacking);
290
291 #ifdef HAVE_PANGO
292         g_type_init();
293 #endif
294
295         stacking.display = display_create(&argc, argv);
296         if (stacking.display == NULL) {
297                 fprintf(stderr, "Failed to create display: %m\n");
298                 return -1;
299         }
300
301         display_set_user_data(stacking.display, &stacking);
302
303         stacking.root_window = new_window(&stacking, NULL);
304
305         display_run(stacking.display);
306
307         return 0;
308 }