Skip to content

Commit

Permalink
* Fix bug that prevented key up events getting to cgame/ui when not i…
Browse files Browse the repository at this point in the history
…n game

* Use Key_[GS]etCatcher everywhere to set keycatcher
* Clear all key states when the catcher changes
  • Loading branch information
timangus committed Oct 2, 2007
1 parent 2664122 commit 5692e30
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 71 deletions.
5 changes: 3 additions & 2 deletions code/client/cl_cgame.c
Expand Up @@ -387,7 +387,7 @@ CL_ShutdonwCGame
====================
*/
void CL_ShutdownCGame( void ) {
cls.keyCatchers &= ~KEYCATCH_CGAME;
Key_SetCatcher( Key_GetCatcher( ) & ~KEYCATCH_CGAME );
cls.cgameStarted = qfalse;
if ( !cgvm ) {
return;
Expand Down Expand Up @@ -607,7 +607,8 @@ intptr_t CL_CgameSystemCalls( intptr_t *args ) {
case CG_KEY_GETCATCHER:
return Key_GetCatcher();
case CG_KEY_SETCATCHER:
Key_SetCatcher( args[1] );
// Don't allow the cgame module to close the console
Key_SetCatcher( args[1] | ( Key_GetCatcher( ) & KEYCATCH_CONSOLE ) );
return 0;
case CG_KEY_GETKEY:
return Key_GetKey( VMA(1) );
Expand Down
26 changes: 13 additions & 13 deletions code/client/cl_console.c
Expand Up @@ -72,15 +72,15 @@ Con_ToggleConsole_f
*/
void Con_ToggleConsole_f (void) {
// Can't toggle the console when it's the only thing available
if ( cls.state == CA_DISCONNECTED && cls.keyCatchers == KEYCATCH_CONSOLE ) {
if ( cls.state == CA_DISCONNECTED && Key_GetCatcher( ) == KEYCATCH_CONSOLE ) {
return;
}

Field_Clear( &g_consoleField );
g_consoleField.widthInChars = g_console_field_width;

Con_ClearNotify ();
cls.keyCatchers ^= KEYCATCH_CONSOLE;
Key_SetCatcher( Key_GetCatcher( ) ^ KEYCATCH_CONSOLE );
}

/*
Expand All @@ -94,7 +94,7 @@ void Con_MessageMode_f (void) {
Field_Clear( &chatField );
chatField.widthInChars = 30;

cls.keyCatchers ^= KEYCATCH_MESSAGE;
Key_SetCatcher( Key_GetCatcher( ) ^ KEYCATCH_MESSAGE );
}

/*
Expand All @@ -107,7 +107,7 @@ void Con_MessageMode2_f (void) {
chat_team = qtrue;
Field_Clear( &chatField );
chatField.widthInChars = 25;
cls.keyCatchers ^= KEYCATCH_MESSAGE;
Key_SetCatcher( Key_GetCatcher( ) ^ KEYCATCH_MESSAGE );
}

/*
Expand All @@ -124,7 +124,7 @@ void Con_MessageMode3_f (void) {
chat_team = qfalse;
Field_Clear( &chatField );
chatField.widthInChars = 30;
cls.keyCatchers ^= KEYCATCH_MESSAGE;
Key_SetCatcher( Key_GetCatcher( ) ^ KEYCATCH_MESSAGE );
}

/*
Expand All @@ -141,7 +141,7 @@ void Con_MessageMode4_f (void) {
chat_team = qfalse;
Field_Clear( &chatField );
chatField.widthInChars = 30;
cls.keyCatchers ^= KEYCATCH_MESSAGE;
Key_SetCatcher( Key_GetCatcher( ) ^ KEYCATCH_MESSAGE );
}

/*
Expand Down Expand Up @@ -477,7 +477,7 @@ Draw the editline after a ] prompt
void Con_DrawInput (void) {
int y;

if ( cls.state != CA_DISCONNECTED && !(cls.keyCatchers & KEYCATCH_CONSOLE ) ) {
if ( cls.state != CA_DISCONNECTED && !(Key_GetCatcher( ) & KEYCATCH_CONSOLE ) ) {
return;
}

Expand Down Expand Up @@ -524,7 +524,7 @@ void Con_DrawNotify (void)
continue;
text = con.text + (i % con.totallines)*con.linewidth;

if (cl.snap.ps.pm_type != PM_INTERMISSION && cls.keyCatchers & (KEYCATCH_UI | KEYCATCH_CGAME) ) {
if (cl.snap.ps.pm_type != PM_INTERMISSION && Key_GetCatcher( ) & (KEYCATCH_UI | KEYCATCH_CGAME) ) {
continue;
}

Expand All @@ -544,12 +544,12 @@ void Con_DrawNotify (void)

re.SetColor( NULL );

if (cls.keyCatchers & (KEYCATCH_UI | KEYCATCH_CGAME) ) {
if (Key_GetCatcher( ) & (KEYCATCH_UI | KEYCATCH_CGAME) ) {
return;
}

// draw the chat line
if ( cls.keyCatchers & KEYCATCH_MESSAGE )
if ( Key_GetCatcher( ) & KEYCATCH_MESSAGE )
{
if (chat_team)
{
Expand Down Expand Up @@ -697,7 +697,7 @@ void Con_DrawConsole( void ) {

// if disconnected, render console full screen
if ( cls.state == CA_DISCONNECTED ) {
if ( !( cls.keyCatchers & (KEYCATCH_UI | KEYCATCH_CGAME)) ) {
if ( !( Key_GetCatcher( ) & (KEYCATCH_UI | KEYCATCH_CGAME)) ) {
Con_DrawSolidConsole( 1.0 );
return;
}
Expand All @@ -724,7 +724,7 @@ Scroll it up or down
*/
void Con_RunConsole (void) {
// decide on the destination height of the console
if ( cls.keyCatchers & KEYCATCH_CONSOLE )
if ( Key_GetCatcher( ) & KEYCATCH_CONSOLE )
con.finalFrac = 0.5; // half screen
else
con.finalFrac = 0; // none visible
Expand Down Expand Up @@ -779,7 +779,7 @@ void Con_Close( void ) {
}
Field_Clear( &g_consoleField );
Con_ClearNotify ();
cls.keyCatchers &= ~KEYCATCH_CONSOLE;
Key_SetCatcher( Key_GetCatcher( ) & ~KEYCATCH_CONSOLE );
con.finalFrac = 0; // none visible
con.displayFrac = 0;
}
8 changes: 4 additions & 4 deletions code/client/cl_input.c
Expand Up @@ -351,9 +351,9 @@ CL_MouseEvent
=================
*/
void CL_MouseEvent( int dx, int dy, int time ) {
if ( cls.keyCatchers & KEYCATCH_UI ) {
if ( Key_GetCatcher( ) & KEYCATCH_UI ) {
VM_Call( uivm, UI_MOUSE_EVENT, dx, dy );
} else if (cls.keyCatchers & KEYCATCH_CGAME) {
} else if (Key_GetCatcher( ) & KEYCATCH_CGAME) {
VM_Call (cgvm, CG_MOUSE_EVENT, dx, dy);
} else {
cl.mouseDx[cl.mouseIndex] += dx;
Expand Down Expand Up @@ -487,13 +487,13 @@ void CL_CmdButtons( usercmd_t *cmd ) {
in_buttons[i].wasPressed = qfalse;
}

if ( cls.keyCatchers ) {
if ( Key_GetCatcher( ) ) {
cmd->buttons |= BUTTON_TALK;
}

// allow the game to know if any key at all is
// currently pressed, even if it isn't bound to anything
if ( anykeydown && !cls.keyCatchers ) {
if ( anykeydown && Key_GetCatcher( ) == 0 ) {
cmd->buttons |= BUTTON_ANY;
}
}
Expand Down
63 changes: 44 additions & 19 deletions code/client/cl_keys.c
Expand Up @@ -720,7 +720,7 @@ void Message_Key( int key ) {


if (key == K_ESCAPE) {
cls.keyCatchers &= ~KEYCATCH_MESSAGE;
Key_SetCatcher( Key_GetCatcher( ) & ~KEYCATCH_MESSAGE );
Field_Clear( &chatField );
return;
}
Expand All @@ -742,7 +742,7 @@ void Message_Key( int key ) {

CL_AddReliableCommand( buffer );
}
cls.keyCatchers &= ~KEYCATCH_MESSAGE;
Key_SetCatcher( Key_GetCatcher( ) & ~KEYCATCH_MESSAGE );
Field_Clear( &chatField );
return;
}
Expand Down Expand Up @@ -1173,7 +1173,8 @@ void CL_KeyEvent (int key, qboolean down, unsigned time) {


// keys can still be used for bound actions
if ( down && ( key < 128 || key == K_MOUSE1 ) && ( clc.demoplaying || cls.state == CA_CINEMATIC ) && !cls.keyCatchers) {
if ( down && ( key < 128 || key == K_MOUSE1 ) &&
( clc.demoplaying || cls.state == CA_CINEMATIC ) && Key_GetCatcher( ) == 0 ) {

if (Cvar_VariableValue ("com_cameraMode") == 0) {
Cvar_Set ("nextdemo","");
Expand All @@ -1184,20 +1185,20 @@ void CL_KeyEvent (int key, qboolean down, unsigned time) {

// escape is always handled special
if ( key == K_ESCAPE && down ) {
if ( cls.keyCatchers & KEYCATCH_MESSAGE ) {
if ( Key_GetCatcher( ) & KEYCATCH_MESSAGE ) {
// clear message mode
Message_Key( key );
return;
}

// escape always gets out of CGAME stuff
if (cls.keyCatchers & KEYCATCH_CGAME) {
cls.keyCatchers &= ~KEYCATCH_CGAME;
if (Key_GetCatcher( ) & KEYCATCH_CGAME) {
Key_SetCatcher( Key_GetCatcher( ) & ~KEYCATCH_CGAME );
VM_Call (cgvm, CG_EVENT_HANDLING, CGAME_EVENT_NONE);
return;
}

if ( !( cls.keyCatchers & KEYCATCH_UI ) ) {
if ( !( Key_GetCatcher( ) & KEYCATCH_UI ) ) {
if ( cls.state == CA_ACTIVE && !clc.demoplaying ) {
VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_INGAME );
}
Expand All @@ -1224,30 +1225,30 @@ void CL_KeyEvent (int key, qboolean down, unsigned time) {
kb = keys[key].binding;

CL_AddKeyUpCommands( key, kb, time );
}

if ( cls.keyCatchers & KEYCATCH_UI && uivm ) {
VM_Call( uivm, UI_KEY_EVENT, key, down );
} else if ( cls.keyCatchers & KEYCATCH_CGAME && cgvm ) {
VM_Call( cgvm, CG_KEY_EVENT, key, down );
}
if ( Key_GetCatcher( ) & KEYCATCH_UI && uivm ) {
VM_Call( uivm, UI_KEY_EVENT, key, down );
} else if ( Key_GetCatcher( ) & KEYCATCH_CGAME && cgvm ) {
VM_Call( cgvm, CG_KEY_EVENT, key, down );
}

return;
}


// distribute the key down event to the apropriate handler
if ( cls.keyCatchers & KEYCATCH_CONSOLE ) {
if ( Key_GetCatcher( ) & KEYCATCH_CONSOLE ) {
Console_Key( key );
} else if ( cls.keyCatchers & KEYCATCH_UI ) {
} else if ( Key_GetCatcher( ) & KEYCATCH_UI ) {
if ( uivm ) {
VM_Call( uivm, UI_KEY_EVENT, key, down );
}
} else if ( cls.keyCatchers & KEYCATCH_CGAME ) {
} else if ( Key_GetCatcher( ) & KEYCATCH_CGAME ) {
if ( cgvm ) {
VM_Call( cgvm, CG_KEY_EVENT, key, down );
}
} else if ( cls.keyCatchers & KEYCATCH_MESSAGE ) {
} else if ( Key_GetCatcher( ) & KEYCATCH_MESSAGE ) {
Message_Key( key );
} else if ( cls.state == CA_DISCONNECTED ) {
Console_Key( key );
Expand Down Expand Up @@ -1315,15 +1316,15 @@ void CL_CharEvent( int key ) {
}

// distribute the key down event to the apropriate handler
if ( cls.keyCatchers & KEYCATCH_CONSOLE )
if ( Key_GetCatcher( ) & KEYCATCH_CONSOLE )
{
Field_CharEvent( &g_consoleField, key );
}
else if ( cls.keyCatchers & KEYCATCH_UI )
else if ( Key_GetCatcher( ) & KEYCATCH_UI )
{
VM_Call( uivm, UI_KEY_EVENT, key | K_CHAR_FLAG, qtrue );
}
else if ( cls.keyCatchers & KEYCATCH_MESSAGE )
else if ( Key_GetCatcher( ) & KEYCATCH_MESSAGE )
{
Field_CharEvent( &chatField, key );
}
Expand Down Expand Up @@ -1355,6 +1356,30 @@ void Key_ClearStates (void)
}
}

static int keyCatchers = 0;

/*
====================
Key_GetCatcher
====================
*/
int Key_GetCatcher( void ) {
return keyCatchers;
}

/*
====================
Key_SetCatcher
====================
*/
void Key_SetCatcher( int catcher ) {
// If the catcher state is changing, clear all key states
if( catcher != keyCatchers )
Key_ClearStates( );

keyCatchers = catcher;
}

// This must not exceed MAX_CMD_LINE
#define MAX_CONSOLE_SAVE_BUFFER 1024
#define CONSOLE_HISTORY_FILE "q3history"
Expand Down
13 changes: 7 additions & 6 deletions code/client/cl_main.c
Expand Up @@ -652,7 +652,7 @@ Closing the main menu will restart the demo loop
void CL_StartDemoLoop( void ) {
// start the demo loop again
Cbuf_AddText ("d1\n");
cls.keyCatchers = 0;
Key_SetCatcher( 0 );
}

/*
Expand Down Expand Up @@ -751,7 +751,7 @@ memory on the hunk from cgame, ui, and renderer
void CL_MapLoading( void ) {
if ( com_dedicated->integer ) {
cls.state = CA_DISCONNECTED;
cls.keyCatchers = KEYCATCH_CONSOLE;
Key_SetCatcher( KEYCATCH_CONSOLE );
return;
}

Expand All @@ -760,7 +760,7 @@ void CL_MapLoading( void ) {
}

Con_Close();
cls.keyCatchers = 0;
Key_SetCatcher( 0 );

// if we are already connected to the local host, stay connected
if ( cls.state >= CA_CONNECTED && !Q_stricmp( cls.servername, "localhost" ) ) {
Expand All @@ -776,7 +776,7 @@ void CL_MapLoading( void ) {
CL_Disconnect( qtrue );
Q_strncpyz( cls.servername, "localhost", sizeof(cls.servername) );
cls.state = CA_CHALLENGING; // so the connect screen is drawn
cls.keyCatchers = 0;
Key_SetCatcher( 0 );
SCR_UpdateScreen();
clc.connectTime = -RETRANSMIT_TIMEOUT;
NET_StringToAdr( cls.servername, &clc.serverAddress);
Expand Down Expand Up @@ -1205,7 +1205,7 @@ void CL_Connect_f( void ) {
cls.state = CA_CONNECTING;
}

cls.keyCatchers = 0;
Key_SetCatcher( 0 );
clc.connectTime = -99999; // CL_CheckForResend() will fire immediately
clc.connectPacketCount = 0;

Expand Down Expand Up @@ -2228,7 +2228,7 @@ void CL_Frame ( int msec ) {
// bring up the cd error dialog if needed
cls.cddialog = qfalse;
VM_Call( uivm, UI_SET_ACTIVE_MENU, UIMENU_NEED_CD );
} else if ( cls.state == CA_DISCONNECTED && !( cls.keyCatchers & KEYCATCH_UI )
} else if ( cls.state == CA_DISCONNECTED && !( Key_GetCatcher( ) & KEYCATCH_UI )
&& !com_sv_running->integer ) {
// if disconnected, bring up the menu
S_StopAllSounds();
Expand Down Expand Up @@ -2857,6 +2857,7 @@ void CL_Shutdown( void ) {
recursive = qfalse;

Com_Memset( &cls, 0, sizeof( cls ) );
Key_SetCatcher( 0 );

Com_Printf( "-----------------------\n" );

Expand Down
2 changes: 1 addition & 1 deletion code/client/cl_scrn.c
Expand Up @@ -490,7 +490,7 @@ void SCR_DrawScreenField( stereoFrame_t stereoFrame ) {
}

// the menu draws next
if ( cls.keyCatchers & KEYCATCH_UI && uivm ) {
if ( Key_GetCatcher( ) & KEYCATCH_UI && uivm ) {
VM_Call( uivm, UI_REFRESH, cls.realtime );
}

Expand Down

0 comments on commit 5692e30

Please sign in to comment.