2 * Copyright © 2011 Kristian Høgsberg
3 * Copyright © 2011 Collabora, Ltd.
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.
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
36 #include <sys/timerfd.h>
37 #include <sys/epoll.h>
38 #include <linux/input.h>
43 #include <wayland-client.h>
45 #include "../shared/cairo-util.h"
46 #include "../shared/config-parser.h"
48 #include "desktop-shell-client-protocol.h"
50 extern char **environ; /* defined by libc */
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;
60 struct window *grab_window;
61 struct widget *grab_widget;
63 struct weston_config *config;
66 enum cursor_type grab_cursor;
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);
80 struct window *window;
81 struct widget *widget;
82 struct wl_list launcher_list;
83 struct panel_clock *clock;
90 struct window *window;
91 struct widget *widget;
100 struct wl_output *output;
101 uint32_t server_output_id;
105 struct background *background;
108 struct panel_launcher {
109 struct widget *widget;
111 cairo_surface_t *icon;
112 int focused, pressed;
115 struct wl_array envp;
116 struct wl_array argv;
120 struct widget *widget;
122 struct task clock_task;
126 struct unlock_dialog {
127 struct window *window;
128 struct widget *widget;
129 struct widget *button;
132 struct desktop *desktop;
136 panel_add_launchers(struct panel *panel, struct desktop *desktop);
139 sigchild_handler(int s)
144 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
145 fprintf(stderr, "child %d exited\n", pid);
149 menu_func(void *data, struct input *input, int index)
151 printf("Selected index %d from a panel menu.\n", index);
155 show_menu(struct panel *panel, struct input *input, uint32_t time)
158 static const char *entries[] = {
159 "Roy", "Pris", "Leon", "Zhora"
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);
169 is_desktop_painted(struct desktop *desktop)
171 struct output *output;
173 wl_list_for_each(output, &desktop->outputs, link) {
174 if (output->panel && !output->panel->painted)
176 if (output->background && !output->background->painted)
184 check_desktop_ready(struct window *window)
186 struct display *display;
187 struct desktop *desktop;
189 display = window_get_display(window);
190 desktop = display_get_user_data(display);
192 if (!desktop->painted && is_desktop_painted(desktop)) {
193 desktop->painted = 1;
195 if (desktop->interface_version >= 2)
196 desktop_shell_desktop_ready(desktop->shell);
201 panel_launcher_activate(struct panel_launcher *widget)
208 fprintf(stderr, "fork failed: %m\n");
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]);
223 panel_launcher_redraw_handler(struct widget *widget, void *data)
225 struct panel_launcher *launcher = data;
226 struct rectangle allocation;
229 cr = widget_cairo_create(launcher->panel->widget);
231 widget_get_allocation(widget, &allocation);
232 if (launcher->pressed) {
237 cairo_set_source_surface(cr, launcher->icon,
238 allocation.x, allocation.y);
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);
251 panel_launcher_motion_handler(struct widget *widget, struct input *input,
252 uint32_t time, float x, float y, void *data)
254 struct panel_launcher *launcher = data;
256 widget_set_tooltip(widget, basename((char *)launcher->path), x, y);
258 return CURSOR_LEFT_PTR;
262 set_hex_color(cairo_t *cr, uint32_t color)
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);
272 panel_redraw_handler(struct widget *widget, void *data)
274 cairo_surface_t *surface;
276 struct panel *panel = data;
278 cr = widget_cairo_create(panel->widget);
279 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
280 set_hex_color(cr, panel->color);
284 surface = window_get_surface(panel->window);
285 cairo_surface_destroy(surface);
287 check_desktop_ready(panel->window);
291 panel_launcher_enter_handler(struct widget *widget, struct input *input,
292 float x, float y, void *data)
294 struct panel_launcher *launcher = data;
296 launcher->focused = 1;
297 widget_schedule_redraw(widget);
299 return CURSOR_LEFT_PTR;
303 panel_launcher_leave_handler(struct widget *widget,
304 struct input *input, void *data)
306 struct panel_launcher *launcher = data;
308 launcher->focused = 0;
309 widget_destroy_tooltip(widget);
310 widget_schedule_redraw(widget);
314 panel_launcher_button_handler(struct widget *widget,
315 struct input *input, uint32_t time,
317 enum wl_pointer_button_state state, void *data)
319 struct panel_launcher *launcher;
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);
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)
333 struct panel_launcher *launcher;
335 launcher = widget_get_user_data(widget);
336 launcher->focused = 1;
337 widget_schedule_redraw(widget);
341 panel_launcher_touch_up_handler(struct widget *widget, struct input *input,
342 uint32_t serial, uint32_t time, int32_t id,
345 struct panel_launcher *launcher;
347 launcher = widget_get_user_data(widget);
348 launcher->focused = 0;
349 widget_schedule_redraw(widget);
350 panel_launcher_activate(launcher);
354 clock_func(struct task *task, uint32_t events)
356 struct panel_clock *clock =
357 container_of(task, struct panel_clock, clock_task);
360 if (read(clock->clock_fd, &exp, sizeof exp) != sizeof exp)
362 widget_schedule_redraw(clock->widget);
366 panel_clock_redraw_handler(struct widget *widget, void *data)
368 struct panel_clock *clock = data;
370 struct rectangle allocation;
371 cairo_text_extents_t extents;
372 cairo_font_extents_t font_extents;
374 struct tm * timeinfo;
378 timeinfo = localtime(&rawtime);
379 strftime(string, sizeof string, "%a %b %d, %I:%M %p", timeinfo);
381 widget_get_allocation(widget, &allocation);
382 if (allocation.width == 0)
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);
404 clock_timer_reset(struct panel_clock *clock)
406 struct itimerspec its;
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");
421 panel_destroy_clock(struct panel_clock *clock)
423 widget_destroy(clock->widget);
425 close(clock->clock_fd);
431 panel_add_clock(struct panel *panel)
433 struct panel_clock *clock;
436 timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
438 fprintf(stderr, "could not create timerfd\n: %m");
442 clock = xzalloc(sizeof *clock);
443 clock->panel = panel;
444 panel->clock = clock;
445 clock->clock_fd = timerfd;
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);
452 clock->widget = widget_add_widget(panel->widget, clock);
453 widget_set_redraw_handler(clock->widget, panel_clock_redraw_handler);
457 panel_button_handler(struct widget *widget,
458 struct input *input, uint32_t time,
460 enum wl_pointer_button_state state, void *data)
462 struct panel *panel = data;
464 if (button == BTN_RIGHT && state == WL_POINTER_BUTTON_STATE_PRESSED)
465 show_menu(panel, input, time);
469 panel_resize_handler(struct widget *widget,
470 int32_t width, int32_t height, void *data)
472 struct panel_launcher *launcher;
473 struct panel *panel = data;
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);
489 widget_set_allocation(panel->clock->widget,
490 width - w - 8, y - h / 2, w + 1, h + 1);
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)
499 struct surface *surface = window_get_user_data(window);
500 struct panel *panel = container_of(surface, struct panel, base);
502 window_schedule_resize(panel->window, width, 32);
506 panel_destroy_launcher(struct panel_launcher *launcher)
508 wl_array_release(&launcher->argv);
509 wl_array_release(&launcher->envp);
511 free(launcher->path);
513 cairo_surface_destroy(launcher->icon);
515 widget_destroy(launcher->widget);
516 wl_list_remove(&launcher->link);
522 panel_destroy(struct panel *panel)
524 struct panel_launcher *tmp;
525 struct panel_launcher *launcher;
527 panel_destroy_clock(panel->clock);
529 wl_list_for_each_safe(launcher, tmp, &panel->launcher_list, link)
530 panel_destroy_launcher(launcher);
532 widget_destroy(panel->widget);
533 window_destroy(panel->window);
538 static struct panel *
539 panel_create(struct desktop *desktop)
542 struct weston_config_section *s;
544 panel = xzalloc(sizeof *panel);
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);
551 window_set_title(panel->window, "panel");
552 window_set_user_data(panel->window, panel);
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);
558 panel_add_clock(panel);
560 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
561 weston_config_section_get_uint(s, "panel-color",
562 &panel->color, 0xaa000000);
564 panel_add_launchers(panel, desktop);
569 static cairo_surface_t *
570 load_icon_or_fallback(const char *icon)
572 cairo_surface_t *surface = cairo_image_surface_create_from_png(icon);
573 cairo_status_t status;
576 status = cairo_surface_status(surface);
577 if (status == CAIRO_STATUS_SUCCESS)
580 cairo_surface_destroy(surface);
581 fprintf(stderr, "ERROR loading icon from file '%s', error: '%s'\n",
582 icon, cairo_status_to_string(status));
584 /* draw fallback icon */
585 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
587 cr = cairo_create(surface);
589 cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1);
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);
607 panel_add_launcher(struct panel *panel, const char *icon, const char *path)
609 struct panel_launcher *launcher;
610 char *start, *p, *eq, **ps;
613 launcher = xzalloc(sizeof *launcher);
614 launcher->icon = load_icon_or_fallback(icon);
615 launcher->path = xstrdup(path);
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);
625 start = launcher->path;
627 for (p = start, eq = NULL; *p && !isspace(*p); p++)
632 ps = launcher->envp.data;
633 for (k = 0; k < i; k++)
634 if (strncmp(ps[k], start, eq - start) == 0) {
639 ps = wl_array_add(&launcher->envp, sizeof *ps);
644 ps = wl_array_add(&launcher->argv, sizeof *ps);
649 while (*p && isspace(*p))
655 ps = wl_array_add(&launcher->envp, sizeof *ps);
657 ps = wl_array_add(&launcher->argv, sizeof *ps);
660 launcher->panel = panel;
661 wl_list_insert(panel->launcher_list.prev, &launcher->link);
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);
682 BACKGROUND_SCALE_CROP,
687 background_draw(struct widget *widget, void *data)
689 struct background *background = data;
690 cairo_surface_t *surface, *image;
691 cairo_pattern_t *pattern;
692 cairo_matrix_t matrix;
697 struct rectangle allocation;
698 struct display *display;
699 struct wl_region *opaque;
701 surface = window_get_surface(background->window);
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);
708 widget_get_allocation(widget, &allocation);
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");
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;
721 pattern = cairo_pattern_create_for_surface(image);
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);
729 case BACKGROUND_SCALE_CROP:
730 s = (sx < sy) ? sx : sy;
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);
739 case BACKGROUND_TILE:
740 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
744 cairo_set_source(cr, pattern);
745 cairo_pattern_destroy (pattern);
746 cairo_surface_destroy(image);
748 set_hex_color(cr, background->color);
753 cairo_surface_destroy(surface);
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);
762 background->painted = 1;
763 check_desktop_ready(background->window);
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)
772 struct background *background =
773 (struct background *) window_get_user_data(window);
775 widget_schedule_resize(background->widget, width, height);
779 unlock_dialog_redraw_handler(struct widget *widget, void *data)
781 struct unlock_dialog *dialog = data;
782 struct rectangle allocation;
783 cairo_surface_t *surface;
785 cairo_pattern_t *pat;
788 cr = widget_cairo_create(widget);
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);
797 cairo_translate(cr, allocation.x, allocation.y);
798 if (dialog->button_focused)
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);
815 widget_set_allocation(dialog->button,
816 allocation.x + cx - r,
817 allocation.y + cy - r, 2 * r, 2 * r);
821 surface = window_get_surface(dialog->window);
822 cairo_surface_destroy(surface);
826 unlock_dialog_button_handler(struct widget *widget,
827 struct input *input, uint32_t time,
829 enum wl_pointer_button_state state, void *data)
831 struct unlock_dialog *dialog = data;
832 struct desktop *desktop = dialog->desktop;
834 if (button == BTN_LEFT) {
835 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
837 display_defer(desktop->display, &desktop->unlock_task);
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)
848 struct unlock_dialog *dialog = data;
850 dialog->button_focused = 1;
851 widget_schedule_redraw(widget);
855 unlock_dialog_touch_up_handler(struct widget *widget, struct input *input,
856 uint32_t serial, uint32_t time, int32_t id,
859 struct unlock_dialog *dialog = data;
860 struct desktop *desktop = dialog->desktop;
862 dialog->button_focused = 0;
863 widget_schedule_redraw(widget);
864 display_defer(desktop->display, &desktop->unlock_task);
869 unlock_dialog_keyboard_focus_handler(struct window *window,
870 struct input *device, void *data)
872 window_schedule_redraw(window);
876 unlock_dialog_widget_enter_handler(struct widget *widget,
878 float x, float y, void *data)
880 struct unlock_dialog *dialog = data;
882 dialog->button_focused = 1;
883 widget_schedule_redraw(widget);
885 return CURSOR_LEFT_PTR;
889 unlock_dialog_widget_leave_handler(struct widget *widget,
890 struct input *input, void *data)
892 struct unlock_dialog *dialog = data;
894 dialog->button_focused = 0;
895 widget_schedule_redraw(widget);
898 static struct unlock_dialog *
899 unlock_dialog_create(struct desktop *desktop)
901 struct display *display = desktop->display;
902 struct unlock_dialog *dialog;
904 dialog = xzalloc(sizeof *dialog);
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");
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);
927 desktop_shell_set_lock_surface(desktop->shell,
928 window_get_wl_surface(dialog->window));
930 window_schedule_resize(dialog->window, 260, 230);
936 unlock_dialog_destroy(struct unlock_dialog *dialog)
938 window_destroy(dialog->window);
943 unlock_dialog_finish(struct task *task, uint32_t events)
945 struct desktop *desktop =
946 container_of(task, struct desktop, unlock_task);
948 desktop_shell_unlock(desktop->shell);
949 unlock_dialog_destroy(desktop->unlock_dialog);
950 desktop->unlock_dialog = NULL;
954 desktop_shell_configure(void *data,
955 struct desktop_shell *desktop_shell,
957 struct wl_surface *surface,
958 int32_t width, int32_t height)
960 struct window *window = wl_surface_get_user_data(surface);
961 struct surface *s = window_get_user_data(window);
963 s->configure(data, desktop_shell, edges, window, width, height);
967 desktop_shell_prepare_lock_surface(void *data,
968 struct desktop_shell *desktop_shell)
970 struct desktop *desktop = data;
972 if (!desktop->locking) {
973 desktop_shell_unlock(desktop->shell);
977 if (!desktop->unlock_dialog) {
978 desktop->unlock_dialog = unlock_dialog_create(desktop);
979 desktop->unlock_dialog->desktop = desktop;
984 desktop_shell_grab_cursor(void *data,
985 struct desktop_shell *desktop_shell,
988 struct desktop *desktop = data;
991 case DESKTOP_SHELL_CURSOR_NONE:
992 desktop->grab_cursor = CURSOR_BLANK;
994 case DESKTOP_SHELL_CURSOR_BUSY:
995 desktop->grab_cursor = CURSOR_WATCH;
997 case DESKTOP_SHELL_CURSOR_MOVE:
998 desktop->grab_cursor = CURSOR_DRAGGING;
1000 case DESKTOP_SHELL_CURSOR_RESIZE_TOP:
1001 desktop->grab_cursor = CURSOR_TOP;
1003 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM:
1004 desktop->grab_cursor = CURSOR_BOTTOM;
1006 case DESKTOP_SHELL_CURSOR_RESIZE_LEFT:
1007 desktop->grab_cursor = CURSOR_LEFT;
1009 case DESKTOP_SHELL_CURSOR_RESIZE_RIGHT:
1010 desktop->grab_cursor = CURSOR_RIGHT;
1012 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_LEFT:
1013 desktop->grab_cursor = CURSOR_TOP_LEFT;
1015 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_RIGHT:
1016 desktop->grab_cursor = CURSOR_TOP_RIGHT;
1018 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_LEFT:
1019 desktop->grab_cursor = CURSOR_BOTTOM_LEFT;
1021 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_RIGHT:
1022 desktop->grab_cursor = CURSOR_BOTTOM_RIGHT;
1024 case DESKTOP_SHELL_CURSOR_ARROW:
1026 desktop->grab_cursor = CURSOR_LEFT_PTR;
1030 static const struct desktop_shell_listener listener = {
1031 desktop_shell_configure,
1032 desktop_shell_prepare_lock_surface,
1033 desktop_shell_grab_cursor
1037 background_destroy(struct background *background)
1039 widget_destroy(background->widget);
1040 window_destroy(background->window);
1042 free(background->image);
1046 static struct background *
1047 background_create(struct desktop *desktop)
1049 struct background *background;
1050 struct weston_config_section *s;
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);
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);
1068 weston_config_section_get_string(s, "background-type",
1071 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
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;
1082 background->type = -1;
1083 fprintf(stderr, "invalid background-type: %s\n",
1093 grab_surface_enter_handler(struct widget *widget, struct input *input,
1094 float x, float y, void *data)
1096 struct desktop *desktop = data;
1098 return desktop->grab_cursor;
1102 grab_surface_destroy(struct desktop *desktop)
1104 widget_destroy(desktop->grab_widget);
1105 window_destroy(desktop->grab_window);
1109 grab_surface_create(struct desktop *desktop)
1111 struct wl_surface *s;
1113 desktop->grab_window = window_create_custom(desktop->display);
1114 window_set_user_data(desktop->grab_window, desktop);
1116 s = window_get_wl_surface(desktop->grab_window);
1117 desktop_shell_set_grab_surface(desktop->shell, s);
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);
1125 widget_set_enter_handler(desktop->grab_widget,
1126 grab_surface_enter_handler);
1130 output_destroy(struct output *output)
1132 background_destroy(output->background);
1134 panel_destroy(output->panel);
1135 wl_output_destroy(output->output);
1136 wl_list_remove(&output->link);
1142 desktop_destroy_outputs(struct desktop *desktop)
1145 struct output *output;
1147 wl_list_for_each_safe(output, tmp, &desktop->outputs, link)
1148 output_destroy(output);
1152 output_handle_geometry(void *data,
1153 struct wl_output *wl_output,
1156 int physical_height,
1162 struct output *output = data;
1165 window_set_buffer_transform(output->panel->window, transform);
1166 window_set_buffer_transform(output->background->window, transform);
1170 output_handle_mode(void *data,
1171 struct wl_output *wl_output,
1180 output_handle_done(void *data,
1181 struct wl_output *wl_output)
1186 output_handle_scale(void *data,
1187 struct wl_output *wl_output,
1190 struct output *output = data;
1193 window_set_buffer_scale(output->panel->window, scale);
1194 window_set_buffer_scale(output->background->window, scale);
1197 static const struct wl_output_listener output_listener = {
1198 output_handle_geometry,
1205 want_panel(struct desktop *desktop)
1207 struct weston_config_section *s;
1208 char *location = NULL;
1211 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
1212 weston_config_section_get_string(s, "panel-location",
1215 if (strcmp(location, "top") != 0)
1224 output_init(struct output *output, struct desktop *desktop)
1226 struct wl_surface *surface;
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);
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);
1242 create_output(struct desktop *desktop, uint32_t id)
1244 struct output *output;
1246 output = calloc(1, sizeof *output);
1251 display_bind(desktop->display, id, &wl_output_interface, 2);
1252 output->server_output_id = id;
1254 wl_output_add_listener(output->output, &output_listener, output);
1256 wl_list_insert(&desktop->outputs, &output->link);
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 */
1261 output_init(output, desktop);
1265 global_handler(struct display *display, uint32_t id,
1266 const char *interface, uint32_t version, void *data)
1268 struct desktop *desktop = data;
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);
1282 global_handler_remove(struct display *display, uint32_t id,
1283 const char *interface, uint32_t version, void *data)
1285 struct desktop *desktop = data;
1286 struct output *output;
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);
1299 panel_add_launchers(struct panel *panel, struct desktop *desktop)
1301 struct weston_config_section *s;
1308 while (weston_config_next_section(desktop->config, &s, &name)) {
1309 if (strcmp(name, "launcher") != 0)
1312 weston_config_section_get_string(s, "icon", &icon, NULL);
1313 weston_config_section_get_string(s, "path", &path, NULL);
1315 if (icon != NULL && path != NULL) {
1316 panel_add_launcher(panel, icon, path);
1319 fprintf(stderr, "invalid launcher section\n");
1327 /* add default launcher */
1328 panel_add_launcher(panel,
1329 DATADIR "/weston/terminal.png",
1330 BINDIR "/weston-terminal");
1334 int main(int argc, char *argv[])
1336 struct desktop desktop = { 0 };
1337 struct output *output;
1338 struct weston_config_section *s;
1340 desktop.unlock_task.run = unlock_dialog_finish;
1341 wl_list_init(&desktop.outputs);
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);
1347 desktop.display = display_create(&argc, argv);
1348 if (desktop.display == NULL) {
1349 fprintf(stderr, "failed to create display: %m\n");
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);
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)
1361 output_init(output, &desktop);
1363 grab_surface_create(&desktop);
1365 signal(SIGCHLD, sigchild_handler);
1367 display_run(desktop.display);
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);