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