blob: b9b9815fbc0e7232ae5020add6dae173881a3512 [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>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040043
Pekka Paalanen50719bc2011-11-22 14:18:50 +020044#include <wayland-client.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040045#include "window.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070046#include "shared/cairo-util.h"
47#include "shared/config-parser.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070048#include "shared/helpers.h"
Bryce Harrington0d1a6222016-02-11 16:42:49 -080049#include "shared/zalloc.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040050
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080051#include "weston-desktop-shell-client-protocol.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040052
Armin Krezovićc6a55db2016-03-10 18:02:34 +010053#define DEFAULT_CLOCK_FORMAT CLOCK_FORMAT_MINUTES
54
Pekka Paalanenb6df4f72012-08-03 14:39:15 +030055extern char **environ; /* defined by libc */
56
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040057struct desktop {
58 struct display *display;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080059 struct weston_desktop_shell *shell;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +020060 struct unlock_dialog *unlock_dialog;
61 struct task unlock_task;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010062 struct wl_list outputs;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040063
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030064 struct window *grab_window;
65 struct widget *grab_widget;
66
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +030067 struct weston_config *config;
68 int locking;
69
Scott Moreauec116022012-07-22 18:23:52 -060070 enum cursor_type grab_cursor;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030071
72 int painted;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010073};
74
75struct surface {
76 void (*configure)(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080077 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040078 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010079 int32_t width, int32_t height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040080};
81
82struct panel {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010083 struct surface base;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040084 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050085 struct widget *widget;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050086 struct wl_list launcher_list;
Martin Minarik1e51a872012-06-08 00:39:11 +020087 struct panel_clock *clock;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030088 int painted;
Armin Krezovićc6a55db2016-03-10 18:02:34 +010089 int clock_format;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +030090 uint32_t color;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040091};
92
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010093struct background {
94 struct surface base;
95 struct window *window;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050096 struct widget *widget;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030097 int painted;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +030098
99 char *image;
100 int type;
101 uint32_t color;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100102};
103
104struct output {
105 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800106 uint32_t server_output_id;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100107 struct wl_list link;
108
109 struct panel *panel;
110 struct background *background;
111};
112
Kristian Høgsberg53880802012-01-09 11:16:50 -0500113struct panel_launcher {
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500114 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400115 struct panel *panel;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400116 cairo_surface_t *icon;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500117 int focused, pressed;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400118 char *path;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500119 struct wl_list link;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400120 struct wl_array envp;
121 struct wl_array argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400122};
123
Martin Minarik1e51a872012-06-08 00:39:11 +0200124struct panel_clock {
125 struct widget *widget;
126 struct panel *panel;
Martin Minarik1e51a872012-06-08 00:39:11 +0200127 struct task clock_task;
128 int clock_fd;
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100129 char *format_string;
130 time_t refresh_timer;
Martin Minarik1e51a872012-06-08 00:39:11 +0200131};
132
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200133struct unlock_dialog {
134 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500135 struct widget *widget;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500136 struct widget *button;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500137 int button_focused;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200138 int closing;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200139 struct desktop *desktop;
140};
141
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300142static void
143panel_add_launchers(struct panel *panel, struct desktop *desktop);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500144
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400145static void
146sigchild_handler(int s)
147{
148 int status;
149 pid_t pid;
150
151 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
152 fprintf(stderr, "child %d exited\n", pid);
153}
154
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300155static int
156is_desktop_painted(struct desktop *desktop)
157{
158 struct output *output;
159
160 wl_list_for_each(output, &desktop->outputs, link) {
161 if (output->panel && !output->panel->painted)
162 return 0;
163 if (output->background && !output->background->painted)
164 return 0;
165 }
166
167 return 1;
168}
169
170static void
171check_desktop_ready(struct window *window)
172{
173 struct display *display;
174 struct desktop *desktop;
175
176 display = window_get_display(window);
177 desktop = display_get_user_data(display);
178
179 if (!desktop->painted && is_desktop_painted(desktop)) {
180 desktop->painted = 1;
181
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800182 weston_desktop_shell_desktop_ready(desktop->shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300183 }
184}
185
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400186static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500187panel_launcher_activate(struct panel_launcher *widget)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400188{
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400189 char **argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400190 pid_t pid;
191
192 pid = fork();
193 if (pid < 0) {
194 fprintf(stderr, "fork failed: %m\n");
195 return;
196 }
197
198 if (pid)
199 return;
Benjamin Franzked7759712011-11-22 12:38:48 +0100200
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400201 argv = widget->argv.data;
202 if (execve(argv[0], argv, widget->envp.data) < 0) {
203 fprintf(stderr, "execl '%s' failed: %m\n", argv[0]);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400204 exit(1);
205 }
206}
207
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400208static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500209panel_launcher_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400210{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500211 struct panel_launcher *launcher = data;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500212 struct rectangle allocation;
213 cairo_t *cr;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400214
Alexander Larssonc584fa62013-05-22 14:41:32 +0200215 cr = widget_cairo_create(launcher->panel->widget);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500216
217 widget_get_allocation(widget, &allocation);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500218 if (launcher->pressed) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500219 allocation.x++;
220 allocation.y++;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400221 }
222
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500223 cairo_set_source_surface(cr, launcher->icon,
224 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400225 cairo_paint(cr);
226
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500227 if (launcher->focused) {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400228 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500229 cairo_mask_surface(cr, launcher->icon,
230 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400231 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400232
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500233 cairo_destroy(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400234}
235
Tiago Vignatti61500722012-05-23 22:06:28 +0300236static int
237panel_launcher_motion_handler(struct widget *widget, struct input *input,
238 uint32_t time, float x, float y, void *data)
239{
240 struct panel_launcher *launcher = data;
241
242 widget_set_tooltip(widget, basename((char *)launcher->path), x, y);
243
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300244 return CURSOR_LEFT_PTR;
Tiago Vignatti61500722012-05-23 22:06:28 +0300245}
246
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400247static void
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500248set_hex_color(cairo_t *cr, uint32_t color)
249{
Michael Vetter2a18a522015-05-15 17:17:47 +0200250 cairo_set_source_rgba(cr,
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500251 ((color >> 16) & 0xff) / 255.0,
252 ((color >> 8) & 0xff) / 255.0,
253 ((color >> 0) & 0xff) / 255.0,
254 ((color >> 24) & 0xff) / 255.0);
255}
256
257static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500258panel_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400259{
260 cairo_surface_t *surface;
261 cairo_t *cr;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500262 struct panel *panel = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400263
Alexander Larssonc584fa62013-05-22 14:41:32 +0200264 cr = widget_cairo_create(panel->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400265 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300266 set_hex_color(cr, panel->color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400267 cairo_paint(cr);
268
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400269 cairo_destroy(cr);
Alexander Larssonc584fa62013-05-22 14:41:32 +0200270 surface = window_get_surface(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400271 cairo_surface_destroy(surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300272 panel->painted = 1;
273 check_desktop_ready(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400274}
275
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500276static int
Kristian Høgsberg53880802012-01-09 11:16:50 -0500277panel_launcher_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400278 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500279{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500280 struct panel_launcher *launcher = data;
281
282 launcher->focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500283 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500284
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300285 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500286}
287
288static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500289panel_launcher_leave_handler(struct widget *widget,
290 struct input *input, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400291{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500292 struct panel_launcher *launcher = data;
293
294 launcher->focused = 0;
Tiago Vignatti61500722012-05-23 22:06:28 +0300295 widget_destroy_tooltip(widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500296 widget_schedule_redraw(widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400297}
298
299static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500300panel_launcher_button_handler(struct widget *widget,
301 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100302 uint32_t button,
303 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400304{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500305 struct panel_launcher *launcher;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400306
Kristian Høgsberg53880802012-01-09 11:16:50 -0500307 launcher = widget_get_user_data(widget);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500308 widget_schedule_redraw(widget);
Daniel Stone4dbadb12012-05-30 16:31:51 +0100309 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg53880802012-01-09 11:16:50 -0500310 panel_launcher_activate(launcher);
Rusty Lynch4384a242013-08-08 21:28:22 -0700311
312}
313
314static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700315panel_launcher_touch_down_handler(struct widget *widget, struct input *input,
316 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch4384a242013-08-08 21:28:22 -0700317 float x, float y, void *data)
318{
319 struct panel_launcher *launcher;
320
321 launcher = widget_get_user_data(widget);
322 launcher->focused = 1;
323 widget_schedule_redraw(widget);
324}
325
326static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700327panel_launcher_touch_up_handler(struct widget *widget, struct input *input,
Michael Vetter2a18a522015-05-15 17:17:47 +0200328 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch1084da52013-08-15 09:10:08 -0700329 void *data)
Rusty Lynch4384a242013-08-08 21:28:22 -0700330{
331 struct panel_launcher *launcher;
332
333 launcher = widget_get_user_data(widget);
334 launcher->focused = 0;
335 widget_schedule_redraw(widget);
336 panel_launcher_activate(launcher);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500337}
338
Martin Minarik1e51a872012-06-08 00:39:11 +0200339static void
340clock_func(struct task *task, uint32_t events)
341{
342 struct panel_clock *clock =
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400343 container_of(task, struct panel_clock, clock_task);
Kristian Høgsberg70226bb2012-06-08 16:54:52 -0400344 uint64_t exp;
Martin Minarik1e51a872012-06-08 00:39:11 +0200345
Martin Olsson8df662a2012-07-08 03:03:47 +0200346 if (read(clock->clock_fd, &exp, sizeof exp) != sizeof exp)
347 abort();
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400348 widget_schedule_redraw(clock->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200349}
350
351static void
352panel_clock_redraw_handler(struct widget *widget, void *data)
353{
Martin Minarik1e51a872012-06-08 00:39:11 +0200354 struct panel_clock *clock = data;
355 cairo_t *cr;
356 struct rectangle allocation;
357 cairo_text_extents_t extents;
358 cairo_font_extents_t font_extents;
Martin Minarik1e51a872012-06-08 00:39:11 +0200359 time_t rawtime;
360 struct tm * timeinfo;
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400361 char string[128];
Martin Minarik1e51a872012-06-08 00:39:11 +0200362
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400363 time(&rawtime);
364 timeinfo = localtime(&rawtime);
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100365 strftime(string, sizeof string, clock->format_string, timeinfo);
Martin Minarik1e51a872012-06-08 00:39:11 +0200366
367 widget_get_allocation(widget, &allocation);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400368 if (allocation.width == 0)
369 return;
Martin Minarik1e51a872012-06-08 00:39:11 +0200370
Alexander Larssonc584fa62013-05-22 14:41:32 +0200371 cr = widget_cairo_create(clock->panel->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200372 cairo_select_font_face(cr, "sans",
373 CAIRO_FONT_SLANT_NORMAL,
374 CAIRO_FONT_WEIGHT_NORMAL);
375 cairo_set_font_size(cr, 14);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400376 cairo_text_extents(cr, string, &extents);
Martin Minarik1e51a872012-06-08 00:39:11 +0200377 cairo_font_extents (cr, &font_extents);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400378 cairo_move_to(cr, allocation.x + 5,
379 allocation.y + 3 * (allocation.height >> 2) + 1);
380 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400381 cairo_show_text(cr, string);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400382 cairo_move_to(cr, allocation.x + 4,
383 allocation.y + 3 * (allocation.height >> 2));
384 cairo_set_source_rgb(cr, 1, 1, 1);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400385 cairo_show_text(cr, string);
Martin Minarik1e51a872012-06-08 00:39:11 +0200386 cairo_destroy(cr);
387}
388
389static int
390clock_timer_reset(struct panel_clock *clock)
391{
392 struct itimerspec its;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400393
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100394 its.it_interval.tv_sec = clock->refresh_timer;
Martin Minarik1e51a872012-06-08 00:39:11 +0200395 its.it_interval.tv_nsec = 0;
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100396 its.it_value.tv_sec = clock->refresh_timer;
Martin Minarik1e51a872012-06-08 00:39:11 +0200397 its.it_value.tv_nsec = 0;
398 if (timerfd_settime(clock->clock_fd, 0, &its, NULL) < 0) {
399 fprintf(stderr, "could not set timerfd\n: %m");
400 return -1;
401 }
402
403 return 0;
404}
405
406static void
U. Artie Eoff44874d92012-10-02 21:12:35 -0700407panel_destroy_clock(struct panel_clock *clock)
408{
409 widget_destroy(clock->widget);
410
411 close(clock->clock_fd);
412
413 free(clock);
414}
415
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100416enum {
417 CLOCK_FORMAT_MINUTES,
418 CLOCK_FORMAT_SECONDS,
419 CLOCK_FORMAT_NONE
420};
421
U. Artie Eoff44874d92012-10-02 21:12:35 -0700422static void
Martin Minarik1e51a872012-06-08 00:39:11 +0200423panel_add_clock(struct panel *panel)
424{
425 struct panel_clock *clock;
426 int timerfd;
427
428 timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
429 if (timerfd < 0) {
430 fprintf(stderr, "could not create timerfd\n: %m");
431 return;
432 }
433
Peter Huttererf3d62272013-08-08 11:57:05 +1000434 clock = xzalloc(sizeof *clock);
Martin Minarik1e51a872012-06-08 00:39:11 +0200435 clock->panel = panel;
436 panel->clock = clock;
437 clock->clock_fd = timerfd;
438
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100439 switch (panel->clock_format) {
440 case CLOCK_FORMAT_MINUTES:
441 clock->format_string = "%a %b %d, %I:%M %p";
442 clock->refresh_timer = 60;
443 break;
444 case CLOCK_FORMAT_SECONDS:
445 clock->format_string = "%a %b %d, %I:%M:%S %p";
446 clock->refresh_timer = 1;
447 break;
448 }
449
Martin Minarik1e51a872012-06-08 00:39:11 +0200450 clock->clock_task.run = clock_func;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400451 display_watch_fd(window_get_display(panel->window), clock->clock_fd,
452 EPOLLIN, &clock->clock_task);
Martin Minarik1e51a872012-06-08 00:39:11 +0200453 clock_timer_reset(clock);
454
455 clock->widget = widget_add_widget(panel->widget, clock);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400456 widget_set_redraw_handler(clock->widget, panel_clock_redraw_handler);
Martin Minarik1e51a872012-06-08 00:39:11 +0200457}
458
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500459static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500460panel_resize_handler(struct widget *widget,
461 int32_t width, int32_t height, void *data)
462{
463 struct panel_launcher *launcher;
464 struct panel *panel = data;
465 int x, y, w, h;
Michael Vetter2a18a522015-05-15 17:17:47 +0200466
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500467 x = 10;
468 y = 16;
469 wl_list_for_each(launcher, &panel->launcher_list, link) {
470 w = cairo_image_surface_get_width(launcher->icon);
471 h = cairo_image_surface_get_height(launcher->icon);
472 widget_set_allocation(launcher->widget,
473 x, y - h / 2, w + 1, h + 1);
474 x += w + 10;
475 }
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100476
477 h = 20;
478
479 if (panel->clock_format == CLOCK_FORMAT_SECONDS)
480 w = 190;
481 else /* CLOCK_FORMAT_MINUTES */
482 w = 170;
Pekka Paalanen01b17252012-06-12 17:42:26 +0300483
484 if (panel->clock)
485 widget_set_allocation(panel->clock->widget,
486 width - w - 8, y - h / 2, w + 1, h + 1);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500487}
488
489static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100490panel_configure(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800491 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400492 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100493 int32_t width, int32_t height)
494{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500495 struct surface *surface = window_get_user_data(window);
496 struct panel *panel = container_of(surface, struct panel, base);
497
498 window_schedule_resize(panel->window, width, 32);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100499}
500
U. Artie Eoff44874d92012-10-02 21:12:35 -0700501static void
502panel_destroy_launcher(struct panel_launcher *launcher)
503{
504 wl_array_release(&launcher->argv);
505 wl_array_release(&launcher->envp);
506
507 free(launcher->path);
508
509 cairo_surface_destroy(launcher->icon);
510
511 widget_destroy(launcher->widget);
512 wl_list_remove(&launcher->link);
513
514 free(launcher);
515}
516
517static void
518panel_destroy(struct panel *panel)
519{
520 struct panel_launcher *tmp;
521 struct panel_launcher *launcher;
522
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100523 if (panel->clock)
524 panel_destroy_clock(panel->clock);
U. Artie Eoff44874d92012-10-02 21:12:35 -0700525
526 wl_list_for_each_safe(launcher, tmp, &panel->launcher_list, link)
527 panel_destroy_launcher(launcher);
528
529 widget_destroy(panel->widget);
530 window_destroy(panel->window);
531
532 free(panel);
533}
534
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400535static struct panel *
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300536panel_create(struct desktop *desktop)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400537{
538 struct panel *panel;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300539 struct weston_config_section *s;
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100540 char *clock_format_option = NULL;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400541
Peter Huttererf3d62272013-08-08 11:57:05 +1000542 panel = xzalloc(sizeof *panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400543
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100544 panel->base.configure = panel_configure;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300545 panel->window = window_create_custom(desktop->display);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500546 panel->widget = window_add_widget(panel->window, panel);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500547 wl_list_init(&panel->launcher_list);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400548
549 window_set_title(panel->window, "panel");
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400550 window_set_user_data(panel->window, panel);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500551
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500552 widget_set_redraw_handler(panel->widget, panel_redraw_handler);
553 widget_set_resize_handler(panel->widget, panel_resize_handler);
Michael Vetter2a18a522015-05-15 17:17:47 +0200554
Armin Krezovićc6a55db2016-03-10 18:02:34 +0100555 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
556 weston_config_section_get_string(s, "clock-format", &clock_format_option, "");
557
558 if (strcmp(clock_format_option, "minutes") == 0)
559 panel->clock_format = CLOCK_FORMAT_MINUTES;
560 else if (strcmp(clock_format_option, "seconds") == 0)
561 panel->clock_format = CLOCK_FORMAT_SECONDS;
562 else if (strcmp(clock_format_option, "none") == 0)
563 panel->clock_format = CLOCK_FORMAT_NONE;
564 else
565 panel->clock_format = DEFAULT_CLOCK_FORMAT;
566
567 if (panel->clock_format != CLOCK_FORMAT_NONE)
568 panel_add_clock(panel);
569
570 free (clock_format_option);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400571
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300572 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
573 weston_config_section_get_uint(s, "panel-color",
574 &panel->color, 0xaa000000);
575
576 panel_add_launchers(panel, desktop);
577
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400578 return panel;
579}
580
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200581static cairo_surface_t *
582load_icon_or_fallback(const char *icon)
583{
584 cairo_surface_t *surface = cairo_image_surface_create_from_png(icon);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200585 cairo_status_t status;
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200586 cairo_t *cr;
587
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200588 status = cairo_surface_status(surface);
589 if (status == CAIRO_STATUS_SUCCESS)
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200590 return surface;
591
592 cairo_surface_destroy(surface);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200593 fprintf(stderr, "ERROR loading icon from file '%s', error: '%s'\n",
594 icon, cairo_status_to_string(status));
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200595
596 /* draw fallback icon */
597 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
598 20, 20);
599 cr = cairo_create(surface);
600
601 cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1);
602 cairo_paint(cr);
603
604 cairo_set_source_rgba(cr, 0, 0, 0, 1);
605 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
606 cairo_rectangle(cr, 0, 0, 20, 20);
607 cairo_move_to(cr, 4, 4);
608 cairo_line_to(cr, 16, 16);
609 cairo_move_to(cr, 4, 16);
610 cairo_line_to(cr, 16, 4);
611 cairo_stroke(cr);
612
613 cairo_destroy(cr);
614
615 return surface;
616}
617
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400618static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500619panel_add_launcher(struct panel *panel, const char *icon, const char *path)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400620{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500621 struct panel_launcher *launcher;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400622 char *start, *p, *eq, **ps;
623 int i, j, k;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400624
Peter Huttererf3d62272013-08-08 11:57:05 +1000625 launcher = xzalloc(sizeof *launcher);
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200626 launcher->icon = load_icon_or_fallback(icon);
U. Artie Eoff3c946772014-01-15 10:59:50 -0800627 launcher->path = xstrdup(path);
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400628
629 wl_array_init(&launcher->envp);
630 wl_array_init(&launcher->argv);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300631 for (i = 0; environ[i]; i++) {
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400632 ps = wl_array_add(&launcher->envp, sizeof *ps);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300633 *ps = environ[i];
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400634 }
635 j = 0;
636
637 start = launcher->path;
638 while (*start) {
639 for (p = start, eq = NULL; *p && !isspace(*p); p++)
640 if (*p == '=')
641 eq = p;
642
643 if (eq && j == 0) {
644 ps = launcher->envp.data;
645 for (k = 0; k < i; k++)
646 if (strncmp(ps[k], start, eq - start) == 0) {
647 ps[k] = start;
648 break;
649 }
650 if (k == i) {
651 ps = wl_array_add(&launcher->envp, sizeof *ps);
652 *ps = start;
653 i++;
654 }
655 } else {
656 ps = wl_array_add(&launcher->argv, sizeof *ps);
657 *ps = start;
658 j++;
659 }
660
661 while (*p && isspace(*p))
662 *p++ = '\0';
663
664 start = p;
665 }
666
667 ps = wl_array_add(&launcher->envp, sizeof *ps);
668 *ps = NULL;
669 ps = wl_array_add(&launcher->argv, sizeof *ps);
670 *ps = NULL;
671
Kristian Høgsberg53880802012-01-09 11:16:50 -0500672 launcher->panel = panel;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500673 wl_list_insert(panel->launcher_list.prev, &launcher->link);
674
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500675 launcher->widget = widget_add_widget(panel->widget, launcher);
Kristian Høgsberg53880802012-01-09 11:16:50 -0500676 widget_set_enter_handler(launcher->widget,
677 panel_launcher_enter_handler);
678 widget_set_leave_handler(launcher->widget,
679 panel_launcher_leave_handler);
680 widget_set_button_handler(launcher->widget,
681 panel_launcher_button_handler);
Rusty Lynch4384a242013-08-08 21:28:22 -0700682 widget_set_touch_down_handler(launcher->widget,
683 panel_launcher_touch_down_handler);
684 widget_set_touch_up_handler(launcher->widget,
685 panel_launcher_touch_up_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500686 widget_set_redraw_handler(launcher->widget,
687 panel_launcher_redraw_handler);
Tiago Vignatti61500722012-05-23 22:06:28 +0300688 widget_set_motion_handler(launcher->widget,
689 panel_launcher_motion_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400690}
691
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500692enum {
693 BACKGROUND_SCALE,
Pekka Paalanena402b052013-05-22 18:03:10 +0300694 BACKGROUND_SCALE_CROP,
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500695 BACKGROUND_TILE
696};
697
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400698static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500699background_draw(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400700{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500701 struct background *background = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400702 cairo_surface_t *surface, *image;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400703 cairo_pattern_t *pattern;
704 cairo_matrix_t matrix;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400705 cairo_t *cr;
Pekka Paalanena402b052013-05-22 18:03:10 +0300706 double im_w, im_h;
707 double sx, sy, s;
708 double tx, ty;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500709 struct rectangle allocation;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400710
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500711 surface = window_get_surface(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400712
Alexander Larssonc584fa62013-05-22 14:41:32 +0200713 cr = widget_cairo_create(background->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400714 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
715 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
716 cairo_paint(cr);
717
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500718 widget_get_allocation(widget, &allocation);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500719 image = NULL;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300720 if (background->image)
721 image = load_cairo_surface(background->image);
Kristian Høgsberg4c3661f2014-04-21 22:54:37 -0700722 else if (background->color == 0)
723 image = load_cairo_surface(DATADIR "/weston/pattern.png");
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500724
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300725 if (image && background->type != -1) {
Pekka Paalanena402b052013-05-22 18:03:10 +0300726 im_w = cairo_image_surface_get_width(image);
727 im_h = cairo_image_surface_get_height(image);
728 sx = im_w / allocation.width;
729 sy = im_h / allocation.height;
730
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400731 pattern = cairo_pattern_create_for_surface(image);
Pekka Paalanena402b052013-05-22 18:03:10 +0300732
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300733 switch (background->type) {
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500734 case BACKGROUND_SCALE:
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500735 cairo_matrix_init_scale(&matrix, sx, sy);
736 cairo_pattern_set_matrix(pattern, &matrix);
Bill Spitzak79b7cb32014-05-08 20:00:35 -0700737 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500738 break;
Pekka Paalanena402b052013-05-22 18:03:10 +0300739 case BACKGROUND_SCALE_CROP:
740 s = (sx < sy) ? sx : sy;
741 /* align center */
742 tx = (im_w - s * allocation.width) * 0.5;
743 ty = (im_h - s * allocation.height) * 0.5;
744 cairo_matrix_init_translate(&matrix, tx, ty);
745 cairo_matrix_scale(&matrix, s, s);
746 cairo_pattern_set_matrix(pattern, &matrix);
Bill Spitzak79b7cb32014-05-08 20:00:35 -0700747 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
Pekka Paalanena402b052013-05-22 18:03:10 +0300748 break;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500749 case BACKGROUND_TILE:
750 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
751 break;
752 }
Pekka Paalanena402b052013-05-22 18:03:10 +0300753
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400754 cairo_set_source(cr, pattern);
755 cairo_pattern_destroy (pattern);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400756 cairo_surface_destroy(image);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500757 } else {
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300758 set_hex_color(cr, background->color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400759 }
760
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500761 cairo_paint(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400762 cairo_destroy(cr);
763 cairo_surface_destroy(surface);
Pekka Paalanen9564c752012-10-24 09:43:08 +0300764
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300765 background->painted = 1;
766 check_desktop_ready(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400767}
768
769static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100770background_configure(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800771 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400772 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100773 int32_t width, int32_t height)
774{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500775 struct background *background =
776 (struct background *) window_get_user_data(window);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100777
Kristian Høgsberg4598f962014-01-01 16:32:09 -0800778 widget_schedule_resize(background->widget, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100779}
780
781static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500782unlock_dialog_redraw_handler(struct widget *widget, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200783{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500784 struct unlock_dialog *dialog = data;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200785 struct rectangle allocation;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200786 cairo_surface_t *surface;
Alexander Larssonc584fa62013-05-22 14:41:32 +0200787 cairo_t *cr;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200788 cairo_pattern_t *pat;
789 double cx, cy, r, f;
790
Alexander Larssonc584fa62013-05-22 14:41:32 +0200791 cr = widget_cairo_create(widget);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500792
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500793 widget_get_allocation(dialog->widget, &allocation);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200794 cairo_rectangle(cr, allocation.x, allocation.y,
795 allocation.width, allocation.height);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200796 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
797 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500798 cairo_fill(cr);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200799
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500800 cairo_translate(cr, allocation.x, allocation.y);
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500801 if (dialog->button_focused)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200802 f = 1.0;
803 else
804 f = 0.7;
805
806 cx = allocation.width / 2.0;
807 cy = allocation.height / 2.0;
808 r = (cx < cy ? cx : cy) * 0.4;
809 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
810 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
811 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
812 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
813 cairo_set_source(cr, pat);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500814 cairo_pattern_destroy(pat);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200815 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
816 cairo_fill(cr);
817
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500818 widget_set_allocation(dialog->button,
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500819 allocation.x + cx - r,
820 allocation.y + cy - r, 2 * r, 2 * r);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200821
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200822 cairo_destroy(cr);
823
Alexander Larssonc584fa62013-05-22 14:41:32 +0200824 surface = window_get_surface(dialog->window);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200825 cairo_surface_destroy(surface);
826}
827
828static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500829unlock_dialog_button_handler(struct widget *widget,
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200830 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100831 uint32_t button,
832 enum wl_pointer_button_state state, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200833{
834 struct unlock_dialog *dialog = data;
835 struct desktop *desktop = dialog->desktop;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200836
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500837 if (button == BTN_LEFT) {
Daniel Stone4dbadb12012-05-30 16:31:51 +0100838 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
839 !dialog->closing) {
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200840 display_defer(desktop->display, &desktop->unlock_task);
841 dialog->closing = 1;
842 }
843 }
844}
845
846static void
Brian J Lovinc4df4082013-08-26 15:58:22 -0700847unlock_dialog_touch_down_handler(struct widget *widget, struct input *input,
848 uint32_t serial, uint32_t time, int32_t id,
849 float x, float y, void *data)
850{
851 struct unlock_dialog *dialog = data;
852
853 dialog->button_focused = 1;
854 widget_schedule_redraw(widget);
855}
856
857static void
858unlock_dialog_touch_up_handler(struct widget *widget, struct input *input,
859 uint32_t serial, uint32_t time, int32_t id,
860 void *data)
861{
862 struct unlock_dialog *dialog = data;
863 struct desktop *desktop = dialog->desktop;
864
865 dialog->button_focused = 0;
866 widget_schedule_redraw(widget);
867 display_defer(desktop->display, &desktop->unlock_task);
868 dialog->closing = 1;
869}
870
871static void
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200872unlock_dialog_keyboard_focus_handler(struct window *window,
873 struct input *device, void *data)
874{
875 window_schedule_redraw(window);
876}
877
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500878static int
Kristian Høgsbergee143232012-01-09 08:42:24 -0500879unlock_dialog_widget_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400880 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400881 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500882{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500883 struct unlock_dialog *dialog = data;
884
885 dialog->button_focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500886 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500887
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300888 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500889}
890
891static void
892unlock_dialog_widget_leave_handler(struct widget *widget,
893 struct input *input, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200894{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500895 struct unlock_dialog *dialog = data;
896
897 dialog->button_focused = 0;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500898 widget_schedule_redraw(widget);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200899}
900
901static struct unlock_dialog *
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500902unlock_dialog_create(struct desktop *desktop)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200903{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500904 struct display *display = desktop->display;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200905 struct unlock_dialog *dialog;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800906 struct wl_surface *surface;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200907
Peter Huttererf3d62272013-08-08 11:57:05 +1000908 dialog = xzalloc(sizeof *dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200909
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400910 dialog->window = window_create_custom(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500911 dialog->widget = window_frame_create(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200912 window_set_title(dialog->window, "Unlock your desktop");
913
914 window_set_user_data(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200915 window_set_keyboard_focus_handler(dialog->window,
916 unlock_dialog_keyboard_focus_handler);
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500917 dialog->button = widget_add_widget(dialog->widget, dialog);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500918 widget_set_redraw_handler(dialog->widget,
919 unlock_dialog_redraw_handler);
Kristian Høgsbergee143232012-01-09 08:42:24 -0500920 widget_set_enter_handler(dialog->button,
921 unlock_dialog_widget_enter_handler);
922 widget_set_leave_handler(dialog->button,
923 unlock_dialog_widget_leave_handler);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500924 widget_set_button_handler(dialog->button,
925 unlock_dialog_button_handler);
Brian J Lovinc4df4082013-08-26 15:58:22 -0700926 widget_set_touch_down_handler(dialog->button,
927 unlock_dialog_touch_down_handler);
928 widget_set_touch_up_handler(dialog->button,
929 unlock_dialog_touch_up_handler);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200930
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800931 surface = window_get_wl_surface(dialog->window);
932 weston_desktop_shell_set_lock_surface(desktop->shell, surface);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500933
Pekka Paalanen40e49ac2012-01-18 16:51:30 +0200934 window_schedule_resize(dialog->window, 260, 230);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200935
936 return dialog;
937}
938
939static void
940unlock_dialog_destroy(struct unlock_dialog *dialog)
941{
942 window_destroy(dialog->window);
943 free(dialog);
944}
945
946static void
947unlock_dialog_finish(struct task *task, uint32_t events)
948{
949 struct desktop *desktop =
Benjamin Franzked7759712011-11-22 12:38:48 +0100950 container_of(task, struct desktop, unlock_task);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200951
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800952 weston_desktop_shell_unlock(desktop->shell);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200953 unlock_dialog_destroy(desktop->unlock_dialog);
954 desktop->unlock_dialog = NULL;
955}
956
957static void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400958desktop_shell_configure(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800959 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400960 uint32_t edges,
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400961 struct wl_surface *surface,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400962 int32_t width, int32_t height)
963{
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400964 struct window *window = wl_surface_get_user_data(surface);
Pekka Paalanen068ae942011-11-28 14:11:15 +0200965 struct surface *s = window_get_user_data(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400966
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400967 s->configure(data, desktop_shell, edges, window, width, height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400968}
969
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200970static void
971desktop_shell_prepare_lock_surface(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800972 struct weston_desktop_shell *desktop_shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200973{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200974 struct desktop *desktop = data;
975
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300976 if (!desktop->locking) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800977 weston_desktop_shell_unlock(desktop->shell);
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200978 return;
979 }
980
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200981 if (!desktop->unlock_dialog) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500982 desktop->unlock_dialog = unlock_dialog_create(desktop);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200983 desktop->unlock_dialog->desktop = desktop;
984 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200985}
986
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300987static void
988desktop_shell_grab_cursor(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800989 struct weston_desktop_shell *desktop_shell,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300990 uint32_t cursor)
991{
992 struct desktop *desktop = data;
993
994 switch (cursor) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800995 case WESTON_DESKTOP_SHELL_CURSOR_NONE:
Philipp Brüschweiler16d59d72012-08-24 15:43:55 +0200996 desktop->grab_cursor = CURSOR_BLANK;
997 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800998 case WESTON_DESKTOP_SHELL_CURSOR_BUSY:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300999 desktop->grab_cursor = CURSOR_WATCH;
1000 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001001 case WESTON_DESKTOP_SHELL_CURSOR_MOVE:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001002 desktop->grab_cursor = CURSOR_DRAGGING;
1003 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001004 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001005 desktop->grab_cursor = CURSOR_TOP;
1006 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001007 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001008 desktop->grab_cursor = CURSOR_BOTTOM;
1009 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001010 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_LEFT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001011 desktop->grab_cursor = CURSOR_LEFT;
1012 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001013 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_RIGHT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001014 desktop->grab_cursor = CURSOR_RIGHT;
1015 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001016 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP_LEFT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001017 desktop->grab_cursor = CURSOR_TOP_LEFT;
1018 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001019 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP_RIGHT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001020 desktop->grab_cursor = CURSOR_TOP_RIGHT;
1021 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001022 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_LEFT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001023 desktop->grab_cursor = CURSOR_BOTTOM_LEFT;
1024 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001025 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_RIGHT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001026 desktop->grab_cursor = CURSOR_BOTTOM_RIGHT;
1027 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001028 case WESTON_DESKTOP_SHELL_CURSOR_ARROW:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001029 default:
1030 desktop->grab_cursor = CURSOR_LEFT_PTR;
1031 }
1032}
1033
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001034static const struct weston_desktop_shell_listener listener = {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001035 desktop_shell_configure,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001036 desktop_shell_prepare_lock_surface,
1037 desktop_shell_grab_cursor
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001038};
1039
U. Artie Eoff44874d92012-10-02 21:12:35 -07001040static void
1041background_destroy(struct background *background)
1042{
1043 widget_destroy(background->widget);
1044 window_destroy(background->window);
1045
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001046 free(background->image);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001047 free(background);
1048}
1049
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001050static struct background *
1051background_create(struct desktop *desktop)
1052{
1053 struct background *background;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001054 struct weston_config_section *s;
1055 char *type;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001056
Peter Huttererf3d62272013-08-08 11:57:05 +10001057 background = xzalloc(sizeof *background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001058 background->base.configure = background_configure;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001059 background->window = window_create_custom(desktop->display);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001060 background->widget = window_add_widget(background->window, background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001061 window_set_user_data(background->window, background);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001062 widget_set_redraw_handler(background->widget, background_draw);
Arnaud Vracfb754a02014-08-25 20:56:49 +02001063 widget_set_transparent(background->widget, 0);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001064 window_set_preferred_format(background->window,
1065 WINDOW_PREFERRED_FORMAT_RGB565);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001066
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001067 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
1068 weston_config_section_get_string(s, "background-image",
Kristian Høgsberg4c3661f2014-04-21 22:54:37 -07001069 &background->image, NULL);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001070 weston_config_section_get_uint(s, "background-color",
Kristian Høgsberg4c3661f2014-04-21 22:54:37 -07001071 &background->color, 0);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001072
1073 weston_config_section_get_string(s, "background-type",
1074 &type, "tile");
U. Artie Eoff3c946772014-01-15 10:59:50 -08001075 if (type == NULL) {
1076 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
1077 exit(EXIT_FAILURE);
1078 }
1079
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001080 if (strcmp(type, "scale") == 0) {
1081 background->type = BACKGROUND_SCALE;
1082 } else if (strcmp(type, "scale-crop") == 0) {
1083 background->type = BACKGROUND_SCALE_CROP;
1084 } else if (strcmp(type, "tile") == 0) {
1085 background->type = BACKGROUND_TILE;
1086 } else {
1087 background->type = -1;
1088 fprintf(stderr, "invalid background-type: %s\n",
1089 type);
1090 }
1091
1092 free(type);
1093
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001094 return background;
1095}
1096
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001097static int
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001098grab_surface_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001099 float x, float y, void *data)
1100{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001101 struct desktop *desktop = data;
1102
1103 return desktop->grab_cursor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001104}
1105
1106static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001107grab_surface_destroy(struct desktop *desktop)
1108{
1109 widget_destroy(desktop->grab_widget);
1110 window_destroy(desktop->grab_window);
1111}
1112
1113static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001114grab_surface_create(struct desktop *desktop)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001115{
1116 struct wl_surface *s;
1117
Ander Conselvan de Oliveira07a91cd2012-07-16 14:15:50 +03001118 desktop->grab_window = window_create_custom(desktop->display);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001119 window_set_user_data(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001120
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001121 s = window_get_wl_surface(desktop->grab_window);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001122 weston_desktop_shell_set_grab_surface(desktop->shell, s);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001123
1124 desktop->grab_widget =
1125 window_add_widget(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001126 /* We set the allocation to 1x1 at 0,0 so the fake enter event
1127 * at 0,0 will go to this widget. */
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001128 widget_set_allocation(desktop->grab_widget, 0, 0, 1, 1);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001129
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001130 widget_set_enter_handler(desktop->grab_widget,
1131 grab_surface_enter_handler);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001132}
1133
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001134static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001135output_destroy(struct output *output)
1136{
1137 background_destroy(output->background);
Jonny Lambe67118c2014-08-12 15:07:51 +02001138 if (output->panel)
1139 panel_destroy(output->panel);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001140 wl_output_destroy(output->output);
1141 wl_list_remove(&output->link);
1142
1143 free(output);
1144}
1145
1146static void
1147desktop_destroy_outputs(struct desktop *desktop)
1148{
1149 struct output *tmp;
1150 struct output *output;
1151
1152 wl_list_for_each_safe(output, tmp, &desktop->outputs, link)
1153 output_destroy(output);
1154}
1155
1156static void
Alexander Larssonc584fa62013-05-22 14:41:32 +02001157output_handle_geometry(void *data,
1158 struct wl_output *wl_output,
1159 int x, int y,
1160 int physical_width,
1161 int physical_height,
1162 int subpixel,
1163 const char *make,
1164 const char *model,
1165 int transform)
1166{
1167 struct output *output = data;
1168
Jonny Lambe67118c2014-08-12 15:07:51 +02001169 if (output->panel)
1170 window_set_buffer_transform(output->panel->window, transform);
Alexander Larssonc584fa62013-05-22 14:41:32 +02001171 window_set_buffer_transform(output->background->window, transform);
1172}
1173
1174static void
1175output_handle_mode(void *data,
1176 struct wl_output *wl_output,
1177 uint32_t flags,
1178 int width,
1179 int height,
1180 int refresh)
1181{
1182}
1183
1184static void
1185output_handle_done(void *data,
1186 struct wl_output *wl_output)
1187{
1188}
1189
1190static void
1191output_handle_scale(void *data,
1192 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001193 int32_t scale)
Alexander Larssonc584fa62013-05-22 14:41:32 +02001194{
1195 struct output *output = data;
1196
Jonny Lambe67118c2014-08-12 15:07:51 +02001197 if (output->panel)
1198 window_set_buffer_scale(output->panel->window, scale);
Alexander Larssonc584fa62013-05-22 14:41:32 +02001199 window_set_buffer_scale(output->background->window, scale);
1200}
1201
1202static const struct wl_output_listener output_listener = {
1203 output_handle_geometry,
1204 output_handle_mode,
1205 output_handle_done,
1206 output_handle_scale
1207};
1208
Jonny Lambe67118c2014-08-12 15:07:51 +02001209static int
1210want_panel(struct desktop *desktop)
1211{
1212 struct weston_config_section *s;
1213 char *location = NULL;
1214 int ret = 1;
1215
1216 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
1217 weston_config_section_get_string(s, "panel-location",
1218 &location, "top");
1219
1220 if (strcmp(location, "top") != 0)
1221 ret = 0;
1222
1223 free(location);
1224
1225 return ret;
1226}
1227
Alexander Larssonc584fa62013-05-22 14:41:32 +02001228static void
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001229output_init(struct output *output, struct desktop *desktop)
1230{
1231 struct wl_surface *surface;
1232
Jonny Lambe67118c2014-08-12 15:07:51 +02001233 if (want_panel(desktop)) {
1234 output->panel = panel_create(desktop);
1235 surface = window_get_wl_surface(output->panel->window);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001236 weston_desktop_shell_set_panel(desktop->shell,
1237 output->output, surface);
Jonny Lambe67118c2014-08-12 15:07:51 +02001238 }
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001239
1240 output->background = background_create(desktop);
1241 surface = window_get_wl_surface(output->background->window);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001242 weston_desktop_shell_set_background(desktop->shell,
1243 output->output, surface);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001244}
1245
1246static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001247create_output(struct desktop *desktop, uint32_t id)
1248{
1249 struct output *output;
1250
Bryce Harrington0d1a6222016-02-11 16:42:49 -08001251 output = zalloc(sizeof *output);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001252 if (!output)
1253 return;
1254
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001255 output->output =
Alexander Larssonc584fa62013-05-22 14:41:32 +02001256 display_bind(desktop->display, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001257 output->server_output_id = id;
Alexander Larssonc584fa62013-05-22 14:41:32 +02001258
1259 wl_output_add_listener(output->output, &output_listener, output);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001260
1261 wl_list_insert(&desktop->outputs, &output->link);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001262
1263 /* On start up we may process an output global before the shell global
1264 * in which case we can't create the panel and background just yet */
1265 if (desktop->shell)
1266 output_init(output, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001267}
1268
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001269static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001270global_handler(struct display *display, uint32_t id,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001271 const char *interface, uint32_t version, void *data)
1272{
1273 struct desktop *desktop = data;
1274
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001275 if (!strcmp(interface, "weston_desktop_shell")) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001276 desktop->shell = display_bind(desktop->display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001277 id,
1278 &weston_desktop_shell_interface,
1279 1);
1280 weston_desktop_shell_add_listener(desktop->shell,
1281 &listener,
1282 desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001283 } else if (!strcmp(interface, "wl_output")) {
1284 create_output(desktop, id);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001285 }
1286}
1287
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001288static void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001289global_handler_remove(struct display *display, uint32_t id,
1290 const char *interface, uint32_t version, void *data)
1291{
1292 struct desktop *desktop = data;
1293 struct output *output;
1294
1295 if (!strcmp(interface, "wl_output")) {
1296 wl_list_for_each(output, &desktop->outputs, link) {
1297 if (output->server_output_id == id) {
1298 output_destroy(output);
1299 break;
1300 }
1301 }
1302 }
1303}
1304
1305static void
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001306panel_add_launchers(struct panel *panel, struct desktop *desktop)
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001307{
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001308 struct weston_config_section *s;
1309 char *icon, *path;
1310 const char *name;
1311 int count;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001312
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001313 count = 0;
1314 s = NULL;
1315 while (weston_config_next_section(desktop->config, &s, &name)) {
1316 if (strcmp(name, "launcher") != 0)
1317 continue;
1318
1319 weston_config_section_get_string(s, "icon", &icon, NULL);
1320 weston_config_section_get_string(s, "path", &path, NULL);
1321
1322 if (icon != NULL && path != NULL) {
1323 panel_add_launcher(panel, icon, path);
Rob Bradford09252d42013-07-26 16:29:45 +01001324 count++;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001325 } else {
1326 fprintf(stderr, "invalid launcher section\n");
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001327 }
1328
1329 free(icon);
1330 free(path);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001331 }
1332
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001333 if (count == 0) {
1334 /* add default launcher */
1335 panel_add_launcher(panel,
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001336 DATADIR "/weston/terminal.png",
Rodney Lorrimar99ff01b2012-02-29 17:31:03 +01001337 BINDIR "/weston-terminal");
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001338 }
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001339}
1340
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001341int main(int argc, char *argv[])
1342{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001343 struct desktop desktop = { 0 };
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001344 struct output *output;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001345 struct weston_config_section *s;
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001346 const char *config_file;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001347
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001348 desktop.unlock_task.run = unlock_dialog_finish;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001349 wl_list_init(&desktop.outputs);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001350
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001351 config_file = weston_config_get_name_from_env();
1352 desktop.config = weston_config_parse(config_file);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001353 s = weston_config_get_section(desktop.config, "shell", NULL, NULL);
1354 weston_config_section_get_bool(s, "locking", &desktop.locking, 1);
1355
Kristian Høgsberg4172f662013-02-20 15:27:49 -05001356 desktop.display = display_create(&argc, argv);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001357 if (desktop.display == NULL) {
1358 fprintf(stderr, "failed to create display: %m\n");
1359 return -1;
1360 }
1361
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001362 display_set_user_data(desktop.display, &desktop);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001363 display_set_global_handler(desktop.display, global_handler);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001364 display_set_global_handler_remove(desktop.display, global_handler_remove);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001365
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001366 /* Create panel and background for outputs processed before the shell
1367 * global interface was processed */
1368 wl_list_for_each(output, &desktop.outputs, link)
1369 if (!output->panel)
1370 output_init(output, &desktop);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001371
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001372 grab_surface_create(&desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001373
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001374 signal(SIGCHLD, sigchild_handler);
1375
1376 display_run(desktop.display);
1377
U. Artie Eoff44874d92012-10-02 21:12:35 -07001378 /* Cleanup */
1379 grab_surface_destroy(&desktop);
1380 desktop_destroy_outputs(&desktop);
1381 if (desktop.unlock_dialog)
1382 unlock_dialog_destroy(desktop.unlock_dialog);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001383 weston_desktop_shell_destroy(desktop.shell);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001384 display_destroy(desktop.display);
1385
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001386 return 0;
1387}