2004-10-29 Colin Walters <walters@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-glib-lowlevel.h>
28 #include "dbus-print-message.h"
29
30 static DBusHandlerResult
31 filter_func (DBusConnection     *connection,
32              DBusMessage        *message,
33              void               *user_data)
34 {
35   print_message (message);
36   
37   if (dbus_message_is_signal (message,
38                               DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
39                               "Disconnected"))
40     exit (0);
41   
42   return DBUS_HANDLER_RESULT_HANDLED;
43 }
44
45 static void
46 usage (char *name, int ecode)
47 {
48   fprintf (stderr, "Usage: %s [--system | --session]\n", name);
49   exit (ecode);
50 }
51
52 int
53 main (int argc, char *argv[])
54 {
55   DBusConnection *connection;
56   DBusError error;
57   DBusBusType type = DBUS_BUS_SESSION;
58   GMainLoop *loop;
59   int i;
60
61   for (i = 1; i < argc; i++)
62     {
63       char *arg = argv[i];
64
65       if (!strcmp (arg, "--system"))
66         type = DBUS_BUS_SYSTEM;
67       else if (!strcmp (arg, "--session"))
68         type = DBUS_BUS_SESSION;
69       else if (!strcmp (arg, "--help"))
70         usage (argv[0], 0);
71       else if (!strcmp (arg, "--"))
72         break;
73       else if (arg[0] == '-')
74         usage (argv[0], 1);
75     }
76
77   if (argc > 2)
78     usage (argv[0], 1);
79
80   loop = g_main_loop_new (NULL, FALSE);
81
82   dbus_error_init (&error);
83   connection = dbus_bus_get (type, &error);
84   if (connection == NULL)
85     {
86       fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
87                (type == DBUS_BUS_SYSTEM) ? "system" : "session",
88                error.message);
89       dbus_error_free (&error);
90       exit (1);
91     }
92
93   dbus_connection_setup_with_g_main (connection, NULL);
94
95   dbus_bus_add_match (connection,
96                       "type='signal'",
97                       &error);
98   if (dbus_error_is_set (&error))
99     goto lose;
100   dbus_bus_add_match (connection,
101                       "type='method_call'",
102                       &error);
103   if (dbus_error_is_set (&error))
104     goto lose;
105   dbus_bus_add_match (connection,
106                       "type='method_return'",
107                       &error);
108   if (dbus_error_is_set (&error))
109     goto lose;
110   dbus_bus_add_match (connection,
111                       "type='error'",
112                       &error);
113   if (dbus_error_is_set (&error))
114     goto lose;
115   if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) {
116     fprintf (stderr, "Couldn't add filter!\n");
117     exit (1);
118   }
119
120   g_main_loop_run (loop);
121
122   exit (0);
123  lose:
124   fprintf (stderr, "Error: %s\n", error.message);
125   exit (1);
126 }