blob: 0f336f5e4ebf28d2060f6ce002f1f89e4684de79 [file] [log] [blame]
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001/*
2 * Copyright © 2011 Kristian Høgsberg
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02003 * Copyright © 2011 Collabora, Ltd.
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04004 *
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.
14 *
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
21 * OF THIS SOFTWARE.
22 */
23
24#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <fcntl.h>
29#include <unistd.h>
30#include <math.h>
31#include <cairo.h>
32#include <sys/wait.h>
33#include <linux/input.h>
34
Pekka Paalanen50719bc2011-11-22 14:18:50 +020035#include <wayland-client.h>
Kristian Høgsberg27d38662011-10-20 13:11:12 -040036#include "cairo-util.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040037#include "window.h"
Kristian Høgsberg9b935c82011-12-08 12:44:27 -050038#include "../shared/config-parser.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040039
Pekka Paalanen50719bc2011-11-22 14:18:50 +020040#include "desktop-shell-client-protocol.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040041
42struct desktop {
43 struct display *display;
44 struct desktop_shell *shell;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +020045 struct unlock_dialog *unlock_dialog;
46 struct task unlock_task;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010047 struct wl_list outputs;
48};
49
50struct surface {
51 void (*configure)(void *data,
52 struct desktop_shell *desktop_shell,
53 uint32_t time, uint32_t edges,
Pekka Paalanen068ae942011-11-28 14:11:15 +020054 struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010055 int32_t width, int32_t height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040056};
57
58struct panel {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010059 struct surface base;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040060 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050061 struct widget *widget;
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -040062 struct window *menu;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050063 struct wl_list launcher_list;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040064};
65
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010066struct background {
67 struct surface base;
68 struct window *window;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050069 struct widget *widget;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010070};
71
72struct output {
73 struct wl_output *output;
74 struct wl_list link;
75
76 struct panel *panel;
77 struct background *background;
78};
79
Kristian Høgsberg53880802012-01-09 11:16:50 -050080struct panel_launcher {
Kristian Høgsbergc51f7992012-01-08 15:09:53 -050081 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -040082 struct panel *panel;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040083 cairo_surface_t *icon;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040084 int pressed;
85 const char *path;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050086 struct wl_list link;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040087};
88
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +020089struct unlock_dialog {
90 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050091 struct widget *widget;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -050092 struct widget *button;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +020093 int closing;
94
95 struct desktop *desktop;
96};
97
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -050098static char *key_background_image;
99static uint32_t key_panel_color;
100static char *key_launcher_icon;
101static char *key_launcher_path;
102static void launcher_section_done(void *data);
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200103static int key_locking = 1;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500104
105static const struct config_key shell_config_keys[] = {
106 { "background-image", CONFIG_KEY_STRING, &key_background_image },
107 { "panel-color", CONFIG_KEY_INTEGER, &key_panel_color },
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200108 { "locking", CONFIG_KEY_BOOLEAN, &key_locking },
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500109};
110
111static const struct config_key launcher_config_keys[] = {
112 { "icon", CONFIG_KEY_STRING, &key_launcher_icon },
113 { "path", CONFIG_KEY_STRING, &key_launcher_path },
114};
115
116static const struct config_section config_sections[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500117 { "desktop-shell",
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500118 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
119 { "launcher",
120 launcher_config_keys, ARRAY_LENGTH(launcher_config_keys),
121 launcher_section_done }
122};
123
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400124static void
125sigchild_handler(int s)
126{
127 int status;
128 pid_t pid;
129
130 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
131 fprintf(stderr, "child %d exited\n", pid);
132}
133
134static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500135show_menu(struct panel *panel, struct input *input, uint32_t time)
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400136{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500137 int32_t x, y;
138 static const char *entries[] = {
139 "Roy", "Pris", "Leon", "Zhora"
140 };
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400141
142 input_get_position(input, &x, &y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500143 panel->menu = window_create_menu(window_get_display(panel->window),
144 input, time, panel->window,
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500145 x - 10, y - 10, NULL, entries, 4);
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400146
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500147 window_schedule_redraw(panel->menu);
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400148}
149
150static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500151panel_launcher_activate(struct panel_launcher *widget)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400152{
153 pid_t pid;
154
155 pid = fork();
156 if (pid < 0) {
157 fprintf(stderr, "fork failed: %m\n");
158 return;
159 }
160
161 if (pid)
162 return;
Benjamin Franzked7759712011-11-22 12:38:48 +0100163
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500164 if (execl(widget->path, widget->path, NULL) < 0) {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400165 fprintf(stderr, "execl failed: %m\n");
166 exit(1);
167 }
168}
169
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400170static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500171panel_launcher_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400172{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500173 struct panel_launcher *launcher = data;
174 cairo_surface_t *surface;
175 struct rectangle allocation;
176 cairo_t *cr;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400177
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500178 surface = window_get_surface(launcher->panel->window);
179 cr = cairo_create(surface);
180
181 widget_get_allocation(widget, &allocation);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500182 if (launcher->pressed) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500183 allocation.x++;
184 allocation.y++;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400185 }
186
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500187 cairo_set_source_surface(cr, launcher->icon,
188 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400189 cairo_paint(cr);
190
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500191 if (window_get_focus_widget(launcher->panel->window) ==
192 launcher->widget) {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400193 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500194 cairo_mask_surface(cr, launcher->icon,
195 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400196 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400197
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500198 cairo_destroy(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400199}
200
201static void
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500202set_hex_color(cairo_t *cr, uint32_t color)
203{
204 cairo_set_source_rgba(cr,
205 ((color >> 16) & 0xff) / 255.0,
206 ((color >> 8) & 0xff) / 255.0,
207 ((color >> 0) & 0xff) / 255.0,
208 ((color >> 24) & 0xff) / 255.0);
209}
210
211static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500212panel_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400213{
214 cairo_surface_t *surface;
215 cairo_t *cr;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500216 struct panel *panel = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400217
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500218 surface = window_get_surface(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400219 cr = cairo_create(surface);
220 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500221 set_hex_color(cr, key_panel_color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400222 cairo_paint(cr);
223
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400224 cairo_destroy(cr);
225 cairo_surface_destroy(surface);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400226}
227
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500228static int
Kristian Høgsberg53880802012-01-09 11:16:50 -0500229panel_launcher_enter_handler(struct widget *widget, struct input *input,
230 uint32_t time, int32_t x, int32_t y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500231{
232 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500233
234 return POINTER_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500235}
236
237static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500238panel_launcher_leave_handler(struct widget *widget,
239 struct input *input, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400240{
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500241 widget_schedule_redraw(widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400242}
243
244static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500245panel_launcher_button_handler(struct widget *widget,
246 struct input *input, uint32_t time,
247 int button, int state, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400248{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500249 struct panel_launcher *launcher;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400250
Kristian Høgsberg53880802012-01-09 11:16:50 -0500251 launcher = widget_get_user_data(widget);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500252 widget_schedule_redraw(widget);
253 if (state == 0)
Kristian Høgsberg53880802012-01-09 11:16:50 -0500254 panel_launcher_activate(launcher);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500255}
256
257static void
258panel_button_handler(struct widget *widget,
259 struct input *input, uint32_t time,
260 int button, int state, void *data)
261{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500262 struct panel *panel = data;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500263
264 if (button == BTN_RIGHT && state)
265 show_menu(panel, input, time);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400266}
267
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100268static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500269panel_resize_handler(struct widget *widget,
270 int32_t width, int32_t height, void *data)
271{
272 struct panel_launcher *launcher;
273 struct panel *panel = data;
274 int x, y, w, h;
275
276 x = 10;
277 y = 16;
278 wl_list_for_each(launcher, &panel->launcher_list, link) {
279 w = cairo_image_surface_get_width(launcher->icon);
280 h = cairo_image_surface_get_height(launcher->icon);
281 widget_set_allocation(launcher->widget,
282 x, y - h / 2, w + 1, h + 1);
283 x += w + 10;
284 }
285}
286
287static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100288panel_configure(void *data,
289 struct desktop_shell *desktop_shell,
290 uint32_t time, uint32_t edges,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200291 struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100292 int32_t width, int32_t height)
293{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500294 struct surface *surface = window_get_user_data(window);
295 struct panel *panel = container_of(surface, struct panel, base);
296
297 window_schedule_resize(panel->window, width, 32);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100298}
299
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400300static struct panel *
301panel_create(struct display *display)
302{
303 struct panel *panel;
304
305 panel = malloc(sizeof *panel);
306 memset(panel, 0, sizeof *panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400307
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100308 panel->base.configure = panel_configure;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400309 panel->window = window_create(display, 0, 0);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500310 panel->widget = window_add_widget(panel->window, panel);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500311 wl_list_init(&panel->launcher_list);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400312
313 window_set_title(panel->window, "panel");
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400314 window_set_custom(panel->window);
315 window_set_user_data(panel->window, panel);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500316
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500317 widget_set_redraw_handler(panel->widget, panel_redraw_handler);
318 widget_set_resize_handler(panel->widget, panel_resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500319 widget_set_button_handler(panel->widget, panel_button_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400320
321 return panel;
322}
323
324static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500325panel_add_launcher(struct panel *panel, const char *icon, const char *path)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400326{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500327 struct panel_launcher *launcher;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400328
Kristian Høgsberg53880802012-01-09 11:16:50 -0500329 launcher = malloc(sizeof *launcher);
330 memset(launcher, 0, sizeof *launcher);
331 launcher->icon = cairo_image_surface_create_from_png(icon);
332 launcher->path = strdup(path);
333 launcher->panel = panel;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500334 wl_list_insert(panel->launcher_list.prev, &launcher->link);
335
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500336 launcher->widget = widget_add_widget(panel->widget, launcher);
Kristian Høgsberg53880802012-01-09 11:16:50 -0500337 widget_set_enter_handler(launcher->widget,
338 panel_launcher_enter_handler);
339 widget_set_leave_handler(launcher->widget,
340 panel_launcher_leave_handler);
341 widget_set_button_handler(launcher->widget,
342 panel_launcher_button_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500343 widget_set_redraw_handler(launcher->widget,
344 panel_launcher_redraw_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400345}
346
347static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500348background_draw(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400349{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500350 struct background *background = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400351 cairo_surface_t *surface, *image;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400352 cairo_pattern_t *pattern;
353 cairo_matrix_t matrix;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400354 cairo_t *cr;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400355 double sx, sy;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500356 struct rectangle allocation;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400357
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500358 surface = window_get_surface(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400359
360 cr = cairo_create(surface);
361 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
362 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
363 cairo_paint(cr);
364
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500365 widget_get_allocation(widget, &allocation);
366 if (key_background_image) {
367 image = load_jpeg(key_background_image);
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400368 pattern = cairo_pattern_create_for_surface(image);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500369 sx = (double) cairo_image_surface_get_width(image) /
370 allocation.width;
371 sy = (double) cairo_image_surface_get_height(image) /
372 allocation.height;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400373 cairo_matrix_init_scale(&matrix, sx, sy);
374 cairo_pattern_set_matrix(pattern, &matrix);
375 cairo_set_source(cr, pattern);
376 cairo_pattern_destroy (pattern);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400377 cairo_paint(cr);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400378 cairo_surface_destroy(image);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400379 }
380
381 cairo_destroy(cr);
382 cairo_surface_destroy(surface);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400383}
384
385static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100386background_configure(void *data,
387 struct desktop_shell *desktop_shell,
388 uint32_t time, uint32_t edges,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200389 struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100390 int32_t width, int32_t height)
391{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500392 struct background *background =
393 (struct background *) window_get_user_data(window);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100394
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500395 widget_schedule_resize(background->widget, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100396}
397
398static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500399unlock_dialog_redraw_handler(struct widget *widget, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200400{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500401 struct unlock_dialog *dialog = data;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200402 struct rectangle allocation;
403 cairo_t *cr;
404 cairo_surface_t *surface;
405 cairo_pattern_t *pat;
406 double cx, cy, r, f;
407
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200408 surface = window_get_surface(dialog->window);
409 cr = cairo_create(surface);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500410 widget_get_allocation(dialog->widget, &allocation);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200411 cairo_rectangle(cr, allocation.x, allocation.y,
412 allocation.width, allocation.height);
413 cairo_clip(cr);
414 cairo_push_group(cr);
415 cairo_translate(cr, allocation.x, allocation.y);
416
417 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
418 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
419 cairo_paint(cr);
420
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500421 if (window_get_focus_widget(dialog->window) == dialog->button)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200422 f = 1.0;
423 else
424 f = 0.7;
425
426 cx = allocation.width / 2.0;
427 cy = allocation.height / 2.0;
428 r = (cx < cy ? cx : cy) * 0.4;
429 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
430 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
431 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
432 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
433 cairo_set_source(cr, pat);
434 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
435 cairo_fill(cr);
436
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500437 widget_set_allocation(dialog->button,
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200438 allocation.x + cx - r,
439 allocation.y + cy - r, 2 * r, 2 * r);
440 cairo_pattern_destroy(pat);
441
442 cairo_pop_group_to_source(cr);
443 cairo_paint(cr);
444 cairo_destroy(cr);
445
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200446 cairo_surface_destroy(surface);
447}
448
449static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500450unlock_dialog_button_handler(struct widget *widget,
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200451 struct input *input, uint32_t time,
452 int button, int state, void *data)
453{
454 struct unlock_dialog *dialog = data;
455 struct desktop *desktop = dialog->desktop;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200456
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500457 if (button == BTN_LEFT) {
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200458 if (state == 0 && !dialog->closing) {
459 display_defer(desktop->display, &desktop->unlock_task);
460 dialog->closing = 1;
461 }
462 }
463}
464
465static void
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200466unlock_dialog_keyboard_focus_handler(struct window *window,
467 struct input *device, void *data)
468{
469 window_schedule_redraw(window);
470}
471
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500472static int
Kristian Høgsbergee143232012-01-09 08:42:24 -0500473unlock_dialog_widget_enter_handler(struct widget *widget,
474 struct input *input, uint32_t time,
475 int32_t x, int32_t y, void *data)
476{
477 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500478
479 return POINTER_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500480}
481
482static void
483unlock_dialog_widget_leave_handler(struct widget *widget,
484 struct input *input, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200485{
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500486 widget_schedule_redraw(widget);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200487}
488
489static struct unlock_dialog *
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500490unlock_dialog_create(struct desktop *desktop)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200491{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500492 struct display *display = desktop->display;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200493 struct unlock_dialog *dialog;
494
495 dialog = malloc(sizeof *dialog);
496 if (!dialog)
497 return NULL;
498 memset(dialog, 0, sizeof *dialog);
499
500 dialog->window = window_create(display, 260, 230);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500501 dialog->widget = frame_create(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200502 window_set_title(dialog->window, "Unlock your desktop");
Benjamin Franzke8193bc12011-11-23 19:35:07 +0100503 window_set_custom(dialog->window);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200504
505 window_set_user_data(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200506 window_set_keyboard_focus_handler(dialog->window,
507 unlock_dialog_keyboard_focus_handler);
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500508 dialog->button = widget_add_widget(dialog->widget, dialog);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500509 widget_set_redraw_handler(dialog->widget,
510 unlock_dialog_redraw_handler);
Kristian Høgsbergee143232012-01-09 08:42:24 -0500511 widget_set_enter_handler(dialog->button,
512 unlock_dialog_widget_enter_handler);
513 widget_set_leave_handler(dialog->button,
514 unlock_dialog_widget_leave_handler);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500515 widget_set_button_handler(dialog->button,
516 unlock_dialog_button_handler);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200517
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500518 desktop_shell_set_lock_surface(desktop->shell,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200519 window_get_wl_shell_surface(dialog->window));
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500520
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500521 window_schedule_redraw(dialog->window);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200522
523 return dialog;
524}
525
526static void
527unlock_dialog_destroy(struct unlock_dialog *dialog)
528{
529 window_destroy(dialog->window);
530 free(dialog);
531}
532
533static void
534unlock_dialog_finish(struct task *task, uint32_t events)
535{
536 struct desktop *desktop =
Benjamin Franzked7759712011-11-22 12:38:48 +0100537 container_of(task, struct desktop, unlock_task);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200538
539 desktop_shell_unlock(desktop->shell);
540 unlock_dialog_destroy(desktop->unlock_dialog);
541 desktop->unlock_dialog = NULL;
542}
543
544static void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400545desktop_shell_configure(void *data,
546 struct desktop_shell *desktop_shell,
547 uint32_t time, uint32_t edges,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200548 struct wl_shell_surface *shell_surface,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400549 int32_t width, int32_t height)
550{
Pekka Paalanen068ae942011-11-28 14:11:15 +0200551 struct window *window = wl_shell_surface_get_user_data(shell_surface);
552 struct surface *s = window_get_user_data(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400553
Pekka Paalanen068ae942011-11-28 14:11:15 +0200554 s->configure(data, desktop_shell, time, edges, window, width, height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400555}
556
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200557static void
558desktop_shell_prepare_lock_surface(void *data,
559 struct desktop_shell *desktop_shell)
560{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200561 struct desktop *desktop = data;
562
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200563 if (!key_locking) {
564 desktop_shell_unlock(desktop->shell);
565 return;
566 }
567
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200568 if (!desktop->unlock_dialog) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500569 desktop->unlock_dialog = unlock_dialog_create(desktop);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200570 desktop->unlock_dialog->desktop = desktop;
571 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200572}
573
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400574static const struct desktop_shell_listener listener = {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200575 desktop_shell_configure,
576 desktop_shell_prepare_lock_surface
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400577};
578
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100579static struct background *
580background_create(struct desktop *desktop)
581{
582 struct background *background;
583
584 background = malloc(sizeof *background);
585 memset(background, 0, sizeof *background);
586
587 background->base.configure = background_configure;
588 background->window = window_create(desktop->display, 0, 0);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500589 background->widget = window_add_widget(background->window, background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100590 window_set_custom(background->window);
591 window_set_user_data(background->window, background);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500592 widget_set_redraw_handler(background->widget, background_draw);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100593
594 return background;
595}
596
597static void
598create_output(struct desktop *desktop, uint32_t id)
599{
600 struct output *output;
601
602 output = calloc(1, sizeof *output);
603 if (!output)
604 return;
605
606 output->output = wl_display_bind(display_get_display(desktop->display),
607 id, &wl_output_interface);
608
609 wl_list_insert(&desktop->outputs, &output->link);
610}
611
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400612static void
613global_handler(struct wl_display *display, uint32_t id,
614 const char *interface, uint32_t version, void *data)
615{
616 struct desktop *desktop = data;
617
618 if (!strcmp(interface, "desktop_shell")) {
619 desktop->shell =
620 wl_display_bind(display, id, &desktop_shell_interface);
621 desktop_shell_add_listener(desktop->shell, &listener, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100622 } else if (!strcmp(interface, "wl_output")) {
623 create_output(desktop, id);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400624 }
625}
626
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500627static void
628launcher_section_done(void *data)
629{
630 struct desktop *desktop = data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100631 struct output *output;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500632
633 if (key_launcher_icon == NULL || key_launcher_path == NULL) {
634 fprintf(stderr, "invalid launcher section\n");
635 return;
636 }
637
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100638 wl_list_for_each(output, &desktop->outputs, link)
Kristian Høgsberg53880802012-01-09 11:16:50 -0500639 panel_add_launcher(output->panel,
640 key_launcher_icon, key_launcher_path);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100641
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500642 free(key_launcher_icon);
643 key_launcher_icon = NULL;
644 free(key_launcher_path);
645 key_launcher_path = NULL;
646}
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400647
648int main(int argc, char *argv[])
649{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200650 struct desktop desktop = { 0 };
Pekka Paalanen668dd562011-11-15 11:45:40 +0200651 char *config_file;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100652 struct output *output;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400653
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200654 desktop.unlock_task.run = unlock_dialog_finish;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100655 wl_list_init(&desktop.outputs);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200656
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400657 desktop.display = display_create(&argc, &argv, NULL);
658 if (desktop.display == NULL) {
659 fprintf(stderr, "failed to create display: %m\n");
660 return -1;
661 }
662
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400663 wl_display_add_global_listener(display_get_display(desktop.display),
664 global_handler, &desktop);
665
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100666 wl_list_for_each(output, &desktop.outputs, link) {
Pekka Paalanen068ae942011-11-28 14:11:15 +0200667 struct wl_shell_surface *s;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100668
669 output->panel = panel_create(desktop.display);
Pekka Paalanen068ae942011-11-28 14:11:15 +0200670 s = window_get_wl_shell_surface(output->panel->window);
671 desktop_shell_set_panel(desktop.shell, output->output, s);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100672
673 output->background = background_create(&desktop);
Pekka Paalanen068ae942011-11-28 14:11:15 +0200674 s = window_get_wl_shell_surface(output->background->window);
675 desktop_shell_set_background(desktop.shell, output->output, s);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100676 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400677
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500678 config_file = config_file_path("weston-desktop-shell.ini");
Pekka Paalanen668dd562011-11-15 11:45:40 +0200679 parse_config_file(config_file,
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500680 config_sections, ARRAY_LENGTH(config_sections),
681 &desktop);
Pekka Paalanen668dd562011-11-15 11:45:40 +0200682 free(config_file);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500683
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400684 signal(SIGCHLD, sigchild_handler);
685
686 display_run(desktop.display);
687
688 return 0;
689}