1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 /* vim:set et sts=4: */
3 /* ibus - The Input Bus
4 * Copyright (C) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
5 * Copyright (C) 2008-2010 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #include "ibusshare.h"
25 #include <glib/gstdio.h>
26 #include <glib-object.h>
28 #include <sys/types.h>
35 static gchar *_display = NULL;
38 ibus_get_local_machine_id (void)
40 static gchar *machine_id = NULL;
42 if (machine_id == NULL) {
44 if (!g_file_get_contents ("/var/lib/dbus/machine-id",
48 !g_file_get_contents ("/etc/machine-id",
52 g_warning ("Unable to load /var/lib/dbus/machine-id: %s", error->message);
54 machine_id = "machine-id";
57 g_strstrip (machine_id);
65 ibus_set_display (const gchar *display)
69 _display = g_strdup (display);
73 ibus_get_user_name (void)
75 return g_get_user_name ();
79 ibus_get_daemon_uid (void)
85 ibus_get_session_id (void)
87 return g_getenv("IBUS_SESSION_ID");
91 ibus_get_socket_path (void)
93 static gchar *path = NULL;
96 gchar *hostname = "unix";
98 gchar *displaynumber = "0";
99 /* gchar *screennumber = "0"; */
102 path = g_strdup (g_getenv ("IBUS_ADDRESS_FILE"));
107 if (_display == NULL) {
108 display = g_strdup (g_getenv ("DISPLAY"));
111 display = g_strdup (_display);
114 if (display == NULL) {
115 g_warning ("DISPLAY is empty! We use default DISPLAY (:0.0)");
120 for (; *p != ':' && *p != '\0'; p++);
128 for (; *p != '.' && *p != '\0'; p++);
133 /* Do not use screennumber
138 if (hostname[0] == '\0')
141 p = g_strdup_printf ("%s-%s-%s",
142 ibus_get_local_machine_id (),
145 path = g_build_filename (g_get_user_config_dir (),
157 ibus_get_timeout (void)
159 /* 6000 ms is the default timeout on the ibus-daemon side (5 sec) plus 1. */
160 static const gint default_timeout = 6000;
162 static gint64 timeout = -2;
164 const gchar *timeout_str = g_getenv ("IBUS_TIMEOUT");
165 if (timeout_str == NULL) {
166 timeout = default_timeout;
168 timeout = g_ascii_strtoll(timeout_str, NULL, 10);
169 if (timeout < -1 || timeout == 0 || timeout > G_MAXINT) {
170 timeout = default_timeout;
178 ibus_get_address (void)
180 static gchar *address = NULL;
182 static gchar buffer[1024];
186 if (address != NULL) {
191 /* get address from env variable */
192 address = g_strdup (g_getenv ("IBUS_ADDRESS"));
197 /* read address from ~/.config/ibus/bus/soketfile */
198 pf = fopen (ibus_get_socket_path (), "r");
205 if (fgets (buffer, sizeof (buffer), pf) == NULL)
208 /* skip comment line */
211 /* parse IBUS_ADDRESS */
212 if (strncmp (p, "IBUS_ADDRESS=", sizeof ("IBUS_ADDRESS=") - 1) == 0) {
213 address = p + sizeof ("IBUS_ADDRESS=") - 1;
214 for (p = (gchar *)address; *p != '\n' && *p != '\0'; p++);
217 address = g_strdup (address);
221 /* parse IBUS_DAEMON_PID */
222 if (strncmp (p, "IBUS_DAEMON_PID=", sizeof ("IBUS_DAEMON_PID=") - 1) == 0) {
223 pid = atoi(p + sizeof ("IBUS_DAEMON_PID=") - 1);
230 if (pid == -1 || kill (pid, 0) != 0) {
238 ibus_write_address (const gchar *address)
242 g_return_if_fail (address != NULL);
244 path = g_path_get_dirname (ibus_get_socket_path ());
245 g_mkdir_with_parents (path, 0700);
248 g_unlink (ibus_get_socket_path ());
249 pf = fopen (ibus_get_socket_path (), "w");
250 g_return_if_fail (pf != NULL);
253 "# This file is created by ibus-daemon, please do not modify it\n"
255 "IBUS_DAEMON_PID=%ld\n",
256 address, (glong) getpid ());
261 ibus_free_strv (gchar **strv)
268 for (p = strv; *p != NULL; p++) {
278 #if !GLIB_CHECK_VERSION(2,35,0)
285 IBUS_TYPE_LOOKUP_TABLE;
287 IBUS_TYPE_ENGINE_DESC;
288 IBUS_TYPE_OBSERVED_PATH;
292 static GMainLoop *main_loop = NULL;
297 main_loop = g_main_loop_new (NULL, FALSE);
299 g_main_loop_run (main_loop);
301 g_main_loop_unref (main_loop);
309 g_main_loop_quit (main_loop);
313 static gboolean ibus_log_handler_is_verbose = FALSE;
314 static guint ibus_log_handler_id = 0;
317 ibus_log_handler (const gchar *log_domain,
318 GLogLevelFlags log_level,
319 const gchar *message,
322 // In the quiet mode (i.e. not verbose), we'll ignore DEBUG and
324 if (!ibus_log_handler_is_verbose &&
325 ((log_level & G_LOG_LEVEL_DEBUG) ||
326 (log_level & G_LOG_LEVEL_WARNING))) {
329 // Add timing info like "17:34:57.680038" (hour, min, sec, microsecond).
330 struct timeval time_val;
331 gettimeofday (&time_val, NULL);
332 struct tm local_time;
333 localtime_r (&time_val.tv_sec, &local_time);
335 g_strdup_printf ("%02d:%02d:%02d.%6d: %s",
339 (int)time_val.tv_usec,
341 g_log_default_handler (log_domain, log_level, new_message, user_data);
342 g_free (new_message);
346 ibus_set_log_handler (gboolean verbose)
348 if (ibus_log_handler_id != 0) {
349 g_log_remove_handler (G_LOG_DOMAIN, ibus_log_handler_id);
352 ibus_log_handler_is_verbose = verbose;
353 ibus_log_handler_id = g_log_set_handler (G_LOG_DOMAIN,
360 ibus_unset_log_handler (void)
362 if (ibus_log_handler_id != 0) {
363 g_log_remove_handler (G_LOG_DOMAIN, ibus_log_handler_id);
364 ibus_log_handler_id = 0;