|
2 | 2 |
|
3 | 3 | #![deny(unsafe_op_in_unsafe_fn)] |
4 | 4 |
|
5 | | -use glib::{prelude::*, subclass::prelude::*}; |
| 5 | +use glib::{prelude::*, subclass::prelude::*, translate::*, GString, StrVRef}; |
6 | 6 |
|
7 | | -use crate::subclass::prelude::DBusInterfaceImpl; |
8 | | -use crate::DBusProxy; |
| 7 | +use crate::{ffi, subclass::prelude::DBusInterfaceImpl, DBusProxy}; |
9 | 8 |
|
10 | 9 | pub trait DBusProxyImpl: |
11 | 10 | ObjectImpl + DBusInterfaceImpl + ObjectSubclass<Type: IsA<DBusProxy>> |
12 | 11 | { |
| 12 | + fn g_properties_changed( |
| 13 | + &self, |
| 14 | + changed_properties: &glib::Variant, |
| 15 | + invalidated_properties: &StrVRef, |
| 16 | + ) { |
| 17 | + self.parent_g_properties_changed(changed_properties, invalidated_properties); |
| 18 | + } |
| 19 | + |
| 20 | + fn g_signal(&self, sender_name: Option<&str>, signal_name: &str, parameters: &glib::Variant) { |
| 21 | + self.parent_g_signal(sender_name, signal_name, parameters); |
| 22 | + } |
13 | 23 | } |
14 | 24 |
|
15 | | -pub trait DBusProxyImplExt: DBusProxyImpl {} |
| 25 | +pub trait DBusProxyImplExt: DBusProxyImpl { |
| 26 | + fn parent_g_properties_changed( |
| 27 | + &self, |
| 28 | + changed_properties: &glib::Variant, |
| 29 | + invalidated_properties: &StrVRef, |
| 30 | + ) { |
| 31 | + unsafe { |
| 32 | + let data = Self::type_data(); |
| 33 | + let parent_class = data.as_ref().parent_class() as *const ffi::GDBusProxyClass; |
| 34 | + |
| 35 | + if let Some(f) = (*parent_class).g_properties_changed { |
| 36 | + f( |
| 37 | + self.obj().unsafe_cast_ref::<DBusProxy>().to_glib_none().0, |
| 38 | + changed_properties.to_glib_none().0, |
| 39 | + invalidated_properties.to_glib_none().0, |
| 40 | + ); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + fn parent_g_signal( |
| 46 | + &self, |
| 47 | + sender_name: Option<&str>, |
| 48 | + signal_name: &str, |
| 49 | + parameters: &glib::Variant, |
| 50 | + ) { |
| 51 | + unsafe { |
| 52 | + let data = Self::type_data(); |
| 53 | + let parent_class = data.as_ref().parent_class() as *const ffi::GDBusProxyClass; |
| 54 | + |
| 55 | + if let Some(f) = (*parent_class).g_signal { |
| 56 | + f( |
| 57 | + self.obj().unsafe_cast_ref::<DBusProxy>().to_glib_none().0, |
| 58 | + sender_name.to_glib_none().0, |
| 59 | + signal_name.to_glib_none().0, |
| 60 | + parameters.to_glib_none().0, |
| 61 | + ); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} |
16 | 66 |
|
17 | 67 | impl<T: DBusProxyImpl> DBusProxyImplExt for T {} |
18 | 68 |
|
19 | 69 | unsafe impl<T: DBusProxyImpl> IsSubclassable<T> for DBusProxy { |
20 | 70 | fn class_init(class: &mut glib::Class<Self>) { |
21 | 71 | Self::parent_class_init::<T>(class); |
| 72 | + let class = class.as_mut(); |
| 73 | + class.g_properties_changed = Some(g_properties_changed::<T>); |
| 74 | + class.g_signal = Some(g_signal::<T>); |
22 | 75 | } |
23 | 76 | } |
| 77 | + |
| 78 | +unsafe extern "C" fn g_properties_changed<T: DBusProxyImpl>( |
| 79 | + proxy: *mut ffi::GDBusProxy, |
| 80 | + changed_properties: *mut glib::ffi::GVariant, |
| 81 | + invalidated_properties: *const *const libc::c_char, |
| 82 | +) { |
| 83 | + let instance = unsafe { &*(proxy as *mut T::Instance) }; |
| 84 | + let imp = instance.imp(); |
| 85 | + |
| 86 | + let changed_properties = unsafe { from_glib_borrow(changed_properties) }; |
| 87 | + let invalidated_properties = unsafe { StrVRef::from_glib_borrow(invalidated_properties) }; |
| 88 | + imp.g_properties_changed(&changed_properties, invalidated_properties); |
| 89 | +} |
| 90 | + |
| 91 | +unsafe extern "C" fn g_signal<T: DBusProxyImpl>( |
| 92 | + proxy: *mut ffi::GDBusProxy, |
| 93 | + sender_name: *const libc::c_char, |
| 94 | + signal_name: *const libc::c_char, |
| 95 | + parameters: *mut glib::ffi::GVariant, |
| 96 | +) { |
| 97 | + let instance = unsafe { &*(proxy as *mut T::Instance) }; |
| 98 | + let imp = instance.imp(); |
| 99 | + |
| 100 | + let sender_name = unsafe { Option::<GString>::from_glib_borrow(sender_name) }; |
| 101 | + let signal_name = unsafe { GString::from_glib_borrow(signal_name) }; |
| 102 | + let parameters = unsafe { from_glib_borrow(parameters) }; |
| 103 | + imp.g_signal(sender_name.as_ref().as_deref(), &signal_name, ¶meters); |
| 104 | +} |
0 commit comments