Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 1 | /* |
| 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øgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 35 | #include "window.h" |
| 36 | |
| 37 | #include <desktop-shell-client-protocol.h> |
| 38 | |
| 39 | struct 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 | |
| 47 | struct panel { |
| 48 | struct window *window; |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | struct panel_item { |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 52 | struct item *item; |
| 53 | struct panel *panel; |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 54 | cairo_surface_t *icon; |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 55 | int pressed; |
| 56 | const char *path; |
| 57 | }; |
| 58 | |
| 59 | static void |
| 60 | sigchild_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 | |
| 69 | static void |
| 70 | panel_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øgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 89 | static void |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 90 | panel_draw_item(struct item *item, void *data) |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 91 | { |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 92 | cairo_t *cr = data; |
| 93 | struct panel_item *pi; |
| 94 | int x, y, width, height; |
| 95 | double dx, dy; |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 96 | |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 97 | 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øgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 105 | } |
| 106 | |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 107 | 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øgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 113 | cairo_paint(cr); |
| 114 | |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 115 | if (window_get_focus_item(pi->panel->window) == item) { |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 116 | cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4); |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 117 | cairo_mask_surface(cr, pi->icon, x, y); |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 118 | } |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 119 | |
| 120 | cairo_translate(cr, width + 10, 0); |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static void |
| 124 | panel_redraw_handler(struct window *window, void *data) |
| 125 | { |
| 126 | cairo_surface_t *surface; |
| 127 | cairo_t *cr; |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 128 | |
| 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øgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 137 | cairo_translate(cr, 10, 32 / 2); |
| 138 | window_for_each_item(window, panel_draw_item, cr); |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 139 | |
| 140 | cairo_destroy(cr); |
| 141 | cairo_surface_destroy(surface); |
| 142 | window_flush(window); |
| 143 | } |
| 144 | |
| 145 | static void |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 146 | panel_item_focus_handler(struct window *window, |
| 147 | struct item *focus, void *data) |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 148 | { |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 149 | window_schedule_redraw(window); |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static void |
| 153 | panel_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øgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 158 | struct panel_item *pi; |
| 159 | struct item *focus; |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 160 | |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 161 | focus = window_get_focus_item(panel->window); |
| 162 | if (focus && button == BTN_LEFT) { |
| 163 | pi = item_get_user_data(focus); |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 164 | window_schedule_redraw(panel->window); |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 165 | if (state == 0) |
| 166 | panel_activate_item(panel, pi); |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | static struct panel * |
| 171 | panel_create(struct display *display) |
| 172 | { |
| 173 | struct panel *panel; |
| 174 | |
| 175 | panel = malloc(sizeof *panel); |
| 176 | memset(panel, 0, sizeof *panel); |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 177 | |
| 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øgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 185 | window_set_button_handler(panel->window, panel_button_handler); |
Kristian Høgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 186 | window_set_item_focus_handler(panel->window, panel_item_focus_handler); |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 187 | |
| 188 | return panel; |
| 189 | } |
| 190 | |
| 191 | static void |
| 192 | panel_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øgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 200 | item->panel = panel; |
| 201 | window_add_item(panel->window, item); |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static void |
| 205 | background_draw(struct window *window, int width, int height, const char *path) |
| 206 | { |
| 207 | cairo_surface_t *surface, *image; |
Kristian Høgsberg | 7e69000 | 2011-09-08 18:18:02 -0400 | [diff] [blame] | 208 | cairo_pattern_t *pattern; |
| 209 | cairo_matrix_t matrix; |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 210 | cairo_t *cr; |
Kristian Høgsberg | 7e69000 | 2011-09-08 18:18:02 -0400 | [diff] [blame] | 211 | double sx, sy; |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 212 | |
| 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øgsberg | 7e69000 | 2011-09-08 18:18:02 -0400 | [diff] [blame] | 224 | 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øgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 231 | cairo_paint(cr); |
| 232 | } |
| 233 | |
| 234 | cairo_destroy(cr); |
| 235 | cairo_surface_destroy(surface); |
| 236 | window_flush(window); |
| 237 | } |
| 238 | |
| 239 | static void |
| 240 | desktop_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øgsberg | e28d05b | 2011-09-20 21:43:54 -0400 | [diff] [blame] | 249 | window_set_child_size(desktop->panel->window, width, 32); |
| 250 | window_schedule_redraw(desktop->panel->window); |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 251 | } else if (surface == window_get_wl_surface(desktop->background)) { |
| 252 | background_draw(desktop->background, |
| 253 | width, height, desktop->background_path); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | static const struct desktop_shell_listener listener = { |
| 258 | desktop_shell_configure |
| 259 | }; |
| 260 | |
| 261 | static void |
| 262 | global_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 | |
| 274 | static 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øgsberg | 67680c1 | 2011-09-08 11:48:53 -0400 | [diff] [blame] | 283 | "/usr/share/icons/gnome/24x24/apps/utilities-terminal.png", |
| 284 | "./clients/terminal" |
| 285 | }, |
| 286 | { |
Kristian Høgsberg | 0c29eb2 | 2011-09-06 18:02:34 -0400 | [diff] [blame] | 287 | "/usr/share/icons/hicolor/24x24/apps/google-chrome.png", |
| 288 | "/usr/bin/google-chrome" |
| 289 | }, |
| 290 | }; |
| 291 | |
| 292 | int 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 | } |