summaryrefslogtreecommitdiff
path: root/gtk/gtkimmodule.c
blob: a4c90dac1327612b7aee04e5c4a59a919ff3a983 (plain)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.Free
 */

/*
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

#include "config.h"

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <glib/gstdio.h>
#include <gmodule.h>
#include "gtkimmodule.h"
#include "gtkimmoduleprivate.h"
#include "gtkimcontextsimple.h"
#include "gtkmodulesprivate.h"
#include "gtksettings.h"
#include "gtkprivate.h"
#include "gtkutilsprivate.h"
#include "gtkintl.h"

#ifdef GDK_WINDOWING_X11
#include "x11/gdkx.h"
#include "gtkimcontextxim.h"
#endif

#ifdef GDK_WINDOWING_WAYLAND
#include "wayland/gdkwayland.h"
#include "gtkimcontextwayland.h"
#endif

#ifdef GDK_WINDOWING_BROADWAY
#include "broadway/gdkbroadway.h"
#include "gtkimcontextbroadway.h"
#endif

#ifdef GDK_WINDOWING_WIN32
#include "win32/gdkwin32.h"
#include "gtkimcontextime.h"
#endif

#ifdef GDK_WINDOWING_QUARTZ
#include "quartz/gdkquartz.h"
#include "gtkimcontextquartz.h"
#endif

#ifdef G_OS_WIN32
#include <windows.h>
#endif

#define SIMPLE_ID "gtk-im-context-simple"
#define NONE_ID   "gtk-im-context-none"

/**
 * _gtk_im_module_create:
 * @context_id: the context ID for the context type to create
 * 
 * Create an IM context of a type specified by the string
 * ID @context_id.
 * 
 * Returns: a newly created input context of or @context_id, or
 *     if that could not be created, a newly created GtkIMContextSimple.
 */
GtkIMContext *
_gtk_im_module_create (const gchar *context_id)
{
  GIOExtensionPoint *ep;
  GIOExtension *ext;
  GType type;
  GtkIMContext *context = NULL;

  if (strcmp (context_id, NONE_ID) == 0)
    return NULL;

  ep = g_io_extension_point_lookup (GTK_IM_MODULE_EXTENSION_POINT_NAME);
  ext = g_io_extension_point_get_extension_by_name (ep, context_id);
  if (ext)
    {
      type = g_io_extension_get_type (ext);
      context = g_object_new (type, NULL);
    }

  return context;
}

static gboolean
match_backend (GdkDisplay *display,
               const char *context_id)
{
  if (g_strcmp0 (context_id, "wayland") == 0)
    {
#ifdef GDK_WINDOWING_WAYLAND
      return GDK_IS_WAYLAND_DISPLAY (display) &&
             gdk_wayland_display_query_registry (display,
                                                 "zwp_text_input_manager_v3");
#else
      return FALSE;
#endif
    }

  if (g_strcmp0 (context_id, "broadway") == 0)
#ifdef GDK_WINDOWING_BROADWAY
    return GDK_IS_BROADWAY_DISPLAY (display);
#else
    return FALSE;
#endif

  if (g_strcmp0 (context_id, "xim") == 0)
#ifdef GDK_WINDOWING_X11
    return GDK_IS_X11_DISPLAY (display);
#else
    return FALSE;
#endif

  if (g_strcmp0 (context_id, "ime") == 0)
#ifdef GDK_WINDOWING_WIN32
    return GDK_IS_WIN32_DISPLAY (display);
#else
    return FALSE;
#endif

  if (g_strcmp0 (context_id, "quartz") == 0)
#ifdef GDK_WINDOWING_QUARTZ
    return GDK_IS_QUARTZ_DISPLAY (display);
#else
    return FALSE;
#endif

  return TRUE;
}

static const gchar *
lookup_immodule (GdkDisplay  *display,
                 gchar      **immodules_list)
{
  guint i;

  for (i = 0; immodules_list[i]; i++)
    {
      if (!match_backend (display, immodules_list[i]))
        continue;

      if (g_strcmp0 (immodules_list[i], SIMPLE_ID) == 0)
        return SIMPLE_ID;
      else if (g_strcmp0 (immodules_list[i], NONE_ID) == 0)
        return NONE_ID;
      else
        {
          GIOExtensionPoint *ep;
          GIOExtension *ext;

          ep = g_io_extension_point_lookup (GTK_IM_MODULE_EXTENSION_POINT_NAME);
          ext = g_io_extension_point_get_extension_by_name (ep, immodules_list[i]);
          if (ext)
            return g_io_extension_get_name (ext);
        }
    }

  return NULL;
}

