aboutsummaryrefslogtreecommitdiff
path: root/src/input_common/mouse/mouse_input.h
blob: c8bae99c149ae88bea6054ab8fc4d9f6059c9add (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
// Copyright 2020 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#pragma once

#include <array>
#include <mutex>
#include <stop_token>
#include <thread>

#include "common/common_types.h"
#include "common/threadsafe_queue.h"
#include "common/vector_math.h"
#include "core/frontend/input.h"
#include "input_common/motion_input.h"

namespace MouseInput {

enum class MouseButton {
    Left,
    Right,
    Wheel,
    Backward,
    Forward,
    Task,
    Extra,
    Undefined,
};

struct MouseStatus {
    MouseButton button{MouseButton::Undefined};
};

struct MouseData {
    bool pressed{};
    std::array<int, 2> axis{};
    Input::MotionStatus motion{};
    Input::TouchStatus touch{};
};

class Mouse {
public:
    Mouse();
    ~Mouse();

    /// Used for polling
    void BeginConfiguration();
    void EndConfiguration();

    /**
     * Signals that a button is pressed.
     * @param x the x-coordinate of the cursor
     * @param y the y-coordinate of the cursor
     * @param button_ the button pressed
     */
    void PressButton(int x, int y, MouseButton button_);

    /**
     * Signals that mouse has moved.
     * @param x the x-coordinate of the cursor
     * @param y the y-coordinate of the cursor
     * @param center_x the x-coordinate of the middle of the screen
     * @param center_y the y-coordinate of the middle of the screen
     */
    void MouseMove(int x, int y, int center_x, int center_y);

    /**
     * Signals that a button is released.
     * @param button_ the button pressed
     */
    void ReleaseButton(MouseButton button_);

    /**
     * Signals that all buttons are released
     */
    void ReleaseAllButtons();

    [[nodiscard]] bool ToggleButton(std::size_t button_);
    [[nodiscard]] bool UnlockButton(std::size_t button_);

    [[nodiscard]] Common::SPSCQueue<MouseStatus>& GetMouseQueue();
    [[nodiscard]] const Common::SPSCQueue<MouseStatus>& GetMouseQueue() const;

    [[nodiscard]] MouseData& GetMouseState(std::size_t button);
    [[nodiscard]] const MouseData& GetMouseState(std::size_t button) const;

private:
    void UpdateThread(std::stop_token stop_token);
    void UpdateYuzuSettings();
    void StopPanning();

    struct MouseInfo {
        InputCommon::MotionInput motion{0.0f, 0.0f, 0.0f};
        Common::Vec2<int> mouse_origin;
        Common::Vec2<int> last_mouse_position;
        Common::Vec2<float> last_mouse_change;
        bool is_tilting = false;
        float sensitivity{0.120f};

        float tilt_speed = 0;
        Common::Vec2<float> tilt_direction;
        MouseData data;
    };

    u16 buttons{};
    u16 toggle_buttons{};
    u16 lock_buttons{};
    std::jthread update_thread;
    MouseButton last_button{MouseButton::Undefined};
    std::array<MouseInfo, 7> mouse_info;
    Common::SPSCQueue<MouseStatus> mouse_queue;
    bool configuring{false};
    int mouse_panning_timout{};
};
} // namespace MouseInput