Main Page | Modules | Namespace List | Class Hierarchy | Data Structures | Directories | File List | Namespace Members | Data Fields | Related Pages

dbus-gmain.c

00001 /* -*- mode: C; c-file-style: "gnu" -*- */
00002 /* dbus-gmain.c GLib main loop integration
00003  *
00004  * Copyright (C) 2002, 2003 CodeFactory AB
00005  * Copyright (C) 2005 Red Hat, Inc.
00006  *
00007  * Licensed under the Academic Free License version 2.1
00008  * 
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  * 
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  *
00023  */
00024 
00025 #include <config.h>
00026 #include <dbus/dbus-glib.h>
00027 #include <dbus/dbus-glib-lowlevel.h>
00028 #include "dbus-gtest.h"
00029 #include "dbus-gutils.h"
00030 #include "dbus-gvalue.h"
00031 #include "dbus-gobject.h"
00032 #include "dbus-gvalue-utils.h"
00033 #include <string.h>
00034 
00035 #include <libintl.h>
00036 #define _(x) dgettext (GETTEXT_PACKAGE, x)
00037 #define N_(x) x
00038 
00065 typedef struct
00066 {
00067   GSource source; 
00068   DBusConnection *connection; 
00069 } DBusGMessageQueue;
00070 
00071 static gboolean message_queue_prepare  (GSource     *source,
00072                                         gint        *timeout);
00073 static gboolean message_queue_check    (GSource     *source);
00074 static gboolean message_queue_dispatch (GSource     *source,
00075                                         GSourceFunc  callback,
00076                                         gpointer     user_data);
00077 
00078 static GSourceFuncs message_queue_funcs = {
00079   message_queue_prepare,
00080   message_queue_check,
00081   message_queue_dispatch,
00082   NULL
00083 };
00084 
00085 static gboolean
00086 message_queue_prepare (GSource *source,
00087                        gint    *timeout)
00088 {
00089   DBusConnection *connection = ((DBusGMessageQueue *)source)->connection;
00090   
00091   *timeout = -1;
00092 
00093   return (dbus_connection_get_dispatch_status (connection) == DBUS_DISPATCH_DATA_REMAINS);  
00094 }
00095 
00096 static gboolean
00097 message_queue_check (GSource *source)
00098 {
00099   return FALSE;
00100 }
00101 
00102 static gboolean
00103 message_queue_dispatch (GSource     *source,
00104                         GSourceFunc  callback,
00105                         gpointer     user_data)
00106 {
00107   DBusConnection *connection = ((DBusGMessageQueue *)source)->connection;
00108 
00109   dbus_connection_ref (connection);
00110 
00111   /* Only dispatch once - we don't want to starve other GSource */
00112   dbus_connection_dispatch (connection);
00113   
00114   dbus_connection_unref (connection);
00115 
00116   return TRUE;
00117 }
00118 
00119 typedef struct
00120 {
00121   GMainContext *context;      
00122   GSList *ios;                
00123   GSList *timeouts;           
00124   DBusConnection *connection; 
00125   GSource *message_queue_source; 
00126 } ConnectionSetup;
00127 
00128 
00129 typedef struct
00130 {
00131   ConnectionSetup *cs;
00132   GSource *source;
00133   DBusWatch *watch;
00134 } IOHandler;
00135 
00136 typedef struct
00137 {
00138   ConnectionSetup *cs;
00139   GSource *source;
00140   DBusTimeout *timeout;
00141 } TimeoutHandler;
00142 
00143 static dbus_int32_t connection_slot = -1;
00144 static dbus_int32_t server_slot = -1;
00145 
00146 static ConnectionSetup*
00147 connection_setup_new (GMainContext   *context,
00148                       DBusConnection *connection)
00149 {
00150   ConnectionSetup *cs;
00151 
00152   cs = g_new0 (ConnectionSetup, 1);
00153 
00154   g_assert (context != NULL);
00155   
00156   cs->context = context;
00157   g_main_context_ref (cs->context);  
00158 
00159   if (connection)
00160     {
00161       cs->connection = connection;
00162 
00163       cs->message_queue_source = g_source_new (&message_queue_funcs,
00164                                                sizeof (DBusGMessageQueue));
00165       ((DBusGMessageQueue*)cs->message_queue_source)->connection = connection;
00166       g_source_attach (cs->message_queue_source, cs->context);
00167     }
00168   
00169   return cs;
00170 }
00171 
00172 static void
00173 io_handler_source_finalized (gpointer data)
00174 {
00175   IOHandler *handler;
00176 
00177   handler = data;
00178 
00179   if (handler->watch)
00180     dbus_watch_set_data (handler->watch, NULL, NULL);
00181   
00182   g_free (handler);
00183 }
00184 
00185 static void
00186 io_handler_destroy_source (void *data)
00187 {
00188   IOHandler *handler;
00189 
00190   handler = data;
00191 
00192   if (handler->source)
00193     {
00194       GSource *source = handler->source;
00195       handler->source = NULL;
00196       handler->cs->ios = g_slist_remove (handler->cs->ios, handler);
00197       g_source_destroy (source);
00198       g_source_unref (source);
00199     }
00200 }
00201 
00202 static void
00203 io_handler_watch_freed (void *data)
00204 {
00205   IOHandler *handler;
00206 
00207   handler = data;
00208 
00209   handler->watch = NULL;
00210 
00211   io_handler_destroy_source (handler);
00212 }
00213 
00214 static gboolean
00215 io_handler_dispatch (GIOChannel   *source,
00216                      GIOCondition  condition,
00217                      gpointer      data)
00218 {
00219   IOHandler *handler;
00220   guint dbus_condition = 0;
00221   DBusConnection *connection;
00222 
00223   handler = data;
00224 
00225   connection = handler->cs->connection;
00226   
00227   if (connection)
00228     dbus_connection_ref (connection);
00229   
00230   if (condition & G_IO_IN)
00231     dbus_condition |= DBUS_WATCH_READABLE;
00232   if (condition & G_IO_OUT)
00233     dbus_condition |= DBUS_WATCH_WRITABLE;
00234   if (condition & G_IO_ERR)
00235     dbus_condition |= DBUS_WATCH_ERROR;
00236   if (condition & G_IO_HUP)
00237     dbus_condition |= DBUS_WATCH_HANGUP;
00238 
00239   /* Note that we don't touch the handler after this, because
00240    * dbus may have disabled the watch and thus killed the
00241    * handler.
00242    */
00243   dbus_watch_handle (handler->watch, dbus_condition);
00244   handler = NULL;
00245 
00246   if (connection)
00247     dbus_connection_unref (connection);
00248   
00249   return TRUE;
00250 }
00251 
00252 static void
00253 connection_setup_add_watch (ConnectionSetup *cs,
00254                             DBusWatch       *watch)
00255 {
00256   guint flags;
00257   GIOCondition condition;
00258   GIOChannel *channel;
00259   IOHandler *handler;
00260   
00261   if (!dbus_watch_get_enabled (watch))
00262     return;
00263   
00264   g_assert (dbus_watch_get_data (watch) == NULL);
00265   
00266   flags = dbus_watch_get_flags (watch);
00267 
00268   condition = G_IO_ERR | G_IO_HUP;
00269   if (flags & DBUS_WATCH_READABLE)
00270     condition |= G_IO_IN;
00271   if (flags & DBUS_WATCH_WRITABLE)
00272     condition |= G_IO_OUT;
00273 
00274   handler = g_new0 (IOHandler, 1);
00275   handler->cs = cs;
00276   handler->watch = watch;
00277   
00278   channel = g_io_channel_unix_new (dbus_watch_get_fd (watch));
00279   
00280   handler->source = g_io_create_watch (channel, condition);
00281   g_source_set_callback (handler->source, (GSourceFunc) io_handler_dispatch, handler,
00282                          io_handler_source_finalized);
00283   g_source_attach (handler->source, cs->context);
00284 
00285   cs->ios = g_slist_prepend (cs->ios, handler);
00286   
00287   dbus_watch_set_data (watch, handler, io_handler_watch_freed);
00288 }
00289 
00290 static void
00291 connection_setup_remove_watch (ConnectionSetup *cs,
00292                                DBusWatch       *watch)
00293 {
00294   IOHandler *handler;
00295 
00296   handler = dbus_watch_get_data (watch);
00297 
00298   if (handler == NULL)
00299     return;
00300   
00301   io_handler_destroy_source (handler);
00302 }
00303 
00304 static void
00305 timeout_handler_source_finalized (gpointer data)
00306 {
00307   TimeoutHandler *handler;
00308 
00309   handler = data;
00310 
00311   if (handler->timeout)
00312     dbus_timeout_set_data (handler->timeout, NULL, NULL);
00313   
00314   g_free (handler);
00315 }
00316 
00317 static void
00318 timeout_handler_destroy_source (void *data)
00319 {
00320   TimeoutHandler *handler;
00321 
00322   handler = data;
00323 
00324   if (handler->source)
00325     {
00326       GSource *source = handler->source;
00327       handler->source = NULL;
00328       handler->cs->timeouts = g_slist_remove (handler->cs->timeouts, handler);
00329       g_source_destroy (source);
00330       g_source_unref (source);
00331     }
00332 }
00333 
00334 static void
00335 timeout_handler_timeout_freed (void *data)
00336 {
00337   TimeoutHandler *handler;
00338 
00339   handler = data;
00340 
00341   handler->timeout = NULL;
00342 
00343   timeout_handler_destroy_source (handler);
00344 }
00345 
00346 static gboolean
00347 timeout_handler_dispatch (gpointer      data)
00348 {
00349   TimeoutHandler *handler;
00350 
00351   handler = data;
00352 
00353   dbus_timeout_handle (handler->timeout);
00354   
00355   return TRUE;
00356 }
00357 
00358 static void
00359 connection_setup_add_timeout (ConnectionSetup *cs,
00360                               DBusTimeout     *timeout)
00361 {
00362   TimeoutHandler *handler;
00363   
00364   if (!dbus_timeout_get_enabled (timeout))
00365     return;
00366   
00367   g_assert (dbus_timeout_get_data (timeout) == NULL);
00368 
00369   handler = g_new0 (TimeoutHandler, 1);
00370   handler->cs = cs;
00371   handler->timeout = timeout;
00372 
00373   handler->source = g_timeout_source_new (dbus_timeout_get_interval (timeout));
00374   g_source_set_callback (handler->source, timeout_handler_dispatch, handler,
00375                          timeout_handler_source_finalized);
00376   g_source_attach (handler->source, handler->cs->context);
00377 
00378   cs->timeouts = g_slist_prepend (cs->timeouts, handler);
00379 
00380   dbus_timeout_set_data (timeout, handler, timeout_handler_timeout_freed);
00381 }
00382 
00383 static void
00384 connection_setup_remove_timeout (ConnectionSetup *cs,
00385                                  DBusTimeout       *timeout)
00386 {
00387   TimeoutHandler *handler;
00388   
00389   handler = dbus_timeout_get_data (timeout);
00390 
00391   if (handler == NULL)
00392     return;
00393   
00394   timeout_handler_destroy_source (handler);
00395 }
00396 
00397 static void
00398 connection_setup_free (ConnectionSetup *cs)
00399 {
00400   while (cs->ios)
00401     io_handler_destroy_source (cs->ios->data);
00402 
00403   while (cs->timeouts)
00404     timeout_handler_destroy_source (cs->timeouts->data);
00405 
00406   if (cs->message_queue_source)
00407     {
00408       GSource *source;
00409 
00410       source = cs->message_queue_source;
00411       cs->message_queue_source = NULL;
00412 
00413       g_source_destroy (source);
00414       g_source_unref (source);
00415     }
00416   
00417   g_main_context_unref (cs->context);
00418   g_free (cs);
00419 }
00420 
00421 static dbus_bool_t
00422 add_watch (DBusWatch *watch,
00423            gpointer   data)
00424 {
00425   ConnectionSetup *cs;
00426 
00427   cs = data;
00428 
00429   connection_setup_add_watch (cs, watch);
00430   
00431   return TRUE;
00432 }
00433 
00434 static void
00435 remove_watch (DBusWatch *watch,
00436               gpointer   data)
00437 {
00438   ConnectionSetup *cs;
00439 
00440   cs = data;
00441 
00442   connection_setup_remove_watch (cs, watch);
00443 }
00444 
00445 static void
00446 watch_toggled (DBusWatch *watch,
00447                void      *data)
00448 {
00449   /* Because we just exit on OOM, enable/disable is
00450    * no different from add/remove
00451    */
00452   if (dbus_watch_get_enabled (watch))
00453     add_watch (watch, data);
00454   else
00455     remove_watch (watch, data);
00456 }
00457 
00458 static dbus_bool_t
00459 add_timeout (DBusTimeout *timeout,
00460              void        *data)
00461 {
00462   ConnectionSetup *cs;
00463 
00464   cs = data;
00465   
00466   if (!dbus_timeout_get_enabled (timeout))
00467     return TRUE;
00468 
00469   connection_setup_add_timeout (cs, timeout);
00470 
00471   return TRUE;
00472 }
00473 
00474 static void
00475 remove_timeout (DBusTimeout *timeout,
00476                 void        *data)
00477 {
00478   ConnectionSetup *cs;
00479 
00480   cs = data;
00481 
00482   connection_setup_remove_timeout (cs, timeout);
00483 }
00484 
00485 static void
00486 timeout_toggled (DBusTimeout *timeout,
00487                  void        *data)
00488 {
00489   /* Because we just exit on OOM, enable/disable is
00490    * no different from add/remove
00491    */
00492   if (dbus_timeout_get_enabled (timeout))
00493     add_timeout (timeout, data);
00494   else
00495     remove_timeout (timeout, data);
00496 }
00497 
00498 static void
00499 wakeup_main (void *data)
00500 {
00501   ConnectionSetup *cs = data;
00502 
00503   g_main_context_wakeup (cs->context);
00504 }
00505 
00506 
00507 /* Move to a new context */
00508 static ConnectionSetup*
00509 connection_setup_new_from_old (GMainContext    *context,
00510                                ConnectionSetup *old)
00511 {
00512   GSList *tmp;
00513   ConnectionSetup *cs;
00514 
00515   g_assert (old->context != context);
00516   
00517   cs = connection_setup_new (context, old->connection);
00518   
00519   tmp = old->ios;
00520   while (tmp != NULL)
00521     {
00522       IOHandler *handler = tmp->data;
00523 
00524       connection_setup_add_watch (cs, handler->watch);
00525       
00526       tmp = tmp->next;
00527     }
00528 
00529   tmp = old->timeouts;
00530   while (tmp != NULL)
00531     {
00532       TimeoutHandler *handler = tmp->data;
00533 
00534       connection_setup_add_timeout (cs, handler->timeout);
00535       
00536       tmp = tmp->next;
00537     }
00538 
00539   return cs;
00540 }
00541  /* End of GLib bindings internals */
00543 
00562 void
00563 dbus_connection_setup_with_g_main (DBusConnection *connection,
00564                                    GMainContext   *context)
00565 {
00566   ConnectionSetup *old_setup;
00567   ConnectionSetup *cs;
00568   
00569   /* FIXME we never free the slot, so its refcount just keeps growing,
00570    * which is kind of broken.
00571    */
00572   dbus_connection_allocate_data_slot (&connection_slot);
00573   if (connection_slot < 0)
00574     goto nomem;
00575 
00576   if (context == NULL)
00577     context = g_main_context_default ();
00578 
00579   cs = NULL;
00580   
00581   old_setup = dbus_connection_get_data (connection, connection_slot);
00582   if (old_setup != NULL)
00583     {
00584       if (old_setup->context == context)
00585         return; /* nothing to do */
00586 
00587       cs = connection_setup_new_from_old (context, old_setup);
00588       
00589       /* Nuke the old setup */
00590       dbus_connection_set_data (connection, connection_slot, NULL, NULL);
00591       old_setup = NULL;
00592     }
00593 
00594   if (cs == NULL)
00595     cs = connection_setup_new (context, connection);
00596 
00597   if (!dbus_connection_set_data (connection, connection_slot, cs,
00598                                  (DBusFreeFunction)connection_setup_free))
00599     goto nomem;
00600   
00601   if (!dbus_connection_set_watch_functions (connection,
00602                                             add_watch,
00603                                             remove_watch,
00604                                             watch_toggled,
00605                                             cs, NULL))
00606     goto nomem;
00607 
00608   if (!dbus_connection_set_timeout_functions (connection,
00609                                               add_timeout,
00610                                               remove_timeout,
00611                                               timeout_toggled,
00612                                               cs, NULL))
00613     goto nomem;
00614     
00615   dbus_connection_set_wakeup_main_function (connection,
00616                                             wakeup_main,
00617                                             cs, NULL);
00618       
00619   return;
00620 
00621  nomem:
00622   g_error ("Not enough memory to set up DBusConnection for use with GLib");
00623 }
00624 
00638 void
00639 dbus_server_setup_with_g_main (DBusServer   *server,
00640                                GMainContext *context)
00641 {
00642   ConnectionSetup *old_setup;
00643   ConnectionSetup *cs;
00644   
00645   /* FIXME we never free the slot, so its refcount just keeps growing,
00646    * which is kind of broken.
00647    */
00648   dbus_server_allocate_data_slot (&server_slot);
00649   if (server_slot < 0)
00650     goto nomem;
00651 
00652   if (context == NULL)
00653     context = g_main_context_default ();
00654 
00655   cs = NULL;
00656   
00657   old_setup = dbus_server_get_data (server, server_slot);
00658   if (old_setup != NULL)
00659     {
00660       if (old_setup->context == context)
00661         return; /* nothing to do */
00662 
00663       cs = connection_setup_new_from_old (context, old_setup);
00664       
00665       /* Nuke the old setup */
00666       dbus_server_set_data (server, server_slot, NULL, NULL);
00667       old_setup = NULL;
00668     }
00669 
00670   if (cs == NULL)
00671     cs = connection_setup_new (context, NULL);
00672 
00673   if (!dbus_server_set_data (server, server_slot, cs,
00674                              (DBusFreeFunction)connection_setup_free))
00675     goto nomem;
00676   
00677   if (!dbus_server_set_watch_functions (server,
00678                                         add_watch,
00679                                         remove_watch,
00680                                         watch_toggled,
00681                                         cs, NULL))
00682     goto nomem;
00683 
00684   if (!dbus_server_set_timeout_functions (server,
00685                                           add_timeout,
00686                                           remove_timeout,
00687                                           timeout_toggled,
00688                                           cs, NULL))
00689     goto nomem;
00690       
00691   return;
00692 
00693  nomem:
00694   g_error ("Not enough memory to set up DBusServer for use with GLib");
00695 }
00696 
00708 DBusGConnection*
00709 dbus_g_bus_get (DBusBusType     type,
00710                 GError        **error)
00711 {
00712   DBusConnection *connection;
00713   DBusError derror;
00714 
00715   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
00716 
00717   dbus_g_value_types_init ();
00718   
00719   dbus_error_init (&derror);
00720 
00721   connection = dbus_bus_get (type, &derror);
00722   if (connection == NULL)
00723     {
00724       dbus_set_g_error (error, &derror);
00725       dbus_error_free (&derror);
00726       return NULL;
00727     }
00728 
00729   /* does nothing if it's already been done */
00730   dbus_connection_setup_with_g_main (connection, NULL);
00731 
00732   return DBUS_G_CONNECTION_FROM_CONNECTION (connection);
00733 }
00734  /* end of public API */
00736 
00737 #ifdef DBUS_BUILD_TESTS
00738 
00744 gboolean
00745 _dbus_gmain_test (const char *test_data_dir)
00746 {
00747   GType rectype;
00748   GType gtype;
00749 
00750   g_type_init ();
00751   dbus_g_value_types_init ();
00752 
00753   rectype = dbus_g_type_get_collection ("GArray", G_TYPE_UINT);
00754   g_assert (rectype != G_TYPE_INVALID);
00755   g_assert (!strcmp (g_type_name (rectype), "GArray+guint"));
00756 
00757   gtype = dbus_gtype_from_signature ("au", TRUE);
00758   g_assert (gtype == rectype);
00759 
00760   rectype = dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_STRING);
00761   g_assert (rectype != G_TYPE_INVALID);
00762   g_assert (!strcmp (g_type_name (rectype), "GHashTable+gchararray+gchararray"));
00763 
00764   gtype = dbus_gtype_from_signature ("a{ss}", TRUE);
00765   g_assert (gtype == rectype);
00766 
00767   gtype = dbus_gtype_from_signature ("o", FALSE);
00768   g_assert (gtype == DBUS_TYPE_G_OBJECT_PATH);
00769   gtype = dbus_gtype_from_signature ("o", TRUE);
00770   g_assert (gtype == DBUS_TYPE_G_OBJECT_PATH);
00771   
00772   return TRUE;
00773 }
00774 
00775 #endif /* DBUS_BUILD_TESTS */

Generated on Tue Sep 13 01:28:06 2005 for D-BUS by  doxygen 1.4.4