Skip to content

Commit 929e8ce

Browse files
committed
refactor: Reorganize macos_permissions into module structure
- Split macos_permissions.rs into organized module structure: - mod.rs: Public API and common types - mac.rs: macOS-specific implementations with FFI - stub.rs: Non-macOS stub implementations - Improves code organization and maintainability - Cleaner separation of platform-specific logic - Same public API, better internal structure
1 parent c11172b commit 929e8ce

File tree

4 files changed

+100
-134
lines changed

4 files changed

+100
-134
lines changed

src/macos_permissions.rs

Lines changed: 0 additions & 134 deletions
This file was deleted.

src/macos_permissions/mac.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use kanata_tcp_protocol::PermissionState;
2+
3+
type Boolean = u8;
4+
type CFDictionaryRef = *const core::ffi::c_void;
5+
6+
#[repr(C)]
7+
pub enum IOHIDRequestType {
8+
IOHIDRequestTypeListenEvent = 0,
9+
}
10+
11+
#[link(name = "ApplicationServices", kind = "framework")]
12+
unsafe extern "C" {
13+
fn AXIsProcessTrustedWithOptions(options: CFDictionaryRef) -> Boolean;
14+
}
15+
16+
#[link(name = "IOKit", kind = "framework")]
17+
unsafe extern "C" {
18+
fn IOHIDCheckAccess(request_type: IOHIDRequestType) -> Boolean;
19+
}
20+
21+
#[derive(Debug)]
22+
pub struct MacosPermissionStatus {
23+
pub accessibility: PermissionState,
24+
pub input_monitoring: PermissionState,
25+
}
26+
27+
pub fn check_macos_permissions() -> MacosPermissionStatus {
28+
let accessibility = unsafe {
29+
let trusted = AXIsProcessTrustedWithOptions(core::ptr::null());
30+
if trusted == 1 {
31+
PermissionState::Granted
32+
} else {
33+
PermissionState::Denied
34+
}
35+
};
36+
37+
let input_monitoring = unsafe {
38+
let granted = IOHIDCheckAccess(IOHIDRequestType::IOHIDRequestTypeListenEvent);
39+
if granted == 1 {
40+
PermissionState::Granted
41+
} else {
42+
PermissionState::Denied
43+
}
44+
};
45+
46+
MacosPermissionStatus {
47+
accessibility,
48+
input_monitoring,
49+
}
50+
}
51+
52+
pub fn restart_process() -> Result<(), String> {
53+
use std::ffi::CString;
54+
55+
let args: Vec<_> = std::env::args().collect();
56+
if args.is_empty() {
57+
return Err("No arguments found".to_string());
58+
}
59+
60+
let c_args: Result<Vec<CString>, _> = args.iter().map(|s| CString::new(s.as_str())).collect();
61+
let c_args = match c_args {
62+
Ok(v) => v,
63+
Err(e) => return Err(format!("Invalid argument: {}", e)),
64+
};
65+
66+
let mut c_ptrs: Vec<*const libc::c_char> = c_args.iter().map(|s| s.as_ptr()).collect();
67+
c_ptrs.push(core::ptr::null());
68+
69+
unsafe {
70+
libc::execv(c_ptrs[0], c_ptrs.as_ptr());
71+
Err(format!("execv failed: {}", std::io::Error::last_os_error()))
72+
}
73+
}

src/macos_permissions/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#[cfg(target_os = "macos")]
2+
mod mac;
3+
#[cfg(not(target_os = "macos"))]
4+
mod stub;
5+
6+
#[cfg(target_os = "macos")]
7+
pub use mac::*;
8+
#[cfg(not(target_os = "macos"))]
9+
pub use stub::*;

src/macos_permissions/stub.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use kanata_tcp_protocol::PermissionState;
2+
3+
#[derive(Debug)]
4+
pub struct MacosPermissionStatus {
5+
pub accessibility: PermissionState,
6+
pub input_monitoring: PermissionState,
7+
}
8+
9+
pub fn check_macos_permissions() -> MacosPermissionStatus {
10+
MacosPermissionStatus {
11+
accessibility: PermissionState::NotApplicable,
12+
input_monitoring: PermissionState::NotApplicable,
13+
}
14+
}
15+
16+
pub fn restart_process() -> Result<(), String> {
17+
Err("Process restart is only supported on macOS".to_string())
18+
}

0 commit comments

Comments
 (0)