]> git.meshlink.io Git - catta/blob - include/catta/watch.h
fix headers to be used with Visual Studio 2015 RC
[catta] / include / catta / watch.h
1 #ifndef foowatchhfoo
2 #define foowatchhfoo
3
4 /***
5   This file is part of catta.
6
7   catta is free software; you can redistribute it and/or modify it
8   under the terms of the GNU Lesser General Public License as
9   published by the Free Software Foundation; either version 2.1 of the
10   License, or (at your option) any later version.
11
12   catta is distributed in the hope that it will be useful, but WITHOUT
13   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
15   Public License for more details.
16
17   You should have received a copy of the GNU Lesser General Public
18   License along with catta; if not, write to the Free Software
19   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20   USA.
21 ***/
22
23 /** \file watch.h Simplistic main loop abstraction */
24
25 #include <sys/poll.h>
26 #ifndef _MSC_VER
27 #include <sys/time.h>
28 #endif
29
30 #include <catta/cdecl.h>
31
32 CATTA_C_DECL_BEGIN
33
34 /** An I/O watch object */
35 typedef struct CattaWatch CattaWatch;
36
37 /** A timeout watch object */
38 typedef struct CattaTimeout CattaTimeout;
39
40 /** An event polling abstraction object */
41 typedef struct CattaPoll CattaPoll;
42
43 /** Type of watch events */
44 typedef enum {
45     CATTA_WATCH_IN = POLLIN,      /**< Input event */
46     CATTA_WATCH_OUT = POLLOUT,    /**< Output event */
47     CATTA_WATCH_ERR = POLLERR,    /**< Error event */
48     CATTA_WATCH_HUP = POLLHUP     /**< Hangup event */
49 } CattaWatchEvent;
50
51 /** Called whenever an I/O event happens  on an I/O watch */
52 typedef void (*CattaWatchCallback)(CattaWatch *w, int fd, CattaWatchEvent event, void *userdata);
53
54 /** Called when the timeout is reached */
55 typedef void (*CattaTimeoutCallback)(CattaTimeout *t, void *userdata);
56
57 /** Defines an abstracted event polling API. This may be used to
58  connect Catta to other main loops. This is loosely based on Unix
59  poll(2). A consumer will call watch_new() for all file descriptors it
60  wants to listen for events on. In addition he can call timeout_new()
61  to define time based events .*/
62 struct CattaPoll {
63
64     /** Some abstract user data usable by the provider of the API */
65     void* userdata;
66
67     /** Create a new watch for the specified file descriptor and for
68      * the specified events. The API will call the callback function
69      * whenever any of the events happens. */
70     CattaWatch* (*watch_new)(const CattaPoll *api, int fd, CattaWatchEvent event, CattaWatchCallback callback, void *userdata);
71
72     /** Update the events to wait for. It is safe to call this function from an CattaWatchCallback */
73     void (*watch_update)(CattaWatch *w, CattaWatchEvent event);
74
75     /** Return the events that happened. It is safe to call this function from an CattaWatchCallback  */
76     CattaWatchEvent (*watch_get_events)(CattaWatch *w);
77
78     /** Free a watch. It is safe to call this function from an CattaWatchCallback */
79     void (*watch_free)(CattaWatch *w);
80
81     /** Set a wakeup time for the polling loop. The API will call the
82     callback function when the absolute time *tv is reached. If tv is
83     NULL, the timeout is disabled. After the timeout expired the
84     callback function will be called and the timeout is disabled. You
85     can reenable it by calling timeout_update()  */
86     CattaTimeout* (*timeout_new)(const CattaPoll *api, const struct timeval *tv, CattaTimeoutCallback callback, void *userdata);
87
88     /** Update the absolute expiration time for a timeout, If tv is
89      * NULL, the timeout is disabled. It is safe to call this function from an CattaTimeoutCallback */
90     void (*timeout_update)(CattaTimeout *, const struct timeval *tv);
91
92     /** Free a timeout. It is safe to call this function from an CattaTimeoutCallback */
93     void (*timeout_free)(CattaTimeout *t);
94 };
95
96 CATTA_C_DECL_END
97
98 #endif
99