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