blob: ae8e5f071dde2139c933ee3ae41f35aff1659062 [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>
Martin Minarik1e51a872012-06-08 00:39:11 +020033#include <sys/timerfd.h>
34#include <sys/epoll.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040035#include <linux/input.h>
Tiago Vignatti61500722012-05-23 22:06:28 +030036#include <libgen.h>
Martin Minarik1e51a872012-06-08 00:39:11 +020037#include <time.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040038
Pekka Paalanen50719bc2011-11-22 14:18:50 +020039#include <wayland-client.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040040#include "window.h"
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040041#include "../shared/cairo-util.h"
Kristian Høgsberg9b935c82011-12-08 12:44:27 -050042#include "../shared/config-parser.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040043
Pekka Paalanen50719bc2011-11-22 14:18:50 +020044#include "desktop-shell-client-protocol.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040045
46struct desktop {
47 struct display *display;
48 struct desktop_shell *shell;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +020049 struct unlock_dialog *unlock_dialog;
50 struct task unlock_task;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010051 struct wl_list outputs;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040052
53 struct window *busy_window;
54 struct widget *busy_widget;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010055};
56
57struct surface {
58 void (*configure)(void *data,
59 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040060 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010061 int32_t width, int32_t height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040062};
63
64struct panel {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010065 struct surface base;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040066 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050067 struct widget *widget;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050068 struct wl_list launcher_list;
Martin Minarik1e51a872012-06-08 00:39:11 +020069 struct panel_clock *clock;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040070};
71
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010072struct background {
73 struct surface base;
74 struct window *window;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050075 struct widget *widget;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010076};
77
78struct output {
79 struct wl_output *output;
80 struct wl_list link;
81
82 struct panel *panel;
83 struct background *background;
84};
85
Kristian Høgsberg53880802012-01-09 11:16:50 -050086struct panel_launcher {
Kristian Høgsbergc51f7992012-01-08 15:09:53 -050087 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -040088 struct panel *panel;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040089 cairo_surface_t *icon;
Kristian Høgsbergb6323512012-01-11 00:04:42 -050090 int focused, pressed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040091 const char *path;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050092 struct wl_list link;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040093};
94
Martin Minarik1e51a872012-06-08 00:39:11 +020095struct panel_clock {
96 struct widget *widget;
97 struct panel *panel;
98 char string[128];
99 struct task clock_task;
100 int clock_fd;
101};
102
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200103struct unlock_dialog {
104 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500105 struct widget *widget;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500106 struct widget *button;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500107 int button_focused;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200108 int closing;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200109 struct desktop *desktop;
110};
111
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500112static char *key_background_image = DATADIR "/weston/pattern.png";
113static char *key_background_type = "tile";
114static uint32_t key_panel_color = 0xaa000000;
115static uint32_t key_background_color = 0xff002244;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500116static char *key_launcher_icon;
117static char *key_launcher_path;
118static void launcher_section_done(void *data);
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200119static int key_locking = 1;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500120
121static const struct config_key shell_config_keys[] = {
122 { "background-image", CONFIG_KEY_STRING, &key_background_image },
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500123 { "background-type", CONFIG_KEY_STRING, &key_background_type },
Scott Moreaufa1de692012-01-27 13:25:49 -0700124 { "panel-color", CONFIG_KEY_UNSIGNED_INTEGER, &key_panel_color },
125 { "background-color", CONFIG_KEY_UNSIGNED_INTEGER, &key_background_color },
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200126 { "locking", CONFIG_KEY_BOOLEAN, &key_locking },
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500127};
128
129static const struct config_key launcher_config_keys[] = {
130 { "icon", CONFIG_KEY_STRING, &key_launcher_icon },
131 { "path", CONFIG_KEY_STRING, &key_launcher_path },
132};
133
134static const struct config_section config_sections[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200135 { "shell",
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500136 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
137 { "launcher",
138 launcher_config_keys, ARRAY_LENGTH(launcher_config_keys),
139 launcher_section_done }
140};
141
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400142static void
143sigchild_handler(int s)
144{
145 int status;
146 pid_t pid;
147
148 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
149 fprintf(stderr, "child %d exited\n", pid);
150}
151
152static void
Pekka Paalanen9e30a822012-01-19 16:40:28 +0200153menu_func(struct window *window, int index, void *data)
154{
155 printf("Selected index %d from a panel menu.\n", index);
156}
157
158static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500159show_menu(struct panel *panel, struct input *input, uint32_t time)
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400160{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500161 int32_t x, y;
162 static const char *entries[] = {
163 "Roy", "Pris", "Leon", "Zhora"
164 };
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400165
166 input_get_position(input, &x, &y);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +0200167 window_show_menu(window_get_display(panel->window),
168 input, time, panel->window,
Pekka Paalanen9e30a822012-01-19 16:40:28 +0200169 x - 10, y - 10, menu_func, entries, 4);
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400170}
171
172static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500173panel_launcher_activate(struct panel_launcher *widget)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400174{
175 pid_t pid;
176
177 pid = fork();
178 if (pid < 0) {
179 fprintf(stderr, "fork failed: %m\n");
180 return;
181 }
182
183 if (pid)
184 return;
Benjamin Franzked7759712011-11-22 12:38:48 +0100185
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500186 if (execl(widget->path, widget->path, NULL) < 0) {
Pekka Paalanena291ae52012-01-27 09:50:02 +0200187 fprintf(stderr, "execl '%s' failed: %m\n", widget->path);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400188 exit(1);
189 }
190}
191
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400192static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500193panel_launcher_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400194{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500195 struct panel_launcher *launcher = data;
196 cairo_surface_t *surface;
197 struct rectangle allocation;
198 cairo_t *cr;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400199
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500200 surface = window_get_surface(launcher->panel->window);
201 cr = cairo_create(surface);
202
203 widget_get_allocation(widget, &allocation);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500204 if (launcher->pressed) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500205 allocation.x++;
206 allocation.y++;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400207 }
208
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500209 cairo_set_source_surface(cr, launcher->icon,
210 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400211 cairo_paint(cr);
212
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500213 if (launcher->focused) {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400214 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500215 cairo_mask_surface(cr, launcher->icon,
216 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400217 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400218
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500219 cairo_destroy(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400220}
221
Tiago Vignatti61500722012-05-23 22:06:28 +0300222static int
223panel_launcher_motion_handler(struct widget *widget, struct input *input,
224 uint32_t time, float x, float y, void *data)
225{
226 struct panel_launcher *launcher = data;
227
228 widget_set_tooltip(widget, basename((char *)launcher->path), x, y);
229
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300230 return CURSOR_LEFT_PTR;
Tiago Vignatti61500722012-05-23 22:06:28 +0300231}
232
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400233static void
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500234set_hex_color(cairo_t *cr, uint32_t color)
235{
236 cairo_set_source_rgba(cr,
237 ((color >> 16) & 0xff) / 255.0,
238 ((color >> 8) & 0xff) / 255.0,
239 ((color >> 0) & 0xff) / 255.0,
240 ((color >> 24) & 0xff) / 255.0);
241}
242
243static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500244panel_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400245{
246 cairo_surface_t *surface;
247 cairo_t *cr;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500248 struct panel *panel = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400249
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500250 surface = window_get_surface(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400251 cr = cairo_create(surface);
252 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500253 set_hex_color(cr, key_panel_color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400254 cairo_paint(cr);
255
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400256 cairo_destroy(cr);
257 cairo_surface_destroy(surface);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400258}
259
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500260static int
Kristian Høgsberg53880802012-01-09 11:16:50 -0500261panel_launcher_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400262 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500263{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500264 struct panel_launcher *launcher = data;
265
266 launcher->focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500267 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500268
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300269 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500270}
271
272static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500273panel_launcher_leave_handler(struct widget *widget,
274 struct input *input, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400275{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500276 struct panel_launcher *launcher = data;
277
278 launcher->focused = 0;
Tiago Vignatti61500722012-05-23 22:06:28 +0300279 widget_destroy_tooltip(widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500280 widget_schedule_redraw(widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400281}
282
283static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500284panel_launcher_button_handler(struct widget *widget,
285 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100286 uint32_t button,
287 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400288{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500289 struct panel_launcher *launcher;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400290
Kristian Høgsberg53880802012-01-09 11:16:50 -0500291 launcher = widget_get_user_data(widget);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500292 widget_schedule_redraw(widget);
Daniel Stone4dbadb12012-05-30 16:31:51 +0100293 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg53880802012-01-09 11:16:50 -0500294 panel_launcher_activate(launcher);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500295}
296
Martin Minarik1e51a872012-06-08 00:39:11 +0200297
298static int
299panel_clock_tick(struct panel_clock *clock)
300{
301 time_t rawtime;
302 struct tm * timeinfo;
303 char string[128];
304
305 time ( &rawtime );
306 timeinfo = localtime ( &rawtime );
307 strftime (string,124,"%a %b %d, %I:%M:%S %p",timeinfo);
308
309 if (0 == strcmp(string, clock->string))
310 return 0;
311
312 strncpy (clock->string, string, 126 );
313 return 1;
314}
315
316static void
317clock_func(struct task *task, uint32_t events)
318{
319 struct panel_clock *clock =
320 container_of(task, struct panel_clock, clock_task);
Kristian Høgsberg70226bb2012-06-08 16:54:52 -0400321 uint64_t exp;
Martin Minarik1e51a872012-06-08 00:39:11 +0200322
Kristian Høgsberg70226bb2012-06-08 16:54:52 -0400323 read(clock->clock_fd, &exp, sizeof exp);
Martin Minarik1e51a872012-06-08 00:39:11 +0200324 if (panel_clock_tick(clock))
325 widget_schedule_redraw(clock->widget);
326}
327
328static void
329panel_clock_redraw_handler(struct widget *widget, void *data)
330{
331 cairo_surface_t *surface;
332 struct panel_clock *clock = data;
333 cairo_t *cr;
334 struct rectangle allocation;
335 cairo_text_extents_t extents;
336 cairo_font_extents_t font_extents;
337
338 time_t rawtime;
339 struct tm * timeinfo;
340
341 time ( &rawtime );
342 timeinfo = localtime ( &rawtime );
343 strftime (clock->string,126,"%a %b %d, %I:%M:%S %p",timeinfo);
344
345 widget_get_allocation(widget, &allocation);
346
347 if (allocation.width == 0) return;
348
349 surface = window_get_surface(clock->panel->window);
350 cr = cairo_create(surface);
351 cairo_rectangle(cr, allocation.x, allocation.y,
352 allocation.width, allocation.height);
353 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
354 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.1);
355 cairo_rectangle (cr, allocation.x, allocation.y, 3, 3);
356 cairo_fill(cr);
357
358 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
359 cairo_select_font_face(cr, "sans",
360 CAIRO_FONT_SLANT_NORMAL,
361 CAIRO_FONT_WEIGHT_NORMAL);
362 cairo_set_font_size(cr, 14);
363 cairo_text_extents(cr, clock->string, &extents);
364 cairo_font_extents (cr, &font_extents);
365 cairo_move_to(cr, allocation.x + 5, allocation.y + 3*(allocation.height>>2) + 1);
366 cairo_set_source_rgb(cr, 0, 0, 0);
367 cairo_show_text(cr, clock->string);
368 cairo_move_to(cr, allocation.x + 4, allocation.y + 3*(allocation.height>>2));
369 cairo_set_source_rgb(cr, 1, 1, 1);
370 cairo_show_text(cr, clock->string);
371 cairo_destroy(cr);
372}
373
374static int
375clock_timer_reset(struct panel_clock *clock)
376{
377 struct itimerspec its;
378 its.it_interval.tv_sec = 1;
379 its.it_interval.tv_nsec = 0;
380
381 its.it_value.tv_sec = 1;
382 its.it_value.tv_nsec = 0;
383 if (timerfd_settime(clock->clock_fd, 0, &its, NULL) < 0) {
384 fprintf(stderr, "could not set timerfd\n: %m");
385 return -1;
386 }
387
388 return 0;
389}
390
391static void
392panel_add_clock(struct panel *panel)
393{
394 struct panel_clock *clock;
395 int timerfd;
396
397 timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
398 if (timerfd < 0) {
399 fprintf(stderr, "could not create timerfd\n: %m");
400 return;
401 }
402
403 clock = malloc(sizeof *clock);
404 memset(clock, 0, sizeof *clock);
405 clock->panel = panel;
406 panel->clock = clock;
407 clock->clock_fd = timerfd;
408
409 clock->clock_task.run = clock_func;
410 display_watch_fd(
411 window_get_display(panel->window),
412 clock->clock_fd,
413 EPOLLIN,
414 &clock->clock_task);
415 clock_timer_reset(clock);
416
417 clock->widget = widget_add_widget(panel->widget, clock);
418 widget_set_redraw_handler(clock->widget,
419 panel_clock_redraw_handler);
420}
421
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500422static void
423panel_button_handler(struct widget *widget,
424 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100425 uint32_t button,
426 enum wl_pointer_button_state state, void *data)
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500427{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500428 struct panel *panel = data;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500429
Daniel Stone4dbadb12012-05-30 16:31:51 +0100430 if (button == BTN_RIGHT && state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500431 show_menu(panel, input, time);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400432}
433
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100434static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500435panel_resize_handler(struct widget *widget,
436 int32_t width, int32_t height, void *data)
437{
438 struct panel_launcher *launcher;
439 struct panel *panel = data;
440 int x, y, w, h;
441
442 x = 10;
443 y = 16;
444 wl_list_for_each(launcher, &panel->launcher_list, link) {
445 w = cairo_image_surface_get_width(launcher->icon);
446 h = cairo_image_surface_get_height(launcher->icon);
447 widget_set_allocation(launcher->widget,
448 x, y - h / 2, w + 1, h + 1);
449 x += w + 10;
450 }
Martin Minarik1e51a872012-06-08 00:39:11 +0200451 h=20;
452 w=170;
453 widget_set_allocation(panel->clock->widget, width - w - 8, y - h / 2, w + 1, h + 1);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500454}
455
456static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100457panel_configure(void *data,
458 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400459 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100460 int32_t width, int32_t height)
461{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500462 struct surface *surface = window_get_user_data(window);
463 struct panel *panel = container_of(surface, struct panel, base);
464
465 window_schedule_resize(panel->window, width, 32);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100466}
467
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400468static struct panel *
469panel_create(struct display *display)
470{
471 struct panel *panel;
472
473 panel = malloc(sizeof *panel);
474 memset(panel, 0, sizeof *panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400475
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100476 panel->base.configure = panel_configure;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500477 panel->window = window_create(display);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500478 panel->widget = window_add_widget(panel->window, panel);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500479 wl_list_init(&panel->launcher_list);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400480
481 window_set_title(panel->window, "panel");
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400482 window_set_custom(panel->window);
483 window_set_user_data(panel->window, panel);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500484
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500485 widget_set_redraw_handler(panel->widget, panel_redraw_handler);
486 widget_set_resize_handler(panel->widget, panel_resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500487 widget_set_button_handler(panel->widget, panel_button_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400488
489 return panel;
490}
491
492static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500493panel_add_launcher(struct panel *panel, const char *icon, const char *path)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400494{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500495 struct panel_launcher *launcher;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400496
Kristian Høgsberg53880802012-01-09 11:16:50 -0500497 launcher = malloc(sizeof *launcher);
498 memset(launcher, 0, sizeof *launcher);
499 launcher->icon = cairo_image_surface_create_from_png(icon);
500 launcher->path = strdup(path);
501 launcher->panel = panel;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500502 wl_list_insert(panel->launcher_list.prev, &launcher->link);
503
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500504 launcher->widget = widget_add_widget(panel->widget, launcher);
Kristian Høgsberg53880802012-01-09 11:16:50 -0500505 widget_set_enter_handler(launcher->widget,
506 panel_launcher_enter_handler);
507 widget_set_leave_handler(launcher->widget,
508 panel_launcher_leave_handler);
509 widget_set_button_handler(launcher->widget,
510 panel_launcher_button_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500511 widget_set_redraw_handler(launcher->widget,
512 panel_launcher_redraw_handler);
Tiago Vignatti61500722012-05-23 22:06:28 +0300513 widget_set_motion_handler(launcher->widget,
514 panel_launcher_motion_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400515}
516
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500517enum {
518 BACKGROUND_SCALE,
519 BACKGROUND_TILE
520};
521
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400522static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500523background_draw(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400524{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500525 struct background *background = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400526 cairo_surface_t *surface, *image;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400527 cairo_pattern_t *pattern;
528 cairo_matrix_t matrix;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400529 cairo_t *cr;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400530 double sx, sy;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500531 struct rectangle allocation;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500532 int type = -1;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400533
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500534 surface = window_get_surface(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400535
536 cr = cairo_create(surface);
537 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
538 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
539 cairo_paint(cr);
540
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500541 widget_get_allocation(widget, &allocation);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500542 image = NULL;
543 if (key_background_image)
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400544 image = load_cairo_surface(key_background_image);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500545
546 if (strcmp(key_background_type, "scale") == 0)
547 type = BACKGROUND_SCALE;
548 else if (strcmp(key_background_type, "tile") == 0)
549 type = BACKGROUND_TILE;
550 else
551 fprintf(stderr, "invalid background-type: %s\n",
552 key_background_type);
553
554 if (image && type != -1) {
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400555 pattern = cairo_pattern_create_for_surface(image);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500556 switch (type) {
557 case BACKGROUND_SCALE:
558 sx = (double) cairo_image_surface_get_width(image) /
559 allocation.width;
560 sy = (double) cairo_image_surface_get_height(image) /
561 allocation.height;
562 cairo_matrix_init_scale(&matrix, sx, sy);
563 cairo_pattern_set_matrix(pattern, &matrix);
564 break;
565 case BACKGROUND_TILE:
566 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
567 break;
568 }
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400569 cairo_set_source(cr, pattern);
570 cairo_pattern_destroy (pattern);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400571 cairo_surface_destroy(image);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500572 } else {
573 set_hex_color(cr, key_background_color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400574 }
575
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500576 cairo_paint(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400577 cairo_destroy(cr);
578 cairo_surface_destroy(surface);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400579}
580
581static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100582background_configure(void *data,
583 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400584 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100585 int32_t width, int32_t height)
586{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500587 struct background *background =
588 (struct background *) window_get_user_data(window);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100589
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500590 widget_schedule_resize(background->widget, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100591}
592
593static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500594unlock_dialog_redraw_handler(struct widget *widget, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200595{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500596 struct unlock_dialog *dialog = data;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200597 struct rectangle allocation;
598 cairo_t *cr;
599 cairo_surface_t *surface;
600 cairo_pattern_t *pat;
601 double cx, cy, r, f;
602
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200603 surface = window_get_surface(dialog->window);
604 cr = cairo_create(surface);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500605
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500606 widget_get_allocation(dialog->widget, &allocation);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200607 cairo_rectangle(cr, allocation.x, allocation.y,
608 allocation.width, allocation.height);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200609 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
610 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500611 cairo_fill(cr);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200612
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500613 cairo_translate(cr, allocation.x, allocation.y);
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500614 if (dialog->button_focused)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200615 f = 1.0;
616 else
617 f = 0.7;
618
619 cx = allocation.width / 2.0;
620 cy = allocation.height / 2.0;
621 r = (cx < cy ? cx : cy) * 0.4;
622 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
623 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
624 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
625 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
626 cairo_set_source(cr, pat);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500627 cairo_pattern_destroy(pat);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200628 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
629 cairo_fill(cr);
630
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500631 widget_set_allocation(dialog->button,
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500632 allocation.x + cx - r,
633 allocation.y + cy - r, 2 * r, 2 * r);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200634
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200635 cairo_destroy(cr);
636
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200637 cairo_surface_destroy(surface);
638}
639
640static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500641unlock_dialog_button_handler(struct widget *widget,
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200642 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100643 uint32_t button,
644 enum wl_pointer_button_state state, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200645{
646 struct unlock_dialog *dialog = data;
647 struct desktop *desktop = dialog->desktop;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200648
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500649 if (button == BTN_LEFT) {
Daniel Stone4dbadb12012-05-30 16:31:51 +0100650 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
651 !dialog->closing) {
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200652 display_defer(desktop->display, &desktop->unlock_task);
653 dialog->closing = 1;
654 }
655 }
656}
657
658static void
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200659unlock_dialog_keyboard_focus_handler(struct window *window,
660 struct input *device, void *data)
661{
662 window_schedule_redraw(window);
663}
664
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500665static int
Kristian Høgsbergee143232012-01-09 08:42:24 -0500666unlock_dialog_widget_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400667 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400668 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500669{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500670 struct unlock_dialog *dialog = data;
671
672 dialog->button_focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500673 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500674
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300675 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500676}
677
678static void
679unlock_dialog_widget_leave_handler(struct widget *widget,
680 struct input *input, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200681{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500682 struct unlock_dialog *dialog = data;
683
684 dialog->button_focused = 0;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500685 widget_schedule_redraw(widget);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200686}
687
688static struct unlock_dialog *
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500689unlock_dialog_create(struct desktop *desktop)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200690{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500691 struct display *display = desktop->display;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200692 struct unlock_dialog *dialog;
693
694 dialog = malloc(sizeof *dialog);
695 if (!dialog)
696 return NULL;
697 memset(dialog, 0, sizeof *dialog);
698
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500699 dialog->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500700 dialog->widget = frame_create(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200701 window_set_title(dialog->window, "Unlock your desktop");
Benjamin Franzke8193bc12011-11-23 19:35:07 +0100702 window_set_custom(dialog->window);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200703
704 window_set_user_data(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200705 window_set_keyboard_focus_handler(dialog->window,
706 unlock_dialog_keyboard_focus_handler);
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500707 dialog->button = widget_add_widget(dialog->widget, dialog);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500708 widget_set_redraw_handler(dialog->widget,
709 unlock_dialog_redraw_handler);
Kristian Høgsbergee143232012-01-09 08:42:24 -0500710 widget_set_enter_handler(dialog->button,
711 unlock_dialog_widget_enter_handler);
712 widget_set_leave_handler(dialog->button,
713 unlock_dialog_widget_leave_handler);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500714 widget_set_button_handler(dialog->button,
715 unlock_dialog_button_handler);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200716
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500717 desktop_shell_set_lock_surface(desktop->shell,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200718 window_get_wl_shell_surface(dialog->window));
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500719
Pekka Paalanen40e49ac2012-01-18 16:51:30 +0200720 window_schedule_resize(dialog->window, 260, 230);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200721
722 return dialog;
723}
724
725static void
726unlock_dialog_destroy(struct unlock_dialog *dialog)
727{
728 window_destroy(dialog->window);
729 free(dialog);
730}
731
732static void
733unlock_dialog_finish(struct task *task, uint32_t events)
734{
735 struct desktop *desktop =
Benjamin Franzked7759712011-11-22 12:38:48 +0100736 container_of(task, struct desktop, unlock_task);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200737
738 desktop_shell_unlock(desktop->shell);
739 unlock_dialog_destroy(desktop->unlock_dialog);
740 desktop->unlock_dialog = NULL;
741}
742
743static void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400744desktop_shell_configure(void *data,
745 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400746 uint32_t edges,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200747 struct wl_shell_surface *shell_surface,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400748 int32_t width, int32_t height)
749{
Pekka Paalanen068ae942011-11-28 14:11:15 +0200750 struct window *window = wl_shell_surface_get_user_data(shell_surface);
751 struct surface *s = window_get_user_data(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400752
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400753 s->configure(data, desktop_shell, edges, window, width, height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400754}
755
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200756static void
757desktop_shell_prepare_lock_surface(void *data,
758 struct desktop_shell *desktop_shell)
759{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200760 struct desktop *desktop = data;
761
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200762 if (!key_locking) {
763 desktop_shell_unlock(desktop->shell);
764 return;
765 }
766
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200767 if (!desktop->unlock_dialog) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500768 desktop->unlock_dialog = unlock_dialog_create(desktop);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200769 desktop->unlock_dialog->desktop = desktop;
770 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200771}
772
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400773static const struct desktop_shell_listener listener = {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200774 desktop_shell_configure,
775 desktop_shell_prepare_lock_surface
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400776};
777
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100778static struct background *
779background_create(struct desktop *desktop)
780{
781 struct background *background;
782
783 background = malloc(sizeof *background);
784 memset(background, 0, sizeof *background);
785
786 background->base.configure = background_configure;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500787 background->window = window_create(desktop->display);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500788 background->widget = window_add_widget(background->window, background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100789 window_set_custom(background->window);
790 window_set_user_data(background->window, background);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500791 widget_set_redraw_handler(background->widget, background_draw);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100792
793 return background;
794}
795
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400796static const struct wl_callback_listener busy_cursor_listener;
797
798static void
799busy_cursor_frame_callback(void *data,
800 struct wl_callback *callback, uint32_t time)
801{
802 struct input *input = data;
803 struct display *display = input_get_display(input);
804 struct desktop *desktop = display_get_user_data(display);
805 struct wl_surface *surface;
806 int index;
807
808 if (callback)
809 wl_callback_destroy(callback);
810 if (input_get_focus_widget(input) != desktop->busy_widget)
811 return;
812
813 /* FIXME: Get frame duration and number of frames from cursor. */
814 index = (time / 100) % 8;
815 input_set_pointer_image_index(input, CURSOR_WATCH, index);
816
817 surface = window_get_wl_surface(desktop->busy_window);
818 callback = wl_surface_frame(surface);
819 wl_callback_add_listener(callback, &busy_cursor_listener, input);
820}
821
822static const struct wl_callback_listener busy_cursor_listener = {
823 busy_cursor_frame_callback
824};
825
826static int
827busy_surface_enter_handler(struct widget *widget, struct input *input,
828 float x, float y, void *data)
829{
830 busy_cursor_frame_callback(input, NULL, 0);
831
832 return CURSOR_WATCH;
833}
834
835static void
836busy_surface_create(struct desktop *desktop)
837{
838 struct wl_surface *s;
839
840 desktop->busy_window = window_create(desktop->display);
841 s = window_get_wl_surface(desktop->busy_window);
842 desktop_shell_set_busy_surface(desktop->shell, s);
843
844 desktop->busy_widget =
845 window_add_widget(desktop->busy_window, desktop);
846 /* We set the allocation to 1x1 at 0,0 so the fake enter event
847 * at 0,0 will go to this widget. */
848 widget_set_allocation(desktop->busy_widget, 0, 0, 1, 1);
849
850 widget_set_enter_handler(desktop->busy_widget,
851 busy_surface_enter_handler);
852}
853
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100854static void
855create_output(struct desktop *desktop, uint32_t id)
856{
857 struct output *output;
858
859 output = calloc(1, sizeof *output);
860 if (!output)
861 return;
862
863 output->output = wl_display_bind(display_get_display(desktop->display),
864 id, &wl_output_interface);
865
866 wl_list_insert(&desktop->outputs, &output->link);
867}
868
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400869static void
870global_handler(struct wl_display *display, uint32_t id,
871 const char *interface, uint32_t version, void *data)
872{
873 struct desktop *desktop = data;
874
875 if (!strcmp(interface, "desktop_shell")) {
876 desktop->shell =
877 wl_display_bind(display, id, &desktop_shell_interface);
878 desktop_shell_add_listener(desktop->shell, &listener, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100879 } else if (!strcmp(interface, "wl_output")) {
880 create_output(desktop, id);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400881 }
882}
883
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500884static void
885launcher_section_done(void *data)
886{
887 struct desktop *desktop = data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100888 struct output *output;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500889
890 if (key_launcher_icon == NULL || key_launcher_path == NULL) {
891 fprintf(stderr, "invalid launcher section\n");
892 return;
893 }
894
Martin Minarik1e51a872012-06-08 00:39:11 +0200895 wl_list_for_each(output, &desktop->outputs, link) {
Kristian Høgsberg53880802012-01-09 11:16:50 -0500896 panel_add_launcher(output->panel,
897 key_launcher_icon, key_launcher_path);
Martin Minarik1e51a872012-06-08 00:39:11 +0200898 panel_add_clock(output->panel);
899 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100900
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500901 free(key_launcher_icon);
902 key_launcher_icon = NULL;
903 free(key_launcher_path);
904 key_launcher_path = NULL;
905}
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400906
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500907static void
908add_default_launcher(struct desktop *desktop)
909{
910 struct output *output;
911
912 wl_list_for_each(output, &desktop->outputs, link)
913 panel_add_launcher(output->panel,
914 DATADIR "/weston/terminal.png",
Rodney Lorrimar99ff01b2012-02-29 17:31:03 +0100915 BINDIR "/weston-terminal");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500916}
917
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400918int main(int argc, char *argv[])
919{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200920 struct desktop desktop = { 0 };
Pekka Paalanen668dd562011-11-15 11:45:40 +0200921 char *config_file;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100922 struct output *output;
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500923 int ret;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400924
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200925 desktop.unlock_task.run = unlock_dialog_finish;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100926 wl_list_init(&desktop.outputs);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200927
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400928 desktop.display = display_create(argc, argv);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400929 if (desktop.display == NULL) {
930 fprintf(stderr, "failed to create display: %m\n");
931 return -1;
932 }
933
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400934 display_set_user_data(desktop.display, &desktop);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400935 wl_display_add_global_listener(display_get_display(desktop.display),
936 global_handler, &desktop);
937
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100938 wl_list_for_each(output, &desktop.outputs, link) {
Pekka Paalanen068ae942011-11-28 14:11:15 +0200939 struct wl_shell_surface *s;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100940
941 output->panel = panel_create(desktop.display);
Pekka Paalanen068ae942011-11-28 14:11:15 +0200942 s = window_get_wl_shell_surface(output->panel->window);
943 desktop_shell_set_panel(desktop.shell, output->output, s);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100944
945 output->background = background_create(&desktop);
Pekka Paalanen068ae942011-11-28 14:11:15 +0200946 s = window_get_wl_shell_surface(output->background->window);
947 desktop_shell_set_background(desktop.shell, output->output, s);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100948 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400949
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400950 busy_surface_create(&desktop);
951
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200952 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500953 ret = parse_config_file(config_file,
954 config_sections, ARRAY_LENGTH(config_sections),
955 &desktop);
Pekka Paalanen668dd562011-11-15 11:45:40 +0200956 free(config_file);
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500957 if (ret < 0)
958 add_default_launcher(&desktop);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500959
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400960 signal(SIGCHLD, sigchild_handler);
961
962 display_run(desktop.display);
963
964 return 0;
965}