watch.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #ifndef foowatchhfoo
  2. #define foowatchhfoo
  3. /***
  4. This file is part of avahi.
  5. avahi is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU Lesser General Public License as
  7. published by the Free Software Foundation; either version 2.1 of the
  8. License, or (at your option) any later version.
  9. avahi is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
  12. Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with avahi; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  16. USA.
  17. ***/
  18. /** \file watch.h Simplistic main loop abstraction */
  19. #include <sys/poll.h>
  20. #include <sys/time.h>
  21. #include <avahi-common/cdecl.h>
  22. AVAHI_C_DECL_BEGIN
  23. /** An I/O watch object */
  24. typedef struct AvahiWatch AvahiWatch;
  25. /** A timeout watch object */
  26. typedef struct AvahiTimeout AvahiTimeout;
  27. /** An event polling abstraction object */
  28. typedef struct AvahiPoll AvahiPoll;
  29. /** Type of watch events */
  30. typedef enum {
  31. AVAHI_WATCH_IN = POLLIN, /**< Input event */
  32. AVAHI_WATCH_OUT = POLLOUT, /**< Output event */
  33. AVAHI_WATCH_ERR = POLLERR, /**< Error event */
  34. AVAHI_WATCH_HUP = POLLHUP /**< Hangup event */
  35. } AvahiWatchEvent;
  36. /** Called whenever an I/O event happens on an I/O watch */
  37. typedef void (*AvahiWatchCallback)(AvahiWatch *w, int fd, AvahiWatchEvent event, void *userdata);
  38. /** Called when the timeout is reached */
  39. typedef void (*AvahiTimeoutCallback)(AvahiTimeout *t, void *userdata);
  40. /** Defines an abstracted event polling API. This may be used to
  41. connect Avahi to other main loops. This is loosely based on Unix
  42. poll(2). A consumer will call watch_new() for all file descriptors it
  43. wants to listen for events on. In addition he can call timeout_new()
  44. to define time based events .*/
  45. struct AvahiPoll {
  46. /** Some abstract user data usable by the provider of the API */
  47. void* userdata;
  48. /** Create a new watch for the specified file descriptor and for
  49. * the specified events. The API will call the callback function
  50. * whenever any of the events happens. */
  51. AvahiWatch* (*watch_new)(const AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void *userdata);
  52. /** Update the events to wait for. It is safe to call this function from an AvahiWatchCallback */
  53. void (*watch_update)(AvahiWatch *w, AvahiWatchEvent event);
  54. /** Return the events that happened. It is safe to call this function from an AvahiWatchCallback */
  55. AvahiWatchEvent (*watch_get_events)(AvahiWatch *w);
  56. /** Free a watch. It is safe to call this function from an AvahiWatchCallback */
  57. void (*watch_free)(AvahiWatch *w);
  58. /** Set a wakeup time for the polling loop. The API will call the
  59. callback function when the absolute time *tv is reached. If tv is
  60. NULL, the timeout is disabled. After the timeout expired the
  61. callback function will be called and the timeout is disabled. You
  62. can reenable it by calling timeout_update() */
  63. AvahiTimeout* (*timeout_new)(const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback callback, void *userdata);
  64. /** Update the absolute expiration time for a timeout, If tv is
  65. * NULL, the timeout is disabled. It is safe to call this function from an AvahiTimeoutCallback */
  66. void (*timeout_update)(AvahiTimeout *, const struct timeval *tv);
  67. /** Free a timeout. It is safe to call this function from an AvahiTimeoutCallback */
  68. void (*timeout_free)(AvahiTimeout *t);
  69. };
  70. AVAHI_C_DECL_END
  71. #endif