File: MouseManager.h

package info (click to toggle)
openterface-qt 0.1.0%2Bds-1
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 1,444 kB
  • sloc: cpp: 9,552; sh: 127; python: 57; ansic: 4; makefile: 4
file content (148 lines) | stat: -rw-r--r-- 5,120 bytes parent folder | download
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
/*
* ========================================================================== *
*                                                                            *
*    This file is part of the Openterface Mini KVM App QT version            *
*                                                                            *
*    Copyright (C) 2024   <info@openterface.com>                             *
*                                                                            *
*    This program 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 version 3.                                 *
*                                                                            *
*    This program 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 this program. If not, see <http://www.gnu.org/licenses/>.    *
*                                                                            *
* ========================================================================== *
*/

#ifndef MOUSEMANAGER_H
#define MOUSEMANAGER_H


#include "serial/SerialPortManager.h"
#include "ui/statusevents.h"

#include <QObject>
#include <QLoggingCategory>

Q_DECLARE_LOGGING_CATEGORY(log_core_mouse)

#include <QThread>
#include <QCursor>
#include <random>

class MouseMoverThread : public QThread {
    Q_OBJECT

public:
    MouseMoverThread() : running(true) {}

    void run() override {
        int x = 4096; // Starting at the top-right corner
        int y = 1;
        int initialYForce = 0; // Initial downward force is 0
        int initialXForce = getRandomForce(); // Random initial horizontal force
        int screenHeight = 4096;
        int screenWidth = 4096;
    
        int yForce = initialYForce;
        int xForce = initialXForce;
        const int yAcceleration = 2; // Constant acceleration to simulate gravity
    
        while (running) {
            // Move the mouse within the screen boundaries
            while (running) {
                y += yForce;
                x += xForce;
    
                // Apply acceleration in the y direction
                yForce += yAcceleration;
    
                // Check for vertical boundaries
                if (y >= screenHeight) {
                    y = y - yAcceleration;
                    yForce = -yForce; // Reverse vertical direction
                } else if (y <= 0) {
                    y = + yAcceleration;
                    yForce = -yForce; // Reverse vertical direction
                }
    
                // Check for horizontal boundaries
                if (x >= screenWidth) {
                    x = screenWidth;
                    xForce = -xForce; // Reverse horizontal direction
                } else if (x <= 0) {
                    x = 0;
                    xForce = -xForce; // Reverse horizontal direction
                }
    
                moveMouse(x, y);
                QThread::msleep(50); // Small delay to simulate movement
            }
        }
    }

    void stop() {
        running = false;
    }

private:
    void moveMouse(int x, int y) {
        QByteArray data;

        data.append(MOUSE_ABS_ACTION_PREFIX);
        data.append(static_cast<char>(0));
        data.append(static_cast<char>(x & 0xFF));
        data.append(static_cast<char>((x >> 8) & 0xFF));
        data.append(static_cast<char>(y & 0xFF));
        data.append(static_cast<char>((y >> 8) & 0xFF));
        data.append(static_cast<char>(0));

        // send the data to serial
        SerialPortManager::getInstance().sendCommandAsync(data, false);
    }

    int getRandomForce() {
        std::random_device rd;
        std::mt19937 gen(rd());
        std::uniform_int_distribution<> dis(5, 20); // Random force between 1 and 20
        return dis(gen);
    }

    std::atomic<bool> running;
};


class MouseManager : public QObject
{
    Q_OBJECT

public:
    explicit MouseManager(QObject *parent = nullptr);

    void handleAbsoluteMouseAction(int x, int y, int mouse_event, int wheelMovement);
    void handleRelativeMouseAction(int dx, int dy, int mouse_event, int wheelMovement);
    void setEventCallback(StatusEventCallback* callback);
    void startAutoMoveMouse();
    void stopAutoMoveMouse();

    void reset() {
        // Reset any internal state
        // For example, clear any stored coordinates or button states
        qDebug() << "Mouse manager reset";
    }

private:
    bool isDragging = false; 
    StatusEventCallback* statusEventCallback = nullptr;

    uint8_t mapScrollWheel(int delta);
    MouseMoverThread* mouseMoverThread = nullptr;
};

#endif // MOUSEMANAGER_H