2 * Copyright © 2011 Intel Corporation
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 #include <linux/input.h>
30 #include "compositor.h"
31 #include "tablet-shell-server-protocol.h"
34 * TODO: Don't fade back from black until we've received a lockscreen
47 struct wl_resource resource;
49 struct wl_listener lock_listener;
50 struct wl_listener unlock_listener;
51 struct wl_listener destroy_listener;
53 struct weston_compositor *compositor;
54 struct weston_process process;
55 struct wl_client *client;
57 struct weston_surface *surface;
59 struct weston_surface *lockscreen_surface;
60 struct wl_listener lockscreen_listener;
61 struct weston_layer lockscreen_layer;
63 struct weston_layer application_layer;
65 struct weston_surface *home_surface;
66 struct weston_layer homescreen_layer;
68 struct weston_surface *switcher_surface;
69 struct wl_listener switcher_listener;
71 struct tablet_client *current_client;
73 int state, previous_state;
74 int long_press_active;
75 struct wl_event_source *long_press_source;
78 struct tablet_client {
79 struct wl_resource resource;
80 struct tablet_shell *shell;
81 struct wl_client *client;
82 struct weston_surface *surface;
87 tablet_shell_destroy(struct wl_listener *listener, void *data);
89 static struct tablet_shell *
90 get_shell(struct weston_compositor *compositor)
92 struct wl_listener *l;
94 l = wl_signal_get(&compositor->destroy_signal, tablet_shell_destroy);
96 return container_of(l, struct tablet_shell, destroy_listener);
102 tablet_shell_sigchld(struct weston_process *process, int status)
104 struct tablet_shell *shell =
105 container_of(process, struct tablet_shell, process);
107 shell->process.pid = 0;
109 weston_log("weston-tablet-shell crashed, exit code %d\n", status);
113 tablet_shell_set_state(struct tablet_shell *shell, int state)
115 static const char *states[] = {
116 "STARTING", "LOCKED", "HOME", "SWITCHER", "TASK"
119 weston_log("switching to state %s (from %s)\n",
120 states[state], states[shell->state]);
121 shell->previous_state = shell->state;
122 shell->state = state;
126 tablet_shell_surface_configure(struct weston_surface *surface,
127 int32_t sx, int32_t sy, int32_t width, int32_t height)
129 struct tablet_shell *shell = get_shell(surface->compositor);
131 if (weston_surface_is_mapped(surface) || width == 0)
134 weston_surface_configure(surface, 0, 0, width, height);
136 if (surface == shell->lockscreen_surface) {
137 wl_list_insert(&shell->lockscreen_layer.surface_list,
138 &surface->layer_link);
139 } else if (surface == shell->switcher_surface) {
141 } else if (surface == shell->home_surface) {
142 if (shell->state == STATE_STARTING) {
143 /* homescreen always visible, at the bottom */
144 wl_list_insert(&shell->homescreen_layer.surface_list,
145 &surface->layer_link);
147 tablet_shell_set_state(shell, STATE_LOCKED);
148 shell->previous_state = STATE_HOME;
149 tablet_shell_send_show_lockscreen(&shell->resource);
151 } else if (shell->current_client &&
152 shell->current_client->surface != surface &&
153 shell->current_client->client == surface->surface.resource.client) {
154 tablet_shell_set_state(shell, STATE_TASK);
155 shell->current_client->surface = surface;
156 weston_zoom_run(surface, 0.3, 1.0, NULL, NULL);
157 wl_list_insert(&shell->application_layer.surface_list,
158 &surface->layer_link);
161 weston_surface_update_transform(surface);
165 handle_lockscreen_surface_destroy(struct wl_listener *listener, void *data)
167 struct tablet_shell *shell =
168 container_of(listener,
169 struct tablet_shell, lockscreen_listener);
171 shell->lockscreen_surface = NULL;
172 tablet_shell_set_state(shell, shell->previous_state);
176 tablet_shell_set_lockscreen(struct wl_client *client,
177 struct wl_resource *resource,
178 struct wl_resource *surface_resource)
180 struct tablet_shell *shell = resource->data;
181 struct weston_surface *es = surface_resource->data;
183 weston_surface_set_position(es, 0, 0);
184 shell->lockscreen_surface = es;
185 shell->lockscreen_surface->configure = tablet_shell_surface_configure;
186 shell->lockscreen_listener.notify = handle_lockscreen_surface_destroy;
187 wl_signal_add(&es->surface.resource.destroy_signal,
188 &shell->lockscreen_listener);
192 handle_switcher_surface_destroy(struct wl_listener *listener, void *data)
194 struct tablet_shell *shell =
195 container_of(listener,
196 struct tablet_shell, switcher_listener);
198 shell->switcher_surface = NULL;
199 if (shell->state != STATE_LOCKED)
200 tablet_shell_set_state(shell, shell->previous_state);
204 tablet_shell_set_switcher(struct wl_client *client,
205 struct wl_resource *resource,
206 struct wl_resource *surface_resource)
208 struct tablet_shell *shell = resource->data;
209 struct weston_surface *es = surface_resource->data;
211 /* FIXME: Switcher should be centered and the compositor
212 * should do the tinting of the background. With the cache
213 * layer idea, we should be able to hit the framerate on the
215 shell->switcher_surface = es;
216 weston_surface_set_position(shell->switcher_surface, 0, 0);
218 shell->switcher_listener.notify = handle_switcher_surface_destroy;
219 wl_signal_add(&es->surface.resource.destroy_signal,
220 &shell->switcher_listener);
224 tablet_shell_set_homescreen(struct wl_client *client,
225 struct wl_resource *resource,
226 struct wl_resource *surface_resource)
228 struct tablet_shell *shell = resource->data;
230 shell->home_surface = surface_resource->data;
231 shell->home_surface->configure = tablet_shell_surface_configure;
233 weston_surface_set_position(shell->home_surface, 0, 0);
237 minimize_zoom_done(struct weston_surface_animation *zoom, void *data)
239 struct tablet_shell *shell = data;
240 struct weston_compositor *compositor = shell->compositor;
241 struct weston_seat *seat;
243 wl_list_for_each(seat, &compositor->seat_list, link)
244 weston_surface_activate(shell->home_surface, seat);
248 tablet_shell_switch_to(struct tablet_shell *shell,
249 struct weston_surface *surface)
251 struct weston_compositor *compositor = shell->compositor;
252 struct weston_seat *seat;
253 struct weston_surface *current;
255 if (shell->state == STATE_SWITCHER) {
256 wl_list_remove(&shell->switcher_listener.link);
257 shell->switcher_surface = NULL;
260 if (surface == shell->home_surface) {
261 tablet_shell_set_state(shell, STATE_HOME);
263 if (shell->current_client && shell->current_client->surface) {
264 current = shell->current_client->surface;
265 weston_zoom_run(current, 1.0, 0.3,
266 minimize_zoom_done, shell);
269 fprintf(stderr, "switch to %p\n", surface);
270 wl_list_for_each(seat, &compositor->seat_list, link)
271 weston_surface_activate(surface, seat);
272 tablet_shell_set_state(shell, STATE_TASK);
273 weston_zoom_run(surface, 0.3, 1.0, NULL, NULL);
278 tablet_shell_show_grid(struct wl_client *client,
279 struct wl_resource *resource,
280 struct wl_resource *surface_resource)
282 struct tablet_shell *shell = resource->data;
283 struct weston_surface *es = surface_resource->data;
285 tablet_shell_switch_to(shell, es);
289 tablet_shell_show_panels(struct wl_client *client,
290 struct wl_resource *resource,
291 struct wl_resource *surface_resource)
293 struct tablet_shell *shell = resource->data;
294 struct weston_surface *es = surface_resource->data;
296 tablet_shell_switch_to(shell, es);
300 destroy_tablet_client(struct wl_resource *resource)
302 struct tablet_client *tablet_client =
303 container_of(resource, struct tablet_client, resource);
305 free(tablet_client->name);
310 tablet_client_destroy(struct wl_client *client,
311 struct wl_resource *resource)
313 wl_resource_destroy(resource);
317 tablet_client_activate(struct wl_client *client, struct wl_resource *resource)
319 struct tablet_client *tablet_client = resource->data;
320 struct tablet_shell *shell = tablet_client->shell;
322 shell->current_client = tablet_client;
323 if (!tablet_client->surface)
326 tablet_shell_switch_to(shell, tablet_client->surface);
329 static const struct tablet_client_interface tablet_client_implementation = {
330 tablet_client_destroy,
331 tablet_client_activate
335 tablet_shell_create_client(struct wl_client *client,
336 struct wl_resource *resource,
337 uint32_t id, const char *name, int fd)
339 struct tablet_shell *shell = resource->data;
340 struct weston_compositor *compositor = shell->compositor;
341 struct tablet_client *tablet_client;
343 tablet_client = malloc(sizeof *tablet_client);
344 if (tablet_client == NULL) {
345 wl_resource_post_no_memory(resource);
349 tablet_client->client = wl_client_create(compositor->wl_display, fd);
350 tablet_client->shell = shell;
351 tablet_client->name = strdup(name);
353 tablet_client->resource.destroy = destroy_tablet_client;
354 tablet_client->resource.object.id = id;
355 tablet_client->resource.object.interface =
356 &tablet_client_interface;
357 tablet_client->resource.object.implementation =
358 (void (**)(void)) &tablet_client_implementation;
360 wl_client_add_resource(client, &tablet_client->resource);
361 tablet_client->surface = NULL;
362 shell->current_client = tablet_client;
364 weston_log("created client %p, id %d, name %s, fd %d\n",
365 tablet_client->client, id, name, fd);
368 static const struct tablet_shell_interface tablet_shell_implementation = {
369 tablet_shell_set_lockscreen,
370 tablet_shell_set_switcher,
371 tablet_shell_set_homescreen,
372 tablet_shell_show_grid,
373 tablet_shell_show_panels,
374 tablet_shell_create_client
378 launch_ux_daemon(struct tablet_shell *shell)
380 const char *shell_exe = LIBEXECDIR "/weston-tablet-shell";
382 shell->client = weston_client_launch(shell->compositor,
384 shell_exe, tablet_shell_sigchld);
388 toggle_switcher(struct tablet_shell *shell)
390 switch (shell->state) {
392 tablet_shell_send_hide_switcher(&shell->resource);
395 tablet_shell_send_show_switcher(&shell->resource);
396 tablet_shell_set_state(shell, STATE_SWITCHER);
402 tablet_shell_lock(struct wl_listener *listener, void *data)
404 struct tablet_shell *shell =
405 container_of(listener, struct tablet_shell, lock_listener);
407 if (shell->state == STATE_LOCKED)
409 if (shell->state == STATE_SWITCHER)
410 tablet_shell_send_hide_switcher(&shell->resource);
412 tablet_shell_send_show_lockscreen(&shell->resource);
413 tablet_shell_set_state(shell, STATE_LOCKED);
417 tablet_shell_unlock(struct wl_listener *listener, void *data)
419 struct tablet_shell *shell =
420 container_of(listener, struct tablet_shell, lock_listener);
422 weston_compositor_wake(shell->compositor);
426 go_home(struct tablet_shell *shell, struct weston_seat *seat)
428 if (shell->state == STATE_SWITCHER)
429 tablet_shell_send_hide_switcher(&shell->resource);
431 weston_surface_activate(shell->home_surface, seat);
433 tablet_shell_set_state(shell, STATE_HOME);
437 long_press_handler(void *data)
439 struct tablet_shell *shell = data;
441 shell->long_press_active = 0;
442 toggle_switcher(shell);
448 menu_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
450 struct tablet_shell *shell = data;
452 if (shell->state == STATE_LOCKED)
455 toggle_switcher(shell);
459 home_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
461 struct tablet_shell *shell = data;
463 if (shell->state == STATE_LOCKED)
467 wl_event_source_timer_update(shell->long_press_source, 500);
468 shell->long_press_active = 1;
469 } else if (shell->long_press_active) {
470 /* This code has never been run ... */
471 wl_event_source_timer_update(shell->long_press_source, 0);
472 shell->long_press_active = 0;
474 switch (shell->state) {
477 toggle_switcher(shell);
480 go_home(shell, (struct weston_seat *) seat);
487 destroy_tablet_shell(struct wl_resource *resource)
492 bind_tablet_shell(struct wl_client *client, void *data, uint32_t version,
495 struct tablet_shell *shell = data;
497 if (shell->client != client)
498 /* Throw an error or just let the client fail when it
499 * tries to access the object?. */
502 shell->resource.object.id = id;
503 shell->resource.object.interface = &tablet_shell_interface;
504 shell->resource.object.implementation =
505 (void (**)(void)) &tablet_shell_implementation;
506 shell->resource.client = client;
507 shell->resource.data = shell;
508 shell->resource.destroy = destroy_tablet_shell;
510 wl_client_add_resource(client, &shell->resource);
514 tablet_shell_destroy(struct wl_listener *listener, void *data)
516 struct tablet_shell *shell =
517 container_of(listener, struct tablet_shell, destroy_listener);
519 if (shell->home_surface)
520 shell->home_surface->configure = NULL;
522 if (shell->lockscreen_surface)
523 shell->lockscreen_surface->configure = NULL;
525 wl_event_source_remove(shell->long_press_source);
530 module_init(struct weston_compositor *compositor,
531 int *argc, char *argv[], const char *config_file)
533 struct tablet_shell *shell;
534 struct wl_event_loop *loop;
536 shell = malloc(sizeof *shell);
540 memset(shell, 0, sizeof *shell);
541 shell->compositor = compositor;
543 shell->destroy_listener.notify = tablet_shell_destroy;
544 wl_signal_add(&compositor->destroy_signal, &shell->destroy_listener);
545 shell->lock_listener.notify = tablet_shell_lock;
546 wl_signal_add(&compositor->idle_signal, &shell->lock_listener);
547 shell->unlock_listener.notify = tablet_shell_unlock;
548 wl_signal_add(&compositor->wake_signal, &shell->unlock_listener);
550 /* FIXME: This will make the object available to all clients. */
551 wl_display_add_global(compositor->wl_display, &tablet_shell_interface,
552 shell, bind_tablet_shell);
554 loop = wl_display_get_event_loop(compositor->wl_display);
555 shell->long_press_source =
556 wl_event_loop_add_timer(loop, long_press_handler, shell);
558 weston_compositor_add_key_binding(compositor, KEY_LEFTMETA, 0,
559 home_key_binding, shell);
560 weston_compositor_add_key_binding(compositor, KEY_RIGHTMETA, 0,
561 home_key_binding, shell);
562 weston_compositor_add_key_binding(compositor, KEY_LEFTMETA,
563 MODIFIER_SUPER, home_key_binding,
565 weston_compositor_add_key_binding(compositor, KEY_RIGHTMETA,
566 MODIFIER_SUPER, home_key_binding,
568 weston_compositor_add_key_binding(compositor, KEY_COMPOSE, 0,
569 menu_key_binding, shell);
571 weston_layer_init(&shell->homescreen_layer,
572 &compositor->cursor_layer.link);
573 weston_layer_init(&shell->application_layer,
574 &compositor->cursor_layer.link);
575 weston_layer_init(&shell->lockscreen_layer,
576 &compositor->cursor_layer.link);
577 launch_ux_daemon(shell);
579 tablet_shell_set_state(shell, STATE_STARTING);