#api-bindings #api

nightly gnome-dbus-api

A simple API to interact with GNOME DBus and Gsettings

11 releases (5 stable)

1.0.4 Oct 14, 2023
1.0.0 Oct 13, 2023
0.1.6 Oct 9, 2023

#1029 in #api

MIT license

140KB
885 lines

Rust dbus and dconf gnome api bindings

Crates.io docs.rs Crates.io Crates.io

A friendly API for interacting with gnome shell, freedesktop and other D-Bus services and settings available on Ubuntu gnome.

This project is thought to be used in Sittly a tauri based app, you will see this trending in the examples.

Disclaimer: I'm not a Rust expert, I'm learning Rust and I'm using this project to learn it. If you see something that can be improved, please open an issue or a PR.

Usage

System apps

This app struct is thought to be used in a GUI to display all the apps installed in the system.

Because of that app.icon is a png image that can be encoded in base64 using app.get_base64_icon() and displayed in a GUI.

use gnome_dbus_api::handlers::easy_gnome::apps::Apps;

#[derive(Serialize, Deserialize, Clone)]
struct AppStruct {
    name: String,
    icon: Option<String>,
    description: String,
}
// Init gtk global instance to use gtk related functions
// If you already have a gtk instance running you must skip this step
gtk::init().unwrap();

async fn get_all_apps() -> Result<Vec<AppStruct>, String> {
  let apps_instance = Apps::new();
  let apps = apps_instance.get_apps();
  let apps_struct: Vec<AppStruct> = apps
      .iter()
      .map(|app| {
          let base64 = app.get_base64_icon();
          let app_struct = AppStruct {
              name: app.name.to_string(),
              icon: base64,
              description: match &app.description {
                  Some(description) => description.to_string(),
                  None => String::from(""),
              },
          };
          app_struct
      })
      .collect();
  Ok(apps_struct)
}

Screen

use gnome_dbus_api::handlers::easy_gnome::screen;

async fn brightness_up() -> Result<(), String> {
    screen::step_up().await;
    Ok(())
}
async fn brightness_down() -> Result<(), String> {
    screen::step_down().await;
    Ok(())
}
async fn get_brightness() -> Result<i32, String> {
    let brightness = screen::brightness().await;
    Ok(brightness)
}
async fn set_brightness(value: i32) -> Result<(), String> {
    screen::set_brightness(value).await;
    Ok(())
}

Night light

use gnome_dbus_api::handlers::easy_gnome::nightlight;

fn get_temperature() {
    let temperature: u32 = nightlight::get_temperature();
}
fn set_temperature() {
    let temperature: u32 = 3000;
    nightlight::set_temperature(temperature);
}

fn reset_temperature() {
    nightlight::reset_temperature();
}
fn set_nightlight_active() {
    let active = true;
    nightlight::set_nightlight_active(active);
}
fn get_nightlight_active() {
   nightlight::get_nightlight_active()
}

Screenshot

use gnome_dbus_api::handlers::easy_gnome::screenshot;
async fn pick_color() {
  let (r, g, b) = screenshot::pick_color().await;
}

Power

use gnome_dbus_api::handlers::easy_gnome::power;
async fn power_off() {
power::power_off().await;
}
async fn reboot() {
power::reboot().await;
}
async fn suspend() {
power::suspend().await;
}

Peripherals

use gnome_dbus_api::handlers::easy_gnome::peripherals;


fn set_keyboard_press_delay() {
    let delay = 100;
    peripherals::set_keyboard_press_delay(delay).unwrap();
    assert_eq!(peripherals::get_keyboard_press_delay().unwrap(), delay);
}

fn reset_keyboard_press_delay() {
    let default_delay = 500;
    peripherals::reset_keyboard_press_delay().unwrap();
    assert_eq!(
        peripherals::get_keyboard_press_delay().unwrap(),
        default_delay
    );
}

fn set_keyboard_repeat_interval() {
    let interval = 100;
    peripherals::set_keyboard_repeat_interval(interval).unwrap();
    assert_eq!(
        peripherals::get_keyboard_repeat_interval().unwrap(),
        interval
    );
}

fn reset_keyboard_repeat_interval() {
    let default_interval = 30;
    peripherals::reset_keyboard_repeat_interval().unwrap();
    assert_eq!(
        peripherals::get_keyboard_repeat_interval().unwrap(),
        default_interval
    );
}

fn set_mouse_natural_scroll() {
    peripherals::set_mouse_natural_scroll(true).unwrap();
    assert_eq!(peripherals::get_mouse_natural_scroll().unwrap(), true);
}

fn reset_mouse_natural_scroll() {
    peripherals::reset_mouse_natural_scroll().unwrap();
    assert_eq!(peripherals::get_mouse_natural_scroll().unwrap(), false);
}

fn set_touchpad_tap_to_click() {
    peripherals::set_touchpad_tap_to_click(false).unwrap();
    assert_eq!(peripherals::get_touchpad_tap_to_click().unwrap(), false);
}

