blob: 41d02d0212cc5825094463ff586393d246f556e6 [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>
Tiago Vignatti61500722012-05-23 22:06:28 +030034#include <libgen.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040035
Pekka Paalanen50719bc2011-11-22 14:18:50 +020036#include <wayland-client.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040037#include "window.h"
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040038#include "../shared/cairo-util.h"
Kristian Høgsberg9b935c82011-12-08 12:44:27 -050039#include "../shared/config-parser.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040040
Pekka Paalanen50719bc2011-11-22 14:18:50 +020041#include "desktop-shell-client-protocol.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040042
43struct desktop {
44 struct display *display;
45 struct desktop_shell *shell;
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,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040054 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010055 int32_t width, int32_t height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040056};
57
58struct panel {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010059 struct surface base;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040060 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050061 struct widget *widget;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050062 struct wl_list launcher_list;
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;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050068 struct widget *widget;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010069};
70
71struct output {
72 struct wl_output *output;
73 struct wl_list link;
74
75 struct panel *panel;
76 struct background *background;
77};
78
Kristian Høgsberg53880802012-01-09 11:16:50 -050079struct panel_launcher {
Kristian Høgsbergc51f7992012-01-08 15:09:53 -050080 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -040081 struct panel *panel;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040082 cairo_surface_t *icon;
Kristian Høgsbergb6323512012-01-11 00:04:42 -050083 int focused, pressed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040084 const char *path;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050085 struct wl_list link;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040086};
87
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +020088struct unlock_dialog {
89 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050090 struct widget *widget;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -050091 struct widget *button;
Kristian Høgsbergb6323512012-01-11 00:04:42 -050092 int button_focused;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +020093 int closing;
94
95 struct desktop *desktop;
96};
97
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -050098static char *key_background_image = DATADIR "/weston/pattern.png";
99static char *key_background_type = "tile";
100static uint32_t key_panel_color = 0xaa000000;
101static uint32_t key_background_color = 0xff002244;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500102static char *key_launcher_icon;
103static char *key_launcher_path;
104static void launcher_section_done(void *data);
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200105static int key_locking = 1;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500106
107static const struct config_key shell_config_keys[] = {
108 { "background-image", CONFIG_KEY_STRING, &key_background_image },
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500109 { "background-type", CONFIG_KEY_STRING, &key_background_type },
Scott Moreaufa1de692012-01-27 13:25:49 -0700110 { "panel-color", CONFIG_KEY_UNSIGNED_INTEGER, &key_panel_color },
111 { "background-color", CONFIG_KEY_UNSIGNED_INTEGER, &key_background_color },
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200112 { "locking", CONFIG_KEY_BOOLEAN, &key_locking },
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500113};
114
115static const struct config_key launcher_config_keys[] = {
116 { "icon", CONFIG_KEY_STRING, &key_launcher_icon },
117 { "path", CONFIG_KEY_STRING, &key_launcher_path },
118};
119
120static const struct config_section config_sections[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200121 { "shell",
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500122 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
123 { "launcher",
124 launcher_config_keys, ARRAY_LENGTH(launcher_config_keys),
125 launcher_section_done }
126};
127
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400128static void
129sigchild_handler(int s)
130{
131 int status;
132 pid_t pid;
133
134 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
135 fprintf(stderr, "child %d exited\n", pid);
136}
137
138static void
Pekka Paalanen9e30a822012-01-19 16:40:28 +0200139menu_func(struct window *window, int index, void *data)
140{
141 printf("Selected index %d from a panel menu.\n", index);
142}
143
144static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500145show_menu(struct panel *panel, struct input *input, uint32_t time)
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400146{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500147 int32_t x, y;
148 static const char *entries[] = {
149 "Roy", "Pris", "Leon", "Zhora"
150 };
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400151
152 input_get_position(input, &x, &y);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +0200153 window_show_menu(window_get_display(panel->window),
154 input, time, panel->window,
Pekka Paalanen9e30a822012-01-19 16:40:28 +0200155 x - 10, y - 10, menu_func, entries, 4);
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400156}
157
158static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500159panel_launcher_activate(struct panel_launcher *widget)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400160{
161 pid_t pid;
162
163 pid = fork();
164 if (pid < 0) {
165 fprintf(stderr, "fork failed: %m\n");
166 return;
167 }
168
169 if (pid)
170 return;
Benjamin Franzked7759712011-11-22 12:38:48 +0100171
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500172 if (execl(widget->path, widget->path, NULL) < 0) {
Pekka Paalanena291ae52012-01-27 09:50:02 +0200173 fprintf(stderr, "execl '%s' failed: %m\n", widget->path);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400174 exit(1);
175 }
176}
177
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400178static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500179panel_launcher_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400180{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500181 struct panel_launcher *launcher = data;
182 cairo_surface_t *surface;
183 struct rectangle allocation;
184 cairo_t *cr;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400185
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500186 surface = window_get_surface(launcher->panel->window);
187 cr = cairo_create(surface);
188
189 widget_get_allocation(widget, &allocation);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500190 if (launcher->pressed) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500191 allocation.x++;
192 allocation.y++;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400193 }
194
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500195 cairo_set_source_surface(cr, launcher->icon,
196 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400197 cairo_paint(cr);
198
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500199 if (launcher->focused) {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400200 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500201 cairo_mask_surface(cr, launcher->icon,
202 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400203 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400204
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500205 cairo_destroy(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400206}
207
Tiago Vignatti61500722012-05-23 22:06:28 +0300208static int
209panel_launcher_motion_handler(struct widget *widget, struct input *input,
210 uint32_t time, float x, float y, void *data)
211{
212 struct panel_launcher *launcher = data;
213
214 widget_set_tooltip(widget, basename((char *)launcher->path), x, y);
215
216 return POINTER_LEFT_PTR;
217}
218
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400219static void
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500220set_hex_color(cairo_t *cr, uint32_t color)
221{
222 cairo_set_source_rgba(cr,
223 ((color >> 16) & 0xff) / 255.0,
224 ((color >> 8) & 0xff) / 255.0,
225 ((color >> 0) & 0xff) / 255.0,
226 ((color >> 24) & 0xff) / 255.0);
227}
228
229static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500230panel_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400231{
232 cairo_surface_t *surface;
233 cairo_t *cr;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500234 struct panel *panel = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400235
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500236 surface = window_get_surface(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400237 cr = cairo_create(surface);
238 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500239 set_hex_color(cr, key_panel_color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400240 cairo_paint(cr);
241
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400242 cairo_destroy(cr);
243 cairo_surface_destroy(surface);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400244}
245
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500246static int
Kristian Høgsberg53880802012-01-09 11:16:50 -0500247panel_launcher_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400248 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500249{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500250 struct panel_launcher *launcher = data;
251
252 launcher->focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500253 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500254
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300255 return POINTER_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500256}
257
258static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500259panel_launcher_leave_handler(struct widget *widget,
260 struct input *input, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400261{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500262 struct panel_launcher *launcher = data;
263
264 launcher->focused = 0;
Tiago Vignatti61500722012-05-23 22:06:28 +0300265 widget_destroy_tooltip(widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500266 widget_schedule_redraw(widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400267}
268
269static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500270panel_launcher_button_handler(struct widget *widget,
271 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +0100272 uint32_t button, uint32_t state, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400273{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500274 struct panel_launcher *launcher;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400275
Kristian Høgsberg53880802012-01-09 11:16:50 -0500276 launcher = widget_get_user_data(widget);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500277 widget_schedule_redraw(widget);
278 if (state == 0)
Kristian Høgsberg53880802012-01-09 11:16:50 -0500279 panel_launcher_activate(launcher);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500280}
281
282static void
283panel_button_handler(struct widget *widget,
284 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +0100285 uint32_t button, uint32_t state, void *data)
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500286{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500287 struct panel *panel = data;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500288
289 if (button == BTN_RIGHT && state)
290 show_menu(panel, input, time);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400291}
292
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100293static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500294panel_resize_handler(struct widget *widget,
295 int32_t width, int32_t height, void *data)
296{
297 struct panel_launcher *launcher;
298 struct panel *panel = data;
299 int x, y, w, h;
300
301 x = 10;
302 y = 16;
303 wl_list_for_each(launcher, &panel->launcher_list, link) {
304 w = cairo_image_surface_get_width(launcher->icon);
305 h = cairo_image_surface_get_height(launcher->icon);
306 widget_set_allocation(launcher->widget,
307 x, y - h / 2, w + 1, h + 1);
308 x += w + 10;
309 }
310}
311
312static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100313panel_configure(void *data,
314 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400315 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100316 int32_t width, int32_t height)
317{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500318 struct surface *surface = window_get_user_data(window);
319 struct panel *panel = container_of(surface, struct panel, base);
320
321 window_schedule_resize(panel->window, width, 32);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100322}
323
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400324static struct panel *
325panel_create(struct display *display)
326{
327 struct panel *panel;
328
329 panel = malloc(sizeof *panel);
330 memset(panel, 0, sizeof *panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400331
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100332 panel->base.configure = panel_configure;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500333 panel->window = window_create(display);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500334 panel->widget = window_add_widget(panel->window, panel);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500335 wl_list_init(&panel->launcher_list);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400336
337 window_set_title(panel->window, "panel");
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400338 window_set_custom(panel->window);
339 window_set_user_data(panel->window, panel);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500340
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500341 widget_set_redraw_handler(panel->widget, panel_redraw_handler);
342 widget_set_resize_handler(panel->widget, panel_resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500343 widget_set_button_handler(panel->widget, panel_button_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400344
345 return panel;
346}
347
348static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500349panel_add_launcher(struct panel *panel, const char *icon, const char *path)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400350{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500351 struct panel_launcher *launcher;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400352
Kristian Høgsberg53880802012-01-09 11:16:50 -0500353 launcher = malloc(sizeof *launcher);
354 memset(launcher, 0, sizeof *launcher);
355 launcher->icon = cairo_image_surface_create_from_png(icon);
356 launcher->path = strdup(path);
357 launcher->panel = panel;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500358 wl_list_insert(panel->launcher_list.prev, &launcher->link);
359
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500360 launcher->widget = widget_add_widget(panel->widget, launcher);
Kristian Høgsberg53880802012-01-09 11:16:50 -0500361 widget_set_enter_handler(launcher->widget,
362 panel_launcher_enter_handler);
363 widget_set_leave_handler(launcher->widget,
364 panel_launcher_leave_handler);
365 widget_set_button_handler(launcher->widget,
366 panel_launcher_button_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500367 widget_set_redraw_handler(launcher->widget,
368 panel_launcher_redraw_handler);
Tiago Vignatti61500722012-05-23 22:06:28 +0300369 widget_set_motion_handler(launcher->widget,
370 panel_launcher_motion_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400371}
372
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500373enum {
374 BACKGROUND_SCALE,
375 BACKGROUND_TILE
376};
377
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400378static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500379background_draw(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400380{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500381 struct background *background = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400382 cairo_surface_t *surface, *image;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400383 cairo_pattern_t *pattern;
384 cairo_matrix_t matrix;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400385 cairo_t *cr;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400386 double sx, sy;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500387 struct rectangle allocation;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500388 int type = -1;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400389
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500390 surface = window_get_surface(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400391
392 cr = cairo_create(surface);
393 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
394 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
395 cairo_paint(cr);
396
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500397 widget_get_allocation(widget, &allocation);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500398 image = NULL;
399 if (key_background_image)
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400400 image = load_cairo_surface(key_background_image);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500401
402 if (strcmp(key_background_type, "scale") == 0)
403 type = BACKGROUND_SCALE;
404 else if (strcmp(key_background_type, "tile") == 0)
405 type = BACKGROUND_TILE;
406 else
407 fprintf(stderr, "invalid background-type: %s\n",
408 key_background_type);
409
410 if (image && type != -1) {
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400411 pattern = cairo_pattern_create_for_surface(image);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500412 switch (type) {
413 case BACKGROUND_SCALE:
414 sx = (double) cairo_image_surface_get_width(image) /
415 allocation.width;
416 sy = (double) cairo_image_surface_get_height(image) /
417 allocation.height;
418 cairo_matrix_init_scale(&matrix, sx, sy);
419 cairo_pattern_set_matrix(pattern, &matrix);
420 break;
421 case BACKGROUND_TILE:
422 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
423 break;
424 }
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400425 cairo_set_source(cr, pattern);
426 cairo_pattern_destroy (pattern);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400427 cairo_surface_destroy(image);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500428 } else {
429 set_hex_color(cr, key_background_color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400430 }
431
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500432 cairo_paint(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400433 cairo_destroy(cr);
434 cairo_surface_destroy(surface);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400435}
436
437static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100438background_configure(void *data,
439 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400440 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100441 int32_t width, int32_t height)
442{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500443 struct background *background =
444 (struct background *) window_get_user_data(window);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100445
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500446 widget_schedule_resize(background->widget, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100447}
448
449static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500450unlock_dialog_redraw_handler(struct widget *widget, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200451{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500452 struct unlock_dialog *dialog = data;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200453 struct rectangle allocation;
454 cairo_t *cr;
455 cairo_surface_t *surface;
456 cairo_pattern_t *pat;
457 double cx, cy, r, f;
458
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200459 surface = window_get_surface(dialog->window);
460 cr = cairo_create(surface);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500461
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500462 widget_get_allocation(dialog->widget, &allocation);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200463 cairo_rectangle(cr, allocation.x, allocation.y,
464 allocation.width, allocation.height);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200465 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
466 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500467 cairo_fill(cr);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200468
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500469 cairo_translate(cr, allocation.x, allocation.y);
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500470 if (dialog->button_focused)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200471 f = 1.0;
472 else
473 f = 0.7;
474
475 cx = allocation.width / 2.0;
476 cy = allocation.height / 2.0;
477 r = (cx < cy ? cx : cy) * 0.4;
478 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
479 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
480 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
481 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
482 cairo_set_source(cr, pat);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500483 cairo_pattern_destroy(pat);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200484 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
485 cairo_fill(cr);
486
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500487 widget_set_allocation(dialog->button,
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500488 allocation.x + cx - r,
489 allocation.y + cy - r, 2 * r, 2 * r);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200490
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200491 cairo_destroy(cr);
492
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200493 cairo_surface_destroy(surface);
494}
495
496static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500497unlock_dialog_button_handler(struct widget *widget,
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200498 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +0100499 uint32_t button, uint32_t state, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200500{
501 struct unlock_dialog *dialog = data;
502 struct desktop *desktop = dialog->desktop;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200503
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500504 if (button == BTN_LEFT) {
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200505 if (state == 0 && !dialog->closing) {
506 display_defer(desktop->display, &desktop->unlock_task);
507 dialog->closing = 1;
508 }
509 }
510}
511
512static void
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200513unlock_dialog_keyboard_focus_handler(struct window *window,
514 struct input *device, void *data)
515{
516 window_schedule_redraw(window);
517}
518
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500519static int
Kristian Høgsbergee143232012-01-09 08:42:24 -0500520unlock_dialog_widget_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400521 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400522 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500523{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500524 struct unlock_dialog *dialog = data;
525
526 dialog->button_focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500527 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500528
Ander Conselvan de Oliveirad8f527c2012-05-25 09:30:02 +0300529 return POINTER_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500530}
531
532static void
533unlock_dialog_widget_leave_handler(struct widget *widget,
534 struct input *input, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200535{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500536 struct unlock_dialog *dialog = data;
537
538 dialog->button_focused = 0;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500539 widget_schedule_redraw(widget);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200540}
541
542static struct unlock_dialog *
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500543unlock_dialog_create(struct desktop *desktop)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200544{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500545 struct display *display = desktop->display;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200546 struct unlock_dialog *dialog;
547
548 dialog = malloc(sizeof *dialog);
549 if (!dialog)
550 return NULL;
551 memset(dialog, 0, sizeof *dialog);
552
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500553 dialog->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500554 dialog->widget = frame_create(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200555 window_set_title(dialog->window, "Unlock your desktop");
Benjamin Franzke8193bc12011-11-23 19:35:07 +0100556 window_set_custom(dialog->window);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200557
558 window_set_user_data(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200559 window_set_keyboard_focus_handler(dialog->window,
560 unlock_dialog_keyboard_focus_handler);
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500561 dialog->button = widget_add_widget(dialog->widget, dialog);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500562 widget_set_redraw_handler(dialog->widget,
563 unlock_dialog_redraw_handler);
Kristian Høgsbergee143232012-01-09 08:42:24 -0500564 widget_set_enter_handler(dialog->button,
565 unlock_dialog_widget_enter_handler);
566 widget_set_leave_handler(dialog->button,
567 unlock_dialog_widget_leave_handler);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500568 widget_set_button_handler(dialog->button,
569 unlock_dialog_button_handler);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200570
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500571 desktop_shell_set_lock_surface(desktop->shell,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200572 window_get_wl_shell_surface(dialog->window));
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500573
Pekka Paalanen40e49ac2012-01-18 16:51:30 +0200574 window_schedule_resize(dialog->window, 260, 230);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200575
576 return dialog;
577}
578
579static void
580unlock_dialog_destroy(struct unlock_dialog *dialog)
581{
582 window_destroy(dialog->window);
583 free(dialog);
584}
585
586static void
587unlock_dialog_finish(struct task *task, uint32_t events)
588{
589 struct desktop *desktop =
Benjamin Franzked7759712011-11-22 12:38:48 +0100590 container_of(task, struct desktop, unlock_task);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200591
592 desktop_shell_unlock(desktop->shell);
593 unlock_dialog_destroy(desktop->unlock_dialog);
594 desktop->unlock_dialog = NULL;
595}
596
597static void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400598desktop_shell_configure(void *data,
599 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400600 uint32_t edges,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200601 struct wl_shell_surface *shell_surface,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400602 int32_t width, int32_t height)
603{
Pekka Paalanen068ae942011-11-28 14:11:15 +0200604 struct window *window = wl_shell_surface_get_user_data(shell_surface);
605 struct surface *s = window_get_user_data(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400606
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400607 s->configure(data, desktop_shell, edges, window, width, height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400608}
609
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200610static void
611desktop_shell_prepare_lock_surface(void *data,
612 struct desktop_shell *desktop_shell)
613{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200614 struct desktop *desktop = data;
615
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200616 if (!key_locking) {
617 desktop_shell_unlock(desktop->shell);
618 return;
619 }
620
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200621 if (!desktop->unlock_dialog) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500622 desktop->unlock_dialog = unlock_dialog_create(desktop);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200623 desktop->unlock_dialog->desktop = desktop;
624 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200625}
626
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400627static const struct desktop_shell_listener listener = {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200628 desktop_shell_configure,
629 desktop_shell_prepare_lock_surface
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400630};
631
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100632static struct background *
633background_create(struct desktop *desktop)
634{
635 struct background *background;
636
637 background = malloc(sizeof *background);
638 memset(background, 0, sizeof *background);
639
640 background->base.configure = background_configure;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500641 background->window = window_create(desktop->display);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500642 background->widget = window_add_widget(background->window, background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100643 window_set_custom(background->window);
644 window_set_user_data(background->window, background);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500645 widget_set_redraw_handler(background->widget, background_draw);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100646
647 return background;
648}
649
650static void
651create_output(struct desktop *desktop, uint32_t id)
652{
653 struct output *output;
654
655 output = calloc(1, sizeof *output);
656 if (!output)
657 return;
658
659 output->output = wl_display_bind(display_get_display(desktop->display),
660 id, &wl_output_interface);
661
662 wl_list_insert(&desktop->outputs, &output->link);
663}
664
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400665static void
666global_handler(struct wl_display *display, uint32_t id,
667 const char *interface, uint32_t version, void *data)
668{
669 struct desktop *desktop = data;
670
671 if (!strcmp(interface, "desktop_shell")) {
672 desktop->shell =
673 wl_display_bind(display, id, &desktop_shell_interface);
674 desktop_shell_add_listener(desktop->shell, &listener, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100675 } else if (!strcmp(interface, "wl_output")) {
676 create_output(desktop, id);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400677 }
678}
679
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500680static void
681launcher_section_done(void *data)
682{
683 struct desktop *desktop = data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100684 struct output *output;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500685
686 if (key_launcher_icon == NULL || key_launcher_path == NULL) {
687 fprintf(stderr, "invalid launcher section\n");
688 return;
689 }
690
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100691 wl_list_for_each(output, &desktop->outputs, link)
Kristian Høgsberg53880802012-01-09 11:16:50 -0500692 panel_add_launcher(output->panel,
693 key_launcher_icon, key_launcher_path);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100694
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500695 free(key_launcher_icon);
696 key_launcher_icon = NULL;
697 free(key_launcher_path);
698 key_launcher_path = NULL;
699}
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400700
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500701static void
702add_default_launcher(struct desktop *desktop)
703{
704 struct output *output;
705
706 wl_list_for_each(output, &desktop->outputs, link)
707 panel_add_launcher(output->panel,
708 DATADIR "/weston/terminal.png",
Rodney Lorrimar99ff01b2012-02-29 17:31:03 +0100709 BINDIR "/weston-terminal");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500710}
711
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400712int main(int argc, char *argv[])
713{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200714 struct desktop desktop = { 0 };
Pekka Paalanen668dd562011-11-15 11:45:40 +0200715 char *config_file;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100716 struct output *output;
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500717 int ret;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400718
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200719 desktop.unlock_task.run = unlock_dialog_finish;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100720 wl_list_init(&desktop.outputs);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200721
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400722 desktop.display = display_create(argc, argv);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400723 if (desktop.display == NULL) {
724 fprintf(stderr, "failed to create display: %m\n");
725 return -1;
726 }
727
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400728 wl_display_add_global_listener(display_get_display(desktop.display),
729 global_handler, &desktop);
730
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100731 wl_list_for_each(output, &desktop.outputs, link) {
Pekka Paalanen068ae942011-11-28 14:11:15 +0200732 struct wl_shell_surface *s;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100733
734 output->panel = panel_create(desktop.display);
Pekka Paalanen068ae942011-11-28 14:11:15 +0200735 s = window_get_wl_shell_surface(output->panel->window);
736 desktop_shell_set_panel(desktop.shell, output->output, s);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100737
738 output->background = background_create(&desktop);
Pekka Paalanen068ae942011-11-28 14:11:15 +0200739 s = window_get_wl_shell_surface(output->background->window);
740 desktop_shell_set_background(desktop.shell, output->output, s);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100741 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400742
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200743 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500744 ret = parse_config_file(config_file,
745 config_sections, ARRAY_LENGTH(config_sections),
746 &desktop);
Pekka Paalanen668dd562011-11-15 11:45:40 +0200747 free(config_file);
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500748 if (ret < 0)
749 add_default_launcher(&desktop);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500750
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400751 signal(SIGCHLD, sigchild_handler);
752
753 display_run(desktop.display);
754
755 return 0;
756}