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'; import 'dart:convert'; 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); } } class MyTheme { static const Color grayBg = Color(0xFFEEEEEE); static const Color white = Color(0xFFFFFFFF); } typedef F1 = void Function(Pointer); typedef F2 = Pointer Function(Pointer); typedef F3 = void Function(Pointer, Pointer); // https://juejin.im/post/6844903864852807694 class FfiModel with ChangeNotifier { F1 _freeCString; F2 _getByName; F3 _setByName; FfiModel() { initialzeFFI(); } void addRemote() { notifyListeners(); } void connect(String id) { 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; } String getId() { return getByName("remote_id"); } List peers() { try { List peers = json.decode(getByName("peers")); return peers .map((s) => s as List) .map((s) => Peer.fromJson(s[0] as String, s[1] as Map)) .toList(); } catch (e) { print(e); } return []; } Future initialzeFFI() async { final dylib = Platform.isAndroid ? DynamicLibrary.open('librustdesk.so') : DynamicLibrary.process(); _getByName = dylib.lookupFunction('get_by_name'); _setByName = dylib.lookupFunction, Pointer), F3>( 'set_by_name'); _freeCString = dylib .lookupFunction), F1>('rust_cstr_free'); final dir = (await getApplicationDocumentsDirectory()).path; setByName("init", dir); notifyListeners(); } } class Peer { final String id; final String username; final String hostname; final String platform; Peer.fromJson(String id, Map json) : id = id, username = json['username'], hostname = json['hostname'], platform = json['platform']; }