Skip to content

Commit

Permalink
Cvars to mute when client windows is unfocused/minimized (closes #143)
Browse files Browse the repository at this point in the history
s_muteWhenMinimized, s_muteWhenUnfocused
  • Loading branch information
aufau committed Jul 4, 2021
1 parent 7617f09 commit 50a83b2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
16 changes: 16 additions & 0 deletions CVARS.rst
Expand Up @@ -284,6 +284,22 @@ Client-Side
quality video drivers. Small negative values (eg "-0.2") can help
with distant textures appearing blurry.

..
:Name: s_muteWhenMinimized
:Values: "0", "1"
:Default: "1"
:Description:
Mute all sounds when client window is minimized.

..
:Name: s_muteWhenUnfocused
:Values: "0", "1"
:Default: "1"
:Description:
Mute all sounds when client window is unfocused.

-----------
Server-Side
-----------
Expand Down
26 changes: 25 additions & 1 deletion src/client/snd_dma.cpp
Expand Up @@ -109,6 +109,7 @@ int numLoopChannels;

static qboolean s_soundStarted;
qboolean s_soundMuted;
static int s_lastMuteModCount;

dma_t dma;

Expand Down Expand Up @@ -139,7 +140,8 @@ cvar_t *s_musicMult;
cvar_t *s_separation;
cvar_t *s_doppler;
cvar_t *s_s_language;

cvar_t *s_muteWhenMinimized;
cvar_t *s_muteWhenUnfocused;

static loopSound_t loopSounds[MAX_GENTITIES];
static channel_t *freelist = NULL;
Expand Down Expand Up @@ -303,6 +305,10 @@ void S_Init( void )
s_testsound = Cvar_Get ("s_testsound", "0", CVAR_CHEAT);

s_s_language = Cvar_Get("s_language", "english", CVAR_ARCHIVE | CVAR_NORESTART | CVAR_GLOBAL);
s_muteWhenUnfocused = Cvar_Get("s_muteWhenUnfocused", "1", CVAR_ARCHIVE | CVAR_GLOBAL);
s_muteWhenMinimized = Cvar_Get("s_muteWhenMinimized", "1", CVAR_ARCHIVE | CVAR_GLOBAL);

s_lastMuteModCount = -1;

MP3_InitCvars();

Expand Down Expand Up @@ -1562,6 +1568,22 @@ void S_Activate(qboolean activate)
}
}

void S_CheckMuteWhenMinimized(void)
{
if (com_minimized->modificationCount +
com_unfocused->modificationCount != s_lastMuteModCount)
{
int disable =
(com_minimized->integer && s_muteWhenMinimized->integer) ||
(com_unfocused->integer && s_muteWhenUnfocused->integer);

S_Activate((qboolean)!disable);

s_lastMuteModCount = com_minimized->modificationCount +
com_unfocused->modificationCount;
}
}

/*
==============================================================
Expand Down Expand Up @@ -2231,6 +2253,8 @@ void S_Update( void ) {
return;
}

S_CheckMuteWhenMinimized();

#ifdef USE_OPENAL
if (s_UseOpenAL)
{
Expand Down
16 changes: 2 additions & 14 deletions src/sdl/sdl_input.cpp
Expand Up @@ -2,7 +2,6 @@
#include "../qcommon/qcommon.h"
#include "../qcommon/q_shared.h"
#include "../client/client.h"
#include "../client/snd_public.h"
#include "../sys/sys_local.h"

static cvar_t *in_keyboardDebug = NULL;
Expand Down Expand Up @@ -854,19 +853,8 @@ static void IN_ProcessEvents( int eventTime )
case SDL_WINDOWEVENT_SHOWN:
case SDL_WINDOWEVENT_RESTORED:
case SDL_WINDOWEVENT_MAXIMIZED: Cvar_SetValue( "com_minimized", 0 ); break;
case SDL_WINDOWEVENT_FOCUS_LOST:
{
Cvar_SetValue( "com_unfocused", 1 );
S_Activate(qfalse);
break;
}

case SDL_WINDOWEVENT_FOCUS_GAINED:
{
Cvar_SetValue( "com_unfocused", 0 );
S_Activate(qtrue);
break;
}
case SDL_WINDOWEVENT_FOCUS_LOST: Cvar_SetValue( "com_unfocused", 1 ); break;
case SDL_WINDOWEVENT_FOCUS_GAINED: Cvar_SetValue( "com_unfocused", 0 ); break;
}
break;

Expand Down
6 changes: 5 additions & 1 deletion src/sdl/sdl_sound.cpp
Expand Up @@ -310,5 +310,9 @@ SNDDMA_BeginPainting
*/
void SNDDMA_Activate(qboolean activate)
{
SDL_PauseAudioDevice(dev, !activate);
qboolean isActive = (qboolean)(SDL_GetAudioDeviceStatus(dev) == SDL_AUDIO_PLAYING);

if (isActive != activate) {
SDL_PauseAudioDevice(dev, !activate);
}
}

0 comments on commit 50a83b2

Please sign in to comment.