1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
use crate::ffi;
use super::*;
use super::check;
use crate::strings::{Signature, Path, Member, ErrorName, Interface};
use std::{ptr, any, mem};
use std::ffi::CStr;
use std::os::raw::{c_void, c_char, c_int};
use std::fs::File;


fn arg_append_basic<T>(i: *mut ffi::DBusMessageIter, arg_type: ArgType, v: T) {
    let p = &v as *const _ as *const c_void;
    unsafe {
        check("dbus_message_iter_append_basic", ffi::dbus_message_iter_append_basic(i, arg_type as c_int, p));
    };
}

fn arg_get_basic<T>(i: *mut ffi::DBusMessageIter, arg_type: ArgType) -> Option<T> {
    unsafe {
        if ffi::dbus_message_iter_get_arg_type(i) != arg_type as c_int { return None };
        let mut c = mem::MaybeUninit::uninit();
        ffi::dbus_message_iter_get_basic(i, &mut c as *mut _ as *mut c_void);
        Some(c.assume_init())
    }
}

fn arg_append_str(i: *mut ffi::DBusMessageIter, arg_type: ArgType, v: &CStr) {
    let p = v.as_ptr();
    let q = &p as *const _ as *const c_void;
    unsafe {
        check("dbus_message_iter_append_basic", ffi::dbus_message_iter_append_basic(i, arg_type as c_int, q));
    };
}

unsafe fn arg_get_str<'a>(i: *mut ffi::DBusMessageIter, arg_type: ArgType) -> Option<&'a CStr> {
    if ffi::dbus_message_iter_get_arg_type(i) != arg_type as c_int { return None };
    let mut p = ptr::null_mut();
    ffi::dbus_message_iter_get_basic(i, &mut p as *mut _ as *mut c_void);
    Some(CStr::from_ptr(p as *const c_char))
}




// Implementation for basic types.

macro_rules! integer_impl {
    ($t: ident, $s: ident, $f: expr, $i: ident, $ii: expr, $u: ident, $uu: expr, $fff: ident, $ff: expr) => {

impl Arg for $t {
    const ARG_TYPE: ArgType = ArgType::$s;
    #[inline]
    fn signature() -> Signature<'static> { unsafe { Signature::from_slice_unchecked($f) } }
}

impl Append for $t {
    fn append_by_ref(&self, i: &mut IterAppend) { arg_append_basic(&mut i.0, ArgType::$s, *self) }
}

impl<'a> Get<'a> for $t {
    fn get(i: &mut Iter) -> Option<Self> { arg_get_basic(&mut i.0, ArgType::$s) }
}

impl RefArg for $t {
    #[inline]
    fn arg_type(&self) -> ArgType { ArgType::$s }
    #[inline]
    fn signature(&self) -> Signature<'static> { unsafe { Signature::from_slice_unchecked($f) } }
    #[inline]
    fn append(&self, i: &mut IterAppend) { arg_append_basic(&mut i.0, ArgType::$s, *self) }
    #[inline]
    fn as_any(&self) -> &dyn any::Any { self }
    #[inline]
    fn as_any_mut(&mut self) -> &mut dyn any::Any { self }
    #[inline]
    fn as_i64(&self) -> Option<i64> { let $i = *self; $ii }
    #[inline]
    fn as_u64(&self) -> Option<u64> { let $u = *self; $uu }
    #[inline]
    fn as_f64(&self) -> Option<f64> { let $fff = *self; $ff }
    #[inline]
    fn box_clone(&self) -> Box<dyn RefArg + 'static> { Box::new(self.clone()) }
    fn array_clone(v: &[Self]) -> Option<Box<dyn RefArg + 'static>> where Self: Sized { Some(Box::new(v.to_vec())) }
}

impl DictKey for $t {}
unsafe impl FixedArray for $t {}

}} // End of macro_rules

integer_impl!(u8, Byte, "y\0", i, Some(i as i64),    u, Some(u as u64), f, Some(f as f64));
integer_impl!(i16, Int16, "n\0", i, Some(i as i64),  _u, None,          f, Some(f as f64));
integer_impl!(u16, UInt16, "q\0", i, Some(i as i64), u, Some(u as u64), f, Some(f as f64));
integer_impl!(i32, Int32, "i\0", i, Some(i as i64),  _u, None,          f, Some(f as f64));
integer_impl!(u32, UInt32, "u\0", i, Some(i as i64), u, Some(u as u64), f, Some(f as f64));
integer_impl!(i64, Int64, "x\0", i, Some(i),         _u, None,          _f, None);
integer_impl!(u64, UInt64, "t\0", _i, None,          u, Some(u as u64), _f, None);


