Support to get input panel layout in wayland text input
[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 #include <Ecore_Wayland.h>
26 #include <stdio.h>
27
28 #include "wayland_imcontext.h"
29 #include <text-client-protocol.h>
30
31 int _ecore_imf_wayland_log_dom = -1;
32
33 static const Ecore_IMF_Context_Info wayland_im_info =
34 {
35     "wayland",
36     "Wayland",
37     "*",
38     NULL,
39     0
40 };
41
42 static Ecore_IMF_Context_Class wayland_imf_class =
43 {
44     wayland_im_context_add,                    /* add */
45     wayland_im_context_del,                    /* del */
46     wayland_im_context_client_window_set,      /* client_window_set */
47     wayland_im_context_client_canvas_set,      /* client_canvas_set */
48     wayland_im_context_show,                   /* show */
49     wayland_im_context_hide,                   /* hide */
50     wayland_im_context_preedit_string_get,     /* get_preedit_string */
51     wayland_im_context_focus_in,               /* focus_in */
52     wayland_im_context_focus_out,              /* focus_out */
53     wayland_im_context_reset,                  /* reset */
54     wayland_im_context_cursor_position_set,    /* cursor_position_set */
55     wayland_im_context_use_preedit_set,        /* use_preedit_set */
56     wayland_im_context_input_mode_set,         /* input_mode_set */
57     wayland_im_context_filter_event,           /* filter_event */
58     wayland_im_context_preedit_string_with_attributes_get, /* preedit_string_with_attribute_get */
59     wayland_im_context_prediction_allow_set,   /* prediction_allow_set */
60     wayland_im_context_autocapital_type_set,   /* autocapital_type_set */
61     NULL,                                      /* control panel show */
62     NULL,                                      /* control panel hide */
63     wayland_im_context_input_panel_layout_set, /* input_panel_layout_set */
64     wayland_im_context_input_panel_layout_get, /* input_panel_layout_get */
65     wayland_im_context_input_panel_language_set,/* input_panel_language_set, */
66     NULL,                                      /* input_panel_language_get, */
67     wayland_im_context_cursor_location_set,    /* cursor_location_set */
68     wayland_im_context_input_panel_imdata_set, /* input_panel_imdata_set */
69     wayland_im_context_input_panel_imdata_get, /* input_panel_imdata_get */
70     wayland_im_context_input_panel_return_key_type_set, /* input_panel_return_key_type_set */
71     wayland_im_context_input_panel_return_key_disabled_set, /* input_panel_return_key_disabled_set */
72     NULL,                                      /* input_panel_caps_lock_mode_set */
73     wayland_im_context_input_panel_geometry_get, /* input_panel_geometry_get */
74     wayland_im_context_input_panel_state_get,  /* input_panel_state_get */
75     NULL,                                      /* input_panel_event_callback_add */
76     NULL,                                      /* input_panel_event_callback_del */
77     wayland_im_context_input_panel_language_locale_get, /* input_panel_language_locale_get */
78     NULL,                                      /* candidate_window_geometry_get */
79     wayland_im_context_input_hint_set,         /* input_hint_set */
80     wayland_im_context_bidi_direction_set      /* bidi_direction_set */
81 };
82
83 static struct wl_text_input_manager *text_input_manager = NULL;
84
85 static Ecore_IMF_Context *
86 im_module_exit(void)
87 {
88     return NULL;
89 }
90
91 static Ecore_IMF_Context *
92 im_module_create()
93 {
94    Ecore_IMF_Context *ctx = NULL;
95    WaylandIMContext *ctxd = NULL;
96
97    if (!text_input_manager) {
98        Ecore_Wl_Global *global;
99        struct wl_registry *registry;
100        Eina_Inlist *globals;
101
102        if (!(registry = ecore_wl_registry_get()))
103            return NULL;
104
105        if (!(globals = ecore_wl_globals_get()))
106            return NULL;
107
108        EINA_INLIST_FOREACH(globals, global)
109        {
110            if (!strcmp(global->interface, "wl_text_input_manager")) {
111                text_input_manager =
112                    wl_registry_bind(registry, global->id,
113                            &wl_text_input_manager_interface, 1);
114                EINA_LOG_DOM_INFO(_ecore_imf_wayland_log_dom,
115                        "bound wl_text_input_manager interface");
116                break;
117            }
118        }
119
120        if (!text_input_manager)
121            return NULL;
122    }
123
124    ctxd = wayland_im_context_new(text_input_manager);
125    if (!ctxd) return NULL;
126
127    ctx = ecore_imf_context_new(&wayland_imf_class);
128    if (!ctx) {
129        free(ctxd);
130        return NULL;
131    }
132
133    ecore_imf_context_data_set(ctx, ctxd);
134
135    return ctx;
136 }
137
138 static Eina_Bool
139 im_module_init(void)
140 {
141    const char *s;
142
143    _ecore_imf_wayland_log_dom =
144      eina_log_domain_register("ecore_imf_wayland", EINA_COLOR_YELLOW);
145
146    if (!getenv("WAYLAND_DISPLAY")) return EINA_FALSE;
147    if ((s = getenv("ELM_DISPLAY"))) {
148        if (strcmp(s, "wl")) return EINA_FALSE;
149    }
150
151    if (!ecore_wl_init(NULL))
152        return EINA_FALSE;
153
154    ecore_imf_module_register(&wayland_im_info, im_module_create,
155                              im_module_exit);
156
157    // TIZEN_ONLY(20150708): Support back key
158    register_key_handler();
159    //
160    EINA_LOG_DOM_INFO(_ecore_imf_wayland_log_dom, "im module initalized");
161
162    return EINA_TRUE;
163 }
164
165 static void
166 im_module_shutdown(void)
167 {
168    // TIZEN_ONLY(20150708): Support back key
169    unregister_key_handler();
170    //
171    EINA_LOG_DOM_INFO(_ecore_imf_wayland_log_dom, "im module shutdown");
172    ecore_wl_shutdown();
173 }
174
175 EINA_MODULE_INIT(im_module_init);
176 EINA_MODULE_SHUTDOWN(im_module_shutdown);