downstream: ivi-shell: Implement ivi_layout_get_keyboard_focus_surface_id
[profile/ivi/weston-ivi-shell.git] / ivi-shell / ivi-layout.c
1 /*
2  * Copyright (C) 2013 DENSO CORPORATION
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 /**
24  * Implementation of ivi-layout library. The actual view on ivi_screen is
25  * not updated till calling ivi_layout_commit_changes. A overview from
26  * calling API for updating properties of ivi_surface/ivi_layer to asking
27  * compositor to compose them by using weston_compositor_schedule_repaint,
28  * 0/ initialize this library by ivi_layout_init_with_compositor
29  *    with (struct weston_compositor *ec) from ivi-shell.
30  * 1/ When a API for updating properties of ivi_surface/ivi_layer, it updates
31  *    pending prop of ivi_surface/ivi_layer/ivi_screen which are structure to
32  *    store properties.
33  * 2/ Before calling commitChanges, in case of calling a API to get a property,
34  *    return current property, not pending property.
35  * 3/ At the timing of calling ivi_layout_commitChanges, pending properties
36  *    are applied to properties.
37  *
38  *    *) ivi_layout_commitChanges is also called by transition animation
39  *    per each frame. See ivi-layout-transition.c in details. Transition
40  *    animation interpolates frames between previous properties of ivi_surface
41  *    and new ones.
42  *    For example, when a property of ivi_surface is changed from invisibility
43  *    to visibility, it behaves like fade-in. When ivi_layout_commitChange is
44  *    called during transition animation, it cancels the transition and
45  *    re-start transition to new properties from current properties of final
46  *    frame just before the the cancellation.
47  *
48  * 4/ According properties, set transformation by using weston_matrix and
49  *    weston_view per ivi_surfaces and ivi_layers in while loop.
50  * 5/ Set damage and trigger transform by using weston_view_geometry_dirty.
51  * 6/ Notify update of properties.
52  * 7/ Trigger composition by weston_compositor_schedule_repaint.
53  *
54  */
55
56 #include <sys/wait.h>
57 #include <unistd.h>
58 #include <stdlib.h>
59 #include <stdio.h>
60 #include <string.h>
61 #include <linux/input.h>
62
63 #include "compositor.h"
64 #include "ivi-layout-export.h"
65 #include "ivi-layout-private.h"
66
67 struct link_layer {
68         struct ivi_layout_layer *ivilayer;
69         struct wl_list link;
70         struct wl_list link_to_layer;
71 };
72
73 struct link_screen {
74         struct ivi_layout_screen *iviscrn;
75         struct wl_list link;
76         struct wl_list link_to_screen;
77 };
78
79 struct listener_layout_notification {
80         void *userdata;
81         struct wl_listener listener;
82 };
83
84 struct ivi_layout;
85
86 struct ivi_layout_screen {
87         struct wl_list link;
88         struct wl_list link_to_layer;
89         uint32_t id_screen;
90
91         struct ivi_layout *layout;
92         struct weston_output *output;
93
94         uint32_t event_mask;
95
96         struct {
97                 struct wl_list layer_list;
98                 struct wl_list link;
99         } pending;
100
101         struct {
102                 struct wl_list layer_list;
103                 struct wl_list link;
104         } order;
105 };
106
107 struct seat_ctx {
108         struct weston_keyboard_grab grab;
109         struct wl_listener updated_caps_listener;
110         struct wl_listener destroy_listener;
111 };
112
113 struct ivi_layout_notification_callback {
114         void *callback;
115         void *data;
116 };
117
118 static struct ivi_layout ivilayout = {0};
119
120 struct ivi_layout *
121 get_layout_instance(void)
122 {
123         return &ivilayout;
124 }
125
126 /**
127  * Internal API to add/remove a link to ivi_surface from ivi_layer.
128  */
129 static void
130 add_link_to_surface(struct ivi_layout_layer *ivilayer,
131                     struct link_layer *link_layer)
132 {
133         struct link_layer *link = NULL;
134
135         wl_list_for_each(link, &ivilayer->link_to_surface, link_to_layer) {
136                 if (link == link_layer)
137                         return;
138         }
139
140         wl_list_insert(&ivilayer->link_to_surface, &link_layer->link_to_layer);
141 }
142
143 static void
144 remove_link_to_surface(struct ivi_layout_layer *ivilayer)
145 {
146         struct link_layer *link = NULL;
147         struct link_layer *next = NULL;
148
149         wl_list_for_each_safe(link, next, &ivilayer->link_to_surface, link_to_layer) {
150                 if (!wl_list_empty(&link->link_to_layer)) {
151                         wl_list_remove(&link->link_to_layer);
152                 }
153                 if (!wl_list_empty(&link->link)) {
154                         wl_list_remove(&link->link);
155                 }
156                 free(link);
157         }
158
159         wl_list_init(&ivilayer->link_to_surface);
160 }
161
162 /**
163  * Internal API to add a link to ivi_layer from ivi_screen.
164  */
165 static void
166 add_link_to_layer(struct ivi_layout_screen *iviscrn,
167                   struct link_screen *link_screen)
168 {
169         wl_list_init(&link_screen->link_to_screen);
170         wl_list_insert(&iviscrn->link_to_layer, &link_screen->link_to_screen);
171 }
172
173 /**
174  * Internal API to add/remove a ivi_surface from ivi_layer.
175  */
176 static void
177 add_ordersurface_to_layer(struct ivi_layout_surface *ivisurf,
178                           struct ivi_layout_layer *ivilayer)
179 {
180         struct link_layer *link_layer = NULL;
181
182         link_layer = malloc(sizeof *link_layer);
183         if (link_layer == NULL) {
184                 weston_log("fails to allocate memory\n");
185                 return;
186         }
187
188         link_layer->ivilayer = ivilayer;
189         wl_list_init(&link_layer->link);
190         wl_list_insert(&ivisurf->layer_list, &link_layer->link);
191         add_link_to_surface(ivilayer, link_layer);
192 }
193
194 static void
195 remove_ordersurface_from_layer(struct ivi_layout_surface *ivisurf)
196 {
197         struct link_layer *link_layer = NULL;
198         struct link_layer *next = NULL;
199
200         wl_list_for_each_safe(link_layer, next, &ivisurf->layer_list, link) {
201                 if (!wl_list_empty(&link_layer->link)) {
202                         wl_list_remove(&link_layer->link);
203                 }
204                 if (!wl_list_empty(&link_layer->link_to_layer)) {
205                         wl_list_remove(&link_layer->link_to_layer);
206                 }
207                 free(link_layer);
208         }
209         wl_list_init(&ivisurf->layer_list);
210 }
211
212 /**
213  * Internal API to add/remove a ivi_layer to/from ivi_screen.
214  */
215 static void
216 add_orderlayer_to_screen(struct ivi_layout_layer *ivilayer,
217                          struct ivi_layout_screen *iviscrn)
218 {
219         struct link_screen *link_scrn = NULL;
220
221         link_scrn = malloc(sizeof *link_scrn);
222         if (link_scrn == NULL) {
223                 weston_log("fails to allocate memory\n");
224                 return;
225         }
226
227         link_scrn->iviscrn = iviscrn;
228         wl_list_init(&link_scrn->link);
229         wl_list_insert(&ivilayer->screen_list, &link_scrn->link);
230         add_link_to_layer(iviscrn, link_scrn);
231 }
232
233 static void
234 remove_orderlayer_from_screen(struct ivi_layout_layer *ivilayer)
235 {
236         struct link_screen *link_scrn = NULL;
237         struct link_screen *next = NULL;
238
239         wl_list_for_each_safe(link_scrn, next, &ivilayer->screen_list, link) {
240                 if (!wl_list_empty(&link_scrn->link)) {
241                         wl_list_remove(&link_scrn->link);
242                 }
243                 if (!wl_list_empty(&link_scrn->link_to_screen)) {
244                         wl_list_remove(&link_scrn->link_to_screen);
245                 }
246                 free(link_scrn);
247         }
248         wl_list_init(&ivilayer->screen_list);
249 }
250
251 /**
252  * Internal API to add/remove a ivi_layer to/from ivi_screen.
253  */
254 static struct ivi_layout_surface *
255 get_surface(struct wl_list *surf_list, uint32_t id_surface)
256 {
257         struct ivi_layout_surface *ivisurf;
258
259         wl_list_for_each(ivisurf, surf_list, link) {
260                 if (ivisurf->id_surface == id_surface) {
261                         return ivisurf;
262                 }
263         }
264
265         return NULL;
266 }
267
268 static struct ivi_layout_layer *
269 get_layer(struct wl_list *layer_list, uint32_t id_layer)
270 {
271         struct ivi_layout_layer *ivilayer;
272
273         wl_list_for_each(ivilayer, layer_list, link) {
274                 if (ivilayer->id_layer == id_layer) {
275                         return ivilayer;
276                 }
277         }
278
279         return NULL;
280 }
281
282 /**
283  * Called at destruction of ivi_surface
284  */
285 static void
286 westonsurface_destroy_from_ivisurface(struct wl_listener *listener, void *data)
287 {
288         struct ivi_layout_surface *ivisurf = NULL;
289
290         ivisurf = container_of(listener, struct ivi_layout_surface,
291                                surface_destroy_listener);
292
293         wl_list_remove(&ivisurf->surface_rotation.link);
294         wl_list_remove(&ivisurf->layer_rotation.link);
295         wl_list_remove(&ivisurf->surface_pos.link);
296         wl_list_remove(&ivisurf->layer_pos.link);
297         wl_list_remove(&ivisurf->scaling.link);
298
299         ivisurf->surface = NULL;
300         ivisurf->view = NULL;
301         ivi_layout_surface_remove(ivisurf);
302 }
303
304 /**
305  * Internal API to check ivi_layer/ivi_surface already added in ivi_layer/ivi_screen.
306  * Called by ivi_layout_layer_add_surface/ivi_layout_screenAddLayer
307  */
308 static int
309 is_surface_in_layer(struct ivi_layout_surface *ivisurf,
310                     struct ivi_layout_layer *ivilayer)
311 {
312         struct ivi_layout_surface *surf = NULL;
313
314         wl_list_for_each(surf, &ivilayer->pending.surface_list, pending.link) {
315                 if (surf->id_surface == ivisurf->id_surface) {
316                         return 1;
317                 }
318         }
319
320         return 0;
321 }
322
323 static int
324 is_layer_in_screen(struct ivi_layout_layer *ivilayer,
325                    struct ivi_layout_screen *iviscrn)
326 {
327         struct ivi_layout_layer *layer = NULL;
328
329         wl_list_for_each(layer, &iviscrn->pending.layer_list, pending.link) {
330                 if (layer->id_layer == ivilayer->id_layer) {
331                         return 1;
332                 }
333         }
334
335         return 0;
336 }
337
338 /**
339  * Internal API to initialize ivi_screens found from output_list of weston_compositor.
340  * Called by ivi_layout_init_with_compositor.
341  */
342 static void
343 create_screen(struct weston_compositor *ec)
344 {
345         struct ivi_layout *layout = get_layout_instance();
346         struct ivi_layout_screen *iviscrn = NULL;
347         struct weston_output *output = NULL;
348         int32_t count = 0;
349
350         wl_list_for_each(output, &ec->output_list, link) {
351                 iviscrn = calloc(1, sizeof *iviscrn);
352                 if (iviscrn == NULL) {
353                         weston_log("fails to allocate memory\n");
354                         continue;
355                 }
356
357                 wl_list_init(&iviscrn->link);
358                 iviscrn->layout = layout;
359
360                 iviscrn->id_screen = count;
361                 count++;
362
363                 iviscrn->output = output;
364                 iviscrn->event_mask = 0;
365
366                 wl_list_init(&iviscrn->pending.layer_list);
367                 wl_list_init(&iviscrn->pending.link);
368
369                 wl_list_init(&iviscrn->order.layer_list);
370                 wl_list_init(&iviscrn->order.link);
371
372                 wl_list_init(&iviscrn->link_to_layer);
373
374                 wl_list_insert(&layout->screen_list, &iviscrn->link);
375         }
376 }
377
378 /**
379  * Internal APIs to initialize properties of ivi_surface/ivi_layer when they are created.
380  */
381 static void
382 init_layer_properties(struct ivi_layout_layer_properties *prop,
383                       int32_t width, int32_t height)
384 {
385         memset(prop, 0, sizeof *prop);
386         prop->opacity = 1.0;
387         prop->source_width = width;
388         prop->source_height = height;
389         prop->dest_width = width;
390         prop->dest_height = height;
391 }
392
393 static void
394 init_surface_properties(struct ivi_layout_surface_properties *prop)
395 {
396         memset(prop, 0, sizeof *prop);
397         prop->opacity = 1.0;
398 }
399
400 /**
401  * Internal APIs to be called from ivi_layout_commit_changes.
402  */
403 static void
404 update_opacity(struct ivi_layout_layer *ivilayer,
405                struct ivi_layout_surface *ivisurf)
406 {
407         double layer_alpha = ivilayer->prop.opacity;
408         double surf_alpha  = ivisurf->prop.opacity;
409
410         if ((ivilayer->event_mask & IVI_NOTIFICATION_OPACITY) ||
411             (ivisurf->event_mask  & IVI_NOTIFICATION_OPACITY)) {
412                 if (ivisurf->view == NULL)
413                         return;
414                 ivisurf->view->alpha = layer_alpha * surf_alpha;
415         }
416 }
417
418 static void
419 update_surface_orientation(struct ivi_layout_layer *ivilayer,
420                            struct ivi_layout_surface *ivisurf)
421 {
422         struct weston_view *view = ivisurf->view;
423         struct weston_matrix  *matrix = &ivisurf->surface_rotation.matrix;
424         float width  = 0.0f;
425         float height = 0.0f;
426         float v_sin  = 0.0f;
427         float v_cos  = 0.0f;
428         float cx = 0.0f;
429         float cy = 0.0f;
430         float sx = 1.0f;
431         float sy = 1.0f;
432
433         if (view == NULL)
434                 return;
435
436         if ((ivilayer->prop.dest_width == 0) ||
437             (ivilayer->prop.dest_height == 0)) {
438                 return;
439         }
440         width  = (float)ivilayer->prop.dest_width;
441         height = (float)ivilayer->prop.dest_height;
442
443         switch (ivisurf->prop.orientation) {
444         case WL_OUTPUT_TRANSFORM_NORMAL:
445                 v_sin = 0.0f;
446                 v_cos = 1.0f;
447                 break;
448         case WL_OUTPUT_TRANSFORM_90:
449                 v_sin = 1.0f;
450                 v_cos = 0.0f;
451                 sx = width / height;
452                 sy = height / width;
453                 break;
454         case WL_OUTPUT_TRANSFORM_180:
455                 v_sin = 0.0f;
456                 v_cos = -1.0f;
457                 break;
458         case WL_OUTPUT_TRANSFORM_270:
459         default:
460                 v_sin = -1.0f;
461                 v_cos = 0.0f;
462                 sx = width / height;
463                 sy = height / width;
464                 break;
465         }
466         wl_list_remove(&ivisurf->surface_rotation.link);
467         weston_view_geometry_dirty(view);
468
469         weston_matrix_init(matrix);
470         cx = 0.5f * width;
471         cy = 0.5f * height;
472         weston_matrix_translate(matrix, -cx, -cy, 0.0f);
473         weston_matrix_rotate_xy(matrix, v_cos, v_sin);
474         weston_matrix_scale(matrix, sx, sy, 1.0);
475         weston_matrix_translate(matrix, cx, cy, 0.0f);
476         wl_list_insert(&view->geometry.transformation_list,
477                        &ivisurf->surface_rotation.link);
478
479         weston_view_set_transform_parent(view, NULL);
480         weston_view_update_transform(view);
481 }
482
483 static void
484 update_layer_orientation(struct ivi_layout_layer *ivilayer,
485                          struct ivi_layout_surface *ivisurf)
486 {
487         struct weston_surface *es = ivisurf->surface;
488         struct weston_view    *view = ivisurf->view;
489         struct weston_matrix  *matrix = &ivisurf->layer_rotation.matrix;
490         struct weston_output  *output = NULL;
491         float width  = 0.0f;
492         float height = 0.0f;
493         float v_sin  = 0.0f;
494         float v_cos  = 0.0f;
495         float cx = 0.0f;
496         float cy = 0.0f;
497         float sx = 1.0f;
498         float sy = 1.0f;
499
500         if (es == NULL || view == NULL) {
501                 return;
502         }
503
504         output = es->output;
505         if (output == NULL) {
506                 return;
507         }
508         if ((output->width == 0) || (output->height == 0)) {
509                 return;
510         }
511         width = (float)output->width;
512         height = (float)output->height;
513
514         switch (ivilayer->prop.orientation) {
515         case WL_OUTPUT_TRANSFORM_NORMAL:
516                 v_sin = 0.0f;
517                 v_cos = 1.0f;
518                 break;
519         case WL_OUTPUT_TRANSFORM_90:
520                 v_sin = 1.0f;
521                 v_cos = 0.0f;
522                 sx = width / height;
523                 sy = height / width;
524                 break;
525         case WL_OUTPUT_TRANSFORM_180:
526                 v_sin = 0.0f;
527                 v_cos = -1.0f;
528                 break;
529         case WL_OUTPUT_TRANSFORM_270:
530         default:
531                 v_sin = -1.0f;
532                 v_cos = 0.0f;
533                 sx = width / height;
534                 sy = height / width;
535                 break;
536         }
537         wl_list_remove(&ivisurf->layer_rotation.link);
538         weston_view_geometry_dirty(view);
539
540         weston_matrix_init(matrix);
541         cx = 0.5f * width;
542         cy = 0.5f * height;
543         weston_matrix_translate(matrix, -cx, -cy, 0.0f);
544         weston_matrix_rotate_xy(matrix, v_cos, v_sin);
545         weston_matrix_scale(matrix, sx, sy, 1.0);
546         weston_matrix_translate(matrix, cx, cy, 0.0f);
547         wl_list_insert(&view->geometry.transformation_list,
548                        &ivisurf->layer_rotation.link);
549
550         weston_view_set_transform_parent(view, NULL);
551         weston_view_update_transform(view);
552 }
553
554 static void
555 update_surface_position(struct ivi_layout_surface *ivisurf)
556 {
557         struct weston_view *view = ivisurf->view;
558         float tx  = (float)ivisurf->prop.dest_x;
559         float ty  = (float)ivisurf->prop.dest_y;
560         struct weston_matrix *matrix = &ivisurf->surface_pos.matrix;
561
562         if (view == NULL) {
563                 return;
564         }
565
566         wl_list_remove(&ivisurf->surface_pos.link);
567
568         weston_matrix_init(matrix);
569         weston_matrix_translate(matrix, tx, ty, 0.0f);
570         wl_list_insert(&view->geometry.transformation_list,
571                        &ivisurf->surface_pos.link);
572
573         weston_view_set_transform_parent(view, NULL);
574         weston_view_update_transform(view);
575 }
576
577 static void
578 update_layer_position(struct ivi_layout_layer *ivilayer,
579                       struct ivi_layout_surface *ivisurf)
580 {
581         struct weston_view *view = ivisurf->view;
582         struct weston_matrix *matrix = &ivisurf->layer_pos.matrix;
583         float tx  = (float)ivilayer->prop.dest_x;
584         float ty  = (float)ivilayer->prop.dest_y;
585
586         if (view == NULL) {
587                 return;
588         }
589
590         wl_list_remove(&ivisurf->layer_pos.link);
591
592         weston_matrix_init(matrix);
593         weston_matrix_translate(matrix, tx, ty, 0.0f);
594         wl_list_insert(&view->geometry.transformation_list,
595                        &ivisurf->layer_pos.link);
596
597         weston_view_set_transform_parent(view, NULL);
598         weston_view_update_transform(view);
599 }
600
601 static void
602 update_scale(struct ivi_layout_layer *ivilayer,
603              struct ivi_layout_surface *ivisurf)
604 {
605         struct weston_view *view = ivisurf->view;
606         struct weston_matrix *matrix = &ivisurf->scaling.matrix;
607         float sx = 0.0f;
608         float sy = 0.0f;
609         float lw = 0.0f;
610         float sw = 0.0f;
611         float lh = 0.0f;
612         float sh = 0.0f;
613
614         if (view == NULL) {
615                 return;
616         }
617
618         if (ivisurf->prop.dest_width == 0 && ivisurf->prop.dest_height == 0) {
619                 ivisurf->prop.dest_width  = ivisurf->surface->width_from_buffer;
620                 ivisurf->prop.dest_height = ivisurf->surface->height_from_buffer;
621         }
622
623         lw = ((float)ivilayer->prop.dest_width  / (float)ivilayer->prop.source_width );
624         sw = ((float)ivisurf->prop.dest_width   / (float)ivisurf->prop.source_width  );
625         lh = ((float)ivilayer->prop.dest_height / (float)ivilayer->prop.source_height);
626         sh = ((float)ivisurf->prop.dest_height  / (float)ivisurf->prop.source_height );
627         sx = sw * lw;
628         sy = sh * lh;
629
630         wl_list_remove(&ivisurf->scaling.link);
631         weston_matrix_init(matrix);
632         weston_matrix_scale(matrix, sx, sy, 1.0f);
633
634         wl_list_insert(&view->geometry.transformation_list,
635                        &ivisurf->scaling.link);
636
637         weston_view_set_transform_parent(view, NULL);
638         weston_view_update_transform(view);
639 }
640
641 static void
642 update_prop(struct ivi_layout_layer *ivilayer,
643             struct ivi_layout_surface *ivisurf)
644 {
645         if (ivilayer->event_mask | ivisurf->event_mask) {
646                 update_opacity(ivilayer, ivisurf);
647                 update_layer_orientation(ivilayer, ivisurf);
648                 update_layer_position(ivilayer, ivisurf);
649                 update_surface_position(ivisurf);
650                 update_surface_orientation(ivilayer, ivisurf);
651                 update_scale(ivilayer, ivisurf);
652
653                 ivisurf->update_count++;
654
655                 if (ivisurf->view != NULL) {
656                   weston_view_geometry_dirty(ivisurf->view);
657                 }
658
659                 if (ivisurf->surface != NULL) {
660                         weston_surface_damage(ivisurf->surface);
661                 }
662         }
663 }
664
665 static void
666 commit_changes(struct ivi_layout *layout)
667 {
668         struct ivi_layout_screen  *iviscrn  = NULL;
669         struct ivi_layout_layer   *ivilayer = NULL;
670         struct ivi_layout_surface *ivisurf  = NULL;
671
672         wl_list_for_each(iviscrn, &layout->screen_list, link) {
673                 wl_list_for_each(ivilayer, &iviscrn->order.layer_list, order.link) {
674                         wl_list_for_each(ivisurf, &ivilayer->order.surface_list, order.link) {
675                                 update_prop(ivilayer, ivisurf);
676                         }
677                 }
678         }
679 }
680
681 static void
682 commit_surface_list(struct ivi_layout *layout)
683 {
684         struct ivi_layout_surface *ivisurf = NULL;
685         int32_t dest_x = 0;
686         int32_t dest_y = 0;
687         int32_t dest_width = 0;
688         int32_t dest_height = 0;
689         int32_t configured = 0;
690
691         wl_list_for_each(ivisurf, &layout->surface_list, link) {
692                 if(ivisurf->pending.prop.transition_type == IVI_LAYOUT_TRANSITION_VIEW_DEFAULT) {
693                         dest_x = ivisurf->prop.dest_x;
694                         dest_y = ivisurf->prop.dest_y;
695                         dest_width = ivisurf->prop.dest_width;
696                         dest_height = ivisurf->prop.dest_height;
697
698                         ivi_layout_transition_move_resize_view(ivisurf,
699                                                                ivisurf->pending.prop.dest_x,
700                                                                ivisurf->pending.prop.dest_y,
701                                                                ivisurf->pending.prop.dest_width,
702                                                                ivisurf->pending.prop.dest_height,
703                                                                ivisurf->pending.prop.transition_duration);
704
705                         if(ivisurf->pending.prop.visibility) {
706                                 ivi_layout_transition_visibility_on(ivisurf, ivisurf->pending.prop.transition_duration);
707                         } else {
708                                 ivi_layout_transition_visibility_off(ivisurf, ivisurf->pending.prop.transition_duration);
709                         }
710
711                         ivisurf->prop = ivisurf->pending.prop;
712                         ivisurf->prop.dest_x = dest_x;
713                         ivisurf->prop.dest_y = dest_y;
714                         ivisurf->prop.dest_width = dest_width;
715                         ivisurf->prop.dest_height = dest_height;
716                         ivisurf->prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;
717                         ivisurf->pending.prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;
718
719                 } else if(ivisurf->pending.prop.transition_type == IVI_LAYOUT_TRANSITION_VIEW_DEST_RECT_ONLY){
720                         dest_x = ivisurf->prop.dest_x;
721                         dest_y = ivisurf->prop.dest_y;
722                         dest_width = ivisurf->prop.dest_width;
723                         dest_height = ivisurf->prop.dest_height;
724
725                         ivi_layout_transition_move_resize_view(ivisurf,
726                                                                ivisurf->pending.prop.dest_x,
727                                                                ivisurf->pending.prop.dest_y,
728                                                                ivisurf->pending.prop.dest_width,
729                                                                ivisurf->pending.prop.dest_height,
730                                                                ivisurf->pending.prop.transition_duration);
731
732                         ivisurf->prop = ivisurf->pending.prop;
733                         ivisurf->prop.dest_x = dest_x;
734                         ivisurf->prop.dest_y = dest_y;
735                         ivisurf->prop.dest_width = dest_width;
736                         ivisurf->prop.dest_height = dest_height;
737
738                         ivisurf->prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;
739                         ivisurf->pending.prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;
740
741                 } else if(ivisurf->pending.prop.transition_type == IVI_LAYOUT_TRANSITION_VIEW_FADE_ONLY){
742                         configured = 0;
743                         if(ivisurf->pending.prop.visibility) {
744                                 ivi_layout_transition_visibility_on(ivisurf, ivisurf->pending.prop.transition_duration);
745                         } else {
746                                 ivi_layout_transition_visibility_off(ivisurf, ivisurf->pending.prop.transition_duration);
747                         }
748
749                         if (ivisurf->prop.dest_width  != ivisurf->pending.prop.dest_width ||
750                             ivisurf->prop.dest_height != ivisurf->pending.prop.dest_height) {
751                                 configured = 1;
752                         }
753
754                         ivisurf->prop = ivisurf->pending.prop;
755                         ivisurf->prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;
756                         ivisurf->pending.prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;
757
758                         if (configured && !is_surface_transition(ivisurf))
759                                 wl_signal_emit(&ivisurf->configured, ivisurf);
760                 } else {
761                         configured = 0;
762                         if (ivisurf->prop.dest_width  != ivisurf->pending.prop.dest_width ||
763                             ivisurf->prop.dest_height != ivisurf->pending.prop.dest_height) {
764                                 configured = 1;
765                         }
766
767                         ivisurf->prop = ivisurf->pending.prop;
768                         ivisurf->prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;
769                         ivisurf->pending.prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;
770
771                         if (configured && !is_surface_transition(ivisurf))
772                                 wl_signal_emit(&ivisurf->configured, ivisurf);
773                 }
774         }
775 }
776
777 static void
778 commit_layer_list(struct ivi_layout *layout)
779 {
780         struct ivi_layout_layer   *ivilayer = NULL;
781         struct ivi_layout_surface *ivisurf  = NULL;
782         struct ivi_layout_surface *next     = NULL;
783
784         wl_list_for_each(ivilayer, &layout->layer_list, link) {
785                 if(ivilayer->pending.prop.transition_type == IVI_LAYOUT_TRANSITION_LAYER_MOVE) {
786                         ivi_layout_transition_move_layer(ivilayer, ivilayer->pending.prop.dest_x, ivilayer->pending.prop.dest_y, ivilayer->pending.prop.transition_duration);
787                 } else if(ivilayer->pending.prop.transition_type == IVI_LAYOUT_TRANSITION_LAYER_FADE) {
788                         ivi_layout_transition_fade_layer(ivilayer,ivilayer->pending.prop.is_fade_in,
789                                                          ivilayer->pending.prop.start_alpha,ivilayer->pending.prop.end_alpha,
790                                                          NULL, NULL,
791                                                          ivilayer->pending.prop.transition_duration);
792                 }
793                 ivilayer->pending.prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;
794
795                 ivilayer->prop = ivilayer->pending.prop;
796
797                 if (!(ivilayer->event_mask &
798                       (IVI_NOTIFICATION_ADD | IVI_NOTIFICATION_REMOVE)) ) {
799                         continue;
800                 }
801
802                 if (ivilayer->event_mask & IVI_NOTIFICATION_REMOVE) {
803                         wl_list_for_each_safe(ivisurf, next,
804                                 &ivilayer->order.surface_list, order.link) {
805                                 remove_ordersurface_from_layer(ivisurf);
806
807                                 if (!wl_list_empty(&ivisurf->order.link)) {
808                                         wl_list_remove(&ivisurf->order.link);
809                                 }
810
811                                 wl_list_init(&ivisurf->order.link);
812                                 ivisurf->event_mask |= IVI_NOTIFICATION_REMOVE;
813                         }
814
815                         wl_list_init(&ivilayer->order.surface_list);
816                 }
817
818                 if (ivilayer->event_mask & IVI_NOTIFICATION_ADD) {
819                         wl_list_for_each_safe(ivisurf, next,
820                                               &ivilayer->order.surface_list, order.link) {
821                                 remove_ordersurface_from_layer(ivisurf);
822
823                                 if (!wl_list_empty(&ivisurf->order.link)) {
824                                         wl_list_remove(&ivisurf->order.link);
825                                 }
826
827                                 wl_list_init(&ivisurf->order.link);
828                         }
829
830                         wl_list_init(&ivilayer->order.surface_list);
831                         wl_list_for_each(ivisurf, &ivilayer->pending.surface_list,
832                                          pending.link) {
833                                 if(!wl_list_empty(&ivisurf->order.link)){
834                                         wl_list_remove(&ivisurf->order.link);
835                                         wl_list_init(&ivisurf->order.link);
836                                 }
837
838                                 wl_list_insert(&ivilayer->order.surface_list,
839                                                &ivisurf->order.link);
840                                 add_ordersurface_to_layer(ivisurf, ivilayer);
841                                 ivisurf->event_mask |= IVI_NOTIFICATION_ADD;
842                         }
843                 }
844         }
845 }
846
847
848
849 static void
850 commit_screen_list(struct ivi_layout *layout)
851 {
852         struct ivi_layout_screen  *iviscrn  = NULL;
853         struct ivi_layout_layer   *ivilayer = NULL;
854         struct ivi_layout_layer   *next     = NULL;
855         struct ivi_layout_surface *ivisurf  = NULL;
856         struct weston_view        *view, *n;
857
858         /* clear view list of layout layer */
859         wl_list_for_each_safe(view, n, &layout->layout_layer.view_list.link, layer_link.link) {
860                 weston_layer_entry_remove(&view->layer_link);
861         }
862
863
864         wl_list_for_each(iviscrn, &layout->screen_list, link) {
865                 if (iviscrn->event_mask & IVI_NOTIFICATION_REMOVE) {
866                         wl_list_for_each_safe(ivilayer, next,
867                                               &iviscrn->order.layer_list, order.link) {
868                                 remove_orderlayer_from_screen(ivilayer);
869
870                                 if (!wl_list_empty(&ivilayer->order.link)) {
871                                     wl_list_remove(&ivilayer->order.link);
872                                 }
873
874                                 wl_list_init(&ivilayer->order.link);
875                                 ivilayer->event_mask |= IVI_NOTIFICATION_REMOVE;
876                         }
877                 }
878
879                 if (iviscrn->event_mask & IVI_NOTIFICATION_ADD) {
880                         wl_list_for_each_safe(ivilayer, next,
881                                               &iviscrn->order.layer_list, order.link) {
882                                 remove_orderlayer_from_screen(ivilayer);
883
884                                 if (!wl_list_empty(&ivilayer->order.link)) {
885                                         wl_list_remove(&ivilayer->order.link);
886                                 }
887
888                                 wl_list_init(&ivilayer->order.link);
889                         }
890
891                         wl_list_init(&iviscrn->order.layer_list);
892                         wl_list_for_each(ivilayer, &iviscrn->pending.layer_list,
893                                          pending.link) {
894                                 wl_list_insert(&iviscrn->order.layer_list,
895                                                &ivilayer->order.link);
896                                 add_orderlayer_to_screen(ivilayer, iviscrn);
897                                 ivilayer->event_mask |= IVI_NOTIFICATION_ADD;
898                         }
899                 }
900
901                 iviscrn->event_mask = 0;
902
903                 /* rebuild view list of layout layer */
904                 wl_list_for_each(ivilayer, &iviscrn->order.layer_list, order.link) {
905                         wl_list_for_each(ivisurf, &ivilayer->order.surface_list, order.link) {
906                                 if (ivisurf->surface == NULL || ivisurf->view == NULL)
907                                         continue;
908
909                                 if (ivilayer->prop.visibility == false ||
910                                     ivisurf->prop.visibility  == false) {
911                                         weston_view_unmap(ivisurf->view);
912                                 }
913                                 else {
914                                         weston_layer_entry_insert(&layout->layout_layer.view_list,
915                                                           &ivisurf->view->layer_link);
916
917                                         if (!weston_view_is_mapped(ivisurf->view) ||
918                                             (ivilayer->event_mask & IVI_NOTIFICATION_ADD)) {
919                                                 weston_view_geometry_dirty(ivisurf->view);
920                                                 weston_view_update_transform(ivisurf->view);
921                                         }
922                                 }
923                         }
924                 }
925         }
926 }
927
928 static void
929 commit_transition(struct ivi_layout* layout)
930 {
931         if(wl_list_empty(&layout->pending_transition_list)){
932                 return;
933         }
934
935         wl_list_insert_list(&layout->transitions->transition_list,
936                             &layout->pending_transition_list);
937
938         wl_list_init(&layout->pending_transition_list);
939
940         wl_event_source_timer_update(layout->transitions->event_source, 1);
941 }
942
943 static void
944 send_surface_prop(struct ivi_layout_surface *ivisurf)
945 {
946         wl_signal_emit(&ivisurf->property_changed, ivisurf);
947         ivisurf->event_mask = 0;
948 }
949
950 static void
951 send_layer_prop(struct ivi_layout_layer *ivilayer)
952 {
953         wl_signal_emit(&ivilayer->property_changed, ivilayer);
954         ivilayer->event_mask = 0;
955 }
956
957 static void
958 send_prop(struct ivi_layout *layout)
959 {
960         struct ivi_layout_layer   *ivilayer = NULL;
961         struct ivi_layout_surface *ivisurf  = NULL;
962
963         wl_list_for_each_reverse(ivilayer, &layout->layer_list, link) {
964                 send_layer_prop(ivilayer);
965         }
966
967         wl_list_for_each_reverse(ivisurf, &layout->surface_list, link) {
968                 send_surface_prop(ivisurf);
969         }
970 }
971
972 static void
973 clear_surface_pending_list(struct ivi_layout_layer *ivilayer)
974 {
975         struct ivi_layout_surface *surface_link = NULL;
976         struct ivi_layout_surface *surface_next = NULL;
977
978         wl_list_for_each_safe(surface_link, surface_next,
979                               &ivilayer->pending.surface_list, pending.link) {
980                 if (!wl_list_empty(&surface_link->pending.link)) {
981                         wl_list_remove(&surface_link->pending.link);
982                 }
983
984                 wl_list_init(&surface_link->pending.link);
985         }
986
987         ivilayer->event_mask |= IVI_NOTIFICATION_REMOVE;
988 }
989
990 static void
991 clear_surface_order_list(struct ivi_layout_layer *ivilayer)
992 {
993         struct ivi_layout_surface *surface_link = NULL;
994         struct ivi_layout_surface *surface_next = NULL;
995
996         wl_list_for_each_safe(surface_link, surface_next,
997                               &ivilayer->order.surface_list, order.link) {
998                 if (!wl_list_empty(&surface_link->order.link)) {
999                         wl_list_remove(&surface_link->order.link);
1000                 }
1001
1002                 wl_list_init(&surface_link->order.link);
1003         }
1004
1005         ivilayer->event_mask |= IVI_NOTIFICATION_REMOVE;
1006 }
1007
1008 static void
1009 layer_created(struct wl_listener *listener, void *data)
1010 {
1011         struct ivi_layout_layer *ivilayer = data;
1012
1013         struct listener_layout_notification *notification =
1014                 container_of(listener,
1015                              struct listener_layout_notification,
1016                              listener);
1017
1018         struct ivi_layout_notification_callback *created_callback =
1019                 notification->userdata;
1020
1021         ((layer_create_notification_func)created_callback->callback)
1022                 (ivilayer, created_callback->data);
1023 }
1024
1025 static void
1026 layer_removed(struct wl_listener *listener, void *data)
1027 {
1028         struct ivi_layout_layer *ivilayer = data;
1029
1030         struct listener_layout_notification *notification =
1031                 container_of(listener,
1032                              struct listener_layout_notification,
1033                              listener);
1034
1035         struct ivi_layout_notification_callback *removed_callback =
1036                 notification->userdata;
1037
1038         ((layer_remove_notification_func)removed_callback->callback)
1039                 (ivilayer, removed_callback->data);
1040 }
1041
1042 static void
1043 layer_prop_changed(struct wl_listener *listener, void *data)
1044 {
1045         struct ivi_layout_layer *ivilayer = data;
1046
1047         struct listener_layout_notification *layout_listener =
1048                 container_of(listener,
1049                              struct listener_layout_notification,
1050                              listener);
1051
1052         struct ivi_layout_notification_callback *prop_callback =
1053                 layout_listener->userdata;
1054
1055         ((layer_property_notification_func)prop_callback->callback)
1056                 (ivilayer, &ivilayer->prop, ivilayer->event_mask, prop_callback->data);
1057 }
1058
1059 static void
1060 surface_created(struct wl_listener *listener, void *data)
1061 {
1062         struct ivi_layout_surface *ivisurface = data;
1063
1064         struct listener_layout_notification *notification =
1065                 container_of(listener,
1066                              struct listener_layout_notification,
1067                              listener);
1068
1069         struct ivi_layout_notification_callback *created_callback =
1070                 notification->userdata;
1071
1072         ((surface_create_notification_func)created_callback->callback)
1073                 (ivisurface, created_callback->data);
1074 }
1075
1076 static void
1077 surface_removed(struct wl_listener *listener, void *data)
1078 {
1079         struct ivi_layout_surface *ivisurface = data;
1080
1081         struct listener_layout_notification *notification =
1082                 container_of(listener,
1083                              struct listener_layout_notification,
1084                              listener);
1085
1086         struct ivi_layout_notification_callback *removed_callback =
1087                 notification->userdata;
1088
1089         ((surface_remove_notification_func)removed_callback->callback)
1090                 (ivisurface, removed_callback->data);
1091 }
1092
1093 static void
1094 surface_prop_changed(struct wl_listener *listener, void *data)
1095 {
1096         struct ivi_layout_surface *ivisurf = data;
1097
1098         struct listener_layout_notification *layout_listener =
1099                 container_of(listener,
1100                              struct listener_layout_notification,
1101                              listener);
1102
1103         struct ivi_layout_notification_callback *prop_callback =
1104                 layout_listener->userdata;
1105
1106         ((surface_property_notification_func)prop_callback->callback)
1107                 (ivisurf, &ivisurf->prop, ivisurf->event_mask, prop_callback->data);
1108 }
1109
1110 static void
1111 surface_configure_changed(struct wl_listener *listener,
1112                           void *data)
1113 {
1114         struct ivi_layout_surface *ivisurface = data;
1115
1116         struct listener_layout_notification *notification =
1117                 container_of(listener,
1118                              struct listener_layout_notification,
1119                              listener);
1120
1121         struct ivi_layout_notification_callback *configure_changed_callback =
1122                 notification->userdata;
1123
1124         ((surface_configure_notification_func)configure_changed_callback->callback)
1125                 (ivisurface, configure_changed_callback->data);
1126 }
1127
1128 static int32_t
1129 add_notification(struct wl_signal *signal,
1130                  wl_notify_func_t callback,
1131                  void *userdata)
1132 {
1133         struct listener_layout_notification *notification = NULL;
1134
1135         notification = malloc(sizeof *notification);
1136         if (notification == NULL) {
1137                 weston_log("fails to allocate memory\n");
1138                 free(userdata);
1139                 return IVI_FAILED;
1140         }
1141
1142         notification->listener.notify = callback;
1143         notification->userdata = userdata;
1144
1145         wl_signal_add(signal, &notification->listener);
1146
1147         return IVI_SUCCEEDED;
1148 }
1149
1150 static void
1151 remove_notification(struct wl_list *listener_list, void *callback, void *userdata)
1152 {
1153         struct wl_listener *listener = NULL;
1154         struct wl_listener *next = NULL;
1155
1156         wl_list_for_each_safe(listener, next, listener_list, link) {
1157                 struct listener_layout_notification *notification =
1158                         container_of(listener,
1159                                      struct listener_layout_notification,
1160                                      listener);
1161
1162                 struct ivi_layout_notification_callback *notification_callback =
1163                         notification->userdata;
1164
1165                 if ((notification_callback->callback != callback) ||
1166                     (notification_callback->data != userdata)) {
1167                         continue;
1168                 }
1169
1170                 if (!wl_list_empty(&listener->link)) {
1171                         wl_list_remove(&listener->link);
1172                 }
1173
1174                 free(notification->userdata);
1175                 free(notification);
1176         }
1177 }
1178
1179 static void
1180 remove_all_notification(struct wl_list *listener_list)
1181 {
1182         struct wl_listener *listener = NULL;
1183         struct wl_listener *next = NULL;
1184
1185         wl_list_for_each_safe(listener, next, listener_list, link) {
1186                 struct listener_layout_notification *notification = NULL;
1187                 if (!wl_list_empty(&listener->link)) {
1188                         wl_list_remove(&listener->link);
1189                 }
1190
1191                 notification =
1192                         container_of(listener,
1193                                      struct listener_layout_notification,
1194                                      listener);
1195
1196                 free(notification->userdata);
1197                 free(notification);
1198         }
1199 }
1200
1201 /**
1202  * Exported APIs of ivi-layout library are implemented from here.
1203  * Brief of APIs is described in ivi-layout-export.h.
1204  */
1205 WL_EXPORT int32_t
1206 ivi_layout_add_notification_create_layer(layer_create_notification_func callback,
1207                                          void *userdata)
1208 {
1209         struct ivi_layout *layout = get_layout_instance();
1210         struct ivi_layout_notification_callback *created_callback = NULL;
1211
1212         if (callback == NULL) {
1213                 weston_log("ivi_layout_add_notification_create_layer: invalid argument\n");
1214                 return IVI_FAILED;
1215         }
1216
1217         created_callback = malloc(sizeof *created_callback);
1218         if (created_callback == NULL) {
1219                 weston_log("fails to allocate memory\n");
1220                 return IVI_FAILED;
1221         }
1222
1223         created_callback->callback = callback;
1224         created_callback->data = userdata;
1225
1226         return add_notification(&layout->layer_notification.created,
1227                                 layer_created,
1228                                 created_callback);
1229 }
1230
1231 WL_EXPORT void
1232 ivi_layout_remove_notification_create_layer(layer_create_notification_func callback,
1233                                             void *userdata)
1234 {
1235         struct ivi_layout *layout = get_layout_instance();
1236         remove_notification(&layout->layer_notification.created.listener_list, callback, userdata);
1237 }
1238
1239 WL_EXPORT int32_t
1240 ivi_layout_add_notification_remove_layer(layer_remove_notification_func callback,
1241                                          void *userdata)
1242 {
1243         struct ivi_layout *layout = get_layout_instance();
1244         struct ivi_layout_notification_callback *removed_callback = NULL;
1245
1246         if (callback == NULL) {
1247                 weston_log("ivi_layout_add_notification_remove_layer: invalid argument\n");
1248                 return IVI_FAILED;
1249         }
1250
1251         removed_callback = malloc(sizeof *removed_callback);
1252         if (removed_callback == NULL) {
1253                 weston_log("fails to allocate memory\n");
1254                 return IVI_FAILED;
1255         }
1256
1257         removed_callback->callback = callback;
1258         removed_callback->data = userdata;
1259         return add_notification(&layout->layer_notification.removed,
1260                                 layer_removed,
1261                                 removed_callback);
1262 }
1263
1264 WL_EXPORT void
1265 ivi_layout_remove_notification_remove_layer(layer_remove_notification_func callback,
1266                                             void *userdata)
1267 {
1268         struct ivi_layout *layout = get_layout_instance();
1269         remove_notification(&layout->layer_notification.removed.listener_list, callback, userdata);
1270 }
1271
1272 WL_EXPORT int32_t
1273 ivi_layout_add_notification_create_surface(surface_create_notification_func callback,
1274                                            void *userdata)
1275 {
1276         struct ivi_layout *layout = get_layout_instance();
1277         struct ivi_layout_notification_callback *created_callback = NULL;
1278
1279         if (callback == NULL) {
1280                 weston_log("ivi_layout_add_notification_create_surface: invalid argument\n");
1281                 return IVI_FAILED;
1282         }
1283
1284         created_callback = malloc(sizeof *created_callback);
1285         if (created_callback == NULL) {
1286                 weston_log("fails to allocate memory\n");
1287                 return IVI_FAILED;
1288         }
1289
1290         created_callback->callback = callback;
1291         created_callback->data = userdata;
1292
1293         return add_notification(&layout->surface_notification.created,
1294                                 surface_created,
1295                                 created_callback);
1296 }
1297
1298 WL_EXPORT void
1299 ivi_layout_remove_notification_create_surface(surface_create_notification_func callback,
1300                                               void *userdata)
1301 {
1302         struct ivi_layout *layout = get_layout_instance();
1303         remove_notification(&layout->surface_notification.created.listener_list, callback, userdata);
1304 }
1305
1306 WL_EXPORT int32_t
1307 ivi_layout_add_notification_remove_surface(surface_remove_notification_func callback,
1308                                            void *userdata)
1309 {
1310         struct ivi_layout *layout = get_layout_instance();
1311         struct ivi_layout_notification_callback *removed_callback = NULL;
1312
1313         if (callback == NULL) {
1314                 weston_log("ivi_layout_add_notification_remove_surface: invalid argument\n");
1315                 return IVI_FAILED;
1316         }
1317
1318         removed_callback = malloc(sizeof *removed_callback);
1319         if (removed_callback == NULL) {
1320                 weston_log("fails to allocate memory\n");
1321                 return IVI_FAILED;
1322         }
1323
1324         removed_callback->callback = callback;
1325         removed_callback->data = userdata;
1326
1327         return add_notification(&layout->surface_notification.removed,
1328                                 surface_removed,
1329                                 removed_callback);
1330 }
1331
1332 WL_EXPORT void
1333 ivi_layout_remove_notification_remove_surface(surface_remove_notification_func callback,
1334                                               void *userdata)
1335 {
1336         struct ivi_layout *layout = get_layout_instance();
1337         remove_notification(&layout->surface_notification.removed.listener_list, callback, userdata);
1338 }
1339
1340 WL_EXPORT int32_t
1341 ivi_layout_add_notification_configure_surface(surface_configure_notification_func callback,
1342                                               void *userdata)
1343 {
1344         struct ivi_layout *layout = get_layout_instance();
1345         struct ivi_layout_notification_callback *configure_changed_callback = NULL;
1346         if (callback == NULL) {
1347                 weston_log("ivi_layout_add_notification_configure_surface: invalid argument\n");
1348                 return IVI_FAILED;
1349         }
1350
1351         configure_changed_callback = malloc(sizeof *configure_changed_callback);
1352         if (configure_changed_callback == NULL) {
1353                 weston_log("fails to allocate memory\n");
1354                 return IVI_FAILED;
1355         }
1356
1357         configure_changed_callback->callback = callback;
1358         configure_changed_callback->data = userdata;
1359
1360         return add_notification(&layout->surface_notification.configure_changed,
1361                                 surface_configure_changed,
1362                                 configure_changed_callback);
1363 }
1364
1365 WL_EXPORT void
1366 ivi_layout_remove_notification_configure_surface(surface_configure_notification_func callback,
1367                                                  void *userdata)
1368 {
1369         struct ivi_layout *layout = get_layout_instance();
1370         remove_notification(&layout->surface_notification.configure_changed.listener_list, callback, userdata);
1371 }
1372
1373 WL_EXPORT uint32_t
1374 ivi_layout_get_id_of_surface(struct ivi_layout_surface *ivisurf)
1375 {
1376         return ivisurf->id_surface;
1377 }
1378
1379 WL_EXPORT uint32_t
1380 ivi_layout_get_id_of_layer(struct ivi_layout_layer *ivilayer)
1381 {
1382         return ivilayer->id_layer;
1383 }
1384
1385 WL_EXPORT struct ivi_layout_layer *
1386 ivi_layout_get_layer_from_id(uint32_t id_layer)
1387 {
1388         struct ivi_layout *layout = get_layout_instance();
1389         struct ivi_layout_layer *ivilayer = NULL;
1390
1391         wl_list_for_each(ivilayer, &layout->layer_list, link) {
1392                 if (ivilayer->id_layer == id_layer) {
1393                         return ivilayer;
1394                 }
1395         }
1396
1397         return NULL;
1398 }
1399
1400 WL_EXPORT struct ivi_layout_surface *
1401 ivi_layout_get_surface_from_id(uint32_t id_surface)
1402 {
1403         struct ivi_layout *layout = get_layout_instance();
1404         struct ivi_layout_surface *ivisurf = NULL;
1405
1406         wl_list_for_each(ivisurf, &layout->surface_list, link) {
1407                 if (ivisurf->id_surface == id_surface) {
1408                         return ivisurf;
1409                 }
1410         }
1411
1412         return NULL;
1413 }
1414
1415 WL_EXPORT struct ivi_layout_screen *
1416 ivi_layout_get_screen_from_id(uint32_t id_screen)
1417 {
1418         struct ivi_layout *layout = get_layout_instance();
1419         struct ivi_layout_screen *iviscrn = NULL;
1420
1421         wl_list_for_each(iviscrn, &layout->screen_list, link) {
1422 /* FIXME : select iviscrn from screen_list by id_screen */
1423                 return iviscrn;
1424                 break;
1425         }
1426
1427         return NULL;
1428 }
1429
1430 WL_EXPORT int32_t
1431 ivi_layout_get_screen_resolution(struct ivi_layout_screen *iviscrn,
1432                                  int32_t *pWidth, int32_t *pHeight)
1433 {
1434         struct weston_output *output = NULL;
1435
1436         if (pWidth == NULL || pHeight == NULL) {
1437                 weston_log("ivi_layout_get_screen_resolution: invalid argument\n");
1438                 return IVI_FAILED;
1439         }
1440
1441         output   = iviscrn->output;
1442         *pWidth  = output->current_mode->width;
1443         *pHeight = output->current_mode->height;
1444
1445         return IVI_SUCCEEDED;
1446 }
1447
1448 WL_EXPORT int32_t
1449 ivi_layout_surface_add_notification(struct ivi_layout_surface *ivisurf,
1450                                     surface_property_notification_func callback,
1451                                     void *userdata)
1452 {
1453         struct listener_layout_notification* notification = NULL;
1454         struct ivi_layout_notification_callback *prop_callback = NULL;
1455
1456         if (ivisurf == NULL || callback == NULL) {
1457                 weston_log("ivi_layout_surface_add_notification: invalid argument\n");
1458                 return IVI_FAILED;
1459         }
1460
1461         notification = malloc(sizeof *notification);
1462         if (notification == NULL) {
1463                 weston_log("fails to allocate memory\n");
1464                 return IVI_FAILED;
1465         }
1466
1467         prop_callback = malloc(sizeof *prop_callback);
1468         if (prop_callback == NULL) {
1469                 weston_log("fails to allocate memory\n");
1470                 return IVI_FAILED;
1471         }
1472
1473         prop_callback->callback = callback;
1474         prop_callback->data = userdata;
1475
1476         notification->listener.notify = surface_prop_changed;
1477         notification->userdata = prop_callback;
1478
1479         wl_signal_add(&ivisurf->property_changed, &notification->listener);
1480
1481         return IVI_SUCCEEDED;
1482 }
1483
1484 WL_EXPORT void
1485 ivi_layout_surface_remove_notification(struct ivi_layout_surface *ivisurf)
1486 {
1487         if (ivisurf == NULL) {
1488                 weston_log("ivi_layout_surface_remove_notification: invalid argument\n");
1489                 return;
1490         }
1491
1492         remove_all_notification(&ivisurf->property_changed.listener_list);
1493 }
1494
1495 static void
1496 remove_configured_listener(struct ivi_layout_surface *ivisurf)
1497 {
1498         struct wl_listener *link = NULL;
1499         struct wl_listener *next = NULL;
1500
1501         wl_list_for_each_safe(link, next, &ivisurf->configured.listener_list, link) {
1502                 wl_list_remove(&link->link);
1503         }
1504 }
1505
1506 WL_EXPORT void
1507 ivi_layout_surface_remove(struct ivi_layout_surface *ivisurf)
1508 {
1509         struct ivi_layout *layout = get_layout_instance();
1510
1511         if (ivisurf == NULL) {
1512                 weston_log("ivi_layout_surface_remove: invalid argument\n");
1513                 return;
1514         }
1515
1516         if (!wl_list_empty(&ivisurf->pending.link)) {
1517                 wl_list_remove(&ivisurf->pending.link);
1518         }
1519         if (!wl_list_empty(&ivisurf->order.link)) {
1520                 wl_list_remove(&ivisurf->order.link);
1521         }
1522         if (!wl_list_empty(&ivisurf->link)) {
1523                 wl_list_remove(&ivisurf->link);
1524         }
1525         remove_ordersurface_from_layer(ivisurf);
1526
1527         wl_signal_emit(&layout->surface_notification.removed, ivisurf);
1528
1529         remove_configured_listener(ivisurf);
1530
1531         ivi_layout_surface_remove_notification(ivisurf);
1532
1533         free(ivisurf);
1534 }
1535
1536 WL_EXPORT const struct ivi_layout_layer_properties *
1537 ivi_layout_get_properties_of_layer(struct ivi_layout_layer *ivilayer)
1538 {
1539         if (ivilayer == NULL) {
1540                 weston_log("ivi_layout_get_properties_of_layer: invalid argument\n");
1541                 return NULL;
1542         }
1543
1544         return &ivilayer->prop;
1545 }
1546
1547 WL_EXPORT int32_t
1548 ivi_layout_get_screens(int32_t *pLength, struct ivi_layout_screen ***ppArray)
1549 {
1550         struct ivi_layout *layout = get_layout_instance();
1551         struct ivi_layout_screen *iviscrn = NULL;
1552         int32_t length = 0;
1553         int32_t n = 0;
1554
1555         if (pLength == NULL || ppArray == NULL) {
1556                 weston_log("ivi_layout_get_screens: invalid argument\n");
1557                 return IVI_FAILED;
1558         }
1559
1560         length = wl_list_length(&layout->screen_list);
1561
1562         if (length != 0){
1563                 /* the Array must be free by module which called this function */
1564                 *ppArray = calloc(length, sizeof(struct ivi_layout_screen *));
1565                 if (*ppArray == NULL) {
1566                         weston_log("fails to allocate memory\n");
1567                         return IVI_FAILED;
1568                 }
1569
1570                 wl_list_for_each(iviscrn, &layout->screen_list, link) {
1571                         (*ppArray)[n++] = iviscrn;
1572                 }
1573         }
1574
1575         *pLength = length;
1576
1577         return IVI_SUCCEEDED;
1578 }
1579
1580 WL_EXPORT int32_t
1581 ivi_layout_get_screens_under_layer(struct ivi_layout_layer *ivilayer,
1582                                    int32_t *pLength,
1583                                    struct ivi_layout_screen ***ppArray)
1584 {
1585         struct link_screen *link_scrn = NULL;
1586         int32_t length = 0;
1587         int32_t n = 0;
1588
1589         if (ivilayer == NULL || pLength == NULL || ppArray == NULL) {
1590                 weston_log("ivi_layout_get_screens_under_layer: invalid argument\n");
1591                 return IVI_FAILED;
1592         }
1593
1594         length = wl_list_length(&ivilayer->screen_list);
1595
1596         if (length != 0){
1597                 /* the Array must be free by module which called this function */
1598                 *ppArray = calloc(length, sizeof(struct ivi_layout_screen *));
1599                 if (*ppArray == NULL) {
1600                         weston_log("fails to allocate memory\n");
1601                         return IVI_FAILED;
1602                 }
1603
1604                 wl_list_for_each(link_scrn, &ivilayer->screen_list, link) {
1605                         (*ppArray)[n++] = link_scrn->iviscrn;
1606                 }
1607         }
1608
1609         *pLength = length;
1610
1611         return IVI_SUCCEEDED;
1612 }
1613
1614 WL_EXPORT int32_t
1615 ivi_layout_get_layers(int32_t *pLength, struct ivi_layout_layer ***ppArray)
1616 {
1617         struct ivi_layout *layout = get_layout_instance();
1618         struct ivi_layout_layer *ivilayer = NULL;
1619         int32_t length = 0;
1620         int32_t n = 0;
1621
1622         if (pLength == NULL || ppArray == NULL) {
1623                 weston_log("ivi_layout_get_layers: invalid argument\n");
1624                 return IVI_FAILED;
1625         }
1626
1627         length = wl_list_length(&layout->layer_list);
1628
1629         if (length != 0){
1630                 /* the Array must be free by module which called this function */
1631                 *ppArray = calloc(length, sizeof(struct ivi_layout_layer *));
1632                 if (*ppArray == NULL) {
1633                         weston_log("fails to allocate memory\n");
1634                         return IVI_FAILED;
1635                 }
1636
1637                 wl_list_for_each(ivilayer, &layout->layer_list, link) {
1638                         (*ppArray)[n++] = ivilayer;
1639                 }
1640         }
1641
1642         *pLength = length;
1643
1644         return IVI_SUCCEEDED;
1645 }
1646
1647 int32_t
1648 ivi_layout_get_layers_on_screen(struct ivi_layout_screen *iviscrn,
1649                                 int32_t *pLength,
1650                                 struct ivi_layout_layer ***ppArray)
1651 {
1652         struct ivi_layout_layer *ivilayer = NULL;
1653         int32_t length = 0;
1654         int32_t n = 0;
1655
1656         if (iviscrn == NULL || pLength == NULL || ppArray == NULL) {
1657                 weston_log("ivi_layout_get_layers_on_screen: invalid argument\n");
1658                 return IVI_FAILED;
1659         }
1660
1661         length = wl_list_length(&iviscrn->order.layer_list);
1662
1663         if (length != 0){
1664                 /* the Array must be free by module which called this function */
1665                 *ppArray = calloc(length, sizeof(struct ivi_layout_layer *));
1666                 if (*ppArray == NULL) {
1667                         weston_log("fails to allocate memory\n");
1668                         return IVI_FAILED;
1669                 }
1670
1671                 wl_list_for_each(ivilayer, &iviscrn->order.layer_list, link) {
1672                         (*ppArray)[n++] = ivilayer;
1673                 }
1674         }
1675
1676         *pLength = length;
1677
1678         return IVI_SUCCEEDED;
1679 }
1680
1681 WL_EXPORT int32_t
1682 ivi_layout_get_layers_under_surface(struct ivi_layout_surface *ivisurf,
1683                                     int32_t *pLength,
1684                                     struct ivi_layout_layer ***ppArray)
1685 {
1686         struct link_layer *link_layer = NULL;
1687         int32_t length = 0;
1688         int32_t n = 0;
1689
1690         if (ivisurf == NULL || pLength == NULL || ppArray == NULL) {
1691                 weston_log("ivi_layout_getLayers: invalid argument\n");
1692                 return IVI_FAILED;
1693         }
1694
1695         length = wl_list_length(&ivisurf->layer_list);
1696
1697         if (length != 0){
1698                 /* the Array must be free by module which called this function */
1699                 *ppArray = calloc(length, sizeof(struct ivi_layout_layer *));
1700                 if (*ppArray == NULL) {
1701                         weston_log("fails to allocate memory\n");
1702                         return IVI_FAILED;
1703                 }
1704
1705                 wl_list_for_each(link_layer, &ivisurf->layer_list, link) {
1706                         (*ppArray)[n++] = link_layer->ivilayer;
1707                 }
1708         }
1709
1710         *pLength = length;
1711
1712         return IVI_SUCCEEDED;
1713 }
1714
1715 WL_EXPORT int32_t
1716 ivi_layout_get_surfaces(int32_t *pLength, struct ivi_layout_surface ***ppArray)
1717 {
1718         struct ivi_layout *layout = get_layout_instance();
1719         struct ivi_layout_surface *ivisurf = NULL;
1720         int32_t length = 0;
1721         int32_t n = 0;
1722
1723         if (pLength == NULL || ppArray == NULL) {
1724                 weston_log("ivi_layout_get_surfaces: invalid argument\n");
1725                 return IVI_FAILED;
1726         }
1727
1728         length = wl_list_length(&layout->surface_list);
1729
1730         if (length != 0){
1731                 /* the Array must be free by module which called this function */
1732                 *ppArray = calloc(length, sizeof(struct ivi_layout_surface *));
1733                 if (*ppArray == NULL) {
1734                         weston_log("fails to allocate memory\n");
1735                         return IVI_FAILED;
1736                 }
1737
1738                 wl_list_for_each(ivisurf, &layout->surface_list, link) {
1739                         (*ppArray)[n++] = ivisurf;
1740                 }
1741         }
1742
1743         *pLength = length;
1744
1745         return IVI_SUCCEEDED;
1746 }
1747
1748 int32_t
1749 ivi_layout_get_surfaces_on_layer(struct ivi_layout_layer *ivilayer,
1750                                  int32_t *pLength,
1751                                  struct ivi_layout_surface ***ppArray)
1752 {
1753         struct ivi_layout_surface *ivisurf = NULL;
1754         int32_t length = 0;
1755         int32_t n = 0;
1756
1757         if (ivilayer == NULL || pLength == NULL || ppArray == NULL) {
1758                 weston_log("ivi_layout_getSurfaceIDsOnLayer: invalid argument\n");
1759                 return IVI_FAILED;
1760         }
1761
1762         length = wl_list_length(&ivilayer->order.surface_list);
1763
1764         if (length != 0) {
1765                 /* the Array must be free by module which called this function */
1766                 *ppArray = calloc(length, sizeof(struct ivi_layout_surface *));
1767                 if (*ppArray == NULL) {
1768                         weston_log("fails to allocate memory\n");
1769                         return IVI_FAILED;
1770                 }
1771
1772                 wl_list_for_each(ivisurf, &ivilayer->order.surface_list, order.link) {
1773                         (*ppArray)[n++] = ivisurf;
1774                 }
1775         }
1776
1777         *pLength = length;
1778
1779         return IVI_SUCCEEDED;
1780 }
1781
1782 WL_EXPORT struct ivi_layout_layer *
1783 ivi_layout_layer_create_with_dimension(uint32_t id_layer,
1784                                        int32_t width, int32_t height)
1785 {
1786         struct ivi_layout *layout = get_layout_instance();
1787         struct ivi_layout_layer *ivilayer = NULL;
1788
1789         ivilayer = get_layer(&layout->layer_list, id_layer);
1790         if (ivilayer != NULL) {
1791                 weston_log("id_layer is already created\n");
1792                 return ivilayer;
1793         }
1794
1795         ivilayer = calloc(1, sizeof *ivilayer);
1796         if (ivilayer == NULL) {
1797                 weston_log("fails to allocate memory\n");
1798                 return NULL;
1799         }
1800
1801         wl_list_init(&ivilayer->link);
1802         wl_signal_init(&ivilayer->property_changed);
1803         wl_list_init(&ivilayer->screen_list);
1804         wl_list_init(&ivilayer->link_to_surface);
1805         ivilayer->layout = layout;
1806         ivilayer->id_layer = id_layer;
1807
1808         init_layer_properties(&ivilayer->prop, width, height);
1809         ivilayer->event_mask = 0;
1810
1811         wl_list_init(&ivilayer->pending.surface_list);
1812         wl_list_init(&ivilayer->pending.link);
1813         ivilayer->pending.prop = ivilayer->prop;
1814
1815         wl_list_init(&ivilayer->order.surface_list);
1816         wl_list_init(&ivilayer->order.link);
1817
1818         wl_list_insert(&layout->layer_list, &ivilayer->link);
1819
1820         wl_signal_emit(&layout->layer_notification.created, ivilayer);
1821
1822         return ivilayer;
1823 }
1824
1825 WL_EXPORT void
1826 ivi_layout_layer_remove(struct ivi_layout_layer *ivilayer)
1827 {
1828         struct ivi_layout *layout = get_layout_instance();
1829
1830         if (ivilayer == NULL) {
1831                 weston_log("ivi_layout_layer_remove: invalid argument\n");
1832                 return;
1833         }
1834
1835         wl_signal_emit(&layout->layer_notification.removed, ivilayer);
1836
1837         clear_surface_pending_list(ivilayer);
1838         clear_surface_order_list(ivilayer);
1839
1840         if (!wl_list_empty(&ivilayer->pending.link)) {
1841                 wl_list_remove(&ivilayer->pending.link);
1842         }
1843         if (!wl_list_empty(&ivilayer->order.link)) {
1844                 wl_list_remove(&ivilayer->order.link);
1845         }
1846         if (!wl_list_empty(&ivilayer->link)) {
1847                 wl_list_remove(&ivilayer->link);
1848         }
1849         remove_orderlayer_from_screen(ivilayer);
1850         remove_link_to_surface(ivilayer);
1851         ivi_layout_layer_remove_notification(ivilayer);
1852
1853         free(ivilayer);
1854 }
1855
1856 WL_EXPORT int32_t
1857 ivi_layout_layer_set_visibility(struct ivi_layout_layer *ivilayer,
1858                                 bool newVisibility)
1859 {
1860         struct ivi_layout_layer_properties *prop = NULL;
1861
1862         if (ivilayer == NULL) {
1863                 weston_log("ivi_layout_layer_set_visibility: invalid argument\n");
1864                 return IVI_FAILED;
1865         }
1866
1867         prop = &ivilayer->pending.prop;
1868         prop->visibility = newVisibility;
1869
1870         ivilayer->event_mask |= IVI_NOTIFICATION_VISIBILITY;
1871
1872         return IVI_SUCCEEDED;
1873 }
1874
1875 bool
1876 ivi_layout_layer_get_visibility(struct ivi_layout_layer *ivilayer)
1877 {
1878         if (ivilayer == NULL) {
1879                 weston_log("ivi_layout_layer_get_visibility: invalid argument\n");
1880                 return false;
1881         }
1882
1883         return ivilayer->prop.visibility;
1884 }
1885
1886 WL_EXPORT int32_t
1887 ivi_layout_layer_set_opacity(struct ivi_layout_layer *ivilayer,
1888                              wl_fixed_t opacity)
1889 {
1890         struct ivi_layout_layer_properties *prop = NULL;
1891
1892         if (ivilayer == NULL) {
1893                 weston_log("ivi_layout_layer_set_opacity: invalid argument\n");
1894                 return IVI_FAILED;
1895         }
1896
1897         prop = &ivilayer->pending.prop;
1898         prop->opacity = opacity;
1899
1900         ivilayer->event_mask |= IVI_NOTIFICATION_OPACITY;
1901
1902         return IVI_SUCCEEDED;
1903 }
1904
1905 WL_EXPORT wl_fixed_t
1906 ivi_layout_layer_get_opacity(struct ivi_layout_layer *ivilayer)
1907 {
1908         if (ivilayer == NULL) {
1909                 weston_log("ivi_layout_layer_get_opacity: invalid argument\n");
1910                 return wl_fixed_from_double(0.0);
1911         }
1912
1913         return ivilayer->prop.opacity;
1914 }
1915
1916 WL_EXPORT int32_t
1917 ivi_layout_layer_set_source_rectangle(struct ivi_layout_layer *ivilayer,
1918                                       int32_t x, int32_t y,
1919                                       int32_t width, int32_t height)
1920 {
1921         struct ivi_layout_layer_properties *prop = NULL;
1922
1923         if (ivilayer == NULL) {
1924                 weston_log("ivi_layout_layer_set_source_rectangle: invalid argument\n");
1925                 return IVI_FAILED;
1926         }
1927
1928         prop = &ivilayer->pending.prop;
1929         prop->source_x = x;
1930         prop->source_y = y;
1931         prop->source_width = width;
1932         prop->source_height = height;
1933
1934         ivilayer->event_mask |= IVI_NOTIFICATION_SOURCE_RECT;
1935
1936         return IVI_SUCCEEDED;
1937 }
1938
1939 WL_EXPORT int32_t
1940 ivi_layout_layer_set_destination_rectangle(struct ivi_layout_layer *ivilayer,
1941                                            int32_t x, int32_t y,
1942                                            int32_t width, int32_t height)
1943 {
1944         struct ivi_layout_layer_properties *prop = NULL;
1945
1946         if (ivilayer == NULL) {
1947                 weston_log("ivi_layout_layer_set_destination_rectangle: invalid argument\n");
1948                 return IVI_FAILED;
1949         }
1950
1951         prop = &ivilayer->pending.prop;
1952         prop->dest_x = x;
1953         prop->dest_y = y;
1954         prop->dest_width = width;
1955         prop->dest_height = height;
1956
1957         ivilayer->event_mask |= IVI_NOTIFICATION_DEST_RECT;
1958
1959         return IVI_SUCCEEDED;
1960 }
1961
1962 int32_t
1963 ivi_layout_layer_get_dimension(struct ivi_layout_layer *ivilayer,
1964                                int32_t *dest_width, int32_t *dest_height)
1965 {
1966         if (ivilayer == NULL || dest_width == NULL || dest_height == NULL) {
1967                 weston_log("ivi_layout_layer_get_dimension: invalid argument\n");
1968                 return IVI_FAILED;
1969         }
1970
1971         *dest_width = ivilayer->prop.dest_width;
1972         *dest_height = ivilayer->prop.dest_height;
1973
1974         return IVI_SUCCEEDED;
1975 }
1976
1977 int32_t
1978 ivi_layout_layer_set_dimension(struct ivi_layout_layer *ivilayer,
1979                                int32_t dest_width, int32_t dest_height)
1980 {
1981         struct ivi_layout_layer_properties *prop = NULL;
1982
1983         if (ivilayer == NULL) {
1984                 weston_log("ivi_layout_layer_set_dimension: invalid argument\n");
1985                 return IVI_FAILED;
1986         }
1987
1988         prop = &ivilayer->pending.prop;
1989
1990         prop->dest_width  = dest_width;
1991         prop->dest_height = dest_height;
1992
1993         ivilayer->event_mask |= IVI_NOTIFICATION_DIMENSION;
1994
1995         return IVI_SUCCEEDED;
1996 }
1997
1998 WL_EXPORT int32_t
1999 ivi_layout_layer_get_position(struct ivi_layout_layer *ivilayer,
2000                               int32_t *dest_x, int32_t *dest_y)
2001 {
2002         if (ivilayer == NULL || dest_x == NULL || dest_y == NULL) {
2003                 weston_log("ivi_layout_layer_get_position: invalid argument\n");
2004                 return IVI_FAILED;
2005         }
2006
2007         *dest_x = ivilayer->prop.dest_x;
2008         *dest_y = ivilayer->prop.dest_y;
2009
2010         return IVI_SUCCEEDED;
2011 }
2012
2013 WL_EXPORT int32_t
2014 ivi_layout_layer_set_position(struct ivi_layout_layer *ivilayer,
2015                               int32_t dest_x, int32_t dest_y)
2016 {
2017         struct ivi_layout_layer_properties *prop = NULL;
2018
2019         if (ivilayer == NULL) {
2020                 weston_log("ivi_layout_layer_set_position: invalid argument\n");
2021                 return IVI_FAILED;
2022         }
2023
2024         prop = &ivilayer->pending.prop;
2025         prop->dest_x = dest_x;
2026         prop->dest_y = dest_y;
2027
2028         ivilayer->event_mask |= IVI_NOTIFICATION_POSITION;
2029
2030         return IVI_SUCCEEDED;
2031 }
2032
2033 WL_EXPORT int32_t
2034 ivi_layout_layer_set_orientation(struct ivi_layout_layer *ivilayer,
2035                                  enum wl_output_transform orientation)
2036 {
2037         struct ivi_layout_layer_properties *prop = NULL;
2038
2039         if (ivilayer == NULL) {
2040                 weston_log("ivi_layout_layer_set_orientation: invalid argument\n");
2041                 return IVI_FAILED;
2042         }
2043
2044         prop = &ivilayer->pending.prop;
2045         prop->orientation = orientation;
2046
2047         ivilayer->event_mask |= IVI_NOTIFICATION_ORIENTATION;
2048
2049         return IVI_SUCCEEDED;
2050 }
2051
2052 enum wl_output_transform
2053 ivi_layout_layer_get_orientation(struct ivi_layout_layer *ivilayer)
2054 {
2055         if (ivilayer == NULL) {
2056                 weston_log("ivi_layout_layer_get_orientation: invalid argument\n");
2057                 return 0;
2058         }
2059
2060         return ivilayer->prop.orientation;
2061 }
2062
2063 WL_EXPORT int32_t
2064 ivi_layout_layer_set_render_order(struct ivi_layout_layer *ivilayer,
2065                                   struct ivi_layout_surface **pSurface,
2066                                   int32_t number)
2067 {
2068         struct ivi_layout *layout = get_layout_instance();
2069         struct ivi_layout_surface *ivisurf = NULL;
2070         struct ivi_layout_surface *next = NULL;
2071         uint32_t *id_surface = NULL;
2072         int32_t i = 0;
2073
2074         if (ivilayer == NULL) {
2075                 weston_log("ivi_layout_layer_set_render_order: invalid argument\n");
2076                 return IVI_FAILED;
2077         }
2078
2079         if (pSurface == NULL) {
2080                 wl_list_for_each_safe(ivisurf, next, &ivilayer->pending.surface_list, pending.link) {
2081                         if (!wl_list_empty(&ivisurf->pending.link)) {
2082                                 wl_list_remove(&ivisurf->pending.link);
2083                         }
2084
2085                         wl_list_init(&ivisurf->pending.link);
2086                 }
2087                 ivilayer->event_mask |= IVI_NOTIFICATION_REMOVE;
2088                 return IVI_SUCCEEDED;
2089         }
2090
2091         for (i = 0; i < number; i++) {
2092                 id_surface = &pSurface[i]->id_surface;
2093
2094                 wl_list_for_each_safe(ivisurf, next, &layout->surface_list, link) {
2095                         if (*id_surface != ivisurf->id_surface) {
2096                                 continue;
2097                         }
2098
2099                         if (!wl_list_empty(&ivisurf->pending.link)) {
2100                                 wl_list_remove(&ivisurf->pending.link);
2101                         }
2102                         wl_list_init(&ivisurf->pending.link);
2103                         wl_list_insert(&ivilayer->pending.surface_list,
2104                                        &ivisurf->pending.link);
2105                         break;
2106                 }
2107         }
2108
2109         ivilayer->event_mask |= IVI_NOTIFICATION_ADD;
2110
2111         return IVI_SUCCEEDED;
2112 }
2113
2114 WL_EXPORT int32_t
2115 ivi_layout_surface_set_visibility(struct ivi_layout_surface *ivisurf,
2116                                   bool newVisibility)
2117 {
2118         struct ivi_layout_surface_properties *prop = NULL;
2119
2120         if (ivisurf == NULL) {
2121                 weston_log("ivi_layout_surface_set_visibility: invalid argument\n");
2122                 return IVI_FAILED;
2123         }
2124
2125         prop = &ivisurf->pending.prop;
2126         prop->visibility = newVisibility;
2127
2128         ivisurf->event_mask |= IVI_NOTIFICATION_VISIBILITY;
2129
2130         return IVI_SUCCEEDED;
2131 }
2132
2133 WL_EXPORT bool
2134 ivi_layout_surface_get_visibility(struct ivi_layout_surface *ivisurf)
2135 {
2136         if (ivisurf == NULL) {
2137                 weston_log("ivi_layout_surface_get_visibility: invalid argument\n");
2138                 return false;
2139         }
2140
2141         return ivisurf->prop.visibility;
2142 }
2143
2144 WL_EXPORT int32_t
2145 ivi_layout_surface_set_opacity(struct ivi_layout_surface *ivisurf,
2146                                wl_fixed_t opacity)
2147 {
2148         struct ivi_layout_surface_properties *prop = NULL;
2149
2150         if (ivisurf == NULL) {
2151                 weston_log("ivi_layout_surface_set_opacity: invalid argument\n");
2152                 return IVI_FAILED;
2153         }
2154
2155         prop = &ivisurf->pending.prop;
2156         prop->opacity = opacity;
2157
2158         ivisurf->event_mask |= IVI_NOTIFICATION_OPACITY;
2159
2160         return IVI_SUCCEEDED;
2161 }
2162
2163 WL_EXPORT wl_fixed_t
2164 ivi_layout_surface_get_opacity(struct ivi_layout_surface *ivisurf)
2165 {
2166         if (ivisurf == NULL) {
2167                 weston_log("ivi_layout_surface_get_opacity: invalid argument\n");
2168                 return wl_fixed_from_double(0.0);
2169         }
2170
2171         return ivisurf->prop.opacity;
2172 }
2173
2174 WL_EXPORT int32_t
2175 ivi_layout_set_keyboard_focus_on(struct ivi_layout_surface *ivisurf)
2176 {
2177         struct ivi_layout *layout = get_layout_instance();
2178         struct wl_list *seat_list = &layout->compositor->seat_list;
2179         struct wl_list *surface_list = &layout->surface_list;
2180         struct ivi_layout_surface *current_surf;
2181
2182         if (ivisurf == NULL) {
2183                 weston_log("%s: invalid argument\n", __FUNCTION__);
2184                 return -1;
2185         }
2186
2187         if (seat_list == NULL) {
2188                 weston_log("%s: seat list is NULL\n", __FUNCTION__);
2189                 return -1;
2190         }
2191
2192         if (ivisurf->surface == NULL) {
2193                 weston_log("%s: ivisurf has no surface\n", __FUNCTION__);
2194                 return -1;
2195         }
2196
2197         if (surface_list == NULL) {
2198                 weston_log("%s: surface list is NULL\n", __FUNCTION__);
2199                 return -1;
2200         }
2201
2202         wl_list_for_each(current_surf, &layout->surface_list, link) {
2203                 if (current_surf == ivisurf) {
2204                         current_surf->prop.has_keyboard_focus = 1;
2205                         current_surf->pending.prop.has_keyboard_focus = 1;
2206                 } else {
2207                         current_surf->prop.has_keyboard_focus = 0;
2208                         current_surf->pending.prop.has_keyboard_focus = 0;
2209                 }
2210                 current_surf->event_mask |= IVI_NOTIFICATION_KEYBOARD_FOCUS;
2211         }
2212
2213         return 0;
2214 }
2215
2216 WL_EXPORT int32_t
2217 ivi_layout_get_keyboard_focus_surface_id(struct ivi_layout_surface **pSurfaceId)
2218 {
2219         struct wl_list *surface_list = &get_layout_instance()->surface_list;
2220         struct ivi_layout_surface *current_surf;
2221
2222         if (surface_list == NULL) {
2223                 weston_log("%s: surface list is NULL\n", __FUNCTION__);
2224                 return -1;
2225         }
2226
2227         wl_list_for_each(current_surf, surface_list, link) {
2228                 if (current_surf->prop.has_keyboard_focus != 0) {
2229                         *pSurfaceId = current_surf;
2230                         break;
2231                 }
2232         }
2233
2234         return 0;
2235 }
2236
2237
2238 WL_EXPORT int32_t
2239 ivi_layout_surface_set_destination_rectangle(struct ivi_layout_surface *ivisurf,
2240                                              int32_t x, int32_t y,
2241                                              int32_t width, int32_t height)
2242 {
2243         struct ivi_layout_surface_properties *prop = NULL;
2244
2245         if (ivisurf == NULL) {
2246                 weston_log("ivi_layout_surface_set_destination_rectangle: invalid argument\n");
2247                 return IVI_FAILED;
2248         }
2249
2250         prop = &ivisurf->pending.prop;
2251         prop->start_x = prop->dest_x;
2252         prop->start_y = prop->dest_y;
2253         prop->dest_x = x;
2254         prop->dest_y = y;
2255         prop->start_width = prop->dest_width;
2256         prop->start_height = prop->dest_height;
2257         prop->dest_width = width;
2258         prop->dest_height = height;
2259
2260         ivisurf->event_mask |= IVI_NOTIFICATION_DEST_RECT;
2261
2262         return IVI_SUCCEEDED;
2263 }
2264
2265 WL_EXPORT int32_t
2266 ivi_layout_surface_set_dimension(struct ivi_layout_surface *ivisurf,
2267                                  int32_t dest_width, int32_t dest_height)
2268 {
2269         struct ivi_layout_surface_properties *prop = NULL;
2270
2271         if (ivisurf == NULL) {
2272                 weston_log("ivi_layout_surface_set_dimension: invalid argument\n");
2273                 return IVI_FAILED;
2274         }
2275
2276         prop = &ivisurf->pending.prop;
2277         prop->dest_width  = dest_width;
2278         prop->dest_height = dest_height;
2279
2280         ivisurf->event_mask |= IVI_NOTIFICATION_DIMENSION;
2281
2282         return IVI_SUCCEEDED;
2283 }
2284
2285 int32_t
2286 ivi_layout_surface_get_dimension(struct ivi_layout_surface *ivisurf,
2287                                  int32_t *dest_width, int32_t *dest_height)
2288 {
2289         if (ivisurf == NULL || dest_width == NULL ||  dest_height == NULL) {
2290                 weston_log("ivi_layout_surface_get_dimension: invalid argument\n");
2291                 return IVI_FAILED;
2292         }
2293
2294         *dest_width = ivisurf->prop.dest_width;
2295         *dest_height = ivisurf->prop.dest_height;
2296
2297         return IVI_SUCCEEDED;
2298 }
2299
2300 WL_EXPORT int32_t
2301 ivi_layout_surface_set_position(struct ivi_layout_surface *ivisurf,
2302                                 int32_t dest_x, int32_t dest_y)
2303 {
2304         struct ivi_layout_surface_properties *prop = NULL;
2305
2306         if (ivisurf == NULL) {
2307                 weston_log("ivi_layout_surface_set_position: invalid argument\n");
2308                 return IVI_FAILED;
2309         }
2310
2311         prop = &ivisurf->pending.prop;
2312         prop->dest_x = dest_x;
2313         prop->dest_y = dest_y;
2314
2315         ivisurf->event_mask |= IVI_NOTIFICATION_POSITION;
2316
2317         return IVI_SUCCEEDED;
2318 }
2319
2320 int32_t
2321 ivi_layout_surface_get_position(struct ivi_layout_surface *ivisurf,
2322                                 int32_t *dest_x, int32_t *dest_y)
2323 {
2324         if (ivisurf == NULL || dest_x == NULL || dest_y == NULL) {
2325                 weston_log("ivi_layout_surface_get_position: invalid argument\n");
2326                 return IVI_FAILED;
2327         }
2328
2329         *dest_x = ivisurf->prop.dest_x;
2330         *dest_y = ivisurf->prop.dest_y;
2331
2332         return IVI_SUCCEEDED;
2333 }
2334
2335 WL_EXPORT int32_t
2336 ivi_layout_surface_set_orientation(struct ivi_layout_surface *ivisurf,
2337                                    enum wl_output_transform orientation)
2338 {
2339         struct ivi_layout_surface_properties *prop = NULL;
2340
2341         if (ivisurf == NULL) {
2342                 weston_log("ivi_layout_surface_set_orientation: invalid argument\n");
2343                 return IVI_FAILED;
2344         }
2345
2346         prop = &ivisurf->pending.prop;
2347         prop->orientation = orientation;
2348
2349         ivisurf->event_mask |= IVI_NOTIFICATION_ORIENTATION;
2350
2351         return IVI_SUCCEEDED;
2352 }
2353
2354 enum wl_output_transform
2355 ivi_layout_surface_get_orientation(struct ivi_layout_surface *ivisurf)
2356 {
2357         if (ivisurf == NULL) {
2358                 weston_log("ivi_layout_surface_get_orientation: invalid argument\n");
2359                 return 0;
2360         }
2361
2362         return ivisurf->prop.orientation;
2363 }
2364
2365 WL_EXPORT int32_t
2366 ivi_layout_screen_add_layer(struct ivi_layout_screen *iviscrn,
2367                             struct ivi_layout_layer *addlayer)
2368 {
2369         struct ivi_layout *layout = get_layout_instance();
2370         struct ivi_layout_layer *ivilayer = NULL;
2371         struct ivi_layout_layer *next = NULL;
2372         int is_layer_in_scrn = 0;
2373
2374         if (iviscrn == NULL || addlayer == NULL) {
2375                 weston_log("ivi_layout_screen_add_layer: invalid argument\n");
2376                 return IVI_FAILED;
2377         }
2378
2379         is_layer_in_scrn = is_layer_in_screen(addlayer, iviscrn);
2380         if (is_layer_in_scrn == 1) {
2381                 weston_log("ivi_layout_screen_add_layer: addlayer is already available\n");
2382                 return IVI_SUCCEEDED;
2383         }
2384
2385         wl_list_for_each_safe(ivilayer, next, &layout->layer_list, link) {
2386                 if (ivilayer->id_layer == addlayer->id_layer) {
2387                         if (!wl_list_empty(&ivilayer->pending.link)) {
2388                                 wl_list_remove(&ivilayer->pending.link);
2389                         }
2390                         wl_list_init(&ivilayer->pending.link);
2391                         wl_list_insert(&iviscrn->pending.layer_list,
2392                                        &ivilayer->pending.link);
2393                         break;
2394                 }
2395         }
2396
2397         iviscrn->event_mask |= IVI_NOTIFICATION_ADD;
2398
2399         return IVI_SUCCEEDED;
2400 }
2401
2402 WL_EXPORT int32_t
2403 ivi_layout_screen_set_render_order(struct ivi_layout_screen *iviscrn,
2404                                    struct ivi_layout_layer **pLayer,
2405                                    const int32_t number)
2406 {
2407         struct ivi_layout *layout = get_layout_instance();
2408         struct ivi_layout_layer *ivilayer = NULL;
2409         struct ivi_layout_layer *next = NULL;
2410         uint32_t *id_layer = NULL;
2411         int32_t i = 0;
2412
2413         if (iviscrn == NULL) {
2414                 weston_log("ivi_layout_screen_set_render_order: invalid argument\n");
2415                 return IVI_FAILED;
2416         }
2417
2418         wl_list_for_each_safe(ivilayer, next,
2419                               &iviscrn->pending.layer_list, pending.link) {
2420                 wl_list_init(&ivilayer->pending.link);
2421         }
2422
2423         wl_list_init(&iviscrn->pending.layer_list);
2424
2425         if (pLayer == NULL) {
2426                 wl_list_for_each_safe(ivilayer, next, &iviscrn->pending.layer_list, pending.link) {
2427                         if (!wl_list_empty(&ivilayer->pending.link)) {
2428                                 wl_list_remove(&ivilayer->pending.link);
2429                         }
2430
2431                         wl_list_init(&ivilayer->pending.link);
2432                 }
2433
2434                 iviscrn->event_mask |= IVI_NOTIFICATION_REMOVE;
2435                 return IVI_SUCCEEDED;
2436         }
2437
2438         for (i = 0; i < number; i++) {
2439                 id_layer = &pLayer[i]->id_layer;
2440                 wl_list_for_each(ivilayer, &layout->layer_list, link) {
2441                         if (*id_layer != ivilayer->id_layer) {
2442                                 continue;
2443                         }
2444
2445                         if (!wl_list_empty(&ivilayer->pending.link)) {
2446                                 wl_list_remove(&ivilayer->pending.link);
2447                         }
2448                         wl_list_init(&ivilayer->pending.link);
2449                         wl_list_insert(&iviscrn->pending.layer_list,
2450                                        &ivilayer->pending.link);
2451                         break;
2452                 }
2453         }
2454
2455         iviscrn->event_mask |= IVI_NOTIFICATION_ADD;
2456
2457         return IVI_SUCCEEDED;
2458 }
2459
2460 WL_EXPORT struct weston_output *
2461 ivi_layout_screen_get_output(struct ivi_layout_screen *iviscrn)
2462 {
2463         return iviscrn->output;
2464 }
2465
2466 /**
2467  * This function is used by the additional ivi-module because of dumping ivi_surface sceenshot.
2468  * The ivi-module, e.g. ivi-controller.so, is in wayland-ivi-extension of Genivi's Layer Management.
2469  * This function is used to get the result of drawing by clients.
2470  */
2471 WL_EXPORT struct weston_surface *
2472 ivi_layout_surface_get_weston_surface(struct ivi_layout_surface *ivisurf)
2473 {
2474         return ivisurf != NULL ? ivisurf->surface : NULL;
2475 }
2476
2477 /**
2478  * This function is used by the additional ivi-module because of dumping ivi_surface sceenshot.
2479  * The ivi-module, e.g. ivi-controller.so, is in wayland-ivi-extension of GENIVI's Layer Management.
2480  * This function is used to get the region and the stride.
2481  */
2482 WL_EXPORT int32_t
2483 ivi_layout_surface_get_size(struct ivi_layout_surface *ivisurf,
2484                             int32_t *width,
2485                             int32_t *height,
2486                             int32_t *stride)
2487 {
2488         if (ivisurf == NULL) {
2489                 return IVI_FAILED;
2490         }
2491
2492         if (width != NULL) {
2493                 *width = ivisurf->prop.source_width;
2494         }
2495
2496         if (height != NULL) {
2497                 *height = ivisurf->prop.source_height;
2498         }
2499
2500         if (stride != NULL &&
2501                 ivisurf->surface->buffer_ref.buffer != NULL &&
2502                 ivisurf->surface->buffer_ref.buffer->shm_buffer != NULL) {
2503                 *stride = wl_shm_buffer_get_stride(ivisurf->surface->buffer_ref.buffer->shm_buffer);
2504         }
2505
2506         return IVI_SUCCEEDED;
2507 }
2508
2509 WL_EXPORT int32_t
2510 ivi_layout_layer_add_notification(struct ivi_layout_layer *ivilayer,
2511                                   layer_property_notification_func callback,
2512                                   void *userdata)
2513 {
2514         struct ivi_layout_notification_callback *prop_callback = NULL;
2515
2516         if (ivilayer == NULL || callback == NULL) {
2517                 weston_log("ivi_layout_layer_add_notification: invalid argument\n");
2518                 return IVI_FAILED;
2519         }
2520
2521         prop_callback = malloc(sizeof *prop_callback);
2522         if (prop_callback == NULL) {
2523                 weston_log("fails to allocate memory\n");
2524                 return IVI_FAILED;
2525         }
2526
2527         prop_callback->callback = callback;
2528         prop_callback->data = userdata;
2529
2530         return add_notification(&ivilayer->property_changed,
2531                                 layer_prop_changed,
2532                                 prop_callback);
2533 }
2534
2535 WL_EXPORT void
2536 ivi_layout_layer_remove_notification(struct ivi_layout_layer *ivilayer)
2537 {
2538         if (ivilayer == NULL) {
2539                 weston_log("ivi_layout_layer_remove_notification: invalid argument\n");
2540                 return;
2541         }
2542
2543         remove_all_notification(&ivilayer->property_changed.listener_list);
2544 }
2545
2546 WL_EXPORT const struct ivi_layout_surface_properties *
2547 ivi_layout_get_properties_of_surface(struct ivi_layout_surface *ivisurf)
2548 {
2549         if (ivisurf == NULL) {
2550                 weston_log("ivi_layout_get_properties_of_surface: invalid argument\n");
2551                 return NULL;
2552         }
2553
2554         return &ivisurf->prop;
2555 }
2556
2557 WL_EXPORT int32_t
2558 ivi_layout_layer_add_surface(struct ivi_layout_layer *ivilayer,
2559                              struct ivi_layout_surface *addsurf)
2560 {
2561         struct ivi_layout *layout = get_layout_instance();
2562         struct ivi_layout_surface *ivisurf = NULL;
2563         struct ivi_layout_surface *next = NULL;
2564         int is_surf_in_layer = 0;
2565
2566         if (ivilayer == NULL || addsurf == NULL) {
2567                 weston_log("ivi_layout_layer_add_surface: invalid argument\n");
2568                 return IVI_FAILED;
2569         }
2570
2571         is_surf_in_layer = is_surface_in_layer(addsurf, ivilayer);
2572         if (is_surf_in_layer == 1) {
2573                 weston_log("ivi_layout_layer_add_surface: addsurf is already available\n");
2574                 return IVI_SUCCEEDED;
2575         }
2576
2577         wl_list_for_each_safe(ivisurf, next, &layout->surface_list, link) {
2578                 if (ivisurf->id_surface == addsurf->id_surface) {
2579                         if (!wl_list_empty(&ivisurf->pending.link)) {
2580                                 wl_list_remove(&ivisurf->pending.link);
2581                         }
2582                         wl_list_init(&ivisurf->pending.link);
2583                         wl_list_insert(&ivilayer->pending.surface_list,
2584                                        &ivisurf->pending.link);
2585                         break;
2586                 }
2587         }
2588
2589         ivilayer->event_mask |= IVI_NOTIFICATION_ADD;
2590
2591         return IVI_SUCCEEDED;
2592 }
2593
2594 WL_EXPORT void
2595 ivi_layout_layer_remove_surface(struct ivi_layout_layer *ivilayer,
2596                                 struct ivi_layout_surface *remsurf)
2597 {
2598         struct ivi_layout_surface *ivisurf = NULL;
2599         struct ivi_layout_surface *next = NULL;
2600
2601         if (ivilayer == NULL || remsurf == NULL) {
2602                 weston_log("ivi_layout_layer_remove_surface: invalid argument\n");
2603                 return;
2604         }
2605
2606         wl_list_for_each_safe(ivisurf, next,
2607                               &ivilayer->pending.surface_list, pending.link) {
2608                 if (ivisurf->id_surface == remsurf->id_surface) {
2609                         if (!wl_list_empty(&ivisurf->pending.link)) {
2610                                 wl_list_remove(&ivisurf->pending.link);
2611                         }
2612                         wl_list_init(&ivisurf->pending.link);
2613                         break;
2614                 }
2615         }
2616
2617         remsurf->event_mask |= IVI_NOTIFICATION_REMOVE;
2618 }
2619
2620 WL_EXPORT int32_t
2621 ivi_layout_surface_set_source_rectangle(struct ivi_layout_surface *ivisurf,
2622                                         int32_t x, int32_t y,
2623                                         int32_t width, int32_t height)
2624 {
2625         struct ivi_layout_surface_properties *prop = NULL;
2626
2627         if (ivisurf == NULL) {
2628                 weston_log("ivi_layout_surface_set_source_rectangle: invalid argument\n");
2629                 return IVI_FAILED;
2630         }
2631
2632         prop = &ivisurf->pending.prop;
2633         prop->source_x = x;
2634         prop->source_y = y;
2635         prop->source_width = width;
2636         prop->source_height = height;
2637
2638         ivisurf->event_mask |= IVI_NOTIFICATION_SOURCE_RECT;
2639
2640         return IVI_SUCCEEDED;
2641 }
2642
2643 WL_EXPORT int32_t
2644 ivi_layout_commit_changes(void)
2645 {
2646         struct ivi_layout *layout = get_layout_instance();
2647
2648         commit_surface_list(layout);
2649         commit_layer_list(layout);
2650         commit_screen_list(layout);
2651
2652         commit_transition(layout);
2653
2654         commit_changes(layout);
2655         send_prop(layout);
2656         weston_compositor_schedule_repaint(layout->compositor);
2657
2658         return IVI_SUCCEEDED;
2659 }
2660
2661 /***called from ivi-shell**/
2662 static struct weston_surface *
2663 ivi_layout_get_weston_surface(struct ivi_layout_surface *surface)
2664 {
2665         return (surface != NULL) ? surface->surface : NULL;
2666 }
2667
2668 static struct weston_view *
2669 ivi_layout_get_weston_view(struct ivi_layout_surface *surface)
2670 {
2671     return (surface != NULL) ? surface->view : NULL;
2672 }
2673
2674 static void
2675 ivi_layout_surface_configure(struct ivi_layout_surface *ivisurf,
2676                              int32_t width, int32_t height)
2677 {
2678         struct ivi_layout *layout = get_layout_instance();
2679         int needs_commit;
2680
2681         ivisurf->surface->width_from_buffer  = width;
2682         ivisurf->surface->height_from_buffer = height;
2683
2684         if (ivisurf->prop.source_width == 0 || ivisurf->prop.source_height == 0) {
2685                 ivisurf->prop.source_width = width;
2686                 ivisurf->prop.source_height = height;
2687         }
2688
2689         if (width != ivisurf->pending.prop.source_width ||
2690             height != ivisurf->pending.prop.source_height) {
2691                 /*
2692                  * This is quick fix for resizing buffers and will reset zooming
2693                  * TODO: replace this with some more complex code that tries to                 *       preserve the zooming ratio and relative position
2694                  */
2695                 ivisurf->pending.prop.source_x = 0;
2696                 ivisurf->pending.prop.source_y = 0;
2697                 ivisurf->pending.prop.source_width = width;
2698                 ivisurf->pending.prop.source_height = height;
2699
2700                 needs_commit = (ivisurf->event_mask == 0) ? 1 : 0;
2701
2702                 ivisurf->event_mask |= IVI_NOTIFICATION_SOURCE_RECT;
2703
2704                 weston_log("resizing source rectangle of surface %u to %dx%d (%s)\n",
2705                            ivisurf->id_surface, width,height,
2706                            needs_commit ? "commiting it" : "no commit");
2707
2708                 if (needs_commit)
2709                         ivi_layout_commit_changes();
2710         }
2711
2712         wl_signal_emit(&layout->surface_notification.configure_changed, ivisurf);
2713 }
2714
2715
2716 WL_EXPORT int32_t
2717 ivi_layout_surface_set_content_observer(struct ivi_layout_surface *ivisurf,
2718                                         ivi_controller_surface_content_callback callback,
2719                                         void* userdata)
2720 {
2721         int32_t ret = IVI_FAILED;
2722
2723         if (ivisurf != NULL) {
2724                 ivisurf->content_observer.callback = callback;
2725                 ivisurf->content_observer.userdata = userdata;
2726                 ret = IVI_SUCCEEDED;
2727         }
2728         return ret;
2729 }
2730
2731 static struct ivi_layout_surface*
2732 ivi_layout_surface_create(struct weston_surface *wl_surface,
2733                           uint32_t id_surface)
2734 {
2735         struct ivi_layout *layout = get_layout_instance();
2736         struct ivi_layout_surface *ivisurf = NULL;
2737
2738         if (wl_surface == NULL) {
2739                 weston_log("ivi_layout_surface_create: invalid argument\n");
2740                 return NULL;
2741         }
2742
2743         ivisurf = get_surface(&layout->surface_list, id_surface);
2744         if (ivisurf != NULL) {
2745                 if (ivisurf->surface != NULL) {
2746                         weston_log("id_surface(%d) is already created\n", id_surface);
2747                         return NULL;
2748                 }
2749         }
2750
2751         ivisurf = calloc(1, sizeof *ivisurf);
2752         if (ivisurf == NULL) {
2753                 weston_log("fails to allocate memory\n");
2754                 return NULL;
2755         }
2756
2757         wl_list_init(&ivisurf->link);
2758         wl_signal_init(&ivisurf->property_changed);
2759         wl_signal_init(&ivisurf->configured);
2760         wl_list_init(&ivisurf->layer_list);
2761         ivisurf->id_surface = id_surface;
2762         ivisurf->layout = layout;
2763
2764         ivisurf->surface = wl_surface;
2765         ivisurf->surface_destroy_listener.notify =
2766                 westonsurface_destroy_from_ivisurface;
2767         wl_resource_add_destroy_listener(wl_surface->resource,
2768                                          &ivisurf->surface_destroy_listener);
2769
2770         ivisurf->view = weston_view_create(wl_surface);
2771         if (ivisurf->view == NULL) {
2772                 weston_log("fails to allocate memory\n");
2773         }
2774
2775         ivisurf->surface->width_from_buffer  = 0;
2776         ivisurf->surface->height_from_buffer = 0;
2777
2778         weston_matrix_init(&ivisurf->surface_rotation.matrix);
2779         weston_matrix_init(&ivisurf->layer_rotation.matrix);
2780         weston_matrix_init(&ivisurf->surface_pos.matrix);
2781         weston_matrix_init(&ivisurf->layer_pos.matrix);
2782         weston_matrix_init(&ivisurf->scaling.matrix);
2783
2784         wl_list_init(&ivisurf->surface_rotation.link);
2785         wl_list_init(&ivisurf->layer_rotation.link);
2786         wl_list_init(&ivisurf->surface_pos.link);
2787         wl_list_init(&ivisurf->layer_pos.link);
2788         wl_list_init(&ivisurf->scaling.link);
2789
2790         init_surface_properties(&ivisurf->prop);
2791         ivisurf->event_mask = 0;
2792
2793         ivisurf->pending.prop = ivisurf->prop;
2794         wl_list_init(&ivisurf->pending.link);
2795
2796         wl_list_init(&ivisurf->order.link);
2797         wl_list_init(&ivisurf->order.layer_list);
2798
2799         wl_list_insert(&layout->surface_list, &ivisurf->link);
2800
2801         wl_signal_emit(&layout->surface_notification.created, ivisurf);
2802
2803         return ivisurf;
2804 }
2805
2806 static struct ivi_layout_surface*
2807 ivi_layout_surface_find(struct weston_surface *wl_surface)
2808 {
2809     struct ivi_layout *layout = get_layout_instance();
2810     struct ivi_layout_surface *ivisurf;
2811
2812     if (wl_surface != NULL) {
2813         wl_list_for_each(ivisurf, &layout->surface_list, link) {
2814             if (wl_surface == ivisurf->surface)
2815                 return ivisurf;
2816         }
2817     }
2818
2819     return NULL;
2820 }
2821
2822 static void
2823 background_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2824 {
2825 }
2826
2827 static void
2828 background_create(struct ivi_layout *layout, struct weston_output *output)
2829 {
2830         struct ivi_background *bg;
2831         struct weston_surface *surface;
2832         struct weston_view *view;
2833         int32_t x1, y1, x2, y2;
2834
2835         weston_log("creating background %d,%d %dx%d\n",
2836                    output->x,output->y, output->width,output->height);
2837
2838         if (output->width <= 0 || output->height <= 0)
2839                 return;
2840
2841         x1 = output->x;
2842         y1 = output->y;
2843         x2 = x1 + output->width;
2844         y2 = y1 + output->height;
2845
2846
2847         bg = calloc(1, sizeof(*bg));
2848         if (bg == NULL) {
2849                 return;
2850         }
2851         else {
2852                 wl_list_init(&bg->link);
2853         }
2854
2855         surface = weston_surface_create(layout->compositor);
2856         if (surface == NULL) {
2857                 free(bg);
2858                 return;
2859         }
2860
2861         view = weston_view_create(surface);
2862         if (view == NULL) {
2863                 weston_surface_destroy(surface);
2864                 free(bg);
2865                 return;
2866         }
2867
2868         surface->configure = background_surface_configure;
2869         surface->configure_private = layout;
2870
2871         weston_surface_set_color(surface,
2872                                  layout->background_color.red,
2873                                  layout->background_color.green,
2874                                  layout->background_color.blue,
2875                                  1.0);
2876
2877         pixman_region32_fini(&surface->opaque);
2878         pixman_region32_init_rect(&surface->opaque, x1,y1, x2,y2);
2879
2880         pixman_region32_fini(&surface->input);
2881         pixman_region32_init_rect(&surface->input, x1,y1, x2,y2);
2882
2883         weston_surface_set_size(surface, output->width, output->height);
2884
2885         wl_list_init(&bg->link);
2886         bg->surface = surface;
2887         bg->view = view;
2888
2889         wl_list_insert(&layout->background_list, &bg->link);
2890
2891         weston_layer_entry_insert(&layout->background_layer.view_list,
2892                                   &view->layer_link);
2893
2894         weston_view_set_position(view, x1,y1);
2895         // weston_view_geometry_dirty(view);
2896         weston_view_update_transform(view);
2897         weston_surface_damage(surface);
2898 }
2899
2900
2901 static int parse_color(char **str_ptr, int separator, float *value_ptr)
2902 {
2903     char *value_str = *str_ptr;
2904     char *p, *e;
2905     float value;
2906
2907     while (*value_str == ' ' || *value_str == '\t')
2908         value_str++;
2909
2910     if (!separator)
2911         p = value_str + strlen(value_str);
2912     else {
2913         if (!(p = strchr(value_str, separator)))
2914             return 0;
2915         *p++ = '\0';
2916     }
2917
2918     value = strtod(value_str, &e);
2919
2920     if (e <= value_str || *e || value < 0.0 || value > 1.0)
2921         return 0;
2922
2923     *value_ptr = value;
2924     *str_ptr = p;
2925
2926     return 1;
2927 }
2928
2929 static void parse_background_color(struct ivi_layout *layout)
2930 {
2931     struct weston_config_section *section;
2932     char *def, *p;
2933     float red, green, blue;
2934
2935     layout->background_color.red = 0.0;
2936     layout->background_color.green = 0.0;
2937     layout->background_color.blue = 0.0;
2938
2939     section = weston_config_get_section(layout->compositor->config,
2940                                         "ivi-shell", NULL, NULL);
2941     if (section == NULL)
2942         return;
2943
2944     weston_config_section_get_string(section, "background-color", &def, NULL);
2945
2946     if (def == NULL)
2947         return;
2948
2949     p = def;
2950
2951     if (!parse_color(&p, ',', &red)   ||
2952         !parse_color(&p, ',', &green) ||
2953         !parse_color(&p, 0,   &blue)   )
2954         weston_log("invalid background color definition: '%s'\n", def);
2955     else {
2956         layout->background_color.red = red;
2957         layout->background_color.green = green;
2958         layout->background_color.blue = blue;
2959     }
2960
2961     free(def);
2962 }
2963
2964 static void
2965 keyboard_grab_key(struct weston_keyboard_grab *grab, uint32_t time,
2966                   uint32_t key, uint32_t state)
2967 {
2968         struct ivi_layout *layout = get_layout_instance();
2969         struct weston_keyboard *keyboard = grab->keyboard;
2970         struct wl_display *display = keyboard->seat->compositor->wl_display;
2971         struct ivi_layout_surface *surf;
2972         struct wl_resource *resource;
2973         uint32_t serial;
2974
2975         wl_list_for_each(surf, &layout->surface_list, link) {
2976                 if (surf->prop.has_keyboard_focus) {
2977                         resource = wl_resource_find_for_client(
2978                                         &keyboard->resource_list,
2979                                         wl_resource_get_client(surf->surface->resource));
2980                         if (!resource)
2981                                 resource = wl_resource_find_for_client(
2982                                                 &keyboard->focus_resource_list,
2983                                                 wl_resource_get_client(surf->surface->resource));
2984
2985                         if (resource) {
2986                                 serial = wl_display_next_serial(display);
2987                                 wl_keyboard_send_key(resource, serial, time, key, state);
2988                         } else {
2989                                 weston_log("%s: No resource found for surface %d\n",
2990                                            __FUNCTION__, surf->id_surface);
2991                         }
2992                 }
2993         }
2994 }
2995
2996 static void
2997 keyboard_grab_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
2998                         uint32_t mods_depressed, uint32_t mods_latched,
2999                         uint32_t mods_locked, uint32_t group)
3000 {
3001         struct ivi_layout *layout = get_layout_instance();
3002         struct weston_keyboard *keyboard = grab->keyboard;
3003         struct ivi_layout_surface *surf;
3004         struct wl_resource *resource;
3005         int sent_to_pointer_client = 0;
3006         struct weston_pointer *pointer = keyboard->seat->pointer;
3007
3008         /* Send modifiers to focussed surface */
3009         wl_list_for_each(surf, &layout->surface_list, link) {
3010                 if (surf->prop.has_keyboard_focus) {
3011                         resource = wl_resource_find_for_client(
3012                                         &keyboard->resource_list,
3013                                         wl_resource_get_client(surf->surface->resource));
3014                         if (!resource)
3015                                 resource = wl_resource_find_for_client(
3016                                                 &keyboard->focus_resource_list,
3017                                                 wl_resource_get_client(surf->surface->resource));
3018
3019                         if (resource) {
3020                                 wl_keyboard_send_modifiers(resource, serial,
3021                                                            mods_depressed,
3022                                                            mods_latched, mods_locked,
3023                                                            group);
3024                                 if (pointer && pointer->focus
3025                                     && pointer->focus->surface->resource
3026                                     && pointer->focus->surface == surf->surface)
3027                                         sent_to_pointer_client = 1;
3028                         } else {
3029                                 weston_log("%s: No resource found for surface %d\n",
3030                                            __FUNCTION__, surf->id_surface);
3031                         }
3032                 }
3033         }
3034
3035         /* Send modifiers to pointer's client, if not already sent */
3036         if (!sent_to_pointer_client && pointer && pointer->focus
3037             && pointer->focus->surface->resource) {
3038                 struct wl_client *pointer_client =
3039                         wl_resource_get_client(pointer->focus->surface->resource);
3040                 wl_resource_for_each(resource, &keyboard->resource_list) {
3041                         if (wl_resource_get_client(resource) == pointer_client) {
3042                                 sent_to_pointer_client = 1;
3043                                 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
3044                                                            mods_latched, mods_locked, group);
3045                                 break;
3046                         }
3047                 }
3048
3049                 if (!sent_to_pointer_client) {
3050                         wl_resource_for_each(resource, &keyboard->focus_resource_list) {
3051                                 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
3052                                                            mods_latched, mods_locked, group);
3053                                 break;
3054                         }
3055                 }
3056         }
3057 }
3058
3059 static void
3060 keyboard_grab_cancel(struct weston_keyboard_grab *grab)
3061 {
3062 }
3063
3064 static struct weston_keyboard_grab_interface keyboard_grab_interface = {
3065         keyboard_grab_key,
3066         keyboard_grab_modifiers,
3067         keyboard_grab_cancel
3068 };
3069
3070 static void
3071 handle_seat_updated_caps(struct wl_listener *listener, void *data)
3072 {
3073         struct weston_seat *seat = data;
3074         struct seat_ctx *ctx = wl_container_of(listener, ctx,
3075                                                updated_caps_listener);
3076         if (seat->keyboard && seat->keyboard != ctx->grab.keyboard)
3077         weston_keyboard_start_grab(seat->keyboard, &ctx->grab);
3078 }
3079
3080 static void
3081 handle_seat_destroy(struct wl_listener *listener, void *data)
3082 {
3083         struct seat_ctx *ctx = wl_container_of(listener, ctx, destroy_listener);
3084         if (ctx->grab.keyboard)
3085                 keyboard_grab_cancel(&ctx->grab);
3086
3087         free(ctx);
3088 }
3089
3090 static void
3091 handle_seat_create(struct wl_listener *listener, void *data)
3092 {
3093         struct weston_seat *seat = data;
3094
3095         struct seat_ctx *ctx = calloc(1, sizeof *ctx);
3096         if (ctx == NULL) {
3097                 weston_log("%s: failed to allocate memory\n", __FUNCTION__);
3098                 return;
3099         }
3100
3101         ctx->grab.interface = &keyboard_grab_interface;
3102
3103         ctx->destroy_listener.notify = &handle_seat_destroy;
3104         wl_signal_add(&seat->destroy_signal, &ctx->destroy_listener);
3105
3106         ctx->updated_caps_listener.notify = &handle_seat_updated_caps;
3107         wl_signal_add(&seat->updated_caps_signal, &ctx->updated_caps_listener);
3108 }
3109
3110 static void
3111 ivi_layout_init_with_compositor(struct weston_compositor *ec)
3112 {
3113         struct ivi_layout *layout = get_layout_instance();
3114         struct weston_output *output;
3115         struct weston_seat *seat;
3116
3117         layout->compositor = ec;
3118
3119         wl_list_init(&layout->surface_list);
3120         wl_list_init(&layout->layer_list);
3121         wl_list_init(&layout->screen_list);
3122
3123         wl_signal_init(&layout->layer_notification.created);
3124         wl_signal_init(&layout->layer_notification.removed);
3125
3126         wl_signal_init(&layout->surface_notification.created);
3127         wl_signal_init(&layout->surface_notification.removed);
3128         wl_signal_init(&layout->surface_notification.configure_changed);
3129
3130         /* Add layout_layer at the last of weston_compositor.layer_list */
3131         weston_layer_init(&layout->layout_layer, ec->layer_list.prev);
3132         weston_layer_init(&layout->background_layer, ec->layer_list.prev);
3133
3134         create_screen(ec);
3135
3136         layout->transitions = ivi_layout_transition_set_create(ec);
3137         wl_list_init(&layout->pending_transition_list);
3138
3139         /* Listen to seat creation, for grab purposes */
3140         layout->seat_create_listener.notify = &handle_seat_create;
3141         wl_signal_add(&ec->seat_created_signal, &layout->seat_create_listener);
3142
3143         /* Handle existing seats */
3144         wl_list_for_each(seat, &ec->seat_list, link) {
3145                 handle_seat_create(NULL, seat);
3146                 wl_signal_emit(&seat->updated_caps_signal, seat);
3147         }
3148
3149         wl_list_init(&layout->background_list);
3150         parse_background_color(layout);
3151         wl_list_for_each(output, &ec->output_list, link)
3152                 background_create(layout, output);
3153 }
3154
3155
3156 static void
3157 ivi_layout_surface_add_configured_listener(struct ivi_layout_surface* ivisurf,
3158                                            struct wl_listener* listener)
3159 {
3160         wl_signal_add(&ivisurf->configured, listener);
3161 }
3162
3163
3164 WL_EXPORT struct ivi_layout_interface ivi_layout_interface = {
3165         .get_weston_surface = ivi_layout_get_weston_surface,
3166         .get_weston_view = ivi_layout_get_weston_view,
3167         .surface_configure = ivi_layout_surface_configure,
3168         .surface_create = ivi_layout_surface_create,
3169         .surface_find = ivi_layout_surface_find,
3170         .surface_add_notification = ivi_layout_surface_add_notification,
3171         .surface_remove_notification = ivi_layout_surface_remove_notification,
3172         .init_with_compositor = ivi_layout_init_with_compositor,
3173         .get_surface_dimension = ivi_layout_surface_get_dimension,
3174         .add_surface_configured_listener = ivi_layout_surface_add_configured_listener
3175 };