From 34506ff2bb1be880cf260e0c1b09efede14ac0d8 Mon Sep 17 00:00:00 2001
From: Arisotura <thetotalworm@gmail.com>
Date: Tue, 19 May 2020 20:48:52 +0200
Subject: actually complete the input config dialog

---
 src/frontend/qt_sdl/Input.h | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100644 src/frontend/qt_sdl/Input.h

(limited to 'src/frontend/qt_sdl/Input.h')

diff --git a/src/frontend/qt_sdl/Input.h b/src/frontend/qt_sdl/Input.h
new file mode 100644
index 0000000..24ec3a7
--- /dev/null
+++ b/src/frontend/qt_sdl/Input.h
@@ -0,0 +1,40 @@
+/*
+    Copyright 2016-2020 Arisotura
+
+    This file is part of melonDS.
+
+    melonDS is free software: you can redistribute it and/or modify it under
+    the terms of the GNU General Public License as published by the Free
+    Software Foundation, either version 3 of the License, or (at your option)
+    any later version.
+
+    melonDS is distributed in the hope that it will be useful, but WITHOUT ANY
+    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+    FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License along
+    with melonDS. If not, see http://www.gnu.org/licenses/.
+*/
+
+#ifndef INPUT_H
+#define INPUT_H
+
+#include "types.h"
+
+namespace Input
+{
+
+extern int JoystickID;
+extern SDL_Joystick* Joystick;
+
+// set joystickID before calling openJoystick()
+void OpenJoystick();
+void CloseJoystick();
+
+void Process();
+
+bool IsRightModKey(QKeyEvent* event);
+
+}
+
+#endif // INPUT_H
-- 
cgit v1.2.3


From b262313816b9fac581353f74dd71e1dde1f23013 Mon Sep 17 00:00:00 2001
From: Arisotura <thetotalworm@gmail.com>
Date: Tue, 19 May 2020 22:22:21 +0200
Subject: actually hook up input to the core

also unbotch CMakeLists.txt
---
 CMakeLists.txt                |   8 +++
 src/frontend/qt_sdl/Input.cpp | 124 +++++++++++++++++++++++++++++++++++++++++-
 src/frontend/qt_sdl/Input.h   |   7 +++
 src/frontend/qt_sdl/main.cpp  |  14 ++++-
 src/frontend/qt_sdl/main.h    |   1 +
 5 files changed, 150 insertions(+), 4 deletions(-)

(limited to 'src/frontend/qt_sdl/Input.h')

diff --git a/CMakeLists.txt b/CMakeLists.txt
index ee021d9..e640a48 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -14,6 +14,14 @@ if(NOT CMAKE_BUILD_TYPE)
   set(CMAKE_BUILD_TYPE Release)
 endif()
 
+if (CMAKE_BUILD_TYPE STREQUAL Debug)
+	add_compile_options(-Og)
+endif()
+
+if (CMAKE_BUILD_TYPE STREQUAL Release)
+	add_compile_options(-O3)
+endif()
+
 add_compile_options(-fno-pic)
 add_link_options(-no-pie)
 
diff --git a/src/frontend/qt_sdl/Input.cpp b/src/frontend/qt_sdl/Input.cpp
index 7caf24a..d60271c 100644
--- a/src/frontend/qt_sdl/Input.cpp
+++ b/src/frontend/qt_sdl/Input.cpp
@@ -34,6 +34,21 @@ u32 KeyHotkeyMask, JoyHotkeyMask;
 u32 HotkeyMask, LastHotkeyMask;
 u32 HotkeyPress, HotkeyRelease;
 
+u32 InputMask;
+
+
+void Init()
+{
+    KeyInputMask = 0xFFF;
+    JoyInputMask = 0xFFF;
+    InputMask = 0xFFF;
+
+    KeyHotkeyMask = 0;
+    JoyHotkeyMask = 0;
+    HotkeyMask = 0;
+    LastHotkeyMask = 0;
+}
+
 
 void OpenJoystick()
 {
@@ -62,6 +77,109 @@ void CloseJoystick()
 }
 
 
