blob: 3b71403b12e277ecee9007f6b709820427c0ab7d [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,
663 BACKGROUND_TILE
664};
665
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400666static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500667background_draw(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400668{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500669 struct background *background = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400670 cairo_surface_t *surface, *image;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400671 cairo_pattern_t *pattern;
672 cairo_matrix_t matrix;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400673 cairo_t *cr;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400674 double sx, sy;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500675 struct rectangle allocation;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500676 int type = -1;
Pekka Paalanen9564c752012-10-24 09:43:08 +0300677 struct display *display;
678 struct wl_region *opaque;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400679
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500680 surface = window_get_surface(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400681
Alexander Larssonc584fa62013-05-22 14:41:32 +0200682 cr = widget_cairo_create(background->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400683 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
684 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
685 cairo_paint(cr);
686
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500687 widget_get_allocation(widget, &allocation);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500688 image = NULL;
689 if (key_background_image)
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400690 image = load_cairo_surface(key_background_image);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500691
692 if (strcmp(key_background_type, "scale") == 0)
693 type = BACKGROUND_SCALE;
694 else if (strcmp(key_background_type, "tile") == 0)
695 type = BACKGROUND_TILE;
696 else
697 fprintf(stderr, "invalid background-type: %s\n",
698 key_background_type);
699
700 if (image && type != -1) {
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400701 pattern = cairo_pattern_create_for_surface(image);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500702 switch (type) {
703 case BACKGROUND_SCALE:
704 sx = (double) cairo_image_surface_get_width(image) /
705 allocation.width;
706 sy = (double) cairo_image_surface_get_height(image) /
707 allocation.height;
708 cairo_matrix_init_scale(&matrix, sx, sy);
709 cairo_pattern_set_matrix(pattern, &matrix);
710 break;
711 case BACKGROUND_TILE:
712 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
713 break;
714 }
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400715 cairo_set_source(cr, pattern);
716 cairo_pattern_destroy (pattern);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400717 cairo_surface_destroy(image);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500718 } else {
719 set_hex_color(cr, key_background_color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400720 }
721
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500722 cairo_paint(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400723 cairo_destroy(cr);
724 cairo_surface_destroy(surface);
Pekka Paalanen9564c752012-10-24 09:43:08 +0300725
726 display = window_get_display(background->window);
727 opaque = wl_compositor_create_region(display_get_compositor(display));
728 wl_region_add(opaque, allocation.x, allocation.y,
729 allocation.width, allocation.height);
730 wl_surface_set_opaque_region(window_get_wl_surface(background->window), opaque);
731 wl_region_destroy(opaque);
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300732
733 background->painted = 1;
734 check_desktop_ready(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400735}
736
737static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100738background_configure(void *data,
739 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400740 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100741 int32_t width, int32_t height)
742{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500743 struct background *background =
744 (struct background *) window_get_user_data(window);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100745
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500746 widget_schedule_resize(background->widget, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100747}
748
749static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500750unlock_dialog_redraw_handler(struct widget *widget, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200751{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500752 struct unlock_dialog *dialog = data;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200753 struct rectangle allocation;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200754 cairo_surface_t *surface;
Alexander Larssonc584fa62013-05-22 14:41:32 +0200755 cairo_t *cr;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200756 cairo_pattern_t *pat;
757 double cx, cy, r, f;
758
Alexander Larssonc584fa62013-05-22 14:41:32 +0200759 cr = widget_cairo_create(widget);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500760
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500761 widget_get_allocation(dialog->widget, &allocation);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200762 cairo_rectangle(cr, allocation.x, allocation.y,
763 allocation.width, allocation.height);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200764 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
765 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500766 cairo_fill(cr);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200767
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500768 cairo_translate(cr, allocation.x, allocation.y);
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500769 if (dialog->button_focused)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200770 f = 1.0;
771 else
772 f = 0.7;
773
774 cx = allocation.width / 2.0;
775 cy = allocation.height / 2.0;
776 r = (cx < cy ? cx : cy) * 0.4;
777 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
778 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
779 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
780 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
781 cairo_set_source(cr, pat);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500782 cairo_pattern_destroy(pat);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200783 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
784 cairo_fill(cr);
785
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500786 widget_set_allocation(dialog->button,
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500787 allocation.x + cx - r,
788 allocation.y + cy - r, 2 * r, 2 * r);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200789
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200790 cairo_destroy(cr);
791
Alexander Larssonc584fa62013-05-22 14:41:32 +0200792 surface = window_get_surface(dialog->window);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200793 cairo_surface_destroy(surface);
794}
795
796static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500797unlock_dialog_button_handler(struct widget *widget,
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200798 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100799 uint32_t button,
800 enum wl_pointer_button_state state, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200801{
802 struct unlock_dialog *dialog = data;
803 struct desktop *desktop = dialog->desktop;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200804
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500805 if (button == BTN_LEFT) {
Daniel Stone4dbadb12012-05-30 16:31:51 +0100806 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
807 !dialog->closing) {
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200808 display_defer(desktop->display, &desktop->unlock_task);
809 dialog->closing = 1;
810 }
811 }
812}
813
814static void
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200815unlock_dialog_keyboard_focus_handler(struct window *window,
816 struct input *device, void *data)
817{
818 window_schedule_redraw(window);
819}
820
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500821static int
Kristian Høgsbergee143232012-01-09 08:42:24 -0500822unlock_dialog_widget_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400823 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400824 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500825{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500826 struct unlock_dialog *dialog = data;
827
828 dialog->button_focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500829 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500830
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300831 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500832}
833
834static void
835unlock_dialog_widget_leave_handler(struct widget *widget,
836 struct input *input, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200837{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500838 struct unlock_dialog *dialog = data;
839
840 dialog->button_focused = 0;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500841 widget_schedule_redraw(widget);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200842}
843
844static struct unlock_dialog *
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500845unlock_dialog_create(struct desktop *desktop)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200846{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500847 struct display *display = desktop->display;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200848 struct unlock_dialog *dialog;
849
850 dialog = malloc(sizeof *dialog);
851 if (!dialog)
852 return NULL;
853 memset(dialog, 0, sizeof *dialog);
854
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400855 dialog->window = window_create_custom(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500856 dialog->widget = frame_create(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200857 window_set_title(dialog->window, "Unlock your desktop");
858
859 window_set_user_data(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200860 window_set_keyboard_focus_handler(dialog->window,
861 unlock_dialog_keyboard_focus_handler);
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500862 dialog->button = widget_add_widget(dialog->widget, dialog);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500863 widget_set_redraw_handler(dialog->widget,
864 unlock_dialog_redraw_handler);
Kristian Høgsbergee143232012-01-09 08:42:24 -0500865 widget_set_enter_handler(dialog->button,
866 unlock_dialog_widget_enter_handler);
867 widget_set_leave_handler(dialog->button,
868 unlock_dialog_widget_leave_handler);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500869 widget_set_button_handler(dialog->button,
870 unlock_dialog_button_handler);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200871
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500872 desktop_shell_set_lock_surface(desktop->shell,
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400873 window_get_wl_surface(dialog->window));
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500874
Pekka Paalanen40e49ac2012-01-18 16:51:30 +0200875 window_schedule_resize(dialog->window, 260, 230);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200876
877 return dialog;
878}
879
880static void
881unlock_dialog_destroy(struct unlock_dialog *dialog)
882{
883 window_destroy(dialog->window);
884 free(dialog);
885}
886
887static void
888unlock_dialog_finish(struct task *task, uint32_t events)
889{
890 struct desktop *desktop =
Benjamin Franzked7759712011-11-22 12:38:48 +0100891 container_of(task, struct desktop, unlock_task);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200892
893 desktop_shell_unlock(desktop->shell);
894 unlock_dialog_destroy(desktop->unlock_dialog);
895 desktop->unlock_dialog = NULL;
896}
897
898static void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400899desktop_shell_configure(void *data,
900 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400901 uint32_t edges,
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400902 struct wl_surface *surface,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400903 int32_t width, int32_t height)
904{
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400905 struct window *window = wl_surface_get_user_data(surface);
Pekka Paalanen068ae942011-11-28 14:11:15 +0200906 struct surface *s = window_get_user_data(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400907
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400908 s->configure(data, desktop_shell, edges, window, width, height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400909}
910
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200911static void
912desktop_shell_prepare_lock_surface(void *data,
913 struct desktop_shell *desktop_shell)
914{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200915 struct desktop *desktop = data;
916
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200917 if (!key_locking) {
918 desktop_shell_unlock(desktop->shell);
919 return;
920 }
921
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200922 if (!desktop->unlock_dialog) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500923 desktop->unlock_dialog = unlock_dialog_create(desktop);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200924 desktop->unlock_dialog->desktop = desktop;
925 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200926}
927
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300928static void
929desktop_shell_grab_cursor(void *data,
930 struct desktop_shell *desktop_shell,
931 uint32_t cursor)
932{
933 struct desktop *desktop = data;
934
935 switch (cursor) {
Philipp Brüschweiler16d59d72012-08-24 15:43:55 +0200936 case DESKTOP_SHELL_CURSOR_NONE:
937 desktop->grab_cursor = CURSOR_BLANK;
938 break;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300939 case DESKTOP_SHELL_CURSOR_BUSY:
940 desktop->grab_cursor = CURSOR_WATCH;
941 break;
942 case DESKTOP_SHELL_CURSOR_MOVE:
943 desktop->grab_cursor = CURSOR_DRAGGING;
944 break;
945 case DESKTOP_SHELL_CURSOR_RESIZE_TOP:
946 desktop->grab_cursor = CURSOR_TOP;
947 break;
948 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM:
949 desktop->grab_cursor = CURSOR_BOTTOM;
950 break;
951 case DESKTOP_SHELL_CURSOR_RESIZE_LEFT:
952 desktop->grab_cursor = CURSOR_LEFT;
953 break;
954 case DESKTOP_SHELL_CURSOR_RESIZE_RIGHT:
955 desktop->grab_cursor = CURSOR_RIGHT;
956 break;
957 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_LEFT:
958 desktop->grab_cursor = CURSOR_TOP_LEFT;
959 break;
960 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_RIGHT:
961 desktop->grab_cursor = CURSOR_TOP_RIGHT;
962 break;
963 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_LEFT:
964 desktop->grab_cursor = CURSOR_BOTTOM_LEFT;
965 break;
966 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_RIGHT:
967 desktop->grab_cursor = CURSOR_BOTTOM_RIGHT;
968 break;
969 case DESKTOP_SHELL_CURSOR_ARROW:
970 default:
971 desktop->grab_cursor = CURSOR_LEFT_PTR;
972 }
973}
974
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400975static const struct desktop_shell_listener listener = {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200976 desktop_shell_configure,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300977 desktop_shell_prepare_lock_surface,
978 desktop_shell_grab_cursor
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400979};
980
U. Artie Eoff44874d92012-10-02 21:12:35 -0700981static void
982background_destroy(struct background *background)
983{
984 widget_destroy(background->widget);
985 window_destroy(background->window);
986
987 free(background);
988}
989
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100990static struct background *
991background_create(struct desktop *desktop)
992{
993 struct background *background;
994
995 background = malloc(sizeof *background);
996 memset(background, 0, sizeof *background);
997
998 background->base.configure = background_configure;
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400999 background->window = window_create_custom(desktop->display);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001000 background->widget = window_add_widget(background->window, background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001001 window_set_user_data(background->window, background);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001002 widget_set_redraw_handler(background->widget, background_draw);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001003
1004 return background;
1005}
1006
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001007static int
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001008grab_surface_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001009 float x, float y, void *data)
1010{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001011 struct desktop *desktop = data;
1012
1013 return desktop->grab_cursor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001014}
1015
1016static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001017grab_surface_destroy(struct desktop *desktop)
1018{
1019 widget_destroy(desktop->grab_widget);
1020 window_destroy(desktop->grab_window);
1021}
1022
1023static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001024grab_surface_create(struct desktop *desktop)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001025{
1026 struct wl_surface *s;
1027
Ander Conselvan de Oliveira07a91cd2012-07-16 14:15:50 +03001028 desktop->grab_window = window_create_custom(desktop->display);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001029 window_set_user_data(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001030
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001031 s = window_get_wl_surface(desktop->grab_window);
1032 desktop_shell_set_grab_surface(desktop->shell, s);
1033
1034 desktop->grab_widget =
1035 window_add_widget(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001036 /* We set the allocation to 1x1 at 0,0 so the fake enter event
1037 * at 0,0 will go to this widget. */
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001038 widget_set_allocation(desktop->grab_widget, 0, 0, 1, 1);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001039
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001040 widget_set_enter_handler(desktop->grab_widget,
1041 grab_surface_enter_handler);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001042}
1043
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001044static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001045output_destroy(struct output *output)
1046{
1047 background_destroy(output->background);
1048 panel_destroy(output->panel);
1049 wl_output_destroy(output->output);
1050 wl_list_remove(&output->link);
1051
1052 free(output);
1053}
1054
1055static void
1056desktop_destroy_outputs(struct desktop *desktop)
1057{
1058 struct output *tmp;
1059 struct output *output;
1060
1061 wl_list_for_each_safe(output, tmp, &desktop->outputs, link)
1062 output_destroy(output);
1063}
1064
1065static void
Alexander Larssonc584fa62013-05-22 14:41:32 +02001066output_handle_geometry(void *data,
1067 struct wl_output *wl_output,
1068 int x, int y,
1069 int physical_width,
1070 int physical_height,
1071 int subpixel,
1072 const char *make,
1073 const char *model,
1074 int transform)
1075{
1076 struct output *output = data;
1077
1078 window_set_buffer_transform(output->panel->window, transform);
1079 window_set_buffer_transform(output->background->window, transform);
1080}
1081
1082static void
1083output_handle_mode(void *data,
1084 struct wl_output *wl_output,
1085 uint32_t flags,
1086 int width,
1087 int height,
1088 int refresh)
1089{
1090}
1091
1092static void
1093output_handle_done(void *data,
1094 struct wl_output *wl_output)
1095{
1096}
1097
1098static void
1099output_handle_scale(void *data,
1100 struct wl_output *wl_output,
1101 uint32_t scale)
1102{
1103 struct output *output = data;
1104
1105 window_set_buffer_scale(output->panel->window, scale);
1106 window_set_buffer_scale(output->background->window, scale);
1107}
1108
1109static const struct wl_output_listener output_listener = {
1110 output_handle_geometry,
1111 output_handle_mode,
1112 output_handle_done,
1113 output_handle_scale
1114};
1115
1116static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001117create_output(struct desktop *desktop, uint32_t id)
1118{
1119 struct output *output;
1120
1121 output = calloc(1, sizeof *output);
1122 if (!output)
1123 return;
1124
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001125 output->output =
Alexander Larssonc584fa62013-05-22 14:41:32 +02001126 display_bind(desktop->display, id, &wl_output_interface, 2);
1127
1128 wl_output_add_listener(output->output, &output_listener, output);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001129
1130 wl_list_insert(&desktop->outputs, &output->link);
1131}
1132
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001133static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001134global_handler(struct display *display, uint32_t id,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001135 const char *interface, uint32_t version, void *data)
1136{
1137 struct desktop *desktop = data;
1138
1139 if (!strcmp(interface, "desktop_shell")) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03001140 desktop->interface_version = (version < 2) ? version : 2;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001141 desktop->shell = display_bind(desktop->display,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03001142 id, &desktop_shell_interface,
1143 desktop->interface_version);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001144 desktop_shell_add_listener(desktop->shell, &listener, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001145 } else if (!strcmp(interface, "wl_output")) {
1146 create_output(desktop, id);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001147 }
1148}
1149
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001150static void
1151launcher_section_done(void *data)
1152{
1153 struct desktop *desktop = data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001154 struct output *output;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001155
1156 if (key_launcher_icon == NULL || key_launcher_path == NULL) {
1157 fprintf(stderr, "invalid launcher section\n");
1158 return;
1159 }
1160
Martin Minarik1e51a872012-06-08 00:39:11 +02001161 wl_list_for_each(output, &desktop->outputs, link) {
Kristian Høgsberg53880802012-01-09 11:16:50 -05001162 panel_add_launcher(output->panel,
1163 key_launcher_icon, key_launcher_path);
Martin Minarik1e51a872012-06-08 00:39:11 +02001164 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001165
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001166 free(key_launcher_icon);
1167 key_launcher_icon = NULL;
1168 free(key_launcher_path);
1169 key_launcher_path = NULL;
1170}
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001171
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001172static void
1173add_default_launcher(struct desktop *desktop)
1174{
1175 struct output *output;
1176
1177 wl_list_for_each(output, &desktop->outputs, link)
1178 panel_add_launcher(output->panel,
1179 DATADIR "/weston/terminal.png",
Rodney Lorrimar99ff01b2012-02-29 17:31:03 +01001180 BINDIR "/weston-terminal");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001181}
1182
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001183int main(int argc, char *argv[])
1184{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001185 struct desktop desktop = { 0 };
Ossama Othmana50e6e42013-05-14 09:48:26 -07001186 int config_fd;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001187 struct output *output;
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001188 int ret;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001189
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001190 desktop.unlock_task.run = unlock_dialog_finish;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001191 wl_list_init(&desktop.outputs);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001192
Kristian Høgsberg4172f662013-02-20 15:27:49 -05001193 desktop.display = display_create(&argc, argv);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001194 if (desktop.display == NULL) {
1195 fprintf(stderr, "failed to create display: %m\n");
1196 return -1;
1197 }
1198
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001199 display_set_user_data(desktop.display, &desktop);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001200 display_set_global_handler(desktop.display, global_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001201
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001202 wl_list_for_each(output, &desktop.outputs, link) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001203 struct wl_surface *surface;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001204
1205 output->panel = panel_create(desktop.display);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001206 surface = window_get_wl_surface(output->panel->window);
1207 desktop_shell_set_panel(desktop.shell,
1208 output->output, surface);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001209
1210 output->background = background_create(&desktop);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001211 surface = window_get_wl_surface(output->background->window);
1212 desktop_shell_set_background(desktop.shell,
1213 output->output, surface);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001214 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001215
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001216 grab_surface_create(&desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001217
Ossama Othmana50e6e42013-05-14 09:48:26 -07001218 config_fd = open_config_file("weston.ini");
1219 ret = parse_config_file(config_fd,
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001220 config_sections, ARRAY_LENGTH(config_sections),
1221 &desktop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07001222 close(config_fd);
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001223 if (ret < 0)
1224 add_default_launcher(&desktop);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001225
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001226 signal(SIGCHLD, sigchild_handler);
1227
1228 display_run(desktop.display);
1229
U. Artie Eoff44874d92012-10-02 21:12:35 -07001230 /* Cleanup */
1231 grab_surface_destroy(&desktop);
1232 desktop_destroy_outputs(&desktop);
1233 if (desktop.unlock_dialog)
1234 unlock_dialog_destroy(desktop.unlock_dialog);
1235 desktop_shell_destroy(desktop.shell);
1236 display_destroy(desktop.display);
1237
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001238 return 0;
1239}