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 ();
73 static gchar *username = NULL;
74 if (username == NULL) {
75 username = g_strdup (getlogin());
77 username = g_strdup (g_getenv("SUDO_USER"));
78 if (username == NULL) {
79 const gchar *uid = g_getenv ("USERHELPER_UID");
82 uid_t id = (uid_t)strtol(uid, &end, 10);
84 struct passwd *pw = getpwuid (id);
86 username = g_strdup (pw->pw_name);
92 username = g_strdup (g_getenv("USERNAME"));
94 username = g_strdup (g_getenv("LOGNAME"));
96 username = g_strdup (g_getenv("USER"));
98 username = g_strdup (g_getenv("LNAME"));
106 ibus_get_daemon_uid (void)
112 const gchar *username;
119 username = ibus_get_user_name ();
120 if (username == NULL)
123 pwd = getpwnam (username);
132 ibus_get_session_id (void)
134 return g_getenv("IBUS_SESSION_ID");
138 ibus_get_socket_path (void)
140 static gchar *path = NULL;
143 gchar *hostname = "unix";
145 gchar *displaynumber = "0";
146 /* gchar *screennumber = "0"; */
149 path = g_strdup (g_getenv ("IBUS_ADDRESS_FILE"));
154 if (_display == NULL) {
155 display = g_strdup (g_getenv ("DISPLAY"));
158 display = g_strdup (_display);
161 if (display == NULL) {
162 g_warning ("DISPLAY is empty! We use default DISPLAY (:0.0)");
167 for (; *p != ':' && *p != '\0'; p++);
175 for (; *p != '.' && *p != '\0'; p++);
180 /* Do not use screennumber
185 if (hostname[0] == '\0')
188 p = g_strdup_printf ("%s-%s-%s",
189 ibus_get_local_machine_id (),
192 path = g_build_filename (g_get_user_config_dir (),
204 ibus_get_timeout (void)
206 /* 6000 ms is the default timeout on the ibus-daemon side (5 sec) plus 1. */
207 static const gint default_timeout = 6000;
209 static gint64 timeout = -2;
211 const gchar *timeout_str = g_getenv ("IBUS_TIMEOUT");
212 if (timeout_str == NULL) {
213 timeout = default_timeout;
215 timeout = g_ascii_strtoll(timeout_str, NULL, 10);
216 if (timeout < -1 || timeout == 0 || timeout > G_MAXINT) {
217 timeout = default_timeout;
225 ibus_get_address (void)
227 static gchar *address = NULL;
229 static gchar buffer[1024];
233 if (address != NULL) {
238 /* get address from env variable */
239 address = g_strdup (g_getenv ("IBUS_ADDRESS"));
244 /* read address from ~/.config/ibus/bus/soketfile */
245 pf = fopen (ibus_get_socket_path (), "r");
252 if (fgets (buffer, sizeof (buffer), pf) == NULL)
255 /* skip comment line */
258 /* parse IBUS_ADDRESS */
259 if (strncmp (p, "IBUS_ADDRESS=", sizeof ("IBUS_ADDRESS=") - 1) == 0) {
260 address = p + sizeof ("IBUS_ADDRESS=") - 1;
261 for (p = (gchar *)address; *p != '\n' && *p != '\0'; p++);
264 address = g_strdup (address);
268 /* parse IBUS_DAEMON_PID */
269 if (strncmp (p, "IBUS_DAEMON_PID=", sizeof ("IBUS_DAEMON_PID=") - 1) == 0) {
270 pid = atoi(p + sizeof ("IBUS_DAEMON_PID=") - 1);
277 if (pid == -1 || kill (pid, 0) != 0) {
285 ibus_write_address (const gchar *address)
289 g_return_if_fail (address != NULL);
291 path = g_path_get_dirname (ibus_get_socket_path ());
292 g_mkdir_with_parents (path, 0700);
295 g_unlink (ibus_get_socket_path ());
296 pf = fopen (ibus_get_socket_path (), "w");
297 g_return_if_fail (pf != NULL);
300 "# This file is created by ibus-daemon, please do not modify it\n"
302 "IBUS_DAEMON_PID=%ld\n",
303 address, (glong) getpid ());
308 ibus_free_strv (gchar **strv)
315 for (p = strv; *p != NULL; p++) {
330 IBUS_TYPE_LOOKUP_TABLE;
332 IBUS_TYPE_ENGINE_DESC;
335 static GMainLoop *main_loop = NULL;
340 main_loop = g_main_loop_new (NULL, FALSE);
342 g_main_loop_run (main_loop);
344 g_main_loop_unref (main_loop);
352 g_main_loop_quit (main_loop);
356 static gboolean ibus_log_handler_is_verbose = FALSE;
357 static guint ibus_log_handler_id = 0;
360 ibus_log_handler (const gchar *log_domain,
361 GLogLevelFlags log_level,
362 const gchar *message,
365 // In the quiet mode (i.e. not verbose), we'll ignore DEBUG and
367 if (!ibus_log_handler_is_verbose &&
368 ((log_level & G_LOG_LEVEL_DEBUG) ||
369 (log_level & G_LOG_LEVEL_WARNING))) {
372 // Add timing info like "17:34:57.680038" (hour, min, sec, microsecond).
373 struct timeval time_val;
374 gettimeofday (&time_val, NULL);
375 struct tm local_time;
376 localtime_r (&time_val.tv_sec, &local_time);
378 g_strdup_printf ("%02d:%02d:%02d.%6d: %s",
382 (int)time_val.tv_usec,
384 g_log_default_handler (log_domain, log_level, new_message, user_data);
385 g_free (new_message);
389 ibus_set_log_handler (gboolean verbose)
391 if (ibus_log_handler_id != 0) {
392 g_log_remove_handler (G_LOG_DOMAIN, ibus_log_handler_id);
395 ibus_log_handler_is_verbose = verbose;
396 ibus_log_handler_id = g_log_set_handler (G_LOG_DOMAIN,
403 ibus_unset_log_handler (void)
405 if (ibus_log_handler_id != 0) {
406 g_log_remove_handler (G_LOG_DOMAIN, ibus_log_handler_id);
407 ibus_log_handler_id = 0;