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