Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ edition.workspace = true
# general
settings = { path = "../settings" }
anyhow = { version = "1.0.100", features = ["backtrace"] }
libc = "0.2"
clap = { version = "4.5.21", features = ["derive", "env"] }
log = { version = "0.4.22", features = ["kv_unstable_serde"] }
simple_logger = { version = "5.0.0", features = [
Expand Down
89 changes: 89 additions & 0 deletions client/src/lock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use std::env;
use std::fs::{File, TryLockError};
use std::io::{Read, Seek, Write};
use std::path::{Path, PathBuf};

const LOCK_FILE_NAME: &str = "centerpiece.lock";

pub struct LockFile {
_file: File,
}

impl LockFile {
pub fn acquire() -> Option<Self> {
let path = lock_file_path()?;
let mut file = open_lock_file(&path)?;

match file.try_lock() {
Ok(()) => {
write_pid(&mut file);
log::info!("Acquired exclusive lock");
Some(Self { _file: file })
}
Err(TryLockError::WouldBlock) => {
kill_existing_instance(&mut file);
// Blocks until the old process exits and releases the lock
file.lock().ok()?;
write_pid(&mut file);
log::info!("Acquired exclusive lock after killing previous instance");
Some(Self { _file: file })
}
Err(TryLockError::Error(e)) => {
log::error!("Failed to acquire lock: {e}");
None
}
}
}

pub fn cleanup() {
if let Some(path) = lock_file_path() {
let _ = std::fs::remove_file(path);
}
}
}

fn lock_file_path() -> Option<PathBuf> {
let dir = env::var("XDG_RUNTIME_DIR").ok();
if dir.is_none() {
log::warn!("XDG_RUNTIME_DIR not set, running without file lock");
}
Some(Path::new(&dir?).join(LOCK_FILE_NAME))
}

fn open_lock_file(path: &Path) -> Option<File> {
File::options()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(path)
.map_err(|e| log::error!("Failed to open lock file: {e}"))
.ok()
}

fn write_pid(file: &mut File) {
let _ = file.set_len(0);
let _ = file.seek(std::io::SeekFrom::Start(0));
let _ = write!(file, "{}", std::process::id());
let _ = file.flush();
}

fn read_pid(file: &mut File) -> Option<u32> {
let mut contents = String::new();
file.seek(std::io::SeekFrom::Start(0)).ok()?;
file.read_to_string(&mut contents).ok()?;
contents.trim().parse().ok()
}

fn kill_existing_instance(file: &mut File) {
let Some(pid) = read_pid(file) else {
log::warn!("Lock held but no PID in lock file");
return;
};
log::info!("Killing existing instance with PID {pid}");
// SAFETY: sending SIGTERM to a known PID
unsafe {
libc::kill(pid as i32, libc::SIGTERM);
}
}

16 changes: 12 additions & 4 deletions client/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::process::exit;

use clap::Parser;
use eframe::egui::{self, Separator};

mod component;
mod lock;
mod model;
mod plugin;

Expand All @@ -16,6 +15,8 @@ pub fn main() {

simple_logger::init_with_level(log::Level::Info).unwrap();

let _lock = lock::LockFile::acquire();

eframe::run_native(
"centerpiece",
settings(),
Expand Down Expand Up @@ -300,6 +301,11 @@ impl Centerpiece {
}
}

fn exit() -> ! {
lock::LockFile::cleanup();
std::process::exit(0);
}

fn handle_input(&mut self, ctx: &eframe::egui::Context) {
if ctx.input(|i| i.key_pressed(eframe::egui::Key::ArrowUp)) {
self.select_previous_entry();
Expand All @@ -311,7 +317,7 @@ impl Centerpiece {
self.activate_selected_entry();
}
if ctx.input(|i| i.key_pressed(eframe::egui::Key::Escape)) {
exit(0);
Self::exit();
}

if ctx.input(|i| i.modifiers.ctrl && i.key_pressed(eframe::egui::Key::J)) {
Expand All @@ -337,7 +343,9 @@ impl Centerpiece {
self.update_entries(plugin_id, entries)
}

Message::Exit => exit(0),
Message::Exit => {
Self::exit();
}
}
}
}
Expand Down