+int GetEventKeyVal(QKeyEvent* event)
+{
+    int key = event->key();
+    int mod = event->modifiers();
+    bool ismod = (key == Qt::Key_Control ||
+                  key == Qt::Key_Alt ||
+                  key == Qt::Key_AltGr ||
+                  key == Qt::Key_Shift ||
+                  key == Qt::Key_Meta);
+
+    if (!ismod)
+        key |= mod;
+    else if (Input::IsRightModKey(event))
+        key |= (1<<31);
+
+    return key;
+}
+
+void KeyPress(QKeyEvent* event)
+{
+    int keyHK = GetEventKeyVal(event);
+    int keyKP = keyHK & ~event->modifiers();
+
+    for (int i = 0; i < 12; i++)
+        if (keyKP == Config::KeyMapping[i])
+            KeyInputMask &= ~(1<<i);
+
+    for (int i = 0; i < HK_MAX; i++)
+        if (keyHK == Config::HKKeyMapping[i])
+            KeyHotkeyMask |= (1<<i);
+}
+
+void KeyRelease(QKeyEvent* event)
+{
+    int keyHK = GetEventKeyVal(event);
+    int keyKP = keyHK & ~event->modifiers();
+
+    for (int i = 0; i < 12; i++)
+        if (keyKP == Config::KeyMapping[i])
+            KeyInputMask |= (1<<i);
+
+    for (int i = 0; i < HK_MAX; i++)
+        if (keyHK == Config::HKKeyMapping[i])
+            KeyHotkeyMask &= ~(1<<i);
+}
+
+
+bool JoystickButtonDown(int val)
+{
+    if (val == -1) return false;
+
+    bool hasbtn = ((val & 0xFFFF) != 0xFFFF);
+
+    if (hasbtn)
+    {
+        if (val & 0x100)
+        {
+            int hatnum = (val >> 4) & 0xF;
+            int hatdir = val & 0xF;
+            Uint8 hatval = SDL_JoystickGetHat(Joystick, hatnum);
+
+            bool pressed = false;
+            if      (hatdir == 0x1) pressed = (hatval & SDL_HAT_UP);
+            else if (hatdir == 0x4) pressed = (hatval & SDL_HAT_DOWN);
+            else if (hatdir == 0x2) pressed = (hatval & SDL_HAT_RIGHT);
+            else if (hatdir == 0x8) pressed = (hatval & SDL_HAT_LEFT);
+
+            if (pressed) return true;
+        }
+        else
+        {
+            int btnnum = val & 0xFFFF;
+            Uint8 btnval = SDL_JoystickGetButton(Joystick, btnnum);
+
+            if (btnval) return true;
+        }
+    }
+
+    if (val & 0x10000)
+    {
+        int axisnum = (val >> 24) & 0xF;
+        int axisdir = (val >> 20) & 0xF;
+        Sint16 axisval = SDL_JoystickGetAxis(Joystick, axisnum);
+
+        switch (axisdir)
+        {
+        case 0: // positive
+            if (axisval > 16384) return true;
+            break;
+
+        case 1: // negative
+            if (axisval < -16384) return true;
+            break;
+
+        case 2: // trigger
+            if (axisval > 0) return true;
+            break;
+        }
+    }
+
+    return false;
+}
+
 void Process()
 {
     SDL_JoystickUpdate();
@@ -80,11 +198,13 @@ void Process()
         OpenJoystick();
     }
 
