2006-10-21 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-connection.h
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-connection.h DBusConnection object
3  *
4  * Copyright (C) 2002, 2003  Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23 #if !defined (DBUS_INSIDE_DBUS_H) && !defined (DBUS_COMPILATION)
24 #error "Only <dbus/dbus.h> can be included directly, this file may disappear or change contents."
25 #endif
26
27 #ifndef DBUS_CONNECTION_H
28 #define DBUS_CONNECTION_H
29
30 #include <dbus/dbus-errors.h>
31 #include <dbus/dbus-memory.h>
32 #include <dbus/dbus-message.h>
33 #include <dbus/dbus-shared.h>
34
35 DBUS_BEGIN_DECLS
36
37 /**
38  * @addtogroup DBusConnection
39  * @{
40  */
41
42 /* documented in dbus-watch.c */
43 typedef struct DBusWatch DBusWatch;
44 /* documented in dbus-timeout.c */
45 typedef struct DBusTimeout DBusTimeout;
46 /** Opaque type representing preallocated resources so a message can be sent without further memory allocation. */
47 typedef struct DBusPreallocatedSend DBusPreallocatedSend;
48 /** Opaque type representing a method call that has not yet received a reply. */
49 typedef struct DBusPendingCall DBusPendingCall;
50 /** Opaque type representing a connection to a remote application and associated incoming/outgoing message queues. */
51 typedef struct DBusConnection DBusConnection;
52 /** Set of functions that must be implemented to handle messages sent to a particular object path. */
53 typedef struct DBusObjectPathVTable DBusObjectPathVTable;
54
55 /**
56  * Indicates the status of a #DBusWatch.
57  */
58 typedef enum
59 {
60   DBUS_WATCH_READABLE = 1 << 0, /**< As in POLLIN */
61   DBUS_WATCH_WRITABLE = 1 << 1, /**< As in POLLOUT */
62   DBUS_WATCH_ERROR    = 1 << 2, /**< As in POLLERR (can't watch for this, but
63                                  *   the flag can be passed to dbus_watch_handle()).
64                                  */
65   DBUS_WATCH_HANGUP   = 1 << 3  /**< As in POLLHUP (can't watch for it, but
66                                  *   can be present in current state).
67                                  */
68 } DBusWatchFlags;
69
70 /**
71  * Indicates the status of incoming data on a #DBusConnection. This determines whether
72  * dbus_connection_dispatch() needs to be called.
73  */
74 typedef enum
75 {
76   DBUS_DISPATCH_DATA_REMAINS,  /**< There is more data to potentially convert to messages. */
77   DBUS_DISPATCH_COMPLETE,      /**< All currently available data has been processed. */
78   DBUS_DISPATCH_NEED_MEMORY    /**< More memory is needed to continue. */
79 } DBusDispatchStatus;
80
81 /** Called when libdbus needs a new watch to be monitored by the main
82  * loop. Returns #FALSE if it lacks enough memory to add the
83  * watch. Set by dbus_connection_set_watch_functions() or
84  * dbus_server_set_watch_functions().
85  */
86 typedef dbus_bool_t (* DBusAddWatchFunction)       (DBusWatch      *watch,
87                                                     void           *data);
88 /** Called when dbus_watch_get_enabled() may return a different value
89  *  than it did before.  Set by dbus_connection_set_watch_functions()
90  *  or dbus_server_set_watch_functions().
91  */
92 typedef void        (* DBusWatchToggledFunction)   (DBusWatch      *watch,
93                                                     void           *data);
94 /** Called when libdbus no longer needs a watch to be monitored by the
95  * main loop. Set by dbus_connection_set_watch_functions() or
96  * dbus_server_set_watch_functions().
97  */
98 typedef void        (* DBusRemoveWatchFunction)    (DBusWatch      *watch,
99                                                     void           *data);
100 /** Called when libdbus needs a new timeout to be monitored by the main
101  * loop. Returns #FALSE if it lacks enough memory to add the
102  * watch. Set by dbus_connection_set_timeout_functions() or
103  * dbus_server_set_timeout_functions().
104  */
105 typedef dbus_bool_t (* DBusAddTimeoutFunction)     (DBusTimeout    *timeout,
106                                                     void           *data);
107 /** Called when dbus_timeout_get_enabled() may return a different
108  * value than it did before.
109  * Set by dbus_connection_set_timeout_functions() or
110  * dbus_server_set_timeout_functions().
111  */
112 typedef void        (* DBusTimeoutToggledFunction) (DBusTimeout    *timeout,
113                                                     void           *data);
114 /** Called when libdbus no longer needs a timeout to be monitored by the
115  * main loop. Set by dbus_connection_set_timeout_functions() or
116  * dbus_server_set_timeout_functions().
117  */
118 typedef void        (* DBusRemoveTimeoutFunction)  (DBusTimeout    *timeout,
119                                                     void           *data);
120 /** Called when the return value of dbus_connection_get_dispatch_status()
121  * may have changed. Set with dbus_connection_set_dispatch_status_function().
122  */
123 typedef void        (* DBusDispatchStatusFunction) (DBusConnection *connection,
124                                                     DBusDispatchStatus new_status,
125                                                     void           *data);
126 /**
127  * Called when the main loop's thread should be notified that there's now work
128  * to do. Set with dbus_connection_set_wakeup_main_function().
129  */
130 typedef void        (* DBusWakeupMainFunction)     (void           *data);
131 /**
132  * Called during authentication on UNIX systems to check whether the given
133  * user ID is allowed to connect. Never called on Windows. Set with
134  * dbus_connection_set_unix_user_function().
135  */ 
136 typedef dbus_bool_t (* DBusAllowUnixUserFunction)  (DBusConnection *connection,
137                                                     unsigned long   uid,
138                                                     void           *data);
139 /**
140  * Called when a pending call now has a reply available. Set with
141  * dbus_pending_call_set_notify().
142  */
143 typedef void (* DBusPendingCallNotifyFunction) (DBusPendingCall *pending,
144                                                 void            *user_data);
145
146 /**
147  * Called when a message needs to be handled. The result indicates whether or
148  * not more handlers should be run. Set with dbus_connection_add_filter().
149  */
150 typedef DBusHandlerResult (* DBusHandleMessageFunction) (DBusConnection     *connection,
151                                                          DBusMessage        *message,
152                                                          void               *user_data);
153
154 DBusConnection*    dbus_connection_open                         (const char                 *address,
155                                                                  DBusError                  *error);
156 DBusConnection*    dbus_connection_open_private                 (const char                 *address,
157                                                                  DBusError                  *error);
158 DBusConnection*    dbus_connection_ref                          (DBusConnection             *connection);
159 void               dbus_connection_unref                        (DBusConnection             *connection);
160 void               dbus_connection_close                        (DBusConnection             *connection);
161 dbus_bool_t        dbus_connection_get_is_connected             (DBusConnection             *connection);
162 dbus_bool_t        dbus_connection_get_is_authenticated         (DBusConnection             *connection);
163 void               dbus_connection_set_exit_on_disconnect       (DBusConnection             *connection,
164                                                                  dbus_bool_t                 exit_on_disconnect);
165 void               dbus_connection_flush                        (DBusConnection             *connection);
166 dbus_bool_t        dbus_connection_read_write_dispatch          (DBusConnection             *connection,
167                                                                  int                         timeout_milliseconds);
168 dbus_bool_t        dbus_connection_read_write                   (DBusConnection             *connection,
169                                                                  int                         timeout_milliseconds);
170 DBusMessage*       dbus_connection_borrow_message               (DBusConnection             *connection);
171 void               dbus_connection_return_message               (DBusConnection             *connection,
172                                                                  DBusMessage                *message);
173 void               dbus_connection_steal_borrowed_message       (DBusConnection             *connection,
174                                                                  DBusMessage                *message);
175 DBusMessage*       dbus_connection_pop_message                  (DBusConnection             *connection);
176 DBusDispatchStatus dbus_connection_get_dispatch_status          (DBusConnection             *connection);
177 DBusDispatchStatus dbus_connection_dispatch                     (DBusConnection             *connection);
178 dbus_bool_t        dbus_connection_has_messages_to_send         (DBusConnection *connection);
179 dbus_bool_t        dbus_connection_send                         (DBusConnection             *connection,
180                                                                  DBusMessage                *message,
181                                                                  dbus_uint32_t              *client_serial);
182 dbus_bool_t        dbus_connection_send_with_reply              (DBusConnection             *connection,
183                                                                  DBusMessage                *message,
184                                                                  DBusPendingCall           **pending_return,
185                                                                  int                         timeout_milliseconds);
186 DBusMessage *      dbus_connection_send_with_reply_and_block    (DBusConnection             *connection,
187                                                                  DBusMessage                *message,
188                                                                  int                         timeout_milliseconds,
189                                                                  DBusError                  *error);
190 dbus_bool_t        dbus_connection_set_watch_functions          (DBusConnection             *connection,
191                                                                  DBusAddWatchFunction        add_function,
192                                                                  DBusRemoveWatchFunction     remove_function,
193                                                                  DBusWatchToggledFunction    toggled_function,
194                                                                  void                       *data,
195                                                                  DBusFreeFunction            free_data_function);
196 dbus_bool_t        dbus_connection_set_timeout_functions        (DBusConnection             *connection,
197                                                                  DBusAddTimeoutFunction      add_function,
198                                                                  DBusRemoveTimeoutFunction   remove_function,
199                                                                  DBusTimeoutToggledFunction  toggled_function,
200                                                                  void                       *data,
201                                                                  DBusFreeFunction            free_data_function);
202 void               dbus_connection_set_wakeup_main_function     (DBusConnection             *connection,
203                                                                  DBusWakeupMainFunction      wakeup_main_function,
204                                                                  void                       *data,
205                                                                  DBusFreeFunction            free_data_function);
206 void               dbus_connection_set_dispatch_status_function (DBusConnection             *connection,
207                                                                  DBusDispatchStatusFunction  function,
208                                                                  void                       *data,
209                                                                  DBusFreeFunction            free_data_function);
210 dbus_bool_t        dbus_connection_get_unix_user                (DBusConnection             *connection,
211                                                                  unsigned long              *uid);
212 dbus_bool_t        dbus_connection_get_unix_process_id          (DBusConnection             *connection,
213                                                                  unsigned long              *pid);
214 void               dbus_connection_set_unix_user_function       (DBusConnection             *connection,
215                                                                  DBusAllowUnixUserFunction   function,
216                                                                  void                       *data,
217                                                                  DBusFreeFunction            free_data_function);
218 void               dbus_connection_set_route_peer_messages      (DBusConnection             *connection,
219                                                                  dbus_bool_t                 value);
220
221
222 int          dbus_watch_get_fd      (DBusWatch        *watch);
223 unsigned int dbus_watch_get_flags   (DBusWatch        *watch);
224 void*        dbus_watch_get_data    (DBusWatch        *watch);
225 void         dbus_watch_set_data    (DBusWatch        *watch,
226                                      void             *data,
227                                      DBusFreeFunction  free_data_function);
228 dbus_bool_t  dbus_watch_handle      (DBusWatch        *watch,
229                                      unsigned int      flags);
230 dbus_bool_t  dbus_watch_get_enabled (DBusWatch        *watch);
231
232 int         dbus_timeout_get_interval (DBusTimeout      *timeout);
233 void*       dbus_timeout_get_data     (DBusTimeout      *timeout);
234 void        dbus_timeout_set_data     (DBusTimeout      *timeout,
235                                        void             *data,
236                                        DBusFreeFunction  free_data_function);
237 dbus_bool_t dbus_timeout_handle       (DBusTimeout      *timeout);
238 dbus_bool_t dbus_timeout_get_enabled  (DBusTimeout      *timeout);
239
240 /* Filters */
241
242 dbus_bool_t dbus_connection_add_filter    (DBusConnection            *connection,
243                                            DBusHandleMessageFunction  function,
244                                            void                      *user_data,
245                                            DBusFreeFunction           free_data_function);
246 void        dbus_connection_remove_filter (DBusConnection            *connection,
247                                            DBusHandleMessageFunction  function,
248                                            void                      *user_data);
249
250
251 /* Other */
252 dbus_bool_t dbus_connection_allocate_data_slot (dbus_int32_t     *slot_p);
253 void        dbus_connection_free_data_slot     (dbus_int32_t     *slot_p);
254 dbus_bool_t dbus_connection_set_data           (DBusConnection   *connection,
255                                                 dbus_int32_t      slot,
256                                                 void             *data,
257                                                 DBusFreeFunction  free_data_func);
258 void*       dbus_connection_get_data           (DBusConnection   *connection,
259                                                 dbus_int32_t      slot);
260
261 void        dbus_connection_set_change_sigpipe (dbus_bool_t       will_modify_sigpipe); 
262
263 void dbus_connection_set_max_message_size  (DBusConnection *connection,
264                                             long            size);
265 long dbus_connection_get_max_message_size  (DBusConnection *connection);
266 void dbus_connection_set_max_received_size (DBusConnection *connection,
267                                             long            size);
268 long dbus_connection_get_max_received_size (DBusConnection *connection);
269 long dbus_connection_get_outgoing_size     (DBusConnection *connection);
270
271 DBusPreallocatedSend* dbus_connection_preallocate_send       (DBusConnection       *connection);
272 void                  dbus_connection_free_preallocated_send (DBusConnection       *connection,
273                                                               DBusPreallocatedSend *preallocated);
274 void                  dbus_connection_send_preallocated      (DBusConnection       *connection,
275                                                               DBusPreallocatedSend *preallocated,
276                                                               DBusMessage          *message,
277                                                               dbus_uint32_t        *client_serial);
278
279
280 /* Object tree functionality */
281
282 /**
283  * Called when a #DBusObjectPathVTable is unregistered (or its connection is freed).
284  * Found in #DBusObjectPathVTable.
285  */
286 typedef void              (* DBusObjectPathUnregisterFunction) (DBusConnection  *connection,
287                                                                 void            *user_data);
288 /**
289  * Called when a message is sent to a registered object path. Found in
290  * #DBusObjectPathVTable which is registered with dbus_connection_register_object_path()
291  * or dbus_connection_register_fallback().
292  */
293 typedef DBusHandlerResult (* DBusObjectPathMessageFunction)    (DBusConnection  *connection,
294                                                                 DBusMessage     *message,
295                                                                 void            *user_data);
296
297 /**
298  * Virtual table that must be implemented to handle a portion of the
299  * object path hierarchy. Attach the vtable to a particular path using
300  * dbus_connection_register_object_path() or
301  * dbus_connection_register_fallback().
302  */
303 struct DBusObjectPathVTable
304 {
305   DBusObjectPathUnregisterFunction   unregister_function; /**< Function to unregister this handler */
306   DBusObjectPathMessageFunction      message_function; /**< Function to handle messages */
307   
308   void (* dbus_internal_pad1) (void *); /**< Reserved for future expansion */
309   void (* dbus_internal_pad2) (void *); /**< Reserved for future expansion */
310   void (* dbus_internal_pad3) (void *); /**< Reserved for future expansion */
311   void (* dbus_internal_pad4) (void *); /**< Reserved for future expansion */
312 };
313
314 dbus_bool_t dbus_connection_register_object_path   (DBusConnection              *connection,
315                                                     const char                  *path,
316                                                     const DBusObjectPathVTable  *vtable,
317                                                     void                        *user_data);
318 dbus_bool_t dbus_connection_register_fallback      (DBusConnection              *connection,
319                                                     const char                  *path,
320                                                     const DBusObjectPathVTable  *vtable,
321                                                     void                        *user_data);
322 dbus_bool_t dbus_connection_unregister_object_path (DBusConnection              *connection,
323                                                     const char                  *path);
324
325 dbus_bool_t dbus_connection_get_object_path_data   (DBusConnection              *connection,
326                                                     const char                  *path,
327                                                     void                       **data_p);
328
329 dbus_bool_t dbus_connection_list_registered        (DBusConnection              *connection,
330                                                     const char                  *parent_path,
331                                                     char                      ***child_entries);
332
333 dbus_bool_t dbus_connection_get_unix_fd            (DBusConnection              *connection,
334                                                     int                         *fd);
335 dbus_bool_t dbus_connection_get_socket             (DBusConnection              *connection,
336                                                     int                         *fd);
337
338 char*       dbus_get_local_machine_id              (void);
339
340 /** @} */
341
342 DBUS_END_DECLS
343
344 #endif /* DBUS_CONNECTION_H */