Buttplug sex toy control library
at dev 100 lines 4.4 kB view raw
1// Buttplug Rust Source Code File - See https://buttplug.io for more info. 2// 3// Copyright 2016-2024 Nonpolynomial Labs LLC. All rights reserved. 4// 5// Licensed under the BSD 3-Clause license. See LICENSE file in the project root 6// for full license information. 7 8use super::ButtplugInProcessClientConnectorBuilder; 9use buttplug_client::ButtplugClient; 10use buttplug_server::{ButtplugServerBuilder, device::ServerDeviceManagerBuilder}; 11use buttplug_server_device_config::DeviceConfigurationManagerBuilder; 12 13/// Convenience function for creating in-process connectors. 14/// 15/// Creates a [ButtplugClient] event loop, with an in-process connector with 16/// all device managers that ship with the library and work on the current 17/// platform added to it already. Takes a maximum ping time to build the 18/// server with, other parameters match `run()`. 19/// 20/// # When To Use This Instead of `run()` 21/// 22/// If you just want to build a quick example and save yourself a few use 23/// statements and setup, this will get you going. For anything *production*, 24/// we recommend using `run()` as you will have more control over what 25/// happens. This method may gain/lose device comm managers at any time. 26/// 27/// # The Device I Want To Use Doesn't Show Up 28/// 29/// If you are trying to use this method to create your client, and do not see 30/// the devices you want, there are a couple of things to check: 31/// 32/// - Are you on a platform that the device communication manager supports? 33/// For instance, we only support XInput on windows. 34/// - Did the developers add a new Device CommunicationManager type and forget 35/// to add it to this method? _It's more likely than you think!_ [File a 36/// bug](https://github.com/buttplugio/buttplug-rs/issues). 37/// 38/// # Errors 39/// 40/// If the library was compiled without any device managers, the 41/// [ButtplugClient] will have nothing to do. This is considered a 42/// catastrophic failure and the library will return an error. 43/// 44/// If the library is using outside device managers, it is recommended to 45/// build your own connector, add your device manager to those, and use the 46/// `run()` method to pass it in. 47pub async fn in_process_client(client_name: &str) -> ButtplugClient { 48 let dcm = DeviceConfigurationManagerBuilder::default() 49 .finish() 50 .unwrap(); 51 52 let mut device_manager_builder = ServerDeviceManagerBuilder::new(dcm); 53 #[cfg(feature = "btleplug-manager")] 54 { 55 use buttplug_server_hwmgr_btleplug::BtlePlugCommunicationManagerBuilder; 56 device_manager_builder.comm_manager(BtlePlugCommunicationManagerBuilder::default()); 57 } 58 #[cfg(feature = "websocket-manager")] 59 { 60 use buttplug_server_hwmgr_websocket::WebsocketServerDeviceCommunicationManagerBuilder; 61 device_manager_builder.comm_manager( 62 WebsocketServerDeviceCommunicationManagerBuilder::default().listen_on_all_interfaces(true), 63 ); 64 } 65 #[cfg(all( 66 feature = "serial-manager", 67 any(target_os = "windows", target_os = "macos", target_os = "linux") 68 ))] 69 { 70 use buttplug_server_hwmgr_serial::SerialPortCommunicationManagerBuilder; 71 device_manager_builder.comm_manager(SerialPortCommunicationManagerBuilder::default()); 72 } 73 #[cfg(feature = "lovense-connect-service-manager")] 74 { 75 use buttplug_server_hwmgr_lovense_connect::LovenseConnectServiceCommunicationManagerBuilder; 76 device_manager_builder 77 .comm_manager(LovenseConnectServiceCommunicationManagerBuilder::default()); 78 } 79 #[cfg(all( 80 feature = "lovense-dongle-manager", 81 any(target_os = "windows", target_os = "macos", target_os = "linux") 82 ))] 83 { 84 use buttplug_server_hwmgr_lovense_dongle::LovenseHIDDongleCommunicationManagerBuilder; 85 device_manager_builder.comm_manager(LovenseHIDDongleCommunicationManagerBuilder::default()); 86 } 87 #[cfg(all(feature = "xinput-manager", target_os = "windows"))] 88 { 89 use buttplug_server_hwmgr_xinput::XInputDeviceCommunicationManagerBuilder; 90 device_manager_builder.comm_manager(XInputDeviceCommunicationManagerBuilder::default()); 91 } 92 let server_builder = ButtplugServerBuilder::new(device_manager_builder.finish().unwrap()); 93 let server = server_builder.finish().unwrap(); 94 let connector = ButtplugInProcessClientConnectorBuilder::default() 95 .server(server) 96 .finish(); 97 let client = ButtplugClient::new(client_name); 98 client.connect(connector).await.unwrap(); 99 client 100}