blob: 0dc0f0ba3137e3e47c869bbbf6b5e9478b072db1 [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>
37#include <linux/input.h>
Tiago Vignatti61500722012-05-23 22:06:28 +030038#include <libgen.h>
Kristian Høgsbergd1936b92012-07-23 22:59:33 -040039#include <ctype.h>
Martin Minarik1e51a872012-06-08 00:39:11 +020040#include <time.h>
Quentin Glidicf9574f22016-06-23 18:55:21 +020041#include <assert.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040042
Pekka Paalanen50719bc2011-11-22 14:18:50 +020043#include <wayland-client.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040044#include "window.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070045#include "shared/cairo-util.h"
46#include "shared/config-parser.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070047#include "shared/helpers.h"
Bryce Harringtone99e4bf2016-03-16 14:15:18 -070048#include "shared/xalloc.h"
Bryce Harrington0d1a6222016-02-11 16:42:49 -080049#include "shared/zalloc.h"
Derek Foremane2772762018-02-06 15:18:38 -060050#include "shared/file-util.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040051
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080052#include "weston-desktop-shell-client-protocol.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040053
Armin Krezovićc6a55db2016-03-10 18:02:34 +010054#define DEFAULT_CLOCK_FORMAT CLOCK_FORMAT_MINUTES
55
Pekka Paalanenb6df4f72012-08-03 14:39:15 +030056extern char **environ; /* defined by libc */
57
Quentin Glidicf9574f22016-06-23 18:55:21 +020058enum clock_format {
59 CLOCK_FORMAT_MINUTES,
60 CLOCK_FORMAT_SECONDS,
61 CLOCK_FORMAT_NONE
62};
63
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040064struct desktop {
65 struct display *display;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080066 struct weston_desktop_shell *shell;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +020067 struct unlock_dialog *unlock_dialog;
68 struct task unlock_task;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010069 struct wl_list outputs;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040070
Quentin Glidic55d57012016-06-23 18:55:18 +020071 int want_panel;
72 enum weston_desktop_shell_panel_position panel_position;
Quentin Glidic3e37b342016-06-23 18:55:22 +020073 enum clock_format clock_format;
Quentin Glidic55d57012016-06-23 18:55:18 +020074
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030075 struct window *grab_window;
76 struct widget *grab_widget;
77
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +030078 struct weston_config *config;
79 int locking;
80
Scott Moreauec116022012-07-22 18:23:52 -060081 enum cursor_type grab_cursor;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030082
83 int painted;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010084};
85
86struct surface {
87 void (*configure)(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080088 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040089 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010090 int32_t width, int32_t height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040091};
92
Pekka Paalanencb115382017-12-07 12:15:01 +020093struct output;
94
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040095struct panel {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010096 struct surface base;
Pekka Paalanen1cbfcf42017-12-07 12:39:15 +020097
98 struct output *owner;
99
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400100 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500101 struct widget *widget;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500102 struct wl_list launcher_list;
Martin Minarik1e51a872012-06-08 00:39:11 +0200103 struct panel_clock *clock;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300104 int painted;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200105 enum weston_desktop_shell_panel_position panel_position;
Quentin Glidicf9574f22016-06-23 18:55:21 +0200106 enum clock_format clock_format;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300107 uint32_t color;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400108};
109
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100110struct background {
111 struct surface base;
Pekka Paalanencb115382017-12-07 12:15:01 +0200112
113 struct output *owner;
114
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100115 struct window *window;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500116 struct widget *widget;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300117 int painted;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300118
119 char *image;
120 int type;
121 uint32_t color;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100122};
123
124struct output {
125 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800126 uint32_t server_output_id;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100127 struct wl_list link;
128
Pekka Paalanenc1bcce62017-12-07 15:30:18 +0200129 int x;
130 int y;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100131 struct panel *panel;
132 struct background *background;
133};
134
Kristian Høgsberg53880802012-01-09 11:16:50 -0500135struct panel_launcher {
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500136 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400137 struct panel *panel;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400138 cairo_surface_t *icon;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500139 int focused, pressed;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400140 char *path;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500141 struct wl_list link;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400142 struct wl_array envp;
143 struct wl_array argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400144};
145
Martin Minarik1e51a872012-06-08 00:39:11 +0200146struct panel_clock {
147 struct widget *widget;
148 struct panel *panel;
Pekka Paalanen64a26bc2018-03-09 13:17:26 +0200149 struct toytimer timer;
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100150 char *format_string;
151 time_t refresh_timer;
Martin Minarik1e51a872012-06-08 00:39:11 +0200152};
153
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200154struct unlock_dialog {
155 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500156 struct widget *widget;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500157 struct widget *button;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500158 int button_focused;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200159 int closing;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200160 struct desktop *desktop;
161};
162
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300163static void
164panel_add_launchers(struct panel *panel, struct desktop *desktop);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500165
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400166static void
167sigchild_handler(int s)
168{
169 int status;
170 pid_t pid;
171
172 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
173 fprintf(stderr, "child %d exited\n", pid);
174}
175
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300176static int
177is_desktop_painted(struct desktop *desktop)
178{
179 struct output *output;
180
181 wl_list_for_each(output, &desktop->outputs, link) {
182 if (output->panel && !output->panel->painted)
183 return 0;
184 if (output->background && !output->background->painted)
185 return 0;
186 }
187
188 return 1;
189}
190
191static void
192check_desktop_ready(struct window *window)
193{
194 struct display *display;
195 struct desktop *desktop;
196
197 display = window_get_display(window);
198 desktop = display_get_user_data(display);
199
200 if (!desktop->painted && is_desktop_painted(desktop)) {
201 desktop->painted = 1;
202
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800203 weston_desktop_shell_desktop_ready(desktop->shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300204 }
205}
206
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400207static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500208panel_launcher_activate(struct panel_launcher *widget)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400209{
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400210 char **argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400211 pid_t pid;
212
213 pid = fork();
214 if (pid < 0) {
215 fprintf(stderr, "fork failed: %m\n");
216 return;
217 }
218
219 if (pid)
220 return;
Benjamin Franzked7759712011-11-22 12:38:48 +0100221
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400222 argv = widget->argv.data;
Derek Foreman091c8012017-03-24 09:41:13 -0500223
224 if (setsid() == -1)
225 exit(EXIT_FAILURE);
226
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400227 if (execve(argv[0], argv, widget->envp.data) < 0) {
228 fprintf(stderr, "execl '%s' failed: %m\n", argv[0]);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400229 exit(1);
230 }
231}
232
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400233static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500234panel_launcher_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400235{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500236 struct panel_launcher *launcher = data;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500237 struct rectangle allocation;
238 cairo_t *cr;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400239
Alexander Larssonc584fa62013-05-22 14:41:32 +0200240 cr = widget_cairo_create(launcher->panel->widget);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500241
242 widget_get_allocation(widget, &allocation);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500243 if (launcher->pressed) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500244 allocation.x++;
245 allocation.y++;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400246 }
247
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500248 cairo_set_source_surface(cr, launcher->icon,
249 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400250 cairo_paint(cr);
251
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500252 if (launcher->focused) {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400253 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500254 cairo_mask_surface(cr, launcher->icon,
255 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400256 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400257
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500258 cairo_destroy(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400259}
260
Tiago Vignatti61500722012-05-23 22:06:28 +0300261static int
262panel_launcher_motion_handler(struct widget *widget, struct input *input,
263 uint32_t time, float x, float y, void *data)
264{
265 struct panel_launcher *launcher = data;
266
267 widget_set_tooltip(widget, basename((char *)launcher->path), x, y);
268
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300269 return CURSOR_LEFT_PTR;
Tiago Vignatti61500722012-05-23 22:06:28 +0300270}
271
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400272static void
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500273set_hex_color(cairo_t *cr, uint32_t color)
274{
Michael Vetter2a18a522015-05-15 17:17:47 +0200275 cairo_set_source_rgba(cr,
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500276 ((color >> 16) & 0xff) / 255.0,
277 ((color >> 8) & 0xff) / 255.0,
278 ((color >> 0) & 0xff) / 255.0,
279 ((color >> 24) & 0xff) / 255.0);
280}
281
282static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500283panel_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400284{
285 cairo_surface_t *surface;
286 cairo_t *cr;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500287 struct panel *panel = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400288
Alexander Larssonc584fa62013-05-22 14:41:32 +0200289 cr = widget_cairo_create(panel->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400290 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300291 set_hex_color(cr, panel->color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400292 cairo_paint(cr);
293
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400294 cairo_destroy(cr);
Alexander Larssonc584fa62013-05-22 14:41:32 +0200295 surface = window_get_surface(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400296 cairo_surface_destroy(surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300297 panel->painted = 1;
298 check_desktop_ready(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400299}
300
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500301static int
Kristian Høgsberg53880802012-01-09 11:16:50 -0500302panel_launcher_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400303 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500304{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500305 struct panel_launcher *launcher = data;
306
307 launcher->focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500308 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500309
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300310 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500311}
312
313static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500314panel_launcher_leave_handler(struct widget *widget,
315 struct input *input, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400316{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500317 struct panel_launcher *launcher = data;
318
319 launcher->focused = 0;
Tiago Vignatti61500722012-05-23 22:06:28 +0300320 widget_destroy_tooltip(widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500321 widget_schedule_redraw(widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400322}
323
324static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500325panel_launcher_button_handler(struct widget *widget,
326 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100327 uint32_t button,
328 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400329{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500330 struct panel_launcher *launcher;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400331
Kristian Høgsberg53880802012-01-09 11:16:50 -0500332 launcher = widget_get_user_data(widget);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500333 widget_schedule_redraw(widget);
Daniel Stone4dbadb12012-05-30 16:31:51 +0100334 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg53880802012-01-09 11:16:50 -0500335 panel_launcher_activate(launcher);
Rusty Lynch4384a242013-08-08 21:28:22 -0700336
337}
338
339static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700340panel_launcher_touch_down_handler(struct widget *widget, struct input *input,
341 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch4384a242013-08-08 21:28:22 -0700342 float x, float y, void *data)
343{
344 struct panel_launcher *launcher;
345
346 launcher = widget_get_user_data(widget);
347 launcher->focused = 1;
348 widget_schedule_redraw(widget);
349}
350
351static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700352panel_launcher_touch_up_handler(struct widget *widget, struct input *input,
Michael Vetter2a18a522015-05-15 17:17:47 +0200353 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch1084da52013-08-15 09:10:08 -0700354 void *data)
Rusty Lynch4384a242013-08-08 21:28:22 -0700355{
356 struct panel_launcher *launcher;
357
358 launcher = widget_get_user_data(widget);
359 launcher->focused = 0;
360 widget_schedule_redraw(widget);
361 panel_launcher_activate(launcher);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500362}
363
Martin Minarik1e51a872012-06-08 00:39:11 +0200364static void
Pekka Paalanen64a26bc2018-03-09 13:17:26 +0200365clock_func(struct toytimer *tt)
Martin Minarik1e51a872012-06-08 00:39:11 +0200366{
Pekka Paalanen64a26bc2018-03-09 13:17:26 +0200367 struct panel_clock *clock = container_of(tt, struct panel_clock, timer);
Martin Minarik1e51a872012-06-08 00:39:11 +0200368
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400369 widget_schedule_redraw(clock->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200370}
371
372static void
373panel_clock_redraw_handler(struct widget *widget, void *data)
374{
Martin Minarik1e51a872012-06-08 00:39:11 +0200375 struct panel_clock *clock = data;
376 cairo_t *cr;
377 struct rectangle allocation;
378 cairo_text_extents_t extents;
379 cairo_font_extents_t font_extents;
Martin Minarik1e51a872012-06-08 00:39:11 +0200380 time_t rawtime;
381 struct tm * timeinfo;
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400382 char string[128];
Martin Minarik1e51a872012-06-08 00:39:11 +0200383
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400384 time(&rawtime);
385 timeinfo = localtime(&rawtime);
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100386 strftime(string, sizeof string, clock->format_string, timeinfo);
Martin Minarik1e51a872012-06-08 00:39:11 +0200387
388 widget_get_allocation(widget, &allocation);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400389 if (allocation.width == 0)
390 return;
Martin Minarik1e51a872012-06-08 00:39:11 +0200391
Alexander Larssonc584fa62013-05-22 14:41:32 +0200392 cr = widget_cairo_create(clock->panel->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200393 cairo_select_font_face(cr, "sans",
394 CAIRO_FONT_SLANT_NORMAL,
395 CAIRO_FONT_WEIGHT_NORMAL);
396 cairo_set_font_size(cr, 14);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400397 cairo_text_extents(cr, string, &extents);
Martin Minarik1e51a872012-06-08 00:39:11 +0200398 cairo_font_extents (cr, &font_extents);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400399 cairo_move_to(cr, allocation.x + 5,
400 allocation.y + 3 * (allocation.height >> 2) + 1);
401 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400402 cairo_show_text(cr, string);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400403 cairo_move_to(cr, allocation.x + 4,
404 allocation.y + 3 * (allocation.height >> 2));
405 cairo_set_source_rgb(cr, 1, 1, 1);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400406 cairo_show_text(cr, string);
Martin Minarik1e51a872012-06-08 00:39:11 +0200407 cairo_destroy(cr);
408}
409
410static int
411clock_timer_reset(struct panel_clock *clock)
412{
413 struct itimerspec its;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400414
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100415 its.it_interval.tv_sec = clock->refresh_timer;
Martin Minarik1e51a872012-06-08 00:39:11 +0200416 its.it_interval.tv_nsec = 0;
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100417 its.it_value.tv_sec = clock->refresh_timer;
Martin Minarik1e51a872012-06-08 00:39:11 +0200418 its.it_value.tv_nsec = 0;
Pekka Paalanen64a26bc2018-03-09 13:17:26 +0200419 toytimer_arm(&clock->timer, &its);
Martin Minarik1e51a872012-06-08 00:39:11 +0200420
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);
Pekka Paalanen64a26bc2018-03-09 13:17:26 +0200428 toytimer_fini(&clock->timer);
U. Artie Eoff44874d92012-10-02 21:12:35 -0700429 free(clock);
430}
431
432static void
Martin Minarik1e51a872012-06-08 00:39:11 +0200433panel_add_clock(struct panel *panel)
434{
435 struct panel_clock *clock;
Martin Minarik1e51a872012-06-08 00:39:11 +0200436
Peter Huttererf3d62272013-08-08 11:57:05 +1000437 clock = xzalloc(sizeof *clock);
Martin Minarik1e51a872012-06-08 00:39:11 +0200438 clock->panel = panel;
439 panel->clock = clock;
Martin Minarik1e51a872012-06-08 00:39:11 +0200440
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100441 switch (panel->clock_format) {
442 case CLOCK_FORMAT_MINUTES:
443 clock->format_string = "%a %b %d, %I:%M %p";
444 clock->refresh_timer = 60;
445 break;
446 case CLOCK_FORMAT_SECONDS:
447 clock->format_string = "%a %b %d, %I:%M:%S %p";
448 clock->refresh_timer = 1;
449 break;
Quentin Glidicf9574f22016-06-23 18:55:21 +0200450 case CLOCK_FORMAT_NONE:
451 assert(!"not reached");
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100452 }
453
Pekka Paalanen64a26bc2018-03-09 13:17:26 +0200454 toytimer_init(&clock->timer, CLOCK_MONOTONIC,
455 window_get_display(panel->window), clock_func);
Martin Minarik1e51a872012-06-08 00:39:11 +0200456 clock_timer_reset(clock);
457
458 clock->widget = widget_add_widget(panel->widget, clock);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400459 widget_set_redraw_handler(clock->widget, panel_clock_redraw_handler);
Martin Minarik1e51a872012-06-08 00:39:11 +0200460}
461
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500462static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500463panel_resize_handler(struct widget *widget,
464 int32_t width, int32_t height, void *data)
465{
466 struct panel_launcher *launcher;
467 struct panel *panel = data;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200468 int bx = width / 2;
Quentin Glidic51c2c372016-06-23 18:55:23 +0200469 int by = height / 2;
470 int spacing = 10;
471 int x = spacing;
472 int y = spacing;
473 int w, h;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200474 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 +0200475
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500476 wl_list_for_each(launcher, &panel->launcher_list, link) {
477 w = cairo_image_surface_get_width(launcher->icon);
478 h = cairo_image_surface_get_height(launcher->icon);
Quentin Glidic51c2c372016-06-23 18:55:23 +0200479
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200480 if (horizontal)
Quentin Glidic51c2c372016-06-23 18:55:23 +0200481 y = by - h / 2;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200482 else
483 x = bx - w / 2;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500484 widget_set_allocation(launcher->widget,
Quentin Glidic51c2c372016-06-23 18:55:23 +0200485 x, y, w + 1, h + 1);
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200486 if (horizontal)
Quentin Glidic51c2c372016-06-23 18:55:23 +0200487 x += w + spacing;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200488 else
489 y += h + spacing;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500490 }
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100491
492 h = 20;
493
494 if (panel->clock_format == CLOCK_FORMAT_SECONDS)
495 w = 190;
496 else /* CLOCK_FORMAT_MINUTES */
497 w = 170;
Pekka Paalanen01b17252012-06-12 17:42:26 +0300498
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200499 if (horizontal) {
Quentin Glidic51c2c372016-06-23 18:55:23 +0200500 x = width - w - spacing;
501 y = by - h / 2;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200502 } else {
503 x = bx - w / 2;
504 y = height - h - spacing;
505 }
Quentin Glidic51c2c372016-06-23 18:55:23 +0200506
Pekka Paalanen01b17252012-06-12 17:42:26 +0300507 if (panel->clock)
508 widget_set_allocation(panel->clock->widget,
Quentin Glidic51c2c372016-06-23 18:55:23 +0200509 x, y, w + 1, h + 1);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500510}
511
512static void
Pekka Paalanen1cbfcf42017-12-07 12:39:15 +0200513panel_destroy(struct panel *panel);
514
515static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100516panel_configure(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800517 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400518 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100519 int32_t width, int32_t height)
520{
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200521 struct desktop *desktop = data;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500522 struct surface *surface = window_get_user_data(window);
523 struct panel *panel = container_of(surface, struct panel, base);
Pekka Paalanen1cbfcf42017-12-07 12:39:15 +0200524 struct output *owner;
525
526 if (width < 1 || height < 1) {
527 /* Shell plugin configures 0x0 for redundant panel. */
528 owner = panel->owner;
529 panel_destroy(panel);
530 owner->panel = NULL;
531 return;
532 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500533
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200534 switch (desktop->panel_position) {
535 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
536 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Quentin Glidic51c2c372016-06-23 18:55:23 +0200537 height = 32;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200538 break;
539 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
540 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
541 switch (desktop->clock_format) {
542 case CLOCK_FORMAT_NONE:
543 width = 32;
544 break;
545 case CLOCK_FORMAT_MINUTES:
546 width = 170;
547 break;
548 case CLOCK_FORMAT_SECONDS:
549 width = 190;
550 break;
551 }
552 break;
553 }
Quentin Glidic51c2c372016-06-23 18:55:23 +0200554 window_schedule_resize(panel->window, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100555}
556
U. Artie Eoff44874d92012-10-02 21:12:35 -0700557static void
558panel_destroy_launcher(struct panel_launcher *launcher)
559{
560 wl_array_release(&launcher->argv);
561 wl_array_release(&launcher->envp);
562
563 free(launcher->path);
564
565 cairo_surface_destroy(launcher->icon);
566
567 widget_destroy(launcher->widget);
568 wl_list_remove(&launcher->link);
569
570 free(launcher);
571}
572
573static void
574panel_destroy(struct panel *panel)
575{
576 struct panel_launcher *tmp;
577 struct panel_launcher *launcher;
578
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100579 if (panel->clock)
580 panel_destroy_clock(panel->clock);
U. Artie Eoff44874d92012-10-02 21:12:35 -0700581
582 wl_list_for_each_safe(launcher, tmp, &panel->launcher_list, link)
583 panel_destroy_launcher(launcher);
584
585 widget_destroy(panel->widget);
586 window_destroy(panel->window);
587
588 free(panel);
589}
590
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400591static struct panel *
Pekka Paalanen1cbfcf42017-12-07 12:39:15 +0200592panel_create(struct desktop *desktop, struct output *output)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400593{
594 struct panel *panel;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300595 struct weston_config_section *s;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400596
Peter Huttererf3d62272013-08-08 11:57:05 +1000597 panel = xzalloc(sizeof *panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400598
Pekka Paalanen1cbfcf42017-12-07 12:39:15 +0200599 panel->owner = output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100600 panel->base.configure = panel_configure;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300601 panel->window = window_create_custom(desktop->display);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500602 panel->widget = window_add_widget(panel->window, panel);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500603 wl_list_init(&panel->launcher_list);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400604
605 window_set_title(panel->window, "panel");
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400606 window_set_user_data(panel->window, panel);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500607
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500608 widget_set_redraw_handler(panel->widget, panel_redraw_handler);
609 widget_set_resize_handler(panel->widget, panel_resize_handler);
Michael Vetter2a18a522015-05-15 17:17:47 +0200610
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200611 panel->panel_position = desktop->panel_position;
Quentin Glidic3e37b342016-06-23 18:55:22 +0200612 panel->clock_format = desktop->clock_format;
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100613 if (panel->clock_format != CLOCK_FORMAT_NONE)
614 panel_add_clock(panel);
615
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300616 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
Bryce Harringtone776f2a2016-07-14 18:28:03 -0700617 weston_config_section_get_color(s, "panel-color",
618 &panel->color, 0xaa000000);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300619
620 panel_add_launchers(panel, desktop);
621
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400622 return panel;
623}
624
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200625static cairo_surface_t *
626load_icon_or_fallback(const char *icon)
627{
628 cairo_surface_t *surface = cairo_image_surface_create_from_png(icon);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200629 cairo_status_t status;
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200630 cairo_t *cr;
631
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200632 status = cairo_surface_status(surface);
633 if (status == CAIRO_STATUS_SUCCESS)
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200634 return surface;
635
636 cairo_surface_destroy(surface);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200637 fprintf(stderr, "ERROR loading icon from file '%s', error: '%s'\n",
638 icon, cairo_status_to_string(status));
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200639
640 /* draw fallback icon */
641 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
642 20, 20);
643 cr = cairo_create(surface);
644
645 cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1);
646 cairo_paint(cr);
647
648 cairo_set_source_rgba(cr, 0, 0, 0, 1);
649 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
650 cairo_rectangle(cr, 0, 0, 20, 20);
651 cairo_move_to(cr, 4, 4);
652 cairo_line_to(cr, 16, 16);
653 cairo_move_to(cr, 4, 16);
654 cairo_line_to(cr, 16, 4);
655 cairo_stroke(cr);
656
657 cairo_destroy(cr);
658
659 return surface;
660}
661
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400662static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500663panel_add_launcher(struct panel *panel, const char *icon, const char *path)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400664{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500665 struct panel_launcher *launcher;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400666 char *start, *p, *eq, **ps;
667 int i, j, k;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400668
Peter Huttererf3d62272013-08-08 11:57:05 +1000669 launcher = xzalloc(sizeof *launcher);
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200670 launcher->icon = load_icon_or_fallback(icon);
U. Artie Eoff3c946772014-01-15 10:59:50 -0800671 launcher->path = xstrdup(path);
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400672
673 wl_array_init(&launcher->envp);
674 wl_array_init(&launcher->argv);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300675 for (i = 0; environ[i]; i++) {
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400676 ps = wl_array_add(&launcher->envp, sizeof *ps);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300677 *ps = environ[i];
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400678 }
679 j = 0;
680
681 start = launcher->path;
682 while (*start) {
683 for (p = start, eq = NULL; *p && !isspace(*p); p++)
684 if (*p == '=')
685 eq = p;
686
687 if (eq && j == 0) {
688 ps = launcher->envp.data;
689 for (k = 0; k < i; k++)
690 if (strncmp(ps[k], start, eq - start) == 0) {
691 ps[k] = start;
692 break;
693 }
694 if (k == i) {
695 ps = wl_array_add(&launcher->envp, sizeof *ps);
696 *ps = start;
697 i++;
698 }
699 } else {
700 ps = wl_array_add(&launcher->argv, sizeof *ps);
701 *ps = start;
702 j++;
703 }
704
705 while (*p && isspace(*p))
706 *p++ = '\0';
707
708 start = p;
709 }
710
711 ps = wl_array_add(&launcher->envp, sizeof *ps);
712 *ps = NULL;
713 ps = wl_array_add(&launcher->argv, sizeof *ps);
714 *ps = NULL;
715
Kristian Høgsberg53880802012-01-09 11:16:50 -0500716 launcher->panel = panel;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500717 wl_list_insert(panel->launcher_list.prev, &launcher->link);
718
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500719 launcher->widget = widget_add_widget(panel->widget, launcher);
Kristian Høgsberg53880802012-01-09 11:16:50 -0500720 widget_set_enter_handler(launcher->widget,
721 panel_launcher_enter_handler);
722 widget_set_leave_handler(launcher->widget,
723 panel_launcher_leave_handler);
724 widget_set_button_handler(launcher->widget,
725 panel_launcher_button_handler);
Rusty Lynch4384a242013-08-08 21:28:22 -0700726 widget_set_touch_down_handler(launcher->widget,
727 panel_launcher_touch_down_handler);
728 widget_set_touch_up_handler(launcher->widget,
729 panel_launcher_touch_up_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500730 widget_set_redraw_handler(launcher->widget,
731 panel_launcher_redraw_handler);
Tiago Vignatti61500722012-05-23 22:06:28 +0300732 widget_set_motion_handler(launcher->widget,
733 panel_launcher_motion_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400734}
735
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500736enum {
737 BACKGROUND_SCALE,
Pekka Paalanena402b052013-05-22 18:03:10 +0300738 BACKGROUND_SCALE_CROP,
Stefan Agner20b24162018-08-22 23:56:07 +0200739 BACKGROUND_TILE,
740 BACKGROUND_CENTERED
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500741};
742
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400743static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500744background_draw(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400745{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500746 struct background *background = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400747 cairo_surface_t *surface, *image;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400748 cairo_pattern_t *pattern;
749 cairo_matrix_t matrix;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400750 cairo_t *cr;
Pekka Paalanena402b052013-05-22 18:03:10 +0300751 double im_w, im_h;
752 double sx, sy, s;
753 double tx, ty;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500754 struct rectangle allocation;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400755
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500756 surface = window_get_surface(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400757
Alexander Larssonc584fa62013-05-22 14:41:32 +0200758 cr = widget_cairo_create(background->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400759 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Stefan Agner1c1e4fd2018-08-22 23:33:10 +0200760 if (background->color == 0)
761 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
762 else
763 set_hex_color(cr, background->color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400764 cairo_paint(cr);
765
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500766 widget_get_allocation(widget, &allocation);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500767 image = NULL;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300768 if (background->image)
769 image = load_cairo_surface(background->image);
Derek Foremane2772762018-02-06 15:18:38 -0600770 else if (background->color == 0) {
771 char *name = file_name_with_datadir("pattern.png");
772
773 image = load_cairo_surface(name);
774 free(name);
775 }
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500776
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300777 if (image && background->type != -1) {
Pekka Paalanena402b052013-05-22 18:03:10 +0300778 im_w = cairo_image_surface_get_width(image);
779 im_h = cairo_image_surface_get_height(image);
780 sx = im_w / allocation.width;
781 sy = im_h / allocation.height;
782
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400783 pattern = cairo_pattern_create_for_surface(image);
Pekka Paalanena402b052013-05-22 18:03:10 +0300784
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300785 switch (background->type) {
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500786 case BACKGROUND_SCALE:
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500787 cairo_matrix_init_scale(&matrix, sx, sy);
788 cairo_pattern_set_matrix(pattern, &matrix);
Bill Spitzak79b7cb32014-05-08 20:00:35 -0700789 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500790 break;
Pekka Paalanena402b052013-05-22 18:03:10 +0300791 case BACKGROUND_SCALE_CROP:
792 s = (sx < sy) ? sx : sy;
793 /* align center */
794 tx = (im_w - s * allocation.width) * 0.5;
795 ty = (im_h - s * allocation.height) * 0.5;
796 cairo_matrix_init_translate(&matrix, tx, ty);
797 cairo_matrix_scale(&matrix, s, s);
798 cairo_pattern_set_matrix(pattern, &matrix);
Bill Spitzak79b7cb32014-05-08 20:00:35 -0700799 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
Pekka Paalanena402b052013-05-22 18:03:10 +0300800 break;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500801 case BACKGROUND_TILE:
802 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
803 break;
Stefan Agner20b24162018-08-22 23:56:07 +0200804 case BACKGROUND_CENTERED:
805 s = (sx < sy) ? sx : sy;
806 if (s < 1.0)
807 s = 1.0;
808
809 /* align center */
810 tx = (im_w - s * allocation.width) * 0.5;
811 ty = (im_h - s * allocation.height) * 0.5;
812
813 cairo_matrix_init_translate(&matrix, tx, ty);
814 cairo_matrix_scale(&matrix, s, s);
815 cairo_pattern_set_matrix(pattern, &matrix);
816 break;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500817 }
Pekka Paalanena402b052013-05-22 18:03:10 +0300818
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400819 cairo_set_source(cr, pattern);
820 cairo_pattern_destroy (pattern);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400821 cairo_surface_destroy(image);
Stefan Agner20b24162018-08-22 23:56:07 +0200822 cairo_mask(cr, pattern);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400823 }
824
825 cairo_destroy(cr);
826 cairo_surface_destroy(surface);
Pekka Paalanen9564c752012-10-24 09:43:08 +0300827
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300828 background->painted = 1;
829 check_desktop_ready(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400830}
831
832static void
Pekka Paalanencb115382017-12-07 12:15:01 +0200833background_destroy(struct background *background);
834
835static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100836background_configure(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800837 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400838 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100839 int32_t width, int32_t height)
840{
Pekka Paalanencb115382017-12-07 12:15:01 +0200841 struct output *owner;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500842 struct background *background =
843 (struct background *) window_get_user_data(window);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100844
Pekka Paalanencb115382017-12-07 12:15:01 +0200845 if (width < 1 || height < 1) {
846 /* Shell plugin configures 0x0 for redundant background. */
847 owner = background->owner;
848 background_destroy(background);
849 owner->background = NULL;
850 return;
851 }
852
Kristian Høgsberg4598f962014-01-01 16:32:09 -0800853 widget_schedule_resize(background->widget, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100854}
855
856static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500857unlock_dialog_redraw_handler(struct widget *widget, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200858{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500859 struct unlock_dialog *dialog = data;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200860 struct rectangle allocation;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200861 cairo_surface_t *surface;
Alexander Larssonc584fa62013-05-22 14:41:32 +0200862 cairo_t *cr;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200863 cairo_pattern_t *pat;
864 double cx, cy, r, f;
865
Alexander Larssonc584fa62013-05-22 14:41:32 +0200866 cr = widget_cairo_create(widget);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500867
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500868 widget_get_allocation(dialog->widget, &allocation);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200869 cairo_rectangle(cr, allocation.x, allocation.y,
870 allocation.width, allocation.height);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200871 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
872 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500873 cairo_fill(cr);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200874
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500875 cairo_translate(cr, allocation.x, allocation.y);
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500876 if (dialog->button_focused)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200877 f = 1.0;
878 else
879 f = 0.7;
880
881 cx = allocation.width / 2.0;
882 cy = allocation.height / 2.0;
883 r = (cx < cy ? cx : cy) * 0.4;
884 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
885 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
886 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
887 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
888 cairo_set_source(cr, pat);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500889 cairo_pattern_destroy(pat);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200890 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
891 cairo_fill(cr);
892
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500893 widget_set_allocation(dialog->button,
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500894 allocation.x + cx - r,
895 allocation.y + cy - r, 2 * r, 2 * r);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200896
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200897 cairo_destroy(cr);
898
Alexander Larssonc584fa62013-05-22 14:41:32 +0200899 surface = window_get_surface(dialog->window);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200900 cairo_surface_destroy(surface);
901}
902
903static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500904unlock_dialog_button_handler(struct widget *widget,
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200905 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100906 uint32_t button,
907 enum wl_pointer_button_state state, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200908{
909 struct unlock_dialog *dialog = data;
910 struct desktop *desktop = dialog->desktop;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200911
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500912 if (button == BTN_LEFT) {
Daniel Stone4dbadb12012-05-30 16:31:51 +0100913 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
914 !dialog->closing) {
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200915 display_defer(desktop->display, &desktop->unlock_task);
916 dialog->closing = 1;
917 }
918 }
919}
920
921static void
Brian J Lovinc4df4082013-08-26 15:58:22 -0700922unlock_dialog_touch_down_handler(struct widget *widget, struct input *input,
923 uint32_t serial, uint32_t time, int32_t id,
924 float x, float y, void *data)
925{
926 struct unlock_dialog *dialog = data;
927
928 dialog->button_focused = 1;
929 widget_schedule_redraw(widget);
930}
931
932static void
933unlock_dialog_touch_up_handler(struct widget *widget, struct input *input,
934 uint32_t serial, uint32_t time, int32_t id,
935 void *data)
936{
937 struct unlock_dialog *dialog = data;
938 struct desktop *desktop = dialog->desktop;
939
940 dialog->button_focused = 0;
941 widget_schedule_redraw(widget);
942 display_defer(desktop->display, &desktop->unlock_task);
943 dialog->closing = 1;
944}
945
946static void
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200947unlock_dialog_keyboard_focus_handler(struct window *window,
948 struct input *device, void *data)
949{
950 window_schedule_redraw(window);
951}
952
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500953static int
Kristian Høgsbergee143232012-01-09 08:42:24 -0500954unlock_dialog_widget_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400955 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400956 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500957{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500958 struct unlock_dialog *dialog = data;
959
960 dialog->button_focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500961 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500962
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300963 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500964}
965
966static void
967unlock_dialog_widget_leave_handler(struct widget *widget,
968 struct input *input, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200969{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500970 struct unlock_dialog *dialog = data;
971
972 dialog->button_focused = 0;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500973 widget_schedule_redraw(widget);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200974}
975
976static struct unlock_dialog *
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500977unlock_dialog_create(struct desktop *desktop)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200978{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500979 struct display *display = desktop->display;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200980 struct unlock_dialog *dialog;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800981 struct wl_surface *surface;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200982
Peter Huttererf3d62272013-08-08 11:57:05 +1000983 dialog = xzalloc(sizeof *dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200984
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400985 dialog->window = window_create_custom(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500986 dialog->widget = window_frame_create(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200987 window_set_title(dialog->window, "Unlock your desktop");
988
989 window_set_user_data(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200990 window_set_keyboard_focus_handler(dialog->window,
991 unlock_dialog_keyboard_focus_handler);
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500992 dialog->button = widget_add_widget(dialog->widget, dialog);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500993 widget_set_redraw_handler(dialog->widget,
994 unlock_dialog_redraw_handler);
Kristian Høgsbergee143232012-01-09 08:42:24 -0500995 widget_set_enter_handler(dialog->button,
996 unlock_dialog_widget_enter_handler);
997 widget_set_leave_handler(dialog->button,
998 unlock_dialog_widget_leave_handler);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500999 widget_set_button_handler(dialog->button,
1000 unlock_dialog_button_handler);
Brian J Lovinc4df4082013-08-26 15:58:22 -07001001 widget_set_touch_down_handler(dialog->button,
1002 unlock_dialog_touch_down_handler);
1003 widget_set_touch_up_handler(dialog->button,
1004 unlock_dialog_touch_up_handler);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001005
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001006 surface = window_get_wl_surface(dialog->window);
1007 weston_desktop_shell_set_lock_surface(desktop->shell, surface);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001008
Pekka Paalanen40e49ac2012-01-18 16:51:30 +02001009 window_schedule_resize(dialog->window, 260, 230);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001010
1011 return dialog;
1012}
1013
1014static void
1015unlock_dialog_destroy(struct unlock_dialog *dialog)
1016{
1017 window_destroy(dialog->window);
1018 free(dialog);
1019}
1020
1021static void
1022unlock_dialog_finish(struct task *task, uint32_t events)
1023{
1024 struct desktop *desktop =
Benjamin Franzked7759712011-11-22 12:38:48 +01001025 container_of(task, struct desktop, unlock_task);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001026
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001027 weston_desktop_shell_unlock(desktop->shell);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001028 unlock_dialog_destroy(desktop->unlock_dialog);
1029 desktop->unlock_dialog = NULL;
1030}
1031
1032static void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001033desktop_shell_configure(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001034 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001035 uint32_t edges,
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001036 struct wl_surface *surface,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001037 int32_t width, int32_t height)
1038{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001039 struct window *window = wl_surface_get_user_data(surface);
Pekka Paalanen068ae942011-11-28 14:11:15 +02001040 struct surface *s = window_get_user_data(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001041
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001042 s->configure(data, desktop_shell, edges, window, width, height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001043}
1044
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001045static void
1046desktop_shell_prepare_lock_surface(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001047 struct weston_desktop_shell *desktop_shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001048{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001049 struct desktop *desktop = data;
1050
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001051 if (!desktop->locking) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001052 weston_desktop_shell_unlock(desktop->shell);
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +02001053 return;
1054 }
1055
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001056 if (!desktop->unlock_dialog) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001057 desktop->unlock_dialog = unlock_dialog_create(desktop);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001058 desktop->unlock_dialog->desktop = desktop;
1059 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001060}
1061
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001062static void
1063desktop_shell_grab_cursor(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001064 struct weston_desktop_shell *desktop_shell,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001065 uint32_t cursor)
1066{
1067 struct desktop *desktop = data;
1068
1069 switch (cursor) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001070 case WESTON_DESKTOP_SHELL_CURSOR_NONE:
Philipp Brüschweiler16d59d72012-08-24 15:43:55 +02001071 desktop->grab_cursor = CURSOR_BLANK;
1072 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001073 case WESTON_DESKTOP_SHELL_CURSOR_BUSY:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001074 desktop->grab_cursor = CURSOR_WATCH;
1075 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001076 case WESTON_DESKTOP_SHELL_CURSOR_MOVE:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001077 desktop->grab_cursor = CURSOR_DRAGGING;
1078 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001079 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001080 desktop->grab_cursor = CURSOR_TOP;
1081 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001082 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001083 desktop->grab_cursor = CURSOR_BOTTOM;
1084 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001085 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_LEFT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001086 desktop->grab_cursor = CURSOR_LEFT;
1087 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001088 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_RIGHT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001089 desktop->grab_cursor = CURSOR_RIGHT;
1090 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001091 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP_LEFT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001092 desktop->grab_cursor = CURSOR_TOP_LEFT;
1093 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001094 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP_RIGHT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001095 desktop->grab_cursor = CURSOR_TOP_RIGHT;
1096 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001097 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_LEFT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001098 desktop->grab_cursor = CURSOR_BOTTOM_LEFT;
1099 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001100 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_RIGHT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001101 desktop->grab_cursor = CURSOR_BOTTOM_RIGHT;
1102 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001103 case WESTON_DESKTOP_SHELL_CURSOR_ARROW:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001104 default:
1105 desktop->grab_cursor = CURSOR_LEFT_PTR;
1106 }
1107}
1108
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001109static const struct weston_desktop_shell_listener listener = {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001110 desktop_shell_configure,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001111 desktop_shell_prepare_lock_surface,
1112 desktop_shell_grab_cursor
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001113};
1114
U. Artie Eoff44874d92012-10-02 21:12:35 -07001115static void
1116background_destroy(struct background *background)
1117{
1118 widget_destroy(background->widget);
1119 window_destroy(background->window);
1120
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001121 free(background->image);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001122 free(background);
1123}
1124
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001125static struct background *
Pekka Paalanencb115382017-12-07 12:15:01 +02001126background_create(struct desktop *desktop, struct output *output)
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001127{
1128 struct background *background;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001129 struct weston_config_section *s;
1130 char *type;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001131
Peter Huttererf3d62272013-08-08 11:57:05 +10001132 background = xzalloc(sizeof *background);
Pekka Paalanencb115382017-12-07 12:15:01 +02001133 background->owner = output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001134 background->base.configure = background_configure;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001135 background->window = window_create_custom(desktop->display);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001136 background->widget = window_add_widget(background->window, background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001137 window_set_user_data(background->window, background);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001138 widget_set_redraw_handler(background->widget, background_draw);
Arnaud Vracfb754a02014-08-25 20:56:49 +02001139 widget_set_transparent(background->widget, 0);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001140
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001141 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
1142 weston_config_section_get_string(s, "background-image",
Kristian Høgsberg4c3661f2014-04-21 22:54:37 -07001143 &background->image, NULL);
Bryce Harringtone776f2a2016-07-14 18:28:03 -07001144 weston_config_section_get_color(s, "background-color",
1145 &background->color, 0x00000000);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001146
1147 weston_config_section_get_string(s, "background-type",
1148 &type, "tile");
U. Artie Eoff3c946772014-01-15 10:59:50 -08001149 if (type == NULL) {
1150 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
1151 exit(EXIT_FAILURE);
1152 }
1153
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001154 if (strcmp(type, "scale") == 0) {
1155 background->type = BACKGROUND_SCALE;
1156 } else if (strcmp(type, "scale-crop") == 0) {
1157 background->type = BACKGROUND_SCALE_CROP;
1158 } else if (strcmp(type, "tile") == 0) {
1159 background->type = BACKGROUND_TILE;
Stefan Agner20b24162018-08-22 23:56:07 +02001160 } else if (strcmp(type, "centered") == 0) {
1161 background->type = BACKGROUND_CENTERED;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001162 } else {
1163 background->type = -1;
1164 fprintf(stderr, "invalid background-type: %s\n",
1165 type);
1166 }
1167
1168 free(type);
1169
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001170 return background;
1171}
1172
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001173static int
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001174grab_surface_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001175 float x, float y, void *data)
1176{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001177 struct desktop *desktop = data;
1178
1179 return desktop->grab_cursor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001180}
1181
1182static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001183grab_surface_destroy(struct desktop *desktop)
1184{
1185 widget_destroy(desktop->grab_widget);
1186 window_destroy(desktop->grab_window);
1187}
1188
1189static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001190grab_surface_create(struct desktop *desktop)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001191{
1192 struct wl_surface *s;
1193
Ander Conselvan de Oliveira07a91cd2012-07-16 14:15:50 +03001194 desktop->grab_window = window_create_custom(desktop->display);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001195 window_set_user_data(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001196
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001197 s = window_get_wl_surface(desktop->grab_window);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001198 weston_desktop_shell_set_grab_surface(desktop->shell, s);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001199
1200 desktop->grab_widget =
1201 window_add_widget(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001202 /* We set the allocation to 1x1 at 0,0 so the fake enter event
1203 * at 0,0 will go to this widget. */
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001204 widget_set_allocation(desktop->grab_widget, 0, 0, 1, 1);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001205
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001206 widget_set_enter_handler(desktop->grab_widget,
1207 grab_surface_enter_handler);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001208}
1209
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001210static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001211output_destroy(struct output *output)
1212{
Pekka Paalanencb115382017-12-07 12:15:01 +02001213 if (output->background)
1214 background_destroy(output->background);
Jonny Lambe67118c2014-08-12 15:07:51 +02001215 if (output->panel)
1216 panel_destroy(output->panel);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001217 wl_output_destroy(output->output);
1218 wl_list_remove(&output->link);
1219
1220 free(output);
1221}
1222
1223static void
1224desktop_destroy_outputs(struct desktop *desktop)
1225{
1226 struct output *tmp;
1227 struct output *output;
1228
1229 wl_list_for_each_safe(output, tmp, &desktop->outputs, link)
1230 output_destroy(output);
1231}
1232
1233static void
Alexander Larssonc584fa62013-05-22 14:41:32 +02001234output_handle_geometry(void *data,
1235 struct wl_output *wl_output,
1236 int x, int y,
1237 int physical_width,
1238 int physical_height,
1239 int subpixel,
1240 const char *make,
1241 const char *model,
1242 int transform)
1243{
1244 struct output *output = data;
1245
Pekka Paalanenc1bcce62017-12-07 15:30:18 +02001246 output->x = x;
1247 output->y = y;
1248
Jonny Lambe67118c2014-08-12 15:07:51 +02001249 if (output->panel)
1250 window_set_buffer_transform(output->panel->window, transform);
Pekka Paalanencb115382017-12-07 12:15:01 +02001251 if (output->background)
1252 window_set_buffer_transform(output->background->window, transform);
Alexander Larssonc584fa62013-05-22 14:41:32 +02001253}
1254
1255static void
1256output_handle_mode(void *data,
1257 struct wl_output *wl_output,
1258 uint32_t flags,
1259 int width,
1260 int height,
1261 int refresh)
1262{
1263}
1264
1265static void
1266output_handle_done(void *data,
1267 struct wl_output *wl_output)
1268{
1269}
1270
1271static void
1272output_handle_scale(void *data,
1273 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001274 int32_t scale)
Alexander Larssonc584fa62013-05-22 14:41:32 +02001275{
1276 struct output *output = data;
1277
Jonny Lambe67118c2014-08-12 15:07:51 +02001278 if (output->panel)
1279 window_set_buffer_scale(output->panel->window, scale);
Pekka Paalanencb115382017-12-07 12:15:01 +02001280 if (output->background)
1281 window_set_buffer_scale(output->background->window, scale);
Alexander Larssonc584fa62013-05-22 14:41:32 +02001282}
1283
1284static const struct wl_output_listener output_listener = {
1285 output_handle_geometry,
1286 output_handle_mode,
1287 output_handle_done,
1288 output_handle_scale
1289};
1290
1291static void
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001292output_init(struct output *output, struct desktop *desktop)
1293{
1294 struct wl_surface *surface;
1295
Quentin Glidic55d57012016-06-23 18:55:18 +02001296 if (desktop->want_panel) {
Pekka Paalanen1cbfcf42017-12-07 12:39:15 +02001297 output->panel = panel_create(desktop, output);
Jonny Lambe67118c2014-08-12 15:07:51 +02001298 surface = window_get_wl_surface(output->panel->window);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001299 weston_desktop_shell_set_panel(desktop->shell,
1300 output->output, surface);
Jonny Lambe67118c2014-08-12 15:07:51 +02001301 }
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001302
Pekka Paalanencb115382017-12-07 12:15:01 +02001303 output->background = background_create(desktop, output);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001304 surface = window_get_wl_surface(output->background->window);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001305 weston_desktop_shell_set_background(desktop->shell,
1306 output->output, surface);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001307}
1308
1309static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001310create_output(struct desktop *desktop, uint32_t id)
1311{
1312 struct output *output;
1313
Bryce Harrington0d1a6222016-02-11 16:42:49 -08001314 output = zalloc(sizeof *output);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001315 if (!output)
1316 return;
1317
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001318 output->output =
Alexander Larssonc584fa62013-05-22 14:41:32 +02001319 display_bind(desktop->display, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001320 output->server_output_id = id;
Alexander Larssonc584fa62013-05-22 14:41:32 +02001321
1322 wl_output_add_listener(output->output, &output_listener, output);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001323
1324 wl_list_insert(&desktop->outputs, &output->link);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001325
1326 /* On start up we may process an output global before the shell global
1327 * in which case we can't create the panel and background just yet */
1328 if (desktop->shell)
1329 output_init(output, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001330}
1331
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001332static void
Pekka Paalanenc1bcce62017-12-07 15:30:18 +02001333output_remove(struct desktop *desktop, struct output *output)
1334{
1335 struct output *cur;
1336 struct output *rep = NULL;
1337
1338 if (!output->background) {
1339 output_destroy(output);
1340 return;
1341 }
1342
1343 /* Find a wl_output that is a clone of the removed wl_output.
1344 * We don't want to leave the clone without a background or panel. */
1345 wl_list_for_each(cur, &desktop->outputs, link) {
1346 if (cur == output)
1347 continue;
1348
1349 /* XXX: Assumes size matches. */
1350 if (cur->x == output->x && cur->y == output->y) {
1351 rep = cur;
1352 break;
1353 }
1354 }
1355
1356 if (rep) {
Pekka Paalanen82dd54d2018-06-21 15:38:56 +03001357 /* If found and it does not already have a background or panel,
1358 * hand over the background and panel so they don't get
1359 * destroyed.
1360 *
1361 * We never create multiple backgrounds or panels for clones,
1362 * but if the compositor moves outputs, a pair of wl_outputs
1363 * might become "clones". This may happen temporarily when
1364 * an output is about to be removed and the rest are reflowed.
1365 * In this case it is correct to let the background/panel be
1366 * destroyed.
1367 */
Pekka Paalanenc1bcce62017-12-07 15:30:18 +02001368
Pekka Paalanen82dd54d2018-06-21 15:38:56 +03001369 if (!rep->background) {
1370 rep->background = output->background;
1371 output->background = NULL;
1372 rep->background->owner = rep;
1373 }
Pekka Paalanenc1bcce62017-12-07 15:30:18 +02001374
Pekka Paalanen82dd54d2018-06-21 15:38:56 +03001375 if (!rep->panel) {
1376 rep->panel = output->panel;
1377 output->panel = NULL;
1378 if (rep->panel)
1379 rep->panel->owner = rep;
1380 }
Pekka Paalanenc1bcce62017-12-07 15:30:18 +02001381 }
1382
1383 output_destroy(output);
1384}
1385
1386static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001387global_handler(struct display *display, uint32_t id,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001388 const char *interface, uint32_t version, void *data)
1389{
1390 struct desktop *desktop = data;
1391
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001392 if (!strcmp(interface, "weston_desktop_shell")) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001393 desktop->shell = display_bind(desktop->display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001394 id,
1395 &weston_desktop_shell_interface,
1396 1);
1397 weston_desktop_shell_add_listener(desktop->shell,
1398 &listener,
1399 desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001400 } else if (!strcmp(interface, "wl_output")) {
1401 create_output(desktop, id);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001402 }
1403}
1404
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001405static void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001406global_handler_remove(struct display *display, uint32_t id,
1407 const char *interface, uint32_t version, void *data)
1408{
1409 struct desktop *desktop = data;
1410 struct output *output;
1411
1412 if (!strcmp(interface, "wl_output")) {
1413 wl_list_for_each(output, &desktop->outputs, link) {
1414 if (output->server_output_id == id) {
Pekka Paalanenc1bcce62017-12-07 15:30:18 +02001415 output_remove(desktop, output);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001416 break;
1417 }
1418 }
1419 }
1420}
1421
1422static void
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001423panel_add_launchers(struct panel *panel, struct desktop *desktop)
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001424{
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001425 struct weston_config_section *s;
1426 char *icon, *path;
1427 const char *name;
1428 int count;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001429
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001430 count = 0;
1431 s = NULL;
1432 while (weston_config_next_section(desktop->config, &s, &name)) {
1433 if (strcmp(name, "launcher") != 0)
1434 continue;
1435
1436 weston_config_section_get_string(s, "icon", &icon, NULL);
1437 weston_config_section_get_string(s, "path", &path, NULL);
1438
1439 if (icon != NULL && path != NULL) {
1440 panel_add_launcher(panel, icon, path);
Rob Bradford09252d42013-07-26 16:29:45 +01001441 count++;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001442 } else {
1443 fprintf(stderr, "invalid launcher section\n");
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001444 }
1445
1446 free(icon);
1447 free(path);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001448 }
1449
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001450 if (count == 0) {
Derek Foremane2772762018-02-06 15:18:38 -06001451 char *name = file_name_with_datadir("terminal.png");
1452
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001453 /* add default launcher */
1454 panel_add_launcher(panel,
Derek Foremane2772762018-02-06 15:18:38 -06001455 name,
Rodney Lorrimar99ff01b2012-02-29 17:31:03 +01001456 BINDIR "/weston-terminal");
Derek Foremane2772762018-02-06 15:18:38 -06001457 free(name);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001458 }
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001459}
1460
Quentin Glidic55d57012016-06-23 18:55:18 +02001461static void
1462parse_panel_position(struct desktop *desktop, struct weston_config_section *s)
1463{
1464 char *position;
1465
Daniel Stone08cf24b2017-01-16 13:18:21 +00001466 desktop->want_panel = 1;
Quentin Glidic55d57012016-06-23 18:55:18 +02001467
Daniel Stone08cf24b2017-01-16 13:18:21 +00001468 weston_config_section_get_string(s, "panel-position", &position, "top");
1469 if (strcmp(position, "top") == 0) {
1470 desktop->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
1471 } else if (strcmp(position, "bottom") == 0) {
1472 desktop->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM;
1473 } else if (strcmp(position, "left") == 0) {
1474 desktop->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT;
1475 } else if (strcmp(position, "right") == 0) {
1476 desktop->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT;
1477 } else {
1478 /* 'none' is valid here */
1479 if (strcmp(position, "none") != 0)
1480 fprintf(stderr, "Wrong panel position: %s\n", position);
1481 desktop->want_panel = 0;
Quentin Glidic55d57012016-06-23 18:55:18 +02001482 }
Daniel Stone08cf24b2017-01-16 13:18:21 +00001483 free(position);
Quentin Glidic55d57012016-06-23 18:55:18 +02001484}
1485
Quentin Glidic3e37b342016-06-23 18:55:22 +02001486static void
1487parse_clock_format(struct desktop *desktop, struct weston_config_section *s)
1488{
1489 char *clock_format;
1490
1491 weston_config_section_get_string(s, "clock-format", &clock_format, "");
1492 if (strcmp(clock_format, "minutes") == 0)
1493 desktop->clock_format = CLOCK_FORMAT_MINUTES;
1494 else if (strcmp(clock_format, "seconds") == 0)
1495 desktop->clock_format = CLOCK_FORMAT_SECONDS;
1496 else if (strcmp(clock_format, "none") == 0)
1497 desktop->clock_format = CLOCK_FORMAT_NONE;
1498 else
1499 desktop->clock_format = DEFAULT_CLOCK_FORMAT;
1500 free(clock_format);
1501}
1502
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001503int main(int argc, char *argv[])
1504{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001505 struct desktop desktop = { 0 };
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001506 struct output *output;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001507 struct weston_config_section *s;
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001508 const char *config_file;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001509
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001510 desktop.unlock_task.run = unlock_dialog_finish;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001511 wl_list_init(&desktop.outputs);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001512
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001513 config_file = weston_config_get_name_from_env();
1514 desktop.config = weston_config_parse(config_file);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001515 s = weston_config_get_section(desktop.config, "shell", NULL, NULL);
1516 weston_config_section_get_bool(s, "locking", &desktop.locking, 1);
Quentin Glidic55d57012016-06-23 18:55:18 +02001517 parse_panel_position(&desktop, s);
Quentin Glidic3e37b342016-06-23 18:55:22 +02001518 parse_clock_format(&desktop, s);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001519
Kristian Høgsberg4172f662013-02-20 15:27:49 -05001520 desktop.display = display_create(&argc, argv);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001521 if (desktop.display == NULL) {
1522 fprintf(stderr, "failed to create display: %m\n");
1523 return -1;
1524 }
1525
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001526 display_set_user_data(desktop.display, &desktop);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001527 display_set_global_handler(desktop.display, global_handler);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001528 display_set_global_handler_remove(desktop.display, global_handler_remove);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001529
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001530 /* Create panel and background for outputs processed before the shell
1531 * global interface was processed */
Quentin Glidic55d57012016-06-23 18:55:18 +02001532 if (desktop.want_panel)
1533 weston_desktop_shell_set_panel_position(desktop.shell, desktop.panel_position);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001534 wl_list_for_each(output, &desktop.outputs, link)
1535 if (!output->panel)
1536 output_init(output, &desktop);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001537
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001538 grab_surface_create(&desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001539
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001540 signal(SIGCHLD, sigchild_handler);
1541
1542 display_run(desktop.display);
1543
U. Artie Eoff44874d92012-10-02 21:12:35 -07001544 /* Cleanup */
1545 grab_surface_destroy(&desktop);
1546 desktop_destroy_outputs(&desktop);
1547 if (desktop.unlock_dialog)
1548 unlock_dialog_destroy(desktop.unlock_dialog);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001549 weston_desktop_shell_destroy(desktop.shell);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001550 display_destroy(desktop.display);
1551
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001552 return 0;
1553}