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 enum cursor_type grab_cursor;
66 void (*configure)(void *data,
67 struct desktop_shell *desktop_shell,
68 uint32_t edges, struct window *window,
69 int32_t width, int32_t height);
74 struct window *window;
75 struct widget *widget;
76 struct wl_list launcher_list;
77 struct panel_clock *clock;
83 struct window *window;
84 struct widget *widget;
89 struct wl_output *output;
93 struct background *background;
96 struct panel_launcher {
97 struct widget *widget;
99 cairo_surface_t *icon;
100 int focused, pressed;
103 struct wl_array envp;
104 struct wl_array argv;
108 struct widget *widget;
110 struct task clock_task;
114 struct unlock_dialog {
115 struct window *window;
116 struct widget *widget;
117 struct widget *button;
120 struct desktop *desktop;
123 static char *key_background_image = DATADIR "/weston/pattern.png";
124 static char *key_background_type = "tile";
125 static uint32_t key_panel_color = 0xaa000000;
126 static uint32_t key_background_color = 0xff002244;
127 static char *key_launcher_icon;
128 static char *key_launcher_path;
129 static void launcher_section_done(void *data);
130 static int key_locking = 1;
132 static const struct config_key shell_config_keys[] = {
133 { "background-image", CONFIG_KEY_STRING, &key_background_image },
134 { "background-type", CONFIG_KEY_STRING, &key_background_type },
135 { "panel-color", CONFIG_KEY_UNSIGNED_INTEGER, &key_panel_color },
136 { "background-color", CONFIG_KEY_UNSIGNED_INTEGER, &key_background_color },
137 { "locking", CONFIG_KEY_BOOLEAN, &key_locking },
140 static const struct config_key launcher_config_keys[] = {
141 { "icon", CONFIG_KEY_STRING, &key_launcher_icon },
142 { "path", CONFIG_KEY_STRING, &key_launcher_path },
145 static const struct config_section config_sections[] = {
147 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
149 launcher_config_keys, ARRAY_LENGTH(launcher_config_keys),
150 launcher_section_done }
154 sigchild_handler(int s)
159 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
160 fprintf(stderr, "child %d exited\n", pid);
164 menu_func(struct window *window, int index, void *data)
166 printf("Selected index %d from a panel menu.\n", index);
170 show_menu(struct panel *panel, struct input *input, uint32_t time)
173 static const char *entries[] = {
174 "Roy", "Pris", "Leon", "Zhora"
177 input_get_position(input, &x, &y);
178 window_show_menu(window_get_display(panel->window),
179 input, time, panel->window,
180 x - 10, y - 10, menu_func, entries, 4);
184 is_desktop_painted(struct desktop *desktop)
186 struct output *output;
188 wl_list_for_each(output, &desktop->outputs, link) {
189 if (output->panel && !output->panel->painted)
191 if (output->background && !output->background->painted)
199 check_desktop_ready(struct window *window)
201 struct display *display;
202 struct desktop *desktop;
204 display = window_get_display(window);
205 desktop = display_get_user_data(display);
207 if (!desktop->painted && is_desktop_painted(desktop)) {
208 desktop->painted = 1;
210 if (desktop->interface_version >= 2)
211 desktop_shell_desktop_ready(desktop->shell);
216 panel_launcher_activate(struct panel_launcher *widget)
223 fprintf(stderr, "fork failed: %m\n");
230 argv = widget->argv.data;
231 if (execve(argv[0], argv, widget->envp.data) < 0) {
232 fprintf(stderr, "execl '%s' failed: %m\n", argv[0]);
238 panel_launcher_redraw_handler(struct widget *widget, void *data)
240 struct panel_launcher *launcher = data;
241 struct rectangle allocation;
244 cr = widget_cairo_create(launcher->panel->widget);
246 widget_get_allocation(widget, &allocation);
247 if (launcher->pressed) {
252 cairo_set_source_surface(cr, launcher->icon,
253 allocation.x, allocation.y);
256 if (launcher->focused) {
257 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
258 cairo_mask_surface(cr, launcher->icon,
259 allocation.x, allocation.y);
266 panel_launcher_motion_handler(struct widget *widget, struct input *input,
267 uint32_t time, float x, float y, void *data)
269 struct panel_launcher *launcher = data;
271 widget_set_tooltip(widget, basename((char *)launcher->path), x, y);
273 return CURSOR_LEFT_PTR;
277 set_hex_color(cairo_t *cr, uint32_t color)
279 cairo_set_source_rgba(cr,
280 ((color >> 16) & 0xff) / 255.0,
281 ((color >> 8) & 0xff) / 255.0,
282 ((color >> 0) & 0xff) / 255.0,
283 ((color >> 24) & 0xff) / 255.0);
287 panel_redraw_handler(struct widget *widget, void *data)
289 cairo_surface_t *surface;
291 struct panel *panel = data;
293 cr = widget_cairo_create(panel->widget);
294 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
295 set_hex_color(cr, key_panel_color);
299 surface = window_get_surface(panel->window);
300 cairo_surface_destroy(surface);
302 check_desktop_ready(panel->window);
306 panel_launcher_enter_handler(struct widget *widget, struct input *input,
307 float x, float y, void *data)
309 struct panel_launcher *launcher = data;
311 launcher->focused = 1;
312 widget_schedule_redraw(widget);
314 return CURSOR_LEFT_PTR;
318 panel_launcher_leave_handler(struct widget *widget,
319 struct input *input, void *data)
321 struct panel_launcher *launcher = data;
323 launcher->focused = 0;
324 widget_destroy_tooltip(widget);
325 widget_schedule_redraw(widget);
329 panel_launcher_button_handler(struct widget *widget,
330 struct input *input, uint32_t time,
332 enum wl_pointer_button_state state, void *data)
334 struct panel_launcher *launcher;
336 launcher = widget_get_user_data(widget);
337 widget_schedule_redraw(widget);
338 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
339 panel_launcher_activate(launcher);
343 clock_func(struct task *task, uint32_t events)
345 struct panel_clock *clock =
346 container_of(task, struct panel_clock, clock_task);
349 if (read(clock->clock_fd, &exp, sizeof exp) != sizeof exp)
351 widget_schedule_redraw(clock->widget);
355 panel_clock_redraw_handler(struct widget *widget, void *data)
357 struct panel_clock *clock = data;
359 struct rectangle allocation;
360 cairo_text_extents_t extents;
361 cairo_font_extents_t font_extents;
363 struct tm * timeinfo;
367 timeinfo = localtime(&rawtime);
368 strftime(string, sizeof string, "%a %b %d, %I:%M %p", timeinfo);
370 widget_get_allocation(widget, &allocation);
371 if (allocation.width == 0)
374 cr = widget_cairo_create(clock->panel->widget);
375 cairo_select_font_face(cr, "sans",
376 CAIRO_FONT_SLANT_NORMAL,
377 CAIRO_FONT_WEIGHT_NORMAL);
378 cairo_set_font_size(cr, 14);
379 cairo_text_extents(cr, string, &extents);
380 cairo_font_extents (cr, &font_extents);
381 cairo_move_to(cr, allocation.x + 5,
382 allocation.y + 3 * (allocation.height >> 2) + 1);
383 cairo_set_source_rgb(cr, 0, 0, 0);
384 cairo_show_text(cr, string);
385 cairo_move_to(cr, allocation.x + 4,
386 allocation.y + 3 * (allocation.height >> 2));
387 cairo_set_source_rgb(cr, 1, 1, 1);
388 cairo_show_text(cr, string);
393 clock_timer_reset(struct panel_clock *clock)
395 struct itimerspec its;
397 its.it_interval.tv_sec = 60;
398 its.it_interval.tv_nsec = 0;
399 its.it_value.tv_sec = 60;
400 its.it_value.tv_nsec = 0;
401 if (timerfd_settime(clock->clock_fd, 0, &its, NULL) < 0) {
402 fprintf(stderr, "could not set timerfd\n: %m");
410 panel_destroy_clock(struct panel_clock *clock)
412 widget_destroy(clock->widget);
414 close(clock->clock_fd);
420 panel_add_clock(struct panel *panel)
422 struct panel_clock *clock;
425 timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
427 fprintf(stderr, "could not create timerfd\n: %m");
431 clock = malloc(sizeof *clock);
432 memset(clock, 0, sizeof *clock);
433 clock->panel = panel;
434 panel->clock = clock;
435 clock->clock_fd = timerfd;
437 clock->clock_task.run = clock_func;
438 display_watch_fd(window_get_display(panel->window), clock->clock_fd,
439 EPOLLIN, &clock->clock_task);
440 clock_timer_reset(clock);
442 clock->widget = widget_add_widget(panel->widget, clock);
443 widget_set_redraw_handler(clock->widget, panel_clock_redraw_handler);
447 panel_button_handler(struct widget *widget,
448 struct input *input, uint32_t time,
450 enum wl_pointer_button_state state, void *data)
452 struct panel *panel = data;
454 if (button == BTN_RIGHT && state == WL_POINTER_BUTTON_STATE_PRESSED)
455 show_menu(panel, input, time);
459 panel_resize_handler(struct widget *widget,
460 int32_t width, int32_t height, void *data)
462 struct panel_launcher *launcher;
463 struct panel *panel = data;
468 wl_list_for_each(launcher, &panel->launcher_list, link) {
469 w = cairo_image_surface_get_width(launcher->icon);
470 h = cairo_image_surface_get_height(launcher->icon);
471 widget_set_allocation(launcher->widget,
472 x, y - h / 2, w + 1, h + 1);
479 widget_set_allocation(panel->clock->widget,
480 width - w - 8, y - h / 2, w + 1, h + 1);
484 panel_configure(void *data,
485 struct desktop_shell *desktop_shell,
486 uint32_t edges, struct window *window,
487 int32_t width, int32_t height)
489 struct surface *surface = window_get_user_data(window);
490 struct panel *panel = container_of(surface, struct panel, base);
492 window_schedule_resize(panel->window, width, 32);
496 panel_destroy_launcher(struct panel_launcher *launcher)
498 wl_array_release(&launcher->argv);
499 wl_array_release(&launcher->envp);
501 free(launcher->path);
503 cairo_surface_destroy(launcher->icon);
505 widget_destroy(launcher->widget);
506 wl_list_remove(&launcher->link);
512 panel_destroy(struct panel *panel)
514 struct panel_launcher *tmp;
515 struct panel_launcher *launcher;
517 panel_destroy_clock(panel->clock);
519 wl_list_for_each_safe(launcher, tmp, &panel->launcher_list, link)
520 panel_destroy_launcher(launcher);
522 widget_destroy(panel->widget);
523 window_destroy(panel->window);
528 static struct panel *
529 panel_create(struct display *display)
533 panel = malloc(sizeof *panel);
534 memset(panel, 0, sizeof *panel);
536 panel->base.configure = panel_configure;
537 panel->window = window_create_custom(display);
538 panel->widget = window_add_widget(panel->window, panel);
539 wl_list_init(&panel->launcher_list);
541 window_set_title(panel->window, "panel");
542 window_set_user_data(panel->window, panel);
544 widget_set_redraw_handler(panel->widget, panel_redraw_handler);
545 widget_set_resize_handler(panel->widget, panel_resize_handler);
546 widget_set_button_handler(panel->widget, panel_button_handler);
548 panel_add_clock(panel);
553 static cairo_surface_t *
554 load_icon_or_fallback(const char *icon)
556 cairo_surface_t *surface = cairo_image_surface_create_from_png(icon);
557 cairo_status_t status;
560 status = cairo_surface_status(surface);
561 if (status == CAIRO_STATUS_SUCCESS)
564 cairo_surface_destroy(surface);
565 fprintf(stderr, "ERROR loading icon from file '%s', error: '%s'\n",
566 icon, cairo_status_to_string(status));
568 /* draw fallback icon */
569 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
571 cr = cairo_create(surface);
573 cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1);
576 cairo_set_source_rgba(cr, 0, 0, 0, 1);
577 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
578 cairo_rectangle(cr, 0, 0, 20, 20);
579 cairo_move_to(cr, 4, 4);
580 cairo_line_to(cr, 16, 16);
581 cairo_move_to(cr, 4, 16);
582 cairo_line_to(cr, 16, 4);
591 panel_add_launcher(struct panel *panel, const char *icon, const char *path)
593 struct panel_launcher *launcher;
594 char *start, *p, *eq, **ps;
597 launcher = malloc(sizeof *launcher);
598 memset(launcher, 0, sizeof *launcher);
599 launcher->icon = load_icon_or_fallback(icon);
600 launcher->path = strdup(path);
602 wl_array_init(&launcher->envp);
603 wl_array_init(&launcher->argv);
604 for (i = 0; environ[i]; i++) {
605 ps = wl_array_add(&launcher->envp, sizeof *ps);
610 start = launcher->path;
612 for (p = start, eq = NULL; *p && !isspace(*p); p++)
617 ps = launcher->envp.data;
618 for (k = 0; k < i; k++)
619 if (strncmp(ps[k], start, eq - start) == 0) {
624 ps = wl_array_add(&launcher->envp, sizeof *ps);
629 ps = wl_array_add(&launcher->argv, sizeof *ps);
634 while (*p && isspace(*p))
640 ps = wl_array_add(&launcher->envp, sizeof *ps);
642 ps = wl_array_add(&launcher->argv, sizeof *ps);
645 launcher->panel = panel;
646 wl_list_insert(panel->launcher_list.prev, &launcher->link);
648 launcher->widget = widget_add_widget(panel->widget, launcher);
649 widget_set_enter_handler(launcher->widget,
650 panel_launcher_enter_handler);
651 widget_set_leave_handler(launcher->widget,
652 panel_launcher_leave_handler);
653 widget_set_button_handler(launcher->widget,
654 panel_launcher_button_handler);
655 widget_set_redraw_handler(launcher->widget,
656 panel_launcher_redraw_handler);
657 widget_set_motion_handler(launcher->widget,
658 panel_launcher_motion_handler);
667 background_draw(struct widget *widget, void *data)
669 struct background *background = data;
670 cairo_surface_t *surface, *image;
671 cairo_pattern_t *pattern;
672 cairo_matrix_t matrix;
675 struct rectangle allocation;
677 struct display *display;
678 struct wl_region *opaque;
680 surface = window_get_surface(background->window);
682 cr = widget_cairo_create(background->widget);
683 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
684 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
687 widget_get_allocation(widget, &allocation);
689 if (key_background_image)
690 image = load_cairo_surface(key_background_image);
692 if (strcmp(key_background_type, "scale") == 0)
693 type = BACKGROUND_SCALE;
694 else if (strcmp(key_background_type, "tile") == 0)
695 type = BACKGROUND_TILE;
697 fprintf(stderr, "invalid background-type: %s\n",
698 key_background_type);
700 if (image && type != -1) {
701 pattern = cairo_pattern_create_for_surface(image);
703 case BACKGROUND_SCALE:
704 sx = (double) cairo_image_surface_get_width(image) /
706 sy = (double) cairo_image_surface_get_height(image) /
708 cairo_matrix_init_scale(&matrix, sx, sy);
709 cairo_pattern_set_matrix(pattern, &matrix);
711 case BACKGROUND_TILE:
712 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
715 cairo_set_source(cr, pattern);
716 cairo_pattern_destroy (pattern);
717 cairo_surface_destroy(image);
719 set_hex_color(cr, key_background_color);
724 cairo_surface_destroy(surface);
726 display = window_get_display(background->window);
727 opaque = wl_compositor_create_region(display_get_compositor(display));
728 wl_region_add(opaque, allocation.x, allocation.y,
729 allocation.width, allocation.height);
730 wl_surface_set_opaque_region(window_get_wl_surface(background->window), opaque);
731 wl_region_destroy(opaque);
733 background->painted = 1;
734 check_desktop_ready(background->window);
738 background_configure(void *data,
739 struct desktop_shell *desktop_shell,
740 uint32_t edges, struct window *window,
741 int32_t width, int32_t height)
743 struct background *background =
744 (struct background *) window_get_user_data(window);
746 widget_schedule_resize(background->widget, width, height);
750 unlock_dialog_redraw_handler(struct widget *widget, void *data)
752 struct unlock_dialog *dialog = data;
753 struct rectangle allocation;
754 cairo_surface_t *surface;
756 cairo_pattern_t *pat;
759 cr = widget_cairo_create(widget);
761 widget_get_allocation(dialog->widget, &allocation);
762 cairo_rectangle(cr, allocation.x, allocation.y,
763 allocation.width, allocation.height);
764 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
765 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
768 cairo_translate(cr, allocation.x, allocation.y);
769 if (dialog->button_focused)
774 cx = allocation.width / 2.0;
775 cy = allocation.height / 2.0;
776 r = (cx < cy ? cx : cy) * 0.4;
777 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
778 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
779 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
780 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
781 cairo_set_source(cr, pat);
782 cairo_pattern_destroy(pat);
783 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
786 widget_set_allocation(dialog->button,
787 allocation.x + cx - r,
788 allocation.y + cy - r, 2 * r, 2 * r);
792 surface = window_get_surface(dialog->window);
793 cairo_surface_destroy(surface);
797 unlock_dialog_button_handler(struct widget *widget,
798 struct input *input, uint32_t time,
800 enum wl_pointer_button_state state, void *data)
802 struct unlock_dialog *dialog = data;
803 struct desktop *desktop = dialog->desktop;
805 if (button == BTN_LEFT) {
806 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
808 display_defer(desktop->display, &desktop->unlock_task);
815 unlock_dialog_keyboard_focus_handler(struct window *window,
816 struct input *device, void *data)
818 window_schedule_redraw(window);
822 unlock_dialog_widget_enter_handler(struct widget *widget,
824 float x, float y, void *data)
826 struct unlock_dialog *dialog = data;
828 dialog->button_focused = 1;
829 widget_schedule_redraw(widget);
831 return CURSOR_LEFT_PTR;
835 unlock_dialog_widget_leave_handler(struct widget *widget,
836 struct input *input, void *data)
838 struct unlock_dialog *dialog = data;
840 dialog->button_focused = 0;
841 widget_schedule_redraw(widget);
844 static struct unlock_dialog *
845 unlock_dialog_create(struct desktop *desktop)
847 struct display *display = desktop->display;
848 struct unlock_dialog *dialog;
850 dialog = malloc(sizeof *dialog);
853 memset(dialog, 0, sizeof *dialog);
855 dialog->window = window_create_custom(display);
856 dialog->widget = frame_create(dialog->window, dialog);
857 window_set_title(dialog->window, "Unlock your desktop");
859 window_set_user_data(dialog->window, dialog);
860 window_set_keyboard_focus_handler(dialog->window,
861 unlock_dialog_keyboard_focus_handler);
862 dialog->button = widget_add_widget(dialog->widget, dialog);
863 widget_set_redraw_handler(dialog->widget,
864 unlock_dialog_redraw_handler);
865 widget_set_enter_handler(dialog->button,
866 unlock_dialog_widget_enter_handler);
867 widget_set_leave_handler(dialog->button,
868 unlock_dialog_widget_leave_handler);
869 widget_set_button_handler(dialog->button,
870 unlock_dialog_button_handler);
872 desktop_shell_set_lock_surface(desktop->shell,
873 window_get_wl_surface(dialog->window));
875 window_schedule_resize(dialog->window, 260, 230);
881 unlock_dialog_destroy(struct unlock_dialog *dialog)
883 window_destroy(dialog->window);
888 unlock_dialog_finish(struct task *task, uint32_t events)
890 struct desktop *desktop =
891 container_of(task, struct desktop, unlock_task);
893 desktop_shell_unlock(desktop->shell);
894 unlock_dialog_destroy(desktop->unlock_dialog);
895 desktop->unlock_dialog = NULL;
899 desktop_shell_configure(void *data,
900 struct desktop_shell *desktop_shell,
902 struct wl_surface *surface,
903 int32_t width, int32_t height)
905 struct window *window = wl_surface_get_user_data(surface);
906 struct surface *s = window_get_user_data(window);
908 s->configure(data, desktop_shell, edges, window, width, height);
912 desktop_shell_prepare_lock_surface(void *data,
913 struct desktop_shell *desktop_shell)
915 struct desktop *desktop = data;
918 desktop_shell_unlock(desktop->shell);
922 if (!desktop->unlock_dialog) {
923 desktop->unlock_dialog = unlock_dialog_create(desktop);
924 desktop->unlock_dialog->desktop = desktop;
929 desktop_shell_grab_cursor(void *data,
930 struct desktop_shell *desktop_shell,
933 struct desktop *desktop = data;
936 case DESKTOP_SHELL_CURSOR_NONE:
937 desktop->grab_cursor = CURSOR_BLANK;
939 case DESKTOP_SHELL_CURSOR_BUSY:
940 desktop->grab_cursor = CURSOR_WATCH;
942 case DESKTOP_SHELL_CURSOR_MOVE:
943 desktop->grab_cursor = CURSOR_DRAGGING;
945 case DESKTOP_SHELL_CURSOR_RESIZE_TOP:
946 desktop->grab_cursor = CURSOR_TOP;
948 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM:
949 desktop->grab_cursor = CURSOR_BOTTOM;
951 case DESKTOP_SHELL_CURSOR_RESIZE_LEFT:
952 desktop->grab_cursor = CURSOR_LEFT;
954 case DESKTOP_SHELL_CURSOR_RESIZE_RIGHT:
955 desktop->grab_cursor = CURSOR_RIGHT;
957 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_LEFT:
958 desktop->grab_cursor = CURSOR_TOP_LEFT;
960 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_RIGHT:
961 desktop->grab_cursor = CURSOR_TOP_RIGHT;
963 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_LEFT:
964 desktop->grab_cursor = CURSOR_BOTTOM_LEFT;
966 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_RIGHT:
967 desktop->grab_cursor = CURSOR_BOTTOM_RIGHT;
969 case DESKTOP_SHELL_CURSOR_ARROW:
971 desktop->grab_cursor = CURSOR_LEFT_PTR;
975 static const struct desktop_shell_listener listener = {
976 desktop_shell_configure,
977 desktop_shell_prepare_lock_surface,
978 desktop_shell_grab_cursor
982 background_destroy(struct background *background)
984 widget_destroy(background->widget);
985 window_destroy(background->window);
990 static struct background *
991 background_create(struct desktop *desktop)
993 struct background *background;
995 background = malloc(sizeof *background);
996 memset(background, 0, sizeof *background);
998 background->base.configure = background_configure;
999 background->window = window_create_custom(desktop->display);
1000 background->widget = window_add_widget(background->window, background);
1001 window_set_user_data(background->window, background);
1002 widget_set_redraw_handler(background->widget, background_draw);
1008 grab_surface_enter_handler(struct widget *widget, struct input *input,
1009 float x, float y, void *data)
1011 struct desktop *desktop = data;
1013 return desktop->grab_cursor;
1017 grab_surface_destroy(struct desktop *desktop)
1019 widget_destroy(desktop->grab_widget);
1020 window_destroy(desktop->grab_window);
1024 grab_surface_create(struct desktop *desktop)
1026 struct wl_surface *s;
1028 desktop->grab_window = window_create_custom(desktop->display);
1029 window_set_user_data(desktop->grab_window, desktop);
1031 s = window_get_wl_surface(desktop->grab_window);
1032 desktop_shell_set_grab_surface(desktop->shell, s);
1034 desktop->grab_widget =
1035 window_add_widget(desktop->grab_window, desktop);
1036 /* We set the allocation to 1x1 at 0,0 so the fake enter event
1037 * at 0,0 will go to this widget. */
1038 widget_set_allocation(desktop->grab_widget, 0, 0, 1, 1);
1040 widget_set_enter_handler(desktop->grab_widget,
1041 grab_surface_enter_handler);
1045 output_destroy(struct output *output)
1047 background_destroy(output->background);
1048 panel_destroy(output->panel);
1049 wl_output_destroy(output->output);
1050 wl_list_remove(&output->link);
1056 desktop_destroy_outputs(struct desktop *desktop)
1059 struct output *output;
1061 wl_list_for_each_safe(output, tmp, &desktop->outputs, link)
1062 output_destroy(output);
1066 output_handle_geometry(void *data,
1067 struct wl_output *wl_output,
1070 int physical_height,
1076 struct output *output = data;
1078 window_set_buffer_transform(output->panel->window, transform);
1079 window_set_buffer_transform(output->background->window, transform);
1083 output_handle_mode(void *data,
1084 struct wl_output *wl_output,
1093 output_handle_done(void *data,
1094 struct wl_output *wl_output)
1099 output_handle_scale(void *data,
1100 struct wl_output *wl_output,
1103 struct output *output = data;
1105 window_set_buffer_scale(output->panel->window, scale);
1106 window_set_buffer_scale(output->background->window, scale);
1109 static const struct wl_output_listener output_listener = {
1110 output_handle_geometry,
1117 create_output(struct desktop *desktop, uint32_t id)
1119 struct output *output;
1121 output = calloc(1, sizeof *output);
1126 display_bind(desktop->display, id, &wl_output_interface, 2);
1128 wl_output_add_listener(output->output, &output_listener, output);
1130 wl_list_insert(&desktop->outputs, &output->link);
1134 global_handler(struct display *display, uint32_t id,
1135 const char *interface, uint32_t version, void *data)
1137 struct desktop *desktop = data;
1139 if (!strcmp(interface, "desktop_shell")) {
1140 desktop->interface_version = (version < 2) ? version : 2;
1141 desktop->shell = display_bind(desktop->display,
1142 id, &desktop_shell_interface,
1143 desktop->interface_version);
1144 desktop_shell_add_listener(desktop->shell, &listener, desktop);
1145 } else if (!strcmp(interface, "wl_output")) {
1146 create_output(desktop, id);
1151 launcher_section_done(void *data)
1153 struct desktop *desktop = data;
1154 struct output *output;
1156 if (key_launcher_icon == NULL || key_launcher_path == NULL) {
1157 fprintf(stderr, "invalid launcher section\n");
1161 wl_list_for_each(output, &desktop->outputs, link) {
1162 panel_add_launcher(output->panel,
1163 key_launcher_icon, key_launcher_path);
1166 free(key_launcher_icon);
1167 key_launcher_icon = NULL;
1168 free(key_launcher_path);
1169 key_launcher_path = NULL;
1173 add_default_launcher(struct desktop *desktop)
1175 struct output *output;
1177 wl_list_for_each(output, &desktop->outputs, link)
1178 panel_add_launcher(output->panel,
1179 DATADIR "/weston/terminal.png",
1180 BINDIR "/weston-terminal");
1183 int main(int argc, char *argv[])
1185 struct desktop desktop = { 0 };
1187 struct output *output;
1190 desktop.unlock_task.run = unlock_dialog_finish;
1191 wl_list_init(&desktop.outputs);
1193 desktop.display = display_create(&argc, argv);
1194 if (desktop.display == NULL) {
1195 fprintf(stderr, "failed to create display: %m\n");
1199 display_set_user_data(desktop.display, &desktop);
1200 display_set_global_handler(desktop.display, global_handler);
1202 wl_list_for_each(output, &desktop.outputs, link) {
1203 struct wl_surface *surface;
1205 output->panel = panel_create(desktop.display);
1206 surface = window_get_wl_surface(output->panel->window);
1207 desktop_shell_set_panel(desktop.shell,
1208 output->output, surface);
1210 output->background = background_create(&desktop);
1211 surface = window_get_wl_surface(output->background->window);
1212 desktop_shell_set_background(desktop.shell,
1213 output->output, surface);
1216 grab_surface_create(&desktop);
1218 config_fd = open_config_file("weston.ini");
1219 ret = parse_config_file(config_fd,
1220 config_sections, ARRAY_LENGTH(config_sections),
1224 add_default_launcher(&desktop);
1226 signal(SIGCHLD, sigchild_handler);
1228 display_run(desktop.display);
1231 grab_surface_destroy(&desktop);
1232 desktop_destroy_outputs(&desktop);
1233 if (desktop.unlock_dialog)
1234 unlock_dialog_destroy(desktop.unlock_dialog);
1235 desktop_shell_destroy(desktop.shell);
1236 display_destroy(desktop.display);