summaryrefslogtreecommitdiff
path: root/src/ibusshare.c
blob: d7724a6b2c550e031eaff261dc13aaa16afe1f5a (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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
/* vim:set et sts=4: */
/* ibus - The Input Bus
 * Copyright (C) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
 * Copyright (C) 2015-2018 Takao Fujiwara <takao.fujiwara1@gmail.com>
 * Copyright (C) 2008-2018 Red Hat, Inc.
 *
 * 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.1 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

#include "ibusshare.h"
#include <glib.h>
#include <glib/gstdio.h>
#include <glib-object.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ibus.h>

static gchar *_display = NULL;

const gchar *
ibus_get_local_machine_id (void)
{
    static gchar *machine_id = NULL;

    if (machine_id == NULL) {
        GError *error = NULL;
        if (!g_file_get_contents ("/var/lib/dbus/machine-id",
                                  &machine_id,
                                  NULL,
                                  &error) &&
            !g_file_get_contents ("/etc/machine-id",
                                  &machine_id,
                                  NULL,
                                  NULL)) {
            g_warning ("Unable to load /var/lib/dbus/machine-id: %s", error->message);
            machine_id = "machine-id";
        }
        else {
            g_strstrip (machine_id);
        }
        if (error != NULL) {
            g_error_free (error);
        }
    }

    return machine_id;
}

void
ibus_set_display (const gchar *display)
{
    if (_display != NULL)
        g_free (_display);
    _display = g_strdup (display);
}

const gchar *
ibus_get_user_name (void)
{
    return g_get_user_name ();
}

glong
ibus_get_daemon_uid (void)
{
    return getuid ();
}

const gchar *
ibus_get_session_id (void)
{
    return g_getenv("IBUS_SESSION_ID");
}

const gchar *
ibus_get_socket_path (void)
{
    static gchar *path = NULL;

    if (path == NULL) {
        gchar *hostname = "unix";
        gchar *display;
        gchar *displaynumber = "0";
        /* gchar *screennumber = "0"; */
        gchar *p;

        path = g_strdup (g_getenv ("IBUS_ADDRESS_FILE"));
        if (path != NULL) {
            return path;
        }

        if (_display == NULL) {
            display = g_strdup (g_getenv ("DISPLAY"));
        }
        else {
            display = g_strdup (_display);
        }

        if (display) {
            p = display;
            hostname = display;
            for (; *p != ':' && *p != '\0'; p++);

            if (*p == ':') {
                *p = '\0';
                p++;
                displaynumber = p;
            }

            for (; *p != '.' && *p != '\0'; p++);

            if (*p == '.') {
                *p = '\0';
                p++;
                /* Do not use screennumber
                 screennumber = p; */
            }
        }

        if (hostname[0] == '\0')
            hostname = "unix";

        p = g_strdup_printf ("%s-%s-%s",
                             ibus_get_local_machine_id (),
                             hostname,
                             displaynumber);
        path = g_build_filename (g_get_user_config_dir (),
                                 "ibus",
                                 "bus",
                                 p,
                                 NULL);
        g_free (p);
        g_free (display);
    }
    return path;
}

gint
ibus_get_timeout (void)
{
    /* 16000 ms is the default timeout on the ibus-daemon side
     * (15 sec) plus 1. */
    static const gint default_timeout = 16000;

    static gint64 timeout = -2;
    if (timeout == -2) {
        const gchar *timeout_str = g_getenv ("IBUS_TIMEOUT");
        if (timeout_str == NULL) {
            timeout = default_timeout;
        } else {
            timeout = g_ascii_strtoll(timeout_str, NULL, 10);
            if (timeout < -1 || timeout == 0 || timeout > G_MAXINT) {
                timeout = default_timeout;
            }
        }
    }
    return timeout;
}

const gchar *
ibus_get_address (void)
{
    static gchar *address = NULL;
    pid_t pid = -1;
    static gchar buffer[1024];
    FILE *pf;

    /* free address */
    if (address != NULL) {
        g_free (address);
        address = NULL;
    }

    /* get address from env variable */
    address = g_strdup (g_getenv ("IBUS_ADDRESS"));
    if (address) {
        return address;
    }

    /* read address from ~/.config/ibus/bus/soketfile */
    pf = fopen (ibus_get_socket_path (), "r");
    if (pf == NULL) {
        return NULL;
    }

    while (!feof (pf)) {
        gchar *p = buffer;
        if (fgets (buffer, sizeof (buffer), pf) == NULL)
            break;

        /* skip comment line */
        if (p[0] == '#')
            continue;
        /* parse IBUS_ADDRESS */
        if (strncmp (p, "IBUS_ADDRESS=", sizeof ("IBUS_ADDRESS=") - 1) == 0) {
            address = p + sizeof ("IBUS_ADDRESS=") - 1;
            for (p = (gchar *)address; *p != '\n' && *p != '\0'; p++);
            if (*p == '\n')
                *p = '\0';
            address = g_strdup (address);
            continue;
        }

        /* parse IBUS_DAEMON_PID */
        if (strncmp (p, "IBUS_DAEMON_PID=", sizeof ("IBUS_DAEMON_PID=") - 1) == 0) {
            pid = atoi(p + sizeof ("IBUS_DAEMON_PID=") - 1);
            continue;
        }

    }
    fclose (pf);

    if (pid == -1 || kill (pid, 0) != 0) {
        return NULL;
    }

    return address;
}

