text_input: add dummy implementation of tizen only handlers
[platform/core/uifw/libds-tizen.git] / src / text_input / text_input.c
1 #include <stdlib.h>
2 #include <wayland-server.h>
3 #include <text-server-protocol.h>
4 #include <libds/log.h>
5
6 #include "util.h"
7 #include <libds-tizen/text_input.h>
8
9 #define TEXT_INPUT_VERSION 1
10
11 struct ds_tizen_text_input {
12     struct wl_resource *resource;
13
14     struct wl_list link;
15
16     struct {
17         struct wl_signal destroy;
18         struct wl_signal activate;
19         struct wl_signal deactivate;
20         struct wl_signal reset;
21         struct wl_signal set_content_type;
22         struct wl_signal invoke_action;
23         struct wl_signal commit_state;
24         struct wl_signal set_preferred_language;
25     } events;
26 };
27
28 struct ds_tizen_text_input_manager {
29     struct wl_global *global;
30     struct wl_list text_inputs;
31
32     struct wl_listener destroy;
33
34     struct {
35         struct wl_signal new_text_input;
36         struct wl_signal destroy;
37     } events;
38 };
39
40 static const struct wl_text_input_interface text_input_impl;
41 static const struct wl_text_input_manager_interface text_input_mgr_impl;
42
43 WL_EXPORT void
44 ds_tizen_text_input_manager_add_destroy_listener(
45     struct ds_tizen_text_input_manager *ti_mgr, struct wl_listener *listener)
46 {
47     wl_signal_add(&ti_mgr->events.destroy, listener);
48 }
49
50 WL_EXPORT void
51 ds_tizen_text_input_manager_add_new_text_input_listener(
52     struct ds_tizen_text_input_manager *ti_mgr, struct wl_listener *listener)
53 {
54     wl_signal_add(&ti_mgr->events.new_text_input, listener);
55 }
56
57 WL_EXPORT void
58 ds_tizen_text_input_add_destroy_listener(
59     struct ds_tizen_text_input *ti, struct wl_listener *listener)
60 {
61     wl_signal_add(&ti->events.destroy, listener);
62 }
63
64 WL_EXPORT void
65 ds_tizen_text_input_add_activate_listener(
66     struct ds_tizen_text_input *ti, struct wl_listener *listener)
67 {
68     wl_signal_add(&ti->events.activate, listener);
69 }
70
71 WL_EXPORT void
72 ds_tizen_text_input_add_deactivate_listener(
73     struct ds_tizen_text_input *ti, struct wl_listener *listener)
74 {
75     wl_signal_add(&ti->events.deactivate, listener);
76 }
77
78 WL_EXPORT void
79 ds_tizen_text_input_add_reset_listener(
80     struct ds_tizen_text_input *ti, struct wl_listener *listener)
81 {
82     wl_signal_add(&ti->events.reset, listener);
83 }
84
85 WL_EXPORT void
86 ds_tizen_text_input_add_set_content_type_listener(
87     struct ds_tizen_text_input *ti, struct wl_listener *listener)
88 {
89     wl_signal_add(&ti->events.set_content_type, listener);
90 }
91
92 WL_EXPORT void
93 ds_tizen_text_input_add_set_preferred_language_listener(
94     struct ds_tizen_text_input *ti, struct wl_listener *listener)
95 {
96     wl_signal_add(&ti->events.set_preferred_language, listener);
97 }
98
99 WL_EXPORT void
100 ds_tizen_text_input_add_commit_state_listener(
101     struct ds_tizen_text_input *ti, struct wl_listener *listener)
102 {
103     wl_signal_add(&ti->events.commit_state, listener);
104 }
105
106 WL_EXPORT void
107 ds_tizen_text_input_add_invoke_action_listener(
108     struct ds_tizen_text_input *ti, struct wl_listener *listener)
109 {
110     wl_signal_add(&ti->events.invoke_action, listener);
111 }
112
113 WL_EXPORT void
114 ds_tizen_text_input_send_enter(struct ds_tizen_text_input *ti,
115     struct ds_surface *surface)
116 {
117     struct wl_resource *resource;
118
119     ds_inf("ds_tizen_text_input_send_enter");
120     resource = ds_surface_get_wl_resource(surface);
121     wl_text_input_send_enter(ti->resource, resource);
122 }
123
124 WL_EXPORT void
125 ds_tizen_text_input_send_leave(struct ds_tizen_text_input *ti)
126 {
127     ds_inf("ds_tizen_text_input_send_leave");
128     wl_text_input_send_leave(ti->resource);
129 }
130
131 WL_EXPORT void
132 ds_tizen_text_input_send_modifiers_map(struct ds_tizen_text_input *ti,
133     struct wl_array *map)
134 {
135     ds_inf("ds_tizen_text_input_send_modifiers_map");
136     wl_text_input_send_modifiers_map(ti->resource, map);
137 }
138
139 WL_EXPORT void
140 ds_tizen_text_input_send_input_panel_state(struct ds_tizen_text_input *ti,
141     uint32_t state)
142 {
143     ds_inf("ds_tizen_text_input_send_input_panel_state");
144     wl_text_input_send_input_panel_state(ti->resource, state);
145 }
146
147 WL_EXPORT void
148 ds_tizen_text_input_send_preedit_string(struct ds_tizen_text_input *ti,
149     uint32_t serial, const char *text, const char *commit)
150 {
151     ds_inf("ds_tizen_text_input_send_preedit_string");
152     wl_text_input_send_preedit_string(ti->resource, serial, text, commit);
153 }
154
155 WL_EXPORT void
156 ds_tizen_text_input_send_preedit_styling(struct ds_tizen_text_input *ti,
157     uint32_t index, uint32_t length, uint32_t style)
158 {
159     ds_inf("ds_tizen_text_input_send_preedit_styling");
160     wl_text_input_send_preedit_styling(ti->resource, index, length, style);
161 }
162
163 WL_EXPORT void
164 ds_tizen_text_input_send_preedit_cursor(struct ds_tizen_text_input *ti,
165     uint32_t index)
166 {
167     ds_inf("ds_tizen_text_input_send_preedit_cursor");
168     wl_text_input_send_preedit_cursor(ti->resource, index);
169 }
170
171 WL_EXPORT void
172 ds_tizen_text_input_send_commit_string(struct ds_tizen_text_input *ti,
173     uint32_t serial, const char *text)
174 {
175     ds_inf("ds_tizen_text_input_send_commit_string");
176     wl_text_input_send_commit_string(ti->resource, serial, text);
177 }
178
179 WL_EXPORT void
180 ds_tizen_text_input_send_cursor_position(struct ds_tizen_text_input *ti,
181     int32_t index, int32_t anchor)
182 {
183     ds_inf("ds_tizen_text_input_send_cursor_position");
184     wl_text_input_send_cursor_position(ti->resource, index, anchor);
185 }
186
187 WL_EXPORT void
188 ds_tizen_text_input_send_delete_surrounding_text(struct ds_tizen_text_input *ti,
189     int32_t index, uint32_t length)
190 {
191     ds_inf("ds_tizen_text_input_send_delete_surrounding_text");
192     wl_text_input_send_delete_surrounding_text(ti->resource, index, length);
193 }
194
195 WL_EXPORT void
196 ds_tizen_text_input_send_keysym(struct ds_tizen_text_input *ti,
197     uint32_t serial, uint32_t time, uint32_t sym, uint32_t state,
198     uint32_t modifiers)
199 {
200     ds_inf("ds_tizen_text_input_send_keysym");
201     wl_text_input_send_keysym(ti->resource, serial, time, sym, state, modifiers);
202 }
203
204 WL_EXPORT void
205 ds_tizen_text_input_send_language(struct ds_tizen_text_input *ti,
206     uint32_t serial, const char *language)
207 {
208     ds_inf("ds_tizen_text_input_send_language");
209     wl_text_input_send_language(ti->resource, serial, language);
210 }
211
212 WL_EXPORT void
213 ds_tizen_text_input_send_text_direction(struct ds_tizen_text_input *ti,
214     uint32_t serial, uint32_t direction)
215 {
216     ds_inf("ds_tizen_text_input_send_text_direction");
217     wl_text_input_send_text_direction(ti->resource, serial, direction);
218 }
219
220 static void
221 text_input_handle_destroy(struct wl_client *client, struct wl_resource *resource)
222 {
223     ds_inf("text_input_handle_destroy");
224     wl_resource_destroy(resource);
225 }
226
227 static void
228 text_input_handle_activate(struct wl_client *client,
229     struct wl_resource *resource, struct wl_resource *seat, struct wl_resource *surface)
230 {
231     struct ds_tizen_text_input *text_input = wl_resource_get_user_data(resource);
232     struct ds_tizen_text_input_event_activate ds_event;
233     struct ds_surface *ds_surf;
234     struct ds_seat_client *seat_client;
235
236     ds_inf("text_input_handle_activate");
237     ds_surf = ds_surface_from_resource(surface);
238
239     ds_event.text_input = text_input;
240     seat_client = ds_seat_client_from_resource(seat);
241     if (seat_client)
242         ds_event.seat = ds_seat_client_get_seat(seat_client);
243     ds_event.surface = ds_surf;
244     wl_signal_emit(&text_input->events.activate, &ds_event);
245
246     ds_tizen_text_input_send_enter(text_input, ds_surf);
247 }
248
249 static void
250 text_input_handle_deactivate(struct wl_client *client,
251     struct wl_resource *resource, struct wl_resource *seat)
252 {
253     struct ds_tizen_text_input *text_input = wl_resource_get_user_data(resource);
254     struct ds_tizen_text_input_event_deactivate ds_event;
255     struct ds_seat_client *seat_client;
256
257     ds_inf("text_input_handle_deactivate");
258     ds_event.text_input = text_input;
259     seat_client = ds_seat_client_from_resource(seat);
260     if (seat_client)
261         ds_event.seat = ds_seat_client_get_seat(seat_client);
262     wl_signal_emit(&text_input->events.deactivate, &ds_event);
263
264     ds_tizen_text_input_send_leave(text_input);
265 }
266
267 static void
268 text_input_handle_show_input_panel(struct wl_client *client,
269     struct wl_resource *resource)
270 {
271     ds_inf("text_input_handle_show_input_panel");
272 }
273
274 static void
275 text_input_handle_hide_input_panel(struct wl_client *client,
276     struct wl_resource *resource)
277 {
278     ds_inf("text_input_handle_hide_input_panel");
279 }
280
281 static void
282 text_input_handle_reset(struct wl_client *client, struct wl_resource *resource)
283 {
284     struct ds_tizen_text_input *text_input = wl_resource_get_user_data(resource);
285
286     ds_inf("text_input_handle_reset");
287     wl_signal_emit(&text_input->events.reset, text_input);
288 }
289
290 static void
291 text_input_handle_set_content_type(struct wl_client *client,
292     struct wl_resource *resource, uint32_t hint, uint32_t purpose)
293 {
294     struct ds_tizen_text_input *text_input = wl_resource_get_user_data(resource);
295     struct ds_tizen_text_input_event_set_content_type ds_event;
296
297     ds_inf("text_input_handle_set_content_type");
298     ds_event.hint = hint;
299     ds_event.purpose = purpose;
300     ds_event.text_input = text_input;
301
302     wl_signal_emit(&text_input->events.set_content_type, &ds_event);
303 }
304
305 static void
306 text_input_handle_set_cursor_rectangle(struct wl_client *client,
307     struct wl_resource *resource, int32_t x, int32_t y,
308     int32_t width, int32_t height)
309 {
310     ds_inf("text_input_handle_set_cursor_rectangle");
311 }
312
313 static void
314 text_input_handle_set_preferred_language(struct wl_client *client,
315     struct wl_resource *resource, const char *language)
316 {
317     struct ds_tizen_text_input *text_input = wl_resource_get_user_data(resource);
318     struct ds_tizen_text_input_event_set_preferred_language ds_event;
319
320     ds_inf("text_input_handle_set_preferred_language");
321     ds_event.language = language;
322     ds_event.text_input = text_input;
323
324     wl_signal_emit(&text_input->events.set_preferred_language, &ds_event);
325 }
326
327 static void
328 text_input_handle_commit_state(struct wl_client *client,
329     struct wl_resource *resource, uint32_t serial)
330 {
331     struct ds_tizen_text_input *text_input = wl_resource_get_user_data(resource);
332     struct ds_tizen_text_input_event_commit_state ds_event;
333
334     ds_inf("text_input_handle_invoke_action");
335     ds_event.serial = serial;
336     ds_event.text_input = text_input;
337
338     wl_signal_emit(&text_input->events.commit_state, &ds_event);
339 }
340
341 static void
342 text_input_handle_invoke_action(struct wl_client *client,
343     struct wl_resource *resource, uint32_t button, uint32_t index)
344 {
345     struct ds_tizen_text_input *text_input = wl_resource_get_user_data(resource);
346     struct ds_tizen_text_input_event_invoke_action ds_event;
347
348     ds_inf("text_input_handle_invoke_action");
349
350     ds_event.button = button;
351     ds_event.index = index;
352     ds_event.text_input = text_input;
353
354     wl_signal_emit(&text_input->events.invoke_action, &ds_event);
355 }
356
357 static void
358 text_input_handle_set_return_key_type(struct wl_client *client,
359     struct wl_resource *resource, uint32_t return_key_type)
360 {
361     // TODO:
362     ds_inf("Not Implemented. text_input_handle_set_return_key_type");
363 }
364
365 static void
366 text_input_handle_set_return_key_disabled(struct wl_client *client,
367     struct wl_resource *resource, uint32_t return_key_disabled)
368 {
369     // TODO:
370     ds_inf("Not Implemented. text_input_handle_set_return_key_disabled");
371 }
372
373 static void
374 text_input_handle_set_input_panel_data(struct wl_client *client,
375     struct wl_resource *resource, const char *input_panel_data,
376     uint32_t input_panel_length)
377 {
378     // TODO:
379     ds_inf("Not Implemented. text_input_handle_set_input_panel_data");
380 }
381
382 static void
383 text_input_handle_bidi_direction(struct wl_client *client,
384     struct wl_resource *resource, uint32_t direction)
385 {
386     // TODO:
387     ds_inf("Not Implemented. text_input_handle_bidi_direction");
388 }
389
390 static void
391 text_input_handle_set_cursor_position(struct wl_client *client,
392     struct wl_resource *resource, uint32_t cursor_position)
393 {
394     // TODO:
395     ds_inf("Not Implemented. text_input_handle_set_cursor_position");
396 }
397
398 static void
399 text_input_handle_process_input_device_event(struct wl_client *client,
400     struct wl_resource *resource, uint32_t event_type, const char *event_data,
401     uint32_t event_length)
402 {
403     // TODO:
404     ds_inf("Not Implemented. text_input_handle_process_input_device_event");
405 }
406
407 static void
408 text_input_handle_filter_key_event(struct wl_client *client,
409     struct wl_resource *resource, uint32_t serial, uint32_t time,
410     const char *keyname, uint32_t modifiers, const char *dev_name,
411     uint32_t dev_class, uint32_t dev_subclass, uint32_t keycode)
412 {
413     // TODO:
414     ds_inf("Not Implemented. text_input_handle_filter_key_event");
415 }
416
417 static void
418 text_input_handle_get_hide_permission(struct wl_client *client,
419     struct wl_resource *resource)
420 {
421     // TODO:
422     ds_inf("Not Implemented. text_input_handle_get_hide_permission");
423 }
424
425 static void
426 text_input_handle_set_capital_mode(struct wl_client *client,
427     struct wl_resource *resource, uint32_t mode)
428 {
429     // TODO:
430     ds_inf("Not Implemented. text_input_handle_set_capital_mode");
431 }
432
433 static void
434 text_input_handle_prediction_hint(struct wl_client *client,
435     struct wl_resource *resource, const char *text)
436 {
437     // TODO:
438     ds_inf("Not Implemented. text_input_handle_prediction_hint");
439 }
440
441 static void
442 text_input_handle_set_mime_type(struct wl_client *client,
443     struct wl_resource *resource, const char *type)
444 {
445     // TODO:
446     ds_inf("Not Implemented. text_input_handle_set_mime_type");
447 }
448
449 static void
450 text_input_handle_set_input_panel_position(struct wl_client *client,
451     struct wl_resource *resource, uint32_t x, uint32_t y)
452 {
453     // TODO:
454     ds_inf("Not Implemented. text_input_handle_set_input_panel_position");
455 }
456
457 static void
458 text_input_handle_finalize_content(struct wl_client *client,
459     struct wl_resource *resource, const char *text, uint32_t cursor_position)
460 {
461     // TODO:
462     ds_inf("Not Implemented. text_input_handle_finalize_content");
463 }
464
465 static void
466 text_input_handle_prediction_hint_data(struct wl_client *client,
467     struct wl_resource *resource, const char *key, const char *value)
468 {
469     // TODO:
470     ds_inf("Not Implemented. text_input_handle_prediction_hint_data");
471 }
472
473 static void
474 text_input_handle_input_panel_enabled(struct wl_client *client,
475     struct wl_resource *resource, uint32_t enabled)
476 {
477     // TODO:
478     ds_inf("Not Implemented. text_input_handle_input_panel_enabled");
479 }
480
481 static const struct wl_text_input_interface text_input_impl = {
482     .destroy = text_input_handle_destroy,
483     .activate = text_input_handle_activate,
484     .deactivate = text_input_handle_deactivate,
485     .show_input_panel = text_input_handle_show_input_panel,
486     .hide_input_panel = text_input_handle_hide_input_panel,
487     .reset = text_input_handle_reset,
488     .set_content_type = text_input_handle_set_content_type,
489     .set_cursor_rectangle = text_input_handle_set_cursor_rectangle,
490     .set_preferred_language = text_input_handle_set_preferred_language,
491     .commit_state = text_input_handle_commit_state,
492     .invoke_action = text_input_handle_invoke_action,
493     //for tizen only
494     .set_return_key_type = text_input_handle_set_return_key_type,
495     .set_return_key_disabled = text_input_handle_set_return_key_disabled,
496     .set_input_panel_data = text_input_handle_set_input_panel_data,
497     .bidi_direction = text_input_handle_bidi_direction,
498     .set_cursor_position = text_input_handle_set_cursor_position,
499     .process_input_device_event = text_input_handle_process_input_device_event,
500     .filter_key_event = text_input_handle_filter_key_event,
501     .get_hide_permission = text_input_handle_get_hide_permission,
502     .set_capital_mode = text_input_handle_set_capital_mode,
503     .prediction_hint = text_input_handle_prediction_hint,
504     .set_mime_type = text_input_handle_set_mime_type,
505     .set_input_panel_position = text_input_handle_set_input_panel_position,
506     .finalize_content = text_input_handle_finalize_content,
507     .prediction_hint_data = text_input_handle_prediction_hint_data,
508     .input_panel_enabled = text_input_handle_input_panel_enabled,
509 };
510
511 static void
512 text_input_destroy(struct ds_tizen_text_input *text_input)
513 {
514     wl_signal_emit(&text_input->events.destroy, text_input);
515     wl_list_remove(&text_input->link);
516
517     free(text_input);
518 }
519
520 static void
521 text_input_mgr_handle_display_destroy(struct wl_listener *listener, void *data)
522 {
523     struct ds_tizen_text_input_manager *text_input_mgr;
524     struct ds_tizen_text_input *text_input, *tmp;
525
526     text_input_mgr = wl_container_of(listener, text_input_mgr, destroy);
527
528     ds_inf("Global destroy: text_input_mgr(%p)", text_input_mgr);
529
530     wl_signal_emit(&text_input_mgr->events.destroy, text_input_mgr);
531     wl_list_remove(&text_input_mgr->destroy.link);
532
533     wl_list_for_each_safe(text_input, tmp, &text_input_mgr->text_inputs, link) {
534         text_input_destroy(text_input);
535     }
536
537     wl_global_destroy(text_input_mgr->global);
538     free(text_input_mgr);
539 }
540
541 static void
542 text_input_client_handle_destroy(struct wl_resource *resource)
543 {
544     struct ds_tizen_text_input *text_input;
545
546     ds_inf("text_input_client_handle_destroy");
547
548     text_input = wl_resource_get_user_data(resource);
549     text_input_destroy(text_input);
550 }
551
552 static void
553 text_input_manager_handle_create_text_input(struct wl_client *wl_client,
554     struct wl_resource *resource, uint32_t id)
555 {
556     struct ds_tizen_text_input *text_input;
557     struct ds_tizen_text_input_manager *text_input_mgr;
558
559     ds_inf("text_input_manager_handle_create_text_input");
560
561     text_input_mgr = wl_resource_get_user_data(resource);
562
563     text_input = calloc(1, sizeof *text_input);
564     if (text_input == NULL) {
565         ds_err("calloc() failed. ds_tizen_text_input");
566         return;
567     }
568
569     text_input->resource = wl_resource_create(wl_client,
570             &wl_text_input_interface,
571             TEXT_INPUT_VERSION, id);
572     if (text_input->resource == NULL) {
573         ds_err("text_input. wl_resource_create() failed.");
574         wl_client_post_no_memory(wl_client);
575         free(text_input);
576         return;
577     }
578     wl_resource_set_implementation(text_input->resource, &text_input_impl,
579         text_input, text_input_client_handle_destroy);
580
581     wl_signal_init(&text_input->events.activate);
582     wl_signal_init(&text_input->events.deactivate);
583     wl_signal_init(&text_input->events.reset);
584     wl_signal_init(&text_input->events.set_content_type);
585     wl_signal_init(&text_input->events.invoke_action);
586     wl_signal_init(&text_input->events.commit_state);
587     wl_signal_init(&text_input->events.set_preferred_language);
588     wl_signal_init(&text_input->events.destroy);
589
590     wl_list_insert(&text_input_mgr->text_inputs, &text_input->link);
591     wl_signal_emit(&text_input_mgr->events.new_text_input, text_input);
592 }
593
594 static const struct wl_text_input_manager_interface text_input_mgr_impl =
595 {
596     .create_text_input = text_input_manager_handle_create_text_input,
597 };
598
599 static void
600 text_input_mgr_bind(struct wl_client *wl_client, void *data,
601     uint32_t version, uint32_t id)
602 {
603     struct ds_tizen_text_input_manager *text_input_mgr = data;
604     struct wl_resource *resource;
605
606     ds_inf("text_input_mgr. client binds. (client:%p)", wl_client);
607
608     resource = wl_resource_create(wl_client,
609             &wl_text_input_manager_interface,
610             version, id);
611     if (resource == NULL) {
612         ds_err("text_input_mgr. wl_resource_create() failed.");
613         wl_client_post_no_memory(wl_client);
614         return;
615     }
616     wl_resource_set_implementation(resource, &text_input_mgr_impl,
617         text_input_mgr, NULL);
618 }
619
620 WL_EXPORT struct ds_tizen_text_input_manager *
621 ds_tizen_text_input_manager_create(struct wl_display *display)
622 {
623     struct ds_tizen_text_input_manager *text_input_mgr;
624
625     text_input_mgr = calloc(1, sizeof *text_input_mgr);
626     if (text_input_mgr == NULL) {
627         ds_err("calloc() failed. ds_tizen_text_input_manager");
628         return NULL;
629     }
630
631     text_input_mgr->global = wl_global_create(display,
632             &wl_text_input_manager_interface, TEXT_INPUT_VERSION, text_input_mgr,
633             text_input_mgr_bind);
634     if (!text_input_mgr->global) {
635         free(text_input_mgr);
636         return NULL;
637     }
638
639     wl_list_init(&text_input_mgr->text_inputs);
640
641     wl_signal_init(&text_input_mgr->events.new_text_input);
642     wl_signal_init(&text_input_mgr->events.destroy);
643
644
645     text_input_mgr->destroy.notify = text_input_mgr_handle_display_destroy;
646     wl_display_add_destroy_listener(display, &text_input_mgr->destroy);
647
648     ds_inf("Global create: wl_text_input_manager. text_input_mgr(%p)", text_input_mgr);
649
650     return text_input_mgr;
651 }