[CONFIGURATION]

* NEW: Remove the ./reconf-all link.

[GB.DBUS]
* NEW: Start to handle incoming DBus message (methods calls, signals...).


git-svn-id: svn://localhost/gambas/trunk@3042 867c0c6c-44f3-4631-809d-bfa615b0a4ec
This commit is contained in:
Benoît Minisini 2010-07-16 01:17:49 +00:00
parent 0bc1701225
commit bc35f266db
9 changed files with 634 additions and 19 deletions

View file

@ -102,7 +102,7 @@ SearchString=True
[OpenFile]
File[1]="/home/benoit/gambas/3.0/link/share/gambas3/examples/Automation/DBusExplorer/.src/FVersiongbXML.form"
Active=2
File[2]="/home/benoit/gambas/3.0/link/share/gambas3/examples/Automation/DBusExplorer/.src/FVersiongbXML.class:99.19"
File[2]="/home/benoit/gambas/3.0/link/share/gambas3/examples/Automation/DBusExplorer/.src/FVersiongbXML.class:45.45"
Count=2
[Watches]

View file

@ -21,8 +21,9 @@ Public Sub _new()
End
Public Sub Form_Open()
Me.Raise
btnRefresh_Click
End
Public Sub ShowPathContent(sPath As String, sBus As String, sApplication As String)
@ -125,8 +126,8 @@ Public Sub btnRefresh_Click()
lstbSession.Clear
$ColArgs.Clear
tvDbus.Clear
lstbSession.List = DBus.Session.Applications
lstbSystem.List = DBus.System.Applications
lstbSession.List = DBus.Session.Applications.Sort()
lstbSystem.List = DBus.System.Applications.Sort()
End

View file

@ -12,4 +12,5 @@ gb_dbus_la_SOURCES = \
helper.c helper.h \
c_dbus.c c_dbus.h \
c_dbusvariant.c c_dbusvariant.h \
c_dbusconnection.c c_dbusconnection.h
c_dbusconnection.c c_dbusconnection.h \
dbus_print_message.c dbus_print_message.h

View file

