packaging: bump version to 0.2.5 and require weston-ivi-shell 0.1.9
[profile/ivi/wayland-ivi-extension.git] / weston-ivi-shell / src / ivi-controller.c
1 /*
2  * Copyright (C) 2013 DENSO CORPORATION
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #include <sys/wait.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <linux/input.h>
29 #include <cairo.h>
30 #include <GLES2/gl2.h>
31
32 #include <wayland-server.h>
33
34 #include <weston/compositor.h>
35 #include <weston/ivi-shell.h>
36 #include <weston/ivi-shell-ext.h>
37 #include <weston/ivi-layout-export.h>
38 #include "ivi-controller-server-protocol.h"
39 #include "bitmap.h"
40
41 struct ivishell;
42 struct ivilayer;
43 struct iviscreen;
44
45 struct link_layer {
46     struct ivilayer *layer;
47     struct wl_list link;
48 };
49
50 struct link_screen {
51     struct iviscreen *screen;
52     struct wl_list link;
53 };
54
55 struct ivisurface {
56     struct wl_list link;
57     struct wl_client *client;
58     struct ivishell *shell;
59     uint32_t update_count;
60     struct ivi_layout_surface *layout_surface;
61     struct wl_listener surface_destroy_listener;
62     struct wl_list list_layer;
63     uint32_t controller_surface_count;
64     int can_be_removed;
65 };
66
67 struct ivilayer {
68     struct wl_list link;
69     struct ivishell *shell;
70     struct ivi_layout_layer *layout_layer;
71     struct wl_list list_screen;
72     uint32_t controller_layer_count;
73     int layer_canbe_removed;
74 };
75
76 struct iviscreen {
77     struct wl_list link;
78     struct ivishell *shell;
79     struct ivi_layout_screen *layout_screen;
80     struct weston_output *output;
81 };
82
83 struct ivicontroller_surface {
84     struct wl_resource *resource;
85     uint32_t id;
86     uint32_t id_surface;
87     struct wl_client *client;
88     struct wl_list link;
89     struct ivishell *shell;
90     int implementation_set;
91 };
92
93 struct ivicontroller_layer {
94     struct wl_resource *resource;
95     uint32_t id;
96     uint32_t id_layer;
97     struct wl_client *client;
98     struct wl_list link;
99     struct ivishell *shell;
100 };
101
102 struct ivicontroller_screen {
103     struct wl_resource *resource;
104     uint32_t id;
105     uint32_t id_screen;
106     struct wl_client *client;
107     struct wl_list link;
108     struct ivishell *shell;
109 };
110
111 struct ivicontroller {
112     struct wl_resource *resource;
113     uint32_t id;
114     struct wl_client *client;
115     struct wl_list link;
116     struct ivishell *shell;
117 };
118
119 struct link_shell_weston_surface
120 {
121     struct wl_resource *resource;
122     struct wl_listener destroy_listener;
123     struct weston_surface *surface;
124     struct wl_list link;
125 };
126
127 struct ivishell {
128     struct wl_resource *resource;
129
130     struct wl_listener destroy_listener;
131
132     struct weston_compositor *compositor;
133
134     struct weston_surface *surface;
135
136     struct weston_process process;
137
138     struct weston_seat *seat;
139
140     struct wl_list list_surface;
141     struct wl_list list_layer;
142     struct wl_list list_screen;
143
144     struct wl_list list_weston_surface;
145
146     struct wl_list list_controller;
147     struct wl_list list_controller_surface;
148     struct wl_list list_controller_layer;
149     struct wl_list list_controller_screen;
150
151     struct {
152         struct weston_process process;
153         struct wl_client *client;
154         struct wl_resource *desktop_shell;
155
156         unsigned deathcount;
157         uint32_t deathstamp;
158     } child;
159
160     int state;
161     int previous_state;
162     int event_restriction;
163 };
164 static void surface_event_remove(struct ivi_layout_surface *, void *);
165
166 static struct ivi_layout_layer_properties no_layer_props;
167 static struct ivi_layout_surface_properties no_surface_props;
168
169
170
171 static void
172 destroy_ivicontroller_surface(struct wl_resource *resource)
173 {
174     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
175     struct ivishell *shell = ivisurf->shell;
176     struct ivicontroller_surface *ctrlsurf = NULL;
177     struct ivicontroller_surface *next = NULL;
178     int is_removed = 0;
179
180     wl_list_for_each_safe(ctrlsurf, next,
181                           &shell->list_controller_surface, link) {
182
183         if (resource != ctrlsurf->resource) {
184             continue;
185         }
186
187         if (!wl_list_empty(&ctrlsurf->link)) {
188             wl_list_remove(&ctrlsurf->link);
189         }
190
191         is_removed = 1;
192         free(ctrlsurf);
193         ctrlsurf = NULL;
194         --ivisurf->controller_surface_count;
195         break;
196     }
197
198     if ((is_removed) && (ivisurf->controller_surface_count == 0)) {
199         if (ivisurf->can_be_removed) {
200             free(ivisurf);
201         }
202     }
203 }
204
205 static void
206 destroy_ivicontroller_layer(struct wl_resource *resource)
207 {
208     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
209     struct ivishell *shell = ivilayer->shell;
210     struct ivicontroller_layer *ctrllayer = NULL;
211     struct ivicontroller_layer *next = NULL;
212     uint32_t id_layer = 0;
213
214     id_layer = ivi_layout_get_id_of_layer(ivilayer->layout_layer);
215
216     wl_list_for_each_safe(ctrllayer, next,
217                           &shell->list_controller_layer, link) {
218
219         if (resource != ctrllayer->resource) {
220             continue;
221         }
222
223         wl_list_remove(&ctrllayer->link);
224         --ivilayer->controller_layer_count;
225         ivi_controller_layer_send_destroyed(ctrllayer->resource);
226         free(ctrllayer);
227         ctrllayer = NULL;
228         break;
229     }
230
231     if ((ivilayer->layout_layer != NULL) &&
232         (ivilayer->controller_layer_count == 0) &&
233         (ivilayer->layer_canbe_removed == 1)) {
234         ivi_layout_layer_remove(ivilayer->layout_layer);
235     }
236 }
237
238 static void
239 destroy_ivicontroller_screen(struct wl_resource *resource)
240 {
241     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
242     struct ivicontroller_screen *ctrlscrn = NULL;
243     struct ivicontroller_screen *next = NULL;
244
245     wl_list_for_each_safe(ctrlscrn, next,
246                           &iviscrn->shell->list_controller_screen, link) {
247 // TODO : Only Single display
248 #if 0
249         if (iviscrn->output->id != ctrlscrn->id_screen) {
250             continue;
251         }
252 #endif
253
254         if (resource != ctrlscrn->resource) {
255             continue;
256         }
257
258         wl_list_remove(&ctrlscrn->link);
259         free(ctrlscrn);
260         ctrlscrn = NULL;
261         break;
262     }
263 }
264
265 static void
266 unbind_resource_controller(struct wl_resource *resource)
267 {
268     struct ivicontroller *controller = wl_resource_get_user_data(resource);
269
270     wl_list_remove(&controller->link);
271
272     free(controller);
273     controller = NULL;
274 }
275
276 static struct ivisurface*
277 get_surface(struct wl_list *list_surf, uint32_t id_surface)
278 {
279     struct ivisurface *ivisurf = NULL;
280     uint32_t ivisurf_id = 0;
281
282     wl_list_for_each(ivisurf, list_surf, link) {
283         ivisurf_id = ivi_layout_get_id_of_surface(ivisurf->layout_surface);
284         if (ivisurf_id == id_surface) {
285             return ivisurf;
286         }
287     }
288
289     return NULL;
290 }
291
292 static struct ivilayer*
293 get_layer(struct wl_list *list_layer, uint32_t id_layer)
294 {
295     struct ivilayer *ivilayer = NULL;
296     uint32_t ivilayer_id = 0;
297
298     wl_list_for_each(ivilayer, list_layer, link) {
299         ivilayer_id = ivi_layout_get_id_of_layer(ivilayer->layout_layer);
300         if (ivilayer_id == id_layer) {
301             return ivilayer;
302         }
303     }
304
305     return NULL;
306 }
307
308 static const
309 struct ivi_controller_screen_interface controller_screen_implementation;
310
311 static struct ivicontroller_screen*
312 controller_screen_create(struct ivishell *shell,
313                          struct wl_client *client,
314                          struct iviscreen *iviscrn)
315 {
316     struct ivicontroller_screen *ctrlscrn = NULL;
317
318     ctrlscrn = calloc(1, sizeof *ctrlscrn);
319     if (ctrlscrn == NULL) {
320         weston_log("no memory to allocate controller screen\n");
321         return NULL;
322     }
323
324     ctrlscrn->client = client;
325     ctrlscrn->shell  = shell;
326 // FIXME
327 // TODO : Only Single display
328 #if 0
329     /* ctrlscrn->id_screen = iviscrn->id_screen; */
330 #else
331     ctrlscrn->id_screen = 0;
332 #endif
333
334     ctrlscrn->resource =
335         wl_resource_create(client, &ivi_controller_screen_interface, 1, 0);
336     if (ctrlscrn->resource == NULL) {
337         weston_log("couldn't new screen controller object");
338
339         free(ctrlscrn);
340         ctrlscrn = NULL;
341
342         return NULL;
343     }
344
345     wl_resource_set_implementation(ctrlscrn->resource,
346                                    &controller_screen_implementation,
347                                    iviscrn, destroy_ivicontroller_screen);
348
349     wl_list_init(&ctrlscrn->link);
350     wl_list_insert(&shell->list_controller_screen, &ctrlscrn->link);
351
352     return ctrlscrn;
353 }
354
355 static void
356 send_surface_add_event(struct ivisurface *ivisurf,
357                        struct wl_resource *resource,
358                        enum ivi_layout_notification_mask mask)
359 {
360     struct ivi_layout_layer **pArray = NULL;
361     uint32_t length = 0;
362     int32_t ans = 0;
363     int i = 0;
364     struct link_layer *link_layer = NULL;
365     struct link_layer *next = NULL;
366     struct ivicontroller_layer *ctrllayer = NULL;
367     struct ivilayer *ivilayer = NULL;
368     struct ivishell *shell = ivisurf->shell;
369     uint32_t id_layout_layer = 0;
370     struct wl_client *surface_client = wl_resource_get_client(resource);
371     int found = 0;
372
373     ans = ivi_layout_get_layers_under_surface(ivisurf->layout_surface,
374                                               &length, &pArray);
375     if (0 != ans) {
376         weston_log("failed to get layers at send_surface_add_event\n");
377         return;
378     }
379
380     /* Send Null to cancel added surface */
381     if (mask & IVI_NOTIFICATION_REMOVE) {
382         wl_list_for_each_safe(link_layer, next, &ivisurf->list_layer, link) {
383             ivi_controller_surface_send_layer(resource, NULL);
384         }
385     }
386     else if (mask & IVI_NOTIFICATION_ADD) {
387         for (i = 0; i < (int)length; i++) {
388             /* Send new surface event */
389             ivilayer = NULL;
390             wl_list_for_each(ivilayer, &shell->list_layer, link) {
391                 if (ivilayer->layout_layer == pArray[i]) {
392                     break;
393                 }
394             }
395
396             if (ivilayer == NULL) {
397                 continue;
398             }
399
400             id_layout_layer =
401                 ivi_layout_get_id_of_layer(ivilayer->layout_layer);
402             wl_list_for_each(ctrllayer, &shell->list_controller_layer, link) {
403                 if (id_layout_layer != ctrllayer->id_layer) {
404                     continue;
405                 }
406
407                 struct wl_client *layer_client = wl_resource_get_client(ctrllayer->resource);
408                 if (surface_client != layer_client) {
409                     continue;
410                 }
411
412                 ivi_controller_surface_send_layer(resource, ctrllayer->resource);
413             }
414         }
415     }
416
417     free(pArray);
418     pArray = NULL;
419 }
420
421 static void
422 send_surface_event(struct wl_resource *resource,
423                    struct ivisurface *ivisurf,
424                    struct ivi_layout_surface_properties *prop,
425                    uint32_t mask)
426 {
427     if (mask & IVI_NOTIFICATION_OPACITY) {
428         ivi_controller_surface_send_opacity(resource,
429                                    wl_fixed_from_double(prop->opacity));
430     }
431     if (mask & IVI_NOTIFICATION_SOURCE_RECT) {
432         ivi_controller_surface_send_source_rectangle(resource,
433             prop->source_x, prop->source_y,
434             prop->source_width, prop->source_height);
435     }
436     if (mask & IVI_NOTIFICATION_DEST_RECT) {
437         ivi_controller_surface_send_destination_rectangle(resource,
438             prop->dest_x, prop->dest_y,
439             prop->dest_width, prop->dest_height);
440     }
441     if (mask & IVI_NOTIFICATION_ORIENTATION) {
442         ivi_controller_surface_send_orientation(resource,
443                                                 prop->orientation);
444     }
445     if (mask & IVI_NOTIFICATION_VISIBILITY) {
446         ivi_controller_surface_send_visibility(resource,
447                                                prop->visibility);
448     }
449 #ifdef SUPPORT_PIXELFORMAT_PROPERTY
450     if (mask & IVI_NOTIFICATION_PIXELFORMAT) {
451         ivi_controller_surface_send_pixelformat(resource,
452                                                 prop->pixelformat);
453     }
454 #endif
455     if (mask & IVI_NOTIFICATION_KEYBOARD_FOCUS) {
456         ivi_controller_surface_send_input_focus(resource,
457                         IVI_CONTROLLER_SURFACE_INPUT_DEVICE_KEYBOARD,
458                         prop->has_keyboard_focus);
459     }
460     if (mask & IVI_NOTIFICATION_REMOVE) {
461         send_surface_add_event(ivisurf, resource, IVI_NOTIFICATION_REMOVE);
462     }
463     if (mask & IVI_NOTIFICATION_ADD) {
464         send_surface_add_event(ivisurf, resource, IVI_NOTIFICATION_ADD);
465     }
466 }
467
468 static void
469 update_surface_prop(struct ivisurface *ivisurf,
470                     uint32_t mask)
471 {
472     struct ivi_layout_layer **pArray = NULL;
473     uint32_t length = 0;
474     int32_t ans = 0;
475     int i = 0;
476     struct ivishell *shell = ivisurf->shell;
477
478     ans = ivi_layout_get_layers_under_surface(ivisurf->layout_surface,
479                                               &length, &pArray);
480     if (0 != ans) {
481         weston_log("failed to get layers at send_surface_add_event\n");
482         return;
483     }
484
485     if (mask & IVI_NOTIFICATION_REMOVE) {
486         struct link_layer *link_layer = NULL;
487         struct link_layer *next = NULL;
488
489         wl_list_for_each_safe(link_layer, next, &ivisurf->list_layer, link) {
490             wl_list_remove(&link_layer->link);
491             free(link_layer);
492             link_layer = NULL;
493         }
494     }
495     if (mask & IVI_NOTIFICATION_ADD) {
496         for (i = 0; i < (int)length; ++i) {
497             /* Create list_layer */
498             struct ivilayer *ivilayer = NULL;
499             struct link_layer *link_layer = calloc(1, sizeof(*link_layer));
500             if (NULL == link_layer) {
501                 continue;
502             }
503             wl_list_init(&link_layer->link);
504             link_layer->layer = NULL;
505             wl_list_for_each(ivilayer, &shell->list_layer, link) {
506                 if (ivilayer->layout_layer == pArray[i]) {
507                     link_layer->layer = ivilayer;
508                     break;
509                 }
510             }
511
512             if (link_layer->layer == NULL) {
513                 free(link_layer);
514                 link_layer = NULL;
515                 continue;
516             }
517
518             wl_list_insert(&ivisurf->list_layer, &link_layer->link);
519         }
520     }
521 }
522
523 static void
524 send_surface_prop(struct ivi_layout_surface *layout_surface,
525                   struct ivi_layout_surface_properties *prop,
526                   enum ivi_layout_notification_mask mask,
527                   void *userdata)
528 {
529     struct ivisurface *ivisurf = userdata;
530     struct ivishell *shell = ivisurf->shell;
531     struct ivicontroller_surface *ctrlsurf = NULL;
532     uint32_t id_surface = 0;
533
534     id_surface = ivi_layout_get_id_of_surface(layout_surface);
535
536     wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
537         if (id_surface != ctrlsurf->id_surface) {
538             continue;
539         }
540         send_surface_event(ctrlsurf->resource, ivisurf, prop, mask);
541     }
542
543     update_surface_prop(ivisurf, mask);
544 }
545
546 static void
547 send_layer_add_event(struct ivilayer *ivilayer,
548                      struct wl_resource *resource,
549                      enum ivi_layout_notification_mask mask)
550 {
551     struct ivi_layout_screen **pArray = NULL;
552     uint32_t length = 0;
553     int32_t ans = 0;
554     int i = 0;
555     struct link_screen *link_scrn = NULL;
556     struct link_screen *next = NULL;
557     struct iviscreen *iviscrn = NULL;
558     struct ivishell *shell = ivilayer->shell;
559     struct wl_client *client = wl_resource_get_client(resource);
560     struct wl_resource *resource_output = NULL;
561
562     ans = ivi_layout_get_screens_under_layer(ivilayer->layout_layer,
563                                              &length, &pArray);
564     if (0 != ans) {
565         weston_log("failed to get screens at send_layer_add_event\n");
566         return;
567     }
568
569     /* Send Null to cancel added layer */
570     if (mask & IVI_NOTIFICATION_REMOVE) {
571         wl_list_for_each_safe(link_scrn, next, &ivilayer->list_screen, link) {
572             ivi_controller_layer_send_screen(resource, NULL);
573         }
574     }
575     else if (mask & IVI_NOTIFICATION_ADD) {
576         for (i = 0; i < (int)length; i++) {
577             /* Send new layer event */
578             iviscrn = NULL;
579             wl_list_for_each(iviscrn, &shell->list_screen, link) {
580                 if (iviscrn->layout_screen == pArray[i]) {
581                     break;
582                 }
583             }
584
585             if (iviscrn == NULL) {
586                 continue;
587             }
588
589             resource_output =
590                 wl_resource_find_for_client(&iviscrn->output->resource_list,
591                                      client);
592             if (resource_output != NULL) {
593                 ivi_controller_layer_send_screen(resource, resource_output);
594             }
595         }
596     }
597
598     free(pArray);
599     pArray = NULL;
600 }
601
602 static void
603 send_layer_event(struct wl_resource *resource,
604                  struct ivilayer *ivilayer,
605                  struct ivi_layout_layer_properties *prop,
606                  uint32_t mask)
607 {
608     if (mask & IVI_NOTIFICATION_OPACITY) {
609         ivi_controller_layer_send_opacity(resource,
610                                 wl_fixed_from_double(prop->opacity));
611     }
612     if (mask & IVI_NOTIFICATION_SOURCE_RECT) {
613         ivi_controller_layer_send_source_rectangle(resource,
614                                           prop->source_x,
615                                           prop->source_y,
616                                           prop->source_width,
617                                           prop->source_height);
618     }
619     if (mask & IVI_NOTIFICATION_DEST_RECT) {
620         ivi_controller_layer_send_destination_rectangle(resource,
621                                           prop->dest_x,
622                                           prop->dest_y,
623                                           prop->dest_width,
624                                           prop->dest_height);
625     }
626     if (mask & IVI_NOTIFICATION_ORIENTATION) {
627         ivi_controller_layer_send_orientation(resource,
628                                           prop->orientation);
629     }
630     if (mask & IVI_NOTIFICATION_VISIBILITY) {
631         ivi_controller_layer_send_visibility(resource,
632                                           prop->visibility);
633     }
634     if (mask & IVI_NOTIFICATION_REMOVE) {
635         send_layer_add_event(ivilayer, resource, IVI_NOTIFICATION_REMOVE);
636     }
637     if (mask & IVI_NOTIFICATION_ADD) {
638         send_layer_add_event(ivilayer, resource, IVI_NOTIFICATION_ADD);
639     }
640 }
641
642 static void
643 update_layer_prop(struct ivilayer *ivilayer,
644                   enum ivi_layout_notification_mask mask)
645 {
646     struct ivi_layout_screen **pArray = NULL;
647     uint32_t length = 0;
648     int32_t ans = 0;
649     struct link_screen *link_scrn = NULL;
650     struct link_screen *next = NULL;
651
652     ans = ivi_layout_get_screens_under_layer(ivilayer->layout_layer,
653                                              &length, &pArray);
654     if (0 != ans) {
655         weston_log("failed to get screens at send_layer_add_event\n");
656         return;
657     }
658
659     /* Send Null to cancel added layer */
660     if (mask & IVI_NOTIFICATION_REMOVE) {
661         wl_list_for_each_safe(link_scrn, next, &ivilayer->list_screen, link) {
662             wl_list_remove(&link_scrn->link);
663             free(link_scrn);
664             link_scrn = NULL;
665         }
666     }
667     if (mask & IVI_NOTIFICATION_ADD) {
668         int i = 0;
669         for (i = 0; i < (int)length; i++) {
670             struct ivishell *shell = ivilayer->shell;
671             struct iviscreen *iviscrn = NULL;
672             /* Create list_screen */
673             link_scrn = calloc(1, sizeof(*link_scrn));
674             if (NULL == link_scrn) {
675                 continue;
676             }
677             wl_list_init(&link_scrn->link);
678             link_scrn->screen = NULL;
679             wl_list_for_each(iviscrn, &shell->list_screen, link) {
680                 if (iviscrn->layout_screen == pArray[i]) {
681                     link_scrn->screen = iviscrn;
682                     break;
683                 }
684             }
685
686             if (link_scrn->screen == NULL) {
687                 free(link_scrn);
688                 link_scrn = NULL;
689                 continue;
690             }
691             wl_list_insert(&ivilayer->list_screen, &link_scrn->link);
692         }
693     }
694
695     free(pArray);
696     pArray = NULL;
697 }
698
699 static void
700 send_layer_prop(struct ivi_layout_layer *layer,
701                 const struct ivi_layout_layer_properties *prop,
702                 enum ivi_layout_notification_mask mask,
703                 void *userdata)
704 {
705     struct ivilayer *ivilayer = userdata;
706     struct ivicontroller_layer *ctrllayer = NULL;
707     struct ivishell *shell = ivilayer->shell;
708     uint32_t id_layout_layer = 0;
709
710     id_layout_layer = ivi_layout_get_id_of_layer(layer);
711     wl_list_for_each(ctrllayer, &shell->list_controller_layer, link) {
712         if (id_layout_layer != ctrllayer->id_layer) {
713             continue;
714         }
715         send_layer_event(ctrllayer->resource, ivilayer, prop, mask);
716     }
717
718     update_layer_prop(ivilayer, mask);
719 }
720
721 static void
722 controller_surface_set_opacity(struct wl_client *client,
723                    struct wl_resource *resource,
724                    wl_fixed_t opacity)
725 {
726     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
727     (void)client;
728     ivi_layout_surface_set_opacity(ivisurf->layout_surface,
729                                  wl_fixed_to_double(opacity));
730 }
731
732 static void
733 controller_surface_set_source_rectangle(struct wl_client *client,
734                    struct wl_resource *resource,
735                    int32_t x,
736                    int32_t y,
737                    int32_t width,
738                    int32_t height)
739 {
740     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
741     (void)client;
742     ivi_layout_surface_set_source_rectangle(ivisurf->layout_surface,
743             (uint32_t)x, (uint32_t)y, (uint32_t)width, (uint32_t)height);
744 }
745
746 static void
747 controller_surface_set_destination_rectangle(struct wl_client *client,
748                      struct wl_resource *resource,
749                      int32_t x,
750                      int32_t y,
751                      int32_t width,
752                      int32_t height)
753 {
754     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
755     (void)client;
756
757     // TODO: create set transition type protocol
758     ivi_layout_surface_set_transition(ivisurf->layout_surface,
759                                       IVI_LAYOUT_TRANSITION_NONE,
760                                       300); // ms
761
762     ivi_layout_surface_set_destination_rectangle(ivisurf->layout_surface,
763             (uint32_t)x, (uint32_t)y, (uint32_t)width, (uint32_t)height);
764 }
765
766 static void
767 controller_surface_set_visibility(struct wl_client *client,
768                       struct wl_resource *resource,
769                       uint32_t visibility)
770 {
771     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
772     (void)client;
773     ivi_layout_surface_set_visibility(ivisurf->layout_surface, visibility);
774 }
775
776 static void
777 controller_surface_set_configuration(struct wl_client *client,
778                    struct wl_resource *resource,
779                    int32_t width, int32_t height)
780 {
781     /* This interface has been supported yet. */
782     (void)client;
783     (void)resource;
784     (void)width;
785     (void)height;
786 }
787
788 static void
789 controller_surface_set_orientation(struct wl_client *client,
790                    struct wl_resource *resource,
791                    int32_t orientation)
792 {
793     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
794     (void)client;
795     ivi_layout_surface_set_orientation(ivisurf->layout_surface, (uint32_t)orientation);
796 }
797
798 static int
799 shm_surface_screenshot(struct weston_surface *surface,
800                        int32_t width,
801                        int32_t height,
802                        int32_t stride,
803                        const char *filename)
804 {
805     struct weston_buffer *weston_buffer = NULL;
806     struct wl_shm_buffer *shm_buffer = NULL;
807     cairo_surface_t *cairo_surf = NULL;
808     uint8_t *source_buffer = NULL;
809     uint8_t *dest_buffer = NULL;
810
811     weston_buffer = surface->buffer_ref.buffer;
812     if (weston_buffer == NULL) {
813         fprintf(stderr, "Failed to get weston buffer.\n");
814         return -1;
815     }
816
817     shm_buffer = wl_shm_buffer_get(weston_buffer->resource);
818     if (shm_buffer == NULL) {
819         return -1;
820     }
821
822     source_buffer = wl_shm_buffer_get_data(shm_buffer);
823     if (source_buffer == NULL) {
824         fprintf(stderr, "Failed to get data from shm buffer.\n");
825         return -1;
826     }
827
828     dest_buffer = malloc(stride * height);
829     if (dest_buffer == NULL) {
830         fprintf(stderr, "Failed to allocate memory.\n");
831         return -1;
832     }
833
834     memcpy(dest_buffer, source_buffer, stride * height);
835
836     cairo_surf = cairo_image_surface_create_for_data(
837             dest_buffer,
838             CAIRO_FORMAT_RGB24,
839             width,
840             height,
841             stride);
842
843     cairo_surface_write_to_png(cairo_surf, filename);
844     cairo_surface_destroy(cairo_surf);
845     free(dest_buffer);
846
847     return 0;
848 }
849
850 static void
851 bind_framebuffer(GLuint *fbo_id, GLuint *tex_id, GLsizei width, GLsizei height)
852 {
853     glGenTextures(1, tex_id);
854     glGenFramebuffers(1, fbo_id);
855     glBindTexture(GL_TEXTURE_2D, *tex_id);
856     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
857     glBindFramebuffer(GL_FRAMEBUFFER, *fbo_id);
858     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, *tex_id, 0);
859     glClearColor(0, 0, 0, 0);
860     glClear(GL_COLOR_BUFFER_BIT);
861 }
862
863 static void
864 unbind_framebuffer(GLuint fbo_id, GLuint tex_id)
865 {
866     glBindTexture(GL_TEXTURE_2D, 0);
867     glBindFramebuffer(GL_FRAMEBUFFER, 0);
868     glDeleteTextures(1, &tex_id);
869     glDeleteFramebuffers(1, &fbo_id);
870 }
871
872 static void
873 dump_surface(struct weston_output *output,
874              struct weston_compositor *compositor,
875              const char *filename,
876              struct weston_view *view,
877              int32_t x,
878              int32_t y,
879              int32_t width,
880              int32_t height)
881 {
882     struct weston_renderer *renderer = compositor->renderer;
883     pixman_region32_t region;
884     struct wl_list backup_transformation_list;
885     struct weston_transform *link = NULL;
886     struct weston_transform *next = NULL;
887     uint8_t *readpixs = NULL;
888     int32_t stride = width * (PIXMAN_FORMAT_BPP(compositor->read_format) / 8);
889     GLuint fbo_id, tex_id;
890
891     readpixs = malloc(stride * height);
892     if (readpixs == NULL) {
893         fprintf(stderr, "Failed to allocate memory.\n");
894         return;
895     }
896
897     pixman_region32_init_rect(&region, x, y, width, height);
898     bind_framebuffer(&fbo_id, &tex_id, output->current_mode->width, output->current_mode->height);
899
900     wl_list_init(&backup_transformation_list);
901     wl_list_for_each_safe(link, next, &view->geometry.transformation_list, link) {
902         wl_list_remove(&link->link);
903         wl_list_insert(&backup_transformation_list, &link->link);
904     }
905
906     wl_list_init(&view->geometry.transformation_list);
907     weston_view_geometry_dirty(view);
908     weston_view_update_transform(view);
909
910     renderer->repaint_output(output, &region);
911     glFinish();
912
913     y = output->current_mode->height - (y + height);
914     int result = renderer->read_pixels(output,
915                                        compositor->read_format,
916                                        readpixs,
917                                        x,
918                                        y,
919                                        width,
920                                        height);
921
922     wl_list_for_each_safe(link, next, &backup_transformation_list, link) {
923         wl_list_remove(&link->link);
924         wl_list_insert(&view->geometry.transformation_list, &link->link);
925     }
926
927     weston_view_geometry_dirty(view);
928     weston_view_update_transform(view);
929
930     unbind_framebuffer(fbo_id, tex_id);
931
932     save_as_bitmap(filename, readpixs, stride * height, width, height, PIXMAN_FORMAT_BPP(compositor->read_format));
933     free(readpixs);
934 }
935
936 static struct weston_view *
937 clear_viewlist_but_specified_surface(struct weston_compositor *compositor,
938                                      struct weston_surface *surface)
939 {
940     struct weston_view *view = NULL;
941     wl_list_init(&compositor->view_list);
942
943     wl_list_for_each(view, &surface->views, surface_link) {
944         if (view == NULL) {
945             continue;
946         }
947
948         wl_list_insert(compositor->view_list.prev, &view->link);
949         break;
950     }
951
952     return view;
953 }
954
955 static void
956 get_gl_surface_rectangle(struct ivi_layout_surface_properties *prop,
957                          int32_t *x,
958                          int32_t *y,
959                          int32_t *width,
960                          int32_t *height)
961 {
962     if (prop == NULL) {
963         return;
964     }
965
966     *x = prop->source_x;
967     *y = prop->source_y;
968     *width = prop->source_width;
969     *height = prop->source_height;
970 }
971
972 static int
973 gl_surface_screenshot(struct ivisurface *ivisurf,
974                       struct weston_surface *surface,
975                       const char *filename)
976 {
977     struct weston_compositor *compositor = surface->compositor;
978     struct ivishell *shell = ivisurf->shell;
979     struct weston_view *view = NULL;
980     struct weston_output *output = NULL;
981     struct iviscreen *link_scrn = NULL;
982     const struct ivi_layout_surface_properties *prop;
983     int32_t x = 0;
984     int32_t y = 0;
985     int32_t width = 0;
986     int32_t height = 0;
987
988     wl_list_for_each(link_scrn, &shell->list_screen, link) {
989         if (link_scrn != NULL) {
990             break;
991         }
992     }
993
994     if (link_scrn == NULL) {
995         fprintf(stderr, "Failed to get iviscreen\n");
996         return -1;
997     }
998
999     if (!(prop = ivi_layout_get_properties_of_surface(ivisurf->layout_surface))) {
1000         fprintf(stderr, "Failed to get surface properties.");
1001         return -1;
1002     }
1003
1004     view = clear_viewlist_but_specified_surface(compositor, surface);
1005     if (view != NULL) {
1006         int32_t plane_is_null = 0;
1007         if (view->plane == NULL) {
1008             plane_is_null = 1;
1009             view->plane = &compositor->primary_plane;
1010         }
1011
1012         get_gl_surface_rectangle(prop, &x, &y, &width, &height);
1013         dump_surface(link_scrn->output,
1014                      compositor,
1015                      filename,
1016                      view,
1017                      x,
1018                      y,
1019                      width,
1020                      height);
1021
1022         if (plane_is_null) {
1023             view->plane = NULL;
1024         }
1025     }
1026
1027     return 0;
1028 }
1029
1030 static void
1031 controller_surface_screenshot(struct wl_client *client,
1032                   struct wl_resource *resource,
1033                   const char *filename)
1034 {
1035     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
1036     struct weston_surface *weston_surface = NULL;
1037     int32_t width = 0;
1038     int32_t height = 0;
1039     int32_t stride = 0;
1040
1041     weston_surface = ivi_layout_surface_get_weston_surface(ivisurf->layout_surface);
1042     if (weston_surface == NULL) {
1043         fprintf(stderr, "Failed to get weston surface.\n");
1044         return;
1045     }
1046
1047     if (ivi_layout_surface_get_size(ivisurf->layout_surface, &width, &height, &stride) != 0) {
1048         fprintf(stderr, "Failed to get surface size.\n");
1049         return;
1050     }
1051
1052     if (shm_surface_screenshot(weston_surface, width, height, stride, filename) != 0) {
1053         if (gl_surface_screenshot(ivisurf, weston_surface, filename) != 0) {
1054             fprintf(stderr, "Failed to capture surface.\n");
1055         }
1056     }
1057 }
1058
1059
1060 static void
1061 controller_surface_send_stats(struct wl_client *client,
1062                               struct wl_resource *resource)
1063 {
1064     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
1065     pid_t pid;
1066     uid_t uid;
1067     gid_t gid;
1068     wl_client_get_credentials(client, &pid, &uid, &gid);
1069
1070     ivi_controller_surface_send_stats(resource, 0, 0,
1071                                       ivisurf->update_count, pid, "");
1072 }
1073
1074 static void
1075 controller_surface_destroy(struct wl_client *client,
1076               struct wl_resource *resource,
1077               int32_t destroy_scene_object)
1078 {
1079     (void)client;
1080     (void)destroy_scene_object;
1081     wl_resource_destroy(resource);
1082 }
1083
1084 static void send_all_keyboard_focus(struct ivishell *shell)
1085 {
1086     const struct ivi_layout_surface_properties *props;
1087     struct ivicontroller_surface *ctrlsurf;
1088     struct ivisurface *current_surf;
1089     uint32_t id_surface;
1090
1091     wl_list_for_each(current_surf, &shell->list_surface, link) {
1092         if (!(props = ivi_layout_get_properties_of_surface(current_surf->layout_surface)))
1093             props = &no_surface_props;
1094
1095         id_surface = ivi_layout_get_id_of_surface(current_surf->layout_surface);
1096         wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
1097             if (id_surface != ctrlsurf->id_surface) {
1098                     continue;
1099             }
1100             ivi_controller_surface_send_input_focus(ctrlsurf->resource,
1101                             IVI_CONTROLLER_SURFACE_INPUT_DEVICE_KEYBOARD,
1102                             props->has_keyboard_focus);
1103         }
1104     }
1105 }
1106
1107 static void
1108 controller_surface_set_input_focus(struct wl_client *client,
1109               struct wl_resource *resource,
1110               uint32_t device,
1111               int32_t enabled)
1112 {
1113     (void)client;
1114     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
1115
1116     if (device & IVI_CONTROLLER_SURFACE_INPUT_DEVICE_KEYBOARD) {
1117         if (enabled) {
1118             ivi_layout_set_keyboard_focus_on(ivisurf->layout_surface);
1119             send_all_keyboard_focus(ivisurf->shell);
1120         }
1121     }
1122 }
1123
1124 static const
1125 struct ivi_controller_surface_interface controller_surface_implementation = {
1126     controller_surface_set_visibility,
1127     controller_surface_set_opacity,
1128     controller_surface_set_source_rectangle,
1129     controller_surface_set_destination_rectangle,
1130     controller_surface_set_configuration,
1131     controller_surface_set_orientation,
1132     controller_surface_screenshot,
1133     controller_surface_send_stats,
1134     controller_surface_destroy,
1135     controller_surface_set_input_focus
1136 };
1137
1138 static void
1139 controller_layer_set_source_rectangle(struct wl_client *client,
1140                    struct wl_resource *resource,
1141                    int32_t x,
1142                    int32_t y,
1143                    int32_t width,
1144                    int32_t height)
1145 {
1146     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1147     (void)client;
1148     ivi_layout_layer_set_source_rectangle(ivilayer->layout_layer,
1149            (uint32_t)x, (uint32_t)y, (uint32_t)width, (uint32_t)height);
1150 }
1151
1152 static void
1153 controller_layer_set_destination_rectangle(struct wl_client *client,
1154                  struct wl_resource *resource,
1155                  int32_t x,
1156                  int32_t y,
1157                  int32_t width,
1158                  int32_t height)
1159 {
1160     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1161     (void)client;
1162     ivi_layout_layer_set_destination_rectangle(ivilayer->layout_layer,
1163             (uint32_t)x, (uint32_t)y, (uint32_t)width, (uint32_t)height);
1164 }
1165
1166 static void
1167 controller_layer_set_visibility(struct wl_client *client,
1168                     struct wl_resource *resource,
1169                     uint32_t visibility)
1170 {
1171     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1172     (void)client;
1173     ivi_layout_layer_set_visibility(ivilayer->layout_layer, visibility);
1174 }
1175
1176 static void
1177 controller_layer_set_opacity(struct wl_client *client,
1178                  struct wl_resource *resource,
1179                  wl_fixed_t opacity)
1180 {
1181     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1182     (void)client;
1183     ivi_layout_layer_set_opacity(ivilayer->layout_layer,
1184                                  wl_fixed_to_double(opacity));
1185 }
1186
1187 static void
1188 controller_layer_set_configuration(struct wl_client *client,
1189                  struct wl_resource *resource,
1190                  int32_t width,
1191                  int32_t height)
1192 {
1193     /* This interface has been supported yet. */
1194     (void)client;
1195     (void)resource;
1196     (void)width;
1197     (void)height;
1198 }
1199
1200 static void
1201 controller_layer_set_orientation(struct wl_client *client,
1202                  struct wl_resource *resource,
1203                  int32_t orientation)
1204 {
1205     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1206     (void)client;
1207     ivi_layout_layer_set_orientation(ivilayer->layout_layer, (uint32_t)orientation);
1208 }
1209
1210 static void
1211 controller_layer_clear_surfaces(struct wl_client *client,
1212                     struct wl_resource *resource)
1213 {
1214     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1215     (void)client;
1216     ivi_layout_layer_set_render_order(ivilayer->layout_layer, NULL, 0);
1217 }
1218
1219 static void
1220 controller_layer_add_surface(struct wl_client *client,
1221                  struct wl_resource *resource,
1222                  struct wl_resource *surface)
1223 {
1224     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1225     struct ivisurface *ivisurf = wl_resource_get_user_data(surface);
1226     (void)client;
1227     ivi_layout_layer_add_surface(ivilayer->layout_layer, ivisurf->layout_surface);
1228 }
1229
1230 static void
1231 controller_layer_remove_surface(struct wl_client *client,
1232                     struct wl_resource *resource,
1233                     struct wl_resource *surface)
1234 {
1235     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1236     struct ivisurface *ivisurf = wl_resource_get_user_data(surface);
1237     (void)client;
1238     ivi_layout_layer_remove_surface(ivilayer->layout_layer, ivisurf->layout_surface);
1239 }
1240
1241 static void
1242 controller_layer_screenshot(struct wl_client *client,
1243                 struct wl_resource *resource,
1244                 const char *filename)
1245 {
1246     (void)client;
1247     (void)resource;
1248     (void)filename;
1249 }
1250
1251 static void
1252 controller_layer_set_render_order(struct wl_client *client,
1253                                   struct wl_resource *resource,
1254                                   struct wl_array *id_surfaces)
1255 {
1256     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1257     struct ivi_layout_surface **layoutsurf_array = NULL;
1258     struct ivisurface *ivisurf = NULL;
1259     uint32_t *id_surface = NULL;
1260     uint32_t id_layout_surface = 0;
1261     int i = 0;
1262     (void)client;
1263
1264     layoutsurf_array = (struct ivi_layout_surface**)calloc(
1265                            id_surfaces->size, sizeof(void*));
1266
1267     wl_array_for_each(id_surface, id_surfaces) {
1268         wl_list_for_each(ivisurf, &ivilayer->shell->list_surface, link) {
1269             id_layout_surface = ivi_layout_get_id_of_surface(ivisurf->layout_surface);
1270             if (*id_surface == id_layout_surface) {
1271                 layoutsurf_array[i] = ivisurf->layout_surface;
1272                 i++;
1273                 break;
1274             }
1275         }
1276     }
1277
1278     ivi_layout_layer_set_render_order(ivilayer->layout_layer,
1279                                       layoutsurf_array, i);
1280     free(layoutsurf_array);
1281 }
1282
1283 static void
1284 controller_layer_destroy(struct wl_client *client,
1285               struct wl_resource *resource,
1286               int32_t destroy_scene_object)
1287 {
1288     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1289     struct ivishell *shell = ivilayer->shell;
1290     struct ivicontroller_layer *ctrllayer = NULL;
1291     struct ivicontroller_layer *next = NULL;
1292     uint32_t id_layer = ivi_layout_get_id_of_layer(ivilayer->layout_layer);
1293     (void)client;
1294     (void)destroy_scene_object;
1295
1296     ivilayer->layer_canbe_removed = 1;
1297     wl_list_for_each_safe(ctrllayer, next, &shell->list_controller_layer, link) {
1298         if (ctrllayer->id_layer != id_layer) {
1299             continue;
1300         }
1301
1302         wl_resource_destroy(ctrllayer->resource);
1303     }
1304 }
1305
1306 static const
1307 struct ivi_controller_layer_interface controller_layer_implementation = {
1308     controller_layer_set_visibility,
1309     controller_layer_set_opacity,
1310     controller_layer_set_source_rectangle,
1311     controller_layer_set_destination_rectangle,
1312     controller_layer_set_configuration,
1313     controller_layer_set_orientation,
1314     controller_layer_screenshot,
1315     controller_layer_clear_surfaces,
1316     controller_layer_add_surface,
1317     controller_layer_remove_surface,
1318     controller_layer_set_render_order,
1319     controller_layer_destroy
1320 };
1321
1322 static void
1323 controller_screen_destroy(struct wl_client *client,
1324                           struct wl_resource *resource)
1325 {
1326     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1327     struct ivicontroller_screen *ctrlscrn = NULL;
1328     struct ivicontroller_screen *next = NULL;
1329 //    uint32_t id_screen = ivi_layout_get_id_of_screen(iviscrn->layout_screen);
1330     (void)client;
1331
1332     wl_list_for_each_safe(ctrlscrn, next,
1333                           &iviscrn->shell->list_controller_screen, link) {
1334         if (resource != ctrlscrn->resource) {
1335             continue;
1336         }
1337
1338         wl_list_remove(&ctrlscrn->link);
1339         wl_resource_destroy(ctrlscrn->resource);
1340         free(ctrlscrn);
1341         ctrlscrn = NULL;
1342         break;
1343     }
1344 }
1345
1346 static void
1347 controller_screen_clear(struct wl_client *client,
1348                 struct wl_resource *resource)
1349 {
1350     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1351     (void)client;
1352     ivi_layout_screen_set_render_order(iviscrn->layout_screen, NULL, 0);
1353 }
1354
1355 static void
1356 controller_screen_add_layer(struct wl_client *client,
1357                 struct wl_resource *resource,
1358                 struct wl_resource *layer)
1359 {
1360     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1361     struct ivilayer *ivilayer = wl_resource_get_user_data(layer);
1362     (void)client;
1363     ivi_layout_screen_add_layer(iviscrn->layout_screen, ivilayer->layout_layer);
1364 }
1365
1366 static void
1367 controller_screen_screenshot(struct wl_client *client,
1368                 struct wl_resource *resource,
1369                 const char *filename)
1370 {
1371     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1372     (void)client;
1373
1374     struct weston_output *output = NULL;
1375     cairo_surface_t *cairo_surf = NULL;
1376     int32_t i = 0;
1377     int32_t width = 0;
1378     int32_t height = 0;
1379     int32_t stride = 0;
1380     uint8_t *readpixs = NULL;
1381
1382     output = ivi_layout_screen_get_output(iviscrn->layout_screen);
1383     --output->disable_planes;
1384
1385     width = output->current_mode->width;
1386     height = output->current_mode->height;
1387     stride = width * (PIXMAN_FORMAT_BPP(output->compositor->read_format) / 8);
1388
1389     readpixs = malloc(stride * height);
1390     if (readpixs == NULL) {
1391         weston_log("fails to allocate memory\n");
1392         return;
1393     }
1394
1395     output->compositor->renderer->read_pixels(
1396             output,
1397             output->compositor->read_format,
1398             readpixs,
1399             0,
1400             0,
1401             width,
1402             height);
1403
1404     save_as_bitmap(filename, readpixs, stride * height, width, height, PIXMAN_FORMAT_BPP(output->compositor->read_format));
1405     free(readpixs);
1406 }
1407
1408 static void
1409 controller_screen_set_render_order(struct wl_client *client,
1410                 struct wl_resource *resource,
1411                 struct wl_array *id_layers)
1412 {
1413     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1414     struct ivi_layout_layer **layoutlayer_array = NULL;
1415     struct ivilayer *ivilayer = NULL;
1416     uint32_t *id_layer = NULL;
1417     uint32_t id_layout_layer = 0;
1418     int i = 0;
1419     (void)client;
1420
1421     layoutlayer_array = (struct ivi_layout_layer**)calloc(
1422                            id_layers->size, sizeof(void*));
1423
1424     wl_array_for_each(id_layer, id_layers) {
1425         wl_list_for_each(ivilayer, &iviscrn->shell->list_layer, link) {
1426             id_layout_layer = ivi_layout_get_id_of_layer(ivilayer->layout_layer);
1427             if (*id_layer == id_layout_layer) {
1428                 layoutlayer_array[i] = ivilayer->layout_layer;
1429                 i++;
1430                 break;
1431             }
1432         }
1433     }
1434
1435     ivi_layout_screen_set_render_order(iviscrn->layout_screen,
1436                                        layoutlayer_array, i);
1437     free(layoutlayer_array);
1438 }
1439
1440 static const
1441 struct ivi_controller_screen_interface controller_screen_implementation = {
1442     controller_screen_destroy,
1443     controller_screen_clear,
1444     controller_screen_add_layer,
1445     controller_screen_screenshot,
1446     controller_screen_set_render_order
1447 };
1448
1449 static void
1450 controller_commit_changes(struct wl_client *client,
1451                           struct wl_resource *resource)
1452 {
1453     int32_t ans = 0;
1454     (void)client;
1455     (void)resource;
1456
1457     ans = ivi_layout_commit_changes();
1458     if (ans < 0) {
1459         weston_log("Failed to commit changes at controller_commit_changes\n");
1460     }
1461 }
1462
1463 static void
1464 controller_layer_create(struct wl_client *client,
1465                         struct wl_resource *resource,
1466                         uint32_t id_layer,
1467                         int32_t width,
1468                         int32_t height,
1469                         uint32_t id)
1470 {
1471     struct ivicontroller *ctrl = wl_resource_get_user_data(resource);
1472     struct ivishell *shell = ctrl->shell;
1473     struct ivi_layout_layer *layout_layer = NULL;
1474     struct ivicontroller_layer *ctrllayer = NULL;
1475     struct ivilayer *ivilayer = NULL;
1476     const struct ivi_layout_layer_properties *prop;
1477
1478     ivilayer = get_layer(&shell->list_layer, id_layer);
1479     if (ivilayer == NULL) {
1480         layout_layer = ivi_layout_layer_create_with_dimension(id_layer,
1481                            (uint32_t)width, (uint32_t)height);
1482         if (layout_layer == NULL) {
1483             weston_log("id_layer is already created\n");
1484             return;
1485         }
1486
1487         /* ivilayer will be created by layer_event_create */
1488         ivilayer = get_layer(&shell->list_layer, id_layer);
1489         if (ivilayer == NULL) {
1490             weston_log("couldn't get layer object\n");
1491             return;
1492         }
1493     }
1494
1495     ctrllayer = calloc(1, sizeof *ctrllayer);
1496     if (!ctrllayer) {
1497         weston_log("no memory to allocate client layer\n");
1498         return;
1499     }
1500
1501     ++ivilayer->controller_layer_count;
1502     ivilayer->layer_canbe_removed = 0;
1503
1504     ctrllayer->shell = shell;
1505     ctrllayer->client = client;
1506     ctrllayer->id = id;
1507     ctrllayer->id_layer = id_layer;
1508     ctrllayer->resource = wl_resource_create(client,
1509                                &ivi_controller_layer_interface, 1, id);
1510     if (ctrllayer->resource == NULL) {
1511         weston_log("couldn't get layer object\n");
1512         return;
1513     }
1514
1515     wl_list_init(&ctrllayer->link);
1516     wl_list_insert(&shell->list_controller_layer, &ctrllayer->link);
1517
1518     wl_resource_set_implementation(ctrllayer->resource,
1519                                    &controller_layer_implementation,
1520                                    ivilayer, destroy_ivicontroller_layer);
1521
1522     if (!(prop = ivi_layout_get_properties_of_layer(ivilayer->layout_layer)))
1523       prop = &no_layer_props;
1524
1525     send_layer_event(ctrllayer->resource, ivilayer,
1526                      prop, IVI_NOTIFICATION_ALL);
1527 }
1528
1529 static void
1530 surface_event_content(struct ivi_layout_surface *layout_surface, int32_t content, void *userdata)
1531 {
1532     struct ivishell *shell = userdata;
1533     struct ivicontroller_surface *ctrlsurf = NULL;
1534     uint32_t id_surface = 0;
1535
1536     if (content == 0) {
1537         surface_event_remove(layout_surface, userdata);
1538     }
1539 }
1540
1541 static void
1542 controller_surface_create(struct wl_client *client,
1543                           struct wl_resource *resource,
1544                           uint32_t id_surface,
1545                           uint32_t id)
1546 {
1547     struct ivicontroller *ctrl = wl_resource_get_user_data(resource);
1548     struct ivishell *shell = ctrl->shell;
1549     struct ivicontroller_surface *ctrlsurf = NULL;
1550     const struct ivi_layout_surface_properties *prop;
1551     struct ivisurface *ivisurf = NULL;
1552     struct ivicontroller_surface *ctrl_link = NULL;
1553
1554     ivisurf = get_surface(&shell->list_surface, id_surface);
1555     if (ivisurf == NULL) {
1556         return;
1557     }
1558
1559     ctrlsurf = calloc(1, sizeof *ctrlsurf);
1560     if (!ctrlsurf) {
1561         weston_log("no memory to allocate controller surface\n");
1562         return;
1563     }
1564
1565     ctrlsurf->shell = shell;
1566     ctrlsurf->client = client;
1567     ctrlsurf->id = id;
1568     ctrlsurf->id_surface = id_surface;
1569     wl_list_init(&ctrlsurf->link);
1570     wl_list_insert(&shell->list_controller_surface, &ctrlsurf->link);
1571
1572     ctrlsurf->resource = wl_resource_create(client,
1573                                &ivi_controller_surface_interface, 1, id);
1574     if (ctrlsurf->resource == NULL) {
1575         weston_log("couldn't surface object");
1576         return;
1577     }
1578
1579     wl_list_for_each(ctrl_link, &shell->list_controller_surface, link) {
1580         if ((ctrl_link->implementation_set == 0) &&
1581             (ctrl_link->id_surface == id_surface) &&
1582             (ctrl_link->shell == shell) &&
1583             (ctrl_link->client != client)) {
1584             ++ivisurf->controller_surface_count;
1585             wl_resource_set_implementation(ctrl_link->resource,
1586                                            &controller_surface_implementation,
1587                                            ivisurf, destroy_ivicontroller_surface);
1588             ctrl_link->implementation_set = 1;
1589         }
1590     }
1591
1592     ++ivisurf->controller_surface_count;
1593
1594     wl_resource_set_implementation(ctrlsurf->resource,
1595                                    &controller_surface_implementation,
1596                                    ivisurf, destroy_ivicontroller_surface);
1597
1598     ctrlsurf->implementation_set = 1;
1599
1600     if (!(prop = ivi_layout_get_properties_of_surface(ivisurf->layout_surface)))
1601       prop = &no_surface_props;
1602
1603     ivi_layout_surface_set_content_observer(ivisurf->layout_surface, surface_event_content, shell);
1604
1605     send_surface_event(ctrlsurf->resource, ivisurf,
1606                        prop, IVI_NOTIFICATION_ALL);
1607 }
1608
1609 static void
1610 controller_get_native_handle(struct wl_client *client,
1611                              struct wl_resource *resource,
1612                              uint32_t id_process,
1613                              const char *title)
1614 {
1615     struct wl_array surfaces;
1616     ivi_shell_get_shell_surfaces(&surfaces);
1617     struct shell_surface ** surface;
1618
1619     wl_array_for_each(surface, &surfaces) {
1620
1621         uint32_t pid = shell_surface_get_process_id(*surface);
1622         if (pid != id_process) {
1623             continue;
1624         }
1625
1626         if (title) {
1627             char* surface_title = shell_surface_get_title(*surface);
1628             if (strcmp(title, surface_title)) {
1629                 continue;
1630             }
1631         }
1632
1633         struct weston_surface *es = shell_surface_get_surface(*surface);
1634         if (es) {
1635             struct wl_resource *res = wl_resource_create(client, &wl_surface_interface, 1, 0);
1636             wl_resource_set_user_data(res, es);
1637
1638             ivi_controller_send_native_handle(resource, res);
1639         }
1640     }
1641
1642     wl_array_release(&surfaces);
1643
1644     int32_t id_object = 0;
1645     ivi_controller_send_error(
1646         resource, id_object, IVI_CONTROLLER_OBJECT_TYPE_SURFACE,
1647         IVI_CONTROLLER_ERROR_CODE_NATIVE_HANDLE_END, "");
1648 }
1649
1650 static void
1651 controller_set_keyboard_focus(struct wl_client *client,
1652                               struct wl_resource *resource,
1653                               struct wl_array *surfaces)
1654 {
1655 #if 0
1656     /* this supposed to go away in later rteleases as
1657        ivi_layout_set_keyboard_focus_on() nowadays takes a single surface
1658        as opposed to an array */
1659     struct ivicontroller *ctrl = wl_resource_get_user_data(resource);
1660     ivi_layout_set_keyboard_focus_on(surfaces->data, surfaces->size);
1661     send_all_keyboard_focus(ctrl->shell);
1662 #endif
1663 }
1664
1665 static const struct ivi_controller_interface controller_implementation = {
1666     controller_commit_changes,
1667     controller_layer_create,
1668     controller_surface_create,
1669     controller_set_keyboard_focus,
1670     controller_get_native_handle
1671 };
1672
1673 static void
1674 add_client_to_resources(struct ivishell *shell,
1675                         struct wl_client *client,
1676                         struct ivicontroller *controller)
1677 {
1678     struct ivisurface* ivisurf = NULL;
1679     struct ivilayer* ivilayer = NULL;
1680     struct iviscreen* iviscrn = NULL;
1681     struct ivicontroller_screen *ctrlscrn = NULL;
1682     struct wl_resource *resource_output = NULL;
1683     uint32_t id_layout_surface = 0;
1684     uint32_t id_layout_layer = 0;
1685     struct ivi_layout_surface *layout_surface;
1686     int32_t pid;
1687     const char *window_title;
1688
1689     wl_list_for_each(iviscrn, &shell->list_screen, link) {
1690         resource_output = wl_resource_find_for_client(
1691                 &iviscrn->output->resource_list, client);
1692         if (resource_output == NULL) {
1693             continue;
1694         }
1695
1696         ctrlscrn = controller_screen_create(iviscrn->shell, client, iviscrn);
1697         if (ctrlscrn == NULL) {
1698             continue;
1699         }
1700
1701         ivi_controller_send_screen(controller->resource,
1702                                    wl_resource_get_id(resource_output),
1703                                    ctrlscrn->resource);
1704     }
1705     wl_list_for_each_reverse(ivilayer, &shell->list_layer, link) {
1706         id_layout_layer =
1707             ivi_layout_get_id_of_layer(ivilayer->layout_layer);
1708
1709         ivi_controller_send_layer(controller->resource,
1710                                   id_layout_layer);
1711     }
1712     wl_list_for_each_reverse(ivisurf, &shell->list_surface, link) {
1713         layout_surface = ivisurf->layout_surface;
1714         id_layout_surface = ivi_layout_get_id_of_surface(layout_surface);
1715
1716         get_wl_shell_info(layout_surface, id_layout_surface,
1717                           &pid, &window_title);
1718
1719         ivi_controller_send_surface(controller->resource,
1720                                     id_layout_surface, pid, window_title);
1721     }
1722 }
1723
1724 static void
1725 bind_ivi_controller(struct wl_client *client, void *data,
1726                     uint32_t version, uint32_t id)
1727 {
1728     struct ivishell *shell = data;
1729     struct ivicontroller *controller;
1730     (void)version;
1731
1732     controller = calloc(1, sizeof *controller);
1733     if (controller == NULL) {
1734         weston_log("no memory to allocate controller\n");
1735         return;
1736     }
1737
1738     controller->resource =
1739         wl_resource_create(client, &ivi_controller_interface, 1, id);
1740     wl_resource_set_implementation(controller->resource,
1741                                    &controller_implementation,
1742                                    controller, unbind_resource_controller);
1743
1744     controller->shell = shell;
1745     controller->client = client;
1746     controller->id = id;
1747
1748     wl_list_init(&controller->link);
1749     wl_list_insert(&shell->list_controller, &controller->link);
1750
1751     add_client_to_resources(shell, client, controller);
1752 }
1753
1754 static struct iviscreen*
1755 create_screen(struct ivishell *shell, struct weston_output *output)
1756 {
1757     struct iviscreen *iviscrn;
1758     iviscrn = calloc(1, sizeof *iviscrn);
1759     if (iviscrn == NULL) {
1760         weston_log("no memory to allocate client screen\n");
1761         return NULL;
1762     }
1763
1764     iviscrn->shell = shell;
1765     iviscrn->output = output;
1766
1767 // TODO : Only Single display
1768     iviscrn->layout_screen = ivi_layout_get_screen_from_id(0);
1769
1770     wl_list_init(&iviscrn->link);
1771
1772     return iviscrn;
1773 }
1774
1775 static struct ivilayer*
1776 create_layer(struct ivishell *shell,
1777              struct ivi_layout_layer *layout_layer,
1778              uint32_t id_layer)
1779 {
1780     struct ivilayer *ivilayer = NULL;
1781     struct ivicontroller *controller = NULL;
1782
1783     ivilayer = get_layer(&shell->list_layer, id_layer);
1784     if (ivilayer != NULL) {
1785         weston_log("id_layer is already created\n");
1786         return NULL;
1787     }
1788
1789     ivilayer = calloc(1, sizeof *ivilayer);
1790     if (NULL == ivilayer) {
1791         weston_log("no memory to allocate client layer\n");
1792         return NULL;
1793     }
1794
1795     ivilayer->shell = shell;
1796     wl_list_init(&ivilayer->list_screen);
1797     wl_list_init(&ivilayer->link);
1798     wl_list_insert(&shell->list_layer, &ivilayer->link);
1799     ivilayer->layout_layer = layout_layer;
1800
1801     ivi_layout_layer_add_notification(layout_layer, send_layer_prop, ivilayer);
1802
1803     wl_list_for_each(controller, &shell->list_controller, link) {
1804         ivi_controller_send_layer(controller->resource, id_layer);
1805     }
1806
1807     return ivilayer;
1808 }
1809
1810 static struct ivisurface*
1811 create_surface(struct ivishell *shell,
1812                struct ivi_layout_surface *layout_surface,
1813                uint32_t id_surface)
1814 {
1815     struct ivisurface *ivisurf = NULL;
1816     struct ivicontroller *controller = NULL;
1817     int32_t pid;
1818     const char *window_title;
1819
1820     ivisurf = get_surface(&shell->list_surface, id_surface);
1821     if (ivisurf != NULL) {
1822         weston_log("id_surface is already created\n");
1823         return NULL;
1824     }
1825
1826     ivisurf = calloc(1, sizeof *ivisurf);
1827     if (ivisurf == NULL) {
1828         weston_log("no memory to allocate client surface\n");
1829         return NULL;
1830     }
1831
1832     ivisurf->shell = shell;
1833     ivisurf->layout_surface = layout_surface;
1834     wl_list_init(&ivisurf->list_layer);
1835     wl_list_init(&ivisurf->link);
1836     wl_list_insert(&shell->list_surface, &ivisurf->link);
1837
1838     get_wl_shell_info(layout_surface, id_surface, &pid, &window_title);
1839
1840     wl_list_for_each(controller, &shell->list_controller, link) {
1841         ivi_controller_send_surface(controller->resource,
1842                                     id_surface, pid, window_title);
1843     }
1844
1845     ivi_layout_surface_add_notification(layout_surface,
1846                                         send_surface_prop, ivisurf);
1847
1848     return ivisurf;
1849 }
1850
1851 static void
1852 layer_event_create(struct ivi_layout_layer *layout_layer,
1853                      void *userdata)
1854 {
1855     struct ivishell *shell = userdata;
1856     struct ivilayer *ivilayer = NULL;
1857     uint32_t id_layer = 0;
1858
1859     id_layer = ivi_layout_get_id_of_layer(layout_layer);
1860
1861     ivilayer = create_layer(shell, layout_layer, id_layer);
1862     if (ivilayer == NULL) {
1863         weston_log("failed to create layer");
1864         return;
1865     }
1866 }
1867
1868 static void
1869 layer_event_remove(struct ivi_layout_layer *layout_layer,
1870                    void *userdata)
1871 {
1872     struct ivishell *shell = userdata;
1873     struct ivicontroller_layer *ctrllayer = NULL;
1874     struct ivilayer *ivilayer = NULL;
1875     struct ivilayer *next = NULL;
1876     uint32_t id_layer = 0;
1877     int is_removed = 0;
1878
1879     wl_list_for_each_safe(ivilayer, next, &shell->list_layer, link) {
1880         if (layout_layer != ivilayer->layout_layer) {
1881             continue;
1882         }
1883
1884         wl_list_remove(&ivilayer->link);
1885
1886         is_removed = 1;
1887         free(ivilayer);
1888         ivilayer = NULL;
1889         break;
1890     }
1891
1892     if (is_removed) {
1893         id_layer = ivi_layout_get_id_of_layer(layout_layer);
1894
1895         wl_list_for_each(ctrllayer, &shell->list_controller_layer, link) {
1896             if (id_layer != ctrllayer->id_layer) {
1897                 continue;
1898             }
1899             ivi_controller_layer_send_destroyed(ctrllayer->resource);
1900         }
1901     }
1902 }
1903
1904
1905 static void
1906 surface_event_create(struct ivi_layout_surface *layout_surface,
1907                      void *userdata)
1908 {
1909     struct ivishell *shell = userdata;
1910     struct ivisurface *ivisurf = NULL;
1911     uint32_t id_surface = 0;
1912     struct ivicontroller_surface *ctrlsurf = NULL;
1913
1914     id_surface = ivi_layout_get_id_of_surface(layout_surface);
1915
1916     ivisurf = create_surface(shell, layout_surface, id_surface);
1917     if (ivisurf == NULL) {
1918         weston_log("failed to create surface");
1919         return;
1920     }
1921
1922     wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
1923         if (id_surface != ctrlsurf->id_surface) {
1924             continue;
1925         }
1926         ivi_controller_surface_send_content(ctrlsurf->resource, IVI_CONTROLLER_SURFACE_CONTENT_STATE_CONTENT_AVAILABLE);
1927     }
1928 }
1929
1930 static void
1931 surface_event_remove(struct ivi_layout_surface *layout_surface,
1932                      void *userdata)
1933 {
1934     struct ivishell *shell = userdata;
1935     struct ivicontroller_surface *ctrlsurf = NULL;
1936     struct ivisurface *ivisurf = NULL;
1937     struct ivisurface *next = NULL;
1938     uint32_t id_surface = 0;
1939     int is_removed = 0;
1940
1941     wl_list_for_each_safe(ivisurf, next, &shell->list_surface, link) {
1942         if (layout_surface != ivisurf->layout_surface) {
1943             continue;
1944         }
1945
1946         wl_list_remove(&ivisurf->link);
1947         is_removed = 1;
1948
1949         if (ivisurf->controller_surface_count == 0) {
1950             free(ivisurf);
1951         }
1952         else {
1953             ivisurf->can_be_removed = 1;
1954         }
1955
1956         break;
1957     }
1958
1959     if (is_removed) {
1960         id_surface = ivi_layout_get_id_of_surface(layout_surface);
1961
1962         wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
1963             if (id_surface != ctrlsurf->id_surface) {
1964                 continue;
1965             }
1966             ivi_controller_surface_send_content(ctrlsurf->resource, IVI_CONTROLLER_SURFACE_CONTENT_STATE_CONTENT_REMOVED);
1967             ivi_controller_surface_send_destroyed(ctrlsurf->resource);
1968         }
1969     }
1970 }
1971
1972 static void
1973 surface_event_configure(struct ivi_layout_surface *layout_surface,
1974                         void *userdata)
1975 {
1976     struct ivishell *shell = userdata;
1977     struct ivisurface *ivisurf = NULL;
1978     struct ivicontroller_surface *ctrlsurf = NULL;
1979     struct ivi_layout_surface_properties *prop;
1980     uint32_t id_surface = 0;
1981
1982     id_surface = ivi_layout_get_id_of_surface(layout_surface);
1983
1984     ivisurf = get_surface(&shell->list_surface, id_surface);
1985     if (ivisurf == NULL) {
1986         weston_log("id_surface is not created yet\n");
1987         return;
1988     }
1989
1990     if (!(prop = ivi_layout_get_properties_of_surface(layout_surface)))
1991       prop = &no_surface_props;
1992
1993     wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
1994         if (id_surface != ctrlsurf->id_surface) {
1995             continue;
1996         }
1997         send_surface_event(ctrlsurf->resource, ivisurf,
1998                            prop, IVI_NOTIFICATION_ALL);
1999     }
2000 }
2001
2002 static int32_t
2003 check_layout_layers(struct ivishell *shell)
2004 {
2005     struct ivi_layout_layer **pArray = NULL;
2006     struct ivilayer *ivilayer = NULL;
2007     uint32_t id_layer = 0;
2008     uint32_t length = 0;
2009     uint32_t i = 0;
2010     int32_t ret = 0;
2011
2012     ret = ivi_layout_get_layers(&length, &pArray);
2013     if(ret != 0) {
2014         weston_log("failed to get layers at check_layout_layers\n");
2015         return -1;
2016     }
2017
2018     if (length == 0) {
2019         /* if length is 0, pArray doesn't need to free.*/
2020         return 0;
2021     }
2022
2023     for (i = 0; i < length; i++) {
2024         id_layer = ivi_layout_get_id_of_layer(pArray[i]);
2025         ivilayer = create_layer(shell, pArray[i], id_layer);
2026         if (ivilayer == NULL) {
2027             weston_log("failed to create layer");
2028         }
2029     }
2030
2031     free(pArray);
2032     pArray = NULL;
2033
2034     return 0;
2035 }
2036
2037 static int32_t
2038 check_layout_surfaces(struct ivishell *shell)
2039 {
2040     struct ivi_layout_surface **pArray = NULL;
2041     struct ivisurface *ivisurf = NULL;
2042     uint32_t id_surface = 0;
2043     uint32_t length = 0;
2044     uint32_t i = 0;
2045     int32_t ret = 0;
2046
2047     ret = ivi_layout_get_surfaces(&length, &pArray);
2048     if(ret != 0) {
2049         weston_log("failed to get surfaces at check_layout_surfaces\n");
2050         return -1;
2051     }
2052
2053     if (length == 0) {
2054         /* if length is 0, pArray doesn't need to free.*/
2055         return 0;
2056     }
2057
2058     for (i = 0; i < length; i++) {
2059         id_surface = ivi_layout_get_id_of_surface(pArray[i]);
2060         ivisurf = create_surface(shell, pArray[i], id_surface);
2061         if (ivisurf == NULL) {
2062             weston_log("failed to create surface");
2063         }
2064     }
2065
2066     free(pArray);
2067     pArray = NULL;
2068
2069     return 0;
2070 }
2071
2072 static void
2073 init_ivi_shell(struct weston_compositor *ec, struct ivishell *shell)
2074 {
2075     struct weston_output *output = NULL;
2076     struct iviscreen *iviscrn = NULL;
2077     int32_t ret = 0;
2078
2079     shell->compositor = ec;
2080
2081     wl_list_init(&shell->list_surface);
2082     wl_list_init(&shell->list_layer);
2083     wl_list_init(&shell->list_screen);
2084     wl_list_init(&shell->list_weston_surface);
2085     wl_list_init(&shell->list_controller);
2086     wl_list_init(&shell->list_controller_screen);
2087     wl_list_init(&shell->list_controller_layer);
2088     wl_list_init(&shell->list_controller_surface);
2089     shell->event_restriction = 0;
2090
2091     wl_list_for_each(output, &ec->output_list, link) {
2092         iviscrn = create_screen(shell, output);
2093         if (iviscrn != NULL) {
2094             wl_list_insert(&shell->list_screen, &iviscrn->link);
2095         }
2096     }
2097
2098     ret = check_layout_layers(shell);
2099     if (ret != 0) {
2100         weston_log("failed to check_layout_layers");
2101     }
2102
2103     ret = check_layout_surfaces(shell);
2104     if (ret != 0) {
2105         weston_log("failed to check_layout_surfaces");
2106     }
2107
2108     ivi_layout_add_notification_create_layer(layer_event_create, shell);
2109     ivi_layout_add_notification_remove_layer(layer_event_remove, shell);
2110
2111     ivi_layout_add_notification_create_surface(surface_event_create, shell);
2112     ivi_layout_add_notification_remove_surface(surface_event_remove, shell);
2113     ivi_layout_add_notification_configure_surface(surface_event_configure, shell);
2114 }
2115
2116 WL_EXPORT int
2117 module_init(struct weston_compositor *ec,
2118             int *argc, char *argv[])
2119 {
2120     struct ivishell *shell;
2121     (void)argc;
2122     (void)argv;
2123
2124     shell = malloc(sizeof *shell);
2125     if (shell == NULL)
2126         return -1;
2127
2128     memset(shell, 0, sizeof *shell);
2129     init_ivi_shell(ec, shell);
2130
2131     if (wl_global_create(ec->wl_display, &ivi_controller_interface, 1,
2132                          shell, bind_ivi_controller) == NULL) {
2133         return -1;
2134     }
2135
2136     return 0;
2137 }