blob: 8114491df82fe3d239138dbb583436fae8410fc5 [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"
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) {
Antonio Borneo39578632019-04-26 23:57:31 +0200215 fprintf(stderr, "fork failed: %s\n", strerror(errno));
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400216 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) {
Antonio Borneo39578632019-04-26 23:57:31 +0200228 fprintf(stderr, "execl '%s' failed: %s\n", argv[0],
229 strerror(errno));
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400230 exit(1);
231 }
232}
233
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400234static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500235panel_launcher_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400236{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500237 struct panel_launcher *launcher = data;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500238 struct rectangle allocation;
239 cairo_t *cr;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400240
Alexander Larssonc584fa62013-05-22 14:41:32 +0200241 cr = widget_cairo_create(launcher->panel->widget);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500242
243 widget_get_allocation(widget, &allocation);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500244 if (launcher->pressed) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500245 allocation.x++;
246 allocation.y++;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400247 }
248
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500249 cairo_set_source_surface(cr, launcher->icon,
250 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400251 cairo_paint(cr);
252
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500253 if (launcher->focused) {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400254 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500255 cairo_mask_surface(cr, launcher->icon,
256 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400257 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400258
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500259 cairo_destroy(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400260}
261
Tiago Vignatti61500722012-05-23 22:06:28 +0300262static int
263panel_launcher_motion_handler(struct widget *widget, struct input *input,
264 uint32_t time, float x, float y, void *data)
265{
266 struct panel_launcher *launcher = data;
267
268 widget_set_tooltip(widget, basename((char *)launcher->path), x, y);
269
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300270 return CURSOR_LEFT_PTR;
Tiago Vignatti61500722012-05-23 22:06:28 +0300271}
272
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400273static void
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500274set_hex_color(cairo_t *cr, uint32_t color)
275{
Michael Vetter2a18a522015-05-15 17:17:47 +0200276 cairo_set_source_rgba(cr,
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500277 ((color >> 16) & 0xff) / 255.0,
278 ((color >> 8) & 0xff) / 255.0,
279 ((color >> 0) & 0xff) / 255.0,
280 ((color >> 24) & 0xff) / 255.0);
281}
282
283static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500284panel_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400285{
286 cairo_surface_t *surface;
287 cairo_t *cr;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500288 struct panel *panel = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400289
Alexander Larssonc584fa62013-05-22 14:41:32 +0200290 cr = widget_cairo_create(panel->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400291 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300292 set_hex_color(cr, panel->color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400293 cairo_paint(cr);
294
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400295 cairo_destroy(cr);
Alexander Larssonc584fa62013-05-22 14:41:32 +0200296 surface = window_get_surface(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400297 cairo_surface_destroy(surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300298 panel->painted = 1;
299 check_desktop_ready(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400300}
301
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500302static int
Kristian Høgsberg53880802012-01-09 11:16:50 -0500303panel_launcher_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400304 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500305{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500306 struct panel_launcher *launcher = data;
307
308 launcher->focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500309 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500310
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300311 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500312}
313
314static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500315panel_launcher_leave_handler(struct widget *widget,
316 struct input *input, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400317{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500318 struct panel_launcher *launcher = data;
319
320 launcher->focused = 0;
Tiago Vignatti61500722012-05-23 22:06:28 +0300321 widget_destroy_tooltip(widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500322 widget_schedule_redraw(widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400323}
324
325static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500326panel_launcher_button_handler(struct widget *widget,
327 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100328 uint32_t button,
329 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400330{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500331 struct panel_launcher *launcher;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400332
Kristian Høgsberg53880802012-01-09 11:16:50 -0500333 launcher = widget_get_user_data(widget);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500334 widget_schedule_redraw(widget);
Daniel Stone4dbadb12012-05-30 16:31:51 +0100335 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg53880802012-01-09 11:16:50 -0500336 panel_launcher_activate(launcher);
Rusty Lynch4384a242013-08-08 21:28:22 -0700337
338}
339
340static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700341panel_launcher_touch_down_handler(struct widget *widget, struct input *input,
342 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch4384a242013-08-08 21:28:22 -0700343 float x, float y, void *data)
344{
345 struct panel_launcher *launcher;
346
347 launcher = widget_get_user_data(widget);
348 launcher->focused = 1;
349 widget_schedule_redraw(widget);
350}
351
352static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700353panel_launcher_touch_up_handler(struct widget *widget, struct input *input,
Michael Vetter2a18a522015-05-15 17:17:47 +0200354 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch1084da52013-08-15 09:10:08 -0700355 void *data)
Rusty Lynch4384a242013-08-08 21:28:22 -0700356{
357 struct panel_launcher *launcher;
358
359 launcher = widget_get_user_data(widget);
360 launcher->focused = 0;
361 widget_schedule_redraw(widget);
362 panel_launcher_activate(launcher);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500363}
364
Martin Minarik1e51a872012-06-08 00:39:11 +0200365static void
Pekka Paalanen64a26bc2018-03-09 13:17:26 +0200366clock_func(struct toytimer *tt)
Martin Minarik1e51a872012-06-08 00:39:11 +0200367{
Pekka Paalanen64a26bc2018-03-09 13:17:26 +0200368 struct panel_clock *clock = container_of(tt, struct panel_clock, timer);
Martin Minarik1e51a872012-06-08 00:39:11 +0200369
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400370 widget_schedule_redraw(clock->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200371}
372
373static void
374panel_clock_redraw_handler(struct widget *widget, void *data)
375{
Martin Minarik1e51a872012-06-08 00:39:11 +0200376 struct panel_clock *clock = data;
377 cairo_t *cr;
378 struct rectangle allocation;
379 cairo_text_extents_t extents;
380 cairo_font_extents_t font_extents;
Martin Minarik1e51a872012-06-08 00:39:11 +0200381 time_t rawtime;
382 struct tm * timeinfo;
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400383 char string[128];
Martin Minarik1e51a872012-06-08 00:39:11 +0200384
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400385 time(&rawtime);
386 timeinfo = localtime(&rawtime);
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100387 strftime(string, sizeof string, clock->format_string, timeinfo);
Martin Minarik1e51a872012-06-08 00:39:11 +0200388
389 widget_get_allocation(widget, &allocation);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400390 if (allocation.width == 0)
391 return;
Martin Minarik1e51a872012-06-08 00:39:11 +0200392
Alexander Larssonc584fa62013-05-22 14:41:32 +0200393 cr = widget_cairo_create(clock->panel->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200394 cairo_select_font_face(cr, "sans",
395 CAIRO_FONT_SLANT_NORMAL,
396 CAIRO_FONT_WEIGHT_NORMAL);
397 cairo_set_font_size(cr, 14);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400398 cairo_text_extents(cr, string, &extents);
Martin Minarik1e51a872012-06-08 00:39:11 +0200399 cairo_font_extents (cr, &font_extents);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400400 cairo_move_to(cr, allocation.x + 5,
401 allocation.y + 3 * (allocation.height >> 2) + 1);
402 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400403 cairo_show_text(cr, string);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400404 cairo_move_to(cr, allocation.x + 4,
405 allocation.y + 3 * (allocation.height >> 2));
406 cairo_set_source_rgb(cr, 1, 1, 1);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400407 cairo_show_text(cr, string);
Martin Minarik1e51a872012-06-08 00:39:11 +0200408 cairo_destroy(cr);
409}
410
411static int
412clock_timer_reset(struct panel_clock *clock)
413{
414 struct itimerspec its;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400415
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100416 its.it_interval.tv_sec = clock->refresh_timer;
Martin Minarik1e51a872012-06-08 00:39:11 +0200417 its.it_interval.tv_nsec = 0;
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100418 its.it_value.tv_sec = clock->refresh_timer;
Martin Minarik1e51a872012-06-08 00:39:11 +0200419 its.it_value.tv_nsec = 0;
Pekka Paalanen64a26bc2018-03-09 13:17:26 +0200420 toytimer_arm(&clock->timer, &its);
Martin Minarik1e51a872012-06-08 00:39:11 +0200421
422 return 0;
423}
424
425static void
U. Artie Eoff44874d92012-10-02 21:12:35 -0700426panel_destroy_clock(struct panel_clock *clock)
427{
428 widget_destroy(clock->widget);
Pekka Paalanen64a26bc2018-03-09 13:17:26 +0200429 toytimer_fini(&clock->timer);
U. Artie Eoff44874d92012-10-02 21:12:35 -0700430 free(clock);
431}
432
433static void
Martin Minarik1e51a872012-06-08 00:39:11 +0200434panel_add_clock(struct panel *panel)
435{
436 struct panel_clock *clock;
Martin Minarik1e51a872012-06-08 00:39:11 +0200437
Peter Huttererf3d62272013-08-08 11:57:05 +1000438 clock = xzalloc(sizeof *clock);
Martin Minarik1e51a872012-06-08 00:39:11 +0200439 clock->panel = panel;
440 panel->clock = clock;
Martin Minarik1e51a872012-06-08 00:39:11 +0200441
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100442 switch (panel->clock_format) {
443 case CLOCK_FORMAT_MINUTES:
444 clock->format_string = "%a %b %d, %I:%M %p";
445 clock->refresh_timer = 60;
446 break;
447 case CLOCK_FORMAT_SECONDS:
448 clock->format_string = "%a %b %d, %I:%M:%S %p";
449 clock->refresh_timer = 1;
450 break;
Quentin Glidicf9574f22016-06-23 18:55:21 +0200451 case CLOCK_FORMAT_NONE:
452 assert(!"not reached");
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100453 }
454
Pekka Paalanen64a26bc2018-03-09 13:17:26 +0200455 toytimer_init(&clock->timer, CLOCK_MONOTONIC,
456 window_get_display(panel->window), clock_func);
Martin Minarik1e51a872012-06-08 00:39:11 +0200457 clock_timer_reset(clock);
458
459 clock->widget = widget_add_widget(panel->widget, clock);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400460 widget_set_redraw_handler(clock->widget, panel_clock_redraw_handler);
Martin Minarik1e51a872012-06-08 00:39:11 +0200461}
462
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500463static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500464panel_resize_handler(struct widget *widget,
465 int32_t width, int32_t height, void *data)
466{
467 struct panel_launcher *launcher;
468 struct panel *panel = data;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200469 int bx = width / 2;
Quentin Glidic51c2c372016-06-23 18:55:23 +0200470 int by = height / 2;
471 int spacing = 10;
472 int x = spacing;
473 int y = spacing;
474 int w, h;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200475 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 +0200476
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500477 wl_list_for_each(launcher, &panel->launcher_list, link) {
478 w = cairo_image_surface_get_width(launcher->icon);
479 h = cairo_image_surface_get_height(launcher->icon);
Quentin Glidic51c2c372016-06-23 18:55:23 +0200480
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200481 if (horizontal)
Quentin Glidic51c2c372016-06-23 18:55:23 +0200482 y = by - h / 2;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200483 else
484 x = bx - w / 2;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500485 widget_set_allocation(launcher->widget,
Quentin Glidic51c2c372016-06-23 18:55:23 +0200486 x, y, w + 1, h + 1);
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200487 if (horizontal)
Quentin Glidic51c2c372016-06-23 18:55:23 +0200488 x += w + spacing;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200489 else
490 y += h + spacing;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500491 }
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100492
493 h = 20;
494
495 if (panel->clock_format == CLOCK_FORMAT_SECONDS)
496 w = 190;
497 else /* CLOCK_FORMAT_MINUTES */
498 w = 170;
Pekka Paalanen01b17252012-06-12 17:42:26 +0300499
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200500 if (horizontal) {
Quentin Glidic51c2c372016-06-23 18:55:23 +0200501 x = width - w - spacing;
502 y = by - h / 2;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200503 } else {
504 x = bx - w / 2;
505 y = height - h - spacing;
506 }
Quentin Glidic51c2c372016-06-23 18:55:23 +0200507
Pekka Paalanen01b17252012-06-12 17:42:26 +0300508 if (panel->clock)
509 widget_set_allocation(panel->clock->widget,
Quentin Glidic51c2c372016-06-23 18:55:23 +0200510 x, y, w + 1, h + 1);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500511}
512
513static void
Pekka Paalanen1cbfcf42017-12-07 12:39:15 +0200514panel_destroy(struct panel *panel);
515
516static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100517panel_configure(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800518 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400519 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100520 int32_t width, int32_t height)
521{
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200522 struct desktop *desktop = data;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500523 struct surface *surface = window_get_user_data(window);
524 struct panel *panel = container_of(surface, struct panel, base);
Pekka Paalanen1cbfcf42017-12-07 12:39:15 +0200525 struct output *owner;
526
527 if (width < 1 || height < 1) {
528 /* Shell plugin configures 0x0 for redundant panel. */
529 owner = panel->owner;
530 panel_destroy(panel);
531 owner->panel = NULL;
532 return;
533 }
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500534
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200535 switch (desktop->panel_position) {
536 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
537 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Quentin Glidic51c2c372016-06-23 18:55:23 +0200538 height = 32;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200539 break;
540 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
541 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
542 switch (desktop->clock_format) {
543 case CLOCK_FORMAT_NONE:
544 width = 32;
545 break;
546 case CLOCK_FORMAT_MINUTES:
547 width = 170;
548 break;
549 case CLOCK_FORMAT_SECONDS:
550 width = 190;
551 break;
552 }
553 break;
554 }
Quentin Glidic51c2c372016-06-23 18:55:23 +0200555 window_schedule_resize(panel->window, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100556}
557
U. Artie Eoff44874d92012-10-02 21:12:35 -0700558static void
559panel_destroy_launcher(struct panel_launcher *launcher)
560{
561 wl_array_release(&launcher->argv);
562 wl_array_release(&launcher->envp);
563
564 free(launcher->path);
565
566 cairo_surface_destroy(launcher->icon);
567
568 widget_destroy(launcher->widget);
569 wl_list_remove(&launcher->link);
570
571 free(launcher);
572}
573
574static void
575panel_destroy(struct panel *panel)
576{
577 struct panel_launcher *tmp;
578 struct panel_launcher *launcher;
579
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100580 if (panel->clock)
581 panel_destroy_clock(panel->clock);
U. Artie Eoff44874d92012-10-02 21:12:35 -0700582
583 wl_list_for_each_safe(launcher, tmp, &panel->launcher_list, link)
584 panel_destroy_launcher(launcher);
585
586 widget_destroy(panel->widget);
587 window_destroy(panel->window);
588
589 free(panel);
590}
591
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400592static struct panel *
Pekka Paalanen1cbfcf42017-12-07 12:39:15 +0200593panel_create(struct desktop *desktop, struct output *output)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400594{
595 struct panel *panel;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300596 struct weston_config_section *s;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400597
Peter Huttererf3d62272013-08-08 11:57:05 +1000598 panel = xzalloc(sizeof *panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400599
Pekka Paalanen1cbfcf42017-12-07 12:39:15 +0200600 panel->owner = output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100601 panel->base.configure = panel_configure;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300602 panel->window = window_create_custom(desktop->display);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500603 panel->widget = window_add_widget(panel->window, panel);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500604 wl_list_init(&panel->launcher_list);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400605
606 window_set_title(panel->window, "panel");
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400607 window_set_user_data(panel->window, panel);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500608
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500609 widget_set_redraw_handler(panel->widget, panel_redraw_handler);
610 widget_set_resize_handler(panel->widget, panel_resize_handler);
Michael Vetter2a18a522015-05-15 17:17:47 +0200611
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200612 panel->panel_position = desktop->panel_position;
Quentin Glidic3e37b342016-06-23 18:55:22 +0200613 panel->clock_format = desktop->clock_format;
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100614 if (panel->clock_format != CLOCK_FORMAT_NONE)
615 panel_add_clock(panel);
616
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300617 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
Bryce Harringtone776f2a2016-07-14 18:28:03 -0700618 weston_config_section_get_color(s, "panel-color",
619 &panel->color, 0xaa000000);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300620
621 panel_add_launchers(panel, desktop);
622
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400623 return panel;
624}
625
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200626static cairo_surface_t *
627load_icon_or_fallback(const char *icon)
628{
629 cairo_surface_t *surface = cairo_image_surface_create_from_png(icon);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200630 cairo_status_t status;
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200631 cairo_t *cr;
632
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200633 status = cairo_surface_status(surface);
634 if (status == CAIRO_STATUS_SUCCESS)
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200635 return surface;
636
637 cairo_surface_destroy(surface);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200638 fprintf(stderr, "ERROR loading icon from file '%s', error: '%s'\n",
639 icon, cairo_status_to_string(status));
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200640
641 /* draw fallback icon */
642 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
643 20, 20);
644 cr = cairo_create(surface);
645
646 cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1);
647 cairo_paint(cr);
648
649 cairo_set_source_rgba(cr, 0, 0, 0, 1);
650 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
651 cairo_rectangle(cr, 0, 0, 20, 20);
652 cairo_move_to(cr, 4, 4);
653 cairo_line_to(cr, 16, 16);
654 cairo_move_to(cr, 4, 16);
655 cairo_line_to(cr, 16, 4);
656 cairo_stroke(cr);
657
658 cairo_destroy(cr);
659
660 return surface;
661}
662
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400663static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500664panel_add_launcher(struct panel *panel, const char *icon, const char *path)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400665{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500666 struct panel_launcher *launcher;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400667 char *start, *p, *eq, **ps;
668 int i, j, k;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400669
Peter Huttererf3d62272013-08-08 11:57:05 +1000670 launcher = xzalloc(sizeof *launcher);
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200671 launcher->icon = load_icon_or_fallback(icon);
U. Artie Eoff3c946772014-01-15 10:59:50 -0800672 launcher->path = xstrdup(path);
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400673
674 wl_array_init(&launcher->envp);
675 wl_array_init(&launcher->argv);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300676 for (i = 0; environ[i]; i++) {
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400677 ps = wl_array_add(&launcher->envp, sizeof *ps);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300678 *ps = environ[i];
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400679 }
680 j = 0;
681
682 start = launcher->path;
683 while (*start) {
684 for (p = start, eq = NULL; *p && !isspace(*p); p++)
685 if (*p == '=')
686 eq = p;
687
688 if (eq && j == 0) {
689 ps = launcher->envp.data;
690 for (k = 0; k < i; k++)
691 if (strncmp(ps[k], start, eq - start) == 0) {
692 ps[k] = start;
693 break;
694 }
695 if (k == i) {
696 ps = wl_array_add(&launcher->envp, sizeof *ps);
697 *ps = start;
698 i++;
699 }
700 } else {
701 ps = wl_array_add(&launcher->argv, sizeof *ps);
702 *ps = start;
703 j++;
704 }
705
706 while (*p && isspace(*p))
707 *p++ = '\0';
708
709 start = p;
710 }
711
712 ps = wl_array_add(&launcher->envp, sizeof *ps);
713 *ps = NULL;
714 ps = wl_array_add(&launcher->argv, sizeof *ps);
715 *ps = NULL;
716
Kristian Høgsberg53880802012-01-09 11:16:50 -0500717 launcher->panel = panel;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500718 wl_list_insert(panel->launcher_list.prev, &launcher->link);
719
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500720 launcher->widget = widget_add_widget(panel->widget, launcher);
Kristian Høgsberg53880802012-01-09 11:16:50 -0500721 widget_set_enter_handler(launcher->widget,
722 panel_launcher_enter_handler);
723 widget_set_leave_handler(launcher->widget,
724 panel_launcher_leave_handler);
725 widget_set_button_handler(launcher->widget,
726 panel_launcher_button_handler);
Rusty Lynch4384a242013-08-08 21:28:22 -0700727 widget_set_touch_down_handler(launcher->widget,
728 panel_launcher_touch_down_handler);
729 widget_set_touch_up_handler(launcher->widget,
730 panel_launcher_touch_up_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500731 widget_set_redraw_handler(launcher->widget,
732 panel_launcher_redraw_handler);
Tiago Vignatti61500722012-05-23 22:06:28 +0300733 widget_set_motion_handler(launcher->widget,
734 panel_launcher_motion_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400735}
736
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500737enum {
738 BACKGROUND_SCALE,
Pekka Paalanena402b052013-05-22 18:03:10 +0300739 BACKGROUND_SCALE_CROP,
Stefan Agner20b24162018-08-22 23:56:07 +0200740 BACKGROUND_TILE,
741 BACKGROUND_CENTERED
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500742};
743
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400744static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500745background_draw(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400746{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500747 struct background *background = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400748 cairo_surface_t *surface, *image;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400749 cairo_pattern_t *pattern;
750 cairo_matrix_t matrix;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400751 cairo_t *cr;
Pekka Paalanena402b052013-05-22 18:03:10 +0300752 double im_w, im_h;
753 double sx, sy, s;
754 double tx, ty;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500755 struct rectangle allocation;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400756
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500757 surface = window_get_surface(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400758
Alexander Larssonc584fa62013-05-22 14:41:32 +0200759 cr = widget_cairo_create(background->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400760 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Stefan Agner1c1e4fd2018-08-22 23:33:10 +0200761 if (background->color == 0)
762 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
763 else
764 set_hex_color(cr, background->color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400765 cairo_paint(cr);
766
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500767 widget_get_allocation(widget, &allocation);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500768 image = NULL;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300769 if (background->image)
770 image = load_cairo_surface(background->image);
Derek Foremane2772762018-02-06 15:18:38 -0600771 else if (background->color == 0) {
772 char *name = file_name_with_datadir("pattern.png");
773
774 image = load_cairo_surface(name);
775 free(name);
776 }
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500777
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300778 if (image && background->type != -1) {
Pekka Paalanena402b052013-05-22 18:03:10 +0300779 im_w = cairo_image_surface_get_width(image);
780 im_h = cairo_image_surface_get_height(image);
781 sx = im_w / allocation.width;
782 sy = im_h / allocation.height;
783
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400784 pattern = cairo_pattern_create_for_surface(image);
Pekka Paalanena402b052013-05-22 18:03:10 +0300785
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300786 switch (background->type) {
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500787 case BACKGROUND_SCALE:
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500788 cairo_matrix_init_scale(&matrix, sx, sy);
789 cairo_pattern_set_matrix(pattern, &matrix);
Bill Spitzak79b7cb32014-05-08 20:00:35 -0700790 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500791 break;
Pekka Paalanena402b052013-05-22 18:03:10 +0300792 case BACKGROUND_SCALE_CROP:
793 s = (sx < sy) ? sx : sy;
794 /* align center */
795 tx = (im_w - s * allocation.width) * 0.5;
796 ty = (im_h - s * allocation.height) * 0.5;
797 cairo_matrix_init_translate(&matrix, tx, ty);
798 cairo_matrix_scale(&matrix, s, s);
799 cairo_pattern_set_matrix(pattern, &matrix);
Bill Spitzak79b7cb32014-05-08 20:00:35 -0700800 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
Pekka Paalanena402b052013-05-22 18:03:10 +0300801 break;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500802 case BACKGROUND_TILE:
803 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
804 break;
Stefan Agner20b24162018-08-22 23:56:07 +0200805 case BACKGROUND_CENTERED:
806 s = (sx < sy) ? sx : sy;
807 if (s < 1.0)
808 s = 1.0;
809
810 /* align center */
811 tx = (im_w - s * allocation.width) * 0.5;
812 ty = (im_h - s * allocation.height) * 0.5;
813
814 cairo_matrix_init_translate(&matrix, tx, ty);
815 cairo_matrix_scale(&matrix, s, s);
816 cairo_pattern_set_matrix(pattern, &matrix);
817 break;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500818 }
Pekka Paalanena402b052013-05-22 18:03:10 +0300819
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400820 cairo_set_source(cr, pattern);
821 cairo_pattern_destroy (pattern);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400822 cairo_surface_destroy(image);
Stefan Agner20b24162018-08-22 23:56:07 +0200823 cairo_mask(cr, pattern);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400824 }
825
826 cairo_destroy(cr);
827 cairo_surface_destroy(surface);
Pekka Paalanen9564c752012-10-24 09:43:08 +0300828
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300829 background->painted = 1;
830 check_desktop_ready(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400831}
832
833static void
Pekka Paalanencb115382017-12-07 12:15:01 +0200834background_destroy(struct background *background);
835
836static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100837background_configure(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800838 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400839 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100840 int32_t width, int32_t height)
841{
Pekka Paalanencb115382017-12-07 12:15:01 +0200842 struct output *owner;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500843 struct background *background =
844 (struct background *) window_get_user_data(window);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100845
Pekka Paalanencb115382017-12-07 12:15:01 +0200846 if (width < 1 || height < 1) {
847 /* Shell plugin configures 0x0 for redundant background. */
848 owner = background->owner;
849 background_destroy(background);
850 owner->background = NULL;
851 return;
852 }
853
Kristian Høgsberg4598f962014-01-01 16:32:09 -0800854 widget_schedule_resize(background->widget, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100855}
856
857static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500858unlock_dialog_redraw_handler(struct widget *widget, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200859{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500860 struct unlock_dialog *dialog = data;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200861 struct rectangle allocation;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200862 cairo_surface_t *surface;
Alexander Larssonc584fa62013-05-22 14:41:32 +0200863 cairo_t *cr;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200864 cairo_pattern_t *pat;
865 double cx, cy, r, f;
866
Alexander Larssonc584fa62013-05-22 14:41:32 +0200867 cr = widget_cairo_create(widget);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500868
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500869 widget_get_allocation(dialog->widget, &allocation);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200870 cairo_rectangle(cr, allocation.x, allocation.y,
871 allocation.width, allocation.height);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200872 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
873 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500874 cairo_fill(cr);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200875
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500876 cairo_translate(cr, allocation.x, allocation.y);
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500877 if (dialog->button_focused)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200878 f = 1.0;
879 else
880 f = 0.7;
881
882 cx = allocation.width / 2.0;
883 cy = allocation.height / 2.0;
884 r = (cx < cy ? cx : cy) * 0.4;
885 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
886 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
887 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
888 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
889 cairo_set_source(cr, pat);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500890 cairo_pattern_destroy(pat);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200891 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
892 cairo_fill(cr);
893
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500894 widget_set_allocation(dialog->button,
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500895 allocation.x + cx - r,
896 allocation.y + cy - r, 2 * r, 2 * r);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200897
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200898 cairo_destroy(cr);
899
Alexander Larssonc584fa62013-05-22 14:41:32 +0200900 surface = window_get_surface(dialog->window);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200901 cairo_surface_destroy(surface);
902}
903
904static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500905unlock_dialog_button_handler(struct widget *widget,
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200906 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100907 uint32_t button,
908 enum wl_pointer_button_state state, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200909{
910 struct unlock_dialog *dialog = data;
911 struct desktop *desktop = dialog->desktop;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200912
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500913 if (button == BTN_LEFT) {
Daniel Stone4dbadb12012-05-30 16:31:51 +0100914 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
915 !dialog->closing) {
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200916 display_defer(desktop->display, &desktop->unlock_task);
917 dialog->closing = 1;
918 }
919 }
920}
921
922static void
Brian J Lovinc4df4082013-08-26 15:58:22 -0700923unlock_dialog_touch_down_handler(struct widget *widget, struct input *input,
924 uint32_t serial, uint32_t time, int32_t id,
925 float x, float y, void *data)
926{
927 struct unlock_dialog *dialog = data;
928
929 dialog->button_focused = 1;
930 widget_schedule_redraw(widget);
931}
932
933static void
934unlock_dialog_touch_up_handler(struct widget *widget, struct input *input,
935 uint32_t serial, uint32_t time, int32_t id,
936 void *data)
937{
938 struct unlock_dialog *dialog = data;
939 struct desktop *desktop = dialog->desktop;
940
941 dialog->button_focused = 0;
942 widget_schedule_redraw(widget);
943 display_defer(desktop->display, &desktop->unlock_task);
944 dialog->closing = 1;
945}
946
947static void
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200948unlock_dialog_keyboard_focus_handler(struct window *window,
949 struct input *device, void *data)
950{
951 window_schedule_redraw(window);
952}
953
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500954static int
Kristian Høgsbergee143232012-01-09 08:42:24 -0500955unlock_dialog_widget_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400956 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400957 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500958{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500959 struct unlock_dialog *dialog = data;
960
961 dialog->button_focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500962 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500963
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300964 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500965}
966
967static void
968unlock_dialog_widget_leave_handler(struct widget *widget,
969 struct input *input, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200970{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500971 struct unlock_dialog *dialog = data;
972
973 dialog->button_focused = 0;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500974 widget_schedule_redraw(widget);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200975}
976
977static struct unlock_dialog *
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500978unlock_dialog_create(struct desktop *desktop)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200979{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500980 struct display *display = desktop->display;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200981 struct unlock_dialog *dialog;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800982 struct wl_surface *surface;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200983
Peter Huttererf3d62272013-08-08 11:57:05 +1000984 dialog = xzalloc(sizeof *dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200985
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400986 dialog->window = window_create_custom(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500987 dialog->widget = window_frame_create(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200988 window_set_title(dialog->window, "Unlock your desktop");
989
990 window_set_user_data(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200991 window_set_keyboard_focus_handler(dialog->window,
992 unlock_dialog_keyboard_focus_handler);
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500993 dialog->button = widget_add_widget(dialog->widget, dialog);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500994 widget_set_redraw_handler(dialog->widget,
995 unlock_dialog_redraw_handler);
Kristian Høgsbergee143232012-01-09 08:42:24 -0500996 widget_set_enter_handler(dialog->button,
997 unlock_dialog_widget_enter_handler);
998 widget_set_leave_handler(dialog->button,
999 unlock_dialog_widget_leave_handler);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05001000 widget_set_button_handler(dialog->button,
1001 unlock_dialog_button_handler);
Brian J Lovinc4df4082013-08-26 15:58:22 -07001002 widget_set_touch_down_handler(dialog->button,
1003 unlock_dialog_touch_down_handler);
1004 widget_set_touch_up_handler(dialog->button,
1005 unlock_dialog_touch_up_handler);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001006
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001007 surface = window_get_wl_surface(dialog->window);
1008 weston_desktop_shell_set_lock_surface(desktop->shell, surface);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001009
Pekka Paalanen40e49ac2012-01-18 16:51:30 +02001010 window_schedule_resize(dialog->window, 260, 230);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001011
1012 return dialog;
1013}
1014
1015static void
1016unlock_dialog_destroy(struct unlock_dialog *dialog)
1017{
1018 window_destroy(dialog->window);
1019 free(dialog);
1020}
1021
1022static void
1023unlock_dialog_finish(struct task *task, uint32_t events)
1024{
1025 struct desktop *desktop =
Benjamin Franzked7759712011-11-22 12:38:48 +01001026 container_of(task, struct desktop, unlock_task);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001027
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001028 weston_desktop_shell_unlock(desktop->shell);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001029 unlock_dialog_destroy(desktop->unlock_dialog);
1030 desktop->unlock_dialog = NULL;
1031}
1032
1033static void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001034desktop_shell_configure(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001035 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001036 uint32_t edges,
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001037 struct wl_surface *surface,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001038 int32_t width, int32_t height)
1039{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001040 struct window *window = wl_surface_get_user_data(surface);
Pekka Paalanen068ae942011-11-28 14:11:15 +02001041 struct surface *s = window_get_user_data(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001042
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001043 s->configure(data, desktop_shell, edges, window, width, height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001044}
1045
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001046static void
1047desktop_shell_prepare_lock_surface(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001048 struct weston_desktop_shell *desktop_shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001049{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001050 struct desktop *desktop = data;
1051
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001052 if (!desktop->locking) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001053 weston_desktop_shell_unlock(desktop->shell);
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +02001054 return;
1055 }
1056
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001057 if (!desktop->unlock_dialog) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001058 desktop->unlock_dialog = unlock_dialog_create(desktop);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001059 desktop->unlock_dialog->desktop = desktop;
1060 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001061}
1062
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001063static void
1064desktop_shell_grab_cursor(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001065 struct weston_desktop_shell *desktop_shell,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001066 uint32_t cursor)
1067{
1068 struct desktop *desktop = data;
1069
1070 switch (cursor) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001071 case WESTON_DESKTOP_SHELL_CURSOR_NONE:
Philipp Brüschweiler16d59d72012-08-24 15:43:55 +02001072 desktop->grab_cursor = CURSOR_BLANK;
1073 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001074 case WESTON_DESKTOP_SHELL_CURSOR_BUSY:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001075 desktop->grab_cursor = CURSOR_WATCH;
1076 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001077 case WESTON_DESKTOP_SHELL_CURSOR_MOVE:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001078 desktop->grab_cursor = CURSOR_DRAGGING;
1079 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001080 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001081 desktop->grab_cursor = CURSOR_TOP;
1082 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001083 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001084 desktop->grab_cursor = CURSOR_BOTTOM;
1085 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001086 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_LEFT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001087 desktop->grab_cursor = CURSOR_LEFT;
1088 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001089 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_RIGHT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001090 desktop->grab_cursor = CURSOR_RIGHT;
1091 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001092 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP_LEFT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001093 desktop->grab_cursor = CURSOR_TOP_LEFT;
1094 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001095 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP_RIGHT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001096 desktop->grab_cursor = CURSOR_TOP_RIGHT;
1097 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001098 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_LEFT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001099 desktop->grab_cursor = CURSOR_BOTTOM_LEFT;
1100 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001101 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_RIGHT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001102 desktop->grab_cursor = CURSOR_BOTTOM_RIGHT;
1103 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001104 case WESTON_DESKTOP_SHELL_CURSOR_ARROW:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001105 default:
1106 desktop->grab_cursor = CURSOR_LEFT_PTR;
1107 }
1108}
1109
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001110static const struct weston_desktop_shell_listener listener = {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001111 desktop_shell_configure,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001112 desktop_shell_prepare_lock_surface,
1113 desktop_shell_grab_cursor
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001114};
1115
U. Artie Eoff44874d92012-10-02 21:12:35 -07001116static void
1117background_destroy(struct background *background)
1118{
1119 widget_destroy(background->widget);
1120 window_destroy(background->window);
1121
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001122 free(background->image);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001123 free(background);
1124}
1125
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001126static struct background *
Pekka Paalanencb115382017-12-07 12:15:01 +02001127background_create(struct desktop *desktop, struct output *output)
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001128{
1129 struct background *background;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001130 struct weston_config_section *s;
1131 char *type;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001132
Peter Huttererf3d62272013-08-08 11:57:05 +10001133 background = xzalloc(sizeof *background);
Pekka Paalanencb115382017-12-07 12:15:01 +02001134 background->owner = output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001135 background->base.configure = background_configure;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001136 background->window = window_create_custom(desktop->display);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001137 background->widget = window_add_widget(background->window, background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001138 window_set_user_data(background->window, background);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001139 widget_set_redraw_handler(background->widget, background_draw);
Arnaud Vracfb754a02014-08-25 20:56:49 +02001140 widget_set_transparent(background->widget, 0);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001141
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001142 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
1143 weston_config_section_get_string(s, "background-image",
Kristian Høgsberg4c3661f2014-04-21 22:54:37 -07001144 &background->image, NULL);
Bryce Harringtone776f2a2016-07-14 18:28:03 -07001145 weston_config_section_get_color(s, "background-color",
1146 &background->color, 0x00000000);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001147
1148 weston_config_section_get_string(s, "background-type",
1149 &type, "tile");
U. Artie Eoff3c946772014-01-15 10:59:50 -08001150 if (type == NULL) {
1151 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
1152 exit(EXIT_FAILURE);
1153 }
1154
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001155 if (strcmp(type, "scale") == 0) {
1156 background->type = BACKGROUND_SCALE;
1157 } else if (strcmp(type, "scale-crop") == 0) {
1158 background->type = BACKGROUND_SCALE_CROP;
1159 } else if (strcmp(type, "tile") == 0) {
1160 background->type = BACKGROUND_TILE;
Stefan Agner20b24162018-08-22 23:56:07 +02001161 } else if (strcmp(type, "centered") == 0) {
1162 background->type = BACKGROUND_CENTERED;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001163 } else {
1164 background->type = -1;
1165 fprintf(stderr, "invalid background-type: %s\n",
1166 type);
1167 }
1168
1169 free(type);
1170
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001171 return background;
1172}
1173
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001174static int
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001175grab_surface_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001176 float x, float y, void *data)
1177{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001178 struct desktop *desktop = data;
1179
1180 return desktop->grab_cursor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001181}
1182
1183static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001184grab_surface_destroy(struct desktop *desktop)
1185{
1186 widget_destroy(desktop->grab_widget);
1187 window_destroy(desktop->grab_window);
1188}
1189
1190static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001191grab_surface_create(struct desktop *desktop)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001192{
1193 struct wl_surface *s;
1194
Ander Conselvan de Oliveira07a91cd2012-07-16 14:15:50 +03001195 desktop->grab_window = window_create_custom(desktop->display);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001196 window_set_user_data(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001197
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001198 s = window_get_wl_surface(desktop->grab_window);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001199 weston_desktop_shell_set_grab_surface(desktop->shell, s);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001200
1201 desktop->grab_widget =
1202 window_add_widget(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001203 /* We set the allocation to 1x1 at 0,0 so the fake enter event
1204 * at 0,0 will go to this widget. */
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001205 widget_set_allocation(desktop->grab_widget, 0, 0, 1, 1);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001206
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001207 widget_set_enter_handler(desktop->grab_widget,
1208 grab_surface_enter_handler);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001209}
1210
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001211static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001212output_destroy(struct output *output)
1213{
Pekka Paalanencb115382017-12-07 12:15:01 +02001214 if (output->background)
1215 background_destroy(output->background);
Jonny Lambe67118c2014-08-12 15:07:51 +02001216 if (output->panel)
1217 panel_destroy(output->panel);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001218 wl_output_destroy(output->output);
1219 wl_list_remove(&output->link);
1220
1221 free(output);
1222}
1223
1224static void
1225desktop_destroy_outputs(struct desktop *desktop)
1226{
1227 struct output *tmp;
1228 struct output *output;
1229
1230 wl_list_for_each_safe(output, tmp, &desktop->outputs, link)
1231 output_destroy(output);
1232}
1233
1234static void
Alexander Larssonc584fa62013-05-22 14:41:32 +02001235output_handle_geometry(void *data,
1236 struct wl_output *wl_output,
1237 int x, int y,
1238 int physical_width,
1239 int physical_height,
1240 int subpixel,
1241 const char *make,
1242 const char *model,
1243 int transform)
1244{
1245 struct output *output = data;
1246
Pekka Paalanenc1bcce62017-12-07 15:30:18 +02001247 output->x = x;
1248 output->y = y;
1249
Jonny Lambe67118c2014-08-12 15:07:51 +02001250 if (output->panel)
1251 window_set_buffer_transform(output->panel->window, transform);
Pekka Paalanencb115382017-12-07 12:15:01 +02001252 if (output->background)
1253 window_set_buffer_transform(output->background->window, transform);
Alexander Larssonc584fa62013-05-22 14:41:32 +02001254}
1255
1256static void
1257output_handle_mode(void *data,
1258 struct wl_output *wl_output,
1259 uint32_t flags,
1260 int width,
1261 int height,
1262 int refresh)
1263{
1264}
1265
1266static void
1267output_handle_done(void *data,
1268 struct wl_output *wl_output)
1269{
1270}
1271
1272static void
1273output_handle_scale(void *data,
1274 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001275 int32_t scale)
Alexander Larssonc584fa62013-05-22 14:41:32 +02001276{
1277 struct output *output = data;
1278
Jonny Lambe67118c2014-08-12 15:07:51 +02001279 if (output->panel)
1280 window_set_buffer_scale(output->panel->window, scale);
Pekka Paalanencb115382017-12-07 12:15:01 +02001281 if (output->background)
1282 window_set_buffer_scale(output->background->window, scale);
Alexander Larssonc584fa62013-05-22 14:41:32 +02001283}
1284
1285static const struct wl_output_listener output_listener = {
1286 output_handle_geometry,
1287 output_handle_mode,
1288 output_handle_done,
1289 output_handle_scale
1290};
1291
1292static void
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001293output_init(struct output *output, struct desktop *desktop)
1294{
1295 struct wl_surface *surface;
1296
Quentin Glidic55d57012016-06-23 18:55:18 +02001297 if (desktop->want_panel) {
Pekka Paalanen1cbfcf42017-12-07 12:39:15 +02001298 output->panel = panel_create(desktop, output);
Jonny Lambe67118c2014-08-12 15:07:51 +02001299 surface = window_get_wl_surface(output->panel->window);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001300 weston_desktop_shell_set_panel(desktop->shell,
1301 output->output, surface);
Jonny Lambe67118c2014-08-12 15:07:51 +02001302 }
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001303
Pekka Paalanencb115382017-12-07 12:15:01 +02001304 output->background = background_create(desktop, output);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001305 surface = window_get_wl_surface(output->background->window);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001306 weston_desktop_shell_set_background(desktop->shell,
1307 output->output, surface);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001308}
1309
1310static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001311create_output(struct desktop *desktop, uint32_t id)
1312{
1313 struct output *output;
1314
Bryce Harrington0d1a6222016-02-11 16:42:49 -08001315 output = zalloc(sizeof *output);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001316 if (!output)
1317 return;
1318
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001319 output->output =
Alexander Larssonc584fa62013-05-22 14:41:32 +02001320 display_bind(desktop->display, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001321 output->server_output_id = id;
Alexander Larssonc584fa62013-05-22 14:41:32 +02001322
1323 wl_output_add_listener(output->output, &output_listener, output);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001324
1325 wl_list_insert(&desktop->outputs, &output->link);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001326
1327 /* On start up we may process an output global before the shell global
1328 * in which case we can't create the panel and background just yet */
1329 if (desktop->shell)
1330 output_init(output, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001331}
1332
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001333static void
Pekka Paalanenc1bcce62017-12-07 15:30:18 +02001334output_remove(struct desktop *desktop, struct output *output)
1335{
1336 struct output *cur;
1337 struct output *rep = NULL;
1338
1339 if (!output->background) {
1340 output_destroy(output);
1341 return;
1342 }
1343
1344 /* Find a wl_output that is a clone of the removed wl_output.
1345 * We don't want to leave the clone without a background or panel. */
1346 wl_list_for_each(cur, &desktop->outputs, link) {
1347 if (cur == output)
1348 continue;
1349
1350 /* XXX: Assumes size matches. */
1351 if (cur->x == output->x && cur->y == output->y) {
1352 rep = cur;
1353 break;
1354 }
1355 }
1356
1357 if (rep) {
Pekka Paalanen82dd54d2018-06-21 15:38:56 +03001358 /* If found and it does not already have a background or panel,
1359 * hand over the background and panel so they don't get
1360 * destroyed.
1361 *
1362 * We never create multiple backgrounds or panels for clones,
1363 * but if the compositor moves outputs, a pair of wl_outputs
1364 * might become "clones". This may happen temporarily when
1365 * an output is about to be removed and the rest are reflowed.
1366 * In this case it is correct to let the background/panel be
1367 * destroyed.
1368 */
Pekka Paalanenc1bcce62017-12-07 15:30:18 +02001369
Pekka Paalanen82dd54d2018-06-21 15:38:56 +03001370 if (!rep->background) {
1371 rep->background = output->background;
1372 output->background = NULL;
1373 rep->background->owner = rep;
1374 }
Pekka Paalanenc1bcce62017-12-07 15:30:18 +02001375
Pekka Paalanen82dd54d2018-06-21 15:38:56 +03001376 if (!rep->panel) {
1377 rep->panel = output->panel;
1378 output->panel = NULL;
1379 if (rep->panel)
1380 rep->panel->owner = rep;
1381 }
Pekka Paalanenc1bcce62017-12-07 15:30:18 +02001382 }
1383
1384 output_destroy(output);
1385}
1386
1387static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001388global_handler(struct display *display, uint32_t id,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001389 const char *interface, uint32_t version, void *data)
1390{
1391 struct desktop *desktop = data;
1392
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001393 if (!strcmp(interface, "weston_desktop_shell")) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001394 desktop->shell = display_bind(desktop->display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001395 id,
1396 &weston_desktop_shell_interface,
1397 1);
1398 weston_desktop_shell_add_listener(desktop->shell,
1399 &listener,
1400 desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001401 } else if (!strcmp(interface, "wl_output")) {
1402 create_output(desktop, id);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001403 }
1404}
1405
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001406static void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001407global_handler_remove(struct display *display, uint32_t id,
1408 const char *interface, uint32_t version, void *data)
1409{
1410 struct desktop *desktop = data;
1411 struct output *output;
1412
1413 if (!strcmp(interface, "wl_output")) {
1414 wl_list_for_each(output, &desktop->outputs, link) {
1415 if (output->server_output_id == id) {
Pekka Paalanenc1bcce62017-12-07 15:30:18 +02001416 output_remove(desktop, output);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001417 break;
1418 }
1419 }
1420 }
1421}
1422
1423static void
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001424panel_add_launchers(struct panel *panel, struct desktop *desktop)
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001425{
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001426 struct weston_config_section *s;
1427 char *icon, *path;
1428 const char *name;
1429 int count;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001430
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001431 count = 0;
1432 s = NULL;
1433 while (weston_config_next_section(desktop->config, &s, &name)) {
1434 if (strcmp(name, "launcher") != 0)
1435 continue;
1436
1437 weston_config_section_get_string(s, "icon", &icon, NULL);
1438 weston_config_section_get_string(s, "path", &path, NULL);
1439
1440 if (icon != NULL && path != NULL) {
1441 panel_add_launcher(panel, icon, path);
Rob Bradford09252d42013-07-26 16:29:45 +01001442 count++;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001443 } else {
1444 fprintf(stderr, "invalid launcher section\n");
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001445 }
1446
1447 free(icon);
1448 free(path);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001449 }
1450
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001451 if (count == 0) {
Derek Foremane2772762018-02-06 15:18:38 -06001452 char *name = file_name_with_datadir("terminal.png");
1453
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001454 /* add default launcher */
1455 panel_add_launcher(panel,
Derek Foremane2772762018-02-06 15:18:38 -06001456 name,
Rodney Lorrimar99ff01b2012-02-29 17:31:03 +01001457 BINDIR "/weston-terminal");
Derek Foremane2772762018-02-06 15:18:38 -06001458 free(name);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001459 }
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001460}
1461
Quentin Glidic55d57012016-06-23 18:55:18 +02001462static void
1463parse_panel_position(struct desktop *desktop, struct weston_config_section *s)
1464{
1465 char *position;
1466
Daniel Stone08cf24b2017-01-16 13:18:21 +00001467 desktop->want_panel = 1;
Quentin Glidic55d57012016-06-23 18:55:18 +02001468
Daniel Stone08cf24b2017-01-16 13:18:21 +00001469 weston_config_section_get_string(s, "panel-position", &position, "top");
1470 if (strcmp(position, "top") == 0) {
1471 desktop->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
1472 } else if (strcmp(position, "bottom") == 0) {
1473 desktop->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM;
1474 } else if (strcmp(position, "left") == 0) {
1475 desktop->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT;
1476 } else if (strcmp(position, "right") == 0) {
1477 desktop->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT;
1478 } else {
1479 /* 'none' is valid here */
1480 if (strcmp(position, "none") != 0)
1481 fprintf(stderr, "Wrong panel position: %s\n", position);
1482 desktop->want_panel = 0;
Quentin Glidic55d57012016-06-23 18:55:18 +02001483 }
Daniel Stone08cf24b2017-01-16 13:18:21 +00001484 free(position);
Quentin Glidic55d57012016-06-23 18:55:18 +02001485}
1486
Quentin Glidic3e37b342016-06-23 18:55:22 +02001487static void
1488parse_clock_format(struct desktop *desktop, struct weston_config_section *s)
1489{
1490 char *clock_format;
1491
1492 weston_config_section_get_string(s, "clock-format", &clock_format, "");
1493 if (strcmp(clock_format, "minutes") == 0)
1494 desktop->clock_format = CLOCK_FORMAT_MINUTES;
1495 else if (strcmp(clock_format, "seconds") == 0)
1496 desktop->clock_format = CLOCK_FORMAT_SECONDS;
1497 else if (strcmp(clock_format, "none") == 0)
1498 desktop->clock_format = CLOCK_FORMAT_NONE;
1499 else
1500 desktop->clock_format = DEFAULT_CLOCK_FORMAT;
1501 free(clock_format);
1502}
1503
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001504int main(int argc, char *argv[])
1505{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001506 struct desktop desktop = { 0 };
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001507 struct output *output;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001508 struct weston_config_section *s;
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001509 const char *config_file;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001510
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001511 desktop.unlock_task.run = unlock_dialog_finish;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001512 wl_list_init(&desktop.outputs);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001513
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001514 config_file = weston_config_get_name_from_env();
1515 desktop.config = weston_config_parse(config_file);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001516 s = weston_config_get_section(desktop.config, "shell", NULL, NULL);
1517 weston_config_section_get_bool(s, "locking", &desktop.locking, 1);
Quentin Glidic55d57012016-06-23 18:55:18 +02001518 parse_panel_position(&desktop, s);
Quentin Glidic3e37b342016-06-23 18:55:22 +02001519 parse_clock_format(&desktop, s);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001520
Kristian Høgsberg4172f662013-02-20 15:27:49 -05001521 desktop.display = display_create(&argc, argv);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001522 if (desktop.display == NULL) {
Antonio Borneo39578632019-04-26 23:57:31 +02001523 fprintf(stderr, "failed to create display: %s\n",
1524 strerror(errno));
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001525 return -1;
1526 }
1527
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001528 display_set_user_data(desktop.display, &desktop);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001529 display_set_global_handler(desktop.display, global_handler);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001530 display_set_global_handler_remove(desktop.display, global_handler_remove);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001531
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001532 /* Create panel and background for outputs processed before the shell
1533 * global interface was processed */
Quentin Glidic55d57012016-06-23 18:55:18 +02001534 if (desktop.want_panel)
1535 weston_desktop_shell_set_panel_position(desktop.shell, desktop.panel_position);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001536 wl_list_for_each(output, &desktop.outputs, link)
1537 if (!output->panel)
1538 output_init(output, &desktop);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001539
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001540 grab_surface_create(&desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001541
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001542 signal(SIGCHLD, sigchild_handler);
1543
1544 display_run(desktop.display);
1545
U. Artie Eoff44874d92012-10-02 21:12:35 -07001546 /* Cleanup */
1547 grab_surface_destroy(&desktop);
1548 desktop_destroy_outputs(&desktop);
1549 if (desktop.unlock_dialog)
1550 unlock_dialog_destroy(desktop.unlock_dialog);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001551 weston_desktop_shell_destroy(desktop.shell);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001552 display_destroy(desktop.display);
1553
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001554 return 0;
1555}