• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 		if (fullscreen->fullscreen)
284 			break;
285 
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 
293 	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 		zwp_fullscreen_shell_v1_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 		zwp_fullscreen_shell_v1_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 		zwp_fullscreen_shell_v1_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 		zwp_fullscreen_shell_mode_feedback_v1_destroy(
347 			zwp_fullscreen_shell_v1_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 
353 	case XKB_KEY_f:
354 		if (fullscreen->fshell)
355 			break;
356 		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 
366 static int
motion_handler(struct widget * widget,struct input * input,uint32_t time,float x,float y,void * data)367 motion_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);
379 
380 	return fullscreen->draw_cursor ? CURSOR_BLANK : CURSOR_LEFT_PTR;
381 }
382 
383 static int
enter_handler(struct widget * widget,struct input * input,float x,float y,void * data)384 enter_handler(struct widget *widget,
385 	      struct input *input,
386 	      float x, float y, void *data)
387 {
388 	struct fullscreen *fullscreen = data;
389 
390 	fullscreen->pointer_x = x;
391 	fullscreen->pointer_y = y;
392 
393 	widget_schedule_redraw(widget);
394 
395 	return fullscreen->draw_cursor ? CURSOR_BLANK : CURSOR_LEFT_PTR;
396 }
397 
398 static void
button_handler(struct widget * widget,struct input * input,uint32_t time,uint32_t button,enum wl_pointer_button_state state,void * data)399 button_handler(struct widget *widget,
400 	       struct input *input, uint32_t time,
401 	       uint32_t button, enum wl_pointer_button_state state, void *data)
402 {
403 	struct fullscreen *fullscreen = data;
404 
405 	switch (button) {
406 	case BTN_LEFT:
407 		if (state == WL_POINTER_BUTTON_STATE_PRESSED)
408 			window_move(fullscreen->window, input,
409 				    display_get_serial(fullscreen->display));
410 		break;
411 	case BTN_RIGHT:
412 		if (state == WL_POINTER_BUTTON_STATE_PRESSED)
413 			window_show_frame_menu(fullscreen->window, input, time);
414 		break;
415 	}
416 }
417 
418 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)419 touch_handler(struct widget *widget, struct input *input,
420 		   uint32_t serial, uint32_t time, int32_t id,
421 		   float x, float y, void *data)
422 {
423 	struct fullscreen *fullscreen = data;
424 	window_move(fullscreen->window, input, display_get_serial(fullscreen->display));
425 }
426 
427 static void
fshell_capability_handler(void * data,struct zwp_fullscreen_shell_v1 * fshell,uint32_t capability)428 fshell_capability_handler(void *data, struct zwp_fullscreen_shell_v1 *fshell,
429 			  uint32_t capability)
430 {
431 	struct fullscreen *fullscreen = data;
432 
433 	switch (capability) {
434 	case ZWP_FULLSCREEN_SHELL_V1_CAPABILITY_CURSOR_PLANE:
435 		fullscreen->draw_cursor = 0;
436 		break;
437 	default:
438 		break;
439 	}
440 }
441 
442 struct zwp_fullscreen_shell_v1_listener fullscreen_shell_listener = {
443 	fshell_capability_handler
444 };
445 
446 static void
usage(int error_code)447 usage(int error_code)
448 {
449 	fprintf(stderr, "Usage: fullscreen [OPTIONS]\n\n"
450 		"   -w <width>\tSet window width to <width>\n"
451 		"   -h <height>\tSet window height to <height>\n"
452 		"   --help\tShow this help text\n\n");
453 
454 	exit(error_code);
455 }
456 
457 static void
output_handler(struct output * output,void * data)458 output_handler(struct output *output, void *data)
459 {
460 	struct fullscreen *fullscreen = data;
461 	struct fs_output *fsout;
462 
463 	/* If we've already seen this one, don't add it to the list */
464 	wl_list_for_each(fsout, &fullscreen->output_list, link)
465 		if (fsout->output == output)
466 			return;
467 
468 	fsout = zalloc(sizeof *fsout);
469 	if (fsout == NULL) {
470 		fprintf(stderr, "out of memory in output_handler\n");
471 		return;
472 	}
473 	fsout->output = output;
474 	wl_list_insert(&fullscreen->output_list, &fsout->link);
475 }
476 
477 static void
global_handler(struct display * display,uint32_t id,const char * interface,uint32_t version,void * data)478 global_handler(struct display *display, uint32_t id, const char *interface,
479 	       uint32_t version, void *data)
480 {
481 	struct fullscreen *fullscreen = data;
482 
483 	if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
484 		fullscreen->fshell = display_bind(display, id,
485 						  &zwp_fullscreen_shell_v1_interface,
486 						  1);
487 		zwp_fullscreen_shell_v1_add_listener(fullscreen->fshell,
488 						     &fullscreen_shell_listener,
489 						     fullscreen);
490 	}
491 }
492 
main(int argc,char * argv[])493 int main(int argc, char *argv[])
494 {
495 	struct fullscreen fullscreen;
496 	struct display *d;
497 	int i;
498 
499 	fullscreen.width = 640;
500 	fullscreen.height = 480;
501 	fullscreen.fullscreen = 0;
502 	fullscreen.present_method = ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_DEFAULT;
503 	wl_list_init(&fullscreen.output_list);
504 	fullscreen.current_output = NULL;
505 
506 	for (i = 1; i < argc; i++) {
507 		if (strcmp(argv[i], "-w") == 0) {
508 			if (++i >= argc)
509 				usage(EXIT_FAILURE);
510 
511 			fullscreen.width = atol(argv[i]);
512 		} else if (strcmp(argv[i], "-h") == 0) {
513 			if (++i >= argc)
514 				usage(EXIT_FAILURE);
515 
516 			fullscreen.height = atol(argv[i]);
517 		} else if (strcmp(argv[i], "--help") == 0)
518 			usage(EXIT_SUCCESS);
519 		else
520 			usage(EXIT_FAILURE);
521 	}
522 
523 	d = display_create(&argc, argv);
524 	if (d == NULL) {
525 		fprintf(stderr, "failed to create display: %s\n",
526 			strerror(errno));
527 		return -1;
528 	}
529 
530 	fullscreen.display = d;
531 	fullscreen.fshell = NULL;
532 	display_set_user_data(fullscreen.display, &fullscreen);
533 	display_set_global_handler(fullscreen.display, global_handler);
534 	display_set_output_configure_handler(fullscreen.display, output_handler);
535 
536 	if (fullscreen.fshell) {
537 		fullscreen.window = window_create_custom(d);
538 		zwp_fullscreen_shell_v1_present_surface(fullscreen.fshell,
539 							window_get_wl_surface(fullscreen.window),
540 							fullscreen.present_method,
541 							NULL);
542 		/* If we get the CURSOR_PLANE capability, we'll change this */
543 		fullscreen.draw_cursor = 1;
544 	} else {
545 		fullscreen.window = window_create(d);
546 		fullscreen.draw_cursor = 0;
547 	}
548 
549 	fullscreen.widget =
550 		window_add_widget(fullscreen.window, &fullscreen);
551 
552 	window_set_title(fullscreen.window, "Fullscreen");
553 
554 	widget_set_transparent(fullscreen.widget, 0);
555 
556 	widget_set_default_cursor(fullscreen.widget, CURSOR_LEFT_PTR);
557 	widget_set_redraw_handler(fullscreen.widget, redraw_handler);
558 	widget_set_button_handler(fullscreen.widget, button_handler);
559 	widget_set_motion_handler(fullscreen.widget, motion_handler);
560 	widget_set_enter_handler(fullscreen.widget, enter_handler);
561 
562 	widget_set_touch_down_handler(fullscreen.widget, touch_handler);
563 
564 	window_set_key_handler(fullscreen.window, key_handler);
565 	window_set_fullscreen_handler(fullscreen.window, fullscreen_handler);
566 
567 	window_set_user_data(fullscreen.window, &fullscreen);
568 	/* Hack to set minimum allocation so we can shrink later */
569 	window_schedule_resize(fullscreen.window,
570 			       1, 1);
571 	window_schedule_resize(fullscreen.window,
572 			       fullscreen.width, fullscreen.height);
573 
574 	display_run(d);
575 
576 	widget_destroy(fullscreen.widget);
577 	window_destroy(fullscreen.window);
578 	display_destroy(d);
579 
580 	return 0;
581 }
582