/**
 * _gtk_im_module_get_default_context_id:
 * @display: The display to look up the module for
 *
 * Return the context_id of the best IM context type 
 * for the given window.
 * 
 * Returns: the context ID (will never be %NULL)
 */
const gchar *
_gtk_im_module_get_default_context_id (GdkDisplay *display)
{
  const gchar *context_id = NULL;
  const gchar *envvar;
  GtkSettings *settings;
  GIOExtensionPoint *ep;
  GList *l;
  char *tmp;

  envvar = g_getenv ("GTK_IM_MODULE");
  if (envvar)
    {
      char **immodules;
      immodules = g_strsplit (envvar, ":", 0);
      context_id = lookup_immodule (display, immodules);
      g_strfreev (immodules);

      if (context_id)
        return context_id;
    }

  /* Check if the certain immodule is set in XSETTINGS. */
  settings = gtk_settings_get_for_display (display);
  g_object_get (G_OBJECT (settings), "gtk-im-module", &tmp, NULL);
  if (tmp)
    {
      char **immodules;

      immodules = g_strsplit (tmp, ":", 0);
      context_id = lookup_immodule (display, immodules);
      g_strfreev (immodules);
      g_free (tmp);

      if (context_id)
        return context_id;
    }

  ep = g_io_extension_point_lookup (GTK_IM_MODULE_EXTENSION_POINT_NAME);
  for (l = g_io_extension_point_get_extensions (ep); l; l = l->next)
    {
      GIOExtension *ext = l->data;

      context_id = g_io_extension_get_name (ext);
      if (match_backend (display, context_id))
        return context_id;
    }

  g_error ("GTK was run without any IM module being present. This must not happen.");

  return SIMPLE_ID;
}

void
gtk_im_module_ensure_extension_point (void)
{
  GIOExtensionPoint *ep;
  static gboolean registered = FALSE;

  if (registered)
    return;

  GTK_NOTE (MODULES,
            g_print ("Registering extension point %s\n", GTK_IM_MODULE_EXTENSION_POINT_NAME));

  ep = g_io_extension_point_register (GTK_IM_MODULE_EXTENSION_POINT_NAME);
  g_io_extension_point_set_required_type (ep, GTK_TYPE_IM_CONTEXT);

  registered = TRUE;
}

void
gtk_im_modules_init (void)
{
  GIOModuleScope *scope;
  char **paths;
  int i;

  gtk_im_module_ensure_extension_point ();

  g_type_ensure (gtk_im_context_simple_get_type ());
#ifdef GDK_WINDOWING_X11
  g_type_ensure (gtk_im_context_xim_get_type ());
#endif
#ifdef GDK_WINDOWING_WAYLAND
  g_type_ensure (gtk_im_context_wayland_get_type ());
#endif
#ifdef GDK_WINDOWING_BROADWAY
  g_type_ensure (gtk_im_context_broadway_get_type ());
#endif
#ifdef GDK_WINDOWING_WIN32
  g_type_ensure (gtk_im_context_ime_get_type ());
#endif
#ifdef GDK_WINDOWING_QUARTZ
  g_type_ensure (gtk_im_context_quartz_get_type ());
#endif

  scope = g_io_module_scope_new (G_IO_MODULE_SCOPE_BLOCK_DUPLICATES);

  paths = _gtk_get_module_path ("immodules");
  for (i = 0; paths[i]; i++)
    {
      GTK_NOTE (MODULES,
                g_print ("Scanning io modules in %s\n", paths[i]));
      g_io_modules_scan_all_in_directory_with_scope (paths[i], scope);
    }
  g_strfreev (paths);

  g_io_module_scope_free (scope);

  if (GTK_DEBUG_CHECK (MODULES))
    {
      GIOExtensionPoint *ep;
      GList *list, *l;

      ep = g_io_extension_point_lookup (GTK_IM_MODULE_EXTENSION_POINT_NAME);
      list = g_io_extension_point_get_extensions (ep);
      for (l = list; l; l = l->next)
        {
          GIOExtension *ext = l->data;
          g_print ("extension: %s: type %s\n",
                   g_io_extension_get_name (ext),
                   g_type_name (g_io_extension_get_type (ext)));
        }
    }
}