summaryrefslogtreecommitdiff
path: root/src/modules/ecore_imf/wayland/wayland_module.c
blob: 57b1117ee1d5905eab090718913332aa63ec0cd5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Copyright © 2012, 2013 Intel Corporation
 *
 * Permission to use, copy, modify, distribute, and sell this software and
 * its documentation for any purpose is hereby granted without fee, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of the copyright holders not be used in
 * advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.  The copyright holders make
 * no representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <Ecore.h>
#include <Ecore_IMF.h>
#include <Ecore_Wl2.h>
#include <stdio.h>

#include "wayland_imcontext.h"
#include "text-input-unstable-v1-client-protocol.h"

int _ecore_imf_wayland_log_dom = -1;
Ecore_Wl2_Display *ewd;

static const Ecore_IMF_Context_Info wayland_im_info =
{
   "wayland",
   "Wayland",
   "*",
   NULL,
   0
};

static Ecore_IMF_Context_Class wayland_imf_class =
{
   wayland_im_context_add,                    /* add */
   wayland_im_context_del,                    /* del */
   wayland_im_context_client_window_set,      /* client_window_set */
   wayland_im_context_client_canvas_set,      /* client_canvas_set */
   wayland_im_context_show,                   /* show */
   wayland_im_context_hide,                   /* hide */
   wayland_im_context_preedit_string_get,     /* get_preedit_string */
   wayland_im_context_focus_in,               /* focus_in */
   wayland_im_context_focus_out,              /* focus_out */
   wayland_im_context_reset,                  /* reset */
   wayland_im_context_cursor_position_set,    /* cursor_position_set */
   wayland_im_context_use_preedit_set,        /* use_preedit_set */
   wayland_im_context_input_mode_set,         /* input_mode_set */
   wayland_im_context_filter_event,           /* filter_event */
   wayland_im_context_preedit_string_with_attributes_get, /* preedit_string_with_attribute_get */
   wayland_im_context_prediction_allow_set,   /* prediction_allow_set */
   wayland_im_context_autocapital_type_set,   /* autocapital_type_set */
   NULL,                                      /* control panel show */
   NULL,                                      /* control panel hide */
   wayland_im_context_input_panel_layout_set, /* input_panel_layout_set */
   NULL,                                      /* input_panel_layout_get, */
   wayland_im_context_input_panel_language_set,/* input_panel_language_set, */
   NULL,                                      /* input_panel_language_get, */
   wayland_im_context_cursor_location_set,    /* cursor_location_set */
   NULL,                                      /* input_panel_imdata_set */
   NULL,                                      /* input_panel_imdata_get */
   NULL,                                      /* input_panel_return_key_type_set */
   NULL,                                      /* input_panel_return_key_disabled_set */
   NULL,                                      /* input_panel_caps_lock_mode_set */
   NULL,                                      /* input_panel_geometry_get */
   NULL,                                      /* input_panel_state_get */
   NULL,                                      /* input_panel_event_callback_add */
   NULL,                                      /* input_panel_event_callback_del */
   wayland_im_context_input_panel_language_locale_get, /* input_panel_language_locale_get */
   NULL,                                      /* candidate_window_geometry_get */
   wayland_im_context_input_hint_set,         /* input_hint_set */
   NULL                                       /* bidi_direction_set */
};

static struct zwp_text_input_manager_v1 *text_input_manager = NULL;

static Ecore_IMF_Context *
im_module_exit(void)
{
   return NULL;
}

static Ecore_IMF_Context *
im_module_create()
{
   Ecore_IMF_Context *ctx = NULL;
   WaylandIMContext *ctxd = NULL;

   if (!text_input_manager)
     {
        Eina_Iterator *itr;

        itr = ecore_wl2_display_globals_get(ewd);
        if (itr)
          {
             Ecore_Wl2_Global *global;
             struct wl_registry *registry;

             registry = ecore_wl2_display_registry_get(ewd);
             EINA_ITERATOR_FOREACH(itr, global)
               {
                  if (!strcmp(global->interface, "zwp_text_input_manager_v1"))
                    {
                       text_input_manager =
                         wl_registry_bind(registry, global->id,
                                          &zwp_text_input_manager_v1_interface, 1);
                       EINA_LOG_DOM_INFO(_ecore_imf_wayland_log_dom,
                                         "bound wl_text_input_manager interface");
                       break;
                    }
               }
             eina_iterator_free(itr);
          }

        if (!text_input_manager)
          return NULL;
     }

   ctxd = wayland_im_context_new(text_input_manager);
   if (!ctxd) return NULL;

   ctx = ecore_imf_context_new(&wayland_imf_class);
   if (!ctx)
     {
        free(ctxd);
        return NULL;
     }

   ecore_imf_context_data_set(ctx, ctxd);

   return ctx;
}

static Eina_Bool
im_module_init(void)
{
   const char *s;

   _ecore_imf_wayland_log_dom = 
     eina_log_domain_register("ecore_imf_wayland", EINA_COLOR_YELLOW);

   if (!getenv("WAYLAND_DISPLAY")) return EINA_FALSE;
   if ((s = getenv("ELM_DISPLAY")))
     {
        if (strcmp(s, "wl")) return EINA_FALSE;
     }

   if (!ecore_wl2_init())
     return EINA_FALSE;

   ewd = ecore_wl2_display_connect(NULL);
   if (!ewd) goto err;

   ecore_imf_module_register(&wayland_im_info, im_module_create, 
                             im_module_exit);
   EINA_LOG_DOM_INFO(_ecore_imf_wayland_log_dom, "im module initalized");

   return EINA_TRUE;

err:
   ecore_wl2_shutdown();
   return EINA_FALSE;
}

static void
im_module_shutdown(void)
{
   EINA_LOG_DOM_INFO(_ecore_imf_wayland_log_dom, "im module shutdown");
   ecore_wl2_display_disconnect(ewd);
   ecore_wl2_shutdown();
}

EINA_MODULE_INIT(im_module_init);
EINA_MODULE_SHUTDOWN(im_module_shutdown);

/* vim:ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0
*/