-    /*JoyInputMask = 0xFFF;
+    JoyInputMask = 0xFFF;
     for (int i = 0; i < 12; i++)
         if (JoystickButtonDown(Config::JoyMapping[i]))
             JoyInputMask &= ~(1<<i);
 
+    InputMask = KeyInputMask & JoyInputMask;
+
     JoyHotkeyMask = 0;
     for (int i = 0; i < HK_MAX; i++)
         if (JoystickButtonDown(Config::HKJoyMapping[i]))
@@ -93,7 +213,7 @@ void Process()
     HotkeyMask = KeyHotkeyMask | JoyHotkeyMask;
     HotkeyPress = HotkeyMask & ~LastHotkeyMask;
     HotkeyRelease = LastHotkeyMask & ~HotkeyMask;
-    LastHotkeyMask = HotkeyMask;*/
+    LastHotkeyMask = HotkeyMask;
 }
 
 
diff --git a/src/frontend/qt_sdl/Input.h b/src/frontend/qt_sdl/Input.h
index 24ec3a7..57b5a4a 100644
--- a/src/frontend/qt_sdl/Input.h
+++ b/src/frontend/qt_sdl/Input.h
@@ -27,10 +27,17 @@ namespace Input
 extern int JoystickID;
 extern SDL_Joystick* Joystick;
 
+extern u32 InputMask;
+
+void Init();
+
 // set joystickID before calling openJoystick()
 void OpenJoystick();
 void CloseJoystick();
 
+void KeyPress(QKeyEvent* event);
+void KeyRelease(QKeyEvent* event);
+
 void Process();
 
 bool IsRightModKey(QKeyEvent* event);
diff --git a/src/frontend/qt_sdl/main.cpp b/src/frontend/qt_sdl/main.cpp
index a68f933..962fdaf 100644
--- a/src/frontend/qt_sdl/main.cpp
+++ b/src/frontend/qt_sdl/main.cpp
@@ -135,6 +135,7 @@ void EmuThread::run()
         GPU3D::InitRenderer(false);
     }
 
+    Input::Init();
     /*Touching = false;
     LidStatus = false;*/
 
@@ -184,7 +185,7 @@ void EmuThread::run()
             EmuStatus = 1;
 
             // process input and hotkeys
