gui: fix hardware keyboard

- Power+voldown works again for taking screenshots
- Right shift key isn't stuck until users presses "s"
- Don't use DataManager for local variables
- Simplify handling of Shift keys
- Clean up #includes a bit

Change-Id: Iff0453107beaa336a9c04422b43e92225fdbe069
diff --git a/gui/hardwarekeyboard.cpp b/gui/hardwarekeyboard.cpp
index 6d5da6b..1f34c5e 100644
--- a/gui/hardwarekeyboard.cpp
+++ b/gui/hardwarekeyboard.cpp
@@ -5,12 +5,10 @@
 #include <stdlib.h>
 #include <string.h>
 #include <fcntl.h>
-#include <sys/reboot.h>
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/mman.h>
 #include <sys/types.h>
-#include <sys/ioctl.h>
 #include <time.h>
 #include <unistd.h>
 #include <stdlib.h>
@@ -19,50 +17,52 @@
 
 extern "C" {
 #include "../common.h"
-#include "../minuitwrp/minui.h"
-#include "../recovery_ui.h"
 }
 
-#include "rapidxml.hpp"
 #include "objects.hpp"
-#include "../data.hpp"
 #include <linux/input.h>
 
-HardwareKeyboard::HardwareKeyboard(void) {
-	// Do Nothing
-	DataManager::SetValue("Lshift_down", 0);
-	DataManager::SetValue("Rshift_down", 0);
-	DataManager::SetValue("last_key", 0);
+HardwareKeyboard::HardwareKeyboard()
+ : mLastKeyChar(0)
+{
 }
 
-HardwareKeyboard::~HardwareKeyboard() {
-	// Do Nothing
+HardwareKeyboard::~HardwareKeyboard()
+{
 }
 
-int HardwareKeyboard::KeyDown(int key_code) {
+// Map keys to other keys.
+static int TranslateKeyCode(int key_code)
+{
+	switch (key_code) {
+		case KEY_HOMEPAGE: // Home key on Asus Transformer hardware keyboard
+			return KEY_HOME;
+		case KEY_SLEEP: // Lock key on Asus Transformer hardware keyboard
+			return KEY_POWER;
+	}
+	return key_code;
+}
 
-	int keyboard = -1;
-	int shiftkey, Lshift_down, Rshift_down;
-
-	DataManager::GetValue("Lshift_down", Lshift_down);
-	DataManager::GetValue("Rshift_down", Rshift_down);
-	if (Lshift_down || Rshift_down)
-		shiftkey = 1;
-	else
-		shiftkey = 0;
-
+int HardwareKeyboard::KeyDown(int key_code)
+{
 #ifdef _EVENT_LOGGING
 	LOGE("HardwareKeyboard::KeyDown %i\n", key_code);
 #endif
+	key_code = TranslateKeyCode(key_code);
+
+	// determine if any Shift key is held down
+	bool shiftkey = false;
+	std::set<int>::iterator it = mPressedKeys.find(KEY_LEFTSHIFT);
+	if (it == mPressedKeys.end())
+		it = mPressedKeys.find(KEY_RIGHTSHIFT);
+	if (it != mPressedKeys.end())
+		shiftkey = true;
+
+	mPressedKeys.insert(key_code);
+
+	int keyboard = -1;
+
 	switch (key_code) {
-		case KEY_LEFTSHIFT: // Left Shift
-			DataManager::SetValue("Lshift_down", 1);
-			return 0;
-			break;
-		case KEY_RIGHTSHIFT: // Right Shift
-			DataManager::SetValue("Rshift_down", 1);
-			return 0;
-			break;
 		case KEY_A:
 			if (shiftkey)
 				keyboard = 'A';
@@ -366,23 +366,7 @@
 		case KEY_RIGHT: // Right arrow
 			keyboard = KEYBOARD_ARROW_RIGHT;
 			break;
-		case KEY_BACK: // back button on screen
-			mPressedKeys.insert(KEY_BACK);
-			PageManager::NotifyKey(KEY_BACK, true);
-			return 0;
-			break;
-		case KEY_HOMEPAGE: // keyboard home button
-		case KEY_HOME: // home button on screen
-			mPressedKeys.insert(KEY_HOME);
-			PageManager::NotifyKey(KEY_HOME, true);
-			return 0;
-			break;
-		case KEY_SLEEP: // keyboard lock button
-		case KEY_POWER: // tablet power button
-			mPressedKeys.insert(KEY_POWER);
-			PageManager::NotifyKey(KEY_POWER, true);
-			return 0;
-			break;
+
 #ifdef _EVENT_LOGGING
 		default:
 			LOGE("Unmapped keycode: %i\n", key_code);
@@ -390,45 +374,43 @@
 #endif
 	}
 	if (keyboard != -1) {
-		DataManager::SetValue("last_key", keyboard);
-		mPressedKeys.insert(keyboard);
+		mLastKeyChar = keyboard;
+		// NotifyKeyboard means: "report character to input widget". KEYBOARD_* codes are special, others are ASCII chars.
 		if (!PageManager::NotifyKeyboard(keyboard))
 			return 1;  // Return 1 to enable key repeat
 	} else {
-		DataManager::SetValue("last_key", 0);
+		mLastKeyChar = 0;
+		PageManager::NotifyKey(key_code, true);
 	}
 	return 0;
 }
 
-int HardwareKeyboard::KeyUp(int key_code) {
+int HardwareKeyboard::KeyUp(int key_code)
+{
+#ifdef _EVENT_LOGGING
+	LOGE("HardwareKeyboard::KeyUp %i\n", key_code);
+#endif
+	key_code = TranslateKeyCode(key_code);
 	std::set<int>::iterator itr = mPressedKeys.find(key_code);
 	if (itr != mPressedKeys.end()) {
 		mPressedKeys.erase(itr);
 		PageManager::NotifyKey(key_code, false);
 	}
-#ifdef _EVENT_LOGGING
-	LOGE("HardwareKeyboard::KeyUp %i\n", key_code);
-#endif
-	if (key_code == KEY_LEFTSHIFT) { // Left Shift
-		DataManager::SetValue("Lshift_down", 0);
-	} else if (key_code == 31) { // Right Shift
-		DataManager::SetValue("Rshift_down", 0);
-	}
 	return 0;
 }
 
-int HardwareKeyboard::KeyRepeat(void) {
-	int last_key;
-
-	DataManager::GetValue("last_key", last_key);
+int HardwareKeyboard::KeyRepeat()
+{
 #ifdef _EVENT_LOGGING
-	LOGE("HardwareKeyboard::KeyRepeat: %i\n", last_key);
+	LOGE("HardwareKeyboard::KeyRepeat: %i\n", mLastKeyChar);
 #endif
-	if (last_key)
-		PageManager::NotifyKeyboard(last_key);
+	if (mLastKeyChar)
+		PageManager::NotifyKeyboard(mLastKeyChar);
 	return 0;
 }
 
-void HardwareKeyboard::ConsumeKeyRelease(int key) {
+void HardwareKeyboard::ConsumeKeyRelease(int key)
+{
+	// causes following KeyUp event to suppress notifications
 	mPressedKeys.erase(key);
 }
diff --git a/gui/objects.hpp b/gui/objects.hpp
index c95a935..1d20c2a 100644
--- a/gui/objects.hpp
+++ b/gui/objects.hpp
@@ -992,17 +992,22 @@
 class HardwareKeyboard
 {
 public:
-	HardwareKeyboard(void);
+	HardwareKeyboard();
 	virtual ~HardwareKeyboard();
 
 public:
-	virtual int KeyDown(int key_code);
-	virtual int KeyUp(int key_code);
-	virtual int KeyRepeat(void);
+	// called by the input handler for key events
+	int KeyDown(int key_code);
+	int KeyUp(int key_code);
 
+	// called by the input handler when holding a key down
+	int KeyRepeat();
+
+	// called by multi-key actions to suppress key-release notifications
 	void ConsumeKeyRelease(int key);
 
 private:
+	int mLastKeyChar;
 	std::set<int> mPressedKeys;
 };
 
diff --git a/gui/resources.hpp b/gui/resources.hpp
index 1a86d54..603b1c9 100644
--- a/gui/resources.hpp
+++ b/gui/resources.hpp
@@ -5,6 +5,10 @@
 
 #include "../minzip/Zip.h"
 
+extern "C" {
+#include "../minuitwrp/minui.h"
+}
+
 // Base Objects
 class Resource
 {