Skip to content

Commit

Permalink
Use cosmic-config for input configuration; allow dynamic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ids1024 committed Aug 1, 2023
1 parent e0aecf9 commit 0fea3e4
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 60 deletions.
31 changes: 17 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ ron = "0.7"
libsystemd = { version = "0.5", optional = true }
wayland-backend = "0.1.0"
wayland-scanner = "0.30.0"
cosmic-config = { git = "https://github.com/pop-os/libcosmic/", rev = "4895b0c", features = ["calloop"] }
cosmic-protocols = { git = "https://github.com/pop-os/cosmic-protocols", branch = "main", default-features = false, features = ["server"] }
libcosmic = { git = "https://github.com/pop-os/libcosmic/", rev = "42d7baf", default-features = false }
iced_tiny_skia = { git = "https://github.com/pop-os/libcosmic/", rev = "42d7baf" }
libcosmic = { git = "https://github.com/pop-os/libcosmic/", rev = "4895b0c", default-features = false }
iced_tiny_skia = { git = "https://github.com/pop-os/libcosmic/", rev = "4895b0c" }
tiny-skia = "0.9"
ordered-float = "3.0"
glow = "0.11.2"
Expand Down
13 changes: 11 additions & 2 deletions src/backend/kms/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ use smithay::{
control::{connector, crtc, Device as ControlDevice, ModeTypeFlags},
Device as _,
},
input::Libinput,
input::{self, Libinput},
nix::{fcntl::OFlag, sys::stat::dev_t},
wayland_protocols::wp::{
linux_dmabuf::zv1::server::zwp_linux_dmabuf_feedback_v1,
Expand Down Expand Up @@ -97,6 +97,7 @@ const MIN_RENDER_TIME: Duration = Duration::from_millis(3);
#[derive(Debug)]
pub struct KmsState {
devices: HashMap<DrmNode, Device>,
pub input_devices: HashMap<String, input::Device>,
pub api: GpuManager<GbmGlesBackend<GlowRenderer>>,
pub primary: DrmNode,
session: LibSeatSession,
Expand Down Expand Up @@ -171,8 +172,15 @@ pub fn init_backend(
let libinput_event_source = event_loop
.handle()
.insert_source(libinput_backend, move |mut event, _, data| {
if let &mut InputEvent::DeviceAdded { ref mut device } = &mut event {
if let InputEvent::DeviceAdded { ref mut device } = &mut event {
data.state.common.config.read_device(device);
data.state
.backend
.kms()
.input_devices
.insert(device.name().into(), device.clone());
} else if let InputEvent::DeviceRemoved { device } = &event {
data.state.backend.kms().input_devices.remove(device.name());
}
data.state.process_input_event(event, true);
for output in data.state.common.shell.outputs() {
Expand Down Expand Up @@ -362,6 +370,7 @@ pub fn init_backend(
primary,
session,
devices: HashMap::new(),
input_devices: HashMap::new(),
});

// Create relative pointer global
Expand Down
103 changes: 62 additions & 41 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
state::{BackendData, Data, State},
wayland::protocols::output_configuration::OutputConfigurationState,
};
use cosmic_config::{ConfigGet, ConfigSet};
use serde::{Deserialize, Serialize};
use smithay::input::Seat;
pub use smithay::{
Expand Down Expand Up @@ -34,6 +35,9 @@ pub use self::types::*;
pub struct Config {
pub static_conf: StaticConfig,
pub dynamic_conf: DynamicConfig,
pub config: cosmic_config::Config,
pub xkb: XkbConfig,
pub input_devices: HashMap<String, InputConfig>,
}

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -65,7 +69,6 @@ pub enum WorkspaceLayout {
#[derive(Debug)]
pub struct DynamicConfig {
outputs: (Option<PathBuf>, OutputsConfig),
inputs: (Option<PathBuf>, InputsConfig),
}

#[derive(Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -152,18 +155,22 @@ impl OutputConfig {
}
}

#[derive(Debug, Deserialize, Serialize)]
pub struct InputsConfig {
xkb: XkbConfig,
devices: HashMap<String, InputConfig>,
}

impl Config {
pub fn load() -> Config {
pub fn load(loop_handle: &LoopHandle<'_, Data>) -> Config {
let config = cosmic_config::Config::new("com.system76.CosmicComp", 1).unwrap();
let source = cosmic_config::calloop::ConfigWatchSource::new(&config).unwrap();
loop_handle
.insert_source(source, |(config, keys), (), shared_data| {
config_changed(config, keys, &mut shared_data.state);
})
.expect("Failed to add cosmic-config to the event loop");
let xdg = xdg::BaseDirectories::new().ok();
Config {
static_conf: Self::load_static(xdg.as_ref()),
dynamic_conf: Self::load_dynamic(xdg.as_ref()),
xkb: get_config(&config, "xkb-config"),
input_devices: get_config(&config, "input-devices"),
config,
}
}

Expand Down Expand Up @@ -218,12 +225,8 @@ impl Config {
xdg.and_then(|base| base.place_state_file("cosmic-comp/outputs.ron").ok());
let outputs = Self::load_outputs(&output_path);

let input_path = xdg.and_then(|base| base.place_state_file("cosmic-comp/inputs.ron").ok());
let inputs = Self::load_inputs(&input_path);

DynamicConfig {
outputs: (output_path, outputs),
inputs: (input_path, inputs),
}
}

Expand All @@ -247,27 +250,6 @@ impl Config {
}
}

fn load_inputs(path: &Option<PathBuf>) -> InputsConfig {
if let Some(path) = path.as_ref() {
if path.exists() {
match ron::de::from_reader(OpenOptions::new().read(true).open(path).unwrap()) {
Ok(config) => return config,
Err(err) => {
warn!(?err, "Failed to read input_config, resetting..");
if let Err(err) = std::fs::remove_file(path) {
error!(?err, "Failed to remove input_config.");
}
}
};
}
}

InputsConfig {
xkb: XkbConfig::default(),
devices: HashMap::new(),
}
}

pub fn read_outputs(
&mut self,
output_state: &mut OutputConfigurationState<State>,
Expand Down Expand Up @@ -422,17 +404,23 @@ impl Config {
}

pub fn xkb_config(&self) -> XkbConfig {
self.dynamic_conf.inputs().xkb.clone()
self.xkb.clone()
}

pub fn read_device(&mut self, device: &mut InputDevice) {
use std::collections::hash_map::Entry;

let mut inputs = self.dynamic_conf.inputs_mut();
match inputs.devices.entry(device.name().into()) {
let mut config_changed = false;
match self.input_devices.entry(device.name().into()) {
Entry::Occupied(entry) => entry.get().update_device(device),
Entry::Vacant(entry) => {
entry.insert(InputConfig::for_device(device));
config_changed = true;
}
}
if config_changed {
if let Err(err) = self.config.set("input-devices", &self.input_devices) {
error!(?err, "Failed to write config 'input-devices'");
}
}
}
Expand Down Expand Up @@ -483,12 +471,45 @@ impl DynamicConfig {
pub fn outputs_mut<'a>(&'a mut self) -> PersistenceGuard<'a, OutputsConfig> {
PersistenceGuard(self.outputs.0.clone(), &mut self.outputs.1)
}
}

pub fn inputs(&self) -> &InputsConfig {
&self.inputs.1
}
fn get_config<T: Default + serde::de::DeserializeOwned>(
config: &cosmic_config::Config,
key: &str,
) -> T {
config.get(key).unwrap_or_else(|err| {
error!(?err, "Failed to read config '{}'", key);
T::default()
})
}

pub fn inputs_mut<'a>(&'a mut self) -> PersistenceGuard<'a, InputsConfig> {
PersistenceGuard(self.inputs.0.clone(), &mut self.inputs.1)
fn config_changed(config: cosmic_config::Config, keys: Vec<String>, state: &mut State) {
for key in &keys {
match key.as_str() {
"xkb-config" => {
let value = get_config::<XkbConfig>(&config, "xkb-config");
for seat in state.common.seats().cloned().collect::<Vec<_>>().iter() {
if let Some(keyboard) = seat.get_keyboard() {
if let Err(err) = keyboard.set_xkb_config(state, (&value).into()) {
error!(?err, "Failed to load provided xkb config");
// TODO Revert to default?
}
}
}
state.common.config.xkb = value;
}
"input-devices" => {
let value = get_config::<HashMap<String, InputConfig>>(&config, "input-devices");
if let BackendData::Kms(ref mut kms_state) = &mut state.backend {
for (name, device) in kms_state.input_devices.iter_mut() {
if let Some(input_config) = value.get(name) {
input_config.update_device(device);
}
}
}
state.common.config.input_devices = value;
}
_ => {}
}
}
}
2 changes: 1 addition & 1 deletion src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ impl State {
.unwrap();

let clock = Clock::new().expect("Failed to initialize clock");
let config = Config::load();
let config = Config::load(&handle);
let compositor_state = CompositorState::new::<Self>(dh);
let data_device_state = DataDeviceState::new::<Self>(dh);
let dmabuf_state = DmabufState::new();
Expand Down

0 comments on commit 0fea3e4

Please sign in to comment.