macro_rules! refarg_impl {
    ($t: ty, $i: ident, $ii: expr, $ss: expr, $uu: expr, $ff: expr) => {

impl RefArg for $t {
    #[inline]
    fn arg_type(&self) -> ArgType { <$t as Arg>::ARG_TYPE }
    #[inline]
    fn signature(&self) -> Signature<'static> { <$t as Arg>::signature() }
    #[inline]
    fn append(&self, i: &mut IterAppend) { <$t as Append>::append_by_ref(self, i) }
    #[inline]
    fn as_any(&self) -> &dyn any::Any { self }
    #[inline]
    fn as_any_mut(&mut self) -> &mut dyn any::Any { self }
    #[inline]
    fn as_i64(&self) -> Option<i64> { let $i = self; $ii }
    #[inline]
    fn as_u64(&self) -> Option<u64> { let $i = self; $uu }
    #[inline]
    fn as_f64(&self) -> Option<f64> { let $i = self; $ff }
    #[inline]
    fn as_str(&self) -> Option<&str> { let $i = self; $ss }
    #[inline]
    fn box_clone(&self) -> Box<dyn RefArg + 'static> { Box::new(self.clone()) }
    fn array_clone(v: &[Self]) -> Option<Box<dyn RefArg + 'static>> where Self: Sized { Some(Box::new(v.to_vec())) }

}

    }
}


impl Arg for bool {
    const ARG_TYPE: ArgType = ArgType::Boolean;
    fn signature() -> Signature<'static> { unsafe { Signature::from_slice_unchecked("b\0") } }
}
impl Append for bool {
    fn append_by_ref(&self, i: &mut IterAppend) { arg_append_basic(&mut i.0, ArgType::Boolean, if *self {1} else {0}) }
}
impl DictKey for bool {}
impl<'a> Get<'a> for bool {
    fn get(i: &mut Iter) -> Option<Self> { arg_get_basic::<u32>(&mut i.0, ArgType::Boolean).map(|q| q != 0) }
}

refarg_impl!(bool, _i, Some(if *_i { 1 } else { 0 }), None, Some(if *_i { 1 as u64 } else { 0 as u64 }), Some(if *_i { 1 as f64 } else { 0 as f64 }));

impl Arg for f64 {
    const ARG_TYPE: ArgType = ArgType::Double;
    fn signature() -> Signature<'static> { unsafe { Signature::from_slice_unchecked("d\0") } }
}
impl Append for f64 {
    fn append_by_ref(&self, i: &mut IterAppend) { arg_append_basic(&mut i.0, ArgType::Double, *self) }
}
impl DictKey for f64 {}
impl<'a> Get<'a> for f64 {
    fn get(i: &mut Iter) -> Option<Self> { arg_get_basic(&mut i.0, ArgType::Double) }
}
unsafe impl FixedArray for f64 {}

refarg_impl!(f64, _i, None, None, None, Some(*_i));

/// Represents a D-Bus string.
impl<'a> Arg for &'a str {
    const ARG_TYPE: ArgType = ArgType::String;
    fn signature() -> Signature<'static> { unsafe { Signature::from_slice_unchecked("s\0") } }
}

impl<'a> Append for &'a str {
    fn append_by_ref(&self, i: &mut IterAppend) {
        use std::borrow::Cow;
        let b: &[u8] = self.as_bytes();
        let v: Cow<[u8]> = if !b.is_empty() && b[b.len()-1] == 0 { Cow::Borrowed(b) }
        else {
            let mut bb: Vec<u8> = b.into();
            bb.push(0);
            Cow::Owned(bb)
        };
        let z = unsafe { CStr::from_ptr(v.as_ptr() as *const c_char) };
        arg_append_str(&mut i.0, ArgType::String, &z)
    }
}
impl<'a> DictKey for &'a str {}
impl<'a> Get<'a> for &'a str {
    fn get(i: &mut Iter<'a>) -> Option<&'a str> { unsafe { arg_get_str(&mut i.0, ArgType::String) }
        .and_then(|s| s.to_str().ok()) }
}

