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)
129 struct tablet_shell *shell = get_shell(surface->compositor);
130 int32_t width, height;
132 if (weston_surface_is_mapped(surface))
135 width = surface->buffer->width;
136 height = surface->buffer->height;
138 weston_surface_configure(surface, 0, 0, width, height);
140 if (surface == shell->lockscreen_surface) {
141 wl_list_insert(&shell->lockscreen_layer.surface_list,
142 &surface->layer_link);
143 } else if (surface == shell->switcher_surface) {
145 } else if (surface == shell->home_surface) {
146 if (shell->state == STATE_STARTING) {
147 /* homescreen always visible, at the bottom */
148 wl_list_insert(&shell->homescreen_layer.surface_list,
149 &surface->layer_link);
151 tablet_shell_set_state(shell, STATE_LOCKED);
152 shell->previous_state = STATE_HOME;
153 tablet_shell_send_show_lockscreen(&shell->resource);
155 } else if (shell->current_client &&
156 shell->current_client->surface != surface &&
157 shell->current_client->client == surface->surface.resource.client) {
158 tablet_shell_set_state(shell, STATE_TASK);
159 shell->current_client->surface = surface;
160 weston_zoom_run(surface, 0.3, 1.0, NULL, NULL);
161 wl_list_insert(&shell->application_layer.surface_list,
162 &surface->layer_link);
165 weston_surface_update_transform(surface);
169 handle_lockscreen_surface_destroy(struct wl_listener *listener, void *data)
171 struct tablet_shell *shell =
172 container_of(listener,
173 struct tablet_shell, lockscreen_listener);
175 shell->lockscreen_surface = NULL;
176 tablet_shell_set_state(shell, shell->previous_state);
180 tablet_shell_set_lockscreen(struct wl_client *client,
181 struct wl_resource *resource,
182 struct wl_resource *surface_resource)
184 struct tablet_shell *shell = resource->data;
185 struct weston_surface *es = surface_resource->data;
187 weston_surface_set_position(es, 0, 0);
188 shell->lockscreen_surface = es;
189 shell->lockscreen_surface->configure = tablet_shell_surface_configure;
190 shell->lockscreen_listener.notify = handle_lockscreen_surface_destroy;
191 wl_signal_add(&es->surface.resource.destroy_signal,
192 &shell->lockscreen_listener);
196 handle_switcher_surface_destroy(struct wl_listener *listener, void *data)
198 struct tablet_shell *shell =
199 container_of(listener,
200 struct tablet_shell, switcher_listener);
202 shell->switcher_surface = NULL;
203 if (shell->state != STATE_LOCKED)
204 tablet_shell_set_state(shell, shell->previous_state);
208 tablet_shell_set_switcher(struct wl_client *client,
209 struct wl_resource *resource,
210 struct wl_resource *surface_resource)
212 struct tablet_shell *shell = resource->data;
213 struct weston_surface *es = surface_resource->data;
215 /* FIXME: Switcher should be centered and the compositor
216 * should do the tinting of the background. With the cache
217 * layer idea, we should be able to hit the framerate on the
219 shell->switcher_surface = es;
220 weston_surface_set_position(shell->switcher_surface, 0, 0);
222 shell->switcher_listener.notify = handle_switcher_surface_destroy;
223 wl_signal_add(&es->surface.resource.destroy_signal,
224 &shell->switcher_listener);
228 tablet_shell_set_homescreen(struct wl_client *client,
229 struct wl_resource *resource,
230 struct wl_resource *surface_resource)
232 struct tablet_shell *shell = resource->data;
234 shell->home_surface = surface_resource->data;
235 shell->home_surface->configure = tablet_shell_surface_configure;
237 weston_surface_set_position(shell->home_surface, 0, 0);
241 minimize_zoom_done(struct weston_surface_animation *zoom, void *data)
243 struct tablet_shell *shell = data;
244 struct weston_compositor *compositor = shell->compositor;
245 struct weston_seat *seat;
247 wl_list_for_each(seat, &compositor->seat_list, link)
248 weston_surface_activate(shell->home_surface, seat);
252 tablet_shell_switch_to(struct tablet_shell *shell,
253 struct weston_surface *surface)
255 struct weston_compositor *compositor = shell->compositor;
256 struct weston_seat *seat;
257 struct weston_surface *current;
259 if (shell->state == STATE_SWITCHER) {
260 wl_list_remove(&shell->switcher_listener.link);
261 shell->switcher_surface = NULL;
264 if (surface == shell->home_surface) {
265 tablet_shell_set_state(shell, STATE_HOME);
267 if (shell->current_client && shell->current_client->surface) {
268 current = shell->current_client->surface;
269 weston_zoom_run(current, 1.0, 0.3,
270 minimize_zoom_done, shell);
273 fprintf(stderr, "switch to %p\n", surface);
274 wl_list_for_each(seat, &compositor->seat_list, link)
275 weston_surface_activate(surface, seat);
276 tablet_shell_set_state(shell, STATE_TASK);
277 weston_zoom_run(surface, 0.3, 1.0, NULL, NULL);
282 tablet_shell_show_grid(struct wl_client *client,
283 struct wl_resource *resource,
284 struct wl_resource *surface_resource)
286 struct tablet_shell *shell = resource->data;
287 struct weston_surface *es = surface_resource->data;
289 tablet_shell_switch_to(shell, es);
293 tablet_shell_show_panels(struct wl_client *client,
294 struct wl_resource *resource,
295 struct wl_resource *surface_resource)
297 struct tablet_shell *shell = resource->data;
298 struct weston_surface *es = surface_resource->data;
300 tablet_shell_switch_to(shell, es);
304 destroy_tablet_client(struct wl_resource *resource)
306 struct tablet_client *tablet_client =
307 container_of(resource, struct tablet_client, resource);
309 free(tablet_client->name);
314 tablet_client_destroy(struct wl_client *client,
315 struct wl_resource *resource)
317 wl_resource_destroy(resource);
321 tablet_client_activate(struct wl_client *client, struct wl_resource *resource)
323 struct tablet_client *tablet_client = resource->data;
324 struct tablet_shell *shell = tablet_client->shell;
326 shell->current_client = tablet_client;
327 if (!tablet_client->surface)
330 tablet_shell_switch_to(shell, tablet_client->surface);
333 static const struct tablet_client_interface tablet_client_implementation = {
334 tablet_client_destroy,
335 tablet_client_activate
339 tablet_shell_create_client(struct wl_client *client,
340 struct wl_resource *resource,
341 uint32_t id, const char *name, int fd)
343 struct tablet_shell *shell = resource->data;
344 struct weston_compositor *compositor = shell->compositor;
345 struct tablet_client *tablet_client;
347 tablet_client = malloc(sizeof *tablet_client);
348 if (tablet_client == NULL) {
349 wl_resource_post_no_memory(resource);
353 tablet_client->client = wl_client_create(compositor->wl_display, fd);
354 tablet_client->shell = shell;
355 tablet_client->name = strdup(name);
357 tablet_client->resource.destroy = destroy_tablet_client;
358 tablet_client->resource.object.id = id;
359 tablet_client->resource.object.interface =
360 &tablet_client_interface;
361 tablet_client->resource.object.implementation =
362 (void (**)(void)) &tablet_client_implementation;
364 wl_client_add_resource(client, &tablet_client->resource);
365 tablet_client->surface = NULL;
366 shell->current_client = tablet_client;
368 weston_log("created client %p, id %d, name %s, fd %d\n",
369 tablet_client->client, id, name, fd);
372 static const struct tablet_shell_interface tablet_shell_implementation = {
373 tablet_shell_set_lockscreen,
374 tablet_shell_set_switcher,
375 tablet_shell_set_homescreen,
376 tablet_shell_show_grid,
377 tablet_shell_show_panels,
378 tablet_shell_create_client
382 launch_ux_daemon(struct tablet_shell *shell)
384 const char *shell_exe = LIBEXECDIR "/weston-tablet-shell";
386 shell->client = weston_client_launch(shell->compositor,
388 shell_exe, tablet_shell_sigchld);
392 toggle_switcher(struct tablet_shell *shell)
394 switch (shell->state) {
396 tablet_shell_send_hide_switcher(&shell->resource);
399 tablet_shell_send_show_switcher(&shell->resource);
400 tablet_shell_set_state(shell, STATE_SWITCHER);
406 tablet_shell_lock(struct wl_listener *listener, void *data)
408 struct tablet_shell *shell =
409 container_of(listener, struct tablet_shell, lock_listener);
411 if (shell->state == STATE_LOCKED)
413 if (shell->state == STATE_SWITCHER)
414 tablet_shell_send_hide_switcher(&shell->resource);
416 tablet_shell_send_show_lockscreen(&shell->resource);
417 tablet_shell_set_state(shell, STATE_LOCKED);
421 tablet_shell_unlock(struct wl_listener *listener, void *data)
423 struct tablet_shell *shell =
424 container_of(listener, struct tablet_shell, lock_listener);
426 weston_compositor_wake(shell->compositor);
430 go_home(struct tablet_shell *shell, struct weston_seat *seat)
432 if (shell->state == STATE_SWITCHER)
433 tablet_shell_send_hide_switcher(&shell->resource);
435 weston_surface_activate(shell->home_surface, seat);
437 tablet_shell_set_state(shell, STATE_HOME);
441 long_press_handler(void *data)
443 struct tablet_shell *shell = data;
445 shell->long_press_active = 0;
446 toggle_switcher(shell);
452 menu_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
454 struct tablet_shell *shell = data;
456 if (shell->state == STATE_LOCKED)
459 toggle_switcher(shell);
463 home_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
465 struct tablet_shell *shell = data;
467 if (shell->state == STATE_LOCKED)
471 wl_event_source_timer_update(shell->long_press_source, 500);
472 shell->long_press_active = 1;
473 } else if (shell->long_press_active) {
474 /* This code has never been run ... */
475 wl_event_source_timer_update(shell->long_press_source, 0);
476 shell->long_press_active = 0;
478 switch (shell->state) {
481 toggle_switcher(shell);
484 go_home(shell, (struct weston_seat *) seat);
491 destroy_tablet_shell(struct wl_resource *resource)
496 bind_tablet_shell(struct wl_client *client, void *data, uint32_t version,
499 struct tablet_shell *shell = data;
501 if (shell->client != client)
502 /* Throw an error or just let the client fail when it
503 * tries to access the object?. */
506 shell->resource.object.id = id;
507 shell->resource.object.interface = &tablet_shell_interface;
508 shell->resource.object.implementation =
509 (void (**)(void)) &tablet_shell_implementation;
510 shell->resource.client = client;
511 shell->resource.data = shell;
512 shell->resource.destroy = destroy_tablet_shell;
514 wl_client_add_resource(client, &shell->resource);
518 tablet_shell_destroy(struct wl_listener *listener, void *data)
520 struct tablet_shell *shell =
521 container_of(listener, struct tablet_shell, destroy_listener);
523 if (shell->home_surface)
524 shell->home_surface->configure = NULL;
526 if (shell->lockscreen_surface)
527 shell->lockscreen_surface->configure = NULL;
529 wl_event_source_remove(shell->long_press_source);
534 module_init(struct weston_compositor *compositor)
536 struct tablet_shell *shell;
537 struct wl_event_loop *loop;
539 shell = malloc(sizeof *shell);
543 memset(shell, 0, sizeof *shell);
544 shell->compositor = compositor;
546 shell->destroy_listener.notify = tablet_shell_destroy;
547 wl_signal_add(&compositor->destroy_signal, &shell->destroy_listener);
548 shell->lock_listener.notify = tablet_shell_lock;
549 wl_signal_add(&compositor->lock_signal, &shell->lock_listener);
550 shell->unlock_listener.notify = tablet_shell_unlock;
551 wl_signal_add(&compositor->unlock_signal, &shell->unlock_listener);
553 /* FIXME: This will make the object available to all clients. */
554 wl_display_add_global(compositor->wl_display, &tablet_shell_interface,
555 shell, bind_tablet_shell);
557 loop = wl_display_get_event_loop(compositor->wl_display);
558 shell->long_press_source =
559 wl_event_loop_add_timer(loop, long_press_handler, shell);
561 weston_compositor_add_key_binding(compositor, KEY_LEFTMETA, 0,
562 home_key_binding, shell);
563 weston_compositor_add_key_binding(compositor, KEY_RIGHTMETA, 0,
564 home_key_binding, shell);
565 weston_compositor_add_key_binding(compositor, KEY_LEFTMETA,
566 MODIFIER_SUPER, home_key_binding,
568 weston_compositor_add_key_binding(compositor, KEY_RIGHTMETA,
569 MODIFIER_SUPER, home_key_binding,
571 weston_compositor_add_key_binding(compositor, KEY_COMPOSE, 0,
572 menu_key_binding, shell);
574 weston_layer_init(&shell->homescreen_layer,
575 &compositor->cursor_layer.link);
576 weston_layer_init(&shell->application_layer,
577 &compositor->cursor_layer.link);
578 weston_layer_init(&shell->lockscreen_layer,
579 &compositor->cursor_layer.link);
580 launch_ux_daemon(shell);
582 tablet_shell_set_state(shell, STATE_STARTING);