Maelstrom: Notify other players when we're minimized and pause the game.

https://github.com/libsdl-org/Maelstrom/commit/495ad585dced4c8bf4c2c6dba68ed9950d049113

From 495ad585dced4c8bf4c2c6dba68ed9950d049113 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 8 Nov 2011 01:02:34 -0500
Subject: [PATCH] Notify other players when we're minimized and pause the game.
 On iOS once we do this the game can't send packets anymore, so don't abort
 the game if we can't do a network sync while this happens.  Unfortunately
 this means right now that once a player is minimized the entire game is no
 longer processing packets or commands, so you can't quit or anything.
 (FIXME!)

---
 controls.cpp        |  8 ++++++++
 controls.h          |  1 +
 netlogic/game.cpp   |  6 ++++++
 netlogic/player.cpp | 49 ++++++++++++++++++++++++++++++++++-----------
 4 files changed, 52 insertions(+), 12 deletions(-)

diff --git a/controls.cpp b/controls.cpp
index 05bf645b..0b558902 100644
--- a/controls.cpp
+++ b/controls.cpp
@@ -342,6 +342,14 @@ static void HandleEvent(SDL_Event *event)
 			}
 			break;
 
+		case SDL_WINDOWEVENT:
+			if (event->window.event == SDL_WINDOWEVENT_MINIMIZED) {
+				SetControl(MINIMIZE_KEY, 1);
+			} if (event->window.event == SDL_WINDOWEVENT_RESTORED) {
+				SetControl(MINIMIZE_KEY, 0);
+			}
+			break;
+
 		case SDL_QUIT:
 			SetControl(ABORT_KEY, 1);
 			break;
diff --git a/controls.h b/controls.h
index 99d6b168..1d811b35 100644
--- a/controls.h
+++ b/controls.h
@@ -44,6 +44,7 @@ extern void	ShowDawn(void);
 #define FIRE_KEY	0x05
 #define PAUSE_KEY	0x06
 #define ABORT_KEY	0x07
+#define MINIMIZE_KEY	0x08
 
 /* The controls structure */
 class Controls
diff --git a/netlogic/game.cpp b/netlogic/game.cpp
index 9676bb93..e401b9c3 100644
--- a/netlogic/game.cpp
+++ b/netlogic/game.cpp
@@ -220,6 +220,12 @@ GamePanelDelegate::OnTick()
 
 	/* -- Send Sync! signal to all players, and handle keyboard. */
 	if ( SyncNetwork() < 0 ) {
+		if ( gPaused & ~0x1 ) {
+			/* One of the other players is minimized and may not
+			   be able to send packets (iOS), so don't abort yet.
+			*/
+			return;
+		}
 		error("Game aborted!\n");
 		gGameOn = 0;
 		return;
diff --git a/netlogic/player.cpp b/netlogic/player.cpp
index 7c3e172e..4da89b52 100644
--- a/netlogic/player.cpp
+++ b/netlogic/player.cpp
@@ -482,6 +482,32 @@ printf("\n");
 	return(Object::Move(Freeze));
 }
 
+static void
+SetPaused(int bit, bool enabled)
+{
+	if (enabled) {
+		sound->PlaySound(gPauseSound, 5);
+		gPaused |= bit;
+	} else {
+		gPaused &= ~bit;
+	}
+}
+static void
+TogglePause()
+{
+	if ( gPaused & 0x01 ) {
+		SetPaused(0x01, false);
+	} else {
+		SetPaused(0x01, true);
+	}
+}
+static void
+SetMinimized(int index, bool enabled)
+{
+	int bit = (1 << (1+index));
+	SetPaused(bit, enabled);
+}
+
 void
 Player::HandleKeys(void)
 {
@@ -498,12 +524,10 @@ Player::HandleKeys(void)
 				/* Only handle Pause and Abort while dead */
 				if ( ! Alive() || Exploding ) {
 					if ( inbuf[++i] == PAUSE_KEY ) {
-						if ( gPaused > 0 ) {
-							--gPaused;
-						} else {
-							sound->PlaySound(gPauseSound, 5);
-							++gPaused;
-						}
+						TogglePause();
+					}
+					if ( inbuf[i] == MINIMIZE_KEY ) {
+						SetMinimized(Index, true);
 					}
 					if ( inbuf[i] == ABORT_KEY )
 						gGameOn = 0;
@@ -527,12 +551,10 @@ Player::HandleKeys(void)
 						Shooting = 1;
 						break;
 					case PAUSE_KEY:
-						if ( gPaused > 0 ) {
-							--gPaused;
-						} else {
-							sound->PlaySound(gPauseSound, 5);
-							++gPaused;
-						}
+						TogglePause();
+						break;
+					case MINIMIZE_KEY:
+						SetMinimized(Index, true);
 						break;
 					case ABORT_KEY:
 						gGameOn = 0;
@@ -561,6 +583,9 @@ Player::HandleKeys(void)
 					case FIRE_KEY:
 						Shooting = 0;
 						break;
+					case MINIMIZE_KEY:
+						SetMinimized(Index, false);
+						break;
 					case ABORT_KEY:
 						/* Do nothing on release */;
 						break;