2003-05-16 Colin Walters <walters@verbum.org>
[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
31 static DBusHandlerResult
32 handler_func (DBusMessageHandler *handler,
33               DBusConnection     *connection,
34               DBusMessage        *message,
35               void               *user_data)
36 {
37   DBusMessageIter iter;
38
39   printf ("message name=%s; sender=%s\n", dbus_message_get_name (message),
40           dbus_message_get_sender (message));
41
42   dbus_message_iter_init (message, &iter);
43
44   do
45     {
46       int type = dbus_message_iter_get_arg_type (&iter);
47       char *str;
48       dbus_uint32_t uint32;
49       dbus_int32_t int32;
50       double d;
51       unsigned char byte;
52
53       if (type == DBUS_TYPE_INVALID)
54         break;
55
56       switch (type)
57         {
58         case DBUS_TYPE_STRING:
59           str = dbus_message_iter_get_string (&iter);
60           printf ("string:%s\n", str);
61           break;
62
63         case DBUS_TYPE_INT32:
64           int32 = dbus_message_iter_get_int32 (&iter);
65           printf ("int32:%d\n", int32);
66           break;
67
68         case DBUS_TYPE_UINT32:
69           uint32 = dbus_message_iter_get_uint32 (&iter);
70           printf ("int32:%u\n", uint32);
71           break;
72
73         case DBUS_TYPE_DOUBLE:
74           d = dbus_message_iter_get_double (&iter);
75           printf ("double:%f\n", d);
76           break;
77
78         case DBUS_TYPE_BYTE:
79           byte = dbus_message_iter_get_byte (&iter);
80           printf ("byte:%d\n", byte);
81           break;
82
83         default:
84           printf ("(unknown arg type %d)\n", type);
85           break;
86         }
87     } while (dbus_message_iter_next (&iter));
88
89   return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
90 }
91
92 static void
93 usage (char *name, int ecode)
94 {
95   fprintf (stderr, "Usage: %s [--session]\n", name);
96   exit (ecode);
97 }
98
99 int
100 main (int argc, char *argv[])
101 {
102   DBusConnection *connection;
103   DBusError error;
104   DBusBusType type = DBUS_BUS_SYSTEM;
105   DBusMessageHandler *handler;
106   GMainLoop *loop;
107   int i;
108
109   for (i = 1; i < argc; i++)
110     {
111       char *arg = argv[i];
112
113       if (!strcmp (arg, "--session"))
114         type = DBUS_BUS_SESSION;
115       else if (!strcmp (arg, "--help"))
116         usage (argv[0], 0);
117       else if (!strcmp (arg, "--"))
118         break;
119       else if (arg[0] == '-')
120         usage (argv[0], 1);
121     }
122
123   if (argc > 2)
124     usage (argv[0], 1);
125
126   loop = g_main_loop_new (NULL, FALSE);
127
128   dbus_error_init (&error);
129   connection = dbus_bus_get (type, &error);
130   if (connection == NULL)
131     {
132       fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
133                (type == DBUS_BUS_SYSTEM) ? "system" : "session",
134                error.message);
135       dbus_error_free (&error);
136       exit (1);
137     }
138
139   dbus_connection_setup_with_g_main (connection, NULL);
140
141   handler = dbus_message_handler_new (handler_func, NULL, NULL);
142   dbus_connection_add_filter (connection, handler);
143
144   g_main_loop_run (loop);
145
146   exit (0);
147 }