blob: 588dc1cd2125dd9763a15996ce1ff3f585f99afe [file] [log] [blame]
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001/*
2 * Copyright © 2011 Kristian Høgsberg
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02003 * Copyright © 2011 Collabora, Ltd.
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04004 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
24#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <fcntl.h>
29#include <unistd.h>
30#include <math.h>
31#include <cairo.h>
32#include <sys/wait.h>
Martin Minarik1e51a872012-06-08 00:39:11 +020033#include <sys/timerfd.h>
34#include <sys/epoll.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040035#include <linux/input.h>
Tiago Vignatti61500722012-05-23 22:06:28 +030036#include <libgen.h>
Kristian Høgsbergd1936b92012-07-23 22:59:33 -040037#include <ctype.h>
Martin Minarik1e51a872012-06-08 00:39:11 +020038#include <time.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040039
Pekka Paalanen50719bc2011-11-22 14:18:50 +020040#include <wayland-client.h>
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040041#include "window.h"
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040042#include "../shared/cairo-util.h"
Kristian Høgsberg9b935c82011-12-08 12:44:27 -050043#include "../shared/config-parser.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040044
Pekka Paalanen50719bc2011-11-22 14:18:50 +020045#include "desktop-shell-client-protocol.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040046
Pekka Paalanenb6df4f72012-08-03 14:39:15 +030047extern char **environ; /* defined by libc */
48
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040049struct desktop {
50 struct display *display;
51 struct desktop_shell *shell;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +020052 struct unlock_dialog *unlock_dialog;
53 struct task unlock_task;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010054 struct wl_list outputs;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -040055
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +030056 struct window *grab_window;
57 struct widget *grab_widget;
58
Scott Moreauec116022012-07-22 18:23:52 -060059 enum cursor_type grab_cursor;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010060};
61
62struct surface {
63 void (*configure)(void *data,
64 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -040065 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010066 int32_t width, int32_t height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040067};
68
69struct panel {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010070 struct surface base;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040071 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050072 struct widget *widget;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050073 struct wl_list launcher_list;
Martin Minarik1e51a872012-06-08 00:39:11 +020074 struct panel_clock *clock;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040075};
76
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010077struct background {
78 struct surface base;
79 struct window *window;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -050080 struct widget *widget;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010081};
82
83struct output {
84 struct wl_output *output;
85 struct wl_list link;
86
87 struct panel *panel;
88 struct background *background;
89};
90
Kristian Høgsberg53880802012-01-09 11:16:50 -050091struct panel_launcher {
Kristian Høgsbergc51f7992012-01-08 15:09:53 -050092 struct widget *widget;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -040093 struct panel *panel;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040094 cairo_surface_t *icon;
Kristian Høgsbergb6323512012-01-11 00:04:42 -050095 int focused, pressed;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -040096 char *path;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050097 struct wl_list link;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -040098 struct wl_array envp;
99 struct wl_array argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400100};
101
Martin Minarik1e51a872012-06-08 00:39:11 +0200102struct panel_clock {
103 struct widget *widget;
104 struct panel *panel;
Martin Minarik1e51a872012-06-08 00:39:11 +0200105 struct task clock_task;
106 int clock_fd;
107};
108
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200109struct unlock_dialog {
110 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500111 struct widget *widget;
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500112 struct widget *button;
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500113 int button_focused;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200114 int closing;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200115 struct desktop *desktop;
116};
117
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500118static char *key_background_image = DATADIR "/weston/pattern.png";
119static char *key_background_type = "tile";
120static uint32_t key_panel_color = 0xaa000000;
121static uint32_t key_background_color = 0xff002244;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500122static char *key_launcher_icon;
123static char *key_launcher_path;
124static void launcher_section_done(void *data);
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200125static int key_locking = 1;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500126
127static const struct config_key shell_config_keys[] = {
128 { "background-image", CONFIG_KEY_STRING, &key_background_image },
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500129 { "background-type", CONFIG_KEY_STRING, &key_background_type },
Scott Moreaufa1de692012-01-27 13:25:49 -0700130 { "panel-color", CONFIG_KEY_UNSIGNED_INTEGER, &key_panel_color },
131 { "background-color", CONFIG_KEY_UNSIGNED_INTEGER, &key_background_color },
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200132 { "locking", CONFIG_KEY_BOOLEAN, &key_locking },
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500133};
134
135static const struct config_key launcher_config_keys[] = {
136 { "icon", CONFIG_KEY_STRING, &key_launcher_icon },
137 { "path", CONFIG_KEY_STRING, &key_launcher_path },
138};
139
140static const struct config_section config_sections[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200141 { "shell",
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500142 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
143 { "launcher",
144 launcher_config_keys, ARRAY_LENGTH(launcher_config_keys),
145 launcher_section_done }
146};
147
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400148static void
149sigchild_handler(int s)
150{
151 int status;
152 pid_t pid;
153
154 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
155 fprintf(stderr, "child %d exited\n", pid);
156}
157
158static void
Pekka Paalanen9e30a822012-01-19 16:40:28 +0200159menu_func(struct window *window, int index, void *data)
160{
161 printf("Selected index %d from a panel menu.\n", index);
162}
163
164static void
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500165show_menu(struct panel *panel, struct input *input, uint32_t time)
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400166{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500167 int32_t x, y;
168 static const char *entries[] = {
169 "Roy", "Pris", "Leon", "Zhora"
170 };
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400171
172 input_get_position(input, &x, &y);
Pekka Paalanen6d174cf2012-01-19 15:17:59 +0200173 window_show_menu(window_get_display(panel->window),
174 input, time, panel->window,
Pekka Paalanen9e30a822012-01-19 16:40:28 +0200175 x - 10, y - 10, menu_func, entries, 4);
Kristian Høgsbergbcee9a42011-10-12 00:36:16 -0400176}
177
178static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500179panel_launcher_activate(struct panel_launcher *widget)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400180{
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400181 char **argv;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400182 pid_t pid;
183
184 pid = fork();
185 if (pid < 0) {
186 fprintf(stderr, "fork failed: %m\n");
187 return;
188 }
189
190 if (pid)
191 return;
Benjamin Franzked7759712011-11-22 12:38:48 +0100192
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400193 argv = widget->argv.data;
194 if (execve(argv[0], argv, widget->envp.data) < 0) {
195 fprintf(stderr, "execl '%s' failed: %m\n", argv[0]);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400196 exit(1);
197 }
198}
199
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400200static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500201panel_launcher_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400202{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500203 struct panel_launcher *launcher = data;
204 cairo_surface_t *surface;
205 struct rectangle allocation;
206 cairo_t *cr;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400207
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500208 surface = window_get_surface(launcher->panel->window);
209 cr = cairo_create(surface);
210
211 widget_get_allocation(widget, &allocation);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500212 if (launcher->pressed) {
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500213 allocation.x++;
214 allocation.y++;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400215 }
216
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500217 cairo_set_source_surface(cr, launcher->icon,
218 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400219 cairo_paint(cr);
220
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500221 if (launcher->focused) {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400222 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500223 cairo_mask_surface(cr, launcher->icon,
224 allocation.x, allocation.y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400225 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400226
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500227 cairo_destroy(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400228}
229
Tiago Vignatti61500722012-05-23 22:06:28 +0300230static int
231panel_launcher_motion_handler(struct widget *widget, struct input *input,
232 uint32_t time, float x, float y, void *data)
233{
234 struct panel_launcher *launcher = data;
235
236 widget_set_tooltip(widget, basename((char *)launcher->path), x, y);
237
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300238 return CURSOR_LEFT_PTR;
Tiago Vignatti61500722012-05-23 22:06:28 +0300239}
240
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400241static void
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500242set_hex_color(cairo_t *cr, uint32_t color)
243{
244 cairo_set_source_rgba(cr,
245 ((color >> 16) & 0xff) / 255.0,
246 ((color >> 8) & 0xff) / 255.0,
247 ((color >> 0) & 0xff) / 255.0,
248 ((color >> 24) & 0xff) / 255.0);
249}
250
251static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500252panel_redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400253{
254 cairo_surface_t *surface;
255 cairo_t *cr;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500256 struct panel *panel = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400257
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500258 surface = window_get_surface(panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400259 cr = cairo_create(surface);
260 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500261 set_hex_color(cr, key_panel_color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400262 cairo_paint(cr);
263
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400264 cairo_destroy(cr);
265 cairo_surface_destroy(surface);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400266}
267
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500268static int
Kristian Høgsberg53880802012-01-09 11:16:50 -0500269panel_launcher_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400270 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500271{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500272 struct panel_launcher *launcher = data;
273
274 launcher->focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500275 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500276
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300277 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500278}
279
280static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500281panel_launcher_leave_handler(struct widget *widget,
282 struct input *input, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400283{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500284 struct panel_launcher *launcher = data;
285
286 launcher->focused = 0;
Tiago Vignatti61500722012-05-23 22:06:28 +0300287 widget_destroy_tooltip(widget);
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500288 widget_schedule_redraw(widget);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400289}
290
291static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500292panel_launcher_button_handler(struct widget *widget,
293 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100294 uint32_t button,
295 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400296{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500297 struct panel_launcher *launcher;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400298
Kristian Høgsberg53880802012-01-09 11:16:50 -0500299 launcher = widget_get_user_data(widget);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500300 widget_schedule_redraw(widget);
Daniel Stone4dbadb12012-05-30 16:31:51 +0100301 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
Kristian Høgsberg53880802012-01-09 11:16:50 -0500302 panel_launcher_activate(launcher);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500303}
304
Martin Minarik1e51a872012-06-08 00:39:11 +0200305static void
306clock_func(struct task *task, uint32_t events)
307{
308 struct panel_clock *clock =
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400309 container_of(task, struct panel_clock, clock_task);
Kristian Høgsberg70226bb2012-06-08 16:54:52 -0400310 uint64_t exp;
Martin Minarik1e51a872012-06-08 00:39:11 +0200311
Martin Olsson8df662a2012-07-08 03:03:47 +0200312 if (read(clock->clock_fd, &exp, sizeof exp) != sizeof exp)
313 abort();
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400314 widget_schedule_redraw(clock->widget);
Martin Minarik1e51a872012-06-08 00:39:11 +0200315}
316
317static void
318panel_clock_redraw_handler(struct widget *widget, void *data)
319{
320 cairo_surface_t *surface;
321 struct panel_clock *clock = data;
322 cairo_t *cr;
323 struct rectangle allocation;
324 cairo_text_extents_t extents;
325 cairo_font_extents_t font_extents;
Martin Minarik1e51a872012-06-08 00:39:11 +0200326 time_t rawtime;
327 struct tm * timeinfo;
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400328 char string[128];
Martin Minarik1e51a872012-06-08 00:39:11 +0200329
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400330 time(&rawtime);
331 timeinfo = localtime(&rawtime);
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400332 strftime(string, sizeof string, "%a %b %d, %I:%M %p", timeinfo);
Martin Minarik1e51a872012-06-08 00:39:11 +0200333
334 widget_get_allocation(widget, &allocation);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400335 if (allocation.width == 0)
336 return;
Martin Minarik1e51a872012-06-08 00:39:11 +0200337
338 surface = window_get_surface(clock->panel->window);
339 cr = cairo_create(surface);
Martin Minarik1e51a872012-06-08 00:39:11 +0200340 cairo_select_font_face(cr, "sans",
341 CAIRO_FONT_SLANT_NORMAL,
342 CAIRO_FONT_WEIGHT_NORMAL);
343 cairo_set_font_size(cr, 14);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400344 cairo_text_extents(cr, string, &extents);
Martin Minarik1e51a872012-06-08 00:39:11 +0200345 cairo_font_extents (cr, &font_extents);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400346 cairo_move_to(cr, allocation.x + 5,
347 allocation.y + 3 * (allocation.height >> 2) + 1);
348 cairo_set_source_rgb(cr, 0, 0, 0);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400349 cairo_show_text(cr, string);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400350 cairo_move_to(cr, allocation.x + 4,
351 allocation.y + 3 * (allocation.height >> 2));
352 cairo_set_source_rgb(cr, 1, 1, 1);
Kristian Høgsberg92a984a2012-06-11 11:10:57 -0400353 cairo_show_text(cr, string);
Martin Minarik1e51a872012-06-08 00:39:11 +0200354 cairo_destroy(cr);
355}
356
357static int
358clock_timer_reset(struct panel_clock *clock)
359{
360 struct itimerspec its;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400361
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400362 its.it_interval.tv_sec = 60;
Martin Minarik1e51a872012-06-08 00:39:11 +0200363 its.it_interval.tv_nsec = 0;
Kristian Høgsberge9f68f62012-06-11 12:24:12 -0400364 its.it_value.tv_sec = 60;
Martin Minarik1e51a872012-06-08 00:39:11 +0200365 its.it_value.tv_nsec = 0;
366 if (timerfd_settime(clock->clock_fd, 0, &its, NULL) < 0) {
367 fprintf(stderr, "could not set timerfd\n: %m");
368 return -1;
369 }
370
371 return 0;
372}
373
374static void
375panel_add_clock(struct panel *panel)
376{
377 struct panel_clock *clock;
378 int timerfd;
379
380 timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
381 if (timerfd < 0) {
382 fprintf(stderr, "could not create timerfd\n: %m");
383 return;
384 }
385
386 clock = malloc(sizeof *clock);
387 memset(clock, 0, sizeof *clock);
388 clock->panel = panel;
389 panel->clock = clock;
390 clock->clock_fd = timerfd;
391
392 clock->clock_task.run = clock_func;
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400393 display_watch_fd(window_get_display(panel->window), clock->clock_fd,
394 EPOLLIN, &clock->clock_task);
Martin Minarik1e51a872012-06-08 00:39:11 +0200395 clock_timer_reset(clock);
396
397 clock->widget = widget_add_widget(panel->widget, clock);
Kristian Høgsbergbb262cf2012-06-11 11:03:03 -0400398 widget_set_redraw_handler(clock->widget, panel_clock_redraw_handler);
Martin Minarik1e51a872012-06-08 00:39:11 +0200399}
400
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500401static void
402panel_button_handler(struct widget *widget,
403 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100404 uint32_t button,
405 enum wl_pointer_button_state state, void *data)
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500406{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500407 struct panel *panel = data;
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500408
Daniel Stone4dbadb12012-05-30 16:31:51 +0100409 if (button == BTN_RIGHT && state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500410 show_menu(panel, input, time);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400411}
412
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100413static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500414panel_resize_handler(struct widget *widget,
415 int32_t width, int32_t height, void *data)
416{
417 struct panel_launcher *launcher;
418 struct panel *panel = data;
419 int x, y, w, h;
420
421 x = 10;
422 y = 16;
423 wl_list_for_each(launcher, &panel->launcher_list, link) {
424 w = cairo_image_surface_get_width(launcher->icon);
425 h = cairo_image_surface_get_height(launcher->icon);
426 widget_set_allocation(launcher->widget,
427 x, y - h / 2, w + 1, h + 1);
428 x += w + 10;
429 }
Martin Minarik1e51a872012-06-08 00:39:11 +0200430 h=20;
431 w=170;
Pekka Paalanen01b17252012-06-12 17:42:26 +0300432
433 if (panel->clock)
434 widget_set_allocation(panel->clock->widget,
435 width - w - 8, y - h / 2, w + 1, h + 1);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500436}
437
438static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100439panel_configure(void *data,
440 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400441 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100442 int32_t width, int32_t height)
443{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500444 struct surface *surface = window_get_user_data(window);
445 struct panel *panel = container_of(surface, struct panel, base);
446
447 window_schedule_resize(panel->window, width, 32);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100448}
449
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400450static struct panel *
451panel_create(struct display *display)
452{
453 struct panel *panel;
454
455 panel = malloc(sizeof *panel);
456 memset(panel, 0, sizeof *panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400457
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100458 panel->base.configure = panel_configure;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400459 panel->window = window_create_custom(display);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500460 panel->widget = window_add_widget(panel->window, panel);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500461 wl_list_init(&panel->launcher_list);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400462
463 window_set_title(panel->window, "panel");
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400464 window_set_user_data(panel->window, panel);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500465
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500466 widget_set_redraw_handler(panel->widget, panel_redraw_handler);
467 widget_set_resize_handler(panel->widget, panel_resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500468 widget_set_button_handler(panel->widget, panel_button_handler);
Rafal Mielniczukb9e513c2012-06-09 20:33:29 +0200469
470 panel_add_clock(panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400471
472 return panel;
473}
474
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200475static cairo_surface_t *
476load_icon_or_fallback(const char *icon)
477{
478 cairo_surface_t *surface = cairo_image_surface_create_from_png(icon);
479 cairo_t *cr;
480
481 if (cairo_surface_status(surface) == CAIRO_STATUS_SUCCESS)
482 return surface;
483
484 cairo_surface_destroy(surface);
485 fprintf(stderr, "ERROR loading icon from file '%s'\n", icon);
486
487 /* draw fallback icon */
488 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
489 20, 20);
490 cr = cairo_create(surface);
491
492 cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1);
493 cairo_paint(cr);
494
495 cairo_set_source_rgba(cr, 0, 0, 0, 1);
496 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
497 cairo_rectangle(cr, 0, 0, 20, 20);
498 cairo_move_to(cr, 4, 4);
499 cairo_line_to(cr, 16, 16);
500 cairo_move_to(cr, 4, 16);
501 cairo_line_to(cr, 16, 4);
502 cairo_stroke(cr);
503
504 cairo_destroy(cr);
505
506 return surface;
507}
508
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400509static void
Kristian Høgsberg53880802012-01-09 11:16:50 -0500510panel_add_launcher(struct panel *panel, const char *icon, const char *path)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400511{
Kristian Høgsberg53880802012-01-09 11:16:50 -0500512 struct panel_launcher *launcher;
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400513 char *start, *p, *eq, **ps;
514 int i, j, k;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400515
Kristian Høgsberg53880802012-01-09 11:16:50 -0500516 launcher = malloc(sizeof *launcher);
517 memset(launcher, 0, sizeof *launcher);
Philipp Brüschweiler467668c2012-08-29 10:53:36 +0200518 launcher->icon = load_icon_or_fallback(icon);
Kristian Høgsberg53880802012-01-09 11:16:50 -0500519 launcher->path = strdup(path);
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400520
521 wl_array_init(&launcher->envp);
522 wl_array_init(&launcher->argv);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300523 for (i = 0; environ[i]; i++) {
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400524 ps = wl_array_add(&launcher->envp, sizeof *ps);
Pekka Paalanenb6df4f72012-08-03 14:39:15 +0300525 *ps = environ[i];
Kristian Høgsbergd1936b92012-07-23 22:59:33 -0400526 }
527 j = 0;
528
529 start = launcher->path;
530 while (*start) {
531 for (p = start, eq = NULL; *p && !isspace(*p); p++)
532 if (*p == '=')
533 eq = p;
534
535 if (eq && j == 0) {
536 ps = launcher->envp.data;
537 for (k = 0; k < i; k++)
538 if (strncmp(ps[k], start, eq - start) == 0) {
539 ps[k] = start;
540 break;
541 }
542 if (k == i) {
543 ps = wl_array_add(&launcher->envp, sizeof *ps);
544 *ps = start;
545 i++;
546 }
547 } else {
548 ps = wl_array_add(&launcher->argv, sizeof *ps);
549 *ps = start;
550 j++;
551 }
552
553 while (*p && isspace(*p))
554 *p++ = '\0';
555
556 start = p;
557 }
558
559 ps = wl_array_add(&launcher->envp, sizeof *ps);
560 *ps = NULL;
561 ps = wl_array_add(&launcher->argv, sizeof *ps);
562 *ps = NULL;
563
Kristian Høgsberg53880802012-01-09 11:16:50 -0500564 launcher->panel = panel;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500565 wl_list_insert(panel->launcher_list.prev, &launcher->link);
566
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500567 launcher->widget = widget_add_widget(panel->widget, launcher);
Kristian Høgsberg53880802012-01-09 11:16:50 -0500568 widget_set_enter_handler(launcher->widget,
569 panel_launcher_enter_handler);
570 widget_set_leave_handler(launcher->widget,
571 panel_launcher_leave_handler);
572 widget_set_button_handler(launcher->widget,
573 panel_launcher_button_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500574 widget_set_redraw_handler(launcher->widget,
575 panel_launcher_redraw_handler);
Tiago Vignatti61500722012-05-23 22:06:28 +0300576 widget_set_motion_handler(launcher->widget,
577 panel_launcher_motion_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400578}
579
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500580enum {
581 BACKGROUND_SCALE,
582 BACKGROUND_TILE
583};
584
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400585static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500586background_draw(struct widget *widget, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400587{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500588 struct background *background = data;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400589 cairo_surface_t *surface, *image;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400590 cairo_pattern_t *pattern;
591 cairo_matrix_t matrix;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400592 cairo_t *cr;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400593 double sx, sy;
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500594 struct rectangle allocation;
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500595 int type = -1;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400596
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500597 surface = window_get_surface(background->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400598
599 cr = cairo_create(surface);
600 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
601 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
602 cairo_paint(cr);
603
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500604 widget_get_allocation(widget, &allocation);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500605 image = NULL;
606 if (key_background_image)
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400607 image = load_cairo_surface(key_background_image);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500608
609 if (strcmp(key_background_type, "scale") == 0)
610 type = BACKGROUND_SCALE;
611 else if (strcmp(key_background_type, "tile") == 0)
612 type = BACKGROUND_TILE;
613 else
614 fprintf(stderr, "invalid background-type: %s\n",
615 key_background_type);
616
617 if (image && type != -1) {
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400618 pattern = cairo_pattern_create_for_surface(image);
Kristian Høgsberg07f72942012-01-25 16:34:36 -0500619 switch (type) {
620 case BACKGROUND_SCALE:
621 sx = (double) cairo_image_surface_get_width(image) /
622 allocation.width;
623 sy = (double) cairo_image_surface_get_height(image) /
624 allocation.height;
625 cairo_matrix_init_scale(&matrix, sx, sy);
626 cairo_pattern_set_matrix(pattern, &matrix);
627 break;
628 case BACKGROUND_TILE:
629 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
630 break;
631 }
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400632 cairo_set_source(cr, pattern);
633 cairo_pattern_destroy (pattern);
Kristian Høgsberg27d38662011-10-20 13:11:12 -0400634 cairo_surface_destroy(image);
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500635 } else {
636 set_hex_color(cr, key_background_color);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400637 }
638
Kristian Høgsberg8129bc02012-01-25 14:55:33 -0500639 cairo_paint(cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400640 cairo_destroy(cr);
641 cairo_surface_destroy(surface);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400642}
643
644static void
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100645background_configure(void *data,
646 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400647 uint32_t edges, struct window *window,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100648 int32_t width, int32_t height)
649{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500650 struct background *background =
651 (struct background *) window_get_user_data(window);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100652
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500653 widget_schedule_resize(background->widget, width, height);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100654}
655
656static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500657unlock_dialog_redraw_handler(struct widget *widget, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200658{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500659 struct unlock_dialog *dialog = data;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200660 struct rectangle allocation;
661 cairo_t *cr;
662 cairo_surface_t *surface;
663 cairo_pattern_t *pat;
664 double cx, cy, r, f;
665
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200666 surface = window_get_surface(dialog->window);
667 cr = cairo_create(surface);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500668
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500669 widget_get_allocation(dialog->widget, &allocation);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200670 cairo_rectangle(cr, allocation.x, allocation.y,
671 allocation.width, allocation.height);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200672 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
673 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500674 cairo_fill(cr);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200675
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500676 cairo_translate(cr, allocation.x, allocation.y);
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500677 if (dialog->button_focused)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200678 f = 1.0;
679 else
680 f = 0.7;
681
682 cx = allocation.width / 2.0;
683 cy = allocation.height / 2.0;
684 r = (cx < cy ? cx : cy) * 0.4;
685 pat = cairo_pattern_create_radial(cx, cy, r * 0.7, cx, cy, r);
686 cairo_pattern_add_color_stop_rgb(pat, 0.0, 0, 0.86 * f, 0);
687 cairo_pattern_add_color_stop_rgb(pat, 0.85, 0.2 * f, f, 0.2 * f);
688 cairo_pattern_add_color_stop_rgb(pat, 1.0, 0, 0.86 * f, 0);
689 cairo_set_source(cr, pat);
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500690 cairo_pattern_destroy(pat);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200691 cairo_arc(cr, cx, cy, r, 0.0, 2.0 * M_PI);
692 cairo_fill(cr);
693
Kristian Høgsbergc51f7992012-01-08 15:09:53 -0500694 widget_set_allocation(dialog->button,
Kristian Høgsberg41c5c4e2012-03-05 20:37:51 -0500695 allocation.x + cx - r,
696 allocation.y + cy - r, 2 * r, 2 * r);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200697
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200698 cairo_destroy(cr);
699
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200700 cairo_surface_destroy(surface);
701}
702
703static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500704unlock_dialog_button_handler(struct widget *widget,
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200705 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100706 uint32_t button,
707 enum wl_pointer_button_state state, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200708{
709 struct unlock_dialog *dialog = data;
710 struct desktop *desktop = dialog->desktop;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200711
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500712 if (button == BTN_LEFT) {
Daniel Stone4dbadb12012-05-30 16:31:51 +0100713 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
714 !dialog->closing) {
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200715 display_defer(desktop->display, &desktop->unlock_task);
716 dialog->closing = 1;
717 }
718 }
719}
720
721static void
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200722unlock_dialog_keyboard_focus_handler(struct window *window,
723 struct input *device, void *data)
724{
725 window_schedule_redraw(window);
726}
727
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500728static int
Kristian Høgsbergee143232012-01-09 08:42:24 -0500729unlock_dialog_widget_enter_handler(struct widget *widget,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400730 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400731 float x, float y, void *data)
Kristian Høgsbergee143232012-01-09 08:42:24 -0500732{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500733 struct unlock_dialog *dialog = data;
734
735 dialog->button_focused = 1;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500736 widget_schedule_redraw(widget);
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500737
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300738 return CURSOR_LEFT_PTR;
Kristian Høgsbergee143232012-01-09 08:42:24 -0500739}
740
741static void
742unlock_dialog_widget_leave_handler(struct widget *widget,
743 struct input *input, void *data)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200744{
Kristian Høgsbergb6323512012-01-11 00:04:42 -0500745 struct unlock_dialog *dialog = data;
746
747 dialog->button_focused = 0;
Kristian Høgsberg9a13dab2012-01-08 15:18:19 -0500748 widget_schedule_redraw(widget);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200749}
750
751static struct unlock_dialog *
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500752unlock_dialog_create(struct desktop *desktop)
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200753{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500754 struct display *display = desktop->display;
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200755 struct unlock_dialog *dialog;
756
757 dialog = malloc(sizeof *dialog);
758 if (!dialog)
759 return NULL;
760 memset(dialog, 0, sizeof *dialog);
761
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400762 dialog->window = window_create_custom(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500763 dialog->widget = frame_create(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200764 window_set_title(dialog->window, "Unlock your desktop");
765
766 window_set_user_data(dialog->window, dialog);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200767 window_set_keyboard_focus_handler(dialog->window,
768 unlock_dialog_keyboard_focus_handler);
Kristian Høgsberg441338c2012-01-10 13:52:34 -0500769 dialog->button = widget_add_widget(dialog->widget, dialog);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500770 widget_set_redraw_handler(dialog->widget,
771 unlock_dialog_redraw_handler);
Kristian Høgsbergee143232012-01-09 08:42:24 -0500772 widget_set_enter_handler(dialog->button,
773 unlock_dialog_widget_enter_handler);
774 widget_set_leave_handler(dialog->button,
775 unlock_dialog_widget_leave_handler);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500776 widget_set_button_handler(dialog->button,
777 unlock_dialog_button_handler);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200778
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500779 desktop_shell_set_lock_surface(desktop->shell,
Kristian Høgsberg730c94d2012-06-26 21:44:35 -0400780 window_get_wl_surface(dialog->window));
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500781
Pekka Paalanen40e49ac2012-01-18 16:51:30 +0200782 window_schedule_resize(dialog->window, 260, 230);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200783
784 return dialog;
785}
786
787static void
788unlock_dialog_destroy(struct unlock_dialog *dialog)
789{
790 window_destroy(dialog->window);
791 free(dialog);
792}
793
794static void
795unlock_dialog_finish(struct task *task, uint32_t events)
796{
797 struct desktop *desktop =
Benjamin Franzked7759712011-11-22 12:38:48 +0100798 container_of(task, struct desktop, unlock_task);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200799
800 desktop_shell_unlock(desktop->shell);
801 unlock_dialog_destroy(desktop->unlock_dialog);
802 desktop->unlock_dialog = NULL;
803}
804
805static void
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400806desktop_shell_configure(void *data,
807 struct desktop_shell *desktop_shell,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400808 uint32_t edges,
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400809 struct wl_surface *surface,
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400810 int32_t width, int32_t height)
811{
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400812 struct window *window = wl_surface_get_user_data(surface);
Pekka Paalanen068ae942011-11-28 14:11:15 +0200813 struct surface *s = window_get_user_data(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400814
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400815 s->configure(data, desktop_shell, edges, window, width, height);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400816}
817
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200818static void
819desktop_shell_prepare_lock_surface(void *data,
820 struct desktop_shell *desktop_shell)
821{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200822 struct desktop *desktop = data;
823
Pekka Paalanenfd83b6d2011-12-08 10:06:53 +0200824 if (!key_locking) {
825 desktop_shell_unlock(desktop->shell);
826 return;
827 }
828
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200829 if (!desktop->unlock_dialog) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500830 desktop->unlock_dialog = unlock_dialog_create(desktop);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +0200831 desktop->unlock_dialog->desktop = desktop;
832 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200833}
834
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300835static void
836desktop_shell_grab_cursor(void *data,
837 struct desktop_shell *desktop_shell,
838 uint32_t cursor)
839{
840 struct desktop *desktop = data;
841
842 switch (cursor) {
Philipp Brüschweiler16d59d72012-08-24 15:43:55 +0200843 case DESKTOP_SHELL_CURSOR_NONE:
844 desktop->grab_cursor = CURSOR_BLANK;
845 break;
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300846 case DESKTOP_SHELL_CURSOR_BUSY:
847 desktop->grab_cursor = CURSOR_WATCH;
848 break;
849 case DESKTOP_SHELL_CURSOR_MOVE:
850 desktop->grab_cursor = CURSOR_DRAGGING;
851 break;
852 case DESKTOP_SHELL_CURSOR_RESIZE_TOP:
853 desktop->grab_cursor = CURSOR_TOP;
854 break;
855 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM:
856 desktop->grab_cursor = CURSOR_BOTTOM;
857 break;
858 case DESKTOP_SHELL_CURSOR_RESIZE_LEFT:
859 desktop->grab_cursor = CURSOR_LEFT;
860 break;
861 case DESKTOP_SHELL_CURSOR_RESIZE_RIGHT:
862 desktop->grab_cursor = CURSOR_RIGHT;
863 break;
864 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_LEFT:
865 desktop->grab_cursor = CURSOR_TOP_LEFT;
866 break;
867 case DESKTOP_SHELL_CURSOR_RESIZE_TOP_RIGHT:
868 desktop->grab_cursor = CURSOR_TOP_RIGHT;
869 break;
870 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_LEFT:
871 desktop->grab_cursor = CURSOR_BOTTOM_LEFT;
872 break;
873 case DESKTOP_SHELL_CURSOR_RESIZE_BOTTOM_RIGHT:
874 desktop->grab_cursor = CURSOR_BOTTOM_RIGHT;
875 break;
876 case DESKTOP_SHELL_CURSOR_ARROW:
877 default:
878 desktop->grab_cursor = CURSOR_LEFT_PTR;
879 }
880}
881
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400882static const struct desktop_shell_listener listener = {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200883 desktop_shell_configure,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300884 desktop_shell_prepare_lock_surface,
885 desktop_shell_grab_cursor
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400886};
887
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100888static struct background *
889background_create(struct desktop *desktop)
890{
891 struct background *background;
892
893 background = malloc(sizeof *background);
894 memset(background, 0, sizeof *background);
895
896 background->base.configure = background_configure;
Kristian Høgsberg962342c2012-06-26 16:29:50 -0400897 background->window = window_create_custom(desktop->display);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500898 background->widget = window_add_widget(background->window, background);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100899 window_set_user_data(background->window, background);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500900 widget_set_redraw_handler(background->widget, background_draw);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100901
902 return background;
903}
904
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400905static int
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300906grab_surface_enter_handler(struct widget *widget, struct input *input,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400907 float x, float y, void *data)
908{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300909 struct desktop *desktop = data;
910
911 return desktop->grab_cursor;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400912}
913
914static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300915grab_surface_create(struct desktop *desktop)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400916{
917 struct wl_surface *s;
918
Ander Conselvan de Oliveira07a91cd2012-07-16 14:15:50 +0300919 desktop->grab_window = window_create_custom(desktop->display);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300920 window_set_user_data(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400921
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300922 s = window_get_wl_surface(desktop->grab_window);
923 desktop_shell_set_grab_surface(desktop->shell, s);
924
925 desktop->grab_widget =
926 window_add_widget(desktop->grab_window, desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400927 /* We set the allocation to 1x1 at 0,0 so the fake enter event
928 * at 0,0 will go to this widget. */
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300929 widget_set_allocation(desktop->grab_widget, 0, 0, 1, 1);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400930
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300931 widget_set_enter_handler(desktop->grab_widget,
932 grab_surface_enter_handler);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -0400933}
934
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100935static void
936create_output(struct desktop *desktop, uint32_t id)
937{
938 struct output *output;
939
940 output = calloc(1, sizeof *output);
941 if (!output)
942 return;
943
944 output->output = wl_display_bind(display_get_display(desktop->display),
945 id, &wl_output_interface);
946
947 wl_list_insert(&desktop->outputs, &output->link);
948}
949
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400950static void
951global_handler(struct wl_display *display, uint32_t id,
952 const char *interface, uint32_t version, void *data)
953{
954 struct desktop *desktop = data;
955
956 if (!strcmp(interface, "desktop_shell")) {
957 desktop->shell =
958 wl_display_bind(display, id, &desktop_shell_interface);
959 desktop_shell_add_listener(desktop->shell, &listener, desktop);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100960 } else if (!strcmp(interface, "wl_output")) {
961 create_output(desktop, id);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400962 }
963}
964
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500965static void
966launcher_section_done(void *data)
967{
968 struct desktop *desktop = data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100969 struct output *output;
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500970
971 if (key_launcher_icon == NULL || key_launcher_path == NULL) {
972 fprintf(stderr, "invalid launcher section\n");
973 return;
974 }
975
Martin Minarik1e51a872012-06-08 00:39:11 +0200976 wl_list_for_each(output, &desktop->outputs, link) {
Kristian Høgsberg53880802012-01-09 11:16:50 -0500977 panel_add_launcher(output->panel,
978 key_launcher_icon, key_launcher_path);
Martin Minarik1e51a872012-06-08 00:39:11 +0200979 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100980
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -0500981 free(key_launcher_icon);
982 key_launcher_icon = NULL;
983 free(key_launcher_path);
984 key_launcher_path = NULL;
985}
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400986
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500987static void
988add_default_launcher(struct desktop *desktop)
989{
990 struct output *output;
991
992 wl_list_for_each(output, &desktop->outputs, link)
993 panel_add_launcher(output->panel,
994 DATADIR "/weston/terminal.png",
Rodney Lorrimar99ff01b2012-02-29 17:31:03 +0100995 BINDIR "/weston-terminal");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500996}
997
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400998int main(int argc, char *argv[])
999{
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001000 struct desktop desktop = { 0 };
Pekka Paalanen668dd562011-11-15 11:45:40 +02001001 char *config_file;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001002 struct output *output;
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001003 int ret;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001004
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001005 desktop.unlock_task.run = unlock_dialog_finish;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001006 wl_list_init(&desktop.outputs);
Pekka Paalanenbfbb26b2011-11-15 13:34:56 +02001007
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001008 desktop.display = display_create(argc, argv);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001009 if (desktop.display == NULL) {
1010 fprintf(stderr, "failed to create display: %m\n");
1011 return -1;
1012 }
1013
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001014 display_set_user_data(desktop.display, &desktop);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001015 wl_display_add_global_listener(display_get_display(desktop.display),
1016 global_handler, &desktop);
1017
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001018 wl_list_for_each(output, &desktop.outputs, link) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001019 struct wl_surface *surface;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001020
1021 output->panel = panel_create(desktop.display);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001022 surface = window_get_wl_surface(output->panel->window);
1023 desktop_shell_set_panel(desktop.shell,
1024 output->output, surface);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001025
1026 output->background = background_create(&desktop);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04001027 surface = window_get_wl_surface(output->background->window);
1028 desktop_shell_set_background(desktop.shell,
1029 output->output, surface);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001030 }
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001031
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001032 grab_surface_create(&desktop);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001033
Tiago Vignatti9a206c42012-03-21 19:49:18 +02001034 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001035 ret = parse_config_file(config_file,
1036 config_sections, ARRAY_LENGTH(config_sections),
1037 &desktop);
Pekka Paalanen668dd562011-11-15 11:45:40 +02001038 free(config_file);
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001039 if (ret < 0)
1040 add_default_launcher(&desktop);
Kristian Høgsbergac3a59a2011-11-14 22:43:37 -05001041
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001042 signal(SIGCHLD, sigchild_handler);
1043
1044 display_run(desktop.display);
1045
1046 return 0;
1047}