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