Files
hyprcosmic/config/bin/hyprcosmic-keybinds
T
gitops fc8c2a727d waybar: six more modules, and a keyboard reference the session had none of
The bar showed which windows existed but not which one had the keyboard,
and nothing at all about the GPU, the fans, the disk, or a unit that had
failed at boot. Six modules, two of which needed a script because waybar
has no module that fits.

hyprland/window, and no compositor change

Reading the active window needs `j/activewindow` on startup and
`activewindow>>` on .socket2.sock, and hypr_ipc already serves both. A
retitle of the focused window propagates too, which is the case that
usually needs a separate `windowtitle` event: this fork diffs a snapshot
on a 150 ms tick rather than emitting from call sites, so src/hypr_ipc/
sync.rs sees a (class, title) change and cannot miss it. There is already
a test for exactly that -- title_change_alone_emits_activewindow.

The rewrite rules trim the app suffix from titles written for a title bar
rather than a 60-column slot. All five now use the same [-<em dash>]
class. Vivaldi had a bare hyphen because it was written from the live
title -- j/clients reports "Inbox - ... - Gmail - Vivaldi" -- and the
others got the class in a later pass, which left the one rule that would
break if Vivaldi ever switched separators. COSMIC's own applications use
an em dash and not a hyphen, so a rule written with a hyphen matches
nothing there.

custom/fan, because neither of the obvious options can name this sensor

waybar has no fan module. Its temperature module cannot be borrowed: it
divides by 1000 to turn millidegrees into degrees, which renders 2700 rpm
as 2 C. Nor can hwmon-path-abs point at the fans. That option names a
parent directory and takes the one hwmonN inside it, which works for
k10temp and amdgpu because each has exactly one, but asus-nb-wmi has two:

    hwmon9   name=asus                  fan1_input, fan2_input
    hwmon10  name=asus_custom_fan_curve  the curve's set points

Which of the two waybar picked would be down to readdir order. So the
script resolves by content instead -- any hwmon with a fan*_input -- which
also makes it work on hardware that is not this laptop. It prints nothing
where there is no readable fan, and waybar draws an empty custom module as
nothing, so a VM loses the item rather than showing a dead 0 rpm.

hyprcosmic-keybinds, and why it does not read cosmic.conf

The obvious implementation reads cosmic.conf and would be wrong. This
machine's cosmic.conf declares six bindings. The session answers to 122.
The other 116 are COSMIC's defaults, which the fork does not restate
because it has no reason to -- Super+Q closes a window whether or not
anybody wrote it down. A reference showing six entries would not look
broken, it would look complete, while missing every window, workspace and
media key on the machine.

So it reads the three RON files the compositor reads: Shortcuts/v1/
defaults for the built-ins, the user's Shortcuts/v1/custom for what
cosmic-conf projected out of cosmic.conf, and Shortcuts/v1/system_actions
to turn System(Screenshot) into cosmic-screenshot. Without the third, a
third of the list would name internal actions rather than the programs
they launch, which is the half of the question that was being asked.

Parsing RON with sed deserves a defence. The principled home for this is a
`cosmic-conf binds` subcommand, which already has the schema and the bind
grammar -- and is a compiled change and a full package build. The files
are one binding per line and nothing downstream consumes this, so the cost
of being wrong is a mangled row in a help window, not a broken keybinding.
The script refuses to show a list it could not parse rather than showing a
short one.

The binding for it was chosen with the script: Super+K and Super+I are
both focus actions in COSMIC's defaults, and Super+Shift+/ is free.

install-assets: prune dot-directories from the audit

audit_config_tree refuses to run unless every file under config/ is
classified, and it started failing on six gitignored .omc/ state files
that tooling had dropped into config/waybar/. Failing on those trains you
to ignore the one message that catches a genuinely unclassified asset.
Dot files are still walked; only directories are pruned.

Verified before committing

The generated config parses as JSON with all 23 modules defined and none
defined but unplaced. The three stylesheets concatenate and parse clean
through GTK's own CssProvider, which is the parser waybar uses, and every
@colour they name is defined. Both scripts pass a syntax check, and
hyprcosmic-keybinds was run with rofi stubbed -- nothing appeared on the
desktop -- producing 118 rows, which is 116 defaults plus 6 ours less the
4 that override. System() actions resolved to real commands, chords sorted
Super first, and the modifier-only Super binding survived, which a parser
requiring a key field would have dropped.

Every sensor was read before being wired to a module rather than after:
amdgpu edge 55 C, cpu_fan 2700 and gpu_fan 2500 rpm, zero failed units in
both scopes, / at 3 percent.

