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
33 #include <sys/timerfd.h>
34 #include <sys/epoll.h>
35 #include <linux/input.h>
40 #include <wayland-client.h>
42 #include "../shared/cairo-util.h"
43 #include "../shared/config-parser.h"
45 #include "desktop-shell-client-protocol.h"
47 extern char **environ; /* defined by libc */
50 struct display *display;
51 struct desktop_shell *shell;
52 uint32_t interface_version;
53 struct unlock_dialog *unlock_dialog;
54 struct task unlock_task;
55 struct wl_list outputs;
57 struct window *grab_window;
58 struct widget *grab_widget;
60 struct weston_config *config;
63 enum cursor_type grab_cursor;
69 void (*configure)(void *data,
70 struct desktop_shell *desktop_shell,
71 uint32_t edges, struct window *window,
72 int32_t width, int32_t height);
77 struct window *window;
78 struct widget *widget;
79 struct wl_list launcher_list;
80 struct panel_clock *clock;
87 struct window *window;
88 struct widget *widget;
97 struct wl_output *output;
98 uint32_t server_output_id;
102 struct background *background;
105 struct panel_launcher {
106 struct widget *widget;
108 cairo_surface_t *icon;
109 int focused, pressed;
112 struct wl_array envp;
113 struct wl_array argv;
117 struct widget *widget;
119 struct task clock_task;
123 struct unlock_dialog {
124 struct window *window;
125 struct widget *widget;
126 struct widget *button;
129 struct desktop *desktop;
133 panel_add_launchers(struct panel *panel, struct desktop *desktop);
136 sigchild_handler(int s)
141 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
142 fprintf(stderr, "child %d exited\n", pid);
146 menu_func(struct window *window, struct input *input, int index, void *data)
148 printf("Selected index %d from a panel menu.\n", index);
152 show_menu(struct panel *panel, struct input *input, uint32_t time)
155 static const char *entries[] = {
156 "Roy", "Pris", "Leon", "Zhora"
159 input_get_position(input, &x, &y);
160 window_show_menu(window_get_display(panel->window),
161 input, time, panel->window,
162 x - 10, y - 10, menu_func, entries, 4);
166 is_desktop_painted(struct desktop *desktop)
168 struct output *output;
170 wl_list_for_each(output, &desktop->outputs, link) {
171 if (output->panel && !output->panel->painted)
173 if (output->background && !output->background->painted)
181 check_desktop_ready(struct window *window)
183 struct display *display;
184 struct desktop *desktop;
186 display = window_get_display(window);
187 desktop = display_get_user_data(display);
189 if (!desktop->painted && is_desktop_painted(desktop)) {
190 desktop->painted = 1;
192 if (desktop->interface_version >= 2)
193 desktop_shell_desktop_ready(desktop->shell);
198 panel_launcher_activate(struct panel_launcher *widget)
205 fprintf(stderr, "fork failed: %m\n");
212 argv = widget->argv.data;
213 if (execve(argv[0], argv, widget->envp.data) < 0) {
214 fprintf(stderr, "execl '%s' failed: %m\n", argv[0]);
220 panel_launcher_redraw_handler(struct widget *widget, void *data)
222 struct panel_launcher *launcher = data;
223 struct rectangle allocation;
226 cr = widget_cairo_create(launcher->panel->widget);
228 widget_get_allocation(widget, &allocation);
229 if (launcher->pressed) {
234 cairo_set_source_surface(cr, launcher->icon,
235 allocation.x, allocation.y);
238 if (launcher->focused) {
239 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
240 cairo_mask_surface(cr, launcher->icon,
241 allocation.x, allocation.y);
248 panel_launcher_motion_handler(struct widget *widget, struct input *input,
249 uint32_t time, float x, float y, void *data)
251 struct panel_launcher *launcher = data;
253 widget_set_tooltip(widget, basename((char *)launcher->path), x, y);
255 return CURSOR_LEFT_PTR;
259 set_hex_color(cairo_t *cr, uint32_t color)
261 cairo_set_source_rgba(cr,
262 ((color >> 16) & 0xff) / 255.0,
263 ((color >> 8) & 0xff) / 255.0,
264 ((color >> 0) & 0xff) / 255.0,
265 ((color >> 24) & 0xff) / 255.0);
269 panel_redraw_handler(struct widget *widget, void *data)
271 cairo_surface_t *surface;
273 struct panel *panel = data;
275 cr = widget_cairo_create(panel->widget);
276 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
277 set_hex_color(cr, panel->color);
281 surface = window_get_surface(panel->window);
282 cairo_surface_destroy(surface);
284 check_desktop_ready(panel->window);
288 panel_launcher_enter_handler(struct widget *widget, struct input *input,
289 float x, float y, void *data)
291 struct panel_launcher *launcher = data;
293 launcher->focused = 1;
294 widget_schedule_redraw(widget);
296 return CURSOR_LEFT_PTR;
300 panel_launcher_leave_handler(struct widget *widget,
301 struct input *input, void *data)
303 struct panel_launcher *launcher = data;
305 launcher->focused = 0;
306 widget_destroy_tooltip(widget);
307 widget_schedule_redraw(widget);
311 panel_launcher_button_handler(struct widget *widget,
312 struct input *input, uint32_t time,
314 enum wl_pointer_button_state state, void *data)
316 struct panel_launcher *launcher;
318 launcher = widget_get_user_data(widget);
319 widget_schedule_redraw(widget);
320 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
321 panel_launcher_activate(launcher);
326 panel_launcher_touch_down_handler(struct widget *widget, struct input *input,
327 uint32_t serial, uint32_t time, int32_t id,
328 float x, float y, void *data)
330 struct panel_launcher *launcher;
332 launcher = widget_get_user_data(widget);
333 launcher->focused = 1;
334 widget_schedule_redraw(widget);
338 panel_launcher_touch_up_handler(struct widget *widget, struct input *input,
339 uint32_t serial, uint32_t time, int32_t id,
342 struct panel_launcher *launcher;
344 launcher = widget_get_user_data(widget);
345 launcher->focused = 0;
346 widget_schedule_redraw(widget);
347 panel_launcher_activate(launcher);
351 clock_func(struct task *task, uint32_t events)
353 struct panel_clock *clock =
354 container_of(task, struct panel_clock, clock_task);
357 if (read(clock->clock_fd, &exp, sizeof exp) != sizeof exp)
359 widget_schedule_redraw(clock->widget);
363 panel_clock_redraw_handler(struct widget *widget, void *data)
365 struct panel_clock *clock = data;
367 struct rectangle allocation;
368 cairo_text_extents_t extents;
369 cairo_font_extents_t font_extents;
371 struct tm * timeinfo;
375 timeinfo = localtime(&rawtime);
376 strftime(string, sizeof string, "%a %b %d, %I:%M %p", timeinfo);
378 widget_get_allocation(widget, &allocation);
379 if (allocation.width == 0)
382 cr = widget_cairo_create(clock->panel->widget);
383 cairo_select_font_face(cr, "sans",
384 CAIRO_FONT_SLANT_NORMAL,
385 CAIRO_FONT_WEIGHT_NORMAL);
386 cairo_set_font_size(cr, 14);
387 cairo_text_extents(cr, string, &extents);
388 cairo_font_extents (cr, &font_extents);
389 cairo_move_to(cr, allocation.x + 5,
390 allocation.y + 3 * (allocation.height >> 2) + 1);
391 cairo_set_source_rgb(cr, 0, 0, 0);
392 cairo_show_text(cr, string);
393 cairo_move_to(cr, allocation.x + 4,
394 allocation.y + 3 * (allocation.height >> 2));
395 cairo_set_source_rgb(cr, 1, 1, 1);
396 cairo_show_text(cr, string);
401 clock_timer_reset(struct panel_clock *clock)
403 struct itimerspec its;
405 its.it_interval.tv_sec = 60;
406 its.it_interval.tv_nsec = 0;
407 its.it_value.tv_sec = 60;
408 its.it_value.tv_nsec = 0;
409 if (timerfd_settime(clock->clock_fd, 0, &its, NULL) < 0) {
410 fprintf(stderr, "could not set timerfd\n: %m");
418 panel_destroy_clock(struct panel_clock *clock)
420 widget_destroy(clock->widget);
422 close(clock->clock_fd);
428 panel_add_clock(struct panel *panel)
430 struct panel_clock *clock;
433 timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
435 fprintf(stderr, "could not create timerfd\n: %m");
439 clock = xzalloc(sizeof *clock);
440 clock->panel = panel;
441 panel->clock = clock;
442 clock->clock_fd = timerfd;
444 clock->clock_task.run = clock_func;
445 display_watch_fd(window_get_display(panel->window), clock->clock_fd,
446 EPOLLIN, &clock->clock_task);
447 clock_timer_reset(clock);
449 clock->widget = widget_add_widget(panel->widget, clock);
450 widget_set_redraw_handler(clock->widget, panel_clock_redraw_handler);
454 panel_button_handler(struct widget *widget,
455 struct input *input, uint32_t time,
457 enum wl_pointer_button_state state, void *data)
459 struct panel *panel = data;
461 if (button == BTN_RIGHT && state == WL_POINTER_BUTTON_STATE_PRESSED)
462 show_menu(panel, input, time);
466 panel_resize_handler(struct widget *widget,
467 int32_t width, int32_t height, void *data)
469 struct panel_launcher *launcher;
470 struct panel *panel = data;
475 wl_list_for_each(launcher, &panel->launcher_list, link) {
476 w = cairo_image_surface_get_width(launcher->icon);
477 h = cairo_image_surface_get_height(launcher->icon);
478 widget_set_allocation(launcher->widget,
479 x, y - h / 2, w + 1, h + 1);
486 widget_set_allocation(panel->clock->widget,
487 width - w - 8, y - h / 2, w + 1, h + 1);
491 panel_configure(void *data,
492 struct desktop_shell *desktop_shell,
493 uint32_t edges, struct window *window,
494 int32_t width, int32_t height)
496 struct surface *surface = window_get_user_data(window);
497 struct panel *panel = container_of(surface, struct panel, base);
499 window_schedule_resize(panel->window, width, 32);
503 panel_destroy_launcher(struct panel_launcher *launcher)
505 wl_array_release(&launcher->argv);
506 wl_array_release(&launcher->envp);
508 free(launcher->path);
510 cairo_surface_destroy(launcher->icon);
512 widget_destroy(launcher->widget);
513 wl_list_remove(&launcher->link);
519 panel_destroy(struct panel *panel)
521 struct panel_launcher *tmp;
522 struct panel_launcher *launcher;
524 panel_destroy_clock(panel->clock);
526 wl_list_for_each_safe(launcher, tmp, &panel->launcher_list, link)
527 panel_destroy_launcher(launcher);
529 widget_destroy(panel->widget);
530 window_destroy(panel->window);
535 static struct panel *
536 panel_create(struct desktop *desktop)
539 struct weston_config_section *s;
541 panel = xzalloc(sizeof *panel);
543 panel->base.configure = panel_configure;
544 panel->window = window_create_custom(desktop->display);
545 panel->widget = window_add_widget(panel->window, panel);
546 wl_list_init(&panel->launcher_list);
548 window_set_title(panel->window, "panel");
549 window_set_user_data(panel->window, panel);
551 widget_set_redraw_handler(panel->widget, panel_redraw_handler);
552 widget_set_resize_handler(panel->widget, panel_resize_handler);
553 widget_set_button_handler(panel->widget, panel_button_handler);
555 panel_add_clock(panel);
557 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
558 weston_config_section_get_uint(s, "panel-color",
559 &panel->color, 0xaa000000);
561 panel_add_launchers(panel, desktop);
566 static cairo_surface_t *
567 load_icon_or_fallback(const char *icon)
569 cairo_surface_t *surface = cairo_image_surface_create_from_png(icon);
570 cairo_status_t status;
573 status = cairo_surface_status(surface);
574 if (status == CAIRO_STATUS_SUCCESS)
577 cairo_surface_destroy(surface);
578 fprintf(stderr, "ERROR loading icon from file '%s', error: '%s'\n",
579 icon, cairo_status_to_string(status));
581 /* draw fallback icon */
582 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
584 cr = cairo_create(surface);
586 cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1);
589 cairo_set_source_rgba(cr, 0, 0, 0, 1);
590 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
591 cairo_rectangle(cr, 0, 0, 20, 20);
592 cairo_move_to(cr, 4, 4);
593 cairo_line_to(cr, 16, 16);
594 cairo_move_to(cr, 4, 16);
595 cairo_line_to(cr, 16, 4);
604 panel_add_launcher(struct panel *panel, const char *icon, const char *path)
606 struct panel_launcher *launcher;
607 char *start, *p, *eq, **ps;
610 launcher = xzalloc(sizeof *launcher);
611 launcher->icon = load_icon_or_fallback(icon);
612 launcher->path = strdup(path);
614 wl_array_init(&launcher->envp);
615 wl_array_init(&launcher->argv);
616 for (i = 0; environ[i]; i++) {
617 ps = wl_array_add(&launcher->envp, sizeof *ps);
622 start = launcher->path;
624 for (p = start, eq = NULL; *p && !isspace(*p); p++)
629 ps = launcher->envp.data;
630 for (k = 0; k < i; k++)
631 if (strncmp(ps[k], start, eq - start) == 0) {
636 ps = wl_array_add(&launcher->envp, sizeof *ps);
641 ps = wl_array_add(&launcher->argv, sizeof *ps);
646 while (*p && isspace(*p))
652 ps = wl_array_add(&launcher->envp, sizeof *ps);
654 ps = wl_array_add(&launcher->argv, sizeof *ps);
657 launcher->panel = panel;
658 wl_list_insert(panel->launcher_list.prev, &launcher->link);
660 launcher->widget = widget_add_widget(panel->widget, launcher);
661 widget_set_enter_handler(launcher->widget,
662 panel_launcher_enter_handler);
663 widget_set_leave_handler(launcher->widget,
664 panel_launcher_leave_handler);
665 widget_set_button_handler(launcher->widget,
666 panel_launcher_button_handler);
667 widget_set_touch_down_handler(launcher->widget,
668 panel_launcher_touch_down_handler);
669 widget_set_touch_up_handler(launcher->widget,
670 panel_launcher_touch_up_handler);
671 widget_set_redraw_handler(launcher->widget,
672 panel_launcher_redraw_handler);
673 widget_set_motion_handler(launcher->widget,
674 panel_launcher_motion_handler);
679 BACKGROUND_SCALE_CROP,
684 background_draw(struct widget *widget, void *data)
686 struct background *background = data;
687 cairo_surface_t *surface, *image;
688 cairo_pattern_t *pattern;
689 cairo_matrix_t matrix;
694 struct rectangle allocation;
695 struct display *display;
696 struct wl_region *opaque;
698 surface = window_get_surface(background->window);
700 cr = widget_cairo_create(background->widget);
701 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
702 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
705 widget_get_allocation(widget, &allocation);
707 if (background->image)
708 image = load_cairo_surface(background->image);
710 if (image && background->type != -1) {
711 im_w = cairo_image_surface_get_width(image);
712 im_h = cairo_image_surface_get_height(image);
713 sx = im_w / allocation.width;
714 sy = im_h / allocation.height;
716 pattern = cairo_pattern_create_for_surface(image);
718 switch (background->type) {
719 case BACKGROUND_SCALE:
720 cairo_matrix_init_scale(&matrix, sx, sy);
721 cairo_pattern_set_matrix(pattern, &matrix);
723 case BACKGROUND_SCALE_CROP:
724 s = (sx < sy) ? sx : sy;
726 tx = (im_w - s * allocation.width) * 0.5;
727 ty = (im_h - s * allocation.height) * 0.5;
728 cairo_matrix_init_translate(&matrix, tx, ty);
729 cairo_matrix_scale(&matrix, s, s);
730 cairo_pattern_set_matrix(pattern, &matrix);
732 case BACKGROUND_TILE:
733 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
737 cairo_set_source(cr, pattern);
738 cairo_pattern_destroy (pattern);
739 cairo_surface_destroy(image);
741 set_hex_color(cr, background->color);
746 cairo_surface_destroy(surface);
748 display = window_get_display(background->window);
749 opaque = wl_compositor_create_region(display_get_compositor(display));
750 wl_region_add(opaque, allocation.x, allocation.y,
751 allocation.width, allocation.height);
752 wl_surface_set_opaque_region(window_get_wl_surface(background->window), opaque);
753 wl_region_destroy(opaque);
755 background->painted = 1;
756 check_desktop_ready(background->window);
760 background_configure(void *data,
761 struct desktop_shell *desktop_shell,
762 uint32_t edges, struct window *window,
763 int32_t width, int32_t height)
765 struct background *background =
766 (struct background *) window_get_user_data(window);
768 widget_schedule_resize(background->widget, width, height);
772 unlock_dialog_redraw_handler(struct widget *widget, void *data)
774 struct unlock_dialog *dialog = data;
775 struct rectangle allocation;
776 cairo_surface_t *surface;
778 cairo_pattern_t *pat;
781 cr = widget_cairo_create(widget);
783 widget_get_allocation(dialog->widget, &allocation);
784 cairo_rectangle(cr, allocation.x, allocation.y,
785 allocation.width, allocation.height);
786 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
787 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
790 cairo_translate(cr, allocation.x, allocation.y);
791 if (dialog->button_focused)
796 cx = allocation.width / 2.0;
797 cy = allocation.height / 2.0;
798 r = (cx < cy ? cx : cy) * 0.4;
799 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
800 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
801 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
802 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
803 cairo_set_source(cr, pat);
804 cairo_pattern_destroy(pat);
805 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
808 widget_set_allocation(dialog->button,
809 allocation.x + cx - r,
810 allocation.y + cy - r, 2 * r, 2 * r);
814 surface = window_get_surface(dialog->window);
815 cairo_surface_destroy(surface);
819 unlock_dialog_button_handler(struct widget *widget,
820 struct input *input, uint32_t time,
822 enum wl_pointer_button_state state, void *data)
824 struct unlock_dialog *dialog = data;
825 struct desktop *desktop = dialog->desktop;
827 if (button == BTN_LEFT) {
828 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
830 display_defer(desktop->display, &desktop->unlock_task);
837 unlock_dialog_touch_down_handler(struct widget *widget, struct input *input,
838 uint32_t serial, uint32_t time, int32_t id,
839 float x, float y, void *data)
841 struct unlock_dialog *dialog = data;
843 dialog->button_focused = 1;
844 widget_schedule_redraw(widget);
848 unlock_dialog_touch_up_handler(struct widget *widget, struct input *input,
849 uint32_t serial, uint32_t time, int32_t id,
852 struct unlock_dialog *dialog = data;
853 struct desktop *desktop = dialog->desktop;
855 dialog->button_focused = 0;
856 widget_schedule_redraw(widget);
857 display_defer(desktop->display, &desktop->unlock_task);
862 unlock_dialog_keyboard_focus_handler(struct window *window,
863 struct input *device, void *data)
865 window_schedule_redraw(window);
869 unlock_dialog_widget_enter_handler(struct widget *widget,
871 float x, float y, void *data)
873 struct unlock_dialog *dialog = data;
875 dialog->button_focused = 1;
876 widget_schedule_redraw(widget);
878 return CURSOR_LEFT_PTR;
882 unlock_dialog_widget_leave_handler(struct widget *widget,
883 struct input *input, void *data)
885 struct unlock_dialog *dialog = data;
887 dialog->button_focused = 0;
888 widget_schedule_redraw(widget);
891 static struct unlock_dialog *
892 unlock_dialog_create(struct desktop *desktop)
894 struct display *display = desktop->display;
895 struct unlock_dialog *dialog;
897 dialog = xzalloc(sizeof *dialog);
899 dialog->window = window_create_custom(display);
900 dialog->widget = window_frame_create(dialog->window, dialog);
901 window_set_title(dialog->window, "Unlock your desktop");
903 window_set_user_data(dialog->window, dialog);
904 window_set_keyboard_focus_handler(dialog->window,
905 unlock_dialog_keyboard_focus_handler);
906 dialog->button = widget_add_widget(dialog->widget, dialog);
907 widget_set_redraw_handler(dialog->widget,
908 unlock_dialog_redraw_handler);
909 widget_set_enter_handler(dialog->button,
910 unlock_dialog_widget_enter_handler);
911 widget_set_leave_handler(dialog->button,
912 unlock_dialog_widget_leave_handler);
913 widget_set_button_handler(dialog->button,
914 unlock_dialog_button_handler);
915 widget_set_touch_down_handler(dialog->button,
916 unlock_dialog_touch_down_handler);
917 widget_set_touch_up_handler(dialog->button,
918 unlock_dialog_touch_up_handler);
920 desktop_shell_set_lock_surface(desktop->shell,
921 window_get_wl_surface(dialog->window));
923 window_schedule_resize(dialog->window, 260, 230);
929 unlock_dialog_destroy(struct unlock_dialog *dialog)
931 window_destroy(dialog->window);
936 unlock_dialog_finish(struct task *task, uint32_t events)
938 struct desktop *desktop =
939 container_of(task, struct desktop, unlock_task);
941 desktop_shell_unlock(desktop->shell);
942 unlock_dialog_destroy(desktop->unlock_dialog);
943 desktop->unlock_dialog = NULL;
947 desktop_shell_configure(void *data,
948 struct desktop_shell *desktop_shell,
950 struct wl_surface *surface,
951 int32_t width, int32_t height)
953 struct window *window = wl_surface_get_user_data(surface);
954 struct surface *s = window_get_user_data(window);
956 s->configure(data, desktop_shell, edges, window, width, height);
960 desktop_shell_prepare_lock_surface(void *data,
961 struct desktop_shell *desktop_shell)
963 struct desktop *desktop = data;
965 if (!desktop->locking) {
966 desktop_shell_unlock(desktop->shell);
970 if (!desktop->unlock_dialog) {
971 desktop->unlock_dialog = unlock_dialog_create(desktop);
972 desktop->unlock_dialog->desktop = desktop;
977 desktop_shell_grab_cursor(void *data,
978 struct desktop_shell *desktop_shell,
981 struct desktop *desktop = data;
984 case DESKTOP_SHELL_CURSOR_NONE:
985 desktop->grab_cursor = CURSOR_BLANK;
987 case DESKTOP_SHELL_CURSOR_BUSY:
988 desktop->grab_cursor = CURSOR_WATCH;
990 case DESKTOP_SHELL_CURSOR_MOVE:
991 desktop->grab_cursor = CURSOR_DRAGGING;
993 case DESKTOP_SHELL_CURSOR_RESIZE_TOP:
994 desktop->grab_cursor = CURSOR_TOP;
996 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM:
997 desktop->grab_cursor = CURSOR_BOTTOM;
999 case DESKTOP_SHELL_CURSOR_RESIZE_LEFT:
1000 desktop->grab_cursor = CURSOR_LEFT;
1002 case DESKTOP_SHELL_CURSOR_RESIZE_RIGHT:
1003 desktop->grab_cursor = CURSOR_RIGHT;
1005 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_LEFT:
1006 desktop->grab_cursor = CURSOR_TOP_LEFT;
1008 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_RIGHT:
1009 desktop->grab_cursor = CURSOR_TOP_RIGHT;
1011 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_LEFT:
1012 desktop->grab_cursor = CURSOR_BOTTOM_LEFT;
1014 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_RIGHT:
1015 desktop->grab_cursor = CURSOR_BOTTOM_RIGHT;
1017 case DESKTOP_SHELL_CURSOR_ARROW:
1019 desktop->grab_cursor = CURSOR_LEFT_PTR;
1023 static const struct desktop_shell_listener listener = {
1024 desktop_shell_configure,
1025 desktop_shell_prepare_lock_surface,
1026 desktop_shell_grab_cursor
1030 background_destroy(struct background *background)
1032 widget_destroy(background->widget);
1033 window_destroy(background->window);
1035 free(background->image);
1039 static struct background *
1040 background_create(struct desktop *desktop)
1042 struct background *background;
1043 struct weston_config_section *s;
1046 background = xzalloc(sizeof *background);
1047 background->base.configure = background_configure;
1048 background->window = window_create_custom(desktop->display);
1049 background->widget = window_add_widget(background->window, background);
1050 window_set_user_data(background->window, background);
1051 widget_set_redraw_handler(background->widget, background_draw);
1052 window_set_preferred_format(background->window,
1053 WINDOW_PREFERRED_FORMAT_RGB565);
1055 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
1056 weston_config_section_get_string(s, "background-image",
1058 DATADIR "/weston/pattern.png");
1059 weston_config_section_get_uint(s, "background-color",
1060 &background->color, 0xff002244);
1062 weston_config_section_get_string(s, "background-type",
1064 if (strcmp(type, "scale") == 0) {
1065 background->type = BACKGROUND_SCALE;
1066 } else if (strcmp(type, "scale-crop") == 0) {
1067 background->type = BACKGROUND_SCALE_CROP;
1068 } else if (strcmp(type, "tile") == 0) {
1069 background->type = BACKGROUND_TILE;
1071 background->type = -1;
1072 fprintf(stderr, "invalid background-type: %s\n",
1082 grab_surface_enter_handler(struct widget *widget, struct input *input,
1083 float x, float y, void *data)
1085 struct desktop *desktop = data;
1087 return desktop->grab_cursor;
1091 grab_surface_destroy(struct desktop *desktop)
1093 widget_destroy(desktop->grab_widget);
1094 window_destroy(desktop->grab_window);
1098 grab_surface_create(struct desktop *desktop)
1100 struct wl_surface *s;
1102 desktop->grab_window = window_create_custom(desktop->display);
1103 window_set_user_data(desktop->grab_window, desktop);
1105 s = window_get_wl_surface(desktop->grab_window);
1106 desktop_shell_set_grab_surface(desktop->shell, s);
1108 desktop->grab_widget =
1109 window_add_widget(desktop->grab_window, desktop);
1110 /* We set the allocation to 1x1 at 0,0 so the fake enter event
1111 * at 0,0 will go to this widget. */
1112 widget_set_allocation(desktop->grab_widget, 0, 0, 1, 1);
1114 widget_set_enter_handler(desktop->grab_widget,
1115 grab_surface_enter_handler);
1119 output_destroy(struct output *output)
1121 background_destroy(output->background);
1122 panel_destroy(output->panel);
1123 wl_output_destroy(output->output);
1124 wl_list_remove(&output->link);
1130 desktop_destroy_outputs(struct desktop *desktop)
1133 struct output *output;
1135 wl_list_for_each_safe(output, tmp, &desktop->outputs, link)
1136 output_destroy(output);
1140 output_handle_geometry(void *data,
1141 struct wl_output *wl_output,
1144 int physical_height,
1150 struct output *output = data;
1152 window_set_buffer_transform(output->panel->window, transform);
1153 window_set_buffer_transform(output->background->window, transform);
1157 output_handle_mode(void *data,
1158 struct wl_output *wl_output,
1167 output_handle_done(void *data,
1168 struct wl_output *wl_output)
1173 output_handle_scale(void *data,
1174 struct wl_output *wl_output,
1177 struct output *output = data;
1179 window_set_buffer_scale(output->panel->window, scale);
1180 window_set_buffer_scale(output->background->window, scale);
1183 static const struct wl_output_listener output_listener = {
1184 output_handle_geometry,
1191 output_init(struct output *output, struct desktop *desktop)
1193 struct wl_surface *surface;
1195 output->panel = panel_create(desktop);
1196 surface = window_get_wl_surface(output->panel->window);
1197 desktop_shell_set_panel(desktop->shell,
1198 output->output, surface);
1200 output->background = background_create(desktop);
1201 surface = window_get_wl_surface(output->background->window);
1202 desktop_shell_set_background(desktop->shell,
1203 output->output, surface);
1207 create_output(struct desktop *desktop, uint32_t id)
1209 struct output *output;
1211 output = calloc(1, sizeof *output);
1216 display_bind(desktop->display, id, &wl_output_interface, 2);
1217 output->server_output_id = id;
1219 wl_output_add_listener(output->output, &output_listener, output);
1221 wl_list_insert(&desktop->outputs, &output->link);
1223 /* On start up we may process an output global before the shell global
1224 * in which case we can't create the panel and background just yet */
1226 output_init(output, desktop);
1230 global_handler(struct display *display, uint32_t id,
1231 const char *interface, uint32_t version, void *data)
1233 struct desktop *desktop = data;
1235 if (!strcmp(interface, "desktop_shell")) {
1236 desktop->interface_version = (version < 2) ? version : 2;
1237 desktop->shell = display_bind(desktop->display,
1238 id, &desktop_shell_interface,
1239 desktop->interface_version);
1240 desktop_shell_add_listener(desktop->shell, &listener, desktop);
1241 } else if (!strcmp(interface, "wl_output")) {
1242 create_output(desktop, id);
1247 global_handler_remove(struct display *display, uint32_t id,
1248 const char *interface, uint32_t version, void *data)
1250 struct desktop *desktop = data;
1251 struct output *output;
1253 if (!strcmp(interface, "wl_output")) {
1254 wl_list_for_each(output, &desktop->outputs, link) {
1255 if (output->server_output_id == id) {
1256 output_destroy(output);
1264 panel_add_launchers(struct panel *panel, struct desktop *desktop)
1266 struct weston_config_section *s;
1273 while (weston_config_next_section(desktop->config, &s, &name)) {
1274 if (strcmp(name, "launcher") != 0)
1277 weston_config_section_get_string(s, "icon", &icon, NULL);
1278 weston_config_section_get_string(s, "path", &path, NULL);
1280 if (icon != NULL && path != NULL) {
1281 panel_add_launcher(panel, icon, path);
1284 fprintf(stderr, "invalid launcher section\n");
1292 /* add default launcher */
1293 panel_add_launcher(panel,
1294 DATADIR "/weston/terminal.png",
1295 BINDIR "/weston-terminal");
1299 int main(int argc, char *argv[])
1301 struct desktop desktop = { 0 };
1302 struct output *output;
1303 struct weston_config_section *s;
1305 desktop.unlock_task.run = unlock_dialog_finish;
1306 wl_list_init(&desktop.outputs);
1308 desktop.config = weston_config_parse("weston.ini");
1309 s = weston_config_get_section(desktop.config, "shell", NULL, NULL);
1310 weston_config_section_get_bool(s, "locking", &desktop.locking, 1);
1312 desktop.display = display_create(&argc, argv);
1313 if (desktop.display == NULL) {
1314 fprintf(stderr, "failed to create display: %m\n");
1318 display_set_user_data(desktop.display, &desktop);
1319 display_set_global_handler(desktop.display, global_handler);
1320 display_set_global_handler_remove(desktop.display, global_handler_remove);
1322 /* Create panel and background for outputs processed before the shell
1323 * global interface was processed */
1324 wl_list_for_each(output, &desktop.outputs, link)
1326 output_init(output, &desktop);
1328 grab_surface_create(&desktop);
1330 signal(SIGCHLD, sigchild_handler);
1332 display_run(desktop.display);
1335 grab_surface_destroy(&desktop);
1336 desktop_destroy_outputs(&desktop);
1337 if (desktop.unlock_dialog)
1338 unlock_dialog_destroy(desktop.unlock_dialog);
1339 desktop_shell_destroy(desktop.shell);
1340 display_destroy(desktop.display);