Add command-line option to specify D-Bus type and address.

This commit is contained in:
Daiki Ueno
2011-02-25 11:10:36 +09:00
parent 40862fcf12
commit 1c3d20ad6f
3 changed files with 152 additions and 27 deletions

View File

@ -31,6 +31,20 @@
#include "server-server.h"
#include "eek/eek.h"
static gboolean opt_system = FALSE;
static gboolean opt_session = FALSE;
static gchar *opt_address = NULL;
static const GOptionEntry options[] = {
{"system", 'y', 0, G_OPTION_ARG_NONE, &opt_system,
N_("Connect to the system bus")},
{"session", 'e', 0, G_OPTION_ARG_NONE, &opt_session,
N_("Connect to the session bus")},
{"address", 'a', 0, G_OPTION_ARG_STRING, &opt_address,
N_("Connect to the given D-Bus address")},
{NULL}
};
static void
on_name_acquired (GDBusConnection *connection,
const gchar *name,
@ -50,6 +64,7 @@ int
main (int argc, char **argv)
{
ServerServer *server;
GBusType bus_type;
GDBusConnection *connection;
GError *error;
GMainLoop *loop;
@ -75,17 +90,46 @@ main (int argc, char **argv)
g_type_class_ref (EEK_TYPE_SYMBOL);
g_type_class_ref (EEK_TYPE_KEYSYM);
error = NULL;
connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
if (error) {
g_printerr ("%s\n", error->message);
exit (1);
if (opt_system)
bus_type = G_BUS_TYPE_SYSTEM;
else if (opt_address)
bus_type = G_BUS_TYPE_NONE;
else
bus_type = G_BUS_TYPE_SESSION;
switch (bus_type) {
case G_BUS_TYPE_SYSTEM:
case G_BUS_TYPE_SESSION:
error = NULL;
connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
if (connection == NULL) {
g_printerr ("Can't connect to the bus: %s\n", error->message);
exit (1);
}
break;
case G_BUS_TYPE_NONE:
error = NULL;
connection = g_dbus_connection_new_for_address_sync (opt_address,
0,
NULL,
NULL,
&error);
if (connection == NULL) {
g_printerr ("Can't connect to the bus at %s: %s\n",
opt_address,
error->message);
exit (1);
}
break;
default:
g_assert_not_reached ();
break;
}
server = server_server_new (SERVER_SERVER_PATH, connection);
if (server == NULL) {
g_printerr ("Can't create server server\n");
g_printerr ("Can't create server\n");
exit (1);
}