blob: 4285a17194cd61c017482a4e5a4d5bd6c8f4fce6 [file] [log] [blame]
Philip Withnall17c2fb42013-11-25 18:01:34 +00001/*
2 * Copyright © 2013 Collabora Ltd.
3 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07004 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
Philip Withnall17c2fb42013-11-25 18:01:34 +000010 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070011 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
Philip Withnall17c2fb42013-11-25 18:01:34 +000022 */
23
24#include "config.h"
25
26#include <assert.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030027#include <stdint.h>
Philip Withnall17c2fb42013-11-25 18:01:34 +000028#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <stdbool.h>
32
33#include <linux/input.h>
34#include <cairo.h>
35
Jon Cruz35b2eaa2015-06-15 15:37:08 -070036#include "shared/helpers.h"
Philip Withnall17c2fb42013-11-25 18:01:34 +000037#include "window.h"
38
39struct stacking {
40 struct display *display;
41 struct window *root_window;
42};
43
44static void
45button_handler(struct widget *widget,
46 struct input *input, uint32_t time,
47 uint32_t button,
48 enum wl_pointer_button_state state, void *data);
49static void
50key_handler(struct window *window,
51 struct input *input, uint32_t time,
52 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
53 void *data);
54static void
Kristian Høgsbergacdae2e2013-12-05 15:14:45 -080055keyboard_focus_handler(struct window *window,
56 struct input *device, void *data);
57static void
Philip Withnall17c2fb42013-11-25 18:01:34 +000058fullscreen_handler(struct window *window, void *data);
59static void
60redraw_handler(struct widget *widget, void *data);
61
Jasper St. Pierre1e47a932013-12-09 15:20:16 -050062/* Iff parent_window is set, the new window will be transient. */
Philip Withnall17c2fb42013-11-25 18:01:34 +000063static struct window *
Jasper St. Pierre1e47a932013-12-09 15:20:16 -050064new_window(struct stacking *stacking, struct window *parent_window)
Philip Withnall17c2fb42013-11-25 18:01:34 +000065{
66 struct window *new_window;
67 struct widget *new_widget;
68
Jasper St. Pierreb0d604f2013-12-09 14:52:33 -050069 new_window = window_create(stacking->display);
Jasper St. Pierrec815d622014-04-10 18:37:54 -070070 window_set_parent(new_window, parent_window);
Jasper St. Pierre1e47a932013-12-09 15:20:16 -050071
Philip Withnall17c2fb42013-11-25 18:01:34 +000072 new_widget = window_frame_create(new_window, new_window);
73
74 window_set_title(new_window, "Stacking Test");
75 window_set_key_handler(new_window, key_handler);
Kristian Høgsbergacdae2e2013-12-05 15:14:45 -080076 window_set_keyboard_focus_handler(new_window, keyboard_focus_handler);
Philip Withnall17c2fb42013-11-25 18:01:34 +000077 window_set_fullscreen_handler(new_window, fullscreen_handler);
78 widget_set_button_handler(new_widget, button_handler);
79 widget_set_redraw_handler(new_widget, redraw_handler);
80 window_set_user_data(new_window, stacking);
81
82 window_schedule_resize(new_window, 300, 300);
83
84 return new_window;
85}
86
87static void
Jasper St. Pierredda93132014-03-13 12:06:00 -040088show_popup_cb(void *data, struct input *input, int index)
Philip Withnall17c2fb42013-11-25 18:01:34 +000089{
90 /* Ignore the selected menu item. */
91}
92
93static void
94show_popup(struct stacking *stacking, struct input *input, uint32_t time,
95 struct window *window)
96{
97 int32_t x, y;
98 static const char *entries[] = {
99 "Test Entry",
100 "Another Test Entry",
101 };
102
103 input_get_position(input, &x, &y);
104 window_show_menu(stacking->display, input, time, window, x, y,
105 show_popup_cb, entries, ARRAY_LENGTH(entries));
106}
107
108static void
109button_handler(struct widget *widget,
110 struct input *input, uint32_t time,
111 uint32_t button,
112 enum wl_pointer_button_state state, void *data)
113{
114 struct stacking *stacking = data;
115
116 switch (button) {
117 case BTN_RIGHT:
118 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
119 show_popup(stacking, input, time,
120 widget_get_user_data(widget));
121 break;
122
123 case BTN_LEFT:
124 default:
125 break;
126 }
127}
128
129static void
130key_handler(struct window *window,
131 struct input *input, uint32_t time,
132 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
133 void *data)
134{
135 struct stacking *stacking = data;
136
137 if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
138 return;
139
140 switch (sym) {
141 case XKB_KEY_f:
142 fullscreen_handler(window, data);
143 break;
144
145 case XKB_KEY_m:
146 window_set_maximized(window, !window_is_maximized(window));
147 break;
148
149 case XKB_KEY_n:
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500150 /* New top-level window. */
151 new_window(stacking, NULL);
Philip Withnall17c2fb42013-11-25 18:01:34 +0000152 break;
153
154 case XKB_KEY_p:
155 show_popup(stacking, input, time, window);
156 break;
157
158 case XKB_KEY_q:
159 exit (0);
160 break;
161
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500162 case XKB_KEY_t:
163 /* New transient window. */
164 new_window(stacking, window);
165 break;
166
Philip Withnall17c2fb42013-11-25 18:01:34 +0000167 default:
168 break;
169 }
170}
171
172static void
Kristian Høgsbergacdae2e2013-12-05 15:14:45 -0800173keyboard_focus_handler(struct window *window,
174 struct input *device, void *data)
175{
176 window_schedule_redraw(window);
177}
178
179static void
Philip Withnall17c2fb42013-11-25 18:01:34 +0000180fullscreen_handler(struct window *window, void *data)
181{
182 window_set_fullscreen(window, !window_is_fullscreen(window));
183}
184
185static void
186draw_string(cairo_t *cr,
187 const char *fmt, ...) __attribute__((format (gnu_printf, 2, 3)));
188
189static void
190draw_string(cairo_t *cr,
191 const char *fmt, ...)
192{
193 char buffer[4096];
194 char *p, *end;
195 va_list argp;
196 cairo_text_extents_t text_extents;
197 cairo_font_extents_t font_extents;
198
199 cairo_save(cr);
200
201 cairo_select_font_face(cr, "sans",
202 CAIRO_FONT_SLANT_NORMAL,
203 CAIRO_FONT_WEIGHT_NORMAL);
204 cairo_set_font_size(cr, 14);
205
206 cairo_font_extents(cr, &font_extents);
207
208 va_start(argp, fmt);
209
210 vsnprintf(buffer, sizeof(buffer), fmt, argp);
211
212 p = buffer;
213 while (*p) {
214 end = strchr(p, '\n');
215 if (end)
216 *end = 0;
217
218 cairo_show_text(cr, p);
219 cairo_text_extents(cr, p, &text_extents);
220 cairo_rel_move_to(cr, -text_extents.x_advance, font_extents.height);
221
222 if (end)
223 p = end + 1;
224 else
225 break;
226 }
227
228 va_end(argp);
229
230 cairo_restore(cr);
231}
232
233static void
234set_window_background_colour(cairo_t *cr, struct window *window)
235{
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700236 if (window_get_parent(window))
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500237 cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 0.4);
238 else if (window_is_maximized(window))
Philip Withnall17c2fb42013-11-25 18:01:34 +0000239 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.6);
240 else if (window_is_fullscreen(window))
241 cairo_set_source_rgba(cr, 0.0, 1.0, 1.0, 0.6);
242 else
243 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
244}
245
246static void
247redraw_handler(struct widget *widget, void *data)
248{
249 struct window *window;
250 struct rectangle allocation;
251 cairo_t *cr;
252
253 widget_get_allocation(widget, &allocation);
254 window = widget_get_user_data(widget);
255
256 cr = widget_cairo_create(widget);
257 cairo_translate(cr, allocation.x, allocation.y);
258
259 /* Draw background. */
Philip Withnall17c2fb42013-11-25 18:01:34 +0000260 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
261 set_window_background_colour(cr, window);
262 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
263 cairo_fill(cr);
264
Philip Withnall17c2fb42013-11-25 18:01:34 +0000265 /* Print the instructions. */
266 cairo_move_to(cr, 5, 15);
267 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
268
269 draw_string(cr,
270 "Window: %p\n"
271 "Fullscreen? %u\n"
272 "Maximized? %u\n"
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500273 "Transient? %u\n"
Philip Withnall17c2fb42013-11-25 18:01:34 +0000274 "Keys: (f)ullscreen, (m)aximize,\n"
275 " (n)ew window, (p)opup,\n"
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500276 " (q)uit, (t)ransient window\n",
Philip Withnall17c2fb42013-11-25 18:01:34 +0000277 window, window_is_fullscreen(window),
Jasper St. Pierrec815d622014-04-10 18:37:54 -0700278 window_is_maximized(window), window_get_parent(window) ? 1 : 0);
Philip Withnall17c2fb42013-11-25 18:01:34 +0000279
280 cairo_destroy(cr);
281}
282
283int
284main(int argc, char *argv[])
285{
286 struct stacking stacking;
287
288 memset(&stacking, 0, sizeof stacking);
289
290#ifdef HAVE_PANGO
291 g_type_init();
292#endif
293
294 stacking.display = display_create(&argc, argv);
295 if (stacking.display == NULL) {
296 fprintf(stderr, "Failed to create display: %m\n");
297 return -1;
298 }
299
300 display_set_user_data(stacking.display, &stacking);
301
Jasper St. Pierre1e47a932013-12-09 15:20:16 -0500302 stacking.root_window = new_window(&stacking, NULL);
Philip Withnall17c2fb42013-11-25 18:01:34 +0000303
304 display_run(stacking.display);
305
vivek31732f72014-05-15 18:58:16 +0530306 window_destroy(stacking.root_window);
307 display_destroy(stacking.display);
308
Philip Withnall17c2fb42013-11-25 18:01:34 +0000309 return 0;
310}