const WEARABLE_VENDOR = '{{VENDOR}}';
const WEARABLE_SHAPE = '{{SHAPE}}';
const DEFAULT_PHYS_W = {{WIDTH}};
const DEFAULT_PHYS_H = {{HEIGHT}};
const DEFAULT_DPR = {{DPR}};
const DEFAULT_WASM_PATH = '{{WASM_PATH}}';
function computeSafeArea(shape, lw, lh) {
if (shape === 'round') {
const d = Math.min(lw, lh);
const margin = Math.max(0, d * (1 - Math.SQRT1_2) * 0.5);
return { top: margin, right: margin, bottom: margin, left: margin };
}
if (shape === 'squircle') {
const r = Math.min(lw, lh) * 0.11;
return { top: r, right: r * 0.6, bottom: r, left: r * 0.6 };
}
return { top: 16, right: 6, bottom: 10, left: 6 };
}
function createWearableBridge(services, wasmGlue) {
const state = {
canvas: null,
ctx: null,
imgData: null,
imgW: 0,
imgH: 0,
wasmApi: null,
wasmMemory: null,
running: false,
ambient: false,
timerId: 0,
logicalW: Math.round(DEFAULT_PHYS_W / DEFAULT_DPR),
logicalH: Math.round(DEFAULT_PHYS_H / DEFAULT_DPR),
dpr: DEFAULT_DPR,
shape: WEARABLE_SHAPE
};
function installOffscreenFontFactory() {
const g = typeof globalThis !== 'undefined' ? globalThis : null;
if (!g) return;
g.__EUV_HOST_MODE = 'wearable';
if (typeof g.__euvCreateOffscreenCanvas !== 'function') {
g.__euvCreateOffscreenCanvas = function (width, height) {
const w = Math.max(1, Math.round(width || 128));
const h = Math.max(1, Math.round(height || 128));
if (typeof OffscreenCanvas !== 'undefined') {
try {
return new OffscreenCanvas(w, h);
} catch (_e) {}
}
if (typeof document !== 'undefined' && typeof document.createElement === 'function') {
const c = document.createElement('canvas');
c.width = w;
c.height = h;
return c;
}
return null;
};
}
}
function executeHostCommand(cmd) {
if (!cmd) return;
const s = services || {};
switch (cmd.kind) {
case 'vibrateShort':
if (s.vibrator && typeof s.vibrator.vibrate === 'function') s.vibrator.vibrate({ mode: 'short' });
break;
case 'setStorageSync':
if (s.storage && typeof s.storage.set === 'function') s.storage.set({ key: cmd.key, value: cmd.value });
break;
case 'navigateTo':
if (s.router && typeof s.router.push === 'function') s.router.push({ uri: cmd.url || '' });
break;
case 'navigateBack':
if (s.router && typeof s.router.back === 'function') s.router.back();
break;
default:
break;
}
}
function drainCommands() {
if (!state.wasmApi || typeof state.wasmApi.euv_host_drain_commands_json !== 'function') return [];
const raw = state.wasmApi.euv_host_drain_commands_json();
if (!raw || raw === '[]') return [];
try {
const list = JSON.parse(raw);
if (Array.isArray(list)) {
for (let i = 0; i < list.length; i += 1) executeHostCommand(list[i]);
return list;
}
} catch (_e) {}
return [];
}
function ensureImageData(pw, ph) {
if (state.imgData && state.imgW === pw && state.imgH === ph) return state.imgData;
if (state.ctx && typeof state.ctx.createImageData === 'function') {
state.imgData = state.ctx.createImageData(pw, ph);
} else if (typeof ImageData !== 'undefined') {
state.imgData = new ImageData(pw, ph);
} else {
state.imgData = { width: pw, height: ph, data: new Uint8ClampedArray(pw * ph * 4) };
}
state.imgW = pw;
state.imgH = ph;
return state.imgData;
}
function blitFrame(forceFull) {
if (!state.wasmApi || !state.wasmMemory || !state.ctx || !state.canvas) return false;
const ptr = state.wasmApi.euv_host_framebuffer_ptr();
const len = state.wasmApi.euv_host_framebuffer_len();
const pw = state.wasmApi.euv_host_framebuffer_width();
const ph = state.wasmApi.euv_host_framebuffer_height();
if (!ptr || !len || !pw || !ph) return false;
if (state.canvas.width !== pw) state.canvas.width = pw;
if (state.canvas.height !== ph) state.canvas.height = ph;
const dx = forceFull ? 0 : state.wasmApi.euv_host_dirty_x();
const dy = forceFull ? 0 : state.wasmApi.euv_host_dirty_y();
const dw = forceFull ? pw : state.wasmApi.euv_host_dirty_w();
const dh = forceFull ? ph : state.wasmApi.euv_host_dirty_h();
if (!dw || !dh) return false;
const rgba = new Uint8ClampedArray(state.wasmMemory.buffer, ptr, len);
const imgData = ensureImageData(pw, ph);
if (dx === 0 && dy === 0 && dw === pw && dh === ph) {
imgData.data.set(rgba);
state.ctx.putImageData(imgData, 0, 0);
} else {
const dst = imgData.data;
const stride = pw * 4;
const rowBytes = dw * 4;
for (let row = 0; row < dh; row += 1) {
const off = (dy + row) * stride + dx * 4;
dst.set(rgba.subarray(off, off + rowBytes), off);
}
state.ctx.putImageData(imgData, 0, 0, dx, dy, dw, dh);
}
return true;
}
function stepNow(dtSecs, forceRender) {
if (!state.wasmApi) return false;
const rendered = state.wasmApi.euv_host_step(dtSecs, Boolean(forceRender));
drainCommands();
if (rendered) return blitFrame(Boolean(forceRender));
return false;
}
function startLoop() {
if (state.running || !state.canvas) return;
state.running = true;
const intervalMs = state.ambient ? 1000 : 16;
state.timerId = setInterval(() => {
if (!state.running) return;
stepNow(state.ambient ? 1.0 : (1 / 60), state.ambient);
}, intervalMs);
}
function stopLoop() {
state.running = false;
if (state.timerId) {
clearInterval(state.timerId);
state.timerId = 0;
}
}
async function mount(canvas, profile) {
state.canvas = canvas;
state.ctx = canvas && typeof canvas.getContext === 'function' ? canvas.getContext('2d') : null;
if (profile) {
state.dpr = Math.max(1.0, Number(profile.dpr || DEFAULT_DPR));
state.logicalW = Math.max(120, Math.round(Number(profile.width || DEFAULT_PHYS_W) / state.dpr));
state.logicalH = Math.max(120, Math.round(Number(profile.height || DEFAULT_PHYS_H) / state.dpr));
if (profile.shape) state.shape = profile.shape;
}
const safe = computeSafeArea(state.shape, state.logicalW, state.logicalH);
installOffscreenFontFactory();
const glue = wasmGlue || (typeof globalThis !== 'undefined' && globalThis.__euvWasmBindgen) || null;
if (!glue || typeof glue.__euvInitHost !== 'function') return false;
const wasmSource = (typeof globalThis !== 'undefined' && globalThis.__EUV_WASM_SOURCE) || DEFAULT_WASM_PATH;
const initialized = await glue.__euvInitHost(wasmSource);
state.wasmApi = initialized.api;
state.wasmMemory = initialized.memory;
state.wasmApi.euv_host_init(3, WEARABLE_VENDOR, state.logicalW, state.logicalH, state.dpr, safe.top, safe.bottom);
state.wasmApi.euv_host_safe_area(safe.top, safe.right, safe.bottom, safe.left);
state.wasmApi.euv_host_theme(true);
state.wasmApi.euv_host_step(1 / 60, true);
blitFrame(true);
drainCommands();
startLoop();
return true;
}
function dispatchTouch(phase, event) {
if (!state.wasmApi || typeof state.wasmApi.euv_host_touch !== 'function') return;
const touches = (event && (event.changedTouches || event.touches)) || [];
const scaleX = state.logicalW / Math.max(1, state.canvas ? state.canvas.width : DEFAULT_PHYS_W);
const scaleY = state.logicalH / Math.max(1, state.canvas ? state.canvas.height : DEFAULT_PHYS_H);
for (let i = 0; i < touches.length; i += 1) {
const t = touches[i];
const rx = Number(typeof t.clientX === 'number' ? t.clientX : (t.x || 0));
const ry = Number(typeof t.clientY === 'number' ? t.clientY : (t.y || 0));
const lx = rx > state.logicalW ? rx * scaleX : rx;
const ly = ry > state.logicalH ? ry * scaleY : ry;
state.wasmApi.euv_host_touch(phase, Number(t.identifier || 0), lx, ly);
}
stepNow(1 / 60, false);
}
function dispatchCrown(delta, mode) {
if (!state.wasmApi || typeof state.wasmApi.euv_host_crown !== 'function') return;
state.wasmApi.euv_host_crown(Number(delta || 0), Number(mode || 0));
const s = services || {};
if (delta !== 0 && s.vibrator && typeof s.vibrator.vibrate === 'function') {
s.vibrator.vibrate({ mode: 'short' });
}
stepNow(1 / 60, false);
}
function setAmbient(isAmbient) {
state.ambient = Boolean(isAmbient);
if (state.wasmApi && typeof state.wasmApi.euv_host_ambient === 'function') {
state.wasmApi.euv_host_ambient(state.ambient);
stepNow(1.0, true);
}
if (state.running) {
stopLoop();
startLoop();
}
}
function dispatchGesture(tag) {
if (!state.wasmApi || typeof state.wasmApi.euv_host_hardware_gesture !== 'function') return;
state.wasmApi.euv_host_hardware_gesture(Number(tag));
stepNow(1 / 60, true);
}
function dispatchLifecycle(tag) {
if (tag === 3 || tag === 4) stopLoop();
else if (tag === 2 && state.canvas && state.wasmApi) startLoop();
if (state.wasmApi && typeof state.wasmApi.euv_host_lifecycle === 'function') {
state.wasmApi.euv_host_lifecycle(tag, '');
if (tag === 2) stepNow(1 / 60, true);
}
}
function navigate(route) {
if (!state.wasmApi || typeof state.wasmApi.euv_host_navigate !== 'function') return 0;
const hash = state.wasmApi.euv_host_navigate(String(route || '/'));
blitFrame(true);
return hash;
}
function getStats() {
if (!state.wasmApi || typeof state.wasmApi.euv_host_stats_json !== 'function') return null;
try {
return JSON.parse(state.wasmApi.euv_host_stats_json());
} catch (_e) {
return null;
}
}
function destroy() {
stopLoop();
if (state.wasmApi && typeof state.wasmApi.euv_host_destroy === 'function') {
state.wasmApi.euv_host_destroy();
}
state.canvas = null;
state.ctx = null;
}
return {
WEARABLE_VENDOR,
WEARABLE_SHAPE,
mount,
stepNow,
drainCommands,
dispatchTouch,
dispatchCrown,
setAmbient,
dispatchGesture,
dispatchLifecycle,
navigate,
getStats,
destroy
};
}