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