blob: 03b1b19305655229e82bb590cdb901c1e1f9c37c [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 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07005 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040011 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070012 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040023 */
24
U. Artie Eoff3c946772014-01-15 10:59:50 -080025#include "config.h"
26
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <fcntl.h>
32#include <unistd.h>
U. Artie Eoff3c946772014-01-15 10:59:50 -080033#include <errno.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040034#include <math.h>
35#include <cairo.h>
36#include <sys/wait.h>
Martin Minarik1e51a872012-06-08 00:39:11 +020037#include <sys/timerfd.h>
Michael Vetter2a18a522015-05-15 17:17:47 +020038#include <sys/epoll.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040039#include <linux/input.h>
Tiago Vignatti61500722012-05-23 22:06:28 +030040#include <libgen.h>
Kristian Høgsbergd1936b92012-07-23 22:59:33 -040041#include <ctype.h>
Martin Minarik1e51a872012-06-08 00:39:11 +020042#include <time.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040043
Pekka Paalanen50719bc2011-11-22 14:18:50 +020044#include <wayland-client.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040045#include "window.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070046#include "shared/cairo-util.h"
47#include "shared/config-parser.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040048
Pekka Paalanen50719bc2011-11-22 14:18:50 +020049#include "desktop-shell-client-protocol.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040050
Pekka Paalanenb6df4f72012-08-03 14:39:15 +030051extern char **environ; /* defined by libc */
52
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040053struct desktop {
54 struct display *display;
55 struct desktop_shell *shell;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030056 uint32_t interface_version;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +020057 struct unlock_dialog *unlock_dialog;
58 struct task unlock_task;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010059 struct wl_list outputs;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040060
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030061 struct window *grab_window;
62 struct widget *grab_widget;
63
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +030064 struct weston_config *config;
65 int locking;
66
Scott Moreauec116022012-07-22 18:23:52 -060067 enum cursor_type grab_cursor;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030068
69 int painted;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010070};
71
72struct surface {
73 void (*configure)(void *data,
74 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040075 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010076 int32_t width, int32_t height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040077};
78
79struct panel {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010080 struct surface base;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040081 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050082 struct widget *widget;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050083 struct wl_list launcher_list;
Martin Minarik1e51a872012-06-08 00:39:11 +020084 struct panel_clock *clock;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030085 int painted;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +030086 uint32_t color;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040087};
88
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010089struct background {
90 struct surface base;
91 struct window *window;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050092 struct widget *widget;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030093 int painted;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +030094
95 char *image;
96 int type;
97 uint32_t color;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010098};
99
100struct output {
101 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800102 uint32_t server_output_id;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100103 struct wl_list link;
104
105 struct panel *panel;
106 struct background *background;
107};
108
Kristian Høgsberg53880802012-01-09 11:16:50 -0500109struct panel_launcher {
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500110 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400111 struct panel *panel;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400112 cairo_surface_t *icon;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500113 int focused, pressed;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400114 char *path;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500115 struct wl_list link;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400116 struct wl_array envp;
117 struct wl_array argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400118};
119
Martin Minarik1e51a872012-06-08 00:39:11 +0200120struct panel_clock {
121 struct widget *widget;
122 struct panel *panel;
Martin Minarik1e51a872012-06-08 00:39:11 +0200123 struct task clock_task;
124 int clock_fd;
125};
126
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200127struct unlock_dialog {
128 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500129 struct widget *widget;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500130 struct widget *button;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500131 int button_focused;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200132 int closing;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200133 struct desktop *desktop;
134};
135
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300136static void
137panel_add_launchers(struct panel *panel, struct desktop *desktop);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500138
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400139static void
140sigchild_handler(int s)
141{
142 int status;
143 pid_t pid;
144
145 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
146 fprintf(stderr, "child %d exited\n", pid);
147}
148
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300149static int
150is_desktop_painted(struct desktop *desktop)
151{
152 struct output *output;
153
154 wl_list_for_each(output, &desktop->outputs, link) {
155 if (output->panel && !output->panel->painted)
156 return 0;
157 if (output->background && !output->background->painted)
158 return 0;
159 }
160
161 return 1;
162}
163
164static void
165check_desktop_ready(struct window *window)
166{
167 struct display *display;
168 struct desktop *desktop;
169
170 display = window_get_display(window);
171 desktop = display_get_user_data(display);
172
173 if (!desktop->painted && is_desktop_painted(desktop)) {
174 desktop->painted = 1;
175
176 if (desktop->interface_version >= 2)
177 desktop_shell_desktop_ready(desktop->shell);
178 }
179}
180
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400181static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500182panel_launcher_activate(struct panel_launcher *widget)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400183{
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400184 char **argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400185 pid_t pid;
186
187 pid = fork();
188 if (pid < 0) {
189 fprintf(stderr, "fork failed: %m\n");
190 return;
191 }
192
193 if (pid)
194 return;
Benjamin Franzked7759712011-11-22 12:38:48 +0100195
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400196 argv = widget->argv.data;
197 if (execve(argv[0], argv, widget->envp.data) < 0) {
198 fprintf(stderr, "execl '%s' failed: %m\n", argv[0]);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400199 exit(1);
200 }
201}
202
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400203static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500204panel_launcher_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400205{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500206 struct panel_launcher *launcher = data;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500207 struct rectangle allocation;
208 cairo_t *cr;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400209
Alexander Larssonc584fa62013-05-22 14:41:32 +0200210 cr = widget_cairo_create(launcher->panel->widget);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500211
212 widget_get_allocation(widget, &allocation);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500213 if (launcher->pressed) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500214 allocation.x++;
215 allocation.y++;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400216 }
217
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500218 cairo_set_source_surface(cr, launcher->icon,
219 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400220 cairo_paint(cr);
221
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500222 if (launcher->focused) {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400223 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500224 cairo_mask_surface(cr, launcher->icon,
225 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400226 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400227
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500228 cairo_destroy(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400229}
230
Tiago Vignatti61500722012-05-23 22:06:28 +0300231static int
232panel_launcher_motion_handler(struct widget *widget, struct input *input,
233 uint32_t time, float x, float y, void *data)
234{
235 struct panel_launcher *launcher = data;
236
237 widget_set_tooltip(widget, basename((char *)launcher->path), x, y);
238
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300239 return CURSOR_LEFT_PTR;
Tiago Vignatti61500722012-05-23 22:06:28 +0300240}
241
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400242static void
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500243set_hex_color(cairo_t *cr, uint32_t color)
244{
Michael Vetter2a18a522015-05-15 17:17:47 +0200245 cairo_set_source_rgba(cr,
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500246 ((color >> 16) & 0xff) / 255.0,
247 ((color >> 8) & 0xff) / 255.0,
248 ((color >> 0) & 0xff) / 255.0,
249 ((color >> 24) & 0xff) / 255.0);
250}
251
252static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500253panel_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400254{
255 cairo_surface_t *surface;
256 cairo_t *cr;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500257 struct panel *panel = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400258
Alexander Larssonc584fa62013-05-22 14:41:32 +0200259 cr = widget_cairo_create(panel->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400260 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300261 set_hex_color(cr, panel->color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400262 cairo_paint(cr);
263
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400264 cairo_destroy(cr);
Alexander Larssonc584fa62013-05-22 14:41:32 +0200265 surface = window_get_surface(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400266 cairo_surface_destroy(surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300267 panel->painted = 1;
268 check_desktop_ready(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400269}
270
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500271static int
Kristian Høgsberg53880802012-01-09 11:16:50 -0500272panel_launcher_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400273 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500274{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500275 struct panel_launcher *launcher = data;
276
277 launcher->focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500278 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500279
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300280 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500281}
282
283static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500284panel_launcher_leave_handler(struct widget *widget,
285 struct input *input, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400286{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500287 struct panel_launcher *launcher = data;
288
289 launcher->focused = 0;
Tiago Vignatti61500722012-05-23 22:06:28 +0300290 widget_destroy_tooltip(widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500291 widget_schedule_redraw(widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400292}
293
294static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500295panel_launcher_button_handler(struct widget *widget,
296 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100297 uint32_t button,
298 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400299{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500300 struct panel_launcher *launcher;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400301
Kristian Høgsberg53880802012-01-09 11:16:50 -0500302 launcher = widget_get_user_data(widget);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500303 widget_schedule_redraw(widget);
Daniel Stone4dbadb12012-05-30 16:31:51 +0100304 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg53880802012-01-09 11:16:50 -0500305 panel_launcher_activate(launcher);
Rusty Lynch4384a242013-08-08 21:28:22 -0700306
307}
308
309static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700310panel_launcher_touch_down_handler(struct widget *widget, struct input *input,
311 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch4384a242013-08-08 21:28:22 -0700312 float x, float y, void *data)
313{
314 struct panel_launcher *launcher;
315
316 launcher = widget_get_user_data(widget);
317 launcher->focused = 1;
318 widget_schedule_redraw(widget);
319}
320
321static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700322panel_launcher_touch_up_handler(struct widget *widget, struct input *input,
Michael Vetter2a18a522015-05-15 17:17:47 +0200323 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch1084da52013-08-15 09:10:08 -0700324 void *data)
Rusty Lynch4384a242013-08-08 21:28:22 -0700325{
326 struct panel_launcher *launcher;
327
328 launcher = widget_get_user_data(widget);
329 launcher->focused = 0;
330 widget_schedule_redraw(widget);
331 panel_launcher_activate(launcher);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500332}
333
Martin Minarik1e51a872012-06-08 00:39:11 +0200334static void
335clock_func(struct task *task, uint32_t events)
336{
337 struct panel_clock *clock =
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400338 container_of(task, struct panel_clock, clock_task);
Kristian Høgsberg70226bb2012-06-08 16:54:52 -0400339 uint64_t exp;
Martin Minarik1e51a872012-06-08 00:39:11 +0200340
Martin Olsson8df662a2012-07-08 03:03:47 +0200341 if (read(clock->clock_fd, &exp, sizeof exp) != sizeof exp)
342 abort();
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400343 widget_schedule_redraw(clock->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200344}
345
346static void
347panel_clock_redraw_handler(struct widget *widget, void *data)
348{
Martin Minarik1e51a872012-06-08 00:39:11 +0200349 struct panel_clock *clock = data;
350 cairo_t *cr;
351 struct rectangle allocation;
352 cairo_text_extents_t extents;
353 cairo_font_extents_t font_extents;
Martin Minarik1e51a872012-06-08 00:39:11 +0200354 time_t rawtime;
355 struct tm * timeinfo;
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400356 char string[128];
Martin Minarik1e51a872012-06-08 00:39:11 +0200357
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400358 time(&rawtime);
359 timeinfo = localtime(&rawtime);
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400360 strftime(string, sizeof string, "%a %b %d, %I:%M %p", timeinfo);
Martin Minarik1e51a872012-06-08 00:39:11 +0200361
362 widget_get_allocation(widget, &allocation);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400363 if (allocation.width == 0)
364 return;
Martin Minarik1e51a872012-06-08 00:39:11 +0200365
Alexander Larssonc584fa62013-05-22 14:41:32 +0200366 cr = widget_cairo_create(clock->panel->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200367 cairo_select_font_face(cr, "sans",
368 CAIRO_FONT_SLANT_NORMAL,
369 CAIRO_FONT_WEIGHT_NORMAL);
370 cairo_set_font_size(cr, 14);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400371 cairo_text_extents(cr, string, &extents);
Martin Minarik1e51a872012-06-08 00:39:11 +0200372 cairo_font_extents (cr, &font_extents);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400373 cairo_move_to(cr, allocation.x + 5,
374 allocation.y + 3 * (allocation.height >> 2) + 1);
375 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400376 cairo_show_text(cr, string);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400377 cairo_move_to(cr, allocation.x + 4,
378 allocation.y + 3 * (allocation.height >> 2));
379 cairo_set_source_rgb(cr, 1, 1, 1);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400380 cairo_show_text(cr, string);
Martin Minarik1e51a872012-06-08 00:39:11 +0200381 cairo_destroy(cr);
382}
383
384static int
385clock_timer_reset(struct panel_clock *clock)
386{
387 struct itimerspec its;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400388
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400389 its.it_interval.tv_sec = 60;
Martin Minarik1e51a872012-06-08 00:39:11 +0200390 its.it_interval.tv_nsec = 0;
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400391 its.it_value.tv_sec = 60;
Martin Minarik1e51a872012-06-08 00:39:11 +0200392 its.it_value.tv_nsec = 0;
393 if (timerfd_settime(clock->clock_fd, 0, &its, NULL) < 0) {
394 fprintf(stderr, "could not set timerfd\n: %m");
395 return -1;
396 }
397
398 return 0;
399}
400
401static void
U. Artie Eoff44874d92012-10-02 21:12:35 -0700402panel_destroy_clock(struct panel_clock *clock)
403{
404 widget_destroy(clock->widget);
405
406 close(clock->clock_fd);
407
408 free(clock);
409}
410
411static void
Martin Minarik1e51a872012-06-08 00:39:11 +0200412panel_add_clock(struct panel *panel)
413{
414 struct panel_clock *clock;
415 int timerfd;
416
417 timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
418 if (timerfd < 0) {
419 fprintf(stderr, "could not create timerfd\n: %m");
420 return;
421 }
422
Peter Huttererf3d62272013-08-08 11:57:05 +1000423 clock = xzalloc(sizeof *clock);
Martin Minarik1e51a872012-06-08 00:39:11 +0200424 clock->panel = panel;
425 panel->clock = clock;
426 clock->clock_fd = timerfd;
427
428 clock->clock_task.run = clock_func;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400429 display_watch_fd(window_get_display(panel->window), clock->clock_fd,
430 EPOLLIN, &clock->clock_task);
Martin Minarik1e51a872012-06-08 00:39:11 +0200431 clock_timer_reset(clock);
432
433 clock->widget = widget_add_widget(panel->widget, clock);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400434 widget_set_redraw_handler(clock->widget, panel_clock_redraw_handler);
Martin Minarik1e51a872012-06-08 00:39:11 +0200435}
436
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500437static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500438panel_resize_handler(struct widget *widget,
439 int32_t width, int32_t height, void *data)
440{
441 struct panel_launcher *launcher;
442 struct panel *panel = data;
443 int x, y, w, h;
Michael Vetter2a18a522015-05-15 17:17:47 +0200444
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500445 x = 10;
446 y = 16;
447 wl_list_for_each(launcher, &panel->launcher_list, link) {
448 w = cairo_image_surface_get_width(launcher->icon);
449 h = cairo_image_surface_get_height(launcher->icon);
450 widget_set_allocation(launcher->widget,
451 x, y - h / 2, w + 1, h + 1);
452 x += w + 10;
453 }
Martin Minarik1e51a872012-06-08 00:39:11 +0200454 h=20;
455 w=170;
Pekka Paalanen01b17252012-06-12 17:42:26 +0300456
457 if (panel->clock)
458 widget_set_allocation(panel->clock->widget,
459 width - w - 8, y - h / 2, w + 1, h + 1);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500460}
461
462static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100463panel_configure(void *data,
464 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400465 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100466 int32_t width, int32_t height)
467{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500468 struct surface *surface = window_get_user_data(window);
469 struct panel *panel = container_of(surface, struct panel, base);
470
471 window_schedule_resize(panel->window, width, 32);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100472}
473
U. Artie Eoff44874d92012-10-02 21:12:35 -0700474static void
475panel_destroy_launcher(struct panel_launcher *launcher)
476{
477 wl_array_release(&launcher->argv);
478 wl_array_release(&launcher->envp);
479
480 free(launcher->path);
481
482 cairo_surface_destroy(launcher->icon);
483
484 widget_destroy(launcher->widget);
485 wl_list_remove(&launcher->link);
486
487 free(launcher);
488}
489
490static void
491panel_destroy(struct panel *panel)
492{
493 struct panel_launcher *tmp;
494 struct panel_launcher *launcher;
495
496 panel_destroy_clock(panel->clock);
497
498 wl_list_for_each_safe(launcher, tmp, &panel->launcher_list, link)
499 panel_destroy_launcher(launcher);
500
501 widget_destroy(panel->widget);
502 window_destroy(panel->window);
503
504 free(panel);
505}
506
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400507static struct panel *
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300508panel_create(struct desktop *desktop)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400509{
510 struct panel *panel;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300511 struct weston_config_section *s;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400512
Peter Huttererf3d62272013-08-08 11:57:05 +1000513 panel = xzalloc(sizeof *panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400514
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100515 panel->base.configure = panel_configure;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300516 panel->window = window_create_custom(desktop->display);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500517 panel->widget = window_add_widget(panel->window, panel);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500518 wl_list_init(&panel->launcher_list);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400519
520 window_set_title(panel->window, "panel");
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400521 window_set_user_data(panel->window, panel);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500522
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500523 widget_set_redraw_handler(panel->widget, panel_redraw_handler);
524 widget_set_resize_handler(panel->widget, panel_resize_handler);
Michael Vetter2a18a522015-05-15 17:17:47 +0200525
Rafal Mielniczukb9e513c2012-06-09 20:33:29 +0200526 panel_add_clock(panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400527
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300528 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
529 weston_config_section_get_uint(s, "panel-color",
530 &panel->color, 0xaa000000);
531
532 panel_add_launchers(panel, desktop);
533
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400534 return panel;
535}
536
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200537static cairo_surface_t *
538load_icon_or_fallback(const char *icon)
539{
540 cairo_surface_t *surface = cairo_image_surface_create_from_png(icon);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200541 cairo_status_t status;
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200542 cairo_t *cr;
543
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200544 status = cairo_surface_status(surface);
545 if (status == CAIRO_STATUS_SUCCESS)
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200546 return surface;
547
548 cairo_surface_destroy(surface);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200549 fprintf(stderr, "ERROR loading icon from file '%s', error: '%s'\n",
550 icon, cairo_status_to_string(status));
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200551
552 /* draw fallback icon */
553 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
554 20, 20);
555 cr = cairo_create(surface);
556
557 cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1);
558 cairo_paint(cr);
559
560 cairo_set_source_rgba(cr, 0, 0, 0, 1);
561 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
562 cairo_rectangle(cr, 0, 0, 20, 20);
563 cairo_move_to(cr, 4, 4);
564 cairo_line_to(cr, 16, 16);
565 cairo_move_to(cr, 4, 16);
566 cairo_line_to(cr, 16, 4);
567 cairo_stroke(cr);
568
569 cairo_destroy(cr);
570
571 return surface;
572}
573
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400574static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500575panel_add_launcher(struct panel *panel, const char *icon, const char *path)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400576{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500577 struct panel_launcher *launcher;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400578 char *start, *p, *eq, **ps;
579 int i, j, k;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400580
Peter Huttererf3d62272013-08-08 11:57:05 +1000581 launcher = xzalloc(sizeof *launcher);
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200582 launcher->icon = load_icon_or_fallback(icon);
U. Artie Eoff3c946772014-01-15 10:59:50 -0800583 launcher->path = xstrdup(path);
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400584
585 wl_array_init(&launcher->envp);
586 wl_array_init(&launcher->argv);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300587 for (i = 0; environ[i]; i++) {
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400588 ps = wl_array_add(&launcher->envp, sizeof *ps);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300589 *ps = environ[i];
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400590 }
591 j = 0;
592
593 start = launcher->path;
594 while (*start) {
595 for (p = start, eq = NULL; *p && !isspace(*p); p++)
596 if (*p == '=')
597 eq = p;
598
599 if (eq && j == 0) {
600 ps = launcher->envp.data;
601 for (k = 0; k < i; k++)
602 if (strncmp(ps[k], start, eq - start) == 0) {
603 ps[k] = start;
604 break;
605 }
606 if (k == i) {
607 ps = wl_array_add(&launcher->envp, sizeof *ps);
608 *ps = start;
609 i++;
610 }
611 } else {
612 ps = wl_array_add(&launcher->argv, sizeof *ps);
613 *ps = start;
614 j++;
615 }
616
617 while (*p && isspace(*p))
618 *p++ = '\0';
619
620 start = p;
621 }
622
623 ps = wl_array_add(&launcher->envp, sizeof *ps);
624 *ps = NULL;
625 ps = wl_array_add(&launcher->argv, sizeof *ps);
626 *ps = NULL;
627
Kristian Høgsberg53880802012-01-09 11:16:50 -0500628 launcher->panel = panel;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500629 wl_list_insert(panel->launcher_list.prev, &launcher->link);
630
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500631 launcher->widget = widget_add_widget(panel->widget, launcher);
Kristian Høgsberg53880802012-01-09 11:16:50 -0500632 widget_set_enter_handler(launcher->widget,
633 panel_launcher_enter_handler);
634 widget_set_leave_handler(launcher->widget,
635 panel_launcher_leave_handler);
636 widget_set_button_handler(launcher->widget,
637 panel_launcher_button_handler);
Rusty Lynch4384a242013-08-08 21:28:22 -0700638 widget_set_touch_down_handler(launcher->widget,
639 panel_launcher_touch_down_handler);
640 widget_set_touch_up_handler(launcher->widget,
641 panel_launcher_touch_up_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500642 widget_set_redraw_handler(launcher->widget,
643 panel_launcher_redraw_handler);
Tiago Vignatti61500722012-05-23 22:06:28 +0300644 widget_set_motion_handler(launcher->widget,
645 panel_launcher_motion_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400646}
647
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500648enum {
649 BACKGROUND_SCALE,
Pekka Paalanena402b052013-05-22 18:03:10 +0300650 BACKGROUND_SCALE_CROP,
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500651 BACKGROUND_TILE
652};
653
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400654static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500655background_draw(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400656{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500657 struct background *background = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400658 cairo_surface_t *surface, *image;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400659 cairo_pattern_t *pattern;
660 cairo_matrix_t matrix;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400661 cairo_t *cr;
Pekka Paalanena402b052013-05-22 18:03:10 +0300662 double im_w, im_h;
663 double sx, sy, s;
664 double tx, ty;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500665 struct rectangle allocation;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400666
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500667 surface = window_get_surface(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400668
Alexander Larssonc584fa62013-05-22 14:41:32 +0200669 cr = widget_cairo_create(background->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400670 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
671 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
672 cairo_paint(cr);
673
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500674 widget_get_allocation(widget, &allocation);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500675 image = NULL;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300676 if (background->image)
677 image = load_cairo_surface(background->image);
Kristian Høgsberg4c3661f2014-04-21 22:54:37 -0700678 else if (background->color == 0)
679 image = load_cairo_surface(DATADIR "/weston/pattern.png");
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500680
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300681 if (image && background->type != -1) {
Pekka Paalanena402b052013-05-22 18:03:10 +0300682 im_w = cairo_image_surface_get_width(image);
683 im_h = cairo_image_surface_get_height(image);
684 sx = im_w / allocation.width;
685 sy = im_h / allocation.height;
686
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400687 pattern = cairo_pattern_create_for_surface(image);
Pekka Paalanena402b052013-05-22 18:03:10 +0300688
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300689 switch (background->type) {
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500690 case BACKGROUND_SCALE:
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500691 cairo_matrix_init_scale(&matrix, sx, sy);
692 cairo_pattern_set_matrix(pattern, &matrix);
Bill Spitzak79b7cb32014-05-08 20:00:35 -0700693 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500694 break;
Pekka Paalanena402b052013-05-22 18:03:10 +0300695 case BACKGROUND_SCALE_CROP:
696 s = (sx < sy) ? sx : sy;
697 /* align center */
698 tx = (im_w - s * allocation.width) * 0.5;
699 ty = (im_h - s * allocation.height) * 0.5;
700 cairo_matrix_init_translate(&matrix, tx, ty);
701 cairo_matrix_scale(&matrix, s, s);
702 cairo_pattern_set_matrix(pattern, &matrix);
Bill Spitzak79b7cb32014-05-08 20:00:35 -0700703 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
Pekka Paalanena402b052013-05-22 18:03:10 +0300704 break;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500705 case BACKGROUND_TILE:
706 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
707 break;
708 }
Pekka Paalanena402b052013-05-22 18:03:10 +0300709
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400710 cairo_set_source(cr, pattern);
711 cairo_pattern_destroy (pattern);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400712 cairo_surface_destroy(image);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500713 } else {
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300714 set_hex_color(cr, background->color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400715 }
716
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500717 cairo_paint(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400718 cairo_destroy(cr);
719 cairo_surface_destroy(surface);
Pekka Paalanen9564c752012-10-24 09:43:08 +0300720
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300721 background->painted = 1;
722 check_desktop_ready(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400723}
724
725static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100726background_configure(void *data,
727 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400728 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100729 int32_t width, int32_t height)
730{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500731 struct background *background =
732 (struct background *) window_get_user_data(window);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100733
Kristian Høgsberg4598f962014-01-01 16:32:09 -0800734 widget_schedule_resize(background->widget, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100735}
736
737static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500738unlock_dialog_redraw_handler(struct widget *widget, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200739{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500740 struct unlock_dialog *dialog = data;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200741 struct rectangle allocation;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200742 cairo_surface_t *surface;
Alexander Larssonc584fa62013-05-22 14:41:32 +0200743 cairo_t *cr;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200744 cairo_pattern_t *pat;
745 double cx, cy, r, f;
746
Alexander Larssonc584fa62013-05-22 14:41:32 +0200747 cr = widget_cairo_create(widget);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500748
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500749 widget_get_allocation(dialog->widget, &allocation);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200750 cairo_rectangle(cr, allocation.x, allocation.y,
751 allocation.width, allocation.height);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200752 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
753 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500754 cairo_fill(cr);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200755
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500756 cairo_translate(cr, allocation.x, allocation.y);
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500757 if (dialog->button_focused)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200758 f = 1.0;
759 else
760 f = 0.7;
761
762 cx = allocation.width / 2.0;
763 cy = allocation.height / 2.0;
764 r = (cx < cy ? cx : cy) * 0.4;
765 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
766 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
767 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
768 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
769 cairo_set_source(cr, pat);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500770 cairo_pattern_destroy(pat);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200771 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
772 cairo_fill(cr);
773
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500774 widget_set_allocation(dialog->button,
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500775 allocation.x + cx - r,
776 allocation.y + cy - r, 2 * r, 2 * r);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200777
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200778 cairo_destroy(cr);
779
Alexander Larssonc584fa62013-05-22 14:41:32 +0200780 surface = window_get_surface(dialog->window);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200781 cairo_surface_destroy(surface);
782}
783
784static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500785unlock_dialog_button_handler(struct widget *widget,
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200786 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100787 uint32_t button,
788 enum wl_pointer_button_state state, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200789{
790 struct unlock_dialog *dialog = data;
791 struct desktop *desktop = dialog->desktop;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200792
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500793 if (button == BTN_LEFT) {
Daniel Stone4dbadb12012-05-30 16:31:51 +0100794 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
795 !dialog->closing) {
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200796 display_defer(desktop->display, &desktop->unlock_task);
797 dialog->closing = 1;
798 }
799 }
800}
801
802static void
Brian J Lovinc4df4082013-08-26 15:58:22 -0700803unlock_dialog_touch_down_handler(struct widget *widget, struct input *input,
804 uint32_t serial, uint32_t time, int32_t id,
805 float x, float y, void *data)
806{
807 struct unlock_dialog *dialog = data;
808
809 dialog->button_focused = 1;
810 widget_schedule_redraw(widget);
811}
812
813static void
814unlock_dialog_touch_up_handler(struct widget *widget, struct input *input,
815 uint32_t serial, uint32_t time, int32_t id,
816 void *data)
817{
818 struct unlock_dialog *dialog = data;
819 struct desktop *desktop = dialog->desktop;
820
821 dialog->button_focused = 0;
822 widget_schedule_redraw(widget);
823 display_defer(desktop->display, &desktop->unlock_task);
824 dialog->closing = 1;
825}
826
827static void
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200828unlock_dialog_keyboard_focus_handler(struct window *window,
829 struct input *device, void *data)
830{
831 window_schedule_redraw(window);
832}
833
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500834static int
Kristian Høgsbergee143232012-01-09 08:42:24 -0500835unlock_dialog_widget_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400836 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400837 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500838{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500839 struct unlock_dialog *dialog = data;
840
841 dialog->button_focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500842 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500843
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300844 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500845}
846
847static void
848unlock_dialog_widget_leave_handler(struct widget *widget,
849 struct input *input, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200850{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500851 struct unlock_dialog *dialog = data;
852
853 dialog->button_focused = 0;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500854 widget_schedule_redraw(widget);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200855}
856
857static struct unlock_dialog *
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500858unlock_dialog_create(struct desktop *desktop)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200859{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500860 struct display *display = desktop->display;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200861 struct unlock_dialog *dialog;
862
Peter Huttererf3d62272013-08-08 11:57:05 +1000863 dialog = xzalloc(sizeof *dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200864
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400865 dialog->window = window_create_custom(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500866 dialog->widget = window_frame_create(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200867 window_set_title(dialog->window, "Unlock your desktop");
868
869 window_set_user_data(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200870 window_set_keyboard_focus_handler(dialog->window,
871 unlock_dialog_keyboard_focus_handler);
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500872 dialog->button = widget_add_widget(dialog->widget, dialog);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500873 widget_set_redraw_handler(dialog->widget,
874 unlock_dialog_redraw_handler);
Kristian Høgsbergee143232012-01-09 08:42:24 -0500875 widget_set_enter_handler(dialog->button,
876 unlock_dialog_widget_enter_handler);
877 widget_set_leave_handler(dialog->button,
878 unlock_dialog_widget_leave_handler);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500879 widget_set_button_handler(dialog->button,
880 unlock_dialog_button_handler);
Brian J Lovinc4df4082013-08-26 15:58:22 -0700881 widget_set_touch_down_handler(dialog->button,
882 unlock_dialog_touch_down_handler);
883 widget_set_touch_up_handler(dialog->button,
884 unlock_dialog_touch_up_handler);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200885
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500886 desktop_shell_set_lock_surface(desktop->shell,
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400887 window_get_wl_surface(dialog->window));
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500888
Pekka Paalanen40e49ac2012-01-18 16:51:30 +0200889 window_schedule_resize(dialog->window, 260, 230);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200890
891 return dialog;
892}
893
894static void
895unlock_dialog_destroy(struct unlock_dialog *dialog)
896{
897 window_destroy(dialog->window);
898 free(dialog);
899}
900
901static void
902unlock_dialog_finish(struct task *task, uint32_t events)
903{
904 struct desktop *desktop =
Benjamin Franzked7759712011-11-22 12:38:48 +0100905 container_of(task, struct desktop, unlock_task);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200906
907 desktop_shell_unlock(desktop->shell);
908 unlock_dialog_destroy(desktop->unlock_dialog);
909 desktop->unlock_dialog = NULL;
910}
911
912static void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400913desktop_shell_configure(void *data,
914 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400915 uint32_t edges,
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400916 struct wl_surface *surface,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400917 int32_t width, int32_t height)
918{
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400919 struct window *window = wl_surface_get_user_data(surface);
Pekka Paalanen068ae942011-11-28 14:11:15 +0200920 struct surface *s = window_get_user_data(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400921
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400922 s->configure(data, desktop_shell, edges, window, width, height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400923}
924
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200925static void
926desktop_shell_prepare_lock_surface(void *data,
927 struct desktop_shell *desktop_shell)
928{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200929 struct desktop *desktop = data;
930
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300931 if (!desktop->locking) {
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200932 desktop_shell_unlock(desktop->shell);
933 return;
934 }
935
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200936 if (!desktop->unlock_dialog) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500937 desktop->unlock_dialog = unlock_dialog_create(desktop);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200938 desktop->unlock_dialog->desktop = desktop;
939 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200940}
941
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300942static void
943desktop_shell_grab_cursor(void *data,
944 struct desktop_shell *desktop_shell,
945 uint32_t cursor)
946{
947 struct desktop *desktop = data;
948
949 switch (cursor) {
Philipp Brüschweiler16d59d72012-08-24 15:43:55 +0200950 case DESKTOP_SHELL_CURSOR_NONE:
951 desktop->grab_cursor = CURSOR_BLANK;
952 break;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300953 case DESKTOP_SHELL_CURSOR_BUSY:
954 desktop->grab_cursor = CURSOR_WATCH;
955 break;
956 case DESKTOP_SHELL_CURSOR_MOVE:
957 desktop->grab_cursor = CURSOR_DRAGGING;
958 break;
959 case DESKTOP_SHELL_CURSOR_RESIZE_TOP:
960 desktop->grab_cursor = CURSOR_TOP;
961 break;
962 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM:
963 desktop->grab_cursor = CURSOR_BOTTOM;
964 break;
965 case DESKTOP_SHELL_CURSOR_RESIZE_LEFT:
966 desktop->grab_cursor = CURSOR_LEFT;
967 break;
968 case DESKTOP_SHELL_CURSOR_RESIZE_RIGHT:
969 desktop->grab_cursor = CURSOR_RIGHT;
970 break;
971 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_LEFT:
972 desktop->grab_cursor = CURSOR_TOP_LEFT;
973 break;
974 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_RIGHT:
975 desktop->grab_cursor = CURSOR_TOP_RIGHT;
976 break;
977 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_LEFT:
978 desktop->grab_cursor = CURSOR_BOTTOM_LEFT;
979 break;
980 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_RIGHT:
981 desktop->grab_cursor = CURSOR_BOTTOM_RIGHT;
982 break;
983 case DESKTOP_SHELL_CURSOR_ARROW:
984 default:
985 desktop->grab_cursor = CURSOR_LEFT_PTR;
986 }
987}
988
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400989static const struct desktop_shell_listener listener = {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200990 desktop_shell_configure,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300991 desktop_shell_prepare_lock_surface,
992 desktop_shell_grab_cursor
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400993};
994
U. Artie Eoff44874d92012-10-02 21:12:35 -0700995static void
996background_destroy(struct background *background)
997{
998 widget_destroy(background->widget);
999 window_destroy(background->window);
1000
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001001 free(background->image);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001002 free(background);
1003}
1004
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001005static struct background *
1006background_create(struct desktop *desktop)
1007{
1008 struct background *background;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001009 struct weston_config_section *s;
1010 char *type;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001011
Peter Huttererf3d62272013-08-08 11:57:05 +10001012 background = xzalloc(sizeof *background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001013 background->base.configure = background_configure;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001014 background->window = window_create_custom(desktop->display);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001015 background->widget = window_add_widget(background->window, background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001016 window_set_user_data(background->window, background);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001017 widget_set_redraw_handler(background->widget, background_draw);
Arnaud Vracfb754a02014-08-25 20:56:49 +02001018 widget_set_transparent(background->widget, 0);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001019 window_set_preferred_format(background->window,
1020 WINDOW_PREFERRED_FORMAT_RGB565);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001021
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001022 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
1023 weston_config_section_get_string(s, "background-image",
Kristian Høgsberg4c3661f2014-04-21 22:54:37 -07001024 &background->image, NULL);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001025 weston_config_section_get_uint(s, "background-color",
Kristian Høgsberg4c3661f2014-04-21 22:54:37 -07001026 &background->color, 0);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001027
1028 weston_config_section_get_string(s, "background-type",
1029 &type, "tile");
U. Artie Eoff3c946772014-01-15 10:59:50 -08001030 if (type == NULL) {
1031 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
1032 exit(EXIT_FAILURE);
1033 }
1034
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001035 if (strcmp(type, "scale") == 0) {
1036 background->type = BACKGROUND_SCALE;
1037 } else if (strcmp(type, "scale-crop") == 0) {
1038 background->type = BACKGROUND_SCALE_CROP;
1039 } else if (strcmp(type, "tile") == 0) {
1040 background->type = BACKGROUND_TILE;
1041 } else {
1042 background->type = -1;
1043 fprintf(stderr, "invalid background-type: %s\n",
1044 type);
1045 }
1046
1047 free(type);
1048
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001049 return background;
1050}
1051
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001052static int
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001053grab_surface_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001054 float x, float y, void *data)
1055{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001056 struct desktop *desktop = data;
1057
1058 return desktop->grab_cursor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001059}
1060
1061static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001062grab_surface_destroy(struct desktop *desktop)
1063{
1064 widget_destroy(desktop->grab_widget);
1065 window_destroy(desktop->grab_window);
1066}
1067
1068static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001069grab_surface_create(struct desktop *desktop)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001070{
1071 struct wl_surface *s;
1072
Ander Conselvan de Oliveira07a91cd2012-07-16 14:15:50 +03001073 desktop->grab_window = window_create_custom(desktop->display);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001074 window_set_user_data(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001075
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001076 s = window_get_wl_surface(desktop->grab_window);
1077 desktop_shell_set_grab_surface(desktop->shell, s);
1078
1079 desktop->grab_widget =
1080 window_add_widget(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001081 /* We set the allocation to 1x1 at 0,0 so the fake enter event
1082 * at 0,0 will go to this widget. */
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001083 widget_set_allocation(desktop->grab_widget, 0, 0, 1, 1);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001084
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001085 widget_set_enter_handler(desktop->grab_widget,
1086 grab_surface_enter_handler);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001087}
1088
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001089static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001090output_destroy(struct output *output)
1091{
1092 background_destroy(output->background);
Jonny Lambe67118c2014-08-12 15:07:51 +02001093 if (output->panel)
1094 panel_destroy(output->panel);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001095 wl_output_destroy(output->output);
1096 wl_list_remove(&output->link);
1097
1098 free(output);
1099}
1100
1101static void
1102desktop_destroy_outputs(struct desktop *desktop)
1103{
1104 struct output *tmp;
1105 struct output *output;
1106
1107 wl_list_for_each_safe(output, tmp, &desktop->outputs, link)
1108 output_destroy(output);
1109}
1110
1111static void
Alexander Larssonc584fa62013-05-22 14:41:32 +02001112output_handle_geometry(void *data,
1113 struct wl_output *wl_output,
1114 int x, int y,
1115 int physical_width,
1116 int physical_height,
1117 int subpixel,
1118 const char *make,
1119 const char *model,
1120 int transform)
1121{
1122 struct output *output = data;
1123
Jonny Lambe67118c2014-08-12 15:07:51 +02001124 if (output->panel)
1125 window_set_buffer_transform(output->panel->window, transform);
Alexander Larssonc584fa62013-05-22 14:41:32 +02001126 window_set_buffer_transform(output->background->window, transform);
1127}
1128
1129static void
1130output_handle_mode(void *data,
1131 struct wl_output *wl_output,
1132 uint32_t flags,
1133 int width,
1134 int height,
1135 int refresh)
1136{
1137}
1138
1139static void
1140output_handle_done(void *data,
1141 struct wl_output *wl_output)
1142{
1143}
1144
1145static void
1146output_handle_scale(void *data,
1147 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001148 int32_t scale)
Alexander Larssonc584fa62013-05-22 14:41:32 +02001149{
1150 struct output *output = data;
1151
Jonny Lambe67118c2014-08-12 15:07:51 +02001152 if (output->panel)
1153 window_set_buffer_scale(output->panel->window, scale);
Alexander Larssonc584fa62013-05-22 14:41:32 +02001154 window_set_buffer_scale(output->background->window, scale);
1155}
1156
1157static const struct wl_output_listener output_listener = {
1158 output_handle_geometry,
1159 output_handle_mode,
1160 output_handle_done,
1161 output_handle_scale
1162};
1163
Jonny Lambe67118c2014-08-12 15:07:51 +02001164static int
1165want_panel(struct desktop *desktop)
1166{
1167 struct weston_config_section *s;
1168 char *location = NULL;
1169 int ret = 1;
1170
1171 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
1172 weston_config_section_get_string(s, "panel-location",
1173 &location, "top");
1174
1175 if (strcmp(location, "top") != 0)
1176 ret = 0;
1177
1178 free(location);
1179
1180 return ret;
1181}
1182
Alexander Larssonc584fa62013-05-22 14:41:32 +02001183static void
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001184output_init(struct output *output, struct desktop *desktop)
1185{
1186 struct wl_surface *surface;
1187
Jonny Lambe67118c2014-08-12 15:07:51 +02001188 if (want_panel(desktop)) {
1189 output->panel = panel_create(desktop);
1190 surface = window_get_wl_surface(output->panel->window);
1191 desktop_shell_set_panel(desktop->shell,
1192 output->output, surface);
1193 }
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001194
1195 output->background = background_create(desktop);
1196 surface = window_get_wl_surface(output->background->window);
1197 desktop_shell_set_background(desktop->shell,
1198 output->output, surface);
1199}
1200
1201static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001202create_output(struct desktop *desktop, uint32_t id)
1203{
1204 struct output *output;
1205
1206 output = calloc(1, sizeof *output);
1207 if (!output)
1208 return;
1209
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001210 output->output =
Alexander Larssonc584fa62013-05-22 14:41:32 +02001211 display_bind(desktop->display, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001212 output->server_output_id = id;
Alexander Larssonc584fa62013-05-22 14:41:32 +02001213
1214 wl_output_add_listener(output->output, &output_listener, output);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001215
1216 wl_list_insert(&desktop->outputs, &output->link);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001217
1218 /* On start up we may process an output global before the shell global
1219 * in which case we can't create the panel and background just yet */
1220 if (desktop->shell)
1221 output_init(output, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001222}
1223
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001224static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001225global_handler(struct display *display, uint32_t id,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001226 const char *interface, uint32_t version, void *data)
1227{
1228 struct desktop *desktop = data;
1229
1230 if (!strcmp(interface, "desktop_shell")) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03001231 desktop->interface_version = (version < 2) ? version : 2;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001232 desktop->shell = display_bind(desktop->display,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03001233 id, &desktop_shell_interface,
1234 desktop->interface_version);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001235 desktop_shell_add_listener(desktop->shell, &listener, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001236 } else if (!strcmp(interface, "wl_output")) {
1237 create_output(desktop, id);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001238 }
1239}
1240
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001241static void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001242global_handler_remove(struct display *display, uint32_t id,
1243 const char *interface, uint32_t version, void *data)
1244{
1245 struct desktop *desktop = data;
1246 struct output *output;
1247
1248 if (!strcmp(interface, "wl_output")) {
1249 wl_list_for_each(output, &desktop->outputs, link) {
1250 if (output->server_output_id == id) {
1251 output_destroy(output);
1252 break;
1253 }
1254 }
1255 }
1256}
1257
1258static void
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001259panel_add_launchers(struct panel *panel, struct desktop *desktop)
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001260{
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001261 struct weston_config_section *s;
1262 char *icon, *path;
1263 const char *name;
1264 int count;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001265
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001266 count = 0;
1267 s = NULL;
1268 while (weston_config_next_section(desktop->config, &s, &name)) {
1269 if (strcmp(name, "launcher") != 0)
1270 continue;
1271
1272 weston_config_section_get_string(s, "icon", &icon, NULL);
1273 weston_config_section_get_string(s, "path", &path, NULL);
1274
1275 if (icon != NULL && path != NULL) {
1276 panel_add_launcher(panel, icon, path);
Rob Bradford09252d42013-07-26 16:29:45 +01001277 count++;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001278 } else {
1279 fprintf(stderr, "invalid launcher section\n");
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001280 }
1281
1282 free(icon);
1283 free(path);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001284 }
1285
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001286 if (count == 0) {
1287 /* add default launcher */
1288 panel_add_launcher(panel,
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001289 DATADIR "/weston/terminal.png",
Rodney Lorrimar99ff01b2012-02-29 17:31:03 +01001290 BINDIR "/weston-terminal");
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001291 }
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001292}
1293
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001294int main(int argc, char *argv[])
1295{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001296 struct desktop desktop = { 0 };
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001297 struct output *output;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001298 struct weston_config_section *s;
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001299 const char *config_file;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001300
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001301 desktop.unlock_task.run = unlock_dialog_finish;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001302 wl_list_init(&desktop.outputs);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001303
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001304 config_file = weston_config_get_name_from_env();
1305 desktop.config = weston_config_parse(config_file);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001306 s = weston_config_get_section(desktop.config, "shell", NULL, NULL);
1307 weston_config_section_get_bool(s, "locking", &desktop.locking, 1);
1308
Kristian Høgsberg4172f662013-02-20 15:27:49 -05001309 desktop.display = display_create(&argc, argv);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001310 if (desktop.display == NULL) {
1311 fprintf(stderr, "failed to create display: %m\n");
1312 return -1;
1313 }
1314
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001315 display_set_user_data(desktop.display, &desktop);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001316 display_set_global_handler(desktop.display, global_handler);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001317 display_set_global_handler_remove(desktop.display, global_handler_remove);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001318
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001319 /* Create panel and background for outputs processed before the shell
1320 * global interface was processed */
1321 wl_list_for_each(output, &desktop.outputs, link)
1322 if (!output->panel)
1323 output_init(output, &desktop);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001324
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001325 grab_surface_create(&desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001326
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001327 signal(SIGCHLD, sigchild_handler);
1328
1329 display_run(desktop.display);
1330
U. Artie Eoff44874d92012-10-02 21:12:35 -07001331 /* Cleanup */
1332 grab_surface_destroy(&desktop);
1333 desktop_destroy_outputs(&desktop);
1334 if (desktop.unlock_dialog)
1335 unlock_dialog_destroy(desktop.unlock_dialog);
1336 desktop_shell_destroy(desktop.shell);
1337 display_destroy(desktop.display);
1338
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001339 return 0;
1340}