blob: a8102caacbd954efba9c5f6ca47f7c10da707876 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef __FDEVENT_H
18#define __FDEVENT_H
19
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020020#include <stdint.h> /* for int64_t */
21
Dan Albert630b9af2014-11-24 23:34:35 -080022#ifdef __cplusplus
23extern "C" {
24#endif
25
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026/* events that may be observed */
27#define FDE_READ 0x0001
28#define FDE_WRITE 0x0002
29#define FDE_ERROR 0x0004
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020030#define FDE_TIMEOUT 0x0008
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031
32/* features that may be set (via the events set/add/del interface) */
33#define FDE_DONT_CLOSE 0x0080
34
35typedef struct fdevent fdevent;
36
37typedef void (*fd_func)(int fd, unsigned events, void *userdata);
38
39/* Allocate and initialize a new fdevent object
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020040 * Note: use FD_TIMER as 'fd' to create a fd-less object
41 * (used to implement timers).
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042*/
43fdevent *fdevent_create(int fd, fd_func func, void *arg);
44
45/* Uninitialize and deallocate an fdevent object that was
46** created by fdevent_create()
47*/
48void fdevent_destroy(fdevent *fde);
49
50/* Initialize an fdevent object that was externally allocated
51*/
52void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);
53
54/* Uninitialize an fdevent object that was initialized by
55** fdevent_install()
56*/
57void fdevent_remove(fdevent *item);
58
59/* Change which events should cause notifications
60*/
61void fdevent_set(fdevent *fde, unsigned events);
62void fdevent_add(fdevent *fde, unsigned events);
63void fdevent_del(fdevent *fde, unsigned events);
64
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020065void fdevent_set_timeout(fdevent *fde, int64_t timeout_ms);
66
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067/* loop forever, handling events.
68*/
69void fdevent_loop();
70
Dan Albert630b9af2014-11-24 23:34:35 -080071struct fdevent {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072 fdevent *next;
73 fdevent *prev;
74
75 int fd;
JP Abgrall408fa572011-03-16 15:57:42 -070076 int force_eof;
77
Dan Albert630b9af2014-11-24 23:34:35 -080078 uint16_t state;
79 uint16_t events;
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020080
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081 fd_func func;
82 void *arg;
83};
84
Dan Albert630b9af2014-11-24 23:34:35 -080085#ifdef __cplusplus
86}
87#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088
89#endif