defs.h (4455B)
1 #pragma once 2 3 #include <assert.h> 4 #include <stdarg.h> 5 #include <stdbool.h> 6 #include <uv.h> 7 8 #include "nvim/eval/typval_defs.h" 9 #include "nvim/types_defs.h" 10 11 enum { EVENT_HANDLER_MAX_ARGC = 10, }; 12 13 typedef void (*argv_callback)(void **argv); 14 typedef struct { 15 argv_callback handler; 16 void *argv[EVENT_HANDLER_MAX_ARGC]; 17 } Event; 18 19 #define event_create(cb, ...) ((Event){ .handler = cb, .argv = { __VA_ARGS__ } }) 20 21 typedef struct multiqueue MultiQueue; 22 typedef void (*PutCallback)(MultiQueue *multiq, void *data); 23 24 typedef struct signal_watcher SignalWatcher; 25 typedef void (*signal_cb)(SignalWatcher *watcher, int signum, void *data); 26 typedef void (*signal_close_cb)(SignalWatcher *watcher, void *data); 27 28 struct signal_watcher { 29 uv_signal_t uv; 30 void *data; 31 signal_cb cb; 32 signal_close_cb close_cb; 33 MultiQueue *events; 34 }; 35 36 typedef struct time_watcher TimeWatcher; 37 typedef void (*time_cb)(TimeWatcher *watcher, void *data); 38 39 struct time_watcher { 40 uv_timer_t uv; 41 void *data; 42 time_cb cb, close_cb; 43 MultiQueue *events; 44 bool blockable; 45 }; 46 47 typedef struct wbuffer WBuffer; 48 typedef void (*wbuffer_data_finalizer)(void *data); 49 50 struct wbuffer { 51 size_t size, refcount; 52 char *data; 53 wbuffer_data_finalizer cb; 54 }; 55 56 typedef struct stream Stream; 57 typedef struct rstream RStream; 58 /// Type of function called when the RStream buffer is filled with data 59 /// 60 /// @param stream The Stream instance 61 /// @param read_data data that was read 62 /// @param count Number of bytes that was read. 63 /// @param data User-defined data 64 /// @param eof If the stream reached EOF. 65 /// @return number of bytes which were consumed 66 typedef size_t (*stream_read_cb)(RStream *stream, const char *read_data, size_t count, void *data, 67 bool eof); 68 69 /// Type of function called when the Stream has information about a write 70 /// request. 71 /// 72 /// @param stream The Stream instance 73 /// @param data User-defined data 74 /// @param status 0 on success, anything else indicates failure 75 typedef void (*stream_write_cb)(Stream *stream, void *data, int status); 76 77 typedef void (*stream_close_cb)(Stream *stream, void *data); 78 79 struct stream { 80 bool closed; 81 bool use_poll; 82 union { 83 uv_pipe_t pipe; 84 uv_tcp_t tcp; 85 uv_idle_t idle; 86 #ifdef MSWIN 87 uv_tty_t tty; 88 #else 89 uv_poll_t poll; 90 #endif 91 } uv; 92 uv_stream_t *uvstream; ///< NULL when the stream is a file 93 uv_file fd; ///< When the stream is a file, this is its file descriptor 94 int64_t fpos; ///< When the stream is a file, this is the position in file 95 void *cb_data; 96 stream_close_cb close_cb, internal_close_cb; 97 void *close_cb_data, *internal_data; 98 size_t pending_reqs; 99 MultiQueue *events; 100 101 // only used for writing: 102 stream_write_cb write_cb; 103 size_t curmem; 104 size_t maxmem; 105 }; 106 107 struct rstream { 108 Stream s; 109 bool did_eof; 110 bool want_read; 111 bool pending_read; 112 bool paused_full; 113 char *buffer; // ARENA_BLOCK_SIZE 114 char *read_pos; 115 char *write_pos; 116 uv_buf_t uvbuf; 117 stream_read_cb read_cb; 118 size_t num_bytes; 119 }; 120 121 #define ADDRESS_MAX_SIZE 256 122 123 typedef struct socket_watcher SocketWatcher; 124 typedef void (*socket_cb)(SocketWatcher *watcher, int result, void *data); 125 typedef void (*socket_close_cb)(SocketWatcher *watcher, void *data); 126 127 struct socket_watcher { 128 // Pipe/socket path, or TCP address string 129 char addr[ADDRESS_MAX_SIZE]; 130 // TCP server or unix socket (named pipe on Windows) 131 union { 132 struct { 133 uv_tcp_t handle; 134 struct addrinfo *addrinfo; 135 } tcp; 136 struct { 137 uv_pipe_t handle; 138 } pipe; 139 } uv; 140 uv_stream_t *stream; 141 void *data; 142 socket_cb cb; 143 socket_close_cb close_cb; 144 MultiQueue *events; 145 }; 146 147 typedef enum { 148 kProcTypeUv, 149 kProcTypePty, 150 } ProcType; 151 152 /// OS process 153 typedef struct proc Proc; 154 typedef void (*proc_exit_cb)(Proc *proc, int status, void *data); 155 typedef void (*proc_state_cb)(Proc *proc, bool suspended, void *data); 156 typedef void (*internal_proc_cb)(Proc *proc); 157 158 struct proc { 159 ProcType type; 160 Loop *loop; 161 void *data; 162 int pid, status, refcount; 163 uint8_t exit_signal; // Signal used when killing (on Windows). 164 uint64_t stopped_time; // proc_stop() timestamp 165 const char *cwd; 166 char **argv; 167 const char *exepath; 168 dict_T *env; 169 Stream in; 170 RStream out, err; 171 /// Exit handler. If set, user must call proc_free(). 172 proc_exit_cb cb; 173 proc_state_cb state_cb; 174 internal_proc_cb internal_exit_cb, internal_close_cb; 175 bool closed, detach, overlapped; 176 MultiQueue *events; 177 };