Skip to content

fix(linux): load Hyprland Lua binds from a file Hyprland reads - #96

Open
kalopsia-space wants to merge 1 commit into
linux-2.0from
fix/linux-2.0-hyprland-binds
Open

kalopsia-space wants to merge 1 commit into
linux-2.0from
fix/linux-2.0-hyprland-binds

Conversation

@kalopsia-space

Copy link
Copy Markdown
Collaborator

Problem

On Hyprland 0.55+ with a hyprland.lua config (CachyOS, Noctalia shell, EndeavourOS reports in #91), no FTHR hotkey does anything, and FTHR shows no warning.

Root cause

  • _apply_compositor_config() wrote the hl.bind(...) block into ~/.config/hypr/custom/keybinds.lua whenever that file or hyprland.lua existed. Only the illogical-impulse (end-4) dotfiles load that file from hyprland.lua. In any other config the binds sit in a file Hyprland never reads.
  • _warn_if_linux_hotkeys_dead() returned early on Hyprland ("binds are auto-written; hotkeys work via the socket"), so the failure was silent.
  • Binds run echo -n action | nc -U <sock>. GNU netcat and BusyBox nc have no -U, so with either of them a bind fires but delivers nothing. This was silent too.

What Hyprland does (sources)

  • Since 0.55 hyprland.lua is the config and hyprlang is deprecated. Binds are written hl.bind(keys, hl.dsp.exec_cmd(cmd), { description = ... }), and other files are loaded with require(), using paths relative to hyprland.lua with . or / as the separator. Requiring a missing module aborts the calling file; pcall(require, ...) is the documented guard. Sources: https://wiki.hypr.land/0.56.0/Configuring/Start/ ("Using Multiple Configuration Files", "Require") and https://wiki.hypr.land/0.56.0/Configuring/Basics/Binds/
  • src/config/lua/ConfigManager.cpp prepends <config dir>/?.lua;<config dir>/?/init.lua to package.path at v0.55.0, v0.56.0 and v0.57.0. A bare module name next to hyprland.lua therefore resolves on every Lua-config release. Absolute-path require (isExplicitRequirePath) first appears in v0.56.0. In v0.55.0, safeLuaRequire only reports errors and does not rethrow them.
  • src/config/lua/bindings/LuaBindingsToplevel.cpp (v0.56.0) stores Lua binds as handler = "__lua" with arg = <registry ref>. hyprctl binds -j (src/debug/HyprCtl.cpp) prints dispatcher, arg and description. For a Lua bind the command is not visible, but the description is.

Change

  1. Write to a file that is loaded. If hyprland.lua requires custom.keybinds or custom/keybinds outside a comment and that file exists, the old behaviour stays. Otherwise FTHR writes ~/.config/hypr/fthr-hotkeys.lua and appends one marked line to hyprland.lua:

    -- FTHR Clips hotkeys (auto-added)
    pcall(require, "fthr-hotkeys")

    This works the same way as the hyprland.conf source = line:

    • It is skipped if hyprland.lua already contains the line or the user's own hotkey.sock binds.
    • An unreadable config is never edited, and a missing one is never created.
    • Nothing is added if the bind file could not be written.

    A complete stale FTHR block left in an unloaded custom/keybinds.lua by earlier builds is removed. An incomplete block is left alone.

  2. Check after reload. After hyprctl reload, and on startup even when nothing changed, FTHR reads hyprctl binds -j on the existing background thread. It retries 3 times, 0.5 s apart. Conf binds count as loaded when arg contains the socket path, Lua binds when the description starts with FTHR Clips:. If neither matches, error_occurred shows HYPRLAND HOTKEYS NOT LOADED once, naming the file written, the main config and the exact line to add. A missing hyprctl, a non-zero exit or unparsable output skips the check.

  3. nc check. linux_tools.nc_supports_unix() looks for -U in the nc -h output, which matches OpenBSD nc and ncat. linux_tools.unix_socket_client() returns nc when it has -U, otherwise socat if installed. socket_command() is still the only place the bind command is built, and it emits socat - UNIX-CONNECT:<sock> when socat is the client. With neither tool usable, the user is told to install openbsd-netcat or socat.

  4. On Hyprland, _warn_if_linux_hotkeys_dead() now reports a socket client that cannot work, instead of returning early.

Testing

  • tests/test_hyprland_binds.py:
    • file routing: plain hyprland.lua, end-4 dot form and slash form, a commented-out require, stale-block cleanup, an incomplete block, idempotency, manual binds, an unreadable config, a failed write, conf/Lua selection
    • the reload-and-check thread with a fake hyprctl and subprocess: Lua match by description, conf match by arg, the warning text and that it is shown once, the startup check with no reload, hyprctl failures, a missing hyprctl, no keys bound, no session
    • nc/socat warning and command
  • tests/test_linux_tools.py: OpenBSD nc, ncat, GNU nc and unprobeable help text; socat fallback; the missing-nc message; socat in the generated command.
  • Every config path is redirected to tmp (the conftest also redirects the new _FTHR_HYPR_LUA), and there is no real hyprctl.
  • Hotkey/compositor/portal/linux_tools test files: 196 passed, 16 skipped (Windows-only), run with an isolated HOME and HYPRLAND_INSTANCE_SIGNATURE/WAYLAND_DISPLAY/DISPLAY unset. ruff check . is clean.
  • Not tested against a live Hyprland session; reporters need to confirm.

Refs #91

With a hyprland.lua config, FTHR wrote its hl.bind() block into
custom/keybinds.lua. Only the illogical-impulse (end-4) dotfiles load
that file; plain migrated configs, CachyOS and Noctalia do not, so the
binds were never registered and nothing said so, because the dead-hotkey
warning returned early on Hyprland (#91).

- Keep custom/keybinds.lua only when hyprland.lua requires it. Otherwise
  write ~/.config/hypr/fthr-hotkeys.lua and append one marked line,
  pcall(require, "fthr-hotkeys"), to hyprland.lua, mirroring the
  hyprland.conf source line: skipped when already present or when the
  user binds the socket by hand, never on an unreadable config, never
  creating one. A stale FTHR block left in an unloaded
  custom/keybinds.lua is removed so a later require cannot double-bind.
- After hyprctl reload, and on startup when nothing changed, read
  `hyprctl binds -j` on the existing background thread. Conf binds are
  found by the socket path in arg; Lua binds by their "FTHR Clips:"
  description, since exec_cmd binds are listed as __lua closures. When
  none is loaded, warn once with the file written and the exact line to
  add. A missing or failing hyprctl skips the check.
- Probe nc -h for -U. GNU netcat and BusyBox nc lack it, so binds fired
  and delivered nothing. socket_command() falls back to
  `socat - UNIX-CONNECT:<sock>` when socat exists; otherwise the user is
  told to install openbsd-netcat or socat. Hyprland now reports this
  instead of returning early.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant