summaryrefslogtreecommitdiffstats
path: root/src/yuzu/hotkeys.h
blob: 56eee8d821a1366ef2a97d8959169a3e3f48b4f2 (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
// SPDX-FileCopyrightText: 2014 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <map>
#include <QKeySequence>
#include <QString>
#include <QWidget>
#include "core/hid/hid_types.h"

class QDialog;
class QSettings;
class QShortcut;
class ControllerShortcut;

namespace Core::HID {
enum class ControllerTriggerType;
class EmulatedController;
} // namespace Core::HID

struct ControllerButtonSequence {
    Core::HID::CaptureButtonState capture{};
    Core::HID::HomeButtonState home{};
    Core::HID::NpadButtonState npad{};
};

class ControllerShortcut : public QObject {
    Q_OBJECT

public:
    explicit ControllerShortcut(Core::HID::EmulatedController* controller);
    ~ControllerShortcut();

    void SetKey(const ControllerButtonSequence& buttons);
    void SetKey(const QString& buttons_shortcut);

    ControllerButtonSequence ButtonSequence() const;

    void SetEnabled(bool enable);
    bool IsEnabled() const;

Q_SIGNALS:
    void Activated();

private:
    void ControllerUpdateEvent(Core::HID::ControllerTriggerType type);

    bool is_enabled{};
    bool active{};
    int callback_key{};
    ControllerButtonSequence button_sequence{};
    std::string name{};
    Core::HID::EmulatedController* emulated_controller = nullptr;
};

class HotkeyRegistry final {
public:
    friend class ConfigureHotkeys;

    explicit HotkeyRegistry();
    ~HotkeyRegistry();

    /**
     * Loads hotkeys from the settings file.
     *
     * @note Yet unregistered hotkeys which are present in the settings will automatically be
     *       registered.
     */
    void LoadHotkeys();

    /**
     * Saves all registered hotkeys to the settings file.
     *
     * @note Each hotkey group will be stored a settings group; For each hotkey inside that group, a
     *       settings group will be created to store the key sequence and the hotkey context.
     */
    void SaveHotkeys();

    /**
     * Returns a QShortcut object whose activated() signal can be connected to other QObjects'
     * slots.
     *
     * @param group  General group this hotkey belongs to (e.g. "Main Window", "Debugger").
     * @param action Name of the action (e.g. "Start Emulation", "Load Image").
     * @param widget Parent widget of the returned QShortcut.
     * @warning If multiple QWidgets' call this function for the same action, the returned QShortcut
     *          will be the same. Thus, you shouldn't rely on the caller really being the
     *          QShortcut's parent.
     */
    QShortcut* GetHotkey(const QString& group, const QString& action, QWidget* widget);
    ControllerShortcut* GetControllerHotkey(const QString& group, const QString& action,
                                            Core::HID::EmulatedController* controller);

    /**
     * Returns a QKeySequence object whose signal can be connected to QAction::setShortcut.
     *
     * @param group  General group this hotkey belongs to (e.g. "Main Window", "Debugger").
     * @param action Name of the action (e.g. "Start Emulation", "Load Image").
     */
    QKeySequence GetKeySequence(const QString& group, const QString& action);

    /**
     * Returns a Qt::ShortcutContext object who can be connected to other
     * QAction::setShortcutContext.
     *
     * @param group  General group this shortcut context belongs to (e.g. "Main Window",
     * "Debugger").
     * @param action Name of the action (e.g. "Start Emulation", "Load Image").
     */
    Qt::ShortcutContext GetShortcutContext(const QString& group, const QString& action);

private:
    struct Hotkey {
        QKeySequence keyseq;
        QString controller_keyseq;
        QShortcut* shortcut = nullptr;
        ControllerShortcut* controller_shortcut = nullptr;
        Qt::ShortcutContext context = Qt::WindowShortcut;
        bool repeat;
    };

    using HotkeyMap = std::map<QString, Hotkey>;
    using HotkeyGroupMap = std::map<QString, HotkeyMap>;

    HotkeyGroupMap hotkey_groups;
};