2006/6/13 火曜日

Skype Linux D-BUS sample code

カテゴリー: Skype — ryu @ 23:22:06

Hi, below is Skype Linux D-BUS API receiving event without Qt/Glib.
This is a sample, so it poll event and not use select and wait.

enjoy!

--
 #define DBUS_API_SUBJECT_TO_CHANGE

 #include <stdio.h>
 #include <string.h>
 #include <dbus/dbus.h>

 #include "mgw.h"

 #define DISPATCH_STATUS_NAME(s)                                               \
                      ((s) == DBUS_DISPATCH_COMPLETE ? "complete" :           \
                       (s) == DBUS_DISPATCH_DATA_REMAINS ? "data remains" : \
                       (s) == DBUS_DISPATCH_NEED_MEMORY ? "need memory" :   \
                       "???")

 char *sendToSkype( DBusConnection *conn, char *msg);

 DBusWatch      *g_watch = NULL;

 dbus_bool_t dbusAddWatchFunction( DBusWatch *watch, void *data ){
     puts( "dbusAddWatchFunction" );
     g_watch = watch;
     return TRUE;
 }
 void dbusRemoveWatchFunction( DBusWatch *watch, void *data ){
     puts( "dbusRemoveWatchFunction" );
 }
 void dbusWatchToggleFunction( DBusWatch *watch, void *data ){
     puts( "dbusWatchToggleFunction" );
 }
 void dbusFreeFunction( void *memory ){
     puts("dbusFreeFunction!!");
 }
 static void nm_unregister_handler( DBusConnection * connection, void *user_data ){
     puts( "nm_unregister_handler" );
 }

 static DBusHandlerResult nm_message_handler(
             DBusConnection * connection, DBusMessage * message, void *user_data )
 {

     DBusMessage *tmpmsg;
     DBusMessageIter iter, array;
     int reply_timeout, array_type;
     char *apim;

     tmpmsg = dbus_message_ref( message );

     dbus_message_iter_init( tmpmsg, &iter );
     if( dbus_message_iter_get_arg_type( &iter ) != DBUS_TYPE_STRING ){
             fprintf (stderr, "Error: reply is not except format 1\n");
             return FALSE;
     }
     for( ; ; dbus_message_iter_next( &iter ) ){
         apim = dbus_message_iter_get_string( &iter );
         printf( "message_handler::[%s]\n", apim );
         if( !dbus_message_iter_has_next(&iter) )
             break;
     }
     if( dbus_message_get_no_reply( tmpmsg ) != TRUE ){
         DBusMessage *reply = dbus_message_new_method_return( tmpmsg );
         dbus_connection_send(connection, reply, NULL );
         dbus_message_unref( reply );
     }

     dbus_message_unref( message );

     return DBUS_HANDLER_RESULT_HANDLED;
 }

 //
 //
 //
 //////////////////////////////////////////////////////////////////
 int main (int argc, char **argv){

     char *tmp;

     DBusConnection *connection  = NULL;
     DBusError error;
     dbus_error_init (&error);

     DBusObjectPathVTable vtable =
         { &nm_unregister_handler, &nm_message_handler,
                         NULL, NULL, NULL, NULL };

     connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
     if (connection == NULL){
         fprintf(stderr, "Failed to open connection to bus: %s\n",
                     error.message);
         dbus_error_free (&error);
         exit (1);
     }
     printf( "D-BUS connection OK!\n" );

     tmp = sendToSkype( connection, "NAME hogehoge");
     puts( tmp );
     printf( "Sent My name!\n" );
     usleep( 200 );
     tmp = sendToSkype( connection, "PROTOCOL 5");
     puts( tmp );
     printf( "PROTOCOL 5!\n" );
     usleep( 200 );

     dbus_connection_set_watch_functions(
                 connection,
                 dbusAddWatchFunction,
                 dbusRemoveWatchFunction,
                 dbusWatchToggleFunction,
                 NULL,
                 dbusFreeFunction );

     dbus_connection_register_object_path(
         connection, "/com/Skype/Client", &vtable, NULL);

     printf( "Added watch functions!\n" );

     DBusDispatchStatus status;
     while (1){
         printf( "main-loop in\n" );
         if( g_watch != NULL )
             dbus_watch_handle ( g_watch, DBUS_WATCH_READABLE );
         dbus_connection_ref( connection );
         status = dbus_connection_dispatch( connection );
         dbus_connection_unref( connection );
         dbus_connection_flush (connection);
         if( DBUS_DISPATCH_DATA_REMAINS != status ){
             usleep( 200 );
         }
         printf( "main-loop out\n" );
     }

     dbus_error_free (&error);
     sleep( 300 );
 }

 char *sendToSkype( DBusConnection * connection, char *msg){

     DBusMessage *message;
     DBusMessage *reply;
     DBusError error;

     static char returnbuf[64*1024];

     char *tmp;
     int reply_timeout = -1;   /*don't timeout*/

     dbus_error_init (&error);

     /* Construct the message */
     message = dbus_message_new_method_call (
                         "com.Skype.API",    /*service*/
                         "/com/Skype",        /*path*/
                         "com.Skype.API",  /*interface*/
                         "Invoke");
     dbus_message_set_auto_activation( message, TRUE );

     if( !dbus_message_append_args( message,
                         DBUS_TYPE_STRING, msg,
                         DBUS_TYPE_INVALID ) ){
         fprintf (stderr, "Error: reply is not except format\n");
         dbus_error_free (&error);
         exit (1);
     }

     reply_timeout = -1;   /*don't timeout*/
     reply = dbus_connection_send_with_reply_and_block (connection,
                         message,
                         reply_timeout,
                         &error);

     if (dbus_error_is_set (&error)){
         fprintf (stderr, "Error in send_with_reply_and_block: %s\n", error.message);
         dbus_error_free (&error);
         exit (1);
     }

     dbus_message_get_args( reply, &error,
                                 DBUS_TYPE_STRING, &tmp,
                                 DBUS_TYPE_INVALID);
     if (dbus_error_is_set (&error)){
         fprintf (stderr, "Error in dbus_message_get_args: %s\n", error.message);
         dbus_error_free (&error);
         exit (1);
     }

     strcpy( returnbuf, tmp );

     dbus_message_unref( message );
     dbus_message_unref( reply );

     dbus_error_free (&error);

     return returnbuf;
 }
--
working with Skype 1.2 and D-BUS 0.23.3.

コメントはまだありません

コメントはまだありません。

このコメント欄の RSS フィード トラックバック URL

コメントフォームは現在閉鎖中です。

37 queries. HTML convert time: 0.432 sec. Powered by WordPress 2.8