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