231567c0bc4b8a47cc6a01cdccc551a2e6a4ea60
[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 #include <GLES2/gl2.h>
31
32 #include "weston/compositor.h"
33 #include "ivi-controller-server-protocol.h"
34 #include "ivi-layout-export.h"
35 #include "bitmap.h"
36
37 struct ivishell;
38 struct ivilayer;
39 struct iviscreen;
40
41 struct link_layer {
42     struct ivilayer *layer;
43     struct wl_list link;
44 };
45
46 struct link_screen {
47     struct iviscreen *screen;
48     struct wl_list link;
49 };
50
51 struct ivisurface {
52     struct wl_list link;
53     struct wl_client *client;
54     struct ivishell *shell;
55     uint32_t update_count;
56     struct ivi_layout_surface *layout_surface;
57     struct wl_listener surface_destroy_listener;
58     struct wl_list list_layer;
59     uint32_t controller_surface_count;
60     int can_be_removed;
61 };
62
63 struct ivilayer {
64     struct wl_list link;
65     struct ivishell *shell;
66     struct ivi_layout_layer *layout_layer;
67     struct wl_list list_screen;
68     uint32_t controller_layer_count;
69     int layer_canbe_removed;
70 };
71
72 struct iviscreen {
73     struct wl_list link;
74     struct ivishell *shell;
75     struct ivi_layout_screen *layout_screen;
76     struct weston_output *output;
77 };
78
79 struct ivicontroller_surface {
80     struct wl_resource *resource;
81     uint32_t id;
82     uint32_t id_surface;
83     struct wl_client *client;
84     struct wl_list link;
85     struct ivishell *shell;
86     int implementation_set;
87 };
88
89 struct ivicontroller_layer {
90     struct wl_resource *resource;
91     uint32_t id;
92     uint32_t id_layer;
93     struct wl_client *client;
94     struct wl_list link;
95     struct ivishell *shell;
96 };
97
98 struct ivicontroller_screen {
99     struct wl_resource *resource;
100     uint32_t id;
101     uint32_t id_screen;
102     struct wl_client *client;
103     struct wl_list link;
104     struct ivishell *shell;
105 };
106
107 struct ivicontroller {
108     struct wl_resource *resource;
109     uint32_t id;
110     struct wl_client *client;
111     struct wl_list link;
112     struct ivishell *shell;
113 };
114
115 struct link_shell_weston_surface
116 {
117     struct wl_resource *resource;
118     struct wl_listener destroy_listener;
119     struct weston_surface *surface;
120     struct wl_list link;
121 };
122
123 struct ivishell {
124     struct wl_resource *resource;
125
126     struct wl_listener destroy_listener;
127
128     struct weston_compositor *compositor;
129
130     struct weston_surface *surface;
131
132     struct weston_process process;
133
134     struct weston_seat *seat;
135
136     struct wl_list list_surface;
137     struct wl_list list_layer;
138     struct wl_list list_screen;
139
140     struct wl_list list_weston_surface;
141
142     struct wl_list list_controller;
143     struct wl_list list_controller_surface;
144     struct wl_list list_controller_layer;
145     struct wl_list list_controller_screen;
146
147     struct {
148         struct weston_process process;
149         struct wl_client *client;
150         struct wl_resource *desktop_shell;
151
152         unsigned deathcount;
153         uint32_t deathstamp;
154     } child;
155
156     int state;
157     int previous_state;
158     int event_restriction;
159 };
160 static void surface_event_remove(struct ivi_layout_surface *, void *);
161
162 static void
163 destroy_ivicontroller_surface(struct wl_resource *resource)
164 {
165     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
166     struct ivishell *shell = ivisurf->shell;
167     struct ivicontroller_surface *ctrlsurf = NULL;
168     struct ivicontroller_surface *next = NULL;
169     int is_removed = 0;
170
171     wl_list_for_each_safe(ctrlsurf, next,
172                           &shell->list_controller_surface, link) {
173
174         if (resource != ctrlsurf->resource) {
175             continue;
176         }
177
178         if (!wl_list_empty(&ctrlsurf->link)) {
179             wl_list_remove(&ctrlsurf->link);
180         }
181
182         is_removed = 1;
183         free(ctrlsurf);
184         ctrlsurf = NULL;
185         --ivisurf->controller_surface_count;
186         break;
187     }
188
189     if ((is_removed) && (ivisurf->controller_surface_count == 0)) {
190         if (ivisurf->can_be_removed) {
191             free(ivisurf);
192         }
193     }
194 }
195
196 static void
197 destroy_ivicontroller_layer(struct wl_resource *resource)
198 {
199     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
200     struct ivishell *shell = ivilayer->shell;
201     struct ivicontroller_layer *ctrllayer = NULL;
202     struct ivicontroller_layer *next = NULL;
203     uint32_t id_layer = 0;
204
205     id_layer = ivi_layout_getIdOfLayer(ivilayer->layout_layer);
206
207     wl_list_for_each_safe(ctrllayer, next,
208                           &shell->list_controller_layer, link) {
209
210         if (resource != ctrllayer->resource) {
211             continue;
212         }
213
214         wl_list_remove(&ctrllayer->link);
215         --ivilayer->controller_layer_count;
216         ivi_controller_layer_send_destroyed(ctrllayer->resource);
217         free(ctrllayer);
218         ctrllayer = NULL;
219         break;
220     }
221
222     if ((ivilayer->layout_layer != NULL) &&
223         (ivilayer->controller_layer_count == 0) &&
224         (ivilayer->layer_canbe_removed == 1)) {
225         ivi_layout_layerRemove(ivilayer->layout_layer);
226     }
227 }
228
229 static void
230 destroy_ivicontroller_screen(struct wl_resource *resource)
231 {
232     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
233     struct ivicontroller_screen *ctrlscrn = NULL;
234     struct ivicontroller_screen *next = NULL;
235
236     wl_list_for_each_safe(ctrlscrn, next,
237                           &iviscrn->shell->list_controller_screen, link) {
238 // TODO : Only Single display
239 #if 0
240         if (iviscrn->output->id != ctrlscrn->id_screen) {
241             continue;
242         }
243 #endif
244
245         if (resource != ctrlscrn->resource) {
246             continue;
247         }
248
249         wl_list_remove(&ctrlscrn->link);
250         free(ctrlscrn);
251         ctrlscrn = NULL;
252         break;
253     }
254 }
255
256 static void
257 unbind_resource_controller(struct wl_resource *resource)
258 {
259     struct ivicontroller *controller = wl_resource_get_user_data(resource);
260
261     wl_list_remove(&controller->link);
262
263     free(controller);
264     controller = NULL;
265 }
266
267 static struct ivisurface*
268 get_surface(struct wl_list *list_surf, uint32_t id_surface)
269 {
270     struct ivisurface *ivisurf = NULL;
271     uint32_t ivisurf_id = 0;
272
273     wl_list_for_each(ivisurf, list_surf, link) {
274         ivisurf_id = ivi_layout_getIdOfSurface(ivisurf->layout_surface);
275         if (ivisurf_id == id_surface) {
276             return ivisurf;
277         }
278     }
279
280     return NULL;
281 }
282
283 static struct ivilayer*
284 get_layer(struct wl_list *list_layer, uint32_t id_layer)
285 {
286     struct ivilayer *ivilayer = NULL;
287     uint32_t ivilayer_id = 0;
288
289     wl_list_for_each(ivilayer, list_layer, link) {
290         ivilayer_id = ivi_layout_getIdOfLayer(ivilayer->layout_layer);
291         if (ivilayer_id == id_layer) {
292             return ivilayer;
293         }
294     }
295
296     return NULL;
297 }
298
299 static const
300 struct ivi_controller_screen_interface controller_screen_implementation;
301
302 static struct ivicontroller_screen*
303 controller_screen_create(struct ivishell *shell,
304                          struct wl_client *client,
305                          struct iviscreen *iviscrn)
306 {
307     struct ivicontroller_screen *ctrlscrn = NULL;
308
309     ctrlscrn = calloc(1, sizeof *ctrlscrn);
310     if (ctrlscrn == NULL) {
311         weston_log("no memory to allocate controller screen\n");
312         return NULL;
313     }
314
315     ctrlscrn->client = client;
316     ctrlscrn->shell  = shell;
317 // FIXME
318 // TODO : Only Single display
319 #if 0
320     /* ctrlscrn->id_screen = iviscrn->id_screen; */
321 #else
322     ctrlscrn->id_screen = 0;
323 #endif
324
325     ctrlscrn->resource =
326         wl_resource_create(client, &ivi_controller_screen_interface, 1, 0);
327     if (ctrlscrn->resource == NULL) {
328         weston_log("couldn't new screen controller object");
329
330         free(ctrlscrn);
331         ctrlscrn = NULL;
332
333         return NULL;
334     }
335
336     wl_resource_set_implementation(ctrlscrn->resource,
337                                    &controller_screen_implementation,
338                                    iviscrn, destroy_ivicontroller_screen);
339
340     wl_list_init(&ctrlscrn->link);
341     wl_list_insert(&shell->list_controller_screen, &ctrlscrn->link);
342
343     return ctrlscrn;
344 }
345
346 static void
347 send_surface_add_event(struct ivisurface *ivisurf,
348                        struct wl_resource *resource,
349                        enum ivi_layout_notification_mask mask)
350 {
351     struct ivi_layout_layer **pArray = NULL;
352     uint32_t length = 0;
353     int32_t ans = 0;
354     int i = 0;
355     struct link_layer *link_layer = NULL;
356     struct link_layer *next = NULL;
357     struct ivicontroller_layer *ctrllayer = NULL;
358     struct ivilayer *ivilayer = NULL;
359     struct ivishell *shell = ivisurf->shell;
360     uint32_t id_layout_layer = 0;
361     struct wl_client *surface_client = wl_resource_get_client(resource);
362     int found = 0;
363
364     ans = ivi_layout_getLayersUnderSurface(ivisurf->layout_surface,
365                                           &length, &pArray);
366     if (0 != ans) {
367         weston_log("failed to get layers at send_surface_add_event\n");
368         return;
369     }
370
371     /* Send Null to cancel added surface */
372     if (mask & IVI_NOTIFICATION_REMOVE) {
373         wl_list_for_each_safe(link_layer, next, &ivisurf->list_layer, link) {
374             ivi_controller_surface_send_layer(resource, NULL);
375         }
376     }
377     else if (mask & IVI_NOTIFICATION_ADD) {
378         for (i = 0; i < (int)length; i++) {
379             /* Send new surface event */
380             ivilayer = NULL;
381             wl_list_for_each(ivilayer, &shell->list_layer, link) {
382                 if (ivilayer->layout_layer == pArray[i]) {
383                     break;
384                 }
385             }
386
387             if (ivilayer == NULL) {
388                 continue;
389             }
390
391             id_layout_layer =
392                 ivi_layout_getIdOfLayer(ivilayer->layout_layer);
393             wl_list_for_each(ctrllayer, &shell->list_controller_layer, link) {
394                 if (id_layout_layer != ctrllayer->id_layer) {
395                     continue;
396                 }
397
398                 struct wl_client *layer_client = wl_resource_get_client(ctrllayer->resource);
399                 if (surface_client != layer_client) {
400                     continue;
401                 }
402
403                 ivi_controller_surface_send_layer(resource, ctrllayer->resource);
404             }
405         }
406     }
407
408     free(pArray);
409     pArray = NULL;
410 }
411
412 static void
413 send_surface_event(struct wl_resource *resource,
414                    struct ivisurface *ivisurf,
415                    struct ivi_layout_SurfaceProperties *prop,
416                    uint32_t mask)
417 {
418     if (mask & IVI_NOTIFICATION_OPACITY) {
419         ivi_controller_surface_send_opacity(resource,
420                                             prop->opacity);
421     }
422     if (mask & IVI_NOTIFICATION_SOURCE_RECT) {
423         ivi_controller_surface_send_source_rectangle(resource,
424             prop->sourceX, prop->sourceY,
425             prop->sourceWidth, prop->sourceHeight);
426     }
427     if (mask & IVI_NOTIFICATION_DEST_RECT) {
428         ivi_controller_surface_send_destination_rectangle(resource,
429             prop->destX, prop->destY,
430             prop->destWidth, prop->destHeight);
431     }
432     if (mask & IVI_NOTIFICATION_ORIENTATION) {
433         ivi_controller_surface_send_orientation(resource,
434                                                 prop->orientation);
435     }
436     if (mask & IVI_NOTIFICATION_VISIBILITY) {
437         ivi_controller_surface_send_visibility(resource,
438                                                prop->visibility);
439     }
440     if (mask & IVI_NOTIFICATION_PIXELFORMAT) {
441         ivi_controller_surface_send_pixelformat(resource,
442                                                 prop->pixelformat);
443     }
444     if (mask & IVI_NOTIFICATION_REMOVE) {
445         send_surface_add_event(ivisurf, resource, IVI_NOTIFICATION_REMOVE);
446     }
447     if (mask & IVI_NOTIFICATION_ADD) {
448         send_surface_add_event(ivisurf, resource, IVI_NOTIFICATION_ADD);
449     }
450 }
451
452 static void
453 update_surface_prop(struct ivisurface *ivisurf,
454                     uint32_t mask)
455 {
456     struct ivi_layout_layer **pArray = NULL;
457     uint32_t length = 0;
458     int32_t ans = 0;
459     int i = 0;
460     struct ivishell *shell = ivisurf->shell;
461
462     ans = ivi_layout_getLayersUnderSurface(ivisurf->layout_surface,
463                                           &length, &pArray);
464     if (0 != ans) {
465         weston_log("failed to get layers at send_surface_add_event\n");
466         return;
467     }
468
469     if (mask & IVI_NOTIFICATION_REMOVE) {
470         struct link_layer *link_layer = NULL;
471         struct link_layer *next = NULL;
472
473         wl_list_for_each_safe(link_layer, next, &ivisurf->list_layer, link) {
474             wl_list_remove(&link_layer->link);
475             free(link_layer);
476             link_layer = NULL;
477         }
478     }
479     if (mask & IVI_NOTIFICATION_ADD) {
480         for (i = 0; i < (int)length; ++i) {
481             /* Create list_layer */
482             struct ivilayer *ivilayer = NULL;
483             struct link_layer *link_layer = calloc(1, sizeof(*link_layer));
484             if (NULL == link_layer) {
485                 continue;
486             }
487             wl_list_init(&link_layer->link);
488             link_layer->layer = NULL;
489             wl_list_for_each(ivilayer, &shell->list_layer, link) {
490                 if (ivilayer->layout_layer == pArray[i]) {
491                     link_layer->layer = ivilayer;
492                     break;
493                 }
494             }
495
496             if (link_layer->layer == NULL) {
497                 free(link_layer);
498                 link_layer = NULL;
499                 continue;
500             }
501
502             wl_list_insert(&ivisurf->list_layer, &link_layer->link);
503         }
504     }
505 }
506
507 static void
508 send_surface_prop(struct ivi_layout_surface *layout_surface,
509                   struct ivi_layout_SurfaceProperties *prop,
510                   enum ivi_layout_notification_mask mask,
511                   void *userdata)
512 {
513     struct ivisurface *ivisurf = userdata;
514     struct ivishell *shell = ivisurf->shell;
515     struct ivicontroller_surface *ctrlsurf = NULL;
516     uint32_t id_surface = 0;
517
518     id_surface = ivi_layout_getIdOfSurface(layout_surface);
519
520     wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
521         if (id_surface != ctrlsurf->id_surface) {
522             continue;
523         }
524         send_surface_event(ctrlsurf->resource, ivisurf, prop, mask);
525     }
526
527     update_surface_prop(ivisurf, mask);
528 }
529
530 static void
531 send_layer_add_event(struct ivilayer *ivilayer,
532                      struct wl_resource *resource,
533                      enum ivi_layout_notification_mask mask)
534 {
535     struct ivi_layout_screen **pArray = NULL;
536     uint32_t length = 0;
537     int32_t ans = 0;
538     int i = 0;
539     struct link_screen *link_scrn = NULL;
540     struct link_screen *next = NULL;
541     struct iviscreen *iviscrn = NULL;
542     struct ivishell *shell = ivilayer->shell;
543     struct wl_client *client = wl_resource_get_client(resource);
544     struct wl_resource *resource_output = NULL;
545
546     ans = ivi_layout_getScreensUnderLayer(ivilayer->layout_layer,
547                                           &length, &pArray);
548     if (0 != ans) {
549         weston_log("failed to get screens at send_layer_add_event\n");
550         return;
551     }
552
553     /* Send Null to cancel added layer */
554     if (mask & IVI_NOTIFICATION_REMOVE) {
555         wl_list_for_each_safe(link_scrn, next, &ivilayer->list_screen, link) {
556             ivi_controller_layer_send_screen(resource, NULL);
557         }
558     }
559     else if (mask & IVI_NOTIFICATION_ADD) {
560         for (i = 0; i < (int)length; i++) {
561             /* Send new layer event */
562             iviscrn = NULL;
563             wl_list_for_each(iviscrn, &shell->list_screen, link) {
564                 if (iviscrn->layout_screen == pArray[i]) {
565                     break;
566                 }
567             }
568
569             if (iviscrn == NULL) {
570                 continue;
571             }
572
573             resource_output =
574                 wl_resource_find_for_client(&iviscrn->output->resource_list,
575                                      client);
576             if (resource_output != NULL) {
577                 ivi_controller_layer_send_screen(resource, resource_output);
578             }
579         }
580     }
581
582     free(pArray);
583     pArray = NULL;
584 }
585
586 static void
587 send_layer_event(struct wl_resource *resource,
588                  struct ivilayer *ivilayer,
589                  struct ivi_layout_LayerProperties *prop,
590                  uint32_t mask)
591 {
592     if (mask & IVI_NOTIFICATION_OPACITY) {
593         ivi_controller_layer_send_opacity(resource,
594                                           prop->opacity);
595     }
596     if (mask & IVI_NOTIFICATION_SOURCE_RECT) {
597         ivi_controller_layer_send_source_rectangle(resource,
598                                           prop->sourceX,
599                                           prop->sourceY,
600                                           prop->sourceWidth,
601                                           prop->sourceHeight);
602     }
603     if (mask & IVI_NOTIFICATION_DEST_RECT) {
604         ivi_controller_layer_send_destination_rectangle(resource,
605                                           prop->destX,
606                                           prop->destY,
607                                           prop->destWidth,
608                                           prop->destHeight);
609     }
610     if (mask & IVI_NOTIFICATION_ORIENTATION) {
611         ivi_controller_layer_send_orientation(resource,
612                                           prop->orientation);
613     }
614     if (mask & IVI_NOTIFICATION_VISIBILITY) {
615         ivi_controller_layer_send_visibility(resource,
616                                           prop->visibility);
617     }
618     if (mask & IVI_NOTIFICATION_REMOVE) {
619         send_layer_add_event(ivilayer, resource, IVI_NOTIFICATION_REMOVE);
620     }
621     if (mask & IVI_NOTIFICATION_ADD) {
622         send_layer_add_event(ivilayer, resource, IVI_NOTIFICATION_ADD);
623     }
624 }
625
626 static void
627 update_layer_prop(struct ivilayer *ivilayer,
628                   enum ivi_layout_notification_mask mask)
629 {
630     struct ivi_layout_screen **pArray = NULL;
631     uint32_t length = 0;
632     int32_t ans = 0;
633     struct link_screen *link_scrn = NULL;
634     struct link_screen *next = NULL;
635
636     ans = ivi_layout_getScreensUnderLayer(ivilayer->layout_layer,
637                                           &length, &pArray);
638     if (0 != ans) {
639         weston_log("failed to get screens at send_layer_add_event\n");
640         return;
641     }
642
643     /* Send Null to cancel added layer */
644     if (mask & IVI_NOTIFICATION_REMOVE) {
645         wl_list_for_each_safe(link_scrn, next, &ivilayer->list_screen, link) {
646             wl_list_remove(&link_scrn->link);
647             free(link_scrn);
648             link_scrn = NULL;
649         }
650     }
651     if (mask & IVI_NOTIFICATION_ADD) {
652         int i = 0;
653         for (i = 0; i < (int)length; i++) {
654             struct ivishell *shell = ivilayer->shell;
655             struct iviscreen *iviscrn = NULL;
656             /* Create list_screen */
657             link_scrn = calloc(1, sizeof(*link_scrn));
658             if (NULL == link_scrn) {
659                 continue;
660             }
661             wl_list_init(&link_scrn->link);
662             link_scrn->screen = NULL;
663             wl_list_for_each(iviscrn, &shell->list_screen, link) {
664                 if (iviscrn->layout_screen == pArray[i]) {
665                     link_scrn->screen = iviscrn;
666                     break;
667                 }
668             }
669
670             if (link_scrn->screen == NULL) {
671                 free(link_scrn);
672                 link_scrn = NULL;
673                 continue;
674             }
675             wl_list_insert(&ivilayer->list_screen, &link_scrn->link);
676         }
677     }
678
679     free(pArray);
680     pArray = NULL;
681 }
682
683 static void
684 send_layer_prop(struct ivi_layout_layer *layer,
685                 struct ivi_layout_LayerProperties *prop,
686                 enum ivi_layout_notification_mask mask,
687                 void *userdata)
688 {
689     struct ivilayer *ivilayer = userdata;
690     struct ivicontroller_layer *ctrllayer = NULL;
691     struct ivishell *shell = ivilayer->shell;
692     uint32_t id_layout_layer = 0;
693
694     id_layout_layer = ivi_layout_getIdOfLayer(layer);
695     wl_list_for_each(ctrllayer, &shell->list_controller_layer, link) {
696         if (id_layout_layer != ctrllayer->id_layer) {
697             continue;
698         }
699         send_layer_event(ctrllayer->resource, ivilayer, prop, mask);
700     }
701
702     update_layer_prop(ivilayer, mask);
703 }
704
705 static void
706 controller_surface_set_opacity(struct wl_client *client,
707                    struct wl_resource *resource,
708                    wl_fixed_t opacity)
709 {
710     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
711     (void)client;
712     ivi_layout_surfaceSetOpacity(ivisurf->layout_surface, (float)opacity);
713 }
714
715 static void
716 controller_surface_set_source_rectangle(struct wl_client *client,
717                    struct wl_resource *resource,
718                    int32_t x,
719                    int32_t y,
720                    int32_t width,
721                    int32_t height)
722 {
723     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
724     (void)client;
725     ivi_layout_surfaceSetSourceRectangle(ivisurf->layout_surface,
726             (uint32_t)x, (uint32_t)y, (uint32_t)width, (uint32_t)height);
727 }
728
729 static void
730 controller_surface_set_destination_rectangle(struct wl_client *client,
731                      struct wl_resource *resource,
732                      int32_t x,
733                      int32_t y,
734                      int32_t width,
735                      int32_t height)
736 {
737     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
738     (void)client;
739
740     // TODO: create set transition type protocol
741     ivi_layout_surfaceSetTransition( ivisurf->layout_surface,
742                                      IVI_LAYOUT_TRANSITION_NONE,
743                                      300); // ms
744
745     ivi_layout_surfaceSetDestinationRectangle(ivisurf->layout_surface,
746             (uint32_t)x, (uint32_t)y, (uint32_t)width, (uint32_t)height);
747 }
748
749 static void
750 controller_surface_set_visibility(struct wl_client *client,
751                       struct wl_resource *resource,
752                       uint32_t visibility)
753 {
754     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
755     (void)client;
756     ivi_layout_surfaceSetVisibility(ivisurf->layout_surface, visibility);
757 }
758
759 static void
760 controller_surface_set_configuration(struct wl_client *client,
761                    struct wl_resource *resource,
762                    int32_t width, int32_t height)
763 {
764     /* This interface has been supported yet. */
765     (void)client;
766     (void)resource;
767     (void)width;
768     (void)height;
769 }
770
771 static void
772 controller_surface_set_orientation(struct wl_client *client,
773                    struct wl_resource *resource,
774                    int32_t orientation)
775 {
776     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
777     (void)client;
778     ivi_layout_surfaceSetOrientation(ivisurf->layout_surface, (uint32_t)orientation);
779 }
780
781 static int
782 shm_surface_screenshot(struct weston_surface *surface,
783                        int32_t width,
784                        int32_t height,
785                        int32_t stride,
786                        const char *filename)
787 {
788     struct weston_buffer *weston_buffer = NULL;
789     struct wl_shm_buffer *shm_buffer = NULL;
790     cairo_surface_t *cairo_surf = NULL;
791     uint8_t *source_buffer = NULL;
792     uint8_t *dest_buffer = NULL;
793
794     weston_buffer = surface->buffer_ref.buffer;
795     if (weston_buffer == NULL) {
796         fprintf(stderr, "Failed to get weston buffer.\n");
797         return -1;
798     }
799
800     shm_buffer = wl_shm_buffer_get(weston_buffer->resource);
801     if (shm_buffer == NULL) {
802         return -1;
803     }
804
805     source_buffer = wl_shm_buffer_get_data(shm_buffer);
806     if (source_buffer == NULL) {
807         fprintf(stderr, "Failed to get data from shm buffer.\n");
808         return -1;
809     }
810
811     dest_buffer = malloc(stride * height);
812     if (dest_buffer == NULL) {
813         fprintf(stderr, "Failed to allocate memory.\n");
814         return -1;
815     }
816
817     memcpy(dest_buffer, source_buffer, stride * height);
818
819     cairo_surf = cairo_image_surface_create_for_data(
820             dest_buffer,
821             CAIRO_FORMAT_RGB24,
822             width,
823             height,
824             stride);
825
826     cairo_surface_write_to_png(cairo_surf, filename);
827     cairo_surface_destroy(cairo_surf);
828     free(dest_buffer);
829
830     return 0;
831 }
832
833 static void
834 bind_framebuffer(GLuint *fbo_id, GLuint *tex_id, GLsizei width, GLsizei height)
835 {
836     glGenTextures(1, tex_id);
837     glGenFramebuffers(1, fbo_id);
838     glBindTexture(GL_TEXTURE_2D, *tex_id);
839     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
840     glBindFramebuffer(GL_FRAMEBUFFER, *fbo_id);
841     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, *tex_id, 0);
842     glClearColor(0, 0, 0, 0);
843     glClear(GL_COLOR_BUFFER_BIT);
844 }
845
846 static void
847 unbind_framebuffer(GLuint fbo_id, GLuint tex_id)
848 {
849     glBindTexture(GL_TEXTURE_2D, 0);
850     glBindFramebuffer(GL_FRAMEBUFFER, 0);
851     glDeleteTextures(1, &tex_id);
852     glDeleteFramebuffers(1, &fbo_id);
853 }
854
855 static void
856 dump_surface(struct weston_output *output,
857              struct weston_compositor *compositor,
858              const char *filename,
859              int32_t x,
860              int32_t y,
861              int32_t width,
862              int32_t height)
863 {
864     struct weston_renderer *renderer = compositor->renderer;
865     pixman_region32_t region;
866     uint8_t *readpixs = NULL;
867     int32_t stride = width * (PIXMAN_FORMAT_BPP(compositor->read_format) / 8);
868     GLuint fbo_id, tex_id;
869
870     readpixs = malloc(stride * height);
871     if (readpixs == NULL) {
872         fprintf(stderr, "Failed to allocate memory.\n");
873         return;
874     }
875
876     pixman_region32_init_rect(&region, x, y, width, height);
877     bind_framebuffer(&fbo_id, &tex_id, output->current_mode->width, output->current_mode->height);
878     renderer->repaint_output(output, &region);
879     glFinish();
880
881     y = output->current_mode->height - (y + height);
882     int result = renderer->read_pixels(output,
883                                        compositor->read_format,
884                                        readpixs,
885                                        x,
886                                        y,
887                                        width,
888                                        height);
889     unbind_framebuffer(fbo_id, tex_id);
890
891     save_as_bitmap(filename, readpixs, stride * height, width, height, PIXMAN_FORMAT_BPP(compositor->read_format));
892     free(readpixs);
893 }
894
895 static void
896 clear_viewlist_but_specified_surface(struct weston_compositor *compositor,
897                                      struct weston_surface *surface)
898 {
899     struct weston_view *view = NULL;
900     wl_list_init(&compositor->view_list);
901
902     wl_list_for_each(view, &surface->views, surface_link) {
903         if (view == NULL) {
904             continue;
905         }
906
907         wl_list_insert(compositor->view_list.prev, &view->link);
908     }
909 }
910
911 static void
912 get_gl_surface_rectangle(struct ivi_layout_SurfaceProperties *prop,
913                          int32_t *x,
914                          int32_t *y,
915                          int32_t *width,
916                          int32_t *height)
917 {
918     if (prop == NULL) {
919         return;
920     }
921
922     if ((prop->destX == 0) &&
923         (prop->destY == 0) &&
924         (prop->destWidth == 0) &&
925         (prop->destHeight == 0)) {
926         *x = prop->sourceX;
927         *y = prop->sourceY;
928         *width = prop->sourceWidth;
929         *height = prop->sourceHeight;
930     }
931     else {
932         *x = prop->destX;
933         *y = prop->destY;
934         *width = prop->destWidth;
935         *height = prop->destHeight;
936     }
937 }
938
939 static int
940 gl_surface_screenshot(struct ivisurface *ivisurf,
941                       struct weston_surface *surface,
942                       const char *filename)
943 {
944     struct weston_compositor *compositor = surface->compositor;
945     struct link_layer *link_layer = NULL;
946     struct link_screen *link_scrn = NULL;
947     struct ivi_layout_SurfaceProperties prop = {};
948     int32_t x = 0;
949     int32_t y = 0;
950     int32_t width = 0;
951     int32_t height = 0;
952
953     wl_list_for_each(link_layer, &ivisurf->list_layer, link) {
954         if (link_layer == NULL) {
955             continue;
956         }
957
958         wl_list_for_each(link_scrn, &link_layer->layer->list_screen, link) {
959             if (link_scrn != NULL) {
960                 break;
961             }
962         }
963     }
964
965     if (link_scrn == NULL) {
966         fprintf(stderr, "Failed to get iviscreen\n");
967         return -1;
968     }
969
970     if (ivi_layout_getPropertiesOfSurface(ivisurf->layout_surface, &prop) != 0) {
971         fprintf(stderr, "Failed to get surface properties.");
972         return -1;
973     }
974
975     get_gl_surface_rectangle(&prop, &x, &y, &width, &height);
976     clear_viewlist_but_specified_surface(compositor, surface);
977     dump_surface(link_scrn->screen->output,
978                  compositor,
979                  filename,
980                  x,
981                  y,
982                  width,
983                  height);
984
985     return 0;
986 }
987
988 static void
989 controller_surface_screenshot(struct wl_client *client,
990                   struct wl_resource *resource,
991                   const char *filename)
992 {
993     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
994     struct weston_surface *weston_surface = NULL;
995     int32_t width = 0;
996     int32_t height = 0;
997     int32_t stride = 0;
998
999     weston_surface = ivi_layout_surfaceGetWestonSurface(ivisurf->layout_surface);
1000     if (weston_surface == NULL) {
1001         fprintf(stderr, "Failed to get weston surface.\n");
1002         return;
1003     }
1004
1005     if (ivi_layout_surfaceGetSize(ivisurf->layout_surface, &width, &height, &stride) != 0) {
1006         fprintf(stderr, "Failed to get surface size.\n");
1007         return;
1008     }
1009
1010     if (shm_surface_screenshot(weston_surface, width, height, stride, filename) != 0) {
1011         if (gl_surface_screenshot(ivisurf, weston_surface, filename) != 0) {
1012             fprintf(stderr, "Failed to capture surface.\n");
1013         }
1014     }
1015 }
1016
1017
1018 static void
1019 controller_surface_send_stats(struct wl_client *client,
1020                               struct wl_resource *resource)
1021 {
1022     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
1023     pid_t pid;
1024     uid_t uid;
1025     gid_t gid;
1026     wl_client_get_credentials(client, &pid, &uid, &gid);
1027
1028     ivi_controller_surface_send_stats(resource, 0, 0,
1029                                       ivisurf->update_count, pid, "");
1030 }
1031
1032 static void
1033 controller_surface_destroy(struct wl_client *client,
1034               struct wl_resource *resource,
1035               int32_t destroy_scene_object)
1036 {
1037     (void)client;
1038     (void)destroy_scene_object;
1039     wl_resource_destroy(resource);
1040 }
1041
1042 static void
1043 controller_surface_set_input_focus(struct wl_client *client,
1044               struct wl_resource *resource,
1045               uint32_t device,
1046               int32_t enabled)
1047 {
1048     (void)client;
1049     (void)resource;
1050     (void)device;
1051     (void)enabled;
1052 }
1053
1054 static const
1055 struct ivi_controller_surface_interface controller_surface_implementation = {
1056     controller_surface_set_visibility,
1057     controller_surface_set_opacity,
1058     controller_surface_set_source_rectangle,
1059     controller_surface_set_destination_rectangle,
1060     controller_surface_set_configuration,
1061     controller_surface_set_orientation,
1062     controller_surface_screenshot,
1063     controller_surface_send_stats,
1064     controller_surface_destroy,
1065     controller_surface_set_input_focus
1066 };
1067
1068 static void
1069 controller_layer_set_source_rectangle(struct wl_client *client,
1070                    struct wl_resource *resource,
1071                    int32_t x,
1072                    int32_t y,
1073                    int32_t width,
1074                    int32_t height)
1075 {
1076     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1077     (void)client;
1078     ivi_layout_layerSetSourceRectangle(ivilayer->layout_layer,
1079            (uint32_t)x, (uint32_t)y, (uint32_t)width, (uint32_t)height);
1080 }
1081
1082 static void
1083 controller_layer_set_destination_rectangle(struct wl_client *client,
1084                  struct wl_resource *resource,
1085                  int32_t x,
1086                  int32_t y,
1087                  int32_t width,
1088                  int32_t height)
1089 {
1090     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1091     (void)client;
1092     ivi_layout_layerSetDestinationRectangle(ivilayer->layout_layer,
1093             (uint32_t)x, (uint32_t)y, (uint32_t)width, (uint32_t)height);
1094 }
1095
1096 static void
1097 controller_layer_set_visibility(struct wl_client *client,
1098                     struct wl_resource *resource,
1099                     uint32_t visibility)
1100 {
1101     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1102     (void)client;
1103     ivi_layout_layerSetVisibility(ivilayer->layout_layer, visibility);
1104 }
1105
1106 static void
1107 controller_layer_set_opacity(struct wl_client *client,
1108                  struct wl_resource *resource,
1109                  wl_fixed_t opacity)
1110 {
1111     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1112     (void)client;
1113     ivi_layout_layerSetOpacity(ivilayer->layout_layer, (float)opacity);
1114 }
1115
1116 static void
1117 controller_layer_set_configuration(struct wl_client *client,
1118                  struct wl_resource *resource,
1119                  int32_t width,
1120                  int32_t height)
1121 {
1122     /* This interface has been supported yet. */
1123     (void)client;
1124     (void)resource;
1125     (void)width;
1126     (void)height;
1127 }
1128
1129 static void
1130 controller_layer_set_orientation(struct wl_client *client,
1131                  struct wl_resource *resource,
1132                  int32_t orientation)
1133 {
1134     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1135     (void)client;
1136     ivi_layout_layerSetOrientation(ivilayer->layout_layer, (uint32_t)orientation);
1137 }
1138
1139 static void
1140 controller_layer_clear_surfaces(struct wl_client *client,
1141                     struct wl_resource *resource)
1142 {
1143     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1144     (void)client;
1145     ivi_layout_layerSetRenderOrder(ivilayer->layout_layer, NULL, 0);
1146 }
1147
1148 static void
1149 controller_layer_add_surface(struct wl_client *client,
1150                  struct wl_resource *resource,
1151                  struct wl_resource *surface)
1152 {
1153     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1154     struct ivisurface *ivisurf = wl_resource_get_user_data(surface);
1155     (void)client;
1156     ivi_layout_layerAddSurface(ivilayer->layout_layer, ivisurf->layout_surface);
1157 }
1158
1159 static void
1160 controller_layer_remove_surface(struct wl_client *client,
1161                     struct wl_resource *resource,
1162                     struct wl_resource *surface)
1163 {
1164     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1165     struct ivisurface *ivisurf = wl_resource_get_user_data(surface);
1166     (void)client;
1167     ivi_layout_layerRemoveSurface(ivilayer->layout_layer, ivisurf->layout_surface);
1168 }
1169
1170 static void
1171 controller_layer_screenshot(struct wl_client *client,
1172                 struct wl_resource *resource,
1173                 const char *filename)
1174 {
1175     (void)client;
1176     (void)resource;
1177     (void)filename;
1178 }
1179
1180 static void
1181 controller_layer_set_render_order(struct wl_client *client,
1182                                   struct wl_resource *resource,
1183                                   struct wl_array *id_surfaces)
1184 {
1185     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1186     struct ivi_layout_surface **layoutsurf_array = NULL;
1187     struct ivisurface *ivisurf = NULL;
1188     uint32_t *id_surface = NULL;
1189     uint32_t id_layout_surface = 0;
1190     int i = 0;
1191     (void)client;
1192
1193     layoutsurf_array = (struct ivi_layout_surface**)calloc(
1194                            id_surfaces->size, sizeof(void*));
1195
1196     wl_array_for_each(id_surface, id_surfaces) {
1197         wl_list_for_each(ivisurf, &ivilayer->shell->list_surface, link) {
1198             id_layout_surface = ivi_layout_getIdOfSurface(ivisurf->layout_surface);
1199             if (*id_surface == id_layout_surface) {
1200                 layoutsurf_array[i] = ivisurf->layout_surface;
1201                 i++;
1202                 break;
1203             }
1204         }
1205     }
1206
1207     ivi_layout_layerSetRenderOrder(ivilayer->layout_layer,
1208                                    layoutsurf_array, i);
1209     free(layoutsurf_array);
1210 }
1211
1212 static void
1213 controller_layer_destroy(struct wl_client *client,
1214               struct wl_resource *resource,
1215               int32_t destroy_scene_object)
1216 {
1217     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1218     struct ivishell *shell = ivilayer->shell;
1219     struct ivicontroller_layer *ctrllayer = NULL;
1220     struct ivicontroller_layer *next = NULL;
1221     uint32_t id_layer = ivi_layout_getIdOfLayer(ivilayer->layout_layer);
1222     (void)client;
1223     (void)destroy_scene_object;
1224
1225     ivilayer->layer_canbe_removed = 1;
1226     wl_list_for_each_safe(ctrllayer, next, &shell->list_controller_layer, link) {
1227         if (ctrllayer->id_layer != id_layer) {
1228             continue;
1229         }
1230
1231         wl_resource_destroy(ctrllayer->resource);
1232     }
1233 }
1234
1235 static const
1236 struct ivi_controller_layer_interface controller_layer_implementation = {
1237     controller_layer_set_visibility,
1238     controller_layer_set_opacity,
1239     controller_layer_set_source_rectangle,
1240     controller_layer_set_destination_rectangle,
1241     controller_layer_set_configuration,
1242     controller_layer_set_orientation,
1243     controller_layer_screenshot,
1244     controller_layer_clear_surfaces,
1245     controller_layer_add_surface,
1246     controller_layer_remove_surface,
1247     controller_layer_set_render_order,
1248     controller_layer_destroy
1249 };
1250
1251 static void
1252 controller_screen_destroy(struct wl_client *client,
1253                           struct wl_resource *resource)
1254 {
1255     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1256     struct ivicontroller_screen *ctrlscrn = NULL;
1257     struct ivicontroller_screen *next = NULL;
1258 //    uint32_t id_screen = ivi_layout_getIdOfScreen(iviscrn->layout_screen);
1259     (void)client;
1260
1261     wl_list_for_each_safe(ctrlscrn, next,
1262                           &iviscrn->shell->list_controller_screen, link) {
1263         if (resource != ctrlscrn->resource) {
1264             continue;
1265         }
1266
1267         wl_list_remove(&ctrlscrn->link);
1268         wl_resource_destroy(ctrlscrn->resource);
1269         free(ctrlscrn);
1270         ctrlscrn = NULL;
1271         break;
1272     }
1273 }
1274
1275 static void
1276 controller_screen_clear(struct wl_client *client,
1277                 struct wl_resource *resource)
1278 {
1279     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1280     (void)client;
1281     ivi_layout_screenSetRenderOrder(iviscrn->layout_screen, NULL, 0);
1282 }
1283
1284 static void
1285 controller_screen_add_layer(struct wl_client *client,
1286                 struct wl_resource *resource,
1287                 struct wl_resource *layer)
1288 {
1289     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1290     struct ivilayer *ivilayer = wl_resource_get_user_data(layer);
1291     (void)client;
1292     ivi_layout_screenAddLayer(iviscrn->layout_screen, ivilayer->layout_layer);
1293 }
1294
1295 static void
1296 controller_screen_screenshot(struct wl_client *client,
1297                 struct wl_resource *resource,
1298                 const char *filename)
1299 {
1300     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1301     (void)client;
1302
1303     struct weston_output *output = NULL;
1304     cairo_surface_t *cairo_surf = NULL;
1305     int32_t i = 0;
1306     int32_t width = 0;
1307     int32_t height = 0;
1308     int32_t stride = 0;
1309     uint8_t *readpixs = NULL;
1310
1311     output = ivi_layout_screenGetOutput(iviscrn->layout_screen);
1312     --output->disable_planes;
1313
1314     width = output->current_mode->width;
1315     height = output->current_mode->height;
1316     stride = width * (PIXMAN_FORMAT_BPP(output->compositor->read_format) / 8);
1317
1318     readpixs = malloc(stride * height);
1319     if (readpixs == NULL) {
1320         weston_log("fails to allocate memory\n");
1321         return;
1322     }
1323
1324     output->compositor->renderer->read_pixels(
1325             output,
1326             output->compositor->read_format,
1327             readpixs,
1328             0,
1329             0,
1330             width,
1331             height);
1332
1333     save_as_bitmap(filename, readpixs, stride * height, width, height, PIXMAN_FORMAT_BPP(output->compositor->read_format));
1334     free(readpixs);
1335 }
1336
1337 static void
1338 controller_screen_set_render_order(struct wl_client *client,
1339                 struct wl_resource *resource,
1340                 struct wl_array *id_layers)
1341 {
1342     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1343     struct ivi_layout_layer **layoutlayer_array = NULL;
1344     struct ivilayer *ivilayer = NULL;
1345     uint32_t *id_layer = NULL;
1346     uint32_t id_layout_layer = 0;
1347     int i = 0;
1348     (void)client;
1349
1350     layoutlayer_array = (struct ivi_layout_layer**)calloc(
1351                            id_layers->size, sizeof(void*));
1352
1353     wl_array_for_each(id_layer, id_layers) {
1354         wl_list_for_each(ivilayer, &iviscrn->shell->list_layer, link) {
1355             id_layout_layer = ivi_layout_getIdOfLayer(ivilayer->layout_layer);
1356             if (*id_layer == id_layout_layer) {
1357                 layoutlayer_array[i] = ivilayer->layout_layer;
1358                 i++;
1359                 break;
1360             }
1361         }
1362     }
1363
1364     ivi_layout_screenSetRenderOrder(iviscrn->layout_screen,
1365                                     layoutlayer_array, i);
1366     free(layoutlayer_array);
1367 }
1368
1369 static const
1370 struct ivi_controller_screen_interface controller_screen_implementation = {
1371     controller_screen_destroy,
1372     controller_screen_clear,
1373     controller_screen_add_layer,
1374     controller_screen_screenshot,
1375     controller_screen_set_render_order
1376 };
1377
1378 static void
1379 controller_commit_changes(struct wl_client *client,
1380                           struct wl_resource *resource)
1381 {
1382     int32_t ans = 0;
1383     (void)client;
1384     (void)resource;
1385
1386     ans = ivi_layout_commitChanges();
1387     if (ans < 0) {
1388         weston_log("Failed to commit changes at controller_commit_changes\n");
1389     }
1390 }
1391
1392 static void
1393 controller_layer_create(struct wl_client *client,
1394                         struct wl_resource *resource,
1395                         uint32_t id_layer,
1396                         int32_t width,
1397                         int32_t height,
1398                         uint32_t id)
1399 {
1400     struct ivicontroller *ctrl = wl_resource_get_user_data(resource);
1401     struct ivishell *shell = ctrl->shell;
1402     struct ivi_layout_layer *layout_layer = NULL;
1403     struct ivicontroller_layer *ctrllayer = NULL;
1404     struct ivilayer *ivilayer = NULL;
1405     struct ivi_layout_LayerProperties prop;
1406
1407     ivilayer = get_layer(&shell->list_layer, id_layer);
1408     if (ivilayer == NULL) {
1409         layout_layer = ivi_layout_layerCreateWithDimension(id_layer,
1410                            (uint32_t)width, (uint32_t)height);
1411         if (layout_layer == NULL) {
1412             weston_log("id_layer is already created\n");
1413             return;
1414         }
1415
1416         /* ivilayer will be created by layer_event_create */
1417         ivilayer = get_layer(&shell->list_layer, id_layer);
1418         if (ivilayer == NULL) {
1419             weston_log("couldn't get layer object\n");
1420             return;
1421         }
1422     }
1423
1424     ctrllayer = calloc(1, sizeof *ctrllayer);
1425     if (!ctrllayer) {
1426         weston_log("no memory to allocate client layer\n");
1427         return;
1428     }
1429
1430     ++ivilayer->controller_layer_count;
1431     ivilayer->layer_canbe_removed = 0;
1432
1433     ctrllayer->shell = shell;
1434     ctrllayer->client = client;
1435     ctrllayer->id = id;
1436     ctrllayer->id_layer = id_layer;
1437     ctrllayer->resource = wl_resource_create(client,
1438                                &ivi_controller_layer_interface, 1, id);
1439     if (ctrllayer->resource == NULL) {
1440         weston_log("couldn't get layer object\n");
1441         return;
1442     }
1443
1444     wl_list_init(&ctrllayer->link);
1445     wl_list_insert(&shell->list_controller_layer, &ctrllayer->link);
1446
1447     wl_resource_set_implementation(ctrllayer->resource,
1448                                    &controller_layer_implementation,
1449                                    ivilayer, destroy_ivicontroller_layer);
1450
1451     memset(&prop, 0, sizeof prop);
1452
1453     ivi_layout_getPropertiesOfLayer(ivilayer->layout_layer, &prop);
1454     send_layer_event(ctrllayer->resource, ivilayer,
1455                      &prop, IVI_NOTIFICATION_ALL);
1456 }
1457
1458 static void
1459 surface_event_content(struct ivi_layout_surface *layout_surface, int32_t content, void *userdata)
1460 {
1461     struct ivishell *shell = userdata;
1462     struct ivicontroller_surface *ctrlsurf = NULL;
1463     uint32_t id_surface = 0;
1464
1465     if (content == 0) {
1466         surface_event_remove(layout_surface, userdata);
1467     }
1468 }
1469
1470 static void
1471 controller_surface_create(struct wl_client *client,
1472                           struct wl_resource *resource,
1473                           uint32_t id_surface,
1474                           uint32_t id)
1475 {
1476     struct ivicontroller *ctrl = wl_resource_get_user_data(resource);
1477     struct ivishell *shell = ctrl->shell;
1478     struct ivicontroller_surface *ctrlsurf = NULL;
1479     struct ivi_layout_SurfaceProperties prop;
1480     struct ivisurface *ivisurf = NULL;
1481     struct ivicontroller_surface *ctrl_link = NULL;
1482
1483     ivisurf = get_surface(&shell->list_surface, id_surface);
1484     if (ivisurf == NULL) {
1485         return;
1486     }
1487
1488     ctrlsurf = calloc(1, sizeof *ctrlsurf);
1489     if (!ctrlsurf) {
1490         weston_log("no memory to allocate controller surface\n");
1491         return;
1492     }
1493
1494     ctrlsurf->shell = shell;
1495     ctrlsurf->client = client;
1496     ctrlsurf->id = id;
1497     ctrlsurf->id_surface = id_surface;
1498     wl_list_init(&ctrlsurf->link);
1499     wl_list_insert(&shell->list_controller_surface, &ctrlsurf->link);
1500
1501     ctrlsurf->resource = wl_resource_create(client,
1502                                &ivi_controller_surface_interface, 1, id);
1503     if (ctrlsurf->resource == NULL) {
1504         weston_log("couldn't surface object");
1505         return;
1506     }
1507
1508     wl_list_for_each(ctrl_link, &shell->list_controller_surface, link) {
1509         if ((ctrl_link->implementation_set == 0) &&
1510             (ctrl_link->id_surface == id_surface) &&
1511             (ctrl_link->shell == shell) &&
1512             (ctrl_link->client != client)) {
1513             ++ivisurf->controller_surface_count;
1514             wl_resource_set_implementation(ctrl_link->resource,
1515                                            &controller_surface_implementation,
1516                                            ivisurf, destroy_ivicontroller_surface);
1517             ctrl_link->implementation_set = 1;
1518         }
1519     }
1520
1521     ++ivisurf->controller_surface_count;
1522
1523     wl_resource_set_implementation(ctrlsurf->resource,
1524                                    &controller_surface_implementation,
1525                                    ivisurf, destroy_ivicontroller_surface);
1526
1527     ctrlsurf->implementation_set = 1;
1528
1529     memset(&prop, 0, sizeof prop);
1530
1531     ivi_layout_getPropertiesOfSurface(ivisurf->layout_surface, &prop);
1532     ivi_layout_surfaceSetContentObserver(ivisurf->layout_surface, surface_event_content, shell);
1533
1534     send_surface_event(ctrlsurf->resource, ivisurf,
1535                        &prop, IVI_NOTIFICATION_ALL);
1536 }
1537
1538 static const struct ivi_controller_interface controller_implementation = {
1539     controller_commit_changes,
1540     controller_layer_create,
1541     controller_surface_create
1542 };
1543
1544 static void
1545 add_client_to_resources(struct ivishell *shell,
1546                         struct wl_client *client,
1547                         struct ivicontroller *controller)
1548 {
1549     struct ivisurface* ivisurf = NULL;
1550     struct ivilayer* ivilayer = NULL;
1551     struct iviscreen* iviscrn = NULL;
1552     struct ivicontroller_screen *ctrlscrn = NULL;
1553     struct wl_resource *resource_output = NULL;
1554     uint32_t id_layout_surface = 0;
1555     uint32_t id_layout_layer = 0;
1556
1557     wl_list_for_each(iviscrn, &shell->list_screen, link) {
1558         resource_output = wl_resource_find_for_client(
1559                 &iviscrn->output->resource_list, client);
1560         if (resource_output == NULL) {
1561             continue;
1562         }
1563
1564         ctrlscrn = controller_screen_create(iviscrn->shell, client, iviscrn);
1565         if (ctrlscrn == NULL) {
1566             continue;
1567         }
1568
1569         ivi_controller_send_screen(controller->resource,
1570                                    wl_resource_get_id(resource_output),
1571                                    ctrlscrn->resource);
1572     }
1573     wl_list_for_each_reverse(ivilayer, &shell->list_layer, link) {
1574         id_layout_layer =
1575             ivi_layout_getIdOfLayer(ivilayer->layout_layer);
1576
1577         ivi_controller_send_layer(controller->resource,
1578                                   id_layout_layer);
1579     }
1580     wl_list_for_each_reverse(ivisurf, &shell->list_surface, link) {
1581         id_layout_surface =
1582             ivi_layout_getIdOfSurface(ivisurf->layout_surface);
1583
1584         ivi_controller_send_surface(controller->resource,
1585                                     id_layout_surface);
1586     }
1587 }
1588
1589 static void
1590 bind_ivi_controller(struct wl_client *client, void *data,
1591                     uint32_t version, uint32_t id)
1592 {
1593     struct ivishell *shell = data;
1594     struct ivicontroller *controller;
1595     (void)version;
1596
1597     controller = calloc(1, sizeof *controller);
1598     if (controller == NULL) {
1599         weston_log("no memory to allocate controller\n");
1600         return;
1601     }
1602
1603     controller->resource =
1604         wl_resource_create(client, &ivi_controller_interface, 1, id);
1605     wl_resource_set_implementation(controller->resource,
1606                                    &controller_implementation,
1607                                    controller, unbind_resource_controller);
1608
1609     controller->shell = shell;
1610     controller->client = client;
1611     controller->id = id;
1612
1613     wl_list_init(&controller->link);
1614     wl_list_insert(&shell->list_controller, &controller->link);
1615
1616     add_client_to_resources(shell, client, controller);
1617 }
1618
1619 static struct iviscreen*
1620 create_screen(struct ivishell *shell, struct weston_output *output)
1621 {
1622     struct iviscreen *iviscrn;
1623     iviscrn = calloc(1, sizeof *iviscrn);
1624     if (iviscrn == NULL) {
1625         weston_log("no memory to allocate client screen\n");
1626         return NULL;
1627     }
1628
1629     iviscrn->shell = shell;
1630     iviscrn->output = output;
1631
1632 // TODO : Only Single display
1633     iviscrn->layout_screen = ivi_layout_getScreenFromId(0);
1634
1635     wl_list_init(&iviscrn->link);
1636
1637     return iviscrn;
1638 }
1639
1640 static struct ivilayer*
1641 create_layer(struct ivishell *shell,
1642              struct ivi_layout_layer *layout_layer,
1643              uint32_t id_layer)
1644 {
1645     struct ivilayer *ivilayer = NULL;
1646     struct ivicontroller *controller = NULL;
1647
1648     ivilayer = get_layer(&shell->list_layer, id_layer);
1649     if (ivilayer != NULL) {
1650         weston_log("id_layer is already created\n");
1651         return NULL;
1652     }
1653
1654     ivilayer = calloc(1, sizeof *ivilayer);
1655     if (NULL == ivilayer) {
1656         weston_log("no memory to allocate client layer\n");
1657         return NULL;
1658     }
1659
1660     ivilayer->shell = shell;
1661     wl_list_init(&ivilayer->list_screen);
1662     wl_list_init(&ivilayer->link);
1663     wl_list_insert(&shell->list_layer, &ivilayer->link);
1664     ivilayer->layout_layer = layout_layer;
1665
1666     ivi_layout_layerAddNotification(layout_layer, send_layer_prop, ivilayer);
1667
1668     wl_list_for_each(controller, &shell->list_controller, link) {
1669         ivi_controller_send_layer(controller->resource, id_layer);
1670     }
1671
1672     return ivilayer;
1673 }
1674
1675 static struct ivisurface*
1676 create_surface(struct ivishell *shell,
1677                struct ivi_layout_surface *layout_surface,
1678                uint32_t id_surface)
1679 {
1680     struct ivisurface *ivisurf = NULL;
1681     struct ivicontroller *controller = NULL;
1682
1683     ivisurf = get_surface(&shell->list_surface, id_surface);
1684     if (ivisurf != NULL) {
1685         weston_log("id_surface is already created\n");
1686         return NULL;
1687     }
1688
1689     ivisurf = calloc(1, sizeof *ivisurf);
1690     if (ivisurf == NULL) {
1691         weston_log("no memory to allocate client surface\n");
1692         return NULL;
1693     }
1694
1695     ivisurf->shell = shell;
1696     ivisurf->layout_surface = layout_surface;
1697     wl_list_init(&ivisurf->list_layer);
1698     wl_list_init(&ivisurf->link);
1699     wl_list_insert(&shell->list_surface, &ivisurf->link);
1700
1701     wl_list_for_each(controller, &shell->list_controller, link) {
1702         ivi_controller_send_surface(controller->resource,
1703                                     id_surface);
1704     }
1705
1706     ivi_layout_surfaceAddNotification(layout_surface,
1707                                     send_surface_prop, ivisurf);
1708
1709     return ivisurf;
1710 }
1711
1712 static void
1713 layer_event_create(struct ivi_layout_layer *layout_layer,
1714                      void *userdata)
1715 {
1716     struct ivishell *shell = userdata;
1717     struct ivilayer *ivilayer = NULL;
1718     uint32_t id_layer = 0;
1719
1720     id_layer = ivi_layout_getIdOfLayer(layout_layer);
1721
1722     ivilayer = create_layer(shell, layout_layer, id_layer);
1723     if (ivilayer == NULL) {
1724         weston_log("failed to create layer");
1725         return;
1726     }
1727 }
1728
1729 static void
1730 layer_event_remove(struct ivi_layout_layer *layout_layer,
1731                      void *userdata)
1732 {
1733     struct ivishell *shell = userdata;
1734     struct ivicontroller_layer *ctrllayer = NULL;
1735     struct ivilayer *ivilayer = NULL;
1736     struct ivilayer *next = NULL;
1737     uint32_t id_layer = 0;
1738     int is_removed = 0;
1739
1740     wl_list_for_each_safe(ivilayer, next, &shell->list_layer, link) {
1741         if (layout_layer != ivilayer->layout_layer) {
1742             continue;
1743         }
1744
1745         wl_list_remove(&ivilayer->link);
1746
1747         is_removed = 1;
1748         free(ivilayer);
1749         ivilayer = NULL;
1750         break;
1751     }
1752
1753     if (is_removed) {
1754         id_layer = ivi_layout_getIdOfLayer(layout_layer);
1755
1756         wl_list_for_each(ctrllayer, &shell->list_controller_layer, link) {
1757             if (id_layer != ctrllayer->id_layer) {
1758                 continue;
1759             }
1760             ivi_controller_layer_send_destroyed(ctrllayer->resource);
1761         }
1762     }
1763 }
1764
1765
1766 static void
1767 surface_event_create(struct ivi_layout_surface *layout_surface,
1768                      void *userdata)
1769 {
1770     struct ivishell *shell = userdata;
1771     struct ivisurface *ivisurf = NULL;
1772     uint32_t id_surface = 0;
1773     struct ivicontroller_surface *ctrlsurf = NULL;
1774
1775     id_surface = ivi_layout_getIdOfSurface(layout_surface);
1776
1777     ivisurf = create_surface(shell, layout_surface, id_surface);
1778     if (ivisurf == NULL) {
1779         weston_log("failed to create surface");
1780         return;
1781     }
1782
1783     wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
1784         if (id_surface != ctrlsurf->id_surface) {
1785             continue;
1786         }
1787         ivi_controller_surface_send_content(ctrlsurf->resource, IVI_CONTROLLER_SURFACE_CONTENT_STATE_CONTENT_AVAILABLE);
1788     }
1789 }
1790
1791 static void
1792 surface_event_remove(struct ivi_layout_surface *layout_surface,
1793                      void *userdata)
1794 {
1795     struct ivishell *shell = userdata;
1796     struct ivicontroller_surface *ctrlsurf = NULL;
1797     struct ivisurface *ivisurf = NULL;
1798     struct ivisurface *next = NULL;
1799     uint32_t id_surface = 0;
1800     int is_removed = 0;
1801
1802     wl_list_for_each_safe(ivisurf, next, &shell->list_surface, link) {
1803         if (layout_surface != ivisurf->layout_surface) {
1804             continue;
1805         }
1806
1807         wl_list_remove(&ivisurf->link);
1808         is_removed = 1;
1809
1810         if (ivisurf->controller_surface_count == 0) {
1811             free(ivisurf);
1812         }
1813         else {
1814             ivisurf->can_be_removed = 1;
1815         }
1816
1817         break;
1818     }
1819
1820     if (is_removed) {
1821         id_surface = ivi_layout_getIdOfSurface(layout_surface);
1822
1823         wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
1824             if (id_surface != ctrlsurf->id_surface) {
1825                 continue;
1826             }
1827             ivi_controller_surface_send_content(ctrlsurf->resource, IVI_CONTROLLER_SURFACE_CONTENT_STATE_CONTENT_REMOVED);
1828             ivi_controller_surface_send_destroyed(ctrlsurf->resource);
1829         }
1830     }
1831 }
1832
1833 static void
1834 surface_event_configure(struct ivi_layout_surface *layout_surface,
1835                         void *userdata)
1836 {
1837     struct ivishell *shell = userdata;
1838     struct ivisurface *ivisurf = NULL;
1839     struct ivicontroller_surface *ctrlsurf = NULL;
1840     struct ivi_layout_SurfaceProperties prop;
1841     uint32_t id_surface = 0;
1842
1843     id_surface = ivi_layout_getIdOfSurface(layout_surface);
1844
1845     ivisurf = get_surface(&shell->list_surface, id_surface);
1846     if (ivisurf == NULL) {
1847         weston_log("id_surface is not created yet\n");
1848         return;
1849     }
1850
1851     memset(&prop, 0, sizeof prop);
1852     ivi_layout_getPropertiesOfSurface(layout_surface, &prop);
1853
1854     wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
1855         if (id_surface != ctrlsurf->id_surface) {
1856             continue;
1857         }
1858         send_surface_event(ctrlsurf->resource, ivisurf,
1859                            &prop, IVI_NOTIFICATION_ALL);
1860     }
1861 }
1862
1863 static int32_t
1864 check_layout_layers(struct ivishell *shell)
1865 {
1866     struct ivi_layout_layer **pArray = NULL;
1867     struct ivilayer *ivilayer = NULL;
1868     uint32_t id_layer = 0;
1869     uint32_t length = 0;
1870     uint32_t i = 0;
1871     int32_t ret = 0;
1872
1873     ret = ivi_layout_getLayers(&length, &pArray);
1874     if(ret != 0) {
1875         weston_log("failed to get layers at check_layout_layers\n");
1876         return -1;
1877     }
1878
1879     if (length == 0) {
1880         /* if length is 0, pArray doesn't need to free.*/
1881         return 0;
1882     }
1883
1884     for (i = 0; i < length; i++) {
1885         id_layer = ivi_layout_getIdOfLayer(pArray[i]);
1886         ivilayer = create_layer(shell, pArray[i], id_layer);
1887         if (ivilayer == NULL) {
1888             weston_log("failed to create layer");
1889         }
1890     }
1891
1892     free(pArray);
1893     pArray = NULL;
1894
1895     return 0;
1896 }
1897
1898 static int32_t
1899 check_layout_surfaces(struct ivishell *shell)
1900 {
1901     struct ivi_layout_surface **pArray = NULL;
1902     struct ivisurface *ivisurf = NULL;
1903     uint32_t id_surface = 0;
1904     uint32_t length = 0;
1905     uint32_t i = 0;
1906     int32_t ret = 0;
1907
1908     ret = ivi_layout_getSurfaces(&length, &pArray);
1909     if(ret != 0) {
1910         weston_log("failed to get surfaces at check_layout_surfaces\n");
1911         return -1;
1912     }
1913
1914     if (length == 0) {
1915         /* if length is 0, pArray doesn't need to free.*/
1916         return 0;
1917     }
1918
1919     for (i = 0; i < length; i++) {
1920         id_surface = ivi_layout_getIdOfSurface(pArray[i]);
1921         ivisurf = create_surface(shell, pArray[i], id_surface);
1922         if (ivisurf == NULL) {
1923             weston_log("failed to create surface");
1924         }
1925     }
1926
1927     free(pArray);
1928     pArray = NULL;
1929
1930     return 0;
1931 }
1932
1933 static void
1934 init_ivi_shell(struct weston_compositor *ec, struct ivishell *shell)
1935 {
1936     struct weston_output *output = NULL;
1937     struct iviscreen *iviscrn = NULL;
1938     int32_t ret = 0;
1939
1940     shell->compositor = ec;
1941
1942     wl_list_init(&shell->list_surface);
1943     wl_list_init(&shell->list_layer);
1944     wl_list_init(&shell->list_screen);
1945     wl_list_init(&shell->list_weston_surface);
1946     wl_list_init(&shell->list_controller);
1947     wl_list_init(&shell->list_controller_screen);
1948     wl_list_init(&shell->list_controller_layer);
1949     wl_list_init(&shell->list_controller_surface);
1950     shell->event_restriction = 0;
1951
1952     wl_list_for_each(output, &ec->output_list, link) {
1953         iviscrn = create_screen(shell, output);
1954         if (iviscrn != NULL) {
1955             wl_list_insert(&shell->list_screen, &iviscrn->link);
1956         }
1957     }
1958
1959     ret = check_layout_layers(shell);
1960     if (ret != 0) {
1961         weston_log("failed to check_layout_layers");
1962     }
1963
1964     ret = check_layout_surfaces(shell);
1965     if (ret != 0) {
1966         weston_log("failed to check_layout_surfaces");
1967     }
1968
1969     ivi_layout_addNotificationCreateLayer(layer_event_create, shell);
1970     ivi_layout_addNotificationRemoveLayer(layer_event_remove, shell);
1971
1972     ivi_layout_addNotificationCreateSurface(surface_event_create, shell);
1973     ivi_layout_addNotificationRemoveSurface(surface_event_remove, shell);
1974     ivi_layout_addNotificationConfigureSurface(surface_event_configure, shell);
1975 }
1976
1977 WL_EXPORT int
1978 module_init(struct weston_compositor *ec,
1979             int *argc, char *argv[])
1980 {
1981     struct ivishell *shell;
1982     (void)argc;
1983     (void)argv;
1984
1985     shell = malloc(sizeof *shell);
1986     if (shell == NULL)
1987         return -1;
1988
1989     memset(shell, 0, sizeof *shell);
1990     init_ivi_shell(ec, shell);
1991
1992     if (wl_global_create(ec->wl_display, &ivi_controller_interface, 1,
1993                          shell, bind_ivi_controller) == NULL) {
1994         return -1;
1995     }
1996
1997     return 0;
1998 }