pika-kernel-manager/src/content/mod.rs

200 lines
6.5 KiB
Rust
Raw Normal View History

use adw::prelude::ActionRowExt;
use gtk::{Align, IconSize, Orientation, SelectionMode, SizeGroupMode};
2024-06-17 00:03:37 +02:00
use gtk::prelude::{BoxExt, WidgetExt};
2024-06-17 05:48:56 +02:00
use std::process::{Command, Stdio};
2024-06-17 07:36:38 +02:00
use crate::{KernelBranch, RunningKernelInfo};
use Vec;
2024-06-17 19:11:51 +02:00
use std::fs;
use std::path::Path;
use duct::cmd;
use version_compare::Version;
2024-06-17 00:03:37 +02:00
pub fn content() -> gtk::Box {
2024-06-17 05:48:56 +02:00
let running_kernel_info = get_running_kernel_info();
2024-06-17 00:03:37 +02:00
let content_box = gtk::Box::builder()
.hexpand(true)
.vexpand(true)
.orientation(Orientation::Vertical)
2024-06-17 00:03:37 +02:00
.build();
let tux_icon = gtk::Image::builder()
.pixel_size(128)
.halign(Align::Center)
.hexpand(true)
.margin_start(10)
.margin_end(10)
.margin_bottom(20)
.margin_top(20)
2024-06-17 00:03:37 +02:00
.build();
tux_icon.set_icon_name(Some("tux-symbolic"));
2024-06-17 00:06:45 +02:00
tux_icon.add_css_class("symbolic-accent-bg");
2024-06-17 00:03:37 +02:00
2024-06-17 07:36:38 +02:00
let kernel_badge_box = gtk::Box::builder()
.hexpand(true)
.vexpand(true)
.orientation(Orientation::Vertical)
.build();
2024-06-17 07:59:10 +02:00
let kernel_branch_expander_row = adw::ExpanderRow::builder()
.build();
let kernel_branch_expander_row_boxedlist = gtk::ListBox::builder()
.selection_mode(SelectionMode::None)
.hexpand(true)
.vexpand(true)
.halign(Align::Center)
.margin_start(10)
.margin_end(10)
.margin_bottom(20)
.margin_top(20)
.build();
kernel_branch_expander_row_boxedlist.add_css_class("boxed-list");
kernel_branch_expander_row_boxedlist.append(&kernel_branch_expander_row);
2024-06-17 07:36:38 +02:00
create_kernel_badges(&kernel_badge_box, &running_kernel_info);
2024-06-17 07:36:38 +02:00
content_box.append(&kernel_badge_box);
2024-06-17 00:03:37 +02:00
content_box.append(&tux_icon);
2024-06-17 07:59:10 +02:00
content_box.append(&kernel_branch_expander_row_boxedlist);
2024-06-17 00:03:37 +02:00
content_box
}
2024-06-17 05:48:56 +02:00
fn create_kernel_badge(label0_text: &str, label1_text: &str, css_style: &str, group_size: &gtk::SizeGroup, group_size0: &gtk::SizeGroup, group_size1: &gtk::SizeGroup) -> gtk::ListBox {
let badge_box = gtk::Box::builder()
.build();
let label0 = gtk::Label::builder()
.label(label0_text)
.margin_start(5)
.margin_end(5)
.margin_bottom(1)
.margin_top(1)
.hexpand(true)
.vexpand(true)
.build();
2024-06-17 05:48:56 +02:00
group_size0.add_widget(&label0);
let label_seprator = gtk::Separator::builder()
.build();
let label1 = gtk::Label::builder()
.label(label1_text)
.margin_start(3)
.margin_end(0)
.margin_bottom(1)
.margin_top(1)
.hexpand(true)
.vexpand(true)
.build();
2024-06-17 05:48:56 +02:00
group_size1.add_widget(&label1);
label1.add_css_class(css_style);
badge_box.append(&label0);
badge_box.append(&label_seprator);
badge_box.append(&label1);
let boxedlist = gtk::ListBox::builder()
.selection_mode(SelectionMode::None)
.halign(Align::Center)
.valign(Align::End)
.margin_start(5)
.margin_end(5)
.margin_bottom(5)
.margin_top(5)
.build();
boxedlist.add_css_class("boxed-list");
boxedlist.append(&badge_box);
group_size.add_widget(&boxedlist);
boxedlist
2024-06-17 05:48:56 +02:00
}
2024-06-17 07:59:10 +02:00
fn get_kernel_branches() -> Vec<KernelBranch> {
let test_branch = KernelBranch {
name: "kernel-cachy".to_string(),
2024-06-17 08:02:13 +02:00
db: "https://raw.githubusercontent.com/CosmicFusion/fedora-kernel-manager/main/data/db-kernel-cachy.json".to_string()
2024-06-17 07:59:10 +02:00
};
2024-06-17 08:02:13 +02:00
vec![test_branch]
2024-06-17 07:59:10 +02:00
}
2024-06-17 05:48:56 +02:00
fn get_running_kernel_info() -> RunningKernelInfo {
2024-06-17 07:36:38 +02:00
let kernel = match Command::new("uname").arg("-r").stdout(Stdio::piped()).output() {
2024-06-17 05:48:56 +02:00
Ok(t) => String::from_utf8(t.stdout).unwrap().trim().to_owned(),
Err(_) => "Unknown".to_string()
};
2024-06-17 07:36:38 +02:00
let version = match linux_version::linux_kernel_version() {
2024-06-17 19:11:51 +02:00
Ok(t) => {
if t.patch == 0 {
format!("{}.{}", t.major, t.minor)
} else {
format!("{}.{}.{}", t.major, t.minor, t.patch)
}
}
2024-06-17 07:36:38 +02:00
Err(_) => "Unknown".to_string()
};
2024-06-17 05:48:56 +02:00
let info = RunningKernelInfo {
2024-06-17 07:36:38 +02:00
kernel: kernel,
2024-06-17 19:11:51 +02:00
version: version.clone(),
2024-06-17 07:36:38 +02:00
// didn't find a way to accurately get this, outside of sched-ext (https://github.com/CachyOS/kernel-manager/blob/develop/src/schedext-window.cpp)
2024-06-17 19:11:51 +02:00
sched: get_current_scheduler(version)
2024-06-17 05:48:56 +02:00
};
info
2024-06-17 07:36:38 +02:00
}
2024-06-17 19:11:51 +02:00
fn get_current_scheduler(version: String) -> String {
if Path::new("/sys/kernel/sched_ext/root/ops").exists() {
println!("sched_ext is detected, getting scx scheduler");
let scx_sched = "sched_ext: ".to_string() + fs::read_to_string("/sys/kernel/sched_ext/root/ops").unwrap().as_str();
scx_sched
} else if bore_check() {
"BORE".to_string()
} else if Version::from(&version) >= Version::from("6.6") {
"EEVDF?".to_string()
} else {
"CFS?".to_string()
}
}
fn bore_check() -> bool {
let is_bore= match cmd!("sysctl", "-n", "kernel.sched_bore").read() {
Ok(t) => {
if t == "1" {
true
} else {
false
}
}
Err(_) => false
};
is_bore
}
2024-06-17 07:36:38 +02:00
fn create_kernel_badges(badge_box: &gtk::Box, running_kernel_info: &RunningKernelInfo) {
let kernel_badges_size_group = gtk::SizeGroup::new(SizeGroupMode::Both);
let kernel_badges_size_group0 = gtk::SizeGroup::new(SizeGroupMode::Both);
let kernel_badges_size_group1 = gtk::SizeGroup::new(SizeGroupMode::Both);
let kernel_version = "6.9";
let version_css_style = if &running_kernel_info.version.as_str() == &kernel_version {
"background-green-bg"
}
else {
"background-red-bg"
};
badge_box.append(&create_kernel_badge("Kernel Branch", "cachy", "background-accent-bg", &kernel_badges_size_group, &kernel_badges_size_group0, &kernel_badges_size_group1));
badge_box.append(&create_kernel_badge("Latest Version", "6.9", "background-accent-bg", &kernel_badges_size_group, &kernel_badges_size_group0, &kernel_badges_size_group1));
badge_box.append(&create_kernel_badge("Running Version", &running_kernel_info.version, &version_css_style, &kernel_badges_size_group, &kernel_badges_size_group0, &kernel_badges_size_group1));
badge_box.append(&create_kernel_badge("Running Kernel", &running_kernel_info.kernel, &version_css_style, &kernel_badges_size_group, &kernel_badges_size_group0, &kernel_badges_size_group1));
badge_box.append(&create_kernel_badge("Running Sched", &running_kernel_info.sched, "background-accent-bg", &kernel_badges_size_group, &kernel_badges_size_group0, &kernel_badges_size_group1));
2024-06-17 00:03:37 +02:00
}