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_warning ("Unable to load /var/lib/dbus/machine-id: %s", error->message);
50 machine_id = "machine-id";
53 g_strstrip (machine_id);
61 ibus_set_display (const gchar *display)
65 _display = g_strdup (display);
69 ibus_get_user_name (void)
71 return g_get_user_name ();
75 ibus_get_daemon_uid (void)
81 ibus_get_session_id (void)
83 return g_getenv("IBUS_SESSION_ID");
87 ibus_get_socket_path (void)
89 static gchar *path = NULL;
92 gchar *hostname = "unix";
94 gchar *displaynumber = "0";
95 /* gchar *screennumber = "0"; */
98 path = g_strdup (g_getenv ("IBUS_ADDRESS_FILE"));
103 if (_display == NULL) {
104 display = g_strdup (g_getenv ("DISPLAY"));
107 display = g_strdup (_display);
110 if (display == NULL) {
111 g_warning ("DISPLAY is empty! We use default DISPLAY (:0.0)");
116 for (; *p != ':' && *p != '\0'; p++);
124 for (; *p != '.' && *p != '\0'; p++);
129 /* Do not use screennumber
134 if (hostname[0] == '\0')
137 p = g_strdup_printf ("%s-%s-%s",
138 ibus_get_local_machine_id (),
141 path = g_build_filename (g_get_user_config_dir (),
153 ibus_get_timeout (void)
155 /* 6000 ms is the default timeout on the ibus-daemon side (5 sec) plus 1. */
156 static const gint default_timeout = 6000;
158 static gint64 timeout = -2;
160 const gchar *timeout_str = g_getenv ("IBUS_TIMEOUT");
161 if (timeout_str == NULL) {
162 timeout = default_timeout;
164 timeout = g_ascii_strtoll(timeout_str, NULL, 10);
165 if (timeout < -1 || timeout == 0 || timeout > G_MAXINT) {
166 timeout = default_timeout;
174 ibus_get_address (void)
176 static gchar *address = NULL;
178 static gchar buffer[1024];
182 if (address != NULL) {
187 /* get address from env variable */
188 address = g_strdup (g_getenv ("IBUS_ADDRESS"));
193 /* read address from ~/.config/ibus/bus/soketfile */
194 pf = fopen (ibus_get_socket_path (), "r");
201 if (fgets (buffer, sizeof (buffer), pf) == NULL)
204 /* skip comment line */
207 /* parse IBUS_ADDRESS */
208 if (strncmp (p, "IBUS_ADDRESS=", sizeof ("IBUS_ADDRESS=") - 1) == 0) {
209 address = p + sizeof ("IBUS_ADDRESS=") - 1;
210 for (p = (gchar *)address; *p != '\n' && *p != '\0'; p++);
213 address = g_strdup (address);
217 /* parse IBUS_DAEMON_PID */
218 if (strncmp (p, "IBUS_DAEMON_PID=", sizeof ("IBUS_DAEMON_PID=") - 1) == 0) {
219 pid = atoi(p + sizeof ("IBUS_DAEMON_PID=") - 1);
226 if (pid == -1 || kill (pid, 0) != 0) {
234 ibus_write_address (const gchar *address)
238 g_return_if_fail (address != NULL);
240 path = g_path_get_dirname (ibus_get_socket_path ());
241 g_mkdir_with_parents (path, 0700);
244 g_unlink (ibus_get_socket_path ());
245 pf = fopen (ibus_get_socket_path (), "w");
246 g_return_if_fail (pf != NULL);
249 "# This file is created by ibus-daemon, please do not modify it\n"
251 "IBUS_DAEMON_PID=%ld\n",
252 address, (glong) getpid ());
257 ibus_free_strv (gchar **strv)
264 for (p = strv; *p != NULL; p++) {
279 IBUS_TYPE_LOOKUP_TABLE;
281 IBUS_TYPE_ENGINE_DESC;
284 static GMainLoop *main_loop = NULL;
289 main_loop = g_main_loop_new (NULL, FALSE);
291 g_main_loop_run (main_loop);
293 g_main_loop_unref (main_loop);
301 g_main_loop_quit (main_loop);
305 static gboolean ibus_log_handler_is_verbose = FALSE;
306 static guint ibus_log_handler_id = 0;
309 ibus_log_handler (const gchar *log_domain,
310 GLogLevelFlags log_level,
311 const gchar *message,
314 // In the quiet mode (i.e. not verbose), we'll ignore DEBUG and
316 if (!ibus_log_handler_is_verbose &&
317 ((log_level & G_LOG_LEVEL_DEBUG) ||
318 (log_level & G_LOG_LEVEL_WARNING))) {
321 // Add timing info like "17:34:57.680038" (hour, min, sec, microsecond).
322 struct timeval time_val;
323 gettimeofday (&time_val, NULL);
324 struct tm local_time;
325 localtime_r (&time_val.tv_sec, &local_time);
327 g_strdup_printf ("%02d:%02d:%02d.%6d: %s",
331 (int)time_val.tv_usec,
333 g_log_default_handler (log_domain, log_level, new_message, user_data);
334 g_free (new_message);
338 ibus_set_log_handler (gboolean verbose)
340 if (ibus_log_handler_id != 0) {
341 g_log_remove_handler (G_LOG_DOMAIN, ibus_log_handler_id);
344 ibus_log_handler_is_verbose = verbose;
345 ibus_log_handler_id = g_log_set_handler (G_LOG_DOMAIN,
352 ibus_unset_log_handler (void)
354 if (ibus_log_handler_id != 0) {
355 g_log_remove_handler (G_LOG_DOMAIN, ibus_log_handler_id);
356 ibus_log_handler_id = 0;