blob: 3aa535b48e6bb1198d676c052904dfc05471afc0 [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;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040045 const char *background_path;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +020046 struct unlock_dialog *unlock_dialog;
47 struct task unlock_task;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010048 struct wl_list outputs;
49};
50
51struct surface {
52 void (*configure)(void *data,
53 struct desktop_shell *desktop_shell,
54 uint32_t time, uint32_t edges,
Pekka Paalanen068ae942011-11-28 14:11:15 +020055 struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010056 int32_t width, int32_t height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040057};
58
59struct panel {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010060 struct surface base;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040061 struct window *window;
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -040062 struct window *menu;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040063};
64
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010065struct background {
66 struct surface base;
67 struct window *window;
68};
69
70struct output {
71 struct wl_output *output;
72 struct wl_list link;
73
74 struct panel *panel;
75 struct background *background;
76};
77
Kristian Høgsbergc51f7992012-01-08 15:09:53 -050078struct panel_widget {
79 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -040080 struct panel *panel;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040081 cairo_surface_t *icon;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040082 int pressed;
83 const char *path;
84};
85
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +020086struct unlock_dialog {
87 struct window *window;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -050088 struct widget *button;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +020089 int closing;
90
91 struct desktop *desktop;
92};
93
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -050094static char *key_background_image;
95static uint32_t key_panel_color;
96static char *key_launcher_icon;
97static char *key_launcher_path;
98static void launcher_section_done(void *data);
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +020099static int key_locking = 1;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500100
101static const struct config_key shell_config_keys[] = {
102 { "background-image", CONFIG_KEY_STRING, &key_background_image },
103 { "panel-color", CONFIG_KEY_INTEGER, &key_panel_color },
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200104 { "locking", CONFIG_KEY_BOOLEAN, &key_locking },
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500105};
106
107static const struct config_key launcher_config_keys[] = {
108 { "icon", CONFIG_KEY_STRING, &key_launcher_icon },
109 { "path", CONFIG_KEY_STRING, &key_launcher_path },
110};
111
112static const struct config_section config_sections[] = {
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500113 { "desktop-shell",
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500114 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
115 { "launcher",
116 launcher_config_keys, ARRAY_LENGTH(launcher_config_keys),
117 launcher_section_done }
118};
119
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400120static void
121sigchild_handler(int s)
122{
123 int status;
124 pid_t pid;
125
126 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
127 fprintf(stderr, "child %d exited\n", pid);
128}
129
130static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500131show_menu(struct panel *panel, struct input *input, uint32_t time)
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400132{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500133 int32_t x, y;
134 static const char *entries[] = {
135 "Roy", "Pris", "Leon", "Zhora"
136 };
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400137
138 input_get_position(input, &x, &y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500139 panel->menu = window_create_menu(window_get_display(panel->window),
140 input, time, panel->window,
Kristian Høgsberg4f7dcd62012-01-06 21:59:05 -0500141 x - 10, y - 10, NULL, entries, 4);
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400142
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500143 window_schedule_redraw(panel->menu);
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400144}
145
146static void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500147panel_activate_widget(struct panel *panel, struct panel_widget *widget)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400148{
149 pid_t pid;
150
151 pid = fork();
152 if (pid < 0) {
153 fprintf(stderr, "fork failed: %m\n");
154 return;
155 }
156
157 if (pid)
158 return;
Benjamin Franzked7759712011-11-22 12:38:48 +0100159
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500160 if (execl(widget->path, widget->path, NULL) < 0) {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400161 fprintf(stderr, "execl failed: %m\n");
162 exit(1);
163 }
164}
165
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400166static void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500167panel_draw_widget(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400168{
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400169 cairo_t *cr = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500170 struct panel_widget *pi;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400171 int x, y, width, height;
172 double dx, dy;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400173
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500174 pi = widget_get_user_data(widget);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400175 width = cairo_image_surface_get_width(pi->icon);
176 height = cairo_image_surface_get_height(pi->icon);
177 x = 0;
178 y = -height / 2;
179 if (pi->pressed) {
180 x++;
181 y++;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400182 }
183
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400184 dx = x;
185 dy = y;
186 cairo_user_to_device(cr, &dx, &dy);
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500187 widget_set_allocation(widget, dx, dy, width, height);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400188
189 cairo_set_source_surface(cr, pi->icon, x, y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400190 cairo_paint(cr);
191
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500192 if (window_get_focus_widget(pi->panel->window) == 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øgsberge28d05b2011-09-20 21:43:54 -0400194 cairo_mask_surface(cr, pi->icon, x, y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400195 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400196
197 cairo_translate(cr, width + 10, 0);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400198}
199
200static void
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500201set_hex_color(cairo_t *cr, uint32_t color)
202{
203 cairo_set_source_rgba(cr,
204 ((color >> 16) & 0xff) / 255.0,
205 ((color >> 8) & 0xff) / 255.0,
206 ((color >> 0) & 0xff) / 255.0,
207 ((color >> 24) & 0xff) / 255.0);
208}
209
210static void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400211panel_redraw_handler(struct window *window, void *data)
212{
213 cairo_surface_t *surface;
214 cairo_t *cr;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400215
216 window_draw(window);
217 surface = window_get_surface(window);
218 cr = cairo_create(surface);
219 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500220 set_hex_color(cr, key_panel_color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400221 cairo_paint(cr);
222
223 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400224 cairo_translate(cr, 10, 32 / 2);
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500225 window_for_each_widget(window, panel_draw_widget, cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400226
227 cairo_destroy(cr);
228 cairo_surface_destroy(surface);
229 window_flush(window);
230}
231
232static void
Kristian Høgsbergee143232012-01-09 08:42:24 -0500233panel_widget_enter_handler(struct widget *widget, struct input *input,
234 uint32_t time, int32_t x, int32_t y, void *data)
235{
236 widget_schedule_redraw(widget);
237}
238
239static void
240panel_widget_leave_handler(struct widget *widget,
241 struct input *input, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400242{
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500243 widget_schedule_redraw(widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400244}
245
246static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500247panel_widget_button_handler(struct widget *widget,
248 struct input *input, uint32_t time,
249 int button, int state, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400250{
251 struct panel *panel = data;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500252 struct panel_widget *pi;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400253
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500254 pi = widget_get_user_data(widget);
255 widget_schedule_redraw(widget);
256 if (state == 0)
257 panel_activate_widget(panel, pi);
258}
259
260static void
261panel_button_handler(struct widget *widget,
262 struct input *input, uint32_t time,
263 int button, int state, void *data)
264{
265 struct window *window = data;
266 struct panel *panel = window_get_user_data(window);
267
268 if (button == BTN_RIGHT && state)
269 show_menu(panel, input, time);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400270}
271
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100272static void
273panel_configure(void *data,
274 struct desktop_shell *desktop_shell,
275 uint32_t time, uint32_t edges,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200276 struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100277 int32_t width, int32_t height)
278{
Pekka Paalanen068ae942011-11-28 14:11:15 +0200279 window_set_child_size(window, width, 32);
280 window_schedule_redraw(window);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100281}
282
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400283static struct panel *
284panel_create(struct display *display)
285{
286 struct panel *panel;
287
288 panel = malloc(sizeof *panel);
289 memset(panel, 0, sizeof *panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400290
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100291 panel->base.configure = panel_configure;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400292 panel->window = window_create(display, 0, 0);
293
294 window_set_title(panel->window, "panel");
295 window_set_decoration(panel->window, 0);
296 window_set_redraw_handler(panel->window, panel_redraw_handler);
297 window_set_custom(panel->window);
298 window_set_user_data(panel->window, panel);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500299
300 widget_set_button_handler(window_get_widget(panel->window),
301 panel_button_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400302
303 return panel;
304}
305
306static void
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500307panel_add_widget(struct panel *panel, const char *icon, const char *path)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400308{
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500309 struct panel_widget *widget;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400310
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500311 widget = malloc(sizeof *widget);
312 memset(widget, 0, sizeof *widget);
313 widget->icon = cairo_image_surface_create_from_png(icon);
314 widget->path = strdup(path);
315 widget->panel = panel;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500316
317 widget->widget = window_add_widget(panel->window, widget);
Kristian Høgsbergee143232012-01-09 08:42:24 -0500318 widget_set_enter_handler(widget->widget, panel_widget_enter_handler);
319 widget_set_leave_handler(widget->widget, panel_widget_leave_handler);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500320 widget_set_button_handler(widget->widget, panel_widget_button_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400321}
322
323static void
324background_draw(struct window *window, int width, int height, const char *path)
325{
326 cairo_surface_t *surface, *image;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400327 cairo_pattern_t *pattern;
328 cairo_matrix_t matrix;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400329 cairo_t *cr;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400330 double sx, sy;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400331
332 window_set_child_size(window, width, height);
333 window_draw(window);
334 surface = window_get_surface(window);
335
336 cr = cairo_create(surface);
337 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
338 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
339 cairo_paint(cr);
340
341 if (path) {
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400342 image = load_jpeg(path);
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400343 pattern = cairo_pattern_create_for_surface(image);
344 sx = (double) cairo_image_surface_get_width(image) / width;
345 sy = (double) cairo_image_surface_get_height(image) / height;
346 cairo_matrix_init_scale(&matrix, sx, sy);
347 cairo_pattern_set_matrix(pattern, &matrix);
348 cairo_set_source(cr, pattern);
349 cairo_pattern_destroy (pattern);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400350 cairo_paint(cr);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400351 cairo_surface_destroy(image);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400352 }
353
354 cairo_destroy(cr);
355 cairo_surface_destroy(surface);
356 window_flush(window);
357}
358
359static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100360background_configure(void *data,
361 struct desktop_shell *desktop_shell,
362 uint32_t time, uint32_t edges,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200363 struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100364 int32_t width, int32_t height)
365{
366 struct desktop *desktop = data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100367
Pekka Paalanen068ae942011-11-28 14:11:15 +0200368 background_draw(window, width, height, desktop->background_path);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100369}
370
371static void
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200372unlock_dialog_draw(struct unlock_dialog *dialog)
373{
374 struct rectangle allocation;
375 cairo_t *cr;
376 cairo_surface_t *surface;
377 cairo_pattern_t *pat;
378 double cx, cy, r, f;
379
380 window_draw(dialog->window);
381
382 surface = window_get_surface(dialog->window);
383 cr = cairo_create(surface);
384 window_get_child_allocation(dialog->window, &allocation);
385 cairo_rectangle(cr, allocation.x, allocation.y,
386 allocation.width, allocation.height);
387 cairo_clip(cr);
388 cairo_push_group(cr);
389 cairo_translate(cr, allocation.x, allocation.y);
390
391 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
392 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
393 cairo_paint(cr);
394
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500395 if (window_get_focus_widget(dialog->window) == dialog->button)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200396 f = 1.0;
397 else
398 f = 0.7;
399
400 cx = allocation.width / 2.0;
401 cy = allocation.height / 2.0;
402 r = (cx < cy ? cx : cy) * 0.4;
403 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
404 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
405 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
406 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
407 cairo_set_source(cr, pat);
408 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
409 cairo_fill(cr);
410
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500411 widget_set_allocation(dialog->button,
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200412 allocation.x + cx - r,
413 allocation.y + cy - r, 2 * r, 2 * r);
414 cairo_pattern_destroy(pat);
415
416 cairo_pop_group_to_source(cr);
417 cairo_paint(cr);
418 cairo_destroy(cr);
419
420 window_flush(dialog->window);
421 cairo_surface_destroy(surface);
422}
423
424static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500425unlock_dialog_button_handler(struct widget *widget,
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200426 struct input *input, uint32_t time,
427 int button, int state, void *data)
428{
429 struct unlock_dialog *dialog = data;
430 struct desktop *desktop = dialog->desktop;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200431
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500432 if (button == BTN_LEFT) {
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200433 if (state == 0 && !dialog->closing) {
434 display_defer(desktop->display, &desktop->unlock_task);
435 dialog->closing = 1;
436 }
437 }
438}
439
440static void
441unlock_dialog_redraw_handler(struct window *window, void *data)
442{
443 struct unlock_dialog *dialog = data;
444
445 unlock_dialog_draw(dialog);
446}
447
448static void
449unlock_dialog_keyboard_focus_handler(struct window *window,
450 struct input *device, void *data)
451{
452 window_schedule_redraw(window);
453}
454
455static void
Kristian Høgsbergee143232012-01-09 08:42:24 -0500456unlock_dialog_widget_enter_handler(struct widget *widget,
457 struct input *input, uint32_t time,
458 int32_t x, int32_t y, void *data)
459{
460 widget_schedule_redraw(widget);
461}
462
463static void
464unlock_dialog_widget_leave_handler(struct widget *widget,
465 struct input *input, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200466{
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500467 widget_schedule_redraw(widget);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200468}
469
470static struct unlock_dialog *
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500471unlock_dialog_create(struct desktop *desktop)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200472{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500473 struct display *display = desktop->display;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200474 struct unlock_dialog *dialog;
475
476 dialog = malloc(sizeof *dialog);
477 if (!dialog)
478 return NULL;
479 memset(dialog, 0, sizeof *dialog);
480
481 dialog->window = window_create(display, 260, 230);
482 window_set_title(dialog->window, "Unlock your desktop");
Benjamin Franzke8193bc12011-11-23 19:35:07 +0100483 window_set_custom(dialog->window);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200484
485 window_set_user_data(dialog->window, dialog);
486 window_set_redraw_handler(dialog->window, unlock_dialog_redraw_handler);
487 window_set_keyboard_focus_handler(dialog->window,
488 unlock_dialog_keyboard_focus_handler);
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500489 dialog->button = window_add_widget(dialog->window, NULL);
Kristian Høgsbergee143232012-01-09 08:42:24 -0500490 widget_set_enter_handler(dialog->button,
491 unlock_dialog_widget_enter_handler);
492 widget_set_leave_handler(dialog->button,
493 unlock_dialog_widget_leave_handler);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500494 widget_set_button_handler(dialog->button,
495 unlock_dialog_button_handler);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200496
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500497 desktop_shell_set_lock_surface(desktop->shell,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200498 window_get_wl_shell_surface(dialog->window));
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500499
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200500 unlock_dialog_draw(dialog);
501
502 return dialog;
503}
504
505static void
506unlock_dialog_destroy(struct unlock_dialog *dialog)
507{
508 window_destroy(dialog->window);
509 free(dialog);
510}
511
512static void
513unlock_dialog_finish(struct task *task, uint32_t events)
514{
515 struct desktop *desktop =
Benjamin Franzked7759712011-11-22 12:38:48 +0100516 container_of(task, struct desktop, unlock_task);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200517
518 desktop_shell_unlock(desktop->shell);
519 unlock_dialog_destroy(desktop->unlock_dialog);
520 desktop->unlock_dialog = NULL;
521}
522
523static void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400524desktop_shell_configure(void *data,
525 struct desktop_shell *desktop_shell,
526 uint32_t time, uint32_t edges,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200527 struct wl_shell_surface *shell_surface,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400528 int32_t width, int32_t height)
529{
Pekka Paalanen068ae942011-11-28 14:11:15 +0200530 struct window *window = wl_shell_surface_get_user_data(shell_surface);
531 struct surface *s = window_get_user_data(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400532
Pekka Paalanen068ae942011-11-28 14:11:15 +0200533 s->configure(data, desktop_shell, time, edges, window, width, height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400534}
535
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200536static void
537desktop_shell_prepare_lock_surface(void *data,
538 struct desktop_shell *desktop_shell)
539{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200540 struct desktop *desktop = data;
541
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200542 if (!key_locking) {
543 desktop_shell_unlock(desktop->shell);
544 return;
545 }
546
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200547 if (!desktop->unlock_dialog) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500548 desktop->unlock_dialog = unlock_dialog_create(desktop);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200549 desktop->unlock_dialog->desktop = desktop;
550 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200551}
552
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400553static const struct desktop_shell_listener listener = {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200554 desktop_shell_configure,
555 desktop_shell_prepare_lock_surface
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400556};
557
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100558static struct background *
559background_create(struct desktop *desktop)
560{
561 struct background *background;
562
563 background = malloc(sizeof *background);
564 memset(background, 0, sizeof *background);
565
566 background->base.configure = background_configure;
567 background->window = window_create(desktop->display, 0, 0);
568 window_set_decoration(background->window, 0);
569 window_set_custom(background->window);
570 window_set_user_data(background->window, background);
571
572 return background;
573}
574
575static void
576create_output(struct desktop *desktop, uint32_t id)
577{
578 struct output *output;
579
580 output = calloc(1, sizeof *output);
581 if (!output)
582 return;
583
584 output->output = wl_display_bind(display_get_display(desktop->display),
585 id, &wl_output_interface);
586
587 wl_list_insert(&desktop->outputs, &output->link);
588}
589
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400590static void
591global_handler(struct wl_display *display, uint32_t id,
592 const char *interface, uint32_t version, void *data)
593{
594 struct desktop *desktop = data;
595
596 if (!strcmp(interface, "desktop_shell")) {
597 desktop->shell =
598 wl_display_bind(display, id, &desktop_shell_interface);
599 desktop_shell_add_listener(desktop->shell, &listener, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100600 } else if (!strcmp(interface, "wl_output")) {
601 create_output(desktop, id);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400602 }
603}
604
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500605static void
606launcher_section_done(void *data)
607{
608 struct desktop *desktop = data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100609 struct output *output;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500610
611 if (key_launcher_icon == NULL || key_launcher_path == NULL) {
612 fprintf(stderr, "invalid launcher section\n");
613 return;
614 }
615
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100616 wl_list_for_each(output, &desktop->outputs, link)
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500617 panel_add_widget(output->panel,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100618 key_launcher_icon, key_launcher_path);
619
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500620 free(key_launcher_icon);
621 key_launcher_icon = NULL;
622 free(key_launcher_path);
623 key_launcher_path = NULL;
624}
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400625
626int main(int argc, char *argv[])
627{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200628 struct desktop desktop = { 0 };
Pekka Paalanen668dd562011-11-15 11:45:40 +0200629 char *config_file;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100630 struct output *output;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400631
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200632 desktop.unlock_task.run = unlock_dialog_finish;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100633 wl_list_init(&desktop.outputs);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200634
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400635 desktop.display = display_create(&argc, &argv, NULL);
636 if (desktop.display == NULL) {
637 fprintf(stderr, "failed to create display: %m\n");
638 return -1;
639 }
640
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400641 wl_display_add_global_listener(display_get_display(desktop.display),
642 global_handler, &desktop);
643
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100644 wl_list_for_each(output, &desktop.outputs, link) {
Pekka Paalanen068ae942011-11-28 14:11:15 +0200645 struct wl_shell_surface *s;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100646
647 output->panel = panel_create(desktop.display);
Pekka Paalanen068ae942011-11-28 14:11:15 +0200648 s = window_get_wl_shell_surface(output->panel->window);
649 desktop_shell_set_panel(desktop.shell, output->output, s);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100650
651 output->background = background_create(&desktop);
Pekka Paalanen068ae942011-11-28 14:11:15 +0200652 s = window_get_wl_shell_surface(output->background->window);
653 desktop_shell_set_background(desktop.shell, output->output, s);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100654 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400655
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500656 config_file = config_file_path("weston-desktop-shell.ini");
Pekka Paalanen668dd562011-11-15 11:45:40 +0200657 parse_config_file(config_file,
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500658 config_sections, ARRAY_LENGTH(config_sections),
659 &desktop);
Pekka Paalanen668dd562011-11-15 11:45:40 +0200660 free(config_file);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500661
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500662 desktop.background_path = key_background_image;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400663
664 signal(SIGCHLD, sigchild_handler);
665
666 display_run(desktop.display);
667
668 return 0;
669}