blob: 7dc437440a497476e0e6cb069bd6422039545be8 [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
24#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <stdarg.h>
28#include <string.h>
29#include <math.h>
30#include <cairo.h>
31
32#include <linux/input.h>
33#include <wayland-client.h>
34#include "window.h"
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -050035#include "fullscreen-shell-client-protocol.h"
36
37struct fs_output {
38 struct wl_list link;
39 struct output *output;
40};
Alexander Larsson73469ed2013-05-28 16:23:34 +020041
42struct fullscreen {
43 struct display *display;
44 struct window *window;
45 struct widget *widget;
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -050046 struct _wl_fullscreen_shell *fshell;
47 enum _wl_fullscreen_shell_present_method present_method;
Alexander Larsson73469ed2013-05-28 16:23:34 +020048 int width, height;
49 int fullscreen;
50 float pointer_x, pointer_y;
Jason Ekstrand7a17d422014-04-02 19:53:53 -050051 int focussed, draw_cursor;
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -050052
53 struct wl_list output_list;
54 struct fs_output *current_output;
Alexander Larsson73469ed2013-05-28 16:23:34 +020055};
56
57static void
58fullscreen_handler(struct window *window, void *data)
59{
60 struct fullscreen *fullscreen = data;
61
62 fullscreen->fullscreen ^= 1;
63 window_set_fullscreen(window, fullscreen->fullscreen);
64}
65
66static void
67resize_handler(struct widget *widget, int width, int height, void *data)
68{
69 struct fullscreen *fullscreen = data;
70
71 widget_set_size(widget, fullscreen->width, fullscreen->height);
72}
73
74static void
75draw_string(cairo_t *cr,
76 const char *fmt, ...)
77{
78 char buffer[4096];
79 char *p, *end;
80 va_list argp;
81 cairo_text_extents_t text_extents;
82 cairo_font_extents_t font_extents;
83
84 cairo_save(cr);
85
86 cairo_select_font_face(cr, "sans",
87 CAIRO_FONT_SLANT_NORMAL,
88 CAIRO_FONT_WEIGHT_NORMAL);
89 cairo_set_font_size(cr, 14);
90
91 cairo_font_extents (cr, &font_extents);
92
93 va_start(argp, fmt);
94
95 vsnprintf(buffer, sizeof(buffer), fmt, argp);
96
97 p = buffer;
98 while (*p) {
99 end = strchr(p, '\n');
100 if (end)
101 *end = 0;
102
103 cairo_show_text(cr, p);
104 cairo_text_extents (cr, p, &text_extents);
105 cairo_rel_move_to (cr, -text_extents.x_advance, font_extents.height);
106
107 if (end)
108 p = end + 1;
109 else
110 break;
111 }
112
113 va_end(argp);
114
115 cairo_restore(cr);
116
117}
118
119static void
120redraw_handler(struct widget *widget, void *data)
121{
122 struct fullscreen *fullscreen = data;
123 struct rectangle allocation;
124 cairo_surface_t *surface;
125 cairo_t *cr;
126 int i;
127 double x, y, border;
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -0500128 const char *method_name[] = { "default", "center", "zoom", "zoom_crop", "stretch"};
Alexander Larsson73469ed2013-05-28 16:23:34 +0200129
130 surface = window_get_surface(fullscreen->window);
131 if (surface == NULL ||
132 cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
133 fprintf(stderr, "failed to create cairo egl surface\n");
134 return;
135 }
136
137 widget_get_allocation(fullscreen->widget, &allocation);
138
139 cr = widget_cairo_create(widget);
140
141 cairo_set_source_rgb(cr, 0, 0, 0);
142 cairo_paint (cr);
143
144 cairo_set_source_rgb(cr, 0, 0, 1);
145 cairo_set_line_width (cr, 10);
146 cairo_rectangle(cr, 5, 5, allocation.width - 10, allocation.height - 10);
147 cairo_stroke (cr);
148
149 cairo_move_to(cr,
150 allocation.x + 15,
151 allocation.y + 25);
152 cairo_set_source_rgb(cr, 1, 1, 1);
153
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -0500154 if (fullscreen->fshell) {
155 draw_string(cr,
156 "Surface size: %d, %d\n"
157 "Scale: %d, transform: %d\n"
158 "Pointer: %f,%f\n"
159 "Output: %s, present method: %s\n"
160 "Keys: (s)cale, (t)ransform, si(z)e, (m)ethod,\n"
161 " (o)utput, modes(w)itch, (q)uit\n",
162 fullscreen->width, fullscreen->height,
163 window_get_buffer_scale (fullscreen->window),
164 window_get_buffer_transform (fullscreen->window),
165 fullscreen->pointer_x, fullscreen->pointer_y,
166 method_name[fullscreen->present_method],
167 fullscreen->current_output ? output_get_model(fullscreen->current_output->output): "null");
168 } else {
169 draw_string(cr,
170 "Surface size: %d, %d\n"
171 "Scale: %d, transform: %d\n"
172 "Pointer: %f,%f\n"
173 "Fullscreen: %d\n"
174 "Keys: (s)cale, (t)ransform, si(z)e, (f)ullscreen, (q)uit\n",
175 fullscreen->width, fullscreen->height,
176 window_get_buffer_scale (fullscreen->window),
177 window_get_buffer_transform (fullscreen->window),
178 fullscreen->pointer_x, fullscreen->pointer_y,
179 fullscreen->fullscreen);
180 }
Alexander Larsson73469ed2013-05-28 16:23:34 +0200181
182 y = 100;
183 i = 0;
184 while (y + 60 < fullscreen->height) {
185 border = (i++ % 2 == 0) ? 1 : 0.5;
186
187 x = 50;
188 cairo_set_line_width (cr, border);
189 while (x + 70 < fullscreen->width) {
Jason Ekstrand7a17d422014-04-02 19:53:53 -0500190 if (fullscreen->focussed &&
191 fullscreen->pointer_x >= x && fullscreen->pointer_x < x + 50 &&
Alexander Larsson73469ed2013-05-28 16:23:34 +0200192 fullscreen->pointer_y >= y && fullscreen->pointer_y < y + 40) {
193 cairo_set_source_rgb(cr, 1, 0, 0);
194 cairo_rectangle(cr,
195 x, y,
196 50, 40);
197 cairo_fill(cr);
198 }
199 cairo_set_source_rgb(cr, 0, 1, 0);
200 cairo_rectangle(cr,
201 x + border/2.0, y + border/2.0,
202 50, 40);
203 cairo_stroke(cr);
204 x += 60;
205 }
206
207 y += 50;
208 }
209
Jason Ekstrand7a17d422014-04-02 19:53:53 -0500210 if (fullscreen->focussed && fullscreen->draw_cursor) {
211 cairo_set_source_rgb(cr, 1, 1, 1);
212 cairo_set_line_width (cr, 8);
213 cairo_move_to(cr,
214 fullscreen->pointer_x - 12,
215 fullscreen->pointer_y - 12);
216 cairo_line_to(cr,
217 fullscreen->pointer_x + 12,
218 fullscreen->pointer_y + 12);
219 cairo_stroke(cr);
220
221 cairo_move_to(cr,
222 fullscreen->pointer_x + 12,
223 fullscreen->pointer_y - 12);
224 cairo_line_to(cr,
225 fullscreen->pointer_x - 12,
226 fullscreen->pointer_y + 12);
227 cairo_stroke(cr);
228
229 cairo_set_source_rgb(cr, 0, 0, 0);
230 cairo_set_line_width (cr, 4);
231 cairo_move_to(cr,
232 fullscreen->pointer_x - 10,
233 fullscreen->pointer_y - 10);
234 cairo_line_to(cr,
235 fullscreen->pointer_x + 10,
236 fullscreen->pointer_y + 10);
237 cairo_stroke(cr);
238
239 cairo_move_to(cr,
240 fullscreen->pointer_x + 10,
241 fullscreen->pointer_y - 10);
242 cairo_line_to(cr,
243 fullscreen->pointer_x - 10,
244 fullscreen->pointer_y + 10);
245 cairo_stroke(cr);
246 }
247
Alexander Larsson73469ed2013-05-28 16:23:34 +0200248 cairo_destroy(cr);
249}
250
251static void
252key_handler(struct window *window, struct input *input, uint32_t time,
253 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
254 void *data)
255{
256 struct fullscreen *fullscreen = data;
257 int transform, scale;
258 static int current_size = 0;
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -0500259 struct fs_output *fsout;
260 struct wl_output *wl_output;
Alexander Larsson73469ed2013-05-28 16:23:34 +0200261 int widths[] = { 640, 320, 800, 400 };
262 int heights[] = { 480, 240, 600, 300 };
263
264 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
265 return;
266
267 switch (sym) {
268 case XKB_KEY_t:
269 transform = window_get_buffer_transform (window);
270 transform = (transform + 1) % 8;
271 window_set_buffer_transform(window, transform);
272 window_schedule_redraw(window);
273 break;
274
275 case XKB_KEY_s:
276 scale = window_get_buffer_scale (window);
277 if (scale == 1)
278 scale = 2;
279 else
280 scale = 1;
281 window_set_buffer_scale(window, scale);
282 window_schedule_redraw(window);
283 break;
284
285 case XKB_KEY_z:
286 current_size = (current_size + 1) % 4;
287 fullscreen->width = widths[current_size];
288 fullscreen->height = heights[current_size];
289 window_schedule_resize(fullscreen->window,
290 fullscreen->width, fullscreen->height);
291 break;
292
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -0500293 case XKB_KEY_m:
294 if (!fullscreen->fshell)
295 break;
296
297 wl_output = NULL;
298 if (fullscreen->current_output)
299 wl_output = output_get_wl_output(fullscreen->current_output->output);
300 fullscreen->present_method = (fullscreen->present_method + 1) % 5;
301 _wl_fullscreen_shell_present_surface(fullscreen->fshell,
302 window_get_wl_surface(fullscreen->window),
303 fullscreen->present_method,
304 wl_output);
305 window_schedule_redraw(window);
306 break;
307
308 case XKB_KEY_o:
309 if (!fullscreen->fshell)
310 break;
311
312 fsout = fullscreen->current_output;
313 wl_output = fsout ? output_get_wl_output(fsout->output) : NULL;
314
315 /* Clear the current presentation */
316 _wl_fullscreen_shell_present_surface(fullscreen->fshell, NULL,
317 0, wl_output);
318
319 if (fullscreen->current_output) {
320 if (fullscreen->current_output->link.next == &fullscreen->output_list)
321 fsout = NULL;
322 else
323 fsout = wl_container_of(fullscreen->current_output->link.next,
324 fsout, link);
325 } else {
326 fsout = wl_container_of(fullscreen->output_list.next,
327 fsout, link);
328 }
329
330 fullscreen->current_output = fsout;
331 wl_output = fsout ? output_get_wl_output(fsout->output) : NULL;
332 _wl_fullscreen_shell_present_surface(fullscreen->fshell,
333 window_get_wl_surface(fullscreen->window),
334 fullscreen->present_method,
335 wl_output);
336 window_schedule_redraw(window);
337 break;
338
339 case XKB_KEY_w:
340 if (!fullscreen->fshell || !fullscreen->current_output)
341 break;
342
343 wl_output = NULL;
344 if (fullscreen->current_output)
345 wl_output = output_get_wl_output(fullscreen->current_output->output);
346 _wl_fullscreen_shell_mode_feedback_destroy(
347 _wl_fullscreen_shell_present_surface_for_mode(fullscreen->fshell,
348 window_get_wl_surface(fullscreen->window),
349 wl_output, 0));
350 window_schedule_redraw(window);
351 break;
352
Alexander Larsson73469ed2013-05-28 16:23:34 +0200353 case XKB_KEY_f:
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -0500354 if (fullscreen->fshell)
355 break;
Alexander Larsson73469ed2013-05-28 16:23:34 +0200356 fullscreen->fullscreen ^= 1;
357 window_set_fullscreen(window, fullscreen->fullscreen);
358 break;
359
360 case XKB_KEY_q:
361 exit (0);
362 break;
363 }
364}
365
366static int
367motion_handler(struct widget *widget,
368 struct input *input,
369 uint32_t time,
370 float x,
371 float y, void *data)
372{
373 struct fullscreen *fullscreen = data;
374
375 fullscreen->pointer_x = x;
376 fullscreen->pointer_y = y;
377
378 widget_schedule_redraw(widget);
Jason Ekstrand7a17d422014-04-02 19:53:53 -0500379
380 return fullscreen->draw_cursor ? CURSOR_BLANK : CURSOR_LEFT_PTR;
Alexander Larsson73469ed2013-05-28 16:23:34 +0200381}
382
Jason Ekstrand7a17d422014-04-02 19:53:53 -0500383static int
384enter_handler(struct widget *widget,
385 struct input *input,
386 float x, float y, void *data)
387{
388 struct fullscreen *fullscreen = data;
389
390 fullscreen->focussed++;
391
392 fullscreen->pointer_x = x;
393 fullscreen->pointer_y = y;
394
395 widget_schedule_redraw(widget);
396
397 return fullscreen->draw_cursor ? CURSOR_BLANK : CURSOR_LEFT_PTR;
398}
399
400static void
401leave_handler(struct widget *widget,
402 struct input *input, void *data)
403{
404 struct fullscreen *fullscreen = data;
405
406 fullscreen->focussed--;
407
408 widget_schedule_redraw(widget);
409}
Alexander Larsson73469ed2013-05-28 16:23:34 +0200410
411static void
412button_handler(struct widget *widget,
413 struct input *input, uint32_t time,
414 uint32_t button, enum wl_pointer_button_state state, void *data)
415{
416 struct fullscreen *fullscreen = data;
417
418 switch (button) {
419 case BTN_LEFT:
420 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
421 window_move(fullscreen->window, input,
422 display_get_serial(fullscreen->display));
423 break;
424 case BTN_RIGHT:
425 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
426 window_show_frame_menu(fullscreen->window, input, time);
427 break;
428 }
429}
430
431static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700432touch_handler(struct widget *widget, struct input *input,
433 uint32_t serial, uint32_t time, int32_t id,
434 float x, float y, void *data)
435{
436 struct fullscreen *fullscreen = data;
Jasper St. Pierre01eaaac2013-11-12 20:19:57 -0500437 window_move(fullscreen->window, input, display_get_serial(fullscreen->display));
Rusty Lynch1084da52013-08-15 09:10:08 -0700438}
439
440static void
Jason Ekstrand7a17d422014-04-02 19:53:53 -0500441fshell_capability_handler(void *data, struct _wl_fullscreen_shell *fshell,
442 uint32_t capability)
443{
444 struct fullscreen *fullscreen = data;
445
446 switch (capability) {
447 case _WL_FULLSCREEN_SHELL_CAPABILITY_CURSOR_PLANE:
448 fullscreen->draw_cursor = 0;
449 break;
450 default:
451 break;
452 }
453}
454
455struct _wl_fullscreen_shell_listener fullscreen_shell_listener = {
456 fshell_capability_handler
457};
458
459static void
Alexander Larsson73469ed2013-05-28 16:23:34 +0200460usage(int error_code)
461{
462 fprintf(stderr, "Usage: fullscreen [OPTIONS]\n\n"
463 " -w <width>\tSet window width to <width>\n"
464 " -h <height>\tSet window height to <height>\n"
465 " --help\tShow this help text\n\n");
466
467 exit(error_code);
468}
469
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -0500470static void
471output_handler(struct output *output, void *data)
472{
473 struct fullscreen *fullscreen = data;
474 struct fs_output *fsout;
475
476 /* If we've already seen this one, don't add it to the list */
477 wl_list_for_each(fsout, &fullscreen->output_list, link)
478 if (fsout->output == output)
479 return;
480
481 fsout = calloc(1, sizeof *fsout);
482 fsout->output = output;
483 wl_list_insert(&fullscreen->output_list, &fsout->link);
484}
485
486static void
487global_handler(struct display *display, uint32_t id, const char *interface,
488 uint32_t version, void *data)
489{
490 struct fullscreen *fullscreen = data;
491
492 if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
493 fullscreen->fshell = display_bind(display, id,
494 &_wl_fullscreen_shell_interface,
495 1);
Jason Ekstrand7a17d422014-04-02 19:53:53 -0500496 _wl_fullscreen_shell_add_listener(fullscreen->fshell,
497 &fullscreen_shell_listener,
498 fullscreen);
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -0500499 }
500}
501
Alexander Larsson73469ed2013-05-28 16:23:34 +0200502int main(int argc, char *argv[])
503{
504 struct fullscreen fullscreen;
505 struct display *d;
506 int i;
507
508 fullscreen.width = 640;
509 fullscreen.height = 480;
510 fullscreen.fullscreen = 0;
Jason Ekstrand7a17d422014-04-02 19:53:53 -0500511 fullscreen.focussed = 0;
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -0500512 fullscreen.present_method = _WL_FULLSCREEN_SHELL_PRESENT_METHOD_DEFAULT;
513 wl_list_init(&fullscreen.output_list);
514 fullscreen.current_output = NULL;
Alexander Larsson73469ed2013-05-28 16:23:34 +0200515
516 for (i = 1; i < argc; i++) {
517 if (strcmp(argv[i], "-w") == 0) {
518 if (++i >= argc)
519 usage(EXIT_FAILURE);
520
521 fullscreen.width = atol(argv[i]);
522 } else if (strcmp(argv[i], "-h") == 0) {
523 if (++i >= argc)
524 usage(EXIT_FAILURE);
525
526 fullscreen.height = atol(argv[i]);
527 } else if (strcmp(argv[i], "--help") == 0)
528 usage(EXIT_SUCCESS);
529 else
530 usage(EXIT_FAILURE);
531 }
532
533 d = display_create(&argc, argv);
534 if (d == NULL) {
535 fprintf(stderr, "failed to create display: %m\n");
536 return -1;
537 }
538
539 fullscreen.display = d;
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -0500540 fullscreen.fshell = NULL;
541 display_set_user_data(fullscreen.display, &fullscreen);
542 display_set_global_handler(fullscreen.display, global_handler);
543 display_set_output_configure_handler(fullscreen.display, output_handler);
544
545 if (fullscreen.fshell) {
546 fullscreen.window = window_create_custom(d);
547 _wl_fullscreen_shell_present_surface(fullscreen.fshell,
548 window_get_wl_surface(fullscreen.window),
549 fullscreen.present_method,
550 NULL);
Jason Ekstrand7a17d422014-04-02 19:53:53 -0500551 /* If we get the CURSOR_PLANE capability, we'll change this */
552 fullscreen.draw_cursor = 1;
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -0500553 } else {
554 fullscreen.window = window_create(d);
Jason Ekstrand7a17d422014-04-02 19:53:53 -0500555 fullscreen.draw_cursor = 0;
Jason Ekstrand2bb72fe2014-04-02 19:53:52 -0500556 }
557
Alexander Larsson73469ed2013-05-28 16:23:34 +0200558 fullscreen.widget =
559 window_add_widget(fullscreen.window, &fullscreen);
560
561 window_set_title(fullscreen.window, "Fullscreen");
Alexander Larsson73469ed2013-05-28 16:23:34 +0200562
563 widget_set_transparent(fullscreen.widget, 0);
Alexander Larsson73469ed2013-05-28 16:23:34 +0200564
Jason Ekstrand7a17d422014-04-02 19:53:53 -0500565 widget_set_default_cursor(fullscreen.widget, CURSOR_LEFT_PTR);
Alexander Larsson73469ed2013-05-28 16:23:34 +0200566 widget_set_resize_handler(fullscreen.widget, resize_handler);
567 widget_set_redraw_handler(fullscreen.widget, redraw_handler);
568 widget_set_button_handler(fullscreen.widget, button_handler);
569 widget_set_motion_handler(fullscreen.widget, motion_handler);
Jason Ekstrand7a17d422014-04-02 19:53:53 -0500570 widget_set_enter_handler(fullscreen.widget, enter_handler);
571 widget_set_leave_handler(fullscreen.widget, leave_handler);
Alexander Larsson73469ed2013-05-28 16:23:34 +0200572
Rusty Lynch1084da52013-08-15 09:10:08 -0700573 widget_set_touch_down_handler(fullscreen.widget, touch_handler);
574
Alexander Larsson73469ed2013-05-28 16:23:34 +0200575 window_set_key_handler(fullscreen.window, key_handler);
576 window_set_fullscreen_handler(fullscreen.window, fullscreen_handler);
577
578 window_set_user_data(fullscreen.window, &fullscreen);
579 /* Hack to set minimum allocation so we can shrink later */
580 window_schedule_resize(fullscreen.window,
581 1, 1);
582 window_schedule_resize(fullscreen.window,
583 fullscreen.width, fullscreen.height);
584
585 display_run(d);
586
587 return 0;
588}