Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 1 | /* |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 2 | * Copyright © 2011, 2012 Intel Corporation |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 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> |
Daniel Stone | 6d4a3c1 | 2012-06-18 19:39:56 +0100 | [diff] [blame] | 27 | #include <unistd.h> |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 28 | #include <sys/wait.h> |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 29 | |
| 30 | #include "window.h" |
Kristian Høgsberg | 5a315bc | 2012-05-15 22:33:43 -0400 | [diff] [blame] | 31 | #include "../shared/cairo-util.h" |
Kristian Høgsberg | 9b935c8 | 2011-12-08 12:44:27 -0500 | [diff] [blame] | 32 | #include "../shared/config-parser.h" |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 33 | |
| 34 | #include "tablet-shell-client-protocol.h" |
| 35 | |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 36 | struct tablet { |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 37 | struct display *display; |
| 38 | struct tablet_shell *tablet_shell; |
| 39 | struct rectangle allocation; |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 40 | struct window *switcher; |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 41 | |
| 42 | struct homescreen *homescreen; |
| 43 | struct lockscreen *lockscreen; |
| 44 | }; |
| 45 | |
| 46 | struct homescreen { |
| 47 | struct window *window; |
| 48 | struct widget *widget; |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 49 | struct wl_list launcher_list; |
| 50 | }; |
| 51 | |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 52 | struct lockscreen { |
| 53 | struct window *window; |
| 54 | struct widget *widget; |
| 55 | }; |
| 56 | |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 57 | struct launcher { |
Alex Wu | 5b72a4d | 2012-06-18 16:52:31 +0800 | [diff] [blame] | 58 | struct widget *widget; |
| 59 | struct homescreen *homescreen; |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 60 | cairo_surface_t *icon; |
Alex Wu | 5b72a4d | 2012-06-18 16:52:31 +0800 | [diff] [blame] | 61 | int focused, pressed; |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 62 | char *path; |
| 63 | struct wl_list link; |
| 64 | }; |
| 65 | |
| 66 | static char *key_lockscreen_icon; |
| 67 | static char *key_lockscreen_background; |
| 68 | static char *key_homescreen_background; |
| 69 | static char *key_launcher_icon; |
| 70 | static char *key_launcher_path; |
| 71 | static void launcher_section_done(void *data); |
| 72 | |
Tiago Vignatti | 9a206c4 | 2012-03-21 19:49:18 +0200 | [diff] [blame] | 73 | static const struct config_key shell_config_keys[] = { |
| 74 | { "lockscreen-icon", CONFIG_KEY_STRING, &key_lockscreen_icon }, |
| 75 | { "lockscreen", CONFIG_KEY_STRING, &key_lockscreen_background }, |
| 76 | { "homescreen", CONFIG_KEY_STRING, &key_homescreen_background }, |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | static const struct config_key launcher_config_keys[] = { |
| 80 | { "icon", CONFIG_KEY_STRING, &key_launcher_icon }, |
| 81 | { "path", CONFIG_KEY_STRING, &key_launcher_path }, |
| 82 | }; |
| 83 | |
| 84 | static const struct config_section config_sections[] = { |
Tiago Vignatti | 9a206c4 | 2012-03-21 19:49:18 +0200 | [diff] [blame] | 85 | { "shell", |
| 86 | shell_config_keys, ARRAY_LENGTH(shell_config_keys) }, |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 87 | { "launcher", |
| 88 | launcher_config_keys, ARRAY_LENGTH(launcher_config_keys), |
| 89 | launcher_section_done } |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | static void |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 93 | sigchild_handler(int s) |
| 94 | { |
| 95 | int status; |
| 96 | pid_t pid; |
| 97 | |
| 98 | while (pid = waitpid(-1, &status, WNOHANG), pid > 0) |
| 99 | fprintf(stderr, "child %d exited\n", pid); |
| 100 | } |
| 101 | |
| 102 | static void |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 103 | paint_background(cairo_t *cr, const char *path, struct rectangle *allocation) |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 104 | { |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 105 | cairo_surface_t *image = NULL; |
| 106 | cairo_pattern_t *pattern; |
| 107 | cairo_matrix_t matrix; |
| 108 | double sx, sy; |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 109 | |
| 110 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 111 | if (path) |
Kristian Høgsberg | f02a649 | 2012-03-12 01:05:25 -0400 | [diff] [blame] | 112 | image = load_cairo_surface(path); |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 113 | if (image) { |
| 114 | pattern = cairo_pattern_create_for_surface(image); |
| 115 | sx = (double) cairo_image_surface_get_width(image) / |
| 116 | allocation->width; |
| 117 | sy = (double) cairo_image_surface_get_height(image) / |
| 118 | allocation->height; |
| 119 | cairo_matrix_init_scale(&matrix, sx, sy); |
| 120 | cairo_pattern_set_matrix(pattern, &matrix); |
| 121 | cairo_set_source(cr, pattern); |
| 122 | cairo_pattern_destroy (pattern); |
| 123 | cairo_surface_destroy(image); |
| 124 | cairo_paint(cr); |
| 125 | } else { |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 126 | fprintf(stderr, "couldn't load background image: %s\n", path); |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 127 | cairo_set_source_rgb(cr, 0.2, 0, 0); |
| 128 | cairo_paint(cr); |
| 129 | } |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 130 | } |
| 131 | |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 132 | static void |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 133 | homescreen_draw(struct widget *widget, void *data) |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 134 | { |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 135 | struct homescreen *homescreen = data; |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 136 | cairo_surface_t *surface; |
| 137 | struct rectangle allocation; |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 138 | cairo_t *cr; |
| 139 | struct launcher *launcher; |
| 140 | const int rows = 4, columns = 5, icon_width = 128, icon_height = 128; |
| 141 | int x, y, i, width, height, vmargin, hmargin, vpadding, hpadding; |
| 142 | |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 143 | surface = window_get_surface(homescreen->window); |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 144 | cr = cairo_create(surface); |
| 145 | |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 146 | widget_get_allocation(widget, &allocation); |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 147 | paint_background(cr, key_homescreen_background, &allocation); |
| 148 | |
| 149 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 150 | |
| 151 | width = allocation.width - columns * icon_width; |
| 152 | hpadding = width / (columns + 1); |
| 153 | hmargin = (width - hpadding * (columns - 1)) / 2; |
| 154 | |
| 155 | height = allocation.height - rows * icon_height; |
| 156 | vpadding = height / (rows + 1); |
| 157 | vmargin = (height - vpadding * (rows - 1)) / 2; |
| 158 | |
| 159 | x = hmargin; |
| 160 | y = vmargin; |
| 161 | i = 0; |
| 162 | |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 163 | wl_list_for_each(launcher, &homescreen->launcher_list, link) { |
Alex Wu | 5b72a4d | 2012-06-18 16:52:31 +0800 | [diff] [blame] | 164 | widget_set_allocation(launcher->widget, |
| 165 | x, y, icon_width, icon_height); |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 166 | x += icon_width + hpadding; |
| 167 | i++; |
| 168 | if (i == columns) { |
| 169 | x = hmargin; |
| 170 | y += icon_height + vpadding; |
| 171 | i = 0; |
| 172 | } |
| 173 | } |
| 174 | |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 175 | cairo_destroy(cr); |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 176 | cairo_surface_destroy(surface); |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 177 | } |
| 178 | |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 179 | static void |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 180 | lockscreen_draw(struct widget *widget, void *data) |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 181 | { |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 182 | struct lockscreen *lockscreen = data; |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 183 | cairo_surface_t *surface; |
| 184 | cairo_surface_t *icon; |
| 185 | struct rectangle allocation; |
| 186 | cairo_t *cr; |
| 187 | int width, height; |
| 188 | |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 189 | surface = window_get_surface(lockscreen->window); |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 190 | cr = cairo_create(surface); |
| 191 | |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 192 | widget_get_allocation(widget, &allocation); |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 193 | paint_background(cr, key_lockscreen_background, &allocation); |
| 194 | |
| 195 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 196 | icon = load_cairo_surface(key_lockscreen_icon); |
Tiago Vignatti | d081020 | 2012-03-28 20:56:48 +0300 | [diff] [blame] | 197 | if (icon) { |
| 198 | width = cairo_image_surface_get_width(icon); |
| 199 | height = cairo_image_surface_get_height(icon); |
| 200 | cairo_set_source_surface(cr, icon, |
| 201 | allocation.x + (allocation.width - width) / 2, |
| 202 | allocation.y + (allocation.height - height) / 2); |
| 203 | } else { |
| 204 | fprintf(stderr, "couldn't load lockscreen icon: %s\n", |
| 205 | key_lockscreen_icon); |
| 206 | cairo_set_source_rgb(cr, 0.2, 0, 0); |
| 207 | } |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 208 | cairo_paint(cr); |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 209 | cairo_destroy(cr); |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 210 | cairo_surface_destroy(icon); |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 211 | cairo_surface_destroy(surface); |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static void |
| 215 | lockscreen_button_handler(struct widget *widget, |
| 216 | struct input *input, uint32_t time, |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 217 | uint32_t button, |
| 218 | enum wl_pointer_button_state state, void *data) |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 219 | { |
| 220 | struct lockscreen *lockscreen = data; |
| 221 | |
Daniel Stone | 4dbadb1 | 2012-05-30 16:31:51 +0100 | [diff] [blame] | 222 | if (state == WL_POINTER_BUTTON_STATE_PRESSED && lockscreen->window) { |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 223 | window_destroy(lockscreen->window); |
| 224 | lockscreen->window = NULL; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | static struct homescreen * |
| 229 | homescreen_create(struct tablet *tablet) |
| 230 | { |
| 231 | struct homescreen *homescreen; |
| 232 | |
| 233 | homescreen = malloc (sizeof *homescreen); |
| 234 | memset(homescreen, 0, sizeof *homescreen); |
| 235 | |
Scott Moreau | fe89f07 | 2012-07-11 20:57:15 -0600 | [diff] [blame] | 236 | homescreen->window = window_create_custom(tablet->display); |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 237 | homescreen->widget = |
| 238 | window_add_widget(homescreen->window, homescreen); |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 239 | window_set_user_data(homescreen->window, homescreen); |
| 240 | window_set_title(homescreen->window, "homescreen"); |
| 241 | widget_set_redraw_handler(homescreen->widget, homescreen_draw); |
| 242 | |
| 243 | return homescreen; |
| 244 | } |
| 245 | |
| 246 | static struct lockscreen * |
| 247 | lockscreen_create(struct tablet *tablet) |
| 248 | { |
| 249 | struct lockscreen *lockscreen; |
| 250 | |
| 251 | lockscreen = malloc (sizeof *lockscreen); |
| 252 | memset(lockscreen, 0, sizeof *lockscreen); |
| 253 | |
Scott Moreau | fe89f07 | 2012-07-11 20:57:15 -0600 | [diff] [blame] | 254 | lockscreen->window = window_create_custom(tablet->display); |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 255 | lockscreen->widget = |
| 256 | window_add_widget(lockscreen->window, lockscreen); |
| 257 | window_set_user_data(lockscreen->window, lockscreen); |
| 258 | window_set_title(lockscreen->window, "lockscreen"); |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 259 | widget_set_redraw_handler(lockscreen->widget, lockscreen_draw); |
| 260 | widget_set_button_handler(lockscreen->widget, |
| 261 | lockscreen_button_handler); |
| 262 | |
| 263 | return lockscreen; |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 264 | } |
| 265 | |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 266 | static void |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 267 | show_lockscreen(void *data, struct tablet_shell *tablet_shell) |
| 268 | { |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 269 | struct tablet *tablet = data; |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 270 | |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 271 | tablet->lockscreen = lockscreen_create(tablet); |
| 272 | tablet_shell_set_lockscreen(tablet->tablet_shell, |
| 273 | window_get_wl_surface(tablet->lockscreen->window)); |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 274 | |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 275 | widget_schedule_resize(tablet->lockscreen->widget, |
| 276 | tablet->allocation.width, |
| 277 | tablet->allocation.height); |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static void |
| 281 | show_switcher(void *data, struct tablet_shell *tablet_shell) |
| 282 | { |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 283 | struct tablet *tablet = data; |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 284 | |
Scott Moreau | fe89f07 | 2012-07-11 20:57:15 -0600 | [diff] [blame] | 285 | tablet->switcher = window_create_custom(tablet->display); |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 286 | window_set_user_data(tablet->switcher, tablet); |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 287 | tablet_shell_set_switcher(tablet->tablet_shell, |
| 288 | window_get_wl_surface(tablet->switcher)); |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | static void |
| 292 | hide_switcher(void *data, struct tablet_shell *tablet_shell) |
| 293 | { |
| 294 | } |
| 295 | |
| 296 | static const struct tablet_shell_listener tablet_shell_listener = { |
| 297 | show_lockscreen, |
| 298 | show_switcher, |
| 299 | hide_switcher |
| 300 | }; |
| 301 | |
Alex Wu | 5b72a4d | 2012-06-18 16:52:31 +0800 | [diff] [blame] | 302 | static int |
| 303 | launcher_enter_handler(struct widget *widget, struct input *input, |
| 304 | float x, float y, void *data) |
| 305 | { |
| 306 | struct launcher *launcher = data; |
| 307 | |
| 308 | launcher->focused = 1; |
| 309 | widget_schedule_redraw(widget); |
| 310 | |
| 311 | return CURSOR_LEFT_PTR; |
| 312 | } |
| 313 | |
| 314 | static void |
| 315 | launcher_leave_handler(struct widget *widget, |
| 316 | struct input *input, void *data) |
| 317 | { |
| 318 | struct launcher *launcher = data; |
| 319 | |
| 320 | launcher->focused = 0; |
| 321 | widget_schedule_redraw(widget); |
| 322 | } |
| 323 | |
| 324 | static void |
| 325 | launcher_activate(struct launcher *widget) |
| 326 | { |
| 327 | pid_t pid; |
| 328 | |
| 329 | pid = fork(); |
| 330 | if (pid < 0) { |
| 331 | fprintf(stderr, "fork failed: %m\n"); |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | if (pid) |
| 336 | return; |
| 337 | |
| 338 | if (execl(widget->path, widget->path, NULL) < 0) { |
| 339 | fprintf(stderr, "execl '%s' failed: %m\n", widget->path); |
| 340 | exit(1); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | static void |
| 345 | launcher_button_handler(struct widget *widget, |
| 346 | struct input *input, uint32_t time, |
| 347 | uint32_t button, |
| 348 | enum wl_pointer_button_state state, void *data) |
| 349 | { |
| 350 | struct launcher *launcher; |
| 351 | |
| 352 | launcher = widget_get_user_data(widget); |
| 353 | widget_schedule_redraw(widget); |
| 354 | if (state == WL_POINTER_BUTTON_STATE_RELEASED) { |
| 355 | launcher_activate(launcher); |
| 356 | launcher->pressed = 0; |
| 357 | } else if (state == WL_POINTER_BUTTON_STATE_PRESSED) |
| 358 | launcher->pressed = 1; |
| 359 | } |
| 360 | |
| 361 | static void |
| 362 | launcher_redraw_handler(struct widget *widget, void *data) |
| 363 | { |
| 364 | struct launcher *launcher = data; |
| 365 | cairo_surface_t *surface; |
| 366 | struct rectangle allocation; |
| 367 | cairo_t *cr; |
| 368 | |
| 369 | surface = window_get_surface(launcher->homescreen->window); |
| 370 | cr = cairo_create(surface); |
| 371 | |
| 372 | widget_get_allocation(widget, &allocation); |
| 373 | if (launcher->pressed) { |
| 374 | allocation.x++; |
| 375 | allocation.y++; |
| 376 | } |
| 377 | |
| 378 | cairo_set_source_surface(cr, launcher->icon, |
| 379 | allocation.x, allocation.y); |
| 380 | cairo_paint(cr); |
| 381 | |
| 382 | if (launcher->focused) { |
| 383 | cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4); |
| 384 | cairo_mask_surface(cr, launcher->icon, |
| 385 | allocation.x, allocation.y); |
| 386 | } |
| 387 | |
| 388 | cairo_destroy(cr); |
| 389 | } |
| 390 | |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 391 | static void |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 392 | tablet_shell_add_launcher(struct tablet *tablet, |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 393 | const char *icon, const char *path) |
| 394 | { |
| 395 | struct launcher *launcher; |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 396 | struct homescreen *homescreen = tablet->homescreen; |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 397 | |
| 398 | launcher = malloc(sizeof *launcher); |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 399 | launcher->icon = load_cairo_surface(icon); |
Juan Zhao | 8634f51 | 2012-06-20 19:29:27 -0700 | [diff] [blame] | 400 | if ( !launcher->icon || |
| 401 | cairo_surface_status (launcher->icon) != CAIRO_STATUS_SUCCESS) { |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 402 | fprintf(stderr, "couldn't load %s\n", icon); |
| 403 | free(launcher); |
| 404 | return; |
| 405 | } |
Rob Bradford | c48c34d | 2013-07-26 16:29:42 +0100 | [diff] [blame^] | 406 | launcher->path = strdup(path); |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 407 | |
Alex Wu | 5b72a4d | 2012-06-18 16:52:31 +0800 | [diff] [blame] | 408 | launcher->homescreen = homescreen; |
| 409 | launcher->widget = widget_add_widget(homescreen->widget, launcher); |
| 410 | widget_set_enter_handler(launcher->widget, |
| 411 | launcher_enter_handler); |
| 412 | widget_set_leave_handler(launcher->widget, |
| 413 | launcher_leave_handler); |
| 414 | widget_set_button_handler(launcher->widget, |
| 415 | launcher_button_handler); |
| 416 | widget_set_redraw_handler(launcher->widget, |
| 417 | launcher_redraw_handler); |
| 418 | |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 419 | wl_list_insert(&homescreen->launcher_list, &launcher->link); |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | static void |
| 423 | launcher_section_done(void *data) |
| 424 | { |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 425 | struct tablet *tablet = data; |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 426 | |
| 427 | if (key_launcher_icon == NULL || key_launcher_path == NULL) { |
| 428 | fprintf(stderr, "invalid launcher section\n"); |
| 429 | return; |
| 430 | } |
| 431 | |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 432 | tablet_shell_add_launcher(tablet, key_launcher_icon, key_launcher_path); |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 433 | |
| 434 | free(key_launcher_icon); |
| 435 | key_launcher_icon = NULL; |
| 436 | free(key_launcher_path); |
| 437 | key_launcher_path = NULL; |
| 438 | } |
| 439 | |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 440 | static void |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 441 | global_handler(struct display *display, uint32_t name, |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 442 | const char *interface, uint32_t version, void *data) |
| 443 | { |
| 444 | struct tablet *tablet = data; |
| 445 | |
| 446 | if (!strcmp(interface, "tablet_shell")) { |
| 447 | tablet->tablet_shell = |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 448 | display_bind(display, name, |
| 449 | &tablet_shell_interface, 1); |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 450 | tablet_shell_add_listener(tablet->tablet_shell, |
| 451 | &tablet_shell_listener, tablet); |
| 452 | } |
| 453 | } |
| 454 | |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 455 | int main(int argc, char *argv[]) |
| 456 | { |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 457 | struct tablet tablet = { 0 }; |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 458 | struct display *display; |
Ossama Othman | a50e6e4 | 2013-05-14 09:48:26 -0700 | [diff] [blame] | 459 | int config_fd; |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 460 | struct output *output; |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 461 | |
Kristian Høgsberg | 4172f66 | 2013-02-20 15:27:49 -0500 | [diff] [blame] | 462 | display = display_create(&argc, argv); |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 463 | if (display == NULL) { |
| 464 | fprintf(stderr, "failed to create display: %m\n"); |
| 465 | return -1; |
| 466 | } |
| 467 | |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 468 | tablet.display = display; |
| 469 | |
Kristian Høgsberg | fa80e11 | 2012-10-10 21:34:26 -0400 | [diff] [blame] | 470 | display_set_user_data(tablet.display, &tablet); |
| 471 | display_set_global_handler(tablet.display, global_handler); |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 472 | |
| 473 | tablet.homescreen = homescreen_create(&tablet); |
| 474 | tablet_shell_set_homescreen(tablet.tablet_shell, |
| 475 | window_get_wl_surface(tablet.homescreen->window)); |
Tiago Vignatti | b67c91d | 2013-04-09 11:48:06 -0300 | [diff] [blame] | 476 | |
| 477 | wl_display_roundtrip (display_get_display(tablet.display)); |
| 478 | |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 479 | wl_list_init(&tablet.homescreen->launcher_list); |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 480 | |
Ossama Othman | a50e6e4 | 2013-05-14 09:48:26 -0700 | [diff] [blame] | 481 | config_fd = open_config_file("weston.ini"); |
| 482 | parse_config_file(config_fd, |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 483 | config_sections, ARRAY_LENGTH(config_sections), |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 484 | &tablet); |
Ossama Othman | a50e6e4 | 2013-05-14 09:48:26 -0700 | [diff] [blame] | 485 | close(config_fd); |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 486 | |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 487 | signal(SIGCHLD, sigchild_handler); |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 488 | |
Tiago Vignatti | 0a38611 | 2012-03-28 13:04:02 +0300 | [diff] [blame] | 489 | output = display_get_output(tablet.display); |
| 490 | output_get_allocation(output, &tablet.allocation); |
| 491 | widget_schedule_resize(tablet.homescreen->widget, |
| 492 | tablet.allocation.width, |
| 493 | tablet.allocation.height); |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 494 | display_run(display); |
| 495 | |
| 496 | return 0; |
| 497 | } |