summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/citra/emu_window/emu_window_glfw.cpp39
-rw-r--r--src/common/CMakeLists.txt2
-rw-r--r--src/common/emu_window.h19
-rw-r--r--src/common/key_map.cpp21
-rw-r--r--src/common/key_map.h35
-rw-r--r--src/core/hle/service/hid.cpp134
-rw-r--r--src/core/hle/service/hid.h87
7 files changed, 310 insertions, 27 deletions
diff --git a/src/citra/emu_window/emu_window_glfw.cpp b/src/citra/emu_window/emu_window_glfw.cpp
index 02f524e03..0a861cff0 100644
--- a/src/citra/emu_window/emu_window_glfw.cpp
+++ b/src/citra/emu_window/emu_window_glfw.cpp
@@ -8,8 +8,35 @@
8 8
9#include "citra/emu_window/emu_window_glfw.h" 9#include "citra/emu_window/emu_window_glfw.h"
10 10
11static void OnKeyEvent(GLFWwindow* win, int key, int action) { 11static const KeyMap::DefaultKeyMapping default_key_map[] = {
12 // TODO(bunnei): ImplementMe 12 { KeyMap::CitraKey(GLFW_KEY_A), HID_User::PAD_A },
13 { KeyMap::CitraKey(GLFW_KEY_B), HID_User::PAD_B },
14 { KeyMap::CitraKey(GLFW_KEY_BACKSLASH), HID_User::PAD_SELECT },
15 { KeyMap::CitraKey(GLFW_KEY_ENTER), HID_User::PAD_START },
16 { KeyMap::CitraKey(GLFW_KEY_RIGHT), HID_User::PAD_RIGHT },
17 { KeyMap::CitraKey(GLFW_KEY_LEFT), HID_User::PAD_LEFT },
18 { KeyMap::CitraKey(GLFW_KEY_UP), HID_User::PAD_UP },
19 { KeyMap::CitraKey(GLFW_KEY_DOWN), HID_User::PAD_DOWN },
20 { KeyMap::CitraKey(GLFW_KEY_R), HID_User::PAD_R },
21 { KeyMap::CitraKey(GLFW_KEY_L), HID_User::PAD_L },
22 { KeyMap::CitraKey(GLFW_KEY_X), HID_User::PAD_X },
23 { KeyMap::CitraKey(GLFW_KEY_Y), HID_User::PAD_Y },
24 { KeyMap::CitraKey(GLFW_KEY_H), HID_User::PAD_CIRCLE_RIGHT },
25 { KeyMap::CitraKey(GLFW_KEY_F), HID_User::PAD_CIRCLE_LEFT },
26 { KeyMap::CitraKey(GLFW_KEY_T), HID_User::PAD_CIRCLE_UP },
27 { KeyMap::CitraKey(GLFW_KEY_G), HID_User::PAD_CIRCLE_DOWN },
28};
29
30
31static void OnKeyEvent(GLFWwindow* win, int key, int scancode, int action, int mods) {
32 if (action == GLFW_PRESS) {
33 EmuWindow::KeyPressed(KeyMap::CitraKey(key));
34 }
35
36 if (action == GLFW_RELEASE) {
37 EmuWindow::KeyReleased(KeyMap::CitraKey(key));
38 }
39 HID_User::PADUpdateComplete();
13} 40}
14 41
15static void OnWindowSizeEvent(GLFWwindow* win, int width, int height) { 42static void OnWindowSizeEvent(GLFWwindow* win, int width, int height) {
@@ -20,6 +47,12 @@ static void OnWindowSizeEvent(GLFWwindow* win, int width, int height) {
20 47
21/// EmuWindow_GLFW constructor 48/// EmuWindow_GLFW constructor
22EmuWindow_GLFW::EmuWindow_GLFW() { 49EmuWindow_GLFW::EmuWindow_GLFW() {
50
51 // Set default key mappings
52 for (int i = 0; i < ARRAY_SIZE(default_key_map); i++) {
53 KeyMap::SetKeyMapping(default_key_map[i].key, default_key_map[i].state);
54 }
55
23 // Initialize the window 56 // Initialize the window
24 if(glfwInit() != GL_TRUE) { 57 if(glfwInit() != GL_TRUE) {
25 printf("Failed to initialize GLFW! Exiting..."); 58 printf("Failed to initialize GLFW! Exiting...");
@@ -45,7 +78,7 @@ EmuWindow_GLFW::EmuWindow_GLFW() {
45 78
46 // Setup callbacks 79 // Setup callbacks
47 glfwSetWindowUserPointer(m_render_window, this); 80 glfwSetWindowUserPointer(m_render_window, this);
48 //glfwSetKeyCallback(m_render_window, OnKeyEvent); 81 glfwSetKeyCallback(m_render_window, OnKeyEvent);
49 //glfwSetWindowSizeCallback(m_render_window, OnWindowSizeEvent); 82 //glfwSetWindowSizeCallback(m_render_window, OnWindowSizeEvent);
50 83
51 DoneCurrent(); 84 DoneCurrent();
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index 3a82f5b80..4ae34bea9 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -8,6 +8,7 @@ set(SRCS
8 file_search.cpp 8 file_search.cpp
9 file_util.cpp 9 file_util.cpp
10 hash.cpp 10 hash.cpp
11 key_map.cpp
11 log_manager.cpp 12 log_manager.cpp
12 math_util.cpp 13 math_util.cpp
13 mem_arena.cpp 14 mem_arena.cpp
@@ -39,6 +40,7 @@ set(HEADERS
39 file_search.h 40 file_search.h
40 file_util.h 41 file_util.h
41 hash.h 42 hash.h
43 key_map.h
42 linear_disk_cache.h 44 linear_disk_cache.h
43 log.h 45 log.h
44 log_manager.h 46 log_manager.h
diff --git a/src/common/emu_window.h b/src/common/emu_window.h
index 5e2c33d7a..90fbd9335 100644
--- a/src/common/emu_window.h
+++ b/src/common/emu_window.h
@@ -7,6 +7,9 @@
7#include "common/common.h" 7#include "common/common.h"
8#include "common/scm_rev.h" 8#include "common/scm_rev.h"
9 9
10#include "common/key_map.h"
11#include "core/hle/service/hid.h"
12
10// Abstraction class used to provide an interface between emulation code and the frontend (e.g. SDL, 13// Abstraction class used to provide an interface between emulation code and the frontend (e.g. SDL,
11// QGLWidget, GLFW, etc...) 14// QGLWidget, GLFW, etc...)
12class EmuWindow 15class EmuWindow
@@ -32,6 +35,22 @@ public:
32 /// Releases (dunno if this is the "right" word) the GLFW context from the caller thread 35 /// Releases (dunno if this is the "right" word) the GLFW context from the caller thread
33 virtual void DoneCurrent() = 0; 36 virtual void DoneCurrent() = 0;
34 37
38 static void KeyPressed(KeyMap::CitraKey key) {
39 HID_User::PADState mapped_key = KeyMap::Get3DSKey(key);
40
41 if (mapped_key.hex != HID_User::PAD_NONE.hex) {
42 HID_User::PADButtonPress(mapped_key);
43 }
44 }
45
46 static void KeyReleased(KeyMap::CitraKey key) {
47 HID_User::PADState mapped_key = KeyMap::Get3DSKey(key);
48
49 if (mapped_key.hex != HID_User::PAD_NONE.hex) {
50 HID_User::PADButtonRelease(mapped_key);
51 }
52 }
53
35 Config GetConfig() const { 54 Config GetConfig() const {
36 return m_config; 55 return m_config;
37 } 56 }
diff --git a/src/common/key_map.cpp b/src/common/key_map.cpp
new file mode 100644
index 000000000..5941a105b
--- /dev/null
+++ b/src/common/key_map.cpp
@@ -0,0 +1,21 @@
1// Copyright 2013 Dolphin Emulator Project
2// Licensed under GPLv2
3// Refer to the license.txt file included.
4
5#include "key_map.h"
6#include <map>
7
8
9namespace KeyMap {
10
11std::map<CitraKey, HID_User::PADState> g_key_map;
12
13void SetKeyMapping(CitraKey key, HID_User::PADState padState) {
14 g_key_map[key].hex = padState.hex;
15}
16
17HID_User::PADState Get3DSKey(CitraKey key) {
18 return g_key_map[key];
19}
20
21}
diff --git a/src/common/key_map.h b/src/common/key_map.h
new file mode 100644
index 000000000..7e94df618
--- /dev/null
+++ b/src/common/key_map.h
@@ -0,0 +1,35 @@
1// Copyright 2013 Dolphin Emulator Project
2// Licensed under GPLv2
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "core/hle/service/hid.h"
8
9namespace KeyMap {
10
11class CitraKey {
12public:
13 CitraKey() : keyCode(0) {}
14 CitraKey(int code) : keyCode(code) {}
15
16 int keyCode;
17
18 bool operator < (const CitraKey &other) const {
19 return keyCode < other.keyCode;
20 }
21
22 bool operator == (const CitraKey &other) const {
23 return keyCode == other.keyCode;
24 }
25};
26
27struct DefaultKeyMapping {
28 KeyMap::CitraKey key;
29 HID_User::PADState state;
30};
31
32void SetKeyMapping(CitraKey key, HID_User::PADState padState);
33HID_User::PADState Get3DSKey(CitraKey key);
34
35}
diff --git a/src/core/hle/service/hid.cpp b/src/core/hle/service/hid.cpp
index 6a4895c90..1ef39fced 100644
--- a/src/core/hle/service/hid.cpp
+++ b/src/core/hle/service/hid.cpp
@@ -16,6 +16,121 @@ namespace HID_User {
16 16
17Handle g_shared_mem = 0; ///< Handle to shared memory region designated to HID_User service 17Handle g_shared_mem = 0; ///< Handle to shared memory region designated to HID_User service
18 18
19// Event handles
20Handle g_event_pad_or_touch_1 = 0;
21Handle g_event_pad_or_touch_2 = 0;
22Handle g_event_accelerometer = 0;
23Handle g_event_gyroscope = 0;
24Handle g_event_debug_pad = 0;
25
26// Next PAD state update information
27PADState g_next_state = {{0}};
28u32 g_next_index = 0;
29s16 g_next_circle_x = 0;
30s16 g_next_circle_y = 0;
31
32/** Gets a pointer to the PADData structure inside HID shared memory
33 */
34static inline PADData* GetPADData() {
35 if (0 == g_shared_mem)
36 return nullptr;
37
38 return reinterpret_cast<PADData*>(Kernel::GetSharedMemoryPointer(g_shared_mem, 0));
39}
40
41/** Circle PAD from keys.
42 *
43 * This is implemented as "pushed all the way to an edge (max) or centered (0)".
44 *
45 * Indicate the circle pad is pushed completely to the edge in 1 of 8 directions.
46 */
47void UpdateNextCirclePADState() {
48 static const s16 max_value = 0x9C;
49 g_next_circle_x = g_next_state.circle_left ? -max_value : 0x0;
50 g_next_circle_x += g_next_state.circle_right ? max_value : 0x0;
51 g_next_circle_y = g_next_state.circle_down ? -max_value : 0x0;
52 g_next_circle_y += g_next_state.circle_up ? max_value : 0x0;
53}
54
55/** Sets a PAD state (button or button combo) as pressed
56 */
57void PADButtonPress(PADState pad_state) {
58 g_next_state.hex |= pad_state.hex;
59 UpdateNextCirclePADState();
60}
61
62/** Sets a PAD state (button or button combo) as released
63 */
64void PADButtonRelease(PADState pad_state) {
65 g_next_state.hex &= ~pad_state.hex;
66 UpdateNextCirclePADState();
67}
68
69/** Called after all PAD changes to be included in this update have been made,
70 * including both PAD key changes and analog circle PAD changes.
71 */
72void PADUpdateComplete() {
73 PADData* pad_data = GetPADData();
74
75 // Update PADData struct
76 pad_data->current_state.hex = g_next_state.hex;
77 pad_data->index = g_next_index;
78 g_next_index = (g_next_index + 1) % pad_data->entries.size();
79
80 // Get the previous PAD state
81 u32 last_entry_index = (pad_data->index - 1) % pad_data->entries.size();
82 PADState old_state = pad_data->entries[last_entry_index].current_state;
83
84 // Compute bitmask with 1s for bits different from the old state
85 PADState changed;
86 changed.hex = (g_next_state.hex ^ old_state.hex);
87
88 // Compute what was added
89 PADState additions;
90 additions.hex = changed.hex & g_next_state.hex;
91
92 // Compute what was removed
93 PADState removals;
94 removals.hex = changed.hex & old_state.hex;
95
96 // Get the current PAD entry
97 PADDataEntry* current_pad_entry = &pad_data->entries[pad_data->index];
98
99 // Update entry properties
100 current_pad_entry->current_state.hex = g_next_state.hex;
101 current_pad_entry->delta_additions.hex = additions.hex;
102 current_pad_entry->delta_removals.hex = removals.hex;
103
104 // Set circle PAD
105 current_pad_entry->circle_pad_x = g_next_circle_x;
106 current_pad_entry->circle_pad_y = g_next_circle_y;
107
108 // If we just updated index 0, provide a new timestamp
109 if (pad_data->index == 0) {
110 pad_data->index_reset_ticks_previous = pad_data->index_reset_ticks;
111 pad_data->index_reset_ticks = (s64)Core::g_app_core->GetTicks();
112 }
113
114 // Signal both handles when there's an update to PAD or touch
115 Kernel::SignalEvent(g_event_pad_or_touch_1);
116 Kernel::SignalEvent(g_event_pad_or_touch_2);
117}
118
119
120// TODO(peachum):
121// Add a method for setting analog input from joystick device for the circle PAD.
122//
123// This method should:
124// * Be called after both PADButton<Press, Release>().
125// * Be called before PADUpdateComplete()
126// * Set current PADEntry.circle_pad_<axis> using analog data
127// * Set PadData.raw_circle_pad_data
128// * Set PadData.current_state.circle_right = 1 if current PADEntry.circle_pad_x >= 41
129// * Set PadData.current_state.circle_up = 1 if current PADEntry.circle_pad_y >= 41
130// * Set PadData.current_state.circle_left = 1 if current PADEntry.circle_pad_x <= -41
131// * Set PadData.current_state.circle_right = 1 if current PADEntry.circle_pad_y <= -41
132
133
19/** 134/**
20 * HID_User::GetIPCHandles service function 135 * HID_User::GetIPCHandles service function
21 * Inputs: 136 * Inputs:
@@ -35,11 +150,11 @@ void GetIPCHandles(Service::Interface* self) {
35 150
36 cmd_buff[1] = 0; // No error 151 cmd_buff[1] = 0; // No error
37 cmd_buff[3] = g_shared_mem; 152 cmd_buff[3] = g_shared_mem;
38 cmd_buff[4] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventA"); 153 cmd_buff[4] = g_event_pad_or_touch_1;
39 cmd_buff[5] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventB"); 154 cmd_buff[5] = g_event_pad_or_touch_2;
40 cmd_buff[6] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventC"); 155 cmd_buff[6] = g_event_accelerometer;
41 cmd_buff[7] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventGyroscope"); 156 cmd_buff[7] = g_event_gyroscope;
42 cmd_buff[8] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventD"); 157 cmd_buff[8] = g_event_debug_pad;
43 158
44 DEBUG_LOG(KERNEL, "called"); 159 DEBUG_LOG(KERNEL, "called");
45} 160}
@@ -58,14 +173,19 @@ const Interface::FunctionInfo FunctionTable[] = {
58}; 173};
59 174
60 175
61
62
63//////////////////////////////////////////////////////////////////////////////////////////////////// 176////////////////////////////////////////////////////////////////////////////////////////////////////
64// Interface class 177// Interface class
65 178
66Interface::Interface() { 179Interface::Interface() {
67 g_shared_mem = Kernel::CreateSharedMemory("HID_User:SharedMem"); // Create shared memory object 180 g_shared_mem = Kernel::CreateSharedMemory("HID_User:SharedMem"); // Create shared memory object
68 181
182 // Create event handles
183 g_event_pad_or_touch_1 = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventPADOrTouch1");
184 g_event_pad_or_touch_2 = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventPADOrTouch2");
185 g_event_accelerometer = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventAccelerometer");
186 g_event_gyroscope = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventGyroscope");
187 g_event_debug_pad = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventDebugPAD");
188
69 Register(FunctionTable, ARRAY_SIZE(FunctionTable)); 189 Register(FunctionTable, ARRAY_SIZE(FunctionTable));
70} 190}
71 191
diff --git a/src/core/hle/service/hid.h b/src/core/hle/service/hid.h
index 6ddf2f80f..f5f76f0fc 100644
--- a/src/core/hle/service/hid.h
+++ b/src/core/hle/service/hid.h
@@ -15,30 +15,83 @@
15 15
16namespace HID_User { 16namespace HID_User {
17 17
18/// Structure of a PAD controller state
18struct PADState { 19struct PADState {
19 union { 20 union {
20 u32 hex; 21 u32 hex;
21 22
22 BitField<0, 1, u32> A; 23 BitField<0, 1, u32> a;
23 BitField<1, 1, u32> B; 24 BitField<1, 1, u32> b;
24 BitField<2, 1, u32> Select; 25 BitField<2, 1, u32> select;
25 BitField<3, 1, u32> Start; 26 BitField<3, 1, u32> start;
26 BitField<4, 1, u32> Right; 27 BitField<4, 1, u32> right;
27 BitField<5, 1, u32> Left; 28 BitField<5, 1, u32> left;
28 BitField<6, 1, u32> Up; 29 BitField<6, 1, u32> up;
29 BitField<7, 1, u32> Down; 30 BitField<7, 1, u32> down;
30 BitField<8, 1, u32> R; 31 BitField<8, 1, u32> r;
31 BitField<9, 1, u32> L; 32 BitField<9, 1, u32> l;
32 BitField<10, 1, u32> X; 33 BitField<10, 1, u32> x;
33 BitField<11, 1, u32> Y; 34 BitField<11, 1, u32> y;
34 35
35 BitField<28, 1, u32> CircleRight; 36 BitField<28, 1, u32> circle_right;
36 BitField<29, 1, u32> CircleLeft; 37 BitField<29, 1, u32> circle_left;
37 BitField<30, 1, u32> CircleUp; 38 BitField<30, 1, u32> circle_up;
38 BitField<31, 1, u32> CircleDown; 39 BitField<31, 1, u32> circle_down;
39 }; 40 };
40}; 41};
41 42
43/// Structure of a single entry in the PADData's PAD state history array
44struct PADDataEntry {
45 PADState current_state;
46 PADState delta_additions;
47 PADState delta_removals;
48
49 s16 circle_pad_x;
50 s16 circle_pad_y;
51};
52
53/// Structure of all data related to the 3DS Pad
54struct PADData {
55 s64 index_reset_ticks;
56 s64 index_reset_ticks_previous;
57 u32 index; // the index of the last updated PAD state history element
58
59 u32 pad1;
60 u32 pad2;
61
62 PADState current_state; // same as entries[index].current_state
63 u32 raw_circle_pad_data;
64
65 u32 pad3;
66
67 std::array<PADDataEntry, 8> entries; // PAD state history
68};
69
70// Pre-defined PADStates for single button presses
71const PADState PAD_NONE = {{0}};
72const PADState PAD_A = {{1u << 0}};
73const PADState PAD_B = {{1u << 1}};
74const PADState PAD_SELECT = {{1u << 2}};
75const PADState PAD_START = {{1u << 3}};
76const PADState PAD_RIGHT = {{1u << 4}};
77const PADState PAD_LEFT = {{1u << 5}};
78const PADState PAD_UP = {{1u << 6}};
79const PADState PAD_DOWN = {{1u << 7}};
80const PADState PAD_R = {{1u << 8}};
81const PADState PAD_L = {{1u << 9}};
82const PADState PAD_X = {{1u << 10}};
83const PADState PAD_Y = {{1u << 11}};
84const PADState PAD_CIRCLE_RIGHT = {{1u << 28}};
85const PADState PAD_CIRCLE_LEFT = {{1u << 29}};
86const PADState PAD_CIRCLE_UP = {{1u << 30}};
87const PADState PAD_CIRCLE_DOWN = {{1u << 31}};
88
89// Methods for updating the HID module's state
90void PADButtonPress(PADState pad_state);
91void PADButtonRelease(PADState pad_state);
92void PADUpdateComplete();
93
94/// HID service interface
42class Interface : public Service::Interface { 95class Interface : public Service::Interface {
43public: 96public:
44 97