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) {