mirror of
https://github.com/weyne85/rustdesk.git
synced 2025-10-29 17:00:05 +00:00
refactor: make multi FFI object && initial flutter multi sessions support
Signed-off-by: Kingtous <kingtous@qq.com>
This commit is contained in:
@@ -41,6 +41,11 @@ class ChatModel with ChangeNotifier {
|
||||
|
||||
int get currentID => _currentID;
|
||||
|
||||
WeakReference<FFI> _ffi;
|
||||
|
||||
/// Constructor
|
||||
ChatModel(this._ffi);
|
||||
|
||||
ChatUser get currentUser {
|
||||
final user = messages[currentID]?.chatUser;
|
||||
if (user == null) {
|
||||
@@ -56,7 +61,7 @@ class ChatModel with ChangeNotifier {
|
||||
_currentID = id;
|
||||
notifyListeners();
|
||||
} else {
|
||||
final client = FFI.serverModel.clients[id];
|
||||
final client = _ffi.target?.serverModel.clients[id];
|
||||
if (client == null) {
|
||||
return debugPrint(
|
||||
"Failed to changeCurrentID,remote user doesn't exist");
|
||||
@@ -80,11 +85,11 @@ class ChatModel with ChangeNotifier {
|
||||
late final chatUser;
|
||||
if (id == clientModeID) {
|
||||
chatUser = ChatUser(
|
||||
name: FFI.ffiModel.pi.username,
|
||||
uid: FFI.getId(),
|
||||
name: _ffi.target?.ffiModel.pi.username,
|
||||
uid: _ffi.target?.getId(),
|
||||
);
|
||||
} else {
|
||||
final client = FFI.serverModel.clients[id];
|
||||
final client = _ffi.target?.serverModel.clients[id];
|
||||
if (client == null) {
|
||||
return debugPrint("Failed to receive msg,user doesn't exist");
|
||||
}
|
||||
@@ -112,12 +117,12 @@ class ChatModel with ChangeNotifier {
|
||||
if (message.text != null && message.text!.isNotEmpty) {
|
||||
_messages[_currentID]?.add(message);
|
||||
if (_currentID == clientModeID) {
|
||||
FFI.setByName("chat_client_mode", message.text!);
|
||||
_ffi.target?.setByName("chat_client_mode", message.text!);
|
||||
} else {
|
||||
final msg = Map()
|
||||
..["id"] = _currentID
|
||||
..["text"] = message.text!;
|
||||
FFI.setByName("chat_server_mode", jsonEncode(msg));
|
||||
_ffi.target?.setByName("chat_server_mode", jsonEncode(msg));
|
||||
}
|
||||
}
|
||||
notifyListeners();
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'package:flutter_hbb/common.dart';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hbb/common.dart';
|
||||
import 'package:flutter_hbb/mobile/pages/file_manager_page.dart';
|
||||
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
||||
import 'package:path/path.dart' as Path;
|
||||
@@ -69,6 +70,10 @@ class FileModel extends ChangeNotifier {
|
||||
|
||||
final _jobResultListener = JobResultListener<Map<String, dynamic>>();
|
||||
|
||||
final WeakReference<FFI> _ffi;
|
||||
|
||||
FileModel(this._ffi);
|
||||
|
||||
toggleSelectMode() {
|
||||
if (jobState == JobState.inProgress) {
|
||||
return;
|
||||
@@ -162,7 +167,7 @@ class FileModel extends ChangeNotifier {
|
||||
// overwrite
|
||||
msg['need_override'] = 'true';
|
||||
}
|
||||
FFI.setByName("set_confirm_override_file", jsonEncode(msg));
|
||||
_ffi.target?.setByName("set_confirm_override_file", jsonEncode(msg));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,20 +177,23 @@ class FileModel extends ChangeNotifier {
|
||||
}
|
||||
|
||||
onReady() async {
|
||||
_localOption.home = FFI.getByName("get_home_dir");
|
||||
_localOption.home = _ffi.target?.getByName("get_home_dir") ?? "";
|
||||
_localOption.showHidden =
|
||||
FFI.getByName("peer_option", "local_show_hidden").isNotEmpty;
|
||||
_ffi.target?.getByName("peer_option", "local_show_hidden").isNotEmpty ??
|
||||
false;
|
||||
|
||||
_remoteOption.showHidden =
|
||||
FFI.getByName("peer_option", "remote_show_hidden").isNotEmpty;
|
||||
_remoteOption.isWindows = FFI.ffiModel.pi.platform == "Windows";
|
||||
_remoteOption.showHidden = _ffi.target
|
||||
?.getByName("peer_option", "remote_show_hidden")
|
||||
.isNotEmpty ??
|
||||
false;
|
||||
_remoteOption.isWindows = _ffi.target?.ffiModel.pi.platform == "Windows";
|
||||
|
||||
debugPrint("remote platform: ${FFI.ffiModel.pi.platform}");
|
||||
debugPrint("remote platform: ${_ffi.target?.ffiModel.pi.platform}");
|
||||
|
||||
await Future.delayed(Duration(milliseconds: 100));
|
||||
|
||||
final local = FFI.getByName("peer_option", "local_dir");
|
||||
final remote = FFI.getByName("peer_option", "remote_dir");
|
||||
final local = _ffi.target?.getByName("peer_option", "local_dir") ?? "";
|
||||
final remote = _ffi.target?.getByName("peer_option", "remote_dir") ?? "";
|
||||
openDirectory(local.isEmpty ? _localOption.home : local, isLocal: true);
|
||||
openDirectory(remote.isEmpty ? _remoteOption.home : remote, isLocal: false);
|
||||
await Future.delayed(Duration(seconds: 1));
|
||||
@@ -205,19 +213,19 @@ class FileModel extends ChangeNotifier {
|
||||
|
||||
msg["name"] = "local_dir";
|
||||
msg["value"] = _currentLocalDir.path;
|
||||
FFI.setByName('peer_option', jsonEncode(msg));
|
||||
_ffi.target?.setByName('peer_option', jsonEncode(msg));
|
||||
|
||||
msg["name"] = "local_show_hidden";
|
||||
msg["value"] = _localOption.showHidden ? "Y" : "";
|
||||
FFI.setByName('peer_option', jsonEncode(msg));
|
||||
_ffi.target?.setByName('peer_option', jsonEncode(msg));
|
||||
|
||||
msg["name"] = "remote_dir";
|
||||
msg["value"] = _currentRemoteDir.path;
|
||||
FFI.setByName('peer_option', jsonEncode(msg));
|
||||
_ffi.target?.setByName('peer_option', jsonEncode(msg));
|
||||
|
||||
msg["name"] = "remote_show_hidden";
|
||||
msg["value"] = _remoteOption.showHidden ? "Y" : "";
|
||||
FFI.setByName('peer_option', jsonEncode(msg));
|
||||
_ffi.target?.setByName('peer_option', jsonEncode(msg));
|
||||
_currentLocalDir.clear();
|
||||
_currentRemoteDir.clear();
|
||||
_localOption.clear();
|
||||
@@ -279,7 +287,7 @@ class FileModel extends ChangeNotifier {
|
||||
"show_hidden": showHidden.toString(),
|
||||
"is_remote": (!(items.isLocal!)).toString()
|
||||
};
|
||||
FFI.setByName("send_files", jsonEncode(msg));
|
||||
_ffi.target?.setByName("send_files", jsonEncode(msg));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -478,7 +486,7 @@ class FileModel extends ChangeNotifier {
|
||||
"file_num": fileNum.toString(),
|
||||
"is_remote": (!(isLocal)).toString()
|
||||
};
|
||||
FFI.setByName("remove_file", jsonEncode(msg));
|
||||
_ffi.target?.setByName("remove_file", jsonEncode(msg));
|
||||
}
|
||||
|
||||
sendRemoveEmptyDir(String path, int fileNum, bool isLocal) {
|
||||
@@ -487,7 +495,7 @@ class FileModel extends ChangeNotifier {
|
||||
"path": path,
|
||||
"is_remote": (!isLocal).toString()
|
||||
};
|
||||
FFI.setByName("remove_all_empty_dirs", jsonEncode(msg));
|
||||
_ffi.target?.setByName("remove_all_empty_dirs", jsonEncode(msg));
|
||||
}
|
||||
|
||||
createDir(String path) {
|
||||
@@ -497,11 +505,11 @@ class FileModel extends ChangeNotifier {
|
||||
"path": path,
|
||||
"is_remote": (!isLocal).toString()
|
||||
};
|
||||
FFI.setByName("create_dir", jsonEncode(msg));
|
||||
_ffi.target?.setByName("create_dir", jsonEncode(msg));
|
||||
}
|
||||
|
||||
cancelJob(int id) {
|
||||
FFI.setByName("cancel_job", id.toString());
|
||||
_ffi.target?.setByName("cancel_job", id.toString());
|
||||
jobReset();
|
||||
}
|
||||
|
||||
@@ -627,11 +635,11 @@ class FileFetcher {
|
||||
try {
|
||||
final msg = {"path": path, "show_hidden": showHidden.toString()};
|
||||
if (isLocal) {
|
||||
final res = FFI.getByName("read_local_dir_sync", jsonEncode(msg));
|
||||
final res = gFFI.getByName("read_local_dir_sync", jsonEncode(msg));
|
||||
final fd = FileDirectory.fromJson(jsonDecode(res));
|
||||
return fd;
|
||||
} else {
|
||||
FFI.setByName("read_remote_dir", jsonEncode(msg));
|
||||
gFFI.setByName("read_remote_dir", jsonEncode(msg));
|
||||
return registerReadTask(isLocal, path);
|
||||
}
|
||||
} catch (e) {
|
||||
@@ -649,7 +657,7 @@ class FileFetcher {
|
||||
"show_hidden": showHidden.toString(),
|
||||
"is_remote": (!isLocal).toString()
|
||||
};
|
||||
FFI.setByName("read_dir_recursive", jsonEncode(msg));
|
||||
gFFI.setByName("read_dir_recursive", jsonEncode(msg));
|
||||
return registerReadRecursiveTask(id);
|
||||
} catch (e) {
|
||||
return Future.error(e);
|
||||
|
||||
@@ -25,6 +25,8 @@ bool _waitForImage = false;
|
||||
class FfiModel with ChangeNotifier {
|
||||
PeerInfo _pi = PeerInfo();
|
||||
Display _display = Display();
|
||||
PlatformFFI _platformFFI = PlatformFFI();
|
||||
|
||||
var _inputBlocked = false;
|
||||
final _permissions = Map<String, bool>();
|
||||
bool? _secure;
|
||||
@@ -32,11 +34,18 @@ class FfiModel with ChangeNotifier {
|
||||
bool _touchMode = false;
|
||||
Timer? _timer;
|
||||
var _reconnects = 1;
|
||||
WeakReference<FFI> parent;
|
||||
|
||||
Map<String, bool> get permissions => _permissions;
|
||||
|
||||
Display get display => _display;
|
||||
|
||||
PlatformFFI get platformFFI => _platformFFI;
|
||||
|
||||
set platformFFI(PlatformFFI value) {
|
||||
_platformFFI = value;
|
||||
}
|
||||
|
||||
bool? get secure => _secure;
|
||||
|
||||
bool? get direct => _direct;
|
||||
@@ -53,13 +62,13 @@ class FfiModel with ChangeNotifier {
|
||||
_inputBlocked = v;
|
||||
}
|
||||
|
||||
FfiModel() {
|
||||
FfiModel(this.parent) {
|
||||
Translator.call = translate;
|
||||
clear();
|
||||
}
|
||||
|
||||
Future<void> init() async {
|
||||
await PlatformFFI.init();
|
||||
await _platformFFI.init();
|
||||
}
|
||||
|
||||
void toggleTouchMode() {
|
||||
@@ -130,41 +139,41 @@ class FfiModel with ChangeNotifier {
|
||||
} else if (name == 'peer_info') {
|
||||
handlePeerInfo(evt, peerId);
|
||||
} else if (name == 'connection_ready') {
|
||||
FFI.ffiModel.setConnectionType(
|
||||
evt['secure'] == 'true', evt['direct'] == 'true');
|
||||
setConnectionType(evt['secure'] == 'true', evt['direct'] == 'true');
|
||||
} else if (name == 'switch_display') {
|
||||
handleSwitchDisplay(evt);
|
||||
} else if (name == 'cursor_data') {
|
||||
FFI.cursorModel.updateCursorData(evt);
|
||||
parent.target?.cursorModel.updateCursorData(evt);
|
||||
} else if (name == 'cursor_id') {
|
||||
FFI.cursorModel.updateCursorId(evt);
|
||||
parent.target?.cursorModel.updateCursorId(evt);
|
||||
} else if (name == 'cursor_position') {
|
||||
FFI.cursorModel.updateCursorPosition(evt);
|
||||
parent.target?.cursorModel.updateCursorPosition(evt);
|
||||
} else if (name == 'clipboard') {
|
||||
Clipboard.setData(ClipboardData(text: evt['content']));
|
||||
} else if (name == 'permission') {
|
||||
FFI.ffiModel.updatePermission(evt);
|
||||
parent.target?.ffiModel.updatePermission(evt);
|
||||
} else if (name == 'chat_client_mode') {
|
||||
FFI.chatModel.receive(ChatModel.clientModeID, evt['text'] ?? "");
|
||||
parent.target?.chatModel
|
||||
.receive(ChatModel.clientModeID, evt['text'] ?? "");
|
||||
} else if (name == 'chat_server_mode') {
|
||||
FFI.chatModel
|
||||
parent.target?.chatModel
|
||||
.receive(int.parse(evt['id'] as String), evt['text'] ?? "");
|
||||
} else if (name == 'file_dir') {
|
||||
FFI.fileModel.receiveFileDir(evt);
|
||||
parent.target?.fileModel.receiveFileDir(evt);
|
||||
} else if (name == 'job_progress') {
|
||||
FFI.fileModel.tryUpdateJobProgress(evt);
|
||||
parent.target?.fileModel.tryUpdateJobProgress(evt);
|
||||
} else if (name == 'job_done') {
|
||||
FFI.fileModel.jobDone(evt);
|
||||
parent.target?.fileModel.jobDone(evt);
|
||||
} else if (name == 'job_error') {
|
||||
FFI.fileModel.jobError(evt);
|
||||
parent.target?.fileModel.jobError(evt);
|
||||
} else if (name == 'override_file_confirm') {
|
||||
FFI.fileModel.overrideFileConfirm(evt);
|
||||
parent.target?.fileModel.overrideFileConfirm(evt);
|
||||
} else if (name == 'try_start_without_auth') {
|
||||
FFI.serverModel.loginRequest(evt);
|
||||
parent.target?.serverModel.loginRequest(evt);
|
||||
} else if (name == 'on_client_authorized') {
|
||||
FFI.serverModel.onClientAuthorized(evt);
|
||||
parent.target?.serverModel.onClientAuthorized(evt);
|
||||
} else if (name == 'on_client_remove') {
|
||||
FFI.serverModel.onClientRemove(evt);
|
||||
parent.target?.serverModel.onClientRemove(evt);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -178,44 +187,45 @@ class FfiModel with ChangeNotifier {
|
||||
} else if (name == 'peer_info') {
|
||||
handlePeerInfo(evt, peerId);
|
||||
} else if (name == 'connection_ready') {
|
||||
FFI.ffiModel.setConnectionType(
|
||||
parent.target?.ffiModel.setConnectionType(
|
||||
evt['secure'] == 'true', evt['direct'] == 'true');
|
||||
} else if (name == 'switch_display') {
|
||||
handleSwitchDisplay(evt);
|
||||
} else if (name == 'cursor_data') {
|
||||
FFI.cursorModel.updateCursorData(evt);
|
||||
parent.target?.cursorModel.updateCursorData(evt);
|
||||
} else if (name == 'cursor_id') {
|
||||
FFI.cursorModel.updateCursorId(evt);
|
||||
parent.target?.cursorModel.updateCursorId(evt);
|
||||
} else if (name == 'cursor_position') {
|
||||
FFI.cursorModel.updateCursorPosition(evt);
|
||||
parent.target?.cursorModel.updateCursorPosition(evt);
|
||||
} else if (name == 'clipboard') {
|
||||
Clipboard.setData(ClipboardData(text: evt['content']));
|
||||
} else if (name == 'permission') {
|
||||
FFI.ffiModel.updatePermission(evt);
|
||||
parent.target?.ffiModel.updatePermission(evt);
|
||||
} else if (name == 'chat_client_mode') {
|
||||
FFI.chatModel.receive(ChatModel.clientModeID, evt['text'] ?? "");
|
||||
parent.target?.chatModel
|
||||
.receive(ChatModel.clientModeID, evt['text'] ?? "");
|
||||
} else if (name == 'chat_server_mode') {
|
||||
FFI.chatModel
|
||||
parent.target?.chatModel
|
||||
.receive(int.parse(evt['id'] as String), evt['text'] ?? "");
|
||||
} else if (name == 'file_dir') {
|
||||
FFI.fileModel.receiveFileDir(evt);
|
||||
parent.target?.fileModel.receiveFileDir(evt);
|
||||
} else if (name == 'job_progress') {
|
||||
FFI.fileModel.tryUpdateJobProgress(evt);
|
||||
parent.target?.fileModel.tryUpdateJobProgress(evt);
|
||||
} else if (name == 'job_done') {
|
||||
FFI.fileModel.jobDone(evt);
|
||||
parent.target?.fileModel.jobDone(evt);
|
||||
} else if (name == 'job_error') {
|
||||
FFI.fileModel.jobError(evt);
|
||||
parent.target?.fileModel.jobError(evt);
|
||||
} else if (name == 'override_file_confirm') {
|
||||
FFI.fileModel.overrideFileConfirm(evt);
|
||||
parent.target?.fileModel.overrideFileConfirm(evt);
|
||||
} else if (name == 'try_start_without_auth') {
|
||||
FFI.serverModel.loginRequest(evt);
|
||||
parent.target?.serverModel.loginRequest(evt);
|
||||
} else if (name == 'on_client_authorized') {
|
||||
FFI.serverModel.onClientAuthorized(evt);
|
||||
parent.target?.serverModel.onClientAuthorized(evt);
|
||||
} else if (name == 'on_client_remove') {
|
||||
FFI.serverModel.onClientRemove(evt);
|
||||
parent.target?.serverModel.onClientRemove(evt);
|
||||
}
|
||||
};
|
||||
PlatformFFI.setEventCallback(cb);
|
||||
platformFFI.setEventCallback(cb);
|
||||
}
|
||||
|
||||
void handleSwitchDisplay(Map<String, dynamic> evt) {
|
||||
@@ -226,7 +236,7 @@ class FfiModel with ChangeNotifier {
|
||||
_display.width = int.parse(evt['width']);
|
||||
_display.height = int.parse(evt['height']);
|
||||
if (old != _pi.currentDisplay)
|
||||
FFI.cursorModel.updateDisplayOrigin(_display.x, _display.y);
|
||||
parent.target?.cursorModel.updateDisplayOrigin(_display.x, _display.y);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@@ -252,7 +262,7 @@ class FfiModel with ChangeNotifier {
|
||||
_timer?.cancel();
|
||||
if (hasRetry) {
|
||||
_timer = Timer(Duration(seconds: _reconnects), () {
|
||||
FFI.bind.sessionReconnect(id: id);
|
||||
parent.target?.bind.sessionReconnect(id: id);
|
||||
clearPermissions();
|
||||
showLoading(translate('Connecting...'));
|
||||
});
|
||||
@@ -274,16 +284,17 @@ class FfiModel with ChangeNotifier {
|
||||
|
||||
if (isPeerAndroid) {
|
||||
_touchMode = true;
|
||||
if (FFI.ffiModel.permissions['keyboard'] != false) {
|
||||
if (parent.target?.ffiModel.permissions['keyboard'] != false) {
|
||||
Timer(Duration(milliseconds: 100), showMobileActionsOverlay);
|
||||
}
|
||||
} else {
|
||||
_touchMode =
|
||||
await FFI.bind.getSessionOption(id: peerId, arg: "touch-mode") != '';
|
||||
_touchMode = await parent.target?.bind
|
||||
.getSessionOption(id: peerId, arg: "touch-mode") !=
|
||||
'';
|
||||
}
|
||||
|
||||
if (evt['is_file_transfer'] == "true") {
|
||||
FFI.fileModel.onReady();
|
||||
parent.target?.fileModel.onReady();
|
||||
} else {
|
||||
_pi.displays = [];
|
||||
List<dynamic> displays = json.decode(evt['displays']);
|
||||
@@ -316,18 +327,22 @@ class ImageModel with ChangeNotifier {
|
||||
|
||||
String _id = "";
|
||||
|
||||
WeakReference<FFI> parent;
|
||||
|
||||
ImageModel(this.parent);
|
||||
|
||||
void onRgba(Uint8List rgba) {
|
||||
if (_waitForImage) {
|
||||
_waitForImage = false;
|
||||
SmartDialog.dismiss();
|
||||
}
|
||||
final pid = FFI.id;
|
||||
final pid = parent.target?.id;
|
||||
ui.decodeImageFromPixels(
|
||||
rgba,
|
||||
FFI.ffiModel.display.width,
|
||||
FFI.ffiModel.display.height,
|
||||
parent.target?.ffiModel.display.width ?? 0,
|
||||
parent.target?.ffiModel.display.height ?? 0,
|
||||
isWeb ? ui.PixelFormat.rgba8888 : ui.PixelFormat.bgra8888, (image) {
|
||||
if (FFI.id != pid) return;
|
||||
if (parent.target?.id != pid) return;
|
||||
try {
|
||||
// my throw exception, because the listener maybe already dispose
|
||||
update(image);
|
||||
@@ -340,19 +355,21 @@ class ImageModel with ChangeNotifier {
|
||||
void update(ui.Image? image) {
|
||||
if (_image == null && image != null) {
|
||||
if (isWebDesktop) {
|
||||
FFI.canvasModel.updateViewStyle();
|
||||
parent.target?.canvasModel.updateViewStyle();
|
||||
} else {
|
||||
final size = MediaQueryData.fromWindow(ui.window).size;
|
||||
final xscale = size.width / image.width;
|
||||
final yscale = size.height / image.height;
|
||||
FFI.canvasModel.scale = max(xscale, yscale);
|
||||
parent.target?.canvasModel.scale = max(xscale, yscale);
|
||||
}
|
||||
if (parent.target != null) {
|
||||
initializeCursorAndCanvas(parent.target!);
|
||||
}
|
||||
initializeCursorAndCanvas();
|
||||
Future.delayed(Duration(milliseconds: 1), () {
|
||||
if (FFI.ffiModel.isPeerAndroid) {
|
||||
FFI.bind
|
||||
if (parent.target?.ffiModel.isPeerAndroid ?? false) {
|
||||
parent.target?.bind
|
||||
.sessionPeerOption(id: _id, name: "view-style", value: "shrink");
|
||||
FFI.canvasModel.updateViewStyle();
|
||||
parent.target?.canvasModel.updateViewStyle();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -383,7 +400,9 @@ class CanvasModel with ChangeNotifier {
|
||||
double _scale = 1.0;
|
||||
String id = ""; // TODO multi canvas model
|
||||
|
||||
CanvasModel();
|
||||
WeakReference<FFI> parent;
|
||||
|
||||
CanvasModel(this.parent);
|
||||
|
||||
double get x => _x;
|
||||
|
||||
@@ -392,13 +411,14 @@ class CanvasModel with ChangeNotifier {
|
||||
double get scale => _scale;
|
||||
|
||||
void updateViewStyle() async {
|
||||
final s = await FFI.bind.getSessionOption(id: id, arg: 'view-style');
|
||||
final s =
|
||||
await parent.target?.bind.getSessionOption(id: id, arg: 'view-style');
|
||||
if (s == null) {
|
||||
return;
|
||||
}
|
||||
final size = MediaQueryData.fromWindow(ui.window).size;
|
||||
final s1 = size.width / FFI.ffiModel.display.width;
|
||||
final s2 = size.height / FFI.ffiModel.display.height;
|
||||
final s1 = size.width / (parent.target?.ffiModel.display.width ?? 720);
|
||||
final s2 = size.height / (parent.target?.ffiModel.display.height ?? 1280);
|
||||
if (s == 'shrink') {
|
||||
final s = s1 < s2 ? s1 : s2;
|
||||
if (s < 1) {
|
||||
@@ -412,8 +432,8 @@ class CanvasModel with ChangeNotifier {
|
||||
} else {
|
||||
_scale = 1;
|
||||
}
|
||||
_x = (size.width - FFI.ffiModel.display.width * _scale) / 2;
|
||||
_y = (size.height - FFI.ffiModel.display.height * _scale) / 2;
|
||||
_x = (size.width - getDisplayWidth() * _scale) / 2;
|
||||
_y = (size.height - getDisplayHeight() * _scale) / 2;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@@ -424,10 +444,18 @@ class CanvasModel with ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
int getDisplayWidth() {
|
||||
return parent.target?.ffiModel.display.width ?? 1080;
|
||||
}
|
||||
|
||||
int getDisplayHeight() {
|
||||
return parent.target?.ffiModel.display.height ?? 720;
|
||||
}
|
||||
|
||||
void moveDesktopMouse(double x, double y) {
|
||||
final size = MediaQueryData.fromWindow(ui.window).size;
|
||||
final dw = FFI.ffiModel.display.width * _scale;
|
||||
final dh = FFI.ffiModel.display.height * _scale;
|
||||
final dw = getDisplayWidth() * _scale;
|
||||
final dh = getDisplayHeight() * _scale;
|
||||
var dxOffset = 0;
|
||||
var dyOffset = 0;
|
||||
if (dw > size.width) {
|
||||
@@ -441,7 +469,7 @@ class CanvasModel with ChangeNotifier {
|
||||
if (dxOffset != 0 || dyOffset != 0) {
|
||||
notifyListeners();
|
||||
}
|
||||
FFI.cursorModel.moveLocal(x, y);
|
||||
parent.target?.cursorModel.moveLocal(x, y);
|
||||
}
|
||||
|
||||
set scale(v) {
|
||||
@@ -470,17 +498,17 @@ class CanvasModel with ChangeNotifier {
|
||||
}
|
||||
|
||||
void updateScale(double v) {
|
||||
if (FFI.imageModel.image == null) return;
|
||||
final offset = FFI.cursorModel.offset;
|
||||
var r = FFI.cursorModel.getVisibleRect();
|
||||
if (parent.target?.imageModel.image == null) return;
|
||||
final offset = parent.target?.cursorModel.offset ?? Offset(0, 0);
|
||||
var r = parent.target?.cursorModel.getVisibleRect() ?? Rect.zero;
|
||||
final px0 = (offset.dx - r.left) * _scale;
|
||||
final py0 = (offset.dy - r.top) * _scale;
|
||||
_scale *= v;
|
||||
final maxs = FFI.imageModel.maxScale;
|
||||
final mins = FFI.imageModel.minScale;
|
||||
final maxs = parent.target?.imageModel.maxScale ?? 1;
|
||||
final mins = parent.target?.imageModel.minScale ?? 1;
|
||||
if (_scale > maxs) _scale = maxs;
|
||||
if (_scale < mins) _scale = mins;
|
||||
r = FFI.cursorModel.getVisibleRect();
|
||||
r = parent.target?.cursorModel.getVisibleRect() ?? Rect.zero;
|
||||
final px1 = (offset.dx - r.left) * _scale;
|
||||
final py1 = (offset.dy - r.top) * _scale;
|
||||
_x -= px1 - px0;
|
||||
@@ -506,6 +534,7 @@ class CursorModel with ChangeNotifier {
|
||||
double _displayOriginX = 0;
|
||||
double _displayOriginY = 0;
|
||||
String id = ""; // TODO multi cursor model
|
||||
WeakReference<FFI> parent;
|
||||
|
||||
ui.Image? get image => _image;
|
||||
|
||||
@@ -519,12 +548,14 @@ class CursorModel with ChangeNotifier {
|
||||
|
||||
double get hoty => _hoty;
|
||||
|
||||
CursorModel(this.parent);
|
||||
|
||||
// remote physical display coordinate
|
||||
Rect getVisibleRect() {
|
||||
final size = MediaQueryData.fromWindow(ui.window).size;
|
||||
final xoffset = FFI.canvasModel.x;
|
||||
final yoffset = FFI.canvasModel.y;
|
||||
final scale = FFI.canvasModel.scale;
|
||||
final xoffset = parent.target?.canvasModel.x ?? 0;
|
||||
final yoffset = parent.target?.canvasModel.y ?? 0;
|
||||
final scale = parent.target?.canvasModel.scale ?? 1;
|
||||
final x0 = _displayOriginX - xoffset / scale;
|
||||
final y0 = _displayOriginY - yoffset / scale;
|
||||
return Rect.fromLTWH(x0, y0, size.width / scale, size.height / scale);
|
||||
@@ -535,7 +566,7 @@ class CursorModel with ChangeNotifier {
|
||||
var keyboardHeight = m.viewInsets.bottom;
|
||||
final size = m.size;
|
||||
if (keyboardHeight < 100) return 0;
|
||||
final s = FFI.canvasModel.scale;
|
||||
final s = parent.target?.canvasModel.scale ?? 1.0;
|
||||
final thresh = (size.height - keyboardHeight) / 2;
|
||||
var h = (_y - getVisibleRect().top) * s; // local physical display height
|
||||
return h - thresh;
|
||||
@@ -543,19 +574,19 @@ class CursorModel with ChangeNotifier {
|
||||
|
||||
void touch(double x, double y, MouseButtons button) {
|
||||
moveLocal(x, y);
|
||||
FFI.moveMouse(_x, _y);
|
||||
FFI.tap(button);
|
||||
parent.target?.moveMouse(_x, _y);
|
||||
parent.target?.tap(button);
|
||||
}
|
||||
|
||||
void move(double x, double y) {
|
||||
moveLocal(x, y);
|
||||
FFI.moveMouse(_x, _y);
|
||||
parent.target?.moveMouse(_x, _y);
|
||||
}
|
||||
|
||||
void moveLocal(double x, double y) {
|
||||
final scale = FFI.canvasModel.scale;
|
||||
final xoffset = FFI.canvasModel.x;
|
||||
final yoffset = FFI.canvasModel.y;
|
||||
final scale = parent.target?.canvasModel.scale ?? 1.0;
|
||||
final xoffset = parent.target?.canvasModel.x ?? 0;
|
||||
final yoffset = parent.target?.canvasModel.y ?? 0;
|
||||
_x = (x - xoffset) / scale + _displayOriginX;
|
||||
_y = (y - yoffset) / scale + _displayOriginY;
|
||||
notifyListeners();
|
||||
@@ -564,22 +595,22 @@ class CursorModel with ChangeNotifier {
|
||||
void reset() {
|
||||
_x = _displayOriginX;
|
||||
_y = _displayOriginY;
|
||||
FFI.moveMouse(_x, _y);
|
||||
FFI.canvasModel.clear(true);
|
||||
parent.target?.moveMouse(_x, _y);
|
||||
parent.target?.canvasModel.clear(true);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void updatePan(double dx, double dy, bool touchMode) {
|
||||
if (FFI.imageModel.image == null) return;
|
||||
if (parent.target?.imageModel.image == null) return;
|
||||
if (touchMode) {
|
||||
final scale = FFI.canvasModel.scale;
|
||||
final scale = parent.target?.canvasModel.scale ?? 1.0;
|
||||
_x += dx / scale;
|
||||
_y += dy / scale;
|
||||
FFI.moveMouse(_x, _y);
|
||||
parent.target?.moveMouse(_x, _y);
|
||||
notifyListeners();
|
||||
return;
|
||||
}
|
||||
final scale = FFI.canvasModel.scale;
|
||||
final scale = parent.target?.canvasModel.scale ?? 1.0;
|
||||
dx /= scale;
|
||||
dy /= scale;
|
||||
final r = getVisibleRect();
|
||||
@@ -588,7 +619,7 @@ class CursorModel with ChangeNotifier {
|
||||
var tryMoveCanvasX = false;
|
||||
if (dx > 0) {
|
||||
final maxCanvasCanMove = _displayOriginX +
|
||||
FFI.imageModel.image!.width -
|
||||
(parent.target?.imageModel.image!.width ?? 1280) -
|
||||
r.right.roundToDouble();
|
||||
tryMoveCanvasX = _x + dx > cx && maxCanvasCanMove > 0;
|
||||
if (tryMoveCanvasX) {
|
||||
@@ -610,7 +641,7 @@ class CursorModel with ChangeNotifier {
|
||||
var tryMoveCanvasY = false;
|
||||
if (dy > 0) {
|
||||
final mayCanvasCanMove = _displayOriginY +
|
||||
FFI.imageModel.image!.height -
|
||||
(parent.target?.imageModel.image!.height ?? 720) -
|
||||
r.bottom.roundToDouble();
|
||||
tryMoveCanvasY = _y + dy > cy && mayCanvasCanMove > 0;
|
||||
if (tryMoveCanvasY) {
|
||||
@@ -634,13 +665,13 @@ class CursorModel with ChangeNotifier {
|
||||
_x += dx;
|
||||
_y += dy;
|
||||
if (tryMoveCanvasX && dx != 0) {
|
||||
FFI.canvasModel.panX(-dx);
|
||||
parent.target?.canvasModel.panX(-dx);
|
||||
}
|
||||
if (tryMoveCanvasY && dy != 0) {
|
||||
FFI.canvasModel.panY(-dy);
|
||||
parent.target?.canvasModel.panY(-dy);
|
||||
}
|
||||
|
||||
FFI.moveMouse(_x, _y);
|
||||
parent.target?.moveMouse(_x, _y);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@@ -652,10 +683,10 @@ class CursorModel with ChangeNotifier {
|
||||
var height = int.parse(evt['height']);
|
||||
List<dynamic> colors = json.decode(evt['colors']);
|
||||
final rgba = Uint8List.fromList(colors.map((s) => s as int).toList());
|
||||
var pid = FFI.id;
|
||||
var pid = parent.target?.id;
|
||||
ui.decodeImageFromPixels(rgba, width, height, ui.PixelFormat.rgba8888,
|
||||
(image) {
|
||||
if (FFI.id != pid) return;
|
||||
if (parent.target?.id != pid) return;
|
||||
_image = image;
|
||||
_images[id] = Tuple3(image, _hotx, _hoty);
|
||||
try {
|
||||
@@ -688,8 +719,8 @@ class CursorModel with ChangeNotifier {
|
||||
_displayOriginY = y;
|
||||
_x = x + 1;
|
||||
_y = y + 1;
|
||||
FFI.moveMouse(x, y);
|
||||
FFI.canvasModel.resetOffset();
|
||||
parent.target?.moveMouse(x, y);
|
||||
parent.target?.canvasModel.resetOffset();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@@ -699,7 +730,7 @@ class CursorModel with ChangeNotifier {
|
||||
_displayOriginY = y;
|
||||
_x = xCursor;
|
||||
_y = yCursor;
|
||||
FFI.moveMouse(x, y);
|
||||
parent.target?.moveMouse(x, y);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@@ -729,33 +760,43 @@ extension ToString on MouseButtons {
|
||||
|
||||
/// FFI class for communicating with the Rust core.
|
||||
class FFI {
|
||||
static var id = "";
|
||||
static var shift = false;
|
||||
static var ctrl = false;
|
||||
static var alt = false;
|
||||
static var command = false;
|
||||
static var version = "";
|
||||
static final imageModel = ImageModel();
|
||||
static final ffiModel = FfiModel();
|
||||
static final cursorModel = CursorModel();
|
||||
static final canvasModel = CanvasModel();
|
||||
static final serverModel = ServerModel();
|
||||
static final chatModel = ChatModel();
|
||||
static final fileModel = FileModel();
|
||||
var id = "";
|
||||
var shift = false;
|
||||
var ctrl = false;
|
||||
var alt = false;
|
||||
var command = false;
|
||||
var version = "";
|
||||
late final ImageModel imageModel;
|
||||
late final FfiModel ffiModel;
|
||||
late final CursorModel cursorModel;
|
||||
late final CanvasModel canvasModel;
|
||||
late final ServerModel serverModel;
|
||||
late final ChatModel chatModel;
|
||||
late final FileModel fileModel;
|
||||
|
||||
FFI() {
|
||||
this.imageModel = ImageModel(WeakReference(this));
|
||||
this.ffiModel = FfiModel(WeakReference(this));
|
||||
this.cursorModel = CursorModel(WeakReference(this));
|
||||
this.canvasModel = CanvasModel(WeakReference(this));
|
||||
this.serverModel = ServerModel(WeakReference(this)); // use global FFI
|
||||
this.chatModel = ChatModel(WeakReference(this));
|
||||
this.fileModel = FileModel(WeakReference(this));
|
||||
}
|
||||
|
||||
/// Get the remote id for current client.
|
||||
static String getId() {
|
||||
String getId() {
|
||||
return getByName('remote_id'); // TODO
|
||||
}
|
||||
|
||||
/// Send a mouse tap event(down and up).
|
||||
static void tap(MouseButtons button) {
|
||||
void tap(MouseButtons button) {
|
||||
sendMouse('down', button);
|
||||
sendMouse('up', button);
|
||||
}
|
||||
|
||||
/// Send scroll event with scroll distance [y].
|
||||
static void scroll(int y) {
|
||||
void scroll(int y) {
|
||||
setByName('send_mouse',
|
||||
json.encode(modify({'id': id, 'type': 'wheel', 'y': y.toString()})));
|
||||
}
|
||||
@@ -763,16 +804,16 @@ class FFI {
|
||||
/// Reconnect to the remote peer.
|
||||
// static void reconnect() {
|
||||
// setByName('reconnect');
|
||||
// FFI.ffiModel.clearPermissions();
|
||||
// parent.target?.ffiModel.clearPermissions();
|
||||
// }
|
||||
|
||||
/// Reset key modifiers to false, including [shift], [ctrl], [alt] and [command].
|
||||
static void resetModifiers() {
|
||||
void resetModifiers() {
|
||||
shift = ctrl = alt = command = false;
|
||||
}
|
||||
|
||||
/// Modify the given modifier map [evt] based on current modifier key status.
|
||||
static Map<String, String> modify(Map<String, String> evt) {
|
||||
Map<String, String> modify(Map<String, String> evt) {
|
||||
if (ctrl) evt['ctrl'] = 'true';
|
||||
if (shift) evt['shift'] = 'true';
|
||||
if (alt) evt['alt'] = 'true';
|
||||
@@ -781,7 +822,7 @@ class FFI {
|
||||
}
|
||||
|
||||
/// Send mouse press event.
|
||||
static void sendMouse(String type, MouseButtons button) {
|
||||
void sendMouse(String type, MouseButtons button) {
|
||||
if (!ffiModel.keyboard()) return;
|
||||
setByName('send_mouse',
|
||||
json.encode(modify({'id': id, 'type': type, 'buttons': button.value})));
|
||||
@@ -790,7 +831,7 @@ class FFI {
|
||||
/// Send key stroke event.
|
||||
/// [down] indicates the key's state(down or up).
|
||||
/// [press] indicates a click event(down and up).
|
||||
static void inputKey(String name, {bool? down, bool? press}) {
|
||||
void inputKey(String name, {bool? down, bool? press}) {
|
||||
if (!ffiModel.keyboard()) return;
|
||||
// final Map<String, String> out = Map();
|
||||
// out['name'] = name;
|
||||
@@ -804,7 +845,7 @@ class FFI {
|
||||
// }
|
||||
// setByName('input_key', json.encode(modify(out)));
|
||||
// TODO id
|
||||
FFI.bind.sessionInputKey(
|
||||
bind.sessionInputKey(
|
||||
id: id,
|
||||
name: name,
|
||||
down: down ?? false,
|
||||
@@ -816,7 +857,7 @@ class FFI {
|
||||
}
|
||||
|
||||
/// Send mouse movement event with distance in [x] and [y].
|
||||
static void moveMouse(double x, double y) {
|
||||
void moveMouse(double x, double y) {
|
||||
if (!ffiModel.keyboard()) return;
|
||||
var x2 = x.toInt();
|
||||
var y2 = y.toInt();
|
||||
@@ -825,7 +866,7 @@ class FFI {
|
||||
}
|
||||
|
||||
/// List the saved peers.
|
||||
static List<Peer> peers() {
|
||||
List<Peer> peers() {
|
||||
try {
|
||||
var str = getByName('peers'); // TODO
|
||||
if (str == "") return [];
|
||||
@@ -842,19 +883,19 @@ class FFI {
|
||||
}
|
||||
|
||||
/// Connect with the given [id]. Only transfer file if [isFileTransfer].
|
||||
static void connect(String id, {bool isFileTransfer = false}) {
|
||||
void connect(String id, {bool isFileTransfer = false}) {
|
||||
if (isFileTransfer) {
|
||||
setByName('connect_file_transfer', id);
|
||||
} else {
|
||||
FFI.chatModel.resetClientMode();
|
||||
chatModel.resetClientMode();
|
||||
// setByName('connect', id);
|
||||
// TODO multi model instances
|
||||
FFI.canvasModel.id = id;
|
||||
FFI.imageModel._id = id;
|
||||
FFI.cursorModel.id = id;
|
||||
canvasModel.id = id;
|
||||
imageModel._id = id;
|
||||
cursorModel.id = id;
|
||||
final stream =
|
||||
FFI.bind.sessionConnect(id: id, isFileTransfer: isFileTransfer);
|
||||
final cb = FFI.ffiModel.startEventListener(id);
|
||||
bind.sessionConnect(id: id, isFileTransfer: isFileTransfer);
|
||||
final cb = ffiModel.startEventListener(id);
|
||||
() async {
|
||||
await for (final message in stream) {
|
||||
if (message is Event) {
|
||||
@@ -866,28 +907,28 @@ class FFI {
|
||||
print('json.decode fail(): $e');
|
||||
}
|
||||
} else if (message is Rgba) {
|
||||
FFI.imageModel.onRgba(message.field0);
|
||||
imageModel.onRgba(message.field0);
|
||||
}
|
||||
}
|
||||
}();
|
||||
// every instance will bind a stream
|
||||
}
|
||||
FFI.id = id;
|
||||
id = id;
|
||||
}
|
||||
|
||||
/// Login with [password], choose if the client should [remember] it.
|
||||
static void login(String id, String password, bool remember) {
|
||||
FFI.bind.sessionLogin(id: id, password: password, remember: remember);
|
||||
void login(String id, String password, bool remember) {
|
||||
bind.sessionLogin(id: id, password: password, remember: remember);
|
||||
}
|
||||
|
||||
/// Close the remote session.
|
||||
static void close() {
|
||||
void close() {
|
||||
chatModel.close();
|
||||
if (FFI.imageModel.image != null && !isWebDesktop) {
|
||||
if (imageModel.image != null && !isWebDesktop) {
|
||||
savePreference(id, cursorModel.x, cursorModel.y, canvasModel.x,
|
||||
canvasModel.y, canvasModel.scale, ffiModel.pi.currentDisplay);
|
||||
}
|
||||
FFI.bind.sessionClose(id: id);
|
||||
bind.sessionClose(id: id);
|
||||
id = "";
|
||||
imageModel.update(null);
|
||||
cursorModel.clear();
|
||||
@@ -898,18 +939,18 @@ class FFI {
|
||||
|
||||
/// Send **get** command to the Rust core based on [name] and [arg].
|
||||
/// Return the result as a string.
|
||||
static String getByName(String name, [String arg = '']) {
|
||||
return PlatformFFI.getByName(name, arg);
|
||||
String getByName(String name, [String arg = '']) {
|
||||
return ffiModel.platformFFI.getByName(name, arg);
|
||||
}
|
||||
|
||||
/// Send **set** command to the Rust core based on [name] and [value].
|
||||
static void setByName(String name, [String value = '']) {
|
||||
PlatformFFI.setByName(name, value);
|
||||
void setByName(String name, [String value = '']) {
|
||||
ffiModel.platformFFI.setByName(name, value);
|
||||
}
|
||||
|
||||
static RustdeskImpl get bind => PlatformFFI.ffiBind;
|
||||
RustdeskImpl get bind => ffiModel.platformFFI.ffiBind;
|
||||
|
||||
static handleMouse(Map<String, dynamic> evt) {
|
||||
handleMouse(Map<String, dynamic> evt) {
|
||||
var type = '';
|
||||
var isMove = false;
|
||||
switch (evt['type']) {
|
||||
@@ -929,16 +970,16 @@ class FFI {
|
||||
var x = evt['x'];
|
||||
var y = evt['y'];
|
||||
if (isMove) {
|
||||
FFI.canvasModel.moveDesktopMouse(x, y);
|
||||
canvasModel.moveDesktopMouse(x, y);
|
||||
}
|
||||
final d = FFI.ffiModel.display;
|
||||
x -= FFI.canvasModel.x;
|
||||
y -= FFI.canvasModel.y;
|
||||
final d = ffiModel.display;
|
||||
x -= canvasModel.x;
|
||||
y -= canvasModel.y;
|
||||
if (!isMove && (x < 0 || x > d.width || y < 0 || y > d.height)) {
|
||||
return;
|
||||
}
|
||||
x /= FFI.canvasModel.scale;
|
||||
y /= FFI.canvasModel.scale;
|
||||
x /= canvasModel.scale;
|
||||
y /= canvasModel.scale;
|
||||
x += d.x;
|
||||
y += d.y;
|
||||
if (type != '') {
|
||||
@@ -964,20 +1005,20 @@ class FFI {
|
||||
setByName('send_mouse', json.encode(evt));
|
||||
}
|
||||
|
||||
static listenToMouse(bool yesOrNo) {
|
||||
listenToMouse(bool yesOrNo) {
|
||||
if (yesOrNo) {
|
||||
PlatformFFI.startDesktopWebListener();
|
||||
ffiModel.platformFFI.startDesktopWebListener();
|
||||
} else {
|
||||
PlatformFFI.stopDesktopWebListener();
|
||||
ffiModel.platformFFI.stopDesktopWebListener();
|
||||
}
|
||||
}
|
||||
|
||||
static void setMethodCallHandler(FMethod callback) {
|
||||
PlatformFFI.setMethodCallHandler(callback);
|
||||
void setMethodCallHandler(FMethod callback) {
|
||||
ffiModel.platformFFI.setMethodCallHandler(callback);
|
||||
}
|
||||
|
||||
static Future<bool> invokeMethod(String method, [dynamic arguments]) async {
|
||||
return await PlatformFFI.invokeMethod(method, arguments);
|
||||
Future<bool> invokeMethod(String method, [dynamic arguments]) async {
|
||||
return await ffiModel.platformFFI.invokeMethod(method, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1038,15 +1079,15 @@ void removePreference(String id) async {
|
||||
prefs.remove('peer' + id);
|
||||
}
|
||||
|
||||
void initializeCursorAndCanvas() async {
|
||||
var p = await getPreference(FFI.id);
|
||||
void initializeCursorAndCanvas(FFI ffi) async {
|
||||
var p = await getPreference(ffi.id);
|
||||
int currentDisplay = 0;
|
||||
if (p != null) {
|
||||
currentDisplay = p['currentDisplay'];
|
||||
}
|
||||
if (p == null || currentDisplay != FFI.ffiModel.pi.currentDisplay) {
|
||||
FFI.cursorModel
|
||||
.updateDisplayOrigin(FFI.ffiModel.display.x, FFI.ffiModel.display.y);
|
||||
if (p == null || currentDisplay != ffi.ffiModel.pi.currentDisplay) {
|
||||
ffi.cursorModel
|
||||
.updateDisplayOrigin(ffi.ffiModel.display.x, ffi.ffiModel.display.y);
|
||||
return;
|
||||
}
|
||||
double xCursor = p['xCursor'];
|
||||
@@ -1054,17 +1095,19 @@ void initializeCursorAndCanvas() async {
|
||||
double xCanvas = p['xCanvas'];
|
||||
double yCanvas = p['yCanvas'];
|
||||
double scale = p['scale'];
|
||||
FFI.cursorModel.updateDisplayOriginWithCursor(
|
||||
FFI.ffiModel.display.x, FFI.ffiModel.display.y, xCursor, yCursor);
|
||||
FFI.canvasModel.update(xCanvas, yCanvas, scale);
|
||||
ffi.cursorModel.updateDisplayOriginWithCursor(
|
||||
ffi.ffiModel.display.x, ffi.ffiModel.display.y, xCursor, yCursor);
|
||||
ffi.canvasModel.update(xCanvas, yCanvas, scale);
|
||||
}
|
||||
|
||||
/// Translate text based on the pre-defined dictionary.
|
||||
String translate(String name) {
|
||||
/// note: params [FFI?] can be used to replace global FFI implementation
|
||||
/// for example: during global initialization, gFFI not exists yet.
|
||||
String translate(String name, {FFI? ffi}) {
|
||||
if (name.startsWith('Failed to') && name.contains(': ')) {
|
||||
return name.split(': ').map((x) => translate(x)).join(': ');
|
||||
}
|
||||
var a = 'translate';
|
||||
var b = '{"locale": "$localeName", "text": "$name"}';
|
||||
return FFI.getByName(a, b);
|
||||
return (ffi ?? gFFI).getByName(a, b);
|
||||
}
|
||||
|
||||
@@ -25,15 +25,15 @@ typedef F3 = void Function(Pointer<Utf8>, Pointer<Utf8>);
|
||||
/// FFI wrapper around the native Rust core.
|
||||
/// Hides the platform differences.
|
||||
class PlatformFFI {
|
||||
static Pointer<RgbaFrame>? _lastRgbaFrame;
|
||||
static String _dir = '';
|
||||
static String _homeDir = '';
|
||||
static F2? _getByName;
|
||||
static F3? _setByName;
|
||||
static late RustdeskImpl _ffiBind;
|
||||
static void Function(Map<String, dynamic>)? _eventCallback;
|
||||
Pointer<RgbaFrame>? _lastRgbaFrame;
|
||||
String _dir = '';
|
||||
String _homeDir = '';
|
||||
F2? _getByName;
|
||||
F3? _setByName;
|
||||
late RustdeskImpl _ffiBind;
|
||||
void Function(Map<String, dynamic>)? _eventCallback;
|
||||
|
||||
static RustdeskImpl get ffiBind => _ffiBind;
|
||||
RustdeskImpl get ffiBind => _ffiBind;
|
||||
|
||||
static Future<String> getVersion() async {
|
||||
PackageInfo packageInfo = await PackageInfo.fromPlatform();
|
||||
@@ -42,7 +42,7 @@ class PlatformFFI {
|
||||
|
||||
/// Send **get** command to the Rust core based on [name] and [arg].
|
||||
/// Return the result as a string.
|
||||
static String getByName(String name, [String arg = '']) {
|
||||
String getByName(String name, [String arg = '']) {
|
||||
if (_getByName == null) return '';
|
||||
var a = name.toNativeUtf8();
|
||||
var b = arg.toNativeUtf8();
|
||||
@@ -56,7 +56,7 @@ class PlatformFFI {
|
||||
}
|
||||
|
||||
/// Send **set** command to the Rust core based on [name] and [value].
|
||||
static void setByName(String name, [String value = '']) {
|
||||
void setByName(String name, [String value = '']) {
|
||||
if (_setByName == null) return;
|
||||
var a = name.toNativeUtf8();
|
||||
var b = value.toNativeUtf8();
|
||||
@@ -66,7 +66,7 @@ class PlatformFFI {
|
||||
}
|
||||
|
||||
/// Init the FFI class, loads the native Rust core library.
|
||||
static Future<Null> init() async {
|
||||
Future<Null> init() async {
|
||||
isIOS = Platform.isIOS;
|
||||
isAndroid = Platform.isAndroid;
|
||||
isDesktop = Platform.isWindows || Platform.isMacOS || Platform.isLinux;
|
||||
@@ -134,7 +134,7 @@ class PlatformFFI {
|
||||
}
|
||||
|
||||
/// Start listening to the Rust core's events and frames.
|
||||
static void _startListenEvent(RustdeskImpl rustdeskImpl) {
|
||||
void _startListenEvent(RustdeskImpl rustdeskImpl) {
|
||||
() async {
|
||||
await for (final message in rustdeskImpl.startGlobalEventStream()) {
|
||||
if (_eventCallback != null) {
|
||||
@@ -149,24 +149,24 @@ class PlatformFFI {
|
||||
}();
|
||||
}
|
||||
|
||||
static void setEventCallback(void Function(Map<String, dynamic>) fun) async {
|
||||
void setEventCallback(void Function(Map<String, dynamic>) fun) async {
|
||||
_eventCallback = fun;
|
||||
}
|
||||
|
||||
static void setRgbaCallback(void Function(Uint8List) fun) async {}
|
||||
void setRgbaCallback(void Function(Uint8List) fun) async {}
|
||||
|
||||
static void startDesktopWebListener() {}
|
||||
void startDesktopWebListener() {}
|
||||
|
||||
static void stopDesktopWebListener() {}
|
||||
void stopDesktopWebListener() {}
|
||||
|
||||
static void setMethodCallHandler(FMethod callback) {
|
||||
void setMethodCallHandler(FMethod callback) {
|
||||
toAndroidChannel.setMethodCallHandler((call) async {
|
||||
callback(call.method, call.arguments);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
static invokeMethod(String method, [dynamic arguments]) async {
|
||||
invokeMethod(String method, [dynamic arguments]) async {
|
||||
if (!isAndroid) return Future<bool>(() => false);
|
||||
return await toAndroidChannel.invokeMethod(method, arguments);
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import '../mobile/pages/server_page.dart';
|
||||
import 'model.dart';
|
||||
|
||||
const loginDialogTag = "LOGIN";
|
||||
final _emptyIdShow = translate("Generating ...");
|
||||
|
||||
class ServerModel with ChangeNotifier {
|
||||
bool _isStart = false; // Android MainService status
|
||||
@@ -20,7 +19,8 @@ class ServerModel with ChangeNotifier {
|
||||
bool _fileOk = false;
|
||||
int _connectStatus = 0; // Rendezvous Server status
|
||||
|
||||
final _serverId = TextEditingController(text: _emptyIdShow);
|
||||
late String _emptyIdShow;
|
||||
late final TextEditingController _serverId;
|
||||
final _serverPasswd = TextEditingController(text: "");
|
||||
|
||||
Map<int, Client> _clients = {};
|
||||
@@ -45,8 +45,12 @@ class ServerModel with ChangeNotifier {
|
||||
|
||||
final controller = ScrollController();
|
||||
|
||||
ServerModel() {
|
||||
WeakReference<FFI> parent;
|
||||
|
||||
ServerModel(this.parent) {
|
||||
() async {
|
||||
_emptyIdShow = translate("Generating ...", ffi: this.parent.target);
|
||||
_serverId = TextEditingController(text: this._emptyIdShow);
|
||||
/**
|
||||
* 1. check android permission
|
||||
* 2. check config
|
||||
@@ -59,39 +63,42 @@ class ServerModel with ChangeNotifier {
|
||||
// audio
|
||||
if (androidVersion < 30 || !await PermissionManager.check("audio")) {
|
||||
_audioOk = false;
|
||||
FFI.setByName(
|
||||
parent.target?.setByName(
|
||||
'option',
|
||||
jsonEncode(Map()
|
||||
..["name"] = "enable-audio"
|
||||
..["value"] = "N"));
|
||||
} else {
|
||||
final audioOption = FFI.getByName('option', 'enable-audio');
|
||||
_audioOk = audioOption.isEmpty;
|
||||
final audioOption = parent.target?.getByName('option', 'enable-audio');
|
||||
_audioOk = audioOption?.isEmpty ?? false;
|
||||
}
|
||||
|
||||
// file
|
||||
if (!await PermissionManager.check("file")) {
|
||||
_fileOk = false;
|
||||
FFI.setByName(
|
||||
parent.target?.setByName(
|
||||
'option',
|
||||
jsonEncode(Map()
|
||||
..["name"] = "enable-file-transfer"
|
||||
..["value"] = "N"));
|
||||
} else {
|
||||
final fileOption = FFI.getByName('option', 'enable-file-transfer');
|
||||
_fileOk = fileOption.isEmpty;
|
||||
final fileOption =
|
||||
parent.target?.getByName('option', 'enable-file-transfer');
|
||||
_fileOk = fileOption?.isEmpty ?? false;
|
||||
}
|
||||
|
||||
// input (mouse control)
|
||||
Map<String, String> res = Map()
|
||||
..["name"] = "enable-keyboard"
|
||||
..["value"] = 'N';
|
||||
FFI.setByName('option', jsonEncode(res)); // input false by default
|
||||
parent.target
|
||||
?.setByName('option', jsonEncode(res)); // input false by default
|
||||
notifyListeners();
|
||||
}();
|
||||
|
||||
Timer.periodic(Duration(seconds: 1), (timer) {
|
||||
var status = int.tryParse(FFI.getByName('connect_statue')) ?? 0;
|
||||
var status =
|
||||
int.tryParse(parent.target?.getByName('connect_statue') ?? "") ?? 0;
|
||||
if (status > 0) {
|
||||
status = 1;
|
||||
}
|
||||
@@ -99,8 +106,9 @@ class ServerModel with ChangeNotifier {
|
||||
_connectStatus = status;
|
||||
notifyListeners();
|
||||
}
|
||||
final res =
|
||||
FFI.getByName('check_clients_length', _clients.length.toString());
|
||||
final res = parent.target
|
||||
?.getByName('check_clients_length', _clients.length.toString()) ??
|
||||
"";
|
||||
if (res.isNotEmpty) {
|
||||
debugPrint("clients not match!");
|
||||
updateClientState(res);
|
||||
@@ -121,7 +129,7 @@ class ServerModel with ChangeNotifier {
|
||||
Map<String, String> res = Map()
|
||||
..["name"] = "enable-audio"
|
||||
..["value"] = _audioOk ? '' : 'N';
|
||||
FFI.setByName('option', jsonEncode(res));
|
||||
parent.target?.setByName('option', jsonEncode(res));
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@@ -138,15 +146,17 @@ class ServerModel with ChangeNotifier {
|
||||
Map<String, String> res = Map()
|
||||
..["name"] = "enable-file-transfer"
|
||||
..["value"] = _fileOk ? '' : 'N';
|
||||
FFI.setByName('option', jsonEncode(res));
|
||||
parent.target?.setByName('option', jsonEncode(res));
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
toggleInput() {
|
||||
if (_inputOk) {
|
||||
FFI.invokeMethod("stop_input");
|
||||
parent.target?.invokeMethod("stop_input");
|
||||
} else {
|
||||
showInputWarnAlert();
|
||||
if (parent.target != null) {
|
||||
showInputWarnAlert(parent.target!);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -203,9 +213,10 @@ class ServerModel with ChangeNotifier {
|
||||
Future<Null> startService() async {
|
||||
_isStart = true;
|
||||
notifyListeners();
|
||||
FFI.ffiModel.updateEventListener("");
|
||||
await FFI.invokeMethod("init_service");
|
||||
FFI.setByName("start_service");
|
||||
// TODO
|
||||
parent.target?.ffiModel.updateEventListener("");
|
||||
await parent.target?.invokeMethod("init_service");
|
||||
parent.target?.setByName("start_service");
|
||||
getIDPasswd();
|
||||
updateClientState();
|
||||
if (!Platform.isLinux) {
|
||||
@@ -217,9 +228,10 @@ class ServerModel with ChangeNotifier {
|
||||
/// Stop the screen sharing service.
|
||||
Future<Null> stopService() async {
|
||||
_isStart = false;
|
||||
FFI.serverModel.closeAll();
|
||||
await FFI.invokeMethod("stop_service");
|
||||
FFI.setByName("stop_service");
|
||||
// TODO
|
||||
parent.target?.serverModel.closeAll();
|
||||
await parent.target?.invokeMethod("stop_service");
|
||||
parent.target?.setByName("stop_service");
|
||||
notifyListeners();
|
||||
if (!Platform.isLinux) {
|
||||
// current linux is not supported
|
||||
@@ -228,12 +240,12 @@ class ServerModel with ChangeNotifier {
|
||||
}
|
||||
|
||||
Future<Null> initInput() async {
|
||||
await FFI.invokeMethod("init_input");
|
||||
await parent.target?.invokeMethod("init_input");
|
||||
}
|
||||
|
||||
Future<bool> updatePassword(String pw) async {
|
||||
final oldPasswd = _serverPasswd.text;
|
||||
FFI.setByName("update_password", pw);
|
||||
parent.target?.setByName("update_password", pw);
|
||||
await Future.delayed(Duration(milliseconds: 500));
|
||||
await getIDPasswd(force: true);
|
||||
|
||||
@@ -261,8 +273,8 @@ class ServerModel with ChangeNotifier {
|
||||
const maxCount = 10;
|
||||
while (count < maxCount) {
|
||||
await Future.delayed(Duration(seconds: 1));
|
||||
final id = FFI.getByName("server_id");
|
||||
final passwd = FFI.getByName("server_password");
|
||||
final id = parent.target?.getByName("server_id") ?? "";
|
||||
final passwd = parent.target?.getByName("server_password") ?? "";
|
||||
if (id.isEmpty) {
|
||||
continue;
|
||||
} else {
|
||||
@@ -299,7 +311,7 @@ class ServerModel with ChangeNotifier {
|
||||
Map<String, String> res = Map()
|
||||
..["name"] = "enable-keyboard"
|
||||
..["value"] = value ? '' : 'N';
|
||||
FFI.setByName('option', jsonEncode(res));
|
||||
parent.target?.setByName('option', jsonEncode(res));
|
||||
}
|
||||
_inputOk = value;
|
||||
break;
|
||||
@@ -310,7 +322,7 @@ class ServerModel with ChangeNotifier {
|
||||
}
|
||||
|
||||
updateClientState([String? json]) {
|
||||
var res = json ?? FFI.getByName("clients_state");
|
||||
var res = json ?? parent.target?.getByName("clients_state") ?? "";
|
||||
try {
|
||||
final List clientsJson = jsonDecode(res);
|
||||
for (var clientJson in clientsJson) {
|
||||
@@ -397,16 +409,16 @@ class ServerModel with ChangeNotifier {
|
||||
response["id"] = client.id;
|
||||
response["res"] = res;
|
||||
if (res) {
|
||||
FFI.setByName("login_res", jsonEncode(response));
|
||||
parent.target?.setByName("login_res", jsonEncode(response));
|
||||
if (!client.isFileTransfer) {
|
||||
FFI.invokeMethod("start_capture");
|
||||
parent.target?.invokeMethod("start_capture");
|
||||
}
|
||||
FFI.invokeMethod("cancel_notification", client.id);
|
||||
parent.target?.invokeMethod("cancel_notification", client.id);
|
||||
_clients[client.id]?.authorized = true;
|
||||
notifyListeners();
|
||||
} else {
|
||||
FFI.setByName("login_res", jsonEncode(response));
|
||||
FFI.invokeMethod("cancel_notification", client.id);
|
||||
parent.target?.setByName("login_res", jsonEncode(response));
|
||||
parent.target?.invokeMethod("cancel_notification", client.id);
|
||||
_clients.remove(client.id);
|
||||
}
|
||||
}
|
||||
@@ -427,7 +439,7 @@ class ServerModel with ChangeNotifier {
|
||||
if (_clients.containsKey(id)) {
|
||||
_clients.remove(id);
|
||||
DialogManager.dismissByTag(getLoginDialogTag(id));
|
||||
FFI.invokeMethod("cancel_notification", id);
|
||||
parent.target?.invokeMethod("cancel_notification", id);
|
||||
}
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
@@ -437,7 +449,7 @@ class ServerModel with ChangeNotifier {
|
||||
|
||||
closeAll() {
|
||||
_clients.forEach((id, client) {
|
||||
FFI.setByName("close_conn", id.toString());
|
||||
parent.target?.setByName("close_conn", id.toString());
|
||||
});
|
||||
_clients.clear();
|
||||
}
|
||||
@@ -485,7 +497,7 @@ String getLoginDialogTag(int id) {
|
||||
return loginDialogTag + id.toString();
|
||||
}
|
||||
|
||||
showInputWarnAlert() {
|
||||
showInputWarnAlert(FFI ffi) {
|
||||
DialogManager.show((setState, close) => CustomAlertDialog(
|
||||
title: Text(translate("How to get Android input permission?")),
|
||||
content: Column(
|
||||
@@ -501,7 +513,7 @@ showInputWarnAlert() {
|
||||
ElevatedButton(
|
||||
child: Text(translate("Open System Setting")),
|
||||
onPressed: () {
|
||||
FFI.serverModel.initInput();
|
||||
ffi.serverModel.initInput();
|
||||
close();
|
||||
}),
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user