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 <sys/types.h>
29 #include <sys/socket.h>
31 #include <linux/input.h>
33 #include "compositor.h"
34 #include "meego-tablet-server-protocol.h"
37 * TODO: Don't fade back from black until we've received a lockscreen
49 struct meego_tablet_shell {
50 struct wl_resource resource;
52 struct wlsc_shell shell;
54 struct wlsc_compositor *compositor;
55 struct wlsc_process process;
56 struct wlsc_input_device *device;
57 struct wl_client *client;
59 struct wlsc_surface *surface;
61 struct wlsc_surface *lockscreen_surface;
62 struct wl_listener lockscreen_listener;
64 struct wlsc_surface *home_surface;
66 struct wlsc_surface *switcher_surface;
67 struct wl_listener switcher_listener;
69 struct meego_tablet_client *current_client;
71 int state, previous_state;
72 int long_press_active;
73 struct wl_event_source *long_press_source;
76 struct meego_tablet_client {
77 struct wl_resource resource;
78 struct meego_tablet_shell *shell;
79 struct wl_client *client;
80 struct wlsc_surface *surface;
84 struct meego_tablet_zoom {
85 struct wlsc_surface *surface;
86 struct wlsc_animation animation;
87 struct wlsc_spring spring;
88 struct wlsc_transform transform;
89 struct wl_listener listener;
90 struct meego_tablet_shell *shell;
91 void (*done)(struct meego_tablet_zoom *zoom);
96 meego_tablet_shell_sigchld(struct wlsc_process *process, int status)
98 struct meego_tablet_shell *shell =
99 container_of(process, struct meego_tablet_shell, process);
101 shell->process.pid = 0;
103 fprintf(stderr, "meego-ux-daemon crashed, exit code %d\n", status);
107 meego_tablet_shell_set_state(struct meego_tablet_shell *shell, int state)
109 static const char *states[] = {
110 "STARTING", "LOCKED", "HOME", "SWITCHER", "TASK"
113 fprintf(stderr, "switching to state %s (from %s)\n",
114 states[state], states[shell->state]);
115 shell->previous_state = shell->state;
116 shell->state = state;
120 meego_tablet_zoom_destroy(struct meego_tablet_zoom *zoom)
122 wl_list_remove(&zoom->animation.link);
123 zoom->surface->transform = NULL;
130 handle_zoom_surface_destroy(struct wl_listener *listener,
131 struct wl_resource *resource, uint32_t time)
133 struct meego_tablet_zoom *zoom =
134 container_of(listener, struct meego_tablet_zoom, listener);
136 fprintf(stderr, "animation surface gone\n");
137 meego_tablet_zoom_destroy(zoom);
141 meego_tablet_zoom_frame(struct wlsc_animation *animation,
142 struct wlsc_output *output, uint32_t msecs)
144 struct meego_tablet_zoom *zoom =
145 container_of(animation, struct meego_tablet_zoom, animation);
146 struct wlsc_surface *es = zoom->surface;
149 wlsc_spring_update(&zoom->spring, msecs);
151 if (wlsc_spring_done(&zoom->spring)) {
152 fprintf(stderr, "animation done\n");
153 meego_tablet_zoom_destroy(zoom);
156 scale = zoom->spring.current;
157 wlsc_matrix_init(&zoom->transform.matrix);
158 wlsc_matrix_translate(&zoom->transform.matrix,
159 -es->width / 2.0, -es->height / 2.0, 0);
160 wlsc_matrix_scale(&zoom->transform.matrix, scale, scale, scale);
161 wlsc_matrix_translate(&zoom->transform.matrix,
162 es->width / 2.0, es->height / 2.0, 0);
164 scale = 1.0 / zoom->spring.current;
165 wlsc_matrix_init(&zoom->transform.inverse);
166 wlsc_matrix_scale(&zoom->transform.inverse, scale, scale, scale);
168 wlsc_surface_damage(es);
171 static struct meego_tablet_zoom *
172 meego_tablet_zoom_run(struct meego_tablet_shell *shell,
173 struct wlsc_surface *surface,
174 GLfloat start, GLfloat stop)
176 struct meego_tablet_zoom *zoom;
178 fprintf(stderr, "starting animation for surface %p\n", surface);
180 zoom = malloc(sizeof *zoom);
185 zoom->surface = surface;
187 surface->transform = &zoom->transform;
188 wlsc_spring_init(&zoom->spring, 200.0, start, stop);
189 zoom->spring.timestamp = wlsc_compositor_get_time();
190 zoom->animation.frame = meego_tablet_zoom_frame;
191 meego_tablet_zoom_frame(&zoom->animation, NULL,
192 zoom->spring.timestamp);
194 zoom->listener.func = handle_zoom_surface_destroy;
195 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
196 &zoom->listener.link);
198 wl_list_insert(shell->compositor->animation_list.prev,
199 &zoom->animation.link);
204 /* FIXME: We should be handling map, not attach... Map is when the
205 * surface becomes visible, which is what we want to catch. Attach
206 * will happen whenever the surface changes. */
209 meego_tablet_shell_attach(struct wlsc_shell *base,
210 struct wlsc_surface *surface)
212 struct meego_tablet_shell *shell =
213 container_of(base, struct meego_tablet_shell, shell);
218 if (surface == shell->lockscreen_surface) {
219 wlsc_compositor_fade(shell->compositor, 0.0);
220 wlsc_compositor_wake(shell->compositor);
221 } else if (surface == shell->switcher_surface) {
223 } else if (surface == shell->home_surface) {
224 if (shell->state == STATE_STARTING) {
225 meego_tablet_shell_set_state(shell, STATE_LOCKED);
226 shell->previous_state = STATE_HOME;
227 wl_resource_post_event(&shell->resource,
228 MEEGO_TABLET_SHELL_SHOW_LOCKSCREEN);
230 } else if (shell->current_client &&
231 shell->current_client->surface != surface &&
232 shell->current_client->client == surface->surface.resource.client) {
233 meego_tablet_shell_set_state(shell, STATE_TASK);
234 shell->current_client->surface = surface;
235 meego_tablet_zoom_run(shell, surface, 0.3, 1.0);
240 handle_lockscreen_surface_destroy(struct wl_listener *listener,
241 struct wl_resource *resource, uint32_t time)
243 struct meego_tablet_shell *shell =
244 container_of(listener,
245 struct meego_tablet_shell, lockscreen_listener);
247 shell->lockscreen_surface = NULL;
248 meego_tablet_shell_set_state(shell, shell->previous_state);
252 tablet_shell_set_lockscreen(struct wl_client *client,
253 struct wl_resource *resource,
254 struct wl_surface *surface)
256 struct meego_tablet_shell *shell = resource->data;
257 struct wlsc_surface *es = (struct wlsc_surface *) surface;
258 struct wlsc_input_device *device =
259 (struct wlsc_input_device *) shell->compositor->input_device;
263 wlsc_surface_activate(es, device, wlsc_compositor_get_time());
264 shell->lockscreen_surface = es;
266 shell->lockscreen_listener.func = handle_lockscreen_surface_destroy;
267 wl_list_insert(es->surface.resource.destroy_listener_list.prev,
268 &shell->lockscreen_listener.link);
272 handle_switcher_surface_destroy(struct wl_listener *listener,
273 struct wl_resource *resource, uint32_t time)
275 struct meego_tablet_shell *shell =
276 container_of(listener,
277 struct meego_tablet_shell, switcher_listener);
279 shell->switcher_surface = NULL;
280 if (shell->state != STATE_LOCKED)
281 meego_tablet_shell_set_state(shell, shell->previous_state);
285 tablet_shell_set_switcher(struct wl_client *client,
286 struct wl_resource *resource,
287 struct wl_surface *surface)
289 struct meego_tablet_shell *shell = resource->data;
290 struct wlsc_input_device *device =
291 (struct wlsc_input_device *) shell->compositor->input_device;
292 struct wlsc_surface *es = (struct wlsc_surface *) surface;
294 /* FIXME: Switcher should be centered and the compositor
295 * should do the tinting of the background. With the cache
296 * layer idea, we should be able to hit the framerate on the
298 shell->switcher_surface = es;
299 shell->switcher_surface->x = 0;
300 shell->switcher_surface->y = 0;
302 wlsc_surface_activate(es, device, wlsc_compositor_get_time());
304 shell->switcher_listener.func = handle_switcher_surface_destroy;
305 wl_list_insert(es->surface.resource.destroy_listener_list.prev,
306 &shell->switcher_listener.link);
310 tablet_shell_set_homescreen(struct wl_client *client,
311 struct wl_resource *resource,
312 struct wl_surface *surface)
314 struct meego_tablet_shell *shell = resource->data;
315 struct wlsc_input_device *device =
316 (struct wlsc_input_device *) shell->compositor->input_device;
318 shell->home_surface = (struct wlsc_surface *) surface;
319 shell->home_surface->x = 0;
320 shell->home_surface->y = 0;
322 wlsc_surface_activate(shell->home_surface, device,
323 wlsc_compositor_get_time());
327 minimize_zoom_done(struct meego_tablet_zoom *zoom)
329 struct meego_tablet_shell *shell = zoom->shell;
330 struct wlsc_compositor *compositor = shell->compositor;
331 struct wlsc_input_device *device =
332 (struct wlsc_input_device *) compositor->input_device;
334 wlsc_surface_activate(shell->home_surface,
335 device, wlsc_compositor_get_time());
339 meego_tablet_shell_switch_to(struct meego_tablet_shell *shell,
340 struct wlsc_surface *surface)
342 struct wlsc_compositor *compositor = shell->compositor;
343 struct wlsc_input_device *device =
344 (struct wlsc_input_device *) compositor->input_device;
345 struct wlsc_surface *current;
346 struct meego_tablet_zoom *zoom;
348 if (shell->state == STATE_SWITCHER) {
349 wl_list_remove(&shell->switcher_listener.link);
350 shell->switcher_surface = NULL;
353 if (surface == shell->home_surface) {
354 meego_tablet_shell_set_state(shell, STATE_HOME);
356 if (shell->current_client && shell->current_client->surface) {
357 current = shell->current_client->surface;
358 zoom = meego_tablet_zoom_run(shell, current, 1.0, 0.3);
359 zoom->done = minimize_zoom_done;
362 fprintf(stderr, "switch to %p\n", surface);
363 wlsc_surface_activate(surface, device,
364 wlsc_compositor_get_time());
365 meego_tablet_shell_set_state(shell, STATE_TASK);
366 meego_tablet_zoom_run(shell, surface, 0.3, 1.0);
371 tablet_shell_show_grid(struct wl_client *client,
372 struct wl_resource *resource,
373 struct wl_surface *surface)
375 struct meego_tablet_shell *shell = resource->data;
377 meego_tablet_shell_switch_to(shell, (struct wlsc_surface *) surface);
381 tablet_shell_show_panels(struct wl_client *client,
382 struct wl_resource *resource,
383 struct wl_surface *surface)
385 struct meego_tablet_shell *shell = resource->data;
387 meego_tablet_shell_switch_to(shell, (struct wlsc_surface *) surface);
391 destroy_tablet_client(struct wl_resource *resource)
393 struct meego_tablet_client *tablet_client =
394 container_of(resource, struct meego_tablet_client, resource);
396 free(tablet_client->name);
401 tablet_client_destroy(struct wl_client *client, struct wl_resource *resource)
403 wl_resource_destroy(resource, wlsc_compositor_get_time());
407 tablet_client_activate(struct wl_client *client, struct wl_resource *resource)
409 struct meego_tablet_client *tablet_client = resource->data;
410 struct meego_tablet_shell *shell = tablet_client->shell;
412 shell->current_client = tablet_client;
413 if (!tablet_client->surface)
416 meego_tablet_shell_switch_to(shell, tablet_client->surface);
419 static const struct meego_tablet_client_interface tablet_client_interface = {
420 tablet_client_destroy,
421 tablet_client_activate
425 tablet_shell_create_client(struct wl_client *client,
426 struct wl_resource *resource,
427 uint32_t id, const char *name, int fd)
429 struct meego_tablet_shell *shell = resource->data;
430 struct wlsc_compositor *compositor = shell->compositor;
431 struct meego_tablet_client *tablet_client;
433 tablet_client = malloc(sizeof *tablet_client);
434 if (tablet_client == NULL) {
435 wl_client_post_no_memory(client);
439 tablet_client->client = wl_client_create(compositor->wl_display, fd);
440 tablet_client->shell = shell;
441 tablet_client->name = strdup(name);
443 tablet_client->resource.destroy = destroy_tablet_client;
444 tablet_client->resource.object.id = id;
445 tablet_client->resource.object.interface =
446 &meego_tablet_client_interface;
447 tablet_client->resource.object.implementation =
448 (void (**)(void)) &tablet_client_interface;
450 wl_client_add_resource(client, &tablet_client->resource);
451 tablet_client->surface = NULL;
452 shell->current_client = tablet_client;
454 fprintf(stderr, "created client %p, id %d, name %s, fd %d\n",
455 tablet_client->client, id, name, fd);
458 static const struct meego_tablet_shell_interface tablet_shell_interface = {
459 tablet_shell_set_lockscreen,
460 tablet_shell_set_switcher,
461 tablet_shell_set_homescreen,
462 tablet_shell_show_grid,
463 tablet_shell_show_panels,
464 tablet_shell_create_client
468 launch_ux_daemon(struct meego_tablet_shell *shell)
470 struct wlsc_compositor *compositor = shell->compositor;
474 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) {
475 fprintf(stderr, "socketpair failed\n");
479 shell->process.pid = fork();
480 shell->process.cleanup = meego_tablet_shell_sigchld;
482 switch (shell->process.pid) {
484 /* SOCK_CLOEXEC closes both ends, so we need to unset
485 * the flag on the client fd. */
486 flags = fcntl(sv[1], F_GETFD);
488 fcntl(sv[1], F_SETFD, flags & ~FD_CLOEXEC);
490 snprintf(s, sizeof s, "%d", sv[1]);
491 setenv("WAYLAND_SOCKET", s, 1);
492 setenv("EGL_PLATFORM", "wayland", 1);
493 setenv("QT_QPA_PLATFORM", "waylandgl", 1);
494 if (execl("/usr/libexec/meego-ux-daemon",
495 "/usr/libexec/meego-ux-daemon", NULL) < 0)
496 fprintf(stderr, "exec failed: %m\n");
502 wl_client_create(compositor->wl_display, sv[0]);
503 wlsc_watch_process(&shell->process);
507 fprintf(stderr, "failed to fork\n");
513 toggle_switcher(struct meego_tablet_shell *shell)
515 switch (shell->state) {
517 wl_resource_post_event(&shell->resource,
518 MEEGO_TABLET_SHELL_HIDE_SWITCHER);
521 wl_resource_post_event(&shell->resource,
522 MEEGO_TABLET_SHELL_SHOW_SWITCHER);
523 meego_tablet_shell_set_state(shell, STATE_SWITCHER);
529 meego_tablet_shell_lock(struct wlsc_shell *base)
531 struct meego_tablet_shell *shell =
532 container_of(base, struct meego_tablet_shell, shell);
534 if (shell->state == STATE_LOCKED)
536 if (shell->state == STATE_SWITCHER)
537 wl_resource_post_event(&shell->resource,
538 MEEGO_TABLET_SHELL_HIDE_SWITCHER);
540 wl_resource_post_event(&shell->resource,
541 MEEGO_TABLET_SHELL_SHOW_LOCKSCREEN);
543 meego_tablet_shell_set_state(shell, STATE_LOCKED);
547 go_home(struct meego_tablet_shell *shell)
549 struct wlsc_input_device *device =
550 (struct wlsc_input_device *) shell->compositor->input_device;
552 if (shell->state == STATE_SWITCHER)
553 wl_resource_post_event(&shell->resource,
554 MEEGO_TABLET_SHELL_HIDE_SWITCHER);
556 wlsc_surface_activate(shell->home_surface, device,
557 wlsc_compositor_get_time());
559 meego_tablet_shell_set_state(shell, STATE_HOME);
563 long_press_handler(void *data)
565 struct meego_tablet_shell *shell = data;
567 shell->long_press_active = 0;
568 toggle_switcher(shell);
574 menu_key_binding(struct wl_input_device *device, uint32_t time,
575 uint32_t key, uint32_t button, uint32_t state, void *data)
577 struct meego_tablet_shell *shell = data;
579 if (shell->state == STATE_LOCKED)
583 toggle_switcher(shell);
587 home_key_binding(struct wl_input_device *device, uint32_t time,
588 uint32_t key, uint32_t button, uint32_t state, void *data)
590 struct meego_tablet_shell *shell = data;
592 if (shell->state == STATE_LOCKED)
595 shell->device = (struct wlsc_input_device *) device;
598 wl_event_source_timer_update(shell->long_press_source, 500);
599 shell->long_press_active = 1;
600 } else if (shell->long_press_active) {
601 wl_event_source_timer_update(shell->long_press_source, 0);
602 shell->long_press_active = 0;
604 switch (shell->state) {
607 toggle_switcher(shell);
617 meego_tablet_shell_set_selection_focus(struct wlsc_shell *shell,
618 struct wl_selection *selection,
619 struct wl_surface *surface,
625 bind_shell(struct wl_client *client,
626 struct wl_object *global, uint32_t version)
628 struct meego_tablet_shell *shell =
630 struct meego_tablet_shell, resource.object);
632 if (shell->client != client)
633 /* Throw an error or just let the client fail when it
634 * tries to access the object?. */
637 shell->resource.client = client;
641 shell_init(struct wlsc_compositor *compositor);
644 shell_init(struct wlsc_compositor *compositor)
646 struct meego_tablet_shell *shell;
647 struct wl_event_loop *loop;
649 shell = malloc(sizeof *shell);
653 memset(shell, 0, sizeof *shell);
654 shell->compositor = compositor;
656 shell->resource.object.interface = &meego_tablet_shell_interface;
657 shell->resource.object.implementation =
658 (void (**)(void)) &tablet_shell_interface;
659 wl_display_add_object(compositor->wl_display, &shell->resource.object);
661 /* FIXME: This will make the object available to all clients. */
662 wl_display_add_global(compositor->wl_display,
663 &shell->resource.object, bind_shell);
665 loop = wl_display_get_event_loop(compositor->wl_display);
666 shell->long_press_source =
667 wl_event_loop_add_timer(loop, long_press_handler, shell);
669 wlsc_compositor_add_binding(compositor, KEY_LEFTMETA, 0, 0,
670 home_key_binding, shell);
671 wlsc_compositor_add_binding(compositor, KEY_RIGHTMETA, 0, 0,
672 home_key_binding, shell);
673 wlsc_compositor_add_binding(compositor, KEY_LEFTMETA, 0,
674 MODIFIER_SUPER, home_key_binding, shell);
675 wlsc_compositor_add_binding(compositor, KEY_RIGHTMETA, 0,
676 MODIFIER_SUPER, home_key_binding, shell);
677 wlsc_compositor_add_binding(compositor, KEY_COMPOSE, 0, 0,
678 menu_key_binding, shell);
680 compositor->shell = &shell->shell;
682 shell->shell.lock = meego_tablet_shell_lock;
683 shell->shell.attach = meego_tablet_shell_attach;
684 shell->shell.set_selection_focus =
685 meego_tablet_shell_set_selection_focus;
686 launch_ux_daemon(shell);
688 wlsc_spring_init(&compositor->fade.spring, 40.0, 1.0, 1.0);
689 meego_tablet_shell_set_state(shell, STATE_STARTING);