ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
vnc_client.cpp
Go to the documentation of this file.
1#include "vnc_client.h"
2
6#include "esphome/core/hal.h"
8#include "esphome/core/log.h"
10#ifdef USE_VNC_AUTH
11#include "vnc_des.h"
12#endif
13
14#include <algorithm>
15#include <cinttypes>
16#include <cstdlib>
17#include <cstring>
18#include <iterator>
19
20namespace esphome::vnc {
21
22ESPHOME_LOG_TAG(TAG, "vnc");
23
24// Below the 50 ms blocking warning, and the same slice it8951 uses for its transfers.
25static constexpr uint32_t LOOP_BUDGET_MS = 20;
26static constexpr uint32_t HANDSHAKE_TIMEOUT_MS = 15000;
27static constexpr uint32_t STABLE_SESSION_MS = 60000;
28static constexpr uint32_t MAX_BACKOFF_MS = 60000;
29static constexpr uint32_t CONFIG_RETRY_MS = 300000;
30static constexpr uint32_t AUTH_AMBIGUOUS_HOLD_MS = 60000;
31static constexpr uint32_t HEARTBEAT_MS = 60000;
32static constexpr uint32_t MIN_REQUEST_SPACING_MS = 50;
33// The longest the first refresh waits for a display with a loop to park once after boot.
34static constexpr uint32_t DISPLAY_START_MS = 5000;
35// Longer than any driver's own busy timeout: a panel that stays busy this long is stuck
36static constexpr uint32_t DISPLAY_BUSY_TIMEOUT_MS = 120000;
37static constexpr uint16_t MAX_CURSOR_SIDE = 1024;
38static constexpr uint32_t MAX_TEXT_LEN = 63;
39
40static constexpr uint8_t SECURITY_NONE = 1;
41static constexpr uint8_t SECURITY_VNC_AUTH = 2;
42
43static constexpr int32_t ENCODING_RAW = 0;
44static constexpr int32_t ENCODING_HEXTILE = 5;
45static constexpr int32_t ENCODING_RICH_CURSOR = -239;
46// In preference order: TightVNC uses the first real encoding it supports for every rectangle.
47static constexpr int32_t ENCODINGS[] = {ENCODING_HEXTILE, ENCODING_RAW, ENCODING_RICH_CURSOR};
48
49// Indexed by VncPixelFormat
50PROGMEM_STRING_TABLE(PixelFormatStrings, "auto", "RGB565", "RGB332");
51
52static constexpr uint8_t HEXTILE_RAW = 0x01;
53static constexpr uint8_t HEXTILE_BACKGROUND = 0x02;
54static constexpr uint8_t HEXTILE_FOREGROUND = 0x04;
55static constexpr uint8_t HEXTILE_ANY_SUBRECTS = 0x08;
56static constexpr uint8_t HEXTILE_SUBRECTS_COLOURED = 0x10;
57
58static constexpr uint8_t MSG_FRAMEBUFFER_UPDATE = 0;
59static constexpr uint8_t MSG_SET_COLOUR_MAP = 1;
60static constexpr uint8_t MSG_BELL = 2;
61static constexpr uint8_t MSG_SERVER_CUT_TEXT = 3;
62
63#ifdef USE_VNC_INPUT
64static constexpr uint8_t MSG_KEY_EVENT = 4;
65static constexpr uint8_t MSG_POINTER_EVENT = 5;
66
67static constexpr uint32_t KEYSYM_BACKSPACE = 0xFF08;
68static constexpr uint32_t KEYSYM_TAB = 0xFF09;
69static constexpr uint32_t KEYSYM_RETURN = 0xFF0D;
70// X11 encodes a character outside Latin-1 as this prefix plus its code point.
71static constexpr uint32_t KEYSYM_UNICODE = 0x01000000;
72#endif
73
74static inline uint16_t read_be16(const uint8_t *p) { return encode_uint16(p[0], p[1]); }
75static inline uint32_t read_be32(const uint8_t *p) { return encode_uint32(p[0], p[1], p[2], p[3]); }
76
77static int parse_three_digits(const uint8_t *p) {
78 int value = 0;
79 for (int i = 0; i < 3; i++) {
80 if (p[i] < '0' || p[i] > '9') {
81 return -1;
82 }
83 value = value * 10 + (p[i] - '0');
84 }
85 return value;
86}
87
88#ifdef USE_VNC_INPUT
89// Keep in step with _character_keysym() in keysyms.py.
90static uint32_t keysym_for_codepoint(uint32_t cp) {
91 switch (cp) {
92 case '\n':
93 return KEYSYM_RETURN;
94 case '\t':
95 return KEYSYM_TAB;
96 case '\b':
97 return KEYSYM_BACKSPACE;
98 default:
99 break;
100 }
101 if (cp < 0x20 || (cp >= 0x7F && cp < 0xA0)) {
102 return 0;
103 }
104 return cp <= 0xFF ? cp : (KEYSYM_UNICODE | cp);
105}
106#endif
107
108#ifdef USE_VNC_TOUCHSCREEN
109static constexpr uint16_t TOUCH_SLOP_PX = 8;
110
111static bool is_down(const touchscreen::TouchPoint &tp) {
112 // A lifted finger lingers in the list as RELEASED or RELEASING until every finger is up.
113 return (tp.state & (touchscreen::STATE_PRESSED | touchscreen::STATE_UPDATED)) != 0 &&
114 (tp.state & touchscreen::STATE_RELEASING) == 0;
115}
116
117// A first touch at (0, 0) matches a new point's default previous position, so only touch() reports it.
119 if (is_down(tp)) {
120 this->handle_touch_(tp.x, tp.y);
121 }
122}
123
125 for (const auto &tp : tpoints) {
126 if (is_down(tp)) {
127 this->handle_touch_(tp.x, tp.y);
128 return;
129 }
130 }
131}
132
134#endif
135
138#ifdef USE_VNC_INPUT
139 this->last_input_ms_ = this->setup_ms_ - INPUT_BOOST_MS; // no input yet
140#endif
141 this->link_.begin(TAG);
142 // Sized for the display now; a rotation changed later swaps the sides but keeps the cell count.
143 const uint32_t wanted = static_cast<uint32_t>((this->display_->get_width() + 15) / 16) *
144 static_cast<uint32_t>((this->display_->get_height() + 15) / 16);
145 const uint32_t cells = std::min<uint32_t>(wanted, UINT16_MAX);
146 if (cells < wanted) {
147 ESP_LOGW(TAG, "Display has %" PRIu32 " tiles, more than the %u the cache holds", wanted, UINT16_MAX);
148 }
149 RAMAllocator<uint32_t> allocator;
150 this->tile_hashes_ = allocator.allocate(cells);
151 if (this->tile_hashes_ == nullptr) {
152 ESP_LOGW(TAG, "Not enough memory to skip unchanged tiles");
153 return;
154 }
155 this->hash_capacity_ = cells;
156 this->clear_tiles_();
157}
158
160 this->need_full_ = true;
161 this->clear_tiles_();
162}
163
165 if (this->tile_hashes_ != nullptr) {
166 std::memset(this->tile_hashes_, 0, sizeof(uint32_t) * this->hash_capacity_);
167 }
168}
169
171 ESP_LOGCONFIG(TAG,
172 "VNC:\n"
173 " Server: %s:%u\n"
174 " Reconnect Interval: %" PRIu32 "ms\n"
175 " Update Delay: %" PRIu32 "ms\n"
176 " Max Update Delay: %" PRIu32 "ms\n"
177 " Pixel Format: %s\n"
178 " Tile Cache: %u tiles",
179 this->link_.host(), this->link_.port(), this->link_.reconnect_interval(), this->update_delay_ms_,
180 this->max_update_delay_ms_,
181 LOG_STR_ARG(PixelFormatStrings::get_log_str(static_cast<uint8_t>(this->pixel_format_), 0)),
182 this->hash_capacity_);
183#ifdef USE_VNC_AUTH
184 ESP_LOGCONFIG(TAG, " Password: %s", this->auth_key_ != nullptr ? LOG_STR_LITERAL("set") : LOG_STR_LITERAL("none"));
185#endif
186#ifdef USE_VNC_TOUCHSCREEN
187 ESP_LOGCONFIG(TAG, " Touchscreen: %s", this->has_touchscreen_ ? LOG_STR_LITERAL("yes") : LOG_STR_LITERAL("no"));
188#endif
189}
190
192
194 // Latched by disconnect() or a wrong password until connect()
196 this->disable_loop();
197 return;
198 }
200 this->pass_start_ms_ = millis();
201 if (this->display_->is_failed()) {
202 this->link_.close(); // a connect still in flight would otherwise keep its socket for good
203 if (this->state_ != VncState::VNC_STATE_IDLE) {
204 this->session_end_(now, VncFailure::VNC_FAILURE_TRANSIENT, LOG_STR("Display failed"));
205 }
206 this->status_set_error(LOG_STR("Display failed"));
207 this->disable_loop();
208 return;
209 }
210 // A display that parks its loop when idle is busy while that loop runs. Displays that never
211 // park (no loop, or one that always runs) update synchronously, so they are never busy.
212 const bool was_busy = this->display_busy_;
213 if (this->display_->is_idle()) {
214 this->display_parks_ = true;
215 this->display_busy_ = false;
216 } else {
217 this->display_busy_ = this->display_parks_;
218 }
219 if (was_busy && !this->display_busy_) {
220 // A refresh the driver gave up on (it sets its warning) leaves the panel behind the buffer: retry it
221 // once and forget the cached tiles, which no longer say what the panel shows
222 if (!this->display_->status_has_warning()) {
223 this->refresh_retried_ = false;
224 } else if (!this->refresh_retried_) {
225 this->refresh_retried_ = true;
226 this->clear_tiles_();
227 if (!this->refresh_pending_) {
228 this->first_change_ms_ = now;
229 }
230 this->last_change_ms_ = now;
231 this->refresh_pending_ = true;
232 }
233 }
234 // A stuck panel, or another owner of the display's loop, would otherwise freeze the screen silently
235 if (!this->display_busy_) {
236 this->busy_since_ms_ = now;
237 if (this->busy_warned_) {
238 this->busy_warned_ = false;
239 // The connection path owns the warning while no session runs
240 if (this->state_ == VncState::VNC_STATE_RUNNING) {
241 this->status_clear_warning();
242 }
243 }
244 } else if (!this->busy_warned_ && now - this->busy_since_ms_ >= DISPLAY_BUSY_TIMEOUT_MS) {
245 this->busy_warned_ = true;
246 this->status_set_warning(LOG_STR("Display busy too long"));
247 }
248
249 if (!this->link_.connected()) {
250 if (this->state_ != VncState::VNC_STATE_IDLE) {
251 this->session_lost_(now);
252 }
253 this->connect_step_(now);
254 if (!this->link_.connected()) {
255 return;
256 }
257 this->start_handshake_(now);
258 }
259 if (this->state_ == VncState::VNC_STATE_HANDSHAKE && now - this->session_start_ms_ >= HANDSHAKE_TIMEOUT_MS) {
260 this->fail_(this->lost_failure_(), LOG_STR("Handshake timed out"));
261 return;
262 }
263
264 const bool blocked = this->in_update_ && this->display_busy_;
265 // A display still starting up (it8951) can go from its start-up straight into a refresh without parking.
266 const bool display_starting =
267 !this->display_parks_ && this->display_->has_overridden_loop() && now - this->setup_ms_ < DISPLAY_START_MS;
268 uint32_t update_delay = this->update_delay_ms_;
269#ifdef USE_VNC_INPUT
270 if (now - this->last_input_ms_ < INPUT_BOOST_MS) {
271 update_delay = 0;
272 }
273#endif
274 // Wait until the screen stops changing, so an animation becomes one refresh showing where it ended.
275 if (this->refresh_pending_ && !this->in_update_ && !this->display_busy_ && !display_starting &&
276 (now - this->last_change_ms_ >= update_delay || now - this->first_change_ms_ >= this->max_update_delay_ms_)) {
277 // Synchronous displays flush inside update(), so this pass does nothing else.
278 this->refresh_pending_ = false;
279 this->display_->update();
280 this->display_busy_ = this->display_parks_ && !this->display_->is_idle();
281 } else if (!blocked && (this->rx_pending_ || this->link_.ready())) {
282 this->service_rx_(now);
283 }
284 if (this->state_ == VncState::VNC_STATE_RUNNING) {
285#ifdef USE_VNC_INPUT
286 this->send_pending_();
287#endif
288 // A new session lays the screen out again for the new rotation and redraws all of it.
289 if (static_cast<uint8_t>(this->display_->get_rotation() / 90) != this->hash_rotation_) {
290 this->fail_(VncFailure::VNC_FAILURE_TRANSIENT, LOG_STR("Display rotation changed"));
291 return;
292 }
293 // Mid-frame there is nothing to send, so a vanished server would never fail a write.
294 if (this->in_update_ && !blocked && !this->rx_pending_ && now - this->last_rx_ms_ >= HEARTBEAT_MS) {
295 this->fail_(VncFailure::VNC_FAILURE_TRANSIENT, LOG_STR("Server stopped sending"));
296 return;
297 }
298 this->pace_(now);
299 }
300 this->link_.flush_tx();
301 if (this->rx_pending_ && !(this->in_update_ && this->display_busy_)) {
302 this->high_freq_.start();
303 } else {
304 this->high_freq_.stop();
305 }
306}
307
310 return;
311 }
312 this->backoff_ms_ = 0;
313 this->hold_ms_ = 0;
314#ifdef USE_VNC_AUTH
315 this->auth_retried_ = false;
316 this->auth_hold_ = false;
317#endif
318 this->status_clear_error();
319 this->clear_link_warning_();
321 // Start a fresh attempt with its full timeout, even if one is in flight.
322 this->link_.close();
323 this->link_.begin(TAG);
324 this->enable_loop();
325}
326
328 if (this->state_ == VncState::VNC_STATE_STOPPED) {
329 return;
330 }
331 const bool was_running = this->state_ == VncState::VNC_STATE_RUNNING;
332 this->link_.close();
333 this->reset_session_();
335 this->clear_link_warning_();
336 ESP_LOGI(TAG, "Disconnected");
337 if (was_running) {
338 this->disconnect_callback_.call();
339 }
340}
341
343 const bool up = network::is_connected();
344 if (up && !this->network_was_up_) {
345 this->backoff_ms_ = 0;
346#ifdef USE_VNC_AUTH
347 // The wait after an unanswered password protects the server's lockout counter, so it survives a network drop.
348 if (!this->auth_hold_) {
349 this->hold_ms_ = 0;
350 }
351#else
352 this->hold_ms_ = 0;
353#endif
354 }
355 this->network_was_up_ = up;
356 if (!up) {
357 return;
358 }
359 if (!this->link_.connecting() && (now - this->hold_start_ms_ < this->hold_ms_ || this->link_.in_backoff())) {
360 return;
361 }
362 this->link_.poll();
363 // DNS failures, refused connects and connect timeouts all leave no socket and start the link's backoff.
364 if (!this->link_.connected() && !this->link_.connecting() && this->link_.in_backoff()) {
365 this->session_end_(now, VncFailure::VNC_FAILURE_TRANSIENT, LOG_STR("Cannot reach the server"));
366 }
367}
368
370 // A stuck display keeps its warning through a reconnect
371 if (!this->busy_warned_) {
372 this->status_clear_warning();
373 }
374}
375
377 // Pixels drawn but never shown would match the cache when the next session resends them.
378 if (this->refresh_pending_ || this->frame_drawn_) {
379 this->clear_tiles_();
380 }
381 // Hextile colours carry from tile to tile; a new server must not inherit the last one's
382 std::memset(this->hex_bg_, 0, sizeof(this->hex_bg_));
383 std::memset(this->hex_fg_, 0, sizeof(this->hex_fg_));
384 this->rx_pos_ = 0;
385 this->rx_len_ = 0;
386 this->rx_pending_ = false;
387 this->in_update_ = false;
388 this->request_outstanding_ = false;
389 this->refresh_pending_ = false;
390 this->need_full_ = false;
391 this->frame_drawn_ = false;
392#ifdef USE_VNC_AUTH
393 this->sec_type_ = 0;
394#endif
395#ifdef USE_VNC_INPUT
396 this->pending_count_ = 0;
397 this->ptr_buttons_ = 0;
398#endif
399#ifdef USE_VNC_TOUCHSCREEN
400 this->touching_ = false;
401#endif
404 this->high_freq_.stop();
405}
406
408 this->reset_session_();
410 this->session_start_ms_ = now;
411 this->last_rx_ms_ = now;
412}
413
417 return this->reason_failure_;
418 }
419#ifdef USE_VNC_AUTH
420 // The server got our password response but never said whether it was right.
421 if (this->step_ == VncStep::VNC_STEP_SECURITY_RESULT && this->sec_type_ == SECURITY_VNC_AUTH) {
423 }
424#endif
425 }
427}
428
430 this->session_end_(now, this->lost_failure_(), LOG_STR("Connection lost"));
431}
432
433VncParse VncClient::fail_(VncFailure failure, const LogString *reason) {
434 this->link_.close();
435 this->link_.note_attempt();
436 this->session_end_(App.get_loop_component_start_time(), failure, reason);
438}
439
440void VncClient::session_end_(uint32_t now, VncFailure failure, const LogString *reason) {
441 const bool was_running = this->state_ == VncState::VNC_STATE_RUNNING;
442 this->schedule_retry_(now, failure, reason, was_running);
443 // Last, so a vnc.connect in on_disconnect overrides the wait and the warning set above.
444 if (was_running) {
445 this->disconnect_callback_.call();
446 }
447}
448
449void VncClient::schedule_retry_(uint32_t now, VncFailure failure, const LogString *reason, bool was_running) {
450#ifdef USE_VNC_AUTH
451 this->auth_hold_ = false;
452#endif
453 this->reset_session_();
455 this->hold_start_ms_ = now;
456 if (was_running) {
457 const uint32_t lasted = now - this->session_start_ms_;
458 ESP_LOGW(TAG, "%s after %" PRIu32 "s", LOG_STR_ARG(reason), lasted / 1000);
459 this->status_set_warning();
460 if (lasted >= STABLE_SESSION_MS) {
461 this->backoff_ms_ = 0;
462 this->hold_ms_ = 0;
463 return;
464 }
466 }
467 switch (failure) {
468#ifdef USE_VNC_AUTH
470 // Only an accepted password, a session that reaches RUNNING, or connect() clears this: a refusal in between
471 // still counts
472 if (!this->auth_retried_) {
473 this->auth_retried_ = true;
474 this->auth_hold_ = true;
475 this->hold_ms_ = AUTH_AMBIGUOUS_HOLD_MS;
476 ESP_LOGW(TAG, "%s before the password was accepted; retrying in %" PRIu32 "s", LOG_STR_ARG(reason),
477 this->hold_ms_ / 1000);
478 this->status_set_warning();
479 return;
480 }
481 this->auth_retried_ = false;
483 ESP_LOGE(TAG,
484 "%s after the password twice. Not retrying: on some servers repeated failures lock out every user. "
485 "Check the password and the server, then reboot or call vnc.connect",
486 LOG_STR_ARG(reason));
487 this->status_set_error(LOG_STR("Server dropped the connection after the password twice"));
488 return;
491 ESP_LOGE(TAG, "Authentication failed. Not retrying: on some servers repeated failures lock out every user. "
492 "Fix the password, then reboot or call vnc.connect");
493 this->status_set_error(LOG_STR("Authentication failed"));
494 return;
495#endif
497 this->hold_ms_ = CONFIG_RETRY_MS;
498 ESP_LOGE(TAG, "%s; retrying in %" PRIu32 "s", LOG_STR_ARG(reason), this->hold_ms_ / 1000);
499 this->status_set_error(reason);
500 return;
501 default: {
502 const uint32_t base = this->link_.reconnect_interval();
503 this->backoff_ms_ =
504 this->backoff_ms_ == 0 ? base : std::min(this->backoff_ms_ * 2, std::max(MAX_BACKOFF_MS, base));
505 this->hold_ms_ = this->backoff_ms_;
506 if (!was_running) {
507 ESP_LOGW(TAG, "%s; retrying in %" PRIu32 "s", LOG_STR_ARG(reason), this->hold_ms_ / 1000);
508 }
509 this->status_set_warning();
510 return;
511 }
512 }
513}
514
515bool VncClient::budget_exhausted_() const { return millis() - this->pass_start_ms_ >= LOOP_BUDGET_MS; }
516
518 for (;;) {
519 VncParse result = this->parse_();
520 if (result == VncParse::VNC_PARSE_STOP) {
521 return;
522 }
523 if (result == VncParse::VNC_PARSE_PAUSE) {
524 this->rx_pending_ = true;
525 return;
526 }
527 // Less than one item is left, so the buffer always has room after compacting.
528 if (this->rx_pos_ != 0) {
529 this->rx_len_ -= this->rx_pos_;
530 std::memmove(this->rx_, this->rx_ + this->rx_pos_, this->rx_len_);
531 this->rx_pos_ = 0;
532 }
533 if (this->budget_exhausted_()) {
534 this->rx_pending_ = true;
535 return;
536 }
537 ssize_t count = this->link_.read(this->rx_ + this->rx_len_, RX_BUFFER_SIZE - this->rx_len_);
538 if (count < 0) {
539 this->session_lost_(now);
540 return;
541 }
542 if (count == 0) {
543 this->rx_pending_ = false;
544 return;
545 }
546 this->rx_len_ += static_cast<uint16_t>(count);
547 this->rx_total_ += count;
548 this->last_rx_ms_ = now;
549 }
550}
551
557
559 const uint8_t shared = 1;
560 return this->send_(&shared, 1);
561}
562
564#ifdef USE_VNC_AUTH
565 this->sec_type_ = type;
566 if (type == SECURITY_VNC_AUTH) {
568 return true;
569 }
570#endif
571 if (this->minor_ == 8) {
573 return true;
574 }
576 return this->send_client_init_();
577}
578
580 for (;;) {
581 const uint8_t *p = this->rx_ + this->rx_pos_;
582 const size_t avail = this->rx_len_ - this->rx_pos_;
583 switch (this->step_) {
585 if (avail < 12) {
587 }
588 const int major = parse_three_digits(p + 4);
589 const int minor = parse_three_digits(p + 8);
590 if (std::memcmp(p, "RFB ", 4) != 0 || p[7] != '.' || p[11] != '\n' || major < 0 || minor < 0) {
591 return this->fail_(VncFailure::VNC_FAILURE_CONFIG, LOG_STR("Not a VNC server"));
592 }
593 if (major < 3) {
594 return this->fail_(VncFailure::VNC_FAILURE_CONFIG, LOG_STR("Unsupported RFB version"));
595 }
596 this->minor_ = (major > 3 || minor >= 8) ? 8 : (minor == 7 ? 7 : 3);
597 this->rx_pos_ += 12;
598 uint8_t reply[12] = {'R', 'F', 'B', ' ', '0', '0', '3', '.', '0', '0', '8', '\n'};
599 reply[10] = '0' + this->minor_;
600 if (!this->send_(reply, sizeof(reply))) {
601 return this->fail_send_();
602 }
604 break;
605 }
607 if (avail < 1) {
609 }
610 this->sec_count_ = p[0];
611 this->rx_pos_ += 1;
612 if (this->sec_count_ == 0) {
613 // A refusal before authentication, such as a lockout caused by another client.
615 } else {
617 }
618 break;
619 }
621 if (avail < this->sec_count_) {
623 }
624 const bool has_key = this->has_auth_key_();
625 uint8_t chosen = 0;
626 bool wants_password = false;
627 for (uint8_t i = 0; i < this->sec_count_; i++) {
628 if (p[i] == SECURITY_NONE || (p[i] == SECURITY_VNC_AUTH && has_key)) {
629 chosen = p[i];
630 break;
631 }
632 wants_password |= p[i] == SECURITY_VNC_AUTH;
633 }
634 this->rx_pos_ += this->sec_count_;
635 if (chosen == 0) {
636 return this->fail_(VncFailure::VNC_FAILURE_CONFIG, wants_password ? LOG_STR("The server requires a password")
637 : LOG_STR("No supported security type"));
638 }
639 if (!this->send_(&chosen, 1) || !this->select_security_(chosen)) {
640 return this->fail_send_();
641 }
642 break;
643 }
645 if (avail < 4) {
647 }
648 const uint32_t type = read_be32(p);
649 this->rx_pos_ += 4;
650 if (type == 0) {
652 } else if (type == SECURITY_NONE || (type == SECURITY_VNC_AUTH && this->has_auth_key_())) {
653 if (!this->select_security_(type)) {
654 return this->fail_send_();
655 }
656 } else {
657 return this->fail_(VncFailure::VNC_FAILURE_CONFIG, type == SECURITY_VNC_AUTH
658 ? LOG_STR("The server requires a password")
659 : LOG_STR("No supported security type"));
660 }
661 break;
662 }
663#ifdef USE_VNC_AUTH
665 if (avail < 16) {
667 }
668 uint8_t *challenge = this->rx_ + this->rx_pos_;
669 vnc_auth_response(this->auth_key_, challenge);
670 if (!this->send_(challenge, 16)) {
671 return this->fail_send_();
672 }
673 this->rx_pos_ += 16;
675 break;
676 }
677#endif
679 if (avail < 4) {
681 }
682 const uint32_t result = read_be32(p);
683 this->rx_pos_ += 4;
684 if (result == 0) {
685#ifdef USE_VNC_AUTH
686 if (this->sec_type_ == SECURITY_VNC_AUTH)
687 this->auth_retried_ = false; // the password was accepted
688#endif
689 if (!this->send_client_init_()) {
690 return this->fail_send_();
691 }
693 break;
694 }
695 // Fixed here, so a drop while the reason arrives cannot turn a wrong password into a retry.
696#ifdef USE_VNC_AUTH
697 this->reason_failure_ =
699#else
701#endif
702 if (this->minor_ == 8) {
704 break;
705 }
706 return this->fail_(this->reason_failure_, LOG_STR("Security handshake failed"));
707 }
709 if (avail < 4) {
711 }
712 this->text_len_ = static_cast<uint8_t>(std::min(read_be32(p), MAX_TEXT_LEN));
713 this->rx_pos_ += 4;
715 break;
716 }
718 if (avail < this->text_len_) {
720 }
721 ESP_LOGE(TAG, "Server: %.*s", static_cast<int>(this->text_len_), reinterpret_cast<const char *>(p));
722 return this->fail_(this->reason_failure_, LOG_STR("Connection refused"));
723 }
725 if (avail < 24) {
727 }
728 this->fb_w_ = read_be16(p);
729 this->fb_h_ = read_be16(p + 2);
730 // Reused for the length of the name that follows.
731 this->skip_left_ = read_be32(p + 20);
732 this->rx_pos_ += 24;
733 if (this->fb_w_ == 0 || this->fb_h_ == 0) {
734 return this->fail_(VncFailure::VNC_FAILURE_TRANSIENT, LOG_STR("The server screen is empty"));
735 }
736 this->text_len_ = static_cast<uint8_t>(std::min(this->skip_left_, MAX_TEXT_LEN));
738 break;
739 }
741 if (avail < this->text_len_) {
743 }
744 ESP_LOGI(TAG, "Server '%.*s', %ux%u, RFB 3.%u", static_cast<int>(this->text_len_),
745 reinterpret_cast<const char *>(p), this->fb_w_, this->fb_h_, this->minor_);
746 this->rx_pos_ += this->text_len_;
747 this->skip_(this->skip_left_ - this->text_len_, VncStep::VNC_STEP_SESSION_START);
748 break;
749 }
752 return this->fail_send_();
753 }
755 break;
757 if (avail < 1) {
759 }
760 switch (p[0]) {
761 case MSG_FRAMEBUFFER_UPDATE:
762 if (avail < 4) {
764 }
765 this->rects_left_ = read_be16(p + 2);
766 this->rx_pos_ += 4;
767 this->in_update_ = true;
768 this->update_start_ms_ = millis();
769 this->update_start_bytes_ = this->rx_total_ - (this->rx_len_ - this->rx_pos_);
770 this->drawn_tiles_ = 0;
771 this->rect_done_();
772 break;
773 case MSG_SET_COLOUR_MAP:
774 if (avail < 6) {
776 }
777 this->rx_pos_ += 6;
778 this->skip_(6u * read_be16(p + 4), VncStep::VNC_STEP_MESSAGE);
779 break;
780 case MSG_BELL:
781 this->rx_pos_ += 1;
782 break;
783 case MSG_SERVER_CUT_TEXT:
784 if (avail < 8) {
786 }
787 this->rx_pos_ += 8;
788 this->skip_(read_be32(p + 4), VncStep::VNC_STEP_MESSAGE);
789 break;
790 default:
791 ESP_LOGW(TAG, "Unknown message type %u", p[0]);
792 return this->fail_(VncFailure::VNC_FAILURE_TRANSIENT, LOG_STR("Protocol error"));
793 }
794 break;
795 }
797 if (this->display_busy_) {
799 }
800 if (avail < 12) {
802 }
803 this->rect_x_ = read_be16(p);
804 this->rect_y_ = read_be16(p + 2);
805 this->rect_w_ = read_be16(p + 4);
806 this->rect_h_ = read_be16(p + 6);
807 const auto encoding = static_cast<int32_t>(read_be32(p + 8));
808 this->rx_pos_ += 12;
809 this->rects_left_--;
810 if (encoding == ENCODING_RAW || encoding == ENCODING_HEXTILE) {
811 if (static_cast<uint32_t>(this->rect_x_) + this->rect_w_ > this->fb_w_ ||
812 static_cast<uint32_t>(this->rect_y_) + this->rect_h_ > this->fb_h_) {
813 ESP_LOGW(TAG, "Rectangle %ux%u at %u,%u is outside the %ux%u screen", this->rect_w_, this->rect_h_,
814 this->rect_x_, this->rect_y_, this->fb_w_, this->fb_h_);
815 return this->fail_(VncFailure::VNC_FAILURE_TRANSIENT, LOG_STR("Protocol error"));
816 }
817 if (this->rect_w_ == 0 || this->rect_h_ == 0) {
818 this->rect_done_();
819 } else if (encoding == ENCODING_RAW) {
820 this->forget_tiles_(this->rect_x_, this->rect_y_, this->rect_w_, this->rect_h_);
821 this->pos_x_ = 0;
822 this->pos_y_ = 0;
824 } else {
825 this->pos_x_ = this->rect_x_;
826 this->pos_y_ = this->rect_y_;
828 }
829 } else if (encoding == ENCODING_RICH_CURSOR) {
830 if (this->rect_w_ > MAX_CURSOR_SIDE || this->rect_h_ > MAX_CURSOR_SIDE) {
831 return this->fail_(VncFailure::VNC_FAILURE_TRANSIENT, LOG_STR("Protocol error"));
832 }
833 const uint32_t size = static_cast<uint32_t>(this->rect_w_) * this->rect_h_ * this->bpp_ +
834 ((this->rect_w_ + 7u) / 8u) * this->rect_h_;
836 } else {
837 ESP_LOGW(TAG, "Unsupported encoding %" PRId32, encoding);
838 return this->fail_(VncFailure::VNC_FAILURE_TRANSIENT, LOG_STR("Protocol error"));
839 }
840 break;
841 }
843 this->rect_done_();
844 break;
846 if (this->display_busy_) {
848 }
849 const size_t pixels = avail / this->bpp_;
850 if (pixels == 0) {
852 }
853 if (this->budget_exhausted_()) {
855 }
856 const auto count = static_cast<uint16_t>(std::min<size_t>(pixels, this->rect_w_ - this->pos_x_));
857 this->draw_row_(this->rect_x_ + this->pos_x_, this->rect_y_ + this->pos_y_, count, p);
858 this->rx_pos_ += count * this->bpp_;
859 this->pos_x_ += count;
860 if (this->pos_x_ == this->rect_w_) {
861 this->pos_x_ = 0;
862 if (++this->pos_y_ == this->rect_h_) {
863 this->rect_done_();
864 }
865 }
866 break;
867 }
869 if (this->display_busy_) {
871 }
872 if (avail < 1) {
874 }
875 if (this->budget_exhausted_()) {
877 }
878 const uint8_t flags = p[0];
879 this->tile_w_ = std::min<uint32_t>(16, static_cast<uint32_t>(this->rect_x_) + this->rect_w_ - this->pos_x_);
880 this->tile_h_ = std::min<uint32_t>(16, static_cast<uint32_t>(this->rect_y_) + this->rect_h_ - this->pos_y_);
881 this->tile_hidden_ = this->pos_x_ >= this->vis_w_ || this->pos_y_ >= this->vis_h_;
882 // With the raw bit set the other bits mean nothing and the pixels follow at once.
883 if (flags & HEXTILE_RAW) {
884 this->rx_pos_ += 1;
885 this->tile_fill_ = 0;
887 break;
888 }
889 const size_t need = 1 + ((flags & HEXTILE_BACKGROUND) ? this->bpp_ : 0) +
890 ((flags & HEXTILE_FOREGROUND) ? this->bpp_ : 0) + ((flags & HEXTILE_ANY_SUBRECTS) ? 1 : 0);
891 if (avail < need) {
893 }
894 const uint8_t *q = p + 1;
895 if (flags & HEXTILE_BACKGROUND) {
896 std::memcpy(this->hex_bg_, q, this->bpp_);
897 q += this->bpp_;
898 }
899 if (flags & HEXTILE_FOREGROUND) {
900 std::memcpy(this->hex_fg_, q, this->bpp_);
901 q += this->bpp_;
902 }
903 this->subrects_left_ = (flags & HEXTILE_ANY_SUBRECTS) ? *q : 0;
904 this->subrects_coloured_ = (flags & HEXTILE_SUBRECTS_COLOURED) != 0;
905 this->rx_pos_ += need;
906 this->fill_tile_(0, 0, this->tile_w_, this->tile_h_, this->hex_bg_);
908 break;
909 }
911 const size_t total = static_cast<size_t>(this->tile_w_) * this->tile_h_ * this->bpp_;
912 const size_t count = std::min(avail, total - this->tile_fill_);
913 if (count == 0) {
915 }
916 std::memcpy(this->tile_ + this->tile_fill_, p, count);
917 this->rx_pos_ += count;
918 this->tile_fill_ += count;
919 if (this->tile_fill_ == total) {
920 this->draw_tile_();
921 this->next_tile_();
922 }
923 break;
924 }
926 if (this->subrects_left_ == 0) {
927 this->draw_tile_();
928 this->next_tile_();
929 break;
930 }
931 const size_t need = (this->subrects_coloured_ ? this->bpp_ : 0) + 2;
932 if (avail < need) {
934 }
935 const uint8_t *pixel = this->hex_fg_;
936 const uint8_t *q = p;
937 if (this->subrects_coloured_) {
938 pixel = q;
939 q += this->bpp_;
940 }
941 this->fill_tile_(q[0] >> 4, q[0] & 0x0F, (q[1] >> 4) + 1, (q[1] & 0x0F) + 1, pixel);
942 this->rx_pos_ += need;
943 this->subrects_left_--;
944 break;
945 }
947 const auto count = static_cast<uint32_t>(std::min<size_t>(avail, this->skip_left_));
948 this->rx_pos_ += count;
949 this->skip_left_ -= count;
950 if (this->skip_left_ != 0) {
952 }
953 this->step_ = this->after_skip_;
954 break;
955 }
956 }
957 }
958}
959
961 if (this->rects_left_ == 0) {
962 this->frame_done_();
963 } else {
965 }
966}
967
970 if (static_cast<uint32_t>(this->pos_x_) + 16 < static_cast<uint32_t>(this->rect_x_) + this->rect_w_) {
971 this->pos_x_ += 16;
972 return;
973 }
974 this->pos_x_ = this->rect_x_;
975 if (static_cast<uint32_t>(this->pos_y_) + 16 < static_cast<uint32_t>(this->rect_y_) + this->rect_h_) {
976 this->pos_y_ += 16;
977 return;
978 }
979 this->rect_done_();
980}
981
983 this->in_update_ = false;
984 this->request_outstanding_ = false;
986 if (this->frame_drawn_) {
988 this->frame_drawn_ = false;
989 if (!this->refresh_pending_) {
990 this->first_change_ms_ = now;
991 }
992 this->last_change_ms_ = now;
993 this->refresh_pending_ = true;
994 ESP_LOGD(TAG, "Update: %" PRIu32 " bytes in %" PRIu32 "ms, %u hextile tiles drawn",
995 this->rx_total_ - (this->rx_len_ - this->rx_pos_) - this->update_start_bytes_,
996 millis() - this->update_start_ms_, this->drawn_tiles_);
997 }
998}
999
1000void VncClient::draw_row_(uint16_t x, uint16_t y, uint16_t count, const uint8_t *pixels) {
1001 if (y >= this->vis_h_ || x >= this->vis_w_) {
1002 return;
1003 }
1004 const int visible = std::min<int>(count, this->vis_w_ - x);
1005 this->display_->draw_pixels_at(x, y, visible, 1, pixels, this->bitness_, this->big_endian_);
1006 this->frame_drawn_ = true;
1007}
1008
1010 if (this->tile_hidden_) {
1011 return;
1012 }
1013 const int w = std::min<int>(this->tile_w_, this->vis_w_ - this->pos_x_);
1014 const int h = std::min<int>(this->tile_h_, this->vis_h_ - this->pos_y_);
1015 uint32_t *cell = nullptr;
1016 uint32_t hash = 0;
1017 if (this->hash_cols_ != 0) {
1018 if ((this->pos_x_ | this->pos_y_) & 15) {
1019 // An off-grid tile spans several cells, and a later write to any of them would make its hash stale.
1020 this->forget_tiles_(this->pos_x_, this->pos_y_, w, h);
1021 } else {
1022 cell = this->tile_hashes_ + (this->pos_y_ / 16) * this->hash_cols_ + this->pos_x_ / 16;
1023 hash = this->tile_hash_();
1024 if (*cell == hash) {
1025 return;
1026 }
1027 }
1028 }
1029 this->display_->draw_pixels_at(this->pos_x_, this->pos_y_, w, h, this->tile_, this->bitness_, this->big_endian_, 0, 0,
1030 this->tile_w_ - w);
1031 this->frame_drawn_ = true;
1032 this->drawn_tiles_++;
1033 if (cell != nullptr) {
1034 *cell = hash;
1035 }
1036}
1037
1039 // The tile's size, pixel size and pixels; the cell gives its place. Zero means "unknown".
1041 static_cast<uint32_t>(this->tile_w_) << 16 | this->tile_h_ << 8 | this->bpp_);
1042 const size_t len = static_cast<size_t>(this->tile_w_) * this->tile_h_ * this->bpp_;
1043 size_t i = 0;
1044 for (; i + 4 <= len; i += 4) {
1045 uint32_t word;
1046 std::memcpy(&word, this->tile_ + i, sizeof(word)); // tile_ is aligned, so this is one load
1047 // Byte by byte: XOR-ing whole words lets high-bit changes in two words cancel.
1048 hash = fnv1a_hash_extend(hash, word);
1049 }
1050 for (; i < len; i++) {
1051 hash = fnv1a_hash_extend(hash, this->tile_[i]);
1052 }
1053 return hash == 0 ? 1 : hash;
1054}
1055
1056void VncClient::forget_tiles_(uint16_t x, uint16_t y, uint16_t w, uint16_t h) {
1057 if (this->hash_cols_ == 0 || w == 0 || h == 0) {
1058 return;
1059 }
1060 const uint16_t col_end = std::min<uint16_t>((x + w - 1) / 16, this->hash_cols_ - 1);
1061 const uint16_t row_end = std::min<uint16_t>((y + h - 1) / 16, this->hash_rows_ - 1);
1062 for (uint16_t row = y / 16; row <= row_end; row++) {
1063 for (uint16_t col = x / 16; col <= col_end; col++) {
1064 this->tile_hashes_[row * this->hash_cols_ + col] = 0;
1065 }
1066 }
1067}
1068
1069void VncClient::fill_tile_(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *pixel) {
1070 if (this->tile_hidden_ || x >= this->tile_w_ || y >= this->tile_h_) {
1071 return;
1072 }
1073 w = std::min<uint8_t>(w, this->tile_w_ - x);
1074 h = std::min<uint8_t>(h, this->tile_h_ - y);
1075 const size_t stride = static_cast<size_t>(this->tile_w_) * this->bpp_;
1076 const size_t row_len = static_cast<size_t>(w) * this->bpp_;
1077 uint8_t *first = this->tile_ + y * stride + x * this->bpp_;
1078 if (this->bpp_ == 1) {
1079 std::memset(first, pixel[0], w);
1080 } else {
1081 for (size_t i = 0; i < row_len; i += 2) {
1082 first[i] = pixel[0];
1083 first[i + 1] = pixel[1];
1084 }
1085 }
1086 for (uint8_t row = 1; row < h; row++) {
1087 std::memcpy(first + row * stride, first, row_len);
1088 }
1089}
1090
1097 }
1098 const bool rgb565 = format == VncPixelFormat::VNC_PIXEL_FORMAT_RGB565;
1099 this->bpp_ = rgb565 ? 2 : 1;
1101
1102 const int disp_w = this->display_->get_width();
1103 const int disp_h = this->display_->get_height();
1104 this->vis_w_ = std::min<int>(this->fb_w_, disp_w);
1105 this->vis_h_ = std::min<int>(this->fb_h_, disp_h);
1106 if (this->fb_w_ != disp_w || this->fb_h_ != disp_h) {
1107 ESP_LOGW(TAG, "Server screen is %ux%u but the display is %dx%d; showing the top-left %ux%u", this->fb_w_,
1108 this->fb_h_, disp_w, disp_h, this->vis_w_, this->vis_h_);
1109 }
1110 const uint16_t cols = (this->vis_w_ + 15) / 16;
1111 const uint16_t rows = (this->vis_h_ + 15) / 16;
1112 const auto rotation = static_cast<uint8_t>(this->display_->get_rotation() / 90);
1113 const bool rotated = rotation != this->hash_rotation_;
1114 this->hash_rotation_ = rotation;
1115 if (this->tile_hashes_ == nullptr) {
1116 this->hash_cols_ = 0;
1117 this->hash_rows_ = 0;
1118 } else if (static_cast<uint32_t>(cols) * rows > this->hash_capacity_) {
1119 ESP_LOGW(TAG, "Screen has %u tiles, more than the %u cached; every tile is drawn", cols * rows,
1120 this->hash_capacity_);
1121 this->hash_cols_ = 0;
1122 this->hash_rows_ = 0;
1123 } else if (cols != this->hash_cols_ || rows != this->hash_rows_ || rotated) {
1124 this->hash_cols_ = cols;
1125 this->hash_rows_ = rows;
1126 this->clear_tiles_();
1127 }
1128
1129 // SetPixelFormat: true colour, in the byte order the display takes
1130 uint8_t pixel_format[20] = {};
1131 pixel_format[4] = rgb565 ? 16 : 8;
1132 pixel_format[5] = rgb565 ? 16 : 8;
1133 pixel_format[6] = this->big_endian_;
1134 pixel_format[7] = 1;
1135 put_be16(pixel_format + 8, rgb565 ? 31 : 7);
1136 put_be16(pixel_format + 10, rgb565 ? 63 : 7);
1137 put_be16(pixel_format + 12, rgb565 ? 31 : 3);
1138 pixel_format[14] = rgb565 ? 11 : 5;
1139 pixel_format[15] = rgb565 ? 5 : 2;
1140 if (!this->send_(pixel_format, sizeof(pixel_format))) {
1141 return false;
1142 }
1143
1144 // SetEncodings
1145 uint8_t encodings[4 + 4 * std::size(ENCODINGS)] = {2};
1146 uint8_t *p = put_be16(encodings + 2, std::size(ENCODINGS));
1147 for (auto encoding : ENCODINGS) {
1148 p = put_be32(p, static_cast<uint32_t>(encoding));
1149 }
1150 if (!this->send_(encodings, sizeof(encodings)) || !this->send_update_request_(false, now)) {
1151 return false;
1152 }
1154 this->session_start_ms_ = now;
1155#ifdef USE_VNC_AUTH
1156 this->auth_retried_ = false;
1157#endif
1158 this->clear_link_warning_();
1159 this->status_clear_error();
1160 this->connect_callback_.call();
1161 return true;
1162}
1163
1165 if (this->display_busy_ || this->in_update_ || now - this->last_request_ms_ < MIN_REQUEST_SPACING_MS) {
1166 return;
1167 }
1168 if (this->need_full_) {
1169 if (this->send_update_request_(false, now)) {
1170 this->need_full_ = false;
1171 }
1172 return;
1173 }
1174 // Repeating a parked request costs nothing on the server and detects a dead link.
1175 if (!this->request_outstanding_ ||
1176 (now - this->last_rx_ms_ >= HEARTBEAT_MS && now - this->last_request_ms_ >= HEARTBEAT_MS)) {
1177 this->send_update_request_(true, now);
1178 }
1179}
1180
1181bool VncClient::send_(const uint8_t *data, size_t len) {
1182 if (this->link_.tx_free() < len) {
1183 this->link_.flush_tx();
1184 if (this->link_.tx_free() < len) {
1185 return false;
1186 }
1187 }
1188 this->link_.queue(data, len);
1189 return true;
1190}
1191
1192bool VncClient::send_update_request_(bool incremental, uint32_t now) {
1193 uint8_t msg[10] = {3, static_cast<uint8_t>(incremental ? 1 : 0)};
1194 put_be16(msg + 6, this->vis_w_);
1195 put_be16(msg + 8, this->vis_h_);
1196 if (!this->send_(msg, sizeof(msg))) {
1197 return false;
1198 }
1199 this->request_outstanding_ = true;
1200 this->last_request_ms_ = now;
1201 return true;
1202}
1203
1204#ifdef USE_VNC_INPUT
1206 // A press is the down and the up event, sent whole or not at all.
1207 uint8_t msg[2 * KEY_EVENT_SIZE] = {MSG_KEY_EVENT, static_cast<uint8_t>(action != VncKeyAction::VNC_KEY_ACTION_UP)};
1208 msg[KEY_EVENT_SIZE] = MSG_KEY_EVENT;
1209 put_be32(msg + 4, keysym);
1210 put_be32(msg + KEY_EVENT_SIZE + 4, keysym);
1211 return this->send_input_(msg, action == VncKeyAction::VNC_KEY_ACTION_PRESS ? sizeof(msg) : KEY_EVENT_SIZE,
1213}
1214
1215// Input goes out after the releases that are still waiting. A release that does not fit waits for room,
1216// because a key or button left down would change every later key and click; other input is dropped.
1217bool VncClient::send_input_(const uint8_t *msg, size_t len, bool release) {
1218 if (this->send_pending_() && this->send_(msg, len)) {
1219 this->note_input_();
1220 return true;
1221 }
1222 if (!release) {
1223 return false;
1224 }
1225 if (this->pending_count_ == MAX_PENDING_RELEASES) {
1226 // Losing a release would leave its key or button held on the server
1227 this->fail_(VncFailure::VNC_FAILURE_TRANSIENT, LOG_STR("Too many releases waiting for the send buffer"));
1228 return false;
1229 }
1230 std::memcpy(this->pending_[this->pending_count_++], msg, len);
1231 return true;
1232}
1233
1235 while (this->pending_count_ != 0) {
1236 const uint8_t *msg = this->pending_[0];
1237 if (!this->send_(msg, msg[0] == MSG_POINTER_EVENT ? POINTER_EVENT_SIZE : KEY_EVENT_SIZE)) {
1238 return false;
1239 }
1240 this->note_input_();
1241 this->pending_count_--;
1242 std::memmove(this->pending_[0], this->pending_[1], this->pending_count_ * sizeof(this->pending_[0]));
1243 }
1244 return true;
1245}
1246
1247bool VncClient::send_pointer(uint16_t x, uint16_t y, uint8_t buttons) {
1248 if (!this->is_connected()) {
1249 ESP_LOGV(TAG, "Not connected, pointer event dropped");
1250 return false;
1251 }
1252 x = std::min<uint16_t>(x, this->fb_w_ - 1);
1253 y = std::min<uint16_t>(y, this->fb_h_ - 1);
1254 uint8_t msg[POINTER_EVENT_SIZE] = {MSG_POINTER_EVENT, buttons};
1255 put_be16(msg + 2, x);
1256 put_be16(msg + 4, y);
1257 const bool release = buttons != this->ptr_buttons_ && (buttons & ~this->ptr_buttons_) == 0;
1258 if (!this->send_input_(msg, sizeof(msg), release)) {
1259 ESP_LOGW(TAG, "Send buffer full, pointer event dropped");
1260 return false;
1261 }
1262 this->ptr_x_ = x;
1263 this->ptr_y_ = y;
1264 this->ptr_buttons_ = buttons;
1265 return true;
1266}
1267
1269
1271 if (!this->is_connected()) {
1272 ESP_LOGV(TAG, "Not connected, key dropped");
1273 return;
1274 }
1275 if (!this->send_key_event_(keysym, action)) {
1276 ESP_LOGW(TAG, "Send buffer full, key dropped");
1277 }
1278}
1279
1280void VncClient::type_text(const char *text, size_t len) {
1281 if (!this->is_connected()) {
1282 ESP_LOGV(TAG, "Not connected, text dropped");
1283 return;
1284 }
1285 size_t typed = 0;
1286 for (size_t i = 0; i < len;) {
1287 // The decoder reads up to a NUL, so give it only the bytes that belong to this text.
1288 char sequence[5] = {};
1289 std::memcpy(sequence, text + i, std::min<size_t>(4, len - i));
1290 size_t used;
1291 const uint32_t cp = unicode::extract_unicode_codepoint(sequence, &used);
1292 // A broken sequence or a NUL types nothing; carry on at the next byte.
1293 i += used == 0 ? 1 : used;
1294 const uint32_t keysym = keysym_for_codepoint(cp);
1295 if (keysym == 0) {
1296 continue;
1297 }
1299 ESP_LOGW(TAG, "Send buffer full, text cut after %u characters", static_cast<unsigned>(typed));
1300 return;
1301 }
1302 typed++;
1303 }
1304}
1305#endif
1306
1307#ifdef USE_VNC_TOUCHSCREEN
1308void VncClient::handle_touch_(uint16_t x, uint16_t y) {
1309 if (!this->is_connected()) {
1310 return;
1311 }
1312 if (!this->touching_) {
1313 if (x >= this->vis_w_ || y >= this->vis_h_) {
1314 return;
1315 }
1316 this->press_x_ = x;
1317 this->press_y_ = y;
1318 this->dragging_ = false;
1319 this->touching_ = this->send_pointer(x, y, 1);
1320 return;
1321 }
1322 x = std::min<uint16_t>(x, this->vis_w_ - 1);
1323 y = std::min<uint16_t>(y, this->vis_h_ - 1);
1324 // Small jitter during a tap would start a text selection in a browser.
1325 if (!this->dragging_) {
1326 if (std::abs(x - this->press_x_) <= TOUCH_SLOP_PX && std::abs(y - this->press_y_) <= TOUCH_SLOP_PX) {
1327 return;
1328 }
1329 this->dragging_ = true;
1330 }
1331 if (x != this->ptr_x_ || y != this->ptr_y_) {
1332 this->send_pointer(x, y, 1);
1333 }
1334}
1335
1337 if (this->touching_) {
1338 this->touching_ = false;
1339 this->send_pointer(this->ptr_x_, this->ptr_y_, 0);
1340 }
1341}
1342#endif
1343
1344} // namespace esphome::vnc
uint8_t h
Definition bl0906.h:2
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
bool has_overridden_loop() const
Definition component.h:339
bool is_failed() const
Definition component.h:287
void status_clear_error()
Definition component.h:315
void enable_loop()
Enable this component's loop.
Definition component.h:261
bool status_has_warning() const
Definition component.h:293
void disable_loop()
Disable this component's loop.
void status_clear_warning()
Definition component.h:309
bool is_idle() const
Check if this component is idle.
Definition component.h:217
void stop()
Stop running the loop continuously.
Definition helpers.cpp:841
void start()
Start running the loop continuously.
Definition helpers.cpp:835
virtual void update()=0
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:2396
T * allocate(size_t n)
Definition helpers.h:2423
virtual int get_height()
Get the calculated height of the display in pixels with rotation applied.
Definition display.h:338
virtual DisplayRotation get_rotation() const
Definition display.h:751
virtual int get_width()
Get the calculated width of the display in pixels with rotation applied.
Definition display.h:336
virtual DisplayType get_display_type()=0
Get the type of display that the buffer corresponds to.
void draw_pixels_at(int x_start, int y_start, int w, int h, const uint8_t *ptr, ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad)
Given an array of pixels encoded in the nominated format, draw these into the display's buffer.
Definition display.h:370
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)
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
LazyCallbackManager< void()> connect_callback_
Definition vnc_client.h:196
void send_key(uint32_t keysym, VncKeyAction action)
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
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().
bool has_auth_key_() const
Definition vnc_client.h:151
void service_rx_(uint32_t now)
void type_text(const char *text, size_t len)
uint32_t tile_hash_() const
void update(const touchscreen::TouchPoints_t &tpoints) override
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 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
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 on_shutdown() override
bool send_update_request_(bool incremental, uint32_t now)
void schedule_retry_(uint32_t now, VncFailure failure, const LogString *reason, bool was_running)
void skip_(uint32_t count, VncStep next)
void touch(touchscreen::TouchPoint tp) override
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)
uint16_t flags
uint8_t type
NameCopy * next
unsigned count
Definition gif.cpp:51
__int64 ssize_t
Definition httplib.h:178
const char * format
ESPHOME_ALWAYS_INLINE bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
Definition util.h:36
std::vector< TouchPoint > TouchPoints_t
Definition touchscreen.h:29
uint32_t extract_unicode_codepoint(const char *utf8_str, size_t *length)
Attempt to extract a 32 bit Unicode codepoint from a UTF-8 string.
Definition unicode.cpp:19
ESPHOME_LOG_TAG(TAG, "vnc")
void vnc_auth_response(const uint8_t *key_progmem, uint8_t challenge[16])
VNC Authentication response: encrypts both 8-byte halves of the challenge in place.
Definition vnc_des.cpp:153
PROGMEM_STRING_TABLE(PixelFormatStrings, "auto", "RGB565", "RGB332")
constexpr uint32_t fnv1a_hash_extend(uint32_t hash, const char *str)
Extend a FNV-1a hash with additional string data.
Definition helpers.h:995
constexpr uint32_t FNV1_OFFSET_BASIS
FNV-1 32-bit offset basis.
Definition helpers.h:968
uint32_t value
Definition hal.cpp:41
const void size_t len
Definition hal.h:68
uint16_t size
Definition helpers.cpp:34
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
Definition helpers.h:1051
constexpr uint8_t * put_be16(uint8_t *dst, uint16_t value)
Write the low 16, 24 or 32 bits of value to dst, most significant byte first (be) or least significan...
Definition helpers.h:1112
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1043
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
Application App
Global storage of Application pointer - only one Application can exist.
constexpr uint8_t * put_be32(uint8_t *dst, uint32_t value)
Definition helpers.h:1123
static void uint32_t
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6