mirror of
				https://github.com/Zedfrigg/ironbar.git
				synced 2025-11-03 23:11:54 +01:00 
			
		
		
		
	refactor: extract initialisation macro
This commit is contained in:
		
					parent
					
						
							
								2ae8d50cab
							
						
					
				
			
			
				commit
				
					
						d56f76f6a8
					
				
			
		
					 1 changed files with 119 additions and 118 deletions
				
			
		| 
						 | 
					@ -35,67 +35,65 @@ struct ClientInner<'l> {
 | 
				
			||||||
    dbus_connection: Connection,
 | 
					    dbus_connection: Connection,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl Client {
 | 
					macro_rules! initialise_path_map {
 | 
				
			||||||
    fn new() -> Result<Client> {
 | 
					    (
 | 
				
			||||||
        let state = Mutable::new(State {
 | 
					        $client:expr,
 | 
				
			||||||
            wired: WiredState::Unknown,
 | 
					        $path_map:ident,
 | 
				
			||||||
            wifi: WifiState::Unknown,
 | 
					        $proxy_type:ident
 | 
				
			||||||
            cellular: CellularState::Unknown,
 | 
					        $(, |$new_path:ident| $property_watcher:expr)*
 | 
				
			||||||
            vpn: VpnState::Unknown,
 | 
					    ) => {
 | 
				
			||||||
        });
 | 
					        let new_paths = $client.root_object.$path_map()?;
 | 
				
			||||||
        let dbus_connection = Connection::system()?;
 | 
					        let mut path_map = HashMap::new();
 | 
				
			||||||
        let root_object = {
 | 
					        for new_path in new_paths {
 | 
				
			||||||
            let root_object = DbusProxyBlocking::new(&dbus_connection)?;
 | 
					            let new_proxy = $proxy_type::builder(&$client.dbus_connection)
 | 
				
			||||||
            // Workaround for the fact that zbus (unnecessarily) requires a static lifetime here
 | 
					                .path(new_path.clone())?
 | 
				
			||||||
            Box::leak(Box::new(root_object))
 | 
					                .build()?;
 | 
				
			||||||
        };
 | 
					            path_map.insert(new_path.clone(), new_proxy);
 | 
				
			||||||
 | 
					            $({
 | 
				
			||||||
        Ok(Client(Arc::new(ClientInner {
 | 
					                let $new_path = &new_path;
 | 
				
			||||||
            state,
 | 
					                $property_watcher;
 | 
				
			||||||
            root_object,
 | 
					            })*
 | 
				
			||||||
            active_connections: RwLock::new(HashMap::new()),
 | 
					 | 
				
			||||||
            devices: RwLock::new(HashMap::new()),
 | 
					 | 
				
			||||||
            dbus_connection,
 | 
					 | 
				
			||||||
        })))
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        *write_lock!($client.$path_map) = path_map;
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn run(&self) -> Result<()> {
 | 
					macro_rules! spawn_path_list_watcher {
 | 
				
			||||||
        macro_rules! spawn_path_list_watcher {
 | 
					 | 
				
			||||||
    (
 | 
					    (
 | 
				
			||||||
        $client:expr,
 | 
					        $client:expr,
 | 
				
			||||||
        $property:ident,
 | 
					        $property:ident,
 | 
				
			||||||
        $property_changes:ident,
 | 
					        $property_changes:ident,
 | 
				
			||||||
                $proxy_type:ident,
 | 
					        $proxy_type:ident
 | 
				
			||||||
                $(|$inner_client:ident, $new_path:ident| $property_watcher:expr,)*
 | 
					        $(, |$inner_client:ident, $new_path:ident| $property_watcher:expr)*
 | 
				
			||||||
    ) => {
 | 
					    ) => {
 | 
				
			||||||
        let client = $client.clone();
 | 
					        let client = $client.clone();
 | 
				
			||||||
        spawn_blocking_result!({
 | 
					        spawn_blocking_result!({
 | 
				
			||||||
            let changes = client.root_object.$property_changes();
 | 
					            let changes = client.root_object.$property_changes();
 | 
				
			||||||
            for _ in changes {
 | 
					            for _ in changes {
 | 
				
			||||||
                        let mut new_pathmap = HashMap::new();
 | 
					                let mut new_path_map = HashMap::new();
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    let new_paths = client.root_object.$property()?;
 | 
					                    let new_paths = client.root_object.$property()?;
 | 
				
			||||||
                            let pathmap = read_lock!(client.$property);
 | 
					                    let path_map = read_lock!(client.$property);
 | 
				
			||||||
                    for new_path in new_paths {
 | 
					                    for new_path in new_paths {
 | 
				
			||||||
                                if pathmap.contains_key(&new_path) {
 | 
					                        if path_map.contains_key(&new_path) {
 | 
				
			||||||
                                    let proxy = pathmap
 | 
					                            let proxy = path_map
 | 
				
			||||||
                                .get(&new_path)
 | 
					                                .get(&new_path)
 | 
				
			||||||
                                .expect("Should contain the key, guarded by runtime check");
 | 
					                                .expect("Should contain the key, guarded by runtime check");
 | 
				
			||||||
                                    new_pathmap.insert(new_path, proxy.to_owned());
 | 
					                            new_path_map.insert(new_path, proxy.to_owned());
 | 
				
			||||||
                        } else {
 | 
					                        } else {
 | 
				
			||||||
                            let new_proxy = $proxy_type::builder(&client.dbus_connection)
 | 
					                            let new_proxy = $proxy_type::builder(&client.dbus_connection)
 | 
				
			||||||
                                .path(new_path.clone())?
 | 
					                                .path(new_path.clone())?
 | 
				
			||||||
                                .build()?;
 | 
					                                .build()?;
 | 
				
			||||||
                                    new_pathmap.insert(new_path.clone(), new_proxy);
 | 
					                            new_path_map.insert(new_path.clone(), new_proxy);
 | 
				
			||||||
                            $({
 | 
					                            $({
 | 
				
			||||||
                                let $inner_client = &client;
 | 
					                                let $inner_client = &client;
 | 
				
			||||||
                                let $new_path = &new_path;
 | 
					                                let $new_path = &new_path;
 | 
				
			||||||
                                        $property_watcher
 | 
					                                $property_watcher;
 | 
				
			||||||
                            })*
 | 
					                            })*
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                        *write_lock!(client.$property) = new_pathmap;
 | 
					                *write_lock!(client.$property) = new_path_map;
 | 
				
			||||||
                client.state.set(State {
 | 
					                client.state.set(State {
 | 
				
			||||||
                    // TODO: Investigate if there's a sane way to do only the relevant updates
 | 
					                    // TODO: Investigate if there's a sane way to do only the relevant updates
 | 
				
			||||||
                    wired: determine_wired_state(&read_lock!(client.devices))?,
 | 
					                    wired: determine_wired_state(&read_lock!(client.devices))?,
 | 
				
			||||||
| 
						 | 
					@ -107,9 +105,9 @@ impl Client {
 | 
				
			||||||
            Ok(())
 | 
					            Ok(())
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
        }
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        macro_rules! spawn_property_watcher {
 | 
					macro_rules! spawn_property_watcher {
 | 
				
			||||||
    (
 | 
					    (
 | 
				
			||||||
        $client:expr,
 | 
					        $client:expr,
 | 
				
			||||||
        $path:expr,
 | 
					        $path:expr,
 | 
				
			||||||
| 
						 | 
					@ -138,51 +136,54 @@ impl Client {
 | 
				
			||||||
            Ok(())
 | 
					            Ok(())
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl Client {
 | 
				
			||||||
 | 
					    fn new() -> Result<Client> {
 | 
				
			||||||
 | 
					        let state = Mutable::new(State {
 | 
				
			||||||
 | 
					            wired: WiredState::Unknown,
 | 
				
			||||||
 | 
					            wifi: WifiState::Unknown,
 | 
				
			||||||
 | 
					            cellular: CellularState::Unknown,
 | 
				
			||||||
 | 
					            vpn: VpnState::Unknown,
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					        let dbus_connection = Connection::system()?;
 | 
				
			||||||
 | 
					        let root_object = {
 | 
				
			||||||
 | 
					            let root_object = DbusProxyBlocking::new(&dbus_connection)?;
 | 
				
			||||||
 | 
					            // Workaround for the fact that zbus (unnecessarily) requires a static lifetime here
 | 
				
			||||||
 | 
					            Box::leak(Box::new(root_object))
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Ok(Client(Arc::new(ClientInner {
 | 
				
			||||||
 | 
					            state,
 | 
				
			||||||
 | 
					            root_object,
 | 
				
			||||||
 | 
					            active_connections: RwLock::new(HashMap::new()),
 | 
				
			||||||
 | 
					            devices: RwLock::new(HashMap::new()),
 | 
				
			||||||
 | 
					            dbus_connection,
 | 
				
			||||||
 | 
					        })))
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Initialisation
 | 
					    fn run(&self) -> Result<()> {
 | 
				
			||||||
        {
 | 
					        initialise_path_map!(
 | 
				
			||||||
            // Initial active connections path list
 | 
					            self.0,
 | 
				
			||||||
            {
 | 
					            active_connections,
 | 
				
			||||||
                let new_paths = self.0.root_object.active_connections()?;
 | 
					            ActiveConnectionDbusProxyBlocking
 | 
				
			||||||
                let mut pathmap = write_lock!(self.0.active_connections);
 | 
					        );
 | 
				
			||||||
                for new_path in new_paths {
 | 
					        initialise_path_map!(self.0, devices, DeviceDbusProxyBlocking, |path| {
 | 
				
			||||||
                    let new_proxy =
 | 
					            spawn_property_watcher!(self.0, path, receive_state_changed, devices);
 | 
				
			||||||
                        ActiveConnectionDbusProxyBlocking::builder(&self.0.dbus_connection)
 | 
					        });
 | 
				
			||||||
                            .path(new_path.clone())?
 | 
					 | 
				
			||||||
                            .build()?;
 | 
					 | 
				
			||||||
                    pathmap.insert(new_path, new_proxy);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            // Initial devices path list
 | 
					 | 
				
			||||||
            {
 | 
					 | 
				
			||||||
                let new_paths = self.0.root_object.devices()?;
 | 
					 | 
				
			||||||
                let mut pathmap = write_lock!(self.0.devices);
 | 
					 | 
				
			||||||
                for new_path in new_paths {
 | 
					 | 
				
			||||||
                    let new_proxy = DeviceDbusProxyBlocking::builder(&self.0.dbus_connection)
 | 
					 | 
				
			||||||
                        .path(new_path.clone())?
 | 
					 | 
				
			||||||
                        .build()?;
 | 
					 | 
				
			||||||
                    pathmap.insert(new_path.clone(), new_proxy);
 | 
					 | 
				
			||||||
                    spawn_property_watcher!(self.0, new_path, receive_state_changed, devices);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        self.0.state.set(State {
 | 
					        self.0.state.set(State {
 | 
				
			||||||
            wired: determine_wired_state(&read_lock!(self.0.devices))?,
 | 
					            wired: determine_wired_state(&read_lock!(self.0.devices))?,
 | 
				
			||||||
            wifi: determine_wifi_state(&read_lock!(self.0.devices))?,
 | 
					            wifi: determine_wifi_state(&read_lock!(self.0.devices))?,
 | 
				
			||||||
            cellular: determine_cellular_state(&read_lock!(self.0.devices))?,
 | 
					            cellular: determine_cellular_state(&read_lock!(self.0.devices))?,
 | 
				
			||||||
            vpn: determine_vpn_state(&read_lock!(self.0.active_connections))?,
 | 
					            vpn: determine_vpn_state(&read_lock!(self.0.active_connections))?,
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        spawn_path_list_watcher!(
 | 
					        spawn_path_list_watcher!(
 | 
				
			||||||
            self.0,
 | 
					            self.0,
 | 
				
			||||||
            active_connections,
 | 
					            active_connections,
 | 
				
			||||||
            receive_active_connections_changed,
 | 
					            receive_active_connections_changed,
 | 
				
			||||||
            ActiveConnectionDbusProxyBlocking,
 | 
					            ActiveConnectionDbusProxyBlocking
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
 | 
					 | 
				
			||||||
        spawn_path_list_watcher!(
 | 
					        spawn_path_list_watcher!(
 | 
				
			||||||
            self.0,
 | 
					            self.0,
 | 
				
			||||||
            devices,
 | 
					            devices,
 | 
				
			||||||
| 
						 | 
					@ -190,7 +191,7 @@ impl Client {
 | 
				
			||||||
            DeviceDbusProxyBlocking,
 | 
					            DeviceDbusProxyBlocking,
 | 
				
			||||||
            |client, path| {
 | 
					            |client, path| {
 | 
				
			||||||
                spawn_property_watcher!(client, path, receive_state_changed, devices);
 | 
					                spawn_property_watcher!(client, path, receive_state_changed, devices);
 | 
				
			||||||
            },
 | 
					            }
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Ok(())
 | 
					        Ok(())
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue