2022-05-17 19:59:37 +08:00
|
|
|
import 'dart:convert';
|
2022-05-25 00:28:59 +08:00
|
|
|
import 'dart:ffi';
|
2022-01-26 12:48:16 +08:00
|
|
|
import 'dart:io';
|
|
|
|
import 'dart:typed_data';
|
2022-05-25 00:28:59 +08:00
|
|
|
|
2022-05-30 13:25:06 +08:00
|
|
|
import 'package:device_info_plus/device_info_plus.dart';
|
2022-02-10 02:07:53 +08:00
|
|
|
import 'package:external_path/external_path.dart';
|
2022-05-25 00:28:59 +08:00
|
|
|
import 'package:ffi/ffi.dart';
|
2022-02-10 02:07:53 +08:00
|
|
|
import 'package:flutter/services.dart';
|
2022-05-25 00:28:59 +08:00
|
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
|
2022-02-28 18:29:25 +08:00
|
|
|
import '../common.dart';
|
2022-05-25 00:28:59 +08:00
|
|
|
import '../generated_bridge.dart';
|
2022-01-26 12:48:16 +08:00
|
|
|
|
|
|
|
class RgbaFrame extends Struct {
|
|
|
|
@Uint32()
|
2022-02-17 15:22:14 +08:00
|
|
|
external int len;
|
|
|
|
external Pointer<Uint8> data;
|
2022-01-26 12:48:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef F2 = Pointer<Utf8> Function(Pointer<Utf8>, Pointer<Utf8>);
|
|
|
|
typedef F3 = void Function(Pointer<Utf8>, Pointer<Utf8>);
|
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// FFI wrapper around the native Rust core.
|
|
|
|
/// Hides the platform differences.
|
2022-01-26 12:48:16 +08:00
|
|
|
class PlatformFFI {
|
2022-06-13 21:07:26 +08:00
|
|
|
Pointer<RgbaFrame>? _lastRgbaFrame;
|
|
|
|
String _dir = '';
|
|
|
|
String _homeDir = '';
|
|
|
|
F2? _getByName;
|
|
|
|
F3? _setByName;
|
|
|
|
late RustdeskImpl _ffiBind;
|
|
|
|
void Function(Map<String, dynamic>)? _eventCallback;
|
2022-01-26 12:48:16 +08:00
|
|
|
|
2022-06-13 21:07:26 +08:00
|
|
|
RustdeskImpl get ffiBind => _ffiBind;
|
2022-05-31 14:44:06 +08:00
|
|
|
|
2022-01-26 19:00:23 +08:00
|
|
|
static Future<String> getVersion() async {
|
|
|
|
PackageInfo packageInfo = await PackageInfo.fromPlatform();
|
|
|
|
return packageInfo.version;
|
|
|
|
}
|
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// Send **get** command to the Rust core based on [name] and [arg].
|
|
|
|
/// Return the result as a string.
|
2022-06-13 21:07:26 +08:00
|
|
|
String getByName(String name, [String arg = '']) {
|
2022-01-26 12:48:16 +08:00
|
|
|
if (_getByName == null) return '';
|
|
|
|
var a = name.toNativeUtf8();
|
|
|
|
var b = arg.toNativeUtf8();
|
2022-02-17 15:22:14 +08:00
|
|
|
var p = _getByName!(a, b);
|
2022-04-18 17:01:45 +08:00
|
|
|
assert(p != nullptr);
|
2022-01-26 12:48:16 +08:00
|
|
|
var res = p.toDartString();
|
|
|
|
calloc.free(p);
|
|
|
|
calloc.free(a);
|
|
|
|
calloc.free(b);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// Send **set** command to the Rust core based on [name] and [value].
|
2022-06-13 21:07:26 +08:00
|
|
|
void setByName(String name, [String value = '']) {
|
2022-01-26 12:48:16 +08:00
|
|
|
if (_setByName == null) return;
|
|
|
|
var a = name.toNativeUtf8();
|
|
|
|
var b = value.toNativeUtf8();
|
2022-02-17 15:22:14 +08:00
|
|
|
_setByName!(a, b);
|
2022-01-26 12:48:16 +08:00
|
|
|
calloc.free(a);
|
|
|
|
calloc.free(b);
|
|
|
|
}
|
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// Init the FFI class, loads the native Rust core library.
|
2022-06-13 21:07:26 +08:00
|
|
|
Future<Null> init() async {
|
2022-01-26 12:48:16 +08:00
|
|
|
isIOS = Platform.isIOS;
|
|
|
|
isAndroid = Platform.isAndroid;
|
2022-05-23 16:02:37 +08:00
|
|
|
isDesktop = Platform.isWindows || Platform.isMacOS || Platform.isLinux;
|
2022-05-25 00:28:59 +08:00
|
|
|
// if (isDesktop) {
|
|
|
|
// // TODO
|
|
|
|
// return;
|
|
|
|
// }
|
2022-01-26 12:48:16 +08:00
|
|
|
final dylib = Platform.isAndroid
|
|
|
|
? DynamicLibrary.open('librustdesk.so')
|
2022-05-25 00:28:59 +08:00
|
|
|
: Platform.isLinux
|
|
|
|
? DynamicLibrary.open("/usr/lib/rustdesk/librustdesk.so")
|
|
|
|
: Platform.isWindows
|
|
|
|
? DynamicLibrary.open("librustdesk.dll")
|
|
|
|
: Platform.isMacOS
|
|
|
|
? DynamicLibrary.open("librustdesk.dylib")
|
|
|
|
: DynamicLibrary.process();
|
2022-01-26 12:48:16 +08:00
|
|
|
print('initializing FFI');
|
|
|
|
try {
|
|
|
|
_getByName = dylib.lookupFunction<F2, F2>('get_by_name');
|
|
|
|
_setByName =
|
|
|
|
dylib.lookupFunction<Void Function(Pointer<Utf8>, Pointer<Utf8>), F3>(
|
|
|
|
'set_by_name');
|
|
|
|
_dir = (await getApplicationDocumentsDirectory()).path;
|
2022-05-31 22:09:36 +08:00
|
|
|
_ffiBind = RustdeskImpl(dylib);
|
|
|
|
_startListenEvent(_ffiBind); // global event
|
2022-04-18 17:01:45 +08:00
|
|
|
try {
|
2022-06-21 17:58:27 +08:00
|
|
|
if (isAndroid) {
|
|
|
|
// only support for android
|
|
|
|
_homeDir = (await ExternalPath.getExternalStorageDirectories())[0];
|
|
|
|
} else {
|
|
|
|
_homeDir = (await getDownloadsDirectory())?.path ?? "";
|
|
|
|
}
|
2022-04-18 17:01:45 +08:00
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-01-26 12:48:16 +08:00
|
|
|
String id = 'NA';
|
|
|
|
String name = 'Flutter';
|
|
|
|
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
|
|
|
|
if (Platform.isAndroid) {
|
|
|
|
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
|
|
|
|
name = '${androidInfo.brand}-${androidInfo.model}';
|
|
|
|
id = androidInfo.id.hashCode.toString();
|
2022-05-30 13:25:06 +08:00
|
|
|
androidVersion = androidInfo.version.sdkInt ?? 0;
|
2022-05-29 04:39:12 +08:00
|
|
|
} else if (Platform.isIOS) {
|
2022-01-26 12:48:16 +08:00
|
|
|
IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
|
2022-05-30 13:25:06 +08:00
|
|
|
name = iosInfo.utsname.machine ?? "";
|
2022-01-26 12:48:16 +08:00
|
|
|
id = iosInfo.identifierForVendor.hashCode.toString();
|
2022-05-30 13:25:06 +08:00
|
|
|
} else if (Platform.isLinux) {
|
|
|
|
LinuxDeviceInfo linuxInfo = await deviceInfo.linuxInfo;
|
|
|
|
name = linuxInfo.name;
|
|
|
|
id = linuxInfo.machineId ?? linuxInfo.id;
|
|
|
|
} else if (Platform.isWindows) {
|
|
|
|
WindowsDeviceInfo winInfo = await deviceInfo.windowsInfo;
|
|
|
|
name = winInfo.computerName;
|
|
|
|
id = winInfo.computerName;
|
|
|
|
} else if (Platform.isMacOS) {
|
|
|
|
MacOsDeviceInfo macOsInfo = await deviceInfo.macOsInfo;
|
|
|
|
name = macOsInfo.computerName;
|
|
|
|
id = macOsInfo.systemGUID ?? "";
|
2022-01-26 12:48:16 +08:00
|
|
|
}
|
2022-02-10 02:07:53 +08:00
|
|
|
print("info1-id:$id,info2-name:$name,dir:$_dir,homeDir:$_homeDir");
|
2022-01-26 12:48:16 +08:00
|
|
|
setByName('info1', id);
|
|
|
|
setByName('info2', name);
|
2022-02-10 02:07:53 +08:00
|
|
|
setByName('home_dir', _homeDir);
|
2022-01-26 12:48:16 +08:00
|
|
|
setByName('init', _dir);
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-02-28 21:26:44 +08:00
|
|
|
version = await getVersion();
|
2022-01-26 12:48:16 +08:00
|
|
|
}
|
2022-02-03 00:53:59 +08:00
|
|
|
|
2022-05-28 03:56:42 +08:00
|
|
|
/// Start listening to the Rust core's events and frames.
|
2022-06-13 21:07:26 +08:00
|
|
|
void _startListenEvent(RustdeskImpl rustdeskImpl) {
|
2022-05-19 23:45:44 +08:00
|
|
|
() async {
|
2022-05-31 22:09:36 +08:00
|
|
|
await for (final message in rustdeskImpl.startGlobalEventStream()) {
|
2022-05-19 23:45:44 +08:00
|
|
|
if (_eventCallback != null) {
|
|
|
|
try {
|
|
|
|
Map<String, dynamic> event = json.decode(message);
|
|
|
|
_eventCallback!(event);
|
|
|
|
} catch (e) {
|
|
|
|
print('json.decode fail(): $e');
|
|
|
|
}
|
2022-05-17 19:59:37 +08:00
|
|
|
}
|
|
|
|
}
|
2022-05-19 23:45:44 +08:00
|
|
|
}();
|
2022-05-17 19:59:37 +08:00
|
|
|
}
|
|
|
|
|
2022-06-13 21:07:26 +08:00
|
|
|
void setEventCallback(void Function(Map<String, dynamic>) fun) async {
|
2022-05-17 19:59:37 +08:00
|
|
|
_eventCallback = fun;
|
|
|
|
}
|
|
|
|
|
2022-06-13 21:07:26 +08:00
|
|
|
void setRgbaCallback(void Function(Uint8List) fun) async {}
|
2022-05-19 23:45:44 +08:00
|
|
|
|
2022-06-13 21:07:26 +08:00
|
|
|
void startDesktopWebListener() {}
|
2022-02-17 15:22:14 +08:00
|
|
|
|
2022-06-13 21:07:26 +08:00
|
|
|
void stopDesktopWebListener() {}
|
2022-02-10 02:07:53 +08:00
|
|
|
|
2022-06-13 21:07:26 +08:00
|
|
|
void setMethodCallHandler(FMethod callback) {
|
2022-02-10 02:07:53 +08:00
|
|
|
toAndroidChannel.setMethodCallHandler((call) async {
|
|
|
|
callback(call.method, call.arguments);
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-13 21:07:26 +08:00
|
|
|
invokeMethod(String method, [dynamic arguments]) async {
|
2022-04-18 11:46:36 +08:00
|
|
|
if (!isAndroid) return Future<bool>(() => false);
|
2022-04-18 17:01:45 +08:00
|
|
|
return await toAndroidChannel.invokeMethod(method, arguments);
|
2022-02-10 02:07:53 +08:00
|
|
|
}
|
2022-01-26 12:48:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
final localeName = Platform.localeName;
|
2022-02-10 02:07:53 +08:00
|
|
|
final toAndroidChannel = MethodChannel("mChannel");
|