mirror of
https://github.com/lhns/steam-frame-nix.git
synced 2026-10-06 07:00:27 +02:00
197 lines
10 KiB
JavaScript
197 lines
10 KiB
JavaScript
// Injected into Steam's SharedJSContext (CEF, 127.0.0.1:8080) by helper.mjs.
|
|
// Extends Steam's VR keyboard for gamescope app windows:
|
|
// - bottom row: Esc Ctrl Alt [space] AltGr and four separate arrow keys
|
|
// - Ctrl/Alt chords and Esc are pressed for real (Steam can't: in VR
|
|
// SteamClient.Input.ControllerKeyboardSetKeyState throws "Unknown method")
|
|
// - while Ctrl/Alt is active and the keyboard is open, the real modifier is
|
|
// held down, so e.g. Ctrl+scroll works
|
|
// - characters Steam's own key emulation (ControllerKeyboardSendText) turns
|
|
// into "1" -- anything non-ASCII or needing AltGr / a dead key on the X
|
|
// keymap -- are typed by the helper instead
|
|
// Everything goes through the CDP binding window.__vrkbdKey("<op>:<arg>"),
|
|
// executed by helper.mjs with xdotool on :0.
|
|
// Idempotent: safe to evaluate repeatedly.
|
|
(() => {
|
|
const VERSION = 7;
|
|
const send = (msg) => window.__vrkbdKey && window.__vrkbdKey(msg);
|
|
const wr = window.__vrkbdWr ||
|
|
(webpackChunksteamui.push([[Symbol('vrkbd')], {}, (r) => { window.__vrkbdWr = r; }]), window.__vrkbdWr);
|
|
const Layouts = wr(40222); // keyboard layouts (exports G$: enabled, r_: current)
|
|
const VKM = wr(5363).PE; // VirtualKeyboardManager class
|
|
const Status = wr(58508).qL; // .VRKeyboardStatus
|
|
|
|
// ---- bottom row ------------------------------------------------------------
|
|
// The keyboard window has a fixed height, so no extra row: Esc/Ctrl/Alt go
|
|
// left of the space bar, and Steam's arrow keys become four separate keys
|
|
// (stock layouts pair them as [Left, Up-when-shifted], [Right, Down-when-shifted]).
|
|
// Key type "Half" for all added keys; the stylesheet below sets their width.
|
|
// Every entry is a [normal, shifted, altgr] triple: Steam's stock bottom row
|
|
// lacks explicit variants, so with Shift the close icon jumps and with AltGr
|
|
// keys turn into empty full-width panels. With AltGr the arrows become
|
|
// Pos1/Ende/PgUp/PgDn (Shift+arrows stay arrows, for selecting).
|
|
const HALF = 2;
|
|
const k = (key, label) => ({ key, label, type: HALF });
|
|
// null as the shifted variant = "same key, no shift label". Steam still draws
|
|
// the AltGr variant as a small secondary label; the stylesheet hides that in
|
|
// the bottom row (the arrows keep theirs as a Pos1/Ende/Bild hint).
|
|
const same = (x) => [x, null, x];
|
|
const MODS = [k('VKX_Escape', 'Esc'), k('Control', 'Ctrl'), k('Alt', 'Alt')].map(same);
|
|
const half = (a) => ({ ...a, type: HALF });
|
|
const ARROWS = [ // Steam's ArrowLeft/Up/Down/Right keys
|
|
[Layouts.Md, k('VKX_Home', 'Pos1')],
|
|
[Layouts.GO, k('VKX_Prior', 'Bild↑')],
|
|
[Layouts.xl, k('VKX_Next', 'Bild↓')],
|
|
[Layouts.B6, k('VKX_End', 'Ende')],
|
|
].map(([a, alt]) => [half(a), null, alt]);
|
|
const first = (x) => (Array.isArray(x) ? x.find((y) => y) : x);
|
|
const isArrow = (x) => (Array.isArray(x) ? x : [x]).some((y) => y && typeof y.key === 'string' && y.key.startsWith('Arrow'));
|
|
const bottomRow = (row) => {
|
|
const out = [];
|
|
for (const x of row) {
|
|
const key = first(x);
|
|
if (!key || isArrow(x)) continue;
|
|
if (key.key === ' ') out.push(...MODS);
|
|
out.push(same(key));
|
|
}
|
|
out.splice(out.length - 1, 0, ...ARROWS); // before Close/Done (last entry)
|
|
return out;
|
|
};
|
|
let changed = false;
|
|
const patchLayout = (l) => {
|
|
if (!l || typeof l.rgLayout !== 'function' || l.rgLayout.__vrkbd === VERSION) return;
|
|
changed = true;
|
|
const orig = l.rgLayout.__vrkbdOrig || l.rgLayout;
|
|
const f = (opts) => { const rows = orig(opts); return [...rows.slice(0, -1), bottomRow(rows[rows.length - 1])]; };
|
|
f.__vrkbd = VERSION; f.__vrkbdOrig = orig;
|
|
l.rgLayout = f;
|
|
};
|
|
for (const l of Layouts.G$() || []) patchLayout(l);
|
|
patchLayout(Layouts.r_());
|
|
|
|
// ---- text: characters Steam's key emulation can't produce --------------------
|
|
// All keyboard text for gamescope windows ends up in
|
|
// SteamClient.Input.ControllerKeyboardSendText (via several paths, incl. the
|
|
// VR text override), which only maps plain ASCII on the base/shift levels;
|
|
// everything else comes out as "1". Hook that one call and hand those
|
|
// characters to the helper, in order; the rest passes through unchanged.
|
|
if (VKM.prototype.DispatchKeypress.__vrkbdOrig) { // undo the v4 hook
|
|
VKM.prototype.DispatchKeypress = VKM.prototype.DispatchKeypress.__vrkbdOrig;
|
|
}
|
|
const viaHelper = (c) => c.codePointAt(0) > 127 || '|@{[]}\\~^`'.includes(c);
|
|
const Input = SteamClient.Input;
|
|
if (Input.ControllerKeyboardSendText.__vrkbd !== VERSION) {
|
|
const orig = Input.ControllerKeyboardSendText.__vrkbdOrig || Input.ControllerKeyboardSendText;
|
|
const patched = function (text, ...rest) {
|
|
if (typeof text !== 'string') return orig.call(this, text, ...rest);
|
|
let run = '';
|
|
for (const c of text) {
|
|
if (viaHelper(c)) {
|
|
if (run) { orig.call(this, run, ...rest); run = ''; }
|
|
send('type:' + c);
|
|
} else run += c;
|
|
}
|
|
if (run) return orig.call(this, run, ...rest);
|
|
};
|
|
patched.__vrkbd = VERSION; patched.__vrkbdOrig = orig;
|
|
Input.ControllerKeyboardSendText = patched;
|
|
}
|
|
|
|
// ---- key handling ----------------------------------------------------------
|
|
const kbPopup = [...(g_PopupManager.GetPopups?.() || [])]
|
|
.find((p) => p.window?.document.querySelector('[data-key]'));
|
|
if (!kbPopup) return 'no keyboard popup yet';
|
|
// Widen the added half keys and slim AltGr; the space bar is the only
|
|
// flexible key, so it adjusts. Re-applied on every inject (the popup can be recreated).
|
|
const doc = kbPopup.window.document;
|
|
let style = doc.getElementById('vrkbd-style');
|
|
if (!style) { style = doc.createElement('style'); style.id = 'vrkbd-style'; doc.head.appendChild(style); }
|
|
style.textContent = ['VKX_Escape', 'Control', 'Alt', 'ArrowLeft', 'ArrowUp', 'ArrowDown', 'ArrowRight',
|
|
'VKX_Home', 'VKX_Prior', 'VKX_Next', 'VKX_End']
|
|
.map((key) => `[data-key="${key}"]`).join(',') + '{ width: 43px !important; }'
|
|
+ '[data-key="AltGr"]{ width: 52px !important; }'
|
|
// Secondary (AltGr) labels in the bottom row, except the arrows' hints.
|
|
+ `[data-key-row="${Math.max(...[...doc.querySelectorAll('[data-key-row]')].map((e) => +e.getAttribute('data-key-row')))}"]`
|
|
+ ':not([data-key^="Arrow"]) span ~ span { display: none !important; }'
|
|
// Arrow keys: smaller icon, raised, and a smaller Pos1/Ende/Bild hint at
|
|
// the bottom edge, so the two don't overlap on the half-width keys.
|
|
+ '[data-key^="Arrow"] span ~ span { font-size: 7px !important; top: auto !important; bottom: 3px !important; line-height: 1 !important; }'
|
|
+ '[data-key^="Arrow"] span:first-child svg { height: 18px !important; transform: translateY(-4px); }';
|
|
const el = doc.querySelector('[data-key]');
|
|
let f = el[Object.keys(el).find((x) => x.startsWith('__reactFiber'))];
|
|
while (f && !(f.stateNode && f.stateNode.TypeKeyInternal)) f = f.return;
|
|
const inst = f?.stateNode;
|
|
if (!inst) return 'no keyboard component';
|
|
window.__vrkbdInst = inst;
|
|
let proto = Object.getPrototypeOf(inst);
|
|
while (proto && !Object.prototype.hasOwnProperty.call(proto, 'TypeKeyInternal')) proto = Object.getPrototypeOf(proto);
|
|
|
|
const active = (v) => (v & 7) !== 0; // toggle state bits: 1 one-shot, 2 locked
|
|
const release = (v) => { const z = v & 3; return (z === 1 ? 0 : z) | (v & 4); };
|
|
const SYMS = {
|
|
' ': 'space', Backspace: 'BackSpace', Enter: 'Return', Tab: 'Tab',
|
|
'.': 'period', ',': 'comma', '-': 'minus', '+': 'plus', '#': 'numbersign', '<': 'less',
|
|
'/': 'slash', 'ß': 'ssharp', 'ü': 'udiaeresis', 'ö': 'odiaeresis', 'ä': 'adiaeresis',
|
|
ArrowLeft: 'Left', ArrowRight: 'Right', ArrowUp: 'Up', ArrowDown: 'Down', // only with Ctrl/Alt; plain arrows stay Steam's
|
|
};
|
|
const keysym = (key) => {
|
|
if (key.startsWith('VKX_')) return key.slice(4);
|
|
if (SYMS[key]) return SYMS[key];
|
|
if (/^[a-zA-Z0-9]$/.test(key)) return key.toLowerCase();
|
|
return null;
|
|
};
|
|
|
|
if (proto.TypeKeyInternal.__vrkbd !== VERSION) {
|
|
const orig = proto.TypeKeyInternal.__vrkbdOrig || proto.TypeKeyInternal;
|
|
const patched = function (st) {
|
|
const key = st?.strKey;
|
|
const ts = this.state?.toggleStates || {};
|
|
const ctrl = active(ts.Control), alt = active(ts.Alt), shift = active(ts.Shift);
|
|
// Extra keys always; Shift+arrow too (Steam's own arrow path drops Shift).
|
|
const special = key?.startsWith('VKX_') || (shift && key?.startsWith('Arrow'));
|
|
const isToggle = key && ['Shift', 'CapsLock', 'Control', 'Alt', 'AltGr'].includes(key);
|
|
if (key && !isToggle && (special || ctrl || alt)) {
|
|
const sym = keysym(key);
|
|
if (sym) {
|
|
send('key:' + [ctrl && 'ctrl', alt && 'alt', shift && 'shift', sym].filter(Boolean).join('+'));
|
|
this.setState((s) => ({ ...s, toggleStates: { ...s.toggleStates,
|
|
Shift: release(s.toggleStates.Shift), Control: release(s.toggleStates.Control),
|
|
Alt: release(s.toggleStates.Alt), AltGr: release(s.toggleStates.AltGr) } }));
|
|
return;
|
|
}
|
|
}
|
|
return orig.call(this, st);
|
|
};
|
|
patched.__vrkbd = VERSION; patched.__vrkbdOrig = orig;
|
|
proto.TypeKeyInternal = patched;
|
|
changed = true;
|
|
}
|
|
|
|
// ---- held modifiers ---------------------------------------------------------
|
|
// Keep the real Ctrl/Alt pressed while the toggle is active and the keyboard is
|
|
// open (for Ctrl+scroll etc.). One timer per Steam UI instance.
|
|
if (!window.__vrkbdHoldTimer) {
|
|
window.__vrkbdHeld = { ctrl: false, alt: false };
|
|
window.__vrkbdHoldTimer = setInterval(() => {
|
|
const i = window.__vrkbdInst;
|
|
const ts = i?.state?.toggleStates || {};
|
|
const open = !!Status.VRKeyboardStatus?.bIsOpen;
|
|
for (const [mod, state] of [['ctrl', ts.Control], ['alt', ts.Alt]]) {
|
|
const want = open && active(state);
|
|
if (want !== window.__vrkbdHeld[mod]) {
|
|
window.__vrkbdHeld[mod] = want;
|
|
send((want ? 'down:' : 'up:') + mod);
|
|
}
|
|
}
|
|
}, 250);
|
|
}
|
|
|
|
// Re-render with the new row (only when something changed or it's a new instance).
|
|
if (changed || inst.__vrkbd !== VERSION) {
|
|
inst.__vrkbd = VERSION;
|
|
inst.setState({ standardLayout: Layouts.r_() });
|
|
inst.forceUpdate();
|
|
}
|
|
window.__vrkbdPatched = VERSION;
|
|
return 'patched';
|
|
})()
|