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 struct unlock_dialog *unlock_dialog;
53 struct task unlock_task;
54 struct wl_list outputs;
56 struct window *grab_window;
57 struct widget *grab_widget;
59 enum cursor_type grab_cursor;
63 void (*configure)(void *data,
64 struct desktop_shell *desktop_shell,
65 uint32_t edges, struct window *window,
66 int32_t width, int32_t height);
71 struct window *window;
72 struct widget *widget;
73 struct wl_list launcher_list;
74 struct panel_clock *clock;
79 struct window *window;
80 struct widget *widget;
84 struct wl_output *output;
88 struct background *background;
91 struct panel_launcher {
92 struct widget *widget;
94 cairo_surface_t *icon;
103 struct widget *widget;
105 struct task clock_task;
109 struct unlock_dialog {
110 struct window *window;
111 struct widget *widget;
112 struct widget *button;
115 struct desktop *desktop;
118 static char *key_background_image = DATADIR "/weston/pattern.png";
119 static char *key_background_type = "tile";
120 static uint32_t key_panel_color = 0xaa000000;
121 static uint32_t key_background_color = 0xff002244;
122 static char *key_launcher_icon;
123 static char *key_launcher_path;
124 static void launcher_section_done(void *data);
125 static int key_locking = 1;
127 static const struct config_key shell_config_keys[] = {
128 { "background-image", CONFIG_KEY_STRING, &key_background_image },
129 { "background-type", CONFIG_KEY_STRING, &key_background_type },
130 { "panel-color", CONFIG_KEY_UNSIGNED_INTEGER, &key_panel_color },
131 { "background-color", CONFIG_KEY_UNSIGNED_INTEGER, &key_background_color },
132 { "locking", CONFIG_KEY_BOOLEAN, &key_locking },
135 static const struct config_key launcher_config_keys[] = {
136 { "icon", CONFIG_KEY_STRING, &key_launcher_icon },
137 { "path", CONFIG_KEY_STRING, &key_launcher_path },
140 static const struct config_section config_sections[] = {
142 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
144 launcher_config_keys, ARRAY_LENGTH(launcher_config_keys),
145 launcher_section_done }
149 sigchild_handler(int s)
154 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
155 fprintf(stderr, "child %d exited\n", pid);
159 menu_func(struct window *window, int index, void *data)
161 printf("Selected index %d from a panel menu.\n", index);
165 show_menu(struct panel *panel, struct input *input, uint32_t time)
168 static const char *entries[] = {
169 "Roy", "Pris", "Leon", "Zhora"
172 input_get_position(input, &x, &y);
173 window_show_menu(window_get_display(panel->window),
174 input, time, panel->window,
175 x - 10, y - 10, menu_func, entries, 4);
179 panel_launcher_activate(struct panel_launcher *widget)
186 fprintf(stderr, "fork failed: %m\n");
193 argv = widget->argv.data;
194 if (execve(argv[0], argv, widget->envp.data) < 0) {
195 fprintf(stderr, "execl '%s' failed: %m\n", argv[0]);
201 panel_launcher_redraw_handler(struct widget *widget, void *data)
203 struct panel_launcher *launcher = data;
204 cairo_surface_t *surface;
205 struct rectangle allocation;
208 surface = window_get_surface(launcher->panel->window);
209 cr = cairo_create(surface);
211 widget_get_allocation(widget, &allocation);
212 if (launcher->pressed) {
217 cairo_set_source_surface(cr, launcher->icon,
218 allocation.x, allocation.y);
221 if (launcher->focused) {
222 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
223 cairo_mask_surface(cr, launcher->icon,
224 allocation.x, allocation.y);
231 panel_launcher_motion_handler(struct widget *widget, struct input *input,
232 uint32_t time, float x, float y, void *data)
234 struct panel_launcher *launcher = data;
236 widget_set_tooltip(widget, basename((char *)launcher->path), x, y);
238 return CURSOR_LEFT_PTR;
242 set_hex_color(cairo_t *cr, uint32_t color)
244 cairo_set_source_rgba(cr,
245 ((color >> 16) & 0xff) / 255.0,
246 ((color >> 8) & 0xff) / 255.0,
247 ((color >> 0) & 0xff) / 255.0,
248 ((color >> 24) & 0xff) / 255.0);
252 panel_redraw_handler(struct widget *widget, void *data)
254 cairo_surface_t *surface;
256 struct panel *panel = data;
258 surface = window_get_surface(panel->window);
259 cr = cairo_create(surface);
260 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
261 set_hex_color(cr, key_panel_color);
265 cairo_surface_destroy(surface);
269 panel_launcher_enter_handler(struct widget *widget, struct input *input,
270 float x, float y, void *data)
272 struct panel_launcher *launcher = data;
274 launcher->focused = 1;
275 widget_schedule_redraw(widget);
277 return CURSOR_LEFT_PTR;
281 panel_launcher_leave_handler(struct widget *widget,
282 struct input *input, void *data)
284 struct panel_launcher *launcher = data;
286 launcher->focused = 0;
287 widget_destroy_tooltip(widget);
288 widget_schedule_redraw(widget);
292 panel_launcher_button_handler(struct widget *widget,
293 struct input *input, uint32_t time,
295 enum wl_pointer_button_state state, void *data)
297 struct panel_launcher *launcher;
299 launcher = widget_get_user_data(widget);
300 widget_schedule_redraw(widget);
301 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
302 panel_launcher_activate(launcher);
306 clock_func(struct task *task, uint32_t events)
308 struct panel_clock *clock =
309 container_of(task, struct panel_clock, clock_task);
312 if (read(clock->clock_fd, &exp, sizeof exp) != sizeof exp)
314 widget_schedule_redraw(clock->widget);
318 panel_clock_redraw_handler(struct widget *widget, void *data)
320 cairo_surface_t *surface;
321 struct panel_clock *clock = data;
323 struct rectangle allocation;
324 cairo_text_extents_t extents;
325 cairo_font_extents_t font_extents;
327 struct tm * timeinfo;
331 timeinfo = localtime(&rawtime);
332 strftime(string, sizeof string, "%a %b %d, %I:%M %p", timeinfo);
334 widget_get_allocation(widget, &allocation);
335 if (allocation.width == 0)
338 surface = window_get_surface(clock->panel->window);
339 cr = cairo_create(surface);
340 cairo_select_font_face(cr, "sans",
341 CAIRO_FONT_SLANT_NORMAL,
342 CAIRO_FONT_WEIGHT_NORMAL);
343 cairo_set_font_size(cr, 14);
344 cairo_text_extents(cr, string, &extents);
345 cairo_font_extents (cr, &font_extents);
346 cairo_move_to(cr, allocation.x + 5,
347 allocation.y + 3 * (allocation.height >> 2) + 1);
348 cairo_set_source_rgb(cr, 0, 0, 0);
349 cairo_show_text(cr, string);
350 cairo_move_to(cr, allocation.x + 4,
351 allocation.y + 3 * (allocation.height >> 2));
352 cairo_set_source_rgb(cr, 1, 1, 1);
353 cairo_show_text(cr, string);
358 clock_timer_reset(struct panel_clock *clock)
360 struct itimerspec its;
362 its.it_interval.tv_sec = 60;
363 its.it_interval.tv_nsec = 0;
364 its.it_value.tv_sec = 60;
365 its.it_value.tv_nsec = 0;
366 if (timerfd_settime(clock->clock_fd, 0, &its, NULL) < 0) {
367 fprintf(stderr, "could not set timerfd\n: %m");
375 panel_destroy_clock(struct panel_clock *clock)
377 widget_destroy(clock->widget);
379 close(clock->clock_fd);
385 panel_add_clock(struct panel *panel)
387 struct panel_clock *clock;
390 timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
392 fprintf(stderr, "could not create timerfd\n: %m");
396 clock = malloc(sizeof *clock);
397 memset(clock, 0, sizeof *clock);
398 clock->panel = panel;
399 panel->clock = clock;
400 clock->clock_fd = timerfd;
402 clock->clock_task.run = clock_func;
403 display_watch_fd(window_get_display(panel->window), clock->clock_fd,
404 EPOLLIN, &clock->clock_task);
405 clock_timer_reset(clock);
407 clock->widget = widget_add_widget(panel->widget, clock);
408 widget_set_redraw_handler(clock->widget, panel_clock_redraw_handler);
412 panel_button_handler(struct widget *widget,
413 struct input *input, uint32_t time,
415 enum wl_pointer_button_state state, void *data)
417 struct panel *panel = data;
419 if (button == BTN_RIGHT && state == WL_POINTER_BUTTON_STATE_PRESSED)
420 show_menu(panel, input, time);
424 panel_resize_handler(struct widget *widget,
425 int32_t width, int32_t height, void *data)
427 struct panel_launcher *launcher;
428 struct panel *panel = data;
433 wl_list_for_each(launcher, &panel->launcher_list, link) {
434 w = cairo_image_surface_get_width(launcher->icon);
435 h = cairo_image_surface_get_height(launcher->icon);
436 widget_set_allocation(launcher->widget,
437 x, y - h / 2, w + 1, h + 1);
444 widget_set_allocation(panel->clock->widget,
445 width - w - 8, y - h / 2, w + 1, h + 1);
449 panel_configure(void *data,
450 struct desktop_shell *desktop_shell,
451 uint32_t edges, struct window *window,
452 int32_t width, int32_t height)
454 struct surface *surface = window_get_user_data(window);
455 struct panel *panel = container_of(surface, struct panel, base);
457 window_schedule_resize(panel->window, width, 32);
461 panel_destroy_launcher(struct panel_launcher *launcher)
463 wl_array_release(&launcher->argv);
464 wl_array_release(&launcher->envp);
466 free(launcher->path);
468 cairo_surface_destroy(launcher->icon);
470 widget_destroy(launcher->widget);
471 wl_list_remove(&launcher->link);
477 panel_destroy(struct panel *panel)
479 struct panel_launcher *tmp;
480 struct panel_launcher *launcher;
482 panel_destroy_clock(panel->clock);
484 wl_list_for_each_safe(launcher, tmp, &panel->launcher_list, link)
485 panel_destroy_launcher(launcher);
487 widget_destroy(panel->widget);
488 window_destroy(panel->window);
493 static struct panel *
494 panel_create(struct display *display)
498 panel = malloc(sizeof *panel);
499 memset(panel, 0, sizeof *panel);
501 panel->base.configure = panel_configure;
502 panel->window = window_create_custom(display);
503 panel->widget = window_add_widget(panel->window, panel);
504 wl_list_init(&panel->launcher_list);
506 window_set_title(panel->window, "panel");
507 window_set_user_data(panel->window, panel);
509 widget_set_redraw_handler(panel->widget, panel_redraw_handler);
510 widget_set_resize_handler(panel->widget, panel_resize_handler);
511 widget_set_button_handler(panel->widget, panel_button_handler);
513 panel_add_clock(panel);
518 static cairo_surface_t *
519 load_icon_or_fallback(const char *icon)
521 cairo_surface_t *surface = cairo_image_surface_create_from_png(icon);
524 if (cairo_surface_status(surface) == CAIRO_STATUS_SUCCESS)
527 cairo_surface_destroy(surface);
528 fprintf(stderr, "ERROR loading icon from file '%s'\n", icon);
530 /* draw fallback icon */
531 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
533 cr = cairo_create(surface);
535 cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1);
538 cairo_set_source_rgba(cr, 0, 0, 0, 1);
539 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
540 cairo_rectangle(cr, 0, 0, 20, 20);
541 cairo_move_to(cr, 4, 4);
542 cairo_line_to(cr, 16, 16);
543 cairo_move_to(cr, 4, 16);
544 cairo_line_to(cr, 16, 4);
553 panel_add_launcher(struct panel *panel, const char *icon, const char *path)
555 struct panel_launcher *launcher;
556 char *start, *p, *eq, **ps;
559 launcher = malloc(sizeof *launcher);
560 memset(launcher, 0, sizeof *launcher);
561 launcher->icon = load_icon_or_fallback(icon);
562 launcher->path = strdup(path);
564 wl_array_init(&launcher->envp);
565 wl_array_init(&launcher->argv);
566 for (i = 0; environ[i]; i++) {
567 ps = wl_array_add(&launcher->envp, sizeof *ps);
572 start = launcher->path;
574 for (p = start, eq = NULL; *p && !isspace(*p); p++)
579 ps = launcher->envp.data;
580 for (k = 0; k < i; k++)
581 if (strncmp(ps[k], start, eq - start) == 0) {
586 ps = wl_array_add(&launcher->envp, sizeof *ps);
591 ps = wl_array_add(&launcher->argv, sizeof *ps);
596 while (*p && isspace(*p))
602 ps = wl_array_add(&launcher->envp, sizeof *ps);
604 ps = wl_array_add(&launcher->argv, sizeof *ps);
607 launcher->panel = panel;
608 wl_list_insert(panel->launcher_list.prev, &launcher->link);
610 launcher->widget = widget_add_widget(panel->widget, launcher);
611 widget_set_enter_handler(launcher->widget,
612 panel_launcher_enter_handler);
613 widget_set_leave_handler(launcher->widget,
614 panel_launcher_leave_handler);
615 widget_set_button_handler(launcher->widget,
616 panel_launcher_button_handler);
617 widget_set_redraw_handler(launcher->widget,
618 panel_launcher_redraw_handler);
619 widget_set_motion_handler(launcher->widget,
620 panel_launcher_motion_handler);
629 background_draw(struct widget *widget, void *data)
631 struct background *background = data;
632 cairo_surface_t *surface, *image;
633 cairo_pattern_t *pattern;
634 cairo_matrix_t matrix;
637 struct rectangle allocation;
639 struct display *display;
640 struct wl_region *opaque;
642 surface = window_get_surface(background->window);
644 cr = cairo_create(surface);
645 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
646 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
649 widget_get_allocation(widget, &allocation);
651 if (key_background_image)
652 image = load_cairo_surface(key_background_image);
654 if (strcmp(key_background_type, "scale") == 0)
655 type = BACKGROUND_SCALE;
656 else if (strcmp(key_background_type, "tile") == 0)
657 type = BACKGROUND_TILE;
659 fprintf(stderr, "invalid background-type: %s\n",
660 key_background_type);
662 if (image && type != -1) {
663 pattern = cairo_pattern_create_for_surface(image);
665 case BACKGROUND_SCALE:
666 sx = (double) cairo_image_surface_get_width(image) /
668 sy = (double) cairo_image_surface_get_height(image) /
670 cairo_matrix_init_scale(&matrix, sx, sy);
671 cairo_pattern_set_matrix(pattern, &matrix);
673 case BACKGROUND_TILE:
674 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
677 cairo_set_source(cr, pattern);
678 cairo_pattern_destroy (pattern);
679 cairo_surface_destroy(image);
681 set_hex_color(cr, key_background_color);
686 cairo_surface_destroy(surface);
688 display = window_get_display(background->window);
689 opaque = wl_compositor_create_region(display_get_compositor(display));
690 wl_region_add(opaque, allocation.x, allocation.y,
691 allocation.width, allocation.height);
692 wl_surface_set_opaque_region(window_get_wl_surface(background->window), opaque);
693 wl_region_destroy(opaque);
697 background_configure(void *data,
698 struct desktop_shell *desktop_shell,
699 uint32_t edges, struct window *window,
700 int32_t width, int32_t height)
702 struct background *background =
703 (struct background *) window_get_user_data(window);
705 widget_schedule_resize(background->widget, width, height);
709 unlock_dialog_redraw_handler(struct widget *widget, void *data)
711 struct unlock_dialog *dialog = data;
712 struct rectangle allocation;
714 cairo_surface_t *surface;
715 cairo_pattern_t *pat;
718 surface = window_get_surface(dialog->window);
719 cr = cairo_create(surface);
721 widget_get_allocation(dialog->widget, &allocation);
722 cairo_rectangle(cr, allocation.x, allocation.y,
723 allocation.width, allocation.height);
724 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
725 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
728 cairo_translate(cr, allocation.x, allocation.y);
729 if (dialog->button_focused)
734 cx = allocation.width / 2.0;
735 cy = allocation.height / 2.0;
736 r = (cx < cy ? cx : cy) * 0.4;
737 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
738 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
739 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
740 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
741 cairo_set_source(cr, pat);
742 cairo_pattern_destroy(pat);
743 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
746 widget_set_allocation(dialog->button,
747 allocation.x + cx - r,
748 allocation.y + cy - r, 2 * r, 2 * r);
752 cairo_surface_destroy(surface);
756 unlock_dialog_button_handler(struct widget *widget,
757 struct input *input, uint32_t time,
759 enum wl_pointer_button_state state, void *data)
761 struct unlock_dialog *dialog = data;
762 struct desktop *desktop = dialog->desktop;
764 if (button == BTN_LEFT) {
765 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
767 display_defer(desktop->display, &desktop->unlock_task);
774 unlock_dialog_keyboard_focus_handler(struct window *window,
775 struct input *device, void *data)
777 window_schedule_redraw(window);
781 unlock_dialog_widget_enter_handler(struct widget *widget,
783 float x, float y, void *data)
785 struct unlock_dialog *dialog = data;
787 dialog->button_focused = 1;
788 widget_schedule_redraw(widget);
790 return CURSOR_LEFT_PTR;
794 unlock_dialog_widget_leave_handler(struct widget *widget,
795 struct input *input, void *data)
797 struct unlock_dialog *dialog = data;
799 dialog->button_focused = 0;
800 widget_schedule_redraw(widget);
803 static struct unlock_dialog *
804 unlock_dialog_create(struct desktop *desktop)
806 struct display *display = desktop->display;
807 struct unlock_dialog *dialog;
809 dialog = malloc(sizeof *dialog);
812 memset(dialog, 0, sizeof *dialog);
814 dialog->window = window_create_custom(display);
815 dialog->widget = frame_create(dialog->window, dialog);
816 window_set_title(dialog->window, "Unlock your desktop");
818 window_set_user_data(dialog->window, dialog);
819 window_set_keyboard_focus_handler(dialog->window,
820 unlock_dialog_keyboard_focus_handler);
821 dialog->button = widget_add_widget(dialog->widget, dialog);
822 widget_set_redraw_handler(dialog->widget,
823 unlock_dialog_redraw_handler);
824 widget_set_enter_handler(dialog->button,
825 unlock_dialog_widget_enter_handler);
826 widget_set_leave_handler(dialog->button,
827 unlock_dialog_widget_leave_handler);
828 widget_set_button_handler(dialog->button,
829 unlock_dialog_button_handler);
831 desktop_shell_set_lock_surface(desktop->shell,
832 window_get_wl_surface(dialog->window));
834 window_schedule_resize(dialog->window, 260, 230);
840 unlock_dialog_destroy(struct unlock_dialog *dialog)
842 window_destroy(dialog->window);
847 unlock_dialog_finish(struct task *task, uint32_t events)
849 struct desktop *desktop =
850 container_of(task, struct desktop, unlock_task);
852 desktop_shell_unlock(desktop->shell);
853 unlock_dialog_destroy(desktop->unlock_dialog);
854 desktop->unlock_dialog = NULL;
858 desktop_shell_configure(void *data,
859 struct desktop_shell *desktop_shell,
861 struct wl_surface *surface,
862 int32_t width, int32_t height)
864 struct window *window = wl_surface_get_user_data(surface);
865 struct surface *s = window_get_user_data(window);
867 s->configure(data, desktop_shell, edges, window, width, height);
871 desktop_shell_prepare_lock_surface(void *data,
872 struct desktop_shell *desktop_shell)
874 struct desktop *desktop = data;
877 desktop_shell_unlock(desktop->shell);
881 if (!desktop->unlock_dialog) {
882 desktop->unlock_dialog = unlock_dialog_create(desktop);
883 desktop->unlock_dialog->desktop = desktop;
888 desktop_shell_grab_cursor(void *data,
889 struct desktop_shell *desktop_shell,
892 struct desktop *desktop = data;
895 case DESKTOP_SHELL_CURSOR_NONE:
896 desktop->grab_cursor = CURSOR_BLANK;
898 case DESKTOP_SHELL_CURSOR_BUSY:
899 desktop->grab_cursor = CURSOR_WATCH;
901 case DESKTOP_SHELL_CURSOR_MOVE:
902 desktop->grab_cursor = CURSOR_DRAGGING;
904 case DESKTOP_SHELL_CURSOR_RESIZE_TOP:
905 desktop->grab_cursor = CURSOR_TOP;
907 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM:
908 desktop->grab_cursor = CURSOR_BOTTOM;
910 case DESKTOP_SHELL_CURSOR_RESIZE_LEFT:
911 desktop->grab_cursor = CURSOR_LEFT;
913 case DESKTOP_SHELL_CURSOR_RESIZE_RIGHT:
914 desktop->grab_cursor = CURSOR_RIGHT;
916 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_LEFT:
917 desktop->grab_cursor = CURSOR_TOP_LEFT;
919 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_RIGHT:
920 desktop->grab_cursor = CURSOR_TOP_RIGHT;
922 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_LEFT:
923 desktop->grab_cursor = CURSOR_BOTTOM_LEFT;
925 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_RIGHT:
926 desktop->grab_cursor = CURSOR_BOTTOM_RIGHT;
928 case DESKTOP_SHELL_CURSOR_ARROW:
930 desktop->grab_cursor = CURSOR_LEFT_PTR;
934 static const struct desktop_shell_listener listener = {
935 desktop_shell_configure,
936 desktop_shell_prepare_lock_surface,
937 desktop_shell_grab_cursor
941 background_destroy(struct background *background)
943 widget_destroy(background->widget);
944 window_destroy(background->window);
949 static struct background *
950 background_create(struct desktop *desktop)
952 struct background *background;
954 background = malloc(sizeof *background);
955 memset(background, 0, sizeof *background);
957 background->base.configure = background_configure;
958 background->window = window_create_custom(desktop->display);
959 background->widget = window_add_widget(background->window, background);
960 window_set_user_data(background->window, background);
961 widget_set_redraw_handler(background->widget, background_draw);
967 grab_surface_enter_handler(struct widget *widget, struct input *input,
968 float x, float y, void *data)
970 struct desktop *desktop = data;
972 return desktop->grab_cursor;
976 grab_surface_destroy(struct desktop *desktop)
978 widget_destroy(desktop->grab_widget);
979 window_destroy(desktop->grab_window);
983 grab_surface_create(struct desktop *desktop)
985 struct wl_surface *s;
987 desktop->grab_window = window_create_custom(desktop->display);
988 window_set_user_data(desktop->grab_window, desktop);
990 s = window_get_wl_surface(desktop->grab_window);
991 desktop_shell_set_grab_surface(desktop->shell, s);
993 desktop->grab_widget =
994 window_add_widget(desktop->grab_window, desktop);
995 /* We set the allocation to 1x1 at 0,0 so the fake enter event
996 * at 0,0 will go to this widget. */
997 widget_set_allocation(desktop->grab_widget, 0, 0, 1, 1);
999 widget_set_enter_handler(desktop->grab_widget,
1000 grab_surface_enter_handler);
1004 output_destroy(struct output *output)
1006 background_destroy(output->background);
1007 panel_destroy(output->panel);
1008 wl_output_destroy(output->output);
1009 wl_list_remove(&output->link);
1015 desktop_destroy_outputs(struct desktop *desktop)
1018 struct output *output;
1020 wl_list_for_each_safe(output, tmp, &desktop->outputs, link)
1021 output_destroy(output);
1025 create_output(struct desktop *desktop, uint32_t id)
1027 struct output *output;
1029 output = calloc(1, sizeof *output);
1034 display_bind(desktop->display, id, &wl_output_interface, 1);
1036 wl_list_insert(&desktop->outputs, &output->link);
1040 global_handler(struct display *display, uint32_t id,
1041 const char *interface, uint32_t version, void *data)
1043 struct desktop *desktop = data;
1045 if (!strcmp(interface, "desktop_shell")) {
1046 desktop->shell = display_bind(desktop->display,
1047 id, &desktop_shell_interface, 1);
1048 desktop_shell_add_listener(desktop->shell, &listener, desktop);
1049 } else if (!strcmp(interface, "wl_output")) {
1050 create_output(desktop, id);
1055 launcher_section_done(void *data)
1057 struct desktop *desktop = data;
1058 struct output *output;
1060 if (key_launcher_icon == NULL || key_launcher_path == NULL) {
1061 fprintf(stderr, "invalid launcher section\n");
1065 wl_list_for_each(output, &desktop->outputs, link) {
1066 panel_add_launcher(output->panel,
1067 key_launcher_icon, key_launcher_path);
1070 free(key_launcher_icon);
1071 key_launcher_icon = NULL;
1072 free(key_launcher_path);
1073 key_launcher_path = NULL;
1077 add_default_launcher(struct desktop *desktop)
1079 struct output *output;
1081 wl_list_for_each(output, &desktop->outputs, link)
1082 panel_add_launcher(output->panel,
1083 DATADIR "/weston/terminal.png",
1084 BINDIR "/weston-terminal");
1087 int main(int argc, char *argv[])
1089 struct desktop desktop = { 0 };
1091 struct output *output;
1094 desktop.unlock_task.run = unlock_dialog_finish;
1095 wl_list_init(&desktop.outputs);
1097 desktop.display = display_create(argc, argv);
1098 if (desktop.display == NULL) {
1099 fprintf(stderr, "failed to create display: %m\n");
1103 display_set_user_data(desktop.display, &desktop);
1104 display_set_global_handler(desktop.display, global_handler);
1106 wl_list_for_each(output, &desktop.outputs, link) {
1107 struct wl_surface *surface;
1109 output->panel = panel_create(desktop.display);
1110 surface = window_get_wl_surface(output->panel->window);
1111 desktop_shell_set_panel(desktop.shell,
1112 output->output, surface);
1114 output->background = background_create(&desktop);
1115 surface = window_get_wl_surface(output->background->window);
1116 desktop_shell_set_background(desktop.shell,
1117 output->output, surface);
1120 grab_surface_create(&desktop);
1122 config_file = config_file_path("weston.ini");
1123 ret = parse_config_file(config_file,
1124 config_sections, ARRAY_LENGTH(config_sections),
1128 add_default_launcher(&desktop);
1130 signal(SIGCHLD, sigchild_handler);
1132 display_run(desktop.display);
1135 grab_surface_destroy(&desktop);
1136 desktop_destroy_outputs(&desktop);
1137 if (desktop.unlock_dialog)
1138 unlock_dialog_destroy(desktop.unlock_dialog);
1139 desktop_shell_destroy(desktop.shell);
1140 display_destroy(desktop.display);