downstream: ivi-shell: fix the layout-layer clearing in commit_list_screen()
[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 ivi_layout_notification_callback {
108         void *callback;
109         void *data;
110 };
111
112 static struct ivi_layout ivilayout = {0};
113
114 struct ivi_layout *
115 get_layout_instance(void)
116 {
117         return &ivilayout;
118 }
119
120 /**
121  * Internal API to add/remove a link to ivi_surface from ivi_layer.
122  */
123 static void
124 add_link_to_surface(struct ivi_layout_layer *ivilayer,
125                     struct link_layer *link_layer)
126 {
127         struct link_layer *link = NULL;
128
129         wl_list_for_each(link, &ivilayer->link_to_surface, link_to_layer) {
130                 if (link == link_layer)
131                         return;
132         }
133
134         wl_list_insert(&ivilayer->link_to_surface, &link_layer->link_to_layer);
135 }
136
137 static void
138 remove_link_to_surface(struct ivi_layout_layer *ivilayer)
139 {
140         struct link_layer *link = NULL;
141         struct link_layer *next = NULL;
142
143         wl_list_for_each_safe(link, next, &ivilayer->link_to_surface, link_to_layer) {
144                 if (!wl_list_empty(&link->link_to_layer)) {
145                         wl_list_remove(&link->link_to_layer);
146                 }
147                 if (!wl_list_empty(&link->link)) {
148                         wl_list_remove(&link->link);
149                 }
150                 free(link);
151         }
152
153         wl_list_init(&ivilayer->link_to_surface);
154 }
155
156 /**
157  * Internal API to add a link to ivi_layer from ivi_screen.
158  */
159 static void
160 add_link_to_layer(struct ivi_layout_screen *iviscrn,
161                   struct link_screen *link_screen)
162 {
163         wl_list_init(&link_screen->link_to_screen);
164         wl_list_insert(&iviscrn->link_to_layer, &link_screen->link_to_screen);
165 }
166
167 /**
168  * Internal API to add/remove a ivi_surface from ivi_layer.
169  */
170 static void
171 add_ordersurface_to_layer(struct ivi_layout_surface *ivisurf,
172                           struct ivi_layout_layer *ivilayer)
173 {
174         struct link_layer *link_layer = NULL;
175
176         link_layer = malloc(sizeof *link_layer);
177         if (link_layer == NULL) {
178                 weston_log("fails to allocate memory\n");
179                 return;
180         }
181
182         link_layer->ivilayer = ivilayer;
183         wl_list_init(&link_layer->link);
184         wl_list_insert(&ivisurf->layer_list, &link_layer->link);
185         add_link_to_surface(ivilayer, link_layer);
186 }
187
188 static void
189 remove_ordersurface_from_layer(struct ivi_layout_surface *ivisurf)
190 {
191         struct link_layer *link_layer = NULL;
192         struct link_layer *next = NULL;
193
194         wl_list_for_each_safe(link_layer, next, &ivisurf->layer_list, link) {
195                 if (!wl_list_empty(&link_layer->link)) {
196                         wl_list_remove(&link_layer->link);
197                 }
198                 if (!wl_list_empty(&link_layer->link_to_layer)) {
199                         wl_list_remove(&link_layer->link_to_layer);
200                 }
201                 free(link_layer);
202         }
203         wl_list_init(&ivisurf->layer_list);
204 }
205
206 /**
207  * Internal API to add/remove a ivi_layer to/from ivi_screen.
208  */
209 static void
210 add_orderlayer_to_screen(struct ivi_layout_layer *ivilayer,
211                          struct ivi_layout_screen *iviscrn)
212 {
213         struct link_screen *link_scrn = NULL;
214
215         link_scrn = malloc(sizeof *link_scrn);
216         if (link_scrn == NULL) {
217                 weston_log("fails to allocate memory\n");
218                 return;
219         }
220
221         link_scrn->iviscrn = iviscrn;
222         wl_list_init(&link_scrn->link);
223         wl_list_insert(&ivilayer->screen_list, &link_scrn->link);
224         add_link_to_layer(iviscrn, link_scrn);
225 }
226
227 static void
228 remove_orderlayer_from_screen(struct ivi_layout_layer *ivilayer)
229 {
230         struct link_screen *link_scrn = NULL;
231         struct link_screen *next = NULL;
232
233         wl_list_for_each_safe(link_scrn, next, &ivilayer->screen_list, link) {
234                 if (!wl_list_empty(&link_scrn->link)) {
235                         wl_list_remove(&link_scrn->link);
236                 }
237                 if (!wl_list_empty(&link_scrn->link_to_screen)) {
238                         wl_list_remove(&link_scrn->link_to_screen);
239                 }
240                 free(link_scrn);
241         }
242         wl_list_init(&ivilayer->screen_list);
243 }
244
245 /**
246  * Internal API to add/remove a ivi_layer to/from ivi_screen.
247  */
248 static struct ivi_layout_surface *
249 get_surface(struct wl_list *surf_list, uint32_t id_surface)
250 {
251         struct ivi_layout_surface *ivisurf;
252
253         wl_list_for_each(ivisurf, surf_list, link) {
254                 if (ivisurf->id_surface == id_surface) {
255                         return ivisurf;
256                 }
257         }
258
259         return NULL;
260 }
261
262 static struct ivi_layout_layer *
263 get_layer(struct wl_list *layer_list, uint32_t id_layer)
264 {
265         struct ivi_layout_layer *ivilayer;
266
267         wl_list_for_each(ivilayer, layer_list, link) {
268                 if (ivilayer->id_layer == id_layer) {
269                         return ivilayer;
270                 }
271         }
272
273         return NULL;
274 }
275
276 /**
277  * Called at destruction of ivi_surface
278  */
279 static void
280 westonsurface_destroy_from_ivisurface(struct wl_listener *listener, void *data)
281 {
282         struct ivi_layout_surface *ivisurf = NULL;
283
284         ivisurf = container_of(listener, struct ivi_layout_surface,
285                                surface_destroy_listener);
286
287         wl_list_remove(&ivisurf->surface_rotation.link);
288         wl_list_remove(&ivisurf->layer_rotation.link);
289         wl_list_remove(&ivisurf->surface_pos.link);
290         wl_list_remove(&ivisurf->layer_pos.link);
291         wl_list_remove(&ivisurf->scaling.link);
292
293         ivisurf->surface = NULL;
294         ivi_layout_surface_remove(ivisurf);
295 }
296
297 /**
298  * Internal API to check ivi_layer/ivi_surface already added in ivi_layer/ivi_screen.
299  * Called by ivi_layout_layer_add_surface/ivi_layout_screenAddLayer
300  */
301 static int
302 is_surface_in_layer(struct ivi_layout_surface *ivisurf,
303                     struct ivi_layout_layer *ivilayer)
304 {
305         struct ivi_layout_surface *surf = NULL;
306
307         wl_list_for_each(surf, &ivilayer->pending.surface_list, pending.link) {
308                 if (surf->id_surface == ivisurf->id_surface) {
309                         return 1;
310                 }
311         }
312
313         return 0;
314 }
315
316 static int
317 is_layer_in_screen(struct ivi_layout_layer *ivilayer,
318                    struct ivi_layout_screen *iviscrn)
319 {
320         struct ivi_layout_layer *layer = NULL;
321
322         wl_list_for_each(layer, &iviscrn->pending.layer_list, pending.link) {
323                 if (layer->id_layer == ivilayer->id_layer) {
324                         return 1;
325                 }
326         }
327
328         return 0;
329 }
330
331 /**
332  * Internal API to initialize ivi_screens found from output_list of weston_compositor.
333  * Called by ivi_layout_init_with_compositor.
334  */
335 static void
336 create_screen(struct weston_compositor *ec)
337 {
338         struct ivi_layout *layout = get_layout_instance();
339         struct ivi_layout_screen *iviscrn = NULL;
340         struct weston_output *output = NULL;
341         int32_t count = 0;
342
343         wl_list_for_each(output, &ec->output_list, link) {
344                 iviscrn = calloc(1, sizeof *iviscrn);
345                 if (iviscrn == NULL) {
346                         weston_log("fails to allocate memory\n");
347                         continue;
348                 }
349
350                 wl_list_init(&iviscrn->link);
351                 iviscrn->layout = layout;
352
353                 iviscrn->id_screen = count;
354                 count++;
355
356                 iviscrn->output = output;
357                 iviscrn->event_mask = 0;
358
359                 wl_list_init(&iviscrn->pending.layer_list);
360                 wl_list_init(&iviscrn->pending.link);
361
362                 wl_list_init(&iviscrn->order.layer_list);
363                 wl_list_init(&iviscrn->order.link);
364
365                 wl_list_init(&iviscrn->link_to_layer);
366
367                 wl_list_insert(&layout->screen_list, &iviscrn->link);
368         }
369 }
370
371 /**
372  * Internal APIs to initialize properties of ivi_surface/ivi_layer when they are created.
373  */
374 static void
375 init_layer_properties(struct ivi_layout_layer_properties *prop,
376                       int32_t width, int32_t height)
377 {
378         memset(prop, 0, sizeof *prop);
379         prop->opacity = wl_fixed_from_double(1.0);
380         prop->source_width = width;
381         prop->source_height = height;
382         prop->dest_width = width;
383         prop->dest_height = height;
384 }
385
386 static void
387 init_surface_properties(struct ivi_layout_surface_properties *prop)
388 {
389         memset(prop, 0, sizeof *prop);
390         prop->opacity = wl_fixed_from_double(1.0);
391 }
392
393 /**
394  * Internal APIs to be called from ivi_layout_commit_changes.
395  */
396 static void
397 update_opacity(struct ivi_layout_layer *ivilayer,
398                struct ivi_layout_surface *ivisurf)
399 {
400         double layer_alpha = wl_fixed_to_double(ivilayer->prop.opacity);
401         double surf_alpha  = wl_fixed_to_double(ivisurf->prop.opacity);
402
403         if ((ivilayer->event_mask & IVI_NOTIFICATION_OPACITY) ||
404             (ivisurf->event_mask  & IVI_NOTIFICATION_OPACITY)) {
405                 struct weston_view *tmpview = NULL;
406                 wl_list_for_each(tmpview, &ivisurf->surface->views, surface_link) {
407                         if (tmpview == NULL) {
408                                 continue;
409                         }
410                         tmpview->alpha = layer_alpha * surf_alpha;
411                 }
412         }
413 }
414
415 static void
416 update_surface_orientation(struct ivi_layout_layer *ivilayer,
417                            struct ivi_layout_surface *ivisurf)
418 {
419         struct weston_view *view;
420         struct weston_matrix  *matrix = &ivisurf->surface_rotation.matrix;
421         float width  = 0.0f;
422         float height = 0.0f;
423         float v_sin  = 0.0f;
424         float v_cos  = 0.0f;
425         float cx = 0.0f;
426         float cy = 0.0f;
427         float sx = 1.0f;
428         float sy = 1.0f;
429
430         wl_list_for_each(view, &ivisurf->surface->views, surface_link) {
431                 if (view != NULL) {
432                         break;
433                 }
434         }
435
436         if (view == NULL) {
437                 return;
438         }
439
440         if ((ivilayer->prop.dest_width == 0) ||
441             (ivilayer->prop.dest_height == 0)) {
442                 return;
443         }
444         width  = (float)ivilayer->prop.dest_width;
445         height = (float)ivilayer->prop.dest_height;
446
447         switch (ivisurf->prop.orientation) {
448         case WL_OUTPUT_TRANSFORM_NORMAL:
449                 v_sin = 0.0f;
450                 v_cos = 1.0f;
451                 break;
452         case WL_OUTPUT_TRANSFORM_90:
453                 v_sin = 1.0f;
454                 v_cos = 0.0f;
455                 sx = width / height;
456                 sy = height / width;
457                 break;
458         case WL_OUTPUT_TRANSFORM_180:
459                 v_sin = 0.0f;
460                 v_cos = -1.0f;
461                 break;
462         case WL_OUTPUT_TRANSFORM_270:
463         default:
464                 v_sin = -1.0f;
465                 v_cos = 0.0f;
466                 sx = width / height;
467                 sy = height / width;
468                 break;
469         }
470         wl_list_remove(&ivisurf->surface_rotation.link);
471         weston_view_geometry_dirty(view);
472
473         weston_matrix_init(matrix);
474         cx = 0.5f * width;
475         cy = 0.5f * height;
476         weston_matrix_translate(matrix, -cx, -cy, 0.0f);
477         weston_matrix_rotate_xy(matrix, v_cos, v_sin);
478         weston_matrix_scale(matrix, sx, sy, 1.0);
479         weston_matrix_translate(matrix, cx, cy, 0.0f);
480         wl_list_insert(&view->geometry.transformation_list,
481                        &ivisurf->surface_rotation.link);
482
483         weston_view_set_transform_parent(view, NULL);
484         weston_view_update_transform(view);
485 }
486
487 static void
488 update_layer_orientation(struct ivi_layout_layer *ivilayer,
489                          struct ivi_layout_surface *ivisurf)
490 {
491         struct weston_surface *es = ivisurf->surface;
492         struct weston_view    *view;
493         struct weston_matrix  *matrix = &ivisurf->layer_rotation.matrix;
494         struct weston_output  *output = NULL;
495         float width  = 0.0f;
496         float height = 0.0f;
497         float v_sin  = 0.0f;
498         float v_cos  = 0.0f;
499         float cx = 0.0f;
500         float cy = 0.0f;
501         float sx = 1.0f;
502         float sy = 1.0f;
503
504         wl_list_for_each(view, &ivisurf->surface->views, surface_link) {
505                 if (view != NULL) {
506                         break;
507                 }
508         }
509
510         if (es == NULL || view == NULL) {
511                 return;
512         }
513
514         output = es->output;
515         if (output == NULL) {
516                 return;
517         }
518         if ((output->width == 0) || (output->height == 0)) {
519                 return;
520         }
521         width = (float)output->width;
522         height = (float)output->height;
523
524         switch (ivilayer->prop.orientation) {
525         case WL_OUTPUT_TRANSFORM_NORMAL:
526                 v_sin = 0.0f;
527                 v_cos = 1.0f;
528                 break;
529         case WL_OUTPUT_TRANSFORM_90:
530                 v_sin = 1.0f;
531                 v_cos = 0.0f;
532                 sx = width / height;
533                 sy = height / width;
534                 break;
535         case WL_OUTPUT_TRANSFORM_180:
536                 v_sin = 0.0f;
537                 v_cos = -1.0f;
538                 break;
539         case WL_OUTPUT_TRANSFORM_270:
540         default:
541                 v_sin = -1.0f;
542                 v_cos = 0.0f;
543                 sx = width / height;
544                 sy = height / width;
545                 break;
546         }
547         wl_list_remove(&ivisurf->layer_rotation.link);
548         weston_view_geometry_dirty(view);
549
550         weston_matrix_init(matrix);
551         cx = 0.5f * width;
552         cy = 0.5f * height;
553         weston_matrix_translate(matrix, -cx, -cy, 0.0f);
554         weston_matrix_rotate_xy(matrix, v_cos, v_sin);
555         weston_matrix_scale(matrix, sx, sy, 1.0);
556         weston_matrix_translate(matrix, cx, cy, 0.0f);
557         wl_list_insert(&view->geometry.transformation_list,
558                        &ivisurf->layer_rotation.link);
559
560         weston_view_set_transform_parent(view, NULL);
561         weston_view_update_transform(view);
562 }
563
564 static void
565 update_surface_position(struct ivi_layout_surface *ivisurf)
566 {
567         struct weston_view *view;
568         float tx  = (float)ivisurf->prop.dest_x;
569         float ty  = (float)ivisurf->prop.dest_y;
570         struct weston_matrix *matrix = &ivisurf->surface_pos.matrix;
571
572         wl_list_for_each(view, &ivisurf->surface->views, surface_link) {
573                 if (view != NULL) {
574                         break;
575                 }
576         }
577
578         if (view == NULL) {
579                 return;
580         }
581
582         wl_list_remove(&ivisurf->surface_pos.link);
583
584         weston_matrix_init(matrix);
585         weston_matrix_translate(matrix, tx, ty, 0.0f);
586         wl_list_insert(&view->geometry.transformation_list,
587                        &ivisurf->surface_pos.link);
588
589         weston_view_set_transform_parent(view, NULL);
590         weston_view_update_transform(view);
591 }
592
593 static void
594 update_layer_position(struct ivi_layout_layer *ivilayer,
595                       struct ivi_layout_surface *ivisurf)
596 {
597         struct weston_view *view;
598         struct weston_matrix *matrix = &ivisurf->layer_pos.matrix;
599         float tx  = (float)ivilayer->prop.dest_x;
600         float ty  = (float)ivilayer->prop.dest_y;
601
602         wl_list_for_each(view, &ivisurf->surface->views, surface_link) {
603                 if (view != NULL) {
604                         break;
605                 }
606         }
607
608         if (view == NULL) {
609                 return;
610         }
611
612         wl_list_remove(&ivisurf->layer_pos.link);
613
614         weston_matrix_init(matrix);
615         weston_matrix_translate(matrix, tx, ty, 0.0f);
616         wl_list_insert(&view->geometry.transformation_list,
617                        &ivisurf->layer_pos.link);
618
619         weston_view_set_transform_parent(view, NULL);
620         weston_view_update_transform(view);
621 }
622
623 static void
624 update_scale(struct ivi_layout_layer *ivilayer,
625              struct ivi_layout_surface *ivisurf)
626 {
627         struct weston_view *view;
628         struct weston_matrix *matrix = &ivisurf->scaling.matrix;
629         float sx = 0.0f;
630         float sy = 0.0f;
631         float lw = 0.0f;
632         float sw = 0.0f;
633         float lh = 0.0f;
634         float sh = 0.0f;
635
636         wl_list_for_each(view, &ivisurf->surface->views, surface_link) {
637                 if (view != NULL) {
638                         break;
639                 }
640         }
641
642         if (view == NULL) {
643                 return;
644         }
645
646         if (ivisurf->prop.dest_width == 0 && ivisurf->prop.dest_height == 0) {
647                 ivisurf->prop.dest_width  = ivisurf->surface->width_from_buffer;
648                 ivisurf->prop.dest_height = ivisurf->surface->height_from_buffer;
649         }
650
651         lw = ((float)ivilayer->prop.dest_width  / (float)ivilayer->prop.source_width );
652         sw = ((float)ivisurf->prop.dest_width   / (float)ivisurf->prop.source_width  );
653         lh = ((float)ivilayer->prop.dest_height / (float)ivilayer->prop.source_height);
654         sh = ((float)ivisurf->prop.dest_height  / (float)ivisurf->prop.source_height );
655         sx = sw * lw;
656         sy = sh * lh;
657
658         wl_list_remove(&ivisurf->scaling.link);
659         weston_matrix_init(matrix);
660         weston_matrix_scale(matrix, sx, sy, 1.0f);
661
662         wl_list_insert(&view->geometry.transformation_list,
663                        &ivisurf->scaling.link);
664
665         weston_view_set_transform_parent(view, NULL);
666         weston_view_update_transform(view);
667 }
668
669 static void
670 update_prop(struct ivi_layout_layer *ivilayer,
671             struct ivi_layout_surface *ivisurf)
672 {
673         if (ivilayer->event_mask | ivisurf->event_mask) {
674                 struct weston_view *tmpview;
675                 update_opacity(ivilayer, ivisurf);
676                 update_layer_orientation(ivilayer, ivisurf);
677                 update_layer_position(ivilayer, ivisurf);
678                 update_surface_position(ivisurf);
679                 update_surface_orientation(ivilayer, ivisurf);
680                 update_scale(ivilayer, ivisurf);
681
682                 ivisurf->update_count++;
683
684                 wl_list_for_each(tmpview, &ivisurf->surface->views, surface_link) {
685                         if (tmpview != NULL) {
686                                 break;
687                         }
688                 }
689
690                 if (tmpview != NULL) {
691                         weston_view_geometry_dirty(tmpview);
692                 }
693
694                 if (ivisurf->surface != NULL) {
695                         weston_surface_damage(ivisurf->surface);
696                 }
697         }
698 }
699
700 static void
701 commit_changes(struct ivi_layout *layout)
702 {
703         struct ivi_layout_screen  *iviscrn  = NULL;
704         struct ivi_layout_layer   *ivilayer = NULL;
705         struct ivi_layout_surface *ivisurf  = NULL;
706
707         wl_list_for_each(iviscrn, &layout->screen_list, link) {
708                 wl_list_for_each(ivilayer, &iviscrn->order.layer_list, order.link) {
709                         wl_list_for_each(ivisurf, &ivilayer->order.surface_list, order.link) {
710                                 update_prop(ivilayer, ivisurf);
711                         }
712                 }
713         }
714 }
715
716 static void
717 commit_surface_list(struct ivi_layout *layout)
718 {
719         struct ivi_layout_surface *ivisurf = NULL;
720         int32_t dest_x = 0;
721         int32_t dest_y = 0;
722         int32_t dest_width = 0;
723         int32_t dest_height = 0;
724         int32_t configured = 0;
725
726         wl_list_for_each(ivisurf, &layout->surface_list, link) {
727                 if(ivisurf->pending.prop.transition_type == IVI_LAYOUT_TRANSITION_VIEW_DEFAULT) {
728                         dest_x = ivisurf->prop.dest_x;
729                         dest_y = ivisurf->prop.dest_y;
730                         dest_width = ivisurf->prop.dest_width;
731                         dest_height = ivisurf->prop.dest_height;
732
733                         ivi_layout_transition_move_resize_view(ivisurf,
734                                                                ivisurf->pending.prop.dest_x,
735                                                                ivisurf->pending.prop.dest_y,
736                                                                ivisurf->pending.prop.dest_width,
737                                                                ivisurf->pending.prop.dest_height,
738                                                                ivisurf->pending.prop.transition_duration);
739
740                         if(ivisurf->pending.prop.visibility) {
741                                 ivi_layout_transition_visibility_on(ivisurf, ivisurf->pending.prop.transition_duration);
742                         } else {
743                                 ivi_layout_transition_visibility_off(ivisurf, ivisurf->pending.prop.transition_duration);
744                         }
745
746                         ivisurf->prop = ivisurf->pending.prop;
747                         ivisurf->prop.dest_x = dest_x;
748                         ivisurf->prop.dest_y = dest_y;
749                         ivisurf->prop.dest_width = dest_width;
750                         ivisurf->prop.dest_height = dest_height;
751                         ivisurf->prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;
752                         ivisurf->pending.prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;
753
754                 } else if(ivisurf->pending.prop.transition_type == IVI_LAYOUT_TRANSITION_VIEW_DEST_RECT_ONLY){
755                         dest_x = ivisurf->prop.dest_x;
756                         dest_y = ivisurf->prop.dest_y;
757                         dest_width = ivisurf->prop.dest_width;
758                         dest_height = ivisurf->prop.dest_height;
759
760                         ivi_layout_transition_move_resize_view(ivisurf,
761                                                                ivisurf->pending.prop.dest_x,
762                                                                ivisurf->pending.prop.dest_y,
763                                                                ivisurf->pending.prop.dest_width,
764                                                                ivisurf->pending.prop.dest_height,
765                                                                ivisurf->pending.prop.transition_duration);
766
767                         ivisurf->prop = ivisurf->pending.prop;
768                         ivisurf->prop.dest_x = dest_x;
769                         ivisurf->prop.dest_y = dest_y;
770                         ivisurf->prop.dest_width = dest_width;
771                         ivisurf->prop.dest_height = dest_height;
772
773                         ivisurf->prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;
774                         ivisurf->pending.prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;
775
776                 } else if(ivisurf->pending.prop.transition_type == IVI_LAYOUT_TRANSITION_VIEW_FADE_ONLY){
777                         configured = 0;
778                         if(ivisurf->pending.prop.visibility) {
779                                 ivi_layout_transition_visibility_on(ivisurf, ivisurf->pending.prop.transition_duration);
780                         } else {
781                                 ivi_layout_transition_visibility_off(ivisurf, ivisurf->pending.prop.transition_duration);
782                         }
783
784                         if (ivisurf->prop.dest_width  != ivisurf->pending.prop.dest_width ||
785                             ivisurf->prop.dest_height != ivisurf->pending.prop.dest_height) {
786                                 configured = 1;
787                         }
788
789                         ivisurf->prop = ivisurf->pending.prop;
790                         ivisurf->prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;
791                         ivisurf->pending.prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;
792
793                         if (configured && !is_surface_transition(ivisurf))
794                                 wl_signal_emit(&ivisurf->configured, ivisurf);
795                 } else {
796                         configured = 0;
797                         if (ivisurf->prop.dest_width  != ivisurf->pending.prop.dest_width ||
798                             ivisurf->prop.dest_height != ivisurf->pending.prop.dest_height) {
799                                 configured = 1;
800                         }
801
802                         ivisurf->prop = ivisurf->pending.prop;
803                         ivisurf->prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;
804                         ivisurf->pending.prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;
805
806                         if (configured && !is_surface_transition(ivisurf))
807                                 wl_signal_emit(&ivisurf->configured, ivisurf);
808                 }
809         }
810 }
811
812 static void
813 commit_layer_list(struct ivi_layout *layout)
814 {
815         struct ivi_layout_layer   *ivilayer = NULL;
816         struct ivi_layout_surface *ivisurf  = NULL;
817         struct ivi_layout_surface *next     = NULL;
818
819         wl_list_for_each(ivilayer, &layout->layer_list, link) {
820                 if(ivilayer->pending.prop.transition_type == IVI_LAYOUT_TRANSITION_LAYER_MOVE) {
821                         ivi_layout_transition_move_layer(ivilayer, ivilayer->pending.prop.dest_x, ivilayer->pending.prop.dest_y, ivilayer->pending.prop.transition_duration);
822                 } else if(ivilayer->pending.prop.transition_type == IVI_LAYOUT_TRANSITION_LAYER_FADE) {
823                         ivi_layout_transition_fade_layer(ivilayer,ivilayer->pending.prop.is_fade_in,
824                                                          ivilayer->pending.prop.start_alpha,ivilayer->pending.prop.end_alpha,
825                                                          NULL, NULL,
826                                                          ivilayer->pending.prop.transition_duration);
827                 }
828                 ivilayer->pending.prop.transition_type = IVI_LAYOUT_TRANSITION_NONE;
829
830                 ivilayer->prop = ivilayer->pending.prop;
831
832                 if (!(ivilayer->event_mask &
833                       (IVI_NOTIFICATION_ADD | IVI_NOTIFICATION_REMOVE)) ) {
834                         continue;
835                 }
836
837                 if (ivilayer->event_mask & IVI_NOTIFICATION_REMOVE) {
838                         wl_list_for_each_safe(ivisurf, next,
839                                 &ivilayer->order.surface_list, order.link) {
840                                 remove_ordersurface_from_layer(ivisurf);
841
842                                 if (!wl_list_empty(&ivisurf->order.link)) {
843                                         wl_list_remove(&ivisurf->order.link);
844                                 }
845
846                                 wl_list_init(&ivisurf->order.link);
847                                 ivisurf->event_mask |= IVI_NOTIFICATION_REMOVE;
848                         }
849
850                         wl_list_init(&ivilayer->order.surface_list);
851                 }
852
853                 if (ivilayer->event_mask & IVI_NOTIFICATION_ADD) {
854                         wl_list_for_each_safe(ivisurf, next,
855                                               &ivilayer->order.surface_list, order.link) {
856                                 remove_ordersurface_from_layer(ivisurf);
857
858                                 if (!wl_list_empty(&ivisurf->order.link)) {
859                                         wl_list_remove(&ivisurf->order.link);
860                                 }
861
862                                 wl_list_init(&ivisurf->order.link);
863                         }
864
865                         wl_list_init(&ivilayer->order.surface_list);
866                         wl_list_for_each(ivisurf, &ivilayer->pending.surface_list,
867                                          pending.link) {
868                                 if(!wl_list_empty(&ivisurf->order.link)){
869                                         wl_list_remove(&ivisurf->order.link);
870                                         wl_list_init(&ivisurf->order.link);
871                                 }
872
873                                 wl_list_insert(&ivilayer->order.surface_list,
874                                                &ivisurf->order.link);
875                                 add_ordersurface_to_layer(ivisurf, ivilayer);
876                                 ivisurf->event_mask |= IVI_NOTIFICATION_ADD;
877                         }
878                 }
879         }
880 }
881
882 static void
883 commit_screen_list(struct ivi_layout *layout)
884 {
885         struct ivi_layout_screen  *iviscrn  = NULL;
886         struct ivi_layout_layer   *ivilayer = NULL;
887         struct ivi_layout_layer   *next     = NULL;
888         struct ivi_layout_surface *ivisurf  = NULL;
889         struct weston_view        *view, *n;
890
891         /* clear view list of layout layer */
892         wl_list_for_each_safe(view, n, &layout->layout_layer.view_list.link, layer_link.link) {
893                 weston_layer_entry_remove(&view->layer_link);
894         }
895
896
897         wl_list_for_each(iviscrn, &layout->screen_list, link) {
898                 if (iviscrn->event_mask & IVI_NOTIFICATION_REMOVE) {
899                         wl_list_for_each_safe(ivilayer, next,
900                                               &iviscrn->order.layer_list, order.link) {
901                                 remove_orderlayer_from_screen(ivilayer);
902
903                                 if (!wl_list_empty(&ivilayer->order.link)) {
904                                     wl_list_remove(&ivilayer->order.link);
905                                 }
906
907                                 wl_list_init(&ivilayer->order.link);
908                                 ivilayer->event_mask |= IVI_NOTIFICATION_REMOVE;
909                         }
910                 }
911
912                 if (iviscrn->event_mask & IVI_NOTIFICATION_ADD) {
913                         wl_list_for_each_safe(ivilayer, next,
914                                               &iviscrn->order.layer_list, order.link) {
915                                 remove_orderlayer_from_screen(ivilayer);
916
917                                 if (!wl_list_empty(&ivilayer->order.link)) {
918                                         wl_list_remove(&ivilayer->order.link);
919                                 }
920
921                                 wl_list_init(&ivilayer->order.link);
922                         }
923
924                         wl_list_init(&iviscrn->order.layer_list);
925                         wl_list_for_each(ivilayer, &iviscrn->pending.layer_list,
926                                          pending.link) {
927                                 wl_list_insert(&iviscrn->order.layer_list,
928                                                &ivilayer->order.link);
929                                 add_orderlayer_to_screen(ivilayer, iviscrn);
930                                 ivilayer->event_mask |= IVI_NOTIFICATION_ADD;
931                         }
932                 }
933
934                 iviscrn->event_mask = 0;
935
936                 /* rebuild view list of layout layer */
937                 wl_list_for_each(ivilayer, &iviscrn->order.layer_list, order.link) {
938                         if (ivilayer->prop.visibility == false)
939                                 continue;
940
941                         wl_list_for_each(ivisurf, &ivilayer->order.surface_list, order.link) {
942                                 struct weston_view *tmpview = NULL;
943                                 wl_list_for_each(tmpview, &ivisurf->surface->views, surface_link) {
944                                         if (tmpview != NULL) {
945                                                 break;
946                                         }
947                                 }
948
949                                 if (ivisurf->prop.visibility == false)
950                                         continue;
951                                 if (ivisurf->surface == NULL || tmpview == NULL)
952                                         continue;
953
954                                 weston_layer_entry_insert(&layout->layout_layer.view_list,
955                                                           &tmpview->layer_link);
956
957                                 ivisurf->surface->output = iviscrn->output;
958                         }
959                 }
960
961                 break;
962         }
963 }
964
965 static void
966 commit_transition(struct ivi_layout* layout)
967 {
968         if(wl_list_empty(&layout->pending_transition_list)){
969                 return;
970         }
971
972         wl_list_insert_list(&layout->transitions->transition_list,
973                             &layout->pending_transition_list);
974
975         wl_list_init(&layout->pending_transition_list);
976
977         wl_event_source_timer_update(layout->transitions->event_source, 1);
978 }
979
980 static void
981 send_surface_prop(struct ivi_layout_surface *ivisurf)
982 {
983         wl_signal_emit(&ivisurf->property_changed, ivisurf);
984         ivisurf->event_mask = 0;
985 }
986
987 static void
988 send_layer_prop(struct ivi_layout_layer *ivilayer)
989 {
990         wl_signal_emit(&ivilayer->property_changed, ivilayer);
991         ivilayer->event_mask = 0;
992 }
993
994 static void
995 send_prop(struct ivi_layout *layout)
996 {
997         struct ivi_layout_layer   *ivilayer = NULL;
998         struct ivi_layout_surface *ivisurf  = NULL;
999
1000         wl_list_for_each_reverse(ivilayer, &layout->layer_list, link) {
1001                 send_layer_prop(ivilayer);
1002         }
1003
1004         wl_list_for_each_reverse(ivisurf, &layout->surface_list, link) {
1005                 send_surface_prop(ivisurf);
1006         }
1007 }
1008
1009 static void
1010 clear_surface_pending_list(struct ivi_layout_layer *ivilayer)
1011 {
1012         struct ivi_layout_surface *surface_link = NULL;
1013         struct ivi_layout_surface *surface_next = NULL;
1014
1015         wl_list_for_each_safe(surface_link, surface_next,
1016                               &ivilayer->pending.surface_list, pending.link) {
1017                 if (!wl_list_empty(&surface_link->pending.link)) {
1018                         wl_list_remove(&surface_link->pending.link);
1019                 }
1020
1021                 wl_list_init(&surface_link->pending.link);
1022         }
1023
1024         ivilayer->event_mask |= IVI_NOTIFICATION_REMOVE;
1025 }
1026
1027 static void
1028 clear_surface_order_list(struct ivi_layout_layer *ivilayer)
1029 {
1030         struct ivi_layout_surface *surface_link = NULL;
1031         struct ivi_layout_surface *surface_next = NULL;
1032
1033         wl_list_for_each_safe(surface_link, surface_next,
1034                               &ivilayer->order.surface_list, order.link) {
1035                 if (!wl_list_empty(&surface_link->order.link)) {
1036                         wl_list_remove(&surface_link->order.link);
1037                 }
1038
1039                 wl_list_init(&surface_link->order.link);
1040         }
1041
1042         ivilayer->event_mask |= IVI_NOTIFICATION_REMOVE;
1043 }
1044
1045 static void
1046 layer_created(struct wl_listener *listener, void *data)
1047 {
1048         struct ivi_layout_layer *ivilayer = data;
1049
1050         struct listener_layout_notification *notification =
1051                 container_of(listener,
1052                              struct listener_layout_notification,
1053                              listener);
1054
1055         struct ivi_layout_notification_callback *created_callback =
1056                 notification->userdata;
1057
1058         ((layer_create_notification_func)created_callback->callback)
1059                 (ivilayer, created_callback->data);
1060 }
1061
1062 static void
1063 layer_removed(struct wl_listener *listener, void *data)
1064 {
1065         struct ivi_layout_layer *ivilayer = data;
1066
1067         struct listener_layout_notification *notification =
1068                 container_of(listener,
1069                              struct listener_layout_notification,
1070                              listener);
1071
1072         struct ivi_layout_notification_callback *removed_callback =
1073                 notification->userdata;
1074
1075         ((layer_remove_notification_func)removed_callback->callback)
1076                 (ivilayer, removed_callback->data);
1077 }
1078
1079 static void
1080 layer_prop_changed(struct wl_listener *listener, void *data)
1081 {
1082         struct ivi_layout_layer *ivilayer = data;
1083
1084         struct listener_layout_notification *layout_listener =
1085                 container_of(listener,
1086                              struct listener_layout_notification,
1087                              listener);
1088
1089         struct ivi_layout_notification_callback *prop_callback =
1090                 layout_listener->userdata;
1091
1092         ((layer_property_notification_func)prop_callback->callback)
1093                 (ivilayer, &ivilayer->prop, ivilayer->event_mask, prop_callback->data);
1094 }
1095
1096 static void
1097 surface_created(struct wl_listener *listener, void *data)
1098 {
1099         struct ivi_layout_surface *ivisurface = data;
1100
1101         struct listener_layout_notification *notification =
1102                 container_of(listener,
1103                              struct listener_layout_notification,
1104                              listener);
1105
1106         struct ivi_layout_notification_callback *created_callback =
1107                 notification->userdata;
1108
1109         ((surface_create_notification_func)created_callback->callback)
1110                 (ivisurface, created_callback->data);
1111 }
1112
1113 static void
1114 surface_removed(struct wl_listener *listener, void *data)
1115 {
1116         struct ivi_layout_surface *ivisurface = data;
1117
1118         struct listener_layout_notification *notification =
1119                 container_of(listener,
1120                              struct listener_layout_notification,
1121                              listener);
1122
1123         struct ivi_layout_notification_callback *removed_callback =
1124                 notification->userdata;
1125
1126         ((surface_remove_notification_func)removed_callback->callback)
1127                 (ivisurface, removed_callback->data);
1128 }
1129
1130 static void
1131 surface_prop_changed(struct wl_listener *listener, void *data)
1132 {
1133         struct ivi_layout_surface *ivisurf = data;
1134
1135         struct listener_layout_notification *layout_listener =
1136                 container_of(listener,
1137                              struct listener_layout_notification,
1138                              listener);
1139
1140         struct ivi_layout_notification_callback *prop_callback =
1141                 layout_listener->userdata;
1142
1143         ((surface_property_notification_func)prop_callback->callback)
1144                 (ivisurf, &ivisurf->prop, ivisurf->event_mask, prop_callback->data);
1145
1146         ivisurf->event_mask = 0;
1147 }
1148
1149 static void
1150 surface_configure_changed(struct wl_listener *listener,
1151                           void *data)
1152 {
1153         struct ivi_layout_surface *ivisurface = data;
1154
1155         struct listener_layout_notification *notification =
1156                 container_of(listener,
1157                              struct listener_layout_notification,
1158                              listener);
1159
1160         struct ivi_layout_notification_callback *configure_changed_callback =
1161                 notification->userdata;
1162
1163         ((surface_configure_notification_func)configure_changed_callback->callback)
1164                 (ivisurface, configure_changed_callback->data);
1165 }
1166
1167 static int32_t
1168 add_notification(struct wl_signal *signal,
1169                  wl_notify_func_t callback,
1170                  void *userdata)
1171 {
1172         struct listener_layout_notification *notification = NULL;
1173
1174         notification = malloc(sizeof *notification);
1175         if (notification == NULL) {
1176                 weston_log("fails to allocate memory\n");
1177                 free(userdata);
1178                 return IVI_FAILED;
1179         }
1180
1181         notification->listener.notify = callback;
1182         notification->userdata = userdata;
1183
1184         wl_signal_add(signal, &notification->listener);
1185
1186         return IVI_SUCCEEDED;
1187 }
1188
1189 static void
1190 remove_notification(struct wl_list *listener_list, void *callback, void *userdata)
1191 {
1192         struct wl_listener *listener = NULL;
1193         struct wl_listener *next = NULL;
1194
1195         wl_list_for_each_safe(listener, next, listener_list, link) {
1196                 struct listener_layout_notification *notification =
1197                         container_of(listener,
1198                                      struct listener_layout_notification,
1199                                      listener);
1200
1201                 struct ivi_layout_notification_callback *notification_callback =
1202                         notification->userdata;
1203
1204                 if ((notification_callback->callback != callback) ||
1205                     (notification_callback->data != userdata)) {
1206                         continue;
1207                 }
1208
1209                 if (!wl_list_empty(&listener->link)) {
1210                         wl_list_remove(&listener->link);
1211                 }
1212
1213                 free(notification->userdata);
1214                 free(notification);
1215         }
1216 }
1217
1218 static void
1219 remove_all_notification(struct wl_list *listener_list)
1220 {
1221         struct wl_listener *listener = NULL;
1222         struct wl_listener *next = NULL;
1223
1224         wl_list_for_each_safe(listener, next, listener_list, link) {
1225                 struct listener_layout_notification *notification = NULL;
1226                 if (!wl_list_empty(&listener->link)) {
1227                         wl_list_remove(&listener->link);
1228                 }
1229
1230                 notification =
1231                         container_of(listener,
1232                                      struct listener_layout_notification,
1233                                      listener);
1234
1235                 free(notification->userdata);
1236                 free(notification);
1237         }
1238 }
1239
1240 /**
1241  * Exported APIs of ivi-layout library are implemented from here.
1242  * Brief of APIs is described in ivi-layout-export.h.
1243  */
1244 WL_EXPORT int32_t
1245 ivi_layout_add_notification_create_layer(layer_create_notification_func callback,
1246                                          void *userdata)
1247 {
1248         struct ivi_layout *layout = get_layout_instance();
1249         struct ivi_layout_notification_callback *created_callback = NULL;
1250
1251         if (callback == NULL) {
1252                 weston_log("ivi_layout_add_notification_create_layer: invalid argument\n");
1253                 return IVI_FAILED;
1254         }
1255
1256         created_callback = malloc(sizeof *created_callback);
1257         if (created_callback == NULL) {
1258                 weston_log("fails to allocate memory\n");
1259                 return IVI_FAILED;
1260         }
1261
1262         created_callback->callback = callback;
1263         created_callback->data = userdata;
1264
1265         return add_notification(&layout->layer_notification.created,
1266                                 layer_created,
1267                                 created_callback);
1268 }
1269
1270 WL_EXPORT void
1271 ivi_layout_remove_notification_create_layer(layer_create_notification_func callback,
1272                                             void *userdata)
1273 {
1274         struct ivi_layout *layout = get_layout_instance();
1275         remove_notification(&layout->layer_notification.created.listener_list, callback, userdata);
1276 }
1277
1278 WL_EXPORT int32_t
1279 ivi_layout_add_notification_remove_layer(layer_remove_notification_func callback,
1280                                          void *userdata)
1281 {
1282         struct ivi_layout *layout = get_layout_instance();
1283         struct ivi_layout_notification_callback *removed_callback = NULL;
1284
1285         if (callback == NULL) {
1286                 weston_log("ivi_layout_add_notification_remove_layer: invalid argument\n");
1287                 return IVI_FAILED;
1288         }
1289
1290         removed_callback = malloc(sizeof *removed_callback);
1291         if (removed_callback == NULL) {
1292                 weston_log("fails to allocate memory\n");
1293                 return IVI_FAILED;
1294         }
1295
1296         removed_callback->callback = callback;
1297         removed_callback->data = userdata;
1298         return add_notification(&layout->layer_notification.removed,
1299                                 layer_removed,
1300                                 removed_callback);
1301 }
1302
1303 WL_EXPORT void
1304 ivi_layout_remove_notification_remove_layer(layer_remove_notification_func callback,
1305                                             void *userdata)
1306 {
1307         struct ivi_layout *layout = get_layout_instance();
1308         remove_notification(&layout->layer_notification.removed.listener_list, callback, userdata);
1309 }
1310
1311 WL_EXPORT int32_t
1312 ivi_layout_add_notification_create_surface(surface_create_notification_func callback,
1313                                            void *userdata)
1314 {
1315         struct ivi_layout *layout = get_layout_instance();
1316         struct ivi_layout_notification_callback *created_callback = NULL;
1317
1318         if (callback == NULL) {
1319                 weston_log("ivi_layout_add_notification_create_surface: invalid argument\n");
1320                 return IVI_FAILED;
1321         }
1322
1323         created_callback = malloc(sizeof *created_callback);
1324         if (created_callback == NULL) {
1325                 weston_log("fails to allocate memory\n");
1326                 return IVI_FAILED;
1327         }
1328
1329         created_callback->callback = callback;
1330         created_callback->data = userdata;
1331
1332         return add_notification(&layout->surface_notification.created,
1333                                 surface_created,
1334                                 created_callback);
1335 }
1336
1337 WL_EXPORT void
1338 ivi_layout_remove_notification_create_surface(surface_create_notification_func callback,
1339                                               void *userdata)
1340 {
1341         struct ivi_layout *layout = get_layout_instance();
1342         remove_notification(&layout->surface_notification.created.listener_list, callback, userdata);
1343 }
1344
1345 WL_EXPORT int32_t
1346 ivi_layout_add_notification_remove_surface(surface_remove_notification_func callback,
1347                                            void *userdata)
1348 {
1349         struct ivi_layout *layout = get_layout_instance();
1350         struct ivi_layout_notification_callback *removed_callback = NULL;
1351
1352         if (callback == NULL) {
1353                 weston_log("ivi_layout_add_notification_remove_surface: invalid argument\n");
1354                 return IVI_FAILED;
1355         }
1356
1357         removed_callback = malloc(sizeof *removed_callback);
1358         if (removed_callback == NULL) {
1359                 weston_log("fails to allocate memory\n");
1360                 return IVI_FAILED;
1361         }
1362
1363         removed_callback->callback = callback;
1364         removed_callback->data = userdata;
1365
1366         return add_notification(&layout->surface_notification.removed,
1367                                 surface_removed,
1368                                 removed_callback);
1369 }
1370
1371 WL_EXPORT void
1372 ivi_layout_remove_notification_remove_surface(surface_remove_notification_func callback,
1373                                               void *userdata)
1374 {
1375         struct ivi_layout *layout = get_layout_instance();
1376         remove_notification(&layout->surface_notification.removed.listener_list, callback, userdata);
1377 }
1378
1379 WL_EXPORT int32_t
1380 ivi_layout_add_notification_configure_surface(surface_configure_notification_func callback,
1381                                               void *userdata)
1382 {
1383         struct ivi_layout *layout = get_layout_instance();
1384         struct ivi_layout_notification_callback *configure_changed_callback = NULL;
1385         if (callback == NULL) {
1386                 weston_log("ivi_layout_add_notification_configure_surface: invalid argument\n");
1387                 return IVI_FAILED;
1388         }
1389
1390         configure_changed_callback = malloc(sizeof *configure_changed_callback);
1391         if (configure_changed_callback == NULL) {
1392                 weston_log("fails to allocate memory\n");
1393                 return IVI_FAILED;
1394         }
1395
1396         configure_changed_callback->callback = callback;
1397         configure_changed_callback->data = userdata;
1398
1399         return add_notification(&layout->surface_notification.configure_changed,
1400                                 surface_configure_changed,
1401                                 configure_changed_callback);
1402 }
1403
1404 WL_EXPORT void
1405 ivi_layout_remove_notification_configure_surface(surface_configure_notification_func callback,
1406                                                  void *userdata)
1407 {
1408         struct ivi_layout *layout = get_layout_instance();
1409         remove_notification(&layout->surface_notification.configure_changed.listener_list, callback, userdata);
1410 }
1411
1412 WL_EXPORT uint32_t
1413 ivi_layout_get_id_of_surface(struct ivi_layout_surface *ivisurf)
1414 {
1415         return ivisurf->id_surface;
1416 }
1417
1418 WL_EXPORT uint32_t
1419 ivi_layout_get_id_of_layer(struct ivi_layout_layer *ivilayer)
1420 {
1421         return ivilayer->id_layer;
1422 }
1423
1424 struct ivi_layout_layer *
1425 ivi_layout_get_layer_from_id(uint32_t id_layer)
1426 {
1427         struct ivi_layout *layout = get_layout_instance();
1428         struct ivi_layout_layer *ivilayer = NULL;
1429
1430         wl_list_for_each(ivilayer, &layout->layer_list, link) {
1431                 if (ivilayer->id_layer == id_layer) {
1432                         return ivilayer;
1433                 }
1434         }
1435
1436         return NULL;
1437 }
1438
1439 WL_EXPORT struct ivi_layout_surface *
1440 ivi_layout_get_surface_from_id(uint32_t id_surface)
1441 {
1442         struct ivi_layout *layout = get_layout_instance();
1443         struct ivi_layout_surface *ivisurf = NULL;
1444
1445         wl_list_for_each(ivisurf, &layout->surface_list, link) {
1446                 if (ivisurf->id_surface == id_surface) {
1447                         return ivisurf;
1448                 }
1449         }
1450
1451         return NULL;
1452 }
1453
1454 WL_EXPORT struct ivi_layout_screen *
1455 ivi_layout_get_screen_from_id(uint32_t id_screen)
1456 {
1457         struct ivi_layout *layout = get_layout_instance();
1458         struct ivi_layout_screen *iviscrn = NULL;
1459
1460         wl_list_for_each(iviscrn, &layout->screen_list, link) {
1461 /* FIXME : select iviscrn from screen_list by id_screen */
1462                 return iviscrn;
1463                 break;
1464         }
1465
1466         return NULL;
1467 }
1468
1469 WL_EXPORT int32_t
1470 ivi_layout_get_screen_resolution(struct ivi_layout_screen *iviscrn,
1471                                  int32_t *pWidth, int32_t *pHeight)
1472 {
1473         struct weston_output *output = NULL;
1474
1475         if (pWidth == NULL || pHeight == NULL) {
1476                 weston_log("ivi_layout_get_screen_resolution: invalid argument\n");
1477                 return IVI_FAILED;
1478         }
1479
1480         output   = iviscrn->output;
1481         *pWidth  = output->current_mode->width;
1482         *pHeight = output->current_mode->height;
1483
1484         return IVI_SUCCEEDED;
1485 }
1486
1487 WL_EXPORT int32_t
1488 ivi_layout_surface_add_notification(struct ivi_layout_surface *ivisurf,
1489                                     surface_property_notification_func callback,
1490                                     void *userdata)
1491 {
1492         struct listener_layout_notification* notification = NULL;
1493         struct ivi_layout_notification_callback *prop_callback = NULL;
1494
1495         if (ivisurf == NULL || callback == NULL) {
1496                 weston_log("ivi_layout_surface_add_notification: invalid argument\n");
1497                 return IVI_FAILED;
1498         }
1499
1500         notification = malloc(sizeof *notification);
1501         if (notification == NULL) {
1502                 weston_log("fails to allocate memory\n");
1503                 return IVI_FAILED;
1504         }
1505
1506         prop_callback = malloc(sizeof *prop_callback);
1507         if (prop_callback == NULL) {
1508                 weston_log("fails to allocate memory\n");
1509                 return IVI_FAILED;
1510         }
1511
1512         prop_callback->callback = callback;
1513         prop_callback->data = userdata;
1514
1515         notification->listener.notify = surface_prop_changed;
1516         notification->userdata = prop_callback;
1517
1518         wl_signal_add(&ivisurf->property_changed, &notification->listener);
1519
1520         return IVI_SUCCEEDED;
1521 }
1522
1523 WL_EXPORT void
1524 ivi_layout_surface_remove_notification(struct ivi_layout_surface *ivisurf)
1525 {
1526         if (ivisurf == NULL) {
1527                 weston_log("ivi_layout_surface_remove_notification: invalid argument\n");
1528                 return;
1529         }
1530
1531         remove_all_notification(&ivisurf->property_changed.listener_list);
1532 }
1533
1534 static void
1535 remove_configured_listener(struct ivi_layout_surface *ivisurf)
1536 {
1537         struct wl_listener *link = NULL;
1538         struct wl_listener *next = NULL;
1539
1540         wl_list_for_each_safe(link, next, &ivisurf->configured.listener_list, link) {
1541                 wl_list_remove(&link->link);
1542         }
1543 }
1544
1545 void
1546 ivi_layout_surface_remove(struct ivi_layout_surface *ivisurf)
1547 {
1548         struct ivi_layout *layout = get_layout_instance();
1549
1550         if (ivisurf == NULL) {
1551                 weston_log("ivi_layout_surface_remove: invalid argument\n");
1552                 return;
1553         }
1554
1555         if (!wl_list_empty(&ivisurf->pending.link)) {
1556                 wl_list_remove(&ivisurf->pending.link);
1557         }
1558         if (!wl_list_empty(&ivisurf->order.link)) {
1559                 wl_list_remove(&ivisurf->order.link);
1560         }
1561         if (!wl_list_empty(&ivisurf->link)) {
1562                 wl_list_remove(&ivisurf->link);
1563         }
1564         remove_ordersurface_from_layer(ivisurf);
1565
1566         wl_signal_emit(&layout->surface_notification.removed, ivisurf);
1567
1568         remove_configured_listener(ivisurf);
1569
1570         ivi_layout_surface_remove_notification(ivisurf);
1571
1572         free(ivisurf);
1573 }
1574
1575 WL_EXPORT const struct ivi_layout_layer_properties *
1576 ivi_layout_get_properties_of_layer(struct ivi_layout_layer *ivilayer)
1577 {
1578         if (ivilayer == NULL) {
1579                 weston_log("ivi_layout_get_properties_of_layer: invalid argument\n");
1580                 return NULL;
1581         }
1582
1583         return &ivilayer->prop;
1584 }
1585
1586 WL_EXPORT int32_t
1587 ivi_layout_get_screens(int32_t *pLength, struct ivi_layout_screen ***ppArray)
1588 {
1589         struct ivi_layout *layout = get_layout_instance();
1590         struct ivi_layout_screen *iviscrn = NULL;
1591         int32_t length = 0;
1592         int32_t n = 0;
1593
1594         if (pLength == NULL || ppArray == NULL) {
1595                 weston_log("ivi_layout_get_screens: invalid argument\n");
1596                 return IVI_FAILED;
1597         }
1598
1599         length = wl_list_length(&layout->screen_list);
1600
1601         if (length != 0){
1602                 /* the Array must be free by module which called this function */
1603                 *ppArray = calloc(length, sizeof(struct ivi_layout_screen *));
1604                 if (*ppArray == NULL) {
1605                         weston_log("fails to allocate memory\n");
1606                         return IVI_FAILED;
1607                 }
1608
1609                 wl_list_for_each(iviscrn, &layout->screen_list, link) {
1610                         (*ppArray)[n++] = iviscrn;
1611                 }
1612         }
1613
1614         *pLength = length;
1615
1616         return IVI_SUCCEEDED;
1617 }
1618
1619 WL_EXPORT int32_t
1620 ivi_layout_get_screens_under_layer(struct ivi_layout_layer *ivilayer,
1621                                    int32_t *pLength,
1622                                    struct ivi_layout_screen ***ppArray)
1623 {
1624         struct link_screen *link_scrn = NULL;
1625         int32_t length = 0;
1626         int32_t n = 0;
1627
1628         if (ivilayer == NULL || pLength == NULL || ppArray == NULL) {
1629                 weston_log("ivi_layout_get_screens_under_layer: invalid argument\n");
1630                 return IVI_FAILED;
1631         }
1632
1633         length = wl_list_length(&ivilayer->screen_list);
1634
1635         if (length != 0){
1636                 /* the Array must be free by module which called this function */
1637                 *ppArray = calloc(length, sizeof(struct ivi_layout_screen *));
1638                 if (*ppArray == NULL) {
1639                         weston_log("fails to allocate memory\n");
1640                         return IVI_FAILED;
1641                 }
1642
1643                 wl_list_for_each(link_scrn, &ivilayer->screen_list, link) {
1644                         (*ppArray)[n++] = link_scrn->iviscrn;
1645                 }
1646         }
1647
1648         *pLength = length;
1649
1650         return IVI_SUCCEEDED;
1651 }
1652
1653 WL_EXPORT int32_t
1654 ivi_layout_get_layers(int32_t *pLength, struct ivi_layout_layer ***ppArray)
1655 {
1656         struct ivi_layout *layout = get_layout_instance();
1657         struct ivi_layout_layer *ivilayer = NULL;
1658         int32_t length = 0;
1659         int32_t n = 0;
1660
1661         if (pLength == NULL || ppArray == NULL) {
1662                 weston_log("ivi_layout_get_layers: invalid argument\n");
1663                 return IVI_FAILED;
1664         }
1665
1666         length = wl_list_length(&layout->layer_list);
1667
1668         if (length != 0){
1669                 /* the Array must be free by module which called this function */
1670                 *ppArray = calloc(length, sizeof(struct ivi_layout_layer *));
1671                 if (*ppArray == NULL) {
1672                         weston_log("fails to allocate memory\n");
1673                         return IVI_FAILED;
1674                 }
1675
1676                 wl_list_for_each(ivilayer, &layout->layer_list, link) {
1677                         (*ppArray)[n++] = ivilayer;
1678                 }
1679         }
1680
1681         *pLength = length;
1682
1683         return IVI_SUCCEEDED;
1684 }
1685
1686 int32_t
1687 ivi_layout_get_layers_on_screen(struct ivi_layout_screen *iviscrn,
1688                                 int32_t *pLength,
1689                                 struct ivi_layout_layer ***ppArray)
1690 {
1691         struct ivi_layout_layer *ivilayer = NULL;
1692         int32_t length = 0;
1693         int32_t n = 0;
1694
1695         if (iviscrn == NULL || pLength == NULL || ppArray == NULL) {
1696                 weston_log("ivi_layout_get_layers_on_screen: invalid argument\n");
1697                 return IVI_FAILED;
1698         }
1699
1700         length = wl_list_length(&iviscrn->order.layer_list);
1701
1702         if (length != 0){
1703                 /* the Array must be free by module which called this function */
1704                 *ppArray = calloc(length, sizeof(struct ivi_layout_layer *));
1705                 if (*ppArray == NULL) {
1706                         weston_log("fails to allocate memory\n");
1707                         return IVI_FAILED;
1708                 }
1709
1710                 wl_list_for_each(ivilayer, &iviscrn->order.layer_list, link) {
1711                         (*ppArray)[n++] = ivilayer;
1712                 }
1713         }
1714
1715         *pLength = length;
1716
1717         return IVI_SUCCEEDED;
1718 }
1719
1720 WL_EXPORT int32_t
1721 ivi_layout_get_layers_under_surface(struct ivi_layout_surface *ivisurf,
1722                                     int32_t *pLength,
1723                                     struct ivi_layout_layer ***ppArray)
1724 {
1725         struct link_layer *link_layer = NULL;
1726         int32_t length = 0;
1727         int32_t n = 0;
1728
1729         if (ivisurf == NULL || pLength == NULL || ppArray == NULL) {
1730                 weston_log("ivi_layout_getLayers: invalid argument\n");
1731                 return IVI_FAILED;
1732         }
1733
1734         length = wl_list_length(&ivisurf->layer_list);
1735
1736         if (length != 0){
1737                 /* the Array must be free by module which called this function */
1738                 *ppArray = calloc(length, sizeof(struct ivi_layout_layer *));
1739                 if (*ppArray == NULL) {
1740                         weston_log("fails to allocate memory\n");
1741                         return IVI_FAILED;
1742                 }
1743
1744                 wl_list_for_each(link_layer, &ivisurf->layer_list, link) {
1745                         (*ppArray)[n++] = link_layer->ivilayer;
1746                 }
1747         }
1748
1749         *pLength = length;
1750
1751         return IVI_SUCCEEDED;
1752 }
1753
1754 WL_EXPORT int32_t
1755 ivi_layout_get_surfaces(int32_t *pLength, struct ivi_layout_surface ***ppArray)
1756 {
1757         struct ivi_layout *layout = get_layout_instance();
1758         struct ivi_layout_surface *ivisurf = NULL;
1759         int32_t length = 0;
1760         int32_t n = 0;
1761
1762         if (pLength == NULL || ppArray == NULL) {
1763                 weston_log("ivi_layout_get_surfaces: invalid argument\n");
1764                 return IVI_FAILED;
1765         }
1766
1767         length = wl_list_length(&layout->surface_list);
1768
1769         if (length != 0){
1770                 /* the Array must be free by module which called this function */
1771                 *ppArray = calloc(length, sizeof(struct ivi_layout_surface *));
1772                 if (*ppArray == NULL) {
1773                         weston_log("fails to allocate memory\n");
1774                         return IVI_FAILED;
1775                 }
1776
1777                 wl_list_for_each(ivisurf, &layout->surface_list, link) {
1778                         (*ppArray)[n++] = ivisurf;
1779                 }
1780         }
1781
1782         *pLength = length;
1783
1784         return IVI_SUCCEEDED;
1785 }
1786
1787 int32_t
1788 ivi_layout_get_surfaces_on_layer(struct ivi_layout_layer *ivilayer,
1789                                  int32_t *pLength,
1790                                  struct ivi_layout_surface ***ppArray)
1791 {
1792         struct ivi_layout_surface *ivisurf = NULL;
1793         int32_t length = 0;
1794         int32_t n = 0;
1795
1796         if (ivilayer == NULL || pLength == NULL || ppArray == NULL) {
1797                 weston_log("ivi_layout_getSurfaceIDsOnLayer: invalid argument\n");
1798                 return IVI_FAILED;
1799         }
1800
1801         length = wl_list_length(&ivilayer->order.surface_list);
1802
1803         if (length != 0) {
1804                 /* the Array must be free by module which called this function */
1805                 *ppArray = calloc(length, sizeof(struct ivi_layout_surface *));
1806                 if (*ppArray == NULL) {
1807                         weston_log("fails to allocate memory\n");
1808                         return IVI_FAILED;
1809                 }
1810
1811                 wl_list_for_each(ivisurf, &ivilayer->order.surface_list, order.link) {
1812                         (*ppArray)[n++] = ivisurf;
1813                 }
1814         }
1815
1816         *pLength = length;
1817
1818         return IVI_SUCCEEDED;
1819 }
1820
1821 WL_EXPORT struct ivi_layout_layer *
1822 ivi_layout_layer_create_with_dimension(uint32_t id_layer,
1823                                        int32_t width, int32_t height)
1824 {
1825         struct ivi_layout *layout = get_layout_instance();
1826         struct ivi_layout_layer *ivilayer = NULL;
1827
1828         ivilayer = get_layer(&layout->layer_list, id_layer);
1829         if (ivilayer != NULL) {
1830                 weston_log("id_layer is already created\n");
1831                 return ivilayer;
1832         }
1833
1834         ivilayer = calloc(1, sizeof *ivilayer);
1835         if (ivilayer == NULL) {
1836                 weston_log("fails to allocate memory\n");
1837                 return NULL;
1838         }
1839
1840         wl_list_init(&ivilayer->link);
1841         wl_signal_init(&ivilayer->property_changed);
1842         wl_list_init(&ivilayer->screen_list);
1843         wl_list_init(&ivilayer->link_to_surface);
1844         ivilayer->layout = layout;
1845         ivilayer->id_layer = id_layer;
1846
1847         init_layer_properties(&ivilayer->prop, width, height);
1848         ivilayer->event_mask = 0;
1849
1850         wl_list_init(&ivilayer->pending.surface_list);
1851         wl_list_init(&ivilayer->pending.link);
1852         ivilayer->pending.prop = ivilayer->prop;
1853
1854         wl_list_init(&ivilayer->order.surface_list);
1855         wl_list_init(&ivilayer->order.link);
1856
1857         wl_list_insert(&layout->layer_list, &ivilayer->link);
1858
1859         wl_signal_emit(&layout->layer_notification.created, ivilayer);
1860
1861         return ivilayer;
1862 }
1863
1864 WL_EXPORT void
1865 ivi_layout_layer_remove(struct ivi_layout_layer *ivilayer)
1866 {
1867         struct ivi_layout *layout = get_layout_instance();
1868
1869         if (ivilayer == NULL) {
1870                 weston_log("ivi_layout_layer_remove: invalid argument\n");
1871                 return;
1872         }
1873
1874         wl_signal_emit(&layout->layer_notification.removed, ivilayer);
1875
1876         clear_surface_pending_list(ivilayer);
1877         clear_surface_order_list(ivilayer);
1878
1879         if (!wl_list_empty(&ivilayer->pending.link)) {
1880                 wl_list_remove(&ivilayer->pending.link);
1881         }
1882         if (!wl_list_empty(&ivilayer->order.link)) {
1883                 wl_list_remove(&ivilayer->order.link);
1884         }
1885         if (!wl_list_empty(&ivilayer->link)) {
1886                 wl_list_remove(&ivilayer->link);
1887         }
1888         remove_orderlayer_from_screen(ivilayer);
1889         remove_link_to_surface(ivilayer);
1890         ivi_layout_layer_remove_notification(ivilayer);
1891
1892         free(ivilayer);
1893 }
1894
1895 WL_EXPORT int32_t
1896 ivi_layout_layer_set_visibility(struct ivi_layout_layer *ivilayer,
1897                                 bool newVisibility)
1898 {
1899         struct ivi_layout_layer_properties *prop = NULL;
1900
1901         if (ivilayer == NULL) {
1902                 weston_log("ivi_layout_layer_set_visibility: invalid argument\n");
1903                 return IVI_FAILED;
1904         }
1905
1906         prop = &ivilayer->pending.prop;
1907         prop->visibility = newVisibility;
1908
1909         ivilayer->event_mask |= IVI_NOTIFICATION_VISIBILITY;
1910
1911         return IVI_SUCCEEDED;
1912 }
1913
1914 bool
1915 ivi_layout_layer_get_visibility(struct ivi_layout_layer *ivilayer)
1916 {
1917         if (ivilayer == NULL) {
1918                 weston_log("ivi_layout_layer_get_visibility: invalid argument\n");
1919                 return false;
1920         }
1921
1922         return ivilayer->prop.visibility;
1923 }
1924
1925 WL_EXPORT int32_t
1926 ivi_layout_layer_set_opacity(struct ivi_layout_layer *ivilayer,
1927                              wl_fixed_t opacity)
1928 {
1929         struct ivi_layout_layer_properties *prop = NULL;
1930
1931         if (ivilayer == NULL) {
1932                 weston_log("ivi_layout_layer_set_opacity: invalid argument\n");
1933                 return IVI_FAILED;
1934         }
1935
1936         prop = &ivilayer->pending.prop;
1937         prop->opacity = opacity;
1938
1939         ivilayer->event_mask |= IVI_NOTIFICATION_OPACITY;
1940
1941         return IVI_SUCCEEDED;
1942 }
1943
1944 WL_EXPORT wl_fixed_t
1945 ivi_layout_layer_get_opacity(struct ivi_layout_layer *ivilayer)
1946 {
1947         if (ivilayer == NULL) {
1948                 weston_log("ivi_layout_layer_get_opacity: invalid argument\n");
1949                 return wl_fixed_from_double(0.0);
1950         }
1951
1952         return ivilayer->prop.opacity;
1953 }
1954
1955 WL_EXPORT int32_t
1956 ivi_layout_layer_set_source_rectangle(struct ivi_layout_layer *ivilayer,
1957                                       int32_t x, int32_t y,
1958                                       int32_t width, int32_t height)
1959 {
1960         struct ivi_layout_layer_properties *prop = NULL;
1961
1962         if (ivilayer == NULL) {
1963                 weston_log("ivi_layout_layer_set_source_rectangle: invalid argument\n");
1964                 return IVI_FAILED;
1965         }
1966
1967         prop = &ivilayer->pending.prop;
1968         prop->source_x = x;
1969         prop->source_y = y;
1970         prop->source_width = width;
1971         prop->source_height = height;
1972
1973         ivilayer->event_mask |= IVI_NOTIFICATION_SOURCE_RECT;
1974
1975         return IVI_SUCCEEDED;
1976 }
1977
1978 WL_EXPORT int32_t
1979 ivi_layout_layer_set_destination_rectangle(struct ivi_layout_layer *ivilayer,
1980                                            int32_t x, int32_t y,
1981                                            int32_t width, int32_t height)
1982 {
1983         struct ivi_layout_layer_properties *prop = NULL;
1984
1985         if (ivilayer == NULL) {
1986                 weston_log("ivi_layout_layer_set_destination_rectangle: invalid argument\n");
1987                 return IVI_FAILED;
1988         }
1989
1990         prop = &ivilayer->pending.prop;
1991         prop->dest_x = x;
1992         prop->dest_y = y;
1993         prop->dest_width = width;
1994         prop->dest_height = height;
1995
1996         ivilayer->event_mask |= IVI_NOTIFICATION_DEST_RECT;
1997
1998         return IVI_SUCCEEDED;
1999 }
2000
2001 int32_t
2002 ivi_layout_layer_get_dimension(struct ivi_layout_layer *ivilayer,
2003                                int32_t *dest_width, int32_t *dest_height)
2004 {
2005         if (ivilayer == NULL || dest_width == NULL || dest_height == NULL) {
2006                 weston_log("ivi_layout_layer_get_dimension: invalid argument\n");
2007                 return IVI_FAILED;
2008         }
2009
2010         *dest_width = ivilayer->prop.dest_width;
2011         *dest_height = ivilayer->prop.dest_height;
2012
2013         return IVI_SUCCEEDED;
2014 }
2015
2016 int32_t
2017 ivi_layout_layer_set_dimension(struct ivi_layout_layer *ivilayer,
2018                                int32_t dest_width, int32_t dest_height)
2019 {
2020         struct ivi_layout_layer_properties *prop = NULL;
2021
2022         if (ivilayer == NULL) {
2023                 weston_log("ivi_layout_layer_set_dimension: invalid argument\n");
2024                 return IVI_FAILED;
2025         }
2026
2027         prop = &ivilayer->pending.prop;
2028
2029         prop->dest_width  = dest_width;
2030         prop->dest_height = dest_height;
2031
2032         ivilayer->event_mask |= IVI_NOTIFICATION_DIMENSION;
2033
2034         return IVI_SUCCEEDED;
2035 }
2036
2037 WL_EXPORT int32_t
2038 ivi_layout_layer_get_position(struct ivi_layout_layer *ivilayer,
2039                               int32_t *dest_x, int32_t *dest_y)
2040 {
2041         if (ivilayer == NULL || dest_x == NULL || dest_y == NULL) {
2042                 weston_log("ivi_layout_layer_get_position: invalid argument\n");
2043                 return IVI_FAILED;
2044         }
2045
2046         *dest_x = ivilayer->prop.dest_x;
2047         *dest_y = ivilayer->prop.dest_y;
2048
2049         return IVI_SUCCEEDED;
2050 }
2051
2052 WL_EXPORT int32_t
2053 ivi_layout_layer_set_position(struct ivi_layout_layer *ivilayer,
2054                               int32_t dest_x, int32_t dest_y)
2055 {
2056         struct ivi_layout_layer_properties *prop = NULL;
2057
2058         if (ivilayer == NULL) {
2059                 weston_log("ivi_layout_layer_set_position: invalid argument\n");
2060                 return IVI_FAILED;
2061         }
2062
2063         prop = &ivilayer->pending.prop;
2064         prop->dest_x = dest_x;
2065         prop->dest_y = dest_y;
2066
2067         ivilayer->event_mask |= IVI_NOTIFICATION_POSITION;
2068
2069         return IVI_SUCCEEDED;
2070 }
2071
2072 WL_EXPORT int32_t
2073 ivi_layout_layer_set_orientation(struct ivi_layout_layer *ivilayer,
2074                                  enum wl_output_transform orientation)
2075 {
2076         struct ivi_layout_layer_properties *prop = NULL;
2077
2078         if (ivilayer == NULL) {
2079                 weston_log("ivi_layout_layer_set_orientation: invalid argument\n");
2080                 return IVI_FAILED;
2081         }
2082
2083         prop = &ivilayer->pending.prop;
2084         prop->orientation = orientation;
2085
2086         ivilayer->event_mask |= IVI_NOTIFICATION_ORIENTATION;
2087
2088         return IVI_SUCCEEDED;
2089 }
2090
2091 enum wl_output_transform
2092 ivi_layout_layer_get_orientation(struct ivi_layout_layer *ivilayer)
2093 {
2094         if (ivilayer == NULL) {
2095                 weston_log("ivi_layout_layer_get_orientation: invalid argument\n");
2096                 return 0;
2097         }
2098
2099         return ivilayer->prop.orientation;
2100 }
2101
2102 WL_EXPORT int32_t
2103 ivi_layout_layer_set_render_order(struct ivi_layout_layer *ivilayer,
2104                                   struct ivi_layout_surface **pSurface,
2105                                   int32_t number)
2106 {
2107         struct ivi_layout *layout = get_layout_instance();
2108         struct ivi_layout_surface *ivisurf = NULL;
2109         struct ivi_layout_surface *next = NULL;
2110         uint32_t *id_surface = NULL;
2111         int32_t i = 0;
2112
2113         if (ivilayer == NULL) {
2114                 weston_log("ivi_layout_layer_set_render_order: invalid argument\n");
2115                 return IVI_FAILED;
2116         }
2117
2118         if (pSurface == NULL) {
2119                 wl_list_for_each_safe(ivisurf, next, &ivilayer->pending.surface_list, pending.link) {
2120                         if (!wl_list_empty(&ivisurf->pending.link)) {
2121                                 wl_list_remove(&ivisurf->pending.link);
2122                         }
2123
2124                         wl_list_init(&ivisurf->pending.link);
2125                 }
2126                 ivilayer->event_mask |= IVI_NOTIFICATION_REMOVE;
2127                 return IVI_SUCCEEDED;
2128         }
2129
2130         for (i = 0; i < number; i++) {
2131                 id_surface = &pSurface[i]->id_surface;
2132
2133                 wl_list_for_each_safe(ivisurf, next, &layout->surface_list, link) {
2134                         if (*id_surface != ivisurf->id_surface) {
2135                                 continue;
2136                         }
2137
2138                         if (!wl_list_empty(&ivisurf->pending.link)) {
2139                                 wl_list_remove(&ivisurf->pending.link);
2140                         }
2141                         wl_list_init(&ivisurf->pending.link);
2142                         wl_list_insert(&ivilayer->pending.surface_list,
2143                                        &ivisurf->pending.link);
2144                         break;
2145                 }
2146         }
2147
2148         ivilayer->event_mask |= IVI_NOTIFICATION_ADD;
2149
2150         return IVI_SUCCEEDED;
2151 }
2152
2153 WL_EXPORT int32_t
2154 ivi_layout_surface_set_visibility(struct ivi_layout_surface *ivisurf,
2155                                   bool newVisibility)
2156 {
2157         struct ivi_layout_surface_properties *prop = NULL;
2158
2159         if (ivisurf == NULL) {
2160                 weston_log("ivi_layout_surface_set_visibility: invalid argument\n");
2161                 return IVI_FAILED;
2162         }
2163
2164         prop = &ivisurf->pending.prop;
2165         prop->visibility = newVisibility;
2166
2167         ivisurf->event_mask |= IVI_NOTIFICATION_VISIBILITY;
2168
2169         return IVI_SUCCEEDED;
2170 }
2171
2172 WL_EXPORT bool
2173 ivi_layout_surface_get_visibility(struct ivi_layout_surface *ivisurf)
2174 {
2175         if (ivisurf == NULL) {
2176                 weston_log("ivi_layout_surface_get_visibility: invalid argument\n");
2177                 return false;
2178         }
2179
2180         return ivisurf->prop.visibility;
2181 }
2182
2183 WL_EXPORT int32_t
2184 ivi_layout_surface_set_opacity(struct ivi_layout_surface *ivisurf,
2185                                wl_fixed_t opacity)
2186 {
2187         struct ivi_layout_surface_properties *prop = NULL;
2188
2189         if (ivisurf == NULL) {
2190                 weston_log("ivi_layout_surface_set_opacity: invalid argument\n");
2191                 return IVI_FAILED;
2192         }
2193
2194         prop = &ivisurf->pending.prop;
2195         prop->opacity = opacity;
2196
2197         ivisurf->event_mask |= IVI_NOTIFICATION_OPACITY;
2198
2199         return IVI_SUCCEEDED;
2200 }
2201
2202 WL_EXPORT wl_fixed_t
2203 ivi_layout_surface_get_opacity(struct ivi_layout_surface *ivisurf)
2204 {
2205         if (ivisurf == NULL) {
2206                 weston_log("ivi_layout_surface_get_opacity: invalid argument\n");
2207                 return wl_fixed_from_double(0.0);
2208         }
2209
2210         return ivisurf->prop.opacity;
2211 }
2212
2213 WL_EXPORT int32_t
2214 ivi_layout_surface_set_destination_rectangle(struct ivi_layout_surface *ivisurf,
2215                                              int32_t x, int32_t y,
2216                                              int32_t width, int32_t height)
2217 {
2218         struct ivi_layout_surface_properties *prop = NULL;
2219
2220         if (ivisurf == NULL) {
2221                 weston_log("ivi_layout_surface_set_destination_rectangle: invalid argument\n");
2222                 return IVI_FAILED;
2223         }
2224
2225         prop = &ivisurf->pending.prop;
2226         prop->start_x = prop->dest_x;
2227         prop->start_y = prop->dest_y;
2228         prop->dest_x = x;
2229         prop->dest_y = y;
2230         prop->start_width = prop->dest_width;
2231         prop->start_height = prop->dest_height;
2232         prop->dest_width = width;
2233         prop->dest_height = height;
2234
2235         ivisurf->event_mask |= IVI_NOTIFICATION_DEST_RECT;
2236
2237         return IVI_SUCCEEDED;
2238 }
2239
2240 int32_t
2241 ivi_layout_surface_set_dimension(struct ivi_layout_surface *ivisurf,
2242                                  int32_t dest_width, int32_t dest_height)
2243 {
2244         struct ivi_layout_surface_properties *prop = NULL;
2245
2246         if (ivisurf == NULL) {
2247                 weston_log("ivi_layout_surface_set_dimension: invalid argument\n");
2248                 return IVI_FAILED;
2249         }
2250
2251         prop = &ivisurf->pending.prop;
2252         prop->dest_width  = dest_width;
2253         prop->dest_height = dest_height;
2254
2255         ivisurf->event_mask |= IVI_NOTIFICATION_DIMENSION;
2256
2257         return IVI_SUCCEEDED;
2258 }
2259
2260 int32_t
2261 ivi_layout_surface_get_dimension(struct ivi_layout_surface *ivisurf,
2262                                  int32_t *dest_width, int32_t *dest_height)
2263 {
2264         if (ivisurf == NULL || dest_width == NULL ||  dest_height == NULL) {
2265                 weston_log("ivi_layout_surface_get_dimension: invalid argument\n");
2266                 return IVI_FAILED;
2267         }
2268
2269         *dest_width = ivisurf->prop.dest_width;
2270         *dest_height = ivisurf->prop.dest_height;
2271
2272         return IVI_SUCCEEDED;
2273 }
2274
2275 int32_t
2276 ivi_layout_surface_set_position(struct ivi_layout_surface *ivisurf,
2277                                 int32_t dest_x, int32_t dest_y)
2278 {
2279         struct ivi_layout_surface_properties *prop = NULL;
2280
2281         if (ivisurf == NULL) {
2282                 weston_log("ivi_layout_surface_set_position: invalid argument\n");
2283                 return IVI_FAILED;
2284         }
2285
2286         prop = &ivisurf->pending.prop;
2287         prop->dest_x = dest_x;
2288         prop->dest_y = dest_y;
2289
2290         ivisurf->event_mask |= IVI_NOTIFICATION_POSITION;
2291
2292         return IVI_SUCCEEDED;
2293 }
2294
2295 int32_t
2296 ivi_layout_surface_get_position(struct ivi_layout_surface *ivisurf,
2297                                 int32_t *dest_x, int32_t *dest_y)
2298 {
2299         if (ivisurf == NULL || dest_x == NULL || dest_y == NULL) {
2300                 weston_log("ivi_layout_surface_get_position: invalid argument\n");
2301                 return IVI_FAILED;
2302         }
2303
2304         *dest_x = ivisurf->prop.dest_x;
2305         *dest_y = ivisurf->prop.dest_y;
2306
2307         return IVI_SUCCEEDED;
2308 }
2309
2310 WL_EXPORT int32_t
2311 ivi_layout_surface_set_orientation(struct ivi_layout_surface *ivisurf,
2312                                    enum wl_output_transform orientation)
2313 {
2314         struct ivi_layout_surface_properties *prop = NULL;
2315
2316         if (ivisurf == NULL) {
2317                 weston_log("ivi_layout_surface_set_orientation: invalid argument\n");
2318                 return IVI_FAILED;
2319         }
2320
2321         prop = &ivisurf->pending.prop;
2322         prop->orientation = orientation;
2323
2324         ivisurf->event_mask |= IVI_NOTIFICATION_ORIENTATION;
2325
2326         return IVI_SUCCEEDED;
2327 }
2328
2329 enum wl_output_transform
2330 ivi_layout_surface_get_orientation(struct ivi_layout_surface *ivisurf)
2331 {
2332         if (ivisurf == NULL) {
2333                 weston_log("ivi_layout_surface_get_orientation: invalid argument\n");
2334                 return 0;
2335         }
2336
2337         return ivisurf->prop.orientation;
2338 }
2339
2340 WL_EXPORT int32_t
2341 ivi_layout_screen_add_layer(struct ivi_layout_screen *iviscrn,
2342                             struct ivi_layout_layer *addlayer)
2343 {
2344         struct ivi_layout *layout = get_layout_instance();
2345         struct ivi_layout_layer *ivilayer = NULL;
2346         struct ivi_layout_layer *next = NULL;
2347         int is_layer_in_scrn = 0;
2348
2349         if (iviscrn == NULL || addlayer == NULL) {
2350                 weston_log("ivi_layout_screen_add_layer: invalid argument\n");
2351                 return IVI_FAILED;
2352         }
2353
2354         is_layer_in_scrn = is_layer_in_screen(addlayer, iviscrn);
2355         if (is_layer_in_scrn == 1) {
2356                 weston_log("ivi_layout_screen_add_layer: addlayer is already available\n");
2357                 return IVI_SUCCEEDED;
2358         }
2359
2360         wl_list_for_each_safe(ivilayer, next, &layout->layer_list, link) {
2361                 if (ivilayer->id_layer == addlayer->id_layer) {
2362                         if (!wl_list_empty(&ivilayer->pending.link)) {
2363                                 wl_list_remove(&ivilayer->pending.link);
2364                         }
2365                         wl_list_init(&ivilayer->pending.link);
2366                         wl_list_insert(&iviscrn->pending.layer_list,
2367                                        &ivilayer->pending.link);
2368                         break;
2369                 }
2370         }
2371
2372         iviscrn->event_mask |= IVI_NOTIFICATION_ADD;
2373
2374         return IVI_SUCCEEDED;
2375 }
2376
2377 WL_EXPORT int32_t
2378 ivi_layout_screen_set_render_order(struct ivi_layout_screen *iviscrn,
2379                                    struct ivi_layout_layer **pLayer,
2380                                    const int32_t number)
2381 {
2382         struct ivi_layout *layout = get_layout_instance();
2383         struct ivi_layout_layer *ivilayer = NULL;
2384         struct ivi_layout_layer *next = NULL;
2385         uint32_t *id_layer = NULL;
2386         int32_t i = 0;
2387
2388         if (iviscrn == NULL) {
2389                 weston_log("ivi_layout_screen_set_render_order: invalid argument\n");
2390                 return IVI_FAILED;
2391         }
2392
2393         wl_list_for_each_safe(ivilayer, next,
2394                               &iviscrn->pending.layer_list, pending.link) {
2395                 wl_list_init(&ivilayer->pending.link);
2396         }
2397
2398         wl_list_init(&iviscrn->pending.layer_list);
2399
2400         if (pLayer == NULL) {
2401                 wl_list_for_each_safe(ivilayer, next, &iviscrn->pending.layer_list, pending.link) {
2402                         if (!wl_list_empty(&ivilayer->pending.link)) {
2403                                 wl_list_remove(&ivilayer->pending.link);
2404                         }
2405
2406                         wl_list_init(&ivilayer->pending.link);
2407                 }
2408
2409                 iviscrn->event_mask |= IVI_NOTIFICATION_REMOVE;
2410                 return IVI_SUCCEEDED;
2411         }
2412
2413         for (i = 0; i < number; i++) {
2414                 id_layer = &pLayer[i]->id_layer;
2415                 wl_list_for_each(ivilayer, &layout->layer_list, link) {
2416                         if (*id_layer != ivilayer->id_layer) {
2417                                 continue;
2418                         }
2419
2420                         if (!wl_list_empty(&ivilayer->pending.link)) {
2421                                 wl_list_remove(&ivilayer->pending.link);
2422                         }
2423                         wl_list_init(&ivilayer->pending.link);
2424                         wl_list_insert(&iviscrn->pending.layer_list,
2425                                        &ivilayer->pending.link);
2426                         break;
2427                 }
2428         }
2429
2430         iviscrn->event_mask |= IVI_NOTIFICATION_ADD;
2431
2432         return IVI_SUCCEEDED;
2433 }
2434
2435 WL_EXPORT struct weston_output *
2436 ivi_layout_screen_get_output(struct ivi_layout_screen *iviscrn)
2437 {
2438         return iviscrn->output;
2439 }
2440
2441 /**
2442  * This function is used by the additional ivi-module because of dumping ivi_surface sceenshot.
2443  * The ivi-module, e.g. ivi-controller.so, is in wayland-ivi-extension of Genivi's Layer Management.
2444  * This function is used to get the result of drawing by clients.
2445  */
2446 WL_EXPORT struct weston_surface *
2447 ivi_layout_surface_get_weston_surface(struct ivi_layout_surface *ivisurf)
2448 {
2449         return ivisurf != NULL ? ivisurf->surface : NULL;
2450 }
2451
2452 /**
2453  * This function is used by the additional ivi-module because of dumping ivi_surface sceenshot.
2454  * The ivi-module, e.g. ivi-controller.so, is in wayland-ivi-extension of GENIVI's Layer Management.
2455  * This function is used to get the region and the stride.
2456  */
2457 WL_EXPORT int32_t
2458 ivi_layout_surface_get_size(struct ivi_layout_surface *ivisurf,
2459                             int32_t *width,
2460                             int32_t *height,
2461                             int32_t *stride)
2462 {
2463         if (ivisurf == NULL) {
2464                 return IVI_FAILED;
2465         }
2466
2467         if (width != NULL) {
2468                 *width = ivisurf->prop.source_width;
2469         }
2470
2471         if (height != NULL) {
2472                 *height = ivisurf->prop.source_height;
2473         }
2474
2475         if (stride != NULL &&
2476                 ivisurf->surface->buffer_ref.buffer != NULL &&
2477                 ivisurf->surface->buffer_ref.buffer->shm_buffer != NULL) {
2478                 *stride = wl_shm_buffer_get_stride(ivisurf->surface->buffer_ref.buffer->shm_buffer);
2479         }
2480
2481         return IVI_SUCCEEDED;
2482 }
2483
2484 WL_EXPORT int32_t
2485 ivi_layout_layer_add_notification(struct ivi_layout_layer *ivilayer,
2486                                   layer_property_notification_func callback,
2487                                   void *userdata)
2488 {
2489         struct ivi_layout_notification_callback *prop_callback = NULL;
2490
2491         if (ivilayer == NULL || callback == NULL) {
2492                 weston_log("ivi_layout_layer_add_notification: invalid argument\n");
2493                 return IVI_FAILED;
2494         }
2495
2496         prop_callback = malloc(sizeof *prop_callback);
2497         if (prop_callback == NULL) {
2498                 weston_log("fails to allocate memory\n");
2499                 return IVI_FAILED;
2500         }
2501
2502         prop_callback->callback = callback;
2503         prop_callback->data = userdata;
2504
2505         return add_notification(&ivilayer->property_changed,
2506                                 layer_prop_changed,
2507                                 prop_callback);
2508 }
2509
2510 WL_EXPORT void
2511 ivi_layout_layer_remove_notification(struct ivi_layout_layer *ivilayer)
2512 {
2513         if (ivilayer == NULL) {
2514                 weston_log("ivi_layout_layer_remove_notification: invalid argument\n");
2515                 return;
2516         }
2517
2518         remove_all_notification(&ivilayer->property_changed.listener_list);
2519 }
2520
2521 WL_EXPORT const struct ivi_layout_surface_properties *
2522 ivi_layout_get_properties_of_surface(struct ivi_layout_surface *ivisurf)
2523 {
2524         if (ivisurf == NULL) {
2525                 weston_log("ivi_layout_get_properties_of_surface: invalid argument\n");
2526                 return NULL;
2527         }
2528
2529         return &ivisurf->prop;
2530 }
2531
2532 WL_EXPORT int32_t
2533 ivi_layout_layer_add_surface(struct ivi_layout_layer *ivilayer,
2534                              struct ivi_layout_surface *addsurf)
2535 {
2536         struct ivi_layout *layout = get_layout_instance();
2537         struct ivi_layout_surface *ivisurf = NULL;
2538         struct ivi_layout_surface *next = NULL;
2539         int is_surf_in_layer = 0;
2540
2541         if (ivilayer == NULL || addsurf == NULL) {
2542                 weston_log("ivi_layout_layer_add_surface: invalid argument\n");
2543                 return IVI_FAILED;
2544         }
2545
2546         is_surf_in_layer = is_surface_in_layer(addsurf, ivilayer);
2547         if (is_surf_in_layer == 1) {
2548                 weston_log("ivi_layout_layer_add_surface: addsurf is already available\n");
2549                 return IVI_SUCCEEDED;
2550         }
2551
2552         wl_list_for_each_safe(ivisurf, next, &layout->surface_list, link) {
2553                 if (ivisurf->id_surface == addsurf->id_surface) {
2554                         if (!wl_list_empty(&ivisurf->pending.link)) {
2555                                 wl_list_remove(&ivisurf->pending.link);
2556                         }
2557                         wl_list_init(&ivisurf->pending.link);
2558                         wl_list_insert(&ivilayer->pending.surface_list,
2559                                        &ivisurf->pending.link);
2560                         break;
2561                 }
2562         }
2563
2564         ivilayer->event_mask |= IVI_NOTIFICATION_ADD;
2565
2566         return IVI_SUCCEEDED;
2567 }
2568
2569 WL_EXPORT void
2570 ivi_layout_layer_remove_surface(struct ivi_layout_layer *ivilayer,
2571                                 struct ivi_layout_surface *remsurf)
2572 {
2573         struct ivi_layout_surface *ivisurf = NULL;
2574         struct ivi_layout_surface *next = NULL;
2575
2576         if (ivilayer == NULL || remsurf == NULL) {
2577                 weston_log("ivi_layout_layer_remove_surface: invalid argument\n");
2578                 return;
2579         }
2580
2581         wl_list_for_each_safe(ivisurf, next,
2582                               &ivilayer->pending.surface_list, pending.link) {
2583                 if (ivisurf->id_surface == remsurf->id_surface) {
2584                         if (!wl_list_empty(&ivisurf->pending.link)) {
2585                                 wl_list_remove(&ivisurf->pending.link);
2586                         }
2587                         wl_list_init(&ivisurf->pending.link);
2588                         break;
2589                 }
2590         }
2591
2592         remsurf->event_mask |= IVI_NOTIFICATION_REMOVE;
2593 }
2594
2595 WL_EXPORT int32_t
2596 ivi_layout_surface_set_source_rectangle(struct ivi_layout_surface *ivisurf,
2597                                         int32_t x, int32_t y,
2598                                         int32_t width, int32_t height)
2599 {
2600         struct ivi_layout_surface_properties *prop = NULL;
2601
2602         if (ivisurf == NULL) {
2603                 weston_log("ivi_layout_surface_set_source_rectangle: invalid argument\n");
2604                 return IVI_FAILED;
2605         }
2606
2607         prop = &ivisurf->pending.prop;
2608         prop->source_x = x;
2609         prop->source_y = y;
2610         prop->source_width = width;
2611         prop->source_height = height;
2612
2613         ivisurf->event_mask |= IVI_NOTIFICATION_SOURCE_RECT;
2614
2615         return IVI_SUCCEEDED;
2616 }
2617
2618 WL_EXPORT int32_t
2619 ivi_layout_commit_changes(void)
2620 {
2621         struct ivi_layout *layout = get_layout_instance();
2622
2623         commit_surface_list(layout);
2624         commit_layer_list(layout);
2625         commit_screen_list(layout);
2626
2627         commit_transition(layout);
2628
2629         commit_changes(layout);
2630         send_prop(layout);
2631         weston_compositor_schedule_repaint(layout->compositor);
2632
2633         return IVI_SUCCEEDED;
2634 }
2635
2636 /***called from ivi-shell**/
2637 static struct weston_surface *
2638 ivi_layout_get_weston_surface(struct ivi_layout_surface *surface)
2639 {
2640         return (surface != NULL) ? surface->surface : NULL;
2641 }
2642
2643 static struct weston_view *
2644 ivi_layout_get_weston_view(struct ivi_layout_surface *surface)
2645 {
2646         struct weston_view *tmpview = NULL;
2647
2648         if(surface == NULL)
2649                 return NULL;
2650
2651         wl_list_for_each(tmpview, &surface->surface->views, surface_link)
2652         {
2653                 if (tmpview != NULL) {
2654                         break;
2655                 }
2656         }
2657         return tmpview;
2658 }
2659
2660 static void
2661 ivi_layout_surface_configure(struct ivi_layout_surface *ivisurf,
2662                              int32_t width, int32_t height)
2663 {
2664         struct ivi_layout *layout = get_layout_instance();
2665         int32_t in_init = 0;
2666
2667         ivisurf->surface->width_from_buffer  = width;
2668         ivisurf->surface->height_from_buffer = height;
2669
2670         if (ivisurf->prop.source_width == 0 || ivisurf->prop.source_height == 0) {
2671                 in_init = 1;
2672         }
2673
2674         /* FIXME: when sourceHeight/Width is used as clipping range in image buffer */
2675         /* if (ivisurf->prop.sourceWidth == 0 || ivisurf->prop.sourceHeight == 0) { */
2676                 ivisurf->pending.prop.source_width = width;
2677                 ivisurf->pending.prop.source_height = height;
2678                 ivisurf->prop.source_width = width;
2679                 ivisurf->prop.source_height = height;
2680         /* } */
2681
2682         ivisurf->event_mask |= IVI_NOTIFICATION_CONFIGURE;
2683
2684         if (in_init) {
2685                 wl_signal_emit(&layout->surface_notification.configure_changed, ivisurf);
2686         } else {
2687                 ivi_layout_commit_changes();
2688         }
2689 }
2690
2691 WL_EXPORT int32_t
2692 ivi_layout_surface_set_content_observer(struct ivi_layout_surface *ivisurf,
2693                                         ivi_controller_surface_content_callback callback,
2694                                         void* userdata)
2695 {
2696         int32_t ret = IVI_FAILED;
2697
2698         if (ivisurf != NULL) {
2699                 ivisurf->content_observer.callback = callback;
2700                 ivisurf->content_observer.userdata = userdata;
2701                 ret = IVI_SUCCEEDED;
2702         }
2703         return ret;
2704 }
2705
2706 static struct ivi_layout_surface*
2707 ivi_layout_surface_create(struct weston_surface *wl_surface,
2708                           uint32_t id_surface)
2709 {
2710         struct ivi_layout *layout = get_layout_instance();
2711         struct ivi_layout_surface *ivisurf = NULL;
2712         struct weston_view *tmpview = NULL;
2713
2714         if (wl_surface == NULL) {
2715                 weston_log("ivi_layout_surface_create: invalid argument\n");
2716                 return NULL;
2717         }
2718
2719         ivisurf = get_surface(&layout->surface_list, id_surface);
2720         if (ivisurf != NULL) {
2721                 if (ivisurf->surface != NULL) {
2722                         weston_log("id_surface(%d) is already created\n", id_surface);
2723                         return NULL;
2724                 }
2725         }
2726
2727         ivisurf = calloc(1, sizeof *ivisurf);
2728         if (ivisurf == NULL) {
2729                 weston_log("fails to allocate memory\n");
2730                 return NULL;
2731         }
2732
2733         wl_list_init(&ivisurf->link);
2734         wl_signal_init(&ivisurf->property_changed);
2735         wl_signal_init(&ivisurf->configured);
2736         wl_list_init(&ivisurf->layer_list);
2737         ivisurf->id_surface = id_surface;
2738         ivisurf->layout = layout;
2739
2740         ivisurf->surface = wl_surface;
2741         ivisurf->surface_destroy_listener.notify =
2742                 westonsurface_destroy_from_ivisurface;
2743         wl_resource_add_destroy_listener(wl_surface->resource,
2744                                          &ivisurf->surface_destroy_listener);
2745
2746         tmpview = weston_view_create(wl_surface);
2747         if (tmpview == NULL) {
2748                 weston_log("fails to allocate memory\n");
2749         }
2750
2751         ivisurf->surface->width_from_buffer  = 0;
2752         ivisurf->surface->height_from_buffer = 0;
2753
2754         weston_matrix_init(&ivisurf->surface_rotation.matrix);
2755         weston_matrix_init(&ivisurf->layer_rotation.matrix);
2756         weston_matrix_init(&ivisurf->surface_pos.matrix);
2757         weston_matrix_init(&ivisurf->layer_pos.matrix);
2758         weston_matrix_init(&ivisurf->scaling.matrix);
2759
2760         wl_list_init(&ivisurf->surface_rotation.link);
2761         wl_list_init(&ivisurf->layer_rotation.link);
2762         wl_list_init(&ivisurf->surface_pos.link);
2763         wl_list_init(&ivisurf->layer_pos.link);
2764         wl_list_init(&ivisurf->scaling.link);
2765
2766         init_surface_properties(&ivisurf->prop);
2767         ivisurf->event_mask = 0;
2768
2769         ivisurf->pending.prop = ivisurf->prop;
2770         wl_list_init(&ivisurf->pending.link);
2771
2772         wl_list_init(&ivisurf->order.link);
2773         wl_list_init(&ivisurf->order.layer_list);
2774
2775         wl_list_insert(&layout->surface_list, &ivisurf->link);
2776
2777         wl_signal_emit(&layout->surface_notification.created, ivisurf);
2778
2779         return ivisurf;
2780 }
2781
2782 static void
2783 ivi_layout_init_with_compositor(struct weston_compositor *ec)
2784 {
2785         struct ivi_layout *layout = get_layout_instance();
2786
2787         layout->compositor = ec;
2788
2789         wl_list_init(&layout->surface_list);
2790         wl_list_init(&layout->layer_list);
2791         wl_list_init(&layout->screen_list);
2792
2793         wl_signal_init(&layout->layer_notification.created);
2794         wl_signal_init(&layout->layer_notification.removed);
2795
2796         wl_signal_init(&layout->surface_notification.created);
2797         wl_signal_init(&layout->surface_notification.removed);
2798         wl_signal_init(&layout->surface_notification.configure_changed);
2799
2800         /* Add layout_layer at the last of weston_compositor.layer_list */
2801         weston_layer_init(&layout->layout_layer, ec->layer_list.prev);
2802
2803         create_screen(ec);
2804
2805         layout->transitions = ivi_layout_transition_set_create(ec);
2806         wl_list_init(&layout->pending_transition_list);
2807 }
2808
2809
2810 static void
2811 ivi_layout_surface_add_configured_listener(struct ivi_layout_surface* ivisurf,
2812                                            struct wl_listener* listener)
2813 {
2814         wl_signal_add(&ivisurf->configured, listener);
2815 }
2816
2817
2818 WL_EXPORT struct ivi_layout_interface ivi_layout_interface = {
2819         .get_weston_surface = ivi_layout_get_weston_surface,
2820         .get_weston_view = ivi_layout_get_weston_view,
2821         .surface_configure = ivi_layout_surface_configure,
2822         .surface_create = ivi_layout_surface_create,
2823         .init_with_compositor = ivi_layout_init_with_compositor,
2824         .get_surface_dimension = ivi_layout_surface_get_dimension,
2825         .add_surface_configured_listener = ivi_layout_surface_add_configured_listener
2826 };