change wl_signal_emit_mutable into wl_signal_emit
[platform/core/uifw/libds-tizen.git] / examples / tinyds-tdm.c
1 #include "tinyds-tdm.h"
2
3 struct tinyds_output
4 {
5     struct tinyds_server *server;
6     struct ds_output *ds_output;
7     struct ds_allocator *allocator;
8 #ifdef USE_TDM_BUFFER_QUEUE
9     struct tinyds_renderer renderer;
10     struct ds_tdm_buffer_queue *buffer_queue;
11     struct wl_listener buffer_queue_acquirable;
12 #else
13     struct ds_swapchain *swapchain;
14 #endif
15     struct ds_buffer *front_buffer;
16
17     struct wl_listener output_destroy;
18     struct wl_listener output_frame;
19
20     int width, height;
21
22     struct wl_event_source *idle_commit;
23     bool committable;
24     bool damaged;
25     bool target_updated;
26
27     struct ds_tdm_output_hwc *hwc;
28     struct ds_tdm_output_hwc_window *bg_hwc_window;
29
30 #ifdef USE_CURSOR
31     bool cursor_enabled;
32     struct ds_tdm_output_hwc_window *cursor_hwc_window;
33 #endif
34 };
35
36 struct tinyds_pointer
37 {
38     struct ds_input_device *dev;
39     struct tinyds_server *server;
40
41     struct tinyds_view *focused_view;
42
43     struct wl_listener destroy;
44     struct wl_listener motion; //relative
45     struct wl_listener button;
46     struct wl_listener frame;
47     struct wl_list link; //tinyds_server::pointers
48 };
49
50 struct tinyds_keyboard
51 {
52     struct ds_input_device *dev;
53     struct tinyds_server *server;
54
55     struct wl_listener destroy;
56     struct wl_listener key;
57     struct wl_list link; //tinyds_server::keyboards
58 };
59
60 struct tinyds_touch
61 {
62     struct ds_input_device *dev;
63     struct tinyds_server *server;
64
65     struct wl_listener destroy;
66     struct wl_listener down;
67     struct wl_listener up;
68     struct wl_listener motion;
69 };
70
71 struct tinyds_text_input {
72     struct ds_tizen_text_input *input;
73     struct ds_tizen_text_input_manager *text_input_mgr;
74
75     struct tinyds_server *server;
76     struct ds_surface *surface;
77
78     struct wl_list input_methods;
79
80     struct wl_listener mgr_destroy;
81     struct wl_listener new_text_input;
82
83     struct wl_listener destroy;
84     struct wl_listener text_input_activate;
85     struct wl_listener text_input_deactivate;
86     struct wl_listener text_input_reset;
87     struct wl_listener text_input_set_content_type;
88     struct wl_listener text_input_invoke_action;
89     struct wl_listener text_input_commit_state;
90     struct wl_listener text_input_set_preferred_language;
91 };
92
93 struct tinyds_input_method {
94     struct ds_tizen_input_method *input_method;
95     struct ds_tizen_input_method_manager *input_method_mgr;
96
97     struct tinyds_server *server;
98     struct tinyds_text_input *input;
99     struct tinyds_input_method_context *context;
100
101     struct wl_list link;
102
103     struct wl_listener destroy;
104     struct wl_listener mgr_destroy;
105 };
106
107 struct tinyds_input_method_context {
108     struct ds_tizen_input_method_context *context;
109
110     struct tinyds_server *server;
111     struct tinyds_text_input *input;
112     struct tinyds_input_method *input_method;
113
114     struct wl_listener destroy;
115
116     struct wl_listener im_context_commit_string;
117     struct wl_listener im_context_preedit_string;
118     struct wl_listener im_context_preedit_styling;
119     struct wl_listener im_context_preedit_cursor;
120     struct wl_listener im_context_delete_surrounding_text;
121     struct wl_listener im_context_cursor_position;
122     struct wl_listener im_context_modifiers_map;
123     struct wl_listener im_context_keysym;
124     struct wl_listener im_context_grab_keyboard;
125     struct wl_listener im_context_key;
126     struct wl_listener im_context_modifiers;
127     struct wl_listener im_context_language;
128     struct wl_listener im_context_text_direction;
129 };
130
131 struct tinyds_server tinyds;
132
133 static bool init_server(struct tinyds_server *server, struct wl_display *display);
134 static int server_dispatch_stdin(int fd, uint32_t mask, void *data);
135 static void output_handle_destroy(struct wl_listener *listener, void *data);
136 static void output_handle_frame(struct wl_listener *listener, void *data);
137 static void draw_server_with_damage(struct tinyds_server *server);
138 static void draw_output(struct tinyds_output *output);
139 static void output_swap_buffer(struct tinyds_output *output,
140         struct ds_buffer *buffer);
141 static void view_send_frame_done(struct tinyds_view *view);
142 static void output_hwc_init(struct tinyds_output *output);
143 static void output_schedule_commit(struct tinyds_output *output);
144 static void output_commit(struct tinyds_output *output);
145 #ifdef USE_TDM_BUFFER_QUEUE
146 static void output_buffer_queue_init(struct tinyds_output *output);
147 static void output_renderer_init(struct tinyds_output *output);
148 static void output_draw_with_renderer(struct tinyds_output *output);
149 #else
150 static void output_swapchain_init(struct tinyds_output *output,
151         int width, int height, uint32_t format);
152 static void output_draw_with_swapchain(struct tinyds_output *output);
153 static void draw_view(struct tinyds_view *view, pixman_image_t *dst_image);
154 #endif
155 static void server_add_keyboard(struct tinyds_server *server,
156         struct ds_input_device *dev);
157 static void server_add_pointer(struct tinyds_server *server,
158         struct ds_input_device *dev);
159 static void server_add_touch(struct tinyds_server *server,
160         struct ds_input_device *dev);
161 static bool add_new_text_input(struct tinyds_server *server);
162 static bool add_new_input_method(struct tinyds_server *server);
163 static bool add_new_input_method_context(
164         struct tinyds_input_method *input_method,
165         struct tinyds_text_input *text_input);
166
167 static void text_input_mgr_handle_destroy(struct wl_listener *listener,
168         void *data TINYDS_UNUSED);
169 static void text_input_mgr_handle_new_text_input(struct wl_listener *listener,
170         void *data TINYDS_UNUSED);
171
172 static void input_method_mgr_handle_destroy(struct wl_listener *listener,
173         void *data TINYDS_UNUSED);
174
175 static void input_method_handle_destroy(struct wl_listener *listener,
176         void *data TINYDS_UNUSED);
177
178 int
179 main(void)
180 {
181     struct tinyds_server *server = &tinyds;
182     struct wl_display *display;
183     struct wl_event_loop *loop;
184     const char *socket;
185     bool res;
186
187     ds_log_init(DS_INF, NULL);
188
189     display = wl_display_create();
190     assert(display);
191
192     res = init_server(server, display);
193     assert(res);
194
195     socket = wl_display_add_socket_auto(display);
196     assert(socket);
197
198     ds_backend_start(server->backend);
199     ds_backend_start(server->input_backend);
200
201     setenv("WAYLAND_DISPLAY", socket, true);
202
203     ds_inf("Running Wayland compositor on WAYLAND_DISPLAY=%s", socket);
204
205     loop = wl_display_get_event_loop(display);
206     server->stdin_source = wl_event_loop_add_fd(loop, STDIN_FILENO,
207             WL_EVENT_READABLE, server_dispatch_stdin, server);
208
209     wl_display_run(display);
210
211     protocol_trace_enable(false);
212     protocol_trace_fini();
213
214     wl_display_destroy_clients(display);
215     wl_display_destroy(display);
216
217     return 0;
218 }
219
220 static void
221 view_populate_pid(struct tinyds_view *view)
222 {
223     struct ds_surface *surface;
224     struct wl_client *client = NULL;
225     pid_t pid;
226
227     surface = ds_xdg_surface_get_surface(view->xdg_surface);
228     if (!surface)
229         return;
230
231     client = wl_resource_get_client(ds_surface_get_wl_resource(surface));
232     if (!client)
233         return;
234
235     wl_client_get_credentials(client, &pid, NULL, NULL);
236     view->pid = pid;
237
238     ds_inf("view pid(%u)", pid);
239
240     view->effect_type = tinyds_launch_get_effect_type(
241             view->server->launch, pid);
242     tinyds_launch_unset_effect_type(view->server->launch, pid);
243
244     ds_inf("view effect_type(%d)", view->effect_type);
245 }
246
247 static void
248 view_handle_xdg_surface_map(struct wl_listener *listener,
249         void *data TINYDS_UNUSED)
250 {
251     struct tinyds_view *view;
252     struct ds_keyboard *keyboard;
253     struct tinyds_keyboard *kbd;
254
255     view = wl_container_of(listener, view, xdg_surface_map);
256     view->mapped = true;
257
258     view_populate_pid(view);
259
260     wl_list_for_each(kbd, &view->server->keyboards, link) {
261         keyboard = ds_input_device_get_keyboard(kbd->dev);
262         if (keyboard != NULL) {
263             ds_seat_keyboard_notify_enter(view->server->seat,
264                     ds_xdg_surface_get_surface(view->xdg_surface),
265                     keyboard->keycodes, keyboard->num_keycodes,
266                     &keyboard->modifiers);
267             return;
268         }
269     }
270 }
271
272 static void
273 view_handle_xdg_surface_unmap(struct wl_listener *listener,
274         void *data TINYDS_UNUSED)
275 {
276     struct tinyds_view *view;
277
278     view = wl_container_of(listener, view, xdg_surface_unmap);
279     view->mapped = false;
280 }
281
282 static void
283 view_handle_xdg_surface_destroy(struct wl_listener *listener,
284         void *data TINYDS_UNUSED) 
285 {
286     struct tinyds_view *view;
287
288     view = wl_container_of(listener, view, xdg_surface_destroy);
289
290     draw_server_with_damage(view->server);
291
292     ds_tdm_output_hwc_window_destroy(view->hwc_window);
293
294     wl_list_remove(&view->xdg_surface_destroy.link);
295     wl_list_remove(&view->xdg_surface_map.link);
296     wl_list_remove(&view->xdg_surface_unmap.link);
297     wl_list_remove(&view->surface_commit.link);
298     wl_list_remove(&view->link);
299     free(view);
300 }
301
302 static void
303 view_handle_surface_commit(struct wl_listener *listener,
304         void *data TINYDS_UNUSED)
305 {
306     struct tinyds_view *view;
307
308     view = wl_container_of(listener, view, surface_commit);
309     draw_server_with_damage(view->server);
310 }
311
312 static void
313 server_new_xdg_surface(struct wl_listener *listener, void *data)
314 {
315     static unsigned int seedx = 1;
316     static unsigned int seedy = 43210;
317     struct tinyds_server *server;
318     struct tinyds_view *view;
319     struct ds_xdg_surface *xdg_surface;
320
321     server = wl_container_of(listener, server, new_xdg_surface);
322     xdg_surface = data;
323
324     ds_inf("New xdg_surface(%p)", (void *)xdg_surface);
325
326     view = calloc(1, sizeof *view);
327     assert(view);
328
329     view->server = server;
330     view->xdg_surface = xdg_surface;
331
332     view->xdg_surface_map.notify = view_handle_xdg_surface_map;
333     ds_xdg_surface_add_map_listener(xdg_surface,
334             &view->xdg_surface_map);
335
336     view->xdg_surface_unmap.notify = view_handle_xdg_surface_unmap;
337     ds_xdg_surface_add_unmap_listener(xdg_surface,
338             &view->xdg_surface_unmap);
339
340     view->xdg_surface_destroy.notify = view_handle_xdg_surface_destroy;
341     ds_xdg_surface_add_destroy_listener(xdg_surface,
342             &view->xdg_surface_destroy);
343
344     view->surface_commit.notify = view_handle_surface_commit;
345     ds_surface_add_commit_listener(
346             ds_xdg_surface_get_surface(xdg_surface),
347             &view->surface_commit);
348
349     view->x = rand_r(&seedx) % 1000;
350     view->y = rand_r(&seedy) % 500;
351
352     view->hwc_window = ds_tdm_output_hwc_window_create(server->output->hwc);
353     assert(view->hwc_window);
354
355     wl_list_insert(server->views.prev, &view->link);
356
357     view->pid = 0;
358     view->effect_type = -1;
359
360     ds_inf("view at (%d, %d)", view->x, view->y);
361 }
362
363 static void
364 backend_handle_new_output(struct wl_listener *listener, void *data)
365 {
366     struct tinyds_server *server;
367     struct tinyds_output *output;
368     struct ds_output *ds_output;
369     const struct ds_output_mode *mode;
370     struct ds_tdm_box src_box;
371
372     server = wl_container_of(listener, server, new_output);
373     ds_output = data;
374
375     ds_inf("New output(%p)", ds_output);
376
377     if (server->output)
378         return;
379
380     mode = ds_output_get_preferred_mode(ds_output);
381     ds_output_set_mode(ds_output, mode);
382
383     output = calloc(1, sizeof *output);
384     if (!output)
385         return;
386
387     output->server = server;
388     output->ds_output = ds_output;
389     output->width = mode->width;
390     output->height = mode->height;
391     output->damaged = true;
392     output->committable = true;
393
394     output_hwc_init(output);
395
396 #ifdef USE_TDM_BUFFER_QUEUE
397     output_buffer_queue_init(output);
398     output_renderer_init(output);
399 #else
400     output_swapchain_init(output, mode->width, mode->height,
401             DRM_FORMAT_XRGB8888);
402 #endif
403
404     output->bg_hwc_window = ds_tdm_output_hwc_window_create(output->hwc);
405     assert(output->bg_hwc_window);
406
407     src_box.x = 0;
408     src_box.y = 0;
409     src_box.width = output->width;
410     src_box.height = output->height;
411
412     ds_tdm_output_hwc_window_set_src_box(output->bg_hwc_window, &src_box);
413     ds_tdm_output_hwc_window_set_position(output->bg_hwc_window, 0, 0);
414     ds_tdm_output_hwc_window_set_dest_size(output->bg_hwc_window, output->width, output->height);
415     ds_tdm_output_hwc_window_set_transform(output->bg_hwc_window, WL_OUTPUT_TRANSFORM_NORMAL);
416
417 #ifdef USE_CURSOR
418     output->cursor_enabled = false;
419 #endif
420
421     output->output_destroy.notify = output_handle_destroy;
422     ds_output_add_destroy_listener(ds_output, &output->output_destroy);
423
424     output->output_frame.notify = output_handle_frame;
425     ds_output_add_frame_listener(ds_output, &output->output_frame);
426
427     tinyds_input_devicemgr_set_output_size(server->input_devicemgr, (uint32_t)output->width, (uint32_t)output->height);
428
429     server->output = output;
430     server->output_x = (double)(output->width) / 2;
431     server->output_y = (double)(output->height) / 2;
432
433     output_schedule_commit(output);
434 }
435
436 static void
437 backend_handle_new_input(struct wl_listener *listener, void *data)
438 {
439     struct tinyds_server *server;
440     struct ds_input_device *dev = data;
441     enum ds_input_device_type dev_type;
442
443     server = wl_container_of(listener, server, new_input);
444
445     dev_type = ds_input_device_get_type(dev);
446
447     switch (dev_type) {
448         case DS_INPUT_DEVICE_KEYBOARD:
449             server_add_keyboard(server, dev);
450             server->seat_caps |= WL_SEAT_CAPABILITY_KEYBOARD;
451             break;
452         case DS_INPUT_DEVICE_TOUCH:
453             server_add_touch(server, dev);
454             server->seat_caps |= WL_SEAT_CAPABILITY_TOUCH;
455             break;
456         case DS_INPUT_DEVICE_POINTER:
457             server_add_pointer(server, dev);
458             server->seat_caps |= WL_SEAT_CAPABILITY_POINTER;
459             break;
460         default:
461             ds_err("Unknown type(%d) of ds_input_device", dev_type);
462             break;
463     }
464
465     ds_seat_set_capabilities(server->seat, server->seat_caps);
466 }
467
468 static void
469 dpms_free_func(void *data)
470 {
471     struct tinyds_server *server = (struct tinyds_server *)data;
472
473     server->dpms = NULL;
474 }
475
476 static void
477 policy_free_func(void *data)
478 {
479     struct tinyds_server *server = (struct tinyds_server *)data;
480
481     server->policy = NULL;
482 }
483
484 static void
485 launch_free_func(void *data)
486 {
487     struct tinyds_server *server = (struct tinyds_server *)data;
488
489     server->launch = NULL;
490 }
491
492 static void
493 input_devicemgr_free_func(void *data)
494 {
495     struct tinyds_server *server = (struct tinyds_server *)data;
496
497     server->input_devicemgr = NULL;
498 }
499
500 static bool
501 init_server(struct tinyds_server *server, struct wl_display *display)
502 {
503     server->display = display;
504
505     wl_list_init(&server->views);
506
507     if (wl_display_init_shm(display) != 0)
508         return false;
509
510     server->backend = ds_tdm_backend_create(display);
511     if (!server->backend)
512         return false;
513
514     server->input_backend = ds_libinput_backend_create(display);
515     if (!server->input_backend) {
516         ds_backend_destroy(server->backend);
517         return false;
518     }
519
520     server->new_output.notify = backend_handle_new_output;
521     ds_backend_add_new_output_listener(server->backend,
522             &server->new_output);
523
524     wl_list_init(&server->keyboards);
525     wl_list_init(&server->pointers);
526     server->new_input.notify = backend_handle_new_input;
527     ds_backend_add_new_input_listener(server->input_backend, &server->new_input);
528
529     server->compositor = ds_compositor_create(display);
530     if (!server->compositor)
531         goto err;
532
533     server->tbm_server = ds_tbm_server_create(display);
534     if (!server->tbm_server)
535         goto err;
536
537     server->xdg_shell = ds_xdg_shell_create(display);
538     if (!server->xdg_shell)
539         goto err;
540
541     server->new_xdg_surface.notify = server_new_xdg_surface;
542     ds_xdg_shell_add_new_surface_listener(server->xdg_shell,
543             &server->new_xdg_surface);
544
545     server->dpms = tinyds_dpms_init(server->display, dpms_free_func, (void *)server);
546     if (!server->dpms)
547         goto err;
548
549     server->policy = tinyds_policy_init(server->display, policy_free_func, (void *)server);
550     if (!server->policy)
551         goto err;
552
553     server->launch = tinyds_launch_init(server->display, launch_free_func, (void *)server);
554     if (!server->launch)
555         goto err;
556
557     server->seat = ds_seat_create(display, "seat0" /* arbitrary name */);
558     if (!server->seat)
559         goto err;
560     server->seat_caps = 0;
561
562     server->input_devicemgr = tinyds_input_devicemgr_init(server->input_backend,
563              server->seat, input_devicemgr_free_func, (void *)server);
564     if (!server->input_devicemgr)
565         goto err;
566
567     if (!add_new_text_input(server))
568         goto err;
569
570     if (!add_new_input_method(server))
571         goto err;
572
573     if (protocol_trace_init(display))
574         protocol_trace_enable(true);
575
576     return true;
577
578 err:
579     ds_backend_destroy(server->backend);
580     ds_backend_destroy(server->input_backend);
581
582     return false;
583 }
584
585 static void
586 output_handle_destroy(struct wl_listener *listener, void *data TINYDS_UNUSED)
587 {
588     struct tinyds_output *output =
589         wl_container_of(listener, output, output_destroy);
590
591     if (output->bg_hwc_window)
592         ds_tdm_output_hwc_window_destroy(output->bg_hwc_window);
593
594     wl_list_remove(&output->output_destroy.link);
595     wl_list_remove(&output->output_frame.link);
596
597     if (output->front_buffer)
598         ds_buffer_unlock(output->front_buffer);
599
600 #ifdef USE_TDM_BUFFER_QUEUE
601     fini_renderer(&output->renderer);
602 #else
603     if (output->swapchain)
604         ds_swapchain_destroy(output->swapchain);
605
606     if (output->allocator)
607         ds_allocator_destroy(output->allocator);
608 #endif
609
610     wl_display_terminate(output->server->display);
611
612     output->server->output = NULL;
613
614     free(output);
615 }
616
617 static void
618 output_commit(struct tinyds_output *output)
619 {
620     uint32_t num_changed = 0;
621     uint32_t num_windows = 0, current_num_windows = 0;
622     struct ds_tdm_output_hwc_window **composited_hwc_windows = NULL;
623     struct ds_tdm_output_hwc_window **changed_hwc_windows = NULL;
624     enum ds_tdm_output_hwc_window_composition composition;
625     struct tinyds_view *view;
626     size_t i;
627     bool need_target = false;
628     bool fully_obscured = false;
629     struct ds_buffer *ds_buffer;
630     struct ds_tdm_box src_box;
631     int w = 0, h = 0;
632
633     if (!output->committable)
634         return;
635
636     if (!output->damaged && !output->target_updated)
637         return;
638
639     wl_list_for_each_reverse(view, &output->server->views, link) {
640         if (!view->hwc_window)
641             continue;
642
643         ds_buffer = ds_surface_get_buffer(
644                 ds_xdg_surface_get_surface(view->xdg_surface));
645         if (!ds_buffer)
646             continue;
647
648         if (!view->mapped)
649             continue;
650
651         num_windows++;
652
653         ds_buffer_get_size(ds_buffer, &w, &h);
654
655         if ((output->width <= w) && (output->height <= h))
656             fully_obscured = true;
657     }
658
659     if (fully_obscured) {
660         ds_tdm_output_hwc_window_set_composition(output->bg_hwc_window,
661                 DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_NONE);
662     } else {
663         ds_tdm_output_hwc_window_set_composition(output->bg_hwc_window,
664                 DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_CLIENT);
665         num_windows++;
666         need_target = true;
667     }
668
669 #ifdef USE_CURSOR
670     if (output->cursor_hwc_window) {
671         src_box.x = 0;
672         src_box.y = 0;
673         src_box.width = CURSOR_W;
674         src_box.height = CURSOR_H;
675
676         ds_tdm_output_hwc_window_set_src_box(output->cursor_hwc_window, &src_box);
677         ds_tdm_output_hwc_window_set_position(output->cursor_hwc_window, output->server->output_x, output->server->output_y);
678         ds_tdm_output_hwc_window_set_dest_size(output->cursor_hwc_window, CURSOR_W, CURSOR_H);
679         ds_tdm_output_hwc_window_set_transform(output->cursor_hwc_window, WL_OUTPUT_TRANSFORM_NORMAL);
680
681         ds_tdm_output_hwc_window_set_composition(output->cursor_hwc_window,
682                     DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_CLIENT);
683         num_windows++;
684         need_target = true;
685     }
686 #endif
687
688     if (num_windows) {
689         composited_hwc_windows = calloc(num_windows, sizeof *composited_hwc_windows);
690         if (!composited_hwc_windows)
691             return;
692
693         wl_list_for_each_reverse(view, &output->server->views, link) {
694             if (!view->hwc_window)
695                 continue;
696
697             ds_buffer = ds_surface_get_buffer(
698                     ds_xdg_surface_get_surface(view->xdg_surface));
699             if (!ds_buffer)
700                 continue;
701
702             ds_tdm_output_hwc_window_set_buffer(view->hwc_window, ds_buffer);
703
704             ds_buffer_get_size(ds_buffer, &w, &h);
705
706             src_box.x = 0;
707             src_box.y = 0;
708             src_box.width = w;
709             src_box.height = h;
710
711             ds_tdm_output_hwc_window_set_src_box(view->hwc_window, &src_box);
712             ds_tdm_output_hwc_window_set_position(view->hwc_window, view->x, view->y);
713             ds_tdm_output_hwc_window_set_dest_size(view->hwc_window, w, h);
714             ds_tdm_output_hwc_window_set_transform(view->hwc_window, WL_OUTPUT_TRANSFORM_NORMAL);
715
716             if (view->mapped) {
717 #ifdef USE_CURSOR
718                 ds_tdm_output_hwc_window_set_composition(view->hwc_window,
719                         DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_CLIENT);
720 #endif
721                 ds_tdm_output_hwc_window_set_composition(view->hwc_window,
722                         DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_DEVICE);
723
724                 composited_hwc_windows[current_num_windows] = view->hwc_window;
725                 current_num_windows++;
726             } else {
727                 ds_tdm_output_hwc_window_set_composition(view->hwc_window,
728                         DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_NONE);
729             }
730         }
731
732         if (!fully_obscured) {
733             composited_hwc_windows[current_num_windows] = output->bg_hwc_window;
734             current_num_windows++;
735         }
736
737 #ifdef USE_CURSOR
738         if (output->cursor_hwc_window) {
739             composited_hwc_windows[current_num_windows] = output->cursor_hwc_window;
740             current_num_windows++;
741         }
742 #endif
743     }
744
745     if (!ds_tdm_output_hwc_validate(output->hwc, composited_hwc_windows,
746             num_windows, &num_changed)) {
747         free(composited_hwc_windows);
748         ds_err("Could not hwc validate");
749         return;
750     }
751
752     if (composited_hwc_windows)
753         free(composited_hwc_windows);
754
755     if (num_changed > 0) {
756         changed_hwc_windows = calloc(num_windows, sizeof *changed_hwc_windows);
757         if (!changed_hwc_windows)
758             return;
759
760         if (!ds_tdm_output_hwc_get_changed_composition(output->hwc, &num_changed,
761                 changed_hwc_windows)) {
762             free(changed_hwc_windows);
763             ds_err("Could not get chaged composition");
764             return;
765         }
766
767         for (i = 0; i < (size_t)num_changed; i++) {
768             composition = ds_tdm_output_hwc_window_get_composition(changed_hwc_windows[i]);
769             if (composition == DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_CLIENT) {
770                 need_target = true;
771                 break;
772             }
773         }
774     }
775
776     if (changed_hwc_windows)
777         free(changed_hwc_windows);
778
779     if (need_target && output->damaged)
780         draw_output(output);
781
782 #ifdef USE_TDM_BUFFER_QUEUE
783     struct ds_buffer *buffer;
784
785     buffer = ds_tdm_buffer_queue_acquire(output->buffer_queue);
786     if (buffer) {
787         if (!ds_tdm_output_hwc_set_client_target_buffer(output->hwc, buffer)) {
788             ds_err("Could not set hwc client target buffer");
789             return;
790         }
791
792         output_swap_buffer(output, buffer);
793     }
794 #endif
795
796     if (!ds_tdm_output_hwc_accept_validation(output->hwc)) {
797         ds_err("Could not hwc accept validateion");
798         return;
799     }
800
801     ds_output_commit(output->ds_output);
802
803     output->committable = false;
804     output->damaged = false;
805     output->target_updated = false;
806
807     wl_list_for_each(view, &output->server->views, link) {
808         if (!view->mapped)
809             continue;
810
811         composition = ds_tdm_output_hwc_window_get_composition(view->hwc_window);
812         if ((composition == DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_DEVICE) ||
813             (composition == DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_VIDEO) ||
814             (composition == DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_CURSOR))
815             view_send_frame_done(view);
816     }
817
818     ds_dbg("output:%p commit", output);
819 }
820
821 static void
822 output_handle_frame(struct wl_listener *listener, void *data TINYDS_UNUSED)
823 {
824     struct tinyds_output *output =
825         wl_container_of(listener, output, output_frame);
826
827     ds_dbg("output:%p handle frame", output);
828
829     output->committable = true;
830
831     output_commit(output);
832 }
833
834 static void
835 draw_server_with_damage(struct tinyds_server *server)
836 {
837     server->output->damaged = true;
838     output_schedule_commit(server->output);
839 }
840
841 static void
842 output_hwc_init(struct tinyds_output *output)
843 {
844     struct ds_tdm_output *tdm_output;
845
846     tdm_output = ds_tdm_output_from_output(output->ds_output);
847     assert(tdm_output);
848
849     output->hwc = ds_tdm_output_get_hwc(tdm_output);
850     assert(output->hwc);
851
852     ds_tdm_output_hwc_set_enabled(output->hwc, true);
853 }
854
855 #ifdef USE_TDM_BUFFER_QUEUE
856 static void
857 output_handle_buffer_queue_acquirable(struct wl_listener *listener,
858         void *data TINYDS_UNUSED)
859 {
860     struct tinyds_output *output;
861
862     output = wl_container_of(listener, output, buffer_queue_acquirable);
863
864     output->target_updated = true;
865     output_schedule_commit(output);
866 }
867
868 static void
869 output_buffer_queue_init(struct tinyds_output *output)
870 {
871     struct ds_tdm_output *tdm_output;
872
873     tdm_output = ds_tdm_output_from_output(output->ds_output);
874     assert(tdm_output);
875
876     output->buffer_queue = ds_tdm_output_get_buffer_queue(tdm_output);
877     assert(output->buffer_queue);
878
879     output->buffer_queue_acquirable.notify =
880         output_handle_buffer_queue_acquirable;
881     ds_tdm_buffer_queue_add_acquirable_listener(output->buffer_queue,
882             &output->buffer_queue_acquirable);
883 }
884
885 static void
886 output_renderer_init(struct tinyds_output *output)
887 {
888     init_renderer(&output->renderer);
889
890     renderer_set_surface_queue(&output->renderer,
891             ds_tdm_buffer_queue_get_native_queue(output->buffer_queue));
892
893     renderer_set_bg_color(&output->renderer, 80, 80, 80);
894 }
895
896 static void
897 output_draw_with_renderer(struct tinyds_output *output)
898 {
899     struct tinyds_view *view;
900
901     ds_dbg(">> BEGIN UPDATE TEXTURES");
902
903     wl_list_for_each(view, &output->server->views, link) {
904         struct ds_buffer *ds_buffer;
905         struct ds_tbm_client_buffer *tbm_buffer;
906         tbm_surface_h surface;
907         enum ds_tdm_output_hwc_window_composition composition;
908
909         if (!view->mapped)
910             continue;
911
912         composition = ds_tdm_output_hwc_window_get_composition(view->hwc_window);
913         if ((composition == DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_DEVICE) ||
914             (composition == DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_VIDEO) ||
915             (composition == DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_CURSOR))
916             continue;
917
918         ds_buffer = ds_surface_get_buffer(
919                 ds_xdg_surface_get_surface(view->xdg_surface));
920         if (!ds_buffer)
921             continue;
922
923         tbm_buffer = ds_tbm_client_buffer_from_buffer(ds_buffer);
924         if (!tbm_buffer)
925             continue;
926
927         surface = ds_tbm_client_buffer_get_tbm_surface(tbm_buffer);
928         if (!surface)
929             continue;
930
931         renderer_add_texture(&output->renderer, surface, view->x, view->y);
932
933         view_send_frame_done(view);
934     }
935
936     ds_dbg("<< END UPDATE TEXTURES");
937
938     renderer_draw(&output->renderer);
939 }
940 #else
941 static void
942 output_swapchain_init(struct tinyds_output *output,
943         int width, int height, uint32_t format)
944
945 {
946     output->allocator = ds_tbm_allocator_create();
947     assert(output->allocator);
948
949     output->swapchain = ds_swapchain_create(output->allocator,
950             width, height, format);
951     assert(output->swapchain);
952 }
953
954 static void
955 output_draw_with_swapchain(struct tinyds_output *output)
956 {
957     struct tinyds_view *view;
958     struct ds_buffer *output_buffer;
959     pixman_image_t *output_image;
960     enum ds_tdm_output_hwc_window_composition composition;
961
962     output_buffer = ds_swapchain_acquire(output->swapchain, NULL);
963     if (!output_buffer)
964         return;
965
966     output_image = pixman_image_from_buffer(output_buffer,
967             DS_BUFFER_DATA_PTR_ACCESS_WRITE);
968     if (!output_image) {
969         ds_buffer_unlock(output_buffer);
970         return;
971     }
972
973     pixman_image_fill_color(output_image, 80, 80, 80);
974
975     wl_list_for_each(view, &output->server->views, link) {
976         if (!view->mapped)
977             continue;
978
979         composition = ds_tdm_output_hwc_window_get_composition(view->hwc_window);
980         if ((composition == DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_DEVICE) ||
981             (composition == DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_VIDEO) ||
982             (composition == DS_TDM_OUTPUT_HWC_WINDOW_COMPOSITION_CURSOR))
983             continue;
984
985         draw_view(view, output_image);
986     }
987     pixman_image_unref(output_image);
988
989     if (!ds_tdm_output_hwc_set_client_target_buffer(output->hwc, output_buffer)) {
990         ds_err("Could not set hwc client target buffer");
991         ds_buffer_unlock(output_buffer);
992         return;
993     }
994
995     output_swap_buffer(output, output_buffer);
996 }
997
998 static void
999 draw_view(struct tinyds_view *view, pixman_image_t *dst_image)
1000 {
1001     struct ds_buffer *buffer;
1002     pixman_image_t *src_image;
1003
1004     buffer = ds_surface_get_buffer(
1005             ds_xdg_surface_get_surface(view->xdg_surface));
1006     if (!buffer)
1007         return;
1008
1009     src_image = pixman_image_from_buffer(buffer,
1010             DS_BUFFER_DATA_PTR_ACCESS_READ);
1011     pixman_image_composite32(PIXMAN_OP_OVER,
1012             src_image,
1013             NULL,
1014             dst_image,
1015             0, 0, 0, 0,
1016             view->x, view->y,
1017             pixman_image_get_width(src_image),
1018             pixman_image_get_height(src_image));
1019     pixman_image_unref(src_image);
1020
1021     view_send_frame_done(view);
1022 }
1023 #endif
1024
1025 static void
1026 draw_output(struct tinyds_output *output)
1027 {
1028 #ifdef USE_TDM_BUFFER_QUEUE
1029     output_draw_with_renderer(output);
1030 #else
1031     output_draw_with_swapchain(output);
1032 #endif
1033
1034     ds_dbg("output:%p draw", output);
1035 }
1036
1037 static void
1038 output_swap_buffer(struct tinyds_output *output, struct ds_buffer *buffer)
1039 {
1040     ds_output_attach_buffer(output->ds_output, buffer);
1041
1042     if (output->front_buffer)
1043         ds_buffer_unlock(output->front_buffer);
1044     output->front_buffer = buffer;
1045 }
1046
1047 static void
1048 view_send_frame_done(struct tinyds_view *view)
1049 {
1050     struct timespec now;
1051     clock_gettime(CLOCK_MONOTONIC, &now);
1052     ds_surface_send_frame_done(ds_xdg_surface_get_surface(view->xdg_surface),
1053             &now);
1054 }
1055
1056 static int
1057 server_dispatch_stdin(int fd, uint32_t mask, void *data)
1058 {
1059     struct tinyds_server *server = data;
1060
1061     wl_display_terminate(server->display);
1062
1063     return 1;
1064 }
1065
1066 static void
1067 keyboard_handle_device_destroy(struct wl_listener *listener, void *data)
1068 {
1069     struct tinyds_keyboard *kbd;
1070
1071     kbd = wl_container_of(listener, kbd, destroy);
1072
1073     ds_inf("Keyboard(%p) destroyed", kbd);
1074
1075     wl_list_remove(&kbd->destroy.link);
1076     wl_list_remove(&kbd->key.link);
1077     wl_list_remove(&kbd->link);
1078
1079     free(kbd);
1080 }
1081
1082 static bool
1083 server_handle_keybinding(struct tinyds_server *server, xkb_keysym_t sym)
1084 {
1085     switch (sym) {
1086         case XKB_KEY_BackSpace:
1087             wl_display_terminate(server->display);
1088             break;
1089         default:
1090             return false;
1091     }
1092
1093     return true;
1094 }
1095
1096 static void
1097 keyboard_handle_key(struct wl_listener *listener, void *data)
1098 {
1099     struct tinyds_keyboard *kbd;
1100     struct ds_keyboard_event_key *event = data;
1101     struct ds_keyboard *ds_keyboard;
1102     struct xkb_state *xkb_state;
1103     const xkb_keysym_t *syms;
1104     int nsyms;
1105     bool handled = false;
1106
1107     kbd = wl_container_of(listener, kbd, key);
1108
1109     ds_inf("Keyboard(%p) event key: keycode(%d), state(%d), time_msec(%d), "
1110             "update_state(%d)", kbd->dev,
1111             event->keycode, event->state, event->time_msec,
1112             event->update_state);
1113
1114     ds_keyboard = ds_input_device_get_keyboard(kbd->dev);
1115
1116     if (event->state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1117         xkb_state = ds_keyboard_get_xkb_state(ds_keyboard);
1118         if (xkb_state) {
1119             nsyms = xkb_state_key_get_syms(xkb_state, event->keycode + 8,
1120                     &syms);
1121             for (int i = 0; i < nsyms; i++) {
1122                 handled = server_handle_keybinding(kbd->server, syms[i]);
1123             }
1124         }
1125     }
1126
1127     if (!handled) {
1128         ds_seat_keyboard_notify_key(kbd->server->seat, event->time_msec,
1129                 event->keycode, event->state);
1130     }
1131 }
1132
1133 static void
1134 server_add_keyboard(struct tinyds_server *server, struct ds_input_device *dev)
1135 {
1136     struct tinyds_keyboard *kbd;
1137     struct xkb_context *context;
1138     struct xkb_keymap *keymap;
1139
1140     kbd = calloc(1, sizeof *kbd);
1141     assert(kbd);
1142
1143     kbd->dev = dev;
1144     kbd->server = server;
1145
1146     context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
1147     if (!context)
1148         goto err;
1149
1150     keymap = xkb_keymap_new_from_names(context, NULL,
1151             XKB_KEYMAP_COMPILE_NO_FLAGS);
1152
1153     if (!keymap) {
1154         ds_err("Failed to compile keymap");
1155         xkb_context_unref(context);
1156         goto err;
1157     }
1158
1159     ds_keyboard_set_keymap(ds_input_device_get_keyboard(dev), keymap);
1160
1161     xkb_keymap_unref(keymap);
1162     xkb_context_unref(context);
1163
1164     kbd->destroy.notify = keyboard_handle_device_destroy;
1165     ds_input_device_add_destroy_listener(dev, &kbd->destroy);
1166
1167     kbd->key.notify = keyboard_handle_key;
1168     ds_keyboard_add_key_listener(ds_input_device_get_keyboard(dev), &kbd->key);
1169
1170     wl_list_insert(&server->keyboards, &kbd->link);
1171
1172     ds_inf("Keyboard(%p) added", kbd);
1173
1174     return;
1175
1176 err:
1177     free(kbd);
1178 }
1179
1180 struct tinyds_view *
1181 tinyds_server_view_at(struct tinyds_server *server, double lx, double ly,
1182         double *sx, double *sy)
1183 {
1184     struct tinyds_view *view;
1185     struct ds_surface *surface;
1186     struct ds_buffer *buffer;
1187     int x, y, w = 0, h = 0;
1188
1189     wl_list_for_each(view, &server->views, link) {
1190         surface = ds_xdg_surface_get_surface(view->xdg_surface);
1191         buffer = ds_surface_get_buffer(surface);
1192         ds_buffer_get_size(buffer, &w, &h);
1193
1194         x = view->x;
1195         y = view->y;
1196
1197         if (lx >= x && lx <= x + w && ly >= y && ly <= y + h) {
1198             *sx = lx - x;
1199             *sy = ly - y;
1200
1201             return view;
1202         }
1203     }
1204
1205     return NULL;
1206 }
1207
1208 static void
1209 touch_handle_device_destroy(struct wl_listener *listener, void *data)
1210 {
1211     struct tinyds_touch *touch;
1212
1213     touch = wl_container_of(listener, touch, destroy);
1214
1215     ds_inf("Touch(%p) destroyed", touch);
1216
1217     wl_list_remove(&touch->destroy.link);
1218     wl_list_remove(&touch->down.link);
1219     wl_list_remove(&touch->up.link);
1220     wl_list_remove(&touch->motion.link);
1221
1222     free(touch);
1223 }
1224
1225 static void
1226 touch_handle_down(struct wl_listener *listener, void *data)
1227 {
1228     struct ds_touch_event_down *event = data;
1229     struct tinyds_touch *touch;
1230     struct tinyds_view *view;
1231     struct tinyds_server *server;
1232     double sx = 0.f, sy = 0.f;
1233
1234     touch = wl_container_of(listener, touch, down);
1235
1236     server = touch->server;
1237     if (server->output) {
1238         server->output_x = event->x * server->output->width;
1239         server->output_y = event->y * server->output->height;
1240     }
1241
1242     ds_inf("Touch(%p) event down: id(%d) x %.3f y %.3f output_x %.1f output_y %.1f",
1243             touch->dev, event->id, event->x, event->y, server->output_x, server->output_y);
1244
1245     view = tinyds_server_view_at(server, server->output_x, server->output_y, &sx, &sy);
1246
1247     if (view) {
1248         ds_seat_touch_notify_down(touch->server->seat, ds_xdg_surface_get_surface(view->xdg_surface),
1249                 event->time_msec, event->id, sx, sy);
1250     }
1251
1252 #ifdef USE_CURSOR
1253     if (server->output && server->output->cursor_enabled) {
1254         renderer_cursor_update(&server->output->renderer, server->output_x, server->output_y);
1255         draw_server_with_damage(server);
1256     }
1257 #endif
1258 }
1259
1260 static void
1261 touch_handle_up(struct wl_listener *listener, void *data)
1262 {
1263     struct ds_touch_event_up *event = data;
1264     struct tinyds_touch *touch;
1265
1266     touch = wl_container_of(listener, touch, up);
1267
1268     ds_inf("Touch(%p) event up: id(%d) time_msec(%d)",
1269             touch->dev, event->id, event->time_msec);
1270
1271     ds_seat_touch_notify_up(touch->server->seat, event->time_msec, event->id);
1272 }
1273
1274 static void
1275 touch_handle_motion(struct wl_listener *listener, void *data)
1276 {
1277     struct ds_touch_event_motion *event = data;
1278     struct tinyds_touch *touch;
1279     struct tinyds_view *view;
1280     struct tinyds_server *server;
1281     double sx = 0.f, sy = 0.f;
1282
1283     touch = wl_container_of(listener, touch, motion);
1284
1285     server = touch->server;
1286     if (server->output) {
1287         server->output_x = event->x * server->output->width;
1288         server->output_y = event->y * server->output->height;
1289     }
1290
1291     ds_inf("Touch(%p) event motion: id(%d) x %.3f y %.3f output_x %.1f output_y %.1f",
1292             touch->dev, event->id, event->x, event->y, server->output_x, server->output_y);
1293
1294     view = tinyds_server_view_at(server, server->output_x, server->output_y, &sx, &sy);
1295
1296     if (view) {
1297         ds_seat_touch_notify_motion(server->seat, event->time_msec,
1298                 event->id, sx, sy);
1299     }
1300
1301 #ifdef USE_CURSOR
1302     if (server->output && server->output->cursor_enabled) {
1303         renderer_cursor_update(&server->output->renderer, server->output_x, server->output_y);
1304         draw_server_with_damage(server);
1305     }
1306 #endif
1307 }
1308
1309 static void
1310 server_add_touch(struct tinyds_server *server, struct ds_input_device *dev)
1311 {
1312     struct tinyds_touch *touch;
1313
1314     touch = calloc(1, sizeof *touch);
1315     assert(touch);
1316
1317     touch->dev = dev;
1318     touch->server = server;
1319
1320     touch->destroy.notify = touch_handle_device_destroy;
1321     ds_input_device_add_destroy_listener(dev, &touch->destroy);
1322
1323     touch->down.notify = touch_handle_down;
1324     ds_touch_add_down_listener(ds_input_device_get_touch(dev), &touch->down);
1325
1326     touch->up.notify = touch_handle_up;
1327     ds_touch_add_up_listener(ds_input_device_get_touch(dev), &touch->up);
1328
1329     touch->motion.notify = touch_handle_motion;
1330     ds_touch_add_motion_listener(ds_input_device_get_touch(dev), &touch->motion);
1331
1332     ds_inf("Touch(%p) added", touch);
1333 }
1334
1335 static void
1336 pointer_handle_device_destroy(struct wl_listener *listener, void *data)
1337 {
1338     struct tinyds_pointer *pointer;
1339     struct tinyds_server *server;
1340
1341     pointer = wl_container_of(listener, pointer, destroy);
1342
1343     ds_inf("Pointer(%p) destroyed", pointer);
1344
1345     wl_list_remove(&pointer->destroy.link);
1346     wl_list_remove(&pointer->motion.link);
1347     wl_list_remove(&pointer->button.link);
1348     wl_list_remove(&pointer->frame.link);
1349     wl_list_remove(&pointer->link);
1350
1351 #ifdef USE_CURSOR
1352     server = pointer->server;
1353     if (server->output && wl_list_empty(&server->pointers))
1354     {
1355         server->output->cursor_enabled = false;
1356         renderer_cursor_destroy(&server->output->renderer);
1357
1358         if (server->output->cursor_hwc_window)
1359         {
1360             ds_tdm_output_hwc_window_destroy(server->output->cursor_hwc_window);
1361             server->output->cursor_hwc_window = NULL;
1362         }
1363         draw_server_with_damage(server);
1364     }
1365 #endif
1366
1367     free(pointer);
1368 }
1369
1370 static void
1371 pointer_handle_motion(struct wl_listener *listener, void *data)
1372 {
1373     struct tinyds_pointer *pointer;
1374     struct ds_pointer_event_motion *event = data;
1375     struct tinyds_view *view;
1376     struct tinyds_server *server;
1377     int ow = 0, oh = 0;
1378     double sx, sy;
1379
1380     pointer = wl_container_of(listener, pointer, motion);
1381
1382     server = pointer->server;
1383     if (server->output) {
1384         ow = server->output->width;
1385         oh = server->output->height;
1386     }
1387
1388     if (server->output_x + event->delta_x >= ow)
1389         server->output_x = ow;
1390     else if(server->output_x + event->delta_x <= 0.f)
1391         server->output_x = 0.f;
1392     else
1393         server->output_x = server->output_x + event->delta_x ;
1394     if (server->output_y + event->delta_y >= oh)
1395         server->output_y = oh;
1396     else if(server->output_y + event->delta_y <= 0.f)
1397         server->output_y = 0.f;
1398     else
1399         server->output_y = server->output_y + event->delta_y ;
1400
1401     ds_inf("Pointer(%p) motion: (delta_x %.1f delta_y %.1f) output_x %.1f output_y %.1f",
1402             pointer, event->delta_x, event->delta_y, server->output_x, server->output_y);
1403
1404     view = tinyds_server_view_at(pointer->server, server->output_x, server->output_y, &sx, &sy);
1405
1406     if (pointer->focused_view != view) {
1407         if (pointer->focused_view) {
1408             ds_inf("Clear pointer focus from view(%p)", pointer->focused_view);
1409             ds_seat_pointer_notify_clear_focus(pointer->server->seat);
1410             pointer->focused_view = NULL;
1411         }
1412
1413         if (view) {
1414             ds_inf("Set pointer focus to view(%p)", view);
1415             ds_seat_pointer_notify_enter(pointer->server->seat,
1416                     ds_xdg_surface_get_surface(view->xdg_surface), sx, sy);
1417             pointer->focused_view = view;
1418         }
1419     }
1420
1421     if (view) {
1422         ds_seat_pointer_notify_motion(pointer->server->seat,
1423                 event->time_msec, sx, sy);
1424     }
1425
1426 #ifdef USE_CURSOR
1427     if (server->output && server->output->cursor_enabled) {
1428         renderer_cursor_update(&server->output->renderer, server->output_x, server->output_y);
1429         draw_server_with_damage(server);
1430     }
1431 #endif
1432 }
1433
1434 static void
1435 pointer_handle_button(struct wl_listener *listener, void *data)
1436 {
1437     struct tinyds_pointer *pointer;
1438     struct ds_pointer_event_button *event = data;
1439
1440     pointer = wl_container_of(listener, pointer, button);
1441
1442     ds_inf("Pointer(%p) button(%d): state(%s) time(%d)",
1443             pointer, event->button,
1444             (event->state == DS_BUTTON_PRESSED) ? "Pressed" : "Released",
1445             event->time_msec);
1446
1447     ds_seat_pointer_notify_button(pointer->server->seat, event->time_msec, event->button, event->state);
1448 }
1449
1450 static void
1451 pointer_handle_frame(struct wl_listener *listener, void *data)
1452 {
1453     struct tinyds_pointer *pointer;
1454
1455     pointer = wl_container_of(listener, pointer, frame);
1456
1457     ds_inf("Pointer(%p) frame", pointer);
1458     ds_seat_pointer_notify_frame(pointer->server->seat);
1459 }
1460
1461 static void
1462 server_add_pointer(struct tinyds_server *server, struct ds_input_device *dev)
1463 {
1464     struct tinyds_pointer *pointer;
1465
1466     pointer = calloc(1, sizeof *pointer);
1467     assert(pointer);
1468
1469     pointer->dev = dev;
1470     pointer->server = server;
1471
1472     pointer->destroy.notify = pointer_handle_device_destroy;
1473     ds_input_device_add_destroy_listener(dev, &pointer->destroy);
1474
1475     pointer->motion.notify = pointer_handle_motion;
1476     ds_pointer_add_motion_listener(ds_input_device_get_pointer(dev),
1477             &pointer->motion);
1478
1479     pointer->button.notify = pointer_handle_button;
1480     ds_pointer_add_button_listener(ds_input_device_get_pointer(dev),
1481             &pointer->button);
1482
1483     pointer->frame.notify = pointer_handle_frame;
1484     ds_pointer_add_frame_listener(ds_input_device_get_pointer(dev),
1485             &pointer->frame);
1486
1487     pointer->focused_view = NULL;
1488
1489 #ifdef USE_CURSOR
1490     if (server->output && wl_list_empty(&server->pointers)) {
1491         server->output_x = (double)(server->output->width) / 2;
1492         server->output_y = (double)(server->output->height) / 2;
1493
1494         server->output->cursor_enabled = true;
1495         renderer_cursor_create(&server->output->renderer, 255, 0, 0, CURSOR_W, CURSOR_H);
1496         renderer_cursor_update(&server->output->renderer, server->output_x, server->output_y);
1497
1498         server->output->cursor_hwc_window = ds_tdm_output_hwc_window_create(server->output->hwc);
1499         assert(server->output->cursor_hwc_window);
1500         draw_server_with_damage(server);
1501     }
1502 #endif
1503
1504     wl_list_insert(&server->pointers, &pointer->link);
1505
1506     ds_inf("Pointer(%p) added", pointer);
1507 }
1508
1509 static void
1510 output_schedule_commit_handle_idle_timer(void *data)
1511 {
1512     struct tinyds_output *output = data;
1513     output->idle_commit = NULL;
1514
1515     output_commit(output);
1516 }
1517
1518 static void
1519 output_schedule_commit(struct tinyds_output *output)
1520 {
1521     if (output->idle_commit)
1522         return;
1523
1524     struct wl_event_loop *ev = wl_display_get_event_loop(output->server->display);
1525     output->idle_commit =
1526         wl_event_loop_add_idle(ev, output_schedule_commit_handle_idle_timer, output);
1527 }
1528
1529 static void
1530 text_input_mgr_handle_destroy(struct wl_listener *listener, void *data)
1531 {
1532     struct tinyds_text_input *text_input;
1533     struct tinyds_server *server;
1534
1535     ds_inf("text_input_mgr_handle_destroy");
1536     text_input = wl_container_of(listener, text_input, mgr_destroy);
1537
1538     wl_list_remove(&text_input->mgr_destroy.link);
1539     wl_list_remove(&text_input->new_text_input.link);
1540
1541     server = text_input->server;
1542     server->text_input = NULL;
1543 }
1544
1545 static void
1546 text_input_handle_destroy(struct wl_listener *listener, void *data)
1547 {
1548     struct tinyds_text_input *text_input;
1549
1550     ds_inf("text_input_handle_destroy");
1551
1552     text_input = wl_container_of(listener, text_input, destroy);
1553
1554     wl_list_remove(&text_input->destroy.link);
1555     wl_list_remove(&text_input->text_input_activate.link);
1556     wl_list_remove(&text_input->text_input_deactivate.link);
1557     wl_list_remove(&text_input->text_input_reset.link);
1558     wl_list_remove(&text_input->text_input_set_content_type.link);
1559     wl_list_remove(&text_input->text_input_invoke_action.link);
1560     wl_list_remove(&text_input->text_input_commit_state.link);
1561     wl_list_remove(&text_input->text_input_set_preferred_language.link);
1562
1563     free(text_input);
1564 }
1565
1566 static void
1567 text_input_handle_activate(struct wl_listener *listener, void *data)
1568 {
1569     struct tinyds_text_input *text_input;
1570     struct tinyds_input_method *input_method;
1571     struct ds_tizen_text_input_event_activate *event = data;
1572
1573     text_input = wl_container_of(listener, text_input, text_input_activate);
1574
1575     input_method = text_input->server->input_method;
1576
1577     ds_inf("text_input_handle_activate. text_input(%p) seat(%p) surface(%p) text_input(%p)",
1578         text_input, event->seat, event->surface, event->text_input);
1579
1580     if (input_method->input == text_input)
1581         return;
1582     if (input_method->input)
1583         ;//deactivate_input_method(server->input_method);
1584     input_method->input = text_input;
1585     wl_list_insert(&text_input->input_methods, &input_method->link);
1586
1587     text_input->surface = event->surface;
1588
1589     if (!add_new_input_method_context(input_method, text_input))
1590         return;
1591
1592     // ds_tizen_input_method_send_set_text_input_id();
1593 }
1594
1595 static void
1596 text_input_handle_deactivate(struct wl_listener *listener, void *data)
1597 {
1598     struct tinyds_text_input *text_input;
1599     struct tinyds_input_method *input_method, *tmp;
1600     struct ds_tizen_text_input_event_deactivate *event = data;
1601
1602     text_input = wl_container_of(listener, text_input, text_input_deactivate);
1603     ds_inf("text_input_handle_deactivate. text_input(%p) seat(%p) text_input(%p)",
1604         text_input, event->seat, event->text_input);
1605
1606     wl_list_for_each_safe(input_method, tmp, &text_input->input_methods, link) {
1607         if (!input_method->input_method || !input_method->context->context) continue;
1608         ds_tizen_input_method_send_deactivate(input_method->input_method, input_method->context->context);
1609         input_method->input = NULL;
1610         input_method->context = NULL;
1611         wl_list_remove(&input_method->link);
1612     }
1613
1614     text_input->surface = NULL;
1615     // ds_tizen_input_method_send_close_connection();
1616 }
1617
1618 static void
1619 text_input_handle_reset(struct wl_listener *listener, void *data)
1620 {
1621     struct tinyds_text_input *text_input;
1622     struct tinyds_input_method *input_method;
1623
1624     text_input = wl_container_of(listener, text_input, text_input_reset);
1625
1626     ds_inf("text_input_handle_reset. text_input(%p)", text_input);
1627
1628     wl_list_for_each(input_method, &text_input->input_methods, link) {
1629         if (!input_method->context || !input_method->context->context) continue;
1630         ds_tizen_input_method_context_send_reset(input_method->context->context);
1631     }
1632 }
1633
1634 static void
1635 text_input_handle_set_content_type(struct wl_listener *listener, void *data)
1636 {
1637     struct tinyds_text_input *text_input;
1638     struct ds_tizen_text_input_event_set_content_type *event = data;
1639     struct tinyds_input_method *input_method;
1640
1641     text_input = wl_container_of(listener, text_input, text_input_set_content_type);
1642
1643     ds_inf("text_input_handle_content_type. text_input(%p) hint(%u) purpose(%u)",
1644         text_input, event->hint, event->purpose);
1645
1646     wl_list_for_each(input_method, &text_input->input_methods, link) {
1647         if (!input_method->context || !input_method->context->context) continue;
1648         ds_tizen_input_method_context_send_content_type(input_method->context->context,
1649             event->hint, event->purpose);
1650     }
1651 }
1652
1653 static void
1654 text_input_handle_invoke_action(struct wl_listener *listener, void *data)
1655 {
1656     struct tinyds_text_input *text_input;
1657     struct ds_tizen_text_input_event_invoke_action *event = data;
1658     struct tinyds_input_method *input_method;
1659
1660     text_input = wl_container_of(listener, text_input, text_input_invoke_action);
1661
1662     ds_inf("text_input_handle_invoke_action. text_input(%p) button(%u) index(%u)",
1663         text_input, event->button, event->index);
1664
1665     wl_list_for_each(input_method, &text_input->input_methods, link) {
1666         if (!input_method->context || !input_method->context->context) continue;
1667         ds_tizen_input_method_context_send_invoke_action(input_method->context->context,
1668             event->button, event->index);
1669     }
1670 }
1671
1672 static void
1673 text_input_handle_commit_state(struct wl_listener *listener, void *data)
1674 {
1675     struct tinyds_text_input *text_input;
1676     struct ds_tizen_text_input_event_commit_state *event = data;
1677     struct tinyds_input_method *input_method;
1678
1679     text_input = wl_container_of(listener, text_input, text_input_commit_state);
1680
1681     ds_inf("text_input_handle_commit_state. text_input(%p) serial(%u)",
1682         text_input, event->serial);
1683
1684     wl_list_for_each(input_method, &text_input->input_methods, link) {
1685         if (!input_method->context || !input_method->context->context) continue;
1686         ds_tizen_input_method_context_send_commit_state(input_method->context->context,
1687             event->serial);
1688     }
1689 }
1690
1691 static void
1692 text_input_handle_set_preferred_language(struct wl_listener *listener, void *data)
1693 {
1694     struct tinyds_text_input *text_input;
1695     struct ds_tizen_text_input_event_set_preferred_language *event = data;
1696     struct tinyds_input_method *input_method;
1697
1698     text_input = wl_container_of(listener, text_input, text_input_set_preferred_language);
1699
1700     ds_inf("text_input_handle_set_preferred_language. text_input(%p) language(%s)",
1701         text_input, event->language);
1702
1703     wl_list_for_each(input_method, &text_input->input_methods, link) {
1704         if (!input_method->context || !input_method->context->context) continue;
1705         ds_tizen_input_method_context_send_preferred_language(input_method->context->context,
1706             event->language);
1707     }
1708 }
1709
1710 static void
1711 text_input_mgr_handle_new_text_input(struct wl_listener *listener, void *data)
1712 {
1713     struct tinyds_text_input *text_input;
1714     struct ds_tizen_text_input *input = data;
1715
1716     text_input = wl_container_of(listener, text_input, new_text_input);
1717
1718     ds_inf("text_input_mgr_handle_new_text_input");
1719
1720     text_input->input = input;
1721
1722     text_input->destroy.notify = text_input_handle_destroy;
1723     ds_tizen_text_input_add_destroy_listener(text_input->input,
1724         &text_input->destroy);
1725
1726     text_input->text_input_activate.notify = text_input_handle_activate;
1727     ds_tizen_text_input_add_activate_listener(text_input->input,
1728         &text_input->text_input_activate);
1729
1730     text_input->text_input_deactivate.notify = text_input_handle_deactivate;
1731     ds_tizen_text_input_add_deactivate_listener(text_input->input,
1732         &text_input->text_input_deactivate);
1733
1734     text_input->text_input_reset.notify = text_input_handle_reset;
1735     ds_tizen_text_input_add_reset_listener(text_input->input,
1736         &text_input->text_input_reset);
1737
1738     text_input->text_input_set_content_type.notify = text_input_handle_set_content_type;
1739     ds_tizen_text_input_add_set_content_type_listener(text_input->input,
1740         &text_input->text_input_set_content_type);
1741
1742     text_input->text_input_invoke_action.notify = text_input_handle_invoke_action;
1743     ds_tizen_text_input_add_invoke_action_listener(text_input->input,
1744         &text_input->text_input_invoke_action);
1745
1746     text_input->text_input_commit_state.notify = text_input_handle_commit_state;
1747     ds_tizen_text_input_add_commit_state_listener(text_input->input,
1748         &text_input->text_input_commit_state);
1749
1750     text_input->text_input_set_preferred_language.notify = text_input_handle_set_preferred_language;
1751     ds_tizen_text_input_add_set_preferred_language_listener(text_input->input,
1752         &text_input->text_input_set_preferred_language);
1753 }
1754
1755 static void
1756 input_method_mgr_handle_destroy(struct wl_listener *listener, void *data)
1757 {
1758     struct tinyds_input_method *input_method;
1759
1760     ds_inf("input_method_mgr_handle_destroy");
1761
1762     input_method = wl_container_of(listener, input_method, mgr_destroy);
1763
1764     wl_list_remove(&input_method->mgr_destroy.link);
1765 }
1766
1767 static void
1768 input_method_handle_destroy(struct wl_listener *listener, void *data)
1769 {
1770     struct tinyds_input_method *input_method;
1771     struct tinyds_server *server;
1772
1773     ds_inf("input_method_handle_destroy");
1774
1775     input_method = wl_container_of(listener, input_method, destroy);
1776
1777     wl_list_remove(&input_method->destroy.link);
1778
1779     server = input_method->server;
1780     server->input_method = NULL;
1781
1782     free(input_method);
1783 }
1784
1785 static void
1786 context_handle_destroy(struct wl_listener *listener, void *data)
1787 {
1788     struct tinyds_input_method_context *context;
1789     struct tinyds_server *server;
1790
1791     ds_inf("context_handle_destroy");
1792
1793     context = wl_container_of(listener, context, destroy);
1794
1795     wl_list_remove(&context->destroy.link);
1796
1797     wl_list_remove(&context->im_context_commit_string.link);
1798     wl_list_remove(&context->im_context_preedit_string.link);
1799     wl_list_remove(&context->im_context_preedit_styling.link);
1800     wl_list_remove(&context->im_context_preedit_cursor.link);
1801     wl_list_remove(&context->im_context_delete_surrounding_text.link);
1802     wl_list_remove(&context->im_context_cursor_position.link);
1803     wl_list_remove(&context->im_context_modifiers_map.link);
1804     wl_list_remove(&context->im_context_keysym.link);
1805     wl_list_remove(&context->im_context_grab_keyboard.link);
1806     wl_list_remove(&context->im_context_key.link);
1807     wl_list_remove(&context->im_context_modifiers.link);
1808     wl_list_remove(&context->im_context_language.link);
1809     wl_list_remove(&context->im_context_text_direction.link);
1810
1811     server = context->server;
1812     server->input_method->context = NULL;
1813
1814     free(context);
1815 }
1816
1817 static void
1818 context_handle_commit_string(struct wl_listener *listener, void *data)
1819 {
1820     struct tinyds_text_input *text_input;
1821     struct tinyds_input_method_context *context;
1822     struct ds_tizen_input_method_context_event_commit_string *event = data;
1823
1824     context = wl_container_of(listener, context, im_context_commit_string);
1825     text_input = context->server->text_input;
1826
1827     ds_inf("context_handle_commit_string. text_input(%p) serial(%u) text(%s)",
1828         text_input, event->serial, event->text);
1829
1830     ds_tizen_text_input_send_commit_string(text_input->input, event->serial, event->text);
1831 }
1832
1833 static void
1834 context_handle_preedit_string(struct wl_listener *listener, void *data)
1835 {
1836     struct tinyds_input_method_context *context;
1837     struct tinyds_text_input *text_input;
1838     struct ds_tizen_input_method_context_event_preedit_string *event = data;
1839
1840     context = wl_container_of(listener, context, im_context_preedit_string);
1841     text_input = context->server->text_input;
1842
1843     ds_inf("context_handle_preedit_string. text_input(%p) serial(%u) text(%s) commit(%s)",
1844         text_input, event->serial, event->text, event->commit);
1845
1846     ds_tizen_text_input_send_preedit_string(text_input->input, event->serial, event->text, event->commit);
1847 }
1848
1849 static void
1850 context_handle_preedit_styling(struct wl_listener *listener, void *data)
1851 {
1852     struct tinyds_input_method_context *context;
1853     struct tinyds_text_input *text_input;
1854     struct ds_tizen_input_method_context_event_preedit_styling *event = data;
1855
1856     context = wl_container_of(listener, context, im_context_preedit_styling);
1857     text_input = context->server->text_input;
1858
1859     ds_inf("context_handle_preedit_styling. text_input(%p) index(%u) length(%u) style(%u)",
1860         text_input, event->index, event->length, event->style);
1861
1862     ds_tizen_text_input_send_preedit_styling(text_input->input, event->index, event->length, event->style);
1863 }
1864
1865 static void
1866 context_handle_preedit_cursor(struct wl_listener *listener, void *data)
1867 {
1868     struct tinyds_input_method_context *context;
1869     struct tinyds_text_input *text_input;
1870     struct ds_tizen_input_method_context_event_preedit_cursor *event = data;
1871
1872     context = wl_container_of(listener, context, im_context_preedit_cursor);
1873     text_input = context->server->text_input;
1874
1875     ds_inf("context_handle_preedit_cursor. text_input(%p) index(%u)",
1876         text_input, event->index);
1877
1878     ds_tizen_text_input_send_preedit_cursor(text_input->input, event->index);
1879 }
1880
1881 static void
1882 context_handle_delete_surrounding_text(struct wl_listener *listener, void *data)
1883 {
1884     struct tinyds_input_method_context *context;
1885     struct tinyds_text_input *text_input;
1886     struct ds_tizen_input_method_context_event_delete_surrounding_text *event = data;
1887
1888     context = wl_container_of(listener, context, im_context_delete_surrounding_text);
1889     text_input = context->server->text_input;
1890
1891     ds_inf("context_handle_delete_surrounding_text. text_input(%p) index(%d) length(%u)",
1892         text_input, event->index, event->length);
1893
1894     ds_tizen_text_input_send_delete_surrounding_text(text_input->input, event->index, event->length);
1895 }
1896
1897 static void
1898 context_handle_cursor_position(struct wl_listener *listener, void *data)
1899 {
1900     struct tinyds_input_method_context *context;
1901     struct tinyds_text_input *text_input;
1902     struct ds_tizen_input_method_context_event_cursor_position *event = data;
1903
1904     context = wl_container_of(listener, context, im_context_cursor_position);
1905     text_input = context->server->text_input;
1906
1907     ds_inf("context_handle_cursor_position. text_input(%p) index(%d) length(%d)",
1908         text_input, event->index, event->anchor);
1909
1910     ds_tizen_text_input_send_cursor_position(text_input->input, event->index, event->anchor);
1911 }
1912
1913 static void
1914 context_handle_modifiers_map(struct wl_listener *listener, void *data)
1915 {
1916     struct tinyds_input_method_context *context;
1917     struct tinyds_text_input *text_input;
1918     struct ds_tizen_input_method_context_event_modifiers_map *event = data;
1919
1920     context = wl_container_of(listener, context, im_context_modifiers_map);
1921     text_input = context->server->text_input;
1922
1923     ds_inf("context_handle_modifiers_map. text_input(%p) map(%p)",
1924         text_input, event->map);
1925
1926     ds_tizen_text_input_send_modifiers_map(text_input->input, event->map);
1927 }
1928
1929 static void
1930 context_handle_keysym(struct wl_listener *listener, void *data)
1931 {
1932     struct tinyds_input_method_context *context;
1933     struct tinyds_text_input *text_input;
1934     struct ds_tizen_input_method_context_event_keysym *event = data;
1935
1936     context = wl_container_of(listener, context, im_context_keysym);
1937     text_input = context->server->text_input;
1938
1939     ds_inf("context_handle_keysym. text_input(%p) serial(%u) time(%u) sysm(%u) state(%u) modifiers(%u)",
1940         text_input, event->serial, event->time, event->sym, event->state, event->modifiers);
1941
1942     ds_tizen_text_input_send_keysym(text_input->input, event->serial, event->time, event->sym, event->state, event->modifiers);
1943 }
1944
1945 static void
1946 context_handle_grab_keyboard(struct wl_listener *listener, void *data)
1947 {
1948     struct tinyds_input_method_context *context;
1949     struct tinyds_text_input *text_input;
1950
1951     context = wl_container_of(listener, context, im_context_grab_keyboard);
1952     text_input = context->server->text_input;
1953
1954     ds_inf("context_handle_grab_keyboard. text_input(%p)",
1955         text_input);
1956
1957     //TODO
1958 }
1959
1960 static void
1961 context_handle_key(struct wl_listener *listener, void *data)
1962 {
1963     struct tinyds_input_method_context *context;
1964     struct tinyds_text_input *text_input;
1965
1966     context = wl_container_of(listener, context, im_context_key);
1967     text_input = context->server->text_input;
1968
1969     ds_inf("context_handle_key. text_input(%p)",
1970         text_input);
1971
1972    //TODO
1973 }
1974
1975 static void
1976 context_handle_modifiers(struct wl_listener *listener, void *data)
1977 {
1978     struct tinyds_input_method_context *context;
1979     struct tinyds_text_input *text_input;
1980
1981     context = wl_container_of(listener, context, im_context_modifiers);
1982     text_input = context->server->text_input;
1983
1984     ds_inf("context_handle_modifiers. text_input(%p)",
1985         text_input);
1986
1987    //TODO
1988 }
1989
1990 static void
1991 context_handle_language(struct wl_listener *listener, void *data)
1992 {
1993     struct tinyds_input_method_context *context;
1994     struct tinyds_text_input *text_input;
1995     struct ds_tizen_input_method_context_event_language *event = data;
1996
1997     context = wl_container_of(listener, context, im_context_language);
1998     text_input = context->server->text_input;
1999
2000     ds_inf("context_handle_language. text_input(%p) serial(%u), language(%s)",
2001         text_input, event->serial, event->language);
2002
2003     ds_tizen_text_input_send_language(text_input->input, event->serial, event->language);
2004 }
2005
2006 static void
2007 context_handle_text_direction(struct wl_listener *listener, void *data)
2008 {
2009     struct tinyds_input_method_context *context;
2010     struct tinyds_text_input *text_input;
2011     struct ds_tizen_input_method_context_event_text_direction *event = data;
2012
2013     context = wl_container_of(listener, context, im_context_text_direction);
2014     text_input = context->server->text_input;
2015
2016     ds_inf("context_handle_text_direction. text_input(%p) serial(%u), direction(%u)",
2017         text_input, event->serial, event->direction);
2018
2019     ds_tizen_text_input_send_text_direction(text_input->input, event->serial, event->direction);
2020 }
2021
2022 static bool
2023 add_new_text_input(struct tinyds_server *server)
2024 {
2025     struct tinyds_text_input *text_input;
2026
2027     text_input = calloc(1, sizeof *text_input);
2028     if (!text_input)
2029         return false;
2030
2031     text_input->text_input_mgr = ds_tizen_text_input_manager_create(server->display);
2032     if (!text_input->text_input_mgr) {
2033         free(text_input);
2034         ds_err("Could not create ds_tizen_text_input_manager");
2035         return false;
2036     }
2037
2038     wl_list_init(&text_input->input_methods);
2039
2040     text_input->mgr_destroy.notify = text_input_mgr_handle_destroy;
2041     ds_tizen_text_input_manager_add_destroy_listener(text_input->text_input_mgr,
2042             &text_input->mgr_destroy);
2043
2044     text_input->new_text_input.notify = text_input_mgr_handle_new_text_input;
2045     ds_tizen_text_input_manager_add_new_text_input_listener(text_input->text_input_mgr,
2046             &text_input->new_text_input);
2047
2048     text_input->server = server;
2049     server->text_input = text_input;
2050
2051     ds_inf("Text_Input (%p) added", text_input);
2052
2053     return true;
2054 }
2055
2056 static bool
2057 add_new_input_method(struct tinyds_server *server)
2058 {
2059     struct tinyds_input_method *input_method;
2060
2061     input_method = calloc(1, sizeof *input_method);
2062     if (!input_method)
2063         return false;
2064
2065     input_method->input_method = ds_tizen_input_method_create(server->display);
2066     if (!input_method->input_method) {
2067         free(input_method);
2068         ds_err("Could not create ds_tizen_input_method");
2069         return false;
2070     }
2071     input_method->destroy.notify = input_method_handle_destroy;
2072     ds_tizen_input_method_add_destroy_listener(input_method->input_method,
2073             &input_method->destroy);
2074
2075     input_method->input_method_mgr = ds_tizen_input_method_manager_create(server->display);
2076     if (!input_method->input_method_mgr) {
2077         free(input_method);
2078         ds_err("Could not create ds_tizen_input_method_manager");
2079         return false;
2080     }
2081
2082     input_method->mgr_destroy.notify = input_method_mgr_handle_destroy;
2083     ds_tizen_input_method_manager_add_destroy_listener(input_method->input_method_mgr,
2084             &input_method->mgr_destroy);
2085
2086     input_method->server = server;
2087     server->input_method = input_method;
2088
2089     ds_inf("Input_Method (%p) added", input_method);
2090
2091     return true;
2092 }
2093
2094 static bool
2095 add_new_input_method_context(struct tinyds_input_method *input_method,
2096         struct tinyds_text_input *text_input)
2097 {
2098     struct tinyds_input_method_context *context;
2099
2100     context = calloc(1, sizeof *context);
2101     if (context == NULL)
2102     {
2103         ds_err("calloc is failed. tinyds_input_method_context");
2104         return false;
2105     }
2106     input_method->context = context;
2107     context->input_method = input_method;
2108     context->server = input_method->server;
2109     context->input = text_input;
2110
2111     context->context = ds_tizen_input_method_create_context(input_method->input_method);
2112     if (context->context == NULL) {
2113         ds_err("ds_tizen_input_method_create_context() failed.");
2114         return false;
2115     }
2116
2117     context->destroy.notify = context_handle_destroy;
2118     ds_tizen_input_method_context_add_destroy_listener(context->context,
2119         &context->destroy);
2120
2121     context->im_context_commit_string.notify = context_handle_commit_string;
2122     ds_tizen_input_method_context_add_commit_string_listener(context->context,
2123         &context->im_context_commit_string);
2124
2125     context->im_context_preedit_string.notify = context_handle_preedit_string;
2126     ds_tizen_input_method_context_add_preedit_string_listener(context->context,
2127         &context->im_context_preedit_string);
2128
2129     context->im_context_preedit_styling.notify = context_handle_preedit_styling;
2130     ds_tizen_input_method_context_add_preedit_styling_listener(context->context,
2131         &context->im_context_preedit_styling);
2132
2133     context->im_context_preedit_cursor.notify = context_handle_preedit_cursor;
2134     ds_tizen_input_method_context_add_preedit_cursor_listener(context->context,
2135         &context->im_context_preedit_cursor);
2136
2137     context->im_context_delete_surrounding_text.notify = context_handle_delete_surrounding_text;
2138     ds_tizen_input_method_context_add_delete_surrounding_text_listener(context->context,
2139         &context->im_context_delete_surrounding_text);
2140
2141     context->im_context_cursor_position.notify = context_handle_cursor_position;
2142     ds_tizen_input_method_context_add_cursor_position_listener(context->context,
2143         &context->im_context_cursor_position);
2144
2145     context->im_context_modifiers_map.notify = context_handle_modifiers_map;
2146     ds_tizen_input_method_context_add_modifiers_map_listener(context->context,
2147         &context->im_context_modifiers_map);
2148
2149     context->im_context_keysym.notify = context_handle_keysym;
2150     ds_tizen_input_method_context_add_keysym_listener(context->context,
2151         &context->im_context_keysym);
2152
2153     context->im_context_grab_keyboard.notify = context_handle_grab_keyboard;
2154     ds_tizen_input_method_context_add_grab_keyboard_listener(context->context,
2155         &context->im_context_grab_keyboard);
2156
2157     context->im_context_key.notify = context_handle_key;
2158     ds_tizen_input_method_context_add_key_listener(context->context,
2159         &context->im_context_key);
2160
2161     context->im_context_modifiers.notify = context_handle_modifiers;
2162     ds_tizen_input_method_context_add_modifiers_listener(context->context,
2163         &context->im_context_modifiers);
2164
2165     context->im_context_language.notify = context_handle_language;
2166     ds_tizen_input_method_context_add_language_listener(context->context,
2167         &context->im_context_language);
2168
2169     context->im_context_text_direction.notify = context_handle_text_direction;
2170     ds_tizen_input_method_context_add_text_direction_listener(context->context,
2171         &context->im_context_text_direction);
2172
2173     return true;
2174 }
2175
2176 struct tinyds_view *
2177 tinyds_server_get_focused_view(struct tinyds_server *server)
2178 {
2179     struct tinyds_view *view = NULL;
2180     struct tinyds_pointer *pointer;
2181
2182     wl_list_for_each(pointer, &server->pointers, link){
2183         if (!pointer->focused_view) continue;
2184
2185         view = pointer->focused_view;
2186     }
2187
2188     return view;
2189 }
2190
2191 void
2192 tinyds_server_get_output_size(struct tinyds_server *server,
2193     int *output_w, int *output_h)
2194 {
2195     *output_w = server->output->width;
2196     *output_h = server->output->height;
2197 }