File: ibusshare.c

package info (click to toggle)
ibus 1.5.23-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 25,552 kB
  • sloc: ansic: 115,459; python: 6,706; sh: 5,785; makefile: 2,110; xml: 1,488; sed: 21
file content (464 lines) | stat: -rw-r--r-- 12,371 bytes parent folder | download
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/* -*- 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");
}

static const gchar *ibus_get_socket_path_internal (gboolean compat, gchar *client_display);

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

    if (path == NULL) {
        path = ibus_get_socket_path_internal (FALSE, NULL);
    }
    return path;
}

static const gchar *
ibus_get_socket_path_x11_compat (void)
{
    static gchar *path = NULL;
    if (path == NULL)
        path = ibus_get_socket_path_internal (TRUE, NULL);
    return path;
}

static const gchar *
ibus_get_socket_path_gnome_xwayland_compat (void)
{
    static gchar *path = NULL;
    if (path == NULL) {
        gint setup_display_number;
        gchar *client_display;
        gchar **tokens;

        tokens = g_strsplit (ibus_get_socket_path_x11_compat (), "-", 3);
        if (tokens[0] != NULL && tokens[1] != NULL && tokens[2] != NULL) {
            setup_display_number = g_strtod(tokens[2], NULL);
            if (setup_display_number > 0) {
                client_display = g_strdup_printf (":%d", setup_display_number - 1);
                path = ibus_get_socket_path_internal (TRUE, client_display);
            }
        }

        g_strfreev (tokens);
    }
    return path;
}

static const gchar *
ibus_get_socket_path_internal (gboolean compat, gchar *client_display)
{
    gchar *path = NULL;

    gchar *hostname = "unix";
    gchar *display;
    gchar *displaynumber = "0";
    /* gchar *screennumber = "0"; */
    gboolean is_wayland = FALSE;
    gchar *p;

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

    if (_display == NULL) {
        if (compat) {
            if (client_display == NULL)
                display = g_strdup (g_getenv ("DISPLAY"));
            else
                display = client_display;
        } else {
            display = g_strdup (g_getenv ("WAYLAND_DISPLAY"));
            if (display)
                is_wayland = TRUE;
            else if (client_display == NULL)
                display = g_strdup (g_getenv ("DISPLAY"));
            else
                display = client_display;
        }
    }
    else {
        display = g_strdup (_display);
    }

    if (is_wayland) {
        displaynumber = display;
    } else 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);
    /* Qt5 IBus module has a hard-coded path and we cannot change this
     * for the back compatibility.
     * XDG_RUNTIME_DIR is not useful because it's generated by
     * login but not `su` command and ibus-daemon can be run with `su`
     * and we may change the path to XDG_CACHE_HOME in the future.
     */
    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;
}

static void ibus_write_address_internal (const gchar *address, const gchar *socket_path);

void
ibus_write_address (const gchar *address)
{
    const gchar *socket_path;
    const gchar *socket_path_compat;

    socket_path = ibus_get_socket_path ();
    ibus_write_address_internal (address, socket_path);

    if (g_getenv ("WAYLAND_DISPLAY") != NULL) {
        socket_path_compat = ibus_get_socket_path_x11_compat ();
        ibus_write_address_internal (address, socket_path_compat);

        if (g_getenv ("GNOME_SETUP_DISPLAY") != NULL &&
            strcmp (g_getenv ("GNOME_SETUP_DISPLAY"), g_getenv ("DISPLAY")) == 0) {
            /* Running from gnome-shell with the setup display; write the socket
             * address to an additional path for X11 clients */
            socket_path_compat = ibus_get_socket_path_gnome_xwayland_compat ();
            if (socket_path_compat != NULL)
                ibus_write_address_internal (address, socket_path_compat);
        }
    }
}

static void
ibus_write_address_internal (const gchar *address, const gchar *socket_path)
{
    FILE *pf;
    gchar *path;
    g_return_if_fail (address != NULL);

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

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

    fprintf (pf,
        "# This file is created by ibus-daemon, please do not modify it.\n"
        "# This file allows processes on the machine to find the\n"
        "# ibus session bus with the below address.\n"
        "# If the IBUS_ADDRESS environment variable is set, it will\n"
        "# be used rather than this file.\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;
    }
}