model.dart 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:math';
  4. import 'dart:typed_data';
  5. import 'dart:ui' as ui;
  6. import 'package:bot_toast/bot_toast.dart';
  7. import 'package:desktop_multi_window/desktop_multi_window.dart';
  8. import 'package:flutter/gestures.dart';
  9. import 'package:flutter/material.dart';
  10. import 'package:flutter/services.dart';
  11. import 'package:flutter_hbb/common/widgets/peers_view.dart';
  12. import 'package:flutter_hbb/consts.dart';
  13. import 'package:flutter_hbb/models/ab_model.dart';
  14. import 'package:flutter_hbb/models/chat_model.dart';
  15. import 'package:flutter_hbb/models/cm_file_model.dart';
  16. import 'package:flutter_hbb/models/file_model.dart';
  17. import 'package:flutter_hbb/models/group_model.dart';
  18. import 'package:flutter_hbb/models/peer_model.dart';
  19. import 'package:flutter_hbb/models/peer_tab_model.dart';
  20. import 'package:flutter_hbb/models/printer_model.dart';
  21. import 'package:flutter_hbb/models/server_model.dart';
  22. import 'package:flutter_hbb/models/user_model.dart';
  23. import 'package:flutter_hbb/models/state_model.dart';
  24. import 'package:flutter_hbb/models/desktop_render_texture.dart';
  25. import 'package:flutter_hbb/plugin/event.dart';
  26. import 'package:flutter_hbb/plugin/manager.dart';
  27. import 'package:flutter_hbb/plugin/widgets/desc_ui.dart';
  28. import 'package:flutter_hbb/common/shared_state.dart';
  29. import 'package:flutter_hbb/utils/multi_window_manager.dart';
  30. import 'package:tuple/tuple.dart';
  31. import 'package:image/image.dart' as img2;
  32. import 'package:flutter_svg/flutter_svg.dart';
  33. import 'package:get/get.dart';
  34. import 'package:uuid/uuid.dart';
  35. import 'package:window_manager/window_manager.dart';
  36. import 'package:file_picker/file_picker.dart';
  37. import '../common.dart';
  38. import '../utils/image.dart' as img;
  39. import '../common/widgets/dialog.dart';
  40. import 'input_model.dart';
  41. import 'platform_model.dart';
  42. import 'package:flutter_hbb/generated_bridge.dart'
  43. if (dart.library.html) 'package:flutter_hbb/web/bridge.dart';
  44. import 'package:flutter_hbb/native/custom_cursor.dart'
  45. if (dart.library.html) 'package:flutter_hbb/web/custom_cursor.dart';
  46. typedef HandleMsgBox = Function(Map<String, dynamic> evt, String id);
  47. typedef ReconnectHandle = Function(OverlayDialogManager, SessionID, bool);
  48. final _constSessionId = Uuid().v4obj();
  49. class CachedPeerData {
  50. Map<String, dynamic> updatePrivacyMode = {};
  51. Map<String, dynamic> peerInfo = {};
  52. List<Map<String, dynamic>> cursorDataList = [];
  53. Map<String, dynamic> lastCursorId = {};
  54. Map<String, bool> permissions = {};
  55. bool secure = false;
  56. bool direct = false;
  57. CachedPeerData();
  58. @override
  59. String toString() {
  60. return jsonEncode({
  61. 'updatePrivacyMode': updatePrivacyMode,
  62. 'peerInfo': peerInfo,
  63. 'cursorDataList': cursorDataList,
  64. 'lastCursorId': lastCursorId,
  65. 'permissions': permissions,
  66. 'secure': secure,
  67. 'direct': direct,
  68. });
  69. }
  70. static CachedPeerData? fromString(String s) {
  71. try {
  72. final map = jsonDecode(s);
  73. final data = CachedPeerData();
  74. data.updatePrivacyMode = map['updatePrivacyMode'];
  75. data.peerInfo = map['peerInfo'];
  76. for (final cursorData in map['cursorDataList']) {
  77. data.cursorDataList.add(cursorData);
  78. }
  79. data.lastCursorId = map['lastCursorId'];
  80. map['permissions'].forEach((key, value) {
  81. data.permissions[key] = value;
  82. });
  83. data.secure = map['secure'];
  84. data.direct = map['direct'];
  85. return data;
  86. } catch (e) {
  87. debugPrint('Failed to parse CachedPeerData: $e');
  88. return null;
  89. }
  90. }
  91. }
  92. class FfiModel with ChangeNotifier {
  93. CachedPeerData cachedPeerData = CachedPeerData();
  94. PeerInfo _pi = PeerInfo();
  95. Rect? _rect;
  96. var _inputBlocked = false;
  97. final _permissions = <String, bool>{};
  98. bool? _secure;
  99. bool? _direct;
  100. bool _touchMode = false;
  101. Timer? _timer;
  102. var _reconnects = 1;
  103. bool _viewOnly = false;
  104. WeakReference<FFI> parent;
  105. late final SessionID sessionId;
  106. RxBool waitForImageDialogShow = true.obs;
  107. Timer? waitForImageTimer;
  108. RxBool waitForFirstImage = true.obs;
  109. bool isRefreshing = false;
  110. Timer? timerScreenshot;
  111. Rect? get rect => _rect;
  112. bool get isOriginalResolutionSet =>
  113. _pi.tryGetDisplayIfNotAllDisplay()?.isOriginalResolutionSet ?? false;
  114. bool get isVirtualDisplayResolution =>
  115. _pi.tryGetDisplayIfNotAllDisplay()?.isVirtualDisplayResolution ?? false;
  116. bool get isOriginalResolution =>
  117. _pi.tryGetDisplayIfNotAllDisplay()?.isOriginalResolution ?? false;
  118. Map<String, bool> get permissions => _permissions;
  119. setPermissions(Map<String, bool> permissions) {
  120. _permissions.clear();
  121. _permissions.addAll(permissions);
  122. }
  123. bool? get secure => _secure;
  124. bool? get direct => _direct;
  125. PeerInfo get pi => _pi;
  126. bool get inputBlocked => _inputBlocked;
  127. bool get touchMode => _touchMode;
  128. bool get isPeerAndroid => _pi.platform == kPeerPlatformAndroid;
  129. bool get isPeerMobile => isPeerAndroid;
  130. bool get viewOnly => _viewOnly;
  131. set inputBlocked(v) {
  132. _inputBlocked = v;
  133. }
  134. FfiModel(this.parent) {
  135. clear();
  136. sessionId = parent.target!.sessionId;
  137. cachedPeerData.permissions = _permissions;
  138. }
  139. Rect? globalDisplaysRect() => _getDisplaysRect(_pi.displays, true);
  140. Rect? displaysRect() => _getDisplaysRect(_pi.getCurDisplays(), false);
  141. Rect? _getDisplaysRect(List<Display> displays, bool useDisplayScale) {
  142. if (displays.isEmpty) {
  143. return null;
  144. }
  145. int scale(int len, double s) {
  146. if (useDisplayScale) {
  147. return len.toDouble() ~/ s;
  148. } else {
  149. return len;
  150. }
  151. }
  152. double l = displays[0].x;
  153. double t = displays[0].y;
  154. double r = displays[0].x + scale(displays[0].width, displays[0].scale);
  155. double b = displays[0].y + scale(displays[0].height, displays[0].scale);
  156. for (var display in displays.sublist(1)) {
  157. l = min(l, display.x);
  158. t = min(t, display.y);
  159. r = max(r, display.x + scale(display.width, display.scale));
  160. b = max(b, display.y + scale(display.height, display.scale));
  161. }
  162. return Rect.fromLTRB(l, t, r, b);
  163. }
  164. toggleTouchMode() {
  165. if (!isPeerAndroid) {
  166. _touchMode = !_touchMode;
  167. notifyListeners();
  168. }
  169. }
  170. updatePermission(Map<String, dynamic> evt, String id) {
  171. evt.forEach((k, v) {
  172. if (k == 'name' || k.isEmpty) return;
  173. _permissions[k] = v == 'true';
  174. });
  175. // Only inited at remote page
  176. if (parent.target?.connType == ConnType.defaultConn) {
  177. KeyboardEnabledState.find(id).value = _permissions['keyboard'] != false;
  178. }
  179. debugPrint('updatePermission: $_permissions');
  180. notifyListeners();
  181. }
  182. bool get keyboard => _permissions['keyboard'] != false;
  183. clear() {
  184. _pi = PeerInfo();
  185. _secure = null;
  186. _direct = null;
  187. _inputBlocked = false;
  188. _timer?.cancel();
  189. _timer = null;
  190. clearPermissions();
  191. waitForImageTimer?.cancel();
  192. timerScreenshot?.cancel();
  193. }
  194. setConnectionType(String peerId, bool secure, bool direct) {
  195. cachedPeerData.secure = secure;
  196. cachedPeerData.direct = direct;
  197. _secure = secure;
  198. _direct = direct;
  199. try {
  200. var connectionType = ConnectionTypeState.find(peerId);
  201. connectionType.setSecure(secure);
  202. connectionType.setDirect(direct);
  203. } catch (e) {
  204. //
  205. }
  206. }
  207. Widget? getConnectionImage() {
  208. if (secure == null || direct == null) {
  209. return null;
  210. } else {
  211. final icon =
  212. '${secure == true ? 'secure' : 'insecure'}${direct == true ? '' : '_relay'}';
  213. return SvgPicture.asset('assets/$icon.svg', width: 48, height: 48);
  214. }
  215. }
  216. clearPermissions() {
  217. _inputBlocked = false;
  218. _permissions.clear();
  219. }
  220. handleCachedPeerData(CachedPeerData data, String peerId) async {
  221. handleMsgBox({
  222. 'type': 'success',
  223. 'title': 'Successful',
  224. 'text': kMsgboxTextWaitingForImage,
  225. 'link': '',
  226. }, sessionId, peerId);
  227. updatePrivacyMode(data.updatePrivacyMode, sessionId, peerId);
  228. setConnectionType(peerId, data.secure, data.direct);
  229. await handlePeerInfo(data.peerInfo, peerId, true);
  230. for (final element in data.cursorDataList) {
  231. updateLastCursorId(element);
  232. await handleCursorData(element);
  233. }
  234. if (data.lastCursorId.isNotEmpty) {
  235. updateLastCursorId(data.lastCursorId);
  236. handleCursorId(data.lastCursorId);
  237. }
  238. }
  239. // todo: why called by two position
  240. StreamEventHandler startEventListener(SessionID sessionId, String peerId) {
  241. return (evt) async {
  242. var name = evt['name'];
  243. if (name == 'msgbox') {
  244. handleMsgBox(evt, sessionId, peerId);
  245. } else if (name == 'toast') {
  246. handleToast(evt, sessionId, peerId);
  247. } else if (name == 'set_multiple_windows_session') {
  248. handleMultipleWindowsSession(evt, sessionId, peerId);
  249. } else if (name == 'peer_info') {
  250. handlePeerInfo(evt, peerId, false);
  251. } else if (name == 'sync_peer_info') {
  252. handleSyncPeerInfo(evt, sessionId, peerId);
  253. } else if (name == 'sync_platform_additions') {
  254. handlePlatformAdditions(evt, sessionId, peerId);
  255. } else if (name == 'connection_ready') {
  256. setConnectionType(
  257. peerId, evt['secure'] == 'true', evt['direct'] == 'true');
  258. } else if (name == 'switch_display') {
  259. // switch display is kept for backward compatibility
  260. handleSwitchDisplay(evt, sessionId, peerId);
  261. } else if (name == 'cursor_data') {
  262. updateLastCursorId(evt);
  263. await handleCursorData(evt);
  264. } else if (name == 'cursor_id') {
  265. updateLastCursorId(evt);
  266. handleCursorId(evt);
  267. } else if (name == 'cursor_position') {
  268. await parent.target?.cursorModel.updateCursorPosition(evt, peerId);
  269. } else if (name == 'clipboard') {
  270. Clipboard.setData(ClipboardData(text: evt['content']));
  271. } else if (name == 'permission') {
  272. updatePermission(evt, peerId);
  273. } else if (name == 'chat_client_mode') {
  274. parent.target?.chatModel
  275. .receive(ChatModel.clientModeID, evt['text'] ?? '');
  276. } else if (name == 'chat_server_mode') {
  277. parent.target?.chatModel
  278. .receive(int.parse(evt['id'] as String), evt['text'] ?? '');
  279. } else if (name == 'file_dir') {
  280. parent.target?.fileModel.receiveFileDir(evt);
  281. } else if (name == 'empty_dirs') {
  282. parent.target?.fileModel.receiveEmptyDirs(evt);
  283. } else if (name == 'job_progress') {
  284. parent.target?.fileModel.jobController.tryUpdateJobProgress(evt);
  285. } else if (name == 'job_done') {
  286. bool? refresh =
  287. await parent.target?.fileModel.jobController.jobDone(evt);
  288. if (refresh == true) {
  289. // many job done for delete directory
  290. // todo: refresh may not work when confirm delete local directory
  291. parent.target?.fileModel.refreshAll();
  292. }
  293. } else if (name == 'job_error') {
  294. parent.target?.fileModel.jobController.jobError(evt);
  295. } else if (name == 'override_file_confirm') {
  296. parent.target?.fileModel.postOverrideFileConfirm(evt);
  297. } else if (name == 'load_last_job') {
  298. parent.target?.fileModel.jobController.loadLastJob(evt);
  299. } else if (name == 'update_folder_files') {
  300. parent.target?.fileModel.jobController.updateFolderFiles(evt);
  301. } else if (name == 'add_connection') {
  302. parent.target?.serverModel.addConnection(evt);
  303. } else if (name == 'on_client_remove') {
  304. parent.target?.serverModel.onClientRemove(evt);
  305. } else if (name == 'update_quality_status') {
  306. parent.target?.qualityMonitorModel.updateQualityStatus(evt);
  307. } else if (name == 'update_block_input_state') {
  308. updateBlockInputState(evt, peerId);
  309. } else if (name == 'update_privacy_mode') {
  310. updatePrivacyMode(evt, sessionId, peerId);
  311. } else if (name == 'show_elevation') {
  312. final show = evt['show'].toString() == 'true';
  313. parent.target?.serverModel.setShowElevation(show);
  314. } else if (name == 'cancel_msgbox') {
  315. cancelMsgBox(evt, sessionId);
  316. } else if (name == 'switch_back') {
  317. final peer_id = evt['peer_id'].toString();
  318. await bind.sessionSwitchSides(sessionId: sessionId);
  319. closeConnection(id: peer_id);
  320. } else if (name == 'portable_service_running') {
  321. _handlePortableServiceRunning(peerId, evt);
  322. } else if (name == 'on_url_scheme_received') {
  323. // currently comes from "_url" ipc of mac and dbus of linux
  324. onUrlSchemeReceived(evt);
  325. } else if (name == 'on_voice_call_waiting') {
  326. // Waiting for the response from the peer.
  327. parent.target?.chatModel.onVoiceCallWaiting();
  328. } else if (name == 'on_voice_call_started') {
  329. // Voice call is connected.
  330. parent.target?.chatModel.onVoiceCallStarted();
  331. } else if (name == 'on_voice_call_closed') {
  332. // Voice call is closed with reason.
  333. final reason = evt['reason'].toString();
  334. parent.target?.chatModel.onVoiceCallClosed(reason);
  335. } else if (name == 'on_voice_call_incoming') {
  336. // Voice call is requested by the peer.
  337. parent.target?.chatModel.onVoiceCallIncoming();
  338. } else if (name == 'update_voice_call_state') {
  339. parent.target?.serverModel.updateVoiceCallState(evt);
  340. } else if (name == 'fingerprint') {
  341. FingerprintState.find(peerId).value = evt['fingerprint'] ?? '';
  342. } else if (name == 'plugin_manager') {
  343. pluginManager.handleEvent(evt);
  344. } else if (name == 'plugin_event') {
  345. handlePluginEvent(evt,
  346. (Map<String, dynamic> e) => handleMsgBox(e, sessionId, peerId));
  347. } else if (name == 'plugin_reload') {
  348. handleReloading(evt);
  349. } else if (name == 'plugin_option') {
  350. handleOption(evt);
  351. } else if (name == "sync_peer_hash_password_to_personal_ab") {
  352. if (desktopType == DesktopType.main || isWeb || isMobile) {
  353. final id = evt['id'];
  354. final hash = evt['hash'];
  355. if (id != null && hash != null) {
  356. gFFI.abModel
  357. .changePersonalHashPassword(id.toString(), hash.toString());
  358. }
  359. }
  360. } else if (name == "cm_file_transfer_log") {
  361. if (isDesktop) {
  362. gFFI.cmFileModel.onFileTransferLog(evt);
  363. }
  364. } else if (name == 'sync_peer_option') {
  365. _handleSyncPeerOption(evt, peerId);
  366. } else if (name == 'follow_current_display') {
  367. handleFollowCurrentDisplay(evt, sessionId, peerId);
  368. } else if (name == 'use_texture_render') {
  369. _handleUseTextureRender(evt, sessionId, peerId);
  370. } else if (name == "selected_files") {
  371. if (isWeb) {
  372. parent.target?.fileModel.onSelectedFiles(evt);
  373. }
  374. } else if (name == "send_emptry_dirs") {
  375. if (isWeb) {
  376. parent.target?.fileModel.sendEmptyDirs(evt);
  377. }
  378. } else if (name == "record_status") {
  379. if (desktopType == DesktopType.remote ||
  380. desktopType == DesktopType.viewCamera ||
  381. isMobile) {
  382. parent.target?.recordingModel.updateStatus(evt['start'] == 'true');
  383. }
  384. } else if (name == "printer_request") {
  385. _handlePrinterRequest(evt, sessionId, peerId);
  386. } else if (name == 'screenshot') {
  387. _handleScreenshot(evt, sessionId, peerId);
  388. } else {
  389. debugPrint('Event is not handled in the fixed branch: $name');
  390. }
  391. };
  392. }
  393. _handleScreenshot(
  394. Map<String, dynamic> evt, SessionID sessionId, String peerId) {
  395. timerScreenshot?.cancel();
  396. timerScreenshot = null;
  397. final msg = evt['msg'] ?? '';
  398. final msgBoxType = 'custom-nook-nocancel-hasclose';
  399. final msgBoxTitle = 'Take screenshot';
  400. final dialogManager = parent.target!.dialogManager;
  401. if (msg.isNotEmpty) {
  402. msgBox(sessionId, msgBoxType, msgBoxTitle, msg, '', dialogManager);
  403. } else {
  404. final msgBoxText = 'screenshot-action-tip';
  405. close() {
  406. dialogManager.dismissAll();
  407. }
  408. saveAs() {
  409. close();
  410. Future.delayed(Duration.zero, () async {
  411. final ts = DateTime.now().millisecondsSinceEpoch ~/ 1000;
  412. String? outputFile = await FilePicker.platform.saveFile(
  413. dialogTitle: '${translate('Save as')}...',
  414. fileName: 'screenshot_$ts.png',
  415. allowedExtensions: ['png'],
  416. type: FileType.custom,
  417. );
  418. if (outputFile == null) {
  419. bind.sessionHandleScreenshot(sessionId: sessionId, action: '2');
  420. } else {
  421. final res = await bind.sessionHandleScreenshot(
  422. sessionId: sessionId, action: '0:$outputFile');
  423. if (res.isNotEmpty) {
  424. msgBox(sessionId, 'custom-nook-nocancel-hasclose-error',
  425. 'Take screenshot', res, '', dialogManager);
  426. }
  427. }
  428. });
  429. }
  430. copyToClipboard() {
  431. bind.sessionHandleScreenshot(sessionId: sessionId, action: '1');
  432. close();
  433. }
  434. cancel() {
  435. bind.sessionHandleScreenshot(sessionId: sessionId, action: '2');
  436. close();
  437. }
  438. final List<Widget> buttons = [
  439. dialogButton('${translate('Save as')}...', onPressed: saveAs),
  440. dialogButton('Copy to clipboard', onPressed: copyToClipboard),
  441. dialogButton('Cancel', onPressed: cancel),
  442. ];
  443. dialogManager.dismissAll();
  444. dialogManager.show(
  445. (setState, close, context) => CustomAlertDialog(
  446. title: null,
  447. content: SelectionArea(
  448. child: msgboxContent(msgBoxType, msgBoxTitle, msgBoxText)),
  449. actions: buttons,
  450. ),
  451. tag: '$msgBoxType-$msgBoxTitle-$msgBoxTitle',
  452. );
  453. }
  454. }
  455. _handlePrinterRequest(
  456. Map<String, dynamic> evt, SessionID sessionId, String peerId) {
  457. final id = evt['id'];
  458. final path = evt['path'];
  459. final dialogManager = parent.target!.dialogManager;
  460. dialogManager.show((setState, close, context) {
  461. PrinterOptions printerOptions = PrinterOptions.load();
  462. final saveSettings = mainGetLocalBoolOptionSync(kKeyPrinterSave).obs;
  463. final dontShowAgain = false.obs;
  464. final Rx<String> selectedPrinterName = printerOptions.printerName.obs;
  465. final printerNames = printerOptions.printerNames;
  466. final defaultOrSelectedGroupValue =
  467. (printerOptions.action == kValuePrinterIncomingJobDismiss
  468. ? kValuePrinterIncomingJobDefault
  469. : printerOptions.action)
  470. .obs;
  471. onRatioChanged(String? value) {
  472. defaultOrSelectedGroupValue.value =
  473. value ?? kValuePrinterIncomingJobDefault;
  474. }
  475. onSubmit() {
  476. final printerName = defaultOrSelectedGroupValue.isEmpty
  477. ? ''
  478. : selectedPrinterName.value;
  479. bind.sessionPrinterResponse(
  480. sessionId: sessionId, id: id, path: path, printerName: printerName);
  481. if (saveSettings.value || dontShowAgain.value) {
  482. bind.mainSetLocalOption(key: kKeyPrinterSelected, value: printerName);
  483. bind.mainSetLocalOption(
  484. key: kKeyPrinterIncomingJobAction,
  485. value: defaultOrSelectedGroupValue.value);
  486. }
  487. if (dontShowAgain.value) {
  488. mainSetLocalBoolOption(kKeyPrinterAllowAutoPrint, true);
  489. }
  490. close();
  491. }
  492. onCancel() {
  493. if (dontShowAgain.value) {
  494. bind.mainSetLocalOption(
  495. key: kKeyPrinterIncomingJobAction,
  496. value: kValuePrinterIncomingJobDismiss);
  497. }
  498. close();
  499. }
  500. final printerItemHeight = 30.0;
  501. final selectionAreaHeight =
  502. printerItemHeight * min(8.0, max(printerNames.length, 3.0));
  503. final content = Column(
  504. children: [
  505. Text(translate('print-incoming-job-confirm-tip')),
  506. Row(
  507. children: [
  508. Obx(() => Radio<String>(
  509. value: kValuePrinterIncomingJobDefault,
  510. groupValue: defaultOrSelectedGroupValue.value,
  511. onChanged: onRatioChanged)),
  512. GestureDetector(
  513. child: Text(translate('use-the-default-printer-tip')),
  514. onTap: () => onRatioChanged(kValuePrinterIncomingJobDefault)),
  515. ],
  516. ),
  517. Column(
  518. children: [
  519. Row(children: [
  520. Obx(() => Radio<String>(
  521. value: kValuePrinterIncomingJobSelected,
  522. groupValue: defaultOrSelectedGroupValue.value,
  523. onChanged: onRatioChanged)),
  524. GestureDetector(
  525. child: Text(translate('use-the-selected-printer-tip')),
  526. onTap: () =>
  527. onRatioChanged(kValuePrinterIncomingJobSelected)),
  528. ]),
  529. SizedBox(
  530. height: selectionAreaHeight,
  531. width: 500,
  532. child: ListView.builder(
  533. itemBuilder: (context, index) {
  534. return Obx(() => GestureDetector(
  535. child: Container(
  536. decoration: BoxDecoration(
  537. color: selectedPrinterName.value ==
  538. printerNames[index]
  539. ? (defaultOrSelectedGroupValue.value ==
  540. kValuePrinterIncomingJobSelected
  541. ? MyTheme.button
  542. : MyTheme.button.withOpacity(0.5))
  543. : Theme.of(context).cardColor,
  544. borderRadius: BorderRadius.all(
  545. Radius.circular(5.0),
  546. ),
  547. ),
  548. key: ValueKey(printerNames[index]),
  549. height: printerItemHeight,
  550. child: Align(
  551. alignment: Alignment.centerLeft,
  552. child: Padding(
  553. padding: const EdgeInsets.only(left: 10.0),
  554. child: Text(
  555. printerNames[index],
  556. style: TextStyle(fontSize: 14),
  557. ),
  558. ),
  559. ),
  560. ),
  561. onTap: defaultOrSelectedGroupValue.value ==
  562. kValuePrinterIncomingJobSelected
  563. ? () {
  564. selectedPrinterName.value =
  565. printerNames[index];
  566. }
  567. : null,
  568. ));
  569. },
  570. itemCount: printerNames.length),
  571. ),
  572. ],
  573. ),
  574. Row(
  575. children: [
  576. Obx(() => Checkbox(
  577. value: saveSettings.value,
  578. onChanged: (value) {
  579. if (value != null) {
  580. saveSettings.value = value;
  581. mainSetLocalBoolOption(kKeyPrinterSave, value);
  582. }
  583. })),
  584. GestureDetector(
  585. child: Text(translate('save-settings-tip')),
  586. onTap: () {
  587. saveSettings.value = !saveSettings.value;
  588. mainSetLocalBoolOption(kKeyPrinterSave, saveSettings.value);
  589. }),
  590. ],
  591. ),
  592. Row(
  593. children: [
  594. Obx(() => Checkbox(
  595. value: dontShowAgain.value,
  596. onChanged: (value) {
  597. if (value != null) {
  598. dontShowAgain.value = value;
  599. }
  600. })),
  601. GestureDetector(
  602. child: Text(translate('dont-show-again-tip')),
  603. onTap: () {
  604. dontShowAgain.value = !dontShowAgain.value;
  605. }),
  606. ],
  607. ),
  608. ],
  609. );
  610. return CustomAlertDialog(
  611. title: Text(translate('Incoming Print Job')),
  612. content: content,
  613. actions: [
  614. dialogButton('OK', onPressed: onSubmit),
  615. dialogButton('Cancel', onPressed: onCancel),
  616. ],
  617. onSubmit: onSubmit,
  618. onCancel: onCancel,
  619. );
  620. });
  621. }
  622. _handleUseTextureRender(
  623. Map<String, dynamic> evt, SessionID sessionId, String peerId) {
  624. parent.target?.imageModel.setUseTextureRender(evt['v'] == 'Y');
  625. waitForFirstImage.value = true;
  626. isRefreshing = true;
  627. showConnectedWaitingForImage(parent.target!.dialogManager, sessionId,
  628. 'success', 'Successful', kMsgboxTextWaitingForImage);
  629. }
  630. _handleSyncPeerOption(Map<String, dynamic> evt, String peer) {
  631. final k = evt['k'];
  632. final v = evt['v'];
  633. if (k == kOptionToggleViewOnly) {
  634. setViewOnly(peer, v as bool);
  635. } else if (k == 'keyboard_mode') {
  636. parent.target?.inputModel.updateKeyboardMode();
  637. } else if (k == 'input_source') {
  638. stateGlobal.getInputSource(force: true);
  639. }
  640. }
  641. onUrlSchemeReceived(Map<String, dynamic> evt) {
  642. final url = evt['url'].toString().trim();
  643. if (url.startsWith(bind.mainUriPrefixSync()) &&
  644. handleUriLink(uriString: url)) {
  645. return;
  646. }
  647. switch (url) {
  648. case kUrlActionClose:
  649. debugPrint("closing all instances");
  650. Future.microtask(() async {
  651. await rustDeskWinManager.closeAllSubWindows();
  652. windowManager.close();
  653. });
  654. break;
  655. default:
  656. windowOnTop(null);
  657. break;
  658. }
  659. }
  660. /// Bind the event listener to receive events from the Rust core.
  661. updateEventListener(SessionID sessionId, String peerId) {
  662. platformFFI.setEventCallback(startEventListener(sessionId, peerId));
  663. }
  664. _handlePortableServiceRunning(String peerId, Map<String, dynamic> evt) {
  665. final running = evt['running'] == 'true';
  666. parent.target?.elevationModel.onPortableServiceRunning(running);
  667. }
  668. handleAliasChanged(Map<String, dynamic> evt) {
  669. if (!(isDesktop || isWebDesktop)) return;
  670. final String peerId = evt['id'];
  671. final String alias = evt['alias'];
  672. String label = getDesktopTabLabel(peerId, alias);
  673. final rxTabLabel = PeerStringOption.find(evt['id'], 'tabLabel');
  674. if (rxTabLabel.value != label) {
  675. rxTabLabel.value = label;
  676. }
  677. }
  678. updateCurDisplay(SessionID sessionId, {updateCursorPos = false}) {
  679. final newRect = displaysRect();
  680. if (newRect == null) {
  681. return;
  682. }
  683. if (newRect != _rect) {
  684. if (newRect.left != _rect?.left || newRect.top != _rect?.top) {
  685. parent.target?.cursorModel.updateDisplayOrigin(
  686. newRect.left, newRect.top,
  687. updateCursorPos: updateCursorPos);
  688. }
  689. _rect = newRect;
  690. parent.target?.canvasModel
  691. .updateViewStyle(refreshMousePos: updateCursorPos);
  692. _updateSessionWidthHeight(sessionId);
  693. }
  694. }
  695. handleSwitchDisplay(
  696. Map<String, dynamic> evt, SessionID sessionId, String peerId) {
  697. final display = int.parse(evt['display']);
  698. if (_pi.currentDisplay != kAllDisplayValue) {
  699. if (bind.peerGetSessionsCount(
  700. id: peerId, connType: parent.target!.connType.index) >
  701. 1) {
  702. if (display != _pi.currentDisplay) {
  703. return;
  704. }
  705. }
  706. if (!_pi.isSupportMultiUiSession) {
  707. _pi.currentDisplay = display;
  708. }
  709. // If `isSupportMultiUiSession` is true, the switch display message should not be used to update current display.
  710. // It is only used to update the display info.
  711. }
  712. var newDisplay = Display();
  713. newDisplay.x = double.tryParse(evt['x']) ?? newDisplay.x;
  714. newDisplay.y = double.tryParse(evt['y']) ?? newDisplay.y;
  715. newDisplay.width = int.tryParse(evt['width']) ?? newDisplay.width;
  716. newDisplay.height = int.tryParse(evt['height']) ?? newDisplay.height;
  717. newDisplay.cursorEmbedded = int.tryParse(evt['cursor_embedded']) == 1;
  718. newDisplay.originalWidth = int.tryParse(
  719. evt['original_width'] ?? kInvalidResolutionValue.toString()) ??
  720. kInvalidResolutionValue;
  721. newDisplay.originalHeight = int.tryParse(
  722. evt['original_height'] ?? kInvalidResolutionValue.toString()) ??
  723. kInvalidResolutionValue;
  724. newDisplay._scale = _pi.scaleOfDisplay(display);
  725. _pi.displays[display] = newDisplay;
  726. if (!_pi.isSupportMultiUiSession || _pi.currentDisplay == display) {
  727. updateCurDisplay(sessionId);
  728. }
  729. if (!_pi.isSupportMultiUiSession) {
  730. try {
  731. CurrentDisplayState.find(peerId).value = display;
  732. } catch (e) {
  733. //
  734. }
  735. }
  736. if (!_pi.isSupportMultiUiSession || _pi.currentDisplay == display) {
  737. handleResolutions(peerId, evt['resolutions']);
  738. }
  739. notifyListeners();
  740. }
  741. cancelMsgBox(Map<String, dynamic> evt, SessionID sessionId) {
  742. if (parent.target == null) return;
  743. final dialogManager = parent.target!.dialogManager;
  744. final tag = '$sessionId-${evt['tag']}';
  745. dialogManager.dismissByTag(tag);
  746. }
  747. handleMultipleWindowsSession(
  748. Map<String, dynamic> evt, SessionID sessionId, String peerId) {
  749. if (parent.target == null) return;
  750. final dialogManager = parent.target!.dialogManager;
  751. final sessions = evt['windows_sessions'];
  752. final title = translate('Multiple Windows sessions found');
  753. final text = translate('Please select the session you want to connect to');
  754. final type = "";
  755. showWindowsSessionsDialog(
  756. type, title, text, dialogManager, sessionId, peerId, sessions);
  757. }
  758. /// Handle the message box event based on [evt] and [id].
  759. handleMsgBox(Map<String, dynamic> evt, SessionID sessionId, String peerId) {
  760. if (parent.target == null) return;
  761. final dialogManager = parent.target!.dialogManager;
  762. final type = evt['type'];
  763. final title = evt['title'];
  764. final text = evt['text'];
  765. final link = evt['link'];
  766. if (type == 're-input-password') {
  767. wrongPasswordDialog(sessionId, dialogManager, type, title, text);
  768. } else if (type == 'input-2fa') {
  769. enter2FaDialog(sessionId, dialogManager);
  770. } else if (type == 'input-password') {
  771. enterPasswordDialog(sessionId, dialogManager);
  772. } else if (type == 'session-login' || type == 'session-re-login') {
  773. enterUserLoginDialog(sessionId, dialogManager);
  774. } else if (type == 'session-login-password' ||
  775. type == 'session-login-password') {
  776. enterUserLoginAndPasswordDialog(sessionId, dialogManager);
  777. } else if (type == 'restarting') {
  778. showMsgBox(sessionId, type, title, text, link, false, dialogManager,
  779. hasCancel: false);
  780. } else if (type == 'wait-remote-accept-nook') {
  781. showWaitAcceptDialog(sessionId, type, title, text, dialogManager);
  782. } else if (type == 'on-uac' || type == 'on-foreground-elevated') {
  783. showOnBlockDialog(sessionId, type, title, text, dialogManager);
  784. } else if (type == 'wait-uac') {
  785. showWaitUacDialog(sessionId, dialogManager, type);
  786. } else if (type == 'elevation-error') {
  787. showElevationError(sessionId, type, title, text, dialogManager);
  788. } else if (type == 'relay-hint' || type == 'relay-hint2') {
  789. showRelayHintDialog(sessionId, type, title, text, dialogManager, peerId);
  790. } else if (text == kMsgboxTextWaitingForImage) {
  791. showConnectedWaitingForImage(dialogManager, sessionId, type, title, text);
  792. } else if (title == 'Privacy mode') {
  793. final hasRetry = evt['hasRetry'] == 'true';
  794. showPrivacyFailedDialog(
  795. sessionId, type, title, text, link, hasRetry, dialogManager);
  796. } else {
  797. final hasRetry = evt['hasRetry'] == 'true';
  798. showMsgBox(sessionId, type, title, text, link, hasRetry, dialogManager);
  799. }
  800. }
  801. handleToast(Map<String, dynamic> evt, SessionID sessionId, String peerId) {
  802. final type = evt['type'] ?? 'info';
  803. final text = evt['text'] ?? '';
  804. final durMsc = evt['dur_msec'] ?? 2000;
  805. final duration = Duration(milliseconds: durMsc);
  806. if ((text).isEmpty) {
  807. BotToast.showLoading(
  808. duration: duration,
  809. clickClose: true,
  810. allowClick: true,
  811. );
  812. } else {
  813. if (type.contains('error')) {
  814. BotToast.showText(
  815. contentColor: Colors.red,
  816. text: translate(text),
  817. duration: duration,
  818. clickClose: true,
  819. onlyOne: true,
  820. );
  821. } else {
  822. BotToast.showText(
  823. text: translate(text),
  824. duration: duration,
  825. clickClose: true,
  826. onlyOne: true,
  827. );
  828. }
  829. }
  830. }
  831. /// Show a message box with [type], [title] and [text].
  832. showMsgBox(SessionID sessionId, String type, String title, String text,
  833. String link, bool hasRetry, OverlayDialogManager dialogManager,
  834. {bool? hasCancel}) {
  835. msgBox(sessionId, type, title, text, link, dialogManager,
  836. hasCancel: hasCancel,
  837. reconnect: hasRetry ? reconnect : null,
  838. reconnectTimeout: hasRetry ? _reconnects : null);
  839. _timer?.cancel();
  840. if (hasRetry) {
  841. _timer = Timer(Duration(seconds: _reconnects), () {
  842. reconnect(dialogManager, sessionId, false);
  843. });
  844. _reconnects *= 2;
  845. } else {
  846. _reconnects = 1;
  847. }
  848. }
  849. void reconnect(OverlayDialogManager dialogManager, SessionID sessionId,
  850. bool forceRelay) {
  851. bind.sessionReconnect(sessionId: sessionId, forceRelay: forceRelay);
  852. clearPermissions();
  853. dialogManager.dismissAll();
  854. dialogManager.showLoading(translate('Connecting...'),
  855. onCancel: closeConnection);
  856. }
  857. void showRelayHintDialog(SessionID sessionId, String type, String title,
  858. String text, OverlayDialogManager dialogManager, String peerId) {
  859. dialogManager.show(tag: '$sessionId-$type', (setState, close, context) {
  860. onClose() {
  861. closeConnection();
  862. close();
  863. }
  864. final style =
  865. ElevatedButton.styleFrom(backgroundColor: Colors.green[700]);
  866. var hint = "\n\n${translate('relay_hint_tip')}";
  867. if (text.contains("10054") || text.contains("104")) {
  868. hint = "";
  869. }
  870. return CustomAlertDialog(
  871. title: null,
  872. content: msgboxContent(type, title, "${translate(text)}$hint"),
  873. actions: [
  874. dialogButton('Close', onPressed: onClose, isOutline: true),
  875. if (type == 'relay-hint')
  876. dialogButton('Connect via relay',
  877. onPressed: () => reconnect(dialogManager, sessionId, true),
  878. buttonStyle: style,
  879. isOutline: true),
  880. dialogButton('Retry',
  881. onPressed: () => reconnect(dialogManager, sessionId, false)),
  882. if (type == 'relay-hint2')
  883. dialogButton('Connect via relay',
  884. onPressed: () => reconnect(dialogManager, sessionId, true),
  885. buttonStyle: style),
  886. ],
  887. onCancel: onClose,
  888. );
  889. });
  890. }
  891. void showConnectedWaitingForImage(OverlayDialogManager dialogManager,
  892. SessionID sessionId, String type, String title, String text) {
  893. onClose() {
  894. closeConnection();
  895. }
  896. if (waitForFirstImage.isFalse) return;
  897. dialogManager.show(
  898. (setState, close, context) => CustomAlertDialog(
  899. title: null,
  900. content: SelectionArea(child: msgboxContent(type, title, text)),
  901. actions: [
  902. dialogButton("Cancel", onPressed: onClose, isOutline: true)
  903. ],
  904. onCancel: onClose),
  905. tag: '$sessionId-waiting-for-image',
  906. );
  907. waitForImageDialogShow.value = true;
  908. waitForImageTimer = Timer(Duration(milliseconds: 1500), () {
  909. if (waitForFirstImage.isTrue && !isRefreshing) {
  910. bind.sessionInputOsPassword(sessionId: sessionId, value: '');
  911. }
  912. });
  913. bind.sessionOnWaitingForImageDialogShow(sessionId: sessionId);
  914. }
  915. void showPrivacyFailedDialog(
  916. SessionID sessionId,
  917. String type,
  918. String title,
  919. String text,
  920. String link,
  921. bool hasRetry,
  922. OverlayDialogManager dialogManager) {
  923. if (text == 'no_need_privacy_mode_no_physical_displays_tip' ||
  924. text == 'Enter privacy mode') {
  925. // There are display changes on the remote side,
  926. // which will cause some messages to refresh the canvas and dismiss dialogs.
  927. // So we add a delay here to ensure the dialog is displayed.
  928. Future.delayed(Duration(milliseconds: 3000), () {
  929. showMsgBox(sessionId, type, title, text, link, hasRetry, dialogManager);
  930. });
  931. } else {
  932. showMsgBox(sessionId, type, title, text, link, hasRetry, dialogManager);
  933. }
  934. }
  935. _updateSessionWidthHeight(SessionID sessionId) {
  936. if (_rect == null) return;
  937. if (_rect!.width <= 0 || _rect!.height <= 0) {
  938. debugPrintStack(
  939. label: 'invalid display size (${_rect!.width},${_rect!.height})');
  940. } else {
  941. final displays = _pi.getCurDisplays();
  942. if (displays.length == 1) {
  943. bind.sessionSetSize(
  944. sessionId: sessionId,
  945. display:
  946. pi.currentDisplay == kAllDisplayValue ? 0 : pi.currentDisplay,
  947. width: _rect!.width.toInt(),
  948. height: _rect!.height.toInt(),
  949. );
  950. } else {
  951. for (int i = 0; i < displays.length; ++i) {
  952. bind.sessionSetSize(
  953. sessionId: sessionId,
  954. display: i,
  955. width: displays[i].width.toInt(),
  956. height: displays[i].height.toInt(),
  957. );
  958. }
  959. }
  960. }
  961. }
  962. /// Handle the peer info event based on [evt].
  963. handlePeerInfo(Map<String, dynamic> evt, String peerId, bool isCache) async {
  964. parent.target?.chatModel.voiceCallStatus.value = VoiceCallStatus.notStarted;
  965. // This call is to ensuer the keyboard mode is updated depending on the peer version.
  966. parent.target?.inputModel.updateKeyboardMode();
  967. // Map clone is required here, otherwise "evt" may be changed by other threads through the reference.
  968. // Because this function is asynchronous, there's an "await" in this function.
  969. cachedPeerData.peerInfo = {...evt};
  970. // Do not cache resolutions, because a new display connection have different resolutions.
  971. cachedPeerData.peerInfo.remove('resolutions');
  972. // Recent peer is updated by handle_peer_info(ui_session_interface.rs) --> handle_peer_info(client.rs) --> save_config(client.rs)
  973. bind.mainLoadRecentPeers();
  974. parent.target?.dialogManager.dismissAll();
  975. _pi.version = evt['version'];
  976. _pi.isSupportMultiUiSession =
  977. bind.isSupportMultiUiSession(version: _pi.version);
  978. _pi.username = evt['username'];
  979. _pi.hostname = evt['hostname'];
  980. _pi.platform = evt['platform'];
  981. _pi.sasEnabled = evt['sas_enabled'] == 'true';
  982. final currentDisplay = int.parse(evt['current_display']);
  983. if (_pi.primaryDisplay == kInvalidDisplayIndex) {
  984. _pi.primaryDisplay = currentDisplay;
  985. }
  986. if (bind.peerGetSessionsCount(
  987. id: peerId, connType: parent.target!.connType.index) <=
  988. 1) {
  989. _pi.currentDisplay = currentDisplay;
  990. }
  991. try {
  992. CurrentDisplayState.find(peerId).value = _pi.currentDisplay;
  993. } catch (e) {
  994. //
  995. }
  996. final connType = parent.target?.connType;
  997. if (isPeerAndroid) {
  998. _touchMode = true;
  999. } else {
  1000. _touchMode = await bind.sessionGetOption(
  1001. sessionId: sessionId, arg: kOptionTouchMode) !=
  1002. '';
  1003. }
  1004. // FIXME: handle ViewCamera ConnType independently.
  1005. if (connType == ConnType.fileTransfer) {
  1006. parent.target?.fileModel.onReady();
  1007. } else if (connType == ConnType.defaultConn ||
  1008. connType == ConnType.viewCamera) {
  1009. List<Display> newDisplays = [];
  1010. List<dynamic> displays = json.decode(evt['displays']);
  1011. for (int i = 0; i < displays.length; ++i) {
  1012. newDisplays.add(evtToDisplay(displays[i]));
  1013. }
  1014. _pi.displays.value = newDisplays;
  1015. _pi.displaysCount.value = _pi.displays.length;
  1016. if (_pi.currentDisplay < _pi.displays.length) {
  1017. // now replaced to _updateCurDisplay
  1018. updateCurDisplay(sessionId);
  1019. }
  1020. if (displays.isNotEmpty) {
  1021. _reconnects = 1;
  1022. waitForFirstImage.value = true;
  1023. isRefreshing = false;
  1024. }
  1025. Map<String, dynamic> features = json.decode(evt['features']);
  1026. _pi.features.privacyMode = features['privacy_mode'] == true;
  1027. if (!isCache) {
  1028. handleResolutions(peerId, evt["resolutions"]);
  1029. }
  1030. parent.target?.elevationModel.onPeerInfo(_pi);
  1031. }
  1032. if (connType == ConnType.defaultConn) {
  1033. setViewOnly(
  1034. peerId,
  1035. bind.sessionGetToggleOptionSync(
  1036. sessionId: sessionId, arg: kOptionToggleViewOnly));
  1037. }
  1038. if (connType == ConnType.defaultConn || connType == ConnType.viewCamera) {
  1039. final platformAdditions = evt['platform_additions'];
  1040. if (platformAdditions != null && platformAdditions != '') {
  1041. try {
  1042. _pi.platformAdditions = json.decode(platformAdditions);
  1043. } catch (e) {
  1044. debugPrint('Failed to decode platformAdditions $e');
  1045. }
  1046. }
  1047. }
  1048. _pi.isSet.value = true;
  1049. stateGlobal.resetLastResolutionGroupValues(peerId);
  1050. if (isDesktop || isWebDesktop) {
  1051. checkDesktopKeyboardMode();
  1052. }
  1053. notifyListeners();
  1054. if (!isCache) {
  1055. tryUseAllMyDisplaysForTheRemoteSession(peerId);
  1056. }
  1057. }
  1058. checkDesktopKeyboardMode() async {
  1059. if (isInputSourceFlutter) {
  1060. // Local side, flutter keyboard input source
  1061. // Currently only map mode is supported, legacy mode is used for compatibility.
  1062. for (final mode in [kKeyMapMode, kKeyLegacyMode]) {
  1063. if (bind.sessionIsKeyboardModeSupported(
  1064. sessionId: sessionId, mode: mode)) {
  1065. await bind.sessionSetKeyboardMode(sessionId: sessionId, value: mode);
  1066. break;
  1067. }
  1068. }
  1069. } else {
  1070. final curMode = await bind.sessionGetKeyboardMode(sessionId: sessionId);
  1071. if (curMode != null) {
  1072. if (bind.sessionIsKeyboardModeSupported(
  1073. sessionId: sessionId, mode: curMode)) {
  1074. return;
  1075. }
  1076. }
  1077. // If current keyboard mode is not supported, change to another one.
  1078. for (final mode in [kKeyMapMode, kKeyTranslateMode, kKeyLegacyMode]) {
  1079. if (bind.sessionIsKeyboardModeSupported(
  1080. sessionId: sessionId, mode: mode)) {
  1081. bind.sessionSetKeyboardMode(sessionId: sessionId, value: mode);
  1082. break;
  1083. }
  1084. }
  1085. }
  1086. }
  1087. tryUseAllMyDisplaysForTheRemoteSession(String peerId) async {
  1088. if (bind.sessionGetUseAllMyDisplaysForTheRemoteSession(
  1089. sessionId: sessionId) !=
  1090. 'Y') {
  1091. return;
  1092. }
  1093. if (!_pi.isSupportMultiDisplay || _pi.displays.length <= 1) {
  1094. return;
  1095. }
  1096. final screenRectList = await getScreenRectList();
  1097. if (screenRectList.length <= 1) {
  1098. return;
  1099. }
  1100. // to-do: peer currentDisplay is the primary display, but the primary display may not be the first display.
  1101. // local primary display also may not be the first display.
  1102. //
  1103. // 0 is assumed to be the primary display here, for now.
  1104. // move to the first display and set fullscreen
  1105. bind.sessionSwitchDisplay(
  1106. isDesktop: isDesktop,
  1107. sessionId: sessionId,
  1108. value: Int32List.fromList([0]),
  1109. );
  1110. _pi.currentDisplay = 0;
  1111. try {
  1112. CurrentDisplayState.find(peerId).value = _pi.currentDisplay;
  1113. } catch (e) {
  1114. //
  1115. }
  1116. await tryMoveToScreenAndSetFullscreen(screenRectList[0]);
  1117. final length = _pi.displays.length < screenRectList.length
  1118. ? _pi.displays.length
  1119. : screenRectList.length;
  1120. for (var i = 1; i < length; i++) {
  1121. openMonitorInNewTabOrWindow(i, peerId, _pi,
  1122. screenRect: screenRectList[i]);
  1123. }
  1124. }
  1125. tryShowAndroidActionsOverlay({int delayMSecs = 10}) {
  1126. if (isPeerAndroid) {
  1127. if (parent.target?.connType == ConnType.defaultConn &&
  1128. parent.target != null &&
  1129. parent.target!.ffiModel.permissions['keyboard'] != false) {
  1130. Timer(Duration(milliseconds: delayMSecs), () {
  1131. if (parent.target!.dialogManager.mobileActionsOverlayVisible.isTrue) {
  1132. parent.target!.dialogManager
  1133. .showMobileActionsOverlay(ffi: parent.target!);
  1134. }
  1135. });
  1136. }
  1137. }
  1138. }
  1139. handleResolutions(String id, dynamic resolutions) {
  1140. try {
  1141. final resolutionsObj = json.decode(resolutions as String);
  1142. late List<dynamic> dynamicArray;
  1143. if (resolutionsObj is Map) {
  1144. // The web version
  1145. dynamicArray = (resolutionsObj as Map<String, dynamic>)['resolutions']
  1146. as List<dynamic>;
  1147. } else {
  1148. // The rust version
  1149. dynamicArray = resolutionsObj as List<dynamic>;
  1150. }
  1151. List<Resolution> arr = List.empty(growable: true);
  1152. for (int i = 0; i < dynamicArray.length; i++) {
  1153. var width = dynamicArray[i]["width"];
  1154. var height = dynamicArray[i]["height"];
  1155. if (width is int && width > 0 && height is int && height > 0) {
  1156. arr.add(Resolution(width, height));
  1157. }
  1158. }
  1159. arr.sort((a, b) {
  1160. if (b.width != a.width) {
  1161. return b.width - a.width;
  1162. } else {
  1163. return b.height - a.height;
  1164. }
  1165. });
  1166. _pi.resolutions = arr;
  1167. } catch (e) {
  1168. debugPrint("Failed to parse resolutions:$e");
  1169. }
  1170. }
  1171. Display evtToDisplay(Map<String, dynamic> evt) {
  1172. var d = Display();
  1173. d.x = evt['x']?.toDouble() ?? d.x;
  1174. d.y = evt['y']?.toDouble() ?? d.y;
  1175. d.width = evt['width'] ?? d.width;
  1176. d.height = evt['height'] ?? d.height;
  1177. d.cursorEmbedded = evt['cursor_embedded'] == 1;
  1178. d.originalWidth = evt['original_width'] ?? kInvalidResolutionValue;
  1179. d.originalHeight = evt['original_height'] ?? kInvalidResolutionValue;
  1180. double v = (evt['scale']?.toDouble() ?? 100.0) / 100;
  1181. d._scale = v > 1.0 ? v : 1.0;
  1182. return d;
  1183. }
  1184. updateLastCursorId(Map<String, dynamic> evt) {
  1185. // int.parse(evt['id']) may cause FormatException
  1186. // Unhandled Exception: FormatException: Positive input exceeds the limit of integer 18446744071749110741
  1187. parent.target?.cursorModel.id = evt['id'];
  1188. }
  1189. handleCursorId(Map<String, dynamic> evt) {
  1190. cachedPeerData.lastCursorId = evt;
  1191. parent.target?.cursorModel.updateCursorId(evt);
  1192. }
  1193. handleCursorData(Map<String, dynamic> evt) async {
  1194. cachedPeerData.cursorDataList.add(evt);
  1195. await parent.target?.cursorModel.updateCursorData(evt);
  1196. }
  1197. /// Handle the peer info synchronization event based on [evt].
  1198. handleSyncPeerInfo(
  1199. Map<String, dynamic> evt, SessionID sessionId, String peerId) async {
  1200. if (evt['displays'] != null) {
  1201. cachedPeerData.peerInfo['displays'] = evt['displays'];
  1202. List<dynamic> displays = json.decode(evt['displays']);
  1203. List<Display> newDisplays = [];
  1204. for (int i = 0; i < displays.length; ++i) {
  1205. newDisplays.add(evtToDisplay(displays[i]));
  1206. }
  1207. _pi.displays.value = newDisplays;
  1208. _pi.displaysCount.value = _pi.displays.length;
  1209. if (_pi.currentDisplay == kAllDisplayValue) {
  1210. updateCurDisplay(sessionId);
  1211. // to-do: What if the displays are changed?
  1212. } else {
  1213. if (_pi.currentDisplay >= 0 &&
  1214. _pi.currentDisplay < _pi.displays.length) {
  1215. updateCurDisplay(sessionId);
  1216. } else {
  1217. if (_pi.displays.isNotEmpty) {
  1218. // Notify to switch display
  1219. msgBox(sessionId, 'custom-nook-nocancel-hasclose-info', 'Prompt',
  1220. 'display_is_plugged_out_msg', '', parent.target!.dialogManager);
  1221. final isPeerPrimaryDisplayValid =
  1222. pi.primaryDisplay == kInvalidDisplayIndex ||
  1223. pi.primaryDisplay >= pi.displays.length;
  1224. final newDisplay =
  1225. isPeerPrimaryDisplayValid ? 0 : pi.primaryDisplay;
  1226. bind.sessionSwitchDisplay(
  1227. isDesktop: isDesktop,
  1228. sessionId: sessionId,
  1229. value: Int32List.fromList([newDisplay]),
  1230. );
  1231. if (_pi.isSupportMultiUiSession) {
  1232. // If the peer supports multi-ui-session, no switch display message will be send back.
  1233. // We need to update the display manually.
  1234. switchToNewDisplay(newDisplay, sessionId, peerId);
  1235. }
  1236. } else {
  1237. msgBox(sessionId, 'nocancel-error', 'Prompt', 'No Displays', '',
  1238. parent.target!.dialogManager);
  1239. }
  1240. }
  1241. }
  1242. }
  1243. parent.target!.canvasModel
  1244. .tryUpdateScrollStyle(Duration(milliseconds: 300), null);
  1245. notifyListeners();
  1246. }
  1247. handlePlatformAdditions(
  1248. Map<String, dynamic> evt, SessionID sessionId, String peerId) async {
  1249. final updateData = evt['platform_additions'] as String?;
  1250. if (updateData == null) {
  1251. return;
  1252. }
  1253. if (updateData.isEmpty) {
  1254. _pi.platformAdditions.remove(kPlatformAdditionsRustDeskVirtualDisplays);
  1255. _pi.platformAdditions.remove(kPlatformAdditionsAmyuniVirtualDisplays);
  1256. } else {
  1257. try {
  1258. final updateJson = json.decode(updateData) as Map<String, dynamic>;
  1259. for (final key in updateJson.keys) {
  1260. _pi.platformAdditions[key] = updateJson[key];
  1261. }
  1262. if (!updateJson
  1263. .containsKey(kPlatformAdditionsRustDeskVirtualDisplays)) {
  1264. _pi.platformAdditions
  1265. .remove(kPlatformAdditionsRustDeskVirtualDisplays);
  1266. }
  1267. if (!updateJson.containsKey(kPlatformAdditionsAmyuniVirtualDisplays)) {
  1268. _pi.platformAdditions.remove(kPlatformAdditionsAmyuniVirtualDisplays);
  1269. }
  1270. } catch (e) {
  1271. debugPrint('Failed to decode platformAdditions $e');
  1272. }
  1273. }
  1274. cachedPeerData.peerInfo['platform_additions'] =
  1275. json.encode(_pi.platformAdditions);
  1276. }
  1277. handleFollowCurrentDisplay(
  1278. Map<String, dynamic> evt, SessionID sessionId, String peerId) async {
  1279. if (evt['display_idx'] != null) {
  1280. if (pi.currentDisplay == kAllDisplayValue) {
  1281. return;
  1282. }
  1283. _pi.currentDisplay = int.parse(evt['display_idx']);
  1284. try {
  1285. CurrentDisplayState.find(peerId).value = _pi.currentDisplay;
  1286. } catch (e) {
  1287. //
  1288. }
  1289. bind.sessionSwitchDisplay(
  1290. isDesktop: isDesktop,
  1291. sessionId: sessionId,
  1292. value: Int32List.fromList([_pi.currentDisplay]),
  1293. );
  1294. }
  1295. notifyListeners();
  1296. }
  1297. // Directly switch to the new display without waiting for the response.
  1298. switchToNewDisplay(int display, SessionID sessionId, String peerId,
  1299. {bool updateCursorPos = false}) {
  1300. // no need to wait for the response
  1301. pi.currentDisplay = display;
  1302. updateCurDisplay(sessionId, updateCursorPos: updateCursorPos);
  1303. try {
  1304. CurrentDisplayState.find(peerId).value = display;
  1305. } catch (e) {
  1306. //
  1307. }
  1308. }
  1309. updateBlockInputState(Map<String, dynamic> evt, String peerId) {
  1310. _inputBlocked = evt['input_state'] == 'on';
  1311. notifyListeners();
  1312. try {
  1313. BlockInputState.find(peerId).value = evt['input_state'] == 'on';
  1314. } catch (e) {
  1315. //
  1316. }
  1317. }
  1318. updatePrivacyMode(
  1319. Map<String, dynamic> evt, SessionID sessionId, String peerId) async {
  1320. notifyListeners();
  1321. try {
  1322. final isOn = bind.sessionGetToggleOptionSync(
  1323. sessionId: sessionId, arg: 'privacy-mode');
  1324. if (isOn) {
  1325. var privacyModeImpl = await bind.sessionGetOption(
  1326. sessionId: sessionId, arg: 'privacy-mode-impl-key');
  1327. // For compatibility, version < 1.2.4, the default value is 'privacy_mode_impl_mag'.
  1328. final initDefaultPrivacyMode = 'privacy_mode_impl_mag';
  1329. PrivacyModeState.find(peerId).value =
  1330. privacyModeImpl ?? initDefaultPrivacyMode;
  1331. } else {
  1332. PrivacyModeState.find(peerId).value = '';
  1333. }
  1334. } catch (e) {
  1335. //
  1336. }
  1337. }
  1338. void setViewOnly(String id, bool value) {
  1339. if (versionCmp(_pi.version, '1.2.0') < 0) return;
  1340. // tmp fix for https://github.com/rustdesk/rustdesk/pull/3706#issuecomment-1481242389
  1341. // because below rx not used in mobile version, so not initialized, below code will cause crash
  1342. // current our flutter code quality is fucking shit now. !!!!!!!!!!!!!!!!
  1343. try {
  1344. if (value) {
  1345. ShowRemoteCursorState.find(id).value = value;
  1346. } else {
  1347. ShowRemoteCursorState.find(id).value = bind.sessionGetToggleOptionSync(
  1348. sessionId: sessionId, arg: 'show-remote-cursor');
  1349. }
  1350. } catch (e) {
  1351. //
  1352. }
  1353. if (_viewOnly != value) {
  1354. _viewOnly = value;
  1355. notifyListeners();
  1356. }
  1357. }
  1358. }
  1359. class ImageModel with ChangeNotifier {
  1360. ui.Image? _image;
  1361. ui.Image? get image => _image;
  1362. String id = '';
  1363. late final SessionID sessionId;
  1364. bool _useTextureRender = false;
  1365. WeakReference<FFI> parent;
  1366. final List<Function(String)> callbacksOnFirstImage = [];
  1367. ImageModel(this.parent) {
  1368. sessionId = parent.target!.sessionId;
  1369. }
  1370. get useTextureRender => _useTextureRender;
  1371. addCallbackOnFirstImage(Function(String) cb) => callbacksOnFirstImage.add(cb);
  1372. clearImage() => _image = null;
  1373. bool _webDecodingRgba = false;
  1374. final List<Uint8List> _webRgbaList = List.empty(growable: true);
  1375. webOnRgba(int display, Uint8List rgba) async {
  1376. // deep copy needed, otherwise "instantiateCodec failed: TypeError: Cannot perform Construct on a detached ArrayBuffer"
  1377. _webRgbaList.add(Uint8List.fromList(rgba));
  1378. if (_webDecodingRgba) {
  1379. return;
  1380. }
  1381. _webDecodingRgba = true;
  1382. try {
  1383. while (_webRgbaList.isNotEmpty) {
  1384. final rgba2 = _webRgbaList.last;
  1385. _webRgbaList.clear();
  1386. await decodeAndUpdate(display, rgba2);
  1387. }
  1388. } catch (e) {
  1389. debugPrint('onRgba error: $e');
  1390. }
  1391. _webDecodingRgba = false;
  1392. }
  1393. onRgba(int display, Uint8List rgba) async {
  1394. try {
  1395. await decodeAndUpdate(display, rgba);
  1396. } catch (e) {
  1397. debugPrint('onRgba error: $e');
  1398. }
  1399. platformFFI.nextRgba(sessionId, display);
  1400. }
  1401. decodeAndUpdate(int display, Uint8List rgba) async {
  1402. final pid = parent.target?.id;
  1403. final rect = parent.target?.ffiModel.pi.getDisplayRect(display);
  1404. final image = await img.decodeImageFromPixels(
  1405. rgba,
  1406. rect?.width.toInt() ?? 0,
  1407. rect?.height.toInt() ?? 0,
  1408. isWeb | isWindows | isLinux
  1409. ? ui.PixelFormat.rgba8888
  1410. : ui.PixelFormat.bgra8888,
  1411. );
  1412. if (parent.target?.id != pid) return;
  1413. await update(image);
  1414. }
  1415. update(ui.Image? image) async {
  1416. if (_image == null && image != null) {
  1417. if (isDesktop || isWebDesktop) {
  1418. await parent.target?.canvasModel.updateViewStyle();
  1419. await parent.target?.canvasModel.updateScrollStyle();
  1420. }
  1421. if (parent.target != null) {
  1422. await initializeCursorAndCanvas(parent.target!);
  1423. }
  1424. }
  1425. _image?.dispose();
  1426. _image = image;
  1427. if (image != null) notifyListeners();
  1428. }
  1429. // mobile only
  1430. double get maxScale {
  1431. if (_image == null) return 1.5;
  1432. final size = parent.target!.canvasModel.getSize();
  1433. final xscale = size.width / _image!.width;
  1434. final yscale = size.height / _image!.height;
  1435. return max(1.5, max(xscale, yscale));
  1436. }
  1437. // mobile only
  1438. double get minScale {
  1439. if (_image == null) return 1.5;
  1440. final size = parent.target!.canvasModel.getSize();
  1441. final xscale = size.width / _image!.width;
  1442. final yscale = size.height / _image!.height;
  1443. return min(xscale, yscale) / 1.5;
  1444. }
  1445. updateUserTextureRender() {
  1446. final preValue = _useTextureRender;
  1447. _useTextureRender = isDesktop && bind.mainGetUseTextureRender();
  1448. if (preValue != _useTextureRender) {
  1449. notifyListeners();
  1450. }
  1451. }
  1452. setUseTextureRender(bool value) {
  1453. _useTextureRender = value;
  1454. notifyListeners();
  1455. }
  1456. void disposeImage() {
  1457. _image?.dispose();
  1458. _image = null;
  1459. }
  1460. }
  1461. enum ScrollStyle {
  1462. scrollbar,
  1463. scrollauto,
  1464. }
  1465. class ViewStyle {
  1466. final String style;
  1467. final double width;
  1468. final double height;
  1469. final int displayWidth;
  1470. final int displayHeight;
  1471. ViewStyle({
  1472. required this.style,
  1473. required this.width,
  1474. required this.height,
  1475. required this.displayWidth,
  1476. required this.displayHeight,
  1477. });
  1478. static defaultViewStyle() {
  1479. final desktop = (isDesktop || isWebDesktop);
  1480. final w =
  1481. desktop ? kDesktopDefaultDisplayWidth : kMobileDefaultDisplayWidth;
  1482. final h =
  1483. desktop ? kDesktopDefaultDisplayHeight : kMobileDefaultDisplayHeight;
  1484. return ViewStyle(
  1485. style: '',
  1486. width: w.toDouble(),
  1487. height: h.toDouble(),
  1488. displayWidth: w,
  1489. displayHeight: h,
  1490. );
  1491. }
  1492. static int _double2Int(double v) => (v * 100).round().toInt();
  1493. @override
  1494. bool operator ==(Object other) =>
  1495. other is ViewStyle &&
  1496. other.runtimeType == runtimeType &&
  1497. _innerEqual(other);
  1498. bool _innerEqual(ViewStyle other) {
  1499. return style == other.style &&
  1500. ViewStyle._double2Int(other.width) == ViewStyle._double2Int(width) &&
  1501. ViewStyle._double2Int(other.height) == ViewStyle._double2Int(height) &&
  1502. other.displayWidth == displayWidth &&
  1503. other.displayHeight == displayHeight;
  1504. }
  1505. @override
  1506. int get hashCode => Object.hash(
  1507. style,
  1508. ViewStyle._double2Int(width),
  1509. ViewStyle._double2Int(height),
  1510. displayWidth,
  1511. displayHeight,
  1512. ).hashCode;
  1513. double get scale {
  1514. double s = 1.0;
  1515. if (style == kRemoteViewStyleAdaptive) {
  1516. if (width != 0 &&
  1517. height != 0 &&
  1518. displayWidth != 0 &&
  1519. displayHeight != 0) {
  1520. final s1 = width / displayWidth;
  1521. final s2 = height / displayHeight;
  1522. s = s1 < s2 ? s1 : s2;
  1523. }
  1524. }
  1525. return s;
  1526. }
  1527. }
  1528. class CanvasModel with ChangeNotifier {
  1529. // image offset of canvas
  1530. double _x = 0;
  1531. // image offset of canvas
  1532. double _y = 0;
  1533. // image scale
  1534. double _scale = 1.0;
  1535. double _devicePixelRatio = 1.0;
  1536. Size _size = Size.zero;
  1537. // the tabbar over the image
  1538. // double tabBarHeight = 0.0;
  1539. // the window border's width
  1540. // double windowBorderWidth = 0.0;
  1541. // remote id
  1542. String id = '';
  1543. late final SessionID sessionId;
  1544. // scroll offset x percent
  1545. double _scrollX = 0.0;
  1546. // scroll offset y percent
  1547. double _scrollY = 0.0;
  1548. ScrollStyle _scrollStyle = ScrollStyle.scrollauto;
  1549. ViewStyle _lastViewStyle = ViewStyle.defaultViewStyle();
  1550. Timer? _timerMobileFocusCanvasCursor;
  1551. // `isMobileCanvasChanged` is used to avoid canvas reset when changing the input method
  1552. // after showing the soft keyboard.
  1553. bool isMobileCanvasChanged = false;
  1554. final ScrollController _horizontal = ScrollController();
  1555. final ScrollController _vertical = ScrollController();
  1556. final _imageOverflow = false.obs;
  1557. WeakReference<FFI> parent;
  1558. CanvasModel(this.parent) {
  1559. sessionId = parent.target!.sessionId;
  1560. }
  1561. double get x => _x;
  1562. double get y => _y;
  1563. double get scale => _scale;
  1564. double get devicePixelRatio => _devicePixelRatio;
  1565. Size get size => _size;
  1566. ScrollStyle get scrollStyle => _scrollStyle;
  1567. ViewStyle get viewStyle => _lastViewStyle;
  1568. RxBool get imageOverflow => _imageOverflow;
  1569. _resetScroll() => setScrollPercent(0.0, 0.0);
  1570. setScrollPercent(double x, double y) {
  1571. _scrollX = x;
  1572. _scrollY = y;
  1573. }
  1574. ScrollController get scrollHorizontal => _horizontal;
  1575. ScrollController get scrollVertical => _vertical;
  1576. double get scrollX => _scrollX;
  1577. double get scrollY => _scrollY;
  1578. static double get leftToEdge =>
  1579. isDesktop ? windowBorderWidth + kDragToResizeAreaPadding.left : 0;
  1580. static double get rightToEdge =>
  1581. isDesktop ? windowBorderWidth + kDragToResizeAreaPadding.right : 0;
  1582. static double get topToEdge => isDesktop
  1583. ? tabBarHeight + windowBorderWidth + kDragToResizeAreaPadding.top
  1584. : 0;
  1585. static double get bottomToEdge =>
  1586. isDesktop ? windowBorderWidth + kDragToResizeAreaPadding.bottom : 0;
  1587. Size getSize() {
  1588. final mediaData = MediaQueryData.fromView(ui.window);
  1589. final size = mediaData.size;
  1590. // If minimized, w or h may be negative here.
  1591. double w = size.width - leftToEdge - rightToEdge;
  1592. double h = size.height - topToEdge - bottomToEdge;
  1593. if (isMobile) {
  1594. h = h -
  1595. mediaData.viewInsets.bottom -
  1596. (parent.target?.cursorModel.keyHelpToolsRectToAdjustCanvas?.bottom ??
  1597. 0);
  1598. }
  1599. return Size(w < 0 ? 0 : w, h < 0 ? 0 : h);
  1600. }
  1601. // mobile only
  1602. double getAdjustY() {
  1603. final bottom =
  1604. parent.target?.cursorModel.keyHelpToolsRectToAdjustCanvas?.bottom ?? 0;
  1605. return max(bottom - MediaQueryData.fromView(ui.window).padding.top, 0);
  1606. }
  1607. updateSize() => _size = getSize();
  1608. updateViewStyle({refreshMousePos = true, notify = true}) async {
  1609. final style = await bind.sessionGetViewStyle(sessionId: sessionId);
  1610. if (style == null) {
  1611. return;
  1612. }
  1613. updateSize();
  1614. final displayWidth = getDisplayWidth();
  1615. final displayHeight = getDisplayHeight();
  1616. final viewStyle = ViewStyle(
  1617. style: style,
  1618. width: size.width,
  1619. height: size.height,
  1620. displayWidth: displayWidth,
  1621. displayHeight: displayHeight,
  1622. );
  1623. if (_lastViewStyle == viewStyle) {
  1624. return;
  1625. }
  1626. if (_lastViewStyle.style != viewStyle.style) {
  1627. _resetScroll();
  1628. }
  1629. _lastViewStyle = viewStyle;
  1630. _scale = viewStyle.scale;
  1631. _devicePixelRatio = ui.window.devicePixelRatio;
  1632. if (kIgnoreDpi && style == kRemoteViewStyleOriginal) {
  1633. _scale = 1.0 / _devicePixelRatio;
  1634. }
  1635. _resetCanvasOffset(displayWidth, displayHeight);
  1636. _imageOverflow.value = _x < 0 || y < 0;
  1637. if (notify) {
  1638. notifyListeners();
  1639. }
  1640. if (!isMobile && refreshMousePos) {
  1641. parent.target?.inputModel.refreshMousePos();
  1642. }
  1643. tryUpdateScrollStyle(Duration.zero, style);
  1644. }
  1645. _resetCanvasOffset(int displayWidth, int displayHeight) {
  1646. _x = (size.width - displayWidth * _scale) / 2;
  1647. _y = (size.height - displayHeight * _scale) / 2;
  1648. if (isMobile) {
  1649. _moveToCenterCursor();
  1650. }
  1651. }
  1652. tryUpdateScrollStyle(Duration duration, String? style) async {
  1653. if (_scrollStyle != ScrollStyle.scrollbar) return;
  1654. style ??= await bind.sessionGetViewStyle(sessionId: sessionId);
  1655. if (style != kRemoteViewStyleOriginal) {
  1656. return;
  1657. }
  1658. _resetScroll();
  1659. Future.delayed(duration, () async {
  1660. updateScrollPercent();
  1661. });
  1662. }
  1663. updateScrollStyle() async {
  1664. final style = await bind.sessionGetScrollStyle(sessionId: sessionId);
  1665. if (style == kRemoteScrollStyleBar) {
  1666. _scrollStyle = ScrollStyle.scrollbar;
  1667. _resetScroll();
  1668. } else {
  1669. _scrollStyle = ScrollStyle.scrollauto;
  1670. }
  1671. notifyListeners();
  1672. }
  1673. update(double x, double y, double scale) {
  1674. _x = x;
  1675. _y = y;
  1676. _scale = scale;
  1677. notifyListeners();
  1678. }
  1679. bool get cursorEmbedded =>
  1680. parent.target?.ffiModel._pi.cursorEmbedded ?? false;
  1681. int getDisplayWidth() {
  1682. final defaultWidth = (isDesktop || isWebDesktop)
  1683. ? kDesktopDefaultDisplayWidth
  1684. : kMobileDefaultDisplayWidth;
  1685. return parent.target?.ffiModel.rect?.width.toInt() ?? defaultWidth;
  1686. }
  1687. int getDisplayHeight() {
  1688. final defaultHeight = (isDesktop || isWebDesktop)
  1689. ? kDesktopDefaultDisplayHeight
  1690. : kMobileDefaultDisplayHeight;
  1691. return parent.target?.ffiModel.rect?.height.toInt() ?? defaultHeight;
  1692. }
  1693. static double get windowBorderWidth => stateGlobal.windowBorderWidth.value;
  1694. static double get tabBarHeight => stateGlobal.tabBarHeight;
  1695. moveDesktopMouse(double x, double y) {
  1696. if (size.width == 0 || size.height == 0) {
  1697. return;
  1698. }
  1699. // On mobile platforms, move the canvas with the cursor.
  1700. final dw = getDisplayWidth() * _scale;
  1701. final dh = getDisplayHeight() * _scale;
  1702. var dxOffset = 0;
  1703. var dyOffset = 0;
  1704. try {
  1705. if (dw > size.width) {
  1706. dxOffset = (x - dw * (x / size.width) - _x).toInt();
  1707. }
  1708. if (dh > size.height) {
  1709. dyOffset = (y - dh * (y / size.height) - _y).toInt();
  1710. }
  1711. } catch (e) {
  1712. debugPrintStack(
  1713. label:
  1714. '(x,y) ($x,$y), (_x,_y) ($_x,$_y), _scale $_scale, display size (${getDisplayWidth()},${getDisplayHeight()}), size $size, , $e');
  1715. return;
  1716. }
  1717. _x += dxOffset;
  1718. _y += dyOffset;
  1719. if (dxOffset != 0 || dyOffset != 0) {
  1720. notifyListeners();
  1721. }
  1722. // If keyboard is not permitted, do not move cursor when mouse is moving.
  1723. if (parent.target != null && parent.target!.ffiModel.keyboard) {
  1724. // Draw cursor if is not desktop.
  1725. if (!(isDesktop || isWebDesktop)) {
  1726. parent.target!.cursorModel.moveLocal(x, y);
  1727. } else {
  1728. try {
  1729. RemoteCursorMovedState.find(id).value = false;
  1730. } catch (e) {
  1731. //
  1732. }
  1733. }
  1734. }
  1735. }
  1736. set scale(v) {
  1737. _scale = v;
  1738. notifyListeners();
  1739. }
  1740. panX(double dx) {
  1741. _x += dx;
  1742. if (isMobile) {
  1743. isMobileCanvasChanged = true;
  1744. }
  1745. notifyListeners();
  1746. }
  1747. resetOffset() {
  1748. if (isWebDesktop) {
  1749. updateViewStyle();
  1750. } else {
  1751. _resetCanvasOffset(getDisplayWidth(), getDisplayHeight());
  1752. }
  1753. notifyListeners();
  1754. }
  1755. panY(double dy) {
  1756. _y += dy;
  1757. if (isMobile) {
  1758. isMobileCanvasChanged = true;
  1759. }
  1760. notifyListeners();
  1761. }
  1762. // mobile only
  1763. updateScale(double v, Offset focalPoint) {
  1764. if (parent.target?.imageModel.image == null) return;
  1765. final s = _scale;
  1766. _scale *= v;
  1767. final maxs = parent.target?.imageModel.maxScale ?? 1;
  1768. final mins = parent.target?.imageModel.minScale ?? 1;
  1769. if (_scale > maxs) _scale = maxs;
  1770. if (_scale < mins) _scale = mins;
  1771. // (focalPoint.dx - _x_1) / s1 + displayOriginX = (focalPoint.dx - _x_2) / s2 + displayOriginX
  1772. // _x_2 = focalPoint.dx - (focalPoint.dx - _x_1) / s1 * s2
  1773. _x = focalPoint.dx - (focalPoint.dx - _x) / s * _scale;
  1774. final adjust = getAdjustY();
  1775. // (focalPoint.dy - _y_1 - adjust) / s1 + displayOriginY = (focalPoint.dy - _y_2 - adjust) / s2 + displayOriginY
  1776. // _y_2 = focalPoint.dy - adjust - (focalPoint.dy - _y_1 - adjust) / s1 * s2
  1777. _y = focalPoint.dy - adjust - (focalPoint.dy - _y - adjust) / s * _scale;
  1778. if (isMobile) {
  1779. isMobileCanvasChanged = true;
  1780. }
  1781. notifyListeners();
  1782. }
  1783. // For reset canvas to the last view style
  1784. reset() {
  1785. _scale = _lastViewStyle.scale;
  1786. _devicePixelRatio = ui.window.devicePixelRatio;
  1787. if (kIgnoreDpi && _lastViewStyle.style == kRemoteViewStyleOriginal) {
  1788. _scale = 1.0 / _devicePixelRatio;
  1789. }
  1790. _resetCanvasOffset(getDisplayWidth(), getDisplayHeight());
  1791. bind.sessionSetViewStyle(sessionId: sessionId, value: _lastViewStyle.style);
  1792. notifyListeners();
  1793. }
  1794. clear() {
  1795. _x = 0;
  1796. _y = 0;
  1797. _scale = 1.0;
  1798. _lastViewStyle = ViewStyle.defaultViewStyle();
  1799. _timerMobileFocusCanvasCursor?.cancel();
  1800. }
  1801. updateScrollPercent() {
  1802. final percentX = _horizontal.hasClients
  1803. ? _horizontal.position.extentBefore /
  1804. (_horizontal.position.extentBefore +
  1805. _horizontal.position.extentInside +
  1806. _horizontal.position.extentAfter)
  1807. : 0.0;
  1808. final percentY = _vertical.hasClients
  1809. ? _vertical.position.extentBefore /
  1810. (_vertical.position.extentBefore +
  1811. _vertical.position.extentInside +
  1812. _vertical.position.extentAfter)
  1813. : 0.0;
  1814. setScrollPercent(percentX, percentY);
  1815. }
  1816. void mobileFocusCanvasCursor() {
  1817. _timerMobileFocusCanvasCursor?.cancel();
  1818. _timerMobileFocusCanvasCursor =
  1819. Timer(Duration(milliseconds: 100), () async {
  1820. updateSize();
  1821. _resetCanvasOffset(getDisplayWidth(), getDisplayHeight());
  1822. notifyListeners();
  1823. });
  1824. }
  1825. // mobile only
  1826. // Move the canvas to make the cursor visible(center) on the screen.
  1827. void _moveToCenterCursor() {
  1828. Rect? imageRect = parent.target?.ffiModel.rect;
  1829. if (imageRect == null) {
  1830. // unreachable
  1831. return;
  1832. }
  1833. final maxX = 0.0;
  1834. final minX = _size.width + (imageRect.left - imageRect.right) * _scale;
  1835. final maxY = 0.0;
  1836. final minY = _size.height + (imageRect.top - imageRect.bottom) * _scale;
  1837. Offset offsetToCenter =
  1838. parent.target?.cursorModel.getCanvasOffsetToCenterCursor() ??
  1839. Offset.zero;
  1840. if (minX < 0) {
  1841. _x = min(max(offsetToCenter.dx, minX), maxX);
  1842. } else {
  1843. // _size.width > (imageRect.right, imageRect.left) * _scale, we should not change _x
  1844. }
  1845. if (minY < 0) {
  1846. _y = min(max(offsetToCenter.dy, minY), maxY);
  1847. } else {
  1848. // _size.height > (imageRect.bottom - imageRect.top) * _scale, , we should not change _y
  1849. }
  1850. }
  1851. }
  1852. // data for cursor
  1853. class CursorData {
  1854. final String peerId;
  1855. final String id;
  1856. final img2.Image image;
  1857. double scale;
  1858. Uint8List? data;
  1859. final double hotxOrigin;
  1860. final double hotyOrigin;
  1861. double hotx;
  1862. double hoty;
  1863. final int width;
  1864. final int height;
  1865. CursorData({
  1866. required this.peerId,
  1867. required this.id,
  1868. required this.image,
  1869. required this.scale,
  1870. required this.data,
  1871. required this.hotxOrigin,
  1872. required this.hotyOrigin,
  1873. required this.width,
  1874. required this.height,
  1875. }) : hotx = hotxOrigin * scale,
  1876. hoty = hotxOrigin * scale;
  1877. int _doubleToInt(double v) => (v * 10e6).round().toInt();
  1878. double _checkUpdateScale(double scale) {
  1879. double oldScale = this.scale;
  1880. if (scale != 1.0) {
  1881. // Update data if scale changed.
  1882. final tgtWidth = (width * scale).toInt();
  1883. final tgtHeight = (width * scale).toInt();
  1884. if (tgtWidth < kMinCursorSize || tgtHeight < kMinCursorSize) {
  1885. double sw = kMinCursorSize.toDouble() / width;
  1886. double sh = kMinCursorSize.toDouble() / height;
  1887. scale = sw < sh ? sh : sw;
  1888. }
  1889. }
  1890. if (_doubleToInt(oldScale) != _doubleToInt(scale)) {
  1891. if (isWindows) {
  1892. data = img2
  1893. .copyResize(
  1894. image,
  1895. width: (width * scale).toInt(),
  1896. height: (height * scale).toInt(),
  1897. interpolation: img2.Interpolation.average,
  1898. )
  1899. .getBytes(order: img2.ChannelOrder.bgra);
  1900. } else {
  1901. data = Uint8List.fromList(
  1902. img2.encodePng(
  1903. img2.copyResize(
  1904. image,
  1905. width: (width * scale).toInt(),
  1906. height: (height * scale).toInt(),
  1907. interpolation: img2.Interpolation.average,
  1908. ),
  1909. ),
  1910. );
  1911. }
  1912. }
  1913. this.scale = scale;
  1914. hotx = hotxOrigin * scale;
  1915. hoty = hotyOrigin * scale;
  1916. return scale;
  1917. }
  1918. String updateGetKey(double scale) {
  1919. scale = _checkUpdateScale(scale);
  1920. return '${peerId}_${id}_${_doubleToInt(width * scale)}_${_doubleToInt(height * scale)}';
  1921. }
  1922. }
  1923. const _forbiddenCursorPng =
  1924. 'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAAAXNSR0IB2cksfwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAkZQTFRFAAAA2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4G2B4GWAwCAAAAAAAA2B4GAAAAMTExAAAAAAAA2B4G2B4G2B4GAAAAmZmZkZGRAQEBAAAA2B4G2B4G2B4G////oKCgAwMDag8D2B4G2B4G2B4Gra2tBgYGbg8D2B4G2B4Gubm5CQkJTwsCVgwC2B4GxcXFDg4OAAAAAAAA2B4G2B4Gz8/PFBQUAAAAAAAA2B4G2B4G2B4G2B4G2B4G2B4G2B4GDgIA2NjYGxsbAAAAAAAA2B4GFwMB4eHhIyMjAAAAAAAA2B4G6OjoLCwsAAAAAAAA2B4G2B4G2B4G2B4G2B4GCQEA4ODgv7+/iYmJY2NjAgICAAAA9PT0Ojo6AAAAAAAAAAAA+/v7SkpKhYWFr6+vAAAAAAAA8/PzOTk5ERER9fX1KCgoAAAAgYGBKioqAAAAAAAApqamlpaWAAAAAAAAAAAAAAAAAAAAAAAALi4u/v7+GRkZAAAAAAAAAAAAAAAAAAAAfn5+AAAAAAAAV1dXkJCQAAAAAAAAAQEBAAAAAAAAAAAA7Hz6BAAAAMJ0Uk5TAAIWEwEynNz6//fVkCAatP2fDUHs6cDD8d0mPfT5fiEskiIR584A0gejr3AZ+P4plfALf5ZiTL85a4ziD6697fzN3UYE4v/4TwrNHuT///tdRKZh///+1U/ZBv///yjb///eAVL//50Cocv//6oFBbPvpGZCbfT//7cIhv///8INM///zBEcWYSZmO7//////1P////ts/////8vBv//////gv//R/z///QQz9sevP///2waXhNO/+fc//8mev/5gAe2r90MAAAByUlEQVR4nGNggANGJmYWBpyAlY2dg5OTi5uHF6s0H78AJxRwCAphyguLgKRExcQlQLSkFLq8tAwnp6ycPNABjAqKQKNElVDllVU4OVVhVquJA81Q10BRoAkUUYbJa4Edoo0sr6PLqaePLG/AyWlohKTAmJPTBFnelAFoixmSAnNOTgsUeQZLTk4rJAXWnJw2EHlbiDyDPCenHZICe04HFrh+RydnBgYWPU5uJAWinJwucPNd3dw9GDw5Ob2QFHBzcnrD7ffx9fMPCOTkDEINhmC4+3x8Q0LDwlEDIoKTMzIKKg9SEBIdE8sZh6SAJZ6Tkx0qD1YQkpCYlIwclCng0AXLQxSEpKalZyCryATKZwkhKQjJzsnNQ1KQXwBUUVhUXBJYWgZREFJeUVmFpMKlWg+anmqgCkJq6+obkG1pLEBTENLU3NKKrIKhrb2js8u4G6Kgpze0r3/CRAZMAHbkpJDJU6ZMmTqtFbuC6TNmhsyaMnsOFlmwgrnzpsxfELJwEXZ5Bp/FS3yWLlsesmLlKuwKVk9Ys5Zh3foN0zduwq5g85atDAzbpqSGbN9RhV0FGOzctWH3lD14FOzdt3H/gQw8Cg4u2gQPAwBYDXXdIH+wqAAAAABJRU5ErkJggg==';
  1925. const _defaultCursorPng =
  1926. 'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAARzQklUCAgICHwIZIgAAAFmSURBVFiF7dWxSlxREMbx34QFDRowYBchZSxSCWlMCOwD5FGEFHap06UI7KPsAyyEEIQFqxRaCqYTsqCJFsKkuAeRXb17wrqV918dztw55zszc2fo6Oh47MR/e3zO1/iAHWmznHKGQwx9ip/LEbCfazbsoY8j/JLOhcC6sCW9wsjEwJf483AC9nPNc1+lFRwI13d+l3rYFS799rFGxJMqARv2pBXh+72XQ7gWvklPS7TmMl9Ak/M+DqrENvxAv/guKKApuKPWl0/TROK4+LbSqzhuB+OZ3fRSeFPWY+Fkyn56Y29hfgTSpnQ+s98cvorVey66uPlNFxKwZOYLCGfCs5n9NMYVrsp6mvXSoFqpqYFDvMBkStgJJe93dZOwVXxbqUnBENulydSReqUrDhcX0PT2EXarBYS3GNXMhboinBgIl9K71kg0L3+PvyYGdVpruT2MwrF0iotiXfIwus0Dj+OOjo6Of+e7ab74RkpgAAAAAElFTkSuQmCC';
  1927. const kPreForbiddenCursorId = "-2";
  1928. final preForbiddenCursor = PredefinedCursor(
  1929. png: _forbiddenCursorPng,
  1930. id: kPreForbiddenCursorId,
  1931. );
  1932. const kPreDefaultCursorId = "-1";
  1933. final preDefaultCursor = PredefinedCursor(
  1934. png: _defaultCursorPng,
  1935. id: kPreDefaultCursorId,
  1936. hotxGetter: (double w) => w / 2,
  1937. hotyGetter: (double h) => h / 2,
  1938. );
  1939. class PredefinedCursor {
  1940. ui.Image? _image;
  1941. img2.Image? _image2;
  1942. CursorData? _cache;
  1943. String png;
  1944. String id;
  1945. double Function(double)? hotxGetter;
  1946. double Function(double)? hotyGetter;
  1947. PredefinedCursor(
  1948. {required this.png, required this.id, this.hotxGetter, this.hotyGetter}) {
  1949. init();
  1950. }
  1951. ui.Image? get image => _image;
  1952. CursorData? get cache => _cache;
  1953. init() {
  1954. _image2 = img2.decodePng(base64Decode(png));
  1955. if (_image2 != null) {
  1956. // The png type of forbidden cursor image is `PngColorType.indexed`.
  1957. if (id == kPreForbiddenCursorId) {
  1958. _image2 = _image2!.convert(format: img2.Format.uint8, numChannels: 4);
  1959. }
  1960. () async {
  1961. final defaultImg = _image2!;
  1962. // This function is called only one time, no need to care about the performance.
  1963. Uint8List data = defaultImg.getBytes(order: img2.ChannelOrder.rgba);
  1964. _image?.dispose();
  1965. _image = await img.decodeImageFromPixels(
  1966. data, defaultImg.width, defaultImg.height, ui.PixelFormat.rgba8888);
  1967. if (_image == null) {
  1968. print("decodeImageFromPixels failed, pre-defined cursor $id");
  1969. return;
  1970. }
  1971. double scale = 1.0;
  1972. if (isWindows) {
  1973. data = _image2!.getBytes(order: img2.ChannelOrder.bgra);
  1974. } else {
  1975. data = Uint8List.fromList(img2.encodePng(_image2!));
  1976. }
  1977. _cache = CursorData(
  1978. peerId: '',
  1979. id: id,
  1980. image: _image2!.clone(),
  1981. scale: scale,
  1982. data: data,
  1983. hotxOrigin:
  1984. hotxGetter != null ? hotxGetter!(_image2!.width.toDouble()) : 0,
  1985. hotyOrigin:
  1986. hotyGetter != null ? hotyGetter!(_image2!.height.toDouble()) : 0,
  1987. width: _image2!.width,
  1988. height: _image2!.height,
  1989. );
  1990. }();
  1991. }
  1992. }
  1993. }
  1994. class CursorModel with ChangeNotifier {
  1995. ui.Image? _image;
  1996. final _images = <String, Tuple3<ui.Image, double, double>>{};
  1997. CursorData? _cache;
  1998. final _cacheMap = <String, CursorData>{};
  1999. final _cacheKeys = <String>{};
  2000. double _x = -10000;
  2001. double _y = -10000;
  2002. // int.parse(evt['id']) may cause FormatException
  2003. // So we use String here.
  2004. String _id = "-1";
  2005. double _hotx = 0;
  2006. double _hoty = 0;
  2007. double _displayOriginX = 0;
  2008. double _displayOriginY = 0;
  2009. DateTime? _firstUpdateMouseTime;
  2010. Rect? _windowRect;
  2011. List<RemoteWindowCoords> _remoteWindowCoords = [];
  2012. bool gotMouseControl = true;
  2013. DateTime _lastPeerMouse = DateTime.now()
  2014. .subtract(Duration(milliseconds: 3000 * kMouseControlTimeoutMSec));
  2015. String peerId = '';
  2016. WeakReference<FFI> parent;
  2017. // Only for mobile, touch mode
  2018. // To block touch event above the KeyHelpTools
  2019. //
  2020. // A better way is to not listen events from the KeyHelpTools.
  2021. // But we're now using a Container(child: Stack(...)) to wrap the KeyHelpTools,
  2022. // and the listener is on the Container.
  2023. Rect? _keyHelpToolsRect;
  2024. // `lastIsBlocked` is only used in common/widgets/remote_input.dart -> _RawTouchGestureDetectorRegionState -> onDoubleTap()
  2025. // Because onDoubleTap() doesn't have the `event` parameter, we can't get the touch event's position.
  2026. bool _lastIsBlocked = false;
  2027. bool _lastKeyboardIsVisible = false;
  2028. bool get lastKeyboardIsVisible => _lastKeyboardIsVisible;
  2029. Rect? get keyHelpToolsRectToAdjustCanvas =>
  2030. _lastKeyboardIsVisible ? _keyHelpToolsRect : null;
  2031. keyHelpToolsVisibilityChanged(Rect? r, bool keyboardIsVisible) {
  2032. _keyHelpToolsRect = r;
  2033. if (r == null) {
  2034. _lastIsBlocked = false;
  2035. } else {
  2036. // Block the touch event is safe here.
  2037. // `lastIsBlocked` is only used in onDoubleTap() to block the touch event from the KeyHelpTools.
  2038. // `lastIsBlocked` will be set when the cursor is moving or touch somewhere else.
  2039. _lastIsBlocked = true;
  2040. }
  2041. if (isMobile && _lastKeyboardIsVisible != keyboardIsVisible) {
  2042. parent.target?.canvasModel.mobileFocusCanvasCursor();
  2043. parent.target?.canvasModel.isMobileCanvasChanged = false;
  2044. }
  2045. _lastKeyboardIsVisible = keyboardIsVisible;
  2046. }
  2047. get lastIsBlocked => _lastIsBlocked;
  2048. ui.Image? get image => _image;
  2049. CursorData? get cache => _cache;
  2050. double get x => _x - _displayOriginX;
  2051. double get y => _y - _displayOriginY;
  2052. double get devicePixelRatio => parent.target!.canvasModel.devicePixelRatio;
  2053. Offset get offset => Offset(_x, _y);
  2054. double get hotx => _hotx;
  2055. double get hoty => _hoty;
  2056. set id(String id) => _id = id;
  2057. bool get isPeerControlProtected =>
  2058. DateTime.now().difference(_lastPeerMouse).inMilliseconds <
  2059. kMouseControlTimeoutMSec;
  2060. bool isConnIn2Secs() {
  2061. if (_firstUpdateMouseTime == null) {
  2062. _firstUpdateMouseTime = DateTime.now();
  2063. return true;
  2064. } else {
  2065. return DateTime.now().difference(_firstUpdateMouseTime!).inSeconds < 2;
  2066. }
  2067. }
  2068. CursorModel(this.parent);
  2069. Set<String> get cachedKeys => _cacheKeys;
  2070. addKey(String key) => _cacheKeys.add(key);
  2071. // remote physical display coordinate
  2072. // For update pan (mobile), onOneFingerPanStart, onOneFingerPanUpdate, onHoldDragUpdate
  2073. Rect getVisibleRect() {
  2074. final size = parent.target?.canvasModel.getSize() ??
  2075. MediaQueryData.fromView(ui.window).size;
  2076. final xoffset = parent.target?.canvasModel.x ?? 0;
  2077. final yoffset = parent.target?.canvasModel.y ?? 0;
  2078. final scale = parent.target?.canvasModel.scale ?? 1;
  2079. final x0 = _displayOriginX - xoffset / scale;
  2080. final y0 = _displayOriginY - yoffset / scale;
  2081. return Rect.fromLTWH(x0, y0, size.width / scale, size.height / scale);
  2082. }
  2083. Offset getCanvasOffsetToCenterCursor() {
  2084. // Cursor should be at the center of the visible rect.
  2085. // _x = rect.left + rect.width / 2
  2086. // _y = rect.right + rect.height / 2
  2087. // See `getVisibleRect()`
  2088. // _x = _displayOriginX - xoffset / scale + size.width / scale * 0.5;
  2089. // _y = _displayOriginY - yoffset / scale + size.height / scale * 0.5;
  2090. final size = parent.target?.canvasModel.getSize() ??
  2091. MediaQueryData.fromView(ui.window).size;
  2092. final xoffset = (_displayOriginX - _x) * scale + size.width * 0.5;
  2093. final yoffset = (_displayOriginY - _y) * scale + size.height * 0.5;
  2094. return Offset(xoffset, yoffset);
  2095. }
  2096. get scale => parent.target?.canvasModel.scale ?? 1.0;
  2097. // mobile Soft keyboard, block touch event from the KeyHelpTools
  2098. shouldBlock(double x, double y) {
  2099. if (!(parent.target?.ffiModel.touchMode ?? false)) {
  2100. return false;
  2101. }
  2102. if (_keyHelpToolsRect == null) {
  2103. return false;
  2104. }
  2105. if (isPointInRect(Offset(x, y), _keyHelpToolsRect!)) {
  2106. return true;
  2107. }
  2108. return false;
  2109. }
  2110. // For touch mode
  2111. Future<bool> move(double x, double y) async {
  2112. if (shouldBlock(x, y)) {
  2113. _lastIsBlocked = true;
  2114. return false;
  2115. }
  2116. _lastIsBlocked = false;
  2117. if (!_moveLocalIfInRemoteRect(x, y)) {
  2118. return false;
  2119. }
  2120. await parent.target?.inputModel.moveMouse(_x, _y);
  2121. return true;
  2122. }
  2123. bool isInRemoteRect(Offset offset) {
  2124. return getRemotePosInRect(offset) != null;
  2125. }
  2126. Offset? getRemotePosInRect(Offset offset) {
  2127. final adjust = parent.target?.canvasModel.getAdjustY() ?? 0;
  2128. final newPos = _getNewPos(offset.dx, offset.dy, adjust);
  2129. final visibleRect = getVisibleRect();
  2130. if (!isPointInRect(newPos, visibleRect)) {
  2131. return null;
  2132. }
  2133. final rect = parent.target?.ffiModel.rect;
  2134. if (rect != null) {
  2135. if (!isPointInRect(newPos, rect)) {
  2136. return null;
  2137. }
  2138. }
  2139. return newPos;
  2140. }
  2141. Offset _getNewPos(double x, double y, double adjust) {
  2142. final xoffset = parent.target?.canvasModel.x ?? 0;
  2143. final yoffset = parent.target?.canvasModel.y ?? 0;
  2144. final newX = (x - xoffset) / scale + _displayOriginX;
  2145. final newY = (y - yoffset - adjust) / scale + _displayOriginY;
  2146. return Offset(newX, newY);
  2147. }
  2148. bool _moveLocalIfInRemoteRect(double x, double y) {
  2149. final newPos = getRemotePosInRect(Offset(x, y));
  2150. if (newPos == null) {
  2151. return false;
  2152. }
  2153. _x = newPos.dx;
  2154. _y = newPos.dy;
  2155. notifyListeners();
  2156. return true;
  2157. }
  2158. moveLocal(double x, double y, {double adjust = 0}) {
  2159. final newPos = _getNewPos(x, y, adjust);
  2160. _x = newPos.dx;
  2161. _y = newPos.dy;
  2162. notifyListeners();
  2163. }
  2164. reset() {
  2165. _x = _displayOriginX;
  2166. _y = _displayOriginY;
  2167. parent.target?.inputModel.moveMouse(_x, _y);
  2168. parent.target?.canvasModel.reset();
  2169. notifyListeners();
  2170. }
  2171. updatePan(Offset delta, Offset localPosition, bool touchMode) async {
  2172. if (touchMode) {
  2173. await _handleTouchMode(delta, localPosition);
  2174. return;
  2175. }
  2176. double dx = delta.dx;
  2177. double dy = delta.dy;
  2178. if (parent.target?.imageModel.image == null) return;
  2179. final scale = parent.target?.canvasModel.scale ?? 1.0;
  2180. dx /= scale;
  2181. dy /= scale;
  2182. final r = getVisibleRect();
  2183. var cx = r.center.dx;
  2184. var cy = r.center.dy;
  2185. var tryMoveCanvasX = false;
  2186. if (dx > 0) {
  2187. final maxCanvasCanMove = _displayOriginX +
  2188. (parent.target?.imageModel.image!.width ?? 1280) -
  2189. r.right.roundToDouble();
  2190. tryMoveCanvasX = _x + dx > cx && maxCanvasCanMove > 0;
  2191. if (tryMoveCanvasX) {
  2192. dx = min(dx, maxCanvasCanMove);
  2193. } else {
  2194. final maxCursorCanMove = r.right - _x;
  2195. dx = min(dx, maxCursorCanMove);
  2196. }
  2197. } else if (dx < 0) {
  2198. final maxCanvasCanMove = _displayOriginX - r.left.roundToDouble();
  2199. tryMoveCanvasX = _x + dx < cx && maxCanvasCanMove < 0;
  2200. if (tryMoveCanvasX) {
  2201. dx = max(dx, maxCanvasCanMove);
  2202. } else {
  2203. final maxCursorCanMove = r.left - _x;
  2204. dx = max(dx, maxCursorCanMove);
  2205. }
  2206. }
  2207. var tryMoveCanvasY = false;
  2208. if (dy > 0) {
  2209. final mayCanvasCanMove = _displayOriginY +
  2210. (parent.target?.imageModel.image!.height ?? 720) -
  2211. r.bottom.roundToDouble();
  2212. tryMoveCanvasY = _y + dy > cy && mayCanvasCanMove > 0;
  2213. if (tryMoveCanvasY) {
  2214. dy = min(dy, mayCanvasCanMove);
  2215. } else {
  2216. final mayCursorCanMove = r.bottom - _y;
  2217. dy = min(dy, mayCursorCanMove);
  2218. }
  2219. } else if (dy < 0) {
  2220. final mayCanvasCanMove = _displayOriginY - r.top.roundToDouble();
  2221. tryMoveCanvasY = _y + dy < cy && mayCanvasCanMove < 0;
  2222. if (tryMoveCanvasY) {
  2223. dy = max(dy, mayCanvasCanMove);
  2224. } else {
  2225. final mayCursorCanMove = r.top - _y;
  2226. dy = max(dy, mayCursorCanMove);
  2227. }
  2228. }
  2229. if (dx == 0 && dy == 0) return;
  2230. Point<double>? newPos;
  2231. final rect = parent.target?.ffiModel.rect;
  2232. if (rect == null) {
  2233. // unreachable
  2234. return;
  2235. }
  2236. newPos = InputModel.getPointInRemoteRect(
  2237. false,
  2238. parent.target?.ffiModel.pi.platform,
  2239. kPointerEventKindMouse,
  2240. kMouseEventTypeDefault,
  2241. _x + dx,
  2242. _y + dy,
  2243. rect,
  2244. buttons: kPrimaryButton);
  2245. if (newPos == null) {
  2246. return;
  2247. }
  2248. dx = newPos.x - _x;
  2249. dy = newPos.y - _y;
  2250. _x = newPos.x;
  2251. _y = newPos.y;
  2252. if (tryMoveCanvasX && dx != 0) {
  2253. parent.target?.canvasModel.panX(-dx * scale);
  2254. }
  2255. if (tryMoveCanvasY && dy != 0) {
  2256. parent.target?.canvasModel.panY(-dy * scale);
  2257. }
  2258. parent.target?.inputModel.moveMouse(_x, _y);
  2259. notifyListeners();
  2260. }
  2261. bool _isInCurrentWindow(double x, double y) {
  2262. final w = _windowRect!.width / devicePixelRatio;
  2263. final h = _windowRect!.width / devicePixelRatio;
  2264. return x >= 0 && y >= 0 && x <= w && y <= h;
  2265. }
  2266. _handleTouchMode(Offset delta, Offset localPosition) async {
  2267. bool isMoved = false;
  2268. if (_remoteWindowCoords.isNotEmpty &&
  2269. _windowRect != null &&
  2270. !_isInCurrentWindow(localPosition.dx, localPosition.dy)) {
  2271. final coords = InputModel.findRemoteCoords(localPosition.dx,
  2272. localPosition.dy, _remoteWindowCoords, devicePixelRatio);
  2273. if (coords != null) {
  2274. double x2 =
  2275. (localPosition.dx - coords.relativeOffset.dx / devicePixelRatio) /
  2276. coords.canvas.scale;
  2277. double y2 =
  2278. (localPosition.dy - coords.relativeOffset.dy / devicePixelRatio) /
  2279. coords.canvas.scale;
  2280. x2 += coords.cursor.offset.dx;
  2281. y2 += coords.cursor.offset.dy;
  2282. await parent.target?.inputModel.moveMouse(x2, y2);
  2283. isMoved = true;
  2284. }
  2285. }
  2286. if (!isMoved) {
  2287. final rect = parent.target?.ffiModel.rect;
  2288. if (rect == null) {
  2289. // unreachable
  2290. return;
  2291. }
  2292. Offset? movementInRect(double x, double y, Rect r) {
  2293. final isXInRect = x >= r.left && x <= r.right;
  2294. final isYInRect = y >= r.top && y <= r.bottom;
  2295. if (!(isXInRect || isYInRect)) {
  2296. return null;
  2297. }
  2298. if (x < r.left) {
  2299. x = r.left;
  2300. } else if (x > r.right) {
  2301. x = r.right;
  2302. }
  2303. if (y < r.top) {
  2304. y = r.top;
  2305. } else if (y > r.bottom) {
  2306. y = r.bottom;
  2307. }
  2308. return Offset(x, y);
  2309. }
  2310. final scale = parent.target?.canvasModel.scale ?? 1.0;
  2311. var movement =
  2312. movementInRect(_x + delta.dx / scale, _y + delta.dy / scale, rect);
  2313. if (movement == null) {
  2314. return;
  2315. }
  2316. movement = movementInRect(movement.dx, movement.dy, getVisibleRect());
  2317. if (movement == null) {
  2318. return;
  2319. }
  2320. _x = movement.dx;
  2321. _y = movement.dy;
  2322. await parent.target?.inputModel.moveMouse(_x, _y);
  2323. }
  2324. notifyListeners();
  2325. }
  2326. disposeImages() {
  2327. _images.forEach((_, v) => v.item1.dispose());
  2328. _images.clear();
  2329. }
  2330. updateCursorData(Map<String, dynamic> evt) async {
  2331. final id = evt['id'];
  2332. final hotx = double.parse(evt['hotx']);
  2333. final hoty = double.parse(evt['hoty']);
  2334. final width = int.parse(evt['width']);
  2335. final height = int.parse(evt['height']);
  2336. List<dynamic> colors = json.decode(evt['colors']);
  2337. final rgba = Uint8List.fromList(colors.map((s) => s as int).toList());
  2338. final image = await img.decodeImageFromPixels(
  2339. rgba, width, height, ui.PixelFormat.rgba8888);
  2340. if (image == null) {
  2341. return;
  2342. }
  2343. if (await _updateCache(rgba, image, id, hotx, hoty, width, height)) {
  2344. _images[id]?.item1.dispose();
  2345. _images[id] = Tuple3(image, hotx, hoty);
  2346. }
  2347. // Update last cursor data.
  2348. // Do not use the previous `image` and `id`, because `_id` may be changed.
  2349. _updateCurData();
  2350. }
  2351. Future<bool> _updateCache(
  2352. Uint8List rgba,
  2353. ui.Image image,
  2354. String id,
  2355. double hotx,
  2356. double hoty,
  2357. int w,
  2358. int h,
  2359. ) async {
  2360. Uint8List? data;
  2361. img2.Image imgOrigin = img2.Image.fromBytes(
  2362. width: w, height: h, bytes: rgba.buffer, order: img2.ChannelOrder.rgba);
  2363. if (isWindows) {
  2364. data = imgOrigin.getBytes(order: img2.ChannelOrder.bgra);
  2365. } else {
  2366. ByteData? imgBytes =
  2367. await image.toByteData(format: ui.ImageByteFormat.png);
  2368. if (imgBytes == null) {
  2369. return false;
  2370. }
  2371. data = imgBytes.buffer.asUint8List();
  2372. }
  2373. final cache = CursorData(
  2374. peerId: peerId,
  2375. id: id,
  2376. image: imgOrigin,
  2377. scale: 1.0,
  2378. data: data,
  2379. hotxOrigin: hotx,
  2380. hotyOrigin: hoty,
  2381. width: w,
  2382. height: h,
  2383. );
  2384. _cacheMap[id] = cache;
  2385. return true;
  2386. }
  2387. bool _updateCurData() {
  2388. _cache = _cacheMap[_id];
  2389. final tmp = _images[_id];
  2390. if (tmp != null) {
  2391. _image = tmp.item1;
  2392. _hotx = tmp.item2;
  2393. _hoty = tmp.item3;
  2394. try {
  2395. // may throw exception, because the listener maybe already dispose
  2396. notifyListeners();
  2397. } catch (e) {
  2398. debugPrint(
  2399. 'WARNING: updateCursorId $_id, without notifyListeners(). $e');
  2400. }
  2401. return true;
  2402. } else {
  2403. return false;
  2404. }
  2405. }
  2406. updateCursorId(Map<String, dynamic> evt) {
  2407. if (!_updateCurData()) {
  2408. debugPrint(
  2409. 'WARNING: updateCursorId $_id, cache is ${_cache == null ? "null" : "not null"}. without notifyListeners()');
  2410. }
  2411. }
  2412. /// Update the cursor position.
  2413. updateCursorPosition(Map<String, dynamic> evt, String id) async {
  2414. if (!isConnIn2Secs()) {
  2415. gotMouseControl = false;
  2416. _lastPeerMouse = DateTime.now();
  2417. }
  2418. _x = double.parse(evt['x']);
  2419. _y = double.parse(evt['y']);
  2420. try {
  2421. RemoteCursorMovedState.find(id).value = true;
  2422. } catch (e) {
  2423. //
  2424. }
  2425. notifyListeners();
  2426. }
  2427. updateDisplayOrigin(double x, double y, {updateCursorPos = true}) {
  2428. _displayOriginX = x;
  2429. _displayOriginY = y;
  2430. if (updateCursorPos) {
  2431. _x = x + 1;
  2432. _y = y + 1;
  2433. parent.target?.inputModel.moveMouse(x, y);
  2434. }
  2435. parent.target?.canvasModel.resetOffset();
  2436. notifyListeners();
  2437. }
  2438. updateDisplayOriginWithCursor(
  2439. double x, double y, double xCursor, double yCursor) {
  2440. _displayOriginX = x;
  2441. _displayOriginY = y;
  2442. _x = xCursor;
  2443. _y = yCursor;
  2444. parent.target?.inputModel.moveMouse(x, y);
  2445. notifyListeners();
  2446. }
  2447. clear() {
  2448. _x = -10000;
  2449. _x = -10000;
  2450. _image = null;
  2451. _firstUpdateMouseTime = null;
  2452. gotMouseControl = true;
  2453. disposeImages();
  2454. _clearCache();
  2455. _cache = null;
  2456. _cacheMap.clear();
  2457. }
  2458. _clearCache() {
  2459. final keys = {...cachedKeys};
  2460. for (var k in keys) {
  2461. debugPrint("deleting cursor with key $k");
  2462. deleteCustomCursor(k);
  2463. }
  2464. resetSystemCursor();
  2465. }
  2466. trySetRemoteWindowCoords() {
  2467. Future.delayed(Duration.zero, () async {
  2468. _windowRect =
  2469. await InputModel.fillRemoteCoordsAndGetCurFrame(_remoteWindowCoords);
  2470. });
  2471. }
  2472. clearRemoteWindowCoords() {
  2473. _windowRect = null;
  2474. _remoteWindowCoords.clear();
  2475. }
  2476. }
  2477. class QualityMonitorData {
  2478. String? speed;
  2479. String? fps;
  2480. String? delay;
  2481. String? targetBitrate;
  2482. String? codecFormat;
  2483. String? chroma;
  2484. }
  2485. class QualityMonitorModel with ChangeNotifier {
  2486. WeakReference<FFI> parent;
  2487. QualityMonitorModel(this.parent);
  2488. var _show = false;
  2489. final _data = QualityMonitorData();
  2490. bool get show => _show;
  2491. QualityMonitorData get data => _data;
  2492. checkShowQualityMonitor(SessionID sessionId) async {
  2493. final show = await bind.sessionGetToggleOption(
  2494. sessionId: sessionId, arg: 'show-quality-monitor') ==
  2495. true;
  2496. if (_show != show) {
  2497. _show = show;
  2498. notifyListeners();
  2499. }
  2500. }
  2501. updateQualityStatus(Map<String, dynamic> evt) {
  2502. try {
  2503. if (evt.containsKey('speed') && (evt['speed'] as String).isNotEmpty) {
  2504. _data.speed = evt['speed'];
  2505. }
  2506. if (evt.containsKey('fps') && (evt['fps'] as String).isNotEmpty) {
  2507. final fps = jsonDecode(evt['fps']) as Map<String, dynamic>;
  2508. final pi = parent.target?.ffiModel.pi;
  2509. if (pi != null) {
  2510. final currentDisplay = pi.currentDisplay;
  2511. if (currentDisplay != kAllDisplayValue) {
  2512. final fps2 = fps[currentDisplay.toString()];
  2513. if (fps2 != null) {
  2514. _data.fps = fps2.toString();
  2515. }
  2516. } else if (fps.isNotEmpty) {
  2517. final fpsList = [];
  2518. for (var i = 0; i < pi.displays.length; i++) {
  2519. fpsList.add((fps[i.toString()] ?? 0).toString());
  2520. }
  2521. _data.fps = fpsList.join(' ');
  2522. }
  2523. } else {
  2524. _data.fps = null;
  2525. }
  2526. }
  2527. if (evt.containsKey('delay') && (evt['delay'] as String).isNotEmpty) {
  2528. _data.delay = evt['delay'];
  2529. }
  2530. if (evt.containsKey('target_bitrate') &&
  2531. (evt['target_bitrate'] as String).isNotEmpty) {
  2532. _data.targetBitrate = evt['target_bitrate'];
  2533. }
  2534. if (evt.containsKey('codec_format') &&
  2535. (evt['codec_format'] as String).isNotEmpty) {
  2536. _data.codecFormat = evt['codec_format'];
  2537. }
  2538. if (evt.containsKey('chroma') && (evt['chroma'] as String).isNotEmpty) {
  2539. _data.chroma = evt['chroma'];
  2540. }
  2541. notifyListeners();
  2542. } catch (e) {
  2543. //
  2544. }
  2545. }
  2546. }
  2547. class RecordingModel with ChangeNotifier {
  2548. WeakReference<FFI> parent;
  2549. RecordingModel(this.parent);
  2550. bool _start = false;
  2551. bool get start => _start;
  2552. toggle() async {
  2553. if (isIOS) return;
  2554. final sessionId = parent.target?.sessionId;
  2555. if (sessionId == null) return;
  2556. final pi = parent.target?.ffiModel.pi;
  2557. if (pi == null) return;
  2558. bool value = !_start;
  2559. if (value) {
  2560. await sessionRefreshVideo(sessionId, pi);
  2561. }
  2562. await bind.sessionRecordScreen(sessionId: sessionId, start: value);
  2563. }
  2564. updateStatus(bool status) {
  2565. _start = status;
  2566. notifyListeners();
  2567. }
  2568. }
  2569. class ElevationModel with ChangeNotifier {
  2570. WeakReference<FFI> parent;
  2571. ElevationModel(this.parent);
  2572. bool _running = false;
  2573. bool _canElevate = false;
  2574. bool get showRequestMenu => _canElevate && !_running;
  2575. onPeerInfo(PeerInfo pi) {
  2576. _canElevate = pi.platform == kPeerPlatformWindows && pi.sasEnabled == false;
  2577. _running = false;
  2578. }
  2579. onPortableServiceRunning(bool running) => _running = running;
  2580. }
  2581. // The index values of `ConnType` are same as rust protobuf.
  2582. enum ConnType { defaultConn, fileTransfer, portForward, rdp, viewCamera }
  2583. /// Flutter state manager and data communication with the Rust core.
  2584. class FFI {
  2585. var id = '';
  2586. var version = '';
  2587. var connType = ConnType.defaultConn;
  2588. var closed = false;
  2589. var auditNote = '';
  2590. /// dialogManager use late to ensure init after main page binding [globalKey]
  2591. late final dialogManager = OverlayDialogManager();
  2592. late final SessionID sessionId;
  2593. late final ImageModel imageModel; // session
  2594. late final FfiModel ffiModel; // session
  2595. late final CursorModel cursorModel; // session
  2596. late final CanvasModel canvasModel; // session
  2597. late final ServerModel serverModel; // global
  2598. late final ChatModel chatModel; // session
  2599. late final FileModel fileModel; // session
  2600. late final AbModel abModel; // global
  2601. late final GroupModel groupModel; // global
  2602. late final UserModel userModel; // global
  2603. late final PeerTabModel peerTabModel; // global
  2604. late final QualityMonitorModel qualityMonitorModel; // session
  2605. late final RecordingModel recordingModel; // session
  2606. late final InputModel inputModel; // session
  2607. late final ElevationModel elevationModel; // session
  2608. late final CmFileModel cmFileModel; // cm
  2609. late final TextureModel textureModel; //session
  2610. late final Peers recentPeersModel; // global
  2611. late final Peers favoritePeersModel; // global
  2612. late final Peers lanPeersModel; // global
  2613. FFI(SessionID? sId) {
  2614. sessionId = sId ?? (isDesktop ? Uuid().v4obj() : _constSessionId);
  2615. imageModel = ImageModel(WeakReference(this));
  2616. ffiModel = FfiModel(WeakReference(this));
  2617. cursorModel = CursorModel(WeakReference(this));
  2618. canvasModel = CanvasModel(WeakReference(this));
  2619. serverModel = ServerModel(WeakReference(this));
  2620. chatModel = ChatModel(WeakReference(this));
  2621. fileModel = FileModel(WeakReference(this));
  2622. userModel = UserModel(WeakReference(this));
  2623. peerTabModel = PeerTabModel(WeakReference(this));
  2624. abModel = AbModel(WeakReference(this));
  2625. groupModel = GroupModel(WeakReference(this));
  2626. qualityMonitorModel = QualityMonitorModel(WeakReference(this));
  2627. recordingModel = RecordingModel(WeakReference(this));
  2628. inputModel = InputModel(WeakReference(this));
  2629. elevationModel = ElevationModel(WeakReference(this));
  2630. cmFileModel = CmFileModel(WeakReference(this));
  2631. textureModel = TextureModel(WeakReference(this));
  2632. recentPeersModel = Peers(
  2633. name: PeersModelName.recent,
  2634. loadEvent: LoadEvent.recent,
  2635. getInitPeers: null);
  2636. favoritePeersModel = Peers(
  2637. name: PeersModelName.favorite,
  2638. loadEvent: LoadEvent.favorite,
  2639. getInitPeers: null);
  2640. lanPeersModel = Peers(
  2641. name: PeersModelName.lan, loadEvent: LoadEvent.lan, getInitPeers: null);
  2642. }
  2643. /// Mobile reuse FFI
  2644. void mobileReset() {
  2645. ffiModel.waitForFirstImage.value = true;
  2646. ffiModel.isRefreshing = false;
  2647. ffiModel.waitForImageDialogShow.value = true;
  2648. ffiModel.waitForImageTimer?.cancel();
  2649. ffiModel.waitForImageTimer = null;
  2650. }
  2651. /// Start with the given [id]. Only transfer file if [isFileTransfer], only view camera if [isViewCamera], only port forward if [isPortForward].
  2652. void start(
  2653. String id, {
  2654. bool isFileTransfer = false,
  2655. bool isViewCamera = false,
  2656. bool isPortForward = false,
  2657. bool isRdp = false,
  2658. String? switchUuid,
  2659. String? password,
  2660. bool? isSharedPassword,
  2661. String? connToken,
  2662. bool? forceRelay,
  2663. int? tabWindowId,
  2664. int? display,
  2665. List<int>? displays,
  2666. }) {
  2667. closed = false;
  2668. auditNote = '';
  2669. if (isMobile) mobileReset();
  2670. assert(
  2671. (!(isPortForward && isViewCamera)) &&
  2672. (!(isViewCamera && isPortForward)) &&
  2673. (!(isPortForward && isFileTransfer)),
  2674. 'more than one connect type');
  2675. if (isFileTransfer) {
  2676. connType = ConnType.fileTransfer;
  2677. } else if (isViewCamera) {
  2678. connType = ConnType.viewCamera;
  2679. } else if (isPortForward) {
  2680. connType = ConnType.portForward;
  2681. } else {
  2682. chatModel.resetClientMode();
  2683. connType = ConnType.defaultConn;
  2684. canvasModel.id = id;
  2685. imageModel.id = id;
  2686. cursorModel.peerId = id;
  2687. }
  2688. final isNewPeer = tabWindowId == null;
  2689. // If tabWindowId != null, this session is a "tab -> window" one.
  2690. // Else this session is a new one.
  2691. if (isNewPeer) {
  2692. // ignore: unused_local_variable
  2693. final addRes = bind.sessionAddSync(
  2694. sessionId: sessionId,
  2695. id: id,
  2696. isFileTransfer: isFileTransfer,
  2697. isViewCamera: isViewCamera,
  2698. isPortForward: isPortForward,
  2699. isRdp: isRdp,
  2700. switchUuid: switchUuid ?? '',
  2701. forceRelay: forceRelay ?? false,
  2702. password: password ?? '',
  2703. isSharedPassword: isSharedPassword ?? false,
  2704. connToken: connToken,
  2705. );
  2706. } else if (display != null) {
  2707. if (displays == null) {
  2708. debugPrint(
  2709. 'Unreachable, failed to add existed session to $id, the displays is null while display is $display');
  2710. return;
  2711. }
  2712. final addRes = bind.sessionAddExistedSync(
  2713. id: id,
  2714. sessionId: sessionId,
  2715. displays: Int32List.fromList(displays),
  2716. isViewCamera: isViewCamera);
  2717. if (addRes != '') {
  2718. debugPrint(
  2719. 'Unreachable, failed to add existed session to $id, $addRes');
  2720. return;
  2721. }
  2722. ffiModel.pi.currentDisplay = display;
  2723. }
  2724. if (isDesktop && connType == ConnType.defaultConn) {
  2725. textureModel.updateCurrentDisplay(display ?? 0);
  2726. }
  2727. // FIXME: separate cameras displays or shift all indices.
  2728. if (isDesktop && connType == ConnType.viewCamera) {
  2729. // FIXME: currently the default 0 is not used.
  2730. textureModel.updateCurrentDisplay(display ?? 0);
  2731. }
  2732. if (isDesktop) {
  2733. inputModel.updateTrackpadSpeed();
  2734. }
  2735. // CAUTION: `sessionStart()` and `sessionStartWithDisplays()` are an async functions.
  2736. // Though the stream is returned immediately, the stream may not be ready.
  2737. // Any operations that depend on the stream should be carefully handled.
  2738. late final Stream<EventToUI> stream;
  2739. if (isNewPeer || display == null || displays == null) {
  2740. stream = bind.sessionStart(sessionId: sessionId, id: id);
  2741. } else {
  2742. // We have to put displays in `sessionStart()` to make sure the stream is ready
  2743. // and then the displays' capturing requests can be sent.
  2744. stream = bind.sessionStartWithDisplays(
  2745. sessionId: sessionId, id: id, displays: Int32List.fromList(displays));
  2746. }
  2747. if (isWeb) {
  2748. platformFFI.setRgbaCallback((int display, Uint8List data) {
  2749. onEvent2UIRgba();
  2750. imageModel.onRgba(display, data);
  2751. });
  2752. this.id = id;
  2753. return;
  2754. }
  2755. final cb = ffiModel.startEventListener(sessionId, id);
  2756. imageModel.updateUserTextureRender();
  2757. final hasGpuTextureRender = bind.mainHasGpuTextureRender();
  2758. final SimpleWrapper<bool> isToNewWindowNotified = SimpleWrapper(false);
  2759. // Preserved for the rgba data.
  2760. stream.listen((message) {
  2761. if (closed) return;
  2762. if (tabWindowId != null && !isToNewWindowNotified.value) {
  2763. // Session is read to be moved to a new window.
  2764. // Get the cached data and handle the cached data.
  2765. Future.delayed(Duration.zero, () async {
  2766. final args = jsonEncode({'id': id, 'close': display == null});
  2767. final cachedData = await DesktopMultiWindow.invokeMethod(
  2768. tabWindowId, kWindowEventGetCachedSessionData, args);
  2769. if (cachedData == null) {
  2770. // unreachable
  2771. debugPrint('Unreachable, the cached data is empty.');
  2772. return;
  2773. }
  2774. final data = CachedPeerData.fromString(cachedData);
  2775. if (data == null) {
  2776. debugPrint('Unreachable, the cached data cannot be decoded.');
  2777. return;
  2778. }
  2779. ffiModel.setPermissions(data.permissions);
  2780. await ffiModel.handleCachedPeerData(data, id);
  2781. await sessionRefreshVideo(sessionId, ffiModel.pi);
  2782. await bind.sessionRequestNewDisplayInitMsgs(
  2783. sessionId: sessionId, display: ffiModel.pi.currentDisplay);
  2784. });
  2785. isToNewWindowNotified.value = true;
  2786. }
  2787. () async {
  2788. if (message is EventToUI_Event) {
  2789. if (message.field0 == "close") {
  2790. closed = true;
  2791. debugPrint('Exit session event loop');
  2792. return;
  2793. }
  2794. Map<String, dynamic>? event;
  2795. try {
  2796. event = json.decode(message.field0);
  2797. } catch (e) {
  2798. debugPrint('json.decode fail1(): $e, ${message.field0}');
  2799. }
  2800. if (event != null) {
  2801. await cb(event);
  2802. }
  2803. } else if (message is EventToUI_Rgba) {
  2804. final display = message.field0;
  2805. // Fetch the image buffer from rust codes.
  2806. final sz = platformFFI.getRgbaSize(sessionId, display);
  2807. if (sz == 0) {
  2808. platformFFI.nextRgba(sessionId, display);
  2809. return;
  2810. }
  2811. final rgba = platformFFI.getRgba(sessionId, display, sz);
  2812. if (rgba != null) {
  2813. onEvent2UIRgba();
  2814. await imageModel.onRgba(display, rgba);
  2815. } else {
  2816. platformFFI.nextRgba(sessionId, display);
  2817. }
  2818. } else if (message is EventToUI_Texture) {
  2819. final display = message.field0;
  2820. final gpuTexture = message.field1;
  2821. debugPrint(
  2822. "EventToUI_Texture display:$display, gpuTexture:$gpuTexture");
  2823. if (gpuTexture && !hasGpuTextureRender) {
  2824. debugPrint('the gpuTexture is not supported.');
  2825. return;
  2826. }
  2827. textureModel.setTextureType(display: display, gpuTexture: gpuTexture);
  2828. onEvent2UIRgba();
  2829. }
  2830. }();
  2831. });
  2832. // every instance will bind a stream
  2833. this.id = id;
  2834. }
  2835. void onEvent2UIRgba() async {
  2836. if (ffiModel.waitForImageDialogShow.isTrue) {
  2837. ffiModel.waitForImageDialogShow.value = false;
  2838. ffiModel.waitForImageTimer?.cancel();
  2839. clearWaitingForImage(dialogManager, sessionId);
  2840. }
  2841. if (ffiModel.waitForFirstImage.value == true) {
  2842. ffiModel.waitForFirstImage.value = false;
  2843. dialogManager.dismissAll();
  2844. await canvasModel.updateViewStyle();
  2845. await canvasModel.updateScrollStyle();
  2846. for (final cb in imageModel.callbacksOnFirstImage) {
  2847. cb(id);
  2848. }
  2849. }
  2850. }
  2851. /// Login with [password], choose if the client should [remember] it.
  2852. void login(String osUsername, String osPassword, SessionID sessionId,
  2853. String password, bool remember) {
  2854. bind.sessionLogin(
  2855. sessionId: sessionId,
  2856. osUsername: osUsername,
  2857. osPassword: osPassword,
  2858. password: password,
  2859. remember: remember);
  2860. }
  2861. void send2FA(SessionID sessionId, String code, bool trustThisDevice) {
  2862. bind.sessionSend2Fa(
  2863. sessionId: sessionId, code: code, trustThisDevice: trustThisDevice);
  2864. }
  2865. /// Close the remote session.
  2866. Future<void> close({bool closeSession = true}) async {
  2867. closed = true;
  2868. chatModel.close();
  2869. if (imageModel.image != null && !isWebDesktop) {
  2870. await setCanvasConfig(
  2871. sessionId,
  2872. cursorModel.x,
  2873. cursorModel.y,
  2874. canvasModel.x,
  2875. canvasModel.y,
  2876. canvasModel.scale,
  2877. ffiModel.pi.currentDisplay);
  2878. }
  2879. imageModel.callbacksOnFirstImage.clear();
  2880. await imageModel.update(null);
  2881. cursorModel.clear();
  2882. ffiModel.clear();
  2883. canvasModel.clear();
  2884. inputModel.resetModifiers();
  2885. if (closeSession) {
  2886. await bind.sessionClose(sessionId: sessionId);
  2887. }
  2888. debugPrint('model $id closed');
  2889. id = '';
  2890. }
  2891. void setMethodCallHandler(FMethod callback) {
  2892. platformFFI.setMethodCallHandler(callback);
  2893. }
  2894. Future<bool> invokeMethod(String method, [dynamic arguments]) async {
  2895. return await platformFFI.invokeMethod(method, arguments);
  2896. }
  2897. }
  2898. const kInvalidResolutionValue = -1;
  2899. const kVirtualDisplayResolutionValue = 0;
  2900. class Display {
  2901. double x = 0;
  2902. double y = 0;
  2903. int width = 0;
  2904. int height = 0;
  2905. bool cursorEmbedded = false;
  2906. int originalWidth = kInvalidResolutionValue;
  2907. int originalHeight = kInvalidResolutionValue;
  2908. double _scale = 1.0;
  2909. double get scale => _scale > 1.0 ? _scale : 1.0;
  2910. Display() {
  2911. width = (isDesktop || isWebDesktop)
  2912. ? kDesktopDefaultDisplayWidth
  2913. : kMobileDefaultDisplayWidth;
  2914. height = (isDesktop || isWebDesktop)
  2915. ? kDesktopDefaultDisplayHeight
  2916. : kMobileDefaultDisplayHeight;
  2917. }
  2918. @override
  2919. bool operator ==(Object other) =>
  2920. other is Display &&
  2921. other.runtimeType == runtimeType &&
  2922. _innerEqual(other);
  2923. bool _innerEqual(Display other) =>
  2924. other.x == x &&
  2925. other.y == y &&
  2926. other.width == width &&
  2927. other.height == height &&
  2928. other.cursorEmbedded == cursorEmbedded;
  2929. bool get isOriginalResolutionSet =>
  2930. originalWidth != kInvalidResolutionValue &&
  2931. originalHeight != kInvalidResolutionValue;
  2932. bool get isVirtualDisplayResolution =>
  2933. originalWidth == kVirtualDisplayResolutionValue &&
  2934. originalHeight == kVirtualDisplayResolutionValue;
  2935. bool get isOriginalResolution =>
  2936. width == originalWidth && height == originalHeight;
  2937. }
  2938. class Resolution {
  2939. int width = 0;
  2940. int height = 0;
  2941. Resolution(this.width, this.height);
  2942. @override
  2943. String toString() {
  2944. return 'Resolution($width,$height)';
  2945. }
  2946. }
  2947. class Features {
  2948. bool privacyMode = false;
  2949. }
  2950. const kInvalidDisplayIndex = -1;
  2951. class PeerInfo with ChangeNotifier {
  2952. String version = '';
  2953. String username = '';
  2954. String hostname = '';
  2955. String platform = '';
  2956. bool sasEnabled = false;
  2957. bool isSupportMultiUiSession = false;
  2958. int currentDisplay = 0;
  2959. int primaryDisplay = kInvalidDisplayIndex;
  2960. RxList<Display> displays = <Display>[].obs;
  2961. Features features = Features();
  2962. List<Resolution> resolutions = [];
  2963. Map<String, dynamic> platformAdditions = {};
  2964. RxInt displaysCount = 0.obs;
  2965. RxBool isSet = false.obs;
  2966. bool get isWayland => platformAdditions[kPlatformAdditionsIsWayland] == true;
  2967. bool get isHeadless => platformAdditions[kPlatformAdditionsHeadless] == true;
  2968. bool get isInstalled =>
  2969. platform != kPeerPlatformWindows ||
  2970. platformAdditions[kPlatformAdditionsIsInstalled] == true;
  2971. List<int> get RustDeskVirtualDisplays => List<int>.from(
  2972. platformAdditions[kPlatformAdditionsRustDeskVirtualDisplays] ?? []);
  2973. int get amyuniVirtualDisplayCount =>
  2974. platformAdditions[kPlatformAdditionsAmyuniVirtualDisplays] ?? 0;
  2975. bool get isSupportMultiDisplay =>
  2976. (isDesktop || isWebDesktop) && isSupportMultiUiSession;
  2977. bool get forceTextureRender => currentDisplay == kAllDisplayValue;
  2978. bool get cursorEmbedded => tryGetDisplay()?.cursorEmbedded ?? false;
  2979. bool get isRustDeskIdd =>
  2980. platformAdditions[kPlatformAdditionsIddImpl] == 'rustdesk_idd';
  2981. bool get isAmyuniIdd =>
  2982. platformAdditions[kPlatformAdditionsIddImpl] == 'amyuni_idd';
  2983. bool get isSupportViewCamera =>
  2984. platformAdditions[kPlatformAdditionsSupportViewCamera] == true;
  2985. Display? tryGetDisplay({int? display}) {
  2986. if (displays.isEmpty) {
  2987. return null;
  2988. }
  2989. display ??= currentDisplay;
  2990. if (display == kAllDisplayValue) {
  2991. return displays[0];
  2992. } else {
  2993. if (display > 0 && display < displays.length) {
  2994. return displays[display];
  2995. } else {
  2996. return displays[0];
  2997. }
  2998. }
  2999. }
  3000. Display? tryGetDisplayIfNotAllDisplay({int? display}) {
  3001. if (displays.isEmpty) {
  3002. return null;
  3003. }
  3004. display ??= currentDisplay;
  3005. if (display == kAllDisplayValue) {
  3006. return null;
  3007. }
  3008. if (display >= 0 && display < displays.length) {
  3009. return displays[display];
  3010. } else {
  3011. return null;
  3012. }
  3013. }
  3014. List<Display> getCurDisplays() {
  3015. if (currentDisplay == kAllDisplayValue) {
  3016. return displays;
  3017. } else {
  3018. if (currentDisplay >= 0 && currentDisplay < displays.length) {
  3019. return [displays[currentDisplay]];
  3020. } else {
  3021. return [];
  3022. }
  3023. }
  3024. }
  3025. double scaleOfDisplay(int display) {
  3026. if (display >= 0 && display < displays.length) {
  3027. return displays[display].scale;
  3028. }
  3029. return 1.0;
  3030. }
  3031. Rect? getDisplayRect(int display) {
  3032. final d = tryGetDisplayIfNotAllDisplay(display: display);
  3033. if (d == null) return null;
  3034. return Rect.fromLTWH(d.x, d.y, d.width.toDouble(), d.height.toDouble());
  3035. }
  3036. }
  3037. const canvasKey = 'canvas';
  3038. Future<void> setCanvasConfig(
  3039. SessionID sessionId,
  3040. double xCursor,
  3041. double yCursor,
  3042. double xCanvas,
  3043. double yCanvas,
  3044. double scale,
  3045. int currentDisplay) async {
  3046. final p = <String, dynamic>{};
  3047. p['xCursor'] = xCursor;
  3048. p['yCursor'] = yCursor;
  3049. p['xCanvas'] = xCanvas;
  3050. p['yCanvas'] = yCanvas;
  3051. p['scale'] = scale;
  3052. p['currentDisplay'] = currentDisplay;
  3053. await bind.sessionSetFlutterOption(
  3054. sessionId: sessionId, k: canvasKey, v: jsonEncode(p));
  3055. }
  3056. Future<Map<String, dynamic>?> getCanvasConfig(SessionID sessionId) async {
  3057. if (!isWebDesktop) return null;
  3058. var p =
  3059. await bind.sessionGetFlutterOption(sessionId: sessionId, k: canvasKey);
  3060. if (p == null || p.isEmpty) return null;
  3061. try {
  3062. Map<String, dynamic> m = json.decode(p);
  3063. return m;
  3064. } catch (e) {
  3065. return null;
  3066. }
  3067. }
  3068. Future<void> initializeCursorAndCanvas(FFI ffi) async {
  3069. var p = await getCanvasConfig(ffi.sessionId);
  3070. int currentDisplay = 0;
  3071. if (p != null) {
  3072. currentDisplay = p['currentDisplay'];
  3073. }
  3074. if (p == null || currentDisplay != ffi.ffiModel.pi.currentDisplay) {
  3075. ffi.cursorModel.updateDisplayOrigin(
  3076. ffi.ffiModel.rect?.left ?? 0, ffi.ffiModel.rect?.top ?? 0);
  3077. return;
  3078. }
  3079. double xCursor = p['xCursor'];
  3080. double yCursor = p['yCursor'];
  3081. double xCanvas = p['xCanvas'];
  3082. double yCanvas = p['yCanvas'];
  3083. double scale = p['scale'];
  3084. ffi.cursorModel.updateDisplayOriginWithCursor(ffi.ffiModel.rect?.left ?? 0,
  3085. ffi.ffiModel.rect?.top ?? 0, xCursor, yCursor);
  3086. ffi.canvasModel.update(xCanvas, yCanvas, scale);
  3087. }
  3088. clearWaitingForImage(OverlayDialogManager? dialogManager, SessionID sessionId) {
  3089. dialogManager?.dismissByTag('$sessionId-waiting-for-image');
  3090. }