blob: f96dcf1c668a883ee2d18160a32f5c0ee7219a1a [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 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
24#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <fcntl.h>
29#include <unistd.h>
30#include <math.h>
31#include <cairo.h>
32#include <sys/wait.h>
Martin Minarik1e51a872012-06-08 00:39:11 +020033#include <sys/timerfd.h>
34#include <sys/epoll.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040035#include <linux/input.h>
Tiago Vignatti61500722012-05-23 22:06:28 +030036#include <libgen.h>
Kristian Høgsbergd1936b92012-07-23 22:59:33 -040037#include <ctype.h>
Martin Minarik1e51a872012-06-08 00:39:11 +020038#include <time.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040039
Pekka Paalanen50719bc2011-11-22 14:18:50 +020040#include <wayland-client.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040041#include "window.h"
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040042#include "../shared/cairo-util.h"
Kristian Høgsberg9b935c82011-12-08 12:44:27 -050043#include "../shared/config-parser.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040044
Pekka Paalanen50719bc2011-11-22 14:18:50 +020045#include "desktop-shell-client-protocol.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040046
Pekka Paalanenb6df4f72012-08-03 14:39:15 +030047extern char **environ; /* defined by libc */
48
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040049struct desktop {
50 struct display *display;
51 struct desktop_shell *shell;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030052 uint32_t interface_version;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +020053 struct unlock_dialog *unlock_dialog;
54 struct task unlock_task;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010055 struct wl_list outputs;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040056
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030057 struct window *grab_window;
58 struct widget *grab_widget;
59
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +030060 struct weston_config *config;
61 int locking;
62
Scott Moreauec116022012-07-22 18:23:52 -060063 enum cursor_type grab_cursor;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030064
65 int painted;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010066};
67
68struct surface {
69 void (*configure)(void *data,
70 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040071 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010072 int32_t width, int32_t height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040073};
74
75struct panel {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010076 struct surface base;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040077 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050078 struct widget *widget;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050079 struct wl_list launcher_list;
Martin Minarik1e51a872012-06-08 00:39:11 +020080 struct panel_clock *clock;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030081 int painted;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +030082 uint32_t color;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040083};
84
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010085struct background {
86 struct surface base;
87 struct window *window;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050088 struct widget *widget;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030089 int painted;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +030090
91 char *image;
92 int type;
93 uint32_t color;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010094};
95
96struct output {
97 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +080098 uint32_t server_output_id;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010099 struct wl_list link;
100
101 struct panel *panel;
102 struct background *background;
103};
104
Kristian Høgsberg53880802012-01-09 11:16:50 -0500105struct panel_launcher {
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500106 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400107 struct panel *panel;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400108 cairo_surface_t *icon;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500109 int focused, pressed;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400110 char *path;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500111 struct wl_list link;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400112 struct wl_array envp;
113 struct wl_array argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400114};
115
Martin Minarik1e51a872012-06-08 00:39:11 +0200116struct panel_clock {
117 struct widget *widget;
118 struct panel *panel;
Martin Minarik1e51a872012-06-08 00:39:11 +0200119 struct task clock_task;
120 int clock_fd;
121};
122
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200123struct unlock_dialog {
124 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500125 struct widget *widget;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500126 struct widget *button;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500127 int button_focused;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200128 int closing;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200129 struct desktop *desktop;
130};
131
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300132static void
133panel_add_launchers(struct panel *panel, struct desktop *desktop);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500134
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400135static void
136sigchild_handler(int s)
137{
138 int status;
139 pid_t pid;
140
141 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
142 fprintf(stderr, "child %d exited\n", pid);
143}
144
145static void
Kristian Høgsberg67b82152013-10-23 16:52:05 -0700146menu_func(struct window *window, struct input *input, int index, void *data)
Pekka Paalanen9e30a822012-01-19 16:40:28 +0200147{
148 printf("Selected index %d from a panel menu.\n", index);
149}
150
151static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500152show_menu(struct panel *panel, struct input *input, uint32_t time)
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400153{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500154 int32_t x, y;
155 static const char *entries[] = {
156 "Roy", "Pris", "Leon", "Zhora"
157 };
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400158
159 input_get_position(input, &x, &y);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +0200160 window_show_menu(window_get_display(panel->window),
161 input, time, panel->window,
Pekka Paalanen9e30a822012-01-19 16:40:28 +0200162 x - 10, y - 10, menu_func, entries, 4);
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400163}
164
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300165static int
166is_desktop_painted(struct desktop *desktop)
167{
168 struct output *output;
169
170 wl_list_for_each(output, &desktop->outputs, link) {
171 if (output->panel && !output->panel->painted)
172 return 0;
173 if (output->background && !output->background->painted)
174 return 0;
175 }
176
177 return 1;
178}
179
180static void
181check_desktop_ready(struct window *window)
182{
183 struct display *display;
184 struct desktop *desktop;
185
186 display = window_get_display(window);
187 desktop = display_get_user_data(display);
188
189 if (!desktop->painted && is_desktop_painted(desktop)) {
190 desktop->painted = 1;
191
192 if (desktop->interface_version >= 2)
193 desktop_shell_desktop_ready(desktop->shell);
194 }
195}
196
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400197static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500198panel_launcher_activate(struct panel_launcher *widget)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400199{
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400200 char **argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400201 pid_t pid;
202
203 pid = fork();
204 if (pid < 0) {
205 fprintf(stderr, "fork failed: %m\n");
206 return;
207 }
208
209 if (pid)
210 return;
Benjamin Franzked7759712011-11-22 12:38:48 +0100211
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400212 argv = widget->argv.data;
213 if (execve(argv[0], argv, widget->envp.data) < 0) {
214 fprintf(stderr, "execl '%s' failed: %m\n", argv[0]);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400215 exit(1);
216 }
217}
218
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400219static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500220panel_launcher_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400221{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500222 struct panel_launcher *launcher = data;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500223 struct rectangle allocation;
224 cairo_t *cr;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400225
Alexander Larssonc584fa62013-05-22 14:41:32 +0200226 cr = widget_cairo_create(launcher->panel->widget);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500227
228 widget_get_allocation(widget, &allocation);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500229 if (launcher->pressed) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500230 allocation.x++;
231 allocation.y++;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400232 }
233
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500234 cairo_set_source_surface(cr, launcher->icon,
235 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400236 cairo_paint(cr);
237
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500238 if (launcher->focused) {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400239 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500240 cairo_mask_surface(cr, launcher->icon,
241 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400242 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400243
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500244 cairo_destroy(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400245}
246
Tiago Vignatti61500722012-05-23 22:06:28 +0300247static int
248panel_launcher_motion_handler(struct widget *widget, struct input *input,
249 uint32_t time, float x, float y, void *data)
250{
251 struct panel_launcher *launcher = data;
252
253 widget_set_tooltip(widget, basename((char *)launcher->path), x, y);
254
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300255 return CURSOR_LEFT_PTR;
Tiago Vignatti61500722012-05-23 22:06:28 +0300256}
257
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400258static void
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500259set_hex_color(cairo_t *cr, uint32_t color)
260{
261 cairo_set_source_rgba(cr,
262 ((color >> 16) & 0xff) / 255.0,
263 ((color >> 8) & 0xff) / 255.0,
264 ((color >> 0) & 0xff) / 255.0,
265 ((color >> 24) & 0xff) / 255.0);
266}
267
268static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500269panel_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400270{
271 cairo_surface_t *surface;
272 cairo_t *cr;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500273 struct panel *panel = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400274
Alexander Larssonc584fa62013-05-22 14:41:32 +0200275 cr = widget_cairo_create(panel->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400276 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300277 set_hex_color(cr, panel->color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400278 cairo_paint(cr);
279
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400280 cairo_destroy(cr);
Alexander Larssonc584fa62013-05-22 14:41:32 +0200281 surface = window_get_surface(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400282 cairo_surface_destroy(surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300283 panel->painted = 1;
284 check_desktop_ready(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400285}
286
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500287static int
Kristian Høgsberg53880802012-01-09 11:16:50 -0500288panel_launcher_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400289 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500290{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500291 struct panel_launcher *launcher = data;
292
293 launcher->focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500294 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500295
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300296 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500297}
298
299static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500300panel_launcher_leave_handler(struct widget *widget,
301 struct input *input, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400302{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500303 struct panel_launcher *launcher = data;
304
305 launcher->focused = 0;
Tiago Vignatti61500722012-05-23 22:06:28 +0300306 widget_destroy_tooltip(widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500307 widget_schedule_redraw(widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400308}
309
310static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500311panel_launcher_button_handler(struct widget *widget,
312 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100313 uint32_t button,
314 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400315{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500316 struct panel_launcher *launcher;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400317
Kristian Høgsberg53880802012-01-09 11:16:50 -0500318 launcher = widget_get_user_data(widget);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500319 widget_schedule_redraw(widget);
Daniel Stone4dbadb12012-05-30 16:31:51 +0100320 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg53880802012-01-09 11:16:50 -0500321 panel_launcher_activate(launcher);
Rusty Lynch4384a242013-08-08 21:28:22 -0700322
323}
324
325static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700326panel_launcher_touch_down_handler(struct widget *widget, struct input *input,
327 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch4384a242013-08-08 21:28:22 -0700328 float x, float y, void *data)
329{
330 struct panel_launcher *launcher;
331
332 launcher = widget_get_user_data(widget);
333 launcher->focused = 1;
334 widget_schedule_redraw(widget);
335}
336
337static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700338panel_launcher_touch_up_handler(struct widget *widget, struct input *input,
339 uint32_t serial, uint32_t time, int32_t id,
340 void *data)
Rusty Lynch4384a242013-08-08 21:28:22 -0700341{
342 struct panel_launcher *launcher;
343
344 launcher = widget_get_user_data(widget);
345 launcher->focused = 0;
346 widget_schedule_redraw(widget);
347 panel_launcher_activate(launcher);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500348}
349
Martin Minarik1e51a872012-06-08 00:39:11 +0200350static void
351clock_func(struct task *task, uint32_t events)
352{
353 struct panel_clock *clock =
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400354 container_of(task, struct panel_clock, clock_task);
Kristian Høgsberg70226bb2012-06-08 16:54:52 -0400355 uint64_t exp;
Martin Minarik1e51a872012-06-08 00:39:11 +0200356
Martin Olsson8df662a2012-07-08 03:03:47 +0200357 if (read(clock->clock_fd, &exp, sizeof exp) != sizeof exp)
358 abort();
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400359 widget_schedule_redraw(clock->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200360}
361
362static void
363panel_clock_redraw_handler(struct widget *widget, void *data)
364{
Martin Minarik1e51a872012-06-08 00:39:11 +0200365 struct panel_clock *clock = data;
366 cairo_t *cr;
367 struct rectangle allocation;
368 cairo_text_extents_t extents;
369 cairo_font_extents_t font_extents;
Martin Minarik1e51a872012-06-08 00:39:11 +0200370 time_t rawtime;
371 struct tm * timeinfo;
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400372 char string[128];
Martin Minarik1e51a872012-06-08 00:39:11 +0200373
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400374 time(&rawtime);
375 timeinfo = localtime(&rawtime);
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400376 strftime(string, sizeof string, "%a %b %d, %I:%M %p", timeinfo);
Martin Minarik1e51a872012-06-08 00:39:11 +0200377
378 widget_get_allocation(widget, &allocation);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400379 if (allocation.width == 0)
380 return;
Martin Minarik1e51a872012-06-08 00:39:11 +0200381
Alexander Larssonc584fa62013-05-22 14:41:32 +0200382 cr = widget_cairo_create(clock->panel->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200383 cairo_select_font_face(cr, "sans",
384 CAIRO_FONT_SLANT_NORMAL,
385 CAIRO_FONT_WEIGHT_NORMAL);
386 cairo_set_font_size(cr, 14);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400387 cairo_text_extents(cr, string, &extents);
Martin Minarik1e51a872012-06-08 00:39:11 +0200388 cairo_font_extents (cr, &font_extents);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400389 cairo_move_to(cr, allocation.x + 5,
390 allocation.y + 3 * (allocation.height >> 2) + 1);
391 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400392 cairo_show_text(cr, string);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400393 cairo_move_to(cr, allocation.x + 4,
394 allocation.y + 3 * (allocation.height >> 2));
395 cairo_set_source_rgb(cr, 1, 1, 1);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400396 cairo_show_text(cr, string);
Martin Minarik1e51a872012-06-08 00:39:11 +0200397 cairo_destroy(cr);
398}
399
400static int
401clock_timer_reset(struct panel_clock *clock)
402{
403 struct itimerspec its;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400404
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400405 its.it_interval.tv_sec = 60;
Martin Minarik1e51a872012-06-08 00:39:11 +0200406 its.it_interval.tv_nsec = 0;
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400407 its.it_value.tv_sec = 60;
Martin Minarik1e51a872012-06-08 00:39:11 +0200408 its.it_value.tv_nsec = 0;
409 if (timerfd_settime(clock->clock_fd, 0, &its, NULL) < 0) {
410 fprintf(stderr, "could not set timerfd\n: %m");
411 return -1;
412 }
413
414 return 0;
415}
416
417static void
U. Artie Eoff44874d92012-10-02 21:12:35 -0700418panel_destroy_clock(struct panel_clock *clock)
419{
420 widget_destroy(clock->widget);
421
422 close(clock->clock_fd);
423
424 free(clock);
425}
426
427static void
Martin Minarik1e51a872012-06-08 00:39:11 +0200428panel_add_clock(struct panel *panel)
429{
430 struct panel_clock *clock;
431 int timerfd;
432
433 timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
434 if (timerfd < 0) {
435 fprintf(stderr, "could not create timerfd\n: %m");
436 return;
437 }
438
Peter Huttererf3d62272013-08-08 11:57:05 +1000439 clock = xzalloc(sizeof *clock);
Martin Minarik1e51a872012-06-08 00:39:11 +0200440 clock->panel = panel;
441 panel->clock = clock;
442 clock->clock_fd = timerfd;
443
444 clock->clock_task.run = clock_func;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400445 display_watch_fd(window_get_display(panel->window), clock->clock_fd,
446 EPOLLIN, &clock->clock_task);
Martin Minarik1e51a872012-06-08 00:39:11 +0200447 clock_timer_reset(clock);
448
449 clock->widget = widget_add_widget(panel->widget, clock);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400450 widget_set_redraw_handler(clock->widget, panel_clock_redraw_handler);
Martin Minarik1e51a872012-06-08 00:39:11 +0200451}
452
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500453static void
454panel_button_handler(struct widget *widget,
455 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100456 uint32_t button,
457 enum wl_pointer_button_state state, void *data)
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500458{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500459 struct panel *panel = data;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500460
Daniel Stone4dbadb12012-05-30 16:31:51 +0100461 if (button == BTN_RIGHT && state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500462 show_menu(panel, input, time);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400463}
464
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100465static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500466panel_resize_handler(struct widget *widget,
467 int32_t width, int32_t height, void *data)
468{
469 struct panel_launcher *launcher;
470 struct panel *panel = data;
471 int x, y, w, h;
472
473 x = 10;
474 y = 16;
475 wl_list_for_each(launcher, &panel->launcher_list, link) {
476 w = cairo_image_surface_get_width(launcher->icon);
477 h = cairo_image_surface_get_height(launcher->icon);
478 widget_set_allocation(launcher->widget,
479 x, y - h / 2, w + 1, h + 1);
480 x += w + 10;
481 }
Martin Minarik1e51a872012-06-08 00:39:11 +0200482 h=20;
483 w=170;
Pekka Paalanen01b17252012-06-12 17:42:26 +0300484
485 if (panel->clock)
486 widget_set_allocation(panel->clock->widget,
487 width - w - 8, y - h / 2, w + 1, h + 1);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500488}
489
490static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100491panel_configure(void *data,
492 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400493 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100494 int32_t width, int32_t height)
495{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500496 struct surface *surface = window_get_user_data(window);
497 struct panel *panel = container_of(surface, struct panel, base);
498
499 window_schedule_resize(panel->window, width, 32);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100500}
501
U. Artie Eoff44874d92012-10-02 21:12:35 -0700502static void
503panel_destroy_launcher(struct panel_launcher *launcher)
504{
505 wl_array_release(&launcher->argv);
506 wl_array_release(&launcher->envp);
507
508 free(launcher->path);
509
510 cairo_surface_destroy(launcher->icon);
511
512 widget_destroy(launcher->widget);
513 wl_list_remove(&launcher->link);
514
515 free(launcher);
516}
517
518static void
519panel_destroy(struct panel *panel)
520{
521 struct panel_launcher *tmp;
522 struct panel_launcher *launcher;
523
524 panel_destroy_clock(panel->clock);
525
526 wl_list_for_each_safe(launcher, tmp, &panel->launcher_list, link)
527 panel_destroy_launcher(launcher);
528
529 widget_destroy(panel->widget);
530 window_destroy(panel->window);
531
532 free(panel);
533}
534
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400535static struct panel *
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300536panel_create(struct desktop *desktop)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400537{
538 struct panel *panel;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300539 struct weston_config_section *s;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400540
Peter Huttererf3d62272013-08-08 11:57:05 +1000541 panel = xzalloc(sizeof *panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400542
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100543 panel->base.configure = panel_configure;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300544 panel->window = window_create_custom(desktop->display);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500545 panel->widget = window_add_widget(panel->window, panel);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500546 wl_list_init(&panel->launcher_list);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400547
548 window_set_title(panel->window, "panel");
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400549 window_set_user_data(panel->window, panel);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500550
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500551 widget_set_redraw_handler(panel->widget, panel_redraw_handler);
552 widget_set_resize_handler(panel->widget, panel_resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500553 widget_set_button_handler(panel->widget, panel_button_handler);
Rafal Mielniczukb9e513c2012-06-09 20:33:29 +0200554
555 panel_add_clock(panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400556
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300557 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
558 weston_config_section_get_uint(s, "panel-color",
559 &panel->color, 0xaa000000);
560
561 panel_add_launchers(panel, desktop);
562
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400563 return panel;
564}
565
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200566static cairo_surface_t *
567load_icon_or_fallback(const char *icon)
568{
569 cairo_surface_t *surface = cairo_image_surface_create_from_png(icon);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200570 cairo_status_t status;
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200571 cairo_t *cr;
572
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200573 status = cairo_surface_status(surface);
574 if (status == CAIRO_STATUS_SUCCESS)
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200575 return surface;
576
577 cairo_surface_destroy(surface);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200578 fprintf(stderr, "ERROR loading icon from file '%s', error: '%s'\n",
579 icon, cairo_status_to_string(status));
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200580
581 /* draw fallback icon */
582 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
583 20, 20);
584 cr = cairo_create(surface);
585
586 cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1);
587 cairo_paint(cr);
588
589 cairo_set_source_rgba(cr, 0, 0, 0, 1);
590 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
591 cairo_rectangle(cr, 0, 0, 20, 20);
592 cairo_move_to(cr, 4, 4);
593 cairo_line_to(cr, 16, 16);
594 cairo_move_to(cr, 4, 16);
595 cairo_line_to(cr, 16, 4);
596 cairo_stroke(cr);
597
598 cairo_destroy(cr);
599
600 return surface;
601}
602
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400603static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500604panel_add_launcher(struct panel *panel, const char *icon, const char *path)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400605{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500606 struct panel_launcher *launcher;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400607 char *start, *p, *eq, **ps;
608 int i, j, k;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400609
Peter Huttererf3d62272013-08-08 11:57:05 +1000610 launcher = xzalloc(sizeof *launcher);
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200611 launcher->icon = load_icon_or_fallback(icon);
Kristian Høgsberg53880802012-01-09 11:16:50 -0500612 launcher->path = strdup(path);
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400613
614 wl_array_init(&launcher->envp);
615 wl_array_init(&launcher->argv);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300616 for (i = 0; environ[i]; i++) {
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400617 ps = wl_array_add(&launcher->envp, sizeof *ps);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300618 *ps = environ[i];
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400619 }
620 j = 0;
621
622 start = launcher->path;
623 while (*start) {
624 for (p = start, eq = NULL; *p && !isspace(*p); p++)
625 if (*p == '=')
626 eq = p;
627
628 if (eq && j == 0) {
629 ps = launcher->envp.data;
630 for (k = 0; k < i; k++)
631 if (strncmp(ps[k], start, eq - start) == 0) {
632 ps[k] = start;
633 break;
634 }
635 if (k == i) {
636 ps = wl_array_add(&launcher->envp, sizeof *ps);
637 *ps = start;
638 i++;
639 }
640 } else {
641 ps = wl_array_add(&launcher->argv, sizeof *ps);
642 *ps = start;
643 j++;
644 }
645
646 while (*p && isspace(*p))
647 *p++ = '\0';
648
649 start = p;
650 }
651
652 ps = wl_array_add(&launcher->envp, sizeof *ps);
653 *ps = NULL;
654 ps = wl_array_add(&launcher->argv, sizeof *ps);
655 *ps = NULL;
656
Kristian Høgsberg53880802012-01-09 11:16:50 -0500657 launcher->panel = panel;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500658 wl_list_insert(panel->launcher_list.prev, &launcher->link);
659
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500660 launcher->widget = widget_add_widget(panel->widget, launcher);
Kristian Høgsberg53880802012-01-09 11:16:50 -0500661 widget_set_enter_handler(launcher->widget,
662 panel_launcher_enter_handler);
663 widget_set_leave_handler(launcher->widget,
664 panel_launcher_leave_handler);
665 widget_set_button_handler(launcher->widget,
666 panel_launcher_button_handler);
Rusty Lynch4384a242013-08-08 21:28:22 -0700667 widget_set_touch_down_handler(launcher->widget,
668 panel_launcher_touch_down_handler);
669 widget_set_touch_up_handler(launcher->widget,
670 panel_launcher_touch_up_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500671 widget_set_redraw_handler(launcher->widget,
672 panel_launcher_redraw_handler);
Tiago Vignatti61500722012-05-23 22:06:28 +0300673 widget_set_motion_handler(launcher->widget,
674 panel_launcher_motion_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400675}
676
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500677enum {
678 BACKGROUND_SCALE,
Pekka Paalanena402b052013-05-22 18:03:10 +0300679 BACKGROUND_SCALE_CROP,
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500680 BACKGROUND_TILE
681};
682
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400683static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500684background_draw(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400685{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500686 struct background *background = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400687 cairo_surface_t *surface, *image;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400688 cairo_pattern_t *pattern;
689 cairo_matrix_t matrix;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400690 cairo_t *cr;
Pekka Paalanena402b052013-05-22 18:03:10 +0300691 double im_w, im_h;
692 double sx, sy, s;
693 double tx, ty;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500694 struct rectangle allocation;
Pekka Paalanen9564c752012-10-24 09:43:08 +0300695 struct display *display;
696 struct wl_region *opaque;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400697
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500698 surface = window_get_surface(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400699
Alexander Larssonc584fa62013-05-22 14:41:32 +0200700 cr = widget_cairo_create(background->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400701 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
702 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
703 cairo_paint(cr);
704
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500705 widget_get_allocation(widget, &allocation);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500706 image = NULL;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300707 if (background->image)
708 image = load_cairo_surface(background->image);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500709
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300710 if (image && background->type != -1) {
Pekka Paalanena402b052013-05-22 18:03:10 +0300711 im_w = cairo_image_surface_get_width(image);
712 im_h = cairo_image_surface_get_height(image);
713 sx = im_w / allocation.width;
714 sy = im_h / allocation.height;
715
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400716 pattern = cairo_pattern_create_for_surface(image);
Pekka Paalanena402b052013-05-22 18:03:10 +0300717
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300718 switch (background->type) {
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500719 case BACKGROUND_SCALE:
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500720 cairo_matrix_init_scale(&matrix, sx, sy);
721 cairo_pattern_set_matrix(pattern, &matrix);
722 break;
Pekka Paalanena402b052013-05-22 18:03:10 +0300723 case BACKGROUND_SCALE_CROP:
724 s = (sx < sy) ? sx : sy;
725 /* align center */
726 tx = (im_w - s * allocation.width) * 0.5;
727 ty = (im_h - s * allocation.height) * 0.5;
728 cairo_matrix_init_translate(&matrix, tx, ty);
729 cairo_matrix_scale(&matrix, s, s);
730 cairo_pattern_set_matrix(pattern, &matrix);
731 break;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500732 case BACKGROUND_TILE:
733 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
734 break;
735 }
Pekka Paalanena402b052013-05-22 18:03:10 +0300736
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400737 cairo_set_source(cr, pattern);
738 cairo_pattern_destroy (pattern);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400739 cairo_surface_destroy(image);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500740 } else {
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300741 set_hex_color(cr, background->color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400742 }
743
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500744 cairo_paint(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400745 cairo_destroy(cr);
746 cairo_surface_destroy(surface);
Pekka Paalanen9564c752012-10-24 09:43:08 +0300747
748 display = window_get_display(background->window);
749 opaque = wl_compositor_create_region(display_get_compositor(display));
750 wl_region_add(opaque, allocation.x, allocation.y,
751 allocation.width, allocation.height);
752 wl_surface_set_opaque_region(window_get_wl_surface(background->window), opaque);
753 wl_region_destroy(opaque);
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300754
755 background->painted = 1;
756 check_desktop_ready(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400757}
758
759static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100760background_configure(void *data,
761 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400762 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100763 int32_t width, int32_t height)
764{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500765 struct background *background =
766 (struct background *) window_get_user_data(window);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100767
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -0800768 widget_schedule_resize(background->widget, width - 256, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100769}
770
771static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500772unlock_dialog_redraw_handler(struct widget *widget, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200773{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500774 struct unlock_dialog *dialog = data;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200775 struct rectangle allocation;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200776 cairo_surface_t *surface;
Alexander Larssonc584fa62013-05-22 14:41:32 +0200777 cairo_t *cr;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200778 cairo_pattern_t *pat;
779 double cx, cy, r, f;
780
Alexander Larssonc584fa62013-05-22 14:41:32 +0200781 cr = widget_cairo_create(widget);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500782
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500783 widget_get_allocation(dialog->widget, &allocation);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200784 cairo_rectangle(cr, allocation.x, allocation.y,
785 allocation.width, allocation.height);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200786 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
787 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500788 cairo_fill(cr);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200789
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500790 cairo_translate(cr, allocation.x, allocation.y);
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500791 if (dialog->button_focused)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200792 f = 1.0;
793 else
794 f = 0.7;
795
796 cx = allocation.width / 2.0;
797 cy = allocation.height / 2.0;
798 r = (cx < cy ? cx : cy) * 0.4;
799 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
800 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
801 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
802 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
803 cairo_set_source(cr, pat);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500804 cairo_pattern_destroy(pat);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200805 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
806 cairo_fill(cr);
807
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500808 widget_set_allocation(dialog->button,
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500809 allocation.x + cx - r,
810 allocation.y + cy - r, 2 * r, 2 * r);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200811
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200812 cairo_destroy(cr);
813
Alexander Larssonc584fa62013-05-22 14:41:32 +0200814 surface = window_get_surface(dialog->window);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200815 cairo_surface_destroy(surface);
816}
817
818static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500819unlock_dialog_button_handler(struct widget *widget,
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200820 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100821 uint32_t button,
822 enum wl_pointer_button_state state, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200823{
824 struct unlock_dialog *dialog = data;
825 struct desktop *desktop = dialog->desktop;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200826
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500827 if (button == BTN_LEFT) {
Daniel Stone4dbadb12012-05-30 16:31:51 +0100828 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
829 !dialog->closing) {
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200830 display_defer(desktop->display, &desktop->unlock_task);
831 dialog->closing = 1;
832 }
833 }
834}
835
836static void
Brian J Lovinc4df4082013-08-26 15:58:22 -0700837unlock_dialog_touch_down_handler(struct widget *widget, struct input *input,
838 uint32_t serial, uint32_t time, int32_t id,
839 float x, float y, void *data)
840{
841 struct unlock_dialog *dialog = data;
842
843 dialog->button_focused = 1;
844 widget_schedule_redraw(widget);
845}
846
847static void
848unlock_dialog_touch_up_handler(struct widget *widget, struct input *input,
849 uint32_t serial, uint32_t time, int32_t id,
850 void *data)
851{
852 struct unlock_dialog *dialog = data;
853 struct desktop *desktop = dialog->desktop;
854
855 dialog->button_focused = 0;
856 widget_schedule_redraw(widget);
857 display_defer(desktop->display, &desktop->unlock_task);
858 dialog->closing = 1;
859}
860
861static void
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200862unlock_dialog_keyboard_focus_handler(struct window *window,
863 struct input *device, void *data)
864{
865 window_schedule_redraw(window);
866}
867
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500868static int
Kristian Høgsbergee143232012-01-09 08:42:24 -0500869unlock_dialog_widget_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400870 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400871 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500872{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500873 struct unlock_dialog *dialog = data;
874
875 dialog->button_focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500876 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500877
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300878 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500879}
880
881static void
882unlock_dialog_widget_leave_handler(struct widget *widget,
883 struct input *input, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200884{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500885 struct unlock_dialog *dialog = data;
886
887 dialog->button_focused = 0;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500888 widget_schedule_redraw(widget);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200889}
890
891static struct unlock_dialog *
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500892unlock_dialog_create(struct desktop *desktop)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200893{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500894 struct display *display = desktop->display;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200895 struct unlock_dialog *dialog;
896
Peter Huttererf3d62272013-08-08 11:57:05 +1000897 dialog = xzalloc(sizeof *dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200898
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400899 dialog->window = window_create_custom(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500900 dialog->widget = window_frame_create(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200901 window_set_title(dialog->window, "Unlock your desktop");
902
903 window_set_user_data(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200904 window_set_keyboard_focus_handler(dialog->window,
905 unlock_dialog_keyboard_focus_handler);
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500906 dialog->button = widget_add_widget(dialog->widget, dialog);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500907 widget_set_redraw_handler(dialog->widget,
908 unlock_dialog_redraw_handler);
Kristian Høgsbergee143232012-01-09 08:42:24 -0500909 widget_set_enter_handler(dialog->button,
910 unlock_dialog_widget_enter_handler);
911 widget_set_leave_handler(dialog->button,
912 unlock_dialog_widget_leave_handler);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500913 widget_set_button_handler(dialog->button,
914 unlock_dialog_button_handler);
Brian J Lovinc4df4082013-08-26 15:58:22 -0700915 widget_set_touch_down_handler(dialog->button,
916 unlock_dialog_touch_down_handler);
917 widget_set_touch_up_handler(dialog->button,
918 unlock_dialog_touch_up_handler);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200919
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500920 desktop_shell_set_lock_surface(desktop->shell,
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400921 window_get_wl_surface(dialog->window));
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500922
Pekka Paalanen40e49ac2012-01-18 16:51:30 +0200923 window_schedule_resize(dialog->window, 260, 230);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200924
925 return dialog;
926}
927
928static void
929unlock_dialog_destroy(struct unlock_dialog *dialog)
930{
931 window_destroy(dialog->window);
932 free(dialog);
933}
934
935static void
936unlock_dialog_finish(struct task *task, uint32_t events)
937{
938 struct desktop *desktop =
Benjamin Franzked7759712011-11-22 12:38:48 +0100939 container_of(task, struct desktop, unlock_task);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200940
941 desktop_shell_unlock(desktop->shell);
942 unlock_dialog_destroy(desktop->unlock_dialog);
943 desktop->unlock_dialog = NULL;
944}
945
946static void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400947desktop_shell_configure(void *data,
948 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400949 uint32_t edges,
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400950 struct wl_surface *surface,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400951 int32_t width, int32_t height)
952{
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400953 struct window *window = wl_surface_get_user_data(surface);
Pekka Paalanen068ae942011-11-28 14:11:15 +0200954 struct surface *s = window_get_user_data(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400955
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400956 s->configure(data, desktop_shell, edges, window, width, height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400957}
958
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200959static void
960desktop_shell_prepare_lock_surface(void *data,
961 struct desktop_shell *desktop_shell)
962{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200963 struct desktop *desktop = data;
964
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300965 if (!desktop->locking) {
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200966 desktop_shell_unlock(desktop->shell);
967 return;
968 }
969
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200970 if (!desktop->unlock_dialog) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500971 desktop->unlock_dialog = unlock_dialog_create(desktop);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200972 desktop->unlock_dialog->desktop = desktop;
973 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200974}
975
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300976static void
977desktop_shell_grab_cursor(void *data,
978 struct desktop_shell *desktop_shell,
979 uint32_t cursor)
980{
981 struct desktop *desktop = data;
982
983 switch (cursor) {
Philipp Brüschweiler16d59d72012-08-24 15:43:55 +0200984 case DESKTOP_SHELL_CURSOR_NONE:
985 desktop->grab_cursor = CURSOR_BLANK;
986 break;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300987 case DESKTOP_SHELL_CURSOR_BUSY:
988 desktop->grab_cursor = CURSOR_WATCH;
989 break;
990 case DESKTOP_SHELL_CURSOR_MOVE:
991 desktop->grab_cursor = CURSOR_DRAGGING;
992 break;
993 case DESKTOP_SHELL_CURSOR_RESIZE_TOP:
994 desktop->grab_cursor = CURSOR_TOP;
995 break;
996 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM:
997 desktop->grab_cursor = CURSOR_BOTTOM;
998 break;
999 case DESKTOP_SHELL_CURSOR_RESIZE_LEFT:
1000 desktop->grab_cursor = CURSOR_LEFT;
1001 break;
1002 case DESKTOP_SHELL_CURSOR_RESIZE_RIGHT:
1003 desktop->grab_cursor = CURSOR_RIGHT;
1004 break;
1005 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_LEFT:
1006 desktop->grab_cursor = CURSOR_TOP_LEFT;
1007 break;
1008 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_RIGHT:
1009 desktop->grab_cursor = CURSOR_TOP_RIGHT;
1010 break;
1011 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_LEFT:
1012 desktop->grab_cursor = CURSOR_BOTTOM_LEFT;
1013 break;
1014 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_RIGHT:
1015 desktop->grab_cursor = CURSOR_BOTTOM_RIGHT;
1016 break;
1017 case DESKTOP_SHELL_CURSOR_ARROW:
1018 default:
1019 desktop->grab_cursor = CURSOR_LEFT_PTR;
1020 }
1021}
1022
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001023static const struct desktop_shell_listener listener = {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001024 desktop_shell_configure,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001025 desktop_shell_prepare_lock_surface,
1026 desktop_shell_grab_cursor
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001027};
1028
U. Artie Eoff44874d92012-10-02 21:12:35 -07001029static void
1030background_destroy(struct background *background)
1031{
1032 widget_destroy(background->widget);
1033 window_destroy(background->window);
1034
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001035 free(background->image);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001036 free(background);
1037}
1038
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001039static struct background *
1040background_create(struct desktop *desktop)
1041{
1042 struct background *background;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001043 struct weston_config_section *s;
1044 char *type;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001045
Peter Huttererf3d62272013-08-08 11:57:05 +10001046 background = xzalloc(sizeof *background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001047 background->base.configure = background_configure;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001048 background->window = window_create_custom(desktop->display);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001049 background->widget = window_add_widget(background->window, background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001050 window_set_user_data(background->window, background);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001051 widget_set_redraw_handler(background->widget, background_draw);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001052 window_set_preferred_format(background->window,
1053 WINDOW_PREFERRED_FORMAT_RGB565);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001054
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001055 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
1056 weston_config_section_get_string(s, "background-image",
1057 &background->image,
1058 DATADIR "/weston/pattern.png");
1059 weston_config_section_get_uint(s, "background-color",
1060 &background->color, 0xff002244);
1061
1062 weston_config_section_get_string(s, "background-type",
1063 &type, "tile");
1064 if (strcmp(type, "scale") == 0) {
1065 background->type = BACKGROUND_SCALE;
1066 } else if (strcmp(type, "scale-crop") == 0) {
1067 background->type = BACKGROUND_SCALE_CROP;
1068 } else if (strcmp(type, "tile") == 0) {
1069 background->type = BACKGROUND_TILE;
1070 } else {
1071 background->type = -1;
1072 fprintf(stderr, "invalid background-type: %s\n",
1073 type);
1074 }
1075
1076 free(type);
1077
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001078 return background;
1079}
1080
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001081static int
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001082grab_surface_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001083 float x, float y, void *data)
1084{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001085 struct desktop *desktop = data;
1086
1087 return desktop->grab_cursor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001088}
1089
1090static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001091grab_surface_destroy(struct desktop *desktop)
1092{
1093 widget_destroy(desktop->grab_widget);
1094 window_destroy(desktop->grab_window);
1095}
1096
1097static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001098grab_surface_create(struct desktop *desktop)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001099{
1100 struct wl_surface *s;
1101
Ander Conselvan de Oliveira07a91cd2012-07-16 14:15:50 +03001102 desktop->grab_window = window_create_custom(desktop->display);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001103 window_set_user_data(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001104
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001105 s = window_get_wl_surface(desktop->grab_window);
1106 desktop_shell_set_grab_surface(desktop->shell, s);
1107
1108 desktop->grab_widget =
1109 window_add_widget(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001110 /* We set the allocation to 1x1 at 0,0 so the fake enter event
1111 * at 0,0 will go to this widget. */
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001112 widget_set_allocation(desktop->grab_widget, 0, 0, 1, 1);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001113
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001114 widget_set_enter_handler(desktop->grab_widget,
1115 grab_surface_enter_handler);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001116}
1117
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001118static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001119output_destroy(struct output *output)
1120{
1121 background_destroy(output->background);
1122 panel_destroy(output->panel);
1123 wl_output_destroy(output->output);
1124 wl_list_remove(&output->link);
1125
1126 free(output);
1127}
1128
1129static void
1130desktop_destroy_outputs(struct desktop *desktop)
1131{
1132 struct output *tmp;
1133 struct output *output;
1134
1135 wl_list_for_each_safe(output, tmp, &desktop->outputs, link)
1136 output_destroy(output);
1137}
1138
1139static void
Alexander Larssonc584fa62013-05-22 14:41:32 +02001140output_handle_geometry(void *data,
1141 struct wl_output *wl_output,
1142 int x, int y,
1143 int physical_width,
1144 int physical_height,
1145 int subpixel,
1146 const char *make,
1147 const char *model,
1148 int transform)
1149{
1150 struct output *output = data;
1151
1152 window_set_buffer_transform(output->panel->window, transform);
1153 window_set_buffer_transform(output->background->window, transform);
1154}
1155
1156static void
1157output_handle_mode(void *data,
1158 struct wl_output *wl_output,
1159 uint32_t flags,
1160 int width,
1161 int height,
1162 int refresh)
1163{
1164}
1165
1166static void
1167output_handle_done(void *data,
1168 struct wl_output *wl_output)
1169{
1170}
1171
1172static void
1173output_handle_scale(void *data,
1174 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001175 int32_t scale)
Alexander Larssonc584fa62013-05-22 14:41:32 +02001176{
1177 struct output *output = data;
1178
1179 window_set_buffer_scale(output->panel->window, scale);
1180 window_set_buffer_scale(output->background->window, scale);
1181}
1182
1183static const struct wl_output_listener output_listener = {
1184 output_handle_geometry,
1185 output_handle_mode,
1186 output_handle_done,
1187 output_handle_scale
1188};
1189
1190static void
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001191output_init(struct output *output, struct desktop *desktop)
1192{
1193 struct wl_surface *surface;
1194
1195 output->panel = panel_create(desktop);
1196 surface = window_get_wl_surface(output->panel->window);
1197 desktop_shell_set_panel(desktop->shell,
1198 output->output, surface);
1199
1200 output->background = background_create(desktop);
1201 surface = window_get_wl_surface(output->background->window);
1202 desktop_shell_set_background(desktop->shell,
1203 output->output, surface);
1204}
1205
1206static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001207create_output(struct desktop *desktop, uint32_t id)
1208{
1209 struct output *output;
1210
1211 output = calloc(1, sizeof *output);
1212 if (!output)
1213 return;
1214
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001215 output->output =
Alexander Larssonc584fa62013-05-22 14:41:32 +02001216 display_bind(desktop->display, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001217 output->server_output_id = id;
Alexander Larssonc584fa62013-05-22 14:41:32 +02001218
1219 wl_output_add_listener(output->output, &output_listener, output);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001220
1221 wl_list_insert(&desktop->outputs, &output->link);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001222
1223 /* On start up we may process an output global before the shell global
1224 * in which case we can't create the panel and background just yet */
1225 if (desktop->shell)
1226 output_init(output, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001227}
1228
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001229static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001230global_handler(struct display *display, uint32_t id,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001231 const char *interface, uint32_t version, void *data)
1232{
1233 struct desktop *desktop = data;
1234
1235 if (!strcmp(interface, "desktop_shell")) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03001236 desktop->interface_version = (version < 2) ? version : 2;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001237 desktop->shell = display_bind(desktop->display,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03001238 id, &desktop_shell_interface,
1239 desktop->interface_version);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001240 desktop_shell_add_listener(desktop->shell, &listener, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001241 } else if (!strcmp(interface, "wl_output")) {
1242 create_output(desktop, id);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001243 }
1244}
1245
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001246static void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001247global_handler_remove(struct display *display, uint32_t id,
1248 const char *interface, uint32_t version, void *data)
1249{
1250 struct desktop *desktop = data;
1251 struct output *output;
1252
1253 if (!strcmp(interface, "wl_output")) {
1254 wl_list_for_each(output, &desktop->outputs, link) {
1255 if (output->server_output_id == id) {
1256 output_destroy(output);
1257 break;
1258 }
1259 }
1260 }
1261}
1262
1263static void
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001264panel_add_launchers(struct panel *panel, struct desktop *desktop)
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001265{
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001266 struct weston_config_section *s;
1267 char *icon, *path;
1268 const char *name;
1269 int count;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001270
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001271 count = 0;
1272 s = NULL;
1273 while (weston_config_next_section(desktop->config, &s, &name)) {
1274 if (strcmp(name, "launcher") != 0)
1275 continue;
1276
1277 weston_config_section_get_string(s, "icon", &icon, NULL);
1278 weston_config_section_get_string(s, "path", &path, NULL);
1279
1280 if (icon != NULL && path != NULL) {
1281 panel_add_launcher(panel, icon, path);
Rob Bradford09252d42013-07-26 16:29:45 +01001282 count++;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001283 } else {
1284 fprintf(stderr, "invalid launcher section\n");
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001285 }
1286
1287 free(icon);
1288 free(path);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001289 }
1290
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001291 if (count == 0) {
1292 /* add default launcher */
1293 panel_add_launcher(panel,
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001294 DATADIR "/weston/terminal.png",
Rodney Lorrimar99ff01b2012-02-29 17:31:03 +01001295 BINDIR "/weston-terminal");
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001296 }
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001297}
1298
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001299int main(int argc, char *argv[])
1300{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001301 struct desktop desktop = { 0 };
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001302 struct output *output;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001303 struct weston_config_section *s;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001304
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001305 desktop.unlock_task.run = unlock_dialog_finish;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001306 wl_list_init(&desktop.outputs);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001307
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001308 desktop.config = weston_config_parse("weston.ini");
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001309 s = weston_config_get_section(desktop.config, "shell", NULL, NULL);
1310 weston_config_section_get_bool(s, "locking", &desktop.locking, 1);
1311
Kristian Høgsberg4172f662013-02-20 15:27:49 -05001312 desktop.display = display_create(&argc, argv);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001313 if (desktop.display == NULL) {
1314 fprintf(stderr, "failed to create display: %m\n");
1315 return -1;
1316 }
1317
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001318 display_set_user_data(desktop.display, &desktop);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001319 display_set_global_handler(desktop.display, global_handler);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001320 display_set_global_handler_remove(desktop.display, global_handler_remove);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001321
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001322 /* Create panel and background for outputs processed before the shell
1323 * global interface was processed */
1324 wl_list_for_each(output, &desktop.outputs, link)
1325 if (!output->panel)
1326 output_init(output, &desktop);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001327
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001328 grab_surface_create(&desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001329
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001330 signal(SIGCHLD, sigchild_handler);
1331
1332 display_run(desktop.display);
1333
U. Artie Eoff44874d92012-10-02 21:12:35 -07001334 /* Cleanup */
1335 grab_surface_destroy(&desktop);
1336 desktop_destroy_outputs(&desktop);
1337 if (desktop.unlock_dialog)
1338 unlock_dialog_destroy(desktop.unlock_dialog);
1339 desktop_shell_destroy(desktop.shell);
1340 display_destroy(desktop.display);
1341
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001342 return 0;
1343}