Skip to content

Commit 8f458aa

Browse files
authored
fix: refined swhks ipc.rs for hash calculation and finding buffer byte (#306)
* fix: unblock all rfkill softblocks to avoid radio devs turning off This commit attempts at fixing the major issue of rfkill not being properly turned off while creating the swhkd virtual device. Changes made: 1. Added RfkillEvent struct that stores the rfkill details and status required with documentation 2. Added two new macros with `ioctl_readwrite` to ensure that we can query the rfkill status and modify properly 3. New function `unblock_rfkill` that unblocks all soft blocks for any device 4. Called `unblock_rfkill` function in `create_uinput_switches_device` that ensures that rfkill is NOT blocked and complies with rfkill-input being disabled This should ensure that rfkill does not react when virtual device is being created. * fix: clean up code in swhks ipc.rs 1. Calculate hash function now borrows the string instead of taking ownership 2. Used match to check for buffer byte instead of if statement 3. Added more debug logging and error handling * chore: fallback commit to parent ca82393 Remove updated logic for uinput.rs as it does not work * fix: use is_some_and instead of map_for in daemon.rs replaced `map_for` with `is_some_and` as it is more efficient in this case (simple bool ops)
1 parent e972f55 commit 8f458aa

File tree

2 files changed

+51
-27
lines changed

2 files changed

+51
-27
lines changed

swhkd/src/daemon.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
543543
}
544544

545545
pub fn check_device_is_keyboard(device: &Device) -> bool {
546-
if device.supported_keys().map_or(false, |keys| keys.contains(Key::KEY_ENTER)) {
546+
if device.supported_keys().is_some_and(|keys| keys.contains(Key::KEY_ENTER)) {
547547
if device.name() == Some("swhkd virtual output") {
548548
return false;
549549
}

swhks/src/ipc.rs

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ fn get_env() -> Result<String, Box<dyn std::error::Error>> {
1717
/// Calculates a simple hash of the string
1818
/// Uses the DefaultHasher from the std::hash module which is not a cryptographically secure hash,
1919
/// however, it is good enough for our use case.
20-
pub fn calculate_hash(t: String) -> u64 {
20+
pub fn calculate_hash(t: &str) -> u64 {
2121
let mut hasher = DefaultHasher::new();
2222
t.hash(&mut hasher);
2323
hasher.finish()
2424
}
2525

2626
pub fn server_loop(sock_file_path: &str) -> std::io::Result<()> {
27-
let mut prev_hash = calculate_hash(String::new());
27+
let mut prev_hash = calculate_hash("");
2828

2929
let listener = UnixListener::bind(sock_file_path)?;
3030
// Init a buffer to read the incoming message
@@ -34,36 +34,60 @@ pub fn server_loop(sock_file_path: &str) -> std::io::Result<()> {
3434
for stream in listener.incoming() {
3535
match stream {
3636
Ok(mut stream) => {
37-
stream.read_exact(&mut buff)?;
38-
// If the buffer is [1] then it is a VERIFY message
39-
// the hash of the environment variables is sent back to the client
40-
// then the stream is flushed and the loop continues
41-
if buff == [1] {
42-
log::debug!("Received VERIFY message from swhkd");
43-
let _ = stream.write_all(prev_hash.to_string().as_bytes());
44-
log::debug!("Sent hash to swhkd");
45-
stream.flush()?;
37+
if let Err(e) = stream.read_exact(&mut buff) {
38+
log::error!("Failed to read from stream: {}", e);
4639
continue;
4740
}
48-
// If the buffer is [2] then it is a GET message
49-
// the environment variables are sent back to the client
50-
// then the stream is flushed and the loop continues
51-
if buff == [2] {
52-
log::debug!("Received GET message from swhkd");
53-
let env = get_env().unwrap();
54-
if prev_hash == calculate_hash(env.clone()) {
55-
log::debug!("No changes in environment variables");
56-
} else {
57-
log::debug!("Changes in environment variables");
41+
42+
match buff[0] {
43+
1 => {
44+
// If the buffer is [1] then it is a VERIFY message
45+
// the hash of the environment variables is sent back to the client
46+
// then the stream is flushed and the loop continues
47+
log::debug!("Received VERIFY request from swhkd");
48+
if let Err(e) = stream.write_all(prev_hash.to_string().as_bytes()) {
49+
log::error!("Failed to write hash to stream: {}", e);
50+
} else {
51+
log::debug!("Sent hash to swhkd");
52+
}
5853
}
59-
prev_hash = calculate_hash(env.clone());
60-
let _ = stream.write_all(env.as_bytes());
61-
stream.flush()?;
62-
continue;
54+
2 => {
55+
// If the buffer is [2] then it is a GET message
56+
// the environment variables are sent back to the client
57+
// then the stream is flushed and the loop continues
58+
log::debug!("Received GET request from swhkd");
59+
60+
match get_env() {
61+
Ok(env) => {
62+
let new_hash = calculate_hash(&env);
63+
if prev_hash == new_hash {
64+
log::debug!("No changes in environment variables");
65+
} else {
66+
log::debug!("Environment variables updated");
67+
prev_hash = new_hash;
68+
}
69+
70+
if let Err(e) = stream.write_all(env.as_bytes()) {
71+
log::error!("Failed to send environment variables: {}", e);
72+
}
73+
}
74+
Err(e) => {
75+
log::error!("Failed to retrieve environment variables: {}", e);
76+
let _ = stream.write_all(b"ERROR: Unable to fetch environment");
77+
}
78+
}
79+
}
80+
_ => {
81+
log::warn!("Received unknown request: {}", buff[0]);
82+
}
83+
}
84+
85+
if let Err(e) = stream.flush() {
86+
log::error!("Failed to flush stream: {}", e);
6387
}
6488
}
6589
Err(e) => {
66-
log::error!("Error: {}", e);
90+
log::error!("Error handling connection: {}", e);
6791
break;
6892
}
6993
}

0 commit comments

Comments
 (0)