tinyds: enable protocol_trace only when protocol_trace_init() returns true
[platform/core/uifw/libds-tizen.git] / examples / tinyds-tdm.c
1 #include <assert.h>
2 #include <stdbool.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <signal.h>
6 #include <time.h>
7
8 #include <drm_fourcc.h>
9 #include <pixman.h>
10 #include <wayland-server.h>
11 #include <libds/log.h>
12 #include <libds/backend.h>
13 #include <libds/output.h>
14 #include <libds/compositor.h>
15 #include <libds/xdg_shell.h>
16 #include <libds-tizen/allocator/tbm.h>
17 #include <libds-tizen/backend/tdm.h>
18 #include <libds/backend/libinput.h>
19 #include <libds-tizen/tbm_server.h>
20 #include <libds-tizen/dpms.h>
21 #include <libds/input_device.h>
22 #include <libds/keyboard.h>
23 #include <libds/touch.h>
24 #include <libds/pointer.h>
25 #include <libds/seat.h>
26 #include <libds-tizen/input_devicemgr.h>
27 #include <xkbcommon/xkbcommon.h>
28 #include <libds/interfaces/keyboard.h>
29 #include <libds-tizen/launch.h>
30 #include <libds-tizen/backend/tdm_output_hwc.h>
31 #include <libds-tizen/input_method.h>
32 #include <libds-tizen/text_input.h>
33 #include <libds-tizen/policy.h>
34
35 #define USE_TDM_BUFFER_QUEUE
36
37 #ifdef USE_TDM_BUFFER_QUEUE
38 #include "pixman-tbm-helper.h"
39 #include "tinyds-tdm-renderer.h"
40 #else
41 #include <libds/swapchain.h>
42 #endif
43
44 #include "pixman-helper.h"
45 #include "protocol-trace.h"
46
47 #define TINYDS_UNUSED   __attribute__((unused))
48 struct tinyds_keyboard;
49 struct tinyds_pointer;
50
51 struct tinyds_output
52 {
53     struct tinyds_server *server;
54     struct ds_output *ds_output;
55     struct ds_allocator *allocator;
56 #ifdef USE_TDM_BUFFER_QUEUE
57     struct tinyds_renderer renderer;
58     struct ds_tdm_buffer_queue *buffer_queue;
59     struct wl_listener buffer_queue_acquirable;
60 #else
61     struct ds_swapchain *swapchain;
62 #endif
63     struct ds_buffer *front_buffer;
64
65     struct wl_listener output_destroy;
66     struct wl_listener output_frame;
67
68     int width, height;
69
70     struct wl_event_source *idle_commit;
71     bool committable;
72     bool damaged;
73     bool target_updated;
74
75     struct ds_tdm_output_hwc *hwc;
76     struct ds_tdm_output_hwc_window *bg_hwc_window;
77 };
78
79 struct tinyds_dpms
80 {
81     struct ds_tizen_dpms *ds_dpms;
82     struct tinyds_server *server;
83
84     struct wl_listener destroy;
85     struct wl_listener set_dpms;
86     struct wl_listener get_dpms;
87 };
88
89 struct tinyds_policy
90 {
91     struct ds_tizen_policy *policy;
92
93     struct wl_listener destroy;
94     struct wl_listener new_surface;
95     struct wl_listener activate_below_by_univeral_id;
96     struct wl_listener lower_by_universal_id;
97     struct wl_listener set_transient_for;
98     struct wl_listener unset_transient_for;
99     struct wl_listener place_subsurface_below_parent;
100     struct wl_listener set_subsurface_stand_alone;
101     struct wl_listener set_background_state;
102     struct wl_listener unset_background_state;
103     struct wl_listener add_activate_above_by_universal_id;
104     struct wl_listener set_appid;
105     struct wl_listener set_transient_for_below;
106
107     struct wl_list policy_surfaces;
108 };
109
110 struct tinyds_policy_surface
111 {
112     struct ds_tizen_policy_surface *policy_surface;
113
114     struct wl_listener destroy;
115     struct wl_listener new_visibility;
116     struct wl_listener new_position;
117     struct wl_listener activate;
118     struct wl_listener raise;
119     struct wl_listener lower;
120     struct wl_listener set_focus_skip;
121     struct wl_listener unset_focus_skip;
122     struct wl_listener set_role;
123     struct wl_listener set_window_type;
124     struct wl_listener set_conformant;
125     struct wl_listener unset_conformant;
126     struct wl_listener get_conformant;
127     struct wl_listener set_notification_level;
128     struct wl_listener set_window_screen_mode;
129     struct wl_listener get_subsurface;
130     struct wl_listener iconify;
131     struct wl_listener uniconify;
132     struct wl_listener add_aux_hint;
133     struct wl_listener change_aux_hint;
134     struct wl_listener delete_aux_hint;
135     struct wl_listener get_supported_aux_hints;
136     struct wl_listener set_floating_mode;
137     struct wl_listener unset_floating_mode;
138     struct wl_listener set_stack_mode;
139     struct wl_listener new_subsurface_watcher;
140     struct wl_listener set_parent;
141     struct wl_listener ack_conformant_region;
142     struct wl_listener set_video;
143     struct wl_listener show;
144     struct wl_listener hide;
145     struct wl_listener set_parent_with_below;
146
147     struct wl_list visibilities;
148     struct wl_list positions;
149     struct wl_list subsurface_watchers;
150
151     struct wl_list link; //tinyds_policy::policy_surfaces
152 };
153
154 struct tinyds_policy_visibility
155 {
156     struct ds_tizen_policy_visibility *visibility;
157
158     struct wl_listener destroy;
159
160     struct wl_list link; //tinyds_policy::visibilities
161 };
162
163 struct tinyds_policy_position
164 {
165     struct ds_tizen_policy_position *position;
166
167     struct wl_listener destroy;
168     struct wl_listener set;
169
170     struct wl_list link; //tinyds_policy::positions
171 };
172
173 struct tinyds_policy_subsurface_watcher
174 {
175     struct ds_tizen_policy_subsurface_watcher *subsurface_watcher;
176
177     struct wl_listener destroy;
178
179     struct wl_list link; //tinyds_policy::subsurface_watchers
180 };
181
182 struct tinyds_server
183 {
184     struct ds_tbm_server *tbm_server;
185
186     struct wl_display *display;
187
188     struct ds_backend *backend;
189     struct ds_backend *input_backend;
190     struct ds_compositor *compositor;
191     struct ds_xdg_shell *xdg_shell;
192     struct ds_seat *seat;
193     uint32_t seat_caps;
194     double output_x, output_y;
195     struct ds_tizen_input_devicemgr *devicemgr;
196     struct ds_tizen_launch_effect *effect;
197     struct ds_tizen_launch_splash *splash;
198
199     struct tinyds_output *output;
200     struct tinyds_dpms *dpms;
201     struct tinyds_policy *policy;
202
203     struct wl_event_source *stdin_source;
204
205     struct wl_list views;
206
207     struct wl_listener new_output;
208     struct wl_listener new_input;
209     struct wl_listener new_xdg_surface;
210     struct wl_listener devicemgr_destroy;
211     struct wl_listener pointer_warp;
212     struct wl_listener effect_destroy;
213     struct wl_listener effect_type_set;
214     struct wl_listener effect_type_unset;
215     struct wl_listener new_splash;
216     struct wl_listener splash_owner;
217
218     struct wl_list keyboards;
219     struct wl_list pointers;
220
221     struct tinyds_text_input *text_input;
222     struct tinyds_input_method *input_method;
223 };
224
225 struct tinyds_view
226 {
227     struct tinyds_server *server;
228
229     struct tinyds_texture *texture;
230     struct ds_xdg_surface *xdg_surface;
231
232     struct wl_listener xdg_surface_map;
233     struct wl_listener xdg_surface_unmap;
234     struct wl_listener xdg_surface_destroy;
235     struct wl_listener surface_commit;
236     struct wl_list link; // tinyds_server::views
237
238     struct ds_tdm_output_hwc_window *hwc_window;
239
240     int x, y;
241     bool mapped;
242
243     pid_t pid;
244     int effect_type;
245 };
246
247 struct tinyds_pointer
248 {
249     struct ds_input_device *dev;
250     struct tinyds_server *server;
251
252     struct tinyds_view *focused_view;
253
254     struct wl_listener destroy;
255     struct wl_listener motion; //relative
256     struct wl_listener button;
257     struct wl_listener frame;
258     struct wl_list link; //tinyds_server::pointers
259 };
260
261 struct tinyds_keyboard
262 {
263     struct ds_input_device *dev;
264     struct tinyds_server *server;
265
266     struct wl_listener destroy;
267     struct wl_listener key;
268     struct wl_list link; //tinyds_server::keyboards
269 };
270
271 struct tinyds_touch
272 {
273     struct ds_input_device *dev;
274     struct tinyds_server *server;
275
276     struct wl_listener destroy;
277     struct wl_listener down;
278     struct wl_listener up;
279     struct wl_listener motion;
280 };
281
282 struct tinyds_text_input {
283     struct ds_tizen_text_input *input;
284     struct ds_tizen_text_input_manager *text_input_mgr;
285
286     struct tinyds_server *server;
287     struct ds_surface *surface;
288
289     struct wl_list input_methods;
290
291     struct wl_listener mgr_destroy;
292     struct wl_listener new_text_input;
293
294     struct wl_listener destroy;
295     struct wl_listener text_input_activate;
296     struct wl_listener text_input_deactivate;
297     struct wl_listener text_input_reset;
298     struct wl_listener text_input_set_content_type;
299     struct wl_listener text_input_invoke_action;
300     struct wl_listener text_input_commit_state;
301     struct wl_listener text_input_set_preferred_language;
302 };
303
304 struct tinyds_input_method {
305     struct ds_tizen_input_method *input_method;
306     struct ds_tizen_input_method_manager *input_method_mgr;
307
308     struct tinyds_server *server;
309     struct tinyds_text_input *input;
310     struct tinyds_input_method_context *context;
311
312     struct wl_list link;
313
314     struct wl_listener destroy;
315     struct wl_listener mgr_destroy;
316 };
317
318 struct tinyds_input_method_context {
319     struct ds_tizen_input_method_context *context;
320
321     struct tinyds_server *server;
322     struct tinyds_text_input *input;
323     struct tinyds_input_method *input_method;
324
325     struct wl_listener destroy;
326
327     struct wl_listener im_context_commit_string;
328     struct wl_listener im_context_preedit_string;
329     struct wl_listener im_context_preedit_styling;
330     struct wl_listener im_context_preedit_cursor;
331     struct wl_listener im_context_delete_surrounding_text;
332     struct wl_listener im_context_cursor_position;
333     struct wl_listener im_context_modifiers_map;
334     struct wl_listener im_context_keysym;
335     struct wl_listener im_context_grab_keyboard;
336     struct wl_listener im_context_key;
337     struct wl_listener im_context_modifiers;
338     struct wl_listener im_context_language;
339     struct wl_listener im_context_text_direction;
340 };
341
342 struct tinyds_server tinyds;
343
344 static bool init_server(struct tinyds_server *server, struct wl_display *display);
345 static int server_dispatch_stdin(int fd, uint32_t mask, void *data);
346 static void output_handle_destroy(struct wl_listener *listener, void *data);
347 static void output_handle_frame(struct wl_listener *listener, void *data);
348 static void draw_server_with_damage(struct tinyds_server *server);
349 static void draw_output(struct tinyds_output *output);
350 static void output_swap_buffer(struct tinyds_output *output,
351         struct ds_buffer *buffer);
352 static void view_send_frame_done(struct tinyds_view *view);
353 static void output_hwc_init(struct tinyds_output *output);
354 static void output_schedule_commit(struct tinyds_output *output);
355 static void output_commit(struct tinyds_output *output);
356 #ifdef USE_TDM_BUFFER_QUEUE
357 static void output_buffer_queue_init(struct tinyds_output *output);
358 static void output_renderer_init(struct tinyds_output *output);
359 static void output_draw_with_renderer(struct tinyds_output *output);
360 #else
361 static void output_swapchain_init(struct tinyds_output *output,
362         int width, int height, uint32_t format);
363 static void output_draw_with_swapchain(struct tinyds_output *output);
364 static void draw_view(struct tinyds_view *view, pixman_image_t *dst_image);
365 #endif
366 static void dpms_handle_destroy(struct wl_listener *listener, void *data);
367 static void dpms_handle_set_dpms(struct wl_listener *listener, void *data);
368 static void dpms_handle_get_dpms(struct wl_listener *listener, void *data);
369 static void server_add_keyboard(struct tinyds_server *server,
370         struct ds_input_device *dev);
371 static void server_add_pointer(struct tinyds_server *server,
372         struct ds_input_device *dev);
373 static void server_add_touch(struct tinyds_server *server,
374         struct ds_input_device *dev);
375 static bool new_policy(struct tinyds_server *server);
376 static struct tinyds_view *
377 server_view_at(struct tinyds_server *server, double lx, double ly,
378         double *sx, double *sy);
379
380 static bool add_new_text_input(struct tinyds_server *server);
381 static bool add_new_input_method(struct tinyds_server *server);
382 static bool add_new_input_method_context(
383         struct tinyds_input_method *input_method,
384         struct tinyds_text_input *text_input);
385
386 static void text_input_mgr_handle_destroy(struct wl_listener *listener,
387         void *data TINYDS_UNUSED);
388 static void text_input_mgr_handle_new_text_input(struct wl_listener *listener,
389         void *data TINYDS_UNUSED);
390
391 static void input_method_mgr_handle_destroy(struct wl_listener *listener,
392         void *data TINYDS_UNUSED);
393
394 static void input_method_handle_destroy(struct wl_listener *listener,
395         void *data TINYDS_UNUSED);
396
397 int
398 main(void)
399 {
400     struct tinyds_server *server = &tinyds;
401     struct wl_display *display;
402     struct wl_event_loop *loop;
403     const char *socket;
404     bool res;
405
406     ds_log_init(DS_INF, NULL);
407
408     display = wl_display_create();
409     assert(display);
410
411     res = init_server(server, display);
412     assert(res);
413
414     socket = wl_display_add_socket_auto(display);
415     assert(socket);
416
417     ds_backend_start(server->backend);
418     ds_backend_start(server->input_backend);
419
420     setenv("WAYLAND_DISPLAY", socket, true);
421
422     ds_inf("Running Wayland compositor on WAYLAND_DISPLAY=%s", socket);
423
424     loop = wl_display_get_event_loop(display);
425     server->stdin_source = wl_event_loop_add_fd(loop, STDIN_FILENO,
426             WL_EVENT_READABLE, server_dispatch_stdin, server);
427
428     wl_display_run(display);
429
430     protocol_trace_enable(false);
431     protocol_trace_fini();
432
433     wl_display_destroy_clients(display);
434     wl_display_destroy(display);
435
436     return 0;
437 }
438
439 static void
440 view_populate_pid(struct tinyds_view *view)
441 {
442     pid_t pid;
443     struct wl_client *client = NULL;
444     struct ds_surface *surface;
445
446     surface = ds_xdg_surface_get_surface(view->xdg_surface);
447     if (!surface)
448         return;
449
450     client = wl_resource_get_client(ds_surface_get_wl_resource(surface));
451     if (!client)
452         return;
453
454     wl_client_get_credentials(client, &pid, NULL, NULL);
455
456     ds_inf("view pid(%u)", pid);
457     view->pid = pid;
458
459     view->effect_type = ds_tizen_launch_effect_get_effect_type(view->server->effect, pid);
460     ds_tizen_launch_effect_unset_effect_type(view->server->effect, pid);
461     ds_inf("view effect_type(%d)", view->effect_type);
462 }
463
464 static void
465 view_handle_xdg_surface_map(struct wl_listener *listener,
466         void *data TINYDS_UNUSED)
467 {
468     struct tinyds_view *view;
469     struct ds_keyboard *keyboard;
470     struct tinyds_keyboard *kbd;
471
472     view = wl_container_of(listener, view, xdg_surface_map);
473     view->mapped = true;
474
475     view_populate_pid(view);
476
477     wl_list_for_each(kbd, &view->server->keyboards, link) {
478         keyboard = ds_input_device_get_keyboard(kbd->dev);
479         if (keyboard != NULL) {
480             ds_seat_keyboard_notify_enter(view->server->seat,
481                     ds_xdg_surface_get_surface(view->xdg_surface),
482                     keyboard->keycodes, keyboard->num_keycodes,
483                     &keyboard->modifiers);
484             return;
485         }
486     }
487 }
488
489 static void
490 view_handle_xdg_surface_unmap(struct wl_listener *listener,
491         void *data TINYDS_UNUSED)
492 {
493     struct tinyds_view *view;
494
495     view = wl_container_of(listener, view, xdg_surface_unmap);
496     view->mapped = false;
497 }
498
499 static void
500 view_handle_xdg_surface_destroy(struct wl_listener *listener,
501         void *data TINYDS_UNUSED) 
502 {
503     struct tinyds_view *view;
504
505     view = wl_container_of(listener, view, xdg_surface_destroy);
506
507     draw_server_with_damage(view->server);
508
509     ds_tdm_output_hwc_window_destroy(view->hwc_window);
510
511     wl_list_remove(&view->xdg_surface_destroy.link);
512     wl_list_remove(&view->xdg_surface_map.link);
513     wl_list_remove(&view->xdg_surface_unmap.link);
514     wl_list_remove(&view->surface_commit.link);
515     wl_list_remove(&view->link);
516     free(view);
517 }
518
519 static void
520 view_handle_surface_commit(struct wl_listener *listener,
521         void *data TINYDS_UNUSED)
522 {
523     struct tinyds_view *view;
524
525     view = wl_container_of(listener, view, surface_commit);
526     draw_server_with_damage(view->server);
527 }
528
529 static void
530 server_new_xdg_surface(struct wl_listener *listener, void *data)
531 {
532     static unsigned int seedx = 1;
533     static unsigned int seedy = 43210;
534     struct tinyds_server *server;
535     struct tinyds_view *view;
536     struct ds_xdg_surface *xdg_surface;
537
538     server = wl_container_of(listener, server, new_xdg_surface);
539     xdg_surface = data;
540
541     ds_inf("New xdg_surface(%p)", (void *)xdg_surface);
542
543     view = calloc(1, sizeof *view);
544     assert(view);
545
546     view->server = server;
547     view->xdg_surface = xdg_surface;
548
549     view->xdg_surface_map.notify = view_handle_xdg_surface_map;
550     ds_xdg_surface_add_map_listener(xdg_surface,
551             &view->xdg_surface_map);
552
553     view->xdg_surface_unmap.notify = view_handle_xdg_surface_unmap;
554     ds_xdg_surface_add_unmap_listener(xdg_surface,
555             &view->xdg_surface_unmap);
556
557     view->xdg_surface_destroy.notify = view_handle_xdg_surface_destroy;
558     ds_xdg_surface_add_destroy_listener(xdg_surface,
559             &view->xdg_surface_destroy);
560
561     view->surface_commit.notify = view_handle_surface_commit;
562     ds_surface_add_commit_listener(
563             ds_xdg_surface_get_surface(xdg_surface),
564             &view->surface_commit);
565
566     view->x = rand_r(&seedx) % 1000;
567     view->y = rand_r(&seedy) % 500;
568
569     view->hwc_window = ds_tdm_output_hwc_window_create(server->output->hwc);
570     assert(view->hwc_window);
571
572     wl_list_insert(server->views.prev, &view->link);
573
574     view->pid = 0;
575     view->effect_type = -1;
576
577     ds_inf("view at (%d, %d)", view->x, view->y);
578 }
579
580 static void
581 backend_handle_new_output(struct wl_listener *listener, void *data)
582 {
583     struct tinyds_server *server;
584     struct tinyds_output *output;
585     struct ds_output *ds_output;
586     const struct ds_output_mode *mode;
587     struct ds_tdm_box src_box;
588
589     server = wl_container_of(listener, server, new_output);
590     ds_output = data;
591
592     ds_inf("New output(%p)", ds_output);
593
594     if (server->output)
595         return;
596
597     mode = ds_output_get_preferred_mode(ds_output);
598     ds_output_set_mode(ds_output, mode);
599
600     output = calloc(1, sizeof *output);
601     if (!output)
602         return;
603
604     output->server = server;
605     output->ds_output = ds_output;
606     output->width = mode->width;
607     output->height = mode->height;
608     output->damaged = true;
609     output->committable = true;
610
611     output_hwc_init(output);
612
613 #ifdef USE_TDM_BUFFER_QUEUE
614     output_buffer_queue_init(output);
615     output_renderer_init(output);
616 #else
617     output_swapchain_init(output, mode->width, mode->height,
618             DRM_FORMAT_XRGB8888);
619 #endif
620
621     output->bg_hwc_window = ds_tdm_output_hwc_window_create(output->hwc);
622     assert(output->bg_hwc_window);
623
624     src_box.x = 0;
625     src_box.y = 0;
626     src_box.width = output->width;
627     src_box.height = output->height;
628
629     ds_tdm_output_hwc_window_set_src_box(output->bg_hwc_window, &src_box);
630     ds_tdm_output_hwc_window_set_position(output->bg_hwc_window, 0, 0);
631     ds_tdm_output_hwc_window_set_dest_size(output->bg_hwc_window, output->width, output->height);
632     ds_tdm_output_hwc_window_set_transform(output->bg_hwc_window, WL_OUTPUT_TRANSFORM_NORMAL);
633
634     output->output_destroy.notify = output_handle_destroy;
635     ds_output_add_destroy_listener(ds_output, &output->output_destroy);
636
637     output->output_frame.notify = output_handle_frame;
638     ds_output_add_frame_listener(ds_output, &output->output_frame);
639
640     ds_tizen_input_devicemgr_set_output_width_height(server->devicemgr, (uint32_t)output->width, (uint32_t)output->height);
641
642     server->output = output;
643
644     output_schedule_commit(output);
645 }
646
647 static bool
648 add_new_dpms(struct tinyds_server *server)
649 {
650     struct tinyds_dpms *dpms;
651
652     dpms = calloc(1, sizeof *dpms);
653     if (!dpms)
654         return false;
655
656     dpms->ds_dpms = ds_tizen_dpms_create(server->display);
657     if (!dpms->ds_dpms) {
658         free(dpms);
659         ds_err("Could not create ds_tizen_dpms");
660         return false;
661     }
662
663     dpms->destroy.notify = dpms_handle_destroy;
664     ds_tizen_dpms_add_destroy_listener(dpms->ds_dpms, &dpms->destroy);
665
666     dpms->set_dpms.notify = dpms_handle_set_dpms;
667     ds_tizen_dpms_add_set_dpms_listener(dpms->ds_dpms, &dpms->set_dpms);
668
669     dpms->get_dpms.notify = dpms_handle_get_dpms;
670     ds_tizen_dpms_add_get_dpms_listener(dpms->ds_dpms, &dpms->get_dpms);
671
672     server->dpms = dpms;
673
674     ds_inf("Dpms (%p) added", dpms);
675
676     return true;
677 }
678
679 static void
680 backend_handle_new_input(struct wl_listener *listener, void *data)
681 {
682     struct tinyds_server *server;
683     struct ds_input_device *dev = data;
684     enum ds_input_device_type dev_type;
685
686     server = wl_container_of(listener, server, new_input);
687
688     dev_type = ds_input_device_get_type(dev);
689
690     switch (dev_type) {
691         case DS_INPUT_DEVICE_KEYBOARD:
692             server_add_keyboard(server, dev);
693             server->seat_caps |= WL_SEAT_CAPABILITY_KEYBOARD;
694             break;
695         case DS_INPUT_DEVICE_TOUCH:
696             server_add_touch(server, dev);
697             server->seat_caps |= WL_SEAT_CAPABILITY_TOUCH;
698             break;
699         case DS_INPUT_DEVICE_POINTER:
700             server_add_pointer(server, dev);
701             server->seat_caps |= WL_SEAT_CAPABILITY_POINTER;
702             break;
703         default:
704             ds_err("Unknown type(%d) of ds_input_device", dev_type);
705             break;
706     }
707
708     ds_seat_set_capabilities(server->seat, server->seat_caps);
709 }
710
711 static void
712 devicemgr_add_keymap_data(struct wl_list *list, const char *name, int keycode)
713 {
714     struct ds_tizen_input_devicemgr_keymap_data *data;
715
716     data = calloc(1, sizeof *data);
717     if (!data) {
718         ds_err("Failed to alloc memory");
719         return;
720     }
721
722     data->name = strdup(name);
723     data->keycode = keycode;
724
725     wl_list_insert(list, &data->link);
726 }
727
728 static void
729 devicemgr_cleanup_keymap_list(struct wl_list *list)
730 {
731     struct ds_tizen_input_devicemgr_keymap_data *data, *tmp;
732
733     wl_list_for_each_safe(data, tmp, list, link) {
734         wl_list_remove(&data->link);
735         free(data->name);
736         free(data);
737     }
738 }
739
740 static void
741 devicemgr_set_keymap(struct ds_tizen_input_devicemgr *devicemgr)
742 {
743     struct wl_list keymap_list;
744     bool res;
745
746     wl_list_init(&keymap_list);
747
748     devicemgr_add_keymap_data(&keymap_list, "XF86VolumeRaise", 455);
749     devicemgr_add_keymap_data(&keymap_list, "XF86VolumeLower", 456);
750     devicemgr_add_keymap_data(&keymap_list, "XF86LightOn", 457);
751     devicemgr_add_keymap_data(&keymap_list, "XF86LightOff", 458);
752
753     res = ds_tizen_input_devicemgr_set_keymap_list(devicemgr, &keymap_list);
754     if (!res)
755         ds_inf("Failed to set keymap");
756
757     devicemgr_cleanup_keymap_list(&keymap_list);
758 }
759
760 static void
761 devicemgr_handle_pointer_warp(struct wl_listener *listener, void *data)
762 {
763     struct tinyds_server *server;
764     struct tinyds_pointer *pointer;
765     struct ds_tizen_input_devicemgr_event_pointer_warp *event = data;
766     double sx = 0.f, sy = 0.f;
767     struct tinyds_view *view = NULL;
768
769     server = wl_container_of(listener, server, pointer_warp);
770
771     ds_inf("Pointer warp: surface(%p) x(%.2f) y(%.2f)", event->surface,
772             event->x, event->y);
773
774     wl_list_for_each(pointer, &server->pointers, link){
775         if (!pointer->focused_view) continue;
776         view = pointer->focused_view;
777     }
778     if (!view) return;
779
780     if (event->surface != ds_xdg_surface_get_surface(view->xdg_surface)) {
781         ds_inf("Pointer is not on the requested surface");
782         return;
783     }
784
785     server->output_x = view->x + (event->x * server->output->width);
786     server->output_y = view->y + (event->y * server->output->height);
787
788     server_view_at(server, server->output_x, server->output_y, &sx, &sy);
789
790     ds_inf("notify motion: sx:%.2f sy:%.2f, output_x:%.1f, output_y:%.1f",
791             sx, sy, server->output_x, server->output_y);
792
793     ds_seat_pointer_notify_motion(server->seat,
794             event->time_msec, sx, sy);
795 }
796
797 static void
798 devicemgr_handle_destroy(struct wl_listener *listener, void *data TINYDS_UNUSED)
799 {
800     struct tinyds_server *server =
801         wl_container_of(listener, server, devicemgr_destroy);
802
803     wl_list_remove(&server->devicemgr_destroy.link);
804     wl_list_remove(&server->pointer_warp.link);
805
806     server->devicemgr = NULL;
807 }
808
809 static void
810 launch_effect_handle_destroy(struct wl_listener *listener, void *data TINYDS_UNUSED)
811 {
812     struct tinyds_server *server =
813         wl_container_of(listener, server, effect_destroy);
814
815     wl_list_remove(&server->effect_destroy.link);
816     wl_list_remove(&server->effect_type_set.link);
817     wl_list_remove(&server->effect_type_unset.link);
818     wl_list_remove(&server->new_splash.link);
819
820     server->effect = NULL;
821 }
822
823 static void
824 launch_effect_handle_type_set(struct wl_listener *listener, void *data)
825 {
826     struct tinyds_server *server;
827     struct ds_tizen_launch_effect_event_type_set *event = data;
828     struct tinyds_view *view = NULL;
829     bool existing = false;
830
831     server = wl_container_of(listener, server, effect_type_set);
832
833     ds_inf("Launch effect. type_set: pid(%u) type:%s", event->pid, (event->effect_type == 1) ? "depth-in" : "launch");
834
835     wl_list_for_each(view, &server->views, link) {
836         if (view->pid == event->pid) {
837             view->effect_type = event->effect_type;
838             ds_inf("Launch effect. existing pid");
839             existing = true;
840         }
841     }
842     if (existing) {
843         ds_tizen_launch_effect_unset_effect_type(server->effect, event->pid);
844     } else {
845         ds_tizen_launch_effect_set_effect_type(server->effect, event->pid, event->effect_type);
846     }
847 }
848
849 static void
850 launch_effect_handle_type_unset(struct wl_listener *listener, void *data)
851 {
852     struct tinyds_server *server;
853     struct ds_tizen_launch_effect_event_type_unset *event = data;
854     struct tinyds_view *view = NULL;
855
856     server = wl_container_of(listener, server, effect_type_unset);
857
858     ds_inf("Launch effect. type_unset: pid(%u)", event->pid);
859
860     wl_list_for_each(view, &server->views, link) {
861         if (view->pid == event->pid) {
862             view->effect_type = -1;
863             ds_inf("Launch effect. pid found");
864         }
865     }
866     ds_tizen_launch_effect_unset_effect_type(server->effect, event->pid);
867 }
868
869 static void
870 launch_splash_handle_owner(struct wl_listener *listener, void *data)
871 {
872     struct tinyds_server *server;
873     struct ds_tizen_launch_splash_event_owner *event = data;
874     struct tinyds_view *view = NULL;
875
876     server = wl_container_of(listener, server, splash_owner);
877
878     ds_inf("Splash owner. pid(%u)", event->pid);
879
880     wl_list_for_each(view, &server->views, link) {
881         if (view->pid == event->pid) {
882             if (event->pid == ds_tizen_launch_splash_get_pid(server->splash))
883                 ;//
884             else {
885                 ds_tizen_launch_splash_set_pid(server->splash, event->pid);
886             }
887         }
888     }
889 }
890
891 static void
892 launch_effect_handle_new_splash(struct wl_listener *listener, void *data)
893 {
894     struct tinyds_server *server;
895     struct ds_tizen_launch_splash *splash = data;
896     struct tinyds_view *view = NULL;
897
898     server = wl_container_of(listener, server, new_splash);
899
900     ds_inf("Launch new splash. splash(%p)", splash);
901     if (!splash) return;
902
903     server->splash = splash;
904
905     // new view for "Launchscreen"
906     view = calloc(1, sizeof *view);
907     assert(view);
908     wl_list_insert(server->views.prev, &view->link);
909     view->pid = ds_tizen_launch_splash_get_pid(splash);
910
911     server->splash_owner.notify = launch_splash_handle_owner;
912     ds_tizen_launch_splash_add_owner_listener(server->splash,
913             &server->splash_owner);
914 }
915
916 static bool
917 init_server(struct tinyds_server *server, struct wl_display *display)
918 {
919     server->display = display;
920
921     wl_list_init(&server->views);
922
923     if (wl_display_init_shm(display) != 0)
924         return false;
925
926     server->backend = ds_tdm_backend_create(display);
927     if (!server->backend)
928         return false;
929
930     server->input_backend = ds_libinput_backend_create(display);
931     if (!server->input_backend) {
932         ds_backend_destroy(server->backend);
933         return false;
934     }
935
936     server->new_output.notify = backend_handle_new_output;
937     ds_backend_add_new_output_listener(server->backend,
938             &server->new_output);
939
940     wl_list_init(&server->keyboards);
941     wl_list_init(&server->pointers);
942     server->new_input.notify = backend_handle_new_input;
943     ds_backend_add_new_input_listener(server->input_backend, &server->new_input);
944
945     server->compositor = ds_compositor_create(display);
946     if (!server->compositor)
947         goto err;
948
949     server->tbm_server = ds_tbm_server_create(display);
950     if (!server->tbm_server)
951         goto err;
952
953     server->xdg_shell = ds_xdg_shell_create(display);
954     if (!server->xdg_shell)
955         goto err;
956
957     server->new_xdg_surface.notify = server_new_xdg_surface;
958     ds_xdg_shell_add_new_surface_listener(server->xdg_shell,
959             &server->new_xdg_surface);
960
961     if (!add_new_dpms(server))
962         goto err;
963
964     if (!new_policy(server))
965         goto err;
966
967     server->seat = ds_seat_create(display, "seat0" /* arbitrary name */);
968     if (!server->seat)
969         goto err;
970     server->seat_caps = 0;
971
972     server->devicemgr = ds_tizen_input_devicemgr_create(
973             server->input_backend, server->seat);
974     if (!server->devicemgr) {
975         ds_err("Could not create ds_tizen_input_devicemgr");
976         goto err;
977     }
978
979     devicemgr_set_keymap(server->devicemgr);
980
981     server->devicemgr_destroy.notify = devicemgr_handle_destroy;
982     ds_tizen_input_devicemgr_add_destroy_listener(server->devicemgr,
983             &server->devicemgr_destroy);
984
985     server->pointer_warp.notify = devicemgr_handle_pointer_warp;
986     ds_tizen_input_devicemgr_add_pointer_warp_listener(server->devicemgr,
987             &server->pointer_warp);
988
989     server->effect = ds_tizen_launch_effect_create(display);
990     if (!server->effect) {
991         goto err;
992     }
993
994     server->effect_destroy.notify = launch_effect_handle_destroy;
995     ds_tizen_launch_effect_add_destroy_listener(server->effect,
996             &server->effect_destroy);
997
998     server->effect_type_set.notify = launch_effect_handle_type_set;
999     ds_tizen_launch_effect_add_type_set_listener(server->effect,
1000             &server->effect_type_set);
1001
1002     server->effect_type_unset.notify = launch_effect_handle_type_unset;
1003     ds_tizen_launch_effect_add_type_unset_listener(server->effect,
1004             &server->effect_type_unset);
1005
1006     server->new_splash.notify = launch_effect_handle_new_splash;
1007     ds_tizen_launch_effect_add_new_splash_listener(server->effect,
1008             &server->new_splash);
1009
1010     if (!add_new_text_input(server))
1011         goto err;
1012
1013     if (!add_new_input_method(server))
1014         goto err;
1015
1016     if (protocol_trace_init(display))
1017         protocol_trace_enable(true);
1018
1019     return true;
1020
1021 err:
1022     ds_backend_destroy(server->backend);
1023     ds_backend_destroy(server->input_backend);
1024
1025     return false;
1026 }
1027
1028 static void
1029 output_handle_destroy(struct wl_listener *listener, void *data TINYDS_UNUSED)
1030 {
1031     struct tinyds_output *output =
1032         wl_container_of(listener, output, output_destroy);
1033
1034     if (output->bg_hwc_window)
1035         ds_tdm_output_hwc_window_destroy(output->bg_hwc_window);
1036
1037     wl_list_remove(&output->output_destroy.link);
1038     wl_list_remove(&output->output_frame.link);
1039
1040     if (output->front_buffer)
1041         ds_buffer_unlock(output->front_buffer);
1042
1043 #ifdef USE_TDM_BUFFER_QUEUE
1044     fini_renderer(&output->renderer);
1045 #else
1046     if (output->swapchain)
1047         ds_swapchain_destroy(output->swapchain);
1048
1049     if (output->allocator)
1050         ds_allocator_destroy(output->allocator);
1051 #endif
1052
1053     wl_display_terminate(output->server->display);
1054
1055     output->server->output = NULL;
1056
1057     free(output);
1058 }
1059
1060 static void
1061 output_commit(struct tinyds_output *output)
1062 {
1063     uint32_t num_changed = 0;
1064     uint32_t num_windows = 0, current_num_windows = 0;
1065     struct ds_tdm_output_hwc_window **composited_hwc_windows = NULL;
1066     struct ds_tdm_output_hwc_window **changed_hwc_windows = NULL;
1067     enum ds_tdm_output_hwc_window_composition composition;
1068     struct tinyds_view *view;
1069     int i;
1070     bool need_target = false;
1071     bool fully_obscured = false;
1072     struct ds_buffer *ds_buffer;
1073     struct ds_tdm_box src_box;
1074     int w = 0, h = 0;
1075
1076     if (!output->committable)
1077         return;
1078
1079     if (!output->damaged && !output->target_updated)
1080         return;
1081
1082     wl_list_for_each_reverse(view, &output->server->views, link) {
1083         if (!view->hwc_window)
1084             continue;
1085
1086         ds_buffer = ds_surface_get_buffer(
1087                 ds_xdg_surface_get_surface(view->xdg_surface));
1088         if (!ds_buffer)
1089             continue;
1090
1091         if (!view->mapped)
1092             continue;
1093
1094         num_windows++;
1095
1096         ds_buffer_get_size(ds_buffer, &w, &h);
1097
1098         if ((output->width <= w) && (output->height <= h))
1099             fully_obscured = true;
1100     }
1101
1102     if (fully_obscured) {
1103         ds_tdm_output_hwc_window_set_composition(output->bg_hwc_window,
1104                 DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_NONE);
1105     } else {
1106         ds_tdm_output_hwc_window_set_composition(output->bg_hwc_window,
1107                 DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_CLIENT);
1108         num_windows++;
1109         need_target = true;
1110     }
1111
1112     if (num_windows) {
1113         composited_hwc_windows = calloc(num_windows, sizeof *composited_hwc_windows);
1114         if (!composited_hwc_windows)
1115             return;
1116
1117         wl_list_for_each_reverse(view, &output->server->views, link) {
1118             if (!view->hwc_window)
1119                 continue;
1120
1121             ds_buffer = ds_surface_get_buffer(
1122                     ds_xdg_surface_get_surface(view->xdg_surface));
1123             if (!ds_buffer)
1124                 continue;
1125
1126             ds_tdm_output_hwc_window_set_buffer(view->hwc_window, ds_buffer);
1127
1128             ds_buffer_get_size(ds_buffer, &w, &h);
1129
1130             src_box.x = 0;
1131             src_box.y = 0;
1132             src_box.width = w;
1133             src_box.height = h;
1134
1135             ds_tdm_output_hwc_window_set_src_box(view->hwc_window, &src_box);
1136             ds_tdm_output_hwc_window_set_position(view->hwc_window, view->x, view->y);
1137             ds_tdm_output_hwc_window_set_dest_size(view->hwc_window, w, h);
1138             ds_tdm_output_hwc_window_set_transform(view->hwc_window, WL_OUTPUT_TRANSFORM_NORMAL);
1139
1140             if (view->mapped) {
1141                 ds_tdm_output_hwc_window_set_composition(view->hwc_window,
1142                         DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_DEVICE);
1143
1144                 composited_hwc_windows[current_num_windows] = view->hwc_window;
1145                 current_num_windows++;
1146             } else {
1147                 ds_tdm_output_hwc_window_set_composition(view->hwc_window,
1148                         DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_NONE);
1149             }
1150         }
1151
1152         if (!fully_obscured) {
1153             composited_hwc_windows[current_num_windows] = output->bg_hwc_window;
1154             current_num_windows++;
1155         }
1156     }
1157
1158     if (!ds_tdm_output_hwc_validate(output->hwc, composited_hwc_windows,
1159             num_windows, &num_changed)) {
1160         free(composited_hwc_windows);
1161         ds_err("Could not hwc validate");
1162         return;
1163     }
1164
1165     if (composited_hwc_windows)
1166         free(composited_hwc_windows);
1167
1168     if (num_changed > 0) {
1169         changed_hwc_windows = calloc(num_windows, sizeof *changed_hwc_windows);
1170         if (!changed_hwc_windows)
1171             return;
1172
1173         if (!ds_tdm_output_hwc_get_changed_composition(output->hwc, &num_changed,
1174                 changed_hwc_windows)) {
1175             free(changed_hwc_windows);
1176             ds_err("Could not get chaged composition");
1177             return;
1178         }
1179
1180         for (i = 0; i < num_changed; i++) {
1181             composition = ds_tdm_output_hwc_window_get_composition(changed_hwc_windows[i]);
1182             if (composition == DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_CLIENT) {
1183                 need_target = true;
1184                 break;
1185             }
1186         }
1187     }
1188
1189     if (changed_hwc_windows)
1190         free(changed_hwc_windows);
1191
1192     if (need_target && output->damaged)
1193         draw_output(output);
1194
1195 #ifdef USE_TDM_BUFFER_QUEUE
1196     struct ds_buffer *buffer;
1197
1198     buffer = ds_tdm_buffer_queue_acquire(output->buffer_queue);
1199     if (buffer) {
1200         if (!ds_tdm_output_hwc_set_client_target_buffer(output->hwc, buffer)) {
1201             ds_err("Could not set hwc client target buffer");
1202             return;
1203         }
1204
1205         output_swap_buffer(output, buffer);
1206     }
1207 #endif
1208
1209     if (!ds_tdm_output_hwc_accept_validation(output->hwc)) {
1210         ds_err("Could not hwc accept validateion");
1211         return;
1212     }
1213
1214     ds_output_commit(output->ds_output);
1215
1216     output->committable = false;
1217     output->damaged = false;
1218     output->target_updated = false;
1219
1220     wl_list_for_each(view, &output->server->views, link) {
1221         enum ds_tdm_output_hwc_window_composition composition;
1222
1223         if (!view->mapped)
1224             continue;
1225
1226         composition = ds_tdm_output_hwc_window_get_composition(view->hwc_window);
1227         if ((composition == DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_DEVICE) ||
1228             (composition == DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_VIDEO) ||
1229             (composition == DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_CURSOR))
1230             view_send_frame_done(view);
1231     }
1232
1233     ds_dbg("output:%p commit", output);
1234 }
1235
1236 static void
1237 output_handle_frame(struct wl_listener *listener, void *data TINYDS_UNUSED)
1238 {
1239     struct tinyds_output *output =
1240         wl_container_of(listener, output, output_frame);
1241
1242     ds_dbg("output:%p handle frame", output);
1243
1244     output->committable = true;
1245
1246     output_commit(output);
1247 }
1248
1249 static void
1250 draw_server_with_damage(struct tinyds_server *server)
1251 {
1252     server->output->damaged = true;
1253     output_schedule_commit(server->output);
1254 }
1255
1256 static void
1257 output_hwc_init(struct tinyds_output *output)
1258 {
1259     struct ds_tdm_output *tdm_output;
1260
1261     tdm_output = ds_tdm_output_from_output(output->ds_output);
1262     assert(tdm_output);
1263
1264     output->hwc = ds_tdm_output_get_hwc(tdm_output);
1265     assert(output->hwc);
1266
1267     ds_tdm_output_hwc_set_enabled(output->hwc, true);
1268 }
1269
1270 #ifdef USE_TDM_BUFFER_QUEUE
1271 static void
1272 output_handle_buffer_queue_acquirable(struct wl_listener *listener,
1273         void *data TINYDS_UNUSED)
1274 {
1275     struct tinyds_output *output;
1276
1277     output = wl_container_of(listener, output, buffer_queue_acquirable);
1278
1279     output->target_updated = true;
1280     output_schedule_commit(output);
1281 }
1282
1283 static void
1284 output_buffer_queue_init(struct tinyds_output *output)
1285 {
1286     struct ds_tdm_output *tdm_output;
1287
1288     tdm_output = ds_tdm_output_from_output(output->ds_output);
1289     assert(tdm_output);
1290
1291     output->buffer_queue = ds_tdm_output_get_buffer_queue(tdm_output);
1292     assert(output->buffer_queue);
1293
1294     output->buffer_queue_acquirable.notify =
1295         output_handle_buffer_queue_acquirable;
1296     ds_tdm_buffer_queue_add_acquirable_listener(output->buffer_queue,
1297             &output->buffer_queue_acquirable);
1298 }
1299
1300 static void
1301 output_renderer_init(struct tinyds_output *output)
1302 {
1303     init_renderer(&output->renderer);
1304
1305     renderer_set_surface_queue(&output->renderer,
1306             ds_tdm_buffer_queue_get_native_queue(output->buffer_queue));
1307
1308     renderer_set_bg_color(&output->renderer, 80, 80, 80);
1309 }
1310
1311 static void
1312 output_draw_with_renderer(struct tinyds_output *output)
1313 {
1314     struct tinyds_view *view;
1315
1316     ds_dbg(">> BEGIN UPDATE TEXTURES");
1317
1318     wl_list_for_each(view, &output->server->views, link) {
1319         struct ds_buffer *ds_buffer;
1320         struct ds_tbm_client_buffer *tbm_buffer;
1321         tbm_surface_h surface;
1322         enum ds_tdm_output_hwc_window_composition composition;
1323
1324         if (!view->mapped)
1325             continue;
1326
1327         composition = ds_tdm_output_hwc_window_get_composition(view->hwc_window);
1328         if ((composition == DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_DEVICE) ||
1329             (composition == DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_VIDEO) ||
1330             (composition == DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_CURSOR))
1331             continue;
1332
1333         ds_buffer = ds_surface_get_buffer(
1334                 ds_xdg_surface_get_surface(view->xdg_surface));
1335         if (!ds_buffer)
1336             continue;
1337
1338         tbm_buffer = ds_tbm_client_buffer_from_buffer(ds_buffer);
1339         if (!tbm_buffer)
1340             continue;
1341
1342         surface = ds_tbm_client_buffer_get_tbm_surface(tbm_buffer);
1343         if (!surface)
1344             continue;
1345
1346         renderer_add_texture(&output->renderer, surface, view->x, view->y);
1347
1348         view_send_frame_done(view);
1349     }
1350
1351     ds_dbg("<< END UPDATE TEXTURES");
1352
1353     renderer_draw(&output->renderer);
1354 }
1355 #else
1356 static void
1357 output_swapchain_init(struct tinyds_output *output,
1358         int width, int height, uint32_t format)
1359
1360 {
1361     output->allocator = ds_tbm_allocator_create();
1362     assert(output->allocator);
1363
1364     output->swapchain = ds_swapchain_create(output->allocator,
1365             width, height, format);
1366     assert(output->swapchain);
1367 }
1368
1369 static void
1370 output_draw_with_swapchain(struct tinyds_output *output)
1371 {
1372     struct tinyds_view *view;
1373     struct ds_buffer *output_buffer;
1374     pixman_image_t *output_image;
1375     enum ds_tdm_output_hwc_window_composition composition;
1376
1377     output_buffer = ds_swapchain_acquire(output->swapchain, NULL);
1378     if (!output_buffer)
1379         return;
1380
1381     output_image = pixman_image_from_buffer(output_buffer,
1382             DS_BUFFER_DATA_PTR_ACCESS_WRITE);
1383     if (!output_image) {
1384         ds_buffer_unlock(output_buffer);
1385         return;
1386     }
1387
1388     pixman_image_fill_color(output_image, 80, 80, 80);
1389
1390     wl_list_for_each(view, &output->server->views, link) {
1391         if (!view->mapped)
1392             continue;
1393
1394         composition = ds_tdm_output_hwc_window_get_composition(view->hwc_window);
1395         if ((composition == DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_DEVICE) ||
1396             (composition == DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_VIDEO) ||
1397             (composition == DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_CURSOR))
1398             continue;
1399
1400         draw_view(view, output_image);
1401     }
1402     pixman_image_unref(output_image);
1403
1404     if (!ds_tdm_output_hwc_set_client_target_buffer(output->hwc, output_buffer)) {
1405         ds_err("Could not set hwc client target buffer");
1406         ds_buffer_unlock(output_buffer);
1407         return;
1408     }
1409
1410     output_swap_buffer(output, output_buffer);
1411 }
1412
1413 static void
1414 draw_view(struct tinyds_view *view, pixman_image_t *dst_image)
1415 {
1416     struct ds_buffer *buffer;
1417     pixman_image_t *src_image;
1418
1419     buffer = ds_surface_get_buffer(
1420             ds_xdg_surface_get_surface(view->xdg_surface));
1421     if (!buffer)
1422         return;
1423
1424     src_image = pixman_image_from_buffer(buffer,
1425             DS_BUFFER_DATA_PTR_ACCESS_READ);
1426     pixman_image_composite32(PIXMAN_OP_OVER,
1427             src_image,
1428             NULL,
1429             dst_image,
1430             0, 0, 0, 0,
1431             view->x, view->y,
1432             pixman_image_get_width(src_image),
1433             pixman_image_get_height(src_image));
1434     pixman_image_unref(src_image);
1435
1436     view_send_frame_done(view);
1437 }
1438 #endif
1439
1440 static void
1441 draw_output(struct tinyds_output *output)
1442 {
1443 #ifdef USE_TDM_BUFFER_QUEUE
1444     output_draw_with_renderer(output);
1445 #else
1446     output_draw_with_swapchain(output);
1447 #endif
1448
1449     ds_dbg("output:%p draw", output);
1450 }
1451
1452 static void
1453 output_swap_buffer(struct tinyds_output *output, struct ds_buffer *buffer)
1454 {
1455     ds_output_attach_buffer(output->ds_output, buffer);
1456
1457     if (output->front_buffer)
1458         ds_buffer_unlock(output->front_buffer);
1459     output->front_buffer = buffer;
1460 }
1461
1462 static void
1463 view_send_frame_done(struct tinyds_view *view)
1464 {
1465     struct timespec now;
1466     clock_gettime(CLOCK_MONOTONIC, &now);
1467     ds_surface_send_frame_done(ds_xdg_surface_get_surface(view->xdg_surface),
1468             &now);
1469 }
1470
1471 static int
1472 server_dispatch_stdin(int fd, uint32_t mask, void *data)
1473 {
1474     struct tinyds_server *server = data;
1475
1476     wl_display_terminate(server->display);
1477
1478     return 1;
1479 }
1480
1481 static void
1482 dpms_handle_destroy(struct wl_listener *listener, void *data)
1483 {
1484     struct tinyds_dpms *dpms;
1485
1486     dpms = wl_container_of(listener, dpms, destroy);
1487
1488     ds_inf("Dpms(%p) destroyed", dpms);
1489
1490     wl_list_remove(&dpms->destroy.link);
1491     wl_list_remove(&dpms->set_dpms.link);
1492     wl_list_remove(&dpms->get_dpms.link);
1493
1494     free(dpms);
1495 }
1496
1497 static void
1498 dpms_handle_set_dpms(struct wl_listener *listener, void *data)
1499 {
1500     struct tinyds_dpms *dpms;
1501     struct ds_tizen_dpms_event *event = data;
1502
1503     dpms = wl_container_of(listener, dpms, set_dpms);
1504
1505     ds_inf("Dpms(%p) set dpms : %d", dpms, event->mode);
1506
1507     //To do
1508     //set dpms mode to output
1509     ds_tizen_dpms_send_set_result(dpms->ds_dpms, event->mode,
1510         DS_TIZEN_DPMS_ERROR_NONE);
1511 }
1512
1513 static void
1514 dpms_handle_get_dpms(struct wl_listener *listener, void *data)
1515 {
1516     struct tinyds_dpms *dpms;
1517
1518     dpms = wl_container_of(listener, dpms, get_dpms);
1519
1520     ds_inf("Dpms(%p) get dpms", dpms);
1521
1522     //To do
1523     //get dpms mode from output
1524     ds_tizen_dpms_send_get_result(dpms->ds_dpms, DS_TIZEN_DPMS_MODE_ON,
1525         DS_TIZEN_DPMS_ERROR_NONE);
1526 }
1527
1528 static void
1529 keyboard_handle_device_destroy(struct wl_listener *listener, void *data)
1530 {
1531     struct tinyds_keyboard *kbd;
1532
1533     kbd = wl_container_of(listener, kbd, destroy);
1534
1535     ds_inf("Keyboard(%p) destroyed", kbd);
1536
1537     wl_list_remove(&kbd->destroy.link);
1538     wl_list_remove(&kbd->key.link);
1539     wl_list_remove(&kbd->link);
1540
1541     free(kbd);
1542 }
1543
1544 static bool
1545 server_handle_keybinding(struct tinyds_server *server, xkb_keysym_t sym)
1546 {
1547     switch (sym) {
1548         case XKB_KEY_BackSpace:
1549             wl_display_terminate(server->display);
1550             break;
1551         default:
1552             return false;
1553     }
1554
1555     return true;
1556 }
1557
1558 static void
1559 keyboard_handle_key(struct wl_listener *listener, void *data)
1560 {
1561     struct tinyds_keyboard *kbd;
1562     struct ds_event_keyboard_key *event = data;
1563     struct ds_keyboard *ds_keyboard;
1564     struct xkb_state *xkb_state;
1565     const xkb_keysym_t *syms;
1566     int nsyms;
1567     bool handled = false;
1568
1569     kbd = wl_container_of(listener, kbd, key);
1570
1571     ds_inf("Keyboard(%p) event key: keycode(%d), state(%d), time_msec(%d), "
1572             "update_state(%d)", kbd->dev,
1573             event->keycode, event->state, event->time_msec,
1574             event->update_state);
1575
1576     ds_keyboard = ds_input_device_get_keyboard(kbd->dev);
1577
1578     if (event->state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1579         xkb_state = ds_keyboard_get_xkb_state(ds_keyboard);
1580         if (xkb_state) {
1581             nsyms = xkb_state_key_get_syms(xkb_state, event->keycode + 8,
1582                     &syms);
1583             for (int i = 0; i < nsyms; i++) {
1584                 handled = server_handle_keybinding(kbd->server, syms[i]);
1585             }
1586         }
1587     }
1588
1589     if (!handled) {
1590         ds_seat_keyboard_notify_key(kbd->server->seat, event->time_msec,
1591                 event->keycode, event->state);
1592     }
1593 }
1594
1595 static void
1596 server_add_keyboard(struct tinyds_server *server, struct ds_input_device *dev)
1597 {
1598     struct tinyds_keyboard *kbd;
1599     struct xkb_context *context;
1600     struct xkb_keymap *keymap;
1601
1602     kbd = calloc(1, sizeof *kbd);
1603     assert(kbd);
1604
1605     kbd->dev = dev;
1606     kbd->server = server;
1607
1608     context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
1609     if (!context)
1610         goto err;
1611
1612     keymap = xkb_keymap_new_from_names(context, NULL,
1613             XKB_KEYMAP_COMPILE_NO_FLAGS);
1614
1615     if (!keymap) {
1616         ds_err("Failed to compile keymap");
1617         xkb_context_unref(context);
1618         goto err;
1619     }
1620
1621     ds_keyboard_set_keymap(ds_input_device_get_keyboard(dev), keymap);
1622
1623     xkb_keymap_unref(keymap);
1624     xkb_context_unref(context);
1625
1626     kbd->destroy.notify = keyboard_handle_device_destroy;
1627     ds_input_device_add_destroy_listener(dev, &kbd->destroy);
1628
1629     kbd->key.notify = keyboard_handle_key;
1630     ds_keyboard_add_key_listener(ds_input_device_get_keyboard(dev), &kbd->key);
1631
1632     wl_list_insert(&server->keyboards, &kbd->link);
1633
1634     ds_inf("Keyboard(%p) added", kbd);
1635
1636     return;
1637
1638 err:
1639     free(kbd);
1640 }
1641
1642 static struct tinyds_view *
1643 server_view_at(struct tinyds_server *server, double lx, double ly,
1644         double *sx, double *sy)
1645 {
1646     struct tinyds_view *view;
1647     struct ds_surface *surface;
1648     struct ds_buffer *buffer;
1649     int x, y, w = 0, h = 0;
1650
1651     wl_list_for_each(view, &server->views, link) {
1652         surface = ds_xdg_surface_get_surface(view->xdg_surface);
1653         buffer = ds_surface_get_buffer(surface);
1654         ds_buffer_get_size(buffer, &w, &h);
1655
1656         x = view->x;
1657         y = view->y;
1658
1659         if (lx >= x && lx <= x + w && ly >= y && ly <= y + h) {
1660             *sx = lx - x;
1661             *sy = ly - y;
1662
1663             return view;
1664         }
1665     }
1666
1667     return NULL;
1668 }
1669
1670 static void
1671 touch_handle_device_destroy(struct wl_listener *listener, void *data)
1672 {
1673     struct tinyds_touch *touch;
1674
1675     touch = wl_container_of(listener, touch, destroy);
1676
1677     ds_inf("Touch(%p) destroyed", touch);
1678
1679     wl_list_remove(&touch->destroy.link);
1680     wl_list_remove(&touch->down.link);
1681     wl_list_remove(&touch->up.link);
1682     wl_list_remove(&touch->motion.link);
1683
1684     free(touch);
1685 }
1686
1687 static void
1688 touch_handle_down(struct wl_listener *listener, void *data)
1689 {
1690     struct ds_event_touch_down *event = data;
1691     struct tinyds_touch *touch;
1692     struct tinyds_view *view;
1693     struct tinyds_server *server;
1694     double sx = 0.f, sy = 0.f;
1695
1696     touch = wl_container_of(listener, touch, down);
1697
1698     server = touch->server;
1699     server->output_x = event->x * server->output->width;
1700     server->output_y = event->y * server->output->height;
1701
1702     ds_inf("Touch(%p) event down: id(%d) x %.3f y %.3f output_x %.1f output_y %.1f",
1703             touch->dev, event->id, event->x, event->y, server->output_x, server->output_y);
1704
1705     view = server_view_at(server, server->output_x, server->output_y, &sx, &sy);
1706
1707     if (view) {
1708         ds_seat_touch_notify_down(touch->server->seat, ds_xdg_surface_get_surface(view->xdg_surface),
1709                 event->time_msec, event->id, sx, sy);
1710     }
1711 }
1712
1713 static void
1714 touch_handle_up(struct wl_listener *listener, void *data)
1715 {
1716     struct ds_event_touch_up *event = data;
1717     struct tinyds_touch *touch;
1718
1719     touch = wl_container_of(listener, touch, up);
1720
1721     ds_inf("Touch(%p) event up: id(%d) time_msec(%d)",
1722             touch->dev, event->id, event->time_msec);
1723
1724     ds_seat_touch_notify_up(touch->server->seat, event->time_msec, event->id);
1725 }
1726
1727 static void
1728 touch_handle_motion(struct wl_listener *listener, void *data)
1729 {
1730     struct ds_event_touch_motion *event = data;
1731     struct tinyds_touch *touch;
1732     struct tinyds_view *view;
1733     struct tinyds_server *server;
1734     double sx = 0.f, sy = 0.f;
1735
1736     touch = wl_container_of(listener, touch, motion);
1737
1738     server = touch->server;
1739     server->output_x = event->x * server->output->width;
1740     server->output_y = event->y * server->output->height;
1741
1742     ds_inf("Touch(%p) event motion: id(%d) x %.3f y %.3f output_x %.1f output_y %.1f",
1743             touch->dev, event->id, event->x, event->y, server->output_x, server->output_y);
1744
1745     view = server_view_at(server, server->output_x, server->output_y, &sx, &sy);
1746
1747     if (view) {
1748         ds_seat_touch_notify_motion(server->seat, event->time_msec,
1749                 event->id, sx, sy);
1750     }
1751 }
1752
1753 static void
1754 server_add_touch(struct tinyds_server *server, struct ds_input_device *dev)
1755 {
1756     struct tinyds_touch *touch;
1757
1758     touch = calloc(1, sizeof *touch);
1759     assert(touch);
1760
1761     touch->dev = dev;
1762     touch->server = server;
1763
1764     touch->destroy.notify = touch_handle_device_destroy;
1765     ds_input_device_add_destroy_listener(dev, &touch->destroy);
1766
1767     touch->down.notify = touch_handle_down;
1768     ds_touch_add_down_listener(ds_input_device_get_touch(dev), &touch->down);
1769
1770     touch->up.notify = touch_handle_up;
1771     ds_touch_add_up_listener(ds_input_device_get_touch(dev), &touch->up);
1772
1773     touch->motion.notify = touch_handle_motion;
1774     ds_touch_add_motion_listener(ds_input_device_get_touch(dev), &touch->motion);
1775
1776     ds_inf("Touch(%p) added", touch);
1777 }
1778
1779 static void
1780 pointer_handle_device_destroy(struct wl_listener *listener, void *data)
1781 {
1782     struct tinyds_pointer *pointer;
1783
1784     pointer = wl_container_of(listener, pointer, destroy);
1785
1786     ds_inf("Pointer(%p) destroyed", pointer);
1787
1788     wl_list_remove(&pointer->destroy.link);
1789     wl_list_remove(&pointer->motion.link);
1790     wl_list_remove(&pointer->button.link);
1791     wl_list_remove(&pointer->frame.link);
1792     wl_list_remove(&pointer->link);
1793
1794     free(pointer);
1795 }
1796
1797 static void
1798 pointer_handle_motion(struct wl_listener *listener, void *data)
1799 {
1800     struct tinyds_pointer *pointer;
1801     struct ds_event_pointer_motion *event = data;
1802     struct tinyds_view *view;
1803     struct tinyds_server *server;
1804     int ow = 0, oh = 0;
1805     double sx, sy;
1806
1807     pointer = wl_container_of(listener, pointer, motion);
1808
1809     server = pointer->server;
1810     if (server->output) {
1811         ow = server->output->width;
1812         oh = server->output->height;
1813     }
1814
1815     if (server->output_x + event->delta_x >= ow)
1816         server->output_x = ow;
1817     else if(server->output_x + event->delta_x <= 0.f)
1818         server->output_x = 0.f;
1819     else
1820         server->output_x = server->output_x + event->delta_x ;
1821     if (server->output_y + event->delta_y >= oh)
1822         server->output_y = oh;
1823     else if(server->output_y + event->delta_y <= 0.f)
1824         server->output_y = 0.f;
1825     else
1826         server->output_y = server->output_y + event->delta_y ;
1827
1828     ds_inf("Pointer(%p) motion: (delta_x %.1f delta_y %.1f) output_x %.1f output_y %.1f",
1829             pointer, event->delta_x, event->delta_y, server->output_x, server->output_y);
1830
1831     view = server_view_at(pointer->server, server->output_x, server->output_y, &sx, &sy);
1832
1833     if (pointer->focused_view != view) {
1834         if (pointer->focused_view) {
1835             ds_inf("Clear pointer focus from view(%p)", pointer->focused_view);
1836             ds_seat_pointer_notify_clear_focus(pointer->server->seat);
1837             pointer->focused_view = NULL;
1838         }
1839
1840         if (view) {
1841             ds_inf("Set pointer focus to view(%p)", view);
1842             ds_seat_pointer_notify_enter(pointer->server->seat,
1843                     ds_xdg_surface_get_surface(view->xdg_surface), sx, sy);
1844             pointer->focused_view = view;
1845         }
1846     }
1847
1848     if (view) {
1849         ds_seat_pointer_notify_motion(pointer->server->seat,
1850                 event->time_msec, sx, sy);
1851     }
1852 }
1853
1854 static void
1855 pointer_handle_button(struct wl_listener *listener, void *data)
1856 {
1857     struct tinyds_pointer *pointer;
1858     struct ds_event_pointer_button *event = data;
1859
1860     pointer = wl_container_of(listener, pointer, button);
1861
1862     ds_inf("Pointer(%p) button(%d): state(%s) time(%d)",
1863             pointer, event->button,
1864             (event->state == DS_BUTTON_PRESSED) ? "Pressed" : "Released",
1865             event->time_msec);
1866
1867     ds_seat_pointer_notify_button(pointer->server->seat, event->time_msec, event->button, event->state);
1868 }
1869
1870 static void
1871 pointer_handle_frame(struct wl_listener *listener, void *data)
1872 {
1873     struct tinyds_pointer *pointer;
1874
1875     pointer = wl_container_of(listener, pointer, frame);
1876
1877     ds_inf("Pointer(%p) frame", pointer);
1878     ds_seat_pointer_notify_frame(pointer->server->seat);
1879 }
1880
1881 static void
1882 server_add_pointer(struct tinyds_server *server, struct ds_input_device *dev)
1883 {
1884     struct tinyds_pointer *pointer;
1885
1886     pointer = calloc(1, sizeof *pointer);
1887     assert(pointer);
1888
1889     pointer->dev = dev;
1890     pointer->server = server;
1891     server->output_x = 200;
1892     server->output_y = 200;
1893
1894     pointer->destroy.notify = pointer_handle_device_destroy;
1895     ds_input_device_add_destroy_listener(dev, &pointer->destroy);
1896
1897     pointer->motion.notify = pointer_handle_motion;
1898     ds_pointer_add_motion_listener(ds_input_device_get_pointer(dev),
1899             &pointer->motion);
1900
1901     pointer->button.notify = pointer_handle_button;
1902     ds_pointer_add_button_listener(ds_input_device_get_pointer(dev),
1903             &pointer->button);
1904
1905     pointer->frame.notify = pointer_handle_frame;
1906     ds_pointer_add_frame_listener(ds_input_device_get_pointer(dev),
1907             &pointer->frame);
1908
1909     pointer->focused_view = NULL;
1910
1911     wl_list_insert(&server->pointers, &pointer->link);
1912
1913     ds_inf("Pointer(%p) added", pointer);
1914 }
1915
1916 static void
1917 output_schedule_commit_handle_idle_timer(void *data)
1918 {
1919     struct tinyds_output *output = data;
1920     output->idle_commit = NULL;
1921
1922     output_commit(output);
1923 }
1924
1925 static void
1926 output_schedule_commit(struct tinyds_output *output)
1927 {
1928     if (output->idle_commit)
1929         return;
1930
1931     struct wl_event_loop *ev = wl_display_get_event_loop(output->server->display);
1932     output->idle_commit =
1933         wl_event_loop_add_idle(ev, output_schedule_commit_handle_idle_timer, output);
1934 }
1935
1936 static void
1937 text_input_mgr_handle_destroy(struct wl_listener *listener, void *data)
1938 {
1939     struct tinyds_text_input *text_input;
1940     struct tinyds_server *server;
1941
1942     ds_inf("text_input_mgr_handle_destroy");
1943     text_input = wl_container_of(listener, text_input, mgr_destroy);
1944
1945     wl_list_remove(&text_input->mgr_destroy.link);
1946     wl_list_remove(&text_input->new_text_input.link);
1947
1948     server = text_input->server;
1949     server->text_input = NULL;
1950 }
1951
1952 static void
1953 text_input_handle_destroy(struct wl_listener *listener, void *data)
1954 {
1955     struct tinyds_text_input *text_input;
1956
1957     ds_inf("text_input_handle_destroy");
1958
1959     text_input = wl_container_of(listener, text_input, destroy);
1960
1961     wl_list_remove(&text_input->destroy.link);
1962     wl_list_remove(&text_input->text_input_activate.link);
1963     wl_list_remove(&text_input->text_input_deactivate.link);
1964     wl_list_remove(&text_input->text_input_reset.link);
1965     wl_list_remove(&text_input->text_input_set_content_type.link);
1966     wl_list_remove(&text_input->text_input_invoke_action.link);
1967     wl_list_remove(&text_input->text_input_commit_state.link);
1968     wl_list_remove(&text_input->text_input_set_preferred_language.link);
1969
1970     free(text_input);
1971 }
1972
1973 static void
1974 text_input_handle_activate(struct wl_listener *listener, void *data)
1975 {
1976     struct tinyds_text_input *text_input;
1977     struct tinyds_input_method *input_method;
1978     struct ds_tizen_text_input_event_activate *event = data;
1979
1980     text_input = wl_container_of(listener, text_input, text_input_activate);
1981
1982     input_method = text_input->server->input_method;
1983
1984     ds_inf("text_input_handle_activate. text_input(%p) seat(%p) surface(%p) text_input(%p)",
1985         text_input, event->seat, event->surface, event->text_input);
1986
1987     if (input_method->input == text_input)
1988         return;
1989     if (input_method->input)
1990         ;//deactivate_input_method(server->input_method);
1991     input_method->input = text_input;
1992     wl_list_insert(&text_input->input_methods, &input_method->link);
1993
1994     text_input->surface = event->surface;
1995
1996     if (!add_new_input_method_context(input_method, text_input))
1997         return;
1998
1999     // ds_tizen_input_method_send_set_text_input_id();
2000 }
2001
2002 static void
2003 text_input_handle_deactivate(struct wl_listener *listener, void *data)
2004 {
2005     struct tinyds_text_input *text_input;
2006     struct tinyds_input_method *input_method, *tmp;
2007     struct ds_tizen_text_input_event_deactivate *event = data;
2008
2009     text_input = wl_container_of(listener, text_input, text_input_deactivate);
2010     ds_inf("text_input_handle_deactivate. text_input(%p) seat(%p) text_input(%p)",
2011         text_input, event->seat, event->text_input);
2012
2013     wl_list_for_each_safe(input_method, tmp, &text_input->input_methods, link) {
2014         if (!input_method->input_method || !input_method->context->context) continue;
2015         ds_tizen_input_method_send_deactivate(input_method->input_method, input_method->context->context);
2016         input_method->input = NULL;
2017         input_method->context = NULL;
2018         wl_list_remove(&input_method->link);
2019     }
2020
2021     text_input->surface = NULL;
2022     // ds_tizen_input_method_send_close_connection();
2023 }
2024
2025 static void
2026 text_input_handle_reset(struct wl_listener *listener, void *data)
2027 {
2028     struct tinyds_text_input *text_input;
2029     struct tinyds_input_method *input_method;
2030
2031     text_input = wl_container_of(listener, text_input, text_input_reset);
2032
2033     ds_inf("text_input_handle_reset. text_input(%p)", text_input);
2034
2035     wl_list_for_each(input_method, &text_input->input_methods, link) {
2036         if (!input_method->context || !input_method->context->context) continue;
2037         ds_tizen_input_method_context_send_reset(input_method->context->context);
2038     }
2039 }
2040
2041 static void
2042 text_input_handle_set_content_type(struct wl_listener *listener, void *data)
2043 {
2044     struct tinyds_text_input *text_input;
2045     struct ds_tizen_text_input_event_set_content_type *event = data;
2046     struct tinyds_input_method *input_method;
2047
2048     text_input = wl_container_of(listener, text_input, text_input_set_content_type);
2049
2050     ds_inf("text_input_handle_content_type. text_input(%p) hint(%u) purpose(%u)",
2051         text_input, event->hint, event->purpose);
2052
2053     wl_list_for_each(input_method, &text_input->input_methods, link) {
2054         if (!input_method->context || !input_method->context->context) continue;
2055         ds_tizen_input_method_context_send_content_type(input_method->context->context,
2056             event->hint, event->purpose);
2057     }
2058 }
2059
2060 static void
2061 text_input_handle_invoke_action(struct wl_listener *listener, void *data)
2062 {
2063     struct tinyds_text_input *text_input;
2064     struct ds_tizen_text_input_event_invoke_action *event = data;
2065     struct tinyds_input_method *input_method;
2066
2067     text_input = wl_container_of(listener, text_input, text_input_invoke_action);
2068
2069     ds_inf("text_input_handle_invoke_action. text_input(%p) button(%u) index(%u)",
2070         text_input, event->button, event->index);
2071
2072     wl_list_for_each(input_method, &text_input->input_methods, link) {
2073         if (!input_method->context || !input_method->context->context) continue;
2074         ds_tizen_input_method_context_send_invoke_action(input_method->context->context,
2075             event->button, event->index);
2076     }
2077 }
2078
2079 static void
2080 text_input_handle_commit_state(struct wl_listener *listener, void *data)
2081 {
2082     struct tinyds_text_input *text_input;
2083     struct ds_tizen_text_input_event_commit_state *event = data;
2084     struct tinyds_input_method *input_method;
2085
2086     text_input = wl_container_of(listener, text_input, text_input_commit_state);
2087
2088     ds_inf("text_input_handle_commit_state. text_input(%p) serial(%u)",
2089         text_input, event->serial);
2090
2091     wl_list_for_each(input_method, &text_input->input_methods, link) {
2092         if (!input_method->context || !input_method->context->context) continue;
2093         ds_tizen_input_method_context_send_commit_state(input_method->context->context,
2094             event->serial);
2095     }
2096 }
2097
2098 static void
2099 text_input_handle_set_preferred_language(struct wl_listener *listener, void *data)
2100 {
2101     struct tinyds_text_input *text_input;
2102     struct ds_tizen_text_input_event_set_preferred_language *event = data;
2103     struct tinyds_input_method *input_method;
2104
2105     text_input = wl_container_of(listener, text_input, text_input_set_preferred_language);
2106
2107     ds_inf("text_input_handle_set_preferred_language. text_input(%p) language(%s)",
2108         text_input, event->language);
2109
2110     wl_list_for_each(input_method, &text_input->input_methods, link) {
2111         if (!input_method->context || !input_method->context->context) continue;
2112         ds_tizen_input_method_context_send_preferred_language(input_method->context->context,
2113             event->language);
2114     }
2115 }
2116
2117 static void
2118 text_input_mgr_handle_new_text_input(struct wl_listener *listener, void *data)
2119 {
2120     struct tinyds_text_input *text_input;
2121     struct ds_tizen_text_input *input = data;
2122
2123     text_input = wl_container_of(listener, text_input, new_text_input);
2124
2125     ds_inf("text_input_mgr_handle_new_text_input");
2126
2127     text_input->input = input;
2128
2129     text_input->destroy.notify = text_input_handle_destroy;
2130     ds_tizen_text_input_add_destroy_listener(text_input->input,
2131         &text_input->destroy);
2132
2133     text_input->text_input_activate.notify = text_input_handle_activate;
2134     ds_tizen_text_input_add_activate_listener(text_input->input,
2135         &text_input->text_input_activate);
2136
2137     text_input->text_input_deactivate.notify = text_input_handle_deactivate;
2138     ds_tizen_text_input_add_deactivate_listener(text_input->input,
2139         &text_input->text_input_deactivate);
2140
2141     text_input->text_input_reset.notify = text_input_handle_reset;
2142     ds_tizen_text_input_add_reset_listener(text_input->input,
2143         &text_input->text_input_reset);
2144
2145     text_input->text_input_set_content_type.notify = text_input_handle_set_content_type;
2146     ds_tizen_text_input_add_set_content_type_listener(text_input->input,
2147         &text_input->text_input_set_content_type);
2148
2149     text_input->text_input_invoke_action.notify = text_input_handle_invoke_action;
2150     ds_tizen_text_input_add_invoke_action_listener(text_input->input,
2151         &text_input->text_input_invoke_action);
2152
2153     text_input->text_input_commit_state.notify = text_input_handle_commit_state;
2154     ds_tizen_text_input_add_commit_state_listener(text_input->input,
2155         &text_input->text_input_commit_state);
2156
2157     text_input->text_input_set_preferred_language.notify = text_input_handle_set_preferred_language;
2158     ds_tizen_text_input_add_set_preferred_language_listener(text_input->input,
2159         &text_input->text_input_set_preferred_language);
2160 }
2161
2162 static void
2163 input_method_mgr_handle_destroy(struct wl_listener *listener, void *data)
2164 {
2165     struct tinyds_input_method *input_method;
2166
2167     ds_inf("input_method_mgr_handle_destroy");
2168
2169     input_method = wl_container_of(listener, input_method, mgr_destroy);
2170
2171     wl_list_remove(&input_method->mgr_destroy.link);
2172 }
2173
2174 static void
2175 input_method_handle_destroy(struct wl_listener *listener, void *data)
2176 {
2177     struct tinyds_input_method *input_method;
2178     struct tinyds_server *server;
2179
2180     ds_inf("input_method_handle_destroy");
2181
2182     input_method = wl_container_of(listener, input_method, destroy);
2183
2184     wl_list_remove(&input_method->destroy.link);
2185
2186     server = input_method->server;
2187     server->input_method = NULL;
2188
2189     free(input_method);
2190 }
2191
2192 static void
2193 context_handle_destroy(struct wl_listener *listener, void *data)
2194 {
2195     struct tinyds_input_method_context *context;
2196     struct tinyds_server *server;
2197
2198     ds_inf("context_handle_destroy");
2199
2200     context = wl_container_of(listener, context, destroy);
2201
2202     wl_list_remove(&context->destroy.link);
2203
2204     wl_list_remove(&context->im_context_commit_string.link);
2205     wl_list_remove(&context->im_context_preedit_string.link);
2206     wl_list_remove(&context->im_context_preedit_styling.link);
2207     wl_list_remove(&context->im_context_preedit_cursor.link);
2208     wl_list_remove(&context->im_context_delete_surrounding_text.link);
2209     wl_list_remove(&context->im_context_cursor_position.link);
2210     wl_list_remove(&context->im_context_modifiers_map.link);
2211     wl_list_remove(&context->im_context_keysym.link);
2212     wl_list_remove(&context->im_context_grab_keyboard.link);
2213     wl_list_remove(&context->im_context_key.link);
2214     wl_list_remove(&context->im_context_modifiers.link);
2215     wl_list_remove(&context->im_context_language.link);
2216     wl_list_remove(&context->im_context_text_direction.link);
2217
2218     server = context->server;
2219     server->input_method->context = NULL;
2220
2221     free(context);
2222 }
2223
2224 static void
2225 context_handle_commit_string(struct wl_listener *listener, void *data)
2226 {
2227     struct tinyds_text_input *text_input;
2228     struct tinyds_input_method_context *context;
2229     struct ds_tizen_input_method_context_event_commit_string *event = data;
2230
2231     context = wl_container_of(listener, context, im_context_commit_string);
2232     text_input = context->server->text_input;
2233
2234     ds_inf("context_handle_commit_string. text_input(%p) serial(%u) text(%s)",
2235         text_input, event->serial, event->text);
2236
2237     ds_tizen_text_input_send_commit_string(text_input->input, event->serial, event->text);
2238 }
2239
2240 static void
2241 context_handle_preedit_string(struct wl_listener *listener, void *data)
2242 {
2243     struct tinyds_input_method_context *context;
2244     struct tinyds_text_input *text_input;
2245     struct ds_tizen_input_method_context_event_preedit_string *event = data;
2246
2247     context = wl_container_of(listener, context, im_context_preedit_string);
2248     text_input = context->server->text_input;
2249
2250     ds_inf("context_handle_preedit_string. text_input(%p) serial(%u) text(%s) commit(%s)",
2251         text_input, event->serial, event->text, event->commit);
2252
2253     ds_tizen_text_input_send_preedit_string(text_input->input, event->serial, event->text, event->commit);
2254 }
2255
2256 static void
2257 context_handle_preedit_styling(struct wl_listener *listener, void *data)
2258 {
2259     struct tinyds_input_method_context *context;
2260     struct tinyds_text_input *text_input;
2261     struct ds_tizen_input_method_context_event_preedit_styling *event = data;
2262
2263     context = wl_container_of(listener, context, im_context_preedit_styling);
2264     text_input = context->server->text_input;
2265
2266     ds_inf("context_handle_preedit_styling. text_input(%p) index(%u) length(%u) style(%u)",
2267         text_input, event->index, event->length, event->style);
2268
2269     ds_tizen_text_input_send_preedit_styling(text_input->input, event->index, event->length, event->style);
2270 }
2271
2272 static void
2273 context_handle_preedit_cursor(struct wl_listener *listener, void *data)
2274 {
2275     struct tinyds_input_method_context *context;
2276     struct tinyds_text_input *text_input;
2277     struct ds_tizen_input_method_context_event_preedit_cursor *event = data;
2278
2279     context = wl_container_of(listener, context, im_context_preedit_cursor);
2280     text_input = context->server->text_input;
2281
2282     ds_inf("context_handle_preedit_cursor. text_input(%p) index(%u)",
2283         text_input, event->index);
2284
2285     ds_tizen_text_input_send_preedit_cursor(text_input->input, event->index);
2286 }
2287
2288 static void
2289 context_handle_delete_surrounding_text(struct wl_listener *listener, void *data)
2290 {
2291     struct tinyds_input_method_context *context;
2292     struct tinyds_text_input *text_input;
2293     struct ds_tizen_input_method_context_event_delete_surrounding_text *event = data;
2294
2295     context = wl_container_of(listener, context, im_context_delete_surrounding_text);
2296     text_input = context->server->text_input;
2297
2298     ds_inf("context_handle_delete_surrounding_text. text_input(%p) index(%d) length(%u)",
2299         text_input, event->index, event->length);
2300
2301     ds_tizen_text_input_send_delete_surrounding_text(text_input->input, event->index, event->length);
2302 }
2303
2304 static void
2305 context_handle_cursor_position(struct wl_listener *listener, void *data)
2306 {
2307     struct tinyds_input_method_context *context;
2308     struct tinyds_text_input *text_input;
2309     struct ds_tizen_input_method_context_event_cursor_position *event = data;
2310
2311     context = wl_container_of(listener, context, im_context_cursor_position);
2312     text_input = context->server->text_input;
2313
2314     ds_inf("context_handle_cursor_position. text_input(%p) index(%d) length(%d)",
2315         text_input, event->index, event->anchor);
2316
2317     ds_tizen_text_input_send_cursor_position(text_input->input, event->index, event->anchor);
2318 }
2319
2320 static void
2321 context_handle_modifiers_map(struct wl_listener *listener, void *data)
2322 {
2323     struct tinyds_input_method_context *context;
2324     struct tinyds_text_input *text_input;
2325     struct ds_tizen_input_method_context_event_modifiers_map *event = data;
2326
2327     context = wl_container_of(listener, context, im_context_modifiers_map);
2328     text_input = context->server->text_input;
2329
2330     ds_inf("context_handle_modifiers_map. text_input(%p) map(%p)",
2331         text_input, event->map);
2332
2333     ds_tizen_text_input_send_modifiers_map(text_input->input, event->map);
2334 }
2335
2336 static void
2337 context_handle_keysym(struct wl_listener *listener, void *data)
2338 {
2339     struct tinyds_input_method_context *context;
2340     struct tinyds_text_input *text_input;
2341     struct ds_tizen_input_method_context_event_keysym *event = data;
2342
2343     context = wl_container_of(listener, context, im_context_keysym);
2344     text_input = context->server->text_input;
2345
2346     ds_inf("context_handle_keysym. text_input(%p) serial(%u) time(%u) sysm(%u) state(%u) modifiers(%u)",
2347         text_input, event->serial, event->time, event->sym, event->state, event->modifiers);
2348
2349     ds_tizen_text_input_send_keysym(text_input->input, event->serial, event->time, event->sym, event->state, event->modifiers);
2350 }
2351
2352 static void
2353 context_handle_grab_keyboard(struct wl_listener *listener, void *data)
2354 {
2355     struct tinyds_input_method_context *context;
2356     struct tinyds_text_input *text_input;
2357
2358     context = wl_container_of(listener, context, im_context_grab_keyboard);
2359     text_input = context->server->text_input;
2360
2361     ds_inf("context_handle_grab_keyboard. text_input(%p)",
2362         text_input);
2363
2364     //TODO
2365 }
2366
2367 static void
2368 context_handle_key(struct wl_listener *listener, void *data)
2369 {
2370     struct tinyds_input_method_context *context;
2371     struct tinyds_text_input *text_input;
2372
2373     context = wl_container_of(listener, context, im_context_key);
2374     text_input = context->server->text_input;
2375
2376     ds_inf("context_handle_key. text_input(%p)",
2377         text_input);
2378
2379    //TODO
2380 }
2381
2382 static void
2383 context_handle_modifiers(struct wl_listener *listener, void *data)
2384 {
2385     struct tinyds_input_method_context *context;
2386     struct tinyds_text_input *text_input;
2387
2388     context = wl_container_of(listener, context, im_context_modifiers);
2389     text_input = context->server->text_input;
2390
2391     ds_inf("context_handle_modifiers. text_input(%p)",
2392         text_input);
2393
2394    //TODO
2395 }
2396
2397 static void
2398 context_handle_language(struct wl_listener *listener, void *data)
2399 {
2400     struct tinyds_input_method_context *context;
2401     struct tinyds_text_input *text_input;
2402     struct ds_tizen_input_method_context_event_language *event = data;
2403
2404     context = wl_container_of(listener, context, im_context_language);
2405     text_input = context->server->text_input;
2406
2407     ds_inf("context_handle_language. text_input(%p) serial(%u), language(%s)",
2408         text_input, event->serial, event->language);
2409
2410     ds_tizen_text_input_send_language(text_input->input, event->serial, event->language);
2411 }
2412
2413 static void
2414 context_handle_text_direction(struct wl_listener *listener, void *data)
2415 {
2416     struct tinyds_input_method_context *context;
2417     struct tinyds_text_input *text_input;
2418     struct ds_tizen_input_method_context_event_text_direction *event = data;
2419
2420     context = wl_container_of(listener, context, im_context_text_direction);
2421     text_input = context->server->text_input;
2422
2423     ds_inf("context_handle_text_direction. text_input(%p) serial(%u), direction(%u)",
2424         text_input, event->serial, event->direction);
2425
2426     ds_tizen_text_input_send_text_direction(text_input->input, event->serial, event->direction);
2427 }
2428
2429 static bool
2430 add_new_text_input(struct tinyds_server *server)
2431 {
2432     struct tinyds_text_input *text_input;
2433
2434     text_input = calloc(1, sizeof *text_input);
2435     if (!text_input)
2436         return false;
2437
2438     text_input->text_input_mgr = ds_tizen_text_input_manager_create(server->display);
2439     if (!text_input->text_input_mgr) {
2440         free(text_input);
2441         ds_err("Could not create ds_tizen_text_input_manager");
2442         return false;
2443     }
2444
2445     wl_list_init(&text_input->input_methods);
2446
2447     text_input->mgr_destroy.notify = text_input_mgr_handle_destroy;
2448     ds_tizen_text_input_manager_add_destroy_listener(text_input->text_input_mgr,
2449             &text_input->mgr_destroy);
2450
2451     text_input->new_text_input.notify = text_input_mgr_handle_new_text_input;
2452     ds_tizen_text_input_manager_add_new_text_input_listener(text_input->text_input_mgr,
2453             &text_input->new_text_input);
2454
2455     text_input->server = server;
2456     server->text_input = text_input;
2457
2458     ds_inf("Text_Input (%p) added", text_input);
2459
2460     return true;
2461 }
2462
2463 static bool
2464 add_new_input_method(struct tinyds_server *server)
2465 {
2466     struct tinyds_input_method *input_method;
2467
2468     input_method = calloc(1, sizeof *input_method);
2469     if (!input_method)
2470         return false;
2471
2472     input_method->input_method = ds_tizen_input_method_create(server->display);
2473     if (!input_method->input_method) {
2474         free(input_method);
2475         ds_err("Could not create ds_tizen_input_method");
2476         return false;
2477     }
2478     input_method->destroy.notify = input_method_handle_destroy;
2479     ds_tizen_input_method_add_destroy_listener(input_method->input_method,
2480             &input_method->destroy);
2481
2482     input_method->input_method_mgr = ds_tizen_input_method_manager_create(server->display);
2483     if (!input_method->input_method_mgr) {
2484         free(input_method);
2485         ds_err("Could not create ds_tizen_input_method_manager");
2486         return false;
2487     }
2488
2489     input_method->mgr_destroy.notify = input_method_mgr_handle_destroy;
2490     ds_tizen_input_method_manager_add_destroy_listener(input_method->input_method_mgr,
2491             &input_method->mgr_destroy);
2492
2493     input_method->server = server;
2494     server->input_method = input_method;
2495
2496     ds_inf("Input_Method (%p) added", input_method);
2497
2498     return true;
2499 }
2500
2501 static bool
2502 add_new_input_method_context(struct tinyds_input_method *input_method,
2503         struct tinyds_text_input *text_input)
2504 {
2505     struct tinyds_input_method_context *context;
2506
2507     context = calloc(1, sizeof *context);
2508     if (context == NULL)
2509     {
2510         ds_err("calloc is failed. tinyds_input_method_context");
2511         return false;
2512     }
2513     input_method->context = context;
2514     context->input_method = input_method;
2515     context->server = input_method->server;
2516     context->input = text_input;
2517
2518     context->context = ds_tizen_input_method_create_context(input_method->input_method);
2519     if (context->context == NULL) {
2520         ds_err("ds_tizen_input_method_create_context() failed.");
2521         return false;
2522     }
2523
2524     context->destroy.notify = context_handle_destroy;
2525     ds_tizen_input_method_context_add_destroy_listener(context->context,
2526         &context->destroy);
2527
2528     context->im_context_commit_string.notify = context_handle_commit_string;
2529     ds_tizen_input_method_context_add_commit_string_listener(context->context,
2530         &context->im_context_commit_string);
2531
2532     context->im_context_preedit_string.notify = context_handle_preedit_string;
2533     ds_tizen_input_method_context_add_preedit_string_listener(context->context,
2534         &context->im_context_preedit_string);
2535
2536     context->im_context_preedit_styling.notify = context_handle_preedit_styling;
2537     ds_tizen_input_method_context_add_preedit_styling_listener(context->context,
2538         &context->im_context_preedit_styling);
2539
2540     context->im_context_preedit_cursor.notify = context_handle_preedit_cursor;
2541     ds_tizen_input_method_context_add_preedit_cursor_listener(context->context,
2542         &context->im_context_preedit_cursor);
2543
2544     context->im_context_delete_surrounding_text.notify = context_handle_delete_surrounding_text;
2545     ds_tizen_input_method_context_add_delete_surrounding_text_listener(context->context,
2546         &context->im_context_delete_surrounding_text);
2547
2548     context->im_context_cursor_position.notify = context_handle_cursor_position;
2549     ds_tizen_input_method_context_add_cursor_position_listener(context->context,
2550         &context->im_context_cursor_position);
2551
2552     context->im_context_modifiers_map.notify = context_handle_modifiers_map;
2553     ds_tizen_input_method_context_add_modifiers_map_listener(context->context,
2554         &context->im_context_modifiers_map);
2555
2556     context->im_context_keysym.notify = context_handle_keysym;
2557     ds_tizen_input_method_context_add_keysym_listener(context->context,
2558         &context->im_context_keysym);
2559
2560     context->im_context_grab_keyboard.notify = context_handle_grab_keyboard;
2561     ds_tizen_input_method_context_add_grab_keyboard_listener(context->context,
2562         &context->im_context_grab_keyboard);
2563
2564     context->im_context_key.notify = context_handle_key;
2565     ds_tizen_input_method_context_add_key_listener(context->context,
2566         &context->im_context_key);
2567
2568     context->im_context_modifiers.notify = context_handle_modifiers;
2569     ds_tizen_input_method_context_add_modifiers_listener(context->context,
2570         &context->im_context_modifiers);
2571
2572     context->im_context_language.notify = context_handle_language;
2573     ds_tizen_input_method_context_add_language_listener(context->context,
2574         &context->im_context_language);
2575
2576     context->im_context_text_direction.notify = context_handle_text_direction;
2577     ds_tizen_input_method_context_add_text_direction_listener(context->context,
2578         &context->im_context_text_direction);
2579
2580     return true;
2581 }
2582
2583 static void
2584 visibility_handle_destroy(struct wl_listener *listener, void *data)
2585 {
2586     struct tinyds_policy_visibility *visibility;
2587
2588     visibility = wl_container_of(listener, visibility, destroy);
2589
2590     ds_inf("Policy Visibility(%p) destroy", visibility);
2591
2592     wl_list_remove(&visibility->destroy.link);
2593     wl_list_remove(&visibility->link);
2594     free(visibility);
2595 }
2596
2597 static void
2598 position_handle_destroy(struct wl_listener *listener, void *data)
2599 {
2600     struct tinyds_policy_position *position;
2601
2602     position = wl_container_of(listener, position, destroy);
2603
2604     ds_inf("Policy Position(%p) destroy", position);
2605
2606     wl_list_remove(&position->destroy.link);
2607     wl_list_remove(&position->set.link);
2608     wl_list_remove(&position->link);
2609     free(position);
2610 }
2611
2612 static void
2613 position_handle_set(struct wl_listener *listener, void *data)
2614 {
2615     struct tinyds_policy_position *position;
2616
2617     position = wl_container_of(listener, position, set);
2618
2619     ds_inf("Policy Position(%p) set", position);
2620
2621     // TODO:
2622 }
2623
2624 static void
2625 subsurface_watcher_handle_destroy(struct wl_listener *listener, void *data)
2626 {
2627     struct tinyds_policy_subsurface_watcher *subsurface_watcher;
2628
2629     subsurface_watcher = wl_container_of(listener, subsurface_watcher, destroy);
2630
2631     ds_inf("Policy Subsurface_Watcher(%p) destroy", subsurface_watcher);
2632
2633     wl_list_remove(&subsurface_watcher->destroy.link);
2634     wl_list_remove(&subsurface_watcher->link);
2635     free(subsurface_watcher);
2636 }
2637
2638 static void
2639 policy_surface_handle_destroy(struct wl_listener *listener, void *data)
2640 {
2641     struct tinyds_policy_surface *policy_surface;
2642
2643     policy_surface = wl_container_of(listener, policy_surface, destroy);
2644
2645     ds_inf("Policy Info(%p) destroy", policy_surface);
2646
2647     wl_list_remove(&policy_surface->destroy.link);
2648     wl_list_remove(&policy_surface->new_visibility.link);
2649     wl_list_remove(&policy_surface->new_position.link);
2650     wl_list_remove(&policy_surface->activate.link);
2651     wl_list_remove(&policy_surface->raise.link);
2652     wl_list_remove(&policy_surface->lower.link);
2653     wl_list_remove(&policy_surface->set_focus_skip.link);
2654     wl_list_remove(&policy_surface->unset_focus_skip.link);
2655     wl_list_remove(&policy_surface->set_role.link);
2656     wl_list_remove(&policy_surface->set_window_type.link);
2657     wl_list_remove(&policy_surface->set_conformant.link);
2658     wl_list_remove(&policy_surface->unset_conformant.link);
2659     wl_list_remove(&policy_surface->get_conformant.link);
2660     wl_list_remove(&policy_surface->set_notification_level.link);
2661     wl_list_remove(&policy_surface->set_window_screen_mode.link);
2662     wl_list_remove(&policy_surface->get_subsurface.link);
2663     wl_list_remove(&policy_surface->iconify.link);
2664     wl_list_remove(&policy_surface->uniconify.link);
2665     wl_list_remove(&policy_surface->add_aux_hint.link);
2666     wl_list_remove(&policy_surface->change_aux_hint.link);
2667     wl_list_remove(&policy_surface->delete_aux_hint.link);
2668     wl_list_remove(&policy_surface->get_supported_aux_hints.link);
2669     wl_list_remove(&policy_surface->set_floating_mode.link);
2670     wl_list_remove(&policy_surface->unset_floating_mode.link);
2671     wl_list_remove(&policy_surface->set_stack_mode.link);
2672     wl_list_remove(&policy_surface->new_subsurface_watcher.link);
2673     wl_list_remove(&policy_surface->set_parent.link);
2674     wl_list_remove(&policy_surface->ack_conformant_region.link);
2675     wl_list_remove(&policy_surface->set_video.link);
2676     wl_list_remove(&policy_surface->show.link);
2677     wl_list_remove(&policy_surface->hide.link);
2678     wl_list_remove(&policy_surface->set_parent_with_below.link);
2679     wl_list_remove(&policy_surface->link);
2680     free(policy_surface);
2681 }
2682
2683 static void
2684 policy_surface_handle_new_visibility(struct wl_listener *listener, void *data)
2685 {
2686     struct tinyds_policy_surface *policy_surface;
2687     struct tinyds_policy_visibility *visibility;
2688     struct ds_tizen_event_policy_surface_new_visibility *event;
2689
2690     policy_surface = wl_container_of(listener, policy_surface, new_visibility);
2691     event = (struct ds_tizen_event_policy_surface_new_visibility *)data;
2692
2693     ds_inf("Policy Info(%p) new_visibility", policy_surface);
2694
2695     visibility = calloc(1, sizeof *visibility);
2696     if (!visibility)
2697         return;
2698
2699     visibility->visibility = event->visibility;
2700
2701     visibility->destroy.notify = visibility_handle_destroy;
2702     ds_tizen_policy_visibility_add_destroy_listener(visibility->visibility,
2703         &visibility->destroy);
2704
2705     wl_list_insert(&policy_surface->visibilities, &visibility->link);
2706 }
2707
2708 static void
2709 policy_surface_handle_new_position(struct wl_listener *listener, void *data)
2710 {
2711     struct tinyds_policy_surface *policy_surface;
2712     struct tinyds_policy_position *position;
2713     struct ds_tizen_event_policy_surface_new_position *event;
2714
2715     policy_surface = wl_container_of(listener, policy_surface, new_position);
2716     event = (struct ds_tizen_event_policy_surface_new_position *)data;
2717
2718     ds_inf("Policy Info(%p) new_position", policy_surface);
2719
2720     position = calloc(1, sizeof *position);
2721     if (!position)
2722         return;
2723
2724     position->position = event->position;
2725
2726     position->destroy.notify = position_handle_destroy;
2727     ds_tizen_policy_position_add_destroy_listener(position->position,
2728         &position->destroy);
2729
2730     position->set.notify = position_handle_set;
2731     ds_tizen_policy_position_add_set_listener(position->position,
2732         &position->set);
2733
2734     wl_list_insert(&policy_surface->positions, &position->link);
2735 }
2736
2737 static void
2738 policy_surface_handle_activate(struct wl_listener *listener, void *data)
2739 {
2740     struct tinyds_policy_surface *policy_surface;
2741
2742     policy_surface = wl_container_of(listener, policy_surface, activate);
2743
2744     ds_inf("Policy Info(%p) activate", policy_surface);
2745
2746     // TODO:
2747 }
2748
2749 static void
2750 policy_surface_handle_raise(struct wl_listener *listener, void *data)
2751 {
2752     struct tinyds_policy_surface *policy_surface;
2753
2754     policy_surface = wl_container_of(listener, policy_surface, raise);
2755
2756     ds_inf("Policy Info(%p) raise", policy_surface);
2757
2758     // TODO:
2759 }
2760
2761 static void
2762 policy_surface_handle_lower(struct wl_listener *listener, void *data)
2763 {
2764     struct tinyds_policy_surface *policy_surface;
2765
2766     policy_surface = wl_container_of(listener, policy_surface, raise);
2767
2768     ds_inf("Policy Info(%p) lower", policy_surface);
2769
2770     // TODO:
2771 }
2772
2773 static void
2774 policy_surface_handle_set_focus_skip(struct wl_listener *listener, void *data)
2775 {
2776     struct tinyds_policy_surface *policy_surface;
2777
2778     policy_surface = wl_container_of(listener, policy_surface, set_focus_skip);
2779
2780     ds_inf("Policy Info(%p) set_focus_skip", policy_surface);
2781
2782     // TODO:
2783 }
2784
2785 static void
2786 policy_surface_handle_unset_focus_skip(struct wl_listener *listener, void *data)
2787 {
2788     struct tinyds_policy_surface *policy_surface;
2789
2790     policy_surface = wl_container_of(listener, policy_surface, unset_focus_skip);
2791
2792     ds_inf("Policy Info(%p) unset_focus_skip", policy_surface);
2793
2794     // TODO:
2795 }
2796
2797 static void
2798 policy_surface_handle_set_role(struct wl_listener *listener, void *data)
2799 {
2800     struct tinyds_policy_surface *policy_surface;
2801
2802     policy_surface = wl_container_of(listener, policy_surface, set_role);
2803
2804     ds_inf("Policy Info(%p) set_role", policy_surface);
2805
2806     // TODO:
2807 }
2808
2809 static void
2810 policy_surface_handle_set_window_type(struct wl_listener *listener, void *data)
2811 {
2812     struct tinyds_policy_surface *policy_surface;
2813
2814     policy_surface = wl_container_of(listener, policy_surface, set_window_type);
2815
2816     ds_inf("Policy Info(%p) set_window_type", policy_surface);
2817
2818     // TODO:
2819 }
2820
2821 static void
2822 policy_surface_handle_set_conformant(struct wl_listener *listener, void *data)
2823 {
2824     struct tinyds_policy_surface *policy_surface;
2825
2826     policy_surface = wl_container_of(listener, policy_surface, set_conformant);
2827
2828     ds_inf("Policy Info(%p) set_conformant", policy_surface);
2829
2830     // TODO:
2831 }
2832
2833 static void
2834 policy_surface_handle_unset_conformant(struct wl_listener *listener, void *data)
2835 {
2836     struct tinyds_policy_surface *policy_surface;
2837
2838     policy_surface = wl_container_of(listener, policy_surface, unset_conformant);
2839
2840     ds_inf("Policy Info(%p) unset_conformant", policy_surface);
2841
2842     // TODO:
2843 }
2844
2845 static void
2846 policy_surface_handle_get_conformant(struct wl_listener *listener, void *data)
2847 {
2848     struct tinyds_policy_surface *policy_surface;
2849
2850     policy_surface = wl_container_of(listener, policy_surface, get_conformant);
2851
2852     ds_inf("Policy Info(%p) get_conformant", policy_surface);
2853
2854     // TODO:
2855 }
2856
2857 static void
2858 policy_surface_handle_set_notification_level(struct wl_listener *listener, void *data)
2859 {
2860     struct tinyds_policy_surface *policy_surface;
2861
2862     policy_surface = wl_container_of(listener, policy_surface, set_notification_level);
2863
2864     ds_inf("Policy Info(%p) set_notification_level", policy_surface);
2865
2866     // TODO:
2867 }
2868
2869 static void
2870 policy_surface_handle_set_window_screen_mode(struct wl_listener *listener, void *data)
2871 {
2872     struct tinyds_policy_surface *policy_surface;
2873
2874     policy_surface = wl_container_of(listener, policy_surface, set_window_screen_mode);
2875
2876     ds_inf("Policy Info(%p) set_window_screen_mode", policy_surface);
2877
2878     // TODO:
2879 }
2880
2881 static void
2882 policy_surface_handle_get_subsurface(struct wl_listener *listener, void *data)
2883 {
2884     struct tinyds_policy_surface *policy_surface;
2885
2886     policy_surface = wl_container_of(listener, policy_surface, get_subsurface);
2887
2888     ds_inf("Policy Info(%p) get_subsurface", policy_surface);
2889
2890     // TODO:
2891 }
2892
2893 static void
2894 policy_surface_handle_iconify(struct wl_listener *listener, void *data)
2895 {
2896     struct tinyds_policy_surface *policy_surface;
2897
2898     policy_surface = wl_container_of(listener, policy_surface, iconify);
2899
2900     ds_inf("Policy Info(%p) iconify", policy_surface);
2901
2902     // TODO:
2903 }
2904
2905 static void
2906 policy_surface_handle_uniconify(struct wl_listener *listener, void *data)
2907 {
2908     struct tinyds_policy_surface *policy_surface;
2909
2910     policy_surface = wl_container_of(listener, policy_surface, uniconify);
2911
2912     ds_inf("Policy Info(%p) uniconify", policy_surface);
2913
2914     // TODO:
2915 }
2916
2917 static void
2918 policy_surface_handle_add_aux_hint(struct wl_listener *listener, void *data)
2919 {
2920     struct tinyds_policy_surface *policy_surface;
2921
2922     policy_surface = wl_container_of(listener, policy_surface, add_aux_hint);
2923
2924     ds_inf("Policy Info(%p) add_aux_hint", policy_surface);
2925
2926     // TODO:
2927 }
2928
2929 static void
2930 policy_surface_handle_change_aux_hint(struct wl_listener *listener, void *data)
2931 {
2932     struct tinyds_policy_surface *policy_surface;
2933
2934     policy_surface = wl_container_of(listener, policy_surface, change_aux_hint);
2935
2936     ds_inf("Policy Info(%p) change_aux_hint", policy_surface);
2937
2938     // TODO:
2939 }
2940
2941 static void
2942 policy_surface_handle_delete_aux_hint(struct wl_listener *listener, void *data)
2943 {
2944     struct tinyds_policy_surface *policy_surface;
2945
2946     policy_surface = wl_container_of(listener, policy_surface, delete_aux_hint);
2947
2948     ds_inf("Policy Info(%p) delete_aux_hint", policy_surface);
2949
2950     // TODO:
2951 }
2952
2953 static void
2954 policy_surface_handle_get_supported_aux_hints(struct wl_listener *listener, void *data)
2955 {
2956     struct tinyds_policy_surface *policy_surface;
2957
2958     policy_surface = wl_container_of(listener, policy_surface, get_supported_aux_hints);
2959
2960     ds_inf("Policy Info(%p) get_supported_aux_hints", policy_surface);
2961
2962     // TODO:
2963 }
2964
2965 static void
2966 policy_surface_handle_set_floating_mode(struct wl_listener *listener, void *data)
2967 {
2968     struct tinyds_policy_surface *policy_surface;
2969
2970     policy_surface = wl_container_of(listener, policy_surface, set_floating_mode);
2971
2972     ds_inf("Policy Info(%p) set_floating_mode", policy_surface);
2973
2974     // TODO:
2975 }
2976
2977 static void
2978 policy_surface_handle_unset_floating_mode(struct wl_listener *listener, void *data)
2979 {
2980     struct tinyds_policy_surface *policy_surface;
2981
2982     policy_surface = wl_container_of(listener, policy_surface, unset_floating_mode);
2983
2984     ds_inf("Policy Info(%p) unset_floating_mode", policy_surface);
2985
2986     // TODO:
2987 }
2988
2989 static void
2990 policy_surface_handle_set_stack_mode(struct wl_listener *listener, void *data)
2991 {
2992     struct tinyds_policy_surface *policy_surface;
2993
2994     policy_surface = wl_container_of(listener, policy_surface, set_stack_mode);
2995
2996     ds_inf("Policy Info(%p) set_stack_mode", policy_surface);
2997
2998     // TODO:
2999 }
3000
3001 static void
3002 policy_surface_handle_new_subsurface_watcher(struct wl_listener *listener, void *data)
3003 {
3004     struct tinyds_policy_surface *policy_surface;
3005     struct tinyds_policy_subsurface_watcher*subsurface_watcher;
3006     struct ds_tizen_event_policy_surface_new_subsurface_watcher *event;
3007
3008     policy_surface = wl_container_of(listener, policy_surface, new_subsurface_watcher);
3009     event = (struct ds_tizen_event_policy_surface_new_subsurface_watcher *)data;
3010
3011     ds_inf("Policy Info(%p) new_subsurface_watcher", policy_surface);
3012
3013     subsurface_watcher = calloc(1, sizeof *subsurface_watcher);
3014     if (!subsurface_watcher)
3015         return;
3016
3017     subsurface_watcher->subsurface_watcher = event->subsurface_watcher;
3018
3019     subsurface_watcher->destroy.notify = subsurface_watcher_handle_destroy;
3020     ds_tizen_policy_subsurface_watcher_add_destroy_listener(subsurface_watcher->subsurface_watcher,
3021         &subsurface_watcher->destroy);
3022
3023     wl_list_insert(&policy_surface->subsurface_watchers, &subsurface_watcher->link);
3024 }
3025
3026 static void
3027 policy_surface_handle_set_parent(struct wl_listener *listener, void *data)
3028 {
3029     struct tinyds_policy_surface *policy_surface;
3030
3031     policy_surface = wl_container_of(listener, policy_surface, set_parent);
3032
3033     ds_inf("Policy Info(%p) set_parent", policy_surface);
3034
3035     // TODO:
3036 }
3037
3038 static void
3039 policy_surface_handle_ack_conformant_region(struct wl_listener *listener, void *data)
3040 {
3041     struct tinyds_policy_surface *policy_surface;
3042
3043     policy_surface = wl_container_of(listener, policy_surface, ack_conformant_region);
3044
3045     ds_inf("Policy Info(%p) ack_conformant_region", policy_surface);
3046
3047     // TODO:
3048 }
3049
3050 static void
3051 policy_surface_handle_set_video(struct wl_listener *listener, void *data)
3052 {
3053     struct tinyds_policy_surface *policy_surface;
3054
3055     policy_surface = wl_container_of(listener, policy_surface, set_video);
3056
3057     ds_inf("Policy Info(%p) set_video", policy_surface);
3058
3059     // TODO:
3060 }
3061
3062 static void
3063 policy_surface_handle_show(struct wl_listener *listener, void *data)
3064 {
3065     struct tinyds_policy_surface *policy_surface;
3066
3067     policy_surface = wl_container_of(listener, policy_surface, show);
3068
3069     ds_inf("Policy Info(%p) show", policy_surface);
3070
3071     // TODO:
3072 }
3073
3074 static void
3075 policy_surface_handle_hide(struct wl_listener *listener, void *data)
3076 {
3077     struct tinyds_policy_surface *policy_surface;
3078
3079     policy_surface = wl_container_of(listener, policy_surface, hide);
3080
3081     ds_inf("Policy Info(%p) hide", policy_surface);
3082
3083     // TODO:
3084 }
3085
3086 static void
3087 policy_surface_handle_set_parent_with_below(struct wl_listener *listener, void *data)
3088 {
3089     struct tinyds_policy_surface *policy_surface;
3090
3091     policy_surface = wl_container_of(listener, policy_surface, set_parent_with_below);
3092
3093     ds_inf("Policy Info(%p) set_parent_with_below", policy_surface);
3094
3095     // TODO:
3096 }
3097
3098 static void
3099 policy_handle_destroy(struct wl_listener *listener, void *data)
3100 {
3101     struct tinyds_policy *policy;
3102
3103     policy = wl_container_of(listener, policy, destroy);
3104
3105     ds_inf("Policy(%p) destroy", policy);
3106
3107     wl_list_remove(&policy->destroy.link);
3108     wl_list_remove(&policy->new_surface.link);
3109     wl_list_remove(&policy->activate_below_by_univeral_id.link);
3110     wl_list_remove(&policy->lower_by_universal_id.link);
3111     wl_list_remove(&policy->set_transient_for.link);
3112     wl_list_remove(&policy->unset_transient_for.link);
3113     wl_list_remove(&policy->place_subsurface_below_parent.link);
3114     wl_list_remove(&policy->set_subsurface_stand_alone.link);
3115     wl_list_remove(&policy->set_background_state.link);
3116     wl_list_remove(&policy->unset_background_state.link);
3117     wl_list_remove(&policy->add_activate_above_by_universal_id.link);
3118     wl_list_remove(&policy->set_appid.link);
3119     wl_list_remove(&policy->set_transient_for_below.link);
3120
3121     free(policy);
3122 }
3123
3124 static void
3125 policy_handle_new_surface(struct wl_listener *listener, void *data)
3126 {
3127     struct tinyds_policy *policy;
3128     struct tinyds_policy_surface *policy_surface;
3129     struct ds_tizen_event_policy_new_surface *event;
3130
3131     policy = wl_container_of(listener, policy, new_surface);
3132     event = (struct ds_tizen_event_policy_new_surface *)data;
3133
3134     ds_inf("Policy(%p) new_surface", policy);
3135
3136     policy_surface = calloc(1, sizeof *policy_surface);
3137     if (!policy_surface)
3138         return;
3139
3140     policy_surface->policy_surface = event->policy_surface;
3141
3142     policy_surface->destroy.notify = policy_surface_handle_destroy;
3143     ds_tizen_policy_surface_add_destroy_listener(policy_surface->policy_surface,
3144         &policy_surface->destroy);
3145
3146     policy_surface->new_visibility.notify = policy_surface_handle_new_visibility;
3147     ds_tizen_policy_surface_add_new_visibility_listener(policy_surface->policy_surface,
3148         &policy_surface->new_visibility);
3149
3150     policy_surface->new_position.notify = policy_surface_handle_new_position;
3151     ds_tizen_policy_surface_add_new_position_listener(policy_surface->policy_surface,
3152         &policy_surface->new_position);
3153
3154     policy_surface->activate.notify = policy_surface_handle_activate;
3155     ds_tizen_policy_surface_add_activate_listener(policy_surface->policy_surface,
3156         &policy_surface->activate);
3157
3158     policy_surface->raise.notify = policy_surface_handle_raise;
3159     ds_tizen_policy_surface_add_raise_listener(policy_surface->policy_surface,
3160         &policy_surface->raise);
3161
3162     policy_surface->lower.notify = policy_surface_handle_lower;
3163     ds_tizen_policy_surface_add_lower_listener(policy_surface->policy_surface,
3164         &policy_surface->lower);
3165
3166     policy_surface->set_focus_skip.notify = policy_surface_handle_set_focus_skip;
3167     ds_tizen_policy_surface_add_set_focus_skip_listener(policy_surface->policy_surface,
3168         &policy_surface->set_focus_skip);
3169
3170     policy_surface->unset_focus_skip.notify = policy_surface_handle_unset_focus_skip;
3171     ds_tizen_policy_surface_add_unset_focus_skip_listener(policy_surface->policy_surface,
3172         &policy_surface->unset_focus_skip);
3173
3174     policy_surface->set_role.notify = policy_surface_handle_set_role;
3175     ds_tizen_policy_surface_add_set_role_listener(policy_surface->policy_surface,
3176         &policy_surface->set_role);
3177
3178     policy_surface->set_window_type.notify = policy_surface_handle_set_window_type;
3179     ds_tizen_policy_surface_add_set_window_type_listener(policy_surface->policy_surface,
3180         &policy_surface->set_window_type);
3181
3182     policy_surface->set_conformant.notify = policy_surface_handle_set_conformant;
3183     ds_tizen_policy_surface_add_set_conformant_listener(policy_surface->policy_surface,
3184         &policy_surface->set_conformant);
3185
3186     policy_surface->unset_conformant.notify = policy_surface_handle_unset_conformant;
3187     ds_tizen_policy_surface_add_unset_conformant_listener(policy_surface->policy_surface,
3188         &policy_surface->unset_conformant);
3189
3190     policy_surface->get_conformant.notify = policy_surface_handle_get_conformant;
3191     ds_tizen_policy_surface_add_get_conformant_listener(policy_surface->policy_surface,
3192         &policy_surface->get_conformant);
3193
3194     policy_surface->set_notification_level.notify =
3195         policy_surface_handle_set_notification_level;
3196     ds_tizen_policy_surface_add_set_notification_level_listener(
3197             policy_surface->policy_surface, &policy_surface->set_notification_level);
3198
3199     policy_surface->set_window_screen_mode.notify =
3200         policy_surface_handle_set_window_screen_mode;
3201     ds_tizen_policy_surface_add_set_window_screen_mode_listener(
3202         policy_surface->policy_surface, &policy_surface->set_window_screen_mode);
3203
3204     policy_surface->get_subsurface.notify = policy_surface_handle_get_subsurface;
3205     ds_tizen_policy_surface_add_get_subsurface_listener(policy_surface->policy_surface,
3206         &policy_surface->get_subsurface);
3207
3208     policy_surface->iconify.notify = policy_surface_handle_iconify;
3209     ds_tizen_policy_surface_add_iconify_listener(policy_surface->policy_surface,
3210         &policy_surface->iconify);
3211
3212     policy_surface->uniconify.notify = policy_surface_handle_uniconify;
3213     ds_tizen_policy_surface_add_uniconify_listener(policy_surface->policy_surface,
3214         &policy_surface->uniconify);
3215
3216     policy_surface->add_aux_hint.notify = policy_surface_handle_add_aux_hint;
3217     ds_tizen_policy_surface_add_add_aux_hint_listener(policy_surface->policy_surface,
3218         &policy_surface->add_aux_hint);
3219
3220     policy_surface->change_aux_hint.notify = policy_surface_handle_change_aux_hint;
3221     ds_tizen_policy_surface_add_change_aux_hint_listener(policy_surface->policy_surface,
3222         &policy_surface->change_aux_hint);
3223
3224     policy_surface->delete_aux_hint.notify = policy_surface_handle_delete_aux_hint;
3225     ds_tizen_policy_surface_add_delete_aux_hint_listener(policy_surface->policy_surface,
3226         &policy_surface->delete_aux_hint);
3227
3228     policy_surface->get_supported_aux_hints.notify =
3229         policy_surface_handle_get_supported_aux_hints;
3230     ds_tizen_policy_surface_add_get_supported_aux_hints_listener(
3231         policy_surface->policy_surface, &policy_surface->get_supported_aux_hints);
3232
3233     policy_surface->set_floating_mode.notify =
3234         policy_surface_handle_set_floating_mode;
3235     ds_tizen_policy_surface_add_set_floating_mode_listener(
3236         policy_surface->policy_surface, &policy_surface->set_floating_mode);
3237
3238     policy_surface->unset_floating_mode.notify =
3239         policy_surface_handle_unset_floating_mode;
3240     ds_tizen_policy_surface_add_unset_floating_mode_listener(
3241         policy_surface->policy_surface, &policy_surface->unset_floating_mode);
3242
3243     policy_surface->set_stack_mode.notify = policy_surface_handle_set_stack_mode;
3244     ds_tizen_policy_surface_add_set_stack_mode_listener(policy_surface->policy_surface,
3245         &policy_surface->set_stack_mode);
3246
3247     policy_surface->new_subsurface_watcher.notify =
3248         policy_surface_handle_new_subsurface_watcher;
3249     ds_tizen_policy_surface_add_new_subsurface_watcher_listener(
3250         policy_surface->policy_surface, &policy_surface->new_subsurface_watcher);
3251
3252     policy_surface->set_parent.notify = policy_surface_handle_set_parent;
3253     ds_tizen_policy_surface_add_set_parent_listener(policy_surface->policy_surface,
3254         &policy_surface->set_parent);
3255
3256     policy_surface->ack_conformant_region.notify =
3257         policy_surface_handle_ack_conformant_region;
3258     ds_tizen_policy_surface_add_ack_conformant_region_listener(
3259         policy_surface->policy_surface, &policy_surface->ack_conformant_region);
3260
3261     policy_surface->set_video.notify = policy_surface_handle_set_video;
3262     ds_tizen_policy_surface_add_set_video_listener(policy_surface->policy_surface,
3263         &policy_surface->set_video);
3264
3265     policy_surface->show.notify = policy_surface_handle_show;
3266     ds_tizen_policy_surface_add_show_listener(policy_surface->policy_surface,
3267         &policy_surface->show);
3268
3269     policy_surface->hide.notify = policy_surface_handle_hide;
3270     ds_tizen_policy_surface_add_hide_listener(policy_surface->policy_surface,
3271         &policy_surface->hide);
3272
3273     policy_surface->set_parent_with_below.notify =
3274         policy_surface_handle_set_parent_with_below;
3275     ds_tizen_policy_surface_add_set_parent_with_below_listener(
3276             policy_surface->policy_surface, &policy_surface->set_parent_with_below);
3277
3278
3279     wl_list_init(&policy_surface->visibilities);
3280     wl_list_init(&policy_surface->positions);
3281     wl_list_init(&policy_surface->subsurface_watchers);
3282
3283     wl_list_insert(&policy->policy_surfaces, &policy_surface->link);
3284 }
3285
3286 static void
3287 policy_handle_activate_below_by_univeral_id(struct wl_listener *listener, void *data)
3288 {
3289     struct tinyds_policy *policy;
3290
3291     policy = wl_container_of(listener, policy, activate_below_by_univeral_id);
3292
3293     ds_inf("Policy(%p) activate_below_by_univeral_id", policy);
3294
3295     // TODO:
3296 }
3297
3298 static void
3299 policy_handle_lower_by_universal_id(struct wl_listener *listener, void *data)
3300 {
3301     struct tinyds_policy *policy;
3302
3303     policy = wl_container_of(listener, policy, lower_by_universal_id);
3304
3305     ds_inf("Policy(%p) lower_by_universal_id", policy);
3306
3307     // TODO:
3308 }
3309
3310 static void
3311 policy_handle_set_transient_for(struct wl_listener *listener, void *data)
3312 {
3313     struct tinyds_policy *policy;
3314
3315     policy = wl_container_of(listener, policy, set_transient_for);
3316
3317     ds_inf("Policy(%p) set_transient_for", policy);
3318
3319     // TODO:
3320 }
3321
3322 static void
3323 policy_handle_unset_transient_for(struct wl_listener *listener, void *data)
3324 {
3325     struct tinyds_policy *policy;
3326
3327     policy = wl_container_of(listener, policy, unset_transient_for);
3328
3329     ds_inf("Policy(%p) unset_transient_for", policy);
3330
3331     // TODO:
3332 }
3333
3334 static void
3335 policy_handle_place_subsurface_below_parent(struct wl_listener *listener, void *data)
3336 {
3337     struct tinyds_policy *policy;
3338
3339     policy = wl_container_of(listener, policy, place_subsurface_below_parent);
3340
3341     ds_inf("Policy(%p) place_subsurface_below_parent", policy);
3342
3343     // TODO:
3344 }
3345
3346 static void
3347 policy_handle_set_subsurface_stand_alone(struct wl_listener *listener, void *data)
3348 {
3349     struct tinyds_policy *policy;
3350
3351     policy = wl_container_of(listener, policy, set_subsurface_stand_alone);
3352
3353     ds_inf("Policy(%p) set_subsurface_stand_alone", policy);
3354
3355     // TODO:
3356 }
3357
3358 static void
3359 policy_handle_set_background_state(struct wl_listener *listener, void *data)
3360 {
3361     struct tinyds_policy *policy;
3362
3363     policy = wl_container_of(listener, policy, set_background_state);
3364
3365     ds_inf("Policy(%p) set_background_state", policy);
3366
3367     // TODO:
3368 }
3369
3370 static void
3371 policy_handle_unset_background_state(struct wl_listener *listener, void *data)
3372 {
3373     struct tinyds_policy *policy;
3374
3375     policy = wl_container_of(listener, policy, unset_background_state);
3376
3377     ds_inf("Policy(%p) unset_background_state", policy);
3378
3379     // TODO:
3380 }
3381
3382 static void
3383 policy_handle_add_activate_above_by_universal_id(struct wl_listener *listener, void *data)
3384 {
3385     struct tinyds_policy *policy;
3386
3387     policy = wl_container_of(listener, policy, add_activate_above_by_universal_id);
3388
3389     ds_inf("Policy(%p) add_activate_above_by_universal_id", policy);
3390
3391     // TODO:
3392 }
3393
3394 static void
3395 policy_handle_set_appid(struct wl_listener *listener, void *data)
3396 {
3397     struct tinyds_policy *policy;
3398
3399     policy = wl_container_of(listener, policy, set_appid);
3400
3401     ds_inf("Policy(%p) set_appid", policy);
3402
3403     // TODO:
3404 }
3405
3406 static void
3407 policy_handle_set_transient_for_below(struct wl_listener *listener, void *data)
3408 {
3409     struct tinyds_policy *policy;
3410
3411     policy = wl_container_of(listener, policy, set_transient_for_below);
3412
3413     ds_inf("Policy(%p) set_transient_for_below", policy);
3414
3415     // TODO:
3416 }
3417
3418 static bool
3419 new_policy(struct tinyds_server *server)
3420 {
3421     struct tinyds_policy *policy;
3422
3423     policy = calloc(1, sizeof *policy);
3424     if (!policy)
3425         return false;
3426
3427     policy->policy = ds_tizen_policy_create(server->display);
3428     if (!policy->policy) {
3429         free(policy);
3430         ds_err("Could not create ds_tizen_policy");
3431         return false;
3432     }
3433
3434     policy->destroy.notify = policy_handle_destroy;
3435     ds_tizen_policy_add_destroy_listener(policy->policy, &policy->destroy);
3436
3437     policy->new_surface.notify = policy_handle_new_surface;
3438     ds_tizen_policy_add_new_surface_listener(policy->policy, &policy->new_surface);
3439
3440     policy->activate_below_by_univeral_id.notify =
3441         policy_handle_activate_below_by_univeral_id;
3442     ds_tizen_policy_add_activate_below_by_univeral_id_listener(policy->policy,
3443         &policy->activate_below_by_univeral_id);
3444
3445     policy->lower_by_universal_id.notify = policy_handle_lower_by_universal_id;
3446     ds_tizen_policy_add_lower_by_universal_id_listener(policy->policy,
3447         &policy->lower_by_universal_id);
3448
3449     policy->set_transient_for.notify = policy_handle_set_transient_for;
3450     ds_tizen_policy_add_set_transient_for_listener(policy->policy,
3451         &policy->set_transient_for);
3452
3453     policy->unset_transient_for.notify = policy_handle_unset_transient_for;
3454     ds_tizen_policy_add_unset_transient_for_listener(policy->policy,
3455         &policy->unset_transient_for);
3456
3457     policy->place_subsurface_below_parent.notify =
3458         policy_handle_place_subsurface_below_parent;
3459     ds_tizen_policy_add_place_subsurface_below_parent_listener(policy->policy,
3460         &policy->place_subsurface_below_parent);
3461
3462     policy->set_subsurface_stand_alone.notify =
3463         policy_handle_set_subsurface_stand_alone;
3464     ds_tizen_policy_add_set_subsurface_stand_alone_listener(policy->policy,
3465         &policy->set_subsurface_stand_alone);
3466
3467     policy->set_background_state.notify = policy_handle_set_background_state;
3468     ds_tizen_policy_add_set_background_state_listener(policy->policy,
3469         &policy->set_background_state);
3470
3471     policy->unset_background_state.notify = policy_handle_unset_background_state;
3472     ds_tizen_policy_add_unset_background_state_listener(policy->policy,
3473         &policy->unset_background_state);
3474
3475     policy->add_activate_above_by_universal_id.notify =
3476         policy_handle_add_activate_above_by_universal_id;
3477     ds_tizen_policy_add_activate_above_by_universal_id_listener(policy->policy,
3478         &policy->add_activate_above_by_universal_id);
3479
3480     policy->set_appid.notify = policy_handle_set_appid;
3481     ds_tizen_policy_add_set_appid_listener(policy->policy, &policy->set_appid);
3482
3483     policy->set_transient_for_below.notify =
3484         policy_handle_set_transient_for_below;
3485     ds_tizen_policy_add_set_transient_for_below_listener(policy->policy,
3486         &policy->set_transient_for_below);
3487
3488     wl_list_init(&policy->policy_surfaces);
3489
3490     server->policy = policy;
3491
3492     ds_inf("Policy (%p) created", policy);
3493
3494     return true;
3495 }