blob: 45eb81e9839b5a38bca937707c4c53d431de6c69 [file] [log] [blame]
Philip Withnall17c2fb42013-11-25 18:01:34 +00001/*
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
36struct stacking {
37 struct display *display;
38 struct window *root_window;
39};
40
41static void
42button_handler(struct widget *widget,
43 struct input *input, uint32_t time,
44 uint32_t button,
45 enum wl_pointer_button_state state, void *data);
46static void
47key_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);
51static void
52fullscreen_handler(struct window *window, void *data);
53static void
54redraw_handler(struct widget *widget, void *data);
55
56/* Iff parent_window is set, the new window will be transient. */
57static struct window *
58new_window(struct stacking *stacking, struct window *parent_window)
59{
60 struct window *new_window;
61 struct widget *new_widget;
62
63 if (parent_window == NULL) {
64 new_window = window_create(stacking->display);
65 } else {
66 new_window = window_create_transient(stacking->display,
67 parent_window, 50, 50, 0);
68 }
69
70 new_widget = window_frame_create(new_window, new_window);
71
72 window_set_title(new_window, "Stacking Test");
73 window_set_key_handler(new_window, key_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
84static void
85show_popup_cb(struct window *window, struct input *input, int index, void *data)
86{
87 /* Ignore the selected menu item. */
88}
89
90static void
91show_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
105static void
106button_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
126static void
127key_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
169static void
170fullscreen_handler(struct window *window, void *data)
171{
172 window_set_fullscreen(window, !window_is_fullscreen(window));
173}
174
175static void
176draw_string(cairo_t *cr,
177 const char *fmt, ...) __attribute__((format (gnu_printf, 2, 3)));
178
179static void
180draw_string(cairo_t *cr,
181 const char *fmt, ...)
182{
183 char buffer[4096];
184 char *p, *end;
185 va_list argp;
186 cairo_text_extents_t text_extents;
187 cairo_font_extents_t font_extents;
188
189 cairo_save(cr);
190
191 cairo_select_font_face(cr, "sans",
192 CAIRO_FONT_SLANT_NORMAL,
193 CAIRO_FONT_WEIGHT_NORMAL);
194 cairo_set_font_size(cr, 14);
195
196 cairo_font_extents(cr, &font_extents);
197
198 va_start(argp, fmt);
199
200 vsnprintf(buffer, sizeof(buffer), fmt, argp);
201
202 p = buffer;
203 while (*p) {
204 end = strchr(p, '\n');
205 if (end)
206 *end = 0;
207
208 cairo_show_text(cr, p);
209 cairo_text_extents(cr, p, &text_extents);
210 cairo_rel_move_to(cr, -text_extents.x_advance, font_extents.height);
211
212 if (end)
213 p = end + 1;
214 else
215 break;
216 }
217
218 va_end(argp);
219
220 cairo_restore(cr);
221}
222
223static void
224set_window_background_colour(cairo_t *cr, struct window *window)
225{
226 if (window_is_transient(window))
227 cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 0.4);
228 else if (window_is_maximized(window))
229 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.6);
230 else if (window_is_fullscreen(window))
231 cairo_set_source_rgba(cr, 0.0, 1.0, 1.0, 0.6);
232 else
233 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
234}
235
236static void
237redraw_handler(struct widget *widget, void *data)
238{
239 struct window *window;
240 struct rectangle allocation;
241 cairo_t *cr;
242
243 widget_get_allocation(widget, &allocation);
244 window = widget_get_user_data(widget);
245
246 cr = widget_cairo_create(widget);
247 cairo_translate(cr, allocation.x, allocation.y);
248
249 /* Draw background. */
250 cairo_push_group(cr);
251 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
252 set_window_background_colour(cr, window);
253 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
254 cairo_fill(cr);
255
256 cairo_pop_group_to_source(cr);
257 cairo_paint(cr);
258
259 /* Print the instructions. */
260 cairo_move_to(cr, 5, 15);
261 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
262
263 draw_string(cr,
264 "Window: %p\n"
265 "Fullscreen? %u\n"
266 "Maximized? %u\n"
267 "Transient? %u\n"
268 "Keys: (f)ullscreen, (m)aximize,\n"
269 " (n)ew window, (p)opup,\n"
270 " (q)uit, (t)ransient window\n",
271 window, window_is_fullscreen(window),
272 window_is_maximized(window), window_is_transient(window));
273
274 cairo_destroy(cr);
275}
276
277int
278main(int argc, char *argv[])
279{
280 struct stacking stacking;
281
282 memset(&stacking, 0, sizeof stacking);
283
284#ifdef HAVE_PANGO
285 g_type_init();
286#endif
287
288 stacking.display = display_create(&argc, argv);
289 if (stacking.display == NULL) {
290 fprintf(stderr, "Failed to create display: %m\n");
291 return -1;
292 }
293
294 display_set_user_data(stacking.display, &stacking);
295
296 stacking.root_window = new_window(&stacking, NULL);
297
298 display_run(stacking.display);
299
300 return 0;
301}