blob: 12bc971e1943d0480eff2476c1775e6ce65878d0 [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>
Quentin Glidicf9574f22016-06-23 18:55:21 +020043#include <assert.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040044
Pekka Paalanen50719bc2011-11-22 14:18:50 +020045#include <wayland-client.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040046#include "window.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070047#include "shared/cairo-util.h"
48#include "shared/config-parser.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070049#include "shared/helpers.h"
Bryce Harringtone99e4bf2016-03-16 14:15:18 -070050#include "shared/xalloc.h"
Bryce Harrington0d1a6222016-02-11 16:42:49 -080051#include "shared/zalloc.h"
Derek Foremane2772762018-02-06 15:18:38 -060052#include "shared/file-util.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040053
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080054#include "weston-desktop-shell-client-protocol.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040055
Armin Krezovićc6a55db2016-03-10 18:02:34 +010056#define DEFAULT_CLOCK_FORMAT CLOCK_FORMAT_MINUTES
57
Pekka Paalanenb6df4f72012-08-03 14:39:15 +030058extern char **environ; /* defined by libc */
59
Quentin Glidicf9574f22016-06-23 18:55:21 +020060enum clock_format {
61 CLOCK_FORMAT_MINUTES,
62 CLOCK_FORMAT_SECONDS,
63 CLOCK_FORMAT_NONE
64};
65
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040066struct desktop {
67 struct display *display;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080068 struct weston_desktop_shell *shell;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +020069 struct unlock_dialog *unlock_dialog;
70 struct task unlock_task;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010071 struct wl_list outputs;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040072
Quentin Glidic55d57012016-06-23 18:55:18 +020073 int want_panel;
74 enum weston_desktop_shell_panel_position panel_position;
Quentin Glidic3e37b342016-06-23 18:55:22 +020075 enum clock_format clock_format;
Quentin Glidic55d57012016-06-23 18:55:18 +020076
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030077 struct window *grab_window;
78 struct widget *grab_widget;
79
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +030080 struct weston_config *config;
81 int locking;
82
Scott Moreauec116022012-07-22 18:23:52 -060083 enum cursor_type grab_cursor;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030084
85 int painted;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010086};
87
88struct surface {
89 void (*configure)(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080090 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040091 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010092 int32_t width, int32_t height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040093};
94
95struct panel {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010096 struct surface base;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040097 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050098 struct widget *widget;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050099 struct wl_list launcher_list;
Martin Minarik1e51a872012-06-08 00:39:11 +0200100 struct panel_clock *clock;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300101 int painted;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200102 enum weston_desktop_shell_panel_position panel_position;
Quentin Glidicf9574f22016-06-23 18:55:21 +0200103 enum clock_format clock_format;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300104 uint32_t color;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400105};
106
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100107struct background {
108 struct surface base;
109 struct window *window;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500110 struct widget *widget;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300111 int painted;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300112
113 char *image;
114 int type;
115 uint32_t color;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100116};
117
118struct output {
119 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800120 uint32_t server_output_id;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100121 struct wl_list link;
122
123 struct panel *panel;
124 struct background *background;
125};
126
Kristian Høgsberg53880802012-01-09 11:16:50 -0500127struct panel_launcher {
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500128 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400129 struct panel *panel;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400130 cairo_surface_t *icon;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500131 int focused, pressed;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400132 char *path;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500133 struct wl_list link;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400134 struct wl_array envp;
135 struct wl_array argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400136};
137
Martin Minarik1e51a872012-06-08 00:39:11 +0200138struct panel_clock {
139 struct widget *widget;
140 struct panel *panel;
Martin Minarik1e51a872012-06-08 00:39:11 +0200141 struct task clock_task;
142 int clock_fd;
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100143 char *format_string;
144 time_t refresh_timer;
Martin Minarik1e51a872012-06-08 00:39:11 +0200145};
146
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200147struct unlock_dialog {
148 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500149 struct widget *widget;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500150 struct widget *button;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500151 int button_focused;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200152 int closing;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200153 struct desktop *desktop;
154};
155
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300156static void
157panel_add_launchers(struct panel *panel, struct desktop *desktop);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500158
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400159static void
160sigchild_handler(int s)
161{
162 int status;
163 pid_t pid;
164
165 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
166 fprintf(stderr, "child %d exited\n", pid);
167}
168
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300169static int
170is_desktop_painted(struct desktop *desktop)
171{
172 struct output *output;
173
174 wl_list_for_each(output, &desktop->outputs, link) {
175 if (output->panel && !output->panel->painted)
176 return 0;
177 if (output->background && !output->background->painted)
178 return 0;
179 }
180
181 return 1;
182}
183
184static void
185check_desktop_ready(struct window *window)
186{
187 struct display *display;
188 struct desktop *desktop;
189
190 display = window_get_display(window);
191 desktop = display_get_user_data(display);
192
193 if (!desktop->painted && is_desktop_painted(desktop)) {
194 desktop->painted = 1;
195
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800196 weston_desktop_shell_desktop_ready(desktop->shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300197 }
198}
199
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400200static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500201panel_launcher_activate(struct panel_launcher *widget)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400202{
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400203 char **argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400204 pid_t pid;
205
206 pid = fork();
207 if (pid < 0) {
208 fprintf(stderr, "fork failed: %m\n");
209 return;
210 }
211
212 if (pid)
213 return;
Benjamin Franzked7759712011-11-22 12:38:48 +0100214
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400215 argv = widget->argv.data;
Derek Foreman091c8012017-03-24 09:41:13 -0500216
217 if (setsid() == -1)
218 exit(EXIT_FAILURE);
219
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400220 if (execve(argv[0], argv, widget->envp.data) < 0) {
221 fprintf(stderr, "execl '%s' failed: %m\n", argv[0]);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400222 exit(1);
223 }
224}
225
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400226static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500227panel_launcher_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400228{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500229 struct panel_launcher *launcher = data;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500230 struct rectangle allocation;
231 cairo_t *cr;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400232
Alexander Larssonc584fa62013-05-22 14:41:32 +0200233 cr = widget_cairo_create(launcher->panel->widget);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500234
235 widget_get_allocation(widget, &allocation);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500236 if (launcher->pressed) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500237 allocation.x++;
238 allocation.y++;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400239 }
240
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500241 cairo_set_source_surface(cr, launcher->icon,
242 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400243 cairo_paint(cr);
244
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500245 if (launcher->focused) {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400246 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500247 cairo_mask_surface(cr, launcher->icon,
248 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400249 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400250
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500251 cairo_destroy(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400252}
253
Tiago Vignatti61500722012-05-23 22:06:28 +0300254static int
255panel_launcher_motion_handler(struct widget *widget, struct input *input,
256 uint32_t time, float x, float y, void *data)
257{
258 struct panel_launcher *launcher = data;
259
260 widget_set_tooltip(widget, basename((char *)launcher->path), x, y);
261
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300262 return CURSOR_LEFT_PTR;
Tiago Vignatti61500722012-05-23 22:06:28 +0300263}
264
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400265static void
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500266set_hex_color(cairo_t *cr, uint32_t color)
267{
Michael Vetter2a18a522015-05-15 17:17:47 +0200268 cairo_set_source_rgba(cr,
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500269 ((color >> 16) & 0xff) / 255.0,
270 ((color >> 8) & 0xff) / 255.0,
271 ((color >> 0) & 0xff) / 255.0,
272 ((color >> 24) & 0xff) / 255.0);
273}
274
275static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500276panel_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400277{
278 cairo_surface_t *surface;
279 cairo_t *cr;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500280 struct panel *panel = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400281
Alexander Larssonc584fa62013-05-22 14:41:32 +0200282 cr = widget_cairo_create(panel->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400283 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300284 set_hex_color(cr, panel->color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400285 cairo_paint(cr);
286
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400287 cairo_destroy(cr);
Alexander Larssonc584fa62013-05-22 14:41:32 +0200288 surface = window_get_surface(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400289 cairo_surface_destroy(surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300290 panel->painted = 1;
291 check_desktop_ready(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400292}
293
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500294static int
Kristian Høgsberg53880802012-01-09 11:16:50 -0500295panel_launcher_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400296 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500297{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500298 struct panel_launcher *launcher = data;
299
300 launcher->focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500301 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500302
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300303 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500304}
305
306static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500307panel_launcher_leave_handler(struct widget *widget,
308 struct input *input, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400309{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500310 struct panel_launcher *launcher = data;
311
312 launcher->focused = 0;
Tiago Vignatti61500722012-05-23 22:06:28 +0300313 widget_destroy_tooltip(widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500314 widget_schedule_redraw(widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400315}
316
317static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500318panel_launcher_button_handler(struct widget *widget,
319 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100320 uint32_t button,
321 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400322{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500323 struct panel_launcher *launcher;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400324
Kristian Høgsberg53880802012-01-09 11:16:50 -0500325 launcher = widget_get_user_data(widget);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500326 widget_schedule_redraw(widget);
Daniel Stone4dbadb12012-05-30 16:31:51 +0100327 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg53880802012-01-09 11:16:50 -0500328 panel_launcher_activate(launcher);
Rusty Lynch4384a242013-08-08 21:28:22 -0700329
330}
331
332static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700333panel_launcher_touch_down_handler(struct widget *widget, struct input *input,
334 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch4384a242013-08-08 21:28:22 -0700335 float x, float y, void *data)
336{
337 struct panel_launcher *launcher;
338
339 launcher = widget_get_user_data(widget);
340 launcher->focused = 1;
341 widget_schedule_redraw(widget);
342}
343
344static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700345panel_launcher_touch_up_handler(struct widget *widget, struct input *input,
Michael Vetter2a18a522015-05-15 17:17:47 +0200346 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch1084da52013-08-15 09:10:08 -0700347 void *data)
Rusty Lynch4384a242013-08-08 21:28:22 -0700348{
349 struct panel_launcher *launcher;
350
351 launcher = widget_get_user_data(widget);
352 launcher->focused = 0;
353 widget_schedule_redraw(widget);
354 panel_launcher_activate(launcher);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500355}
356
Martin Minarik1e51a872012-06-08 00:39:11 +0200357static void
358clock_func(struct task *task, uint32_t events)
359{
360 struct panel_clock *clock =
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400361 container_of(task, struct panel_clock, clock_task);
Kristian Høgsberg70226bb2012-06-08 16:54:52 -0400362 uint64_t exp;
Martin Minarik1e51a872012-06-08 00:39:11 +0200363
Martin Olsson8df662a2012-07-08 03:03:47 +0200364 if (read(clock->clock_fd, &exp, sizeof exp) != sizeof exp)
365 abort();
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400366 widget_schedule_redraw(clock->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200367}
368
369static void
370panel_clock_redraw_handler(struct widget *widget, void *data)
371{
Martin Minarik1e51a872012-06-08 00:39:11 +0200372 struct panel_clock *clock = data;
373 cairo_t *cr;
374 struct rectangle allocation;
375 cairo_text_extents_t extents;
376 cairo_font_extents_t font_extents;
Martin Minarik1e51a872012-06-08 00:39:11 +0200377 time_t rawtime;
378 struct tm * timeinfo;
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400379 char string[128];
Martin Minarik1e51a872012-06-08 00:39:11 +0200380
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400381 time(&rawtime);
382 timeinfo = localtime(&rawtime);
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100383 strftime(string, sizeof string, clock->format_string, timeinfo);
Martin Minarik1e51a872012-06-08 00:39:11 +0200384
385 widget_get_allocation(widget, &allocation);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400386 if (allocation.width == 0)
387 return;
Martin Minarik1e51a872012-06-08 00:39:11 +0200388
Alexander Larssonc584fa62013-05-22 14:41:32 +0200389 cr = widget_cairo_create(clock->panel->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200390 cairo_select_font_face(cr, "sans",
391 CAIRO_FONT_SLANT_NORMAL,
392 CAIRO_FONT_WEIGHT_NORMAL);
393 cairo_set_font_size(cr, 14);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400394 cairo_text_extents(cr, string, &extents);
Martin Minarik1e51a872012-06-08 00:39:11 +0200395 cairo_font_extents (cr, &font_extents);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400396 cairo_move_to(cr, allocation.x + 5,
397 allocation.y + 3 * (allocation.height >> 2) + 1);
398 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400399 cairo_show_text(cr, string);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400400 cairo_move_to(cr, allocation.x + 4,
401 allocation.y + 3 * (allocation.height >> 2));
402 cairo_set_source_rgb(cr, 1, 1, 1);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400403 cairo_show_text(cr, string);
Martin Minarik1e51a872012-06-08 00:39:11 +0200404 cairo_destroy(cr);
405}
406
407static int
408clock_timer_reset(struct panel_clock *clock)
409{
410 struct itimerspec its;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400411
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100412 its.it_interval.tv_sec = clock->refresh_timer;
Martin Minarik1e51a872012-06-08 00:39:11 +0200413 its.it_interval.tv_nsec = 0;
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100414 its.it_value.tv_sec = clock->refresh_timer;
Martin Minarik1e51a872012-06-08 00:39:11 +0200415 its.it_value.tv_nsec = 0;
416 if (timerfd_settime(clock->clock_fd, 0, &its, NULL) < 0) {
417 fprintf(stderr, "could not set timerfd\n: %m");
418 return -1;
419 }
420
421 return 0;
422}
423
424static void
U. Artie Eoff44874d92012-10-02 21:12:35 -0700425panel_destroy_clock(struct panel_clock *clock)
426{
427 widget_destroy(clock->widget);
428
429 close(clock->clock_fd);
430
431 free(clock);
432}
433
434static void
Martin Minarik1e51a872012-06-08 00:39:11 +0200435panel_add_clock(struct panel *panel)
436{
437 struct panel_clock *clock;
438 int timerfd;
439
440 timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
441 if (timerfd < 0) {
442 fprintf(stderr, "could not create timerfd\n: %m");
443 return;
444 }
445
Peter Huttererf3d62272013-08-08 11:57:05 +1000446 clock = xzalloc(sizeof *clock);
Martin Minarik1e51a872012-06-08 00:39:11 +0200447 clock->panel = panel;
448 panel->clock = clock;
449 clock->clock_fd = timerfd;
450
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100451 switch (panel->clock_format) {
452 case CLOCK_FORMAT_MINUTES:
453 clock->format_string = "%a %b %d, %I:%M %p";
454 clock->refresh_timer = 60;
455 break;
456 case CLOCK_FORMAT_SECONDS:
457 clock->format_string = "%a %b %d, %I:%M:%S %p";
458 clock->refresh_timer = 1;
459 break;
Quentin Glidicf9574f22016-06-23 18:55:21 +0200460 case CLOCK_FORMAT_NONE:
461 assert(!"not reached");
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100462 }
463
Martin Minarik1e51a872012-06-08 00:39:11 +0200464 clock->clock_task.run = clock_func;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400465 display_watch_fd(window_get_display(panel->window), clock->clock_fd,
466 EPOLLIN, &clock->clock_task);
Martin Minarik1e51a872012-06-08 00:39:11 +0200467 clock_timer_reset(clock);
468
469 clock->widget = widget_add_widget(panel->widget, clock);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400470 widget_set_redraw_handler(clock->widget, panel_clock_redraw_handler);
Martin Minarik1e51a872012-06-08 00:39:11 +0200471}
472
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500473static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500474panel_resize_handler(struct widget *widget,
475 int32_t width, int32_t height, void *data)
476{
477 struct panel_launcher *launcher;
478 struct panel *panel = data;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200479 int bx = width / 2;
Quentin Glidic51c2c372016-06-23 18:55:23 +0200480 int by = height / 2;
481 int spacing = 10;
482 int x = spacing;
483 int y = spacing;
484 int w, h;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200485 int horizontal = panel->panel_position == WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP || panel->panel_position == WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM;
Michael Vetter2a18a522015-05-15 17:17:47 +0200486
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500487 wl_list_for_each(launcher, &panel->launcher_list, link) {
488 w = cairo_image_surface_get_width(launcher->icon);
489 h = cairo_image_surface_get_height(launcher->icon);
Quentin Glidic51c2c372016-06-23 18:55:23 +0200490
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200491 if (horizontal)
Quentin Glidic51c2c372016-06-23 18:55:23 +0200492 y = by - h / 2;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200493 else
494 x = bx - w / 2;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500495 widget_set_allocation(launcher->widget,
Quentin Glidic51c2c372016-06-23 18:55:23 +0200496 x, y, w + 1, h + 1);
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200497 if (horizontal)
Quentin Glidic51c2c372016-06-23 18:55:23 +0200498 x += w + spacing;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200499 else
500 y += h + spacing;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500501 }
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100502
503 h = 20;
504
505 if (panel->clock_format == CLOCK_FORMAT_SECONDS)
506 w = 190;
507 else /* CLOCK_FORMAT_MINUTES */
508 w = 170;
Pekka Paalanen01b17252012-06-12 17:42:26 +0300509
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200510 if (horizontal) {
Quentin Glidic51c2c372016-06-23 18:55:23 +0200511 x = width - w - spacing;
512 y = by - h / 2;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200513 } else {
514 x = bx - w / 2;
515 y = height - h - spacing;
516 }
Quentin Glidic51c2c372016-06-23 18:55:23 +0200517
Pekka Paalanen01b17252012-06-12 17:42:26 +0300518 if (panel->clock)
519 widget_set_allocation(panel->clock->widget,
Quentin Glidic51c2c372016-06-23 18:55:23 +0200520 x, y, w + 1, h + 1);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500521}
522
523static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100524panel_configure(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800525 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400526 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100527 int32_t width, int32_t height)
528{
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200529 struct desktop *desktop = data;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500530 struct surface *surface = window_get_user_data(window);
531 struct panel *panel = container_of(surface, struct panel, base);
532
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200533 switch (desktop->panel_position) {
534 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
535 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Quentin Glidic51c2c372016-06-23 18:55:23 +0200536 height = 32;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200537 break;
538 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
539 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
540 switch (desktop->clock_format) {
541 case CLOCK_FORMAT_NONE:
542 width = 32;
543 break;
544 case CLOCK_FORMAT_MINUTES:
545 width = 170;
546 break;
547 case CLOCK_FORMAT_SECONDS:
548 width = 190;
549 break;
550 }
551 break;
552 }
Quentin Glidic51c2c372016-06-23 18:55:23 +0200553 window_schedule_resize(panel->window, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100554}
555
U. Artie Eoff44874d92012-10-02 21:12:35 -0700556static void
557panel_destroy_launcher(struct panel_launcher *launcher)
558{
559 wl_array_release(&launcher->argv);
560 wl_array_release(&launcher->envp);
561
562 free(launcher->path);
563
564 cairo_surface_destroy(launcher->icon);
565
566 widget_destroy(launcher->widget);
567 wl_list_remove(&launcher->link);
568
569 free(launcher);
570}
571
572static void
573panel_destroy(struct panel *panel)
574{
575 struct panel_launcher *tmp;
576 struct panel_launcher *launcher;
577
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100578 if (panel->clock)
579 panel_destroy_clock(panel->clock);
U. Artie Eoff44874d92012-10-02 21:12:35 -0700580
581 wl_list_for_each_safe(launcher, tmp, &panel->launcher_list, link)
582 panel_destroy_launcher(launcher);
583
584 widget_destroy(panel->widget);
585 window_destroy(panel->window);
586
587 free(panel);
588}
589
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400590static struct panel *
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300591panel_create(struct desktop *desktop)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400592{
593 struct panel *panel;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300594 struct weston_config_section *s;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400595
Peter Huttererf3d62272013-08-08 11:57:05 +1000596 panel = xzalloc(sizeof *panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400597
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100598 panel->base.configure = panel_configure;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300599 panel->window = window_create_custom(desktop->display);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500600 panel->widget = window_add_widget(panel->window, panel);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500601 wl_list_init(&panel->launcher_list);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400602
603 window_set_title(panel->window, "panel");
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400604 window_set_user_data(panel->window, panel);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500605
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500606 widget_set_redraw_handler(panel->widget, panel_redraw_handler);
607 widget_set_resize_handler(panel->widget, panel_resize_handler);
Michael Vetter2a18a522015-05-15 17:17:47 +0200608
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200609 panel->panel_position = desktop->panel_position;
Quentin Glidic3e37b342016-06-23 18:55:22 +0200610 panel->clock_format = desktop->clock_format;
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100611 if (panel->clock_format != CLOCK_FORMAT_NONE)
612 panel_add_clock(panel);
613
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300614 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
Bryce Harringtone776f2a2016-07-14 18:28:03 -0700615 weston_config_section_get_color(s, "panel-color",
616 &panel->color, 0xaa000000);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300617
618 panel_add_launchers(panel, desktop);
619
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400620 return panel;
621}
622
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200623static cairo_surface_t *
624load_icon_or_fallback(const char *icon)
625{
626 cairo_surface_t *surface = cairo_image_surface_create_from_png(icon);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200627 cairo_status_t status;
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200628 cairo_t *cr;
629
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200630 status = cairo_surface_status(surface);
631 if (status == CAIRO_STATUS_SUCCESS)
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200632 return surface;
633
634 cairo_surface_destroy(surface);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200635 fprintf(stderr, "ERROR loading icon from file '%s', error: '%s'\n",
636 icon, cairo_status_to_string(status));
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200637
638 /* draw fallback icon */
639 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
640 20, 20);
641 cr = cairo_create(surface);
642
643 cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1);
644 cairo_paint(cr);
645
646 cairo_set_source_rgba(cr, 0, 0, 0, 1);
647 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
648 cairo_rectangle(cr, 0, 0, 20, 20);
649 cairo_move_to(cr, 4, 4);
650 cairo_line_to(cr, 16, 16);
651 cairo_move_to(cr, 4, 16);
652 cairo_line_to(cr, 16, 4);
653 cairo_stroke(cr);
654
655 cairo_destroy(cr);
656
657 return surface;
658}
659
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400660static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500661panel_add_launcher(struct panel *panel, const char *icon, const char *path)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400662{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500663 struct panel_launcher *launcher;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400664 char *start, *p, *eq, **ps;
665 int i, j, k;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400666
Peter Huttererf3d62272013-08-08 11:57:05 +1000667 launcher = xzalloc(sizeof *launcher);
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200668 launcher->icon = load_icon_or_fallback(icon);
U. Artie Eoff3c946772014-01-15 10:59:50 -0800669 launcher->path = xstrdup(path);
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400670
671 wl_array_init(&launcher->envp);
672 wl_array_init(&launcher->argv);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300673 for (i = 0; environ[i]; i++) {
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400674 ps = wl_array_add(&launcher->envp, sizeof *ps);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300675 *ps = environ[i];
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400676 }
677 j = 0;
678
679 start = launcher->path;
680 while (*start) {
681 for (p = start, eq = NULL; *p && !isspace(*p); p++)
682 if (*p == '=')
683 eq = p;
684
685 if (eq && j == 0) {
686 ps = launcher->envp.data;
687 for (k = 0; k < i; k++)
688 if (strncmp(ps[k], start, eq - start) == 0) {
689 ps[k] = start;
690 break;
691 }
692 if (k == i) {
693 ps = wl_array_add(&launcher->envp, sizeof *ps);
694 *ps = start;
695 i++;
696 }
697 } else {
698 ps = wl_array_add(&launcher->argv, sizeof *ps);
699 *ps = start;
700 j++;
701 }
702
703 while (*p && isspace(*p))
704 *p++ = '\0';
705
706 start = p;
707 }
708
709 ps = wl_array_add(&launcher->envp, sizeof *ps);
710 *ps = NULL;
711 ps = wl_array_add(&launcher->argv, sizeof *ps);
712 *ps = NULL;
713
Kristian Høgsberg53880802012-01-09 11:16:50 -0500714 launcher->panel = panel;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500715 wl_list_insert(panel->launcher_list.prev, &launcher->link);
716
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500717 launcher->widget = widget_add_widget(panel->widget, launcher);
Kristian Høgsberg53880802012-01-09 11:16:50 -0500718 widget_set_enter_handler(launcher->widget,
719 panel_launcher_enter_handler);
720 widget_set_leave_handler(launcher->widget,
721 panel_launcher_leave_handler);
722 widget_set_button_handler(launcher->widget,
723 panel_launcher_button_handler);
Rusty Lynch4384a242013-08-08 21:28:22 -0700724 widget_set_touch_down_handler(launcher->widget,
725 panel_launcher_touch_down_handler);
726 widget_set_touch_up_handler(launcher->widget,
727 panel_launcher_touch_up_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500728 widget_set_redraw_handler(launcher->widget,
729 panel_launcher_redraw_handler);
Tiago Vignatti61500722012-05-23 22:06:28 +0300730 widget_set_motion_handler(launcher->widget,
731 panel_launcher_motion_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400732}
733
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500734enum {
735 BACKGROUND_SCALE,
Pekka Paalanena402b052013-05-22 18:03:10 +0300736 BACKGROUND_SCALE_CROP,
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500737 BACKGROUND_TILE
738};
739
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400740static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500741background_draw(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400742{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500743 struct background *background = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400744 cairo_surface_t *surface, *image;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400745 cairo_pattern_t *pattern;
746 cairo_matrix_t matrix;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400747 cairo_t *cr;
Pekka Paalanena402b052013-05-22 18:03:10 +0300748 double im_w, im_h;
749 double sx, sy, s;
750 double tx, ty;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500751 struct rectangle allocation;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400752
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500753 surface = window_get_surface(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400754
Alexander Larssonc584fa62013-05-22 14:41:32 +0200755 cr = widget_cairo_create(background->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400756 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
757 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
758 cairo_paint(cr);
759
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500760 widget_get_allocation(widget, &allocation);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500761 image = NULL;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300762 if (background->image)
763 image = load_cairo_surface(background->image);
Derek Foremane2772762018-02-06 15:18:38 -0600764 else if (background->color == 0) {
765 char *name = file_name_with_datadir("pattern.png");
766
767 image = load_cairo_surface(name);
768 free(name);
769 }
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500770
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300771 if (image && background->type != -1) {
Pekka Paalanena402b052013-05-22 18:03:10 +0300772 im_w = cairo_image_surface_get_width(image);
773 im_h = cairo_image_surface_get_height(image);
774 sx = im_w / allocation.width;
775 sy = im_h / allocation.height;
776
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400777 pattern = cairo_pattern_create_for_surface(image);
Pekka Paalanena402b052013-05-22 18:03:10 +0300778
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300779 switch (background->type) {
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500780 case BACKGROUND_SCALE:
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500781 cairo_matrix_init_scale(&matrix, sx, sy);
782 cairo_pattern_set_matrix(pattern, &matrix);
Bill Spitzak79b7cb32014-05-08 20:00:35 -0700783 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500784 break;
Pekka Paalanena402b052013-05-22 18:03:10 +0300785 case BACKGROUND_SCALE_CROP:
786 s = (sx < sy) ? sx : sy;
787 /* align center */
788 tx = (im_w - s * allocation.width) * 0.5;
789 ty = (im_h - s * allocation.height) * 0.5;
790 cairo_matrix_init_translate(&matrix, tx, ty);
791 cairo_matrix_scale(&matrix, s, s);
792 cairo_pattern_set_matrix(pattern, &matrix);
Bill Spitzak79b7cb32014-05-08 20:00:35 -0700793 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
Pekka Paalanena402b052013-05-22 18:03:10 +0300794 break;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500795 case BACKGROUND_TILE:
796 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
797 break;
798 }
Pekka Paalanena402b052013-05-22 18:03:10 +0300799
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400800 cairo_set_source(cr, pattern);
801 cairo_pattern_destroy (pattern);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400802 cairo_surface_destroy(image);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500803 } else {
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300804 set_hex_color(cr, background->color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400805 }
806
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500807 cairo_paint(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400808 cairo_destroy(cr);
809 cairo_surface_destroy(surface);
Pekka Paalanen9564c752012-10-24 09:43:08 +0300810
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300811 background->painted = 1;
812 check_desktop_ready(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400813}
814
815static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100816background_configure(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800817 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400818 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100819 int32_t width, int32_t height)
820{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500821 struct background *background =
822 (struct background *) window_get_user_data(window);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100823
Kristian Høgsberg4598f962014-01-01 16:32:09 -0800824 widget_schedule_resize(background->widget, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100825}
826
827static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500828unlock_dialog_redraw_handler(struct widget *widget, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200829{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500830 struct unlock_dialog *dialog = data;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200831 struct rectangle allocation;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200832 cairo_surface_t *surface;
Alexander Larssonc584fa62013-05-22 14:41:32 +0200833 cairo_t *cr;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200834 cairo_pattern_t *pat;
835 double cx, cy, r, f;
836
Alexander Larssonc584fa62013-05-22 14:41:32 +0200837 cr = widget_cairo_create(widget);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500838
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500839 widget_get_allocation(dialog->widget, &allocation);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200840 cairo_rectangle(cr, allocation.x, allocation.y,
841 allocation.width, allocation.height);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200842 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
843 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500844 cairo_fill(cr);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200845
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500846 cairo_translate(cr, allocation.x, allocation.y);
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500847 if (dialog->button_focused)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200848 f = 1.0;
849 else
850 f = 0.7;
851
852 cx = allocation.width / 2.0;
853 cy = allocation.height / 2.0;
854 r = (cx < cy ? cx : cy) * 0.4;
855 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
856 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
857 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
858 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
859 cairo_set_source(cr, pat);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500860 cairo_pattern_destroy(pat);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200861 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
862 cairo_fill(cr);
863
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500864 widget_set_allocation(dialog->button,
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500865 allocation.x + cx - r,
866 allocation.y + cy - r, 2 * r, 2 * r);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200867
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200868 cairo_destroy(cr);
869
Alexander Larssonc584fa62013-05-22 14:41:32 +0200870 surface = window_get_surface(dialog->window);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200871 cairo_surface_destroy(surface);
872}
873
874static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500875unlock_dialog_button_handler(struct widget *widget,
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200876 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100877 uint32_t button,
878 enum wl_pointer_button_state state, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200879{
880 struct unlock_dialog *dialog = data;
881 struct desktop *desktop = dialog->desktop;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200882
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500883 if (button == BTN_LEFT) {
Daniel Stone4dbadb12012-05-30 16:31:51 +0100884 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
885 !dialog->closing) {
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200886 display_defer(desktop->display, &desktop->unlock_task);
887 dialog->closing = 1;
888 }
889 }
890}
891
892static void
Brian J Lovinc4df4082013-08-26 15:58:22 -0700893unlock_dialog_touch_down_handler(struct widget *widget, struct input *input,
894 uint32_t serial, uint32_t time, int32_t id,
895 float x, float y, void *data)
896{
897 struct unlock_dialog *dialog = data;
898
899 dialog->button_focused = 1;
900 widget_schedule_redraw(widget);
901}
902
903static void
904unlock_dialog_touch_up_handler(struct widget *widget, struct input *input,
905 uint32_t serial, uint32_t time, int32_t id,
906 void *data)
907{
908 struct unlock_dialog *dialog = data;
909 struct desktop *desktop = dialog->desktop;
910
911 dialog->button_focused = 0;
912 widget_schedule_redraw(widget);
913 display_defer(desktop->display, &desktop->unlock_task);
914 dialog->closing = 1;
915}
916
917static void
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200918unlock_dialog_keyboard_focus_handler(struct window *window,
919 struct input *device, void *data)
920{
921 window_schedule_redraw(window);
922}
923
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500924static int
Kristian Høgsbergee143232012-01-09 08:42:24 -0500925unlock_dialog_widget_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400926 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400927 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500928{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500929 struct unlock_dialog *dialog = data;
930
931 dialog->button_focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500932 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500933
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300934 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500935}
936
937static void
938unlock_dialog_widget_leave_handler(struct widget *widget,
939 struct input *input, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200940{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500941 struct unlock_dialog *dialog = data;
942
943 dialog->button_focused = 0;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500944 widget_schedule_redraw(widget);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200945}
946
947static struct unlock_dialog *
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500948unlock_dialog_create(struct desktop *desktop)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200949{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500950 struct display *display = desktop->display;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200951 struct unlock_dialog *dialog;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800952 struct wl_surface *surface;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200953
Peter Huttererf3d62272013-08-08 11:57:05 +1000954 dialog = xzalloc(sizeof *dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200955
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400956 dialog->window = window_create_custom(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500957 dialog->widget = window_frame_create(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200958 window_set_title(dialog->window, "Unlock your desktop");
959
960 window_set_user_data(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200961 window_set_keyboard_focus_handler(dialog->window,
962 unlock_dialog_keyboard_focus_handler);
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500963 dialog->button = widget_add_widget(dialog->widget, dialog);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500964 widget_set_redraw_handler(dialog->widget,
965 unlock_dialog_redraw_handler);
Kristian Høgsbergee143232012-01-09 08:42:24 -0500966 widget_set_enter_handler(dialog->button,
967 unlock_dialog_widget_enter_handler);
968 widget_set_leave_handler(dialog->button,
969 unlock_dialog_widget_leave_handler);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500970 widget_set_button_handler(dialog->button,
971 unlock_dialog_button_handler);
Brian J Lovinc4df4082013-08-26 15:58:22 -0700972 widget_set_touch_down_handler(dialog->button,
973 unlock_dialog_touch_down_handler);
974 widget_set_touch_up_handler(dialog->button,
975 unlock_dialog_touch_up_handler);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200976
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800977 surface = window_get_wl_surface(dialog->window);
978 weston_desktop_shell_set_lock_surface(desktop->shell, surface);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500979
Pekka Paalanen40e49ac2012-01-18 16:51:30 +0200980 window_schedule_resize(dialog->window, 260, 230);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200981
982 return dialog;
983}
984
985static void
986unlock_dialog_destroy(struct unlock_dialog *dialog)
987{
988 window_destroy(dialog->window);
989 free(dialog);
990}
991
992static void
993unlock_dialog_finish(struct task *task, uint32_t events)
994{
995 struct desktop *desktop =
Benjamin Franzked7759712011-11-22 12:38:48 +0100996 container_of(task, struct desktop, unlock_task);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200997
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800998 weston_desktop_shell_unlock(desktop->shell);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200999 unlock_dialog_destroy(desktop->unlock_dialog);
1000 desktop->unlock_dialog = NULL;
1001}
1002
1003static void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001004desktop_shell_configure(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001005 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001006 uint32_t edges,
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001007 struct wl_surface *surface,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001008 int32_t width, int32_t height)
1009{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001010 struct window *window = wl_surface_get_user_data(surface);
Pekka Paalanen068ae942011-11-28 14:11:15 +02001011 struct surface *s = window_get_user_data(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001012
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001013 s->configure(data, desktop_shell, edges, window, width, height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001014}
1015
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001016static void
1017desktop_shell_prepare_lock_surface(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001018 struct weston_desktop_shell *desktop_shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001019{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001020 struct desktop *desktop = data;
1021
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001022 if (!desktop->locking) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001023 weston_desktop_shell_unlock(desktop->shell);
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +02001024 return;
1025 }
1026
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001027 if (!desktop->unlock_dialog) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001028 desktop->unlock_dialog = unlock_dialog_create(desktop);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001029 desktop->unlock_dialog->desktop = desktop;
1030 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001031}
1032
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001033static void
1034desktop_shell_grab_cursor(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001035 struct weston_desktop_shell *desktop_shell,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001036 uint32_t cursor)
1037{
1038 struct desktop *desktop = data;
1039
1040 switch (cursor) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001041 case WESTON_DESKTOP_SHELL_CURSOR_NONE:
Philipp Brüschweiler16d59d72012-08-24 15:43:55 +02001042 desktop->grab_cursor = CURSOR_BLANK;
1043 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001044 case WESTON_DESKTOP_SHELL_CURSOR_BUSY:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001045 desktop->grab_cursor = CURSOR_WATCH;
1046 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001047 case WESTON_DESKTOP_SHELL_CURSOR_MOVE:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001048 desktop->grab_cursor = CURSOR_DRAGGING;
1049 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001050 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001051 desktop->grab_cursor = CURSOR_TOP;
1052 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001053 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001054 desktop->grab_cursor = CURSOR_BOTTOM;
1055 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001056 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_LEFT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001057 desktop->grab_cursor = CURSOR_LEFT;
1058 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001059 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_RIGHT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001060 desktop->grab_cursor = CURSOR_RIGHT;
1061 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001062 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP_LEFT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001063 desktop->grab_cursor = CURSOR_TOP_LEFT;
1064 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001065 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP_RIGHT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001066 desktop->grab_cursor = CURSOR_TOP_RIGHT;
1067 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001068 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_LEFT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001069 desktop->grab_cursor = CURSOR_BOTTOM_LEFT;
1070 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001071 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_RIGHT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001072 desktop->grab_cursor = CURSOR_BOTTOM_RIGHT;
1073 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001074 case WESTON_DESKTOP_SHELL_CURSOR_ARROW:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001075 default:
1076 desktop->grab_cursor = CURSOR_LEFT_PTR;
1077 }
1078}
1079
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001080static const struct weston_desktop_shell_listener listener = {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001081 desktop_shell_configure,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001082 desktop_shell_prepare_lock_surface,
1083 desktop_shell_grab_cursor
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001084};
1085
U. Artie Eoff44874d92012-10-02 21:12:35 -07001086static void
1087background_destroy(struct background *background)
1088{
1089 widget_destroy(background->widget);
1090 window_destroy(background->window);
1091
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001092 free(background->image);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001093 free(background);
1094}
1095
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001096static struct background *
1097background_create(struct desktop *desktop)
1098{
1099 struct background *background;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001100 struct weston_config_section *s;
1101 char *type;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001102
Peter Huttererf3d62272013-08-08 11:57:05 +10001103 background = xzalloc(sizeof *background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001104 background->base.configure = background_configure;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001105 background->window = window_create_custom(desktop->display);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001106 background->widget = window_add_widget(background->window, background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001107 window_set_user_data(background->window, background);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001108 widget_set_redraw_handler(background->widget, background_draw);
Arnaud Vracfb754a02014-08-25 20:56:49 +02001109 widget_set_transparent(background->widget, 0);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001110
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001111 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
1112 weston_config_section_get_string(s, "background-image",
Kristian Høgsberg4c3661f2014-04-21 22:54:37 -07001113 &background->image, NULL);
Bryce Harringtone776f2a2016-07-14 18:28:03 -07001114 weston_config_section_get_color(s, "background-color",
1115 &background->color, 0x00000000);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001116
1117 weston_config_section_get_string(s, "background-type",
1118 &type, "tile");
U. Artie Eoff3c946772014-01-15 10:59:50 -08001119 if (type == NULL) {
1120 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
1121 exit(EXIT_FAILURE);
1122 }
1123
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001124 if (strcmp(type, "scale") == 0) {
1125 background->type = BACKGROUND_SCALE;
1126 } else if (strcmp(type, "scale-crop") == 0) {
1127 background->type = BACKGROUND_SCALE_CROP;
1128 } else if (strcmp(type, "tile") == 0) {
1129 background->type = BACKGROUND_TILE;
1130 } else {
1131 background->type = -1;
1132 fprintf(stderr, "invalid background-type: %s\n",
1133 type);
1134 }
1135
1136 free(type);
1137
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001138 return background;
1139}
1140
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001141static int
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001142grab_surface_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001143 float x, float y, void *data)
1144{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001145 struct desktop *desktop = data;
1146
1147 return desktop->grab_cursor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001148}
1149
1150static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001151grab_surface_destroy(struct desktop *desktop)
1152{
1153 widget_destroy(desktop->grab_widget);
1154 window_destroy(desktop->grab_window);
1155}
1156
1157static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001158grab_surface_create(struct desktop *desktop)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001159{
1160 struct wl_surface *s;
1161
Ander Conselvan de Oliveira07a91cd2012-07-16 14:15:50 +03001162 desktop->grab_window = window_create_custom(desktop->display);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001163 window_set_user_data(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001164
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001165 s = window_get_wl_surface(desktop->grab_window);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001166 weston_desktop_shell_set_grab_surface(desktop->shell, s);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001167
1168 desktop->grab_widget =
1169 window_add_widget(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001170 /* We set the allocation to 1x1 at 0,0 so the fake enter event
1171 * at 0,0 will go to this widget. */
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001172 widget_set_allocation(desktop->grab_widget, 0, 0, 1, 1);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001173
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001174 widget_set_enter_handler(desktop->grab_widget,
1175 grab_surface_enter_handler);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001176}
1177
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001178static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001179output_destroy(struct output *output)
1180{
1181 background_destroy(output->background);
Jonny Lambe67118c2014-08-12 15:07:51 +02001182 if (output->panel)
1183 panel_destroy(output->panel);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001184 wl_output_destroy(output->output);
1185 wl_list_remove(&output->link);
1186
1187 free(output);
1188}
1189
1190static void
1191desktop_destroy_outputs(struct desktop *desktop)
1192{
1193 struct output *tmp;
1194 struct output *output;
1195
1196 wl_list_for_each_safe(output, tmp, &desktop->outputs, link)
1197 output_destroy(output);
1198}
1199
1200static void
Alexander Larssonc584fa62013-05-22 14:41:32 +02001201output_handle_geometry(void *data,
1202 struct wl_output *wl_output,
1203 int x, int y,
1204 int physical_width,
1205 int physical_height,
1206 int subpixel,
1207 const char *make,
1208 const char *model,
1209 int transform)
1210{
1211 struct output *output = data;
1212
Jonny Lambe67118c2014-08-12 15:07:51 +02001213 if (output->panel)
1214 window_set_buffer_transform(output->panel->window, transform);
Alexander Larssonc584fa62013-05-22 14:41:32 +02001215 window_set_buffer_transform(output->background->window, transform);
1216}
1217
1218static void
1219output_handle_mode(void *data,
1220 struct wl_output *wl_output,
1221 uint32_t flags,
1222 int width,
1223 int height,
1224 int refresh)
1225{
1226}
1227
1228static void
1229output_handle_done(void *data,
1230 struct wl_output *wl_output)
1231{
1232}
1233
1234static void
1235output_handle_scale(void *data,
1236 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001237 int32_t scale)
Alexander Larssonc584fa62013-05-22 14:41:32 +02001238{
1239 struct output *output = data;
1240
Jonny Lambe67118c2014-08-12 15:07:51 +02001241 if (output->panel)
1242 window_set_buffer_scale(output->panel->window, scale);
Alexander Larssonc584fa62013-05-22 14:41:32 +02001243 window_set_buffer_scale(output->background->window, scale);
1244}
1245
1246static const struct wl_output_listener output_listener = {
1247 output_handle_geometry,
1248 output_handle_mode,
1249 output_handle_done,
1250 output_handle_scale
1251};
1252
1253static void
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001254output_init(struct output *output, struct desktop *desktop)
1255{
1256 struct wl_surface *surface;
1257
Quentin Glidic55d57012016-06-23 18:55:18 +02001258 if (desktop->want_panel) {
Jonny Lambe67118c2014-08-12 15:07:51 +02001259 output->panel = panel_create(desktop);
1260 surface = window_get_wl_surface(output->panel->window);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001261 weston_desktop_shell_set_panel(desktop->shell,
1262 output->output, surface);
Jonny Lambe67118c2014-08-12 15:07:51 +02001263 }
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001264
1265 output->background = background_create(desktop);
1266 surface = window_get_wl_surface(output->background->window);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001267 weston_desktop_shell_set_background(desktop->shell,
1268 output->output, surface);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001269}
1270
1271static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001272create_output(struct desktop *desktop, uint32_t id)
1273{
1274 struct output *output;
1275
Bryce Harrington0d1a6222016-02-11 16:42:49 -08001276 output = zalloc(sizeof *output);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001277 if (!output)
1278 return;
1279
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001280 output->output =
Alexander Larssonc584fa62013-05-22 14:41:32 +02001281 display_bind(desktop->display, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001282 output->server_output_id = id;
Alexander Larssonc584fa62013-05-22 14:41:32 +02001283
1284 wl_output_add_listener(output->output, &output_listener, output);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001285
1286 wl_list_insert(&desktop->outputs, &output->link);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001287
1288 /* On start up we may process an output global before the shell global
1289 * in which case we can't create the panel and background just yet */
1290 if (desktop->shell)
1291 output_init(output, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001292}
1293
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001294static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001295global_handler(struct display *display, uint32_t id,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001296 const char *interface, uint32_t version, void *data)
1297{
1298 struct desktop *desktop = data;
1299
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001300 if (!strcmp(interface, "weston_desktop_shell")) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001301 desktop->shell = display_bind(desktop->display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001302 id,
1303 &weston_desktop_shell_interface,
1304 1);
1305 weston_desktop_shell_add_listener(desktop->shell,
1306 &listener,
1307 desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001308 } else if (!strcmp(interface, "wl_output")) {
1309 create_output(desktop, id);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001310 }
1311}
1312
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001313static void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001314global_handler_remove(struct display *display, uint32_t id,
1315 const char *interface, uint32_t version, void *data)
1316{
1317 struct desktop *desktop = data;
1318 struct output *output;
1319
1320 if (!strcmp(interface, "wl_output")) {
1321 wl_list_for_each(output, &desktop->outputs, link) {
1322 if (output->server_output_id == id) {
1323 output_destroy(output);
1324 break;
1325 }
1326 }
1327 }
1328}
1329
1330static void
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001331panel_add_launchers(struct panel *panel, struct desktop *desktop)
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001332{
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001333 struct weston_config_section *s;
1334 char *icon, *path;
1335 const char *name;
1336 int count;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001337
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001338 count = 0;
1339 s = NULL;
1340 while (weston_config_next_section(desktop->config, &s, &name)) {
1341 if (strcmp(name, "launcher") != 0)
1342 continue;
1343
1344 weston_config_section_get_string(s, "icon", &icon, NULL);
1345 weston_config_section_get_string(s, "path", &path, NULL);
1346
1347 if (icon != NULL && path != NULL) {
1348 panel_add_launcher(panel, icon, path);
Rob Bradford09252d42013-07-26 16:29:45 +01001349 count++;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001350 } else {
1351 fprintf(stderr, "invalid launcher section\n");
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001352 }
1353
1354 free(icon);
1355 free(path);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001356 }
1357
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001358 if (count == 0) {
Derek Foremane2772762018-02-06 15:18:38 -06001359 char *name = file_name_with_datadir("terminal.png");
1360
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001361 /* add default launcher */
1362 panel_add_launcher(panel,
Derek Foremane2772762018-02-06 15:18:38 -06001363 name,
Rodney Lorrimar99ff01b2012-02-29 17:31:03 +01001364 BINDIR "/weston-terminal");
Derek Foremane2772762018-02-06 15:18:38 -06001365 free(name);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001366 }
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001367}
1368
Quentin Glidic55d57012016-06-23 18:55:18 +02001369static void
1370parse_panel_position(struct desktop *desktop, struct weston_config_section *s)
1371{
1372 char *position;
1373
Daniel Stone08cf24b2017-01-16 13:18:21 +00001374 desktop->want_panel = 1;
Quentin Glidic55d57012016-06-23 18:55:18 +02001375
Daniel Stone08cf24b2017-01-16 13:18:21 +00001376 weston_config_section_get_string(s, "panel-position", &position, "top");
1377 if (strcmp(position, "top") == 0) {
1378 desktop->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
1379 } else if (strcmp(position, "bottom") == 0) {
1380 desktop->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM;
1381 } else if (strcmp(position, "left") == 0) {
1382 desktop->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT;
1383 } else if (strcmp(position, "right") == 0) {
1384 desktop->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT;
1385 } else {
1386 /* 'none' is valid here */
1387 if (strcmp(position, "none") != 0)
1388 fprintf(stderr, "Wrong panel position: %s\n", position);
1389 desktop->want_panel = 0;
Quentin Glidic55d57012016-06-23 18:55:18 +02001390 }
Daniel Stone08cf24b2017-01-16 13:18:21 +00001391 free(position);
Quentin Glidic55d57012016-06-23 18:55:18 +02001392}
1393
Quentin Glidic3e37b342016-06-23 18:55:22 +02001394static void
1395parse_clock_format(struct desktop *desktop, struct weston_config_section *s)
1396{
1397 char *clock_format;
1398
1399 weston_config_section_get_string(s, "clock-format", &clock_format, "");
1400 if (strcmp(clock_format, "minutes") == 0)
1401 desktop->clock_format = CLOCK_FORMAT_MINUTES;
1402 else if (strcmp(clock_format, "seconds") == 0)
1403 desktop->clock_format = CLOCK_FORMAT_SECONDS;
1404 else if (strcmp(clock_format, "none") == 0)
1405 desktop->clock_format = CLOCK_FORMAT_NONE;
1406 else
1407 desktop->clock_format = DEFAULT_CLOCK_FORMAT;
1408 free(clock_format);
1409}
1410
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001411int main(int argc, char *argv[])
1412{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001413 struct desktop desktop = { 0 };
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001414 struct output *output;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001415 struct weston_config_section *s;
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001416 const char *config_file;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001417
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001418 desktop.unlock_task.run = unlock_dialog_finish;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001419 wl_list_init(&desktop.outputs);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001420
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001421 config_file = weston_config_get_name_from_env();
1422 desktop.config = weston_config_parse(config_file);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001423 s = weston_config_get_section(desktop.config, "shell", NULL, NULL);
1424 weston_config_section_get_bool(s, "locking", &desktop.locking, 1);
Quentin Glidic55d57012016-06-23 18:55:18 +02001425 parse_panel_position(&desktop, s);
Quentin Glidic3e37b342016-06-23 18:55:22 +02001426 parse_clock_format(&desktop, s);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001427
Kristian Høgsberg4172f662013-02-20 15:27:49 -05001428 desktop.display = display_create(&argc, argv);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001429 if (desktop.display == NULL) {
1430 fprintf(stderr, "failed to create display: %m\n");
1431 return -1;
1432 }
1433
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001434 display_set_user_data(desktop.display, &desktop);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001435 display_set_global_handler(desktop.display, global_handler);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001436 display_set_global_handler_remove(desktop.display, global_handler_remove);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001437
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001438 /* Create panel and background for outputs processed before the shell
1439 * global interface was processed */
Quentin Glidic55d57012016-06-23 18:55:18 +02001440 if (desktop.want_panel)
1441 weston_desktop_shell_set_panel_position(desktop.shell, desktop.panel_position);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001442 wl_list_for_each(output, &desktop.outputs, link)
1443 if (!output->panel)
1444 output_init(output, &desktop);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001445
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001446 grab_surface_create(&desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001447
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001448 signal(SIGCHLD, sigchild_handler);
1449
1450 display_run(desktop.display);
1451
U. Artie Eoff44874d92012-10-02 21:12:35 -07001452 /* Cleanup */
1453 grab_surface_destroy(&desktop);
1454 desktop_destroy_outputs(&desktop);
1455 if (desktop.unlock_dialog)
1456 unlock_dialog_destroy(desktop.unlock_dialog);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001457 weston_desktop_shell_destroy(desktop.shell);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001458 display_destroy(desktop.display);
1459
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001460 return 0;
1461}