input: Rename weston_device_repick() to weston_seat_repick()
[platform/upstream/weston.git] / src / tablet-shell.c
1 /*
2  * Copyright © 2011 Intel Corporation
3  *
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.
13  *
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.
21  */
22
23 #include <sys/wait.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <linux/input.h>
29
30 #include "compositor.h"
31 #include "tablet-shell-server-protocol.h"
32
33 /*
34  * TODO: Don't fade back from black until we've received a lockscreen
35  * attachment.
36  */
37
38 enum {
39         STATE_STARTING,
40         STATE_LOCKED,
41         STATE_HOME,
42         STATE_SWITCHER,
43         STATE_TASK
44 };
45
46 struct tablet_shell {
47         struct wl_resource resource;
48
49         struct wl_listener lock_listener;
50         struct wl_listener unlock_listener;
51         struct wl_listener destroy_listener;
52
53         struct weston_compositor *compositor;
54         struct weston_process process;
55         struct wl_client *client;
56
57         struct weston_surface *surface;
58
59         struct weston_surface *lockscreen_surface;
60         struct wl_listener lockscreen_listener;
61         struct weston_layer lockscreen_layer;
62
63         struct weston_layer application_layer;
64
65         struct weston_surface *home_surface;
66         struct weston_layer homescreen_layer;
67
68         struct weston_surface *switcher_surface;
69         struct wl_listener switcher_listener;
70
71         struct tablet_client *current_client;
72
73         int state, previous_state;
74         int long_press_active;
75         struct wl_event_source *long_press_source;
76 };
77
78 struct tablet_client {
79         struct wl_resource resource;
80         struct tablet_shell *shell;
81         struct wl_client *client;
82         struct weston_surface *surface;
83         char *name;
84 };
85
86 static void
87 tablet_shell_destroy(struct wl_listener *listener, void *data);
88
89 static struct tablet_shell *
90 get_shell(struct weston_compositor *compositor)
91 {
92         struct wl_listener *l;
93
94         l = wl_signal_get(&compositor->destroy_signal, tablet_shell_destroy);
95         if (l)
96                 return container_of(l, struct tablet_shell, destroy_listener);
97
98         return NULL;
99 }
100
101 static void
102 tablet_shell_sigchld(struct weston_process *process, int status)
103 {
104         struct tablet_shell *shell =
105                 container_of(process, struct tablet_shell, process);
106
107         shell->process.pid = 0;
108
109         weston_log("weston-tablet-shell crashed, exit code %d\n", status);
110 }
111
112 static void
113 tablet_shell_set_state(struct tablet_shell *shell, int state)
114 {
115         static const char *states[] = {
116                 "STARTING", "LOCKED", "HOME", "SWITCHER", "TASK"
117         };
118
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;
123 }
124
125 static void
126 tablet_shell_surface_configure(struct weston_surface *surface,
127                                int32_t sx, int32_t sy, int32_t width, int32_t height)
128 {
129         struct tablet_shell *shell = get_shell(surface->compositor);
130
131         if (weston_surface_is_mapped(surface) || width == 0)
132                 return;
133
134         weston_surface_configure(surface, 0, 0, width, height);
135
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) {
140                 /* */
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);
146
147                         tablet_shell_set_state(shell, STATE_LOCKED);
148                         shell->previous_state = STATE_HOME;
149                         tablet_shell_send_show_lockscreen(&shell->resource);
150                 }
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);
159         }
160
161         weston_surface_update_transform(surface);
162 }
163
164 static void
165 handle_lockscreen_surface_destroy(struct wl_listener *listener, void *data)
166 {
167         struct tablet_shell *shell =
168                 container_of(listener,
169                              struct tablet_shell, lockscreen_listener);
170
171         shell->lockscreen_surface = NULL;
172         tablet_shell_set_state(shell, shell->previous_state);
173 }
174
175 static void
176 tablet_shell_set_lockscreen(struct wl_client *client,
177                             struct wl_resource *resource,
178                             struct wl_resource *surface_resource)
179 {
180         struct tablet_shell *shell = resource->data;
181         struct weston_surface *es = surface_resource->data;
182
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);
189 }
190
191 static void
192 handle_switcher_surface_destroy(struct wl_listener *listener, void *data)
193 {
194         struct tablet_shell *shell =
195                 container_of(listener,
196                              struct tablet_shell, switcher_listener);
197
198         shell->switcher_surface = NULL;
199         if (shell->state != STATE_LOCKED)
200                 tablet_shell_set_state(shell, shell->previous_state);
201 }
202
203 static void
204 tablet_shell_set_switcher(struct wl_client *client,
205                           struct wl_resource *resource,
206                           struct wl_resource *surface_resource)
207 {
208         struct tablet_shell *shell = resource->data;
209         struct weston_surface *es = surface_resource->data;
210
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
214          * fade/zoom in. */
215         shell->switcher_surface = es;
216         weston_surface_set_position(shell->switcher_surface, 0, 0);
217
218         shell->switcher_listener.notify = handle_switcher_surface_destroy;
219         wl_signal_add(&es->surface.resource.destroy_signal,
220                       &shell->switcher_listener);
221 }
222
223 static void
224 tablet_shell_set_homescreen(struct wl_client *client,
225                             struct wl_resource *resource,
226                             struct wl_resource *surface_resource)
227 {
228         struct tablet_shell *shell = resource->data;
229
230         shell->home_surface = surface_resource->data;
231         shell->home_surface->configure = tablet_shell_surface_configure;
232
233         weston_surface_set_position(shell->home_surface, 0, 0);
234 }
235
236 static void
237 minimize_zoom_done(struct weston_surface_animation *zoom, void *data)
238 {
239         struct tablet_shell *shell = data;
240         struct weston_compositor *compositor = shell->compositor;
241         struct weston_seat *seat;
242
243         wl_list_for_each(seat, &compositor->seat_list, link)
244                 weston_surface_activate(shell->home_surface, seat);
245 }
246
247 static void
248 tablet_shell_switch_to(struct tablet_shell *shell,
249                              struct weston_surface *surface)
250 {
251         struct weston_compositor *compositor = shell->compositor;
252         struct weston_seat *seat;
253         struct weston_surface *current;
254
255         if (shell->state == STATE_SWITCHER) {
256                 wl_list_remove(&shell->switcher_listener.link);
257                 shell->switcher_surface = NULL;
258         };
259
260         if (surface == shell->home_surface) {
261                 tablet_shell_set_state(shell, STATE_HOME);
262
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);
267                 }
268         } else {
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);
274         }
275 }
276
277 static void
278 tablet_shell_show_grid(struct wl_client *client,
279                        struct wl_resource *resource,
280                        struct wl_resource *surface_resource)
281 {
282         struct tablet_shell *shell = resource->data;
283         struct weston_surface *es = surface_resource->data;
284
285         tablet_shell_switch_to(shell, es);
286 }
287
288 static void
289 tablet_shell_show_panels(struct wl_client *client,
290                          struct wl_resource *resource,
291                          struct wl_resource *surface_resource)
292 {
293         struct tablet_shell *shell = resource->data;
294         struct weston_surface *es = surface_resource->data;
295
296         tablet_shell_switch_to(shell, es);
297 }
298
299 static void
300 destroy_tablet_client(struct wl_resource *resource)
301 {
302         struct tablet_client *tablet_client =
303                 container_of(resource, struct tablet_client, resource);
304
305         free(tablet_client->name);
306         free(tablet_client);
307 }
308
309 static void
310 tablet_client_destroy(struct wl_client *client,
311                       struct wl_resource *resource)
312 {
313         wl_resource_destroy(resource);
314 }
315
316 static void
317 tablet_client_activate(struct wl_client *client, struct wl_resource *resource)
318 {
319         struct tablet_client *tablet_client = resource->data;
320         struct tablet_shell *shell = tablet_client->shell;
321
322         shell->current_client = tablet_client;
323         if (!tablet_client->surface)
324                 return;
325
326         tablet_shell_switch_to(shell, tablet_client->surface);
327 }
328
329 static const struct tablet_client_interface tablet_client_implementation = {
330         tablet_client_destroy,
331         tablet_client_activate
332 };
333
334 static void
335 tablet_shell_create_client(struct wl_client *client,
336                            struct wl_resource *resource,
337                            uint32_t id, const char *name, int fd)
338 {
339         struct tablet_shell *shell = resource->data;
340         struct weston_compositor *compositor = shell->compositor;
341         struct tablet_client *tablet_client;
342
343         tablet_client = malloc(sizeof *tablet_client);
344         if (tablet_client == NULL) {
345                 wl_resource_post_no_memory(resource);
346                 return;
347         }
348
349         tablet_client->client = wl_client_create(compositor->wl_display, fd);
350         tablet_client->shell = shell;
351         tablet_client->name = strdup(name);
352
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;
359
360         wl_client_add_resource(client, &tablet_client->resource);
361         tablet_client->surface = NULL;
362         shell->current_client = tablet_client;
363
364         weston_log("created client %p, id %d, name %s, fd %d\n",
365                 tablet_client->client, id, name, fd);
366 }
367
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
375 };
376
377 static void
378 launch_ux_daemon(struct tablet_shell *shell)
379 {
380         const char *shell_exe = LIBEXECDIR "/weston-tablet-shell";
381
382         shell->client = weston_client_launch(shell->compositor,
383                                            &shell->process,
384                                            shell_exe, tablet_shell_sigchld);
385 }
386
387 static void
388 toggle_switcher(struct tablet_shell *shell)
389 {
390         switch (shell->state) {
391         case STATE_SWITCHER:
392                 tablet_shell_send_hide_switcher(&shell->resource);
393                 break;
394         default:
395                 tablet_shell_send_show_switcher(&shell->resource);
396                 tablet_shell_set_state(shell, STATE_SWITCHER);
397                 break;
398         }
399 }
400
401 static void
402 tablet_shell_lock(struct wl_listener *listener, void *data)
403 {
404         struct tablet_shell *shell =
405                 container_of(listener, struct tablet_shell, lock_listener);
406
407         if (shell->state == STATE_LOCKED)
408                 return;
409         if (shell->state == STATE_SWITCHER)
410                 tablet_shell_send_hide_switcher(&shell->resource);
411
412         tablet_shell_send_show_lockscreen(&shell->resource);
413         tablet_shell_set_state(shell, STATE_LOCKED);
414 }
415
416 static void
417 tablet_shell_unlock(struct wl_listener *listener, void *data)
418 {
419         struct tablet_shell *shell =
420                 container_of(listener, struct tablet_shell, lock_listener);
421
422         weston_compositor_wake(shell->compositor);
423 }
424
425 static void
426 go_home(struct tablet_shell *shell, struct weston_seat *seat)
427 {
428         if (shell->state == STATE_SWITCHER)
429                 tablet_shell_send_hide_switcher(&shell->resource);
430
431         weston_surface_activate(shell->home_surface, seat);
432
433         tablet_shell_set_state(shell, STATE_HOME);
434 }
435
436 static int
437 long_press_handler(void *data)
438 {
439         struct tablet_shell *shell = data;
440
441         shell->long_press_active = 0;
442         toggle_switcher(shell);
443
444         return 1;
445 }
446
447 static void
448 menu_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
449 {
450         struct tablet_shell *shell = data;
451
452         if (shell->state == STATE_LOCKED)
453                 return;
454
455         toggle_switcher(shell);
456 }
457
458 static void
459 home_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
460 {
461         struct tablet_shell *shell = data;
462
463         if (shell->state == STATE_LOCKED)
464                 return;
465
466         if (1) {
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;
473
474                 switch (shell->state) {
475                 case STATE_HOME:
476                 case STATE_SWITCHER:
477                         toggle_switcher(shell);
478                         break;
479                 default:
480                         go_home(shell, (struct weston_seat *) seat);
481                         break;
482                 }
483         }
484 }
485
486 static void
487 destroy_tablet_shell(struct wl_resource *resource)
488 {
489 }
490
491 static void
492 bind_tablet_shell(struct wl_client *client, void *data, uint32_t version,
493                   uint32_t id)
494 {
495         struct tablet_shell *shell = data;
496
497         if (shell->client != client)
498                 /* Throw an error or just let the client fail when it
499                  * tries to access the object?. */
500                 return;
501
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;
509
510         wl_client_add_resource(client, &shell->resource);
511 }
512
513 static void
514 tablet_shell_destroy(struct wl_listener *listener, void *data)
515 {
516         struct tablet_shell *shell =
517                 container_of(listener, struct tablet_shell, destroy_listener);
518
519         if (shell->home_surface)
520                 shell->home_surface->configure = NULL;
521
522         if (shell->lockscreen_surface)
523                 shell->lockscreen_surface->configure = NULL;
524
525         wl_event_source_remove(shell->long_press_source);
526         free(shell);
527 }
528
529 WL_EXPORT int
530 module_init(struct weston_compositor *compositor,
531             int *argc, char *argv[], const char *config_file)
532 {
533         struct tablet_shell *shell;
534         struct wl_event_loop *loop;
535
536         shell = malloc(sizeof *shell);
537         if (shell == NULL)
538                 return -1;
539
540         memset(shell, 0, sizeof *shell);
541         shell->compositor = compositor;
542
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);
549
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);
553
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);
557
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,
564                                           shell);
565         weston_compositor_add_key_binding(compositor, KEY_RIGHTMETA,
566                                           MODIFIER_SUPER, home_key_binding,
567                                           shell);
568         weston_compositor_add_key_binding(compositor, KEY_COMPOSE, 0,
569                                           menu_key_binding, shell);
570
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);
578
579         tablet_shell_set_state(shell, STATE_STARTING);
580
581         return 0;
582 }