compositor: Don't set DPMS state on start up
[profile/ivi/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)
128 {
129         struct tablet_shell *shell = get_shell(surface->compositor);
130         int32_t width, height;
131
132         if (weston_surface_is_mapped(surface))
133                 return;
134
135         width = surface->buffer->width;
136         height = surface->buffer->height;
137
138         weston_surface_configure(surface, 0, 0, width, height);
139
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) {
144                 /* */
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);
150
151                         tablet_shell_set_state(shell, STATE_LOCKED);
152                         shell->previous_state = STATE_HOME;
153                         tablet_shell_send_show_lockscreen(&shell->resource);
154                 }
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);
163         }
164
165         weston_surface_update_transform(surface);
166 }
167
168 static void
169 handle_lockscreen_surface_destroy(struct wl_listener *listener, void *data)
170 {
171         struct tablet_shell *shell =
172                 container_of(listener,
173                              struct tablet_shell, lockscreen_listener);
174
175         shell->lockscreen_surface = NULL;
176         tablet_shell_set_state(shell, shell->previous_state);
177 }
178
179 static void
180 tablet_shell_set_lockscreen(struct wl_client *client,
181                             struct wl_resource *resource,
182                             struct wl_resource *surface_resource)
183 {
184         struct tablet_shell *shell = resource->data;
185         struct weston_surface *es = surface_resource->data;
186
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);
193 }
194
195 static void
196 handle_switcher_surface_destroy(struct wl_listener *listener, void *data)
197 {
198         struct tablet_shell *shell =
199                 container_of(listener,
200                              struct tablet_shell, switcher_listener);
201
202         shell->switcher_surface = NULL;
203         if (shell->state != STATE_LOCKED)
204                 tablet_shell_set_state(shell, shell->previous_state);
205 }
206
207 static void
208 tablet_shell_set_switcher(struct wl_client *client,
209                           struct wl_resource *resource,
210                           struct wl_resource *surface_resource)
211 {
212         struct tablet_shell *shell = resource->data;
213         struct weston_surface *es = surface_resource->data;
214
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
218          * fade/zoom in. */
219         shell->switcher_surface = es;
220         weston_surface_set_position(shell->switcher_surface, 0, 0);
221
222         shell->switcher_listener.notify = handle_switcher_surface_destroy;
223         wl_signal_add(&es->surface.resource.destroy_signal,
224                       &shell->switcher_listener);
225 }
226
227 static void
228 tablet_shell_set_homescreen(struct wl_client *client,
229                             struct wl_resource *resource,
230                             struct wl_resource *surface_resource)
231 {
232         struct tablet_shell *shell = resource->data;
233
234         shell->home_surface = surface_resource->data;
235         shell->home_surface->configure = tablet_shell_surface_configure;
236
237         weston_surface_set_position(shell->home_surface, 0, 0);
238 }
239
240 static void
241 minimize_zoom_done(struct weston_surface_animation *zoom, void *data)
242 {
243         struct tablet_shell *shell = data;
244         struct weston_compositor *compositor = shell->compositor;
245         struct weston_seat *seat;
246
247         wl_list_for_each(seat, &compositor->seat_list, link)
248                 weston_surface_activate(shell->home_surface, seat);
249 }
250
251 static void
252 tablet_shell_switch_to(struct tablet_shell *shell,
253                              struct weston_surface *surface)
254 {
255         struct weston_compositor *compositor = shell->compositor;
256         struct weston_seat *seat;
257         struct weston_surface *current;
258
259         if (shell->state == STATE_SWITCHER) {
260                 wl_list_remove(&shell->switcher_listener.link);
261                 shell->switcher_surface = NULL;
262         };
263
264         if (surface == shell->home_surface) {
265                 tablet_shell_set_state(shell, STATE_HOME);
266
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);
271                 }
272         } else {
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);
278         }
279 }
280
281 static void
282 tablet_shell_show_grid(struct wl_client *client,
283                        struct wl_resource *resource,
284                        struct wl_resource *surface_resource)
285 {
286         struct tablet_shell *shell = resource->data;
287         struct weston_surface *es = surface_resource->data;
288
289         tablet_shell_switch_to(shell, es);
290 }
291
292 static void
293 tablet_shell_show_panels(struct wl_client *client,
294                          struct wl_resource *resource,
295                          struct wl_resource *surface_resource)
296 {
297         struct tablet_shell *shell = resource->data;
298         struct weston_surface *es = surface_resource->data;
299
300         tablet_shell_switch_to(shell, es);
301 }
302
303 static void
304 destroy_tablet_client(struct wl_resource *resource)
305 {
306         struct tablet_client *tablet_client =
307                 container_of(resource, struct tablet_client, resource);
308
309         free(tablet_client->name);
310         free(tablet_client);
311 }
312
313 static void
314 tablet_client_destroy(struct wl_client *client,
315                       struct wl_resource *resource)
316 {
317         wl_resource_destroy(resource);
318 }
319
320 static void
321 tablet_client_activate(struct wl_client *client, struct wl_resource *resource)
322 {
323         struct tablet_client *tablet_client = resource->data;
324         struct tablet_shell *shell = tablet_client->shell;
325
326         shell->current_client = tablet_client;
327         if (!tablet_client->surface)
328                 return;
329
330         tablet_shell_switch_to(shell, tablet_client->surface);
331 }
332
333 static const struct tablet_client_interface tablet_client_implementation = {
334         tablet_client_destroy,
335         tablet_client_activate
336 };
337
338 static void
339 tablet_shell_create_client(struct wl_client *client,
340                            struct wl_resource *resource,
341                            uint32_t id, const char *name, int fd)
342 {
343         struct tablet_shell *shell = resource->data;
344         struct weston_compositor *compositor = shell->compositor;
345         struct tablet_client *tablet_client;
346
347         tablet_client = malloc(sizeof *tablet_client);
348         if (tablet_client == NULL) {
349                 wl_resource_post_no_memory(resource);
350                 return;
351         }
352
353         tablet_client->client = wl_client_create(compositor->wl_display, fd);
354         tablet_client->shell = shell;
355         tablet_client->name = strdup(name);
356
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;
363
364         wl_client_add_resource(client, &tablet_client->resource);
365         tablet_client->surface = NULL;
366         shell->current_client = tablet_client;
367
368         weston_log("created client %p, id %d, name %s, fd %d\n",
369                 tablet_client->client, id, name, fd);
370 }
371
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
379 };
380
381 static void
382 launch_ux_daemon(struct tablet_shell *shell)
383 {
384         const char *shell_exe = LIBEXECDIR "/weston-tablet-shell";
385
386         shell->client = weston_client_launch(shell->compositor,
387                                            &shell->process,
388                                            shell_exe, tablet_shell_sigchld);
389 }
390
391 static void
392 toggle_switcher(struct tablet_shell *shell)
393 {
394         switch (shell->state) {
395         case STATE_SWITCHER:
396                 tablet_shell_send_hide_switcher(&shell->resource);
397                 break;
398         default:
399                 tablet_shell_send_show_switcher(&shell->resource);
400                 tablet_shell_set_state(shell, STATE_SWITCHER);
401                 break;
402         }
403 }
404
405 static void
406 tablet_shell_lock(struct wl_listener *listener, void *data)
407 {
408         struct tablet_shell *shell =
409                 container_of(listener, struct tablet_shell, lock_listener);
410
411         if (shell->state == STATE_LOCKED)
412                 return;
413         if (shell->state == STATE_SWITCHER)
414                 tablet_shell_send_hide_switcher(&shell->resource);
415
416         tablet_shell_send_show_lockscreen(&shell->resource);
417         tablet_shell_set_state(shell, STATE_LOCKED);
418 }
419
420 static void
421 tablet_shell_unlock(struct wl_listener *listener, void *data)
422 {
423         struct tablet_shell *shell =
424                 container_of(listener, struct tablet_shell, lock_listener);
425
426         weston_compositor_wake(shell->compositor);
427 }
428
429 static void
430 go_home(struct tablet_shell *shell, struct weston_seat *seat)
431 {
432         if (shell->state == STATE_SWITCHER)
433                 tablet_shell_send_hide_switcher(&shell->resource);
434
435         weston_surface_activate(shell->home_surface, seat);
436
437         tablet_shell_set_state(shell, STATE_HOME);
438 }
439
440 static int
441 long_press_handler(void *data)
442 {
443         struct tablet_shell *shell = data;
444
445         shell->long_press_active = 0;
446         toggle_switcher(shell);
447
448         return 1;
449 }
450
451 static void
452 menu_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
453 {
454         struct tablet_shell *shell = data;
455
456         if (shell->state == STATE_LOCKED)
457                 return;
458
459         toggle_switcher(shell);
460 }
461
462 static void
463 home_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
464 {
465         struct tablet_shell *shell = data;
466
467         if (shell->state == STATE_LOCKED)
468                 return;
469
470         if (1) {
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;
477
478                 switch (shell->state) {
479                 case STATE_HOME:
480                 case STATE_SWITCHER:
481                         toggle_switcher(shell);
482                         break;
483                 default:
484                         go_home(shell, (struct weston_seat *) seat);
485                         break;
486                 }
487         }
488 }
489
490 static void
491 destroy_tablet_shell(struct wl_resource *resource)
492 {
493 }
494
495 static void
496 bind_tablet_shell(struct wl_client *client, void *data, uint32_t version,
497                   uint32_t id)
498 {
499         struct tablet_shell *shell = data;
500
501         if (shell->client != client)
502                 /* Throw an error or just let the client fail when it
503                  * tries to access the object?. */
504                 return;
505
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;
513
514         wl_client_add_resource(client, &shell->resource);
515 }
516
517 static void
518 tablet_shell_destroy(struct wl_listener *listener, void *data)
519 {
520         struct tablet_shell *shell =
521                 container_of(listener, struct tablet_shell, destroy_listener);
522
523         if (shell->home_surface)
524                 shell->home_surface->configure = NULL;
525
526         if (shell->lockscreen_surface)
527                 shell->lockscreen_surface->configure = NULL;
528
529         wl_event_source_remove(shell->long_press_source);
530         free(shell);
531 }
532
533 WL_EXPORT int
534 module_init(struct weston_compositor *compositor)
535 {
536         struct tablet_shell *shell;
537         struct wl_event_loop *loop;
538
539         shell = malloc(sizeof *shell);
540         if (shell == NULL)
541                 return -1;
542
543         memset(shell, 0, sizeof *shell);
544         shell->compositor = compositor;
545
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);
552
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);
556
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);
560
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,
567                                           shell);
568         weston_compositor_add_key_binding(compositor, KEY_RIGHTMETA,
569                                           MODIFIER_SUPER, home_key_binding,
570                                           shell);
571         weston_compositor_add_key_binding(compositor, KEY_COMPOSE, 0,
572                                           menu_key_binding, shell);
573
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);
581
582         tablet_shell_set_state(shell, STATE_STARTING);
583
584         return 0;
585 }