File: KeyboardMouse.cpp

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 (195 lines) | stat: -rw-r--r-- 7,682 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* ========================================================================== *
*                                                                            *
*    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/>.    *
*                                                                            *
*    This program incorporates portions of the AutoHotkey source code,       *
*    which is licensed under the GNU General Public License version 2 or     *
*    later. The original AutoHotkey source code can be found at:             *
*    https://github.com/Lexikos/AutoHotkey_L                                 *
*                                                                            *
*                                                                            *
*    The AutoHotkey source code is copyrighted by the AutoHotkey             *
*    development team and contributors.                                      *
*                                                                            *
* ========================================================================== *
*/

#include "KeyboardMouse.h"
#include <queue>
#include <QDebug>


KeyboardMouse::KeyboardMouse(QObject *parent) : QObject(parent)
{
    // Constructor implementation
}

void KeyboardMouse::addKeyPacket(const keyPacket& packet) {
    keyData.push(packet);
}


void KeyboardMouse::dataSend(){
    while(!keyData.empty()){
        
        qDebug() << "Sending data for key packet: " 
            << keyData.front().keyboardSendOrNot
            << keyData.front().mouseSendOrNot
            << keyData.front().keyboardMouseSendOrNot;
        if(keyData.front().keyboardSendOrNot) {
            qDebug() << "Sending keyboard data.";
            keyboardSend();
        }
        if(keyData.front().mouseSendOrNot) {
            qDebug() << "Sending mouse data.";
            mouseSend();
        }
        if(keyData.front().keyboardMouseSendOrNot) {
            qDebug() << "Sending keyboard and mouse data.";
            keyboardMouseSend();
        }
        keyData.pop();
        qDebug() << "After" << keyData.size();
    }
}

void KeyboardMouse::keyboardSend(){
    QByteArray data = CMD_SEND_KB_GENERAL_DATA;
    QByteArray release = CMD_SEND_KB_GENERAL_DATA;
    // while(!keyData.empty()){
    QByteArray tmpKeyData = keyData.front().KeytoQByteArray();
    qDebug() << "Data: " << tmpKeyData.toHex();
    data.replace(data.size() - 8, 8, tmpKeyData);   // replace the last 8 byte data
    qDebug() << "After checksum data: " << data.toHex();
    emit SerialPortManager::getInstance().sendCommandAsync(data, false);
    QThread::msleep(clickInterval);
    emit SerialPortManager::getInstance().sendCommandAsync(release, false);
    // keyData.pop();
    // }
}

uint8_t KeyboardMouse::calculateChecksum(const QByteArray &data){
    quint32 sum = 0;
    for (auto byte : data) {
        sum += static_cast<unsigned char>(byte);
    }
    return sum % 256;
}

void KeyboardMouse::mouseSend(){
    QByteArray data;
    QByteArray release;
    uint8_t clickCount = keyData.front().mouseClickCount;
    // while(!keyData.empty()){
    if (keyData.front().mouseMode == 0x02) {
        data.append(MOUSE_ABS_ACTION_PREFIX);
        release.append(MOUSE_ABS_ACTION_PREFIX);
    }else{
        data.append(MOUSE_REL_ACTION_PREFIX);
        release.append(MOUSE_REL_ACTION_PREFIX);
    } 

    QByteArray tmpMouseData = keyData.front().MousetoQByteArray();
    QByteArray releaseMouseData = tmpMouseData;
    releaseMouseData[0] = 0x00;
    qDebug() << "Release data: " << releaseMouseData.toHex();
    qDebug() << "Mouse Data: " << tmpMouseData.toHex();
    
    data.append(tmpMouseData);
    release.append(releaseMouseData);
    data.append(static_cast<char>(calculateChecksum(data)));
    release.append(static_cast<char>(calculateChecksum(release)));
    qDebug() << "merged data: " << data.toHex();
    qDebug() << "merged release: " << release.toHex();
    for (int i = 0; i<clickCount; i++){
        emit SerialPortManager::getInstance().sendCommandAsync(data, false);
        emit SerialPortManager::getInstance().sendCommandAsync(release, false);
        QThread::msleep(clickInterval);
    }
    
    // keyData.pop();
        // emit SerialPortManager::getInstance().sendCommandAsync(release, false);
    // }
}

void KeyboardMouse::keyboardMouseSend(){
    QByteArray mouseData;
    QByteArray mouseRelease;
    QByteArray keyboardData = CMD_SEND_KB_GENERAL_DATA;
    QByteArray keyboardRelease = CMD_SEND_KB_GENERAL_DATA;

    // Prepare keyboard data
    QByteArray tmpKeyData = keyData.front().KeytoQByteArray();
    keyboardData.replace(keyboardData.size() - 8, 8, tmpKeyData);

    // Prepare mouse data
    if (keyData.front().mouseMode == 0x02) {
        mouseData.append(MOUSE_ABS_ACTION_PREFIX);
        mouseRelease.append(MOUSE_ABS_ACTION_PREFIX);
    } else {
        mouseData.append(MOUSE_REL_ACTION_PREFIX);
        mouseRelease.append(MOUSE_REL_ACTION_PREFIX);
    }

    QByteArray tmpMouseData = keyData.front().MousetoQByteArray();
    QByteArray releaseMouseData = tmpMouseData;
    releaseMouseData[0] = 0x00;  // Set release state for mouse

    mouseData.append(tmpMouseData);
    mouseRelease.append(releaseMouseData);
    mouseData.append(static_cast<char>(calculateChecksum(mouseData)));
    mouseRelease.append(static_cast<char>(calculateChecksum(mouseRelease)));
    qDebug() << "keyboard data: " << keyboardData.toHex();
    qDebug() << "mouse data: " << mouseData.toHex();

    // Send press data for both devices
    emit SerialPortManager::getInstance().sendCommandAsync(keyboardData, false);
    emit SerialPortManager::getInstance().sendCommandAsync(mouseData, false);

    // Send release data for both devices
    emit SerialPortManager::getInstance().sendCommandAsync(mouseRelease, false);
    QThread::msleep(keyInterval);
    emit SerialPortManager::getInstance().sendCommandAsync(keyboardRelease, false);
    

    
}

void KeyboardMouse::setMouseSpeed(int speed){
    mouseSpeed = speed;
}

int KeyboardMouse::getMouseSpeed(){
    return mouseSpeed;
}

void KeyboardMouse::updateNumCapsScrollLockState(){
    emit SerialPortManager::getInstance().sendCommandAsync(CMD_GET_INFO, false);
}

bool KeyboardMouse::getNumLockState_(){
    return SerialPortManager::getInstance().getNumLockState();
}

bool KeyboardMouse::getCapsLockState_(){
    return SerialPortManager::getInstance().getCapsLockState();
}

bool KeyboardMouse::getScrollLockState_(){
    return SerialPortManager::getInstance().getScrollLockState();
}