1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-monitor.c Utility program to monitor messages on the bus
4 * Copyright (C) 2003 Philip Blundell <philb@gnu.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <dbus/dbus.h>
28 /* Don't copy this, for programs outside the dbus tree it's dbus/dbus-glib.h */
29 #include <glib/dbus-glib.h>
31 static DBusHandlerResult
32 handler_func (DBusMessageHandler *handler,
33 DBusConnection *connection,
39 printf ("message name=%s; sender=%s\n", dbus_message_get_name (message),
40 dbus_message_get_sender (message));
42 dbus_message_iter_init (message, &iter);
46 int type = dbus_message_iter_get_arg_type (&iter);
53 if (type == DBUS_TYPE_INVALID)
58 case DBUS_TYPE_STRING:
59 str = dbus_message_iter_get_string (&iter);
60 printf ("string:%s\n", str);
64 int32 = dbus_message_iter_get_int32 (&iter);
65 printf ("int32:%d\n", int32);
68 case DBUS_TYPE_UINT32:
69 uint32 = dbus_message_iter_get_uint32 (&iter);
70 printf ("int32:%u\n", uint32);
73 case DBUS_TYPE_DOUBLE:
74 d = dbus_message_iter_get_double (&iter);
75 printf ("double:%f\n", d);
79 byte = dbus_message_iter_get_byte (&iter);
80 printf ("byte:%d\n", byte);
84 printf ("(unknown arg type %d)\n", type);
87 } while (dbus_message_iter_next (&iter));
89 return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
93 usage (char *name, int ecode)
95 fprintf (stderr, "Usage: %s [--session]\n", name);
100 main (int argc, char *argv[])
102 DBusConnection *connection;
104 DBusBusType type = DBUS_BUS_SYSTEM;
105 DBusMessageHandler *handler;
109 for (i = 1; i < argc; i++)
113 if (!strcmp (arg, "--session"))
114 type = DBUS_BUS_SESSION;
115 else if (!strcmp (arg, "--help"))
117 else if (!strcmp (arg, "--"))
119 else if (arg[0] == '-')
126 loop = g_main_loop_new (NULL, FALSE);
128 dbus_error_init (&error);
129 connection = dbus_bus_get (type, &error);
130 if (connection == NULL)
132 fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
133 (type == DBUS_BUS_SYSTEM) ? "system" : "session",
135 dbus_error_free (&error);
139 dbus_connection_setup_with_g_main (connection, NULL);
141 handler = dbus_message_handler_new (handler_func, NULL, NULL);
142 dbus_connection_add_filter (connection, handler);
144 g_main_loop_run (loop);