Skip to content

Commit

Permalink
Implement support for toggling the mic state
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseTG committed Jul 9, 2023
1 parent 34b5c1d commit db19fab
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/libretro/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ namespace melonds {
[[nodiscard]] int TouchY() const noexcept { return touch_y; }
[[nodiscard]] bool SwapScreenPressed() const noexcept { return swap_screens_btn; }
[[nodiscard]] bool MicButtonPressed() const noexcept { return holding_noise_btn; }
[[nodiscard]] bool MicButtonJustPressed() const noexcept { return holding_noise_btn && !previous_holding_noise_btn; }
[[nodiscard]] bool MicButtonJustReleased() const noexcept { return !holding_noise_btn && previous_holding_noise_btn; }
[[nodiscard]] bool LidClosed() const noexcept { return lid_closed; }
void Update(const melonds::ScreenLayoutData& screen_layout_data) noexcept;

Expand Down
30 changes: 25 additions & 5 deletions src/libretro/libretro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ namespace melonds {
static InputState input_state;
static ScreenLayoutData screenLayout;
static bool swap_screen_toggled = false;
static bool mic_state_toggled = false;
static bool deferred_initialization_pending = false;
static bool first_frame_run = false;
static std::unique_ptr<NdsCart> _loaded_nds_cart;
Expand Down Expand Up @@ -95,7 +96,7 @@ namespace melonds {
static void ValidateFirmware();

// functions for running games
static void read_microphone(melonds::InputState& input_state) noexcept;
static void read_microphone(melonds::InputState& inputState) noexcept;
static void render_audio();
static void flush_save_data() noexcept;
static void flush_gba_sram(const retro_game_info& gba_save_info) noexcept;
Expand Down Expand Up @@ -370,27 +371,46 @@ PUBLIC_SYMBOL void retro_run(void) {
}
}

static void melonds::read_microphone(melonds::InputState& input_state) noexcept {
static void melonds::read_microphone(melonds::InputState& inputState) noexcept {
MicInputMode mic_input_mode = config::audio::MicInputMode();
MicButtonMode mic_button_mode = config::audio::MicButtonMode();
bool should_mic_be_on = false;

switch (config::audio::MicButtonMode()) {
switch (mic_button_mode) {
// If the microphone button...
case MicButtonMode::Hold: {
// ...must be held...
if (!input_state.MicButtonPressed()) {
mic_state_toggled = false;
if (!inputState.MicButtonPressed()) {
// ...but it isn't...
mic_input_mode = MicInputMode::None;
}
should_mic_be_on = inputState.MicButtonPressed();
break;
}
case MicButtonMode::Toggle: {
// ...must be toggled...
if (inputState.MicButtonJustPressed()) {
// ...but it isn't...
mic_state_toggled = !mic_state_toggled;
if (!mic_state_toggled) {
mic_input_mode = MicInputMode::None;
}
}
should_mic_be_on = mic_state_toggled;
break;
}
case MicButtonMode::Always: {
// ...is unnecessary...
// Do nothing, the mic input mode is already set
should_mic_be_on = true;
break;
}
}

if (retro::microphone::is_open()) {
retro::microphone::set_state(input_state.MicButtonPressed() || config::audio::MicButtonMode() == MicButtonMode::Always);
// TODO: Don't set constantly, only when the mic button state changes (or when the input mode is set to Always)
retro::microphone::set_state(should_mic_be_on);
}

switch (mic_input_mode) {
Expand Down

0 comments on commit db19fab

Please sign in to comment.