blob: 497e4ffdb80f8e4ce43916d26f3b37d10757261c [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>
Kristian Høgsbergd1936b92012-07-23 22:59:33 -040037#include <ctype.h>
Martin Minarik1e51a872012-06-08 00:39:11 +020038#include <time.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040039
Pekka Paalanen50719bc2011-11-22 14:18:50 +020040#include <wayland-client.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040041#include "window.h"
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040042#include "../shared/cairo-util.h"
Kristian Høgsberg9b935c82011-12-08 12:44:27 -050043#include "../shared/config-parser.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040044
Pekka Paalanen50719bc2011-11-22 14:18:50 +020045#include "desktop-shell-client-protocol.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040046
Pekka Paalanenb6df4f72012-08-03 14:39:15 +030047extern char **environ; /* defined by libc */
48
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040049struct desktop {
50 struct display *display;
51 struct desktop_shell *shell;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +020052 struct unlock_dialog *unlock_dialog;
53 struct task unlock_task;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010054 struct wl_list outputs;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040055
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030056 struct window *grab_window;
57 struct widget *grab_widget;
58
Scott Moreauec116022012-07-22 18:23:52 -060059 enum cursor_type grab_cursor;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010060};
61
62struct surface {
63 void (*configure)(void *data,
64 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040065 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010066 int32_t width, int32_t height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040067};
68
69struct panel {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010070 struct surface base;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040071 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050072 struct widget *widget;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050073 struct wl_list launcher_list;
Martin Minarik1e51a872012-06-08 00:39:11 +020074 struct panel_clock *clock;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040075};
76
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010077struct background {
78 struct surface base;
79 struct window *window;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050080 struct widget *widget;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010081};
82
83struct output {
84 struct wl_output *output;
85 struct wl_list link;
86
87 struct panel *panel;
88 struct background *background;
89};
90
Kristian Høgsberg53880802012-01-09 11:16:50 -050091struct panel_launcher {
Kristian Høgsbergc51f7992012-01-08 15:09:53 -050092 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -040093 struct panel *panel;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040094 cairo_surface_t *icon;
Kristian Høgsbergb6323512012-01-11 00:04:42 -050095 int focused, pressed;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -040096 char *path;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050097 struct wl_list link;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -040098 struct wl_array envp;
99 struct wl_array argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400100};
101
Martin Minarik1e51a872012-06-08 00:39:11 +0200102struct panel_clock {
103 struct widget *widget;
104 struct panel *panel;
Martin Minarik1e51a872012-06-08 00:39:11 +0200105 struct task clock_task;
106 int clock_fd;
107};
108
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200109struct unlock_dialog {
110 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500111 struct widget *widget;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500112 struct widget *button;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500113 int button_focused;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200114 int closing;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200115 struct desktop *desktop;
116};
117
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500118static char *key_background_image = DATADIR "/weston/pattern.png";
119static char *key_background_type = "tile";
120static uint32_t key_panel_color = 0xaa000000;
121static uint32_t key_background_color = 0xff002244;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500122static char *key_launcher_icon;
123static char *key_launcher_path;
124static void launcher_section_done(void *data);
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200125static int key_locking = 1;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500126
127static const struct config_key shell_config_keys[] = {
128 { "background-image", CONFIG_KEY_STRING, &key_background_image },
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500129 { "background-type", CONFIG_KEY_STRING, &key_background_type },
Scott Moreaufa1de692012-01-27 13:25:49 -0700130 { "panel-color", CONFIG_KEY_UNSIGNED_INTEGER, &key_panel_color },
131 { "background-color", CONFIG_KEY_UNSIGNED_INTEGER, &key_background_color },
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200132 { "locking", CONFIG_KEY_BOOLEAN, &key_locking },
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500133};
134
135static const struct config_key launcher_config_keys[] = {
136 { "icon", CONFIG_KEY_STRING, &key_launcher_icon },
137 { "path", CONFIG_KEY_STRING, &key_launcher_path },
138};
139
140static const struct config_section config_sections[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200141 { "shell",
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500142 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
143 { "launcher",
144 launcher_config_keys, ARRAY_LENGTH(launcher_config_keys),
145 launcher_section_done }
146};
147
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400148static void
149sigchild_handler(int s)
150{
151 int status;
152 pid_t pid;
153
154 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
155 fprintf(stderr, "child %d exited\n", pid);
156}
157
158static void
Pekka Paalanen9e30a822012-01-19 16:40:28 +0200159menu_func(struct window *window, int index, void *data)
160{
161 printf("Selected index %d from a panel menu.\n", index);
162}
163
164static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500165show_menu(struct panel *panel, struct input *input, uint32_t time)
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400166{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500167 int32_t x, y;
168 static const char *entries[] = {
169 "Roy", "Pris", "Leon", "Zhora"
170 };
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400171
172 input_get_position(input, &x, &y);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +0200173 window_show_menu(window_get_display(panel->window),
174 input, time, panel->window,
Pekka Paalanen9e30a822012-01-19 16:40:28 +0200175 x - 10, y - 10, menu_func, entries, 4);
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400176}
177
178static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500179panel_launcher_activate(struct panel_launcher *widget)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400180{
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400181 char **argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400182 pid_t pid;
183
184 pid = fork();
185 if (pid < 0) {
186 fprintf(stderr, "fork failed: %m\n");
187 return;
188 }
189
190 if (pid)
191 return;
Benjamin Franzked7759712011-11-22 12:38:48 +0100192
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400193 argv = widget->argv.data;
194 if (execve(argv[0], argv, widget->envp.data) < 0) {
195 fprintf(stderr, "execl '%s' failed: %m\n", argv[0]);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400196 exit(1);
197 }
198}
199
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400200static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500201panel_launcher_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400202{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500203 struct panel_launcher *launcher = data;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500204 struct rectangle allocation;
205 cairo_t *cr;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400206
Alexander Larssonc584fa62013-05-22 14:41:32 +0200207 cr = widget_cairo_create(launcher->panel->widget);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500208
209 widget_get_allocation(widget, &allocation);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500210 if (launcher->pressed) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500211 allocation.x++;
212 allocation.y++;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400213 }
214
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500215 cairo_set_source_surface(cr, launcher->icon,
216 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400217 cairo_paint(cr);
218
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500219 if (launcher->focused) {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400220 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500221 cairo_mask_surface(cr, launcher->icon,
222 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400223 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400224
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500225 cairo_destroy(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400226}
227
Tiago Vignatti61500722012-05-23 22:06:28 +0300228static int
229panel_launcher_motion_handler(struct widget *widget, struct input *input,
230 uint32_t time, float x, float y, void *data)
231{
232 struct panel_launcher *launcher = data;
233
234 widget_set_tooltip(widget, basename((char *)launcher->path), x, y);
235
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300236 return CURSOR_LEFT_PTR;
Tiago Vignatti61500722012-05-23 22:06:28 +0300237}
238
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400239static void
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500240set_hex_color(cairo_t *cr, uint32_t color)
241{
242 cairo_set_source_rgba(cr,
243 ((color >> 16) & 0xff) / 255.0,
244 ((color >> 8) & 0xff) / 255.0,
245 ((color >> 0) & 0xff) / 255.0,
246 ((color >> 24) & 0xff) / 255.0);
247}
248
249static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500250panel_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400251{
252 cairo_surface_t *surface;
253 cairo_t *cr;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500254 struct panel *panel = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400255
Alexander Larssonc584fa62013-05-22 14:41:32 +0200256 cr = widget_cairo_create(panel->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400257 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500258 set_hex_color(cr, key_panel_color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400259 cairo_paint(cr);
260
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400261 cairo_destroy(cr);
Alexander Larssonc584fa62013-05-22 14:41:32 +0200262 surface = window_get_surface(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400263 cairo_surface_destroy(surface);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400264}
265
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500266static int
Kristian Høgsberg53880802012-01-09 11:16:50 -0500267panel_launcher_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400268 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500269{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500270 struct panel_launcher *launcher = data;
271
272 launcher->focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500273 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500274
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300275 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500276}
277
278static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500279panel_launcher_leave_handler(struct widget *widget,
280 struct input *input, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400281{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500282 struct panel_launcher *launcher = data;
283
284 launcher->focused = 0;
Tiago Vignatti61500722012-05-23 22:06:28 +0300285 widget_destroy_tooltip(widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500286 widget_schedule_redraw(widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400287}
288
289static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500290panel_launcher_button_handler(struct widget *widget,
291 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100292 uint32_t button,
293 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400294{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500295 struct panel_launcher *launcher;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400296
Kristian Høgsberg53880802012-01-09 11:16:50 -0500297 launcher = widget_get_user_data(widget);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500298 widget_schedule_redraw(widget);
Daniel Stone4dbadb12012-05-30 16:31:51 +0100299 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg53880802012-01-09 11:16:50 -0500300 panel_launcher_activate(launcher);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500301}
302
Martin Minarik1e51a872012-06-08 00:39:11 +0200303static void
304clock_func(struct task *task, uint32_t events)
305{
306 struct panel_clock *clock =
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400307 container_of(task, struct panel_clock, clock_task);
Kristian Høgsberg70226bb2012-06-08 16:54:52 -0400308 uint64_t exp;
Martin Minarik1e51a872012-06-08 00:39:11 +0200309
Martin Olsson8df662a2012-07-08 03:03:47 +0200310 if (read(clock->clock_fd, &exp, sizeof exp) != sizeof exp)
311 abort();
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400312 widget_schedule_redraw(clock->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200313}
314
315static void
316panel_clock_redraw_handler(struct widget *widget, void *data)
317{
Martin Minarik1e51a872012-06-08 00:39:11 +0200318 struct panel_clock *clock = data;
319 cairo_t *cr;
320 struct rectangle allocation;
321 cairo_text_extents_t extents;
322 cairo_font_extents_t font_extents;
Martin Minarik1e51a872012-06-08 00:39:11 +0200323 time_t rawtime;
324 struct tm * timeinfo;
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400325 char string[128];
Martin Minarik1e51a872012-06-08 00:39:11 +0200326
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400327 time(&rawtime);
328 timeinfo = localtime(&rawtime);
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400329 strftime(string, sizeof string, "%a %b %d, %I:%M %p", timeinfo);
Martin Minarik1e51a872012-06-08 00:39:11 +0200330
331 widget_get_allocation(widget, &allocation);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400332 if (allocation.width == 0)
333 return;
Martin Minarik1e51a872012-06-08 00:39:11 +0200334
Alexander Larssonc584fa62013-05-22 14:41:32 +0200335 cr = widget_cairo_create(clock->panel->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200336 cairo_select_font_face(cr, "sans",
337 CAIRO_FONT_SLANT_NORMAL,
338 CAIRO_FONT_WEIGHT_NORMAL);
339 cairo_set_font_size(cr, 14);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400340 cairo_text_extents(cr, string, &extents);
Martin Minarik1e51a872012-06-08 00:39:11 +0200341 cairo_font_extents (cr, &font_extents);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400342 cairo_move_to(cr, allocation.x + 5,
343 allocation.y + 3 * (allocation.height >> 2) + 1);
344 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400345 cairo_show_text(cr, string);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400346 cairo_move_to(cr, allocation.x + 4,
347 allocation.y + 3 * (allocation.height >> 2));
348 cairo_set_source_rgb(cr, 1, 1, 1);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400349 cairo_show_text(cr, string);
Martin Minarik1e51a872012-06-08 00:39:11 +0200350 cairo_destroy(cr);
351}
352
353static int
354clock_timer_reset(struct panel_clock *clock)
355{
356 struct itimerspec its;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400357
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400358 its.it_interval.tv_sec = 60;
Martin Minarik1e51a872012-06-08 00:39:11 +0200359 its.it_interval.tv_nsec = 0;
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400360 its.it_value.tv_sec = 60;
Martin Minarik1e51a872012-06-08 00:39:11 +0200361 its.it_value.tv_nsec = 0;
362 if (timerfd_settime(clock->clock_fd, 0, &its, NULL) < 0) {
363 fprintf(stderr, "could not set timerfd\n: %m");
364 return -1;
365 }
366
367 return 0;
368}
369
370static void
U. Artie Eoff44874d92012-10-02 21:12:35 -0700371panel_destroy_clock(struct panel_clock *clock)
372{
373 widget_destroy(clock->widget);
374
375 close(clock->clock_fd);
376
377 free(clock);
378}
379
380static void
Martin Minarik1e51a872012-06-08 00:39:11 +0200381panel_add_clock(struct panel *panel)
382{
383 struct panel_clock *clock;
384 int timerfd;
385
386 timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
387 if (timerfd < 0) {
388 fprintf(stderr, "could not create timerfd\n: %m");
389 return;
390 }
391
392 clock = malloc(sizeof *clock);
393 memset(clock, 0, sizeof *clock);
394 clock->panel = panel;
395 panel->clock = clock;
396 clock->clock_fd = timerfd;
397
398 clock->clock_task.run = clock_func;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400399 display_watch_fd(window_get_display(panel->window), clock->clock_fd,
400 EPOLLIN, &clock->clock_task);
Martin Minarik1e51a872012-06-08 00:39:11 +0200401 clock_timer_reset(clock);
402
403 clock->widget = widget_add_widget(panel->widget, clock);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400404 widget_set_redraw_handler(clock->widget, panel_clock_redraw_handler);
Martin Minarik1e51a872012-06-08 00:39:11 +0200405}
406
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500407static void
408panel_button_handler(struct widget *widget,
409 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100410 uint32_t button,
411 enum wl_pointer_button_state state, void *data)
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500412{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500413 struct panel *panel = data;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500414
Daniel Stone4dbadb12012-05-30 16:31:51 +0100415 if (button == BTN_RIGHT && state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500416 show_menu(panel, input, time);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400417}
418
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100419static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500420panel_resize_handler(struct widget *widget,
421 int32_t width, int32_t height, void *data)
422{
423 struct panel_launcher *launcher;
424 struct panel *panel = data;
425 int x, y, w, h;
426
427 x = 10;
428 y = 16;
429 wl_list_for_each(launcher, &panel->launcher_list, link) {
430 w = cairo_image_surface_get_width(launcher->icon);
431 h = cairo_image_surface_get_height(launcher->icon);
432 widget_set_allocation(launcher->widget,
433 x, y - h / 2, w + 1, h + 1);
434 x += w + 10;
435 }
Martin Minarik1e51a872012-06-08 00:39:11 +0200436 h=20;
437 w=170;
Pekka Paalanen01b17252012-06-12 17:42:26 +0300438
439 if (panel->clock)
440 widget_set_allocation(panel->clock->widget,
441 width - w - 8, y - h / 2, w + 1, h + 1);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500442}
443
444static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100445panel_configure(void *data,
446 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400447 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100448 int32_t width, int32_t height)
449{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500450 struct surface *surface = window_get_user_data(window);
451 struct panel *panel = container_of(surface, struct panel, base);
452
453 window_schedule_resize(panel->window, width, 32);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100454}
455
U. Artie Eoff44874d92012-10-02 21:12:35 -0700456static void
457panel_destroy_launcher(struct panel_launcher *launcher)
458{
459 wl_array_release(&launcher->argv);
460 wl_array_release(&launcher->envp);
461
462 free(launcher->path);
463
464 cairo_surface_destroy(launcher->icon);
465
466 widget_destroy(launcher->widget);
467 wl_list_remove(&launcher->link);
468
469 free(launcher);
470}
471
472static void
473panel_destroy(struct panel *panel)
474{
475 struct panel_launcher *tmp;
476 struct panel_launcher *launcher;
477
478 panel_destroy_clock(panel->clock);
479
480 wl_list_for_each_safe(launcher, tmp, &panel->launcher_list, link)
481 panel_destroy_launcher(launcher);
482
483 widget_destroy(panel->widget);
484 window_destroy(panel->window);
485
486 free(panel);
487}
488
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400489static struct panel *
490panel_create(struct display *display)
491{
492 struct panel *panel;
493
494 panel = malloc(sizeof *panel);
495 memset(panel, 0, sizeof *panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400496
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100497 panel->base.configure = panel_configure;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400498 panel->window = window_create_custom(display);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500499 panel->widget = window_add_widget(panel->window, panel);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500500 wl_list_init(&panel->launcher_list);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400501
502 window_set_title(panel->window, "panel");
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400503 window_set_user_data(panel->window, panel);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500504
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500505 widget_set_redraw_handler(panel->widget, panel_redraw_handler);
506 widget_set_resize_handler(panel->widget, panel_resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500507 widget_set_button_handler(panel->widget, panel_button_handler);
Rafal Mielniczukb9e513c2012-06-09 20:33:29 +0200508
509 panel_add_clock(panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400510
511 return panel;
512}
513
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200514static cairo_surface_t *
515load_icon_or_fallback(const char *icon)
516{
517 cairo_surface_t *surface = cairo_image_surface_create_from_png(icon);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200518 cairo_status_t status;
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200519 cairo_t *cr;
520
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200521 status = cairo_surface_status(surface);
522 if (status == CAIRO_STATUS_SUCCESS)
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200523 return surface;
524
525 cairo_surface_destroy(surface);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200526 fprintf(stderr, "ERROR loading icon from file '%s', error: '%s'\n",
527 icon, cairo_status_to_string(status));
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200528
529 /* draw fallback icon */
530 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
531 20, 20);
532 cr = cairo_create(surface);
533
534 cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1);
535 cairo_paint(cr);
536
537 cairo_set_source_rgba(cr, 0, 0, 0, 1);
538 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
539 cairo_rectangle(cr, 0, 0, 20, 20);
540 cairo_move_to(cr, 4, 4);
541 cairo_line_to(cr, 16, 16);
542 cairo_move_to(cr, 4, 16);
543 cairo_line_to(cr, 16, 4);
544 cairo_stroke(cr);
545
546 cairo_destroy(cr);
547
548 return surface;
549}
550
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400551static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500552panel_add_launcher(struct panel *panel, const char *icon, const char *path)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400553{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500554 struct panel_launcher *launcher;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400555 char *start, *p, *eq, **ps;
556 int i, j, k;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400557
Kristian Høgsberg53880802012-01-09 11:16:50 -0500558 launcher = malloc(sizeof *launcher);
559 memset(launcher, 0, sizeof *launcher);
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200560 launcher->icon = load_icon_or_fallback(icon);
Kristian Høgsberg53880802012-01-09 11:16:50 -0500561 launcher->path = strdup(path);
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400562
563 wl_array_init(&launcher->envp);
564 wl_array_init(&launcher->argv);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300565 for (i = 0; environ[i]; i++) {
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400566 ps = wl_array_add(&launcher->envp, sizeof *ps);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300567 *ps = environ[i];
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400568 }
569 j = 0;
570
571 start = launcher->path;
572 while (*start) {
573 for (p = start, eq = NULL; *p && !isspace(*p); p++)
574 if (*p == '=')
575 eq = p;
576
577 if (eq && j == 0) {
578 ps = launcher->envp.data;
579 for (k = 0; k < i; k++)
580 if (strncmp(ps[k], start, eq - start) == 0) {
581 ps[k] = start;
582 break;
583 }
584 if (k == i) {
585 ps = wl_array_add(&launcher->envp, sizeof *ps);
586 *ps = start;
587 i++;
588 }
589 } else {
590 ps = wl_array_add(&launcher->argv, sizeof *ps);
591 *ps = start;
592 j++;
593 }
594
595 while (*p && isspace(*p))
596 *p++ = '\0';
597
598 start = p;
599 }
600
601 ps = wl_array_add(&launcher->envp, sizeof *ps);
602 *ps = NULL;
603 ps = wl_array_add(&launcher->argv, sizeof *ps);
604 *ps = NULL;
605
Kristian Høgsberg53880802012-01-09 11:16:50 -0500606 launcher->panel = panel;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500607 wl_list_insert(panel->launcher_list.prev, &launcher->link);
608
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500609 launcher->widget = widget_add_widget(panel->widget, launcher);
Kristian Høgsberg53880802012-01-09 11:16:50 -0500610 widget_set_enter_handler(launcher->widget,
611 panel_launcher_enter_handler);
612 widget_set_leave_handler(launcher->widget,
613 panel_launcher_leave_handler);
614 widget_set_button_handler(launcher->widget,
615 panel_launcher_button_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500616 widget_set_redraw_handler(launcher->widget,
617 panel_launcher_redraw_handler);
Tiago Vignatti61500722012-05-23 22:06:28 +0300618 widget_set_motion_handler(launcher->widget,
619 panel_launcher_motion_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400620}
621
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500622enum {
623 BACKGROUND_SCALE,
624 BACKGROUND_TILE
625};
626
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400627static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500628background_draw(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400629{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500630 struct background *background = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400631 cairo_surface_t *surface, *image;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400632 cairo_pattern_t *pattern;
633 cairo_matrix_t matrix;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400634 cairo_t *cr;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400635 double sx, sy;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500636 struct rectangle allocation;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500637 int type = -1;
Pekka Paalanen9564c752012-10-24 09:43:08 +0300638 struct display *display;
639 struct wl_region *opaque;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400640
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500641 surface = window_get_surface(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400642
Alexander Larssonc584fa62013-05-22 14:41:32 +0200643 cr = widget_cairo_create(background->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400644 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
645 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
646 cairo_paint(cr);
647
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500648 widget_get_allocation(widget, &allocation);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500649 image = NULL;
650 if (key_background_image)
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400651 image = load_cairo_surface(key_background_image);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500652
653 if (strcmp(key_background_type, "scale") == 0)
654 type = BACKGROUND_SCALE;
655 else if (strcmp(key_background_type, "tile") == 0)
656 type = BACKGROUND_TILE;
657 else
658 fprintf(stderr, "invalid background-type: %s\n",
659 key_background_type);
660
661 if (image && type != -1) {
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400662 pattern = cairo_pattern_create_for_surface(image);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500663 switch (type) {
664 case BACKGROUND_SCALE:
665 sx = (double) cairo_image_surface_get_width(image) /
666 allocation.width;
667 sy = (double) cairo_image_surface_get_height(image) /
668 allocation.height;
669 cairo_matrix_init_scale(&matrix, sx, sy);
670 cairo_pattern_set_matrix(pattern, &matrix);
671 break;
672 case BACKGROUND_TILE:
673 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
674 break;
675 }
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400676 cairo_set_source(cr, pattern);
677 cairo_pattern_destroy (pattern);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400678 cairo_surface_destroy(image);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500679 } else {
680 set_hex_color(cr, key_background_color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400681 }
682
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500683 cairo_paint(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400684 cairo_destroy(cr);
685 cairo_surface_destroy(surface);
Pekka Paalanen9564c752012-10-24 09:43:08 +0300686
687 display = window_get_display(background->window);
688 opaque = wl_compositor_create_region(display_get_compositor(display));
689 wl_region_add(opaque, allocation.x, allocation.y,
690 allocation.width, allocation.height);
691 wl_surface_set_opaque_region(window_get_wl_surface(background->window), opaque);
692 wl_region_destroy(opaque);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400693}
694
695static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100696background_configure(void *data,
697 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400698 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100699 int32_t width, int32_t height)
700{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500701 struct background *background =
702 (struct background *) window_get_user_data(window);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100703
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500704 widget_schedule_resize(background->widget, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100705}
706
707static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500708unlock_dialog_redraw_handler(struct widget *widget, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200709{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500710 struct unlock_dialog *dialog = data;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200711 struct rectangle allocation;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200712 cairo_surface_t *surface;
Alexander Larssonc584fa62013-05-22 14:41:32 +0200713 cairo_t *cr;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200714 cairo_pattern_t *pat;
715 double cx, cy, r, f;
716
Alexander Larssonc584fa62013-05-22 14:41:32 +0200717 cr = widget_cairo_create(widget);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500718
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500719 widget_get_allocation(dialog->widget, &allocation);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200720 cairo_rectangle(cr, allocation.x, allocation.y,
721 allocation.width, allocation.height);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200722 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
723 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500724 cairo_fill(cr);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200725
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500726 cairo_translate(cr, allocation.x, allocation.y);
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500727 if (dialog->button_focused)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200728 f = 1.0;
729 else
730 f = 0.7;
731
732 cx = allocation.width / 2.0;
733 cy = allocation.height / 2.0;
734 r = (cx < cy ? cx : cy) * 0.4;
735 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
736 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
737 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
738 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
739 cairo_set_source(cr, pat);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500740 cairo_pattern_destroy(pat);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200741 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
742 cairo_fill(cr);
743
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500744 widget_set_allocation(dialog->button,
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500745 allocation.x + cx - r,
746 allocation.y + cy - r, 2 * r, 2 * r);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200747
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200748 cairo_destroy(cr);
749
Alexander Larssonc584fa62013-05-22 14:41:32 +0200750 surface = window_get_surface(dialog->window);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200751 cairo_surface_destroy(surface);
752}
753
754static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500755unlock_dialog_button_handler(struct widget *widget,
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200756 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100757 uint32_t button,
758 enum wl_pointer_button_state state, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200759{
760 struct unlock_dialog *dialog = data;
761 struct desktop *desktop = dialog->desktop;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200762
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500763 if (button == BTN_LEFT) {
Daniel Stone4dbadb12012-05-30 16:31:51 +0100764 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
765 !dialog->closing) {
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200766 display_defer(desktop->display, &desktop->unlock_task);
767 dialog->closing = 1;
768 }
769 }
770}
771
772static void
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200773unlock_dialog_keyboard_focus_handler(struct window *window,
774 struct input *device, void *data)
775{
776 window_schedule_redraw(window);
777}
778
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500779static int
Kristian Høgsbergee143232012-01-09 08:42:24 -0500780unlock_dialog_widget_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400781 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400782 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500783{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500784 struct unlock_dialog *dialog = data;
785
786 dialog->button_focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500787 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500788
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300789 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500790}
791
792static void
793unlock_dialog_widget_leave_handler(struct widget *widget,
794 struct input *input, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200795{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500796 struct unlock_dialog *dialog = data;
797
798 dialog->button_focused = 0;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500799 widget_schedule_redraw(widget);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200800}
801
802static struct unlock_dialog *
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500803unlock_dialog_create(struct desktop *desktop)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200804{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500805 struct display *display = desktop->display;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200806 struct unlock_dialog *dialog;
807
808 dialog = malloc(sizeof *dialog);
809 if (!dialog)
810 return NULL;
811 memset(dialog, 0, sizeof *dialog);
812
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400813 dialog->window = window_create_custom(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500814 dialog->widget = frame_create(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200815 window_set_title(dialog->window, "Unlock your desktop");
816
817 window_set_user_data(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200818 window_set_keyboard_focus_handler(dialog->window,
819 unlock_dialog_keyboard_focus_handler);
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500820 dialog->button = widget_add_widget(dialog->widget, dialog);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500821 widget_set_redraw_handler(dialog->widget,
822 unlock_dialog_redraw_handler);
Kristian Høgsbergee143232012-01-09 08:42:24 -0500823 widget_set_enter_handler(dialog->button,
824 unlock_dialog_widget_enter_handler);
825 widget_set_leave_handler(dialog->button,
826 unlock_dialog_widget_leave_handler);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500827 widget_set_button_handler(dialog->button,
828 unlock_dialog_button_handler);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200829
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500830 desktop_shell_set_lock_surface(desktop->shell,
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400831 window_get_wl_surface(dialog->window));
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500832
Pekka Paalanen40e49ac2012-01-18 16:51:30 +0200833 window_schedule_resize(dialog->window, 260, 230);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200834
835 return dialog;
836}
837
838static void
839unlock_dialog_destroy(struct unlock_dialog *dialog)
840{
841 window_destroy(dialog->window);
842 free(dialog);
843}
844
845static void
846unlock_dialog_finish(struct task *task, uint32_t events)
847{
848 struct desktop *desktop =
Benjamin Franzked7759712011-11-22 12:38:48 +0100849 container_of(task, struct desktop, unlock_task);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200850
851 desktop_shell_unlock(desktop->shell);
852 unlock_dialog_destroy(desktop->unlock_dialog);
853 desktop->unlock_dialog = NULL;
854}
855
856static void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400857desktop_shell_configure(void *data,
858 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400859 uint32_t edges,
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400860 struct wl_surface *surface,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400861 int32_t width, int32_t height)
862{
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400863 struct window *window = wl_surface_get_user_data(surface);
Pekka Paalanen068ae942011-11-28 14:11:15 +0200864 struct surface *s = window_get_user_data(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400865
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400866 s->configure(data, desktop_shell, edges, window, width, height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400867}
868
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200869static void
870desktop_shell_prepare_lock_surface(void *data,
871 struct desktop_shell *desktop_shell)
872{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200873 struct desktop *desktop = data;
874
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200875 if (!key_locking) {
876 desktop_shell_unlock(desktop->shell);
877 return;
878 }
879
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200880 if (!desktop->unlock_dialog) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500881 desktop->unlock_dialog = unlock_dialog_create(desktop);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200882 desktop->unlock_dialog->desktop = desktop;
883 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200884}
885
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300886static void
887desktop_shell_grab_cursor(void *data,
888 struct desktop_shell *desktop_shell,
889 uint32_t cursor)
890{
891 struct desktop *desktop = data;
892
893 switch (cursor) {
Philipp Brüschweiler16d59d72012-08-24 15:43:55 +0200894 case DESKTOP_SHELL_CURSOR_NONE:
895 desktop->grab_cursor = CURSOR_BLANK;
896 break;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300897 case DESKTOP_SHELL_CURSOR_BUSY:
898 desktop->grab_cursor = CURSOR_WATCH;
899 break;
900 case DESKTOP_SHELL_CURSOR_MOVE:
901 desktop->grab_cursor = CURSOR_DRAGGING;
902 break;
903 case DESKTOP_SHELL_CURSOR_RESIZE_TOP:
904 desktop->grab_cursor = CURSOR_TOP;
905 break;
906 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM:
907 desktop->grab_cursor = CURSOR_BOTTOM;
908 break;
909 case DESKTOP_SHELL_CURSOR_RESIZE_LEFT:
910 desktop->grab_cursor = CURSOR_LEFT;
911 break;
912 case DESKTOP_SHELL_CURSOR_RESIZE_RIGHT:
913 desktop->grab_cursor = CURSOR_RIGHT;
914 break;
915 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_LEFT:
916 desktop->grab_cursor = CURSOR_TOP_LEFT;
917 break;
918 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_RIGHT:
919 desktop->grab_cursor = CURSOR_TOP_RIGHT;
920 break;
921 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_LEFT:
922 desktop->grab_cursor = CURSOR_BOTTOM_LEFT;
923 break;
924 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_RIGHT:
925 desktop->grab_cursor = CURSOR_BOTTOM_RIGHT;
926 break;
927 case DESKTOP_SHELL_CURSOR_ARROW:
928 default:
929 desktop->grab_cursor = CURSOR_LEFT_PTR;
930 }
931}
932
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400933static const struct desktop_shell_listener listener = {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200934 desktop_shell_configure,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300935 desktop_shell_prepare_lock_surface,
936 desktop_shell_grab_cursor
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400937};
938
U. Artie Eoff44874d92012-10-02 21:12:35 -0700939static void
940background_destroy(struct background *background)
941{
942 widget_destroy(background->widget);
943 window_destroy(background->window);
944
945 free(background);
946}
947
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100948static struct background *
949background_create(struct desktop *desktop)
950{
951 struct background *background;
952
953 background = malloc(sizeof *background);
954 memset(background, 0, sizeof *background);
955
956 background->base.configure = background_configure;
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400957 background->window = window_create_custom(desktop->display);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500958 background->widget = window_add_widget(background->window, background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100959 window_set_user_data(background->window, background);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500960 widget_set_redraw_handler(background->widget, background_draw);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100961
962 return background;
963}
964
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400965static int
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300966grab_surface_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400967 float x, float y, void *data)
968{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300969 struct desktop *desktop = data;
970
971 return desktop->grab_cursor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400972}
973
974static void
U. Artie Eoff44874d92012-10-02 21:12:35 -0700975grab_surface_destroy(struct desktop *desktop)
976{
977 widget_destroy(desktop->grab_widget);
978 window_destroy(desktop->grab_window);
979}
980
981static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300982grab_surface_create(struct desktop *desktop)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400983{
984 struct wl_surface *s;
985
Ander Conselvan de Oliveira07a91cd2012-07-16 14:15:50 +0300986 desktop->grab_window = window_create_custom(desktop->display);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300987 window_set_user_data(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400988
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300989 s = window_get_wl_surface(desktop->grab_window);
990 desktop_shell_set_grab_surface(desktop->shell, s);
991
992 desktop->grab_widget =
993 window_add_widget(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400994 /* We set the allocation to 1x1 at 0,0 so the fake enter event
995 * at 0,0 will go to this widget. */
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300996 widget_set_allocation(desktop->grab_widget, 0, 0, 1, 1);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400997
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300998 widget_set_enter_handler(desktop->grab_widget,
999 grab_surface_enter_handler);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001000}
1001
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001002static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001003output_destroy(struct output *output)
1004{
1005 background_destroy(output->background);
1006 panel_destroy(output->panel);
1007 wl_output_destroy(output->output);
1008 wl_list_remove(&output->link);
1009
1010 free(output);
1011}
1012
1013static void
1014desktop_destroy_outputs(struct desktop *desktop)
1015{
1016 struct output *tmp;
1017 struct output *output;
1018
1019 wl_list_for_each_safe(output, tmp, &desktop->outputs, link)
1020 output_destroy(output);
1021}
1022
1023static void
Alexander Larssonc584fa62013-05-22 14:41:32 +02001024output_handle_geometry(void *data,
1025 struct wl_output *wl_output,
1026 int x, int y,
1027 int physical_width,
1028 int physical_height,
1029 int subpixel,
1030 const char *make,
1031 const char *model,
1032 int transform)
1033{
1034 struct output *output = data;
1035
1036 window_set_buffer_transform(output->panel->window, transform);
1037 window_set_buffer_transform(output->background->window, transform);
1038}
1039
1040static void
1041output_handle_mode(void *data,
1042 struct wl_output *wl_output,
1043 uint32_t flags,
1044 int width,
1045 int height,
1046 int refresh)
1047{
1048}
1049
1050static void
1051output_handle_done(void *data,
1052 struct wl_output *wl_output)
1053{
1054}
1055
1056static void
1057output_handle_scale(void *data,
1058 struct wl_output *wl_output,
1059 uint32_t scale)
1060{
1061 struct output *output = data;
1062
1063 window_set_buffer_scale(output->panel->window, scale);
1064 window_set_buffer_scale(output->background->window, scale);
1065}
1066
1067static const struct wl_output_listener output_listener = {
1068 output_handle_geometry,
1069 output_handle_mode,
1070 output_handle_done,
1071 output_handle_scale
1072};
1073
1074static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001075create_output(struct desktop *desktop, uint32_t id)
1076{
1077 struct output *output;
1078
1079 output = calloc(1, sizeof *output);
1080 if (!output)
1081 return;
1082
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001083 output->output =
Alexander Larssonc584fa62013-05-22 14:41:32 +02001084 display_bind(desktop->display, id, &wl_output_interface, 2);
1085
1086 wl_output_add_listener(output->output, &output_listener, output);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001087
1088 wl_list_insert(&desktop->outputs, &output->link);
1089}
1090
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001091static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001092global_handler(struct display *display, uint32_t id,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001093 const char *interface, uint32_t version, void *data)
1094{
1095 struct desktop *desktop = data;
1096
1097 if (!strcmp(interface, "desktop_shell")) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001098 desktop->shell = display_bind(desktop->display,
1099 id, &desktop_shell_interface, 1);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001100 desktop_shell_add_listener(desktop->shell, &listener, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001101 } else if (!strcmp(interface, "wl_output")) {
1102 create_output(desktop, id);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001103 }
1104}
1105
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001106static void
1107launcher_section_done(void *data)
1108{
1109 struct desktop *desktop = data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001110 struct output *output;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001111
1112 if (key_launcher_icon == NULL || key_launcher_path == NULL) {
1113 fprintf(stderr, "invalid launcher section\n");
1114 return;
1115 }
1116
Martin Minarik1e51a872012-06-08 00:39:11 +02001117 wl_list_for_each(output, &desktop->outputs, link) {
Kristian Høgsberg53880802012-01-09 11:16:50 -05001118 panel_add_launcher(output->panel,
1119 key_launcher_icon, key_launcher_path);
Martin Minarik1e51a872012-06-08 00:39:11 +02001120 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001121
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001122 free(key_launcher_icon);
1123 key_launcher_icon = NULL;
1124 free(key_launcher_path);
1125 key_launcher_path = NULL;
1126}
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001127
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001128static void
1129add_default_launcher(struct desktop *desktop)
1130{
1131 struct output *output;
1132
1133 wl_list_for_each(output, &desktop->outputs, link)
1134 panel_add_launcher(output->panel,
1135 DATADIR "/weston/terminal.png",
Rodney Lorrimar99ff01b2012-02-29 17:31:03 +01001136 BINDIR "/weston-terminal");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001137}
1138
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001139int main(int argc, char *argv[])
1140{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001141 struct desktop desktop = { 0 };
Ossama Othmana50e6e42013-05-14 09:48:26 -07001142 int config_fd;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001143 struct output *output;
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001144 int ret;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001145
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001146 desktop.unlock_task.run = unlock_dialog_finish;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001147 wl_list_init(&desktop.outputs);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001148
Kristian Høgsberg4172f662013-02-20 15:27:49 -05001149 desktop.display = display_create(&argc, argv);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001150 if (desktop.display == NULL) {
1151 fprintf(stderr, "failed to create display: %m\n");
1152 return -1;
1153 }
1154
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001155 display_set_user_data(desktop.display, &desktop);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001156 display_set_global_handler(desktop.display, global_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001157
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001158 wl_list_for_each(output, &desktop.outputs, link) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001159 struct wl_surface *surface;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001160
1161 output->panel = panel_create(desktop.display);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001162 surface = window_get_wl_surface(output->panel->window);
1163 desktop_shell_set_panel(desktop.shell,
1164 output->output, surface);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001165
1166 output->background = background_create(&desktop);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001167 surface = window_get_wl_surface(output->background->window);
1168 desktop_shell_set_background(desktop.shell,
1169 output->output, surface);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001170 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001171
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001172 grab_surface_create(&desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001173
Ossama Othmana50e6e42013-05-14 09:48:26 -07001174 config_fd = open_config_file("weston.ini");
1175 ret = parse_config_file(config_fd,
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001176 config_sections, ARRAY_LENGTH(config_sections),
1177 &desktop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07001178 close(config_fd);
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001179 if (ret < 0)
1180 add_default_launcher(&desktop);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001181
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001182 signal(SIGCHLD, sigchild_handler);
1183
1184 display_run(desktop.display);
1185
U. Artie Eoff44874d92012-10-02 21:12:35 -07001186 /* Cleanup */
1187 grab_surface_destroy(&desktop);
1188 desktop_destroy_outputs(&desktop);
1189 if (desktop.unlock_dialog)
1190 unlock_dialog_destroy(desktop.unlock_dialog);
1191 desktop_shell_destroy(desktop.shell);
1192 display_destroy(desktop.display);
1193
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001194 return 0;
1195}