feat(src/commands): add Wayland detection and Xorg modesetting support

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.
This commit is contained in:
sankalp committed 2026-05-25 13:27:17 +05:45
1 parent d12d0a0916
commit 94aed29728
1 file changed
+73 -3
+73 -3
View File
@@ -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) {