blob: f20e19a76336dfa88624af45581b8ee9efcf103f [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øgsberg0c29eb22011-09-06 18:02:34 -040078struct panel_item {
Kristian Høgsberge28d05b2011-09-20 21:43:54 -040079 struct item *item;
80 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;
88 struct item *button;
89 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øgsbergbcee9a42011-10-12 00:36:16 -0400131show_menu(struct panel *panel, struct input *input)
132{
133 int32_t x, y, width = 200, height = 200;
134 struct display *display;
135
136 input_get_position(input, &x, &y);
137 display = window_get_display(panel->window);
138 panel->menu = window_create_transient(display, panel->window,
139 x - 10, y - 10, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100140 window_set_user_data(panel->menu, panel);
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400141
142 window_draw(panel->menu);
143 window_flush(panel->menu);
144}
145
146static void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400147panel_activate_item(struct panel *panel, struct panel_item *item)
148{
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øgsberg0c29eb22011-09-06 18:02:34 -0400160 if (execl(item->path, item->path, NULL) < 0) {
161 fprintf(stderr, "execl failed: %m\n");
162 exit(1);
163 }
164}
165
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400166static void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400167panel_draw_item(struct item *item, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400168{
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400169 cairo_t *cr = data;
170 struct panel_item *pi;
171 int x, y, width, height;
172 double dx, dy;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400173
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400174 pi = item_get_user_data(item);
175 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);
187 item_set_allocation(item, dx, dy, width, height);
188
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øgsberge28d05b2011-09-20 21:43:54 -0400192 if (window_get_focus_item(pi->panel->window) == item) {
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);
225 window_for_each_item(window, panel_draw_item, 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øgsberge28d05b2011-09-20 21:43:54 -0400233panel_item_focus_handler(struct window *window,
234 struct item *focus, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400235{
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400236 window_schedule_redraw(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400237}
238
239static void
240panel_button_handler(struct window *window,
241 struct input *input, uint32_t time,
242 int button, int state, void *data)
243{
244 struct panel *panel = data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400245 struct panel_item *pi;
246 struct item *focus;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400247
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400248 focus = window_get_focus_item(panel->window);
249 if (focus && button == BTN_LEFT) {
250 pi = item_get_user_data(focus);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400251 window_schedule_redraw(panel->window);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400252 if (state == 0)
253 panel_activate_item(panel, pi);
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400254 } else if (button == BTN_RIGHT) {
255 if (state)
256 show_menu(panel, input);
257 else
258 window_destroy(panel->menu);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400259 }
260}
261
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100262static void
263panel_configure(void *data,
264 struct desktop_shell *desktop_shell,
265 uint32_t time, uint32_t edges,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200266 struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100267 int32_t width, int32_t height)
268{
Pekka Paalanen068ae942011-11-28 14:11:15 +0200269 window_set_child_size(window, width, 32);
270 window_schedule_redraw(window);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100271}
272
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400273static struct panel *
274panel_create(struct display *display)
275{
276 struct panel *panel;
277
278 panel = malloc(sizeof *panel);
279 memset(panel, 0, sizeof *panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400280
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100281 panel->base.configure = panel_configure;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400282 panel->window = window_create(display, 0, 0);
283
284 window_set_title(panel->window, "panel");
285 window_set_decoration(panel->window, 0);
286 window_set_redraw_handler(panel->window, panel_redraw_handler);
287 window_set_custom(panel->window);
288 window_set_user_data(panel->window, panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400289 window_set_button_handler(panel->window, panel_button_handler);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400290 window_set_item_focus_handler(panel->window, panel_item_focus_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400291
292 return panel;
293}
294
295static void
296panel_add_item(struct panel *panel, const char *icon, const char *path)
297{
298 struct panel_item *item;
299
300 item = malloc(sizeof *item);
301 memset(item, 0, sizeof *item);
302 item->icon = cairo_image_surface_create_from_png(icon);
303 item->path = strdup(path);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400304 item->panel = panel;
305 window_add_item(panel->window, item);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400306}
307
308static void
309background_draw(struct window *window, int width, int height, const char *path)
310{
311 cairo_surface_t *surface, *image;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400312 cairo_pattern_t *pattern;
313 cairo_matrix_t matrix;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400314 cairo_t *cr;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400315 double sx, sy;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400316
317 window_set_child_size(window, width, height);
318 window_draw(window);
319 surface = window_get_surface(window);
320
321 cr = cairo_create(surface);
322 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
323 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
324 cairo_paint(cr);
325
326 if (path) {
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400327 image = load_jpeg(path);
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400328 pattern = cairo_pattern_create_for_surface(image);
329 sx = (double) cairo_image_surface_get_width(image) / width;
330 sy = (double) cairo_image_surface_get_height(image) / height;
331 cairo_matrix_init_scale(&matrix, sx, sy);
332 cairo_pattern_set_matrix(pattern, &matrix);
333 cairo_set_source(cr, pattern);
334 cairo_pattern_destroy (pattern);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400335 cairo_paint(cr);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400336 cairo_surface_destroy(image);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400337 }
338
339 cairo_destroy(cr);
340 cairo_surface_destroy(surface);
341 window_flush(window);
342}
343
344static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100345background_configure(void *data,
346 struct desktop_shell *desktop_shell,
347 uint32_t time, uint32_t edges,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200348 struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100349 int32_t width, int32_t height)
350{
351 struct desktop *desktop = data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100352
Pekka Paalanen068ae942011-11-28 14:11:15 +0200353 background_draw(window, width, height, desktop->background_path);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100354}
355
356static void
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200357unlock_dialog_draw(struct unlock_dialog *dialog)
358{
359 struct rectangle allocation;
360 cairo_t *cr;
361 cairo_surface_t *surface;
362 cairo_pattern_t *pat;
363 double cx, cy, r, f;
364
365 window_draw(dialog->window);
366
367 surface = window_get_surface(dialog->window);
368 cr = cairo_create(surface);
369 window_get_child_allocation(dialog->window, &allocation);
370 cairo_rectangle(cr, allocation.x, allocation.y,
371 allocation.width, allocation.height);
372 cairo_clip(cr);
373 cairo_push_group(cr);
374 cairo_translate(cr, allocation.x, allocation.y);
375
376 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
377 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
378 cairo_paint(cr);
379
380 if (window_get_focus_item(dialog->window) == dialog->button)
381 f = 1.0;
382 else
383 f = 0.7;
384
385 cx = allocation.width / 2.0;
386 cy = allocation.height / 2.0;
387 r = (cx < cy ? cx : cy) * 0.4;
388 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
389 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
390 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
391 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
392 cairo_set_source(cr, pat);
393 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
394 cairo_fill(cr);
395
396 item_set_allocation(dialog->button,
397 allocation.x + cx - r,
398 allocation.y + cy - r, 2 * r, 2 * r);
399 cairo_pattern_destroy(pat);
400
401 cairo_pop_group_to_source(cr);
402 cairo_paint(cr);
403 cairo_destroy(cr);
404
405 window_flush(dialog->window);
406 cairo_surface_destroy(surface);
407}
408
409static void
410unlock_dialog_button_handler(struct window *window,
411 struct input *input, uint32_t time,
412 int button, int state, void *data)
413{
414 struct unlock_dialog *dialog = data;
415 struct desktop *desktop = dialog->desktop;
416 struct item *focus;
417
418 focus = window_get_focus_item(dialog->window);
419 if (focus && button == BTN_LEFT) {
420 if (state == 0 && !dialog->closing) {
421 display_defer(desktop->display, &desktop->unlock_task);
422 dialog->closing = 1;
423 }
424 }
425}
426
427static void
428unlock_dialog_redraw_handler(struct window *window, void *data)
429{
430 struct unlock_dialog *dialog = data;
431
432 unlock_dialog_draw(dialog);
433}
434
435static void
436unlock_dialog_keyboard_focus_handler(struct window *window,
437 struct input *device, void *data)
438{
439 window_schedule_redraw(window);
440}
441
442static void
443unlock_dialog_item_focus_handler(struct window *window,
Benjamin Franzked7759712011-11-22 12:38:48 +0100444 struct item *focus, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200445{
446 window_schedule_redraw(window);
447}
448
449static struct unlock_dialog *
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500450unlock_dialog_create(struct desktop *desktop)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200451{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500452 struct display *display = desktop->display;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200453 struct unlock_dialog *dialog;
454
455 dialog = malloc(sizeof *dialog);
456 if (!dialog)
457 return NULL;
458 memset(dialog, 0, sizeof *dialog);
459
460 dialog->window = window_create(display, 260, 230);
461 window_set_title(dialog->window, "Unlock your desktop");
Benjamin Franzke8193bc12011-11-23 19:35:07 +0100462 window_set_custom(dialog->window);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200463
464 window_set_user_data(dialog->window, dialog);
465 window_set_redraw_handler(dialog->window, unlock_dialog_redraw_handler);
466 window_set_keyboard_focus_handler(dialog->window,
467 unlock_dialog_keyboard_focus_handler);
468 window_set_button_handler(dialog->window, unlock_dialog_button_handler);
469 window_set_item_focus_handler(dialog->window,
470 unlock_dialog_item_focus_handler);
471 dialog->button = window_add_item(dialog->window, NULL);
472
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500473 desktop_shell_set_lock_surface(desktop->shell,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200474 window_get_wl_shell_surface(dialog->window));
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500475
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200476 unlock_dialog_draw(dialog);
477
478 return dialog;
479}
480
481static void
482unlock_dialog_destroy(struct unlock_dialog *dialog)
483{
484 window_destroy(dialog->window);
485 free(dialog);
486}
487
488static void
489unlock_dialog_finish(struct task *task, uint32_t events)
490{
491 struct desktop *desktop =
Benjamin Franzked7759712011-11-22 12:38:48 +0100492 container_of(task, struct desktop, unlock_task);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200493
494 desktop_shell_unlock(desktop->shell);
495 unlock_dialog_destroy(desktop->unlock_dialog);
496 desktop->unlock_dialog = NULL;
497}
498
499static void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400500desktop_shell_configure(void *data,
501 struct desktop_shell *desktop_shell,
502 uint32_t time, uint32_t edges,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200503 struct wl_shell_surface *shell_surface,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400504 int32_t width, int32_t height)
505{
Pekka Paalanen068ae942011-11-28 14:11:15 +0200506 struct window *window = wl_shell_surface_get_user_data(shell_surface);
507 struct surface *s = window_get_user_data(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400508
Pekka Paalanen068ae942011-11-28 14:11:15 +0200509 s->configure(data, desktop_shell, time, edges, window, width, height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400510}
511
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200512static void
513desktop_shell_prepare_lock_surface(void *data,
514 struct desktop_shell *desktop_shell)
515{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200516 struct desktop *desktop = data;
517
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200518 if (!key_locking) {
519 desktop_shell_unlock(desktop->shell);
520 return;
521 }
522
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200523 if (!desktop->unlock_dialog) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500524 desktop->unlock_dialog = unlock_dialog_create(desktop);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200525 desktop->unlock_dialog->desktop = desktop;
526 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200527}
528
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400529static const struct desktop_shell_listener listener = {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200530 desktop_shell_configure,
531 desktop_shell_prepare_lock_surface
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400532};
533
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100534static struct background *
535background_create(struct desktop *desktop)
536{
537 struct background *background;
538
539 background = malloc(sizeof *background);
540 memset(background, 0, sizeof *background);
541
542 background->base.configure = background_configure;
543 background->window = window_create(desktop->display, 0, 0);
544 window_set_decoration(background->window, 0);
545 window_set_custom(background->window);
546 window_set_user_data(background->window, background);
547
548 return background;
549}
550
551static void
552create_output(struct desktop *desktop, uint32_t id)
553{
554 struct output *output;
555
556 output = calloc(1, sizeof *output);
557 if (!output)
558 return;
559
560 output->output = wl_display_bind(display_get_display(desktop->display),
561 id, &wl_output_interface);
562
563 wl_list_insert(&desktop->outputs, &output->link);
564}
565
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400566static void
567global_handler(struct wl_display *display, uint32_t id,
568 const char *interface, uint32_t version, void *data)
569{
570 struct desktop *desktop = data;
571
572 if (!strcmp(interface, "desktop_shell")) {
573 desktop->shell =
574 wl_display_bind(display, id, &desktop_shell_interface);
575 desktop_shell_add_listener(desktop->shell, &listener, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100576 } else if (!strcmp(interface, "wl_output")) {
577 create_output(desktop, id);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400578 }
579}
580
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500581static void
582launcher_section_done(void *data)
583{
584 struct desktop *desktop = data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100585 struct output *output;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500586
587 if (key_launcher_icon == NULL || key_launcher_path == NULL) {
588 fprintf(stderr, "invalid launcher section\n");
589 return;
590 }
591
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100592 wl_list_for_each(output, &desktop->outputs, link)
593 panel_add_item(output->panel,
594 key_launcher_icon, key_launcher_path);
595
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500596 free(key_launcher_icon);
597 key_launcher_icon = NULL;
598 free(key_launcher_path);
599 key_launcher_path = NULL;
600}
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400601
602int main(int argc, char *argv[])
603{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200604 struct desktop desktop = { 0 };
Pekka Paalanen668dd562011-11-15 11:45:40 +0200605 char *config_file;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100606 struct output *output;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400607
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200608 desktop.unlock_task.run = unlock_dialog_finish;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100609 wl_list_init(&desktop.outputs);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200610
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400611 desktop.display = display_create(&argc, &argv, NULL);
612 if (desktop.display == NULL) {
613 fprintf(stderr, "failed to create display: %m\n");
614 return -1;
615 }
616
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400617 wl_display_add_global_listener(display_get_display(desktop.display),
618 global_handler, &desktop);
619
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100620 wl_list_for_each(output, &desktop.outputs, link) {
Pekka Paalanen068ae942011-11-28 14:11:15 +0200621 struct wl_shell_surface *s;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100622
623 output->panel = panel_create(desktop.display);
Pekka Paalanen068ae942011-11-28 14:11:15 +0200624 s = window_get_wl_shell_surface(output->panel->window);
625 desktop_shell_set_panel(desktop.shell, output->output, s);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100626
627 output->background = background_create(&desktop);
Pekka Paalanen068ae942011-11-28 14:11:15 +0200628 s = window_get_wl_shell_surface(output->background->window);
629 desktop_shell_set_background(desktop.shell, output->output, s);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100630 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400631
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500632 config_file = config_file_path("weston-desktop-shell.ini");
Pekka Paalanen668dd562011-11-15 11:45:40 +0200633 parse_config_file(config_file,
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500634 config_sections, ARRAY_LENGTH(config_sections),
635 &desktop);
Pekka Paalanen668dd562011-11-15 11:45:40 +0200636 free(config_file);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500637
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500638 desktop.background_path = key_background_image;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400639
640 signal(SIGCHLD, sigchild_handler);
641
642 display_run(desktop.display);
643
644 return 0;
645}