blob: 6ab76dc744ecbcbf5e1f8d1547a9b6e6e7cd54d6 [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"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040049
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080050#include "weston-desktop-shell-client-protocol.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040051
Pekka Paalanenb6df4f72012-08-03 14:39:15 +030052extern char **environ; /* defined by libc */
53
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040054struct desktop {
55 struct display *display;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080056 struct weston_desktop_shell *shell;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +020057 struct unlock_dialog *unlock_dialog;
58 struct task unlock_task;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010059 struct wl_list outputs;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040060
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030061 struct window *grab_window;
62 struct widget *grab_widget;
63
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +030064 struct weston_config *config;
65 int locking;
66
Scott Moreauec116022012-07-22 18:23:52 -060067 enum cursor_type grab_cursor;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030068
69 int painted;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010070};
71
72struct surface {
73 void (*configure)(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080074 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040075 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010076 int32_t width, int32_t height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040077};
78
79struct panel {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010080 struct surface base;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040081 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050082 struct widget *widget;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050083 struct wl_list launcher_list;
Martin Minarik1e51a872012-06-08 00:39:11 +020084 struct panel_clock *clock;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030085 int painted;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +030086 uint32_t color;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040087};
88
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010089struct background {
90 struct surface base;
91 struct window *window;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050092 struct widget *widget;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030093 int painted;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +030094
95 char *image;
96 int type;
97 uint32_t color;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010098};
99
100struct output {
101 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800102 uint32_t server_output_id;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100103 struct wl_list link;
104
105 struct panel *panel;
106 struct background *background;
107};
108
Kristian Høgsberg53880802012-01-09 11:16:50 -0500109struct panel_launcher {
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500110 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400111 struct panel *panel;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400112 cairo_surface_t *icon;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500113 int focused, pressed;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400114 char *path;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500115 struct wl_list link;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400116 struct wl_array envp;
117 struct wl_array argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400118};
119
Martin Minarik1e51a872012-06-08 00:39:11 +0200120struct panel_clock {
121 struct widget *widget;
122 struct panel *panel;
Martin Minarik1e51a872012-06-08 00:39:11 +0200123 struct task clock_task;
124 int clock_fd;
125};
126
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200127struct unlock_dialog {
128 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500129 struct widget *widget;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500130 struct widget *button;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500131 int button_focused;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200132 int closing;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200133 struct desktop *desktop;
134};
135
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300136static void
137panel_add_launchers(struct panel *panel, struct desktop *desktop);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500138
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400139static void
140sigchild_handler(int s)
141{
142 int status;
143 pid_t pid;
144
145 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
146 fprintf(stderr, "child %d exited\n", pid);
147}
148
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300149static int
150is_desktop_painted(struct desktop *desktop)
151{
152 struct output *output;
153
154 wl_list_for_each(output, &desktop->outputs, link) {
155 if (output->panel && !output->panel->painted)
156 return 0;
157 if (output->background && !output->background->painted)
158 return 0;
159 }
160
161 return 1;
162}
163
164static void
165check_desktop_ready(struct window *window)
166{
167 struct display *display;
168 struct desktop *desktop;
169
170 display = window_get_display(window);
171 desktop = display_get_user_data(display);
172
173 if (!desktop->painted && is_desktop_painted(desktop)) {
174 desktop->painted = 1;
175
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800176 weston_desktop_shell_desktop_ready(desktop->shell);
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300177 }
178}
179
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400180static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500181panel_launcher_activate(struct panel_launcher *widget)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400182{
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400183 char **argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400184 pid_t pid;
185
186 pid = fork();
187 if (pid < 0) {
188 fprintf(stderr, "fork failed: %m\n");
189 return;
190 }
191
192 if (pid)
193 return;
Benjamin Franzked7759712011-11-22 12:38:48 +0100194
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400195 argv = widget->argv.data;
196 if (execve(argv[0], argv, widget->envp.data) < 0) {
197 fprintf(stderr, "execl '%s' failed: %m\n", argv[0]);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400198 exit(1);
199 }
200}
201
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400202static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500203panel_launcher_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400204{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500205 struct panel_launcher *launcher = data;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500206 struct rectangle allocation;
207 cairo_t *cr;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400208
Alexander Larssonc584fa62013-05-22 14:41:32 +0200209 cr = widget_cairo_create(launcher->panel->widget);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500210
211 widget_get_allocation(widget, &allocation);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500212 if (launcher->pressed) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500213 allocation.x++;
214 allocation.y++;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400215 }
216
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500217 cairo_set_source_surface(cr, launcher->icon,
218 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400219 cairo_paint(cr);
220
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500221 if (launcher->focused) {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400222 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500223 cairo_mask_surface(cr, launcher->icon,
224 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400225 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400226
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500227 cairo_destroy(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400228}
229
Tiago Vignatti61500722012-05-23 22:06:28 +0300230static int
231panel_launcher_motion_handler(struct widget *widget, struct input *input,
232 uint32_t time, float x, float y, void *data)
233{
234 struct panel_launcher *launcher = data;
235
236 widget_set_tooltip(widget, basename((char *)launcher->path), x, y);
237
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300238 return CURSOR_LEFT_PTR;
Tiago Vignatti61500722012-05-23 22:06:28 +0300239}
240
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400241static void
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500242set_hex_color(cairo_t *cr, uint32_t color)
243{
Michael Vetter2a18a522015-05-15 17:17:47 +0200244 cairo_set_source_rgba(cr,
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500245 ((color >> 16) & 0xff) / 255.0,
246 ((color >> 8) & 0xff) / 255.0,
247 ((color >> 0) & 0xff) / 255.0,
248 ((color >> 24) & 0xff) / 255.0);
249}
250
251static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500252panel_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400253{
254 cairo_surface_t *surface;
255 cairo_t *cr;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500256 struct panel *panel = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400257
Alexander Larssonc584fa62013-05-22 14:41:32 +0200258 cr = widget_cairo_create(panel->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400259 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300260 set_hex_color(cr, panel->color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400261 cairo_paint(cr);
262
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400263 cairo_destroy(cr);
Alexander Larssonc584fa62013-05-22 14:41:32 +0200264 surface = window_get_surface(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400265 cairo_surface_destroy(surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300266 panel->painted = 1;
267 check_desktop_ready(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400268}
269
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500270static int
Kristian Høgsberg53880802012-01-09 11:16:50 -0500271panel_launcher_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400272 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500273{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500274 struct panel_launcher *launcher = data;
275
276 launcher->focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500277 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500278
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300279 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500280}
281
282static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500283panel_launcher_leave_handler(struct widget *widget,
284 struct input *input, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400285{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500286 struct panel_launcher *launcher = data;
287
288 launcher->focused = 0;
Tiago Vignatti61500722012-05-23 22:06:28 +0300289 widget_destroy_tooltip(widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500290 widget_schedule_redraw(widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400291}
292
293static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500294panel_launcher_button_handler(struct widget *widget,
295 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100296 uint32_t button,
297 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400298{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500299 struct panel_launcher *launcher;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400300
Kristian Høgsberg53880802012-01-09 11:16:50 -0500301 launcher = widget_get_user_data(widget);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500302 widget_schedule_redraw(widget);
Daniel Stone4dbadb12012-05-30 16:31:51 +0100303 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg53880802012-01-09 11:16:50 -0500304 panel_launcher_activate(launcher);
Rusty Lynch4384a242013-08-08 21:28:22 -0700305
306}
307
308static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700309panel_launcher_touch_down_handler(struct widget *widget, struct input *input,
310 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch4384a242013-08-08 21:28:22 -0700311 float x, float y, void *data)
312{
313 struct panel_launcher *launcher;
314
315 launcher = widget_get_user_data(widget);
316 launcher->focused = 1;
317 widget_schedule_redraw(widget);
318}
319
320static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700321panel_launcher_touch_up_handler(struct widget *widget, struct input *input,
Michael Vetter2a18a522015-05-15 17:17:47 +0200322 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch1084da52013-08-15 09:10:08 -0700323 void *data)
Rusty Lynch4384a242013-08-08 21:28:22 -0700324{
325 struct panel_launcher *launcher;
326
327 launcher = widget_get_user_data(widget);
328 launcher->focused = 0;
329 widget_schedule_redraw(widget);
330 panel_launcher_activate(launcher);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500331}
332
Martin Minarik1e51a872012-06-08 00:39:11 +0200333static void
334clock_func(struct task *task, uint32_t events)
335{
336 struct panel_clock *clock =
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400337 container_of(task, struct panel_clock, clock_task);
Kristian Høgsberg70226bb2012-06-08 16:54:52 -0400338 uint64_t exp;
Martin Minarik1e51a872012-06-08 00:39:11 +0200339
Martin Olsson8df662a2012-07-08 03:03:47 +0200340 if (read(clock->clock_fd, &exp, sizeof exp) != sizeof exp)
341 abort();
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400342 widget_schedule_redraw(clock->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200343}
344
345static void
346panel_clock_redraw_handler(struct widget *widget, void *data)
347{
Martin Minarik1e51a872012-06-08 00:39:11 +0200348 struct panel_clock *clock = data;
349 cairo_t *cr;
350 struct rectangle allocation;
351 cairo_text_extents_t extents;
352 cairo_font_extents_t font_extents;
Martin Minarik1e51a872012-06-08 00:39:11 +0200353 time_t rawtime;
354 struct tm * timeinfo;
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400355 char string[128];
Martin Minarik1e51a872012-06-08 00:39:11 +0200356
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400357 time(&rawtime);
358 timeinfo = localtime(&rawtime);
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400359 strftime(string, sizeof string, "%a %b %d, %I:%M %p", timeinfo);
Martin Minarik1e51a872012-06-08 00:39:11 +0200360
361 widget_get_allocation(widget, &allocation);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400362 if (allocation.width == 0)
363 return;
Martin Minarik1e51a872012-06-08 00:39:11 +0200364
Alexander Larssonc584fa62013-05-22 14:41:32 +0200365 cr = widget_cairo_create(clock->panel->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200366 cairo_select_font_face(cr, "sans",
367 CAIRO_FONT_SLANT_NORMAL,
368 CAIRO_FONT_WEIGHT_NORMAL);
369 cairo_set_font_size(cr, 14);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400370 cairo_text_extents(cr, string, &extents);
Martin Minarik1e51a872012-06-08 00:39:11 +0200371 cairo_font_extents (cr, &font_extents);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400372 cairo_move_to(cr, allocation.x + 5,
373 allocation.y + 3 * (allocation.height >> 2) + 1);
374 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400375 cairo_show_text(cr, string);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400376 cairo_move_to(cr, allocation.x + 4,
377 allocation.y + 3 * (allocation.height >> 2));
378 cairo_set_source_rgb(cr, 1, 1, 1);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400379 cairo_show_text(cr, string);
Martin Minarik1e51a872012-06-08 00:39:11 +0200380 cairo_destroy(cr);
381}
382
383static int
384clock_timer_reset(struct panel_clock *clock)
385{
386 struct itimerspec its;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400387
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400388 its.it_interval.tv_sec = 60;
Martin Minarik1e51a872012-06-08 00:39:11 +0200389 its.it_interval.tv_nsec = 0;
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400390 its.it_value.tv_sec = 60;
Martin Minarik1e51a872012-06-08 00:39:11 +0200391 its.it_value.tv_nsec = 0;
392 if (timerfd_settime(clock->clock_fd, 0, &its, NULL) < 0) {
393 fprintf(stderr, "could not set timerfd\n: %m");
394 return -1;
395 }
396
397 return 0;
398}
399
400static void
U. Artie Eoff44874d92012-10-02 21:12:35 -0700401panel_destroy_clock(struct panel_clock *clock)
402{
403 widget_destroy(clock->widget);
404
405 close(clock->clock_fd);
406
407 free(clock);
408}
409
410static void
Martin Minarik1e51a872012-06-08 00:39:11 +0200411panel_add_clock(struct panel *panel)
412{
413 struct panel_clock *clock;
414 int timerfd;
415
416 timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
417 if (timerfd < 0) {
418 fprintf(stderr, "could not create timerfd\n: %m");
419 return;
420 }
421
Peter Huttererf3d62272013-08-08 11:57:05 +1000422 clock = xzalloc(sizeof *clock);
Martin Minarik1e51a872012-06-08 00:39:11 +0200423 clock->panel = panel;
424 panel->clock = clock;
425 clock->clock_fd = timerfd;
426
427 clock->clock_task.run = clock_func;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400428 display_watch_fd(window_get_display(panel->window), clock->clock_fd,
429 EPOLLIN, &clock->clock_task);
Martin Minarik1e51a872012-06-08 00:39:11 +0200430 clock_timer_reset(clock);
431
432 clock->widget = widget_add_widget(panel->widget, clock);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400433 widget_set_redraw_handler(clock->widget, panel_clock_redraw_handler);
Martin Minarik1e51a872012-06-08 00:39:11 +0200434}
435
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500436static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500437panel_resize_handler(struct widget *widget,
438 int32_t width, int32_t height, void *data)
439{
440 struct panel_launcher *launcher;
441 struct panel *panel = data;
442 int x, y, w, h;
Michael Vetter2a18a522015-05-15 17:17:47 +0200443
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500444 x = 10;
445 y = 16;
446 wl_list_for_each(launcher, &panel->launcher_list, link) {
447 w = cairo_image_surface_get_width(launcher->icon);
448 h = cairo_image_surface_get_height(launcher->icon);
449 widget_set_allocation(launcher->widget,
450 x, y - h / 2, w + 1, h + 1);
451 x += w + 10;
452 }
Martin Minarik1e51a872012-06-08 00:39:11 +0200453 h=20;
454 w=170;
Pekka Paalanen01b17252012-06-12 17:42:26 +0300455
456 if (panel->clock)
457 widget_set_allocation(panel->clock->widget,
458 width - w - 8, y - h / 2, w + 1, h + 1);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500459}
460
461static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100462panel_configure(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800463 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400464 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100465 int32_t width, int32_t height)
466{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500467 struct surface *surface = window_get_user_data(window);
468 struct panel *panel = container_of(surface, struct panel, base);
469
470 window_schedule_resize(panel->window, width, 32);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100471}
472
U. Artie Eoff44874d92012-10-02 21:12:35 -0700473static void
474panel_destroy_launcher(struct panel_launcher *launcher)
475{
476 wl_array_release(&launcher->argv);
477 wl_array_release(&launcher->envp);
478
479 free(launcher->path);
480
481 cairo_surface_destroy(launcher->icon);
482
483 widget_destroy(launcher->widget);
484 wl_list_remove(&launcher->link);
485
486 free(launcher);
487}
488
489static void
490panel_destroy(struct panel *panel)
491{
492 struct panel_launcher *tmp;
493 struct panel_launcher *launcher;
494
495 panel_destroy_clock(panel->clock);
496
497 wl_list_for_each_safe(launcher, tmp, &panel->launcher_list, link)
498 panel_destroy_launcher(launcher);
499
500 widget_destroy(panel->widget);
501 window_destroy(panel->window);
502
503 free(panel);
504}
505
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400506static struct panel *
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300507panel_create(struct desktop *desktop)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400508{
509 struct panel *panel;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300510 struct weston_config_section *s;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400511
Peter Huttererf3d62272013-08-08 11:57:05 +1000512 panel = xzalloc(sizeof *panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400513
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100514 panel->base.configure = panel_configure;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300515 panel->window = window_create_custom(desktop->display);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500516 panel->widget = window_add_widget(panel->window, panel);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500517 wl_list_init(&panel->launcher_list);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400518
519 window_set_title(panel->window, "panel");
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400520 window_set_user_data(panel->window, panel);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500521
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500522 widget_set_redraw_handler(panel->widget, panel_redraw_handler);
523 widget_set_resize_handler(panel->widget, panel_resize_handler);
Michael Vetter2a18a522015-05-15 17:17:47 +0200524
Rafal Mielniczukb9e513c2012-06-09 20:33:29 +0200525 panel_add_clock(panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400526
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300527 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
528 weston_config_section_get_uint(s, "panel-color",
529 &panel->color, 0xaa000000);
530
531 panel_add_launchers(panel, desktop);
532
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400533 return panel;
534}
535
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200536static cairo_surface_t *
537load_icon_or_fallback(const char *icon)
538{
539 cairo_surface_t *surface = cairo_image_surface_create_from_png(icon);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200540 cairo_status_t status;
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200541 cairo_t *cr;
542
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200543 status = cairo_surface_status(surface);
544 if (status == CAIRO_STATUS_SUCCESS)
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200545 return surface;
546
547 cairo_surface_destroy(surface);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200548 fprintf(stderr, "ERROR loading icon from file '%s', error: '%s'\n",
549 icon, cairo_status_to_string(status));
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200550
551 /* draw fallback icon */
552 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
553 20, 20);
554 cr = cairo_create(surface);
555
556 cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1);
557 cairo_paint(cr);
558
559 cairo_set_source_rgba(cr, 0, 0, 0, 1);
560 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
561 cairo_rectangle(cr, 0, 0, 20, 20);
562 cairo_move_to(cr, 4, 4);
563 cairo_line_to(cr, 16, 16);
564 cairo_move_to(cr, 4, 16);
565 cairo_line_to(cr, 16, 4);
566 cairo_stroke(cr);
567
568 cairo_destroy(cr);
569
570 return surface;
571}
572
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400573static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500574panel_add_launcher(struct panel *panel, const char *icon, const char *path)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400575{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500576 struct panel_launcher *launcher;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400577 char *start, *p, *eq, **ps;
578 int i, j, k;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400579
Peter Huttererf3d62272013-08-08 11:57:05 +1000580 launcher = xzalloc(sizeof *launcher);
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200581 launcher->icon = load_icon_or_fallback(icon);
U. Artie Eoff3c946772014-01-15 10:59:50 -0800582 launcher->path = xstrdup(path);
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400583
584 wl_array_init(&launcher->envp);
585 wl_array_init(&launcher->argv);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300586 for (i = 0; environ[i]; i++) {
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400587 ps = wl_array_add(&launcher->envp, sizeof *ps);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300588 *ps = environ[i];
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400589 }
590 j = 0;
591
592 start = launcher->path;
593 while (*start) {
594 for (p = start, eq = NULL; *p && !isspace(*p); p++)
595 if (*p == '=')
596 eq = p;
597
598 if (eq && j == 0) {
599 ps = launcher->envp.data;
600 for (k = 0; k < i; k++)
601 if (strncmp(ps[k], start, eq - start) == 0) {
602 ps[k] = start;
603 break;
604 }
605 if (k == i) {
606 ps = wl_array_add(&launcher->envp, sizeof *ps);
607 *ps = start;
608 i++;
609 }
610 } else {
611 ps = wl_array_add(&launcher->argv, sizeof *ps);
612 *ps = start;
613 j++;
614 }
615
616 while (*p && isspace(*p))
617 *p++ = '\0';
618
619 start = p;
620 }
621
622 ps = wl_array_add(&launcher->envp, sizeof *ps);
623 *ps = NULL;
624 ps = wl_array_add(&launcher->argv, sizeof *ps);
625 *ps = NULL;
626
Kristian Høgsberg53880802012-01-09 11:16:50 -0500627 launcher->panel = panel;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500628 wl_list_insert(panel->launcher_list.prev, &launcher->link);
629
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500630 launcher->widget = widget_add_widget(panel->widget, launcher);
Kristian Høgsberg53880802012-01-09 11:16:50 -0500631 widget_set_enter_handler(launcher->widget,
632 panel_launcher_enter_handler);
633 widget_set_leave_handler(launcher->widget,
634 panel_launcher_leave_handler);
635 widget_set_button_handler(launcher->widget,
636 panel_launcher_button_handler);
Rusty Lynch4384a242013-08-08 21:28:22 -0700637 widget_set_touch_down_handler(launcher->widget,
638 panel_launcher_touch_down_handler);
639 widget_set_touch_up_handler(launcher->widget,
640 panel_launcher_touch_up_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500641 widget_set_redraw_handler(launcher->widget,
642 panel_launcher_redraw_handler);
Tiago Vignatti61500722012-05-23 22:06:28 +0300643 widget_set_motion_handler(launcher->widget,
644 panel_launcher_motion_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400645}
646
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500647enum {
648 BACKGROUND_SCALE,
Pekka Paalanena402b052013-05-22 18:03:10 +0300649 BACKGROUND_SCALE_CROP,
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500650 BACKGROUND_TILE
651};
652
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400653static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500654background_draw(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400655{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500656 struct background *background = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400657 cairo_surface_t *surface, *image;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400658 cairo_pattern_t *pattern;
659 cairo_matrix_t matrix;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400660 cairo_t *cr;
Pekka Paalanena402b052013-05-22 18:03:10 +0300661 double im_w, im_h;
662 double sx, sy, s;
663 double tx, ty;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500664 struct rectangle allocation;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400665
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500666 surface = window_get_surface(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400667
Alexander Larssonc584fa62013-05-22 14:41:32 +0200668 cr = widget_cairo_create(background->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400669 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
670 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
671 cairo_paint(cr);
672
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500673 widget_get_allocation(widget, &allocation);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500674 image = NULL;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300675 if (background->image)
676 image = load_cairo_surface(background->image);
Kristian Høgsberg4c3661f2014-04-21 22:54:37 -0700677 else if (background->color == 0)
678 image = load_cairo_surface(DATADIR "/weston/pattern.png");
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500679
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300680 if (image && background->type != -1) {
Pekka Paalanena402b052013-05-22 18:03:10 +0300681 im_w = cairo_image_surface_get_width(image);
682 im_h = cairo_image_surface_get_height(image);
683 sx = im_w / allocation.width;
684 sy = im_h / allocation.height;
685
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400686 pattern = cairo_pattern_create_for_surface(image);
Pekka Paalanena402b052013-05-22 18:03:10 +0300687
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300688 switch (background->type) {
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500689 case BACKGROUND_SCALE:
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500690 cairo_matrix_init_scale(&matrix, sx, sy);
691 cairo_pattern_set_matrix(pattern, &matrix);
Bill Spitzak79b7cb32014-05-08 20:00:35 -0700692 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500693 break;
Pekka Paalanena402b052013-05-22 18:03:10 +0300694 case BACKGROUND_SCALE_CROP:
695 s = (sx < sy) ? sx : sy;
696 /* align center */
697 tx = (im_w - s * allocation.width) * 0.5;
698 ty = (im_h - s * allocation.height) * 0.5;
699 cairo_matrix_init_translate(&matrix, tx, ty);
700 cairo_matrix_scale(&matrix, s, s);
701 cairo_pattern_set_matrix(pattern, &matrix);
Bill Spitzak79b7cb32014-05-08 20:00:35 -0700702 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
Pekka Paalanena402b052013-05-22 18:03:10 +0300703 break;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500704 case BACKGROUND_TILE:
705 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
706 break;
707 }
Pekka Paalanena402b052013-05-22 18:03:10 +0300708
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400709 cairo_set_source(cr, pattern);
710 cairo_pattern_destroy (pattern);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400711 cairo_surface_destroy(image);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500712 } else {
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300713 set_hex_color(cr, background->color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400714 }
715
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500716 cairo_paint(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400717 cairo_destroy(cr);
718 cairo_surface_destroy(surface);
Pekka Paalanen9564c752012-10-24 09:43:08 +0300719
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300720 background->painted = 1;
721 check_desktop_ready(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400722}
723
724static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100725background_configure(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800726 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400727 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100728 int32_t width, int32_t height)
729{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500730 struct background *background =
731 (struct background *) window_get_user_data(window);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100732
Kristian Høgsberg4598f962014-01-01 16:32:09 -0800733 widget_schedule_resize(background->widget, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100734}
735
736static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500737unlock_dialog_redraw_handler(struct widget *widget, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200738{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500739 struct unlock_dialog *dialog = data;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200740 struct rectangle allocation;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200741 cairo_surface_t *surface;
Alexander Larssonc584fa62013-05-22 14:41:32 +0200742 cairo_t *cr;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200743 cairo_pattern_t *pat;
744 double cx, cy, r, f;
745
Alexander Larssonc584fa62013-05-22 14:41:32 +0200746 cr = widget_cairo_create(widget);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500747
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500748 widget_get_allocation(dialog->widget, &allocation);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200749 cairo_rectangle(cr, allocation.x, allocation.y,
750 allocation.width, allocation.height);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200751 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
752 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500753 cairo_fill(cr);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200754
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500755 cairo_translate(cr, allocation.x, allocation.y);
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500756 if (dialog->button_focused)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200757 f = 1.0;
758 else
759 f = 0.7;
760
761 cx = allocation.width / 2.0;
762 cy = allocation.height / 2.0;
763 r = (cx < cy ? cx : cy) * 0.4;
764 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
765 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
766 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
767 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
768 cairo_set_source(cr, pat);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500769 cairo_pattern_destroy(pat);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200770 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
771 cairo_fill(cr);
772
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500773 widget_set_allocation(dialog->button,
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500774 allocation.x + cx - r,
775 allocation.y + cy - r, 2 * r, 2 * r);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200776
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200777 cairo_destroy(cr);
778
Alexander Larssonc584fa62013-05-22 14:41:32 +0200779 surface = window_get_surface(dialog->window);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200780 cairo_surface_destroy(surface);
781}
782
783static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500784unlock_dialog_button_handler(struct widget *widget,
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200785 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100786 uint32_t button,
787 enum wl_pointer_button_state state, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200788{
789 struct unlock_dialog *dialog = data;
790 struct desktop *desktop = dialog->desktop;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200791
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500792 if (button == BTN_LEFT) {
Daniel Stone4dbadb12012-05-30 16:31:51 +0100793 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
794 !dialog->closing) {
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200795 display_defer(desktop->display, &desktop->unlock_task);
796 dialog->closing = 1;
797 }
798 }
799}
800
801static void
Brian J Lovinc4df4082013-08-26 15:58:22 -0700802unlock_dialog_touch_down_handler(struct widget *widget, struct input *input,
803 uint32_t serial, uint32_t time, int32_t id,
804 float x, float y, void *data)
805{
806 struct unlock_dialog *dialog = data;
807
808 dialog->button_focused = 1;
809 widget_schedule_redraw(widget);
810}
811
812static void
813unlock_dialog_touch_up_handler(struct widget *widget, struct input *input,
814 uint32_t serial, uint32_t time, int32_t id,
815 void *data)
816{
817 struct unlock_dialog *dialog = data;
818 struct desktop *desktop = dialog->desktop;
819
820 dialog->button_focused = 0;
821 widget_schedule_redraw(widget);
822 display_defer(desktop->display, &desktop->unlock_task);
823 dialog->closing = 1;
824}
825
826static void
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200827unlock_dialog_keyboard_focus_handler(struct window *window,
828 struct input *device, void *data)
829{
830 window_schedule_redraw(window);
831}
832
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500833static int
Kristian Høgsbergee143232012-01-09 08:42:24 -0500834unlock_dialog_widget_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400835 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400836 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500837{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500838 struct unlock_dialog *dialog = data;
839
840 dialog->button_focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500841 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500842
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300843 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500844}
845
846static void
847unlock_dialog_widget_leave_handler(struct widget *widget,
848 struct input *input, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200849{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500850 struct unlock_dialog *dialog = data;
851
852 dialog->button_focused = 0;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500853 widget_schedule_redraw(widget);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200854}
855
856static struct unlock_dialog *
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500857unlock_dialog_create(struct desktop *desktop)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200858{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500859 struct display *display = desktop->display;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200860 struct unlock_dialog *dialog;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800861 struct wl_surface *surface;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200862
Peter Huttererf3d62272013-08-08 11:57:05 +1000863 dialog = xzalloc(sizeof *dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200864
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400865 dialog->window = window_create_custom(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500866 dialog->widget = window_frame_create(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200867 window_set_title(dialog->window, "Unlock your desktop");
868
869 window_set_user_data(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200870 window_set_keyboard_focus_handler(dialog->window,
871 unlock_dialog_keyboard_focus_handler);
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500872 dialog->button = widget_add_widget(dialog->widget, dialog);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500873 widget_set_redraw_handler(dialog->widget,
874 unlock_dialog_redraw_handler);
Kristian Høgsbergee143232012-01-09 08:42:24 -0500875 widget_set_enter_handler(dialog->button,
876 unlock_dialog_widget_enter_handler);
877 widget_set_leave_handler(dialog->button,
878 unlock_dialog_widget_leave_handler);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500879 widget_set_button_handler(dialog->button,
880 unlock_dialog_button_handler);
Brian J Lovinc4df4082013-08-26 15:58:22 -0700881 widget_set_touch_down_handler(dialog->button,
882 unlock_dialog_touch_down_handler);
883 widget_set_touch_up_handler(dialog->button,
884 unlock_dialog_touch_up_handler);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200885
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800886 surface = window_get_wl_surface(dialog->window);
887 weston_desktop_shell_set_lock_surface(desktop->shell, surface);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500888
Pekka Paalanen40e49ac2012-01-18 16:51:30 +0200889 window_schedule_resize(dialog->window, 260, 230);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200890
891 return dialog;
892}
893
894static void
895unlock_dialog_destroy(struct unlock_dialog *dialog)
896{
897 window_destroy(dialog->window);
898 free(dialog);
899}
900
901static void
902unlock_dialog_finish(struct task *task, uint32_t events)
903{
904 struct desktop *desktop =
Benjamin Franzked7759712011-11-22 12:38:48 +0100905 container_of(task, struct desktop, unlock_task);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200906
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800907 weston_desktop_shell_unlock(desktop->shell);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200908 unlock_dialog_destroy(desktop->unlock_dialog);
909 desktop->unlock_dialog = NULL;
910}
911
912static void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400913desktop_shell_configure(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800914 struct weston_desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400915 uint32_t edges,
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400916 struct wl_surface *surface,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400917 int32_t width, int32_t height)
918{
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400919 struct window *window = wl_surface_get_user_data(surface);
Pekka Paalanen068ae942011-11-28 14:11:15 +0200920 struct surface *s = window_get_user_data(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400921
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400922 s->configure(data, desktop_shell, edges, window, width, height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400923}
924
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200925static void
926desktop_shell_prepare_lock_surface(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800927 struct weston_desktop_shell *desktop_shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200928{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200929 struct desktop *desktop = data;
930
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300931 if (!desktop->locking) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800932 weston_desktop_shell_unlock(desktop->shell);
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200933 return;
934 }
935
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200936 if (!desktop->unlock_dialog) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500937 desktop->unlock_dialog = unlock_dialog_create(desktop);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200938 desktop->unlock_dialog->desktop = desktop;
939 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200940}
941
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300942static void
943desktop_shell_grab_cursor(void *data,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800944 struct weston_desktop_shell *desktop_shell,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300945 uint32_t cursor)
946{
947 struct desktop *desktop = data;
948
949 switch (cursor) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800950 case WESTON_DESKTOP_SHELL_CURSOR_NONE:
Philipp Brüschweiler16d59d72012-08-24 15:43:55 +0200951 desktop->grab_cursor = CURSOR_BLANK;
952 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800953 case WESTON_DESKTOP_SHELL_CURSOR_BUSY:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300954 desktop->grab_cursor = CURSOR_WATCH;
955 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800956 case WESTON_DESKTOP_SHELL_CURSOR_MOVE:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300957 desktop->grab_cursor = CURSOR_DRAGGING;
958 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800959 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300960 desktop->grab_cursor = CURSOR_TOP;
961 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800962 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300963 desktop->grab_cursor = CURSOR_BOTTOM;
964 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800965 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_LEFT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300966 desktop->grab_cursor = CURSOR_LEFT;
967 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800968 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_RIGHT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300969 desktop->grab_cursor = CURSOR_RIGHT;
970 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800971 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP_LEFT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300972 desktop->grab_cursor = CURSOR_TOP_LEFT;
973 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800974 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_TOP_RIGHT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300975 desktop->grab_cursor = CURSOR_TOP_RIGHT;
976 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800977 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_LEFT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300978 desktop->grab_cursor = CURSOR_BOTTOM_LEFT;
979 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800980 case WESTON_DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_RIGHT:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300981 desktop->grab_cursor = CURSOR_BOTTOM_RIGHT;
982 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800983 case WESTON_DESKTOP_SHELL_CURSOR_ARROW:
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300984 default:
985 desktop->grab_cursor = CURSOR_LEFT_PTR;
986 }
987}
988
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800989static const struct weston_desktop_shell_listener listener = {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200990 desktop_shell_configure,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300991 desktop_shell_prepare_lock_surface,
992 desktop_shell_grab_cursor
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400993};
994
U. Artie Eoff44874d92012-10-02 21:12:35 -0700995static void
996background_destroy(struct background *background)
997{
998 widget_destroy(background->widget);
999 window_destroy(background->window);
1000
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001001 free(background->image);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001002 free(background);
1003}
1004
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001005static struct background *
1006background_create(struct desktop *desktop)
1007{
1008 struct background *background;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001009 struct weston_config_section *s;
1010 char *type;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001011
Peter Huttererf3d62272013-08-08 11:57:05 +10001012 background = xzalloc(sizeof *background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001013 background->base.configure = background_configure;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001014 background->window = window_create_custom(desktop->display);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001015 background->widget = window_add_widget(background->window, background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001016 window_set_user_data(background->window, background);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001017 widget_set_redraw_handler(background->widget, background_draw);
Arnaud Vracfb754a02014-08-25 20:56:49 +02001018 widget_set_transparent(background->widget, 0);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001019 window_set_preferred_format(background->window,
1020 WINDOW_PREFERRED_FORMAT_RGB565);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001021
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001022 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
1023 weston_config_section_get_string(s, "background-image",
Kristian Høgsberg4c3661f2014-04-21 22:54:37 -07001024 &background->image, NULL);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001025 weston_config_section_get_uint(s, "background-color",
Kristian Høgsberg4c3661f2014-04-21 22:54:37 -07001026 &background->color, 0);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001027
1028 weston_config_section_get_string(s, "background-type",
1029 &type, "tile");
U. Artie Eoff3c946772014-01-15 10:59:50 -08001030 if (type == NULL) {
1031 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
1032 exit(EXIT_FAILURE);
1033 }
1034
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001035 if (strcmp(type, "scale") == 0) {
1036 background->type = BACKGROUND_SCALE;
1037 } else if (strcmp(type, "scale-crop") == 0) {
1038 background->type = BACKGROUND_SCALE_CROP;
1039 } else if (strcmp(type, "tile") == 0) {
1040 background->type = BACKGROUND_TILE;
1041 } else {
1042 background->type = -1;
1043 fprintf(stderr, "invalid background-type: %s\n",
1044 type);
1045 }
1046
1047 free(type);
1048
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001049 return background;
1050}
1051
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001052static int
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001053grab_surface_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001054 float x, float y, void *data)
1055{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001056 struct desktop *desktop = data;
1057
1058 return desktop->grab_cursor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001059}
1060
1061static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001062grab_surface_destroy(struct desktop *desktop)
1063{
1064 widget_destroy(desktop->grab_widget);
1065 window_destroy(desktop->grab_window);
1066}
1067
1068static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001069grab_surface_create(struct desktop *desktop)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001070{
1071 struct wl_surface *s;
1072
Ander Conselvan de Oliveira07a91cd2012-07-16 14:15:50 +03001073 desktop->grab_window = window_create_custom(desktop->display);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001074 window_set_user_data(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001075
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001076 s = window_get_wl_surface(desktop->grab_window);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001077 weston_desktop_shell_set_grab_surface(desktop->shell, s);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001078
1079 desktop->grab_widget =
1080 window_add_widget(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001081 /* We set the allocation to 1x1 at 0,0 so the fake enter event
1082 * at 0,0 will go to this widget. */
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001083 widget_set_allocation(desktop->grab_widget, 0, 0, 1, 1);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001084
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001085 widget_set_enter_handler(desktop->grab_widget,
1086 grab_surface_enter_handler);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001087}
1088
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001089static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001090output_destroy(struct output *output)
1091{
1092 background_destroy(output->background);
Jonny Lambe67118c2014-08-12 15:07:51 +02001093 if (output->panel)
1094 panel_destroy(output->panel);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001095 wl_output_destroy(output->output);
1096 wl_list_remove(&output->link);
1097
1098 free(output);
1099}
1100
1101static void
1102desktop_destroy_outputs(struct desktop *desktop)
1103{
1104 struct output *tmp;
1105 struct output *output;
1106
1107 wl_list_for_each_safe(output, tmp, &desktop->outputs, link)
1108 output_destroy(output);
1109}
1110
1111static void
Alexander Larssonc584fa62013-05-22 14:41:32 +02001112output_handle_geometry(void *data,
1113 struct wl_output *wl_output,
1114 int x, int y,
1115 int physical_width,
1116 int physical_height,
1117 int subpixel,
1118 const char *make,
1119 const char *model,
1120 int transform)
1121{
1122 struct output *output = data;
1123
Jonny Lambe67118c2014-08-12 15:07:51 +02001124 if (output->panel)
1125 window_set_buffer_transform(output->panel->window, transform);
Alexander Larssonc584fa62013-05-22 14:41:32 +02001126 window_set_buffer_transform(output->background->window, transform);
1127}
1128
1129static void
1130output_handle_mode(void *data,
1131 struct wl_output *wl_output,
1132 uint32_t flags,
1133 int width,
1134 int height,
1135 int refresh)
1136{
1137}
1138
1139static void
1140output_handle_done(void *data,
1141 struct wl_output *wl_output)
1142{
1143}
1144
1145static void
1146output_handle_scale(void *data,
1147 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001148 int32_t scale)
Alexander Larssonc584fa62013-05-22 14:41:32 +02001149{
1150 struct output *output = data;
1151
Jonny Lambe67118c2014-08-12 15:07:51 +02001152 if (output->panel)
1153 window_set_buffer_scale(output->panel->window, scale);
Alexander Larssonc584fa62013-05-22 14:41:32 +02001154 window_set_buffer_scale(output->background->window, scale);
1155}
1156
1157static const struct wl_output_listener output_listener = {
1158 output_handle_geometry,
1159 output_handle_mode,
1160 output_handle_done,
1161 output_handle_scale
1162};
1163
Jonny Lambe67118c2014-08-12 15:07:51 +02001164static int
1165want_panel(struct desktop *desktop)
1166{
1167 struct weston_config_section *s;
1168 char *location = NULL;
1169 int ret = 1;
1170
1171 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
1172 weston_config_section_get_string(s, "panel-location",
1173 &location, "top");
1174
1175 if (strcmp(location, "top") != 0)
1176 ret = 0;
1177
1178 free(location);
1179
1180 return ret;
1181}
1182
Alexander Larssonc584fa62013-05-22 14:41:32 +02001183static void
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001184output_init(struct output *output, struct desktop *desktop)
1185{
1186 struct wl_surface *surface;
1187
Jonny Lambe67118c2014-08-12 15:07:51 +02001188 if (want_panel(desktop)) {
1189 output->panel = panel_create(desktop);
1190 surface = window_get_wl_surface(output->panel->window);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001191 weston_desktop_shell_set_panel(desktop->shell,
1192 output->output, surface);
Jonny Lambe67118c2014-08-12 15:07:51 +02001193 }
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001194
1195 output->background = background_create(desktop);
1196 surface = window_get_wl_surface(output->background->window);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001197 weston_desktop_shell_set_background(desktop->shell,
1198 output->output, surface);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001199}
1200
1201static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001202create_output(struct desktop *desktop, uint32_t id)
1203{
1204 struct output *output;
1205
1206 output = calloc(1, sizeof *output);
1207 if (!output)
1208 return;
1209
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001210 output->output =
Alexander Larssonc584fa62013-05-22 14:41:32 +02001211 display_bind(desktop->display, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001212 output->server_output_id = id;
Alexander Larssonc584fa62013-05-22 14:41:32 +02001213
1214 wl_output_add_listener(output->output, &output_listener, output);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001215
1216 wl_list_insert(&desktop->outputs, &output->link);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001217
1218 /* On start up we may process an output global before the shell global
1219 * in which case we can't create the panel and background just yet */
1220 if (desktop->shell)
1221 output_init(output, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001222}
1223
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001224static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001225global_handler(struct display *display, uint32_t id,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001226 const char *interface, uint32_t version, void *data)
1227{
1228 struct desktop *desktop = data;
1229
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001230 if (!strcmp(interface, "weston_desktop_shell")) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001231 desktop->shell = display_bind(desktop->display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001232 id,
1233 &weston_desktop_shell_interface,
1234 1);
1235 weston_desktop_shell_add_listener(desktop->shell,
1236 &listener,
1237 desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001238 } else if (!strcmp(interface, "wl_output")) {
1239 create_output(desktop, id);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001240 }
1241}
1242
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001243static void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001244global_handler_remove(struct display *display, uint32_t id,
1245 const char *interface, uint32_t version, void *data)
1246{
1247 struct desktop *desktop = data;
1248 struct output *output;
1249
1250 if (!strcmp(interface, "wl_output")) {
1251 wl_list_for_each(output, &desktop->outputs, link) {
1252 if (output->server_output_id == id) {
1253 output_destroy(output);
1254 break;
1255 }
1256 }
1257 }
1258}
1259
1260static void
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001261panel_add_launchers(struct panel *panel, struct desktop *desktop)
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001262{
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001263 struct weston_config_section *s;
1264 char *icon, *path;
1265 const char *name;
1266 int count;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001267
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001268 count = 0;
1269 s = NULL;
1270 while (weston_config_next_section(desktop->config, &s, &name)) {
1271 if (strcmp(name, "launcher") != 0)
1272 continue;
1273
1274 weston_config_section_get_string(s, "icon", &icon, NULL);
1275 weston_config_section_get_string(s, "path", &path, NULL);
1276
1277 if (icon != NULL && path != NULL) {
1278 panel_add_launcher(panel, icon, path);
Rob Bradford09252d42013-07-26 16:29:45 +01001279 count++;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001280 } else {
1281 fprintf(stderr, "invalid launcher section\n");
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001282 }
1283
1284 free(icon);
1285 free(path);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001286 }
1287
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001288 if (count == 0) {
1289 /* add default launcher */
1290 panel_add_launcher(panel,
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001291 DATADIR "/weston/terminal.png",
Rodney Lorrimar99ff01b2012-02-29 17:31:03 +01001292 BINDIR "/weston-terminal");
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001293 }
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001294}
1295
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001296int main(int argc, char *argv[])
1297{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001298 struct desktop desktop = { 0 };
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001299 struct output *output;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001300 struct weston_config_section *s;
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001301 const char *config_file;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001302
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001303 desktop.unlock_task.run = unlock_dialog_finish;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001304 wl_list_init(&desktop.outputs);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001305
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001306 config_file = weston_config_get_name_from_env();
1307 desktop.config = weston_config_parse(config_file);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001308 s = weston_config_get_section(desktop.config, "shell", NULL, NULL);
1309 weston_config_section_get_bool(s, "locking", &desktop.locking, 1);
1310
Kristian Høgsberg4172f662013-02-20 15:27:49 -05001311 desktop.display = display_create(&argc, argv);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001312 if (desktop.display == NULL) {
1313 fprintf(stderr, "failed to create display: %m\n");
1314 return -1;
1315 }
1316
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001317 display_set_user_data(desktop.display, &desktop);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001318 display_set_global_handler(desktop.display, global_handler);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001319 display_set_global_handler_remove(desktop.display, global_handler_remove);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001320
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001321 /* Create panel and background for outputs processed before the shell
1322 * global interface was processed */
1323 wl_list_for_each(output, &desktop.outputs, link)
1324 if (!output->panel)
1325 output_init(output, &desktop);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001326
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001327 grab_surface_create(&desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001328
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001329 signal(SIGCHLD, sigchild_handler);
1330
1331 display_run(desktop.display);
1332
U. Artie Eoff44874d92012-10-02 21:12:35 -07001333 /* Cleanup */
1334 grab_surface_destroy(&desktop);
1335 desktop_destroy_outputs(&desktop);
1336 if (desktop.unlock_dialog)
1337 unlock_dialog_destroy(desktop.unlock_dialog);
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001338 weston_desktop_shell_destroy(desktop.shell);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001339 display_destroy(desktop.display);
1340
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001341 return 0;
1342}