blob: 9cf3d686a125396e739fd76b72972557db97da39 [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
Pekka Paalanen50719bc2011-11-22 14:18:50 +020050#include "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;
56 struct desktop_shell *shell;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030057 uint32_t interface_version;
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,
75 struct 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
177 if (desktop->interface_version >= 2)
178 desktop_shell_desktop_ready(desktop->shell);
179 }
180}
181
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400182static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500183panel_launcher_activate(struct panel_launcher *widget)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400184{
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400185 char **argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400186 pid_t pid;
187
188 pid = fork();
189 if (pid < 0) {
190 fprintf(stderr, "fork failed: %m\n");
191 return;
192 }
193
194 if (pid)
195 return;
Benjamin Franzked7759712011-11-22 12:38:48 +0100196
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400197 argv = widget->argv.data;
198 if (execve(argv[0], argv, widget->envp.data) < 0) {
199 fprintf(stderr, "execl '%s' failed: %m\n", argv[0]);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400200 exit(1);
201 }
202}
203
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400204static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500205panel_launcher_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400206{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500207 struct panel_launcher *launcher = data;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500208 struct rectangle allocation;
209 cairo_t *cr;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400210
Alexander Larssonc584fa62013-05-22 14:41:32 +0200211 cr = widget_cairo_create(launcher->panel->widget);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500212
213 widget_get_allocation(widget, &allocation);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500214 if (launcher->pressed) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500215 allocation.x++;
216 allocation.y++;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400217 }
218
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500219 cairo_set_source_surface(cr, launcher->icon,
220 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400221 cairo_paint(cr);
222
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500223 if (launcher->focused) {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400224 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500225 cairo_mask_surface(cr, launcher->icon,
226 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400227 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400228
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500229 cairo_destroy(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400230}
231
Tiago Vignatti61500722012-05-23 22:06:28 +0300232static int
233panel_launcher_motion_handler(struct widget *widget, struct input *input,
234 uint32_t time, float x, float y, void *data)
235{
236 struct panel_launcher *launcher = data;
237
238 widget_set_tooltip(widget, basename((char *)launcher->path), x, y);
239
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300240 return CURSOR_LEFT_PTR;
Tiago Vignatti61500722012-05-23 22:06:28 +0300241}
242
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400243static void
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500244set_hex_color(cairo_t *cr, uint32_t color)
245{
Michael Vetter2a18a522015-05-15 17:17:47 +0200246 cairo_set_source_rgba(cr,
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500247 ((color >> 16) & 0xff) / 255.0,
248 ((color >> 8) & 0xff) / 255.0,
249 ((color >> 0) & 0xff) / 255.0,
250 ((color >> 24) & 0xff) / 255.0);
251}
252
253static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500254panel_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400255{
256 cairo_surface_t *surface;
257 cairo_t *cr;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500258 struct panel *panel = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400259
Alexander Larssonc584fa62013-05-22 14:41:32 +0200260 cr = widget_cairo_create(panel->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400261 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300262 set_hex_color(cr, panel->color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400263 cairo_paint(cr);
264
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400265 cairo_destroy(cr);
Alexander Larssonc584fa62013-05-22 14:41:32 +0200266 surface = window_get_surface(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400267 cairo_surface_destroy(surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300268 panel->painted = 1;
269 check_desktop_ready(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400270}
271
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500272static int
Kristian Høgsberg53880802012-01-09 11:16:50 -0500273panel_launcher_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400274 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500275{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500276 struct panel_launcher *launcher = data;
277
278 launcher->focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500279 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500280
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300281 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500282}
283
284static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500285panel_launcher_leave_handler(struct widget *widget,
286 struct input *input, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400287{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500288 struct panel_launcher *launcher = data;
289
290 launcher->focused = 0;
Tiago Vignatti61500722012-05-23 22:06:28 +0300291 widget_destroy_tooltip(widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500292 widget_schedule_redraw(widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400293}
294
295static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500296panel_launcher_button_handler(struct widget *widget,
297 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100298 uint32_t button,
299 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400300{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500301 struct panel_launcher *launcher;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400302
Kristian Høgsberg53880802012-01-09 11:16:50 -0500303 launcher = widget_get_user_data(widget);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500304 widget_schedule_redraw(widget);
Daniel Stone4dbadb12012-05-30 16:31:51 +0100305 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg53880802012-01-09 11:16:50 -0500306 panel_launcher_activate(launcher);
Rusty Lynch4384a242013-08-08 21:28:22 -0700307
308}
309
310static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700311panel_launcher_touch_down_handler(struct widget *widget, struct input *input,
312 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch4384a242013-08-08 21:28:22 -0700313 float x, float y, void *data)
314{
315 struct panel_launcher *launcher;
316
317 launcher = widget_get_user_data(widget);
318 launcher->focused = 1;
319 widget_schedule_redraw(widget);
320}
321
322static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700323panel_launcher_touch_up_handler(struct widget *widget, struct input *input,
Michael Vetter2a18a522015-05-15 17:17:47 +0200324 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch1084da52013-08-15 09:10:08 -0700325 void *data)
Rusty Lynch4384a242013-08-08 21:28:22 -0700326{
327 struct panel_launcher *launcher;
328
329 launcher = widget_get_user_data(widget);
330 launcher->focused = 0;
331 widget_schedule_redraw(widget);
332 panel_launcher_activate(launcher);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500333}
334
Martin Minarik1e51a872012-06-08 00:39:11 +0200335static void
336clock_func(struct task *task, uint32_t events)
337{
338 struct panel_clock *clock =
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400339 container_of(task, struct panel_clock, clock_task);
Kristian Høgsberg70226bb2012-06-08 16:54:52 -0400340 uint64_t exp;
Martin Minarik1e51a872012-06-08 00:39:11 +0200341
Martin Olsson8df662a2012-07-08 03:03:47 +0200342 if (read(clock->clock_fd, &exp, sizeof exp) != sizeof exp)
343 abort();
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400344 widget_schedule_redraw(clock->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200345}
346
347static void
348panel_clock_redraw_handler(struct widget *widget, void *data)
349{
Martin Minarik1e51a872012-06-08 00:39:11 +0200350 struct panel_clock *clock = data;
351 cairo_t *cr;
352 struct rectangle allocation;
353 cairo_text_extents_t extents;
354 cairo_font_extents_t font_extents;
Martin Minarik1e51a872012-06-08 00:39:11 +0200355 time_t rawtime;
356 struct tm * timeinfo;
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400357 char string[128];
Martin Minarik1e51a872012-06-08 00:39:11 +0200358
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400359 time(&rawtime);
360 timeinfo = localtime(&rawtime);
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400361 strftime(string, sizeof string, "%a %b %d, %I:%M %p", timeinfo);
Martin Minarik1e51a872012-06-08 00:39:11 +0200362
363 widget_get_allocation(widget, &allocation);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400364 if (allocation.width == 0)
365 return;
Martin Minarik1e51a872012-06-08 00:39:11 +0200366
Alexander Larssonc584fa62013-05-22 14:41:32 +0200367 cr = widget_cairo_create(clock->panel->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200368 cairo_select_font_face(cr, "sans",
369 CAIRO_FONT_SLANT_NORMAL,
370 CAIRO_FONT_WEIGHT_NORMAL);
371 cairo_set_font_size(cr, 14);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400372 cairo_text_extents(cr, string, &extents);
Martin Minarik1e51a872012-06-08 00:39:11 +0200373 cairo_font_extents (cr, &font_extents);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400374 cairo_move_to(cr, allocation.x + 5,
375 allocation.y + 3 * (allocation.height >> 2) + 1);
376 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400377 cairo_show_text(cr, string);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400378 cairo_move_to(cr, allocation.x + 4,
379 allocation.y + 3 * (allocation.height >> 2));
380 cairo_set_source_rgb(cr, 1, 1, 1);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400381 cairo_show_text(cr, string);
Martin Minarik1e51a872012-06-08 00:39:11 +0200382 cairo_destroy(cr);
383}
384
385static int
386clock_timer_reset(struct panel_clock *clock)
387{
388 struct itimerspec its;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400389
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400390 its.it_interval.tv_sec = 60;
Martin Minarik1e51a872012-06-08 00:39:11 +0200391 its.it_interval.tv_nsec = 0;
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400392 its.it_value.tv_sec = 60;
Martin Minarik1e51a872012-06-08 00:39:11 +0200393 its.it_value.tv_nsec = 0;
394 if (timerfd_settime(clock->clock_fd, 0, &its, NULL) < 0) {
395 fprintf(stderr, "could not set timerfd\n: %m");
396 return -1;
397 }
398
399 return 0;
400}
401
402static void
U. Artie Eoff44874d92012-10-02 21:12:35 -0700403panel_destroy_clock(struct panel_clock *clock)
404{
405 widget_destroy(clock->widget);
406
407 close(clock->clock_fd);
408
409 free(clock);
410}
411
412static void
Martin Minarik1e51a872012-06-08 00:39:11 +0200413panel_add_clock(struct panel *panel)
414{
415 struct panel_clock *clock;
416 int timerfd;
417
418 timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
419 if (timerfd < 0) {
420 fprintf(stderr, "could not create timerfd\n: %m");
421 return;
422 }
423
Peter Huttererf3d62272013-08-08 11:57:05 +1000424 clock = xzalloc(sizeof *clock);
Martin Minarik1e51a872012-06-08 00:39:11 +0200425 clock->panel = panel;
426 panel->clock = clock;
427 clock->clock_fd = timerfd;
428
429 clock->clock_task.run = clock_func;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400430 display_watch_fd(window_get_display(panel->window), clock->clock_fd,
431 EPOLLIN, &clock->clock_task);
Martin Minarik1e51a872012-06-08 00:39:11 +0200432 clock_timer_reset(clock);
433
434 clock->widget = widget_add_widget(panel->widget, clock);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400435 widget_set_redraw_handler(clock->widget, panel_clock_redraw_handler);
Martin Minarik1e51a872012-06-08 00:39:11 +0200436}
437
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500438static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500439panel_resize_handler(struct widget *widget,
440 int32_t width, int32_t height, void *data)
441{
442 struct panel_launcher *launcher;
443 struct panel *panel = data;
444 int x, y, w, h;
Michael Vetter2a18a522015-05-15 17:17:47 +0200445
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500446 x = 10;
447 y = 16;
448 wl_list_for_each(launcher, &panel->launcher_list, link) {
449 w = cairo_image_surface_get_width(launcher->icon);
450 h = cairo_image_surface_get_height(launcher->icon);
451 widget_set_allocation(launcher->widget,
452 x, y - h / 2, w + 1, h + 1);
453 x += w + 10;
454 }
Martin Minarik1e51a872012-06-08 00:39:11 +0200455 h=20;
456 w=170;
Pekka Paalanen01b17252012-06-12 17:42:26 +0300457
458 if (panel->clock)
459 widget_set_allocation(panel->clock->widget,
460 width - w - 8, y - h / 2, w + 1, h + 1);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500461}
462
463static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100464panel_configure(void *data,
465 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400466 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100467 int32_t width, int32_t height)
468{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500469 struct surface *surface = window_get_user_data(window);
470 struct panel *panel = container_of(surface, struct panel, base);
471
472 window_schedule_resize(panel->window, width, 32);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100473}
474
U. Artie Eoff44874d92012-10-02 21:12:35 -0700475static void
476panel_destroy_launcher(struct panel_launcher *launcher)
477{
478 wl_array_release(&launcher->argv);
479 wl_array_release(&launcher->envp);
480
481 free(launcher->path);
482
483 cairo_surface_destroy(launcher->icon);
484
485 widget_destroy(launcher->widget);
486 wl_list_remove(&launcher->link);
487
488 free(launcher);
489}
490
491static void
492panel_destroy(struct panel *panel)
493{
494 struct panel_launcher *tmp;
495 struct panel_launcher *launcher;
496
497 panel_destroy_clock(panel->clock);
498
499 wl_list_for_each_safe(launcher, tmp, &panel->launcher_list, link)
500 panel_destroy_launcher(launcher);
501
502 widget_destroy(panel->widget);
503 window_destroy(panel->window);
504
505 free(panel);
506}
507
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400508static struct panel *
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300509panel_create(struct desktop *desktop)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400510{
511 struct panel *panel;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300512 struct weston_config_section *s;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400513
Peter Huttererf3d62272013-08-08 11:57:05 +1000514 panel = xzalloc(sizeof *panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400515
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100516 panel->base.configure = panel_configure;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300517 panel->window = window_create_custom(desktop->display);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500518 panel->widget = window_add_widget(panel->window, panel);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500519 wl_list_init(&panel->launcher_list);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400520
521 window_set_title(panel->window, "panel");
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400522 window_set_user_data(panel->window, panel);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500523
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500524 widget_set_redraw_handler(panel->widget, panel_redraw_handler);
525 widget_set_resize_handler(panel->widget, panel_resize_handler);
Michael Vetter2a18a522015-05-15 17:17:47 +0200526
Rafal Mielniczukb9e513c2012-06-09 20:33:29 +0200527 panel_add_clock(panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400528
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300529 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
530 weston_config_section_get_uint(s, "panel-color",
531 &panel->color, 0xaa000000);
532
533 panel_add_launchers(panel, desktop);
534
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400535 return panel;
536}
537
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200538static cairo_surface_t *
539load_icon_or_fallback(const char *icon)
540{
541 cairo_surface_t *surface = cairo_image_surface_create_from_png(icon);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200542 cairo_status_t status;
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200543 cairo_t *cr;
544
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200545 status = cairo_surface_status(surface);
546 if (status == CAIRO_STATUS_SUCCESS)
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200547 return surface;
548
549 cairo_surface_destroy(surface);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200550 fprintf(stderr, "ERROR loading icon from file '%s', error: '%s'\n",
551 icon, cairo_status_to_string(status));
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200552
553 /* draw fallback icon */
554 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
555 20, 20);
556 cr = cairo_create(surface);
557
558 cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1);
559 cairo_paint(cr);
560
561 cairo_set_source_rgba(cr, 0, 0, 0, 1);
562 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
563 cairo_rectangle(cr, 0, 0, 20, 20);
564 cairo_move_to(cr, 4, 4);
565 cairo_line_to(cr, 16, 16);
566 cairo_move_to(cr, 4, 16);
567 cairo_line_to(cr, 16, 4);
568 cairo_stroke(cr);
569
570 cairo_destroy(cr);
571
572 return surface;
573}
574
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400575static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500576panel_add_launcher(struct panel *panel, const char *icon, const char *path)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400577{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500578 struct panel_launcher *launcher;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400579 char *start, *p, *eq, **ps;
580 int i, j, k;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400581
Peter Huttererf3d62272013-08-08 11:57:05 +1000582 launcher = xzalloc(sizeof *launcher);
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200583 launcher->icon = load_icon_or_fallback(icon);
U. Artie Eoff3c946772014-01-15 10:59:50 -0800584 launcher->path = xstrdup(path);
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400585
586 wl_array_init(&launcher->envp);
587 wl_array_init(&launcher->argv);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300588 for (i = 0; environ[i]; i++) {
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400589 ps = wl_array_add(&launcher->envp, sizeof *ps);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300590 *ps = environ[i];
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400591 }
592 j = 0;
593
594 start = launcher->path;
595 while (*start) {
596 for (p = start, eq = NULL; *p && !isspace(*p); p++)
597 if (*p == '=')
598 eq = p;
599
600 if (eq && j == 0) {
601 ps = launcher->envp.data;
602 for (k = 0; k < i; k++)
603 if (strncmp(ps[k], start, eq - start) == 0) {
604 ps[k] = start;
605 break;
606 }
607 if (k == i) {
608 ps = wl_array_add(&launcher->envp, sizeof *ps);
609 *ps = start;
610 i++;
611 }
612 } else {
613 ps = wl_array_add(&launcher->argv, sizeof *ps);
614 *ps = start;
615 j++;
616 }
617
618 while (*p && isspace(*p))
619 *p++ = '\0';
620
621 start = p;
622 }
623
624 ps = wl_array_add(&launcher->envp, sizeof *ps);
625 *ps = NULL;
626 ps = wl_array_add(&launcher->argv, sizeof *ps);
627 *ps = NULL;
628
Kristian Høgsberg53880802012-01-09 11:16:50 -0500629 launcher->panel = panel;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500630 wl_list_insert(panel->launcher_list.prev, &launcher->link);
631
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500632 launcher->widget = widget_add_widget(panel->widget, launcher);
Kristian Høgsberg53880802012-01-09 11:16:50 -0500633 widget_set_enter_handler(launcher->widget,
634 panel_launcher_enter_handler);
635 widget_set_leave_handler(launcher->widget,
636 panel_launcher_leave_handler);
637 widget_set_button_handler(launcher->widget,
638 panel_launcher_button_handler);
Rusty Lynch4384a242013-08-08 21:28:22 -0700639 widget_set_touch_down_handler(launcher->widget,
640 panel_launcher_touch_down_handler);
641 widget_set_touch_up_handler(launcher->widget,
642 panel_launcher_touch_up_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500643 widget_set_redraw_handler(launcher->widget,
644 panel_launcher_redraw_handler);
Tiago Vignatti61500722012-05-23 22:06:28 +0300645 widget_set_motion_handler(launcher->widget,
646 panel_launcher_motion_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400647}
648
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500649enum {
650 BACKGROUND_SCALE,
Pekka Paalanena402b052013-05-22 18:03:10 +0300651 BACKGROUND_SCALE_CROP,
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500652 BACKGROUND_TILE
653};
654
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400655static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500656background_draw(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400657{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500658 struct background *background = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400659 cairo_surface_t *surface, *image;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400660 cairo_pattern_t *pattern;
661 cairo_matrix_t matrix;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400662 cairo_t *cr;
Pekka Paalanena402b052013-05-22 18:03:10 +0300663 double im_w, im_h;
664 double sx, sy, s;
665 double tx, ty;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500666 struct rectangle allocation;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400667
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500668 surface = window_get_surface(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400669
Alexander Larssonc584fa62013-05-22 14:41:32 +0200670 cr = widget_cairo_create(background->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400671 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
672 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
673 cairo_paint(cr);
674
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500675 widget_get_allocation(widget, &allocation);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500676 image = NULL;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300677 if (background->image)
678 image = load_cairo_surface(background->image);
Kristian Høgsberg4c3661f2014-04-21 22:54:37 -0700679 else if (background->color == 0)
680 image = load_cairo_surface(DATADIR "/weston/pattern.png");
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500681
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300682 if (image && background->type != -1) {
Pekka Paalanena402b052013-05-22 18:03:10 +0300683 im_w = cairo_image_surface_get_width(image);
684 im_h = cairo_image_surface_get_height(image);
685 sx = im_w / allocation.width;
686 sy = im_h / allocation.height;
687
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400688 pattern = cairo_pattern_create_for_surface(image);
Pekka Paalanena402b052013-05-22 18:03:10 +0300689
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300690 switch (background->type) {
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500691 case BACKGROUND_SCALE:
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500692 cairo_matrix_init_scale(&matrix, sx, sy);
693 cairo_pattern_set_matrix(pattern, &matrix);
Bill Spitzak79b7cb32014-05-08 20:00:35 -0700694 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500695 break;
Pekka Paalanena402b052013-05-22 18:03:10 +0300696 case BACKGROUND_SCALE_CROP:
697 s = (sx < sy) ? sx : sy;
698 /* align center */
699 tx = (im_w - s * allocation.width) * 0.5;
700 ty = (im_h - s * allocation.height) * 0.5;
701 cairo_matrix_init_translate(&matrix, tx, ty);
702 cairo_matrix_scale(&matrix, s, s);
703 cairo_pattern_set_matrix(pattern, &matrix);
Bill Spitzak79b7cb32014-05-08 20:00:35 -0700704 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
Pekka Paalanena402b052013-05-22 18:03:10 +0300705 break;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500706 case BACKGROUND_TILE:
707 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
708 break;
709 }
Pekka Paalanena402b052013-05-22 18:03:10 +0300710
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400711 cairo_set_source(cr, pattern);
712 cairo_pattern_destroy (pattern);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400713 cairo_surface_destroy(image);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500714 } else {
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300715 set_hex_color(cr, background->color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400716 }
717
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500718 cairo_paint(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400719 cairo_destroy(cr);
720 cairo_surface_destroy(surface);
Pekka Paalanen9564c752012-10-24 09:43:08 +0300721
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300722 background->painted = 1;
723 check_desktop_ready(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400724}
725
726static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100727background_configure(void *data,
728 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400729 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100730 int32_t width, int32_t height)
731{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500732 struct background *background =
733 (struct background *) window_get_user_data(window);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100734
Kristian Høgsberg4598f962014-01-01 16:32:09 -0800735 widget_schedule_resize(background->widget, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100736}
737
738static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500739unlock_dialog_redraw_handler(struct widget *widget, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200740{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500741 struct unlock_dialog *dialog = data;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200742 struct rectangle allocation;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200743 cairo_surface_t *surface;
Alexander Larssonc584fa62013-05-22 14:41:32 +0200744 cairo_t *cr;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200745 cairo_pattern_t *pat;
746 double cx, cy, r, f;
747
Alexander Larssonc584fa62013-05-22 14:41:32 +0200748 cr = widget_cairo_create(widget);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500749
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500750 widget_get_allocation(dialog->widget, &allocation);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200751 cairo_rectangle(cr, allocation.x, allocation.y,
752 allocation.width, allocation.height);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200753 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
754 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500755 cairo_fill(cr);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200756
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500757 cairo_translate(cr, allocation.x, allocation.y);
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500758 if (dialog->button_focused)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200759 f = 1.0;
760 else
761 f = 0.7;
762
763 cx = allocation.width / 2.0;
764 cy = allocation.height / 2.0;
765 r = (cx < cy ? cx : cy) * 0.4;
766 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
767 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
768 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
769 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
770 cairo_set_source(cr, pat);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500771 cairo_pattern_destroy(pat);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200772 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
773 cairo_fill(cr);
774
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500775 widget_set_allocation(dialog->button,
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500776 allocation.x + cx - r,
777 allocation.y + cy - r, 2 * r, 2 * r);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200778
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200779 cairo_destroy(cr);
780
Alexander Larssonc584fa62013-05-22 14:41:32 +0200781 surface = window_get_surface(dialog->window);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200782 cairo_surface_destroy(surface);
783}
784
785static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500786unlock_dialog_button_handler(struct widget *widget,
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200787 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100788 uint32_t button,
789 enum wl_pointer_button_state state, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200790{
791 struct unlock_dialog *dialog = data;
792 struct desktop *desktop = dialog->desktop;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200793
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500794 if (button == BTN_LEFT) {
Daniel Stone4dbadb12012-05-30 16:31:51 +0100795 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
796 !dialog->closing) {
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200797 display_defer(desktop->display, &desktop->unlock_task);
798 dialog->closing = 1;
799 }
800 }
801}
802
803static void
Brian J Lovinc4df4082013-08-26 15:58:22 -0700804unlock_dialog_touch_down_handler(struct widget *widget, struct input *input,
805 uint32_t serial, uint32_t time, int32_t id,
806 float x, float y, void *data)
807{
808 struct unlock_dialog *dialog = data;
809
810 dialog->button_focused = 1;
811 widget_schedule_redraw(widget);
812}
813
814static void
815unlock_dialog_touch_up_handler(struct widget *widget, struct input *input,
816 uint32_t serial, uint32_t time, int32_t id,
817 void *data)
818{
819 struct unlock_dialog *dialog = data;
820 struct desktop *desktop = dialog->desktop;
821
822 dialog->button_focused = 0;
823 widget_schedule_redraw(widget);
824 display_defer(desktop->display, &desktop->unlock_task);
825 dialog->closing = 1;
826}
827
828static void
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200829unlock_dialog_keyboard_focus_handler(struct window *window,
830 struct input *device, void *data)
831{
832 window_schedule_redraw(window);
833}
834
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500835static int
Kristian Høgsbergee143232012-01-09 08:42:24 -0500836unlock_dialog_widget_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400837 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400838 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500839{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500840 struct unlock_dialog *dialog = data;
841
842 dialog->button_focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500843 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500844
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300845 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500846}
847
848static void
849unlock_dialog_widget_leave_handler(struct widget *widget,
850 struct input *input, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200851{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500852 struct unlock_dialog *dialog = data;
853
854 dialog->button_focused = 0;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500855 widget_schedule_redraw(widget);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200856}
857
858static struct unlock_dialog *
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500859unlock_dialog_create(struct desktop *desktop)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200860{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500861 struct display *display = desktop->display;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200862 struct unlock_dialog *dialog;
863
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
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500887 desktop_shell_set_lock_surface(desktop->shell,
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400888 window_get_wl_surface(dialog->window));
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
908 desktop_shell_unlock(desktop->shell);
909 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,
915 struct 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,
928 struct desktop_shell *desktop_shell)
929{
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) {
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200933 desktop_shell_unlock(desktop->shell);
934 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,
945 struct desktop_shell *desktop_shell,
946 uint32_t cursor)
947{
948 struct desktop *desktop = data;
949
950 switch (cursor) {
Philipp Brüschweiler16d59d72012-08-24 15:43:55 +0200951 case DESKTOP_SHELL_CURSOR_NONE:
952 desktop->grab_cursor = CURSOR_BLANK;
953 break;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300954 case DESKTOP_SHELL_CURSOR_BUSY:
955 desktop->grab_cursor = CURSOR_WATCH;
956 break;
957 case DESKTOP_SHELL_CURSOR_MOVE:
958 desktop->grab_cursor = CURSOR_DRAGGING;
959 break;
960 case DESKTOP_SHELL_CURSOR_RESIZE_TOP:
961 desktop->grab_cursor = CURSOR_TOP;
962 break;
963 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM:
964 desktop->grab_cursor = CURSOR_BOTTOM;
965 break;
966 case DESKTOP_SHELL_CURSOR_RESIZE_LEFT:
967 desktop->grab_cursor = CURSOR_LEFT;
968 break;
969 case DESKTOP_SHELL_CURSOR_RESIZE_RIGHT:
970 desktop->grab_cursor = CURSOR_RIGHT;
971 break;
972 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_LEFT:
973 desktop->grab_cursor = CURSOR_TOP_LEFT;
974 break;
975 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_RIGHT:
976 desktop->grab_cursor = CURSOR_TOP_RIGHT;
977 break;
978 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_LEFT:
979 desktop->grab_cursor = CURSOR_BOTTOM_LEFT;
980 break;
981 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_RIGHT:
982 desktop->grab_cursor = CURSOR_BOTTOM_RIGHT;
983 break;
984 case DESKTOP_SHELL_CURSOR_ARROW:
985 default:
986 desktop->grab_cursor = CURSOR_LEFT_PTR;
987 }
988}
989
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400990static const struct 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);
1078 desktop_shell_set_grab_surface(desktop->shell, s);
1079
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);
1192 desktop_shell_set_panel(desktop->shell,
1193 output->output, surface);
1194 }
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);
1198 desktop_shell_set_background(desktop->shell,
1199 output->output, surface);
1200}
1201
1202static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001203create_output(struct desktop *desktop, uint32_t id)
1204{
1205 struct output *output;
1206
1207 output = calloc(1, sizeof *output);
1208 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
1231 if (!strcmp(interface, "desktop_shell")) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03001232 desktop->interface_version = (version < 2) ? version : 2;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001233 desktop->shell = display_bind(desktop->display,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03001234 id, &desktop_shell_interface,
1235 desktop->interface_version);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001236 desktop_shell_add_listener(desktop->shell, &listener, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001237 } else if (!strcmp(interface, "wl_output")) {
1238 create_output(desktop, id);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001239 }
1240}
1241
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001242static void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001243global_handler_remove(struct display *display, uint32_t id,
1244 const char *interface, uint32_t version, void *data)
1245{
1246 struct desktop *desktop = data;
1247 struct output *output;
1248
1249 if (!strcmp(interface, "wl_output")) {
1250 wl_list_for_each(output, &desktop->outputs, link) {
1251 if (output->server_output_id == id) {
1252 output_destroy(output);
1253 break;
1254 }
1255 }
1256 }
1257}
1258
1259static void
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001260panel_add_launchers(struct panel *panel, struct desktop *desktop)
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001261{
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001262 struct weston_config_section *s;
1263 char *icon, *path;
1264 const char *name;
1265 int count;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001266
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001267 count = 0;
1268 s = NULL;
1269 while (weston_config_next_section(desktop->config, &s, &name)) {
1270 if (strcmp(name, "launcher") != 0)
1271 continue;
1272
1273 weston_config_section_get_string(s, "icon", &icon, NULL);
1274 weston_config_section_get_string(s, "path", &path, NULL);
1275
1276 if (icon != NULL && path != NULL) {
1277 panel_add_launcher(panel, icon, path);
Rob Bradford09252d42013-07-26 16:29:45 +01001278 count++;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001279 } else {
1280 fprintf(stderr, "invalid launcher section\n");
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001281 }
1282
1283 free(icon);
1284 free(path);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001285 }
1286
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001287 if (count == 0) {
1288 /* add default launcher */
1289 panel_add_launcher(panel,
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001290 DATADIR "/weston/terminal.png",
Rodney Lorrimar99ff01b2012-02-29 17:31:03 +01001291 BINDIR "/weston-terminal");
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001292 }
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001293}
1294
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001295int main(int argc, char *argv[])
1296{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001297 struct desktop desktop = { 0 };
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001298 struct output *output;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001299 struct weston_config_section *s;
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001300 const char *config_file;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001301
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001302 desktop.unlock_task.run = unlock_dialog_finish;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001303 wl_list_init(&desktop.outputs);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001304
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02001305 config_file = weston_config_get_name_from_env();
1306 desktop.config = weston_config_parse(config_file);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001307 s = weston_config_get_section(desktop.config, "shell", NULL, NULL);
1308 weston_config_section_get_bool(s, "locking", &desktop.locking, 1);
1309
Kristian Høgsberg4172f662013-02-20 15:27:49 -05001310 desktop.display = display_create(&argc, argv);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001311 if (desktop.display == NULL) {
1312 fprintf(stderr, "failed to create display: %m\n");
1313 return -1;
1314 }
1315
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001316 display_set_user_data(desktop.display, &desktop);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001317 display_set_global_handler(desktop.display, global_handler);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001318 display_set_global_handler_remove(desktop.display, global_handler_remove);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001319
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001320 /* Create panel and background for outputs processed before the shell
1321 * global interface was processed */
1322 wl_list_for_each(output, &desktop.outputs, link)
1323 if (!output->panel)
1324 output_init(output, &desktop);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001325
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001326 grab_surface_create(&desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001327
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001328 signal(SIGCHLD, sigchild_handler);
1329
1330 display_run(desktop.display);
1331
U. Artie Eoff44874d92012-10-02 21:12:35 -07001332 /* Cleanup */
1333 grab_surface_destroy(&desktop);
1334 desktop_destroy_outputs(&desktop);
1335 if (desktop.unlock_dialog)
1336 unlock_dialog_destroy(desktop.unlock_dialog);
1337 desktop_shell_destroy(desktop.shell);
1338 display_destroy(desktop.display);
1339
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001340 return 0;
1341}