impl<'a> Arg for String {
    const ARG_TYPE: ArgType = ArgType::String;
    fn signature() -> Signature<'static> { unsafe { Signature::from_slice_unchecked("s\0") } }
}
impl<'a> Append for String {
    fn append(mut self, i: &mut IterAppend) {
        self.push_str("\0");
        let s: &str = &self;
        s.append(i)
    }
    fn append_by_ref(&self, i: &mut IterAppend) {
        (&**self).append_by_ref(i)
    }
}
impl<'a> DictKey for String {}
impl<'a> Get<'a> for String {
    fn get(i: &mut Iter<'a>) -> Option<String> { <&str>::get(i).map(String::from) }
}

refarg_impl!(String, _i, None, Some(&_i), None, None);

/// Represents a D-Bus string.
impl<'a> Arg for &'a CStr {
    const ARG_TYPE: ArgType = ArgType::String;
    fn signature() -> Signature<'static> { unsafe { Signature::from_slice_unchecked("s\0") } }
}

/*
/// Note: Will give D-Bus errors in case the CStr is not valid UTF-8.
impl<'a> Append for &'a CStr {
    fn append(self, i: &mut IterAppend) {
        arg_append_str(&mut i.0, Self::arg_type(), &self)
    }
}
*/

impl<'a> DictKey for &'a CStr {}
impl<'a> Get<'a> for &'a CStr {
    fn get(i: &mut Iter<'a>) -> Option<&'a CStr> { unsafe { arg_get_str(&mut i.0, Self::ARG_TYPE) }}
}

impl Arg for OwnedFd {
    const ARG_TYPE: ArgType = ArgType::UnixFd;
    fn signature() -> Signature<'static> { unsafe { Signature::from_slice_unchecked("h\0") } }
}
impl Append for OwnedFd {
    fn append_by_ref(&self, i: &mut IterAppend) {
        arg_append_basic(&mut i.0, ArgType::UnixFd, self.as_raw_fd())
    }
}
impl DictKey for OwnedFd {}
impl<'a> Get<'a> for OwnedFd {
    fn get(i: &mut Iter) -> Option<Self> {
        arg_get_basic(&mut i.0, ArgType::UnixFd).map(|fd| unsafe { OwnedFd::new(fd) })
    }
}

refarg_impl!(OwnedFd, _i, { use std::os::unix::io::AsRawFd; Some(_i.as_raw_fd() as i64) }, None, None, None);

impl Arg for File {
    const ARG_TYPE: ArgType = ArgType::UnixFd;
    fn signature() -> Signature<'static> { unsafe { Signature::from_slice_unchecked("h\0") } }
}
impl Append for File {
    fn append_by_ref(&self, i: &mut IterAppend) {
        arg_append_basic(&mut i.0, ArgType::UnixFd, self.as_raw_fd())
    }
}
impl DictKey for File {}
impl<'a> Get<'a> for File {
    fn get(i: &mut Iter) -> Option<Self> {
        arg_get_basic(&mut i.0, ArgType::UnixFd).map(|fd| unsafe { File::from_raw_fd(fd) })
    }
}

impl RefArg for File {
    #[inline]
    fn arg_type(&self) -> ArgType { <File as Arg>::ARG_TYPE }
    #[inline]
    fn signature(&self) -> Signature<'static> { <File as Arg>::signature() }
    #[inline]
    fn append(&self, i: &mut IterAppend) { <File as Append>::append_by_ref(self, i) }
    #[inline]
    fn as_any(&self) -> &dyn any::Any { self }
    #[inline]
    fn as_any_mut(&mut self) -> &mut dyn any::Any { self }
    #[inline]
    fn as_i64(&self) -> Option<i64> { Some(self.as_raw_fd() as i64) }
    #[inline]
    fn box_clone(&self) -> Box<dyn RefArg + 'static> { Box::new(self.try_clone().unwrap()) }
}


macro_rules! string_impl {
    ($t: ident, $s: ident, $f: expr) => {

impl<'a> Arg for $t<'a> {
    const ARG_TYPE: ArgType = ArgType::$s;
    fn signature() -> Signature<'static> { unsafe { Signature::from_slice_unchecked($f) } }
}

impl RefArg for $t<'static> {
    fn arg_type(&self) -> ArgType { ArgType::$s }
    fn signature(&self) -> Signature<'static> { unsafe { Signature::from_slice_unchecked($f) } }
    fn append(&self, i: &mut IterAppend) { arg_append_str(&mut i.0, ArgType::$s, self.as_cstr()) }
    #[inline]
    fn as_any(&self) -> &dyn any::Any { self }
    #[inline]
    fn as_any_mut(&mut self) -> &mut dyn any::Any { self }
    #[inline]
    fn as_str(&self) -> Option<&str> { Some(self) }
    #[inline]
    fn box_clone(&self) -> Box<dyn RefArg + 'static> { Box::new(self.clone().into_static()) }
    fn array_clone(v: &[Self]) -> Option<Box<dyn RefArg + 'static>> where Self: Sized { Some(Box::new(v.to_vec())) }
}

impl<'a> DictKey for $t<'a> {}

impl<'a> Append for $t<'a> {
    fn append_by_ref(&self, i: &mut IterAppend) {
        arg_append_str(&mut i.0, ArgType::$s, self.as_cstr())
    }
}

/*

Unfortunately, this does not work because it conflicts with getting a $t<'static>.

impl<'a> Get<'a> for $t<'a> {
    fn get(i: &mut Iter<'a>) -> Option<$t<'a>> { unsafe { arg_get_str(&mut i.0, ArgType::$s) }
        .map(|s| unsafe { $t::from_slice_unchecked(s.to_bytes_with_nul()) } ) }
}
*/

impl<'a> Get<'a> for $t<'static> {
    fn get(i: &mut Iter<'a>) -> Option<$t<'static>> { unsafe {
        let c = arg_get_str(&mut i.0, ArgType::$s)?;
        let s = std::str::from_utf8(c.to_bytes_with_nul()).ok()?;
        Some($t::from_slice_unchecked(s).into_static())
    }}
}


    }
}

string_impl!(Interface, String, "s\0");
string_impl!(ErrorName, String, "s\0");
string_impl!(Member, String, "s\0");
string_impl!(Path, ObjectPath, "o\0");
string_impl!(Signature, Signature, "g\0");