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>
30 #include "dbus-print-message.h"
32 static DBusHandlerResult
33 handler_func (DBusMessageHandler *handler,
34 DBusConnection *connection,
38 print_message (message);
40 if (dbus_message_has_name (message, DBUS_MESSAGE_LOCAL_DISCONNECT))
43 return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
47 usage (char *name, int ecode)
49 fprintf (stderr, "Usage: %s [--session]\n", name);
54 main (int argc, char *argv[])
56 DBusConnection *connection;
58 DBusBusType type = DBUS_BUS_SYSTEM;
59 DBusMessageHandler *handler;
63 for (i = 1; i < argc; i++)
67 if (!strcmp (arg, "--session"))
68 type = DBUS_BUS_SESSION;
69 else if (!strcmp (arg, "--help"))
71 else if (!strcmp (arg, "--"))
73 else if (arg[0] == '-')
80 loop = g_main_loop_new (NULL, FALSE);
82 dbus_error_init (&error);
83 connection = dbus_bus_get (type, &error);
84 if (connection == NULL)
86 fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
87 (type == DBUS_BUS_SYSTEM) ? "system" : "session",
89 dbus_error_free (&error);
93 dbus_connection_setup_with_g_main (connection, NULL);
95 handler = dbus_message_handler_new (handler_func, NULL, NULL);
96 dbus_connection_add_filter (connection, handler);
98 g_main_loop_run (loop);