Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 68 additions & 1 deletion default/hypr/wide-gamut.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,73 @@ local paths = require("default.hypr.paths")

local chosen = {}

-- The area of the primaries triangle in an EDID's chromaticity block (sRGB is
-- about 0.11). A panel that reports no primaries has no gamut to map into:
-- "edid" then collapses the output to black. Unreadable EDIDs count as usable.
local function edid_has_gamut(name)
if not name:match("^[%w%-]+$") then
return true
end

local pipe = io.popen("cat /sys/class/drm/card*-" .. name .. "/edid 2>/dev/null", "r")
if not pipe then
return true
end
local edid = pipe:read("a")
pipe:close()
if #edid < 35 then

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: requiring 128 bytes and the EDID header would keep a truncated blob from being read as primaries.

— Marcelo's agent

return true
end

local function coord(msb_offset, lsb_offset, shift)
local msb = edid:byte(msb_offset + 1)
local lsb = (edid:byte(lsb_offset + 1) // 2 ^ shift) % 4
return (msb * 4 + lsb) / 1024
end

local rx, ry = coord(27, 25, 6), coord(28, 25, 4)
local gx, gy = coord(29, 25, 2), coord(30, 25, 0)
local bx, by = coord(31, 26, 6), coord(32, 26, 4)
return math.abs(rx * (gy - by) + gx * (by - ry) + bx * (ry - gy)) / 2 > 0.01
end

-- Whether a rule's output selector covers a monitor: "" covers all, otherwise
-- the port name or a "desc:" prefix of the monitor description.
local function covers(output, monitor)
local prefix = output:match("^desc:(.+)$")
if prefix then
return (monitor.description or ""):sub(1, #prefix) == prefix
end
return output == "" or monitor.name == output
end

-- The color mapping for a rule. "edid" when every monitor it covers has a
-- gamut. A Mac's built-in panel is always P3, so a built-in panel that reports
-- no primaries gets "dp3", which needs no EDID: sRGB there would show the
-- panel's native gamut oversaturated. Any other monitor without primaries (or
-- one sharing the rule with such a panel) gets no mapping, which is sRGB.
local function cm_for_rule(output)
local builtin_without, other = false, false
for _, monitor in ipairs(hl.get_monitors()) do

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If hl.get_monitors() is still empty while the config first loads, the catch-all gets edid and the panel goes black at login. Worth re-checking on hyprland.start and monitor.added.

— Marcelo's agent

if covers(output, monitor) then
local builtin = monitor.name:match("^eDP") ~= nil
if not edid_has_gamut(monitor.name) then
if not builtin then
return nil
end
builtin_without = true
elseif not builtin then
other = true
end
end
end

if not builtin_without then
return "edid"
end
return (not other) and "dp3" or nil

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the catch-all covers only a panel without primaries, it gets dp3, and an sRGB display plugged in later inherits it. Could the dp3 go on the panel's own connector instead, with the catch-all staying on edid?

— Marcelo's agent

end

local function chosen_for(output)
if chosen[output] then
return chosen[output]
Expand Down Expand Up @@ -47,7 +114,7 @@ if o.shell_succeeds(o.shell_quote(paths.omarchy_path .. "/bin/omarchy-hw-aarch64
if rule.cm ~= nil then
chosen[output] = rule.cm
else
rule.cm = chosen_for(output) or "edid"
rule.cm = chosen_for(output) or cm_for_rule(output)
end
end

Expand Down
75 changes: 75 additions & 0 deletions test/shell.d/wide-gamut-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/bin/bash

set -euo pipefail

source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"

require_command lua

# Runs the wrapper with fake monitors and EDIDs and checks the cm each rule gets.
OMARCHY_PATH="$ROOT" lua - <<'LUA' || fail "wide-gamut picks the color mapping for each monitor rule"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Failed assertions can pass

On Lua 5.5, failed scripts read from stdin can exit successfully. A failed assertion here can skip fail and still print the final success message, hiding a broken color-mapping change. Run the Lua code from a temporary file, as hyprland-workspace-layout-test.sh does.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Comment on lines +9 to +10

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Related tests are split

This adds a separate *-test.sh even though hyprland-monitor-settings-test.sh already tests wide-gamut.lua, including explicit color choices and description rules. The contributor guide requires extending the area's existing test file rather than adding another one. Move these EDID cases into that file before merging.

File Used: AGENTS.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

local script = os.getenv("OMARCHY_PATH") .. "/default/hypr/wide-gamut.lua"

package.loaded["default.hypr.paths"] = { omarchy_path = "/omarchy" }
o = {
shell_succeeds = function() return true end,
shell_quote = function(value) return value end,
}

-- An EDID whose chromaticity block holds the DCI-P3 primaries, or none at all.
local function edid(primaries)
local bytes = {}
for i = 1, 128 do bytes[i] = 0 end
if primaries then
local function ten(x) return math.floor(x * 1024 + 0.5) end
local rx, ry, gx, gy, bx, by = ten(0.680), ten(0.320), ten(0.265), ten(0.690), ten(0.150), ten(0.060)
bytes[26] = (rx % 4) << 6 | (ry % 4) << 4 | (gx % 4) << 2 | (gy % 4)
bytes[27] = (bx % 4) << 6 | (by % 4) << 4
bytes[28], bytes[29], bytes[30], bytes[31], bytes[32], bytes[33] =
rx >> 2, ry >> 2, gx >> 2, gy >> 2, bx >> 2, by >> 2
end
return string.char(table.unpack(bytes))
end

local with_gamut, without_gamut = edid(true), edid(false)

-- monitors: { {name, description, edid}, ... }; rule: the table passed to hl.monitor.
local function cm_for(monitors, rule)
local by_name, list, seen = {}, {}, nil
for _, m in ipairs(monitors) do
by_name[m[1]] = m[3]
list[#list + 1] = { name = m[1], description = m[2] }
end

io.popen = function(command)
local name = command:match("card%*%-([%w%-]+)/edid")
return { read = function() return by_name[name] or "" end, close = function() end }
end
hl = {
get_monitors = function() return list end,
monitor = function(r) seen = r end,
}

dofile(script)
hl.monitor(rule)
return seen.cm
end

local function expect(got, want, what)
assert(got == want, what .. ": got " .. tostring(got) .. ", want " .. tostring(want))
end

local panel = { "eDP-2", "Built-in", with_gamut }
local panel_blank = { "eDP-2", "Built-in", without_gamut }
local external = { "DP-1", "Studio Display", with_gamut }
local external_blank = { "DP-1", "Other", without_gamut }

expect(cm_for({ panel }, { output = "eDP-2" }), "edid", "panel with primaries")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of these cases uses a desc: selector without cm, so the covers() description match from 5c3bd3c has no test.

— Marcelo's agent

expect(cm_for({ panel_blank }, { output = "eDP-2" }), "dp3", "built-in panel without primaries")
expect(cm_for({ external_blank }, { output = "DP-1" }), nil, "external monitor without primaries")
expect(cm_for({ panel, external }, { output = "" }), "edid", "all monitors with primaries")
expect(cm_for({ panel_blank, external }, { output = "" }), nil, "a rule shared by a blank panel and an external monitor")
expect(cm_for({ panel_blank, external }, { output = "eDP-2" }), "dp3", "the rule for the panel alone")
expect(cm_for({ panel_blank }, { output = "eDP-2", cm = "srgb" }), "srgb", "a cm in the rule is kept")
LUA
pass "wide-gamut maps a built-in panel without primaries to dp3, never to a black screen"