blob: bd0032a7943f52e4283a007352a70c60d7e76699 [file] [log] [blame]
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001/*
2 * Copyright © 2011 Kristian Høgsberg
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02003 * Copyright © 2011 Collabora, Ltd.
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04004 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07005 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040011 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070012 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040023 */
24
U. Artie Eoff3c946772014-01-15 10:59:50 -080025#include "config.h"
26
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <fcntl.h>
32#include <unistd.h>
U. Artie Eoff3c946772014-01-15 10:59:50 -080033#include <errno.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040034#include <math.h>
35#include <cairo.h>
36#include <sys/wait.h>
Martin Minarik1e51a872012-06-08 00:39:11 +020037#include <sys/timerfd.h>
Michael Vetter2a18a522015-05-15 17:17:47 +020038#include <sys/epoll.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040039#include <linux/input.h>
Tiago Vignatti61500722012-05-23 22:06:28 +030040#include <libgen.h>
Kristian Høgsbergd1936b92012-07-23 22:59:33 -040041#include <ctype.h>
Martin Minarik1e51a872012-06-08 00:39:11 +020042#include <time.h>
Quentin Glidicf9574f22016-06-23 18:55:21 +020043#include <assert.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040044
Pekka Paalanen50719bc2011-11-22 14:18:50 +020045#include <wayland-client.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040046#include "window.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070047#include "shared/cairo-util.h"
48#include "shared/config-parser.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070049#include "shared/helpers.h"
Bryce Harringtone99e4bf2016-03-16 14:15:18 -070050#include "shared/xalloc.h"
Bryce Harrington0d1a6222016-02-11 16:42:49 -080051#include "shared/zalloc.h"
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
56
Pekka Paalanenb6df4f72012-08-03 14:39:15 +030057extern char **environ; /* defined by libc */
58
Quentin Glidicf9574f22016-06-23 18:55:21 +020059enum clock_format {
60 CLOCK_FORMAT_MINUTES,
61 CLOCK_FORMAT_SECONDS,
62 CLOCK_FORMAT_NONE
63};
64
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040065struct desktop {
66 struct display *display;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080067 struct weston_desktop_shell *shell;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +020068 struct unlock_dialog *unlock_dialog;
69 struct task unlock_task;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010070 struct wl_list outputs;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040071
Quentin Glidic55d57012016-06-23 18:55:18 +020072 int want_panel;
73 enum weston_desktop_shell_panel_position panel_position;
Quentin Glidic3e37b342016-06-23 18:55:22 +020074 enum clock_format clock_format;
Quentin Glidic55d57012016-06-23 18:55:18 +020075
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030076 struct window *grab_window;
77 struct widget *grab_widget;
78
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +030079 struct weston_config *config;
80 int locking;
81
Scott Moreauec116022012-07-22 18:23:52 -060082 enum cursor_type grab_cursor;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030083
84 int painted;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010085};
86
87struct surface {
88 void (*configure)(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080089 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040090 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010091 int32_t width, int32_t height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040092};
93
94struct panel {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010095 struct surface base;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040096 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050097 struct widget *widget;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050098 struct wl_list launcher_list;
Martin Minarik1e51a872012-06-08 00:39:11 +020099 struct panel_clock *clock;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300100 int painted;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200101 enum weston_desktop_shell_panel_position panel_position;
Quentin Glidicf9574f22016-06-23 18:55:21 +0200102 enum clock_format clock_format;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300103 uint32_t color;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400104};
105
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100106struct background {
107 struct surface base;
108 struct window *window;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500109 struct widget *widget;
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300110 int painted;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300111
112 char *image;
113 int type;
114 uint32_t color;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100115};
116
117struct output {
118 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800119 uint32_t server_output_id;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100120 struct wl_list link;
121
122 struct panel *panel;
123 struct background *background;
124};
125
Kristian Høgsberg53880802012-01-09 11:16:50 -0500126struct panel_launcher {
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500127 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400128 struct panel *panel;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400129 cairo_surface_t *icon;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500130 int focused, pressed;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400131 char *path;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500132 struct wl_list link;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400133 struct wl_array envp;
134 struct wl_array argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400135};
136
Martin Minarik1e51a872012-06-08 00:39:11 +0200137struct panel_clock {
138 struct widget *widget;
139 struct panel *panel;
Martin Minarik1e51a872012-06-08 00:39:11 +0200140 struct task clock_task;
141 int clock_fd;
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100142 char *format_string;
143 time_t refresh_timer;
Martin Minarik1e51a872012-06-08 00:39:11 +0200144};
145
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200146struct unlock_dialog {
147 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500148 struct widget *widget;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500149 struct widget *button;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500150 int button_focused;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200151 int closing;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200152 struct desktop *desktop;
153};
154
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300155static void
156panel_add_launchers(struct panel *panel, struct desktop *desktop);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500157
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400158static void
159sigchild_handler(int s)
160{
161 int status;
162 pid_t pid;
163
164 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
165 fprintf(stderr, "child %d exited\n", pid);
166}
167
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300168static int
169is_desktop_painted(struct desktop *desktop)
170{
171 struct output *output;
172
173 wl_list_for_each(output, &desktop->outputs, link) {
174 if (output->panel && !output->panel->painted)
175 return 0;
176 if (output->background && !output->background->painted)
177 return 0;
178 }
179
180 return 1;
181}
182
183static void
184check_desktop_ready(struct window *window)
185{
186 struct display *display;
187 struct desktop *desktop;
188
189 display = window_get_display(window);
190 desktop = display_get_user_data(display);
191
192 if (!desktop->painted && is_desktop_painted(desktop)) {
193 desktop->painted = 1;
194
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800195 weston_desktop_shell_desktop_ready(desktop->shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300196 }
197}
198
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400199static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500200panel_launcher_activate(struct panel_launcher *widget)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400201{
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400202 char **argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400203 pid_t pid;
204
205 pid = fork();
206 if (pid < 0) {
207 fprintf(stderr, "fork failed: %m\n");
208 return;
209 }
210
211 if (pid)
212 return;
Benjamin Franzked7759712011-11-22 12:38:48 +0100213
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400214 argv = widget->argv.data;
215 if (execve(argv[0], argv, widget->envp.data) < 0) {
216 fprintf(stderr, "execl '%s' failed: %m\n", argv[0]);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400217 exit(1);
218 }
219}
220
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400221static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500222panel_launcher_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400223{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500224 struct panel_launcher *launcher = data;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500225 struct rectangle allocation;
226 cairo_t *cr;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400227
Alexander Larssonc584fa62013-05-22 14:41:32 +0200228 cr = widget_cairo_create(launcher->panel->widget);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500229
230 widget_get_allocation(widget, &allocation);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500231 if (launcher->pressed) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500232 allocation.x++;
233 allocation.y++;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400234 }
235
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500236 cairo_set_source_surface(cr, launcher->icon,
237 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400238 cairo_paint(cr);
239
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500240 if (launcher->focused) {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400241 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500242 cairo_mask_surface(cr, launcher->icon,
243 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400244 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400245
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500246 cairo_destroy(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400247}
248
Tiago Vignatti61500722012-05-23 22:06:28 +0300249static int
250panel_launcher_motion_handler(struct widget *widget, struct input *input,
251 uint32_t time, float x, float y, void *data)
252{
253 struct panel_launcher *launcher = data;
254
255 widget_set_tooltip(widget, basename((char *)launcher->path), x, y);
256
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300257 return CURSOR_LEFT_PTR;
Tiago Vignatti61500722012-05-23 22:06:28 +0300258}
259
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400260static void
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500261set_hex_color(cairo_t *cr, uint32_t color)
262{
Michael Vetter2a18a522015-05-15 17:17:47 +0200263 cairo_set_source_rgba(cr,
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500264 ((color >> 16) & 0xff) / 255.0,
265 ((color >> 8) & 0xff) / 255.0,
266 ((color >> 0) & 0xff) / 255.0,
267 ((color >> 24) & 0xff) / 255.0);
268}
269
270static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500271panel_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400272{
273 cairo_surface_t *surface;
274 cairo_t *cr;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500275 struct panel *panel = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400276
Alexander Larssonc584fa62013-05-22 14:41:32 +0200277 cr = widget_cairo_create(panel->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400278 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300279 set_hex_color(cr, panel->color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400280 cairo_paint(cr);
281
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400282 cairo_destroy(cr);
Alexander Larssonc584fa62013-05-22 14:41:32 +0200283 surface = window_get_surface(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400284 cairo_surface_destroy(surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300285 panel->painted = 1;
286 check_desktop_ready(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400287}
288
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500289static int
Kristian Høgsberg53880802012-01-09 11:16:50 -0500290panel_launcher_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400291 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500292{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500293 struct panel_launcher *launcher = data;
294
295 launcher->focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500296 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500297
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300298 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500299}
300
301static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500302panel_launcher_leave_handler(struct widget *widget,
303 struct input *input, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400304{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500305 struct panel_launcher *launcher = data;
306
307 launcher->focused = 0;
Tiago Vignatti61500722012-05-23 22:06:28 +0300308 widget_destroy_tooltip(widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500309 widget_schedule_redraw(widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400310}
311
312static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500313panel_launcher_button_handler(struct widget *widget,
314 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100315 uint32_t button,
316 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400317{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500318 struct panel_launcher *launcher;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400319
Kristian Høgsberg53880802012-01-09 11:16:50 -0500320 launcher = widget_get_user_data(widget);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500321 widget_schedule_redraw(widget);
Daniel Stone4dbadb12012-05-30 16:31:51 +0100322 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg53880802012-01-09 11:16:50 -0500323 panel_launcher_activate(launcher);
Rusty Lynch4384a242013-08-08 21:28:22 -0700324
325}
326
327static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700328panel_launcher_touch_down_handler(struct widget *widget, struct input *input,
329 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch4384a242013-08-08 21:28:22 -0700330 float x, float y, void *data)
331{
332 struct panel_launcher *launcher;
333
334 launcher = widget_get_user_data(widget);
335 launcher->focused = 1;
336 widget_schedule_redraw(widget);
337}
338
339static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700340panel_launcher_touch_up_handler(struct widget *widget, struct input *input,
Michael Vetter2a18a522015-05-15 17:17:47 +0200341 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch1084da52013-08-15 09:10:08 -0700342 void *data)
Rusty Lynch4384a242013-08-08 21:28:22 -0700343{
344 struct panel_launcher *launcher;
345
346 launcher = widget_get_user_data(widget);
347 launcher->focused = 0;
348 widget_schedule_redraw(widget);
349 panel_launcher_activate(launcher);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500350}
351
Martin Minarik1e51a872012-06-08 00:39:11 +0200352static void
353clock_func(struct task *task, uint32_t events)
354{
355 struct panel_clock *clock =
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400356 container_of(task, struct panel_clock, clock_task);
Kristian Høgsberg70226bb2012-06-08 16:54:52 -0400357 uint64_t exp;
Martin Minarik1e51a872012-06-08 00:39:11 +0200358
Martin Olsson8df662a2012-07-08 03:03:47 +0200359 if (read(clock->clock_fd, &exp, sizeof exp) != sizeof exp)
360 abort();
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400361 widget_schedule_redraw(clock->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200362}
363
364static void
365panel_clock_redraw_handler(struct widget *widget, void *data)
366{
Martin Minarik1e51a872012-06-08 00:39:11 +0200367 struct panel_clock *clock = data;
368 cairo_t *cr;
369 struct rectangle allocation;
370 cairo_text_extents_t extents;
371 cairo_font_extents_t font_extents;
Martin Minarik1e51a872012-06-08 00:39:11 +0200372 time_t rawtime;
373 struct tm * timeinfo;
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400374 char string[128];
Martin Minarik1e51a872012-06-08 00:39:11 +0200375
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400376 time(&rawtime);
377 timeinfo = localtime(&rawtime);
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100378 strftime(string, sizeof string, clock->format_string, timeinfo);
Martin Minarik1e51a872012-06-08 00:39:11 +0200379
380 widget_get_allocation(widget, &allocation);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400381 if (allocation.width == 0)
382 return;
Martin Minarik1e51a872012-06-08 00:39:11 +0200383
Alexander Larssonc584fa62013-05-22 14:41:32 +0200384 cr = widget_cairo_create(clock->panel->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200385 cairo_select_font_face(cr, "sans",
386 CAIRO_FONT_SLANT_NORMAL,
387 CAIRO_FONT_WEIGHT_NORMAL);
388 cairo_set_font_size(cr, 14);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400389 cairo_text_extents(cr, string, &extents);
Martin Minarik1e51a872012-06-08 00:39:11 +0200390 cairo_font_extents (cr, &font_extents);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400391 cairo_move_to(cr, allocation.x + 5,
392 allocation.y + 3 * (allocation.height >> 2) + 1);
393 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400394 cairo_show_text(cr, string);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400395 cairo_move_to(cr, allocation.x + 4,
396 allocation.y + 3 * (allocation.height >> 2));
397 cairo_set_source_rgb(cr, 1, 1, 1);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400398 cairo_show_text(cr, string);
Martin Minarik1e51a872012-06-08 00:39:11 +0200399 cairo_destroy(cr);
400}
401
402static int
403clock_timer_reset(struct panel_clock *clock)
404{
405 struct itimerspec its;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400406
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100407 its.it_interval.tv_sec = clock->refresh_timer;
Martin Minarik1e51a872012-06-08 00:39:11 +0200408 its.it_interval.tv_nsec = 0;
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100409 its.it_value.tv_sec = clock->refresh_timer;
Martin Minarik1e51a872012-06-08 00:39:11 +0200410 its.it_value.tv_nsec = 0;
411 if (timerfd_settime(clock->clock_fd, 0, &its, NULL) < 0) {
412 fprintf(stderr, "could not set timerfd\n: %m");
413 return -1;
414 }
415
416 return 0;
417}
418
419static void
U. Artie Eoff44874d92012-10-02 21:12:35 -0700420panel_destroy_clock(struct panel_clock *clock)
421{
422 widget_destroy(clock->widget);
423
424 close(clock->clock_fd);
425
426 free(clock);
427}
428
429static void
Martin Minarik1e51a872012-06-08 00:39:11 +0200430panel_add_clock(struct panel *panel)
431{
432 struct panel_clock *clock;
433 int timerfd;
434
435 timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
436 if (timerfd < 0) {
437 fprintf(stderr, "could not create timerfd\n: %m");
438 return;
439 }
440
Peter Huttererf3d62272013-08-08 11:57:05 +1000441 clock = xzalloc(sizeof *clock);
Martin Minarik1e51a872012-06-08 00:39:11 +0200442 clock->panel = panel;
443 panel->clock = clock;
444 clock->clock_fd = timerfd;
445
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100446 switch (panel->clock_format) {
447 case CLOCK_FORMAT_MINUTES:
448 clock->format_string = "%a %b %d, %I:%M %p";
449 clock->refresh_timer = 60;
450 break;
451 case CLOCK_FORMAT_SECONDS:
452 clock->format_string = "%a %b %d, %I:%M:%S %p";
453 clock->refresh_timer = 1;
454 break;
Quentin Glidicf9574f22016-06-23 18:55:21 +0200455 case CLOCK_FORMAT_NONE:
456 assert(!"not reached");
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100457 }
458
Martin Minarik1e51a872012-06-08 00:39:11 +0200459 clock->clock_task.run = clock_func;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400460 display_watch_fd(window_get_display(panel->window), clock->clock_fd,
461 EPOLLIN, &clock->clock_task);
Martin Minarik1e51a872012-06-08 00:39:11 +0200462 clock_timer_reset(clock);
463
464 clock->widget = widget_add_widget(panel->widget, clock);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400465 widget_set_redraw_handler(clock->widget, panel_clock_redraw_handler);
Martin Minarik1e51a872012-06-08 00:39:11 +0200466}
467
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500468static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500469panel_resize_handler(struct widget *widget,
470 int32_t width, int32_t height, void *data)
471{
472 struct panel_launcher *launcher;
473 struct panel *panel = data;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200474 int bx = width / 2;
Quentin Glidic51c2c372016-06-23 18:55:23 +0200475 int by = height / 2;
476 int spacing = 10;
477 int x = spacing;
478 int y = spacing;
479 int w, h;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200480 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 +0200481
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500482 wl_list_for_each(launcher, &panel->launcher_list, link) {
483 w = cairo_image_surface_get_width(launcher->icon);
484 h = cairo_image_surface_get_height(launcher->icon);
Quentin Glidic51c2c372016-06-23 18:55:23 +0200485
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200486 if (horizontal)
Quentin Glidic51c2c372016-06-23 18:55:23 +0200487 y = by - h / 2;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200488 else
489 x = bx - w / 2;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500490 widget_set_allocation(launcher->widget,
Quentin Glidic51c2c372016-06-23 18:55:23 +0200491 x, y, w + 1, h + 1);
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200492 if (horizontal)
Quentin Glidic51c2c372016-06-23 18:55:23 +0200493 x += w + spacing;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200494 else
495 y += h + spacing;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500496 }
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100497
498 h = 20;
499
500 if (panel->clock_format == CLOCK_FORMAT_SECONDS)
501 w = 190;
502 else /* CLOCK_FORMAT_MINUTES */
503 w = 170;
Pekka Paalanen01b17252012-06-12 17:42:26 +0300504
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200505 if (horizontal) {
Quentin Glidic51c2c372016-06-23 18:55:23 +0200506 x = width - w - spacing;
507 y = by - h / 2;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200508 } else {
509 x = bx - w / 2;
510 y = height - h - spacing;
511 }
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
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100519panel_configure(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800520 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400521 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100522 int32_t width, int32_t height)
523{
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200524 struct desktop *desktop = data;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500525 struct surface *surface = window_get_user_data(window);
526 struct panel *panel = container_of(surface, struct panel, base);
527
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200528 switch (desktop->panel_position) {
529 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
530 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Quentin Glidic51c2c372016-06-23 18:55:23 +0200531 height = 32;
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200532 break;
533 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
534 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
535 switch (desktop->clock_format) {
536 case CLOCK_FORMAT_NONE:
537 width = 32;
538 break;
539 case CLOCK_FORMAT_MINUTES:
540 width = 170;
541 break;
542 case CLOCK_FORMAT_SECONDS:
543 width = 190;
544 break;
545 }
546 break;
547 }
Quentin Glidic51c2c372016-06-23 18:55:23 +0200548 window_schedule_resize(panel->window, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100549}
550
U. Artie Eoff44874d92012-10-02 21:12:35 -0700551static void
552panel_destroy_launcher(struct panel_launcher *launcher)
553{
554 wl_array_release(&launcher->argv);
555 wl_array_release(&launcher->envp);
556
557 free(launcher->path);
558
559 cairo_surface_destroy(launcher->icon);
560
561 widget_destroy(launcher->widget);
562 wl_list_remove(&launcher->link);
563
564 free(launcher);
565}
566
567static void
568panel_destroy(struct panel *panel)
569{
570 struct panel_launcher *tmp;
571 struct panel_launcher *launcher;
572
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100573 if (panel->clock)
574 panel_destroy_clock(panel->clock);
U. Artie Eoff44874d92012-10-02 21:12:35 -0700575
576 wl_list_for_each_safe(launcher, tmp, &panel->launcher_list, link)
577 panel_destroy_launcher(launcher);
578
579 widget_destroy(panel->widget);
580 window_destroy(panel->window);
581
582 free(panel);
583}
584
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400585static struct panel *
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300586panel_create(struct desktop *desktop)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400587{
588 struct panel *panel;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300589 struct weston_config_section *s;
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100590 char *clock_format_option = NULL;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400591
Peter Huttererf3d62272013-08-08 11:57:05 +1000592 panel = xzalloc(sizeof *panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400593
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100594 panel->base.configure = panel_configure;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300595 panel->window = window_create_custom(desktop->display);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500596 panel->widget = window_add_widget(panel->window, panel);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500597 wl_list_init(&panel->launcher_list);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400598
599 window_set_title(panel->window, "panel");
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400600 window_set_user_data(panel->window, panel);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500601
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500602 widget_set_redraw_handler(panel->widget, panel_redraw_handler);
603 widget_set_resize_handler(panel->widget, panel_resize_handler);
Michael Vetter2a18a522015-05-15 17:17:47 +0200604
Quentin Glidice7ed60f2016-06-23 18:55:24 +0200605 panel->panel_position = desktop->panel_position;
Quentin Glidic3e37b342016-06-23 18:55:22 +0200606 panel->clock_format = desktop->clock_format;
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100607 if (panel->clock_format != CLOCK_FORMAT_NONE)
608 panel_add_clock(panel);
609
610 free (clock_format_option);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400611
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300612 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
Bryce Harringtone776f2a2016-07-14 18:28:03 -0700613 weston_config_section_get_color(s, "panel-color",
614 &panel->color, 0xaa000000);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300615
616 panel_add_launchers(panel, desktop);
617
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400618 return panel;
619}
620
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200621static cairo_surface_t *
622load_icon_or_fallback(const char *icon)
623{
624 cairo_surface_t *surface = cairo_image_surface_create_from_png(icon);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200625 cairo_status_t status;
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200626 cairo_t *cr;
627
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200628 status = cairo_surface_status(surface);
629 if (status == CAIRO_STATUS_SUCCESS)
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200630 return surface;
631
632 cairo_surface_destroy(surface);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200633 fprintf(stderr, "ERROR loading icon from file '%s', error: '%s'\n",
634 icon, cairo_status_to_string(status));
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200635
636 /* draw fallback icon */
637 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
638 20, 20);
639 cr = cairo_create(surface);
640
641 cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1);
642 cairo_paint(cr);
643
644 cairo_set_source_rgba(cr, 0, 0, 0, 1);
645 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
646 cairo_rectangle(cr, 0, 0, 20, 20);
647 cairo_move_to(cr, 4, 4);
648 cairo_line_to(cr, 16, 16);
649 cairo_move_to(cr, 4, 16);
650 cairo_line_to(cr, 16, 4);
651 cairo_stroke(cr);
652
653 cairo_destroy(cr);
654
655 return surface;
656}
657
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400658static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500659panel_add_launcher(struct panel *panel, const char *icon, const char *path)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400660{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500661 struct panel_launcher *launcher;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400662 char *start, *p, *eq, **ps;
663 int i, j, k;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400664
Peter Huttererf3d62272013-08-08 11:57:05 +1000665 launcher = xzalloc(sizeof *launcher);
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200666 launcher->icon = load_icon_or_fallback(icon);
U. Artie Eoff3c946772014-01-15 10:59:50 -0800667 launcher->path = xstrdup(path);
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400668
669 wl_array_init(&launcher->envp);
670 wl_array_init(&launcher->argv);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300671 for (i = 0; environ[i]; i++) {
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400672 ps = wl_array_add(&launcher->envp, sizeof *ps);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300673 *ps = environ[i];
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400674 }
675 j = 0;
676
677 start = launcher->path;
678 while (*start) {
679 for (p = start, eq = NULL; *p && !isspace(*p); p++)
680 if (*p == '=')
681 eq = p;
682
683 if (eq && j == 0) {
684 ps = launcher->envp.data;
685 for (k = 0; k < i; k++)
686 if (strncmp(ps[k], start, eq - start) == 0) {
687 ps[k] = start;
688 break;
689 }
690 if (k == i) {
691 ps = wl_array_add(&launcher->envp, sizeof *ps);
692 *ps = start;
693 i++;
694 }
695 } else {
696 ps = wl_array_add(&launcher->argv, sizeof *ps);
697 *ps = start;
698 j++;
699 }
700
701 while (*p && isspace(*p))
702 *p++ = '\0';
703
704 start = p;
705 }
706
707 ps = wl_array_add(&launcher->envp, sizeof *ps);
708 *ps = NULL;
709 ps = wl_array_add(&launcher->argv, sizeof *ps);
710 *ps = NULL;
711
Kristian Høgsberg53880802012-01-09 11:16:50 -0500712 launcher->panel = panel;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500713 wl_list_insert(panel->launcher_list.prev, &launcher->link);
714
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500715 launcher->widget = widget_add_widget(panel->widget, launcher);
Kristian Høgsberg53880802012-01-09 11:16:50 -0500716 widget_set_enter_handler(launcher->widget,
717 panel_launcher_enter_handler);
718 widget_set_leave_handler(launcher->widget,
719 panel_launcher_leave_handler);
720 widget_set_button_handler(launcher->widget,
721 panel_launcher_button_handler);
Rusty Lynch4384a242013-08-08 21:28:22 -0700722 widget_set_touch_down_handler(launcher->widget,
723 panel_launcher_touch_down_handler);
724 widget_set_touch_up_handler(launcher->widget,
725 panel_launcher_touch_up_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500726 widget_set_redraw_handler(launcher->widget,
727 panel_launcher_redraw_handler);
Tiago Vignatti61500722012-05-23 22:06:28 +0300728 widget_set_motion_handler(launcher->widget,
729 panel_launcher_motion_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400730}
731
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500732enum {
733 BACKGROUND_SCALE,
Pekka Paalanena402b052013-05-22 18:03:10 +0300734 BACKGROUND_SCALE_CROP,
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500735 BACKGROUND_TILE
736};
737
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400738static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500739background_draw(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400740{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500741 struct background *background = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400742 cairo_surface_t *surface, *image;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400743 cairo_pattern_t *pattern;
744 cairo_matrix_t matrix;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400745 cairo_t *cr;
Pekka Paalanena402b052013-05-22 18:03:10 +0300746 double im_w, im_h;
747 double sx, sy, s;
748 double tx, ty;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500749 struct rectangle allocation;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400750
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500751 surface = window_get_surface(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400752
Alexander Larssonc584fa62013-05-22 14:41:32 +0200753 cr = widget_cairo_create(background->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400754 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
755 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
756 cairo_paint(cr);
757
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500758 widget_get_allocation(widget, &allocation);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500759 image = NULL;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300760 if (background->image)
761 image = load_cairo_surface(background->image);
Kristian Høgsberg4c3661f2014-04-21 22:54:37 -0700762 else if (background->color == 0)
763 image = load_cairo_surface(DATADIR "/weston/pattern.png");
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500764
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300765 if (image && background->type != -1) {
Pekka Paalanena402b052013-05-22 18:03:10 +0300766 im_w = cairo_image_surface_get_width(image);
767 im_h = cairo_image_surface_get_height(image);
768 sx = im_w / allocation.width;
769 sy = im_h / allocation.height;
770
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400771 pattern = cairo_pattern_create_for_surface(image);
Pekka Paalanena402b052013-05-22 18:03:10 +0300772
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300773 switch (background->type) {
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500774 case BACKGROUND_SCALE:
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500775 cairo_matrix_init_scale(&matrix, sx, sy);
776 cairo_pattern_set_matrix(pattern, &matrix);
Bill Spitzak79b7cb32014-05-08 20:00:35 -0700777 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500778 break;
Pekka Paalanena402b052013-05-22 18:03:10 +0300779 case BACKGROUND_SCALE_CROP:
780 s = (sx < sy) ? sx : sy;
781 /* align center */
782 tx = (im_w - s * allocation.width) * 0.5;
783 ty = (im_h - s * allocation.height) * 0.5;
784 cairo_matrix_init_translate(&matrix, tx, ty);
785 cairo_matrix_scale(&matrix, s, s);
786 cairo_pattern_set_matrix(pattern, &matrix);
Bill Spitzak79b7cb32014-05-08 20:00:35 -0700787 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
Pekka Paalanena402b052013-05-22 18:03:10 +0300788 break;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500789 case BACKGROUND_TILE:
790 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
791 break;
792 }
Pekka Paalanena402b052013-05-22 18:03:10 +0300793
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400794 cairo_set_source(cr, pattern);
795 cairo_pattern_destroy (pattern);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400796 cairo_surface_destroy(image);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500797 } else {
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300798 set_hex_color(cr, background->color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400799 }
800
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500801 cairo_paint(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400802 cairo_destroy(cr);
803 cairo_surface_destroy(surface);
Pekka Paalanen9564c752012-10-24 09:43:08 +0300804
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300805 background->painted = 1;
806 check_desktop_ready(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400807}
808
809static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100810background_configure(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800811 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400812 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100813 int32_t width, int32_t height)
814{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500815 struct background *background =
816 (struct background *) window_get_user_data(window);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100817
Kristian Høgsberg4598f962014-01-01 16:32:09 -0800818 widget_schedule_resize(background->widget, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100819}
820
821static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500822unlock_dialog_redraw_handler(struct widget *widget, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200823{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500824 struct unlock_dialog *dialog = data;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200825 struct rectangle allocation;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200826 cairo_surface_t *surface;
Alexander Larssonc584fa62013-05-22 14:41:32 +0200827 cairo_t *cr;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200828 cairo_pattern_t *pat;
829 double cx, cy, r, f;
830
Alexander Larssonc584fa62013-05-22 14:41:32 +0200831 cr = widget_cairo_create(widget);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500832
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500833 widget_get_allocation(dialog->widget, &allocation);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200834 cairo_rectangle(cr, allocation.x, allocation.y,
835 allocation.width, allocation.height);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200836 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
837 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500838 cairo_fill(cr);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200839
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500840 cairo_translate(cr, allocation.x, allocation.y);
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500841 if (dialog->button_focused)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200842 f = 1.0;
843 else
844 f = 0.7;
845
846 cx = allocation.width / 2.0;
847 cy = allocation.height / 2.0;
848 r = (cx < cy ? cx : cy) * 0.4;
849 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
850 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
851 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
852 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
853 cairo_set_source(cr, pat);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500854 cairo_pattern_destroy(pat);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200855 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
856 cairo_fill(cr);
857
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500858 widget_set_allocation(dialog->button,
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500859 allocation.x + cx - r,
860 allocation.y + cy - r, 2 * r, 2 * r);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200861
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200862 cairo_destroy(cr);
863
Alexander Larssonc584fa62013-05-22 14:41:32 +0200864 surface = window_get_surface(dialog->window);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200865 cairo_surface_destroy(surface);
866}
867
868static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500869unlock_dialog_button_handler(struct widget *widget,
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200870 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100871 uint32_t button,
872 enum wl_pointer_button_state state, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200873{
874 struct unlock_dialog *dialog = data;
875 struct desktop *desktop = dialog->desktop;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200876
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500877 if (button == BTN_LEFT) {
Daniel Stone4dbadb12012-05-30 16:31:51 +0100878 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
879 !dialog->closing) {
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200880 display_defer(desktop->display, &desktop->unlock_task);
881 dialog->closing = 1;
882 }
883 }
884}
885
886static void
Brian J Lovinc4df4082013-08-26 15:58:22 -0700887unlock_dialog_touch_down_handler(struct widget *widget, struct input *input,
888 uint32_t serial, uint32_t time, int32_t id,
889 float x, float y, void *data)
890{
891 struct unlock_dialog *dialog = data;
892
893 dialog->button_focused = 1;
894 widget_schedule_redraw(widget);
895}
896
897static void
898unlock_dialog_touch_up_handler(struct widget *widget, struct input *input,
899 uint32_t serial, uint32_t time, int32_t id,
900 void *data)
901{
902 struct unlock_dialog *dialog = data;
903 struct desktop *desktop = dialog->desktop;
904
905 dialog->button_focused = 0;
906 widget_schedule_redraw(widget);
907 display_defer(desktop->display, &desktop->unlock_task);
908 dialog->closing = 1;
909}
910
911static void
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200912unlock_dialog_keyboard_focus_handler(struct window *window,
913 struct input *device, void *data)
914{
915 window_schedule_redraw(window);
916}
917
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500918static int
Kristian Høgsbergee143232012-01-09 08:42:24 -0500919unlock_dialog_widget_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400920 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400921 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500922{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500923 struct unlock_dialog *dialog = data;
924
925 dialog->button_focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500926 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500927
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300928 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500929}
930
931static void
932unlock_dialog_widget_leave_handler(struct widget *widget,
933 struct input *input, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200934{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500935 struct unlock_dialog *dialog = data;
936
937 dialog->button_focused = 0;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500938 widget_schedule_redraw(widget);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200939}
940
941static struct unlock_dialog *
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500942unlock_dialog_create(struct desktop *desktop)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200943{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500944 struct display *display = desktop->display;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200945 struct unlock_dialog *dialog;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800946 struct wl_surface *surface;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200947
Peter Huttererf3d62272013-08-08 11:57:05 +1000948 dialog = xzalloc(sizeof *dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200949
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400950 dialog->window = window_create_custom(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500951 dialog->widget = window_frame_create(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200952 window_set_title(dialog->window, "Unlock your desktop");
953
954 window_set_user_data(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200955 window_set_keyboard_focus_handler(dialog->window,
956 unlock_dialog_keyboard_focus_handler);
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500957 dialog->button = widget_add_widget(dialog->widget, dialog);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500958 widget_set_redraw_handler(dialog->widget,
959 unlock_dialog_redraw_handler);
Kristian Høgsbergee143232012-01-09 08:42:24 -0500960 widget_set_enter_handler(dialog->button,
961 unlock_dialog_widget_enter_handler);
962 widget_set_leave_handler(dialog->button,
963 unlock_dialog_widget_leave_handler);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500964 widget_set_button_handler(dialog->button,
965 unlock_dialog_button_handler);
Brian J Lovinc4df4082013-08-26 15:58:22 -0700966 widget_set_touch_down_handler(dialog->button,
967 unlock_dialog_touch_down_handler);
968 widget_set_touch_up_handler(dialog->button,
969 unlock_dialog_touch_up_handler);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200970
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800971 surface = window_get_wl_surface(dialog->window);
972 weston_desktop_shell_set_lock_surface(desktop->shell, surface);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500973
Pekka Paalanen40e49ac2012-01-18 16:51:30 +0200974 window_schedule_resize(dialog->window, 260, 230);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200975
976 return dialog;
977}
978
979static void
980unlock_dialog_destroy(struct unlock_dialog *dialog)
981{
982 window_destroy(dialog->window);
983 free(dialog);
984}
985
986static void
987unlock_dialog_finish(struct task *task, uint32_t events)
988{
989 struct desktop *desktop =
Benjamin Franzked7759712011-11-22 12:38:48 +0100990 container_of(task, struct desktop, unlock_task);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200991
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800992 weston_desktop_shell_unlock(desktop->shell);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200993 unlock_dialog_destroy(desktop->unlock_dialog);
994 desktop->unlock_dialog = NULL;
995}
996
997static void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400998desktop_shell_configure(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800999 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001000 uint32_t edges,
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001001 struct wl_surface *surface,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001002 int32_t width, int32_t height)
1003{
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001004 struct window *window = wl_surface_get_user_data(surface);
Pekka Paalanen068ae942011-11-28 14:11:15 +02001005 struct surface *s = window_get_user_data(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001006
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001007 s->configure(data, desktop_shell, edges, window, width, height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001008}
1009
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001010static void
1011desktop_shell_prepare_lock_surface(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001012 struct weston_desktop_shell *desktop_shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001013{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001014 struct desktop *desktop = data;
1015
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001016 if (!desktop->locking) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001017 weston_desktop_shell_unlock(desktop->shell);
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +02001018 return;
1019 }
1020
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001021 if (!desktop->unlock_dialog) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001022 desktop->unlock_dialog = unlock_dialog_create(desktop);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001023 desktop->unlock_dialog->desktop = desktop;
1024 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001025}
1026
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001027static void
1028desktop_shell_grab_cursor(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001029 struct weston_desktop_shell *desktop_shell,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001030 uint32_t cursor)
1031{
1032 struct desktop *desktop = data;
1033
1034 switch (cursor) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001035 case WESTON_DESKTOP_SHELL_CURSOR_NONE:
Philipp Brüschweiler16d59d72012-08-24 15:43:55 +02001036 desktop->grab_cursor = CURSOR_BLANK;
1037 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001038 case WESTON_DESKTOP_SHELL_CURSOR_BUSY:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001039 desktop->grab_cursor = CURSOR_WATCH;
1040 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001041 case WESTON_DESKTOP_SHELL_CURSOR_MOVE:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001042 desktop->grab_cursor = CURSOR_DRAGGING;
1043 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001044 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001045 desktop->grab_cursor = CURSOR_TOP;
1046 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001047 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001048 desktop->grab_cursor = CURSOR_BOTTOM;
1049 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001050 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_LEFT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001051 desktop->grab_cursor = CURSOR_LEFT;
1052 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001053 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_RIGHT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001054 desktop->grab_cursor = CURSOR_RIGHT;
1055 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001056 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP_LEFT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001057 desktop->grab_cursor = CURSOR_TOP_LEFT;
1058 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001059 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP_RIGHT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001060 desktop->grab_cursor = CURSOR_TOP_RIGHT;
1061 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001062 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_LEFT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001063 desktop->grab_cursor = CURSOR_BOTTOM_LEFT;
1064 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001065 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_RIGHT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001066 desktop->grab_cursor = CURSOR_BOTTOM_RIGHT;
1067 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001068 case WESTON_DESKTOP_SHELL_CURSOR_ARROW:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001069 default:
1070 desktop->grab_cursor = CURSOR_LEFT_PTR;
1071 }
1072}
1073
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001074static const struct weston_desktop_shell_listener listener = {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001075 desktop_shell_configure,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001076 desktop_shell_prepare_lock_surface,
1077 desktop_shell_grab_cursor
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001078};
1079
U. Artie Eoff44874d92012-10-02 21:12:35 -07001080static void
1081background_destroy(struct background *background)
1082{
1083 widget_destroy(background->widget);
1084 window_destroy(background->window);
1085
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001086 free(background->image);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001087 free(background);
1088}
1089
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001090static struct background *
1091background_create(struct desktop *desktop)
1092{
1093 struct background *background;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001094 struct weston_config_section *s;
1095 char *type;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001096
Peter Huttererf3d62272013-08-08 11:57:05 +10001097 background = xzalloc(sizeof *background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001098 background->base.configure = background_configure;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001099 background->window = window_create_custom(desktop->display);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001100 background->widget = window_add_widget(background->window, background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001101 window_set_user_data(background->window, background);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001102 widget_set_redraw_handler(background->widget, background_draw);
Arnaud Vracfb754a02014-08-25 20:56:49 +02001103 widget_set_transparent(background->widget, 0);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001104
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001105 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
1106 weston_config_section_get_string(s, "background-image",
Kristian Høgsberg4c3661f2014-04-21 22:54:37 -07001107 &background->image, NULL);
Bryce Harringtone776f2a2016-07-14 18:28:03 -07001108 weston_config_section_get_color(s, "background-color",
1109 &background->color, 0x00000000);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001110
1111 weston_config_section_get_string(s, "background-type",
1112 &type, "tile");
U. Artie Eoff3c946772014-01-15 10:59:50 -08001113 if (type == NULL) {
1114 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
1115 exit(EXIT_FAILURE);
1116 }
1117
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001118 if (strcmp(type, "scale") == 0) {
1119 background->type = BACKGROUND_SCALE;
1120 } else if (strcmp(type, "scale-crop") == 0) {
1121 background->type = BACKGROUND_SCALE_CROP;
1122 } else if (strcmp(type, "tile") == 0) {
1123 background->type = BACKGROUND_TILE;
1124 } else {
1125 background->type = -1;
1126 fprintf(stderr, "invalid background-type: %s\n",
1127 type);
1128 }
1129
1130 free(type);
1131
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001132 return background;
1133}
1134
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001135static int
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001136grab_surface_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001137 float x, float y, void *data)
1138{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001139 struct desktop *desktop = data;
1140
1141 return desktop->grab_cursor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001142}
1143
1144static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001145grab_surface_destroy(struct desktop *desktop)
1146{
1147 widget_destroy(desktop->grab_widget);
1148 window_destroy(desktop->grab_window);
1149}
1150
1151static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001152grab_surface_create(struct desktop *desktop)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001153{
1154 struct wl_surface *s;
1155
Ander Conselvan de Oliveira07a91cd2012-07-16 14:15:50 +03001156 desktop->grab_window = window_create_custom(desktop->display);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001157 window_set_user_data(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001158
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001159 s = window_get_wl_surface(desktop->grab_window);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001160 weston_desktop_shell_set_grab_surface(desktop->shell, s);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001161
1162 desktop->grab_widget =
1163 window_add_widget(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001164 /* We set the allocation to 1x1 at 0,0 so the fake enter event
1165 * at 0,0 will go to this widget. */
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001166 widget_set_allocation(desktop->grab_widget, 0, 0, 1, 1);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001167
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001168 widget_set_enter_handler(desktop->grab_widget,
1169 grab_surface_enter_handler);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001170}
1171
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001172static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001173output_destroy(struct output *output)
1174{
1175 background_destroy(output->background);
Jonny Lambe67118c2014-08-12 15:07:51 +02001176 if (output->panel)
1177 panel_destroy(output->panel);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001178 wl_output_destroy(output->output);
1179 wl_list_remove(&output->link);
1180
1181 free(output);
1182}
1183
1184static void
1185desktop_destroy_outputs(struct desktop *desktop)
1186{
1187 struct output *tmp;
1188 struct output *output;
1189
1190 wl_list_for_each_safe(output, tmp, &desktop->outputs, link)
1191 output_destroy(output);
1192}
1193
1194static void
Alexander Larssonc584fa62013-05-22 14:41:32 +02001195output_handle_geometry(void *data,
1196 struct wl_output *wl_output,
1197 int x, int y,
1198 int physical_width,
1199 int physical_height,
1200 int subpixel,
1201 const char *make,
1202 const char *model,
1203 int transform)
1204{
1205 struct output *output = data;
1206
Jonny Lambe67118c2014-08-12 15:07:51 +02001207 if (output->panel)
1208 window_set_buffer_transform(output->panel->window, transform);
Alexander Larssonc584fa62013-05-22 14:41:32 +02001209 window_set_buffer_transform(output->background->window, transform);
1210}
1211
1212static void
1213output_handle_mode(void *data,
1214 struct wl_output *wl_output,
1215 uint32_t flags,
1216 int width,
1217 int height,
1218 int refresh)
1219{
1220}
1221
1222static void
1223output_handle_done(void *data,
1224 struct wl_output *wl_output)
1225{
1226}
1227
1228static void
1229output_handle_scale(void *data,
1230 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001231 int32_t scale)
Alexander Larssonc584fa62013-05-22 14:41:32 +02001232{
1233 struct output *output = data;
1234
Jonny Lambe67118c2014-08-12 15:07:51 +02001235 if (output->panel)
1236 window_set_buffer_scale(output->panel->window, scale);
Alexander Larssonc584fa62013-05-22 14:41:32 +02001237 window_set_buffer_scale(output->background->window, scale);
1238}
1239
1240static const struct wl_output_listener output_listener = {
1241 output_handle_geometry,
1242 output_handle_mode,
1243 output_handle_done,
1244 output_handle_scale
1245};
1246
1247static void
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001248output_init(struct output *output, struct desktop *desktop)
1249{
1250 struct wl_surface *surface;
1251
Quentin Glidic55d57012016-06-23 18:55:18 +02001252 if (desktop->want_panel) {
Jonny Lambe67118c2014-08-12 15:07:51 +02001253 output->panel = panel_create(desktop);
1254 surface = window_get_wl_surface(output->panel->window);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001255 weston_desktop_shell_set_panel(desktop->shell,
1256 output->output, surface);
Jonny Lambe67118c2014-08-12 15:07:51 +02001257 }
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001258
1259 output->background = background_create(desktop);
1260 surface = window_get_wl_surface(output->background->window);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001261 weston_desktop_shell_set_background(desktop->shell,
1262 output->output, surface);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001263}
1264
1265static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001266create_output(struct desktop *desktop, uint32_t id)
1267{
1268 struct output *output;
1269
Bryce Harrington0d1a6222016-02-11 16:42:49 -08001270 output = zalloc(sizeof *output);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001271 if (!output)
1272 return;
1273
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001274 output->output =
Alexander Larssonc584fa62013-05-22 14:41:32 +02001275 display_bind(desktop->display, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001276 output->server_output_id = id;
Alexander Larssonc584fa62013-05-22 14:41:32 +02001277
1278 wl_output_add_listener(output->output, &output_listener, output);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001279
1280 wl_list_insert(&desktop->outputs, &output->link);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001281
1282 /* On start up we may process an output global before the shell global
1283 * in which case we can't create the panel and background just yet */
1284 if (desktop->shell)
1285 output_init(output, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001286}
1287
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001288static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001289global_handler(struct display *display, uint32_t id,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001290 const char *interface, uint32_t version, void *data)
1291{
1292 struct desktop *desktop = data;
1293
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001294 if (!strcmp(interface, "weston_desktop_shell")) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001295 desktop->shell = display_bind(desktop->display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001296 id,
1297 &weston_desktop_shell_interface,
1298 1);
1299 weston_desktop_shell_add_listener(desktop->shell,
1300 &listener,
1301 desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001302 } else if (!strcmp(interface, "wl_output")) {
1303 create_output(desktop, id);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001304 }
1305}
1306
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001307static void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001308global_handler_remove(struct display *display, uint32_t id,
1309 const char *interface, uint32_t version, void *data)
1310{
1311 struct desktop *desktop = data;
1312 struct output *output;
1313
1314 if (!strcmp(interface, "wl_output")) {
1315 wl_list_for_each(output, &desktop->outputs, link) {
1316 if (output->server_output_id == id) {
1317 output_destroy(output);
1318 break;
1319 }
1320 }
1321 }
1322}
1323
1324static void
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001325panel_add_launchers(struct panel *panel, struct desktop *desktop)
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001326{
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001327 struct weston_config_section *s;
1328 char *icon, *path;
1329 const char *name;
1330 int count;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001331
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001332 count = 0;
1333 s = NULL;
1334 while (weston_config_next_section(desktop->config, &s, &name)) {
1335 if (strcmp(name, "launcher") != 0)
1336 continue;
1337
1338 weston_config_section_get_string(s, "icon", &icon, NULL);
1339 weston_config_section_get_string(s, "path", &path, NULL);
1340
1341 if (icon != NULL && path != NULL) {
1342 panel_add_launcher(panel, icon, path);
Rob Bradford09252d42013-07-26 16:29:45 +01001343 count++;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001344 } else {
1345 fprintf(stderr, "invalid launcher section\n");
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001346 }
1347
1348 free(icon);
1349 free(path);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001350 }
1351
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001352 if (count == 0) {
1353 /* add default launcher */
1354 panel_add_launcher(panel,
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001355 DATADIR "/weston/terminal.png",
Rodney Lorrimar99ff01b2012-02-29 17:31:03 +01001356 BINDIR "/weston-terminal");
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001357 }
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001358}
1359
Quentin Glidic55d57012016-06-23 18:55:18 +02001360static void
1361parse_panel_position(struct desktop *desktop, struct weston_config_section *s)
1362{
1363 char *position;
1364
1365 weston_config_section_get_string(s, "panel-position", &position, "top");
1366 if (strcmp(position, "top") == 0)
1367 desktop->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
1368 else if (strcmp(position, "bottom") == 0)
1369 desktop->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM;
1370 else if (strcmp(position, "left") == 0)
1371 desktop->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT;
1372 else if (strcmp(position, "right") == 0)
1373 desktop->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT;
1374 else
1375 fprintf(stderr, "Wrong panel position: %s\n", position);
1376 free(position);
1377
1378 if (desktop->panel_position == WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP
Quentin Glidice7ed60f2016-06-23 18:55:24 +02001379 || desktop->panel_position == WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM
1380 || desktop->panel_position == WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT
1381 || desktop->panel_position == WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Quentin Glidic55d57012016-06-23 18:55:18 +02001382 desktop->want_panel = 1;
Quentin Glidic55d57012016-06-23 18:55:18 +02001383 }
1384}
1385
Quentin Glidic3e37b342016-06-23 18:55:22 +02001386static void
1387parse_clock_format(struct desktop *desktop, struct weston_config_section *s)
1388{
1389 char *clock_format;
1390
1391 weston_config_section_get_string(s, "clock-format", &clock_format, "");
1392 if (strcmp(clock_format, "minutes") == 0)
1393 desktop->clock_format = CLOCK_FORMAT_MINUTES;
1394 else if (strcmp(clock_format, "seconds") == 0)
1395 desktop->clock_format = CLOCK_FORMAT_SECONDS;
1396 else if (strcmp(clock_format, "none") == 0)
1397 desktop->clock_format = CLOCK_FORMAT_NONE;
1398 else
1399 desktop->clock_format = DEFAULT_CLOCK_FORMAT;
1400 free(clock_format);
1401}
1402
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001403int main(int argc, char *argv[])
1404{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001405 struct desktop desktop = { 0 };
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001406 struct output *output;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001407 struct weston_config_section *s;
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001408 const char *config_file;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001409
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001410 desktop.unlock_task.run = unlock_dialog_finish;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001411 wl_list_init(&desktop.outputs);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001412
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001413 config_file = weston_config_get_name_from_env();
1414 desktop.config = weston_config_parse(config_file);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001415 s = weston_config_get_section(desktop.config, "shell", NULL, NULL);
1416 weston_config_section_get_bool(s, "locking", &desktop.locking, 1);
Quentin Glidic55d57012016-06-23 18:55:18 +02001417 parse_panel_position(&desktop, s);
Quentin Glidic3e37b342016-06-23 18:55:22 +02001418 parse_clock_format(&desktop, s);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001419
Kristian Høgsberg4172f662013-02-20 15:27:49 -05001420 desktop.display = display_create(&argc, argv);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001421 if (desktop.display == NULL) {
1422 fprintf(stderr, "failed to create display: %m\n");
1423 return -1;
1424 }
1425
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001426 display_set_user_data(desktop.display, &desktop);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001427 display_set_global_handler(desktop.display, global_handler);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001428 display_set_global_handler_remove(desktop.display, global_handler_remove);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001429
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001430 /* Create panel and background for outputs processed before the shell
1431 * global interface was processed */
Quentin Glidic55d57012016-06-23 18:55:18 +02001432 if (desktop.want_panel)
1433 weston_desktop_shell_set_panel_position(desktop.shell, desktop.panel_position);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001434 wl_list_for_each(output, &desktop.outputs, link)
1435 if (!output->panel)
1436 output_init(output, &desktop);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001437
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001438 grab_surface_create(&desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001439
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001440 signal(SIGCHLD, sigchild_handler);
1441
1442 display_run(desktop.display);
1443
U. Artie Eoff44874d92012-10-02 21:12:35 -07001444 /* Cleanup */
1445 grab_surface_destroy(&desktop);
1446 desktop_destroy_outputs(&desktop);
1447 if (desktop.unlock_dialog)
1448 unlock_dialog_destroy(desktop.unlock_dialog);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001449 weston_desktop_shell_destroy(desktop.shell);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001450 display_destroy(desktop.display);
1451
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001452 return 0;
1453}