rustdesk/flutter_hbb/lib/common.dart

117 lines
2.9 KiB
Dart
Raw Normal View History

2020-11-15 15:04:05 +03:00
import 'package:flutter/material.dart';
import 'package:ffi/ffi.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:io';
import 'dart:ffi';
import 'dart:async';
2020-11-16 06:36:53 +03:00
import 'dart:convert';
2020-11-16 16:21:27 +03:00
import 'package:flutter_easyloading/flutter_easyloading.dart';
2020-11-15 15:04:05 +03:00
class HexColor extends Color {
HexColor(final String hexColor) : super(_getColorFromHex(hexColor));
static int _getColorFromHex(String hexColor) {
hexColor = hexColor.toUpperCase().replaceAll('#', '');
if (hexColor.length == 6) {
hexColor = 'FF' + hexColor;
}
return int.parse(hexColor, radix: 16);
}
}
2020-11-15 20:13:26 +03:00
class MyTheme {
static const Color grayBg = Color(0xFFEEEEEE);
static const Color white = Color(0xFFFFFFFF);
}
typedef F1 = void Function(Pointer<Utf8>);
2020-11-16 15:20:02 +03:00
typedef F2 = Pointer<Utf8> Function(Pointer<Utf8>);
typedef F3 = void Function(Pointer<Utf8>, Pointer<Utf8>);
2020-11-15 20:13:26 +03:00
2020-11-15 15:04:05 +03:00
// https://juejin.im/post/6844903864852807694
class FfiModel with ChangeNotifier {
2020-11-15 21:12:37 +03:00
F1 _freeCString;
2020-11-16 15:20:02 +03:00
F2 _getByName;
F3 _setByName;
2020-11-15 15:04:05 +03:00
FfiModel() {
initialzeFFI();
}
void addRemote() {
notifyListeners();
}
void connect(String id) {
2020-11-16 15:20:02 +03:00
setByName("connect", id);
_setByName(Utf8.toUtf8("connect"), Utf8.toUtf8(id));
}
void setByName(String name, String value) {
_setByName(Utf8.toUtf8(name), Utf8.toUtf8(value));
}
String getByName(String name) {
var p = _getByName(Utf8.toUtf8(name));
var res = Utf8.fromUtf8(p);
// https://github.com/brickpop/flutter-rust-ffi
_freeCString(p);
return res;
2020-11-15 15:04:05 +03:00
}
2020-11-15 21:12:37 +03:00
String getId() {
2020-11-16 15:20:02 +03:00
return getByName("remote_id");
2020-11-15 21:12:37 +03:00
}
2020-11-16 15:20:02 +03:00
List<Peer> peers() {
2020-11-16 06:36:53 +03:00
try {
2020-11-16 15:20:02 +03:00
List<dynamic> peers = json.decode(getByName("peers"));
return peers
2020-11-16 06:36:53 +03:00
.map((s) => s as List<dynamic>)
.map((s) =>
2020-11-16 15:20:02 +03:00
Peer.fromJson(s[0] as String, s[1] as Map<String, dynamic>))
2020-11-16 06:36:53 +03:00
.toList();
} catch (e) {
print(e);
}
2020-11-16 15:20:02 +03:00
return [];
2020-11-15 21:12:37 +03:00
}
2020-11-15 15:04:05 +03:00
Future<Null> initialzeFFI() async {
final dylib = Platform.isAndroid
? DynamicLibrary.open('librustdesk.so')
: DynamicLibrary.process();
2020-11-16 15:20:02 +03:00
_getByName = dylib.lookupFunction<F2, F2>('get_by_name');
_setByName =
dylib.lookupFunction<Void Function(Pointer<Utf8>, Pointer<Utf8>), F3>(
'set_by_name');
2020-11-15 21:12:37 +03:00
_freeCString = dylib
.lookupFunction<Void Function(Pointer<Utf8>), F1>('rust_cstr_free');
2020-11-15 15:04:05 +03:00
final dir = (await getApplicationDocumentsDirectory()).path;
2020-11-16 15:20:02 +03:00
setByName("init", dir);
2020-11-15 15:04:05 +03:00
notifyListeners();
}
}
2020-11-16 06:36:53 +03:00
class Peer {
2020-11-16 15:20:02 +03:00
final String id;
final String username;
final String hostname;
final String platform;
2020-11-16 06:36:53 +03:00
2020-11-16 15:20:02 +03:00
Peer.fromJson(String id, Map<String, dynamic> json)
: id = id,
username = json['username'],
hostname = json['hostname'],
platform = json['platform'];
2020-11-16 06:36:53 +03:00
}
2020-11-16 16:21:27 +03:00
// https://github.com/huangjianke/flutter_easyloading
void showLoading(String text) {
EasyLoading.show(status: text);
}
void dismissLoading() {
EasyLoading.dismiss();
}