rustdesk/src/common.ts

66 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-01-30 03:39:54 +08:00
import * as zstd from "zstddec";
import { KeyEvent, controlKeyFromJSON, ControlKey } from "./message";
import { KEY_MAP, LANGS } from "./gen_js_from_hbb";
let decompressor: zstd.ZSTDDecoder;
export async function initZstd() {
const tmp = new zstd.ZSTDDecoder();
await tmp.init();
console.log("zstd ready");
decompressor = tmp;
}
export async function decompress(compressedArray: Uint8Array) {
const MAX = 1024 * 1024 * 64;
const MIN = 1024 * 1024;
let n = 30 * compressedArray.length;
if (n > MAX) {
n = MAX;
}
if (n < MIN) {
n = MIN;
}
try {
if (!decompressor) {
await initZstd();
}
return decompressor.decode(compressedArray, n);
} catch (e) {
console.error("decompress failed: " + e);
return undefined;
}
}
export function translate(locale: string, text: string): string {
2022-01-31 02:01:17 +08:00
const lang = locale.substring(locale.length - 2).toLowerCase();
2022-01-30 03:39:54 +08:00
let en = LANGS.en as any;
let dict = (LANGS as any)[lang];
if (!dict) dict = en;
let res = dict[text];
if (!res && lang != "en") res = en[text];
return res || text;
}
const zCode = "z".charCodeAt(0);
const aCode = "a".charCodeAt(0);
2022-02-05 01:55:23 +08:00
export function mapKey(name: string, isDesktop: Boolean) {
2022-02-05 04:28:40 +08:00
const tmp = KEY_MAP[name] || name;
2022-01-30 03:39:54 +08:00
if (tmp.length == 1) {
const chr = tmp.charCodeAt(0);
2022-02-05 01:55:23 +08:00
if (!isDesktop && (chr > zCode || chr < aCode))
2022-01-30 03:39:54 +08:00
return KeyEvent.fromPartial({ unicode: chr });
else return KeyEvent.fromPartial({ chr });
}
const control_key = controlKeyFromJSON(name);
if (control_key == ControlKey.UNRECOGNIZED) {
console.error("Unknown control key " + name);
}
return KeyEvent.fromPartial({ control_key });
}
export async function sleep(ms: number) {
await new Promise((r) => setTimeout(r, ms));
}