neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

channel.h (2399B)


      1 #pragma once
      2 
      3 #include <stdbool.h>
      4 #include <stdint.h>
      5 #include <stdlib.h>
      6 
      7 #include "nvim/channel_defs.h"  // IWYU pragma: keep
      8 #include "nvim/eval/typval_defs.h"
      9 #include "nvim/event/defs.h"
     10 #include "nvim/event/libuv_proc.h"
     11 #include "nvim/macros_defs.h"
     12 #include "nvim/map_defs.h"
     13 #include "nvim/msgpack_rpc/channel_defs.h"
     14 #include "nvim/os/pty_proc.h"
     15 #include "nvim/types_defs.h"
     16 
     17 struct Channel {
     18  uint64_t id;
     19  size_t refcount;
     20  MultiQueue *events;
     21 
     22  ChannelStreamType streamtype;
     23  union {
     24    Proc proc;
     25    LibuvProc uv;
     26    PtyProc pty;
     27    RStream socket;
     28    StdioPair stdio;
     29    StderrState err;
     30    InternalState internal;
     31  } stream;
     32 
     33  bool is_rpc;
     34  bool detach;  ///< Prevents self-exit on channel-close. Normally, Nvim self-exits if its primary
     35                ///< RPC channel is closed, unless detach=true. Note: currently, detach=false does
     36                ///< not FORCE self-exit.
     37  RpcState rpc;
     38  Terminal *term;
     39 
     40  CallbackReader on_data;
     41  CallbackReader on_stderr;
     42  Callback on_exit;
     43  int exit_status;  ///< Process exit-code (if the channel wraps a process).
     44 
     45  bool callback_busy;
     46  bool callback_scheduled;
     47 };
     48 
     49 #include "channel.h.generated.h"
     50 #include "channel.h.inline.generated.h"
     51 
     52 static inline bool callback_reader_set(CallbackReader reader)
     53 {
     54  return reader.cb.type != kCallbackNone || reader.self;
     55 }
     56 
     57 EXTERN PMap(uint64_t) channels INIT( = MAP_INIT);
     58 
     59 EXTERN Callback on_print INIT( = CALLBACK_INIT);
     60 
     61 /// @returns Channel with the id or NULL if not found
     62 static inline Channel *find_channel(uint64_t id)
     63 {
     64  return (Channel *)pmap_get(uint64_t)(&channels, id);
     65 }
     66 
     67 static inline Stream *channel_instream(Channel *chan)
     68  FUNC_ATTR_NONNULL_ALL
     69 {
     70  switch (chan->streamtype) {
     71  case kChannelStreamProc:
     72    return &chan->stream.proc.in;
     73 
     74  case kChannelStreamSocket:
     75    return &chan->stream.socket.s;
     76 
     77  case kChannelStreamStdio:
     78    return &chan->stream.stdio.out;
     79 
     80  case kChannelStreamInternal:
     81  case kChannelStreamStderr:
     82    abort();
     83  }
     84  abort();
     85 }
     86 
     87 static inline RStream *channel_outstream(Channel *chan)
     88  FUNC_ATTR_NONNULL_ALL
     89 {
     90  switch (chan->streamtype) {
     91  case kChannelStreamProc:
     92    return &chan->stream.proc.out;
     93 
     94  case kChannelStreamSocket:
     95    return &chan->stream.socket;
     96 
     97  case kChannelStreamStdio:
     98    return &chan->stream.stdio.in;
     99 
    100  case kChannelStreamInternal:
    101  case kChannelStreamStderr:
    102    abort();
    103  }
    104  abort();
    105 }