ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
vnc_client.h
Go to the documentation of this file.
1#pragma once
2
7#ifdef USE_VNC_TOUCHSCREEN
9#endif
10
11#include <cstddef>
12#include <cstdint>
13#include <string>
14
15namespace esphome::vnc {
16
22
28
36
60
61enum class VncParse : uint8_t {
62 VNC_PARSE_MORE, // the next step needs bytes that have not arrived
63 VNC_PARSE_PAUSE, // out of time, or the display is busy; resume without new data
64 VNC_PARSE_STOP, // the session ended
65};
66
73
75class VncClient : public Component
76#ifdef USE_VNC_TOUCHSCREEN
77 ,
79#endif
80{
81 public:
82 explicit VncClient(display::Display *display, bool big_endian = false) : display_(display), big_endian_(big_endian) {}
83
84 void set_host(const char *host) { this->link_.set_host(host); }
85 void set_port(uint16_t port) { this->link_.set_port(port); }
90#ifdef USE_VNC_AUTH
92 void set_auth_key(const uint8_t *key) { this->auth_key_ = key; }
93#endif
94#ifdef USE_VNC_TOUCHSCREEN
96 touchscreen->register_listener(this);
97 this->has_touchscreen_ = true;
98 }
99 void touch(touchscreen::TouchPoint tp) override;
100 void update(const touchscreen::TouchPoints_t &tpoints) override;
101 void release() override;
102#endif
103
104 void setup() override;
105 void loop() override;
106 void dump_config() override;
107 void on_shutdown() override;
108 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
109
110 bool is_connected() const { return this->state_ == VncState::VNC_STATE_RUNNING; }
112 void connect();
114 void disconnect();
116 void refresh();
117
118#ifdef USE_VNC_INPUT
119 bool send_pointer(uint16_t x, uint16_t y, uint8_t buttons);
120 void send_key(uint32_t keysym, VncKeyAction action);
121 void type_text(const char *text, size_t len);
122 void type_text(const std::string &text) { this->type_text(text.data(), text.size()); }
123#endif
124
125 template<typename F> void add_on_connect_callback(F &&callback) {
126 this->connect_callback_.add(std::forward<F>(callback));
127 }
128 template<typename F> void add_on_disconnect_callback(F &&callback) {
129 this->disconnect_callback_.add(std::forward<F>(callback));
130 }
131
132 protected:
133 static constexpr size_t RX_BUFFER_SIZE = 512;
134 static constexpr size_t TILE_BUFFER_SIZE = 16 * 16 * 2;
135#ifdef USE_VNC_INPUT
136 // For this long after input, frames are painted as soon as they arrive: the reaction matters more than a calm panel.
137 static constexpr uint32_t INPUT_BOOST_MS = 2000;
138 static constexpr size_t KEY_EVENT_SIZE = 8;
139 static constexpr size_t POINTER_EVENT_SIZE = 6;
140 static constexpr size_t MAX_PENDING_RELEASES = 4;
141#endif
142
143 void connect_step_(uint32_t now);
144 void start_handshake_(uint32_t now);
145 void service_rx_(uint32_t now);
147 void pace_(uint32_t now);
148 bool start_session_(uint32_t now);
149 bool send_client_init_();
150 bool select_security_(uint8_t type);
151 bool has_auth_key_() const {
152#ifdef USE_VNC_AUTH
153 return this->auth_key_ != nullptr;
154#else
155 return false;
156#endif
157 }
158 void frame_done_();
159 void rect_done_();
160 void next_tile_();
161 void draw_tile_();
162 uint32_t tile_hash_() const;
163 void clear_tiles_();
164 void forget_tiles_(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
165 void draw_row_(uint16_t x, uint16_t y, uint16_t count, const uint8_t *pixels);
166 void fill_tile_(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *pixel);
168 bool budget_exhausted_() const;
169 bool send_(const uint8_t *data, size_t len);
170 bool send_update_request_(bool incremental, uint32_t now);
171#ifdef USE_VNC_INPUT
172 bool send_key_event_(uint32_t keysym, VncKeyAction action);
173 bool send_input_(const uint8_t *msg, size_t len, bool release);
174 bool send_pending_();
175 void note_input_();
176#endif
177#ifdef USE_VNC_TOUCHSCREEN
179 void handle_touch_(uint16_t x, uint16_t y);
180 void handle_release_();
181#endif
182 VncParse fail_(VncFailure failure, const LogString *reason);
183 VncParse fail_send_() { return this->fail_(VncFailure::VNC_FAILURE_TRANSIENT, LOG_STR("Send failed")); }
185 void session_lost_(uint32_t now);
186 void session_end_(uint32_t now, VncFailure failure, const LogString *reason);
187 void schedule_retry_(uint32_t now, VncFailure failure, const LogString *reason, bool was_running);
188 void reset_session_();
189 void clear_link_warning_();
190
193#ifdef USE_VNC_AUTH
194 const uint8_t *auth_key_{nullptr};
195#endif
199 // Fingerprint of the tile last drawn at each 16x16 cell, so tiles the server resends unchanged are skipped.
200 // A 32-bit FNV-1a collision keeps a stale tile until it changes again or vnc.refresh runs; accepted.
202
212 // The first and the latest frame drawn since the display last refreshed.
215#ifdef USE_VNC_INPUT
217#endif
224
225 uint16_t rx_pos_{0};
226 uint16_t rx_len_{0};
227 uint16_t fb_w_{0};
228 uint16_t fb_h_{0};
229 uint16_t vis_w_{0};
230 uint16_t vis_h_{0};
231 uint16_t rects_left_{0};
232 uint16_t rect_x_{0};
233 uint16_t rect_y_{0};
234 uint16_t rect_w_{0};
235 uint16_t rect_h_{0};
236 // Raw: column and row inside the rect. Hextile: absolute origin of the current tile.
237 uint16_t pos_x_{0};
238 uint16_t pos_y_{0};
239 uint16_t tile_fill_{0};
240 uint16_t drawn_tiles_{0};
241 uint16_t hash_capacity_{0};
242 uint16_t hash_cols_{0};
243 uint16_t hash_rows_{0};
244#ifdef USE_VNC_INPUT
245 uint16_t ptr_x_{0};
246 uint16_t ptr_y_{0};
247#endif
248#ifdef USE_VNC_TOUCHSCREEN
249 uint16_t press_x_{0};
250 uint16_t press_y_{0};
251#endif
252
253#ifdef USE_VNC_INPUT
254 uint8_t ptr_buttons_{0}; // as the server will see them once the waiting releases are sent
255 uint8_t pending_count_{0};
256#endif
257 uint8_t tile_w_{0};
258 uint8_t tile_h_{0};
259 uint8_t subrects_left_{0};
260 uint8_t bpp_{2};
261 uint8_t minor_{8};
262#ifdef USE_VNC_AUTH
263 uint8_t sec_type_{0};
264#endif
265 uint8_t sec_count_{0};
266 uint8_t text_len_{0}; // clamped to MAX_TEXT_LEN
267 // The display rotation the cached tiles were drawn at, in quarter turns.
268 uint8_t hash_rotation_{0};
269 uint8_t hex_bg_[2]{};
270 uint8_t hex_fg_[2]{};
277
278 bool rx_pending_{false};
279#ifdef USE_VNC_AUTH
280 // Set by the one retry after a drop left the password's fate unknown; hold_ms_ is then the wait before it
281 bool auth_retried_{false};
282 bool auth_hold_{false}; // hold_ms_ is the wait after an unanswered password, which survives a network drop
283#endif
284 bool in_update_{false};
286 bool refresh_pending_{false};
287 bool need_full_{false};
288 bool frame_drawn_{false};
290 bool tile_hidden_{false};
291 bool display_parks_{false};
292 bool display_busy_{false};
293 bool busy_warned_{false};
294 bool refresh_retried_{false}; // one retry per refresh the driver abandoned
295 bool network_was_up_{false};
296#ifdef USE_VNC_TOUCHSCREEN
297 bool has_touchscreen_{false};
298 bool dragging_{false};
299 bool touching_{false};
300#endif
301 bool big_endian_; // the pixel order the display takes
302
304#ifdef USE_VNC_INPUT
305 // Key-up and button-release messages waiting for room, oldest first.
307#endif
308 alignas(4) uint8_t tile_[TILE_BUFFER_SIZE];
309};
310
311} // 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:2329
void register_listener(TouchListener *listener)
Definition touchscreen.h:65
RFB (VNC) client that draws the remote framebuffer into a display.
Definition vnc_client.h:80
static constexpr size_t KEY_EVENT_SIZE
Definition vnc_client.h:138
LazyCallbackManager< void()> disconnect_callback_
Definition vnc_client.h:197
void disconnect()
Close the session and stop retrying until connect().
VncFailure lost_failure_() const
bool select_security_(uint8_t type)
void set_auth_key(const uint8_t *key)
8 bytes, already bit-reversed for VNC, may live in PROGMEM.
Definition vnc_client.h:92
uint8_t tile_[TILE_BUFFER_SIZE]
Definition vnc_client.h:308
static constexpr uint32_t INPUT_BOOST_MS
Definition vnc_client.h:137
void release() override
static constexpr size_t POINTER_EVENT_SIZE
Definition vnc_client.h:139
static constexpr size_t TILE_BUFFER_SIZE
Definition vnc_client.h:134
LazyCallbackManager< void()> connect_callback_
Definition vnc_client.h:196
void send_key(uint32_t keysym, VncKeyAction action)
float get_setup_priority() const override
Definition vnc_client.h:108
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)
void handle_touch_(uint16_t x, uint16_t y)
Touch input in display coordinates.
display::ColorBitness bitness_
Definition vnc_client.h:271
void set_max_update_delay(uint32_t ms)
Definition vnc_client.h:88
void type_text(const std::string &text)
Definition vnc_client.h:122
display::Display * display_
Definition vnc_client.h:191
socket::TcpClientLink link_
Definition vnc_client.h:192
const uint8_t * auth_key_
Definition vnc_client.h:194
void draw_row_(uint16_t x, uint16_t y, uint16_t count, const uint8_t *pixels)
bool is_connected() const
Definition vnc_client.h:110
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:198
void connect()
Retry now, also after an authentication failure or disconnect().
void register_touchscreen(touchscreen::Touchscreen *touchscreen)
Definition vnc_client.h:95
bool has_auth_key_() const
Definition vnc_client.h:151
void add_on_disconnect_callback(F &&callback)
Definition vnc_client.h:128
void service_rx_(uint32_t now)
void type_text(const char *text, size_t len)
uint32_t tile_hash_() const
bool send_input_(const uint8_t *msg, size_t len, bool release)
void start_handshake_(uint32_t now)
static constexpr size_t RX_BUFFER_SIZE
Definition vnc_client.h:133
void set_port(uint16_t port)
Definition vnc_client.h:85
void session_end_(uint32_t now, VncFailure failure, const LogString *reason)
void connect_step_(uint32_t now)
VncPixelFormat pixel_format_
Definition vnc_client.h:272
static constexpr size_t MAX_PENDING_RELEASES
Definition vnc_client.h:140
bool budget_exhausted_() const
void set_pixel_format(VncPixelFormat format)
Definition vnc_client.h:89
uint8_t pending_[MAX_PENDING_RELEASES][KEY_EVENT_SIZE]
Definition vnc_client.h:306
bool send_pointer(uint16_t x, uint16_t y, uint8_t buttons)
bool send_key_event_(uint32_t keysym, VncKeyAction action)
void setup() override
void set_reconnect_interval(uint32_t ms)
Definition vnc_client.h:86
void set_update_delay(uint32_t ms)
Definition vnc_client.h:87
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:82
void schedule_retry_(uint32_t now, VncFailure failure, const LogString *reason, bool was_running)
void skip_(uint32_t count, VncStep next)
void add_on_connect_callback(F &&callback)
Definition vnc_client.h:125
void touch(touchscreen::TouchPoint tp) override
void set_host(const char *host)
Definition vnc_client.h:84
uint8_t rx_[RX_BUFFER_SIZE]
Definition vnc_client.h:303
void fill_tile_(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *pixel)
uint8_t type
const char * key
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
std::vector< TouchPoint > TouchPoints_t
Definition touchscreen.h:29
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