model.dart 100 KB

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