summaryrefslogtreecommitdiff
path: root/watch.c
blob: 9602f3900190806a8875a10062ed68ae37cf6520 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- */

/*  Libnul - Another Utility Library
 *  Copyright (C) 2002-2008  Søren Sandmann
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the 
 *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA  02111-1307, USA.
 */

#include <glib.h>
#include "libnul.h"

typedef struct Watch Watch;

struct Watch
{
    GSource source;
    GPollFD poll_fd;
    gboolean removed;
    
    WatchCallback read_callback;
    WatchCallback write_callback;
    WatchCallback hangup_callback;
    WatchCallback error_callback;
    WatchCallback priority_callback;
    WatchCallback poll_callback; /* Called before going back to poll */

    gpointer data;
};

static GHashTable *watched_fds;

static void
init (void)
{
    if (!watched_fds)
        watched_fds = g_hash_table_new (g_int_hash, g_int_equal);
}

static Watch *
lookup_watch (gint fd)
{
    init ();
    
    return g_hash_table_lookup (watched_fds, &fd);
}

static void
internal_add_watch (Watch *watch)
{
    gpointer fd = &(watch->poll_fd.fd);
    
    init ();
    
    g_hash_table_insert (watched_fds, fd, watch);
    g_source_add_poll ((GSource *)watch, &(watch->poll_fd));
}

static void
internal_remove_watch (Watch *watch)
{
    gpointer fd = &(watch->poll_fd.fd);
    
    init ();
    
    watch->removed = TRUE;
    g_source_remove_poll ((GSource *)watch, &(watch->poll_fd));
    g_hash_table_remove (watched_fds, fd);
}

static gboolean
watch_prepare (GSource *source,
               gint    *timeout)
{
    Watch *watch = (Watch *)source;
    
    *timeout = -1;
    
    if (watch->poll_callback)
        watch->poll_callback (watch->data);
    
    return FALSE;
}

static gboolean
watch_check (GSource *source)
{
    Watch *watch = (Watch *)source;
    gint revents = watch->poll_fd.revents;
    
    if (revents & (G_IO_NVAL))
    {
        /* This can happen if the user closes the file descriptor
         * without first removing the watch. We silently ignore it
         */
        internal_remove_watch (watch);
        g_source_unref (source);
        return FALSE;
    }
    
    if ((revents & G_IO_HUP) && watch->hangup_callback)
        return TRUE;
    
    if ((revents & G_IO_IN) && watch->read_callback)
        return TRUE;
    
    if ((revents & G_IO_PRI) && watch->priority_callback)
        return TRUE;
    
    if ((revents & G_IO_ERR) && watch->error_callback)
        return TRUE;
    
    if ((revents & G_IO_OUT) && watch->write_callback)
        return TRUE;
    
    return FALSE;
}

static gboolean
watch_dispatch (GSource     *source,
                GSourceFunc  callback,
                gpointer     user_data)
{
    Watch *watch = (Watch *)source;
    gint revents = watch->poll_fd.revents;
    gboolean removed;
    
    g_source_ref (source);
    
    if (!watch->removed && (revents & G_IO_IN)  && watch->read_callback)
        watch->read_callback (watch->data);
    
    if (!watch->removed && (revents & G_IO_PRI) && watch->priority_callback)
        watch->priority_callback (watch->data);
    
    if (!watch->removed && (revents & G_IO_OUT) && watch->write_callback)
        watch->write_callback (watch->data);
    
    if (!watch->removed && (revents & G_IO_ERR) && watch->error_callback)
        watch->error_callback (watch->data);
    
    if (!watch->removed && (revents & G_IO_HUP) && watch->hangup_callback)
        watch->hangup_callback (watch->data);
    
    removed = watch->removed;
    
    g_source_unref (source);
    
    if (removed)
        return FALSE;
    
    return TRUE;
}

static void
watch_finalize (GSource *source)
{
}

static void
update_poll_mask (Watch *watch)
{
    gint events = 0;
    
    g_source_remove_poll ((GSource *)watch, &(watch->poll_fd));
    
    if (watch->read_callback)
        events |= G_IO_IN;
    
    if (watch->write_callback)
        events |= G_IO_OUT;
    
    if (watch->priority_callback)
        events |= G_IO_PRI;
    
    if (watch->hangup_callback)
        events |= G_IO_HUP;
    
    if (watch->error_callback)
        events |= G_IO_ERR;
    
    watch->poll_fd.events = events;
    
    g_source_add_poll ((GSource *)watch, &(watch->poll_fd));
}

