blob: e121cc73af9412e0cd1bb9d22899a3fe5b39babb [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
U. Artie Eoff3c946772014-01-15 10:59:50 -080024#include "config.h"
25
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040026#include <stdint.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <fcntl.h>
31#include <unistd.h>
U. Artie Eoff3c946772014-01-15 10:59:50 -080032#include <errno.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040033#include <math.h>
34#include <cairo.h>
35#include <sys/wait.h>
Martin Minarik1e51a872012-06-08 00:39:11 +020036#include <sys/timerfd.h>
37#include <sys/epoll.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040038#include <linux/input.h>
Tiago Vignatti61500722012-05-23 22:06:28 +030039#include <libgen.h>
Kristian Høgsbergd1936b92012-07-23 22:59:33 -040040#include <ctype.h>
Martin Minarik1e51a872012-06-08 00:39:11 +020041#include <time.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040042
Pekka Paalanen50719bc2011-11-22 14:18:50 +020043#include <wayland-client.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040044#include "window.h"
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040045#include "../shared/cairo-util.h"
Kristian Høgsberg9b935c82011-12-08 12:44:27 -050046#include "../shared/config-parser.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040047
Pekka Paalanen50719bc2011-11-22 14:18:50 +020048#include "desktop-shell-client-protocol.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040049
Pekka Paalanenb6df4f72012-08-03 14:39:15 +030050extern char **environ; /* defined by libc */
51
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040052struct desktop {
53 struct display *display;
54 struct desktop_shell *shell;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030055 uint32_t interface_version;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +020056 struct unlock_dialog *unlock_dialog;
57 struct task unlock_task;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010058 struct wl_list outputs;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040059
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030060 struct window *grab_window;
61 struct widget *grab_widget;
62
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +030063 struct weston_config *config;
64 int locking;
65
Scott Moreauec116022012-07-22 18:23:52 -060066 enum cursor_type grab_cursor;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030067
68 int painted;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010069};
70
71struct surface {
72 void (*configure)(void *data,
73 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040074 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010075 int32_t width, int32_t height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040076};
77
78struct panel {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010079 struct surface base;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040080 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050081 struct widget *widget;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050082 struct wl_list launcher_list;
Martin Minarik1e51a872012-06-08 00:39:11 +020083 struct panel_clock *clock;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030084 int painted;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +030085 uint32_t color;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040086};
87
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010088struct background {
89 struct surface base;
90 struct window *window;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050091 struct widget *widget;
Pekka Paalanen79346ab2013-05-22 18:03:09 +030092 int painted;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +030093
94 char *image;
95 int type;
96 uint32_t color;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010097};
98
99struct output {
100 struct wl_output *output;
Xiong Zhang83d8ee72013-10-23 13:58:35 +0800101 uint32_t server_output_id;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100102 struct wl_list link;
103
104 struct panel *panel;
105 struct background *background;
106};
107
Kristian Høgsberg53880802012-01-09 11:16:50 -0500108struct panel_launcher {
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500109 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400110 struct panel *panel;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400111 cairo_surface_t *icon;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500112 int focused, pressed;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400113 char *path;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500114 struct wl_list link;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400115 struct wl_array envp;
116 struct wl_array argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400117};
118
Martin Minarik1e51a872012-06-08 00:39:11 +0200119struct panel_clock {
120 struct widget *widget;
121 struct panel *panel;
Martin Minarik1e51a872012-06-08 00:39:11 +0200122 struct task clock_task;
123 int clock_fd;
124};
125
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200126struct unlock_dialog {
127 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500128 struct widget *widget;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500129 struct widget *button;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500130 int button_focused;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200131 int closing;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200132 struct desktop *desktop;
133};
134
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300135static void
136panel_add_launchers(struct panel *panel, struct desktop *desktop);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500137
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400138static void
139sigchild_handler(int s)
140{
141 int status;
142 pid_t pid;
143
144 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
145 fprintf(stderr, "child %d exited\n", pid);
146}
147
148static void
Kristian Høgsberg67b82152013-10-23 16:52:05 -0700149menu_func(struct window *window, struct input *input, int index, void *data)
Pekka Paalanen9e30a822012-01-19 16:40:28 +0200150{
151 printf("Selected index %d from a panel menu.\n", index);
152}
153
154static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500155show_menu(struct panel *panel, struct input *input, uint32_t time)
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400156{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500157 int32_t x, y;
158 static const char *entries[] = {
159 "Roy", "Pris", "Leon", "Zhora"
160 };
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400161
162 input_get_position(input, &x, &y);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +0200163 window_show_menu(window_get_display(panel->window),
164 input, time, panel->window,
Pekka Paalanen9e30a822012-01-19 16:40:28 +0200165 x - 10, y - 10, menu_func, entries, 4);
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400166}
167
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300168static int
169is_desktop_painted(struct desktop *desktop)
170{
171 struct output *output;
172
173 wl_list_for_each(output, &desktop->outputs, link) {
174 if (output->panel && !output->panel->painted)
175 return 0;
176 if (output->background && !output->background->painted)
177 return 0;
178 }
179
180 return 1;
181}
182
183static void
184check_desktop_ready(struct window *window)
185{
186 struct display *display;
187 struct desktop *desktop;
188
189 display = window_get_display(window);
190 desktop = display_get_user_data(display);
191
192 if (!desktop->painted && is_desktop_painted(desktop)) {
193 desktop->painted = 1;
194
195 if (desktop->interface_version >= 2)
196 desktop_shell_desktop_ready(desktop->shell);
197 }
198}
199
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400200static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500201panel_launcher_activate(struct panel_launcher *widget)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400202{
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400203 char **argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400204 pid_t pid;
205
206 pid = fork();
207 if (pid < 0) {
208 fprintf(stderr, "fork failed: %m\n");
209 return;
210 }
211
212 if (pid)
213 return;
Benjamin Franzked7759712011-11-22 12:38:48 +0100214
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400215 argv = widget->argv.data;
216 if (execve(argv[0], argv, widget->envp.data) < 0) {
217 fprintf(stderr, "execl '%s' failed: %m\n", argv[0]);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400218 exit(1);
219 }
220}
221
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400222static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500223panel_launcher_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400224{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500225 struct panel_launcher *launcher = data;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500226 struct rectangle allocation;
227 cairo_t *cr;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400228
Alexander Larssonc584fa62013-05-22 14:41:32 +0200229 cr = widget_cairo_create(launcher->panel->widget);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500230
231 widget_get_allocation(widget, &allocation);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500232 if (launcher->pressed) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500233 allocation.x++;
234 allocation.y++;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400235 }
236
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500237 cairo_set_source_surface(cr, launcher->icon,
238 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400239 cairo_paint(cr);
240
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500241 if (launcher->focused) {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400242 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500243 cairo_mask_surface(cr, launcher->icon,
244 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400245 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400246
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500247 cairo_destroy(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400248}
249
Tiago Vignatti61500722012-05-23 22:06:28 +0300250static int
251panel_launcher_motion_handler(struct widget *widget, struct input *input,
252 uint32_t time, float x, float y, void *data)
253{
254 struct panel_launcher *launcher = data;
255
256 widget_set_tooltip(widget, basename((char *)launcher->path), x, y);
257
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300258 return CURSOR_LEFT_PTR;
Tiago Vignatti61500722012-05-23 22:06:28 +0300259}
260
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400261static void
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500262set_hex_color(cairo_t *cr, uint32_t color)
263{
264 cairo_set_source_rgba(cr,
265 ((color >> 16) & 0xff) / 255.0,
266 ((color >> 8) & 0xff) / 255.0,
267 ((color >> 0) & 0xff) / 255.0,
268 ((color >> 24) & 0xff) / 255.0);
269}
270
271static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500272panel_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400273{
274 cairo_surface_t *surface;
275 cairo_t *cr;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500276 struct panel *panel = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400277
Alexander Larssonc584fa62013-05-22 14:41:32 +0200278 cr = widget_cairo_create(panel->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400279 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300280 set_hex_color(cr, panel->color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400281 cairo_paint(cr);
282
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400283 cairo_destroy(cr);
Alexander Larssonc584fa62013-05-22 14:41:32 +0200284 surface = window_get_surface(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400285 cairo_surface_destroy(surface);
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300286 panel->painted = 1;
287 check_desktop_ready(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400288}
289
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500290static int
Kristian Høgsberg53880802012-01-09 11:16:50 -0500291panel_launcher_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400292 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500293{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500294 struct panel_launcher *launcher = data;
295
296 launcher->focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500297 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500298
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300299 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500300}
301
302static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500303panel_launcher_leave_handler(struct widget *widget,
304 struct input *input, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400305{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500306 struct panel_launcher *launcher = data;
307
308 launcher->focused = 0;
Tiago Vignatti61500722012-05-23 22:06:28 +0300309 widget_destroy_tooltip(widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500310 widget_schedule_redraw(widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400311}
312
313static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500314panel_launcher_button_handler(struct widget *widget,
315 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100316 uint32_t button,
317 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400318{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500319 struct panel_launcher *launcher;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400320
Kristian Høgsberg53880802012-01-09 11:16:50 -0500321 launcher = widget_get_user_data(widget);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500322 widget_schedule_redraw(widget);
Daniel Stone4dbadb12012-05-30 16:31:51 +0100323 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg53880802012-01-09 11:16:50 -0500324 panel_launcher_activate(launcher);
Rusty Lynch4384a242013-08-08 21:28:22 -0700325
326}
327
328static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700329panel_launcher_touch_down_handler(struct widget *widget, struct input *input,
330 uint32_t serial, uint32_t time, int32_t id,
Rusty Lynch4384a242013-08-08 21:28:22 -0700331 float x, float y, void *data)
332{
333 struct panel_launcher *launcher;
334
335 launcher = widget_get_user_data(widget);
336 launcher->focused = 1;
337 widget_schedule_redraw(widget);
338}
339
340static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700341panel_launcher_touch_up_handler(struct widget *widget, struct input *input,
342 uint32_t serial, uint32_t time, int32_t id,
343 void *data)
Rusty Lynch4384a242013-08-08 21:28:22 -0700344{
345 struct panel_launcher *launcher;
346
347 launcher = widget_get_user_data(widget);
348 launcher->focused = 0;
349 widget_schedule_redraw(widget);
350 panel_launcher_activate(launcher);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500351}
352
Martin Minarik1e51a872012-06-08 00:39:11 +0200353static void
354clock_func(struct task *task, uint32_t events)
355{
356 struct panel_clock *clock =
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400357 container_of(task, struct panel_clock, clock_task);
Kristian Høgsberg70226bb2012-06-08 16:54:52 -0400358 uint64_t exp;
Martin Minarik1e51a872012-06-08 00:39:11 +0200359
Martin Olsson8df662a2012-07-08 03:03:47 +0200360 if (read(clock->clock_fd, &exp, sizeof exp) != sizeof exp)
361 abort();
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400362 widget_schedule_redraw(clock->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200363}
364
365static void
366panel_clock_redraw_handler(struct widget *widget, void *data)
367{
Martin Minarik1e51a872012-06-08 00:39:11 +0200368 struct panel_clock *clock = data;
369 cairo_t *cr;
370 struct rectangle allocation;
371 cairo_text_extents_t extents;
372 cairo_font_extents_t font_extents;
Martin Minarik1e51a872012-06-08 00:39:11 +0200373 time_t rawtime;
374 struct tm * timeinfo;
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400375 char string[128];
Martin Minarik1e51a872012-06-08 00:39:11 +0200376
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400377 time(&rawtime);
378 timeinfo = localtime(&rawtime);
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400379 strftime(string, sizeof string, "%a %b %d, %I:%M %p", timeinfo);
Martin Minarik1e51a872012-06-08 00:39:11 +0200380
381 widget_get_allocation(widget, &allocation);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400382 if (allocation.width == 0)
383 return;
Martin Minarik1e51a872012-06-08 00:39:11 +0200384
Alexander Larssonc584fa62013-05-22 14:41:32 +0200385 cr = widget_cairo_create(clock->panel->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200386 cairo_select_font_face(cr, "sans",
387 CAIRO_FONT_SLANT_NORMAL,
388 CAIRO_FONT_WEIGHT_NORMAL);
389 cairo_set_font_size(cr, 14);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400390 cairo_text_extents(cr, string, &extents);
Martin Minarik1e51a872012-06-08 00:39:11 +0200391 cairo_font_extents (cr, &font_extents);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400392 cairo_move_to(cr, allocation.x + 5,
393 allocation.y + 3 * (allocation.height >> 2) + 1);
394 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400395 cairo_show_text(cr, string);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400396 cairo_move_to(cr, allocation.x + 4,
397 allocation.y + 3 * (allocation.height >> 2));
398 cairo_set_source_rgb(cr, 1, 1, 1);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400399 cairo_show_text(cr, string);
Martin Minarik1e51a872012-06-08 00:39:11 +0200400 cairo_destroy(cr);
401}
402
403static int
404clock_timer_reset(struct panel_clock *clock)
405{
406 struct itimerspec its;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400407
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400408 its.it_interval.tv_sec = 60;
Martin Minarik1e51a872012-06-08 00:39:11 +0200409 its.it_interval.tv_nsec = 0;
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400410 its.it_value.tv_sec = 60;
Martin Minarik1e51a872012-06-08 00:39:11 +0200411 its.it_value.tv_nsec = 0;
412 if (timerfd_settime(clock->clock_fd, 0, &its, NULL) < 0) {
413 fprintf(stderr, "could not set timerfd\n: %m");
414 return -1;
415 }
416
417 return 0;
418}
419
420static void
U. Artie Eoff44874d92012-10-02 21:12:35 -0700421panel_destroy_clock(struct panel_clock *clock)
422{
423 widget_destroy(clock->widget);
424
425 close(clock->clock_fd);
426
427 free(clock);
428}
429
430static void
Martin Minarik1e51a872012-06-08 00:39:11 +0200431panel_add_clock(struct panel *panel)
432{
433 struct panel_clock *clock;
434 int timerfd;
435
436 timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
437 if (timerfd < 0) {
438 fprintf(stderr, "could not create timerfd\n: %m");
439 return;
440 }
441
Peter Huttererf3d62272013-08-08 11:57:05 +1000442 clock = xzalloc(sizeof *clock);
Martin Minarik1e51a872012-06-08 00:39:11 +0200443 clock->panel = panel;
444 panel->clock = clock;
445 clock->clock_fd = timerfd;
446
447 clock->clock_task.run = clock_func;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400448 display_watch_fd(window_get_display(panel->window), clock->clock_fd,
449 EPOLLIN, &clock->clock_task);
Martin Minarik1e51a872012-06-08 00:39:11 +0200450 clock_timer_reset(clock);
451
452 clock->widget = widget_add_widget(panel->widget, clock);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400453 widget_set_redraw_handler(clock->widget, panel_clock_redraw_handler);
Martin Minarik1e51a872012-06-08 00:39:11 +0200454}
455
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500456static void
457panel_button_handler(struct widget *widget,
458 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100459 uint32_t button,
460 enum wl_pointer_button_state state, void *data)
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500461{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500462 struct panel *panel = data;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500463
Daniel Stone4dbadb12012-05-30 16:31:51 +0100464 if (button == BTN_RIGHT && state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500465 show_menu(panel, input, time);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400466}
467
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100468static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500469panel_resize_handler(struct widget *widget,
470 int32_t width, int32_t height, void *data)
471{
472 struct panel_launcher *launcher;
473 struct panel *panel = data;
474 int x, y, w, h;
475
476 x = 10;
477 y = 16;
478 wl_list_for_each(launcher, &panel->launcher_list, link) {
479 w = cairo_image_surface_get_width(launcher->icon);
480 h = cairo_image_surface_get_height(launcher->icon);
481 widget_set_allocation(launcher->widget,
482 x, y - h / 2, w + 1, h + 1);
483 x += w + 10;
484 }
Martin Minarik1e51a872012-06-08 00:39:11 +0200485 h=20;
486 w=170;
Pekka Paalanen01b17252012-06-12 17:42:26 +0300487
488 if (panel->clock)
489 widget_set_allocation(panel->clock->widget,
490 width - w - 8, y - h / 2, w + 1, h + 1);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500491}
492
493static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100494panel_configure(void *data,
495 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400496 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100497 int32_t width, int32_t height)
498{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500499 struct surface *surface = window_get_user_data(window);
500 struct panel *panel = container_of(surface, struct panel, base);
501
502 window_schedule_resize(panel->window, width, 32);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100503}
504
U. Artie Eoff44874d92012-10-02 21:12:35 -0700505static void
506panel_destroy_launcher(struct panel_launcher *launcher)
507{
508 wl_array_release(&launcher->argv);
509 wl_array_release(&launcher->envp);
510
511 free(launcher->path);
512
513 cairo_surface_destroy(launcher->icon);
514
515 widget_destroy(launcher->widget);
516 wl_list_remove(&launcher->link);
517
518 free(launcher);
519}
520
521static void
522panel_destroy(struct panel *panel)
523{
524 struct panel_launcher *tmp;
525 struct panel_launcher *launcher;
526
527 panel_destroy_clock(panel->clock);
528
529 wl_list_for_each_safe(launcher, tmp, &panel->launcher_list, link)
530 panel_destroy_launcher(launcher);
531
532 widget_destroy(panel->widget);
533 window_destroy(panel->window);
534
535 free(panel);
536}
537
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400538static struct panel *
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300539panel_create(struct desktop *desktop)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400540{
541 struct panel *panel;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300542 struct weston_config_section *s;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400543
Peter Huttererf3d62272013-08-08 11:57:05 +1000544 panel = xzalloc(sizeof *panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400545
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100546 panel->base.configure = panel_configure;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300547 panel->window = window_create_custom(desktop->display);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500548 panel->widget = window_add_widget(panel->window, panel);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500549 wl_list_init(&panel->launcher_list);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400550
551 window_set_title(panel->window, "panel");
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400552 window_set_user_data(panel->window, panel);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500553
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500554 widget_set_redraw_handler(panel->widget, panel_redraw_handler);
555 widget_set_resize_handler(panel->widget, panel_resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500556 widget_set_button_handler(panel->widget, panel_button_handler);
Rafal Mielniczukb9e513c2012-06-09 20:33:29 +0200557
558 panel_add_clock(panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400559
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300560 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
561 weston_config_section_get_uint(s, "panel-color",
562 &panel->color, 0xaa000000);
563
564 panel_add_launchers(panel, desktop);
565
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400566 return panel;
567}
568
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200569static cairo_surface_t *
570load_icon_or_fallback(const char *icon)
571{
572 cairo_surface_t *surface = cairo_image_surface_create_from_png(icon);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200573 cairo_status_t status;
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200574 cairo_t *cr;
575
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200576 status = cairo_surface_status(surface);
577 if (status == CAIRO_STATUS_SUCCESS)
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200578 return surface;
579
580 cairo_surface_destroy(surface);
Philipp Brüschweiler96386b82013-04-15 20:10:40 +0200581 fprintf(stderr, "ERROR loading icon from file '%s', error: '%s'\n",
582 icon, cairo_status_to_string(status));
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200583
584 /* draw fallback icon */
585 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
586 20, 20);
587 cr = cairo_create(surface);
588
589 cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1);
590 cairo_paint(cr);
591
592 cairo_set_source_rgba(cr, 0, 0, 0, 1);
593 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
594 cairo_rectangle(cr, 0, 0, 20, 20);
595 cairo_move_to(cr, 4, 4);
596 cairo_line_to(cr, 16, 16);
597 cairo_move_to(cr, 4, 16);
598 cairo_line_to(cr, 16, 4);
599 cairo_stroke(cr);
600
601 cairo_destroy(cr);
602
603 return surface;
604}
605
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400606static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500607panel_add_launcher(struct panel *panel, const char *icon, const char *path)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400608{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500609 struct panel_launcher *launcher;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400610 char *start, *p, *eq, **ps;
611 int i, j, k;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400612
Peter Huttererf3d62272013-08-08 11:57:05 +1000613 launcher = xzalloc(sizeof *launcher);
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200614 launcher->icon = load_icon_or_fallback(icon);
U. Artie Eoff3c946772014-01-15 10:59:50 -0800615 launcher->path = xstrdup(path);
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400616
617 wl_array_init(&launcher->envp);
618 wl_array_init(&launcher->argv);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300619 for (i = 0; environ[i]; i++) {
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400620 ps = wl_array_add(&launcher->envp, sizeof *ps);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300621 *ps = environ[i];
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400622 }
623 j = 0;
624
625 start = launcher->path;
626 while (*start) {
627 for (p = start, eq = NULL; *p && !isspace(*p); p++)
628 if (*p == '=')
629 eq = p;
630
631 if (eq && j == 0) {
632 ps = launcher->envp.data;
633 for (k = 0; k < i; k++)
634 if (strncmp(ps[k], start, eq - start) == 0) {
635 ps[k] = start;
636 break;
637 }
638 if (k == i) {
639 ps = wl_array_add(&launcher->envp, sizeof *ps);
640 *ps = start;
641 i++;
642 }
643 } else {
644 ps = wl_array_add(&launcher->argv, sizeof *ps);
645 *ps = start;
646 j++;
647 }
648
649 while (*p && isspace(*p))
650 *p++ = '\0';
651
652 start = p;
653 }
654
655 ps = wl_array_add(&launcher->envp, sizeof *ps);
656 *ps = NULL;
657 ps = wl_array_add(&launcher->argv, sizeof *ps);
658 *ps = NULL;
659
Kristian Høgsberg53880802012-01-09 11:16:50 -0500660 launcher->panel = panel;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500661 wl_list_insert(panel->launcher_list.prev, &launcher->link);
662
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500663 launcher->widget = widget_add_widget(panel->widget, launcher);
Kristian Høgsberg53880802012-01-09 11:16:50 -0500664 widget_set_enter_handler(launcher->widget,
665 panel_launcher_enter_handler);
666 widget_set_leave_handler(launcher->widget,
667 panel_launcher_leave_handler);
668 widget_set_button_handler(launcher->widget,
669 panel_launcher_button_handler);
Rusty Lynch4384a242013-08-08 21:28:22 -0700670 widget_set_touch_down_handler(launcher->widget,
671 panel_launcher_touch_down_handler);
672 widget_set_touch_up_handler(launcher->widget,
673 panel_launcher_touch_up_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500674 widget_set_redraw_handler(launcher->widget,
675 panel_launcher_redraw_handler);
Tiago Vignatti61500722012-05-23 22:06:28 +0300676 widget_set_motion_handler(launcher->widget,
677 panel_launcher_motion_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400678}
679
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500680enum {
681 BACKGROUND_SCALE,
Pekka Paalanena402b052013-05-22 18:03:10 +0300682 BACKGROUND_SCALE_CROP,
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500683 BACKGROUND_TILE
684};
685
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400686static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500687background_draw(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400688{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500689 struct background *background = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400690 cairo_surface_t *surface, *image;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400691 cairo_pattern_t *pattern;
692 cairo_matrix_t matrix;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400693 cairo_t *cr;
Pekka Paalanena402b052013-05-22 18:03:10 +0300694 double im_w, im_h;
695 double sx, sy, s;
696 double tx, ty;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500697 struct rectangle allocation;
Pekka Paalanen9564c752012-10-24 09:43:08 +0300698 struct display *display;
699 struct wl_region *opaque;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400700
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500701 surface = window_get_surface(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400702
Alexander Larssonc584fa62013-05-22 14:41:32 +0200703 cr = widget_cairo_create(background->widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400704 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
705 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
706 cairo_paint(cr);
707
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500708 widget_get_allocation(widget, &allocation);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500709 image = NULL;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300710 if (background->image)
711 image = load_cairo_surface(background->image);
Kristian Høgsberg4c3661f2014-04-21 22:54:37 -0700712 else if (background->color == 0)
713 image = load_cairo_surface(DATADIR "/weston/pattern.png");
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500714
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300715 if (image && background->type != -1) {
Pekka Paalanena402b052013-05-22 18:03:10 +0300716 im_w = cairo_image_surface_get_width(image);
717 im_h = cairo_image_surface_get_height(image);
718 sx = im_w / allocation.width;
719 sy = im_h / allocation.height;
720
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400721 pattern = cairo_pattern_create_for_surface(image);
Pekka Paalanena402b052013-05-22 18:03:10 +0300722
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300723 switch (background->type) {
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500724 case BACKGROUND_SCALE:
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500725 cairo_matrix_init_scale(&matrix, sx, sy);
726 cairo_pattern_set_matrix(pattern, &matrix);
Bill Spitzak79b7cb32014-05-08 20:00:35 -0700727 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500728 break;
Pekka Paalanena402b052013-05-22 18:03:10 +0300729 case BACKGROUND_SCALE_CROP:
730 s = (sx < sy) ? sx : sy;
731 /* align center */
732 tx = (im_w - s * allocation.width) * 0.5;
733 ty = (im_h - s * allocation.height) * 0.5;
734 cairo_matrix_init_translate(&matrix, tx, ty);
735 cairo_matrix_scale(&matrix, s, s);
736 cairo_pattern_set_matrix(pattern, &matrix);
Bill Spitzak79b7cb32014-05-08 20:00:35 -0700737 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_PAD);
Pekka Paalanena402b052013-05-22 18:03:10 +0300738 break;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500739 case BACKGROUND_TILE:
740 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
741 break;
742 }
Pekka Paalanena402b052013-05-22 18:03:10 +0300743
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400744 cairo_set_source(cr, pattern);
745 cairo_pattern_destroy (pattern);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400746 cairo_surface_destroy(image);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500747 } else {
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300748 set_hex_color(cr, background->color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400749 }
750
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500751 cairo_paint(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400752 cairo_destroy(cr);
753 cairo_surface_destroy(surface);
Pekka Paalanen9564c752012-10-24 09:43:08 +0300754
755 display = window_get_display(background->window);
756 opaque = wl_compositor_create_region(display_get_compositor(display));
757 wl_region_add(opaque, allocation.x, allocation.y,
758 allocation.width, allocation.height);
759 wl_surface_set_opaque_region(window_get_wl_surface(background->window), opaque);
760 wl_region_destroy(opaque);
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300761
762 background->painted = 1;
763 check_desktop_ready(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400764}
765
766static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100767background_configure(void *data,
768 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400769 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100770 int32_t width, int32_t height)
771{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500772 struct background *background =
773 (struct background *) window_get_user_data(window);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100774
Kristian Høgsberg4598f962014-01-01 16:32:09 -0800775 widget_schedule_resize(background->widget, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100776}
777
778static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500779unlock_dialog_redraw_handler(struct widget *widget, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200780{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500781 struct unlock_dialog *dialog = data;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200782 struct rectangle allocation;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200783 cairo_surface_t *surface;
Alexander Larssonc584fa62013-05-22 14:41:32 +0200784 cairo_t *cr;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200785 cairo_pattern_t *pat;
786 double cx, cy, r, f;
787
Alexander Larssonc584fa62013-05-22 14:41:32 +0200788 cr = widget_cairo_create(widget);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500789
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500790 widget_get_allocation(dialog->widget, &allocation);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200791 cairo_rectangle(cr, allocation.x, allocation.y,
792 allocation.width, allocation.height);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200793 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
794 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500795 cairo_fill(cr);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200796
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500797 cairo_translate(cr, allocation.x, allocation.y);
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500798 if (dialog->button_focused)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200799 f = 1.0;
800 else
801 f = 0.7;
802
803 cx = allocation.width / 2.0;
804 cy = allocation.height / 2.0;
805 r = (cx < cy ? cx : cy) * 0.4;
806 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
807 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
808 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
809 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
810 cairo_set_source(cr, pat);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500811 cairo_pattern_destroy(pat);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200812 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
813 cairo_fill(cr);
814
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500815 widget_set_allocation(dialog->button,
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500816 allocation.x + cx - r,
817 allocation.y + cy - r, 2 * r, 2 * r);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200818
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200819 cairo_destroy(cr);
820
Alexander Larssonc584fa62013-05-22 14:41:32 +0200821 surface = window_get_surface(dialog->window);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200822 cairo_surface_destroy(surface);
823}
824
825static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500826unlock_dialog_button_handler(struct widget *widget,
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200827 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100828 uint32_t button,
829 enum wl_pointer_button_state state, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200830{
831 struct unlock_dialog *dialog = data;
832 struct desktop *desktop = dialog->desktop;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200833
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500834 if (button == BTN_LEFT) {
Daniel Stone4dbadb12012-05-30 16:31:51 +0100835 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
836 !dialog->closing) {
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200837 display_defer(desktop->display, &desktop->unlock_task);
838 dialog->closing = 1;
839 }
840 }
841}
842
843static void
Brian J Lovinc4df4082013-08-26 15:58:22 -0700844unlock_dialog_touch_down_handler(struct widget *widget, struct input *input,
845 uint32_t serial, uint32_t time, int32_t id,
846 float x, float y, void *data)
847{
848 struct unlock_dialog *dialog = data;
849
850 dialog->button_focused = 1;
851 widget_schedule_redraw(widget);
852}
853
854static void
855unlock_dialog_touch_up_handler(struct widget *widget, struct input *input,
856 uint32_t serial, uint32_t time, int32_t id,
857 void *data)
858{
859 struct unlock_dialog *dialog = data;
860 struct desktop *desktop = dialog->desktop;
861
862 dialog->button_focused = 0;
863 widget_schedule_redraw(widget);
864 display_defer(desktop->display, &desktop->unlock_task);
865 dialog->closing = 1;
866}
867
868static void
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200869unlock_dialog_keyboard_focus_handler(struct window *window,
870 struct input *device, void *data)
871{
872 window_schedule_redraw(window);
873}
874
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500875static int
Kristian Høgsbergee143232012-01-09 08:42:24 -0500876unlock_dialog_widget_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400877 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400878 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500879{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500880 struct unlock_dialog *dialog = data;
881
882 dialog->button_focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500883 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500884
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300885 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500886}
887
888static void
889unlock_dialog_widget_leave_handler(struct widget *widget,
890 struct input *input, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200891{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500892 struct unlock_dialog *dialog = data;
893
894 dialog->button_focused = 0;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500895 widget_schedule_redraw(widget);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200896}
897
898static struct unlock_dialog *
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500899unlock_dialog_create(struct desktop *desktop)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200900{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500901 struct display *display = desktop->display;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200902 struct unlock_dialog *dialog;
903
Peter Huttererf3d62272013-08-08 11:57:05 +1000904 dialog = xzalloc(sizeof *dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200905
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400906 dialog->window = window_create_custom(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500907 dialog->widget = window_frame_create(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200908 window_set_title(dialog->window, "Unlock your desktop");
909
910 window_set_user_data(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200911 window_set_keyboard_focus_handler(dialog->window,
912 unlock_dialog_keyboard_focus_handler);
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500913 dialog->button = widget_add_widget(dialog->widget, dialog);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500914 widget_set_redraw_handler(dialog->widget,
915 unlock_dialog_redraw_handler);
Kristian Høgsbergee143232012-01-09 08:42:24 -0500916 widget_set_enter_handler(dialog->button,
917 unlock_dialog_widget_enter_handler);
918 widget_set_leave_handler(dialog->button,
919 unlock_dialog_widget_leave_handler);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500920 widget_set_button_handler(dialog->button,
921 unlock_dialog_button_handler);
Brian J Lovinc4df4082013-08-26 15:58:22 -0700922 widget_set_touch_down_handler(dialog->button,
923 unlock_dialog_touch_down_handler);
924 widget_set_touch_up_handler(dialog->button,
925 unlock_dialog_touch_up_handler);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200926
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500927 desktop_shell_set_lock_surface(desktop->shell,
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400928 window_get_wl_surface(dialog->window));
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500929
Pekka Paalanen40e49ac2012-01-18 16:51:30 +0200930 window_schedule_resize(dialog->window, 260, 230);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200931
932 return dialog;
933}
934
935static void
936unlock_dialog_destroy(struct unlock_dialog *dialog)
937{
938 window_destroy(dialog->window);
939 free(dialog);
940}
941
942static void
943unlock_dialog_finish(struct task *task, uint32_t events)
944{
945 struct desktop *desktop =
Benjamin Franzked7759712011-11-22 12:38:48 +0100946 container_of(task, struct desktop, unlock_task);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200947
948 desktop_shell_unlock(desktop->shell);
949 unlock_dialog_destroy(desktop->unlock_dialog);
950 desktop->unlock_dialog = NULL;
951}
952
953static void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400954desktop_shell_configure(void *data,
955 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400956 uint32_t edges,
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400957 struct wl_surface *surface,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400958 int32_t width, int32_t height)
959{
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400960 struct window *window = wl_surface_get_user_data(surface);
Pekka Paalanen068ae942011-11-28 14:11:15 +0200961 struct surface *s = window_get_user_data(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400962
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400963 s->configure(data, desktop_shell, edges, window, width, height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400964}
965
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200966static void
967desktop_shell_prepare_lock_surface(void *data,
968 struct desktop_shell *desktop_shell)
969{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200970 struct desktop *desktop = data;
971
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300972 if (!desktop->locking) {
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200973 desktop_shell_unlock(desktop->shell);
974 return;
975 }
976
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200977 if (!desktop->unlock_dialog) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500978 desktop->unlock_dialog = unlock_dialog_create(desktop);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200979 desktop->unlock_dialog->desktop = desktop;
980 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200981}
982
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300983static void
984desktop_shell_grab_cursor(void *data,
985 struct desktop_shell *desktop_shell,
986 uint32_t cursor)
987{
988 struct desktop *desktop = data;
989
990 switch (cursor) {
Philipp Brüschweiler16d59d72012-08-24 15:43:55 +0200991 case DESKTOP_SHELL_CURSOR_NONE:
992 desktop->grab_cursor = CURSOR_BLANK;
993 break;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300994 case DESKTOP_SHELL_CURSOR_BUSY:
995 desktop->grab_cursor = CURSOR_WATCH;
996 break;
997 case DESKTOP_SHELL_CURSOR_MOVE:
998 desktop->grab_cursor = CURSOR_DRAGGING;
999 break;
1000 case DESKTOP_SHELL_CURSOR_RESIZE_TOP:
1001 desktop->grab_cursor = CURSOR_TOP;
1002 break;
1003 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM:
1004 desktop->grab_cursor = CURSOR_BOTTOM;
1005 break;
1006 case DESKTOP_SHELL_CURSOR_RESIZE_LEFT:
1007 desktop->grab_cursor = CURSOR_LEFT;
1008 break;
1009 case DESKTOP_SHELL_CURSOR_RESIZE_RIGHT:
1010 desktop->grab_cursor = CURSOR_RIGHT;
1011 break;
1012 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_LEFT:
1013 desktop->grab_cursor = CURSOR_TOP_LEFT;
1014 break;
1015 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_RIGHT:
1016 desktop->grab_cursor = CURSOR_TOP_RIGHT;
1017 break;
1018 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_LEFT:
1019 desktop->grab_cursor = CURSOR_BOTTOM_LEFT;
1020 break;
1021 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_RIGHT:
1022 desktop->grab_cursor = CURSOR_BOTTOM_RIGHT;
1023 break;
1024 case DESKTOP_SHELL_CURSOR_ARROW:
1025 default:
1026 desktop->grab_cursor = CURSOR_LEFT_PTR;
1027 }
1028}
1029
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001030static const struct desktop_shell_listener listener = {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001031 desktop_shell_configure,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001032 desktop_shell_prepare_lock_surface,
1033 desktop_shell_grab_cursor
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001034};
1035
U. Artie Eoff44874d92012-10-02 21:12:35 -07001036static void
1037background_destroy(struct background *background)
1038{
1039 widget_destroy(background->widget);
1040 window_destroy(background->window);
1041
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001042 free(background->image);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001043 free(background);
1044}
1045
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001046static struct background *
1047background_create(struct desktop *desktop)
1048{
1049 struct background *background;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001050 struct weston_config_section *s;
1051 char *type;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001052
Peter Huttererf3d62272013-08-08 11:57:05 +10001053 background = xzalloc(sizeof *background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001054 background->base.configure = background_configure;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001055 background->window = window_create_custom(desktop->display);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001056 background->widget = window_add_widget(background->window, background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001057 window_set_user_data(background->window, background);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001058 widget_set_redraw_handler(background->widget, background_draw);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001059 window_set_preferred_format(background->window,
1060 WINDOW_PREFERRED_FORMAT_RGB565);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001061
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001062 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
1063 weston_config_section_get_string(s, "background-image",
Kristian Høgsberg4c3661f2014-04-21 22:54:37 -07001064 &background->image, NULL);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001065 weston_config_section_get_uint(s, "background-color",
Kristian Høgsberg4c3661f2014-04-21 22:54:37 -07001066 &background->color, 0);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001067
1068 weston_config_section_get_string(s, "background-type",
1069 &type, "tile");
U. Artie Eoff3c946772014-01-15 10:59:50 -08001070 if (type == NULL) {
1071 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
1072 exit(EXIT_FAILURE);
1073 }
1074
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001075 if (strcmp(type, "scale") == 0) {
1076 background->type = BACKGROUND_SCALE;
1077 } else if (strcmp(type, "scale-crop") == 0) {
1078 background->type = BACKGROUND_SCALE_CROP;
1079 } else if (strcmp(type, "tile") == 0) {
1080 background->type = BACKGROUND_TILE;
1081 } else {
1082 background->type = -1;
1083 fprintf(stderr, "invalid background-type: %s\n",
1084 type);
1085 }
1086
1087 free(type);
1088
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001089 return background;
1090}
1091
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001092static int
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001093grab_surface_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001094 float x, float y, void *data)
1095{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001096 struct desktop *desktop = data;
1097
1098 return desktop->grab_cursor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001099}
1100
1101static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001102grab_surface_destroy(struct desktop *desktop)
1103{
1104 widget_destroy(desktop->grab_widget);
1105 window_destroy(desktop->grab_window);
1106}
1107
1108static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001109grab_surface_create(struct desktop *desktop)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001110{
1111 struct wl_surface *s;
1112
Ander Conselvan de Oliveira07a91cd2012-07-16 14:15:50 +03001113 desktop->grab_window = window_create_custom(desktop->display);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001114 window_set_user_data(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001115
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001116 s = window_get_wl_surface(desktop->grab_window);
1117 desktop_shell_set_grab_surface(desktop->shell, s);
1118
1119 desktop->grab_widget =
1120 window_add_widget(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001121 /* We set the allocation to 1x1 at 0,0 so the fake enter event
1122 * at 0,0 will go to this widget. */
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001123 widget_set_allocation(desktop->grab_widget, 0, 0, 1, 1);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001124
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001125 widget_set_enter_handler(desktop->grab_widget,
1126 grab_surface_enter_handler);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001127}
1128
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001129static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001130output_destroy(struct output *output)
1131{
1132 background_destroy(output->background);
1133 panel_destroy(output->panel);
1134 wl_output_destroy(output->output);
1135 wl_list_remove(&output->link);
1136
1137 free(output);
1138}
1139
1140static void
1141desktop_destroy_outputs(struct desktop *desktop)
1142{
1143 struct output *tmp;
1144 struct output *output;
1145
1146 wl_list_for_each_safe(output, tmp, &desktop->outputs, link)
1147 output_destroy(output);
1148}
1149
1150static void
Alexander Larssonc584fa62013-05-22 14:41:32 +02001151output_handle_geometry(void *data,
1152 struct wl_output *wl_output,
1153 int x, int y,
1154 int physical_width,
1155 int physical_height,
1156 int subpixel,
1157 const char *make,
1158 const char *model,
1159 int transform)
1160{
1161 struct output *output = data;
1162
1163 window_set_buffer_transform(output->panel->window, transform);
1164 window_set_buffer_transform(output->background->window, transform);
1165}
1166
1167static void
1168output_handle_mode(void *data,
1169 struct wl_output *wl_output,
1170 uint32_t flags,
1171 int width,
1172 int height,
1173 int refresh)
1174{
1175}
1176
1177static void
1178output_handle_done(void *data,
1179 struct wl_output *wl_output)
1180{
1181}
1182
1183static void
1184output_handle_scale(void *data,
1185 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001186 int32_t scale)
Alexander Larssonc584fa62013-05-22 14:41:32 +02001187{
1188 struct output *output = data;
1189
1190 window_set_buffer_scale(output->panel->window, scale);
1191 window_set_buffer_scale(output->background->window, scale);
1192}
1193
1194static const struct wl_output_listener output_listener = {
1195 output_handle_geometry,
1196 output_handle_mode,
1197 output_handle_done,
1198 output_handle_scale
1199};
1200
1201static void
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001202output_init(struct output *output, struct desktop *desktop)
1203{
1204 struct wl_surface *surface;
1205
1206 output->panel = panel_create(desktop);
1207 surface = window_get_wl_surface(output->panel->window);
1208 desktop_shell_set_panel(desktop->shell,
1209 output->output, surface);
1210
1211 output->background = background_create(desktop);
1212 surface = window_get_wl_surface(output->background->window);
1213 desktop_shell_set_background(desktop->shell,
1214 output->output, surface);
1215}
1216
1217static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001218create_output(struct desktop *desktop, uint32_t id)
1219{
1220 struct output *output;
1221
1222 output = calloc(1, sizeof *output);
1223 if (!output)
1224 return;
1225
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001226 output->output =
Alexander Larssonc584fa62013-05-22 14:41:32 +02001227 display_bind(desktop->display, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001228 output->server_output_id = id;
Alexander Larssonc584fa62013-05-22 14:41:32 +02001229
1230 wl_output_add_listener(output->output, &output_listener, output);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001231
1232 wl_list_insert(&desktop->outputs, &output->link);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001233
1234 /* On start up we may process an output global before the shell global
1235 * in which case we can't create the panel and background just yet */
1236 if (desktop->shell)
1237 output_init(output, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001238}
1239
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001240static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001241global_handler(struct display *display, uint32_t id,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001242 const char *interface, uint32_t version, void *data)
1243{
1244 struct desktop *desktop = data;
1245
1246 if (!strcmp(interface, "desktop_shell")) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03001247 desktop->interface_version = (version < 2) ? version : 2;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001248 desktop->shell = display_bind(desktop->display,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03001249 id, &desktop_shell_interface,
1250 desktop->interface_version);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001251 desktop_shell_add_listener(desktop->shell, &listener, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001252 } else if (!strcmp(interface, "wl_output")) {
1253 create_output(desktop, id);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001254 }
1255}
1256
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001257static void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001258global_handler_remove(struct display *display, uint32_t id,
1259 const char *interface, uint32_t version, void *data)
1260{
1261 struct desktop *desktop = data;
1262 struct output *output;
1263
1264 if (!strcmp(interface, "wl_output")) {
1265 wl_list_for_each(output, &desktop->outputs, link) {
1266 if (output->server_output_id == id) {
1267 output_destroy(output);
1268 break;
1269 }
1270 }
1271 }
1272}
1273
1274static void
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001275panel_add_launchers(struct panel *panel, struct desktop *desktop)
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001276{
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001277 struct weston_config_section *s;
1278 char *icon, *path;
1279 const char *name;
1280 int count;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001281
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001282 count = 0;
1283 s = NULL;
1284 while (weston_config_next_section(desktop->config, &s, &name)) {
1285 if (strcmp(name, "launcher") != 0)
1286 continue;
1287
1288 weston_config_section_get_string(s, "icon", &icon, NULL);
1289 weston_config_section_get_string(s, "path", &path, NULL);
1290
1291 if (icon != NULL && path != NULL) {
1292 panel_add_launcher(panel, icon, path);
Rob Bradford09252d42013-07-26 16:29:45 +01001293 count++;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001294 } else {
1295 fprintf(stderr, "invalid launcher section\n");
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001296 }
1297
1298 free(icon);
1299 free(path);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001300 }
1301
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001302 if (count == 0) {
1303 /* add default launcher */
1304 panel_add_launcher(panel,
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001305 DATADIR "/weston/terminal.png",
Rodney Lorrimar99ff01b2012-02-29 17:31:03 +01001306 BINDIR "/weston-terminal");
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001307 }
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001308}
1309
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001310int main(int argc, char *argv[])
1311{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001312 struct desktop desktop = { 0 };
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001313 struct output *output;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001314 struct weston_config_section *s;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001315
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001316 desktop.unlock_task.run = unlock_dialog_finish;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001317 wl_list_init(&desktop.outputs);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001318
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001319 desktop.config = weston_config_parse("weston.ini");
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001320 s = weston_config_get_section(desktop.config, "shell", NULL, NULL);
1321 weston_config_section_get_bool(s, "locking", &desktop.locking, 1);
1322
Kristian Høgsberg4172f662013-02-20 15:27:49 -05001323 desktop.display = display_create(&argc, argv);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001324 if (desktop.display == NULL) {
1325 fprintf(stderr, "failed to create display: %m\n");
1326 return -1;
1327 }
1328
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001329 display_set_user_data(desktop.display, &desktop);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001330 display_set_global_handler(desktop.display, global_handler);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001331 display_set_global_handler_remove(desktop.display, global_handler_remove);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001332
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001333 /* Create panel and background for outputs processed before the shell
1334 * global interface was processed */
1335 wl_list_for_each(output, &desktop.outputs, link)
1336 if (!output->panel)
1337 output_init(output, &desktop);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001338
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001339 grab_surface_create(&desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001340
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001341 signal(SIGCHLD, sigchild_handler);
1342
1343 display_run(desktop.display);
1344
U. Artie Eoff44874d92012-10-02 21:12:35 -07001345 /* Cleanup */
1346 grab_surface_destroy(&desktop);
1347 desktop_destroy_outputs(&desktop);
1348 if (desktop.unlock_dialog)
1349 unlock_dialog_destroy(desktop.unlock_dialog);
1350 desktop_shell_destroy(desktop.shell);
1351 display_destroy(desktop.display);
1352
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001353 return 0;
1354}