fix: Show context for graphics only if switchable graphics found
This commit is contained in:
parent
21f699e483
commit
eb6028f8fc
4 changed files with 81 additions and 16 deletions
17
Cargo.lock
generated
17
Cargo.lock
generated
|
|
@ -1184,6 +1184,12 @@ dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "numtoa"
|
||||||
|
version = "0.2.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6aa2c4e539b869820a2b82e1aef6ff40aa85e65decdd5185e83fb4b1249cd00f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "objc"
|
name = "objc"
|
||||||
version = "0.2.7"
|
version = "0.2.7"
|
||||||
|
|
@ -1398,6 +1404,7 @@ dependencies = [
|
||||||
name = "pop-launcher-plugins"
|
name = "pop-launcher-plugins"
|
||||||
version = "1.1.0"
|
version = "1.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
"async-pidfd",
|
"async-pidfd",
|
||||||
"fork",
|
"fork",
|
||||||
"freedesktop-desktop-entry",
|
"freedesktop-desktop-entry",
|
||||||
|
|
@ -1416,6 +1423,7 @@ dependencies = [
|
||||||
"slab",
|
"slab",
|
||||||
"smol",
|
"smol",
|
||||||
"strsim",
|
"strsim",
|
||||||
|
"sysfs-class",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
"url",
|
"url",
|
||||||
|
|
@ -1846,6 +1854,15 @@ dependencies = [
|
||||||
"unicode-xid",
|
"unicode-xid",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sysfs-class"
|
||||||
|
version = "0.1.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5e1bbcf869732c45a77898f7f61ed6d411dfc37613517e444842f58d428856d1"
|
||||||
|
dependencies = [
|
||||||
|
"numtoa",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "system-deps"
|
name = "system-deps"
|
||||||
version = "6.0.2"
|
version = "6.0.2"
|
||||||
|
|
|
||||||
|
|
@ -32,3 +32,5 @@ zvariant = "3.1.2"
|
||||||
ward = "2.1.0"
|
ward = "2.1.0"
|
||||||
isahc = "1.6.0"
|
isahc = "1.6.0"
|
||||||
url = "2.2.2"
|
url = "2.2.2"
|
||||||
|
sysfs-class = "0.1.3"
|
||||||
|
anyhow = "1.0.55"
|
||||||
|
|
|
||||||
43
plugins/src/desktop_entries/graphics.rs
Normal file
43
plugins/src/desktop_entries/graphics.rs
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
// Copyright 2022 System76 <info@system76.com>
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-only
|
||||||
|
|
||||||
|
use anyhow::Context;
|
||||||
|
use sysfs_class::{PciDevice, SysClass};
|
||||||
|
|
||||||
|
/// Checks if the system has switchable graphics.
|
||||||
|
///
|
||||||
|
/// A system is considered switchable if multiple graphics card devices are found.
|
||||||
|
pub fn is_switchable() -> bool {
|
||||||
|
let main = || -> anyhow::Result<bool> {
|
||||||
|
let devices = PciDevice::all().context("cannot get PCI devices")?;
|
||||||
|
|
||||||
|
let mut amd_graphics = 0;
|
||||||
|
let mut intel_graphics = 0;
|
||||||
|
let mut nvidia_graphics = 0;
|
||||||
|
|
||||||
|
for dev in devices {
|
||||||
|
let c = dev.class().context("cannot get class of device")?;
|
||||||
|
if let 0x03 = (c >> 16) & 0xFF {
|
||||||
|
match dev.vendor().context("cannot get vendor of device")? {
|
||||||
|
0x1002 => amd_graphics += 1,
|
||||||
|
0x10DE => nvidia_graphics += 1,
|
||||||
|
0x8086 => intel_graphics += 1,
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let switchable = (nvidia_graphics > 0 && (intel_graphics > 0 || amd_graphics > 0))
|
||||||
|
|| (intel_graphics > 0 && amd_graphics > 0);
|
||||||
|
|
||||||
|
Ok(switchable)
|
||||||
|
};
|
||||||
|
|
||||||
|
match main() {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(why) => {
|
||||||
|
tracing::error!("{}", why);
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
// SPDX-License-Identifier: GPL-3.0-only
|
// SPDX-License-Identifier: GPL-3.0-only
|
||||||
// Copyright © 2021 System76
|
// Copyright © 2021 System76
|
||||||
|
|
||||||
|
mod graphics;
|
||||||
|
|
||||||
use crate::*;
|
use crate::*;
|
||||||
use freedesktop_desktop_entry::{default_paths, DesktopEntry, Iter as DesktopIter, PathSource};
|
use freedesktop_desktop_entry::{default_paths, DesktopEntry, Iter as DesktopIter, PathSource};
|
||||||
use futures_lite::{AsyncWrite, StreamExt};
|
use futures_lite::{AsyncWrite, StreamExt};
|
||||||
|
|
@ -20,7 +22,6 @@ struct Item {
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
prefers_non_default_gpu: bool,
|
prefers_non_default_gpu: bool,
|
||||||
src: PathSource,
|
src: PathSource,
|
||||||
terminal_command: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Hash for Item {
|
impl Hash for Item {
|
||||||
|
|
@ -162,7 +163,6 @@ impl<W: AsyncWrite + Unpin> App<W> {
|
||||||
icon: entry.icon().map(|x| x.to_owned()),
|
icon: entry.icon().map(|x| x.to_owned()),
|
||||||
exec: exec.to_owned(),
|
exec: exec.to_owned(),
|
||||||
path: path.clone(),
|
path: path.clone(),
|
||||||
terminal_command: entry.terminal(),
|
|
||||||
prefers_non_default_gpu: entry.prefers_non_default_gpu(),
|
prefers_non_default_gpu: entry.prefers_non_default_gpu(),
|
||||||
src,
|
src,
|
||||||
};
|
};
|
||||||
|
|
@ -212,22 +212,25 @@ impl<W: AsyncWrite + Unpin> App<W> {
|
||||||
|
|
||||||
async fn context(&mut self, id: u32) {
|
async fn context(&mut self, id: u32) {
|
||||||
if let Some(entry) = self.entries.get(id as usize) {
|
if let Some(entry) = self.entries.get(id as usize) {
|
||||||
let option = ContextOption {
|
let mut options = Vec::new();
|
||||||
id: 0,
|
|
||||||
name: (if entry.prefers_non_default_gpu {
|
|
||||||
"Launch Using Integrated Graphics Card"
|
|
||||||
} else {
|
|
||||||
"Launch Using Discrete Graphics Card"
|
|
||||||
})
|
|
||||||
.to_owned(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let response = PluginResponse::Context {
|
if graphics::is_switchable() {
|
||||||
id,
|
options.push(ContextOption {
|
||||||
options: vec![option],
|
id: 0,
|
||||||
};
|
name: (if entry.prefers_non_default_gpu {
|
||||||
|
"Launch Using Integrated Graphics Card"
|
||||||
|
} else {
|
||||||
|
"Launch Using Discrete Graphics Card"
|
||||||
|
})
|
||||||
|
.to_owned(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
send(&mut self.tx, response).await;
|
if !options.is_empty() {
|
||||||
|
let response = PluginResponse::Context { id, options };
|
||||||
|
|
||||||
|
send(&mut self.tx, response).await;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue