ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
vnc_client.h
Go to the documentation of this file.
1#pragma once
2
7
8#include <cstddef>
9#include <cstdint>
10
11namespace esphome::vnc {
12
18
19enum class VncState : uint8_t {
23};
24
45
46enum class VncParse : uint8_t {
47 VNC_PARSE_MORE, // the next step needs bytes that have not arrived
48 VNC_PARSE_PAUSE, // out of time, or the display is busy; resume without new data
49 VNC_PARSE_STOP, // the session ended
50};
51
52enum class VncFailure : uint8_t {
55};
56
58class VncClient : public Component {
59 public:
60 explicit VncClient(display::Display *display, bool big_endian = false) : display_(display), big_endian_(big_endian) {}
61
62 void set_host(const char *host) { this->link_.set_host(host); }
63 void set_port(uint16_t port) { this->link_.set_port(port); }
68
69 void setup() override;
70 void loop() override;
71 void dump_config() override;
72 void on_shutdown() override;
73 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
74
75 bool is_connected() const { return this->state_ == VncState::VNC_STATE_RUNNING; }
77 void refresh();
78
79 template<typename F> void add_on_connect_callback(F &&callback) {
80 this->connect_callback_.add(std::forward<F>(callback));
81 }
82 template<typename F> void add_on_disconnect_callback(F &&callback) {
83 this->disconnect_callback_.add(std::forward<F>(callback));
84 }
85
86 protected:
87 static constexpr size_t RX_BUFFER_SIZE = 512;
88 static constexpr size_t TILE_BUFFER_SIZE = 16 * 16 * 2;
89
90 void connect_step_(uint32_t now);
91 void start_handshake_(uint32_t now);
92 void service_rx_(uint32_t now);
94 void pace_(uint32_t now);
95 bool start_session_(uint32_t now);
96 bool send_client_init_();
97 void frame_done_();
98 void rect_done_();
99 void next_tile_();
100 void draw_tile_();
101 uint32_t tile_hash_() const;
102 void clear_tiles_();
103 void forget_tiles_(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
104 void draw_row_(uint16_t x, uint16_t y, uint16_t count, const uint8_t *pixels);
105 void fill_tile_(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *pixel);
107 bool budget_exhausted_() const;
108 bool send_(const uint8_t *data, size_t len);
109 bool send_update_request_(bool incremental, uint32_t now);
110 VncParse fail_(VncFailure failure, const LogString *reason);
111 VncParse fail_send_() { return this->fail_(VncFailure::VNC_FAILURE_TRANSIENT, LOG_STR("Send failed")); }
113 void session_lost_(uint32_t now);
114 void session_end_(uint32_t now, VncFailure failure, const LogString *reason);
115 void reset_session_();
116
122 // Fingerprint of the tile last drawn at each 16x16 cell, so tiles the server resends unchanged are skipped.
123 // A 32-bit FNV-1a collision keeps a stale tile until it changes again or vnc.refresh runs; accepted.
125
135 // The first and the latest frame drawn since the display last refreshed.
144
145 uint16_t rx_pos_{0};
146 uint16_t rx_len_{0};
147 uint16_t fb_w_{0};
148 uint16_t fb_h_{0};
149 uint16_t vis_w_{0};
150 uint16_t vis_h_{0};
151 uint16_t rects_left_{0};
152 uint16_t rect_x_{0};
153 uint16_t rect_y_{0};
154 uint16_t rect_w_{0};
155 uint16_t rect_h_{0};
156 // Raw: column and row inside the rect. Hextile: absolute origin of the current tile.
157 uint16_t pos_x_{0};
158 uint16_t pos_y_{0};
159 uint16_t tile_fill_{0};
160 uint16_t drawn_tiles_{0};
161 uint16_t hash_capacity_{0};
162 uint16_t hash_cols_{0};
163 uint16_t hash_rows_{0};
164
165 uint8_t tile_w_{0};
166 uint8_t tile_h_{0};
167 uint8_t subrects_left_{0};
168 uint8_t bpp_{2};
169 uint8_t minor_{8};
170 uint8_t sec_count_{0};
171 uint8_t text_len_{0}; // clamped to MAX_TEXT_LEN
172 // The display rotation the cached tiles were drawn at, in quarter turns.
173 uint8_t hash_rotation_{0};
174 uint8_t hex_bg_[2]{};
175 uint8_t hex_fg_[2]{};
182
183 bool rx_pending_{false};
184 bool in_update_{false};
186 bool refresh_pending_{false};
187 bool need_full_{false};
188 bool frame_drawn_{false};
190 bool tile_hidden_{false};
191 bool display_parks_{false};
192 bool display_busy_{false};
193 bool busy_warned_{false};
194 bool refresh_retried_{false}; // one retry per refresh the driver abandoned
195 bool network_was_up_{false};
196 bool big_endian_; // the pixel order the display takes
197
199 alignas(4) uint8_t tile_[TILE_BUFFER_SIZE];
200};
201
202} // namespace esphome::vnc
uint8_t h
Definition bl0906.h:2
Helper class to request loop() to be called as fast as possible.
Definition helpers.h:2231
RFB (VNC) client that draws the remote framebuffer into a display.
Definition vnc_client.h:58
LazyCallbackManager< void()> disconnect_callback_
Definition vnc_client.h:120
VncFailure lost_failure_() const
uint8_t tile_[TILE_BUFFER_SIZE]
Definition vnc_client.h:199
static constexpr size_t TILE_BUFFER_SIZE
Definition vnc_client.h:88
LazyCallbackManager< void()> connect_callback_
Definition vnc_client.h:119
float get_setup_priority() const override
Definition vnc_client.h:73
void session_lost_(uint32_t now)
void forget_tiles_(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
VncParse fail_(VncFailure failure, const LogString *reason)
display::ColorBitness bitness_
Definition vnc_client.h:176
void set_max_update_delay(uint32_t ms)
Definition vnc_client.h:66
display::Display * display_
Definition vnc_client.h:117
socket::TcpClientLink link_
Definition vnc_client.h:118
void draw_row_(uint16_t x, uint16_t y, uint16_t count, const uint8_t *pixels)
bool is_connected() const
Definition vnc_client.h:75
void dump_config() override
bool send_(const uint8_t *data, size_t len)
bool start_session_(uint32_t now)
void refresh()
Ask the server for the whole screen again.
void pace_(uint32_t now)
HighFrequencyLoopRequester high_freq_
Definition vnc_client.h:121
void add_on_disconnect_callback(F &&callback)
Definition vnc_client.h:82
void service_rx_(uint32_t now)
uint32_t tile_hash_() const
void start_handshake_(uint32_t now)
static constexpr size_t RX_BUFFER_SIZE
Definition vnc_client.h:87
void set_port(uint16_t port)
Definition vnc_client.h:63
void session_end_(uint32_t now, VncFailure failure, const LogString *reason)
void connect_step_(uint32_t now)
VncPixelFormat pixel_format_
Definition vnc_client.h:177
bool budget_exhausted_() const
void set_pixel_format(VncPixelFormat format)
Definition vnc_client.h:67
void setup() override
void set_reconnect_interval(uint32_t ms)
Definition vnc_client.h:64
void set_update_delay(uint32_t ms)
Definition vnc_client.h:65
void on_shutdown() override
bool send_update_request_(bool incremental, uint32_t now)
VncClient(display::Display *display, bool big_endian=false)
Definition vnc_client.h:60
void skip_(uint32_t count, VncStep next)
void add_on_connect_callback(F &&callback)
Definition vnc_client.h:79
void set_host(const char *host)
Definition vnc_client.h:62
uint8_t rx_[RX_BUFFER_SIZE]
Definition vnc_client.h:198
void fill_tile_(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *pixel)
NameCopy * next
unsigned count
Definition gif.cpp:51
const char * format
constexpr float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.h:55
const void size_t len
Definition hal.h:68
static void uint32_t
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6