ilmControl: renamed child controller_surface listener
[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(void);
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(void);
151
152 void init_ilmControlPlatformTable(void)
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 wayland_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 wayland_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_event_queue *queue;
338     struct wl_compositor *compositor;
339     struct ivi_controller *controller;
340     uint32_t num_screen;
341
342     struct wl_list list_surface;
343     struct wl_list list_layer;
344     struct wl_list list_screen;
345 };
346
347 struct ilm_control_context {
348     struct wayland_context main_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 void roundtrip_done(void *data, struct wl_callback *callback,
363                            uint32_t serial)
364 {
365     (void) callback;
366     (void) serial;
367
368     *(int *)data = 1;
369 }
370
371 static struct wl_callback_listener roundtrip_listener = {roundtrip_done};
372
373 int display_roundtrip_queue(struct wl_display *display,
374                             struct wl_event_queue *queue)
375 {
376     int done = 0;
377     int ret = 0;
378     struct wl_callback *callback = wl_display_sync(display);
379     wl_proxy_set_queue((void *)callback, queue);
380     wl_callback_add_listener(callback, &roundtrip_listener, &done);
381
382     while (ret != -1 && !done)
383     {
384         ret = wl_display_dispatch_queue(display, queue);
385     }
386
387     wl_callback_destroy(callback);
388
389     return ret;
390 }
391
392 static inline void lock_context(struct ilm_control_context *ctx)
393 {
394    pthread_mutex_lock(&ctx->mutex);
395 }
396
397 static inline void unlock_context(struct ilm_control_context *ctx)
398 {
399    pthread_mutex_unlock(&ctx->mutex);
400 }
401
402 static int init_control(void);
403
404 static struct ilm_control_context* get_instance(void);
405
406 static int create_controller_layer(struct wayland_context *ctx, t_ilm_uint width, t_ilm_uint height, t_ilm_layer layerid);
407
408 static int32_t
409 wayland_controller_is_inside_surface_list(struct wl_list *list,
410                                           uint32_t id_surface)
411 {
412     struct surface_context *ctx_surf = NULL;
413     wl_list_for_each(ctx_surf, list, link) {
414         if (ctx_surf->id_surface == id_surface) {
415             return 1;
416         }
417     }
418
419     return 0;
420 }
421
422 static int32_t
423 wayland_controller_is_inside_layer_list(struct wl_list *list,
424                                         uint32_t id_layer)
425 {
426     struct layer_context *ctx_layer = NULL;
427     wl_list_for_each(ctx_layer, list, link) {
428         if (ctx_layer->id_layer == id_layer) {
429             return 1;
430         }
431     }
432
433     return 0;
434 }
435
436 static struct layer_context*
437 wayland_controller_get_layer_context(struct wayland_context *ctx,
438                                      uint32_t id_layer)
439 {
440     struct layer_context *ctx_layer = NULL;
441
442     if (ctx->controller == NULL) {
443         fprintf(stderr, "controller is not initialized in ilmControl\n");
444         return NULL;
445     }
446
447     wl_list_for_each(ctx_layer, &ctx->list_layer, link) {
448         if (ctx_layer->id_layer == id_layer) {
449             return ctx_layer;
450         }
451     }
452
453     fprintf(stderr, "failed to get layer context in ilmControl\n");
454     return NULL;
455 }
456
457 static void
458 output_listener_geometry(void *data,
459                          struct wl_output *output,
460                          int32_t x,
461                          int32_t y,
462                          int32_t physical_width,
463                          int32_t physical_height,
464                          int32_t subpixel,
465                          const char *make,
466                          const char *model,
467                          int32_t transform)
468 {
469     (void)output;
470     (void)x;
471     (void)y;
472     (void)subpixel;
473     (void)make;
474     (void)model;
475     (void)transform;
476 }
477
478 static void
479 output_listener_mode(void *data,
480                      struct wl_output *output,
481                      uint32_t flags,
482                      int32_t width,
483                      int32_t height,
484                      int32_t refresh)
485 {
486     (void)data;
487     (void)output;
488     (void)flags;
489     (void)width;
490     (void)height;
491     (void)refresh;
492     struct screen_context *ctx_scrn = data;
493     ctx_scrn->prop.screenWidth = width;
494     ctx_scrn->prop.screenHeight = height;
495 }
496
497 static void
498 output_listener_done(void *data,
499                      struct wl_output *output)
500 {
501     (void)data;
502     (void)output;
503 }
504
505 static void
506 output_listener_scale(void *data,
507                       struct wl_output *output,
508                       int32_t factor)
509 {
510     (void)data;
511     (void)output;
512     (void)factor;
513 }
514
515 static struct wl_output_listener output_listener = {
516     output_listener_geometry,
517     output_listener_mode,
518     output_listener_done,
519     output_listener_scale
520 };
521
522 static struct screen_context*
523 get_screen_context_by_output(struct wayland_context *ctx,
524                              struct wl_output *output)
525 {
526     struct screen_context *ctx_scrn = NULL;
527     struct wl_proxy *pxy_out = NULL;
528     struct wl_proxy *pxy_out_in_scrn = NULL;
529     uint32_t pxy_id = 0;
530     uint32_t pxy_id_in_scrn = 0;
531
532     wl_list_for_each(ctx_scrn, &ctx->list_screen, link) {
533         pxy_out = (struct wl_proxy*)output;
534         pxy_out_in_scrn = (struct wl_proxy*)ctx_scrn->output;
535         pxy_id = wl_proxy_get_id(pxy_out);
536         pxy_id_in_scrn = wl_proxy_get_id(pxy_out_in_scrn);
537         if (pxy_id == pxy_id_in_scrn) {
538             return ctx_scrn;
539         }
540     }
541     return NULL;
542 }
543
544 static struct screen_context*
545 get_screen_context_by_serverid(struct wayland_context *ctx,
546                                uint32_t id_screen)
547 {
548     struct screen_context *ctx_scrn = NULL;
549
550     wl_list_for_each(ctx_scrn, &ctx->list_screen, link) {
551         if (ctx_scrn->id_from_server == id_screen) {
552             return ctx_scrn;
553         }
554     }
555     return NULL;
556 }
557
558 static void
559 add_orderlayer_to_screen(struct layer_context *ctx_layer,
560                          struct wl_output* output)
561 {
562     struct screen_context *ctx_scrn = NULL;
563
564     ctx_scrn = get_screen_context_by_output(ctx_layer->ctx, output);
565     if (ctx_scrn == NULL) {
566         fprintf(stderr, "failed to add_orderlayer_to_screen\n");
567         return;
568     }
569
570     int found = 0;
571     struct layer_context *layer_link;
572     wl_list_for_each(layer_link, &ctx_scrn->order.list_layer, order.link) {
573         if (layer_link == ctx_layer) {
574             found = 1;
575             break;
576         }
577     }
578
579     if (found == 0) {
580         wl_list_init(&ctx_layer->order.link);
581         wl_list_insert(&ctx_scrn->order.list_layer, &ctx_layer->order.link);
582     }
583 }
584
585 static void
586 remove_orderlayer_from_screen(struct layer_context *ctx_layer)
587 {
588     wl_list_remove(&ctx_layer->order.link);
589     wl_list_init(&ctx_layer->order.link);
590 }
591
592 static void
593 controller_layer_listener_visibility(void *data,
594                             struct ivi_controller_layer *controller,
595                             int32_t visibility)
596 {
597     struct layer_context *ctx_layer = data;
598
599     ctx_layer->prop.visibility = (t_ilm_bool)visibility;
600
601     if (ctx_layer->notification != NULL) {
602         ctx_layer->notification(ctx_layer->id_layer,
603                                 &ctx_layer->prop,
604                                 ILM_NOTIFICATION_VISIBILITY);
605     }
606 }
607
608 static void
609 controller_layer_listener_opacity(void *data,
610                        struct ivi_controller_layer *controller,
611                        wl_fixed_t opacity)
612 {
613     struct layer_context *ctx_layer = data;
614
615     ctx_layer->prop.opacity = (t_ilm_float)wl_fixed_to_double(opacity);
616
617     if (ctx_layer->notification != NULL) {
618         ctx_layer->notification(ctx_layer->id_layer,
619                                 &ctx_layer->prop,
620                                 ILM_NOTIFICATION_OPACITY);
621     }
622 }
623
624 static void
625 controller_layer_listener_source_rectangle(void *data,
626                                 struct ivi_controller_layer *controller,
627                                 int32_t x,
628                                 int32_t y,
629                                 int32_t width,
630                                 int32_t height)
631 {
632     struct layer_context *ctx_layer = data;
633
634     ctx_layer->prop.sourceX = (t_ilm_uint)x;
635     ctx_layer->prop.sourceY = (t_ilm_uint)y;
636     ctx_layer->prop.sourceWidth = (t_ilm_uint)width;
637     ctx_layer->prop.sourceHeight = (t_ilm_uint)height;
638     if (ctx_layer->prop.origSourceWidth == 0) {
639         ctx_layer->prop.origSourceWidth = (t_ilm_uint)width;
640     }
641     if (ctx_layer->prop.origSourceHeight == 0) {
642         ctx_layer->prop.origSourceHeight = (t_ilm_uint)height;
643     }
644
645     if (ctx_layer->notification != NULL) {
646         ctx_layer->notification(ctx_layer->id_layer,
647                                 &ctx_layer->prop,
648                                 ILM_NOTIFICATION_SOURCE_RECT);
649     }
650 }
651
652 static void
653 controller_layer_listener_destination_rectangle(void *data,
654                                      struct ivi_controller_layer *controller,
655                                      int32_t x,
656                                      int32_t y,
657                                      int32_t width,
658                                      int32_t height)
659 {
660     struct layer_context *ctx_layer = data;
661
662     ctx_layer->prop.destX = (t_ilm_uint)x;
663     ctx_layer->prop.destY = (t_ilm_uint)y;
664     ctx_layer->prop.destWidth = (t_ilm_uint)width;
665     ctx_layer->prop.destHeight = (t_ilm_uint)height;
666
667     if (ctx_layer->notification != NULL) {
668         ctx_layer->notification(ctx_layer->id_layer,
669                                 &ctx_layer->prop,
670                                 ILM_NOTIFICATION_DEST_RECT);
671     }
672 }
673
674 static void
675 controller_layer_listener_configuration(void *data,
676                           struct ivi_controller_layer *controller,
677                           int32_t width,
678                           int32_t height)
679 {
680     struct layer_context *ctx_layer = data;
681
682     ctx_layer->prop.sourceWidth = (t_ilm_uint)width;
683     ctx_layer->prop.sourceHeight = (t_ilm_uint)height;
684 }
685
686 static void
687 controller_layer_listener_orientation(void *data,
688                              struct ivi_controller_layer *controller,
689                              int32_t orientation)
690 {
691     ilmOrientation ilmorientation = ILM_ZERO;
692     struct layer_context *ctx_layer = data;
693
694     switch(orientation) {
695     case IVI_CONTROLLER_SURFACE_ORIENTATION_0_DEGREES:
696         ilmorientation = ILM_ZERO;
697         break;
698     case IVI_CONTROLLER_SURFACE_ORIENTATION_90_DEGREES:
699         ilmorientation = ILM_NINETY;
700         break;
701     case IVI_CONTROLLER_SURFACE_ORIENTATION_180_DEGREES:
702         ilmorientation = ILM_ONEHUNDREDEIGHTY;
703         break;
704     case IVI_CONTROLLER_SURFACE_ORIENTATION_270_DEGREES:
705         ilmorientation = ILM_TWOHUNDREDSEVENTY;
706         break;
707     default:
708         break;
709     }
710
711     ctx_layer->prop.orientation = ilmorientation;
712
713     if (ctx_layer->notification != NULL) {
714         ctx_layer->notification(ctx_layer->id_layer,
715                                 &ctx_layer->prop,
716                                 ILM_NOTIFICATION_ORIENTATION);
717     }
718 }
719
720 static void
721 controller_layer_listener_screen(void *data,
722                                  struct ivi_controller_layer *controller,
723                                  struct wl_output *output)
724 {
725     struct layer_context *ctx_layer = data;
726
727     if (output == NULL) {
728         remove_orderlayer_from_screen(ctx_layer);
729     } else {
730         add_orderlayer_to_screen(ctx_layer, output);
731     }
732 }
733
734 static void
735 controller_layer_listener_destroyed(void *data,
736                                     struct ivi_controller_layer *controller)
737 {
738     struct layer_context *ctx_layer = data;
739     wl_list_remove(&ctx_layer->link);
740     free(ctx_layer);
741 }
742
743 static struct ivi_controller_layer_listener controller_layer_listener =
744 {
745     controller_layer_listener_visibility,
746     controller_layer_listener_opacity,
747     controller_layer_listener_source_rectangle,
748     controller_layer_listener_destination_rectangle,
749     controller_layer_listener_configuration,
750     controller_layer_listener_orientation,
751     controller_layer_listener_screen,
752     controller_layer_listener_destroyed
753 };
754
755 static void
756 add_ordersurface_to_layer(struct surface_context *ctx_surf,
757                           struct ivi_controller_layer *layer)
758 {
759     struct layer_context *ctx_layer = NULL;
760     struct surface_context *link = NULL;
761     int found = 0;
762
763     ctx_layer = ivi_controller_layer_get_user_data(layer);
764
765     wl_list_for_each(link, &ctx_layer->order.list_surface, order.link) {
766         if (link == ctx_surf) {
767             found = 1;
768             break;
769         }
770     }
771
772     if (found == 0) {
773         wl_list_init(&ctx_surf->order.link);
774         wl_list_insert(&ctx_layer->order.list_surface, &ctx_surf->order.link);
775     }
776 }
777
778 static void
779 remove_ordersurface_from_layer(struct surface_context *ctx_surf)
780 {
781     wl_list_remove(&ctx_surf->order.link);
782     wl_list_init(&ctx_surf->order.link);
783 }
784
785 static void
786 controller_surface_listener_visibility(void *data,
787                             struct ivi_controller_surface *controller,
788                             int32_t visibility)
789 {
790     struct surface_context *ctx_surf = data;
791
792     ctx_surf->prop.visibility = (t_ilm_bool)visibility;
793
794     if (ctx_surf->notification != NULL) {
795         ctx_surf->notification(ctx_surf->id_surface,
796                                 &ctx_surf->prop,
797                                 ILM_NOTIFICATION_VISIBILITY);
798     }
799 }
800
801 static void
802 controller_surface_listener_opacity(void *data,
803                          struct ivi_controller_surface *controller,
804                          wl_fixed_t opacity)
805 {
806     struct surface_context *ctx_surf = data;
807
808     ctx_surf->prop.opacity = (t_ilm_float)wl_fixed_to_double(opacity);
809
810     if (ctx_surf->notification != NULL) {
811         ctx_surf->notification(ctx_surf->id_surface,
812                                 &ctx_surf->prop,
813                                 ILM_NOTIFICATION_OPACITY);
814     }
815 }
816
817 static void
818 controller_surface_listener_configuration(void *data,
819                            struct ivi_controller_surface *controller,
820                            int32_t width,
821                            int32_t height)
822 {
823     struct surface_context *ctx_surf = data;
824
825     ctx_surf->prop.sourceWidth = (t_ilm_uint)width;
826     ctx_surf->prop.sourceHeight = (t_ilm_uint)height;
827 }
828
829 static void
830 controller_surface_listener_source_rectangle(void *data,
831                                   struct ivi_controller_surface *controller,
832                                   int32_t x,
833                                   int32_t y,
834                                   int32_t width,
835                                   int32_t height)
836 {
837     struct surface_context *ctx_surf = data;
838
839     ctx_surf->prop.sourceX = (t_ilm_uint)x;
840     ctx_surf->prop.sourceY = (t_ilm_uint)y;
841     ctx_surf->prop.sourceWidth = (t_ilm_uint)width;
842     ctx_surf->prop.sourceHeight = (t_ilm_uint)height;
843     if (ctx_surf->prop.origSourceWidth == 0) {
844         ctx_surf->prop.origSourceWidth = (t_ilm_uint)width;
845     }
846     if (ctx_surf->prop.origSourceHeight == 0) {
847         ctx_surf->prop.origSourceHeight = (t_ilm_uint)height;
848     }
849
850     if (ctx_surf->notification != NULL) {
851         ctx_surf->notification(ctx_surf->id_surface,
852                                 &ctx_surf->prop,
853                                 ILM_NOTIFICATION_SOURCE_RECT);
854     }
855 }
856
857 static void
858 controller_surface_listener_destination_rectangle(void *data,
859                    struct ivi_controller_surface *controller,
860                    int32_t x,
861                    int32_t y,
862                    int32_t width,
863                    int32_t height)
864 {
865     struct surface_context *ctx_surf = data;
866
867     ctx_surf->prop.destX = (t_ilm_uint)x;
868     ctx_surf->prop.destY = (t_ilm_uint)y;
869     ctx_surf->prop.destWidth = (t_ilm_uint)width;
870     ctx_surf->prop.destHeight = (t_ilm_uint)height;
871
872     if (ctx_surf->notification != NULL) {
873         ctx_surf->notification(ctx_surf->id_surface,
874                                 &ctx_surf->prop,
875                                 ILM_NOTIFICATION_DEST_RECT);
876     }
877 }
878
879 static void
880 controller_surface_listener_orientation(void *data,
881                              struct ivi_controller_surface *controller,
882                              int32_t orientation)
883 {
884     struct surface_context *ctx_surf = data;
885     ilmOrientation ilmorientation = ILM_ZERO;
886
887     switch (orientation) {
888     case IVI_CONTROLLER_SURFACE_ORIENTATION_0_DEGREES:
889         ilmorientation = ILM_ZERO;
890         break;
891     case IVI_CONTROLLER_SURFACE_ORIENTATION_90_DEGREES:
892         ilmorientation = ILM_NINETY;
893         break;
894     case IVI_CONTROLLER_SURFACE_ORIENTATION_180_DEGREES:
895         ilmorientation = ILM_ONEHUNDREDEIGHTY;
896         break;
897     case IVI_CONTROLLER_SURFACE_ORIENTATION_270_DEGREES:
898         ilmorientation = ILM_TWOHUNDREDSEVENTY;
899         break;
900     default:
901         break;
902     }
903
904     ctx_surf->prop.orientation = ilmorientation;
905
906     if (ctx_surf->notification != NULL) {
907         ctx_surf->notification(ctx_surf->id_surface,
908                                 &ctx_surf->prop,
909                                 ILM_NOTIFICATION_ORIENTATION);
910     }
911 }
912
913 static void
914 controller_surface_listener_pixelformat(void *data,
915                              struct ivi_controller_surface *controller,
916                              int32_t pixelformat)
917 {
918     struct surface_context *ctx_surf = data;
919
920     ctx_surf->prop.pixelformat = (t_ilm_uint)pixelformat;
921 }
922
923 static void
924 controller_surface_listener_layer(void *data,
925                                   struct ivi_controller_surface *controller,
926                                   struct ivi_controller_layer *layer)
927 {
928     struct surface_context *ctx_surf = data;
929
930     if (layer == NULL) {
931         remove_ordersurface_from_layer(ctx_surf);
932     } else {
933         add_ordersurface_to_layer(ctx_surf, layer);
934     }
935 }
936
937 static void
938 controller_surface_listener_stats(void *data,
939                                   struct ivi_controller_surface *controller,
940                                   uint32_t redraw_count,
941                                   uint32_t frame_count,
942                                   uint32_t update_count,
943                                   uint32_t pid,
944                                   const char *process_name)
945 {
946     struct surface_context *ctx_surf = data;
947     (void)process_name;
948
949     ctx_surf->prop.drawCounter = (t_ilm_uint)redraw_count;
950     ctx_surf->prop.frameCounter = (t_ilm_uint)frame_count;
951     ctx_surf->prop.updateCounter = (t_ilm_uint)update_count;
952     ctx_surf->prop.creatorPid = (t_ilm_uint)pid;
953 }
954
955 static void
956 controller_surface_listener_destroyed(void *data,
957                   struct ivi_controller_surface *controller)
958 {
959     struct surface_context *ctx_surf = data;
960
961     wl_list_remove(&ctx_surf->link);
962     free(ctx_surf);
963 }
964
965 static void
966 controller_surface_listener_content(void *data,
967                    struct ivi_controller_surface *controller,
968                    int32_t content_state)
969 {
970     // if client surface (=content) was removed with ilm_surfaceDestroy()
971     // the expected behavior within ILM API mandates a full removal
972     // of the surface from the scene. We must remove the controller
973     // from scene, too.
974     if (IVI_CONTROLLER_SURFACE_CONTENT_STATE_CONTENT_REMOVED == content_state)
975     {
976         struct surface_context *ctx_surf = data;
977
978         ivi_controller_surface_destroy(controller, 1);
979
980         wl_list_remove(&ctx_surf->link);
981         free(ctx_surf);
982     }
983 }
984
985 static void
986 controller_surface_listener_input_focus(void *data,
987                    struct ivi_controller_surface *controller,
988                    int32_t enabled)
989 {
990     (void)data;
991     (void)controller;
992     (void)enabled;
993 }
994
995 static struct ivi_controller_surface_listener controller_surface_listener=
996 {
997     controller_surface_listener_visibility,
998     controller_surface_listener_opacity,
999     controller_surface_listener_source_rectangle,
1000     controller_surface_listener_destination_rectangle,
1001     controller_surface_listener_configuration,
1002     controller_surface_listener_orientation,
1003     controller_surface_listener_pixelformat,
1004     controller_surface_listener_layer,
1005     controller_surface_listener_stats,
1006     controller_surface_listener_destroyed,
1007     controller_surface_listener_content,
1008     controller_surface_listener_input_focus
1009 };
1010
1011 static void
1012 controller_surface_listener_visibility_main(void *data,
1013                             struct ivi_controller_surface *controller,
1014                             int32_t visibility)
1015 {
1016     (void)data;
1017     (void)controller;
1018     (void)visibility;
1019 }
1020
1021 static void
1022 controller_surface_listener_opacity_main(void *data,
1023                          struct ivi_controller_surface *controller,
1024                          wl_fixed_t opacity)
1025 {
1026     (void)data;
1027     (void)controller;
1028     (void)opacity;
1029 }
1030
1031 static void
1032 controller_surface_listener_source_rectangle_main(void *data,
1033                                   struct ivi_controller_surface *controller,
1034                                   int32_t x,
1035                                   int32_t y,
1036                                   int32_t width,
1037                                   int32_t height)
1038 {
1039     (void)data;
1040     (void)controller;
1041     (void)x;
1042     (void)y;
1043     (void)width;
1044     (void)height;
1045 }
1046
1047 static void
1048 controller_surface_listener_destination_rectangle_main(void *data,
1049                    struct ivi_controller_surface *controller,
1050                    int32_t x,
1051                    int32_t y,
1052                    int32_t width,
1053                    int32_t height)
1054 {
1055     (void)data;
1056     (void)controller;
1057     (void)x;
1058     (void)y;
1059     (void)width;
1060     (void)height;
1061 }
1062
1063 static void
1064 controller_surface_listener_configuration_main(void *data,
1065                            struct ivi_controller_surface *controller,
1066                            int32_t width,
1067                            int32_t height)
1068 {
1069     (void)data;
1070     (void)controller;
1071     (void)width;
1072     (void)height;
1073 }
1074
1075 static void
1076 controller_surface_listener_orientation_main(void *data,
1077                              struct ivi_controller_surface *controller,
1078                              int32_t orientation)
1079 {
1080     (void)data;
1081     (void)controller;
1082     (void)orientation;
1083 }
1084
1085 static void
1086 controller_surface_listener_pixelformat_main(void *data,
1087                              struct ivi_controller_surface *controller,
1088                              int32_t pixelformat)
1089 {
1090     (void)data;
1091     (void)controller;
1092     (void)pixelformat;
1093 }
1094
1095 static void
1096 controller_surface_listener_layer_main(void *data,
1097                                   struct ivi_controller_surface *controller,
1098                                   struct ivi_controller_layer *layer)
1099 {
1100     struct surface_context *ctx_surf = data;
1101
1102     if (layer == NULL) {
1103         remove_ordersurface_from_layer(ctx_surf);
1104     } else {
1105         add_ordersurface_to_layer(ctx_surf, layer);
1106     }
1107 }
1108
1109 static void
1110 controller_surface_listener_stats_main(void *data,
1111                                   struct ivi_controller_surface *controller,
1112                                   uint32_t redraw_count,
1113                                   uint32_t frame_count,
1114                                   uint32_t update_count,
1115                                   uint32_t pid,
1116                                   const char *process_name)
1117 {
1118     (void)data;
1119     (void)controller;
1120     (void)redraw_count;
1121     (void)frame_count;
1122     (void)update_count;
1123     (void)pid;
1124     (void)process_name;
1125 }
1126
1127 static void
1128 controller_surface_listener_destroyed_main(void *data,
1129                   struct ivi_controller_surface *controller)
1130 {
1131     struct surface_context *ctx_surf = data;
1132
1133     wl_list_remove(&ctx_surf->link);
1134     free(ctx_surf);
1135 }
1136
1137 static void
1138 controller_surface_listener_content_main(void *data,
1139                    struct ivi_controller_surface *controller,
1140                    int32_t content_state)
1141 {
1142     // if client surface (=content) was removed with ilm_surfaceDestroy()
1143     // the expected behavior within ILM API mandates a full removal
1144     // of the surface from the scene. We must remove the controller
1145     // from scene, too.
1146     if (IVI_CONTROLLER_SURFACE_CONTENT_STATE_CONTENT_REMOVED == content_state)
1147     {
1148         struct surface_context *ctx_surf = data;
1149
1150         ivi_controller_surface_destroy(controller, 1);
1151
1152         wl_list_remove(&ctx_surf->link);
1153         free(ctx_surf);
1154     }
1155 }
1156
1157 static void
1158 controller_surface_listener_input_focus_main(void *data,
1159                    struct ivi_controller_surface *controller,
1160                    int32_t enabled)
1161 {
1162     (void)data;
1163     (void)controller;
1164     (void)enabled;
1165 }
1166
1167 static struct ivi_controller_surface_listener controller_surface_listener_main =
1168 {
1169     controller_surface_listener_visibility_main,
1170     controller_surface_listener_opacity_main,
1171     controller_surface_listener_source_rectangle_main,
1172     controller_surface_listener_destination_rectangle_main,
1173     controller_surface_listener_configuration_main,
1174     controller_surface_listener_orientation_main,
1175     controller_surface_listener_pixelformat_main,
1176     controller_surface_listener_layer_main,
1177     controller_surface_listener_stats_main,
1178     controller_surface_listener_destroyed_main,
1179     controller_surface_listener_content_main,
1180     controller_surface_listener_input_focus_main
1181 };
1182
1183 static void
1184 controller_listener_screen_for_child(void *data,
1185                            struct ivi_controller *ivi_controller,
1186                            uint32_t id_screen,
1187                            struct ivi_controller_screen *controller_screen)
1188 {
1189     (void)data;
1190     (void)ivi_controller;
1191     (void)id_screen;
1192     (void)controller_screen;
1193 }
1194
1195 static void
1196 controller_listener_layer_for_child(void *data,
1197                           struct ivi_controller *controller,
1198                           uint32_t id_layer)
1199 {
1200    struct wayland_context *ctx = data;
1201
1202    if (wayland_controller_is_inside_layer_list(&ctx->list_layer, id_layer))
1203    {
1204       return;
1205    }
1206
1207    (void) create_controller_layer(ctx, 0, 0, id_layer);
1208 }
1209
1210 static void
1211 controller_listener_surface_for_child(void *data,
1212                             struct ivi_controller *controller,
1213                             uint32_t id_surface)
1214 {
1215     struct wayland_context *ctx = data;
1216     struct surface_context *ctx_surf = NULL;
1217     int32_t is_inside = 0;
1218
1219     is_inside = wayland_controller_is_inside_surface_list(
1220                     &ctx->list_surface, id_surface);
1221
1222     if (is_inside != 0) {
1223         fprintf(stderr, "invalid id_surface in controller_listener_surface\n");
1224         return;
1225     }
1226
1227     ctx_surf = calloc(1, sizeof *ctx_surf);
1228     if (ctx_surf == NULL) {
1229         fprintf(stderr, "Failed to allocate memory for surface_context\n");
1230         return;
1231     }
1232
1233     ctx_surf->controller = ivi_controller_surface_create(
1234                                controller, id_surface);
1235     if (ctx_surf->controller == NULL) {
1236         fprintf(stderr, "Failed to create controller surface\n");
1237         return;
1238     }
1239     ctx_surf->id_surface = id_surface;
1240     ctx_surf->prop.inputDevicesAcceptance = ILM_INPUT_DEVICE_ALL;
1241     ctx_surf->ctx = ctx;
1242
1243     wl_list_init(&ctx_surf->link);
1244     wl_list_insert(&ctx->list_surface, &ctx_surf->link);
1245     ivi_controller_surface_add_listener(ctx_surf->controller,
1246                                         &controller_surface_listener, ctx_surf);
1247 }
1248
1249 static void
1250 controller_listener_error_for_child(void *data,
1251                           struct ivi_controller *ivi_controller,
1252                           int32_t object_id,
1253                           int32_t object_type,
1254                           int32_t error_code,
1255                           const char *error_text)
1256 {
1257     (void)data;
1258     (void)ivi_controller;
1259     (void)object_id;
1260     (void)object_type;
1261     (void)error_code;
1262     (void)error_text;
1263 }
1264
1265 static struct ivi_controller_listener controller_listener_for_child = {
1266     controller_listener_screen_for_child,
1267     controller_listener_layer_for_child,
1268     controller_listener_surface_for_child,
1269     controller_listener_error_for_child
1270 };
1271
1272 static void
1273 controller_listener_screen_for_main(void *data,
1274                            struct ivi_controller *ivi_controller,
1275                            uint32_t id_screen,
1276                            struct ivi_controller_screen *controller_screen)
1277 {
1278     struct wayland_context *ctx = data;
1279     struct screen_context *ctx_screen;
1280     (void)ivi_controller;
1281
1282     ctx_screen = get_screen_context_by_serverid(ctx, id_screen);
1283     if (ctx_screen == NULL) {
1284         fprintf(stderr, "Failed to allocate memory for screen_context\n");
1285         return;
1286     }
1287     ctx_screen->controller = controller_screen;
1288 }
1289
1290 static struct ivi_controller_listener controller_listener_for_main = {
1291     controller_listener_screen_for_main,
1292     controller_listener_layer_for_child,
1293     controller_listener_surface_for_child,
1294     controller_listener_error_for_child
1295 };
1296
1297 static void
1298 registry_handle_control_for_child(void *data,
1299                        struct wl_registry *registry,
1300                        uint32_t name, const char *interface,
1301                        uint32_t version)
1302 {
1303     struct wayland_context *ctx = data;
1304     (void)version;
1305
1306     if (strcmp(interface, "ivi_controller") == 0) {
1307         ctx->controller = wl_registry_bind(registry, name,
1308                                            &ivi_controller_interface, 1);
1309         if (ctx->controller == NULL) {
1310             fprintf(stderr, "Failed to registry bind ivi_controller\n");
1311             return;
1312         }
1313         if (ivi_controller_add_listener(ctx->controller,
1314                                        &controller_listener_for_child,
1315                                        ctx)) {
1316             fprintf(stderr, "Failed to add ivi_controller listener\n");
1317             return;
1318         }
1319     } else if (strcmp(interface, "wl_output") == 0) {
1320         struct screen_context *ctx_scrn = calloc(1, sizeof *ctx_scrn);
1321         if (ctx_scrn == NULL) {
1322             fprintf(stderr, "Failed to allocate memory for screen_context\n");
1323             return;
1324         }
1325         wl_list_init(&ctx_scrn->link);
1326         ctx_scrn->output = wl_registry_bind(registry, name,
1327                                            &wl_output_interface, 1);
1328         if (ctx_scrn->output == NULL) {
1329             free(ctx_scrn);
1330             fprintf(stderr, "Failed to registry bind wl_output\n");
1331             return;
1332         }
1333
1334         if (wl_output_add_listener(ctx_scrn->output,
1335                                    &output_listener,
1336                                    ctx_scrn)) {
1337             free(ctx_scrn);
1338             fprintf(stderr, "Failed to add wl_output listener\n");
1339             return;
1340         }
1341
1342         ctx_scrn->id_screen = ctx->num_screen;
1343         ctx->num_screen++;
1344         wl_list_init(&ctx_scrn->order.list_layer);
1345         wl_list_insert(&ctx->list_screen, &ctx_scrn->link);
1346     }
1347 }
1348
1349 static const struct wl_registry_listener
1350 registry_control_listener_for_child = {
1351     registry_handle_control_for_child,
1352     NULL
1353 };
1354
1355 static void
1356 registry_handle_control_for_main(void *data,
1357                        struct wl_registry *registry,
1358                        uint32_t name, const char *interface,
1359                        uint32_t version)
1360 {
1361     struct ilm_control_context *ctx = data;
1362     (void)version;
1363
1364     if (strcmp(interface, "ivi_controller") == 0) {
1365         ctx->main_ctx.controller = wl_registry_bind(registry, name,
1366                                            &ivi_controller_interface, 1);
1367         if (ctx->main_ctx.controller == NULL) {
1368             fprintf(stderr, "Failed to registry bind ivi_controller\n");
1369             return;
1370         }
1371         if (ivi_controller_add_listener(ctx->main_ctx.controller,
1372                                        &controller_listener_for_main,
1373                                        &ctx->main_ctx)) {
1374             fprintf(stderr, "Failed to add ivi_controller listener\n");
1375             return;
1376         }
1377     } else if (strcmp(interface, "wl_output") == 0) {
1378
1379         struct screen_context *ctx_scrn = calloc(1, sizeof *ctx_scrn);
1380         struct wl_proxy *pxy = NULL;
1381
1382         if (ctx_scrn == NULL) {
1383             fprintf(stderr, "Failed to allocate memory for screen_context\n");
1384             return;
1385         }
1386         wl_list_init(&ctx_scrn->link);
1387         ctx_scrn->output = wl_registry_bind(registry, name,
1388                                            &wl_output_interface, 1);
1389         if (ctx_scrn->output == NULL) {
1390             free(ctx_scrn);
1391             fprintf(stderr, "Failed to registry bind wl_output\n");
1392             return;
1393         }
1394
1395         if (wl_output_add_listener(ctx_scrn->output,
1396                                    &output_listener,
1397                                    ctx_scrn)) {
1398             free(ctx_scrn);
1399             fprintf(stderr, "Failed to add wl_output listener\n");
1400             return;
1401         }
1402
1403         pxy = (struct wl_proxy*)ctx_scrn->output;
1404         ctx_scrn->id_from_server = wl_proxy_get_id(pxy);
1405         ctx_scrn->id_screen = ctx->num_screen;
1406         ctx->num_screen++;
1407         ctx_scrn->controller = NULL;
1408         ctx_scrn->prop.layerCount = 0;
1409         ctx_scrn->prop.layerIds = NULL;
1410         ctx_scrn->prop.harwareLayerCount = 0;
1411         ctx_scrn->prop.screenWidth = 0;
1412         ctx_scrn->prop.screenHeight = 0;
1413         wl_list_init(&ctx_scrn->order.list_layer);
1414         wl_list_insert(&ctx->main_ctx.list_screen, &ctx_scrn->link);
1415     }
1416 }
1417
1418 static const struct wl_registry_listener
1419 registry_control_listener_for_main = {
1420     registry_handle_control_for_main,
1421     NULL
1422 };
1423
1424 static struct ilm_control_context ilm_context = {0};
1425
1426 static void
1427 destroy_control_resources(void)
1428 {
1429     struct ilm_control_context *ctx = &ilm_context;
1430     struct screen_context *ctx_scrn;
1431     struct screen_context *next;
1432
1433     wl_list_for_each_safe(ctx_scrn, next, &ctx->main_ctx.list_screen, link) {
1434         if (ctx_scrn->output != NULL) {
1435             wl_list_remove(&ctx_scrn->link);
1436             wl_output_destroy(ctx_scrn->output);
1437             free(ctx_scrn);
1438         }
1439     }
1440     if (ctx->main_ctx.controller != NULL) {
1441         ivi_controller_destroy(ctx->main_ctx.controller);
1442         ctx->main_ctx.controller = NULL;
1443     }
1444
1445     wl_display_flush(ctx->main_ctx.display);
1446
1447     wl_event_queue_destroy(ctx->main_ctx.queue);
1448     ctx->main_ctx.queue = NULL;
1449
1450     if (0 != pthread_mutex_destroy(&ctx->mutex)) {
1451         fprintf(stderr, "failed to destroy pthread_mutex\n");
1452     }
1453 }
1454
1455 static void
1456 wayland_destroy(void)
1457 {
1458     struct ilm_control_context *ctx = &ilm_context;
1459     lock_context(ctx);
1460     ctx->valid = 0;
1461     unlock_context(ctx);
1462     void* threadRetVal = NULL;
1463     pthread_cancel(ctx->thread);
1464     if (0 != pthread_join(ctx->thread, &threadRetVal)) {
1465         fprintf(stderr, "failed to join control thread\n");
1466     }
1467     destroy_control_resources();
1468 }
1469
1470 static ilmErrorTypes
1471 wayland_init(t_ilm_nativedisplay nativedisplay)
1472 {
1473     struct ilm_control_context *ctx = &ilm_context;
1474
1475     if (ctx->valid != 0)
1476     {
1477         fprintf(stderr, "Already initialized!\n");
1478         return ILM_FAILED;
1479     }
1480
1481     if (nativedisplay == 0) {
1482         return ILM_ERROR_INVALID_ARGUMENTS;
1483     }
1484
1485     memset(ctx, 0, sizeof *ctx);
1486
1487     ctx->main_ctx.display = (struct wl_display*)nativedisplay;
1488
1489     wl_list_init(&ctx->main_ctx.list_screen);
1490     wl_list_init(&ctx->main_ctx.list_layer);
1491     wl_list_init(&ctx->main_ctx.list_surface);
1492
1493     {
1494        pthread_mutexattr_t a;
1495        if (pthread_mutexattr_init(&a) != 0)
1496        {
1497           return ILM_FAILED;
1498        }
1499
1500        if (pthread_mutexattr_settype(&a, PTHREAD_MUTEX_RECURSIVE) != 0)
1501        {
1502           pthread_mutexattr_destroy(&a);
1503           return ILM_FAILED;
1504        }
1505
1506        if (pthread_mutex_init(&ctx->mutex, &a) != 0)
1507        {
1508            pthread_mutexattr_destroy(&a);
1509            fprintf(stderr, "failed to initialize pthread_mutex\n");
1510            return ILM_FAILED;
1511        }
1512
1513        pthread_mutexattr_destroy(&a);
1514     }
1515
1516     ctx->internal_id_surface = 0;
1517     ctx->num_screen = 0;
1518
1519     return init_control() == 0 ? ILM_SUCCESS : ILM_FAILED;
1520 }
1521
1522 static void*
1523 control_thread(void *p_ret)
1524 {
1525     struct ilm_control_context *ctx = &ilm_context;
1526     struct wayland_context *main_ctx = &ctx->main_ctx;
1527
1528     (void) p_ret;
1529
1530     while (1)
1531     {
1532         lock_context(ctx);
1533         int valid = ctx->valid;
1534         unlock_context(ctx);
1535
1536         if (valid != 1)
1537         {
1538             break;
1539         }
1540
1541         lock_context(ctx);
1542         while (wl_display_prepare_read_queue(main_ctx->display, main_ctx->queue) != 0)
1543         {
1544             wl_display_dispatch_queue_pending(main_ctx->display, main_ctx->queue);
1545         }
1546         unlock_context(ctx);
1547
1548         if (wl_display_flush(main_ctx->display) == -1)
1549         {
1550             break;
1551         }
1552
1553         struct pollfd pfd;
1554
1555         pfd.fd = wl_display_get_fd(main_ctx->display);
1556         pfd.events = POLLIN;
1557         pfd.revents = 0;
1558
1559         if (poll(&pfd, 1, -1) != -1 && (pfd.revents & POLLIN))
1560         {
1561             wl_display_read_events(main_ctx->display);
1562
1563             lock_context(ctx);
1564             int ret = wl_display_dispatch_queue_pending(main_ctx->display, main_ctx->queue);
1565             unlock_context(ctx);
1566
1567             if (ret == -1)
1568             {
1569                 break;
1570             }
1571         }
1572         else
1573         {
1574             wl_display_cancel_read(main_ctx->display);
1575         }
1576     }
1577
1578     return NULL;
1579 }
1580
1581 static int
1582 init_control(void)
1583 {
1584     struct ilm_control_context *ctx = &ilm_context;
1585     struct wayland_context *main_ctx = &ctx->main_ctx;
1586     int wait_count = 0;
1587     int ret = 0;
1588
1589     wl_list_init(&ctx->list_nativehandle);
1590
1591     main_ctx->queue = wl_display_create_queue(main_ctx->display);
1592
1593     /* registry_add_listener for request by ivi-controller */
1594     main_ctx->registry = wl_display_get_registry(main_ctx->display);
1595     if (main_ctx->registry == NULL) {
1596         fprintf(stderr, "Failed to get registry\n");
1597         return -1;
1598     }
1599     wl_proxy_set_queue((void*)main_ctx->registry, main_ctx->queue);
1600
1601     if (wl_registry_add_listener(main_ctx->registry,
1602                              &registry_control_listener_for_main, ctx)) {
1603         fprintf(stderr, "Failed to add registry listener\n");
1604         return -1;
1605     }
1606
1607     display_roundtrip_queue(main_ctx->display, main_ctx->queue);
1608     display_roundtrip_queue(main_ctx->display, main_ctx->queue);
1609
1610     ctx->valid = 1;
1611
1612     ret = pthread_create(&ctx->thread, NULL, control_thread, NULL);
1613
1614     if (ret != 0) {
1615         fprintf(stderr, "Failed to start internal receive thread. returned %d\n", ret);
1616         return -1;
1617     }
1618
1619     return 0;
1620 }
1621
1622 static struct ilm_control_context*
1623 get_instance(void)
1624 {
1625     struct ilm_control_context *ctx = &ilm_context;
1626     lock_context(ctx);
1627     display_roundtrip_queue(ctx->main_ctx.display, ctx->main_ctx.queue);
1628     unlock_context(ctx);
1629     return ctx;
1630 }
1631
1632 static uint32_t
1633 gen_layer_id(struct ilm_control_context *ctx)
1634 {
1635     struct layer_context *ctx_layer = NULL;
1636     do {
1637         int found = 0;
1638         if (wl_list_length(&ctx->main_ctx.list_layer) == 0) {
1639             ctx->internal_id_layer++;
1640             return ctx->internal_id_layer;
1641         }
1642         wl_list_for_each(ctx_layer, &ctx->main_ctx.list_layer, link) {
1643             if (ctx_layer->id_layer == ctx->internal_id_layer) {
1644                 found = 1;
1645                 break;
1646             }
1647
1648             if (found == 0) {
1649                 return ctx->internal_id_layer;
1650             }
1651         }
1652         ctx->internal_id_layer++;
1653     } while(1);
1654 }
1655
1656 static struct surface_context*
1657 get_surface_context(struct wayland_context *ctx,
1658                           uint32_t id_surface)
1659 {
1660     struct surface_context *ctx_surf = NULL;
1661
1662     if (ctx->controller == NULL) {
1663         fprintf(stderr, "controller is not initialized in ilmControl\n");
1664         return NULL;
1665     }
1666
1667     wl_list_for_each(ctx_surf, &ctx->list_surface, link) {
1668         if (ctx_surf->id_surface == id_surface) {
1669             return ctx_surf;
1670         }
1671     }
1672
1673     fprintf(stderr, "failed to get surface context in ilmControl\n");
1674     return NULL;
1675 }
1676
1677 static struct screen_context*
1678 get_screen_context_by_id(struct wayland_context *ctx, uint32_t id_screen)
1679 {
1680     struct screen_context *ctx_scrn = NULL;
1681
1682     if (ctx->controller == NULL) {
1683         fprintf(stderr, "get_screen_context_by_id: controller is NULL\n");
1684         return NULL;
1685     }
1686
1687     wl_list_for_each(ctx_scrn, &ctx->list_screen, link) {
1688         if (ctx_scrn->id_screen == id_screen) {
1689             return ctx_scrn;
1690         }
1691     }
1692     return NULL;
1693 }
1694
1695 static ilmErrorTypes
1696 wayland_getPropertiesOfLayer(t_ilm_uint layerID,
1697                          struct ilmLayerProperties* pLayerProperties)
1698 {
1699     ilmErrorTypes returnValue = ILM_FAILED;
1700     struct ilm_control_context *ctx = get_instance();
1701     lock_context(ctx);
1702     struct layer_context *ctx_layer = NULL;
1703
1704     if (pLayerProperties != NULL) {
1705
1706         ctx_layer = (struct layer_context*)
1707                     wayland_controller_get_layer_context(
1708                         &ctx->main_ctx, (uint32_t)layerID);
1709
1710         if (ctx_layer != NULL) {
1711             *pLayerProperties = ctx_layer->prop;
1712             returnValue = ILM_SUCCESS;
1713         }
1714     }
1715
1716     unlock_context(ctx);
1717     return returnValue;
1718 }
1719
1720 static void
1721 create_layerids(struct screen_context *ctx_screen,
1722                 t_ilm_layer **layer_ids, t_ilm_uint *layer_count)
1723 {
1724     struct layer_context *ctx_layer = NULL;
1725     t_ilm_layer *ids = NULL;
1726
1727     *layer_count = wl_list_length(&ctx_screen->order.list_layer);
1728     if (*layer_count == 0) {
1729         *layer_ids = NULL;
1730         return;
1731     }
1732
1733     *layer_ids = malloc(*layer_count * sizeof(t_ilm_layer));
1734     if (*layer_ids == NULL) {
1735         fprintf(stderr, "memory insufficient for layerids\n");
1736         *layer_count = 0;
1737         return;
1738     }
1739
1740     ids = *layer_ids;
1741     wl_list_for_each_reverse(ctx_layer, &ctx_screen->order.list_layer, order.link) {
1742         *ids = (t_ilm_layer)ctx_layer->id_layer;
1743         ids++;
1744     }
1745 }
1746
1747 static ilmErrorTypes
1748 wayland_getPropertiesOfScreen(t_ilm_display screenID,
1749                               struct ilmScreenProperties* pScreenProperties)
1750 {
1751     ilmErrorTypes returnValue = ILM_FAILED;
1752     struct ilm_control_context *ctx = get_instance();
1753     lock_context(ctx);
1754
1755     if (pScreenProperties != NULL) {
1756         struct screen_context *ctx_screen = NULL;
1757         ctx_screen = get_screen_context_by_id(&ctx->main_ctx, (uint32_t)screenID);
1758         if (ctx_screen != NULL) {
1759             *pScreenProperties = ctx_screen->prop;
1760             create_layerids(ctx_screen, &pScreenProperties->layerIds,
1761                                         &pScreenProperties->layerCount);
1762             returnValue = ILM_SUCCESS;
1763         }
1764     }
1765     else {
1766         pScreenProperties->layerCount = 0;
1767         pScreenProperties->harwareLayerCount = 0;
1768         pScreenProperties->layerIds = NULL;
1769         pScreenProperties->screenWidth = 0;
1770         pScreenProperties->screenHeight = 0;
1771     }
1772
1773     unlock_context(ctx);
1774     return returnValue;
1775 }
1776
1777 static ilmErrorTypes
1778 wayland_getNumberOfHardwareLayers(t_ilm_uint screenID,
1779                                   t_ilm_uint* pNumberOfHardwareLayers)
1780 {
1781     (void)screenID;
1782     /* Not supported */
1783     if (pNumberOfHardwareLayers != NULL) {
1784         *pNumberOfHardwareLayers = 0;
1785         return ILM_SUCCESS;
1786     } else {
1787         return ILM_FAILED;
1788     }
1789 }
1790
1791 static ilmErrorTypes
1792 wayland_getScreenIDs(t_ilm_uint* pNumberOfIDs, t_ilm_uint** ppIDs)
1793 {
1794     ilmErrorTypes returnValue = ILM_FAILED;
1795     struct ilm_control_context *ctx = get_instance();
1796     lock_context(ctx);
1797
1798     if ((pNumberOfIDs != NULL) && (ppIDs != NULL)) {
1799         struct screen_context *ctx_scrn = NULL;
1800         t_ilm_uint length = wl_list_length(&ctx->main_ctx.list_screen);
1801         *pNumberOfIDs = 0;
1802
1803         *ppIDs = (t_ilm_uint*)malloc(length * sizeof *ppIDs);
1804         if (*ppIDs != NULL) {
1805             t_ilm_uint* ids = *ppIDs;
1806             wl_list_for_each(ctx_scrn, &ctx->main_ctx.list_screen, link) {
1807                 *ids = ctx_scrn->id_screen;
1808                 ids++;
1809             }
1810             *pNumberOfIDs = length;
1811
1812             returnValue = ILM_SUCCESS;
1813         }
1814     }
1815
1816     unlock_context(ctx);
1817     return returnValue;
1818 }
1819
1820 static ilmErrorTypes
1821 wayland_getLayerIDs(t_ilm_int* pLength, t_ilm_layer** ppArray)
1822 {
1823     ilmErrorTypes returnValue = ILM_FAILED;
1824     struct ilm_control_context *ctx = get_instance();
1825     lock_context(ctx);
1826
1827     if ((pLength != NULL) && (ppArray != NULL)) {
1828         struct layer_context *ctx_layer = NULL;
1829         t_ilm_uint length = wl_list_length(&ctx->main_ctx.list_layer);
1830         *pLength = 0;
1831
1832         *ppArray = (t_ilm_layer*)malloc(length * sizeof *ppArray);
1833         if (*ppArray != NULL) {
1834             // compositor sends layers in opposite order
1835             // write ids from back to front to turn them around
1836             t_ilm_layer* ids = *ppArray;
1837             wl_list_for_each_reverse(ctx_layer, &ctx->main_ctx.list_layer, link)
1838             {
1839                 *ids = ctx_layer->id_layer;
1840                 ++ids;
1841             }
1842             *pLength = length;
1843
1844             returnValue = ILM_SUCCESS;
1845         }
1846     }
1847
1848     unlock_context(ctx);
1849     return returnValue;
1850 }
1851
1852 static ilmErrorTypes
1853 wayland_getLayerIDsOnScreen(t_ilm_uint screenId,
1854                             t_ilm_int* pLength,
1855                             t_ilm_layer** ppArray)
1856 {
1857     ilmErrorTypes returnValue = ILM_FAILED;
1858     struct ilm_control_context *ctx = get_instance();
1859     lock_context(ctx);
1860
1861     if ((pLength != NULL) && (ppArray != NULL)) {
1862         struct screen_context *ctx_screen = NULL;
1863         ctx_screen = get_screen_context_by_id(&ctx->main_ctx, screenId);
1864         if (ctx_screen != NULL) {
1865             struct layer_context *ctx_layer = NULL;
1866             t_ilm_int length = wl_list_length(&ctx_screen->order.list_layer);
1867
1868             if (0 < length)
1869             {
1870                 *ppArray = (t_ilm_layer*)malloc(length * sizeof *ppArray);
1871                 if (*ppArray != NULL) {
1872                     // compositor sends layers in opposite order
1873                     // write ids from back to front to turn them around
1874                     t_ilm_layer* ids = *ppArray;
1875                     wl_list_for_each_reverse(ctx_layer, &ctx_screen->order.list_layer, order.link)
1876                     {
1877                         *ids = ctx_layer->id_layer;
1878                         ++ids;
1879                     }
1880
1881                 }
1882             }
1883             else
1884             {
1885                 *ppArray = NULL;
1886             }
1887
1888             *pLength = length;
1889             returnValue = ILM_SUCCESS;
1890         }
1891     }
1892
1893     unlock_context(ctx);
1894     return returnValue;
1895 }
1896
1897 static ilmErrorTypes
1898 wayland_getSurfaceIDs(t_ilm_int* pLength, t_ilm_surface** ppArray)
1899 {
1900     ilmErrorTypes returnValue = ILM_FAILED;
1901     struct ilm_control_context *ctx = get_instance();
1902     lock_context(ctx);
1903
1904     if ((pLength != NULL) && (ppArray != NULL)) {
1905         struct surface_context *ctx_surf = NULL;
1906         t_ilm_uint length = wl_list_length(&ctx->main_ctx.list_surface);
1907         *pLength = 0;
1908
1909         *ppArray = (t_ilm_surface*)malloc(length * sizeof *ppArray);
1910         if (*ppArray != NULL) {
1911             t_ilm_surface* ids = *ppArray;
1912             wl_list_for_each_reverse(ctx_surf, &ctx->main_ctx.list_surface, link) {
1913                 *ids = ctx_surf->id_surface;
1914                 ids++;
1915             }
1916             *pLength = length;
1917
1918             returnValue = ILM_SUCCESS;
1919         }
1920     }
1921
1922     unlock_context(ctx);
1923     return returnValue;
1924 }
1925
1926 static ilmErrorTypes
1927 wayland_getSurfaceIDsOnLayer(t_ilm_layer layer,
1928                              t_ilm_int* pLength,
1929                              t_ilm_surface** ppArray)
1930 {
1931     struct ilm_control_context *ctx = get_instance();
1932     lock_context(ctx);
1933     struct layer_context *ctx_layer = NULL;
1934     struct surface_context *ctx_surf = NULL;
1935     t_ilm_uint length = 0;
1936     t_ilm_surface* ids = NULL;
1937
1938     if ((pLength == NULL) || (ppArray == NULL)) {
1939         unlock_context(ctx);
1940         return ILM_FAILED;
1941     }
1942
1943     ctx_layer = (struct layer_context*)wayland_controller_get_layer_context(
1944                     &ctx->main_ctx, (uint32_t)layer);
1945
1946     if (ctx_layer == NULL) {
1947         unlock_context(ctx);
1948         return ILM_FAILED;
1949     }
1950
1951     length = wl_list_length(&ctx_layer->order.list_surface);
1952     *ppArray = (t_ilm_surface*)malloc(length * sizeof *ppArray);
1953     if (*ppArray == NULL) {
1954         unlock_context(ctx);
1955         return ILM_FAILED;
1956     }
1957
1958     ids = *ppArray;
1959     wl_list_for_each_reverse(ctx_surf, &ctx_layer->order.list_surface, order.link) {
1960         *ids = (t_ilm_surface)ctx_surf->id_surface;
1961         ids++;
1962     }
1963     *pLength = length;
1964
1965     unlock_context(ctx);
1966     return ILM_SUCCESS;
1967 }
1968
1969 static int create_controller_layer(struct wayland_context *ctx, t_ilm_uint width, t_ilm_uint height, t_ilm_layer layerid)
1970 {
1971      struct layer_context *ctx_layer = calloc(1, sizeof *ctx_layer);
1972      if (ctx_layer == NULL) {
1973          fprintf(stderr, "Failed to allocate memory for layer_context\n");
1974          return -1;
1975      }
1976
1977      ctx_layer->controller = ivi_controller_layer_create(
1978                                  ctx->controller,
1979                                  layerid, width, height);
1980      if (ctx_layer->controller == NULL) {
1981          fprintf(stderr, "Failed to create layer\n");
1982          free(ctx_layer);
1983          return -1;
1984      }
1985      ctx_layer->id_layer = layerid;
1986      ctx_layer->ctx = ctx;
1987
1988      wl_list_init(&ctx_layer->link);
1989      wl_list_insert(&ctx->list_layer, &ctx_layer->link);
1990      wl_list_init(&ctx_layer->order.link);
1991      wl_list_init(&ctx_layer->order.list_surface);
1992
1993      ivi_controller_layer_add_listener(ctx_layer->controller,
1994                                    &controller_layer_listener, ctx_layer);
1995
1996      return 0;
1997 }
1998
1999 static ilmErrorTypes
2000 wayland_layerCreateWithDimension(t_ilm_layer* pLayerId,
2001                                  t_ilm_uint width,
2002                                  t_ilm_uint height)
2003 {
2004     ilmErrorTypes returnValue = ILM_FAILED;
2005     struct ilm_control_context *ctx = get_instance();
2006     lock_context(ctx);
2007     uint32_t layerid = 0;
2008     int32_t is_inside = 0;
2009
2010     do {
2011         if (pLayerId == NULL) {
2012             break;
2013         }
2014
2015         if (*pLayerId != INVALID_ID) {
2016             /* Return failed, if layerid is already inside list_layer */
2017             is_inside = wayland_controller_is_inside_layer_list(
2018                             &ctx->main_ctx.list_layer, *pLayerId);
2019             if (0 != is_inside) {
2020                 fprintf(stderr, "layerid=%d is already used.\n", *pLayerId);
2021                 break;
2022             }
2023             layerid = *pLayerId;
2024         }
2025         else {
2026             /* Generate ID, if layerid is INVALID_ID */
2027             layerid = gen_layer_id(ctx);
2028             *pLayerId = layerid;
2029         }
2030
2031         if (create_controller_layer(&ctx->main_ctx, width, height, layerid) == 0)
2032         {
2033            returnValue = ILM_SUCCESS;
2034         }
2035     } while(0);
2036
2037     unlock_context(ctx);
2038     return returnValue;
2039 }
2040
2041 static ilmErrorTypes
2042 wayland_layerRemove(t_ilm_layer layerId)
2043 {
2044     ilmErrorTypes returnValue = ILM_FAILED;
2045     struct ilm_control_context *ctx = get_instance();
2046     lock_context(ctx);
2047     struct layer_context *ctx_layer = NULL;
2048     struct layer_context *ctx_next = NULL;
2049
2050     wl_list_for_each_safe(ctx_layer, ctx_next,
2051             &ctx->main_ctx.list_layer, link) {
2052         if (ctx_layer->id_layer == layerId) {
2053             ivi_controller_layer_destroy(ctx_layer->controller, 1);
2054
2055             wl_list_remove(&ctx_layer->link);
2056             free(ctx_layer);
2057
2058             returnValue = ILM_SUCCESS;
2059             break;
2060         }
2061     }
2062
2063     unlock_context(ctx);
2064     return returnValue;
2065 }
2066
2067 static ilmErrorTypes
2068 wayland_layerGetType(t_ilm_layer layerId, ilmLayerType* pLayerType)
2069 {
2070     if (!pLayerType)
2071     {
2072        return ILM_ERROR_INVALID_ARGUMENTS;
2073     }
2074
2075     struct ilm_control_context *ctx = get_instance();
2076     lock_context(ctx);
2077
2078     *pLayerType = wayland_controller_is_inside_layer_list(&ctx->main_ctx.list_layer, layerId) ?
2079        ILM_LAYERTYPE_SOFTWARE2D :
2080        ILM_LAYERTYPE_UNKNOWN;
2081
2082     unlock_context(ctx);
2083     return ILM_SUCCESS; // even if non existent?
2084 }
2085
2086 static ilmErrorTypes
2087 wayland_layerSetVisibility(t_ilm_layer layerId, t_ilm_bool newVisibility)
2088 {
2089     ilmErrorTypes returnValue = ILM_FAILED;
2090     struct ilm_control_context *ctx = get_instance();
2091     lock_context(ctx);
2092     struct layer_context *ctx_layer = NULL;
2093
2094     ctx_layer = (struct layer_context*)wayland_controller_get_layer_context(
2095                     &ctx->main_ctx, (uint32_t)layerId);
2096
2097     if (ctx_layer != NULL) {
2098         uint32_t visibility = 0;
2099         if (newVisibility == ILM_TRUE) {
2100             visibility = 1;
2101         }
2102         ivi_controller_layer_set_visibility(ctx_layer->controller,
2103                                             visibility);
2104         returnValue = ILM_SUCCESS;
2105     }
2106
2107     unlock_context(ctx);
2108     return returnValue;
2109 }
2110
2111 static ilmErrorTypes
2112 wayland_layerGetVisibility(t_ilm_layer layerId, t_ilm_bool *pVisibility)
2113 {
2114     ilmErrorTypes returnValue = ILM_FAILED;
2115     struct ilm_control_context *ctx = get_instance();
2116     lock_context(ctx);
2117
2118     if (pVisibility != NULL) {
2119         struct layer_context *ctx_layer = NULL;
2120
2121         ctx_layer = (struct layer_context*)
2122                     wayland_controller_get_layer_context(
2123                         &ctx->main_ctx, (uint32_t)layerId);
2124
2125         if (ctx_layer != NULL) {
2126             *pVisibility = ctx_layer->prop.visibility;
2127             returnValue = ILM_SUCCESS;
2128         }
2129     }
2130
2131     unlock_context(ctx);
2132     return returnValue;
2133 }
2134
2135 static ilmErrorTypes
2136 wayland_layerSetOpacity(t_ilm_layer layerId, t_ilm_float opacity)
2137 {
2138     ilmErrorTypes returnValue = ILM_FAILED;
2139     struct ilm_control_context *ctx = get_instance();
2140     lock_context(ctx);
2141     struct layer_context *ctx_layer = NULL;
2142
2143     ctx_layer = (struct layer_context*)wayland_controller_get_layer_context(
2144                     &ctx->main_ctx, (uint32_t)layerId);
2145
2146     if (ctx_layer != NULL) {
2147         wl_fixed_t opacity_fixed = wl_fixed_from_double((double)opacity);
2148         ivi_controller_layer_set_opacity(ctx_layer->controller,
2149                                          opacity_fixed);
2150         returnValue = ILM_SUCCESS;
2151     }
2152
2153     unlock_context(ctx);
2154     return returnValue;
2155 }
2156
2157 static ilmErrorTypes
2158 wayland_layerGetOpacity(t_ilm_layer layerId, t_ilm_float *pOpacity)
2159 {
2160     ilmErrorTypes returnValue = ILM_FAILED;
2161     struct ilm_control_context *ctx = get_instance();
2162     lock_context(ctx);
2163
2164     if (pOpacity != NULL) {
2165         struct layer_context *ctx_layer = NULL;
2166
2167         ctx_layer = (struct layer_context*)
2168                     wayland_controller_get_layer_context(
2169                         &ctx->main_ctx, (uint32_t)layerId);
2170
2171         if (ctx_layer != NULL) {
2172             *pOpacity = ctx_layer->prop.opacity;
2173             returnValue = ILM_SUCCESS;
2174         }
2175     }
2176
2177     unlock_context(ctx);
2178     return returnValue;
2179 }
2180
2181 static ilmErrorTypes
2182 wayland_layerSetSourceRectangle(t_ilm_layer layerId,
2183                                 t_ilm_uint x, t_ilm_uint y,
2184                                 t_ilm_uint width, t_ilm_uint height)
2185 {
2186     ilmErrorTypes returnValue = ILM_FAILED;
2187     struct ilm_control_context *ctx = get_instance();
2188     lock_context(ctx);
2189     struct layer_context *ctx_layer = NULL;
2190
2191     ctx_layer = (struct layer_context*)wayland_controller_get_layer_context(
2192                     &ctx->main_ctx, (uint32_t)layerId);
2193
2194     if (ctx_layer != NULL) {
2195         ivi_controller_layer_set_source_rectangle(ctx_layer->controller,
2196                                                   (uint32_t)x,
2197                                                   (uint32_t)y,
2198                                                   (uint32_t)width,
2199                                                   (uint32_t)height);
2200         returnValue = ILM_SUCCESS;
2201     }
2202
2203     unlock_context(ctx);
2204     return returnValue;
2205 }
2206
2207 static ilmErrorTypes
2208 wayland_layerSetDestinationRectangle(t_ilm_layer layerId,
2209                                  t_ilm_int x, t_ilm_int y,
2210                                  t_ilm_int width, t_ilm_int height)
2211 {
2212     ilmErrorTypes returnValue = ILM_FAILED;
2213     struct ilm_control_context *ctx = get_instance();
2214     lock_context(ctx);
2215     struct layer_context *ctx_layer = NULL;
2216
2217     ctx_layer = (struct layer_context*)wayland_controller_get_layer_context(
2218                     &ctx->main_ctx, (uint32_t)layerId);
2219     if (ctx_layer != NULL) {
2220         ivi_controller_layer_set_destination_rectangle(
2221                                          ctx_layer->controller,
2222                                          (uint32_t)x, (uint32_t)y,
2223                                          (uint32_t)width,
2224                                          (uint32_t)height);
2225         returnValue = ILM_SUCCESS;
2226     }
2227
2228     unlock_context(ctx);
2229     return returnValue;
2230 }
2231
2232 static ilmErrorTypes
2233 wayland_layerGetDimension(t_ilm_layer layerId, t_ilm_uint *pDimension)
2234 {
2235     ilmErrorTypes returnValue = ILM_FAILED;
2236     struct ilm_control_context *ctx = get_instance();
2237     lock_context(ctx);
2238     struct layer_context *ctx_layer = NULL;
2239
2240     if (pDimension != NULL) {
2241         ctx_layer = (struct layer_context*)
2242                     wayland_controller_get_layer_context(
2243                         &ctx->main_ctx, (uint32_t)layerId);
2244         if (ctx_layer != NULL) {
2245             *pDimension = ctx_layer->prop.destWidth;
2246             *(pDimension + 1) = ctx_layer->prop.destHeight;
2247             returnValue = ILM_SUCCESS;
2248         }
2249     }
2250
2251     unlock_context(ctx);
2252     return returnValue;
2253 }
2254
2255 static ilmErrorTypes
2256 wayland_layerSetDimension(t_ilm_layer layerId, t_ilm_uint *pDimension)
2257 {
2258     ilmErrorTypes returnValue = ILM_FAILED;
2259     struct ilm_control_context *ctx = get_instance();
2260     lock_context(ctx);
2261     struct layer_context *ctx_layer = NULL;
2262
2263     if (pDimension != NULL) {
2264         ctx_layer = (struct layer_context*)
2265                     wayland_controller_get_layer_context(
2266                         &ctx->main_ctx, (uint32_t)layerId);
2267         if (ctx_layer != NULL) {
2268             ivi_controller_layer_set_destination_rectangle(
2269                 ctx_layer->controller,
2270                 ctx_layer->prop.destX, ctx_layer->prop.destY,
2271                 (int32_t)*pDimension, (int32_t)*(pDimension + 1));
2272             returnValue = ILM_SUCCESS;
2273         }
2274     }
2275
2276     unlock_context(ctx);
2277     return returnValue;
2278 }
2279
2280 static ilmErrorTypes
2281 wayland_layerGetPosition(t_ilm_layer layerId, t_ilm_uint *pPosition)
2282 {
2283     ilmErrorTypes returnValue = ILM_FAILED;
2284     struct ilm_control_context *ctx = get_instance();
2285     lock_context(ctx);
2286     struct layer_context *ctx_layer = NULL;
2287
2288     if (pPosition != NULL) {
2289         ctx_layer = (struct layer_context*)
2290                     wayland_controller_get_layer_context(
2291                         &ctx->main_ctx, (uint32_t)layerId);
2292         if (ctx_layer != NULL) {
2293             *pPosition = ctx_layer->prop.destX;
2294             *(pPosition + 1) = ctx_layer->prop.destY;
2295             returnValue = ILM_SUCCESS;
2296         }
2297     }
2298
2299     unlock_context(ctx);
2300     return returnValue;
2301 }
2302
2303 static ilmErrorTypes
2304 wayland_layerSetPosition(t_ilm_layer layerId, t_ilm_uint *pPosition)
2305 {
2306     ilmErrorTypes returnValue = ILM_FAILED;
2307     struct ilm_control_context *ctx = get_instance();
2308     lock_context(ctx);
2309     struct layer_context *ctx_layer = NULL;
2310
2311     if (pPosition != NULL) {
2312         ctx_layer = (struct layer_context*)
2313                     wayland_controller_get_layer_context(
2314                         &ctx->main_ctx, (uint32_t)layerId);
2315         if (ctx_layer != NULL) {
2316             ivi_controller_layer_set_destination_rectangle(
2317                 ctx_layer->controller,
2318                 (int32_t)*pPosition, (int32_t)*(pPosition + 1),
2319                 ctx_layer->prop.destWidth, ctx_layer->prop.destHeight);
2320             returnValue = ILM_SUCCESS;
2321         }
2322     }
2323
2324     unlock_context(ctx);
2325     return returnValue;
2326 }
2327
2328 static ilmErrorTypes
2329 wayland_layerSetOrientation(t_ilm_layer layerId, ilmOrientation orientation)
2330 {
2331     ilmErrorTypes returnValue = ILM_FAILED;
2332     struct ilm_control_context *ctx = get_instance();
2333     lock_context(ctx);
2334     struct layer_context *ctx_layer = NULL;
2335     int32_t iviorientation = 0;
2336
2337     do {
2338         switch(orientation) {
2339         case ILM_ZERO:
2340             iviorientation = IVI_CONTROLLER_SURFACE_ORIENTATION_0_DEGREES;
2341             break;
2342         case ILM_NINETY:
2343             iviorientation = IVI_CONTROLLER_SURFACE_ORIENTATION_90_DEGREES;
2344             break;
2345         case ILM_ONEHUNDREDEIGHTY:
2346             iviorientation = IVI_CONTROLLER_SURFACE_ORIENTATION_180_DEGREES;
2347             break;
2348         case ILM_TWOHUNDREDSEVENTY:
2349             iviorientation = IVI_CONTROLLER_SURFACE_ORIENTATION_270_DEGREES;
2350             break;
2351         default:
2352             returnValue = ILM_ERROR_INVALID_ARGUMENTS;
2353             break;
2354         }
2355
2356         ctx_layer = (struct layer_context*)wayland_controller_get_layer_context(
2357                         &ctx->main_ctx, (uint32_t)layerId);
2358         if (ctx_layer == NULL) {
2359             returnValue = ILM_FAILED;
2360             break;
2361         }
2362
2363         ivi_controller_layer_set_orientation(ctx_layer->controller,
2364                                              iviorientation);
2365
2366         returnValue = ILM_SUCCESS;
2367     } while(0);
2368
2369     unlock_context(ctx);
2370     return returnValue;
2371 }
2372
2373 static ilmErrorTypes
2374 wayland_layerGetOrientation(t_ilm_layer layerId, ilmOrientation *pOrientation)
2375 {
2376     ilmErrorTypes returnValue = ILM_FAILED;
2377     struct ilm_control_context *ctx = get_instance();
2378     lock_context(ctx);
2379     struct layer_context *ctx_layer = NULL;
2380
2381     if (pOrientation != NULL) {
2382         ctx_layer = (struct layer_context*)
2383                     wayland_controller_get_layer_context(
2384                         &ctx->main_ctx, (uint32_t)layerId);
2385         if (ctx_layer != NULL) {
2386             *pOrientation = ctx_layer->prop.orientation;
2387             returnValue = ILM_SUCCESS;
2388         }
2389     }
2390
2391     unlock_context(ctx);
2392     return returnValue;
2393 }
2394
2395 static ilmErrorTypes
2396 wayland_layerSetChromaKey(t_ilm_layer layerId, t_ilm_int* pColor)
2397 {
2398     (void)layerId;
2399     (void)pColor;
2400     /* Not supported */
2401     return ILM_FAILED;
2402 }
2403
2404 static ilmErrorTypes
2405 wayland_layerSetRenderOrder(t_ilm_layer layerId,
2406                         t_ilm_surface *pSurfaceId,
2407                         t_ilm_int number)
2408 {
2409     ilmErrorTypes returnValue = ILM_FAILED;
2410     struct ilm_control_context *ctx = get_instance();
2411     lock_context(ctx);
2412     struct layer_context *ctx_layer = NULL;
2413
2414     ctx_layer = (struct layer_context*)wayland_controller_get_layer_context(
2415                     &ctx->main_ctx, (uint32_t)layerId);
2416     if (ctx_layer) {
2417         int cnt = 0;
2418         uint32_t id = 0;
2419         struct surface_context *ctx_surf = NULL;
2420         ivi_controller_layer_clear_surfaces(ctx_layer->controller);
2421
2422         for (cnt = 0; cnt < number; cnt++) {
2423             id = (uint32_t)*(pSurfaceId + cnt);
2424             ctx_surf = get_surface_context(&ctx->main_ctx, id);
2425
2426             if (ctx_surf == NULL) {
2427                 fprintf(stderr, "invalud argument \
2428                         in ilm_layerSetRenderOrder\n");
2429                 continue;
2430             }
2431             ivi_controller_layer_add_surface(ctx_layer->controller,
2432                                              ctx_surf->controller);
2433         }
2434
2435         returnValue = ILM_SUCCESS;
2436     }
2437
2438     unlock_context(ctx);
2439     return returnValue;
2440 }
2441
2442 static ilmErrorTypes
2443 wayland_layerGetCapabilities(t_ilm_layer layerId,
2444                          t_ilm_layercapabilities *pCapabilities)
2445 {
2446     (void)layerId;
2447     (void)pCapabilities;
2448     /* Not supported */
2449     return ILM_FAILED;
2450 }
2451
2452 static ilmErrorTypes
2453 wayland_layerTypeGetCapabilities(ilmLayerType layerType,
2454                              t_ilm_layercapabilities *pCapabilities)
2455 {
2456     (void)layerType;
2457     (void)pCapabilities;
2458     /* Not supported */
2459     return ILM_FAILED;
2460 }
2461
2462 static ilmErrorTypes
2463 wayland_surfaceSetVisibility(t_ilm_surface surfaceId, t_ilm_bool newVisibility)
2464 {
2465     ilmErrorTypes returnValue = ILM_FAILED;
2466     struct ilm_control_context *ctx = get_instance();
2467     lock_context(ctx);
2468     struct surface_context *ctx_surf = NULL;
2469     uint32_t visibility = 0;
2470
2471     if (newVisibility == ILM_TRUE) {
2472         visibility = 1;
2473     }
2474     ctx_surf = get_surface_context(&ctx->main_ctx, surfaceId);
2475     if (ctx_surf) {
2476         ivi_controller_surface_set_visibility(ctx_surf->controller,
2477                                               visibility);
2478         returnValue = ILM_SUCCESS;
2479     }
2480
2481     unlock_context(ctx);
2482     return returnValue;
2483 }
2484
2485 static ilmErrorTypes
2486 wayland_surfaceSetOpacity(t_ilm_surface surfaceId, t_ilm_float opacity)
2487 {
2488     ilmErrorTypes returnValue = ILM_FAILED;
2489     struct ilm_control_context *ctx = get_instance();
2490     lock_context(ctx);
2491     struct surface_context *ctx_surf = NULL;
2492     wl_fixed_t opacity_fixed = 0;
2493
2494     opacity_fixed = wl_fixed_from_double((double)opacity);
2495     ctx_surf = get_surface_context(&ctx->main_ctx, surfaceId);
2496     if (ctx_surf) {
2497         ivi_controller_surface_set_opacity(ctx_surf->controller,
2498                                            opacity_fixed);
2499         returnValue = ILM_SUCCESS;
2500     }
2501
2502     unlock_context(ctx);
2503     return returnValue;
2504 }
2505
2506 static ilmErrorTypes
2507 wayland_surfaceGetOpacity(t_ilm_surface surfaceId, t_ilm_float *pOpacity)
2508 {
2509     ilmErrorTypes returnValue = ILM_FAILED;
2510     struct ilm_control_context *ctx = get_instance();
2511     lock_context(ctx);
2512
2513     if (pOpacity != NULL) {
2514         struct surface_context *ctx_surf = NULL;
2515         ctx_surf = get_surface_context(&ctx->main_ctx, surfaceId);
2516         if (ctx_surf) {
2517             *pOpacity = ctx_surf->prop.opacity;
2518             returnValue = ILM_SUCCESS;
2519         }
2520     }
2521
2522     unlock_context(ctx);
2523     return returnValue;
2524 }
2525
2526 static ilmErrorTypes
2527 wayland_SetKeyboardFocusOn(t_ilm_surface surfaceId)
2528 {
2529     ilmErrorTypes returnValue = ILM_FAILED;
2530     (void)surfaceId;
2531     returnValue = ILM_SUCCESS;
2532     return returnValue;
2533 }
2534
2535 static ilmErrorTypes
2536 wayland_GetKeyboardFocusSurfaceId(t_ilm_surface* pSurfaceId)
2537 {
2538     ilmErrorTypes returnValue = ILM_FAILED;
2539     (void)pSurfaceId;
2540     returnValue = ILM_SUCCESS;
2541     return returnValue;
2542 }
2543
2544 static ilmErrorTypes
2545 wayland_surfaceSetDestinationRectangle(t_ilm_surface surfaceId,
2546                                    t_ilm_int x, t_ilm_int y,
2547                                    t_ilm_int width, t_ilm_int height)
2548 {
2549     ilmErrorTypes returnValue = ILM_FAILED;
2550     struct ilm_control_context *ctx = get_instance();
2551     lock_context(ctx);
2552     struct surface_context *ctx_surf = NULL;
2553
2554     ctx_surf = get_surface_context(&ctx->main_ctx, surfaceId);
2555     if (ctx_surf) {
2556         ivi_controller_surface_set_destination_rectangle(
2557                                              ctx_surf->controller,
2558                                              x, y, width, height);
2559         returnValue = ILM_SUCCESS;
2560     }
2561
2562     unlock_context(ctx);
2563     return returnValue;
2564 }
2565
2566 static ilmErrorTypes
2567 wayland_surfaceSetDimension(t_ilm_surface surfaceId, t_ilm_uint *pDimension)
2568 {
2569     ilmErrorTypes returnValue = ILM_FAILED;
2570     struct ilm_control_context *ctx = get_instance();
2571     lock_context(ctx);
2572
2573     if (pDimension != NULL) {
2574         struct surface_context *ctx_surf = NULL;
2575         ctx_surf = get_surface_context(&ctx->main_ctx, surfaceId);
2576         if (ctx_surf) {
2577             uint32_t width = *pDimension;
2578             uint32_t height = *(pDimension + 1);
2579             ivi_controller_surface_set_destination_rectangle(
2580                 ctx_surf->controller,
2581                 ctx_surf->prop.destX, ctx_surf->prop.destY, width, height);
2582             returnValue = ILM_SUCCESS;
2583         }
2584     }
2585
2586     unlock_context(ctx);
2587     return returnValue;
2588 }
2589
2590 static ilmErrorTypes
2591 wayland_surfaceGetPosition(t_ilm_surface surfaceId, t_ilm_uint *pPosition)
2592 {
2593     ilmErrorTypes returnValue = ILM_FAILED;
2594     struct ilm_control_context *ctx = get_instance();
2595     lock_context(ctx);
2596
2597     if (pPosition != NULL) {
2598         struct surface_context *ctx_surf = NULL;
2599         ctx_surf = get_surface_context(&ctx->main_ctx, surfaceId);
2600         if (ctx_surf) {
2601             *pPosition = ctx_surf->prop.destX;
2602             *(pPosition + 1) = ctx_surf->prop.destY;
2603             returnValue = ILM_SUCCESS;
2604         }
2605     }
2606
2607     unlock_context(ctx);
2608     return returnValue;
2609 }
2610
2611 static ilmErrorTypes
2612 wayland_surfaceSetPosition(t_ilm_surface surfaceId, t_ilm_uint *pPosition)
2613 {
2614     ilmErrorTypes returnValue = ILM_FAILED;
2615     struct ilm_control_context *ctx = get_instance();
2616     lock_context(ctx);
2617
2618     if (pPosition != NULL) {
2619         struct surface_context *ctx_surf = NULL;
2620         ctx_surf = get_surface_context(&ctx->main_ctx, surfaceId);
2621         if (ctx_surf) {
2622             int32_t destX = (int32_t)*pPosition;
2623             int32_t destY = (int32_t)*(pPosition + 1);
2624             ivi_controller_surface_set_destination_rectangle(
2625                 ctx_surf->controller, destX, destY,
2626                 ctx_surf->prop.destWidth, ctx_surf->prop.destHeight);
2627             returnValue = ILM_SUCCESS;
2628         }
2629     }
2630
2631     unlock_context(ctx);
2632     return returnValue;
2633 }
2634
2635 static ilmErrorTypes
2636 wayland_surfaceSetOrientation(t_ilm_surface surfaceId,
2637                               ilmOrientation orientation)
2638 {
2639     ilmErrorTypes returnValue = ILM_FAILED;
2640     struct ilm_control_context *ctx = get_instance();
2641     lock_context(ctx);
2642     struct surface_context *ctx_surf = NULL;
2643     int32_t iviorientation = 0;
2644
2645     do {
2646         switch(orientation) {
2647         case ILM_ZERO:
2648             iviorientation = IVI_CONTROLLER_SURFACE_ORIENTATION_0_DEGREES;
2649             break;
2650         case ILM_NINETY:
2651             iviorientation = IVI_CONTROLLER_SURFACE_ORIENTATION_90_DEGREES;
2652             break;
2653         case ILM_ONEHUNDREDEIGHTY:
2654             iviorientation = IVI_CONTROLLER_SURFACE_ORIENTATION_180_DEGREES;
2655             break;
2656         case ILM_TWOHUNDREDSEVENTY:
2657             iviorientation = IVI_CONTROLLER_SURFACE_ORIENTATION_270_DEGREES;
2658             break;
2659         default:
2660             returnValue = ILM_ERROR_INVALID_ARGUMENTS;
2661             break;
2662         }
2663
2664         ctx_surf = get_surface_context(&ctx->main_ctx, surfaceId);
2665         if (ctx_surf == NULL) {
2666             returnValue = ILM_FAILED;
2667             break;
2668         }
2669
2670         ivi_controller_surface_set_orientation(ctx_surf->controller,
2671                                                iviorientation);
2672
2673         returnValue = ILM_SUCCESS;
2674     } while(0);
2675
2676     unlock_context(ctx);
2677     return returnValue;
2678 }
2679
2680 static ilmErrorTypes
2681 wayland_surfaceGetOrientation(t_ilm_surface surfaceId,
2682                               ilmOrientation *pOrientation)
2683 {
2684     ilmErrorTypes returnValue = ILM_FAILED;
2685     struct ilm_control_context *ctx = get_instance();
2686     lock_context(ctx);
2687
2688     if (pOrientation != NULL) {
2689         struct surface_context *ctx_surf = NULL;
2690         ctx_surf = get_surface_context(&ctx->main_ctx, surfaceId);
2691         if (ctx_surf) {
2692             *pOrientation = ctx_surf->prop.orientation;
2693             returnValue = ILM_SUCCESS;
2694         }
2695     }
2696
2697     unlock_context(ctx);
2698     return returnValue;
2699 }
2700
2701 static ilmErrorTypes
2702 wayland_surfaceGetPixelformat(t_ilm_layer surfaceId,
2703                               ilmPixelFormat *pPixelformat)
2704 {
2705     ilmErrorTypes returnValue = ILM_FAILED;
2706     struct ilm_control_context *ctx = get_instance();
2707     lock_context(ctx);
2708
2709     if (pPixelformat != NULL) {
2710         struct surface_context *ctx_surf = NULL;
2711         ctx_surf = get_surface_context(&ctx->main_ctx, surfaceId);
2712         if (ctx_surf) {
2713             *pPixelformat = ctx_surf->prop.pixelformat;
2714             returnValue = ILM_SUCCESS;
2715         }
2716     }
2717
2718     unlock_context(ctx);
2719     return returnValue;
2720 }
2721
2722 static ilmErrorTypes
2723 wayland_surfaceSetChromaKey(t_ilm_surface surfaceId, t_ilm_int* pColor)
2724 {
2725     (void)surfaceId;
2726     (void)pColor;
2727     /* Not supported */
2728     return ILM_FAILED;
2729 }
2730
2731 static ilmErrorTypes
2732 wayland_displaySetRenderOrder(t_ilm_display display,
2733                           t_ilm_layer *pLayerId, const t_ilm_uint number)
2734 {
2735     ilmErrorTypes returnValue = ILM_FAILED;
2736     struct ilm_control_context *ctx = get_instance();
2737     lock_context(ctx);
2738     struct screen_context *ctx_scrn = NULL;
2739
2740     ctx_scrn = get_screen_context_by_id(&ctx->main_ctx, (uint32_t)display);
2741     if (ctx_scrn != NULL) {
2742         int cnt = 0;
2743         uint32_t id = 0;
2744         struct layer_context *ctx_layer = NULL;
2745
2746         ivi_controller_screen_clear(ctx_scrn->controller);
2747
2748         for (cnt = 0; cnt < (int)number; cnt++) {
2749             id = (uint32_t)*(pLayerId + cnt);
2750             ctx_layer =
2751                  (struct layer_context*)wayland_controller_get_layer_context(
2752                             &ctx->main_ctx, id);
2753             if (ctx_layer != NULL) {
2754                 ivi_controller_screen_add_layer(ctx_scrn->controller,
2755                                                 ctx_layer->controller);
2756             }
2757         }
2758
2759         returnValue = ILM_SUCCESS;
2760     }
2761
2762     unlock_context(ctx);
2763     return returnValue;
2764 }
2765
2766 static ilmErrorTypes
2767 wayland_takeScreenshot(t_ilm_uint screen, t_ilm_const_string filename)
2768 {
2769     ilmErrorTypes returnValue = ILM_FAILED;
2770     struct ilm_control_context *ctx = get_instance();
2771     lock_context(ctx);
2772     struct screen_context *ctx_scrn = NULL;
2773
2774     ctx_scrn = get_screen_context_by_id(&ctx->main_ctx, (uint32_t)screen);
2775     if (ctx_scrn != NULL) {
2776         ivi_controller_screen_screenshot(ctx_scrn->controller,
2777                                         filename);
2778         wl_display_flush(ctx->main_ctx.display);
2779         returnValue = ILM_SUCCESS;
2780     }
2781
2782     unlock_context(ctx);
2783     return returnValue;
2784 }
2785
2786 static ilmErrorTypes
2787 wayland_takeLayerScreenshot(t_ilm_const_string filename, t_ilm_layer layerid)
2788 {
2789     ilmErrorTypes returnValue = ILM_FAILED;
2790     struct ilm_control_context *ctx = get_instance();
2791     lock_context(ctx);
2792     struct layer_context *ctx_layer = NULL;
2793
2794     ctx_layer = (struct layer_context*)wayland_controller_get_layer_context(
2795                     &ctx->main_ctx, (uint32_t)layerid);
2796     if (ctx_layer != NULL) {
2797         ivi_controller_layer_screenshot(ctx_layer->controller,
2798                                         filename);
2799         returnValue = ILM_SUCCESS;
2800     }
2801
2802     unlock_context(ctx);
2803     return returnValue;
2804 }
2805
2806 static ilmErrorTypes
2807 wayland_takeSurfaceScreenshot(t_ilm_const_string filename,
2808                               t_ilm_surface surfaceid)
2809 {
2810     ilmErrorTypes returnValue = ILM_FAILED;
2811     struct ilm_control_context *ctx = get_instance();
2812     lock_context(ctx);
2813     struct surface_context *ctx_surf = NULL;
2814
2815     ctx_surf = get_surface_context(&ctx->main_ctx, (uint32_t)surfaceid);
2816     if (ctx_surf) {
2817         ivi_controller_surface_screenshot(ctx_surf->controller,
2818                                           filename);
2819         wl_display_flush(ctx->main_ctx.display);
2820         returnValue = ILM_SUCCESS;
2821     }
2822
2823     unlock_context(ctx);
2824     return returnValue;
2825 }
2826
2827 static ilmErrorTypes
2828 wayland_SetOptimizationMode(ilmOptimization id, ilmOptimizationMode mode)
2829 {
2830     (void)id;
2831     (void)mode;
2832     /* Not supported */
2833     return ILM_FAILED;
2834 }
2835
2836 static ilmErrorTypes
2837 wayland_GetOptimizationMode(ilmOptimization id, ilmOptimizationMode* pMode)
2838 {
2839     (void)id;
2840     (void)pMode;
2841     /* Not supported */
2842     return ILM_FAILED;
2843 }
2844
2845 // TODO
2846 static ilmErrorTypes
2847 wayland_layerAddNotification(t_ilm_layer layer,
2848                              layerNotificationFunc callback)
2849 {
2850     ilmErrorTypes returnValue = ILM_FAILED;
2851     struct ilm_control_context *ctx = get_instance();
2852     lock_context(ctx);
2853     struct layer_context *ctx_layer = NULL;
2854
2855     ctx_layer = (struct layer_context*)wayland_controller_get_layer_context(
2856                     &ctx->main_ctx, (uint32_t)layer);
2857     if (ctx_layer == NULL) {
2858         returnValue = ILM_ERROR_INVALID_ARGUMENTS;
2859     } else {
2860         ctx_layer->notification = callback;
2861
2862         returnValue = ILM_SUCCESS;
2863     }
2864
2865     unlock_context(ctx);
2866     return returnValue;
2867 }
2868
2869 // TODO
2870 static ilmErrorTypes
2871 wayland_layerRemoveNotification(t_ilm_layer layer)
2872 {
2873    return wayland_layerAddNotification(layer, NULL);
2874 }
2875
2876 // TODO
2877 static ilmErrorTypes
2878 wayland_surfaceAddNotification(t_ilm_surface surface,
2879                              surfaceNotificationFunc callback)
2880 {
2881     ilmErrorTypes returnValue = ILM_FAILED;
2882     struct ilm_control_context *ctx = get_instance();
2883     lock_context(ctx);
2884     struct surface_context *ctx_surf = NULL;
2885
2886     ctx_surf = (struct surface_context*)get_surface_context(
2887                     &ctx->main_ctx, (uint32_t)surface);
2888     if (ctx_surf == NULL) {
2889         returnValue = ILM_ERROR_INVALID_ARGUMENTS;
2890     } else {
2891         ctx_surf->notification = callback;
2892
2893         returnValue = ILM_SUCCESS;
2894     }
2895
2896     unlock_context(ctx);
2897     return returnValue;
2898 }
2899
2900 // TODO
2901 static ilmErrorTypes
2902 wayland_surfaceRemoveNotification(t_ilm_surface surface)
2903 {
2904     return wayland_surfaceAddNotification(surface, NULL);
2905 }
2906
2907 static ilmErrorTypes
2908 wayland_getNativeHandle(t_ilm_uint pid, t_ilm_int *n_handle,
2909                         t_ilm_nativehandle **p_handles)
2910 {
2911     struct ilm_control_context *ctx = get_instance();
2912     lock_context(ctx);
2913     struct nativehandle_context *p_nh_ctx = NULL;
2914
2915     *n_handle = 0;
2916     *p_handles = NULL;
2917
2918     wl_list_for_each(p_nh_ctx, &ctx->list_nativehandle, link)
2919     {
2920         if (p_nh_ctx->pid == pid)
2921         {
2922             *n_handle = 1;
2923             *p_handles =
2924                  (t_ilm_nativehandle*)malloc(sizeof(t_ilm_nativehandle));
2925             (*p_handles)[0] = p_nh_ctx->nativehandle;
2926             break;
2927         }
2928     }
2929
2930     unlock_context(ctx);
2931     return (*n_handle > 0) ? ILM_SUCCESS : ILM_FAILED;
2932 }
2933
2934 static ilmErrorTypes
2935 wayland_getPropertiesOfSurface(t_ilm_uint surfaceID,
2936                         struct ilmSurfaceProperties* pSurfaceProperties)
2937 {
2938     ilmErrorTypes returnValue = ILM_FAILED;
2939     struct ilm_control_context *ctx = get_instance();
2940     lock_context(ctx);
2941
2942     if (pSurfaceProperties != NULL) {
2943         struct surface_context *ctx_surf = NULL;
2944
2945         ctx_surf = get_surface_context(&ctx->main_ctx, (uint32_t)surfaceID);
2946         if (ctx_surf != NULL) {
2947
2948             *pSurfaceProperties = ctx_surf->prop;
2949             returnValue = ILM_SUCCESS;
2950         }
2951     }
2952
2953     unlock_context(ctx);
2954     return returnValue;
2955 }
2956
2957 static ilmErrorTypes
2958 wayland_layerAddSurface(t_ilm_layer layerId,
2959                         t_ilm_surface surfaceId)
2960 {
2961     ilmErrorTypes returnValue = ILM_FAILED;
2962     struct ilm_control_context *ctx = get_instance();
2963     lock_context(ctx);
2964     struct layer_context *ctx_layer = NULL;
2965     struct surface_context *ctx_surf = NULL;
2966
2967     ctx_layer = (struct layer_context*)wayland_controller_get_layer_context(
2968                     &ctx->main_ctx, (uint32_t)layerId);
2969     ctx_surf = get_surface_context(&ctx->main_ctx, (uint32_t)surfaceId);
2970     if ((ctx_layer != NULL) && (ctx_surf != NULL)) {
2971         ivi_controller_layer_add_surface(ctx_layer->controller,
2972                                          ctx_surf->controller);
2973         returnValue = ILM_SUCCESS;
2974     }
2975
2976     unlock_context(ctx);
2977     return returnValue;
2978 }
2979
2980 static ilmErrorTypes
2981 wayland_layerRemoveSurface(t_ilm_layer layerId,
2982                            t_ilm_surface surfaceId)
2983 {
2984     ilmErrorTypes returnValue = ILM_FAILED;
2985     struct ilm_control_context *ctx = get_instance();
2986     lock_context(ctx);
2987     struct layer_context *ctx_layer = NULL;
2988     struct surface_context *ctx_surf = NULL;
2989
2990     ctx_layer = (struct layer_context*)wayland_controller_get_layer_context(
2991                     &ctx->main_ctx, (uint32_t)layerId);
2992     ctx_surf = get_surface_context(&ctx->main_ctx, (uint32_t)surfaceId);
2993     if ((ctx_layer != NULL) && (ctx_surf != NULL)) {
2994         ivi_controller_layer_remove_surface(ctx_layer->controller,
2995                                             ctx_surf->controller);
2996         returnValue = ILM_SUCCESS;
2997     }
2998
2999     unlock_context(ctx);
3000     return returnValue;
3001 }
3002
3003 static ilmErrorTypes
3004 wayland_surfaceGetDimension(t_ilm_surface surfaceId,
3005                             t_ilm_uint *pDimension)
3006 {
3007     ilmErrorTypes returnValue = ILM_FAILED;
3008     struct ilm_control_context *ctx = get_instance();
3009     lock_context(ctx);
3010
3011     if (pDimension != NULL) {
3012         struct surface_context *ctx_surf = NULL;
3013
3014         ctx_surf = get_surface_context(&ctx->main_ctx, (uint32_t)surfaceId);
3015         if (ctx_surf != NULL) {
3016             *pDimension = (t_ilm_uint)ctx_surf->prop.destWidth;
3017             *(pDimension + 1) = (t_ilm_uint)ctx_surf->prop.destHeight;
3018             returnValue = ILM_SUCCESS;
3019         }
3020     }
3021
3022     unlock_context(ctx);
3023     return returnValue;
3024 }
3025
3026 static ilmErrorTypes
3027 wayland_surfaceGetVisibility(t_ilm_surface surfaceId,
3028                              t_ilm_bool *pVisibility)
3029 {
3030     ilmErrorTypes returnValue = ILM_FAILED;
3031     struct ilm_control_context *ctx = get_instance();
3032     lock_context(ctx);
3033     struct surface_context *ctx_surf = NULL;
3034
3035     if (pVisibility != NULL) {
3036         ctx_surf = get_surface_context(&ctx->main_ctx, (uint32_t)surfaceId);
3037         if (ctx_surf != NULL) {
3038             *pVisibility = (t_ilm_bool)ctx_surf->prop.visibility;
3039             returnValue = ILM_SUCCESS;
3040         }
3041     }
3042
3043     unlock_context(ctx);
3044     return returnValue;
3045 }
3046
3047 static ilmErrorTypes
3048 wayland_surfaceSetSourceRectangle(t_ilm_surface surfaceId,
3049                                   t_ilm_int x, t_ilm_int y,
3050                                   t_ilm_int width, t_ilm_int height)
3051 {
3052     ilmErrorTypes returnValue = ILM_FAILED;
3053     struct ilm_control_context *ctx = get_instance();
3054     lock_context(ctx);
3055     struct surface_context *ctx_surf = NULL;
3056
3057     ctx_surf = get_surface_context(&ctx->main_ctx, (uint32_t)surfaceId);
3058     if (ctx_surf != NULL) {
3059         if (ctx_surf->controller != NULL) {
3060             ivi_controller_surface_set_source_rectangle(
3061                     ctx_surf->controller,
3062                     x, y, width, height);
3063             returnValue = ILM_SUCCESS;
3064         }
3065     }
3066
3067     unlock_context(ctx);
3068     return returnValue;
3069 }
3070
3071 static ilmErrorTypes
3072 wayland_commitChanges(void)
3073 {
3074     ilmErrorTypes returnValue = ILM_FAILED;
3075     struct ilm_control_context *ctx = get_instance();
3076     lock_context(ctx);
3077
3078     if (ctx->main_ctx.controller != NULL) {
3079         ivi_controller_commit_changes(ctx->main_ctx.controller);
3080
3081         display_roundtrip_queue(ctx->main_ctx.display, ctx->main_ctx.queue);
3082         returnValue = ILM_SUCCESS;
3083     }
3084
3085     unlock_context(ctx);
3086     return returnValue;
3087 }