Skip to content

Commit 654a3b7

Browse files
committed
Add proper quit
1 parent f0a90e9 commit 654a3b7

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/vonal_daemon/main.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1+
#![feature(panic_info_message)]
12
use glutin::{
23
dpi::PhysicalSize,
34
event::{Event, StartCause},
45
event_loop::EventLoop,
56
};
6-
use std::thread;
7-
use std::{fs, io::Read, os::unix::net::UnixListener, path::Path, sync::mpsc, time::Instant};
7+
use std::{
8+
fs, io::Read, os::unix::net::UnixListener, path::Path, process, sync::mpsc, time::Instant,
9+
};
10+
use std::{os::unix::net::UnixStream, thread};
811

912
mod app;
1013
#[path = "../common.rs"]
1114
mod common;
1215
mod plugins;
1316

1417
fn main() {
18+
// Set less distracting panic message
19+
std::panic::set_hook(Box::new(|info| match info.message() {
20+
Some(message) => println!("Error: {}", message),
21+
None => println!("{}", info),
22+
}));
23+
1524
let (tx, rx) = mpsc::channel();
1625
let socket_thread = thread::spawn(move || {
1726
start_socket(&tx);
@@ -23,6 +32,11 @@ fn main() {
2332
fn start_socket(tx: &mpsc::Sender<UserEvent>) {
2433
let socket = Path::new(common::SOCKET_PATH);
2534

35+
if UnixStream::connect(&socket).is_ok() {
36+
tx.send(UserEvent::Quit).unwrap();
37+
panic!("One daemon is already listening.")
38+
}
39+
2640
// Delete old socket if necessary
2741
if socket.exists() {
2842
fs::remove_file(&socket).unwrap();
@@ -123,6 +137,7 @@ fn start_gui(rx: mpsc::Receiver<UserEvent>) {
123137
}
124138
command => println!("Got command: {:?}", command),
125139
},
140+
Event::UserEvent(UserEvent::Quit) => process::exit(0),
126141
_ => {}
127142
});
128143
}
@@ -204,5 +219,6 @@ fn create_display(
204219

205220
#[derive(Debug)]
206221
enum UserEvent {
222+
Quit,
207223
CliCommand(String),
208224
}

0 commit comments

Comments
 (0)