robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
d303617132
master
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 11enum {EVENT_HANDLER_MAX_ARGC = 10 , }; 12 13typedef void (* argv_callback )(void ** argv ); 14typedef struct { 15argv_callback handler ; 16void * argv [EVENT_HANDLER_MAX_ARGC ]; 17}Event ; 18 19#define event_create (cb , ...) ((Event){ .handler = cb, .argv = { __VA_ARGS__ } }) 20 21typedef struct multiqueue MultiQueue ; 22typedef void (* PutCallback )(MultiQueue * multiq ,void * data ); 23 24typedef struct signal_watcher SignalWatcher ; 25typedef void (* signal_cb )(SignalWatcher * watcher ,int signum ,void * data ); 26typedef void (* signal_close_cb )(SignalWatcher * watcher ,void * data ); 27 28struct signal_watcher { 29uv_signal_t uv ; 30void * data ; 31signal_cb cb ; 32signal_close_cb close_cb ; 33MultiQueue * events ; 34}; 35 36typedef struct time_watcher TimeWatcher ; 37typedef void (* time_cb )(TimeWatcher * watcher ,void * data ); 38 39struct time_watcher { 40uv_timer_t uv ; 41void * data ; 42time_cb cb ,close_cb ; 43MultiQueue * events ; 44bool blockable ; 45}; 46 47typedef struct wbuffer WBuffer ; 48typedef void (* wbuffer_data_finalizer )(void * data ); 49 50struct wbuffer { 51size_t size ,refcount ; 52char * data ; 53wbuffer_data_finalizer cb ; 54}; 55 56typedef struct stream Stream ; 57typedef 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 66typedef size_t (* stream_read_cb )(RStream * stream ,const char * read_data ,size_t count ,void * data , 67bool 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 75typedef void (* stream_write_cb )(Stream * stream ,void * data ,int status ); 76 77typedef void (* stream_close_cb )(Stream * stream ,void * data ); 78 79struct stream { 80bool closed ; 81union { 82uv_pipe_t pipe ; 83uv_tcp_t tcp ; 84uv_idle_t idle ; 85#ifdef MSWIN 86uv_tty_t tty ; 87#endif 88 }uv ; 89uv_stream_t * uvstream ;///< NULL when the stream is a file 90uv_file fd ;///< When the stream is a file, this is its file descriptor 91int64_t fpos ;///< When the stream is a file, this is the position in file 92void * cb_data ; 93stream_close_cb before_close_cb ,close_cb ,internal_close_cb ; 94void * close_cb_data ,* internal_data ; 95size_t pending_reqs ; 96MultiQueue * events ; 97 98// only used for writing: 99stream_write_cb write_cb ; 100size_t curmem ; 101size_t maxmem ; 102}; 103 104struct rstream { 105Stream s ; 106bool did_eof ; 107bool want_read ; 108bool pending_read ; 109bool paused_full ; 110char * buffer ;// ARENA_BLOCK_SIZE 111char * read_pos ; 112char * write_pos ; 113uv_buf_t uvbuf ; 114stream_read_cb read_cb ; 115size_t num_bytes ; 116}; 117 118#define ADDRESS_MAX_SIZE 256 119 120typedef struct socket_watcher SocketWatcher ; 121typedef void (* socket_cb )(SocketWatcher * watcher ,int result ,void * data ); 122typedef void (* socket_close_cb )(SocketWatcher * watcher ,void * data ); 123 124struct socket_watcher { 125// Pipe/socket path, or TCP address string 126char addr [ADDRESS_MAX_SIZE ]; 127// TCP server or unix socket (named pipe on Windows) 128union { 129struct { 130uv_tcp_t handle ; 131struct addrinfo * addrinfo ; 132 }tcp ; 133struct { 134uv_pipe_t handle ; 135 }pipe ; 136 }uv ; 137uv_stream_t * stream ; 138void * data ; 139socket_cb cb ; 140socket_close_cb close_cb ; 141MultiQueue * events ; 142}; 143 144typedef enum { 145kProcTypeUv , 146kProcTypePty , 147}ProcType ; 148 149/// OS process 150typedef struct proc Proc ; 151typedef void (* proc_exit_cb )(Proc * proc ,int status ,void * data ); 152typedef void (* proc_state_cb )(Proc * proc ,bool suspended ,void * data ); 153typedef void (* internal_proc_cb )(Proc * proc ); 154 155struct proc { 156ProcType type ; 157Loop * loop ; 158void * data ; 159int pid ,status ,refcount ; 160uint8_t exit_signal ;// Signal used when killing (on Windows). 161uint64_t stopped_time ;// proc_stop() timestamp 162const char * cwd ; 163char ** argv ; 164const char * exepath ; 165dict_T * env ; 166Stream in ; 167RStream out ,err ; 168/// Exit handler. If set, user must call proc_free(). 169proc_exit_cb cb ; 170proc_state_cb state_cb ; 171internal_proc_cb internal_exit_cb ,internal_close_cb ; 172bool closed ,detach ,overlapped ,fwd_err ; 173bool stdio_noinherit ;///< MSWIN: don't share parent console with child (prevent leaking to TUI). 174///< No equivalent on Unix, where inherited stdio is a pipe, not terminal. 175MultiQueue * events ; 176};