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;
101 struct background *background;
104 struct panel_launcher {
105 struct widget *widget;
107 cairo_surface_t *icon;
108 int focused, pressed;
111 struct wl_array envp;
112 struct wl_array argv;
116 struct widget *widget;
118 struct task clock_task;
122 struct unlock_dialog {
123 struct window *window;
124 struct widget *widget;
125 struct widget *button;
128 struct desktop *desktop;
132 panel_add_launchers(struct panel *panel, struct desktop *desktop);
135 sigchild_handler(int s)
140 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
141 fprintf(stderr, "child %d exited\n", pid);
145 menu_func(struct window *window, struct input *input, int index, void *data)
147 printf("Selected index %d from a panel menu.\n", index);
151 show_menu(struct panel *panel, struct input *input, uint32_t time)
154 static const char *entries[] = {
155 "Roy", "Pris", "Leon", "Zhora"
158 input_get_position(input, &x, &y);
159 window_show_menu(window_get_display(panel->window),
160 input, time, panel->window,
161 x - 10, y - 10, menu_func, entries, 4);
165 is_desktop_painted(struct desktop *desktop)
167 struct output *output;
169 wl_list_for_each(output, &desktop->outputs, link) {
170 if (output->panel && !output->panel->painted)
172 if (output->background && !output->background->painted)
180 check_desktop_ready(struct window *window)
182 struct display *display;
183 struct desktop *desktop;
185 display = window_get_display(window);
186 desktop = display_get_user_data(display);
188 if (!desktop->painted && is_desktop_painted(desktop)) {
189 desktop->painted = 1;
191 if (desktop->interface_version >= 2)
192 desktop_shell_desktop_ready(desktop->shell);
197 panel_launcher_activate(struct panel_launcher *widget)
204 fprintf(stderr, "fork failed: %m\n");
211 argv = widget->argv.data;
212 if (execve(argv[0], argv, widget->envp.data) < 0) {
213 fprintf(stderr, "execl '%s' failed: %m\n", argv[0]);
219 panel_launcher_redraw_handler(struct widget *widget, void *data)
221 struct panel_launcher *launcher = data;
222 struct rectangle allocation;
225 cr = widget_cairo_create(launcher->panel->widget);
227 widget_get_allocation(widget, &allocation);
228 if (launcher->pressed) {
233 cairo_set_source_surface(cr, launcher->icon,
234 allocation.x, allocation.y);
237 if (launcher->focused) {
238 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
239 cairo_mask_surface(cr, launcher->icon,
240 allocation.x, allocation.y);
247 panel_launcher_motion_handler(struct widget *widget, struct input *input,
248 uint32_t time, float x, float y, void *data)
250 struct panel_launcher *launcher = data;
252 widget_set_tooltip(widget, basename((char *)launcher->path), x, y);
254 return CURSOR_LEFT_PTR;
258 set_hex_color(cairo_t *cr, uint32_t color)
260 cairo_set_source_rgba(cr,
261 ((color >> 16) & 0xff) / 255.0,
262 ((color >> 8) & 0xff) / 255.0,
263 ((color >> 0) & 0xff) / 255.0,
264 ((color >> 24) & 0xff) / 255.0);
268 panel_redraw_handler(struct widget *widget, void *data)
270 cairo_surface_t *surface;
272 struct panel *panel = data;
274 cr = widget_cairo_create(panel->widget);
275 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
276 set_hex_color(cr, panel->color);
280 surface = window_get_surface(panel->window);
281 cairo_surface_destroy(surface);
283 check_desktop_ready(panel->window);
287 panel_launcher_enter_handler(struct widget *widget, struct input *input,
288 float x, float y, void *data)
290 struct panel_launcher *launcher = data;
292 launcher->focused = 1;
293 widget_schedule_redraw(widget);
295 return CURSOR_LEFT_PTR;
299 panel_launcher_leave_handler(struct widget *widget,
300 struct input *input, void *data)
302 struct panel_launcher *launcher = data;
304 launcher->focused = 0;
305 widget_destroy_tooltip(widget);
306 widget_schedule_redraw(widget);
310 panel_launcher_button_handler(struct widget *widget,
311 struct input *input, uint32_t time,
313 enum wl_pointer_button_state state, void *data)
315 struct panel_launcher *launcher;
317 launcher = widget_get_user_data(widget);
318 widget_schedule_redraw(widget);
319 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
320 panel_launcher_activate(launcher);
325 panel_launcher_touch_down_handler(struct widget *widget, struct input *input,
326 uint32_t serial, uint32_t time, int32_t id,
327 float x, float y, void *data)
329 struct panel_launcher *launcher;
331 launcher = widget_get_user_data(widget);
332 launcher->focused = 1;
333 widget_schedule_redraw(widget);
337 panel_launcher_touch_up_handler(struct widget *widget, struct input *input,
338 uint32_t serial, uint32_t time, int32_t id,
341 struct panel_launcher *launcher;
343 launcher = widget_get_user_data(widget);
344 launcher->focused = 0;
345 widget_schedule_redraw(widget);
346 panel_launcher_activate(launcher);
350 clock_func(struct task *task, uint32_t events)
352 struct panel_clock *clock =
353 container_of(task, struct panel_clock, clock_task);
356 if (read(clock->clock_fd, &exp, sizeof exp) != sizeof exp)
358 widget_schedule_redraw(clock->widget);
362 panel_clock_redraw_handler(struct widget *widget, void *data)
364 struct panel_clock *clock = data;
366 struct rectangle allocation;
367 cairo_text_extents_t extents;
368 cairo_font_extents_t font_extents;
370 struct tm * timeinfo;
374 timeinfo = localtime(&rawtime);
375 strftime(string, sizeof string, "%a %b %d, %I:%M %p", timeinfo);
377 widget_get_allocation(widget, &allocation);
378 if (allocation.width == 0)
381 cr = widget_cairo_create(clock->panel->widget);
382 cairo_select_font_face(cr, "sans",
383 CAIRO_FONT_SLANT_NORMAL,
384 CAIRO_FONT_WEIGHT_NORMAL);
385 cairo_set_font_size(cr, 14);
386 cairo_text_extents(cr, string, &extents);
387 cairo_font_extents (cr, &font_extents);
388 cairo_move_to(cr, allocation.x + 5,
389 allocation.y + 3 * (allocation.height >> 2) + 1);
390 cairo_set_source_rgb(cr, 0, 0, 0);
391 cairo_show_text(cr, string);
392 cairo_move_to(cr, allocation.x + 4,
393 allocation.y + 3 * (allocation.height >> 2));
394 cairo_set_source_rgb(cr, 1, 1, 1);
395 cairo_show_text(cr, string);
400 clock_timer_reset(struct panel_clock *clock)
402 struct itimerspec its;
404 its.it_interval.tv_sec = 60;
405 its.it_interval.tv_nsec = 0;
406 its.it_value.tv_sec = 60;
407 its.it_value.tv_nsec = 0;
408 if (timerfd_settime(clock->clock_fd, 0, &its, NULL) < 0) {
409 fprintf(stderr, "could not set timerfd\n: %m");
417 panel_destroy_clock(struct panel_clock *clock)
419 widget_destroy(clock->widget);
421 close(clock->clock_fd);
427 panel_add_clock(struct panel *panel)
429 struct panel_clock *clock;
432 timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
434 fprintf(stderr, "could not create timerfd\n: %m");
438 clock = xzalloc(sizeof *clock);
439 clock->panel = panel;
440 panel->clock = clock;
441 clock->clock_fd = timerfd;
443 clock->clock_task.run = clock_func;
444 display_watch_fd(window_get_display(panel->window), clock->clock_fd,
445 EPOLLIN, &clock->clock_task);
446 clock_timer_reset(clock);
448 clock->widget = widget_add_widget(panel->widget, clock);
449 widget_set_redraw_handler(clock->widget, panel_clock_redraw_handler);
453 panel_button_handler(struct widget *widget,
454 struct input *input, uint32_t time,
456 enum wl_pointer_button_state state, void *data)
458 struct panel *panel = data;
460 if (button == BTN_RIGHT && state == WL_POINTER_BUTTON_STATE_PRESSED)
461 show_menu(panel, input, time);
465 panel_resize_handler(struct widget *widget,
466 int32_t width, int32_t height, void *data)
468 struct panel_launcher *launcher;
469 struct panel *panel = data;
474 wl_list_for_each(launcher, &panel->launcher_list, link) {
475 w = cairo_image_surface_get_width(launcher->icon);
476 h = cairo_image_surface_get_height(launcher->icon);
477 widget_set_allocation(launcher->widget,
478 x, y - h / 2, w + 1, h + 1);
485 widget_set_allocation(panel->clock->widget,
486 width - w - 8, y - h / 2, w + 1, h + 1);
490 panel_configure(void *data,
491 struct desktop_shell *desktop_shell,
492 uint32_t edges, struct window *window,
493 int32_t width, int32_t height)
495 struct surface *surface = window_get_user_data(window);
496 struct panel *panel = container_of(surface, struct panel, base);
498 window_schedule_resize(panel->window, width, 32);
502 panel_destroy_launcher(struct panel_launcher *launcher)
504 wl_array_release(&launcher->argv);
505 wl_array_release(&launcher->envp);
507 free(launcher->path);
509 cairo_surface_destroy(launcher->icon);
511 widget_destroy(launcher->widget);
512 wl_list_remove(&launcher->link);
518 panel_destroy(struct panel *panel)
520 struct panel_launcher *tmp;
521 struct panel_launcher *launcher;
523 panel_destroy_clock(panel->clock);
525 wl_list_for_each_safe(launcher, tmp, &panel->launcher_list, link)
526 panel_destroy_launcher(launcher);
528 widget_destroy(panel->widget);
529 window_destroy(panel->window);
534 static struct panel *
535 panel_create(struct desktop *desktop)
538 struct weston_config_section *s;
540 panel = xzalloc(sizeof *panel);
542 panel->base.configure = panel_configure;
543 panel->window = window_create_custom(desktop->display);
544 panel->widget = window_add_widget(panel->window, panel);
545 wl_list_init(&panel->launcher_list);
547 window_set_title(panel->window, "panel");
548 window_set_user_data(panel->window, panel);
550 widget_set_redraw_handler(panel->widget, panel_redraw_handler);
551 widget_set_resize_handler(panel->widget, panel_resize_handler);
552 widget_set_button_handler(panel->widget, panel_button_handler);
554 panel_add_clock(panel);
556 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
557 weston_config_section_get_uint(s, "panel-color",
558 &panel->color, 0xaa000000);
560 panel_add_launchers(panel, desktop);
565 static cairo_surface_t *
566 load_icon_or_fallback(const char *icon)
568 cairo_surface_t *surface = cairo_image_surface_create_from_png(icon);
569 cairo_status_t status;
572 status = cairo_surface_status(surface);
573 if (status == CAIRO_STATUS_SUCCESS)
576 cairo_surface_destroy(surface);
577 fprintf(stderr, "ERROR loading icon from file '%s', error: '%s'\n",
578 icon, cairo_status_to_string(status));
580 /* draw fallback icon */
581 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
583 cr = cairo_create(surface);
585 cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1);
588 cairo_set_source_rgba(cr, 0, 0, 0, 1);
589 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
590 cairo_rectangle(cr, 0, 0, 20, 20);
591 cairo_move_to(cr, 4, 4);
592 cairo_line_to(cr, 16, 16);
593 cairo_move_to(cr, 4, 16);
594 cairo_line_to(cr, 16, 4);
603 panel_add_launcher(struct panel *panel, const char *icon, const char *path)
605 struct panel_launcher *launcher;
606 char *start, *p, *eq, **ps;
609 launcher = xzalloc(sizeof *launcher);
610 launcher->icon = load_icon_or_fallback(icon);
611 launcher->path = strdup(path);
613 wl_array_init(&launcher->envp);
614 wl_array_init(&launcher->argv);
615 for (i = 0; environ[i]; i++) {
616 ps = wl_array_add(&launcher->envp, sizeof *ps);
621 start = launcher->path;
623 for (p = start, eq = NULL; *p && !isspace(*p); p++)
628 ps = launcher->envp.data;
629 for (k = 0; k < i; k++)
630 if (strncmp(ps[k], start, eq - start) == 0) {
635 ps = wl_array_add(&launcher->envp, sizeof *ps);
640 ps = wl_array_add(&launcher->argv, sizeof *ps);
645 while (*p && isspace(*p))
651 ps = wl_array_add(&launcher->envp, sizeof *ps);
653 ps = wl_array_add(&launcher->argv, sizeof *ps);
656 launcher->panel = panel;
657 wl_list_insert(panel->launcher_list.prev, &launcher->link);
659 launcher->widget = widget_add_widget(panel->widget, launcher);
660 widget_set_enter_handler(launcher->widget,
661 panel_launcher_enter_handler);
662 widget_set_leave_handler(launcher->widget,
663 panel_launcher_leave_handler);
664 widget_set_button_handler(launcher->widget,
665 panel_launcher_button_handler);
666 widget_set_touch_down_handler(launcher->widget,
667 panel_launcher_touch_down_handler);
668 widget_set_touch_up_handler(launcher->widget,
669 panel_launcher_touch_up_handler);
670 widget_set_redraw_handler(launcher->widget,
671 panel_launcher_redraw_handler);
672 widget_set_motion_handler(launcher->widget,
673 panel_launcher_motion_handler);
678 BACKGROUND_SCALE_CROP,
683 background_draw(struct widget *widget, void *data)
685 struct background *background = data;
686 cairo_surface_t *surface, *image;
687 cairo_pattern_t *pattern;
688 cairo_matrix_t matrix;
693 struct rectangle allocation;
694 struct display *display;
695 struct wl_region *opaque;
697 surface = window_get_surface(background->window);
699 cr = widget_cairo_create(background->widget);
700 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
701 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
704 widget_get_allocation(widget, &allocation);
706 if (background->image)
707 image = load_cairo_surface(background->image);
709 if (image && background->type != -1) {
710 im_w = cairo_image_surface_get_width(image);
711 im_h = cairo_image_surface_get_height(image);
712 sx = im_w / allocation.width;
713 sy = im_h / allocation.height;
715 pattern = cairo_pattern_create_for_surface(image);
717 switch (background->type) {
718 case BACKGROUND_SCALE:
719 cairo_matrix_init_scale(&matrix, sx, sy);
720 cairo_pattern_set_matrix(pattern, &matrix);
722 case BACKGROUND_SCALE_CROP:
723 s = (sx < sy) ? sx : sy;
725 tx = (im_w - s * allocation.width) * 0.5;
726 ty = (im_h - s * allocation.height) * 0.5;
727 cairo_matrix_init_translate(&matrix, tx, ty);
728 cairo_matrix_scale(&matrix, s, s);
729 cairo_pattern_set_matrix(pattern, &matrix);
731 case BACKGROUND_TILE:
732 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
736 cairo_set_source(cr, pattern);
737 cairo_pattern_destroy (pattern);
738 cairo_surface_destroy(image);
740 set_hex_color(cr, background->color);
745 cairo_surface_destroy(surface);
747 display = window_get_display(background->window);
748 opaque = wl_compositor_create_region(display_get_compositor(display));
749 wl_region_add(opaque, allocation.x, allocation.y,
750 allocation.width, allocation.height);
751 wl_surface_set_opaque_region(window_get_wl_surface(background->window), opaque);
752 wl_region_destroy(opaque);
754 background->painted = 1;
755 check_desktop_ready(background->window);
759 background_configure(void *data,
760 struct desktop_shell *desktop_shell,
761 uint32_t edges, struct window *window,
762 int32_t width, int32_t height)
764 struct background *background =
765 (struct background *) window_get_user_data(window);
767 widget_schedule_resize(background->widget, width, height);
771 unlock_dialog_redraw_handler(struct widget *widget, void *data)
773 struct unlock_dialog *dialog = data;
774 struct rectangle allocation;
775 cairo_surface_t *surface;
777 cairo_pattern_t *pat;
780 cr = widget_cairo_create(widget);
782 widget_get_allocation(dialog->widget, &allocation);
783 cairo_rectangle(cr, allocation.x, allocation.y,
784 allocation.width, allocation.height);
785 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
786 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
789 cairo_translate(cr, allocation.x, allocation.y);
790 if (dialog->button_focused)
795 cx = allocation.width / 2.0;
796 cy = allocation.height / 2.0;
797 r = (cx < cy ? cx : cy) * 0.4;
798 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
799 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
800 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
801 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
802 cairo_set_source(cr, pat);
803 cairo_pattern_destroy(pat);
804 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
807 widget_set_allocation(dialog->button,
808 allocation.x + cx - r,
809 allocation.y + cy - r, 2 * r, 2 * r);
813 surface = window_get_surface(dialog->window);
814 cairo_surface_destroy(surface);
818 unlock_dialog_button_handler(struct widget *widget,
819 struct input *input, uint32_t time,
821 enum wl_pointer_button_state state, void *data)
823 struct unlock_dialog *dialog = data;
824 struct desktop *desktop = dialog->desktop;
826 if (button == BTN_LEFT) {
827 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
829 display_defer(desktop->display, &desktop->unlock_task);
836 unlock_dialog_touch_down_handler(struct widget *widget, struct input *input,
837 uint32_t serial, uint32_t time, int32_t id,
838 float x, float y, void *data)
840 struct unlock_dialog *dialog = data;
842 dialog->button_focused = 1;
843 widget_schedule_redraw(widget);
847 unlock_dialog_touch_up_handler(struct widget *widget, struct input *input,
848 uint32_t serial, uint32_t time, int32_t id,
851 struct unlock_dialog *dialog = data;
852 struct desktop *desktop = dialog->desktop;
854 dialog->button_focused = 0;
855 widget_schedule_redraw(widget);
856 display_defer(desktop->display, &desktop->unlock_task);
861 unlock_dialog_keyboard_focus_handler(struct window *window,
862 struct input *device, void *data)
864 window_schedule_redraw(window);
868 unlock_dialog_widget_enter_handler(struct widget *widget,
870 float x, float y, void *data)
872 struct unlock_dialog *dialog = data;
874 dialog->button_focused = 1;
875 widget_schedule_redraw(widget);
877 return CURSOR_LEFT_PTR;
881 unlock_dialog_widget_leave_handler(struct widget *widget,
882 struct input *input, void *data)
884 struct unlock_dialog *dialog = data;
886 dialog->button_focused = 0;
887 widget_schedule_redraw(widget);
890 static struct unlock_dialog *
891 unlock_dialog_create(struct desktop *desktop)
893 struct display *display = desktop->display;
894 struct unlock_dialog *dialog;
896 dialog = xzalloc(sizeof *dialog);
898 dialog->window = window_create_custom(display);
899 dialog->widget = window_frame_create(dialog->window, dialog);
900 window_set_title(dialog->window, "Unlock your desktop");
902 window_set_user_data(dialog->window, dialog);
903 window_set_keyboard_focus_handler(dialog->window,
904 unlock_dialog_keyboard_focus_handler);
905 dialog->button = widget_add_widget(dialog->widget, dialog);
906 widget_set_redraw_handler(dialog->widget,
907 unlock_dialog_redraw_handler);
908 widget_set_enter_handler(dialog->button,
909 unlock_dialog_widget_enter_handler);
910 widget_set_leave_handler(dialog->button,
911 unlock_dialog_widget_leave_handler);
912 widget_set_button_handler(dialog->button,
913 unlock_dialog_button_handler);
914 widget_set_touch_down_handler(dialog->button,
915 unlock_dialog_touch_down_handler);
916 widget_set_touch_up_handler(dialog->button,
917 unlock_dialog_touch_up_handler);
919 desktop_shell_set_lock_surface(desktop->shell,
920 window_get_wl_surface(dialog->window));
922 window_schedule_resize(dialog->window, 260, 230);
928 unlock_dialog_destroy(struct unlock_dialog *dialog)
930 window_destroy(dialog->window);
935 unlock_dialog_finish(struct task *task, uint32_t events)
937 struct desktop *desktop =
938 container_of(task, struct desktop, unlock_task);
940 desktop_shell_unlock(desktop->shell);
941 unlock_dialog_destroy(desktop->unlock_dialog);
942 desktop->unlock_dialog = NULL;
946 desktop_shell_configure(void *data,
947 struct desktop_shell *desktop_shell,
949 struct wl_surface *surface,
950 int32_t width, int32_t height)
952 struct window *window = wl_surface_get_user_data(surface);
953 struct surface *s = window_get_user_data(window);
955 s->configure(data, desktop_shell, edges, window, width, height);
959 desktop_shell_prepare_lock_surface(void *data,
960 struct desktop_shell *desktop_shell)
962 struct desktop *desktop = data;
964 if (!desktop->locking) {
965 desktop_shell_unlock(desktop->shell);
969 if (!desktop->unlock_dialog) {
970 desktop->unlock_dialog = unlock_dialog_create(desktop);
971 desktop->unlock_dialog->desktop = desktop;
976 desktop_shell_grab_cursor(void *data,
977 struct desktop_shell *desktop_shell,
980 struct desktop *desktop = data;
983 case DESKTOP_SHELL_CURSOR_NONE:
984 desktop->grab_cursor = CURSOR_BLANK;
986 case DESKTOP_SHELL_CURSOR_BUSY:
987 desktop->grab_cursor = CURSOR_WATCH;
989 case DESKTOP_SHELL_CURSOR_MOVE:
990 desktop->grab_cursor = CURSOR_DRAGGING;
992 case DESKTOP_SHELL_CURSOR_RESIZE_TOP:
993 desktop->grab_cursor = CURSOR_TOP;
995 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM:
996 desktop->grab_cursor = CURSOR_BOTTOM;
998 case DESKTOP_SHELL_CURSOR_RESIZE_LEFT:
999 desktop->grab_cursor = CURSOR_LEFT;
1001 case DESKTOP_SHELL_CURSOR_RESIZE_RIGHT:
1002 desktop->grab_cursor = CURSOR_RIGHT;
1004 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_LEFT:
1005 desktop->grab_cursor = CURSOR_TOP_LEFT;
1007 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_RIGHT:
1008 desktop->grab_cursor = CURSOR_TOP_RIGHT;
1010 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_LEFT:
1011 desktop->grab_cursor = CURSOR_BOTTOM_LEFT;
1013 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_RIGHT:
1014 desktop->grab_cursor = CURSOR_BOTTOM_RIGHT;
1016 case DESKTOP_SHELL_CURSOR_ARROW:
1018 desktop->grab_cursor = CURSOR_LEFT_PTR;
1022 static const struct desktop_shell_listener listener = {
1023 desktop_shell_configure,
1024 desktop_shell_prepare_lock_surface,
1025 desktop_shell_grab_cursor
1029 background_destroy(struct background *background)
1031 widget_destroy(background->widget);
1032 window_destroy(background->window);
1034 free(background->image);
1038 static struct background *
1039 background_create(struct desktop *desktop)
1041 struct background *background;
1042 struct weston_config_section *s;
1045 background = xzalloc(sizeof *background);
1046 background->base.configure = background_configure;
1047 background->window = window_create_custom(desktop->display);
1048 background->widget = window_add_widget(background->window, background);
1049 window_set_user_data(background->window, background);
1050 widget_set_redraw_handler(background->widget, background_draw);
1051 window_set_preferred_format(background->window,
1052 WINDOW_PREFERRED_FORMAT_RGB565);
1054 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
1055 weston_config_section_get_string(s, "background-image",
1057 DATADIR "/weston/pattern.png");
1058 weston_config_section_get_uint(s, "background-color",
1059 &background->color, 0xff002244);
1061 weston_config_section_get_string(s, "background-type",
1063 if (strcmp(type, "scale") == 0) {
1064 background->type = BACKGROUND_SCALE;
1065 } else if (strcmp(type, "scale-crop") == 0) {
1066 background->type = BACKGROUND_SCALE_CROP;
1067 } else if (strcmp(type, "tile") == 0) {
1068 background->type = BACKGROUND_TILE;
1070 background->type = -1;
1071 fprintf(stderr, "invalid background-type: %s\n",
1081 grab_surface_enter_handler(struct widget *widget, struct input *input,
1082 float x, float y, void *data)
1084 struct desktop *desktop = data;
1086 return desktop->grab_cursor;
1090 grab_surface_destroy(struct desktop *desktop)
1092 widget_destroy(desktop->grab_widget);
1093 window_destroy(desktop->grab_window);
1097 grab_surface_create(struct desktop *desktop)
1099 struct wl_surface *s;
1101 desktop->grab_window = window_create_custom(desktop->display);
1102 window_set_user_data(desktop->grab_window, desktop);
1104 s = window_get_wl_surface(desktop->grab_window);
1105 desktop_shell_set_grab_surface(desktop->shell, s);
1107 desktop->grab_widget =
1108 window_add_widget(desktop->grab_window, desktop);
1109 /* We set the allocation to 1x1 at 0,0 so the fake enter event
1110 * at 0,0 will go to this widget. */
1111 widget_set_allocation(desktop->grab_widget, 0, 0, 1, 1);
1113 widget_set_enter_handler(desktop->grab_widget,
1114 grab_surface_enter_handler);
1118 output_destroy(struct output *output)
1120 background_destroy(output->background);
1121 panel_destroy(output->panel);
1122 wl_output_destroy(output->output);
1123 wl_list_remove(&output->link);
1129 desktop_destroy_outputs(struct desktop *desktop)
1132 struct output *output;
1134 wl_list_for_each_safe(output, tmp, &desktop->outputs, link)
1135 output_destroy(output);
1139 output_handle_geometry(void *data,
1140 struct wl_output *wl_output,
1143 int physical_height,
1149 struct output *output = data;
1151 window_set_buffer_transform(output->panel->window, transform);
1152 window_set_buffer_transform(output->background->window, transform);
1156 output_handle_mode(void *data,
1157 struct wl_output *wl_output,
1166 output_handle_done(void *data,
1167 struct wl_output *wl_output)
1172 output_handle_scale(void *data,
1173 struct wl_output *wl_output,
1176 struct output *output = data;
1178 window_set_buffer_scale(output->panel->window, scale);
1179 window_set_buffer_scale(output->background->window, scale);
1182 static const struct wl_output_listener output_listener = {
1183 output_handle_geometry,
1190 output_init(struct output *output, struct desktop *desktop)
1192 struct wl_surface *surface;
1194 output->panel = panel_create(desktop);
1195 surface = window_get_wl_surface(output->panel->window);
1196 desktop_shell_set_panel(desktop->shell,
1197 output->output, surface);
1199 output->background = background_create(desktop);
1200 surface = window_get_wl_surface(output->background->window);
1201 desktop_shell_set_background(desktop->shell,
1202 output->output, surface);
1206 create_output(struct desktop *desktop, uint32_t id)
1208 struct output *output;
1210 output = calloc(1, sizeof *output);
1215 display_bind(desktop->display, id, &wl_output_interface, 2);
1217 wl_output_add_listener(output->output, &output_listener, output);
1219 wl_list_insert(&desktop->outputs, &output->link);
1221 /* On start up we may process an output global before the shell global
1222 * in which case we can't create the panel and background just yet */
1224 output_init(output, desktop);
1228 global_handler(struct display *display, uint32_t id,
1229 const char *interface, uint32_t version, void *data)
1231 struct desktop *desktop = data;
1233 if (!strcmp(interface, "desktop_shell")) {
1234 desktop->interface_version = (version < 2) ? version : 2;
1235 desktop->shell = display_bind(desktop->display,
1236 id, &desktop_shell_interface,
1237 desktop->interface_version);
1238 desktop_shell_add_listener(desktop->shell, &listener, desktop);
1239 } else if (!strcmp(interface, "wl_output")) {
1240 create_output(desktop, id);
1245 panel_add_launchers(struct panel *panel, struct desktop *desktop)
1247 struct weston_config_section *s;
1254 while (weston_config_next_section(desktop->config, &s, &name)) {
1255 if (strcmp(name, "launcher") != 0)
1258 weston_config_section_get_string(s, "icon", &icon, NULL);
1259 weston_config_section_get_string(s, "path", &path, NULL);
1261 if (icon != NULL && path != NULL) {
1262 panel_add_launcher(panel, icon, path);
1265 fprintf(stderr, "invalid launcher section\n");
1273 /* add default launcher */
1274 panel_add_launcher(panel,
1275 DATADIR "/weston/terminal.png",
1276 BINDIR "/weston-terminal");
1280 int main(int argc, char *argv[])
1282 struct desktop desktop = { 0 };
1283 struct output *output;
1284 struct weston_config_section *s;
1286 desktop.unlock_task.run = unlock_dialog_finish;
1287 wl_list_init(&desktop.outputs);
1289 desktop.config = weston_config_parse("weston.ini");
1290 s = weston_config_get_section(desktop.config, "shell", NULL, NULL);
1291 weston_config_section_get_bool(s, "locking", &desktop.locking, 1);
1293 desktop.display = display_create(&argc, argv);
1294 if (desktop.display == NULL) {
1295 fprintf(stderr, "failed to create display: %m\n");
1299 display_set_user_data(desktop.display, &desktop);
1300 display_set_global_handler(desktop.display, global_handler);
1302 /* Create panel and background for outputs processed before the shell
1303 * global interface was processed */
1304 wl_list_for_each(output, &desktop.outputs, link)
1306 output_init(output, &desktop);
1308 grab_surface_create(&desktop);
1310 signal(SIGCHLD, sigchild_handler);
1312 display_run(desktop.display);
1315 grab_surface_destroy(&desktop);
1316 desktop_destroy_outputs(&desktop);
1317 if (desktop.unlock_dialog)
1318 unlock_dialog_destroy(desktop.unlock_dialog);
1319 desktop_shell_destroy(desktop.shell);
1320 display_destroy(desktop.display);