robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Barrett Ruthfeat(channel): add ChanClose event #40568d0a262db88

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