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