-            NDS::SetKeyMask(0xFFF);
+            NDS::SetKeyMask(Input::InputMask);
             /*NDS::SetKeyMask(KeyInputMask & JoyInputMask);
 
             if (HotkeyPressed(HK_Lid))
@@ -565,7 +566,16 @@ MainWindow::~MainWindow()
 
 void MainWindow::keyPressEvent(QKeyEvent* event)
 {
-    printf("key press. %d %d %08X %08X\n", event->key(), event->nativeScanCode(), event->modifiers(), event->nativeModifiers());
+    if (event->isAutoRepeat()) return;
+
+    Input::KeyPress(event);
+}
+
+void MainWindow::keyReleaseEvent(QKeyEvent* event)
+{
+    if (event->isAutoRepeat()) return;
+
+    Input::KeyRelease(event);
 }
 
 
diff --git a/src/frontend/qt_sdl/main.h b/src/frontend/qt_sdl/main.h
index 4553875..a3125cc 100644
--- a/src/frontend/qt_sdl/main.h
+++ b/src/frontend/qt_sdl/main.h
@@ -87,6 +87,7 @@ public:
 
 protected:
     void keyPressEvent(QKeyEvent* event) override;
+    void keyReleaseEvent(QKeyEvent* event) override;
 
 private slots:
     void onOpenFile();
-- 
cgit v1.2.3


From 95f9698077fe50d2567aacd8728ff2625f88f228 Mon Sep 17 00:00:00 2001
From: Arisotura <thetotalworm@gmail.com>
Date: Tue, 19 May 2020 22:37:48 +0200
Subject: add back some hotkeys. remove some legacy cruft from NDS.cpp.

---
 src/NDS.cpp                   | 18 ++++++++--------
 src/NDS.h                     |  3 +--
 src/frontend/qt_sdl/Input.cpp |  5 +++++
 src/frontend/qt_sdl/Input.h   |  4 ++++
 src/frontend/qt_sdl/main.cpp  | 48 ++++++++++++++++++++-----------------------
 5 files changed, 40 insertions(+), 38 deletions(-)

(limited to 'src/frontend/qt_sdl/Input.h')

diff --git a/src/NDS.cpp b/src/NDS.cpp
index 745ed28..e89aa66 100644
--- a/src/NDS.cpp
+++ b/src/NDS.cpp
@@ -951,23 +951,15 @@ void CancelEvent(u32 id)
 }
 
 
-void PressKey(u32 key)
-{
-    KeyInput &= ~(1 << key);
-}
-
-void ReleaseKey(u32 key)
-{
-    KeyInput |= (1 << key);
-}
-
 void TouchScreen(u16 x, u16 y)
 {
+    KeyInput &= ~(1<<22);
     SPI_TSC::SetTouchCoords(x, y);
 }
 
 void ReleaseScreen()
 {
+    KeyInput |= (1<<22);
     SPI_TSC::SetTouchCoords(0x000, 0xFFF);
 }
 
@@ -981,6 +973,12 @@ void SetKeyMask(u32 mask)
     KeyInput |= key_lo | (key_hi << 16);
 }
 
+bool IsLidClosed()
+{
+    if (KeyInput & (1<<23)) return true;
+    return false;
+}
+
 void SetLidClosed(bool closed)
 {
     if (closed)
diff --git a/src/NDS.h b/src/NDS.h
index c7b455e..daeadc4 100644
--- a/src/NDS.h
+++ b/src/NDS.h
@@ -142,13 +142,12 @@ void RelocateSave(const char* path, bool write);
 
 u32 RunFrame();
 
-void PressKey(u32 key);
-void ReleaseKey(u32 key);
 void TouchScreen(u16 x, u16 y);
 void ReleaseScreen();
 
 void SetKeyMask(u32 mask);
 
+bool IsLidClosed();
 void SetLidClosed(bool closed);
 
 void MicInputFrame(s16* data, int samples);
diff --git a/src/frontend/qt_sdl/Input.cpp b/src/frontend/qt_sdl/Input.cpp
index d60271c..84d20ad 100644
--- a/src/frontend/qt_sdl/Input.cpp
+++ b/src/frontend/qt_sdl/Input.cpp
@@ -217,6 +217,11 @@ void Process()
 }
 
 
+bool HotkeyDown(int id)     { return HotkeyMask    & (1<<id); }
+bool HotkeyPressed(int id)  { return HotkeyPress   & (1<<id); }
+bool HotkeyReleased(int id) { return HotkeyRelease & (1<<id); }
+
+
 // TODO: MacOS version of this!
 // distinguish between left and right modifier keys (Ctrl, Alt, Shift)
 // Qt provides no real cross-platform way to do this, so here we go
diff --git a/src/frontend/qt_sdl/Input.h b/src/frontend/qt_sdl/Input.h
index 57b5a4a..14e7ea8 100644
--- a/src/frontend/qt_sdl/Input.h
+++ b/src/frontend/qt_sdl/Input.h
@@ -40,6 +40,10 @@ void KeyRelease(QKeyEvent* event);
 
 void Process();
 
+bool HotkeyDown(int id);
+bool HotkeyPressed(int id);
+bool HotkeyReleased(int id);
+
 bool IsRightModKey(QKeyEvent* event);
 
 }
diff --git a/src/frontend/qt_sdl/main.cpp b/src/frontend/qt_sdl/main.cpp
index 962fdaf..67ac92c 100644
--- a/src/frontend/qt_sdl/main.cpp
+++ b/src/frontend/qt_sdl/main.cpp
@@ -42,6 +42,7 @@
 #include "FrontendUtil.h"
 
 #include "NDS.h"
+#include "GBACart.h"
 #include "GPU.h"
 #include "SPU.h"
 #include "Wifi.h"
@@ -136,8 +137,7 @@ void EmuThread::run()
     }
 
     Input::Init();
-    /*Touching = false;
-    LidStatus = false;*/
+    /*Touching = false;*/
 
     u32 nframes = 0;
     u32 starttick = SDL_GetTicks();
