weston-ivi-shell: Remove child context of a layer when the main context of the layer...
[profile/ivi/wayland-ivi-extension.git] / ivi-layermanagement-api / ilmControl / src / ilm_control_wayland_platform.c
1 /**************************************************************************
2  *
3  * Copyright (C) 2013 DENSO CORPORATION
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  ****************************************************************************/
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <memory.h>
22 #include <unistd.h>
23 #include <pthread.h>
24 #include <signal.h>
25 #include <poll.h>
26 #include "ilm_common.h"
27 #include "ilm_control_platform.h"
28 #include "wayland-util.h"
29 #include "ivi-controller-client-protocol.h"
30
31 static ilmErrorTypes wayland_getPropertiesOfLayer(t_ilm_uint layerID,
32                          struct ilmLayerProperties* pLayerProperties);
33 static ilmErrorTypes wayland_getPropertiesOfScreen(t_ilm_display screenID,
34                          struct ilmScreenProperties* pScreenProperties);
35 static ilmErrorTypes wayland_getNumberOfHardwareLayers(t_ilm_uint screenID,
36                          t_ilm_uint* pNumberOfHardwareLayers);
37 static ilmErrorTypes wayland_getScreenIDs(t_ilm_uint* pNumberOfIDs,
38                          t_ilm_uint** ppIDs);
39 static ilmErrorTypes wayland_getLayerIDs(t_ilm_int* pLength,
40                          t_ilm_layer** ppArray);
41 static ilmErrorTypes wayland_getLayerIDsOnScreen(t_ilm_uint screenId,
42                          t_ilm_int* pLength, t_ilm_layer** ppArray);
43 static ilmErrorTypes wayland_getSurfaceIDs(t_ilm_int* pLength,
44                          t_ilm_surface** ppArray);
45 static ilmErrorTypes wayland_getSurfaceIDsOnLayer(t_ilm_layer layer,
46                          t_ilm_int* pLength, t_ilm_surface** ppArray);
47 static ilmErrorTypes wayland_layerCreateWithDimension(t_ilm_layer* pLayerId,
48                          t_ilm_uint width, t_ilm_uint height);
49 static ilmErrorTypes wayland_layerRemove(t_ilm_layer layerId);
50 static ilmErrorTypes wayland_layerGetType(t_ilm_layer layerId,
51                          ilmLayerType* pLayerType);
52 static ilmErrorTypes wayland_layerSetVisibility(t_ilm_layer layerId,
53                          t_ilm_bool newVisibility);
54 static ilmErrorTypes wayland_layerGetVisibility(t_ilm_layer layerId,
55                          t_ilm_bool *pVisibility);
56 static ilmErrorTypes wayland_layerSetOpacity(t_ilm_layer layerId,
57                          t_ilm_float opacity);
58 static ilmErrorTypes wayland_layerGetOpacity(t_ilm_layer layerId,
59                          t_ilm_float *pOpacity);
60 static ilmErrorTypes wayland_layerSetSourceRectangle(t_ilm_layer layerId,
61                          t_ilm_uint x, t_ilm_uint y,
62                          t_ilm_uint width, t_ilm_uint height);
63 static ilmErrorTypes wayland_layerSetDestinationRectangle(t_ilm_layer layerId,
64                          t_ilm_int x, t_ilm_int y,
65                          t_ilm_int width, t_ilm_int height);
66 static ilmErrorTypes wayland_layerGetDimension(t_ilm_layer layerId,
67                          t_ilm_uint *pDimension);
68 static ilmErrorTypes wayland_layerSetDimension(t_ilm_layer layerId,
69                          t_ilm_uint *pDimension);
70 static ilmErrorTypes wayland_layerGetPosition(t_ilm_layer layerId,
71                          t_ilm_uint *pPosition);
72 static ilmErrorTypes wayland_layerSetPosition(t_ilm_layer layerId,
73                          t_ilm_uint *pPosition);
74 static ilmErrorTypes wayland_layerSetOrientation(t_ilm_layer layerId,
75                          ilmOrientation orientation);
76 static ilmErrorTypes wayland_layerGetOrientation(t_ilm_layer layerId,
77                          ilmOrientation *pOrientation);
78 static ilmErrorTypes wayland_layerSetChromaKey(t_ilm_layer layerId,
79                          t_ilm_int* pColor);
80 static ilmErrorTypes wayland_layerSetRenderOrder(t_ilm_layer layerId,
81                          t_ilm_layer *pSurfaceId,
82                          t_ilm_int number);
83 static ilmErrorTypes wayland_layerGetCapabilities(t_ilm_layer layerId,
84                          t_ilm_layercapabilities *pCapabilities);
85 static ilmErrorTypes wayland_layerTypeGetCapabilities(ilmLayerType layerType,
86                          t_ilm_layercapabilities *pCapabilities);
87 static ilmErrorTypes wayland_surfaceSetVisibility(t_ilm_surface surfaceId,
88                          t_ilm_bool newVisibility);
89 static ilmErrorTypes wayland_surfaceSetOpacity(t_ilm_surface surfaceId,
90                          t_ilm_float opacity);
91 static ilmErrorTypes wayland_surfaceGetOpacity(t_ilm_surface surfaceId,
92                          t_ilm_float *pOpacity);
93 static ilmErrorTypes wayland_SetKeyboardFocusOn(t_ilm_surface surfaceId);
94 static ilmErrorTypes wayland_GetKeyboardFocusSurfaceId(
95                          t_ilm_surface* pSurfaceId);
96 static ilmErrorTypes wayland_surfaceSetDestinationRectangle(
97                          t_ilm_surface surfaceId,
98                          t_ilm_int x, t_ilm_int y,
99                          t_ilm_int width, t_ilm_int height);
100 static ilmErrorTypes wayland_surfaceSetDimension(t_ilm_surface surfaceId,
101                          t_ilm_uint *pDimension);
102 static ilmErrorTypes wayland_surfaceGetPosition(t_ilm_surface surfaceId,
103                          t_ilm_uint *pPosition);
104 static ilmErrorTypes wayland_surfaceSetPosition(t_ilm_surface surfaceId,
105                          t_ilm_uint *pPosition);
106 static ilmErrorTypes wayland_surfaceSetOrientation(t_ilm_surface surfaceId,
107                          ilmOrientation orientation);
108 static ilmErrorTypes wayland_surfaceGetOrientation(t_ilm_surface surfaceId,
109                          ilmOrientation *pOrientation);
110 static ilmErrorTypes wayland_surfaceGetPixelformat(t_ilm_layer surfaceId,
111                          ilmPixelFormat *pPixelformat);
112 static ilmErrorTypes wayland_surfaceSetChromaKey(t_ilm_surface surfaceId,
113                          t_ilm_int* pColor);
114 static ilmErrorTypes wayland_displaySetRenderOrder(t_ilm_display display,
115                          t_ilm_layer *pLayerId, const t_ilm_uint number);
116 static ilmErrorTypes wayland_takeScreenshot(t_ilm_uint screen,
117                          t_ilm_const_string filename);
118 static ilmErrorTypes wayland_takeLayerScreenshot(t_ilm_const_string filename,
119                          t_ilm_layer layerid);
120 static ilmErrorTypes wayland_takeSurfaceScreenshot(t_ilm_const_string filename,
121                          t_ilm_surface surfaceid);
122 static ilmErrorTypes wayland_SetOptimizationMode(ilmOptimization id,
123                          ilmOptimizationMode mode);
124 static ilmErrorTypes wayland_GetOptimizationMode(ilmOptimization id,
125                          ilmOptimizationMode* pMode);
126 static ilmErrorTypes wayland_layerAddNotification(t_ilm_layer layer,
127                          layerNotificationFunc callback);
128 static ilmErrorTypes wayland_layerRemoveNotification(t_ilm_layer layer);
129 static ilmErrorTypes wayland_surfaceAddNotification(t_ilm_surface surface,
130                          surfaceNotificationFunc callback);
131 static ilmErrorTypes wayland_surfaceRemoveNotification(t_ilm_surface surface);
132 static ilmErrorTypes wayland_init(t_ilm_nativedisplay nativedisplay);
133 static void wayland_destroy();
134 static ilmErrorTypes wayland_getNativeHandle(t_ilm_uint pid,
135                          t_ilm_int *n_handle,
136                          t_ilm_nativehandle **p_handles);
137 static ilmErrorTypes wayland_getPropertiesOfSurface(t_ilm_uint surfaceID,
138                          struct ilmSurfaceProperties* pSurfaceProperties);
139 static ilmErrorTypes wayland_layerAddSurface(t_ilm_layer layerId,
140                          t_ilm_surface surfaceId);
141 static ilmErrorTypes wayland_layerRemoveSurface(t_ilm_layer layerId,
142                          t_ilm_surface surfaceId);
143 static ilmErrorTypes wayland_surfaceGetDimension(t_ilm_surface surfaceId,
144                          t_ilm_uint *pDimension);
145 static ilmErrorTypes wayland_surfaceGetVisibility(t_ilm_surface surfaceId,
146                          t_ilm_bool *pVisibility);
147 static ilmErrorTypes wayland_surfaceSetSourceRectangle(t_ilm_surface surfaceId,
148                          t_ilm_int x, t_ilm_int y,
149                          t_ilm_int width, t_ilm_int height);
150 static ilmErrorTypes wayland_commitChanges();
151
152 void init_ilmControlPlatformTable()
153 {
154     gIlmControlPlatformFunc.getPropertiesOfLayer =
155         wayland_getPropertiesOfLayer;
156     gIlmControlPlatformFunc.getPropertiesOfScreen =
157         wayland_getPropertiesOfScreen;
158     gIlmControlPlatformFunc.getNumberOfHardwareLayers =
159         wayland_getNumberOfHardwareLayers;
160     gIlmControlPlatformFunc.getScreenIDs =
161         wayland_getScreenIDs;
162     gIlmControlPlatformFunc.getLayerIDs =
163         wayland_getLayerIDs;
164     gIlmControlPlatformFunc.getLayerIDsOnScreen =
165         wayland_getLayerIDsOnScreen;
166     gIlmControlPlatformFunc.getSurfaceIDs =
167         wayland_getSurfaceIDs;
168     gIlmControlPlatformFunc.getSurfaceIDsOnLayer =
169         wayland_getSurfaceIDsOnLayer;
170     gIlmControlPlatformFunc.layerCreateWithDimension =
171         wayland_layerCreateWithDimension;
172     gIlmControlPlatformFunc.layerRemove =
173         wayland_layerRemove;
174     gIlmControlPlatformFunc.layerGetType =
175         wayland_layerGetType;
176     gIlmControlPlatformFunc.layerSetVisibility =
177         wayland_layerSetVisibility;
178     gIlmControlPlatformFunc.layerGetVisibility =
179         wayland_layerGetVisibility;
180     gIlmControlPlatformFunc.layerSetOpacity =
181         wayland_layerSetOpacity;
182     gIlmControlPlatformFunc.layerGetOpacity =
183         wayland_layerGetOpacity;
184     gIlmControlPlatformFunc.layerSetSourceRectangle =
185         wayland_layerSetSourceRectangle;
186     gIlmControlPlatformFunc.layerSetDestinationRectangle =
187         wayland_layerSetDestinationRectangle;
188     gIlmControlPlatformFunc.layerGetDimension =
189         wayland_layerGetDimension;
190     gIlmControlPlatformFunc.layerSetDimension =
191         wayland_layerSetDimension;
192     gIlmControlPlatformFunc.layerGetPosition =
193         wayland_layerGetPosition;
194     gIlmControlPlatformFunc.layerSetPosition =
195         wayland_layerSetPosition;
196     gIlmControlPlatformFunc.layerSetOrientation =
197         wayland_layerSetOrientation;
198     gIlmControlPlatformFunc.layerGetOrientation =
199         wayland_layerGetOrientation;
200     gIlmControlPlatformFunc.layerSetChromaKey =
201         wayland_layerSetChromaKey;
202     gIlmControlPlatformFunc.layerSetRenderOrder =
203         wayland_layerSetRenderOrder;
204     gIlmControlPlatformFunc.layerGetCapabilities =
205         wayland_layerGetCapabilities;
206     gIlmControlPlatformFunc.layerTypeGetCapabilities =
207         wayland_layerTypeGetCapabilities;
208     gIlmControlPlatformFunc.surfaceSetVisibility =
209         wayland_surfaceSetVisibility;
210     gIlmControlPlatformFunc.surfaceSetOpacity =
211         wayland_surfaceSetOpacity;
212     gIlmControlPlatformFunc.surfaceGetOpacity =
213         wayland_surfaceGetOpacity;
214     gIlmControlPlatformFunc.SetKeyboardFocusOn =
215         wayland_SetKeyboardFocusOn;
216     gIlmControlPlatformFunc.GetKeyboardFocusSurfaceId =
217         wayland_GetKeyboardFocusSurfaceId;
218     gIlmControlPlatformFunc.surfaceSetDestinationRectangle =
219         wayland_surfaceSetDestinationRectangle;
220     gIlmControlPlatformFunc.surfaceSetDimension =
221         wayland_surfaceSetDimension;
222     gIlmControlPlatformFunc.surfaceGetPosition =
223         wayland_surfaceGetPosition;
224     gIlmControlPlatformFunc.surfaceSetPosition =
225         wayland_surfaceSetPosition;
226     gIlmControlPlatformFunc.surfaceSetOrientation =
227         wayland_surfaceSetOrientation;
228     gIlmControlPlatformFunc.surfaceGetOrientation =
229         wayland_surfaceGetOrientation;
230     gIlmControlPlatformFunc.surfaceGetPixelformat =
231         wayland_surfaceGetPixelformat;
232     gIlmControlPlatformFunc.surfaceSetChromaKey =
233         wayland_surfaceSetChromaKey;
234     gIlmControlPlatformFunc.displaySetRenderOrder =
235         wayland_displaySetRenderOrder;
236     gIlmControlPlatformFunc.takeScreenshot =
237         wayland_takeScreenshot;
238     gIlmControlPlatformFunc.takeLayerScreenshot =
239         wayland_takeLayerScreenshot;
240     gIlmControlPlatformFunc.takeSurfaceScreenshot =
241         wayland_takeSurfaceScreenshot;
242     gIlmControlPlatformFunc.SetOptimizationMode =
243         wayland_SetOptimizationMode;
244     gIlmControlPlatformFunc.GetOptimizationMode =
245         wayland_GetOptimizationMode;
246     gIlmControlPlatformFunc.layerAddNotification =
247         wayland_layerAddNotification;
248     gIlmControlPlatformFunc.layerRemoveNotification =
249         wayland_layerRemoveNotification;
250     gIlmControlPlatformFunc.surfaceAddNotification =
251         wayland_surfaceAddNotification;
252     gIlmControlPlatformFunc.surfaceRemoveNotification =
253         wayland_surfaceRemoveNotification;
254     gIlmControlPlatformFunc.init =
255         wayland_init;
256     gIlmControlPlatformFunc.destroy =
257         wayland_destroy;
258     gIlmControlPlatformFunc.getNativeHandle =
259         wayland_getNativeHandle;
260     gIlmControlPlatformFunc.getPropertiesOfSurface =
261         wayland_getPropertiesOfSurface;
262     gIlmControlPlatformFunc.layerAddSurface =
263         wayland_layerAddSurface;
264     gIlmControlPlatformFunc.layerRemoveSurface =
265         wayland_layerRemoveSurface;
266     gIlmControlPlatformFunc.surfaceGetDimension =
267         wayland_surfaceGetDimension;
268     gIlmControlPlatformFunc.surfaceGetVisibility =
269         wayland_surfaceGetVisibility;
270     gIlmControlPlatformFunc.surfaceSetSourceRectangle =
271         wayland_surfaceSetSourceRectangle;
272     gIlmControlPlatformFunc.commitChanges =
273         wayland_commitChanges;
274 }
275
276 struct surface_context {
277     struct wl_list link;
278
279     struct ivi_surface *surface;
280     struct ivi_controller_surface *controller;
281
282     t_ilm_uint id_surface;
283     struct ilmSurfaceProperties prop;
284     surfaceNotificationFunc notification;
285
286     struct {
287         struct wl_list link;
288     } order;
289
290     struct ilm_control_context *ctx;
291 };
292
293 struct layer_context {
294     struct wl_list link;
295
296     struct ivi_controller_layer *controller;
297     t_ilm_uint id_layer;
298
299     struct ilmLayerProperties prop;
300     layerNotificationFunc notification;
301
302     struct {
303         struct wl_list list_surface;
304         struct wl_list link;
305     } order;
306
307     struct ilm_control_context *ctx;
308 };
309
310 struct screen_context {
311     struct wl_list link;
312
313     struct wl_output *output;
314     struct ivi_controller_screen *controller;
315     t_ilm_uint id_from_server;
316     t_ilm_uint id_screen;
317
318     struct ilmScreenProperties prop;
319
320     struct {
321         struct wl_list list_layer;
322         struct wl_list link;
323     } order;
324
325     struct ilm_control_context *ctx;
326 };
327
328 struct nativehandle_context {
329     uint32_t pid;
330     uint32_t nativehandle;
331     struct wl_list link;
332 };
333
334 struct wayland_context {
335     struct wl_display *display;
336     struct wl_registry *registry;
337     struct wl_compositor *compositor;
338     struct ivi_controller *controller;
339     uint32_t num_screen;
340
341     struct wl_list list_surface;
342     struct wl_list list_layer;
343     struct wl_list list_screen;
344 };
345
346 struct ilm_control_context {
347     struct wayland_context main_ctx;
348     struct wayland_context child_ctx;
349     int32_t valid;
350
351     uint32_t num_screen;
352
353     uint32_t internal_id_layer;
354
355     struct wl_list list_nativehandle;
356
357     pthread_t thread;
358     pthread_mutex_t mutex;
359     uint32_t internal_id_surface;
360 };
361
362 static int32_t
363 wayland_controller_is_inside_surface_list(struct wl_list *list,
364                                           uint32_t id_surface)
365 {
366     struct surface_context *ctx_surf = NULL;
367     wl_list_for_each(ctx_surf, list, link) {
368         if (ctx_surf->id_surface == id_surface) {
369             return 1;
370         }
371     }
372
373     return 0;
374 }
375
376 static int32_t
377 wayland_controller_is_inside_layer_list(struct wl_list *list,
378                                         uint32_t id_layer)
379 {
380     struct layer_context *ctx_layer = NULL;
381     wl_list_for_each(ctx_layer, list, link) {
382         if (ctx_layer->id_layer == id_layer) {
383             return 1;
384         }
385     }
386
387     return 0;
388 }
389
390 static struct layer_context*
391 wayland_controller_get_layer_context(struct wayland_context *ctx,
392                                      uint32_t id_layer)
393 {
394     struct layer_context *ctx_layer = NULL;
395
396     if (ctx->controller == NULL) {
397         fprintf(stderr, "controller is not initialized in ilmControl\n");
398         return NULL;
399     }
400
401     wl_list_for_each(ctx_layer, &ctx->list_layer, link) {
402         if (ctx_layer->id_layer == id_layer) {
403             return ctx_layer;
404         }
405     }
406
407     fprintf(stderr, "failed to get layer context in ilmControl\n");
408     return NULL;
409 }
410
411 static struct ilm_control_context* get_instance();
412
413 static void
414 output_listener_geometry(void *data,
415                          struct wl_output *output,
416                          int32_t x,
417                          int32_t y,
418                          int32_t physical_width,
419                          int32_t physical_height,
420                          int32_t subpixel,
421                          const char *make,
422                          const char *model,
423                          int32_t transform)
424 {
425     struct screen_context *ctx_scrn = data;
426     (void)output;
427     (void)x;
428     (void)y;
429     (void)subpixel;
430     (void)make;
431     (void)model;
432     (void)transform;
433
434     ctx_scrn->prop.screenWidth = physical_width;
435     ctx_scrn->prop.screenHeight = physical_height;
436 }
437
438 static void
439 output_listener_mode(void *data,
440                      struct wl_output *output,
441                      uint32_t flags,
442                      int32_t width,
443                      int32_t height,
444                      int32_t refresh)
445 {
446     (void)data;
447     (void)output;
448     (void)flags;
449     (void)width;
450     (void)height;
451     (void)refresh;
452 }
453
454 static void
455 output_listener_done(void *data,
456                      struct wl_output *output)
457 {
458     (void)data;
459     (void)output;
460 }
461
462 static void
463 output_listener_scale(void *data,
464                       struct wl_output *output,
465                       int32_t factor)
466 {
467     (void)data;
468     (void)output;
469     (void)factor;
470 }
471
472 static struct wl_output_listener output_listener = {
473     output_listener_geometry,
474     output_listener_mode,
475     output_listener_done,
476     output_listener_scale
477 };
478
479 static struct screen_context*
480 get_screen_context_by_output(struct wayland_context *ctx,
481                              struct wl_output *output)
482 {
483     struct screen_context *ctx_scrn = NULL;
484     struct wl_proxy *pxy_out = NULL;
485     struct wl_proxy *pxy_out_in_scrn = NULL;
486     uint32_t pxy_id = 0;
487     uint32_t pxy_id_in_scrn = 0;
488
489     wl_list_for_each(ctx_scrn, &ctx->list_screen, link) {
490         pxy_out = (struct wl_proxy*)output;
491         pxy_out_in_scrn = (struct wl_proxy*)ctx_scrn->output;
492         pxy_id = wl_proxy_get_id(pxy_out);
493         pxy_id_in_scrn = wl_proxy_get_id(pxy_out_in_scrn);
494         if (pxy_id == pxy_id_in_scrn) {
495             return ctx_scrn;
496         }
497     }
498     return NULL;
499 }
500
501 static struct screen_context*
502 get_screen_context_by_serverid(struct wayland_context *ctx,
503                                uint32_t id_screen)
504 {
505     struct screen_context *ctx_scrn = NULL;
506
507     wl_list_for_each(ctx_scrn, &ctx->list_screen, link) {
508         if (ctx_scrn->id_from_server == id_screen) {
509             return ctx_scrn;
510         }
511     }
512     return NULL;
513 }
514
515 static void
516 add_orderlayer_to_screen(struct wayland_context *ctx,
517                          struct layer_context *ctx_layer,
518                          struct wl_output* output)
519 {
520     struct screen_context *ctx_scrn = NULL;
521
522     ctx_scrn = get_screen_context_by_output(ctx, output);
523     if (ctx_scrn == NULL) {
524         fprintf(stderr, "failed to add_orderlayer_to_screen\n");
525         return;
526     }
527
528     int found = 0;
529     struct layer_context *layer_link;
530     wl_list_for_each(layer_link, &ctx_scrn->order.list_layer, order.link) {
531         if (layer_link == ctx_layer) {
532             found = 1;
533             break;
534         }
535     }
536
537     if (found == 0) {
538         wl_list_init(&ctx_layer->order.link);
539         wl_list_insert(&ctx_scrn->order.list_layer, &ctx_layer->order.link);
540     }
541 }
542
543 static void
544 remove_orderlayer_from_screen(struct wayland_context *ctx,
545                               struct layer_context *ctx_layer)
546 {
547     struct screen_context *ctx_scrn = NULL;
548     struct layer_context *ctx_orderlayer = NULL;
549     struct layer_context *next = NULL;
550     struct wl_proxy *pxy_layer = NULL;
551     struct wl_proxy *pxy_orderlayer = NULL;
552     uint32_t id_layer = 0;
553     uint32_t id_orderlayer = 0;
554
555     wl_list_for_each(ctx_scrn, &ctx->list_screen, link) {
556         wl_list_for_each_safe(ctx_orderlayer, next,
557                          &ctx_scrn->order.list_layer,
558                          order.link) {
559             pxy_layer = (struct wl_proxy*)ctx_layer->controller;
560             pxy_orderlayer = (struct wl_proxy*)ctx_orderlayer->controller;
561             id_layer = wl_proxy_get_id(pxy_layer);
562             id_orderlayer = wl_proxy_get_id(pxy_orderlayer);
563             if (id_layer == id_orderlayer) {
564                 wl_list_remove(&ctx_orderlayer->order.link);
565                 break;
566             }
567         }
568     }
569 }
570
571 static struct layer_context*
572 get_layer_context_by_controller(struct wayland_context *ctx,
573                                 struct ivi_controller_layer *controller)
574 {
575     struct layer_context *ctx_layer = NULL;
576     struct wl_proxy *pxy_layer = NULL;
577     struct wl_proxy *pxy_addlayer = NULL;
578     uint32_t id_layer = 0;
579     uint32_t id_addlayer = 0;
580
581     wl_list_for_each(ctx_layer, &ctx->list_layer, link) {
582         pxy_layer = (struct wl_proxy*)ctx_layer->controller;
583         pxy_addlayer = (struct wl_proxy*)controller;
584         id_layer = wl_proxy_get_id(pxy_layer);
585         id_addlayer = wl_proxy_get_id(pxy_addlayer);
586         if (id_layer == id_addlayer) {
587             return ctx_layer;
588         }
589     }
590     return NULL;
591 }
592
593 static void
594 controller_layer_listener_visibility_child(void *data,
595                             struct ivi_controller_layer *controller,
596                             int32_t visibility)
597 {
598     struct ilm_control_context *ctx = data;
599     struct layer_context *ctx_layer = NULL;
600
601     ctx_layer = get_layer_context_by_controller(&ctx->child_ctx, controller);
602     if (ctx_layer == NULL) {
603         fprintf(stderr, "Invalid controller_layer in %s\n", __FUNCTION__);
604         return;
605     }
606
607     ctx_layer->prop.visibility = (t_ilm_bool)visibility;
608
609     if (ctx_layer->notification != NULL) {
610         ctx_layer->notification(ctx_layer->id_layer,
611                                 &ctx_layer->prop,
612                                 ILM_NOTIFICATION_VISIBILITY);
613     }
614 }
615
616 static void
617 controller_layer_listener_opacity_child(void *data,
618                        struct ivi_controller_layer *controller,
619                        wl_fixed_t opacity)
620 {
621     struct ilm_control_context *ctx = data;
622     struct layer_context *ctx_layer = NULL;
623
624     ctx_layer = get_layer_context_by_controller(&ctx->child_ctx, controller);
625     if (ctx_layer == NULL) {
626         fprintf(stderr, "Invalid controller_layer in %s\n", __FUNCTION__);
627         return;
628     }
629
630     ctx_layer->prop.opacity = (t_ilm_float)wl_fixed_to_double(opacity);
631
632     if (ctx_layer->notification != NULL) {
633         ctx_layer->notification(ctx_layer->id_layer,
634                                 &ctx_layer->prop,
635                                 ILM_NOTIFICATION_OPACITY);
636     }
637 }
638
639 static void
640 controller_layer_listener_source_rectangle_child(void *data,
641                                 struct ivi_controller_layer *controller,
642                                 int32_t x,
643                                 int32_t y,
644                                 int32_t width,
645                                 int32_t height)
646 {
647     struct ilm_control_context *ctx = data;
648     struct layer_context *ctx_layer = NULL;
649
650     ctx_layer = get_layer_context_by_controller(&ctx->child_ctx, controller);
651     if (ctx_layer == NULL) {
652         fprintf(stderr, "Invalid controller_layer in %s\n", __FUNCTION__);
653         return;
654     }
655
656     ctx_layer->prop.sourceX = (t_ilm_uint)x;
657     ctx_layer->prop.sourceY = (t_ilm_uint)y;
658     ctx_layer->prop.sourceWidth = (t_ilm_uint)width;
659     ctx_layer->prop.sourceHeight = (t_ilm_uint)height;
660     if (ctx_layer->prop.origSourceWidth == 0) {
661         ctx_layer->prop.origSourceWidth = (t_ilm_uint)width;
662     }
663     if (ctx_layer->prop.origSourceHeight == 0) {
664         ctx_layer->prop.origSourceHeight = (t_ilm_uint)height;
665     }
666
667     if (ctx_layer->notification != NULL) {
668         ctx_layer->notification(ctx_layer->id_layer,
669                                 &ctx_layer->prop,
670                                 ILM_NOTIFICATION_SOURCE_RECT);
671     }
672 }
673
674 static void
675 controller_layer_listener_destination_rectangle_child(void *data,
676                                      struct ivi_controller_layer *controller,
677                                      int32_t x,
678                                      int32_t y,
679                                      int32_t width,
680                                      int32_t height)
681 {
682     struct ilm_control_context *ctx = data;
683     struct layer_context *ctx_layer = NULL;
684
685     ctx_layer = get_layer_context_by_controller(&ctx->child_ctx, controller);
686     if (ctx_layer == NULL) {
687         fprintf(stderr, "Invalid controller_layer in %s\n", __FUNCTION__);
688         return;
689     }
690
691     ctx_layer->prop.destX = (t_ilm_uint)x;
692     ctx_layer->prop.destY = (t_ilm_uint)y;
693     ctx_layer->prop.destWidth = (t_ilm_uint)width;
694     ctx_layer->prop.destHeight = (t_ilm_uint)height;
695
696     if (ctx_layer->notification != NULL) {
697         ctx_layer->notification(ctx_layer->id_layer,
698                                 &ctx_layer->prop,
699                                 ILM_NOTIFICATION_DEST_RECT);
700     }
701 }
702
703 static void
704 controller_layer_listener_configuration_child(void *data,
705                           struct ivi_controller_layer *controller,
706                           int32_t width,
707                           int32_t height)
708 {
709     struct ilm_control_context *ctx = data;
710     struct layer_context *ctx_layer = NULL;
711
712     ctx_layer = get_layer_context_by_controller(&ctx->child_ctx, controller);
713     if (ctx_layer == NULL) {
714         fprintf(stderr, "Invalid controller_layer in %s\n", __FUNCTION__);
715         return;
716     }
717
718     ctx_layer->prop.sourceWidth = (t_ilm_uint)width;
719     ctx_layer->prop.sourceHeight = (t_ilm_uint)height;
720 }
721
722 static void
723 controller_layer_listener_orientation_child(void *data,
724                              struct ivi_controller_layer *controller,
725                              int32_t orientation)
726 {
727     ilmOrientation ilmorientation = ILM_ZERO;
728     struct ilm_control_context *ctx = data;
729     struct layer_context *ctx_layer = NULL;
730
731     ctx_layer = get_layer_context_by_controller(&ctx->child_ctx, controller);
732     if (ctx_layer == NULL) {
733         fprintf(stderr, "Invalid controller_layer in %s\n", __FUNCTION__);
734         return;
735     }
736
737     switch(orientation) {
738     case IVI_CONTROLLER_SURFACE_ORIENTATION_0_DEGREES:
739         ilmorientation = ILM_ZERO;
740         break;
741     case IVI_CONTROLLER_SURFACE_ORIENTATION_90_DEGREES:
742         ilmorientation = ILM_NINETY;
743         break;
744     case IVI_CONTROLLER_SURFACE_ORIENTATION_180_DEGREES:
745         ilmorientation = ILM_ONEHUNDREDEIGHTY;
746         break;
747     case IVI_CONTROLLER_SURFACE_ORIENTATION_270_DEGREES:
748         ilmorientation = ILM_TWOHUNDREDSEVENTY;
749         break;
750     default:
751         break;
752     }
753
754     ctx_layer->prop.orientation = ilmorientation;
755
756     if (ctx_layer->notification != NULL) {
757         ctx_layer->notification(ctx_layer->id_layer,
758                                 &ctx_layer->prop,
759                                 ILM_NOTIFICATION_ORIENTATION);
760     }
761 }
762
763 static void
764 controller_layer_listener_screen_child(void *data,
765                                  struct ivi_controller_layer *controller,
766                                  struct wl_output *output)
767 {
768     struct ilm_control_context *ctx = data;
769     struct layer_context *ctx_layer = NULL;
770
771     ctx_layer = get_layer_context_by_controller(&ctx->child_ctx, controller);
772     if (ctx_layer == NULL) {
773         fprintf(stderr, "Invalid controller_layer in %s\n", __FUNCTION__);
774         return;
775     }
776
777     if (output == NULL) {
778         remove_orderlayer_from_screen(&ctx->child_ctx, ctx_layer);
779     } else {
780         add_orderlayer_to_screen(&ctx->child_ctx, ctx_layer, output);
781     }
782 }
783
784 static void
785 controller_layer_listener_destroyed_child(void *data,
786                                     struct ivi_controller_layer *controller)
787 {
788     struct ilm_control_context *ctx = data;
789     struct layer_context *ctx_layer = NULL;
790
791     ctx_layer = get_layer_context_by_controller(&ctx->child_ctx, controller);
792     if (ctx_layer == NULL) {
793         fprintf(stderr, "Invalid controller_layer in %s\n", __FUNCTION__);
794         return;
795     }
796
797     wl_list_remove(&ctx_layer->link);
798     free(ctx_layer);
799 }
800
801 static struct ivi_controller_layer_listener controller_layer_listener_child =
802 {
803     controller_layer_listener_visibility_child,
804     controller_layer_listener_opacity_child,
805     controller_layer_listener_source_rectangle_child,
806     controller_layer_listener_destination_rectangle_child,
807     controller_layer_listener_configuration_child,
808     controller_layer_listener_orientation_child,
809     controller_layer_listener_screen_child,
810     controller_layer_listener_destroyed_child
811 };
812
813 static void
814 controller_layer_listener_visibility_main(void *data,
815                             struct ivi_controller_layer *controller,
816                             int32_t visibility)
817 {
818     struct ilm_control_context *ctx = data;
819     struct layer_context *ctx_layer = NULL;
820
821     ctx_layer = get_layer_context_by_controller(&ctx->main_ctx, controller);
822     if (ctx_layer == NULL) {
823         fprintf(stderr, "Invalid controller_layer in %s\n", __FUNCTION__);
824         return;
825     }
826
827     ctx_layer->prop.visibility = (t_ilm_bool)visibility;
828 }
829
830 static void
831 controller_layer_listener_opacity_main(void *data,
832                        struct ivi_controller_layer *controller,
833                        wl_fixed_t opacity)
834 {
835     struct ilm_control_context *ctx = data;
836     struct layer_context *ctx_layer = NULL;
837
838     ctx_layer = get_layer_context_by_controller(&ctx->main_ctx, controller);
839     if (ctx_layer == NULL) {
840         fprintf(stderr, "Invalid controller_layer in %s\n", __FUNCTION__);
841         return;
842     }
843
844     ctx_layer->prop.opacity = (t_ilm_float)wl_fixed_to_double(opacity);
845 }
846
847 static void
848 controller_layer_listener_source_rectangle_main(void *data,
849                                 struct ivi_controller_layer *controller,
850                                 int32_t x,
851                                 int32_t y,
852                                 int32_t width,
853                                 int32_t height)
854 {
855     struct ilm_control_context *ctx = data;
856     struct layer_context *ctx_layer = NULL;
857
858     ctx_layer = get_layer_context_by_controller(&ctx->main_ctx, controller);
859     if (ctx_layer == NULL) {
860         fprintf(stderr, "Invalid controller_layer in %s\n", __FUNCTION__);
861         return;
862     }
863
864     ctx_layer->prop.sourceX = (t_ilm_uint)x;
865     ctx_layer->prop.sourceY = (t_ilm_uint)y;
866     ctx_layer->prop.sourceWidth = (t_ilm_uint)width;
867     ctx_layer->prop.sourceHeight = (t_ilm_uint)height;
868     if (ctx_layer->prop.origSourceWidth == 0) {
869         ctx_layer->prop.origSourceWidth = (t_ilm_uint)width;
870     }
871     if (ctx_layer->prop.origSourceHeight == 0) {
872         ctx_layer->prop.origSourceHeight = (t_ilm_uint)height;
873     }
874 }
875
876 static void
877 controller_layer_listener_destination_rectangle_main(void *data,
878                                      struct ivi_controller_layer *controller,
879                                      int32_t x,
880                                      int32_t y,
881                                      int32_t width,
882                                      int32_t height)
883 {
884     struct ilm_control_context *ctx = data;
885     struct layer_context *ctx_layer = NULL;
886
887     ctx_layer = get_layer_context_by_controller(&ctx->main_ctx, controller);
888     if (ctx_layer == NULL) {
889         fprintf(stderr, "Invalid controller_layer in %s\n", __FUNCTION__);
890         return;
891     }
892
893     ctx_layer->prop.destX = (t_ilm_uint)x;
894     ctx_layer->prop.destY = (t_ilm_uint)y;
895     ctx_layer->prop.destWidth = (t_ilm_uint)width;
896     ctx_layer->prop.destHeight = (t_ilm_uint)height;
897 }
898
899 static void
900 controller_layer_listener_configuration_main(void *data,
901                           struct ivi_controller_layer *controller,
902                           int32_t width,
903                           int32_t height)
904 {
905     struct ilm_control_context *ctx = data;
906     struct layer_context *ctx_layer = NULL;
907
908     ctx_layer = get_layer_context_by_controller(&ctx->main_ctx, controller);
909     if (ctx_layer == NULL) {
910         fprintf(stderr, "Invalid controller_layer in %s\n", __FUNCTION__);
911         return;
912     }
913
914     ctx_layer->prop.sourceWidth = (t_ilm_uint)width;
915     ctx_layer->prop.sourceHeight = (t_ilm_uint)height;
916 }
917
918 static void
919 controller_layer_listener_orientation_main(void *data,
920                              struct ivi_controller_layer *controller,
921                              int32_t orientation)
922 {
923     ilmOrientation ilmorientation = ILM_ZERO;
924     struct ilm_control_context *ctx = data;
925     struct layer_context *ctx_layer = NULL;
926
927     ctx_layer = get_layer_context_by_controller(&ctx->main_ctx, controller);
928     if (ctx_layer == NULL) {
929         fprintf(stderr, "Invalid controller_layer in %s\n", __FUNCTION__);
930         return;
931     }
932
933     switch(orientation) {
934     case IVI_CONTROLLER_SURFACE_ORIENTATION_0_DEGREES:
935         ilmorientation = ILM_ZERO;
936         break;
937     case IVI_CONTROLLER_SURFACE_ORIENTATION_90_DEGREES:
938         ilmorientation = ILM_NINETY;
939         break;
940     case IVI_CONTROLLER_SURFACE_ORIENTATION_180_DEGREES:
941         ilmorientation = ILM_ONEHUNDREDEIGHTY;
942         break;
943     case IVI_CONTROLLER_SURFACE_ORIENTATION_270_DEGREES:
944         ilmorientation = ILM_TWOHUNDREDSEVENTY;
945         break;
946     default:
947         break;
948     }
949
950     ctx_layer->prop.orientation = ilmorientation;
951 }
952
953 static void
954 controller_layer_listener_screen_main(void *data,
955                                  struct ivi_controller_layer *controller,
956                                  struct wl_output *output)
957 {
958     struct ilm_control_context *ctx = data;
959     struct layer_context *ctx_layer = NULL;
960
961     ctx_layer = get_layer_context_by_controller(&ctx->main_ctx, controller);
962     if (ctx_layer == NULL) {
963         fprintf(stderr, "Invalid controller_layer in %s\n", __FUNCTION__);
964         return;
965     }
966
967     if (output == NULL) {
968         remove_orderlayer_from_screen(&ctx->main_ctx, ctx_layer);
969     } else {
970         add_orderlayer_to_screen(&ctx->main_ctx, ctx_layer, output);
971     }
972 }
973
974 static void
975 controller_layer_listener_destroyed_main(void *data,
976                                     struct ivi_controller_layer *controller)
977 {
978     struct ilm_control_context *ctx = data;
979     struct layer_context *ctx_layer = NULL;
980
981     ctx_layer = get_layer_context_by_controller(&ctx->main_ctx, controller);
982     if (ctx_layer == NULL) {
983         fprintf(stderr, "Invalid controller_layer in %s\n", __FUNCTION__);
984         return;
985     }
986
987     wl_list_remove(&ctx_layer->link);
988     free(ctx_layer);
989 }
990
991 static struct ivi_controller_layer_listener controller_layer_listener_main =
992 {
993     controller_layer_listener_visibility_main,
994     controller_layer_listener_opacity_main,
995     controller_layer_listener_source_rectangle_main,
996     controller_layer_listener_destination_rectangle_main,
997     controller_layer_listener_configuration_main,
998     controller_layer_listener_orientation_main,
999     controller_layer_listener_screen_main,
1000     controller_layer_listener_destroyed_main
1001 };
1002
1003
1004 static struct layer_context*
1005 get_layer_context(struct wayland_context *ctx,
1006                          struct ivi_controller_layer *ivi_layer)
1007 {
1008     struct layer_context *ctx_layer = NULL;
1009     struct wl_proxy *pxy_layer = NULL;
1010     struct wl_proxy *pxy_addlayer = NULL;
1011     uint32_t id_layer = 0;
1012     uint32_t id_addlayer = 0;
1013
1014     wl_list_for_each(ctx_layer, &ctx->list_layer, link) {
1015         pxy_layer = (struct wl_proxy*)ctx_layer->controller;
1016         pxy_addlayer = (struct wl_proxy*)ivi_layer;
1017         id_layer = wl_proxy_get_id(pxy_layer);
1018         id_addlayer = wl_proxy_get_id(pxy_addlayer);
1019         if (id_layer == id_addlayer) {
1020             return ctx_layer;
1021         }
1022     }
1023     return NULL;
1024 }
1025
1026 static void
1027 add_ordersurface_to_layer(struct wayland_context *ctx,
1028                           struct surface_context *ctx_surf,
1029                           struct ivi_controller_layer *layer)
1030 {
1031     struct layer_context *ctx_layer = NULL;
1032     struct surface_context *link = NULL;
1033     int found = 0;
1034
1035     ctx_layer = get_layer_context(ctx, layer);
1036     if (ctx_layer == NULL) {
1037         fprintf(stderr, "failed to add_ordersurface_to_layer\n");
1038         return;
1039     }
1040
1041     wl_list_for_each(link, &ctx_layer->order.list_surface, order.link) {
1042         if (link == ctx_surf) {
1043             found = 1;
1044             break;
1045         }
1046     }
1047
1048     if (found == 0) {
1049         wl_list_init(&ctx_surf->order.link);
1050         wl_list_insert(&ctx_layer->order.list_surface, &ctx_surf->order.link);
1051     }
1052 }
1053
1054 static void
1055 remove_ordersurface_from_layer(struct wayland_context *ctx,
1056                                struct surface_context *ctx_surf)
1057 {
1058     struct layer_context *ctx_layer = NULL;
1059     struct surface_context *ctx_ordersurf = NULL;
1060     struct surface_context *next = NULL;
1061     struct wl_proxy *pxy_surf = NULL;
1062     struct wl_proxy *pxy_ordersurf = NULL;
1063     uint32_t id_surf = 0;
1064     uint32_t id_ordersurf = 0;
1065
1066     wl_list_for_each(ctx_layer, &ctx->list_layer, link) {
1067         wl_list_for_each_safe(ctx_ordersurf, next,
1068                          &ctx_layer->order.list_surface,
1069                          order.link) {
1070             pxy_surf = (struct wl_proxy*)ctx_surf->surface;
1071             pxy_ordersurf = (struct wl_proxy*)ctx_ordersurf->surface;
1072             id_surf = wl_proxy_get_id(pxy_surf);
1073             id_ordersurf = wl_proxy_get_id(pxy_ordersurf);
1074             if (id_surf == id_ordersurf) {
1075                 wl_list_remove(&ctx_ordersurf->order.link);
1076             }
1077         }
1078     }
1079 }
1080
1081 static struct surface_context*
1082 get_surface_context_by_controller(struct wayland_context *ctx,
1083                                   struct ivi_controller_surface *controller)
1084 {
1085     struct surface_context *ctx_surf = NULL;
1086     wl_list_for_each(ctx_surf, &ctx->list_surface, link) {
1087         if (ctx_surf->controller == controller) {
1088             return ctx_surf;
1089         }
1090     }
1091
1092     fprintf(stderr, "failed to get surface context in %s\n", __FUNCTION__);
1093     return NULL;
1094 }
1095
1096 static void
1097 controller_surface_listener_visibility_child(void *data,
1098                             struct ivi_controller_surface *controller,
1099                             int32_t visibility)
1100 {
1101     struct wayland_context *ctx = data;
1102     struct surface_context *ctx_surf = NULL;
1103
1104     ctx_surf = get_surface_context_by_controller(ctx, controller);
1105     if (ctx_surf == NULL) {
1106         fprintf(stderr, "Invalid controller_surface in %s\n", __FUNCTION__);
1107         return;
1108     }
1109
1110     ctx_surf->prop.visibility = (t_ilm_bool)visibility;
1111
1112     if (ctx_surf->notification != NULL) {
1113         ctx_surf->notification(ctx_surf->id_surface,
1114                                 &ctx_surf->prop,
1115                                 ILM_NOTIFICATION_VISIBILITY);
1116     }
1117 }
1118
1119 static void
1120 controller_surface_listener_opacity_child(void *data,
1121                          struct ivi_controller_surface *controller,
1122                          wl_fixed_t opacity)
1123 {
1124     struct wayland_context *ctx = data;
1125     struct surface_context *ctx_surf = NULL;
1126
1127     ctx_surf = get_surface_context_by_controller(ctx, controller);
1128     if (ctx_surf == NULL) {
1129         fprintf(stderr, "Invalid controller_surface in %s\n", __FUNCTION__);
1130         return;
1131     }
1132
1133     ctx_surf->prop.opacity = (t_ilm_float)wl_fixed_to_double(opacity);
1134
1135     if (ctx_surf->notification != NULL) {
1136         ctx_surf->notification(ctx_surf->id_surface,
1137                                 &ctx_surf->prop,
1138                                 ILM_NOTIFICATION_OPACITY);
1139     }
1140 }
1141
1142 static void
1143 controller_surface_listener_configuration_child(void *data,
1144                            struct ivi_controller_surface *controller,
1145                            int32_t width,
1146                            int32_t height)
1147 {
1148     struct wayland_context *ctx = data;
1149     struct surface_context *ctx_surf = NULL;
1150
1151     ctx_surf = get_surface_context_by_controller(ctx, controller);
1152     if (ctx_surf == NULL) {
1153         fprintf(stderr, "Invalid controller_surface in %s\n", __FUNCTION__);
1154         return;
1155     }
1156
1157     if (ctx_surf != NULL) {
1158         ctx_surf->prop.sourceWidth = (t_ilm_uint)width;
1159         ctx_surf->prop.sourceHeight = (t_ilm_uint)height;
1160     }
1161 }
1162
1163 static void
1164 controller_surface_listener_source_rectangle_child(void *data,
1165                                   struct ivi_controller_surface *controller,
1166                                   int32_t x,
1167                                   int32_t y,
1168                                   int32_t width,
1169                                   int32_t height)
1170 {
1171     struct wayland_context *ctx = data;
1172     struct surface_context *ctx_surf = NULL;
1173
1174     ctx_surf = get_surface_context_by_controller(ctx, controller);
1175     if (ctx_surf == NULL) {
1176         fprintf(stderr, "Invalid controller_surface in %s\n", __FUNCTION__);
1177         return;
1178     }
1179
1180     ctx_surf->prop.sourceX = (t_ilm_uint)x;
1181     ctx_surf->prop.sourceY = (t_ilm_uint)y;
1182     ctx_surf->prop.sourceWidth = (t_ilm_uint)width;
1183     ctx_surf->prop.sourceHeight = (t_ilm_uint)height;
1184     if (ctx_surf->prop.origSourceWidth == 0) {
1185         ctx_surf->prop.origSourceWidth = (t_ilm_uint)width;
1186     }
1187     if (ctx_surf->prop.origSourceHeight == 0) {
1188         ctx_surf->prop.origSourceHeight = (t_ilm_uint)height;
1189     }
1190
1191     if (ctx_surf->notification != NULL) {
1192         ctx_surf->notification(ctx_surf->id_surface,
1193                                 &ctx_surf->prop,
1194                                 ILM_NOTIFICATION_SOURCE_RECT);
1195     }
1196 }
1197
1198 static void
1199 controller_surface_listener_destination_rectangle_child(void *data,
1200                    struct ivi_controller_surface *controller,
1201                    int32_t x,
1202                    int32_t y,
1203                    int32_t width,
1204                    int32_t height)
1205 {
1206     struct wayland_context *ctx = data;
1207     struct surface_context *ctx_surf = NULL;
1208
1209     ctx_surf = get_surface_context_by_controller(ctx, controller);
1210     if (ctx_surf == NULL) {
1211         fprintf(stderr, "Invalid controller_surface in %s\n", __FUNCTION__);
1212         return;
1213     }
1214
1215     ctx_surf->prop.destX = (t_ilm_uint)x;
1216     ctx_surf->prop.destY = (t_ilm_uint)y;
1217     ctx_surf->prop.destWidth = (t_ilm_uint)width;
1218     ctx_surf->prop.destHeight = (t_ilm_uint)height;
1219
1220     if (ctx_surf->notification != NULL) {
1221         ctx_surf->notification(ctx_surf->id_surface,
1222                                 &ctx_surf->prop,
1223                                 ILM_NOTIFICATION_DEST_RECT);
1224     }
1225 }
1226
1227 static void
1228 controller_surface_listener_orientation_child(void *data,
1229                              struct ivi_controller_surface *controller,
1230                              int32_t orientation)
1231 {
1232     struct wayland_context *ctx = data;
1233     struct surface_context *ctx_surf = NULL;
1234     ilmOrientation ilmorientation = ILM_ZERO;
1235
1236     ctx_surf = get_surface_context_by_controller(ctx, controller);
1237     if (ctx_surf == NULL) {
1238         fprintf(stderr, "Invalid controller_surface in %s\n", __FUNCTION__);
1239         return;
1240     }
1241
1242     switch (orientation) {
1243     case IVI_CONTROLLER_SURFACE_ORIENTATION_0_DEGREES:
1244         ilmorientation = ILM_ZERO;
1245         break;
1246     case IVI_CONTROLLER_SURFACE_ORIENTATION_90_DEGREES:
1247         ilmorientation = ILM_NINETY;
1248         break;
1249     case IVI_CONTROLLER_SURFACE_ORIENTATION_180_DEGREES:
1250         ilmorientation = ILM_ONEHUNDREDEIGHTY;
1251         break;
1252     case IVI_CONTROLLER_SURFACE_ORIENTATION_270_DEGREES:
1253         ilmorientation = ILM_TWOHUNDREDSEVENTY;
1254         break;
1255     default:
1256         break;
1257     }
1258
1259     ctx_surf->prop.orientation = ilmorientation;
1260
1261     if (ctx_surf->notification != NULL) {
1262         ctx_surf->notification(ctx_surf->id_surface,
1263                                 &ctx_surf->prop,
1264                                 ILM_NOTIFICATION_ORIENTATION);
1265     }
1266 }
1267
1268 static void
1269 controller_surface_listener_pixelformat_child(void *data,
1270                              struct ivi_controller_surface *controller,
1271                              int32_t pixelformat)
1272 {
1273     struct wayland_context *ctx = data;
1274     struct surface_context *ctx_surf = NULL;
1275
1276     ctx_surf = get_surface_context_by_controller(ctx, controller);
1277     if (ctx_surf == NULL) {
1278         fprintf(stderr, "Invalid controller_surface in %s\n", __FUNCTION__);
1279         return;
1280     }
1281
1282     if (ctx_surf != NULL) {
1283         ctx_surf->prop.pixelformat = (t_ilm_uint)pixelformat;
1284     }
1285 }
1286
1287 static void
1288 controller_surface_listener_layer_child(void *data,
1289                                   struct ivi_controller_surface *controller,
1290                                   struct ivi_controller_layer *layer)
1291 {
1292     struct wayland_context *ctx = data;
1293     struct surface_context *ctx_surf = NULL;
1294
1295     ctx_surf = get_surface_context_by_controller(ctx, controller);
1296     if (ctx_surf == NULL) {
1297         fprintf(stderr, "Invalid controller_surface in %s\n", __FUNCTION__);
1298         return;
1299     }
1300
1301     if (layer == NULL) {
1302         remove_ordersurface_from_layer(ctx, ctx_surf);
1303     } else {
1304         add_ordersurface_to_layer(ctx, ctx_surf, layer);
1305     }
1306 }
1307
1308 static void
1309 controller_surface_listener_stats_child(void *data,
1310                                   struct ivi_controller_surface *controller,
1311                                   uint32_t redraw_count,
1312                                   uint32_t frame_count,
1313                                   uint32_t update_count,
1314                                   uint32_t pid,
1315                                   const char *process_name)
1316 {
1317     struct wayland_context *ctx = data;
1318     struct surface_context *ctx_surf = NULL;
1319     (void)process_name;
1320
1321     ctx_surf = get_surface_context_by_controller(ctx, controller);
1322     if (ctx_surf == NULL) {
1323         fprintf(stderr, "Invalid controller_surface in %s\n", __FUNCTION__);
1324
1325     }
1326
1327     if (ctx_surf != NULL) {
1328         ctx_surf->prop.drawCounter = (t_ilm_uint)redraw_count;
1329         ctx_surf->prop.frameCounter = (t_ilm_uint)frame_count;
1330         ctx_surf->prop.updateCounter = (t_ilm_uint)update_count;
1331         ctx_surf->prop.creatorPid = (t_ilm_uint)pid;
1332     }
1333 }
1334
1335 static void
1336 controller_surface_listener_destroyed_child(void *data,
1337                   struct ivi_controller_surface *controller)
1338 {
1339     struct wayland_context *ctx = data;
1340     struct surface_context *ctx_surf = NULL;
1341
1342     ctx_surf = get_surface_context_by_controller(ctx, controller);
1343     if (ctx_surf == NULL) {
1344         fprintf(stderr, "Invalid controller_surface in %s\n", __FUNCTION__);
1345         return;
1346     }
1347
1348     wl_list_remove(&ctx_surf->link);
1349     free(ctx_surf);
1350 }
1351
1352 static void
1353 controller_surface_listener_content_child(void *data,
1354                    struct ivi_controller_surface *controller,
1355                    int32_t content_state)
1356 {
1357     // if client surface (=content) was removed with ilm_surfaceDestroy()
1358     // the expected behavior within ILM API mandates a full removal
1359     // of the surface from the scene. We must remove the controller
1360     // from scene, too.
1361     if (IVI_CONTROLLER_SURFACE_CONTENT_STATE_CONTENT_REMOVED == content_state)
1362     {
1363         struct wayland_context *ctx = data;
1364         struct surface_context *ctx_surf = NULL;
1365
1366         ctx_surf = get_surface_context_by_controller(ctx, controller);
1367         if (ctx_surf == NULL) {
1368             fprintf(stderr, "Invalid controller_surface in %s\n", __FUNCTION__);
1369             return;
1370         }
1371
1372         ivi_controller_surface_destroy(controller, IVI_CONTROLLER_OBJECT_TYPE_SURFACE);
1373
1374         wl_list_remove(&ctx_surf->link);
1375         free(ctx_surf);
1376     }
1377 }
1378
1379 static void
1380 controller_surface_listener_input_focus_child(void *data,
1381                    struct ivi_controller_surface *controller,
1382                    int32_t enabled)
1383 {
1384     (void)data;
1385     (void)controller;
1386     (void)enabled;
1387 }
1388
1389 static struct ivi_controller_surface_listener controller_surface_listener_child =
1390 {
1391     controller_surface_listener_visibility_child,
1392     controller_surface_listener_opacity_child,
1393     controller_surface_listener_source_rectangle_child,
1394     controller_surface_listener_destination_rectangle_child,
1395     controller_surface_listener_configuration_child,
1396     controller_surface_listener_orientation_child,
1397     controller_surface_listener_pixelformat_child,
1398     controller_surface_listener_layer_child,
1399     controller_surface_listener_stats_child,
1400     controller_surface_listener_destroyed_child,
1401     controller_surface_listener_content_child,
1402     controller_surface_listener_input_focus_child
1403 };
1404
1405 static void
1406 controller_surface_listener_visibility_main(void *data,
1407                             struct ivi_controller_surface *controller,
1408                             int32_t visibility)
1409 {
1410     (void)data;
1411     (void)controller;
1412     (void)visibility;
1413 }
1414
1415 static void
1416 controller_surface_listener_opacity_main(void *data,
1417                          struct ivi_controller_surface *controller,
1418                          wl_fixed_t opacity)
1419 {
1420     (void)data;
1421     (void)controller;
1422     (void)opacity;
1423 }
1424
1425 static void
1426 controller_surface_listener_source_rectangle_main(void *data,
1427                                   struct ivi_controller_surface *controller,
1428                                   int32_t x,
1429                                   int32_t y,
1430                                   int32_t width,
1431                                   int32_t height)
1432 {
1433     (void)data;
1434     (void)controller;
1435     (void)x;
1436     (void)y;
1437     (void)width;
1438     (void)height;
1439 }
1440
1441 static void
1442 controller_surface_listener_destination_rectangle_main(void *data,
1443                    struct ivi_controller_surface *controller,
1444                    int32_t x,
1445                    int32_t y,
1446                    int32_t width,
1447                    int32_t height)
1448 {
1449     (void)data;
1450     (void)controller;
1451     (void)x;
1452     (void)y;
1453     (void)width;
1454     (void)height;
1455 }
1456
1457 static void
1458 controller_surface_listener_configuration_main(void *data,
1459                            struct ivi_controller_surface *controller,
1460                            int32_t width,
1461                            int32_t height)
1462 {
1463     (void)data;
1464     (void)controller;
1465     (void)width;
1466     (void)height;
1467 }
1468
1469 static void
1470 controller_surface_listener_orientation_main(void *data,
1471                              struct ivi_controller_surface *controller,
1472                              int32_t orientation)
1473 {
1474     (void)data;
1475     (void)controller;
1476     (void)orientation;
1477 }
1478
1479 static void
1480 controller_surface_listener_pixelformat_main(void *data,
1481                              struct ivi_controller_surface *controller,
1482                              int32_t pixelformat)
1483 {
1484     (void)data;
1485     (void)controller;
1486     (void)pixelformat;
1487 }
1488
1489 static void
1490 controller_surface_listener_layer_main(void *data,
1491                                   struct ivi_controller_surface *controller,
1492                                   struct ivi_controller_layer *layer)
1493 {
1494     struct wayland_context *ctx = data;
1495     struct surface_context *ctx_surf = NULL;
1496
1497     ctx_surf = get_surface_context_by_controller(ctx, controller);
1498     if (ctx_surf == NULL) {
1499         fprintf(stderr, "Invalid controller_surface in %s\n", __FUNCTION__);
1500         return;
1501     }
1502
1503     if (layer == NULL) {
1504         remove_ordersurface_from_layer(ctx, ctx_surf);
1505     } else {
1506         add_ordersurface_to_layer(ctx, ctx_surf, layer);
1507     }
1508 }
1509
1510 static void
1511 controller_surface_listener_stats_main(void *data,
1512                                   struct ivi_controller_surface *controller,
1513                                   uint32_t redraw_count,
1514                                   uint32_t frame_count,
1515                                   uint32_t update_count,
1516                                   uint32_t pid,
1517                                   const char *process_name)
1518 {
1519     (void)data;
1520     (void)controller;
1521     (void)redraw_count;
1522     (void)frame_count;
1523     (void)update_count;
1524     (void)pid;
1525     (void)process_name;
1526 }
1527
1528 static void
1529 controller_surface_listener_destroyed_main(void *data,
1530                   struct ivi_controller_surface *controller)
1531 {
1532     struct wayland_context *ctx = data;
1533     struct surface_context *ctx_surf = NULL;
1534
1535     ctx_surf = get_surface_context_by_controller(ctx, controller);
1536     if (ctx_surf == NULL) {
1537         fprintf(stderr, "Invalid controller_surface in %s\n", __FUNCTION__);
1538         return;
1539     }
1540
1541     wl_list_remove(&ctx_surf->link);
1542     free(ctx_surf);
1543 }
1544
1545 static void
1546 controller_surface_listener_content_main(void *data,
1547                    struct ivi_controller_surface *controller,
1548                    int32_t content_state)
1549 {
1550     // if client surface (=content) was removed with ilm_surfaceDestroy()
1551     // the expected behavior within ILM API mandates a full removal
1552     // of the surface from the scene. We must remove the controller
1553     // from scene, too.
1554     if (IVI_CONTROLLER_SURFACE_CONTENT_STATE_CONTENT_REMOVED == content_state)
1555     {
1556         struct wayland_context *ctx = data;
1557         struct surface_context *ctx_surf = NULL;
1558
1559         ctx_surf = get_surface_context_by_controller(ctx, controller);
1560         if (ctx_surf == NULL) {
1561             fprintf(stderr, "Invalid controller_surface in %s\n", __FUNCTION__);
1562             return;
1563         }
1564
1565         ivi_controller_surface_destroy(controller, IVI_CONTROLLER_OBJECT_TYPE_SURFACE);
1566
1567         wl_list_remove(&ctx_surf->link);
1568         free(ctx_surf);
1569     }
1570 }
1571
1572 static void
1573 controller_surface_listener_input_focus_main(void *data,
1574                    struct ivi_controller_surface *controller,
1575                    int32_t enabled)
1576 {
1577     (void)data;
1578     (void)controller;
1579     (void)enabled;
1580 }
1581
1582 static struct ivi_controller_surface_listener controller_surface_listener_main =
1583 {
1584     controller_surface_listener_visibility_main,
1585     controller_surface_listener_opacity_main,
1586     controller_surface_listener_source_rectangle_main,
1587     controller_surface_listener_destination_rectangle_main,
1588     controller_surface_listener_configuration_main,
1589     controller_surface_listener_orientation_main,
1590     controller_surface_listener_pixelformat_main,
1591     controller_surface_listener_layer_main,
1592     controller_surface_listener_stats_main,
1593     controller_surface_listener_destroyed_main,
1594     controller_surface_listener_content_main,
1595     controller_surface_listener_input_focus_main
1596 };
1597
1598 static void
1599 controller_listener_screen_for_child(void *data,
1600                            struct ivi_controller *ivi_controller,
1601                            uint32_t id_screen,
1602                            struct ivi_controller_screen *controller_screen)
1603 {
1604     (void)data;
1605     (void)ivi_controller;
1606     (void)id_screen;
1607     (void)controller_screen;
1608 }
1609
1610 static void
1611 controller_listener_layer_for_child(void *data,
1612                           struct ivi_controller *controller,
1613                           uint32_t id_layer)
1614 {
1615     (void)data;
1616     (void)controller;
1617     (void)id_layer;
1618 }
1619
1620 static void
1621 controller_listener_surface_for_child(void *data,
1622                             struct ivi_controller *controller,
1623                             uint32_t id_surface)
1624 {
1625     struct wayland_context *ctx = data;
1626     struct surface_context *ctx_surf = NULL;
1627     int32_t is_inside = 0;
1628
1629     is_inside = wayland_controller_is_inside_surface_list(
1630                     &ctx->list_surface, id_surface);
1631
1632     if (is_inside != 0) {
1633         fprintf(stderr, "invalid id_surface in controller_listener_surface\n");
1634         return;
1635     }
1636
1637     ctx_surf = calloc(1, sizeof *ctx_surf);
1638     if (ctx_surf == NULL) {
1639         fprintf(stderr, "Failed to allocate memory for surface_context\n");
1640         return;
1641     }
1642
1643     ctx_surf->controller = ivi_controller_surface_create(
1644                                controller, id_surface);
1645     if (ctx_surf->controller == NULL) {
1646         fprintf(stderr, "Failed to create controller surface\n");
1647         return;
1648     }
1649     ctx_surf->id_surface = id_surface;
1650     ctx_surf->prop.inputDevicesAcceptance = ILM_INPUT_DEVICE_ALL;
1651
1652     wl_list_init(&ctx_surf->link);
1653     wl_list_insert(&ctx->list_surface, &ctx_surf->link);
1654     ivi_controller_surface_add_listener(ctx_surf->controller,
1655                                         &controller_surface_listener_child, ctx);
1656     wl_display_roundtrip(ctx->display);
1657 }
1658
1659 static void
1660 controller_listener_error_for_child(void *data,
1661                           struct ivi_controller *ivi_controller,
1662                           int32_t object_id,
1663                           int32_t object_type,
1664                           int32_t error_code,
1665                           const char *error_text)
1666 {
1667     (void)data;
1668     (void)ivi_controller;
1669     (void)object_id;
1670     (void)object_type;
1671     (void)error_code;
1672     (void)error_text;
1673 }
1674
1675 static struct ivi_controller_listener controller_listener_for_child = {
1676     controller_listener_screen_for_child,
1677     controller_listener_layer_for_child,
1678     controller_listener_surface_for_child,
1679     controller_listener_error_for_child
1680 };
1681
1682 static void
1683 controller_listener_screen_for_main(void *data,
1684                            struct ivi_controller *ivi_controller,
1685                            uint32_t id_screen,
1686                            struct ivi_controller_screen *controller_screen)
1687 {
1688     struct ilm_control_context *ctx = data;
1689     struct screen_context *ctx_screen;
1690     (void)ivi_controller;
1691
1692     ctx_screen = get_screen_context_by_serverid(&ctx->main_ctx, id_screen);
1693     if (ctx_screen == NULL) {
1694         fprintf(stderr, "Failed to allocate memory for screen_context\n");
1695         return;
1696     }
1697     ctx_screen->controller = controller_screen;
1698 }
1699
1700 static void
1701 controller_listener_layer_for_main(void *data,
1702                           struct ivi_controller *controller,
1703                           uint32_t id_layer)
1704 {
1705     (void)data;
1706     (void)controller;
1707     (void)id_layer;
1708 }
1709
1710 static void
1711 controller_listener_surface_for_main(void *data,
1712                             struct ivi_controller *controller,
1713                             uint32_t id_surface)
1714 {
1715     struct ilm_control_context *ctx = data;
1716     struct surface_context *ctx_surf = NULL;
1717     int32_t is_inside = 0;
1718
1719     is_inside = wayland_controller_is_inside_surface_list(
1720                     &ctx->main_ctx.list_surface, id_surface);
1721
1722     if (is_inside != 0) {
1723         fprintf(stderr, "invalid id_surface in controller_listener_surface\n");
1724         return;
1725     }
1726
1727     ctx_surf = calloc(1, sizeof *ctx_surf);
1728     if (ctx_surf == NULL) {
1729         fprintf(stderr, "Failed to allocate memory for surface_context\n");
1730         return;
1731     }
1732
1733     ctx_surf->controller = ivi_controller_surface_create(
1734                                controller, id_surface);
1735     if (ctx_surf->controller == NULL) {
1736         fprintf(stderr, "Failed to create controller surface\n");
1737         return;
1738     }
1739     ctx_surf->id_surface = id_surface;
1740     ctx_surf->prop.inputDevicesAcceptance = ILM_INPUT_DEVICE_ALL;
1741
1742     wl_list_init(&ctx_surf->link);
1743     wl_list_insert(&ctx->main_ctx.list_surface, &ctx_surf->link);
1744     ivi_controller_surface_add_listener(ctx_surf->controller,
1745                                         &controller_surface_listener_main, ctx);
1746 }
1747
1748 static void
1749 controller_listener_error_for_main(void *data,
1750                           struct ivi_controller *ivi_controller,
1751                           int32_t object_id,
1752                           int32_t object_type,
1753                           int32_t error_code,
1754                           const char *error_text)
1755 {
1756     (void)data;
1757     (void)ivi_controller;
1758     (void)object_id;
1759     (void)object_type;
1760     (void)error_code;
1761     (void)error_text;
1762 }
1763
1764 static struct ivi_controller_listener controller_listener_for_main = {
1765     controller_listener_screen_for_main,
1766     controller_listener_layer_for_main,
1767     controller_listener_surface_for_main,
1768     controller_listener_error_for_main
1769 };
1770
1771 static void
1772 registry_handle_control_for_child(void *data,
1773                        struct wl_registry *registry,
1774                        uint32_t name, const char *interface,
1775                        uint32_t version)
1776 {
1777     struct wayland_context *ctx = data;
1778     (void)version;
1779
1780     if (strcmp(interface, "ivi_controller") == 0) {
1781         ctx->controller = wl_registry_bind(registry, name,
1782                                            &ivi_controller_interface, 1);
1783         if (ctx->controller == NULL) {
1784             fprintf(stderr, "Failed to registry bind ivi_controller\n");
1785             return;
1786         }
1787         if (ivi_controller_add_listener(ctx->controller,
1788                                        &controller_listener_for_child,
1789                                        ctx)) {
1790             fprintf(stderr, "Failed to add ivi_controller listener\n");
1791             return;
1792         }
1793     } else if (strcmp(interface, "wl_output") == 0) {
1794         struct screen_context *ctx_scrn = calloc(1, sizeof *ctx_scrn);
1795         if (ctx_scrn == NULL) {
1796             fprintf(stderr, "Failed to allocate memory for screen_context\n");
1797             return;
1798         }
1799         wl_list_init(&ctx_scrn->link);
1800         ctx_scrn->output = wl_registry_bind(registry, name,
1801                                            &wl_output_interface, 1);
1802         if (ctx_scrn->output == NULL) {
1803             free(ctx_scrn);
1804             fprintf(stderr, "Failed to registry bind wl_output\n");
1805             return;
1806         }
1807
1808         if (wl_output_add_listener(ctx_scrn->output,
1809                                    &output_listener,
1810                                    ctx_scrn)) {
1811             free(ctx_scrn);
1812             fprintf(stderr, "Failed to add wl_output listener\n");
1813             return;
1814         }
1815
1816         ctx_scrn->id_screen = ctx->num_screen;
1817         ctx->num_screen++;
1818         wl_list_init(&ctx_scrn->order.list_layer);
1819         wl_list_insert(&ctx->list_screen, &ctx_scrn->link);
1820     }
1821 }
1822
1823 static const struct wl_registry_listener
1824 registry_control_listener_for_child = {
1825     registry_handle_control_for_child,
1826     NULL
1827 };
1828
1829 static void
1830 registry_handle_control_for_main(void *data,
1831                        struct wl_registry *registry,
1832                        uint32_t name, const char *interface,
1833                        uint32_t version)
1834 {
1835     struct ilm_control_context *ctx = data;
1836     (void)version;
1837
1838     if (strcmp(interface, "ivi_controller") == 0) {
1839         ctx->main_ctx.controller = wl_registry_bind(registry, name,
1840                                            &ivi_controller_interface, 1);
1841         if (ctx->main_ctx.controller == NULL) {
1842             fprintf(stderr, "Failed to registry bind ivi_controller\n");
1843             return;
1844         }
1845         if (ivi_controller_add_listener(ctx->main_ctx.controller,
1846                                        &controller_listener_for_main,
1847                                        ctx)) {
1848             fprintf(stderr, "Failed to add ivi_controller listener\n");
1849             return;
1850         }
1851     } else if (strcmp(interface, "wl_output") == 0) {
1852
1853         struct screen_context *ctx_scrn = calloc(1, sizeof *ctx_scrn);
1854         struct wl_proxy *pxy = NULL;
1855
1856         if (ctx_scrn == NULL) {
1857             fprintf(stderr, "Failed to allocate memory for screen_context\n");
1858             return;
1859         }
1860         wl_list_init(&ctx_scrn->link);
1861         ctx_scrn->output = wl_registry_bind(registry, name,
1862                                            &wl_output_interface, 1);
1863         if (ctx_scrn->output == NULL) {
1864             free(ctx_scrn);
1865             fprintf(stderr, "Failed to registry bind wl_output\n");
1866             return;
1867         }
1868
1869         if (wl_output_add_listener(ctx_scrn->output,
1870                                    &output_listener,
1871                                    ctx_scrn)) {
1872             free(ctx_scrn);
1873             fprintf(stderr, "Failed to add wl_output listener\n");
1874             return;
1875         }
1876
1877         pxy = (struct wl_proxy*)ctx_scrn->output;
1878         ctx_scrn->id_from_server = wl_proxy_get_id(pxy);
1879         ctx_scrn->id_screen = ctx->num_screen;
1880         ctx->num_screen++;
1881         ctx_scrn->controller = NULL;
1882         ctx_scrn->prop.layerCount = 0;
1883         ctx_scrn->prop.layerIds = NULL;
1884         ctx_scrn->prop.harwareLayerCount = 0;
1885         ctx_scrn->prop.screenWidth = 0;
1886         ctx_scrn->prop.screenHeight = 0;
1887         wl_list_init(&ctx_scrn->order.list_layer);
1888         wl_list_insert(&ctx->main_ctx.list_screen, &ctx_scrn->link);
1889     }
1890 }
1891
1892 static const struct wl_registry_listener
1893 registry_control_listener_for_main = {
1894     registry_handle_control_for_main,
1895     NULL
1896 };
1897
1898 static struct ilm_control_context ilm_context = {0};
1899
1900 static void
1901 wayland_destroy()
1902 {
1903     struct ilm_control_context *ctx = &ilm_context;
1904     ctx->valid = 0;
1905     void* threadRetVal = NULL;
1906     pthread_cancel(ctx->thread);
1907     if (0 != pthread_join(ctx->thread, &threadRetVal)) {
1908         fprintf(stderr, "failed to join control thread\n");
1909     }
1910 }
1911
1912 static void
1913 destroy_control_resources()
1914 {
1915     struct ilm_control_context *ctx = &ilm_context;
1916     struct screen_context *ctx_scrn;
1917     struct screen_context *next;
1918
1919     wl_list_for_each_safe(ctx_scrn, next, &ctx->child_ctx.list_screen, link) {
1920         if (ctx_scrn->output != NULL) {
1921             wl_list_remove(&ctx_scrn->link);
1922             wl_output_destroy(ctx_scrn->output);
1923             free(ctx_scrn);
1924         }
1925     }
1926     if (ctx->child_ctx.controller != NULL) {
1927         ivi_controller_destroy(ctx->child_ctx.controller);
1928         ctx->child_ctx.controller = NULL;
1929     }
1930
1931     wl_display_flush(ctx->child_ctx.display);
1932
1933     if (ctx->child_ctx.display != NULL) {
1934         wl_display_disconnect(ctx->child_ctx.display);
1935         ctx->child_ctx.display = NULL;
1936     }
1937
1938     if (0 != pthread_mutex_destroy(&ctx->mutex)) {
1939         fprintf(stderr, "failed to destroy pthread_mutex\n");
1940     }
1941 }
1942
1943 static void
1944 wayland_context_init(struct wayland_context *ctx)
1945 {
1946     wl_list_init(&ctx->list_screen);
1947     wl_list_init(&ctx->list_layer);
1948     wl_list_init(&ctx->list_surface);
1949 }
1950
1951 static ilmErrorTypes
1952 wayland_init(t_ilm_nativedisplay nativedisplay)
1953 {
1954     struct ilm_control_context *ctx = &ilm_context;
1955
1956     if (nativedisplay == 0) {
1957         return ILM_ERROR_INVALID_ARGUMENTS;
1958     }
1959
1960     memset(ctx, 0, sizeof *ctx);
1961
1962     ctx->main_ctx.display = (struct wl_display*)nativedisplay;
1963
1964     int ans = 0;
1965     ans = pthread_mutex_init(&ctx->mutex, NULL);
1966     if (ans != 0) {
1967         fprintf(stderr, "failed to initialize pthread_mutex\n");
1968     }
1969
1970     ctx->internal_id_surface = 0;
1971     ctx->num_screen = 0;
1972
1973     wayland_context_init(&ctx->main_ctx);
1974     wayland_context_init(&ctx->child_ctx);
1975
1976     return ILM_SUCCESS;
1977 }
1978
1979 static void*
1980 control_thread(void *p_ret)
1981 {
1982     struct ilm_control_context *ctx = &ilm_context;
1983     struct wayland_context *child_ctx = &ctx->child_ctx;
1984     (void)p_ret;
1985
1986     ctx->num_screen = 0;
1987     wl_list_init(&child_ctx->list_screen);
1988     wl_list_init(&child_ctx->list_layer);
1989     wl_list_init(&child_ctx->list_surface);
1990
1991     child_ctx->display = wl_display_connect(NULL);
1992     if (child_ctx->display == NULL) {
1993         fprintf(stderr, "Failed to connect display in libilmCommon\n");
1994         return NULL;
1995     }
1996
1997     child_ctx->registry = wl_display_get_registry(child_ctx->display);
1998     if (child_ctx->registry == NULL) {
1999         fprintf(stderr, "Failed to get registry\n");
2000         return NULL;
2001     }
2002     if (wl_registry_add_listener(child_ctx->registry,
2003             &registry_control_listener_for_child, &ctx->child_ctx)) {
2004         fprintf(stderr, "Failed to add registry listener\n");
2005         return NULL;
2006     }
2007
2008     wl_display_dispatch(child_ctx->display);
2009     wl_display_roundtrip(child_ctx->display);
2010
2011     ctx->valid = 1;
2012     while (0 < ctx->valid)
2013     {
2014         wl_display_dispatch(child_ctx->display);
2015     }
2016
2017     destroy_control_resources();
2018
2019     return NULL;
2020 }
2021
2022 static void
2023 init_control()
2024 {
2025     struct ilm_control_context *ctx = &ilm_context;
2026     struct wayland_context *main_ctx = &ctx->main_ctx;
2027     int wait_count = 0;
2028     int ret = 0;
2029     pthread_attr_t thread_attrs;
2030
2031     wl_list_init(&ctx->list_nativehandle);
2032
2033     pthread_attr_init(&thread_attrs);
2034     pthread_attr_setdetachstate(&thread_attrs, PTHREAD_CREATE_JOINABLE);
2035     ret = pthread_create(&ctx->thread, &thread_attrs,
2036                          control_thread, NULL);
2037     if (ret != 0) {
2038         fprintf(stderr, "Failed to start internal receive \
2039                 thread. returned %d\n", ret);
2040         return;
2041     }
2042
2043     /* registry_add_listener for request by ivi-controller */
2044     main_ctx->registry = wl_display_get_registry(main_ctx->display);
2045     if (main_ctx->registry == NULL) {
2046         fprintf(stderr, "Failed to get registry\n");
2047         return;
2048     }
2049     if (wl_registry_add_listener(main_ctx->registry,
2050                              &registry_control_listener_for_main, ctx)) {
2051         fprintf(stderr, "Failed to add registry listener\n");
2052         return;
2053     }
2054
2055     wl_display_dispatch(main_ctx->display);
2056     wl_display_roundtrip(main_ctx->display);
2057
2058     ctx->valid = 1;
2059
2060     /* Wait for bind to wayland interface */
2061     do {
2062         if (ctx->child_ctx.controller != NULL) {
2063             break;
2064         }
2065         usleep(10000);
2066     } while (++wait_count <= 1000); /* wait until 10sec */
2067
2068     if ((ctx->child_ctx.display == NULL) || (ctx->child_ctx.controller == NULL)) {
2069         fprintf(stderr, "Failed to connect display\n");
2070         return;
2071     }
2072 }
2073
2074 static struct ilm_control_context*
2075 get_instance()
2076 {
2077     struct ilm_control_context *ctx = &ilm_context;
2078     if (ctx->valid == 0) {
2079         init_control();
2080     }
2081
2082     if (ctx->valid < 0) {
2083         exit(0);
2084     }
2085
2086     usleep(20000);
2087     wl_display_roundtrip(ctx->main_ctx.display);
2088     return ctx;
2089 }
2090
2091 static uint32_t
2092 gen_layer_id(struct ilm_control_context *ctx)
2093 {
2094     struct layer_context *ctx_layer = NULL;
2095     do {
2096         int found = 0;
2097         if (wl_list_length(&ctx->main_ctx.list_layer) == 0) {
2098             ctx->internal_id_layer++;
2099             return ctx->internal_id_layer;
2100         }
2101         wl_list_for_each(ctx_layer, &ctx->main_ctx.list_layer, link) {
2102             if (ctx_layer->id_layer == ctx->internal_id_layer) {
2103                 found = 1;
2104                 break;
2105             }
2106
2107             if (found == 0) {
2108                 return ctx->internal_id_layer;
2109             }
2110         }
2111         ctx->internal_id_layer++;
2112     } while(1);
2113 }
2114
2115 static struct surface_context*
2116 get_surface_context(struct wayland_context *ctx,
2117                           uint32_t id_surface)
2118 {
2119     struct surface_context *ctx_surf = NULL;
2120
2121     if (ctx->controller == NULL) {
2122         fprintf(stderr, "controller is not initialized in ilmControl\n");
2123         return NULL;
2124     }
2125
2126     wl_list_for_each(ctx_surf, &ctx->list_surface, link) {
2127         if (ctx_surf->id_surface == id_surface) {
2128             return ctx_surf;
2129         }
2130     }
2131
2132     fprintf(stderr, "failed to get surface context in ilmControl\n");
2133     return NULL;
2134 }
2135
2136 static struct screen_context*
2137 get_screen_context_by_id(struct wayland_context *ctx, uint32_t id_screen)
2138 {
2139     struct screen_context *ctx_scrn = NULL;
2140
2141     if (ctx->controller == NULL) {
2142         fprintf(stderr, "get_screen_context_by_id: controller is NULL\n");
2143         return NULL;
2144     }
2145
2146     wl_list_for_each(ctx_scrn, &ctx->list_screen, link) {
2147         if (ctx_scrn->id_screen == id_screen) {
2148             return ctx_scrn;
2149         }
2150     }
2151     return NULL;
2152 }
2153
2154 static ilmErrorTypes
2155 wayland_getPropertiesOfLayer(t_ilm_uint layerID,
2156                          struct ilmLayerProperties* pLayerProperties)
2157 {
2158     ilmErrorTypes returnValue = ILM_FAILED;
2159     struct ilm_control_context *ctx = get_instance();
2160     struct layer_context *ctx_layer = NULL;
2161
2162     if (pLayerProperties != NULL) {
2163
2164         ctx_layer = (struct layer_context*)
2165                     wayland_controller_get_layer_context(
2166                         &ctx->main_ctx, (uint32_t)layerID);
2167
2168         if (ctx_layer != NULL) {
2169             *pLayerProperties = ctx_layer->prop;
2170             returnValue = ILM_SUCCESS;
2171         }
2172     }
2173
2174     return returnValue;
2175 }
2176
2177 static void
2178 create_layerids(struct screen_context *ctx_screen,
2179                 t_ilm_layer **layer_ids, t_ilm_uint *layer_count)
2180 {
2181     struct layer_context *ctx_layer = NULL;
2182     t_ilm_layer *ids = NULL;
2183
2184     *layer_count = wl_list_length(&ctx_screen->order.list_layer);
2185     if (*layer_count == 0) {
2186         *layer_ids = NULL;
2187         return;
2188     }
2189
2190     *layer_ids = malloc(*layer_count * sizeof(t_ilm_layer));
2191     if (*layer_ids == NULL) {
2192         fprintf(stderr, "memory insufficient for layerids\n");
2193         *layer_count = 0;
2194         return;
2195     }
2196
2197     ids = *layer_ids;
2198     wl_list_for_each(ctx_layer, &ctx_screen->order.list_layer, order.link) {
2199         *ids = (t_ilm_layer)ctx_layer->id_layer;
2200         ids++;
2201     }
2202 }
2203
2204 static ilmErrorTypes
2205 wayland_getPropertiesOfScreen(t_ilm_display screenID,
2206                               struct ilmScreenProperties* pScreenProperties)
2207 {
2208     ilmErrorTypes returnValue = ILM_FAILED;
2209     struct ilm_control_context *ctx = get_instance();
2210
2211     if (pScreenProperties != NULL) {
2212         struct screen_context *ctx_screen = NULL;
2213         ctx_screen = get_screen_context_by_id(&ctx->main_ctx, (uint32_t)screenID);
2214         if (ctx_screen != NULL) {
2215             *pScreenProperties = ctx_screen->prop;
2216             create_layerids(ctx_screen, &pScreenProperties->layerIds,
2217                                         &pScreenProperties->layerCount);
2218             returnValue = ILM_SUCCESS;
2219         }
2220     }
2221     else {
2222         pScreenProperties->layerCount = 0;
2223         pScreenProperties->harwareLayerCount = 0;
2224         pScreenProperties->layerIds = NULL;
2225         pScreenProperties->screenWidth = 0;
2226         pScreenProperties->screenHeight = 0;
2227     }
2228
2229     return returnValue;
2230 }
2231
2232 static ilmErrorTypes
2233 wayland_getNumberOfHardwareLayers(t_ilm_uint screenID,
2234                                   t_ilm_uint* pNumberOfHardwareLayers)
2235 {
2236     (void)screenID;
2237     /* Not supported */
2238     if (pNumberOfHardwareLayers != NULL) {
2239         *pNumberOfHardwareLayers = 0;
2240         return ILM_SUCCESS;
2241     } else {
2242         return ILM_FAILED;
2243     }
2244 }
2245
2246 static ilmErrorTypes
2247 wayland_getScreenIDs(t_ilm_uint* pNumberOfIDs, t_ilm_uint** ppIDs)
2248 {
2249     ilmErrorTypes returnValue = ILM_FAILED;
2250     struct ilm_control_context *ctx = get_instance();
2251
2252     if ((pNumberOfIDs != NULL) && (ppIDs != NULL)) {
2253         struct screen_context *ctx_scrn = NULL;
2254         t_ilm_uint length = wl_list_length(&ctx->main_ctx.list_screen);
2255         *pNumberOfIDs = 0;
2256
2257         *ppIDs = (t_ilm_uint*)malloc(length * sizeof *ppIDs);
2258         if (*ppIDs != NULL) {
2259             t_ilm_uint* ids = *ppIDs;
2260             wl_list_for_each(ctx_scrn, &ctx->main_ctx.list_screen, link) {
2261                 *ids = ctx_scrn->id_screen;
2262                 ids++;
2263             }
2264             *pNumberOfIDs = length;
2265
2266             returnValue = ILM_SUCCESS;
2267         }
2268     }
2269
2270     return returnValue;
2271 }
2272
2273 static ilmErrorTypes
2274 wayland_getLayerIDs(t_ilm_int* pLength, t_ilm_layer** ppArray)
2275 {
2276     ilmErrorTypes returnValue = ILM_FAILED;
2277     struct ilm_control_context *ctx = get_instance();
2278
2279     if ((pLength != NULL) && (ppArray != NULL)) {
2280         struct layer_context *ctx_layer = NULL;
2281         t_ilm_uint length = wl_list_length(&ctx->main_ctx.list_layer);
2282         *pLength = 0;
2283
2284         *ppArray = (t_ilm_layer*)malloc(length * sizeof *ppArray);
2285         if (*ppArray != NULL) {
2286             // compositor sends layers in opposite order
2287             // write ids from back to front to turn them around
2288             t_ilm_layer* ids = &((*ppArray)[length - 1]);
2289             wl_list_for_each(ctx_layer, &ctx->main_ctx.list_layer, link)
2290             {
2291                 *ids = ctx_layer->id_layer;
2292                 --ids;
2293             }
2294             *pLength = length;
2295
2296             returnValue = ILM_SUCCESS;
2297         }
2298     }
2299
2300     return returnValue;
2301 }
2302
2303 static ilmErrorTypes
2304 wayland_getLayerIDsOnScreen(t_ilm_uint screenId,
2305                             t_ilm_int* pLength,
2306                             t_ilm_layer** ppArray)
2307 {
2308     ilmErrorTypes returnValue = ILM_FAILED;
2309     struct ilm_control_context *ctx = get_instance();
2310
2311     if ((pLength != NULL) && (ppArray != NULL)) {
2312         struct screen_context *ctx_screen = NULL;
2313         ctx_screen = get_screen_context_by_id(&ctx->main_ctx, screenId);
2314         if (ctx_screen != NULL) {
2315             struct layer_context *ctx_layer = NULL;
2316             t_ilm_int length = wl_list_length(&ctx_screen->order.list_layer);
2317
2318             if (0 < length)
2319             {
2320                 *ppArray = (t_ilm_layer*)malloc(length * sizeof *ppArray);
2321                 if (*ppArray != NULL) {
2322                     // compositor sends layers in opposite order
2323                     // write ids from back to front to turn them around
2324                     t_ilm_layer* ids = &((*ppArray)[length - 1]);
2325                     wl_list_for_each(ctx_layer, &ctx->main_ctx.list_layer, link)
2326                     {
2327                         *ids = ctx_layer->id_layer;
2328                         --ids;
2329                     }
2330
2331                 }
2332             }
2333             else
2334             {
2335                 *ppArray = NULL;
2336             }
2337
2338             *pLength = length;
2339             returnValue = ILM_SUCCESS;
2340         }
2341     }
2342
2343     return returnValue;
2344 }
2345
2346 static ilmErrorTypes
2347 wayland_getSurfaceIDs(t_ilm_int* pLength, t_ilm_surface** ppArray)
2348 {
2349     ilmErrorTypes returnValue = ILM_FAILED;
2350     struct ilm_control_context *ctx = get_instance();
2351
2352     if ((pLength != NULL) && (ppArray != NULL)) {
2353         struct surface_context *ctx_surf = NULL;
2354         t_ilm_uint length = wl_list_length(&ctx->main_ctx.list_surface);
2355         *pLength = 0;
2356
2357         *ppArray = (t_ilm_surface*)malloc(length * sizeof *ppArray);
2358         if (*ppArray != NULL) {
2359             t_ilm_surface* ids = *ppArray;
2360             wl_list_for_each(ctx_surf, &ctx->main_ctx.list_surface, link) {
2361                 *ids = ctx_surf->id_surface;
2362                 ids++;
2363             }
2364             *pLength = length;
2365
2366             returnValue = ILM_SUCCESS;
2367         }
2368     }
2369
2370     return returnValue;
2371 }
2372
2373 static ilmErrorTypes
2374 wayland_getSurfaceIDsOnLayer(t_ilm_layer layer,
2375                              t_ilm_int* pLength,
2376                              t_ilm_surface** ppArray)
2377 {
2378     struct ilm_control_context *ctx = get_instance();
2379     struct layer_context *ctx_layer = NULL;
2380     struct surface_context *ctx_surf = NULL;
2381     t_ilm_uint length = 0;
2382     t_ilm_surface* ids = NULL;
2383
2384     if ((pLength == NULL) || (ppArray == NULL)) {
2385         return ILM_FAILED;
2386     }
2387
2388     ctx_layer = (struct layer_context*)wayland_controller_get_layer_context(
2389                     &ctx->main_ctx, (uint32_t)layer);
2390
2391     if (ctx_layer == NULL) {
2392         return ILM_FAILED;
2393     }
2394
2395     length = wl_list_length(&ctx_layer->order.list_surface);
2396     *ppArray = (t_ilm_surface*)malloc(length * sizeof *ppArray);
2397     if (*ppArray == NULL) {
2398         return ILM_FAILED;
2399     }
2400
2401     ids = *ppArray;
2402     wl_list_for_each(ctx_surf, &ctx_layer->order.list_surface, order.link) {
2403         *ids = (t_ilm_surface)ctx_surf->id_surface;
2404         ids++;
2405     }
2406     *pLength = length;
2407
2408     return ILM_SUCCESS;
2409 }
2410
2411 static ilmErrorTypes
2412 wayland_layerCreateWithDimension(t_ilm_layer* pLayerId,
2413                                  t_ilm_uint width,
2414                                  t_ilm_uint height)
2415 {
2416     ilmErrorTypes returnValue = ILM_FAILED;
2417     struct ilm_control_context *ctx = get_instance();
2418     uint32_t layerid = 0;
2419     struct layer_context *ctx_layer_main = NULL;
2420     struct layer_context *ctx_layer_child = NULL;
2421     int32_t is_inside = 0;
2422
2423     do {
2424         if (pLayerId == NULL) {
2425             break;
2426         }
2427
2428         if (*pLayerId != INVALID_ID) {
2429             /* Return failed, if layerid is already inside list_layer */
2430             is_inside = wayland_controller_is_inside_layer_list(
2431                             &ctx->main_ctx.list_layer, *pLayerId);
2432             if (0 != is_inside) {
2433                 fprintf(stderr, "layerid=%d is already used.\n", *pLayerId);
2434                 break;
2435             }
2436             layerid = *pLayerId;
2437         }
2438         else {
2439             /* Generate ID, if layerid is INVALID_ID */
2440             layerid = gen_layer_id(ctx);
2441             *pLayerId = layerid;
2442         }
2443
2444         // For main context
2445         ctx_layer_main = calloc(1, sizeof *ctx_layer_main);
2446         if (ctx_layer_main == NULL) {
2447             fprintf(stderr, "Failed to allocate memory for layer_context\n");
2448             break;
2449         }
2450
2451         ctx_layer_main->controller = ivi_controller_layer_create(
2452                                     ctx->main_ctx.controller,
2453                                     layerid, width, height);
2454         if (ctx_layer_main->controller == NULL) {
2455             fprintf(stderr, "Failed to create layer\n");
2456             free(ctx_layer_main);
2457             break;
2458         }
2459         ctx_layer_main->id_layer = layerid;
2460
2461         wl_list_init(&ctx_layer_main->link);
2462         wl_list_insert(&ctx->main_ctx.list_layer, &ctx_layer_main->link);
2463         wl_list_init(&ctx_layer_main->order.link);
2464         wl_list_init(&ctx_layer_main->order.list_surface);
2465
2466         ivi_controller_layer_add_listener(ctx_layer_main->controller,
2467                                       &controller_layer_listener_main, ctx);
2468
2469         // For child context
2470         ctx_layer_child = calloc(1, sizeof *ctx_layer_child);
2471         if (ctx_layer_child == NULL) {
2472             fprintf(stderr, "Failed to allocate memory for layer_context\n");
2473             break;
2474         }
2475
2476         ctx_layer_child->controller = ivi_controller_layer_create(
2477                                     ctx->child_ctx.controller,
2478                                     layerid, width, height);
2479         if (ctx_layer_child->controller == NULL) {
2480             fprintf(stderr, "Failed to create layer\n");
2481             free(ctx_layer_child);
2482             break;
2483         }
2484         ctx_layer_child->id_layer = layerid;
2485
2486         wl_list_init(&ctx_layer_child->link);
2487         wl_list_insert(&ctx->child_ctx.list_layer, &ctx_layer_child->link);
2488         wl_list_init(&ctx_layer_child->order.link);
2489         wl_list_init(&ctx_layer_child->order.list_surface);
2490
2491         ivi_controller_layer_add_listener(ctx_layer_child->controller,
2492                                       &controller_layer_listener_child, ctx);
2493
2494         returnValue = ILM_SUCCESS;
2495     } while(0);
2496
2497     return returnValue;
2498 }
2499
2500 static ilmErrorTypes
2501 wayland_layerRemove(t_ilm_layer layerId)
2502 {
2503     ilmErrorTypes returnValue = ILM_FAILED;
2504     struct ilm_control_context *ctx = get_instance();
2505     struct layer_context *ctx_layer = NULL;
2506     struct layer_context *ctx_next = NULL;
2507
2508     wl_list_for_each_safe(ctx_layer, ctx_next,
2509             &ctx->main_ctx.list_layer, link) {
2510         if (ctx_layer->id_layer == layerId) {
2511             ivi_controller_layer_destroy(ctx_layer->controller,
2512                 IVI_CONTROLLER_OBJECT_TYPE_LAYER);
2513
2514             wl_list_remove(&ctx_layer->link);
2515             free(ctx_layer);
2516
2517             returnValue = ILM_SUCCESS;
2518             break;
2519         }
2520     }
2521
2522     return returnValue;
2523 }
2524
2525 static ilmErrorTypes
2526 wayland_layerGetType(t_ilm_layer layerId, ilmLayerType* pLayerType)
2527 {
2528     (void)layerId;
2529     /* Not supported */
2530     if (pLayerType != NULL) {
2531         *pLayerType = ILM_LAYERTYPE_SOFTWARE2D;
2532     }
2533     return ILM_SUCCESS;
2534 }
2535
2536 static ilmErrorTypes
2537 wayland_layerSetVisibility(t_ilm_layer layerId, t_ilm_bool newVisibility)
2538 {
2539     ilmErrorTypes returnValue = ILM_FAILED;
2540     struct ilm_control_context *ctx = get_instance();
2541     struct layer_context *ctx_layer = NULL;
2542
2543     ctx_layer = (struct layer_context*)wayland_controller_get_layer_context(
2544                     &ctx->main_ctx, (uint32_t)layerId);
2545
2546     if (ctx_layer != NULL) {
2547         uint32_t visibility = 0;
2548         if (newVisibility == ILM_TRUE) {
2549             visibility = 1;
2550         }
2551         ivi_controller_layer_set_visibility(ctx_layer->controller,
2552                                             visibility);
2553         returnValue = ILM_SUCCESS;
2554     }
2555
2556     return returnValue;
2557 }
2558
2559 static ilmErrorTypes
2560 wayland_layerGetVisibility(t_ilm_layer layerId, t_ilm_bool *pVisibility)
2561 {
2562     ilmErrorTypes returnValue = ILM_FAILED;
2563     struct ilm_control_context *ctx = get_instance();
2564
2565     if (pVisibility != NULL) {
2566         struct layer_context *ctx_layer = NULL;
2567
2568         ctx_layer = (struct layer_context*)
2569                     wayland_controller_get_layer_context(
2570                         &ctx->main_ctx, (uint32_t)layerId);
2571
2572         if (ctx_layer != NULL) {
2573             *pVisibility = ctx_layer->prop.visibility;
2574             returnValue = ILM_SUCCESS;
2575         }
2576     }
2577
2578     return returnValue;
2579 }
2580
2581 static ilmErrorTypes
2582 wayland_layerSetOpacity(t_ilm_layer layerId, t_ilm_float opacity)
2583 {
2584     ilmErrorTypes returnValue = ILM_FAILED;
2585     struct ilm_control_context *ctx = get_instance();
2586     struct layer_context *ctx_layer = NULL;
2587
2588     ctx_layer = (struct layer_context*)wayland_controller_get_layer_context(
2589                     &ctx->main_ctx, (uint32_t)layerId);
2590
2591     if (ctx_layer != NULL) {
2592         wl_fixed_t opacity_fixed = wl_fixed_from_double((double)opacity);
2593         ivi_controller_layer_set_opacity(ctx_layer->controller,
2594                                          opacity_fixed);
2595         returnValue = ILM_SUCCESS;
2596     }
2597
2598     return returnValue;
2599 }
2600
2601 static ilmErrorTypes
2602 wayland_layerGetOpacity(t_ilm_layer layerId, t_ilm_float *pOpacity)
2603 {
2604     ilmErrorTypes returnValue = ILM_FAILED;
2605     struct ilm_control_context *ctx = get_instance();
2606
2607     if (pOpacity != NULL) {
2608         struct layer_context *ctx_layer = NULL;
2609
2610         ctx_layer = (struct layer_context*)
2611                     wayland_controller_get_layer_context(
2612                         &ctx->main_ctx, (uint32_t)layerId);
2613
2614         if (ctx_layer != NULL) {
2615             *pOpacity = ctx_layer->prop.opacity;
2616             returnValue = ILM_SUCCESS;
2617         }
2618     }
2619
2620     return returnValue;
2621 }
2622
2623 static ilmErrorTypes
2624 wayland_layerSetSourceRectangle(t_ilm_layer layerId,
2625                                 t_ilm_uint x, t_ilm_uint y,
2626                                 t_ilm_uint width, t_ilm_uint height)
2627 {
2628     ilmErrorTypes returnValue = ILM_FAILED;
2629     struct ilm_control_context *ctx = get_instance();
2630     struct layer_context *ctx_layer = NULL;
2631
2632     ctx_layer = (struct layer_context*)wayland_controller_get_layer_context(
2633                     &ctx->main_ctx, (uint32_t)layerId);
2634
2635     if (ctx_layer != NULL) {
2636         ivi_controller_layer_set_source_rectangle(ctx_layer->controller,
2637                                                   (uint32_t)x,
2638                                                   (uint32_t)y,
2639                                                   (uint32_t)width,
2640                                                   (uint32_t)height);
2641         returnValue = ILM_SUCCESS;
2642     }
2643
2644     return returnValue;
2645 }
2646
2647 static ilmErrorTypes
2648 wayland_layerSetDestinationRectangle(t_ilm_layer layerId,
2649                                  t_ilm_int x, t_ilm_int y,
2650                                  t_ilm_int width, t_ilm_int height)
2651 {
2652     ilmErrorTypes returnValue = ILM_FAILED;
2653     struct ilm_control_context *ctx = get_instance();
2654     struct layer_context *ctx_layer = NULL;
2655
2656     ctx_layer = (struct layer_context*)wayland_controller_get_layer_context(
2657                     &ctx->main_ctx, (uint32_t)layerId);
2658     if (ctx_layer != NULL) {
2659         ivi_controller_layer_set_destination_rectangle(
2660                                          ctx_layer->controller,
2661                                          (uint32_t)x, (uint32_t)y,
2662                                          (uint32_t)width,
2663                                          (uint32_t)height);
2664         returnValue = ILM_SUCCESS;
2665     }
2666
2667     return returnValue;
2668 }
2669
2670 static ilmErrorTypes
2671 wayland_layerGetDimension(t_ilm_layer layerId, t_ilm_uint *pDimension)
2672 {
2673     ilmErrorTypes returnValue = ILM_FAILED;
2674     struct ilm_control_context *ctx = get_instance();
2675     struct layer_context *ctx_layer = NULL;
2676
2677     if (pDimension != NULL) {
2678         ctx_layer = (struct layer_context*)
2679                     wayland_controller_get_layer_context(
2680                         &ctx->main_ctx, (uint32_t)layerId);
2681         if (ctx_layer != NULL) {
2682             *pDimension = ctx_layer->prop.destWidth;
2683             *(pDimension + 1) = ctx_layer->prop.destHeight;
2684             returnValue = ILM_SUCCESS;
2685         }
2686     }
2687
2688     return returnValue;
2689 }
2690
2691 static ilmErrorTypes
2692 wayland_layerSetDimension(t_ilm_layer layerId, t_ilm_uint *pDimension)
2693 {
2694     ilmErrorTypes returnValue = ILM_FAILED;
2695     struct ilm_control_context *ctx = get_instance();
2696     struct layer_context *ctx_layer = NULL;
2697
2698     if (pDimension != NULL) {
2699         ctx_layer = (struct layer_context*)
2700                     wayland_controller_get_layer_context(
2701                         &ctx->main_ctx, (uint32_t)layerId);
2702         if (ctx_layer != NULL) {
2703             ivi_controller_layer_set_destination_rectangle(
2704                 ctx_layer->controller,
2705                 ctx_layer->prop.destX, ctx_layer->prop.destY,
2706                 (int32_t)*pDimension, (int32_t)*(pDimension + 1));
2707             returnValue = ILM_SUCCESS;
2708         }
2709     }
2710
2711     return returnValue;
2712 }
2713
2714 static ilmErrorTypes
2715 wayland_layerGetPosition(t_ilm_layer layerId, t_ilm_uint *pPosition)
2716 {
2717     ilmErrorTypes returnValue = ILM_FAILED;
2718     struct ilm_control_context *ctx = get_instance();
2719     struct layer_context *ctx_layer = NULL;
2720
2721     if (pPosition != NULL) {
2722         ctx_layer = (struct layer_context*)
2723                     wayland_controller_get_layer_context(
2724                         &ctx->main_ctx, (uint32_t)layerId);
2725         if (ctx_layer != NULL) {
2726             *pPosition = ctx_layer->prop.destX;
2727             *(pPosition + 1) = ctx_layer->prop.destY;
2728             returnValue = ILM_SUCCESS;
2729         }
2730     }
2731
2732     return returnValue;
2733 }
2734
2735 static ilmErrorTypes
2736 wayland_layerSetPosition(t_ilm_layer layerId, t_ilm_uint *pPosition)
2737 {
2738     ilmErrorTypes returnValue = ILM_FAILED;
2739     struct ilm_control_context *ctx = get_instance();
2740     struct layer_context *ctx_layer = NULL;
2741
2742     if (pPosition != NULL) {
2743         ctx_layer = (struct layer_context*)
2744                     wayland_controller_get_layer_context(
2745                         &ctx->main_ctx, (uint32_t)layerId);
2746         if (ctx_layer != NULL) {
2747             ivi_controller_layer_set_destination_rectangle(
2748                 ctx_layer->controller,
2749                 (int32_t)*pPosition, (int32_t)*(pPosition + 1),
2750                 ctx_layer->prop.destWidth, ctx_layer->prop.destHeight);
2751             returnValue = ILM_SUCCESS;
2752         }
2753     }
2754
2755     return returnValue;
2756 }
2757
2758 static ilmErrorTypes
2759 wayland_layerSetOrientation(t_ilm_layer layerId, ilmOrientation orientation)
2760 {
2761     ilmErrorTypes returnValue = ILM_FAILED;
2762     struct ilm_control_context *ctx = get_instance();
2763     struct layer_context *ctx_layer = NULL;
2764     int32_t iviorientation = 0;
2765
2766     do {
2767         switch(orientation) {
2768         case ILM_ZERO:
2769             iviorientation = IVI_CONTROLLER_SURFACE_ORIENTATION_0_DEGREES;
2770             break;
2771         case ILM_NINETY:
2772             iviorientation = IVI_CONTROLLER_SURFACE_ORIENTATION_90_DEGREES;
2773             break;
2774         case ILM_ONEHUNDREDEIGHTY:
2775             iviorientation = IVI_CONTROLLER_SURFACE_ORIENTATION_180_DEGREES;
2776             break;
2777         case ILM_TWOHUNDREDSEVENTY:
2778             iviorientation = IVI_CONTROLLER_SURFACE_ORIENTATION_270_DEGREES;
2779             break;
2780         default:
2781             returnValue = ILM_ERROR_INVALID_ARGUMENTS;
2782             break;
2783         }
2784
2785         ctx_layer = (struct layer_context*)wayland_controller_get_layer_context(
2786                         &ctx->main_ctx, (uint32_t)layerId);
2787         if (ctx_layer == NULL) {
2788             returnValue = ILM_FAILED;
2789             break;
2790         }
2791
2792         ivi_controller_layer_set_orientation(ctx_layer->controller,
2793                                              iviorientation);
2794
2795         returnValue = ILM_SUCCESS;
2796     } while(0);
2797
2798     return returnValue;
2799 }
2800
2801 static ilmErrorTypes
2802 wayland_layerGetOrientation(t_ilm_layer layerId, ilmOrientation *pOrientation)
2803 {
2804     ilmErrorTypes returnValue = ILM_FAILED;
2805     struct ilm_control_context *ctx = get_instance();
2806     struct layer_context *ctx_layer = NULL;
2807
2808     if (pOrientation != NULL) {
2809         ctx_layer = (struct layer_context*)
2810                     wayland_controller_get_layer_context(
2811                         &ctx->main_ctx, (uint32_t)layerId);
2812         if (ctx_layer != NULL) {
2813             *pOrientation = ctx_layer->prop.orientation;
2814             returnValue = ILM_SUCCESS;
2815         }
2816     }
2817
2818     return returnValue;
2819 }
2820
2821 static ilmErrorTypes
2822 wayland_layerSetChromaKey(t_ilm_layer layerId, t_ilm_int* pColor)
2823 {
2824     (void)layerId;
2825     (void)pColor;
2826     /* Not supported */
2827     return ILM_FAILED;
2828 }
2829
2830 static ilmErrorTypes
2831 wayland_layerSetRenderOrder(t_ilm_layer layerId,
2832                         t_ilm_surface *pSurfaceId,
2833                         t_ilm_int number)
2834 {
2835     ilmErrorTypes returnValue = ILM_FAILED;
2836     struct ilm_control_context *ctx = get_instance();
2837     struct layer_context *ctx_layer = NULL;
2838
2839     ctx_layer = (struct layer_context*)wayland_controller_get_layer_context(
2840                     &ctx->main_ctx, (uint32_t)layerId);
2841     if (ctx_layer) {
2842         int cnt = 0;
2843         uint32_t id = 0;
2844         struct surface_context *ctx_surf = NULL;
2845         ivi_controller_layer_clear_surfaces(ctx_layer->controller);
2846
2847         for (cnt = 0; cnt < number; cnt++) {
2848             id = (uint32_t)*(pSurfaceId + cnt);
2849             ctx_surf = get_surface_context(&ctx->main_ctx, id);
2850
2851             if (ctx_surf == NULL) {
2852                 fprintf(stderr, "invalud argument \
2853                         in ilm_layerSetRenderOrder\n");
2854                 continue;
2855             }
2856             ivi_controller_layer_add_surface(ctx_layer->controller,
2857                                              ctx_surf->controller);
2858         }
2859
2860         returnValue = ILM_SUCCESS;
2861     }
2862
2863     return returnValue;
2864 }
2865
2866 static ilmErrorTypes
2867 wayland_layerGetCapabilities(t_ilm_layer layerId,
2868                          t_ilm_layercapabilities *pCapabilities)
2869 {
2870     (void)layerId;
2871     (void)pCapabilities;
2872     /* Not supported */
2873     return ILM_FAILED;
2874 }
2875
2876 static ilmErrorTypes
2877 wayland_layerTypeGetCapabilities(ilmLayerType layerType,
2878                              t_ilm_layercapabilities *pCapabilities)
2879 {
2880     (void)layerType;
2881     (void)pCapabilities;
2882     /* Not supported */
2883     return ILM_FAILED;
2884 }
2885
2886 static ilmErrorTypes
2887 wayland_surfaceSetVisibility(t_ilm_surface surfaceId, t_ilm_bool newVisibility)
2888 {
2889     ilmErrorTypes returnValue = ILM_FAILED;
2890     struct ilm_control_context *ctx = get_instance();
2891     struct surface_context *ctx_surf = NULL;
2892     uint32_t visibility = 0;
2893
2894     if (newVisibility == ILM_TRUE) {
2895         visibility = 1;
2896     }
2897     ctx_surf = get_surface_context(&ctx->main_ctx, surfaceId);
2898     if (ctx_surf) {
2899         ivi_controller_surface_set_visibility(ctx_surf->controller,
2900                                               visibility);
2901         returnValue = ILM_SUCCESS;
2902     }
2903
2904     return returnValue;
2905 }
2906
2907 static ilmErrorTypes
2908 wayland_surfaceSetOpacity(t_ilm_surface surfaceId, t_ilm_float opacity)
2909 {
2910     ilmErrorTypes returnValue = ILM_FAILED;
2911     struct ilm_control_context *ctx = get_instance();
2912     struct surface_context *ctx_surf = NULL;
2913     wl_fixed_t opacity_fixed = 0;
2914
2915     opacity_fixed = wl_fixed_from_double((double)opacity);
2916     ctx_surf = get_surface_context(&ctx->main_ctx, surfaceId);
2917     if (ctx_surf) {
2918         ivi_controller_surface_set_opacity(ctx_surf->controller,
2919                                            opacity_fixed);
2920         returnValue = ILM_SUCCESS;
2921     }
2922
2923     return returnValue;
2924 }
2925
2926 static ilmErrorTypes
2927 wayland_surfaceGetOpacity(t_ilm_surface surfaceId, t_ilm_float *pOpacity)
2928 {
2929     ilmErrorTypes returnValue = ILM_FAILED;
2930     struct ilm_control_context *ctx = get_instance();
2931
2932     if (pOpacity != NULL) {
2933         struct surface_context *ctx_surf = NULL;
2934         ctx_surf = get_surface_context(&ctx->child_ctx, surfaceId);
2935         if (ctx_surf) {
2936             *pOpacity = ctx_surf->prop.opacity;
2937             returnValue = ILM_SUCCESS;
2938         }
2939     }
2940
2941     return returnValue;
2942 }
2943
2944 static ilmErrorTypes
2945 wayland_SetKeyboardFocusOn(t_ilm_surface surfaceId)
2946 {
2947     ilmErrorTypes returnValue = ILM_FAILED;
2948     (void)surfaceId;
2949     returnValue = ILM_SUCCESS;
2950     return returnValue;
2951 }
2952
2953 static ilmErrorTypes
2954 wayland_GetKeyboardFocusSurfaceId(t_ilm_surface* pSurfaceId)
2955 {
2956     ilmErrorTypes returnValue = ILM_FAILED;
2957     (void)pSurfaceId;
2958     returnValue = ILM_SUCCESS;
2959     return returnValue;
2960 }
2961
2962 static ilmErrorTypes
2963 wayland_surfaceSetDestinationRectangle(t_ilm_surface surfaceId,
2964                                    t_ilm_int x, t_ilm_int y,
2965                                    t_ilm_int width, t_ilm_int height)
2966 {
2967     ilmErrorTypes returnValue = ILM_FAILED;
2968     struct ilm_control_context *ctx = get_instance();
2969     struct surface_context *ctx_surf = NULL;
2970
2971     ctx_surf = get_surface_context(&ctx->main_ctx, surfaceId);
2972     if (ctx_surf) {
2973         ivi_controller_surface_set_destination_rectangle(
2974                                              ctx_surf->controller,
2975                                              x, y, width, height);
2976         returnValue = ILM_SUCCESS;
2977     }
2978
2979     return returnValue;
2980 }
2981
2982 static ilmErrorTypes
2983 wayland_surfaceSetDimension(t_ilm_surface surfaceId, t_ilm_uint *pDimension)
2984 {
2985     ilmErrorTypes returnValue = ILM_FAILED;
2986     struct ilm_control_context *ctx = get_instance();
2987
2988     if (pDimension != NULL) {
2989         struct surface_context *ctx_surf = NULL;
2990         ctx_surf = get_surface_context(&ctx->main_ctx, surfaceId);
2991         if (ctx_surf) {
2992             uint32_t width = *pDimension;
2993             uint32_t height = *(pDimension + 1);
2994             ivi_controller_surface_set_destination_rectangle(
2995                 ctx_surf->controller,
2996                 ctx_surf->prop.destX, ctx_surf->prop.destY, width, height);
2997             returnValue = ILM_SUCCESS;
2998         }
2999     }
3000
3001     return returnValue;
3002 }
3003
3004 static ilmErrorTypes
3005 wayland_surfaceGetPosition(t_ilm_surface surfaceId, t_ilm_uint *pPosition)
3006 {
3007     ilmErrorTypes returnValue = ILM_FAILED;
3008     struct ilm_control_context *ctx = get_instance();
3009
3010     if (pPosition != NULL) {
3011         struct surface_context *ctx_surf = NULL;
3012         ctx_surf = get_surface_context(&ctx->child_ctx, surfaceId);
3013         if (ctx_surf) {
3014             *pPosition = ctx_surf->prop.destX;
3015             *(pPosition + 1) = ctx_surf->prop.destY;
3016             returnValue = ILM_SUCCESS;
3017         }
3018     }
3019
3020     return returnValue;
3021 }
3022
3023 static ilmErrorTypes
3024 wayland_surfaceSetPosition(t_ilm_surface surfaceId, t_ilm_uint *pPosition)
3025 {
3026     ilmErrorTypes returnValue = ILM_FAILED;
3027     struct ilm_control_context *ctx = get_instance();
3028
3029     if (pPosition != NULL) {
3030         struct surface_context *ctx_surf = NULL;
3031         ctx_surf = get_surface_context(&ctx->main_ctx, surfaceId);
3032         if (ctx_surf) {
3033             int32_t destX = (int32_t)*pPosition;
3034             int32_t destY = (int32_t)*(pPosition + 1);
3035             ivi_controller_surface_set_destination_rectangle(
3036                 ctx_surf->controller, destX, destY,
3037                 ctx_surf->prop.destWidth, ctx_surf->prop.destHeight);
3038             returnValue = ILM_SUCCESS;
3039         }
3040     }
3041
3042     return returnValue;
3043 }
3044
3045 static ilmErrorTypes
3046 wayland_surfaceSetOrientation(t_ilm_surface surfaceId,
3047                               ilmOrientation orientation)
3048 {
3049     ilmErrorTypes returnValue = ILM_FAILED;
3050     struct ilm_control_context *ctx = get_instance();
3051     struct surface_context *ctx_surf = NULL;
3052     int32_t iviorientation = 0;
3053
3054     do {
3055         switch(orientation) {
3056         case ILM_ZERO:
3057             iviorientation = IVI_CONTROLLER_SURFACE_ORIENTATION_0_DEGREES;
3058             break;
3059         case ILM_NINETY:
3060             iviorientation = IVI_CONTROLLER_SURFACE_ORIENTATION_90_DEGREES;
3061             break;
3062         case ILM_ONEHUNDREDEIGHTY:
3063             iviorientation = IVI_CONTROLLER_SURFACE_ORIENTATION_180_DEGREES;
3064             break;
3065         case ILM_TWOHUNDREDSEVENTY:
3066             iviorientation = IVI_CONTROLLER_SURFACE_ORIENTATION_270_DEGREES;
3067             break;
3068         default:
3069             returnValue = ILM_ERROR_INVALID_ARGUMENTS;
3070             break;
3071         }
3072
3073         ctx_surf = get_surface_context(&ctx->main_ctx, surfaceId);
3074         if (ctx_surf == NULL) {
3075             returnValue = ILM_FAILED;
3076             break;
3077         }
3078
3079         ivi_controller_surface_set_orientation(ctx_surf->controller,
3080                                                iviorientation);
3081
3082         returnValue = ILM_SUCCESS;
3083     } while(0);
3084
3085     return returnValue;
3086 }
3087
3088 static ilmErrorTypes
3089 wayland_surfaceGetOrientation(t_ilm_surface surfaceId,
3090                               ilmOrientation *pOrientation)
3091 {
3092     ilmErrorTypes returnValue = ILM_FAILED;
3093     struct ilm_control_context *ctx = get_instance();
3094
3095     if (pOrientation != NULL) {
3096         struct surface_context *ctx_surf = NULL;
3097         ctx_surf = get_surface_context(&ctx->child_ctx, surfaceId);
3098         if (ctx_surf) {
3099             *pOrientation = ctx_surf->prop.orientation;
3100             returnValue = ILM_SUCCESS;
3101         }
3102     }
3103
3104     return returnValue;
3105 }
3106
3107 static ilmErrorTypes
3108 wayland_surfaceGetPixelformat(t_ilm_layer surfaceId,
3109                               ilmPixelFormat *pPixelformat)
3110 {
3111     ilmErrorTypes returnValue = ILM_FAILED;
3112     struct ilm_control_context *ctx = get_instance();
3113
3114     if (pPixelformat != NULL) {
3115         struct surface_context *ctx_surf = NULL;
3116         ctx_surf = get_surface_context(&ctx->child_ctx, surfaceId);
3117         if (ctx_surf) {
3118             *pPixelformat = ctx_surf->prop.pixelformat;
3119             returnValue = ILM_SUCCESS;
3120         }
3121     }
3122
3123     return returnValue;
3124 }
3125
3126 static ilmErrorTypes
3127 wayland_surfaceSetChromaKey(t_ilm_surface surfaceId, t_ilm_int* pColor)
3128 {
3129     (void)surfaceId;
3130     (void)pColor;
3131     /* Not supported */
3132     return ILM_FAILED;
3133 }
3134
3135 static ilmErrorTypes
3136 wayland_displaySetRenderOrder(t_ilm_display display,
3137                           t_ilm_layer *pLayerId, const t_ilm_uint number)
3138 {
3139     ilmErrorTypes returnValue = ILM_FAILED;
3140     struct ilm_control_context *ctx = get_instance();
3141     struct screen_context *ctx_scrn = NULL;
3142
3143     ctx_scrn = get_screen_context_by_id(&ctx->main_ctx, (uint32_t)display);
3144     if (ctx_scrn != NULL) {
3145         int cnt = 0;
3146         uint32_t id = 0;
3147         struct layer_context *ctx_layer = NULL;
3148
3149         ivi_controller_screen_clear(ctx_scrn->controller);
3150
3151         for (cnt = 0; cnt < (int)number; cnt++) {
3152             id = (uint32_t)*(pLayerId + cnt);
3153             ctx_layer =
3154                  (struct layer_context*)wayland_controller_get_layer_context(
3155                             &ctx->main_ctx, id);
3156             if (ctx_layer != NULL) {
3157                 ivi_controller_screen_add_layer(ctx_scrn->controller,
3158                                                 ctx_layer->controller);
3159             }
3160         }
3161
3162         returnValue = ILM_SUCCESS;
3163     }
3164
3165     return returnValue;
3166 }
3167
3168 static ilmErrorTypes
3169 wayland_takeScreenshot(t_ilm_uint screen, t_ilm_const_string filename)
3170 {
3171     ilmErrorTypes returnValue = ILM_FAILED;
3172     struct ilm_control_context *ctx = get_instance();
3173     struct screen_context *ctx_scrn = NULL;
3174
3175     ctx_scrn = get_screen_context_by_id(&ctx->main_ctx, (uint32_t)screen);
3176     if (ctx_scrn != NULL) {
3177         ivi_controller_screen_screenshot(ctx_scrn->controller,
3178                                         filename);
3179         wl_display_flush(ctx->main_ctx.display);
3180         returnValue = ILM_SUCCESS;
3181     }
3182
3183     return returnValue;
3184 }
3185
3186 static ilmErrorTypes
3187 wayland_takeLayerScreenshot(t_ilm_const_string filename, t_ilm_layer layerid)
3188 {
3189     ilmErrorTypes returnValue = ILM_FAILED;
3190     struct ilm_control_context *ctx = get_instance();
3191     struct layer_context *ctx_layer = NULL;
3192
3193     ctx_layer = (struct layer_context*)wayland_controller_get_layer_context(
3194                     &ctx->main_ctx, (uint32_t)layerid);
3195     if (ctx_layer != NULL) {
3196         ivi_controller_layer_screenshot(ctx_layer->controller,
3197                                         filename);
3198         returnValue = ILM_SUCCESS;
3199     }
3200
3201     return returnValue;
3202 }
3203
3204 static ilmErrorTypes
3205 wayland_takeSurfaceScreenshot(t_ilm_const_string filename,
3206                               t_ilm_surface surfaceid)
3207 {
3208     ilmErrorTypes returnValue = ILM_FAILED;
3209     struct ilm_control_context *ctx = get_instance();
3210     struct surface_context *ctx_surf = NULL;
3211
3212     ctx_surf = get_surface_context(&ctx->main_ctx, (uint32_t)surfaceid);
3213     if (ctx_surf) {
3214         ivi_controller_surface_screenshot(ctx_surf->controller,
3215                                           filename);
3216         wl_display_flush(ctx->main_ctx.display);
3217         returnValue = ILM_SUCCESS;
3218     }
3219
3220     return returnValue;
3221 }
3222
3223 static ilmErrorTypes
3224 wayland_SetOptimizationMode(ilmOptimization id, ilmOptimizationMode mode)
3225 {
3226     (void)id;
3227     (void)mode;
3228     /* Not supported */
3229     return ILM_FAILED;
3230 }
3231
3232 static ilmErrorTypes
3233 wayland_GetOptimizationMode(ilmOptimization id, ilmOptimizationMode* pMode)
3234 {
3235     (void)id;
3236     (void)pMode;
3237     /* Not supported */
3238     return ILM_FAILED;
3239 }
3240
3241 // TODO
3242 static ilmErrorTypes
3243 wayland_layerAddNotification(t_ilm_layer layer,
3244                              layerNotificationFunc callback)
3245 {
3246     ilmErrorTypes returnValue = ILM_FAILED;
3247     struct ilm_control_context *ctx = get_instance();
3248     struct layer_context *ctx_layer = NULL;
3249
3250     ctx_layer = (struct layer_context*)wayland_controller_get_layer_context(
3251                     &ctx->child_ctx, (uint32_t)layer);
3252     if (ctx_layer == NULL) {
3253         returnValue = ILM_ERROR_INVALID_ARGUMENTS;
3254     } else {
3255         ctx_layer->notification = callback;
3256
3257         returnValue = ILM_SUCCESS;
3258     }
3259
3260     return returnValue;
3261 }
3262
3263 // TODO
3264 static ilmErrorTypes
3265 wayland_layerRemoveNotification(t_ilm_layer layer)
3266 {
3267     ilmErrorTypes returnValue = ILM_FAILED;
3268     struct ilm_control_context *ctx = get_instance();
3269     struct layer_context *ctx_layer = NULL;
3270
3271     ctx_layer = (struct layer_context*)wayland_controller_get_layer_context(
3272                     &ctx->child_ctx, (uint32_t)layer);
3273     if (ctx_layer == NULL) {
3274         returnValue = ILM_ERROR_INVALID_ARGUMENTS;
3275     } else {
3276         ctx_layer->notification = NULL;
3277
3278         returnValue = ILM_SUCCESS;
3279     }
3280
3281     return returnValue;
3282 }
3283
3284 // TODO
3285 static ilmErrorTypes
3286 wayland_surfaceAddNotification(t_ilm_surface surface,
3287                              surfaceNotificationFunc callback)
3288 {
3289     ilmErrorTypes returnValue = ILM_FAILED;
3290     struct ilm_control_context *ctx = get_instance();
3291     struct surface_context *ctx_surf = NULL;
3292
3293     ctx_surf = (struct surface_context*)get_surface_context(
3294                     &ctx->child_ctx, (uint32_t)surface);
3295     if (ctx_surf == NULL) {
3296         returnValue = ILM_ERROR_INVALID_ARGUMENTS;
3297     } else {
3298         ctx_surf->notification = callback;
3299
3300         returnValue = ILM_SUCCESS;
3301     }
3302
3303     return returnValue;
3304 }
3305
3306 // TODO
3307 static ilmErrorTypes
3308 wayland_surfaceRemoveNotification(t_ilm_surface surface)
3309 {
3310     ilmErrorTypes returnValue = ILM_FAILED;
3311     struct ilm_control_context *ctx = get_instance();
3312     struct surface_context *ctx_surf = NULL;
3313
3314     ctx_surf = (struct surface_context*)get_surface_context(
3315                     &ctx->child_ctx, (uint32_t)surface);
3316     if (ctx_surf == NULL) {
3317         returnValue = ILM_ERROR_INVALID_ARGUMENTS;
3318     } else {
3319         ctx_surf->notification = NULL;
3320
3321         returnValue = ILM_SUCCESS;
3322     }
3323
3324     return returnValue;
3325 }
3326
3327 static ilmErrorTypes
3328 wayland_getNativeHandle(t_ilm_uint pid, t_ilm_int *n_handle,
3329                         t_ilm_nativehandle **p_handles)
3330 {
3331     struct ilm_control_context *ctx = get_instance();
3332     struct nativehandle_context *p_nh_ctx = NULL;
3333
3334     *n_handle = 0;
3335     *p_handles = NULL;
3336
3337     wl_list_for_each(p_nh_ctx, &ctx->list_nativehandle, link)
3338     {
3339         if (p_nh_ctx->pid == pid)
3340         {
3341             *n_handle = 1;
3342             *p_handles =
3343                  (t_ilm_nativehandle*)malloc(sizeof(t_ilm_nativehandle));
3344             (*p_handles)[0] = p_nh_ctx->nativehandle;
3345             break;
3346         }
3347     }
3348
3349     return (*n_handle > 0) ? ILM_SUCCESS : ILM_FAILED;
3350 }
3351
3352 static ilmErrorTypes
3353 wayland_getPropertiesOfSurface(t_ilm_uint surfaceID,
3354                         struct ilmSurfaceProperties* pSurfaceProperties)
3355 {
3356     ilmErrorTypes returnValue = ILM_FAILED;
3357     struct ilm_control_context *ctx = get_instance();
3358
3359     if (pSurfaceProperties != NULL) {
3360         struct surface_context *ctx_surf = NULL;
3361
3362         ctx_surf = get_surface_context(&ctx->child_ctx, (uint32_t)surfaceID);
3363         if (ctx_surf != NULL) {
3364
3365             *pSurfaceProperties = ctx_surf->prop;
3366             returnValue = ILM_SUCCESS;
3367         }
3368     }
3369
3370     return returnValue;
3371 }
3372
3373 static ilmErrorTypes
3374 wayland_layerAddSurface(t_ilm_layer layerId,
3375                         t_ilm_surface surfaceId)
3376 {
3377     ilmErrorTypes returnValue = ILM_FAILED;
3378     struct ilm_control_context *ctx = get_instance();
3379     struct layer_context *ctx_layer = NULL;
3380     struct surface_context *ctx_surf = NULL;
3381
3382     ctx_layer = (struct layer_context*)wayland_controller_get_layer_context(
3383                     &ctx->main_ctx, (uint32_t)layerId);
3384     ctx_surf = get_surface_context(&ctx->main_ctx, (uint32_t)surfaceId);
3385     if ((ctx_layer != NULL) && (ctx_surf != NULL)) {
3386         ivi_controller_layer_add_surface(ctx_layer->controller,
3387                                          ctx_surf->controller);
3388         returnValue = ILM_SUCCESS;
3389     }
3390
3391     return returnValue;
3392 }
3393
3394 static ilmErrorTypes
3395 wayland_layerRemoveSurface(t_ilm_layer layerId,
3396                            t_ilm_surface surfaceId)
3397 {
3398     ilmErrorTypes returnValue = ILM_FAILED;
3399     struct ilm_control_context *ctx = get_instance();
3400     struct layer_context *ctx_layer = NULL;
3401     struct surface_context *ctx_surf = NULL;
3402
3403     ctx_layer = (struct layer_context*)wayland_controller_get_layer_context(
3404                     &ctx->main_ctx, (uint32_t)layerId);
3405     ctx_surf = get_surface_context(&ctx->main_ctx, (uint32_t)surfaceId);
3406     if ((ctx_layer != NULL) && (ctx_surf != NULL)) {
3407         ivi_controller_layer_remove_surface(ctx_layer->controller,
3408                                             ctx_surf->controller);
3409         returnValue = ILM_SUCCESS;
3410     }
3411
3412     return returnValue;
3413 }
3414
3415 static ilmErrorTypes
3416 wayland_surfaceGetDimension(t_ilm_surface surfaceId,
3417                             t_ilm_uint *pDimension)
3418 {
3419     ilmErrorTypes returnValue = ILM_FAILED;
3420     struct ilm_control_context *ctx = get_instance();
3421
3422     if (pDimension != NULL) {
3423         struct surface_context *ctx_surf = NULL;
3424
3425         ctx_surf = get_surface_context(&ctx->child_ctx, (uint32_t)surfaceId);
3426         if (ctx_surf != NULL) {
3427             *pDimension = (t_ilm_uint)ctx_surf->prop.destWidth;
3428             *(pDimension + 1) = (t_ilm_uint)ctx_surf->prop.destHeight;
3429             returnValue = ILM_SUCCESS;
3430         }
3431     }
3432
3433     return returnValue;
3434 }
3435
3436 static ilmErrorTypes
3437 wayland_surfaceGetVisibility(t_ilm_surface surfaceId,
3438                              t_ilm_bool *pVisibility)
3439 {
3440     ilmErrorTypes returnValue = ILM_FAILED;
3441     struct ilm_control_context *ctx = get_instance();
3442     struct surface_context *ctx_surf = NULL;
3443
3444     if (pVisibility != NULL) {
3445         ctx_surf = get_surface_context(&ctx->child_ctx, (uint32_t)surfaceId);
3446         if (ctx_surf != NULL) {
3447             *pVisibility = (t_ilm_bool)ctx_surf->prop.visibility;
3448             returnValue = ILM_SUCCESS;
3449         }
3450     }
3451
3452     return returnValue;
3453 }
3454
3455 static ilmErrorTypes
3456 wayland_surfaceSetSourceRectangle(t_ilm_surface surfaceId,
3457                                   t_ilm_int x, t_ilm_int y,
3458                                   t_ilm_int width, t_ilm_int height)
3459 {
3460     ilmErrorTypes returnValue = ILM_FAILED;
3461     struct ilm_control_context *ctx = get_instance();
3462     struct surface_context *ctx_surf = NULL;
3463
3464     ctx_surf = get_surface_context(&ctx->main_ctx, (uint32_t)surfaceId);
3465     if (ctx_surf != NULL) {
3466         if (ctx_surf->controller != NULL) {
3467             ivi_controller_surface_set_source_rectangle(
3468                     ctx_surf->controller,
3469                     x, y, width, height);
3470             returnValue = ILM_SUCCESS;
3471         }
3472     }
3473
3474     return returnValue;
3475 }
3476
3477 static ilmErrorTypes
3478 wayland_commitChanges()
3479 {
3480     ilmErrorTypes returnValue = ILM_FAILED;
3481     struct ilm_control_context *ctx = get_instance();
3482
3483     if (ctx->main_ctx.controller != NULL) {
3484         ivi_controller_commit_changes(ctx->main_ctx.controller);
3485
3486         wl_display_roundtrip(ctx->main_ctx.display);
3487         returnValue = ILM_SUCCESS;
3488     }
3489
3490     return returnValue;
3491 }