fn reset_touchpad_tap_to_click() {
    peripherals::reset_touchpad_tap_to_click().unwrap();
    assert_eq!(peripherals::get_touchpad_tap_to_click().unwrap(), false);
}

fn set_two_finger_scroll() {
    peripherals::set_two_finger_scroll(false).unwrap();
    assert_eq!(peripherals::get_two_finger_scroll().unwrap(), false);
}

fn reset_two_finger_scroll() {
    peripherals::reset_two_finger_scroll().unwrap();
    assert_eq!(peripherals::get_two_finger_scroll().unwrap(), true);
}

Battery

use gnome_dbus_api::handlers::easy_gnome::battery;

async fn get_battery_display() {
    let battery_display = battery::get_current_device_battery().await.unwrap();

    let full_design_battery = battery_display.energy_full_design().await.unwrap();
    let full_battery = battery_display.energy_full().await.unwrap();
    let current_battery = battery_display.energy().await.unwrap();
    let percentage = battery_display.percentage().await.unwrap();
    let battery_state = battery_display.state().await.unwrap();
    let temperature = battery_display.temperature().await.unwrap();
    let is_rechargable = battery_display.is_rechargeable().await.unwrap();
    let model = battery_display.model().await.unwrap();
    let vendor = battery_display.vendor().await.unwrap();
    let power_supply = battery_display.power_supply().await.unwrap();
    let battery_type = battery_display.type_().await.unwrap();
}

async fn get_devices_battery() {
    let battery_devices = battery::get_devices_battery().await.unwrap();

    for device in battery_devices {
        let full_design_battery = device.energy_full_design().await.unwrap();
        let full_battery = device.energy_full().await.unwrap();
        let current_battery = device.energy().await.unwrap();
        let percentage = device.percentage().await.unwrap();
        let battery_state = device.state().await.unwrap();
        let temperature = device.temperature().await.unwrap();
        let is_rechargable = device.is_rechargeable().await.unwrap();
        let model = device.model().await.unwrap();
        let vendor = device.vendor().await.unwrap();
        let power_supply = device.power_supply().await.unwrap();
        let battery_type = device.type_().await.unwrap();

        println!(
            "full_design_battery: {} full_battery: {} current_battery: {} percentage: {} battery_state: {:?} temperature: {} is_rechargable: {} model: {} vendor: {} power_supply: {} battery_type: {:?}",
            full_design_battery, full_battery, current_battery, percentage, battery_state, temperature, is_rechargable, model, vendor, power_supply,battery_type
        );
    }
}

Gnome extensions

use gnome_dbus_api::handlers::easy_gnome::extensions;

async fn get_extensions() {
    let extensions = extensions::get_extensions().await;
    assert!(extensions.len() > 0);
    println!("{:?}", extensions);
}

async fn launch_extension_preferences() {
    let _extensions_list = extensions::get_extensions().await;
    // You can get the extension uuid from the extensions::get_extensions() function
    let extension_uuid = "ubuntu-appindicators@ubuntu.com";
    extensions::open_extension_preferences(extension_uuid).await
}

async fn disable_extension() {
    let _extensions_list = extensions::get_extensions().await;
    // You can get the extension uuid from the extensions::get_extensions() function
    let extension_uuid = "extension-list@tu.berry";
    extensions::disable_extension(extension_uuid).await
}

async fn enable_extension() {
    let _extensions_list = extensions::get_extensions().await;
    // You can get the extension uuid from the extensions::get_extensions() function
    let extension_uuid = "extension-list@tu.berry";
    extensions::enable_extension(extension_uuid).await
}

async fn uninstall_extension() {
    let _extensions_list = extensions::get_extensions().await;
    // You can get the extension uuid from the extensions::get_extensions() function
    let extension_uuid = "extension-list@tu.berry";
    extensions::uninstall_extension(extension_uuid).await
}

Features

  • Power management

    • Power off
    • Reboot
    • Suspend
  • Locales

    • Get x11 layout
  • Gnome extensions

    • Get extensions
    • Enable extension
    • Disable extension
    • Uninstall extension
  • Gnome shell screenshot

    • Pick color
  • Settings

    • Night light
      • Get night light status
      • Set night light status
      • Get night light temperature
      • Set night light temperature
  • Gsettings Dconf (https://crates.io/crates/dconf_rs/0.3.0)

    • image
    • org.gnome.desktop.peripherals.touchpad two-finger-scrolling-enabled true
    • org.gnome.desktop.peripherals.touchpad tap-to-click true
    • org.gnome.desktop.peripherals.mouse natural-scroll true
    • org.gnome.desktop.peripherals.keyboard repeat-interval 30 (initial key repeat delay)
    • org.gnome.desktop.peripherals.keyboard delay 500 (initial key repeat delay)
    • org.gnome.desktop.interface show-battery-percentage true
    • org.gnome.desktop.interface locate-pointer false (with ctrl key)
    • org.gnome.desktop.interface cursor-size 24

Dependencies

~30–44MB
~751K SLoC