playing pass the hotkey

This commit is contained in:
Nickiel12 2021-12-30 13:24:53 -08:00
parent 34316bacc1
commit 0ac2c3938e
2 changed files with 33 additions and 20 deletions

View file

@ -1,7 +1,7 @@
use std::{time::Duration, thread};
use std::{time::Duration, thread, io::Read};
use crossbeam_channel::unbounded;
use modules::{socket_handler::Socket, stream_states::stream_states_class::StreamState, message_handler::{MessageHandler}};
use modules::{socket_handler::Socket, stream_states::stream_states_class::StreamState, message_handler::{MessageHandler}, external_interface::{Hotkeys, OPTIONS_PATH}};
use workctl::sync_flag;
use crate::modules::stream_states::state_update::StateUpdate;
@ -18,6 +18,17 @@ const SERVER_ADDRESS: &str = "10.0.0.209:5000";
const SERVER_ADDRESS: &str = "10.0.0.168:5000";
fn main() {
let settings_json: serde_json::Value;
{
let mut settings_file = std::fs::File::open(OPTIONS_PATH).unwrap();
let mut settings_str = String::new();
settings_file.read_to_string(&mut settings_str).unwrap();
settings_json = serde_json::from_str(settings_str.as_str()).unwrap();
drop(settings_file);
}
let hotkeys = Hotkeys {
hotkeys: settings_json,
};
let mut state = StreamState::new();
let (control_c_flag_tx, control_c_called_flag_rx) = sync_flag::new_syncflag(false);
@ -45,33 +56,33 @@ fn main() {
if update == StateUpdate::UpdateClient {
update_all(&state, &socket);
}
let updates = state.handle_update(update);
let updates = state.handle_update(update, &hotkeys);
if updates.0.is_some() {
socket.send(updates.0.unwrap().to_json().to_string());
}
if updates.1.is_some() {
handle_instructions(updates.1.unwrap(), &mut state, &socket);
handle_instructions(updates.1.unwrap(), &mut state, &socket, &hotkeys);
}
},
Err(_) => {continue},
}
let tick_update = state.tick();
if tick_update.0.is_some() {state.handle_update(tick_update.0.unwrap());}
if tick_update.1.is_some() {state.handle_update(tick_update.1.unwrap());}
if tick_update.0.is_some() {state.handle_update(tick_update.0.unwrap(), &hotkeys);}
if tick_update.1.is_some() {state.handle_update(tick_update.1.unwrap(), &hotkeys);}
}
socket.close();
hotkey_handle.join().unwrap();
}
fn handle_instructions(mut instructions: Vec<StateUpdate>, state: &mut StreamState, socket: &Socket) {
fn handle_instructions(mut instructions: Vec<StateUpdate>, state: &mut StreamState, socket: &Socket, hotkeys: &Hotkeys) {
for i in instructions.iter_mut() {
let updates = state.handle_update(i.to_owned());
let updates = state.handle_update(i.to_owned(), &hotkeys);
if updates.0.is_some() {
socket.send(updates.0.unwrap().to_json().to_string());
}
if updates.1.is_some() {
handle_instructions(updates.1.unwrap(), state, socket);
handle_instructions(updates.1.unwrap(), state, socket, hotkeys);
}
}
}

View file

@ -1,15 +1,17 @@
use std::time::{SystemTime};
use super::{stream_states::{state_update::StateUpdate, stream_states_class::StreamState, enums::{SlideChange, Scenes}}, external_interface};
use super::{stream_states::{state_update::StateUpdate, stream_states_class::StreamState, enums::{SlideChange, Scenes}}, external_interface::{Hotkeys}};
pub trait MessageHandler { //the first one goes to socket, the second propogates
fn handle_update(&mut self, update: StateUpdate) -> (Option<StateUpdate>, Option<Vec<StateUpdate>>);
fn handle_update(&mut self, update: StateUpdate, hotkey_handler: &Hotkeys)
-> (Option<StateUpdate>, Option<Vec<StateUpdate>>);
fn get_states(&self) -> StreamState;
fn tick(&mut self) -> (Option<StateUpdate>, Option<StateUpdate>);
}
impl MessageHandler for StreamState {
fn handle_update(&mut self, update: StateUpdate) -> (Option<StateUpdate>, Option<Vec<StateUpdate>>) {
fn handle_update(&mut self, update: StateUpdate, hotkey_handler: &Hotkeys)
-> (Option<StateUpdate>, Option<Vec<StateUpdate>>) {
self.update(update.clone());
if self.debug_mode {
@ -24,10 +26,10 @@ impl MessageHandler for StreamState {
}
match direction {
SlideChange::Next => {
external_interface::next_slide();
hotkey_handler.next_slide();
},
SlideChange::Previous => {
external_interface::prev_slide();
hotkey_handler.prev_slide();
}
}
if self.change_scene_on_change_slide_hotkey {
@ -60,25 +62,25 @@ impl MessageHandler for StreamState {
if value.get_type() == Scenes::Camera {
self.camera_sub_scene = value;
if self.current_scene == Scenes::Camera {
external_interface::change_scene(Scenes::Camera, Some(self.camera_sub_scene));
hotkey_handler.change_scene(Scenes::Camera, Some(self.camera_sub_scene));
}
return (Some(update), None)
} else if value.get_type() == Scenes::Screen {
self.screen_sub_scene = value;
if self.current_scene == Scenes::Screen {
external_interface::change_scene(Scenes::Screen, Some(self.screen_sub_scene));
hotkey_handler.change_scene(Scenes::Screen, Some(self.screen_sub_scene));
}
return (Some(update), None)
}
},
StateUpdate::Scene(value) => {
external_interface::change_scene(value, None);
hotkey_handler.change_scene(value, None);
self.current_scene = value;
return (Some(update), None);
},
StateUpdate::StreamSoundToggleOn(value) => {external_interface::toggle_stream_sound(value); return (Some(update), None)},
StateUpdate::ToggleComputerSoundOn(value) => {external_interface::toggle_computer_sound(value); return (Some(update), None)},
StateUpdate::ComputerMediaDoPause(value) => {external_interface::toggle_media_play_pause(value); return (Some(update), None)},
StateUpdate::StreamSoundToggleOn(value) => {hotkey_handler.toggle_stream_sound(value); return (Some(update), None)},
StateUpdate::ToggleComputerSoundOn(value) => {hotkey_handler.toggle_computer_sound(value); return (Some(update), None)},
StateUpdate::ComputerMediaDoPause(value) => {hotkey_handler.toggle_media_play_pause(value); return (Some(update), None)},
StateUpdate::UpdateClient => {},
StateUpdate::StreamRunning(_) => {},
//_ => {}