Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 Intel Corporation |
| 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 | |
| 28 | #include "window.h" |
| 29 | #include "cairo-util.h" |
Kristian Høgsberg | 9b935c8 | 2011-12-08 12:44:27 -0500 | [diff] [blame] | 30 | #include "../shared/config-parser.h" |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 31 | |
| 32 | #include "tablet-shell-client-protocol.h" |
| 33 | |
| 34 | struct tablet_shell { |
| 35 | struct display *display; |
| 36 | struct tablet_shell *tablet_shell; |
| 37 | struct rectangle allocation; |
| 38 | struct window *lockscreen; |
| 39 | struct window *switcher; |
| 40 | struct window *homescreen; |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 41 | struct wl_list launcher_list; |
| 42 | }; |
| 43 | |
| 44 | struct launcher { |
| 45 | cairo_surface_t *icon; |
| 46 | char *path; |
| 47 | struct wl_list link; |
| 48 | }; |
| 49 | |
| 50 | static char *key_lockscreen_icon; |
| 51 | static char *key_lockscreen_background; |
| 52 | static char *key_homescreen_background; |
| 53 | static char *key_launcher_icon; |
| 54 | static char *key_launcher_path; |
| 55 | static void launcher_section_done(void *data); |
| 56 | |
| 57 | static const struct config_key lockscreen_config_keys[] = { |
| 58 | { "icon", CONFIG_KEY_STRING, &key_lockscreen_icon }, |
| 59 | { "background", CONFIG_KEY_STRING, &key_lockscreen_background }, |
| 60 | }; |
| 61 | |
| 62 | static const struct config_key homescreen_config_keys[] = { |
| 63 | { "background", CONFIG_KEY_STRING, &key_homescreen_background }, |
| 64 | }; |
| 65 | |
| 66 | static const struct config_key launcher_config_keys[] = { |
| 67 | { "icon", CONFIG_KEY_STRING, &key_launcher_icon }, |
| 68 | { "path", CONFIG_KEY_STRING, &key_launcher_path }, |
| 69 | }; |
| 70 | |
| 71 | static const struct config_section config_sections[] = { |
| 72 | { "lockscreen", |
| 73 | lockscreen_config_keys, ARRAY_LENGTH(lockscreen_config_keys) }, |
| 74 | { "homescreen", |
| 75 | homescreen_config_keys, ARRAY_LENGTH(homescreen_config_keys) }, |
| 76 | { "launcher", |
| 77 | launcher_config_keys, ARRAY_LENGTH(launcher_config_keys), |
| 78 | launcher_section_done } |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | static void |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 82 | paint_background(cairo_t *cr, const char *path, struct rectangle *allocation) |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 83 | { |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 84 | cairo_surface_t *image = NULL; |
| 85 | cairo_pattern_t *pattern; |
| 86 | cairo_matrix_t matrix; |
| 87 | double sx, sy; |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 88 | |
| 89 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 90 | if (path) |
Kristian Høgsberg | d654876 | 2012-01-25 15:43:48 -0500 | [diff] [blame] | 91 | image = load_image(path); |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 92 | if (image) { |
| 93 | pattern = cairo_pattern_create_for_surface(image); |
| 94 | sx = (double) cairo_image_surface_get_width(image) / |
| 95 | allocation->width; |
| 96 | sy = (double) cairo_image_surface_get_height(image) / |
| 97 | allocation->height; |
| 98 | cairo_matrix_init_scale(&matrix, sx, sy); |
| 99 | cairo_pattern_set_matrix(pattern, &matrix); |
| 100 | cairo_set_source(cr, pattern); |
| 101 | cairo_pattern_destroy (pattern); |
| 102 | cairo_surface_destroy(image); |
| 103 | cairo_paint(cr); |
| 104 | } else { |
| 105 | fprintf(stderr, "couldn't load backgrond image: %s\n", |
| 106 | key_lockscreen_background); |
| 107 | cairo_set_source_rgb(cr, 0.2, 0, 0); |
| 108 | cairo_paint(cr); |
| 109 | } |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 110 | } |
| 111 | |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 112 | static void |
| 113 | homescreen_draw(struct tablet_shell *shell) |
| 114 | { |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 115 | cairo_surface_t *surface; |
| 116 | struct rectangle allocation; |
| 117 | cairo_pattern_t *pattern; |
| 118 | cairo_matrix_t matrix; |
| 119 | cairo_t *cr; |
| 120 | struct launcher *launcher; |
| 121 | const int rows = 4, columns = 5, icon_width = 128, icon_height = 128; |
| 122 | int x, y, i, width, height, vmargin, hmargin, vpadding, hpadding; |
| 123 | |
Kristian Høgsberg | 5d12990 | 2012-01-10 10:49:41 -0500 | [diff] [blame] | 124 | window_create_surface(shell->homescreen); |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 125 | window_get_allocation(shell->homescreen, &allocation); |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 126 | surface = window_get_surface(shell->homescreen); |
| 127 | cr = cairo_create(surface); |
| 128 | |
| 129 | paint_background(cr, key_homescreen_background, &allocation); |
| 130 | |
| 131 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 132 | |
| 133 | width = allocation.width - columns * icon_width; |
| 134 | hpadding = width / (columns + 1); |
| 135 | hmargin = (width - hpadding * (columns - 1)) / 2; |
| 136 | |
| 137 | height = allocation.height - rows * icon_height; |
| 138 | vpadding = height / (rows + 1); |
| 139 | vmargin = (height - vpadding * (rows - 1)) / 2; |
| 140 | |
| 141 | x = hmargin; |
| 142 | y = vmargin; |
| 143 | i = 0; |
| 144 | |
| 145 | wl_list_for_each(launcher, &shell->launcher_list, link) { |
| 146 | pattern = cairo_pattern_create_for_surface(launcher->icon); |
| 147 | cairo_matrix_init_scale(&matrix, 2.0, 2.0); |
| 148 | cairo_matrix_translate(&matrix, -x, -y); |
| 149 | cairo_pattern_set_matrix(pattern, &matrix); |
| 150 | cairo_pattern_set_extend(pattern, CAIRO_EXTEND_NONE); |
| 151 | cairo_set_source(cr, pattern); |
| 152 | cairo_pattern_destroy(pattern); |
| 153 | cairo_paint(cr); |
| 154 | x += icon_width + hpadding; |
| 155 | i++; |
| 156 | if (i == columns) { |
| 157 | x = hmargin; |
| 158 | y += icon_height + vpadding; |
| 159 | i = 0; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | cairo_surface_flush(surface); |
| 164 | cairo_surface_destroy(surface); |
| 165 | window_flush(shell->homescreen); |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | |
| 169 | static void |
| 170 | lockscreen_draw(struct tablet_shell *shell) |
| 171 | { |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 172 | cairo_surface_t *surface; |
| 173 | cairo_surface_t *icon; |
| 174 | struct rectangle allocation; |
| 175 | cairo_t *cr; |
| 176 | int width, height; |
| 177 | |
Kristian Høgsberg | 5d12990 | 2012-01-10 10:49:41 -0500 | [diff] [blame] | 178 | window_create_surface(shell->lockscreen); |
Kristian Høgsberg | bb97700 | 2012-01-10 19:11:42 -0500 | [diff] [blame] | 179 | window_get_allocation(shell->lockscreen, &allocation); |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 180 | surface = window_get_surface(shell->lockscreen); |
| 181 | cr = cairo_create(surface); |
| 182 | |
| 183 | paint_background(cr, key_lockscreen_background, &allocation); |
| 184 | |
| 185 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 186 | icon = cairo_image_surface_create_from_png(key_lockscreen_icon); |
| 187 | width = cairo_image_surface_get_width(icon); |
| 188 | height = cairo_image_surface_get_height(icon); |
| 189 | cairo_set_source_surface(cr, icon, |
| 190 | allocation.x + (allocation.width - width) / 2, |
| 191 | allocation.y + (allocation.height - height) / 2); |
| 192 | cairo_paint(cr); |
| 193 | cairo_surface_destroy(icon); |
| 194 | |
| 195 | cairo_surface_flush(surface); |
| 196 | cairo_surface_destroy(surface); |
| 197 | window_flush(shell->lockscreen); |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 198 | } |
| 199 | |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 200 | static void |
Kristian Høgsberg | a8a0db3 | 2012-01-09 11:12:05 -0500 | [diff] [blame] | 201 | lockscreen_button_handler(struct widget *widget, |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 202 | struct input *input, uint32_t time, |
| 203 | int button, int state, void *data) |
| 204 | { |
Kristian Høgsberg | b67e94b | 2012-01-10 12:23:19 -0500 | [diff] [blame] | 205 | struct tablet_shell *shell = data; |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 206 | |
| 207 | window_destroy(shell->lockscreen); |
| 208 | shell->lockscreen = NULL; |
| 209 | } |
| 210 | |
| 211 | static void |
| 212 | show_lockscreen(void *data, struct tablet_shell *tablet_shell) |
| 213 | { |
| 214 | struct tablet_shell *shell = data; |
| 215 | |
Kristian Høgsberg | 009ac0a | 2012-01-31 15:24:48 -0500 | [diff] [blame] | 216 | shell->lockscreen = window_create(shell->display); |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 217 | window_set_user_data(shell->lockscreen, shell); |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 218 | window_set_custom(shell->lockscreen); |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 219 | |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 220 | tablet_shell_set_lockscreen(shell->tablet_shell, |
| 221 | window_get_wl_surface(shell->lockscreen)); |
| 222 | lockscreen_draw(shell); |
| 223 | } |
| 224 | |
| 225 | static void |
| 226 | show_switcher(void *data, struct tablet_shell *tablet_shell) |
| 227 | { |
| 228 | struct tablet_shell *shell = data; |
| 229 | |
Kristian Høgsberg | 009ac0a | 2012-01-31 15:24:48 -0500 | [diff] [blame] | 230 | shell->switcher = window_create(shell->display); |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 231 | window_set_user_data(shell->switcher, shell); |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 232 | window_set_custom(shell->switcher); |
| 233 | tablet_shell_set_switcher(shell->tablet_shell, |
| 234 | window_get_wl_surface(shell->switcher)); |
| 235 | } |
| 236 | |
| 237 | static void |
| 238 | hide_switcher(void *data, struct tablet_shell *tablet_shell) |
| 239 | { |
| 240 | } |
| 241 | |
| 242 | static const struct tablet_shell_listener tablet_shell_listener = { |
| 243 | show_lockscreen, |
| 244 | show_switcher, |
| 245 | hide_switcher |
| 246 | }; |
| 247 | |
| 248 | static struct tablet_shell * |
| 249 | tablet_shell_create(struct display *display, uint32_t id) |
| 250 | { |
| 251 | struct tablet_shell *shell; |
| 252 | struct output *output; |
| 253 | |
| 254 | shell = malloc(sizeof *shell); |
| 255 | |
| 256 | shell->display = display; |
| 257 | shell->tablet_shell = |
| 258 | wl_display_bind(display_get_display(display), |
| 259 | id, &tablet_shell_interface); |
| 260 | tablet_shell_add_listener(shell->tablet_shell, |
| 261 | &tablet_shell_listener, shell); |
| 262 | output = display_get_output(display); |
| 263 | output_get_allocation(output, &shell->allocation); |
| 264 | |
Kristian Høgsberg | 009ac0a | 2012-01-31 15:24:48 -0500 | [diff] [blame] | 265 | shell->homescreen = window_create(display); |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 266 | window_set_user_data(shell->homescreen, shell); |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 267 | window_set_custom(shell->homescreen); |
| 268 | |
| 269 | tablet_shell_set_homescreen(shell->tablet_shell, |
| 270 | window_get_wl_surface(shell->homescreen)); |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 271 | wl_list_init(&shell->launcher_list); |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 272 | |
| 273 | return shell; |
| 274 | } |
| 275 | |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 276 | static void |
| 277 | tablet_shell_add_launcher(struct tablet_shell *shell, |
| 278 | const char *icon, const char *path) |
| 279 | { |
| 280 | struct launcher *launcher; |
| 281 | |
| 282 | launcher = malloc(sizeof *launcher); |
| 283 | launcher->path = strdup(path); |
| 284 | launcher->icon = cairo_image_surface_create_from_png(icon); |
| 285 | if (cairo_surface_status (launcher->icon) != CAIRO_STATUS_SUCCESS) { |
| 286 | fprintf(stderr, "couldn't load %s\n", icon); |
| 287 | free(launcher); |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | wl_list_insert(&shell->launcher_list, &launcher->link); |
| 292 | } |
| 293 | |
| 294 | static void |
| 295 | launcher_section_done(void *data) |
| 296 | { |
| 297 | struct tablet_shell *shell = data; |
| 298 | |
| 299 | if (key_launcher_icon == NULL || key_launcher_path == NULL) { |
| 300 | fprintf(stderr, "invalid launcher section\n"); |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | tablet_shell_add_launcher(shell, key_launcher_icon, key_launcher_path); |
| 305 | |
| 306 | free(key_launcher_icon); |
| 307 | key_launcher_icon = NULL; |
| 308 | free(key_launcher_path); |
| 309 | key_launcher_path = NULL; |
| 310 | } |
| 311 | |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 312 | int main(int argc, char *argv[]) |
| 313 | { |
| 314 | struct display *display; |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 315 | char *config_file; |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 316 | uint32_t id; |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 317 | struct tablet_shell *shell; |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 318 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame^] | 319 | display = display_create(argc, argv); |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 320 | if (display == NULL) { |
| 321 | fprintf(stderr, "failed to create display: %m\n"); |
| 322 | return -1; |
| 323 | } |
| 324 | |
| 325 | wl_display_roundtrip(display_get_display(display)); |
| 326 | id = wl_display_get_global(display_get_display(display), |
| 327 | "tablet_shell", 1); |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 328 | shell = tablet_shell_create(display, id); |
| 329 | |
Kristian Høgsberg | 9724b51 | 2012-01-03 14:35:49 -0500 | [diff] [blame] | 330 | config_file = config_file_path("weston-tablet-shell.ini"); |
Kristian Høgsberg | f033a7b | 2011-11-26 23:38:41 -0500 | [diff] [blame] | 331 | parse_config_file(config_file, |
| 332 | config_sections, ARRAY_LENGTH(config_sections), |
| 333 | shell); |
| 334 | free(config_file); |
| 335 | |
| 336 | homescreen_draw(shell); |
| 337 | |
Kristian Høgsberg | 6336e46 | 2011-11-26 17:36:23 -0500 | [diff] [blame] | 338 | display_run(display); |
| 339 | |
| 340 | return 0; |
| 341 | } |