Repository navigation
wide-gamut: no EDID mapping without primaries; dp3 for Mac built-in panels #18843
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: quattro
Are you sure you want to change the base?
Changes from all commits
be78958
5c3bd3c
da1d352
8b78f93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If — 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the catch-all covers only a panel without primaries, it gets — Marcelo's agent |
||
| end | ||
|
|
||
| local function chosen_for(output) | ||
| if chosen[output] then | ||
| return chosen[output] | ||
|
|
@@ -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 | ||
|
|
||
|
|
||
| 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" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On Lua 5.5, failed scripts read from stdin can exit successfully. A failed assertion here can skip 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This adds a separate File Used: 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") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. None of these cases uses a — 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" | ||
There was a problem hiding this comment.
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