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 <linux/input.h>
35 #include <wayland-client.h>
36 #include "cairo-util.h"
38 #include "../shared/config-parser.h"
40 #include "desktop-shell-client-protocol.h"
43 struct display *display;
44 struct desktop_shell *shell;
45 struct unlock_dialog *unlock_dialog;
46 struct task unlock_task;
47 struct wl_list outputs;
51 void (*configure)(void *data,
52 struct desktop_shell *desktop_shell,
53 uint32_t time, uint32_t edges,
54 struct window *window,
55 int32_t width, int32_t height);
60 struct window *window;
61 struct widget *widget;
62 struct wl_list launcher_list;
67 struct window *window;
68 struct widget *widget;
72 struct wl_output *output;
76 struct background *background;
79 struct panel_launcher {
80 struct widget *widget;
82 cairo_surface_t *icon;
88 struct unlock_dialog {
89 struct window *window;
90 struct widget *widget;
91 struct widget *button;
95 struct desktop *desktop;
98 static char *key_background_image = DATADIR "/weston/pattern.png";
99 static char *key_background_type = "tile";
100 static uint32_t key_panel_color = 0xaa000000;
101 static uint32_t key_background_color = 0xff002244;
102 static char *key_launcher_icon;
103 static char *key_launcher_path;
104 static void launcher_section_done(void *data);
105 static int key_locking = 1;
107 static const struct config_key shell_config_keys[] = {
108 { "background-image", CONFIG_KEY_STRING, &key_background_image },
109 { "background-type", CONFIG_KEY_STRING, &key_background_type },
110 { "panel-color", CONFIG_KEY_UNSIGNED_INTEGER, &key_panel_color },
111 { "background-color", CONFIG_KEY_UNSIGNED_INTEGER, &key_background_color },
112 { "locking", CONFIG_KEY_BOOLEAN, &key_locking },
115 static const struct config_key launcher_config_keys[] = {
116 { "icon", CONFIG_KEY_STRING, &key_launcher_icon },
117 { "path", CONFIG_KEY_STRING, &key_launcher_path },
120 static const struct config_section config_sections[] = {
122 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
124 launcher_config_keys, ARRAY_LENGTH(launcher_config_keys),
125 launcher_section_done }
129 sigchild_handler(int s)
134 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
135 fprintf(stderr, "child %d exited\n", pid);
139 menu_func(struct window *window, int index, void *data)
141 printf("Selected index %d from a panel menu.\n", index);
145 show_menu(struct panel *panel, struct input *input, uint32_t time)
148 static const char *entries[] = {
149 "Roy", "Pris", "Leon", "Zhora"
152 input_get_position(input, &x, &y);
153 window_show_menu(window_get_display(panel->window),
154 input, time, panel->window,
155 x - 10, y - 10, menu_func, entries, 4);
159 panel_launcher_activate(struct panel_launcher *widget)
165 fprintf(stderr, "fork failed: %m\n");
172 if (execl(widget->path, widget->path, NULL) < 0) {
173 fprintf(stderr, "execl '%s' failed: %m\n", widget->path);
179 panel_launcher_redraw_handler(struct widget *widget, void *data)
181 struct panel_launcher *launcher = data;
182 cairo_surface_t *surface;
183 struct rectangle allocation;
186 surface = window_get_surface(launcher->panel->window);
187 cr = cairo_create(surface);
189 widget_get_allocation(widget, &allocation);
190 if (launcher->pressed) {
195 cairo_set_source_surface(cr, launcher->icon,
196 allocation.x, allocation.y);
199 if (launcher->focused) {
200 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
201 cairo_mask_surface(cr, launcher->icon,
202 allocation.x, allocation.y);
209 set_hex_color(cairo_t *cr, uint32_t color)
211 cairo_set_source_rgba(cr,
212 ((color >> 16) & 0xff) / 255.0,
213 ((color >> 8) & 0xff) / 255.0,
214 ((color >> 0) & 0xff) / 255.0,
215 ((color >> 24) & 0xff) / 255.0);
219 panel_redraw_handler(struct widget *widget, void *data)
221 cairo_surface_t *surface;
223 struct panel *panel = data;
225 surface = window_get_surface(panel->window);
226 cr = cairo_create(surface);
227 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
228 set_hex_color(cr, key_panel_color);
232 cairo_surface_destroy(surface);
236 panel_launcher_enter_handler(struct widget *widget, struct input *input,
237 uint32_t time, int32_t x, int32_t y, void *data)
239 struct panel_launcher *launcher = data;
241 launcher->focused = 1;
242 widget_schedule_redraw(widget);
244 return POINTER_LEFT_PTR;
248 panel_launcher_leave_handler(struct widget *widget,
249 struct input *input, void *data)
251 struct panel_launcher *launcher = data;
253 launcher->focused = 0;
254 widget_schedule_redraw(widget);
258 panel_launcher_button_handler(struct widget *widget,
259 struct input *input, uint32_t time,
260 int button, int state, void *data)
262 struct panel_launcher *launcher;
264 launcher = widget_get_user_data(widget);
265 widget_schedule_redraw(widget);
267 panel_launcher_activate(launcher);
271 panel_button_handler(struct widget *widget,
272 struct input *input, uint32_t time,
273 int button, int state, void *data)
275 struct panel *panel = data;
277 if (button == BTN_RIGHT && state)
278 show_menu(panel, input, time);
282 panel_resize_handler(struct widget *widget,
283 int32_t width, int32_t height, void *data)
285 struct panel_launcher *launcher;
286 struct panel *panel = data;
291 wl_list_for_each(launcher, &panel->launcher_list, link) {
292 w = cairo_image_surface_get_width(launcher->icon);
293 h = cairo_image_surface_get_height(launcher->icon);
294 widget_set_allocation(launcher->widget,
295 x, y - h / 2, w + 1, h + 1);
301 panel_configure(void *data,
302 struct desktop_shell *desktop_shell,
303 uint32_t time, uint32_t edges,
304 struct window *window,
305 int32_t width, int32_t height)
307 struct surface *surface = window_get_user_data(window);
308 struct panel *panel = container_of(surface, struct panel, base);
310 window_schedule_resize(panel->window, width, 32);
313 static struct panel *
314 panel_create(struct display *display)
318 panel = malloc(sizeof *panel);
319 memset(panel, 0, sizeof *panel);
321 panel->base.configure = panel_configure;
322 panel->window = window_create(display);
323 panel->widget = window_add_widget(panel->window, panel);
324 wl_list_init(&panel->launcher_list);
326 window_set_title(panel->window, "panel");
327 window_set_custom(panel->window);
328 window_set_user_data(panel->window, panel);
330 widget_set_redraw_handler(panel->widget, panel_redraw_handler);
331 widget_set_resize_handler(panel->widget, panel_resize_handler);
332 widget_set_button_handler(panel->widget, panel_button_handler);
338 panel_add_launcher(struct panel *panel, const char *icon, const char *path)
340 struct panel_launcher *launcher;
342 launcher = malloc(sizeof *launcher);
343 memset(launcher, 0, sizeof *launcher);
344 launcher->icon = cairo_image_surface_create_from_png(icon);
345 launcher->path = strdup(path);
346 launcher->panel = panel;
347 wl_list_insert(panel->launcher_list.prev, &launcher->link);
349 launcher->widget = widget_add_widget(panel->widget, launcher);
350 widget_set_enter_handler(launcher->widget,
351 panel_launcher_enter_handler);
352 widget_set_leave_handler(launcher->widget,
353 panel_launcher_leave_handler);
354 widget_set_button_handler(launcher->widget,
355 panel_launcher_button_handler);
356 widget_set_redraw_handler(launcher->widget,
357 panel_launcher_redraw_handler);
366 background_draw(struct widget *widget, void *data)
368 struct background *background = data;
369 cairo_surface_t *surface, *image;
370 cairo_pattern_t *pattern;
371 cairo_matrix_t matrix;
374 struct rectangle allocation;
377 surface = window_get_surface(background->window);
379 cr = cairo_create(surface);
380 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
381 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
384 widget_get_allocation(widget, &allocation);
386 if (key_background_image)
387 image = load_image(key_background_image);
389 if (strcmp(key_background_type, "scale") == 0)
390 type = BACKGROUND_SCALE;
391 else if (strcmp(key_background_type, "tile") == 0)
392 type = BACKGROUND_TILE;
394 fprintf(stderr, "invalid background-type: %s\n",
395 key_background_type);
397 if (image && type != -1) {
398 pattern = cairo_pattern_create_for_surface(image);
400 case BACKGROUND_SCALE:
401 sx = (double) cairo_image_surface_get_width(image) /
403 sy = (double) cairo_image_surface_get_height(image) /
405 cairo_matrix_init_scale(&matrix, sx, sy);
406 cairo_pattern_set_matrix(pattern, &matrix);
408 case BACKGROUND_TILE:
409 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
412 cairo_set_source(cr, pattern);
413 cairo_pattern_destroy (pattern);
414 cairo_surface_destroy(image);
416 set_hex_color(cr, key_background_color);
421 cairo_surface_destroy(surface);
425 background_configure(void *data,
426 struct desktop_shell *desktop_shell,
427 uint32_t time, uint32_t edges,
428 struct window *window,
429 int32_t width, int32_t height)
431 struct background *background =
432 (struct background *) window_get_user_data(window);
434 widget_schedule_resize(background->widget, width, height);
438 unlock_dialog_redraw_handler(struct widget *widget, void *data)
440 struct unlock_dialog *dialog = data;
441 struct rectangle allocation;
443 cairo_surface_t *surface;
444 cairo_pattern_t *pat;
447 surface = window_get_surface(dialog->window);
448 cr = cairo_create(surface);
449 widget_get_allocation(dialog->widget, &allocation);
450 cairo_rectangle(cr, allocation.x, allocation.y,
451 allocation.width, allocation.height);
453 cairo_push_group(cr);
454 cairo_translate(cr, allocation.x, allocation.y);
456 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
457 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
460 if (dialog->button_focused)
465 cx = allocation.width / 2.0;
466 cy = allocation.height / 2.0;
467 r = (cx < cy ? cx : cy) * 0.4;
468 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
469 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
470 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
471 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
472 cairo_set_source(cr, pat);
473 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
476 widget_set_allocation(dialog->button,
477 allocation.x + cx - r,
478 allocation.y + cy - r, 2 * r, 2 * r);
479 cairo_pattern_destroy(pat);
481 cairo_pop_group_to_source(cr);
485 cairo_surface_destroy(surface);
489 unlock_dialog_button_handler(struct widget *widget,
490 struct input *input, uint32_t time,
491 int button, int state, void *data)
493 struct unlock_dialog *dialog = data;
494 struct desktop *desktop = dialog->desktop;
496 if (button == BTN_LEFT) {
497 if (state == 0 && !dialog->closing) {
498 display_defer(desktop->display, &desktop->unlock_task);
505 unlock_dialog_keyboard_focus_handler(struct window *window,
506 struct input *device, void *data)
508 window_schedule_redraw(window);
512 unlock_dialog_widget_enter_handler(struct widget *widget,
513 struct input *input, uint32_t time,
514 int32_t x, int32_t y, void *data)
516 struct unlock_dialog *dialog = data;
518 dialog->button_focused = 1;
519 widget_schedule_redraw(widget);
521 return POINTER_LEFT_PTR;
525 unlock_dialog_widget_leave_handler(struct widget *widget,
526 struct input *input, void *data)
528 struct unlock_dialog *dialog = data;
530 dialog->button_focused = 0;
531 widget_schedule_redraw(widget);
534 static struct unlock_dialog *
535 unlock_dialog_create(struct desktop *desktop)
537 struct display *display = desktop->display;
538 struct unlock_dialog *dialog;
540 dialog = malloc(sizeof *dialog);
543 memset(dialog, 0, sizeof *dialog);
545 dialog->window = window_create(display);
546 dialog->widget = frame_create(dialog->window, dialog);
547 window_set_title(dialog->window, "Unlock your desktop");
548 window_set_custom(dialog->window);
550 window_set_user_data(dialog->window, dialog);
551 window_set_keyboard_focus_handler(dialog->window,
552 unlock_dialog_keyboard_focus_handler);
553 dialog->button = widget_add_widget(dialog->widget, dialog);
554 widget_set_redraw_handler(dialog->widget,
555 unlock_dialog_redraw_handler);
556 widget_set_enter_handler(dialog->button,
557 unlock_dialog_widget_enter_handler);
558 widget_set_leave_handler(dialog->button,
559 unlock_dialog_widget_leave_handler);
560 widget_set_button_handler(dialog->button,
561 unlock_dialog_button_handler);
563 desktop_shell_set_lock_surface(desktop->shell,
564 window_get_wl_shell_surface(dialog->window));
566 window_schedule_resize(dialog->window, 260, 230);
572 unlock_dialog_destroy(struct unlock_dialog *dialog)
574 window_destroy(dialog->window);
579 unlock_dialog_finish(struct task *task, uint32_t events)
581 struct desktop *desktop =
582 container_of(task, struct desktop, unlock_task);
584 desktop_shell_unlock(desktop->shell);
585 unlock_dialog_destroy(desktop->unlock_dialog);
586 desktop->unlock_dialog = NULL;
590 desktop_shell_configure(void *data,
591 struct desktop_shell *desktop_shell,
592 uint32_t time, uint32_t edges,
593 struct wl_shell_surface *shell_surface,
594 int32_t width, int32_t height)
596 struct window *window = wl_shell_surface_get_user_data(shell_surface);
597 struct surface *s = window_get_user_data(window);
599 s->configure(data, desktop_shell, time, edges, window, width, height);
603 desktop_shell_prepare_lock_surface(void *data,
604 struct desktop_shell *desktop_shell)
606 struct desktop *desktop = data;
609 desktop_shell_unlock(desktop->shell);
613 if (!desktop->unlock_dialog) {
614 desktop->unlock_dialog = unlock_dialog_create(desktop);
615 desktop->unlock_dialog->desktop = desktop;
619 static const struct desktop_shell_listener listener = {
620 desktop_shell_configure,
621 desktop_shell_prepare_lock_surface
624 static struct background *
625 background_create(struct desktop *desktop)
627 struct background *background;
629 background = malloc(sizeof *background);
630 memset(background, 0, sizeof *background);
632 background->base.configure = background_configure;
633 background->window = window_create(desktop->display);
634 background->widget = window_add_widget(background->window, background);
635 window_set_custom(background->window);
636 window_set_user_data(background->window, background);
637 widget_set_redraw_handler(background->widget, background_draw);
643 create_output(struct desktop *desktop, uint32_t id)
645 struct output *output;
647 output = calloc(1, sizeof *output);
651 output->output = wl_display_bind(display_get_display(desktop->display),
652 id, &wl_output_interface);
654 wl_list_insert(&desktop->outputs, &output->link);
658 global_handler(struct wl_display *display, uint32_t id,
659 const char *interface, uint32_t version, void *data)
661 struct desktop *desktop = data;
663 if (!strcmp(interface, "desktop_shell")) {
665 wl_display_bind(display, id, &desktop_shell_interface);
666 desktop_shell_add_listener(desktop->shell, &listener, desktop);
667 } else if (!strcmp(interface, "wl_output")) {
668 create_output(desktop, id);
673 launcher_section_done(void *data)
675 struct desktop *desktop = data;
676 struct output *output;
678 if (key_launcher_icon == NULL || key_launcher_path == NULL) {
679 fprintf(stderr, "invalid launcher section\n");
683 wl_list_for_each(output, &desktop->outputs, link)
684 panel_add_launcher(output->panel,
685 key_launcher_icon, key_launcher_path);
687 free(key_launcher_icon);
688 key_launcher_icon = NULL;
689 free(key_launcher_path);
690 key_launcher_path = NULL;
694 add_default_launcher(struct desktop *desktop)
696 struct output *output;
698 wl_list_for_each(output, &desktop->outputs, link)
699 panel_add_launcher(output->panel,
700 DATADIR "/weston/terminal.png",
701 "/usr/bin/weston-terminal");
704 int main(int argc, char *argv[])
706 struct desktop desktop = { 0 };
708 struct output *output;
711 desktop.unlock_task.run = unlock_dialog_finish;
712 wl_list_init(&desktop.outputs);
714 desktop.display = display_create(&argc, &argv, NULL);
715 if (desktop.display == NULL) {
716 fprintf(stderr, "failed to create display: %m\n");
720 wl_display_add_global_listener(display_get_display(desktop.display),
721 global_handler, &desktop);
723 wl_list_for_each(output, &desktop.outputs, link) {
724 struct wl_shell_surface *s;
726 output->panel = panel_create(desktop.display);
727 s = window_get_wl_shell_surface(output->panel->window);
728 desktop_shell_set_panel(desktop.shell, output->output, s);
730 output->background = background_create(&desktop);
731 s = window_get_wl_shell_surface(output->background->window);
732 desktop_shell_set_background(desktop.shell, output->output, s);
735 config_file = config_file_path("weston-desktop-shell.ini");
736 ret = parse_config_file(config_file,
737 config_sections, ARRAY_LENGTH(config_sections),
741 add_default_launcher(&desktop);
743 signal(SIGCHLD, sigchild_handler);
745 display_run(desktop.display);