The change of the configuration and the implementation of the event input.
[profile/ivi/ico-uxf-weston-plugin.git] / src / ico_ivi_shell.c
1 /*
2  * Copyright © 2010-2012 Intel Corporation
3  * Copyright © 2011-2012 Collabora, Ltd.
4  * Copyright © 2013 TOYOTA MOTOR CORPORATION.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and
7  * its documentation for any purpose is hereby granted without fee, provided
8  * that the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of the copyright holders not be used in
11  * advertising or publicity pertaining to distribution of the software
12  * without specific, written prior permission.  The copyright holders make
13  * no representations about the suitability of this software for any
14  * purpose.  It is provided "as is" without express or implied warranty.
15  *
16  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  */
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <stdbool.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <linux/input.h>
31 #include <assert.h>
32 #include <signal.h>
33 #include <math.h>
34 #include <sys/types.h>
35
36 #include <wayland-server.h>
37 #include <weston/compositor.h>
38 #include "desktop-shell-server-protocol.h"
39 #include "input-method-server-protocol.h"
40 #include "workspaces-server-protocol.h"
41 #include <weston/config-parser.h>
42 #include "ico_ivi_shell.h"
43
44 #define DEFAULT_NUM_WORKSPACES 1
45 #define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
46
47 enum animation_type {
48     ANIMATION_NONE,
49
50     ANIMATION_ZOOM,
51     ANIMATION_FADE
52 };
53
54 enum fade_type {
55     FADE_IN,
56     FADE_OUT
57 };
58
59 struct focus_state {
60     struct weston_seat *seat;
61     struct workspace *ws;
62     struct weston_surface *keyboard_focus;
63     struct wl_list link;
64     struct wl_listener seat_destroy_listener;
65     struct wl_listener surface_destroy_listener;
66 };
67
68 struct workspace {
69     struct weston_layer layer;
70
71     struct wl_list focus_list;
72     struct wl_listener seat_destroyed_listener;
73 };
74
75 struct input_panel_surface {
76     struct wl_resource *resource;
77     struct wl_signal destroy_signal;
78
79     struct desktop_shell *shell;
80
81     struct wl_list link;
82     struct weston_surface *surface;
83     struct wl_listener surface_destroy_listener;
84
85     struct weston_output *output;
86     uint32_t panel;
87 };
88
89 struct desktop_shell {
90     struct weston_compositor *compositor;
91
92     struct wl_listener idle_listener;
93     struct wl_listener wake_listener;
94     struct wl_listener destroy_listener;
95     struct wl_listener show_input_panel_listener;
96     struct wl_listener hide_input_panel_listener;
97     struct wl_listener update_input_panel_listener;
98
99     struct weston_layer fullscreen_layer;
100     struct weston_layer panel_layer;
101     struct weston_layer background_layer;
102     struct weston_layer lock_layer;
103     struct weston_layer input_panel_layer;
104
105     struct wl_listener pointer_focus_listener;
106     struct weston_surface *grab_surface;
107
108     struct {
109         struct weston_process process;
110         struct wl_client *client;
111         struct wl_resource *desktop_shell;
112
113         unsigned deathcount;
114         uint32_t deathstamp;
115     } child;
116
117     bool locked;
118     bool showing_input_panels;
119     bool prepare_event_sent;
120
121     struct {
122         struct weston_surface *surface;
123         pixman_box32_t cursor_rectangle;
124     } text_input;
125
126     struct weston_surface *lock_surface;
127     struct wl_listener lock_surface_listener;
128
129     struct {
130         struct wl_array array;
131         unsigned int current;
132         unsigned int num;
133
134         struct wl_list client_list;
135
136         struct weston_animation animation;
137         struct wl_list anim_sticky_list;
138         int anim_dir;
139         uint32_t anim_timestamp;
140         double anim_current;
141         struct workspace *anim_from;
142         struct workspace *anim_to;
143     } workspaces;
144
145     struct {
146         char *path;
147         int duration;
148         struct wl_resource *binding;
149         struct weston_process process;
150         struct wl_event_source *timer;
151     } screensaver;
152
153     struct {
154         struct wl_resource *binding;
155         struct wl_list surfaces;
156     } input_panel;
157
158     struct {
159         struct weston_surface *surface;
160         struct weston_surface_animation *animation;
161         enum fade_type type;
162         struct wl_event_source *startup_timer;
163     } fade;
164
165     uint32_t binding_modifier;
166     enum animation_type win_animation_type;
167 };
168
169 enum shell_surface_type {
170     SHELL_SURFACE_NONE,
171     SHELL_SURFACE_TOPLEVEL,
172     SHELL_SURFACE_TRANSIENT,
173     SHELL_SURFACE_FULLSCREEN,
174     SHELL_SURFACE_MAXIMIZED,
175     SHELL_SURFACE_POPUP,
176     SHELL_SURFACE_XWAYLAND
177 };
178
179 struct ping_timer {
180     struct wl_event_source *source;
181     uint32_t serial;
182 };
183
184 struct shell_surface {
185     struct wl_resource *resource;
186     struct wl_signal destroy_signal;
187
188     struct weston_surface *surface;
189     struct wl_listener surface_destroy_listener;
190     struct weston_surface *parent;
191     struct desktop_shell *shell;
192
193     enum shell_surface_type type, next_type;
194     char *title, *class;
195     int32_t saved_x, saved_y;
196     bool saved_position_valid;
197     bool saved_rotation_valid;
198     int unresponsive;
199
200     struct {
201         struct weston_transform transform;
202         struct weston_matrix rotation;
203     } rotation;
204
205     struct {
206         struct wl_list grab_link;
207         int32_t x, y;
208         struct shell_seat *shseat;
209         uint32_t serial;
210     } popup;
211
212     struct {
213         int32_t x, y;
214         uint32_t flags;
215     } transient;
216
217     struct {
218         enum wl_shell_surface_fullscreen_method type;
219         struct weston_transform transform; /* matrix from x, y */
220         uint32_t framerate;
221         struct weston_surface *black_surface;
222     } fullscreen;
223
224     struct ping_timer *ping_timer;
225
226     struct weston_transform workspace_transform;
227
228     struct weston_output *fullscreen_output;
229     struct weston_output *output;
230     struct wl_list link;
231
232     const struct weston_shell_client *client;
233 };
234
235 struct shell_grab {
236     struct weston_pointer_grab grab;
237     struct shell_surface *shsurf;
238     struct wl_listener shsurf_destroy_listener;
239 };
240
241 struct shell_touch_grab {
242     struct weston_touch_grab grab;
243     struct shell_surface *shsurf;
244     struct wl_listener shsurf_destroy_listener;
245     struct weston_touch *touch;
246 };
247
248 struct weston_move_grab {
249     struct shell_grab base;
250     wl_fixed_t dx, dy;
251 };
252
253 struct weston_touch_move_grab {
254     struct shell_touch_grab base;
255     wl_fixed_t dx, dy;
256 };
257
258 struct rotate_grab {
259     struct shell_grab base;
260     struct weston_matrix rotation;
261     struct {
262         float x;
263         float y;
264     } center;
265 };
266
267 struct shell_seat {
268     struct weston_seat *seat;
269     struct wl_listener seat_destroy_listener;
270
271     struct {
272         struct weston_pointer_grab grab;
273         struct wl_list surfaces_list;
274         struct wl_client *client;
275         int32_t initial_up;
276     } popup_grab;
277 };
278
279 static void
280 activate(struct desktop_shell *shell, struct weston_surface *es,
281      struct weston_seat *seat);
282
283 static struct workspace *
284 get_current_workspace(struct desktop_shell *shell);
285
286 static struct shell_surface *
287 get_shell_surface(struct weston_surface *surface);
288
289 static struct desktop_shell *
290 shell_surface_get_shell(struct shell_surface *shsurf);
291
292 static void
293 surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
294
295 static void
296 shell_fade_startup(struct desktop_shell *shell);
297
298 /* shell management table           */
299 static struct desktop_shell *_ico_ivi_shell = NULL;
300 /* shell program path for ico-ivi   */
301 static char *shell_exe = NULL;
302 #define DEFAULT_DEBUG_LEVEL 4
303 static int  ico_debug_level = DEFAULT_DEBUG_LEVEL;  /* Debug Level                  */
304
305 /* debug log macros         */
306 #define uifw_debug(fmt,...)  \
307     { if (ico_debug_level >= 5) {weston_log("DBG>"fmt" (%s:%d)\n",##__VA_ARGS__,__FILE__,__LINE__);} }
308 #define uifw_trace(fmt,...)  \
309     { if (ico_debug_level >= 4) {weston_log("TRC>"fmt" (%s:%d)\n",##__VA_ARGS__,__FILE__,__LINE__);} }
310 #define uifw_info(fmt,...)  \
311     { if (ico_debug_level >= 3) {weston_log("INF>"fmt" (%s:%d)\n",##__VA_ARGS__,__FILE__,__LINE__);} }
312 #define uifw_warn(fmt,...)  \
313     { if (ico_debug_level >= 2) {weston_log("WRN>"fmt" (%s:%d)\n",##__VA_ARGS__,__FILE__,__LINE__);} }
314 #define uifw_error(fmt,...)  \
315     { if (ico_debug_level >= 1) {weston_log("ERR>"fmt" (%s:%d)\n",##__VA_ARGS__,__FILE__,__LINE__);} }
316
317 /* hook functions           */
318 static void (*shell_hook_bind)(struct wl_client *client, void *shell) = NULL;
319 static void (*shell_hook_unbind)(struct wl_client *client) = NULL;
320 static void (*shell_hook_create)(struct wl_client *client, struct wl_resource *resource,
321                                  struct weston_surface *surface,
322                                  struct shell_surface *shsurf) = NULL;
323 static void (*shell_hook_destroy)(struct weston_surface *surface) = NULL;
324 static void (*shell_hook_map)(struct weston_surface *surface, int32_t *width,
325                               int32_t *height, int32_t *sx, int32_t *sy) = NULL;
326 static void (*shell_hook_configure)(struct weston_surface *surface) = NULL;
327 static void (*shell_hook_select)(struct weston_surface *surface) = NULL;
328 static void (*shell_hook_title)(struct weston_surface *surface, const char *title) = NULL;
329 static void (*shell_hook_move)(struct weston_surface *surface, int *dx, int *dy) = NULL;
330
331 static bool
332 shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
333 {
334     struct desktop_shell *shell;
335     struct weston_surface *top_fs_es;
336
337     shell = shell_surface_get_shell(shsurf);
338
339     if (wl_list_empty(&shell->fullscreen_layer.surface_list))
340         return false;
341
342     top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
343                      struct weston_surface,
344                  layer_link);
345     return (shsurf == get_shell_surface(top_fs_es));
346 }
347
348 static void
349 destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
350 {
351     struct shell_grab *grab;
352
353     grab = container_of(listener, struct shell_grab,
354                 shsurf_destroy_listener);
355
356     grab->shsurf = NULL;
357 }
358
359 static void
360 popup_grab_end(struct weston_pointer *pointer);
361
362 static void
363 shell_grab_start(struct shell_grab *grab,
364          const struct weston_pointer_grab_interface *interface,
365          struct shell_surface *shsurf,
366          struct weston_pointer *pointer,
367          enum desktop_shell_cursor cursor)
368 {
369     struct desktop_shell *shell = shsurf->shell;
370
371     /* if ico_window_mgr hook, not change grab  */
372     if (shell_hook_select)  {
373         return;
374     }
375
376     popup_grab_end(pointer);
377
378     grab->grab.interface = interface;
379     grab->shsurf = shsurf;
380     grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
381     wl_signal_add(&shsurf->destroy_signal,
382               &grab->shsurf_destroy_listener);
383
384     weston_pointer_start_grab(pointer, &grab->grab);
385     if (shell->child.desktop_shell) {
386         desktop_shell_send_grab_cursor(shell->child.desktop_shell,
387                            cursor);
388         weston_pointer_set_focus(pointer, shell->grab_surface,
389                      wl_fixed_from_int(0),
390                      wl_fixed_from_int(0));
391     }
392 }
393
394 static void
395 shell_grab_end(struct shell_grab *grab)
396 {
397     if (grab->shsurf)
398         wl_list_remove(&grab->shsurf_destroy_listener.link);
399
400     weston_pointer_end_grab(grab->grab.pointer);
401 }
402
403 static void
404 shell_touch_grab_start(struct shell_touch_grab *grab,
405                const struct weston_touch_grab_interface *interface,
406                struct shell_surface *shsurf,
407                struct weston_touch *touch)
408 {
409     struct desktop_shell *shell = shsurf->shell;
410
411     grab->grab.interface = interface;
412     grab->shsurf = shsurf;
413     grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
414     wl_signal_add(&shsurf->destroy_signal,
415               &grab->shsurf_destroy_listener);
416
417     grab->touch = touch;
418
419     weston_touch_start_grab(touch, &grab->grab);
420     if (shell->child.desktop_shell)
421         weston_touch_set_focus(touch->seat, shell->grab_surface);
422 }
423
424 static void
425 shell_touch_grab_end(struct shell_touch_grab *grab)
426 {
427     if (grab->shsurf)
428         wl_list_remove(&grab->shsurf_destroy_listener.link);
429
430     weston_touch_end_grab(grab->touch);
431 }
432
433 static void
434 center_on_output(struct weston_surface *surface,
435          struct weston_output *output);
436
437 static enum weston_keyboard_modifier
438 get_modifier(char *modifier)
439 {
440     if (!modifier)
441         return MODIFIER_SUPER;
442
443     if (!strcmp("ctrl", modifier))
444         return MODIFIER_CTRL;
445     else if (!strcmp("alt", modifier))
446         return MODIFIER_ALT;
447     else if (!strcmp("super", modifier))
448         return MODIFIER_SUPER;
449     else
450         return MODIFIER_SUPER;
451 }
452
453 static enum animation_type
454 get_animation_type(char *animation)
455 {
456     if (!animation)
457         return ANIMATION_NONE;
458
459     if (!strcmp("zoom", animation))
460         return ANIMATION_ZOOM;
461     else if (!strcmp("fade", animation))
462         return ANIMATION_FADE;
463     else
464         return ANIMATION_NONE;
465 }
466
467 static void
468 shell_configuration(struct desktop_shell *shell)
469 {
470     struct weston_config_section *section;
471     int duration;
472     char *s;
473
474     section = weston_config_get_section(shell->compositor->config,
475                         "screensaver", NULL, NULL);
476     weston_config_section_get_string(section,
477                      "path", &shell->screensaver.path, NULL);
478     weston_config_section_get_int(section, "duration", &duration, 60);
479     shell->screensaver.duration = duration * 1000;
480
481     section = weston_config_get_section(shell->compositor->config,
482                         "shell", NULL, NULL);
483     weston_config_section_get_string(section,
484                      "binding-modifier", &s, "super");
485     shell->binding_modifier = get_modifier(s);
486     free(s);
487     weston_config_section_get_string(section, "animation", &s, "none");
488     shell->win_animation_type = get_animation_type(s);
489     free(s);
490     weston_config_section_get_uint(section, "num-workspaces",
491                        &shell->workspaces.num,
492                        DEFAULT_NUM_WORKSPACES);
493     /* shell program path for ico-ivi   */
494     weston_config_section_get_string(section, "shell-exe", &shell_exe,
495                                      LIBEXECDIR "/weston-desktop-shell");
496     weston_log("ws=%d exe=%s\n", shell->workspaces.num, shell_exe);
497
498     /* get debug level for ivi debug    */
499     section = weston_config_get_section(shell->compositor->config, "ivi-debug", NULL, NULL);
500     if (section)    {
501         weston_config_section_get_int(section, "log",
502                                       &ico_debug_level, DEFAULT_DEBUG_LEVEL);
503     }
504 }
505
506 static void
507 focus_state_destroy(struct focus_state *state)
508 {
509     wl_list_remove(&state->seat_destroy_listener.link);
510     wl_list_remove(&state->surface_destroy_listener.link);
511     free(state);
512 }
513
514 static void
515 focus_state_seat_destroy(struct wl_listener *listener, void *data)
516 {
517     struct focus_state *state = container_of(listener,
518                          struct focus_state,
519                          seat_destroy_listener);
520
521     wl_list_remove(&state->link);
522     focus_state_destroy(state);
523 }
524
525 static void
526 focus_state_surface_destroy(struct wl_listener *listener, void *data)
527 {
528     struct focus_state *state = container_of(listener,
529                          struct focus_state,
530                          surface_destroy_listener);
531     struct desktop_shell *shell;
532     struct weston_surface *main_surface;
533     struct weston_surface *surface, *next;
534
535     main_surface = weston_surface_get_main_surface(state->keyboard_focus);
536
537     next = NULL;
538     wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
539         if (surface == main_surface)
540             continue;
541
542         next = surface;
543         break;
544     }
545
546     /* if the focus was a sub-surface, activate its main surface */
547     if (main_surface != state->keyboard_focus)
548         next = main_surface;
549
550     if (next) {
551         shell = state->seat->compositor->shell_interface.shell;
552         activate(shell, next, state->seat);
553     } else {
554         wl_list_remove(&state->link);
555         focus_state_destroy(state);
556     }
557 }
558
559 static struct focus_state *
560 focus_state_create(struct weston_seat *seat, struct workspace *ws)
561 {
562     struct focus_state *state;
563
564     state = malloc(sizeof *state);
565     if (state == NULL)
566         return NULL;
567
568     state->ws = ws;
569     state->seat = seat;
570     wl_list_insert(&ws->focus_list, &state->link);
571
572     state->seat_destroy_listener.notify = focus_state_seat_destroy;
573     state->surface_destroy_listener.notify = focus_state_surface_destroy;
574     wl_signal_add(&seat->destroy_signal,
575               &state->seat_destroy_listener);
576     wl_list_init(&state->surface_destroy_listener.link);
577
578     return state;
579 }
580
581 static struct focus_state *
582 ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
583 {
584     struct workspace *ws = get_current_workspace(shell);
585     struct focus_state *state;
586
587     wl_list_for_each(state, &ws->focus_list, link)
588         if (state->seat == seat)
589             break;
590
591     if (&state->link == &ws->focus_list)
592         state = focus_state_create(seat, ws);
593
594     return state;
595 }
596
597 static void
598 restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
599 {
600     struct focus_state *state, *next;
601     struct weston_surface *surface;
602
603     wl_list_for_each_safe(state, next, &ws->focus_list, link) {
604         surface = state->keyboard_focus;
605
606         weston_keyboard_set_focus(state->seat->keyboard, surface);
607     }
608 }
609
610 static void
611 replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
612             struct weston_seat *seat)
613 {
614     struct focus_state *state;
615     struct weston_surface *surface;
616
617     wl_list_for_each(state, &ws->focus_list, link) {
618         if (state->seat == seat) {
619             surface = seat->keyboard->focus;
620             state->keyboard_focus = surface;
621             return;
622         }
623     }
624 }
625
626 static void
627 drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
628          struct weston_surface *surface)
629 {
630     struct focus_state *state;
631
632     wl_list_for_each(state, &ws->focus_list, link)
633         if (state->keyboard_focus == surface)
634             state->keyboard_focus = NULL;
635 }
636
637 static void
638 workspace_destroy(struct workspace *ws)
639 {
640     struct focus_state *state, *next;
641
642     wl_list_for_each_safe(state, next, &ws->focus_list, link)
643         focus_state_destroy(state);
644
645     free(ws);
646 }
647
648 static void
649 seat_destroyed(struct wl_listener *listener, void *data)
650 {
651     struct weston_seat *seat = data;
652     struct focus_state *state, *next;
653     struct workspace *ws = container_of(listener,
654                         struct workspace,
655                         seat_destroyed_listener);
656
657     wl_list_for_each_safe(state, next, &ws->focus_list, link)
658         if (state->seat == seat)
659             wl_list_remove(&state->link);
660 }
661
662 static struct workspace *
663 workspace_create(void)
664 {
665     struct workspace *ws = malloc(sizeof *ws);
666     if (ws == NULL)
667         return NULL;
668
669     weston_layer_init(&ws->layer, NULL);
670
671     wl_list_init(&ws->focus_list);
672     wl_list_init(&ws->seat_destroyed_listener.link);
673     ws->seat_destroyed_listener.notify = seat_destroyed;
674
675     return ws;
676 }
677
678 static int
679 workspace_is_empty(struct workspace *ws)
680 {
681     return wl_list_empty(&ws->layer.surface_list);
682 }
683
684 static struct workspace *
685 get_workspace(struct desktop_shell *shell, unsigned int index)
686 {
687     struct workspace **pws = shell->workspaces.array.data;
688     assert(index < shell->workspaces.num);
689     pws += index;
690     return *pws;
691 }
692
693 static struct workspace *
694 get_current_workspace(struct desktop_shell *shell)
695 {
696     return get_workspace(shell, shell->workspaces.current);
697 }
698
699 static void
700 activate_workspace(struct desktop_shell *shell, unsigned int index)
701 {
702     struct workspace *ws;
703
704     ws = get_workspace(shell, index);
705     wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
706
707     shell->workspaces.current = index;
708 }
709
710 static unsigned int
711 get_output_height(struct weston_output *output)
712 {
713     return abs(output->region.extents.y1 - output->region.extents.y2);
714 }
715
716 static void
717 surface_translate(struct weston_surface *surface, double d)
718 {
719     struct shell_surface *shsurf = get_shell_surface(surface);
720     struct weston_transform *transform;
721
722     transform = &shsurf->workspace_transform;
723     if (wl_list_empty(&transform->link))
724         wl_list_insert(surface->geometry.transformation_list.prev,
725                    &shsurf->workspace_transform.link);
726
727     weston_matrix_init(&shsurf->workspace_transform.matrix);
728     weston_matrix_translate(&shsurf->workspace_transform.matrix,
729                 0.0, d, 0.0);
730     weston_surface_geometry_dirty(surface);
731 }
732
733 static void
734 workspace_translate_out(struct workspace *ws, double fraction)
735 {
736     struct weston_surface *surface;
737     unsigned int height;
738     double d;
739
740     wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
741         height = get_output_height(surface->output);
742         d = height * fraction;
743
744         surface_translate(surface, d);
745     }
746 }
747
748 static void
749 workspace_translate_in(struct workspace *ws, double fraction)
750 {
751     struct weston_surface *surface;
752     unsigned int height;
753     double d;
754
755     wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
756         height = get_output_height(surface->output);
757
758         if (fraction > 0)
759             d = -(height - height * fraction);
760         else
761             d = height + height * fraction;
762
763         surface_translate(surface, d);
764     }
765 }
766
767 static void
768 broadcast_current_workspace_state(struct desktop_shell *shell)
769 {
770     struct wl_list *link;
771
772     for (link = shell->workspaces.client_list.next;
773          link != &shell->workspaces.client_list;
774          link = link->next) {
775         workspace_manager_send_state(wl_resource_from_link(link),
776                          shell->workspaces.current,
777                          shell->workspaces.num);
778     }
779 }
780
781 static void
782 reverse_workspace_change_animation(struct desktop_shell *shell,
783                    unsigned int index,
784                    struct workspace *from,
785                    struct workspace *to)
786 {
787     shell->workspaces.current = index;
788
789     shell->workspaces.anim_to = to;
790     shell->workspaces.anim_from = from;
791     shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
792     shell->workspaces.anim_timestamp = 0;
793
794     weston_compositor_schedule_repaint(shell->compositor);
795 }
796
797 static void
798 workspace_deactivate_transforms(struct workspace *ws)
799 {
800     struct weston_surface *surface;
801     struct shell_surface *shsurf;
802
803     wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
804         shsurf = get_shell_surface(surface);
805         if (!wl_list_empty(&shsurf->workspace_transform.link)) {
806             wl_list_remove(&shsurf->workspace_transform.link);
807             wl_list_init(&shsurf->workspace_transform.link);
808         }
809         weston_surface_geometry_dirty(surface);
810     }
811 }
812
813 static void
814 finish_workspace_change_animation(struct desktop_shell *shell,
815                   struct workspace *from,
816                   struct workspace *to)
817 {
818     weston_compositor_schedule_repaint(shell->compositor);
819
820     wl_list_remove(&shell->workspaces.animation.link);
821     workspace_deactivate_transforms(from);
822     workspace_deactivate_transforms(to);
823     shell->workspaces.anim_to = NULL;
824
825     wl_list_remove(&shell->workspaces.anim_from->layer.link);
826 }
827
828 static void
829 animate_workspace_change_frame(struct weston_animation *animation,
830                    struct weston_output *output, uint32_t msecs)
831 {
832     struct desktop_shell *shell =
833         container_of(animation, struct desktop_shell,
834                  workspaces.animation);
835     struct workspace *from = shell->workspaces.anim_from;
836     struct workspace *to = shell->workspaces.anim_to;
837     uint32_t t;
838     double x, y;
839
840     if (workspace_is_empty(from) && workspace_is_empty(to)) {
841         finish_workspace_change_animation(shell, from, to);
842         return;
843     }
844
845     if (shell->workspaces.anim_timestamp == 0) {
846         if (shell->workspaces.anim_current == 0.0)
847             shell->workspaces.anim_timestamp = msecs;
848         else
849             shell->workspaces.anim_timestamp =
850                 msecs -
851                 /* Invers of movement function 'y' below. */
852                 (asin(1.0 - shell->workspaces.anim_current) *
853                  DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
854                  M_2_PI);
855     }
856
857     t = msecs - shell->workspaces.anim_timestamp;
858
859     /*
860      * x = [0, π/2]
861      * y(x) = sin(x)
862      */
863     x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
864     y = sin(x);
865
866     if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
867         weston_compositor_schedule_repaint(shell->compositor);
868
869         workspace_translate_out(from, shell->workspaces.anim_dir * y);
870         workspace_translate_in(to, shell->workspaces.anim_dir * y);
871         shell->workspaces.anim_current = y;
872
873         weston_compositor_schedule_repaint(shell->compositor);
874     }
875     else
876         finish_workspace_change_animation(shell, from, to);
877 }
878
879 static void
880 animate_workspace_change(struct desktop_shell *shell,
881              unsigned int index,
882              struct workspace *from,
883              struct workspace *to)
884 {
885     struct weston_output *output;
886
887     int dir;
888
889     if (index > shell->workspaces.current)
890         dir = -1;
891     else
892         dir = 1;
893
894     shell->workspaces.current = index;
895
896     shell->workspaces.anim_dir = dir;
897     shell->workspaces.anim_from = from;
898     shell->workspaces.anim_to = to;
899     shell->workspaces.anim_current = 0.0;
900     shell->workspaces.anim_timestamp = 0;
901
902     output = container_of(shell->compositor->output_list.next,
903                   struct weston_output, link);
904     wl_list_insert(&output->animation_list,
905                &shell->workspaces.animation.link);
906
907     wl_list_insert(from->layer.link.prev, &to->layer.link);
908
909     workspace_translate_in(to, 0);
910
911     restore_focus_state(shell, to);
912
913     weston_compositor_schedule_repaint(shell->compositor);
914 }
915
916 static void
917 update_workspace(struct desktop_shell *shell, unsigned int index,
918          struct workspace *from, struct workspace *to)
919 {
920     shell->workspaces.current = index;
921     wl_list_insert(&from->layer.link, &to->layer.link);
922     wl_list_remove(&from->layer.link);
923 }
924
925 static void
926 change_workspace(struct desktop_shell *shell, unsigned int index)
927 {
928     struct workspace *from;
929     struct workspace *to;
930
931     if (index == shell->workspaces.current)
932         return;
933
934     /* Don't change workspace when there is any fullscreen surfaces. */
935     if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
936         return;
937
938     from = get_current_workspace(shell);
939     to = get_workspace(shell, index);
940
941     if (shell->workspaces.anim_from == to &&
942         shell->workspaces.anim_to == from) {
943         restore_focus_state(shell, to);
944         reverse_workspace_change_animation(shell, index, from, to);
945         broadcast_current_workspace_state(shell);
946         return;
947     }
948
949     if (shell->workspaces.anim_to != NULL)
950         finish_workspace_change_animation(shell,
951                           shell->workspaces.anim_from,
952                           shell->workspaces.anim_to);
953
954     restore_focus_state(shell, to);
955
956     if (workspace_is_empty(to) && workspace_is_empty(from))
957         update_workspace(shell, index, from, to);
958     else
959         animate_workspace_change(shell, index, from, to);
960
961     broadcast_current_workspace_state(shell);
962 }
963
964 static bool
965 workspace_has_only(struct workspace *ws, struct weston_surface *surface)
966 {
967     struct wl_list *list = &ws->layer.surface_list;
968     struct wl_list *e;
969
970     if (wl_list_empty(list))
971         return false;
972
973     e = list->next;
974
975     if (e->next != list)
976         return false;
977
978     return container_of(e, struct weston_surface, layer_link) == surface;
979 }
980
981 static void
982 move_surface_to_workspace(struct desktop_shell *shell,
983               struct weston_surface *surface,
984               uint32_t workspace)
985 {
986     struct workspace *from;
987     struct workspace *to;
988     struct weston_seat *seat;
989     struct weston_surface *focus;
990
991     assert(weston_surface_get_main_surface(surface) == surface);
992
993     if (workspace == shell->workspaces.current)
994         return;
995
996     if (workspace >= shell->workspaces.num)
997         workspace = shell->workspaces.num - 1;
998
999     from = get_current_workspace(shell);
1000     to = get_workspace(shell, workspace);
1001
1002     wl_list_remove(&surface->layer_link);
1003     wl_list_insert(&to->layer.surface_list, &surface->layer_link);
1004
1005     drop_focus_state(shell, from, surface);
1006     wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1007         if (!seat->keyboard)
1008             continue;
1009
1010         focus = weston_surface_get_main_surface(seat->keyboard->focus);
1011         if (focus == surface)
1012             weston_keyboard_set_focus(seat->keyboard, NULL);
1013     }
1014
1015     weston_surface_damage_below(surface);
1016 }
1017
1018 static void
1019 take_surface_to_workspace_by_seat(struct desktop_shell *shell,
1020                   struct weston_seat *seat,
1021                   unsigned int index)
1022 {
1023     struct weston_surface *surface;
1024     struct shell_surface *shsurf;
1025     struct workspace *from;
1026     struct workspace *to;
1027     struct focus_state *state;
1028
1029     surface = weston_surface_get_main_surface(seat->keyboard->focus);
1030     if (surface == NULL ||
1031         index == shell->workspaces.current)
1032         return;
1033
1034     from = get_current_workspace(shell);
1035     to = get_workspace(shell, index);
1036
1037     wl_list_remove(&surface->layer_link);
1038     wl_list_insert(&to->layer.surface_list, &surface->layer_link);
1039
1040     replace_focus_state(shell, to, seat);
1041     drop_focus_state(shell, from, surface);
1042
1043     if (shell->workspaces.anim_from == to &&
1044         shell->workspaces.anim_to == from) {
1045         wl_list_remove(&to->layer.link);
1046         wl_list_insert(from->layer.link.prev, &to->layer.link);
1047
1048         reverse_workspace_change_animation(shell, index, from, to);
1049         broadcast_current_workspace_state(shell);
1050
1051         return;
1052     }
1053
1054     if (shell->workspaces.anim_to != NULL)
1055         finish_workspace_change_animation(shell,
1056                           shell->workspaces.anim_from,
1057                           shell->workspaces.anim_to);
1058
1059     if (workspace_is_empty(from) &&
1060         workspace_has_only(to, surface))
1061         update_workspace(shell, index, from, to);
1062     else {
1063         shsurf = get_shell_surface(surface);
1064         if (wl_list_empty(&shsurf->workspace_transform.link))
1065             wl_list_insert(&shell->workspaces.anim_sticky_list,
1066                        &shsurf->workspace_transform.link);
1067
1068         animate_workspace_change(shell, index, from, to);
1069     }
1070
1071     broadcast_current_workspace_state(shell);
1072
1073     state = ensure_focus_state(shell, seat);
1074     if (state != NULL)
1075         state->keyboard_focus = surface;
1076 }
1077
1078 static void
1079 workspace_manager_move_surface(struct wl_client *client,
1080                    struct wl_resource *resource,
1081                    struct wl_resource *surface_resource,
1082                    uint32_t workspace)
1083 {
1084     struct desktop_shell *shell = wl_resource_get_user_data(resource);
1085     struct weston_surface *surface =
1086         wl_resource_get_user_data(surface_resource);
1087     struct weston_surface *main_surface;
1088
1089     main_surface = weston_surface_get_main_surface(surface);
1090     move_surface_to_workspace(shell, main_surface, workspace);
1091 }
1092
1093 static const struct workspace_manager_interface workspace_manager_implementation = {
1094     workspace_manager_move_surface,
1095 };
1096
1097 static void
1098 unbind_resource(struct wl_resource *resource)
1099 {
1100     wl_list_remove(wl_resource_get_link(resource));
1101 }
1102
1103 static void
1104 bind_workspace_manager(struct wl_client *client,
1105                void *data, uint32_t version, uint32_t id)
1106 {
1107     struct desktop_shell *shell = data;
1108     struct wl_resource *resource;
1109
1110     resource = wl_resource_create(client,
1111                       &workspace_manager_interface, 1, id);
1112
1113     if (resource == NULL) {
1114         weston_log("couldn't add workspace manager object");
1115         return;
1116     }
1117
1118     wl_resource_set_implementation(resource,
1119                        &workspace_manager_implementation,
1120                        shell, unbind_resource);
1121     wl_list_insert(&shell->workspaces.client_list,
1122                wl_resource_get_link(resource));
1123
1124     workspace_manager_send_state(resource,
1125                      shell->workspaces.current,
1126                      shell->workspaces.num);
1127 }
1128
1129 static void
1130 touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1131              int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1132 {
1133 }
1134
1135 static void
1136 touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1137 {
1138     struct shell_touch_grab *shell_grab = container_of(grab,
1139                                struct shell_touch_grab,
1140                                grab);
1141     shell_touch_grab_end(shell_grab);
1142 }
1143
1144 static void
1145 touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1146                int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1147 {
1148     struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1149     struct shell_surface *shsurf = move->base.shsurf;
1150     struct weston_surface *es;
1151     int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1152     int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1153
1154     if (!shsurf)
1155         return;
1156
1157     es = shsurf->surface;
1158
1159     weston_surface_configure(es, dx, dy,
1160                  es->geometry.width, es->geometry.height);
1161
1162     weston_compositor_schedule_repaint(es->compositor);
1163 }
1164
1165 static const struct weston_touch_grab_interface touch_move_grab_interface = {
1166     touch_move_grab_down,
1167     touch_move_grab_up,
1168     touch_move_grab_motion,
1169 };
1170
1171 static int
1172 surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1173 {
1174     struct weston_touch_move_grab *move;
1175
1176     if (!shsurf)
1177         return -1;
1178
1179     if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1180         return 0;
1181
1182     move = malloc(sizeof *move);
1183     if (!move)
1184         return -1;
1185
1186     move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
1187             seat->touch->grab_x;
1188     move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
1189             seat->touch->grab_y;
1190
1191     shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1192                    seat->touch);
1193
1194     return 0;
1195 }
1196
1197 static void
1198 noop_grab_focus(struct weston_pointer_grab *grab)
1199 {
1200 }
1201
1202 static void
1203 move_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
1204 {
1205     struct weston_move_grab *move = (struct weston_move_grab *) grab;
1206     struct weston_pointer *pointer = grab->pointer;
1207     struct shell_surface *shsurf = move->base.shsurf;
1208     struct weston_surface *es;
1209     int dx = wl_fixed_to_int(pointer->x + move->dx);
1210     int dy = wl_fixed_to_int(pointer->y + move->dy);
1211
1212     if (!shsurf)
1213         return;
1214
1215     es = shsurf->surface;
1216
1217     /* ico-ivi-shell hook move      */
1218     if (shell_hook_move)    {
1219         (*shell_hook_move)(shsurf->surface, &dx, &dy);
1220     }
1221     weston_surface_configure(es, dx, dy,
1222                  es->geometry.width, es->geometry.height);
1223
1224     weston_compositor_schedule_repaint(es->compositor);
1225 }
1226
1227 static void
1228 move_grab_button(struct weston_pointer_grab *grab,
1229          uint32_t time, uint32_t button, uint32_t state_w)
1230 {
1231     struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1232                             grab);
1233     struct weston_pointer *pointer = grab->pointer;
1234     enum wl_pointer_button_state state = state_w;
1235
1236     if (pointer->button_count == 0 &&
1237         state == WL_POINTER_BUTTON_STATE_RELEASED) {
1238         shell_grab_end(shell_grab);
1239         free(grab);
1240     }
1241 }
1242
1243 static const struct weston_pointer_grab_interface move_grab_interface = {
1244     noop_grab_focus,
1245     move_grab_motion,
1246     move_grab_button,
1247 };
1248
1249 static int
1250 surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
1251 {
1252     struct weston_move_grab *move;
1253
1254     if (!shsurf)
1255         return -1;
1256
1257     if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1258         return 0;
1259
1260     move = malloc(sizeof *move);
1261     if (!move)
1262         return -1;
1263
1264     move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
1265             seat->pointer->grab_x;
1266     move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
1267             seat->pointer->grab_y;
1268
1269     shell_grab_start(&move->base, &move_grab_interface, shsurf,
1270              seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
1271
1272     return 0;
1273 }
1274
1275 static void
1276 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1277            struct wl_resource *seat_resource, uint32_t serial)
1278 {
1279     struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
1280     struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1281     struct weston_surface *surface;
1282
1283     surface = weston_surface_get_main_surface(seat->pointer->focus);
1284     if (seat->pointer->button_count > 0 && seat->pointer->grab_serial == serial) {
1285         surface = weston_surface_get_main_surface(seat->pointer->focus);
1286         if ((surface == shsurf->surface) &&
1287             (surface_move(shsurf, seat) < 0))
1288             wl_resource_post_no_memory(resource);
1289     } else if (seat->touch->grab_serial == serial) {
1290         surface = weston_surface_get_main_surface(seat->touch->focus);
1291         if ((surface == shsurf->surface) &&
1292             (surface_touch_move(shsurf, seat) < 0))
1293             wl_resource_post_no_memory(resource);
1294     }
1295 }
1296
1297 struct weston_resize_grab {
1298     struct shell_grab base;
1299     uint32_t edges;
1300     int32_t width, height;
1301 };
1302
1303 static void
1304 resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
1305 {
1306     struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1307     struct weston_pointer *pointer = grab->pointer;
1308     struct shell_surface *shsurf = resize->base.shsurf;
1309     int32_t width, height;
1310     wl_fixed_t from_x, from_y;
1311     wl_fixed_t to_x, to_y;
1312
1313     if (!shsurf)
1314         return;
1315
1316     weston_surface_from_global_fixed(shsurf->surface,
1317                          pointer->grab_x, pointer->grab_y,
1318                          &from_x, &from_y);
1319     weston_surface_from_global_fixed(shsurf->surface,
1320                          pointer->x, pointer->y, &to_x, &to_y);
1321
1322     width = resize->width;
1323     if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1324         width += wl_fixed_to_int(from_x - to_x);
1325     } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1326         width += wl_fixed_to_int(to_x - from_x);
1327     }
1328
1329     height = resize->height;
1330     if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1331         height += wl_fixed_to_int(from_y - to_y);
1332     } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1333         height += wl_fixed_to_int(to_y - from_y);
1334     }
1335
1336     uifw_trace("resize_grab_motion: send configure %08x %x w/h=%d/%d",
1337                (int)shsurf->surface, resize->edges, width, height);
1338     shsurf->client->send_configure(shsurf->surface,
1339                        resize->edges, width, height);
1340 }
1341
1342 static void
1343 send_configure(struct weston_surface *surface,
1344            uint32_t edges, int32_t width, int32_t height)
1345 {
1346     struct shell_surface *shsurf = get_shell_surface(surface);
1347
1348     uifw_trace("send_configure: send %08x %x w/h=%d/%d",
1349                (int)shsurf->surface, edges, width, height);
1350     wl_shell_surface_send_configure(shsurf->resource,
1351                     edges, width, height);
1352 }
1353
1354 static const struct weston_shell_client shell_client = {
1355     send_configure
1356 };
1357
1358 static void
1359 resize_grab_button(struct weston_pointer_grab *grab,
1360            uint32_t time, uint32_t button, uint32_t state_w)
1361 {
1362     struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1363     struct weston_pointer *pointer = grab->pointer;
1364     enum wl_pointer_button_state state = state_w;
1365
1366     if (pointer->button_count == 0 &&
1367         state == WL_POINTER_BUTTON_STATE_RELEASED) {
1368         shell_grab_end(&resize->base);
1369         free(grab);
1370     }
1371 }
1372
1373 static const struct weston_pointer_grab_interface resize_grab_interface = {
1374     noop_grab_focus,
1375     resize_grab_motion,
1376     resize_grab_button,
1377 };
1378
1379 /*
1380  * Returns the bounding box of a surface and all its sub-surfaces,
1381  * in the surface coordinates system. */
1382 static void
1383 surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1384                 int32_t *y, int32_t *w, int32_t *h) {
1385     pixman_region32_t region;
1386     pixman_box32_t *box;
1387     struct weston_subsurface *subsurface;
1388
1389     pixman_region32_init_rect(&region, 0, 0,
1390                               surface->geometry.width,
1391                               surface->geometry.height);
1392
1393     wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1394         pixman_region32_union_rect(&region, &region,
1395                                    subsurface->position.x,
1396                                    subsurface->position.y,
1397                                    subsurface->surface->geometry.width,
1398                                    subsurface->surface->geometry.height);
1399     }
1400
1401     box = pixman_region32_extents(&region);
1402     if (x)
1403         *x = box->x1;
1404     if (y)
1405         *y = box->y1;
1406     if (w)
1407         *w = box->x2 - box->x1;
1408     if (h)
1409         *h = box->y2 - box->y1;
1410
1411     pixman_region32_fini(&region);
1412 }
1413
1414 static int
1415 surface_resize(struct shell_surface *shsurf,
1416            struct weston_seat *seat, uint32_t edges)
1417 {
1418     struct weston_resize_grab *resize;
1419
1420     if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1421         shsurf->type == SHELL_SURFACE_MAXIMIZED)
1422         return 0;
1423
1424     if (edges == 0 || edges > 15 ||
1425         (edges & 3) == 3 || (edges & 12) == 12)
1426         return 0;
1427
1428     resize = malloc(sizeof *resize);
1429     if (!resize)
1430         return -1;
1431
1432     resize->edges = edges;
1433     surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1434                                     &resize->width, &resize->height);
1435
1436     shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1437              seat->pointer, edges);
1438
1439     return 0;
1440 }
1441
1442 static void
1443 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1444              struct wl_resource *seat_resource, uint32_t serial,
1445              uint32_t edges)
1446 {
1447     struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
1448     struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1449     struct weston_surface *surface;
1450
1451     if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1452         return;
1453
1454     surface = weston_surface_get_main_surface(seat->pointer->focus);
1455     if (seat->pointer->button_count == 0 ||
1456         seat->pointer->grab_serial != serial ||
1457         surface != shsurf->surface)
1458         return;
1459
1460     if (surface_resize(shsurf, seat, edges) < 0)
1461         wl_resource_post_no_memory(resource);
1462 }
1463
1464 static void
1465 end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1466
1467 static void
1468 busy_cursor_grab_focus(struct weston_pointer_grab *base)
1469 {
1470     struct shell_grab *grab = (struct shell_grab *) base;
1471     struct weston_pointer *pointer = base->pointer;
1472     struct weston_surface *surface;
1473     wl_fixed_t sx, sy;
1474
1475     surface = weston_compositor_pick_surface(pointer->seat->compositor,
1476                          pointer->x, pointer->y,
1477                          &sx, &sy);
1478
1479     if (!grab->shsurf || grab->shsurf->surface != surface)
1480         end_busy_cursor(grab->shsurf, pointer);
1481 }
1482
1483 static void
1484 busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
1485 {
1486 }
1487
1488 static void
1489 busy_cursor_grab_button(struct weston_pointer_grab *base,
1490             uint32_t time, uint32_t button, uint32_t state)
1491 {
1492     struct shell_grab *grab = (struct shell_grab *) base;
1493     struct shell_surface *shsurf = grab->shsurf;
1494     struct weston_seat *seat = grab->grab.pointer->seat;
1495
1496     if (shsurf && button == BTN_LEFT && state) {
1497         activate(shsurf->shell, shsurf->surface, seat);
1498         surface_move(shsurf, seat);
1499     } else if (shsurf && button == BTN_RIGHT && state) {
1500         activate(shsurf->shell, shsurf->surface, seat);
1501         surface_rotate(shsurf, seat);
1502     }
1503 }
1504
1505 static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
1506     busy_cursor_grab_focus,
1507     busy_cursor_grab_motion,
1508     busy_cursor_grab_button,
1509 };
1510
1511 static void
1512 set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
1513 {
1514     struct shell_grab *grab;
1515
1516     grab = malloc(sizeof *grab);
1517     if (!grab)
1518         return;
1519
1520     shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1521              DESKTOP_SHELL_CURSOR_BUSY);
1522 }
1523
1524 static void
1525 end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
1526 {
1527     struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1528
1529     if (grab->grab.interface == &busy_cursor_grab_interface &&
1530         grab->shsurf == shsurf) {
1531         shell_grab_end(grab);
1532         free(grab);
1533     }
1534 }
1535
1536 static void
1537 ping_timer_destroy(struct shell_surface *shsurf)
1538 {
1539     if (!shsurf || !shsurf->ping_timer)
1540         return;
1541
1542     if (shsurf->ping_timer->source)
1543         wl_event_source_remove(shsurf->ping_timer->source);
1544
1545     free(shsurf->ping_timer);
1546     shsurf->ping_timer = NULL;
1547 }
1548
1549 static int
1550 ping_timeout_handler(void *data)
1551 {
1552     struct shell_surface *shsurf = data;
1553     struct weston_seat *seat;
1554
1555     /* Client is not responding */
1556     shsurf->unresponsive = 1;
1557
1558     wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
1559         if (seat->pointer->focus == shsurf->surface)
1560             set_busy_cursor(shsurf, seat->pointer);
1561
1562     return 1;
1563 }
1564
1565 static void
1566 ping_handler(struct weston_surface *surface, uint32_t serial)
1567 {
1568     struct shell_surface *shsurf = get_shell_surface(surface);
1569     struct wl_event_loop *loop;
1570     int ping_timeout = 200;
1571
1572     if (!shsurf)
1573         return;
1574     if (!shsurf->resource)
1575         return;
1576
1577     if (shsurf->surface == shsurf->shell->grab_surface)
1578         return;
1579
1580     if (!shsurf->ping_timer) {
1581         shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
1582         if (!shsurf->ping_timer)
1583             return;
1584
1585         shsurf->ping_timer->serial = serial;
1586         loop = wl_display_get_event_loop(surface->compositor->wl_display);
1587         shsurf->ping_timer->source =
1588             wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1589         wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1590
1591         wl_shell_surface_send_ping(shsurf->resource, serial);
1592     }
1593 }
1594
1595 static void
1596 handle_pointer_focus(struct wl_listener *listener, void *data)
1597 {
1598     struct weston_pointer *pointer = data;
1599     struct weston_surface *surface = pointer->focus;
1600     struct weston_compositor *compositor;
1601     struct shell_surface *shsurf;
1602     uint32_t serial;
1603
1604     if (!surface)
1605         return;
1606
1607     compositor = surface->compositor;
1608     shsurf = get_shell_surface(surface);
1609
1610     if (shsurf && shsurf->unresponsive) {
1611         set_busy_cursor(shsurf, pointer);
1612     } else {
1613         serial = wl_display_next_serial(compositor->wl_display);
1614         ping_handler(surface, serial);
1615     }
1616 }
1617
1618 static void
1619 create_pointer_focus_listener(struct weston_seat *seat)
1620 {
1621     struct wl_listener *listener;
1622
1623     if (!seat->pointer)
1624         return;
1625
1626     listener = malloc(sizeof *listener);
1627     listener->notify = handle_pointer_focus;
1628     wl_signal_add(&seat->pointer->focus_signal, listener);
1629 }
1630
1631 static void
1632 shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1633                             uint32_t serial)
1634 {
1635     struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1636     struct weston_seat *seat;
1637     struct weston_compositor *ec = shsurf->surface->compositor;
1638
1639     if (shsurf->ping_timer == NULL)
1640         /* Just ignore unsolicited pong. */
1641         return;
1642
1643     if (shsurf->ping_timer->serial == serial) {
1644         shsurf->unresponsive = 0;
1645         wl_list_for_each(seat, &ec->seat_list, link) {
1646             if(seat->pointer)
1647                 end_busy_cursor(shsurf, seat->pointer);
1648         }
1649         ping_timer_destroy(shsurf);
1650     }
1651 }
1652
1653 static void
1654 shell_surface_set_title(struct wl_client *client,
1655             struct wl_resource *resource, const char *title)
1656 {
1657     struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1658
1659     free(shsurf->title);
1660     shsurf->title = strdup(title);
1661
1662     /* if ico_window_mgr hook, call hook routine    */
1663     if (shell_hook_title)  {
1664         (*shell_hook_title)(shsurf->surface, shsurf->title);
1665     }
1666 }
1667
1668 static void
1669 shell_surface_set_class(struct wl_client *client,
1670             struct wl_resource *resource, const char *class)
1671 {
1672     struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1673
1674     free(shsurf->class);
1675     shsurf->class = strdup(class);
1676 }
1677
1678 static struct weston_output *
1679 get_default_output(struct weston_compositor *compositor)
1680 {
1681     return container_of(compositor->output_list.next,
1682                 struct weston_output, link);
1683 }
1684
1685 static void
1686 restore_output_mode(struct weston_output *output)
1687 {
1688     if (output->current != output->origin ||
1689         (int32_t)output->scale != output->origin_scale)
1690         weston_output_switch_mode(output,
1691                       output->origin,
1692                       output->origin_scale);
1693 }
1694
1695 static void
1696 restore_all_output_modes(struct weston_compositor *compositor)
1697 {
1698     struct weston_output *output;
1699
1700     wl_list_for_each(output, &compositor->output_list, link)
1701         restore_output_mode(output);
1702 }
1703
1704 static void
1705 shell_unset_fullscreen(struct shell_surface *shsurf)
1706 {
1707     struct workspace *ws;
1708     /* undo all fullscreen things here */
1709     if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1710         shell_surface_is_top_fullscreen(shsurf)) {
1711         restore_output_mode(shsurf->fullscreen_output);
1712     }
1713     shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1714     shsurf->fullscreen.framerate = 0;
1715     wl_list_remove(&shsurf->fullscreen.transform.link);
1716     wl_list_init(&shsurf->fullscreen.transform.link);
1717     if (shsurf->fullscreen.black_surface)
1718         weston_surface_destroy(shsurf->fullscreen.black_surface);
1719     shsurf->fullscreen.black_surface = NULL;
1720     shsurf->fullscreen_output = NULL;
1721     weston_surface_set_position(shsurf->surface,
1722                     shsurf->saved_x, shsurf->saved_y);
1723     if (shsurf->saved_rotation_valid) {
1724         wl_list_insert(&shsurf->surface->geometry.transformation_list,
1725                            &shsurf->rotation.transform.link);
1726         shsurf->saved_rotation_valid = false;
1727     }
1728
1729     ws = get_current_workspace(shsurf->shell);
1730     wl_list_remove(&shsurf->surface->layer_link);
1731     wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1732 }
1733
1734 static void
1735 shell_unset_maximized(struct shell_surface *shsurf)
1736 {
1737     struct workspace *ws;
1738     /* undo all maximized things here */
1739     shsurf->output = get_default_output(shsurf->surface->compositor);
1740     weston_surface_set_position(shsurf->surface,
1741                     shsurf->saved_x,
1742                     shsurf->saved_y);
1743
1744     if (shsurf->saved_rotation_valid) {
1745         wl_list_insert(&shsurf->surface->geometry.transformation_list,
1746                            &shsurf->rotation.transform.link);
1747         shsurf->saved_rotation_valid = false;
1748     }
1749
1750     ws = get_current_workspace(shsurf->shell);
1751     wl_list_remove(&shsurf->surface->layer_link);
1752     wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1753 }
1754
1755 static int
1756 reset_shell_surface_type(struct shell_surface *surface)
1757 {
1758     switch (surface->type) {
1759     case SHELL_SURFACE_FULLSCREEN:
1760         shell_unset_fullscreen(surface);
1761         break;
1762     case SHELL_SURFACE_MAXIMIZED:
1763         shell_unset_maximized(surface);
1764         break;
1765     case SHELL_SURFACE_NONE:
1766     case SHELL_SURFACE_TOPLEVEL:
1767     case SHELL_SURFACE_TRANSIENT:
1768     case SHELL_SURFACE_POPUP:
1769     case SHELL_SURFACE_XWAYLAND:
1770         break;
1771     }
1772
1773     surface->type = SHELL_SURFACE_NONE;
1774     return 0;
1775 }
1776
1777 static void
1778 set_surface_type(struct shell_surface *shsurf)
1779 {
1780     struct weston_surface *surface = shsurf->surface;
1781     struct weston_surface *pes = shsurf->parent;
1782
1783     reset_shell_surface_type(shsurf);
1784
1785     shsurf->type = shsurf->next_type;
1786     shsurf->next_type = SHELL_SURFACE_NONE;
1787
1788     switch (shsurf->type) {
1789     case SHELL_SURFACE_TOPLEVEL:
1790         break;
1791     case SHELL_SURFACE_TRANSIENT:
1792         weston_surface_set_position(surface,
1793                 pes->geometry.x + shsurf->transient.x,
1794                 pes->geometry.y + shsurf->transient.y);
1795         break;
1796
1797     case SHELL_SURFACE_MAXIMIZED:
1798     case SHELL_SURFACE_FULLSCREEN:
1799         shsurf->saved_x = surface->geometry.x;
1800         shsurf->saved_y = surface->geometry.y;
1801         shsurf->saved_position_valid = true;
1802
1803         if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1804             wl_list_remove(&shsurf->rotation.transform.link);
1805             wl_list_init(&shsurf->rotation.transform.link);
1806             weston_surface_geometry_dirty(shsurf->surface);
1807             shsurf->saved_rotation_valid = true;
1808         }
1809         break;
1810
1811     case SHELL_SURFACE_XWAYLAND:
1812         weston_surface_set_position(surface, shsurf->transient.x,
1813                         shsurf->transient.y);
1814         break;
1815
1816     default:
1817         break;
1818     }
1819 }
1820
1821 static void
1822 set_toplevel(struct shell_surface *shsurf)
1823 {
1824        shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
1825 }
1826
1827 static void
1828 shell_surface_set_toplevel(struct wl_client *client,
1829                struct wl_resource *resource)
1830 {
1831     struct shell_surface *surface = wl_resource_get_user_data(resource);
1832
1833     set_toplevel(surface);
1834 }
1835
1836 static void
1837 set_transient(struct shell_surface *shsurf,
1838           struct weston_surface *parent, int x, int y, uint32_t flags)
1839 {
1840     /* assign to parents output */
1841     shsurf->parent = parent;
1842     shsurf->transient.x = x;
1843     shsurf->transient.y = y;
1844     shsurf->transient.flags = flags;
1845     shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1846 }
1847
1848 static void
1849 shell_surface_set_transient(struct wl_client *client,
1850                 struct wl_resource *resource,
1851                 struct wl_resource *parent_resource,
1852                 int x, int y, uint32_t flags)
1853 {
1854     struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1855     struct weston_surface *parent =
1856         wl_resource_get_user_data(parent_resource);
1857
1858     set_transient(shsurf, parent, x, y, flags);
1859 }
1860
1861 static struct desktop_shell *
1862 shell_surface_get_shell(struct shell_surface *shsurf)
1863 {
1864     return shsurf->shell;
1865 }
1866
1867 static int
1868 get_output_panel_height(struct desktop_shell *shell,
1869             struct weston_output *output)
1870 {
1871     struct weston_surface *surface;
1872     int panel_height = 0;
1873
1874     if (!output)
1875         return 0;
1876
1877     wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
1878         if (surface->output == output) {
1879             panel_height = surface->geometry.height;
1880             break;
1881         }
1882     }
1883
1884     return panel_height;
1885 }
1886
1887 static void
1888 shell_surface_set_maximized(struct wl_client *client,
1889                 struct wl_resource *resource,
1890                 struct wl_resource *output_resource )
1891 {
1892     struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1893     struct weston_surface *es = shsurf->surface;
1894     struct desktop_shell *shell = NULL;
1895     uint32_t edges = 0, panel_height = 0;
1896
1897     /* get the default output, if the client set it as NULL
1898        check whether the ouput is available */
1899     if (output_resource)
1900         shsurf->output = wl_resource_get_user_data(output_resource);
1901     else if (es->output)
1902         shsurf->output = es->output;
1903     else
1904         shsurf->output = get_default_output(es->compositor);
1905
1906     shell = shell_surface_get_shell(shsurf);
1907     panel_height = get_output_panel_height(shell, shsurf->output);
1908     edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
1909
1910     uifw_trace("shell_surface_set_maximized: send %08x %x w/h=%d/%d",
1911                (int)shsurf->surface, edges, shsurf->output->width,
1912                shsurf->output->height - panel_height);
1913     shsurf->client->send_configure(shsurf->surface, edges,
1914                        shsurf->output->width,
1915                        shsurf->output->height - panel_height);
1916
1917     shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
1918 }
1919
1920 static void
1921 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height);
1922
1923 static struct weston_surface *
1924 create_black_surface(struct weston_compositor *ec,
1925              struct weston_surface *fs_surface,
1926              float x, float y, int w, int h)
1927 {
1928     struct weston_surface *surface = NULL;
1929
1930     surface = weston_surface_create(ec);
1931     if (surface == NULL) {
1932         weston_log("no memory\n");
1933         return NULL;
1934     }
1935
1936     surface->configure = black_surface_configure;
1937     surface->configure_private = fs_surface;
1938     uifw_trace("create_black_surface: configure %08x x/y=%d/%d w/h=%d/%d",
1939                (int)surface, (int)x, (int)y, w, h);
1940     weston_surface_configure(surface, x, y, w, h);
1941     weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1942     pixman_region32_fini(&surface->opaque);
1943     pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
1944     pixman_region32_fini(&surface->input);
1945     pixman_region32_init_rect(&surface->input, 0, 0, w, h);
1946
1947     return surface;
1948 }
1949
1950 /* Create black surface and append it to the associated fullscreen surface.
1951  * Handle size dismatch and positioning according to the method. */
1952 static void
1953 shell_configure_fullscreen(struct shell_surface *shsurf)
1954 {
1955     struct weston_output *output = shsurf->fullscreen_output;
1956     struct weston_surface *surface = shsurf->surface;
1957     struct weston_matrix *matrix;
1958     float scale, output_aspect, surface_aspect, x, y;
1959     int32_t surf_x, surf_y, surf_width, surf_height;
1960
1961     if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
1962         restore_output_mode(output);
1963
1964     if (!shsurf->fullscreen.black_surface)
1965         shsurf->fullscreen.black_surface =
1966             create_black_surface(surface->compositor,
1967                          surface,
1968                          output->x, output->y,
1969                          output->width,
1970                          output->height);
1971
1972     wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1973     wl_list_insert(&surface->layer_link,
1974                &shsurf->fullscreen.black_surface->layer_link);
1975     shsurf->fullscreen.black_surface->output = output;
1976
1977     surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
1978                                     &surf_width, &surf_height);
1979
1980     switch (shsurf->fullscreen.type) {
1981     case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1982         if (surface->buffer_ref.buffer)
1983             center_on_output(surface, shsurf->fullscreen_output);
1984         break;
1985     case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1986         /* 1:1 mapping between surface and output dimensions */
1987         if (output->width == surf_width &&
1988             output->height == surf_height) {
1989             weston_surface_set_position(surface, output->x - surf_x,
1990                                                  output->y - surf_y);
1991             break;
1992         }
1993
1994         matrix = &shsurf->fullscreen.transform.matrix;
1995         weston_matrix_init(matrix);
1996
1997         output_aspect = (float) output->width /
1998             (float) output->height;
1999         surface_aspect = (float) surface->geometry.width /
2000             (float) surface->geometry.height;
2001         if (output_aspect < surface_aspect)
2002             scale = (float) output->width /
2003                 (float) surf_width;
2004         else
2005             scale = (float) output->height /
2006                 (float) surf_height;
2007
2008         weston_matrix_scale(matrix, scale, scale, 1);
2009         wl_list_remove(&shsurf->fullscreen.transform.link);
2010         wl_list_insert(&surface->geometry.transformation_list,
2011                    &shsurf->fullscreen.transform.link);
2012         x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2013         y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
2014         weston_surface_set_position(surface, x, y);
2015
2016         break;
2017     case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
2018         if (shell_surface_is_top_fullscreen(shsurf)) {
2019             struct weston_mode mode = {0,
2020                 surf_width * surface->buffer_scale,
2021                 surf_height * surface->buffer_scale,
2022                 shsurf->fullscreen.framerate};
2023
2024             if (weston_output_switch_mode(output, &mode, surface->buffer_scale) == 0) {
2025                 weston_surface_set_position(surface,
2026                                 output->x - surf_x,
2027                                 output->y - surf_y);
2028                 uifw_trace("shell_configure_fullscreen: configure %08x x/y=%d/%d w/h=%d/%d",
2029                            (int)shsurf->fullscreen.black_surface, (int)(output->x - surf_x),
2030                            (int)(output->y - surf_y), output->width, output->height);
2031                 weston_surface_configure(shsurf->fullscreen.black_surface,
2032                                      output->x - surf_x,
2033                                      output->y - surf_y,
2034                              output->width,
2035                              output->height);
2036                 break;
2037             } else {
2038                 restore_output_mode(output);
2039                 center_on_output(surface, output);
2040             }
2041         }
2042         break;
2043     case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
2044         center_on_output(surface, output);
2045         break;
2046     default:
2047         break;
2048     }
2049 }
2050
2051 /* make the fullscreen and black surface at the top */
2052 static void
2053 shell_stack_fullscreen(struct shell_surface *shsurf)
2054 {
2055     struct weston_output *output = shsurf->fullscreen_output;
2056     struct weston_surface *surface = shsurf->surface;
2057     struct desktop_shell *shell = shell_surface_get_shell(shsurf);
2058
2059     wl_list_remove(&surface->layer_link);
2060     wl_list_insert(&shell->fullscreen_layer.surface_list,
2061                &surface->layer_link);
2062     weston_surface_damage(surface);
2063
2064     if (!shsurf->fullscreen.black_surface)
2065         shsurf->fullscreen.black_surface =
2066             create_black_surface(surface->compositor,
2067                          surface,
2068                          output->x, output->y,
2069                          output->width,
2070                          output->height);
2071
2072     wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
2073     wl_list_insert(&surface->layer_link,
2074                &shsurf->fullscreen.black_surface->layer_link);
2075     weston_surface_damage(shsurf->fullscreen.black_surface);
2076 }
2077
2078 static void
2079 shell_map_fullscreen(struct shell_surface *shsurf)
2080 {
2081     shell_stack_fullscreen(shsurf);
2082     shell_configure_fullscreen(shsurf);
2083 }
2084
2085 static void
2086 set_fullscreen(struct shell_surface *shsurf,
2087            uint32_t method,
2088            uint32_t framerate,
2089            struct weston_output *output)
2090 {
2091     struct weston_surface *es = shsurf->surface;
2092
2093     if (output)
2094         shsurf->output = output;
2095     else if (es->output)
2096         shsurf->output = es->output;
2097     else
2098         shsurf->output = get_default_output(es->compositor);
2099
2100     shsurf->fullscreen_output = shsurf->output;
2101     shsurf->fullscreen.type = method;
2102     shsurf->fullscreen.framerate = framerate;
2103     shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
2104
2105     uifw_trace("set_fullscreen: send %08x 0 w/h=%d/%d",
2106                (int)shsurf->surface, shsurf->output->width, shsurf->output->height);
2107     shsurf->client->send_configure(shsurf->surface, 0,
2108                        shsurf->output->width,
2109                        shsurf->output->height);
2110 }
2111
2112 static void
2113 shell_surface_set_fullscreen(struct wl_client *client,
2114                  struct wl_resource *resource,
2115                  uint32_t method,
2116                  uint32_t framerate,
2117                  struct wl_resource *output_resource)
2118 {
2119     struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2120     struct weston_output *output;
2121
2122     if (output_resource)
2123         output = wl_resource_get_user_data(output_resource);
2124     else
2125         output = NULL;
2126
2127     set_fullscreen(shsurf, method, framerate, output);
2128 }
2129
2130 static void
2131 set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2132 {
2133     /* XXX: using the same fields for transient type */
2134     shsurf->transient.x = x;
2135     shsurf->transient.y = y;
2136     shsurf->transient.flags = flags;
2137     shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2138 }
2139
2140 static const struct weston_pointer_grab_interface popup_grab_interface;
2141
2142 static void
2143 destroy_shell_seat(struct wl_listener *listener, void *data)
2144 {
2145     struct shell_seat *shseat =
2146         container_of(listener,
2147                  struct shell_seat, seat_destroy_listener);
2148     struct shell_surface *shsurf, *prev = NULL;
2149
2150     if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
2151         weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
2152         shseat->popup_grab.client = NULL;
2153
2154         wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2155             shsurf->popup.shseat = NULL;
2156             if (prev) {
2157                 wl_list_init(&prev->popup.grab_link);
2158             }
2159             prev = shsurf;
2160         }
2161         wl_list_init(&prev->popup.grab_link);
2162     }
2163
2164     wl_list_remove(&shseat->seat_destroy_listener.link);
2165     free(shseat);
2166 }
2167
2168 static struct shell_seat *
2169 create_shell_seat(struct weston_seat *seat)
2170 {
2171     struct shell_seat *shseat;
2172
2173     shseat = calloc(1, sizeof *shseat);
2174     if (!shseat) {
2175         weston_log("no memory to allocate shell seat\n");
2176         return NULL;
2177     }
2178
2179     shseat->seat = seat;
2180     wl_list_init(&shseat->popup_grab.surfaces_list);
2181
2182     shseat->seat_destroy_listener.notify = destroy_shell_seat;
2183     wl_signal_add(&seat->destroy_signal,
2184                   &shseat->seat_destroy_listener);
2185
2186     return shseat;
2187 }
2188
2189 static struct shell_seat *
2190 get_shell_seat(struct weston_seat *seat)
2191 {
2192     struct wl_listener *listener;
2193
2194     listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2195     if (listener == NULL)
2196         return create_shell_seat(seat);
2197
2198     return container_of(listener,
2199                 struct shell_seat, seat_destroy_listener);
2200 }
2201
2202 static void
2203 popup_grab_focus(struct weston_pointer_grab *grab)
2204 {
2205     struct weston_pointer *pointer = grab->pointer;
2206     struct weston_surface *surface;
2207     struct shell_seat *shseat =
2208         container_of(grab, struct shell_seat, popup_grab.grab);
2209     struct wl_client *client = shseat->popup_grab.client;
2210     wl_fixed_t sx, sy;
2211
2212     surface = weston_compositor_pick_surface(pointer->seat->compositor,
2213                          pointer->x, pointer->y,
2214                          &sx, &sy);
2215
2216     if (surface && wl_resource_get_client(surface->resource) == client) {
2217         weston_pointer_set_focus(pointer, surface, sx, sy);
2218     } else {
2219         weston_pointer_set_focus(pointer, NULL,
2220                      wl_fixed_from_int(0),
2221                      wl_fixed_from_int(0));
2222     }
2223 }
2224
2225 static void
2226 popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
2227 {
2228     struct weston_pointer *pointer = grab->pointer;
2229     wl_fixed_t sx, sy;
2230
2231     if (pointer->focus_resource) {
2232         weston_surface_from_global_fixed(pointer->focus,
2233                          pointer->x, pointer->y,
2234                          &sx, &sy);
2235         wl_pointer_send_motion(pointer->focus_resource, time, sx, sy);
2236     }
2237 }
2238
2239 static void
2240 popup_grab_button(struct weston_pointer_grab *grab,
2241           uint32_t time, uint32_t button, uint32_t state_w)
2242 {
2243     struct wl_resource *resource;
2244     struct shell_seat *shseat =
2245         container_of(grab, struct shell_seat, popup_grab.grab);
2246     struct wl_display *display;
2247     enum wl_pointer_button_state state = state_w;
2248     uint32_t serial;
2249
2250     resource = grab->pointer->focus_resource;
2251     if (resource) {
2252         display = wl_client_get_display(wl_resource_get_client(resource));
2253         serial = wl_display_get_serial(display);
2254         wl_pointer_send_button(resource, serial, time, button, state);
2255     } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
2256            (shseat->popup_grab.initial_up ||
2257             time - shseat->seat->pointer->grab_time > 500)) {
2258         popup_grab_end(grab->pointer);
2259     }
2260
2261     if (state == WL_POINTER_BUTTON_STATE_RELEASED)
2262         shseat->popup_grab.initial_up = 1;
2263 }
2264
2265 static const struct weston_pointer_grab_interface popup_grab_interface = {
2266     popup_grab_focus,
2267     popup_grab_motion,
2268     popup_grab_button,
2269 };
2270
2271 static void
2272 popup_grab_end(struct weston_pointer *pointer)
2273 {
2274     struct weston_pointer_grab *grab = pointer->grab;
2275     struct shell_seat *shseat =
2276         container_of(grab, struct shell_seat, popup_grab.grab);
2277     struct shell_surface *shsurf;
2278     struct shell_surface *prev = NULL;
2279
2280     if (pointer->grab->interface == &popup_grab_interface) {
2281         weston_pointer_end_grab(grab->pointer);
2282         shseat->popup_grab.client = NULL;
2283         shseat->popup_grab.grab.interface = NULL;
2284         assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
2285         /* Send the popup_done event to all the popups open */
2286         wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2287             wl_shell_surface_send_popup_done(shsurf->resource);
2288             shsurf->popup.shseat = NULL;
2289             if (prev) {
2290                 wl_list_init(&prev->popup.grab_link);
2291             }
2292             prev = shsurf;
2293         }
2294         wl_list_init(&prev->popup.grab_link);
2295         wl_list_init(&shseat->popup_grab.surfaces_list);
2296     }
2297 }
2298
2299 static void
2300 add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2301 {
2302     struct weston_seat *seat = shseat->seat;
2303
2304     if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
2305         shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
2306         shseat->popup_grab.grab.interface = &popup_grab_interface;
2307         /* We must make sure here that this popup was opened after
2308          * a mouse press, and not just by moving around with other
2309          * popups already open. */
2310         if (shseat->seat->pointer->button_count > 0)
2311             shseat->popup_grab.initial_up = 0;
2312
2313         wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
2314         weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
2315     } else {
2316         wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
2317     }
2318 }
2319
2320 static void
2321 remove_popup_grab(struct shell_surface *shsurf)
2322 {
2323     struct shell_seat *shseat = shsurf->popup.shseat;
2324
2325     wl_list_remove(&shsurf->popup.grab_link);
2326     wl_list_init(&shsurf->popup.grab_link);
2327     if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
2328         weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
2329         shseat->popup_grab.grab.interface = NULL;
2330     }
2331 }
2332
2333 static void
2334 shell_map_popup(struct shell_surface *shsurf)
2335 {
2336     struct shell_seat *shseat = shsurf->popup.shseat;
2337     struct weston_surface *es = shsurf->surface;
2338     struct weston_surface *parent = shsurf->parent;
2339
2340     es->output = parent->output;
2341
2342     weston_surface_set_transform_parent(es, parent);
2343     weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2344     weston_surface_update_transform(es);
2345
2346     if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
2347         add_popup_grab(shsurf, shseat);
2348     } else {
2349         wl_shell_surface_send_popup_done(shsurf->resource);
2350         shseat->popup_grab.client = NULL;
2351     }
2352 }
2353
2354 static void
2355 shell_surface_set_popup(struct wl_client *client,
2356             struct wl_resource *resource,
2357             struct wl_resource *seat_resource,
2358             uint32_t serial,
2359             struct wl_resource *parent_resource,
2360             int32_t x, int32_t y, uint32_t flags)
2361 {
2362     struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2363
2364     shsurf->type = SHELL_SURFACE_POPUP;
2365     shsurf->parent = wl_resource_get_user_data(parent_resource);
2366     shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
2367     shsurf->popup.serial = serial;
2368     shsurf->popup.x = x;
2369     shsurf->popup.y = y;
2370 }
2371
2372 static const struct wl_shell_surface_interface shell_surface_implementation = {
2373     shell_surface_pong,
2374     shell_surface_move,
2375     shell_surface_resize,
2376     shell_surface_set_toplevel,
2377     shell_surface_set_transient,
2378     shell_surface_set_fullscreen,
2379     shell_surface_set_popup,
2380     shell_surface_set_maximized,
2381     shell_surface_set_title,
2382     shell_surface_set_class
2383 };
2384
2385 static void
2386 destroy_shell_surface(struct shell_surface *shsurf)
2387 {
2388     wl_signal_emit(&shsurf->destroy_signal, shsurf);
2389
2390     /* if ico_window_mgr hook, call hook routine    */
2391     if (shell_hook_destroy)  {
2392         (*shell_hook_destroy)(shsurf->surface);
2393     }
2394
2395     if (!wl_list_empty(&shsurf->popup.grab_link)) {
2396         remove_popup_grab(shsurf);
2397     }
2398
2399     if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2400         shell_surface_is_top_fullscreen(shsurf))
2401         restore_output_mode (shsurf->fullscreen_output);
2402
2403     if (shsurf->fullscreen.black_surface)
2404         weston_surface_destroy(shsurf->fullscreen.black_surface);
2405
2406     /* As destroy_resource() use wl_list_for_each_safe(),
2407      * we can always remove the listener.
2408      */
2409     wl_list_remove(&shsurf->surface_destroy_listener.link);
2410     shsurf->surface->configure = NULL;
2411     ping_timer_destroy(shsurf);
2412     free(shsurf->title);
2413
2414     wl_list_remove(&shsurf->link);
2415     free(shsurf);
2416 }
2417
2418 static void
2419 shell_destroy_shell_surface(struct wl_resource *resource)
2420 {
2421     struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2422
2423     destroy_shell_surface(shsurf);
2424 }
2425
2426 static void
2427 shell_handle_surface_destroy(struct wl_listener *listener, void *data)
2428 {
2429     struct shell_surface *shsurf = container_of(listener,
2430                             struct shell_surface,
2431                             surface_destroy_listener);
2432
2433     if (shsurf->resource)
2434         wl_resource_destroy(shsurf->resource);
2435     else
2436         destroy_shell_surface(shsurf);
2437 }
2438
2439 static void
2440 shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
2441
2442 static struct shell_surface *
2443 get_shell_surface(struct weston_surface *surface)
2444 {
2445     if (surface->configure == shell_surface_configure)
2446         return surface->configure_private;
2447     else
2448         return NULL;
2449 }
2450
2451 static  struct shell_surface *
2452 create_shell_surface(void *shell, struct weston_surface *surface,
2453              const struct weston_shell_client *client)
2454 {
2455     struct shell_surface *shsurf;
2456
2457     if (surface->configure) {
2458         weston_log("surface->configure already set\n");
2459         return NULL;
2460     }
2461
2462     shsurf = calloc(1, sizeof *shsurf);
2463     if (!shsurf) {
2464         weston_log("no memory to allocate shell surface\n");
2465         return NULL;
2466     }
2467
2468     surface->configure = shell_surface_configure;
2469     surface->configure_private = shsurf;
2470
2471     shsurf->shell = (struct desktop_shell *) shell;
2472     shsurf->unresponsive = 0;
2473     shsurf->saved_position_valid = false;
2474     shsurf->saved_rotation_valid = false;
2475     shsurf->surface = surface;
2476     shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2477     shsurf->fullscreen.framerate = 0;
2478     shsurf->fullscreen.black_surface = NULL;
2479     shsurf->ping_timer = NULL;
2480     wl_list_init(&shsurf->fullscreen.transform.link);
2481
2482     wl_signal_init(&shsurf->destroy_signal);
2483     shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
2484     wl_signal_add(&surface->destroy_signal,
2485               &shsurf->surface_destroy_listener);
2486
2487     /* init link so its safe to always remove it in destroy_shell_surface */
2488     wl_list_init(&shsurf->link);
2489     wl_list_init(&shsurf->popup.grab_link);
2490
2491     /* empty when not in use */
2492     wl_list_init(&shsurf->rotation.transform.link);
2493     weston_matrix_init(&shsurf->rotation.rotation);
2494
2495     wl_list_init(&shsurf->workspace_transform.link);
2496
2497     shsurf->type = SHELL_SURFACE_NONE;
2498     shsurf->next_type = SHELL_SURFACE_NONE;
2499
2500     shsurf->client = client;
2501
2502     return shsurf;
2503 }
2504
2505 static void
2506 shell_get_shell_surface(struct wl_client *client,
2507             struct wl_resource *resource,
2508             uint32_t id,
2509             struct wl_resource *surface_resource)
2510 {
2511     struct weston_surface *surface =
2512         wl_resource_get_user_data(surface_resource);
2513     struct desktop_shell *shell = wl_resource_get_user_data(resource);
2514     struct shell_surface *shsurf;
2515
2516     if (get_shell_surface(surface)) {
2517         wl_resource_post_error(surface_resource,
2518                        WL_DISPLAY_ERROR_INVALID_OBJECT,
2519                        "desktop_shell::get_shell_surface already requested");
2520         return;
2521     }
2522
2523     shsurf = create_shell_surface(shell, surface, &shell_client);
2524     if (!shsurf) {
2525         wl_resource_post_error(surface_resource,
2526                        WL_DISPLAY_ERROR_INVALID_OBJECT,
2527                        "surface->configure already set");
2528         return;
2529     }
2530
2531     shsurf->resource =
2532         wl_resource_create(client,
2533                    &wl_shell_surface_interface, 1, id);
2534     wl_resource_set_implementation(shsurf->resource,
2535                        &shell_surface_implementation,
2536                        shsurf, shell_destroy_shell_surface);
2537
2538     /* if ico_window_mgr hook, call hook routine    */
2539     if (shell_hook_create)  {
2540         (*shell_hook_create)(client, resource, surface, shsurf);
2541     }
2542 }
2543
2544 static const struct wl_shell_interface shell_implementation = {
2545     shell_get_shell_surface
2546 };
2547
2548 static void
2549 shell_fade(struct desktop_shell *shell, enum fade_type type);
2550
2551 static int
2552 screensaver_timeout(void *data)
2553 {
2554     struct desktop_shell *shell = data;
2555
2556     shell_fade(shell, FADE_OUT);
2557
2558     return 1;
2559 }
2560
2561 static void
2562 handle_screensaver_sigchld(struct weston_process *proc, int status)
2563 {
2564     struct desktop_shell *shell =
2565         container_of(proc, struct desktop_shell, screensaver.process);
2566
2567     proc->pid = 0;
2568
2569     if (shell->locked)
2570         weston_compositor_sleep(shell->compositor);
2571 }
2572
2573 static void
2574 launch_screensaver(struct desktop_shell *shell)
2575 {
2576     if (shell->screensaver.binding)
2577         return;
2578
2579     if (!shell->screensaver.path) {
2580         weston_compositor_sleep(shell->compositor);
2581         return;
2582     }
2583
2584     if (shell->screensaver.process.pid != 0) {
2585         weston_log("old screensaver still running\n");
2586         return;
2587     }
2588
2589     weston_client_launch(shell->compositor,
2590                &shell->screensaver.process,
2591                shell->screensaver.path,
2592                handle_screensaver_sigchld);
2593 }
2594
2595 static void
2596 terminate_screensaver(struct desktop_shell *shell)
2597 {
2598     if (shell->screensaver.process.pid == 0)
2599         return;
2600
2601     kill(shell->screensaver.process.pid, SIGTERM);
2602 }
2603
2604 static void
2605 configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
2606 {
2607     struct weston_surface *s, *next;
2608
2609     if (width == 0)
2610         return;
2611
2612     wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2613         if (s->output == es->output && s != es) {
2614             weston_surface_unmap(s);
2615             s->configure = NULL;
2616         }
2617     }
2618
2619     uifw_trace("configure_static_surface: configure %08x x/y=%d/%d w/h=%d/%d",
2620                (int)es, (int)es->output->x, (int)es->output->y, width, height);
2621     weston_surface_configure(es, es->output->x, es->output->y, width, height);
2622
2623     if (wl_list_empty(&es->layer_link)) {
2624         wl_list_insert(&layer->surface_list, &es->layer_link);
2625         weston_compositor_schedule_repaint(es->compositor);
2626     }
2627
2628     /* if ico_window_mgr hook, call hook routine    */
2629     if (shell_hook_configure)  {
2630         (*shell_hook_configure)(es);
2631     }
2632 }
2633
2634 static void
2635 background_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
2636 {
2637     struct desktop_shell *shell = es->configure_private;
2638
2639     configure_static_surface(es, &shell->background_layer, width, height);
2640 }
2641
2642 static void
2643 desktop_shell_set_background(struct wl_client *client,
2644                  struct wl_resource *resource,
2645                  struct wl_resource *output_resource,
2646                  struct wl_resource *surface_resource)
2647 {
2648     struct desktop_shell *shell = wl_resource_get_user_data(resource);
2649     struct weston_surface *surface =
2650         wl_resource_get_user_data(surface_resource);
2651
2652     if (surface->configure) {
2653         wl_resource_post_error(surface_resource,
2654                        WL_DISPLAY_ERROR_INVALID_OBJECT,
2655                        "surface role already assigned");
2656         return;
2657     }
2658
2659     surface->configure = background_configure;
2660     surface->configure_private = shell;
2661     surface->output = wl_resource_get_user_data(output_resource);
2662     uifw_trace("desktop_shell_set_background: send %08x 0 w/h=%d/%d",
2663                (int)surface, surface->output->width, surface->output->height);
2664     desktop_shell_send_configure(resource, 0,
2665                      surface_resource,
2666                      surface->output->width,
2667                      surface->output->height);
2668 }
2669
2670 static void
2671 panel_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
2672 {
2673     struct desktop_shell *shell = es->configure_private;
2674
2675     configure_static_surface(es, &shell->panel_layer, width, height);
2676 }
2677
2678 static void
2679 desktop_shell_set_panel(struct wl_client *client,
2680             struct wl_resource *resource,
2681             struct wl_resource *output_resource,
2682             struct wl_resource *surface_resource)
2683 {
2684     struct desktop_shell *shell = wl_resource_get_user_data(resource);
2685     struct weston_surface *surface =
2686         wl_resource_get_user_data(surface_resource);
2687
2688     if (surface->configure) {
2689         wl_resource_post_error(surface_resource,
2690                        WL_DISPLAY_ERROR_INVALID_OBJECT,
2691                        "surface role already assigned");
2692         return;
2693     }
2694
2695     surface->configure = panel_configure;
2696     surface->configure_private = shell;
2697     surface->output = wl_resource_get_user_data(output_resource);
2698     uifw_trace("desktop_shell_set_panel: send %08x 0 w/h=%d/%d",
2699                (int)surface, surface->output->width, surface->output->height);
2700     desktop_shell_send_configure(resource, 0,
2701                      surface_resource,
2702                      surface->output->width,
2703                      surface->output->height);
2704 }
2705
2706 static void
2707 lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
2708 {
2709     struct desktop_shell *shell = surface->configure_private;
2710
2711     if (width == 0)
2712         return;
2713
2714     uifw_trace("lock_surface_configure: change %08x w/h=%d/%d", (int)surface, width, height);
2715     surface->geometry.width = width;
2716     surface->geometry.height = height;
2717     center_on_output(surface, get_default_output(shell->compositor));
2718
2719     if (!weston_surface_is_mapped(surface)) {
2720         wl_list_insert(&shell->lock_layer.surface_list,
2721                    &surface->layer_link);
2722         weston_surface_update_transform(surface);
2723         shell_fade(shell, FADE_IN);
2724     }
2725 }
2726
2727 static void
2728 handle_lock_surface_destroy(struct wl_listener *listener, void *data)
2729 {
2730     struct desktop_shell *shell =
2731         container_of(listener, struct desktop_shell, lock_surface_listener);
2732
2733     weston_log("lock surface gone\n");
2734     shell->lock_surface = NULL;
2735 }
2736
2737 static void
2738 desktop_shell_set_lock_surface(struct wl_client *client,
2739                    struct wl_resource *resource,
2740                    struct wl_resource *surface_resource)
2741 {
2742     struct desktop_shell *shell = wl_resource_get_user_data(resource);
2743     struct weston_surface *surface =
2744         wl_resource_get_user_data(surface_resource);
2745
2746     shell->prepare_event_sent = false;
2747
2748     if (!shell->locked)
2749         return;
2750
2751     shell->lock_surface = surface;
2752
2753     shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2754     wl_signal_add(&surface->destroy_signal,
2755               &shell->lock_surface_listener);
2756
2757     surface->configure = lock_surface_configure;
2758     surface->configure_private = shell;
2759 }
2760
2761 static void
2762 resume_desktop(struct desktop_shell *shell)
2763 {
2764     struct workspace *ws = get_current_workspace(shell);
2765
2766     terminate_screensaver(shell);
2767
2768     wl_list_remove(&shell->lock_layer.link);
2769     wl_list_insert(&shell->compositor->cursor_layer.link,
2770                &shell->fullscreen_layer.link);
2771     wl_list_insert(&shell->fullscreen_layer.link,
2772                &shell->panel_layer.link);
2773     if (shell->showing_input_panels) {
2774         wl_list_insert(&shell->panel_layer.link,
2775                    &shell->input_panel_layer.link);
2776         wl_list_insert(&shell->input_panel_layer.link,
2777                    &ws->layer.link);
2778     } else {
2779         wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2780     }
2781
2782     restore_focus_state(shell, get_current_workspace(shell));
2783
2784     shell->locked = false;
2785     shell_fade(shell, FADE_IN);
2786     weston_compositor_damage_all(shell->compositor);
2787 }
2788
2789 static void
2790 desktop_shell_unlock(struct wl_client *client,
2791              struct wl_resource *resource)
2792 {
2793     struct desktop_shell *shell = wl_resource_get_user_data(resource);
2794
2795     shell->prepare_event_sent = false;
2796
2797     if (shell->locked)
2798         resume_desktop(shell);
2799 }
2800
2801 static void
2802 desktop_shell_set_grab_surface(struct wl_client *client,
2803                    struct wl_resource *resource,
2804                    struct wl_resource *surface_resource)
2805 {
2806     struct desktop_shell *shell = wl_resource_get_user_data(resource);
2807
2808     shell->grab_surface = wl_resource_get_user_data(surface_resource);
2809 }
2810
2811 static void
2812 desktop_shell_desktop_ready(struct wl_client *client,
2813                 struct wl_resource *resource)
2814 {
2815     struct desktop_shell *shell = wl_resource_get_user_data(resource);
2816
2817     shell_fade_startup(shell);
2818 }
2819
2820 static const struct desktop_shell_interface desktop_shell_implementation = {
2821     desktop_shell_set_background,
2822     desktop_shell_set_panel,
2823     desktop_shell_set_lock_surface,
2824     desktop_shell_unlock,
2825     desktop_shell_set_grab_surface,
2826     desktop_shell_desktop_ready
2827 };
2828
2829 static enum shell_surface_type
2830 get_shell_surface_type(struct weston_surface *surface)
2831 {
2832     struct shell_surface *shsurf;
2833
2834     shsurf = get_shell_surface(surface);
2835     if (!shsurf)
2836         return SHELL_SURFACE_NONE;
2837     return shsurf->type;
2838 }
2839
2840 static void
2841 move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
2842 {
2843     struct weston_surface *focus =
2844         (struct weston_surface *) seat->pointer->focus;
2845     struct weston_surface *surface;
2846     struct shell_surface *shsurf;
2847
2848     surface = weston_surface_get_main_surface(focus);
2849     if (surface == NULL)
2850         return;
2851
2852     shsurf = get_shell_surface(surface);
2853     if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2854         shsurf->type == SHELL_SURFACE_MAXIMIZED)
2855         return;
2856
2857     surface_move(shsurf, (struct weston_seat *) seat);
2858 }
2859
2860 static void
2861 resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
2862 {
2863     struct weston_surface *focus =
2864         (struct weston_surface *) seat->pointer->focus;
2865     struct weston_surface *surface;
2866     uint32_t edges = 0;
2867     int32_t x, y;
2868     struct shell_surface *shsurf;
2869
2870     surface = weston_surface_get_main_surface(focus);
2871     if (surface == NULL)
2872         return;
2873
2874     shsurf = get_shell_surface(surface);
2875     if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2876         shsurf->type == SHELL_SURFACE_MAXIMIZED)
2877         return;
2878
2879     weston_surface_from_global(surface,
2880                    wl_fixed_to_int(seat->pointer->grab_x),
2881                    wl_fixed_to_int(seat->pointer->grab_y),
2882                    &x, &y);
2883
2884     if (x < surface->geometry.width / 3)
2885         edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
2886     else if (x < 2 * surface->geometry.width / 3)
2887         edges |= 0;
2888     else
2889         edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
2890
2891     if (y < surface->geometry.height / 3)
2892         edges |= WL_SHELL_SURFACE_RESIZE_TOP;
2893     else if (y < 2 * surface->geometry.height / 3)
2894         edges |= 0;
2895     else
2896         edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
2897
2898     surface_resize(shsurf, (struct weston_seat *) seat, edges);
2899 }
2900
2901 static void
2902 surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
2903             wl_fixed_t value, void *data)
2904 {
2905     float step = 0.005;
2906     struct shell_surface *shsurf;
2907     struct weston_surface *focus =
2908         (struct weston_surface *) seat->pointer->focus;
2909     struct weston_surface *surface;
2910
2911     /* XXX: broken for windows containing sub-surfaces */
2912     surface = weston_surface_get_main_surface(focus);
2913     if (surface == NULL)
2914         return;
2915
2916     shsurf = get_shell_surface(surface);
2917     if (!shsurf)
2918         return;
2919
2920     surface->alpha -= wl_fixed_to_double(value) * step;
2921
2922     if (surface->alpha > 1.0)
2923         surface->alpha = 1.0;
2924     if (surface->alpha < step)
2925         surface->alpha = step;
2926
2927     weston_surface_geometry_dirty(surface);
2928     weston_surface_damage(surface);
2929 }
2930
2931 static void
2932 do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
2933     wl_fixed_t value)
2934 {
2935     struct weston_seat *ws = (struct weston_seat *) seat;
2936     struct weston_compositor *compositor = ws->compositor;
2937     struct weston_output *output;
2938     float increment;
2939
2940     wl_list_for_each(output, &compositor->output_list, link) {
2941         if (pixman_region32_contains_point(&output->region,
2942                            wl_fixed_to_double(seat->pointer->x),
2943                            wl_fixed_to_double(seat->pointer->y),
2944                            NULL)) {
2945             if (key == KEY_PAGEUP)
2946                 increment = output->zoom.increment;
2947             else if (key == KEY_PAGEDOWN)
2948                 increment = -output->zoom.increment;
2949             else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
2950                 /* For every pixel zoom 20th of a step */
2951                 increment = output->zoom.increment *
2952                         -wl_fixed_to_double(value) / 20.0;
2953             else
2954                 increment = 0;
2955
2956             output->zoom.level += increment;
2957
2958             if (output->zoom.level < 0.0)
2959                 output->zoom.level = 0.0;
2960             else if (output->zoom.level > output->zoom.max_level)
2961                 output->zoom.level = output->zoom.max_level;
2962             else if (!output->zoom.active) {
2963                 output->zoom.active = 1;
2964                 output->disable_planes++;
2965             }
2966
2967             output->zoom.spring_z.target = output->zoom.level;
2968
2969             weston_output_update_zoom(output, output->zoom.type);
2970         }
2971     }
2972 }
2973
2974 static void
2975 zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
2976           wl_fixed_t value, void *data)
2977 {
2978     do_zoom(seat, time, 0, axis, value);
2979 }
2980
2981 static void
2982 zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2983          void *data)
2984 {
2985     do_zoom(seat, time, key, 0, 0);
2986 }
2987
2988 static void
2989 terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2990           void *data)
2991 {
2992     struct weston_compositor *compositor = data;
2993
2994     wl_display_terminate(compositor->wl_display);
2995 }
2996
2997 static void
2998 rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
2999 {
3000     struct rotate_grab *rotate =
3001         container_of(grab, struct rotate_grab, base.grab);
3002     struct weston_pointer *pointer = grab->pointer;
3003     struct shell_surface *shsurf = rotate->base.shsurf;
3004     struct weston_surface *surface;
3005     float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
3006
3007     if (!shsurf)
3008         return;
3009
3010     surface = shsurf->surface;
3011
3012     cx = 0.5f * surface->geometry.width;
3013     cy = 0.5f * surface->geometry.height;
3014
3015     dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3016     dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
3017     r = sqrtf(dx * dx + dy * dy);
3018
3019     wl_list_remove(&shsurf->rotation.transform.link);
3020     weston_surface_geometry_dirty(shsurf->surface);
3021
3022     if (r > 20.0f) {
3023         struct weston_matrix *matrix =
3024             &shsurf->rotation.transform.matrix;
3025
3026         weston_matrix_init(&rotate->rotation);
3027         weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
3028
3029         weston_matrix_init(matrix);
3030         weston_matrix_translate(matrix, -cx, -cy, 0.0f);
3031         weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
3032         weston_matrix_multiply(matrix, &rotate->rotation);
3033         weston_matrix_translate(matrix, cx, cy, 0.0f);
3034
3035         wl_list_insert(
3036             &shsurf->surface->geometry.transformation_list,
3037             &shsurf->rotation.transform.link);
3038     } else {
3039         wl_list_init(&shsurf->rotation.transform.link);
3040         weston_matrix_init(&shsurf->rotation.rotation);
3041         weston_matrix_init(&rotate->rotation);
3042     }
3043
3044     /* We need to adjust the position of the surface
3045      * in case it was resized in a rotated state before */
3046     cposx = surface->geometry.x + cx;
3047     cposy = surface->geometry.y + cy;
3048     dposx = rotate->center.x - cposx;
3049     dposy = rotate->center.y - cposy;
3050     if (dposx != 0.0f || dposy != 0.0f) {
3051         weston_surface_set_position(surface,
3052                         surface->geometry.x + dposx,
3053                         surface->geometry.y + dposy);
3054     }
3055
3056     /* Repaint implies weston_surface_update_transform(), which
3057      * lazily applies the damage due to rotation update.
3058      */
3059     weston_compositor_schedule_repaint(shsurf->surface->compositor);
3060 }
3061
3062 static void
3063 rotate_grab_button(struct weston_pointer_grab *grab,
3064            uint32_t time, uint32_t button, uint32_t state_w)
3065 {
3066     struct rotate_grab *rotate =
3067         container_of(grab, struct rotate_grab, base.grab);
3068     struct weston_pointer *pointer = grab->pointer;
3069     struct shell_surface *shsurf = rotate->base.shsurf;
3070     enum wl_pointer_button_state state = state_w;
3071
3072     if (pointer->button_count == 0 &&
3073         state == WL_POINTER_BUTTON_STATE_RELEASED) {
3074         if (shsurf)
3075             weston_matrix_multiply(&shsurf->rotation.rotation,
3076                            &rotate->rotation);
3077         shell_grab_end(&rotate->base);
3078         free(rotate);
3079     }
3080 }
3081
3082 static const struct weston_pointer_grab_interface rotate_grab_interface = {
3083     noop_grab_focus,
3084     rotate_grab_motion,
3085     rotate_grab_button,
3086 };
3087
3088 static void
3089 surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
3090 {
3091     struct rotate_grab *rotate;
3092     float dx, dy;
3093     float r;
3094
3095     rotate = malloc(sizeof *rotate);
3096     if (!rotate)
3097         return;
3098
3099     weston_surface_to_global_float(surface->surface,
3100                        surface->surface->geometry.width * 0.5f,
3101                        surface->surface->geometry.height * 0.5f,
3102                        &rotate->center.x, &rotate->center.y);
3103
3104     dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
3105     dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
3106     r = sqrtf(dx * dx + dy * dy);
3107     if (r > 20.0f) {
3108         struct weston_matrix inverse;
3109
3110         weston_matrix_init(&inverse);
3111         weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
3112         weston_matrix_multiply(&surface->rotation.rotation, &inverse);
3113
3114         weston_matrix_init(&rotate->rotation);
3115         weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
3116     } else {
3117         weston_matrix_init(&surface->rotation.rotation);
3118         weston_matrix_init(&rotate->rotation);
3119     }
3120
3121     shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
3122              seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
3123 }
3124
3125 static void
3126 rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
3127            void *data)
3128 {
3129     struct weston_surface *focus =
3130         (struct weston_surface *) seat->pointer->focus;
3131     struct weston_surface *base_surface;
3132     struct shell_surface *surface;
3133
3134     base_surface = weston_surface_get_main_surface(focus);
3135     if (base_surface == NULL)
3136         return;
3137
3138     surface = get_shell_surface(base_surface);
3139     if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
3140         surface->type == SHELL_SURFACE_MAXIMIZED)
3141         return;
3142
3143     surface_rotate(surface, seat);
3144 }
3145
3146 static void
3147 lower_fullscreen_layer(struct desktop_shell *shell)
3148 {
3149     struct workspace *ws;
3150     struct weston_surface *surface, *prev;
3151
3152     ws = get_current_workspace(shell);
3153     wl_list_for_each_reverse_safe(surface, prev,
3154                       &shell->fullscreen_layer.surface_list,
3155                       layer_link)
3156         weston_surface_restack(surface, &ws->layer.surface_list);
3157 }
3158
3159 static void
3160 activate(struct desktop_shell *shell, struct weston_surface *es,
3161      struct weston_seat *seat)
3162 {
3163     struct weston_surface *main_surface;
3164     struct focus_state *state;
3165     struct workspace *ws;
3166
3167     main_surface = weston_surface_get_main_surface(es);
3168
3169     weston_surface_activate(es, seat);
3170
3171     state = ensure_focus_state(shell, seat);
3172     if (state == NULL)
3173         return;
3174
3175     state->keyboard_focus = es;
3176     wl_list_remove(&state->surface_destroy_listener.link);
3177     wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
3178
3179     /* if ico_window_mgr hook, not change surface stack */
3180     if (shell_hook_select)  {
3181         return;
3182     }
3183
3184     switch (get_shell_surface_type(main_surface)) {
3185     case SHELL_SURFACE_FULLSCREEN:
3186         /* should on top of panels */
3187         shell_stack_fullscreen(get_shell_surface(main_surface));
3188         shell_configure_fullscreen(get_shell_surface(main_surface));
3189         break;
3190     default:
3191         restore_all_output_modes(shell->compositor);
3192         ws = get_current_workspace(shell);
3193         weston_surface_restack(main_surface, &ws->layer.surface_list);
3194         break;
3195     }
3196 }
3197
3198 /* no-op func for checking black surface */
3199 static void
3200 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
3201 {
3202 }
3203
3204 static bool
3205 is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
3206 {
3207     if (es->configure == black_surface_configure) {
3208         if (fs_surface)
3209             *fs_surface = (struct weston_surface *)es->configure_private;
3210         return true;
3211     }
3212     return false;
3213 }
3214
3215 static void
3216 click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
3217               void *data)
3218 {
3219     struct weston_seat *ws = (struct weston_seat *) seat;
3220     struct desktop_shell *shell = data;
3221     struct weston_surface *focus;
3222     struct weston_surface *main_surface;
3223
3224     if (button == BTN_LEFT)
3225         focus = (struct weston_surface *) seat->pointer->focus;
3226     else
3227         focus = (struct weston_surface *) seat->touch->focus;
3228     if (!focus)
3229         return;
3230
3231     if (is_black_surface(focus, &main_surface))
3232         focus = main_surface;
3233
3234     main_surface = weston_surface_get_main_surface(focus);
3235     if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
3236         return;
3237
3238     if (seat->pointer->grab == &seat->pointer->default_grab)
3239         activate(shell, focus, ws);
3240
3241     /* if ico_window_mgr hook, call hook routine    */
3242     if (shell_hook_select)  {
3243         (*shell_hook_select)(focus);
3244     }
3245 }
3246
3247 static void
3248 lock(struct desktop_shell *shell)
3249 {
3250     struct workspace *ws = get_current_workspace(shell);
3251
3252     if (shell->locked) {
3253         weston_compositor_sleep(shell->compositor);
3254         return;
3255     }
3256
3257     shell->locked = true;
3258
3259     /* Hide all surfaces by removing the fullscreen, panel and
3260      * toplevel layers.  This way nothing else can show or receive
3261      * input events while we are locked. */
3262
3263     wl_list_remove(&shell->panel_layer.link);
3264     wl_list_remove(&shell->fullscreen_layer.link);
3265     if (shell->showing_input_panels)
3266         wl_list_remove(&shell->input_panel_layer.link);
3267     wl_list_remove(&ws->layer.link);
3268     wl_list_insert(&shell->compositor->cursor_layer.link,
3269                &shell->lock_layer.link);
3270
3271     launch_screensaver(shell);
3272
3273     /* TODO: disable bindings that should not work while locked. */
3274
3275     /* All this must be undone in resume_desktop(). */
3276 }
3277
3278 static void
3279 unlock(struct desktop_shell *shell)
3280 {
3281     if (!shell->locked || shell->lock_surface) {
3282         shell_fade(shell, FADE_IN);
3283         return;
3284     }
3285
3286     /* If desktop-shell client has gone away, unlock immediately. */
3287     if (!shell->child.desktop_shell) {
3288         resume_desktop(shell);
3289         return;
3290     }
3291
3292     if (shell->prepare_event_sent)
3293         return;
3294
3295     desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
3296     shell->prepare_event_sent = true;
3297 }
3298
3299 static void
3300 shell_fade_done(struct weston_surface_animation *animation, void *data)
3301 {
3302     struct desktop_shell *shell = data;
3303
3304     shell->fade.animation = NULL;
3305
3306     switch (shell->fade.type) {
3307     case FADE_IN:
3308         weston_surface_destroy(shell->fade.surface);
3309         shell->fade.surface = NULL;
3310         break;
3311     case FADE_OUT:
3312         lock(shell);
3313         break;
3314     }
3315 }
3316
3317 static struct weston_surface *
3318 shell_fade_create_surface(struct desktop_shell *shell)
3319 {
3320     struct weston_compositor *compositor = shell->compositor;
3321     struct weston_surface *surface;
3322
3323     surface = weston_surface_create(compositor);
3324     if (!surface)
3325         return NULL;
3326
3327     uifw_trace("shell_fade_create_surface: configure %08x x/y=%d/%d w/h=%d/%d",
3328                (int)surface, 0, 0, 8192, 8192);
3329     weston_surface_configure(surface, 0, 0, 8192, 8192);
3330     weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
3331     wl_list_insert(&compositor->fade_layer.surface_list,
3332                &surface->layer_link);
3333     pixman_region32_init(&surface->input);
3334
3335     return surface;
3336 }
3337
3338 static void
3339 shell_fade(struct desktop_shell *shell, enum fade_type type)
3340 {
3341     float tint;
3342
3343     switch (type) {
3344     case FADE_IN:
3345         tint = 0.0;
3346         break;
3347     case FADE_OUT:
3348         tint = 1.0;
3349         break;
3350     default:
3351         weston_log("shell: invalid fade type\n");
3352         return;
3353     }
3354
3355     shell->fade.type = type;
3356
3357     if (shell->fade.surface == NULL) {
3358         shell->fade.surface = shell_fade_create_surface(shell);
3359         if (!shell->fade.surface)
3360             return;
3361
3362         shell->fade.surface->alpha = 1.0 - tint;
3363         weston_surface_update_transform(shell->fade.surface);
3364     }
3365
3366     if (shell->fade.animation)
3367         weston_fade_update(shell->fade.animation, tint);
3368     else
3369         shell->fade.animation =
3370             weston_fade_run(shell->fade.surface,
3371                     1.0 - tint, tint, 300.0,
3372                     shell_fade_done, shell);
3373 }
3374
3375 static void
3376 do_shell_fade_startup(void *data)
3377 {
3378     struct desktop_shell *shell = data;
3379
3380     shell_fade(shell, FADE_IN);
3381 }
3382
3383 static void
3384 shell_fade_startup(struct desktop_shell *shell)
3385 {
3386     struct wl_event_loop *loop;
3387
3388     if (!shell->fade.startup_timer)
3389         return;
3390
3391     wl_event_source_remove(shell->fade.startup_timer);
3392     shell->fade.startup_timer = NULL;
3393
3394     loop = wl_display_get_event_loop(shell->compositor->wl_display);
3395     wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3396 }
3397
3398 static int
3399 fade_startup_timeout(void *data)
3400 {
3401     struct desktop_shell *shell = data;
3402
3403     shell_fade_startup(shell);
3404     return 0;
3405 }
3406
3407 static void
3408 shell_fade_init(struct desktop_shell *shell)
3409 {
3410     /* Make compositor output all black, and wait for the desktop-shell
3411      * client to signal it is ready, then fade in. The timer triggers a
3412      * fade-in, in case the desktop-shell client takes too long.
3413      */
3414
3415     struct wl_event_loop *loop;
3416
3417     if (shell->fade.surface != NULL) {
3418         weston_log("%s: warning: fade surface already exists\n",
3419                __func__);
3420         return;
3421     }
3422
3423     shell->fade.surface = shell_fade_create_surface(shell);
3424     if (!shell->fade.surface)
3425         return;
3426
3427     weston_surface_update_transform(shell->fade.surface);
3428     weston_surface_damage(shell->fade.surface);
3429
3430     loop = wl_display_get_event_loop(shell->compositor->wl_display);
3431     shell->fade.startup_timer =
3432         wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3433     wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3434 }
3435
3436 static void
3437 idle_handler(struct wl_listener *listener, void *data)
3438 {
3439     struct desktop_shell *shell =
3440         container_of(listener, struct desktop_shell, idle_listener);
3441
3442     shell_fade(shell, FADE_OUT);
3443     /* lock() is called from shell_fade_done() */
3444 }
3445
3446 static void
3447 wake_handler(struct wl_listener *listener, void *data)
3448 {
3449     struct desktop_shell *shell =
3450         container_of(listener, struct desktop_shell, wake_listener);
3451
3452     unlock(shell);
3453 }
3454
3455 static void
3456 show_input_panels(struct wl_listener *listener, void *data)
3457 {
3458     struct desktop_shell *shell =
3459         container_of(listener, struct desktop_shell,
3460                  show_input_panel_listener);
3461     struct input_panel_surface *surface, *next;
3462     struct weston_surface *ws;
3463
3464     shell->text_input.surface = (struct weston_surface*)data;
3465
3466     if (shell->showing_input_panels)
3467         return;
3468
3469     shell->showing_input_panels = true;
3470
3471     if (!shell->locked)
3472         wl_list_insert(&shell->panel_layer.link,
3473                    &shell->input_panel_layer.link);
3474
3475     wl_list_for_each_safe(surface, next,
3476                   &shell->input_panel.surfaces, link) {
3477         ws = surface->surface;
3478         if (!ws->buffer_ref.buffer)
3479             continue;
3480         wl_list_insert(&shell->input_panel_layer.surface_list,
3481                    &ws->layer_link);
3482         weston_surface_geometry_dirty(ws);
3483         weston_surface_update_transform(ws);
3484         weston_surface_damage(ws);
3485         weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
3486     }
3487 }
3488
3489 static void
3490 hide_input_panels(struct wl_listener *listener, void *data)
3491 {
3492     struct desktop_shell *shell =
3493         container_of(listener, struct desktop_shell,
3494                  hide_input_panel_listener);
3495     struct weston_surface *surface, *next;
3496
3497     if (!shell->showing_input_panels)
3498         return;
3499
3500     shell->showing_input_panels = false;
3501
3502     if (!shell->locked)
3503         wl_list_remove(&shell->input_panel_layer.link);
3504
3505     wl_list_for_each_safe(surface, next,
3506                   &shell->input_panel_layer.surface_list, layer_link)
3507         weston_surface_unmap(surface);
3508 }
3509
3510 static void
3511 update_input_panels(struct wl_listener *listener, void *data)
3512 {
3513     struct desktop_shell *shell =
3514         container_of(listener, struct desktop_shell,
3515                  update_input_panel_listener);
3516
3517     memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3518 }
3519
3520 static void
3521 center_on_output(struct weston_surface *surface, struct weston_output *output)
3522 {
3523     int32_t surf_x, surf_y, width, height;
3524     float x, y;
3525
3526     surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y, &width, &height);
3527
3528     x = output->x + (output->width - width) / 2 - surf_x / 2;
3529     y = output->y + (output->height - height) / 2 - surf_y / 2;
3530
3531     uifw_trace("center_on_output: %08x x/y=%d/%d w/h=%d/%d",
3532                (int)output, (int)x, (int)y, width, height);
3533     weston_surface_configure(surface, x, y, width, height);
3534 }
3535
3536 static void
3537 weston_surface_set_initial_position (struct weston_surface *surface,
3538                      struct desktop_shell *shell)
3539 {
3540     struct weston_compositor *compositor = shell->compositor;
3541     int ix = 0, iy = 0;
3542     int range_x, range_y;
3543     int dx, dy, x, y, panel_height;
3544     struct weston_output *output, *target_output = NULL;
3545     struct weston_seat *seat;
3546
3547     /* As a heuristic place the new window on the same output as the
3548      * pointer. Falling back to the output containing 0, 0.
3549      *
3550      * TODO: Do something clever for touch too?
3551      */
3552     wl_list_for_each(seat, &compositor->seat_list, link) {
3553         if (seat->pointer) {
3554             ix = wl_fixed_to_int(seat->pointer->x);
3555             iy = wl_fixed_to_int(seat->pointer->y);
3556             break;
3557         }
3558     }
3559
3560     wl_list_for_each(output, &compositor->output_list, link) {
3561         if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3562             target_output = output;
3563             break;
3564         }
3565     }
3566
3567     if (!target_output) {
3568         weston_surface_set_position(surface, 10 + random() % 400,
3569                        10 + random() % 400);
3570         return;
3571     }
3572
3573     /* Valid range within output where the surface will still be onscreen.
3574      * If this is negative it means that the surface is bigger than
3575      * output.
3576      */
3577     panel_height = get_output_panel_height(shell, target_output);
3578     range_x = target_output->width - surface->geometry.width;
3579     range_y = (target_output->height - panel_height) -
3580           surface->geometry.height;
3581
3582     if (range_x > 0)
3583         dx = random() % range_x;
3584     else
3585         dx = 0;
3586
3587     if (range_y > 0)
3588         dy = panel_height + random() % range_y;
3589     else
3590         dy = panel_height;
3591
3592     x = target_output->x + dx;
3593     y = target_output->y + dy;
3594
3595     weston_surface_set_position (surface, x, y);
3596 }
3597
3598 static void
3599 map(struct desktop_shell *shell, struct weston_surface *surface,
3600     int32_t width, int32_t height, int32_t sx, int32_t sy)
3601 {
3602     struct weston_compositor *compositor = shell->compositor;
3603     struct shell_surface *shsurf = get_shell_surface(surface);
3604     enum shell_surface_type surface_type = shsurf->type;
3605     struct weston_surface *parent;
3606     struct weston_seat *seat;
3607     struct workspace *ws;
3608     int panel_height = 0;
3609     int32_t surf_x, surf_y;
3610
3611     uifw_trace("map: %08x sx/sy=%d/%d w/h=%d/%d", (int)surface, sx, sy, width, height);
3612     surface->geometry.width = width;
3613     surface->geometry.height = height;
3614     weston_surface_geometry_dirty(surface);
3615
3616     /* initial positioning, see also configure() */
3617     switch (surface_type) {
3618     case SHELL_SURFACE_TOPLEVEL:
3619         weston_surface_set_initial_position(surface, shell);
3620         break;
3621     case SHELL_SURFACE_FULLSCREEN:
3622         center_on_output(surface, shsurf->fullscreen_output);
3623         shell_map_fullscreen(shsurf);
3624         break;
3625     case SHELL_SURFACE_MAXIMIZED:
3626         /* use surface configure to set the geometry */
3627         panel_height = get_output_panel_height(shell,surface->output);
3628         surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3629                                                          NULL, NULL);
3630         weston_surface_set_position(surface, shsurf->output->x - surf_x,
3631                                     shsurf->output->y + panel_height - surf_y);
3632         break;
3633     case SHELL_SURFACE_POPUP:
3634         shell_map_popup(shsurf);
3635         break;
3636     case SHELL_SURFACE_NONE:
3637         weston_surface_set_position(surface,
3638                         surface->geometry.x + sx,
3639                         surface->geometry.y + sy);
3640         break;
3641     default:
3642         ;
3643     }
3644
3645     /* surface stacking order, see also activate() */
3646     switch (surface_type) {
3647     case SHELL_SURFACE_POPUP:
3648     case SHELL_SURFACE_TRANSIENT:
3649         parent = shsurf->parent;
3650         wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3651         break;
3652     case SHELL_SURFACE_FULLSCREEN:
3653     case SHELL_SURFACE_NONE:
3654         break;
3655     case SHELL_SURFACE_XWAYLAND:
3656     default:
3657         ws = get_current_workspace(shell);
3658         wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
3659         break;
3660     }
3661
3662     if (surface_type != SHELL_SURFACE_NONE) {
3663         weston_surface_update_transform(surface);
3664         if (surface_type == SHELL_SURFACE_MAXIMIZED)
3665             surface->output = shsurf->output;
3666     }
3667
3668     switch (surface_type) {
3669     /* XXX: xwayland's using the same fields for transient type */
3670     case SHELL_SURFACE_XWAYLAND:
3671     case SHELL_SURFACE_TRANSIENT:
3672         if (shsurf->transient.flags ==
3673                 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3674             break;
3675     case SHELL_SURFACE_TOPLEVEL:
3676     case SHELL_SURFACE_FULLSCREEN:
3677     case SHELL_SURFACE_MAXIMIZED:
3678         if (!shell->locked) {
3679             wl_list_for_each(seat, &compositor->seat_list, link)
3680                 activate(shell, surface, seat);
3681         }
3682         break;
3683     default:
3684         break;
3685     }
3686
3687     if (surface_type == SHELL_SURFACE_TOPLEVEL)
3688     {
3689         switch (shell->win_animation_type) {
3690         case ANIMATION_FADE:
3691             weston_fade_run(surface, 0.0, 1.0, 300.0, NULL, NULL);
3692             break;
3693         case ANIMATION_ZOOM:
3694             weston_zoom_run(surface, 0.5, 1.0, NULL, NULL);
3695             break;
3696         default:
3697             break;
3698         }
3699     }
3700
3701     /* if ico_window_mgr hook, call hook routine    */
3702     if (shell_hook_map)  {
3703         (*shell_hook_map)(surface, &width, &height, &sx, &sy);
3704     }
3705     if (shell_hook_configure)  {
3706         (*shell_hook_configure)(surface);           /* send event to manager    */
3707     }
3708 }
3709
3710 static void
3711 configure(struct desktop_shell *shell, struct weston_surface *surface,
3712       float x, float y, int32_t width, int32_t height)
3713 {
3714     enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3715     struct shell_surface *shsurf;
3716     int32_t surf_x, surf_y;
3717
3718     shsurf = get_shell_surface(surface);
3719     if (shsurf)
3720         surface_type = shsurf->type;
3721
3722     uifw_trace("configure: %08x x/y=%d/%d w/h=%d/%d",
3723                (int)surface, (int)x, (int)y, width, height);
3724     weston_surface_configure(surface, x, y, width, height);
3725
3726     switch (surface_type) {
3727     case SHELL_SURFACE_FULLSCREEN:
3728         shell_stack_fullscreen(shsurf);
3729         shell_configure_fullscreen(shsurf);
3730         break;
3731     case SHELL_SURFACE_MAXIMIZED:
3732         /* setting x, y and using configure to change that geometry */
3733         surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3734                                                          NULL, NULL);
3735         surface->geometry.x = surface->output->x - surf_x;
3736         surface->geometry.y = surface->output->y +
3737         get_output_panel_height(shell,surface->output) - surf_y;
3738         break;
3739     case SHELL_SURFACE_TOPLEVEL:
3740         break;
3741     default:
3742         break;
3743     }
3744
3745     /* XXX: would a fullscreen surface need the same handling? */
3746     if (surface->output) {
3747         weston_surface_update_transform(surface);
3748
3749         if (surface_type == SHELL_SURFACE_MAXIMIZED)
3750             surface->output = shsurf->output;
3751     }
3752
3753     /* if ico_window_mgr hook, call hook routine    */
3754     if (shell_hook_configure)  {
3755         (*shell_hook_configure)(surface);
3756     }
3757 }
3758
3759 static void
3760 shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
3761 {
3762     struct shell_surface *shsurf = get_shell_surface(es);
3763     struct desktop_shell *shell = shsurf->shell;
3764
3765     int type_changed = 0;
3766
3767     if (!weston_surface_is_mapped(es) &&
3768         !wl_list_empty(&shsurf->popup.grab_link)) {
3769         remove_popup_grab(shsurf);
3770     }
3771
3772     if (width == 0)
3773         return;
3774
3775     if (shsurf->next_type != SHELL_SURFACE_NONE &&
3776         shsurf->type != shsurf->next_type) {
3777         set_surface_type(shsurf);
3778         type_changed = 1;
3779     }
3780
3781     if (!weston_surface_is_mapped(es)) {
3782         map(shell, es, width, height, sx, sy);
3783     } else if (type_changed || sx != 0 || sy != 0 ||
3784            es->geometry.width != width ||
3785            es->geometry.height != height) {
3786         float from_x, from_y;
3787         float to_x, to_y;
3788
3789         weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3790         weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3791         uifw_trace("shell_surface_configure: configure %08x x/y=%d/%d w/h=%d/%d",
3792                    (int)es, (int)(es->geometry.x + to_x - from_x),
3793                    (int)(es->geometry.y + to_y - from_y), width, height);
3794         configure(shell, es,
3795               es->geometry.x + to_x - from_x,
3796               es->geometry.y + to_y - from_y,
3797               width, height);
3798     }
3799 }
3800
3801 static void launch_desktop_shell_process(void *data);
3802
3803 static void
3804 desktop_shell_sigchld(struct weston_process *process, int status)
3805 {
3806     uint32_t time;
3807     struct desktop_shell *shell =
3808         container_of(process, struct desktop_shell, child.process);
3809
3810     shell->child.process.pid = 0;
3811     shell->child.client = NULL; /* already destroyed by wayland */
3812
3813     /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3814     time = weston_compositor_get_time();
3815     if (time - shell->child.deathstamp > 30000) {
3816         shell->child.deathstamp = time;
3817         shell->child.deathcount = 0;
3818     }
3819
3820     shell->child.deathcount++;
3821     if (shell->child.deathcount > 5) {
3822         weston_log("%s died, giving up.\n", shell_exe);
3823         return;
3824     }
3825
3826     weston_log("%s died, respawning...\n", shell_exe);
3827     launch_desktop_shell_process(shell);
3828     shell_fade_startup(shell);
3829 }
3830
3831 static void
3832 launch_desktop_shell_process(void *data)
3833 {
3834     struct desktop_shell *shell = data;
3835     /* shell program path configurable for ico-ivi                  */
3836     /* const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";  */
3837
3838     if ((shell_exe[0] == 0) || (shell_exe[0] == ' '))   {
3839         weston_log("no shell program\n");
3840     }
3841     else    {
3842         shell->child.client = weston_client_launch(shell->compositor,
3843                                                    &shell->child.process,
3844                                                    shell_exe,
3845                                                    desktop_shell_sigchld);
3846
3847         if (!shell->child.client)
3848             weston_log("not able to start %s\n", shell_exe);
3849         else
3850             weston_log("shell %s started\n", shell_exe);
3851     }
3852 }
3853
3854 static void
3855 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3856 {
3857     struct desktop_shell *shell = data;
3858     struct wl_resource *resource;
3859
3860     resource = wl_resource_create(client, &wl_shell_interface, 1, id);
3861     if (resource)
3862         wl_resource_set_implementation(resource, &shell_implementation,
3863                            shell, NULL);
3864
3865     /* if ico_window_mgr hook, call hook routine    */
3866     if (shell_hook_bind)    {
3867         (*shell_hook_bind)(client, data);
3868     }
3869 }
3870
3871 static void
3872 unbind_desktop_shell(struct wl_resource *resource)
3873 {
3874     struct desktop_shell *shell = wl_resource_get_user_data(resource);
3875
3876     /* if ico_window_mgr hook, call hook routine    */
3877     if (shell_hook_unbind)  {
3878         (*shell_hook_unbind)(resource->client);
3879     }
3880
3881     if (shell->locked)
3882         resume_desktop(shell);
3883
3884     shell->child.desktop_shell = NULL;
3885     shell->prepare_event_sent = false;
3886 }
3887
3888 static void
3889 bind_desktop_shell(struct wl_client *client,
3890            void *data, uint32_t version, uint32_t id)
3891 {
3892     struct desktop_shell *shell = data;
3893     struct wl_resource *resource;
3894
3895     resource = wl_resource_create(client, &desktop_shell_interface,
3896                       MIN(version, 2), id);
3897
3898     if (client == shell->child.client) {
3899         wl_resource_set_implementation(resource,
3900                            &desktop_shell_implementation,
3901                            shell, unbind_desktop_shell);
3902         shell->child.desktop_shell = resource;
3903
3904         if (version < 2)
3905             shell_fade_startup(shell);
3906
3907         return;
3908     }
3909
3910     wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3911                    "permission to bind desktop_shell denied");
3912     wl_resource_destroy(resource);
3913 }
3914
3915 static void
3916 screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
3917 {
3918     struct desktop_shell *shell = surface->configure_private;
3919
3920     if (width == 0)
3921         return;
3922
3923     /* XXX: starting weston-screensaver beforehand does not work */
3924     if (!shell->locked)
3925         return;
3926
3927     center_on_output(surface, surface->output);
3928
3929     if (wl_list_empty(&surface->layer_link)) {
3930         wl_list_insert(shell->lock_layer.surface_list.prev,
3931                    &surface->layer_link);
3932         weston_surface_update_transform(surface);
3933         wl_event_source_timer_update(shell->screensaver.timer,
3934                          shell->screensaver.duration);
3935         shell_fade(shell, FADE_IN);
3936     }
3937 }
3938
3939 static void
3940 screensaver_set_surface(struct wl_client *client,
3941             struct wl_resource *resource,
3942             struct wl_resource *surface_resource,
3943             struct wl_resource *output_resource)
3944 {
3945     struct desktop_shell *shell = wl_resource_get_user_data(resource);
3946     struct weston_surface *surface =
3947         wl_resource_get_user_data(surface_resource);
3948     struct weston_output *output = wl_resource_get_user_data(output_resource);
3949
3950     surface->configure = screensaver_configure;
3951     surface->configure_private = shell;
3952     surface->output = output;
3953 }
3954
3955 static const struct screensaver_interface screensaver_implementation = {
3956     screensaver_set_surface
3957 };
3958
3959 static void
3960 unbind_screensaver(struct wl_resource *resource)
3961 {
3962     struct desktop_shell *shell = wl_resource_get_user_data(resource);
3963
3964     shell->screensaver.binding = NULL;
3965 }
3966
3967 static void
3968 bind_screensaver(struct wl_client *client,
3969          void *data, uint32_t version, uint32_t id)
3970 {
3971     struct desktop_shell *shell = data;
3972     struct wl_resource *resource;
3973
3974     resource = wl_resource_create(client, &screensaver_interface, 1, id);
3975
3976     if (shell->screensaver.binding == NULL) {
3977         wl_resource_set_implementation(resource,
3978                            &screensaver_implementation,
3979                            shell, unbind_screensaver);
3980         shell->screensaver.binding = resource;
3981         return;
3982     }
3983
3984     wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3985                    "interface object already bound");
3986     wl_resource_destroy(resource);
3987 }
3988
3989 static void
3990 input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
3991 {
3992     struct input_panel_surface *ip_surface = surface->configure_private;
3993     struct desktop_shell *shell = ip_surface->shell;
3994     float x, y;
3995     uint32_t show_surface = 0;
3996
3997     if (width == 0)
3998         return;
3999
4000     if (!weston_surface_is_mapped(surface)) {
4001         if (!shell->showing_input_panels)
4002             return;
4003
4004         show_surface = 1;
4005     }
4006
4007     fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
4008
4009     if (ip_surface->panel) {
4010         x = shell->text_input.surface->geometry.x + shell->text_input.cursor_rectangle.x2;
4011         y = shell->text_input.surface->geometry.y + shell->text_input.cursor_rectangle.y2;
4012     } else {
4013         x = ip_surface->output->x + (ip_surface->output->width - width) / 2;
4014         y = ip_surface->output->y + ip_surface->output->height - height;
4015     }
4016
4017     uifw_trace("input_panel_configure: configure %08x x/y=%d/%d w/h=%d/%d",
4018                (int)surface, (int)x, (int)y, width, height);
4019     weston_surface_configure(surface,
4020                  x, y,
4021                  width, height);
4022
4023     if (show_surface) {
4024         wl_list_insert(&shell->input_panel_layer.surface_list,
4025                    &surface->layer_link);
4026         weston_surface_update_transform(surface);
4027         weston_surface_damage(surface);
4028         weston_slide_run(surface, surface->geometry.height, 0, NULL, NULL);
4029     }
4030 }
4031
4032 static void
4033 destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
4034 {
4035     wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
4036
4037     wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
4038     wl_list_remove(&input_panel_surface->link);
4039
4040     input_panel_surface->surface->configure = NULL;
4041
4042     free(input_panel_surface);
4043 }
4044
4045 static struct input_panel_surface *
4046 get_input_panel_surface(struct weston_surface *surface)
4047 {
4048     if (surface->configure == input_panel_configure) {
4049         return surface->configure_private;
4050     } else {
4051         return NULL;
4052     }
4053 }
4054
4055 static void
4056 input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
4057 {
4058     struct input_panel_surface *ipsurface = container_of(listener,
4059                                  struct input_panel_surface,
4060                                  surface_destroy_listener);
4061
4062     if (ipsurface->resource) {
4063         wl_resource_destroy(ipsurface->resource);
4064     } else {
4065         destroy_input_panel_surface(ipsurface);
4066     }
4067 }
4068
4069 static struct input_panel_surface *
4070 create_input_panel_surface(struct desktop_shell *shell,
4071                struct weston_surface *surface)
4072 {
4073     struct input_panel_surface *input_panel_surface;
4074
4075     input_panel_surface = calloc(1, sizeof *input_panel_surface);
4076     if (!input_panel_surface)
4077         return NULL;
4078
4079     surface->configure = input_panel_configure;
4080     surface->configure_private = input_panel_surface;
4081
4082     input_panel_surface->shell = shell;
4083
4084     input_panel_surface->surface = surface;
4085
4086     wl_signal_init(&input_panel_surface->destroy_signal);
4087     input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
4088     wl_signal_add(&surface->destroy_signal,
4089               &input_panel_surface->surface_destroy_listener);
4090
4091     wl_list_init(&input_panel_surface->link);
4092
4093     return input_panel_surface;
4094 }
4095
4096 static void
4097 input_panel_surface_set_toplevel(struct wl_client *client,
4098                  struct wl_resource *resource,
4099                  struct wl_resource *output_resource,
4100                  uint32_t position)
4101 {
4102     struct input_panel_surface *input_panel_surface =
4103         wl_resource_get_user_data(resource);
4104     struct desktop_shell *shell = input_panel_surface->shell;
4105
4106     wl_list_insert(&shell->input_panel.surfaces,
4107                &input_panel_surface->link);
4108
4109     input_panel_surface->output = wl_resource_get_user_data(output_resource);
4110     input_panel_surface->panel = 0;
4111 }
4112
4113 static void
4114 input_panel_surface_set_overlay_panel(struct wl_client *client,
4115                       struct wl_resource *resource)
4116 {
4117     struct input_panel_surface *input_panel_surface =
4118         wl_resource_get_user_data(resource);
4119     struct desktop_shell *shell = input_panel_surface->shell;
4120
4121     wl_list_insert(&shell->input_panel.surfaces,
4122                &input_panel_surface->link);
4123
4124     input_panel_surface->panel = 1;
4125 }
4126
4127 static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
4128     input_panel_surface_set_toplevel,
4129     input_panel_surface_set_overlay_panel
4130 };
4131
4132 static void
4133 destroy_input_panel_surface_resource(struct wl_resource *resource)
4134 {
4135     struct input_panel_surface *ipsurf =
4136         wl_resource_get_user_data(resource);
4137
4138     destroy_input_panel_surface(ipsurf);
4139 }
4140
4141 static void
4142 input_panel_get_input_panel_surface(struct wl_client *client,
4143                     struct wl_resource *resource,
4144                     uint32_t id,
4145                     struct wl_resource *surface_resource)
4146 {
4147     struct weston_surface *surface =
4148         wl_resource_get_user_data(surface_resource);
4149     struct desktop_shell *shell = wl_resource_get_user_data(resource);
4150     struct input_panel_surface *ipsurf;
4151
4152     if (get_input_panel_surface(surface)) {
4153         wl_resource_post_error(surface_resource,
4154                        WL_DISPLAY_ERROR_INVALID_OBJECT,
4155                        "wl_input_panel::get_input_panel_surface already requested");
4156         return;
4157     }
4158
4159     ipsurf = create_input_panel_surface(shell, surface);
4160     if (!ipsurf) {
4161         wl_resource_post_error(surface_resource,
4162                        WL_DISPLAY_ERROR_INVALID_OBJECT,
4163                        "surface->configure already set");
4164         return;
4165     }
4166
4167     ipsurf->resource =
4168         wl_resource_create(client,
4169                    &wl_input_panel_surface_interface, 1, id);
4170     wl_resource_set_implementation(ipsurf->resource,
4171                        &input_panel_surface_implementation,
4172                        ipsurf,
4173                        destroy_input_panel_surface_resource);
4174 }
4175
4176 static const struct wl_input_panel_interface input_panel_implementation = {
4177     input_panel_get_input_panel_surface
4178 };
4179
4180 static void
4181 unbind_input_panel(struct wl_resource *resource)
4182 {
4183     struct desktop_shell *shell = wl_resource_get_user_data(resource);
4184
4185     shell->input_panel.binding = NULL;
4186 }
4187
4188 static void
4189 bind_input_panel(struct wl_client *client,
4190           void *data, uint32_t version, uint32_t id)
4191 {
4192     struct desktop_shell *shell = data;
4193     struct wl_resource *resource;
4194
4195     resource = wl_resource_create(client,
4196                       &wl_input_panel_interface, 1, id);
4197
4198     if (shell->input_panel.binding == NULL) {
4199         wl_resource_set_implementation(resource,
4200                            &input_panel_implementation,
4201                            shell, unbind_input_panel);
4202         shell->input_panel.binding = resource;
4203         return;
4204     }
4205
4206     wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4207                    "interface object already bound");
4208     wl_resource_destroy(resource);
4209 }
4210
4211 struct switcher {
4212     struct desktop_shell *shell;
4213     struct weston_surface *current;
4214     struct wl_listener listener;
4215     struct weston_keyboard_grab grab;
4216 };
4217
4218 static void
4219 switcher_next(struct switcher *switcher)
4220 {
4221     struct weston_surface *surface;
4222     struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
4223     struct shell_surface *shsurf;
4224     struct workspace *ws = get_current_workspace(switcher->shell);
4225
4226     wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
4227         switch (get_shell_surface_type(surface)) {
4228         case SHELL_SURFACE_TOPLEVEL:
4229         case SHELL_SURFACE_FULLSCREEN:
4230         case SHELL_SURFACE_MAXIMIZED:
4231             if (first == NULL)
4232                 first = surface;
4233             if (prev == switcher->current)
4234                 next = surface;
4235             prev = surface;
4236             surface->alpha = 0.25;
4237             weston_surface_geometry_dirty(surface);
4238             weston_surface_damage(surface);
4239             break;
4240         default:
4241             break;
4242         }
4243
4244         if (is_black_surface(surface, NULL)) {
4245             surface->alpha = 0.25;
4246             weston_surface_geometry_dirty(surface);
4247             weston_surface_damage(surface);
4248         }
4249     }
4250
4251     if (next == NULL)
4252         next = first;
4253
4254     if (next == NULL)
4255         return;
4256
4257     wl_list_remove(&switcher->listener.link);
4258     wl_signal_add(&next->destroy_signal, &switcher->listener);
4259
4260     switcher->current = next;
4261     next->alpha = 1.0;
4262
4263     shsurf = get_shell_surface(switcher->current);
4264     if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
4265         shsurf->fullscreen.black_surface->alpha = 1.0;
4266 }
4267
4268 static void
4269 switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
4270 {
4271     struct switcher *switcher =
4272         container_of(listener, struct switcher, listener);
4273
4274     switcher_next(switcher);
4275 }
4276
4277 static void
4278 switcher_destroy(struct switcher *switcher)
4279 {
4280     struct weston_surface *surface;
4281     struct weston_keyboard *keyboard = switcher->grab.keyboard;
4282     struct workspace *ws = get_current_workspace(switcher->shell);
4283
4284     wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
4285         surface->alpha = 1.0;
4286         weston_surface_damage(surface);
4287     }
4288
4289     if (switcher->current)
4290         activate(switcher->shell, switcher->current,
4291              (struct weston_seat *) keyboard->seat);
4292     wl_list_remove(&switcher->listener.link);
4293     weston_keyboard_end_grab(keyboard);
4294     if (keyboard->input_method_resource)
4295         keyboard->grab = &keyboard->input_method_grab;
4296     free(switcher);
4297 }
4298
4299 static void
4300 switcher_key(struct weston_keyboard_grab *grab,
4301          uint32_t time, uint32_t key, uint32_t state_w)
4302 {
4303     struct switcher *switcher = container_of(grab, struct switcher, grab);
4304     enum wl_keyboard_key_state state = state_w;
4305
4306     if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
4307         switcher_next(switcher);
4308 }
4309
4310 static void
4311 switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
4312           uint32_t mods_depressed, uint32_t mods_latched,
4313           uint32_t mods_locked, uint32_t group)
4314 {
4315     struct switcher *switcher = container_of(grab, struct switcher, grab);
4316     struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
4317
4318     if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4319         switcher_destroy(switcher);
4320 }
4321
4322 static const struct weston_keyboard_grab_interface switcher_grab = {
4323     switcher_key,
4324     switcher_modifier,
4325 };
4326
4327 static void
4328 switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
4329          void *data)
4330 {
4331     struct desktop_shell *shell = data;
4332     struct switcher *switcher;
4333
4334     switcher = malloc(sizeof *switcher);
4335     switcher->shell = shell;
4336     switcher->current = NULL;
4337     switcher->listener.notify = switcher_handle_surface_destroy;
4338     wl_list_init(&switcher->listener.link);
4339
4340     restore_all_output_modes(shell->compositor);
4341     lower_fullscreen_layer(switcher->shell);
4342     switcher->grab.interface = &switcher_grab;
4343     weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4344     weston_keyboard_set_focus(seat->keyboard, NULL);
4345     switcher_next(switcher);
4346 }
4347
4348 static void
4349 backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
4350           void *data)
4351 {
4352     struct weston_compositor *compositor = data;
4353     struct weston_output *output;
4354     long backlight_new = 0;
4355
4356     /* TODO: we're limiting to simple use cases, where we assume just
4357      * control on the primary display. We'd have to extend later if we
4358      * ever get support for setting backlights on random desktop LCD
4359      * panels though */
4360     output = get_default_output(compositor);
4361     if (!output)
4362         return;
4363
4364     if (!output->set_backlight)
4365         return;
4366
4367     if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4368         backlight_new = output->backlight_current - 25;
4369     else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4370         backlight_new = output->backlight_current + 25;
4371
4372     if (backlight_new < 5)
4373         backlight_new = 5;
4374     if (backlight_new > 255)
4375         backlight_new = 255;
4376
4377     output->backlight_current = backlight_new;
4378     output->set_backlight(output, output->backlight_current);
4379 }
4380
4381 struct debug_binding_grab {
4382     struct weston_keyboard_grab grab;
4383     struct weston_seat *seat;
4384     uint32_t key[2];
4385     int key_released[2];
4386 };
4387
4388 static void
4389 debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
4390           uint32_t key, uint32_t state)
4391 {
4392     struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
4393     struct wl_resource *resource;
4394     struct wl_display *display;
4395     uint32_t serial;
4396     int send = 0, terminate = 0;
4397     int check_binding = 1;
4398     int i;
4399
4400     if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
4401         /* Do not run bindings on key releases */
4402         check_binding = 0;
4403
4404         for (i = 0; i < 2; i++)
4405             if (key == db->key[i])
4406                 db->key_released[i] = 1;
4407
4408         if (db->key_released[0] && db->key_released[1]) {
4409             /* All key releases been swalled so end the grab */
4410             terminate = 1;
4411         } else if (key != db->key[0] && key != db->key[1]) {
4412             /* Should not swallow release of other keys */
4413             send = 1;
4414         }
4415     } else if (key == db->key[0] && !db->key_released[0]) {
4416         /* Do not check bindings for the first press of the binding
4417          * key. This allows it to be used as a debug shortcut.
4418          * We still need to swallow this event. */
4419         check_binding = 0;
4420     } else if (db->key[1]) {
4421         /* If we already ran a binding don't process another one since
4422          * we can't keep track of all the binding keys that were
4423          * pressed in order to swallow the release events. */
4424         send = 1;
4425         check_binding = 0;
4426     }
4427
4428     if (check_binding) {
4429         struct weston_compositor *ec = db->seat->compositor;
4430
4431         if (weston_compositor_run_debug_binding(ec, db->seat, time,
4432                             key, state)) {
4433             /* We ran a binding so swallow the press and keep the
4434              * grab to swallow the released too. */
4435             send = 0;
4436             terminate = 0;
4437             db->key[1] = key;
4438         } else {
4439             /* Terminate the grab since the key pressed is not a
4440              * debug binding key. */
4441             send = 1;
4442             terminate = 1;
4443         }
4444     }
4445
4446     if (send) {
4447         resource = grab->keyboard->focus_resource;
4448
4449         if (resource) {
4450             display = wl_client_get_display(wl_resource_get_client(resource));
4451             serial = wl_display_next_serial(display);
4452             wl_keyboard_send_key(resource, serial, time, key, state);
4453         }
4454     }
4455
4456     if (terminate) {
4457         weston_keyboard_end_grab(grab->keyboard);
4458         if (grab->keyboard->input_method_resource)
4459             grab->keyboard->grab = &grab->keyboard->input_method_grab;
4460         free(db);
4461     }
4462 }
4463
4464 static void
4465 debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
4466             uint32_t mods_depressed, uint32_t mods_latched,
4467             uint32_t mods_locked, uint32_t group)
4468 {
4469     struct wl_resource *resource;
4470
4471     resource = grab->keyboard->focus_resource;
4472     if (!resource)
4473         return;
4474
4475     wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4476                    mods_latched, mods_locked, group);
4477 }
4478
4479 struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
4480     debug_binding_key,
4481     debug_binding_modifiers
4482 };
4483
4484 static void
4485 debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
4486 {
4487     struct debug_binding_grab *grab;
4488
4489     grab = calloc(1, sizeof *grab);
4490     if (!grab)
4491         return;
4492
4493     grab->seat = (struct weston_seat *) seat;
4494     grab->key[0] = key;
4495     grab->grab.interface = &debug_binding_keyboard_grab;
4496     weston_keyboard_start_grab(seat->keyboard, &grab->grab);
4497 }
4498
4499 static void
4500 force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
4501            void *data)
4502 {
4503     struct weston_surface *focus_surface;
4504     struct wl_client *client;
4505     struct desktop_shell *shell = data;
4506     struct weston_compositor *compositor = shell->compositor;
4507     pid_t pid;
4508
4509     focus_surface = seat->keyboard->focus;
4510     if (!focus_surface)
4511         return;
4512
4513     wl_signal_emit(&compositor->kill_signal, focus_surface);
4514
4515     client = wl_resource_get_client(focus_surface->resource);
4516     wl_client_get_credentials(client, &pid, NULL, NULL);
4517
4518     /* Skip clients that we launched ourselves (the credentials of
4519      * the socketpair is ours) */
4520     if (pid == getpid())
4521         return;
4522
4523     kill(pid, SIGKILL);
4524 }
4525
4526 static void
4527 workspace_up_binding(struct weston_seat *seat, uint32_t time,
4528              uint32_t key, void *data)
4529 {
4530     struct desktop_shell *shell = data;
4531     unsigned int new_index = shell->workspaces.current;
4532
4533     if (shell->locked)
4534         return;
4535     if (new_index != 0)
4536         new_index--;
4537
4538     change_workspace(shell, new_index);
4539 }
4540
4541 static void
4542 workspace_down_binding(struct weston_seat *seat, uint32_t time,
4543                uint32_t key, void *data)
4544 {
4545     struct desktop_shell *shell = data;
4546     unsigned int new_index = shell->workspaces.current;
4547
4548     if (shell->locked)
4549         return;
4550     if (new_index < shell->workspaces.num - 1)
4551         new_index++;
4552
4553     change_workspace(shell, new_index);
4554 }
4555
4556 static void
4557 workspace_f_binding(struct weston_seat *seat, uint32_t time,
4558             uint32_t key, void *data)
4559 {
4560     struct desktop_shell *shell = data;
4561     unsigned int new_index;
4562
4563     if (shell->locked)
4564         return;
4565     new_index = key - KEY_F1;
4566     if (new_index >= shell->workspaces.num)
4567         new_index = shell->workspaces.num - 1;
4568
4569     change_workspace(shell, new_index);
4570 }
4571
4572 static void
4573 workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
4574                   uint32_t key, void *data)
4575 {
4576     struct desktop_shell *shell = data;
4577     unsigned int new_index = shell->workspaces.current;
4578
4579     if (shell->locked)
4580         return;
4581
4582     if (new_index != 0)
4583         new_index--;
4584
4585     take_surface_to_workspace_by_seat(shell, seat, new_index);
4586 }
4587
4588 static void
4589 workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
4590                     uint32_t key, void *data)
4591 {
4592     struct desktop_shell *shell = data;
4593     unsigned int new_index = shell->workspaces.current;
4594
4595     if (shell->locked)
4596         return;
4597
4598     if (new_index < shell->workspaces.num - 1)
4599         new_index++;
4600
4601     take_surface_to_workspace_by_seat(shell, seat, new_index);
4602 }
4603
4604 static void
4605 shell_destroy(struct wl_listener *listener, void *data)
4606 {
4607     struct desktop_shell *shell =
4608         container_of(listener, struct desktop_shell, destroy_listener);
4609     struct workspace **ws;
4610
4611     if (shell->child.client)
4612         wl_client_destroy(shell->child.client);
4613
4614     wl_list_remove(&shell->idle_listener.link);
4615     wl_list_remove(&shell->wake_listener.link);
4616     wl_list_remove(&shell->show_input_panel_listener.link);
4617     wl_list_remove(&shell->hide_input_panel_listener.link);
4618
4619     wl_array_for_each(ws, &shell->workspaces.array)
4620         workspace_destroy(*ws);
4621     wl_array_release(&shell->workspaces.array);
4622
4623     free(shell->screensaver.path);
4624     free(shell);
4625 }
4626
4627 static void
4628 shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4629 {
4630     uint32_t mod;
4631     int i, num_workspace_bindings;
4632
4633     /* fixed bindings */
4634     weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4635                           MODIFIER_CTRL | MODIFIER_ALT,
4636                           terminate_binding, ec);
4637     weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4638                          click_to_activate_binding,
4639                          shell);
4640     weston_compositor_add_button_binding(ec, BTN_TOUCH, 0,
4641                          click_to_activate_binding,
4642                          shell);
4643     weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4644                            MODIFIER_SUPER | MODIFIER_ALT,
4645                            surface_opacity_binding, NULL);
4646     weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4647                        MODIFIER_SUPER, zoom_axis_binding,
4648                        NULL);
4649
4650     /* configurable bindings */
4651     mod = shell->binding_modifier;
4652     weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4653                       zoom_key_binding, NULL);
4654     weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4655                       zoom_key_binding, NULL);
4656     weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4657                          shell);
4658     weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4659                          resize_binding, shell);
4660
4661     if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
4662         weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4663                              rotate_binding, NULL);
4664
4665     weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4666                       shell);
4667     weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4668                       ec);
4669     weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4670                           backlight_binding, ec);
4671     weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4672                       ec);
4673     weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4674                           backlight_binding, ec);
4675     weston_compositor_add_key_binding(ec, KEY_K, mod,
4676                           force_kill_binding, shell);
4677     weston_compositor_add_key_binding(ec, KEY_UP, mod,
4678                       workspace_up_binding, shell);
4679     weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4680                       workspace_down_binding, shell);
4681     weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4682                       workspace_move_surface_up_binding,
4683                       shell);
4684     weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4685                       workspace_move_surface_down_binding,
4686                       shell);
4687
4688     /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4689     if (shell->workspaces.num > 1) {
4690         num_workspace_bindings = shell->workspaces.num;
4691         if (num_workspace_bindings > 6)
4692             num_workspace_bindings = 6;
4693         for (i = 0; i < num_workspace_bindings; i++)
4694             weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4695                               workspace_f_binding,
4696                               shell);
4697     }
4698
4699     /* Debug bindings */
4700     weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4701                       debug_binding, shell);
4702 }
4703
4704 WL_EXPORT int
4705 module_init(struct weston_compositor *ec,
4706         int *argc, char *argv[])
4707 {
4708     struct weston_seat *seat;
4709     struct desktop_shell *shell;
4710     struct workspace **pws;
4711     unsigned int i;
4712     struct wl_event_loop *loop;
4713
4714     shell = malloc(sizeof *shell);
4715     if (shell == NULL)
4716         return -1;
4717
4718     /* save shell management table for other plugin */
4719     _ico_ivi_shell = shell;
4720
4721     memset(shell, 0, sizeof *shell);
4722     shell->compositor = ec;
4723
4724     shell->destroy_listener.notify = shell_destroy;
4725     wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
4726     shell->idle_listener.notify = idle_handler;
4727     wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4728     shell->wake_listener.notify = wake_handler;
4729     wl_signal_add(&ec->wake_signal, &shell->wake_listener);
4730     shell->show_input_panel_listener.notify = show_input_panels;
4731     wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4732     shell->hide_input_panel_listener.notify = hide_input_panels;
4733     wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
4734     shell->update_input_panel_listener.notify = update_input_panels;
4735     wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
4736     ec->ping_handler = ping_handler;
4737     ec->shell_interface.shell = shell;
4738     ec->shell_interface.create_shell_surface = create_shell_surface;
4739     ec->shell_interface.set_toplevel = set_toplevel;
4740     ec->shell_interface.set_transient = set_transient;
4741     ec->shell_interface.set_fullscreen = set_fullscreen;
4742     ec->shell_interface.set_xwayland = set_xwayland;
4743     ec->shell_interface.move = surface_move;
4744     ec->shell_interface.resize = surface_resize;
4745
4746     wl_list_init(&shell->input_panel.surfaces);
4747
4748     weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4749     weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
4750     weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4751     weston_layer_init(&shell->lock_layer, NULL);
4752     weston_layer_init(&shell->input_panel_layer, NULL);
4753
4754     wl_array_init(&shell->workspaces.array);
4755     wl_list_init(&shell->workspaces.client_list);
4756
4757     shell_configuration(shell);
4758
4759     for (i = 0; i < shell->workspaces.num; i++) {
4760         pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4761         if (pws == NULL)
4762             return -1;
4763
4764         *pws = workspace_create();
4765         if (*pws == NULL)
4766             return -1;
4767     }
4768     activate_workspace(shell, 0);
4769
4770     wl_list_init(&shell->workspaces.anim_sticky_list);
4771     wl_list_init(&shell->workspaces.animation.link);
4772     shell->workspaces.animation.frame = animate_workspace_change_frame;
4773
4774     if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
4775                   shell, bind_shell) == NULL)
4776         return -1;
4777
4778     if (wl_global_create(ec->wl_display,
4779                  &desktop_shell_interface, 2,
4780                  shell, bind_desktop_shell) == NULL)
4781         return -1;
4782
4783     if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
4784                  shell, bind_screensaver) == NULL)
4785         return -1;
4786
4787     if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
4788                   shell, bind_input_panel) == NULL)
4789         return -1;
4790
4791     if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
4792                  shell, bind_workspace_manager) == NULL)
4793         return -1;
4794
4795     shell->child.deathstamp = weston_compositor_get_time();
4796
4797     loop = wl_display_get_event_loop(ec->wl_display);
4798     wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
4799
4800     shell->screensaver.timer =
4801         wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4802
4803     wl_list_for_each(seat, &ec->seat_list, link)
4804         create_pointer_focus_listener(seat);
4805
4806     shell_add_bindings(ec, shell);
4807
4808     shell_fade_init(shell);
4809
4810     return 0;
4811 }
4812
4813 /*--------------------------------------------------------------------------*/
4814 /**
4815  * @brief   ico_ivi_shell_current_layer: get current weston layer
4816  *
4817  * @param       none
4818  * @return      current weston layer
4819  * @retval      !=NULL      success(current weston layer)
4820  * @retval      ==NULL      error(no layer)
4821  */
4822 /*--------------------------------------------------------------------------*/
4823 /* API for other plugin         */
4824 WL_EXPORT struct weston_layer *
4825 ico_ivi_shell_current_layer(void)
4826 {
4827     struct workspace *ws = get_current_workspace(_ico_ivi_shell);
4828
4829     return (ws != NULL) ? &ws->layer : NULL;
4830 }
4831
4832 /*--------------------------------------------------------------------------*/
4833 /**
4834  * @brief   ico_ivi_shell_set_toplevel: set surface to TopLevel
4835  *
4836  * @param[in]   shsurf          shell surface
4837  * @return      none
4838  */
4839 /*--------------------------------------------------------------------------*/
4840 WL_EXPORT void
4841 ico_ivi_shell_set_toplevel(struct shell_surface *shsurf)
4842 {
4843     set_toplevel(shsurf);
4844 }
4845
4846 /*--------------------------------------------------------------------------*/
4847 /**
4848  * @brief   ico_ivi_shell_set_surface_type: set surface type
4849  *
4850  * @param[in]   shsurf      shell surface
4851  * @return      none
4852  */
4853 /*--------------------------------------------------------------------------*/
4854 WL_EXPORT void
4855 ico_ivi_shell_set_surface_type(struct shell_surface *shsurf)
4856 {
4857     set_surface_type(shsurf);
4858 }
4859
4860 /*--------------------------------------------------------------------------*/
4861 /**
4862  * @brief   ico_ivi_shell_send_configure: send configure event to client application
4863  *
4864  * @param[in]   surface     weston surface
4865  * @param[in]   edges       surface resize position
4866  * @param[in]   width       surface width
4867  * @param[in]   height      surface height
4868  * @return      none
4869  */
4870 /*--------------------------------------------------------------------------*/
4871 WL_EXPORT void
4872 ico_ivi_shell_send_configure(struct weston_surface *surface,
4873                              const uint32_t edges, const int width, const int height)
4874 {
4875     send_configure(surface, edges, width, height);
4876 }
4877
4878 /*--------------------------------------------------------------------------*/
4879 /**
4880  * @brief   ico_ivi_shell_startup: start shell surface fade
4881  *
4882  * @param[in]   shell          shell table address
4883  * @return      none
4884  */
4885 /*--------------------------------------------------------------------------*/
4886 WL_EXPORT void
4887 ico_ivi_shell_startup(void *shell)
4888 {
4889     shell_fade_startup((struct desktop_shell *)shell);
4890 }
4891
4892 /*--------------------------------------------------------------------------*/
4893 /**
4894  * @brief   ico_ivi_shell_hook_bind: regist hook function for shell bind
4895  *
4896  * @param[in]   hook_bind       hook function(if NULL, reset hook function)
4897  * @return      none
4898  */
4899 /*--------------------------------------------------------------------------*/
4900 WL_EXPORT void
4901 ico_ivi_shell_hook_bind(void (*hook_bind)(struct wl_client *client, void *shell))
4902 {
4903     shell_hook_bind = hook_bind;
4904 }
4905
4906 /*--------------------------------------------------------------------------*/
4907 /**
4908  * @brief   ico_ivi_shell_hook_unbind: regist hook function for shell unbind
4909  *
4910  * @param[in]   hook_unbind     hook function(if NULL, reset hook function)
4911  * @return      none
4912  */
4913 /*--------------------------------------------------------------------------*/
4914 WL_EXPORT void
4915 ico_ivi_shell_hook_unbind(void (*hook_unbind)(struct wl_client *client))
4916 {
4917     shell_hook_unbind = hook_unbind;
4918 }
4919
4920 /*--------------------------------------------------------------------------*/
4921 /**
4922  * @brief   ico_ivi_shell_hook_create: regist hook function for create shell surface
4923  *
4924  * @param[in]   hook_create     hook function(if NULL, reset hook function)
4925  * @return      none
4926  */
4927 /*--------------------------------------------------------------------------*/
4928 WL_EXPORT void
4929 ico_ivi_shell_hook_create(void (*hook_create)(struct wl_client *client,
4930                                               struct wl_resource *resource,
4931                                               struct weston_surface *surface,
4932                                               struct shell_surface *shsurf))
4933 {
4934     shell_hook_create = hook_create;
4935 }
4936
4937 /*--------------------------------------------------------------------------*/
4938 /**
4939  * @brief   ico_ivi_shell_hook_destroy: regist hook function for destroy shell surface
4940  *
4941  * @param[in]   hook_destroy    hook function(if NULL, reset hook function)
4942  * @return      none
4943  */
4944 /*--------------------------------------------------------------------------*/
4945 WL_EXPORT void
4946 ico_ivi_shell_hook_destroy(void (*hook_destroy)(struct weston_surface *surface))
4947 {
4948     shell_hook_destroy = hook_destroy;
4949 }
4950
4951 /*--------------------------------------------------------------------------*/
4952 /**
4953  * @brief   ico_ivi_shell_hook_map: regist hook function for map shell surface
4954  *
4955  * @param[in]   hook_map        hook function(if NULL, reset hook function)
4956  * @return      none
4957  */
4958 /*--------------------------------------------------------------------------*/
4959 WL_EXPORT void
4960 ico_ivi_shell_hook_map(void (*hook_map)(struct weston_surface *surface,
4961                                         int32_t *width, int32_t *height,
4962                                         int32_t *sx, int32_t *sy))
4963 {
4964     shell_hook_map = hook_map;
4965 }
4966
4967 /*--------------------------------------------------------------------------*/
4968 /**
4969  * @brief   ico_ivi_shell_hook_configure: regist hook function for configure shell surface
4970  *
4971  * @param[in]   hook_configure  hook function(if NULL, reset hook function)
4972  * @return      none
4973  */
4974 /*--------------------------------------------------------------------------*/
4975 WL_EXPORT void
4976 ico_ivi_shell_hook_configure(void (*hook_configure)(struct weston_surface *surface))
4977 {
4978     shell_hook_configure = hook_configure;
4979 }
4980
4981 /*--------------------------------------------------------------------------*/
4982 /**
4983  * @brief   ico_ivi_shell_hook_select: regist hook function for select(active) shell surface
4984  *
4985  * @param[in]   hook_select     hook function(if NULL, reset hook function)
4986  * @return      none
4987  */
4988 /*--------------------------------------------------------------------------*/
4989 WL_EXPORT void
4990 ico_ivi_shell_hook_select(void (*hook_select)(struct weston_surface *surface))
4991 {
4992     shell_hook_select = hook_select;
4993 }
4994
4995 /*--------------------------------------------------------------------------*/
4996 /**
4997  * @brief   ico_ivi_shell_hook_title: regist hook function for select(active) shell surface
4998  *
4999  * @param[in]   hook_title     hook function(if NULL, reset hook function)
5000  * @return      none
5001  */
5002 /*--------------------------------------------------------------------------*/
5003 WL_EXPORT void
5004 ico_ivi_shell_hook_title(void (*hook_title)(struct weston_surface *surface,
5005                                             const char *title))
5006 {
5007     shell_hook_title = hook_title;
5008 }
5009
5010 /*--------------------------------------------------------------------------*/
5011 /**
5012  * @brief   ico_ivi_shell_hook_move: regist hook function for move grab
5013  *
5014  * @param[in]       hook_move   hook function(if NULL, reset hook function)
5015  * @param[in/out]   dx          new X coordinate
5016  * @param[in/out]   dy          new Y coordinate
5017  * @return          none
5018  */
5019 /*--------------------------------------------------------------------------*/
5020 WL_EXPORT void
5021 ico_ivi_shell_hook_move(void (*hook_move)(struct weston_surface *surface, int *dx, int *dy))
5022 {
5023     shell_hook_move = hook_move;
5024 }
5025