blob: 496d0d1831963be485615406f1984ec3ed3d3be4 [file] [log] [blame]
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -04001/*
2 * Copyright © 2011 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <fcntl.h>
28#include <unistd.h>
29#include <math.h>
30#include <cairo.h>
31#include <sys/wait.h>
32#include <linux/input.h>
33
34#include "wayland-client.h"
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040035#include "window.h"
36
37#include <desktop-shell-client-protocol.h>
38
39struct desktop {
40 struct display *display;
41 struct desktop_shell *shell;
42 struct panel *panel;
43 struct window *background;
44 const char *background_path;
45};
46
47struct panel {
48 struct window *window;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040049};
50
51struct panel_item {
Kristian Høgsberge28d05b2011-09-20 21:43:54 -040052 struct item *item;
53 struct panel *panel;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040054 cairo_surface_t *icon;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040055 int pressed;
56 const char *path;
57};
58
59static void
60sigchild_handler(int s)
61{
62 int status;
63 pid_t pid;
64
65 while (pid = waitpid(-1, &status, WNOHANG), pid > 0)
66 fprintf(stderr, "child %d exited\n", pid);
67}
68
69static void
70panel_activate_item(struct panel *panel, struct panel_item *item)
71{
72 pid_t pid;
73
74 pid = fork();
75 if (pid < 0) {
76 fprintf(stderr, "fork failed: %m\n");
77 return;
78 }
79
80 if (pid)
81 return;
82
83 if (execl(item->path, item->path, NULL) < 0) {
84 fprintf(stderr, "execl failed: %m\n");
85 exit(1);
86 }
87}
88
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040089static void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -040090panel_draw_item(struct item *item, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040091{
Kristian Høgsberge28d05b2011-09-20 21:43:54 -040092 cairo_t *cr = data;
93 struct panel_item *pi;
94 int x, y, width, height;
95 double dx, dy;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -040096
Kristian Høgsberge28d05b2011-09-20 21:43:54 -040097 pi = item_get_user_data(item);
98 width = cairo_image_surface_get_width(pi->icon);
99 height = cairo_image_surface_get_height(pi->icon);
100 x = 0;
101 y = -height / 2;
102 if (pi->pressed) {
103 x++;
104 y++;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400105 }
106
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400107 dx = x;
108 dy = y;
109 cairo_user_to_device(cr, &dx, &dy);
110 item_set_allocation(item, dx, dy, width, height);
111
112 cairo_set_source_surface(cr, pi->icon, x, y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400113 cairo_paint(cr);
114
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400115 if (window_get_focus_item(pi->panel->window) == item) {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400116 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400117 cairo_mask_surface(cr, pi->icon, x, y);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400118 }
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400119
120 cairo_translate(cr, width + 10, 0);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400121}
122
123static void
124panel_redraw_handler(struct window *window, void *data)
125{
126 cairo_surface_t *surface;
127 cairo_t *cr;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400128
129 window_draw(window);
130 surface = window_get_surface(window);
131 cr = cairo_create(surface);
132 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
133 cairo_set_source_rgba(cr, 0.1, 0.1, 0.1, 0.9);
134 cairo_paint(cr);
135
136 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400137 cairo_translate(cr, 10, 32 / 2);
138 window_for_each_item(window, panel_draw_item, cr);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400139
140 cairo_destroy(cr);
141 cairo_surface_destroy(surface);
142 window_flush(window);
143}
144
145static void
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400146panel_item_focus_handler(struct window *window,
147 struct item *focus, void *data)
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400148{
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400149 window_schedule_redraw(window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400150}
151
152static void
153panel_button_handler(struct window *window,
154 struct input *input, uint32_t time,
155 int button, int state, void *data)
156{
157 struct panel *panel = data;
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400158 struct panel_item *pi;
159 struct item *focus;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400160
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400161 focus = window_get_focus_item(panel->window);
162 if (focus && button == BTN_LEFT) {
163 pi = item_get_user_data(focus);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400164 window_schedule_redraw(panel->window);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400165 if (state == 0)
166 panel_activate_item(panel, pi);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400167 }
168}
169
170static struct panel *
171panel_create(struct display *display)
172{
173 struct panel *panel;
174
175 panel = malloc(sizeof *panel);
176 memset(panel, 0, sizeof *panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400177
178 panel->window = window_create(display, 0, 0);
179
180 window_set_title(panel->window, "panel");
181 window_set_decoration(panel->window, 0);
182 window_set_redraw_handler(panel->window, panel_redraw_handler);
183 window_set_custom(panel->window);
184 window_set_user_data(panel->window, panel);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400185 window_set_button_handler(panel->window, panel_button_handler);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400186 window_set_item_focus_handler(panel->window, panel_item_focus_handler);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400187
188 return panel;
189}
190
191static void
192panel_add_item(struct panel *panel, const char *icon, const char *path)
193{
194 struct panel_item *item;
195
196 item = malloc(sizeof *item);
197 memset(item, 0, sizeof *item);
198 item->icon = cairo_image_surface_create_from_png(icon);
199 item->path = strdup(path);
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400200 item->panel = panel;
201 window_add_item(panel->window, item);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400202}
203
204static void
205background_draw(struct window *window, int width, int height, const char *path)
206{
207 cairo_surface_t *surface, *image;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400208 cairo_pattern_t *pattern;
209 cairo_matrix_t matrix;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400210 cairo_t *cr;
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400211 double sx, sy;
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400212
213 window_set_child_size(window, width, height);
214 window_draw(window);
215 surface = window_get_surface(window);
216
217 cr = cairo_create(surface);
218 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
219 cairo_set_source_rgba(cr, 0.0, 0.0, 0.2, 1.0);
220 cairo_paint(cr);
221
222 if (path) {
223 image = cairo_image_surface_create_from_png(path);
Kristian Høgsberg7e690002011-09-08 18:18:02 -0400224 pattern = cairo_pattern_create_for_surface(image);
225 sx = (double) cairo_image_surface_get_width(image) / width;
226 sy = (double) cairo_image_surface_get_height(image) / height;
227 cairo_matrix_init_scale(&matrix, sx, sy);
228 cairo_pattern_set_matrix(pattern, &matrix);
229 cairo_set_source(cr, pattern);
230 cairo_pattern_destroy (pattern);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400231 cairo_paint(cr);
232 }
233
234 cairo_destroy(cr);
235 cairo_surface_destroy(surface);
236 window_flush(window);
237}
238
239static void
240desktop_shell_configure(void *data,
241 struct desktop_shell *desktop_shell,
242 uint32_t time, uint32_t edges,
243 struct wl_surface *surface,
244 int32_t width, int32_t height)
245{
246 struct desktop *desktop = data;
247
248 if (surface == window_get_wl_surface(desktop->panel->window)) {
Kristian Høgsberge28d05b2011-09-20 21:43:54 -0400249 window_set_child_size(desktop->panel->window, width, 32);
250 window_schedule_redraw(desktop->panel->window);
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400251 } else if (surface == window_get_wl_surface(desktop->background)) {
252 background_draw(desktop->background,
253 width, height, desktop->background_path);
254 }
255}
256
257static const struct desktop_shell_listener listener = {
258 desktop_shell_configure
259};
260
261static void
262global_handler(struct wl_display *display, uint32_t id,
263 const char *interface, uint32_t version, void *data)
264{
265 struct desktop *desktop = data;
266
267 if (!strcmp(interface, "desktop_shell")) {
268 desktop->shell =
269 wl_display_bind(display, id, &desktop_shell_interface);
270 desktop_shell_add_listener(desktop->shell, &listener, desktop);
271 }
272}
273
274static const struct {
275 const char *icon;
276 const char *path;
277} launchers[] = {
278 {
279 "/usr/share/icons/gnome/24x24/apps/utilities-terminal.png",
280 "/usr/bin/gnome-terminal"
281 },
282 {
Kristian Høgsberg67680c12011-09-08 11:48:53 -0400283 "/usr/share/icons/gnome/24x24/apps/utilities-terminal.png",
284 "./clients/terminal"
285 },
286 {
Kristian Høgsberg0c29eb22011-09-06 18:02:34 -0400287 "/usr/share/icons/hicolor/24x24/apps/google-chrome.png",
288 "/usr/bin/google-chrome"
289 },
290};
291
292int main(int argc, char *argv[])
293{
294 struct desktop desktop;
295 int i;
296
297 desktop.display = display_create(&argc, &argv, NULL);
298 if (desktop.display == NULL) {
299 fprintf(stderr, "failed to create display: %m\n");
300 return -1;
301 }
302
303 wl_display_add_global_listener(display_get_display(desktop.display),
304 global_handler, &desktop);
305
306 desktop.panel = panel_create(desktop.display);
307
308 for (i = 0; i < ARRAY_LENGTH(launchers); i++)
309 panel_add_item(desktop.panel,
310 launchers[i].icon, launchers[i].path);
311
312 desktop_shell_set_panel(desktop.shell,
313 window_get_wl_surface(desktop.panel->window));
314
315 desktop.background = window_create(desktop.display, 0, 0);
316 window_set_decoration(desktop.background, 0);
317 window_set_custom(desktop.background);
318 desktop.background_path = argv[1];
319 desktop_shell_set_background(desktop.shell,
320 window_get_wl_surface(desktop.background));
321
322 signal(SIGCHLD, sigchild_handler);
323
324 display_run(desktop.display);
325
326 return 0;
327}