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