Skip to content

Commit

Permalink
refactor(server): ♻️ Light refactoring for hand gestures code
Browse files Browse the repository at this point in the history
  • Loading branch information
zmerp committed Sep 12, 2023
1 parent ae66ce9 commit 096ec85
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 190 deletions.
136 changes: 69 additions & 67 deletions alvr/server/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ pub fn handshake_loop() {
data_manager
.client_list()
.get(&client_hostname)
.unwrap()
.trusted
.map(|c| c.trusted)
.unwrap_or(false)
};

// do not attempt connection if the client is already connected
Expand All @@ -272,9 +272,8 @@ pub fn handshake_loop() {
.read()
.client_list()
.get(&client_hostname)
.unwrap()
.connection_state
== ConnectionState::Disconnected
.map(|c| c.connection_state == ConnectionState::Disconnected)
.unwrap_or(false)
{
if let Err(e) =
try_connect([(client_ip, client_hostname.clone())].into_iter().collect())
Expand All @@ -297,8 +296,9 @@ fn try_connect(mut client_ips: HashMap<IpAddr, String>) -> ConResult {
let (disconnect_sender, disconnect_receiver) = mpsc::channel();
*DISCONNECT_CLIENT_NOTIFIER.lock() = Some(disconnect_sender);

// Safety: this never panics because client_ip is picked from client_ips keys
let client_hostname = client_ips.remove(&client_ip).unwrap();
let Some(client_hostname) = client_ips.remove(&client_ip) else {
con_bail!("unreachable");
};

struct DropGuard {
hostname: String,
Expand Down Expand Up @@ -650,19 +650,12 @@ fn try_connect(mut client_ips: HashMap<IpAddr, String>) -> ConResult {
let tracking_manager = Arc::clone(&tracking_manager);
let hand_gesture_manager = Arc::clone(&hand_gesture_manager);

let mut gestures_button_mapping_manager = SERVER_DATA_MANAGER
.read()
.settings()
.headset
.controllers
.as_option()
.and_then(|config| {
config.hand_tracking.use_gestures.as_option().and_then(|_| {
Some(ButtonMappingManager::new_automatic(
&HAND_GESTURE_BUTTON_SET,
&config.button_mapping_config,
))
})
let mut gestures_button_mapping_manager =
settings.headset.controllers.as_option().map(|config| {
ButtonMappingManager::new_automatic(
&HAND_GESTURE_BUTTON_SET,
&config.button_mapping_config,
)
});

move || {
Expand All @@ -675,11 +668,6 @@ fn try_connect(mut client_ips: HashMap<IpAddr, String>) -> ConResult {
FaceTrackingSink::new(config.sink, settings.connection.osc_local_port).ok()
});

let mut track_controllers = 0u32;
if let Switch::Enabled(config) = &settings.headset.controllers {
track_controllers = config.tracked.into();
}

while IS_STREAMING.value() {
let data = match tracking_receiver.recv(STREAMING_RECV_TIMEOUT) {
Ok(tracking) => tracking,
Expand All @@ -690,6 +678,21 @@ fn try_connect(mut client_ips: HashMap<IpAddr, String>) -> ConResult {
return;
};

let controllers_config = {
let data_lock = SERVER_DATA_MANAGER.read();
data_lock
.settings()
.headset
.controllers
.clone()
.into_option()
};

let track_controllers = controllers_config
.as_ref()
.map(|c| c.tracked)
.unwrap_or(false);

let mut tracking_manager_lock = tracking_manager.lock();

let motions;
Expand Down Expand Up @@ -766,50 +769,49 @@ fn try_connect(mut client_ips: HashMap<IpAddr, String>) -> ConResult {

drop(tracking_manager_lock);

if let Some(stats) = &mut *STATISTICS_MANAGER.lock() {
stats.report_tracking_received(tracking.target_timestamp);

// Handle hand gestures
if let Switch::Enabled(controllers_config) = &settings.headset.controllers {
if let Switch::Enabled(gestures_config) =
&controllers_config.hand_tracking.use_gestures
{
let mut hand_gesture_manager_lock = hand_gesture_manager.lock();

if let Some(hand_skeleton) = tracking.hand_skeletons[0] {
trigger_hand_gesture_actions(
gestures_button_mapping_manager.as_mut().unwrap(),
*LEFT_HAND_ID,
&hand_gesture_manager_lock.get_active_gestures(
hand_skeleton,
gestures_config.clone(),
*LEFT_HAND_ID,
),
);
}
if let Some(hand_skeleton) = tracking.hand_skeletons[1] {
trigger_hand_gesture_actions(
gestures_button_mapping_manager.as_mut().unwrap(),
*RIGHT_HAND_ID,
&hand_gesture_manager_lock.get_active_gestures(
hand_skeleton,
gestures_config.clone(),
*RIGHT_HAND_ID,
),
);
}
}
// Handle hand gestures
if let (Some(gestures_config), Some(gestures_button_mapping_manager)) = (
controllers_config
.as_ref()
.and_then(|c| c.hand_tracking.gestures.as_option()),
&mut gestures_button_mapping_manager,
) {
let mut hand_gesture_manager_lock = hand_gesture_manager.lock();

if let Some(hand_skeleton) = tracking.hand_skeletons[0] {
trigger_hand_gesture_actions(
gestures_button_mapping_manager,
*LEFT_HAND_ID,
&hand_gesture_manager_lock.get_active_gestures(
hand_skeleton,
gestures_config,
*LEFT_HAND_ID,
),
);
}
if let Some(hand_skeleton) = tracking.hand_skeletons[1] {
trigger_hand_gesture_actions(
gestures_button_mapping_manager,
*RIGHT_HAND_ID,
&hand_gesture_manager_lock.get_active_gestures(
hand_skeleton,
gestures_config,
*RIGHT_HAND_ID,
),
);
}
}

let data_manager_lock = SERVER_DATA_MANAGER.read();
let config = &data_manager_lock.settings().headset;
if controllers_config
.map(|c| c.hand_tracking.enable_skeleton)
.unwrap_or(false)
{
ffi_left_hand_skeleton = None;
ffi_right_hand_skeleton = None;
}

if let Switch::Enabled(controllers) = &config.controllers {
if !controllers.hand_tracking.enable_skeleton {
ffi_left_hand_skeleton = None;
ffi_right_hand_skeleton = None;
}
}
if let Some(stats) = &mut *STATISTICS_MANAGER.lock() {
stats.report_tracking_received(tracking.target_timestamp);

unsafe {
crate::SetTracking(
Expand All @@ -827,7 +829,7 @@ fn try_connect(mut client_ips: HashMap<IpAddr, String>) -> ConResult {
} else {
ptr::null()
},
track_controllers,
track_controllers.into(),
)
};
}
Expand Down
Loading

0 comments on commit 096ec85

Please sign in to comment.