blob: 4880888adaf14b566c97d295f59763e81e303498 [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);
727 break;
Pekka Paalanena402b052013-05-22 18:03:10 +0300728 case BACKGROUND_SCALE_CROP:
729 s = (sx < sy) ? sx : sy;
730 /* align center */
731 tx = (im_w - s * allocation.width) * 0.5;
732 ty = (im_h - s * allocation.height) * 0.5;
733 cairo_matrix_init_translate(&matrix, tx, ty);
734 cairo_matrix_scale(&matrix, s, s);
735 cairo_pattern_set_matrix(pattern, &matrix);
736 break;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500737 case BACKGROUND_TILE:
738 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
739 break;
740 }
Pekka Paalanena402b052013-05-22 18:03:10 +0300741
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400742 cairo_set_source(cr, pattern);
743 cairo_pattern_destroy (pattern);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400744 cairo_surface_destroy(image);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500745 } else {
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300746 set_hex_color(cr, background->color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400747 }
748
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500749 cairo_paint(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400750 cairo_destroy(cr);
751 cairo_surface_destroy(surface);
Pekka Paalanen9564c752012-10-24 09:43:08 +0300752
753 display = window_get_display(background->window);
754 opaque = wl_compositor_create_region(display_get_compositor(display));
755 wl_region_add(opaque, allocation.x, allocation.y,
756 allocation.width, allocation.height);
757 wl_surface_set_opaque_region(window_get_wl_surface(background->window), opaque);
758 wl_region_destroy(opaque);
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300759
760 background->painted = 1;
761 check_desktop_ready(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400762}
763
764static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100765background_configure(void *data,
766 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400767 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100768 int32_t width, int32_t height)
769{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500770 struct background *background =
771 (struct background *) window_get_user_data(window);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100772
Kristian Høgsberg4598f962014-01-01 16:32:09 -0800773 widget_schedule_resize(background->widget, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100774}
775
776static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500777unlock_dialog_redraw_handler(struct widget *widget, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200778{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500779 struct unlock_dialog *dialog = data;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200780 struct rectangle allocation;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200781 cairo_surface_t *surface;
Alexander Larssonc584fa62013-05-22 14:41:32 +0200782 cairo_t *cr;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200783 cairo_pattern_t *pat;
784 double cx, cy, r, f;
785
Alexander Larssonc584fa62013-05-22 14:41:32 +0200786 cr = widget_cairo_create(widget);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500787
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500788 widget_get_allocation(dialog->widget, &allocation);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200789 cairo_rectangle(cr, allocation.x, allocation.y,
790 allocation.width, allocation.height);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200791 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
792 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500793 cairo_fill(cr);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200794
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500795 cairo_translate(cr, allocation.x, allocation.y);
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500796 if (dialog->button_focused)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200797 f = 1.0;
798 else
799 f = 0.7;
800
801 cx = allocation.width / 2.0;
802 cy = allocation.height / 2.0;
803 r = (cx < cy ? cx : cy) * 0.4;
804 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
805 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
806 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
807 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
808 cairo_set_source(cr, pat);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500809 cairo_pattern_destroy(pat);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200810 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
811 cairo_fill(cr);
812
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500813 widget_set_allocation(dialog->button,
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500814 allocation.x + cx - r,
815 allocation.y + cy - r, 2 * r, 2 * r);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200816
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200817 cairo_destroy(cr);
818
Alexander Larssonc584fa62013-05-22 14:41:32 +0200819 surface = window_get_surface(dialog->window);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200820 cairo_surface_destroy(surface);
821}
822
823static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500824unlock_dialog_button_handler(struct widget *widget,
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200825 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100826 uint32_t button,
827 enum wl_pointer_button_state state, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200828{
829 struct unlock_dialog *dialog = data;
830 struct desktop *desktop = dialog->desktop;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200831
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500832 if (button == BTN_LEFT) {
Daniel Stone4dbadb12012-05-30 16:31:51 +0100833 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
834 !dialog->closing) {
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200835 display_defer(desktop->display, &desktop->unlock_task);
836 dialog->closing = 1;
837 }
838 }
839}
840
841static void
Brian J Lovinc4df4082013-08-26 15:58:22 -0700842unlock_dialog_touch_down_handler(struct widget *widget, struct input *input,
843 uint32_t serial, uint32_t time, int32_t id,
844 float x, float y, void *data)
845{
846 struct unlock_dialog *dialog = data;
847
848 dialog->button_focused = 1;
849 widget_schedule_redraw(widget);
850}
851
852static void
853unlock_dialog_touch_up_handler(struct widget *widget, struct input *input,
854 uint32_t serial, uint32_t time, int32_t id,
855 void *data)
856{
857 struct unlock_dialog *dialog = data;
858 struct desktop *desktop = dialog->desktop;
859
860 dialog->button_focused = 0;
861 widget_schedule_redraw(widget);
862 display_defer(desktop->display, &desktop->unlock_task);
863 dialog->closing = 1;
864}
865
866static void
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200867unlock_dialog_keyboard_focus_handler(struct window *window,
868 struct input *device, void *data)
869{
870 window_schedule_redraw(window);
871}
872
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500873static int
Kristian Høgsbergee143232012-01-09 08:42:24 -0500874unlock_dialog_widget_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400875 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400876 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500877{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500878 struct unlock_dialog *dialog = data;
879
880 dialog->button_focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500881 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500882
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300883 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500884}
885
886static void
887unlock_dialog_widget_leave_handler(struct widget *widget,
888 struct input *input, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200889{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500890 struct unlock_dialog *dialog = data;
891
892 dialog->button_focused = 0;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500893 widget_schedule_redraw(widget);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200894}
895
896static struct unlock_dialog *
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500897unlock_dialog_create(struct desktop *desktop)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200898{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500899 struct display *display = desktop->display;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200900 struct unlock_dialog *dialog;
901
Peter Huttererf3d62272013-08-08 11:57:05 +1000902 dialog = xzalloc(sizeof *dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200903
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400904 dialog->window = window_create_custom(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500905 dialog->widget = window_frame_create(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200906 window_set_title(dialog->window, "Unlock your desktop");
907
908 window_set_user_data(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200909 window_set_keyboard_focus_handler(dialog->window,
910 unlock_dialog_keyboard_focus_handler);
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500911 dialog->button = widget_add_widget(dialog->widget, dialog);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500912 widget_set_redraw_handler(dialog->widget,
913 unlock_dialog_redraw_handler);
Kristian Høgsbergee143232012-01-09 08:42:24 -0500914 widget_set_enter_handler(dialog->button,
915 unlock_dialog_widget_enter_handler);
916 widget_set_leave_handler(dialog->button,
917 unlock_dialog_widget_leave_handler);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500918 widget_set_button_handler(dialog->button,
919 unlock_dialog_button_handler);
Brian J Lovinc4df4082013-08-26 15:58:22 -0700920 widget_set_touch_down_handler(dialog->button,
921 unlock_dialog_touch_down_handler);
922 widget_set_touch_up_handler(dialog->button,
923 unlock_dialog_touch_up_handler);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200924
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500925 desktop_shell_set_lock_surface(desktop->shell,
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400926 window_get_wl_surface(dialog->window));
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500927
Pekka Paalanen40e49ac2012-01-18 16:51:30 +0200928 window_schedule_resize(dialog->window, 260, 230);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200929
930 return dialog;
931}
932
933static void
934unlock_dialog_destroy(struct unlock_dialog *dialog)
935{
936 window_destroy(dialog->window);
937 free(dialog);
938}
939
940static void
941unlock_dialog_finish(struct task *task, uint32_t events)
942{
943 struct desktop *desktop =
Benjamin Franzked7759712011-11-22 12:38:48 +0100944 container_of(task, struct desktop, unlock_task);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200945
946 desktop_shell_unlock(desktop->shell);
947 unlock_dialog_destroy(desktop->unlock_dialog);
948 desktop->unlock_dialog = NULL;
949}
950
951static void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400952desktop_shell_configure(void *data,
953 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400954 uint32_t edges,
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400955 struct wl_surface *surface,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400956 int32_t width, int32_t height)
957{
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400958 struct window *window = wl_surface_get_user_data(surface);
Pekka Paalanen068ae942011-11-28 14:11:15 +0200959 struct surface *s = window_get_user_data(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400960
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400961 s->configure(data, desktop_shell, edges, window, width, height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400962}
963
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200964static void
965desktop_shell_prepare_lock_surface(void *data,
966 struct desktop_shell *desktop_shell)
967{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200968 struct desktop *desktop = data;
969
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +0300970 if (!desktop->locking) {
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200971 desktop_shell_unlock(desktop->shell);
972 return;
973 }
974
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200975 if (!desktop->unlock_dialog) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500976 desktop->unlock_dialog = unlock_dialog_create(desktop);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200977 desktop->unlock_dialog->desktop = desktop;
978 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200979}
980
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300981static void
982desktop_shell_grab_cursor(void *data,
983 struct desktop_shell *desktop_shell,
984 uint32_t cursor)
985{
986 struct desktop *desktop = data;
987
988 switch (cursor) {
Philipp Brüschweiler16d59d72012-08-24 15:43:55 +0200989 case DESKTOP_SHELL_CURSOR_NONE:
990 desktop->grab_cursor = CURSOR_BLANK;
991 break;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300992 case DESKTOP_SHELL_CURSOR_BUSY:
993 desktop->grab_cursor = CURSOR_WATCH;
994 break;
995 case DESKTOP_SHELL_CURSOR_MOVE:
996 desktop->grab_cursor = CURSOR_DRAGGING;
997 break;
998 case DESKTOP_SHELL_CURSOR_RESIZE_TOP:
999 desktop->grab_cursor = CURSOR_TOP;
1000 break;
1001 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM:
1002 desktop->grab_cursor = CURSOR_BOTTOM;
1003 break;
1004 case DESKTOP_SHELL_CURSOR_RESIZE_LEFT:
1005 desktop->grab_cursor = CURSOR_LEFT;
1006 break;
1007 case DESKTOP_SHELL_CURSOR_RESIZE_RIGHT:
1008 desktop->grab_cursor = CURSOR_RIGHT;
1009 break;
1010 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_LEFT:
1011 desktop->grab_cursor = CURSOR_TOP_LEFT;
1012 break;
1013 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_RIGHT:
1014 desktop->grab_cursor = CURSOR_TOP_RIGHT;
1015 break;
1016 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_LEFT:
1017 desktop->grab_cursor = CURSOR_BOTTOM_LEFT;
1018 break;
1019 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_RIGHT:
1020 desktop->grab_cursor = CURSOR_BOTTOM_RIGHT;
1021 break;
1022 case DESKTOP_SHELL_CURSOR_ARROW:
1023 default:
1024 desktop->grab_cursor = CURSOR_LEFT_PTR;
1025 }
1026}
1027
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001028static const struct desktop_shell_listener listener = {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001029 desktop_shell_configure,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001030 desktop_shell_prepare_lock_surface,
1031 desktop_shell_grab_cursor
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001032};
1033
U. Artie Eoff44874d92012-10-02 21:12:35 -07001034static void
1035background_destroy(struct background *background)
1036{
1037 widget_destroy(background->widget);
1038 window_destroy(background->window);
1039
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001040 free(background->image);
U. Artie Eoff44874d92012-10-02 21:12:35 -07001041 free(background);
1042}
1043
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001044static struct background *
1045background_create(struct desktop *desktop)
1046{
1047 struct background *background;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001048 struct weston_config_section *s;
1049 char *type;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001050
Peter Huttererf3d62272013-08-08 11:57:05 +10001051 background = xzalloc(sizeof *background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001052 background->base.configure = background_configure;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001053 background->window = window_create_custom(desktop->display);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001054 background->widget = window_add_widget(background->window, background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001055 window_set_user_data(background->window, background);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001056 widget_set_redraw_handler(background->widget, background_draw);
Tomeu Vizosobee45a12013-08-06 20:05:54 +02001057 window_set_preferred_format(background->window,
1058 WINDOW_PREFERRED_FORMAT_RGB565);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001059
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001060 s = weston_config_get_section(desktop->config, "shell", NULL, NULL);
1061 weston_config_section_get_string(s, "background-image",
Kristian Høgsberg4c3661f2014-04-21 22:54:37 -07001062 &background->image, NULL);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001063 weston_config_section_get_uint(s, "background-color",
Kristian Høgsberg4c3661f2014-04-21 22:54:37 -07001064 &background->color, 0);
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001065
1066 weston_config_section_get_string(s, "background-type",
1067 &type, "tile");
U. Artie Eoff3c946772014-01-15 10:59:50 -08001068 if (type == NULL) {
1069 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
1070 exit(EXIT_FAILURE);
1071 }
1072
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001073 if (strcmp(type, "scale") == 0) {
1074 background->type = BACKGROUND_SCALE;
1075 } else if (strcmp(type, "scale-crop") == 0) {
1076 background->type = BACKGROUND_SCALE_CROP;
1077 } else if (strcmp(type, "tile") == 0) {
1078 background->type = BACKGROUND_TILE;
1079 } else {
1080 background->type = -1;
1081 fprintf(stderr, "invalid background-type: %s\n",
1082 type);
1083 }
1084
1085 free(type);
1086
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001087 return background;
1088}
1089
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001090static int
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001091grab_surface_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001092 float x, float y, void *data)
1093{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001094 struct desktop *desktop = data;
1095
1096 return desktop->grab_cursor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001097}
1098
1099static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001100grab_surface_destroy(struct desktop *desktop)
1101{
1102 widget_destroy(desktop->grab_widget);
1103 window_destroy(desktop->grab_window);
1104}
1105
1106static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001107grab_surface_create(struct desktop *desktop)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001108{
1109 struct wl_surface *s;
1110
Ander Conselvan de Oliveira07a91cd2012-07-16 14:15:50 +03001111 desktop->grab_window = window_create_custom(desktop->display);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001112 window_set_user_data(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001113
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001114 s = window_get_wl_surface(desktop->grab_window);
1115 desktop_shell_set_grab_surface(desktop->shell, s);
1116
1117 desktop->grab_widget =
1118 window_add_widget(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001119 /* We set the allocation to 1x1 at 0,0 so the fake enter event
1120 * at 0,0 will go to this widget. */
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001121 widget_set_allocation(desktop->grab_widget, 0, 0, 1, 1);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001122
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001123 widget_set_enter_handler(desktop->grab_widget,
1124 grab_surface_enter_handler);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001125}
1126
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001127static void
U. Artie Eoff44874d92012-10-02 21:12:35 -07001128output_destroy(struct output *output)
1129{
1130 background_destroy(output->background);
1131 panel_destroy(output->panel);
1132 wl_output_destroy(output->output);
1133 wl_list_remove(&output->link);
1134
1135 free(output);
1136}
1137
1138static void
1139desktop_destroy_outputs(struct desktop *desktop)
1140{
1141 struct output *tmp;
1142 struct output *output;
1143
1144 wl_list_for_each_safe(output, tmp, &desktop->outputs, link)
1145 output_destroy(output);
1146}
1147
1148static void
Alexander Larssonc584fa62013-05-22 14:41:32 +02001149output_handle_geometry(void *data,
1150 struct wl_output *wl_output,
1151 int x, int y,
1152 int physical_width,
1153 int physical_height,
1154 int subpixel,
1155 const char *make,
1156 const char *model,
1157 int transform)
1158{
1159 struct output *output = data;
1160
1161 window_set_buffer_transform(output->panel->window, transform);
1162 window_set_buffer_transform(output->background->window, transform);
1163}
1164
1165static void
1166output_handle_mode(void *data,
1167 struct wl_output *wl_output,
1168 uint32_t flags,
1169 int width,
1170 int height,
1171 int refresh)
1172{
1173}
1174
1175static void
1176output_handle_done(void *data,
1177 struct wl_output *wl_output)
1178{
1179}
1180
1181static void
1182output_handle_scale(void *data,
1183 struct wl_output *wl_output,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001184 int32_t scale)
Alexander Larssonc584fa62013-05-22 14:41:32 +02001185{
1186 struct output *output = data;
1187
1188 window_set_buffer_scale(output->panel->window, scale);
1189 window_set_buffer_scale(output->background->window, scale);
1190}
1191
1192static const struct wl_output_listener output_listener = {
1193 output_handle_geometry,
1194 output_handle_mode,
1195 output_handle_done,
1196 output_handle_scale
1197};
1198
1199static void
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001200output_init(struct output *output, struct desktop *desktop)
1201{
1202 struct wl_surface *surface;
1203
1204 output->panel = panel_create(desktop);
1205 surface = window_get_wl_surface(output->panel->window);
1206 desktop_shell_set_panel(desktop->shell,
1207 output->output, surface);
1208
1209 output->background = background_create(desktop);
1210 surface = window_get_wl_surface(output->background->window);
1211 desktop_shell_set_background(desktop->shell,
1212 output->output, surface);
1213}
1214
1215static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001216create_output(struct desktop *desktop, uint32_t id)
1217{
1218 struct output *output;
1219
1220 output = calloc(1, sizeof *output);
1221 if (!output)
1222 return;
1223
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001224 output->output =
Alexander Larssonc584fa62013-05-22 14:41:32 +02001225 display_bind(desktop->display, id, &wl_output_interface, 2);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001226 output->server_output_id = id;
Alexander Larssonc584fa62013-05-22 14:41:32 +02001227
1228 wl_output_add_listener(output->output, &output_listener, output);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001229
1230 wl_list_insert(&desktop->outputs, &output->link);
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001231
1232 /* On start up we may process an output global before the shell global
1233 * in which case we can't create the panel and background just yet */
1234 if (desktop->shell)
1235 output_init(output, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001236}
1237
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001238static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001239global_handler(struct display *display, uint32_t id,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001240 const char *interface, uint32_t version, void *data)
1241{
1242 struct desktop *desktop = data;
1243
1244 if (!strcmp(interface, "desktop_shell")) {
Pekka Paalanen79346ab2013-05-22 18:03:09 +03001245 desktop->interface_version = (version < 2) ? version : 2;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001246 desktop->shell = display_bind(desktop->display,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03001247 id, &desktop_shell_interface,
1248 desktop->interface_version);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001249 desktop_shell_add_listener(desktop->shell, &listener, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001250 } else if (!strcmp(interface, "wl_output")) {
1251 create_output(desktop, id);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001252 }
1253}
1254
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001255static void
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001256global_handler_remove(struct display *display, uint32_t id,
1257 const char *interface, uint32_t version, void *data)
1258{
1259 struct desktop *desktop = data;
1260 struct output *output;
1261
1262 if (!strcmp(interface, "wl_output")) {
1263 wl_list_for_each(output, &desktop->outputs, link) {
1264 if (output->server_output_id == id) {
1265 output_destroy(output);
1266 break;
1267 }
1268 }
1269 }
1270}
1271
1272static void
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001273panel_add_launchers(struct panel *panel, struct desktop *desktop)
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001274{
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001275 struct weston_config_section *s;
1276 char *icon, *path;
1277 const char *name;
1278 int count;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001279
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001280 count = 0;
1281 s = NULL;
1282 while (weston_config_next_section(desktop->config, &s, &name)) {
1283 if (strcmp(name, "launcher") != 0)
1284 continue;
1285
1286 weston_config_section_get_string(s, "icon", &icon, NULL);
1287 weston_config_section_get_string(s, "path", &path, NULL);
1288
1289 if (icon != NULL && path != NULL) {
1290 panel_add_launcher(panel, icon, path);
Rob Bradford09252d42013-07-26 16:29:45 +01001291 count++;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001292 } else {
1293 fprintf(stderr, "invalid launcher section\n");
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001294 }
1295
1296 free(icon);
1297 free(path);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001298 }
1299
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001300 if (count == 0) {
1301 /* add default launcher */
1302 panel_add_launcher(panel,
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001303 DATADIR "/weston/terminal.png",
Rodney Lorrimar99ff01b2012-02-29 17:31:03 +01001304 BINDIR "/weston-terminal");
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001305 }
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001306}
1307
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001308int main(int argc, char *argv[])
1309{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001310 struct desktop desktop = { 0 };
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001311 struct output *output;
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001312 struct weston_config_section *s;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001313
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001314 desktop.unlock_task.run = unlock_dialog_finish;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001315 wl_list_init(&desktop.outputs);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001316
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07001317 desktop.config = weston_config_parse("weston.ini");
Ander Conselvan de Oliveira6d75da72013-07-05 16:05:27 +03001318 s = weston_config_get_section(desktop.config, "shell", NULL, NULL);
1319 weston_config_section_get_bool(s, "locking", &desktop.locking, 1);
1320
Kristian Høgsberg4172f662013-02-20 15:27:49 -05001321 desktop.display = display_create(&argc, argv);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001322 if (desktop.display == NULL) {
1323 fprintf(stderr, "failed to create display: %m\n");
1324 return -1;
1325 }
1326
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001327 display_set_user_data(desktop.display, &desktop);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001328 display_set_global_handler(desktop.display, global_handler);
Xiong Zhang83d8ee72013-10-23 13:58:35 +08001329 display_set_global_handler_remove(desktop.display, global_handler_remove);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001330
Ander Conselvan de Oliveirae4925492013-07-05 16:05:28 +03001331 /* Create panel and background for outputs processed before the shell
1332 * global interface was processed */
1333 wl_list_for_each(output, &desktop.outputs, link)
1334 if (!output->panel)
1335 output_init(output, &desktop);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001336
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001337 grab_surface_create(&desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001338
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001339 signal(SIGCHLD, sigchild_handler);
1340
1341 display_run(desktop.display);
1342
U. Artie Eoff44874d92012-10-02 21:12:35 -07001343 /* Cleanup */
1344 grab_surface_destroy(&desktop);
1345 desktop_destroy_outputs(&desktop);
1346 if (desktop.unlock_dialog)
1347 unlock_dialog_destroy(desktop.unlock_dialog);
1348 desktop_shell_destroy(desktop.shell);
1349 display_destroy(desktop.display);
1350
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001351 return 0;
1352}