The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 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' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 20 | #include <stdint.h> /* for int64_t */ |
| 21 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 22 | /* events that may be observed */ |
| 23 | #define FDE_READ 0x0001 |
| 24 | #define FDE_WRITE 0x0002 |
| 25 | #define FDE_ERROR 0x0004 |
David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 26 | #define FDE_TIMEOUT 0x0008 |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | |
| 28 | /* features that may be set (via the events set/add/del interface) */ |
| 29 | #define FDE_DONT_CLOSE 0x0080 |
| 30 | |
Elliott Hughes | 2d4121c | 2015-04-17 09:47:42 -0700 | [diff] [blame] | 31 | struct fdevent; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 32 | |
| 33 | typedef void (*fd_func)(int fd, unsigned events, void *userdata); |
| 34 | |
| 35 | /* Allocate and initialize a new fdevent object |
David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 36 | * Note: use FD_TIMER as 'fd' to create a fd-less object |
| 37 | * (used to implement timers). |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 38 | */ |
| 39 | fdevent *fdevent_create(int fd, fd_func func, void *arg); |
| 40 | |
| 41 | /* Uninitialize and deallocate an fdevent object that was |
| 42 | ** created by fdevent_create() |
| 43 | */ |
| 44 | void fdevent_destroy(fdevent *fde); |
| 45 | |
| 46 | /* Initialize an fdevent object that was externally allocated |
| 47 | */ |
| 48 | void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg); |
| 49 | |
| 50 | /* Uninitialize an fdevent object that was initialized by |
| 51 | ** fdevent_install() |
| 52 | */ |
| 53 | void fdevent_remove(fdevent *item); |
| 54 | |
| 55 | /* Change which events should cause notifications |
| 56 | */ |
| 57 | void fdevent_set(fdevent *fde, unsigned events); |
| 58 | void fdevent_add(fdevent *fde, unsigned events); |
| 59 | void fdevent_del(fdevent *fde, unsigned events); |
| 60 | |
David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 61 | void fdevent_set_timeout(fdevent *fde, int64_t timeout_ms); |
| 62 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 63 | /* loop forever, handling events. |
| 64 | */ |
| 65 | void fdevent_loop(); |
| 66 | |
Dan Albert | 630b9af | 2014-11-24 23:34:35 -0800 | [diff] [blame] | 67 | struct fdevent { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 68 | fdevent *next; |
| 69 | fdevent *prev; |
| 70 | |
| 71 | int fd; |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 72 | int force_eof; |
| 73 | |
Dan Albert | 630b9af | 2014-11-24 23:34:35 -0800 | [diff] [blame] | 74 | uint16_t state; |
| 75 | uint16_t events; |
David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 76 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 77 | fd_func func; |
| 78 | void *arg; |
| 79 | }; |
| 80 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 81 | #endif |