Debian Codegraph @

src/backend/xdg_desktop_portal/portal/ffi.rs

raw
1 #![allow(unused)]
2
3 use std::{
4 ffi::CStr,
5 os::raw::{c_char, c_int, c_uint, c_void},
6 ptr::NonNull,
7 sync::OnceLock,
8 };
9
10 pub type DBusConnection = c_void;
11 pub type DBusMessage = c_void;
12
13 #[repr(C)]
14 #[derive(Debug, PartialEq, Copy, Clone)]
15 /// System or Session bus
16 pub enum DBusBusType {
17 Session = 0,
18 System = 1,
19 Starter = 2,
20 }
21
22 pub const DBUS_TYPE_ARRAY: c_int = 'a' as c_int;
23 pub const DBUS_TYPE_VARIANT: c_int = 'v' as c_int;
24 pub const DBUS_TYPE_BOOLEAN: c_int = 'b' as c_int;
25 pub const DBUS_TYPE_INVALID: c_int = 0;
26 pub const DBUS_TYPE_STRING: c_int = 's' as c_int;
27 pub const DBUS_TYPE_DICT_ENTRY: c_int = 'e' as c_int;
28 pub const DBUS_TYPE_BYTE: c_int = 'y' as c_int;
29 pub const DBUS_TYPE_INT16: c_int = 'n' as c_int;
30 pub const DBUS_TYPE_UINT16: c_int = 'q' as c_int;
31 pub const DBUS_TYPE_INT32: c_int = 'i' as c_int;
32 pub const DBUS_TYPE_UINT32: c_int = 'u' as c_int;
33 pub const DBUS_TYPE_INT64: c_int = 'x' as c_int;
34 pub const DBUS_TYPE_UINT64: c_int = 't' as c_int;
35 pub const DBUS_TYPE_DOUBLE: c_int = 'd' as c_int;
36 pub const DBUS_TYPE_UNIX_FD: c_int = 'h' as c_int;
37 pub const DBUS_TYPE_STRUCT: c_int = 'r' as c_int;
38 pub const DBUS_TYPE_OBJECT_PATH: c_int = 'o' as c_int;
39 pub const DBUS_TYPE_SIGNATURE: c_int = 'g' as c_int;
40
41 #[repr(C)]
42 pub struct DBusError {
43 pub name: *const c_char,
44 pub message: *const c_char,
45 pub dummy: c_uint,
46 pub padding1: *const c_void,
47 }
48
49 #[repr(C)]
50 #[derive(Copy, Clone)]
51 pub struct DBusMessageIter {
52 pub dummy1: *mut c_void,
53 pub dummy2: *mut c_void,
54 pub dummy3: u32,
55 pub dummy4: c_int,
56 pub dummy5: c_int,
57 pub dummy6: c_int,
58 pub dummy7: c_int,
59 pub dummy8: c_int,
60 pub dummy9: c_int,
61 pub dummy10: c_int,
62 pub dummy11: c_int,
63 pub pad1: c_int,
64 pub pad2: c_int,
65 // Here would have been padding; because pad3 is a 8 byte aligned pointer (on amd64).
66 // Rust reserves the right not to copy the padding when cloning a struct,
67 // but the D-Bus library uses this part of the struct too.
68 // Hence, add a field as big as the padding to ensure Rust copies all data.
69 pub pad2_added_by_rust: c_int,
70 pub pad3: *mut c_void,
71 }
72
73 pub struct Libdbus {
74 _handle: Liblary,
75 // Bus Connections
76 pub dbus_bus_get_private:
77 unsafe extern "C" fn(DBusBusType, *mut DBusError) -> *mut DBusConnection,
78 pub dbus_bus_get_unique_name: unsafe extern "C" fn(*mut DBusConnection) -> *const c_char,
79
80 pub dbus_bus_add_match:
81 unsafe extern "C" fn(*mut DBusConnection, *const c_char, *mut DBusError),
82
83 // Connection Management
84 pub dbus_connection_close: unsafe extern "C" fn(*mut DBusConnection),
85 pub dbus_connection_flush: unsafe extern "C" fn(*mut DBusConnection),
86 pub dbus_connection_send_with_reply_and_block: unsafe extern "C" fn(
87 *mut DBusConnection,
88 *mut DBusMessage,
89 c_int,
90 *mut DBusError,
91 ) -> *mut DBusMessage,
92 pub dbus_connection_read_write: unsafe extern "C" fn(*mut DBusConnection, c_int) -> u32,
93 pub dbus_connection_pop_message: unsafe extern "C" fn(*mut DBusConnection) -> *mut DBusMessage,
94
95 // Errors
96 pub dbus_error_init: unsafe extern "C" fn(*mut DBusError),
97 pub dbus_error_free: unsafe extern "C" fn(*mut DBusError),
98
99 // Messages
100 pub dbus_message_new_method_call: unsafe extern "C" fn(
101 *const c_char,
102 *const c_char,
103 *const c_char,
104 *const c_char,
105 ) -> *mut DBusMessage,
106 pub dbus_message_unref: unsafe extern "C" fn(*mut DBusMessage),
107 pub dbus_message_is_signal:
108 unsafe extern "C" fn(*mut DBusMessage, *const c_char, *const c_char) -> u32,
109 pub dbus_message_get_path: unsafe extern "C" fn(*mut DBusMessage) -> *const c_char,
110
111 // Message Iterators
112 pub dbus_message_iter_append_basic:
113 unsafe extern "C" fn(*mut DBusMessageIter, c_int, *const c_void) -> u32,
114 pub dbus_message_iter_init: unsafe extern "C" fn(*mut DBusMessage, *mut DBusMessageIter) -> u32,
115 pub dbus_message_iter_init_append: unsafe extern "C" fn(*mut DBusMessage, *mut DBusMessageIter),
116 pub dbus_message_iter_get_arg_type: unsafe extern "C" fn(*mut DBusMessageIter) -> c_int,
117 pub dbus_message_iter_get_basic: unsafe extern "C" fn(*mut DBusMessageIter, *mut c_void),
118 pub dbus_message_iter_next: unsafe extern "C" fn(*mut DBusMessageIter) -> u32,
119 pub dbus_message_iter_recurse: unsafe extern "C" fn(*mut DBusMessageIter, *mut DBusMessageIter),
120 pub dbus_message_iter_open_container: unsafe extern "C" fn(
121 *mut DBusMessageIter,
122 c_int,
123 *const c_char,
124 *mut DBusMessageIter,
125 ) -> u32,
126 pub dbus_message_iter_close_container:
127 unsafe extern "C" fn(*mut DBusMessageIter, *mut DBusMessageIter) -> u32,
128 }
129
130 unsafe impl Send for Libdbus {}
131 unsafe impl Sync for Libdbus {}
132
133 macro_rules! load_symbols {
134 ($handle:expr, [ $($name:ident),* $(,)? ]) => {
135 #[allow(clippy::missing_transmute_annotations)]
136 Self {
137 $(
138 $name: unsafe {
139 // 1. Stringify the identifier name
140 // 2. Append null terminator
141 // 3. Convert to CStr (equivalent to c"name")
142 // 4. Load symbol and transmute
143 std::mem::transmute($handle.symbol(
144 std::ffi::CStr::from_bytes_with_nul_unchecked(
145 concat!(stringify!($name), "\0").as_bytes()
146 )
147 )?)
148 },
149 )*
150 _handle: $handle,
151 }
152 };
153 }
154
155 static LIB: OnceLock<Libdbus> = OnceLock::new();
156
157 impl Libdbus {
158 fn new(mut handle: Liblary) -> Option<Self> {
159 Some(load_symbols!(
160 handle,
161 [
162 // Bus Connections
163 dbus_bus_get_private,
164 dbus_bus_get_unique_name,
165 dbus_bus_add_match,
166 // Connection Management
167 dbus_connection_close,
168 dbus_connection_flush,
169 dbus_connection_send_with_reply_and_block,
170 dbus_connection_read_write,
171 dbus_connection_pop_message,
172 // Errors
173 dbus_error_init,
174 dbus_error_free,
175 // Messages
176 dbus_message_new_method_call,
177 dbus_message_unref,
178 dbus_message_is_signal,
179 dbus_message_get_path,
180 // Message Iterators
181 dbus_message_iter_append_basic,
182 dbus_message_iter_init,
183 dbus_message_iter_init_append,
184 dbus_message_iter_get_arg_type,
185 dbus_message_iter_get_basic,
186 dbus_message_iter_next,
187 dbus_message_iter_recurse,
188 dbus_message_iter_open_container,
189 dbus_message_iter_close_container,
190 ]
191 ))
192 }
193
194 pub fn open_libdbus() -> Option<&'static Self> {
195 if let Some(lib) = LIB.get() {
196 return Some(lib);
197 }
198
199 let lib = Liblary::open(c"libdbus-1.so.3").or_else(|| Liblary::open(c"libdbus-1.so"))?;
200
201 let lib = Self::new(lib)?;
202 LIB.set(lib).ok();
203 LIB.get()
204 }
205
206 pub fn get() -> &'static Self {
207 LIB.get().unwrap()
208 }
209 }
210
211 struct Liblary(pub NonNull<c_void>);
212 unsafe impl Send for Liblary {}
213 unsafe impl Sync for Liblary {}
214
215 impl Liblary {
216 pub fn open(lib: &CStr) -> Option<Self> {
217 unsafe {
218 NonNull::new(libc::dlopen(
219 lib.as_ptr(),
220 libc::RTLD_LAZY | libc::RTLD_LOCAL,
221 ))
222 .map(Self)
223 }
224 }
225
226 pub fn symbol(&mut self, symbol: &CStr) -> Option<NonNull<c_void>> {
227 unsafe { NonNull::new(libc::dlsym(self.0.as_ptr(), symbol.as_ptr())) }
228 }
229 }
230
231 impl Drop for Liblary {
232 fn drop(&mut self) {
233 unsafe { libc::dlclose(self.0.as_ptr()) };
234 }
235 }