weston-ivi-shell and ilmControl: Implement 'set keyboard focus'
[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              struct weston_view *view,
860              int32_t x,
861              int32_t y,
862              int32_t width,
863              int32_t height)
864 {
865     struct weston_renderer *renderer = compositor->renderer;
866     pixman_region32_t region;
867     struct wl_list backup_transformation_list;
868     struct weston_transform *link = NULL;
869     struct weston_transform *next = NULL;
870     uint8_t *readpixs = NULL;
871     int32_t stride = width * (PIXMAN_FORMAT_BPP(compositor->read_format) / 8);
872     GLuint fbo_id, tex_id;
873
874     readpixs = malloc(stride * height);
875     if (readpixs == NULL) {
876         fprintf(stderr, "Failed to allocate memory.\n");
877         return;
878     }
879
880     pixman_region32_init_rect(&region, x, y, width, height);
881     bind_framebuffer(&fbo_id, &tex_id, output->current_mode->width, output->current_mode->height);
882
883     wl_list_init(&backup_transformation_list);
884     wl_list_for_each_safe(link, next, &view->geometry.transformation_list, link) {
885         wl_list_remove(&link->link);
886         wl_list_insert(&backup_transformation_list, &link->link);
887     }
888
889     wl_list_init(&view->geometry.transformation_list);
890     weston_view_geometry_dirty(view);
891     weston_view_update_transform(view);
892
893     renderer->repaint_output(output, &region);
894     glFinish();
895
896     y = output->current_mode->height - (y + height);
897     int result = renderer->read_pixels(output,
898                                        compositor->read_format,
899                                        readpixs,
900                                        x,
901                                        y,
902                                        width,
903                                        height);
904
905     wl_list_for_each_safe(link, next, &backup_transformation_list, link) {
906         wl_list_remove(&link->link);
907         wl_list_insert(&view->geometry.transformation_list, &link->link);
908     }
909
910     weston_view_geometry_dirty(view);
911     weston_view_update_transform(view);
912
913     unbind_framebuffer(fbo_id, tex_id);
914
915     save_as_bitmap(filename, readpixs, stride * height, width, height, PIXMAN_FORMAT_BPP(compositor->read_format));
916     free(readpixs);
917 }
918
919 static struct weston_view *
920 clear_viewlist_but_specified_surface(struct weston_compositor *compositor,
921                                      struct weston_surface *surface)
922 {
923     struct weston_view *view = NULL;
924     wl_list_init(&compositor->view_list);
925
926     wl_list_for_each(view, &surface->views, surface_link) {
927         if (view == NULL) {
928             continue;
929         }
930
931         wl_list_insert(compositor->view_list.prev, &view->link);
932         break;
933     }
934
935     return view;
936 }
937
938 static void
939 get_gl_surface_rectangle(struct ivi_layout_SurfaceProperties *prop,
940                          int32_t *x,
941                          int32_t *y,
942                          int32_t *width,
943                          int32_t *height)
944 {
945     if (prop == NULL) {
946         return;
947     }
948
949     *x = prop->sourceX;
950     *y = prop->sourceY;
951     *width = prop->sourceWidth;
952     *height = prop->sourceHeight;
953 }
954
955 static int
956 gl_surface_screenshot(struct ivisurface *ivisurf,
957                       struct weston_surface *surface,
958                       const char *filename)
959 {
960     struct weston_compositor *compositor = surface->compositor;
961     struct ivishell *shell = ivisurf->shell;
962     struct weston_view *view = NULL;
963     struct weston_output *output = NULL;
964     struct iviscreen *link_scrn = NULL;
965     struct ivi_layout_SurfaceProperties prop = {};
966     int32_t x = 0;
967     int32_t y = 0;
968     int32_t width = 0;
969     int32_t height = 0;
970
971     wl_list_for_each(link_scrn, &shell->list_screen, link) {
972         if (link_scrn != NULL) {
973             break;
974         }
975     }
976
977     if (link_scrn == NULL) {
978         fprintf(stderr, "Failed to get iviscreen\n");
979         return -1;
980     }
981
982     if (ivi_layout_getPropertiesOfSurface(ivisurf->layout_surface, &prop) != 0) {
983         fprintf(stderr, "Failed to get surface properties.");
984         return -1;
985     }
986
987     view = clear_viewlist_but_specified_surface(compositor, surface);
988     if (view != NULL) {
989         int32_t plane_is_null = 0;
990         if (view->plane == NULL) {
991             plane_is_null = 1;
992             view->plane = &compositor->primary_plane;
993         }
994
995         get_gl_surface_rectangle(&prop, &x, &y, &width, &height);
996         dump_surface(link_scrn->output,
997                      compositor,
998                      filename,
999                      view,
1000                      x,
1001                      y,
1002                      width,
1003                      height);
1004
1005         if (plane_is_null) {
1006             view->plane = NULL;
1007         }
1008     }
1009
1010     return 0;
1011 }
1012
1013 static void
1014 controller_surface_screenshot(struct wl_client *client,
1015                   struct wl_resource *resource,
1016                   const char *filename)
1017 {
1018     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
1019     struct weston_surface *weston_surface = NULL;
1020     int32_t width = 0;
1021     int32_t height = 0;
1022     int32_t stride = 0;
1023
1024     weston_surface = ivi_layout_surfaceGetWestonSurface(ivisurf->layout_surface);
1025     if (weston_surface == NULL) {
1026         fprintf(stderr, "Failed to get weston surface.\n");
1027         return;
1028     }
1029
1030     if (ivi_layout_surfaceGetSize(ivisurf->layout_surface, &width, &height, &stride) != 0) {
1031         fprintf(stderr, "Failed to get surface size.\n");
1032         return;
1033     }
1034
1035     if (shm_surface_screenshot(weston_surface, width, height, stride, filename) != 0) {
1036         if (gl_surface_screenshot(ivisurf, weston_surface, filename) != 0) {
1037             fprintf(stderr, "Failed to capture surface.\n");
1038         }
1039     }
1040 }
1041
1042
1043 static void
1044 controller_surface_send_stats(struct wl_client *client,
1045                               struct wl_resource *resource)
1046 {
1047     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
1048     pid_t pid;
1049     uid_t uid;
1050     gid_t gid;
1051     wl_client_get_credentials(client, &pid, &uid, &gid);
1052
1053     ivi_controller_surface_send_stats(resource, 0, 0,
1054                                       ivisurf->update_count, pid, "");
1055 }
1056
1057 static void
1058 controller_surface_destroy(struct wl_client *client,
1059               struct wl_resource *resource,
1060               int32_t destroy_scene_object)
1061 {
1062     (void)client;
1063     (void)destroy_scene_object;
1064     wl_resource_destroy(resource);
1065 }
1066
1067 static void
1068 controller_surface_set_input_focus(struct wl_client *client,
1069               struct wl_resource *resource,
1070               uint32_t device,
1071               int32_t enabled)
1072 {
1073     (void)client;
1074     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
1075
1076     if (device & IVI_CONTROLLER_SURFACE_INPUT_DEVICE_KEYBOARD) {
1077         if (enabled) {
1078             ivi_layout_SetKeyboardFocusOn(ivisurf->layout_surface);
1079         }
1080     }
1081 }
1082
1083 static const
1084 struct ivi_controller_surface_interface controller_surface_implementation = {
1085     controller_surface_set_visibility,
1086     controller_surface_set_opacity,
1087     controller_surface_set_source_rectangle,
1088     controller_surface_set_destination_rectangle,
1089     controller_surface_set_configuration,
1090     controller_surface_set_orientation,
1091     controller_surface_screenshot,
1092     controller_surface_send_stats,
1093     controller_surface_destroy,
1094     controller_surface_set_input_focus
1095 };
1096
1097 static void
1098 controller_layer_set_source_rectangle(struct wl_client *client,
1099                    struct wl_resource *resource,
1100                    int32_t x,
1101                    int32_t y,
1102                    int32_t width,
1103                    int32_t height)
1104 {
1105     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1106     (void)client;
1107     ivi_layout_layerSetSourceRectangle(ivilayer->layout_layer,
1108            (uint32_t)x, (uint32_t)y, (uint32_t)width, (uint32_t)height);
1109 }
1110
1111 static void
1112 controller_layer_set_destination_rectangle(struct wl_client *client,
1113                  struct wl_resource *resource,
1114                  int32_t x,
1115                  int32_t y,
1116                  int32_t width,
1117                  int32_t height)
1118 {
1119     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1120     (void)client;
1121     ivi_layout_layerSetDestinationRectangle(ivilayer->layout_layer,
1122             (uint32_t)x, (uint32_t)y, (uint32_t)width, (uint32_t)height);
1123 }
1124
1125 static void
1126 controller_layer_set_visibility(struct wl_client *client,
1127                     struct wl_resource *resource,
1128                     uint32_t visibility)
1129 {
1130     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1131     (void)client;
1132     ivi_layout_layerSetVisibility(ivilayer->layout_layer, visibility);
1133 }
1134
1135 static void
1136 controller_layer_set_opacity(struct wl_client *client,
1137                  struct wl_resource *resource,
1138                  wl_fixed_t opacity)
1139 {
1140     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1141     (void)client;
1142     ivi_layout_layerSetOpacity(ivilayer->layout_layer, (float)opacity);
1143 }
1144
1145 static void
1146 controller_layer_set_configuration(struct wl_client *client,
1147                  struct wl_resource *resource,
1148                  int32_t width,
1149                  int32_t height)
1150 {
1151     /* This interface has been supported yet. */
1152     (void)client;
1153     (void)resource;
1154     (void)width;
1155     (void)height;
1156 }
1157
1158 static void
1159 controller_layer_set_orientation(struct wl_client *client,
1160                  struct wl_resource *resource,
1161                  int32_t orientation)
1162 {
1163     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1164     (void)client;
1165     ivi_layout_layerSetOrientation(ivilayer->layout_layer, (uint32_t)orientation);
1166 }
1167
1168 static void
1169 controller_layer_clear_surfaces(struct wl_client *client,
1170                     struct wl_resource *resource)
1171 {
1172     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1173     (void)client;
1174     ivi_layout_layerSetRenderOrder(ivilayer->layout_layer, NULL, 0);
1175 }
1176
1177 static void
1178 controller_layer_add_surface(struct wl_client *client,
1179                  struct wl_resource *resource,
1180                  struct wl_resource *surface)
1181 {
1182     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1183     struct ivisurface *ivisurf = wl_resource_get_user_data(surface);
1184     (void)client;
1185     ivi_layout_layerAddSurface(ivilayer->layout_layer, ivisurf->layout_surface);
1186 }
1187
1188 static void
1189 controller_layer_remove_surface(struct wl_client *client,
1190                     struct wl_resource *resource,
1191                     struct wl_resource *surface)
1192 {
1193     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1194     struct ivisurface *ivisurf = wl_resource_get_user_data(surface);
1195     (void)client;
1196     ivi_layout_layerRemoveSurface(ivilayer->layout_layer, ivisurf->layout_surface);
1197 }
1198
1199 static void
1200 controller_layer_screenshot(struct wl_client *client,
1201                 struct wl_resource *resource,
1202                 const char *filename)
1203 {
1204     (void)client;
1205     (void)resource;
1206     (void)filename;
1207 }
1208
1209 static void
1210 controller_layer_set_render_order(struct wl_client *client,
1211                                   struct wl_resource *resource,
1212                                   struct wl_array *id_surfaces)
1213 {
1214     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1215     struct ivi_layout_surface **layoutsurf_array = NULL;
1216     struct ivisurface *ivisurf = NULL;
1217     uint32_t *id_surface = NULL;
1218     uint32_t id_layout_surface = 0;
1219     int i = 0;
1220     (void)client;
1221
1222     layoutsurf_array = (struct ivi_layout_surface**)calloc(
1223                            id_surfaces->size, sizeof(void*));
1224
1225     wl_array_for_each(id_surface, id_surfaces) {
1226         wl_list_for_each(ivisurf, &ivilayer->shell->list_surface, link) {
1227             id_layout_surface = ivi_layout_getIdOfSurface(ivisurf->layout_surface);
1228             if (*id_surface == id_layout_surface) {
1229                 layoutsurf_array[i] = ivisurf->layout_surface;
1230                 i++;
1231                 break;
1232             }
1233         }
1234     }
1235
1236     ivi_layout_layerSetRenderOrder(ivilayer->layout_layer,
1237                                    layoutsurf_array, i);
1238     free(layoutsurf_array);
1239 }
1240
1241 static void
1242 controller_layer_destroy(struct wl_client *client,
1243               struct wl_resource *resource,
1244               int32_t destroy_scene_object)
1245 {
1246     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1247     struct ivishell *shell = ivilayer->shell;
1248     struct ivicontroller_layer *ctrllayer = NULL;
1249     struct ivicontroller_layer *next = NULL;
1250     uint32_t id_layer = ivi_layout_getIdOfLayer(ivilayer->layout_layer);
1251     (void)client;
1252     (void)destroy_scene_object;
1253
1254     ivilayer->layer_canbe_removed = 1;
1255     wl_list_for_each_safe(ctrllayer, next, &shell->list_controller_layer, link) {
1256         if (ctrllayer->id_layer != id_layer) {
1257             continue;
1258         }
1259
1260         wl_resource_destroy(ctrllayer->resource);
1261     }
1262 }
1263
1264 static const
1265 struct ivi_controller_layer_interface controller_layer_implementation = {
1266     controller_layer_set_visibility,
1267     controller_layer_set_opacity,
1268     controller_layer_set_source_rectangle,
1269     controller_layer_set_destination_rectangle,
1270     controller_layer_set_configuration,
1271     controller_layer_set_orientation,
1272     controller_layer_screenshot,
1273     controller_layer_clear_surfaces,
1274     controller_layer_add_surface,
1275     controller_layer_remove_surface,
1276     controller_layer_set_render_order,
1277     controller_layer_destroy
1278 };
1279
1280 static void
1281 controller_screen_destroy(struct wl_client *client,
1282                           struct wl_resource *resource)
1283 {
1284     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1285     struct ivicontroller_screen *ctrlscrn = NULL;
1286     struct ivicontroller_screen *next = NULL;
1287 //    uint32_t id_screen = ivi_layout_getIdOfScreen(iviscrn->layout_screen);
1288     (void)client;
1289
1290     wl_list_for_each_safe(ctrlscrn, next,
1291                           &iviscrn->shell->list_controller_screen, link) {
1292         if (resource != ctrlscrn->resource) {
1293             continue;
1294         }
1295
1296         wl_list_remove(&ctrlscrn->link);
1297         wl_resource_destroy(ctrlscrn->resource);
1298         free(ctrlscrn);
1299         ctrlscrn = NULL;
1300         break;
1301     }
1302 }
1303
1304 static void
1305 controller_screen_clear(struct wl_client *client,
1306                 struct wl_resource *resource)
1307 {
1308     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1309     (void)client;
1310     ivi_layout_screenSetRenderOrder(iviscrn->layout_screen, NULL, 0);
1311 }
1312
1313 static void
1314 controller_screen_add_layer(struct wl_client *client,
1315                 struct wl_resource *resource,
1316                 struct wl_resource *layer)
1317 {
1318     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1319     struct ivilayer *ivilayer = wl_resource_get_user_data(layer);
1320     (void)client;
1321     ivi_layout_screenAddLayer(iviscrn->layout_screen, ivilayer->layout_layer);
1322 }
1323
1324 static void
1325 controller_screen_screenshot(struct wl_client *client,
1326                 struct wl_resource *resource,
1327                 const char *filename)
1328 {
1329     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1330     (void)client;
1331
1332     struct weston_output *output = NULL;
1333     cairo_surface_t *cairo_surf = NULL;
1334     int32_t i = 0;
1335     int32_t width = 0;
1336     int32_t height = 0;
1337     int32_t stride = 0;
1338     uint8_t *readpixs = NULL;
1339
1340     output = ivi_layout_screenGetOutput(iviscrn->layout_screen);
1341     --output->disable_planes;
1342
1343     width = output->current_mode->width;
1344     height = output->current_mode->height;
1345     stride = width * (PIXMAN_FORMAT_BPP(output->compositor->read_format) / 8);
1346
1347     readpixs = malloc(stride * height);
1348     if (readpixs == NULL) {
1349         weston_log("fails to allocate memory\n");
1350         return;
1351     }
1352
1353     output->compositor->renderer->read_pixels(
1354             output,
1355             output->compositor->read_format,
1356             readpixs,
1357             0,
1358             0,
1359             width,
1360             height);
1361
1362     save_as_bitmap(filename, readpixs, stride * height, width, height, PIXMAN_FORMAT_BPP(output->compositor->read_format));
1363     free(readpixs);
1364 }
1365
1366 static void
1367 controller_screen_set_render_order(struct wl_client *client,
1368                 struct wl_resource *resource,
1369                 struct wl_array *id_layers)
1370 {
1371     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1372     struct ivi_layout_layer **layoutlayer_array = NULL;
1373     struct ivilayer *ivilayer = NULL;
1374     uint32_t *id_layer = NULL;
1375     uint32_t id_layout_layer = 0;
1376     int i = 0;
1377     (void)client;
1378
1379     layoutlayer_array = (struct ivi_layout_layer**)calloc(
1380                            id_layers->size, sizeof(void*));
1381
1382     wl_array_for_each(id_layer, id_layers) {
1383         wl_list_for_each(ivilayer, &iviscrn->shell->list_layer, link) {
1384             id_layout_layer = ivi_layout_getIdOfLayer(ivilayer->layout_layer);
1385             if (*id_layer == id_layout_layer) {
1386                 layoutlayer_array[i] = ivilayer->layout_layer;
1387                 i++;
1388                 break;
1389             }
1390         }
1391     }
1392
1393     ivi_layout_screenSetRenderOrder(iviscrn->layout_screen,
1394                                     layoutlayer_array, i);
1395     free(layoutlayer_array);
1396 }
1397
1398 static const
1399 struct ivi_controller_screen_interface controller_screen_implementation = {
1400     controller_screen_destroy,
1401     controller_screen_clear,
1402     controller_screen_add_layer,
1403     controller_screen_screenshot,
1404     controller_screen_set_render_order
1405 };
1406
1407 static void
1408 controller_commit_changes(struct wl_client *client,
1409                           struct wl_resource *resource)
1410 {
1411     int32_t ans = 0;
1412     (void)client;
1413     (void)resource;
1414
1415     ans = ivi_layout_commitChanges();
1416     if (ans < 0) {
1417         weston_log("Failed to commit changes at controller_commit_changes\n");
1418     }
1419 }
1420
1421 static void
1422 controller_layer_create(struct wl_client *client,
1423                         struct wl_resource *resource,
1424                         uint32_t id_layer,
1425                         int32_t width,
1426                         int32_t height,
1427                         uint32_t id)
1428 {
1429     struct ivicontroller *ctrl = wl_resource_get_user_data(resource);
1430     struct ivishell *shell = ctrl->shell;
1431     struct ivi_layout_layer *layout_layer = NULL;
1432     struct ivicontroller_layer *ctrllayer = NULL;
1433     struct ivilayer *ivilayer = NULL;
1434     struct ivi_layout_LayerProperties prop;
1435
1436     ivilayer = get_layer(&shell->list_layer, id_layer);
1437     if (ivilayer == NULL) {
1438         layout_layer = ivi_layout_layerCreateWithDimension(id_layer,
1439                            (uint32_t)width, (uint32_t)height);
1440         if (layout_layer == NULL) {
1441             weston_log("id_layer is already created\n");
1442             return;
1443         }
1444
1445         /* ivilayer will be created by layer_event_create */
1446         ivilayer = get_layer(&shell->list_layer, id_layer);
1447         if (ivilayer == NULL) {
1448             weston_log("couldn't get layer object\n");
1449             return;
1450         }
1451     }
1452
1453     ctrllayer = calloc(1, sizeof *ctrllayer);
1454     if (!ctrllayer) {
1455         weston_log("no memory to allocate client layer\n");
1456         return;
1457     }
1458
1459     ++ivilayer->controller_layer_count;
1460     ivilayer->layer_canbe_removed = 0;
1461
1462     ctrllayer->shell = shell;
1463     ctrllayer->client = client;
1464     ctrllayer->id = id;
1465     ctrllayer->id_layer = id_layer;
1466     ctrllayer->resource = wl_resource_create(client,
1467                                &ivi_controller_layer_interface, 1, id);
1468     if (ctrllayer->resource == NULL) {
1469         weston_log("couldn't get layer object\n");
1470         return;
1471     }
1472
1473     wl_list_init(&ctrllayer->link);
1474     wl_list_insert(&shell->list_controller_layer, &ctrllayer->link);
1475
1476     wl_resource_set_implementation(ctrllayer->resource,
1477                                    &controller_layer_implementation,
1478                                    ivilayer, destroy_ivicontroller_layer);
1479
1480     memset(&prop, 0, sizeof prop);
1481
1482     ivi_layout_getPropertiesOfLayer(ivilayer->layout_layer, &prop);
1483     send_layer_event(ctrllayer->resource, ivilayer,
1484                      &prop, IVI_NOTIFICATION_ALL);
1485 }
1486
1487 static void
1488 surface_event_content(struct ivi_layout_surface *layout_surface, int32_t content, void *userdata)
1489 {
1490     struct ivishell *shell = userdata;
1491     struct ivicontroller_surface *ctrlsurf = NULL;
1492     uint32_t id_surface = 0;
1493
1494     if (content == 0) {
1495         surface_event_remove(layout_surface, userdata);
1496     }
1497 }
1498
1499 static void
1500 controller_surface_create(struct wl_client *client,
1501                           struct wl_resource *resource,
1502                           uint32_t id_surface,
1503                           uint32_t id)
1504 {
1505     struct ivicontroller *ctrl = wl_resource_get_user_data(resource);
1506     struct ivishell *shell = ctrl->shell;
1507     struct ivicontroller_surface *ctrlsurf = NULL;
1508     struct ivi_layout_SurfaceProperties prop;
1509     struct ivisurface *ivisurf = NULL;
1510     struct ivicontroller_surface *ctrl_link = NULL;
1511
1512     ivisurf = get_surface(&shell->list_surface, id_surface);
1513     if (ivisurf == NULL) {
1514         return;
1515     }
1516
1517     ctrlsurf = calloc(1, sizeof *ctrlsurf);
1518     if (!ctrlsurf) {
1519         weston_log("no memory to allocate controller surface\n");
1520         return;
1521     }
1522
1523     ctrlsurf->shell = shell;
1524     ctrlsurf->client = client;
1525     ctrlsurf->id = id;
1526     ctrlsurf->id_surface = id_surface;
1527     wl_list_init(&ctrlsurf->link);
1528     wl_list_insert(&shell->list_controller_surface, &ctrlsurf->link);
1529
1530     ctrlsurf->resource = wl_resource_create(client,
1531                                &ivi_controller_surface_interface, 1, id);
1532     if (ctrlsurf->resource == NULL) {
1533         weston_log("couldn't surface object");
1534         return;
1535     }
1536
1537     wl_list_for_each(ctrl_link, &shell->list_controller_surface, link) {
1538         if ((ctrl_link->implementation_set == 0) &&
1539             (ctrl_link->id_surface == id_surface) &&
1540             (ctrl_link->shell == shell) &&
1541             (ctrl_link->client != client)) {
1542             ++ivisurf->controller_surface_count;
1543             wl_resource_set_implementation(ctrl_link->resource,
1544                                            &controller_surface_implementation,
1545                                            ivisurf, destroy_ivicontroller_surface);
1546             ctrl_link->implementation_set = 1;
1547         }
1548     }
1549
1550     ++ivisurf->controller_surface_count;
1551
1552     wl_resource_set_implementation(ctrlsurf->resource,
1553                                    &controller_surface_implementation,
1554                                    ivisurf, destroy_ivicontroller_surface);
1555
1556     ctrlsurf->implementation_set = 1;
1557
1558     memset(&prop, 0, sizeof prop);
1559
1560     ivi_layout_getPropertiesOfSurface(ivisurf->layout_surface, &prop);
1561     ivi_layout_surfaceSetContentObserver(ivisurf->layout_surface, surface_event_content, shell);
1562
1563     send_surface_event(ctrlsurf->resource, ivisurf,
1564                        &prop, IVI_NOTIFICATION_ALL);
1565 }
1566
1567 static const struct ivi_controller_interface controller_implementation = {
1568     controller_commit_changes,
1569     controller_layer_create,
1570     controller_surface_create
1571 };
1572
1573 static void
1574 add_client_to_resources(struct ivishell *shell,
1575                         struct wl_client *client,
1576                         struct ivicontroller *controller)
1577 {
1578     struct ivisurface* ivisurf = NULL;
1579     struct ivilayer* ivilayer = NULL;
1580     struct iviscreen* iviscrn = NULL;
1581     struct ivicontroller_screen *ctrlscrn = NULL;
1582     struct wl_resource *resource_output = NULL;
1583     uint32_t id_layout_surface = 0;
1584     uint32_t id_layout_layer = 0;
1585
1586     wl_list_for_each(iviscrn, &shell->list_screen, link) {
1587         resource_output = wl_resource_find_for_client(
1588                 &iviscrn->output->resource_list, client);
1589         if (resource_output == NULL) {
1590             continue;
1591         }
1592
1593         ctrlscrn = controller_screen_create(iviscrn->shell, client, iviscrn);
1594         if (ctrlscrn == NULL) {
1595             continue;
1596         }
1597
1598         ivi_controller_send_screen(controller->resource,
1599                                    wl_resource_get_id(resource_output),
1600                                    ctrlscrn->resource);
1601     }
1602     wl_list_for_each_reverse(ivilayer, &shell->list_layer, link) {
1603         id_layout_layer =
1604             ivi_layout_getIdOfLayer(ivilayer->layout_layer);
1605
1606         ivi_controller_send_layer(controller->resource,
1607                                   id_layout_layer);
1608     }
1609     wl_list_for_each_reverse(ivisurf, &shell->list_surface, link) {
1610         id_layout_surface =
1611             ivi_layout_getIdOfSurface(ivisurf->layout_surface);
1612
1613         ivi_controller_send_surface(controller->resource,
1614                                     id_layout_surface);
1615     }
1616 }
1617
1618 static void
1619 bind_ivi_controller(struct wl_client *client, void *data,
1620                     uint32_t version, uint32_t id)
1621 {
1622     struct ivishell *shell = data;
1623     struct ivicontroller *controller;
1624     (void)version;
1625
1626     controller = calloc(1, sizeof *controller);
1627     if (controller == NULL) {
1628         weston_log("no memory to allocate controller\n");
1629         return;
1630     }
1631
1632     controller->resource =
1633         wl_resource_create(client, &ivi_controller_interface, 1, id);
1634     wl_resource_set_implementation(controller->resource,
1635                                    &controller_implementation,
1636                                    controller, unbind_resource_controller);
1637
1638     controller->shell = shell;
1639     controller->client = client;
1640     controller->id = id;
1641
1642     wl_list_init(&controller->link);
1643     wl_list_insert(&shell->list_controller, &controller->link);
1644
1645     add_client_to_resources(shell, client, controller);
1646 }
1647
1648 static struct iviscreen*
1649 create_screen(struct ivishell *shell, struct weston_output *output)
1650 {
1651     struct iviscreen *iviscrn;
1652     iviscrn = calloc(1, sizeof *iviscrn);
1653     if (iviscrn == NULL) {
1654         weston_log("no memory to allocate client screen\n");
1655         return NULL;
1656     }
1657
1658     iviscrn->shell = shell;
1659     iviscrn->output = output;
1660
1661 // TODO : Only Single display
1662     iviscrn->layout_screen = ivi_layout_getScreenFromId(0);
1663
1664     wl_list_init(&iviscrn->link);
1665
1666     return iviscrn;
1667 }
1668
1669 static struct ivilayer*
1670 create_layer(struct ivishell *shell,
1671              struct ivi_layout_layer *layout_layer,
1672              uint32_t id_layer)
1673 {
1674     struct ivilayer *ivilayer = NULL;
1675     struct ivicontroller *controller = NULL;
1676
1677     ivilayer = get_layer(&shell->list_layer, id_layer);
1678     if (ivilayer != NULL) {
1679         weston_log("id_layer is already created\n");
1680         return NULL;
1681     }
1682
1683     ivilayer = calloc(1, sizeof *ivilayer);
1684     if (NULL == ivilayer) {
1685         weston_log("no memory to allocate client layer\n");
1686         return NULL;
1687     }
1688
1689     ivilayer->shell = shell;
1690     wl_list_init(&ivilayer->list_screen);
1691     wl_list_init(&ivilayer->link);
1692     wl_list_insert(&shell->list_layer, &ivilayer->link);
1693     ivilayer->layout_layer = layout_layer;
1694
1695     ivi_layout_layerAddNotification(layout_layer, send_layer_prop, ivilayer);
1696
1697     wl_list_for_each(controller, &shell->list_controller, link) {
1698         ivi_controller_send_layer(controller->resource, id_layer);
1699     }
1700
1701     return ivilayer;
1702 }
1703
1704 static struct ivisurface*
1705 create_surface(struct ivishell *shell,
1706                struct ivi_layout_surface *layout_surface,
1707                uint32_t id_surface)
1708 {
1709     struct ivisurface *ivisurf = NULL;
1710     struct ivicontroller *controller = NULL;
1711
1712     ivisurf = get_surface(&shell->list_surface, id_surface);
1713     if (ivisurf != NULL) {
1714         weston_log("id_surface is already created\n");
1715         return NULL;
1716     }
1717
1718     ivisurf = calloc(1, sizeof *ivisurf);
1719     if (ivisurf == NULL) {
1720         weston_log("no memory to allocate client surface\n");
1721         return NULL;
1722     }
1723
1724     ivisurf->shell = shell;
1725     ivisurf->layout_surface = layout_surface;
1726     wl_list_init(&ivisurf->list_layer);
1727     wl_list_init(&ivisurf->link);
1728     wl_list_insert(&shell->list_surface, &ivisurf->link);
1729
1730     wl_list_for_each(controller, &shell->list_controller, link) {
1731         ivi_controller_send_surface(controller->resource,
1732                                     id_surface);
1733     }
1734
1735     ivi_layout_surfaceAddNotification(layout_surface,
1736                                     send_surface_prop, ivisurf);
1737
1738     return ivisurf;
1739 }
1740
1741 static void
1742 layer_event_create(struct ivi_layout_layer *layout_layer,
1743                      void *userdata)
1744 {
1745     struct ivishell *shell = userdata;
1746     struct ivilayer *ivilayer = NULL;
1747     uint32_t id_layer = 0;
1748
1749     id_layer = ivi_layout_getIdOfLayer(layout_layer);
1750
1751     ivilayer = create_layer(shell, layout_layer, id_layer);
1752     if (ivilayer == NULL) {
1753         weston_log("failed to create layer");
1754         return;
1755     }
1756 }
1757
1758 static void
1759 layer_event_remove(struct ivi_layout_layer *layout_layer,
1760                      void *userdata)
1761 {
1762     struct ivishell *shell = userdata;
1763     struct ivicontroller_layer *ctrllayer = NULL;
1764     struct ivilayer *ivilayer = NULL;
1765     struct ivilayer *next = NULL;
1766     uint32_t id_layer = 0;
1767     int is_removed = 0;
1768
1769     wl_list_for_each_safe(ivilayer, next, &shell->list_layer, link) {
1770         if (layout_layer != ivilayer->layout_layer) {
1771             continue;
1772         }
1773
1774         wl_list_remove(&ivilayer->link);
1775
1776         is_removed = 1;
1777         free(ivilayer);
1778         ivilayer = NULL;
1779         break;
1780     }
1781
1782     if (is_removed) {
1783         id_layer = ivi_layout_getIdOfLayer(layout_layer);
1784
1785         wl_list_for_each(ctrllayer, &shell->list_controller_layer, link) {
1786             if (id_layer != ctrllayer->id_layer) {
1787                 continue;
1788             }
1789             ivi_controller_layer_send_destroyed(ctrllayer->resource);
1790         }
1791     }
1792 }
1793
1794
1795 static void
1796 surface_event_create(struct ivi_layout_surface *layout_surface,
1797                      void *userdata)
1798 {
1799     struct ivishell *shell = userdata;
1800     struct ivisurface *ivisurf = NULL;
1801     uint32_t id_surface = 0;
1802     struct ivicontroller_surface *ctrlsurf = NULL;
1803
1804     id_surface = ivi_layout_getIdOfSurface(layout_surface);
1805
1806     ivisurf = create_surface(shell, layout_surface, id_surface);
1807     if (ivisurf == NULL) {
1808         weston_log("failed to create surface");
1809         return;
1810     }
1811
1812     wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
1813         if (id_surface != ctrlsurf->id_surface) {
1814             continue;
1815         }
1816         ivi_controller_surface_send_content(ctrlsurf->resource, IVI_CONTROLLER_SURFACE_CONTENT_STATE_CONTENT_AVAILABLE);
1817     }
1818 }
1819
1820 static void
1821 surface_event_remove(struct ivi_layout_surface *layout_surface,
1822                      void *userdata)
1823 {
1824     struct ivishell *shell = userdata;
1825     struct ivicontroller_surface *ctrlsurf = NULL;
1826     struct ivisurface *ivisurf = NULL;
1827     struct ivisurface *next = NULL;
1828     uint32_t id_surface = 0;
1829     int is_removed = 0;
1830
1831     wl_list_for_each_safe(ivisurf, next, &shell->list_surface, link) {
1832         if (layout_surface != ivisurf->layout_surface) {
1833             continue;
1834         }
1835
1836         wl_list_remove(&ivisurf->link);
1837         is_removed = 1;
1838
1839         if (ivisurf->controller_surface_count == 0) {
1840             free(ivisurf);
1841         }
1842         else {
1843             ivisurf->can_be_removed = 1;
1844         }
1845
1846         break;
1847     }
1848
1849     if (is_removed) {
1850         id_surface = ivi_layout_getIdOfSurface(layout_surface);
1851
1852         wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
1853             if (id_surface != ctrlsurf->id_surface) {
1854                 continue;
1855             }
1856             ivi_controller_surface_send_content(ctrlsurf->resource, IVI_CONTROLLER_SURFACE_CONTENT_STATE_CONTENT_REMOVED);
1857             ivi_controller_surface_send_destroyed(ctrlsurf->resource);
1858         }
1859     }
1860 }
1861
1862 static void
1863 surface_event_configure(struct ivi_layout_surface *layout_surface,
1864                         void *userdata)
1865 {
1866     struct ivishell *shell = userdata;
1867     struct ivisurface *ivisurf = NULL;
1868     struct ivicontroller_surface *ctrlsurf = NULL;
1869     struct ivi_layout_SurfaceProperties prop;
1870     uint32_t id_surface = 0;
1871
1872     id_surface = ivi_layout_getIdOfSurface(layout_surface);
1873
1874     ivisurf = get_surface(&shell->list_surface, id_surface);
1875     if (ivisurf == NULL) {
1876         weston_log("id_surface is not created yet\n");
1877         return;
1878     }
1879
1880     memset(&prop, 0, sizeof prop);
1881     ivi_layout_getPropertiesOfSurface(layout_surface, &prop);
1882
1883     wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
1884         if (id_surface != ctrlsurf->id_surface) {
1885             continue;
1886         }
1887         send_surface_event(ctrlsurf->resource, ivisurf,
1888                            &prop, IVI_NOTIFICATION_ALL);
1889     }
1890 }
1891
1892 static int32_t
1893 check_layout_layers(struct ivishell *shell)
1894 {
1895     struct ivi_layout_layer **pArray = NULL;
1896     struct ivilayer *ivilayer = NULL;
1897     uint32_t id_layer = 0;
1898     uint32_t length = 0;
1899     uint32_t i = 0;
1900     int32_t ret = 0;
1901
1902     ret = ivi_layout_getLayers(&length, &pArray);
1903     if(ret != 0) {
1904         weston_log("failed to get layers at check_layout_layers\n");
1905         return -1;
1906     }
1907
1908     if (length == 0) {
1909         /* if length is 0, pArray doesn't need to free.*/
1910         return 0;
1911     }
1912
1913     for (i = 0; i < length; i++) {
1914         id_layer = ivi_layout_getIdOfLayer(pArray[i]);
1915         ivilayer = create_layer(shell, pArray[i], id_layer);
1916         if (ivilayer == NULL) {
1917             weston_log("failed to create layer");
1918         }
1919     }
1920
1921     free(pArray);
1922     pArray = NULL;
1923
1924     return 0;
1925 }
1926
1927 static int32_t
1928 check_layout_surfaces(struct ivishell *shell)
1929 {
1930     struct ivi_layout_surface **pArray = NULL;
1931     struct ivisurface *ivisurf = NULL;
1932     uint32_t id_surface = 0;
1933     uint32_t length = 0;
1934     uint32_t i = 0;
1935     int32_t ret = 0;
1936
1937     ret = ivi_layout_getSurfaces(&length, &pArray);
1938     if(ret != 0) {
1939         weston_log("failed to get surfaces at check_layout_surfaces\n");
1940         return -1;
1941     }
1942
1943     if (length == 0) {
1944         /* if length is 0, pArray doesn't need to free.*/
1945         return 0;
1946     }
1947
1948     for (i = 0; i < length; i++) {
1949         id_surface = ivi_layout_getIdOfSurface(pArray[i]);
1950         ivisurf = create_surface(shell, pArray[i], id_surface);
1951         if (ivisurf == NULL) {
1952             weston_log("failed to create surface");
1953         }
1954     }
1955
1956     free(pArray);
1957     pArray = NULL;
1958
1959     return 0;
1960 }
1961
1962 static void
1963 init_ivi_shell(struct weston_compositor *ec, struct ivishell *shell)
1964 {
1965     struct weston_output *output = NULL;
1966     struct iviscreen *iviscrn = NULL;
1967     int32_t ret = 0;
1968
1969     shell->compositor = ec;
1970
1971     wl_list_init(&shell->list_surface);
1972     wl_list_init(&shell->list_layer);
1973     wl_list_init(&shell->list_screen);
1974     wl_list_init(&shell->list_weston_surface);
1975     wl_list_init(&shell->list_controller);
1976     wl_list_init(&shell->list_controller_screen);
1977     wl_list_init(&shell->list_controller_layer);
1978     wl_list_init(&shell->list_controller_surface);
1979     shell->event_restriction = 0;
1980
1981     wl_list_for_each(output, &ec->output_list, link) {
1982         iviscrn = create_screen(shell, output);
1983         if (iviscrn != NULL) {
1984             wl_list_insert(&shell->list_screen, &iviscrn->link);
1985         }
1986     }
1987
1988     ret = check_layout_layers(shell);
1989     if (ret != 0) {
1990         weston_log("failed to check_layout_layers");
1991     }
1992
1993     ret = check_layout_surfaces(shell);
1994     if (ret != 0) {
1995         weston_log("failed to check_layout_surfaces");
1996     }
1997
1998     ivi_layout_addNotificationCreateLayer(layer_event_create, shell);
1999     ivi_layout_addNotificationRemoveLayer(layer_event_remove, shell);
2000
2001     ivi_layout_addNotificationCreateSurface(surface_event_create, shell);
2002     ivi_layout_addNotificationRemoveSurface(surface_event_remove, shell);
2003     ivi_layout_addNotificationConfigureSurface(surface_event_configure, shell);
2004 }
2005
2006 WL_EXPORT int
2007 module_init(struct weston_compositor *ec,
2008             int *argc, char *argv[])
2009 {
2010     struct ivishell *shell;
2011     (void)argc;
2012     (void)argv;
2013
2014     shell = malloc(sizeof *shell);
2015     if (shell == NULL)
2016         return -1;
2017
2018     memset(shell, 0, sizeof *shell);
2019     init_ivi_shell(ec, shell);
2020
2021     if (wl_global_create(ec->wl_display, &ivi_controller_interface, 1,
2022                          shell, bind_ivi_controller) == NULL) {
2023         return -1;
2024     }
2025
2026     return 0;
2027 }