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