2 * Copyright © 2008 Kristian Høgsberg
3 * Copyright © 2012 Intel Corporation
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
32 #include <linux/input.h>
33 #include <wayland-client.h>
37 struct display *display;
38 struct window *window;
39 struct widget *widget;
42 float pointer_x, pointer_y;
43 enum wl_shell_surface_fullscreen_method fullscreen_method;
47 fullscreen_handler(struct window *window, void *data)
49 struct fullscreen *fullscreen = data;
51 fullscreen->fullscreen ^= 1;
52 window_set_fullscreen(window, fullscreen->fullscreen);
56 resize_handler(struct widget *widget, int width, int height, void *data)
58 struct fullscreen *fullscreen = data;
60 widget_set_size(widget, fullscreen->width, fullscreen->height);
64 draw_string(cairo_t *cr,
70 cairo_text_extents_t text_extents;
71 cairo_font_extents_t font_extents;
75 cairo_select_font_face(cr, "sans",
76 CAIRO_FONT_SLANT_NORMAL,
77 CAIRO_FONT_WEIGHT_NORMAL);
78 cairo_set_font_size(cr, 14);
80 cairo_font_extents (cr, &font_extents);
84 vsnprintf(buffer, sizeof(buffer), fmt, argp);
88 end = strchr(p, '\n');
92 cairo_show_text(cr, p);
93 cairo_text_extents (cr, p, &text_extents);
94 cairo_rel_move_to (cr, -text_extents.x_advance, font_extents.height);
109 redraw_handler(struct widget *widget, void *data)
111 struct fullscreen *fullscreen = data;
112 struct rectangle allocation;
113 cairo_surface_t *surface;
117 const char *method_name[] = { "default", "scale", "driver", "fill" };
119 surface = window_get_surface(fullscreen->window);
120 if (surface == NULL ||
121 cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
122 fprintf(stderr, "failed to create cairo egl surface\n");
126 widget_get_allocation(fullscreen->widget, &allocation);
128 cr = widget_cairo_create(widget);
130 cairo_set_source_rgb(cr, 0, 0, 0);
133 cairo_set_source_rgb(cr, 0, 0, 1);
134 cairo_set_line_width (cr, 10);
135 cairo_rectangle(cr, 5, 5, allocation.width - 10, allocation.height - 10);
141 cairo_set_source_rgb(cr, 1, 1, 1);
144 "Surface size: %d, %d\n"
145 "Scale: %d, transform: %d\n"
147 "Fullscreen: %d, method: %s\n"
148 "Keys: (s)cale, (t)ransform, si(z)e, (m)ethod, (f)ullscreen, (q)uit\n",
149 fullscreen->width, fullscreen->height,
150 window_get_buffer_scale (fullscreen->window),
151 window_get_buffer_transform (fullscreen->window),
152 fullscreen->pointer_x, fullscreen->pointer_y,
153 fullscreen->fullscreen, method_name[fullscreen->fullscreen_method]);
157 while (y + 60 < fullscreen->height) {
158 border = (i++ % 2 == 0) ? 1 : 0.5;
161 cairo_set_line_width (cr, border);
162 while (x + 70 < fullscreen->width) {
163 if (fullscreen->pointer_x >= x && fullscreen->pointer_x < x + 50 &&
164 fullscreen->pointer_y >= y && fullscreen->pointer_y < y + 40) {
165 cairo_set_source_rgb(cr, 1, 0, 0);
171 cairo_set_source_rgb(cr, 0, 1, 0);
173 x + border/2.0, y + border/2.0,
186 key_handler(struct window *window, struct input *input, uint32_t time,
187 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
190 struct fullscreen *fullscreen = data;
191 int transform, scale;
192 static int current_size = 0;
193 int widths[] = { 640, 320, 800, 400 };
194 int heights[] = { 480, 240, 600, 300 };
196 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
201 transform = window_get_buffer_transform (window);
202 transform = (transform + 1) % 8;
203 window_set_buffer_transform(window, transform);
204 window_schedule_redraw(window);
208 scale = window_get_buffer_scale (window);
213 window_set_buffer_scale(window, scale);
214 window_schedule_redraw(window);
218 current_size = (current_size + 1) % 4;
219 fullscreen->width = widths[current_size];
220 fullscreen->height = heights[current_size];
221 window_schedule_resize(fullscreen->window,
222 fullscreen->width, fullscreen->height);
226 fullscreen->fullscreen_method = (fullscreen->fullscreen_method + 1) % 4;
227 window_set_fullscreen_method(fullscreen->window,
228 fullscreen->fullscreen_method);
229 window_schedule_redraw(window);
233 fullscreen->fullscreen ^= 1;
234 window_set_fullscreen(window, fullscreen->fullscreen);
244 motion_handler(struct widget *widget,
250 struct fullscreen *fullscreen = data;
252 fullscreen->pointer_x = x;
253 fullscreen->pointer_y = y;
255 widget_schedule_redraw(widget);
261 button_handler(struct widget *widget,
262 struct input *input, uint32_t time,
263 uint32_t button, enum wl_pointer_button_state state, void *data)
265 struct fullscreen *fullscreen = data;
269 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
270 window_move(fullscreen->window, input,
271 display_get_serial(fullscreen->display));
274 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
275 window_show_frame_menu(fullscreen->window, input, time);
281 touch_handler(struct widget *widget, struct input *input,
282 uint32_t serial, uint32_t time, int32_t id,
283 float x, float y, void *data)
285 struct fullscreen *fullscreen = data;
286 window_move(fullscreen->window, input, display_get_serial(fullscreen->display));
290 usage(int error_code)
292 fprintf(stderr, "Usage: fullscreen [OPTIONS]\n\n"
293 " -w <width>\tSet window width to <width>\n"
294 " -h <height>\tSet window height to <height>\n"
295 " --help\tShow this help text\n\n");
300 int main(int argc, char *argv[])
302 struct fullscreen fullscreen;
306 fullscreen.width = 640;
307 fullscreen.height = 480;
308 fullscreen.fullscreen = 0;
309 fullscreen.fullscreen_method =
310 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
312 for (i = 1; i < argc; i++) {
313 if (strcmp(argv[i], "-w") == 0) {
317 fullscreen.width = atol(argv[i]);
318 } else if (strcmp(argv[i], "-h") == 0) {
322 fullscreen.height = atol(argv[i]);
323 } else if (strcmp(argv[i], "--help") == 0)
329 d = display_create(&argc, argv);
331 fprintf(stderr, "failed to create display: %m\n");
335 fullscreen.display = d;
336 fullscreen.window = window_create(d);
338 window_add_widget(fullscreen.window, &fullscreen);
340 window_set_title(fullscreen.window, "Fullscreen");
341 window_set_fullscreen_method(fullscreen.window,
342 fullscreen.fullscreen_method);
344 widget_set_transparent(fullscreen.widget, 0);
345 widget_set_default_cursor(fullscreen.widget, CURSOR_LEFT_PTR);
347 widget_set_resize_handler(fullscreen.widget, resize_handler);
348 widget_set_redraw_handler(fullscreen.widget, redraw_handler);
349 widget_set_button_handler(fullscreen.widget, button_handler);
350 widget_set_motion_handler(fullscreen.widget, motion_handler);
352 widget_set_touch_down_handler(fullscreen.widget, touch_handler);
354 window_set_key_handler(fullscreen.window, key_handler);
355 window_set_fullscreen_handler(fullscreen.window, fullscreen_handler);
357 window_set_user_data(fullscreen.window, &fullscreen);
358 /* Hack to set minimum allocation so we can shrink later */
359 window_schedule_resize(fullscreen.window,
361 window_schedule_resize(fullscreen.window,
362 fullscreen.width, fullscreen.height);