2003-08-18 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / tools / dbus-monitor.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-monitor.c  Utility program to monitor messages on the bus
3  *
4  * Copyright (C) 2003 Philip Blundell <philb@gnu.org>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <glib.h>
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"
31
32 static DBusHandlerResult
33 handler_func (DBusMessageHandler *handler,
34               DBusConnection     *connection,
35               DBusMessage        *message,
36               void               *user_data)
37 {
38   print_message (message);
39   
40   if (dbus_message_is_signal (message,
41                               DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
42                               "Disconnected"))
43     exit (0);
44   
45   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
46 }
47
48 static void
49 usage (char *name, int ecode)
50 {
51   fprintf (stderr, "Usage: %s [--system | --session]\n", name);
52   exit (ecode);
53 }
54
55 int
56 main (int argc, char *argv[])
57 {
58   DBusConnection *connection;
59   DBusError error;
60   DBusBusType type = DBUS_BUS_SESSION;
61   DBusMessageHandler *handler;
62   GMainLoop *loop;
63   int i;
64
65   for (i = 1; i < argc; i++)
66     {
67       char *arg = argv[i];
68
69       if (!strcmp (arg, "--system"))
70         type = DBUS_BUS_SYSTEM;
71       else if (!strcmp (arg, "--session"))
72         type = DBUS_BUS_SESSION;
73       else if (!strcmp (arg, "--help"))
74         usage (argv[0], 0);
75       else if (!strcmp (arg, "--"))
76         break;
77       else if (arg[0] == '-')
78         usage (argv[0], 1);
79     }
80
81   if (argc > 2)
82     usage (argv[0], 1);
83
84   loop = g_main_loop_new (NULL, FALSE);
85
86   dbus_error_init (&error);
87   connection = dbus_bus_get (type, &error);
88   if (connection == NULL)
89     {
90       fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
91                (type == DBUS_BUS_SYSTEM) ? "system" : "session",
92                error.message);
93       dbus_error_free (&error);
94       exit (1);
95     }
96
97   dbus_connection_setup_with_g_main (connection, NULL);
98
99   handler = dbus_message_handler_new (handler_func, NULL, NULL);
100   dbus_connection_add_filter (connection, handler);
101
102   g_main_loop_run (loop);
103
104   exit (0);
105 }