blob: bfccbc668e1f5b6bb2e3bf2c96d7a05ae0c45e3f [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
Kristian Høgsbergacdae2e2013-12-05 15:14:45 -080052keyboard_focus_handler(struct window *window,
53 struct input *device, void *data);
54static void
Philip Withnall17c2fb42013-11-25 18:01:34 +000055fullscreen_handler(struct window *window, void *data);
56static void
57redraw_handler(struct widget *widget, void *data);
58
Philip Withnall17c2fb42013-11-25 18:01:34 +000059static struct window *
Jasper St. Pierreb0d604f2013-12-09 14:52:33 -050060new_window(struct stacking *stacking)
Philip Withnall17c2fb42013-11-25 18:01:34 +000061{
62 struct window *new_window;
63 struct widget *new_widget;
64
Jasper St. Pierreb0d604f2013-12-09 14:52:33 -050065 new_window = window_create(stacking->display);
Philip Withnall17c2fb42013-11-25 18:01:34 +000066 new_widget = window_frame_create(new_window, new_window);
67
68 window_set_title(new_window, "Stacking Test");
69 window_set_key_handler(new_window, key_handler);
Kristian Høgsbergacdae2e2013-12-05 15:14:45 -080070 window_set_keyboard_focus_handler(new_window, keyboard_focus_handler);
Philip Withnall17c2fb42013-11-25 18:01:34 +000071 window_set_fullscreen_handler(new_window, fullscreen_handler);
72 widget_set_button_handler(new_widget, button_handler);
73 widget_set_redraw_handler(new_widget, redraw_handler);
74 window_set_user_data(new_window, stacking);
75
76 window_schedule_resize(new_window, 300, 300);
77
78 return new_window;
79}
80
81static void
82show_popup_cb(struct window *window, struct input *input, int index, void *data)
83{
84 /* Ignore the selected menu item. */
85}
86
87static void
88show_popup(struct stacking *stacking, struct input *input, uint32_t time,
89 struct window *window)
90{
91 int32_t x, y;
92 static const char *entries[] = {
93 "Test Entry",
94 "Another Test Entry",
95 };
96
97 input_get_position(input, &x, &y);
98 window_show_menu(stacking->display, input, time, window, x, y,
99 show_popup_cb, entries, ARRAY_LENGTH(entries));
100}
101
102static void
103button_handler(struct widget *widget,
104 struct input *input, uint32_t time,
105 uint32_t button,
106 enum wl_pointer_button_state state, void *data)
107{
108 struct stacking *stacking = data;
109
110 switch (button) {
111 case BTN_RIGHT:
112 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
113 show_popup(stacking, input, time,
114 widget_get_user_data(widget));
115 break;
116
117 case BTN_LEFT:
118 default:
119 break;
120 }
121}
122
123static void
124key_handler(struct window *window,
125 struct input *input, uint32_t time,
126 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
127 void *data)
128{
129 struct stacking *stacking = data;
130
131 if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
132 return;
133
134 switch (sym) {
135 case XKB_KEY_f:
136 fullscreen_handler(window, data);
137 break;
138
139 case XKB_KEY_m:
140 window_set_maximized(window, !window_is_maximized(window));
141 break;
142
143 case XKB_KEY_n:
Jasper St. Pierreb0d604f2013-12-09 14:52:33 -0500144 new_window(stacking);
Philip Withnall17c2fb42013-11-25 18:01:34 +0000145 break;
146
147 case XKB_KEY_p:
148 show_popup(stacking, input, time, window);
149 break;
150
151 case XKB_KEY_q:
152 exit (0);
153 break;
154
Philip Withnall17c2fb42013-11-25 18:01:34 +0000155 default:
156 break;
157 }
158}
159
160static void
Kristian Høgsbergacdae2e2013-12-05 15:14:45 -0800161keyboard_focus_handler(struct window *window,
162 struct input *device, void *data)
163{
164 window_schedule_redraw(window);
165}
166
167static void
Philip Withnall17c2fb42013-11-25 18:01:34 +0000168fullscreen_handler(struct window *window, void *data)
169{
170 window_set_fullscreen(window, !window_is_fullscreen(window));
171}
172
173static void
174draw_string(cairo_t *cr,
175 const char *fmt, ...) __attribute__((format (gnu_printf, 2, 3)));
176
177static void
178draw_string(cairo_t *cr,
179 const char *fmt, ...)
180{
181 char buffer[4096];
182 char *p, *end;
183 va_list argp;
184 cairo_text_extents_t text_extents;
185 cairo_font_extents_t font_extents;
186
187 cairo_save(cr);
188
189 cairo_select_font_face(cr, "sans",
190 CAIRO_FONT_SLANT_NORMAL,
191 CAIRO_FONT_WEIGHT_NORMAL);
192 cairo_set_font_size(cr, 14);
193
194 cairo_font_extents(cr, &font_extents);
195
196 va_start(argp, fmt);
197
198 vsnprintf(buffer, sizeof(buffer), fmt, argp);
199
200 p = buffer;
201 while (*p) {
202 end = strchr(p, '\n');
203 if (end)
204 *end = 0;
205
206 cairo_show_text(cr, p);
207 cairo_text_extents(cr, p, &text_extents);
208 cairo_rel_move_to(cr, -text_extents.x_advance, font_extents.height);
209
210 if (end)
211 p = end + 1;
212 else
213 break;
214 }
215
216 va_end(argp);
217
218 cairo_restore(cr);
219}
220
221static void
222set_window_background_colour(cairo_t *cr, struct window *window)
223{
Jasper St. Pierreb0d604f2013-12-09 14:52:33 -0500224 if (window_is_maximized(window))
Philip Withnall17c2fb42013-11-25 18:01:34 +0000225 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.6);
226 else if (window_is_fullscreen(window))
227 cairo_set_source_rgba(cr, 0.0, 1.0, 1.0, 0.6);
228 else
229 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
230}
231
232static void
233redraw_handler(struct widget *widget, void *data)
234{
235 struct window *window;
236 struct rectangle allocation;
237 cairo_t *cr;
238
239 widget_get_allocation(widget, &allocation);
240 window = widget_get_user_data(widget);
241
242 cr = widget_cairo_create(widget);
243 cairo_translate(cr, allocation.x, allocation.y);
244
245 /* Draw background. */
246 cairo_push_group(cr);
247 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
248 set_window_background_colour(cr, window);
249 cairo_rectangle(cr, 0, 0, allocation.width, allocation.height);
250 cairo_fill(cr);
251
252 cairo_pop_group_to_source(cr);
253 cairo_paint(cr);
254
255 /* Print the instructions. */
256 cairo_move_to(cr, 5, 15);
257 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
258
259 draw_string(cr,
260 "Window: %p\n"
261 "Fullscreen? %u\n"
262 "Maximized? %u\n"
Philip Withnall17c2fb42013-11-25 18:01:34 +0000263 "Keys: (f)ullscreen, (m)aximize,\n"
264 " (n)ew window, (p)opup,\n"
Jasper St. Pierreb0d604f2013-12-09 14:52:33 -0500265 " (q)uit\n",
Philip Withnall17c2fb42013-11-25 18:01:34 +0000266 window, window_is_fullscreen(window),
Jasper St. Pierreb0d604f2013-12-09 14:52:33 -0500267 window_is_maximized(window));
Philip Withnall17c2fb42013-11-25 18:01:34 +0000268
269 cairo_destroy(cr);
270}
271
272int
273main(int argc, char *argv[])
274{
275 struct stacking stacking;
276
277 memset(&stacking, 0, sizeof stacking);
278
279#ifdef HAVE_PANGO
280 g_type_init();
281#endif
282
283 stacking.display = display_create(&argc, argv);
284 if (stacking.display == NULL) {
285 fprintf(stderr, "Failed to create display: %m\n");
286 return -1;
287 }
288
289 display_set_user_data(stacking.display, &stacking);
290
Jasper St. Pierreb0d604f2013-12-09 14:52:33 -0500291 stacking.root_window = new_window(&stacking);
Philip Withnall17c2fb42013-11-25 18:01:34 +0000292
293 display_run(stacking.display);
294
295 return 0;
296}