rustdesk/flutter_hbb/lib/common.dart

312 lines
7.7 KiB
Dart
Raw Normal View History

2020-11-15 20:04:05 +08: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 11:36:53 +08:00
import 'dart:convert';
2020-11-16 21:21:27 +08:00
import 'package:flutter_easyloading/flutter_easyloading.dart';
2020-11-18 00:28:55 +08:00
import 'dart:typed_data';
2020-11-15 20:04:05 +08:00
2020-11-17 18:10:49 +08:00
class RgbaFrame extends Struct {
@Uint32()
int len;
Pointer<Uint8> data;
}
2020-11-15 20:04:05 +08: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-16 01:13:26 +08:00
class MyTheme {
static const Color grayBg = Color(0xFFEEEEEE);
static const Color white = Color(0xFFFFFFFF);
2020-11-16 22:00:09 +08:00
static const Color accent = Color(0xFF0071FF);
2020-11-16 01:13:26 +08:00
}
typedef F1 = void Function(Pointer<Utf8>);
2020-11-16 22:00:09 +08:00
typedef F2 = Pointer<Utf8> Function(Pointer<Utf8>, Pointer<Utf8>);
2020-11-16 20:20:02 +08:00
typedef F3 = void Function(Pointer<Utf8>, Pointer<Utf8>);
2020-11-17 18:10:49 +08:00
typedef F4 = void Function(Pointer<RgbaFrame>);
typedef F5 = Pointer<RgbaFrame> Function();
2020-11-16 01:13:26 +08:00
2020-11-15 20:04:05 +08:00
// https://juejin.im/post/6844903864852807694
class FfiModel with ChangeNotifier {
FfiModel() {
2020-11-17 11:12:55 +08:00
init();
2020-11-15 20:04:05 +08:00
}
2020-11-17 11:12:55 +08:00
Future<Null> init() async {
await FFI.init();
notifyListeners();
}
}
class FFI {
static F1 _freeCString;
static F2 _getByName;
static F3 _setByName;
2020-11-17 18:10:49 +08:00
static F4 _freeRgba;
static F5 _getRgba;
2020-11-18 00:28:55 +08:00
static Pointer<RgbaFrame> _lastRgbaFrame;
2020-11-17 11:12:55 +08:00
static String getId() {
2020-11-18 00:28:55 +08:00
return getByName('remote_id');
2020-11-17 00:47:27 +08:00
}
2020-11-17 11:12:55 +08:00
static List<Peer> peers() {
2020-11-17 00:47:27 +08:00
try {
2020-11-18 00:28:55 +08:00
List<dynamic> peers = json.decode(getByName('peers'));
2020-11-17 00:47:27 +08:00
return peers
.map((s) => s as List<dynamic>)
.map((s) =>
Peer.fromJson(s[0] as String, s[1] as Map<String, dynamic>))
.toList();
} catch (e) {
print(e);
}
return [];
}
2020-11-17 11:12:55 +08:00
static void connect(String id) {
2020-11-18 00:28:55 +08:00
setByName('connect', id);
}
static void _clearRgbaFrame() {
if (_lastRgbaFrame != null && _lastRgbaFrame != nullptr)
_freeRgba(_lastRgbaFrame);
}
static Uint8List getRgba() {
_clearRgbaFrame();
_lastRgbaFrame = _getRgba();
if (_lastRgbaFrame == null || _lastRgbaFrame == nullptr) return null;
final ref = _lastRgbaFrame.ref;
return Uint8List.sublistView(ref.data.asTypedList(ref.len));
2020-11-17 00:47:27 +08:00
}
2020-11-18 01:55:26 +08:00
static Map<String, dynamic> popEvent() {
2020-11-18 00:28:55 +08:00
var s = getByName('event');
if (s == '') return null;
2020-11-17 00:47:27 +08:00
try {
2020-11-18 01:55:26 +08:00
Map<String, dynamic> event = json.decode(s);
2020-11-17 00:47:27 +08:00
return event;
} catch (e) {
print(e);
}
return null;
}
2020-11-17 11:12:55 +08:00
static void login(String password, bool remember) {
2020-11-17 00:47:27 +08:00
setByName(
2020-11-18 00:28:55 +08:00
'login',
2020-11-17 00:47:27 +08:00
json.encode({
2020-11-18 00:28:55 +08:00
'password': password,
'remember': remember ? 'true' : 'false',
2020-11-17 00:47:27 +08:00
}));
}
2020-11-17 11:12:55 +08:00
static void close() {
2020-11-18 00:28:55 +08:00
_clearRgbaFrame();
setByName('close', '');
2020-11-16 20:20:02 +08:00
}
2020-11-17 11:12:55 +08:00
static void setByName(String name, String value) {
2020-11-16 20:20:02 +08:00
_setByName(Utf8.toUtf8(name), Utf8.toUtf8(value));
}
2020-11-18 00:28:55 +08:00
static String getByName(String name, {String arg = ''}) {
2020-11-16 22:00:09 +08:00
var p = _getByName(Utf8.toUtf8(name), Utf8.toUtf8(arg));
2020-11-18 00:28:55 +08:00
assert(p != nullptr && p != null);
2020-11-16 20:20:02 +08:00
var res = Utf8.fromUtf8(p);
// https://github.com/brickpop/flutter-rust-ffi
_freeCString(p);
return res;
2020-11-15 20:04:05 +08:00
}
2020-11-17 11:12:55 +08:00
static Future<Null> init() async {
2020-11-15 20:04:05 +08:00
final dylib = Platform.isAndroid
? DynamicLibrary.open('librustdesk.so')
: DynamicLibrary.process();
2020-11-16 20:20:02 +08:00
_getByName = dylib.lookupFunction<F2, F2>('get_by_name');
_setByName =
dylib.lookupFunction<Void Function(Pointer<Utf8>, Pointer<Utf8>), F3>(
'set_by_name');
2020-11-16 02:12:37 +08:00
_freeCString = dylib
.lookupFunction<Void Function(Pointer<Utf8>), F1>('rust_cstr_free');
2020-11-17 18:10:49 +08:00
_freeRgba = dylib
.lookupFunction<Void Function(Pointer<RgbaFrame>), F4>('free_rgba');
_getRgba = dylib.lookupFunction<F5, F5>('get_rgba');
2020-11-15 20:04:05 +08:00
final dir = (await getApplicationDocumentsDirectory()).path;
2020-11-18 00:28:55 +08:00
setByName('init', dir);
2020-11-15 20:04:05 +08:00
}
}
2020-11-16 11:36:53 +08:00
class Peer {
2020-11-16 20:20:02 +08:00
final String id;
final String username;
final String hostname;
final String platform;
2020-11-16 11:36:53 +08:00
2020-11-16 20:20:02 +08:00
Peer.fromJson(String id, Map<String, dynamic> json)
: id = id,
username = json['username'],
hostname = json['hostname'],
platform = json['platform'];
2020-11-16 11:36:53 +08:00
}
2020-11-16 21:21:27 +08:00
// https://github.com/huangjianke/flutter_easyloading
void showLoading(String text) {
2020-11-18 12:49:43 +08:00
dismissLoading();
2020-11-16 21:21:27 +08:00
EasyLoading.show(status: text);
}
void dismissLoading() {
EasyLoading.dismiss();
}
2020-11-16 22:00:09 +08:00
2020-11-16 22:12:32 +08:00
void showSuccess(String text) {
2020-11-18 12:49:43 +08:00
dismissLoading();
2020-11-16 22:12:32 +08:00
EasyLoading.showSuccess(text);
}
2020-11-18 12:49:43 +08:00
bool _hasDialog = false;
2020-11-16 22:14:50 +08:00
// https://material.io/develop/flutter/components/dialogs
2020-11-18 12:49:43 +08:00
Future<Null> enterPasswordDialog(String id, BuildContext context) async {
2020-11-18 01:55:26 +08:00
dismissLoading();
2020-11-18 12:49:43 +08:00
if (_hasDialog) {
Navigator.pop(context);
}
_hasDialog = true;
2020-11-18 02:36:47 +08:00
final controller = TextEditingController();
2020-11-18 00:28:55 +08:00
var remember = FFI.getByName('remember', arg: id) == 'true';
2020-11-16 22:14:29 +08:00
var dialog = AlertDialog(
2020-11-16 22:00:09 +08:00
title: Text('Please enter your password'),
2020-11-18 12:49:43 +08:00
contentPadding: const EdgeInsets.all(20.0),
2020-11-16 22:00:09 +08:00
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextField(
2020-11-18 02:36:47 +08:00
autofocus: true,
2020-11-16 22:00:09 +08:00
obscureText: true,
2020-11-18 02:36:47 +08:00
controller: controller,
2020-11-16 22:00:09 +08:00
decoration: const InputDecoration(
labelText: 'Password',
),
),
ListTile(
title: Text(
'Remember the password',
),
leading: Checkbox(
value: remember,
2020-11-18 02:36:47 +08:00
onChanged: (v) {
remember = v;
},
2020-11-16 22:00:09 +08:00
),
),
],
),
actions: [
FlatButton(
textColor: MyTheme.accent,
2020-11-18 02:36:47 +08:00
onPressed: () {
Navigator.pop(context);
Navigator.pop(context);
},
2020-11-16 22:00:09 +08:00
child: Text('Cancel'),
),
FlatButton(
textColor: MyTheme.accent,
2020-11-18 02:36:47 +08:00
onPressed: () {
var text = controller.text.trim();
if (text == '') return;
FFI.login(text, remember);
showLoading('Logging in...');
Navigator.pop(context);
},
2020-11-16 22:00:09 +08:00
child: Text('OK'),
),
],
);
2020-11-18 12:49:43 +08:00
await showDialog<void>(
context: context,
barrierDismissible: false,
builder: (context) => dialog);
_hasDialog = false;
2020-11-16 22:00:09 +08:00
}
2020-11-16 22:12:32 +08:00
2020-11-18 12:49:43 +08:00
Future<Null> wrongPasswordDialog(String id, BuildContext context) async {
2020-11-18 01:55:26 +08:00
dismissLoading();
2020-11-18 12:49:43 +08:00
if (_hasDialog) {
Navigator.pop(context);
}
_hasDialog = true;
2020-11-16 22:14:29 +08:00
var dialog = AlertDialog(
2020-11-18 02:36:47 +08:00
title: Text('Wrong Password'),
2020-11-18 12:49:43 +08:00
contentPadding: const EdgeInsets.all(20.0),
2020-11-16 22:12:32 +08:00
content: Text('Do you want to enter again?'),
actions: [
FlatButton(
textColor: MyTheme.accent,
2020-11-18 12:49:43 +08:00
onPressed: () {
Navigator.pop(context);
Navigator.pop(context);
},
2020-11-16 22:12:32 +08:00
child: Text('Cancel'),
),
FlatButton(
textColor: MyTheme.accent,
onPressed: () {
Navigator.pop(context);
enterPasswordDialog(id, context);
},
child: Text('Retry'),
),
],
);
2020-11-18 12:49:43 +08:00
await showDialog<void>(
context: context,
barrierDismissible: false,
builder: (context) => dialog);
_hasDialog = false;
2020-11-16 22:12:32 +08:00
}
2020-11-18 01:55:26 +08:00
2020-11-18 12:49:43 +08:00
Future<Null> msgbox(
String type, String title, String text, BuildContext context) async {
2020-11-18 01:55:26 +08:00
dismissLoading();
2020-11-18 12:49:43 +08:00
if (_hasDialog) {
Navigator.pop(context);
}
_hasDialog = true;
2020-11-18 01:55:26 +08:00
var dialog = AlertDialog(
title: Text(title),
2020-11-18 12:49:43 +08:00
contentPadding: const EdgeInsets.all(20.0),
2020-11-18 01:55:26 +08:00
content: Text(text),
actions: [
FlatButton(
textColor: MyTheme.accent,
onPressed: () {
Navigator.pop(context);
Navigator.pop(context);
},
child: Text('OK'),
),
],
);
2020-11-18 12:49:43 +08:00
await showDialog<void>(
context: context,
barrierDismissible: false,
builder: (context) => dialog);
_hasDialog = false;
2020-11-18 01:55:26 +08:00
}