1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-watch.c DBusWatch implementation
4 * Copyright (C) 2002 Red Hat Inc.
6 * Licensed under the Academic Free License version 1.2
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.
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.
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
24 #include "dbus-internals.h"
25 #include "dbus-watch.h"
26 #include "dbus-list.h"
29 * @defgroup DBusWatchInternals DBusWatch implementation details
30 * @ingroup DBusInternals
31 * @brief implementation details for DBusWatch
38 int refcount; /**< Reference count */
39 int fd; /**< File descriptor. */
40 unsigned int flags; /**< Conditions to watch. */
41 void *data; /**< Application data. */
42 DBusFreeFunction free_data_function; /**< Free the application data. */
46 * Creates a new DBusWatch. Normally used by a DBusTransport
48 * @param fd the file descriptor to be watched.
49 * @param flags the conditions to watch for on the descriptor.
50 * @returns the new DBusWatch object.
53 _dbus_watch_new (int fd,
58 #define VALID_WATCH_FLAGS (DBUS_WATCH_WRITABLE | DBUS_WATCH_READABLE)
60 _dbus_assert ((flags & VALID_WATCH_FLAGS) == flags);
62 watch = dbus_new0 (DBusWatch, 1);
71 * Increments the reference count of a DBusWatch object.
73 * @param watch the watch object.
76 _dbus_watch_ref (DBusWatch *watch)
82 * Decrements the reference count of a DBusWatch object
83 * and finalizes the object if the count reaches zero.
85 * @param watch the watch object.
88 _dbus_watch_unref (DBusWatch *watch)
90 _dbus_assert (watch != NULL);
91 _dbus_assert (watch->refcount > 0);
94 if (watch->refcount == 0)
96 dbus_watch_set_data (watch, NULL, NULL); /* call free_data_function */
102 * Clears the file descriptor from a now-invalid watch object so that
103 * no one tries to use it. This is because a watch may stay alive due
104 * to reference counts after the file descriptor is closed.
105 * Invalidation makes it easier to catch bugs. It also
106 * keeps people from doing dorky things like assuming file descriptors
107 * are unique (never recycled).
109 * @param watch the watch object.
112 _dbus_watch_invalidate (DBusWatch *watch)
119 * Sanitizes the given condition so that it only contains
120 * flags that the DBusWatch requested. e.g. if the
121 * watch is a DBUS_WATCH_READABLE watch then
122 * DBUS_WATCH_WRITABLE will be stripped from the condition.
124 * @param watch the watch object.
125 * @param condition address of the condition to sanitize.
128 _dbus_watch_sanitize_condition (DBusWatch *watch,
129 unsigned int *condition)
131 if (!(watch->flags & DBUS_WATCH_READABLE))
132 *condition &= ~DBUS_WATCH_READABLE;
133 if (!(watch->flags & DBUS_WATCH_WRITABLE))
134 *condition &= ~DBUS_WATCH_WRITABLE;
139 * @typedef DBusWatchList
141 * Opaque data type representing a list of watches
142 * and a set of DBusAddWatchFunction/DBusRemoveWatchFunction.
143 * Automatically handles removing/re-adding watches
144 * when the DBusAddWatchFunction is updated or changed.
145 * Holds a reference count to each watch.
147 * Used in the implementation of both DBusServer and
153 * DBusWatchList implementation details. All fields
159 DBusList *watches; /**< Watch objects. */
161 DBusAddWatchFunction add_watch_function; /**< Callback for adding a watch. */
162 DBusAddWatchFunction remove_watch_function; /**< Callback for removing a watch. */
163 void *watch_data; /**< Data for watch callbacks */
164 DBusFreeFunction watch_free_data_function; /**< Free function for watch callback data */
168 * Creates a new watch list. Returns #NULL if insufficient
171 * @returns the new watch list, or #NULL on failure.
174 _dbus_watch_list_new (void)
176 DBusWatchList *watch_list;
178 watch_list = dbus_new0 (DBusWatchList, 1);
179 if (watch_list == NULL)
186 * Frees a DBusWatchList.
188 * @param watch_list the watch list.
191 _dbus_watch_list_free (DBusWatchList *watch_list)
193 /* free watch_data as a side effect */
194 _dbus_watch_list_set_functions (watch_list,
195 NULL, NULL, NULL, NULL);
197 _dbus_list_foreach (&watch_list->watches,
198 (DBusForeachFunction) _dbus_watch_unref,
200 _dbus_list_clear (&watch_list->watches);
202 dbus_free (watch_list);
206 * Sets the watch functions. This function is the "backend"
207 * for dbus_connection_set_watch_functions() and
208 * dbus_server_set_watch_functions().
210 * @param watch_list the watch list.
211 * @param add_function the add watch function.
212 * @param remove_function the remove watch function.
213 * @param data the data for those functions.
214 * @param free_data_function the function to free the data.
218 _dbus_watch_list_set_functions (DBusWatchList *watch_list,
219 DBusAddWatchFunction add_function,
220 DBusRemoveWatchFunction remove_function,
222 DBusFreeFunction free_data_function)
224 /* Remove all current watches from previous watch handlers */
226 if (watch_list->remove_watch_function != NULL)
228 _dbus_list_foreach (&watch_list->watches,
229 (DBusForeachFunction) watch_list->remove_watch_function,
230 watch_list->watch_data);
233 if (watch_list->watch_free_data_function != NULL)
234 (* watch_list->watch_free_data_function) (watch_list->watch_data);
236 watch_list->add_watch_function = add_function;
237 watch_list->remove_watch_function = remove_function;
238 watch_list->watch_data = data;
239 watch_list->watch_free_data_function = free_data_function;
241 /* Re-add all pending watches */
242 if (watch_list->add_watch_function != NULL)
244 _dbus_list_foreach (&watch_list->watches,
245 (DBusForeachFunction) watch_list->add_watch_function,
246 watch_list->watch_data);
251 * Adds a new watch to the watch list, invoking the
252 * application DBusAddWatchFunction if appropriate.
254 * @param watch_list the watch list.
255 * @param watch the watch to add.
256 * @returns #TRUE on success, #FALSE if no memory.
259 _dbus_watch_list_add_watch (DBusWatchList *watch_list,
262 if (!_dbus_list_append (&watch_list->watches, watch))
265 _dbus_watch_ref (watch);
267 if (watch_list->add_watch_function != NULL)
268 (* watch_list->add_watch_function) (watch,
269 watch_list->watch_data);
275 * Removes a watch from the watch list, invoking the
276 * application's DBusRemoveWatchFunction if appropriate.
278 * @param watch_list the watch list.
279 * @param watch the watch to remove.
282 _dbus_watch_list_remove_watch (DBusWatchList *watch_list,
285 if (!_dbus_list_remove (&watch_list->watches, watch))
286 _dbus_assert_not_reached ("Nonexistent watch was removed");
288 if (watch_list->remove_watch_function != NULL)
289 (* watch_list->remove_watch_function) (watch,
290 watch_list->watch_data);
292 _dbus_watch_unref (watch);
298 * @defgroup DBusWatch DBusWatch
300 * @brief Object representing a file descriptor to be watched.
302 * Types and functions related to DBusWatch. A watch represents
303 * a file descriptor that the main loop needs to monitor,
304 * as in Qt's QSocketNotifier or GLib's g_io_add_watch().
312 * Opaque object representing a file descriptor
313 * to be watched for changes in readability,
314 * writability, or hangup.
318 * Gets the file descriptor that should be watched.
320 * @param watch the DBusWatch object.
321 * @returns the file descriptor to watch.
324 dbus_watch_get_fd (DBusWatch *watch)
330 * Gets flags from DBusWatchFlags indicating
331 * what conditions should be monitored on the
334 * @param watch the DBusWatch object.
335 * @returns the conditions to watch.
338 dbus_watch_get_flags (DBusWatch *watch)
340 _dbus_assert ((watch->flags & VALID_WATCH_FLAGS) == watch->flags);
346 * Gets data previously set with dbus_watch_set_data()
349 * @param watch the DBusWatch object.
350 * @returns previously-set data.
353 dbus_watch_get_data (DBusWatch *watch)
359 * Sets data which can be retrieved with dbus_watch_get_data().
360 * Intended for use by the DBusAddWatchFunction and
361 * DBusRemoveWatchFunction to store their own data. For example with
362 * Qt you might store the QSocketNotifier for this watch and with GLib
363 * you might store a GSource.
365 * @param watch the DBusWatch object.
366 * @param data the data.
367 * @param free_data_function function to be called to free the data.
370 dbus_watch_set_data (DBusWatch *watch,
372 DBusFreeFunction free_data_function)
374 if (watch->free_data_function != NULL)
375 (* watch->free_data_function) (watch->data);
378 watch->free_data_function = free_data_function;