1 /* vim:set et sts=4: */
2 /* ibus - The Input Bus
3 * Copyright (C) 2008-2009 Huang Peng <shawn.p.huang@gmail.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 #include <sys/types.h>
29 gchar **g_argv = NULL;
31 static gboolean daemonize = FALSE;
32 static gboolean single = FALSE;
33 static gboolean xim = FALSE;
34 static gboolean replace = FALSE;
35 static gchar *panel = "default";
36 static gchar *config = "default";
37 static gchar *desktop = "gnome";
38 static gchar *address = "";
39 gboolean g_rescan = FALSE;
40 static gboolean verbose = FALSE;
42 static const GOptionEntry entries[] =
44 { "daemonize", 'd', 0, G_OPTION_ARG_NONE, &daemonize, "run ibus as background process.", NULL },
45 { "single", 's', 0, G_OPTION_ARG_NONE, &single, "do not execute panel and config module.", NULL },
46 { "xim", 'x', 0, G_OPTION_ARG_NONE, &xim, "execute ibus XIM server.", NULL },
47 { "desktop", 'n', 0, G_OPTION_ARG_STRING, &desktop, "specify the name of desktop session. [default=gnome]", "name" },
48 { "panel", 'p', 0, G_OPTION_ARG_STRING, &panel, "specify the cmdline of panel program.", "cmdline" },
49 { "config", 'c', 0, G_OPTION_ARG_STRING, &config, "specify the cmdline of config program.", "cmdline" },
50 { "address", 'a', 0, G_OPTION_ARG_STRING, &address, "specify the address of ibus daemon.", "address" },
51 { "replace", 'r', 0, G_OPTION_ARG_NONE, &replace, "if there is an old ibus-daemon is running, it will be replaced.", NULL },
52 { "re-scan", 't', 0, G_OPTION_ARG_NONE, &g_rescan, "force to re-scan components, and re-create registry cache.", NULL },
53 { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "verbose.", NULL },
58 execute_cmdline (const gchar *cmdline)
68 if (!g_shell_parse_argv (cmdline, &argc, &argv, &error)) {
69 g_warning ("Can not parse cmdline `%s` exec: %s", cmdline, error->message);
75 retval = g_spawn_async (NULL, argv, NULL,
76 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
82 g_warning ("Can not execute cmdline `%s`: %s", cmdline, error->message);
91 main (gint argc, gchar **argv)
93 GOptionContext *context;
99 setlocale (LC_ALL, "");
101 context = g_option_context_new ("- ibus daemon");
103 g_option_context_add_main_entries (context, entries, "ibus-daemon");
105 g_argv = g_strdupv (argv);
106 if (!g_option_context_parse (context, &argc, &argv, &error)) {
107 g_printerr ("Option parsing failed: %s\n", error->message);
113 gchar *username = ibus_get_user_name ();
114 uid_t uid = getuid ();
115 struct passwd *pwd = getpwuid (uid);
117 if (pwd == NULL || strcmp (pwd->pw_name, username) != 0) {
118 g_printerr ("Please run ibus-daemon with login user! Do not run ibus-daemon with sudo or su.\n");
123 /* daemonize process */
125 if (daemon (1, 0) != 0) {
126 g_printerr ("Can not daemonize ibus.\n");
131 /* create a new process group */
136 /* check if ibus-daemon is running in this session */
137 bus = ibus_bus_new ();
139 if (ibus_bus_is_connected (bus)) {
141 g_printerr ("current session already has an ibus-daemon.\n");
144 ibus_bus_exit (bus, FALSE);
145 while (ibus_bus_is_connected (bus)) {
146 g_main_context_iteration (NULL, TRUE);
149 g_object_unref (bus);
152 /* create ibus server */
153 server = bus_server_get_default ();
154 bus_server_listen (server);
157 /* execute config component */
158 if (g_strcmp0 (config, "default") == 0) {
159 IBusComponent *component;
160 component = bus_registry_lookup_component_by_name (BUS_DEFAULT_REGISTRY, IBUS_SERVICE_CONFIG);
161 if (component == NULL || !ibus_component_start (component)) {
162 g_printerr ("Can not execute default config program\n");
165 } else if (g_strcmp0 (config, "disable") != 0 && g_strcmp0 (config, "") != 0) {
166 if (!execute_cmdline (config))
170 /* execut panel component */
171 if (g_strcmp0 (panel, "default") == 0) {
172 IBusComponent *component;
173 component = bus_registry_lookup_component_by_name (BUS_DEFAULT_REGISTRY, IBUS_SERVICE_PANEL);
174 if (component == NULL || !ibus_component_start (component)) {
175 g_printerr ("Can not execute default panel program\n");
178 } else if (g_strcmp0 (panel, "disable") != 0 && g_strcmp0 (panel, "") != 0) {
179 if (!execute_cmdline (panel))
184 /* execute ibus xim server */
186 if (!execute_cmdline (LIBEXECDIR"/ibus-x11 --kill-daemon"))
190 bus_server_run (server);