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