1 /*
2  * Copyright © 2011 Kristian Høgsberg
3  * Copyright © 2011 Collabora, Ltd.
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 <string.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <math.h>
35 #include <cairo.h>
36 #include <sys/wait.h>
37 #include <linux/input.h>
38 #include <libgen.h>
39 #include <ctype.h>
40 #include <signal.h>
41 #include <time.h>
42 
43 #include <wayland-client.h>
44 #include "window.h"
45 #include "shared/cairo-util.h"
46 #include "shared/config-parser.h"
47 #include "shared/helpers.h"
48 
49 #include "desktop-shell-client-protocol.h"
50 
51 extern char **environ; /* defined by libc */
52 
53 struct desktop {
54 	struct display *display;
55 	struct desktop_shell *shell;
56 	uint32_t interface_version;
57 	struct unlock_dialog *unlock_dialog;
58 	struct task unlock_task;
59 	struct wl_list outputs;
60 
61 	struct window *grab_window;
62 	struct widget *grab_widget;
63 
64 	struct weston_config *config;
65 	int locking;
66 
67 	enum cursor_type grab_cursor;
68 
69 	int painted;
70 };
71 
72 struct surface {
73 	void (*configure)(void *data,
74 			  struct desktop_shell *desktop_shell,
75 			  uint32_t edges, struct window *window,
76 			  int32_t width, int32_t height);
77 };
78 
79 struct panel {
80 	struct surface base;
81 	struct window *window;
82 	struct widget *widget;
83 	struct wl_list launcher_list;
84 	struct panel_clock *clock;
85 	int painted;
86 	uint32_t color;
87 };
88 
89 struct background {
90 	struct surface base;
91 	struct window *window;
92 	struct widget *widget;
93 	int painted;
94 
95 	char *image;
96 	int type;
97 	uint32_t color;
98 };
99 
100 struct output {
101 	struct wl_output *output;
102 	uint32_t server_output_id;
103 	struct wl_list link;
104 
105 	struct panel *panel;
106 	struct background *background;
107 };
108 
109 struct panel_launcher {
110 	struct widget *widget;
111 	struct panel *panel;
112 	cairo_surface_t *icon;
113 	int focused, pressed;
114 	char *path;
115 	struct wl_list link;
116 	struct wl_array envp;
117 	struct wl_array argv;
118 };
119 
120 struct panel_clock {
121 	struct widget *widget;
122 	struct panel *panel;
123 	struct task clock_task;
124 	struct event *clockev;
125 };
126 
127 struct unlock_dialog {
128 	struct window *window;
129 	struct widget *widget;
130 	struct widget *button;
131 	int button_focused;
132 	int closing;
133 	struct desktop *desktop;
134 };
135 
136 static void
137 panel_add_launchers(struct panel *panel, struct desktop *desktop);
138 
139 static void
sigchild_handler(int s)140 sigchild_handler(int s)
141 {
142 	int status;
143 	pid_t pid;
144 
145 	while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
146 		fprintf(stderr, "child %d exited\n", pid);
147 }
148 
149 static int
is_desktop_painted(struct desktop * desktop)150 is_desktop_painted(struct desktop *desktop)
151 {
152 	struct output *output;
153 
154 	wl_list_for_each(output, &desktop->outputs, link) {
155 		if (output->panel && !output->panel->painted)
156 			return 0;
157 		if (output->background && !output->background->painted)
158 			return 0;
159 	}
160 
161 	return 1;
162 }
163 
164 static void
check_desktop_ready(struct window * window)165 check_desktop_ready(struct window *window)
166 {
167 	struct display *display;
168 	struct desktop *desktop;
169 
170 	display = window_get_display(window);
171 	desktop = display_get_user_data(display);
172 
173 	if (!desktop->painted && is_desktop_painted(desktop)) {
174 		desktop->painted = 1;
175 
176 		if (desktop->interface_version >= 2)
177 			desktop_shell_desktop_ready(desktop->shell);
178 	}
179 }
180 
181 static void
panel_launcher_activate(struct panel_launcher * widget)182 panel_launcher_activate(struct panel_launcher *widget)
183 {
184 	char **argv;
185 	pid_t pid;
186 
187 	pid = fork();
188 	if (pid < 0) {
189 		fprintf(stderr, "fork failed: %s\n", strerror(errno));
190 		return;
191 	}
192 
193 	if (pid)
194 		return;
195 
196 	argv = widget->argv.data;
197 	if (execve(argv[0], argv, widget->envp.data) < 0) {
198 		fprintf(stderr, "execl '%s' failed: %s\n", argv[0],
199 		    strerror(errno));
200 		exit(1);
201 	}
202 }
203 
204 static void
panel_launcher_redraw_handler(struct widget * widget,void * data)205 panel_launcher_redraw_handler(struct widget *widget, void *data)
206 {
207 	struct panel_launcher *launcher = data;
208 	struct rectangle allocation;
209 	cairo_t *cr;
210 
211 	cr = widget_cairo_create(launcher->panel->widget);
212 
213 	widget_get_allocation(widget, &allocation);
214 	if (launcher->pressed) {
215 		allocation.x++;
216 		allocation.y++;
217 	}
218 
219 	cairo_set_source_surface(cr, launcher->icon,
220 				 allocation.x, allocation.y);
221 	cairo_paint(cr);
222 
223 	if (launcher->focused) {
224 		cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
225 		cairo_mask_surface(cr, launcher->icon,
226 				   allocation.x, allocation.y);
227 	}
228 
229 	cairo_destroy(cr);
230 }
231 
232 static int
panel_launcher_motion_handler(struct widget * widget,struct input * input,uint32_t time,float x,float y,void * data)233 panel_launcher_motion_handler(struct widget *widget, struct input *input,
234 			      uint32_t time, float x, float y, void *data)
235 {
236 	struct panel_launcher *launcher = data;
237 
238 	widget_set_tooltip(widget, basename((char *)launcher->path), x, y);
239 
240 	return CURSOR_LEFT_PTR;
241 }
242 
243 static void
set_hex_color(cairo_t * cr,uint32_t color)244 set_hex_color(cairo_t *cr, uint32_t color)
245 {
246 	cairo_set_source_rgba(cr,
247 			      ((color >> 16) & 0xff) / 255.0,
248 			      ((color >>  8) & 0xff) / 255.0,
249 			      ((color >>  0) & 0xff) / 255.0,
250 			      ((color >> 24) & 0xff) / 255.0);
251 }
252 
253 static void
panel_redraw_handler(struct widget * widget,void * data)254 panel_redraw_handler(struct widget *widget, void *data)
255 {
256 	cairo_surface_t *surface;
257 	cairo_t *cr;
258 	struct panel *panel = data;
259 
260 	cr = widget_cairo_create(panel->widget);
261 	cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
262 	set_hex_color(cr, panel->color);
263 	cairo_paint(cr);
264 
265 	cairo_destroy(cr);
266 	surface = window_get_surface(panel->window);
267 	cairo_surface_destroy(surface);
268 	panel->painted = 1;
269 	check_desktop_ready(panel->window);
270 }
271 
272 static int
panel_launcher_enter_handler(struct widget * widget,struct input * input,float x,float y,void * data)273 panel_launcher_enter_handler(struct widget *widget, struct input *input,
274 			     float x, float y, void *data)
275 {
276 	struct panel_launcher *launcher = data;
277 
278 	launcher->focused = 1;
279 	widget_schedule_redraw(widget);
280 
281 	return CURSOR_LEFT_PTR;
282 }
283 
284 static void
panel_launcher_leave_handler(struct widget * widget,struct input * input,void * data)285 panel_launcher_leave_handler(struct widget *widget,
286 			     struct input *input, void *data)
287 {
288 	struct panel_launcher *launcher = data;
289 
290 	launcher->focused = 0;
291 	widget_destroy_tooltip(widget);
292 	widget_schedule_redraw(widget);
293 }
294 
295 static void
panel_launcher_button_handler(struct widget * widget,struct input * input,uint32_t time,uint32_t button,enum wl_pointer_button_state state,void * data)296 panel_launcher_button_handler(struct widget *widget,
297 			      struct input *input, uint32_t time,
298 			      uint32_t button,
299 			      enum wl_pointer_button_state state, void *data)
300 {
301 	struct panel_launcher *launcher;
302 
303 	launcher = widget_get_user_data(widget);
304 	widget_schedule_redraw(widget);
305 	if (state == WL_POINTER_BUTTON_STATE_RELEASED)
306 		panel_launcher_activate(launcher);
307 
308 }
309 
310 static void
panel_launcher_touch_down_handler(struct widget * widget,struct input * input,uint32_t serial,uint32_t time,int32_t id,float x,float y,void * data)311 panel_launcher_touch_down_handler(struct widget *widget, struct input *input,
312 				  uint32_t serial, uint32_t time, int32_t id,
313 				  float x, float y, void *data)
314 {
315 	struct panel_launcher *launcher;
316 
317 	launcher = widget_get_user_data(widget);
318 	launcher->focused = 1;
319 	widget_schedule_redraw(widget);
320 }
321 
322 static void
panel_launcher_touch_up_handler(struct widget * widget,struct input * input,uint32_t serial,uint32_t time,int32_t id,void * data)323 panel_launcher_touch_up_handler(struct widget *widget, struct input *input,
324 				uint32_t serial, uint32_t time, int32_t id,
325 				void *data)
326 {
327 	struct panel_launcher *launcher;
328 
329 	launcher = widget_get_user_data(widget);
330 	launcher->focused = 0;
331 	widget_schedule_redraw(widget);
332 	panel_launcher_activate(launcher);
333 }
334 
335 static void
clock_func(evutil_socket_t fd,short what,void * arg)336 clock_func(evutil_socket_t fd, short what, void *arg)
337 {
338 	struct task *task = (struct task *)arg;
339 	struct panel_clock *clock =
340 		container_of(task, struct panel_clock, clock_task);
341 
342 	widget_schedule_redraw(clock->widget);
343 }
344 
345 static void
panel_clock_redraw_handler(struct widget * widget,void * data)346 panel_clock_redraw_handler(struct widget *widget, void *data)
347 {
348 	struct panel_clock *clock = data;
349 	cairo_t *cr;
350 	struct rectangle allocation;
351 	cairo_text_extents_t extents;
352 	cairo_font_extents_t font_extents;
353 	time_t rawtime;
354 	struct tm * timeinfo;
355 	char string[128];
356 
357 	time(&rawtime);
358 	timeinfo = localtime(&rawtime);
359 	strftime(string, sizeof string, "%a %b %d, %I:%M %p", timeinfo);
360 
361 	widget_get_allocation(widget, &allocation);
362 	if (allocation.width == 0)
363 		return;
364 
365 	cr = widget_cairo_create(clock->panel->widget);
366 	cairo_select_font_face(cr, "sans",
367 			       CAIRO_FONT_SLANT_NORMAL,
368 			       CAIRO_FONT_WEIGHT_NORMAL);
369 	cairo_set_font_size(cr, 14);
370 	cairo_text_extents(cr, string, &extents);
371 	cairo_font_extents (cr, &font_extents);
372 	cairo_move_to(cr, allocation.x + 5,
373 		      allocation.y + 3 * (allocation.height >> 2) + 1);
374 	cairo_set_source_rgb(cr, 0, 0, 0);
375 	cairo_show_text(cr, string);
376 	cairo_move_to(cr, allocation.x + 4,
377 		      allocation.y + 3 * (allocation.height >> 2));
378 	cairo_set_source_rgb(cr, 1, 1, 1);
379 	cairo_show_text(cr, string);
380 	cairo_destroy(cr);
381 }
382 
383 static int
clock_timer_reset(struct panel_clock * clock)384 clock_timer_reset(struct panel_clock *clock)
385 {
386 	struct timeval tv = { .tv_sec = 60, .tv_usec = 0 };
387 
388 	event_add(clock->clockev, &tv);
389 
390 	return 0;
391 }
392 
393 static void
panel_destroy_clock(struct panel_clock * clock)394 panel_destroy_clock(struct panel_clock *clock)
395 {
396 	widget_destroy(clock->widget);
397 
398 	event_free(clock->clockev);
399 
400 	free(clock);
401 }
402 
403 static void
panel_add_clock(struct panel * panel)404 panel_add_clock(struct panel *panel)
405 {
406 	struct panel_clock *clock;
407 
408 	clock = xzalloc(sizeof *clock);
409 	clock->panel = panel;
410 	panel->clock = clock;
411 
412 	clock->clock_task.run = clock_func;
413 	clock->clockev = display_add_periodic_timer
414 	    (window_get_display(panel->window), &clock->clock_task);
415 	clock_timer_reset(clock);
416 
417 	clock->widget = widget_add_widget(panel->widget, clock);
418 	widget_set_redraw_handler(clock->widget, panel_clock_redraw_handler);
419 }
420 
421 static void
panel_resize_handler(struct widget * widget,int32_t width,int32_t height,void * data)422 panel_resize_handler(struct widget *widget,
423 		     int32_t width, int32_t height, void *data)
424 {
425 	struct panel_launcher *launcher;
426 	struct panel *panel = data;
427 	int x, y, w, h;
428 
429 	x = 10;
430 	y = 16;
431 	wl_list_for_each(launcher, &panel->launcher_list, link) {
432 		w = cairo_image_surface_get_width(launcher->icon);
433 		h = cairo_image_surface_get_height(launcher->icon);
434 		widget_set_allocation(launcher->widget,
435 				      x, y - h / 2, w + 1, h + 1);
436 		x += w + 10;
437 	}
438 	h=20;
439 	w=170;
440 
441 	if (panel->clock)
442 		widget_set_allocation(panel->clock->widget,
443 				      width - w - 8, y - h / 2, w + 1, h + 1);
444 }
445 
446 static void
panel_configure(void * data,struct desktop_shell * desktop_shell,uint32_t edges,struct window * window,int32_t width,int32_t height)447 panel_configure(void *data,
448 		struct desktop_shell *desktop_shell,
449 		uint32_t edges, struct window *window,
450 		int32_t width, int32_t height)
451 {
452 	struct surface *surface = window_get_user_data(window);
453 	struct panel *panel = container_of(surface, struct panel, base);
454 
455 	window_schedule_resize(panel->window, width, 32);
456 }
457 
458 static void
panel_destroy_launcher(struct panel_launcher * launcher)459 panel_destroy_launcher(struct panel_launcher *launcher)
460 {
461 	wl_array_release(&launcher->argv);
462 	wl_array_release(&launcher->envp);
463 
464 	free(launcher->path);
465 
466 	cairo_surface_destroy(launcher->icon);
467 
468 	widget_destroy(launcher->widget);
469 	wl_list_remove(&launcher->link);
470 
471 	free(launcher);
472 }
473 
474 static void
panel_destroy(struct panel * panel)475 panel_destroy(struct panel *panel)
476 {
477 	struct panel_launcher *tmp;
478 	struct panel_launcher *launcher;
479 
480 	panel_destroy_clock(panel->clock);
481 
482 	wl_list_for_each_safe(launcher, tmp, &panel->launcher_list, link)
483 		panel_destroy_launcher(launcher);
484 
485 	widget_destroy(panel->widget);
486 	window_destroy(panel->window);
487 
488 	free(panel);
489 }
490 
491 static struct panel *
panel_create(struct desktop * desktop)492 panel_create(struct desktop *desktop)
493 {
494 	struct panel *panel;
495 	struct weston_config_section *s;
496 
497 	panel = xzalloc(sizeof *panel);
498 
499 	panel->base.configure = panel_configure;
500 	panel->window = window_create_custom(desktop->display);
501 	panel->widget = window_add_widget(panel->window, panel);
502 	wl_list_init(&panel->launcher_list);
503 
504 	window_set_title(panel->window, "panel");
505 	window_set_user_data(panel->window, panel);
506 
507 	widget_set_redraw_handler(panel->widget, panel_redraw_handler);
508 	widget_set_resize_handler(panel->widget, panel_resize_handler);
509 
510 	panel_add_clock(panel);
511 
512 	s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
513 	weston_config_section_get_uint(s, "panel-color",
514 				       &panel->color, 0xaa000000);
515 
516 	panel_add_launchers(panel, desktop);
517 
518 	return panel;
519 }
520 
521 static cairo_surface_t *
load_icon_or_fallback(const char * icon)522 load_icon_or_fallback(const char *icon)
523 {
524 	cairo_surface_t *surface = cairo_image_surface_create_from_png(icon);
525 	cairo_status_t status;
526 	cairo_t *cr;
527 
528 	status = cairo_surface_status(surface);
529 	if (status == CAIRO_STATUS_SUCCESS)
530 		return surface;
531 
532 	cairo_surface_destroy(surface);
533 	fprintf(stderr, "ERROR loading icon from file '%s', error: '%s'\n",
534 		icon, cairo_status_to_string(status));
535 
536 	/* draw fallback icon */
537 	surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
538 					     20, 20);
539 	cr = cairo_create(surface);
540 
541 	cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1);
542 	cairo_paint(cr);
543 
544 	cairo_set_source_rgba(cr, 0, 0, 0, 1);
545 	cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
546 	cairo_rectangle(cr, 0, 0, 20, 20);
547 	cairo_move_to(cr, 4, 4);
548 	cairo_line_to(cr, 16, 16);
549 	cairo_move_to(cr, 4, 16);
550 	cairo_line_to(cr, 16, 4);
551 	cairo_stroke(cr);
552 
553 	cairo_destroy(cr);
554 
555 	return surface;
556 }
557 
558 static void
panel_add_launcher(struct panel * panel,const char * icon,const char * path)559 panel_add_launcher(struct panel *panel, const char *icon, const char *path)
560 {
561 	struct panel_launcher *launcher;
562 	char *start, *p, *eq, **ps;
563 	int i, j, k;
564 
565 	launcher = xzalloc(sizeof *launcher);
566 	launcher->icon = load_icon_or_fallback(icon);
567 	launcher->path = xstrdup(path);
568 
569 	wl_array_init(&launcher->envp);
570 	wl_array_init(&launcher->argv);
571 	for (i = 0; environ[i]; i++) {
572 		ps = wl_array_add(&launcher->envp, sizeof *ps);
573 		*ps = environ[i];
574 	}
575 	j = 0;
576 
577 	start = launcher->path;
578 	while (*start) {
579 		for (p = start, eq = NULL; *p && !isspace(*p); p++)
580 			if (*p == '=')
581 				eq = p;
582 
583 		if (eq && j == 0) {
584 			ps = launcher->envp.data;
585 			for (k = 0; k < i; k++)
586 				if (strncmp(ps[k], start, eq - start) == 0) {
587 					ps[k] = start;
588 					break;
589 				}
590 			if (k == i) {
591 				ps = wl_array_add(&launcher->envp, sizeof *ps);
592 				*ps = start;
593 				i++;
594 			}
595 		} else {
596 			ps = wl_array_add(&launcher->argv, sizeof *ps);
597 			*ps = start;
598 			j++;
599 		}
600 
601 		while (*p && isspace(*p))
602 			*p++ = '\0';
603 
604 		start = p;
605 	}
606 
607 	ps = wl_array_add(&launcher->envp, sizeof *ps);
608 	*ps = NULL;
609 	ps = wl_array_add(&launcher->argv, sizeof *ps);
610 	*ps = NULL;
611 
612 	launcher->panel = panel;
613 	wl_list_insert(panel->launcher_list.prev, &launcher->link);
614 
615 	launcher->widget = widget_add_widget(panel->widget, launcher);
616 	widget_set_enter_handler(launcher->widget,
617 				 panel_launcher_enter_handler);
618 	widget_set_leave_handler(launcher->widget,
619 				   panel_launcher_leave_handler);
620 	widget_set_button_handler(launcher->widget,
621 				    panel_launcher_button_handler);
622 	widget_set_touch_down_handler(launcher->widget,
623 				      panel_launcher_touch_down_handler);
624 	widget_set_touch_up_handler(launcher->widget,
625 				    panel_launcher_touch_up_handler);
626 	widget_set_redraw_handler(launcher->widget,
627 				  panel_launcher_redraw_handler);
628 	widget_set_motion_handler(launcher->widget,
629 				  panel_launcher_motion_handler);
630 }
631 
632 enum {
633 	BACKGROUND_SCALE,
634 	BACKGROUND_SCALE_CROP,
635 	BACKGROUND_TILE
636 };
637 
638 static void
background_draw(struct widget * widget,void * data)639 background_draw(struct widget *widget, void *data)
640 {
641 	struct background *background = data;
642 	cairo_surface_t *surface, *image;
643 	cairo_pattern_t *pattern;
644 	cairo_matrix_t matrix;
645 	cairo_t *cr;
646 	double im_w, im_h;
647 	double sx, sy, s;
648 	double tx, ty;
649 	struct rectangle allocation;
650 
651 	surface = window_get_surface(background->window);
652 
653 	cr = widget_cairo_create(background->widget);
654 	cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
655 	cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
656 	cairo_paint(cr);
657 
658 	widget_get_allocation(widget, &allocation);
659 	image = NULL;
660 	if (background->image)
661 		image = load_cairo_surface(background->image);
662 	else if (background->color == 0)
663 		image = load_cairo_surface(DATADIR "/weston/pattern.png");
664 
665 	if (image && background->type != -1) {
666 		im_w = cairo_image_surface_get_width(image);
667 		im_h = cairo_image_surface_get_height(image);
668 		sx = im_w / allocation.width;
669 		sy = im_h / allocation.height;
670 
671 		pattern = cairo_pattern_create_for_surface(image);
672 
673 		switch (background->type) {
674 		case BACKGROUND_SCALE:
675 			cairo_matrix_init_scale(&matrix, sx, sy);
676 			cairo_pattern_set_matrix(pattern, &matrix);
677 			cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
678 			break;
679 		case BACKGROUND_SCALE_CROP:
680 			s = (sx < sy) ? sx : sy;
681 			/* align center */
682 			tx = (im_w - s * allocation.width) * 0.5;
683 			ty = (im_h - s * allocation.height) * 0.5;
684 			cairo_matrix_init_translate(&matrix, tx, ty);
685 			cairo_matrix_scale(&matrix, s, s);
686 			cairo_pattern_set_matrix(pattern, &matrix);
687 			cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
688 			break;
689 		case BACKGROUND_TILE:
690 			cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
691 			break;
692 		}
693 
694 		cairo_set_source(cr, pattern);
695 		cairo_pattern_destroy (pattern);
696 		cairo_surface_destroy(image);
697 	} else {
698 		set_hex_color(cr, background->color);
699 	}
700 
701 	cairo_paint(cr);
702 	cairo_destroy(cr);
703 	cairo_surface_destroy(surface);
704 
705 	background->painted = 1;
706 	check_desktop_ready(background->window);
707 }
708 
709 static void
background_configure(void * data,struct desktop_shell * desktop_shell,uint32_t edges,struct window * window,int32_t width,int32_t height)710 background_configure(void *data,
711 		     struct desktop_shell *desktop_shell,
712 		     uint32_t edges, struct window *window,
713 		     int32_t width, int32_t height)
714 {
715 	struct background *background =
716 		(struct background *) window_get_user_data(window);
717 
718 	widget_schedule_resize(background->widget, width, height);
719 }
720 
721 static void
unlock_dialog_redraw_handler(struct widget * widget,void * data)722 unlock_dialog_redraw_handler(struct widget *widget, void *data)
723 {
724 	struct unlock_dialog *dialog = data;
725 	struct rectangle allocation;
726 	cairo_surface_t *surface;
727 	cairo_t *cr;
728 	cairo_pattern_t *pat;
729 	double cx, cy, r, f;
730 
731 	cr = widget_cairo_create(widget);
732 
733 	widget_get_allocation(dialog->widget, &allocation);
734 	cairo_rectangle(cr, allocation.x, allocation.y,
735 			allocation.width, allocation.height);
736 	cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
737 	cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
738 	cairo_fill(cr);
739 
740 	cairo_translate(cr, allocation.x, allocation.y);
741 	if (dialog->button_focused)
742 		f = 1.0;
743 	else
744 		f = 0.7;
745 
746 	cx = allocation.width / 2.0;
747 	cy = allocation.height / 2.0;
748 	r = (cx < cy ? cx : cy) * 0.4;
749 	pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
750 	cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
751 	cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
752 	cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
753 	cairo_set_source(cr, pat);
754 	cairo_pattern_destroy(pat);
755 	cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
756 	cairo_fill(cr);
757 
758 	widget_set_allocation(dialog->button,
759 			      allocation.x + cx - r,
760 			      allocation.y + cy - r, 2 * r, 2 * r);
761 
762 	cairo_destroy(cr);
763 
764 	surface = window_get_surface(dialog->window);
765 	cairo_surface_destroy(surface);
766 }
767 
768 static void
unlock_dialog_button_handler(struct widget * widget,struct input * input,uint32_t time,uint32_t button,enum wl_pointer_button_state state,void * data)769 unlock_dialog_button_handler(struct widget *widget,
770 			     struct input *input, uint32_t time,
771 			     uint32_t button,
772 			     enum wl_pointer_button_state state, void *data)
773 {
774 	struct unlock_dialog *dialog = data;
775 	struct desktop *desktop = dialog->desktop;
776 
777 	if (button == BTN_LEFT) {
778 		if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
779 		    !dialog->closing) {
780 			display_defer(desktop->display, &desktop->unlock_task);
781 			dialog->closing = 1;
782 		}
783 	}
784 }
785 
786 static void
unlock_dialog_touch_down_handler(struct widget * widget,struct input * input,uint32_t serial,uint32_t time,int32_t id,float x,float y,void * data)787 unlock_dialog_touch_down_handler(struct widget *widget, struct input *input,
788 		   uint32_t serial, uint32_t time, int32_t id,
789 		   float x, float y, void *data)
790 {
791 	struct unlock_dialog *dialog = data;
792 
793 	dialog->button_focused = 1;
794 	widget_schedule_redraw(widget);
795 }
796 
797 static void
unlock_dialog_touch_up_handler(struct widget * widget,struct input * input,uint32_t serial,uint32_t time,int32_t id,void * data)798 unlock_dialog_touch_up_handler(struct widget *widget, struct input *input,
799 				uint32_t serial, uint32_t time, int32_t id,
800 				void *data)
801 {
802 	struct unlock_dialog *dialog = data;
803 	struct desktop *desktop = dialog->desktop;
804 
805 	dialog->button_focused = 0;
806 	widget_schedule_redraw(widget);
807 	display_defer(desktop->display, &desktop->unlock_task);
808 	dialog->closing = 1;
809 }
810 
811 static void
unlock_dialog_keyboard_focus_handler(struct window * window,struct input * device,void * data)812 unlock_dialog_keyboard_focus_handler(struct window *window,
813 				     struct input *device, void *data)
814 {
815 	window_schedule_redraw(window);
816 }
817 
818 static int
unlock_dialog_widget_enter_handler(struct widget * widget,struct input * input,float x,float y,void * data)819 unlock_dialog_widget_enter_handler(struct widget *widget,
820 				   struct input *input,
821 				   float x, float y, void *data)
822 {
823 	struct unlock_dialog *dialog = data;
824 
825 	dialog->button_focused = 1;
826 	widget_schedule_redraw(widget);
827 
828 	return CURSOR_LEFT_PTR;
829 }
830 
831 static void
unlock_dialog_widget_leave_handler(struct widget * widget,struct input * input,void * data)832 unlock_dialog_widget_leave_handler(struct widget *widget,
833 				   struct input *input, void *data)
834 {
835 	struct unlock_dialog *dialog = data;
836 
837 	dialog->button_focused = 0;
838 	widget_schedule_redraw(widget);
839 }
840 
841 static struct unlock_dialog *
unlock_dialog_create(struct desktop * desktop)842 unlock_dialog_create(struct desktop *desktop)
843 {
844 	struct display *display = desktop->display;
845 	struct unlock_dialog *dialog;
846 
847 	dialog = xzalloc(sizeof *dialog);
848 
849 	dialog->window = window_create_custom(display);
850 	dialog->widget = window_frame_create(dialog->window, dialog);
851 	window_set_title(dialog->window, "Unlock your desktop");
852 
853 	window_set_user_data(dialog->window, dialog);
854 	window_set_keyboard_focus_handler(dialog->window,
855 					  unlock_dialog_keyboard_focus_handler);
856 	dialog->button = widget_add_widget(dialog->widget, dialog);
857 	widget_set_redraw_handler(dialog->widget,
858 				  unlock_dialog_redraw_handler);
859 	widget_set_enter_handler(dialog->button,
860 				 unlock_dialog_widget_enter_handler);
861 	widget_set_leave_handler(dialog->button,
862 				 unlock_dialog_widget_leave_handler);
863 	widget_set_button_handler(dialog->button,
864 				  unlock_dialog_button_handler);
865 	widget_set_touch_down_handler(dialog->button,
866 				      unlock_dialog_touch_down_handler);
867 	widget_set_touch_up_handler(dialog->button,
868 				      unlock_dialog_touch_up_handler);
869 
870 	desktop_shell_set_lock_surface(desktop->shell,
871 				       window_get_wl_surface(dialog->window));
872 
873 	window_schedule_resize(dialog->window, 260, 230);
874 
875 	return dialog;
876 }
877 
878 static void
unlock_dialog_destroy(struct unlock_dialog * dialog)879 unlock_dialog_destroy(struct unlock_dialog *dialog)
880 {
881 	window_destroy(dialog->window);
882 	free(dialog);
883 }
884 
885 static void
unlock_dialog_finish(evutil_socket_t fd,short what,void * arg)886 unlock_dialog_finish(evutil_socket_t fd, short what, void *arg)
887 {
888 	struct task *task = (struct task *)arg;
889 	struct desktop *desktop =
890 		container_of(task, struct desktop, unlock_task);
891 
892 	desktop_shell_unlock(desktop->shell);
893 	unlock_dialog_destroy(desktop->unlock_dialog);
894 	desktop->unlock_dialog = NULL;
895 }
896 
897 static void
desktop_shell_configure(void * data,struct desktop_shell * desktop_shell,uint32_t edges,struct wl_surface * surface,int32_t width,int32_t height)898 desktop_shell_configure(void *data,
899 			struct desktop_shell *desktop_shell,
900 			uint32_t edges,
901 			struct wl_surface *surface,
902 			int32_t width, int32_t height)
903 {
904 	struct window *window = wl_surface_get_user_data(surface);
905 	struct surface *s = window_get_user_data(window);
906 
907 	s->configure(data, desktop_shell, edges, window, width, height);
908 }
909 
910 static void
desktop_shell_prepare_lock_surface(void * data,struct desktop_shell * desktop_shell)911 desktop_shell_prepare_lock_surface(void *data,
912 				   struct desktop_shell *desktop_shell)
913 {
914 	struct desktop *desktop = data;
915 
916 	if (!desktop->locking) {
917 		desktop_shell_unlock(desktop->shell);
918 		return;
919 	}
920 
921 	if (!desktop->unlock_dialog) {
922 		desktop->unlock_dialog = unlock_dialog_create(desktop);
923 		desktop->unlock_dialog->desktop = desktop;
924 	}
925 }
926 
927 static void
desktop_shell_grab_cursor(void * data,struct desktop_shell * desktop_shell,uint32_t cursor)928 desktop_shell_grab_cursor(void *data,
929 			  struct desktop_shell *desktop_shell,
930 			  uint32_t cursor)
931 {
932 	struct desktop *desktop = data;
933 
934 	switch (cursor) {
935 	case DESKTOP_SHELL_CURSOR_NONE:
936 		desktop->grab_cursor = CURSOR_BLANK;
937 		break;
938 	case DESKTOP_SHELL_CURSOR_BUSY:
939 		desktop->grab_cursor = CURSOR_WATCH;
940 		break;
941 	case DESKTOP_SHELL_CURSOR_MOVE:
942 		desktop->grab_cursor = CURSOR_DRAGGING;
943 		break;
944 	case DESKTOP_SHELL_CURSOR_RESIZE_TOP:
945 		desktop->grab_cursor = CURSOR_TOP;
946 		break;
947 	case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM:
948 		desktop->grab_cursor = CURSOR_BOTTOM;
949 		break;
950 	case DESKTOP_SHELL_CURSOR_RESIZE_LEFT:
951 		desktop->grab_cursor = CURSOR_LEFT;
952 		break;
953 	case DESKTOP_SHELL_CURSOR_RESIZE_RIGHT:
954 		desktop->grab_cursor = CURSOR_RIGHT;
955 		break;
956 	case DESKTOP_SHELL_CURSOR_RESIZE_TOP_LEFT:
957 		desktop->grab_cursor = CURSOR_TOP_LEFT;
958 		break;
959 	case DESKTOP_SHELL_CURSOR_RESIZE_TOP_RIGHT:
960 		desktop->grab_cursor = CURSOR_TOP_RIGHT;
961 		break;
962 	case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_LEFT:
963 		desktop->grab_cursor = CURSOR_BOTTOM_LEFT;
964 		break;
965 	case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_RIGHT:
966 		desktop->grab_cursor = CURSOR_BOTTOM_RIGHT;
967 		break;
968 	case DESKTOP_SHELL_CURSOR_ARROW:
969 	default:
970 		desktop->grab_cursor = CURSOR_LEFT_PTR;
971 	}
972 }
973 
974 static const struct desktop_shell_listener listener = {
975 	desktop_shell_configure,
976 	desktop_shell_prepare_lock_surface,
977 	desktop_shell_grab_cursor
978 };
979 
980 static void
background_destroy(struct background * background)981 background_destroy(struct background *background)
982 {
983 	widget_destroy(background->widget);
984 	window_destroy(background->window);
985 
986 	free(background->image);
987 	free(background);
988 }
989 
990 static struct background *
background_create(struct desktop * desktop)991 background_create(struct desktop *desktop)
992 {
993 	struct background *background;
994 	struct weston_config_section *s;
995 	char *type;
996 
997 	background = xzalloc(sizeof *background);
998 	background->base.configure = background_configure;
999 	background->window = window_create_custom(desktop->display);
1000 	background->widget = window_add_widget(background->window, background);
1001 	window_set_user_data(background->window, background);
1002 	widget_set_redraw_handler(background->widget, background_draw);
1003 	widget_set_transparent(background->widget, 0);
1004 	window_set_preferred_format(background->window,
1005 				    WINDOW_PREFERRED_FORMAT_RGB565);
1006 
1007 	s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
1008 	weston_config_section_get_string(s, "background-image",
1009 					 &background->image, NULL);
1010 	weston_config_section_get_uint(s, "background-color",
1011 				       &background->color, 0);
1012 
1013 	weston_config_section_get_string(s, "background-type",
1014 					 &type, "tile");
1015 	if (type == NULL) {
1016 		fprintf(stderr, "%s: out of memory\n", getprogname());
1017 		exit(EXIT_FAILURE);
1018 	}
1019 
1020 	if (strcmp(type, "scale") == 0) {
1021 		background->type = BACKGROUND_SCALE;
1022 	} else if (strcmp(type, "scale-crop") == 0) {
1023 		background->type = BACKGROUND_SCALE_CROP;
1024 	} else if (strcmp(type, "tile") == 0) {
1025 		background->type = BACKGROUND_TILE;
1026 	} else {
1027 		background->type = -1;
1028 		fprintf(stderr, "invalid background-type: %s\n",
1029 			type);
1030 	}
1031 
1032 	free(type);
1033 
1034 	return background;
1035 }
1036 
1037 static int
grab_surface_enter_handler(struct widget * widget,struct input * input,float x,float y,void * data)1038 grab_surface_enter_handler(struct widget *widget, struct input *input,
1039 			   float x, float y, void *data)
1040 {
1041 	struct desktop *desktop = data;
1042 
1043 	return desktop->grab_cursor;
1044 }
1045 
1046 static void
grab_surface_destroy(struct desktop * desktop)1047 grab_surface_destroy(struct desktop *desktop)
1048 {
1049 	widget_destroy(desktop->grab_widget);
1050 	window_destroy(desktop->grab_window);
1051 }
1052 
1053 static void
grab_surface_create(struct desktop * desktop)1054 grab_surface_create(struct desktop *desktop)
1055 {
1056 	struct wl_surface *s;
1057 
1058 	desktop->grab_window = window_create_custom(desktop->display);
1059 	window_set_user_data(desktop->grab_window, desktop);
1060 
1061 	s = window_get_wl_surface(desktop->grab_window);
1062 	desktop_shell_set_grab_surface(desktop->shell, s);
1063 
1064 	desktop->grab_widget =
1065 		window_add_widget(desktop->grab_window, desktop);
1066 	/* We set the allocation to 1x1 at 0,0 so the fake enter event
1067 	 * at 0,0 will go to this widget. */
1068 	widget_set_allocation(desktop->grab_widget, 0, 0, 1, 1);
1069 
1070 	widget_set_enter_handler(desktop->grab_widget,
1071 				 grab_surface_enter_handler);
1072 }
1073 
1074 static void
output_destroy(struct output * output)1075 output_destroy(struct output *output)
1076 {
1077 	background_destroy(output->background);
1078 	if (output->panel)
1079 		panel_destroy(output->panel);
1080 	wl_output_destroy(output->output);
1081 	wl_list_remove(&output->link);
1082 
1083 	free(output);
1084 }
1085 
1086 static void
desktop_destroy_outputs(struct desktop * desktop)1087 desktop_destroy_outputs(struct desktop *desktop)
1088 {
1089 	struct output *tmp;
1090 	struct output *output;
1091 
1092 	wl_list_for_each_safe(output, tmp, &desktop->outputs, link)
1093 		output_destroy(output);
1094 }
1095 
1096 static void
output_handle_geometry(void * data,struct wl_output * wl_output,int x,int y,int physical_width,int physical_height,int subpixel,const char * make,const char * model,int transform)1097 output_handle_geometry(void *data,
1098                        struct wl_output *wl_output,
1099                        int x, int y,
1100                        int physical_width,
1101                        int physical_height,
1102                        int subpixel,
1103                        const char *make,
1104                        const char *model,
1105                        int transform)
1106 {
1107 	struct output *output = data;
1108 
1109 	if (output->panel)
1110 		window_set_buffer_transform(output->panel->window, transform);
1111 	window_set_buffer_transform(output->background->window, transform);
1112 }
1113 
1114 static void
output_handle_mode(void * data,struct wl_output * wl_output,uint32_t flags,int width,int height,int refresh)1115 output_handle_mode(void *data,
1116 		   struct wl_output *wl_output,
1117 		   uint32_t flags,
1118 		   int width,
1119 		   int height,
1120 		   int refresh)
1121 {
1122 }
1123 
1124 static void
output_handle_done(void * data,struct wl_output * wl_output)1125 output_handle_done(void *data,
1126                    struct wl_output *wl_output)
1127 {
1128 }
1129 
1130 static void
output_handle_scale(void * data,struct wl_output * wl_output,int32_t scale)1131 output_handle_scale(void *data,
1132                     struct wl_output *wl_output,
1133                     int32_t scale)
1134 {
1135 	struct output *output = data;
1136 
1137 	if (output->panel)
1138 		window_set_buffer_scale(output->panel->window, scale);
1139 	window_set_buffer_scale(output->background->window, scale);
1140 }
1141 
1142 static const struct wl_output_listener output_listener = {
1143 	output_handle_geometry,
1144 	output_handle_mode,
1145 	output_handle_done,
1146 	output_handle_scale
1147 };
1148 
1149 static int
want_panel(struct desktop * desktop)1150 want_panel(struct desktop *desktop)
1151 {
1152 	struct weston_config_section *s;
1153 	char *location = NULL;
1154 	int ret = 1;
1155 
1156 	s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
1157 	weston_config_section_get_string(s, "panel-location",
1158 					 &location, "top");
1159 
1160 	if (strcmp(location, "top") != 0)
1161 		ret = 0;
1162 
1163 	free(location);
1164 
1165 	return ret;
1166 }
1167 
1168 static void
output_init(struct output * output,struct desktop * desktop)1169 output_init(struct output *output, struct desktop *desktop)
1170 {
1171 	struct wl_surface *surface;
1172 
1173 	if (want_panel(desktop)) {
1174 		output->panel = panel_create(desktop);
1175 		surface = window_get_wl_surface(output->panel->window);
1176 		desktop_shell_set_panel(desktop->shell,
1177 					output->output, surface);
1178 	}
1179 
1180 	output->background = background_create(desktop);
1181 	surface = window_get_wl_surface(output->background->window);
1182 	desktop_shell_set_background(desktop->shell,
1183 				     output->output, surface);
1184 }
1185 
1186 static void
create_output(struct desktop * desktop,uint32_t id)1187 create_output(struct desktop *desktop, uint32_t id)
1188 {
1189 	struct output *output;
1190 
1191 	output = calloc(1, sizeof *output);
1192 	if (!output)
1193 		return;
1194 
1195 	output->output =
1196 		display_bind(desktop->display, id, &wl_output_interface, 2);
1197 	output->server_output_id = id;
1198 
1199 	wl_output_add_listener(output->output, &output_listener, output);
1200 
1201 	wl_list_insert(&desktop->outputs, &output->link);
1202 
1203 	/* On start up we may process an output global before the shell global
1204 	 * in which case we can't create the panel and background just yet */
1205 	if (desktop->shell)
1206 		output_init(output, desktop);
1207 }
1208 
1209 static void
global_handler(struct display * display,uint32_t id,const char * interface,uint32_t version,void * data)1210 global_handler(struct display *display, uint32_t id,
1211 	       const char *interface, uint32_t version, void *data)
1212 {
1213 	struct desktop *desktop = data;
1214 
1215 	if (!strcmp(interface, "desktop_shell")) {
1216 		desktop->interface_version = (version < 2) ? version : 2;
1217 		desktop->shell = display_bind(desktop->display,
1218 					      id, &desktop_shell_interface,
1219 					      desktop->interface_version);
1220 		desktop_shell_add_listener(desktop->shell, &listener, desktop);
1221 	} else if (!strcmp(interface, "wl_output")) {
1222 		create_output(desktop, id);
1223 	}
1224 }
1225 
1226 static void
global_handler_remove(struct display * display,uint32_t id,const char * interface,uint32_t version,void * data)1227 global_handler_remove(struct display *display, uint32_t id,
1228 	       const char *interface, uint32_t version, void *data)
1229 {
1230 	struct desktop *desktop = data;
1231 	struct output *output;
1232 
1233 	if (!strcmp(interface, "wl_output")) {
1234 		wl_list_for_each(output, &desktop->outputs, link) {
1235 			if (output->server_output_id == id) {
1236 				output_destroy(output);
1237 				break;
1238 			}
1239 		}
1240 	}
1241 }
1242 
1243 static void
panel_add_launchers(struct panel * panel,struct desktop * desktop)1244 panel_add_launchers(struct panel *panel, struct desktop *desktop)
1245 {
1246 	struct weston_config_section *s;
1247 	char *icon, *path;
1248 	const char *name;
1249 	int count;
1250 
1251 	count = 0;
1252 	s = NULL;
1253 	while (weston_config_next_section(desktop->config, &s, &name)) {
1254 		if (strcmp(name, "launcher") != 0)
1255 			continue;
1256 
1257 		weston_config_section_get_string(s, "icon", &icon, NULL);
1258 		weston_config_section_get_string(s, "path", &path, NULL);
1259 
1260 		if (icon != NULL && path != NULL) {
1261 			panel_add_launcher(panel, icon, path);
1262 			count++;
1263 		} else {
1264 			fprintf(stderr, "invalid launcher section\n");
1265 		}
1266 
1267 		free(icon);
1268 		free(path);
1269 	}
1270 
1271 	if (count == 0) {
1272 		/* add default launcher */
1273 		panel_add_launcher(panel,
1274 				   DATADIR "/weston/terminal.png",
1275 				   BINDIR "/weston-terminal");
1276 	}
1277 }
1278 
main(int argc,char * argv[])1279 int main(int argc, char *argv[])
1280 {
1281 	struct desktop desktop = { 0 };
1282 	struct output *output;
1283 	struct weston_config_section *s;
1284 	const char *config_file;
1285 
1286 	desktop.unlock_task.run = unlock_dialog_finish;
1287 	wl_list_init(&desktop.outputs);
1288 
1289 	config_file = weston_config_get_name_from_env();
1290 	desktop.config = weston_config_parse(config_file);
1291 	s = weston_config_get_section(desktop.config, "shell", NULL, NULL);
1292 	weston_config_section_get_bool(s, "locking", &desktop.locking, 1);
1293 
1294 	desktop.display = display_create(&argc, argv);
1295 	if (desktop.display == NULL) {
1296 		fprintf(stderr, "failed to create display: %s\n",
1297 		    strerror(errno));
1298 		return -1;
1299 	}
1300 
1301 	display_set_user_data(desktop.display, &desktop);
1302 	display_set_global_handler(desktop.display, global_handler);
1303 	display_set_global_handler_remove(desktop.display, global_handler_remove);
1304 
1305 	/* Create panel and background for outputs processed before the shell
1306 	 * global interface was processed */
1307 	wl_list_for_each(output, &desktop.outputs, link)
1308 		if (!output->panel)
1309 			output_init(output, &desktop);
1310 
1311 	grab_surface_create(&desktop);
1312 
1313 	signal(SIGCHLD, sigchild_handler);
1314 
1315 	display_run(desktop.display);
1316 
1317 	/* Cleanup */
1318 	grab_surface_destroy(&desktop);
1319 	desktop_destroy_outputs(&desktop);
1320 	if (desktop.unlock_dialog)
1321 		unlock_dialog_destroy(desktop.unlock_dialog);
1322 	desktop_shell_destroy(desktop.shell);
1323 	display_destroy(desktop.display);
1324 
1325 	return 0;
1326 }
1327