summaryrefslogtreecommitdiff
path: root/plugins/ibus/gsd-ibus-manager.c
blob: 903ed961fc61f43840d9fd3b164787440ea23473 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2011 Red Hat, Inc
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

#include "config.h"

#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#include <locale.h>

#include <glib.h>
#include <glib/gi18n.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#include <gtk/gtk.h>

#include <ibus.h>

#include "gnome-settings-profile.h"
#include "gsd-ibus-manager.h"

#define IBUS_SETTINGS "desktop.ibus.shortcuts"
#define NEXT_INPUT_SOURCE_KEY "next-input-source"
#define PREV_INPUT_SOURCE_KEY "previous-input-source"

#define GSD_IBUS_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GSD_TYPE_IBUS_MANAGER, GsdIBusManagerPrivate))

struct GsdIBusManagerPrivate
{
        GSettings *ibus_settings;
};

enum {
        PROP_0,
};

static void     gsd_ibus_manager_class_init  (GsdIBusManagerClass *klass);
static void     gsd_ibus_manager_init        (GsdIBusManager      *ibus_manager);
static void     gsd_ibus_manager_finalize    (GObject             *object);

G_DEFINE_TYPE (GsdIBusManager, gsd_ibus_manager, G_TYPE_OBJECT)

static gpointer manager_object = NULL;

static gchar *
translate_accel (const gchar *in)
{
  struct {
    guint mask;
    const gchar *name;
  } mods[] = {
    { GDK_SHIFT_MASK, "Shift" },
    { GDK_LOCK_MASK, "Lock" },
    { GDK_CONTROL_MASK, "Control" },
    { GDK_MOD1_MASK, "Alt" },
    { GDK_MOD2_MASK, "Mod2" },
    { GDK_MOD3_MASK, "Mod3" },
    { GDK_MOD4_MASK, "Mod4" },
    { GDK_MOD5_MASK, "Mod5" },
    { GDK_SUPER_MASK, "Super" },
    { GDK_HYPER_MASK, "Hyper" },
    { GDK_META_MASK, "Meta" },
    { GDK_RELEASE_MASK, "Release" }
  };
  guint key;
  GdkModifierType mod;
  GString *s;
  gint i;

  gtk_accelerator_parse (in, &key, &mod);

  s = g_string_new ("");

  for (i = 0; i < G_N_ELEMENTS(mods); i++)
    {
      if (mod & mods[i].mask)
        {
          g_string_append (s, mods[i].name);
          g_string_append_c (s, '+');
        }
    }

  g_string_append (s, gdk_keyval_name (gdk_keyval_to_lower (key)));

  return g_string_free (s, FALSE);
}

static void
set_ibus_shortcut (GsdIBusManager *manager,
                   const gchar    *key,
                   const gchar    *value)
{
        IBusBus *bus;
        IBusConfig *config;
        GVariant *v;
        gchar *accel;
        const gchar *str[2];

        accel = translate_accel (value);

        bus = ibus_bus_new ();
        config = ibus_bus_get_config (bus);

        str[0] = accel;
        str[1] = NULL;

        v = g_variant_new_strv (str, 1);

        if (g_str_equal (key, NEXT_INPUT_SOURCE_KEY)) {
                ibus_config_set_value (config, "general/hotkey", "next_engine_in_menu", v);
        }
        else if (g_str_equal (key, PREV_INPUT_SOURCE_KEY)) {
                ibus_config_set_value (config, "general/hotkey", "previous_engine", v);
        }

        g_variant_unref (v);
        g_free (accel);

        g_object_unref (bus);
}

static void
ibus_settings_changed (GSettings      *settings,
                       const gchar    *key,
                       GsdIBusManager *manager)
{
        if (g_str_equal (key, NEXT_INPUT_SOURCE_KEY) ||
            g_str_equal (key, PREV_INPUT_SOURCE_KEY)) {
                gchar *value;

                value = g_settings_get_string (settings, key);
                g_debug ("Setting changed: %s %s, new value: %s", IBUS_SETTINGS, key, value);
                set_ibus_shortcut (manager, key, value);
                g_free (value);
        }
}

gboolean
gsd_ibus_manager_start (GsdIBusManager  *manager,
                        GError         **error)
{
        g_debug ("Starting ibus manager");
        gnome_settings_profile_start (NULL);

        ibus_init ();

        manager->priv->ibus_settings = g_settings_new (IBUS_SETTINGS);
        g_signal_connect (manager->priv->ibus_settings, "changed",
                          G_CALLBACK (ibus_settings_changed), manager);

        gnome_settings_profile_end (NULL);
        return TRUE;
}

void
gsd_ibus_manager_stop (GsdIBusManager *manager)
{
        g_debug ("Stopping ibus manager");

        g_object_unref (manager->priv->ibus_settings);
        manager->priv->ibus_settings = NULL;
}

static void
gsd_ibus_manager_set_property (GObject        *object,
                               guint           prop_id,
                               const GValue   *value,
                               GParamSpec     *pspec)
{
        switch (prop_id) {
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
        }
}

static void
gsd_ibus_manager_get_property (GObject        *object,
                               guint           prop_id,
                               GValue         *value,
                               GParamSpec     *pspec)
{
        switch (prop_id) {
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
        }
}

static GObject *
gsd_ibus_manager_constructor (GType                  type,
                              guint                  n_construct_properties,
                              GObjectConstructParam *construct_properties)
{
        GsdIBusManager      *ibus_manager;

        ibus_manager = GSD_IBUS_MANAGER (G_OBJECT_CLASS (gsd_ibus_manager_parent_class)->constructor (type,
                                                                                                      n_construct_properties,
                                                                                                      construct_properties));

        return G_OBJECT (ibus_manager);
}

static void
gsd_ibus_manager_dispose (GObject *object)
{
        G_OBJECT_CLASS (gsd_ibus_manager_parent_class)->dispose (object);
}

static void
gsd_ibus_manager_class_init (GsdIBusManagerClass *klass)
{
        GObjectClass   *object_class = G_OBJECT_CLASS (klass);

        object_class->get_property = gsd_ibus_manager_get_property;
        object_class->set_property = gsd_ibus_manager_set_property;
        object_class->constructor = gsd_ibus_manager_constructor;
        object_class->dispose = gsd_ibus_manager_dispose;
        object_class->finalize = gsd_ibus_manager_finalize;

        g_type_class_add_private (klass, sizeof (GsdIBusManagerPrivate));
}

static void
gsd_ibus_manager_init (GsdIBusManager *manager)
{
        manager->priv = GSD_IBUS_MANAGER_GET_PRIVATE (manager);

}

static void
gsd_ibus_manager_finalize (GObject *object)
{
        GsdIBusManager *ibus_manager;

        g_return_if_fail (object != NULL);
        g_return_if_fail (GSD_IS_IBUS_MANAGER (object));

        ibus_manager = GSD_IBUS_MANAGER (object);

        g_return_if_fail (ibus_manager->priv != NULL);

        G_OBJECT_CLASS (gsd_ibus_manager_parent_class)->finalize (object);
}

GsdIBusManager *
gsd_ibus_manager_new (void)
{
        if (manager_object != NULL) {
                g_object_ref (manager_object);
        } else {
                manager_object = g_object_new (GSD_TYPE_IBUS_MANAGER, NULL);
                g_object_add_weak_pointer (manager_object,
                                           (gpointer *) &manager_object);
        }

        return GSD_IBUS_MANAGER (manager_object);
}