blob: ca1494c87b3b6c8fb20e36fcee07fd1da08a4c5e [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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022/* events that may be observed */
23#define FDE_READ 0x0001
24#define FDE_WRITE 0x0002
25#define FDE_ERROR 0x0004
26
27/* features that may be set (via the events set/add/del interface) */
28#define FDE_DONT_CLOSE 0x0080
29
Elliott Hughes2d4121c2015-04-17 09:47:42 -070030struct fdevent;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031
32typedef void (*fd_func)(int fd, unsigned events, void *userdata);
33
34/* Allocate and initialize a new fdevent object
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020035 * Note: use FD_TIMER as 'fd' to create a fd-less object
36 * (used to implement timers).
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037*/
38fdevent *fdevent_create(int fd, fd_func func, void *arg);
39
40/* Uninitialize and deallocate an fdevent object that was
41** created by fdevent_create()
42*/
43void fdevent_destroy(fdevent *fde);
44
45/* Initialize an fdevent object that was externally allocated
46*/
47void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);
48
49/* Uninitialize an fdevent object that was initialized by
50** fdevent_install()
51*/
52void fdevent_remove(fdevent *item);
53
54/* Change which events should cause notifications
55*/
56void fdevent_set(fdevent *fde, unsigned events);
57void fdevent_add(fdevent *fde, unsigned events);
58void fdevent_del(fdevent *fde, unsigned events);
59
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020060void fdevent_set_timeout(fdevent *fde, int64_t timeout_ms);
61
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062/* loop forever, handling events.
63*/
64void fdevent_loop();
65
Dan Albert630b9af2014-11-24 23:34:35 -080066struct fdevent {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067 fdevent *next;
68 fdevent *prev;
69
70 int fd;
JP Abgrall408fa572011-03-16 15:57:42 -070071 int force_eof;
72
Dan Albert630b9af2014-11-24 23:34:35 -080073 uint16_t state;
74 uint16_t events;
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020075
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076 fd_func func;
77 void *arg;
78};
79
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080#endif