2022-06-20 00:15:37 +08:00
|
|
|
// ignore_for_file: avoid_web_libraries_in_flutter
|
|
|
|
|
2022-05-17 19:59:37 +08:00
|
|
|
import 'dart:convert';
|
2022-01-25 18:13:11 +08:00
|
|
|
import 'dart:typed_data';
|
2022-05-17 19:59:37 +08:00
|
|
|
import 'dart:js';
|
2022-02-03 00:53:59 +08:00
|
|
|
import 'dart:html';
|
|
|
|
import 'dart:async';
|
|
|
|
|
2024-03-22 13:16:37 +08:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
|
|
|
import 'package:flutter_hbb/web/bridge.dart';
|
|
|
|
import 'package:flutter_hbb/common.dart';
|
|
|
|
|
2022-02-03 17:19:25 +08:00
|
|
|
final List<StreamSubscription<MouseEvent>> mouseListeners = [];
|
2022-02-05 04:29:10 +08:00
|
|
|
final List<StreamSubscription<KeyboardEvent>> keyListeners = [];
|
2022-01-25 18:13:11 +08:00
|
|
|
|
2024-03-22 13:16:37 +08:00
|
|
|
typedef HandleEvent = Future<void> Function(Map<String, dynamic> evt);
|
|
|
|
|
2022-01-26 12:48:16 +08:00
|
|
|
class PlatformFFI {
|
2024-03-22 13:16:37 +08:00
|
|
|
final _eventHandlers = <String, Map<String, HandleEvent>>{};
|
2024-03-23 10:08:55 +08:00
|
|
|
final RustdeskImpl _ffiBind = RustdeskImpl();
|
2024-03-22 13:16:37 +08:00
|
|
|
|
2022-01-26 12:48:16 +08:00
|
|
|
static String getByName(String name, [String arg = '']) {
|
2022-05-17 19:59:37 +08:00
|
|
|
return context.callMethod('getByName', [name, arg]);
|
2022-01-25 18:13:11 +08:00
|
|
|
}
|
|
|
|
|
2022-01-26 12:48:16 +08:00
|
|
|
static void setByName(String name, [String value = '']) {
|
2022-05-17 19:59:37 +08:00
|
|
|
context.callMethod('setByName', [name, value]);
|
2022-01-25 18:13:11 +08:00
|
|
|
}
|
|
|
|
|
2022-08-03 22:03:31 +08:00
|
|
|
PlatformFFI._();
|
|
|
|
static final PlatformFFI instance = PlatformFFI._();
|
|
|
|
|
|
|
|
static get localeName => window.navigator.language;
|
2024-03-22 13:16:37 +08:00
|
|
|
RustdeskImpl get ffiBind => _ffiBind;
|
|
|
|
|
|
|
|
static Future<String> getVersion() async {
|
|
|
|
throw UnimplementedError();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool registerEventHandler(
|
|
|
|
String eventName, String handlerName, HandleEvent handler) {
|
|
|
|
debugPrint('registerEventHandler $eventName $handlerName');
|
|
|
|
var handlers = _eventHandlers[eventName];
|
|
|
|
if (handlers == null) {
|
|
|
|
_eventHandlers[eventName] = {handlerName: handler};
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if (handlers.containsKey(handlerName)) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
handlers[handlerName] = handler;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void unregisterEventHandler(String eventName, String handlerName) {
|
|
|
|
debugPrint('unregisterEventHandler $eventName $handlerName');
|
|
|
|
var handlers = _eventHandlers[eventName];
|
|
|
|
if (handlers != null) {
|
|
|
|
handlers.remove(handlerName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> tryHandle(Map<String, dynamic> evt) async {
|
|
|
|
final name = evt['name'];
|
|
|
|
if (name != null) {
|
|
|
|
final handlers = _eventHandlers[name];
|
|
|
|
if (handlers != null) {
|
|
|
|
if (handlers.isNotEmpty) {
|
|
|
|
for (var handler in handlers.values) {
|
|
|
|
await handler(evt);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-03 22:03:31 +08:00
|
|
|
|
2024-03-22 13:16:37 +08:00
|
|
|
String translate(String name, String locale) =>
|
|
|
|
_ffiBind.translate(name: name, locale: locale);
|
|
|
|
|
|
|
|
Uint8List? getRgba(SessionID sessionId, int display, int bufSize) {
|
|
|
|
throw UnimplementedError();
|
|
|
|
}
|
|
|
|
|
|
|
|
int getRgbaSize(SessionID sessionId, int display) =>
|
|
|
|
_ffiBind.sessionGetRgbaSize(sessionId: sessionId, display: display);
|
|
|
|
void nextRgba(SessionID sessionId, int display) =>
|
|
|
|
_ffiBind.sessionNextRgba(sessionId: sessionId, display: display);
|
|
|
|
void registerPixelbufferTexture(SessionID sessionId, int display, int ptr) =>
|
|
|
|
_ffiBind.sessionRegisterPixelbufferTexture(
|
|
|
|
sessionId: sessionId, display: display, ptr: ptr);
|
|
|
|
void registerGpuTexture(SessionID sessionId, int display, int ptr) =>
|
|
|
|
_ffiBind.sessionRegisterGpuTexture(
|
|
|
|
sessionId: sessionId, display: display, ptr: ptr);
|
|
|
|
|
|
|
|
Future<void> init(String appType) async {
|
2022-05-17 19:59:37 +08:00
|
|
|
context.callMethod('init');
|
2022-03-24 20:57:30 +08:00
|
|
|
version = getByName('version');
|
2024-03-28 11:38:11 +08:00
|
|
|
window.onContextMenu.listen((event) {
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
2024-03-23 10:08:55 +08:00
|
|
|
|
|
|
|
context['onRegisteredEvent'] = (String message) {
|
|
|
|
try {
|
|
|
|
Map<String, dynamic> event = json.decode(message);
|
|
|
|
tryHandle(event);
|
|
|
|
} catch (e) {
|
|
|
|
print('json.decode fail(): $e');
|
|
|
|
}
|
|
|
|
};
|
2022-01-25 18:13:11 +08:00
|
|
|
}
|
2022-02-03 00:53:59 +08:00
|
|
|
|
2024-03-22 13:16:37 +08:00
|
|
|
void setEventCallback(void Function(Map<String, dynamic>) fun) {
|
2022-05-17 19:59:37 +08:00
|
|
|
context["onGlobalEvent"] = (String message) {
|
|
|
|
try {
|
|
|
|
Map<String, dynamic> event = json.decode(message);
|
|
|
|
fun(event);
|
|
|
|
} catch (e) {
|
|
|
|
print('json.decode fail(): $e');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-03-25 10:47:53 +08:00
|
|
|
void setRgbaCallback(void Function(int, Uint8List) fun) {
|
|
|
|
context["onRgba"] = (int display, Uint8List? rgba) {
|
2022-05-19 23:45:44 +08:00
|
|
|
if (rgba != null) {
|
2024-03-25 10:47:53 +08:00
|
|
|
fun(display, rgba);
|
2022-05-19 23:45:44 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-03-22 13:16:37 +08:00
|
|
|
void startDesktopWebListener() {
|
2022-02-03 17:19:25 +08:00
|
|
|
mouseListeners.add(
|
|
|
|
window.document.onContextMenu.listen((evt) => evt.preventDefault()));
|
2022-02-03 00:53:59 +08:00
|
|
|
}
|
|
|
|
|
2024-03-22 13:16:37 +08:00
|
|
|
void stopDesktopWebListener() {
|
2022-11-03 21:58:25 +08:00
|
|
|
for (var ml in mouseListeners) {
|
|
|
|
ml.cancel();
|
|
|
|
}
|
2022-02-03 17:19:25 +08:00
|
|
|
mouseListeners.clear();
|
2022-11-03 21:58:25 +08:00
|
|
|
for (var kl in keyListeners) {
|
|
|
|
kl.cancel();
|
|
|
|
}
|
2022-02-05 04:29:10 +08:00
|
|
|
keyListeners.clear();
|
2022-02-03 00:53:59 +08:00
|
|
|
}
|
2022-02-10 02:07:53 +08:00
|
|
|
|
2024-03-22 13:16:37 +08:00
|
|
|
void setMethodCallHandler(FMethod callback) {}
|
2022-02-10 02:07:53 +08:00
|
|
|
|
2024-03-22 13:16:37 +08:00
|
|
|
invokeMethod(String method, [dynamic arguments]) async {
|
2022-02-10 02:07:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
2024-03-22 13:16:37 +08:00
|
|
|
|
|
|
|
// just for compilation
|
2024-03-23 10:08:55 +08:00
|
|
|
void syncAndroidServiceAppDirConfigPath() {}
|
2022-02-03 00:53:59 +08:00
|
|
|
}
|