downstream: use ifdef SUPPORT_INPUT_FOCUS for keyboard focus functions.
[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 #ifdef SUPPORT_INPUT_FOCUS
456     if (mask & IVI_NOTIFICATION_KEYBOARD_FOCUS) {
457         ivi_controller_surface_send_input_focus(resource,
458                         IVI_CONTROLLER_SURFACE_INPUT_DEVICE_KEYBOARD,
459                         prop->has_keyboard_focus);
460     }
461 #endif
462     if (mask & IVI_NOTIFICATION_REMOVE) {
463         send_surface_add_event(ivisurf, resource, IVI_NOTIFICATION_REMOVE);
464     }
465     if (mask & IVI_NOTIFICATION_ADD) {
466         send_surface_add_event(ivisurf, resource, IVI_NOTIFICATION_ADD);
467     }
468 }
469
470 static void
471 update_surface_prop(struct ivisurface *ivisurf,
472                     uint32_t mask)
473 {
474     struct ivi_layout_layer **pArray = NULL;
475     uint32_t length = 0;
476     int32_t ans = 0;
477     int i = 0;
478     struct ivishell *shell = ivisurf->shell;
479
480     ans = ivi_layout_get_layers_under_surface(ivisurf->layout_surface,
481                                               &length, &pArray);
482     if (0 != ans) {
483         weston_log("failed to get layers at send_surface_add_event\n");
484         return;
485     }
486
487     if (mask & IVI_NOTIFICATION_REMOVE) {
488         struct link_layer *link_layer = NULL;
489         struct link_layer *next = NULL;
490
491         wl_list_for_each_safe(link_layer, next, &ivisurf->list_layer, link) {
492             wl_list_remove(&link_layer->link);
493             free(link_layer);
494             link_layer = NULL;
495         }
496     }
497     if (mask & IVI_NOTIFICATION_ADD) {
498         for (i = 0; i < (int)length; ++i) {
499             /* Create list_layer */
500             struct ivilayer *ivilayer = NULL;
501             struct link_layer *link_layer = calloc(1, sizeof(*link_layer));
502             if (NULL == link_layer) {
503                 continue;
504             }
505             wl_list_init(&link_layer->link);
506             link_layer->layer = NULL;
507             wl_list_for_each(ivilayer, &shell->list_layer, link) {
508                 if (ivilayer->layout_layer == pArray[i]) {
509                     link_layer->layer = ivilayer;
510                     break;
511                 }
512             }
513
514             if (link_layer->layer == NULL) {
515                 free(link_layer);
516                 link_layer = NULL;
517                 continue;
518             }
519
520             wl_list_insert(&ivisurf->list_layer, &link_layer->link);
521         }
522     }
523 }
524
525 static void
526 send_surface_prop(struct ivi_layout_surface *layout_surface,
527                   struct ivi_layout_surface_properties *prop,
528                   enum ivi_layout_notification_mask mask,
529                   void *userdata)
530 {
531     struct ivisurface *ivisurf = userdata;
532     struct ivishell *shell = ivisurf->shell;
533     struct ivicontroller_surface *ctrlsurf = NULL;
534     uint32_t id_surface = 0;
535
536     id_surface = ivi_layout_get_id_of_surface(layout_surface);
537
538     wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
539         if (id_surface != ctrlsurf->id_surface) {
540             continue;
541         }
542         send_surface_event(ctrlsurf->resource, ivisurf, prop, mask);
543     }
544
545     update_surface_prop(ivisurf, mask);
546 }
547
548 static void
549 send_layer_add_event(struct ivilayer *ivilayer,
550                      struct wl_resource *resource,
551                      enum ivi_layout_notification_mask mask)
552 {
553     struct ivi_layout_screen **pArray = NULL;
554     uint32_t length = 0;
555     int32_t ans = 0;
556     int i = 0;
557     struct link_screen *link_scrn = NULL;
558     struct link_screen *next = NULL;
559     struct iviscreen *iviscrn = NULL;
560     struct ivishell *shell = ivilayer->shell;
561     struct wl_client *client = wl_resource_get_client(resource);
562     struct wl_resource *resource_output = NULL;
563
564     ans = ivi_layout_get_screens_under_layer(ivilayer->layout_layer,
565                                              &length, &pArray);
566     if (0 != ans) {
567         weston_log("failed to get screens at send_layer_add_event\n");
568         return;
569     }
570
571     /* Send Null to cancel added layer */
572     if (mask & IVI_NOTIFICATION_REMOVE) {
573         wl_list_for_each_safe(link_scrn, next, &ivilayer->list_screen, link) {
574             ivi_controller_layer_send_screen(resource, NULL);
575         }
576     }
577     else if (mask & IVI_NOTIFICATION_ADD) {
578         for (i = 0; i < (int)length; i++) {
579             /* Send new layer event */
580             iviscrn = NULL;
581             wl_list_for_each(iviscrn, &shell->list_screen, link) {
582                 if (iviscrn->layout_screen == pArray[i]) {
583                     break;
584                 }
585             }
586
587             if (iviscrn == NULL) {
588                 continue;
589             }
590
591             resource_output =
592                 wl_resource_find_for_client(&iviscrn->output->resource_list,
593                                      client);
594             if (resource_output != NULL) {
595                 ivi_controller_layer_send_screen(resource, resource_output);
596             }
597         }
598     }
599
600     free(pArray);
601     pArray = NULL;
602 }
603
604 static void
605 send_layer_event(struct wl_resource *resource,
606                  struct ivilayer *ivilayer,
607                  struct ivi_layout_layer_properties *prop,
608                  uint32_t mask)
609 {
610     if (mask & IVI_NOTIFICATION_OPACITY) {
611         ivi_controller_layer_send_opacity(resource,
612                                 wl_fixed_from_double(prop->opacity));
613     }
614     if (mask & IVI_NOTIFICATION_SOURCE_RECT) {
615         ivi_controller_layer_send_source_rectangle(resource,
616                                           prop->source_x,
617                                           prop->source_y,
618                                           prop->source_width,
619                                           prop->source_height);
620     }
621     if (mask & IVI_NOTIFICATION_DEST_RECT) {
622         ivi_controller_layer_send_destination_rectangle(resource,
623                                           prop->dest_x,
624                                           prop->dest_y,
625                                           prop->dest_width,
626                                           prop->dest_height);
627     }
628     if (mask & IVI_NOTIFICATION_ORIENTATION) {
629         ivi_controller_layer_send_orientation(resource,
630                                           prop->orientation);
631     }
632     if (mask & IVI_NOTIFICATION_VISIBILITY) {
633         ivi_controller_layer_send_visibility(resource,
634                                           prop->visibility);
635     }
636     if (mask & IVI_NOTIFICATION_REMOVE) {
637         send_layer_add_event(ivilayer, resource, IVI_NOTIFICATION_REMOVE);
638     }
639     if (mask & IVI_NOTIFICATION_ADD) {
640         send_layer_add_event(ivilayer, resource, IVI_NOTIFICATION_ADD);
641     }
642 }
643
644 static void
645 update_layer_prop(struct ivilayer *ivilayer,
646                   enum ivi_layout_notification_mask mask)
647 {
648     struct ivi_layout_screen **pArray = NULL;
649     uint32_t length = 0;
650     int32_t ans = 0;
651     struct link_screen *link_scrn = NULL;
652     struct link_screen *next = NULL;
653
654     ans = ivi_layout_get_screens_under_layer(ivilayer->layout_layer,
655                                              &length, &pArray);
656     if (0 != ans) {
657         weston_log("failed to get screens at send_layer_add_event\n");
658         return;
659     }
660
661     /* Send Null to cancel added layer */
662     if (mask & IVI_NOTIFICATION_REMOVE) {
663         wl_list_for_each_safe(link_scrn, next, &ivilayer->list_screen, link) {
664             wl_list_remove(&link_scrn->link);
665             free(link_scrn);
666             link_scrn = NULL;
667         }
668     }
669     if (mask & IVI_NOTIFICATION_ADD) {
670         int i = 0;
671         for (i = 0; i < (int)length; i++) {
672             struct ivishell *shell = ivilayer->shell;
673             struct iviscreen *iviscrn = NULL;
674             /* Create list_screen */
675             link_scrn = calloc(1, sizeof(*link_scrn));
676             if (NULL == link_scrn) {
677                 continue;
678             }
679             wl_list_init(&link_scrn->link);
680             link_scrn->screen = NULL;
681             wl_list_for_each(iviscrn, &shell->list_screen, link) {
682                 if (iviscrn->layout_screen == pArray[i]) {
683                     link_scrn->screen = iviscrn;
684                     break;
685                 }
686             }
687
688             if (link_scrn->screen == NULL) {
689                 free(link_scrn);
690                 link_scrn = NULL;
691                 continue;
692             }
693             wl_list_insert(&ivilayer->list_screen, &link_scrn->link);
694         }
695     }
696
697     free(pArray);
698     pArray = NULL;
699 }
700
701 static void
702 send_layer_prop(struct ivi_layout_layer *layer,
703                 const struct ivi_layout_layer_properties *prop,
704                 enum ivi_layout_notification_mask mask,
705                 void *userdata)
706 {
707     struct ivilayer *ivilayer = userdata;
708     struct ivicontroller_layer *ctrllayer = NULL;
709     struct ivishell *shell = ivilayer->shell;
710     uint32_t id_layout_layer = 0;
711
712     id_layout_layer = ivi_layout_get_id_of_layer(layer);
713     wl_list_for_each(ctrllayer, &shell->list_controller_layer, link) {
714         if (id_layout_layer != ctrllayer->id_layer) {
715             continue;
716         }
717         send_layer_event(ctrllayer->resource, ivilayer, prop, mask);
718     }
719
720     update_layer_prop(ivilayer, mask);
721 }
722
723 static void
724 controller_surface_set_opacity(struct wl_client *client,
725                    struct wl_resource *resource,
726                    wl_fixed_t opacity)
727 {
728     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
729     (void)client;
730     ivi_layout_surface_set_opacity(ivisurf->layout_surface,
731                                  wl_fixed_to_double(opacity));
732 }
733
734 static void
735 controller_surface_set_source_rectangle(struct wl_client *client,
736                    struct wl_resource *resource,
737                    int32_t x,
738                    int32_t y,
739                    int32_t width,
740                    int32_t height)
741 {
742     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
743     (void)client;
744     ivi_layout_surface_set_source_rectangle(ivisurf->layout_surface,
745             (uint32_t)x, (uint32_t)y, (uint32_t)width, (uint32_t)height);
746 }
747
748 static void
749 controller_surface_set_destination_rectangle(struct wl_client *client,
750                      struct wl_resource *resource,
751                      int32_t x,
752                      int32_t y,
753                      int32_t width,
754                      int32_t height)
755 {
756     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
757     (void)client;
758
759     // TODO: create set transition type protocol
760     ivi_layout_surface_set_transition(ivisurf->layout_surface,
761                                       IVI_LAYOUT_TRANSITION_NONE,
762                                       300); // ms
763
764     ivi_layout_surface_set_destination_rectangle(ivisurf->layout_surface,
765             (uint32_t)x, (uint32_t)y, (uint32_t)width, (uint32_t)height);
766 }
767
768 static void
769 controller_surface_set_visibility(struct wl_client *client,
770                       struct wl_resource *resource,
771                       uint32_t visibility)
772 {
773     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
774     (void)client;
775     ivi_layout_surface_set_visibility(ivisurf->layout_surface, visibility);
776 }
777
778 static void
779 controller_surface_set_configuration(struct wl_client *client,
780                    struct wl_resource *resource,
781                    int32_t width, int32_t height)
782 {
783     /* This interface has been supported yet. */
784     (void)client;
785     (void)resource;
786     (void)width;
787     (void)height;
788 }
789
790 static void
791 controller_surface_set_orientation(struct wl_client *client,
792                    struct wl_resource *resource,
793                    int32_t orientation)
794 {
795     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
796     (void)client;
797     ivi_layout_surface_set_orientation(ivisurf->layout_surface, (uint32_t)orientation);
798 }
799
800 static int
801 shm_surface_screenshot(struct weston_surface *surface,
802                        int32_t width,
803                        int32_t height,
804                        int32_t stride,
805                        const char *filename)
806 {
807     struct weston_buffer *weston_buffer = NULL;
808     struct wl_shm_buffer *shm_buffer = NULL;
809     cairo_surface_t *cairo_surf = NULL;
810     uint8_t *source_buffer = NULL;
811     uint8_t *dest_buffer = NULL;
812
813     weston_buffer = surface->buffer_ref.buffer;
814     if (weston_buffer == NULL) {
815         fprintf(stderr, "Failed to get weston buffer.\n");
816         return -1;
817     }
818
819     shm_buffer = wl_shm_buffer_get(weston_buffer->resource);
820     if (shm_buffer == NULL) {
821         return -1;
822     }
823
824     source_buffer = wl_shm_buffer_get_data(shm_buffer);
825     if (source_buffer == NULL) {
826         fprintf(stderr, "Failed to get data from shm buffer.\n");
827         return -1;
828     }
829
830     dest_buffer = malloc(stride * height);
831     if (dest_buffer == NULL) {
832         fprintf(stderr, "Failed to allocate memory.\n");
833         return -1;
834     }
835
836     memcpy(dest_buffer, source_buffer, stride * height);
837
838     cairo_surf = cairo_image_surface_create_for_data(
839             dest_buffer,
840             CAIRO_FORMAT_RGB24,
841             width,
842             height,
843             stride);
844
845     cairo_surface_write_to_png(cairo_surf, filename);
846     cairo_surface_destroy(cairo_surf);
847     free(dest_buffer);
848
849     return 0;
850 }
851
852 static void
853 bind_framebuffer(GLuint *fbo_id, GLuint *tex_id, GLsizei width, GLsizei height)
854 {
855     glGenTextures(1, tex_id);
856     glGenFramebuffers(1, fbo_id);
857     glBindTexture(GL_TEXTURE_2D, *tex_id);
858     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
859     glBindFramebuffer(GL_FRAMEBUFFER, *fbo_id);
860     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, *tex_id, 0);
861     glClearColor(0, 0, 0, 0);
862     glClear(GL_COLOR_BUFFER_BIT);
863 }
864
865 static void
866 unbind_framebuffer(GLuint fbo_id, GLuint tex_id)
867 {
868     glBindTexture(GL_TEXTURE_2D, 0);
869     glBindFramebuffer(GL_FRAMEBUFFER, 0);
870     glDeleteTextures(1, &tex_id);
871     glDeleteFramebuffers(1, &fbo_id);
872 }
873
874 static void
875 dump_surface(struct weston_output *output,
876              struct weston_compositor *compositor,
877              const char *filename,
878              struct weston_view *view,
879              int32_t x,
880              int32_t y,
881              int32_t width,
882              int32_t height)
883 {
884     struct weston_renderer *renderer = compositor->renderer;
885     pixman_region32_t region;
886     struct wl_list backup_transformation_list;
887     struct weston_transform *link = NULL;
888     struct weston_transform *next = NULL;
889     uint8_t *readpixs = NULL;
890     int32_t stride = width * (PIXMAN_FORMAT_BPP(compositor->read_format) / 8);
891     GLuint fbo_id, tex_id;
892
893     readpixs = malloc(stride * height);
894     if (readpixs == NULL) {
895         fprintf(stderr, "Failed to allocate memory.\n");
896         return;
897     }
898
899     pixman_region32_init_rect(&region, x, y, width, height);
900     bind_framebuffer(&fbo_id, &tex_id, output->current_mode->width, output->current_mode->height);
901
902     wl_list_init(&backup_transformation_list);
903     wl_list_for_each_safe(link, next, &view->geometry.transformation_list, link) {
904         wl_list_remove(&link->link);
905         wl_list_insert(&backup_transformation_list, &link->link);
906     }
907
908     wl_list_init(&view->geometry.transformation_list);
909     weston_view_geometry_dirty(view);
910     weston_view_update_transform(view);
911
912     renderer->repaint_output(output, &region);
913     glFinish();
914
915     y = output->current_mode->height - (y + height);
916     int result = renderer->read_pixels(output,
917                                        compositor->read_format,
918                                        readpixs,
919                                        x,
920                                        y,
921                                        width,
922                                        height);
923
924     wl_list_for_each_safe(link, next, &backup_transformation_list, link) {
925         wl_list_remove(&link->link);
926         wl_list_insert(&view->geometry.transformation_list, &link->link);
927     }
928
929     weston_view_geometry_dirty(view);
930     weston_view_update_transform(view);
931
932     unbind_framebuffer(fbo_id, tex_id);
933
934     save_as_bitmap(filename, readpixs, stride * height, width, height, PIXMAN_FORMAT_BPP(compositor->read_format));
935     free(readpixs);
936 }
937
938 static struct weston_view *
939 clear_viewlist_but_specified_surface(struct weston_compositor *compositor,
940                                      struct weston_surface *surface)
941 {
942     struct weston_view *view = NULL;
943     wl_list_init(&compositor->view_list);
944
945     wl_list_for_each(view, &surface->views, surface_link) {
946         if (view == NULL) {
947             continue;
948         }
949
950         wl_list_insert(compositor->view_list.prev, &view->link);
951         break;
952     }
953
954     return view;
955 }
956
957 static void
958 get_gl_surface_rectangle(struct ivi_layout_surface_properties *prop,
959                          int32_t *x,
960                          int32_t *y,
961                          int32_t *width,
962                          int32_t *height)
963 {
964     if (prop == NULL) {
965         return;
966     }
967
968     *x = prop->source_x;
969     *y = prop->source_y;
970     *width = prop->source_width;
971     *height = prop->source_height;
972 }
973
974 static int
975 gl_surface_screenshot(struct ivisurface *ivisurf,
976                       struct weston_surface *surface,
977                       const char *filename)
978 {
979     struct weston_compositor *compositor = surface->compositor;
980     struct ivishell *shell = ivisurf->shell;
981     struct weston_view *view = NULL;
982     struct weston_output *output = NULL;
983     struct iviscreen *link_scrn = NULL;
984     const struct ivi_layout_surface_properties *prop;
985     int32_t x = 0;
986     int32_t y = 0;
987     int32_t width = 0;
988     int32_t height = 0;
989
990     wl_list_for_each(link_scrn, &shell->list_screen, link) {
991         if (link_scrn != NULL) {
992             break;
993         }
994     }
995
996     if (link_scrn == NULL) {
997         fprintf(stderr, "Failed to get iviscreen\n");
998         return -1;
999     }
1000
1001     if (!(prop = ivi_layout_get_properties_of_surface(ivisurf->layout_surface))) {
1002         fprintf(stderr, "Failed to get surface properties.");
1003         return -1;
1004     }
1005
1006     view = clear_viewlist_but_specified_surface(compositor, surface);
1007     if (view != NULL) {
1008         int32_t plane_is_null = 0;
1009         if (view->plane == NULL) {
1010             plane_is_null = 1;
1011             view->plane = &compositor->primary_plane;
1012         }
1013
1014         get_gl_surface_rectangle(prop, &x, &y, &width, &height);
1015         dump_surface(link_scrn->output,
1016                      compositor,
1017                      filename,
1018                      view,
1019                      x,
1020                      y,
1021                      width,
1022                      height);
1023
1024         if (plane_is_null) {
1025             view->plane = NULL;
1026         }
1027     }
1028
1029     return 0;
1030 }
1031
1032 static void
1033 controller_surface_screenshot(struct wl_client *client,
1034                   struct wl_resource *resource,
1035                   const char *filename)
1036 {
1037     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
1038     struct weston_surface *weston_surface = NULL;
1039     int32_t width = 0;
1040     int32_t height = 0;
1041     int32_t stride = 0;
1042
1043     weston_surface = ivi_layout_surface_get_weston_surface(ivisurf->layout_surface);
1044     if (weston_surface == NULL) {
1045         fprintf(stderr, "Failed to get weston surface.\n");
1046         return;
1047     }
1048
1049     if (ivi_layout_surface_get_size(ivisurf->layout_surface, &width, &height, &stride) != 0) {
1050         fprintf(stderr, "Failed to get surface size.\n");
1051         return;
1052     }
1053
1054     if (shm_surface_screenshot(weston_surface, width, height, stride, filename) != 0) {
1055         if (gl_surface_screenshot(ivisurf, weston_surface, filename) != 0) {
1056             fprintf(stderr, "Failed to capture surface.\n");
1057         }
1058     }
1059 }
1060
1061
1062 static void
1063 controller_surface_send_stats(struct wl_client *client,
1064                               struct wl_resource *resource)
1065 {
1066     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
1067     pid_t pid;
1068     uid_t uid;
1069     gid_t gid;
1070     wl_client_get_credentials(client, &pid, &uid, &gid);
1071
1072     ivi_controller_surface_send_stats(resource, 0, 0,
1073                                       ivisurf->update_count, pid, "");
1074 }
1075
1076 static void
1077 controller_surface_destroy(struct wl_client *client,
1078               struct wl_resource *resource,
1079               int32_t destroy_scene_object)
1080 {
1081     (void)client;
1082     (void)destroy_scene_object;
1083     wl_resource_destroy(resource);
1084 }
1085
1086 static void send_all_keyboard_focus(struct ivishell *shell)
1087 {
1088 #ifdef SUPPORT_INPUT_FOCUS
1089     const struct ivi_layout_surface_properties *props;
1090     struct ivicontroller_surface *ctrlsurf;
1091     struct ivisurface *current_surf;
1092     uint32_t id_surface;
1093
1094     wl_list_for_each(current_surf, &shell->list_surface, link) {
1095         if (!(props = ivi_layout_get_properties_of_surface(current_surf->layout_surface)))
1096             props = &no_surface_props;
1097
1098         id_surface = ivi_layout_get_id_of_surface(current_surf->layout_surface);
1099         wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
1100             if (id_surface != ctrlsurf->id_surface) {
1101                     continue;
1102             }
1103             ivi_controller_surface_send_input_focus(ctrlsurf->resource,
1104                             IVI_CONTROLLER_SURFACE_INPUT_DEVICE_KEYBOARD,
1105                             props->has_keyboard_focus);
1106         }
1107     }
1108 #endif
1109 }
1110
1111 static void
1112 controller_surface_set_input_focus(struct wl_client *client,
1113               struct wl_resource *resource,
1114               uint32_t device,
1115               int32_t enabled)
1116 {
1117 #ifdef SUPPORT_INPUT_FOCUS
1118     (void)client;
1119     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
1120
1121     if (device & IVI_CONTROLLER_SURFACE_INPUT_DEVICE_KEYBOARD) {
1122         if (enabled) {
1123             uint32_t id = ivi_layout_get_id_of_surface(ivisurf->layout_surface);
1124             ivi_layout_set_keyboard_focus_on(&id, 1);
1125             send_all_keyboard_focus(ivisurf->shell);
1126         }
1127     }
1128 #endif
1129 }
1130
1131 static const
1132 struct ivi_controller_surface_interface controller_surface_implementation = {
1133     controller_surface_set_visibility,
1134     controller_surface_set_opacity,
1135     controller_surface_set_source_rectangle,
1136     controller_surface_set_destination_rectangle,
1137     controller_surface_set_configuration,
1138     controller_surface_set_orientation,
1139     controller_surface_screenshot,
1140     controller_surface_send_stats,
1141     controller_surface_destroy,
1142     controller_surface_set_input_focus
1143 };
1144
1145 static void
1146 controller_layer_set_source_rectangle(struct wl_client *client,
1147                    struct wl_resource *resource,
1148                    int32_t x,
1149                    int32_t y,
1150                    int32_t width,
1151                    int32_t height)
1152 {
1153     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1154     (void)client;
1155     ivi_layout_layer_set_source_rectangle(ivilayer->layout_layer,
1156            (uint32_t)x, (uint32_t)y, (uint32_t)width, (uint32_t)height);
1157 }
1158
1159 static void
1160 controller_layer_set_destination_rectangle(struct wl_client *client,
1161                  struct wl_resource *resource,
1162                  int32_t x,
1163                  int32_t y,
1164                  int32_t width,
1165                  int32_t height)
1166 {
1167     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1168     (void)client;
1169     ivi_layout_layer_set_destination_rectangle(ivilayer->layout_layer,
1170             (uint32_t)x, (uint32_t)y, (uint32_t)width, (uint32_t)height);
1171 }
1172
1173 static void
1174 controller_layer_set_visibility(struct wl_client *client,
1175                     struct wl_resource *resource,
1176                     uint32_t visibility)
1177 {
1178     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1179     (void)client;
1180     ivi_layout_layer_set_visibility(ivilayer->layout_layer, visibility);
1181 }
1182
1183 static void
1184 controller_layer_set_opacity(struct wl_client *client,
1185                  struct wl_resource *resource,
1186                  wl_fixed_t opacity)
1187 {
1188     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1189     (void)client;
1190     ivi_layout_layer_set_opacity(ivilayer->layout_layer,
1191                                  wl_fixed_to_double(opacity));
1192 }
1193
1194 static void
1195 controller_layer_set_configuration(struct wl_client *client,
1196                  struct wl_resource *resource,
1197                  int32_t width,
1198                  int32_t height)
1199 {
1200     /* This interface has been supported yet. */
1201     (void)client;
1202     (void)resource;
1203     (void)width;
1204     (void)height;
1205 }
1206
1207 static void
1208 controller_layer_set_orientation(struct wl_client *client,
1209                  struct wl_resource *resource,
1210                  int32_t orientation)
1211 {
1212     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1213     (void)client;
1214     ivi_layout_layer_set_orientation(ivilayer->layout_layer, (uint32_t)orientation);
1215 }
1216
1217 static void
1218 controller_layer_clear_surfaces(struct wl_client *client,
1219                     struct wl_resource *resource)
1220 {
1221     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1222     (void)client;
1223     ivi_layout_layer_set_render_order(ivilayer->layout_layer, NULL, 0);
1224 }
1225
1226 static void
1227 controller_layer_add_surface(struct wl_client *client,
1228                  struct wl_resource *resource,
1229                  struct wl_resource *surface)
1230 {
1231     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1232     struct ivisurface *ivisurf = wl_resource_get_user_data(surface);
1233     (void)client;
1234     ivi_layout_layer_add_surface(ivilayer->layout_layer, ivisurf->layout_surface);
1235 }
1236
1237 static void
1238 controller_layer_remove_surface(struct wl_client *client,
1239                     struct wl_resource *resource,
1240                     struct wl_resource *surface)
1241 {
1242     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1243     struct ivisurface *ivisurf = wl_resource_get_user_data(surface);
1244     (void)client;
1245     ivi_layout_layer_remove_surface(ivilayer->layout_layer, ivisurf->layout_surface);
1246 }
1247
1248 static void
1249 controller_layer_screenshot(struct wl_client *client,
1250                 struct wl_resource *resource,
1251                 const char *filename)
1252 {
1253     (void)client;
1254     (void)resource;
1255     (void)filename;
1256 }
1257
1258 static void
1259 controller_layer_set_render_order(struct wl_client *client,
1260                                   struct wl_resource *resource,
1261                                   struct wl_array *id_surfaces)
1262 {
1263     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1264     struct ivi_layout_surface **layoutsurf_array = NULL;
1265     struct ivisurface *ivisurf = NULL;
1266     uint32_t *id_surface = NULL;
1267     uint32_t id_layout_surface = 0;
1268     int i = 0;
1269     (void)client;
1270
1271     layoutsurf_array = (struct ivi_layout_surface**)calloc(
1272                            id_surfaces->size, sizeof(void*));
1273
1274     wl_array_for_each(id_surface, id_surfaces) {
1275         wl_list_for_each(ivisurf, &ivilayer->shell->list_surface, link) {
1276             id_layout_surface = ivi_layout_get_id_of_surface(ivisurf->layout_surface);
1277             if (*id_surface == id_layout_surface) {
1278                 layoutsurf_array[i] = ivisurf->layout_surface;
1279                 i++;
1280                 break;
1281             }
1282         }
1283     }
1284
1285     ivi_layout_layer_set_render_order(ivilayer->layout_layer,
1286                                       layoutsurf_array, i);
1287     free(layoutsurf_array);
1288 }
1289
1290 static void
1291 controller_layer_destroy(struct wl_client *client,
1292               struct wl_resource *resource,
1293               int32_t destroy_scene_object)
1294 {
1295     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1296     struct ivishell *shell = ivilayer->shell;
1297     struct ivicontroller_layer *ctrllayer = NULL;
1298     struct ivicontroller_layer *next = NULL;
1299     uint32_t id_layer = ivi_layout_get_id_of_layer(ivilayer->layout_layer);
1300     (void)client;
1301     (void)destroy_scene_object;
1302
1303     ivilayer->layer_canbe_removed = 1;
1304     wl_list_for_each_safe(ctrllayer, next, &shell->list_controller_layer, link) {
1305         if (ctrllayer->id_layer != id_layer) {
1306             continue;
1307         }
1308
1309         wl_resource_destroy(ctrllayer->resource);
1310     }
1311 }
1312
1313 static const
1314 struct ivi_controller_layer_interface controller_layer_implementation = {
1315     controller_layer_set_visibility,
1316     controller_layer_set_opacity,
1317     controller_layer_set_source_rectangle,
1318     controller_layer_set_destination_rectangle,
1319     controller_layer_set_configuration,
1320     controller_layer_set_orientation,
1321     controller_layer_screenshot,
1322     controller_layer_clear_surfaces,
1323     controller_layer_add_surface,
1324     controller_layer_remove_surface,
1325     controller_layer_set_render_order,
1326     controller_layer_destroy
1327 };
1328
1329 static void
1330 controller_screen_destroy(struct wl_client *client,
1331                           struct wl_resource *resource)
1332 {
1333     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1334     struct ivicontroller_screen *ctrlscrn = NULL;
1335     struct ivicontroller_screen *next = NULL;
1336 //    uint32_t id_screen = ivi_layout_get_id_of_screen(iviscrn->layout_screen);
1337     (void)client;
1338
1339     wl_list_for_each_safe(ctrlscrn, next,
1340                           &iviscrn->shell->list_controller_screen, link) {
1341         if (resource != ctrlscrn->resource) {
1342             continue;
1343         }
1344
1345         wl_list_remove(&ctrlscrn->link);
1346         wl_resource_destroy(ctrlscrn->resource);
1347         free(ctrlscrn);
1348         ctrlscrn = NULL;
1349         break;
1350     }
1351 }
1352
1353 static void
1354 controller_screen_clear(struct wl_client *client,
1355                 struct wl_resource *resource)
1356 {
1357     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1358     (void)client;
1359     ivi_layout_screen_set_render_order(iviscrn->layout_screen, NULL, 0);
1360 }
1361
1362 static void
1363 controller_screen_add_layer(struct wl_client *client,
1364                 struct wl_resource *resource,
1365                 struct wl_resource *layer)
1366 {
1367     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1368     struct ivilayer *ivilayer = wl_resource_get_user_data(layer);
1369     (void)client;
1370     ivi_layout_screen_add_layer(iviscrn->layout_screen, ivilayer->layout_layer);
1371 }
1372
1373 static void
1374 controller_screen_screenshot(struct wl_client *client,
1375                 struct wl_resource *resource,
1376                 const char *filename)
1377 {
1378     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1379     (void)client;
1380
1381     struct weston_output *output = NULL;
1382     cairo_surface_t *cairo_surf = NULL;
1383     int32_t i = 0;
1384     int32_t width = 0;
1385     int32_t height = 0;
1386     int32_t stride = 0;
1387     uint8_t *readpixs = NULL;
1388
1389     output = ivi_layout_screen_get_output(iviscrn->layout_screen);
1390     --output->disable_planes;
1391
1392     width = output->current_mode->width;
1393     height = output->current_mode->height;
1394     stride = width * (PIXMAN_FORMAT_BPP(output->compositor->read_format) / 8);
1395
1396     readpixs = malloc(stride * height);
1397     if (readpixs == NULL) {
1398         weston_log("fails to allocate memory\n");
1399         return;
1400     }
1401
1402     output->compositor->renderer->read_pixels(
1403             output,
1404             output->compositor->read_format,
1405             readpixs,
1406             0,
1407             0,
1408             width,
1409             height);
1410
1411     save_as_bitmap(filename, readpixs, stride * height, width, height, PIXMAN_FORMAT_BPP(output->compositor->read_format));
1412     free(readpixs);
1413 }
1414
1415 static void
1416 controller_screen_set_render_order(struct wl_client *client,
1417                 struct wl_resource *resource,
1418                 struct wl_array *id_layers)
1419 {
1420     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1421     struct ivi_layout_layer **layoutlayer_array = NULL;
1422     struct ivilayer *ivilayer = NULL;
1423     uint32_t *id_layer = NULL;
1424     uint32_t id_layout_layer = 0;
1425     int i = 0;
1426     (void)client;
1427
1428     layoutlayer_array = (struct ivi_layout_layer**)calloc(
1429                            id_layers->size, sizeof(void*));
1430
1431     wl_array_for_each(id_layer, id_layers) {
1432         wl_list_for_each(ivilayer, &iviscrn->shell->list_layer, link) {
1433             id_layout_layer = ivi_layout_get_id_of_layer(ivilayer->layout_layer);
1434             if (*id_layer == id_layout_layer) {
1435                 layoutlayer_array[i] = ivilayer->layout_layer;
1436                 i++;
1437                 break;
1438             }
1439         }
1440     }
1441
1442     ivi_layout_screen_set_render_order(iviscrn->layout_screen,
1443                                        layoutlayer_array, i);
1444     free(layoutlayer_array);
1445 }
1446
1447 static const
1448 struct ivi_controller_screen_interface controller_screen_implementation = {
1449     controller_screen_destroy,
1450     controller_screen_clear,
1451     controller_screen_add_layer,
1452     controller_screen_screenshot,
1453     controller_screen_set_render_order
1454 };
1455
1456 static void
1457 controller_commit_changes(struct wl_client *client,
1458                           struct wl_resource *resource)
1459 {
1460     int32_t ans = 0;
1461     (void)client;
1462     (void)resource;
1463
1464     ans = ivi_layout_commit_changes();
1465     if (ans < 0) {
1466         weston_log("Failed to commit changes at controller_commit_changes\n");
1467     }
1468 }
1469
1470 static void
1471 controller_layer_create(struct wl_client *client,
1472                         struct wl_resource *resource,
1473                         uint32_t id_layer,
1474                         int32_t width,
1475                         int32_t height,
1476                         uint32_t id)
1477 {
1478     struct ivicontroller *ctrl = wl_resource_get_user_data(resource);
1479     struct ivishell *shell = ctrl->shell;
1480     struct ivi_layout_layer *layout_layer = NULL;
1481     struct ivicontroller_layer *ctrllayer = NULL;
1482     struct ivilayer *ivilayer = NULL;
1483     const struct ivi_layout_layer_properties *prop;
1484
1485     ivilayer = get_layer(&shell->list_layer, id_layer);
1486     if (ivilayer == NULL) {
1487         layout_layer = ivi_layout_layer_create_with_dimension(id_layer,
1488                            (uint32_t)width, (uint32_t)height);
1489         if (layout_layer == NULL) {
1490             weston_log("id_layer is already created\n");
1491             return;
1492         }
1493
1494         /* ivilayer will be created by layer_event_create */
1495         ivilayer = get_layer(&shell->list_layer, id_layer);
1496         if (ivilayer == NULL) {
1497             weston_log("couldn't get layer object\n");
1498             return;
1499         }
1500     }
1501
1502     ctrllayer = calloc(1, sizeof *ctrllayer);
1503     if (!ctrllayer) {
1504         weston_log("no memory to allocate client layer\n");
1505         return;
1506     }
1507
1508     ++ivilayer->controller_layer_count;
1509     ivilayer->layer_canbe_removed = 0;
1510
1511     ctrllayer->shell = shell;
1512     ctrllayer->client = client;
1513     ctrllayer->id = id;
1514     ctrllayer->id_layer = id_layer;
1515     ctrllayer->resource = wl_resource_create(client,
1516                                &ivi_controller_layer_interface, 1, id);
1517     if (ctrllayer->resource == NULL) {
1518         weston_log("couldn't get layer object\n");
1519         return;
1520     }
1521
1522     wl_list_init(&ctrllayer->link);
1523     wl_list_insert(&shell->list_controller_layer, &ctrllayer->link);
1524
1525     wl_resource_set_implementation(ctrllayer->resource,
1526                                    &controller_layer_implementation,
1527                                    ivilayer, destroy_ivicontroller_layer);
1528
1529     if (!(prop = ivi_layout_get_properties_of_layer(ivilayer->layout_layer)))
1530       prop = &no_layer_props;
1531
1532     send_layer_event(ctrllayer->resource, ivilayer,
1533                      prop, IVI_NOTIFICATION_ALL);
1534 }
1535
1536 static void
1537 surface_event_content(struct ivi_layout_surface *layout_surface, int32_t content, void *userdata)
1538 {
1539     struct ivishell *shell = userdata;
1540     struct ivicontroller_surface *ctrlsurf = NULL;
1541     uint32_t id_surface = 0;
1542
1543     if (content == 0) {
1544         surface_event_remove(layout_surface, userdata);
1545     }
1546 }
1547
1548 static void
1549 controller_surface_create(struct wl_client *client,
1550                           struct wl_resource *resource,
1551                           uint32_t id_surface,
1552                           uint32_t id)
1553 {
1554     struct ivicontroller *ctrl = wl_resource_get_user_data(resource);
1555     struct ivishell *shell = ctrl->shell;
1556     struct ivicontroller_surface *ctrlsurf = NULL;
1557     const struct ivi_layout_surface_properties *prop;
1558     struct ivisurface *ivisurf = NULL;
1559     struct ivicontroller_surface *ctrl_link = NULL;
1560
1561     ivisurf = get_surface(&shell->list_surface, id_surface);
1562     if (ivisurf == NULL) {
1563         return;
1564     }
1565
1566     ctrlsurf = calloc(1, sizeof *ctrlsurf);
1567     if (!ctrlsurf) {
1568         weston_log("no memory to allocate controller surface\n");
1569         return;
1570     }
1571
1572     ctrlsurf->shell = shell;
1573     ctrlsurf->client = client;
1574     ctrlsurf->id = id;
1575     ctrlsurf->id_surface = id_surface;
1576     wl_list_init(&ctrlsurf->link);
1577     wl_list_insert(&shell->list_controller_surface, &ctrlsurf->link);
1578
1579     ctrlsurf->resource = wl_resource_create(client,
1580                                &ivi_controller_surface_interface, 1, id);
1581     if (ctrlsurf->resource == NULL) {
1582         weston_log("couldn't surface object");
1583         return;
1584     }
1585
1586     wl_list_for_each(ctrl_link, &shell->list_controller_surface, link) {
1587         if ((ctrl_link->implementation_set == 0) &&
1588             (ctrl_link->id_surface == id_surface) &&
1589             (ctrl_link->shell == shell) &&
1590             (ctrl_link->client != client)) {
1591             ++ivisurf->controller_surface_count;
1592             wl_resource_set_implementation(ctrl_link->resource,
1593                                            &controller_surface_implementation,
1594                                            ivisurf, destroy_ivicontroller_surface);
1595             ctrl_link->implementation_set = 1;
1596         }
1597     }
1598
1599     ++ivisurf->controller_surface_count;
1600
1601     wl_resource_set_implementation(ctrlsurf->resource,
1602                                    &controller_surface_implementation,
1603                                    ivisurf, destroy_ivicontroller_surface);
1604
1605     ctrlsurf->implementation_set = 1;
1606
1607     if (!(prop = ivi_layout_get_properties_of_surface(ivisurf->layout_surface)))
1608       prop = &no_surface_props;
1609
1610     ivi_layout_surface_set_content_observer(ivisurf->layout_surface, surface_event_content, shell);
1611
1612     send_surface_event(ctrlsurf->resource, ivisurf,
1613                        prop, IVI_NOTIFICATION_ALL);
1614 }
1615
1616 static void
1617 controller_get_native_handle(struct wl_client *client,
1618                              struct wl_resource *resource,
1619                              uint32_t id_process,
1620                              const char *title)
1621 {
1622     struct wl_array surfaces;
1623     ivi_shell_get_shell_surfaces(&surfaces);
1624     struct shell_surface ** surface;
1625
1626     wl_array_for_each(surface, &surfaces) {
1627
1628         uint32_t pid = shell_surface_get_process_id(*surface);
1629         if (pid != id_process) {
1630             continue;
1631         }
1632
1633         if (title) {
1634             char* surface_title = shell_surface_get_title(*surface);
1635             if (strcmp(title, surface_title)) {
1636                 continue;
1637             }
1638         }
1639
1640         struct weston_surface *es = shell_surface_get_surface(*surface);
1641         if (es) {
1642             struct wl_resource *res = wl_resource_create(client, &wl_surface_interface, 1, 0);
1643             wl_resource_set_user_data(res, es);
1644
1645             ivi_controller_send_native_handle(resource, res);
1646         }
1647     }
1648
1649     wl_array_release(&surfaces);
1650
1651     int32_t id_object = 0;
1652     ivi_controller_send_error(
1653         resource, id_object, IVI_CONTROLLER_OBJECT_TYPE_SURFACE,
1654         IVI_CONTROLLER_ERROR_CODE_NATIVE_HANDLE_END, "");
1655 }
1656
1657 static void
1658 controller_set_keyboard_focus(struct wl_client *client,
1659                               struct wl_resource *resource,
1660                               struct wl_array *surfaces)
1661 {
1662 #ifdef SUPPORT_INPUT_FOCUS
1663     struct ivicontroller *ctrl = wl_resource_get_user_data(resource);
1664     ivi_layout_set_keyboard_focus_on(surfaces->data, surfaces->size);
1665     send_all_keyboard_focus(ctrl->shell);
1666 #endif
1667 }
1668
1669 static const struct ivi_controller_interface controller_implementation = {
1670     controller_commit_changes,
1671     controller_layer_create,
1672     controller_surface_create,
1673     controller_set_keyboard_focus,
1674     controller_get_native_handle
1675 };
1676
1677 static void
1678 add_client_to_resources(struct ivishell *shell,
1679                         struct wl_client *client,
1680                         struct ivicontroller *controller)
1681 {
1682     struct ivisurface* ivisurf = NULL;
1683     struct ivilayer* ivilayer = NULL;
1684     struct iviscreen* iviscrn = NULL;
1685     struct ivicontroller_screen *ctrlscrn = NULL;
1686     struct wl_resource *resource_output = NULL;
1687     uint32_t id_layout_surface = 0;
1688     uint32_t id_layout_layer = 0;
1689     struct ivi_layout_surface *layout_surface;
1690     int32_t pid;
1691     const char *window_title;
1692
1693     wl_list_for_each(iviscrn, &shell->list_screen, link) {
1694         resource_output = wl_resource_find_for_client(
1695                 &iviscrn->output->resource_list, client);
1696         if (resource_output == NULL) {
1697             continue;
1698         }
1699
1700         ctrlscrn = controller_screen_create(iviscrn->shell, client, iviscrn);
1701         if (ctrlscrn == NULL) {
1702             continue;
1703         }
1704
1705         ivi_controller_send_screen(controller->resource,
1706                                    wl_resource_get_id(resource_output),
1707                                    ctrlscrn->resource);
1708     }
1709     wl_list_for_each_reverse(ivilayer, &shell->list_layer, link) {
1710         id_layout_layer =
1711             ivi_layout_get_id_of_layer(ivilayer->layout_layer);
1712
1713         ivi_controller_send_layer(controller->resource,
1714                                   id_layout_layer);
1715     }
1716     wl_list_for_each_reverse(ivisurf, &shell->list_surface, link) {
1717         layout_surface = ivisurf->layout_surface;
1718         id_layout_surface = ivi_layout_get_id_of_surface(layout_surface);
1719
1720         get_wl_shell_info(layout_surface, id_layout_surface,
1721                           &pid, &window_title);
1722
1723         ivi_controller_send_surface(controller->resource,
1724                                     id_layout_surface, pid, window_title);
1725     }
1726 }
1727
1728 static void
1729 bind_ivi_controller(struct wl_client *client, void *data,
1730                     uint32_t version, uint32_t id)
1731 {
1732     struct ivishell *shell = data;
1733     struct ivicontroller *controller;
1734     (void)version;
1735
1736     controller = calloc(1, sizeof *controller);
1737     if (controller == NULL) {
1738         weston_log("no memory to allocate controller\n");
1739         return;
1740     }
1741
1742     controller->resource =
1743         wl_resource_create(client, &ivi_controller_interface, 1, id);
1744     wl_resource_set_implementation(controller->resource,
1745                                    &controller_implementation,
1746                                    controller, unbind_resource_controller);
1747
1748     controller->shell = shell;
1749     controller->client = client;
1750     controller->id = id;
1751
1752     wl_list_init(&controller->link);
1753     wl_list_insert(&shell->list_controller, &controller->link);
1754
1755     add_client_to_resources(shell, client, controller);
1756 }
1757
1758 static struct iviscreen*
1759 create_screen(struct ivishell *shell, struct weston_output *output)
1760 {
1761     struct iviscreen *iviscrn;
1762     iviscrn = calloc(1, sizeof *iviscrn);
1763     if (iviscrn == NULL) {
1764         weston_log("no memory to allocate client screen\n");
1765         return NULL;
1766     }
1767
1768     iviscrn->shell = shell;
1769     iviscrn->output = output;
1770
1771 // TODO : Only Single display
1772     iviscrn->layout_screen = ivi_layout_get_screen_from_id(0);
1773
1774     wl_list_init(&iviscrn->link);
1775
1776     return iviscrn;
1777 }
1778
1779 static struct ivilayer*
1780 create_layer(struct ivishell *shell,
1781              struct ivi_layout_layer *layout_layer,
1782              uint32_t id_layer)
1783 {
1784     struct ivilayer *ivilayer = NULL;
1785     struct ivicontroller *controller = NULL;
1786
1787     ivilayer = get_layer(&shell->list_layer, id_layer);
1788     if (ivilayer != NULL) {
1789         weston_log("id_layer is already created\n");
1790         return NULL;
1791     }
1792
1793     ivilayer = calloc(1, sizeof *ivilayer);
1794     if (NULL == ivilayer) {
1795         weston_log("no memory to allocate client layer\n");
1796         return NULL;
1797     }
1798
1799     ivilayer->shell = shell;
1800     wl_list_init(&ivilayer->list_screen);
1801     wl_list_init(&ivilayer->link);
1802     wl_list_insert(&shell->list_layer, &ivilayer->link);
1803     ivilayer->layout_layer = layout_layer;
1804
1805     ivi_layout_layer_add_notification(layout_layer, send_layer_prop, ivilayer);
1806
1807     wl_list_for_each(controller, &shell->list_controller, link) {
1808         ivi_controller_send_layer(controller->resource, id_layer);
1809     }
1810
1811     return ivilayer;
1812 }
1813
1814 static struct ivisurface*
1815 create_surface(struct ivishell *shell,
1816                struct ivi_layout_surface *layout_surface,
1817                uint32_t id_surface)
1818 {
1819     struct ivisurface *ivisurf = NULL;
1820     struct ivicontroller *controller = NULL;
1821     int32_t pid;
1822     const char *window_title;
1823
1824     ivisurf = get_surface(&shell->list_surface, id_surface);
1825     if (ivisurf != NULL) {
1826         weston_log("id_surface is already created\n");
1827         return NULL;
1828     }
1829
1830     ivisurf = calloc(1, sizeof *ivisurf);
1831     if (ivisurf == NULL) {
1832         weston_log("no memory to allocate client surface\n");
1833         return NULL;
1834     }
1835
1836     ivisurf->shell = shell;
1837     ivisurf->layout_surface = layout_surface;
1838     wl_list_init(&ivisurf->list_layer);
1839     wl_list_init(&ivisurf->link);
1840     wl_list_insert(&shell->list_surface, &ivisurf->link);
1841
1842     get_wl_shell_info(layout_surface, id_surface, &pid, &window_title);
1843
1844     wl_list_for_each(controller, &shell->list_controller, link) {
1845         ivi_controller_send_surface(controller->resource,
1846                                     id_surface, pid, window_title);
1847     }
1848
1849     ivi_layout_surface_add_notification(layout_surface,
1850                                         send_surface_prop, ivisurf);
1851
1852     return ivisurf;
1853 }
1854
1855 static void
1856 layer_event_create(struct ivi_layout_layer *layout_layer,
1857                      void *userdata)
1858 {
1859     struct ivishell *shell = userdata;
1860     struct ivilayer *ivilayer = NULL;
1861     uint32_t id_layer = 0;
1862
1863     id_layer = ivi_layout_get_id_of_layer(layout_layer);
1864
1865     ivilayer = create_layer(shell, layout_layer, id_layer);
1866     if (ivilayer == NULL) {
1867         weston_log("failed to create layer");
1868         return;
1869     }
1870 }
1871
1872 static void
1873 layer_event_remove(struct ivi_layout_layer *layout_layer,
1874                    void *userdata)
1875 {
1876     struct ivishell *shell = userdata;
1877     struct ivicontroller_layer *ctrllayer = NULL;
1878     struct ivilayer *ivilayer = NULL;
1879     struct ivilayer *next = NULL;
1880     uint32_t id_layer = 0;
1881     int is_removed = 0;
1882
1883     wl_list_for_each_safe(ivilayer, next, &shell->list_layer, link) {
1884         if (layout_layer != ivilayer->layout_layer) {
1885             continue;
1886         }
1887
1888         wl_list_remove(&ivilayer->link);
1889
1890         is_removed = 1;
1891         free(ivilayer);
1892         ivilayer = NULL;
1893         break;
1894     }
1895
1896     if (is_removed) {
1897         id_layer = ivi_layout_get_id_of_layer(layout_layer);
1898
1899         wl_list_for_each(ctrllayer, &shell->list_controller_layer, link) {
1900             if (id_layer != ctrllayer->id_layer) {
1901                 continue;
1902             }
1903             ivi_controller_layer_send_destroyed(ctrllayer->resource);
1904         }
1905     }
1906 }
1907
1908
1909 static void
1910 surface_event_create(struct ivi_layout_surface *layout_surface,
1911                      void *userdata)
1912 {
1913     struct ivishell *shell = userdata;
1914     struct ivisurface *ivisurf = NULL;
1915     uint32_t id_surface = 0;
1916     struct ivicontroller_surface *ctrlsurf = NULL;
1917
1918     id_surface = ivi_layout_get_id_of_surface(layout_surface);
1919
1920     ivisurf = create_surface(shell, layout_surface, id_surface);
1921     if (ivisurf == NULL) {
1922         weston_log("failed to create surface");
1923         return;
1924     }
1925
1926     wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
1927         if (id_surface != ctrlsurf->id_surface) {
1928             continue;
1929         }
1930         ivi_controller_surface_send_content(ctrlsurf->resource, IVI_CONTROLLER_SURFACE_CONTENT_STATE_CONTENT_AVAILABLE);
1931     }
1932 }
1933
1934 static void
1935 surface_event_remove(struct ivi_layout_surface *layout_surface,
1936                      void *userdata)
1937 {
1938     struct ivishell *shell = userdata;
1939     struct ivicontroller_surface *ctrlsurf = NULL;
1940     struct ivisurface *ivisurf = NULL;
1941     struct ivisurface *next = NULL;
1942     uint32_t id_surface = 0;
1943     int is_removed = 0;
1944
1945     wl_list_for_each_safe(ivisurf, next, &shell->list_surface, link) {
1946         if (layout_surface != ivisurf->layout_surface) {
1947             continue;
1948         }
1949
1950         wl_list_remove(&ivisurf->link);
1951         is_removed = 1;
1952
1953         if (ivisurf->controller_surface_count == 0) {
1954             free(ivisurf);
1955         }
1956         else {
1957             ivisurf->can_be_removed = 1;
1958         }
1959
1960         break;
1961     }
1962
1963     if (is_removed) {
1964         id_surface = ivi_layout_get_id_of_surface(layout_surface);
1965
1966         wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
1967             if (id_surface != ctrlsurf->id_surface) {
1968                 continue;
1969             }
1970             ivi_controller_surface_send_content(ctrlsurf->resource, IVI_CONTROLLER_SURFACE_CONTENT_STATE_CONTENT_REMOVED);
1971             ivi_controller_surface_send_destroyed(ctrlsurf->resource);
1972         }
1973     }
1974 }
1975
1976 static void
1977 surface_event_configure(struct ivi_layout_surface *layout_surface,
1978                         void *userdata)
1979 {
1980     struct ivishell *shell = userdata;
1981     struct ivisurface *ivisurf = NULL;
1982     struct ivicontroller_surface *ctrlsurf = NULL;
1983     struct ivi_layout_surface_properties *prop;
1984     uint32_t id_surface = 0;
1985
1986     id_surface = ivi_layout_get_id_of_surface(layout_surface);
1987
1988     ivisurf = get_surface(&shell->list_surface, id_surface);
1989     if (ivisurf == NULL) {
1990         weston_log("id_surface is not created yet\n");
1991         return;
1992     }
1993
1994     if (!(prop = ivi_layout_get_properties_of_surface(layout_surface)))
1995       prop = &no_surface_props;
1996
1997     wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
1998         if (id_surface != ctrlsurf->id_surface) {
1999             continue;
2000         }
2001         send_surface_event(ctrlsurf->resource, ivisurf,
2002                            prop, IVI_NOTIFICATION_ALL);
2003     }
2004 }
2005
2006 static int32_t
2007 check_layout_layers(struct ivishell *shell)
2008 {
2009     struct ivi_layout_layer **pArray = NULL;
2010     struct ivilayer *ivilayer = NULL;
2011     uint32_t id_layer = 0;
2012     uint32_t length = 0;
2013     uint32_t i = 0;
2014     int32_t ret = 0;
2015
2016     ret = ivi_layout_get_layers(&length, &pArray);
2017     if(ret != 0) {
2018         weston_log("failed to get layers at check_layout_layers\n");
2019         return -1;
2020     }
2021
2022     if (length == 0) {
2023         /* if length is 0, pArray doesn't need to free.*/
2024         return 0;
2025     }
2026
2027     for (i = 0; i < length; i++) {
2028         id_layer = ivi_layout_get_id_of_layer(pArray[i]);
2029         ivilayer = create_layer(shell, pArray[i], id_layer);
2030         if (ivilayer == NULL) {
2031             weston_log("failed to create layer");
2032         }
2033     }
2034
2035     free(pArray);
2036     pArray = NULL;
2037
2038     return 0;
2039 }
2040
2041 static int32_t
2042 check_layout_surfaces(struct ivishell *shell)
2043 {
2044     struct ivi_layout_surface **pArray = NULL;
2045     struct ivisurface *ivisurf = NULL;
2046     uint32_t id_surface = 0;
2047     uint32_t length = 0;
2048     uint32_t i = 0;
2049     int32_t ret = 0;
2050
2051     ret = ivi_layout_get_surfaces(&length, &pArray);
2052     if(ret != 0) {
2053         weston_log("failed to get surfaces at check_layout_surfaces\n");
2054         return -1;
2055     }
2056
2057     if (length == 0) {
2058         /* if length is 0, pArray doesn't need to free.*/
2059         return 0;
2060     }
2061
2062     for (i = 0; i < length; i++) {
2063         id_surface = ivi_layout_get_id_of_surface(pArray[i]);
2064         ivisurf = create_surface(shell, pArray[i], id_surface);
2065         if (ivisurf == NULL) {
2066             weston_log("failed to create surface");
2067         }
2068     }
2069
2070     free(pArray);
2071     pArray = NULL;
2072
2073     return 0;
2074 }
2075
2076 static void
2077 init_ivi_shell(struct weston_compositor *ec, struct ivishell *shell)
2078 {
2079     struct weston_output *output = NULL;
2080     struct iviscreen *iviscrn = NULL;
2081     int32_t ret = 0;
2082
2083     shell->compositor = ec;
2084
2085     wl_list_init(&shell->list_surface);
2086     wl_list_init(&shell->list_layer);
2087     wl_list_init(&shell->list_screen);
2088     wl_list_init(&shell->list_weston_surface);
2089     wl_list_init(&shell->list_controller);
2090     wl_list_init(&shell->list_controller_screen);
2091     wl_list_init(&shell->list_controller_layer);
2092     wl_list_init(&shell->list_controller_surface);
2093     shell->event_restriction = 0;
2094
2095     wl_list_for_each(output, &ec->output_list, link) {
2096         iviscrn = create_screen(shell, output);
2097         if (iviscrn != NULL) {
2098             wl_list_insert(&shell->list_screen, &iviscrn->link);
2099         }
2100     }
2101
2102     ret = check_layout_layers(shell);
2103     if (ret != 0) {
2104         weston_log("failed to check_layout_layers");
2105     }
2106
2107     ret = check_layout_surfaces(shell);
2108     if (ret != 0) {
2109         weston_log("failed to check_layout_surfaces");
2110     }
2111
2112     ivi_layout_add_notification_create_layer(layer_event_create, shell);
2113     ivi_layout_add_notification_remove_layer(layer_event_remove, shell);
2114
2115     ivi_layout_add_notification_create_surface(surface_event_create, shell);
2116     ivi_layout_add_notification_remove_surface(surface_event_remove, shell);
2117     ivi_layout_add_notification_configure_surface(surface_event_configure, shell);
2118 }
2119
2120 WL_EXPORT int
2121 module_init(struct weston_compositor *ec,
2122             int *argc, char *argv[])
2123 {
2124     struct ivishell *shell;
2125     (void)argc;
2126     (void)argv;
2127
2128     shell = malloc(sizeof *shell);
2129     if (shell == NULL)
2130         return -1;
2131
2132     memset(shell, 0, sizeof *shell);
2133     init_ivi_shell(ec, shell);
2134
2135     if (wl_global_create(ec->wl_display, &ivi_controller_interface, 1,
2136                          shell, bind_ivi_controller) == NULL) {
2137         return -1;
2138     }
2139
2140     return 0;
2141 }