weston-ivi-shell: Renamed ivi-shell to ivi-controller
[profile/ivi/wayland-ivi-extension.git] / weston-ivi-shell / src / ivi-controller.c
1 /*
2  * Copyright (C) 2013 DENSO CORPORATION
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #include <sys/wait.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <linux/input.h>
29 #include <cairo.h>
30
31 #include "compositor.h"
32 #include "ivi-application-server-protocol.h"
33 #include "ivi-controller-server-protocol.h"
34
35 enum {
36     PROP_EVENT_OPACITY     = 0x00000001,
37     PROP_EVENT_SRC_RECT    = 0x00000002,
38     PROP_EVENT_DST_RECT    = 0x00000004,
39     PROP_EVENT_ORIENTATION = 0x00000008,
40     PROP_EVENT_VISIBILITY  = 0x00000010,
41     PROP_EVENT_PIXELFORMAT = 0x00000020,
42     PROP_EVENT_ADD         = 0x00000040,
43     PROP_EVENT_ALL         = 0x7FFFFFFF
44 };
45
46 struct ivishell;
47
48 struct ivi_properties {
49     wl_fixed_t opacity;
50     int32_t src_x;
51     int32_t src_y;
52     int32_t src_width;
53     int32_t src_height;
54     int32_t dest_x;
55     int32_t dest_y;
56     int32_t dest_width;
57     int32_t dest_height;
58     int32_t orientation;
59     uint32_t visibility;
60 };
61
62 struct ivilayer;
63 struct iviscreen;
64
65 struct link_layer {
66     struct ivilayer *layer;
67     struct wl_list link;
68 };
69
70 struct link_screen {
71     struct iviscreen *screen;
72     struct wl_list link;
73 };
74
75 struct ivisurface {
76     struct wl_list link;
77     struct wl_list list_layer;
78     struct wl_client *client;
79     uint32_t id_surface;
80     struct ivishell *shell;
81     uint32_t update_count;
82
83     struct weston_surface *surface;
84     struct wl_listener surface_destroy_listener;
85     struct weston_transform surface_rotation;
86     struct weston_transform layer_rotation;
87     struct weston_transform surface_pos;
88     struct weston_transform layer_pos;
89     struct weston_transform scaling;
90     struct ivi_properties prop;
91     int32_t pixelformat;
92     uint32_t event_mask;
93
94     struct {
95         struct ivi_properties prop;
96         struct wl_list link;
97     } pending;
98
99     struct {
100         struct wl_list link;
101         struct wl_list list_layer;
102     } order;
103 };
104
105 struct ivilayer {
106     struct wl_list link;
107     struct wl_list list_screen;
108     struct ivishell *shell;
109     struct weston_layer el;
110     uint32_t id_layer;
111
112     struct ivi_properties prop;
113     uint32_t event_mask;
114
115     struct {
116         struct wl_list list_surface;
117         struct wl_list link;
118         struct ivi_properties prop;
119     } pending;
120
121     struct {
122         struct wl_list list_surface;
123         struct wl_list link;
124     } order;
125 };
126
127 struct iviscreen {
128     struct wl_list link;
129     struct ivishell *shell;
130     struct wl_list list_resource;
131
132     uint32_t id_screen;
133     struct weston_output *output;
134     struct wl_list list_layer;
135     uint32_t event_mask;
136
137     struct {
138         struct wl_list list_layer;
139         struct wl_list link;
140     } pending;
141
142     struct {
143         struct wl_list list_layer;
144         struct wl_list link;
145     } order;
146 };
147
148 struct ivicontroller_surface {
149     struct wl_resource *resource;
150     uint32_t id;
151     uint32_t id_surface;
152     struct wl_client *client;
153     struct wl_list link;
154     struct ivishell *shell;
155 };
156
157 struct ivicontroller_layer {
158     struct wl_resource *resource;
159     uint32_t id;
160     uint32_t id_layer;
161     struct wl_client *client;
162     struct wl_list link;
163     struct ivishell *shell;
164 };
165
166 struct ivicontroller_screen {
167     struct wl_resource *resource;
168     uint32_t id;
169     uint32_t id_screen;
170     struct wl_client *client;
171     struct wl_list link;
172     struct ivishell *shell;
173 };
174
175 struct ivicontroller {
176     struct wl_resource *resource;
177     uint32_t id;
178     struct wl_client *client;
179     struct wl_list link;
180     struct ivishell *shell;
181 };
182
183 struct link_shell_weston_surface
184 {
185     struct wl_resource *resource;
186     struct wl_listener destroy_listener;
187     struct weston_surface *surface;
188     struct wl_list link;
189 };
190
191 struct ping_timer {
192     struct wl_event_source *source;
193     uint32_t serial;
194 };
195
196 struct shell_surface {
197     struct wl_resource *resource;
198
199     struct weston_surface *surface;
200     struct wl_listener surface_destroy_listener;
201
202     struct ivishell *shell;
203     struct ping_timer *ping_timer;
204
205     char *title, *class;
206     int32_t width;
207     int32_t height;
208     pid_t pid;
209     struct wl_list link;
210
211     const struct weston_shell_client *client;
212     struct weston_output *output;
213 };
214
215 struct ivishell {
216     struct wl_resource *resource;
217
218     struct wl_listener destroy_listener;
219
220     struct weston_compositor *compositor;
221
222     struct weston_surface *surface;
223
224     struct weston_process process;
225
226     struct weston_seat *seat;
227
228     struct wl_list list_surface;
229     struct wl_list list_layer;
230     struct wl_list list_screen;
231
232     struct wl_list list_weston_surface;
233
234     struct wl_list list_controller;
235     struct wl_list list_controller_surface;
236     struct wl_list list_controller_layer;
237     struct wl_list list_controller_screen;
238
239     struct {
240         struct weston_process process;
241         struct wl_client *client;
242         struct wl_resource *desktop_shell;
243
244         unsigned deathcount;
245         uint32_t deathstamp;
246     } child;
247
248     int state;
249     int previous_state;
250     int event_restriction;
251 };
252
253 static const
254 struct ivi_surface_interface surface_implementation;
255
256 static void
257 add_ordersurface_to_layer(struct ivisurface *ivisurf,
258                           struct ivilayer *ivilayer)
259 {
260     struct link_layer *link_layer = NULL;
261
262     link_layer = malloc(sizeof *link_layer);
263     if (link_layer == NULL) {
264         weston_log("memory insufficient for link_layer\n");
265         return;
266     }
267
268     link_layer->layer = ivilayer;
269     wl_list_init(&link_layer->link);
270     wl_list_insert(&ivisurf->list_layer, &link_layer->link);
271 }
272
273 static void
274 remove_ordersurface_from_layer(struct ivisurface *ivisurf)
275 {
276     struct link_layer *link_layer = NULL;
277     struct link_layer *next = NULL;
278
279     wl_list_for_each_safe(link_layer, next, &ivisurf->list_layer, link) {
280         if (!wl_list_empty(&link_layer->link)) {
281             wl_list_remove(&link_layer->link);
282         }
283         free(link_layer);
284     }
285     wl_list_init(&ivisurf->list_layer);
286 }
287
288 static void
289 add_orderlayer_to_screen(struct ivilayer *ivilayer,
290                          struct iviscreen *iviscreen)
291 {
292     struct link_screen *link_scrn = NULL;
293
294     link_scrn = malloc(sizeof *link_scrn);
295     if (link_scrn == NULL) {
296         weston_log("memory insufficient for link_screen\n");
297         return;
298     }
299
300     link_scrn->screen = iviscreen;
301     wl_list_init(&link_scrn->link);
302     wl_list_insert(&ivilayer->list_screen, &link_scrn->link);
303 }
304
305 static void
306 remove_orderlayer_from_screen(struct ivilayer *ivilayer)
307 {
308     struct link_screen *link_scrn = NULL;
309     struct link_screen *next = NULL;
310
311     wl_list_for_each_safe(link_scrn, next, &ivilayer->list_screen, link) {
312         if (!wl_list_empty(&link_scrn->link)) {
313             wl_list_remove(&link_scrn->link);
314         }
315         free(link_scrn);
316     }
317     wl_list_init(&ivilayer->list_screen);
318 }
319
320 static void
321 destroy_ivicontroller_surface(struct wl_resource *resource)
322 {
323     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
324     struct ivishell *shell = ivisurf->shell;
325
326     struct ivicontroller_surface *ctrlsurf = NULL;
327     struct ivicontroller_surface *next = NULL;
328
329     wl_list_for_each_safe(ctrlsurf, next,
330                           &shell->list_controller_surface, link) {
331         if (ivisurf->id_surface != ctrlsurf->id_surface) {
332             continue;
333         }
334         wl_list_remove(&ctrlsurf->link);
335         free(ctrlsurf);
336         break;
337     }
338 }
339
340 static void
341 destroy_ivicontroller_layer(struct wl_resource *resource)
342 {
343     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
344     struct ivishell *shell = ivilayer->shell;
345     struct ivicontroller_layer *ctrllayer = NULL;
346     struct ivicontroller_layer *next = NULL;
347
348     wl_list_for_each_safe(ctrllayer, next,
349                           &shell->list_controller_layer, link) {
350         if (ivilayer->id_layer != ctrllayer->id_layer) {
351             continue;
352         }
353         wl_list_remove(&ctrllayer->link);
354         free(ctrllayer);
355         break;
356     }
357 }
358
359 static void
360 destroy_ivicontroller_screen(struct wl_resource *resource)
361 {
362     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
363     struct ivicontroller_screen *ctrlscrn = NULL;
364     struct ivicontroller_screen *next = NULL;
365
366     wl_list_for_each_safe(ctrlscrn, next,
367                           &iviscrn->shell->list_controller_screen, link) {
368         if (iviscrn->id_screen != ctrlscrn->id_screen) {
369             continue;
370         }
371         wl_list_remove(&ctrlscrn->link);
372         free(ctrlscrn);
373         break;
374     }
375 }
376
377 static void
378 unbind_resource_controller(struct wl_resource *resource)
379 {
380     struct ivicontroller *controller = wl_resource_get_user_data(resource);
381
382     wl_list_remove(&controller->link);
383
384     free(controller);
385 }
386
387 static struct ivisurface*
388 get_surface(struct wl_list *list_surf, uint32_t id_surface)
389 {
390     struct ivisurface *ivisurf;
391     wl_list_for_each(ivisurf, list_surf, link) {
392         if (ivisurf->id_surface == id_surface) {
393             return ivisurf;
394         }
395     }
396
397     return NULL;
398 }
399
400 static struct ivilayer*
401 get_layer(struct wl_list *list_layer, uint32_t id_layer)
402 {
403     struct ivilayer *ivilayer;
404     wl_list_for_each(ivilayer, list_layer, link) {
405         if (ivilayer->id_layer == id_layer) {
406             return ivilayer;
407         }
408     }
409
410     return NULL;
411 }
412
413 static void
414 init_properties(struct ivi_properties *prop)
415 {
416     memset(prop, 0, sizeof *prop);
417     prop->opacity = wl_fixed_from_double(1.0);
418 }
419
420 static const
421 struct ivi_controller_screen_interface controller_screen_implementation;
422
423 static struct ivicontroller_screen*
424 controller_screen_create(struct ivishell *shell,
425                         struct wl_client *client,
426                         struct iviscreen *iviscrn)
427 {
428     struct ivicontroller_screen *ctrlscrn;
429
430     ctrlscrn = calloc(1, sizeof *ctrlscrn);
431     if (ctrlscrn == NULL) {
432         weston_log("no memory to allocate controller screen\n");
433         return NULL;
434     }
435
436     ctrlscrn->client = client;
437     ctrlscrn->shell = shell;
438     ctrlscrn->id_screen = iviscrn->id_screen;
439
440     ctrlscrn->resource =
441         wl_resource_create(client, &ivi_controller_screen_interface, 1, 0);
442     if (ctrlscrn->resource == NULL) {
443         weston_log("couldn't new screen controller object");
444         free(ctrlscrn);
445         return NULL;
446     }
447
448     wl_resource_set_implementation(ctrlscrn->resource,
449                                    &controller_screen_implementation,
450                                    iviscrn, destroy_ivicontroller_screen);
451
452     wl_list_init(&ctrlscrn->link);
453     wl_list_insert(&shell->list_controller_screen, &ctrlscrn->link);
454
455     return ctrlscrn;
456 }
457
458 static void
459 update_opacity(struct ivilayer *ivilayer,
460                struct ivisurface *ivisurf)
461 {
462     double layer_alpha = wl_fixed_to_double(ivilayer->prop.opacity);
463     double surf_alpha = wl_fixed_to_double(ivisurf->prop.opacity);
464
465     if ((ivilayer->event_mask & PROP_EVENT_OPACITY) ||
466         (ivisurf->event_mask & PROP_EVENT_OPACITY)) {
467         if (ivisurf->surface == NULL) {
468             return;
469         }
470         ivisurf->surface->alpha = layer_alpha * surf_alpha;
471     }
472 }
473
474 static void
475 shell_surface_configure(struct weston_surface *,
476                         int32_t, int32_t, int32_t, int32_t);
477
478 static struct shell_surface *
479 get_shell_surface(struct weston_surface *surface)
480 {
481     if (surface->configure == shell_surface_configure) {
482         return surface->configure_private;
483     } else {
484         return NULL;
485     }
486 }
487
488 static void
489 update_surface_orientation(struct ivilayer *ivilayer,
490                            struct ivisurface *ivisurf)
491 {
492     float cx = 0.0f;
493     float cy = 0.0f;
494     struct weston_surface *es = ivisurf->surface;
495     float v_sin = 0.0f;
496     float v_cos = 0.0f;
497     float sx = 1.0f;
498     float sy = 1.0f;
499     float width = 0.0f;
500     float height = 0.0f;
501     struct weston_matrix *matrix = &ivisurf->surface_rotation.matrix;
502     struct shell_surface *shsurf = NULL;
503
504     if (es == NULL) {
505         return;
506     }
507
508     shsurf = get_shell_surface(es);
509     if (shsurf == NULL) {
510         return;
511     }
512
513     if ((shsurf->width == 0) || (shsurf->height == 0)) {
514         return;
515     }
516
517     if ((ivilayer->prop.dest_width == 0) ||
518         (ivilayer->prop.dest_height == 0)) {
519         return;
520     }
521     width = (float)ivilayer->prop.dest_width;
522     height = (float)ivilayer->prop.dest_height;
523
524     switch (ivisurf->prop.orientation) {
525     case IVI_CONTROLLER_SURFACE_ORIENTATION_0_DEGREES:
526         v_sin = 0.0f;
527         v_cos = 1.0f;
528         break;
529     case IVI_CONTROLLER_SURFACE_ORIENTATION_90_DEGREES:
530         v_sin = 1.0f;
531         v_cos = 0.0f;
532         sx = width / height;
533         sy = height / width;
534         break;
535     case IVI_CONTROLLER_SURFACE_ORIENTATION_180_DEGREES:
536         v_sin = 0.0f;
537         v_cos = -1.0f;
538         break;
539     case IVI_CONTROLLER_SURFACE_ORIENTATION_270_DEGREES:
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->surface_rotation.link);
548     weston_surface_geometry_dirty(es);
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(&es->geometry.transformation_list,
558                    &ivisurf->surface_rotation.link);
559
560     weston_surface_set_transform_parent(es, NULL);
561     weston_surface_update_transform(es);
562 }
563
564 static void
565 update_layer_orientation(struct ivilayer *ivilayer,
566                          struct ivisurface *ivisurf)
567 {
568     float cx = 0.0f;
569     float cy = 0.0f;
570     struct weston_surface *es = ivisurf->surface;
571     struct weston_output *output = NULL;
572     float v_sin = 0.0f;
573     float v_cos = 0.0f;
574     float sx = 1.0f;
575     float sy = 1.0f;
576     float width = 0.0f;
577     float height = 0.0f;
578     struct weston_matrix *matrix = &ivisurf->layer_rotation.matrix;
579     struct shell_surface *shsurf = NULL;
580
581     if (es == NULL) {
582         return;
583     }
584
585     shsurf = get_shell_surface(es);
586     if (shsurf == NULL) {
587         return;
588     }
589
590     if ((shsurf->width == 0) || (shsurf->height == 0)) {
591         return;
592     }
593
594     output = es->output;
595     if (output == NULL) {
596         return;
597     }
598     if ((output->width == 0) || (output->height == 0)) {
599         return;
600     }
601     width = (float)output->width;
602     height = (float)output->height;
603
604     switch (ivilayer->prop.orientation) {
605     case IVI_CONTROLLER_SURFACE_ORIENTATION_0_DEGREES:
606         v_sin = 0.0f;
607         v_cos = 1.0f;
608         break;
609     case IVI_CONTROLLER_SURFACE_ORIENTATION_90_DEGREES:
610         v_sin = 1.0f;
611         v_cos = 0.0f;
612         sx = width / height;
613         sy = height / width;
614         break;
615     case IVI_CONTROLLER_SURFACE_ORIENTATION_180_DEGREES:
616         v_sin = 0.0f;
617         v_cos = -1.0f;
618         break;
619     case IVI_CONTROLLER_SURFACE_ORIENTATION_270_DEGREES:
620     default:
621         v_sin = -1.0f;
622         v_cos = 0.0f;
623         sx = width / height;
624         sy = height / width;
625         break;
626     }
627     wl_list_remove(&ivisurf->layer_rotation.link);
628     weston_surface_geometry_dirty(es);
629
630     weston_matrix_init(matrix);
631     cx = 0.5f * width;
632     cy = 0.5f * height;
633     weston_matrix_translate(matrix, -cx, -cy, 0.0f);
634     weston_matrix_rotate_xy(matrix, v_cos, v_sin);
635     weston_matrix_scale(matrix, sx, sy, 1.0);
636     weston_matrix_translate(matrix, cx, cy, 0.0f);
637     wl_list_insert(&es->geometry.transformation_list,
638                    &ivisurf->layer_rotation.link);
639
640     weston_surface_set_transform_parent(es, NULL);
641     weston_surface_update_transform(es);
642 }
643
644 static void
645 update_surface_position(struct ivisurface *ivisurf)
646 {
647     struct weston_surface *es = ivisurf->surface;
648     float tx  = (float)ivisurf->prop.dest_x;
649     float ty  = (float)ivisurf->prop.dest_y;
650     struct weston_matrix *matrix = &ivisurf->surface_pos.matrix;
651     struct shell_surface *shsurf = NULL;
652
653     if (es == NULL) {
654         return;
655     }
656
657     shsurf = get_shell_surface(es);
658     if (shsurf == NULL) {
659         return;
660     }
661
662     if ((shsurf->width == 0) || (shsurf->height == 0)) {
663         return;
664     }
665
666     wl_list_remove(&ivisurf->surface_pos.link);
667
668     weston_matrix_init(matrix);
669     weston_matrix_translate(matrix, tx, ty, 0.0f);
670     wl_list_insert(&es->geometry.transformation_list,
671                    &ivisurf->surface_pos.link);
672
673     weston_surface_set_transform_parent(es, NULL);
674     weston_surface_update_transform(es);
675
676     //weston_zoom_run(es, 0.0, 1.0, NULL, NULL);
677 }
678
679 static void
680 update_layer_position(struct ivilayer *ivilayer,
681                struct ivisurface *ivisurf)
682 {
683     struct weston_surface *es = ivisurf->surface;
684     float tx  = (float)ivilayer->prop.dest_x;
685     float ty  = (float)ivilayer->prop.dest_y;
686     struct weston_matrix *matrix = &ivisurf->layer_pos.matrix;
687     struct shell_surface *shsurf = NULL;
688
689     if (es == NULL) {
690         return;
691     }
692
693     shsurf = get_shell_surface(es);
694     if (shsurf == NULL) {
695         return;
696     }
697
698     if ((shsurf->width == 0) || (shsurf->height == 0)) {
699         return;
700     }
701
702     wl_list_remove(&ivisurf->layer_pos.link);
703
704     weston_matrix_init(matrix);
705     weston_matrix_translate(matrix, tx, ty, 0.0f);
706     wl_list_insert(
707         &es->geometry.transformation_list,
708         &ivisurf->layer_pos.link);
709
710     weston_surface_set_transform_parent(es, NULL);
711     weston_surface_update_transform(es);
712 }
713
714 static void
715 update_scale(struct ivilayer *ivilayer,
716                struct ivisurface *ivisurf)
717 {
718     struct weston_surface *es = ivisurf->surface;
719     float sx = 0.0f;
720     float sy = 0.0f;
721     float lw = 0.0f;
722     float sw = 0.0f;
723     float lh = 0.0f;
724     float sh = 0.0f;
725     struct weston_matrix *matrix = &ivisurf->scaling.matrix;
726     struct shell_surface *shsurf = NULL;
727
728     if (es == NULL) {
729         return;
730     }
731
732     shsurf = get_shell_surface(es);
733     if (shsurf == NULL) {
734         return;
735     }
736
737     if ((shsurf->width == 0) || (shsurf->height == 0)) {
738         return;
739     }
740
741     lw = ((float)ivilayer->prop.dest_width / shsurf->width);
742     sw = ((float)ivisurf->prop.dest_width / shsurf->width);
743     lh = ((float)ivilayer->prop.dest_height / shsurf->height);
744     sh = ((float)ivisurf->prop.dest_height / shsurf->height);
745     sx = sw * lw;
746     sy = sh * lh;
747
748     wl_list_remove(&ivisurf->scaling.link);
749     weston_matrix_init(matrix);
750     weston_matrix_scale(matrix, sx, sy, 1.0f);
751
752     wl_list_insert(&es->geometry.transformation_list,
753                    &ivisurf->scaling.link);
754
755     weston_surface_set_transform_parent(es, NULL);
756     weston_surface_update_transform(es);
757 }
758
759 static void
760 update_prop(struct ivilayer *ivilayer,
761             struct ivisurface *ivisurf)
762 {
763     if (ivilayer->event_mask | ivisurf->event_mask) {
764         update_opacity(ivilayer, ivisurf);
765         update_layer_orientation(ivilayer, ivisurf);
766         update_layer_position(ivilayer, ivisurf);
767         update_surface_position(ivisurf);
768         update_surface_orientation(ivilayer, ivisurf);
769         update_scale(ivilayer, ivisurf);
770
771         ivisurf->update_count++;
772
773         if (ivisurf->surface == NULL) {
774             return;
775         }
776         weston_surface_geometry_dirty(ivisurf->surface);
777         weston_surface_damage(ivisurf->surface);
778     }
779 }
780
781 static void
782 send_surface_add_event(struct ivisurface *ivisurf,
783                        struct wl_resource *res_surf)
784 {
785     struct ivishell *shell = ivisurf->shell;
786     struct link_layer* link_layer = NULL;
787     struct ivicontroller_layer *ctrllayer = NULL;
788     struct wl_client *client_surf = NULL;
789
790     client_surf = wl_resource_get_client(res_surf);
791     wl_list_for_each(link_layer, &ivisurf->list_layer, link) {
792         wl_list_for_each(ctrllayer, &shell->list_controller_layer, link) {
793
794             if (ctrllayer->client != client_surf) {
795                 continue;
796             }
797
798             if (ctrllayer->id_layer != link_layer->layer->id_layer) {
799                 continue;
800             }
801
802             ivi_controller_surface_send_layer(res_surf, NULL);
803             ivi_controller_surface_send_layer(res_surf, ctrllayer->resource);
804         }
805     }
806 }
807
808 static void
809 send_surface_event(struct ivisurface *ivisurf,
810                    struct wl_resource *resource, uint32_t mask)
811 {
812     if (mask & PROP_EVENT_OPACITY) {
813         ivi_controller_surface_send_opacity(resource,
814                                             ivisurf->prop.opacity);
815     }
816     if (mask & PROP_EVENT_SRC_RECT) {
817         ivi_controller_surface_send_source_rectangle(resource,
818             ivisurf->prop.src_x, ivisurf->prop.src_y,
819             ivisurf->prop.src_width, ivisurf->prop.src_height);
820     }
821     if (mask & PROP_EVENT_DST_RECT) {
822         ivi_controller_surface_send_destination_rectangle(resource,
823             ivisurf->prop.dest_x, ivisurf->prop.dest_y,
824             ivisurf->prop.dest_width, ivisurf->prop.dest_height);
825     }
826     if (mask & PROP_EVENT_ORIENTATION) {
827         ivi_controller_surface_send_orientation(resource,
828                                                 ivisurf->prop.orientation);
829     }
830     if (mask & PROP_EVENT_VISIBILITY) {
831         ivi_controller_surface_send_visibility(resource,
832                                                ivisurf->prop.visibility);
833     }
834     if (mask & PROP_EVENT_PIXELFORMAT) {
835         ivi_controller_surface_send_pixelformat(resource,
836                                                 ivisurf->pixelformat);
837     }
838     if (mask & PROP_EVENT_ADD) {
839         send_surface_add_event(ivisurf, resource);
840     }
841 }
842
843 static void
844 send_layer_add_event(struct ivilayer *ivilayer,
845                        struct wl_resource *res_layer)
846 {
847     struct link_screen *link_scrn = NULL;
848     struct wl_client *client_layer = NULL;
849     struct wl_resource *resource_output = NULL;
850
851     client_layer = wl_resource_get_client(res_layer);
852     wl_list_for_each(link_scrn, &ivilayer->list_screen, link) {
853
854         resource_output = wl_resource_find_for_client(
855                               &link_scrn->screen->output->resource_list,
856                               client_layer);
857         if (resource_output == NULL) {
858             continue;
859         }
860
861         ivi_controller_layer_send_screen(res_layer, resource_output);
862     }
863 }
864
865 static void
866 send_layer_event(struct ivilayer *ivilayer,
867                  struct wl_resource *resource, uint32_t mask)
868 {
869     if (mask & PROP_EVENT_OPACITY) {
870         ivi_controller_layer_send_opacity(resource,
871                                           ivilayer->prop.opacity);
872     }
873     if (mask & PROP_EVENT_SRC_RECT) {
874         ivi_controller_layer_send_source_rectangle(resource,
875                                           ivilayer->prop.src_x,
876                                           ivilayer->prop.src_y,
877                                           ivilayer->prop.src_width,
878                                           ivilayer->prop.src_height);
879     }
880     if (mask & PROP_EVENT_DST_RECT) {
881         ivi_controller_layer_send_destination_rectangle(resource,
882                                           ivilayer->prop.dest_x,
883                                           ivilayer->prop.dest_y,
884                                           ivilayer->prop.dest_width,
885                                           ivilayer->prop.dest_height);
886     }
887     if (mask & PROP_EVENT_ORIENTATION) {
888         ivi_controller_layer_send_orientation(resource,
889                                           ivilayer->prop.orientation);
890     }
891     if (mask & PROP_EVENT_VISIBILITY) {
892         ivi_controller_layer_send_visibility(resource,
893                                           ivilayer->prop.visibility);
894     }
895     if (mask & PROP_EVENT_ADD) {
896         send_layer_add_event(ivilayer, resource);
897     }
898 }
899
900 static void
901 send_surface_prop(struct ivisurface *ivisurf)
902 {
903     struct ivicontroller_surface *ctrlsurf = 0;
904     struct ivishell *shell = ivisurf->shell;
905     wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
906         if (ivisurf->id_surface != ctrlsurf->id_surface) {
907             continue;
908         }
909         send_surface_event(ivisurf, ctrlsurf->resource, ivisurf->event_mask);
910     }
911     ivisurf->event_mask = 0;
912 }
913
914 static void
915 send_layer_prop(struct ivilayer *ivilayer)
916 {
917     struct ivicontroller_layer *ctrllayer = 0;
918     struct ivishell *shell = ivilayer->shell;
919     wl_list_for_each(ctrllayer, &shell->list_controller_layer, link) {
920         if (ivilayer->id_layer != ctrllayer->id_layer) {
921             continue;
922         }
923         send_layer_event(ivilayer, ctrllayer->resource, ivilayer->event_mask);
924     }
925     ivilayer->event_mask = 0;
926 }
927
928 static void
929 commit_changes(struct ivishell *shell)
930 {
931     struct iviscreen *iviscrn;
932     struct ivilayer *ivilayer;
933     struct ivisurface *ivisurf;
934     wl_list_for_each(iviscrn, &shell->list_screen, link) {
935         wl_list_for_each(ivilayer,
936                         &iviscrn->order.list_layer,
937                         order.link) {
938             wl_list_for_each(ivisurf,
939                              &ivilayer->order.list_surface,
940                              order.link) {
941                 update_prop(ivilayer, ivisurf);
942             }
943         }
944     }
945 }
946
947 static void
948 send_prop(struct ivishell *shell)
949 {
950     struct ivilayer *ivilayer;
951     struct ivisurface *ivisurf;
952     wl_list_for_each(ivilayer, &shell->list_layer, link) {
953         send_layer_prop(ivilayer);
954     }
955
956     wl_list_for_each(ivisurf, &shell->list_surface, link) {
957         send_surface_prop(ivisurf);
958     }
959 }
960
961 static void
962 surface_destroy(struct wl_client *client,
963                 struct wl_resource *resource)
964 {
965     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
966     (void)client;
967
968     if (!wl_list_empty(&ivisurf->pending.link)) {
969         wl_list_remove(&ivisurf->pending.link);
970     }
971     if (!wl_list_empty(&ivisurf->order.link)) {
972         wl_list_remove(&ivisurf->order.link);
973     }
974     if (!wl_list_empty(&ivisurf->link)) {
975         wl_list_remove(&ivisurf->link);
976     }
977     remove_ordersurface_from_layer(ivisurf);
978     wl_list_remove(&ivisurf->surface_destroy_listener.link);
979
980     free(ivisurf);
981 }
982
983 static const struct ivi_surface_interface surface_implementation = {
984     surface_destroy
985 };
986
987 static void
988 controller_surface_set_opacity(struct wl_client *client,
989                    struct wl_resource *resource,
990                    wl_fixed_t opacity)
991 {
992     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
993     struct ivi_properties *prop = &ivisurf->pending.prop;
994     (void)client;
995
996     prop->opacity = opacity;
997     ivisurf->event_mask |= PROP_EVENT_OPACITY;
998 }
999
1000 static void
1001 controller_surface_set_source_rectangle(struct wl_client *client,
1002                    struct wl_resource *resource,
1003                    int32_t x,
1004                    int32_t y,
1005                    int32_t width,
1006                    int32_t height)
1007 {
1008     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
1009     struct ivi_properties *prop = &ivisurf->pending.prop;
1010     (void)client;
1011
1012     prop->src_x = x;
1013     prop->src_y = y;
1014     prop->src_width = width;
1015     prop->src_height = height;
1016     ivisurf->event_mask |= PROP_EVENT_SRC_RECT;
1017 }
1018
1019 static void
1020 controller_surface_set_destination_rectangle(struct wl_client *client,
1021                      struct wl_resource *resource,
1022                      int32_t x,
1023                      int32_t y,
1024                      int32_t width,
1025                      int32_t height)
1026 {
1027     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
1028     struct ivi_properties *prop = &ivisurf->pending.prop;
1029     (void)client;
1030
1031     prop->dest_x = x;
1032     prop->dest_y = y;
1033     prop->dest_width = width;
1034     prop->dest_height = height;
1035
1036     ivisurf->event_mask |= PROP_EVENT_DST_RECT;
1037 }
1038
1039 static void
1040 controller_surface_set_visibility(struct wl_client *client,
1041                       struct wl_resource *resource,
1042                       uint32_t visibility)
1043 {
1044     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
1045     struct ivi_properties *prop = &ivisurf->pending.prop;
1046     (void)client;
1047
1048     prop->visibility = visibility;
1049     ivisurf->event_mask |= PROP_EVENT_VISIBILITY;
1050 }
1051
1052 static void
1053 controller_surface_set_configuration(struct wl_client *client,
1054                    struct wl_resource *resource,
1055                    int32_t width, int32_t height)
1056 {
1057     /* This interface has been supported yet. */
1058     (void)client;
1059     (void)resource;
1060     (void)width;
1061     (void)height;
1062 }
1063
1064 static void
1065 controller_surface_set_orientation(struct wl_client *client,
1066                    struct wl_resource *resource,
1067                    int32_t orientation)
1068 {
1069     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
1070     struct ivi_properties *prop = &ivisurf->pending.prop;
1071     (void)client;
1072
1073     prop->orientation = orientation;
1074     ivisurf->event_mask |= PROP_EVENT_ORIENTATION;
1075 }
1076
1077 static void
1078 controller_surface_screenshot(struct wl_client *client,
1079                   struct wl_resource *resource,
1080                   const char *filename)
1081 {
1082     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
1083     struct weston_compositor *compositor = ivisurf->shell->compositor;
1084     cairo_surface_t *surface;
1085     int32_t width;
1086     int32_t height;
1087     int32_t stride;
1088     uint8_t *pixels;
1089     (void)client;
1090
1091     width = ivisurf->prop.dest_width;
1092     height = ivisurf->prop.dest_height;
1093     stride = width *
1094              (PIXMAN_FORMAT_BPP(compositor->read_format) / 8);
1095     pixels = malloc(stride * height);
1096     if (pixels == NULL) {
1097         return;
1098     }
1099
1100     compositor->renderer->read_surface_pixels(ivisurf->surface,
1101                              compositor->read_format, pixels,
1102                              0, 0, width, height);
1103
1104     surface = cairo_image_surface_create_for_data(pixels,
1105                                                   CAIRO_FORMAT_ARGB32,
1106                                                   width, height, stride);
1107     cairo_surface_write_to_png(surface, filename);
1108     cairo_surface_destroy(surface);
1109     free(pixels);
1110 }
1111
1112 static void
1113 controller_surface_send_stats(struct wl_client *client,
1114                               struct wl_resource *resource)
1115 {
1116     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
1117     pid_t pid;
1118     uid_t uid;
1119     gid_t gid;
1120     wl_client_get_credentials(client, &pid, &uid, &gid);
1121
1122     ivi_controller_surface_send_stats(resource, 0, 0,
1123                                       ivisurf->update_count, pid, "");
1124 }
1125
1126 static void
1127 controller_surface_destroy(struct wl_client *client,
1128               struct wl_resource *resource,
1129               int32_t destroy_scene_object)
1130 {
1131     struct ivisurface *ivisurf = wl_resource_get_user_data(resource);
1132     struct ivishell *shell = ivisurf->shell;
1133     struct ivicontroller_surface *ctrlsurf = NULL;
1134     (void)client;
1135     (void)destroy_scene_object;
1136
1137     wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
1138         if (ctrlsurf->id_surface != ivisurf->id_surface) {
1139             continue;
1140         }
1141
1142         if (!wl_list_empty(&ctrlsurf->link)) {
1143             wl_list_remove(&ctrlsurf->link);
1144         }
1145         wl_resource_destroy(resource);
1146         break;
1147     }
1148 }
1149
1150 static void
1151 controller_surface_set_input_focus(struct wl_client *client,
1152               struct wl_resource *resource,
1153               int32_t enabled)
1154 {
1155     (void)client;
1156     (void)resource;
1157     (void)enabled;
1158 }
1159
1160 static const
1161 struct ivi_controller_surface_interface controller_surface_implementation = {
1162     controller_surface_set_visibility,
1163     controller_surface_set_opacity,
1164     controller_surface_set_source_rectangle,
1165     controller_surface_set_destination_rectangle,
1166     controller_surface_set_configuration,
1167     controller_surface_set_orientation,
1168     controller_surface_screenshot,
1169     controller_surface_send_stats,
1170     controller_surface_destroy,
1171     controller_surface_set_input_focus
1172 };
1173
1174 static void
1175 controller_layer_set_source_rectangle(struct wl_client *client,
1176                    struct wl_resource *resource,
1177                    int32_t x,
1178                    int32_t y,
1179                    int32_t width,
1180                    int32_t height)
1181 {
1182     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1183     struct ivi_properties *prop = &ivilayer->pending.prop;
1184     (void)client;
1185
1186     prop->src_x = x;
1187     prop->src_y = y;
1188     prop->src_width = width;
1189     prop->src_height = height;
1190     ivilayer->event_mask |= PROP_EVENT_SRC_RECT;
1191 }
1192
1193 static void
1194 controller_layer_set_destination_rectangle(struct wl_client *client,
1195                  struct wl_resource *resource,
1196                  int32_t x,
1197                  int32_t y,
1198                  int32_t width,
1199                  int32_t height)
1200 {
1201     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1202     struct ivi_properties *prop = &ivilayer->pending.prop;
1203     (void)client;
1204
1205     prop->dest_x = x;
1206     prop->dest_y = y;
1207     prop->dest_width = width;
1208     prop->dest_height = height;
1209     ivilayer->event_mask |= PROP_EVENT_DST_RECT;
1210 }
1211
1212 static void
1213 controller_layer_set_visibility(struct wl_client *client,
1214                     struct wl_resource *resource,
1215                     uint32_t visibility)
1216 {
1217     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1218     struct ivi_properties *prop = &ivilayer->pending.prop;
1219     (void)client;
1220
1221     prop->visibility = visibility;
1222     ivilayer->event_mask |= PROP_EVENT_VISIBILITY;
1223 }
1224
1225 static void
1226 controller_layer_set_opacity(struct wl_client *client,
1227                  struct wl_resource *resource,
1228                  wl_fixed_t opacity)
1229 {
1230     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1231     struct ivi_properties *prop = &ivilayer->pending.prop;
1232     (void)client;
1233
1234     prop->opacity = opacity;
1235     ivilayer->event_mask |= PROP_EVENT_OPACITY;
1236 }
1237
1238 static void
1239 controller_layer_set_configuration(struct wl_client *client,
1240                  struct wl_resource *resource,
1241                  int32_t width,
1242                  int32_t height)
1243 {
1244     /* This interface has been supported yet. */
1245     (void)client;
1246     (void)resource;
1247     (void)width;
1248     (void)height;
1249 }
1250
1251 static void
1252 controller_layer_set_orientation(struct wl_client *client,
1253                  struct wl_resource *resource,
1254                  int32_t orientation)
1255 {
1256     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1257     struct ivi_properties *prop = &ivilayer->pending.prop;
1258     (void)client;
1259
1260     prop->orientation = orientation;
1261     ivilayer->event_mask |= PROP_EVENT_ORIENTATION;
1262 }
1263
1264 static void
1265 controller_layer_clear_surfaces(struct wl_client *client,
1266                     struct wl_resource *resource)
1267 {
1268     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1269     (void)client;
1270
1271     wl_list_init(&ivilayer->pending.list_surface);
1272
1273     ivilayer->event_mask |= PROP_EVENT_ADD;
1274 }
1275
1276 static int
1277 is_surface_in_layer(struct ivisurface *ivisurf,
1278                     struct ivilayer *ivilayer)
1279 {
1280     struct ivisurface *surf = NULL;
1281
1282     wl_list_for_each(surf, &ivilayer->pending.list_surface, pending.link) {
1283         if (surf->id_surface == ivisurf->id_surface) {
1284             return 1;
1285         }
1286     }
1287
1288     return 0;
1289 }
1290
1291 static void
1292 controller_layer_add_surface(struct wl_client *client,
1293                  struct wl_resource *resource,
1294                  struct wl_resource *surface)
1295 {
1296     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1297     struct ivisurface *addsurf = wl_resource_get_user_data(surface);
1298     struct ivisurface *ivisurf;
1299     int is_surf_in_layer = 0;
1300     (void)client;
1301
1302     if (addsurf == NULL) {
1303         weston_log("invalid surface in layer_add_surface\n");
1304         return;
1305     }
1306     is_surf_in_layer = is_surface_in_layer(addsurf, ivilayer);
1307     if (is_surf_in_layer == 1) {
1308         return;
1309     }
1310
1311     wl_list_for_each(ivisurf, &ivilayer->shell->list_surface, link) {
1312         if (ivisurf->id_surface == addsurf->id_surface) {
1313             if (!wl_list_empty(&ivisurf->pending.link)) {
1314                 wl_list_remove(&ivisurf->pending.link);
1315             }
1316             wl_list_init(&ivisurf->pending.link);
1317             wl_list_insert(&ivilayer->pending.list_surface,
1318                            &ivisurf->pending.link);
1319             break;
1320         }
1321     }
1322
1323     ivilayer->event_mask |= PROP_EVENT_ADD;
1324 }
1325
1326 static void
1327 controller_layer_remove_surface(struct wl_client *client,
1328                     struct wl_resource *resource,
1329                     struct wl_resource *surface)
1330 {
1331     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1332     struct ivisurface *remsurf = wl_resource_get_user_data(surface);
1333     struct ivisurface *ivisurf = NULL;
1334     struct ivisurface *next = NULL;
1335     (void)client;
1336     (void)resource;
1337     (void)surface;
1338
1339     wl_list_for_each_safe(ivisurf, next,
1340                           &ivilayer->pending.list_surface, pending.link) {
1341         if (ivisurf->id_surface == remsurf->id_surface) {
1342             if (!wl_list_empty(&ivisurf->pending.link)) {
1343                 wl_list_remove(&ivisurf->pending.link);
1344             }
1345             wl_list_init(&ivisurf->pending.link);
1346             break;
1347         }
1348     }
1349 }
1350
1351 static void
1352 controller_layer_screenshot(struct wl_client *client,
1353                 struct wl_resource *resource,
1354                 const char *filename)
1355 {
1356     (void)client;
1357     (void)resource;
1358     (void)filename;
1359 }
1360
1361 static void
1362 controller_layer_set_render_order(struct wl_client *client,
1363                                   struct wl_resource *resource,
1364                                   struct wl_array *id_surfaces)
1365 {
1366     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1367     struct ivisurface *ivisurf = NULL;
1368     uint32_t *id_surface = NULL;
1369     (void)client;
1370
1371     wl_list_init(&ivilayer->pending.list_surface);
1372     wl_array_for_each(id_surface, id_surfaces) {
1373         wl_list_for_each(ivisurf, &ivilayer->shell->list_surface, link) {
1374             if (*id_surface != ivisurf->id_surface) {
1375                 continue;
1376             }
1377
1378             if (!wl_list_empty(&ivisurf->pending.link)) {
1379                 wl_list_remove(&ivisurf->pending.link);
1380             }
1381             wl_list_init(&ivisurf->pending.link);
1382             wl_list_insert(&ivilayer->pending.list_surface,
1383                            &ivisurf->pending.link);
1384             break;
1385         }
1386     }
1387
1388     ivilayer->event_mask |= PROP_EVENT_ADD;
1389 }
1390
1391 static void
1392 layer_destroy(struct ivilayer *ivilayer)
1393 {
1394     if (!wl_list_empty(&ivilayer->pending.link)) {
1395         wl_list_remove(&ivilayer->pending.link);
1396     }
1397     if (!wl_list_empty(&ivilayer->order.link)) {
1398         wl_list_remove(&ivilayer->order.link);
1399     }
1400     if (!wl_list_empty(&ivilayer->link)) {
1401         wl_list_remove(&ivilayer->link);
1402     }
1403     remove_orderlayer_from_screen(ivilayer);
1404
1405     free(ivilayer);
1406 }
1407
1408 static void
1409 controller_layer_destroy(struct wl_client *client,
1410               struct wl_resource *resource,
1411               int32_t destroy_scene_object)
1412 {
1413     struct ivilayer *ivilayer = wl_resource_get_user_data(resource);
1414     struct ivishell *shell = ivilayer->shell;
1415     struct ivicontroller_layer *ctrllayer = NULL;
1416     (void)client;
1417     (void)destroy_scene_object;
1418
1419     wl_list_for_each(ctrllayer, &shell->list_controller_layer, link) {
1420         if (ctrllayer->id_layer != ivilayer->id_layer) {
1421             continue;
1422         }
1423
1424         if (!wl_list_empty(&ctrllayer->link)) {
1425             wl_list_remove(&ctrllayer->link);
1426         }
1427         wl_resource_destroy(resource);
1428         break;
1429     }
1430
1431     layer_destroy(ivilayer);
1432 }
1433
1434 static const
1435 struct ivi_controller_layer_interface controller_layer_implementation = {
1436     controller_layer_set_visibility,
1437     controller_layer_set_opacity,
1438     controller_layer_set_source_rectangle,
1439     controller_layer_set_destination_rectangle,
1440     controller_layer_set_configuration,
1441     controller_layer_set_orientation,
1442     controller_layer_screenshot,
1443     controller_layer_clear_surfaces,
1444     controller_layer_add_surface,
1445     controller_layer_remove_surface,
1446     controller_layer_set_render_order,
1447     controller_layer_destroy
1448 };
1449
1450 static void
1451 controller_screen_destroy(struct wl_client *client,
1452                           struct wl_resource *resource)
1453 {
1454     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1455     struct ivicontroller_screen *ctrlscrn = NULL;
1456     struct ivicontroller_screen *next = NULL;
1457     (void)client;
1458
1459     wl_list_for_each_safe(ctrlscrn, next,
1460                           &iviscrn->shell->list_controller_screen, link) {
1461         if (iviscrn->id_screen != ctrlscrn->id_screen) {
1462             continue;
1463         }
1464         destroy_ivicontroller_screen(ctrlscrn->resource);
1465         wl_resource_destroy(ctrlscrn->resource);
1466         break;
1467     }
1468 }
1469
1470 static void
1471 controller_screen_clear(struct wl_client *client,
1472                 struct wl_resource *resource)
1473 {
1474     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1475     (void)client;
1476
1477     wl_list_init(&iviscrn->pending.list_layer);
1478
1479     iviscrn->event_mask |= PROP_EVENT_ADD;
1480 }
1481
1482 static int
1483 is_layer_in_screen(struct ivilayer *ivilayer,
1484                     struct iviscreen *iviscrn)
1485 {
1486     struct ivilayer *layer = NULL;
1487
1488     wl_list_for_each(layer, &iviscrn->pending.list_layer, pending.link) {
1489         if (layer->id_layer == ivilayer->id_layer) {
1490             return 1;
1491         }
1492     }
1493
1494     return 0;
1495 }
1496
1497 static void
1498 controller_screen_add_layer(struct wl_client *client,
1499                 struct wl_resource *resource,
1500                 struct wl_resource *layer)
1501 {
1502     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1503     struct ivilayer *addlayer = wl_resource_get_user_data(layer);
1504     struct ivilayer *ivilayer;
1505     int is_layer_in_scrn = 0;
1506     (void)client;
1507
1508     is_layer_in_scrn = is_layer_in_screen(addlayer, iviscrn);
1509     if (is_layer_in_scrn == 1) {
1510         return;
1511     }
1512
1513     wl_list_for_each(ivilayer, &iviscrn->shell->list_layer, link) {
1514         if (ivilayer->id_layer == addlayer->id_layer) {
1515             if (!wl_list_empty(&ivilayer->pending.link)) {
1516                 wl_list_remove(&ivilayer->pending.link);
1517             }
1518             wl_list_init(&ivilayer->pending.link);
1519             wl_list_insert(&iviscrn->pending.list_layer,
1520                            &ivilayer->pending.link);
1521             break;
1522         }
1523     }
1524
1525     iviscrn->event_mask |= PROP_EVENT_ADD;
1526 }
1527
1528 static void
1529 controller_screen_screenshot(struct wl_client *client,
1530                 struct wl_resource *resource,
1531                 const char *filename)
1532 {
1533     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1534     struct weston_output *output = iviscrn->output;
1535     cairo_surface_t *surface = NULL;
1536     int i = 0;
1537     int32_t width = 0;
1538     int32_t height = 0;
1539     int32_t stride = 0;
1540     uint8_t *readpixs = NULL;
1541     uint8_t *writepixs = NULL;
1542     uint8_t *d = NULL;
1543     uint8_t *s = NULL;
1544     (void)client;
1545
1546     output->disable_planes--;
1547
1548     width = output->current_mode->width;
1549     height = output->current_mode->height;
1550     stride = width *
1551              (PIXMAN_FORMAT_BPP(output->compositor->read_format) / 8);
1552     readpixs = malloc(stride * height);
1553     if (readpixs == NULL) {
1554         return;
1555     }
1556     writepixs = malloc(stride * height);
1557     if (writepixs == NULL) {
1558         return;
1559     }
1560
1561     output->compositor->renderer->read_pixels(output,
1562                              output->compositor->read_format, readpixs,
1563                              0, 0, width, height);
1564
1565     s = readpixs;
1566     d = writepixs + stride * (height - 1);
1567
1568     for (i = 0; i < height; i++) {
1569         memcpy(d, s, stride);
1570         d -= stride;
1571         s += stride;
1572     }
1573
1574     surface = cairo_image_surface_create_for_data(writepixs,
1575                                                   CAIRO_FORMAT_ARGB32,
1576                                                   width, height, stride);
1577     cairo_surface_write_to_png(surface, filename);
1578     cairo_surface_destroy(surface);
1579     free(writepixs);
1580     free(readpixs);
1581 }
1582
1583 static void
1584 controller_screen_set_render_order(struct wl_client *client,
1585                 struct wl_resource *resource,
1586                 struct wl_array *id_layers)
1587 {
1588     struct iviscreen *iviscrn = wl_resource_get_user_data(resource);
1589     uint32_t *id_layer = NULL;
1590     struct ivilayer *ivilayer;
1591     (void)client;
1592
1593     wl_list_init(&iviscrn->pending.list_layer);
1594     wl_array_for_each(id_layer, id_layers) {
1595         wl_list_for_each(ivilayer, &iviscrn->shell->list_layer, link) {
1596             if (*id_layer == ivilayer->id_layer) {
1597                 continue;
1598             }
1599
1600             if (!wl_list_empty(&ivilayer->pending.link)) {
1601                 wl_list_remove(&ivilayer->pending.link);
1602             }
1603             wl_list_init(&ivilayer->pending.link);
1604             wl_list_insert(&iviscrn->pending.list_layer,
1605                            &ivilayer->pending.link);
1606             break;
1607         }
1608     }
1609
1610     iviscrn->event_mask |= PROP_EVENT_ADD;
1611 }
1612
1613 static const
1614 struct ivi_controller_screen_interface controller_screen_implementation = {
1615     controller_screen_destroy,
1616     controller_screen_clear,
1617     controller_screen_add_layer,
1618     controller_screen_screenshot,
1619     controller_screen_set_render_order
1620 };
1621
1622 static void
1623 commit_list_surface(struct ivishell *shell)
1624 {
1625     struct ivisurface *ivisurf;
1626     wl_list_for_each(ivisurf, &shell->list_surface, link) {
1627         ivisurf->prop = ivisurf->pending.prop;
1628     }
1629 }
1630
1631 static void
1632 commit_list_layer(struct ivishell *shell)
1633 {
1634     struct ivilayer *ivilayer;
1635     struct ivisurface *ivisurf;
1636
1637     wl_list_for_each(ivilayer, &shell->list_layer, link) {
1638         ivilayer->prop = ivilayer->pending.prop;
1639
1640         if (!(ivilayer->event_mask & PROP_EVENT_ADD)) {
1641             continue;
1642         }
1643         wl_list_init(&ivilayer->order.list_surface);
1644         wl_list_for_each(ivisurf, &ivilayer->pending.list_surface,
1645                               pending.link) {
1646             remove_ordersurface_from_layer(ivisurf);
1647
1648             if (!wl_list_empty(&ivisurf->order.link)) {
1649                 wl_list_remove(&ivisurf->order.link);
1650             }
1651             wl_list_init(&ivisurf->order.link);
1652             wl_list_insert(&ivilayer->order.list_surface,
1653                            &ivisurf->order.link);
1654             add_ordersurface_to_layer(ivisurf, ivilayer);
1655         }
1656     }
1657 }
1658
1659 static void
1660 commit_list_screen(struct ivishell *shell)
1661 {
1662     struct weston_compositor *ec = shell->compositor;
1663     struct iviscreen *iviscrn;
1664     struct ivilayer *ivilayer;
1665     struct ivisurface *ivisurf;
1666     wl_list_for_each(iviscrn, &shell->list_screen, link) {
1667         if (iviscrn->event_mask & PROP_EVENT_ADD) {
1668             wl_list_init(&iviscrn->order.list_layer);
1669
1670             wl_list_for_each(ivilayer, &iviscrn->pending.list_layer,
1671                              pending.link) {
1672                 remove_orderlayer_from_screen(ivilayer);
1673
1674                 wl_list_insert(&iviscrn->order.list_layer,
1675                                &ivilayer->order.link);
1676                 add_orderlayer_to_screen(ivilayer, iviscrn);
1677             }
1678             iviscrn->event_mask = 0;
1679         }
1680
1681         /* For rendering */
1682         wl_list_init(&ec->layer_list);
1683         wl_list_for_each(ivilayer, &iviscrn->order.list_layer, order.link) {
1684             if (ivilayer->prop.visibility == 0) {
1685                 continue;
1686             }
1687
1688             wl_list_insert(&ec->layer_list, &ivilayer->el.link);
1689             wl_list_init(&ivilayer->el.surface_list);
1690
1691             wl_list_for_each(ivisurf, &ivilayer->order.list_surface,
1692                              order.link) {
1693                 if (ivisurf->prop.visibility == 0) {
1694                     continue;
1695                 }
1696
1697                 if (ivisurf->surface == NULL) {
1698                     continue;
1699                 }
1700
1701                 wl_list_insert(&ivilayer->el.surface_list,
1702                                &ivisurf->surface->layer_link);
1703                 ivisurf->surface->output = iviscrn->output;
1704             }
1705         }
1706         break;
1707     }
1708 }
1709
1710 static void
1711 controller_commit_changes(struct wl_client *client,
1712                           struct wl_resource *resource)
1713 {
1714     struct ivicontroller *controller = wl_resource_get_user_data(resource);
1715     (void)client;
1716
1717     commit_list_surface(controller->shell);
1718     commit_list_layer(controller->shell);
1719     commit_list_screen(controller->shell);
1720
1721     commit_changes(controller->shell);
1722     send_prop(controller->shell);
1723     weston_compositor_schedule_repaint(controller->shell->compositor);
1724 }
1725
1726 static struct ivilayer*
1727 layer_create(struct wl_client *client,
1728                         struct ivicontroller *ctrl,
1729                         uint32_t id_layer,
1730                         int32_t width,
1731                         int32_t height,
1732                         uint32_t id)
1733 {
1734     struct ivishell *shell = ctrl->shell;
1735     struct ivilayer *ivilayer = NULL;
1736     struct ivicontroller *controller = NULL;
1737     struct ivicontroller_layer *ctrllayer = NULL;
1738     (void)client;
1739     (void)width;
1740     (void)height;
1741     (void)id;
1742
1743     ivilayer = get_layer(&shell->list_layer, id_layer);
1744     if (ivilayer != NULL)
1745     {
1746         return ivilayer;
1747     }
1748
1749     ivilayer = calloc(1, sizeof *ivilayer);
1750     if (!ivilayer) {
1751         weston_log("no memory to allocate client layer\n");
1752         return NULL;
1753     }
1754
1755     init_properties(&ivilayer->prop);
1756     ivilayer->pending.prop = ivilayer->prop;
1757     ivilayer->id_layer = id_layer;
1758     ivilayer->shell = shell;
1759     ivilayer->event_mask = 0;
1760     wl_list_init(&ivilayer->link);
1761     wl_list_init(&ivilayer->pending.link);
1762     wl_list_init(&ivilayer->order.link);
1763     wl_list_init(&ivilayer->list_screen);
1764
1765     wl_list_init(&ivilayer->pending.list_surface);
1766     wl_list_init(&ivilayer->order.list_surface);
1767     wl_list_insert(&shell->list_layer, &ivilayer->link);
1768
1769     wl_list_for_each(controller, &shell->list_controller, link) {
1770         ivi_controller_send_layer(controller->resource,
1771                                   ivilayer->id_layer);
1772
1773         wl_list_for_each(ctrllayer, &shell->list_controller_layer, link) {
1774             if (ivilayer->id_layer != ctrllayer->id_layer) {
1775                 continue;
1776             }
1777             send_layer_event(ivilayer, ctrllayer->resource, PROP_EVENT_ALL);
1778         }
1779     }
1780
1781     return ivilayer;
1782 }
1783
1784 static void
1785 controller_layer_create(struct wl_client *client,
1786                         struct wl_resource *resource,
1787                         uint32_t id_layer,
1788                         int32_t width,
1789                         int32_t height,
1790                         uint32_t id)
1791 {
1792     struct ivicontroller *ctrl = wl_resource_get_user_data(resource);
1793     struct ivishell *shell = ctrl->shell;
1794     struct ivicontroller_layer *ctrllayer = NULL;
1795     struct ivilayer *ivilayer = NULL;
1796
1797 weston_log("controller_layer_create is called\n");
1798     ctrllayer = calloc(1, sizeof *ctrllayer);
1799     if (!ctrllayer) {
1800         weston_log("no memory to allocate client layer\n");
1801         return;
1802     }
1803
1804     ctrllayer->shell = shell;
1805     ctrllayer->client = client;
1806     ctrllayer->id = id;
1807     ctrllayer->id_layer = id_layer;
1808     ctrllayer->resource = wl_resource_create(client,
1809                                &ivi_controller_layer_interface, 1, id);
1810     if (ctrllayer->resource == NULL) {
1811         weston_log("couldn't layer object\n");
1812         return;
1813     }
1814
1815 weston_log("controller_layer_create is called\n");
1816     wl_list_init(&ctrllayer->link);
1817     wl_list_insert(&shell->list_controller_layer, &ctrllayer->link);
1818
1819     ivilayer = layer_create(client, ctrl, id_layer, width, height, id);
1820
1821     if (ivilayer != NULL) {
1822         wl_resource_set_implementation(ctrllayer->resource,
1823                                    &controller_layer_implementation,
1824                                    ivilayer, destroy_ivicontroller_layer);
1825     }
1826 }
1827
1828 static void
1829 controller_surface_create(struct wl_client *client,
1830                           struct wl_resource *resource,
1831                           uint32_t id_surface,
1832                           uint32_t id)
1833 {
1834     struct ivicontroller *ctrl = wl_resource_get_user_data(resource);
1835     struct ivishell *shell = ctrl->shell;
1836     struct ivisurface *ivisurf = NULL;
1837     struct ivicontroller_surface *ctrlsurf = NULL;
1838
1839     ctrlsurf = calloc(1, sizeof *ctrlsurf);
1840     if (!ctrlsurf) {
1841         weston_log("no memory to allocate controller surface\n");
1842         return;
1843     }
1844
1845     ctrlsurf->shell = shell;
1846     ctrlsurf->client = client;
1847     ctrlsurf->id = id;
1848     ctrlsurf->id_surface = id_surface;
1849     wl_list_init(&ctrlsurf->link);
1850     ctrlsurf->resource = wl_resource_create(client,
1851                                &ivi_controller_surface_interface, 1, id);
1852     if (ctrlsurf->resource == NULL) {
1853         weston_log("couldn't surface object");
1854         return;
1855     }
1856
1857     wl_list_for_each(ivisurf, &shell->list_surface, link) {
1858         if (ivisurf->id_surface == ctrlsurf->id_surface) {
1859             wl_resource_set_implementation(ctrlsurf->resource,
1860                                    &controller_surface_implementation,
1861                                    ivisurf, destroy_ivicontroller_surface);
1862             break;
1863         }
1864     }
1865 }
1866
1867 static const struct ivi_controller_interface controller_implementation = {
1868     controller_commit_changes,
1869     controller_layer_create,
1870     controller_surface_create
1871 };
1872
1873 static void
1874 westonsurface_destroy_from_ivisurface(struct wl_listener *listener, void *data)
1875 {
1876     struct ivisurface *ivisurf = container_of(listener,
1877                                                struct ivisurface,
1878                                                surface_destroy_listener);
1879     (void)data;
1880     ivisurf->surface = NULL;
1881 }
1882
1883 static void
1884 application_surface_create(struct wl_client *client,
1885                       struct wl_resource *resource,
1886                       uint32_t id_surface,
1887                       struct wl_resource *surface_resource,
1888                       uint32_t id)
1889 {
1890 weston_log("application_surface_create\n");
1891     struct ivishell *shell = wl_resource_get_user_data(resource);
1892     struct weston_surface *es = NULL;
1893     struct ivisurface *ivisurf = NULL;
1894     struct ivicontroller *controller = NULL;
1895     struct ivicontroller_surface *ctrlsurf = NULL;
1896
1897     es = wl_resource_get_user_data(surface_resource);
1898     if (es == NULL) {
1899         weston_log("application_surface_create: invalid surface\n");
1900     }
1901
1902     ivisurf = get_surface(&shell->list_surface, id_surface);
1903     if (ivisurf != NULL)
1904     {
1905         weston_log("id_surface is already created\n");
1906
1907         resource = wl_resource_create(client, &ivi_surface_interface, 1, id);
1908         ivisurf->surface = es;
1909         if (es != NULL) {
1910             ivisurf->surface_destroy_listener.notify =
1911                 westonsurface_destroy_from_ivisurface;
1912             wl_resource_add_destroy_listener(es->resource,
1913                           &ivisurf->surface_destroy_listener);
1914         }
1915         return;
1916     }
1917
1918     ivisurf = calloc(1, sizeof *ivisurf);
1919     if (ivisurf == NULL) {
1920         weston_log("no memory to allocate client surface\n");
1921         return;
1922     }
1923
1924     init_properties(&ivisurf->prop);
1925     ivisurf->pixelformat = IVI_CONTROLLER_SURFACE_PIXELFORMAT_RGBA_8888;
1926     ivisurf->pending.prop = ivisurf->prop;
1927     ivisurf->surface = es;
1928     if (es != NULL) {
1929         ivisurf->surface_destroy_listener.notify =
1930             westonsurface_destroy_from_ivisurface;
1931         wl_resource_add_destroy_listener(es->resource,
1932                       &ivisurf->surface_destroy_listener);
1933     }
1934     ivisurf->id_surface = id_surface;
1935     ivisurf->shell = shell;
1936     ivisurf->event_mask = 0;
1937
1938     wl_list_init(&ivisurf->surface_rotation.link);
1939     wl_list_init(&ivisurf->layer_rotation.link);
1940     wl_list_init(&ivisurf->surface_pos.link);
1941     wl_list_init(&ivisurf->layer_pos.link);
1942     wl_list_init(&ivisurf->scaling.link);
1943
1944     wl_list_init(&ivisurf->link);
1945     wl_list_init(&ivisurf->pending.link);
1946     wl_list_init(&ivisurf->order.link);
1947     wl_list_init(&ivisurf->list_layer);
1948     wl_list_insert(&shell->list_surface, &ivisurf->link);
1949
1950     wl_list_for_each(controller, &shell->list_controller, link) {
1951         ivi_controller_send_surface(controller->resource,
1952                                     ivisurf->id_surface);
1953
1954         wl_list_for_each(ctrlsurf, &shell->list_controller_surface, link) {
1955             if (ivisurf->id_surface != ctrlsurf->id_surface) {
1956                 continue;
1957             }
1958             send_surface_event(ivisurf, ctrlsurf->resource, PROP_EVENT_ALL);
1959         }
1960     }
1961
1962     resource = wl_resource_create(client, &ivi_surface_interface, 1, id);
1963     if (resource == NULL) {
1964         weston_log("couldn't surface object");
1965         return;
1966     }
1967     wl_resource_set_implementation(resource,
1968                                    &surface_implementation,
1969                                    ivisurf, NULL);
1970 }
1971
1972 static const struct ivi_application_interface application_implementation = {
1973     application_surface_create
1974 };
1975
1976 static void
1977 bind_ivi_application(struct wl_client *client, void *data,
1978                      uint32_t version, uint32_t id)
1979 {
1980     struct ivishell *shell = data;
1981     struct wl_resource *resource = NULL;
1982     (void)version;
1983
1984     resource = wl_resource_create(client, &ivi_application_interface, 1, id);
1985     wl_resource_set_implementation(resource,
1986                                    &application_implementation,
1987                                    shell, NULL);
1988 }
1989
1990 static void
1991 add_client_to_resources(struct ivishell *shell,
1992                         struct wl_client *client,
1993                         struct ivicontroller *controller)
1994 {
1995     struct ivisurface* ivisurf = NULL;
1996     struct ivilayer* ivilayer = NULL;
1997     struct iviscreen* iviscrn = NULL;
1998     struct ivicontroller_screen *ctrlscrn = NULL;
1999     struct wl_resource *resource_output = NULL;
2000
2001     wl_list_for_each(ivisurf, &shell->list_surface, link) {
2002         ivi_controller_send_surface(controller->resource,
2003                                     ivisurf->id_surface);
2004     }
2005
2006     wl_list_for_each(ivilayer, &shell->list_layer, link) {
2007         ivi_controller_send_layer(controller->resource,
2008                                   ivilayer->id_layer);
2009     }
2010
2011     wl_list_for_each(iviscrn, &shell->list_screen, link) {
2012         resource_output =
2013             wl_resource_find_for_client(&iviscrn->output->resource_list,
2014                                  client);
2015         if (resource_output == NULL) {
2016             continue;
2017         }
2018
2019         ctrlscrn = controller_screen_create(iviscrn->shell, client, iviscrn);
2020         if (ctrlscrn == NULL) {
2021             continue;
2022         }
2023
2024         ivi_controller_send_screen(controller->resource,
2025                                    wl_resource_get_id(resource_output),
2026                                    ctrlscrn->resource);
2027     }
2028 }
2029
2030 static void
2031 bind_ivi_controller(struct wl_client *client, void *data,
2032                     uint32_t version, uint32_t id)
2033 {
2034     struct ivishell *shell = data;
2035     struct ivicontroller *controller;
2036     (void)version;
2037
2038     controller = calloc(1, sizeof *controller);
2039     if (controller == NULL) {
2040         weston_log("no memory to allocate controller\n");
2041         return;
2042     }
2043
2044     controller->resource =
2045         wl_resource_create(client, &ivi_controller_interface, 1, id);
2046     wl_resource_set_implementation(controller->resource,
2047                                    &controller_implementation,
2048                                    controller, unbind_resource_controller);
2049
2050     controller->shell = shell;
2051     controller->client = client;
2052     controller->id = id;
2053
2054     wl_list_init(&controller->link);
2055     wl_list_insert(&shell->list_controller, &controller->link);
2056
2057     add_client_to_resources(shell, client, controller);
2058 }
2059
2060 static struct iviscreen*
2061 create_screen(struct ivishell *shell, struct weston_output *output)
2062 {
2063     struct iviscreen *iviscrn;
2064     iviscrn = calloc(1, sizeof *iviscrn);
2065     if (iviscrn == NULL) {
2066         weston_log("no memory to allocate client screen\n");
2067         return NULL;
2068     }
2069     iviscrn->shell = shell;
2070     iviscrn->output = output;
2071     iviscrn->event_mask = 0;
2072
2073     wl_list_init(&iviscrn->link);
2074     wl_list_init(&iviscrn->list_layer);
2075     wl_list_init(&iviscrn->pending.list_layer);
2076     wl_list_init(&iviscrn->order.list_layer);
2077     wl_list_init(&iviscrn->list_resource);
2078
2079     return iviscrn;
2080 }
2081
2082 static void
2083 init_ivi_shell(struct weston_compositor *ec, struct ivishell *shell)
2084 {
2085     struct weston_output *output = NULL;
2086     struct iviscreen *iviscrn = NULL;
2087     shell->compositor = ec;
2088
2089     wl_list_init(&ec->layer_list);
2090     wl_list_init(&shell->list_surface);
2091     wl_list_init(&shell->list_layer);
2092     wl_list_init(&shell->list_screen);
2093     wl_list_init(&shell->list_weston_surface);
2094     wl_list_init(&shell->list_controller);
2095     wl_list_init(&shell->list_controller_screen);
2096     wl_list_init(&shell->list_controller_layer);
2097     wl_list_init(&shell->list_controller_surface);
2098     shell->event_restriction = 0;
2099
2100     wl_list_for_each(output, &ec->output_list, link) {
2101         iviscrn = create_screen(shell, output);
2102         if (iviscrn != NULL) {
2103             wl_list_insert(&shell->list_screen, &iviscrn->link);
2104         }
2105         wl_list_init(&iviscrn->order.list_layer);
2106     }
2107 }
2108
2109 static void
2110 shell_destroy(struct wl_listener *listener, void *data)
2111 {
2112     struct ivishell *shell =
2113         container_of(listener, struct ivishell, destroy_listener);
2114     (void)data;
2115
2116     free(shell);
2117 }
2118
2119 static void
2120 ping_timer_destroy(struct shell_surface *shsurf)
2121 {
2122     if (!shsurf || !shsurf->ping_timer) {
2123         return;
2124     }
2125
2126     if (shsurf->ping_timer->source) {
2127         wl_event_source_remove(shsurf->ping_timer->source);
2128     }
2129
2130     free(shsurf->ping_timer);
2131     shsurf->ping_timer = NULL;
2132 }
2133
2134 static void
2135 destroy_shell_surface(struct shell_surface *shsurf)
2136 {
2137     wl_list_remove(&shsurf->surface_destroy_listener.link);
2138     shsurf->surface->configure = NULL;
2139     ping_timer_destroy(shsurf);
2140     free(shsurf->title);
2141
2142     wl_list_remove(&shsurf->link);
2143     free(shsurf);
2144 }
2145
2146 static void
2147 shell_handle_surface_destroy(struct wl_listener *listener, void *data)
2148 {
2149     struct shell_surface *shsurf = container_of(listener,
2150                                                struct shell_surface,
2151                                                surface_destroy_listener);
2152     (void)data;
2153
2154     if (wl_resource_get_client(shsurf->resource)) {
2155         wl_resource_destroy(shsurf->resource);
2156     } else {
2157         wl_resource_destroy(shsurf->resource);
2158         destroy_shell_surface(shsurf);
2159     }
2160 }
2161
2162 static void
2163 configure(struct weston_surface *surface,
2164           float x, float y, int32_t width, int32_t height)
2165 {
2166     weston_surface_configure(surface, x, y, width, height);
2167     if (surface->output) {
2168         weston_surface_update_transform(surface);
2169     }
2170 }
2171
2172 static void
2173 shell_surface_configure(struct weston_surface *es,
2174                         int32_t sx, int32_t sy,
2175                         int32_t width, int32_t height)
2176 {
2177     float from_x = 0.0f;
2178     float from_y = 0.0f;
2179     float to_x = 0.0f;
2180     float to_y = 0.0f;
2181     struct shell_surface *shsurf = get_shell_surface(es);
2182
2183     if ((width == 0) || (height == 0) || (shsurf == NULL)) {
2184         return;
2185     }
2186
2187     if (shsurf->width != width || shsurf->height != height) {
2188
2189         shsurf->width = width;
2190         shsurf->height = height;
2191
2192         weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2193         weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2194         configure(es,
2195                   es->geometry.x + to_x - from_x,
2196                   es->geometry.y + to_y - from_y,
2197                   width, height);
2198
2199         commit_changes(shsurf->shell);
2200     }
2201 }
2202
2203 static struct shell_surface *
2204 create_shell_surface(void *shell, struct weston_surface *surface,
2205                      const struct weston_shell_client *client)
2206 {
2207     struct shell_surface *shsurf;
2208
2209     if (surface->configure) {
2210         weston_log("surface->configure already set\n");
2211         return NULL;
2212     }
2213
2214     shsurf = calloc(1, sizeof *shsurf);
2215     if (shsurf == NULL) {
2216         weston_log("no memory to allocate shell surface\n");
2217         return NULL;
2218     }
2219
2220     surface->configure = shell_surface_configure;
2221     surface->configure_private = shsurf;
2222
2223     shsurf->shell = (struct ivishell *)shell;
2224     shsurf->surface = surface;
2225     shsurf->ping_timer = NULL;
2226     shsurf->title = strdup("");
2227
2228     /* init link so its safe to always remove it in destroy_shell_surface */
2229     wl_list_init(&shsurf->link);
2230
2231     shsurf->client = client;
2232
2233     return shsurf;
2234 }
2235
2236 static void
2237 send_configure(struct weston_surface *surface,
2238                uint32_t edges, int32_t width, int32_t height)
2239 {
2240     struct shell_surface *shsurf = get_shell_surface(surface);
2241
2242     wl_shell_surface_send_configure(shsurf->resource,
2243                                     edges, width, height);
2244 }
2245
2246 static const struct weston_shell_client shell_client = {
2247     send_configure
2248 };
2249
2250 static void
2251 shell_destroy_shell_surface(struct wl_resource *resource)
2252 {
2253     struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2254
2255     destroy_shell_surface(shsurf);
2256 }
2257
2258 static void
2259 shell_surface_pong(struct wl_client *client,
2260                    struct wl_resource *resource, uint32_t serial)
2261 {
2262     struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2263     (void)client;
2264
2265     if (shsurf->ping_timer == NULL) {
2266         return;
2267     }
2268
2269     if (shsurf->ping_timer->serial == serial) {
2270         ping_timer_destroy(shsurf);
2271     }
2272 }
2273
2274 static void
2275 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
2276                    struct wl_resource *seat_resource, uint32_t serial)
2277 {
2278     (void)client;
2279     (void)resource;
2280     (void)seat_resource;
2281     (void)serial;
2282 }
2283
2284 static void
2285 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
2286                      struct wl_resource *seat_resource, uint32_t serial,
2287                      uint32_t edges)
2288 {
2289     (void)client;
2290     (void)resource;
2291     (void)seat_resource;
2292     (void)serial;
2293     (void)edges;
2294 }
2295
2296 static void
2297 shell_surface_set_toplevel(struct wl_client *client,
2298                            struct wl_resource *resource)
2299 {
2300     (void)client;
2301     (void)resource;
2302 }
2303
2304 static void
2305 shell_surface_set_transient(struct wl_client *client,
2306                             struct wl_resource *resource,
2307                             struct wl_resource *parent_resource,
2308                             int x, int y, uint32_t flags)
2309 {
2310     (void)client;
2311     (void)resource;
2312     (void)parent_resource;
2313     (void)x;
2314     (void)y;
2315     (void)flags;
2316 }
2317
2318 static void
2319 shell_surface_set_fullscreen(struct wl_client *client,
2320                              struct wl_resource *resource,
2321                              uint32_t method,
2322                              uint32_t framerate,
2323                              struct wl_resource *output_resource)
2324 {
2325     (void)client;
2326     (void)resource;
2327     (void)method;
2328     (void)framerate;
2329     (void)output_resource;
2330 }
2331
2332 static void
2333 shell_surface_set_popup(struct wl_client *client,
2334                         struct wl_resource *resource,
2335                         struct wl_resource *seat_resource,
2336                         uint32_t serial,
2337                         struct wl_resource *parent_resource,
2338                         int32_t x, int32_t y, uint32_t flags)
2339 {
2340     (void)client;
2341     (void)resource;
2342     (void)seat_resource;
2343     (void)serial;
2344     (void)parent_resource;
2345     (void)x;
2346     (void)y;
2347     (void)flags;
2348 }
2349
2350 static void
2351 shell_surface_set_maximized(struct wl_client *client,
2352                             struct wl_resource *resource,
2353                             struct wl_resource *output_resource)
2354 {
2355     (void)client;
2356     (void)resource;
2357     (void)output_resource;
2358 }
2359
2360 static void
2361 shell_surface_set_title(struct wl_client *client,
2362                         struct wl_resource *resource, const char *title)
2363 {
2364     (void)client;
2365     (void)resource;
2366     (void)title;
2367 }
2368
2369 static void
2370 shell_surface_set_class(struct wl_client *client,
2371                         struct wl_resource *resource, const char *class)
2372 {
2373     struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2374     (void)client;
2375
2376     free(shsurf->class);
2377     shsurf->class = strdup(class);
2378 }
2379
2380 static const struct wl_shell_surface_interface
2381 shell_surface_implementation = {
2382     shell_surface_pong,
2383     shell_surface_move,
2384     shell_surface_resize,
2385     shell_surface_set_toplevel,
2386     shell_surface_set_transient,
2387     shell_surface_set_fullscreen,
2388     shell_surface_set_popup,
2389     shell_surface_set_maximized,
2390     shell_surface_set_title,
2391     shell_surface_set_class
2392 };
2393
2394 static void
2395 shell_weston_surface_destroy(struct wl_listener *listener, void *data)
2396 {
2397     struct link_shell_weston_surface *lsurf =
2398         container_of(listener,
2399                      struct link_shell_weston_surface,
2400                      destroy_listener);
2401     (void)data;
2402
2403     wl_list_remove(&lsurf->link);
2404     free(lsurf);
2405 }
2406
2407
2408 static void
2409 shell_get_shell_surface(struct wl_client *client,
2410                         struct wl_resource *resource,
2411                         uint32_t id,
2412                         struct wl_resource *surface_resource)
2413 {
2414     struct weston_surface *surface =
2415         wl_resource_get_user_data(surface_resource);
2416     struct ivishell *shell = wl_resource_get_user_data(resource);
2417     struct shell_surface *shsurf;
2418     struct link_shell_weston_surface *lsurf;
2419     pid_t pid;
2420     uid_t uid;
2421     gid_t gid;
2422
2423     if (get_shell_surface(surface)) {
2424         wl_resource_post_error(surface_resource,
2425             WL_DISPLAY_ERROR_INVALID_OBJECT,
2426             "ivishell::get_shell_surface already requested");
2427         return;
2428     }
2429
2430     shsurf = create_shell_surface(shell, surface, &shell_client);
2431     if (!shsurf) {
2432         wl_resource_post_error(surface_resource,
2433                                WL_DISPLAY_ERROR_INVALID_OBJECT,
2434                                "surface->configure already set");
2435         return;
2436     }
2437
2438     shsurf->resource = wl_resource_create(client,
2439                                           &wl_shell_surface_interface,
2440                                           1, id);
2441     wl_resource_set_implementation(shsurf->resource,
2442                                    &shell_surface_implementation,
2443                                    shsurf, shell_destroy_shell_surface);
2444
2445     shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
2446     wl_resource_add_destroy_listener(surface->resource,
2447                   &shsurf->surface_destroy_listener);
2448
2449     lsurf = (struct link_shell_weston_surface*)malloc(sizeof *lsurf);
2450     lsurf->surface = surface;
2451     wl_list_init(&lsurf->link);
2452     wl_list_insert(&shell->list_weston_surface, &lsurf->link);
2453
2454     lsurf->destroy_listener.notify = shell_weston_surface_destroy;
2455     wl_resource_add_destroy_listener(surface->resource,
2456                                      &lsurf->destroy_listener);
2457
2458     wl_client_get_credentials(client, &pid, &uid, &gid);
2459     shsurf->pid = pid;
2460 }
2461
2462 static const struct wl_shell_interface shell_implementation = {
2463     shell_get_shell_surface
2464 };
2465
2466 static void
2467 bind_shell(struct wl_client *client, void *data,
2468            uint32_t version, uint32_t id)
2469 {
2470     struct ivishell *shell = data;
2471     struct wl_resource *resource = NULL;
2472     (void)version;
2473
2474     resource = wl_resource_create(client, &wl_shell_interface, 1, id);
2475     wl_resource_set_implementation(resource,
2476                                    &shell_implementation,
2477                                    shell, NULL);
2478 }
2479
2480 WL_EXPORT int
2481 module_init(struct weston_compositor *ec,
2482             int *argc, char *argv[])
2483 {
2484     struct weston_seat *seat;
2485     struct ivishell *shell;
2486     (void)argc;
2487     (void)argv;
2488
2489     shell = malloc(sizeof *shell);
2490     if (shell == NULL)
2491         return -1;
2492
2493     memset(shell, 0, sizeof *shell);
2494     init_ivi_shell(ec, shell);
2495
2496     shell->destroy_listener.notify = shell_destroy;
2497     wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
2498
2499     if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
2500                               shell, bind_shell) == NULL) {
2501         return -1;
2502     }
2503
2504     if (wl_global_create(ec->wl_display, &ivi_application_interface, 1,
2505                   shell, bind_ivi_application) == NULL)
2506         return -1;
2507
2508     if (wl_global_create(ec->wl_display, &ivi_controller_interface, 1,
2509                   shell, bind_ivi_controller) == NULL)
2510         return -1;
2511
2512     wl_list_for_each(seat, &ec->seat_list, link) {
2513         if (seat) {
2514             shell->seat = seat;
2515         }
2516     }
2517
2518     return 0;
2519 }