Skip to content

Commit

Permalink
monitors: move to env for flatpak
Browse files Browse the repository at this point in the history
  • Loading branch information
DashieTM committed Jun 11, 2024
1 parent a44ec94 commit 982502c
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 152 deletions.
52 changes: 29 additions & 23 deletions keyboard_plugin/src/backend/gnome.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
use std::process::Command;

use glib::{getenv, Variant, VariantTy};
use re_set_lib::ERROR;
#[cfg(debug_assertions)]
use re_set_lib::{utils::macros::ErrorLevel, write_log_to_file};
use re_set_lib::ERROR;

use crate::keyboard_layout::KeyboardLayout;

pub fn get_saved_layouts_gnome(all_keyboards: &[KeyboardLayout]) -> Vec<KeyboardLayout> {
let mut kb = vec![];

let result;
if getenv("container").is_none() {
dbg!("not flatpak");
result = Command::new("dconf")
.args(&["read", "/org/gnome/desktop/input-sources/sources"])
.output();
let result = if getenv("container").is_none() {
Command::new("dconf")
.args(["read", "/org/gnome/desktop/input-sources/sources"])
.output()
} else {
dbg!("flatpak");
result = Command::new("flatpak-spawn")
.args(&["--host", "dconf", "read", "/org/gnome/desktop/input-sources/sources"])
.output();
}
Command::new("flatpak-spawn")
.args([
"--host",
"dconf",
"read",
"/org/gnome/desktop/input-sources/sources",
])
.output()
};
if result.is_err() {
dbg!("result error");
return kb;
}
let output = result.unwrap();
let layout_variant = String::from_utf8(output.stdout).unwrap();

let layout_variant = Variant::parse(Some(&VariantTy::new("a(ss)").unwrap()), &layout_variant);
let layout_variant = Variant::parse(Some(VariantTy::new("a(ss)").unwrap()), &layout_variant);
if layout_variant.is_err() {
dbg!("couldnt parse layout variant");
return kb;
}
let layout_variant = layout_variant.unwrap();
dbg!(&layout_variant);
let layouts = layout_variant.get::<Vec<(String, String)>>().unwrap();
for layout in layouts {
let kb_layout: Vec<&KeyboardLayout> = if layout.1.contains("+") {
Expand Down Expand Up @@ -71,24 +70,31 @@ pub fn write_to_config_gnome(layouts: &Vec<KeyboardLayout>) {
}

let mut all_layouts = all_layouts.join(", ");
all_layouts.insert_str(0, "[");
all_layouts.push_str("]");
all_layouts.insert(0, '[');
all_layouts.push(']');

if getenv("container").is_none() {
dbg!("not flatpak");
let result = Command::new("dconf")
.args(&["write", "/org/gnome/desktop/input-sources/sources", all_layouts.as_str()])
.args([
"write",
"/org/gnome/desktop/input-sources/sources",
all_layouts.as_str(),
])
.output();
if result.is_err() {
ERROR!("Failed to write layouts", ErrorLevel::PartialBreakage);
}
} else {
dbg!("flatpak");
let result = Command::new("flatpak-spawn")
.args(&["--host", "dconf", "write", "/org/gnome/desktop/input-sources/sources", all_layouts.as_str()])
.args([
"--host",
"dconf",
"write",
"/org/gnome/desktop/input-sources/sources",
all_layouts.as_str(),
])
.output();
if result.is_err() {
dbg!("Failed to write with flatpak");
ERROR!("Failed to write layouts", ErrorLevel::PartialBreakage);
}
}
Expand Down
2 changes: 1 addition & 1 deletion keyboard_plugin/src/backend/hyprland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn get_saved_layouts_hyprland(all_keyboards: &[KeyboardLayout]) -> Vec<Keybo
kb
}

pub fn write_to_config_hyprland(layouts: &Vec<KeyboardLayout>) {
pub fn write_to_config_hyprland(layouts: &[KeyboardLayout]) {
let path;
#[allow(clippy::borrow_interior_mutable_const)]
if let Some(test) = CONFIG.get("Keyboard").unwrap().get("path") {
Expand Down
2 changes: 1 addition & 1 deletion keyboard_plugin/src/backend/kde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn get_saved_layouts_kde(all_keyboards: &[KeyboardLayout]) -> Vec<KeyboardLa
kb
}

pub fn write_to_config_kde(layouts: &Vec<KeyboardLayout>) {
pub fn write_to_config_kde(layouts: &[KeyboardLayout]) {
let mut layout_string = String::new();
let mut variant_string = String::new();
for x in layouts.iter() {
Expand Down
99 changes: 44 additions & 55 deletions keyboard_plugin/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ use std::sync::{Arc, RwLock, RwLockWriteGuard};

use dbus_crossroads::IfaceBuilder;
use re_set_lib::utils::plugin_setup::CrossWrapper;
use xkbregistry::{rxkb_context_new, RXKB_CONTEXT_NO_FLAGS, rxkb_context_parse_default_ruleset, rxkb_context_unref, rxkb_layout_first, rxkb_layout_get_description, rxkb_layout_get_name, rxkb_layout_get_variant, rxkb_layout_next};
use xkbregistry::{
rxkb_context_new, rxkb_context_parse_default_ruleset, rxkb_context_unref, rxkb_layout_first,
rxkb_layout_get_description, rxkb_layout_get_name, rxkb_layout_get_variant, rxkb_layout_next,
RXKB_CONTEXT_NO_FLAGS,
};

use crate::backend::gnome::{get_saved_layouts_gnome, write_to_config_gnome};
use crate::backend::hyprland::{get_saved_layouts_hyprland, write_to_config_hyprland};
Expand All @@ -12,8 +16,8 @@ use crate::keyboard_layout::KeyboardLayout;
use crate::r#const::{GNOME, HYPRLAND, INTERFACE, KDE};
use crate::utils::get_environment;

mod hyprland;
mod gnome;
mod hyprland;
mod kde;

#[no_mangle]
Expand All @@ -31,17 +35,11 @@ pub fn get_saved_layouts() -> Vec<KeyboardLayout> {
if env.contains(GNOME) {
return get_saved_layouts_gnome(&all_keyboards);
}

match env.as_str() {
HYPRLAND => {
get_saved_layouts_hyprland(&all_keyboards)
}
GNOME => {
get_saved_layouts_gnome(&all_keyboards)
}
KDE => {
get_saved_layouts_kde(&all_keyboards)
}
HYPRLAND => get_saved_layouts_hyprland(&all_keyboards),
GNOME => get_saved_layouts_gnome(&all_keyboards),
KDE => get_saved_layouts_kde(&all_keyboards),
_ => {
let kb = vec![];
kb
Expand All @@ -54,7 +52,7 @@ fn write_to_config(layouts: Vec<KeyboardLayout>) {
if env.contains(GNOME) {
return write_to_config_gnome(&layouts);
}

match env.as_str() {
HYPRLAND => {
write_to_config_hyprland(&layouts);
Expand Down Expand Up @@ -99,52 +97,43 @@ fn get_keyboard_list_backend() -> Vec<KeyboardLayout> {

fn get_max_active_keyboards() -> u32 {
match get_environment().as_str() {
HYPRLAND => { 4 }
GNOME => { 4 }
KDE => { 4 }
_ => { 4 }
HYPRLAND => 4,
GNOME => 4,
KDE => 4,
_ => 4,
}
}

pub fn setup_dbus_interface<T: Send + Sync>(
cross: &mut RwLockWriteGuard<CrossWrapper>,
) -> dbus_crossroads::IfaceToken<T> {
cross.register::<T>(
INTERFACE,
|c: &mut IfaceBuilder<T>| {
c.method_with_cr_async(
"GetKeyboardLayouts",
(),
("layouts", ),
move |mut ctx, _, ()| async move {
ctx.reply(Ok((get_keyboard_list_backend(), )))
},
);
c.method_with_cr_async(
"GetSavedLayouts",
(),
("layouts", ),
move |mut ctx, _, ()| async move {
ctx.reply(Ok((get_saved_layouts(), )))
},
);
c.method_with_cr_async(
"SaveLayoutOrder",
("layouts", ),
(),
move |mut ctx, _, (layouts, ): (Vec<KeyboardLayout>, )| async move {
write_to_config(layouts);
ctx.reply(Ok(()))
},
);
c.method_with_cr_async(
"GetMaxActiveKeyboards",
(),
("max", ),
move |mut ctx, _, ()| async move {
ctx.reply(Ok((get_max_active_keyboards(), )))
},
);
},
)
cross.register::<T>(INTERFACE, |c: &mut IfaceBuilder<T>| {
c.method_with_cr_async(
"GetKeyboardLayouts",
(),
("layouts",),
move |mut ctx, _, ()| async move { ctx.reply(Ok((get_keyboard_list_backend(),))) },
);
c.method_with_cr_async(
"GetSavedLayouts",
(),
("layouts",),
move |mut ctx, _, ()| async move { ctx.reply(Ok((get_saved_layouts(),))) },
);
c.method_with_cr_async(
"SaveLayoutOrder",
("layouts",),
(),
move |mut ctx, _, (layouts,): (Vec<KeyboardLayout>,)| async move {
write_to_config(layouts);
ctx.reply(Ok(()))
},
);
c.method_with_cr_async(
"GetMaxActiveKeyboards",
(),
("max",),
move |mut ctx, _, ()| async move { ctx.reply(Ok((get_max_active_keyboards(),))) },
);
})
}
4 changes: 2 additions & 2 deletions keyboard_plugin/src/const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub const BASE: &str = "org.Xetibo.ReSet.Daemon";
pub const DBUS_PATH: &str = "/org/Xebito/ReSet/Plugins/Keyboard";
pub const INTERFACE: &str = "org.Xetibo.ReSet.Keyboard";


pub const HYPRLAND: &str = "Hyprland";
pub const GNOME: &str = "GNOME";
pub const KDE: &str = "KDE";
pub const KDE: &str = "KDE";

1 change: 0 additions & 1 deletion keyboard_plugin/src/frontend/main_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,3 @@ fn create_action_row(title: String) -> ActionRow {

action_row
}

45 changes: 27 additions & 18 deletions keyboard_plugin/src/frontend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,55 @@ use std::cell::RefCell;
use std::rc::Rc;
use std::time::Duration;

use adw::{ActionRow, gdk, PreferencesGroup};
use adw::prelude::{PreferencesGroupExt, PreferencesRowExt};
use adw::{gdk, ActionRow, PreferencesGroup};
use dbus::blocking::Connection;
use dbus::Error;
use gdk4::ContentProvider;
use glib::{clone, Variant};
use gtk::{prelude::*, DragSource};
use gtk::{Align, EventController, Label, ListBox, WidgetPaintable};
use gtk::{DragSource, prelude::*};

use crate::keyboard_layout::KeyboardLayout;
use crate::r#const::{BASE, DBUS_PATH, INTERFACE};
use crate::utils::get_max_active_keyboards;

pub mod main_page;
pub mod add_layout_page;
pub mod main_page;

pub fn add_listener(keyboard_list: &PreferencesGroup, layout_row: ActionRow) {
let max_keyboards = get_max_active_keyboards();

keyboard_list.add(&layout_row);
let source = DragSource::builder()
.actions(gdk::DragAction::MOVE)
.build();
let source = DragSource::builder().actions(gdk::DragAction::MOVE).build();

source.connect_prepare(clone!(@weak layout_row => @default-return None, move |_, _, _| {
source.connect_prepare(
clone!(@weak layout_row => @default-return None, move |_, _, _| {
let value = glib::Value::from(layout_row);
Some(ContentProvider::for_value(&value))
}));
}),
);

source.connect_drag_begin(clone!(@weak layout_row, @weak keyboard_list => move |value, _| {
source.connect_drag_begin(
clone!(@weak layout_row, @weak keyboard_list => move |value, _| {
layout_row.add_css_class("selectedLanguage");

let paintable = WidgetPaintable::new(Some(&layout_row));
value.set_icon(Some(&paintable), 0, 0);
}));
}),
);

source.connect_drag_end(clone!(@weak layout_row => move |_, _, _| {
layout_row.remove_css_class("selectedLanguage");
}));
layout_row.remove_css_class("selectedLanguage");
}));

let target = gtk::DropTarget::builder()
.actions(gdk::DragAction::MOVE)
.formats(&gdk::ContentFormats::for_type(ActionRow::static_type()))
.build();

target.connect_drop(clone!(@weak keyboard_list => @default-return false, move |target, value, _ , _| {
target.connect_drop(
clone!(@weak keyboard_list => @default-return false, move |target, value, _ , _| {
let selected_row = value.get::<ActionRow>().unwrap();
let droptarget_row = target.widget();
let droptarget_row = droptarget_row.downcast_ref::<ActionRow>().unwrap();
Expand All @@ -60,7 +63,7 @@ pub fn add_listener(keyboard_list: &PreferencesGroup, layout_row: ActionRow) {
let from_to = Variant::from((selected_row.index(), index));
keyboard_list.remove(&selected_row);
listbox.insert(&selected_row, index);

let mut current_row = listbox.first_child();
while let Some(ref widget) = current_row {
let next_row = widget.downcast_ref::<ActionRow>().unwrap();
Expand All @@ -71,22 +74,27 @@ pub fn add_listener(keyboard_list: &PreferencesGroup, layout_row: ActionRow) {
}
current_row = next_row.next_sibling();
}

keyboard_list.activate_action("keyboard.changeorder", Some(&from_to))
.expect("Failed to activate action.");
return true;
}

false
}));
}),
);
layout_row.add_controller(EventController::from(source));
layout_row.add_controller(EventController::from(target));
}

pub fn update_input(user_layouts: &Rc<RefCell<Vec<KeyboardLayout>>>) {
let conn = Connection::new_session().unwrap();
let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(1000));
let res: Result<(), Error> = proxy.method_call(INTERFACE, "SaveLayoutOrder", (user_layouts.borrow().clone(), ));
let res: Result<(), Error> = proxy.method_call(
INTERFACE,
"SaveLayoutOrder",
(user_layouts.borrow().clone(),),
);
res.expect("Failed to save layout order.");
}

Expand All @@ -103,7 +111,8 @@ pub fn create_title() -> Label {
pub fn get_keyboard_list_frontend() -> Vec<KeyboardLayout> {
let conn = Connection::new_session().unwrap();
let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(1000));
let res: Result<(Vec<KeyboardLayout>, ), Error> = proxy.method_call(INTERFACE, "GetKeyboardLayouts", ());
let res: Result<(Vec<KeyboardLayout>,), Error> =
proxy.method_call(INTERFACE, "GetKeyboardLayouts", ());
if res.is_err() {
return Vec::new();
}
Expand Down
Loading

0 comments on commit 982502c

Please sign in to comment.