void
nul_fd_add_watch (gint     fd,
              gpointer data)
{
    static GSourceFuncs watch_funcs = {
        watch_prepare,
        watch_check,
        watch_dispatch,
        watch_finalize,
        NULL,
    };
    Watch *watch = lookup_watch (fd);
    
    g_return_if_fail (fd > 0);
    g_return_if_fail (watch == NULL);
    
    watch = (Watch *)g_source_new (&watch_funcs, sizeof (Watch)); 
    g_source_set_can_recurse ((GSource *)watch, TRUE);
    g_source_attach ((GSource *)watch, NULL);
    
    watch->poll_fd.fd = fd;
    watch->poll_fd.events = 0;
    watch->removed = FALSE;
    
    watch->read_callback = NULL;
    watch->write_callback = NULL;
    watch->hangup_callback = NULL;
    watch->error_callback = NULL;
    watch->priority_callback = NULL;
    
    watch->data = data;
    
    internal_add_watch (watch);
}

gpointer
nul_fd_get_data (gint fd)
{
    Watch *watch = lookup_watch (fd);
    
    g_return_val_if_fail (fd > 0, NULL);
    g_return_val_if_fail (watch != NULL, NULL);

    return watch->data;
}

void
nul_fd_set_data (gint     fd,
             gpointer data)
{
    Watch *watch = lookup_watch (fd);
    
    g_return_if_fail (fd > 0);
    g_return_if_fail (watch != NULL);

    watch->data = data;
}

void
nul_fd_set_read_callback (gint             fd,
                      WatchCallback read_cb)
{
    Watch *watch = lookup_watch (fd);
    
    g_return_if_fail (fd > 0);
    g_return_if_fail (watch != NULL);
    
    if (watch->read_callback != read_cb)
    {
        watch->read_callback = read_cb;
        update_poll_mask (watch);
    }
}

void
nul_fd_set_write_callback (gint             fd,
                       WatchCallback write_cb)
{
    Watch *watch = lookup_watch (fd);
    
    g_return_if_fail (fd > 0);
    g_return_if_fail (watch != NULL);
    
    if (watch->write_callback != write_cb)
    {
        watch->write_callback = write_cb;
        update_poll_mask (watch);
    }
}

void
nul_fd_set_hangup_callback (gint             fd,
                        WatchCallback hangup_cb)
{
    Watch *watch = lookup_watch (fd);
    
    g_return_if_fail (fd > 0);
    g_return_if_fail (watch != NULL);
    
    if (watch->hangup_callback != hangup_cb)
    {
        watch->hangup_callback = hangup_cb;
        update_poll_mask (watch);
    }
}

void
nul_fd_set_error_callback (gint             fd,
                       WatchCallback error_cb)
{
    Watch *watch = lookup_watch (fd);
    
    g_return_if_fail (fd > 0);
    g_return_if_fail (watch != NULL);
    
    if (watch->error_callback != error_cb)
    {
        watch->error_callback = error_cb;
        update_poll_mask (watch);
    }
}

void
nul_fd_set_priority_callback (gint             fd,
                          WatchCallback priority_cb)
{
    Watch *watch = lookup_watch (fd);
    
    g_return_if_fail (fd > 0);
    g_return_if_fail (watch != NULL);
    
    if (watch->priority_callback != priority_cb)
    {
        watch->priority_callback = priority_cb;
        update_poll_mask (watch);
    }
}

void
nul_fd_set_poll_handler (gint	   fd,
                     WatchCallback poll_cb)
{
    Watch *watch = lookup_watch (fd);

    g_return_if_fail (fd > 0);
    g_return_if_fail (watch != NULL);

    if (watch->poll_callback != poll_cb)
    {
        watch->poll_callback = poll_cb;
        update_poll_mask (watch);
    }
}

void
nul_fd_remove_watch (gint fd)
{
    Watch *watch = lookup_watch (fd);
    
    g_return_if_fail (fd > 0);
    
    if (!watch)
        return;
    
    internal_remove_watch (watch);
    g_source_unref ((GSource *)watch);
}

gboolean
nul_fd_is_watched (gint fd)
{
    g_return_val_if_fail (fd > 0, FALSE);
    
    if (lookup_watch (fd))
        return TRUE;
    else
        return FALSE;
}