summaryrefslogtreecommitdiffstats
path: root/src/common/key_map.cpp
blob: e882f5f52943339a590ac0463972f1b9de2f7c2d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2014 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <map>

#include "common/emu_window.h"
#include "common/key_map.h"

namespace KeyMap {

// TODO (wwylele): currently we treat c-stick as four direction buttons
//     and map it directly to EmuWindow::ButtonPressed.
//     It should go the analog input way like circle pad does.
const std::array<KeyTarget, Settings::NativeInput::NUM_INPUTS> mapping_targets = {{
    Service::HID::PAD_A,
    Service::HID::PAD_B,
    Service::HID::PAD_X,
    Service::HID::PAD_Y,
    Service::HID::PAD_L,
    Service::HID::PAD_R,
    Service::HID::PAD_ZL,
    Service::HID::PAD_ZR,
    Service::HID::PAD_START,
    Service::HID::PAD_SELECT,
    Service::HID::PAD_NONE,
    Service::HID::PAD_UP,
    Service::HID::PAD_DOWN,
    Service::HID::PAD_LEFT,
    Service::HID::PAD_RIGHT,
    Service::HID::PAD_C_UP,
    Service::HID::PAD_C_DOWN,
    Service::HID::PAD_C_LEFT,
    Service::HID::PAD_C_RIGHT,

    IndirectTarget::CirclePadUp,
    IndirectTarget::CirclePadDown,
    IndirectTarget::CirclePadLeft,
    IndirectTarget::CirclePadRight,
    IndirectTarget::CirclePadModifier,
}};

static std::map<HostDeviceKey, KeyTarget> key_map;
static int next_device_id = 0;

static bool circle_pad_up = false;
static bool circle_pad_down = false;
static bool circle_pad_left = false;
static bool circle_pad_right = false;
static bool circle_pad_modifier = false;

static void UpdateCirclePad(EmuWindow& emu_window) {
    constexpr float SQRT_HALF = 0.707106781;
    int x = 0, y = 0;

    if (circle_pad_right)
        ++x;
    if (circle_pad_left)
        --x;
    if (circle_pad_up)
        ++y;
    if (circle_pad_down)
        --y;

    float modifier = circle_pad_modifier ? Settings::values.pad_circle_modifier_scale : 1.0;
    emu_window.CirclePadUpdated(x * modifier * (y == 0 ? 1.0 : SQRT_HALF),
                                y * modifier * (x == 0 ? 1.0 : SQRT_HALF));
}

int NewDeviceId() {
    return next_device_id++;
}

void SetKeyMapping(HostDeviceKey key, KeyTarget target) {
    key_map[key] = target;
}

void ClearKeyMapping(int device_id) {
    auto iter = key_map.begin();
    while (iter != key_map.end()) {
        if (iter->first.device_id == device_id)
            key_map.erase(iter++);
        else
            ++iter;
    }
}

void PressKey(EmuWindow& emu_window, HostDeviceKey key) {
    auto target = key_map.find(key);
    if (target == key_map.end())
        return;

    if (target->second.direct) {
        emu_window.ButtonPressed({{target->second.target.direct_target_hex}});
    } else {
        switch (target->second.target.indirect_target) {
        case IndirectTarget::CirclePadUp:
            circle_pad_up = true;
            UpdateCirclePad(emu_window);
            break;
        case IndirectTarget::CirclePadDown:
            circle_pad_down = true;
            UpdateCirclePad(emu_window);
            break;
        case IndirectTarget::CirclePadLeft:
            circle_pad_left = true;
            UpdateCirclePad(emu_window);
            break;
        case IndirectTarget::CirclePadRight:
            circle_pad_right = true;
            UpdateCirclePad(emu_window);
            break;
        case IndirectTarget::CirclePadModifier:
            circle_pad_modifier = true;
            UpdateCirclePad(emu_window);
            break;
        }
    }
}

void ReleaseKey(EmuWindow& emu_window, HostDeviceKey key) {
    auto target = key_map.find(key);
    if (target == key_map.end())
        return;

    if (target->second.direct) {
        emu_window.ButtonReleased({{target->second.target.direct_target_hex}});
    } else {
        switch (target->second.target.indirect_target) {
        case IndirectTarget::CirclePadUp:
            circle_pad_up = false;
            UpdateCirclePad(emu_window);
            break;
        case IndirectTarget::CirclePadDown:
            circle_pad_down = false;
            UpdateCirclePad(emu_window);
            break;
        case IndirectTarget::CirclePadLeft:
            circle_pad_left = false;
            UpdateCirclePad(emu_window);
            break;
        case IndirectTarget::CirclePadRight:
            circle_pad_right = false;
            UpdateCirclePad(emu_window);
            break;
        case IndirectTarget::CirclePadModifier:
            circle_pad_modifier = false;
            UpdateCirclePad(emu_window);
            break;
        }
    }
}
}