blob: 5c5dd2bc128c696c3f050789d59e5e1e5159d5cf [file] [log] [blame]
Alexander Larsson73469ed2013-05-28 16:23:34 +02001/*
2 * Copyright © 2008 Kristian Høgsberg
3 * Copyright © 2012 Intel Corporation
4 *
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.
14 *
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
21 * OF THIS SOFTWARE.
22 */
23
Andrew Wedgbury9cd661e2014-04-07 12:40:35 +010024#include "config.h"
25
Alexander Larsson73469ed2013-05-28 16:23:34 +020026#include <stdint.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <stdarg.h>
30#include <string.h>
31#include <math.h>
32#include <cairo.h>
33
34#include <linux/input.h>
35#include <wayland-client.h>
36#include "window.h"
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -050037#include "fullscreen-shell-client-protocol.h"
38
39struct fs_output {
40 struct wl_list link;
41 struct output *output;
42};
Alexander Larsson73469ed2013-05-28 16:23:34 +020043
44struct fullscreen {
45 struct display *display;
46 struct window *window;
47 struct widget *widget;
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -050048 struct _wl_fullscreen_shell *fshell;
49 enum _wl_fullscreen_shell_present_method present_method;
Alexander Larsson73469ed2013-05-28 16:23:34 +020050 int width, height;
51 int fullscreen;
52 float pointer_x, pointer_y;
Jasper St. Pierreaf314bb2014-05-12 11:23:44 -040053 int draw_cursor;
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -050054
55 struct wl_list output_list;
56 struct fs_output *current_output;
Alexander Larsson73469ed2013-05-28 16:23:34 +020057};
58
59static void
60fullscreen_handler(struct window *window, void *data)
61{
62 struct fullscreen *fullscreen = data;
63
64 fullscreen->fullscreen ^= 1;
65 window_set_fullscreen(window, fullscreen->fullscreen);
66}
67
68static void
Alexander Larsson73469ed2013-05-28 16:23:34 +020069draw_string(cairo_t *cr,
70 const char *fmt, ...)
71{
72 char buffer[4096];
73 char *p, *end;
74 va_list argp;
75 cairo_text_extents_t text_extents;
76 cairo_font_extents_t font_extents;
77
78 cairo_save(cr);
79
80 cairo_select_font_face(cr, "sans",
81 CAIRO_FONT_SLANT_NORMAL,
82 CAIRO_FONT_WEIGHT_NORMAL);
83 cairo_set_font_size(cr, 14);
84
85 cairo_font_extents (cr, &font_extents);
86
87 va_start(argp, fmt);
88
89 vsnprintf(buffer, sizeof(buffer), fmt, argp);
90
91 p = buffer;
92 while (*p) {
93 end = strchr(p, '\n');
94 if (end)
95 *end = 0;
96
97 cairo_show_text(cr, p);
98 cairo_text_extents (cr, p, &text_extents);
99 cairo_rel_move_to (cr, -text_extents.x_advance, font_extents.height);
100
101 if (end)
102 p = end + 1;
103 else
104 break;
105 }
106
107 va_end(argp);
108
109 cairo_restore(cr);
110
111}
112
113static void
114redraw_handler(struct widget *widget, void *data)
115{
116 struct fullscreen *fullscreen = data;
117 struct rectangle allocation;
118 cairo_surface_t *surface;
119 cairo_t *cr;
120 int i;
121 double x, y, border;
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -0500122 const char *method_name[] = { "default", "center", "zoom", "zoom_crop", "stretch"};
Alexander Larsson73469ed2013-05-28 16:23:34 +0200123
124 surface = window_get_surface(fullscreen->window);
125 if (surface == NULL ||
126 cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
127 fprintf(stderr, "failed to create cairo egl surface\n");
128 return;
129 }
130
131 widget_get_allocation(fullscreen->widget, &allocation);
132
133 cr = widget_cairo_create(widget);
134
135 cairo_set_source_rgb(cr, 0, 0, 0);
136 cairo_paint (cr);
137
138 cairo_set_source_rgb(cr, 0, 0, 1);
139 cairo_set_line_width (cr, 10);
140 cairo_rectangle(cr, 5, 5, allocation.width - 10, allocation.height - 10);
141 cairo_stroke (cr);
142
143 cairo_move_to(cr,
144 allocation.x + 15,
145 allocation.y + 25);
146 cairo_set_source_rgb(cr, 1, 1, 1);
147
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -0500148 if (fullscreen->fshell) {
149 draw_string(cr,
150 "Surface size: %d, %d\n"
151 "Scale: %d, transform: %d\n"
152 "Pointer: %f,%f\n"
153 "Output: %s, present method: %s\n"
154 "Keys: (s)cale, (t)ransform, si(z)e, (m)ethod,\n"
155 " (o)utput, modes(w)itch, (q)uit\n",
156 fullscreen->width, fullscreen->height,
157 window_get_buffer_scale (fullscreen->window),
158 window_get_buffer_transform (fullscreen->window),
159 fullscreen->pointer_x, fullscreen->pointer_y,
160 method_name[fullscreen->present_method],
161 fullscreen->current_output ? output_get_model(fullscreen->current_output->output): "null");
162 } else {
163 draw_string(cr,
164 "Surface size: %d, %d\n"
165 "Scale: %d, transform: %d\n"
166 "Pointer: %f,%f\n"
167 "Fullscreen: %d\n"
168 "Keys: (s)cale, (t)ransform, si(z)e, (f)ullscreen, (q)uit\n",
169 fullscreen->width, fullscreen->height,
170 window_get_buffer_scale (fullscreen->window),
171 window_get_buffer_transform (fullscreen->window),
172 fullscreen->pointer_x, fullscreen->pointer_y,
173 fullscreen->fullscreen);
174 }
Alexander Larsson73469ed2013-05-28 16:23:34 +0200175
176 y = 100;
177 i = 0;
178 while (y + 60 < fullscreen->height) {
179 border = (i++ % 2 == 0) ? 1 : 0.5;
180
181 x = 50;
182 cairo_set_line_width (cr, border);
183 while (x + 70 < fullscreen->width) {
Jasper St. Pierreaf314bb2014-05-12 11:23:44 -0400184 if (window_has_focus(fullscreen->window) &&
Jason Ekstrand7a17d422014-04-02 19:53:53 -0500185 fullscreen->pointer_x >= x && fullscreen->pointer_x < x + 50 &&
Alexander Larsson73469ed2013-05-28 16:23:34 +0200186 fullscreen->pointer_y >= y && fullscreen->pointer_y < y + 40) {
187 cairo_set_source_rgb(cr, 1, 0, 0);
188 cairo_rectangle(cr,
189 x, y,
190 50, 40);
191 cairo_fill(cr);
192 }
193 cairo_set_source_rgb(cr, 0, 1, 0);
194 cairo_rectangle(cr,
195 x + border/2.0, y + border/2.0,
196 50, 40);
197 cairo_stroke(cr);
198 x += 60;
199 }
200
201 y += 50;
202 }
203
Jasper St. Pierreaf314bb2014-05-12 11:23:44 -0400204 if (window_has_focus(fullscreen->window) && fullscreen->draw_cursor) {
Jason Ekstrand7a17d422014-04-02 19:53:53 -0500205 cairo_set_source_rgb(cr, 1, 1, 1);
206 cairo_set_line_width (cr, 8);
207 cairo_move_to(cr,
208 fullscreen->pointer_x - 12,
209 fullscreen->pointer_y - 12);
210 cairo_line_to(cr,
211 fullscreen->pointer_x + 12,
212 fullscreen->pointer_y + 12);
213 cairo_stroke(cr);
214
215 cairo_move_to(cr,
216 fullscreen->pointer_x + 12,
217 fullscreen->pointer_y - 12);
218 cairo_line_to(cr,
219 fullscreen->pointer_x - 12,
220 fullscreen->pointer_y + 12);
221 cairo_stroke(cr);
222
223 cairo_set_source_rgb(cr, 0, 0, 0);
224 cairo_set_line_width (cr, 4);
225 cairo_move_to(cr,
226 fullscreen->pointer_x - 10,
227 fullscreen->pointer_y - 10);
228 cairo_line_to(cr,
229 fullscreen->pointer_x + 10,
230 fullscreen->pointer_y + 10);
231 cairo_stroke(cr);
232
233 cairo_move_to(cr,
234 fullscreen->pointer_x + 10,
235 fullscreen->pointer_y - 10);
236 cairo_line_to(cr,
237 fullscreen->pointer_x - 10,
238 fullscreen->pointer_y + 10);
239 cairo_stroke(cr);
240 }
241
Alexander Larsson73469ed2013-05-28 16:23:34 +0200242 cairo_destroy(cr);
243}
244
245static void
246key_handler(struct window *window, struct input *input, uint32_t time,
247 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
248 void *data)
249{
250 struct fullscreen *fullscreen = data;
251 int transform, scale;
252 static int current_size = 0;
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -0500253 struct fs_output *fsout;
254 struct wl_output *wl_output;
Alexander Larsson73469ed2013-05-28 16:23:34 +0200255 int widths[] = { 640, 320, 800, 400 };
256 int heights[] = { 480, 240, 600, 300 };
257
258 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
259 return;
260
261 switch (sym) {
262 case XKB_KEY_t:
263 transform = window_get_buffer_transform (window);
264 transform = (transform + 1) % 8;
265 window_set_buffer_transform(window, transform);
266 window_schedule_redraw(window);
267 break;
268
269 case XKB_KEY_s:
270 scale = window_get_buffer_scale (window);
271 if (scale == 1)
272 scale = 2;
273 else
274 scale = 1;
275 window_set_buffer_scale(window, scale);
276 window_schedule_redraw(window);
277 break;
278
279 case XKB_KEY_z:
280 current_size = (current_size + 1) % 4;
281 fullscreen->width = widths[current_size];
282 fullscreen->height = heights[current_size];
283 window_schedule_resize(fullscreen->window,
284 fullscreen->width, fullscreen->height);
285 break;
286
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -0500287 case XKB_KEY_m:
288 if (!fullscreen->fshell)
289 break;
290
291 wl_output = NULL;
292 if (fullscreen->current_output)
293 wl_output = output_get_wl_output(fullscreen->current_output->output);
294 fullscreen->present_method = (fullscreen->present_method + 1) % 5;
295 _wl_fullscreen_shell_present_surface(fullscreen->fshell,
296 window_get_wl_surface(fullscreen->window),
297 fullscreen->present_method,
298 wl_output);
299 window_schedule_redraw(window);
300 break;
301
302 case XKB_KEY_o:
303 if (!fullscreen->fshell)
304 break;
305
306 fsout = fullscreen->current_output;
307 wl_output = fsout ? output_get_wl_output(fsout->output) : NULL;
308
309 /* Clear the current presentation */
310 _wl_fullscreen_shell_present_surface(fullscreen->fshell, NULL,
311 0, wl_output);
312
313 if (fullscreen->current_output) {
314 if (fullscreen->current_output->link.next == &fullscreen->output_list)
315 fsout = NULL;
316 else
317 fsout = wl_container_of(fullscreen->current_output->link.next,
318 fsout, link);
319 } else {
320 fsout = wl_container_of(fullscreen->output_list.next,
321 fsout, link);
322 }
323
324 fullscreen->current_output = fsout;
325 wl_output = fsout ? output_get_wl_output(fsout->output) : NULL;
326 _wl_fullscreen_shell_present_surface(fullscreen->fshell,
327 window_get_wl_surface(fullscreen->window),
328 fullscreen->present_method,
329 wl_output);
330 window_schedule_redraw(window);
331 break;
332
333 case XKB_KEY_w:
334 if (!fullscreen->fshell || !fullscreen->current_output)
335 break;
336
337 wl_output = NULL;
338 if (fullscreen->current_output)
339 wl_output = output_get_wl_output(fullscreen->current_output->output);
340 _wl_fullscreen_shell_mode_feedback_destroy(
341 _wl_fullscreen_shell_present_surface_for_mode(fullscreen->fshell,
342 window_get_wl_surface(fullscreen->window),
343 wl_output, 0));
344 window_schedule_redraw(window);
345 break;
346
Alexander Larsson73469ed2013-05-28 16:23:34 +0200347 case XKB_KEY_f:
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -0500348 if (fullscreen->fshell)
349 break;
Alexander Larsson73469ed2013-05-28 16:23:34 +0200350 fullscreen->fullscreen ^= 1;
351 window_set_fullscreen(window, fullscreen->fullscreen);
352 break;
353
354 case XKB_KEY_q:
355 exit (0);
356 break;
357 }
358}
359
360static int
361motion_handler(struct widget *widget,
362 struct input *input,
363 uint32_t time,
364 float x,
365 float y, void *data)
366{
367 struct fullscreen *fullscreen = data;
368
369 fullscreen->pointer_x = x;
370 fullscreen->pointer_y = y;
371
372 widget_schedule_redraw(widget);
Jason Ekstrand7a17d422014-04-02 19:53:53 -0500373
374 return fullscreen->draw_cursor ? CURSOR_BLANK : CURSOR_LEFT_PTR;
Alexander Larsson73469ed2013-05-28 16:23:34 +0200375}
376
Jason Ekstrand7a17d422014-04-02 19:53:53 -0500377static int
378enter_handler(struct widget *widget,
379 struct input *input,
380 float x, float y, void *data)
381{
382 struct fullscreen *fullscreen = data;
383
Jason Ekstrand7a17d422014-04-02 19:53:53 -0500384 fullscreen->pointer_x = x;
385 fullscreen->pointer_y = y;
386
387 widget_schedule_redraw(widget);
388
389 return fullscreen->draw_cursor ? CURSOR_BLANK : CURSOR_LEFT_PTR;
390}
391
392static void
Alexander Larsson73469ed2013-05-28 16:23:34 +0200393button_handler(struct widget *widget,
394 struct input *input, uint32_t time,
395 uint32_t button, enum wl_pointer_button_state state, void *data)
396{
397 struct fullscreen *fullscreen = data;
398
399 switch (button) {
400 case BTN_LEFT:
401 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
402 window_move(fullscreen->window, input,
403 display_get_serial(fullscreen->display));
404 break;
405 case BTN_RIGHT:
406 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
407 window_show_frame_menu(fullscreen->window, input, time);
408 break;
409 }
410}
411
412static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700413touch_handler(struct widget *widget, struct input *input,
414 uint32_t serial, uint32_t time, int32_t id,
415 float x, float y, void *data)
416{
417 struct fullscreen *fullscreen = data;
Jasper St. Pierre01eaaac2013-11-12 20:19:57 -0500418 window_move(fullscreen->window, input, display_get_serial(fullscreen->display));
Rusty Lynch1084da52013-08-15 09:10:08 -0700419}
420
421static void
Jason Ekstrand7a17d422014-04-02 19:53:53 -0500422fshell_capability_handler(void *data, struct _wl_fullscreen_shell *fshell,
423 uint32_t capability)
424{
425 struct fullscreen *fullscreen = data;
426
427 switch (capability) {
428 case _WL_FULLSCREEN_SHELL_CAPABILITY_CURSOR_PLANE:
429 fullscreen->draw_cursor = 0;
430 break;
431 default:
432 break;
433 }
434}
435
436struct _wl_fullscreen_shell_listener fullscreen_shell_listener = {
437 fshell_capability_handler
438};
439
440static void
Alexander Larsson73469ed2013-05-28 16:23:34 +0200441usage(int error_code)
442{
443 fprintf(stderr, "Usage: fullscreen [OPTIONS]\n\n"
444 " -w <width>\tSet window width to <width>\n"
445 " -h <height>\tSet window height to <height>\n"
446 " --help\tShow this help text\n\n");
447
448 exit(error_code);
449}
450
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -0500451static void
452output_handler(struct output *output, void *data)
453{
454 struct fullscreen *fullscreen = data;
455 struct fs_output *fsout;
456
457 /* If we've already seen this one, don't add it to the list */
458 wl_list_for_each(fsout, &fullscreen->output_list, link)
459 if (fsout->output == output)
460 return;
461
462 fsout = calloc(1, sizeof *fsout);
463 fsout->output = output;
464 wl_list_insert(&fullscreen->output_list, &fsout->link);
465}
466
467static void
468global_handler(struct display *display, uint32_t id, const char *interface,
469 uint32_t version, void *data)
470{
471 struct fullscreen *fullscreen = data;
472
473 if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
474 fullscreen->fshell = display_bind(display, id,
475 &_wl_fullscreen_shell_interface,
476 1);
Jason Ekstrand7a17d422014-04-02 19:53:53 -0500477 _wl_fullscreen_shell_add_listener(fullscreen->fshell,
478 &fullscreen_shell_listener,
479 fullscreen);
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -0500480 }
481}
482
Alexander Larsson73469ed2013-05-28 16:23:34 +0200483int main(int argc, char *argv[])
484{
485 struct fullscreen fullscreen;
486 struct display *d;
487 int i;
488
489 fullscreen.width = 640;
490 fullscreen.height = 480;
491 fullscreen.fullscreen = 0;
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -0500492 fullscreen.present_method = _WL_FULLSCREEN_SHELL_PRESENT_METHOD_DEFAULT;
493 wl_list_init(&fullscreen.output_list);
494 fullscreen.current_output = NULL;
Alexander Larsson73469ed2013-05-28 16:23:34 +0200495
496 for (i = 1; i < argc; i++) {
497 if (strcmp(argv[i], "-w") == 0) {
498 if (++i >= argc)
499 usage(EXIT_FAILURE);
500
501 fullscreen.width = atol(argv[i]);
502 } else if (strcmp(argv[i], "-h") == 0) {
503 if (++i >= argc)
504 usage(EXIT_FAILURE);
505
506 fullscreen.height = atol(argv[i]);
507 } else if (strcmp(argv[i], "--help") == 0)
508 usage(EXIT_SUCCESS);
509 else
510 usage(EXIT_FAILURE);
511 }
512
513 d = display_create(&argc, argv);
514 if (d == NULL) {
515 fprintf(stderr, "failed to create display: %m\n");
516 return -1;
517 }
518
519 fullscreen.display = d;
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -0500520 fullscreen.fshell = NULL;
521 display_set_user_data(fullscreen.display, &fullscreen);
522 display_set_global_handler(fullscreen.display, global_handler);
523 display_set_output_configure_handler(fullscreen.display, output_handler);
524
525 if (fullscreen.fshell) {
526 fullscreen.window = window_create_custom(d);
527 _wl_fullscreen_shell_present_surface(fullscreen.fshell,
528 window_get_wl_surface(fullscreen.window),
529 fullscreen.present_method,
530 NULL);
Jason Ekstrand7a17d422014-04-02 19:53:53 -0500531 /* If we get the CURSOR_PLANE capability, we'll change this */
532 fullscreen.draw_cursor = 1;
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -0500533 } else {
534 fullscreen.window = window_create(d);
Jason Ekstrand7a17d422014-04-02 19:53:53 -0500535 fullscreen.draw_cursor = 0;
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -0500536 }
537
Alexander Larsson73469ed2013-05-28 16:23:34 +0200538 fullscreen.widget =
539 window_add_widget(fullscreen.window, &fullscreen);
540
541 window_set_title(fullscreen.window, "Fullscreen");
Alexander Larsson73469ed2013-05-28 16:23:34 +0200542
543 widget_set_transparent(fullscreen.widget, 0);
Alexander Larsson73469ed2013-05-28 16:23:34 +0200544
Jason Ekstrand7a17d422014-04-02 19:53:53 -0500545 widget_set_default_cursor(fullscreen.widget, CURSOR_LEFT_PTR);
Alexander Larsson73469ed2013-05-28 16:23:34 +0200546 widget_set_redraw_handler(fullscreen.widget, redraw_handler);
547 widget_set_button_handler(fullscreen.widget, button_handler);
548 widget_set_motion_handler(fullscreen.widget, motion_handler);
Jason Ekstrand7a17d422014-04-02 19:53:53 -0500549 widget_set_enter_handler(fullscreen.widget, enter_handler);
Alexander Larsson73469ed2013-05-28 16:23:34 +0200550
Rusty Lynch1084da52013-08-15 09:10:08 -0700551 widget_set_touch_down_handler(fullscreen.widget, touch_handler);
552
Alexander Larsson73469ed2013-05-28 16:23:34 +0200553 window_set_key_handler(fullscreen.window, key_handler);
554 window_set_fullscreen_handler(fullscreen.window, fullscreen_handler);
555
556 window_set_user_data(fullscreen.window, &fullscreen);
557 /* Hack to set minimum allocation so we can shrink later */
558 window_schedule_resize(fullscreen.window,
559 1, 1);
560 window_schedule_resize(fullscreen.window,
561 fullscreen.width, fullscreen.height);
562
563 display_run(d);
564
vivek31732f72014-05-15 18:58:16 +0530565 widget_destroy(fullscreen.widget);
566 window_destroy(fullscreen.window);
567 display_destroy(d);
568
Alexander Larsson73469ed2013-05-28 16:23:34 +0200569 return 0;
570}