Not verified: how any of it looks. Rendering needs waybar restarted
against a session that is not at the lock screen.
2026-08-11 07:46:29 +07:00

206 lines
8.8 KiB
Bash
Executable File

#!/usr/bin/bash
#
# Show every keyboard shortcut in the session, and what each one runs.
#
# WHY THIS EXISTS
# ---------------
# COSMIC has no keyboard reference. cosmic-settings lists shortcuts in a
# scrolling pane behind three clicks, which is where you go to *change* one, not
# where you go to remember one mid-task. Hyprland desktops all ship a cheatsheet
# on a key and a bar click for exactly that reason, and this is that.
#
# WHERE THE BINDINGS COME FROM, AND WHY NOT cosmic.conf
# -----------------------------------------------------
# The obvious implementation reads cosmic.conf, and it would be wrong. This
# machine's cosmic.conf declares six bindings. The session answers to 122. The
# other 116 are COSMIC's own defaults, which the fork does not restate because
# it has no reason to -- Super+Q closes a window whether or not anybody wrote it
# down. A cheatsheet showing six entries would not look broken; it would look
# complete, and it would be missing every window, workspace and media key on the
# machine. That is a worse failure than not having the feature.
#
# So the sources are the three RON files the compositor actually reads:
#
# /usr/share/cosmic/.../Shortcuts/v1/defaults 116 COSMIC built-ins
# ~/.config/cosmic/.../Shortcuts/v1/custom 6 ours, projected from
# cosmic.conf by cosmic-conf
# /usr/share/cosmic/.../Shortcuts/v1/system_actions
# what System(X) actually runs
#
# custom wins over defaults on a collision, matching the order the compositor
# merges them, so a rebound key is listed once with the binding in force.
#
# system_actions is what makes this answer the question that was asked. A
# default reads `System(Screenshot)`, which names an action and not a program;
# system_actions maps that to `cosmic-screenshot`. Without it a third of the
# list would name internal actions rather than the applications they launch.
#
# THE PARSER
# ----------
# These are RON files, and this reads them with a regex, which deserves a
# defence. The alternative is a `cosmic-conf binds` subcommand, which would be
# the principled home for this -- cosmic-conf already has the schema and the
# bind grammar. That is a compiled change and a full package build, and this
# feature does not need one: the files are one binding per line, and the shape
# has not moved. The cost of being wrong here is a mangled row in a reference
# window, not a broken keybinding, because nothing downstream consumes this.
#
# If the format does move, the failure is visible immediately and loudly: the
# count check below refuses to show a list it could not parse.
set -uo pipefail
SYS_DIR=/usr/share/cosmic/com.system76.CosmicSettings.Shortcuts/v1
USER_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/cosmic/com.system76.CosmicSettings.Shortcuts/v1"
command -v rofi >/dev/null 2>&1 || {
printf 'hyprcosmic-keybinds: rofi is not installed\n' >&2
exit 1
}
# Errors go to a rofi dialog as well as stderr: the two ways in are a keybinding
# and a bar click, and neither has a terminal to read stderr from. Same reasoning
# as hyprcosmic-powermenu.
fail() {
printf 'hyprcosmic-keybinds: %s\n' "$*" >&2
rofi -e "$*" >/dev/null 2>&1 || :
exit 1
}
[[ -r "$SYS_DIR/defaults" ]] ||
fail "cannot read $SYS_DIR/defaults, so the built-in shortcuts are unknown"
# Resolve System(X) to the command it runs, as "X<tab>command" pairs.
#
# system_actions carries /// doc comments and commented-out entries for actions
# that exist in the enum but are not wired up (KeyboardBrightnessUp is one), so
# this matches only lines that are a bare name followed by a quoted string.
system_actions() {
[[ -r "$SYS_DIR/system_actions" ]] || return 0
sed -nE 's/^[[:space:]]*([A-Za-z]+):[[:space:]]*"(.*)",?[[:space:]]*$/\1\t\2/p' \
"$SYS_DIR/system_actions"
}
# One binding per line, as "modifiers<tab>key<tab>action".
#
# Two shapes appear. The usual one carries a key:
# (modifiers: [Super, Shift], key: "Escape"): System(LogOut),
# and a modifier-only binding has none at all:
# (modifiers: [Super]): Spawn("rofi -show drun"),
# The second is how Super alone opens the launcher, so dropping it would lose
# the single most used binding on the machine.
bindings() {
sed -nE \
-e 's/^[[:space:]]*\(modifiers:[[:space:]]*\[([^]]*)\],[[:space:]]*key:[[:space:]]*"([^"]*)"\):[[:space:]]*(.*),[[:space:]]*$/\1\t\2\t\3/p' \
-e 's/^[[:space:]]*\(modifiers:[[:space:]]*\[([^]]*)\]\):[[:space:]]*(.*),[[:space:]]*$/\1\t\t\2/p' \
"$1"
}
actions_tsv="$(system_actions)"
# custom is read second so that its rows overwrite defaults in the map below.
# Missing is normal, not an error: a session with no custom bindings has no such
# file, and every default still applies.
raw="$(bindings "$SYS_DIR/defaults")"
if [[ -r "$USER_DIR/custom" ]]; then
raw+=$'\n'"$(bindings "$USER_DIR/custom")"
fi
[[ -n "${raw//[[:space:]]/}" ]] ||
fail "parsed no shortcuts at all from $SYS_DIR/defaults; the file format has changed"
rendered="$(
LC_ALL=C awk -F'\t' -v actions="$actions_tsv" '
BEGIN {
# System(X) -> command, from system_actions.
n = split(actions, lines, "\n")
for (i = 1; i <= n; i++) {
if (split(lines[i], kv, "\t") == 2) cmd[kv[1]] = kv[2]
}
}
# Turn CamelCase into words so an action reads as a description rather than
# an identifier: MoveToWorkspace -> "move to workspace". Applied only to the
# action name, never to a command, which must stay verbatim to be typed.
function words(s, out) {
out = s
gsub(/([a-z0-9])([A-Z])/, "\\1 \\2", out)
return tolower(out)
}
# Super is written first and Shift last, so that chords sort and read the
# way they are pressed rather than the way the RON happened to list them.
function order(m) {
if (m == "Super") return 1
if (m == "Ctrl") return 2
if (m == "Alt") return 3
if (m == "Shift") return 4
return 5
}
{
mods = $1; key = $2; action = $3
if (action == "") next
# "Super, Alt" -> "Super + Alt", in a fixed order.
cnt = split(mods, m, /,[[:space:]]*/)
for (i = 1; i <= cnt; i++) gsub(/^[[:space:]]+|[[:space:]]+$/, "", m[i])
for (i = 1; i < cnt; i++)
for (j = i + 1; j <= cnt; j++)
if (order(m[j]) < order(m[i])) { t = m[i]; m[i] = m[j]; m[j] = t }
chord = ""
for (i = 1; i <= cnt; i++) chord = chord (chord == "" ? "" : " + ") m[i]
if (key != "") chord = chord (chord == "" ? "" : " + ") key
# Spawn("cmd") is a command as written in cosmic.conf.
# System(X) is an action name that system_actions turns into a command.
# Everything else is internal to the compositor and has no command; the
# action itself is the honest answer, and inventing a program name for
# Focus(Left) would be a lie.
what = ""
if (action ~ /^Spawn\(".*"\)$/) {
what = substr(action, 8, length(action) - 9)
} else if (action ~ /^System\(/) {
name = action
sub(/^System\(/, "", name); sub(/\)$/, "", name)
what = (name in cmd) ? cmd[name] : words(name)
} else if (action ~ /\(/) {
name = action; arg = action
sub(/\(.*$/, "", name)
sub(/^[^(]*\(/, "", arg); sub(/\)$/, "", arg)
what = words(name) " " arg
} else {
what = words(action)
}
# Last write wins, which is why custom is appended after defaults.
seen[chord] = what
if (!(chord in orderkey)) orderkey[chord] = ++seq
}
END {
for (c in seen) printf "%d\t%s\t%s\n", orderkey[c], c, seen[c]
}
' <<<"$raw" | sort -n | cut -f2-
)"
count="$(grep -c . <<<"$rendered")"
((count > 0)) || fail "every shortcut line failed to parse; the RON format has changed"
# Column-aligned so the chords form a readable left edge. Done here rather than
# in awk because the width has to be measured across the whole set first.
width="$(cut -f1 <<<"$rendered" | LC_ALL=C awk '{ if (length($0) > w) w = length($0) } END { print w }')"
# -dmenu rather than a static window, so the list is filterable: 122 bindings is
# more than fits on a screen, and typing "workspace" is how you actually use
# this. No theme arguments, so ~/.config/rofi/config.rasi applies and it matches
# the launcher and the power menu.
#
# The selection is discarded. This is a reference, not a menu -- there is no
# sensible action for "you picked Super + Q", and running the bound command on
# Return would make an accidental keypress close a window from a help screen.
LC_ALL=C awk -F'\t' -v w="$width" '{ printf "%-*s %s\n", w, $1, $2 }' <<<"$rendered" |
rofi -dmenu -i -no-custom -no-show-icons -p "keys" \
-mesg "$count shortcuts" >/dev/null 2>&1 || :