local D = desktop local later, selector, dispatch = D.later, D.workspace_selector, hl.dispatch local recovering = false local recovery_epoch, lg_mode = 0, "1920x1080@60" local outputs = { oled = "desc:Dell Inc. AW3225QF JR11YZ3", lg = "desc:LG Electronics LG HDR 4K 0x000404F0" } D.hdr, D.lg_enabled = D.flags.hdr ~= false, D.flags.lg ~= false D.flags.hdr, D.flags.lg = D.hdr, D.lg_enabled local function save() D.flags.hdr, D.flags.lg = D.hdr, D.lg_enabled D.save() end local function display(kind) for _, m in ipairs(hl.get_monitors()) do if kind == "oled" and m.description:find("AW3225QF", 1, true) and m.serial == "JR11YZ3" then return m end if kind == "lg" and m.description:find("LG HDR 4K", 1, true) then return m end end end function D.monitors() if recovering then return end local oled, lg = display("oled"), display("lg") local refresh = 239.991 if oled then refresh = 0 for _, mode in ipairs(oled.available_modes) do if mode.width == 3840 and mode.height == 2160 then refresh = math.max(refresh, mode.refresh_rate) end end if refresh == 0 then refresh = 239.991 end end local dual = D.lg_enabled and lg ~= nil hl.monitor({ output = outputs.oled, mode = string.format("3840x2160@%.3f", refresh), position = dual and "0x1680" or "0x0", scale = 1, bitdepth = 10, cm = D.hdr and "hdredid" or "srgb", sdr_max_luminance = 265, sdrbrightness = 1, sdrsaturation = 1 }) hl.monitor({ output = outputs.lg, mode = lg_mode, position = "3840x0", scale = 1, transform = 3, bitdepth = 8, cm = "srgb", disabled = not D.lg_enabled }) end local function cancel_recovery() recovery_epoch = recovery_epoch + 1 recovering = false lg_mode = "1920x1080@60" D.lg_recovery = "pending" end function D.display_profile(dual) D.lg_enabled = dual -- Explicitly move workspaces before disabling; reconnecting does not move them back. if not dual then cancel_recovery() local lg, oled = display("lg"), display("oled") if lg and oled then for _, ws in ipairs(hl.get_workspaces()) do if ws.monitor == lg then dispatch(hl.dsp.workspace.move({ workspace = selector(ws), monitor = oled.name })) end end end end D.monitors() if dual then later(300, D.recover_lg) end save() end function D.hdr_toggle() D.hdr = not D.hdr; D.monitors(); save() end function D.monitor_focus(kind, move) local m = display(kind) if not m then return end if move then if m.active_workspace then D.move(tostring(m.active_workspace.id)) end else dispatch(hl.dsp.focus({ monitor = m.name })) end end -- Monitor rules apply asynchronously. Hold each rung only after its mode is active. function D.recover_lg() if not os.getenv("HYPRLAND_INSTANCE_SIGNATURE") or not D.lg_enabled or not display("lg") or recovering then return end recovering = true recovery_epoch = recovery_epoch + 1 local epoch = recovery_epoch D.lg_recovery = "settling" local steps = { { mode = false, hold = 1000 }, { mode = "1920x1080@60", width = 1920, height = 1080, refresh = 60, hold = 3000 }, { mode = "3840x2160@30", width = 3840, height = 2160, refresh = 30, hold = 3000 }, { mode = "3840x2160@60", width = 3840, height = 2160, refresh = 60, hold = 1000 }, } local function valid() return epoch == recovery_epoch and D.lg_enabled end local rung rung = function(index) if not valid() then return end local step = steps[index] if not step then recovering = false D.lg_recovery = "ready" D.monitors() return end D.lg_recovery = step.mode or "off" if step.mode then lg_mode = step.mode end hl.monitor({ output = outputs.lg, disabled = not step.mode, mode = lg_mode }) local function await_mode(attempt) if not valid() then return end local lg = display("lg") local applied = not step.mode and not lg or step.mode and lg and lg.width == step.width and lg.height == step.height and math.abs(lg.refresh_rate - step.refresh) < 0.1 and lg.dpms_status if applied then later(step.hold, function() rung(index + 1) end) elseif attempt < 50 then later(100, function() await_mode(attempt + 1) end) else cancel_recovery() D.lg_recovery = "failed" D.monitors() end end later(100, function() await_mode(1) end) end -- Match the X11 helper's wake settling time before tearing down the HDMI link. later(3000, function() rung(1) end) end D.on_wake = D.recover_lg D.on_restore = function() D.monitors(); D.recover_lg() end hl.on("monitor.added", function(m) if recovering then return end local lg_added = m.description:find("LG HDR 4K", 1, true) ~= nil later(300, function() if recovering then return end D.monitors() if lg_added then D.recover_lg() end end) end) hl.on("monitor.removed", function(m) if m.description:find("LG HDR 4K", 1, true) and not (recovering and D.lg_recovery == "off") then cancel_recovery() end later(300, D.monitors) end) hl.monitor({ output = outputs.oled, mode = "3840x2160@239.991", position = "0x1680", scale = 1, bitdepth = 10, cm = D.hdr and "hdredid" or "srgb", sdr_max_luminance = 265, sdrbrightness = 1, sdrsaturation = 1 }) hl.monitor({ output = outputs.lg, mode = lg_mode, position = "3840x0", scale = 1, transform = 3, bitdepth = 8, cm = "srgb", disabled = not D.lg_enabled }) local function bind(keys, description, action) hl.bind(keys, action, { description = description }) end bind("SUPER + SHIFT + ALT + 1", "Dual monitor profile", function() D.display_profile(true) end) bind("SUPER + SHIFT + ALT + 2", "OLED only profile", function() D.display_profile(false) end) bind("SUPER + SHIFT + ALT + H", "Toggle OLED HDR", D.hdr_toggle) bind("SUPER + SHIFT + ALT + R", "Retrain LG HDMI after wake", D.wake)