From b16813e33729c9418223c53b8f0a1d37e19386c2 Mon Sep 17 00:00:00 2001
From: Rudolf Polzer <divVerent@xonotic.org>
Date: Thu, 2 Nov 2017 16:58:55 +0100
Subject: [PATCH] Manage resetting the key event state on video
 reinitialization centrally.

Fixes:
- No key state clear when SDL gets restarted.
- Key state clear was the wrong action - it allows all keys to get pressed
  again, but does not actually release anything in cl_input.c, so e.g. +jump
  binds stay pressed on vid_restart.

Only vid_wgl.c retains some vid-specific handling (reset of key state when
alt-tabbing out and back in) - there's a chance that we may need similar logic
in the other vid drivers; if so, it may be possible to do this in a shared
place too by watching for changes to vid_activewindow.
---
 keys.c       | 11 -----------
 keys.h       |  1 -
 vid_agl.c    |  1 -
 vid_glx.c    |  1 -
 vid_shared.c |  2 ++
 vid_wgl.c    |  2 +-
 6 files changed, 3 insertions(+), 15 deletions(-)

diff --git a/keys.c b/keys.c
index d233471b..2f91c06c 100644
--- a/keys.c
+++ b/keys.c
@@ -1989,14 +1989,3 @@ Key_ReleaseAll (void)
 	// now all keys are guaranteed down (once the event queue is unblocked)
 	// and only future events count
 }
-
-/*
-===================
-Key_ClearStates
-===================
-*/
-void
-Key_ClearStates (void)
-{
-	memset(keydown, 0, sizeof(keydown));
-}
diff --git a/keys.h b/keys.h
index a84eb4c6..7841e9d0 100644
--- a/keys.h
+++ b/keys.h
@@ -378,7 +378,6 @@ void Key_Shutdown(void);
 void Key_Init_Cvars(void);
 void Key_Event(int key, int ascii, qboolean down);
 void Key_ReleaseAll (void);
-void Key_ClearStates (void); // FIXME: should this function still exist? Or should Key_ReleaseAll be used instead when shutting down a vid driver?
 void Key_EventQueue_Block(void);
 void Key_EventQueue_Unblock(void);
 
diff --git a/vid_agl.c b/vid_agl.c
index bacbb5ff..7a524f4d 100644
--- a/vid_agl.c
+++ b/vid_agl.c
@@ -419,7 +419,6 @@ void VID_Shutdown(void)
 	vid_isfullscreen = false;
 
 	GL_CloseLibrary();
-	Key_ClearStates ();
 }
 
 // Since the event handler can be called at any time, we store the events for later processing
diff --git a/vid_glx.c b/vid_glx.c
index 8327b260..4e70e069 100644
--- a/vid_glx.c
+++ b/vid_glx.c
@@ -887,7 +887,6 @@ void VID_Shutdown(void)
 	ctx = NULL;
 
 	GL_CloseLibrary();
-	Key_ClearStates ();
 }
 
 static void signal_handler(int sig)
diff --git a/vid_shared.c b/vid_shared.c
index 843707b4..7503c12f 100644
--- a/vid_shared.c
+++ b/vid_shared.c
@@ -1941,6 +1941,7 @@ static int VID_Mode(int fullscreen, int width, int height, int bpp, float refres
 
 static void VID_OpenSystems(void)
 {
+	Key_ReleaseAll();
 	R_Modules_Start();
 	S_Startup();
 }
@@ -1949,6 +1950,7 @@ static void VID_CloseSystems(void)
 {
 	S_Shutdown();
 	R_Modules_Shutdown();
+	Key_ReleaseAll();
 }
 
 qboolean vid_commandlinecheck = true;
diff --git a/vid_wgl.c b/vid_wgl.c
index 24bcdabc..86feacbb 100644
--- a/vid_wgl.c
+++ b/vid_wgl.c
@@ -429,7 +429,7 @@ ClearAllStates
 */
 static void ClearAllStates (void)
 {
-	Key_ClearStates ();
+	Key_ReleaseAll();
 	if (vid_usingmouse)
 		mouse_oldbuttonstate = 0;
 }
-- 
2.39.5