blob: 0605f844da0b5ef1ab221d73b7b305b0ebd80e0a [file] [log] [blame]
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001/*
2 * Copyright © 2011 Kristian Høgsberg
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02003 * Copyright © 2011 Collabora, Ltd.
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04004 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
24#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <fcntl.h>
29#include <unistd.h>
30#include <math.h>
31#include <cairo.h>
32#include <sys/wait.h>
Martin Minarik1e51a872012-06-08 00:39:11 +020033#include <sys/timerfd.h>
34#include <sys/epoll.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040035#include <linux/input.h>
Tiago Vignatti61500722012-05-23 22:06:28 +030036#include <libgen.h>
Martin Minarik1e51a872012-06-08 00:39:11 +020037#include <time.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040038
Pekka Paalanen50719bc2011-11-22 14:18:50 +020039#include <wayland-client.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040040#include "window.h"
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040041#include "../shared/cairo-util.h"
Kristian Høgsberg9b935c82011-12-08 12:44:27 -050042#include "../shared/config-parser.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040043
Pekka Paalanen50719bc2011-11-22 14:18:50 +020044#include "desktop-shell-client-protocol.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040045
46struct desktop {
47 struct display *display;
48 struct desktop_shell *shell;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +020049 struct unlock_dialog *unlock_dialog;
50 struct task unlock_task;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010051 struct wl_list outputs;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040052
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030053 struct window *grab_window;
54 struct widget *grab_widget;
55
56 enum desktop_shell_cursor grab_cursor;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010057};
58
59struct surface {
60 void (*configure)(void *data,
61 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040062 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010063 int32_t width, int32_t height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040064};
65
66struct panel {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010067 struct surface base;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040068 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050069 struct widget *widget;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050070 struct wl_list launcher_list;
Martin Minarik1e51a872012-06-08 00:39:11 +020071 struct panel_clock *clock;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040072};
73
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010074struct background {
75 struct surface base;
76 struct window *window;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050077 struct widget *widget;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010078};
79
80struct output {
81 struct wl_output *output;
82 struct wl_list link;
83
84 struct panel *panel;
85 struct background *background;
86};
87
Kristian Høgsberg53880802012-01-09 11:16:50 -050088struct panel_launcher {
Kristian Høgsbergc51f7992012-01-08 15:09:53 -050089 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -040090 struct panel *panel;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040091 cairo_surface_t *icon;
Kristian Høgsbergb6323512012-01-11 00:04:42 -050092 int focused, pressed;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040093 const char *path;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050094 struct wl_list link;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040095};
96
Martin Minarik1e51a872012-06-08 00:39:11 +020097struct panel_clock {
98 struct widget *widget;
99 struct panel *panel;
Martin Minarik1e51a872012-06-08 00:39:11 +0200100 struct task clock_task;
101 int clock_fd;
102};
103
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200104struct unlock_dialog {
105 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500106 struct widget *widget;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500107 struct widget *button;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500108 int button_focused;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200109 int closing;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200110 struct desktop *desktop;
111};
112
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500113static char *key_background_image = DATADIR "/weston/pattern.png";
114static char *key_background_type = "tile";
115static uint32_t key_panel_color = 0xaa000000;
116static uint32_t key_background_color = 0xff002244;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500117static char *key_launcher_icon;
118static char *key_launcher_path;
119static void launcher_section_done(void *data);
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200120static int key_locking = 1;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500121
122static const struct config_key shell_config_keys[] = {
123 { "background-image", CONFIG_KEY_STRING, &key_background_image },
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500124 { "background-type", CONFIG_KEY_STRING, &key_background_type },
Scott Moreaufa1de692012-01-27 13:25:49 -0700125 { "panel-color", CONFIG_KEY_UNSIGNED_INTEGER, &key_panel_color },
126 { "background-color", CONFIG_KEY_UNSIGNED_INTEGER, &key_background_color },
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200127 { "locking", CONFIG_KEY_BOOLEAN, &key_locking },
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500128};
129
130static const struct config_key launcher_config_keys[] = {
131 { "icon", CONFIG_KEY_STRING, &key_launcher_icon },
132 { "path", CONFIG_KEY_STRING, &key_launcher_path },
133};
134
135static const struct config_section config_sections[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200136 { "shell",
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500137 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
138 { "launcher",
139 launcher_config_keys, ARRAY_LENGTH(launcher_config_keys),
140 launcher_section_done }
141};
142
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400143static void
144sigchild_handler(int s)
145{
146 int status;
147 pid_t pid;
148
149 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
150 fprintf(stderr, "child %d exited\n", pid);
151}
152
153static void
Pekka Paalanen9e30a822012-01-19 16:40:28 +0200154menu_func(struct window *window, int index, void *data)
155{
156 printf("Selected index %d from a panel menu.\n", index);
157}
158
159static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500160show_menu(struct panel *panel, struct input *input, uint32_t time)
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400161{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500162 int32_t x, y;
163 static const char *entries[] = {
164 "Roy", "Pris", "Leon", "Zhora"
165 };
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400166
167 input_get_position(input, &x, &y);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +0200168 window_show_menu(window_get_display(panel->window),
169 input, time, panel->window,
Pekka Paalanen9e30a822012-01-19 16:40:28 +0200170 x - 10, y - 10, menu_func, entries, 4);
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400171}
172
173static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500174panel_launcher_activate(struct panel_launcher *widget)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400175{
176 pid_t pid;
177
178 pid = fork();
179 if (pid < 0) {
180 fprintf(stderr, "fork failed: %m\n");
181 return;
182 }
183
184 if (pid)
185 return;
Benjamin Franzked7759712011-11-22 12:38:48 +0100186
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500187 if (execl(widget->path, widget->path, NULL) < 0) {
Pekka Paalanena291ae52012-01-27 09:50:02 +0200188 fprintf(stderr, "execl '%s' failed: %m\n", widget->path);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400189 exit(1);
190 }
191}
192
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400193static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500194panel_launcher_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400195{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500196 struct panel_launcher *launcher = data;
197 cairo_surface_t *surface;
198 struct rectangle allocation;
199 cairo_t *cr;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400200
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500201 surface = window_get_surface(launcher->panel->window);
202 cr = cairo_create(surface);
203
204 widget_get_allocation(widget, &allocation);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500205 if (launcher->pressed) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500206 allocation.x++;
207 allocation.y++;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400208 }
209
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500210 cairo_set_source_surface(cr, launcher->icon,
211 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400212 cairo_paint(cr);
213
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500214 if (launcher->focused) {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400215 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500216 cairo_mask_surface(cr, launcher->icon,
217 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400218 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400219
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500220 cairo_destroy(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400221}
222
Tiago Vignatti61500722012-05-23 22:06:28 +0300223static int
224panel_launcher_motion_handler(struct widget *widget, struct input *input,
225 uint32_t time, float x, float y, void *data)
226{
227 struct panel_launcher *launcher = data;
228
229 widget_set_tooltip(widget, basename((char *)launcher->path), x, y);
230
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300231 return CURSOR_LEFT_PTR;
Tiago Vignatti61500722012-05-23 22:06:28 +0300232}
233
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400234static void
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500235set_hex_color(cairo_t *cr, uint32_t color)
236{
237 cairo_set_source_rgba(cr,
238 ((color >> 16) & 0xff) / 255.0,
239 ((color >> 8) & 0xff) / 255.0,
240 ((color >> 0) & 0xff) / 255.0,
241 ((color >> 24) & 0xff) / 255.0);
242}
243
244static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500245panel_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400246{
247 cairo_surface_t *surface;
248 cairo_t *cr;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500249 struct panel *panel = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400250
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500251 surface = window_get_surface(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400252 cr = cairo_create(surface);
253 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500254 set_hex_color(cr, key_panel_color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400255 cairo_paint(cr);
256
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400257 cairo_destroy(cr);
258 cairo_surface_destroy(surface);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400259}
260
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500261static int
Kristian Høgsberg53880802012-01-09 11:16:50 -0500262panel_launcher_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400263 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500264{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500265 struct panel_launcher *launcher = data;
266
267 launcher->focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500268 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500269
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300270 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500271}
272
273static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500274panel_launcher_leave_handler(struct widget *widget,
275 struct input *input, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400276{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500277 struct panel_launcher *launcher = data;
278
279 launcher->focused = 0;
Tiago Vignatti61500722012-05-23 22:06:28 +0300280 widget_destroy_tooltip(widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500281 widget_schedule_redraw(widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400282}
283
284static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500285panel_launcher_button_handler(struct widget *widget,
286 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100287 uint32_t button,
288 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400289{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500290 struct panel_launcher *launcher;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400291
Kristian Høgsberg53880802012-01-09 11:16:50 -0500292 launcher = widget_get_user_data(widget);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500293 widget_schedule_redraw(widget);
Daniel Stone4dbadb12012-05-30 16:31:51 +0100294 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg53880802012-01-09 11:16:50 -0500295 panel_launcher_activate(launcher);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500296}
297
Martin Minarik1e51a872012-06-08 00:39:11 +0200298static void
299clock_func(struct task *task, uint32_t events)
300{
301 struct panel_clock *clock =
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400302 container_of(task, struct panel_clock, clock_task);
Kristian Høgsberg70226bb2012-06-08 16:54:52 -0400303 uint64_t exp;
Martin Minarik1e51a872012-06-08 00:39:11 +0200304
Kristian Høgsberg70226bb2012-06-08 16:54:52 -0400305 read(clock->clock_fd, &exp, sizeof exp);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400306 widget_schedule_redraw(clock->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200307}
308
309static void
310panel_clock_redraw_handler(struct widget *widget, void *data)
311{
312 cairo_surface_t *surface;
313 struct panel_clock *clock = data;
314 cairo_t *cr;
315 struct rectangle allocation;
316 cairo_text_extents_t extents;
317 cairo_font_extents_t font_extents;
Martin Minarik1e51a872012-06-08 00:39:11 +0200318 time_t rawtime;
319 struct tm * timeinfo;
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400320 char string[128];
Martin Minarik1e51a872012-06-08 00:39:11 +0200321
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400322 time(&rawtime);
323 timeinfo = localtime(&rawtime);
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400324 strftime(string, sizeof string, "%a %b %d, %I:%M %p", timeinfo);
Martin Minarik1e51a872012-06-08 00:39:11 +0200325
326 widget_get_allocation(widget, &allocation);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400327 if (allocation.width == 0)
328 return;
Martin Minarik1e51a872012-06-08 00:39:11 +0200329
330 surface = window_get_surface(clock->panel->window);
331 cr = cairo_create(surface);
Martin Minarik1e51a872012-06-08 00:39:11 +0200332 cairo_select_font_face(cr, "sans",
333 CAIRO_FONT_SLANT_NORMAL,
334 CAIRO_FONT_WEIGHT_NORMAL);
335 cairo_set_font_size(cr, 14);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400336 cairo_text_extents(cr, string, &extents);
Martin Minarik1e51a872012-06-08 00:39:11 +0200337 cairo_font_extents (cr, &font_extents);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400338 cairo_move_to(cr, allocation.x + 5,
339 allocation.y + 3 * (allocation.height >> 2) + 1);
340 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400341 cairo_show_text(cr, string);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400342 cairo_move_to(cr, allocation.x + 4,
343 allocation.y + 3 * (allocation.height >> 2));
344 cairo_set_source_rgb(cr, 1, 1, 1);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400345 cairo_show_text(cr, string);
Martin Minarik1e51a872012-06-08 00:39:11 +0200346 cairo_destroy(cr);
347}
348
349static int
350clock_timer_reset(struct panel_clock *clock)
351{
352 struct itimerspec its;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400353
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400354 its.it_interval.tv_sec = 60;
Martin Minarik1e51a872012-06-08 00:39:11 +0200355 its.it_interval.tv_nsec = 0;
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400356 its.it_value.tv_sec = 60;
Martin Minarik1e51a872012-06-08 00:39:11 +0200357 its.it_value.tv_nsec = 0;
358 if (timerfd_settime(clock->clock_fd, 0, &its, NULL) < 0) {
359 fprintf(stderr, "could not set timerfd\n: %m");
360 return -1;
361 }
362
363 return 0;
364}
365
366static void
367panel_add_clock(struct panel *panel)
368{
369 struct panel_clock *clock;
370 int timerfd;
371
372 timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
373 if (timerfd < 0) {
374 fprintf(stderr, "could not create timerfd\n: %m");
375 return;
376 }
377
378 clock = malloc(sizeof *clock);
379 memset(clock, 0, sizeof *clock);
380 clock->panel = panel;
381 panel->clock = clock;
382 clock->clock_fd = timerfd;
383
384 clock->clock_task.run = clock_func;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400385 display_watch_fd(window_get_display(panel->window), clock->clock_fd,
386 EPOLLIN, &clock->clock_task);
Martin Minarik1e51a872012-06-08 00:39:11 +0200387 clock_timer_reset(clock);
388
389 clock->widget = widget_add_widget(panel->widget, clock);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400390 widget_set_redraw_handler(clock->widget, panel_clock_redraw_handler);
Martin Minarik1e51a872012-06-08 00:39:11 +0200391}
392
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500393static void
394panel_button_handler(struct widget *widget,
395 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100396 uint32_t button,
397 enum wl_pointer_button_state state, void *data)
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500398{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500399 struct panel *panel = data;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500400
Daniel Stone4dbadb12012-05-30 16:31:51 +0100401 if (button == BTN_RIGHT && state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500402 show_menu(panel, input, time);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400403}
404
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100405static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500406panel_resize_handler(struct widget *widget,
407 int32_t width, int32_t height, void *data)
408{
409 struct panel_launcher *launcher;
410 struct panel *panel = data;
411 int x, y, w, h;
412
413 x = 10;
414 y = 16;
415 wl_list_for_each(launcher, &panel->launcher_list, link) {
416 w = cairo_image_surface_get_width(launcher->icon);
417 h = cairo_image_surface_get_height(launcher->icon);
418 widget_set_allocation(launcher->widget,
419 x, y - h / 2, w + 1, h + 1);
420 x += w + 10;
421 }
Martin Minarik1e51a872012-06-08 00:39:11 +0200422 h=20;
423 w=170;
Pekka Paalanen01b17252012-06-12 17:42:26 +0300424
425 if (panel->clock)
426 widget_set_allocation(panel->clock->widget,
427 width - w - 8, y - h / 2, w + 1, h + 1);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500428}
429
430static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100431panel_configure(void *data,
432 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400433 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100434 int32_t width, int32_t height)
435{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500436 struct surface *surface = window_get_user_data(window);
437 struct panel *panel = container_of(surface, struct panel, base);
438
439 window_schedule_resize(panel->window, width, 32);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100440}
441
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400442static struct panel *
443panel_create(struct display *display)
444{
445 struct panel *panel;
446
447 panel = malloc(sizeof *panel);
448 memset(panel, 0, sizeof *panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400449
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100450 panel->base.configure = panel_configure;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400451 panel->window = window_create_custom(display);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500452 panel->widget = window_add_widget(panel->window, panel);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500453 wl_list_init(&panel->launcher_list);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400454
455 window_set_title(panel->window, "panel");
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400456 window_set_user_data(panel->window, panel);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500457
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500458 widget_set_redraw_handler(panel->widget, panel_redraw_handler);
459 widget_set_resize_handler(panel->widget, panel_resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500460 widget_set_button_handler(panel->widget, panel_button_handler);
Rafal Mielniczukb9e513c2012-06-09 20:33:29 +0200461
462 panel_add_clock(panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400463
464 return panel;
465}
466
467static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500468panel_add_launcher(struct panel *panel, const char *icon, const char *path)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400469{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500470 struct panel_launcher *launcher;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400471
Kristian Høgsberg53880802012-01-09 11:16:50 -0500472 launcher = malloc(sizeof *launcher);
473 memset(launcher, 0, sizeof *launcher);
474 launcher->icon = cairo_image_surface_create_from_png(icon);
475 launcher->path = strdup(path);
476 launcher->panel = panel;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500477 wl_list_insert(panel->launcher_list.prev, &launcher->link);
478
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500479 launcher->widget = widget_add_widget(panel->widget, launcher);
Kristian Høgsberg53880802012-01-09 11:16:50 -0500480 widget_set_enter_handler(launcher->widget,
481 panel_launcher_enter_handler);
482 widget_set_leave_handler(launcher->widget,
483 panel_launcher_leave_handler);
484 widget_set_button_handler(launcher->widget,
485 panel_launcher_button_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500486 widget_set_redraw_handler(launcher->widget,
487 panel_launcher_redraw_handler);
Tiago Vignatti61500722012-05-23 22:06:28 +0300488 widget_set_motion_handler(launcher->widget,
489 panel_launcher_motion_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400490}
491
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500492enum {
493 BACKGROUND_SCALE,
494 BACKGROUND_TILE
495};
496
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400497static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500498background_draw(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400499{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500500 struct background *background = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400501 cairo_surface_t *surface, *image;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400502 cairo_pattern_t *pattern;
503 cairo_matrix_t matrix;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400504 cairo_t *cr;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400505 double sx, sy;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500506 struct rectangle allocation;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500507 int type = -1;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400508
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500509 surface = window_get_surface(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400510
511 cr = cairo_create(surface);
512 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
513 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
514 cairo_paint(cr);
515
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500516 widget_get_allocation(widget, &allocation);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500517 image = NULL;
518 if (key_background_image)
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400519 image = load_cairo_surface(key_background_image);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500520
521 if (strcmp(key_background_type, "scale") == 0)
522 type = BACKGROUND_SCALE;
523 else if (strcmp(key_background_type, "tile") == 0)
524 type = BACKGROUND_TILE;
525 else
526 fprintf(stderr, "invalid background-type: %s\n",
527 key_background_type);
528
529 if (image && type != -1) {
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400530 pattern = cairo_pattern_create_for_surface(image);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500531 switch (type) {
532 case BACKGROUND_SCALE:
533 sx = (double) cairo_image_surface_get_width(image) /
534 allocation.width;
535 sy = (double) cairo_image_surface_get_height(image) /
536 allocation.height;
537 cairo_matrix_init_scale(&matrix, sx, sy);
538 cairo_pattern_set_matrix(pattern, &matrix);
539 break;
540 case BACKGROUND_TILE:
541 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
542 break;
543 }
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400544 cairo_set_source(cr, pattern);
545 cairo_pattern_destroy (pattern);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400546 cairo_surface_destroy(image);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500547 } else {
548 set_hex_color(cr, key_background_color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400549 }
550
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500551 cairo_paint(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400552 cairo_destroy(cr);
553 cairo_surface_destroy(surface);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400554}
555
556static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100557background_configure(void *data,
558 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400559 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100560 int32_t width, int32_t height)
561{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500562 struct background *background =
563 (struct background *) window_get_user_data(window);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100564
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500565 widget_schedule_resize(background->widget, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100566}
567
568static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500569unlock_dialog_redraw_handler(struct widget *widget, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200570{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500571 struct unlock_dialog *dialog = data;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200572 struct rectangle allocation;
573 cairo_t *cr;
574 cairo_surface_t *surface;
575 cairo_pattern_t *pat;
576 double cx, cy, r, f;
577
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200578 surface = window_get_surface(dialog->window);
579 cr = cairo_create(surface);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500580
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500581 widget_get_allocation(dialog->widget, &allocation);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200582 cairo_rectangle(cr, allocation.x, allocation.y,
583 allocation.width, allocation.height);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200584 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
585 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500586 cairo_fill(cr);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200587
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500588 cairo_translate(cr, allocation.x, allocation.y);
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500589 if (dialog->button_focused)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200590 f = 1.0;
591 else
592 f = 0.7;
593
594 cx = allocation.width / 2.0;
595 cy = allocation.height / 2.0;
596 r = (cx < cy ? cx : cy) * 0.4;
597 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
598 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
599 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
600 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
601 cairo_set_source(cr, pat);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500602 cairo_pattern_destroy(pat);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200603 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
604 cairo_fill(cr);
605
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500606 widget_set_allocation(dialog->button,
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500607 allocation.x + cx - r,
608 allocation.y + cy - r, 2 * r, 2 * r);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200609
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200610 cairo_destroy(cr);
611
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200612 cairo_surface_destroy(surface);
613}
614
615static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500616unlock_dialog_button_handler(struct widget *widget,
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200617 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100618 uint32_t button,
619 enum wl_pointer_button_state state, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200620{
621 struct unlock_dialog *dialog = data;
622 struct desktop *desktop = dialog->desktop;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200623
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500624 if (button == BTN_LEFT) {
Daniel Stone4dbadb12012-05-30 16:31:51 +0100625 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
626 !dialog->closing) {
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200627 display_defer(desktop->display, &desktop->unlock_task);
628 dialog->closing = 1;
629 }
630 }
631}
632
633static void
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200634unlock_dialog_keyboard_focus_handler(struct window *window,
635 struct input *device, void *data)
636{
637 window_schedule_redraw(window);
638}
639
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500640static int
Kristian Høgsbergee143232012-01-09 08:42:24 -0500641unlock_dialog_widget_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400642 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400643 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500644{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500645 struct unlock_dialog *dialog = data;
646
647 dialog->button_focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500648 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500649
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300650 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500651}
652
653static void
654unlock_dialog_widget_leave_handler(struct widget *widget,
655 struct input *input, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200656{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500657 struct unlock_dialog *dialog = data;
658
659 dialog->button_focused = 0;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500660 widget_schedule_redraw(widget);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200661}
662
663static struct unlock_dialog *
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500664unlock_dialog_create(struct desktop *desktop)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200665{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500666 struct display *display = desktop->display;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200667 struct unlock_dialog *dialog;
668
669 dialog = malloc(sizeof *dialog);
670 if (!dialog)
671 return NULL;
672 memset(dialog, 0, sizeof *dialog);
673
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400674 dialog->window = window_create_custom(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500675 dialog->widget = frame_create(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200676 window_set_title(dialog->window, "Unlock your desktop");
677
678 window_set_user_data(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200679 window_set_keyboard_focus_handler(dialog->window,
680 unlock_dialog_keyboard_focus_handler);
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500681 dialog->button = widget_add_widget(dialog->widget, dialog);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500682 widget_set_redraw_handler(dialog->widget,
683 unlock_dialog_redraw_handler);
Kristian Høgsbergee143232012-01-09 08:42:24 -0500684 widget_set_enter_handler(dialog->button,
685 unlock_dialog_widget_enter_handler);
686 widget_set_leave_handler(dialog->button,
687 unlock_dialog_widget_leave_handler);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500688 widget_set_button_handler(dialog->button,
689 unlock_dialog_button_handler);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200690
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500691 desktop_shell_set_lock_surface(desktop->shell,
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400692 window_get_wl_surface(dialog->window));
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500693
Pekka Paalanen40e49ac2012-01-18 16:51:30 +0200694 window_schedule_resize(dialog->window, 260, 230);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200695
696 return dialog;
697}
698
699static void
700unlock_dialog_destroy(struct unlock_dialog *dialog)
701{
702 window_destroy(dialog->window);
703 free(dialog);
704}
705
706static void
707unlock_dialog_finish(struct task *task, uint32_t events)
708{
709 struct desktop *desktop =
Benjamin Franzked7759712011-11-22 12:38:48 +0100710 container_of(task, struct desktop, unlock_task);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200711
712 desktop_shell_unlock(desktop->shell);
713 unlock_dialog_destroy(desktop->unlock_dialog);
714 desktop->unlock_dialog = NULL;
715}
716
717static void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400718desktop_shell_configure(void *data,
719 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400720 uint32_t edges,
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400721 struct wl_surface *surface,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400722 int32_t width, int32_t height)
723{
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400724 struct window *window = wl_surface_get_user_data(surface);
Pekka Paalanen068ae942011-11-28 14:11:15 +0200725 struct surface *s = window_get_user_data(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400726
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400727 s->configure(data, desktop_shell, edges, window, width, height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400728}
729
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200730static void
731desktop_shell_prepare_lock_surface(void *data,
732 struct desktop_shell *desktop_shell)
733{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200734 struct desktop *desktop = data;
735
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200736 if (!key_locking) {
737 desktop_shell_unlock(desktop->shell);
738 return;
739 }
740
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200741 if (!desktop->unlock_dialog) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500742 desktop->unlock_dialog = unlock_dialog_create(desktop);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200743 desktop->unlock_dialog->desktop = desktop;
744 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200745}
746
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300747static void
748desktop_shell_grab_cursor(void *data,
749 struct desktop_shell *desktop_shell,
750 uint32_t cursor)
751{
752 struct desktop *desktop = data;
753
754 switch (cursor) {
755 case DESKTOP_SHELL_CURSOR_BUSY:
756 desktop->grab_cursor = CURSOR_WATCH;
757 break;
758 case DESKTOP_SHELL_CURSOR_MOVE:
759 desktop->grab_cursor = CURSOR_DRAGGING;
760 break;
761 case DESKTOP_SHELL_CURSOR_RESIZE_TOP:
762 desktop->grab_cursor = CURSOR_TOP;
763 break;
764 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM:
765 desktop->grab_cursor = CURSOR_BOTTOM;
766 break;
767 case DESKTOP_SHELL_CURSOR_RESIZE_LEFT:
768 desktop->grab_cursor = CURSOR_LEFT;
769 break;
770 case DESKTOP_SHELL_CURSOR_RESIZE_RIGHT:
771 desktop->grab_cursor = CURSOR_RIGHT;
772 break;
773 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_LEFT:
774 desktop->grab_cursor = CURSOR_TOP_LEFT;
775 break;
776 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_RIGHT:
777 desktop->grab_cursor = CURSOR_TOP_RIGHT;
778 break;
779 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_LEFT:
780 desktop->grab_cursor = CURSOR_BOTTOM_LEFT;
781 break;
782 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_RIGHT:
783 desktop->grab_cursor = CURSOR_BOTTOM_RIGHT;
784 break;
785 case DESKTOP_SHELL_CURSOR_ARROW:
786 default:
787 desktop->grab_cursor = CURSOR_LEFT_PTR;
788 }
789}
790
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400791static const struct desktop_shell_listener listener = {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200792 desktop_shell_configure,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300793 desktop_shell_prepare_lock_surface,
794 desktop_shell_grab_cursor
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400795};
796
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100797static struct background *
798background_create(struct desktop *desktop)
799{
800 struct background *background;
801
802 background = malloc(sizeof *background);
803 memset(background, 0, sizeof *background);
804
805 background->base.configure = background_configure;
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400806 background->window = window_create_custom(desktop->display);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500807 background->widget = window_add_widget(background->window, background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100808 window_set_user_data(background->window, background);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500809 widget_set_redraw_handler(background->widget, background_draw);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100810
811 return background;
812}
813
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400814static int
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300815grab_surface_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400816 float x, float y, void *data)
817{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300818 struct desktop *desktop = data;
819
820 return desktop->grab_cursor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400821}
822
823static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300824grab_surface_create(struct desktop *desktop)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400825{
826 struct wl_surface *s;
827
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300828 desktop->grab_window = window_create(desktop->display);
829 window_set_user_data(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400830
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300831 s = window_get_wl_surface(desktop->grab_window);
832 desktop_shell_set_grab_surface(desktop->shell, s);
833
834 desktop->grab_widget =
835 window_add_widget(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400836 /* We set the allocation to 1x1 at 0,0 so the fake enter event
837 * at 0,0 will go to this widget. */
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300838 widget_set_allocation(desktop->grab_widget, 0, 0, 1, 1);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400839
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300840 widget_set_enter_handler(desktop->grab_widget,
841 grab_surface_enter_handler);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400842}
843
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100844static void
845create_output(struct desktop *desktop, uint32_t id)
846{
847 struct output *output;
848
849 output = calloc(1, sizeof *output);
850 if (!output)
851 return;
852
853 output->output = wl_display_bind(display_get_display(desktop->display),
854 id, &wl_output_interface);
855
856 wl_list_insert(&desktop->outputs, &output->link);
857}
858
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400859static void
860global_handler(struct wl_display *display, uint32_t id,
861 const char *interface, uint32_t version, void *data)
862{
863 struct desktop *desktop = data;
864
865 if (!strcmp(interface, "desktop_shell")) {
866 desktop->shell =
867 wl_display_bind(display, id, &desktop_shell_interface);
868 desktop_shell_add_listener(desktop->shell, &listener, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100869 } else if (!strcmp(interface, "wl_output")) {
870 create_output(desktop, id);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400871 }
872}
873
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500874static void
875launcher_section_done(void *data)
876{
877 struct desktop *desktop = data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100878 struct output *output;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500879
880 if (key_launcher_icon == NULL || key_launcher_path == NULL) {
881 fprintf(stderr, "invalid launcher section\n");
882 return;
883 }
884
Martin Minarik1e51a872012-06-08 00:39:11 +0200885 wl_list_for_each(output, &desktop->outputs, link) {
Kristian Høgsberg53880802012-01-09 11:16:50 -0500886 panel_add_launcher(output->panel,
887 key_launcher_icon, key_launcher_path);
Martin Minarik1e51a872012-06-08 00:39:11 +0200888 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100889
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500890 free(key_launcher_icon);
891 key_launcher_icon = NULL;
892 free(key_launcher_path);
893 key_launcher_path = NULL;
894}
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400895
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500896static void
897add_default_launcher(struct desktop *desktop)
898{
899 struct output *output;
900
901 wl_list_for_each(output, &desktop->outputs, link)
902 panel_add_launcher(output->panel,
903 DATADIR "/weston/terminal.png",
Rodney Lorrimar99ff01b2012-02-29 17:31:03 +0100904 BINDIR "/weston-terminal");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500905}
906
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400907int main(int argc, char *argv[])
908{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200909 struct desktop desktop = { 0 };
Pekka Paalanen668dd562011-11-15 11:45:40 +0200910 char *config_file;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100911 struct output *output;
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500912 int ret;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400913
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200914 desktop.unlock_task.run = unlock_dialog_finish;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100915 wl_list_init(&desktop.outputs);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200916
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400917 desktop.display = display_create(argc, argv);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400918 if (desktop.display == NULL) {
919 fprintf(stderr, "failed to create display: %m\n");
920 return -1;
921 }
922
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400923 display_set_user_data(desktop.display, &desktop);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400924 wl_display_add_global_listener(display_get_display(desktop.display),
925 global_handler, &desktop);
926
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100927 wl_list_for_each(output, &desktop.outputs, link) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400928 struct wl_surface *surface;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100929
930 output->panel = panel_create(desktop.display);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400931 surface = window_get_wl_surface(output->panel->window);
932 desktop_shell_set_panel(desktop.shell,
933 output->output, surface);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100934
935 output->background = background_create(&desktop);
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400936 surface = window_get_wl_surface(output->background->window);
937 desktop_shell_set_background(desktop.shell,
938 output->output, surface);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100939 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400940
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300941 grab_surface_create(&desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400942
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200943 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500944 ret = parse_config_file(config_file,
945 config_sections, ARRAY_LENGTH(config_sections),
946 &desktop);
Pekka Paalanen668dd562011-11-15 11:45:40 +0200947 free(config_file);
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500948 if (ret < 0)
949 add_default_launcher(&desktop);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500950
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400951 signal(SIGCHLD, sigchild_handler);
952
953 display_run(desktop.display);
954
955 return 0;
956}