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