c000b4bcc3c3cff872b7be07983296205ae24425
[platform/core/uifw/isf.git] / ism / extras / wayland_immodule / wayland_module.c
1 /*
2  * Copyright © 2012, 2013 Intel Corporation
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #include <Ecore.h>
24 #include <Ecore_IMF.h>
25 #define EFL_BETA_API_SUPPORT
26 #include <Ecore_Wl2.h>
27 #include <stdio.h>
28 #include <dlog.h>
29
30 #include "wayland_imcontext.h"
31 #include <text-client-protocol.h>
32
33 #ifdef LOG_TAG
34 # undef LOG_TAG
35 #endif
36 #define LOG_TAG "IMMODULE"
37
38 int _ecore_imf_wayland_log_dom = -1;
39 static int ecore_wl2_init_count = 0;
40 static Ecore_Wl2_Display *ewd = NULL;
41
42 static const Ecore_IMF_Context_Info wayland_im_info =
43 {
44     "wayland",
45     "Wayland",
46     "*",
47     NULL,
48     0
49 };
50
51 static Ecore_IMF_Context_Class wayland_imf_class =
52 {
53     wayland_im_context_add,                    /* add */
54     wayland_im_context_del,                    /* del */
55     wayland_im_context_client_window_set,      /* client_window_set */
56     wayland_im_context_client_canvas_set,      /* client_canvas_set */
57     wayland_im_context_show,                   /* show */
58     wayland_im_context_hide,                   /* hide */
59     wayland_im_context_preedit_string_get,     /* get_preedit_string */
60     wayland_im_context_focus_in,               /* focus_in */
61     wayland_im_context_focus_out,              /* focus_out */
62     wayland_im_context_reset,                  /* reset */
63     wayland_im_context_cursor_position_set,    /* cursor_position_set */
64     wayland_im_context_use_preedit_set,        /* use_preedit_set */
65     wayland_im_context_input_mode_set,         /* input_mode_set */
66     wayland_im_context_filter_event,           /* filter_event */
67     wayland_im_context_preedit_string_with_attributes_get, /* preedit_string_with_attribute_get */
68     wayland_im_context_prediction_allow_set,   /* prediction_allow_set */
69     wayland_im_context_autocapital_type_set,   /* autocapital_type_set */
70     NULL,                                      /* control panel show */
71     NULL,                                      /* control panel hide */
72     wayland_im_context_input_panel_layout_set, /* input_panel_layout_set */
73     wayland_im_context_input_panel_layout_get, /* input_panel_layout_get */
74     wayland_im_context_input_panel_language_set,/* input_panel_language_set, */
75     NULL,                                      /* input_panel_language_get, */
76     wayland_im_context_cursor_location_set,    /* cursor_location_set */
77     wayland_im_context_input_panel_imdata_set, /* input_panel_imdata_set */
78     wayland_im_context_input_panel_imdata_get, /* input_panel_imdata_get */
79     wayland_im_context_input_panel_return_key_type_set, /* input_panel_return_key_type_set */
80     wayland_im_context_input_panel_return_key_disabled_set, /* input_panel_return_key_disabled_set */
81     NULL,                                      /* input_panel_caps_lock_mode_set */
82     wayland_im_context_input_panel_geometry_get, /* input_panel_geometry_get */
83     wayland_im_context_input_panel_state_get,  /* input_panel_state_get */
84     NULL,                                      /* input_panel_event_callback_add */
85     NULL,                                      /* input_panel_event_callback_del */
86     wayland_im_context_input_panel_language_locale_get, /* input_panel_language_locale_get */
87     NULL,                                      /* candidate_window_geometry_get */
88     wayland_im_context_input_hint_set,         /* input_hint_set */
89     wayland_im_context_bidi_direction_set,     /* bidi_direction_set */
90     wayland_im_context_input_panel_keyboard_mode_get, /* input_panel_keyboard_mode_get */
91     wayland_im_context_prediction_hint_set,    /* prediction_hint_set */
92     wayland_im_context_mime_type_accept_set,   /* mime_type_accept_set */
93     wayland_im_context_input_panel_position_set/* input_panel_position_set */
94 };
95
96 static struct wl_text_input_manager *text_input_manager = NULL;
97
98 static Ecore_IMF_Context *
99 im_module_exit(void)
100 {
101     return NULL;
102 }
103
104 static Ecore_IMF_Context *
105 im_module_create()
106 {
107    Ecore_IMF_Context *ctx = NULL;
108    WaylandIMContext *ctxd = NULL;
109
110    if (!text_input_manager) {
111        Ecore_Wl2_Global *global;
112        struct wl_registry *registry;
113        Eina_Iterator *globals;
114
115        ecore_wl2_init_count = ecore_wl2_init();
116        if (!ecore_wl2_init_count) {
117            LOGE("Could not initialize ecore_wl2");
118            return NULL;
119        }
120
121        ewd = ecore_wl2_display_connect(NULL);
122        if (!ewd) {
123            LOGE("Could not connect to Wayland Display");
124            goto err;
125        }
126
127        if (!(registry = ecore_wl2_display_registry_get(ewd)))
128            goto err;
129
130        if (!(globals = ecore_wl2_display_globals_get(ewd)))
131            goto err;
132
133        EINA_ITERATOR_FOREACH(globals, global)
134        {
135            if (!strcmp(global->interface, "wl_text_input_manager")) {
136                text_input_manager =
137                    wl_registry_bind(registry, global->id,
138                            &wl_text_input_manager_interface, 1);
139                EINA_LOG_DOM_INFO(_ecore_imf_wayland_log_dom,
140                        "bound wl_text_input_manager interface");
141                break;
142            }
143        }
144        eina_iterator_free (globals);
145
146        if (!text_input_manager)
147            goto err;
148    }
149
150    ctxd = wayland_im_context_new(text_input_manager);
151    if (!ctxd) return NULL;
152
153    ctx = ecore_imf_context_new(&wayland_imf_class);
154    if (!ctx) {
155        free(ctxd);
156        return NULL;
157    }
158
159    ecore_imf_context_data_set(ctx, ctxd);
160    wayland_im_context_pair_set(ctx, ctxd);
161
162    return ctx;
163
164 err:
165     ecore_wl2_init_count = 0;
166     if (ewd) {
167         ecore_wl2_display_disconnect(ewd);
168         ewd = NULL;
169     }
170
171     ecore_wl2_shutdown();
172     return NULL;
173 }
174
175 static Eina_Bool
176 im_module_init(void)
177 {
178    const char *s;
179
180    _ecore_imf_wayland_log_dom =
181      eina_log_domain_register("ecore_imf_wayland", EINA_COLOR_YELLOW);
182
183    if (!getenv("WAYLAND_DISPLAY")) return EINA_FALSE;
184    if ((s = getenv("ELM_DISPLAY"))) {
185        if (strcmp(s, "wl")) return EINA_FALSE;
186    }
187
188    ecore_imf_module_register(&wayland_im_info, im_module_create,
189                              im_module_exit);
190
191    // TIZEN_ONLY(20150708): Support back key
192    wayland_im_initialize ();
193    //
194    EINA_LOG_DOM_INFO(_ecore_imf_wayland_log_dom, "im module initialized");
195
196    return EINA_TRUE;
197 }
198
199 static void
200 im_module_shutdown(void)
201 {
202    // TIZEN_ONLY(20150708): Support back key
203     wayland_im_uninitialize ();
204    //
205    EINA_LOG_DOM_INFO(_ecore_imf_wayland_log_dom, "im module shutdown");
206
207    if (ewd)
208        ecore_wl2_display_disconnect(ewd);
209
210    if (ecore_wl2_init_count > 0)
211        ecore_wl2_shutdown();
212 }
213
214 EINA_MODULE_INIT(im_module_init);
215 EINA_MODULE_SHUTDOWN(im_module_shutdown);