ivi-controller: Create a link layer.
[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
31 #include "weston/compositor.h"
32 #include "ivi-controller-server-protocol.h"
33 #include "ivi-layout-export.h"
34
35 struct ivishell;
36 struct ivilayer;
37 struct iviscreen;
38
39 struct link_layer {
40     struct ivilayer *layer;
41     struct wl_list link;
42 };
43
44 struct link_screen {
45     struct iviscreen *screen;
46     struct wl_list link;
47 };
48
49 struct ivisurface {
50     struct wl_list link;
51     struct wl_client *client;
52     struct ivishell *shell;
53     uint32_t update_count;
54     struct ivi_layout_surface *layout_surface;
55     struct wl_listener surface_destroy_listener;
56     struct wl_list list_layer;
57     uint32_t controller_surface_count;
58     int can_be_removed;
59 };
60
61 struct ivilayer {
62     struct wl_list link;
63     struct ivishell *shell;
64     struct ivi_layout_layer *layout_layer;
65     struct wl_list list_screen;
66     uint32_t controller_layer_count;
67 };
68
69 struct iviscreen {
70     struct wl_list link;
71     struct ivishell *shell;
72     struct ivi_layout_screen *layout_screen;
73     struct weston_output *output;
74 };
75
76 struct ivicontroller_surface {
77     struct wl_resource *resource;
78     uint32_t id;
79     uint32_t id_surface;
80     struct wl_client *client;
81     struct wl_list link;
82     struct ivishell *shell;
83 };
84
85 struct ivicontroller_layer {
86     struct wl_resource *resource;
87     uint32_t id;
88     uint32_t id_layer;
89     struct wl_client *client;
90     struct wl_list link;
91     struct ivishell *shell;
92 };
93
94 struct ivicontroller_screen {
95     struct wl_resource *resource;
96     uint32_t id;
97     uint32_t id_screen;
98     struct wl_client *client;
99     struct wl_list link;
100     struct ivishell *shell;
101 };
102
103 struct ivicontroller {
104     struct wl_resource *resource;
105     uint32_t id;
106     struct wl_client *client;
107     struct wl_list link;
108     struct ivishell *shell;
109 };
110
111 struct link_shell_weston_surface
112 {
113     struct wl_resource *resource;
114     struct wl_listener destroy_listener;
115     struct weston_surface *surface;
116     struct wl_list link;
117 };
118
119 struct ivishell {
120     struct wl_resource *resource;
121
122     struct wl_listener destroy_listener;
123
124     struct weston_compositor *compositor;
125
126     struct weston_surface *surface;
127
128     struct weston_process process;
129
130     struct weston_seat *seat;
131
132     struct wl_list list_surface;
133     struct wl_list list_layer;
134     struct wl_list list_screen;
135
136     struct wl_list list_weston_surface;
137
138     struct wl_list list_controller;
139     struct wl_list list_controller_surface;
140     struct wl_list list_controller_layer;
141     struct wl_list list_controller_screen;
142
143     struct {
144         struct weston_process process;
145         struct wl_client *client;
146         struct wl_resource *desktop_shell;
147
148         unsigned deathcount;
149         uint32_t deathstamp;
150     } child;
151
152     int state;
153     int previous_state;
154     int event_restriction;
155 };
156 static void surface_event_remove(struct ivi_layout_surface *, void *);
157
158 static void
159 destroy_ivicontroller_surface(struct wl_resource *resource)
160 {
161     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
162     struct ivishell *shell = ivisurf->shell;
163     struct ivicontroller_surface *ctrlsurf = NULL;
164     struct ivicontroller_surface *next = NULL;
165     int is_removed = 0;
166
167     wl_list_for_each_safe(ctrlsurf, next,
168                           &shell->list_controller_surface, link) {
169
170         if (resource != ctrlsurf->resource) {
171             continue;
172         }
173
174         if (!wl_list_empty(&ctrlsurf->link)) {
175             wl_list_remove(&ctrlsurf->link);
176         }
177
178         is_removed = 1;
179         free(ctrlsurf);
180         ctrlsurf = NULL;
181         --ivisurf->controller_surface_count;
182         break;
183     }
184
185     if ((is_removed) && (ivisurf->controller_surface_count == 0)) {
186         if (ivisurf->can_be_removed) {
187             free(ivisurf);
188         }
189         else {
190             ivisurf->can_be_removed = 1;
191         }
192     }
193 }
194
195 static void
196 destroy_ivicontroller_layer(struct wl_resource *resource)
197 {
198     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
199     struct ivishell *shell = ivilayer->shell;
200     struct ivicontroller_layer *ctrllayer = NULL;
201     struct ivicontroller_layer *next = NULL;
202     uint32_t id_layer = 0;
203
204     id_layer = ivi_layout_getIdOfLayer(ivilayer->layout_layer);
205
206     wl_list_for_each_safe(ctrllayer, next,
207                           &shell->list_controller_layer, link) {
208
209         if (resource != ctrllayer->resource) {
210             continue;
211         }
212
213         wl_list_remove(&ctrllayer->link);
214         --ivilayer->controller_layer_count;
215         free(ctrllayer);
216         ctrllayer = NULL;
217         break;
218     }
219
220     if ((ivilayer->layout_layer != NULL) &&
221         (ivilayer->controller_layer_count == 0)) {
222         ivi_layout_layerRemove(ivilayer->layout_layer);
223     }
224 }
225
226 static void
227 destroy_ivicontroller_screen(struct wl_resource *resource)
228 {
229     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
230     struct ivicontroller_screen *ctrlscrn = NULL;
231     struct ivicontroller_screen *next = NULL;
232
233     wl_list_for_each_safe(ctrlscrn, next,
234                           &iviscrn->shell->list_controller_screen, link) {
235 // TODO : Only Single display
236 #if 0
237         if (iviscrn->output->id != ctrlscrn->id_screen) {
238             continue;
239         }
240 #endif
241
242         if (resource != ctrlscrn->resource) {
243             continue;
244         }
245
246         wl_list_remove(&ctrlscrn->link);
247         free(ctrlscrn);
248         ctrlscrn = NULL;
249         break;
250     }
251 }
252
253 static void
254 unbind_resource_controller(struct wl_resource *resource)
255 {
256     struct ivicontroller *controller = wl_resource_get_user_data(resource);
257
258     wl_list_remove(&controller->link);
259
260     free(controller);
261     controller = NULL;
262 }
263
264 static struct ivisurface*
265 get_surface(struct wl_list *list_surf, uint32_t id_surface)
266 {
267     struct ivisurface *ivisurf = NULL;
268     uint32_t ivisurf_id = 0;
269
270     wl_list_for_each(ivisurf, list_surf, link) {
271         ivisurf_id = ivi_layout_getIdOfSurface(ivisurf->layout_surface);
272         if (ivisurf_id == id_surface) {
273             return ivisurf;
274         }
275     }
276
277     return NULL;
278 }
279
280 static struct ivilayer*
281 get_layer(struct wl_list *list_layer, uint32_t id_layer)
282 {
283     struct ivilayer *ivilayer = NULL;
284     uint32_t ivilayer_id = 0;
285
286     wl_list_for_each(ivilayer, list_layer, link) {
287         ivilayer_id = ivi_layout_getIdOfLayer(ivilayer->layout_layer);
288         if (ivilayer_id == id_layer) {
289             return ivilayer;
290         }
291     }
292
293     return NULL;
294 }
295
296 static const
297 struct ivi_controller_screen_interface controller_screen_implementation;
298
299 static struct ivicontroller_screen*
300 controller_screen_create(struct ivishell *shell,
301                          struct wl_client *client,
302                          struct iviscreen *iviscrn)
303 {
304     struct ivicontroller_screen *ctrlscrn = NULL;
305
306     ctrlscrn = calloc(1, sizeof *ctrlscrn);
307     if (ctrlscrn == NULL) {
308         weston_log("no memory to allocate controller screen\n");
309         return NULL;
310     }
311
312     ctrlscrn->client = client;
313     ctrlscrn->shell  = shell;
314 // FIXME
315 // TODO : Only Single display
316 #if 0
317     /* ctrlscrn->id_screen = iviscrn->id_screen; */
318 #else
319     ctrlscrn->id_screen = 0;
320 #endif
321
322     ctrlscrn->resource =
323         wl_resource_create(client, &ivi_controller_screen_interface, 1, 0);
324     if (ctrlscrn->resource == NULL) {
325         weston_log("couldn't new screen controller object");
326
327         free(ctrlscrn);
328         ctrlscrn = NULL;
329
330         return NULL;
331     }
332
333     wl_resource_set_implementation(ctrlscrn->resource,
334                                    &controller_screen_implementation,
335                                    iviscrn, destroy_ivicontroller_screen);
336
337     wl_list_init(&ctrlscrn->link);
338     wl_list_insert(&shell->list_controller_screen, &ctrlscrn->link);
339
340     return ctrlscrn;
341 }
342
343 static void
344 send_surface_add_event(struct ivisurface *ivisurf,
345                        struct wl_resource *resource)
346 {
347     struct ivi_layout_layer **pArray = NULL;
348     uint32_t length = 0;
349     int32_t ans = 0;
350     int i = 0;
351     struct link_layer *link_layer = NULL;
352     struct link_layer *next = NULL;
353     struct ivicontroller_layer *ctrllayer = NULL;
354     struct ivilayer *ivilayer = NULL;
355     struct ivishell *shell = ivisurf->shell;
356     uint32_t id_layout_layer = 0;
357     int found = 0;
358
359     ans = ivi_layout_getLayersUnderSurface(ivisurf->layout_surface,
360                                           &length, &pArray);
361     if (0 != ans) {
362         weston_log("failed to get layers at send_surface_add_event\n");
363         return;
364     }
365
366     /* Send Null to cancel added surface */
367     wl_list_for_each_safe(link_layer, next, &ivisurf->list_layer, link) {
368         for (i = 0, found = 0; i < (int)length; i++) {
369             if (pArray[i] == link_layer->layer->layout_layer) {
370                 /* No need to send event, if new layer doesn't be added. */
371                 found = 1;
372                 break;
373             }
374         }
375         if (found != 0) {
376             continue;
377         }
378
379         ivi_controller_surface_send_layer(resource, NULL);
380         wl_list_remove(&link_layer->link);
381         free(link_layer);
382         link_layer = NULL;
383     }
384
385     for (i = 0; i < (int)length; i++) {
386         found = 0;
387         wl_list_for_each(link_layer, &ivisurf->list_layer, link) {
388             if (pArray[i] == link_layer->layer->layout_layer) {
389                 /* No need to send event, if new layer doesn't be added. */
390                 found = 1;
391                 break;
392             }
393         }
394         if (found != 0) {
395             continue;
396         }
397
398         /* Create list_layer */
399         link_layer = calloc(1, sizeof(*link_layer));
400         if (NULL == link_layer) {
401             continue;
402         }
403         wl_list_init(&link_layer->link);
404         link_layer->layer = NULL;
405         wl_list_for_each(ivilayer, &shell->list_layer, link) {
406             if (ivilayer->layout_layer == pArray[i]) {
407                 link_layer->layer = ivilayer;
408                 break;
409             }
410         }
411
412         if (link_layer->layer == NULL) {
413             free(link_layer);
414             link_layer = NULL;
415             continue;
416         }
417         wl_list_insert(&ivisurf->list_layer, &link_layer->link);
418
419         /* Send new surface event */
420         id_layout_layer =
421             ivi_layout_getIdOfLayer(link_layer->layer->layout_layer);
422         wl_list_for_each(ctrllayer, &shell->list_controller_layer, link) {
423             if (id_layout_layer != ctrllayer->id_layer) {
424                 continue;
425             }
426             ivi_controller_surface_send_layer(resource, ctrllayer->resource);
427         }
428     }
429
430     free(pArray);
431     pArray = NULL;
432 }
433
434 static void
435 send_surface_event(struct wl_resource *resource,
436                    struct ivisurface *ivisurf,
437                    struct ivi_layout_SurfaceProperties *prop,
438                    uint32_t mask)
439 {
440     if (mask & IVI_NOTIFICATION_OPACITY) {
441         ivi_controller_surface_send_opacity(resource,
442                                             prop->opacity);
443     }
444     if (mask & IVI_NOTIFICATION_SOURCE_RECT) {
445         ivi_controller_surface_send_source_rectangle(resource,
446             prop->sourceX, prop->sourceY,
447             prop->sourceWidth, prop->sourceHeight);
448     }
449     if (mask & IVI_NOTIFICATION_DEST_RECT) {
450         ivi_controller_surface_send_destination_rectangle(resource,
451             prop->destX, prop->destY,
452             prop->destWidth, prop->destHeight);
453     }
454     if (mask & IVI_NOTIFICATION_ORIENTATION) {
455         ivi_controller_surface_send_orientation(resource,
456                                                 prop->orientation);
457     }
458     if (mask & IVI_NOTIFICATION_VISIBILITY) {
459         ivi_controller_surface_send_visibility(resource,
460                                                prop->visibility);
461     }
462     if (mask & IVI_NOTIFICATION_PIXELFORMAT) {
463         ivi_controller_surface_send_pixelformat(resource,
464                                                 prop->pixelformat);
465     }
466     if (mask & IVI_NOTIFICATION_ADD) {
467         send_surface_add_event(ivisurf, resource);
468     }
469 }
470
471 static void
472 send_surface_prop(struct ivi_layout_surface *layout_surface,
473                   struct ivi_layout_SurfaceProperties *prop,
474                   enum ivi_layout_notification_mask mask,
475                   void *userdata)
476 {
477     struct ivisurface *ivisurf = userdata;
478     struct ivishell *shell = ivisurf->shell;
479     struct ivicontroller_surface *ctrlsurf = NULL;
480     uint32_t id_surface = 0;
481
482     id_surface = ivi_layout_getIdOfSurface(layout_surface);
483
484     wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
485         if (id_surface != ctrlsurf->id_surface) {
486             continue;
487         }
488         send_surface_event(ctrlsurf->resource, ivisurf, prop, mask);
489     }
490 }
491
492 static void
493 send_layer_add_event(struct ivilayer *ivilayer,
494                      struct wl_resource *resource)
495 {
496     struct ivi_layout_screen **pArray = NULL;
497     uint32_t length = 0;
498     int32_t ans = 0;
499     int i = 0;
500     struct link_screen *link_scrn = NULL;
501     struct link_screen *next = NULL;
502     struct iviscreen *iviscrn = NULL;
503     struct ivishell *shell = ivilayer->shell;
504     int found = 0;
505     struct wl_client *client = wl_resource_get_client(resource);
506     struct wl_resource *resource_output = NULL;
507
508     ans = ivi_layout_getScreensUnderLayer(ivilayer->layout_layer,
509                                           &length, &pArray);
510     if (0 != ans) {
511         weston_log("failed to get screens at send_layer_add_event\n");
512         return;
513     }
514
515     /* Send Null to cancel added layer */
516     wl_list_for_each_safe(link_scrn, next, &ivilayer->list_screen, link) {
517         for (i = 0, found = 0; i < (int)length; i++) {
518             if (pArray[i] == link_scrn->screen->layout_screen) {
519                 /* No need to send event, if new layer doesn't be added. */
520                 found = 1;
521                 break;
522             }
523         }
524         if (found != 0) {
525             continue;
526         }
527
528         ivi_controller_layer_send_screen(resource, NULL);
529         wl_list_remove(&link_scrn->link);
530         free(link_scrn);
531         link_scrn = NULL;
532     }
533
534     for (i = 0; i < (int)length; i++) {
535         found = 0;
536         wl_list_for_each(link_scrn, &ivilayer->list_screen, link) {
537             if (pArray[i] == link_scrn->screen->layout_screen) {
538                 /* No need to send event, if new screen doesn't be added. */
539                 found = 1;
540                 break;
541             }
542         }
543         if (found != 0) {
544             continue;
545         }
546
547         /* Create list_screen */
548         link_scrn = calloc(1, sizeof(*link_scrn));
549         if (NULL == link_scrn) {
550             continue;
551         }
552         wl_list_init(&link_scrn->link);
553         link_scrn->screen = NULL;
554         wl_list_for_each(iviscrn, &shell->list_screen, link) {
555             if (iviscrn->layout_screen == pArray[i]) {
556                 link_scrn->screen = iviscrn;
557                 break;
558             }
559         }
560
561         if (link_scrn->screen == NULL) {
562             free(link_scrn);
563             link_scrn = NULL;
564             continue;
565         }
566         wl_list_insert(&ivilayer->list_screen, &link_scrn->link);
567
568         /* Send new layer event */
569         resource_output =
570             wl_resource_find_for_client(&iviscrn->output->resource_list,
571                                  client);
572         if (resource_output != NULL) {
573             ivi_controller_layer_send_screen(resource, resource_output);
574         }
575     }
576
577     free(pArray);
578     pArray = NULL;
579 }
580
581 static void
582 send_layer_event(struct wl_resource *resource,
583                  struct ivilayer *ivilayer,
584                  struct ivi_layout_LayerProperties *prop,
585                  uint32_t mask)
586 {
587     if (mask & IVI_NOTIFICATION_OPACITY) {
588         ivi_controller_layer_send_opacity(resource,
589                                           prop->opacity);
590     }
591     if (mask & IVI_NOTIFICATION_SOURCE_RECT) {
592         ivi_controller_layer_send_source_rectangle(resource,
593                                           prop->sourceX,
594                                           prop->sourceY,
595                                           prop->sourceWidth,
596                                           prop->sourceHeight);
597     }
598     if (mask & IVI_NOTIFICATION_DEST_RECT) {
599         ivi_controller_layer_send_destination_rectangle(resource,
600                                           prop->destX,
601                                           prop->destY,
602                                           prop->destWidth,
603                                           prop->destHeight);
604     }
605     if (mask & IVI_NOTIFICATION_ORIENTATION) {
606         ivi_controller_layer_send_orientation(resource,
607                                           prop->orientation);
608     }
609     if (mask & IVI_NOTIFICATION_VISIBILITY) {
610         ivi_controller_layer_send_visibility(resource,
611                                           prop->visibility);
612     }
613     if (mask & IVI_NOTIFICATION_ADD) {
614         send_layer_add_event(ivilayer, resource);
615     }
616     if (mask & IVI_NOTIFICATION_REMOVE) {
617         send_layer_add_event(ivilayer, resource);
618     }
619 }
620
621 static void
622 send_layer_prop(struct ivi_layout_layer *layer,
623                 struct ivi_layout_LayerProperties *prop,
624                 enum ivi_layout_notification_mask mask,
625                 void *userdata)
626 {
627     struct ivilayer *ivilayer = userdata;
628     struct ivicontroller_layer *ctrllayer = NULL;
629     struct ivishell *shell = ivilayer->shell;
630     uint32_t id_layout_layer = 0;
631
632     id_layout_layer = ivi_layout_getIdOfLayer(layer);
633     wl_list_for_each(ctrllayer, &shell->list_controller_layer, link) {
634         if (id_layout_layer != ctrllayer->id_layer) {
635             continue;
636         }
637         send_layer_event(ctrllayer->resource, ivilayer, prop, mask);
638     }
639 }
640
641 static void
642 controller_surface_set_opacity(struct wl_client *client,
643                    struct wl_resource *resource,
644                    wl_fixed_t opacity)
645 {
646     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
647     (void)client;
648     ivi_layout_surfaceSetOpacity(ivisurf->layout_surface, (float)opacity);
649 }
650
651 static void
652 controller_surface_set_source_rectangle(struct wl_client *client,
653                    struct wl_resource *resource,
654                    int32_t x,
655                    int32_t y,
656                    int32_t width,
657                    int32_t height)
658 {
659     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
660     (void)client;
661     ivi_layout_surfaceSetSourceRectangle(ivisurf->layout_surface,
662             (uint32_t)x, (uint32_t)y, (uint32_t)width, (uint32_t)height);
663 }
664
665 static void
666 controller_surface_set_destination_rectangle(struct wl_client *client,
667                      struct wl_resource *resource,
668                      int32_t x,
669                      int32_t y,
670                      int32_t width,
671                      int32_t height)
672 {
673     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
674     (void)client;
675     ivi_layout_surfaceSetDestinationRectangle(ivisurf->layout_surface,
676             (uint32_t)x, (uint32_t)y, (uint32_t)width, (uint32_t)height);
677 }
678
679 static void
680 controller_surface_set_visibility(struct wl_client *client,
681                       struct wl_resource *resource,
682                       uint32_t visibility)
683 {
684     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
685     (void)client;
686     ivi_layout_surfaceSetVisibility(ivisurf->layout_surface, visibility);
687 }
688
689 static void
690 controller_surface_set_configuration(struct wl_client *client,
691                    struct wl_resource *resource,
692                    int32_t width, int32_t height)
693 {
694     /* This interface has been supported yet. */
695     (void)client;
696     (void)resource;
697     (void)width;
698     (void)height;
699 }
700
701 static void
702 controller_surface_set_orientation(struct wl_client *client,
703                    struct wl_resource *resource,
704                    int32_t orientation)
705 {
706     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
707     (void)client;
708     ivi_layout_surfaceSetOrientation(ivisurf->layout_surface, (uint32_t)orientation);
709 }
710
711 static void
712 controller_surface_screenshot(struct wl_client *client,
713                   struct wl_resource *resource,
714                   const char *filename)
715 {
716     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
717     (void)client;
718     ivi_layout_takeSurfaceScreenshot(filename, ivisurf->layout_surface);
719 }
720
721 static void
722 controller_surface_send_stats(struct wl_client *client,
723                               struct wl_resource *resource)
724 {
725     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
726     pid_t pid;
727     uid_t uid;
728     gid_t gid;
729     wl_client_get_credentials(client, &pid, &uid, &gid);
730
731     ivi_controller_surface_send_stats(resource, 0, 0,
732                                       ivisurf->update_count, pid, "");
733 }
734
735 static void
736 controller_surface_destroy(struct wl_client *client,
737               struct wl_resource *resource,
738               int32_t destroy_scene_object)
739 {
740     (void)client;
741     (void)destroy_scene_object;
742     wl_resource_destroy(resource);
743 }
744
745 static void
746 controller_surface_set_input_focus(struct wl_client *client,
747               struct wl_resource *resource,
748               int32_t enabled)
749 {
750     (void)client;
751     (void)resource;
752     (void)enabled;
753 }
754
755 static const
756 struct ivi_controller_surface_interface controller_surface_implementation = {
757     controller_surface_set_visibility,
758     controller_surface_set_opacity,
759     controller_surface_set_source_rectangle,
760     controller_surface_set_destination_rectangle,
761     controller_surface_set_configuration,
762     controller_surface_set_orientation,
763     controller_surface_screenshot,
764     controller_surface_send_stats,
765     controller_surface_destroy,
766     controller_surface_set_input_focus
767 };
768
769 static void
770 controller_layer_set_source_rectangle(struct wl_client *client,
771                    struct wl_resource *resource,
772                    int32_t x,
773                    int32_t y,
774                    int32_t width,
775                    int32_t height)
776 {
777     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
778     (void)client;
779     ivi_layout_layerSetSourceRectangle(ivilayer->layout_layer,
780            (uint32_t)x, (uint32_t)y, (uint32_t)width, (uint32_t)height);
781 }
782
783 static void
784 controller_layer_set_destination_rectangle(struct wl_client *client,
785                  struct wl_resource *resource,
786                  int32_t x,
787                  int32_t y,
788                  int32_t width,
789                  int32_t height)
790 {
791     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
792     (void)client;
793     ivi_layout_layerSetDestinationRectangle(ivilayer->layout_layer,
794             (uint32_t)x, (uint32_t)y, (uint32_t)width, (uint32_t)height);
795 }
796
797 static void
798 controller_layer_set_visibility(struct wl_client *client,
799                     struct wl_resource *resource,
800                     uint32_t visibility)
801 {
802     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
803     (void)client;
804     ivi_layout_layerSetVisibility(ivilayer->layout_layer, visibility);
805 }
806
807 static void
808 controller_layer_set_opacity(struct wl_client *client,
809                  struct wl_resource *resource,
810                  wl_fixed_t opacity)
811 {
812     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
813     (void)client;
814     ivi_layout_layerSetOpacity(ivilayer->layout_layer, (float)opacity);
815 }
816
817 static void
818 controller_layer_set_configuration(struct wl_client *client,
819                  struct wl_resource *resource,
820                  int32_t width,
821                  int32_t height)
822 {
823     /* This interface has been supported yet. */
824     (void)client;
825     (void)resource;
826     (void)width;
827     (void)height;
828 }
829
830 static void
831 controller_layer_set_orientation(struct wl_client *client,
832                  struct wl_resource *resource,
833                  int32_t orientation)
834 {
835     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
836     (void)client;
837     ivi_layout_layerSetOrientation(ivilayer->layout_layer, (uint32_t)orientation);
838 }
839
840 static void
841 controller_layer_clear_surfaces(struct wl_client *client,
842                     struct wl_resource *resource)
843 {
844     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
845     (void)client;
846     ivi_layout_layerSetRenderOrder(ivilayer->layout_layer, NULL, 0);
847 }
848
849 static void
850 controller_layer_add_surface(struct wl_client *client,
851                  struct wl_resource *resource,
852                  struct wl_resource *surface)
853 {
854     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
855     struct ivisurface *ivisurf = wl_resource_get_user_data(surface);
856     (void)client;
857     ivi_layout_layerAddSurface(ivilayer->layout_layer, ivisurf->layout_surface);
858 }
859
860 static void
861 controller_layer_remove_surface(struct wl_client *client,
862                     struct wl_resource *resource,
863                     struct wl_resource *surface)
864 {
865     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
866     struct ivisurface *ivisurf = wl_resource_get_user_data(surface);
867     (void)client;
868     ivi_layout_layerRemoveSurface(ivilayer->layout_layer, ivisurf->layout_surface);
869 }
870
871 static void
872 controller_layer_screenshot(struct wl_client *client,
873                 struct wl_resource *resource,
874                 const char *filename)
875 {
876     (void)client;
877     (void)resource;
878     (void)filename;
879 }
880
881 static void
882 controller_layer_set_render_order(struct wl_client *client,
883                                   struct wl_resource *resource,
884                                   struct wl_array *id_surfaces)
885 {
886     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
887     struct ivi_layout_surface **layoutsurf_array = NULL;
888     struct ivisurface *ivisurf = NULL;
889     uint32_t *id_surface = NULL;
890     uint32_t id_layout_surface = 0;
891     int i = 0;
892     (void)client;
893
894     wl_array_for_each(id_surface, id_surfaces) {
895         wl_list_for_each(ivisurf, &ivilayer->shell->list_surface, link) {
896             id_layout_surface = ivi_layout_getIdOfSurface(ivisurf->layout_surface);
897             if (*id_surface == id_layout_surface) {
898                 layoutsurf_array[i] = ivisurf->layout_surface;
899                 i++;
900                 break;
901             }
902         }
903     }
904
905     ivi_layout_layerSetRenderOrder(ivilayer->layout_layer,
906                                    layoutsurf_array, id_surfaces->size);
907     free(layoutsurf_array);
908 }
909
910 static void
911 controller_layer_destroy(struct wl_client *client,
912               struct wl_resource *resource,
913               int32_t destroy_scene_object)
914 {
915     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
916     struct ivishell *shell = ivilayer->shell;
917     struct ivicontroller_layer *ctrllayer = NULL;
918     struct ivicontroller_layer *next = NULL;
919     uint32_t id_layer = ivi_layout_getIdOfLayer(ivilayer->layout_layer);
920     (void)client;
921     (void)destroy_scene_object;
922
923     wl_list_for_each_safe(ctrllayer, next, &shell->list_controller_layer, link) {
924         if (ctrllayer->resource != resource) {
925             continue;
926     }
927
928         wl_resource_destroy(resource);
929         break;
930     }
931 }
932
933 static const
934 struct ivi_controller_layer_interface controller_layer_implementation = {
935     controller_layer_set_visibility,
936     controller_layer_set_opacity,
937     controller_layer_set_source_rectangle,
938     controller_layer_set_destination_rectangle,
939     controller_layer_set_configuration,
940     controller_layer_set_orientation,
941     controller_layer_screenshot,
942     controller_layer_clear_surfaces,
943     controller_layer_add_surface,
944     controller_layer_remove_surface,
945     controller_layer_set_render_order,
946     controller_layer_destroy
947 };
948
949 static void
950 controller_screen_destroy(struct wl_client *client,
951                           struct wl_resource *resource)
952 {
953     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
954     struct ivicontroller_screen *ctrlscrn = NULL;
955     struct ivicontroller_screen *next = NULL;
956 //    uint32_t id_screen = ivi_layout_getIdOfScreen(iviscrn->layout_screen);
957     (void)client;
958
959     wl_list_for_each_safe(ctrlscrn, next,
960                           &iviscrn->shell->list_controller_screen, link) {
961         if (resource != ctrlscrn->resource) {
962             continue;
963         }
964
965         wl_list_remove(&ctrlscrn->link);
966         free(ctrlscrn);
967         ctrlscrn = NULL;
968         wl_resource_destroy(ctrlscrn->resource);
969         break;
970     }
971 }
972
973 static void
974 controller_screen_clear(struct wl_client *client,
975                 struct wl_resource *resource)
976 {
977     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
978     (void)client;
979     ivi_layout_screenSetRenderOrder(iviscrn->layout_screen, NULL, 0);
980 }
981
982 static void
983 controller_screen_add_layer(struct wl_client *client,
984                 struct wl_resource *resource,
985                 struct wl_resource *layer)
986 {
987     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
988     struct ivilayer *ivilayer = wl_resource_get_user_data(layer);
989     (void)client;
990     ivi_layout_screenAddLayer(iviscrn->layout_screen, ivilayer->layout_layer);
991 }
992
993 static void
994 controller_screen_screenshot(struct wl_client *client,
995                 struct wl_resource *resource,
996                 const char *filename)
997 {
998     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
999     (void)client;
1000     ivi_layout_takeScreenshot(iviscrn->layout_screen, filename);
1001 }
1002
1003 static void
1004 controller_screen_set_render_order(struct wl_client *client,
1005                 struct wl_resource *resource,
1006                 struct wl_array *id_layers)
1007 {
1008     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1009     struct ivi_layout_layer **layoutlayer_array = NULL;
1010     struct ivilayer *ivilayer = NULL;
1011     uint32_t *id_layer = NULL;
1012     uint32_t id_layout_layer = 0;
1013     int i = 0;
1014     (void)client;
1015
1016     *layoutlayer_array = (struct ivi_layout_layer*)calloc(
1017                             id_layers->size, sizeof(void*));
1018
1019     wl_array_for_each(id_layer, id_layers) {
1020         wl_list_for_each(ivilayer, &iviscrn->shell->list_layer, link) {
1021             id_layout_layer = ivi_layout_getIdOfLayer(ivilayer->layout_layer);
1022             if (*id_layer == id_layout_layer) {
1023                 layoutlayer_array[i] = ivilayer->layout_layer;
1024                 i++;
1025                 break;
1026             }
1027         }
1028     }
1029
1030     ivi_layout_screenSetRenderOrder(iviscrn->layout_screen,
1031                                     layoutlayer_array, id_layers->size);
1032     free(layoutlayer_array);
1033 }
1034
1035 static const
1036 struct ivi_controller_screen_interface controller_screen_implementation = {
1037     controller_screen_destroy,
1038     controller_screen_clear,
1039     controller_screen_add_layer,
1040     controller_screen_screenshot,
1041     controller_screen_set_render_order
1042 };
1043
1044 static void
1045 controller_commit_changes(struct wl_client *client,
1046                           struct wl_resource *resource)
1047 {
1048     int32_t ans = 0;
1049     (void)client;
1050     (void)resource;
1051
1052     ans = ivi_layout_commitChanges();
1053     if (ans < 0) {
1054         weston_log("Failed to commit changes at controller_commit_changes\n");
1055     }
1056 }
1057
1058 static void
1059 controller_layer_create(struct wl_client *client,
1060                         struct wl_resource *resource,
1061                         uint32_t id_layer,
1062                         int32_t width,
1063                         int32_t height,
1064                         uint32_t id)
1065 {
1066     struct ivicontroller *ctrl = wl_resource_get_user_data(resource);
1067     struct ivishell *shell = ctrl->shell;
1068     struct ivi_layout_layer *layout_layer = NULL;
1069     struct ivicontroller_layer *ctrllayer = NULL;
1070     struct ivilayer *ivilayer = NULL;
1071     struct ivi_layout_LayerProperties prop;
1072
1073     ivilayer = get_layer(&shell->list_layer, id_layer);
1074     if (ivilayer == NULL) {
1075         layout_layer = ivi_layout_layerCreateWithDimension(id_layer,
1076                            (uint32_t)width, (uint32_t)height);
1077         if (layout_layer == NULL) {
1078             weston_log("id_layer is already created\n");
1079             return;
1080         }
1081
1082         /* ivilayer will be created by layer_event_create */
1083         ivilayer = get_layer(&shell->list_layer, id_layer);
1084         if (ivilayer == NULL) {
1085             weston_log("couldn't get layer object\n");
1086             return;
1087         }
1088     }
1089
1090     ctrllayer = calloc(1, sizeof *ctrllayer);
1091     if (!ctrllayer) {
1092         weston_log("no memory to allocate client layer\n");
1093         return;
1094     }
1095
1096     ++ivilayer->controller_layer_count;
1097
1098     ctrllayer->shell = shell;
1099     ctrllayer->client = client;
1100     ctrllayer->id = id;
1101     ctrllayer->id_layer = id_layer;
1102     ctrllayer->resource = wl_resource_create(client,
1103                                &ivi_controller_layer_interface, 1, id);
1104     if (ctrllayer->resource == NULL) {
1105         weston_log("couldn't get layer object\n");
1106         return;
1107     }
1108
1109     wl_list_init(&ctrllayer->link);
1110     wl_list_insert(&shell->list_controller_layer, &ctrllayer->link);
1111
1112     wl_resource_set_implementation(ctrllayer->resource,
1113                                    &controller_layer_implementation,
1114                                    ivilayer, destroy_ivicontroller_layer);
1115
1116     memset(&prop, 0, sizeof prop);
1117
1118     ivi_layout_getPropertiesOfLayer(ivilayer->layout_layer, &prop);
1119     send_layer_event(ctrllayer->resource, ivilayer,
1120                      &prop, IVI_NOTIFICATION_ALL);
1121 }
1122
1123 static void
1124 surface_event_content(struct ivi_layout_surface *layout_surface, int32_t content, void *userdata)
1125 {
1126     struct ivishell *shell = userdata;
1127     struct ivicontroller_surface *ctrlsurf = NULL;
1128     uint32_t id_surface = 0;
1129
1130     if (content == 0) {
1131         surface_event_remove(layout_surface, userdata);
1132     }
1133 }
1134
1135 static void
1136 controller_surface_create(struct wl_client *client,
1137                           struct wl_resource *resource,
1138                           uint32_t id_surface,
1139                           uint32_t id)
1140 {
1141     struct ivicontroller *ctrl = wl_resource_get_user_data(resource);
1142     struct ivishell *shell = ctrl->shell;
1143     struct ivicontroller_surface *ctrlsurf = NULL;
1144     struct ivi_layout_SurfaceProperties prop;
1145     struct ivisurface *ivisurf = NULL;
1146
1147     ctrlsurf = calloc(1, sizeof *ctrlsurf);
1148     if (!ctrlsurf) {
1149         weston_log("no memory to allocate controller surface\n");
1150         return;
1151     }
1152
1153     ctrlsurf->shell = shell;
1154     ctrlsurf->client = client;
1155     ctrlsurf->id = id;
1156     ctrlsurf->id_surface = id_surface;
1157     wl_list_init(&ctrlsurf->link);
1158     wl_list_insert(&shell->list_controller_surface, &ctrlsurf->link);
1159
1160     ctrlsurf->resource = wl_resource_create(client,
1161                                &ivi_controller_surface_interface, 1, id);
1162     if (ctrlsurf->resource == NULL) {
1163         weston_log("couldn't surface object");
1164         return;
1165     }
1166
1167     ivisurf = get_surface(&shell->list_surface, id_surface);
1168     if (ivisurf == NULL) {
1169         return;
1170     }
1171
1172     ++ivisurf->controller_surface_count;
1173
1174     wl_resource_set_implementation(ctrlsurf->resource,
1175                                    &controller_surface_implementation,
1176                                    ivisurf, destroy_ivicontroller_surface);
1177
1178     memset(&prop, 0, sizeof prop);
1179
1180     ivi_layout_getPropertiesOfSurface(ivisurf->layout_surface, &prop);
1181     ivi_layout_surfaceSetContentObserver(ivisurf->layout_surface, surface_event_content, shell);
1182
1183     send_surface_event(ctrlsurf->resource, ivisurf,
1184                        &prop, IVI_NOTIFICATION_ALL);
1185 }
1186
1187 static const struct ivi_controller_interface controller_implementation = {
1188     controller_commit_changes,
1189     controller_layer_create,
1190     controller_surface_create
1191 };
1192
1193 static void
1194 add_client_to_resources(struct ivishell *shell,
1195                         struct wl_client *client,
1196                         struct ivicontroller *controller)
1197 {
1198     struct ivisurface* ivisurf = NULL;
1199     struct ivilayer* ivilayer = NULL;
1200     struct iviscreen* iviscrn = NULL;
1201     struct ivicontroller_screen *ctrlscrn = NULL;
1202     struct wl_resource *resource_output = NULL;
1203     uint32_t id_layout_surface = 0;
1204     uint32_t id_layout_layer = 0;
1205
1206     wl_list_for_each(ivisurf, &shell->list_surface, link) {
1207         id_layout_surface =
1208             ivi_layout_getIdOfSurface(ivisurf->layout_surface);
1209
1210         ivi_controller_send_surface(controller->resource,
1211                                     id_layout_surface);
1212     }
1213
1214     wl_list_for_each(ivilayer, &shell->list_layer, link) {
1215         id_layout_layer =
1216             ivi_layout_getIdOfLayer(ivilayer->layout_layer);
1217
1218         ivi_controller_send_layer(controller->resource,
1219                                   id_layout_layer);
1220     }
1221
1222     wl_list_for_each(iviscrn, &shell->list_screen, link) {
1223         resource_output = wl_resource_find_for_client(
1224                 &iviscrn->output->resource_list, client);
1225         if (resource_output == NULL) {
1226             continue;
1227         }
1228
1229         ctrlscrn = controller_screen_create(iviscrn->shell, client, iviscrn);
1230         if (ctrlscrn == NULL) {
1231             continue;
1232         }
1233
1234         ivi_controller_send_screen(controller->resource,
1235                                    wl_resource_get_id(resource_output),
1236                                    ctrlscrn->resource);
1237     }
1238 }
1239
1240 static void
1241 bind_ivi_controller(struct wl_client *client, void *data,
1242                     uint32_t version, uint32_t id)
1243 {
1244     struct ivishell *shell = data;
1245     struct ivicontroller *controller;
1246     (void)version;
1247
1248     controller = calloc(1, sizeof *controller);
1249     if (controller == NULL) {
1250         weston_log("no memory to allocate controller\n");
1251         return;
1252     }
1253
1254     controller->resource =
1255         wl_resource_create(client, &ivi_controller_interface, 1, id);
1256     wl_resource_set_implementation(controller->resource,
1257                                    &controller_implementation,
1258                                    controller, unbind_resource_controller);
1259
1260     controller->shell = shell;
1261     controller->client = client;
1262     controller->id = id;
1263
1264     wl_list_init(&controller->link);
1265     wl_list_insert(&shell->list_controller, &controller->link);
1266
1267     add_client_to_resources(shell, client, controller);
1268 }
1269
1270 static struct iviscreen*
1271 create_screen(struct ivishell *shell, struct weston_output *output)
1272 {
1273     struct iviscreen *iviscrn;
1274     iviscrn = calloc(1, sizeof *iviscrn);
1275     if (iviscrn == NULL) {
1276         weston_log("no memory to allocate client screen\n");
1277         return NULL;
1278     }
1279
1280     iviscrn->shell = shell;
1281     iviscrn->output = output;
1282
1283 // TODO : Only Single display
1284     iviscrn->layout_screen = ivi_layout_getScreenFromId(0);
1285
1286     wl_list_init(&iviscrn->link);
1287
1288     return iviscrn;
1289 }
1290
1291 static struct ivilayer*
1292 create_layer(struct ivishell *shell,
1293              struct ivi_layout_layer *layout_layer,
1294              uint32_t id_layer)
1295 {
1296     struct ivilayer *ivilayer = NULL;
1297     struct ivicontroller *controller = NULL;
1298
1299     ivilayer = get_layer(&shell->list_layer, id_layer);
1300     if (ivilayer != NULL) {
1301         weston_log("id_layer is already created\n");
1302         return NULL;
1303     }
1304
1305     ivilayer = calloc(1, sizeof *ivilayer);
1306     if (NULL == ivilayer) {
1307         weston_log("no memory to allocate client layer\n");
1308         return NULL;
1309     }
1310
1311     ivilayer->shell = shell;
1312     wl_list_init(&ivilayer->list_screen);
1313     wl_list_init(&ivilayer->link);
1314     wl_list_insert(&shell->list_layer, &ivilayer->link);
1315     ivilayer->layout_layer = layout_layer;
1316
1317     ivi_layout_layerAddNotification(layout_layer, send_layer_prop, ivilayer);
1318
1319     wl_list_for_each(controller, &shell->list_controller, link) {
1320         ivi_controller_send_layer(controller->resource, id_layer);
1321     }
1322
1323     return ivilayer;
1324 }
1325
1326 static struct ivisurface*
1327 create_surface(struct ivishell *shell,
1328                struct ivi_layout_surface *layout_surface,
1329                uint32_t id_surface)
1330 {
1331     struct ivisurface *ivisurf = NULL;
1332     struct ivicontroller *controller = NULL;
1333
1334     ivisurf = get_surface(&shell->list_surface, id_surface);
1335     if (ivisurf != NULL) {
1336         weston_log("id_surface is already created\n");
1337         return NULL;
1338     }
1339
1340     ivisurf = calloc(1, sizeof *ivisurf);
1341     if (ivisurf == NULL) {
1342         weston_log("no memory to allocate client surface\n");
1343         return NULL;
1344     }
1345
1346     ivisurf->shell = shell;
1347     ivisurf->layout_surface = layout_surface;
1348     wl_list_init(&ivisurf->list_layer);
1349     wl_list_init(&ivisurf->link);
1350     wl_list_insert(&shell->list_surface, &ivisurf->link);
1351
1352     wl_list_for_each(controller, &shell->list_controller, link) {
1353         ivi_controller_send_surface(controller->resource,
1354                                     id_surface);
1355     }
1356
1357     ivi_layout_surfaceAddNotification(layout_surface,
1358                                     send_surface_prop, ivisurf);
1359
1360     return ivisurf;
1361 }
1362
1363 static void
1364 layer_event_create(struct ivi_layout_layer *layout_layer,
1365                      void *userdata)
1366 {
1367     struct ivishell *shell = userdata;
1368     struct ivilayer *ivilayer = NULL;
1369     uint32_t id_layer = 0;
1370
1371     id_layer = ivi_layout_getIdOfLayer(layout_layer);
1372
1373     ivilayer = create_layer(shell, layout_layer, id_layer);
1374     if (ivilayer == NULL) {
1375         weston_log("failed to create layer");
1376         return;
1377     }
1378 }
1379
1380 static void
1381 layer_event_remove(struct ivi_layout_layer *layout_layer,
1382                      void *userdata)
1383 {
1384     struct ivishell *shell = userdata;
1385     struct ivicontroller_layer *ctrllayer = NULL;
1386     struct ivilayer *ivilayer = NULL;
1387     struct ivilayer *next = NULL;
1388     uint32_t id_layer = 0;
1389     int is_removed = 0;
1390
1391     wl_list_for_each_safe(ivilayer, next, &shell->list_layer, link) {
1392         if (layout_layer != ivilayer->layout_layer) {
1393             continue;
1394         }
1395
1396         wl_list_remove(&ivilayer->link);
1397
1398         is_removed = 1;
1399         free(ivilayer);
1400         ivilayer = NULL;
1401         break;
1402     }
1403
1404     if (is_removed) {
1405         id_layer = ivi_layout_getIdOfLayer(layout_layer);
1406
1407         wl_list_for_each(ctrllayer, &shell->list_controller_layer, link) {
1408             if (id_layer != ctrllayer->id_layer) {
1409                 continue;
1410             }
1411             ivi_controller_layer_send_destroyed(ctrllayer->resource);
1412         }
1413     }
1414 }
1415
1416
1417 static void
1418 surface_event_create(struct ivi_layout_surface *layout_surface,
1419                      void *userdata)
1420 {
1421     struct ivishell *shell = userdata;
1422     struct ivisurface *ivisurf = NULL;
1423     uint32_t id_surface = 0;
1424
1425     id_surface = ivi_layout_getIdOfSurface(layout_surface);
1426
1427     ivisurf = create_surface(shell, layout_surface, id_surface);
1428     if (ivisurf == NULL) {
1429         weston_log("failed to create surface");
1430         return;
1431     }
1432 }
1433
1434 static void
1435 surface_event_remove(struct ivi_layout_surface *layout_surface,
1436                      void *userdata)
1437 {
1438     struct ivishell *shell = userdata;
1439     struct ivicontroller_surface *ctrlsurf = NULL;
1440     struct ivisurface *ivisurf = NULL;
1441     struct ivisurface *next = NULL;
1442     uint32_t id_surface = 0;
1443     int is_removed = 0;
1444
1445     wl_list_for_each_safe(ivisurf, next, &shell->list_surface, link) {
1446         if (layout_surface != ivisurf->layout_surface) {
1447             continue;
1448         }
1449
1450         wl_list_remove(&ivisurf->link);
1451         is_removed = 1;
1452
1453         if (ivisurf->controller_surface_count == 0) {
1454             free(ivisurf);
1455         }
1456         else {
1457             ivisurf->can_be_removed = 1;
1458         }
1459
1460         break;
1461     }
1462
1463     if (is_removed) {
1464         id_surface = ivi_layout_getIdOfSurface(layout_surface);
1465
1466         wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
1467             if (id_surface != ctrlsurf->id_surface) {
1468                 continue;
1469             }
1470             ivi_controller_surface_send_destroyed(ctrlsurf->resource);
1471         }
1472     }
1473 }
1474
1475 static void
1476 surface_event_configure(struct ivi_layout_surface *layout_surface,
1477                         void *userdata)
1478 {
1479     struct ivishell *shell = userdata;
1480     struct ivisurface *ivisurf = NULL;
1481     struct ivicontroller_surface *ctrlsurf = NULL;
1482     struct ivi_layout_SurfaceProperties prop;
1483     uint32_t id_surface = 0;
1484
1485     id_surface = ivi_layout_getIdOfSurface(layout_surface);
1486
1487     ivisurf = get_surface(&shell->list_surface, id_surface);
1488     if (ivisurf == NULL) {
1489         weston_log("id_surface is not created yet\n");
1490         return;
1491     }
1492
1493     memset(&prop, 0, sizeof prop);
1494     ivi_layout_getPropertiesOfSurface(layout_surface, &prop);
1495
1496     wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
1497         if (id_surface != ctrlsurf->id_surface) {
1498             continue;
1499         }
1500         send_surface_event(ctrlsurf->resource, ivisurf,
1501                            &prop, IVI_NOTIFICATION_ALL);
1502     }
1503 }
1504
1505 static int32_t
1506 check_layout_layers(struct ivishell *shell)
1507 {
1508     struct ivi_layout_layer **pArray = NULL;
1509     struct ivilayer *ivilayer = NULL;
1510     uint32_t id_layer = 0;
1511     uint32_t length = 0;
1512     uint32_t i = 0;
1513     int32_t ret = 0;
1514
1515     ret = ivi_layout_getLayers(&length, &pArray);
1516     if(ret != 0) {
1517         weston_log("failed to get layers at check_layout_layers\n");
1518         return -1;
1519     }
1520
1521     if (length == 0) {
1522         /* if length is 0, pArray doesn't need to free.*/
1523         return 0;
1524     }
1525
1526     for (i = 0; i < length; i++) {
1527         id_layer = ivi_layout_getIdOfLayer(pArray[i]);
1528         ivilayer = create_layer(shell, pArray[i], id_layer);
1529         if (ivilayer == NULL) {
1530             weston_log("failed to create layer");
1531         }
1532     }
1533
1534     free(pArray);
1535     pArray = NULL;
1536
1537     return 0;
1538 }
1539
1540 static int32_t
1541 check_layout_surfaces(struct ivishell *shell)
1542 {
1543     struct ivi_layout_surface **pArray = NULL;
1544     struct ivisurface *ivisurf = NULL;
1545     uint32_t id_surface = 0;
1546     uint32_t length = 0;
1547     uint32_t i = 0;
1548     int32_t ret = 0;
1549
1550     ret = ivi_layout_getSurfaces(&length, &pArray);
1551     if(ret != 0) {
1552         weston_log("failed to get surfaces at check_layout_surfaces\n");
1553         return -1;
1554     }
1555
1556     if (length == 0) {
1557         /* if length is 0, pArray doesn't need to free.*/
1558         return 0;
1559     }
1560
1561     for (i = 0; i < length; i++) {
1562         id_surface = ivi_layout_getIdOfSurface(pArray[i]);
1563         ivisurf = create_surface(shell, pArray[i], id_surface);
1564         if (ivisurf == NULL) {
1565             weston_log("failed to create surface");
1566         }
1567     }
1568
1569     free(pArray);
1570     pArray = NULL;
1571
1572     return 0;
1573 }
1574
1575 static void
1576 init_ivi_shell(struct weston_compositor *ec, struct ivishell *shell)
1577 {
1578     struct weston_output *output = NULL;
1579     struct iviscreen *iviscrn = NULL;
1580     int32_t ret = 0;
1581
1582     shell->compositor = ec;
1583
1584     wl_list_init(&shell->list_surface);
1585     wl_list_init(&shell->list_layer);
1586     wl_list_init(&shell->list_screen);
1587     wl_list_init(&shell->list_weston_surface);
1588     wl_list_init(&shell->list_controller);
1589     wl_list_init(&shell->list_controller_screen);
1590     wl_list_init(&shell->list_controller_layer);
1591     wl_list_init(&shell->list_controller_surface);
1592     shell->event_restriction = 0;
1593
1594     wl_list_for_each(output, &ec->output_list, link) {
1595         iviscrn = create_screen(shell, output);
1596         if (iviscrn != NULL) {
1597             wl_list_insert(&shell->list_screen, &iviscrn->link);
1598         }
1599     }
1600
1601     ret = check_layout_layers(shell);
1602     if (ret != 0) {
1603         weston_log("failed to check_layout_layers");
1604     }
1605
1606     ret = check_layout_surfaces(shell);
1607     if (ret != 0) {
1608         weston_log("failed to check_layout_surfaces");
1609     }
1610
1611     ivi_layout_addNotificationCreateLayer(layer_event_create, shell);
1612     ivi_layout_addNotificationRemoveLayer(layer_event_remove, shell);
1613
1614     ivi_layout_addNotificationCreateSurface(surface_event_create, shell);
1615     ivi_layout_addNotificationRemoveSurface(surface_event_remove, shell);
1616     ivi_layout_addNotificationConfigureSurface(surface_event_configure, shell);
1617 }
1618
1619 WL_EXPORT int
1620 module_init(struct weston_compositor *ec,
1621             int *argc, char *argv[])
1622 {
1623     struct ivishell *shell;
1624     (void)argc;
1625     (void)argv;
1626
1627     shell = malloc(sizeof *shell);
1628     if (shell == NULL)
1629         return -1;
1630
1631     memset(shell, 0, sizeof *shell);
1632     init_ivi_shell(ec, shell);
1633
1634     if (wl_global_create(ec->wl_display, &ivi_controller_interface, 1,
1635                          shell, bind_ivi_controller) == NULL) {
1636         return -1;
1637     }
1638
1639     return 0;
1640 }