- Splash: Summit's icon rises in over an amber glow, the name follows, then it fades into the app (about a second, no keys taken, off when motion is off) - install.sh: after installing icons, rebuild KDE's sycoca and send KIconLoader.iconChanged so a running Plasma shows the updated icon - New app icon (SVG + rendered PNG sizes, render.sh), used by the packages and the engine's window - Lutris scan, consoles (Remote Play via chiaki-ng frame tap, capture cards), Steam account switching, running-game tracking, window mode, artwork - Library UI: add dialog, profile editor, accounts, "now playing" screen - Engine: in-game overlay/HUD, overlay key, global key fixes Co-Authored-By: Claude Opus 5.5 <[email protected]>
199 lines
9.6 KiB
Python
199 lines
9.6 KiB
Python
"""Shortcuts for App window mode (Experimental). SmoovPlay's picture sits over the app as a click-through layer that
|
|
never takes the keyboard: the app keeps it, as some apps stop drawing without it. So the video window's keys come
|
|
through Windows' system-wide shortcuts (RegisterHotKey) instead, Ctrl+Alt with the usual key by default:
|
|
|
|
Insert overlay Ctrl+Alt+F fullscreen Ctrl+Alt+Esc quit
|
|
Ctrl+Alt+S stats Ctrl+Alt+I smooth on/off
|
|
|
|
The overlay's key is the one key that may be a key on its own (Insert, or `run.py --overlay-key`: see
|
|
framegen/overlaykey.py). A shortcut another program already has goes to the next one in its list (Ctrl+Alt+M, then
|
|
Ctrl+Alt+Space, for the overlay), and the video and the launcher say which keys SmoovPlay got. People can pick their
|
|
own on the launcher's Play page, which passes them as `run.py --window-keys "menu=Ctrl+Alt+K;quit=Ctrl+Alt+End"`.
|
|
|
|
While the overlay is open, its own keys (arrows, Enter, Tab, Esc, Backspace) are taken too, without Ctrl+Alt, and
|
|
given back when it closes. Esc on its own is never taken otherwise: it's the app's. Windows treats AltGr as Ctrl+Alt, so while SmoovPlay plays in this mode, AltGr with these keys types
|
|
nothing (AltGr+M is µ on a German keyboard), which is why only five keys are taken.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
from dataclasses import dataclass
|
|
|
|
MODS = {"ctrl": 0x0002, "alt": 0x0001, "shift": 0x0004, "win": 0x0008} # MOD_CONTROL, MOD_ALT, MOD_SHIFT, MOD_WIN
|
|
MOD_ORDER = ("Ctrl", "Alt", "Shift", "Win")
|
|
NOREPEAT = 0x4000 # MOD_NOREPEAT: holding the keys down fires once
|
|
WM_HOTKEY = 0x0312
|
|
|
|
# Key names (as written and shown) -> Windows' virtual-key codes
|
|
KEYS = {"Esc": 0x1B, "Space": 0x20, "Enter": 0x0D, "Backspace": 0x08, "Tab": 0x09, "End": 0x23, "Home": 0x24, "PageUp": 0x21,
|
|
"PageDown": 0x22, "Insert": 0x2D, "Delete": 0x2E, "Pause": 0x13, "Left": 0x25, "Up": 0x26, "Right": 0x27,
|
|
"Down": 0x28}
|
|
KEYS |= {chr(c): c for c in range(ord("A"), ord("Z") + 1)}
|
|
KEYS |= {str(d): 0x30 + d for d in range(10)}
|
|
KEYS |= {f"F{n}": 0x6F + n for n in range(1, 25)}
|
|
_ALIASES = {"escape": "Esc", "return": "Enter", "pgup": "PageUp", "pgdn": "PageDown", "del": "Delete", "ins": "Insert",
|
|
"control": "Ctrl", "windows": "Win"}
|
|
|
|
# action: (what it does, in words; the video window's own key it stands for; its shortcuts, first free one wins)
|
|
ACTIONS = {
|
|
"menu": ("Overlay", "M", ("Insert", "Ctrl+Alt+M", "Ctrl+Alt+Space")),
|
|
"quit": ("Quit", "Q", ("Ctrl+Alt+Esc", "Ctrl+Alt+End")),
|
|
"fullscreen": ("Fullscreen", "F", ("Ctrl+Alt+F",)),
|
|
"stats": ("Stats overlay", "S", ("Ctrl+Alt+S",)),
|
|
"smooth": ("Smooth on/off", "I", ("Ctrl+Alt+I",)),
|
|
}
|
|
NAV = ("Up", "Down", "Left", "Right", "Enter", "Esc", "Backspace", "Tab") # the overlay's keys, plain, while it's open
|
|
BARE = ("menu",) # actions whose shortcut may be a key on its own (framegen/overlaykey.py says which keys)
|
|
|
|
# Windows' own, or keys people need to get out of things: never taken
|
|
RESERVED = {"Ctrl+Alt+Delete", "Ctrl+Shift+Esc", "Alt+Tab", "Alt+Esc", "Alt+F4", "Ctrl+Alt+Tab", "Win+L", "Win+D",
|
|
"Win+Tab", "Ctrl+Esc", "Win"}
|
|
OWN = {"Ctrl+Alt+R": "saving a replay", "Ctrl+Alt+B": "saving a before & after"} # instant replay's (replay.py)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Combo:
|
|
mods: int # MODS flags
|
|
vk: int
|
|
name: str # as shown: "Ctrl+Alt+M"
|
|
|
|
|
|
def parse(text: str, bare: bool = False) -> Combo:
|
|
"""A shortcut from its name ("Ctrl+Alt+M", "ctrl + alt + escape"), or ValueError saying what's wrong with it. It
|
|
needs Ctrl, Alt or Win: a key on its own, or with Shift only, would be taken from the app. bare: the overlay's
|
|
key, which may be one of the keys framegen/overlaykey.py allows on their own (Insert, Home, the F keys...)."""
|
|
parts = [p.strip() for p in text.replace(" ", "").split("+") if p.strip()]
|
|
if not parts:
|
|
raise ValueError("no keys")
|
|
mods, names, key = 0, [], None
|
|
for p in parts:
|
|
word = _ALIASES.get(p.lower(), p)
|
|
if word.lower() in MODS:
|
|
flag = MODS[word.lower()]
|
|
if mods & flag:
|
|
raise ValueError(f"{word} twice")
|
|
mods |= flag
|
|
continue
|
|
if key is not None:
|
|
raise ValueError("more than one key besides Ctrl, Alt, Shift and Win")
|
|
key = next((k for k in KEYS if k.lower() == word.lower()), None)
|
|
if key is None:
|
|
raise ValueError(f"unknown key {p!r}")
|
|
if key is None:
|
|
raise ValueError("Ctrl, Alt, Shift or Win needs a key with it")
|
|
if not mods & (MODS["ctrl"] | MODS["alt"] | MODS["win"]):
|
|
from .overlaykey import ALONE
|
|
if not (bare and key in ALONE):
|
|
raise ValueError("it needs Ctrl, Alt or Win, or the app would lose that key")
|
|
names = [m for m in MOD_ORDER if mods & MODS[m.lower()]]
|
|
name = "+".join(names + [key])
|
|
if name in RESERVED:
|
|
raise ValueError(f"{name} belongs to Windows")
|
|
return Combo(mods, KEYS[key], name)
|
|
|
|
|
|
def parse_arg(text: str | None) -> dict[str, str]:
|
|
"""--window-keys "menu=Ctrl+Alt+K;quit=Ctrl+Alt+End": the shortcuts people picked, checked. ValueError if one
|
|
can't be used, or two actions share one."""
|
|
out: dict[str, str] = {}
|
|
for item in (text or "").split(";"):
|
|
if not item.strip():
|
|
continue
|
|
action, _, combo = item.partition("=")
|
|
action = action.strip().lower()
|
|
if action not in ACTIONS:
|
|
raise ValueError(f"no action {action!r} (there are {', '.join(ACTIONS)})")
|
|
try:
|
|
out[action] = parse(combo, action in BARE).name
|
|
except ValueError as e:
|
|
raise ValueError(f"{action}: {combo.strip()!r}: {e}") from None
|
|
names = list(out.values())
|
|
if len(set(names)) != len(names):
|
|
raise ValueError("two actions have the same shortcut")
|
|
for action, name in out.items():
|
|
if name in OWN:
|
|
raise ValueError(f"{action}: {name} is SmoovPlay's own, for {OWN[name]}")
|
|
return out
|
|
|
|
|
|
def choices(picked: dict[str, str] | None = None) -> dict[str, tuple[str, ...]]:
|
|
"""Each action's shortcuts to try, in order: the one picked for it, or its defaults (minus any picked for another
|
|
action)."""
|
|
picked = picked or {}
|
|
taken = set(picked.values())
|
|
return {a: (picked[a],) if a in picked else tuple(c for c in d if c not in taken)
|
|
for a, (_, _, d) in ACTIONS.items()}
|
|
|
|
|
|
def describe(got: dict[str, str | None], sep: str = "\n") -> str:
|
|
"""The keys, menu first, one a line by default ("Ctrl+Alt+M: menu", "Ctrl+Alt+F: fullscreen"...), in plain
|
|
ASCII: the video window's font has nothing else."""
|
|
order = ("menu", "fullscreen", "quit", "stats", "smooth")
|
|
return sep.join(f"{got[a]}: {ACTIONS[a][0].lower()}" for a in order if got.get(a)) # "Insert: overlay"
|
|
|
|
|
|
class Hotkeys:
|
|
"""The shortcuts, registered for the calling thread (the one that draws the video and polls its messages)."""
|
|
|
|
def __init__(self, picked: dict[str, str] | None = None, user32=None) -> None:
|
|
self.u32 = user32 if user32 is not None else _user32()
|
|
self.got: dict[str, str | None] = {} # action -> the shortcut it got (None: all of its shortcuts were taken)
|
|
self.taken: list[str] = [] # shortcuts another program has
|
|
self._ids: dict[int, str] = {} # registration id -> action, or "nav:<key>"
|
|
self.nav_on = False
|
|
for i, (action, combos) in enumerate(choices(picked).items(), start=1):
|
|
self.got[action] = None
|
|
for name in combos:
|
|
c = parse(name, action in BARE)
|
|
if self.u32.RegisterHotKey(None, i, c.mods | NOREPEAT, c.vk):
|
|
self.got[action], self._ids[i] = name, action
|
|
break
|
|
self.taken.append(name)
|
|
|
|
def set_nav(self, on: bool) -> None:
|
|
"""The menu's keys, taken while it's open (with key repeat, for holding an arrow down)."""
|
|
if on == self.nav_on:
|
|
return
|
|
self.nav_on = on
|
|
for j, key in enumerate(NAV, start=100):
|
|
if on:
|
|
if self.u32.RegisterHotKey(None, j, 0, KEYS[key]):
|
|
self._ids[j] = "nav:" + key
|
|
elif self._ids.pop(j, None) is not None:
|
|
self.u32.UnregisterHotKey(None, j)
|
|
|
|
def poll(self) -> list[str]:
|
|
"""What was pressed since the last look: actions ("menu") and menu keys ("nav:Up"), in order. Run it before
|
|
GLFW looks at the messages: these come to the thread, not a window, and GLFW would drop them."""
|
|
import ctypes
|
|
from ctypes import wintypes as wt
|
|
out, msg = [], wt.MSG()
|
|
while self.u32.PeekMessageW(ctypes.byref(msg), None, WM_HOTKEY, WM_HOTKEY, 1): # PM_REMOVE
|
|
what = self._ids.get(int(msg.wParam))
|
|
if what:
|
|
out.append(what)
|
|
return out
|
|
|
|
def close(self) -> None:
|
|
for i in list(self._ids):
|
|
self.u32.UnregisterHotKey(None, i)
|
|
self._ids.clear()
|
|
self.nav_on = False
|
|
|
|
def report(self) -> str:
|
|
"""The line for the launcher: "[window-keys] {json}" with what each action got and what was taken."""
|
|
import json
|
|
return "[window-keys] " + json.dumps({"keys": self.got, "taken": self.taken})
|
|
|
|
|
|
def _user32():
|
|
if sys.platform != "win32":
|
|
raise OSError("system-wide shortcuts need Windows")
|
|
import ctypes
|
|
from ctypes import wintypes as wt
|
|
u32 = ctypes.WinDLL("user32", use_last_error=True)
|
|
u32.RegisterHotKey.argtypes = [wt.HWND, ctypes.c_int, wt.UINT, wt.UINT]
|
|
u32.UnregisterHotKey.argtypes = [wt.HWND, ctypes.c_int]
|
|
u32.PeekMessageW.argtypes = [ctypes.POINTER(wt.MSG), wt.HWND, wt.UINT, wt.UINT, wt.UINT]
|
|
return u32
|