2003-04-14 03:25:19 +00:00
|
|
|
/* -*- mode: C; c-file-style: "gnu" -*- */
|
|
|
|
|
/* dbus-monitor.c Utility program to monitor messages on the bus
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2003 Philip Blundell <philb@gnu.org>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include <glib.h>
|
2004-06-20 15:28:15 +00:00
|
|
|
#include <dbus/dbus-glib-lowlevel.h>
|
2003-05-16 20:09:25 +00:00
|
|
|
#include "dbus-print-message.h"
|
2003-04-14 03:25:19 +00:00
|
|
|
|
|
|
|
|
static DBusHandlerResult
|
2003-08-31 03:25:24 +00:00
|
|
|
filter_func (DBusConnection *connection,
|
|
|
|
|
DBusMessage *message,
|
|
|
|
|
void *user_data)
|
2003-04-14 03:25:19 +00:00
|
|
|
{
|
2003-05-16 20:09:25 +00:00
|
|
|
print_message (message);
|
|
|
|
|
|
2003-08-18 22:43:30 +00:00
|
|
|
if (dbus_message_is_signal (message,
|
|
|
|
|
DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
|
|
|
|
|
"Disconnected"))
|
2003-05-16 20:09:25 +00:00
|
|
|
exit (0);
|
|
|
|
|
|
2004-10-29 18:52:30 +00:00
|
|
|
/* Conceptually we want this to be
|
|
|
|
|
* DBUS_HANDLER_RESULT_NOT_YET_HANDLED, but this raises
|
|
|
|
|
* some problems. See bug 1719.
|
|
|
|
|
*/
|
2004-10-29 18:50:29 +00:00
|
|
|
return DBUS_HANDLER_RESULT_HANDLED;
|
2003-04-14 03:25:19 +00:00
|
|
|
}
|
|
|
|
|
|
2003-05-16 16:01:56 +00:00
|
|
|
static void
|
|
|
|
|
usage (char *name, int ecode)
|
|
|
|
|
{
|
2003-06-19 22:22:37 +00:00
|
|
|
fprintf (stderr, "Usage: %s [--system | --session]\n", name);
|
2003-05-16 16:01:56 +00:00
|
|
|
exit (ecode);
|
|
|
|
|
}
|
|
|
|
|
|
2003-04-14 03:25:19 +00:00
|
|
|
int
|
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
DBusConnection *connection;
|
|
|
|
|
DBusError error;
|
2003-05-18 02:39:47 +00:00
|
|
|
DBusBusType type = DBUS_BUS_SESSION;
|
2003-04-14 03:25:19 +00:00
|
|
|
GMainLoop *loop;
|
2003-05-16 16:01:56 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
|
{
|
|
|
|
|
char *arg = argv[i];
|
|
|
|
|
|
2003-05-28 21:26:03 +00:00
|
|
|
if (!strcmp (arg, "--system"))
|
2003-05-18 02:39:47 +00:00
|
|
|
type = DBUS_BUS_SYSTEM;
|
2003-06-19 22:22:37 +00:00
|
|
|
else if (!strcmp (arg, "--session"))
|
|
|
|
|
type = DBUS_BUS_SESSION;
|
2003-05-16 16:01:56 +00:00
|
|
|
else if (!strcmp (arg, "--help"))
|
|
|
|
|
usage (argv[0], 0);
|
|
|
|
|
else if (!strcmp (arg, "--"))
|
|
|
|
|
break;
|
|
|
|
|
else if (arg[0] == '-')
|
|
|
|
|
usage (argv[0], 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (argc > 2)
|
|
|
|
|
usage (argv[0], 1);
|
2003-04-14 03:25:19 +00:00
|
|
|
|
|
|
|
|
loop = g_main_loop_new (NULL, FALSE);
|
|
|
|
|
|
|
|
|
|
dbus_error_init (&error);
|
|
|
|
|
connection = dbus_bus_get (type, &error);
|
|
|
|
|
if (connection == NULL)
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
|
|
|
|
|
(type == DBUS_BUS_SYSTEM) ? "system" : "session",
|
|
|
|
|
error.message);
|
|
|
|
|
dbus_error_free (&error);
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
2003-04-24 02:22:49 +00:00
|
|
|
dbus_connection_setup_with_g_main (connection, NULL);
|
2003-04-14 03:25:19 +00:00
|
|
|
|
2004-05-25 18:02:17 +00:00
|
|
|
dbus_bus_add_match (connection,
|
|
|
|
|
"type='signal'",
|
|
|
|
|
&error);
|
|
|
|
|
if (dbus_error_is_set (&error))
|
|
|
|
|
goto lose;
|
|
|
|
|
dbus_bus_add_match (connection,
|
|
|
|
|
"type='method_call'",
|
|
|
|
|
&error);
|
|
|
|
|
if (dbus_error_is_set (&error))
|
|
|
|
|
goto lose;
|
|
|
|
|
dbus_bus_add_match (connection,
|
|
|
|
|
"type='method_return'",
|
|
|
|
|
&error);
|
|
|
|
|
if (dbus_error_is_set (&error))
|
|
|
|
|
goto lose;
|
|
|
|
|
dbus_bus_add_match (connection,
|
|
|
|
|
"type='error'",
|
|
|
|
|
&error);
|
|
|
|
|
if (dbus_error_is_set (&error))
|
|
|
|
|
goto lose;
|
|
|
|
|
if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) {
|
|
|
|
|
fprintf (stderr, "Couldn't add filter!\n");
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
2003-04-14 03:25:19 +00:00
|
|
|
|
|
|
|
|
g_main_loop_run (loop);
|
|
|
|
|
|
|
|
|
|
exit (0);
|
2004-05-25 18:02:17 +00:00
|
|
|
lose:
|
|
|
|
|
fprintf (stderr, "Error: %s\n", error.message);
|
|
|
|
|
exit (1);
|
2003-04-14 03:25:19 +00:00
|
|
|
}
|