@ -34,10 +34,11 @@ static DBusConnection *get_bus(DBusBusType type)
DBusError error;
dbus_error_init (&error);
conn = dbus_bus_get (type, &error);
conn = dbus_bus_get(type, &error);
if (!conn)
GB.Error("Cannot connect to the &1 bus", type == DBUS_BUS_SYSTEM ? "system" : "session");
else
dbus_connection_set_exit_on_disconnect(conn, FALSE);
return conn;
}
@ -78,20 +79,20 @@ CDBUSCONNECTION *CDBUSCONNECTION_get(DBusBusType type)
return NULL;
}
BEGIN_METHOD_VOID(dbusconnection_exit)
BEGIN_METHOD_VOID(DBusConnection_exit)
GB.Unref(POINTER(&_session));
GB.Unref(POINTER(&_system));
END_METHOD
BEGIN_METHOD_VOID(dbusconnection_free)
BEGIN_METHOD_VOID(DBusConnection_free)
dbus_connection_unref(THIS->connection);
END_METHOD
BEGIN_METHOD(dbusconnection_Introspect, GB_STRING application; GB_STRING object)
BEGIN_METHOD(DBusConnection_Introspect, GB_STRING application; GB_STRING object)
char *application = GB.ToZeroString(ARG(application));
char *object;
@ -105,7 +106,7 @@ BEGIN_METHOD(dbusconnection_Introspect, GB_STRING application; GB_STRING object)
END_METHOD
BEGIN_METHOD(dbusconnection_CallMethod, GB_STRING application; GB_STRING object; GB_STRING interface; GB_STRING method;
BEGIN_METHOD(DBusConnection_CallMethod, GB_STRING application; GB_STRING object; GB_STRING interface; GB_STRING method;
GB_STRING input_signature; GB_STRING output_signature; GB_OBJECT arguments)
char *application = GB.ToZeroString(ARG(application));
@ -139,21 +140,110 @@ BEGIN_METHOD(dbusconnection_CallMethod, GB_STRING application; GB_STRING object;
END_METHOD
BEGIN_PROPERTY(dbusconnection_Applications)
BEGIN_PROPERTY(DBusConnection_Applications)
DBUS_call_method(THIS->connection, "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "ListNames", "", "as", NULL);
END_PROPERTY
BEGIN_METHOD(DBusConnection_Register, GB_STRING name; GB_BOOLEAN unique)
bool ret = DBUS_register(THIS->connection, GB.ToZeroString(ARG(name)), VARGOPT(unique, FALSE));
GB.ReturnBoolean(ret);
END_METHOD
BEGIN_PROPERTY(DBusConnection_Name)
GB.ReturnNewZeroString(dbus_bus_get_unique_name(THIS->connection));
END_PROPERTY
static void add_rule(char **match, const char *name, const char *rule, int len)
{
if (len <= 0)
len = strlen(rule);
if (len == 1 && *rule == '*')
return;
if (*match)
GB.AddString(match, ",", 1);
GB.AddString(match, name, 0);
GB.AddString(match, "='", 2);
GB.AddString(match, rule, len);
GB.AddString(match, "'", 1);
}
static bool handle_match(bool add, DBusConnection *connection, char *match)
{
DBusError error;
dbus_error_init(&error);
if (add)
dbus_bus_add_match(connection, match, &error);
else
dbus_bus_remove_match(connection, match, &error);
GB.FreeString(&match);
return dbus_error_is_set(&error);
}
BEGIN_METHOD(DBusConnection_AddMatch, GB_STRING type; GB_STRING object; GB_STRING member; GB_STRING interface; GB_STRING destination)
char *match = NULL;
add_rule(&match, "type", STRING(type), LENGTH(type));
if (!MISSING(object)) add_rule(&match, "path", STRING(object), LENGTH(object));
if (!MISSING(member)) add_rule(&match, "member", STRING(member), LENGTH(member));
if (!MISSING(interface)) add_rule(&match, "interface", STRING(interface), LENGTH(interface));
if (MISSING(destination))
add_rule(&match, "destination", dbus_bus_get_unique_name(THIS->connection), 0);
else
add_rule(&match, "destination", STRING(destination), LENGTH(destination));
GB.ReturnBoolean(handle_match(TRUE, THIS->connection, match));
END_METHOD
BEGIN_METHOD(DBusConnection_RemoveMatch, GB_STRING type; GB_STRING object; GB_STRING member; GB_STRING interface; GB_STRING destination)
char *match = NULL;
add_rule(&match, "type", STRING(type), LENGTH(type));
if (!MISSING(object)) add_rule(&match, "path", STRING(object), LENGTH(object));
if (!MISSING(member)) add_rule(&match, "member", STRING(member), LENGTH(member));
if (!MISSING(interface)) add_rule(&match, "interface", STRING(interface), LENGTH(interface));
if (MISSING(destination))
add_rule(&match, "destination", dbus_bus_get_unique_name(THIS->connection), 0);
else
add_rule(&match, "destination", STRING(destination), LENGTH(destination));
GB.ReturnBoolean(handle_match(FALSE, THIS->connection, match));
END_METHOD
GB_DESC CDBusConnectionDesc[] =
{
GB_DECLARE("DBusConnection", sizeof(CDBUSCONNECTION)),
GB_STATIC_METHOD("_exit", NULL, dbusconnection_exit, NULL),
GB_METHOD("_free", NULL, dbusconnection_free, NULL),
GB_METHOD("_Introspect", "s", dbusconnection_Introspect, "(Application)s[(Object)s]"),
GB_METHOD("_CallMethod", "v", dbusconnection_CallMethod, "(Application)s(Object)s(Interface)s(Method)s(InputSignature)s(OutputSignature)s(Arguments)Array;"),
GB_PROPERTY_READ("Applications", "String[]", dbusconnection_Applications),
GB_STATIC_METHOD("_exit", NULL, DBusConnection_exit, NULL),
GB_METHOD("_free", NULL, DBusConnection_free, NULL),
GB_METHOD("_Introspect", "s", DBusConnection_Introspect, "(Application)s[(Object)s]"),
GB_METHOD("_CallMethod", "v", DBusConnection_CallMethod, "(Application)s(Object)s(Interface)s(Method)s(InputSignature)s(OutputSignature)s(Arguments)Array;"),
GB_METHOD("_AddMatch", "b", DBusConnection_AddMatch, "(Type)s[(Object)s(Member)s(Interface)s(Destination)s]"),
GB_METHOD("_RemoveMatch", "b", DBusConnection_RemoveMatch, "(Type)s[(Object)s(Member)s(Interface)s(Destination)s]"),
GB_PROPERTY_READ("Applications", "String[]", DBusConnection_Applications),
GB_METHOD("Register", "b", DBusConnection_Register, "(Name)s[(Unique)b]"),
GB_PROPERTY_READ("Name", "s", DBusConnection_Name),
GB_END_DECLARE
};

View file

@ -0,0 +1,407 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-print-message.h Utility function to print out a message
*
* Copyright (C) 2003 Philip Blundell <philb@gnu.org>
* Copyright (C) 2003 Red Hat, Inc.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "dbus_print_message.h"
#include <stdlib.h>
#include "config.h"
static const char*
type_to_name (int message_type)
{
switch (message_type)
{
case DBUS_MESSAGE_TYPE_SIGNAL:
return "signal";
case DBUS_MESSAGE_TYPE_METHOD_CALL:
return "method call";
case DBUS_MESSAGE_TYPE_METHOD_RETURN:
return "method return";
case DBUS_MESSAGE_TYPE_ERROR:
return "error";
default:
return "(unknown message type)";
}
}
#define INDENT 3
static void
indent (int depth)
{
while (depth-- > 0)
printf (" "); /* INDENT spaces. */
}
static void
print_hex (unsigned char *bytes, unsigned int len, int depth)
{
int i, columns;
printf ("array of bytes [\n");
indent (depth + 1);
/* Each byte takes 3 cells (two hexits, and a space), except the last one. */
columns = (80 - ((depth + 1) * INDENT)) / 3;
if (columns < 8)
columns = 8;
i = 0;
while (i < len)
{
printf ("%02x", bytes[i]);
i++;
if (i != len)
{
if (i % columns == 0)
{
printf ("\n");
indent (depth + 1);
}
else
{
printf (" ");
}
}
}
printf ("\n");
indent (depth);
printf ("]\n");
}
#define DEFAULT_SIZE 100
static void
print_ay (DBusMessageIter *iter, int depth)
{
/* Not using DBusString because it's not public API. It's 2009, and I'm
* manually growing a string chunk by chunk.
*/
unsigned char *bytes = malloc (DEFAULT_SIZE + 1);
unsigned int len = 0;
unsigned int max = DEFAULT_SIZE;
dbus_bool_t all_ascii = TRUE;
int current_type;
while ((current_type = dbus_message_iter_get_arg_type (iter))
!= DBUS_TYPE_INVALID)
{
unsigned char val;
dbus_message_iter_get_basic (iter, &val);
bytes[len] = val;
len++;
if (val < 32 || val > 126)
all_ascii = FALSE;
if (len == max)
{
max *= 2;
bytes = realloc (bytes, max + 1);
}
dbus_message_iter_next (iter);
}
if (all_ascii)
{
bytes[len] = '\0';
printf ("array of bytes \"%s\"\n", bytes);
}
else
{
print_hex (bytes, len, depth);
}
free (bytes);
}
static void
print_iter (DBusMessageIter *iter, dbus_bool_t literal, int depth)
{
do
{
int type = dbus_message_iter_get_arg_type (iter);
if (type == DBUS_TYPE_INVALID)
break;
indent(depth);
switch (type)
{
case DBUS_TYPE_STRING:
{
char *val;
dbus_message_iter_get_basic (iter, &val);
if (!literal)
printf ("string \"");
printf ("%s", val);
if (!literal)
printf ("\"\n");
break;
}
case DBUS_TYPE_SIGNATURE:
{
char *val;
dbus_message_iter_get_basic (iter, &val);
if (!literal)
printf ("signature \"");
printf ("%s", val);
if (!literal)
printf ("\"\n");
break;
}
case DBUS_TYPE_OBJECT_PATH:
{
char *val;
dbus_message_iter_get_basic (iter, &val);
if (!literal)
printf ("object path \"");
printf ("%s", val);
if (!literal)
printf ("\"\n");
break;
}
case DBUS_TYPE_INT16:
{
dbus_int16_t val;
dbus_message_iter_get_basic (iter, &val);
printf ("int16 %d\n", val);
break;
}
case DBUS_TYPE_UINT16:
{
dbus_uint16_t val;
dbus_message_iter_get_basic (iter, &val);
printf ("uint16 %u\n", val);
break;
}
case DBUS_TYPE_INT32:
{
dbus_int32_t val;
dbus_message_iter_get_basic (iter, &val);
printf ("int32 %d\n", val);
break;
}
case DBUS_TYPE_UINT32:
{
dbus_uint32_t val;
dbus_message_iter_get_basic (iter, &val);
printf ("uint32 %u\n", val);
break;
}
case DBUS_TYPE_INT64:
{
dbus_int64_t val;
dbus_message_iter_get_basic (iter, &val);
#ifdef DBUS_INT64_PRINTF_MODIFIER
printf ("int64 %" DBUS_INT64_PRINTF_MODIFIER "d\n", val);
#else
printf ("int64 (omitted)\n");
#endif
break;
}
case DBUS_TYPE_UINT64:
{
dbus_uint64_t val;
dbus_message_iter_get_basic (iter, &val);
#ifdef DBUS_INT64_PRINTF_MODIFIER
printf ("uint64 %" DBUS_INT64_PRINTF_MODIFIER "u\n", val);
#else
printf ("uint64 (omitted)\n");
#endif
break;
}
case DBUS_TYPE_DOUBLE:
{
double val;
dbus_message_iter_get_basic (iter, &val);
printf ("double %g\n", val);
break;
}
case DBUS_TYPE_BYTE:
{
unsigned char val;
dbus_message_iter_get_basic (iter, &val);
printf ("byte %d\n", val);
break;
}
case DBUS_TYPE_BOOLEAN:
{
dbus_bool_t val;
dbus_message_iter_get_basic (iter, &val);
printf ("boolean %s\n", val ? "true" : "false");
break;
}
case DBUS_TYPE_VARIANT:
{
DBusMessageIter subiter;
dbus_message_iter_recurse (iter, &subiter);
printf ("variant ");
print_iter (&subiter, literal, depth+1);
break;
}
case DBUS_TYPE_ARRAY:
{
int current_type;
DBusMessageIter subiter;
dbus_message_iter_recurse (iter, &subiter);
current_type = dbus_message_iter_get_arg_type (&subiter);
if (current_type == DBUS_TYPE_BYTE)
{
print_ay (&subiter, depth);
break;
}
printf("array [\n");
while (current_type != DBUS_TYPE_INVALID)
{
print_iter (&subiter, literal, depth+1);
dbus_message_iter_next (&subiter);
current_type = dbus_message_iter_get_arg_type (&subiter);
if (current_type != DBUS_TYPE_INVALID)
printf (",");
}
indent(depth);
printf("]\n");
break;
}
case DBUS_TYPE_DICT_ENTRY:
{
DBusMessageIter subiter;
dbus_message_iter_recurse (iter, &subiter);
printf("dict entry(\n");
print_iter (&subiter, literal, depth+1);
dbus_message_iter_next (&subiter);
print_iter (&subiter, literal, depth+1);
indent(depth);
printf(")\n");
break;
}
case DBUS_TYPE_STRUCT:
{
int current_type;
DBusMessageIter subiter;
dbus_message_iter_recurse (iter, &subiter);
printf("struct {\n");
while ((current_type = dbus_message_iter_get_arg_type (&subiter)) != DBUS_TYPE_INVALID)
{
print_iter (&subiter, literal, depth+1);
dbus_message_iter_next (&subiter);
if (dbus_message_iter_get_arg_type (&subiter) != DBUS_TYPE_INVALID)
printf (",");
}
indent(depth);
printf("}\n");
break;
}
default:
printf (" (dbus-monitor too dumb to decipher arg type '%c')\n", type);
break;
}
} while (dbus_message_iter_next (iter));
}
void
print_message (DBusMessage *message, dbus_bool_t literal)
{
DBusMessageIter iter;
const char *sender;
const char *destination;
int message_type;
message_type = dbus_message_get_type (message);
sender = dbus_message_get_sender (message);
destination = dbus_message_get_destination (message);
if (!literal)
{
printf ("%s sender=%s -> dest=%s",
type_to_name (message_type),
sender ? sender : "(null sender)",
destination ? destination : "(null destination)");
switch (message_type)
{
case DBUS_MESSAGE_TYPE_METHOD_CALL:
case DBUS_MESSAGE_TYPE_SIGNAL:
printf (" serial=%u path=%s; interface=%s; member=%s\n",
dbus_message_get_serial (message),
dbus_message_get_path (message),
dbus_message_get_interface (message),
dbus_message_get_member (message));
break;
case DBUS_MESSAGE_TYPE_METHOD_RETURN:
printf (" reply_serial=%u\n",
dbus_message_get_reply_serial (message));
break;
case DBUS_MESSAGE_TYPE_ERROR:
printf (" error_name=%s reply_serial=%u\n",
dbus_message_get_error_name (message),
dbus_message_get_reply_serial (message));
break;
default:
printf ("\n");
break;
}
}
dbus_message_iter_init (message, &iter);
print_iter (&iter, literal, 1);
fflush (stdout);
}

View file

@ -0,0 +1,31 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-print-message.h Utility function to print out a message
*
* Copyright (C) 2003 Philip Blundell <philb@gnu.org>
* Copyright (C) 2003 Red Hat, Inc.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef DBUS_PRINT_MESSAGE_H
#define DBUS_PRINT_MESSAGE_H
#include <stdio.h>
#include <string.h>
#include <dbus/dbus.h>
void print_message (DBusMessage *message, dbus_bool_t literal);
#endif /* DBUS_PRINT_MESSAGE_H */

View file

@ -23,6 +23,7 @@
#define __HELPER_C
#include "c_dbusvariant.h"
#include "dbus_print_message.h"
#include "helper.h"
typedef
@ -552,7 +553,7 @@ char *DBUS_introspect(DBusConnection *connection, const char *application, const
dbus_error_init(&error);
reply = dbus_connection_send_with_reply_and_block (connection, message, -1, &error);
if (dbus_error_is_set (&error))
if (dbus_error_is_set(&error))
{
GB.Error("&1: &2", error.name, error.message);
goto __RETURN;
@ -615,6 +616,87 @@ __RETURN:
}
static DBusHandlerResult filter_func (DBusConnection *connection, DBusMessage *message, void *user_data)
{
print_message(message, FALSE);
return DBUS_HANDLER_RESULT_HANDLED;
}
static void handle_message(int fd, int type, DBusConnection *connection)
{
fprintf(stdout, "handle_message\n");
do
{
dbus_connection_read_write_dispatch(connection, -1);
}
while (dbus_connection_get_dispatch_status(connection) == DBUS_DISPATCH_DATA_REMAINS);
}
/*static bool add_match(DBusConnection *connection, const char *rule)
{
DBusError error;
dbus_error_init(&error);
char buffer[256];
sprintf(buffer, "destination='%s',%s", dbus_bus_get_unique_name(connection), rule);
dbus_bus_add_match(connection, buffer, &error);
return dbus_error_is_set(&error);
}
static bool remove_match(DBusConnection *connection, const char *rule)
{
DBusError error;
dbus_error_init(&error);
char buffer[256];
sprintf(buffer, "destination='%s',%s", dbus_bus_get_unique_name(connection), rule);
dbus_bus_remove_match(connection, buffer, &error);
return dbus_error_is_set(&error);
}*/
bool DBUS_register(DBusConnection *connection, const char *name, bool unique)
{
DBusError error;
int ret, socket;
if (!dbus_connection_get_socket(connection, &socket) || !dbus_connection_add_filter(connection, filter_func, NULL, NULL))
{
GB.Error("Unable to watch the DBus connection");
return TRUE;
}
dbus_error_init(&error);
ret = dbus_bus_request_name(connection, name, unique ? DBUS_NAME_FLAG_DO_NOT_QUEUE : 0, &error);
if (dbus_error_is_set(&error))
{
GB.Error("Unable to register application");
return TRUE;
}
if (unique && ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
return TRUE;
/*if (add_match(connection, "type='signal'")) goto ERROR_MATCH;
if (add_match(connection, "type='method_call'")) goto ERROR_MATCH;
//if (add_match(connection, "type='method_return'") goto ERROR_MATCH;
if (add_match(connection, "type='error'")) goto ERROR_MATCH;*/
GB.Watch(socket, GB_WATCH_READ, (void *)handle_message, (intptr_t)connection);
if (dbus_connection_get_dispatch_status(connection) == DBUS_DISPATCH_DATA_REMAINS)
handle_message(-1, 0, connection);
return FALSE;
}
/***************************************************************************
Validation routines taken directly from the D-Bus source code.
@ -771,3 +853,4 @@ bool DBUS_validate_method(const char *method, int len)
return FALSE;
}

View file

@ -30,8 +30,11 @@ bool DBUS_call_method(DBusConnection *connection, const char *application, const
char *DBUS_introspect(DBusConnection *connection, const char *dest, const char *path);
bool DBUS_register(DBusConnection *connection, const char *name, bool unique);
bool DBUS_validate_path(const char *path, int len);
bool DBUS_validate_interface (const char *interface, int len);
bool DBUS_validate_method(const char *method, int len);
#endif

View file

@ -1 +0,0 @@
reconf