blob: 49a3077a1a31711ccd6b23d3c3f9368828d9853f [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
Pekka Paalanenb6df4f72012-08-03 14:39:15 +030053extern char **environ; /* defined by libc */
54
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040055struct desktop {
56 struct display *display;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080057 struct weston_desktop_shell *shell;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +020058 struct unlock_dialog *unlock_dialog;
59 struct task unlock_task;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010060 struct wl_list outputs;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040061
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030062 struct window *grab_window;
63 struct widget *grab_widget;
64
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +030065 struct weston_config *config;
66 int locking;
67
Scott Moreauec116022012-07-22 18:23:52 -060068 enum cursor_type grab_cursor;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030069
70 int painted;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010071};
72
73struct surface {
74 void (*configure)(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080075 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040076 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010077 int32_t width, int32_t height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040078};
79
80struct panel {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010081 struct surface base;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040082 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050083 struct widget *widget;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050084 struct wl_list launcher_list;
Martin Minarik1e51a872012-06-08 00:39:11 +020085 struct panel_clock *clock;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030086 int painted;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +030087 uint32_t color;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040088};
89
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010090struct background {
91 struct surface base;
92 struct window *window;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050093 struct widget *widget;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030094 int painted;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +030095
96 char *image;
97 int type;
98 uint32_t color;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010099};
100
101struct output {
102 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800103 uint32_t server_output_id;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100104 struct wl_list link;
105
106 struct panel *panel;
107 struct background *background;
108};
109
Kristian Høgsberg53880802012-01-09 11:16:50 -0500110struct panel_launcher {
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500111 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400112 struct panel *panel;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400113 cairo_surface_t *icon;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500114 int focused, pressed;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400115 char *path;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500116 struct wl_list link;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400117 struct wl_array envp;
118 struct wl_array argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400119};
120
Martin Minarik1e51a872012-06-08 00:39:11 +0200121struct panel_clock {
122 struct widget *widget;
123 struct panel *panel;
Martin Minarik1e51a872012-06-08 00:39:11 +0200124 struct task clock_task;
125 int clock_fd;
126};
127
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200128struct unlock_dialog {
129 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500130 struct widget *widget;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500131 struct widget *button;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500132 int button_focused;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200133 int closing;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200134 struct desktop *desktop;
135};
136
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300137static void
138panel_add_launchers(struct panel *panel, struct desktop *desktop);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500139
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400140static void
141sigchild_handler(int s)
142{
143 int status;
144 pid_t pid;
145
146 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
147 fprintf(stderr, "child %d exited\n", pid);
148}
149
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300150static int
151is_desktop_painted(struct desktop *desktop)
152{
153 struct output *output;
154
155 wl_list_for_each(output, &desktop->outputs, link) {
156 if (output->panel && !output->panel->painted)
157 return 0;
158 if (output->background && !output->background->painted)
159 return 0;
160 }
161
162 return 1;
163}
164
165static void
166check_desktop_ready(struct window *window)
167{
168 struct display *display;
169 struct desktop *desktop;
170
171 display = window_get_display(window);
172 desktop = display_get_user_data(display);
173
174 if (!desktop->painted && is_desktop_painted(desktop)) {
175 desktop->painted = 1;
176
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800177 weston_desktop_shell_desktop_ready(desktop->shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300178 }
179}
180
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400181static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500182panel_launcher_activate(struct panel_launcher *widget)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400183{
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400184 char **argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400185 pid_t pid;
186
187 pid = fork();
188 if (pid < 0) {
189 fprintf(stderr, "fork failed: %m\n");
190 return;
191 }
192
193 if (pid)
194 return;
Benjamin Franzked7759712011-11-22 12:38:48 +0100195
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400196 argv = widget->argv.data;
197 if (execve(argv[0], argv, widget->envp.data) < 0) {
198 fprintf(stderr, "execl '%s' failed: %m\n", argv[0]);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400199 exit(1);
200 }
201}
202
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400203static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500204panel_launcher_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400205{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500206 struct panel_launcher *launcher = data;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500207 struct rectangle allocation;
208 cairo_t *cr;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400209
Alexander Larssonc584fa62013-05-22 14:41:32 +0200210 cr = widget_cairo_create(launcher->panel->widget);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500211
212 widget_get_allocation(widget, &allocation);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500213 if (launcher->pressed) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500214 allocation.x++;
215 allocation.y++;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400216 }
217
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500218 cairo_set_source_surface(cr, launcher->icon,
219 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400220 cairo_paint(cr);
221
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500222 if (launcher->focused) {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400223 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500224 cairo_mask_surface(cr, launcher->icon,
225 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400226 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400227
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500228 cairo_destroy(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400229}
230
Tiago Vignatti61500722012-05-23 22:06:28 +0300231static int
232panel_launcher_motion_handler(struct widget *widget, struct input *input,
233 uint32_t time, float x, float y, void *data)
234{
235 struct panel_launcher *launcher = data;
236
237 widget_set_tooltip(widget, basename((char *)launcher->path), x, y);
238
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300239 return CURSOR_LEFT_PTR;
Tiago Vignatti61500722012-05-23 22:06:28 +0300240}
241
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400242static void
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500243set_hex_color(cairo_t *cr, uint32_t color)
244{
Michael Vetter2a18a522015-05-15 17:17:47 +0200245 cairo_set_source_rgba(cr,
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500246 ((color >> 16) & 0xff) / 255.0,
247 ((color >> 8) & 0xff) / 255.0,
248 ((color >> 0) & 0xff) / 255.0,
249 ((color >> 24) & 0xff) / 255.0);
250}
251
252static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500253panel_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400254{
255 cairo_surface_t *surface;
256 cairo_t *cr;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500257 struct panel *panel = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400258
Alexander Larssonc584fa62013-05-22 14:41:32 +0200259 cr = widget_cairo_create(panel->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400260 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300261 set_hex_color(cr, panel->color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400262 cairo_paint(cr);
263
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400264 cairo_destroy(cr);
Alexander Larssonc584fa62013-05-22 14:41:32 +0200265 surface = window_get_surface(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400266 cairo_surface_destroy(surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300267 panel->painted = 1;
268 check_desktop_ready(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400269}
270
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500271static int
Kristian Høgsberg53880802012-01-09 11:16:50 -0500272panel_launcher_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400273 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500274{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500275 struct panel_launcher *launcher = data;
276
277 launcher->focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500278 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500279
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300280 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500281}
282
283static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500284panel_launcher_leave_handler(struct widget *widget,
285 struct input *input, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400286{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500287 struct panel_launcher *launcher = data;
288
289 launcher->focused = 0;
Tiago Vignatti61500722012-05-23 22:06:28 +0300290 widget_destroy_tooltip(widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500291 widget_schedule_redraw(widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400292}
293
294static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500295panel_launcher_button_handler(struct widget *widget,
296 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100297 uint32_t button,
298 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400299{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500300 struct panel_launcher *launcher;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400301
Kristian Høgsberg53880802012-01-09 11:16:50 -0500302 launcher = widget_get_user_data(widget);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500303 widget_schedule_redraw(widget);
Daniel Stone4dbadb12012-05-30 16:31:51 +0100304 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg53880802012-01-09 11:16:50 -0500305 panel_launcher_activate(launcher);
Rusty Lynch4384a242013-08-08 21:28:22 -0700306
307}
308
309static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700310panel_launcher_touch_down_handler(struct widget *widget, struct input *input,
311 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch4384a242013-08-08 21:28:22 -0700312 float x, float y, void *data)
313{
314 struct panel_launcher *launcher;
315
316 launcher = widget_get_user_data(widget);
317 launcher->focused = 1;
318 widget_schedule_redraw(widget);
319}
320
321static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700322panel_launcher_touch_up_handler(struct widget *widget, struct input *input,
Michael Vetter2a18a522015-05-15 17:17:47 +0200323 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch1084da52013-08-15 09:10:08 -0700324 void *data)
Rusty Lynch4384a242013-08-08 21:28:22 -0700325{
326 struct panel_launcher *launcher;
327
328 launcher = widget_get_user_data(widget);
329 launcher->focused = 0;
330 widget_schedule_redraw(widget);
331 panel_launcher_activate(launcher);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500332}
333
Martin Minarik1e51a872012-06-08 00:39:11 +0200334static void
335clock_func(struct task *task, uint32_t events)
336{
337 struct panel_clock *clock =
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400338 container_of(task, struct panel_clock, clock_task);
Kristian Høgsberg70226bb2012-06-08 16:54:52 -0400339 uint64_t exp;
Martin Minarik1e51a872012-06-08 00:39:11 +0200340
Martin Olsson8df662a2012-07-08 03:03:47 +0200341 if (read(clock->clock_fd, &exp, sizeof exp) != sizeof exp)
342 abort();
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400343 widget_schedule_redraw(clock->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200344}
345
346static void
347panel_clock_redraw_handler(struct widget *widget, void *data)
348{
Martin Minarik1e51a872012-06-08 00:39:11 +0200349 struct panel_clock *clock = data;
350 cairo_t *cr;
351 struct rectangle allocation;
352 cairo_text_extents_t extents;
353 cairo_font_extents_t font_extents;
Martin Minarik1e51a872012-06-08 00:39:11 +0200354 time_t rawtime;
355 struct tm * timeinfo;
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400356 char string[128];
Martin Minarik1e51a872012-06-08 00:39:11 +0200357
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400358 time(&rawtime);
359 timeinfo = localtime(&rawtime);
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400360 strftime(string, sizeof string, "%a %b %d, %I:%M %p", timeinfo);
Martin Minarik1e51a872012-06-08 00:39:11 +0200361
362 widget_get_allocation(widget, &allocation);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400363 if (allocation.width == 0)
364 return;
Martin Minarik1e51a872012-06-08 00:39:11 +0200365
Alexander Larssonc584fa62013-05-22 14:41:32 +0200366 cr = widget_cairo_create(clock->panel->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200367 cairo_select_font_face(cr, "sans",
368 CAIRO_FONT_SLANT_NORMAL,
369 CAIRO_FONT_WEIGHT_NORMAL);
370 cairo_set_font_size(cr, 14);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400371 cairo_text_extents(cr, string, &extents);
Martin Minarik1e51a872012-06-08 00:39:11 +0200372 cairo_font_extents (cr, &font_extents);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400373 cairo_move_to(cr, allocation.x + 5,
374 allocation.y + 3 * (allocation.height >> 2) + 1);
375 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400376 cairo_show_text(cr, string);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400377 cairo_move_to(cr, allocation.x + 4,
378 allocation.y + 3 * (allocation.height >> 2));
379 cairo_set_source_rgb(cr, 1, 1, 1);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400380 cairo_show_text(cr, string);
Martin Minarik1e51a872012-06-08 00:39:11 +0200381 cairo_destroy(cr);
382}
383
384static int
385clock_timer_reset(struct panel_clock *clock)
386{
387 struct itimerspec its;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400388
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400389 its.it_interval.tv_sec = 60;
Martin Minarik1e51a872012-06-08 00:39:11 +0200390 its.it_interval.tv_nsec = 0;
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400391 its.it_value.tv_sec = 60;
Martin Minarik1e51a872012-06-08 00:39:11 +0200392 its.it_value.tv_nsec = 0;
393 if (timerfd_settime(clock->clock_fd, 0, &its, NULL) < 0) {
394 fprintf(stderr, "could not set timerfd\n: %m");
395 return -1;
396 }
397
398 return 0;
399}
400
401static void
U. Artie Eoff44874d92012-10-02 21:12:35 -0700402panel_destroy_clock(struct panel_clock *clock)
403{
404 widget_destroy(clock->widget);
405
406 close(clock->clock_fd);
407
408 free(clock);
409}
410
411static void
Martin Minarik1e51a872012-06-08 00:39:11 +0200412panel_add_clock(struct panel *panel)
413{
414 struct panel_clock *clock;
415 int timerfd;
416
417 timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
418 if (timerfd < 0) {
419 fprintf(stderr, "could not create timerfd\n: %m");
420 return;
421 }
422
Peter Huttererf3d62272013-08-08 11:57:05 +1000423 clock = xzalloc(sizeof *clock);
Martin Minarik1e51a872012-06-08 00:39:11 +0200424 clock->panel = panel;
425 panel->clock = clock;
426 clock->clock_fd = timerfd;
427
428 clock->clock_task.run = clock_func;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400429 display_watch_fd(window_get_display(panel->window), clock->clock_fd,
430 EPOLLIN, &clock->clock_task);
Martin Minarik1e51a872012-06-08 00:39:11 +0200431 clock_timer_reset(clock);
432
433 clock->widget = widget_add_widget(panel->widget, clock);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400434 widget_set_redraw_handler(clock->widget, panel_clock_redraw_handler);
Martin Minarik1e51a872012-06-08 00:39:11 +0200435}
436
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500437static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500438panel_resize_handler(struct widget *widget,
439 int32_t width, int32_t height, void *data)
440{
441 struct panel_launcher *launcher;
442 struct panel *panel = data;
443 int x, y, w, h;
Michael Vetter2a18a522015-05-15 17:17:47 +0200444
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500445 x = 10;
446 y = 16;
447 wl_list_for_each(launcher, &panel->launcher_list, link) {
448 w = cairo_image_surface_get_width(launcher->icon);
449 h = cairo_image_surface_get_height(launcher->icon);
450 widget_set_allocation(launcher->widget,
451 x, y - h / 2, w + 1, h + 1);
452 x += w + 10;
453 }
Martin Minarik1e51a872012-06-08 00:39:11 +0200454 h=20;
455 w=170;
Pekka Paalanen01b17252012-06-12 17:42:26 +0300456
457 if (panel->clock)
458 widget_set_allocation(panel->clock->widget,
459 width - w - 8, y - h / 2, w + 1, h + 1);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500460}
461
462static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100463panel_configure(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800464 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400465 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100466 int32_t width, int32_t height)
467{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500468 struct surface *surface = window_get_user_data(window);
469 struct panel *panel = container_of(surface, struct panel, base);
470
471 window_schedule_resize(panel->window, width, 32);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100472}
473
U. Artie Eoff44874d92012-10-02 21:12:35 -0700474static void
475panel_destroy_launcher(struct panel_launcher *launcher)
476{
477 wl_array_release(&launcher->argv);
478 wl_array_release(&launcher->envp);
479
480 free(launcher->path);
481
482 cairo_surface_destroy(launcher->icon);
483
484 widget_destroy(launcher->widget);
485 wl_list_remove(&launcher->link);
486
487 free(launcher);
488}
489
490static void
491panel_destroy(struct panel *panel)
492{
493 struct panel_launcher *tmp;
494 struct panel_launcher *launcher;
495
496 panel_destroy_clock(panel->clock);
497
498 wl_list_for_each_safe(launcher, tmp, &panel->launcher_list, link)
499 panel_destroy_launcher(launcher);
500
501 widget_destroy(panel->widget);
502 window_destroy(panel->window);
503
504 free(panel);
505}
506
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400507static struct panel *
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300508panel_create(struct desktop *desktop)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400509{
510 struct panel *panel;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300511 struct weston_config_section *s;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400512
Peter Huttererf3d62272013-08-08 11:57:05 +1000513 panel = xzalloc(sizeof *panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400514
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100515 panel->base.configure = panel_configure;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300516 panel->window = window_create_custom(desktop->display);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500517 panel->widget = window_add_widget(panel->window, panel);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500518 wl_list_init(&panel->launcher_list);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400519
520 window_set_title(panel->window, "panel");
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400521 window_set_user_data(panel->window, panel);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500522
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500523 widget_set_redraw_handler(panel->widget, panel_redraw_handler);
524 widget_set_resize_handler(panel->widget, panel_resize_handler);
Michael Vetter2a18a522015-05-15 17:17:47 +0200525
Rafal Mielniczukb9e513c2012-06-09 20:33:29 +0200526 panel_add_clock(panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400527
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300528 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
529 weston_config_section_get_uint(s, "panel-color",
530 &panel->color, 0xaa000000);
531
532 panel_add_launchers(panel, desktop);
533
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400534 return panel;
535}
536
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200537static cairo_surface_t *
538load_icon_or_fallback(const char *icon)
539{
540 cairo_surface_t *surface = cairo_image_surface_create_from_png(icon);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200541 cairo_status_t status;
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200542 cairo_t *cr;
543
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200544 status = cairo_surface_status(surface);
545 if (status == CAIRO_STATUS_SUCCESS)
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200546 return surface;
547
548 cairo_surface_destroy(surface);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200549 fprintf(stderr, "ERROR loading icon from file '%s', error: '%s'\n",
550 icon, cairo_status_to_string(status));
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200551
552 /* draw fallback icon */
553 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
554 20, 20);
555 cr = cairo_create(surface);
556
557 cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1);
558 cairo_paint(cr);
559
560 cairo_set_source_rgba(cr, 0, 0, 0, 1);
561 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
562 cairo_rectangle(cr, 0, 0, 20, 20);
563 cairo_move_to(cr, 4, 4);
564 cairo_line_to(cr, 16, 16);
565 cairo_move_to(cr, 4, 16);
566 cairo_line_to(cr, 16, 4);
567 cairo_stroke(cr);
568
569 cairo_destroy(cr);
570
571 return surface;
572}
573
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400574static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500575panel_add_launcher(struct panel *panel, const char *icon, const char *path)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400576{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500577 struct panel_launcher *launcher;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400578 char *start, *p, *eq, **ps;
579 int i, j, k;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400580
Peter Huttererf3d62272013-08-08 11:57:05 +1000581 launcher = xzalloc(sizeof *launcher);
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200582 launcher->icon = load_icon_or_fallback(icon);
U. Artie Eoff3c946772014-01-15 10:59:50 -0800583 launcher->path = xstrdup(path);
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400584
585 wl_array_init(&launcher->envp);
586 wl_array_init(&launcher->argv);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300587 for (i = 0; environ[i]; i++) {
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400588 ps = wl_array_add(&launcher->envp, sizeof *ps);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300589 *ps = environ[i];
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400590 }
591 j = 0;
592
593 start = launcher->path;
594 while (*start) {
595 for (p = start, eq = NULL; *p && !isspace(*p); p++)
596 if (*p == '=')
597 eq = p;
598
599 if (eq && j == 0) {
600 ps = launcher->envp.data;
601 for (k = 0; k < i; k++)
602 if (strncmp(ps[k], start, eq - start) == 0) {
603 ps[k] = start;
604 break;
605 }
606 if (k == i) {
607 ps = wl_array_add(&launcher->envp, sizeof *ps);
608 *ps = start;
609 i++;
610 }
611 } else {
612 ps = wl_array_add(&launcher->argv, sizeof *ps);
613 *ps = start;
614 j++;
615 }
616
617 while (*p && isspace(*p))
618 *p++ = '\0';
619
620 start = p;
621 }
622
623 ps = wl_array_add(&launcher->envp, sizeof *ps);
624 *ps = NULL;
625 ps = wl_array_add(&launcher->argv, sizeof *ps);
626 *ps = NULL;
627
Kristian Høgsberg53880802012-01-09 11:16:50 -0500628 launcher->panel = panel;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500629 wl_list_insert(panel->launcher_list.prev, &launcher->link);
630
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500631 launcher->widget = widget_add_widget(panel->widget, launcher);
Kristian Høgsberg53880802012-01-09 11:16:50 -0500632 widget_set_enter_handler(launcher->widget,
633 panel_launcher_enter_handler);
634 widget_set_leave_handler(launcher->widget,
635 panel_launcher_leave_handler);
636 widget_set_button_handler(launcher->widget,
637 panel_launcher_button_handler);
Rusty Lynch4384a242013-08-08 21:28:22 -0700638 widget_set_touch_down_handler(launcher->widget,
639 panel_launcher_touch_down_handler);
640 widget_set_touch_up_handler(launcher->widget,
641 panel_launcher_touch_up_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500642 widget_set_redraw_handler(launcher->widget,
643 panel_launcher_redraw_handler);
Tiago Vignatti61500722012-05-23 22:06:28 +0300644 widget_set_motion_handler(launcher->widget,
645 panel_launcher_motion_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400646}
647
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500648enum {
649 BACKGROUND_SCALE,
Pekka Paalanena402b052013-05-22 18:03:10 +0300650 BACKGROUND_SCALE_CROP,
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500651 BACKGROUND_TILE
652};
653
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400654static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500655background_draw(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400656{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500657 struct background *background = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400658 cairo_surface_t *surface, *image;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400659 cairo_pattern_t *pattern;
660 cairo_matrix_t matrix;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400661 cairo_t *cr;
Pekka Paalanena402b052013-05-22 18:03:10 +0300662 double im_w, im_h;
663 double sx, sy, s;
664 double tx, ty;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500665 struct rectangle allocation;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400666
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500667 surface = window_get_surface(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400668
Alexander Larssonc584fa62013-05-22 14:41:32 +0200669 cr = widget_cairo_create(background->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400670 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
671 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
672 cairo_paint(cr);
673
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500674 widget_get_allocation(widget, &allocation);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500675 image = NULL;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300676 if (background->image)
677 image = load_cairo_surface(background->image);
Kristian Høgsberg4c3661f2014-04-21 22:54:37 -0700678 else if (background->color == 0)
679 image = load_cairo_surface(DATADIR "/weston/pattern.png");
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500680
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300681 if (image && background->type != -1) {
Pekka Paalanena402b052013-05-22 18:03:10 +0300682 im_w = cairo_image_surface_get_width(image);
683 im_h = cairo_image_surface_get_height(image);
684 sx = im_w / allocation.width;
685 sy = im_h / allocation.height;
686
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400687 pattern = cairo_pattern_create_for_surface(image);
Pekka Paalanena402b052013-05-22 18:03:10 +0300688
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300689 switch (background->type) {
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500690 case BACKGROUND_SCALE:
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500691 cairo_matrix_init_scale(&matrix, sx, sy);
692 cairo_pattern_set_matrix(pattern, &matrix);
Bill Spitzak79b7cb32014-05-08 20:00:35 -0700693 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500694 break;
Pekka Paalanena402b052013-05-22 18:03:10 +0300695 case BACKGROUND_SCALE_CROP:
696 s = (sx < sy) ? sx : sy;
697 /* align center */
698 tx = (im_w - s * allocation.width) * 0.5;
699 ty = (im_h - s * allocation.height) * 0.5;
700 cairo_matrix_init_translate(&matrix, tx, ty);
701 cairo_matrix_scale(&matrix, s, s);
702 cairo_pattern_set_matrix(pattern, &matrix);
Bill Spitzak79b7cb32014-05-08 20:00:35 -0700703 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
Pekka Paalanena402b052013-05-22 18:03:10 +0300704 break;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500705 case BACKGROUND_TILE:
706 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
707 break;
708 }
Pekka Paalanena402b052013-05-22 18:03:10 +0300709
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400710 cairo_set_source(cr, pattern);
711 cairo_pattern_destroy (pattern);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400712 cairo_surface_destroy(image);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500713 } else {
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300714 set_hex_color(cr, background->color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400715 }
716
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500717 cairo_paint(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400718 cairo_destroy(cr);
719 cairo_surface_destroy(surface);
Pekka Paalanen9564c752012-10-24 09:43:08 +0300720
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300721 background->painted = 1;
722 check_desktop_ready(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400723}
724
725static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100726background_configure(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800727 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400728 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100729 int32_t width, int32_t height)
730{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500731 struct background *background =
732 (struct background *) window_get_user_data(window);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100733
Kristian Høgsberg4598f962014-01-01 16:32:09 -0800734 widget_schedule_resize(background->widget, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100735}
736
737static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500738unlock_dialog_redraw_handler(struct widget *widget, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200739{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500740 struct unlock_dialog *dialog = data;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200741 struct rectangle allocation;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200742 cairo_surface_t *surface;
Alexander Larssonc584fa62013-05-22 14:41:32 +0200743 cairo_t *cr;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200744 cairo_pattern_t *pat;
745 double cx, cy, r, f;
746
Alexander Larssonc584fa62013-05-22 14:41:32 +0200747 cr = widget_cairo_create(widget);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500748
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500749 widget_get_allocation(dialog->widget, &allocation);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200750 cairo_rectangle(cr, allocation.x, allocation.y,
751 allocation.width, allocation.height);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200752 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
753 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500754 cairo_fill(cr);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200755
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500756 cairo_translate(cr, allocation.x, allocation.y);
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500757 if (dialog->button_focused)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200758 f = 1.0;
759 else
760 f = 0.7;
761
762 cx = allocation.width / 2.0;
763 cy = allocation.height / 2.0;
764 r = (cx < cy ? cx : cy) * 0.4;
765 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
766 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
767 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
768 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
769 cairo_set_source(cr, pat);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500770 cairo_pattern_destroy(pat);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200771 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
772 cairo_fill(cr);
773
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500774 widget_set_allocation(dialog->button,
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500775 allocation.x + cx - r,
776 allocation.y + cy - r, 2 * r, 2 * r);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200777
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200778 cairo_destroy(cr);
779
Alexander Larssonc584fa62013-05-22 14:41:32 +0200780 surface = window_get_surface(dialog->window);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200781 cairo_surface_destroy(surface);
782}
783
784static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500785unlock_dialog_button_handler(struct widget *widget,
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200786 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100787 uint32_t button,
788 enum wl_pointer_button_state state, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200789{
790 struct unlock_dialog *dialog = data;
791 struct desktop *desktop = dialog->desktop;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200792
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500793 if (button == BTN_LEFT) {
Daniel Stone4dbadb12012-05-30 16:31:51 +0100794 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
795 !dialog->closing) {
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200796 display_defer(desktop->display, &desktop->unlock_task);
797 dialog->closing = 1;
798 }
799 }
800}
801
802static void
Brian J Lovinc4df4082013-08-26 15:58:22 -0700803unlock_dialog_touch_down_handler(struct widget *widget, struct input *input,
804 uint32_t serial, uint32_t time, int32_t id,
805 float x, float y, void *data)
806{
807 struct unlock_dialog *dialog = data;
808
809 dialog->button_focused = 1;
810 widget_schedule_redraw(widget);
811}
812
813static void
814unlock_dialog_touch_up_handler(struct widget *widget, struct input *input,
815 uint32_t serial, uint32_t time, int32_t id,
816 void *data)
817{
818 struct unlock_dialog *dialog = data;
819 struct desktop *desktop = dialog->desktop;
820
821 dialog->button_focused = 0;
822 widget_schedule_redraw(widget);
823 display_defer(desktop->display, &desktop->unlock_task);
824 dialog->closing = 1;
825}
826
827static void
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200828unlock_dialog_keyboard_focus_handler(struct window *window,
829 struct input *device, void *data)
830{
831 window_schedule_redraw(window);
832}
833
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500834static int
Kristian Høgsbergee143232012-01-09 08:42:24 -0500835unlock_dialog_widget_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400836 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400837 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500838{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500839 struct unlock_dialog *dialog = data;
840
841 dialog->button_focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500842 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500843
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300844 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500845}
846
847static void
848unlock_dialog_widget_leave_handler(struct widget *widget,
849 struct input *input, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200850{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500851 struct unlock_dialog *dialog = data;
852
853 dialog->button_focused = 0;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500854 widget_schedule_redraw(widget);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200855}
856
857static struct unlock_dialog *
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500858unlock_dialog_create(struct desktop *desktop)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200859{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500860 struct display *display = desktop->display;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200861 struct unlock_dialog *dialog;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800862 struct wl_surface *surface;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200863
Peter Huttererf3d62272013-08-08 11:57:05 +1000864 dialog = xzalloc(sizeof *dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200865
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400866 dialog->window = window_create_custom(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500867 dialog->widget = window_frame_create(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200868 window_set_title(dialog->window, "Unlock your desktop");
869
870 window_set_user_data(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200871 window_set_keyboard_focus_handler(dialog->window,
872 unlock_dialog_keyboard_focus_handler);
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500873 dialog->button = widget_add_widget(dialog->widget, dialog);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500874 widget_set_redraw_handler(dialog->widget,
875 unlock_dialog_redraw_handler);
Kristian Høgsbergee143232012-01-09 08:42:24 -0500876 widget_set_enter_handler(dialog->button,
877 unlock_dialog_widget_enter_handler);
878 widget_set_leave_handler(dialog->button,
879 unlock_dialog_widget_leave_handler);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500880 widget_set_button_handler(dialog->button,
881 unlock_dialog_button_handler);
Brian J Lovinc4df4082013-08-26 15:58:22 -0700882 widget_set_touch_down_handler(dialog->button,
883 unlock_dialog_touch_down_handler);
884 widget_set_touch_up_handler(dialog->button,
885 unlock_dialog_touch_up_handler);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200886
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800887 surface = window_get_wl_surface(dialog->window);
888 weston_desktop_shell_set_lock_surface(desktop->shell, surface);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500889
Pekka Paalanen40e49ac2012-01-18 16:51:30 +0200890 window_schedule_resize(dialog->window, 260, 230);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200891
892 return dialog;
893}
894
895static void
896unlock_dialog_destroy(struct unlock_dialog *dialog)
897{
898 window_destroy(dialog->window);
899 free(dialog);
900}
901
902static void
903unlock_dialog_finish(struct task *task, uint32_t events)
904{
905 struct desktop *desktop =
Benjamin Franzked7759712011-11-22 12:38:48 +0100906 container_of(task, struct desktop, unlock_task);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200907
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800908 weston_desktop_shell_unlock(desktop->shell);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200909 unlock_dialog_destroy(desktop->unlock_dialog);
910 desktop->unlock_dialog = NULL;
911}
912
913static void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400914desktop_shell_configure(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800915 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400916 uint32_t edges,
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400917 struct wl_surface *surface,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400918 int32_t width, int32_t height)
919{
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400920 struct window *window = wl_surface_get_user_data(surface);
Pekka Paalanen068ae942011-11-28 14:11:15 +0200921 struct surface *s = window_get_user_data(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400922
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400923 s->configure(data, desktop_shell, edges, window, width, height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400924}
925
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200926static void
927desktop_shell_prepare_lock_surface(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800928 struct weston_desktop_shell *desktop_shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200929{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200930 struct desktop *desktop = data;
931
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300932 if (!desktop->locking) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800933 weston_desktop_shell_unlock(desktop->shell);
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200934 return;
935 }
936
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200937 if (!desktop->unlock_dialog) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500938 desktop->unlock_dialog = unlock_dialog_create(desktop);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200939 desktop->unlock_dialog->desktop = desktop;
940 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200941}
942
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300943static void
944desktop_shell_grab_cursor(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800945 struct weston_desktop_shell *desktop_shell,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300946 uint32_t cursor)
947{
948 struct desktop *desktop = data;
949
950 switch (cursor) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800951 case WESTON_DESKTOP_SHELL_CURSOR_NONE:
Philipp Brüschweiler16d59d72012-08-24 15:43:55 +0200952 desktop->grab_cursor = CURSOR_BLANK;
953 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800954 case WESTON_DESKTOP_SHELL_CURSOR_BUSY:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300955 desktop->grab_cursor = CURSOR_WATCH;
956 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800957 case WESTON_DESKTOP_SHELL_CURSOR_MOVE:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300958 desktop->grab_cursor = CURSOR_DRAGGING;
959 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800960 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300961 desktop->grab_cursor = CURSOR_TOP;
962 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800963 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300964 desktop->grab_cursor = CURSOR_BOTTOM;
965 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800966 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_LEFT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300967 desktop->grab_cursor = CURSOR_LEFT;
968 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800969 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_RIGHT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300970 desktop->grab_cursor = CURSOR_RIGHT;
971 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800972 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP_LEFT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300973 desktop->grab_cursor = CURSOR_TOP_LEFT;
974 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800975 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP_RIGHT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300976 desktop->grab_cursor = CURSOR_TOP_RIGHT;
977 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800978 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_LEFT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300979 desktop->grab_cursor = CURSOR_BOTTOM_LEFT;
980 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800981 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_RIGHT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300982 desktop->grab_cursor = CURSOR_BOTTOM_RIGHT;
983 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800984 case WESTON_DESKTOP_SHELL_CURSOR_ARROW:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300985 default:
986 desktop->grab_cursor = CURSOR_LEFT_PTR;
987 }
988}
989
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800990static const struct weston_desktop_shell_listener listener = {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200991 desktop_shell_configure,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300992 desktop_shell_prepare_lock_surface,
993 desktop_shell_grab_cursor
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400994};
995
U. Artie Eoff44874d92012-10-02 21:12:35 -0700996static void
997background_destroy(struct background *background)
998{
999 widget_destroy(background->widget);
1000 window_destroy(background->window);
1001
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001002 free(background->image);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001003 free(background);
1004}
1005
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001006static struct background *
1007background_create(struct desktop *desktop)
1008{
1009 struct background *background;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001010 struct weston_config_section *s;
1011 char *type;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001012
Peter Huttererf3d62272013-08-08 11:57:05 +10001013 background = xzalloc(sizeof *background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001014 background->base.configure = background_configure;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001015 background->window = window_create_custom(desktop->display);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001016 background->widget = window_add_widget(background->window, background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001017 window_set_user_data(background->window, background);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001018 widget_set_redraw_handler(background->widget, background_draw);
Arnaud Vracfb754a02014-08-25 20:56:49 +02001019 widget_set_transparent(background->widget, 0);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001020 window_set_preferred_format(background->window,
1021 WINDOW_PREFERRED_FORMAT_RGB565);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001022
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001023 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
1024 weston_config_section_get_string(s, "background-image",
Kristian Høgsberg4c3661f2014-04-21 22:54:37 -07001025 &background->image, NULL);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001026 weston_config_section_get_uint(s, "background-color",
Kristian Høgsberg4c3661f2014-04-21 22:54:37 -07001027 &background->color, 0);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001028
1029 weston_config_section_get_string(s, "background-type",
1030 &type, "tile");
U. Artie Eoff3c946772014-01-15 10:59:50 -08001031 if (type == NULL) {
1032 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
1033 exit(EXIT_FAILURE);
1034 }
1035
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001036 if (strcmp(type, "scale") == 0) {
1037 background->type = BACKGROUND_SCALE;
1038 } else if (strcmp(type, "scale-crop") == 0) {
1039 background->type = BACKGROUND_SCALE_CROP;
1040 } else if (strcmp(type, "tile") == 0) {
1041 background->type = BACKGROUND_TILE;
1042 } else {
1043 background->type = -1;
1044 fprintf(stderr, "invalid background-type: %s\n",
1045 type);
1046 }
1047
1048 free(type);
1049
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001050 return background;
1051}
1052
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001053static int
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001054grab_surface_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001055 float x, float y, void *data)
1056{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001057 struct desktop *desktop = data;
1058
1059 return desktop->grab_cursor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001060}
1061
1062static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001063grab_surface_destroy(struct desktop *desktop)
1064{
1065 widget_destroy(desktop->grab_widget);
1066 window_destroy(desktop->grab_window);
1067}
1068
1069static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001070grab_surface_create(struct desktop *desktop)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001071{
1072 struct wl_surface *s;
1073
Ander Conselvan de Oliveira07a91cd2012-07-16 14:15:50 +03001074 desktop->grab_window = window_create_custom(desktop->display);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001075 window_set_user_data(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001076
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001077 s = window_get_wl_surface(desktop->grab_window);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001078 weston_desktop_shell_set_grab_surface(desktop->shell, s);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001079
1080 desktop->grab_widget =
1081 window_add_widget(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001082 /* We set the allocation to 1x1 at 0,0 so the fake enter event
1083 * at 0,0 will go to this widget. */
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001084 widget_set_allocation(desktop->grab_widget, 0, 0, 1, 1);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001085
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001086 widget_set_enter_handler(desktop->grab_widget,
1087 grab_surface_enter_handler);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001088}
1089
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001090static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001091output_destroy(struct output *output)
1092{
1093 background_destroy(output->background);
Jonny Lambe67118c2014-08-12 15:07:51 +02001094 if (output->panel)
1095 panel_destroy(output->panel);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001096 wl_output_destroy(output->output);
1097 wl_list_remove(&output->link);
1098
1099 free(output);
1100}
1101
1102static void
1103desktop_destroy_outputs(struct desktop *desktop)
1104{
1105 struct output *tmp;
1106 struct output *output;
1107
1108 wl_list_for_each_safe(output, tmp, &desktop->outputs, link)
1109 output_destroy(output);
1110}
1111
1112static void
Alexander Larssonc584fa62013-05-22 14:41:32 +02001113output_handle_geometry(void *data,
1114 struct wl_output *wl_output,
1115 int x, int y,
1116 int physical_width,
1117 int physical_height,
1118 int subpixel,
1119 const char *make,
1120 const char *model,
1121 int transform)
1122{
1123 struct output *output = data;
1124
Jonny Lambe67118c2014-08-12 15:07:51 +02001125 if (output->panel)
1126 window_set_buffer_transform(output->panel->window, transform);
Alexander Larssonc584fa62013-05-22 14:41:32 +02001127 window_set_buffer_transform(output->background->window, transform);
1128}
1129
1130static void
1131output_handle_mode(void *data,
1132 struct wl_output *wl_output,
1133 uint32_t flags,
1134 int width,
1135 int height,
1136 int refresh)
1137{
1138}
1139
1140static void
1141output_handle_done(void *data,
1142 struct wl_output *wl_output)
1143{
1144}
1145
1146static void
1147output_handle_scale(void *data,
1148 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001149 int32_t scale)
Alexander Larssonc584fa62013-05-22 14:41:32 +02001150{
1151 struct output *output = data;
1152
Jonny Lambe67118c2014-08-12 15:07:51 +02001153 if (output->panel)
1154 window_set_buffer_scale(output->panel->window, scale);
Alexander Larssonc584fa62013-05-22 14:41:32 +02001155 window_set_buffer_scale(output->background->window, scale);
1156}
1157
1158static const struct wl_output_listener output_listener = {
1159 output_handle_geometry,
1160 output_handle_mode,
1161 output_handle_done,
1162 output_handle_scale
1163};
1164
Jonny Lambe67118c2014-08-12 15:07:51 +02001165static int
1166want_panel(struct desktop *desktop)
1167{
1168 struct weston_config_section *s;
1169 char *location = NULL;
1170 int ret = 1;
1171
1172 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
1173 weston_config_section_get_string(s, "panel-location",
1174 &location, "top");
1175
1176 if (strcmp(location, "top") != 0)
1177 ret = 0;
1178
1179 free(location);
1180
1181 return ret;
1182}
1183
Alexander Larssonc584fa62013-05-22 14:41:32 +02001184static void
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001185output_init(struct output *output, struct desktop *desktop)
1186{
1187 struct wl_surface *surface;
1188
Jonny Lambe67118c2014-08-12 15:07:51 +02001189 if (want_panel(desktop)) {
1190 output->panel = panel_create(desktop);
1191 surface = window_get_wl_surface(output->panel->window);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001192 weston_desktop_shell_set_panel(desktop->shell,
1193 output->output, surface);
Jonny Lambe67118c2014-08-12 15:07:51 +02001194 }
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001195
1196 output->background = background_create(desktop);
1197 surface = window_get_wl_surface(output->background->window);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001198 weston_desktop_shell_set_background(desktop->shell,
1199 output->output, surface);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001200}
1201
1202static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001203create_output(struct desktop *desktop, uint32_t id)
1204{
1205 struct output *output;
1206
Bryce Harrington0d1a6222016-02-11 16:42:49 -08001207 output = zalloc(sizeof *output);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001208 if (!output)
1209 return;
1210
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001211 output->output =
Alexander Larssonc584fa62013-05-22 14:41:32 +02001212 display_bind(desktop->display, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001213 output->server_output_id = id;
Alexander Larssonc584fa62013-05-22 14:41:32 +02001214
1215 wl_output_add_listener(output->output, &output_listener, output);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001216
1217 wl_list_insert(&desktop->outputs, &output->link);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001218
1219 /* On start up we may process an output global before the shell global
1220 * in which case we can't create the panel and background just yet */
1221 if (desktop->shell)
1222 output_init(output, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001223}
1224
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001225static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001226global_handler(struct display *display, uint32_t id,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001227 const char *interface, uint32_t version, void *data)
1228{
1229 struct desktop *desktop = data;
1230
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001231 if (!strcmp(interface, "weston_desktop_shell")) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001232 desktop->shell = display_bind(desktop->display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001233 id,
1234 &weston_desktop_shell_interface,
1235 1);
1236 weston_desktop_shell_add_listener(desktop->shell,
1237 &listener,
1238 desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001239 } else if (!strcmp(interface, "wl_output")) {
1240 create_output(desktop, id);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001241 }
1242}
1243
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001244static void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001245global_handler_remove(struct display *display, uint32_t id,
1246 const char *interface, uint32_t version, void *data)
1247{
1248 struct desktop *desktop = data;
1249 struct output *output;
1250
1251 if (!strcmp(interface, "wl_output")) {
1252 wl_list_for_each(output, &desktop->outputs, link) {
1253 if (output->server_output_id == id) {
1254 output_destroy(output);
1255 break;
1256 }
1257 }
1258 }
1259}
1260
1261static void
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001262panel_add_launchers(struct panel *panel, struct desktop *desktop)
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001263{
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001264 struct weston_config_section *s;
1265 char *icon, *path;
1266 const char *name;
1267 int count;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001268
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001269 count = 0;
1270 s = NULL;
1271 while (weston_config_next_section(desktop->config, &s, &name)) {
1272 if (strcmp(name, "launcher") != 0)
1273 continue;
1274
1275 weston_config_section_get_string(s, "icon", &icon, NULL);
1276 weston_config_section_get_string(s, "path", &path, NULL);
1277
1278 if (icon != NULL && path != NULL) {
1279 panel_add_launcher(panel, icon, path);
Rob Bradford09252d42013-07-26 16:29:45 +01001280 count++;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001281 } else {
1282 fprintf(stderr, "invalid launcher section\n");
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001283 }
1284
1285 free(icon);
1286 free(path);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001287 }
1288
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001289 if (count == 0) {
1290 /* add default launcher */
1291 panel_add_launcher(panel,
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001292 DATADIR "/weston/terminal.png",
Rodney Lorrimar99ff01b2012-02-29 17:31:03 +01001293 BINDIR "/weston-terminal");
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001294 }
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001295}
1296
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001297int main(int argc, char *argv[])
1298{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001299 struct desktop desktop = { 0 };
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001300 struct output *output;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001301 struct weston_config_section *s;
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001302 const char *config_file;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001303
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001304 desktop.unlock_task.run = unlock_dialog_finish;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001305 wl_list_init(&desktop.outputs);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001306
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001307 config_file = weston_config_get_name_from_env();
1308 desktop.config = weston_config_parse(config_file);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001309 s = weston_config_get_section(desktop.config, "shell", NULL, NULL);
1310 weston_config_section_get_bool(s, "locking", &desktop.locking, 1);
1311
Kristian Høgsberg4172f662013-02-20 15:27:49 -05001312 desktop.display = display_create(&argc, argv);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001313 if (desktop.display == NULL) {
1314 fprintf(stderr, "failed to create display: %m\n");
1315 return -1;
1316 }
1317
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001318 display_set_user_data(desktop.display, &desktop);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001319 display_set_global_handler(desktop.display, global_handler);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001320 display_set_global_handler_remove(desktop.display, global_handler_remove);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001321
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001322 /* Create panel and background for outputs processed before the shell
1323 * global interface was processed */
1324 wl_list_for_each(output, &desktop.outputs, link)
1325 if (!output->panel)
1326 output_init(output, &desktop);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001327
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001328 grab_surface_create(&desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001329
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001330 signal(SIGCHLD, sigchild_handler);
1331
1332 display_run(desktop.display);
1333
U. Artie Eoff44874d92012-10-02 21:12:35 -07001334 /* Cleanup */
1335 grab_surface_destroy(&desktop);
1336 desktop_destroy_outputs(&desktop);
1337 if (desktop.unlock_dialog)
1338 unlock_dialog_destroy(desktop.unlock_dialog);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001339 weston_desktop_shell_destroy(desktop.shell);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001340 display_destroy(desktop.display);
1341
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001342 return 0;
1343}