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