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