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.1 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 Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
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) {
117 for (; *p != ':' && *p != '\0'; p++);
125 for (; *p != '.' && *p != '\0'; p++);
130 /* Do not use screennumber
135 if (hostname[0] == '\0')
138 p = g_strdup_printf ("%s-%s-%s",
139 ibus_get_local_machine_id (),
142 path = g_build_filename (g_get_user_config_dir (),
154 ibus_get_timeout (void)
156 /* 6000 ms is the default timeout on the ibus-daemon side (5 sec) plus 1. */
157 static const gint default_timeout = 6000;
159 static gint64 timeout = -2;
161 const gchar *timeout_str = g_getenv ("IBUS_TIMEOUT");
162 if (timeout_str == NULL) {
163 timeout = default_timeout;
165 timeout = g_ascii_strtoll(timeout_str, NULL, 10);
166 if (timeout < -1 || timeout == 0 || timeout > G_MAXINT) {
167 timeout = default_timeout;
175 ibus_get_address (void)
177 static gchar *address = NULL;
179 static gchar buffer[1024];
183 if (address != NULL) {
188 /* get address from env variable */
189 address = g_strdup (g_getenv ("IBUS_ADDRESS"));
194 /* read address from ~/.config/ibus/bus/soketfile */
195 pf = fopen (ibus_get_socket_path (), "r");
202 if (fgets (buffer, sizeof (buffer), pf) == NULL)
205 /* skip comment line */
208 /* parse IBUS_ADDRESS */
209 if (strncmp (p, "IBUS_ADDRESS=", sizeof ("IBUS_ADDRESS=") - 1) == 0) {
210 address = p + sizeof ("IBUS_ADDRESS=") - 1;
211 for (p = (gchar *)address; *p != '\n' && *p != '\0'; p++);
214 address = g_strdup (address);
218 /* parse IBUS_DAEMON_PID */
219 if (strncmp (p, "IBUS_DAEMON_PID=", sizeof ("IBUS_DAEMON_PID=") - 1) == 0) {
220 pid = atoi(p + sizeof ("IBUS_DAEMON_PID=") - 1);
227 if (pid == -1 || kill (pid, 0) != 0) {
235 ibus_write_address (const gchar *address)
239 g_return_if_fail (address != NULL);
241 path = g_path_get_dirname (ibus_get_socket_path ());
242 g_mkdir_with_parents (path, 0700);
245 g_unlink (ibus_get_socket_path ());
246 pf = fopen (ibus_get_socket_path (), "w");
247 g_return_if_fail (pf != NULL);
250 "# This file is created by ibus-daemon, please do not modify it\n"
252 "IBUS_DAEMON_PID=%ld\n",
253 address, (glong) getpid ());
258 ibus_free_strv (gchar **strv)
265 for (p = strv; *p != NULL; p++) {
275 #if !GLIB_CHECK_VERSION(2,35,0)
282 IBUS_TYPE_LOOKUP_TABLE;
284 IBUS_TYPE_ENGINE_DESC;
285 IBUS_TYPE_OBSERVED_PATH;
289 static GMainLoop *main_loop = NULL;
294 main_loop = g_main_loop_new (NULL, FALSE);
296 g_main_loop_run (main_loop);
298 g_main_loop_unref (main_loop);
306 g_main_loop_quit (main_loop);
310 static gboolean ibus_log_handler_is_verbose = FALSE;
311 static guint ibus_log_handler_id = 0;
314 ibus_log_handler (const gchar *log_domain,
315 GLogLevelFlags log_level,
316 const gchar *message,
319 // In the quiet mode (i.e. not verbose), we'll ignore DEBUG and
321 if (!ibus_log_handler_is_verbose &&
322 ((log_level & G_LOG_LEVEL_DEBUG) ||
323 (log_level & G_LOG_LEVEL_WARNING))) {
326 // Add timing info like "17:34:57.680038" (hour, min, sec, microsecond).
327 struct timeval time_val;
328 gettimeofday (&time_val, NULL);
329 struct tm local_time;
330 localtime_r (&time_val.tv_sec, &local_time);
332 g_strdup_printf ("%02d:%02d:%02d.%6d: %s",
336 (int)time_val.tv_usec,
338 g_log_default_handler (log_domain, log_level, new_message, user_data);
339 g_free (new_message);
343 ibus_set_log_handler (gboolean verbose)
345 if (ibus_log_handler_id != 0) {
346 g_log_remove_handler (G_LOG_DOMAIN, ibus_log_handler_id);
349 ibus_log_handler_is_verbose = verbose;
350 ibus_log_handler_id = g_log_set_handler (G_LOG_DOMAIN,
357 ibus_unset_log_handler (void)
359 if (ibus_log_handler_id != 0) {
360 g_log_remove_handler (G_LOG_DOMAIN, ibus_log_handler_id);
361 ibus_log_handler_id = 0;