Files
rustdesk/src/flutter.rs
T
RustDeskandClaude Opus 5.5 e9ddbd8f46 cursor: bound the decoded cursor caches on the Flutter client (#16304)
* cursor: name a shape by what it looks like, not by its handle

A cursor id was the platform's handle for the shape: HCURSOR on
Windows, the XFixes serial on X11. Apps mint a new handle for a shape
they have shown before (Chrome, Electron, Qt custom cursors), and an
X11 animated cursor gets one per frame, so one arrow arrived under
thousands of ids over a session. The controlled side cached and sent
it again under each, and the controller decoded, rendered and kept it
again under each, and grew by gigabytes over weeks of connection.

The controlled side now names a shape by an xxh3 hash of its size,
hotspot and pixels, taken once when a handle is first seen, before
the pixels are compressed. The per-connection send already sends a
shape once and afterwards only its id, so a shape now crosses once
however many handles it has. The id is opaque in the protocol, so
controllers of any version accept it. The DRM backend already named
shapes by content with FNV-1a; it uses the same hash now.

A peer before 1.5.0 still names shapes by handle, and the fix has to
work against every deployed peer. For those peers the controller
hashes each CursorData's compressed colors with its geometry: one
peer compresses the same pixels to the same bytes, so a shape seen
before is recognised without being decompressed. The UI gets that
shape once, under one id, and every later handle for it is passed on
as a cursor_id of that id. The map from the peer's ids is kept for the
connection, unbounded, since the peer sends a shape once and may
select any id it named again; an entry is two integers. From 1.5.0
the controller passes the peer's ids through unchanged, on the old
path.

The web core in flutter/web does the same in its own tree.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: hold a content id to what the web client can read

The web client decodes a u64 into a JS number and throws on a value
past 2^53, which drops the whole message. A full 64-bit xxh3 id is
almost always past it, so a web client connected to a peer on this
build would never have been given a cursor shape. DRM's ids were
already full width, so web clients lost the cursor on DRM peers too;
the controlled side now renames those as well.

The id keeps the hash's low 53 bits. Two of ten thousand distinct
shapes then collide with a chance of about one in two hundred million.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: hand the UI a shape's pixels as they are, not as text

A shape's pixels went to the Flutter UI as a JSON array of integers
inside the cursor_data event: the core serialised a 1 MiB shape into
up to 4 MiB of text on the receive loop, and the UI parsed that into a
million-element list and copied it into bytes, on the UI thread. The
event, text and all, was then kept for the session so that a tab
moved to another window could replay it, and a reconnect, on which the
peer sends every shape again, appended another copy of each.

The core now sends a shape as an EventToUI::Cursor variant carrying
Vec<u8>, which flutter_rust_bridge hands to Dart as a Uint8List, on
the same per-session stream as the events so that it keeps its order
with the cursor_id events around it. The web core calls onCursorData
with the Uint8Array, as it calls onRgba for a frame. The id stays
text, since the peer's ids can exceed Dart's int.

The replay keeps each shape's pixels once, by id, and encodes them as
base64 only when a tab moves. A shape arriving is also recorded as the
last one selected, since the replay goes in first-seen order; before,
a tab moved right after a new shape came back on the previous one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: let a controller ask the peer for a shape again

A peer sends each shape once per connection and afterwards only its
id, so a controller must keep every shape it was sent for as long as
it is connected. The controlled side now answers
Misc::request_cursor_data with the CursorData of that id, from the
shapes it has sent, and only to a connection the cursor service
would send it to. A controller from 1.5.0 on can then bound what it
keeps and ask again for a shape it let go. None asks yet: the peer has
to have the answer before a controller can rely on it, and peers are
updated last.

The shapes are kept by content id, and every handle for a shape
shares its one message, where each handle used to keep its own copy.
They are dropped with the service's own cache.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: keep a shape as its ui.Image and nothing else

Each shape the UI had been sent stayed in four copies for the session:
the pixels for a tab move, an image package copy to resize from (on a
macOS or Linux controller of a Windows or Linux peer, a second one
decoded back from a PNG), the bytes handed to the native cursor, and
the ui.Image painted when the peer moves the pointer.

A shape now keeps only its ui.Image. The resize source and the native
bytes exist while a native cursor is being registered and are dropped
once it is; a shape shown again at a raster it was registered at needs
neither. For a raster it lacks, the pixels are read back from the
ui.Image, which returns them exactly as they came. A tab move reads
every shape back the same way, and the moved window decodes them as
it would have on arrival.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: pin that a released shape keeps neither its pixels nor its bytes

The shape in use once registered, the shapes not in use, and the
pixels read back for a moved tab are all dropped; the tests now say so,
so that a later change keeping one of them fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: keep one native cursor per shape, and each tab its own

A shape got a native cursor for every raster it was shown at, and all
of them stayed until the session ended, so every zoom or DPR change
added a native cursor for each shape in use. A shape now keeps the one
at its current raster; the one it replaces is deleted the next time a
cursor already registered is built, by when its successor has been
activated. A raster returned to before that takes its cursor back
instead of registering another under the same name.

The native cursors of a window's engine are shared by its tabs, and
the predefined cursors had one name in all of them, so a tab closing
deleted the default and forbidden cursors the other tabs still showed.
Names are now scoped to the tab's cursor model.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: keep the shapes compressed in the core, and only the one in use in the UI

The UI kept every shape as a ui.Image, one raw copy each, for as long
as the session lasted, because the peer sends a shape once and a
shape might have to be drawn again. The core receives the shapes
compressed, at a few hundredths of that, and now keeps them so:
Session::cursor_shapes holds each shape that decoded, as the peer sent
it, under the id the UI knows it by. The colors are copied, since the
message's may be a slice of a larger received buffer.

The UI keeps a ui.Image for the shape in use alone. A shape shown
again at a raster its native cursor has needs no pixels at all; for a
raster it lacks, for painting it, or for the window a tab moved to,
the UI asks the core through session_get_cursor_shape, which
decompresses and checks the shape again with decode_cursor_data, the
same size and decompression limits as on arrival. A shape the core
cannot give is not asked for again until the peer sends it. A tab
move now carries only the id in use, and nothing is read back from
the GPU.

The web core keeps the shapes the same way and gives them back through
getCursorShape.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: a decode that finishes after the session clears keeps nothing

A shape asked of the core while a tab closed was decoded after the
cursor model had been cleared and stored in it, where nothing disposed
of its ui.Image again; a shape whose cache failed to build left its
ui.Image undisposed as well. A clear now starts a new generation, and a
fetch or decode from an earlier one disposes what it made. A shape the
core gave but that did not decode is not asked for again on every
frame it is painted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: keep a native cursor for the shape in use alone

Every shape the peer showed kept a native cursor for the session, each
a raster the size of the shape as displayed. A shape switched away
from now gives its native cursor up the way a replaced raster does:
deleted the next time a cursor already registered is built, taken back
if the shape returns first. A shape shown again after that is rebuilt
from the compressed copy the core keeps. The predefined cursors are
not the peer's shapes and stay registered.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: keep the native cursors of the 16 shapes used last

Keeping a native cursor for the shape in use alone rebuilt one on
nearly every switch, since the pointer moves among a few shapes: the
arrow, text, hand, four resize arrows, wait. The native cursors of the
peer's shapes are now kept in order of use, 16 of them, and the one
used longest ago gives its cursor up the way a replaced raster does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: forget the native cursors a session clear deletes

The clear deleted every registered native cursor but kept their names,
so a cursor model used again, as the mobile client's is, took each
name for registered and showed a cursor that no longer existed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: hold a native cursor to 512 pixels a side

A native cursor could be registered up to 1024 pixels a side, four
MiB of pixels each, which with sixteen kept made 64 MiB at worst. The
largest cursor a system shows is 256 pixels (Windows' largest pointer
size), so a raster twice that is shown no larger: the cursor is drawn
smaller rather than rejected, and sixteen kept come to 16 MiB at most.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: a shape switched away from while it decodes is not marked lost

A shape asked of the core was taken for undecodable when it was no
longer kept once its decode finished, but a shape the peer switched
away from in the meantime is let go on purpose, and marking it made
every later request for it be skipped: its native cursor stayed
deferred and its painted cursor missing for the rest of the session.
The decode now reports whether it decoded, and only a failure marks
the shape.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* flutter: leave two untouched signatures as they were

dart format had rewrapped registerEventHandler and sessionSetCommon
next to the cursor additions.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: read a shape's pixels from where its view starts

The pixels used to be copied out of the JSON event into a fresh list,
so the list's buffer began at its first pixel. Now they come straight
from the core, native or web, as whatever Uint8List it hands over, and
img2's fromBytes takes the buffer, not the list, so a view into a
larger buffer would have been read from the buffer's start. Neither
core gives a view today; this stops depending on that.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: a DRM cache reset keeps the shape it was making room for

The shape being sent was filed in CURSOR_SHAPES before the DRM ceiling
cleared it with everything else, so a controller asking for the shape
on screen got nothing until the next new shape.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: delete a replaced native cursor at the next build that shows one

A replaced native cursor was deleted only by a build of a cursor
already registered, so a run of new shapes, each shown once, left
every replaced cursor registered: 16 on the books, all of them in the
system. Now every build that shows a cursor deletes the ones replaced
before it, right before it registers or takes one back; what replaced
them was activated in an earlier frame, so they are off screen. A
build that has to ask for pixels shows nothing new and deletes
nothing, so a raster returned to right after being replaced still
takes its cursor back; one returned to later registers again.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: a shape that does not decode leaves no handle and no copy

CursorDedupe named a handle as it hashed the shape, before the shape
was checked, so a peer sending shapes that fail the checks under new
handles grew the map with nothing the UI could ever select, and such
a shape took over the handle of one that had decoded. The handle is
now named once its shape decoded, or was shown before.

The flutter build also copied a shape's compressed colors out of the
message before the checks, so a message with a small size and huge
colors was copied whole before being rejected. The copy is made once
the shape decoded; until then the message's own bytes are held.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: remember the shape the core lacks only while it is in use

Every id the core could not give was kept in a set for the session,
so a peer sending cursor_id after cursor_id of shapes it never sent
grew it by a string each, at a few bytes of traffic per entry. The
mark exists so that painting does not ask for the shape in use on
every frame, and the shape in use is one, so one mark is kept: the
peer selecting another shape forgets it, and selecting a lost shape
again asks once more.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: forget the shape the core lacks once the peer selects another

The mark was replaced only by another shape the core lacked, so a
lost shape selected again after a known one was still not asked for.
Selecting any other shape now clears it.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: leave the DRM cursor's own id as it was

The wire id is cursor_content_id whatever the platform, set in
run_cursor over the shape's pixels, so the id the DRM reader derives
for itself only tells one hardware cursor from the next and keys the
service's cache. Which hash it uses does not reach a controller; the
change to xxh3 and the dependency it brought to scrap go.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: say the 1.5.0 gate means the release

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: say why the peer answers request_cursor_data with what it holds

No controller sends it yet, and a review asked for it to go because a
cache reset on the peer could leave a request unanswered. A shape the
peer's cache dropped is rebuilt, and indexed again, the next time it
is shown, before any connection names it by id, so the shape a
controller has just been told to show is always there to give; that
is the contract a future bounded controller relies on.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: drop the tests nothing can fail

a_handle_is_named_once_its_shape_is_shown dates from when `name`
wrote the handle map. `name` is pure now, so its last assertion
cannot fail, and the others repeat two neighbours.
every_handle_the_peer_named_stays_for_the_connection fails only for
a bound added on purpose, which the struct's doc rules out. The Dart
arrival test is covered by the first test of its file.

The kept-shape test now asserts what `cursor_shape` is for, a copy
rather than a view of the received buffer, in place of the unknown
fields it drops. The content-id test moves out from between two
`use` lines.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: let the kept-shape tests run without the flutter feature

CI's cargo test builds the default features, and the two functions
under test are pure, so they compile for every test build and the
module loses its feature gate.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: keep native cursors for every frame of an animated cursor

An animated cursor is a shape per frame, 18 for the Windows busy cursor
and 23 for KDE's wait cursor. With 16 native cursors a cycle longer than
that missed on every frame: fetched from the core, decoded, registered
again and the replaced cursor deleted, about thirty times a second for
as long as the peer was busy. 64 holds two such animations and the
everyday set besides.

The tests settle in 10 ms instead of 100 ms, so the ones that fill the
limit stay quick.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: release only the shapes that can hold pixels

Selecting a cursor swept every shape of the session to release pixels,
though only two can hold any: the shape selected before, and a shape
that finished decoding after the peer moved on. Release those, so a
cursor_id costs the same however many shapes the peer has shown.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: show the native cursor limit holds two animations and the everyday set

64 is a working set, not the longest animation: KDE's wait and progress
cursors are 23 frames each, and a session that meets both plus a dozen
static shapes must keep them all, or every turn rebuilds.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: let the shape in use keep its pixels for a new raster

Dropping them once the native cursor was registered meant every scale
change, zooming with the cursor zoomed or crossing to a display of
another scale, showed the system arrow while the core gave the shape
back and it decoded again. The shape in use now keeps them and makes the
new raster at once; a shape switched away from still keeps none.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: never fall back to the default cursor on a switch

A switch to a shape without its image, or without a native cursor at
its raster, showed the default arrow until the core gave the shape back
and it decoded: the painted cursor on every switch back, the native one
after an eviction or while a new shape decoded.

- The shape shown before stays, image, hotspot and native cursor, until
  the one in use is ready.
- Where the cursor is painted, on mobile or with the remote cursor
  shown, the images of the 64 shapes used last are kept, like the native
  cursors, so an animation does not decode a frame per turn. Elsewhere
  only the shape in use keeps one.
- A shape that finishes decoding after the peer moved on is kept with
  them instead of dropped, so a fast animation fills them at all.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: name the dedupe's content-id set for what it holds

`shown` read as the shape on screen. It is the shapes decoded and given
to the UI, keyed by content id, so a shape arriving under a new handle
is looked up once instead of scanning the handle map: `decoded`,
`has_decoded`, and `record` for the call that fills both maps. The web
mirror is renamed the same way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: keep native cursors for the session, as before

Evicting native cursors and deleting a shape's replaced raster made
Windows leak more than master. The engine's deleteCustomCursor/windows
frees the HCURSOR with DeleteObject, which does not take a cursor, so a
delete frees nothing and each cursor made again for a raster, or for a
shape shown once more, leaks another handle. Keeping every raster of
every shape until the session is cleared creates each one once, as
master does.

Fixing the engine instead is not cheap: x64 runs our fork on Flutter
3.24, where the fix is one more patch to rebuild and carry across every
upgrade; arm64 runs the stock engine of a newer Flutter with the same
code, where it means porting the fork and publishing an arm64 engine
too, or waiting for an upstream fix to reach stable. Content ids keep
the count to the shapes the peer really shows.

The painted images, the compressed shapes in the core, and a switch
showing the cursor shown before are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: record the Windows measurement behind keeping native cursors

A measurement on Windows confirmed what the engine source implied: of
500 cursors made by CreateIconIndirect, DeleteObject freed none and
left 500 USER objects alive, while DestroyCursor and DestroyIcon freed
all 500. The comment now says so, that native cursors have no LRU for
that reason only, and that one can come back once both engines destroy
cursors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: look a native cursor up by the raster asked for

The key ended in the raster made last. A shape switched away from keeps
no pixels, so its raster stays where it was, and going back to a raster
it was shown at before looked up the wrong key: A at 1.0, A at 0.5, then
B, then A at 1.0 again missed the 32x32 cursor that was still there,
fetched A from the core, and showed B meanwhile. With native cursors
kept for every raster, the key now ends in the raster asked for; with
pixels, that is the raster made, as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: let a shape keep its pixels until a native cursor holds them

A shape switched away from, or decoded after the peer moved on, let its
pixels go whether or not it had a native cursor, and a native cursor is
made only for the shape in use. A shape whose restore kept finishing
after the next switch, as an animation's frames can, was fetched and
decoded again every time it came back and never got a native cursor.

A shape without one now keeps its pixels until one is made, at most
kRecentShapes of them, the one waiting longest let go first. Mobile
only paints the cursor, so it lets them go as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* proto: keep cursor ids as strings in JS

A Windows peer before 1.5.0 names a cursor by its handle, sign-extended
above 2^53 when its top bit is set. The web client decodes u64 fields
into JS numbers, and ts-proto throws on a value a number cannot hold,
so each such cursor_data or cursor_id was dropped whole and the web
cursor stayed on the shape before. [jstype = JS_STRING] makes ts-proto
generate these two fields as strings; the wire format, the Rust code
generated and every other client are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: wait for a native cursor at the raster asked for, not any raster

A shape let its pixels go once it had a native cursor at some raster.
After a zoom, or on a display of another scale, an animation's frames
had native cursors at the old raster only; each was fetched again at
the new one, and a restore that finished after the next switch let its
pixels go at once, so the frames never got a native cursor at the new
raster and the cursor stayed on the one shown before. A lookup that
finds no native cursor at the raster asked for now counts the shape as
waiting for one, so a late restore keeps its pixels until it has one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: count a shape held again when its asked raster has a native cursor

A lookup that missed took the shape out of _nativeIds, but one that hit
did not put it back. A shape asked at a new raster, then at one it has a
native cursor for, still counted as waiting: a restore finishing after
the switch away kept its pixels in _awaitingNative instead of letting
them go. The set now says whether the raster asked last has a native
cursor, either way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* Revert "cursor: count a shape held again when its asked raster has a native cursor"

This reverts commit 7cefbfc7a.

_nativeIds is meant as "no raster of this shape is still missing its
native cursor", not "the raster asked last has one". A shape asked at a
new raster whose restore is on its way, then shown at a raster it has a
cursor for, still waits for the new one; counting it held let the late
restore's pixels go, so going back to the new raster fetched it again,
and with an animation's frames and a scale that keeps changing, the
loop 1e4388771 fixed could come back. Keeping the pixels costs at most
a waiting slot, within the 64. A test now locks this: a raster still
missing keeps a late restore whatever was shown meanwhile.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: reuse a shape sent before without compressing it again

A handle not seen before sent the service through the whole path:
capture the pixels, name them by content, compress them, and only then
find the shape already in CURSOR_SHAPES and drop what it compressed. An
app that mints a new handle for the same cursor, as Chrome and Electron
do, paid a compress per handle, a few ms for an enlarged cursor. The
content id is now looked up first, and only a new shape is compressed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: bound the handles the service files, and the shapes with them

A platform may mint a new handle each time it shows a shape, as Chrome
and Electron do, so cached_cursor_data grew by an entry per handle for
the life of the service, even when every one named the same shape; and
the compressed shapes in CURSOR_SHAPES were bounded by nothing but the
handles. Past 4096 handles, or 32 MiB of compressed shapes however few
the handles, both start over together, keeping the shape being sent: a
handle shown again is captured and named again. On every platform; the
DRM build's own 64-entry ceiling is unchanged and comes first there.

CURSOR_SHAPES keeps the bytes it holds beside the shapes, cleared with
them, so every path that clears it resets the count.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: let the macOS seed report a change, and the content name the cursor

macOS named a cursor by a fingerprint of its size, hotspot and two
pixels near the hotspot. It collides between standard cursors: the open
and the closed hand give one value, and the arrows with a badge (copy,
not allowed, contextual menu, disappearing item) another, so a switch
between them was never sent, or a cached shape of the other was.
CGSCurrentCursorSeed already says when the cursor changed; get_cursor
now reports the seed, and the shape is named by its content, as on
every platform. The fingerprint is gone.

get_cursor_data checks the seed before and after the capture. The seed
was taken before capturing, so a capture that found the cursor changed
again lost the change; it now leaves it for the next poll.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: file no macOS seeds, and send nothing for the shape already shown

A macOS seed marks a change and never comes back, so filing it as a
handle only filled the map; the byte ceiling still bounds the shapes
there. A new handle or seed for the shape already shown, which a seed
change without a new shape or an app minting handles gives, sent every
connection a cursor_id for what it already showed; a shape is one
message however many handles name it, so the same message is not sent
again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: capture the macOS cursor shown without checking the seed around it

Every macOS seed change is now captured, pixel by pixel, and a seed
that moved during that capture failed it; the service then logged an
error and slept a second, so the controller's cursor lagged while it
changed. The check guarded nothing: the shape is named by its content,
not the seed, and a change after get_cursor read the seed moves it on,
so the next poll captures it anyway. Resetting the seed on a failed
check was not needed for the same reason.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: fall back to the default cursor for a shape the core cannot give

The shape shown before stays while the one in use decodes, so a switch
does not flash the default cursor. It also stayed once the shape in use
was known to be lost (rejected by the core, or not decoding): the
controller kept drawing, say, an I-beam for a pointer it never got,
until the peer changed shape. Both the native and the painted cursor
now leave the default one, as master did for the native cursor. A shape
that does not decode is marked in updateCursorData, whether it was sent
or fetched again, so both paths agree.

The client also selects a shape it rejects as it arrives. It showed the
shape before until the peer selected the rejected one again, and the
default cursor from then on; it now looks the same both times. Sciter
ignores an id it has no shape for, as it did.

The default and forbidden cursors are made with the model. They were
made the first time a build asked for one, and their decode lands
later with nothing to draw again, so that first build showed the shape
before in their place.

A fetch failing after the session was cleared marks nothing in the new
one, and a fetch that throws at once tells the listeners after the
build that asked for it, not during it.

The forbidden cursor, shown while input is off, became the cursor shown
last, so a raster made after input came back showed it until the
shape's pixels were fetched, or for good if they never came. Only a
shape stands in for a shape now; CursorModel.shown keeps the rule for
both builders.

_checkView asks for a single registration again: every native cursor
is made once per session, and one made twice leaks an HCURSOR on
Windows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: name a handle by the shape rejected under it, not the one before

A shape the client rejects is selected as it arrives, so that it shows
the default cursor then as it does later. Under a handle reused for it,
the dedupe still named the shape the handle had brought before, and
selecting that switched the pointer to an old shape: A under handle 7,
then B under 8, then a rejected C under 7 showed A in place of B, and
again whenever the peer selected 7.

The handle now names the rejected content, which is never marked
decoded, since only a shape not decoded before reaches the decode: the
UI has no shape under it and shows the default cursor, then and when
the handle is selected again. The shape the handle named before stays
kept for the other handles naming it, and a handle that brings it again
names it again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: file no handle for a shape that is rejected under it

A shape rejected under a handle named the handle by the rejected
content, so that a known handle does not show the shape it brought
before. A handle never seen was filed the same way, so a peer sending
shapes that do not decode under new handles grew the map with each,
where a rejected shape used to leave nothing. Only a known handle is
renamed now; one never seen is not filed and is passed on as unknown,
which the UI shows as the default cursor.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

* cursor: say what Future.sync defers, and give the default cursor time on a slow runner

The comment on the fetch in restorePixels read as if the fetch were put
off; it runs at once, and Future.sync only turns its throw into an error
the await hands over later, which is what keeps the notify out of the
build.

cursor_default_test gave the default cursor 100 ms to decode, which a
slow runner may miss. It cannot wait for the cursor itself, since asking
for it would make it; it now waits 500 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 12:09:46 +08:00

2394 lines
80 KiB
Rust

use crate::{
client::*,
flutter_ffi::{EventToUI, SessionID},
ui_session_interface::{io_loop, InvokeUiSession, Session},
};
use flutter_rust_bridge::StreamSink;
#[cfg(not(any(target_os = "android", target_os = "ios")))]
use hbb_common::dlopen::{
symbor::{Library, Symbol},
Error as LibError,
};
use hbb_common::{
anyhow::anyhow, bail, config::LocalConfig, get_version_number, log,
rendezvous_proto::ConnType, ResultType,
};
use base::message_proto::*;
use serde::Serialize;
use serde_json::json;
#[cfg(target_os = "windows")]
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
use std::{
collections::{HashMap, HashSet},
ffi::CString,
os::raw::{c_char, c_int, c_void},
str::FromStr,
sync::{
atomic::{AtomicBool, AtomicUsize, Ordering},
Arc, RwLock,
},
};
/// tag "main" for [Desktop Main Page] and [Mobile (Client and Server)] (the mobile don't need multiple windows, only one global event stream is needed)
/// tag "cm" only for [Desktop CM Page]
pub(crate) const APP_TYPE_MAIN: &str = "main";
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub(crate) const APP_TYPE_CM: &str = "cm";
#[cfg(any(target_os = "android", target_os = "ios"))]
pub(crate) const APP_TYPE_CM: &str = "main";
// Do not remove the following constants.
// Uncomment them when they are used.
// pub(crate) const APP_TYPE_DESKTOP_REMOTE: &str = "remote";
// pub(crate) const APP_TYPE_DESKTOP_FILE_TRANSFER: &str = "file transfer";
// pub(crate) const APP_TYPE_DESKTOP_PORT_FORWARD: &str = "port forward";
pub type FlutterSession = Arc<Session<FlutterHandler>>;
lazy_static::lazy_static! {
pub(crate) static ref CUR_SESSION_ID: RwLock<SessionID> = Default::default(); // For desktop only
static ref GLOBAL_EVENT_STREAM: RwLock<HashMap<String, StreamSink<String>>> = Default::default(); // rust to dart event channel
}
#[cfg(target_os = "windows")]
lazy_static::lazy_static! {
pub static ref TEXTURE_RGBA_RENDERER_PLUGIN: Result<Library, LibError> = load_plugin_in_app_path("texture_rgba_renderer_plugin.dll");
}
#[cfg(target_os = "linux")]
lazy_static::lazy_static! {
pub static ref TEXTURE_RGBA_RENDERER_PLUGIN: Result<Library, LibError> = Library::open("libtexture_rgba_renderer_plugin.so");
}
#[cfg(target_os = "macos")]
lazy_static::lazy_static! {
pub static ref TEXTURE_RGBA_RENDERER_PLUGIN: Result<Library, LibError> = Library::open_self();
}
#[cfg(target_os = "windows")]
lazy_static::lazy_static! {
pub static ref TEXTURE_GPU_RENDERER_PLUGIN: Result<Library, LibError> = load_plugin_in_app_path("flutter_gpu_texture_renderer_plugin.dll");
}
// Move this function into `src/platform/windows.rs` if there're more calls to load plugins.
// Load dll with full path.
#[cfg(target_os = "windows")]
fn load_plugin_in_app_path(dll_name: &str) -> Result<Library, LibError> {
match std::env::current_exe() {
Ok(exe_file) => {
if let Some(cur_dir) = exe_file.parent() {
let full_path = cur_dir.join(dll_name);
if !full_path.exists() {
Err(LibError::OpeningLibraryError(IoError::new(
IoErrorKind::NotFound,
format!("{} not found", dll_name),
)))
} else {
Library::open(full_path)
}
} else {
Err(LibError::OpeningLibraryError(IoError::new(
IoErrorKind::Other,
format!(
"Invalid exe parent for {}",
exe_file.to_string_lossy().as_ref()
),
)))
}
}
Err(e) => Err(LibError::OpeningLibraryError(e)),
}
}
/// FFI for rustdesk core's main entry.
/// Return true if the app should continue running with UI(possibly Flutter), false if the app should exit.
#[cfg(not(windows))]
#[no_mangle]
pub extern "C" fn rustdesk_core_main() -> bool {
#[cfg(any(target_os = "linux", target_os = "macos"))]
{
use hbb_common::libc;
// Native runners bypass Rust's startup, which normally ignores SIGPIPE.
if unsafe { libc::signal(libc::SIGPIPE, libc::SIG_IGN) } == libc::SIG_ERR {
eprintln!("Failed to ignore SIGPIPE: {}", std::io::Error::last_os_error());
std::process::exit(1);
}
}
#[cfg(not(any(target_os = "android", target_os = "ios")))]
if crate::core_main::core_main().is_some() {
return true;
} else {
#[cfg(target_os = "macos")]
std::process::exit(0);
}
#[cfg(not(target_os = "macos"))]
false
}
#[cfg(target_os = "macos")]
#[no_mangle]
pub extern "C" fn handle_applicationShouldOpenUntitledFile() {
crate::platform::macos::handle_application_should_open_untitled_file();
}
#[cfg(windows)]
#[no_mangle]
pub extern "C" fn rustdesk_core_main_args(args_len: *mut c_int) -> *mut *mut c_char {
unsafe { std::ptr::write(args_len, 0) };
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
if let Some(args) = crate::core_main::core_main() {
return rust_args_to_c_args(args, args_len);
}
return std::ptr::null_mut() as _;
}
#[cfg(any(target_os = "android", target_os = "ios"))]
return std::ptr::null_mut() as _;
}
#[cfg(windows)]
#[no_mangle]
pub extern "C" fn rustdesk_is_disable_installation() -> c_int {
hbb_common::config::is_disable_installation() as c_int
}
// https://gist.github.com/iskakaushik/1c5b8aa75c77479c33c4320913eebef6
#[cfg(windows)]
fn rust_args_to_c_args(args: Vec<String>, outlen: *mut c_int) -> *mut *mut c_char {
let mut v = vec![];
// Let's fill a vector with null-terminated strings
for s in args {
match CString::new(s) {
Ok(s) => v.push(s),
Err(_) => return std::ptr::null_mut() as _,
}
}
// Turning each null-terminated string into a pointer.
// `into_raw` takes ownershop, gives us the pointer and does NOT drop the data.
let mut out = v.into_iter().map(|s| s.into_raw()).collect::<Vec<_>>();
// Make sure we're not wasting space.
out.shrink_to_fit();
debug_assert!(out.len() == out.capacity());
// Get the pointer to our vector.
let len = out.len();
let ptr = out.as_mut_ptr();
std::mem::forget(out);
// Let's write back the length the caller can expect
unsafe { std::ptr::write(outlen, len as c_int) };
// Finally return the data
ptr
}
#[no_mangle]
pub unsafe extern "C" fn free_c_args(ptr: *mut *mut c_char, len: c_int) {
let len = len as usize;
// Get back our vector.
// Previously we shrank to fit, so capacity == length.
let v = Vec::from_raw_parts(ptr, len, len);
// Now drop one string at a time.
for elem in v {
let s = CString::from_raw(elem);
std::mem::drop(s);
}
// Afterwards the vector will be dropped and thus freed.
}
#[cfg(windows)]
#[no_mangle]
pub unsafe extern "C" fn get_rustdesk_app_name(buffer: *mut u16, length: i32) -> i32 {
let name = crate::platform::wide_string(&crate::get_app_name());
if length > name.len() as i32 {
std::ptr::copy_nonoverlapping(name.as_ptr(), buffer, name.len());
return 0;
}
-1
}
#[derive(Default)]
struct SessionHandler {
event_stream: Option<StreamSink<EventToUI>>,
// displays of current session.
// We need this variable to check if the display is in use before pushing rgba to flutter.
displays: Vec<usize>,
renderer: VideoRenderer,
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
enum RenderType {
PixelBuffer,
#[cfg(feature = "vram")]
Texture,
}
#[derive(Clone)]
pub struct FlutterHandler {
// ui session id -> display handler data
session_handlers: Arc<RwLock<HashMap<SessionID, SessionHandler>>>,
display_rgbas: Arc<RwLock<HashMap<usize, RgbaData>>>,
peer_info: Arc<RwLock<PeerInfo>>,
use_texture_render: Arc<AtomicBool>,
}
impl Default for FlutterHandler {
fn default() -> Self {
Self {
session_handlers: Default::default(),
display_rgbas: Default::default(),
peer_info: Default::default(),
use_texture_render: Arc::new(
AtomicBool::new(crate::ui_interface::use_texture_render()),
),
}
}
}
#[derive(Default, Clone)]
struct RgbaData {
// SAFETY: [rgba] is guarded by [rgba_valid], and it's safe to reach [rgba] with `rgba_valid == true`.
// We must check the `rgba_valid` before reading [rgba].
data: Vec<u8>,
valid: bool,
}
pub type FlutterRgbaRendererPluginOnRgba = unsafe extern "C" fn(
texture_rgba: *mut c_void,
buffer: *const u8,
len: c_int,
width: c_int,
height: c_int,
dst_rgba_stride: c_int,
);
#[cfg(feature = "vram")]
pub type FlutterGpuTextureRendererPluginCApiSetTexture =
unsafe extern "C" fn(output: *mut c_void, texture: *mut c_void);
#[cfg(feature = "vram")]
pub type FlutterGpuTextureRendererPluginCApiGetAdapterLuid = unsafe extern "C" fn() -> i64;
pub(super) type TextureRgbaPtr = usize;
struct DisplaySessionInfo {
// TextureRgba pointer in flutter native.
texture_rgba_ptr: TextureRgbaPtr,
size: (usize, usize),
#[cfg(feature = "vram")]
gpu_output_ptr: usize,
notify_render_type: Option<RenderType>,
}
// Video Texture Renderer in Flutter
#[derive(Clone)]
struct VideoRenderer {
is_support_multi_ui_session: bool,
map_display_sessions: Arc<RwLock<HashMap<usize, DisplaySessionInfo>>>,
#[cfg(not(any(target_os = "android", target_os = "ios")))]
on_rgba_func: Option<Symbol<'static, FlutterRgbaRendererPluginOnRgba>>,
#[cfg(feature = "vram")]
on_texture_func: Option<Symbol<'static, FlutterGpuTextureRendererPluginCApiSetTexture>>,
}
impl Default for VideoRenderer {
fn default() -> Self {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
let on_rgba_func = match &*TEXTURE_RGBA_RENDERER_PLUGIN {
Ok(lib) => {
let find_sym_res = unsafe {
lib.symbol::<FlutterRgbaRendererPluginOnRgba>("FlutterRgbaRendererPluginOnRgba")
};
match find_sym_res {
Ok(sym) => Some(sym),
Err(e) => {
log::error!("Failed to find symbol FlutterRgbaRendererPluginOnRgba, {e}");
None
}
}
}
Err(e) => {
log::error!("Failed to load texture rgba renderer plugin, {e}");
None
}
};
#[cfg(feature = "vram")]
let on_texture_func = match &*TEXTURE_GPU_RENDERER_PLUGIN {
Ok(lib) => {
let find_sym_res = unsafe {
lib.symbol::<FlutterGpuTextureRendererPluginCApiSetTexture>(
"FlutterGpuTextureRendererPluginCApiSetTexture",
)
};
match find_sym_res {
Ok(sym) => Some(sym),
Err(e) => {
log::error!("Failed to find symbol FlutterGpuTextureRendererPluginCApiSetTexture, {e}");
None
}
}
}
Err(e) => {
log::error!("Failed to load texture gpu renderer plugin, {e}");
None
}
};
Self {
map_display_sessions: Default::default(),
is_support_multi_ui_session: false,
#[cfg(not(any(target_os = "android", target_os = "ios")))]
on_rgba_func,
#[cfg(feature = "vram")]
on_texture_func,
}
}
}
impl VideoRenderer {
#[inline]
fn set_size(&mut self, display: usize, width: usize, height: usize) {
let mut sessions_lock = self.map_display_sessions.write().unwrap();
if let Some(info) = sessions_lock.get_mut(&display) {
info.size = (width, height);
info.notify_render_type = None;
} else {
sessions_lock.insert(
display,
DisplaySessionInfo {
texture_rgba_ptr: usize::default(),
size: (width, height),
#[cfg(feature = "vram")]
gpu_output_ptr: usize::default(),
notify_render_type: None,
},
);
}
}
fn register_pixelbuffer_texture(&self, display: usize, ptr: usize) {
let mut sessions_lock = self.map_display_sessions.write().unwrap();
if ptr == 0 {
if let Some(info) = sessions_lock.get_mut(&display) {
if info.texture_rgba_ptr != usize::default() {
info.texture_rgba_ptr = usize::default();
}
#[cfg(feature = "vram")]
if info.gpu_output_ptr != usize::default() {
return;
}
}
sessions_lock.remove(&display);
} else {
if let Some(info) = sessions_lock.get_mut(&display) {
if info.texture_rgba_ptr != usize::default()
&& info.texture_rgba_ptr != ptr as TextureRgbaPtr
{
log::warn!(
"texture_rgba_ptr is not null and not equal to ptr, replace {} to {}",
info.texture_rgba_ptr,
ptr
);
}
info.texture_rgba_ptr = ptr as _;
info.notify_render_type = None;
} else {
if ptr != 0 {
sessions_lock.insert(
display,
DisplaySessionInfo {
texture_rgba_ptr: ptr as _,
size: (0, 0),
#[cfg(feature = "vram")]
gpu_output_ptr: usize::default(),
notify_render_type: None,
},
);
}
}
}
}
#[cfg(feature = "vram")]
pub fn register_gpu_output(&self, display: usize, ptr: usize) {
let mut sessions_lock = self.map_display_sessions.write().unwrap();
if ptr == 0 {
if let Some(info) = sessions_lock.get_mut(&display) {
if info.gpu_output_ptr != usize::default() {
info.gpu_output_ptr = usize::default();
}
if info.texture_rgba_ptr != usize::default() {
return;
}
}
sessions_lock.remove(&display);
} else {
if let Some(info) = sessions_lock.get_mut(&display) {
if info.gpu_output_ptr != usize::default() && info.gpu_output_ptr != ptr {
log::error!(
"gpu_output_ptr is not null and not equal to ptr, relace {} to {}",
info.gpu_output_ptr,
ptr
);
}
info.gpu_output_ptr = ptr as _;
info.notify_render_type = None;
} else {
if ptr != usize::default() {
sessions_lock.insert(
display,
DisplaySessionInfo {
texture_rgba_ptr: usize::default(),
size: (0, 0),
gpu_output_ptr: ptr,
notify_render_type: None,
},
);
}
}
}
}
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn on_rgba(&self, display: usize, rgba: &scrap::ImageRgb) -> bool {
let mut write_lock = self.map_display_sessions.write().unwrap();
let opt_info = if !self.is_support_multi_ui_session {
write_lock.values_mut().next()
} else {
write_lock.get_mut(&display)
};
let Some(info) = opt_info else {
return false;
};
if info.texture_rgba_ptr == usize::default() {
return false;
}
if info.size.0 != rgba.w || info.size.1 != rgba.h {
log::error!(
"width/height mismatch: ({},{}) != ({},{})",
info.size.0,
info.size.1,
rgba.w,
rgba.h
);
// Peer info's handling is async and may be late than video frame's handling
// Allow peer info not set, but not allow wrong width/height for correct local cursor position
if info.size != (0, 0) {
return false;
}
}
if let Some(func) = &self.on_rgba_func {
unsafe {
func(
info.texture_rgba_ptr as _,
rgba.raw.as_ptr() as _,
rgba.raw.len() as _,
rgba.w as _,
rgba.h as _,
rgba.align() as _,
)
};
}
if info.notify_render_type != Some(RenderType::PixelBuffer) {
info.notify_render_type = Some(RenderType::PixelBuffer);
true
} else {
false
}
}
#[cfg(feature = "vram")]
pub fn on_texture(&self, display: usize, texture: *mut c_void) -> bool {
let mut write_lock = self.map_display_sessions.write().unwrap();
let opt_info = if !self.is_support_multi_ui_session {
write_lock.values_mut().next()
} else {
write_lock.get_mut(&display)
};
let Some(info) = opt_info else {
return false;
};
if info.gpu_output_ptr == usize::default() {
return false;
}
if let Some(func) = &self.on_texture_func {
unsafe { func(info.gpu_output_ptr as _, texture) };
}
if info.notify_render_type != Some(RenderType::Texture) {
info.notify_render_type = Some(RenderType::Texture);
true
} else {
false
}
}
pub fn reset_all_display_render_type(&self) {
let mut write_lock = self.map_display_sessions.write().unwrap();
write_lock
.values_mut()
.map(|v| v.notify_render_type = None)
.count();
}
}
impl SessionHandler {
pub fn on_waiting_for_image_dialog_show(&self) {
self.renderer.reset_all_display_render_type();
// rgba array render will notify every frame
}
}
impl FlutterHandler {
/// Push an event to all the event queues.
/// An event is stored as json in the event queues.
///
/// # Arguments
///
/// * `name` - The name of the event.
/// * `event` - Fields of the event content.
pub fn push_event<V>(&self, name: &str, event: &[(&str, V)], excludes: &[&SessionID])
where
V: Sized + Serialize + Clone,
{
self.push_event_(name, event, &[], excludes);
}
pub fn push_event_to<V>(&self, name: &str, event: &[(&str, V)], include: &[&SessionID])
where
V: Sized + Serialize + Clone,
{
self.push_event_(name, event, include, &[]);
}
pub fn push_event_<V>(
&self,
name: &str,
event: &[(&str, V)],
includes: &[&SessionID],
excludes: &[&SessionID],
) where
V: Sized + Serialize + Clone,
{
let mut h: HashMap<&str, serde_json::Value> =
event.iter().map(|(k, v)| (*k, json!(*v))).collect();
debug_assert!(h.get("name").is_none());
h.insert("name", json!(name));
let out = serde_json::ser::to_string(&h).unwrap_or("".to_owned());
for (sid, session) in self.session_handlers.read().unwrap().iter() {
let mut push = false;
if includes.is_empty() {
if !excludes.contains(&sid) {
push = true;
}
} else {
if includes.contains(&sid) {
push = true;
}
}
if push {
if let Some(stream) = &session.event_stream {
stream.add(EventToUI::Event(out.clone()));
}
}
}
}
pub(crate) fn close_event_stream(&self, session_id: SessionID) {
// to-do: Make sure the following logic is correct.
// No need to remove the display handler, because it will be removed when the connection is closed.
if let Some(session) = self.session_handlers.write().unwrap().get_mut(&session_id) {
try_send_close_event(&session.event_stream);
}
}
fn make_displays_msg(displays: &Vec<DisplayInfo>) -> String {
let mut msg_vec = Vec::new();
for ref d in displays.iter() {
let mut h: HashMap<&str, i32> = Default::default();
h.insert("x", d.x);
h.insert("y", d.y);
h.insert("width", d.width);
h.insert("height", d.height);
h.insert("cursor_embedded", if d.cursor_embedded { 1 } else { 0 });
if let Some(original_resolution) = d.original_resolution.as_ref() {
h.insert("original_width", original_resolution.width);
h.insert("original_height", original_resolution.height);
}
// Don't convert scale (x 100) to i32 directly.
// (d.scale * 100.0f64) as i32 may produces inaccuracies.
//
// Example: GNOME Wayland with Fractional Scaling enabled:
// - Physical resolution: 2560x1600
// - Logical resolution: 1074x1065
// - Scale factor: 150%
// Passing physical dimensions and scale factor prevents accurate logical resolution calculation
// since 2560/1.5 = 1706.666... (rounded to 1706.67) and 1600/1.5 = 1066.666... (rounded to 1066.67)
// h.insert("scale", (d.scale * 100.0f64) as i32);
// Send scaled_width for accurate logical scale calculation.
if d.scale > 0.0 {
let scaled_width = (d.width as f64 / d.scale).round() as i32;
h.insert("scaled_width", scaled_width);
}
msg_vec.push(h);
}
serde_json::ser::to_string(&msg_vec).unwrap_or("".to_owned())
}
pub fn update_use_texture_render(&self) {
self.use_texture_render
.store(crate::ui_interface::use_texture_render(), Ordering::Relaxed);
self.display_rgbas.write().unwrap().clear();
}
}
impl InvokeUiSession for FlutterHandler {
// On the event stream, so that it keeps its order with the cursor_id events around it.
fn set_cursor_data(&self, cd: CursorData) {
let colors = cd.colors.to_vec();
for session in self.session_handlers.read().unwrap().values() {
if let Some(stream) = &session.event_stream {
stream.add(EventToUI::Cursor {
id: cd.id.to_string(),
hotx: cd.hotx,
hoty: cd.hoty,
width: cd.width,
height: cd.height,
colors: colors.clone(),
});
}
}
}
fn set_cursor_id(&self, id: String) {
self.push_event("cursor_id", &[("id", &id.to_string())], &[]);
}
fn set_cursor_position(&self, cp: CursorPosition) {
self.push_event(
"cursor_position",
&[("x", &cp.x.to_string()), ("y", &cp.y.to_string())],
&[],
);
}
/// unused in flutter, use switch_display or set_peer_info
fn set_display(&self, _x: i32, _y: i32, _w: i32, _h: i32, _cursor_embedded: bool, _scale: f64) {}
fn update_privacy_mode(&self) {
self.push_event::<&str>("update_privacy_mode", &[], &[]);
}
fn set_permission(&self, name: &str, value: bool) {
self.push_event("permission", &[(name, &value.to_string())], &[]);
}
// unused in flutter
fn close_success(&self) {}
fn update_quality_status(&self, status: QualityStatus) {
const NULL: String = String::new();
self.push_event(
"update_quality_status",
&[
("speed", &status.speed.map_or(NULL, |it| it)),
(
"fps",
&serde_json::ser::to_string(&status.fps).unwrap_or(NULL.to_owned()),
),
("delay", &status.delay.map_or(NULL, |it| it.to_string())),
(
"target_bitrate",
&status.target_bitrate.map_or(NULL, |it| it.to_string()),
),
(
"codec_format",
&status.codec_format.map_or(NULL, |it| it.to_string()),
),
("chroma", &status.chroma.map_or(NULL, |it| it.to_string())),
],
&[],
);
}
fn set_connection_type(&self, is_secured: bool, direct: bool, stream_type: &str) {
self.push_event(
"connection_ready",
&[
("secure", &is_secured.to_string()),
("direct", &direct.to_string()),
("stream_type", &stream_type.to_string()),
],
&[],
);
}
fn set_fingerprint(&self, fingerprint: String) {
self.push_event("fingerprint", &[("fingerprint", &fingerprint)], &[]);
}
fn job_error(&self, id: i32, err: String, file_num: i32) {
self.push_event(
"job_error",
&[
("id", &id.to_string()),
("err", &err),
("file_num", &file_num.to_string()),
],
&[],
);
}
fn job_done(&self, id: i32, file_num: i32) {
self.push_event(
"job_done",
&[("id", &id.to_string()), ("file_num", &file_num.to_string())],
&[],
);
}
// unused in flutter
fn clear_all_jobs(&self) {}
fn load_last_job(&self, _cnt: i32, job_json: &str, _auto_start: bool) {
self.push_event("load_last_job", &[("value", job_json)], &[]);
}
fn update_folder_files(
&self,
id: i32,
entries: &Vec<FileEntry>,
path: String,
#[allow(unused_variables)] is_local: bool,
only_count: bool,
) {
// TODO opt
if only_count {
self.push_event(
"update_folder_files",
&[("info", &make_fd_flutter(id, entries, only_count))],
&[],
);
} else {
self.push_event(
"file_dir",
&[
("is_local", "false"),
("value", &crate::common::make_fd_to_json(id, path, entries)),
],
&[],
);
}
}
fn update_empty_dirs(&self, res: ReadEmptyDirsResponse) {
self.push_event(
"empty_dirs",
&[
("is_local", "false"),
(
"value",
&crate::common::make_empty_dirs_response_to_json(&res),
),
],
&[],
);
}
// unused in flutter
fn update_transfer_list(&self) {}
// unused in flutter // TEST flutter
fn confirm_delete_files(&self, _id: i32, _i: i32, _name: String) {}
fn override_file_confirm(
&self,
id: i32,
file_num: i32,
to: String,
is_upload: bool,
is_identical: bool,
) {
self.push_event(
"override_file_confirm",
&[
("id", &id.to_string()),
("file_num", &file_num.to_string()),
("read_path", &to),
("is_upload", &is_upload.to_string()),
("is_identical", &is_identical.to_string()),
],
&[],
);
}
fn job_progress(&self, id: i32, file_num: i32, speed: f64, finished_size: f64) {
self.push_event(
"job_progress",
&[
("id", &id.to_string()),
("file_num", &file_num.to_string()),
("speed", &speed.to_string()),
("finished_size", &finished_size.to_string()),
],
&[],
);
}
// unused in flutter
fn adapt_size(&self) {}
#[inline]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
fn on_rgba(&self, display: usize, rgba: &mut scrap::ImageRgb) {
let use_texture_render = self.use_texture_render.load(Ordering::Relaxed);
self.on_rgba_flutter_texture_render(use_texture_render, display, rgba);
if !use_texture_render {
self.on_rgba_soft_render(display, rgba);
}
}
#[inline]
#[cfg(any(target_os = "android", target_os = "ios"))]
fn on_rgba(&self, display: usize, rgba: &mut scrap::ImageRgb) {
self.on_rgba_soft_render(display, rgba);
}
#[inline]
#[cfg(feature = "vram")]
fn on_texture(&self, display: usize, texture: *mut c_void) {
if !self.use_texture_render.load(Ordering::Relaxed) {
return;
}
for (_, session) in self.session_handlers.read().unwrap().iter() {
if session.renderer.on_texture(display, texture) {
if let Some(stream) = &session.event_stream {
stream.add(EventToUI::Texture(display, true));
}
}
}
}
fn set_peer_info(&self, pi: &PeerInfo) {
let displays = Self::make_displays_msg(&pi.displays);
let mut features: HashMap<&str, bool> = Default::default();
for ref f in pi.features.iter() {
features.insert("privacy_mode", f.privacy_mode);
}
// compatible with 1.1.9
if get_version_number(&pi.version) < get_version_number("1.2.0") {
features.insert("privacy_mode", false);
}
let features = serde_json::ser::to_string(&features).unwrap_or("".to_owned());
let resolutions = serialize_resolutions(&pi.resolutions.resolutions);
*self.peer_info.write().unwrap() = pi.clone();
#[cfg(not(any(target_os = "android", target_os = "ios")))]
let is_support_multi_ui_session = crate::common::is_support_multi_ui_session(&pi.version);
#[cfg(any(target_os = "android", target_os = "ios"))]
let is_support_multi_ui_session = false;
self.session_handlers
.write()
.unwrap()
.values_mut()
.for_each(|h| {
h.renderer.is_support_multi_ui_session = is_support_multi_ui_session;
});
self.push_event(
"peer_info",
&[
("username", &pi.username),
("hostname", &pi.hostname),
("platform", &pi.platform),
("sas_enabled", &pi.sas_enabled.to_string()),
("displays", &displays),
("version", &pi.version),
("features", &features),
("current_display", &pi.current_display.to_string()),
("resolutions", &resolutions),
("platform_additions", &pi.platform_additions),
],
&[],
);
}
fn set_displays(&self, displays: &Vec<DisplayInfo>) {
self.peer_info.write().unwrap().displays = displays.clone();
self.push_event(
"sync_peer_info",
&[("displays", &Self::make_displays_msg(displays))],
&[],
);
}
fn set_platform_additions(&self, data: &str) {
self.push_event(
"sync_platform_additions",
&[("platform_additions", &data)],
&[],
)
}
fn set_multiple_windows_session(&self, sessions: Vec<WindowsSession>) {
let mut msg_vec = Vec::new();
let mut sessions = sessions;
for d in sessions.drain(..) {
let mut h: HashMap<&str, String> = Default::default();
h.insert("sid", d.sid.to_string());
h.insert("name", d.name);
msg_vec.push(h);
}
self.push_event(
"set_multiple_windows_session",
&[(
"windows_sessions",
&serde_json::ser::to_string(&msg_vec).unwrap_or("".to_owned()),
)],
&[],
);
}
fn is_multi_ui_session(&self) -> bool {
self.session_handlers.read().unwrap().len() > 1
}
fn set_current_display(&self, disp_idx: i32) {
if self.is_multi_ui_session() {
return;
}
self.push_event(
"follow_current_display",
&[("display_idx", &disp_idx.to_string())],
&[],
);
}
fn on_connected(&self, _conn_type: ConnType) {}
fn msgbox(&self, msgtype: &str, title: &str, text: &str, link: &str, retry: bool) {
let has_retry = if retry { "true" } else { "" };
self.push_event(
"msgbox",
&[
("type", msgtype),
("title", title),
("text", text),
("link", link),
("hasRetry", has_retry),
],
&[],
);
}
fn cancel_msgbox(&self, tag: &str) {
self.push_event("cancel_msgbox", &[("tag", tag)], &[]);
}
fn new_message(&self, msg: String) {
self.push_event("chat_client_mode", &[("text", &msg)], &[]);
}
fn switch_display(&self, display: &SwitchDisplay) {
let resolutions = serialize_resolutions(&display.resolutions.resolutions);
self.push_event(
"switch_display",
&[
("display", &display.display.to_string()),
("x", &display.x.to_string()),
("y", &display.y.to_string()),
("width", &display.width.to_string()),
("height", &display.height.to_string()),
(
"cursor_embedded",
&{
if display.cursor_embedded {
1
} else {
0
}
}
.to_string(),
),
("resolutions", &resolutions),
(
"original_width",
&display.original_resolution.width.to_string(),
),
(
"original_height",
&display.original_resolution.height.to_string(),
),
],
&[],
);
}
fn update_block_input_state(&self, on: bool) {
self.push_event(
"update_block_input_state",
&[("input_state", if on { "on" } else { "off" })],
&[],
);
}
#[cfg(any(target_os = "android", target_os = "ios"))]
fn clipboard(&self, content: String) {
self.push_event("clipboard", &[("content", &content)], &[]);
}
fn switch_back(&self, peer_id: &str) {
self.push_event("switch_back", &[("peer_id", peer_id)], &[]);
}
fn portable_service_running(&self, running: bool) {
self.push_event(
"portable_service_running",
&[("running", running.to_string().as_str())],
&[],
);
}
fn on_voice_call_started(&self) {
self.push_event::<&str>("on_voice_call_started", &[], &[]);
}
fn on_voice_call_closed(&self, reason: &str) {
let _res = self.push_event("on_voice_call_closed", &[("reason", reason)], &[]);
}
fn on_voice_call_waiting(&self) {
self.push_event::<&str>("on_voice_call_waiting", &[], &[]);
}
fn on_voice_call_incoming(&self) {
self.push_event::<&str>("on_voice_call_incoming", &[], &[]);
}
#[inline]
fn get_rgba(&self, _display: usize) -> *const u8 {
if let Some(rgba_data) = self.display_rgbas.read().unwrap().get(&_display) {
if rgba_data.valid {
return rgba_data.data.as_ptr();
}
}
std::ptr::null_mut()
}
#[inline]
fn next_rgba(&self, _display: usize) {
if let Some(rgba_data) = self.display_rgbas.write().unwrap().get_mut(&_display) {
rgba_data.valid = false;
}
}
fn update_record_status(&self, start: bool) {
self.push_event("record_status", &[("start", &start.to_string())], &[]);
}
fn printer_request(&self, id: i32, path: String) {
self.push_event(
"printer_request",
&[("id", json!(id)), ("path", json!(path))],
&[],
);
}
fn handle_screenshot_resp(&self, sid: String, msg: String) {
match SessionID::from_str(&sid) {
Ok(sid) => self.push_event_to("screenshot", &[("msg", json!(msg))], &[&sid]),
Err(e) => {
// Unreachable!
log::error!("Failed to parse sid \"{}\", {}", sid, e);
}
}
}
fn handle_terminal_response(&self, response: TerminalResponse) {
use base::message_proto::terminal_response::Union;
match response.union {
Some(Union::Opened(opened)) => {
let mut event_data: Vec<(&str, serde_json::Value)> = vec![
("type", json!("opened")),
("terminal_id", json!(opened.terminal_id)),
("success", json!(opened.success)),
("message", json!(&opened.message)),
("pid", json!(opened.pid)),
("service_id", json!(&opened.service_id)),
(
"replay_terminal_output",
json!(opened.replay_terminal_output),
),
];
if !opened.persistent_sessions.is_empty() {
event_data.push(("persistent_sessions", json!(opened.persistent_sessions)));
}
self.push_event_("terminal_response", &event_data, &[], &[]);
}
Some(Union::Data(data)) => {
// Decompress data if needed
let output_data = if data.compressed {
hbb_common::compress::decompress(&data.data)
} else {
data.data.to_vec()
};
let encoded = crate::encode64(&output_data);
let event_data: Vec<(&str, serde_json::Value)> = vec![
("type", json!("data")),
("terminal_id", json!(data.terminal_id)),
("data", json!(&encoded)),
];
self.push_event_("terminal_response", &event_data, &[], &[]);
}
Some(Union::Closed(closed)) => {
let event_data: Vec<(&str, serde_json::Value)> = vec![
("type", json!("closed")),
("terminal_id", json!(closed.terminal_id)),
("exit_code", json!(closed.exit_code)),
];
self.push_event_("terminal_response", &event_data, &[], &[]);
}
Some(Union::Error(error)) => {
let event_data: Vec<(&str, serde_json::Value)> = vec![
("type", json!("error")),
("terminal_id", json!(error.terminal_id)),
("message", json!(&error.message)),
];
self.push_event_("terminal_response", &event_data, &[], &[]);
}
None => {}
Some(_) => {
log::warn!("Unhandled terminal response type");
}
}
}
}
impl FlutterHandler {
#[inline]
fn on_rgba_soft_render(&self, display: usize, rgba: &mut scrap::ImageRgb) {
// If the current rgba is not fetched by flutter, i.e., is valid.
// We give up sending a new event to flutter.
let mut rgba_write_lock = self.display_rgbas.write().unwrap();
if let Some(rgba_data) = rgba_write_lock.get_mut(&display) {
if rgba_data.valid {
return;
} else {
rgba_data.valid = true;
}
// Return the rgba buffer to the video handler for reusing allocated rgba buffer.
std::mem::swap::<Vec<u8>>(&mut rgba.raw, &mut rgba_data.data);
} else {
let mut rgba_data = RgbaData::default();
std::mem::swap::<Vec<u8>>(&mut rgba.raw, &mut rgba_data.data);
rgba_data.valid = true;
rgba_write_lock.insert(display, rgba_data);
}
drop(rgba_write_lock);
let mut is_sent = false;
let is_multi_sessions = self.is_multi_ui_session();
for h in self.session_handlers.read().unwrap().values() {
// The soft renderer does not support multi-displays session for now.
if h.displays.len() > 1 {
continue;
}
// If there're multiple ui sessions, we only notify the ui session that has the display.
if is_multi_sessions {
if !h.displays.contains(&display) {
continue;
}
}
if let Some(stream) = &h.event_stream {
stream.add(EventToUI::Rgba(display));
is_sent = true;
}
}
// We need `is_sent` here. Because we use texture render for multi-displays session.
//
// Eg. We have two windows, one is display 1, the other is displays 0&1.
// When image of display 0 is received, we will not send the event.
//
// 1. "display 1" will not send the event.
// 2. "displays 0&1" will not send the event. Because it uses texutre render for now.
if !is_sent {
if let Some(rgba_data) = self.display_rgbas.write().unwrap().get_mut(&display) {
rgba_data.valid = false;
}
}
}
#[inline]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
fn on_rgba_flutter_texture_render(
&self,
use_texture_render: bool,
display: usize,
rgba: &mut scrap::ImageRgb,
) {
for (_, session) in self.session_handlers.read().unwrap().iter() {
if use_texture_render || session.displays.len() > 1 {
if session.renderer.on_rgba(display, rgba) {
if let Some(stream) = &session.event_stream {
stream.add(EventToUI::Texture(display, false));
}
}
}
}
}
}
// This function is only used for the default connection session.
pub fn session_add_existed(
peer_id: String,
session_id: SessionID,
displays: Vec<i32>,
is_view_camera: bool,
) -> ResultType<()> {
let conn_type = if is_view_camera {
ConnType::VIEW_CAMERA
} else {
ConnType::DEFAULT_CONN
};
sessions::insert_peer_session_id(peer_id, conn_type, session_id, displays);
Ok(())
}
/// Create a new remote session with the given id.
///
/// # Arguments
///
/// * `id` - The identifier of the remote session with prefix. Regex: [\w]*[\_]*[\d]+
/// * `is_file_transfer` - If the session is used for file transfer.
/// * `is_view_camera` - If the session is used for view camera.
/// * `is_port_forward` - If the session is used for port forward.
pub fn session_add(
session_id: &SessionID,
id: &str,
is_file_transfer: bool,
is_view_camera: bool,
is_port_forward: bool,
is_rdp: bool,
is_terminal: bool,
switch_uuid: &str,
force_relay: bool,
password: String,
is_shared_password: bool,
conn_token: Option<String>,
) -> ResultType<FlutterSession> {
let conn_type = if is_file_transfer {
ConnType::FILE_TRANSFER
} else if is_view_camera {
ConnType::VIEW_CAMERA
} else if is_terminal {
ConnType::TERMINAL
} else if is_port_forward {
if is_rdp {
ConnType::RDP
} else {
ConnType::PORT_FORWARD
}
} else {
ConnType::DEFAULT_CONN
};
// to-do: check the same id session.
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
if session.lc.read().unwrap().conn_type != conn_type {
bail!("same session id is found with different conn type?");
}
// The same session is added before?
bail!("same session id is found");
}
LocalConfig::set_remote_id(&id);
let mut preset_password = password.clone();
let shared_password = if is_shared_password {
// To achieve a flexible password application order, we don't treat shared password as a preset password.
preset_password = Default::default();
Some(password)
} else {
None
};
let session: Session<FlutterHandler> = Session {
password: preset_password,
server_keyboard_enabled: Arc::new(RwLock::new(true)),
server_file_transfer_enabled: Arc::new(RwLock::new(true)),
server_clipboard_enabled: Arc::new(RwLock::new(true)),
reconnect_count: Arc::new(AtomicUsize::new(0)),
..Default::default()
};
let switch_uuid = if switch_uuid.is_empty() {
None
} else {
Some(switch_uuid.to_string())
};
session.lc.write().unwrap().initialize(
id.to_owned(),
conn_type,
switch_uuid,
force_relay,
get_adapter_luid(),
shared_password,
conn_token,
);
let session = Arc::new(session.clone());
sessions::insert_session(session_id.to_owned(), conn_type, session.clone());
Ok(session)
}
/// start a session with the given id.
///
/// # Arguments
///
/// * `id` - The identifier of the remote session with prefix. Regex: [\w]*[\_]*[\d]+
/// * `events2ui` - The events channel to ui.
pub fn session_start_(
session_id: &SessionID,
id: &str,
event_stream: StreamSink<EventToUI>,
) -> ResultType<()> {
// is_connected is used to indicate whether to start a peer connection. For two cases:
// 1. "Move tab to new window"
// 2. multi ui session within the same peer connection.
let mut is_connected = false;
let mut is_found = false;
for s in sessions::get_sessions() {
if let Some(h) = s.session_handlers.write().unwrap().get_mut(session_id) {
is_connected = h.event_stream.is_some();
try_send_close_event(&h.event_stream);
h.event_stream = Some(event_stream);
is_found = true;
break;
}
}
if !is_found {
bail!(
"No session with peer id {}, session id: {}",
id,
session_id.to_string()
);
}
if let Some(session) = sessions::get_session_by_session_id(session_id) {
let is_first_ui_session = session.session_handlers.read().unwrap().len() == 1;
if !is_connected && is_first_ui_session {
log::info!(
"Session {} start, use texture render: {}",
id,
session.use_texture_render.load(Ordering::Relaxed)
);
let session = (*session).clone();
std::thread::spawn(move || {
let round = session.connection_round_state.lock().unwrap().new_round();
io_loop(session, round);
});
}
Ok(())
} else {
bail!("No session with peer id {}", id)
}
}
#[inline]
fn try_send_close_event(event_stream: &Option<StreamSink<EventToUI>>) {
if let Some(stream) = &event_stream {
stream.add(EventToUI::Event("close".to_owned()));
}
}
#[cfg(not(target_os = "ios"))]
pub fn update_text_clipboard_required() {
let is_required = sessions::get_sessions()
.iter()
.filter(|s| s.connection_round_state.lock().unwrap().is_connected())
.any(|s| s.is_default() && s.is_text_clipboard_required());
#[cfg(target_os = "android")]
let _ = scrap::android::ffi::call_clipboard_manager_enable_client_clipboard(is_required);
Client::set_is_text_clipboard_required(is_required);
}
#[cfg(feature = "unix-file-copy-paste")]
pub fn update_file_clipboard_required() {
let is_required = sessions::get_sessions()
.iter()
.filter(|s| s.connection_round_state.lock().unwrap().is_connected())
.any(|s| s.is_default() && s.is_file_clipboard_required());
Client::set_is_file_clipboard_required(is_required);
}
#[cfg(not(target_os = "ios"))]
pub fn send_clipboard_msg(msg: Message, _is_file: bool) {
send_clipboard_msg_impl(msg, _is_file, None);
}
// `except_session_id` is the session the content came from, to avoid sending it back.
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn send_clipboard_msg_to_other_sessions(msg: Message, except_session_id: u64) {
send_clipboard_msg_impl(msg, false, Some(except_session_id));
}
#[cfg(not(target_os = "ios"))]
fn send_clipboard_msg_impl(msg: Message, _is_file: bool, except_session_id: Option<u64>) {
for s in sessions::get_sessions() {
if !s.is_default() {
continue;
}
if let Some(except_session_id) = except_session_id {
if s.lc.read().unwrap().session_id == except_session_id {
continue;
}
}
#[cfg(feature = "unix-file-copy-paste")]
if _is_file {
if crate::is_support_file_copy_paste_num(s.lc.read().unwrap().version)
&& s.is_file_clipboard_required()
{
s.send(Data::Message(msg.clone()));
}
continue;
}
if s.is_text_clipboard_required() {
// Check if the client supports multi clipboards
if let Some(message::Union::MultiClipboards(multi_clipboards)) = &msg.union {
let version = s.ui_handler.peer_info.read().unwrap().version.clone();
let platform = s.ui_handler.peer_info.read().unwrap().platform.clone();
if let Some(msg_out) = crate::clipboard::get_msg_if_not_support_multi_clip(
&version,
&platform,
multi_clipboards,
) {
s.send(Data::Message(msg_out));
continue;
}
}
s.send(Data::Message(msg.clone()));
}
}
}
// Server Side
#[cfg(not(any(target_os = "ios")))]
pub mod connection_manager {
use std::collections::HashMap;
#[cfg(any(target_os = "android"))]
use hbb_common::log;
#[cfg(any(target_os = "android"))]
use scrap::android::call_main_service_set_by_name;
use serde_json::json;
use crate::ui_cm_interface::InvokeUiCM;
use super::GLOBAL_EVENT_STREAM;
#[derive(Clone)]
struct FlutterHandler {}
impl InvokeUiCM for FlutterHandler {
//TODO port_forward
fn add_connection(&self, client: &crate::ui_cm_interface::Client) {
let client_json = serde_json::to_string(&client).unwrap_or("".into());
// send to Android service, active notification no matter UI is shown or not.
#[cfg(target_os = "android")]
if let Err(e) =
call_main_service_set_by_name("add_connection", Some(&client_json), None)
{
log::debug!("call_main_service_set_by_name fail,{}", e);
}
// send to UI, refresh widget
self.push_event("add_connection", &[("client", &client_json)]);
}
fn remove_connection(&self, id: i32, close: bool) {
self.push_event(
"on_client_remove",
&[("id", &id.to_string()), ("close", &close.to_string())],
);
}
fn new_message(&self, id: i32, text: String) {
self.push_event(
"chat_server_mode",
&[("id", &id.to_string()), ("text", &text)],
);
}
fn change_theme(&self, dark: String) {
self.push_event("theme", &[("dark", &dark)]);
}
fn change_language(&self) {
self.push_event::<&str>("language", &[]);
}
fn show_elevation(&self, show: bool) {
self.push_event("show_elevation", &[("show", &show.to_string())]);
}
fn update_voice_call_state(&self, client: &crate::ui_cm_interface::Client) {
let client_json = serde_json::to_string(&client).unwrap_or("".into());
// send to Android service, active notification no matter UI is shown or not.
#[cfg(target_os = "android")]
if let Err(e) =
call_main_service_set_by_name("update_voice_call_state", Some(&client_json), None)
{
log::debug!("call_main_service_set_by_name fail,{}", e);
}
self.push_event("update_voice_call_state", &[("client", &client_json)]);
}
fn file_transfer_log(&self, action: &str, log: &str) {
self.push_event("cm_file_transfer_log", &[(action, log)]);
}
}
impl FlutterHandler {
fn push_event<V>(&self, name: &str, event: &[(&str, V)])
where
V: Sized + serde::Serialize + Clone,
{
let mut h: HashMap<&str, serde_json::Value> =
event.iter().map(|(k, v)| (*k, json!(*v))).collect();
debug_assert!(h.get("name").is_none());
h.insert("name", json!(name));
if let Some(s) = GLOBAL_EVENT_STREAM.read().unwrap().get(super::APP_TYPE_CM) {
s.add(serde_json::ser::to_string(&h).unwrap_or("".to_owned()));
} else {
println!(
"Push event {} failed. No {} event stream found.",
name,
super::APP_TYPE_CM
);
};
}
}
#[cfg(not(any(target_os = "android", target_os = "ios")))]
fn start_listen_ipc() {
use crate::ui_cm_interface::{start_ipc, ConnectionManager};
#[cfg(target_os = "linux")]
std::thread::spawn(crate::ipc::start_pa);
let cm = ConnectionManager {
ui_handler: FlutterHandler {},
};
std::thread::spawn(move || start_ipc(cm));
}
#[inline]
pub fn cm_init() {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
start_listen_ipc();
}
#[cfg(target_os = "android")]
use hbb_common::tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
#[cfg(target_os = "android")]
pub fn start_channel(
rx: UnboundedReceiver<crate::ipc::Data>,
tx: UnboundedSender<crate::ipc::Data>,
) {
use crate::ui_cm_interface::start_listen;
let cm = crate::ui_cm_interface::ConnectionManager {
ui_handler: FlutterHandler {},
};
std::thread::spawn(move || start_listen(cm, rx, tx));
}
}
pub fn make_fd_flutter(id: i32, entries: &Vec<FileEntry>, only_count: bool) -> String {
let mut m = serde_json::Map::new();
m.insert("id".into(), json!(id));
let mut a = vec![];
let mut n: u64 = 0;
for entry in entries {
n += entry.size;
if only_count {
continue;
}
let mut e = serde_json::Map::new();
e.insert("name".into(), json!(entry.name.to_owned()));
let tmp = entry.entry_type.value();
e.insert("type".into(), json!(if tmp == 0 { 1 } else { tmp }));
e.insert("time".into(), json!(entry.modified_time as f64));
e.insert("size".into(), json!(entry.size as f64));
a.push(e);
}
if only_count {
m.insert("num_entries".into(), json!(entries.len() as i32));
} else {
m.insert("entries".into(), json!(a));
}
m.insert("total_size".into(), json!(n as f64));
serde_json::to_string(&m).unwrap_or("".into())
}
pub fn get_cur_session_id() -> SessionID {
CUR_SESSION_ID.read().unwrap().clone()
}
pub fn get_cur_peer_id() -> String {
sessions::get_peer_id_by_session_id(&get_cur_session_id(), ConnType::DEFAULT_CONN)
.unwrap_or("".to_string())
}
pub fn set_cur_session_id(session_id: SessionID) {
if get_cur_session_id() != session_id {
*CUR_SESSION_ID.write().unwrap() = session_id;
}
}
#[inline]
fn serialize_resolutions(resolutions: &Vec<Resolution>) -> String {
#[derive(Debug, serde::Serialize)]
struct ResolutionSerde {
width: i32,
height: i32,
}
let mut v = vec![];
resolutions
.iter()
.map(|r| {
v.push(ResolutionSerde {
width: r.width,
height: r.height,
})
})
.count();
serde_json::ser::to_string(&v).unwrap_or("".to_string())
}
fn char_to_session_id(c: *const char) -> ResultType<SessionID> {
if c.is_null() {
bail!("Session id ptr is null");
}
let cstr = unsafe { std::ffi::CStr::from_ptr(c as _) };
let str = cstr.to_str()?;
SessionID::from_str(str).map_err(|e| anyhow!("{:?}", e))
}
pub fn session_get_rgba_size(session_id: SessionID, display: usize) -> usize {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
return session
.display_rgbas
.read()
.unwrap()
.get(&display)
.map_or(0, |rgba| rgba.data.len());
}
0
}
#[no_mangle]
pub extern "C" fn session_get_rgba(session_uuid_str: *const char, display: usize) -> *const u8 {
if let Ok(session_id) = char_to_session_id(session_uuid_str) {
if let Some(s) = sessions::get_session_by_session_id(&session_id) {
return s.ui_handler.get_rgba(display);
}
}
std::ptr::null()
}
pub fn session_next_rgba(session_id: SessionID, display: usize) {
if let Some(s) = sessions::get_session_by_session_id(&session_id) {
return s.ui_handler.next_rgba(display);
}
}
#[inline]
pub fn session_set_size(session_id: SessionID, display: usize, width: usize, height: usize) {
for s in sessions::get_sessions() {
if let Some(h) = s
.ui_handler
.session_handlers
.write()
.unwrap()
.get_mut(&session_id)
{
// If the session is the first connection, displays is not set yet.
// `displays`` is set while switching displays or adding a new session.
if !h.displays.contains(&display) {
h.displays.push(display);
}
h.renderer.set_size(display, width, height);
break;
}
}
}
#[inline]
pub fn session_register_pixelbuffer_texture(session_id: SessionID, display: usize, ptr: usize) {
for s in sessions::get_sessions() {
if let Some(h) = s
.ui_handler
.session_handlers
.read()
.unwrap()
.get(&session_id)
{
h.renderer.register_pixelbuffer_texture(display, ptr);
break;
}
}
}
#[inline]
pub fn session_register_gpu_texture(_session_id: SessionID, _display: usize, _output_ptr: usize) {
#[cfg(feature = "vram")]
for s in sessions::get_sessions() {
if let Some(h) = s
.ui_handler
.session_handlers
.read()
.unwrap()
.get(&_session_id)
{
h.renderer.register_gpu_output(_display, _output_ptr);
break;
}
}
}
#[inline]
#[cfg(not(feature = "vram"))]
pub fn get_adapter_luid() -> Option<i64> {
None
}
#[cfg(feature = "vram")]
pub fn get_adapter_luid() -> Option<i64> {
if !crate::ui_interface::use_texture_render() {
return None;
}
let get_adapter_luid_func = match &*TEXTURE_GPU_RENDERER_PLUGIN {
Ok(lib) => {
let find_sym_res = unsafe {
lib.symbol::<FlutterGpuTextureRendererPluginCApiGetAdapterLuid>(
"FlutterGpuTextureRendererPluginCApiGetAdapterLuid",
)
};
match find_sym_res {
Ok(sym) => Some(sym),
Err(e) => {
log::error!("Failed to find symbol FlutterGpuTextureRendererPluginCApiGetAdapterLuid, {e}");
None
}
}
}
Err(e) => {
log::error!("Failed to load texture gpu renderer plugin, {e}");
None
}
};
let adapter_luid = match get_adapter_luid_func {
Some(get_adapter_luid_func) => unsafe { Some(get_adapter_luid_func()) },
None => Default::default(),
};
return adapter_luid;
}
#[inline]
pub fn push_session_event(session_id: &SessionID, name: &str, event: Vec<(&str, &str)>) {
if let Some(s) = sessions::get_session_by_session_id(session_id) {
s.push_event(name, &event, &[]);
}
}
#[inline]
pub fn push_global_event(channel: &str, event: String) -> Option<bool> {
Some(GLOBAL_EVENT_STREAM.read().unwrap().get(channel)?.add(event))
}
#[inline]
pub fn get_global_event_channels() -> Vec<String> {
GLOBAL_EVENT_STREAM
.read()
.unwrap()
.keys()
.cloned()
.collect()
}
pub fn start_global_event_stream(s: StreamSink<String>, app_type: String) -> ResultType<()> {
let app_type_values = app_type.split(",").collect::<Vec<&str>>();
let mut lock = GLOBAL_EVENT_STREAM.write().unwrap();
if !lock.contains_key(app_type_values[0]) {
lock.insert(app_type_values[0].to_string(), s);
} else {
if let Some(_) = lock.insert(app_type.clone(), s) {
log::warn!(
"Global event stream of type {} is started before, but now removed",
app_type
);
}
}
Ok(())
}
pub fn stop_global_event_stream(app_type: String) {
let _ = GLOBAL_EVENT_STREAM.write().unwrap().remove(&app_type);
}
#[inline]
fn session_send_touch_scale(
session_id: SessionID,
v: &serde_json::Value,
alt: bool,
ctrl: bool,
shift: bool,
command: bool,
) {
match v.get("v").and_then(|s| s.as_i64()) {
Some(scale) => {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session.send_touch_scale(scale as _, alt, ctrl, shift, command);
}
}
None => {}
}
}
#[inline]
fn session_send_touch_pan(
session_id: SessionID,
v: &serde_json::Value,
pan_event: &str,
alt: bool,
ctrl: bool,
shift: bool,
command: bool,
) {
match v.get("v") {
Some(v) => match (
v.get("x").and_then(|x| x.as_i64()),
v.get("y").and_then(|y| y.as_i64()),
) {
(Some(x), Some(y)) => {
if let Some(session) = sessions::get_session_by_session_id(&session_id) {
session
.send_touch_pan_event(pan_event, x as _, y as _, alt, ctrl, shift, command);
}
}
_ => {}
},
_ => {}
}
}
fn session_send_touch_event(
session_id: SessionID,
v: &serde_json::Value,
alt: bool,
ctrl: bool,
shift: bool,
command: bool,
) {
match v.get("t").and_then(|t| t.as_str()) {
Some("scale") => session_send_touch_scale(session_id, v, alt, ctrl, shift, command),
Some(pan_event) => {
session_send_touch_pan(session_id, v, pan_event, alt, ctrl, shift, command)
}
_ => {}
}
}
pub fn session_send_pointer(session_id: SessionID, msg: String) {
if let Ok(m) = serde_json::from_str::<HashMap<String, serde_json::Value>>(&msg) {
let alt = m.get("alt").is_some();
let ctrl = m.get("ctrl").is_some();
let shift = m.get("shift").is_some();
let command = m.get("command").is_some();
match (m.get("k"), m.get("v")) {
(Some(k), Some(v)) => match k.as_str() {
Some("touch") => session_send_touch_event(session_id, v, alt, ctrl, shift, command),
_ => {}
},
_ => {}
}
}
}
#[inline]
pub fn session_on_waiting_for_image_dialog_show(session_id: SessionID) {
for s in sessions::get_sessions() {
if let Some(h) = s.session_handlers.write().unwrap().get_mut(&session_id) {
h.on_waiting_for_image_dialog_show();
}
}
}
#[inline]
pub fn get_cur_session() -> Option<FlutterSession> {
sessions::get_session_by_session_id(&*CUR_SESSION_ID.read().unwrap())
}
#[inline]
pub fn try_sync_peer_option(
session: &FlutterSession,
cur_id: &SessionID,
key: &str,
_value: Option<serde_json::Value>,
) {
let mut event = Vec::new();
#[cfg(not(any(target_os = "android", target_os = "ios")))]
if key == "view-only" {
event = vec![
("k", json!(key.to_string())),
("v", json!(session.lc.read().unwrap().view_only.v)),
];
}
if ["keyboard_mode", "input_source"].contains(&key) {
event = vec![("k", json!(key.to_string())), ("v", json!(""))];
}
if !event.is_empty() {
session.push_event("sync_peer_option", &event, &[cur_id]);
}
}
pub(super) fn session_update_virtual_display(session: &FlutterSession, index: i32, on: bool) {
let virtual_display_key = "virtual-display";
let displays = session.get_option(virtual_display_key.to_owned());
if !on {
if index == -1 {
if !displays.is_empty() {
session.set_option(virtual_display_key.to_owned(), "".to_owned());
}
} else {
let mut vdisplays = displays.split(',').collect::<Vec<_>>();
let len = vdisplays.len();
if index == 0 {
// 0 means we can't toggle the virtual display by index.
vdisplays.remove(vdisplays.len() - 1);
} else {
if let Some(i) = vdisplays.iter().position(|&x| x == index.to_string()) {
vdisplays.remove(i);
}
}
if vdisplays.len() != len {
session.set_option(
virtual_display_key.to_owned(),
vdisplays.join(",").to_owned(),
);
}
}
} else {
let mut vdisplays = displays
.split(',')
.map(|x| x.to_string())
.collect::<Vec<_>>();
let len = vdisplays.len();
if index == 0 {
vdisplays.push(index.to_string());
} else {
if !vdisplays.iter().any(|x| *x == index.to_string()) {
vdisplays.push(index.to_string());
}
}
if vdisplays.len() != len {
session.set_option(
virtual_display_key.to_owned(),
vdisplays.join(",").to_owned(),
);
}
}
}
// sessions mod is used to avoid the big lock of sessions' map.
pub mod sessions {
use super::*;
lazy_static::lazy_static! {
// peer -> peer session, peer session -> ui sessions
static ref SESSIONS: RwLock<HashMap<(String, ConnType), FlutterSession>> = Default::default();
}
#[inline]
pub fn get_session_count(peer_id: String, conn_type: ConnType) -> usize {
SESSIONS
.read()
.unwrap()
.get(&(peer_id, conn_type))
.map(|s| s.ui_handler.session_handlers.read().unwrap().len())
.unwrap_or(0)
}
#[inline]
pub fn get_peer_id_by_session_id(id: &SessionID, conn_type: ConnType) -> Option<String> {
SESSIONS
.read()
.unwrap()
.iter()
.find_map(|((peer_id, t), s)| {
if *t == conn_type
&& s.ui_handler
.session_handlers
.read()
.unwrap()
.contains_key(id)
{
Some(peer_id.clone())
} else {
None
}
})
}
#[inline]
pub fn get_session_by_session_id(id: &SessionID) -> Option<FlutterSession> {
SESSIONS
.read()
.unwrap()
.values()
.find(|s| {
s.ui_handler
.session_handlers
.read()
.unwrap()
.contains_key(id)
})
.cloned()
}
#[inline]
pub fn get_session_by_peer_id(peer_id: String, conn_type: ConnType) -> Option<FlutterSession> {
SESSIONS.read().unwrap().get(&(peer_id, conn_type)).cloned()
}
#[inline]
pub fn remove_session_by_session_id(id: &SessionID) -> Option<FlutterSession> {
let mut remove_peer_key = None;
for (peer_key, s) in SESSIONS.write().unwrap().iter_mut() {
let mut write_lock = s.ui_handler.session_handlers.write().unwrap();
let remove_ret = write_lock.remove(id);
match remove_ret {
Some(_) => {
if write_lock.is_empty() {
remove_peer_key = Some(peer_key.clone());
} else {
check_remove_unused_displays(None, id, s, &write_lock);
}
break;
}
None => {}
}
}
let s = SESSIONS.write().unwrap().remove(&remove_peer_key?);
#[cfg(not(any(target_os = "android", target_os = "ios")))]
update_session_count_to_server();
s
}
/// Close every client session, returning how many peer sessions were closed.
///
/// Used when the UI is gone but the process keeps running, e.g. the Android
/// task is swiped away from recents while a foreground service keeps the
/// process alive. The orphaned `io_loop` would otherwise keep answering
/// `TestDelay`, so the peer never hits its inactivity timeout and the
/// session stays established with no way to close it.
#[cfg(any(target_os = "android", target_os = "ios"))]
pub fn close_all_sessions() -> usize {
// Release held keys before draining: the release path sends through
// `get_cur_session()`, which resolves against SESSIONS, so draining
// first would take TO_RELEASE and then silently drop every key-up,
// leaving the key stuck on the controlled side. A no-op when nothing
// is held.
crate::keyboard::release_remote_keys("map");
// Drain so the map lock is released before closing each session.
let sessions: Vec<FlutterSession> = SESSIONS
.write()
.unwrap()
.drain()
.map(|(_, session)| session)
.collect();
for session in sessions.iter() {
let session_ids: Vec<SessionID> = session
.ui_handler
.session_handlers
.read()
.unwrap()
.keys()
.cloned()
.collect();
for session_id in session_ids {
session.close_event_stream(session_id);
}
session.close();
}
sessions.len()
}
/// Check if removing a session by session_id would result in removing the entire peer.
///
/// Returns:
/// - `true`: The session exists and removing it would leave the peer with no other sessions,
/// so the entire peer would be removed (equivalent to `remove_session_by_session_id` returning `Some`)
/// - `false`: The session doesn't exist, or it exists but the peer has other sessions,
/// so the peer would not be removed (equivalent to `remove_session_by_session_id` returning `None`)
#[inline]
pub fn would_remove_peer_by_session_id(id: &SessionID) -> bool {
for (_peer_key, s) in SESSIONS.read().unwrap().iter() {
let read_lock = s.ui_handler.session_handlers.read().unwrap();
if read_lock.contains_key(id) {
// Found the session, check if it's the only one for this peer
return read_lock.len() == 1;
}
}
// Session not found
false
}
fn check_remove_unused_displays(
current: Option<usize>,
session_id: &SessionID,
session: &FlutterSession,
handlers: &HashMap<SessionID, SessionHandler>,
) {
// Set capture displays if some are not used any more.
let mut remains_displays = HashSet::new();
if let Some(current) = current {
remains_displays.insert(current);
}
for (k, h) in handlers.iter() {
if k == session_id {
continue;
}
remains_displays.extend(
h.renderer
.map_display_sessions
.read()
.unwrap()
.keys()
.cloned(),
);
}
if !remains_displays.is_empty() {
session.capture_displays(
vec![],
vec![],
remains_displays.iter().map(|d| *d as i32).collect(),
);
}
}
pub fn session_switch_display(is_desktop: bool, session_id: SessionID, value: Vec<i32>) {
for s in SESSIONS.read().unwrap().values() {
let mut write_lock = s.ui_handler.session_handlers.write().unwrap();
if let Some(h) = write_lock.get_mut(&session_id) {
h.displays = value.iter().map(|x| *x as usize).collect::<_>();
#[cfg(not(any(target_os = "android", target_os = "ios")))]
let displays_refresh = value.clone();
if value.len() == 1 {
// Switch display.
// This operation will also cause the peer to send a switch display message.
// The switch display message will contain `SupportedResolutions`, which is useful when changing resolutions.
s.switch_display(value[0]);
// Reset the valid flag of the display.
s.next_rgba(value[0] as usize);
if !is_desktop {
s.capture_displays(vec![], vec![], value);
} else {
// Check if other displays are needed.
if value.len() == 1 {
check_remove_unused_displays(
Some(value[0] as _),
&session_id,
&s,
&write_lock,
);
}
}
} else {
// Try capture all displays.
s.capture_displays(vec![], vec![], value);
}
// When switching display, we also need to send "Refresh display" message.
// On the controlled side:
// 1. If this display is not currently captured -> Refresh -> Message "Refresh display" is not required.
// One more key frame (first frame) will be sent because the refresh message.
// 2. If this display is currently captured -> Not refresh -> Message "Refresh display" is required.
// Without the message, the control side cannot see the latest display image.
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
let is_support_multi_ui_session = crate::common::is_support_multi_ui_session(
&s.ui_handler.peer_info.read().unwrap().version,
);
if is_support_multi_ui_session {
for display in displays_refresh.iter() {
s.refresh_video(*display);
}
}
}
break;
}
}
}
#[inline]
pub fn insert_session(session_id: SessionID, conn_type: ConnType, session: FlutterSession) {
SESSIONS
.write()
.unwrap()
.entry((session.get_id(), conn_type))
.or_insert(session)
.ui_handler
.session_handlers
.write()
.unwrap()
.insert(session_id, Default::default());
#[cfg(not(any(target_os = "android", target_os = "ios")))]
update_session_count_to_server();
}
#[inline]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
fn update_session_count_to_server() {
crate::ipc::update_controlling_session_count(SESSIONS.read().unwrap().len()).ok();
}
#[inline]
pub fn insert_peer_session_id(
peer_id: String,
conn_type: ConnType,
session_id: SessionID,
displays: Vec<i32>,
) -> bool {
if let Some(s) = SESSIONS.read().unwrap().get(&(peer_id, conn_type)) {
let mut h = SessionHandler::default();
h.displays = displays.iter().map(|x| *x as usize).collect::<_>();
#[cfg(not(any(target_os = "android", target_os = "ios")))]
let is_support_multi_ui_session = crate::common::is_support_multi_ui_session(
&s.ui_handler.peer_info.read().unwrap().version,
);
#[cfg(any(target_os = "android", target_os = "ios"))]
let is_support_multi_ui_session = false;
h.renderer.is_support_multi_ui_session = is_support_multi_ui_session;
let _ = s
.ui_handler
.session_handlers
.write()
.unwrap()
.insert(session_id, h);
// If the session is a single display session, it may be a software rgba rendered display.
// If this is the second time the display is opened, the old valid flag may be true.
if displays.len() == 1 {
s.ui_handler.next_rgba(displays[0] as usize);
}
true
} else {
false
}
}
#[inline]
pub fn get_sessions() -> Vec<FlutterSession> {
SESSIONS.read().unwrap().values().cloned().collect()
}
#[inline]
#[cfg(not(target_os = "ios"))]
pub fn has_sessions_running(conn_type: ConnType) -> bool {
SESSIONS.read().unwrap().iter().any(|((_, r#type), s)| {
*r#type == conn_type && s.session_handlers.read().unwrap().len() != 0
})
}
#[inline]
#[cfg(not(target_os = "ios"))]
pub fn has_connected_sessions_running(conn_type: ConnType) -> bool {
SESSIONS.read().unwrap().iter().any(|((_, r#type), s)| {
*r#type == conn_type
&& s.session_handlers.read().unwrap().len() != 0
&& s.connection_round_state.lock().unwrap().is_connected()
})
}
}
pub(super) mod async_tasks {
use hbb_common::{bail, tokio, ResultType};
use std::{
collections::HashMap,
sync::{
mpsc::{sync_channel, SyncSender},
Arc, Mutex,
},
};
type TxQueryOnlines = SyncSender<Vec<String>>;
lazy_static::lazy_static! {
static ref TX_QUERY_ONLINES: Arc<Mutex<Option<TxQueryOnlines>>> = Default::default();
}
#[inline]
pub fn start_flutter_async_runner() {
std::thread::spawn(start_flutter_async_runner_);
}
#[allow(dead_code)]
pub fn stop_flutter_async_runner() {
let _ = TX_QUERY_ONLINES.lock().unwrap().take();
}
#[tokio::main(flavor = "current_thread")]
async fn start_flutter_async_runner_() {
// Only one task is allowed to run at the same time.
let (tx_onlines, rx_onlines) = sync_channel::<Vec<String>>(1);
TX_QUERY_ONLINES.lock().unwrap().replace(tx_onlines);
loop {
match rx_onlines.recv() {
Ok(ids) => {
crate::client::peer_online::query_online_states(ids, handle_query_onlines).await
}
_ => {
// unreachable!
break;
}
}
}
}
pub fn query_onlines(ids: Vec<String>) -> ResultType<()> {
if let Some(tx) = TX_QUERY_ONLINES.lock().unwrap().as_ref() {
// Ignore if the channel is full.
let _ = tx.try_send(ids)?;
} else {
bail!("No tx_query_onlines");
}
Ok(())
}
fn handle_query_onlines(onlines: Vec<String>, offlines: Vec<String>) {
let data = HashMap::from([
("name", "callback_query_onlines".to_owned()),
("onlines", onlines.join(",")),
("offlines", offlines.join(",")),
]);
let _res = super::push_global_event(
super::APP_TYPE_MAIN,
serde_json::ser::to_string(&data).unwrap_or("".to_owned()),
);
}
}