@@ -150,35 +150,33 @@ void EmuThread::run()
     while (EmuRunning != 0)
     {
         Input::Process();
-        /*ProcessInput();
 
-        if (HotkeyPressed(HK_FastForwardToggle))
+        /*if (Input::HotkeyPressed(HK_FastForwardToggle))
         {
             Config::LimitFPS = !Config::LimitFPS;
-            uiQueueMain(UpdateFPSLimit, NULL);
-        }
-        // TODO: similar hotkeys for video/audio sync?
+            // TODO: reflect in UI!
+        }*/
 
-        if (HotkeyPressed(HK_Pause)) uiQueueMain(TogglePause, NULL);
-        if (HotkeyPressed(HK_Reset)) uiQueueMain(Reset, NULL);
+        //if (Input::HotkeyPressed(HK_Pause)) uiQueueMain(TogglePause, NULL);
+        //if (Input::HotkeyPressed(HK_Reset)) uiQueueMain(Reset, NULL);
 
         if (GBACart::CartInserted && GBACart::HasSolarSensor)
         {
-            if (HotkeyPressed(HK_SolarSensorDecrease))
+            if (Input::HotkeyPressed(HK_SolarSensorDecrease))
             {
                 if (GBACart_SolarSensor::LightLevel > 0) GBACart_SolarSensor::LightLevel--;
-                char msg[64];
-                sprintf(msg, "Solar sensor level set to %d", GBACart_SolarSensor::LightLevel);
-                OSD::AddMessage(0, msg);
+                //char msg[64];
+                //sprintf(msg, "Solar sensor level set to %d", GBACart_SolarSensor::LightLevel);
+                //OSD::AddMessage(0, msg);
             }
-            if (HotkeyPressed(HK_SolarSensorIncrease))
+            if (Input::HotkeyPressed(HK_SolarSensorIncrease))
             {
                 if (GBACart_SolarSensor::LightLevel < 10) GBACart_SolarSensor::LightLevel++;
-                char msg[64];
-                sprintf(msg, "Solar sensor level set to %d", GBACart_SolarSensor::LightLevel);
-                OSD::AddMessage(0, msg);
+                //char msg[64];
+                //sprintf(msg, "Solar sensor level set to %d", GBACart_SolarSensor::LightLevel);
+                //OSD::AddMessage(0, msg);
             }
-        }*/
+        }
 
         if (EmuRunning == 1)
         {
@@ -186,14 +184,13 @@ void EmuThread::run()
 
             // process input and hotkeys
             NDS::SetKeyMask(Input::InputMask);
-            /*NDS::SetKeyMask(KeyInputMask & JoyInputMask);
 
-            if (HotkeyPressed(HK_Lid))
+            if (Input::HotkeyPressed(HK_Lid))
             {
-                LidStatus = !LidStatus;
-                NDS::SetLidClosed(LidStatus);
-                OSD::AddMessage(0, LidStatus ? "Lid closed" : "Lid opened");
-            }*/
+                bool lid = !NDS::IsLidClosed();
+                NDS::SetLidClosed(lid);
+                //OSD::AddMessage(0, lid ? "Lid closed" : "Lid opened");
+            }
 
             // microphone input
             /*FeedMicInput();
@@ -250,8 +247,7 @@ void EmuThread::run()
             uiAreaQueueRedrawAll(MainDrawArea);*/
             mainWindow->update();
 
-            bool fastforward = false;
-            //bool fastforward = HotkeyDown(HK_FastForward);
+            bool fastforward = Input::HotkeyDown(HK_FastForward);
 
             if (Config::AudioSync && (!fastforward) && audioDevice)
             {
-- 
cgit v1.2.3