Skip to content

Commit

Permalink
Refactor shared structs into common crate
Browse files Browse the repository at this point in the history
  • Loading branch information
arusahni committed Dec 29, 2023
1 parent d3543fb commit 24e54fe
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use directories::ProjectDirs;
use serde::{Deserialize, Serialize};
use tracing::debug;

use crate::{errors::Error, utils::abort, structs::WindowGeometry};
use crate::{errors::Error, structs::WindowGeometry, utils::abort};

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
Expand Down
21 changes: 8 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use directories::ProjectDirs;
use tracing::{debug, error, info, trace, warn};

use errors::Error;
use structs::{Instance, WindowGeometry};
use structs::{Context, Instance, WindowGeometry, WindowMatcher};
use tracing_subscriber::{
fmt::writer::MakeWriterExt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter,
};
Expand Down Expand Up @@ -111,7 +111,7 @@ fn handle_socket_messages(listener: UnixListener, tx: mpsc::Sender<String>) -> R
}

/// Find and position the window
fn block_for_window(matcher: &x11::WindowMatcher, instance: &Instance) -> u32 {
fn block_for_window(matcher: &WindowMatcher, instance: &Instance) -> u32 {
trace!("blocking for window {:?}", matcher);
let mut count = 0;
let start = SystemTime::now();
Expand Down Expand Up @@ -140,11 +140,6 @@ fn block_for_window(matcher: &x11::WindowMatcher, instance: &Instance) -> u32 {
}
}

pub(crate) struct Context {
pub matcher: x11::WindowMatcher,
pub window_id: Option<u32>,
}

pub(crate) fn program_thread(
rx: mpsc::Receiver<String>,
instance: Instance,
Expand All @@ -158,8 +153,8 @@ pub(crate) fn program_thread(
info!("[{}] Started PID: {}", instance.name, program.id());
{
let write_ctx = &mut ctx.write().unwrap();
if matches!(write_ctx.matcher, x11::WindowMatcher::ProcessId(_)) {
write_ctx.matcher = x11::WindowMatcher::ProcessId(Some(program.id()));
if matches!(write_ctx.matcher, WindowMatcher::ProcessId(_)) {
write_ctx.matcher = WindowMatcher::ProcessId(Some(program.id()));
trace!("[{}] Set a new PID {}", instance.name, program.id());
}
write_ctx.window_id = Some(block_for_window(&write_ctx.matcher, &instance));
Expand Down Expand Up @@ -196,9 +191,9 @@ pub(crate) fn program_thread(
.arg(instance.command.clone())
.spawn()
.expect("failed to start");
if matches!(write_ctx.matcher, x11::WindowMatcher::ProcessId(_)) {
if matches!(write_ctx.matcher, WindowMatcher::ProcessId(_)) {
trace!("[{}] Setting new pid {}", instance.name, program.id());
write_ctx.matcher = x11::WindowMatcher::ProcessId(Some(program.id()));
write_ctx.matcher = WindowMatcher::ProcessId(Some(program.id()));
}
write_ctx.window_id = Some(block_for_window(&write_ctx.matcher, &instance));
} else {
Expand Down Expand Up @@ -323,13 +318,13 @@ fn main() -> Result<(), Error> {
name: instance_name.clone(),
command: instance.command.clone(),
matcher: match instance.matcher {
config::WindowMatcher::Class => x11::WindowMatcher::WmClass(
config::WindowMatcher::Class => WindowMatcher::WmClass(
instance
.class_name
.clone()
.unwrap_or_else(|| abort("'class_name' must be specified")),
),
config::WindowMatcher::Process => x11::WindowMatcher::ProcessId(None),
config::WindowMatcher::Process => WindowMatcher::ProcessId(None),
},
window_delay: instance.window_delay_ms.or(Some(100)),
geometry: instance.geometry.clone().unwrap_or_else(|| WindowGeometry {
Expand Down
15 changes: 12 additions & 3 deletions src/structs.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use serde::{Deserialize, Serialize};

use crate::x11;

#[derive(Debug, Clone)]
pub(crate) struct Instance {
pub(crate) name: String,
pub(crate) command: String,
pub(crate) matcher: x11::WindowMatcher,
pub(crate) matcher: WindowMatcher,
pub(crate) window_delay: Option<u64>,
pub(crate) geometry: WindowGeometry,
}
Expand Down Expand Up @@ -47,3 +45,14 @@ impl WindowGeometry {
(width, height)
}
}

pub(crate) struct Context {
pub matcher: WindowMatcher,
pub window_id: Option<u32>,
}

#[derive(Debug, Clone)]
pub(crate) enum WindowMatcher {
ProcessId(Option<u32>),
WmClass(String),
}
11 changes: 4 additions & 7 deletions src/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use x11rb::{
wrapper::ConnectionExt as WrapperConnectionExt,
};

use crate::{errors::Error, structs::Instance};
use crate::{
errors::Error,
structs::{Instance, WindowMatcher},
};

x11rb::atom_manager! {
pub(crate) Atoms:
Expand Down Expand Up @@ -57,12 +60,6 @@ pub(crate) fn get_active_window(
Ok(response.to_owned().value32().unwrap().next().unwrap())
}

#[derive(Debug, Clone)]
pub(crate) enum WindowMatcher {
ProcessId(Option<u32>),
WmClass(String),
}

fn query_windows(connection: &x11rb::rust_connection::RustConnection, root: u32) -> Vec<u32> {
let tree = connection
.query_tree(root)
Expand Down

0 comments on commit 24e54fe

Please sign in to comment.