1 /*
2  * Copyright © 2008 Kristian Høgsberg
3  * Copyright © 2012 Intel Corporation
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include "config.h"
26 
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <string.h>
32 #include <math.h>
33 #include <cairo.h>
34 
35 #include <linux/input.h>
36 #include <wayland-client.h>
37 #include "window.h"
38 #include "fullscreen-shell-client-protocol.h"
39 
40 struct fs_output {
41 	struct wl_list link;
42 	struct output *output;
43 };
44 
45 struct fullscreen {
46 	struct display *display;
47 	struct window *window;
48 	struct widget *widget;
49 	struct _wl_fullscreen_shell *fshell;
50 	enum _wl_fullscreen_shell_present_method present_method;
51 	int width, height;
52 	int fullscreen;
53 	float pointer_x, pointer_y;
54 	int draw_cursor;
55 
56 	struct wl_list output_list;
57 	struct fs_output *current_output;
58 };
59 
60 static void
fullscreen_handler(struct window * window,void * data)61 fullscreen_handler(struct window *window, void *data)
62 {
63 	struct fullscreen *fullscreen = data;
64 
65 	fullscreen->fullscreen ^= 1;
66 	window_set_fullscreen(window, fullscreen->fullscreen);
67 }
68 
69 static void
draw_string(cairo_t * cr,const char * fmt,...)70 draw_string(cairo_t *cr,
71 	    const char *fmt, ...)
72 {
73 	char buffer[4096];
74 	char *p, *end;
75 	va_list argp;
76 	cairo_text_extents_t text_extents;
77 	cairo_font_extents_t font_extents;
78 
79 	cairo_save(cr);
80 
81 	cairo_select_font_face(cr, "sans",
82 			       CAIRO_FONT_SLANT_NORMAL,
83 			       CAIRO_FONT_WEIGHT_NORMAL);
84 	cairo_set_font_size(cr, 14);
85 
86 	cairo_font_extents (cr, &font_extents);
87 
88 	va_start(argp, fmt);
89 
90 	vsnprintf(buffer, sizeof(buffer), fmt, argp);
91 
92 	p = buffer;
93 	while (*p) {
94 		end = strchr(p, '\n');
95 		if (end)
96 			*end = 0;
97 
98 		cairo_show_text(cr, p);
99 		cairo_text_extents (cr, p, &text_extents);
100 		cairo_rel_move_to (cr, -text_extents.x_advance, font_extents.height);
101 
102 		if (end)
103 			p = end + 1;
104 		else
105 			break;
106 	}
107 
108 	va_end(argp);
109 
110 	cairo_restore(cr);
111 
112 }
113 
114 static void
redraw_handler(struct widget * widget,void * data)115 redraw_handler(struct widget *widget, void *data)
116 {
117 	struct fullscreen *fullscreen = data;
118 	struct rectangle allocation;
119 	cairo_surface_t *surface;
120 	cairo_t *cr;
121 	int i;
122 	double x, y, border;
123 	const char *method_name[] = { "default", "center", "zoom", "zoom_crop", "stretch"};
124 
125 	surface = window_get_surface(fullscreen->window);
126 	if (surface == NULL ||
127 	    cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
128 		fprintf(stderr, "failed to create cairo egl surface\n");
129 		return;
130 	}
131 
132 	widget_get_allocation(fullscreen->widget, &allocation);
133 
134 	cr = widget_cairo_create(widget);
135 
136 	cairo_set_source_rgb(cr, 0, 0, 0);
137 	cairo_paint (cr);
138 
139 	cairo_set_source_rgb(cr, 0, 0, 1);
140 	cairo_set_line_width (cr, 10);
141 	cairo_rectangle(cr, 5, 5, allocation.width - 10, allocation.height - 10);
142 	cairo_stroke (cr);
143 
144 	cairo_move_to(cr,
145 		      allocation.x + 15,
146 		      allocation.y + 25);
147 	cairo_set_source_rgb(cr, 1, 1, 1);
148 
149 	if (fullscreen->fshell) {
150 		draw_string(cr,
151 			    "Surface size: %d, %d\n"
152 			    "Scale: %d, transform: %d\n"
153 			    "Pointer: %f,%f\n"
154 			    "Output: %s, present method: %s\n"
155 			    "Keys: (s)cale, (t)ransform, si(z)e, (m)ethod,\n"
156 			    "      (o)utput, modes(w)itch, (q)uit\n",
157 			    fullscreen->width, fullscreen->height,
158 			    window_get_buffer_scale (fullscreen->window),
159 			    window_get_buffer_transform (fullscreen->window),
160 			    fullscreen->pointer_x, fullscreen->pointer_y,
161 			    method_name[fullscreen->present_method],
162 			    fullscreen->current_output ? output_get_model(fullscreen->current_output->output): "null");
163 	} else {
164 		draw_string(cr,
165 			    "Surface size: %d, %d\n"
166 			    "Scale: %d, transform: %d\n"
167 			    "Pointer: %f,%f\n"
168 			    "Fullscreen: %d\n"
169 			    "Keys: (s)cale, (t)ransform, si(z)e, (f)ullscreen, (q)uit\n",
170 			    fullscreen->width, fullscreen->height,
171 			    window_get_buffer_scale (fullscreen->window),
172 			    window_get_buffer_transform (fullscreen->window),
173 			    fullscreen->pointer_x, fullscreen->pointer_y,
174 			    fullscreen->fullscreen);
175 	}
176 
177 	y = 100;
178 	i = 0;
179 	while (y + 60 < fullscreen->height) {
180 		border = (i++ % 2 == 0) ? 1 : 0.5;
181 
182 		x = 50;
183 		cairo_set_line_width (cr, border);
184 		while (x + 70 < fullscreen->width) {
185 			if (window_has_focus(fullscreen->window) &&
186 			    fullscreen->pointer_x >= x && fullscreen->pointer_x < x + 50 &&
187 			    fullscreen->pointer_y >= y && fullscreen->pointer_y < y + 40) {
188 				cairo_set_source_rgb(cr, 1, 0, 0);
189 				cairo_rectangle(cr,
190 						x, y,
191 						50, 40);
192 				cairo_fill(cr);
193 			}
194 			cairo_set_source_rgb(cr, 0, 1, 0);
195 			cairo_rectangle(cr,
196 					x + border/2.0, y + border/2.0,
197 					50, 40);
198 			cairo_stroke(cr);
199 			x += 60;
200 		}
201 
202 		y += 50;
203 	}
204 
205 	if (window_has_focus(fullscreen->window) && fullscreen->draw_cursor) {
206 		cairo_set_source_rgb(cr, 1, 1, 1);
207 		cairo_set_line_width (cr, 8);
208 		cairo_move_to(cr,
209 			      fullscreen->pointer_x - 12,
210 			      fullscreen->pointer_y - 12);
211 		cairo_line_to(cr,
212 			      fullscreen->pointer_x + 12,
213 			      fullscreen->pointer_y + 12);
214 		cairo_stroke(cr);
215 
216 		cairo_move_to(cr,
217 			      fullscreen->pointer_x + 12,
218 			      fullscreen->pointer_y - 12);
219 		cairo_line_to(cr,
220 			      fullscreen->pointer_x - 12,
221 			      fullscreen->pointer_y + 12);
222 		cairo_stroke(cr);
223 
224 		cairo_set_source_rgb(cr, 0, 0, 0);
225 		cairo_set_line_width (cr, 4);
226 		cairo_move_to(cr,
227 			      fullscreen->pointer_x - 10,
228 			      fullscreen->pointer_y - 10);
229 		cairo_line_to(cr,
230 			      fullscreen->pointer_x + 10,
231 			      fullscreen->pointer_y + 10);
232 		cairo_stroke(cr);
233 
234 		cairo_move_to(cr,
235 			      fullscreen->pointer_x + 10,
236 			      fullscreen->pointer_y - 10);
237 		cairo_line_to(cr,
238 			      fullscreen->pointer_x - 10,
239 			      fullscreen->pointer_y + 10);
240 		cairo_stroke(cr);
241 	}
242 
243 	cairo_destroy(cr);
244 }
245 
246 static void
key_handler(struct window * window,struct input * input,uint32_t time,uint32_t key,uint32_t sym,enum wl_keyboard_key_state state,void * data)247 key_handler(struct window *window, struct input *input, uint32_t time,
248 	    uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
249 	    void *data)
250 {
251 	struct fullscreen *fullscreen = data;
252 	int transform, scale;
253 	static int current_size = 0;
254 	struct fs_output *fsout;
255 	struct wl_output *wl_output;
256 	int widths[] = { 640, 320, 800, 400 };
257 	int heights[] = { 480, 240, 600, 300 };
258 
259 	if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
260 		return;
261 
262 	switch (sym) {
263 	case XKB_KEY_t:
264 		transform = window_get_buffer_transform (window);
265 		transform = (transform + 1) % 8;
266 		window_set_buffer_transform(window, transform);
267 		window_schedule_redraw(window);
268 		break;
269 
270 	case XKB_KEY_s:
271 		scale = window_get_buffer_scale (window);
272 		if (scale == 1)
273 			scale = 2;
274 		else
275 			scale = 1;
276 		window_set_buffer_scale(window, scale);
277 		window_schedule_redraw(window);
278 		break;
279 
280 	case XKB_KEY_z:
281 		current_size = (current_size + 1) % 4;
282 		fullscreen->width = widths[current_size];
283 		fullscreen->height = heights[current_size];
284 		window_schedule_resize(fullscreen->window,
285 				       fullscreen->width, fullscreen->height);
286 		break;
287 
288 	case XKB_KEY_m:
289 		if (!fullscreen->fshell)
290 			break;
291 
292 		wl_output = NULL;
293 		if (fullscreen->current_output)
294 			wl_output = output_get_wl_output(fullscreen->current_output->output);
295 		fullscreen->present_method = (fullscreen->present_method + 1) % 5;
296 		_wl_fullscreen_shell_present_surface(fullscreen->fshell,
297 						     window_get_wl_surface(fullscreen->window),
298 						     fullscreen->present_method,
299 						     wl_output);
300 		window_schedule_redraw(window);
301 		break;
302 
303 	case XKB_KEY_o:
304 		if (!fullscreen->fshell)
305 			break;
306 
307 		fsout = fullscreen->current_output;
308 		wl_output = fsout ? output_get_wl_output(fsout->output) : NULL;
309 
310 		/* Clear the current presentation */
311 		_wl_fullscreen_shell_present_surface(fullscreen->fshell, NULL,
312 						     0, wl_output);
313 
314 		if (fullscreen->current_output) {
315 			if (fullscreen->current_output->link.next == &fullscreen->output_list)
316 				fsout = NULL;
317 			else
318 				fsout = wl_container_of(fullscreen->current_output->link.next,
319 							fsout, link);
320 		} else {
321 			fsout = wl_container_of(fullscreen->output_list.next,
322 						fsout, link);
323 		}
324 
325 		fullscreen->current_output = fsout;
326 		wl_output = fsout ? output_get_wl_output(fsout->output) : NULL;
327 		_wl_fullscreen_shell_present_surface(fullscreen->fshell,
328 						     window_get_wl_surface(fullscreen->window),
329 						     fullscreen->present_method,
330 						     wl_output);
331 		window_schedule_redraw(window);
332 		break;
333 
334 	case XKB_KEY_w:
335 		if (!fullscreen->fshell || !fullscreen->current_output)
336 			break;
337 
338 		wl_output = NULL;
339 		if (fullscreen->current_output)
340 			wl_output = output_get_wl_output(fullscreen->current_output->output);
341 		_wl_fullscreen_shell_mode_feedback_destroy(
342 			_wl_fullscreen_shell_present_surface_for_mode(fullscreen->fshell,
343 								      window_get_wl_surface(fullscreen->window),
344 								      wl_output, 0));
345 		window_schedule_redraw(window);
346 		break;
347 
348 	case XKB_KEY_f:
349 		if (fullscreen->fshell)
350 			break;
351 		fullscreen->fullscreen ^= 1;
352 		window_set_fullscreen(window, fullscreen->fullscreen);
353 		break;
354 
355 	case XKB_KEY_q:
356 		exit (0);
357 		break;
358 	}
359 }
360 
361 static int
motion_handler(struct widget * widget,struct input * input,uint32_t time,float x,float y,void * data)362 motion_handler(struct widget *widget,
363 	       struct input *input,
364 	       uint32_t time,
365 	       float x,
366 	       float y, void *data)
367 {
368 	struct fullscreen *fullscreen = data;
369 
370 	fullscreen->pointer_x = x;
371 	fullscreen->pointer_y = y;
372 
373 	widget_schedule_redraw(widget);
374 
375 	return fullscreen->draw_cursor ? CURSOR_BLANK : CURSOR_LEFT_PTR;
376 }
377 
378 static int
enter_handler(struct widget * widget,struct input * input,float x,float y,void * data)379 enter_handler(struct widget *widget,
380 	      struct input *input,
381 	      float x, float y, void *data)
382 {
383 	struct fullscreen *fullscreen = data;
384 
385 	fullscreen->pointer_x = x;
386 	fullscreen->pointer_y = y;
387 
388 	widget_schedule_redraw(widget);
389 
390 	return fullscreen->draw_cursor ? CURSOR_BLANK : CURSOR_LEFT_PTR;
391 }
392 
393 static void
button_handler(struct widget * widget,struct input * input,uint32_t time,uint32_t button,enum wl_pointer_button_state state,void * data)394 button_handler(struct widget *widget,
395 	       struct input *input, uint32_t time,
396 	       uint32_t button, enum wl_pointer_button_state state, void *data)
397 {
398 	struct fullscreen *fullscreen = data;
399 
400 	switch (button) {
401 	case BTN_LEFT:
402 		if (state == WL_POINTER_BUTTON_STATE_PRESSED)
403 			window_move(fullscreen->window, input,
404 				    display_get_serial(fullscreen->display));
405 		break;
406 	case BTN_RIGHT:
407 		if (state == WL_POINTER_BUTTON_STATE_PRESSED)
408 			window_show_frame_menu(fullscreen->window, input, time);
409 		break;
410 	}
411 }
412 
413 static void
touch_handler(struct widget * widget,struct input * input,uint32_t serial,uint32_t time,int32_t id,float x,float y,void * data)414 touch_handler(struct widget *widget, struct input *input,
415 		   uint32_t serial, uint32_t time, int32_t id,
416 		   float x, float y, void *data)
417 {
418 	struct fullscreen *fullscreen = data;
419 	window_move(fullscreen->window, input, display_get_serial(fullscreen->display));
420 }
421 
422 static void
fshell_capability_handler(void * data,struct _wl_fullscreen_shell * fshell,uint32_t capability)423 fshell_capability_handler(void *data, struct _wl_fullscreen_shell *fshell,
424 			  uint32_t capability)
425 {
426 	struct fullscreen *fullscreen = data;
427 
428 	switch (capability) {
429 	case _WL_FULLSCREEN_SHELL_CAPABILITY_CURSOR_PLANE:
430 		fullscreen->draw_cursor = 0;
431 		break;
432 	default:
433 		break;
434 	}
435 }
436 
437 struct _wl_fullscreen_shell_listener fullscreen_shell_listener = {
438 	fshell_capability_handler
439 };
440 
441 static void
usage(int error_code)442 usage(int error_code)
443 {
444 	fprintf(stderr, "Usage: fullscreen [OPTIONS]\n\n"
445 		"   -w <width>\tSet window width to <width>\n"
446 		"   -h <height>\tSet window height to <height>\n"
447 		"   --help\tShow this help text\n\n");
448 
449 	exit(error_code);
450 }
451 
452 static void
output_handler(struct output * output,void * data)453 output_handler(struct output *output, void *data)
454 {
455 	struct fullscreen *fullscreen = data;
456 	struct fs_output *fsout;
457 
458 	/* If we've already seen this one, don't add it to the list */
459 	wl_list_for_each(fsout, &fullscreen->output_list, link)
460 		if (fsout->output == output)
461 			return;
462 
463 	fsout = calloc(1, sizeof *fsout);
464 	fsout->output = output;
465 	wl_list_insert(&fullscreen->output_list, &fsout->link);
466 }
467 
468 static void
global_handler(struct display * display,uint32_t id,const char * interface,uint32_t version,void * data)469 global_handler(struct display *display, uint32_t id, const char *interface,
470 	       uint32_t version, void *data)
471 {
472 	struct fullscreen *fullscreen = data;
473 
474 	if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
475 		fullscreen->fshell = display_bind(display, id,
476 						  &_wl_fullscreen_shell_interface,
477 						  1);
478 		_wl_fullscreen_shell_add_listener(fullscreen->fshell,
479 						  &fullscreen_shell_listener,
480 						  fullscreen);
481 	}
482 }
483 
main(int argc,char * argv[])484 int main(int argc, char *argv[])
485 {
486 	struct fullscreen fullscreen;
487 	struct display *d;
488 	int i;
489 
490 	fullscreen.width = 640;
491 	fullscreen.height = 480;
492 	fullscreen.fullscreen = 0;
493 	fullscreen.present_method = _WL_FULLSCREEN_SHELL_PRESENT_METHOD_DEFAULT;
494 	wl_list_init(&fullscreen.output_list);
495 	fullscreen.current_output = NULL;
496 
497 	for (i = 1; i < argc; i++) {
498 		if (strcmp(argv[i], "-w") == 0) {
499 			if (++i >= argc)
500 				usage(EXIT_FAILURE);
501 
502 			fullscreen.width = atol(argv[i]);
503 		} else if (strcmp(argv[i], "-h") == 0) {
504 			if (++i >= argc)
505 				usage(EXIT_FAILURE);
506 
507 			fullscreen.height = atol(argv[i]);
508 		} else if (strcmp(argv[i], "--help") == 0)
509 			usage(EXIT_SUCCESS);
510 		else
511 			usage(EXIT_FAILURE);
512 	}
513 
514 	d = display_create(&argc, argv);
515 	if (d == NULL) {
516 		fprintf(stderr, "failed to create display: %m\n");
517 		return -1;
518 	}
519 
520 	fullscreen.display = d;
521 	fullscreen.fshell = NULL;
522 	display_set_user_data(fullscreen.display, &fullscreen);
523 	display_set_global_handler(fullscreen.display, global_handler);
524 	display_set_output_configure_handler(fullscreen.display, output_handler);
525 
526 	if (fullscreen.fshell) {
527 		fullscreen.window = window_create_custom(d);
528 		_wl_fullscreen_shell_present_surface(fullscreen.fshell,
529 						     window_get_wl_surface(fullscreen.window),
530 						     fullscreen.present_method,
531 						     NULL);
532 		/* If we get the CURSOR_PLANE capability, we'll change this */
533 		fullscreen.draw_cursor = 1;
534 	} else {
535 		fullscreen.window = window_create(d);
536 		fullscreen.draw_cursor = 0;
537 	}
538 
539 	fullscreen.widget =
540 		window_add_widget(fullscreen.window, &fullscreen);
541 
542 	window_set_title(fullscreen.window, "Fullscreen");
543 
544 	widget_set_transparent(fullscreen.widget, 0);
545 
546 	widget_set_default_cursor(fullscreen.widget, CURSOR_LEFT_PTR);
547 	widget_set_redraw_handler(fullscreen.widget, redraw_handler);
548 	widget_set_button_handler(fullscreen.widget, button_handler);
549 	widget_set_motion_handler(fullscreen.widget, motion_handler);
550 	widget_set_enter_handler(fullscreen.widget, enter_handler);
551 
552 	widget_set_touch_down_handler(fullscreen.widget, touch_handler);
553 
554 	window_set_key_handler(fullscreen.window, key_handler);
555 	window_set_fullscreen_handler(fullscreen.window, fullscreen_handler);
556 
557 	window_set_user_data(fullscreen.window, &fullscreen);
558 	/* Hack to set minimum allocation so we can shrink later */
559 	window_schedule_resize(fullscreen.window,
560 			       1, 1);
561 	window_schedule_resize(fullscreen.window,
562 			       fullscreen.width, fullscreen.height);
563 
564 	display_run(d);
565 
566 	widget_destroy(fullscreen.widget);
567 	window_destroy(fullscreen.window);
568 	display_destroy(d);
569 
570 	return 0;
571 }
572