blob: 51ce3ec20dd01781c3f78dd03319fd6c5398ae66 [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 Paalanen79346ab2013-05-22 18:03:09 +030052 uint32_t interface_version;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +020053 struct unlock_dialog *unlock_dialog;
54 struct task unlock_task;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010055 struct wl_list outputs;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040056
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030057 struct window *grab_window;
58 struct widget *grab_widget;
59
Scott Moreauec116022012-07-22 18:23:52 -060060 enum cursor_type grab_cursor;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030061
62 int painted;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010063};
64
65struct surface {
66 void (*configure)(void *data,
67 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040068 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010069 int32_t width, int32_t height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040070};
71
72struct panel {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010073 struct surface base;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040074 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050075 struct widget *widget;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050076 struct wl_list launcher_list;
Martin Minarik1e51a872012-06-08 00:39:11 +020077 struct panel_clock *clock;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030078 int painted;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040079};
80
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010081struct background {
82 struct surface base;
83 struct window *window;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050084 struct widget *widget;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030085 int painted;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010086};
87
88struct output {
89 struct wl_output *output;
90 struct wl_list link;
91
92 struct panel *panel;
93 struct background *background;
94};
95
Kristian Høgsberg53880802012-01-09 11:16:50 -050096struct panel_launcher {
Kristian Høgsbergc51f7992012-01-08 15:09:53 -050097 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -040098 struct panel *panel;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040099 cairo_surface_t *icon;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500100 int focused, pressed;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400101 char *path;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500102 struct wl_list link;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400103 struct wl_array envp;
104 struct wl_array argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400105};
106
Martin Minarik1e51a872012-06-08 00:39:11 +0200107struct panel_clock {
108 struct widget *widget;
109 struct panel *panel;
Martin Minarik1e51a872012-06-08 00:39:11 +0200110 struct task clock_task;
111 int clock_fd;
112};
113
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200114struct unlock_dialog {
115 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500116 struct widget *widget;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500117 struct widget *button;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500118 int button_focused;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200119 int closing;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200120 struct desktop *desktop;
121};
122
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500123static char *key_background_image = DATADIR "/weston/pattern.png";
124static char *key_background_type = "tile";
125static uint32_t key_panel_color = 0xaa000000;
126static uint32_t key_background_color = 0xff002244;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500127static char *key_launcher_icon;
128static char *key_launcher_path;
129static void launcher_section_done(void *data);
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200130static int key_locking = 1;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500131
132static const struct config_key shell_config_keys[] = {
133 { "background-image", CONFIG_KEY_STRING, &key_background_image },
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500134 { "background-type", CONFIG_KEY_STRING, &key_background_type },
Scott Moreaufa1de692012-01-27 13:25:49 -0700135 { "panel-color", CONFIG_KEY_UNSIGNED_INTEGER, &key_panel_color },
136 { "background-color", CONFIG_KEY_UNSIGNED_INTEGER, &key_background_color },
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200137 { "locking", CONFIG_KEY_BOOLEAN, &key_locking },
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500138};
139
140static const struct config_key launcher_config_keys[] = {
141 { "icon", CONFIG_KEY_STRING, &key_launcher_icon },
142 { "path", CONFIG_KEY_STRING, &key_launcher_path },
143};
144
145static const struct config_section config_sections[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200146 { "shell",
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500147 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
148 { "launcher",
149 launcher_config_keys, ARRAY_LENGTH(launcher_config_keys),
150 launcher_section_done }
151};
152
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400153static void
154sigchild_handler(int s)
155{
156 int status;
157 pid_t pid;
158
159 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
160 fprintf(stderr, "child %d exited\n", pid);
161}
162
163static void
Pekka Paalanen9e30a822012-01-19 16:40:28 +0200164menu_func(struct window *window, int index, void *data)
165{
166 printf("Selected index %d from a panel menu.\n", index);
167}
168
169static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500170show_menu(struct panel *panel, struct input *input, uint32_t time)
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400171{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500172 int32_t x, y;
173 static const char *entries[] = {
174 "Roy", "Pris", "Leon", "Zhora"
175 };
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400176
177 input_get_position(input, &x, &y);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +0200178 window_show_menu(window_get_display(panel->window),
179 input, time, panel->window,
Pekka Paalanen9e30a822012-01-19 16:40:28 +0200180 x - 10, y - 10, menu_func, entries, 4);
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400181}
182
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300183static int
184is_desktop_painted(struct desktop *desktop)
185{
186 struct output *output;
187
188 wl_list_for_each(output, &desktop->outputs, link) {
189 if (output->panel && !output->panel->painted)
190 return 0;
191 if (output->background && !output->background->painted)
192 return 0;
193 }
194
195 return 1;
196}
197
198static void
199check_desktop_ready(struct window *window)
200{
201 struct display *display;
202 struct desktop *desktop;
203
204 display = window_get_display(window);
205 desktop = display_get_user_data(display);
206
207 if (!desktop->painted && is_desktop_painted(desktop)) {
208 desktop->painted = 1;
209
210 if (desktop->interface_version >= 2)
211 desktop_shell_desktop_ready(desktop->shell);
212 }
213}
214
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400215static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500216panel_launcher_activate(struct panel_launcher *widget)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400217{
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400218 char **argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400219 pid_t pid;
220
221 pid = fork();
222 if (pid < 0) {
223 fprintf(stderr, "fork failed: %m\n");
224 return;
225 }
226
227 if (pid)
228 return;
Benjamin Franzked7759712011-11-22 12:38:48 +0100229
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400230 argv = widget->argv.data;
231 if (execve(argv[0], argv, widget->envp.data) < 0) {
232 fprintf(stderr, "execl '%s' failed: %m\n", argv[0]);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400233 exit(1);
234 }
235}
236
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400237static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500238panel_launcher_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400239{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500240 struct panel_launcher *launcher = data;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500241 struct rectangle allocation;
242 cairo_t *cr;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400243
Alexander Larssonc584fa62013-05-22 14:41:32 +0200244 cr = widget_cairo_create(launcher->panel->widget);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500245
246 widget_get_allocation(widget, &allocation);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500247 if (launcher->pressed) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500248 allocation.x++;
249 allocation.y++;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400250 }
251
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500252 cairo_set_source_surface(cr, launcher->icon,
253 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400254 cairo_paint(cr);
255
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500256 if (launcher->focused) {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400257 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500258 cairo_mask_surface(cr, launcher->icon,
259 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400260 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400261
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500262 cairo_destroy(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400263}
264
Tiago Vignatti61500722012-05-23 22:06:28 +0300265static int
266panel_launcher_motion_handler(struct widget *widget, struct input *input,
267 uint32_t time, float x, float y, void *data)
268{
269 struct panel_launcher *launcher = data;
270
271 widget_set_tooltip(widget, basename((char *)launcher->path), x, y);
272
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300273 return CURSOR_LEFT_PTR;
Tiago Vignatti61500722012-05-23 22:06:28 +0300274}
275
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400276static void
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500277set_hex_color(cairo_t *cr, uint32_t color)
278{
279 cairo_set_source_rgba(cr,
280 ((color >> 16) & 0xff) / 255.0,
281 ((color >> 8) & 0xff) / 255.0,
282 ((color >> 0) & 0xff) / 255.0,
283 ((color >> 24) & 0xff) / 255.0);
284}
285
286static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500287panel_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400288{
289 cairo_surface_t *surface;
290 cairo_t *cr;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500291 struct panel *panel = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400292
Alexander Larssonc584fa62013-05-22 14:41:32 +0200293 cr = widget_cairo_create(panel->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400294 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500295 set_hex_color(cr, key_panel_color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400296 cairo_paint(cr);
297
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400298 cairo_destroy(cr);
Alexander Larssonc584fa62013-05-22 14:41:32 +0200299 surface = window_get_surface(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400300 cairo_surface_destroy(surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300301 panel->painted = 1;
302 check_desktop_ready(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400303}
304
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500305static int
Kristian Høgsberg53880802012-01-09 11:16:50 -0500306panel_launcher_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400307 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500308{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500309 struct panel_launcher *launcher = data;
310
311 launcher->focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500312 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500313
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300314 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500315}
316
317static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500318panel_launcher_leave_handler(struct widget *widget,
319 struct input *input, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400320{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500321 struct panel_launcher *launcher = data;
322
323 launcher->focused = 0;
Tiago Vignatti61500722012-05-23 22:06:28 +0300324 widget_destroy_tooltip(widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500325 widget_schedule_redraw(widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400326}
327
328static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500329panel_launcher_button_handler(struct widget *widget,
330 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100331 uint32_t button,
332 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400333{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500334 struct panel_launcher *launcher;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400335
Kristian Høgsberg53880802012-01-09 11:16:50 -0500336 launcher = widget_get_user_data(widget);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500337 widget_schedule_redraw(widget);
Daniel Stone4dbadb12012-05-30 16:31:51 +0100338 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg53880802012-01-09 11:16:50 -0500339 panel_launcher_activate(launcher);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500340}
341
Martin Minarik1e51a872012-06-08 00:39:11 +0200342static void
343clock_func(struct task *task, uint32_t events)
344{
345 struct panel_clock *clock =
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400346 container_of(task, struct panel_clock, clock_task);
Kristian Høgsberg70226bb2012-06-08 16:54:52 -0400347 uint64_t exp;
Martin Minarik1e51a872012-06-08 00:39:11 +0200348
Martin Olsson8df662a2012-07-08 03:03:47 +0200349 if (read(clock->clock_fd, &exp, sizeof exp) != sizeof exp)
350 abort();
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400351 widget_schedule_redraw(clock->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200352}
353
354static void
355panel_clock_redraw_handler(struct widget *widget, void *data)
356{
Martin Minarik1e51a872012-06-08 00:39:11 +0200357 struct panel_clock *clock = data;
358 cairo_t *cr;
359 struct rectangle allocation;
360 cairo_text_extents_t extents;
361 cairo_font_extents_t font_extents;
Martin Minarik1e51a872012-06-08 00:39:11 +0200362 time_t rawtime;
363 struct tm * timeinfo;
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400364 char string[128];
Martin Minarik1e51a872012-06-08 00:39:11 +0200365
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400366 time(&rawtime);
367 timeinfo = localtime(&rawtime);
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400368 strftime(string, sizeof string, "%a %b %d, %I:%M %p", timeinfo);
Martin Minarik1e51a872012-06-08 00:39:11 +0200369
370 widget_get_allocation(widget, &allocation);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400371 if (allocation.width == 0)
372 return;
Martin Minarik1e51a872012-06-08 00:39:11 +0200373
Alexander Larssonc584fa62013-05-22 14:41:32 +0200374 cr = widget_cairo_create(clock->panel->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200375 cairo_select_font_face(cr, "sans",
376 CAIRO_FONT_SLANT_NORMAL,
377 CAIRO_FONT_WEIGHT_NORMAL);
378 cairo_set_font_size(cr, 14);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400379 cairo_text_extents(cr, string, &extents);
Martin Minarik1e51a872012-06-08 00:39:11 +0200380 cairo_font_extents (cr, &font_extents);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400381 cairo_move_to(cr, allocation.x + 5,
382 allocation.y + 3 * (allocation.height >> 2) + 1);
383 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400384 cairo_show_text(cr, string);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400385 cairo_move_to(cr, allocation.x + 4,
386 allocation.y + 3 * (allocation.height >> 2));
387 cairo_set_source_rgb(cr, 1, 1, 1);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400388 cairo_show_text(cr, string);
Martin Minarik1e51a872012-06-08 00:39:11 +0200389 cairo_destroy(cr);
390}
391
392static int
393clock_timer_reset(struct panel_clock *clock)
394{
395 struct itimerspec its;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400396
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400397 its.it_interval.tv_sec = 60;
Martin Minarik1e51a872012-06-08 00:39:11 +0200398 its.it_interval.tv_nsec = 0;
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400399 its.it_value.tv_sec = 60;
Martin Minarik1e51a872012-06-08 00:39:11 +0200400 its.it_value.tv_nsec = 0;
401 if (timerfd_settime(clock->clock_fd, 0, &its, NULL) < 0) {
402 fprintf(stderr, "could not set timerfd\n: %m");
403 return -1;
404 }
405
406 return 0;
407}
408
409static void
U. Artie Eoff44874d92012-10-02 21:12:35 -0700410panel_destroy_clock(struct panel_clock *clock)
411{
412 widget_destroy(clock->widget);
413
414 close(clock->clock_fd);
415
416 free(clock);
417}
418
419static void
Martin Minarik1e51a872012-06-08 00:39:11 +0200420panel_add_clock(struct panel *panel)
421{
422 struct panel_clock *clock;
423 int timerfd;
424
425 timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
426 if (timerfd < 0) {
427 fprintf(stderr, "could not create timerfd\n: %m");
428 return;
429 }
430
431 clock = malloc(sizeof *clock);
432 memset(clock, 0, sizeof *clock);
433 clock->panel = panel;
434 panel->clock = clock;
435 clock->clock_fd = timerfd;
436
437 clock->clock_task.run = clock_func;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400438 display_watch_fd(window_get_display(panel->window), clock->clock_fd,
439 EPOLLIN, &clock->clock_task);
Martin Minarik1e51a872012-06-08 00:39:11 +0200440 clock_timer_reset(clock);
441
442 clock->widget = widget_add_widget(panel->widget, clock);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400443 widget_set_redraw_handler(clock->widget, panel_clock_redraw_handler);
Martin Minarik1e51a872012-06-08 00:39:11 +0200444}
445
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500446static void
447panel_button_handler(struct widget *widget,
448 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100449 uint32_t button,
450 enum wl_pointer_button_state state, void *data)
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500451{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500452 struct panel *panel = data;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500453
Daniel Stone4dbadb12012-05-30 16:31:51 +0100454 if (button == BTN_RIGHT && state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500455 show_menu(panel, input, time);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400456}
457
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100458static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500459panel_resize_handler(struct widget *widget,
460 int32_t width, int32_t height, void *data)
461{
462 struct panel_launcher *launcher;
463 struct panel *panel = data;
464 int x, y, w, h;
465
466 x = 10;
467 y = 16;
468 wl_list_for_each(launcher, &panel->launcher_list, link) {
469 w = cairo_image_surface_get_width(launcher->icon);
470 h = cairo_image_surface_get_height(launcher->icon);
471 widget_set_allocation(launcher->widget,
472 x, y - h / 2, w + 1, h + 1);
473 x += w + 10;
474 }
Martin Minarik1e51a872012-06-08 00:39:11 +0200475 h=20;
476 w=170;
Pekka Paalanen01b17252012-06-12 17:42:26 +0300477
478 if (panel->clock)
479 widget_set_allocation(panel->clock->widget,
480 width - w - 8, y - h / 2, w + 1, h + 1);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500481}
482
483static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100484panel_configure(void *data,
485 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400486 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100487 int32_t width, int32_t height)
488{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500489 struct surface *surface = window_get_user_data(window);
490 struct panel *panel = container_of(surface, struct panel, base);
491
492 window_schedule_resize(panel->window, width, 32);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100493}
494
U. Artie Eoff44874d92012-10-02 21:12:35 -0700495static void
496panel_destroy_launcher(struct panel_launcher *launcher)
497{
498 wl_array_release(&launcher->argv);
499 wl_array_release(&launcher->envp);
500
501 free(launcher->path);
502
503 cairo_surface_destroy(launcher->icon);
504
505 widget_destroy(launcher->widget);
506 wl_list_remove(&launcher->link);
507
508 free(launcher);
509}
510
511static void
512panel_destroy(struct panel *panel)
513{
514 struct panel_launcher *tmp;
515 struct panel_launcher *launcher;
516
517 panel_destroy_clock(panel->clock);
518
519 wl_list_for_each_safe(launcher, tmp, &panel->launcher_list, link)
520 panel_destroy_launcher(launcher);
521
522 widget_destroy(panel->widget);
523 window_destroy(panel->window);
524
525 free(panel);
526}
527
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400528static struct panel *
529panel_create(struct display *display)
530{
531 struct panel *panel;
532
533 panel = malloc(sizeof *panel);
534 memset(panel, 0, sizeof *panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400535
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100536 panel->base.configure = panel_configure;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400537 panel->window = window_create_custom(display);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500538 panel->widget = window_add_widget(panel->window, panel);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500539 wl_list_init(&panel->launcher_list);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400540
541 window_set_title(panel->window, "panel");
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400542 window_set_user_data(panel->window, panel);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500543
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500544 widget_set_redraw_handler(panel->widget, panel_redraw_handler);
545 widget_set_resize_handler(panel->widget, panel_resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500546 widget_set_button_handler(panel->widget, panel_button_handler);
Rafal Mielniczukb9e513c2012-06-09 20:33:29 +0200547
548 panel_add_clock(panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400549
550 return panel;
551}
552
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200553static cairo_surface_t *
554load_icon_or_fallback(const char *icon)
555{
556 cairo_surface_t *surface = cairo_image_surface_create_from_png(icon);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200557 cairo_status_t status;
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200558 cairo_t *cr;
559
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200560 status = cairo_surface_status(surface);
561 if (status == CAIRO_STATUS_SUCCESS)
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200562 return surface;
563
564 cairo_surface_destroy(surface);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200565 fprintf(stderr, "ERROR loading icon from file '%s', error: '%s'\n",
566 icon, cairo_status_to_string(status));
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200567
568 /* draw fallback icon */
569 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
570 20, 20);
571 cr = cairo_create(surface);
572
573 cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1);
574 cairo_paint(cr);
575
576 cairo_set_source_rgba(cr, 0, 0, 0, 1);
577 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
578 cairo_rectangle(cr, 0, 0, 20, 20);
579 cairo_move_to(cr, 4, 4);
580 cairo_line_to(cr, 16, 16);
581 cairo_move_to(cr, 4, 16);
582 cairo_line_to(cr, 16, 4);
583 cairo_stroke(cr);
584
585 cairo_destroy(cr);
586
587 return surface;
588}
589
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400590static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500591panel_add_launcher(struct panel *panel, const char *icon, const char *path)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400592{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500593 struct panel_launcher *launcher;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400594 char *start, *p, *eq, **ps;
595 int i, j, k;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400596
Kristian Høgsberg53880802012-01-09 11:16:50 -0500597 launcher = malloc(sizeof *launcher);
598 memset(launcher, 0, sizeof *launcher);
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200599 launcher->icon = load_icon_or_fallback(icon);
Kristian Høgsberg53880802012-01-09 11:16:50 -0500600 launcher->path = strdup(path);
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400601
602 wl_array_init(&launcher->envp);
603 wl_array_init(&launcher->argv);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300604 for (i = 0; environ[i]; i++) {
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400605 ps = wl_array_add(&launcher->envp, sizeof *ps);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300606 *ps = environ[i];
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400607 }
608 j = 0;
609
610 start = launcher->path;
611 while (*start) {
612 for (p = start, eq = NULL; *p && !isspace(*p); p++)
613 if (*p == '=')
614 eq = p;
615
616 if (eq && j == 0) {
617 ps = launcher->envp.data;
618 for (k = 0; k < i; k++)
619 if (strncmp(ps[k], start, eq - start) == 0) {
620 ps[k] = start;
621 break;
622 }
623 if (k == i) {
624 ps = wl_array_add(&launcher->envp, sizeof *ps);
625 *ps = start;
626 i++;
627 }
628 } else {
629 ps = wl_array_add(&launcher->argv, sizeof *ps);
630 *ps = start;
631 j++;
632 }
633
634 while (*p && isspace(*p))
635 *p++ = '\0';
636
637 start = p;
638 }
639
640 ps = wl_array_add(&launcher->envp, sizeof *ps);
641 *ps = NULL;
642 ps = wl_array_add(&launcher->argv, sizeof *ps);
643 *ps = NULL;
644
Kristian Høgsberg53880802012-01-09 11:16:50 -0500645 launcher->panel = panel;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500646 wl_list_insert(panel->launcher_list.prev, &launcher->link);
647
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500648 launcher->widget = widget_add_widget(panel->widget, launcher);
Kristian Høgsberg53880802012-01-09 11:16:50 -0500649 widget_set_enter_handler(launcher->widget,
650 panel_launcher_enter_handler);
651 widget_set_leave_handler(launcher->widget,
652 panel_launcher_leave_handler);
653 widget_set_button_handler(launcher->widget,
654 panel_launcher_button_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500655 widget_set_redraw_handler(launcher->widget,
656 panel_launcher_redraw_handler);
Tiago Vignatti61500722012-05-23 22:06:28 +0300657 widget_set_motion_handler(launcher->widget,
658 panel_launcher_motion_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400659}
660
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500661enum {
662 BACKGROUND_SCALE,
Pekka Paalanena402b052013-05-22 18:03:10 +0300663 BACKGROUND_SCALE_CROP,
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500664 BACKGROUND_TILE
665};
666
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400667static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500668background_draw(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400669{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500670 struct background *background = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400671 cairo_surface_t *surface, *image;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400672 cairo_pattern_t *pattern;
673 cairo_matrix_t matrix;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400674 cairo_t *cr;
Pekka Paalanena402b052013-05-22 18:03:10 +0300675 double im_w, im_h;
676 double sx, sy, s;
677 double tx, ty;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500678 struct rectangle allocation;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500679 int type = -1;
Pekka Paalanen9564c752012-10-24 09:43:08 +0300680 struct display *display;
681 struct wl_region *opaque;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400682
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500683 surface = window_get_surface(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400684
Alexander Larssonc584fa62013-05-22 14:41:32 +0200685 cr = widget_cairo_create(background->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400686 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
687 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
688 cairo_paint(cr);
689
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500690 widget_get_allocation(widget, &allocation);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500691 image = NULL;
692 if (key_background_image)
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400693 image = load_cairo_surface(key_background_image);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500694
695 if (strcmp(key_background_type, "scale") == 0)
696 type = BACKGROUND_SCALE;
Pekka Paalanena402b052013-05-22 18:03:10 +0300697 else if (strcmp(key_background_type, "scale-crop") == 0)
698 type = BACKGROUND_SCALE_CROP;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500699 else if (strcmp(key_background_type, "tile") == 0)
700 type = BACKGROUND_TILE;
701 else
702 fprintf(stderr, "invalid background-type: %s\n",
703 key_background_type);
704
705 if (image && type != -1) {
Pekka Paalanena402b052013-05-22 18:03:10 +0300706 im_w = cairo_image_surface_get_width(image);
707 im_h = cairo_image_surface_get_height(image);
708 sx = im_w / allocation.width;
709 sy = im_h / allocation.height;
710
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400711 pattern = cairo_pattern_create_for_surface(image);
Pekka Paalanena402b052013-05-22 18:03:10 +0300712
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500713 switch (type) {
714 case BACKGROUND_SCALE:
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500715 cairo_matrix_init_scale(&matrix, sx, sy);
716 cairo_pattern_set_matrix(pattern, &matrix);
717 break;
Pekka Paalanena402b052013-05-22 18:03:10 +0300718 case BACKGROUND_SCALE_CROP:
719 s = (sx < sy) ? sx : sy;
720 /* align center */
721 tx = (im_w - s * allocation.width) * 0.5;
722 ty = (im_h - s * allocation.height) * 0.5;
723 cairo_matrix_init_translate(&matrix, tx, ty);
724 cairo_matrix_scale(&matrix, s, s);
725 cairo_pattern_set_matrix(pattern, &matrix);
726 break;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500727 case BACKGROUND_TILE:
728 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
729 break;
730 }
Pekka Paalanena402b052013-05-22 18:03:10 +0300731
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400732 cairo_set_source(cr, pattern);
733 cairo_pattern_destroy (pattern);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400734 cairo_surface_destroy(image);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500735 } else {
736 set_hex_color(cr, key_background_color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400737 }
738
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500739 cairo_paint(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400740 cairo_destroy(cr);
741 cairo_surface_destroy(surface);
Pekka Paalanen9564c752012-10-24 09:43:08 +0300742
743 display = window_get_display(background->window);
744 opaque = wl_compositor_create_region(display_get_compositor(display));
745 wl_region_add(opaque, allocation.x, allocation.y,
746 allocation.width, allocation.height);
747 wl_surface_set_opaque_region(window_get_wl_surface(background->window), opaque);
748 wl_region_destroy(opaque);
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300749
750 background->painted = 1;
751 check_desktop_ready(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400752}
753
754static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100755background_configure(void *data,
756 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400757 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100758 int32_t width, int32_t height)
759{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500760 struct background *background =
761 (struct background *) window_get_user_data(window);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100762
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500763 widget_schedule_resize(background->widget, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100764}
765
766static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500767unlock_dialog_redraw_handler(struct widget *widget, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200768{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500769 struct unlock_dialog *dialog = data;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200770 struct rectangle allocation;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200771 cairo_surface_t *surface;
Alexander Larssonc584fa62013-05-22 14:41:32 +0200772 cairo_t *cr;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200773 cairo_pattern_t *pat;
774 double cx, cy, r, f;
775
Alexander Larssonc584fa62013-05-22 14:41:32 +0200776 cr = widget_cairo_create(widget);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500777
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500778 widget_get_allocation(dialog->widget, &allocation);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200779 cairo_rectangle(cr, allocation.x, allocation.y,
780 allocation.width, allocation.height);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200781 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
782 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500783 cairo_fill(cr);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200784
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500785 cairo_translate(cr, allocation.x, allocation.y);
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500786 if (dialog->button_focused)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200787 f = 1.0;
788 else
789 f = 0.7;
790
791 cx = allocation.width / 2.0;
792 cy = allocation.height / 2.0;
793 r = (cx < cy ? cx : cy) * 0.4;
794 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
795 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
796 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
797 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
798 cairo_set_source(cr, pat);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500799 cairo_pattern_destroy(pat);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200800 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
801 cairo_fill(cr);
802
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500803 widget_set_allocation(dialog->button,
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500804 allocation.x + cx - r,
805 allocation.y + cy - r, 2 * r, 2 * r);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200806
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200807 cairo_destroy(cr);
808
Alexander Larssonc584fa62013-05-22 14:41:32 +0200809 surface = window_get_surface(dialog->window);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200810 cairo_surface_destroy(surface);
811}
812
813static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500814unlock_dialog_button_handler(struct widget *widget,
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200815 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100816 uint32_t button,
817 enum wl_pointer_button_state state, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200818{
819 struct unlock_dialog *dialog = data;
820 struct desktop *desktop = dialog->desktop;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200821
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500822 if (button == BTN_LEFT) {
Daniel Stone4dbadb12012-05-30 16:31:51 +0100823 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
824 !dialog->closing) {
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200825 display_defer(desktop->display, &desktop->unlock_task);
826 dialog->closing = 1;
827 }
828 }
829}
830
831static void
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200832unlock_dialog_keyboard_focus_handler(struct window *window,
833 struct input *device, void *data)
834{
835 window_schedule_redraw(window);
836}
837
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500838static int
Kristian Høgsbergee143232012-01-09 08:42:24 -0500839unlock_dialog_widget_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400840 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400841 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500842{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500843 struct unlock_dialog *dialog = data;
844
845 dialog->button_focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500846 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500847
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300848 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500849}
850
851static void
852unlock_dialog_widget_leave_handler(struct widget *widget,
853 struct input *input, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200854{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500855 struct unlock_dialog *dialog = data;
856
857 dialog->button_focused = 0;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500858 widget_schedule_redraw(widget);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200859}
860
861static struct unlock_dialog *
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500862unlock_dialog_create(struct desktop *desktop)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200863{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500864 struct display *display = desktop->display;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200865 struct unlock_dialog *dialog;
866
867 dialog = malloc(sizeof *dialog);
868 if (!dialog)
869 return NULL;
870 memset(dialog, 0, sizeof *dialog);
871
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400872 dialog->window = window_create_custom(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500873 dialog->widget = frame_create(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200874 window_set_title(dialog->window, "Unlock your desktop");
875
876 window_set_user_data(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200877 window_set_keyboard_focus_handler(dialog->window,
878 unlock_dialog_keyboard_focus_handler);
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500879 dialog->button = widget_add_widget(dialog->widget, dialog);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500880 widget_set_redraw_handler(dialog->widget,
881 unlock_dialog_redraw_handler);
Kristian Høgsbergee143232012-01-09 08:42:24 -0500882 widget_set_enter_handler(dialog->button,
883 unlock_dialog_widget_enter_handler);
884 widget_set_leave_handler(dialog->button,
885 unlock_dialog_widget_leave_handler);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500886 widget_set_button_handler(dialog->button,
887 unlock_dialog_button_handler);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200888
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500889 desktop_shell_set_lock_surface(desktop->shell,
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400890 window_get_wl_surface(dialog->window));
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500891
Pekka Paalanen40e49ac2012-01-18 16:51:30 +0200892 window_schedule_resize(dialog->window, 260, 230);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200893
894 return dialog;
895}
896
897static void
898unlock_dialog_destroy(struct unlock_dialog *dialog)
899{
900 window_destroy(dialog->window);
901 free(dialog);
902}
903
904static void
905unlock_dialog_finish(struct task *task, uint32_t events)
906{
907 struct desktop *desktop =
Benjamin Franzked7759712011-11-22 12:38:48 +0100908 container_of(task, struct desktop, unlock_task);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200909
910 desktop_shell_unlock(desktop->shell);
911 unlock_dialog_destroy(desktop->unlock_dialog);
912 desktop->unlock_dialog = NULL;
913}
914
915static void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400916desktop_shell_configure(void *data,
917 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400918 uint32_t edges,
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400919 struct wl_surface *surface,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400920 int32_t width, int32_t height)
921{
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400922 struct window *window = wl_surface_get_user_data(surface);
Pekka Paalanen068ae942011-11-28 14:11:15 +0200923 struct surface *s = window_get_user_data(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400924
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400925 s->configure(data, desktop_shell, edges, window, width, height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400926}
927
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200928static void
929desktop_shell_prepare_lock_surface(void *data,
930 struct desktop_shell *desktop_shell)
931{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200932 struct desktop *desktop = data;
933
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200934 if (!key_locking) {
935 desktop_shell_unlock(desktop->shell);
936 return;
937 }
938
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200939 if (!desktop->unlock_dialog) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500940 desktop->unlock_dialog = unlock_dialog_create(desktop);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200941 desktop->unlock_dialog->desktop = desktop;
942 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200943}
944
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300945static void
946desktop_shell_grab_cursor(void *data,
947 struct desktop_shell *desktop_shell,
948 uint32_t cursor)
949{
950 struct desktop *desktop = data;
951
952 switch (cursor) {
Philipp Brüschweiler16d59d72012-08-24 15:43:55 +0200953 case DESKTOP_SHELL_CURSOR_NONE:
954 desktop->grab_cursor = CURSOR_BLANK;
955 break;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300956 case DESKTOP_SHELL_CURSOR_BUSY:
957 desktop->grab_cursor = CURSOR_WATCH;
958 break;
959 case DESKTOP_SHELL_CURSOR_MOVE:
960 desktop->grab_cursor = CURSOR_DRAGGING;
961 break;
962 case DESKTOP_SHELL_CURSOR_RESIZE_TOP:
963 desktop->grab_cursor = CURSOR_TOP;
964 break;
965 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM:
966 desktop->grab_cursor = CURSOR_BOTTOM;
967 break;
968 case DESKTOP_SHELL_CURSOR_RESIZE_LEFT:
969 desktop->grab_cursor = CURSOR_LEFT;
970 break;
971 case DESKTOP_SHELL_CURSOR_RESIZE_RIGHT:
972 desktop->grab_cursor = CURSOR_RIGHT;
973 break;
974 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_LEFT:
975 desktop->grab_cursor = CURSOR_TOP_LEFT;
976 break;
977 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_RIGHT:
978 desktop->grab_cursor = CURSOR_TOP_RIGHT;
979 break;
980 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_LEFT:
981 desktop->grab_cursor = CURSOR_BOTTOM_LEFT;
982 break;
983 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_RIGHT:
984 desktop->grab_cursor = CURSOR_BOTTOM_RIGHT;
985 break;
986 case DESKTOP_SHELL_CURSOR_ARROW:
987 default:
988 desktop->grab_cursor = CURSOR_LEFT_PTR;
989 }
990}
991
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400992static const struct desktop_shell_listener listener = {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200993 desktop_shell_configure,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300994 desktop_shell_prepare_lock_surface,
995 desktop_shell_grab_cursor
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400996};
997
U. Artie Eoff44874d92012-10-02 21:12:35 -0700998static void
999background_destroy(struct background *background)
1000{
1001 widget_destroy(background->widget);
1002 window_destroy(background->window);
1003
1004 free(background);
1005}
1006
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001007static struct background *
1008background_create(struct desktop *desktop)
1009{
1010 struct background *background;
1011
1012 background = malloc(sizeof *background);
1013 memset(background, 0, sizeof *background);
1014
1015 background->base.configure = background_configure;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001016 background->window = window_create_custom(desktop->display);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001017 background->widget = window_add_widget(background->window, background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001018 window_set_user_data(background->window, background);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001019 widget_set_redraw_handler(background->widget, background_draw);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001020
1021 return background;
1022}
1023
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001024static int
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001025grab_surface_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001026 float x, float y, void *data)
1027{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001028 struct desktop *desktop = data;
1029
1030 return desktop->grab_cursor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001031}
1032
1033static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001034grab_surface_destroy(struct desktop *desktop)
1035{
1036 widget_destroy(desktop->grab_widget);
1037 window_destroy(desktop->grab_window);
1038}
1039
1040static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001041grab_surface_create(struct desktop *desktop)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001042{
1043 struct wl_surface *s;
1044
Ander Conselvan de Oliveira07a91cd2012-07-16 14:15:50 +03001045 desktop->grab_window = window_create_custom(desktop->display);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001046 window_set_user_data(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001047
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001048 s = window_get_wl_surface(desktop->grab_window);
1049 desktop_shell_set_grab_surface(desktop->shell, s);
1050
1051 desktop->grab_widget =
1052 window_add_widget(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001053 /* We set the allocation to 1x1 at 0,0 so the fake enter event
1054 * at 0,0 will go to this widget. */
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001055 widget_set_allocation(desktop->grab_widget, 0, 0, 1, 1);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001056
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001057 widget_set_enter_handler(desktop->grab_widget,
1058 grab_surface_enter_handler);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001059}
1060
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001061static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001062output_destroy(struct output *output)
1063{
1064 background_destroy(output->background);
1065 panel_destroy(output->panel);
1066 wl_output_destroy(output->output);
1067 wl_list_remove(&output->link);
1068
1069 free(output);
1070}
1071
1072static void
1073desktop_destroy_outputs(struct desktop *desktop)
1074{
1075 struct output *tmp;
1076 struct output *output;
1077
1078 wl_list_for_each_safe(output, tmp, &desktop->outputs, link)
1079 output_destroy(output);
1080}
1081
1082static void
Alexander Larssonc584fa62013-05-22 14:41:32 +02001083output_handle_geometry(void *data,
1084 struct wl_output *wl_output,
1085 int x, int y,
1086 int physical_width,
1087 int physical_height,
1088 int subpixel,
1089 const char *make,
1090 const char *model,
1091 int transform)
1092{
1093 struct output *output = data;
1094
1095 window_set_buffer_transform(output->panel->window, transform);
1096 window_set_buffer_transform(output->background->window, transform);
1097}
1098
1099static void
1100output_handle_mode(void *data,
1101 struct wl_output *wl_output,
1102 uint32_t flags,
1103 int width,
1104 int height,
1105 int refresh)
1106{
1107}
1108
1109static void
1110output_handle_done(void *data,
1111 struct wl_output *wl_output)
1112{
1113}
1114
1115static void
1116output_handle_scale(void *data,
1117 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001118 int32_t scale)
Alexander Larssonc584fa62013-05-22 14:41:32 +02001119{
1120 struct output *output = data;
1121
1122 window_set_buffer_scale(output->panel->window, scale);
1123 window_set_buffer_scale(output->background->window, scale);
1124}
1125
1126static const struct wl_output_listener output_listener = {
1127 output_handle_geometry,
1128 output_handle_mode,
1129 output_handle_done,
1130 output_handle_scale
1131};
1132
1133static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001134create_output(struct desktop *desktop, uint32_t id)
1135{
1136 struct output *output;
1137
1138 output = calloc(1, sizeof *output);
1139 if (!output)
1140 return;
1141
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001142 output->output =
Alexander Larssonc584fa62013-05-22 14:41:32 +02001143 display_bind(desktop->display, id, &wl_output_interface, 2);
1144
1145 wl_output_add_listener(output->output, &output_listener, output);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001146
1147 wl_list_insert(&desktop->outputs, &output->link);
1148}
1149
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001150static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001151global_handler(struct display *display, uint32_t id,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001152 const char *interface, uint32_t version, void *data)
1153{
1154 struct desktop *desktop = data;
1155
1156 if (!strcmp(interface, "desktop_shell")) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03001157 desktop->interface_version = (version < 2) ? version : 2;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001158 desktop->shell = display_bind(desktop->display,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03001159 id, &desktop_shell_interface,
1160 desktop->interface_version);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001161 desktop_shell_add_listener(desktop->shell, &listener, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001162 } else if (!strcmp(interface, "wl_output")) {
1163 create_output(desktop, id);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001164 }
1165}
1166
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001167static void
1168launcher_section_done(void *data)
1169{
1170 struct desktop *desktop = data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001171 struct output *output;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001172
1173 if (key_launcher_icon == NULL || key_launcher_path == NULL) {
1174 fprintf(stderr, "invalid launcher section\n");
1175 return;
1176 }
1177
Martin Minarik1e51a872012-06-08 00:39:11 +02001178 wl_list_for_each(output, &desktop->outputs, link) {
Kristian Høgsberg53880802012-01-09 11:16:50 -05001179 panel_add_launcher(output->panel,
1180 key_launcher_icon, key_launcher_path);
Martin Minarik1e51a872012-06-08 00:39:11 +02001181 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001182
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001183 free(key_launcher_icon);
1184 key_launcher_icon = NULL;
1185 free(key_launcher_path);
1186 key_launcher_path = NULL;
1187}
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001188
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001189static void
1190add_default_launcher(struct desktop *desktop)
1191{
1192 struct output *output;
1193
1194 wl_list_for_each(output, &desktop->outputs, link)
1195 panel_add_launcher(output->panel,
1196 DATADIR "/weston/terminal.png",
Rodney Lorrimar99ff01b2012-02-29 17:31:03 +01001197 BINDIR "/weston-terminal");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001198}
1199
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001200int main(int argc, char *argv[])
1201{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001202 struct desktop desktop = { 0 };
Ossama Othmana50e6e42013-05-14 09:48:26 -07001203 int config_fd;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001204 struct output *output;
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001205 int ret;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001206
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001207 desktop.unlock_task.run = unlock_dialog_finish;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001208 wl_list_init(&desktop.outputs);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001209
Kristian Høgsberg4172f662013-02-20 15:27:49 -05001210 desktop.display = display_create(&argc, argv);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001211 if (desktop.display == NULL) {
1212 fprintf(stderr, "failed to create display: %m\n");
1213 return -1;
1214 }
1215
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001216 display_set_user_data(desktop.display, &desktop);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001217 display_set_global_handler(desktop.display, global_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001218
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001219 wl_list_for_each(output, &desktop.outputs, link) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001220 struct wl_surface *surface;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001221
1222 output->panel = panel_create(desktop.display);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001223 surface = window_get_wl_surface(output->panel->window);
1224 desktop_shell_set_panel(desktop.shell,
1225 output->output, surface);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001226
1227 output->background = background_create(&desktop);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001228 surface = window_get_wl_surface(output->background->window);
1229 desktop_shell_set_background(desktop.shell,
1230 output->output, surface);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001231 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001232
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001233 grab_surface_create(&desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001234
Ossama Othmana50e6e42013-05-14 09:48:26 -07001235 config_fd = open_config_file("weston.ini");
1236 ret = parse_config_file(config_fd,
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001237 config_sections, ARRAY_LENGTH(config_sections),
1238 &desktop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07001239 close(config_fd);
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001240 if (ret < 0)
1241 add_default_launcher(&desktop);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001242
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001243 signal(SIGCHLD, sigchild_handler);
1244
1245 display_run(desktop.display);
1246
U. Artie Eoff44874d92012-10-02 21:12:35 -07001247 /* Cleanup */
1248 grab_surface_destroy(&desktop);
1249 desktop_destroy_outputs(&desktop);
1250 if (desktop.unlock_dialog)
1251 unlock_dialog_destroy(desktop.unlock_dialog);
1252 desktop_shell_destroy(desktop.shell);
1253 display_destroy(desktop.display);
1254
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001255 return 0;
1256}