blob: 55cbd0014a5884723d50c8aeb21bab5ab58a6e75 [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"
Pekka Paalanen91b10102019-04-04 14:27:31 +030046#include <libweston/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"
Pekka Paalanenecbdcfd2019-04-04 14:46:00 +030049#include <libweston/zalloc.h>
Derek Foremane2772762018-02-06 15:18:38 -060050#include "shared/file-util.h"
Veeresh Kadasanied4a0e92020-12-21 15:24:51 +053051#include "shared/timespec-util.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040052
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080053#include "weston-desktop-shell-client-protocol.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040054
Armin Krezovićc6a55db2016-03-10 18:02:34 +010055#define DEFAULT_CLOCK_FORMAT CLOCK_FORMAT_MINUTES
-c505af82019-06-26 21:00:43 +000056#define DEFAULT_SPACING 10
Armin Krezovićc6a55db2016-03-10 18:02:34 +010057
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;
Daniel Stone51d995a2019-11-26 00:14:24 +000081 bool locking;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +030082
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
Pekka Paalanencb115382017-12-07 12:15:01 +020095struct output;
96
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040097struct panel {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010098 struct surface base;
Pekka Paalanen1cbfcf42017-12-07 12:39:15 +020099
100 struct output *owner;
101
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400102 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500103 struct widget *widget;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500104 struct wl_list launcher_list;
Martin Minarik1e51a872012-06-08 00:39:11 +0200105 struct panel_clock *clock;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300106 int painted;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200107 enum weston_desktop_shell_panel_position panel_position;
Quentin Glidicf9574f22016-06-23 18:55:21 +0200108 enum clock_format clock_format;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300109 uint32_t color;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400110};
111
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100112struct background {
113 struct surface base;
Pekka Paalanencb115382017-12-07 12:15:01 +0200114
115 struct output *owner;
116
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100117 struct window *window;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500118 struct widget *widget;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300119 int painted;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300120
121 char *image;
122 int type;
123 uint32_t color;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100124};
125
126struct output {
127 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800128 uint32_t server_output_id;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100129 struct wl_list link;
130
Pekka Paalanenc1bcce62017-12-07 15:30:18 +0200131 int x;
132 int y;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100133 struct panel *panel;
134 struct background *background;
135};
136
Kristian Høgsberg53880802012-01-09 11:16:50 -0500137struct panel_launcher {
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500138 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400139 struct panel *panel;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400140 cairo_surface_t *icon;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500141 int focused, pressed;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400142 char *path;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500143 struct wl_list link;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400144 struct wl_array envp;
145 struct wl_array argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400146};
147
Martin Minarik1e51a872012-06-08 00:39:11 +0200148struct panel_clock {
149 struct widget *widget;
150 struct panel *panel;
Pekka Paalanen64a26bc2018-03-09 13:17:26 +0200151 struct toytimer timer;
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100152 char *format_string;
153 time_t refresh_timer;
Martin Minarik1e51a872012-06-08 00:39:11 +0200154};
155
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200156struct unlock_dialog {
157 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500158 struct widget *widget;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500159 struct widget *button;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500160 int button_focused;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200161 int closing;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200162 struct desktop *desktop;
163};
164
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300165static void
166panel_add_launchers(struct panel *panel, struct desktop *desktop);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500167
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400168static void
169sigchild_handler(int s)
170{
171 int status;
172 pid_t pid;
173
174 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
175 fprintf(stderr, "child %d exited\n", pid);
176}
177
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300178static int
179is_desktop_painted(struct desktop *desktop)
180{
181 struct output *output;
182
183 wl_list_for_each(output, &desktop->outputs, link) {
184 if (output->panel && !output->panel->painted)
185 return 0;
186 if (output->background && !output->background->painted)
187 return 0;
188 }
189
190 return 1;
191}
192
193static void
194check_desktop_ready(struct window *window)
195{
196 struct display *display;
197 struct desktop *desktop;
198
199 display = window_get_display(window);
200 desktop = display_get_user_data(display);
201
202 if (!desktop->painted && is_desktop_painted(desktop)) {
203 desktop->painted = 1;
204
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800205 weston_desktop_shell_desktop_ready(desktop->shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300206 }
207}
208
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400209static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500210panel_launcher_activate(struct panel_launcher *widget)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400211{
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400212 char **argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400213 pid_t pid;
214
215 pid = fork();
216 if (pid < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +0200217 fprintf(stderr, "fork failed: %s\n", strerror(errno));
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400218 return;
219 }
220
221 if (pid)
222 return;
Benjamin Franzked7759712011-11-22 12:38:48 +0100223
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400224 argv = widget->argv.data;
Derek Foreman091c8012017-03-24 09:41:13 -0500225
226 if (setsid() == -1)
227 exit(EXIT_FAILURE);
228
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400229 if (execve(argv[0], argv, widget->envp.data) < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +0200230 fprintf(stderr, "execl '%s' failed: %s\n", argv[0],
231 strerror(errno));
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400232 exit(1);
233 }
234}
235
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400236static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500237panel_launcher_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400238{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500239 struct panel_launcher *launcher = data;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500240 struct rectangle allocation;
241 cairo_t *cr;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400242
Alexander Larssonc584fa62013-05-22 14:41:32 +0200243 cr = widget_cairo_create(launcher->panel->widget);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500244
245 widget_get_allocation(widget, &allocation);
-c505af82019-06-26 21:00:43 +0000246 allocation.x += allocation.width / 2 -
247 cairo_image_surface_get_width(launcher->icon) / 2;
248 if (allocation.width > allocation.height)
249 allocation.x += allocation.width / 2 - allocation.height / 2;
250 allocation.y += allocation.height / 2 -
251 cairo_image_surface_get_height(launcher->icon) / 2;
252 if (allocation.height > allocation.width)
253 allocation.y += allocation.height / 2 - allocation.width / 2;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500254 if (launcher->pressed) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500255 allocation.x++;
256 allocation.y++;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400257 }
258
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500259 cairo_set_source_surface(cr, launcher->icon,
260 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400261 cairo_paint(cr);
262
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500263 if (launcher->focused) {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400264 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500265 cairo_mask_surface(cr, launcher->icon,
266 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400267 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400268
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500269 cairo_destroy(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400270}
271
Tiago Vignatti61500722012-05-23 22:06:28 +0300272static int
273panel_launcher_motion_handler(struct widget *widget, struct input *input,
274 uint32_t time, float x, float y, void *data)
275{
276 struct panel_launcher *launcher = data;
277
278 widget_set_tooltip(widget, basename((char *)launcher->path), x, y);
279
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300280 return CURSOR_LEFT_PTR;
Tiago Vignatti61500722012-05-23 22:06:28 +0300281}
282
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400283static void
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500284set_hex_color(cairo_t *cr, uint32_t color)
285{
Michael Vetter2a18a522015-05-15 17:17:47 +0200286 cairo_set_source_rgba(cr,
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500287 ((color >> 16) & 0xff) / 255.0,
288 ((color >> 8) & 0xff) / 255.0,
289 ((color >> 0) & 0xff) / 255.0,
290 ((color >> 24) & 0xff) / 255.0);
291}
292
293static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500294panel_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400295{
296 cairo_surface_t *surface;
297 cairo_t *cr;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500298 struct panel *panel = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400299
Alexander Larssonc584fa62013-05-22 14:41:32 +0200300 cr = widget_cairo_create(panel->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400301 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300302 set_hex_color(cr, panel->color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400303 cairo_paint(cr);
304
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400305 cairo_destroy(cr);
Alexander Larssonc584fa62013-05-22 14:41:32 +0200306 surface = window_get_surface(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400307 cairo_surface_destroy(surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300308 panel->painted = 1;
309 check_desktop_ready(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400310}
311
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500312static int
Kristian Høgsberg53880802012-01-09 11:16:50 -0500313panel_launcher_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400314 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500315{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500316 struct panel_launcher *launcher = data;
317
318 launcher->focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500319 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500320
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300321 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500322}
323
324static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500325panel_launcher_leave_handler(struct widget *widget,
326 struct input *input, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400327{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500328 struct panel_launcher *launcher = data;
329
330 launcher->focused = 0;
Tiago Vignatti61500722012-05-23 22:06:28 +0300331 widget_destroy_tooltip(widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500332 widget_schedule_redraw(widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400333}
334
335static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500336panel_launcher_button_handler(struct widget *widget,
337 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100338 uint32_t button,
339 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400340{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500341 struct panel_launcher *launcher;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400342
Kristian Høgsberg53880802012-01-09 11:16:50 -0500343 launcher = widget_get_user_data(widget);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500344 widget_schedule_redraw(widget);
Daniel Stone4dbadb12012-05-30 16:31:51 +0100345 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg53880802012-01-09 11:16:50 -0500346 panel_launcher_activate(launcher);
Rusty Lynch4384a242013-08-08 21:28:22 -0700347
348}
349
350static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700351panel_launcher_touch_down_handler(struct widget *widget, struct input *input,
352 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch4384a242013-08-08 21:28:22 -0700353 float x, float y, void *data)
354{
355 struct panel_launcher *launcher;
356
357 launcher = widget_get_user_data(widget);
358 launcher->focused = 1;
359 widget_schedule_redraw(widget);
360}
361
362static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700363panel_launcher_touch_up_handler(struct widget *widget, struct input *input,
Michael Vetter2a18a522015-05-15 17:17:47 +0200364 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch1084da52013-08-15 09:10:08 -0700365 void *data)
Rusty Lynch4384a242013-08-08 21:28:22 -0700366{
367 struct panel_launcher *launcher;
368
369 launcher = widget_get_user_data(widget);
370 launcher->focused = 0;
371 widget_schedule_redraw(widget);
372 panel_launcher_activate(launcher);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500373}
374
Martin Minarik1e51a872012-06-08 00:39:11 +0200375static void
Pekka Paalanen64a26bc2018-03-09 13:17:26 +0200376clock_func(struct toytimer *tt)
Martin Minarik1e51a872012-06-08 00:39:11 +0200377{
Pekka Paalanen64a26bc2018-03-09 13:17:26 +0200378 struct panel_clock *clock = container_of(tt, struct panel_clock, timer);
Martin Minarik1e51a872012-06-08 00:39:11 +0200379
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400380 widget_schedule_redraw(clock->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200381}
382
383static void
384panel_clock_redraw_handler(struct widget *widget, void *data)
385{
Martin Minarik1e51a872012-06-08 00:39:11 +0200386 struct panel_clock *clock = data;
387 cairo_t *cr;
388 struct rectangle allocation;
389 cairo_text_extents_t extents;
Martin Minarik1e51a872012-06-08 00:39:11 +0200390 time_t rawtime;
391 struct tm * timeinfo;
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400392 char string[128];
Martin Minarik1e51a872012-06-08 00:39:11 +0200393
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400394 time(&rawtime);
395 timeinfo = localtime(&rawtime);
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100396 strftime(string, sizeof string, clock->format_string, timeinfo);
Martin Minarik1e51a872012-06-08 00:39:11 +0200397
398 widget_get_allocation(widget, &allocation);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400399 if (allocation.width == 0)
400 return;
Martin Minarik1e51a872012-06-08 00:39:11 +0200401
Alexander Larssonc584fa62013-05-22 14:41:32 +0200402 cr = widget_cairo_create(clock->panel->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200403 cairo_set_font_size(cr, 14);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400404 cairo_text_extents(cr, string, &extents);
-c505af82019-06-26 21:00:43 +0000405 if (allocation.x > 0)
406 allocation.x +=
407 allocation.width - DEFAULT_SPACING * 1.5 - extents.width;
408 else
409 allocation.x +=
410 allocation.width / 2 - extents.width / 2;
411 allocation.y += allocation.height / 2 - 1 + extents.height / 2;
412 cairo_move_to(cr, allocation.x + 1, allocation.y + 1);
413 cairo_set_source_rgba(cr, 0, 0, 0, 0.85);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400414 cairo_show_text(cr, string);
-c505af82019-06-26 21:00:43 +0000415 cairo_move_to(cr, allocation.x, allocation.y);
416 cairo_set_source_rgba(cr, 1, 1, 1, 0.85);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400417 cairo_show_text(cr, string);
Martin Minarik1e51a872012-06-08 00:39:11 +0200418 cairo_destroy(cr);
419}
420
421static int
422clock_timer_reset(struct panel_clock *clock)
423{
424 struct itimerspec its;
Veeresh Kadasanied4a0e92020-12-21 15:24:51 +0530425 struct timespec ts;
426 struct tm *tm;
427
428 clock_gettime(CLOCK_REALTIME, &ts);
429 tm = localtime(&ts.tv_sec);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400430
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100431 its.it_interval.tv_sec = clock->refresh_timer;
Martin Minarik1e51a872012-06-08 00:39:11 +0200432 its.it_interval.tv_nsec = 0;
Veeresh Kadasanied4a0e92020-12-21 15:24:51 +0530433 its.it_value.tv_sec = clock->refresh_timer - tm->tm_sec % clock->refresh_timer;
434 its.it_value.tv_nsec = 10000000; /* 10 ms late to ensure the clock digit has actually changed */
435 timespec_add_nsec(&its.it_value, &its.it_value, -ts.tv_nsec);
Martin Minarik1e51a872012-06-08 00:39:11 +0200436
Veeresh Kadasanied4a0e92020-12-21 15:24:51 +0530437 toytimer_arm(&clock->timer, &its);
Martin Minarik1e51a872012-06-08 00:39:11 +0200438 return 0;
439}
440
441static void
U. Artie Eoff44874d92012-10-02 21:12:35 -0700442panel_destroy_clock(struct panel_clock *clock)
443{
444 widget_destroy(clock->widget);
Pekka Paalanen64a26bc2018-03-09 13:17:26 +0200445 toytimer_fini(&clock->timer);
U. Artie Eoff44874d92012-10-02 21:12:35 -0700446 free(clock);
447}
448
449static void
Martin Minarik1e51a872012-06-08 00:39:11 +0200450panel_add_clock(struct panel *panel)
451{
452 struct panel_clock *clock;
Martin Minarik1e51a872012-06-08 00:39:11 +0200453
Peter Huttererf3d62272013-08-08 11:57:05 +1000454 clock = xzalloc(sizeof *clock);
Martin Minarik1e51a872012-06-08 00:39:11 +0200455 clock->panel = panel;
456 panel->clock = clock;
Martin Minarik1e51a872012-06-08 00:39:11 +0200457
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100458 switch (panel->clock_format) {
459 case CLOCK_FORMAT_MINUTES:
460 clock->format_string = "%a %b %d, %I:%M %p";
461 clock->refresh_timer = 60;
462 break;
463 case CLOCK_FORMAT_SECONDS:
464 clock->format_string = "%a %b %d, %I:%M:%S %p";
465 clock->refresh_timer = 1;
466 break;
Quentin Glidicf9574f22016-06-23 18:55:21 +0200467 case CLOCK_FORMAT_NONE:
468 assert(!"not reached");
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100469 }
470
Pekka Paalanen64a26bc2018-03-09 13:17:26 +0200471 toytimer_init(&clock->timer, CLOCK_MONOTONIC,
472 window_get_display(panel->window), clock_func);
Martin Minarik1e51a872012-06-08 00:39:11 +0200473 clock_timer_reset(clock);
474
475 clock->widget = widget_add_widget(panel->widget, clock);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400476 widget_set_redraw_handler(clock->widget, panel_clock_redraw_handler);
Martin Minarik1e51a872012-06-08 00:39:11 +0200477}
478
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500479static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500480panel_resize_handler(struct widget *widget,
481 int32_t width, int32_t height, void *data)
482{
483 struct panel_launcher *launcher;
484 struct panel *panel = data;
-c505af82019-06-26 21:00:43 +0000485 int x = 0;
486 int y = 0;
487 int w = height > width ? width : height;
488 int h = w;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200489 int horizontal = panel->panel_position == WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP || panel->panel_position == WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM;
-c505af82019-06-26 21:00:43 +0000490 int first_pad_h = horizontal ? 0 : DEFAULT_SPACING / 2;
491 int first_pad_w = horizontal ? DEFAULT_SPACING / 2 : 0;
Michael Vetter2a18a522015-05-15 17:17:47 +0200492
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500493 wl_list_for_each(launcher, &panel->launcher_list, link) {
-c505af82019-06-26 21:00:43 +0000494 widget_set_allocation(launcher->widget, x, y,
495 w + first_pad_w + 1, h + first_pad_h + 1);
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200496 if (horizontal)
-c505af82019-06-26 21:00:43 +0000497 x += w + first_pad_w;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200498 else
-c505af82019-06-26 21:00:43 +0000499 y += h + first_pad_h;
500 first_pad_h = first_pad_w = 0;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500501 }
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100502
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100503 if (panel->clock_format == CLOCK_FORMAT_SECONDS)
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100504 w = 170;
-c505af82019-06-26 21:00:43 +0000505 else /* CLOCK_FORMAT_MINUTES */
506 w = 150;
Pekka Paalanen01b17252012-06-12 17:42:26 +0300507
-c505af82019-06-26 21:00:43 +0000508 if (horizontal)
509 x = width - w;
510 else
511 y = height - (h = DEFAULT_SPACING * 3);
Quentin Glidic51c2c372016-06-23 18:55:23 +0200512
Pekka Paalanen01b17252012-06-12 17:42:26 +0300513 if (panel->clock)
514 widget_set_allocation(panel->clock->widget,
Quentin Glidic51c2c372016-06-23 18:55:23 +0200515 x, y, w + 1, h + 1);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500516}
517
518static void
Pekka Paalanen1cbfcf42017-12-07 12:39:15 +0200519panel_destroy(struct panel *panel);
520
521static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100522panel_configure(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800523 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400524 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100525 int32_t width, int32_t height)
526{
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200527 struct desktop *desktop = data;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500528 struct surface *surface = window_get_user_data(window);
529 struct panel *panel = container_of(surface, struct panel, base);
Pekka Paalanen1cbfcf42017-12-07 12:39:15 +0200530 struct output *owner;
531
532 if (width < 1 || height < 1) {
533 /* Shell plugin configures 0x0 for redundant panel. */
534 owner = panel->owner;
535 panel_destroy(panel);
536 owner->panel = NULL;
537 return;
538 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500539
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200540 switch (desktop->panel_position) {
541 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
542 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Quentin Glidic51c2c372016-06-23 18:55:23 +0200543 height = 32;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200544 break;
545 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
546 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
547 switch (desktop->clock_format) {
548 case CLOCK_FORMAT_NONE:
549 width = 32;
550 break;
551 case CLOCK_FORMAT_MINUTES:
-c505af82019-06-26 21:00:43 +0000552 width = 150;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200553 break;
554 case CLOCK_FORMAT_SECONDS:
-c505af82019-06-26 21:00:43 +0000555 width = 170;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200556 break;
557 }
558 break;
559 }
Quentin Glidic51c2c372016-06-23 18:55:23 +0200560 window_schedule_resize(panel->window, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100561}
562
U. Artie Eoff44874d92012-10-02 21:12:35 -0700563static void
564panel_destroy_launcher(struct panel_launcher *launcher)
565{
566 wl_array_release(&launcher->argv);
567 wl_array_release(&launcher->envp);
568
569 free(launcher->path);
570
571 cairo_surface_destroy(launcher->icon);
572
573 widget_destroy(launcher->widget);
574 wl_list_remove(&launcher->link);
575
576 free(launcher);
577}
578
579static void
580panel_destroy(struct panel *panel)
581{
582 struct panel_launcher *tmp;
583 struct panel_launcher *launcher;
584
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100585 if (panel->clock)
586 panel_destroy_clock(panel->clock);
U. Artie Eoff44874d92012-10-02 21:12:35 -0700587
588 wl_list_for_each_safe(launcher, tmp, &panel->launcher_list, link)
589 panel_destroy_launcher(launcher);
590
591 widget_destroy(panel->widget);
592 window_destroy(panel->window);
593
594 free(panel);
595}
596
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400597static struct panel *
Pekka Paalanen1cbfcf42017-12-07 12:39:15 +0200598panel_create(struct desktop *desktop, struct output *output)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400599{
600 struct panel *panel;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300601 struct weston_config_section *s;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400602
Peter Huttererf3d62272013-08-08 11:57:05 +1000603 panel = xzalloc(sizeof *panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400604
Pekka Paalanen1cbfcf42017-12-07 12:39:15 +0200605 panel->owner = output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100606 panel->base.configure = panel_configure;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300607 panel->window = window_create_custom(desktop->display);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500608 panel->widget = window_add_widget(panel->window, panel);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500609 wl_list_init(&panel->launcher_list);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400610
611 window_set_title(panel->window, "panel");
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400612 window_set_user_data(panel->window, panel);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500613
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500614 widget_set_redraw_handler(panel->widget, panel_redraw_handler);
615 widget_set_resize_handler(panel->widget, panel_resize_handler);
Michael Vetter2a18a522015-05-15 17:17:47 +0200616
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200617 panel->panel_position = desktop->panel_position;
Quentin Glidic3e37b342016-06-23 18:55:22 +0200618 panel->clock_format = desktop->clock_format;
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100619 if (panel->clock_format != CLOCK_FORMAT_NONE)
620 panel_add_clock(panel);
621
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300622 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
Bryce Harringtone776f2a2016-07-14 18:28:03 -0700623 weston_config_section_get_color(s, "panel-color",
624 &panel->color, 0xaa000000);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300625
626 panel_add_launchers(panel, desktop);
627
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400628 return panel;
629}
630
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200631static cairo_surface_t *
632load_icon_or_fallback(const char *icon)
633{
634 cairo_surface_t *surface = cairo_image_surface_create_from_png(icon);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200635 cairo_status_t status;
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200636 cairo_t *cr;
637
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200638 status = cairo_surface_status(surface);
639 if (status == CAIRO_STATUS_SUCCESS)
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200640 return surface;
641
642 cairo_surface_destroy(surface);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200643 fprintf(stderr, "ERROR loading icon from file '%s', error: '%s'\n",
644 icon, cairo_status_to_string(status));
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200645
646 /* draw fallback icon */
647 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
648 20, 20);
649 cr = cairo_create(surface);
650
651 cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1);
652 cairo_paint(cr);
653
654 cairo_set_source_rgba(cr, 0, 0, 0, 1);
655 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
656 cairo_rectangle(cr, 0, 0, 20, 20);
657 cairo_move_to(cr, 4, 4);
658 cairo_line_to(cr, 16, 16);
659 cairo_move_to(cr, 4, 16);
660 cairo_line_to(cr, 16, 4);
661 cairo_stroke(cr);
662
663 cairo_destroy(cr);
664
665 return surface;
666}
667
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400668static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500669panel_add_launcher(struct panel *panel, const char *icon, const char *path)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400670{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500671 struct panel_launcher *launcher;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400672 char *start, *p, *eq, **ps;
673 int i, j, k;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400674
Peter Huttererf3d62272013-08-08 11:57:05 +1000675 launcher = xzalloc(sizeof *launcher);
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200676 launcher->icon = load_icon_or_fallback(icon);
U. Artie Eoff3c946772014-01-15 10:59:50 -0800677 launcher->path = xstrdup(path);
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400678
679 wl_array_init(&launcher->envp);
680 wl_array_init(&launcher->argv);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300681 for (i = 0; environ[i]; i++) {
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400682 ps = wl_array_add(&launcher->envp, sizeof *ps);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300683 *ps = environ[i];
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400684 }
685 j = 0;
686
687 start = launcher->path;
688 while (*start) {
689 for (p = start, eq = NULL; *p && !isspace(*p); p++)
690 if (*p == '=')
691 eq = p;
692
693 if (eq && j == 0) {
694 ps = launcher->envp.data;
695 for (k = 0; k < i; k++)
696 if (strncmp(ps[k], start, eq - start) == 0) {
697 ps[k] = start;
698 break;
699 }
700 if (k == i) {
701 ps = wl_array_add(&launcher->envp, sizeof *ps);
702 *ps = start;
703 i++;
704 }
705 } else {
706 ps = wl_array_add(&launcher->argv, sizeof *ps);
707 *ps = start;
708 j++;
709 }
710
711 while (*p && isspace(*p))
712 *p++ = '\0';
713
714 start = p;
715 }
716
717 ps = wl_array_add(&launcher->envp, sizeof *ps);
718 *ps = NULL;
719 ps = wl_array_add(&launcher->argv, sizeof *ps);
720 *ps = NULL;
721
Kristian Høgsberg53880802012-01-09 11:16:50 -0500722 launcher->panel = panel;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500723 wl_list_insert(panel->launcher_list.prev, &launcher->link);
724
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500725 launcher->widget = widget_add_widget(panel->widget, launcher);
Kristian Høgsberg53880802012-01-09 11:16:50 -0500726 widget_set_enter_handler(launcher->widget,
727 panel_launcher_enter_handler);
728 widget_set_leave_handler(launcher->widget,
729 panel_launcher_leave_handler);
730 widget_set_button_handler(launcher->widget,
731 panel_launcher_button_handler);
Rusty Lynch4384a242013-08-08 21:28:22 -0700732 widget_set_touch_down_handler(launcher->widget,
733 panel_launcher_touch_down_handler);
734 widget_set_touch_up_handler(launcher->widget,
735 panel_launcher_touch_up_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500736 widget_set_redraw_handler(launcher->widget,
737 panel_launcher_redraw_handler);
Tiago Vignatti61500722012-05-23 22:06:28 +0300738 widget_set_motion_handler(launcher->widget,
739 panel_launcher_motion_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400740}
741
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500742enum {
743 BACKGROUND_SCALE,
Pekka Paalanena402b052013-05-22 18:03:10 +0300744 BACKGROUND_SCALE_CROP,
Stefan Agner20b24162018-08-22 23:56:07 +0200745 BACKGROUND_TILE,
746 BACKGROUND_CENTERED
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500747};
748
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400749static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500750background_draw(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400751{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500752 struct background *background = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400753 cairo_surface_t *surface, *image;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400754 cairo_pattern_t *pattern;
755 cairo_matrix_t matrix;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400756 cairo_t *cr;
Pekka Paalanena402b052013-05-22 18:03:10 +0300757 double im_w, im_h;
758 double sx, sy, s;
759 double tx, ty;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500760 struct rectangle allocation;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400761
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500762 surface = window_get_surface(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400763
Alexander Larssonc584fa62013-05-22 14:41:32 +0200764 cr = widget_cairo_create(background->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400765 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Stefan Agner1c1e4fd2018-08-22 23:33:10 +0200766 if (background->color == 0)
767 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
768 else
769 set_hex_color(cr, background->color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400770 cairo_paint(cr);
771
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500772 widget_get_allocation(widget, &allocation);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500773 image = NULL;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300774 if (background->image)
775 image = load_cairo_surface(background->image);
Derek Foremane2772762018-02-06 15:18:38 -0600776 else if (background->color == 0) {
777 char *name = file_name_with_datadir("pattern.png");
778
779 image = load_cairo_surface(name);
780 free(name);
781 }
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500782
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300783 if (image && background->type != -1) {
Pekka Paalanena402b052013-05-22 18:03:10 +0300784 im_w = cairo_image_surface_get_width(image);
785 im_h = cairo_image_surface_get_height(image);
786 sx = im_w / allocation.width;
787 sy = im_h / allocation.height;
788
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400789 pattern = cairo_pattern_create_for_surface(image);
Pekka Paalanena402b052013-05-22 18:03:10 +0300790
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300791 switch (background->type) {
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500792 case BACKGROUND_SCALE:
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500793 cairo_matrix_init_scale(&matrix, sx, sy);
794 cairo_pattern_set_matrix(pattern, &matrix);
Bill Spitzak79b7cb32014-05-08 20:00:35 -0700795 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500796 break;
Pekka Paalanena402b052013-05-22 18:03:10 +0300797 case BACKGROUND_SCALE_CROP:
798 s = (sx < sy) ? sx : sy;
799 /* align center */
800 tx = (im_w - s * allocation.width) * 0.5;
801 ty = (im_h - s * allocation.height) * 0.5;
802 cairo_matrix_init_translate(&matrix, tx, ty);
803 cairo_matrix_scale(&matrix, s, s);
804 cairo_pattern_set_matrix(pattern, &matrix);
Bill Spitzak79b7cb32014-05-08 20:00:35 -0700805 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
Pekka Paalanena402b052013-05-22 18:03:10 +0300806 break;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500807 case BACKGROUND_TILE:
808 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
809 break;
Stefan Agner20b24162018-08-22 23:56:07 +0200810 case BACKGROUND_CENTERED:
811 s = (sx < sy) ? sx : sy;
812 if (s < 1.0)
813 s = 1.0;
814
815 /* align center */
816 tx = (im_w - s * allocation.width) * 0.5;
817 ty = (im_h - s * allocation.height) * 0.5;
818
819 cairo_matrix_init_translate(&matrix, tx, ty);
820 cairo_matrix_scale(&matrix, s, s);
821 cairo_pattern_set_matrix(pattern, &matrix);
822 break;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500823 }
Pekka Paalanena402b052013-05-22 18:03:10 +0300824
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400825 cairo_set_source(cr, pattern);
826 cairo_pattern_destroy (pattern);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400827 cairo_surface_destroy(image);
Stefan Agner20b24162018-08-22 23:56:07 +0200828 cairo_mask(cr, pattern);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400829 }
830
831 cairo_destroy(cr);
832 cairo_surface_destroy(surface);
Pekka Paalanen9564c752012-10-24 09:43:08 +0300833
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300834 background->painted = 1;
835 check_desktop_ready(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400836}
837
838static void
Pekka Paalanencb115382017-12-07 12:15:01 +0200839background_destroy(struct background *background);
840
841static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100842background_configure(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800843 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400844 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100845 int32_t width, int32_t height)
846{
Pekka Paalanencb115382017-12-07 12:15:01 +0200847 struct output *owner;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500848 struct background *background =
849 (struct background *) window_get_user_data(window);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100850
Pekka Paalanencb115382017-12-07 12:15:01 +0200851 if (width < 1 || height < 1) {
852 /* Shell plugin configures 0x0 for redundant background. */
853 owner = background->owner;
854 background_destroy(background);
855 owner->background = NULL;
856 return;
857 }
858
Tanmay Shah8ef3ce52020-10-16 06:05:45 +0000859 if (!background->image && background->color) {
Harish Krupo3623e462018-12-11 13:59:07 +0530860 widget_set_viewport_destination(background->widget, width, height);
861 width = 1;
862 height = 1;
863 }
864
Kristian Høgsberg4598f962014-01-01 16:32:09 -0800865 widget_schedule_resize(background->widget, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100866}
867
868static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500869unlock_dialog_redraw_handler(struct widget *widget, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200870{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500871 struct unlock_dialog *dialog = data;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200872 struct rectangle allocation;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200873 cairo_surface_t *surface;
Alexander Larssonc584fa62013-05-22 14:41:32 +0200874 cairo_t *cr;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200875 cairo_pattern_t *pat;
876 double cx, cy, r, f;
877
Alexander Larssonc584fa62013-05-22 14:41:32 +0200878 cr = widget_cairo_create(widget);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500879
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500880 widget_get_allocation(dialog->widget, &allocation);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200881 cairo_rectangle(cr, allocation.x, allocation.y,
882 allocation.width, allocation.height);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200883 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
884 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500885 cairo_fill(cr);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200886
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500887 cairo_translate(cr, allocation.x, allocation.y);
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500888 if (dialog->button_focused)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200889 f = 1.0;
890 else
891 f = 0.7;
892
893 cx = allocation.width / 2.0;
894 cy = allocation.height / 2.0;
895 r = (cx < cy ? cx : cy) * 0.4;
896 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
897 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
898 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
899 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
900 cairo_set_source(cr, pat);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500901 cairo_pattern_destroy(pat);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200902 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
903 cairo_fill(cr);
904
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500905 widget_set_allocation(dialog->button,
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500906 allocation.x + cx - r,
907 allocation.y + cy - r, 2 * r, 2 * r);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200908
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200909 cairo_destroy(cr);
910
Alexander Larssonc584fa62013-05-22 14:41:32 +0200911 surface = window_get_surface(dialog->window);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200912 cairo_surface_destroy(surface);
913}
914
915static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500916unlock_dialog_button_handler(struct widget *widget,
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200917 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100918 uint32_t button,
919 enum wl_pointer_button_state state, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200920{
921 struct unlock_dialog *dialog = data;
922 struct desktop *desktop = dialog->desktop;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200923
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500924 if (button == BTN_LEFT) {
Daniel Stone4dbadb12012-05-30 16:31:51 +0100925 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
926 !dialog->closing) {
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200927 display_defer(desktop->display, &desktop->unlock_task);
928 dialog->closing = 1;
929 }
930 }
931}
932
933static void
Brian J Lovinc4df4082013-08-26 15:58:22 -0700934unlock_dialog_touch_down_handler(struct widget *widget, struct input *input,
935 uint32_t serial, uint32_t time, int32_t id,
936 float x, float y, void *data)
937{
938 struct unlock_dialog *dialog = data;
939
940 dialog->button_focused = 1;
941 widget_schedule_redraw(widget);
942}
943
944static void
945unlock_dialog_touch_up_handler(struct widget *widget, struct input *input,
946 uint32_t serial, uint32_t time, int32_t id,
947 void *data)
948{
949 struct unlock_dialog *dialog = data;
950 struct desktop *desktop = dialog->desktop;
951
952 dialog->button_focused = 0;
953 widget_schedule_redraw(widget);
954 display_defer(desktop->display, &desktop->unlock_task);
955 dialog->closing = 1;
956}
957
958static void
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200959unlock_dialog_keyboard_focus_handler(struct window *window,
960 struct input *device, void *data)
961{
962 window_schedule_redraw(window);
963}
964
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500965static int
Kristian Høgsbergee143232012-01-09 08:42:24 -0500966unlock_dialog_widget_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400967 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400968 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500969{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500970 struct unlock_dialog *dialog = data;
971
972 dialog->button_focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500973 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500974
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300975 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500976}
977
978static void
979unlock_dialog_widget_leave_handler(struct widget *widget,
980 struct input *input, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200981{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500982 struct unlock_dialog *dialog = data;
983
984 dialog->button_focused = 0;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500985 widget_schedule_redraw(widget);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200986}
987
988static struct unlock_dialog *
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500989unlock_dialog_create(struct desktop *desktop)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200990{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500991 struct display *display = desktop->display;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200992 struct unlock_dialog *dialog;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800993 struct wl_surface *surface;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200994
Peter Huttererf3d62272013-08-08 11:57:05 +1000995 dialog = xzalloc(sizeof *dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200996
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400997 dialog->window = window_create_custom(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500998 dialog->widget = window_frame_create(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200999 window_set_title(dialog->window, "Unlock your desktop");
1000
1001 window_set_user_data(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001002 window_set_keyboard_focus_handler(dialog->window,
1003 unlock_dialog_keyboard_focus_handler);
Kristian Høgsberg441338c2012-01-10 13:52:34 -05001004 dialog->button = widget_add_widget(dialog->widget, dialog);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001005 widget_set_redraw_handler(dialog->widget,
1006 unlock_dialog_redraw_handler);
Kristian Høgsbergee143232012-01-09 08:42:24 -05001007 widget_set_enter_handler(dialog->button,
1008 unlock_dialog_widget_enter_handler);
1009 widget_set_leave_handler(dialog->button,
1010 unlock_dialog_widget_leave_handler);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001011 widget_set_button_handler(dialog->button,
1012 unlock_dialog_button_handler);
Brian J Lovinc4df4082013-08-26 15:58:22 -07001013 widget_set_touch_down_handler(dialog->button,
1014 unlock_dialog_touch_down_handler);
1015 widget_set_touch_up_handler(dialog->button,
1016 unlock_dialog_touch_up_handler);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001017
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001018 surface = window_get_wl_surface(dialog->window);
1019 weston_desktop_shell_set_lock_surface(desktop->shell, surface);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001020
Pekka Paalanen40e49ac2012-01-18 16:51:30 +02001021 window_schedule_resize(dialog->window, 260, 230);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001022
1023 return dialog;
1024}
1025
1026static void
1027unlock_dialog_destroy(struct unlock_dialog *dialog)
1028{
1029 window_destroy(dialog->window);
1030 free(dialog);
1031}
1032
1033static void
1034unlock_dialog_finish(struct task *task, uint32_t events)
1035{
1036 struct desktop *desktop =
Benjamin Franzked7759712011-11-22 12:38:48 +01001037 container_of(task, struct desktop, unlock_task);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001038
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001039 weston_desktop_shell_unlock(desktop->shell);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001040 unlock_dialog_destroy(desktop->unlock_dialog);
1041 desktop->unlock_dialog = NULL;
1042}
1043
1044static void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001045desktop_shell_configure(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001046 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001047 uint32_t edges,
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001048 struct wl_surface *surface,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001049 int32_t width, int32_t height)
1050{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001051 struct window *window = wl_surface_get_user_data(surface);
Pekka Paalanen068ae942011-11-28 14:11:15 +02001052 struct surface *s = window_get_user_data(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001053
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001054 s->configure(data, desktop_shell, edges, window, width, height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001055}
1056
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001057static void
1058desktop_shell_prepare_lock_surface(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001059 struct weston_desktop_shell *desktop_shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001060{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001061 struct desktop *desktop = data;
1062
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001063 if (!desktop->locking) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001064 weston_desktop_shell_unlock(desktop->shell);
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +02001065 return;
1066 }
1067
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001068 if (!desktop->unlock_dialog) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001069 desktop->unlock_dialog = unlock_dialog_create(desktop);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001070 desktop->unlock_dialog->desktop = desktop;
1071 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001072}
1073
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001074static void
1075desktop_shell_grab_cursor(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001076 struct weston_desktop_shell *desktop_shell,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001077 uint32_t cursor)
1078{
1079 struct desktop *desktop = data;
1080
1081 switch (cursor) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001082 case WESTON_DESKTOP_SHELL_CURSOR_NONE:
Philipp Brüschweiler16d59d72012-08-24 15:43:55 +02001083 desktop->grab_cursor = CURSOR_BLANK;
1084 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001085 case WESTON_DESKTOP_SHELL_CURSOR_BUSY:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001086 desktop->grab_cursor = CURSOR_WATCH;
1087 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001088 case WESTON_DESKTOP_SHELL_CURSOR_MOVE:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001089 desktop->grab_cursor = CURSOR_DRAGGING;
1090 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001091 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001092 desktop->grab_cursor = CURSOR_TOP;
1093 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001094 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001095 desktop->grab_cursor = CURSOR_BOTTOM;
1096 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001097 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_LEFT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001098 desktop->grab_cursor = CURSOR_LEFT;
1099 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001100 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_RIGHT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001101 desktop->grab_cursor = CURSOR_RIGHT;
1102 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001103 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP_LEFT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001104 desktop->grab_cursor = CURSOR_TOP_LEFT;
1105 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001106 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP_RIGHT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001107 desktop->grab_cursor = CURSOR_TOP_RIGHT;
1108 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001109 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_LEFT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001110 desktop->grab_cursor = CURSOR_BOTTOM_LEFT;
1111 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001112 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_RIGHT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001113 desktop->grab_cursor = CURSOR_BOTTOM_RIGHT;
1114 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001115 case WESTON_DESKTOP_SHELL_CURSOR_ARROW:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001116 default:
1117 desktop->grab_cursor = CURSOR_LEFT_PTR;
1118 }
1119}
1120
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001121static const struct weston_desktop_shell_listener listener = {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001122 desktop_shell_configure,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001123 desktop_shell_prepare_lock_surface,
1124 desktop_shell_grab_cursor
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001125};
1126
U. Artie Eoff44874d92012-10-02 21:12:35 -07001127static void
1128background_destroy(struct background *background)
1129{
1130 widget_destroy(background->widget);
1131 window_destroy(background->window);
1132
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001133 free(background->image);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001134 free(background);
1135}
1136
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001137static struct background *
Pekka Paalanencb115382017-12-07 12:15:01 +02001138background_create(struct desktop *desktop, struct output *output)
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001139{
1140 struct background *background;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001141 struct weston_config_section *s;
1142 char *type;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001143
Peter Huttererf3d62272013-08-08 11:57:05 +10001144 background = xzalloc(sizeof *background);
Pekka Paalanencb115382017-12-07 12:15:01 +02001145 background->owner = output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001146 background->base.configure = background_configure;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001147 background->window = window_create_custom(desktop->display);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001148 background->widget = window_add_widget(background->window, background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001149 window_set_user_data(background->window, background);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001150 widget_set_redraw_handler(background->widget, background_draw);
Arnaud Vracfb754a02014-08-25 20:56:49 +02001151 widget_set_transparent(background->widget, 0);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001152
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001153 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
1154 weston_config_section_get_string(s, "background-image",
Kristian Høgsberg4c3661f2014-04-21 22:54:37 -07001155 &background->image, NULL);
Bryce Harringtone776f2a2016-07-14 18:28:03 -07001156 weston_config_section_get_color(s, "background-color",
1157 &background->color, 0x00000000);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001158
1159 weston_config_section_get_string(s, "background-type",
1160 &type, "tile");
U. Artie Eoff3c946772014-01-15 10:59:50 -08001161 if (type == NULL) {
1162 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
1163 exit(EXIT_FAILURE);
1164 }
1165
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001166 if (strcmp(type, "scale") == 0) {
1167 background->type = BACKGROUND_SCALE;
1168 } else if (strcmp(type, "scale-crop") == 0) {
1169 background->type = BACKGROUND_SCALE_CROP;
1170 } else if (strcmp(type, "tile") == 0) {
1171 background->type = BACKGROUND_TILE;
Stefan Agner20b24162018-08-22 23:56:07 +02001172 } else if (strcmp(type, "centered") == 0) {
1173 background->type = BACKGROUND_CENTERED;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001174 } else {
1175 background->type = -1;
1176 fprintf(stderr, "invalid background-type: %s\n",
1177 type);
1178 }
1179
1180 free(type);
1181
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001182 return background;
1183}
1184
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001185static int
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001186grab_surface_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001187 float x, float y, void *data)
1188{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001189 struct desktop *desktop = data;
1190
1191 return desktop->grab_cursor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001192}
1193
1194static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001195grab_surface_destroy(struct desktop *desktop)
1196{
1197 widget_destroy(desktop->grab_widget);
1198 window_destroy(desktop->grab_window);
1199}
1200
1201static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001202grab_surface_create(struct desktop *desktop)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001203{
1204 struct wl_surface *s;
1205
Ander Conselvan de Oliveira07a91cd2012-07-16 14:15:50 +03001206 desktop->grab_window = window_create_custom(desktop->display);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001207 window_set_user_data(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001208
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001209 s = window_get_wl_surface(desktop->grab_window);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001210 weston_desktop_shell_set_grab_surface(desktop->shell, s);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001211
1212 desktop->grab_widget =
1213 window_add_widget(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001214 /* We set the allocation to 1x1 at 0,0 so the fake enter event
1215 * at 0,0 will go to this widget. */
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001216 widget_set_allocation(desktop->grab_widget, 0, 0, 1, 1);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001217
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001218 widget_set_enter_handler(desktop->grab_widget,
1219 grab_surface_enter_handler);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001220}
1221
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001222static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001223output_destroy(struct output *output)
1224{
Pekka Paalanencb115382017-12-07 12:15:01 +02001225 if (output->background)
1226 background_destroy(output->background);
Jonny Lambe67118c2014-08-12 15:07:51 +02001227 if (output->panel)
1228 panel_destroy(output->panel);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001229 wl_output_destroy(output->output);
1230 wl_list_remove(&output->link);
1231
1232 free(output);
1233}
1234
1235static void
1236desktop_destroy_outputs(struct desktop *desktop)
1237{
1238 struct output *tmp;
1239 struct output *output;
1240
1241 wl_list_for_each_safe(output, tmp, &desktop->outputs, link)
1242 output_destroy(output);
1243}
1244
1245static void
Alexander Larssonc584fa62013-05-22 14:41:32 +02001246output_handle_geometry(void *data,
1247 struct wl_output *wl_output,
1248 int x, int y,
1249 int physical_width,
1250 int physical_height,
1251 int subpixel,
1252 const char *make,
1253 const char *model,
1254 int transform)
1255{
1256 struct output *output = data;
1257
Pekka Paalanenc1bcce62017-12-07 15:30:18 +02001258 output->x = x;
1259 output->y = y;
1260
Jonny Lambe67118c2014-08-12 15:07:51 +02001261 if (output->panel)
1262 window_set_buffer_transform(output->panel->window, transform);
Pekka Paalanencb115382017-12-07 12:15:01 +02001263 if (output->background)
1264 window_set_buffer_transform(output->background->window, transform);
Alexander Larssonc584fa62013-05-22 14:41:32 +02001265}
1266
1267static void
1268output_handle_mode(void *data,
1269 struct wl_output *wl_output,
1270 uint32_t flags,
1271 int width,
1272 int height,
1273 int refresh)
1274{
1275}
1276
1277static void
1278output_handle_done(void *data,
1279 struct wl_output *wl_output)
1280{
1281}
1282
1283static void
1284output_handle_scale(void *data,
1285 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001286 int32_t scale)
Alexander Larssonc584fa62013-05-22 14:41:32 +02001287{
1288 struct output *output = data;
1289
Jonny Lambe67118c2014-08-12 15:07:51 +02001290 if (output->panel)
1291 window_set_buffer_scale(output->panel->window, scale);
Pekka Paalanencb115382017-12-07 12:15:01 +02001292 if (output->background)
1293 window_set_buffer_scale(output->background->window, scale);
Alexander Larssonc584fa62013-05-22 14:41:32 +02001294}
1295
1296static const struct wl_output_listener output_listener = {
1297 output_handle_geometry,
1298 output_handle_mode,
1299 output_handle_done,
1300 output_handle_scale
1301};
1302
1303static void
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001304output_init(struct output *output, struct desktop *desktop)
1305{
1306 struct wl_surface *surface;
1307
Quentin Glidic55d57012016-06-23 18:55:18 +02001308 if (desktop->want_panel) {
Pekka Paalanen1cbfcf42017-12-07 12:39:15 +02001309 output->panel = panel_create(desktop, output);
Jonny Lambe67118c2014-08-12 15:07:51 +02001310 surface = window_get_wl_surface(output->panel->window);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001311 weston_desktop_shell_set_panel(desktop->shell,
1312 output->output, surface);
Jonny Lambe67118c2014-08-12 15:07:51 +02001313 }
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001314
Pekka Paalanencb115382017-12-07 12:15:01 +02001315 output->background = background_create(desktop, output);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001316 surface = window_get_wl_surface(output->background->window);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001317 weston_desktop_shell_set_background(desktop->shell,
1318 output->output, surface);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001319}
1320
1321static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001322create_output(struct desktop *desktop, uint32_t id)
1323{
1324 struct output *output;
1325
Bryce Harrington0d1a6222016-02-11 16:42:49 -08001326 output = zalloc(sizeof *output);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001327 if (!output)
1328 return;
1329
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001330 output->output =
Alexander Larssonc584fa62013-05-22 14:41:32 +02001331 display_bind(desktop->display, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001332 output->server_output_id = id;
Alexander Larssonc584fa62013-05-22 14:41:32 +02001333
1334 wl_output_add_listener(output->output, &output_listener, output);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001335
1336 wl_list_insert(&desktop->outputs, &output->link);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001337
1338 /* On start up we may process an output global before the shell global
1339 * in which case we can't create the panel and background just yet */
1340 if (desktop->shell)
1341 output_init(output, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001342}
1343
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001344static void
Pekka Paalanenc1bcce62017-12-07 15:30:18 +02001345output_remove(struct desktop *desktop, struct output *output)
1346{
1347 struct output *cur;
1348 struct output *rep = NULL;
1349
1350 if (!output->background) {
1351 output_destroy(output);
1352 return;
1353 }
1354
1355 /* Find a wl_output that is a clone of the removed wl_output.
1356 * We don't want to leave the clone without a background or panel. */
1357 wl_list_for_each(cur, &desktop->outputs, link) {
1358 if (cur == output)
1359 continue;
1360
1361 /* XXX: Assumes size matches. */
1362 if (cur->x == output->x && cur->y == output->y) {
1363 rep = cur;
1364 break;
1365 }
1366 }
1367
1368 if (rep) {
Pekka Paalanen82dd54d2018-06-21 15:38:56 +03001369 /* If found and it does not already have a background or panel,
1370 * hand over the background and panel so they don't get
1371 * destroyed.
1372 *
1373 * We never create multiple backgrounds or panels for clones,
1374 * but if the compositor moves outputs, a pair of wl_outputs
1375 * might become "clones". This may happen temporarily when
1376 * an output is about to be removed and the rest are reflowed.
1377 * In this case it is correct to let the background/panel be
1378 * destroyed.
1379 */
Pekka Paalanenc1bcce62017-12-07 15:30:18 +02001380
Pekka Paalanen82dd54d2018-06-21 15:38:56 +03001381 if (!rep->background) {
1382 rep->background = output->background;
1383 output->background = NULL;
1384 rep->background->owner = rep;
1385 }
Pekka Paalanenc1bcce62017-12-07 15:30:18 +02001386
Pekka Paalanen82dd54d2018-06-21 15:38:56 +03001387 if (!rep->panel) {
1388 rep->panel = output->panel;
1389 output->panel = NULL;
1390 if (rep->panel)
1391 rep->panel->owner = rep;
1392 }
Pekka Paalanenc1bcce62017-12-07 15:30:18 +02001393 }
1394
1395 output_destroy(output);
1396}
1397
1398static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001399global_handler(struct display *display, uint32_t id,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001400 const char *interface, uint32_t version, void *data)
1401{
1402 struct desktop *desktop = data;
1403
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001404 if (!strcmp(interface, "weston_desktop_shell")) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001405 desktop->shell = display_bind(desktop->display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001406 id,
1407 &weston_desktop_shell_interface,
1408 1);
1409 weston_desktop_shell_add_listener(desktop->shell,
1410 &listener,
1411 desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001412 } else if (!strcmp(interface, "wl_output")) {
1413 create_output(desktop, id);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001414 }
1415}
1416
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001417static void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001418global_handler_remove(struct display *display, uint32_t id,
1419 const char *interface, uint32_t version, void *data)
1420{
1421 struct desktop *desktop = data;
1422 struct output *output;
1423
1424 if (!strcmp(interface, "wl_output")) {
1425 wl_list_for_each(output, &desktop->outputs, link) {
1426 if (output->server_output_id == id) {
Pekka Paalanenc1bcce62017-12-07 15:30:18 +02001427 output_remove(desktop, output);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001428 break;
1429 }
1430 }
1431 }
1432}
1433
1434static void
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001435panel_add_launchers(struct panel *panel, struct desktop *desktop)
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001436{
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001437 struct weston_config_section *s;
1438 char *icon, *path;
1439 const char *name;
1440 int count;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001441
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001442 count = 0;
1443 s = NULL;
1444 while (weston_config_next_section(desktop->config, &s, &name)) {
1445 if (strcmp(name, "launcher") != 0)
1446 continue;
1447
1448 weston_config_section_get_string(s, "icon", &icon, NULL);
1449 weston_config_section_get_string(s, "path", &path, NULL);
1450
1451 if (icon != NULL && path != NULL) {
1452 panel_add_launcher(panel, icon, path);
Rob Bradford09252d42013-07-26 16:29:45 +01001453 count++;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001454 } else {
1455 fprintf(stderr, "invalid launcher section\n");
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001456 }
1457
1458 free(icon);
1459 free(path);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001460 }
1461
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001462 if (count == 0) {
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02001463 char *name = file_name_with_datadir("terminal.png");
Derek Foremane2772762018-02-06 15:18:38 -06001464
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001465 /* add default launcher */
1466 panel_add_launcher(panel,
Derek Foremane2772762018-02-06 15:18:38 -06001467 name,
Rodney Lorrimar99ff01b2012-02-29 17:31:03 +01001468 BINDIR "/weston-terminal");
Derek Foremane2772762018-02-06 15:18:38 -06001469 free(name);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001470 }
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001471}
1472
Quentin Glidic55d57012016-06-23 18:55:18 +02001473static void
1474parse_panel_position(struct desktop *desktop, struct weston_config_section *s)
1475{
1476 char *position;
1477
Daniel Stone08cf24b2017-01-16 13:18:21 +00001478 desktop->want_panel = 1;
Quentin Glidic55d57012016-06-23 18:55:18 +02001479
Daniel Stone08cf24b2017-01-16 13:18:21 +00001480 weston_config_section_get_string(s, "panel-position", &position, "top");
1481 if (strcmp(position, "top") == 0) {
1482 desktop->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
1483 } else if (strcmp(position, "bottom") == 0) {
1484 desktop->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM;
1485 } else if (strcmp(position, "left") == 0) {
1486 desktop->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT;
1487 } else if (strcmp(position, "right") == 0) {
1488 desktop->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT;
1489 } else {
1490 /* 'none' is valid here */
1491 if (strcmp(position, "none") != 0)
1492 fprintf(stderr, "Wrong panel position: %s\n", position);
1493 desktop->want_panel = 0;
Quentin Glidic55d57012016-06-23 18:55:18 +02001494 }
Daniel Stone08cf24b2017-01-16 13:18:21 +00001495 free(position);
Quentin Glidic55d57012016-06-23 18:55:18 +02001496}
1497
Quentin Glidic3e37b342016-06-23 18:55:22 +02001498static void
1499parse_clock_format(struct desktop *desktop, struct weston_config_section *s)
1500{
1501 char *clock_format;
1502
1503 weston_config_section_get_string(s, "clock-format", &clock_format, "");
1504 if (strcmp(clock_format, "minutes") == 0)
1505 desktop->clock_format = CLOCK_FORMAT_MINUTES;
1506 else if (strcmp(clock_format, "seconds") == 0)
1507 desktop->clock_format = CLOCK_FORMAT_SECONDS;
1508 else if (strcmp(clock_format, "none") == 0)
1509 desktop->clock_format = CLOCK_FORMAT_NONE;
1510 else
1511 desktop->clock_format = DEFAULT_CLOCK_FORMAT;
1512 free(clock_format);
1513}
1514
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001515int main(int argc, char *argv[])
1516{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001517 struct desktop desktop = { 0 };
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001518 struct output *output;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001519 struct weston_config_section *s;
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001520 const char *config_file;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001521
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001522 desktop.unlock_task.run = unlock_dialog_finish;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001523 wl_list_init(&desktop.outputs);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001524
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001525 config_file = weston_config_get_name_from_env();
1526 desktop.config = weston_config_parse(config_file);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001527 s = weston_config_get_section(desktop.config, "shell", NULL, NULL);
Daniel Stone51d995a2019-11-26 00:14:24 +00001528 weston_config_section_get_bool(s, "locking", &desktop.locking, true);
Quentin Glidic55d57012016-06-23 18:55:18 +02001529 parse_panel_position(&desktop, s);
Quentin Glidic3e37b342016-06-23 18:55:22 +02001530 parse_clock_format(&desktop, s);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001531
Kristian Høgsberg4172f662013-02-20 15:27:49 -05001532 desktop.display = display_create(&argc, argv);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001533 if (desktop.display == NULL) {
Antonio Borneo39578632019-04-26 23:57:31 +02001534 fprintf(stderr, "failed to create display: %s\n",
1535 strerror(errno));
Anurup Mc640cb62020-11-25 06:35:40 +05301536 weston_config_destroy(desktop.config);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001537 return -1;
1538 }
1539
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001540 display_set_user_data(desktop.display, &desktop);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001541 display_set_global_handler(desktop.display, global_handler);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001542 display_set_global_handler_remove(desktop.display, global_handler_remove);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001543
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001544 /* Create panel and background for outputs processed before the shell
1545 * global interface was processed */
Quentin Glidic55d57012016-06-23 18:55:18 +02001546 if (desktop.want_panel)
1547 weston_desktop_shell_set_panel_position(desktop.shell, desktop.panel_position);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001548 wl_list_for_each(output, &desktop.outputs, link)
1549 if (!output->panel)
1550 output_init(output, &desktop);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001551
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001552 grab_surface_create(&desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001553
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001554 signal(SIGCHLD, sigchild_handler);
1555
1556 display_run(desktop.display);
1557
U. Artie Eoff44874d92012-10-02 21:12:35 -07001558 /* Cleanup */
1559 grab_surface_destroy(&desktop);
1560 desktop_destroy_outputs(&desktop);
1561 if (desktop.unlock_dialog)
1562 unlock_dialog_destroy(desktop.unlock_dialog);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001563 weston_desktop_shell_destroy(desktop.shell);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001564 display_destroy(desktop.display);
Anurup Mc640cb62020-11-25 06:35:40 +05301565 weston_config_destroy(desktop.config);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001566
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001567 return 0;
1568}