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