mirror of
https://github.com/rustdesk/rustdesk.git
synced 2026-09-06 16:11:02 +03:00
* feat: add rendezvous WebRTC signaling fields * feat: route WebRTC ICE on controlled side * feat: race WebRTC as a direct transport enhancement * fix: route WebRTC ICE through rendezvous paths * feat: WebRTC transport racing, DTLS identity binding, and pc-leak fixes - prefer-P2P racing (race_transports_prefer_webrtc) across punch and RelayResponse; ICE bridge with 400ms candidate resend - controlled-side answerer and ICE routing; sign local DTLS fingerprint into SignedId, controller verifies the binding fail-closed - fix pc leaks: close_webrtc() on insecure-decline paths (io_loop, port_forward); compute direct before disarming the offerer guard - point hbb_common to the WebRTC data-plane commit 9f5a296 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * fix: preserve WebRTC transport preference * feat: decouple WebRTC from UDP punch, route controlled signaling over TCP - the WebRTC offer now rides any punch request; only an offer-less request may close and reuse the rendezvous socket for TCP punching (request_allows_tcp_punch replaces the udp_port-based invariant), with a separate offer-less request racing as the TCP fallback - WebSocket mode no longer disables WebRTC — ws only tunnels the signaling/relay legs while ICE stays the only P2P path there; SOCKS proxy still disables it (ICE would bypass the proxy and leak the real IP) - controlled side: WebRTC-only punch replies and trickled ICE candidates go over dedicated TCP connections to the rendezvous server instead of the UDP mediator channel, for ws/TCP-only hbbs deployments; drop the now-redundant rz_sender plumbing and the 400ms candidate re-send on that leg - guard is_udp handling against responses to requests that advertised no udp_port; skip the IPv6 socket bind under force-relay - test_udp_uat: drop the STUN port race — the punch port must come from the rendezvous server's TestNatResponse observing this socket's mapping, a STUN probe from another socket can advertise an unreachable port - bump hbb_common (webrtc 0.13 MSRV pin rationale + upgrade checklist docs) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: KCP/UDP resilience to ICMP resets; optional KCP congestion control - treat ICMP-driven UDP socket errors (WSAECONNRESET 10054 on Windows, ECONNREFUSED on Linux) as packet loss in punch_udp and the KCP pump instead of tearing the session down; KCP retransmits through them and a truly dead link is still reaped by the pong/app-level timeouts - resolve STUN hostnames via tokio::net::lookup_host so DNS never blocks a runtime worker; fix the inverted non-IPv4 error message - add enable-kcp-congestion-control option (default on): switch the turbo profile to nc=0 so brief loss on constrained links no longer spirals into stalls; sender-side only, no wire negotiation - pin kcp-sys to the rustdesk-patches branch: upstream main lost the RustDesk patches on the EasyTier sync, and this branch also wires set_kcp_config_factory into connection setup, making the option effective Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: carry switch_code through WebRTC relay fallbacks after rebase The rebase onto master (switch-code feature) added an 8th request_relay parameter; pass the interface's switch code from both WebRTC->relay fallback paths so a role-swap session survives the fallback. Also drop a duplicate bindgen 0.72.1 entry the Cargo.lock merge produced. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ExUfAkYbq8UC9pQCiLy8TQ * fix: don't let the preferred branch's own relay preempt a direct fallback race_transports_prefer_webrtc committed any success from its first argument outright, on the assumption that it is the WebRTC connect. It is not: the call site passes a whole punch attempt, which internally falls back to request_relay when its direct transports fail. That relay was therefore committed instantly while the offer-less fallback's TCP punch was still in flight — inverting the preference this function exists to enforce, since the is_p2p predicate the caller already supplies was applied only to the `others` branch. Apply it to both branches: a direct result from either side still commits immediately, and a relayed result from either side is held for the window so the other side can land something direct. Also commit a held connection when the surviving branch errors, which the previous code only did on the first branch's failure path. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ExUfAkYbq8UC9pQCiLy8TQ * fix: evict the oldest pending ICE candidate, not the newest Candidates arrive in gathering order — host, then srflx, then relay — so a full buffer was discarding exactly the ones that traverse NAT while keeping host ones that only work on a shared LAN. Evict from the front instead. Also document why the controller's ICE bridge must not reconnect on error, in contrast to the controlled side's per-candidate retry: its socket address is the return route itself (mangled into PunchHole.socket_addr, echoed back in IceCandidate.socket_addr, resolved through tcp_punch), so a reconnect would arrive from an address no route points at, and the server drops the old entry when the connection closes. Once it dies both directions are dead, and abandoning WebRTC is the correct response rather than retrying. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ExUfAkYbq8UC9pQCiLy8TQ * fix: bound log volume on sites whose rate a peer or retry loop controls Debug output goes to the log file, so a site that fires per received message or per retry lets someone else decide how much a machine writes to disk. The WebRTC work added the first such sites. - KCP io loop: absorbing ICMP errors as packet loss made a broken socket write ~100 lines a second for the 60s until the pong timeout reaps it. Log by run instead: one line when a run starts, one per ~5s while it persists so a stuck socket stays visible, and one on recovery with the total. - punch_udp: the recv error retries every 10ms for up to MAX_TIME, so one line per occurrence wrote thousands per punch. Log the first, report the count in the timeout message. - ICE candidate paths (client, mediator): the peer sets the candidate rate and the rendezvous route carrying them needs no prior punch, so throttle to one line a minute each with the suppressed count. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ExUfAkYbq8UC9pQCiLy8TQ * fix: the KCP io throttle reset itself every cycle, so it never throttled The send and recv arms shared one counter, and an ICMP error on a connected socket is reported once and then cleared — so the steady state is an alternation: the send succeeds and clears the counter, the next recv reports the error and finds the counter at 1, and logs. Every error still wrote a line, at the ~100/s the previous commit set out to stop, while the persistent-failure and recovery branches were unreachable. Use one LogThrottle per direction instead of a hand-rolled counter. That removes the shared state the bug lived in, drops a third throttling mechanism in favour of the one already added, and leaves the surrounding `if let Err` untouched rather than reshaping it into a match. Also fix test_udp_uat's socket-error arm, the untreated twin of the punch_udp site: it had no backoff at all, so a persistent error re-armed recv immediately and spun the loop at CPU speed, one warn line per iteration. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ExUfAkYbq8UC9pQCiLy8TQ * bump kcp-sys: 14 review fixes on rustdesk-patches (6e44b93 -> fa51c15) Picks up the handshake-recovery work plus the review round on top of it: ABBA deadlock between the endpoint's two DashMaps, graceful-close tail truncation, mid-stream hole on ikcp_send failure, FIN retransmission for lost-FIN half-open hangs, SYN-ACK budget burned on dropped packets, spurious ConnectTimeout after a completed handshake, accept-backlog overflow stranding conns, aliasing UB in the output callback, and the log-facade/throttling cleanup (per-packet sites no longer reach the debug-level file logger, peer-rate warns throttled). Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ExUfAkYbq8UC9pQCiLy8TQ * ws: decouple ICE policy from force_relay — full-ICE WebRTC over WebSocket WebSocket support folds into force_relay because a ws tunnel kills classic TCP/UDP punching — but that conflated transport necessity with relay policy, and the WebRTC decisions keyed off the merged flag: a ws client built no offerer at all without TURN, and only a Relay-only-ICE one with it. ws deployments could never reach a direct WebRTC connection, which is exactly the path they are supposed to live on. Split the flag. LoginConfigHandler now tracks policy_relay (the force-always-relay option, an explicit relay request — /r ids and retry-via-relay included — and proxy) separately; force_relay stays policy_relay || use_ws() and keeps governing the classic paths, so non-ws behavior is unchanged everywhere: - the offerer's existence and ICE policy follow policy_relay: under pure ws the offer gathers every candidate type and may go direct; under relay-by-policy it stays Relay-only ICE, TURN-gated, exactly as before; - the RelayResponse race applies the prefer-P2P window under ws (a direct ICE path is worth delaying an already-ready relay for) while policy relay keeps first-success semantics; - the request carries webrtc_all_ice (hbb_common 64b54ab) so the controlled side knows the offer is full-ICE: it answers with full ICE and no TURN requirement, while offers without the bit keep today's relay-only answer path on every version-skew combination. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ExUfAkYbq8UC9pQCiLy8TQ * bump kcp-sys: 7 review fixes on rustdesk-patches (fa51c15 -> 023a006) Reverts the connect/accept/add_conn changes that regressed concurrent connects (the state_map guard held across add_conn is load-bearing), states the single-conn contract on KcpEndpoint so shared-endpoint behaviour stops consuming review effort, pins the two invariants that keep truncated input from aborting under panic='abort', and fixes three findings from external review: sendwnd() echoing raw config instead of KCP's effective window (a non-positive factory value stalled sending forever), the passive closer's lost final FIN delaying EOF by up to ~20s, and the doubled window overflowing for extreme factory values. Lock-only change: cargo update -p kcp-sys also re-picked libloading's windows-targets between two versions already present in the lock; that was reverted to keep this commit to the one line it is about. cargo metadata --locked passes on the result. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * ws: read the all-ICE declaration from the offer envelope, drop the proto field Companion to hbb_common 68d2729: the full-ICE declaration now lives as an `ice_policy: "all"` key inside the webrtc:// envelope, so the request assembly no longer sets webrtc_all_ice and the controlled side asks the envelope (endpoint_declares_all_ice) instead of a PunchHole field. The rendezvous server carries the offer opaquely — no forwarding to keep in sync. Skew behavior is unchanged: an unmarked or unparseable envelope reads as the old Relay-only semantics. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ExUfAkYbq8UC9pQCiLy8TQ * add enable-webrtc option; gate test_ipv6 under forced relay OPTION_ENABLE_WEBRTC (hbb_common 48c2d4d) follows the udp/ipv6 punch options end to end: default on against the public server, off against private ones, same settings UI placement on desktop and mobile, and the same bool2option local-option handling. Gates: - controller: should_create_webrtc_offerer checks it first — no pc, no STUN/TURN gathering, no offer in the request; - controlled: unlike the udp/ipv6 legs, which deliberately follow the request, answering builds a pc that gathers ICE from this host, so the answerer honors this machine's own switch too. Translations for "Enable WebRTC P2P connection" added to all 50 lang files next to the IPv6 entry (IPv6 and WebRTC are invariant terms in the same grammatical slot in every one of them). Also stop probing v6 reachability (test_ipv6) under any forced relay: the v6 punch socket is never bound there, so the probe was wasted work on every ws/proxy/relay connection. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ExUfAkYbq8UC9pQCiLy8TQ * kcp: client-side integration tests over real loopback sockets kcp-sys has been through two review rounds of behavioral fixes; the client wrapper (kcp_io pumps, connect/accept deadlines, framed-stream adaptation, guard lifetimes) had no tests pinning what rustdesk actually relies on. Four now do, each through real 127.0.0.1 UDP sockets and the BytesCodec framing sessions use: - handshake + bidirectional framed roundtrip + graceful close: the peer observes end-of-stream instead of hanging (guard outlives the framed stream so the FIN goes out); - a writer that queues 50 frames and closes immediately loses none of them - the client-side pin for the close-tail-drain semantics; - socket errors after the peer vanishes are treated as loss: writes keep succeeding, nothing tears down (ICMP is advisory on connected UDP); - the connect deadline holds when nothing answers. Mutation-checked: dropping inbound forwarding in kcp_io reddens exactly the three tests that need the pump, and the timeout test alone stays green. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ExUfAkYbq8UC9pQCiLy8TQ * ipc/auth: replace the local throttle with the shared throttled_log! auth.rs predated hbb_common's LogThrottle and grew its own equivalent: same shape (last_log_at + suppressed), same 5s interval, plus a helper and three OnceLock<Mutex<..>> statics. It also counted the other way - excluding the event being reported - so each of the three sites carried two near-identical log::warn! arms to avoid printing "suppressed 0". The shared macro covers all of it: one static per call site declared by the expansion, and the multiplicity suffix appears only when there is one, which is what those duplicated arms were for. 102 lines out, 27 in. Behavior difference, deliberate: a burst now reads "(x47)" - the total including this line - instead of "(suppressed 46 similar events)". One number, no arithmetic, and one convention across the codebase. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ExUfAkYbq8UC9pQCiLy8TQ * kcp: make the congestion-control profile opt-in, not the default The branch had flipped KCP to nc=0 (built-in congestion window) for every session. That is a transport-behavior change for all users made on reasoning alone, and the reasoning does not decide it: which profile wins depends on why packets are being lost. nc=1 - what RustDesk has always shipped - never shrinks the send window, so on a genuinely congested uplink it deepens the loss it is reacting to. But nc=0's backoff is blunt: a fast retransmit halves the window while an RTO sets cwnd = 1 outright (ikcp.c) and recovery slow-starts from one packet, so on a link with random loss and no congestion - Wi-Fi interference, a long-haul path - it reads loss as congestion and can stall an interactive stream for seconds. That failure mode is also the more visible one to a remote-desktop user. No benchmark settles this either: a loopback A/B has no bottleneck queue, hence no congestion to control, and would flatter nc=1 by construction. Deciding it needs a shaped link or field data. So keep the profile users already run and let the other one be asked for ("enable-kcp-congestion-control" = "Y"). Flipping the default later is a one-line change once there is evidence. kcp-sys keeps its own test covering the nc=0 path. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ExUfAkYbq8UC9pQCiLy8TQ * android: define getifaddrs/freeifaddrs for the api-21 sysroot Turning on hbb_common's "webrtc" feature pulls webrtc-util into the android link, and its ifaces() -- reached from vnet::Net::new() on every ICE gather -- calls getifaddrs(). bionic exports getifaddrs/freeifaddrs only from API 24, while flutter/ndk_*.sh builds against --platform 21, so every abi failed to link on the undefined symbols. Raising the platform to 24 would have to drag minSdkVersion 22 with it and turn the link error into a load-time one on Android 5.1/6.0, so define the two symbols instead, using the RTM_GETLINK + RTM_GETADDR netlink dump bionic itself uses. The definition also shadows bionic's on API >= 24 rather than delegating to it, so the path that ships is the path every test device runs. Checked against synthesised netlink dumps on the host -- link/address parsing, prefix masks, point-to-point, ipv6 scope ids, malformed and truncated messages -- under UBSan and byte-exact guard malloc, with a deliberately unsigned remainder as the negative control. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix three ways ws + WebRTC could not work in practice Review of #15684 and hbb_common#579. Each of these left the code reading correct while the feature did not function. - The RelayResponse race classified P2P with `result.2 == "IPv6"`, but that site's futures are only ever the relay ("Relay"/"WebSocket") and the WebRTC branch's own "WebRTC" — so the predicate was constantly false. When the relay landed first the result was still right (the webrtc arm's `others_fut.is_none()` fallback), but when WebRTC connected FIRST it was parked as if it were a relay and the relay was committed on arrival, discarding a live direct connection. That is the LAN case: the better the network, the worse the outcome. Classify by what the label means, via is_direct_transport, and test both orderings — only the relay-first one was covered. - handle_peer_info wrote "force-always-relay=Y" into the peer's saved config whenever force_relay was set, which now includes the WebSocket transport. One ws session therefore turned the peer into a permanent relay-by-policy peer, and relay-by-policy means Relay-only ICE, so WebRTC could never go direct to it again — the flagship path worked exactly once. Persist policy_relay, which is the user's choice; the transport is a property of this client, not of the peer. - The answerer gated on this machine's enable-webrtc option, but that is LocalConfig: the UI process writes it and never syncs it over IPC, while handle_punch_hole runs in the server process, which on Windows resolves LocalConfig under a different profile and reads the private-server default of "N". The gate refused to answer in exactly the self-hosted deployments the transport exists for. Drop it: the answerer follows the request, like the udp/ipv6 legs, and the option still gates the feature where it can — an offer only exists because some controller had it enabled. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ExUfAkYbq8UC9pQCiLy8TQ * webrtc: close without an await point; do not report an unknown path as direct - close_webrtc is no longer async (hbb_common 88f965f), so the ten call sites in port_forward and io_loop - all inside select! arms or futures the UI can abandon - can no longer be cancelled mid-teardown, which left the pc unclosable and its session entry stranded. Client's own spawn_close_webrtc went with it: the runtime-teardown guard it existed for now lives in close_detached, so both Drop paths share one implementation. - webrtc_relayed() returns None when no candidate pair is selected or the pc closed under a concurrent teardown, and both call sites read that as "not relayed", i.e. direct. A TURN-relayed session could therefore be shown to the user as peer-to-peer. Claiming a direct path needs evidence of one, so an unknown answer now counts as relayed. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ExUfAkYbq8UC9pQCiLy8TQ * scrap/benchmark: give the Duration divisor an explicit u32 The webrtc feature pulls time 0.3 into scrap's graph (hbb_common -> webrtc -> webrtc-dtls -> der-parser -> asn1-rs), and that crate carries an `impl Div<time::Duration> for std::time::Duration`. Orphan rules allow it because the RHS is its own type, and trait impls are visible across the whole dependency graph without a use, so std::time::Duration now has two Div candidates. `yuv_count as _` casts to a plain inference variable, which both candidates fit, so it stops resolving: error[E0282]: type annotations needed --> libs/scrap/examples/benchmark.rs:146:33 Only two of the four sites are reported - rustc emits one E0282 per function body - so all four are annotated. The already-explicit `as u32` at the hwcodec site and `start.elapsed() / cnt` are unaffected, the latter because an integer literal's variable can only unify with an integral type and rules the time impl out on its own. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * webrtc: judge the race by the resolved path, not the label; bound the ICE queue Third review round. Two of these are regressions from the previous one. - The RelayResponse race predicate was `is_direct_transport(result.2)`, which answers true for the label "WebRTC" - but WebRTC is only a direct path when ICE nominated a non-TURN pair. A TURN-relayed WebRTC result therefore committed instantly and cancelled the IPv6 attempt racing beside it, which is the same inversion the previous fix removed in the other direction. (That fix was also argued from a wrong premise: the site does carry an IPv6 future, pushed ~50 lines earlier than the relay one.) Each future now resolves whether its path is direct and the predicate reads that bool, matching the outer race, and the downstream recomputation goes away. - policy_relay still folded in Config::is_proxy(), and that is what gets persisted into the peer's config as force-always-relay - so one session through a proxy pinned the peer to relay forever and disabled WebRTC for it, exactly the latch the previous round fixed for WebSocket. Split out peer_relay: the saved option or an explicit request for THIS peer, and the only part written back. - The controlled side buffered remote ICE candidates in an unbounded channel while the controller caps the same buffer at 64, and draining one costs a JSON parse plus the ICE agent's lock. Whoever can reach a session's route could grow it without limit inside the long-lived service process. Bounded, with the overflow logged through the existing throttle. - That route was also removed by key alone when an answerer finished, so a punch retry that built a fresh answerer under the same fingerprint had its live sender deleted by the previous one's cleanup - after which it received no candidates at all. Evict only our own sender, the way the session cache already guards the analogous case. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ExUfAkYbq8UC9pQCiLy8TQ * webrtc: trim the comments to AGENTS.md length; drop is_direct_transport 386 added comment lines down to 287 across client, mediator, kcp_stream and common. Same rule as hbb_common 3d64e43: out go past-bug narration, rejected alternatives, measurements and restatements of the code; the non-derivable why stays. is_direct_transport goes with them. Judging the race by a transport label was replaced by the resolved direct flag, leaving it used only by its own test — and, having been inserted between the doc comment and race_transports_prefer_webrtc, it had also taken that function's contract with it. Removing it reattaches the doc where it belongs. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ExUfAkYbq8UC9pQCiLy8TQ * webrtc: fix race edge cases that discard or mislabel a direct connection Three correctness fixes in the transport race, plus three convention cleanups. - race_transports_prefer_webrtc committed a relayed result while a direct attempt was still in flight: the others arm returned on webrtc_fut.is_none() even with an unfinished direct future, and the WebRTC-error arm returned a held relay without checking others_fut. A relay is now committed only when nothing direct can still arrive (or the window expires); a parked relay is also preferred over composing an error when both sides fail. Three regression tests, mutation-checked. - connect()'s plain select_ok let a TURN-relayed WebRTC win as "first success", dropping still-racing UDP/IPv6 direct attempts and reporting the relayed pair as direct. It now runs through the same prefer-P2P race with each attempt carrying whether its path is direct, and the WebRTC future resolves is_relayed() so a TURN win is held behind direct attempts, not committed as one. - The RelayResponse path kept direct == true when a WebRTC win's DTLS handshake failed and it fell back to relay, so the relay was reported P2P. Clear the flag with the transport switch. - Trim the OffererGuard doc to the three-line max; move the new enable-webrtc localization key to the end of every lang list; the KCP option constant moved to hbb_common config::keys (0f663aa). Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ExUfAkYbq8UC9pQCiLy8TQ * bump hbb_common: WebRTC peer connections own their I/O runtime Closing the controlling window left the controlled side waiting out ICE decay — ~25-30s in the peer's log, its disconnected/failed ladder running to completion — where TCP delivers a FIN at once. The session end closed the pc by spawning onto io_loop's own `#[tokio::main(flavor = "current_thread")]` runtime, which is dropped the moment io_loop returns, and nothing after that call yields: the task was never polled even once, so no DTLS close_notify ever left. Every attempt to fix that on the caller's side failed the same way, because the mismatch was never about where the close ran: a pc's UDP sockets register with the reactor, and its ICE/DTLS/SCTP pumps spawn on the runtime, that is current while it is built — so a pc created by a session outlives the only runtime that can drive its I/O, and a close driven anywhere else completes without reaching the wire. The bump homes them where they can outlive any caller: WebRTCStream builds on a process-lifetime runtime and every detached close runs there as its own never-cancelled task. io_loop keeps its plain close_webrtc() calls and only documents why nothing here may spawn or await the teardown on the dying session runtime. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016HV43uh1ztv6Wm5qi3Y1ne * fix: give the UDP NAT test a real window when the TCP clock is faked The punch request carries udp_port only if the rendezvous server's TestNatResponse has arrived, and the wait for it was bounded by rtt / 2 — half the TCP connect time, on the assumption that TCP and UDP round trips are comparable and the test, started earlier, has already answered. A transparent TCP proxy breaks that assumption: a TUN-mode VPN on the host, or a redirect-mode proxy on the LAN gateway serving every device behind it, completes the handshake locally in ~3ms while the real UDP round trip is hundreds of ms. Log-confirmed against 5.161.65.208: ping 341ms, TCP connect 3.7ms, connect to a dead port there "succeeds" just as fast. The window collapsed to ~1.5ms, udp_port stayed 0 on every attempt, and UDP punch was never even requested — although UDP itself passes such gateways untouched. So use the TCP clock only when it is believable: below a plausible WAN round trip it says nothing about the UDP path, and a flat ceiling applies instead. The loop still exits the moment the port arrives, so a genuinely nearby server pays nothing and only a UDP-dead network waits out the ceiling — on the udp-carrying round alone, while the parallel pure-TCP round is unaffected. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016HV43uh1ztv6Wm5qi3Y1ne * feat: make the TCP punch a user option, with TCP as the backstop TCP punching was the one direct transport without a switch, while UDP, IPv6 and WebRTC each had one. Add "Enable TCP hole punching" above the UDP toggle on both desktop and mobile, default on — including on self-hosted servers, since unlike the other three (whose default-off there guards against an hbbs that cannot forward their fields) TCP punching has always been supported by every server. Turning all four off would leave no way to punch at all, so TCP runs regardless in that case. That backstop keys off the switches alone: a transport that is enabled but fails to materialize — no public v6 address, no NAT port, a failed offerer — is already covered by the relay fallback for a round that ends up with no usable direct transport. With the TCP punch off, the fallback request is skipped too: it exists only to carry that punch, and would otherwise reach connect() with nothing to try and merely open a second relay. Known cost, unchanged behavior for the peer: the request carries no field for this choice, so a peer that receives one with no udp_port and no offer still punches a TCP hole and listens for a connection the controller will not make. Representing the transport choice on the wire needs a proto field and the server forwarding it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016HV43uh1ztv6Wm5qi3Y1ne * bump hbb_common: name the punch by every transport it carries `get_local_endpoint_trickle` became `local_endpoint() -> &str`, which cannot fail, so both call sites lose an unreachable error arm — the mediator's closed a pc against a failure that no longer exists. `punch_type` named one transport, and picked it off `allow_tcp_punch`. A round carries several at once — a NAT port and a v6 address and an offer — and since the TCP punch became a switch it can carry none, so one name had to misreport both: the logs of the round that broke WebRTC read "#1 UDP punch attempt" while the request also carried the v6 address and the offer that was actually failing, and a round with nothing to punch with was labelled "WebRTC". List them instead — "UDP+IPv6+WebRTC" — and call the empty round "Relay", which is what it can still end as and what `typ` prints for it. The offer is moved into the request rather than cloned into it; that was its last use. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019UzcMTdYTEv2QbMHcTSUy3 * bump hbb_common: drop link-local IPv6 from ICE gathering Also pin webrtc-util to a fork of 0.11.0 carrying a Windows IPv6 enumeration fix. `ifaces` reads the adapter list's on-wire IPv6 bytes as host-order `[u16; 8]`, so on a little-endian host every group comes out byte-swapped and unbindable: a peer's real 240e:369:9606:4600:f52a:7a8d:2530:4de0 is enumerated as e24:6903:696:46:2af5:8d7a:3025:e04d, ::1 as ::100 and fe80:: as 80fe::. Each fails to bind with WSAEADDRNOTAVAIL, so ICE gathers no IPv6 host candidate at all on Windows - where a globally routable address is the one NAT-free path a CGNAT'd peer has. Never reported upstream; the unix twin of the same bug was fixed in webrtc-rs#475 (2023). Fork: rustdesk-org/webrtc, branch rustdesk-patches, tag webrtc-util-0.11.0-win-ipv6. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019UzcMTdYTEv2QbMHcTSUy3 * bump hbb_common: name the family a WebRTC session runs over `stream_type` reaches the UI as the transport that won the race, and every other transport already carries the family in that label - the v6 punch reports `IPv6`. WebRTC does not: one label covers both families, and it is the one path whose real remote address can differ from the rendezvous-observed one the session is identified by. Refine it at the hand-off to the UI rather than at the source: five sites in client.rs compare `typ == "WebRTC"`, so widening the label there would silently move control flow. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019UzcMTdYTEv2QbMHcTSUy3 * bump hbb_common: one STUN list, and drop the dead IPv4 half `test_ipv6` kept its own hand-written copy of the STUN servers. It now reads `WebRTCStream::stun_servers()`, so an operator who points OPTION_ICE_SERVERS at their own server gets it on both paths instead of one. `test_bind_ipv6` sends nothing - `connect` only makes the kernel pick a route and a source address - so the whole cost is DNS. It races the lookups rather than betting this host's IPv6 support on whether the first entry happens to publish a AAAA where the user resolves from; google's does not, from a Chinese resolver, and it was the entry being bet on. `stun_ipv4_test`, `STUNS_V4` and `test_nat_ipv4` have had no callers since the punch stopped taking its port from a second socket, and go. `get_kcp_cc_enabled` reads the renamed option through `option2bool`, like every other one. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019UzcMTdYTEv2QbMHcTSUy3 * webrtc: take dcsctp's retransmission timings and IPv6-safe MTU webrtc-sctp ships RFC 4960's RTO.Initial/RTO.Min (3000/1000), TCP's values for arbitrary public paths. On this workload they set the recovery time outright: a request/response exchange keeps one chunk in flight, so no later SACK ever raises miss_indicator to the 3 that arms fast retransmit, and the T3 floor is the only way back. A single loss during a handshake or a first keyframe therefore costs whole seconds. The fork now carries dcsctp's numbers instead - the SCTP implementation Google wrote to replace usrsctp for Chrome's WebRTC data channels, the same realtime workload: rto_initial 500, rto_min 400, a 220ms floor under the RTT variance, and mtu 1191. INITIAL_MTU 1228 plus DTLS/UDP/IPv6 overhead is 1313, past the 1280 minimum, so every full-size chunk fragmented on an IPv6 path. Both patch entries move to the new branch, which also carries the Windows IPv6 byte-swap fix, so one rev matches the whole webrtc 0.13 stack. * udp: make the punch prove itself, and keep the listener answering punch_udp sent a zero-length datagram and called the hole open on whatever arrived next. The rendezvous NAT test's own leftover replies satisfy that immediately - connect() does not flush the receive queue - so the retry loop never ran and success meant nothing. The dead socket then cost KCP its full timeout to rediscover, which is how a failed punch came to take 18 seconds. Probes now carry a magic and a 64-bit transaction id, and both ends answer each other's probes, so returning is a fact: a reply echoing our own id is the one thing that proves the pair carries traffic both ways. With failure now distinguishable from 'not yet', the window drops from 20s to 3s. Two asymmetries fall out of that: Only the connector stops on its own acknowledgement, because only it has something to send next. An acknowledgement proves our probe came back, not that the peer's probe was answered - and after punch_udp returns nothing answers probes any more, since KCP's io loop drops anything shorter than its header. A listener that stopped there would go mute while a peer whose own probe or answer was lost - the normal state of a hole still opening - kept probing an endpoint that works, until it timed out. So the listener stops on the peer's first real packet instead, and hands that packet to KcpStream::accept as its init_packet: its arrival proves the pair as well as an acknowledgement would, and KCP never retransmits its SYN. * webrtc: correct the RTT variance floor to dcsctp's scaling The earlier commit took dcsctp's min_rtt_variance = 220 as a raw floor under rttvar. dcsctp divides the option by kHeuristicVarianceAdjustment = 8.0 first, a historical accident it kept because downstream users had measured good values with it, so the intended floor is 27.5ms of variance contributing 110ms to RTO. Flooring at 220 contributed 880ms instead, which on a 50ms path left RTO within 7% of the 1000ms default this change exists to escape. The fork also now records why T1/T2 share T3's RTO manager here, unlike dcsctp's separate control timers: RTO_INITIAL is the T3 value for the first DATA chunk, since no RTT sample exists before the first SACK. * webrtc: skip the controller's ICE re-send instead of queueing it twice The controller sends every candidate twice, because the server's hop to a peer registered over UDP can lose one. The ICE agent that dedups repeats sits downstream of the answerer's queue, so the answerer paid for both copies: a slot, a JSON parse, and the ICE agent's lock, once per repeat. Remember a digest of what was queued and skip the repeat. Recorded only once queued, so a candidate a full queue refused stays repairable by the re-send. The queue's depth is unchanged. A real peer gathers well under it - four STUN servers, link-local IPv6 filtered, one component - and the drain empties it as candidates trickle in, so what this removes is the redundant work, not an overflow. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019aokqJuhjvB3kijXtAg5Ns * tcp: repeat the punch across the controller's dial window The single punch leaves before hbbs has told the controller where to dial, so it is never in flight at the same time as the controller's SYN: it opens our NAT, meets nothing, and a gateway that answers it with RST takes the mapping down with it, leaving the listener waiting on a hole that no longer exists. Punch again while the controller may still be dialing, and race those punches against the accept. That is two ways in where there was one: the mapping is rebuilt if a RST took it, and once the controller sits in SYN_SENT one of the punches meets its SYN and completes as a simultaneous open - which a punch sent before the controller had been told anything never could. The crossing reaches the punch rather than the listener because the two sockets share the address but only the punch matches the four-tuple, which the tests now pin down. There is no instant to aim at, and no window either. `Client::connect` sizes the controller's dial only after our PunchHoleSent, from its own rendezvous time and the direct failures it has recorded for us: CONNECT_TIMEOUT between two known-asymmetric NATs that never failed, punch_time_used times three or six otherwise, floored at a second - so a peer that failed once dials for a second or two from then on, and none of that reaches this side. The repeats therefore cover our own ceiling instead, CONNECT_TIMEOUT, which is exactly as long as the accept has always been willing to take a connection through the hole, and back off across it: dense at the start, where every window begins and the short ones end, sparse afterwards, which is `punch_udp`'s shape for the same reason. A window past that ceiling was lost before this change too, and mostly to the controller's own kernel - Windows gives a SYN up at 21s, Linux's next re-send after 15s is at 31s; a window short of it costs a few SYNs to a port already closed. No punch is cut on a per-attempt timeout; one in flight is bounded only by the shared deadline plus PUNCH_GRACE. A punch is cancel-safe only while it is still in SYN_SENT; once the controller's SYN has crossed it the socket is half way through a handshake, and cutting it there cuts the connection the controller is opening - whose `connect` has already returned, so that attempt fails outright, there being no relay fallback after a failed TCP handshake. A timer cannot tell the two states apart, and none is needed: a gateway that answers with RST fails the connect at once and the loop punches again, while one that drops the SYN in silence leaves the socket in SYN_SENT, holding the mapping open while the kernel re-sends, which any SYN of the controller's then crosses - a second punch has nothing to add. The deadline decides whether another punch starts; one in flight runs a grace past it, enough for a crossing begun just before it to complete. The last sleep is cut at the deadline rather than run out past it, so the window ends on a punch given that grace and not on a gap of up to the backoff ceiling: the controller's window opened after ours, on the PunchHoleSent hbbs relayed, so one as long as ours is still open through our tail. Only the accept races the punch, never `accept_connection`: that one does not return until the session it goes on to run has ended, so racing it would tear a live session down. Whichever arrives first is the one connection the request produces. `meta` carries the control permissions hbbs granted for this one controller, so serving the loser as well would hand them to a second peer - and nothing about a connection tells the two apart before `create_tcp_connection` has spoken to it, least of all its address: a carrier NAT shares one between subscribers, and a NAT that pools its external addresses may dial us from a different one than hbbs saw the controller through. So the address is not checked, as `accept_connection` never checked it; the handshake says who arrived, and what holds the invariant is that there is no second serve. Those permissions are a ceiling and not a grant either way: `Connection` gates every message on `authorized`, and latches the login scope of the first request it accepts, so a peer that reached the hole still arrives with nothing. The accept loops rather than taking a single connection, so that a transient accept error does not spend the window the controller still has to arrive in. libp2p's DCUtR reaches the same place by having both peers dial at one instant agreed over the relay. Nothing we send reaches the controller directly, so we cover its dial window rather than name an instant inside it. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019aokqJuhjvB3kijXtAg5Ns * hbb_common: bump to the webrtc branch rebased on main Picks up upstream's session-cache eviction by pc identity (#589, adopted without its unused insert-path helper), the 90-day log retention, and the wlroots output fixes. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019aokqJuhjvB3kijXtAg5Ns * webrtc: send over SCTP without a congestion window, as KCP does The same link that streams over KCP crawls over WebRTC. webrtc-sctp runs RFC 4960's AIMD: a fast retransmit halves cwnd, a T3 drops it to one MTU, and slow start only rebuilds it while data is queued behind it. Where the loss is random rather than congestion - a lossy long-haul link - the rate settles at the Mathis ceiling MSS/(RTT*sqrt(p)) however idle the link is: about 1.3 Mbps at 70ms RTT and 1% loss, 0.6 Mbps at 5%, while 1080p wants 2-5 Mbps. KCP's turbo profile (nc=1) has no congestion window at all. The fork now carries a switch that bypasses the two places gating sends on cwnd, and hbb_common turns it on for every peer connection unless `allow-webrtc-congestion-control` is set - the same opt-in KCP has in `allow-kcp-congestion-control`, for the reason at `get_kcp_cc_enabled`. Sender-side only; a browser or an older build on the other end interoperates. Measured over a simulated link (35ms one-way, random loss both ways, 12 KB frames at 30fps, 300 frames): at 1% loss the window stretches 9.9s of video to 20.7s with a mean latency of 5.5s; without it the stream stays realtime at a mean of 113ms. At 3%: 47s and 15s against 10.2s and 290ms. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019aokqJuhjvB3kijXtAg5Ns * webrtc: take the fork's loss recovery for sending without a congestion window rustdesk-org/webrtc 825a0a48: without a congestion window a chunk is lost once three chunks sent after its latest transmission are acked, counted in send order so retransmitted chunks are covered too, and the fast retransmit sends every lost chunk at once, as KCP nc=1 does; before, a lost retransmission waited for T3-rtx. Also fixes the delayed SACK timer never re-arming, the switch applying to established associations, T3-rtx resending one chunk when the peer's window is full, and bounds new data to 1 MiB / 1024 chunks in flight like KCP's snd_wnd. Simulated 35ms one-way, random loss both ways, 30 fps, frames later than 200ms out of 1200: 12 KB at 5% loss 996 -> 55 (KCP 61); 40 KB at 2% loss 1183 -> 20 (KCP 39). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019aokqJuhjvB3kijXtAg5Ns * bump hbb_common: decode TURN userinfo, add the webrtc_echo example Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JXJJGEGdgu26wgCppvUXdZ * web: show the WebRTC toggle and transport in the web UI The web client now speaks WebRTC, but the desktop settings page hides the punch options on web and the remote page opens without the session tab that carries the transport name. Let the existing "Enable WebRTC P2P connection" checkbox through on web (the other punch options stay native-only), and add a Transport row to the quality monitor for WebRTC sessions only (with "(TURN)" when ICE relayed), on every platform. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JXJJGEGdgu26wgCppvUXdZ * bump hbb_common: end the ICE forwarder at gathering complete, drop the closes Drop covers hbb_common now closes the local-candidate channel when gathering completes, so the controlled side's forwarder in spawn_webrtc_answerer ends there, and its signaling connection to hbbs with it, instead of sitting on a socket hbbs closed at 90s idle for the rest of the session. It also keeps the reassembly buffer across fragmented frames. Stream closes the WebRTC peer connection on drop (hbb_common b0b624d), so the close_webrtc() calls in port_forward and io_loop that sat immediately before a return or the end of scope did nothing Drop was not about to do, while the comments beside them still said a bare drop leaked the pc. Remove both. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019aokqJuhjvB3kijXtAg5Ns * bump hbb_common: quiet the webrtc-rs warnings that describe the race's normal outcome Cancelling the transport that lost the race, and trickle checking before it holds a pair, are what the design does on every session that connects - and webrtc-rs reports both at warn, 90 lines of a 386-line controlled-side log, beside connections that succeeded. agent_internal and peer_connection drop to error; agent_gather keeps warn, since an unreachable STUN server is the one upstream signal that explains a session which never connected. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01M54JAqUK4RynudFou89hod * port_forward: restore the `?` the close removal left as a match Dropping the explicit close_webrtc() from the parse-error arm left a match that only re-spells `?`; master just reworked this function, so the branch now leaves port_forward.rs untouched. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019aokqJuhjvB3kijXtAg5Ns * l10n: the two WebRTC keys were missing from Urdu Every other lang file on the branch carries them; ur.rs was skipped when they were added. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019aokqJuhjvB3kijXtAg5Ns * udp: make the punch deadline absolute, so a talking peer cannot defer it `select!` rebuilds every arm each iteration, so the relative retry sleep was restarted by each datagram that arrived before it fired. The peer sets that rate, and an old-build peer's empty datagrams match no arm and loop without even the recv-error pause, so MAX_TIME went unchecked and the retransmit was starved with it. `udp_nat_connect` awaits the punch ahead of the KCP timeout and nothing above it bounds the phase, so the punch held the direct race open and the relay fallback out of reach for as long as the peer kept sending. Absolute instants for both clocks. The new test floods empty datagrams for four times the deadline: the punch now ends at 3s where it ran the full 12s. Also note at the symmetric-NAT branch that WebRTC not following the legacy relay decision there is deliberate, so it is not later "fixed" into agreement. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019aokqJuhjvB3kijXtAg5Ns * bump webrtc fork: MTU-safe bundles, a reordering window, tail loss within the RTT rustdesk-org/webrtc cc6633bc, three commits on 825a0a48, all on the path that sends without a congestion window: Both bundlers counted a DATA chunk by its payload alone; with the header and padding counted, bundles of small chunks stay within the MTU, and the fragment payload rounds down to 1160 so a full chunk does too. A chunk is fast retransmitted at most five times, KCP's IKCP_FASTACK_LIMIT. A frame's chunks go out within microseconds of each other, so on a path that jitters the send-order rule resent every chunk that landed behind three of its siblings: 2.7x the payload on the wire at 10ms of jitter, and on a link without the room for that, a queue that fed on itself. A reordering window, RACK's, makes evidence count only from what was sent a quarter of an srtt after the chunk once the path is seen to reorder, widening on the duplicate TSNs the receiver reports. 5 Mbps, 1% loss, 20ms jitter: 600 of 600 frames at a 98ms mean where 290 arrived at 6.2s. A chunk lost at the tail of a burst has only T3-rtx, which ran from floors sized for a 200ms delayed ack and restarted only on the tail's predecessor's ack: 600ms and more. Every DATA chunk now carries the I bit, the floors are KCP's shape, and a fast retransmission restarts the timer. One 200-byte message per frame at 5% loss: 9 of 600 later than 200ms, from 42. Random loss without jitter is unchanged at every rate and frame size. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019aokqJuhjvB3kijXtAg5Ns * bump webrtc fork: T3-rtx restarts only for the earliest chunk's fast retransmission rustdesk-org/webrtc 2b8e55bc. Sending without a congestion window, a fast retransmission of any chunk restarted T3-rtx, so a chunk past the fast retransmission cap - left to that timer - never reached it while later chunks kept being resent, which a lossy stream does every couple of frames. The timer is the earliest in-flight chunk's, and only its resend restarts it now. Nothing else changes; the benchmark is unchanged. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019aokqJuhjvB3kijXtAg5Ns * bump webrtc fork: T3-rtx restart on fast retransmission while shutting down too rustdesk-org/webrtc 48100bf1. The restart for the earliest chunk's fast retransmission reached only the Established branch of the write loop; the shutdown states still carry data in flight and recover it the same way, so a closing association could still resend everything on a loss its fast retransmit had already recovered. Both branches share one helper now. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019aokqJuhjvB3kijXtAg5Ns --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
4344 lines
142 KiB
Dart
4344 lines
142 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'dart:math';
|
|
import 'dart:typed_data';
|
|
import 'dart:ui' as ui;
|
|
|
|
import 'package:bot_toast/bot_toast.dart';
|
|
import 'package:desktop_multi_window/desktop_multi_window.dart';
|
|
import 'package:flutter/gestures.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter/scheduler.dart';
|
|
import 'package:flutter_hbb/common/widgets/peers_view.dart';
|
|
import 'package:flutter_hbb/consts.dart';
|
|
import 'package:flutter_hbb/models/ab_model.dart';
|
|
import 'package:flutter_hbb/models/chat_model.dart';
|
|
import 'package:flutter_hbb/models/cm_file_model.dart';
|
|
import 'package:flutter_hbb/models/file_model.dart';
|
|
import 'package:flutter_hbb/models/group_model.dart';
|
|
import 'package:flutter_hbb/models/peer_model.dart';
|
|
import 'package:flutter_hbb/models/peer_tab_model.dart';
|
|
import 'package:flutter_hbb/models/printer_model.dart';
|
|
import 'package:flutter_hbb/models/server_model.dart';
|
|
import 'package:flutter_hbb/models/user_model.dart';
|
|
import 'package:flutter_hbb/models/state_model.dart';
|
|
import 'package:flutter_hbb/models/desktop_render_texture.dart';
|
|
import 'package:flutter_hbb/models/terminal_model.dart';
|
|
import 'package:flutter_hbb/common/shared_state.dart';
|
|
import 'package:flutter_hbb/utils/multi_window_manager.dart';
|
|
import 'package:flutter_hbb/utils/http_service.dart' as http;
|
|
import 'package:tuple/tuple.dart';
|
|
import 'package:image/image.dart' as img2;
|
|
import 'package:flutter_svg/flutter_svg.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:uuid/uuid.dart';
|
|
import 'package:window_manager/window_manager.dart';
|
|
import 'package:file_picker/file_picker.dart';
|
|
import 'package:vector_math/vector_math.dart' show Vector2;
|
|
|
|
import '../common.dart';
|
|
import '../utils/image.dart' as img;
|
|
import '../common/widgets/dialog.dart';
|
|
import 'input_model.dart';
|
|
import 'platform_model.dart';
|
|
import 'package:flutter_hbb/utils/scale.dart';
|
|
|
|
import 'package:flutter_hbb/generated_bridge.dart'
|
|
if (dart.library.html) 'package:flutter_hbb/web/bridge.dart';
|
|
import 'package:flutter_hbb/native/custom_cursor.dart'
|
|
if (dart.library.html) 'package:flutter_hbb/web/custom_cursor.dart';
|
|
|
|
typedef HandleMsgBox = Function(Map<String, dynamic> evt, String id);
|
|
typedef ReconnectHandle = Function(OverlayDialogManager, SessionID, bool);
|
|
final _constSessionId = Uuid().v4obj();
|
|
// Empirical restart reconnect cadence: keep the last frame briefly and retry quickly.
|
|
const _restartReconnectSilentDelaySecs = 5;
|
|
|
|
class CachedPeerData {
|
|
Map<String, dynamic> updatePrivacyMode = {};
|
|
Map<String, dynamic> peerInfo = {};
|
|
List<Map<String, dynamic>> cursorDataList = [];
|
|
Map<String, dynamic> lastCursorId = {};
|
|
Map<String, bool> permissions = {};
|
|
|
|
bool secure = false;
|
|
bool direct = false;
|
|
String streamType = '';
|
|
|
|
CachedPeerData();
|
|
|
|
@override
|
|
String toString() {
|
|
return jsonEncode({
|
|
'updatePrivacyMode': updatePrivacyMode,
|
|
'peerInfo': peerInfo,
|
|
'cursorDataList': cursorDataList,
|
|
'lastCursorId': lastCursorId,
|
|
'permissions': permissions,
|
|
'secure': secure,
|
|
'direct': direct,
|
|
'streamType': streamType,
|
|
});
|
|
}
|
|
|
|
static CachedPeerData? fromString(String s) {
|
|
try {
|
|
final map = jsonDecode(s);
|
|
final data = CachedPeerData();
|
|
data.updatePrivacyMode = map['updatePrivacyMode'];
|
|
data.peerInfo = map['peerInfo'];
|
|
for (final cursorData in map['cursorDataList']) {
|
|
data.cursorDataList.add(cursorData);
|
|
}
|
|
data.lastCursorId = map['lastCursorId'];
|
|
map['permissions'].forEach((key, value) {
|
|
data.permissions[key] = value;
|
|
});
|
|
data.secure = map['secure'];
|
|
data.direct = map['direct'];
|
|
data.streamType = map['streamType'];
|
|
return data;
|
|
} catch (e) {
|
|
debugPrint('Failed to parse CachedPeerData: $e');
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
class FfiModel with ChangeNotifier {
|
|
CachedPeerData cachedPeerData = CachedPeerData();
|
|
PeerInfo _pi = PeerInfo();
|
|
int? lastUserDisplay;
|
|
int? pendingMonitorRestore;
|
|
Timer? _pendingRestoreTimer;
|
|
Rect? _rect;
|
|
|
|
var _inputBlocked = false;
|
|
final _permissions = <String, bool>{};
|
|
bool? _secure;
|
|
bool? _direct;
|
|
bool _touchMode = false;
|
|
late VirtualMouseMode virtualMouseMode;
|
|
Timer? _timer;
|
|
Timer? _restartReconnectDelayTimer;
|
|
var _reconnects = 1;
|
|
DateTime? _offlineReconnectStartTime;
|
|
bool _androidDocumentPickerActive = false;
|
|
bool _androidDocumentPickerInterruptedConnection = false;
|
|
bool _viewOnly = false;
|
|
bool _showMyCursor = false;
|
|
WeakReference<FFI> parent;
|
|
late final SessionID sessionId;
|
|
|
|
RxBool waitForImageDialogShow = true.obs;
|
|
Timer? waitForImageTimer;
|
|
RxBool waitForFirstImage = true.obs;
|
|
bool isRefreshing = false;
|
|
|
|
Timer? timerScreenshot;
|
|
|
|
Rect? get rect => _rect;
|
|
bool get isOriginalResolutionSet =>
|
|
_pi.tryGetDisplayIfNotAllDisplay()?.isOriginalResolutionSet ?? false;
|
|
bool get isVirtualDisplayResolution =>
|
|
_pi.tryGetDisplayIfNotAllDisplay()?.isVirtualDisplayResolution ?? false;
|
|
bool get isOriginalResolution =>
|
|
_pi.tryGetDisplayIfNotAllDisplay()?.isOriginalResolution ?? false;
|
|
|
|
Map<String, bool> get permissions => _permissions;
|
|
setPermissions(Map<String, bool> permissions) {
|
|
_permissions.clear();
|
|
_permissions.addAll(permissions);
|
|
}
|
|
|
|
bool? get secure => _secure;
|
|
|
|
bool? get direct => _direct;
|
|
|
|
PeerInfo get pi => _pi;
|
|
|
|
bool get inputBlocked => _inputBlocked;
|
|
|
|
bool get touchMode => _touchMode;
|
|
|
|
bool get isPeerAndroid => _pi.platform == kPeerPlatformAndroid;
|
|
bool get isPeerMobile => isPeerAndroid;
|
|
|
|
bool get isPeerLinux => _pi.platform == kPeerPlatformLinux;
|
|
|
|
bool get viewOnly => _viewOnly;
|
|
bool get showMyCursor => _showMyCursor;
|
|
|
|
set inputBlocked(v) {
|
|
_inputBlocked = v;
|
|
}
|
|
|
|
FfiModel(this.parent) {
|
|
clear();
|
|
sessionId = parent.target!.sessionId;
|
|
cachedPeerData.permissions = _permissions;
|
|
virtualMouseMode = VirtualMouseMode(this);
|
|
}
|
|
|
|
Rect? globalDisplaysRect() => _getDisplaysRect(_pi.displays, true);
|
|
Rect? displaysRect() => _getDisplaysRect(_pi.getCurDisplays(), false);
|
|
Rect? _getDisplaysRect(List<Display> displays, bool useDisplayScale) {
|
|
if (displays.isEmpty) {
|
|
return null;
|
|
}
|
|
if (isPeerLinux) {
|
|
useDisplayScale = true;
|
|
}
|
|
int scale(int len, double s) {
|
|
if (useDisplayScale) {
|
|
return len.toDouble() ~/ s;
|
|
} else {
|
|
return len;
|
|
}
|
|
}
|
|
|
|
double l = displays[0].x;
|
|
double t = displays[0].y;
|
|
double r = displays[0].x + scale(displays[0].width, displays[0].scale);
|
|
double b = displays[0].y + scale(displays[0].height, displays[0].scale);
|
|
for (var display in displays.sublist(1)) {
|
|
l = min(l, display.x);
|
|
t = min(t, display.y);
|
|
r = max(r, display.x + scale(display.width, display.scale));
|
|
b = max(b, display.y + scale(display.height, display.scale));
|
|
}
|
|
return Rect.fromLTRB(l, t, r, b);
|
|
}
|
|
|
|
toggleTouchMode() {
|
|
if (!isPeerAndroid) {
|
|
_touchMode = !_touchMode;
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
updatePermission(Map<String, dynamic> evt, String id) {
|
|
// Track previous keyboard permission to detect revocation.
|
|
final hadKeyboardPerm = _permissions['keyboard'] != false;
|
|
|
|
evt.forEach((k, v) {
|
|
if (k == 'name' || k.isEmpty) return;
|
|
_permissions[k] = v == 'true';
|
|
});
|
|
// Only inited at remote page
|
|
if (parent.target?.connType == ConnType.defaultConn) {
|
|
KeyboardEnabledState.find(id).value = _permissions['keyboard'] != false;
|
|
}
|
|
|
|
// If keyboard permission was revoked while relative mouse mode is active,
|
|
// forcefully disable relative mouse mode to prevent the user from being trapped.
|
|
final hasKeyboardPerm = _permissions['keyboard'] != false;
|
|
if (hadKeyboardPerm && !hasKeyboardPerm) {
|
|
final inputModel = parent.target?.inputModel;
|
|
if (inputModel != null && inputModel.relativeMouseMode.value) {
|
|
inputModel.setRelativeMouseMode(false);
|
|
showToast(translate('rel-mouse-permission-lost-tip'));
|
|
}
|
|
}
|
|
|
|
debugPrint('updatePermission: $_permissions');
|
|
notifyListeners();
|
|
}
|
|
|
|
bool get keyboard => _permissions['keyboard'] != false;
|
|
|
|
clear() {
|
|
_pi = PeerInfo();
|
|
lastUserDisplay = null;
|
|
_cancelPendingMonitorRestore();
|
|
_secure = null;
|
|
_direct = null;
|
|
_inputBlocked = false;
|
|
_timer?.cancel();
|
|
_timer = null;
|
|
_androidDocumentPickerActive = false;
|
|
_androidDocumentPickerInterruptedConnection = false;
|
|
resetRestartReconnectState();
|
|
clearPermissions();
|
|
waitForImageTimer?.cancel();
|
|
timerScreenshot?.cancel();
|
|
}
|
|
|
|
setConnectionType(
|
|
String peerId, bool secure, bool direct, String streamType) {
|
|
cachedPeerData.secure = secure;
|
|
cachedPeerData.direct = direct;
|
|
cachedPeerData.streamType = streamType;
|
|
_secure = secure;
|
|
_direct = direct;
|
|
try {
|
|
var connectionType = ConnectionTypeState.find(peerId);
|
|
connectionType.setSecure(secure);
|
|
connectionType.setDirect(direct);
|
|
connectionType.setStreamType(streamType);
|
|
} catch (e) {
|
|
//
|
|
}
|
|
}
|
|
|
|
Widget? getConnectionImageText() {
|
|
if (secure == null || direct == null) {
|
|
return null;
|
|
} else {
|
|
final icon =
|
|
'${secure == true ? 'secure' : 'insecure'}${direct == true ? '' : '_relay'}';
|
|
final iconWidget =
|
|
SvgPicture.asset('assets/$icon.svg', width: 48, height: 48);
|
|
String connectionText =
|
|
getConnectionText(secure!, direct!, cachedPeerData.streamType);
|
|
return Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
iconWidget,
|
|
SizedBox(height: 4),
|
|
Text(
|
|
connectionText,
|
|
style: TextStyle(fontSize: 12),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
clearPermissions() {
|
|
_inputBlocked = false;
|
|
_permissions.clear();
|
|
}
|
|
|
|
handleCachedPeerData(CachedPeerData data, String peerId) async {
|
|
handleMsgBox({
|
|
'type': 'success',
|
|
'title': 'Successful',
|
|
'text': kMsgboxTextWaitingForImage,
|
|
'link': '',
|
|
}, sessionId, peerId);
|
|
updatePrivacyMode(data.updatePrivacyMode, sessionId, peerId);
|
|
setConnectionType(peerId, data.secure, data.direct, data.streamType);
|
|
await handlePeerInfo(data.peerInfo, peerId, true);
|
|
for (final element in data.cursorDataList) {
|
|
updateLastCursorId(element);
|
|
await handleCursorData(element);
|
|
}
|
|
if (data.lastCursorId.isNotEmpty) {
|
|
updateLastCursorId(data.lastCursorId);
|
|
handleCursorId(data.lastCursorId);
|
|
}
|
|
}
|
|
|
|
// todo: why called by two position
|
|
StreamEventHandler startEventListener(SessionID sessionId, String peerId) {
|
|
return (evt) async {
|
|
var name = evt['name'];
|
|
if (name == 'msgbox') {
|
|
handleMsgBox(evt, sessionId, peerId);
|
|
} else if (name == 'toast') {
|
|
handleToast(evt, sessionId, peerId);
|
|
} else if (name == 'set_multiple_windows_session') {
|
|
handleMultipleWindowsSession(evt, sessionId, peerId);
|
|
} else if (name == 'peer_info') {
|
|
handlePeerInfo(evt, peerId, false);
|
|
} else if (name == 'sync_peer_info') {
|
|
handleSyncPeerInfo(evt, sessionId, peerId);
|
|
} else if (name == 'sync_platform_additions') {
|
|
handlePlatformAdditions(evt, sessionId, peerId);
|
|
} else if (name == 'connection_ready') {
|
|
setConnectionType(peerId, evt['secure'] == 'true',
|
|
evt['direct'] == 'true', evt['stream_type'] ?? '');
|
|
resetRestartReconnectState();
|
|
} else if (name == 'switch_display') {
|
|
// switch display is kept for backward compatibility
|
|
handleSwitchDisplay(evt, sessionId, peerId);
|
|
} else if (name == 'cursor_data') {
|
|
updateLastCursorId(evt);
|
|
await handleCursorData(evt);
|
|
} else if (name == 'cursor_id') {
|
|
updateLastCursorId(evt);
|
|
handleCursorId(evt);
|
|
} else if (name == 'cursor_position') {
|
|
await parent.target?.cursorModel.updateCursorPosition(evt, peerId);
|
|
} else if (name == 'clipboard') {
|
|
Clipboard.setData(ClipboardData(text: evt['content']));
|
|
} else if (name == 'permission') {
|
|
updatePermission(evt, peerId);
|
|
} else if (name == 'chat_client_mode') {
|
|
parent.target?.chatModel
|
|
.receive(ChatModel.clientModeID, evt['text'] ?? '');
|
|
} else if (name == 'chat_server_mode') {
|
|
parent.target?.chatModel
|
|
.receive(int.parse(evt['id'] as String), evt['text'] ?? '');
|
|
} else if (name == 'terminal_response') {
|
|
parent.target?.routeTerminalResponse(evt);
|
|
} else if (name == 'file_dir') {
|
|
parent.target?.fileModel.receiveFileDir(evt);
|
|
} else if (name == 'empty_dirs') {
|
|
parent.target?.fileModel.receiveEmptyDirs(evt);
|
|
} else if (name == 'job_progress') {
|
|
parent.target?.fileModel.jobController.tryUpdateJobProgress(evt);
|
|
} else if (name == 'job_done') {
|
|
bool? refresh =
|
|
await parent.target?.fileModel.jobController.jobDone(evt);
|
|
if (refresh == true) {
|
|
// many job done for delete directory
|
|
// todo: refresh may not work when confirm delete local directory
|
|
parent.target?.fileModel.refreshAll();
|
|
}
|
|
} else if (name == 'job_error') {
|
|
parent.target?.fileModel.handleJobError(evt);
|
|
} else if (name == 'override_file_confirm') {
|
|
parent.target?.fileModel.postOverrideFileConfirm(evt);
|
|
} else if (name == 'load_last_job') {
|
|
parent.target?.fileModel.jobController.loadLastJob(evt);
|
|
} else if (name == 'update_folder_files') {
|
|
parent.target?.fileModel.jobController.updateFolderFiles(evt);
|
|
} else if (name == 'add_connection') {
|
|
parent.target?.serverModel.addConnection(evt);
|
|
} else if (name == 'on_client_remove') {
|
|
parent.target?.serverModel.onClientRemove(evt);
|
|
} else if (name == 'update_quality_status') {
|
|
parent.target?.qualityMonitorModel.updateQualityStatus(evt);
|
|
} else if (name == 'update_block_input_state') {
|
|
updateBlockInputState(evt, peerId);
|
|
} else if (name == 'update_privacy_mode') {
|
|
updatePrivacyMode(evt, sessionId, peerId);
|
|
} else if (name == 'show_elevation') {
|
|
final show = evt['show'].toString() == 'true';
|
|
parent.target?.serverModel.setShowElevation(show);
|
|
} else if (name == 'cancel_msgbox') {
|
|
cancelMsgBox(evt, sessionId);
|
|
} else if (name == 'switch_back') {
|
|
final peer_id = evt['peer_id'].toString();
|
|
await bind.sessionSwitchSides(sessionId: sessionId);
|
|
closeConnection(id: peer_id);
|
|
} else if (name == 'portable_service_running') {
|
|
_handlePortableServiceRunning(peerId, evt);
|
|
} else if (name == 'on_url_scheme_received') {
|
|
// currently comes from "_url" ipc of mac and dbus of linux
|
|
onUrlSchemeReceived(evt);
|
|
} else if (name == 'on_voice_call_waiting') {
|
|
// Waiting for the response from the peer.
|
|
parent.target?.chatModel.onVoiceCallWaiting();
|
|
} else if (name == 'on_voice_call_started') {
|
|
// Voice call is connected.
|
|
parent.target?.chatModel.onVoiceCallStarted();
|
|
} else if (name == 'on_voice_call_closed') {
|
|
// Voice call is closed with reason.
|
|
final reason = evt['reason'].toString();
|
|
parent.target?.chatModel.onVoiceCallClosed(reason);
|
|
} else if (name == 'on_voice_call_incoming') {
|
|
// Voice call is requested by the peer.
|
|
parent.target?.chatModel.onVoiceCallIncoming();
|
|
} else if (name == 'update_voice_call_state') {
|
|
parent.target?.serverModel.updateVoiceCallState(evt);
|
|
} else if (name == 'fingerprint') {
|
|
FingerprintState.find(peerId).value = evt['fingerprint'] ?? '';
|
|
} else if (name == "sync_peer_hash_password_to_personal_ab") {
|
|
if (desktopType == DesktopType.main || isWeb || isMobile) {
|
|
final id = evt['id'];
|
|
final hash = evt['hash'];
|
|
if (id != null && hash != null) {
|
|
gFFI.abModel
|
|
.changePersonalHashPassword(id.toString(), hash.toString());
|
|
}
|
|
}
|
|
} else if (name == "cm_file_transfer_log") {
|
|
if (isDesktop) {
|
|
gFFI.cmFileModel.onFileTransferLog(evt);
|
|
}
|
|
} else if (name == 'sync_peer_option') {
|
|
_handleSyncPeerOption(evt, peerId);
|
|
} else if (name == 'follow_current_display') {
|
|
handleFollowCurrentDisplay(evt, sessionId, peerId);
|
|
} else if (name == 'use_texture_render') {
|
|
_handleUseTextureRender(evt, sessionId, peerId);
|
|
} else if (name == "selected_files") {
|
|
if (isWeb) {
|
|
parent.target?.fileModel.onSelectedFiles(evt);
|
|
}
|
|
} else if (name == "send_emptry_dirs") {
|
|
if (isWeb) {
|
|
parent.target?.fileModel.sendEmptyDirs(evt);
|
|
}
|
|
} else if (name == "record_status") {
|
|
if (desktopType == DesktopType.remote ||
|
|
desktopType == DesktopType.viewCamera ||
|
|
isMobile) {
|
|
parent.target?.recordingModel.updateStatus(evt['start'] == 'true');
|
|
}
|
|
} else if (name == "printer_request") {
|
|
_handlePrinterRequest(evt, sessionId, peerId);
|
|
} else if (name == 'screenshot') {
|
|
_handleScreenshot(evt, sessionId, peerId);
|
|
} else if (name == 'exit_relative_mouse_mode') {
|
|
// Handle exit shortcut from rdev grab loop (Ctrl+Alt on Win/Linux, Cmd+G on macOS)
|
|
parent.target?.inputModel.exitRelativeMouseModeWithKeyRelease();
|
|
} else {
|
|
debugPrint('Event is not handled in the fixed branch: $name');
|
|
}
|
|
};
|
|
}
|
|
|
|
_handleScreenshot(
|
|
Map<String, dynamic> evt, SessionID sessionId, String peerId) {
|
|
timerScreenshot?.cancel();
|
|
timerScreenshot = null;
|
|
final msg = evt['msg'] ?? '';
|
|
final msgBoxType = 'custom-nook-nocancel-hasclose';
|
|
final msgBoxTitle = 'Take screenshot';
|
|
final dialogManager = parent.target!.dialogManager;
|
|
if (msg.isNotEmpty) {
|
|
msgBox(sessionId, msgBoxType, msgBoxTitle, msg, '', dialogManager);
|
|
} else {
|
|
final msgBoxText = 'screenshot-action-tip';
|
|
|
|
close() {
|
|
dialogManager.dismissAll();
|
|
}
|
|
|
|
saveAs() {
|
|
close();
|
|
Future.delayed(Duration.zero, () async {
|
|
final ts = DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
|
String? outputFile = await FilePicker.platform.saveFile(
|
|
dialogTitle: '${translate('Save as')}...',
|
|
fileName: 'screenshot_$ts.png',
|
|
allowedExtensions: ['png'],
|
|
type: FileType.custom,
|
|
);
|
|
if (outputFile == null) {
|
|
bind.sessionHandleScreenshot(sessionId: sessionId, action: '2');
|
|
} else {
|
|
final res = await bind.sessionHandleScreenshot(
|
|
sessionId: sessionId, action: '0:$outputFile');
|
|
if (res.isNotEmpty) {
|
|
msgBox(sessionId, 'custom-nook-nocancel-hasclose-error',
|
|
'Take screenshot', res, '', dialogManager);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
copyToClipboard() {
|
|
bind.sessionHandleScreenshot(sessionId: sessionId, action: '1');
|
|
close();
|
|
}
|
|
|
|
cancel() {
|
|
bind.sessionHandleScreenshot(sessionId: sessionId, action: '2');
|
|
close();
|
|
}
|
|
|
|
final List<Widget> buttons = [
|
|
dialogButton('${translate('Save as')}...', onPressed: saveAs),
|
|
dialogButton('Copy to clipboard', onPressed: copyToClipboard),
|
|
dialogButton('Cancel', onPressed: cancel),
|
|
];
|
|
dialogManager.dismissAll();
|
|
dialogManager.show(
|
|
(setState, close, context) => CustomAlertDialog(
|
|
title: null,
|
|
content: SelectionArea(
|
|
child: msgboxContent(msgBoxType, msgBoxTitle, msgBoxText)),
|
|
actions: buttons,
|
|
),
|
|
tag: '$msgBoxType-$msgBoxTitle-$msgBoxTitle',
|
|
);
|
|
}
|
|
}
|
|
|
|
_handlePrinterRequest(
|
|
Map<String, dynamic> evt, SessionID sessionId, String peerId) {
|
|
final id = evt['id'];
|
|
final path = evt['path'];
|
|
final dialogManager = parent.target!.dialogManager;
|
|
dialogManager.show((setState, close, context) {
|
|
PrinterOptions printerOptions = PrinterOptions.load();
|
|
final saveSettings = mainGetLocalBoolOptionSync(kKeyPrinterSave).obs;
|
|
final dontShowAgain = false.obs;
|
|
final Rx<String> selectedPrinterName = printerOptions.printerName.obs;
|
|
final printerNames = printerOptions.printerNames;
|
|
final defaultOrSelectedGroupValue =
|
|
(printerOptions.action == kValuePrinterIncomingJobDismiss
|
|
? kValuePrinterIncomingJobDefault
|
|
: printerOptions.action)
|
|
.obs;
|
|
|
|
onRatioChanged(String? value) {
|
|
defaultOrSelectedGroupValue.value =
|
|
value ?? kValuePrinterIncomingJobDefault;
|
|
}
|
|
|
|
onSubmit() {
|
|
final printerName = defaultOrSelectedGroupValue.isEmpty
|
|
? ''
|
|
: selectedPrinterName.value;
|
|
bind.sessionPrinterResponse(
|
|
sessionId: sessionId, id: id, path: path, printerName: printerName);
|
|
if (saveSettings.value || dontShowAgain.value) {
|
|
bind.mainSetLocalOption(key: kKeyPrinterSelected, value: printerName);
|
|
bind.mainSetLocalOption(
|
|
key: kKeyPrinterIncomingJobAction,
|
|
value: defaultOrSelectedGroupValue.value);
|
|
}
|
|
if (dontShowAgain.value) {
|
|
mainSetLocalBoolOption(kKeyPrinterAllowAutoPrint, true);
|
|
}
|
|
close();
|
|
}
|
|
|
|
onCancel() {
|
|
if (dontShowAgain.value) {
|
|
bind.mainSetLocalOption(
|
|
key: kKeyPrinterIncomingJobAction,
|
|
value: kValuePrinterIncomingJobDismiss);
|
|
}
|
|
close();
|
|
}
|
|
|
|
final printerItemHeight = 30.0;
|
|
final selectionAreaHeight =
|
|
printerItemHeight * min(8.0, max(printerNames.length, 3.0));
|
|
final content = Column(
|
|
children: [
|
|
Text(translate('print-incoming-job-confirm-tip')),
|
|
Row(
|
|
children: [
|
|
Obx(() => Radio<String>(
|
|
value: kValuePrinterIncomingJobDefault,
|
|
groupValue: defaultOrSelectedGroupValue.value,
|
|
onChanged: onRatioChanged)),
|
|
GestureDetector(
|
|
child: Text(translate('use-the-default-printer-tip')),
|
|
onTap: () => onRatioChanged(kValuePrinterIncomingJobDefault)),
|
|
],
|
|
),
|
|
Column(
|
|
children: [
|
|
Row(children: [
|
|
Obx(() => Radio<String>(
|
|
value: kValuePrinterIncomingJobSelected,
|
|
groupValue: defaultOrSelectedGroupValue.value,
|
|
onChanged: onRatioChanged)),
|
|
GestureDetector(
|
|
child: Text(translate('use-the-selected-printer-tip')),
|
|
onTap: () =>
|
|
onRatioChanged(kValuePrinterIncomingJobSelected)),
|
|
]),
|
|
SizedBox(
|
|
height: selectionAreaHeight,
|
|
width: 500,
|
|
child: ListView.builder(
|
|
itemBuilder: (context, index) {
|
|
return Obx(() => GestureDetector(
|
|
child: Container(
|
|
decoration: BoxDecoration(
|
|
color: selectedPrinterName.value ==
|
|
printerNames[index]
|
|
? (defaultOrSelectedGroupValue.value ==
|
|
kValuePrinterIncomingJobSelected
|
|
? MyTheme.button
|
|
: MyTheme.button.withOpacity(0.5))
|
|
: Theme.of(context).cardColor,
|
|
borderRadius: BorderRadius.all(
|
|
Radius.circular(5.0),
|
|
),
|
|
),
|
|
key: ValueKey(printerNames[index]),
|
|
height: printerItemHeight,
|
|
child: Align(
|
|
alignment: Alignment.centerLeft,
|
|
child: Padding(
|
|
padding: const EdgeInsets.only(left: 10.0),
|
|
child: Text(
|
|
printerNames[index],
|
|
style: TextStyle(fontSize: 14),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
onTap: defaultOrSelectedGroupValue.value ==
|
|
kValuePrinterIncomingJobSelected
|
|
? () {
|
|
selectedPrinterName.value =
|
|
printerNames[index];
|
|
}
|
|
: null,
|
|
));
|
|
},
|
|
itemCount: printerNames.length),
|
|
),
|
|
],
|
|
),
|
|
Row(
|
|
children: [
|
|
Obx(() => Checkbox(
|
|
value: saveSettings.value,
|
|
onChanged: (value) {
|
|
if (value != null) {
|
|
saveSettings.value = value;
|
|
mainSetLocalBoolOption(kKeyPrinterSave, value);
|
|
}
|
|
})),
|
|
GestureDetector(
|
|
child: Text(translate('save-settings-tip')),
|
|
onTap: () {
|
|
saveSettings.value = !saveSettings.value;
|
|
mainSetLocalBoolOption(kKeyPrinterSave, saveSettings.value);
|
|
}),
|
|
],
|
|
),
|
|
Row(
|
|
children: [
|
|
Obx(() => Checkbox(
|
|
value: dontShowAgain.value,
|
|
onChanged: (value) {
|
|
if (value != null) {
|
|
dontShowAgain.value = value;
|
|
}
|
|
})),
|
|
GestureDetector(
|
|
child: Text(translate('dont-show-again-tip')),
|
|
onTap: () {
|
|
dontShowAgain.value = !dontShowAgain.value;
|
|
}),
|
|
],
|
|
),
|
|
],
|
|
);
|
|
return CustomAlertDialog(
|
|
title: Text(translate('Incoming Print Job')),
|
|
content: content,
|
|
actions: [
|
|
dialogButton('OK', onPressed: onSubmit),
|
|
dialogButton('Cancel', onPressed: onCancel),
|
|
],
|
|
onSubmit: onSubmit,
|
|
onCancel: onCancel,
|
|
);
|
|
});
|
|
}
|
|
|
|
_handleUseTextureRender(
|
|
Map<String, dynamic> evt, SessionID sessionId, String peerId) {
|
|
parent.target?.imageModel.setUseTextureRender(evt['v'] == 'Y');
|
|
waitForFirstImage.value = true;
|
|
isRefreshing = true;
|
|
showConnectedWaitingForImage(parent.target!.dialogManager, sessionId,
|
|
'success', 'Successful', kMsgboxTextWaitingForImage);
|
|
}
|
|
|
|
_handleSyncPeerOption(Map<String, dynamic> evt, String peer) {
|
|
final k = evt['k'];
|
|
final v = evt['v'];
|
|
if (k == kOptionToggleViewOnly) {
|
|
setViewOnly(peer, v as bool);
|
|
} else if (k == 'keyboard_mode') {
|
|
parent.target?.inputModel.updateKeyboardMode();
|
|
} else if (k == 'input_source') {
|
|
stateGlobal.getInputSource(force: true);
|
|
}
|
|
}
|
|
|
|
onUrlSchemeReceived(Map<String, dynamic> evt) {
|
|
final url = evt['url'].toString().trim();
|
|
if (url.startsWith(bind.mainUriPrefixSync()) &&
|
|
handleUriLink(uriString: url)) {
|
|
return;
|
|
}
|
|
switch (url) {
|
|
case kUrlActionClose:
|
|
debugPrint("closing all instances");
|
|
Future.microtask(() async {
|
|
await rustDeskWinManager.closeAllSubWindows();
|
|
windowManager.close();
|
|
});
|
|
break;
|
|
default:
|
|
windowOnTop(null);
|
|
break;
|
|
}
|
|
}
|
|
|
|
/// Bind the event listener to receive events from the Rust core.
|
|
updateEventListener(SessionID sessionId, String peerId) {
|
|
platformFFI.setEventCallback(startEventListener(sessionId, peerId));
|
|
}
|
|
|
|
_handlePortableServiceRunning(String peerId, Map<String, dynamic> evt) {
|
|
final running = evt['running'] == 'true';
|
|
parent.target?.elevationModel.onPortableServiceRunning(running);
|
|
}
|
|
|
|
handleAliasChanged(Map<String, dynamic> evt) {
|
|
if (!(isDesktop || isWebDesktop)) return;
|
|
final String peerId = evt['id'];
|
|
final String alias = evt['alias'];
|
|
String label = getDesktopTabLabel(peerId, alias);
|
|
final rxTabLabel = PeerStringOption.find(evt['id'], 'tabLabel');
|
|
if (rxTabLabel.value != label) {
|
|
rxTabLabel.value = label;
|
|
}
|
|
}
|
|
|
|
Future<void> updateCurDisplay(SessionID sessionId,
|
|
{updateCursorPos = false}) async {
|
|
final newRect = displaysRect();
|
|
if (newRect == null) {
|
|
return;
|
|
}
|
|
if (newRect != _rect) {
|
|
if (newRect.left != _rect?.left || newRect.top != _rect?.top) {
|
|
parent.target?.cursorModel.updateDisplayOrigin(
|
|
newRect.left, newRect.top,
|
|
updateCursorPos: updateCursorPos);
|
|
}
|
|
_rect = newRect;
|
|
// Await updateViewStyle to ensure view geometry is fully updated before
|
|
// updating pointer lock center. This prevents stale center calculations.
|
|
await parent.target?.canvasModel
|
|
.updateViewStyle(refreshMousePos: updateCursorPos);
|
|
_updateSessionWidthHeight(sessionId);
|
|
|
|
// Keep pointer lock center in sync when using relative mouse mode.
|
|
// Note: updatePointerLockCenter is async-safe (handles errors internally),
|
|
// so we fire-and-forget here.
|
|
final inputModel = parent.target?.inputModel;
|
|
if (inputModel != null && inputModel.relativeMouseMode.value) {
|
|
inputModel.updatePointerLockCenter();
|
|
}
|
|
}
|
|
}
|
|
|
|
handleSwitchDisplay(
|
|
Map<String, dynamic> evt, SessionID sessionId, String peerId) {
|
|
final display = int.parse(evt['display']);
|
|
|
|
if (_pi.currentDisplay != kAllDisplayValue) {
|
|
if (bind.peerGetSessionsCount(
|
|
id: peerId, connType: parent.target!.connType.index) >
|
|
1) {
|
|
if (display != _pi.currentDisplay) {
|
|
return;
|
|
}
|
|
}
|
|
if (!_pi.isSupportMultiUiSession) {
|
|
_pi.currentDisplay = display;
|
|
}
|
|
// If `isSupportMultiUiSession` is true, the switch display message should not be used to update current display.
|
|
// It is only used to update the display info.
|
|
}
|
|
|
|
var newDisplay = Display();
|
|
newDisplay.x = double.tryParse(evt['x']) ?? newDisplay.x;
|
|
newDisplay.y = double.tryParse(evt['y']) ?? newDisplay.y;
|
|
newDisplay.width = int.tryParse(evt['width']) ?? newDisplay.width;
|
|
newDisplay.height = int.tryParse(evt['height']) ?? newDisplay.height;
|
|
newDisplay.cursorEmbedded = int.tryParse(evt['cursor_embedded']) == 1;
|
|
newDisplay.originalWidth = int.tryParse(
|
|
evt['original_width'] ?? kInvalidResolutionValue.toString()) ??
|
|
kInvalidResolutionValue;
|
|
newDisplay.originalHeight = int.tryParse(
|
|
evt['original_height'] ?? kInvalidResolutionValue.toString()) ??
|
|
kInvalidResolutionValue;
|
|
newDisplay._scale = _pi.scaleOfDisplay(display);
|
|
_pi.displays[display] = newDisplay;
|
|
|
|
if (!_pi.isSupportMultiUiSession || _pi.currentDisplay == display) {
|
|
updateCurDisplay(sessionId);
|
|
}
|
|
|
|
if (!_pi.isSupportMultiUiSession) {
|
|
try {
|
|
CurrentDisplayState.find(peerId).value = display;
|
|
} catch (e) {
|
|
//
|
|
}
|
|
}
|
|
|
|
if (!_pi.isSupportMultiUiSession || _pi.currentDisplay == display) {
|
|
handleResolutions(peerId, evt['resolutions']);
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
cancelMsgBox(Map<String, dynamic> evt, SessionID sessionId) {
|
|
if (parent.target == null) return;
|
|
final dialogManager = parent.target!.dialogManager;
|
|
final tag = '$sessionId-${evt['tag']}';
|
|
dialogManager.dismissByTag(tag);
|
|
}
|
|
|
|
handleMultipleWindowsSession(
|
|
Map<String, dynamic> evt, SessionID sessionId, String peerId) {
|
|
if (parent.target == null) return;
|
|
final dialogManager = parent.target!.dialogManager;
|
|
final sessions = evt['windows_sessions'];
|
|
final title = translate('Multiple Windows sessions found');
|
|
final text = translate('Please select the session you want to connect to');
|
|
final type = "";
|
|
|
|
showWindowsSessionsDialog(
|
|
type, title, text, dialogManager, sessionId, peerId, sessions);
|
|
}
|
|
|
|
/// Handle the message box event based on [evt] and [id].
|
|
handleMsgBox(Map<String, dynamic> evt, SessionID sessionId, String peerId) {
|
|
if (parent.target == null) return;
|
|
final dialogManager = parent.target!.dialogManager;
|
|
final type = evt['type'];
|
|
final title = evt['title'];
|
|
final text = evt['text'];
|
|
final link = evt['link'];
|
|
|
|
if (isAndroid &&
|
|
_androidDocumentPickerActive &&
|
|
title == 'Connection Error') {
|
|
_androidDocumentPickerInterruptedConnection = true;
|
|
return;
|
|
}
|
|
|
|
// Disable relative mouse mode on any error-type message to ensure cursor is released.
|
|
// This includes connection errors, session-ending messages, elevation errors, etc.
|
|
// Safety: releasing pointer lock on errors prevents the user from being stuck.
|
|
if (title == 'Connection Error' ||
|
|
type == 'error' ||
|
|
type == 'restarting' ||
|
|
(type is String && type.contains('error'))) {
|
|
parent.target?.inputModel.setRelativeMouseMode(false);
|
|
}
|
|
|
|
if (type == 're-input-password') {
|
|
wrongPasswordDialog(sessionId, dialogManager, type, title, text);
|
|
} else if (type == 'input-2fa') {
|
|
enter2FaDialog(sessionId, dialogManager);
|
|
} else if (type == 'input-password') {
|
|
enterPasswordDialog(sessionId, dialogManager);
|
|
} else if (type == 'terminal-admin-login') {
|
|
enterUserLoginDialog(
|
|
sessionId, dialogManager, 'terminal-admin-login-tip');
|
|
} else if (type == 'terminal-admin-login-password') {
|
|
enterUserLoginAndPasswordDialog(
|
|
sessionId, dialogManager, 'terminal-admin-login-tip');
|
|
} else if (type == 'restarting') {
|
|
// Treat restart messages as reconnect control events. Rust still sends
|
|
// title/text for legacy UI and translation reuse; Flutter keeps the last
|
|
// frame briefly, then shows the Connecting overlay.
|
|
if (_restartReconnectDelayTimer == null) {
|
|
parent.target?.inputModel.setRelativeMouseMode(false);
|
|
_cancelPendingMonitorRestore();
|
|
bind.sessionReconnect(sessionId: sessionId, forceRelay: false);
|
|
clearPermissions();
|
|
// Retry once more after the silent window so restart reconnect attempts
|
|
// are spaced by the empirical short cadence instead of only updating UI.
|
|
_restartReconnectDelayTimer =
|
|
Timer(Duration(seconds: _restartReconnectSilentDelaySecs), () {
|
|
_restartReconnectDelayTimer = null;
|
|
if (parent.target?.closed == true) {
|
|
return;
|
|
}
|
|
reconnect(dialogManager, sessionId, false);
|
|
});
|
|
}
|
|
} else if (type == 'restarting-show') {
|
|
_restartReconnectDelayTimer?.cancel();
|
|
_restartReconnectDelayTimer = null;
|
|
reconnect(dialogManager, sessionId, false);
|
|
} else if (type == 'wait-remote-accept-nook') {
|
|
showWaitAcceptDialog(sessionId, type, title, text, dialogManager);
|
|
} else if (type == 'on-uac' || type == 'on-foreground-elevated') {
|
|
showOnBlockDialog(sessionId, type, title, text, dialogManager);
|
|
} else if (type == 'wait-uac') {
|
|
showWaitUacDialog(sessionId, dialogManager, type);
|
|
} else if (type == 'elevation-error') {
|
|
showElevationError(sessionId, type, title, text, dialogManager);
|
|
} else if (type == 'relay-hint' || type == 'relay-hint2') {
|
|
showRelayHintDialog(sessionId, type, title, text, dialogManager, peerId);
|
|
} else if (text == kMsgboxTextWaitingForImage) {
|
|
showConnectedWaitingForImage(dialogManager, sessionId, type, title, text);
|
|
} else if (title == 'Privacy mode') {
|
|
final hasRetry = evt['hasRetry'] == 'true';
|
|
showPrivacyFailedDialog(
|
|
sessionId, type, title, text, link, hasRetry, dialogManager);
|
|
} else {
|
|
var hasRetry = evt['hasRetry'] == 'true';
|
|
if (!hasRetry) {
|
|
hasRetry = shouldAutoRetryOnOffline(type, title, text);
|
|
}
|
|
showMsgBox(sessionId, type, title, text, link, hasRetry, dialogManager);
|
|
}
|
|
}
|
|
|
|
void resetRestartReconnectState() {
|
|
_restartReconnectDelayTimer?.cancel();
|
|
_restartReconnectDelayTimer = null;
|
|
}
|
|
|
|
void beginAndroidDocumentPicker() {
|
|
if (!isAndroid) return;
|
|
_androidDocumentPickerActive = true;
|
|
_androidDocumentPickerInterruptedConnection = false;
|
|
}
|
|
|
|
void endAndroidDocumentPicker() {
|
|
if (!isAndroid) return;
|
|
_androidDocumentPickerActive = false;
|
|
if (!_androidDocumentPickerInterruptedConnection ||
|
|
parent.target?.closed == true) {
|
|
return;
|
|
}
|
|
_androidDocumentPickerInterruptedConnection = false;
|
|
reconnect(parent.target!.dialogManager, sessionId, false);
|
|
}
|
|
|
|
/// Auto-retry check for "Remote desktop is offline" error.
|
|
/// returns true to auto-retry, false otherwise.
|
|
bool shouldAutoRetryOnOffline(
|
|
String type,
|
|
String title,
|
|
String text,
|
|
) {
|
|
if (type == 'error' &&
|
|
title == 'Connection Error' &&
|
|
text == 'Remote desktop is offline' &&
|
|
_pi.isSet.isTrue) {
|
|
// Auto retry for ~30s (server's peer offline threshold) when controlled peer's account changes
|
|
// (e.g., signout, switch user, login into OS) causes temporary offline via websocket/tcp connection.
|
|
// The actual wait may exceed 30s (e.g., 20s elapsed + 16s next retry = 36s), which is acceptable
|
|
// since the controlled side reconnects quickly after account changes.
|
|
// Uses time-based check instead of _reconnects count because user can manually retry.
|
|
// https://github.com/rustdesk/rustdesk/discussions/14048
|
|
if (_offlineReconnectStartTime == null) {
|
|
// First offline, record time and start retry
|
|
_offlineReconnectStartTime = DateTime.now();
|
|
return true;
|
|
} else {
|
|
final elapsed =
|
|
DateTime.now().difference(_offlineReconnectStartTime!).inSeconds;
|
|
if (elapsed < 30) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
handleToast(Map<String, dynamic> evt, SessionID sessionId, String peerId) {
|
|
final type = evt['type'] ?? 'info';
|
|
final text = evt['text'] ?? '';
|
|
final durMsc = evt['dur_msec'] ?? 2000;
|
|
final duration = Duration(milliseconds: durMsc);
|
|
if ((text).isEmpty) {
|
|
BotToast.showLoading(
|
|
duration: duration,
|
|
clickClose: true,
|
|
allowClick: true,
|
|
);
|
|
} else {
|
|
if (type.contains('error')) {
|
|
BotToast.showText(
|
|
contentColor: Colors.red,
|
|
text: translate(text),
|
|
duration: duration,
|
|
clickClose: true,
|
|
onlyOne: true,
|
|
);
|
|
} else {
|
|
BotToast.showText(
|
|
text: translate(text),
|
|
duration: duration,
|
|
clickClose: true,
|
|
onlyOne: true,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Show a message box with [type], [title] and [text].
|
|
showMsgBox(SessionID sessionId, String type, String title, String text,
|
|
String link, bool hasRetry, OverlayDialogManager dialogManager,
|
|
{bool? hasCancel}) async {
|
|
final noteAllowed = parent.target != null &&
|
|
allowAskForNoteAtEndOfConnection(parent.target, false) &&
|
|
(title == "Connection Error" || type == "restarting");
|
|
final showNoteEdit = noteAllowed && !hasRetry;
|
|
if (showNoteEdit) {
|
|
await showConnEndAuditDialogCloseCanceled(
|
|
ffi: parent.target!, type: type, title: title, text: text);
|
|
closeConnection();
|
|
} else {
|
|
VoidCallback? onSubmit;
|
|
if (noteAllowed && hasRetry) {
|
|
final ffi = parent.target!;
|
|
onSubmit = () async {
|
|
_timer?.cancel();
|
|
_timer = null;
|
|
await showConnEndAuditDialogCloseCanceled(
|
|
ffi: ffi, type: type, title: title, text: text);
|
|
closeConnection();
|
|
};
|
|
}
|
|
msgBox(sessionId, type, title, text, link, dialogManager,
|
|
hasCancel: hasCancel,
|
|
reconnect: hasRetry ? reconnect : null,
|
|
reconnectTimeout: hasRetry ? _reconnects : null,
|
|
onSubmit: onSubmit);
|
|
}
|
|
_timer?.cancel();
|
|
if (hasRetry) {
|
|
_timer = Timer(Duration(seconds: _reconnects), () {
|
|
reconnect(dialogManager, sessionId, false);
|
|
});
|
|
_reconnects *= 2;
|
|
} else {
|
|
_reconnects = 1;
|
|
_offlineReconnectStartTime = null;
|
|
}
|
|
}
|
|
|
|
void _cancelPendingMonitorRestore() {
|
|
_pendingRestoreTimer?.cancel();
|
|
_pendingRestoreTimer = null;
|
|
pendingMonitorRestore = null;
|
|
}
|
|
|
|
void cancelPendingRestoreTimer() {
|
|
_pendingRestoreTimer?.cancel();
|
|
_pendingRestoreTimer = null;
|
|
}
|
|
|
|
void reconnect(OverlayDialogManager dialogManager, SessionID sessionId,
|
|
bool forceRelay) {
|
|
// Disable relative mouse mode before reconnecting to ensure cursor is released.
|
|
parent.target?.inputModel.setRelativeMouseMode(false);
|
|
_cancelPendingMonitorRestore();
|
|
bind.sessionReconnect(sessionId: sessionId, forceRelay: forceRelay);
|
|
clearPermissions();
|
|
dialogManager.dismissAll();
|
|
dialogManager.showLoading(translate('Connecting...'),
|
|
onCancel: closeConnection);
|
|
}
|
|
|
|
Future<void> showRelayHintDialog(
|
|
SessionID sessionId,
|
|
String type,
|
|
String title,
|
|
String text,
|
|
OverlayDialogManager dialogManager,
|
|
String peerId) async {
|
|
var hint = "\n\n${translate('relay_hint_tip')}";
|
|
if (text.contains("10054") || text.contains("104")) {
|
|
hint = "";
|
|
}
|
|
final text2 = "${translate(text)}$hint";
|
|
|
|
if (parent.target != null &&
|
|
allowAskForNoteAtEndOfConnection(parent.target, false) &&
|
|
pi.isSet.isTrue) {
|
|
if (await showConnEndAuditDialogCloseCanceled(
|
|
ffi: parent.target!, type: type, title: title, text: text2)) {
|
|
return;
|
|
}
|
|
closeConnection();
|
|
return;
|
|
}
|
|
|
|
dialogManager.show(tag: '$sessionId-$type', (setState, close, context) {
|
|
onClose() {
|
|
closeConnection();
|
|
close();
|
|
}
|
|
|
|
final style =
|
|
ElevatedButton.styleFrom(backgroundColor: Colors.green[700]);
|
|
|
|
return CustomAlertDialog(
|
|
title: null,
|
|
content: msgboxContent(type, title, text2),
|
|
actions: [
|
|
dialogButton('Close', onPressed: onClose, isOutline: true),
|
|
if (type == 'relay-hint')
|
|
dialogButton('Connect via relay',
|
|
onPressed: () => reconnect(dialogManager, sessionId, true),
|
|
buttonStyle: style,
|
|
isOutline: true),
|
|
dialogButton('Retry',
|
|
onPressed: () => reconnect(dialogManager, sessionId, false)),
|
|
if (type == 'relay-hint2')
|
|
dialogButton('Connect via relay',
|
|
onPressed: () => reconnect(dialogManager, sessionId, true),
|
|
buttonStyle: style),
|
|
],
|
|
onCancel: onClose,
|
|
);
|
|
});
|
|
}
|
|
|
|
void showConnectedWaitingForImage(OverlayDialogManager dialogManager,
|
|
SessionID sessionId, String type, String title, String text) {
|
|
onClose() {
|
|
closeConnection();
|
|
}
|
|
|
|
if (waitForFirstImage.isFalse) return;
|
|
dialogManager.show(
|
|
(setState, close, context) => CustomAlertDialog(
|
|
title: null,
|
|
content: SelectionArea(child: msgboxContent(type, title, text)),
|
|
actions: [
|
|
dialogButton("Cancel", onPressed: onClose, isOutline: true)
|
|
],
|
|
onCancel: onClose),
|
|
tag: '$sessionId-waiting-for-image',
|
|
);
|
|
waitForImageDialogShow.value = true;
|
|
waitForImageTimer = Timer(Duration(milliseconds: 1500), () {
|
|
if (waitForFirstImage.isTrue && !isRefreshing) {
|
|
bind.sessionInputOsPassword(sessionId: sessionId, value: '');
|
|
}
|
|
});
|
|
bind.sessionOnWaitingForImageDialogShow(sessionId: sessionId);
|
|
}
|
|
|
|
void showPrivacyFailedDialog(
|
|
SessionID sessionId,
|
|
String type,
|
|
String title,
|
|
String text,
|
|
String link,
|
|
bool hasRetry,
|
|
OverlayDialogManager dialogManager) {
|
|
// There are display changes on the remote side,
|
|
// which will cause some messages to refresh the canvas and dismiss dialogs.
|
|
// So we add a delay here to ensure the dialog is displayed.
|
|
Future.delayed(Duration(milliseconds: 3000), () {
|
|
showMsgBox(sessionId, type, title, text, link, hasRetry, dialogManager);
|
|
});
|
|
}
|
|
|
|
_updateSessionWidthHeight(SessionID sessionId) {
|
|
if (_rect == null) return;
|
|
if (_rect!.width <= 0 || _rect!.height <= 0) {
|
|
debugPrintStack(
|
|
label: 'invalid display size (${_rect!.width},${_rect!.height})');
|
|
} else {
|
|
final displays = _pi.getCurDisplays();
|
|
if (displays.length == 1) {
|
|
bind.sessionSetSize(
|
|
sessionId: sessionId,
|
|
display:
|
|
pi.currentDisplay == kAllDisplayValue ? 0 : pi.currentDisplay,
|
|
width: displays[0].width,
|
|
height: displays[0].height,
|
|
);
|
|
} else {
|
|
for (int i = 0; i < displays.length; ++i) {
|
|
bind.sessionSetSize(
|
|
sessionId: sessionId,
|
|
display: i,
|
|
width: displays[i].width,
|
|
height: displays[i].height,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void _queryAuditGuid(String peerId) async {
|
|
try {
|
|
if (bind.isDisableAccount()) {
|
|
return;
|
|
}
|
|
if (bind
|
|
.sessionGetAuditServerSync(sessionId: sessionId, typ: "conn/active")
|
|
.isEmpty) {
|
|
return;
|
|
}
|
|
if (!mainGetLocalBoolOptionSync(
|
|
kOptionAllowAskForNoteAtEndOfConnection)) {
|
|
return;
|
|
}
|
|
if (bind.sessionGetAuditGuid(sessionId: sessionId).isNotEmpty) {
|
|
debugPrint('Get cached audit GUID');
|
|
return;
|
|
}
|
|
final url = bind.sessionGetAuditServerSync(
|
|
sessionId: sessionId, typ: "conn/active");
|
|
if (url.isEmpty) {
|
|
return;
|
|
}
|
|
final initialConnSessionId =
|
|
bind.sessionGetConnSessionId(sessionId: sessionId);
|
|
final connType = switch (parent.target?.connType) {
|
|
ConnType.defaultConn => 0,
|
|
ConnType.fileTransfer => 1,
|
|
ConnType.portForward => 2,
|
|
ConnType.rdp => 2,
|
|
ConnType.viewCamera => 3,
|
|
ConnType.terminal => 4,
|
|
_ => 0,
|
|
};
|
|
|
|
const retryIntervals = [1, 1, 2, 2, 3, 3];
|
|
|
|
for (int attempt = 1; attempt <= retryIntervals.length; attempt++) {
|
|
final currentConnSessionId =
|
|
bind.sessionGetConnSessionId(sessionId: sessionId);
|
|
if (currentConnSessionId != initialConnSessionId) {
|
|
debugPrint('connSessionId changed, stopping audit GUID query');
|
|
return;
|
|
}
|
|
|
|
final fullUrl =
|
|
'$url?id=$peerId&session_id=$currentConnSessionId&conn_type=$connType';
|
|
|
|
debugPrint(
|
|
'Querying audit GUID, attempt $attempt/${retryIntervals.length}');
|
|
try {
|
|
var headers = getHttpHeaders();
|
|
headers['Content-Type'] = "application/json";
|
|
|
|
final response = await http.get(
|
|
Uri.parse(fullUrl),
|
|
headers: headers,
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final guid = jsonDecode(response.body) as String?;
|
|
if (guid != null && guid.isNotEmpty) {
|
|
bind.sessionSetAuditGuid(sessionId: sessionId, guid: guid);
|
|
debugPrint('Successfully retrieved audit GUID');
|
|
return;
|
|
}
|
|
} else {
|
|
debugPrint(
|
|
'Failed to query audit GUID. Status: ${response.statusCode}, Body: ${response.body}');
|
|
return;
|
|
}
|
|
} catch (e) {
|
|
debugPrint('Error querying audit GUID (attempt $attempt): $e');
|
|
}
|
|
|
|
if (attempt < retryIntervals.length) {
|
|
await Future.delayed(Duration(seconds: retryIntervals[attempt - 1]));
|
|
}
|
|
}
|
|
|
|
debugPrint(
|
|
'Failed to retrieve audit GUID after ${retryIntervals.length} attempts');
|
|
} catch (e) {
|
|
debugPrint('Error in _queryAuditGuid: $e');
|
|
}
|
|
}
|
|
|
|
/// Handle the peer info event based on [evt].
|
|
handlePeerInfo(Map<String, dynamic> evt, String peerId, bool isCache) async {
|
|
parent.target?.chatModel.voiceCallStatus.value = VoiceCallStatus.notStarted;
|
|
|
|
_queryAuditGuid(peerId);
|
|
|
|
// Map clone is required here, otherwise "evt" may be changed by other threads through the reference.
|
|
// Because this function is asynchronous, there's an "await" in this function.
|
|
cachedPeerData.peerInfo = {...evt};
|
|
// Do not cache resolutions, because a new display connection have different resolutions.
|
|
cachedPeerData.peerInfo.remove('resolutions');
|
|
|
|
// Recent peer is updated by handle_peer_info(ui_session_interface.rs) --> handle_peer_info(client.rs) --> save_config(client.rs)
|
|
bind.mainLoadRecentPeers();
|
|
|
|
parent.target?.dialogManager.dismissAll();
|
|
_pi.version = evt['version'];
|
|
// Note: Relative mouse mode is NOT auto-enabled on connect.
|
|
// Users must manually enable it via toolbar or keyboard shortcut (Ctrl+Alt+Shift+M).
|
|
//
|
|
// For desktop/webDesktop, keyboard mode initialization is handled later by
|
|
// checkDesktopKeyboardMode() which may change the mode if not supported,
|
|
// followed by updateKeyboardMode() to sync InputModel.keyboardMode.
|
|
// For mobile, updateKeyboardMode() is currently a no-op (only executes on desktop/web),
|
|
// but we call it here for consistency and future-proofing.
|
|
if (isMobile) {
|
|
parent.target?.inputModel.updateKeyboardMode();
|
|
}
|
|
_pi.isSupportMultiUiSession =
|
|
bind.isSupportMultiUiSession(version: _pi.version);
|
|
_pi.username = evt['username'];
|
|
_pi.hostname = evt['hostname'];
|
|
_pi.platform = evt['platform'];
|
|
_pi.sasEnabled = evt['sas_enabled'] == 'true';
|
|
final currentDisplay = int.parse(evt['current_display']);
|
|
if (_pi.primaryDisplay == kInvalidDisplayIndex) {
|
|
_pi.primaryDisplay = currentDisplay;
|
|
}
|
|
|
|
if (bind.peerGetSessionsCount(
|
|
id: peerId, connType: parent.target!.connType.index) <=
|
|
1) {
|
|
_pi.currentDisplay = currentDisplay;
|
|
}
|
|
|
|
try {
|
|
CurrentDisplayState.find(peerId).value = _pi.currentDisplay;
|
|
} catch (e) {
|
|
//
|
|
}
|
|
|
|
final connType = parent.target?.connType;
|
|
if (isPeerAndroid) {
|
|
_touchMode = true;
|
|
} else {
|
|
// `kOptionTouchMode` is originally peer option, but it is moved to local option later.
|
|
// We check local option first, if not set, then check peer option.
|
|
// Because if local option is not empty:
|
|
// 1. User has set the touch mode explicitly.
|
|
// 2. The advanced option (custom client) is set.
|
|
// Then we choose to use the local option.
|
|
final optLocal = bind.mainGetLocalOption(key: kOptionTouchMode);
|
|
if (optLocal != '') {
|
|
_touchMode = optLocal == 'Y';
|
|
} else {
|
|
final optSession = await bind.sessionGetOption(
|
|
sessionId: sessionId, arg: kOptionTouchMode);
|
|
_touchMode = optSession != '';
|
|
}
|
|
}
|
|
if (isMobile) {
|
|
virtualMouseMode.loadOptions();
|
|
}
|
|
if (connType == ConnType.fileTransfer) {
|
|
parent.target?.fileModel.onReady();
|
|
} else if (connType == ConnType.terminal) {
|
|
// Call onReady on all registered terminal models
|
|
final models = parent.target?._terminalModels.values ?? [];
|
|
for (final model in models) {
|
|
model.onReady();
|
|
}
|
|
} else if (connType == ConnType.defaultConn ||
|
|
connType == ConnType.viewCamera) {
|
|
List<Display> newDisplays = [];
|
|
List<dynamic> displays = json.decode(evt['displays']);
|
|
for (int i = 0; i < displays.length; ++i) {
|
|
newDisplays.add(evtToDisplay(displays[i]));
|
|
}
|
|
_pi.displays.value = newDisplays;
|
|
_pi.displaysCount.value = _pi.displays.length;
|
|
if (_pi.currentDisplay < _pi.displays.length) {
|
|
// now replaced to _updateCurDisplay
|
|
updateCurDisplay(sessionId);
|
|
}
|
|
// After reconnecting, restore the last selected monitor once the canvas is ready.
|
|
// Switching earlier can offset the view if the monitor sizes differ.
|
|
final last = lastUserDisplay;
|
|
pendingMonitorRestore = (!isCache &&
|
|
last != null &&
|
|
last != currentDisplay &&
|
|
bind.sessionGetUseAllMyDisplaysForTheRemoteSession(
|
|
sessionId: sessionId) !=
|
|
'Y' &&
|
|
((last == kAllDisplayValue && _pi.displays.isNotEmpty) ||
|
|
(last >= 0 && last < _pi.displays.length)))
|
|
? last
|
|
: null;
|
|
// Fallback if the first image event never reaches this tab (multi-UI).
|
|
_pendingRestoreTimer?.cancel();
|
|
if (pendingMonitorRestore != null) {
|
|
_pendingRestoreTimer = Timer(const Duration(milliseconds: 1500),
|
|
() => parent.target?._applyPendingMonitorRestore());
|
|
}
|
|
if (displays.isNotEmpty) {
|
|
_reconnects = 1;
|
|
_offlineReconnectStartTime = null;
|
|
resetRestartReconnectState();
|
|
waitForFirstImage.value = true;
|
|
isRefreshing = false;
|
|
}
|
|
Map<String, dynamic> features = json.decode(evt['features']);
|
|
_pi.features.privacyMode = features['privacy_mode'] == true;
|
|
if (!isCache) {
|
|
handleResolutions(peerId, evt["resolutions"]);
|
|
}
|
|
parent.target?.elevationModel.onPeerInfo(_pi);
|
|
}
|
|
if (connType == ConnType.defaultConn) {
|
|
setViewOnly(
|
|
peerId,
|
|
bind.sessionGetToggleOptionSync(
|
|
sessionId: sessionId, arg: kOptionToggleViewOnly));
|
|
setShowMyCursor(bind.sessionGetToggleOptionSync(
|
|
sessionId: sessionId, arg: kOptionToggleShowMyCursor));
|
|
}
|
|
if (connType == ConnType.defaultConn || connType == ConnType.viewCamera) {
|
|
final platformAdditions = evt['platform_additions'];
|
|
if (platformAdditions != null && platformAdditions != '') {
|
|
try {
|
|
_pi.platformAdditions = json.decode(platformAdditions);
|
|
} catch (e) {
|
|
debugPrint('Failed to decode platformAdditions $e');
|
|
}
|
|
}
|
|
}
|
|
|
|
_pi.isSet.value = true;
|
|
stateGlobal.resetLastResolutionGroupValues(peerId);
|
|
|
|
if (isDesktop || isWebDesktop) {
|
|
// checkDesktopKeyboardMode may change the keyboard mode if the current
|
|
// mode is not supported. Re-sync InputModel.keyboardMode afterwards.
|
|
// Note: updateKeyboardMode() is a no-op on mobile (early-returns).
|
|
await checkDesktopKeyboardMode();
|
|
await parent.target?.inputModel.updateKeyboardMode();
|
|
}
|
|
|
|
notifyListeners();
|
|
|
|
if (!isCache) {
|
|
tryUseAllMyDisplaysForTheRemoteSession(peerId);
|
|
}
|
|
}
|
|
|
|
checkDesktopKeyboardMode() async {
|
|
if (isInputSourceFlutter) {
|
|
// Local side, flutter keyboard input source
|
|
// Currently only map mode is supported, legacy mode is used for compatibility.
|
|
for (final mode in [kKeyMapMode, kKeyLegacyMode]) {
|
|
if (bind.sessionIsKeyboardModeSupported(
|
|
sessionId: sessionId, mode: mode)) {
|
|
await bind.sessionSetKeyboardMode(sessionId: sessionId, value: mode);
|
|
break;
|
|
}
|
|
}
|
|
} else {
|
|
final curMode = await bind.sessionGetKeyboardMode(sessionId: sessionId);
|
|
if (curMode != null) {
|
|
if (bind.sessionIsKeyboardModeSupported(
|
|
sessionId: sessionId, mode: curMode)) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
// If current keyboard mode is not supported, change to another one.
|
|
for (final mode in [kKeyMapMode, kKeyTranslateMode, kKeyLegacyMode]) {
|
|
if (bind.sessionIsKeyboardModeSupported(
|
|
sessionId: sessionId, mode: mode)) {
|
|
bind.sessionSetKeyboardMode(sessionId: sessionId, value: mode);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
tryUseAllMyDisplaysForTheRemoteSession(String peerId) async {
|
|
if (bind.sessionGetUseAllMyDisplaysForTheRemoteSession(
|
|
sessionId: sessionId) !=
|
|
'Y') {
|
|
return;
|
|
}
|
|
|
|
if (!_pi.isSupportMultiDisplay || _pi.displays.length <= 1) {
|
|
return;
|
|
}
|
|
|
|
final screenRectList = await getScreenRectList();
|
|
if (screenRectList.length <= 1) {
|
|
return;
|
|
}
|
|
|
|
// to-do: peer currentDisplay is the primary display, but the primary display may not be the first display.
|
|
// local primary display also may not be the first display.
|
|
//
|
|
// 0 is assumed to be the primary display here, for now.
|
|
|
|
// move to the first display and set fullscreen
|
|
bind.sessionSwitchDisplay(
|
|
isDesktop: isDesktop,
|
|
sessionId: sessionId,
|
|
value: Int32List.fromList([0]),
|
|
);
|
|
_pi.currentDisplay = 0;
|
|
try {
|
|
CurrentDisplayState.find(peerId).value = _pi.currentDisplay;
|
|
} catch (e) {
|
|
//
|
|
}
|
|
await tryMoveToScreenAndSetFullscreen(screenRectList[0]);
|
|
|
|
final length = _pi.displays.length < screenRectList.length
|
|
? _pi.displays.length
|
|
: screenRectList.length;
|
|
for (var i = 1; i < length; i++) {
|
|
openMonitorInNewTabOrWindow(i, peerId, _pi,
|
|
screenRect: screenRectList[i]);
|
|
}
|
|
}
|
|
|
|
tryShowAndroidActionsOverlay({int delayMSecs = 10}) {
|
|
if (isPeerAndroid) {
|
|
if (parent.target?.connType == ConnType.defaultConn &&
|
|
parent.target != null &&
|
|
parent.target!.ffiModel.permissions['keyboard'] != false) {
|
|
Timer(Duration(milliseconds: delayMSecs), () {
|
|
if (parent.target!.dialogManager.mobileActionsOverlayVisible.isTrue) {
|
|
parent.target!.dialogManager
|
|
.showMobileActionsOverlay(ffi: parent.target!);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
handleResolutions(String id, dynamic resolutions) {
|
|
try {
|
|
final resolutionsObj = json.decode(resolutions as String);
|
|
late List<dynamic> dynamicArray;
|
|
if (resolutionsObj is Map) {
|
|
// The web version
|
|
dynamicArray = (resolutionsObj as Map<String, dynamic>)['resolutions']
|
|
as List<dynamic>;
|
|
} else {
|
|
// The rust version
|
|
dynamicArray = resolutionsObj as List<dynamic>;
|
|
}
|
|
List<Resolution> arr = List.empty(growable: true);
|
|
for (int i = 0; i < dynamicArray.length; i++) {
|
|
var width = dynamicArray[i]["width"];
|
|
var height = dynamicArray[i]["height"];
|
|
if (width is int && width > 0 && height is int && height > 0) {
|
|
arr.add(Resolution(width, height));
|
|
}
|
|
}
|
|
arr.sort((a, b) {
|
|
if (b.width != a.width) {
|
|
return b.width - a.width;
|
|
} else {
|
|
return b.height - a.height;
|
|
}
|
|
});
|
|
_pi.resolutions = arr;
|
|
} catch (e) {
|
|
debugPrint("Failed to parse resolutions:$e");
|
|
}
|
|
}
|
|
|
|
Display evtToDisplay(Map<String, dynamic> evt) {
|
|
var d = Display();
|
|
d.x = evt['x']?.toDouble() ?? d.x;
|
|
d.y = evt['y']?.toDouble() ?? d.y;
|
|
d.width = evt['width'] ?? d.width;
|
|
d.height = evt['height'] ?? d.height;
|
|
d.cursorEmbedded = evt['cursor_embedded'] == 1;
|
|
d.originalWidth = evt['original_width'] ?? kInvalidResolutionValue;
|
|
d.originalHeight = evt['original_height'] ?? kInvalidResolutionValue;
|
|
d._scale = 1.0;
|
|
final scaledWidth = evt['scaled_width'];
|
|
if (scaledWidth != null) {
|
|
final sw = int.tryParse(scaledWidth.toString());
|
|
if (sw != null && sw > 0 && d.width > 0) {
|
|
d._scale = max(d.width.toDouble() / sw, 1.0);
|
|
} else {
|
|
debugPrint(
|
|
"Invalid scaled_width ($scaledWidth) or width (${d.width}), using default scale 1.0");
|
|
}
|
|
}
|
|
return d;
|
|
}
|
|
|
|
updateLastCursorId(Map<String, dynamic> evt) {
|
|
// int.parse(evt['id']) may cause FormatException
|
|
// Unhandled Exception: FormatException: Positive input exceeds the limit of integer 18446744071749110741
|
|
parent.target?.cursorModel.id = evt['id'];
|
|
}
|
|
|
|
handleCursorId(Map<String, dynamic> evt) {
|
|
cachedPeerData.lastCursorId = evt;
|
|
parent.target?.cursorModel.updateCursorId(evt);
|
|
}
|
|
|
|
handleCursorData(Map<String, dynamic> evt) async {
|
|
cachedPeerData.cursorDataList.add(evt);
|
|
await parent.target?.cursorModel.updateCursorData(evt);
|
|
}
|
|
|
|
/// Handle the peer info synchronization event based on [evt].
|
|
handleSyncPeerInfo(
|
|
Map<String, dynamic> evt, SessionID sessionId, String peerId) async {
|
|
if (evt['displays'] != null) {
|
|
cachedPeerData.peerInfo['displays'] = evt['displays'];
|
|
List<dynamic> displays = json.decode(evt['displays']);
|
|
List<Display> newDisplays = [];
|
|
for (int i = 0; i < displays.length; ++i) {
|
|
newDisplays.add(evtToDisplay(displays[i]));
|
|
}
|
|
_pi.displays.value = newDisplays;
|
|
_pi.displaysCount.value = _pi.displays.length;
|
|
|
|
if (_pi.currentDisplay == kAllDisplayValue) {
|
|
updateCurDisplay(sessionId);
|
|
// to-do: What if the displays are changed?
|
|
} else {
|
|
if (_pi.currentDisplay >= 0 &&
|
|
_pi.currentDisplay < _pi.displays.length) {
|
|
updateCurDisplay(sessionId);
|
|
} else {
|
|
if (_pi.displays.isNotEmpty) {
|
|
// Notify to switch display
|
|
msgBox(sessionId, 'custom-nook-nocancel-hasclose-info', 'Prompt',
|
|
'display_is_plugged_out_msg', '', parent.target!.dialogManager);
|
|
final isPeerPrimaryDisplayValid =
|
|
pi.primaryDisplay == kInvalidDisplayIndex ||
|
|
pi.primaryDisplay >= pi.displays.length;
|
|
final newDisplay =
|
|
isPeerPrimaryDisplayValid ? 0 : pi.primaryDisplay;
|
|
bind.sessionSwitchDisplay(
|
|
isDesktop: isDesktop,
|
|
sessionId: sessionId,
|
|
value: Int32List.fromList([newDisplay]),
|
|
);
|
|
|
|
if (_pi.isSupportMultiUiSession) {
|
|
// If the peer supports multi-ui-session, no switch display message will be send back.
|
|
// We need to update the display manually.
|
|
switchToNewDisplay(newDisplay, sessionId, peerId);
|
|
}
|
|
} else {
|
|
msgBox(sessionId, 'nocancel-error', 'Prompt', 'No Displays', '',
|
|
parent.target!.dialogManager);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
parent.target!.canvasModel
|
|
.tryUpdateScrollStyle(Duration(milliseconds: 300), null);
|
|
notifyListeners();
|
|
}
|
|
|
|
handlePlatformAdditions(
|
|
Map<String, dynamic> evt, SessionID sessionId, String peerId) async {
|
|
final updateData = evt['platform_additions'] as String?;
|
|
if (updateData == null) {
|
|
return;
|
|
}
|
|
|
|
if (updateData.isEmpty) {
|
|
_pi.platformAdditions.remove(kPlatformAdditionsRustDeskVirtualDisplays);
|
|
_pi.platformAdditions.remove(kPlatformAdditionsAmyuniVirtualDisplays);
|
|
} else {
|
|
try {
|
|
final updateJson = json.decode(updateData) as Map<String, dynamic>;
|
|
for (final key in updateJson.keys) {
|
|
_pi.platformAdditions[key] = updateJson[key];
|
|
}
|
|
if (!updateJson
|
|
.containsKey(kPlatformAdditionsRustDeskVirtualDisplays)) {
|
|
_pi.platformAdditions
|
|
.remove(kPlatformAdditionsRustDeskVirtualDisplays);
|
|
}
|
|
if (!updateJson.containsKey(kPlatformAdditionsAmyuniVirtualDisplays)) {
|
|
_pi.platformAdditions.remove(kPlatformAdditionsAmyuniVirtualDisplays);
|
|
}
|
|
} catch (e) {
|
|
debugPrint('Failed to decode platformAdditions $e');
|
|
}
|
|
}
|
|
|
|
cachedPeerData.peerInfo['platform_additions'] =
|
|
json.encode(_pi.platformAdditions);
|
|
}
|
|
|
|
handleFollowCurrentDisplay(
|
|
Map<String, dynamic> evt, SessionID sessionId, String peerId) async {
|
|
if (evt['display_idx'] != null) {
|
|
if (pi.currentDisplay == kAllDisplayValue) {
|
|
return;
|
|
}
|
|
_pi.currentDisplay = int.parse(evt['display_idx']);
|
|
try {
|
|
CurrentDisplayState.find(peerId).value = _pi.currentDisplay;
|
|
} catch (e) {
|
|
//
|
|
}
|
|
bind.sessionSwitchDisplay(
|
|
isDesktop: isDesktop,
|
|
sessionId: sessionId,
|
|
value: Int32List.fromList([_pi.currentDisplay]),
|
|
);
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
// Directly switch to the new display without waiting for the response.
|
|
switchToNewDisplay(int display, SessionID sessionId, String peerId,
|
|
{bool updateCursorPos = false}) {
|
|
// no need to wait for the response
|
|
pi.currentDisplay = display;
|
|
updateCurDisplay(sessionId, updateCursorPos: updateCursorPos);
|
|
try {
|
|
CurrentDisplayState.find(peerId).value = display;
|
|
} catch (e) {
|
|
//
|
|
}
|
|
}
|
|
|
|
updateBlockInputState(Map<String, dynamic> evt, String peerId) {
|
|
_inputBlocked = evt['input_state'] == 'on';
|
|
notifyListeners();
|
|
try {
|
|
BlockInputState.find(peerId).value = evt['input_state'] == 'on';
|
|
} catch (e) {
|
|
//
|
|
}
|
|
}
|
|
|
|
updatePrivacyMode(
|
|
Map<String, dynamic> evt, SessionID sessionId, String peerId) async {
|
|
notifyListeners();
|
|
try {
|
|
final isOn = bind.sessionGetToggleOptionSync(
|
|
sessionId: sessionId, arg: 'privacy-mode');
|
|
if (isOn) {
|
|
var privacyModeImpl = await bind.sessionGetOption(
|
|
sessionId: sessionId, arg: 'privacy-mode-impl-key');
|
|
// For compatibility, version < 1.2.4, the default value is 'privacy_mode_impl_mag'.
|
|
final initDefaultPrivacyMode = 'privacy_mode_impl_mag';
|
|
PrivacyModeState.find(peerId).value =
|
|
privacyModeImpl ?? initDefaultPrivacyMode;
|
|
} else {
|
|
PrivacyModeState.find(peerId).value = '';
|
|
}
|
|
} catch (e) {
|
|
//
|
|
}
|
|
}
|
|
|
|
void setViewOnly(String id, bool value) {
|
|
if (versionCmp(_pi.version, '1.2.0') < 0) return;
|
|
// tmp fix for https://github.com/rustdesk/rustdesk/pull/3706#issuecomment-1481242389
|
|
// because below rx not used in mobile version, so not initialized, below code will cause crash
|
|
// current our flutter code quality is fucking shit now. !!!!!!!!!!!!!!!!
|
|
try {
|
|
if (value) {
|
|
ShowRemoteCursorState.find(id).value = value;
|
|
} else {
|
|
ShowRemoteCursorState.find(id).value = bind.sessionGetToggleOptionSync(
|
|
sessionId: sessionId, arg: 'show-remote-cursor');
|
|
}
|
|
} catch (e) {
|
|
//
|
|
}
|
|
if (_viewOnly != value) {
|
|
_viewOnly = value;
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
void setShowMyCursor(bool value) {
|
|
if (_showMyCursor != value) {
|
|
_showMyCursor = value;
|
|
notifyListeners();
|
|
}
|
|
}
|
|
}
|
|
|
|
class VirtualMouseMode with ChangeNotifier {
|
|
bool _showVirtualMouse = false;
|
|
double _virtualMouseScale = 1.0;
|
|
bool _showVirtualJoystick = false;
|
|
|
|
bool get showVirtualMouse => _showVirtualMouse;
|
|
double get virtualMouseScale => _virtualMouseScale;
|
|
bool get showVirtualJoystick => _showVirtualJoystick;
|
|
|
|
FfiModel ffiModel;
|
|
|
|
VirtualMouseMode(this.ffiModel);
|
|
|
|
bool _shouldShow() => !ffiModel.isPeerAndroid;
|
|
|
|
setShowVirtualMouse(bool b) {
|
|
if (b == _showVirtualMouse) return;
|
|
if (_shouldShow()) {
|
|
_showVirtualMouse = b;
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
setVirtualMouseScale(double s) {
|
|
if (s <= 0) return;
|
|
if (s == _virtualMouseScale) return;
|
|
_virtualMouseScale = s;
|
|
bind.mainSetLocalOption(key: kOptionVirtualMouseScale, value: s.toString());
|
|
notifyListeners();
|
|
}
|
|
|
|
setShowVirtualJoystick(bool b) {
|
|
if (b == _showVirtualJoystick) return;
|
|
if (_shouldShow()) {
|
|
_showVirtualJoystick = b;
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
void loadOptions() {
|
|
_showVirtualMouse =
|
|
bind.mainGetLocalOption(key: kOptionShowVirtualMouse) == 'Y';
|
|
_virtualMouseScale = double.tryParse(
|
|
bind.mainGetLocalOption(key: kOptionVirtualMouseScale)) ??
|
|
1.0;
|
|
_showVirtualJoystick =
|
|
bind.mainGetLocalOption(key: kOptionShowVirtualJoystick) == 'Y';
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> toggleVirtualMouse() async {
|
|
await bind.mainSetLocalOption(
|
|
key: kOptionShowVirtualMouse, value: showVirtualMouse ? 'N' : 'Y');
|
|
setShowVirtualMouse(
|
|
bind.mainGetLocalOption(key: kOptionShowVirtualMouse) == 'Y');
|
|
}
|
|
|
|
Future<void> toggleVirtualJoystick() async {
|
|
await bind.mainSetLocalOption(
|
|
key: kOptionShowVirtualJoystick,
|
|
value: showVirtualJoystick ? 'N' : 'Y');
|
|
setShowVirtualJoystick(
|
|
bind.mainGetLocalOption(key: kOptionShowVirtualJoystick) == 'Y');
|
|
}
|
|
}
|
|
|
|
class ImageModel with ChangeNotifier {
|
|
ui.Image? _image;
|
|
|
|
ui.Image? get image => _image;
|
|
|
|
String id = '';
|
|
|
|
late final SessionID sessionId;
|
|
|
|
bool _useTextureRender = false;
|
|
|
|
WeakReference<FFI> parent;
|
|
|
|
final List<Function(String)> callbacksOnFirstImage = [];
|
|
|
|
ImageModel(this.parent) {
|
|
sessionId = parent.target!.sessionId;
|
|
}
|
|
|
|
get useTextureRender => _useTextureRender;
|
|
|
|
addCallbackOnFirstImage(Function(String) cb) => callbacksOnFirstImage.add(cb);
|
|
|
|
clearImage() => _image = null;
|
|
|
|
bool _webDecodingRgba = false;
|
|
final List<Uint8List> _webRgbaList = List.empty(growable: true);
|
|
webOnRgba(int display, Uint8List rgba) async {
|
|
// deep copy needed, otherwise "instantiateCodec failed: TypeError: Cannot perform Construct on a detached ArrayBuffer"
|
|
_webRgbaList.add(Uint8List.fromList(rgba));
|
|
if (_webDecodingRgba) {
|
|
return;
|
|
}
|
|
_webDecodingRgba = true;
|
|
try {
|
|
while (_webRgbaList.isNotEmpty) {
|
|
final rgba2 = _webRgbaList.last;
|
|
_webRgbaList.clear();
|
|
await decodeAndUpdate(display, rgba2);
|
|
}
|
|
} catch (e) {
|
|
debugPrint('onRgba error: $e');
|
|
}
|
|
_webDecodingRgba = false;
|
|
}
|
|
|
|
onRgba(int display, Uint8List rgba) async {
|
|
try {
|
|
await decodeAndUpdate(display, rgba);
|
|
} catch (e) {
|
|
debugPrint('onRgba error: $e');
|
|
}
|
|
platformFFI.nextRgba(sessionId, display);
|
|
}
|
|
|
|
// web only: image already created from a decoded WebCodecs frame
|
|
Future<void> onImage(
|
|
int display, ui.Image image, bool Function() isCurrentSession) async {
|
|
await update(image, isCurrentSession: isCurrentSession);
|
|
}
|
|
|
|
decodeAndUpdate(int display, Uint8List rgba) async {
|
|
final pid = parent.target?.id;
|
|
final rect = parent.target?.ffiModel.pi.getDisplayRect(display);
|
|
final image = await img.decodeImageFromPixels(
|
|
rgba,
|
|
rect?.width.toInt() ?? 0,
|
|
rect?.height.toInt() ?? 0,
|
|
isWeb | isWindows | isLinux
|
|
? ui.PixelFormat.rgba8888
|
|
: ui.PixelFormat.bgra8888,
|
|
);
|
|
if (parent.target?.id != pid) {
|
|
image?.dispose();
|
|
return;
|
|
}
|
|
await update(image);
|
|
}
|
|
|
|
Future<void> update(ui.Image? image,
|
|
{bool Function()? isCurrentSession}) async {
|
|
if (_disposeIfStale(image, isCurrentSession)) return;
|
|
if (_image == null && image != null) {
|
|
if (isDesktop || isWebDesktop) {
|
|
await parent.target?.canvasModel.updateViewStyle();
|
|
await parent.target?.canvasModel.updateScrollStyle();
|
|
await parent.target?.canvasModel.initializeEdgeScrollEdgeThickness();
|
|
}
|
|
if (parent.target != null) {
|
|
await initializeCursorAndCanvas(parent.target!);
|
|
}
|
|
}
|
|
if (_disposeIfStale(image, isCurrentSession)) return;
|
|
_image?.dispose();
|
|
_image = image;
|
|
if (image != null) notifyListeners();
|
|
}
|
|
|
|
bool _disposeIfStale(ui.Image? image, bool Function()? isCurrentSession) {
|
|
if (image == null || isCurrentSession == null) return false;
|
|
if (isCurrentSession()) return false;
|
|
image.dispose();
|
|
return true;
|
|
}
|
|
|
|
// mobile only
|
|
double get maxScale {
|
|
if (_image == null) return 1.5;
|
|
final size = parent.target!.canvasModel.getSize();
|
|
final xscale = size.width / _image!.width;
|
|
final yscale = size.height / _image!.height;
|
|
return max(1.5, max(xscale, yscale));
|
|
}
|
|
|
|
// mobile only
|
|
double get minScale {
|
|
if (_image == null) return 1.5;
|
|
final size = parent.target!.canvasModel.getSize();
|
|
final xscale = size.width / _image!.width;
|
|
final yscale = size.height / _image!.height;
|
|
return min(xscale, yscale) / 1.5;
|
|
}
|
|
|
|
updateUserTextureRender() {
|
|
final preValue = _useTextureRender;
|
|
_useTextureRender = isDesktop && bind.mainGetUseTextureRender();
|
|
if (preValue != _useTextureRender) {
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
setUseTextureRender(bool value) {
|
|
_useTextureRender = value;
|
|
notifyListeners();
|
|
}
|
|
|
|
void disposeImage() {
|
|
_image?.dispose();
|
|
_image = null;
|
|
}
|
|
}
|
|
|
|
enum ScrollStyle {
|
|
scrollbar(kRemoteScrollStyleBar),
|
|
scrollauto(kRemoteScrollStyleAuto),
|
|
scrolledge(kRemoteScrollStyleEdge);
|
|
|
|
const ScrollStyle(this.stringValue);
|
|
|
|
final String stringValue;
|
|
|
|
String toJson() {
|
|
return name;
|
|
}
|
|
|
|
static ScrollStyle fromJson(String json, [ScrollStyle? fallbackValue]) {
|
|
switch (json) {
|
|
case 'scrollbar':
|
|
return scrollbar;
|
|
case 'scrollauto':
|
|
return scrollauto;
|
|
case 'scrolledge':
|
|
return scrolledge;
|
|
}
|
|
|
|
if (fallbackValue != null) {
|
|
return fallbackValue;
|
|
}
|
|
|
|
throw ArgumentError("Unknown ScrollStyle JSON value: '$json'");
|
|
}
|
|
|
|
@override
|
|
String toString() {
|
|
return stringValue;
|
|
}
|
|
|
|
static ScrollStyle fromString(String string, [ScrollStyle? fallbackValue]) {
|
|
switch (string) {
|
|
case kRemoteScrollStyleBar:
|
|
return scrollbar;
|
|
case kRemoteScrollStyleAuto:
|
|
return scrollauto;
|
|
case kRemoteScrollStyleEdge:
|
|
return scrolledge;
|
|
}
|
|
|
|
if (fallbackValue != null) {
|
|
return fallbackValue;
|
|
}
|
|
|
|
throw ArgumentError("Unknown ScrollStyle string value: '$string'");
|
|
}
|
|
}
|
|
|
|
class ViewStyle {
|
|
final String style;
|
|
final double width;
|
|
final double height;
|
|
final int displayWidth;
|
|
final int displayHeight;
|
|
ViewStyle({
|
|
required this.style,
|
|
required this.width,
|
|
required this.height,
|
|
required this.displayWidth,
|
|
required this.displayHeight,
|
|
});
|
|
|
|
static defaultViewStyle() {
|
|
final desktop = (isDesktop || isWebDesktop);
|
|
final w =
|
|
desktop ? kDesktopDefaultDisplayWidth : kMobileDefaultDisplayWidth;
|
|
final h =
|
|
desktop ? kDesktopDefaultDisplayHeight : kMobileDefaultDisplayHeight;
|
|
return ViewStyle(
|
|
style: '',
|
|
width: w.toDouble(),
|
|
height: h.toDouble(),
|
|
displayWidth: w,
|
|
displayHeight: h,
|
|
);
|
|
}
|
|
|
|
static int _double2Int(double v) => (v * 100).round().toInt();
|
|
|
|
@override
|
|
bool operator ==(Object other) =>
|
|
other is ViewStyle &&
|
|
other.runtimeType == runtimeType &&
|
|
_innerEqual(other);
|
|
|
|
bool _innerEqual(ViewStyle other) {
|
|
return style == other.style &&
|
|
ViewStyle._double2Int(other.width) == ViewStyle._double2Int(width) &&
|
|
ViewStyle._double2Int(other.height) == ViewStyle._double2Int(height) &&
|
|
other.displayWidth == displayWidth &&
|
|
other.displayHeight == displayHeight;
|
|
}
|
|
|
|
@override
|
|
int get hashCode => Object.hash(
|
|
style,
|
|
ViewStyle._double2Int(width),
|
|
ViewStyle._double2Int(height),
|
|
displayWidth,
|
|
displayHeight,
|
|
).hashCode;
|
|
|
|
double get scale {
|
|
double s = 1.0;
|
|
if (style == kRemoteViewStyleAdaptive) {
|
|
if (width != 0 &&
|
|
height != 0 &&
|
|
displayWidth != 0 &&
|
|
displayHeight != 0) {
|
|
final s1 = width / displayWidth;
|
|
final s2 = height / displayHeight;
|
|
s = s1 < s2 ? s1 : s2;
|
|
}
|
|
} else if (style == kRemoteViewStyleCustom) {
|
|
// Custom scale is session-scoped and applied in CanvasModel.updateViewStyle()
|
|
}
|
|
return s;
|
|
}
|
|
}
|
|
|
|
enum EdgeScrollState {
|
|
inactive,
|
|
armed,
|
|
active,
|
|
}
|
|
|
|
class EdgeScrollFallbackState {
|
|
final CanvasModel _owner;
|
|
|
|
late Ticker _ticker;
|
|
|
|
Duration _lastTotalElapsed = Duration.zero;
|
|
bool _nextEventIsFirst = true;
|
|
Vector2 _encroachment = Vector2.zero();
|
|
|
|
EdgeScrollFallbackState(this._owner, TickerProvider tickerProvider) {
|
|
_ticker = tickerProvider.createTicker(emitTick);
|
|
}
|
|
|
|
void setEncroachment(Vector2 encroachment) {
|
|
_encroachment = encroachment;
|
|
}
|
|
|
|
void emitTick(Duration totalElapsed) {
|
|
if (_nextEventIsFirst) {
|
|
_lastTotalElapsed = totalElapsed;
|
|
_nextEventIsFirst = false;
|
|
} else {
|
|
final thisTickElapsed = totalElapsed - _lastTotalElapsed;
|
|
|
|
const double kFrameTime = 1000.0 / 60.0;
|
|
const double kSpeedFactor = 0.1;
|
|
|
|
var delta = _encroachment *
|
|
(kSpeedFactor * thisTickElapsed.inMilliseconds / kFrameTime);
|
|
|
|
_owner.performEdgeScroll(delta);
|
|
|
|
_lastTotalElapsed = totalElapsed;
|
|
}
|
|
}
|
|
|
|
void start() {
|
|
if (!_ticker.isActive) {
|
|
_nextEventIsFirst = true;
|
|
_ticker.start();
|
|
}
|
|
}
|
|
|
|
void stop() {
|
|
_ticker.stop();
|
|
}
|
|
}
|
|
|
|
class CanvasModel with ChangeNotifier {
|
|
// image offset of canvas
|
|
double _x = 0;
|
|
// image offset of canvas
|
|
double _y = 0;
|
|
// image scale
|
|
double _scale = 1.0;
|
|
bool _locked = false;
|
|
double _devicePixelRatio = 1.0;
|
|
Size _size = Size.zero;
|
|
// the tabbar over the image
|
|
// double tabBarHeight = 0.0;
|
|
// the window border's width
|
|
// double windowBorderWidth = 0.0;
|
|
// remote id
|
|
String id = '';
|
|
late final SessionID sessionId;
|
|
// scroll offset x percent
|
|
double _scrollX = 0.0;
|
|
// scroll offset y percent
|
|
double _scrollY = 0.0;
|
|
ScrollStyle _scrollStyle = ScrollStyle.scrollauto;
|
|
// edge scroll mode: trigger scrolling when the cursor is close to the edge of the view
|
|
int _edgeScrollEdgeThickness = 100;
|
|
// tracks whether edge scroll should be active, prevents spurious
|
|
// scrolling when the cursor enters the view from outside
|
|
EdgeScrollState _edgeScrollState = EdgeScrollState.inactive;
|
|
// fallback strategy for when Bump Mouse isn't available
|
|
late EdgeScrollFallbackState _edgeScrollFallbackState;
|
|
// to avoid hammering a non-functional Bump Mouse
|
|
bool _bumpMouseIsWorking = true;
|
|
ViewStyle _lastViewStyle = ViewStyle.defaultViewStyle();
|
|
|
|
Timer? _timerMobileFocusCanvasCursor;
|
|
Timer? _timerMobileRestoreCanvasOffset;
|
|
Offset? _offsetBeforeMobileSoftKeyboard;
|
|
double? _scaleBeforeMobileSoftKeyboard;
|
|
|
|
// `isMobileCanvasChanged` is used to avoid canvas reset when changing the input method
|
|
// after showing the soft keyboard.
|
|
bool isMobileCanvasChanged = false;
|
|
|
|
final ScrollController _horizontal = ScrollController();
|
|
final ScrollController _vertical = ScrollController();
|
|
|
|
final _imageOverflow = false.obs;
|
|
|
|
WeakReference<FFI> parent;
|
|
|
|
CanvasModel(this.parent) {
|
|
sessionId = parent.target!.sessionId;
|
|
}
|
|
|
|
double get x => _x;
|
|
double get y => _y;
|
|
double get scale => _scale;
|
|
bool get locked => _locked;
|
|
double get devicePixelRatio => _devicePixelRatio;
|
|
Size get size => _size;
|
|
ScrollStyle get scrollStyle => _scrollStyle;
|
|
ViewStyle get viewStyle => _lastViewStyle;
|
|
RxBool get imageOverflow => _imageOverflow;
|
|
|
|
void setLocked(bool value) {
|
|
if (_locked == value) return;
|
|
_locked = value;
|
|
notifyListeners();
|
|
}
|
|
|
|
_resetScroll() => setScrollPercent(0.0, 0.0);
|
|
|
|
void setScrollPercent(double x, double y) {
|
|
_scrollX = x.isFinite ? x : 0.0;
|
|
_scrollY = y.isFinite ? y : 0.0;
|
|
}
|
|
|
|
void pushScrollPositionToUI(double scrollPixelX, double scrollPixelY) {
|
|
if (_horizontal.hasClients) {
|
|
_horizontal.jumpTo(scrollPixelX);
|
|
}
|
|
if (_vertical.hasClients) {
|
|
_vertical.jumpTo(scrollPixelY);
|
|
}
|
|
}
|
|
|
|
ScrollController get scrollHorizontal => _horizontal;
|
|
ScrollController get scrollVertical => _vertical;
|
|
double get scrollX => _scrollX;
|
|
double get scrollY => _scrollY;
|
|
|
|
static double get leftToEdge =>
|
|
isDesktop ? windowBorderWidth + kDragToResizeAreaPadding.left : 0;
|
|
static double get rightToEdge =>
|
|
isDesktop ? windowBorderWidth + kDragToResizeAreaPadding.right : 0;
|
|
static double get topToEdge => isDesktop
|
|
? tabBarHeight + windowBorderWidth + kDragToResizeAreaPadding.top
|
|
: 0;
|
|
static double get bottomToEdge =>
|
|
isDesktop ? windowBorderWidth + kDragToResizeAreaPadding.bottom : 0;
|
|
|
|
Size getSize() {
|
|
final mediaData = MediaQueryData.fromView(ui.window);
|
|
final size = mediaData.size;
|
|
// If minimized, w or h may be negative here.
|
|
double w = size.width - leftToEdge - rightToEdge;
|
|
double h = size.height - topToEdge - bottomToEdge;
|
|
if (isMobile) {
|
|
// Account for horizontal safe area insets on both orientations.
|
|
w = w - mediaData.padding.left - mediaData.padding.right;
|
|
// Vertically, subtract the bottom keyboard inset (viewInsets.bottom) and any
|
|
// bottom overlay (e.g. key-help tools) so the canvas is not covered.
|
|
h = h -
|
|
mediaData.viewInsets.bottom -
|
|
(parent.target?.cursorModel.keyHelpToolsRectToAdjustCanvas?.bottom ??
|
|
0);
|
|
// Orientation-specific handling:
|
|
// - Portrait: additionally subtract top padding (e.g. status bar / notch)
|
|
// - Landscape: does not subtract mediaData.padding.top/bottom (home indicator auto-hides)
|
|
final isPortrait = size.height > size.width;
|
|
if (isPortrait) {
|
|
// In portrait mode, subtract the top safe-area padding (e.g. status bar / notch)
|
|
// so the remote image is not truncated, while keeping the bottom inset to avoid
|
|
// introducing unnecessary blank space around the canvas.
|
|
//
|
|
// iOS -> Android, portrait, adjust mode:
|
|
// h = h (no padding subtracted): top and bottom are truncated
|
|
// https://github.com/user-attachments/assets/30ed4559-c27e-432b-847f-8fec23c9f998
|
|
// h = h - top - bottom: extra blank spaces appear
|
|
// https://github.com/user-attachments/assets/12a98817-3b4e-43aa-be0f-4b03cf364b7e
|
|
// h = h - top (current): works fine
|
|
// https://github.com/user-attachments/assets/95f047f2-7f47-4a36-8113-5023989a0c81
|
|
h = h - mediaData.padding.top;
|
|
}
|
|
}
|
|
return Size(w < 0 ? 0 : w, h < 0 ? 0 : h);
|
|
}
|
|
|
|
// mobile only
|
|
double getAdjustY() {
|
|
final bottom =
|
|
parent.target?.cursorModel.keyHelpToolsRectToAdjustCanvas?.bottom ?? 0;
|
|
return max(bottom - MediaQueryData.fromView(ui.window).padding.top, 0);
|
|
}
|
|
|
|
updateSize() => _size = getSize();
|
|
|
|
updateViewStyle({refreshMousePos = true, notify = true}) async {
|
|
final style = await bind.sessionGetViewStyle(sessionId: sessionId);
|
|
if (style == null) {
|
|
return;
|
|
}
|
|
|
|
updateSize();
|
|
final displayWidth = getDisplayWidth();
|
|
final displayHeight = getDisplayHeight();
|
|
final viewStyle = ViewStyle(
|
|
style: style,
|
|
width: size.width,
|
|
height: size.height,
|
|
displayWidth: displayWidth,
|
|
displayHeight: displayHeight,
|
|
);
|
|
// If only the Custom scale percent changed, proceed to update even if
|
|
// the basic ViewStyle fields are equal.
|
|
// In Custom scale mode, the scale percent can change independently of the other
|
|
// ViewStyle fields and is not captured by the equality check. Therefore, we must
|
|
// allow updates to proceed when style == kRemoteViewStyleCustom, even if the
|
|
// rest of the ViewStyle fields are unchanged.
|
|
if (_lastViewStyle == viewStyle && style != kRemoteViewStyleCustom) {
|
|
return;
|
|
}
|
|
if (_lastViewStyle.style != viewStyle.style) {
|
|
_resetScroll();
|
|
}
|
|
_lastViewStyle = viewStyle;
|
|
_scale = viewStyle.scale;
|
|
|
|
// Apply custom scale percent when in Custom mode
|
|
if (style == kRemoteViewStyleCustom) {
|
|
try {
|
|
_scale = await getSessionCustomScale(sessionId);
|
|
} catch (e, stack) {
|
|
debugPrint('Error in getSessionCustomScale: $e');
|
|
debugPrintStack(stackTrace: stack);
|
|
_scale = 1.0;
|
|
}
|
|
}
|
|
|
|
_devicePixelRatio = ui.window.devicePixelRatio;
|
|
if (kIgnoreDpi) {
|
|
if (style == kRemoteViewStyleOriginal) {
|
|
_scale = 1.0 / _devicePixelRatio;
|
|
} else if (_scale != 0 && style == kRemoteViewStyleCustom) {
|
|
_scale /= _devicePixelRatio;
|
|
}
|
|
}
|
|
_resetCanvasOffset(displayWidth, displayHeight);
|
|
final overflow = _x < 0 || y < 0;
|
|
if (_imageOverflow.value != overflow) {
|
|
_imageOverflow.value = overflow;
|
|
}
|
|
if (notify) {
|
|
notifyListeners();
|
|
}
|
|
if (!isMobile && refreshMousePos) {
|
|
parent.target?.inputModel.refreshMousePos();
|
|
}
|
|
tryUpdateScrollStyle(Duration.zero, style);
|
|
}
|
|
|
|
_resetCanvasOffset(int displayWidth, int displayHeight) {
|
|
_x = (size.width - displayWidth * _scale) / 2;
|
|
_y = (size.height - displayHeight * _scale) / 2;
|
|
if (isMobile) {
|
|
_moveToCenterCursor();
|
|
}
|
|
}
|
|
|
|
tryUpdateScrollStyle(Duration duration, String? style) async {
|
|
if (_scrollStyle == ScrollStyle.scrollauto) return;
|
|
style ??= await bind.sessionGetViewStyle(sessionId: sessionId);
|
|
if (style != kRemoteViewStyleOriginal && style != kRemoteViewStyleCustom) {
|
|
return;
|
|
}
|
|
|
|
_resetScroll();
|
|
|
|
Future.delayed(duration, () async {
|
|
updateScrollPercent();
|
|
});
|
|
}
|
|
|
|
Future<void> updateScrollStyle() async {
|
|
final style = await bind.sessionGetScrollStyle(sessionId: sessionId);
|
|
|
|
_scrollStyle =
|
|
style != null ? ScrollStyle.fromString(style) : ScrollStyle.scrollauto;
|
|
|
|
if (_scrollStyle != ScrollStyle.scrollauto) {
|
|
_resetScroll();
|
|
}
|
|
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> initializeEdgeScrollEdgeThickness() async {
|
|
final savedValue =
|
|
await bind.sessionGetEdgeScrollEdgeThickness(sessionId: sessionId);
|
|
|
|
if (savedValue != null) {
|
|
_edgeScrollEdgeThickness = savedValue;
|
|
}
|
|
}
|
|
|
|
void updateEdgeScrollEdgeThickness(int newThickness) {
|
|
_edgeScrollEdgeThickness = newThickness;
|
|
notifyListeners();
|
|
}
|
|
|
|
void update(double x, double y, double scale) {
|
|
_x = x;
|
|
_y = y;
|
|
_scale = scale;
|
|
notifyListeners();
|
|
}
|
|
|
|
bool get cursorEmbedded =>
|
|
parent.target?.ffiModel._pi.cursorEmbedded ?? false;
|
|
|
|
int getDisplayWidth() {
|
|
final defaultWidth = (isDesktop || isWebDesktop)
|
|
? kDesktopDefaultDisplayWidth
|
|
: kMobileDefaultDisplayWidth;
|
|
return parent.target?.ffiModel.rect?.width.toInt() ?? defaultWidth;
|
|
}
|
|
|
|
int getDisplayHeight() {
|
|
final defaultHeight = (isDesktop || isWebDesktop)
|
|
? kDesktopDefaultDisplayHeight
|
|
: kMobileDefaultDisplayHeight;
|
|
return parent.target?.ffiModel.rect?.height.toInt() ?? defaultHeight;
|
|
}
|
|
|
|
static double get windowBorderWidth => stateGlobal.windowBorderWidth.value;
|
|
static double get tabBarHeight => stateGlobal.tabBarHeight;
|
|
|
|
void activateLocalCursor() {
|
|
if (isDesktop || isWebDesktop) {
|
|
try {
|
|
RemoteCursorMovedState.find(id).value = false;
|
|
} catch (e) {
|
|
//
|
|
}
|
|
}
|
|
}
|
|
|
|
void updateLocalCursor(double x, double y) {
|
|
if (parent.target?.ffiModel.viewOnly == true) return;
|
|
// If keyboard is not permitted, do not move cursor when mouse is moving.
|
|
if (parent.target != null && parent.target!.ffiModel.keyboard) {
|
|
// Draw cursor if is not desktop.
|
|
if (!(isDesktop || isWebDesktop)) {
|
|
parent.target!.cursorModel.moveLocal(x, y);
|
|
} else {
|
|
try {
|
|
RemoteCursorMovedState.find(id).value = false;
|
|
} catch (e) {
|
|
//
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void moveDesktopMouse(double x, double y) {
|
|
if (size.width == 0 || size.height == 0) {
|
|
return;
|
|
}
|
|
|
|
// On mobile platforms, move the canvas with the cursor.
|
|
final dw = getDisplayWidth() * _scale;
|
|
final dh = getDisplayHeight() * _scale;
|
|
var dxOffset = 0;
|
|
var dyOffset = 0;
|
|
try {
|
|
if (dw > size.width) {
|
|
dxOffset = (x - dw * (x / size.width) - _x).toInt();
|
|
}
|
|
if (dh > size.height) {
|
|
dyOffset = (y - dh * (y / size.height) - _y).toInt();
|
|
}
|
|
} catch (e) {
|
|
debugPrintStack(
|
|
label:
|
|
'(x,y) ($x,$y), (_x,_y) ($_x,$_y), _scale $_scale, display size (${getDisplayWidth()},${getDisplayHeight()}), size $size, , $e');
|
|
return;
|
|
}
|
|
|
|
_x += dxOffset;
|
|
_y += dyOffset;
|
|
if (dxOffset != 0 || dyOffset != 0) {
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
void initializeEdgeScrollFallback(TickerProvider tickerProvider) {
|
|
_edgeScrollFallbackState = EdgeScrollFallbackState(this, tickerProvider);
|
|
}
|
|
|
|
void disableEdgeScroll() {
|
|
_edgeScrollState = EdgeScrollState.inactive;
|
|
cancelEdgeScroll();
|
|
}
|
|
|
|
void rearmEdgeScroll() {
|
|
_edgeScrollState = EdgeScrollState.armed;
|
|
}
|
|
|
|
void cancelEdgeScroll() {
|
|
_edgeScrollFallbackState.stop();
|
|
}
|
|
|
|
(Vector2, Vector2) getScrollInfo() {
|
|
final scrollPixel = Vector2(
|
|
_horizontal.hasClients ? _horizontal.position.pixels : 0,
|
|
_vertical.hasClients ? _vertical.position.pixels : 0);
|
|
|
|
final max = Vector2(
|
|
_horizontal.hasClients ? _horizontal.position.maxScrollExtent : 0,
|
|
_vertical.hasClients ? _vertical.position.maxScrollExtent : 0);
|
|
|
|
return (scrollPixel, max);
|
|
}
|
|
|
|
void edgeScrollMouse(double x, double y) async {
|
|
if ((_edgeScrollState == EdgeScrollState.inactive) ||
|
|
(size.width == 0 || size.height == 0) ||
|
|
!(_horizontal.hasClients || _vertical.hasClients)) {
|
|
return;
|
|
}
|
|
|
|
if (_edgeScrollState == EdgeScrollState.armed) {
|
|
// Edge scroll is armed to become active once the cursor
|
|
// is observed within the rectangle interior to the
|
|
// edge scroll regions. If the user has just moved the
|
|
// cursor in from outside of the window, edge scrolling
|
|
// doesn't happen yet.
|
|
final clientArea = Rect.fromLTWH(0, 0, size.width, size.height);
|
|
|
|
final innerZone = clientArea.deflate(_edgeScrollEdgeThickness.toDouble());
|
|
|
|
if (innerZone.contains(Offset(x, y))) {
|
|
_edgeScrollState = EdgeScrollState.active;
|
|
} else {
|
|
// Not yet.
|
|
return;
|
|
}
|
|
}
|
|
|
|
var dxOffset = 0.0;
|
|
var dyOffset = 0.0;
|
|
|
|
if (x < _edgeScrollEdgeThickness) {
|
|
dxOffset = x - _edgeScrollEdgeThickness;
|
|
} else if (x >= size.width - _edgeScrollEdgeThickness) {
|
|
dxOffset = x - (size.width - _edgeScrollEdgeThickness);
|
|
}
|
|
|
|
if (y < _edgeScrollEdgeThickness) {
|
|
dyOffset = y - _edgeScrollEdgeThickness;
|
|
} else if (y >= size.height - _edgeScrollEdgeThickness) {
|
|
dyOffset = y - (size.height - _edgeScrollEdgeThickness);
|
|
}
|
|
|
|
var encroachment = Vector2(dxOffset, dyOffset);
|
|
|
|
var (scrollPixel, max) = getScrollInfo();
|
|
|
|
encroachment.clamp(-scrollPixel, max - scrollPixel);
|
|
|
|
if (encroachment.length2 == 0) {
|
|
_edgeScrollFallbackState.stop();
|
|
} else {
|
|
var bumpAmount = -encroachment;
|
|
|
|
// Round away from 0: this ensures that the mouse will be bumped clear of
|
|
// whichever edge scroll zone(s) it is in
|
|
bumpAmount.x += bumpAmount.x.sign * 0.5;
|
|
bumpAmount.y += bumpAmount.y.sign * 0.5;
|
|
|
|
var bumpMouseSucceeded = _bumpMouseIsWorking &&
|
|
(await rustDeskWinManager.call(WindowType.Main, kWindowBumpMouse,
|
|
{"dx": bumpAmount.x.round(), "dy": bumpAmount.y.round()}))
|
|
.result;
|
|
|
|
if (bumpMouseSucceeded) {
|
|
performEdgeScroll(encroachment);
|
|
} else {
|
|
// If we can't BumpMouse, then we switch to slower scrolling with autorepeat
|
|
|
|
// Don't keep hammering BumpMouse if it's not working.
|
|
_bumpMouseIsWorking = false;
|
|
|
|
// Keep scrolling as long as the user is overtop of an edge.
|
|
_edgeScrollFallbackState.setEncroachment(encroachment);
|
|
_edgeScrollFallbackState.start();
|
|
}
|
|
}
|
|
}
|
|
|
|
void performEdgeScroll(Vector2 delta) {
|
|
var (scrollPixel, max) = getScrollInfo();
|
|
|
|
scrollPixel += delta;
|
|
|
|
scrollPixel.clamp(Vector2.zero(), max);
|
|
|
|
var scrollPixelPercent = scrollPixel.clone();
|
|
|
|
scrollPixelPercent.divide(max);
|
|
scrollPixelPercent.scale(100.0);
|
|
|
|
setScrollPercent(scrollPixelPercent.x, scrollPixelPercent.y);
|
|
pushScrollPositionToUI(scrollPixel.x, scrollPixel.y);
|
|
|
|
notifyListeners();
|
|
}
|
|
|
|
panX(double dx) {
|
|
_x += dx;
|
|
if (isMobile) {
|
|
isMobileCanvasChanged = true;
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
resetOffset() {
|
|
if (isWebDesktop) {
|
|
updateViewStyle();
|
|
} else {
|
|
_resetCanvasOffset(getDisplayWidth(), getDisplayHeight());
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
panY(double dy) {
|
|
_y += dy;
|
|
if (isMobile) {
|
|
isMobileCanvasChanged = true;
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
// mobile only
|
|
updateScale(double v, Offset focalPoint) {
|
|
if (parent.target?.imageModel.image == null) return;
|
|
final s = _scale;
|
|
_scale *= v;
|
|
final maxs = parent.target?.imageModel.maxScale ?? 1;
|
|
final mins = parent.target?.imageModel.minScale ?? 1;
|
|
if (_scale > maxs) _scale = maxs;
|
|
if (_scale < mins) _scale = mins;
|
|
// (focalPoint.dx - _x_1) / s1 + displayOriginX = (focalPoint.dx - _x_2) / s2 + displayOriginX
|
|
// _x_2 = focalPoint.dx - (focalPoint.dx - _x_1) / s1 * s2
|
|
_x = focalPoint.dx - (focalPoint.dx - _x) / s * _scale;
|
|
final adjust = getAdjustY();
|
|
// (focalPoint.dy - _y_1 - adjust) / s1 + displayOriginY = (focalPoint.dy - _y_2 - adjust) / s2 + displayOriginY
|
|
// _y_2 = focalPoint.dy - adjust - (focalPoint.dy - _y_1 - adjust) / s1 * s2
|
|
_y = focalPoint.dy - adjust - (focalPoint.dy - _y - adjust) / s * _scale;
|
|
if (isMobile) {
|
|
isMobileCanvasChanged = true;
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
// For reset canvas to the last view style
|
|
reset() {
|
|
_scale = _lastViewStyle.scale;
|
|
_devicePixelRatio = ui.window.devicePixelRatio;
|
|
if (kIgnoreDpi && _lastViewStyle.style == kRemoteViewStyleOriginal) {
|
|
_scale = 1.0 / _devicePixelRatio;
|
|
}
|
|
_resetCanvasOffset(getDisplayWidth(), getDisplayHeight());
|
|
bind.sessionSetViewStyle(sessionId: sessionId, value: _lastViewStyle.style);
|
|
notifyListeners();
|
|
}
|
|
|
|
clear() {
|
|
_x = 0;
|
|
_y = 0;
|
|
_scale = 1.0;
|
|
_locked = false;
|
|
_lastViewStyle = ViewStyle.defaultViewStyle();
|
|
_timerMobileFocusCanvasCursor?.cancel();
|
|
_timerMobileRestoreCanvasOffset?.cancel();
|
|
_offsetBeforeMobileSoftKeyboard = null;
|
|
_scaleBeforeMobileSoftKeyboard = null;
|
|
}
|
|
|
|
updateScrollPercent() {
|
|
final percentX = _horizontal.hasClients
|
|
? _horizontal.position.extentBefore /
|
|
(_horizontal.position.extentBefore +
|
|
_horizontal.position.extentInside +
|
|
_horizontal.position.extentAfter)
|
|
: 0.0;
|
|
final percentY = _vertical.hasClients
|
|
? _vertical.position.extentBefore /
|
|
(_vertical.position.extentBefore +
|
|
_vertical.position.extentInside +
|
|
_vertical.position.extentAfter)
|
|
: 0.0;
|
|
setScrollPercent(percentX, percentY);
|
|
}
|
|
|
|
void mobileFocusCanvasCursor() {
|
|
_timerMobileFocusCanvasCursor?.cancel();
|
|
_timerMobileFocusCanvasCursor =
|
|
Timer(Duration(milliseconds: 100), () async {
|
|
updateSize();
|
|
_resetCanvasOffset(getDisplayWidth(), getDisplayHeight());
|
|
notifyListeners();
|
|
});
|
|
}
|
|
|
|
void saveMobileOffsetBeforeSoftKeyboard() {
|
|
_timerMobileRestoreCanvasOffset?.cancel();
|
|
_offsetBeforeMobileSoftKeyboard = Offset(_x, _y);
|
|
_scaleBeforeMobileSoftKeyboard = _scale;
|
|
}
|
|
|
|
void restoreMobileOffsetAfterSoftKeyboard() {
|
|
_timerMobileRestoreCanvasOffset?.cancel();
|
|
_timerMobileFocusCanvasCursor?.cancel();
|
|
final targetOffset = _offsetBeforeMobileSoftKeyboard;
|
|
final targetScale = _scaleBeforeMobileSoftKeyboard;
|
|
if (targetOffset == null || targetScale == null) {
|
|
return;
|
|
}
|
|
_timerMobileRestoreCanvasOffset = Timer(Duration(milliseconds: 100), () {
|
|
updateSize();
|
|
_x = targetOffset.dx;
|
|
_y = targetOffset.dy;
|
|
_scale = targetScale;
|
|
_offsetBeforeMobileSoftKeyboard = null;
|
|
_scaleBeforeMobileSoftKeyboard = null;
|
|
notifyListeners();
|
|
});
|
|
}
|
|
|
|
// mobile only
|
|
// Move the canvas to make the cursor visible(center) on the screen.
|
|
void _moveToCenterCursor() {
|
|
Rect? imageRect = parent.target?.ffiModel.rect;
|
|
if (imageRect == null) {
|
|
// unreachable
|
|
return;
|
|
}
|
|
final maxX = 0.0;
|
|
final minX = _size.width + (imageRect.left - imageRect.right) * _scale;
|
|
final maxY = 0.0;
|
|
final minY = _size.height + (imageRect.top - imageRect.bottom) * _scale;
|
|
Offset offsetToCenter =
|
|
parent.target?.cursorModel.getCanvasOffsetToCenterCursor() ??
|
|
Offset.zero;
|
|
if (minX < 0) {
|
|
_x = min(max(offsetToCenter.dx, minX), maxX);
|
|
} else {
|
|
// _size.width > (imageRect.right, imageRect.left) * _scale, we should not change _x
|
|
}
|
|
if (minY < 0) {
|
|
_y = min(max(offsetToCenter.dy, minY), maxY);
|
|
} else {
|
|
// _size.height > (imageRect.bottom - imageRect.top) * _scale, , we should not change _y
|
|
}
|
|
}
|
|
}
|
|
|
|
// data for cursor
|
|
class CursorData {
|
|
final String peerId;
|
|
final String id;
|
|
final img2.Image image;
|
|
double scale;
|
|
Uint8List? data;
|
|
final double hotxOrigin;
|
|
final double hotyOrigin;
|
|
double hotx;
|
|
double hoty;
|
|
final int width;
|
|
final int height;
|
|
|
|
CursorData({
|
|
required this.peerId,
|
|
required this.id,
|
|
required this.image,
|
|
required this.scale,
|
|
required this.data,
|
|
required this.hotxOrigin,
|
|
required this.hotyOrigin,
|
|
required this.width,
|
|
required this.height,
|
|
}) : hotx = hotxOrigin * scale,
|
|
hoty = hotyOrigin * scale;
|
|
|
|
int _doubleToInt(double v) => (v * 10e6).round().toInt();
|
|
|
|
double _checkUpdateScale(double scale) {
|
|
double oldScale = this.scale;
|
|
if (scale != 1.0) {
|
|
// Update data if scale changed.
|
|
final tgtWidth = (width * scale).toInt();
|
|
final tgtHeight = (width * scale).toInt();
|
|
if (tgtWidth < kMinCursorSize || tgtHeight < kMinCursorSize) {
|
|
double sw = kMinCursorSize.toDouble() / width;
|
|
double sh = kMinCursorSize.toDouble() / height;
|
|
scale = sw < sh ? sh : sw;
|
|
}
|
|
}
|
|
|
|
if (_doubleToInt(oldScale) != _doubleToInt(scale)) {
|
|
if (isWindows) {
|
|
data = img2
|
|
.copyResize(
|
|
image,
|
|
width: (width * scale).toInt(),
|
|
height: (height * scale).toInt(),
|
|
interpolation: img2.Interpolation.average,
|
|
)
|
|
.getBytes(order: img2.ChannelOrder.bgra);
|
|
} else {
|
|
data = Uint8List.fromList(
|
|
img2.encodePng(
|
|
img2.copyResize(
|
|
image,
|
|
width: (width * scale).toInt(),
|
|
height: (height * scale).toInt(),
|
|
interpolation: img2.Interpolation.average,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
this.scale = scale;
|
|
hotx = hotxOrigin * scale;
|
|
hoty = hotyOrigin * scale;
|
|
return scale;
|
|
}
|
|
|
|
String updateGetKey(double scale) {
|
|
scale = _checkUpdateScale(scale);
|
|
return '${peerId}_${id}_${_doubleToInt(width * scale)}_${_doubleToInt(height * scale)}';
|
|
}
|
|
}
|
|
|
|
const _forbiddenCursorPng =
|
|
'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAAAXNSR0IB2cksfwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAkZQTFRFAAAA2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4GWAwCAAAAAAAA2B4GAAAAMTExAAAAAAAA2B4G2B4G2B4GAAAAmZmZkZGRAQEBAAAA2B4G2B4G2B4G////oKCgAwMDag8D2B4G2B4G2B4Gra2tBgYGbg8D2B4G2B4Gubm5CQkJTwsCVgwC2B4GxcXFDg4OAAAAAAAA2B4G2B4Gz8/PFBQUAAAAAAAA2B4G2B4G2B4G2B4G2B4G2B4G2B4GDgIA2NjYGxsbAAAAAAAA2B4GFwMB4eHhIyMjAAAAAAAA2B4G6OjoLCwsAAAAAAAA2B4G2B4G2B4G2B4G2B4GCQEA4ODgv7+/iYmJY2NjAgICAAAA9PT0Ojo6AAAAAAAAAAAA+/v7SkpKhYWFr6+vAAAAAAAA8/PzOTk5ERER9fX1KCgoAAAAgYGBKioqAAAAAAAApqamlpaWAAAAAAAAAAAAAAAAAAAAAAAALi4u/v7+GRkZAAAAAAAAAAAAAAAAAAAAfn5+AAAAAAAAV1dXkJCQAAAAAAAAAQEBAAAAAAAAAAAA7Hz6BAAAAMJ0Uk5TAAIWEwEynNz6//fVkCAatP2fDUHs6cDD8d0mPfT5fiEskiIR584A0gejr3AZ+P4plfALf5ZiTL85a4ziD6697fzN3UYE4v/4TwrNHuT///tdRKZh///+1U/ZBv///yjb///eAVL//50Cocv//6oFBbPvpGZCbfT//7cIhv///8INM///zBEcWYSZmO7//////1P////ts/////8vBv//////gv//R/z///QQz9sevP///2waXhNO/+fc//8mev/5gAe2r90MAAAByUlEQVR4nGNggANGJmYWBpyAlY2dg5OTi5uHF6s0H78AJxRwCAphyguLgKRExcQlQLSkFLq8tAwnp6ycPNABjAqKQKNElVDllVU4OVVhVquJA81Q10BRoAkUUYbJa4Edoo0sr6PLqaePLG/AyWlohKTAmJPTBFnelAFoixmSAnNOTgsUeQZLTk4rJAXWnJw2EHlbiDyDPCenHZICe04HFrh+RydnBgYWPU5uJAWinJwucPNd3dw9GDw5Ob2QFHBzcnrD7ffx9fMPCOTkDEINhmC4+3x8Q0LDwlEDIoKTMzIKKg9SEBIdE8sZh6SAJZ6Tkx0qD1YQkpCYlIwclCng0AXLQxSEpKalZyCryATKZwkhKQjJzsnNQ1KQXwBUUVhUXBJYWgZREFJeUVmFpMKlWg+anmqgCkJq6+obkG1pLEBTENLU3NKKrIKhrb2js8u4G6Kgpze0r3/CRAZMAHbkpJDJU6ZMmTqtFbuC6TNmhsyaMnsOFlmwgrnzpsxfELJwEXZ5Bp/FS3yWLlsesmLlKuwKVk9Ys5Zh3foN0zduwq5g85atDAzbpqSGbN9RhV0FGOzctWH3lD14FOzdt3H/gQw8Cg4u2gQPAwBYDXXdIH+wqAAAAABJRU5ErkJggg==';
|
|
const _defaultCursorPng =
|
|
'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAARzQklUCAgICHwIZIgAAAFmSURBVFiF7dWxSlxREMbx34QFDRowYBchZSxSCWlMCOwD5FGEFHap06UI7KPsAyyEEIQFqxRaCqYTsqCJFsKkuAeRXb17wrqV918dztw55zszc2fo6Oh47MR/e3zO1/iAHWmznHKGQwx9ip/LEbCfazbsoY8j/JLOhcC6sCW9wsjEwJf483AC9nPNc1+lFRwI13d+l3rYFS799rFGxJMqARv2pBXh+72XQ7gWvklPS7TmMl9Ak/M+DqrENvxAv/guKKApuKPWl0/TROK4+LbSqzhuB+OZ3fRSeFPWY+Fkyn56Y29hfgTSpnQ+s98cvorVey66uPlNFxKwZOYLCGfCs5n9NMYVrsp6mvXSoFqpqYFDvMBkStgJJe93dZOwVXxbqUnBENulydSReqUrDhcX0PT2EXarBYS3GNXMhboinBgIl9K71kg0L3+PvyYGdVpruT2MwrF0iotiXfIwus0Dj+OOjo6Of+e7ab74RkpgAAAAAElFTkSuQmCC';
|
|
|
|
const kPreForbiddenCursorId = "-2";
|
|
final preForbiddenCursor = PredefinedCursor(
|
|
png: _forbiddenCursorPng,
|
|
id: kPreForbiddenCursorId,
|
|
);
|
|
const kPreDefaultCursorId = "-1";
|
|
final preDefaultCursor = PredefinedCursor(
|
|
png: _defaultCursorPng,
|
|
id: kPreDefaultCursorId,
|
|
hotxGetter: (double w) => w / 2,
|
|
hotyGetter: (double h) => h / 2,
|
|
);
|
|
|
|
class PredefinedCursor {
|
|
ui.Image? _image;
|
|
img2.Image? _image2;
|
|
CursorData? _cache;
|
|
String png;
|
|
String id;
|
|
double Function(double)? hotxGetter;
|
|
double Function(double)? hotyGetter;
|
|
|
|
PredefinedCursor(
|
|
{required this.png, required this.id, this.hotxGetter, this.hotyGetter}) {
|
|
init();
|
|
}
|
|
|
|
ui.Image? get image => _image;
|
|
CursorData? get cache => _cache;
|
|
|
|
init() {
|
|
_image2 = img2.decodePng(base64Decode(png));
|
|
if (_image2 != null) {
|
|
// The png type of forbidden cursor image is `PngColorType.indexed`.
|
|
if (id == kPreForbiddenCursorId) {
|
|
_image2 = _image2!.convert(format: img2.Format.uint8, numChannels: 4);
|
|
}
|
|
|
|
() async {
|
|
final defaultImg = _image2!;
|
|
// This function is called only one time, no need to care about the performance.
|
|
Uint8List data = defaultImg.getBytes(order: img2.ChannelOrder.rgba);
|
|
_image?.dispose();
|
|
_image = await img.decodeImageFromPixels(
|
|
data, defaultImg.width, defaultImg.height, ui.PixelFormat.rgba8888);
|
|
if (_image == null) {
|
|
print("decodeImageFromPixels failed, pre-defined cursor $id");
|
|
return;
|
|
}
|
|
double scale = 1.0;
|
|
if (isWindows) {
|
|
data = _image2!.getBytes(order: img2.ChannelOrder.bgra);
|
|
} else {
|
|
data = Uint8List.fromList(img2.encodePng(_image2!));
|
|
}
|
|
|
|
_cache = CursorData(
|
|
peerId: '',
|
|
id: id,
|
|
image: _image2!.clone(),
|
|
scale: scale,
|
|
data: data,
|
|
hotxOrigin:
|
|
hotxGetter != null ? hotxGetter!(_image2!.width.toDouble()) : 0,
|
|
hotyOrigin:
|
|
hotyGetter != null ? hotyGetter!(_image2!.height.toDouble()) : 0,
|
|
width: _image2!.width,
|
|
height: _image2!.height,
|
|
);
|
|
}();
|
|
}
|
|
}
|
|
}
|
|
|
|
class CursorModel with ChangeNotifier {
|
|
ui.Image? _image;
|
|
final _images = <String, Tuple3<ui.Image, double, double>>{};
|
|
CursorData? _cache;
|
|
final _cacheMap = <String, CursorData>{};
|
|
final _cacheKeys = <String>{};
|
|
double _x = -10000;
|
|
double _y = -10000;
|
|
// int.parse(evt['id']) may cause FormatException
|
|
// So we use String here.
|
|
String _id = "-1";
|
|
double _hotx = 0;
|
|
double _hoty = 0;
|
|
double _displayOriginX = 0;
|
|
double _displayOriginY = 0;
|
|
DateTime? _firstUpdateMouseTime;
|
|
Rect? _windowRect;
|
|
List<RemoteWindowCoords> _remoteWindowCoords = [];
|
|
bool gotMouseControl = true;
|
|
DateTime _lastPeerMouse = DateTime.now()
|
|
.subtract(Duration(milliseconds: 3000 * kMouseControlTimeoutMSec));
|
|
String peerId = '';
|
|
WeakReference<FFI> parent;
|
|
|
|
// Only for mobile, touch mode
|
|
// To block touch event above the KeyHelpTools
|
|
//
|
|
// A better way is to not listen events from the KeyHelpTools.
|
|
// But we're now using a Container(child: Stack(...)) to wrap the KeyHelpTools,
|
|
// and the listener is on the Container.
|
|
Rect? _keyHelpToolsRect;
|
|
// `lastIsBlocked` is only used in common/widgets/remote_input.dart -> _RawTouchGestureDetectorRegionState -> onDoubleTap()
|
|
// Because onDoubleTap() doesn't have the `event` parameter, we can't get the touch event's position.
|
|
bool _lastIsBlocked = false;
|
|
bool _lastKeyboardIsVisible = false;
|
|
|
|
bool get lastKeyboardIsVisible => _lastKeyboardIsVisible;
|
|
|
|
Rect? get keyHelpToolsRectToAdjustCanvas =>
|
|
_lastKeyboardIsVisible ? _keyHelpToolsRect : null;
|
|
// The blocked rect is used to block the pointer/touch events in the remote page.
|
|
final List<Rect> _blockedRects = [];
|
|
// Used in shouldBlock().
|
|
// _blockEvents is a flag to block pointer/touch events on the remote image.
|
|
// It is set to true to prevent accidental touch events in the following scenarios:
|
|
// 1. In floating mouse mode, when the scroll circle is shown.
|
|
// 2. In floating mouse widgets mode, when the left/right buttons are moving.
|
|
// 3. In floating mouse widgets mode, when using the virtual joystick.
|
|
// When _blockEvents is true, all pointer/touch events are blocked regardless of the contents of _blockedRects.
|
|
// _blockedRects contains specific rectangular regions where events are blocked; these are checked when _blockEvents is false.
|
|
// In summary: _blockEvents acts as a global block, while _blockedRects provides fine-grained blocking.
|
|
bool _blockEvents = false;
|
|
List<Rect> get blockedRects => List.unmodifiable(_blockedRects);
|
|
|
|
set blockEvents(bool v) => _blockEvents = v;
|
|
|
|
keyHelpToolsVisibilityChanged(Rect? rect, bool keyboardIsVisible) {
|
|
_keyHelpToolsRect = rect;
|
|
if (rect == null) {
|
|
_lastIsBlocked = false;
|
|
} else {
|
|
// Block the touch event is safe here.
|
|
// `lastIsBlocked` is only used in onDoubleTap() to block the touch event from the KeyHelpTools.
|
|
// `lastIsBlocked` will be set when the cursor is moving or touch somewhere else.
|
|
_lastIsBlocked = true;
|
|
}
|
|
if (isMobile && _lastKeyboardIsVisible != keyboardIsVisible) {
|
|
if (keyboardIsVisible) {
|
|
parent.target?.canvasModel.saveMobileOffsetBeforeSoftKeyboard();
|
|
parent.target?.canvasModel.mobileFocusCanvasCursor();
|
|
parent.target?.canvasModel.isMobileCanvasChanged = false;
|
|
} else {
|
|
parent.target?.canvasModel.restoreMobileOffsetAfterSoftKeyboard();
|
|
}
|
|
}
|
|
_lastKeyboardIsVisible = keyboardIsVisible;
|
|
}
|
|
|
|
addBlockedRect(Rect rect) {
|
|
_blockedRects.add(rect);
|
|
}
|
|
|
|
removeBlockedRect(Rect rect) {
|
|
_blockedRects.remove(rect);
|
|
}
|
|
|
|
get lastIsBlocked => _lastIsBlocked;
|
|
|
|
ui.Image? get image => _image;
|
|
CursorData? get cache => _cache;
|
|
|
|
double get x => _x - _displayOriginX;
|
|
double get y => _y - _displayOriginY;
|
|
|
|
double get devicePixelRatio => parent.target!.canvasModel.devicePixelRatio;
|
|
|
|
Offset get offset => Offset(_x, _y);
|
|
|
|
double get hotx => _hotx;
|
|
double get hoty => _hoty;
|
|
|
|
set id(String id) => _id = id;
|
|
|
|
bool get isPeerControlProtected =>
|
|
DateTime.now().difference(_lastPeerMouse).inMilliseconds <
|
|
kMouseControlTimeoutMSec;
|
|
|
|
bool isConnIn2Secs() {
|
|
if (_firstUpdateMouseTime == null) {
|
|
_firstUpdateMouseTime = DateTime.now();
|
|
return true;
|
|
} else {
|
|
return DateTime.now().difference(_firstUpdateMouseTime!).inSeconds < 2;
|
|
}
|
|
}
|
|
|
|
CursorModel(this.parent);
|
|
|
|
Set<String> get cachedKeys => _cacheKeys;
|
|
addKey(String key) => _cacheKeys.add(key);
|
|
|
|
// remote physical display coordinate
|
|
// For update pan (mobile), onOneFingerPanStart, onOneFingerPanUpdate, onHoldDragUpdate
|
|
Rect getVisibleRect() {
|
|
final size = parent.target?.canvasModel.getSize() ??
|
|
MediaQueryData.fromView(ui.window).size;
|
|
final xoffset = parent.target?.canvasModel.x ?? 0;
|
|
final yoffset = parent.target?.canvasModel.y ?? 0;
|
|
final scale = parent.target?.canvasModel.scale ?? 1;
|
|
final x0 = _displayOriginX - xoffset / scale;
|
|
final y0 = _displayOriginY - yoffset / scale;
|
|
return Rect.fromLTWH(x0, y0, size.width / scale, size.height / scale);
|
|
}
|
|
|
|
Offset getCanvasOffsetToCenterCursor() {
|
|
// Cursor should be at the center of the visible rect.
|
|
// _x = rect.left + rect.width / 2
|
|
// _y = rect.right + rect.height / 2
|
|
// See `getVisibleRect()`
|
|
// _x = _displayOriginX - xoffset / scale + size.width / scale * 0.5;
|
|
// _y = _displayOriginY - yoffset / scale + size.height / scale * 0.5;
|
|
final size = parent.target?.canvasModel.getSize() ??
|
|
MediaQueryData.fromView(ui.window).size;
|
|
final xoffset = (_displayOriginX - _x) * scale + size.width * 0.5;
|
|
final yoffset = (_displayOriginY - _y) * scale + size.height * 0.5;
|
|
return Offset(xoffset, yoffset);
|
|
}
|
|
|
|
get scale => parent.target?.canvasModel.scale ?? 1.0;
|
|
|
|
// mobile Soft keyboard, block touch event from the KeyHelpTools
|
|
shouldBlock(double x, double y) {
|
|
if (_blockEvents) {
|
|
return true;
|
|
}
|
|
final offset = Offset(x, y);
|
|
for (final rect in _blockedRects) {
|
|
if (isPointInRect(offset, rect)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
// For help tools rectangle, only block touch event when in touch mode.
|
|
if (!(parent.target?.ffiModel.touchMode ?? false)) {
|
|
return false;
|
|
}
|
|
if (_keyHelpToolsRect != null &&
|
|
isPointInRect(offset, _keyHelpToolsRect!)) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// For touch mode
|
|
Future<bool> move(double x, double y) async {
|
|
if (shouldBlock(x, y)) {
|
|
_lastIsBlocked = true;
|
|
return false;
|
|
}
|
|
_lastIsBlocked = false;
|
|
if (!_moveLocalIfInRemoteRect(x, y)) {
|
|
return false;
|
|
}
|
|
await parent.target?.inputModel.moveMouse(_x, _y);
|
|
return true;
|
|
}
|
|
|
|
Future<void> syncCursorPosition() async {
|
|
await parent.target?.inputModel.moveMouse(_x, _y);
|
|
}
|
|
|
|
bool isInRemoteRect(Offset offset) {
|
|
return getRemotePosInRect(offset) != null;
|
|
}
|
|
|
|
Offset? getRemotePosInRect(Offset offset) {
|
|
final adjust = parent.target?.canvasModel.getAdjustY() ?? 0;
|
|
final newPos = _getNewPos(offset.dx, offset.dy, adjust);
|
|
final visibleRect = getVisibleRect();
|
|
if (!isPointInRect(newPos, visibleRect)) {
|
|
return null;
|
|
}
|
|
final rect = parent.target?.ffiModel.rect;
|
|
if (rect != null) {
|
|
if (!isPointInRect(newPos, rect)) {
|
|
return null;
|
|
}
|
|
}
|
|
return newPos;
|
|
}
|
|
|
|
Offset _getNewPos(double x, double y, double adjust) {
|
|
final xoffset = parent.target?.canvasModel.x ?? 0;
|
|
final yoffset = parent.target?.canvasModel.y ?? 0;
|
|
final newX = (x - xoffset) / scale + _displayOriginX;
|
|
final newY = (y - yoffset - adjust) / scale + _displayOriginY;
|
|
return Offset(newX, newY);
|
|
}
|
|
|
|
bool _moveLocalIfInRemoteRect(double x, double y) {
|
|
final newPos = getRemotePosInRect(Offset(x, y));
|
|
if (newPos == null) {
|
|
return false;
|
|
}
|
|
_x = newPos.dx;
|
|
_y = newPos.dy;
|
|
notifyListeners();
|
|
return true;
|
|
}
|
|
|
|
moveLocal(double x, double y, {double adjust = 0}) {
|
|
final newPos = _getNewPos(x, y, adjust);
|
|
_x = newPos.dx;
|
|
_y = newPos.dy;
|
|
notifyListeners();
|
|
}
|
|
|
|
reset() {
|
|
_x = _displayOriginX;
|
|
_y = _displayOriginY;
|
|
parent.target?.inputModel.moveMouse(_x, _y);
|
|
parent.target?.canvasModel.reset();
|
|
notifyListeners();
|
|
}
|
|
|
|
updatePan(Offset delta, Offset localPosition, bool touchMode) async {
|
|
if (touchMode) {
|
|
await _handleTouchMode(delta, localPosition);
|
|
return;
|
|
}
|
|
double dx = delta.dx;
|
|
double dy = delta.dy;
|
|
if (parent.target?.imageModel.image == null) return;
|
|
final scale = parent.target?.canvasModel.scale ?? 1.0;
|
|
dx /= scale;
|
|
dy /= scale;
|
|
final r = getVisibleRect();
|
|
var cx = r.center.dx;
|
|
var cy = r.center.dy;
|
|
var tryMoveCanvasX = false;
|
|
final displayRect = parent.target?.ffiModel.rect;
|
|
if (dx > 0) {
|
|
final maxCanvasCanMove = _displayOriginX +
|
|
(displayRect?.width ?? 1280) -
|
|
r.right.roundToDouble();
|
|
tryMoveCanvasX = _x + dx > cx && maxCanvasCanMove > 0;
|
|
if (tryMoveCanvasX) {
|
|
dx = min(dx, maxCanvasCanMove);
|
|
} else {
|
|
final maxCursorCanMove = r.right - _x;
|
|
dx = min(dx, maxCursorCanMove);
|
|
}
|
|
} else if (dx < 0) {
|
|
final maxCanvasCanMove = _displayOriginX - r.left.roundToDouble();
|
|
tryMoveCanvasX = _x + dx < cx && maxCanvasCanMove < 0;
|
|
if (tryMoveCanvasX) {
|
|
dx = max(dx, maxCanvasCanMove);
|
|
} else {
|
|
final maxCursorCanMove = r.left - _x;
|
|
dx = max(dx, maxCursorCanMove);
|
|
}
|
|
}
|
|
var tryMoveCanvasY = false;
|
|
if (dy > 0) {
|
|
final mayCanvasCanMove = _displayOriginY +
|
|
(displayRect?.height ?? 720) -
|
|
r.bottom.roundToDouble();
|
|
tryMoveCanvasY = _y + dy > cy && mayCanvasCanMove > 0;
|
|
if (tryMoveCanvasY) {
|
|
dy = min(dy, mayCanvasCanMove);
|
|
} else {
|
|
final mayCursorCanMove = r.bottom - _y;
|
|
dy = min(dy, mayCursorCanMove);
|
|
}
|
|
} else if (dy < 0) {
|
|
final mayCanvasCanMove = _displayOriginY - r.top.roundToDouble();
|
|
tryMoveCanvasY = _y + dy < cy && mayCanvasCanMove < 0;
|
|
if (tryMoveCanvasY) {
|
|
dy = max(dy, mayCanvasCanMove);
|
|
} else {
|
|
final mayCursorCanMove = r.top - _y;
|
|
dy = max(dy, mayCursorCanMove);
|
|
}
|
|
}
|
|
|
|
if (dx == 0 && dy == 0) return;
|
|
|
|
Point<double>? newPos;
|
|
final rect = parent.target?.ffiModel.rect;
|
|
if (rect == null) {
|
|
// unreachable
|
|
return;
|
|
}
|
|
newPos = InputModel.getPointInRemoteRect(
|
|
false,
|
|
parent.target?.ffiModel.pi.platform,
|
|
kPointerEventKindMouse,
|
|
kMouseEventTypeDefault,
|
|
_x + dx,
|
|
_y + dy,
|
|
rect,
|
|
buttons: kPrimaryButton);
|
|
if (newPos == null) {
|
|
return;
|
|
}
|
|
dx = newPos.x - _x;
|
|
dy = newPos.y - _y;
|
|
_x = newPos.x;
|
|
_y = newPos.y;
|
|
if (tryMoveCanvasX && dx != 0) {
|
|
parent.target?.canvasModel.panX(-dx * scale);
|
|
}
|
|
if (tryMoveCanvasY && dy != 0) {
|
|
parent.target?.canvasModel.panY(-dy * scale);
|
|
}
|
|
|
|
parent.target?.inputModel.moveMouse(_x, _y);
|
|
notifyListeners();
|
|
}
|
|
|
|
bool _isInCurrentWindow(double x, double y) {
|
|
final w = _windowRect!.width / devicePixelRatio;
|
|
final h = _windowRect!.width / devicePixelRatio;
|
|
return x >= 0 && y >= 0 && x <= w && y <= h;
|
|
}
|
|
|
|
_handleTouchMode(Offset delta, Offset localPosition) async {
|
|
bool isMoved = false;
|
|
if (_remoteWindowCoords.isNotEmpty &&
|
|
_windowRect != null &&
|
|
!_isInCurrentWindow(localPosition.dx, localPosition.dy)) {
|
|
final coords = InputModel.findRemoteCoords(localPosition.dx,
|
|
localPosition.dy, _remoteWindowCoords, devicePixelRatio);
|
|
if (coords != null) {
|
|
double x2 =
|
|
(localPosition.dx - coords.relativeOffset.dx / devicePixelRatio) /
|
|
coords.canvas.scale;
|
|
double y2 =
|
|
(localPosition.dy - coords.relativeOffset.dy / devicePixelRatio) /
|
|
coords.canvas.scale;
|
|
x2 += coords.cursor.offset.dx;
|
|
y2 += coords.cursor.offset.dy;
|
|
await parent.target?.inputModel.moveMouse(x2, y2);
|
|
isMoved = true;
|
|
}
|
|
}
|
|
if (!isMoved) {
|
|
final rect = parent.target?.ffiModel.rect;
|
|
if (rect == null) {
|
|
// unreachable
|
|
return;
|
|
}
|
|
|
|
Offset? movementInRect(double x, double y, Rect r) {
|
|
final isXInRect = x >= r.left && x <= r.right;
|
|
final isYInRect = y >= r.top && y <= r.bottom;
|
|
if (!(isXInRect || isYInRect)) {
|
|
return null;
|
|
}
|
|
if (x < r.left) {
|
|
x = r.left;
|
|
} else if (x > r.right) {
|
|
x = r.right;
|
|
}
|
|
if (y < r.top) {
|
|
y = r.top;
|
|
} else if (y > r.bottom) {
|
|
y = r.bottom;
|
|
}
|
|
return Offset(x, y);
|
|
}
|
|
|
|
final scale = parent.target?.canvasModel.scale ?? 1.0;
|
|
var movement =
|
|
movementInRect(_x + delta.dx / scale, _y + delta.dy / scale, rect);
|
|
if (movement == null) {
|
|
return;
|
|
}
|
|
movement = movementInRect(movement.dx, movement.dy, getVisibleRect());
|
|
if (movement == null) {
|
|
return;
|
|
}
|
|
|
|
_x = movement.dx;
|
|
_y = movement.dy;
|
|
await parent.target?.inputModel.moveMouse(_x, _y);
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
disposeImages() {
|
|
_images.forEach((_, v) => v.item1.dispose());
|
|
_images.clear();
|
|
}
|
|
|
|
updateCursorData(Map<String, dynamic> evt) async {
|
|
final id = evt['id'];
|
|
final hotx = double.parse(evt['hotx']);
|
|
final hoty = double.parse(evt['hoty']);
|
|
final width = int.parse(evt['width']);
|
|
final height = int.parse(evt['height']);
|
|
List<dynamic> colors = json.decode(evt['colors']);
|
|
final rgba = Uint8List.fromList(colors.map((s) => s as int).toList());
|
|
final image = await img.decodeImageFromPixels(
|
|
rgba, width, height, ui.PixelFormat.rgba8888);
|
|
if (image == null) {
|
|
return;
|
|
}
|
|
if (await _updateCache(rgba, image, id, hotx, hoty, width, height)) {
|
|
_images[id]?.item1.dispose();
|
|
_images[id] = Tuple3(image, hotx, hoty);
|
|
}
|
|
|
|
// Update last cursor data.
|
|
// Do not use the previous `image` and `id`, because `_id` may be changed.
|
|
_updateCurData();
|
|
}
|
|
|
|
Future<bool> _updateCache(
|
|
Uint8List rgba,
|
|
ui.Image image,
|
|
String id,
|
|
double hotx,
|
|
double hoty,
|
|
int w,
|
|
int h,
|
|
) async {
|
|
Uint8List? data;
|
|
img2.Image imgOrigin = img2.Image.fromBytes(
|
|
width: w, height: h, bytes: rgba.buffer, order: img2.ChannelOrder.rgba);
|
|
if (isWindows) {
|
|
data = imgOrigin.getBytes(order: img2.ChannelOrder.bgra);
|
|
} else {
|
|
ByteData? imgBytes =
|
|
await image.toByteData(format: ui.ImageByteFormat.png);
|
|
if (imgBytes == null) {
|
|
return false;
|
|
}
|
|
data = imgBytes.buffer.asUint8List();
|
|
}
|
|
final cache = CursorData(
|
|
peerId: peerId,
|
|
id: id,
|
|
image: imgOrigin,
|
|
scale: 1.0,
|
|
data: data,
|
|
hotxOrigin: hotx,
|
|
hotyOrigin: hoty,
|
|
width: w,
|
|
height: h,
|
|
);
|
|
_cacheMap[id] = cache;
|
|
return true;
|
|
}
|
|
|
|
bool _updateCurData() {
|
|
_cache = _cacheMap[_id];
|
|
final tmp = _images[_id];
|
|
if (tmp != null) {
|
|
_image = tmp.item1;
|
|
_hotx = tmp.item2;
|
|
_hoty = tmp.item3;
|
|
try {
|
|
// may throw exception, because the listener maybe already dispose
|
|
notifyListeners();
|
|
} catch (e) {
|
|
debugPrint(
|
|
'WARNING: updateCursorId $_id, without notifyListeners(). $e');
|
|
}
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
updateCursorId(Map<String, dynamic> evt) {
|
|
if (!_updateCurData()) {
|
|
debugPrint(
|
|
'WARNING: updateCursorId $_id, cache is ${_cache == null ? "null" : "not null"}. without notifyListeners()');
|
|
}
|
|
}
|
|
|
|
/// Update the cursor position.
|
|
updateCursorPosition(Map<String, dynamic> evt, String id) async {
|
|
if (!isConnIn2Secs()) {
|
|
gotMouseControl = false;
|
|
_lastPeerMouse = DateTime.now();
|
|
}
|
|
_x = double.parse(evt['x']);
|
|
_y = double.parse(evt['y']);
|
|
try {
|
|
RemoteCursorMovedState.find(id).value = true;
|
|
} catch (e) {
|
|
//
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
updateDisplayOrigin(double x, double y, {updateCursorPos = true}) {
|
|
_displayOriginX = x;
|
|
_displayOriginY = y;
|
|
if (updateCursorPos) {
|
|
_x = x + 1;
|
|
_y = y + 1;
|
|
parent.target?.inputModel.moveMouse(x, y);
|
|
}
|
|
parent.target?.canvasModel.resetOffset();
|
|
notifyListeners();
|
|
}
|
|
|
|
updateDisplayOriginWithCursor(
|
|
double x, double y, double xCursor, double yCursor) {
|
|
_displayOriginX = x;
|
|
_displayOriginY = y;
|
|
_x = xCursor;
|
|
_y = yCursor;
|
|
parent.target?.inputModel.moveMouse(x, y);
|
|
notifyListeners();
|
|
}
|
|
|
|
clear() {
|
|
_x = -10000;
|
|
_x = -10000;
|
|
_image = null;
|
|
_firstUpdateMouseTime = null;
|
|
gotMouseControl = true;
|
|
disposeImages();
|
|
|
|
_clearCache();
|
|
_cache = null;
|
|
_cacheMap.clear();
|
|
}
|
|
|
|
_clearCache() {
|
|
final keys = {...cachedKeys};
|
|
for (var k in keys) {
|
|
debugPrint("deleting cursor with key $k");
|
|
deleteCustomCursor(k);
|
|
}
|
|
resetSystemCursor();
|
|
}
|
|
|
|
trySetRemoteWindowCoords() {
|
|
Future.delayed(Duration.zero, () async {
|
|
_windowRect =
|
|
await InputModel.fillRemoteCoordsAndGetCurFrame(_remoteWindowCoords);
|
|
});
|
|
}
|
|
|
|
clearRemoteWindowCoords() {
|
|
_windowRect = null;
|
|
_remoteWindowCoords.clear();
|
|
}
|
|
}
|
|
|
|
class QualityMonitorData {
|
|
String? speed;
|
|
String? fps;
|
|
String? delay;
|
|
String? targetBitrate;
|
|
String? codecFormat;
|
|
String? chroma;
|
|
}
|
|
|
|
class QualityMonitorModel with ChangeNotifier {
|
|
WeakReference<FFI> parent;
|
|
|
|
QualityMonitorModel(this.parent);
|
|
var _show = false;
|
|
final _data = QualityMonitorData();
|
|
|
|
bool get show => _show;
|
|
QualityMonitorData get data => _data;
|
|
|
|
// Only a WebRTC session names its transport here: web has no session tab
|
|
// to show it on, and WebRTC is the one path that can be direct or TURN.
|
|
String? get webrtcTransport {
|
|
final ffiModel = parent.target?.ffiModel;
|
|
if (ffiModel == null) return null;
|
|
final streamType = ffiModel.cachedPeerData.streamType;
|
|
if (!streamType.startsWith('WebRTC')) return null;
|
|
return ffiModel.direct == false ? '$streamType (TURN)' : streamType;
|
|
}
|
|
|
|
checkShowQualityMonitor(SessionID sessionId) async {
|
|
final show = await bind.sessionGetToggleOption(
|
|
sessionId: sessionId, arg: 'show-quality-monitor') ==
|
|
true;
|
|
if (_show != show) {
|
|
_show = show;
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
updateQualityStatus(Map<String, dynamic> evt) {
|
|
try {
|
|
if (evt.containsKey('speed') && (evt['speed'] as String).isNotEmpty) {
|
|
_data.speed = evt['speed'];
|
|
}
|
|
if (evt.containsKey('fps') && (evt['fps'] as String).isNotEmpty) {
|
|
final fps = jsonDecode(evt['fps']) as Map<String, dynamic>;
|
|
final pi = parent.target?.ffiModel.pi;
|
|
if (pi != null) {
|
|
final currentDisplay = pi.currentDisplay;
|
|
if (currentDisplay != kAllDisplayValue) {
|
|
final fps2 = fps[currentDisplay.toString()];
|
|
if (fps2 != null) {
|
|
_data.fps = fps2.toString();
|
|
}
|
|
} else if (fps.isNotEmpty) {
|
|
final fpsList = [];
|
|
for (var i = 0; i < pi.displays.length; i++) {
|
|
fpsList.add((fps[i.toString()] ?? 0).toString());
|
|
}
|
|
_data.fps = fpsList.join(' ');
|
|
}
|
|
} else {
|
|
_data.fps = null;
|
|
}
|
|
}
|
|
if (evt.containsKey('delay') && (evt['delay'] as String).isNotEmpty) {
|
|
_data.delay = evt['delay'];
|
|
}
|
|
if (evt.containsKey('target_bitrate') &&
|
|
(evt['target_bitrate'] as String).isNotEmpty) {
|
|
_data.targetBitrate = evt['target_bitrate'];
|
|
}
|
|
if (evt.containsKey('codec_format') &&
|
|
(evt['codec_format'] as String).isNotEmpty) {
|
|
_data.codecFormat = evt['codec_format'];
|
|
}
|
|
if (evt.containsKey('chroma') && (evt['chroma'] as String).isNotEmpty) {
|
|
_data.chroma = evt['chroma'];
|
|
}
|
|
notifyListeners();
|
|
} catch (e) {
|
|
//
|
|
}
|
|
}
|
|
}
|
|
|
|
class RecordingModel with ChangeNotifier {
|
|
WeakReference<FFI> parent;
|
|
RecordingModel(this.parent);
|
|
bool _start = false;
|
|
bool get start => _start;
|
|
|
|
toggle() async {
|
|
if (isIOS) return;
|
|
final sessionId = parent.target?.sessionId;
|
|
if (sessionId == null) return;
|
|
final pi = parent.target?.ffiModel.pi;
|
|
if (pi == null) return;
|
|
bool value = !_start;
|
|
if (value) {
|
|
await sessionRefreshVideo(sessionId, pi);
|
|
}
|
|
await bind.sessionRecordScreen(sessionId: sessionId, start: value);
|
|
}
|
|
|
|
updateStatus(bool status) {
|
|
_start = status;
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
class ElevationModel with ChangeNotifier {
|
|
WeakReference<FFI> parent;
|
|
ElevationModel(this.parent);
|
|
bool _running = false;
|
|
bool _canElevate = false;
|
|
bool get showRequestMenu => _canElevate && !_running;
|
|
onPeerInfo(PeerInfo pi) {
|
|
_canElevate = pi.platform == kPeerPlatformWindows && pi.sasEnabled == false;
|
|
_running = false;
|
|
}
|
|
|
|
onPortableServiceRunning(bool running) => _running = running;
|
|
}
|
|
|
|
// The index values of `ConnType` are same as rust protobuf.
|
|
enum ConnType {
|
|
defaultConn,
|
|
fileTransfer,
|
|
portForward,
|
|
rdp,
|
|
viewCamera,
|
|
terminal
|
|
}
|
|
|
|
/// Flutter state manager and data communication with the Rust core.
|
|
class FFI {
|
|
var id = '';
|
|
var version = '';
|
|
var connType = ConnType.defaultConn;
|
|
var closed = false;
|
|
|
|
/// dialogManager use late to ensure init after main page binding [globalKey]
|
|
late final dialogManager = OverlayDialogManager();
|
|
|
|
late final SessionID sessionId;
|
|
late final ImageModel imageModel; // session
|
|
late final FfiModel ffiModel; // session
|
|
late final CursorModel cursorModel; // session
|
|
late final CanvasModel canvasModel; // session
|
|
late final ServerModel serverModel; // global
|
|
late final ChatModel chatModel; // session
|
|
late final FileModel fileModel; // session
|
|
late final AbModel abModel; // global
|
|
late final GroupModel groupModel; // global
|
|
late final UserModel userModel; // global
|
|
late final PeerTabModel peerTabModel; // global
|
|
late final QualityMonitorModel qualityMonitorModel; // session
|
|
late final RecordingModel recordingModel; // session
|
|
late final InputModel inputModel; // session
|
|
late final ElevationModel elevationModel; // session
|
|
late final CmFileModel cmFileModel; // cm
|
|
late final TextureModel textureModel; //session
|
|
late final Peers recentPeersModel; // global
|
|
late final Peers favoritePeersModel; // global
|
|
late final Peers lanPeersModel; // global
|
|
|
|
// Terminal model registry for multiple terminals
|
|
final Map<int, TerminalModel> _terminalModels = {};
|
|
|
|
// Getter for terminal models
|
|
Map<int, TerminalModel> get terminalModels => _terminalModels;
|
|
|
|
FFI(SessionID? sId) {
|
|
sessionId = sId ?? (isDesktop ? Uuid().v4obj() : _constSessionId);
|
|
imageModel = ImageModel(WeakReference(this));
|
|
ffiModel = FfiModel(WeakReference(this));
|
|
cursorModel = CursorModel(WeakReference(this));
|
|
canvasModel = CanvasModel(WeakReference(this));
|
|
serverModel = ServerModel(WeakReference(this));
|
|
chatModel = ChatModel(WeakReference(this));
|
|
fileModel = FileModel(WeakReference(this));
|
|
userModel = UserModel(WeakReference(this));
|
|
peerTabModel = PeerTabModel(WeakReference(this));
|
|
abModel = AbModel(WeakReference(this));
|
|
groupModel = GroupModel(WeakReference(this));
|
|
qualityMonitorModel = QualityMonitorModel(WeakReference(this));
|
|
recordingModel = RecordingModel(WeakReference(this));
|
|
inputModel = InputModel(WeakReference(this));
|
|
elevationModel = ElevationModel(WeakReference(this));
|
|
cmFileModel = CmFileModel(WeakReference(this));
|
|
textureModel = TextureModel(WeakReference(this));
|
|
recentPeersModel = Peers(
|
|
name: PeersModelName.recent,
|
|
loadEvent: LoadEvent.recent,
|
|
getInitPeers: null);
|
|
favoritePeersModel = Peers(
|
|
name: PeersModelName.favorite,
|
|
loadEvent: LoadEvent.favorite,
|
|
getInitPeers: null);
|
|
lanPeersModel = Peers(
|
|
name: PeersModelName.lan, loadEvent: LoadEvent.lan, getInitPeers: null);
|
|
}
|
|
|
|
/// Mobile reuse FFI
|
|
void mobileReset() {
|
|
ffiModel.resetRestartReconnectState();
|
|
ffiModel.waitForFirstImage.value = true;
|
|
ffiModel.isRefreshing = false;
|
|
ffiModel.waitForImageDialogShow.value = true;
|
|
ffiModel.waitForImageTimer?.cancel();
|
|
ffiModel.waitForImageTimer = null;
|
|
}
|
|
|
|
/// Start with the given [id]. Only transfer file if [isFileTransfer], only view camera if [isViewCamera], only port forward if [isPortForward].
|
|
void start(
|
|
String id, {
|
|
bool isFileTransfer = false,
|
|
bool isViewCamera = false,
|
|
bool isPortForward = false,
|
|
bool isRdp = false,
|
|
bool isTerminal = false,
|
|
String? switchUuid,
|
|
String? password,
|
|
bool? isSharedPassword,
|
|
String? connToken,
|
|
bool? forceRelay,
|
|
int? tabWindowId,
|
|
int? display,
|
|
List<int>? displays,
|
|
}) {
|
|
closed = false;
|
|
if (isMobile) mobileReset();
|
|
assert(
|
|
(!(isPortForward && isViewCamera)) &&
|
|
(!(isViewCamera && isPortForward)) &&
|
|
(!(isPortForward && isFileTransfer)) &&
|
|
(!(isTerminal && isFileTransfer)) &&
|
|
(!(isTerminal && isViewCamera)) &&
|
|
(!(isTerminal && isPortForward)),
|
|
'more than one connect type');
|
|
if (isFileTransfer) {
|
|
connType = ConnType.fileTransfer;
|
|
} else if (isViewCamera) {
|
|
connType = ConnType.viewCamera;
|
|
} else if (isPortForward) {
|
|
connType = ConnType.portForward;
|
|
} else if (isTerminal) {
|
|
connType = ConnType.terminal;
|
|
} else {
|
|
chatModel.resetClientMode();
|
|
connType = ConnType.defaultConn;
|
|
canvasModel.id = id;
|
|
imageModel.id = id;
|
|
cursorModel.peerId = id;
|
|
}
|
|
|
|
final isNewPeer = tabWindowId == null;
|
|
// If tabWindowId != null, this session is a "tab -> window" one.
|
|
// Else this session is a new one.
|
|
if (isNewPeer) {
|
|
// ignore: unused_local_variable
|
|
final addRes = bind.sessionAddSync(
|
|
sessionId: sessionId,
|
|
id: id,
|
|
isFileTransfer: isFileTransfer,
|
|
isViewCamera: isViewCamera,
|
|
isPortForward: isPortForward,
|
|
isRdp: isRdp,
|
|
isTerminal: isTerminal,
|
|
switchUuid: switchUuid ?? '',
|
|
forceRelay: forceRelay ?? false,
|
|
password: password ?? '',
|
|
isSharedPassword: isSharedPassword ?? false,
|
|
connToken: connToken,
|
|
);
|
|
} else if (display != null) {
|
|
if (displays == null) {
|
|
debugPrint(
|
|
'Unreachable, failed to add existed session to $id, the displays is null while display is $display');
|
|
return;
|
|
}
|
|
final addRes = bind.sessionAddExistedSync(
|
|
id: id,
|
|
sessionId: sessionId,
|
|
displays: Int32List.fromList(displays),
|
|
isViewCamera: isViewCamera);
|
|
if (addRes != '') {
|
|
debugPrint(
|
|
'Unreachable, failed to add existed session to $id, $addRes');
|
|
return;
|
|
}
|
|
ffiModel.pi.currentDisplay = display;
|
|
}
|
|
if (isDesktop && connType == ConnType.defaultConn) {
|
|
textureModel.updateCurrentDisplay(display ?? 0);
|
|
}
|
|
// FIXME: separate cameras displays or shift all indices.
|
|
if (isDesktop && connType == ConnType.viewCamera) {
|
|
// FIXME: currently the default 0 is not used.
|
|
textureModel.updateCurrentDisplay(display ?? 0);
|
|
}
|
|
|
|
if (isDesktop) {
|
|
inputModel.updateTrackpadSpeed();
|
|
}
|
|
|
|
// CAUTION: `sessionStart()` and `sessionStartWithDisplays()` are an async functions.
|
|
// Though the stream is returned immediately, the stream may not be ready.
|
|
// Any operations that depend on the stream should be carefully handled.
|
|
late final Stream<EventToUI> stream;
|
|
if (isNewPeer || display == null || displays == null) {
|
|
stream = bind.sessionStart(sessionId: sessionId, id: id);
|
|
} else {
|
|
// We have to put displays in `sessionStart()` to make sure the stream is ready
|
|
// and then the displays' capturing requests can be sent.
|
|
stream = bind.sessionStartWithDisplays(
|
|
sessionId: sessionId, id: id, displays: Int32List.fromList(displays));
|
|
}
|
|
|
|
if (isWeb) {
|
|
platformFFI.setRgbaCallback((int display, Uint8List data) {
|
|
onEvent2UIRgba();
|
|
imageModel.onRgba(display, data);
|
|
});
|
|
platformFFI.setVideoFrameCallback((int display, ui.Image image,
|
|
bool Function() isCurrentSession) async {
|
|
if (!isCurrentSession()) {
|
|
image.dispose();
|
|
return;
|
|
}
|
|
await onEvent2UIRgba();
|
|
await imageModel.onImage(display, image, isCurrentSession);
|
|
});
|
|
this.id = id;
|
|
return;
|
|
}
|
|
|
|
final cb = ffiModel.startEventListener(sessionId, id);
|
|
|
|
imageModel.updateUserTextureRender();
|
|
final hasGpuTextureRender = bind.mainHasGpuTextureRender();
|
|
final SimpleWrapper<bool> isToNewWindowNotified = SimpleWrapper(false);
|
|
// Preserved for the rgba data.
|
|
stream.listen((message) {
|
|
if (closed) return;
|
|
if (tabWindowId != null && !isToNewWindowNotified.value) {
|
|
// Session is read to be moved to a new window.
|
|
// Get the cached data and handle the cached data.
|
|
Future.delayed(Duration.zero, () async {
|
|
final args = jsonEncode({'id': id, 'close': display == null});
|
|
final cachedData = await DesktopMultiWindow.invokeMethod(
|
|
tabWindowId, kWindowEventGetCachedSessionData, args);
|
|
if (cachedData == null) {
|
|
// unreachable
|
|
debugPrint('Unreachable, the cached data is empty.');
|
|
return;
|
|
}
|
|
final data = CachedPeerData.fromString(cachedData);
|
|
if (data == null) {
|
|
debugPrint('Unreachable, the cached data cannot be decoded.');
|
|
return;
|
|
}
|
|
ffiModel.setPermissions(data.permissions);
|
|
await ffiModel.handleCachedPeerData(data, id);
|
|
await sessionRefreshVideo(sessionId, ffiModel.pi);
|
|
await bind.sessionRequestNewDisplayInitMsgs(
|
|
sessionId: sessionId, display: ffiModel.pi.currentDisplay);
|
|
});
|
|
isToNewWindowNotified.value = true;
|
|
}
|
|
() async {
|
|
if (message is EventToUI_Event) {
|
|
if (message.field0 == "close") {
|
|
closed = true;
|
|
debugPrint('Exit session event loop');
|
|
return;
|
|
}
|
|
|
|
Map<String, dynamic>? event;
|
|
try {
|
|
event = json.decode(message.field0);
|
|
} catch (e) {
|
|
debugPrint('json.decode fail1(): $e, ${message.field0}');
|
|
}
|
|
if (event != null) {
|
|
await cb(event);
|
|
}
|
|
} else if (message is EventToUI_Rgba) {
|
|
final display = message.field0;
|
|
// Fetch the image buffer from rust codes.
|
|
final sz = platformFFI.getRgbaSize(sessionId, display);
|
|
if (sz == 0) {
|
|
platformFFI.nextRgba(sessionId, display);
|
|
return;
|
|
}
|
|
final rgba = platformFFI.getRgba(sessionId, display, sz);
|
|
if (rgba != null) {
|
|
onEvent2UIRgba();
|
|
await imageModel.onRgba(display, rgba);
|
|
} else {
|
|
platformFFI.nextRgba(sessionId, display);
|
|
}
|
|
} else if (message is EventToUI_Texture) {
|
|
final display = message.field0;
|
|
final gpuTexture = message.field1;
|
|
debugPrint(
|
|
"EventToUI_Texture display:$display, gpuTexture:$gpuTexture");
|
|
if (gpuTexture && !hasGpuTextureRender) {
|
|
debugPrint('the gpuTexture is not supported.');
|
|
return;
|
|
}
|
|
textureModel.setTextureType(display: display, gpuTexture: gpuTexture);
|
|
onEvent2UIRgba();
|
|
}
|
|
}();
|
|
});
|
|
// every instance will bind a stream
|
|
this.id = id;
|
|
}
|
|
|
|
Future<void> onEvent2UIRgba() async {
|
|
if (ffiModel.waitForImageDialogShow.isTrue) {
|
|
ffiModel.waitForImageDialogShow.value = false;
|
|
ffiModel.waitForImageTimer?.cancel();
|
|
clearWaitingForImage(dialogManager, sessionId);
|
|
}
|
|
if (ffiModel.waitForFirstImage.value == true) {
|
|
ffiModel.waitForFirstImage.value = false;
|
|
ffiModel.cancelPendingRestoreTimer();
|
|
ffiModel.resetRestartReconnectState();
|
|
dialogManager.dismissAll();
|
|
try {
|
|
await canvasModel.updateViewStyle();
|
|
await canvasModel.updateScrollStyle();
|
|
await canvasModel.initializeEdgeScrollEdgeThickness();
|
|
for (final cb in imageModel.callbacksOnFirstImage) {
|
|
cb(id);
|
|
}
|
|
} finally {
|
|
_applyPendingMonitorRestore();
|
|
}
|
|
}
|
|
}
|
|
|
|
void _applyPendingMonitorRestore() {
|
|
final restore = ffiModel.pendingMonitorRestore;
|
|
ffiModel._cancelPendingMonitorRestore();
|
|
if (restore == null || closed) return;
|
|
// The display list may have changed since the restore was queued.
|
|
final displays = ffiModel.pi.displays;
|
|
if ((restore == kAllDisplayValue && displays.isNotEmpty) ||
|
|
(restore >= 0 && restore < displays.length)) {
|
|
openMonitorInTheSameTab(restore, this, ffiModel.pi,
|
|
recordSelection: false, updateCursorPos: false);
|
|
}
|
|
}
|
|
|
|
/// Login with [password], choose if the client should [remember] it.
|
|
void login(String osUsername, String osPassword, SessionID sessionId,
|
|
String password, bool remember) {
|
|
bind.sessionLogin(
|
|
sessionId: sessionId,
|
|
osUsername: osUsername,
|
|
osPassword: osPassword,
|
|
password: password,
|
|
remember: remember);
|
|
}
|
|
|
|
void send2FA(SessionID sessionId, String code, bool trustThisDevice) {
|
|
bind.sessionSend2Fa(
|
|
sessionId: sessionId, code: code, trustThisDevice: trustThisDevice);
|
|
}
|
|
|
|
/// Close the remote session.
|
|
Future<void> close({bool closeSession = true}) async {
|
|
closed = true;
|
|
if (isWeb) {
|
|
platformFFI.clearVideoFrameCallback();
|
|
}
|
|
chatModel.close();
|
|
// Close all terminal models
|
|
for (final model in _terminalModels.values) {
|
|
model.dispose();
|
|
}
|
|
_terminalModels.clear();
|
|
if (imageModel.image != null && !isWebDesktop) {
|
|
await setCanvasConfig(
|
|
sessionId,
|
|
cursorModel.x,
|
|
cursorModel.y,
|
|
canvasModel.x,
|
|
canvasModel.y,
|
|
canvasModel.scale,
|
|
ffiModel.pi.currentDisplay);
|
|
}
|
|
imageModel.callbacksOnFirstImage.clear();
|
|
await imageModel.update(null);
|
|
cursorModel.clear();
|
|
ffiModel.clear();
|
|
canvasModel.clear();
|
|
inputModel.resetModifiers();
|
|
// Dispose relative mouse mode resources to ensure cursor is restored
|
|
inputModel.disposeRelativeMouseMode();
|
|
inputModel.disposeSideButtonTracking();
|
|
if (closeSession) {
|
|
await bind.sessionClose(sessionId: sessionId);
|
|
}
|
|
debugPrint('model $id closed');
|
|
id = '';
|
|
}
|
|
|
|
void setMethodCallHandler(FMethod callback) {
|
|
platformFFI.setMethodCallHandler(callback);
|
|
}
|
|
|
|
Future<bool> invokeMethod(String method, [dynamic arguments]) async {
|
|
return await platformFFI.invokeMethod(method, arguments);
|
|
}
|
|
|
|
Future<T?> invokeMethodWithResult<T>(String method,
|
|
[dynamic arguments]) async {
|
|
return await platformFFI.invokeMethodWithResult<T>(method, arguments);
|
|
}
|
|
|
|
// Terminal model management
|
|
void registerTerminalModel(int terminalId, TerminalModel model) {
|
|
debugPrint('[FFI] Registering terminal model for terminal $terminalId');
|
|
_terminalModels[terminalId] = model;
|
|
}
|
|
|
|
void unregisterTerminalModel(int terminalId) {
|
|
debugPrint('[FFI] Unregistering terminal model for terminal $terminalId');
|
|
_terminalModels.remove(terminalId);
|
|
}
|
|
|
|
void routeTerminalResponse(Map<String, dynamic> evt) {
|
|
final int terminalId = TerminalModel.getTerminalIdFromEvt(evt);
|
|
|
|
// Route to specific terminal model if it exists
|
|
final model = _terminalModels[terminalId];
|
|
if (model != null) {
|
|
model.handleTerminalResponse(evt);
|
|
}
|
|
}
|
|
}
|
|
|
|
const kInvalidResolutionValue = -1;
|
|
const kVirtualDisplayResolutionValue = 0;
|
|
|
|
class Display {
|
|
double x = 0;
|
|
double y = 0;
|
|
int width = 0;
|
|
int height = 0;
|
|
bool cursorEmbedded = false;
|
|
int originalWidth = kInvalidResolutionValue;
|
|
int originalHeight = kInvalidResolutionValue;
|
|
double _scale = 1.0;
|
|
double get scale => _scale > 1.0 ? _scale : 1.0;
|
|
|
|
Display() {
|
|
width = (isDesktop || isWebDesktop)
|
|
? kDesktopDefaultDisplayWidth
|
|
: kMobileDefaultDisplayWidth;
|
|
height = (isDesktop || isWebDesktop)
|
|
? kDesktopDefaultDisplayHeight
|
|
: kMobileDefaultDisplayHeight;
|
|
}
|
|
|
|
@override
|
|
bool operator ==(Object other) =>
|
|
other is Display &&
|
|
other.runtimeType == runtimeType &&
|
|
_innerEqual(other);
|
|
|
|
bool _innerEqual(Display other) =>
|
|
other.x == x &&
|
|
other.y == y &&
|
|
other.width == width &&
|
|
other.height == height &&
|
|
other.cursorEmbedded == cursorEmbedded;
|
|
|
|
bool get isOriginalResolutionSet =>
|
|
originalWidth != kInvalidResolutionValue &&
|
|
originalHeight != kInvalidResolutionValue;
|
|
bool get isVirtualDisplayResolution =>
|
|
originalWidth == kVirtualDisplayResolutionValue &&
|
|
originalHeight == kVirtualDisplayResolutionValue;
|
|
bool get isOriginalResolution =>
|
|
width == (originalWidth * scale).round() &&
|
|
height == (originalHeight * scale).round();
|
|
}
|
|
|
|
class Resolution {
|
|
int width = 0;
|
|
int height = 0;
|
|
Resolution(this.width, this.height);
|
|
|
|
@override
|
|
String toString() {
|
|
return 'Resolution($width,$height)';
|
|
}
|
|
}
|
|
|
|
class Features {
|
|
bool privacyMode = false;
|
|
}
|
|
|
|
const kInvalidDisplayIndex = -1;
|
|
|
|
class PeerInfo with ChangeNotifier {
|
|
String version = '';
|
|
String username = '';
|
|
String hostname = '';
|
|
String platform = '';
|
|
bool sasEnabled = false;
|
|
bool isSupportMultiUiSession = false;
|
|
int currentDisplay = 0;
|
|
int primaryDisplay = kInvalidDisplayIndex;
|
|
RxList<Display> displays = <Display>[].obs;
|
|
Features features = Features();
|
|
List<Resolution> resolutions = [];
|
|
Map<String, dynamic> platformAdditions = {};
|
|
|
|
RxInt displaysCount = 0.obs;
|
|
RxBool isSet = false.obs;
|
|
|
|
bool get isWayland => platformAdditions[kPlatformAdditionsIsWayland] == true;
|
|
bool get isInstalled =>
|
|
platform != kPeerPlatformWindows ||
|
|
platformAdditions[kPlatformAdditionsIsInstalled] == true;
|
|
List<int> get RustDeskVirtualDisplays => List<int>.from(
|
|
platformAdditions[kPlatformAdditionsRustDeskVirtualDisplays] ?? []);
|
|
int get amyuniVirtualDisplayCount =>
|
|
platformAdditions[kPlatformAdditionsAmyuniVirtualDisplays] ?? 0;
|
|
|
|
bool get isSupportMultiDisplay =>
|
|
(isDesktop || isWebDesktop) && isSupportMultiUiSession;
|
|
bool get forceTextureRender => currentDisplay == kAllDisplayValue;
|
|
|
|
bool get cursorEmbedded => tryGetDisplay()?.cursorEmbedded ?? false;
|
|
|
|
bool get isRustDeskIdd =>
|
|
platformAdditions[kPlatformAdditionsIddImpl] == 'rustdesk_idd';
|
|
bool get isAmyuniIdd =>
|
|
platformAdditions[kPlatformAdditionsIddImpl] == 'amyuni_idd';
|
|
|
|
Display? tryGetDisplay({int? display}) {
|
|
if (displays.isEmpty) {
|
|
return null;
|
|
}
|
|
display ??= currentDisplay;
|
|
if (display == kAllDisplayValue) {
|
|
return displays[0];
|
|
} else {
|
|
if (display > 0 && display < displays.length) {
|
|
return displays[display];
|
|
} else {
|
|
return displays[0];
|
|
}
|
|
}
|
|
}
|
|
|
|
Display? tryGetDisplayIfNotAllDisplay({int? display}) {
|
|
if (displays.isEmpty) {
|
|
return null;
|
|
}
|
|
display ??= currentDisplay;
|
|
if (display == kAllDisplayValue) {
|
|
return null;
|
|
}
|
|
if (display >= 0 && display < displays.length) {
|
|
return displays[display];
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
List<Display> getCurDisplays() {
|
|
if (currentDisplay == kAllDisplayValue) {
|
|
return displays;
|
|
} else {
|
|
if (currentDisplay >= 0 && currentDisplay < displays.length) {
|
|
return [displays[currentDisplay]];
|
|
} else {
|
|
return [];
|
|
}
|
|
}
|
|
}
|
|
|
|
double scaleOfDisplay(int display) {
|
|
if (display >= 0 && display < displays.length) {
|
|
return displays[display].scale;
|
|
}
|
|
return 1.0;
|
|
}
|
|
|
|
Rect? getDisplayRect(int display) {
|
|
final d = tryGetDisplayIfNotAllDisplay(display: display);
|
|
if (d == null) return null;
|
|
return Rect.fromLTWH(d.x, d.y, d.width.toDouble(), d.height.toDouble());
|
|
}
|
|
}
|
|
|
|
const canvasKey = 'canvas';
|
|
|
|
Future<void> setCanvasConfig(
|
|
SessionID sessionId,
|
|
double xCursor,
|
|
double yCursor,
|
|
double xCanvas,
|
|
double yCanvas,
|
|
double scale,
|
|
int currentDisplay) async {
|
|
final p = <String, dynamic>{};
|
|
p['xCursor'] = xCursor;
|
|
p['yCursor'] = yCursor;
|
|
p['xCanvas'] = xCanvas;
|
|
p['yCanvas'] = yCanvas;
|
|
p['scale'] = scale;
|
|
p['currentDisplay'] = currentDisplay;
|
|
await bind.sessionSetFlutterOption(
|
|
sessionId: sessionId, k: canvasKey, v: jsonEncode(p));
|
|
}
|
|
|
|
Future<Map<String, dynamic>?> getCanvasConfig(SessionID sessionId) async {
|
|
if (!isWebDesktop) return null;
|
|
var p =
|
|
await bind.sessionGetFlutterOption(sessionId: sessionId, k: canvasKey);
|
|
if (p == null || p.isEmpty) return null;
|
|
try {
|
|
Map<String, dynamic> m = json.decode(p);
|
|
return m;
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
Future<void> initializeCursorAndCanvas(FFI ffi) async {
|
|
var p = await getCanvasConfig(ffi.sessionId);
|
|
int currentDisplay = 0;
|
|
if (p != null) {
|
|
currentDisplay = p['currentDisplay'];
|
|
}
|
|
if (p == null || currentDisplay != ffi.ffiModel.pi.currentDisplay) {
|
|
ffi.cursorModel.updateDisplayOrigin(
|
|
ffi.ffiModel.rect?.left ?? 0, ffi.ffiModel.rect?.top ?? 0);
|
|
return;
|
|
}
|
|
double xCursor = p['xCursor'];
|
|
double yCursor = p['yCursor'];
|
|
double xCanvas = p['xCanvas'];
|
|
double yCanvas = p['yCanvas'];
|
|
double scale = p['scale'];
|
|
ffi.cursorModel.updateDisplayOriginWithCursor(ffi.ffiModel.rect?.left ?? 0,
|
|
ffi.ffiModel.rect?.top ?? 0, xCursor, yCursor);
|
|
ffi.canvasModel.update(xCanvas, yCanvas, scale);
|
|
}
|
|
|
|
clearWaitingForImage(OverlayDialogManager? dialogManager, SessionID sessionId) {
|
|
dialogManager?.dismissByTag('$sessionId-waiting-for-image');
|
|
}
|