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.
27 #include <linux/input.h>
29 #include "compositor.h"
30 #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_surface *home_surface;
64 struct weston_layer homescreen_layer;
66 struct weston_surface *switcher_surface;
67 struct wl_listener switcher_listener;
69 struct tablet_client *current_client;
71 int state, previous_state;
72 int long_press_active;
73 struct wl_event_source *long_press_source;
76 struct tablet_client {
77 struct wl_resource resource;
78 struct tablet_shell *shell;
79 struct wl_client *client;
80 struct weston_surface *surface;
85 tablet_shell_destroy(struct wl_listener *listener, void *data);
87 static struct tablet_shell *
88 get_shell(struct weston_compositor *compositor)
90 struct wl_listener *l;
92 l = wl_signal_get(&compositor->destroy_signal, tablet_shell_destroy);
94 return container_of(l, struct tablet_shell, destroy_listener);
100 tablet_shell_sigchld(struct weston_process *process, int status)
102 struct tablet_shell *shell =
103 container_of(process, struct tablet_shell, process);
105 shell->process.pid = 0;
107 weston_log("weston-tablet-shell crashed, exit code %d\n", status);
111 tablet_shell_set_state(struct tablet_shell *shell, int state)
113 static const char *states[] = {
114 "STARTING", "LOCKED", "HOME", "SWITCHER", "TASK"
117 weston_log("switching to state %s (from %s)\n",
118 states[state], states[shell->state]);
119 shell->previous_state = shell->state;
120 shell->state = state;
124 tablet_shell_surface_configure(struct weston_surface *surface,
125 int32_t sx, int32_t sy)
127 struct tablet_shell *shell = get_shell(surface->compositor);
128 int32_t width, height;
130 if (weston_surface_is_mapped(surface))
133 width = surface->buffer->width;
134 height = surface->buffer->height;
136 weston_surface_configure(surface, 0, 0, width, height);
138 if (surface == shell->lockscreen_surface) {
139 wl_list_insert(&shell->lockscreen_layer.surface_list,
140 &surface->layer_link);
141 } else if (surface == shell->switcher_surface) {
143 } else if (surface == shell->home_surface) {
144 if (shell->state == STATE_STARTING) {
145 /* homescreen always visible, at the bottom */
146 wl_list_insert(&shell->homescreen_layer.surface_list,
147 &surface->layer_link);
149 tablet_shell_set_state(shell, STATE_LOCKED);
150 shell->previous_state = STATE_HOME;
151 tablet_shell_send_show_lockscreen(&shell->resource);
153 } else if (shell->current_client &&
154 shell->current_client->surface != surface &&
155 shell->current_client->client == surface->surface.resource.client) {
156 tablet_shell_set_state(shell, STATE_TASK);
157 shell->current_client->surface = surface;
158 weston_zoom_run(surface, 0.3, 1.0, NULL, NULL);
161 wl_list_insert(&shell->compositor->surface_list, &surface->link);
162 weston_surface_assign_output(surface);
166 handle_lockscreen_surface_destroy(struct wl_listener *listener, void *data)
168 struct tablet_shell *shell =
169 container_of(listener,
170 struct tablet_shell, lockscreen_listener);
172 shell->lockscreen_surface = NULL;
173 tablet_shell_set_state(shell, shell->previous_state);
177 tablet_shell_set_lockscreen(struct wl_client *client,
178 struct wl_resource *resource,
179 struct wl_resource *surface_resource)
181 struct tablet_shell *shell = resource->data;
182 struct weston_surface *es = surface_resource->data;
184 weston_surface_set_position(es, 0, 0);
185 shell->lockscreen_surface = es;
186 shell->lockscreen_surface->configure = tablet_shell_surface_configure;
187 shell->lockscreen_listener.notify = handle_lockscreen_surface_destroy;
188 wl_signal_add(&es->surface.resource.destroy_signal,
189 &shell->lockscreen_listener);
193 handle_switcher_surface_destroy(struct wl_listener *listener, void *data)
195 struct tablet_shell *shell =
196 container_of(listener,
197 struct tablet_shell, switcher_listener);
199 shell->switcher_surface = NULL;
200 if (shell->state != STATE_LOCKED)
201 tablet_shell_set_state(shell, shell->previous_state);
205 tablet_shell_set_switcher(struct wl_client *client,
206 struct wl_resource *resource,
207 struct wl_resource *surface_resource)
209 struct tablet_shell *shell = resource->data;
210 struct weston_surface *es = surface_resource->data;
212 /* FIXME: Switcher should be centered and the compositor
213 * should do the tinting of the background. With the cache
214 * layer idea, we should be able to hit the framerate on the
216 shell->switcher_surface = es;
217 weston_surface_set_position(shell->switcher_surface, 0, 0);
219 shell->switcher_listener.notify = handle_switcher_surface_destroy;
220 wl_signal_add(&es->surface.resource.destroy_signal,
221 &shell->switcher_listener);
225 tablet_shell_set_homescreen(struct wl_client *client,
226 struct wl_resource *resource,
227 struct wl_resource *surface_resource)
229 struct tablet_shell *shell = resource->data;
231 shell->home_surface = surface_resource->data;
232 shell->home_surface->configure = tablet_shell_surface_configure;
234 weston_surface_set_position(shell->home_surface, 0, 0);
238 minimize_zoom_done(struct weston_zoom *zoom, void *data)
240 struct tablet_shell *shell = data;
241 struct weston_compositor *compositor = shell->compositor;
242 struct weston_seat *seat;
244 wl_list_for_each(seat, &compositor->seat_list, link)
245 weston_surface_activate(shell->home_surface, seat);
249 tablet_shell_switch_to(struct tablet_shell *shell,
250 struct weston_surface *surface)
252 struct weston_compositor *compositor = shell->compositor;
253 struct weston_seat *seat;
254 struct weston_surface *current;
256 if (shell->state == STATE_SWITCHER) {
257 wl_list_remove(&shell->switcher_listener.link);
258 shell->switcher_surface = NULL;
261 if (surface == shell->home_surface) {
262 tablet_shell_set_state(shell, STATE_HOME);
264 if (shell->current_client && shell->current_client->surface) {
265 current = shell->current_client->surface;
266 weston_zoom_run(current, 1.0, 0.3,
267 minimize_zoom_done, shell);
270 fprintf(stderr, "switch to %p\n", surface);
271 wl_list_for_each(seat, &compositor->seat_list, link)
272 weston_surface_activate(surface, seat);
273 tablet_shell_set_state(shell, STATE_TASK);
274 weston_zoom_run(surface, 0.3, 1.0, NULL, NULL);
279 tablet_shell_show_grid(struct wl_client *client,
280 struct wl_resource *resource,
281 struct wl_resource *surface_resource)
283 struct tablet_shell *shell = resource->data;
284 struct weston_surface *es = surface_resource->data;
286 tablet_shell_switch_to(shell, es);
290 tablet_shell_show_panels(struct wl_client *client,
291 struct wl_resource *resource,
292 struct wl_resource *surface_resource)
294 struct tablet_shell *shell = resource->data;
295 struct weston_surface *es = surface_resource->data;
297 tablet_shell_switch_to(shell, es);
301 destroy_tablet_client(struct wl_resource *resource)
303 struct tablet_client *tablet_client =
304 container_of(resource, struct tablet_client, resource);
306 free(tablet_client->name);
311 tablet_client_destroy(struct wl_client *client,
312 struct wl_resource *resource)
314 wl_resource_destroy(resource);
318 tablet_client_activate(struct wl_client *client, struct wl_resource *resource)
320 struct tablet_client *tablet_client = resource->data;
321 struct tablet_shell *shell = tablet_client->shell;
323 shell->current_client = tablet_client;
324 if (!tablet_client->surface)
327 tablet_shell_switch_to(shell, tablet_client->surface);
330 static const struct tablet_client_interface tablet_client_implementation = {
331 tablet_client_destroy,
332 tablet_client_activate
336 tablet_shell_create_client(struct wl_client *client,
337 struct wl_resource *resource,
338 uint32_t id, const char *name, int fd)
340 struct tablet_shell *shell = resource->data;
341 struct weston_compositor *compositor = shell->compositor;
342 struct tablet_client *tablet_client;
344 tablet_client = malloc(sizeof *tablet_client);
345 if (tablet_client == NULL) {
346 wl_resource_post_no_memory(resource);
350 tablet_client->client = wl_client_create(compositor->wl_display, fd);
351 tablet_client->shell = shell;
352 tablet_client->name = strdup(name);
354 tablet_client->resource.destroy = destroy_tablet_client;
355 tablet_client->resource.object.id = id;
356 tablet_client->resource.object.interface =
357 &tablet_client_interface;
358 tablet_client->resource.object.implementation =
359 (void (**)(void)) &tablet_client_implementation;
361 wl_client_add_resource(client, &tablet_client->resource);
362 tablet_client->surface = NULL;
363 shell->current_client = tablet_client;
365 weston_log("created client %p, id %d, name %s, fd %d\n",
366 tablet_client->client, id, name, fd);
369 static const struct tablet_shell_interface tablet_shell_implementation = {
370 tablet_shell_set_lockscreen,
371 tablet_shell_set_switcher,
372 tablet_shell_set_homescreen,
373 tablet_shell_show_grid,
374 tablet_shell_show_panels,
375 tablet_shell_create_client
379 launch_ux_daemon(struct tablet_shell *shell)
381 const char *shell_exe = LIBEXECDIR "/weston-tablet-shell";
383 shell->client = weston_client_launch(shell->compositor,
385 shell_exe, tablet_shell_sigchld);
389 toggle_switcher(struct tablet_shell *shell)
391 switch (shell->state) {
393 tablet_shell_send_hide_switcher(&shell->resource);
396 tablet_shell_send_show_switcher(&shell->resource);
397 tablet_shell_set_state(shell, STATE_SWITCHER);
403 tablet_shell_lock(struct wl_listener *listener, void *data)
405 struct tablet_shell *shell =
406 container_of(listener, struct tablet_shell, lock_listener);
408 if (shell->state == STATE_LOCKED)
410 if (shell->state == STATE_SWITCHER)
411 tablet_shell_send_hide_switcher(&shell->resource);
413 tablet_shell_send_show_lockscreen(&shell->resource);
414 tablet_shell_set_state(shell, STATE_LOCKED);
418 tablet_shell_unlock(struct wl_listener *listener, void *data)
420 struct tablet_shell *shell =
421 container_of(listener, struct tablet_shell, lock_listener);
423 weston_compositor_wake(shell->compositor);
427 go_home(struct tablet_shell *shell, struct weston_seat *seat)
429 if (shell->state == STATE_SWITCHER)
430 tablet_shell_send_hide_switcher(&shell->resource);
432 weston_surface_activate(shell->home_surface, seat);
434 tablet_shell_set_state(shell, STATE_HOME);
438 long_press_handler(void *data)
440 struct tablet_shell *shell = data;
442 shell->long_press_active = 0;
443 toggle_switcher(shell);
449 menu_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
451 struct tablet_shell *shell = data;
453 if (shell->state == STATE_LOCKED)
456 toggle_switcher(shell);
460 home_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
462 struct tablet_shell *shell = data;
464 if (shell->state == STATE_LOCKED)
468 wl_event_source_timer_update(shell->long_press_source, 500);
469 shell->long_press_active = 1;
470 } else if (shell->long_press_active) {
471 /* This code has never been run ... */
472 wl_event_source_timer_update(shell->long_press_source, 0);
473 shell->long_press_active = 0;
475 switch (shell->state) {
478 toggle_switcher(shell);
481 go_home(shell, (struct weston_seat *) seat);
488 destroy_tablet_shell(struct wl_resource *resource)
493 bind_tablet_shell(struct wl_client *client, void *data, uint32_t version,
496 struct tablet_shell *shell = data;
498 if (shell->client != client)
499 /* Throw an error or just let the client fail when it
500 * tries to access the object?. */
503 shell->resource.object.id = id;
504 shell->resource.object.interface = &tablet_shell_interface;
505 shell->resource.object.implementation =
506 (void (**)(void)) &tablet_shell_implementation;
507 shell->resource.client = client;
508 shell->resource.data = shell;
509 shell->resource.destroy = destroy_tablet_shell;
511 wl_client_add_resource(client, &shell->resource);
515 tablet_shell_destroy(struct wl_listener *listener, void *data)
517 struct tablet_shell *shell =
518 container_of(listener, struct tablet_shell, destroy_listener);
520 if (shell->home_surface)
521 shell->home_surface->configure = NULL;
523 if (shell->lockscreen_surface)
524 shell->lockscreen_surface->configure = NULL;
526 wl_event_source_remove(shell->long_press_source);
531 shell_init(struct weston_compositor *compositor);
534 shell_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->lockscreen_layer,
577 &compositor->cursor_layer.link);
578 launch_ux_daemon(shell);
580 tablet_shell_set_state(shell, STATE_STARTING);