void
ibus_write_address (const gchar *address)
{
    FILE *pf;
    gchar *path;
    g_return_if_fail (address != NULL);

    path = g_path_get_dirname (ibus_get_socket_path ());
    g_mkdir_with_parents (path, 0700);
    g_free (path);

    g_unlink (ibus_get_socket_path ());
    pf = fopen (ibus_get_socket_path (), "w");
    g_return_if_fail (pf != NULL);

    fprintf (pf,
        "# This file is created by ibus-daemon, please do not modify it\n"
        "IBUS_ADDRESS=%s\n"
        "IBUS_DAEMON_PID=%ld\n",
        address, (glong) getpid ());
    fclose (pf);
}

void
ibus_free_strv (gchar **strv)
{
    gchar **p;

    if (strv == NULL)
        return;

    for (p = strv; *p != NULL; p++) {
        g_free (*p);
    }

    g_free (strv);
}

void
ibus_init (void)
{
#if !GLIB_CHECK_VERSION(2,35,0)
    g_type_init ();
#endif
    IBUS_ERROR;
    IBUS_TYPE_TEXT;
    IBUS_TYPE_ATTRIBUTE;
    IBUS_TYPE_ATTR_LIST;
    IBUS_TYPE_LOOKUP_TABLE;
    IBUS_TYPE_COMPONENT;
    IBUS_TYPE_ENGINE_DESC;
    IBUS_TYPE_OBSERVED_PATH;
    IBUS_TYPE_REGISTRY;
    IBUS_TYPE_X_EVENT;
}

static GMainLoop *main_loop = NULL;

void
ibus_main (void)
{
    main_loop = g_main_loop_new (NULL, FALSE);

    g_main_loop_run (main_loop);

    g_main_loop_unref (main_loop);
    main_loop = NULL;
}

void
ibus_quit (void)
{
    if (main_loop) {
        g_main_loop_quit (main_loop);
    }
}

static gboolean ibus_log_handler_is_verbose = FALSE;
static guint ibus_log_handler_id = 0;

static void
ibus_log_handler (const gchar    *log_domain,
                  GLogLevelFlags  log_level,
                  const gchar    *message,
                  gpointer        user_data)
{
    // In the quiet mode (i.e. not verbose), we'll ignore DEBUG and
    // WARNING messages.
    if (!ibus_log_handler_is_verbose &&
        ((log_level & G_LOG_LEVEL_DEBUG) ||
         (log_level & G_LOG_LEVEL_WARNING))) {
        return;
    }
    // Add timing info like "17:34:57.680038" (hour, min, sec, microsecond).
    struct timeval time_val;
    gettimeofday (&time_val, NULL);
    struct tm local_time;
    localtime_r (&time_val.tv_sec, &local_time);
    char* new_message =
        g_strdup_printf ("%02d:%02d:%02d.%6d: %s",
                         local_time.tm_hour,
                         local_time.tm_min,
                         local_time.tm_sec,
                         (int)time_val.tv_usec,
                         message);
    g_log_default_handler (log_domain, log_level, new_message, user_data);
    g_free (new_message);
}

void
ibus_set_log_handler (gboolean verbose)
{
    if (ibus_log_handler_id != 0) {
        g_log_remove_handler (G_LOG_DOMAIN, ibus_log_handler_id);
    }

    ibus_log_handler_is_verbose = verbose;
    ibus_log_handler_id = g_log_set_handler (G_LOG_DOMAIN,
                                             G_LOG_LEVEL_MASK,
                                             ibus_log_handler,
                                             NULL);
}

void
ibus_unset_log_handler (void)
{
    if (ibus_log_handler_id != 0) {
        g_log_remove_handler (G_LOG_DOMAIN, ibus_log_handler_id);
        ibus_log_handler_id = 0;
    }
}