From 94aed29728f0c36788869193a02d95346ec58285 Mon Sep 17 00:00:00 2001 From: S4NKALP Date: Mon, 25 May 2026 13:27:17 +0545 Subject: [PATCH] feat(src/commands): add Wayland detection and Xorg modesetting support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Enable automatic handling of NVIDIA commands in Wayland sessions and generate proper Xorg config snippets. - New Features - Adds `is_wayland_session()` that detects a Wayland environment via `WAYLAND_DISPLAY` or `XDG_SESSION_TYPE`. - Adds `xorg_modesetting(bus_id)` that builds a full Xorg configuration snippet for the modesetting driver. - Enhancements - `switch_nvidia()` now automatically treats NVIDIA commands as Wayland‑aware when a Wayland session is detected. - Refactored command matching in `execute()` to preserve original fields while injecting Wayland awareness. - Fixed daemon invocation to pass a reference to the (potentially transformed) command. - Bug Fixes - Corrected argument passing in `try_call_daemon` and `execute_local` to avoid move errors. - Prevented duplicate command handling when a Wayland session is detected. --- src/commands/gpu.rs | 76 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 3 deletions(-) diff --git a/src/commands/gpu.rs b/src/commands/gpu.rs index 5c9c46f..e2f656b 100644 --- a/src/commands/gpu.rs +++ b/src/commands/gpu.rs @@ -109,6 +109,13 @@ fn try_call_daemon(cmd: &GpuCommands) -> bool { }) } +fn is_wayland_session() -> bool { + std::env::var("WAYLAND_DISPLAY").is_ok() + || std::env::var("XDG_SESSION_TYPE") + .map(|v| v == "wayland") + .unwrap_or(false) +} + const CACHE_FILE_PATH: &str = "/var/cache/lapctl/cache.json"; const BLACKLIST_PATH: &str = "/etc/modprobe.d/blacklist-nvidia.conf"; const BLACKLIST_CONTENT: &str = r#"# Automatically generated by lapctl @@ -289,6 +296,42 @@ EndSection ) } +pub fn xorg_modesetting(bus_id: &str) -> String { + format!( + r#"# Automatically generated by lapctl + +Section "ServerLayout" + Identifier "layout" + Screen 0 "nvidia" + Inactive "modesetting" +EndSection + +Section "Device" + Identifier "nvidia" + Driver "nvidia" + BusID "{}" +EndSection + +Section "Screen" + Identifier "nvidia" + Device "nvidia" + Option "AllowEmptyInitialConfiguration" +EndSection + +Section "Device" + Identifier "modesetting" + Driver "modesetting" +EndSection + +Section "Screen" + Identifier "modesetting" + Device "modesetting" +EndSection +"#, + bus_id + ) +} + pub fn generate_xrandr_script(igpu_vendor: Option<&String>) -> String { let source_name = match igpu_vendor.map(|s| s.as_str()) { Some("intel") => "modesetting".to_string(), @@ -580,6 +623,9 @@ fn switch_nvidia( no_reboot: bool, ) { assert_root(); + + let wayland = wayland || is_wayland_session(); + println!("Switching to nvidia mode"); println!("Enable ForceCompositionPipeline: {}", force_comp); println!("Enable Coolbits: {}", coolbits.is_some()); @@ -628,6 +674,8 @@ fn switch_nvidia( } else if vendor == "amd" { create_file(XORG_PATH, &xorg_amd(&pci_bus), false); } + } else { + create_file(XORG_PATH, &xorg_modesetting(&pci_bus), false); } let mut extra_xorg = EXTRA_XORG_CONTENT.to_string(); @@ -744,18 +792,40 @@ fn run_on_dgpu(command: &[String]) { } pub fn execute(cmd: &GpuCommands) { - match cmd { + let cmd = match cmd { + GpuCommands::Nvidia { + wayland: false, + dm, + force_comp, + coolbits, + use_nvidia_current, + no_reboot, + } if is_wayland_session() => { + println!("Auto-detected Wayland session"); + GpuCommands::Nvidia { + wayland: true, + dm: dm.clone(), + force_comp: *force_comp, + coolbits: *coolbits, + use_nvidia_current: *use_nvidia_current, + no_reboot: *no_reboot, + } + } + _ => cmd.clone(), + }; + + match &cmd { GpuCommands::Integrated { .. } => println!("Switching to Integrated GPU mode..."), GpuCommands::Hybrid { .. } => println!("Switching to Hybrid GPU mode..."), GpuCommands::Nvidia { .. } => println!("Switching to NVIDIA-only GPU mode..."), _ => {} } - if try_call_daemon(cmd) { + if try_call_daemon(&cmd) { return; } - execute_local(cmd); + execute_local(&cmd); } pub fn execute_local(cmd: &GpuCommands) {