weston-ivi-shell: Add destroy condition for ivi-layout-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             ivi_controller_surface_send_layer(resource, ctrllayer->resource);
429         }
430     }
431
432     free(pArray);
433     pArray = NULL;
434 }
435
436 static void
437 send_surface_event(struct wl_resource *resource,
438                    struct ivisurface *ivisurf,
439                    struct ivi_layout_SurfaceProperties *prop,
440                    uint32_t mask)
441 {
442     if (mask & IVI_NOTIFICATION_OPACITY) {
443         ivi_controller_surface_send_opacity(resource,
444                                             prop->opacity);
445     }
446     if (mask & IVI_NOTIFICATION_SOURCE_RECT) {
447         ivi_controller_surface_send_source_rectangle(resource,
448             prop->sourceX, prop->sourceY,
449             prop->sourceWidth, prop->sourceHeight);
450     }
451     if (mask & IVI_NOTIFICATION_DEST_RECT) {
452         ivi_controller_surface_send_destination_rectangle(resource,
453             prop->destX, prop->destY,
454             prop->destWidth, prop->destHeight);
455     }
456     if (mask & IVI_NOTIFICATION_ORIENTATION) {
457         ivi_controller_surface_send_orientation(resource,
458                                                 prop->orientation);
459     }
460     if (mask & IVI_NOTIFICATION_VISIBILITY) {
461         ivi_controller_surface_send_visibility(resource,
462                                                prop->visibility);
463     }
464     if (mask & IVI_NOTIFICATION_PIXELFORMAT) {
465         ivi_controller_surface_send_pixelformat(resource,
466                                                 prop->pixelformat);
467     }
468     if (mask & IVI_NOTIFICATION_ADD) {
469         send_surface_add_event(ivisurf, resource);
470     }
471 }
472
473 static void
474 send_surface_prop(struct ivi_layout_surface *layout_surface,
475                   struct ivi_layout_SurfaceProperties *prop,
476                   enum ivi_layout_notification_mask mask,
477                   void *userdata)
478 {
479     struct ivisurface *ivisurf = userdata;
480     struct ivishell *shell = ivisurf->shell;
481     struct ivicontroller_surface *ctrlsurf = NULL;
482     uint32_t id_surface = 0;
483
484     id_surface = ivi_layout_getIdOfSurface(layout_surface);
485
486     wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
487         if (id_surface != ctrlsurf->id_surface) {
488             continue;
489         }
490         send_surface_event(ctrlsurf->resource, ivisurf, prop, mask);
491     }
492 }
493
494 static void
495 send_layer_add_event(struct ivilayer *ivilayer,
496                      struct wl_resource *resource)
497 {
498     struct ivi_layout_screen **pArray = NULL;
499     uint32_t length = 0;
500     int32_t ans = 0;
501     int i = 0;
502     struct link_screen *link_scrn = NULL;
503     struct link_screen *next = NULL;
504     struct iviscreen *iviscrn = NULL;
505     struct ivishell *shell = ivilayer->shell;
506     int found = 0;
507     struct wl_client *client = wl_resource_get_client(resource);
508     struct wl_resource *resource_output = NULL;
509
510     ans = ivi_layout_getScreensUnderLayer(ivilayer->layout_layer,
511                                           &length, &pArray);
512     if (0 != ans) {
513         weston_log("failed to get screens at send_layer_add_event\n");
514         return;
515     }
516
517     /* Send Null to cancel added layer */
518     wl_list_for_each_safe(link_scrn, next, &ivilayer->list_screen, link) {
519         for (i = 0, found = 0; i < (int)length; i++) {
520             if (pArray[i] == link_scrn->screen->layout_screen) {
521                 /* No need to send event, if new layer doesn't be added. */
522                 found = 1;
523                 break;
524             }
525         }
526         if (found != 0) {
527             continue;
528         }
529
530         ivi_controller_layer_send_screen(resource, NULL);
531         wl_list_remove(&link_scrn->link);
532         free(link_scrn);
533         link_scrn = NULL;
534     }
535
536     for (i = 0; i < (int)length; i++) {
537         found = 0;
538         wl_list_for_each(link_scrn, &ivilayer->list_screen, link) {
539             if (pArray[i] == link_scrn->screen->layout_screen) {
540                 /* No need to send event, if new screen doesn't be added. */
541                 found = 1;
542                 break;
543             }
544         }
545         if (found != 0) {
546             continue;
547         }
548
549         /* Create list_screen */
550         link_scrn = calloc(1, sizeof(*link_scrn));
551         if (NULL == link_scrn) {
552             continue;
553         }
554         wl_list_init(&link_scrn->link);
555         link_scrn->screen = NULL;
556         wl_list_for_each(iviscrn, &shell->list_screen, link) {
557             if (iviscrn->layout_screen == pArray[i]) {
558                 link_scrn->screen = iviscrn;
559                 break;
560             }
561         }
562
563         if (link_scrn->screen == NULL) {
564             free(link_scrn);
565             link_scrn = NULL;
566             continue;
567         }
568         wl_list_insert(&ivilayer->list_screen, &link_scrn->link);
569
570         /* Send new layer event */
571         resource_output =
572             wl_resource_find_for_client(&iviscrn->output->resource_list,
573                                  client);
574         if (resource_output != NULL) {
575             ivi_controller_layer_send_screen(resource, resource_output);
576         }
577     }
578
579     free(pArray);
580     pArray = NULL;
581 }
582
583 static void
584 send_layer_event(struct wl_resource *resource,
585                  struct ivilayer *ivilayer,
586                  struct ivi_layout_LayerProperties *prop,
587                  uint32_t mask)
588 {
589     if (mask & IVI_NOTIFICATION_OPACITY) {
590         ivi_controller_layer_send_opacity(resource,
591                                           prop->opacity);
592     }
593     if (mask & IVI_NOTIFICATION_SOURCE_RECT) {
594         ivi_controller_layer_send_source_rectangle(resource,
595                                           prop->sourceX,
596                                           prop->sourceY,
597                                           prop->sourceWidth,
598                                           prop->sourceHeight);
599     }
600     if (mask & IVI_NOTIFICATION_DEST_RECT) {
601         ivi_controller_layer_send_destination_rectangle(resource,
602                                           prop->destX,
603                                           prop->destY,
604                                           prop->destWidth,
605                                           prop->destHeight);
606     }
607     if (mask & IVI_NOTIFICATION_ORIENTATION) {
608         ivi_controller_layer_send_orientation(resource,
609                                           prop->orientation);
610     }
611     if (mask & IVI_NOTIFICATION_VISIBILITY) {
612         ivi_controller_layer_send_visibility(resource,
613                                           prop->visibility);
614     }
615     if (mask & IVI_NOTIFICATION_ADD) {
616         send_layer_add_event(ivilayer, resource);
617     }
618     if (mask & IVI_NOTIFICATION_REMOVE) {
619         send_layer_add_event(ivilayer, resource);
620     }
621 }
622
623 static void
624 send_layer_prop(struct ivi_layout_layer *layer,
625                 struct ivi_layout_LayerProperties *prop,
626                 enum ivi_layout_notification_mask mask,
627                 void *userdata)
628 {
629     struct ivilayer *ivilayer = userdata;
630     struct ivicontroller_layer *ctrllayer = NULL;
631     struct ivishell *shell = ivilayer->shell;
632     uint32_t id_layout_layer = 0;
633
634     id_layout_layer = ivi_layout_getIdOfLayer(layer);
635     wl_list_for_each(ctrllayer, &shell->list_controller_layer, link) {
636         if (id_layout_layer != ctrllayer->id_layer) {
637             continue;
638         }
639         send_layer_event(ctrllayer->resource, ivilayer, prop, mask);
640     }
641 }
642
643 static void
644 controller_surface_set_opacity(struct wl_client *client,
645                    struct wl_resource *resource,
646                    wl_fixed_t opacity)
647 {
648     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
649     (void)client;
650     ivi_layout_surfaceSetOpacity(ivisurf->layout_surface, (float)opacity);
651 }
652
653 static void
654 controller_surface_set_source_rectangle(struct wl_client *client,
655                    struct wl_resource *resource,
656                    int32_t x,
657                    int32_t y,
658                    int32_t width,
659                    int32_t height)
660 {
661     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
662     (void)client;
663     ivi_layout_surfaceSetSourceRectangle(ivisurf->layout_surface,
664             (uint32_t)x, (uint32_t)y, (uint32_t)width, (uint32_t)height);
665 }
666
667 static void
668 controller_surface_set_destination_rectangle(struct wl_client *client,
669                      struct wl_resource *resource,
670                      int32_t x,
671                      int32_t y,
672                      int32_t width,
673                      int32_t height)
674 {
675     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
676     (void)client;
677     ivi_layout_surfaceSetDestinationRectangle(ivisurf->layout_surface,
678             (uint32_t)x, (uint32_t)y, (uint32_t)width, (uint32_t)height);
679 }
680
681 static void
682 controller_surface_set_visibility(struct wl_client *client,
683                       struct wl_resource *resource,
684                       uint32_t visibility)
685 {
686     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
687     (void)client;
688     ivi_layout_surfaceSetVisibility(ivisurf->layout_surface, visibility);
689 }
690
691 static void
692 controller_surface_set_configuration(struct wl_client *client,
693                    struct wl_resource *resource,
694                    int32_t width, int32_t height)
695 {
696     /* This interface has been supported yet. */
697     (void)client;
698     (void)resource;
699     (void)width;
700     (void)height;
701 }
702
703 static void
704 controller_surface_set_orientation(struct wl_client *client,
705                    struct wl_resource *resource,
706                    int32_t orientation)
707 {
708     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
709     (void)client;
710     ivi_layout_surfaceSetOrientation(ivisurf->layout_surface, (uint32_t)orientation);
711 }
712
713 static void
714 controller_surface_screenshot(struct wl_client *client,
715                   struct wl_resource *resource,
716                   const char *filename)
717 {
718     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
719     (void)client;
720     ivi_layout_takeSurfaceScreenshot(filename, ivisurf->layout_surface);
721 }
722
723 static void
724 controller_surface_send_stats(struct wl_client *client,
725                               struct wl_resource *resource)
726 {
727     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
728     pid_t pid;
729     uid_t uid;
730     gid_t gid;
731     wl_client_get_credentials(client, &pid, &uid, &gid);
732
733     ivi_controller_surface_send_stats(resource, 0, 0,
734                                       ivisurf->update_count, pid, "");
735 }
736
737 static void
738 controller_surface_destroy(struct wl_client *client,
739               struct wl_resource *resource,
740               int32_t destroy_scene_object)
741 {
742     (void)client;
743     (void)destroy_scene_object;
744     wl_resource_destroy(resource);
745 }
746
747 static void
748 controller_surface_set_input_focus(struct wl_client *client,
749               struct wl_resource *resource,
750               int32_t enabled)
751 {
752     (void)client;
753     (void)resource;
754     (void)enabled;
755 }
756
757 static const
758 struct ivi_controller_surface_interface controller_surface_implementation = {
759     controller_surface_set_visibility,
760     controller_surface_set_opacity,
761     controller_surface_set_source_rectangle,
762     controller_surface_set_destination_rectangle,
763     controller_surface_set_configuration,
764     controller_surface_set_orientation,
765     controller_surface_screenshot,
766     controller_surface_send_stats,
767     controller_surface_destroy,
768     controller_surface_set_input_focus
769 };
770
771 static void
772 controller_layer_set_source_rectangle(struct wl_client *client,
773                    struct wl_resource *resource,
774                    int32_t x,
775                    int32_t y,
776                    int32_t width,
777                    int32_t height)
778 {
779     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
780     (void)client;
781     ivi_layout_layerSetSourceRectangle(ivilayer->layout_layer,
782            (uint32_t)x, (uint32_t)y, (uint32_t)width, (uint32_t)height);
783 }
784
785 static void
786 controller_layer_set_destination_rectangle(struct wl_client *client,
787                  struct wl_resource *resource,
788                  int32_t x,
789                  int32_t y,
790                  int32_t width,
791                  int32_t height)
792 {
793     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
794     (void)client;
795     ivi_layout_layerSetDestinationRectangle(ivilayer->layout_layer,
796             (uint32_t)x, (uint32_t)y, (uint32_t)width, (uint32_t)height);
797 }
798
799 static void
800 controller_layer_set_visibility(struct wl_client *client,
801                     struct wl_resource *resource,
802                     uint32_t visibility)
803 {
804     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
805     (void)client;
806     ivi_layout_layerSetVisibility(ivilayer->layout_layer, visibility);
807 }
808
809 static void
810 controller_layer_set_opacity(struct wl_client *client,
811                  struct wl_resource *resource,
812                  wl_fixed_t opacity)
813 {
814     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
815     (void)client;
816     ivi_layout_layerSetOpacity(ivilayer->layout_layer, (float)opacity);
817 }
818
819 static void
820 controller_layer_set_configuration(struct wl_client *client,
821                  struct wl_resource *resource,
822                  int32_t width,
823                  int32_t height)
824 {
825     /* This interface has been supported yet. */
826     (void)client;
827     (void)resource;
828     (void)width;
829     (void)height;
830 }
831
832 static void
833 controller_layer_set_orientation(struct wl_client *client,
834                  struct wl_resource *resource,
835                  int32_t orientation)
836 {
837     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
838     (void)client;
839     ivi_layout_layerSetOrientation(ivilayer->layout_layer, (uint32_t)orientation);
840 }
841
842 static void
843 controller_layer_clear_surfaces(struct wl_client *client,
844                     struct wl_resource *resource)
845 {
846     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
847     (void)client;
848     ivi_layout_layerSetRenderOrder(ivilayer->layout_layer, NULL, 0);
849 }
850
851 static void
852 controller_layer_add_surface(struct wl_client *client,
853                  struct wl_resource *resource,
854                  struct wl_resource *surface)
855 {
856     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
857     struct ivisurface *ivisurf = wl_resource_get_user_data(surface);
858     (void)client;
859     ivi_layout_layerAddSurface(ivilayer->layout_layer, ivisurf->layout_surface);
860 }
861
862 static void
863 controller_layer_remove_surface(struct wl_client *client,
864                     struct wl_resource *resource,
865                     struct wl_resource *surface)
866 {
867     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
868     struct ivisurface *ivisurf = wl_resource_get_user_data(surface);
869     (void)client;
870     ivi_layout_layerRemoveSurface(ivilayer->layout_layer, ivisurf->layout_surface);
871 }
872
873 static void
874 controller_layer_screenshot(struct wl_client *client,
875                 struct wl_resource *resource,
876                 const char *filename)
877 {
878     (void)client;
879     (void)resource;
880     (void)filename;
881 }
882
883 static void
884 controller_layer_set_render_order(struct wl_client *client,
885                                   struct wl_resource *resource,
886                                   struct wl_array *id_surfaces)
887 {
888     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
889     struct ivi_layout_surface **layoutsurf_array = NULL;
890     struct ivisurface *ivisurf = NULL;
891     uint32_t *id_surface = NULL;
892     uint32_t id_layout_surface = 0;
893     int i = 0;
894     (void)client;
895
896     wl_array_for_each(id_surface, id_surfaces) {
897         wl_list_for_each(ivisurf, &ivilayer->shell->list_surface, link) {
898             id_layout_surface = ivi_layout_getIdOfSurface(ivisurf->layout_surface);
899             if (*id_surface == id_layout_surface) {
900                 layoutsurf_array[i] = ivisurf->layout_surface;
901                 i++;
902                 break;
903             }
904         }
905     }
906
907     ivi_layout_layerSetRenderOrder(ivilayer->layout_layer,
908                                    layoutsurf_array, id_surfaces->size);
909     free(layoutsurf_array);
910 }
911
912 static void
913 controller_layer_destroy(struct wl_client *client,
914               struct wl_resource *resource,
915               int32_t destroy_scene_object)
916 {
917     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
918     struct ivishell *shell = ivilayer->shell;
919     struct ivicontroller_layer *ctrllayer = NULL;
920     struct ivicontroller_layer *next = NULL;
921     uint32_t id_layer = ivi_layout_getIdOfLayer(ivilayer->layout_layer);
922     (void)client;
923     (void)destroy_scene_object;
924
925     ivilayer->layer_canbe_removed = 1;
926     wl_list_for_each_safe(ctrllayer, next, &shell->list_controller_layer, link) {
927         if (ctrllayer->resource != resource) {
928             continue;
929     }
930
931         wl_resource_destroy(resource);
932         break;
933     }
934 }
935
936 static const
937 struct ivi_controller_layer_interface controller_layer_implementation = {
938     controller_layer_set_visibility,
939     controller_layer_set_opacity,
940     controller_layer_set_source_rectangle,
941     controller_layer_set_destination_rectangle,
942     controller_layer_set_configuration,
943     controller_layer_set_orientation,
944     controller_layer_screenshot,
945     controller_layer_clear_surfaces,
946     controller_layer_add_surface,
947     controller_layer_remove_surface,
948     controller_layer_set_render_order,
949     controller_layer_destroy
950 };
951
952 static void
953 controller_screen_destroy(struct wl_client *client,
954                           struct wl_resource *resource)
955 {
956     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
957     struct ivicontroller_screen *ctrlscrn = NULL;
958     struct ivicontroller_screen *next = NULL;
959 //    uint32_t id_screen = ivi_layout_getIdOfScreen(iviscrn->layout_screen);
960     (void)client;
961
962     wl_list_for_each_safe(ctrlscrn, next,
963                           &iviscrn->shell->list_controller_screen, link) {
964         if (resource != ctrlscrn->resource) {
965             continue;
966         }
967
968         wl_list_remove(&ctrlscrn->link);
969         free(ctrlscrn);
970         ctrlscrn = NULL;
971         wl_resource_destroy(ctrlscrn->resource);
972         break;
973     }
974 }
975
976 static void
977 controller_screen_clear(struct wl_client *client,
978                 struct wl_resource *resource)
979 {
980     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
981     (void)client;
982     ivi_layout_screenSetRenderOrder(iviscrn->layout_screen, NULL, 0);
983 }
984
985 static void
986 controller_screen_add_layer(struct wl_client *client,
987                 struct wl_resource *resource,
988                 struct wl_resource *layer)
989 {
990     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
991     struct ivilayer *ivilayer = wl_resource_get_user_data(layer);
992     (void)client;
993     ivi_layout_screenAddLayer(iviscrn->layout_screen, ivilayer->layout_layer);
994 }
995
996 static void
997 controller_screen_screenshot(struct wl_client *client,
998                 struct wl_resource *resource,
999                 const char *filename)
1000 {
1001     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1002     (void)client;
1003     ivi_layout_takeScreenshot(iviscrn->layout_screen, filename);
1004 }
1005
1006 static void
1007 controller_screen_set_render_order(struct wl_client *client,
1008                 struct wl_resource *resource,
1009                 struct wl_array *id_layers)
1010 {
1011     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1012     struct ivi_layout_layer **layoutlayer_array = NULL;
1013     struct ivilayer *ivilayer = NULL;
1014     uint32_t *id_layer = NULL;
1015     uint32_t id_layout_layer = 0;
1016     int i = 0;
1017     (void)client;
1018
1019     *layoutlayer_array = (struct ivi_layout_layer*)calloc(
1020                             id_layers->size, sizeof(void*));
1021
1022     wl_array_for_each(id_layer, id_layers) {
1023         wl_list_for_each(ivilayer, &iviscrn->shell->list_layer, link) {
1024             id_layout_layer = ivi_layout_getIdOfLayer(ivilayer->layout_layer);
1025             if (*id_layer == id_layout_layer) {
1026                 layoutlayer_array[i] = ivilayer->layout_layer;
1027                 i++;
1028                 break;
1029             }
1030         }
1031     }
1032
1033     ivi_layout_screenSetRenderOrder(iviscrn->layout_screen,
1034                                     layoutlayer_array, id_layers->size);
1035     free(layoutlayer_array);
1036 }
1037
1038 static const
1039 struct ivi_controller_screen_interface controller_screen_implementation = {
1040     controller_screen_destroy,
1041     controller_screen_clear,
1042     controller_screen_add_layer,
1043     controller_screen_screenshot,
1044     controller_screen_set_render_order
1045 };
1046
1047 static void
1048 controller_commit_changes(struct wl_client *client,
1049                           struct wl_resource *resource)
1050 {
1051     int32_t ans = 0;
1052     (void)client;
1053     (void)resource;
1054
1055     ans = ivi_layout_commitChanges();
1056     if (ans < 0) {
1057         weston_log("Failed to commit changes at controller_commit_changes\n");
1058     }
1059 }
1060
1061 static void
1062 controller_layer_create(struct wl_client *client,
1063                         struct wl_resource *resource,
1064                         uint32_t id_layer,
1065                         int32_t width,
1066                         int32_t height,
1067                         uint32_t id)
1068 {
1069     struct ivicontroller *ctrl = wl_resource_get_user_data(resource);
1070     struct ivishell *shell = ctrl->shell;
1071     struct ivi_layout_layer *layout_layer = NULL;
1072     struct ivicontroller_layer *ctrllayer = NULL;
1073     struct ivilayer *ivilayer = NULL;
1074     struct ivi_layout_LayerProperties prop;
1075
1076     ivilayer = get_layer(&shell->list_layer, id_layer);
1077     if (ivilayer == NULL) {
1078         layout_layer = ivi_layout_layerCreateWithDimension(id_layer,
1079                            (uint32_t)width, (uint32_t)height);
1080         if (layout_layer == NULL) {
1081             weston_log("id_layer is already created\n");
1082             return;
1083         }
1084
1085         /* ivilayer will be created by layer_event_create */
1086         ivilayer = get_layer(&shell->list_layer, id_layer);
1087         if (ivilayer == NULL) {
1088             weston_log("couldn't get layer object\n");
1089             return;
1090         }
1091     }
1092
1093     ctrllayer = calloc(1, sizeof *ctrllayer);
1094     if (!ctrllayer) {
1095         weston_log("no memory to allocate client layer\n");
1096         return;
1097     }
1098
1099     ++ivilayer->controller_layer_count;
1100     ivilayer->layer_canbe_removed = 0;
1101
1102     ctrllayer->shell = shell;
1103     ctrllayer->client = client;
1104     ctrllayer->id = id;
1105     ctrllayer->id_layer = id_layer;
1106     ctrllayer->resource = wl_resource_create(client,
1107                                &ivi_controller_layer_interface, 1, id);
1108     if (ctrllayer->resource == NULL) {
1109         weston_log("couldn't get layer object\n");
1110         return;
1111     }
1112
1113     wl_list_init(&ctrllayer->link);
1114     wl_list_insert(&shell->list_controller_layer, &ctrllayer->link);
1115
1116     wl_resource_set_implementation(ctrllayer->resource,
1117                                    &controller_layer_implementation,
1118                                    ivilayer, destroy_ivicontroller_layer);
1119
1120     memset(&prop, 0, sizeof prop);
1121
1122     ivi_layout_getPropertiesOfLayer(ivilayer->layout_layer, &prop);
1123     send_layer_event(ctrllayer->resource, ivilayer,
1124                      &prop, IVI_NOTIFICATION_ALL);
1125 }
1126
1127 static void
1128 surface_event_content(struct ivi_layout_surface *layout_surface, int32_t content, void *userdata)
1129 {
1130     struct ivishell *shell = userdata;
1131     struct ivicontroller_surface *ctrlsurf = NULL;
1132     uint32_t id_surface = 0;
1133
1134     if (content == 0) {
1135         surface_event_remove(layout_surface, userdata);
1136     }
1137 }
1138
1139 static void
1140 controller_surface_create(struct wl_client *client,
1141                           struct wl_resource *resource,
1142                           uint32_t id_surface,
1143                           uint32_t id)
1144 {
1145     struct ivicontroller *ctrl = wl_resource_get_user_data(resource);
1146     struct ivishell *shell = ctrl->shell;
1147     struct ivicontroller_surface *ctrlsurf = NULL;
1148     struct ivi_layout_SurfaceProperties prop;
1149     struct ivisurface *ivisurf = NULL;
1150
1151     ctrlsurf = calloc(1, sizeof *ctrlsurf);
1152     if (!ctrlsurf) {
1153         weston_log("no memory to allocate controller surface\n");
1154         return;
1155     }
1156
1157     ctrlsurf->shell = shell;
1158     ctrlsurf->client = client;
1159     ctrlsurf->id = id;
1160     ctrlsurf->id_surface = id_surface;
1161     wl_list_init(&ctrlsurf->link);
1162     wl_list_insert(&shell->list_controller_surface, &ctrlsurf->link);
1163
1164     ctrlsurf->resource = wl_resource_create(client,
1165                                &ivi_controller_surface_interface, 1, id);
1166     if (ctrlsurf->resource == NULL) {
1167         weston_log("couldn't surface object");
1168         return;
1169     }
1170
1171     ivisurf = get_surface(&shell->list_surface, id_surface);
1172     if (ivisurf == NULL) {
1173         return;
1174     }
1175
1176     ++ivisurf->controller_surface_count;
1177
1178     wl_resource_set_implementation(ctrlsurf->resource,
1179                                    &controller_surface_implementation,
1180                                    ivisurf, destroy_ivicontroller_surface);
1181
1182     memset(&prop, 0, sizeof prop);
1183
1184     ivi_layout_getPropertiesOfSurface(ivisurf->layout_surface, &prop);
1185     ivi_layout_surfaceSetContentObserver(ivisurf->layout_surface, surface_event_content, shell);
1186
1187     send_surface_event(ctrlsurf->resource, ivisurf,
1188                        &prop, IVI_NOTIFICATION_ALL);
1189 }
1190
1191 static const struct ivi_controller_interface controller_implementation = {
1192     controller_commit_changes,
1193     controller_layer_create,
1194     controller_surface_create
1195 };
1196
1197 static void
1198 add_client_to_resources(struct ivishell *shell,
1199                         struct wl_client *client,
1200                         struct ivicontroller *controller)
1201 {
1202     struct ivisurface* ivisurf = NULL;
1203     struct ivilayer* ivilayer = NULL;
1204     struct iviscreen* iviscrn = NULL;
1205     struct ivicontroller_screen *ctrlscrn = NULL;
1206     struct wl_resource *resource_output = NULL;
1207     uint32_t id_layout_surface = 0;
1208     uint32_t id_layout_layer = 0;
1209
1210     wl_list_for_each(ivisurf, &shell->list_surface, link) {
1211         id_layout_surface =
1212             ivi_layout_getIdOfSurface(ivisurf->layout_surface);
1213
1214         ivi_controller_send_surface(controller->resource,
1215                                     id_layout_surface);
1216     }
1217
1218     wl_list_for_each(ivilayer, &shell->list_layer, link) {
1219         id_layout_layer =
1220             ivi_layout_getIdOfLayer(ivilayer->layout_layer);
1221
1222         ivi_controller_send_layer(controller->resource,
1223                                   id_layout_layer);
1224     }
1225
1226     wl_list_for_each(iviscrn, &shell->list_screen, link) {
1227         resource_output = wl_resource_find_for_client(
1228                 &iviscrn->output->resource_list, client);
1229         if (resource_output == NULL) {
1230             continue;
1231         }
1232
1233         ctrlscrn = controller_screen_create(iviscrn->shell, client, iviscrn);
1234         if (ctrlscrn == NULL) {
1235             continue;
1236         }
1237
1238         ivi_controller_send_screen(controller->resource,
1239                                    wl_resource_get_id(resource_output),
1240                                    ctrlscrn->resource);
1241     }
1242 }
1243
1244 static void
1245 bind_ivi_controller(struct wl_client *client, void *data,
1246                     uint32_t version, uint32_t id)
1247 {
1248     struct ivishell *shell = data;
1249     struct ivicontroller *controller;
1250     (void)version;
1251
1252     controller = calloc(1, sizeof *controller);
1253     if (controller == NULL) {
1254         weston_log("no memory to allocate controller\n");
1255         return;
1256     }
1257
1258     controller->resource =
1259         wl_resource_create(client, &ivi_controller_interface, 1, id);
1260     wl_resource_set_implementation(controller->resource,
1261                                    &controller_implementation,
1262                                    controller, unbind_resource_controller);
1263
1264     controller->shell = shell;
1265     controller->client = client;
1266     controller->id = id;
1267
1268     wl_list_init(&controller->link);
1269     wl_list_insert(&shell->list_controller, &controller->link);
1270
1271     add_client_to_resources(shell, client, controller);
1272 }
1273
1274 static struct iviscreen*
1275 create_screen(struct ivishell *shell, struct weston_output *output)
1276 {
1277     struct iviscreen *iviscrn;
1278     iviscrn = calloc(1, sizeof *iviscrn);
1279     if (iviscrn == NULL) {
1280         weston_log("no memory to allocate client screen\n");
1281         return NULL;
1282     }
1283
1284     iviscrn->shell = shell;
1285     iviscrn->output = output;
1286
1287 // TODO : Only Single display
1288     iviscrn->layout_screen = ivi_layout_getScreenFromId(0);
1289
1290     wl_list_init(&iviscrn->link);
1291
1292     return iviscrn;
1293 }
1294
1295 static struct ivilayer*
1296 create_layer(struct ivishell *shell,
1297              struct ivi_layout_layer *layout_layer,
1298              uint32_t id_layer)
1299 {
1300     struct ivilayer *ivilayer = NULL;
1301     struct ivicontroller *controller = NULL;
1302
1303     ivilayer = get_layer(&shell->list_layer, id_layer);
1304     if (ivilayer != NULL) {
1305         weston_log("id_layer is already created\n");
1306         return NULL;
1307     }
1308
1309     ivilayer = calloc(1, sizeof *ivilayer);
1310     if (NULL == ivilayer) {
1311         weston_log("no memory to allocate client layer\n");
1312         return NULL;
1313     }
1314
1315     ivilayer->shell = shell;
1316     wl_list_init(&ivilayer->list_screen);
1317     wl_list_init(&ivilayer->link);
1318     wl_list_insert(&shell->list_layer, &ivilayer->link);
1319     ivilayer->layout_layer = layout_layer;
1320
1321     ivi_layout_layerAddNotification(layout_layer, send_layer_prop, ivilayer);
1322
1323     wl_list_for_each(controller, &shell->list_controller, link) {
1324         ivi_controller_send_layer(controller->resource, id_layer);
1325     }
1326
1327     return ivilayer;
1328 }
1329
1330 static struct ivisurface*
1331 create_surface(struct ivishell *shell,
1332                struct ivi_layout_surface *layout_surface,
1333                uint32_t id_surface)
1334 {
1335     struct ivisurface *ivisurf = NULL;
1336     struct ivicontroller *controller = NULL;
1337
1338     ivisurf = get_surface(&shell->list_surface, id_surface);
1339     if (ivisurf != NULL) {
1340         weston_log("id_surface is already created\n");
1341         return NULL;
1342     }
1343
1344     ivisurf = calloc(1, sizeof *ivisurf);
1345     if (ivisurf == NULL) {
1346         weston_log("no memory to allocate client surface\n");
1347         return NULL;
1348     }
1349
1350     ivisurf->shell = shell;
1351     ivisurf->layout_surface = layout_surface;
1352     wl_list_init(&ivisurf->list_layer);
1353     wl_list_init(&ivisurf->link);
1354     wl_list_insert(&shell->list_surface, &ivisurf->link);
1355
1356     wl_list_for_each(controller, &shell->list_controller, link) {
1357         ivi_controller_send_surface(controller->resource,
1358                                     id_surface);
1359     }
1360
1361     ivi_layout_surfaceAddNotification(layout_surface,
1362                                     send_surface_prop, ivisurf);
1363
1364     return ivisurf;
1365 }
1366
1367 static void
1368 layer_event_create(struct ivi_layout_layer *layout_layer,
1369                      void *userdata)
1370 {
1371     struct ivishell *shell = userdata;
1372     struct ivilayer *ivilayer = NULL;
1373     uint32_t id_layer = 0;
1374
1375     id_layer = ivi_layout_getIdOfLayer(layout_layer);
1376
1377     ivilayer = create_layer(shell, layout_layer, id_layer);
1378     if (ivilayer == NULL) {
1379         weston_log("failed to create layer");
1380         return;
1381     }
1382 }
1383
1384 static void
1385 layer_event_remove(struct ivi_layout_layer *layout_layer,
1386                      void *userdata)
1387 {
1388     struct ivishell *shell = userdata;
1389     struct ivicontroller_layer *ctrllayer = NULL;
1390     struct ivilayer *ivilayer = NULL;
1391     struct ivilayer *next = NULL;
1392     uint32_t id_layer = 0;
1393     int is_removed = 0;
1394
1395     wl_list_for_each_safe(ivilayer, next, &shell->list_layer, link) {
1396         if (layout_layer != ivilayer->layout_layer) {
1397             continue;
1398         }
1399
1400         wl_list_remove(&ivilayer->link);
1401
1402         is_removed = 1;
1403         free(ivilayer);
1404         ivilayer = NULL;
1405         break;
1406     }
1407
1408     if (is_removed) {
1409         id_layer = ivi_layout_getIdOfLayer(layout_layer);
1410
1411         wl_list_for_each(ctrllayer, &shell->list_controller_layer, link) {
1412             if (id_layer != ctrllayer->id_layer) {
1413                 continue;
1414             }
1415             ivi_controller_layer_send_destroyed(ctrllayer->resource);
1416         }
1417     }
1418 }
1419
1420
1421 static void
1422 surface_event_create(struct ivi_layout_surface *layout_surface,
1423                      void *userdata)
1424 {
1425     struct ivishell *shell = userdata;
1426     struct ivisurface *ivisurf = NULL;
1427     uint32_t id_surface = 0;
1428
1429     id_surface = ivi_layout_getIdOfSurface(layout_surface);
1430
1431     ivisurf = create_surface(shell, layout_surface, id_surface);
1432     if (ivisurf == NULL) {
1433         weston_log("failed to create surface");
1434         return;
1435     }
1436 }
1437
1438 static void
1439 surface_event_remove(struct ivi_layout_surface *layout_surface,
1440                      void *userdata)
1441 {
1442     struct ivishell *shell = userdata;
1443     struct ivicontroller_surface *ctrlsurf = NULL;
1444     struct ivisurface *ivisurf = NULL;
1445     struct ivisurface *next = NULL;
1446     uint32_t id_surface = 0;
1447     int is_removed = 0;
1448
1449     wl_list_for_each_safe(ivisurf, next, &shell->list_surface, link) {
1450         if (layout_surface != ivisurf->layout_surface) {
1451             continue;
1452         }
1453
1454         wl_list_remove(&ivisurf->link);
1455         is_removed = 1;
1456
1457         if (ivisurf->controller_surface_count == 0) {
1458             free(ivisurf);
1459         }
1460         else {
1461             ivisurf->can_be_removed = 1;
1462         }
1463
1464         break;
1465     }
1466
1467     if (is_removed) {
1468         id_surface = ivi_layout_getIdOfSurface(layout_surface);
1469
1470         wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
1471             if (id_surface != ctrlsurf->id_surface) {
1472                 continue;
1473             }
1474             ivi_controller_surface_send_destroyed(ctrlsurf->resource);
1475         }
1476     }
1477 }
1478
1479 static void
1480 surface_event_configure(struct ivi_layout_surface *layout_surface,
1481                         void *userdata)
1482 {
1483     struct ivishell *shell = userdata;
1484     struct ivisurface *ivisurf = NULL;
1485     struct ivicontroller_surface *ctrlsurf = NULL;
1486     struct ivi_layout_SurfaceProperties prop;
1487     uint32_t id_surface = 0;
1488
1489     id_surface = ivi_layout_getIdOfSurface(layout_surface);
1490
1491     ivisurf = get_surface(&shell->list_surface, id_surface);
1492     if (ivisurf == NULL) {
1493         weston_log("id_surface is not created yet\n");
1494         return;
1495     }
1496
1497     memset(&prop, 0, sizeof prop);
1498     ivi_layout_getPropertiesOfSurface(layout_surface, &prop);
1499
1500     wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
1501         if (id_surface != ctrlsurf->id_surface) {
1502             continue;
1503         }
1504         send_surface_event(ctrlsurf->resource, ivisurf,
1505                            &prop, IVI_NOTIFICATION_ALL);
1506     }
1507 }
1508
1509 static int32_t
1510 check_layout_layers(struct ivishell *shell)
1511 {
1512     struct ivi_layout_layer **pArray = NULL;
1513     struct ivilayer *ivilayer = NULL;
1514     uint32_t id_layer = 0;
1515     uint32_t length = 0;
1516     uint32_t i = 0;
1517     int32_t ret = 0;
1518
1519     ret = ivi_layout_getLayers(&length, &pArray);
1520     if(ret != 0) {
1521         weston_log("failed to get layers at check_layout_layers\n");
1522         return -1;
1523     }
1524
1525     if (length == 0) {
1526         /* if length is 0, pArray doesn't need to free.*/
1527         return 0;
1528     }
1529
1530     for (i = 0; i < length; i++) {
1531         id_layer = ivi_layout_getIdOfLayer(pArray[i]);
1532         ivilayer = create_layer(shell, pArray[i], id_layer);
1533         if (ivilayer == NULL) {
1534             weston_log("failed to create layer");
1535         }
1536     }
1537
1538     free(pArray);
1539     pArray = NULL;
1540
1541     return 0;
1542 }
1543
1544 static int32_t
1545 check_layout_surfaces(struct ivishell *shell)
1546 {
1547     struct ivi_layout_surface **pArray = NULL;
1548     struct ivisurface *ivisurf = NULL;
1549     uint32_t id_surface = 0;
1550     uint32_t length = 0;
1551     uint32_t i = 0;
1552     int32_t ret = 0;
1553
1554     ret = ivi_layout_getSurfaces(&length, &pArray);
1555     if(ret != 0) {
1556         weston_log("failed to get surfaces at check_layout_surfaces\n");
1557         return -1;
1558     }
1559
1560     if (length == 0) {
1561         /* if length is 0, pArray doesn't need to free.*/
1562         return 0;
1563     }
1564
1565     for (i = 0; i < length; i++) {
1566         id_surface = ivi_layout_getIdOfSurface(pArray[i]);
1567         ivisurf = create_surface(shell, pArray[i], id_surface);
1568         if (ivisurf == NULL) {
1569             weston_log("failed to create surface");
1570         }
1571     }
1572
1573     free(pArray);
1574     pArray = NULL;
1575
1576     return 0;
1577 }
1578
1579 static void
1580 init_ivi_shell(struct weston_compositor *ec, struct ivishell *shell)
1581 {
1582     struct weston_output *output = NULL;
1583     struct iviscreen *iviscrn = NULL;
1584     int32_t ret = 0;
1585
1586     shell->compositor = ec;
1587
1588     wl_list_init(&shell->list_surface);
1589     wl_list_init(&shell->list_layer);
1590     wl_list_init(&shell->list_screen);
1591     wl_list_init(&shell->list_weston_surface);
1592     wl_list_init(&shell->list_controller);
1593     wl_list_init(&shell->list_controller_screen);
1594     wl_list_init(&shell->list_controller_layer);
1595     wl_list_init(&shell->list_controller_surface);
1596     shell->event_restriction = 0;
1597
1598     wl_list_for_each(output, &ec->output_list, link) {
1599         iviscrn = create_screen(shell, output);
1600         if (iviscrn != NULL) {
1601             wl_list_insert(&shell->list_screen, &iviscrn->link);
1602         }
1603     }
1604
1605     ret = check_layout_layers(shell);
1606     if (ret != 0) {
1607         weston_log("failed to check_layout_layers");
1608     }
1609
1610     ret = check_layout_surfaces(shell);
1611     if (ret != 0) {
1612         weston_log("failed to check_layout_surfaces");
1613     }
1614
1615     ivi_layout_addNotificationCreateLayer(layer_event_create, shell);
1616     ivi_layout_addNotificationRemoveLayer(layer_event_remove, shell);
1617
1618     ivi_layout_addNotificationCreateSurface(surface_event_create, shell);
1619     ivi_layout_addNotificationRemoveSurface(surface_event_remove, shell);
1620     ivi_layout_addNotificationConfigureSurface(surface_event_configure, shell);
1621 }
1622
1623 WL_EXPORT int
1624 module_init(struct weston_compositor *ec,
1625             int *argc, char *argv[])
1626 {
1627     struct ivishell *shell;
1628     (void)argc;
1629     (void)argv;
1630
1631     shell = malloc(sizeof *shell);
1632     if (shell == NULL)
1633         return -1;
1634
1635     memset(shell, 0, sizeof *shell);
1636     init_ivi_shell(ec, shell);
1637
1638     if (wl_global_create(ec->wl_display, &ivi_controller_interface, 1,
1639                          shell, bind_ivi_controller) == NULL) {
1640         return -1;
1641     }
1642
1643     return 0;
1644 }