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;
46 fullscreen_handler(struct window *window, void *data)
48 struct fullscreen *fullscreen = data;
50 fullscreen->fullscreen ^= 1;
51 window_set_fullscreen(window, fullscreen->fullscreen);
55 resize_handler(struct widget *widget, int width, int height, void *data)
57 struct fullscreen *fullscreen = data;
59 widget_set_size(widget, fullscreen->width, fullscreen->height);
63 draw_string(cairo_t *cr,
69 cairo_text_extents_t text_extents;
70 cairo_font_extents_t font_extents;
74 cairo_select_font_face(cr, "sans",
75 CAIRO_FONT_SLANT_NORMAL,
76 CAIRO_FONT_WEIGHT_NORMAL);
77 cairo_set_font_size(cr, 14);
79 cairo_font_extents (cr, &font_extents);
83 vsnprintf(buffer, sizeof(buffer), fmt, argp);
87 end = strchr(p, '\n');
91 cairo_show_text(cr, p);
92 cairo_text_extents (cr, p, &text_extents);
93 cairo_rel_move_to (cr, -text_extents.x_advance, font_extents.height);
108 redraw_handler(struct widget *widget, void *data)
110 struct fullscreen *fullscreen = data;
111 struct rectangle allocation;
112 cairo_surface_t *surface;
117 surface = window_get_surface(fullscreen->window);
118 if (surface == NULL ||
119 cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
120 fprintf(stderr, "failed to create cairo egl surface\n");
124 widget_get_allocation(fullscreen->widget, &allocation);
126 cr = widget_cairo_create(widget);
128 cairo_set_source_rgb(cr, 0, 0, 0);
131 cairo_set_source_rgb(cr, 0, 0, 1);
132 cairo_set_line_width (cr, 10);
133 cairo_rectangle(cr, 5, 5, allocation.width - 10, allocation.height - 10);
139 cairo_set_source_rgb(cr, 1, 1, 1);
142 "Surface size: %d, %d\n"
143 "Scale: %d, transform: %d\n"
146 "Keys: (s)cale, (t)ransform, si(z)e, (f)ullscreen, (q)uit\n",
147 fullscreen->width, fullscreen->height,
148 window_get_buffer_scale (fullscreen->window),
149 window_get_buffer_transform (fullscreen->window),
150 fullscreen->pointer_x, fullscreen->pointer_y,
151 fullscreen->fullscreen);
155 while (y + 60 < fullscreen->height) {
156 border = (i++ % 2 == 0) ? 1 : 0.5;
159 cairo_set_line_width (cr, border);
160 while (x + 70 < fullscreen->width) {
161 if (fullscreen->pointer_x >= x && fullscreen->pointer_x < x + 50 &&
162 fullscreen->pointer_y >= y && fullscreen->pointer_y < y + 40) {
163 cairo_set_source_rgb(cr, 1, 0, 0);
169 cairo_set_source_rgb(cr, 0, 1, 0);
171 x + border/2.0, y + border/2.0,
184 key_handler(struct window *window, struct input *input, uint32_t time,
185 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
188 struct fullscreen *fullscreen = data;
189 int transform, scale;
190 static int current_size = 0;
191 int widths[] = { 640, 320, 800, 400 };
192 int heights[] = { 480, 240, 600, 300 };
194 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
199 transform = window_get_buffer_transform (window);
200 transform = (transform + 1) % 8;
201 window_set_buffer_transform(window, transform);
202 window_schedule_redraw(window);
206 scale = window_get_buffer_scale (window);
211 window_set_buffer_scale(window, scale);
212 window_schedule_redraw(window);
216 current_size = (current_size + 1) % 4;
217 fullscreen->width = widths[current_size];
218 fullscreen->height = heights[current_size];
219 window_schedule_resize(fullscreen->window,
220 fullscreen->width, fullscreen->height);
224 fullscreen->fullscreen ^= 1;
225 window_set_fullscreen(window, fullscreen->fullscreen);
235 motion_handler(struct widget *widget,
241 struct fullscreen *fullscreen = data;
243 fullscreen->pointer_x = x;
244 fullscreen->pointer_y = y;
246 widget_schedule_redraw(widget);
252 button_handler(struct widget *widget,
253 struct input *input, uint32_t time,
254 uint32_t button, enum wl_pointer_button_state state, void *data)
256 struct fullscreen *fullscreen = data;
260 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
261 window_move(fullscreen->window, input,
262 display_get_serial(fullscreen->display));
265 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
266 window_show_frame_menu(fullscreen->window, input, time);
272 touch_handler(struct widget *widget, struct input *input,
273 uint32_t serial, uint32_t time, int32_t id,
274 float x, float y, void *data)
276 struct fullscreen *fullscreen = data;
277 window_move(fullscreen->window, input, display_get_serial(fullscreen->display));
281 usage(int error_code)
283 fprintf(stderr, "Usage: fullscreen [OPTIONS]\n\n"
284 " -w <width>\tSet window width to <width>\n"
285 " -h <height>\tSet window height to <height>\n"
286 " --help\tShow this help text\n\n");
291 int main(int argc, char *argv[])
293 struct fullscreen fullscreen;
297 fullscreen.width = 640;
298 fullscreen.height = 480;
299 fullscreen.fullscreen = 0;
301 for (i = 1; i < argc; i++) {
302 if (strcmp(argv[i], "-w") == 0) {
306 fullscreen.width = atol(argv[i]);
307 } else if (strcmp(argv[i], "-h") == 0) {
311 fullscreen.height = atol(argv[i]);
312 } else if (strcmp(argv[i], "--help") == 0)
318 d = display_create(&argc, argv);
320 fprintf(stderr, "failed to create display: %m\n");
324 fullscreen.display = d;
325 fullscreen.window = window_create(d);
327 window_add_widget(fullscreen.window, &fullscreen);
329 window_set_title(fullscreen.window, "Fullscreen");
331 widget_set_transparent(fullscreen.widget, 0);
332 widget_set_default_cursor(fullscreen.widget, CURSOR_LEFT_PTR);
334 widget_set_resize_handler(fullscreen.widget, resize_handler);
335 widget_set_redraw_handler(fullscreen.widget, redraw_handler);
336 widget_set_button_handler(fullscreen.widget, button_handler);
337 widget_set_motion_handler(fullscreen.widget, motion_handler);
339 widget_set_touch_down_handler(fullscreen.widget, touch_handler);
341 window_set_key_handler(fullscreen.window, key_handler);
342 window_set_fullscreen_handler(fullscreen.window, fullscreen_handler);
344 window_set_user_data(fullscreen.window, &fullscreen);
345 /* Hack to set minimum allocation so we can shrink later */
346 window_schedule_resize(fullscreen.window,
348 window_schedule_resize(fullscreen.window,
349 fullscreen.width, fullscreen.height);