blob: 4a22f177b479252ce2c84240cf68fc81ea80e292 [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;
Martin Minarik1e51a872012-06-08 00:39:11 +020098 struct task clock_task;
99 int clock_fd;
100};
101
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200102struct unlock_dialog {
103 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500104 struct widget *widget;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500105 struct widget *button;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500106 int button_focused;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200107 int closing;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200108 struct desktop *desktop;
109};
110
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500111static char *key_background_image = DATADIR "/weston/pattern.png";
112static char *key_background_type = "tile";
113static uint32_t key_panel_color = 0xaa000000;
114static uint32_t key_background_color = 0xff002244;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500115static char *key_launcher_icon;
116static char *key_launcher_path;
117static void launcher_section_done(void *data);
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200118static int key_locking = 1;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500119
120static const struct config_key shell_config_keys[] = {
121 { "background-image", CONFIG_KEY_STRING, &key_background_image },
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500122 { "background-type", CONFIG_KEY_STRING, &key_background_type },
Scott Moreaufa1de692012-01-27 13:25:49 -0700123 { "panel-color", CONFIG_KEY_UNSIGNED_INTEGER, &key_panel_color },
124 { "background-color", CONFIG_KEY_UNSIGNED_INTEGER, &key_background_color },
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200125 { "locking", CONFIG_KEY_BOOLEAN, &key_locking },
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500126};
127
128static const struct config_key launcher_config_keys[] = {
129 { "icon", CONFIG_KEY_STRING, &key_launcher_icon },
130 { "path", CONFIG_KEY_STRING, &key_launcher_path },
131};
132
133static const struct config_section config_sections[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200134 { "shell",
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500135 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
136 { "launcher",
137 launcher_config_keys, ARRAY_LENGTH(launcher_config_keys),
138 launcher_section_done }
139};
140
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400141static void
142sigchild_handler(int s)
143{
144 int status;
145 pid_t pid;
146
147 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
148 fprintf(stderr, "child %d exited\n", pid);
149}
150
151static void
Pekka Paalanen9e30a822012-01-19 16:40:28 +0200152menu_func(struct window *window, int index, void *data)
153{
154 printf("Selected index %d from a panel menu.\n", index);
155}
156
157static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500158show_menu(struct panel *panel, struct input *input, uint32_t time)
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400159{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500160 int32_t x, y;
161 static const char *entries[] = {
162 "Roy", "Pris", "Leon", "Zhora"
163 };
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400164
165 input_get_position(input, &x, &y);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +0200166 window_show_menu(window_get_display(panel->window),
167 input, time, panel->window,
Pekka Paalanen9e30a822012-01-19 16:40:28 +0200168 x - 10, y - 10, menu_func, entries, 4);
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400169}
170
171static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500172panel_launcher_activate(struct panel_launcher *widget)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400173{
174 pid_t pid;
175
176 pid = fork();
177 if (pid < 0) {
178 fprintf(stderr, "fork failed: %m\n");
179 return;
180 }
181
182 if (pid)
183 return;
Benjamin Franzked7759712011-11-22 12:38:48 +0100184
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500185 if (execl(widget->path, widget->path, NULL) < 0) {
Pekka Paalanena291ae52012-01-27 09:50:02 +0200186 fprintf(stderr, "execl '%s' failed: %m\n", widget->path);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400187 exit(1);
188 }
189}
190
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400191static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500192panel_launcher_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400193{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500194 struct panel_launcher *launcher = data;
195 cairo_surface_t *surface;
196 struct rectangle allocation;
197 cairo_t *cr;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400198
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500199 surface = window_get_surface(launcher->panel->window);
200 cr = cairo_create(surface);
201
202 widget_get_allocation(widget, &allocation);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500203 if (launcher->pressed) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500204 allocation.x++;
205 allocation.y++;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400206 }
207
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500208 cairo_set_source_surface(cr, launcher->icon,
209 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400210 cairo_paint(cr);
211
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500212 if (launcher->focused) {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400213 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500214 cairo_mask_surface(cr, launcher->icon,
215 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400216 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400217
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500218 cairo_destroy(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400219}
220
Tiago Vignatti61500722012-05-23 22:06:28 +0300221static int
222panel_launcher_motion_handler(struct widget *widget, struct input *input,
223 uint32_t time, float x, float y, void *data)
224{
225 struct panel_launcher *launcher = data;
226
227 widget_set_tooltip(widget, basename((char *)launcher->path), x, y);
228
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300229 return CURSOR_LEFT_PTR;
Tiago Vignatti61500722012-05-23 22:06:28 +0300230}
231
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400232static void
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500233set_hex_color(cairo_t *cr, uint32_t color)
234{
235 cairo_set_source_rgba(cr,
236 ((color >> 16) & 0xff) / 255.0,
237 ((color >> 8) & 0xff) / 255.0,
238 ((color >> 0) & 0xff) / 255.0,
239 ((color >> 24) & 0xff) / 255.0);
240}
241
242static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500243panel_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400244{
245 cairo_surface_t *surface;
246 cairo_t *cr;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500247 struct panel *panel = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400248
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500249 surface = window_get_surface(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400250 cr = cairo_create(surface);
251 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500252 set_hex_color(cr, key_panel_color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400253 cairo_paint(cr);
254
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400255 cairo_destroy(cr);
256 cairo_surface_destroy(surface);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400257}
258
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500259static int
Kristian Høgsberg53880802012-01-09 11:16:50 -0500260panel_launcher_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400261 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500262{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500263 struct panel_launcher *launcher = data;
264
265 launcher->focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500266 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500267
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300268 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500269}
270
271static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500272panel_launcher_leave_handler(struct widget *widget,
273 struct input *input, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400274{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500275 struct panel_launcher *launcher = data;
276
277 launcher->focused = 0;
Tiago Vignatti61500722012-05-23 22:06:28 +0300278 widget_destroy_tooltip(widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500279 widget_schedule_redraw(widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400280}
281
282static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500283panel_launcher_button_handler(struct widget *widget,
284 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100285 uint32_t button,
286 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400287{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500288 struct panel_launcher *launcher;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400289
Kristian Høgsberg53880802012-01-09 11:16:50 -0500290 launcher = widget_get_user_data(widget);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500291 widget_schedule_redraw(widget);
Daniel Stone4dbadb12012-05-30 16:31:51 +0100292 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg53880802012-01-09 11:16:50 -0500293 panel_launcher_activate(launcher);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500294}
295
Martin Minarik1e51a872012-06-08 00:39:11 +0200296static void
297clock_func(struct task *task, uint32_t events)
298{
299 struct panel_clock *clock =
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400300 container_of(task, struct panel_clock, clock_task);
Kristian Høgsberg70226bb2012-06-08 16:54:52 -0400301 uint64_t exp;
Martin Minarik1e51a872012-06-08 00:39:11 +0200302
Kristian Høgsberg70226bb2012-06-08 16:54:52 -0400303 read(clock->clock_fd, &exp, sizeof exp);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400304 widget_schedule_redraw(clock->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200305}
306
307static void
308panel_clock_redraw_handler(struct widget *widget, void *data)
309{
310 cairo_surface_t *surface;
311 struct panel_clock *clock = data;
312 cairo_t *cr;
313 struct rectangle allocation;
314 cairo_text_extents_t extents;
315 cairo_font_extents_t font_extents;
Martin Minarik1e51a872012-06-08 00:39:11 +0200316 time_t rawtime;
317 struct tm * timeinfo;
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400318 char string[128];
Martin Minarik1e51a872012-06-08 00:39:11 +0200319
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400320 time(&rawtime);
321 timeinfo = localtime(&rawtime);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400322 strftime(string, sizeof string, "%a %b %d, %I:%M:%S %p", timeinfo);
Martin Minarik1e51a872012-06-08 00:39:11 +0200323
324 widget_get_allocation(widget, &allocation);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400325 if (allocation.width == 0)
326 return;
Martin Minarik1e51a872012-06-08 00:39:11 +0200327
328 surface = window_get_surface(clock->panel->window);
329 cr = cairo_create(surface);
330 cairo_rectangle(cr, allocation.x, allocation.y,
331 allocation.width, allocation.height);
332 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
333 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.1);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400334 cairo_rectangle(cr, allocation.x, allocation.y, 3, 3);
Martin Minarik1e51a872012-06-08 00:39:11 +0200335 cairo_fill(cr);
336
337 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
338 cairo_select_font_face(cr, "sans",
339 CAIRO_FONT_SLANT_NORMAL,
340 CAIRO_FONT_WEIGHT_NORMAL);
341 cairo_set_font_size(cr, 14);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400342 cairo_text_extents(cr, string, &extents);
Martin Minarik1e51a872012-06-08 00:39:11 +0200343 cairo_font_extents (cr, &font_extents);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400344 cairo_move_to(cr, allocation.x + 5,
345 allocation.y + 3 * (allocation.height >> 2) + 1);
346 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400347 cairo_show_text(cr, string);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400348 cairo_move_to(cr, allocation.x + 4,
349 allocation.y + 3 * (allocation.height >> 2));
350 cairo_set_source_rgb(cr, 1, 1, 1);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400351 cairo_show_text(cr, string);
Martin Minarik1e51a872012-06-08 00:39:11 +0200352 cairo_destroy(cr);
353}
354
355static int
356clock_timer_reset(struct panel_clock *clock)
357{
358 struct itimerspec its;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400359
Martin Minarik1e51a872012-06-08 00:39:11 +0200360 its.it_interval.tv_sec = 1;
361 its.it_interval.tv_nsec = 0;
Martin Minarik1e51a872012-06-08 00:39:11 +0200362 its.it_value.tv_sec = 1;
363 its.it_value.tv_nsec = 0;
364 if (timerfd_settime(clock->clock_fd, 0, &its, NULL) < 0) {
365 fprintf(stderr, "could not set timerfd\n: %m");
366 return -1;
367 }
368
369 return 0;
370}
371
372static void
373panel_add_clock(struct panel *panel)
374{
375 struct panel_clock *clock;
376 int timerfd;
377
378 timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
379 if (timerfd < 0) {
380 fprintf(stderr, "could not create timerfd\n: %m");
381 return;
382 }
383
384 clock = malloc(sizeof *clock);
385 memset(clock, 0, sizeof *clock);
386 clock->panel = panel;
387 panel->clock = clock;
388 clock->clock_fd = timerfd;
389
390 clock->clock_task.run = clock_func;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400391 display_watch_fd(window_get_display(panel->window), clock->clock_fd,
392 EPOLLIN, &clock->clock_task);
Martin Minarik1e51a872012-06-08 00:39:11 +0200393 clock_timer_reset(clock);
394
395 clock->widget = widget_add_widget(panel->widget, clock);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400396 widget_set_redraw_handler(clock->widget, panel_clock_redraw_handler);
Martin Minarik1e51a872012-06-08 00:39:11 +0200397}
398
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500399static void
400panel_button_handler(struct widget *widget,
401 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100402 uint32_t button,
403 enum wl_pointer_button_state state, void *data)
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500404{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500405 struct panel *panel = data;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500406
Daniel Stone4dbadb12012-05-30 16:31:51 +0100407 if (button == BTN_RIGHT && state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500408 show_menu(panel, input, time);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400409}
410
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100411static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500412panel_resize_handler(struct widget *widget,
413 int32_t width, int32_t height, void *data)
414{
415 struct panel_launcher *launcher;
416 struct panel *panel = data;
417 int x, y, w, h;
418
419 x = 10;
420 y = 16;
421 wl_list_for_each(launcher, &panel->launcher_list, link) {
422 w = cairo_image_surface_get_width(launcher->icon);
423 h = cairo_image_surface_get_height(launcher->icon);
424 widget_set_allocation(launcher->widget,
425 x, y - h / 2, w + 1, h + 1);
426 x += w + 10;
427 }
Martin Minarik1e51a872012-06-08 00:39:11 +0200428 h=20;
429 w=170;
430 widget_set_allocation(panel->clock->widget, width - w - 8, y - h / 2, w + 1, h + 1);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500431}
432
433static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100434panel_configure(void *data,
435 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400436 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100437 int32_t width, int32_t height)
438{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500439 struct surface *surface = window_get_user_data(window);
440 struct panel *panel = container_of(surface, struct panel, base);
441
442 window_schedule_resize(panel->window, width, 32);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100443}
444
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400445static struct panel *
446panel_create(struct display *display)
447{
448 struct panel *panel;
449
450 panel = malloc(sizeof *panel);
451 memset(panel, 0, sizeof *panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400452
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100453 panel->base.configure = panel_configure;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500454 panel->window = window_create(display);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500455 panel->widget = window_add_widget(panel->window, panel);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500456 wl_list_init(&panel->launcher_list);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400457
458 window_set_title(panel->window, "panel");
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400459 window_set_custom(panel->window);
460 window_set_user_data(panel->window, panel);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500461
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500462 widget_set_redraw_handler(panel->widget, panel_redraw_handler);
463 widget_set_resize_handler(panel->widget, panel_resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500464 widget_set_button_handler(panel->widget, panel_button_handler);
Rafal Mielniczukb9e513c2012-06-09 20:33:29 +0200465
466 panel_add_clock(panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400467
468 return panel;
469}
470
471static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500472panel_add_launcher(struct panel *panel, const char *icon, const char *path)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400473{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500474 struct panel_launcher *launcher;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400475
Kristian Høgsberg53880802012-01-09 11:16:50 -0500476 launcher = malloc(sizeof *launcher);
477 memset(launcher, 0, sizeof *launcher);
478 launcher->icon = cairo_image_surface_create_from_png(icon);
479 launcher->path = strdup(path);
480 launcher->panel = panel;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500481 wl_list_insert(panel->launcher_list.prev, &launcher->link);
482
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500483 launcher->widget = widget_add_widget(panel->widget, launcher);
Kristian Høgsberg53880802012-01-09 11:16:50 -0500484 widget_set_enter_handler(launcher->widget,
485 panel_launcher_enter_handler);
486 widget_set_leave_handler(launcher->widget,
487 panel_launcher_leave_handler);
488 widget_set_button_handler(launcher->widget,
489 panel_launcher_button_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500490 widget_set_redraw_handler(launcher->widget,
491 panel_launcher_redraw_handler);
Tiago Vignatti61500722012-05-23 22:06:28 +0300492 widget_set_motion_handler(launcher->widget,
493 panel_launcher_motion_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400494}
495
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500496enum {
497 BACKGROUND_SCALE,
498 BACKGROUND_TILE
499};
500
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400501static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500502background_draw(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400503{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500504 struct background *background = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400505 cairo_surface_t *surface, *image;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400506 cairo_pattern_t *pattern;
507 cairo_matrix_t matrix;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400508 cairo_t *cr;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400509 double sx, sy;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500510 struct rectangle allocation;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500511 int type = -1;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400512
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500513 surface = window_get_surface(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400514
515 cr = cairo_create(surface);
516 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
517 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
518 cairo_paint(cr);
519
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500520 widget_get_allocation(widget, &allocation);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500521 image = NULL;
522 if (key_background_image)
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400523 image = load_cairo_surface(key_background_image);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500524
525 if (strcmp(key_background_type, "scale") == 0)
526 type = BACKGROUND_SCALE;
527 else if (strcmp(key_background_type, "tile") == 0)
528 type = BACKGROUND_TILE;
529 else
530 fprintf(stderr, "invalid background-type: %s\n",
531 key_background_type);
532
533 if (image && type != -1) {
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400534 pattern = cairo_pattern_create_for_surface(image);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500535 switch (type) {
536 case BACKGROUND_SCALE:
537 sx = (double) cairo_image_surface_get_width(image) /
538 allocation.width;
539 sy = (double) cairo_image_surface_get_height(image) /
540 allocation.height;
541 cairo_matrix_init_scale(&matrix, sx, sy);
542 cairo_pattern_set_matrix(pattern, &matrix);
543 break;
544 case BACKGROUND_TILE:
545 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
546 break;
547 }
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400548 cairo_set_source(cr, pattern);
549 cairo_pattern_destroy (pattern);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400550 cairo_surface_destroy(image);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500551 } else {
552 set_hex_color(cr, key_background_color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400553 }
554
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500555 cairo_paint(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400556 cairo_destroy(cr);
557 cairo_surface_destroy(surface);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400558}
559
560static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100561background_configure(void *data,
562 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400563 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100564 int32_t width, int32_t height)
565{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500566 struct background *background =
567 (struct background *) window_get_user_data(window);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100568
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500569 widget_schedule_resize(background->widget, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100570}
571
572static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500573unlock_dialog_redraw_handler(struct widget *widget, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200574{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500575 struct unlock_dialog *dialog = data;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200576 struct rectangle allocation;
577 cairo_t *cr;
578 cairo_surface_t *surface;
579 cairo_pattern_t *pat;
580 double cx, cy, r, f;
581
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200582 surface = window_get_surface(dialog->window);
583 cr = cairo_create(surface);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500584
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500585 widget_get_allocation(dialog->widget, &allocation);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200586 cairo_rectangle(cr, allocation.x, allocation.y,
587 allocation.width, allocation.height);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200588 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
589 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500590 cairo_fill(cr);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200591
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500592 cairo_translate(cr, allocation.x, allocation.y);
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500593 if (dialog->button_focused)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200594 f = 1.0;
595 else
596 f = 0.7;
597
598 cx = allocation.width / 2.0;
599 cy = allocation.height / 2.0;
600 r = (cx < cy ? cx : cy) * 0.4;
601 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
602 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
603 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
604 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
605 cairo_set_source(cr, pat);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500606 cairo_pattern_destroy(pat);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200607 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
608 cairo_fill(cr);
609
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500610 widget_set_allocation(dialog->button,
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500611 allocation.x + cx - r,
612 allocation.y + cy - r, 2 * r, 2 * r);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200613
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200614 cairo_destroy(cr);
615
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200616 cairo_surface_destroy(surface);
617}
618
619static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500620unlock_dialog_button_handler(struct widget *widget,
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200621 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100622 uint32_t button,
623 enum wl_pointer_button_state state, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200624{
625 struct unlock_dialog *dialog = data;
626 struct desktop *desktop = dialog->desktop;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200627
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500628 if (button == BTN_LEFT) {
Daniel Stone4dbadb12012-05-30 16:31:51 +0100629 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
630 !dialog->closing) {
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200631 display_defer(desktop->display, &desktop->unlock_task);
632 dialog->closing = 1;
633 }
634 }
635}
636
637static void
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200638unlock_dialog_keyboard_focus_handler(struct window *window,
639 struct input *device, void *data)
640{
641 window_schedule_redraw(window);
642}
643
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500644static int
Kristian Høgsbergee143232012-01-09 08:42:24 -0500645unlock_dialog_widget_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400646 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400647 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500648{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500649 struct unlock_dialog *dialog = data;
650
651 dialog->button_focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500652 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500653
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300654 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500655}
656
657static void
658unlock_dialog_widget_leave_handler(struct widget *widget,
659 struct input *input, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200660{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500661 struct unlock_dialog *dialog = data;
662
663 dialog->button_focused = 0;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500664 widget_schedule_redraw(widget);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200665}
666
667static struct unlock_dialog *
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500668unlock_dialog_create(struct desktop *desktop)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200669{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500670 struct display *display = desktop->display;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200671 struct unlock_dialog *dialog;
672
673 dialog = malloc(sizeof *dialog);
674 if (!dialog)
675 return NULL;
676 memset(dialog, 0, sizeof *dialog);
677
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500678 dialog->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500679 dialog->widget = frame_create(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200680 window_set_title(dialog->window, "Unlock your desktop");
Benjamin Franzke8193bc12011-11-23 19:35:07 +0100681 window_set_custom(dialog->window);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200682
683 window_set_user_data(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200684 window_set_keyboard_focus_handler(dialog->window,
685 unlock_dialog_keyboard_focus_handler);
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500686 dialog->button = widget_add_widget(dialog->widget, dialog);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500687 widget_set_redraw_handler(dialog->widget,
688 unlock_dialog_redraw_handler);
Kristian Høgsbergee143232012-01-09 08:42:24 -0500689 widget_set_enter_handler(dialog->button,
690 unlock_dialog_widget_enter_handler);
691 widget_set_leave_handler(dialog->button,
692 unlock_dialog_widget_leave_handler);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500693 widget_set_button_handler(dialog->button,
694 unlock_dialog_button_handler);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200695
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500696 desktop_shell_set_lock_surface(desktop->shell,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200697 window_get_wl_shell_surface(dialog->window));
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500698
Pekka Paalanen40e49ac2012-01-18 16:51:30 +0200699 window_schedule_resize(dialog->window, 260, 230);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200700
701 return dialog;
702}
703
704static void
705unlock_dialog_destroy(struct unlock_dialog *dialog)
706{
707 window_destroy(dialog->window);
708 free(dialog);
709}
710
711static void
712unlock_dialog_finish(struct task *task, uint32_t events)
713{
714 struct desktop *desktop =
Benjamin Franzked7759712011-11-22 12:38:48 +0100715 container_of(task, struct desktop, unlock_task);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200716
717 desktop_shell_unlock(desktop->shell);
718 unlock_dialog_destroy(desktop->unlock_dialog);
719 desktop->unlock_dialog = NULL;
720}
721
722static void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400723desktop_shell_configure(void *data,
724 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400725 uint32_t edges,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200726 struct wl_shell_surface *shell_surface,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400727 int32_t width, int32_t height)
728{
Pekka Paalanen068ae942011-11-28 14:11:15 +0200729 struct window *window = wl_shell_surface_get_user_data(shell_surface);
730 struct surface *s = window_get_user_data(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400731
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400732 s->configure(data, desktop_shell, edges, window, width, height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400733}
734
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200735static void
736desktop_shell_prepare_lock_surface(void *data,
737 struct desktop_shell *desktop_shell)
738{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200739 struct desktop *desktop = data;
740
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200741 if (!key_locking) {
742 desktop_shell_unlock(desktop->shell);
743 return;
744 }
745
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200746 if (!desktop->unlock_dialog) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500747 desktop->unlock_dialog = unlock_dialog_create(desktop);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200748 desktop->unlock_dialog->desktop = desktop;
749 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200750}
751
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400752static const struct desktop_shell_listener listener = {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200753 desktop_shell_configure,
754 desktop_shell_prepare_lock_surface
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400755};
756
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100757static struct background *
758background_create(struct desktop *desktop)
759{
760 struct background *background;
761
762 background = malloc(sizeof *background);
763 memset(background, 0, sizeof *background);
764
765 background->base.configure = background_configure;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500766 background->window = window_create(desktop->display);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500767 background->widget = window_add_widget(background->window, background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100768 window_set_custom(background->window);
769 window_set_user_data(background->window, background);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500770 widget_set_redraw_handler(background->widget, background_draw);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100771
772 return background;
773}
774
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400775static const struct wl_callback_listener busy_cursor_listener;
776
777static void
778busy_cursor_frame_callback(void *data,
779 struct wl_callback *callback, uint32_t time)
780{
781 struct input *input = data;
782 struct display *display = input_get_display(input);
783 struct desktop *desktop = display_get_user_data(display);
784 struct wl_surface *surface;
785 int index;
786
787 if (callback)
788 wl_callback_destroy(callback);
789 if (input_get_focus_widget(input) != desktop->busy_widget)
790 return;
791
792 /* FIXME: Get frame duration and number of frames from cursor. */
793 index = (time / 100) % 8;
794 input_set_pointer_image_index(input, CURSOR_WATCH, index);
795
796 surface = window_get_wl_surface(desktop->busy_window);
797 callback = wl_surface_frame(surface);
798 wl_callback_add_listener(callback, &busy_cursor_listener, input);
799}
800
801static const struct wl_callback_listener busy_cursor_listener = {
802 busy_cursor_frame_callback
803};
804
805static int
806busy_surface_enter_handler(struct widget *widget, struct input *input,
807 float x, float y, void *data)
808{
809 busy_cursor_frame_callback(input, NULL, 0);
810
811 return CURSOR_WATCH;
812}
813
814static void
815busy_surface_create(struct desktop *desktop)
816{
817 struct wl_surface *s;
818
819 desktop->busy_window = window_create(desktop->display);
820 s = window_get_wl_surface(desktop->busy_window);
821 desktop_shell_set_busy_surface(desktop->shell, s);
822
823 desktop->busy_widget =
824 window_add_widget(desktop->busy_window, desktop);
825 /* We set the allocation to 1x1 at 0,0 so the fake enter event
826 * at 0,0 will go to this widget. */
827 widget_set_allocation(desktop->busy_widget, 0, 0, 1, 1);
828
829 widget_set_enter_handler(desktop->busy_widget,
830 busy_surface_enter_handler);
831}
832
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100833static void
834create_output(struct desktop *desktop, uint32_t id)
835{
836 struct output *output;
837
838 output = calloc(1, sizeof *output);
839 if (!output)
840 return;
841
842 output->output = wl_display_bind(display_get_display(desktop->display),
843 id, &wl_output_interface);
844
845 wl_list_insert(&desktop->outputs, &output->link);
846}
847
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400848static void
849global_handler(struct wl_display *display, uint32_t id,
850 const char *interface, uint32_t version, void *data)
851{
852 struct desktop *desktop = data;
853
854 if (!strcmp(interface, "desktop_shell")) {
855 desktop->shell =
856 wl_display_bind(display, id, &desktop_shell_interface);
857 desktop_shell_add_listener(desktop->shell, &listener, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100858 } else if (!strcmp(interface, "wl_output")) {
859 create_output(desktop, id);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400860 }
861}
862
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500863static void
864launcher_section_done(void *data)
865{
866 struct desktop *desktop = data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100867 struct output *output;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500868
869 if (key_launcher_icon == NULL || key_launcher_path == NULL) {
870 fprintf(stderr, "invalid launcher section\n");
871 return;
872 }
873
Martin Minarik1e51a872012-06-08 00:39:11 +0200874 wl_list_for_each(output, &desktop->outputs, link) {
Kristian Høgsberg53880802012-01-09 11:16:50 -0500875 panel_add_launcher(output->panel,
876 key_launcher_icon, key_launcher_path);
Martin Minarik1e51a872012-06-08 00:39:11 +0200877 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100878
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500879 free(key_launcher_icon);
880 key_launcher_icon = NULL;
881 free(key_launcher_path);
882 key_launcher_path = NULL;
883}
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400884
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500885static void
886add_default_launcher(struct desktop *desktop)
887{
888 struct output *output;
889
890 wl_list_for_each(output, &desktop->outputs, link)
891 panel_add_launcher(output->panel,
892 DATADIR "/weston/terminal.png",
Rodney Lorrimar99ff01b2012-02-29 17:31:03 +0100893 BINDIR "/weston-terminal");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500894}
895
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400896int main(int argc, char *argv[])
897{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200898 struct desktop desktop = { 0 };
Pekka Paalanen668dd562011-11-15 11:45:40 +0200899 char *config_file;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100900 struct output *output;
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500901 int ret;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400902
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200903 desktop.unlock_task.run = unlock_dialog_finish;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100904 wl_list_init(&desktop.outputs);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200905
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400906 desktop.display = display_create(argc, argv);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400907 if (desktop.display == NULL) {
908 fprintf(stderr, "failed to create display: %m\n");
909 return -1;
910 }
911
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400912 display_set_user_data(desktop.display, &desktop);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400913 wl_display_add_global_listener(display_get_display(desktop.display),
914 global_handler, &desktop);
915
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100916 wl_list_for_each(output, &desktop.outputs, link) {
Pekka Paalanen068ae942011-11-28 14:11:15 +0200917 struct wl_shell_surface *s;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100918
919 output->panel = panel_create(desktop.display);
Pekka Paalanen068ae942011-11-28 14:11:15 +0200920 s = window_get_wl_shell_surface(output->panel->window);
921 desktop_shell_set_panel(desktop.shell, output->output, s);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100922
923 output->background = background_create(&desktop);
Pekka Paalanen068ae942011-11-28 14:11:15 +0200924 s = window_get_wl_shell_surface(output->background->window);
925 desktop_shell_set_background(desktop.shell, output->output, s);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100926 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400927
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400928 busy_surface_create(&desktop);
929
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200930 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500931 ret = parse_config_file(config_file,
932 config_sections, ARRAY_LENGTH(config_sections),
933 &desktop);
Pekka Paalanen668dd562011-11-15 11:45:40 +0200934 free(config_file);
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500935 if (ret < 0)
936 add_default_launcher(&desktop);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500937
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400938 signal(SIGCHLD, sigchild_handler);
939
940 display_run(desktop.display);
941
942 return 0;
943}