XInputSimulator/XInputSimulator/xinputsimulatorimpllinux.cpp

200 lines
5.7 KiB
C++
Raw Permalink Normal View History

// Copyright 2013 Dustin Bensing
// This file is part of XInputSimulator.
// XInputSimulator is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
// XInputSimulator 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 Lesser Public License for more details.
// You should have received a copy of the GNU Lesser Public License
// along with XInputSimulator. If not, see <http://www.gnu.org/licenses/>.
2013-07-15 16:14:20 +02:00
#ifdef __linux__
#include <unistd.h> //usleep
2013-07-14 13:52:02 +02:00
#include "xinputsimulatorimpllinux.h"
#include <iostream>
//memset
#include <stdio.h>
#include <cstring>
2022-04-20 21:03:11 +02:00
#include <thread>
2013-07-14 13:52:02 +02:00
XInputSimulatorImplLinux::XInputSimulatorImplLinux()
{
if((display = XOpenDisplay(NULL)) == NULL) {
std::cout << "can not access display server!" << std::endl;
return;
}
root = DefaultRootWindow(display);
Screen* pscr = DefaultScreenOfDisplay( display );
this->displayX = pscr->width;
this->displayY = pscr->height;
//XCloseDisplay( pdsp );
2013-07-14 13:52:02 +02:00
}
void XInputSimulatorImplLinux::initMouseEvent(int button)
{
event.xbutton.button = button; //which button
event.xbutton.same_screen = True;
event.xbutton.subwindow = DefaultRootWindow(display);
while (event.xbutton.subwindow)
{
event.xbutton.window = event.xbutton.subwindow;
XQueryPointer(display, event.xbutton.window,
&event.xbutton.root, &event.xbutton.subwindow,
&event.xbutton.x_root, &event.xbutton.y_root,
&event.xbutton.x, &event.xbutton.y,
&event.xbutton.state);
}
}
2013-07-14 13:52:02 +02:00
void XInputSimulatorImplLinux::mouseMoveTo(int x, int y)
{
if(!display){
return;
}
XWarpPointer(display, None, root, 0, 0, 0, 0, x, y);
XFlush(display);
XEvent event;
memset(&event, 0, sizeof (event));
2013-07-14 13:52:02 +02:00
}
void XInputSimulatorImplLinux::mouseMoveRelative(int x, int y)
{
if(!display){
return;
}
2013-07-14 14:25:29 +02:00
XWarpPointer(display, None, None, 0, 0, 0, 0, x, y);
XFlush(display);
}
2013-07-14 14:25:29 +02:00
2013-07-14 13:52:02 +02:00
void XInputSimulatorImplLinux::mouseDown(int button)
{
XTestFakeButtonEvent(display, button, true, CurrentTime);
XFlush(display);
2013-07-14 13:52:02 +02:00
}
void XInputSimulatorImplLinux::mouseUp(int button)
{
XTestFakeButtonEvent(display, button, false, CurrentTime);
XFlush(display);
2013-07-14 13:52:02 +02:00
}
void XInputSimulatorImplLinux::mouseClick(int button)
{
this->mouseDown(button);
usleep(100);
this->mouseUp(button);
2013-07-14 13:52:02 +02:00
}
2013-07-14 17:55:23 +02:00
//kajsdölfkjasdölfkjasldökfjaölsdkjfalsdkjfalskdjfaldskjfkajsdölfkjasdölfkjasldökfjaölsdkjfalsdkjfalskdjfaldskjfkajsdölfkjasdölfkjasldökfjaölsdkjfalsdkjfalskdjfaldskjfkajsdölfkjasdölfkjasldökfjaölsdkjfalsdkjfalskdjfaldskjfkajsdölfkjasdölfkjasldökfjaölsdkjfalsdkjfalskdjfaldskjfkajsdölfkjasdölfkjasldökfjaölsdkjfalsdkjfalskdjfaldskjfkajsdölfkjasdölfkjasldökfjaölsdkjfalsdkjfalskdjfaldskjfkajsdölfkjasdölfkjasldökfjaölsdkjfalsdkjfalskdjfaldskjfkajsdölfkjasdölfkjasldökfjaölsdkjfalsdkjfalskdjfaldskjfkajsdölfkjasdölfkjasldökfjaölsdkjfalsdkjfalskdjfaldskjfkajsdölfkjasdölfkjasldökfjaölsdkjfalsdkjfalskdjfaldskjfkajsdölfkjasdölfkjasldökfjaölsdkjfalsdkjfalskdjfaldskjfkajsdölfkjasdölfkjasldökfjaölsdkjfalsdkjfalskdjfaldskjfkajsdölfkjasdölfkjasldökfjaölsdkjfalsdkjfalskdjfaldskjfkajsdölfkjasdölfkjasldökfjaölsdkjfalsdkjfalskdjfaldskjfkajsdölfkjasdölfkjasldökfjaölsdkjfalsdkjfalskdjfaldskjfkajsdölfkjasdölfkjasldökfjaölsdkjfalsdkjfalskdjfaldskjf
2013-07-14 13:52:02 +02:00
void XInputSimulatorImplLinux::mouseScrollX(int length)
{
2013-07-14 17:55:23 +02:00
int button;
if(length < 0){
2013-07-16 17:58:24 +02:00
button = 6; //scroll left button
2013-07-14 17:55:23 +02:00
}else{
2013-07-16 17:58:24 +02:00
button = 7; //scroll right button
2013-07-14 17:55:23 +02:00
}
if(length < 0){
length *= -1;
}
for(int cnt = 0; cnt < length; cnt++){
this->mouseDown(button);
this->mouseUp(button);
}
2013-07-14 13:52:02 +02:00
}
void XInputSimulatorImplLinux::mouseScrollY(int length)
{
2013-07-14 17:55:23 +02:00
int button;
if(length < 0){
2013-07-16 17:58:24 +02:00
button = 4; //scroll up button
2013-07-14 17:55:23 +02:00
}else{
2013-07-16 17:58:24 +02:00
button = 5; //scroll down button
2013-07-14 17:55:23 +02:00
}
if(length < 0){
length *= -1;
}
for(int cnt = 0; cnt < length; cnt++){
this->mouseDown(button);
this->mouseUp(button);
}
2013-07-14 13:52:02 +02:00
}
void XInputSimulatorImplLinux::keyDown(int key)
{
2013-07-16 23:08:00 +02:00
XTestFakeKeyEvent(display, key, True, 0);
XFlush(display);
2013-07-14 13:52:02 +02:00
}
void XInputSimulatorImplLinux::keyUp(int key)
{
2013-07-16 23:08:00 +02:00
XTestFakeKeyEvent(display, key, False, 0);
XFlush(display);
2013-07-14 13:52:02 +02:00
}
2013-07-15 16:14:20 +02:00
2013-07-16 19:39:22 +02:00
void XInputSimulatorImplLinux::keyClick(int key)
{
2013-07-16 23:08:00 +02:00
this->keyDown(key);
2022-04-20 21:03:11 +02:00
std::this_thread::sleep_for(std::chrono::milliseconds(25));
2013-07-16 23:08:00 +02:00
this->keyUp(key);
2022-04-20 21:03:11 +02:00
std::this_thread::sleep_for(std::chrono::milliseconds(36));
2013-07-16 19:39:22 +02:00
}
2013-07-16 23:08:00 +02:00
int XInputSimulatorImplLinux::charToKeyCode(char key_char)
2013-07-16 19:39:22 +02:00
{
2022-04-20 21:03:11 +02:00
// std::cout << "cchar: " << (int)key_char << std::endl;
2013-07-16 23:08:00 +02:00
int keyCode = XKeysymToKeycode(display, key_char);
// int keyCode = XKeysymToKeycode(display, XStringToKeysym(&key_char));
2022-04-20 21:03:11 +02:00
// std::cout << "ccode: " << keyCode << std::endl;
2013-07-16 23:08:00 +02:00
return keyCode;
2013-07-16 19:39:22 +02:00
}
void XInputSimulatorImplLinux::keySequence(const std::string &sequence)
{
2013-07-16 23:08:00 +02:00
for(const char c : sequence) {
int keyCode = this->charToKeyCode(c);
if (isupper(c)) {
this->keyDown(XKeysymToKeycode(display, XK_Shift_L));
2022-04-20 21:03:11 +02:00
std::this_thread::sleep_for(std::chrono::milliseconds(42));
this->keyClick(keyCode);
2022-04-20 21:03:11 +02:00
this->keyUp(XKeysymToKeycode(display, XK_Shift_L));
}
else {
this->keyClick(keyCode);
}
2022-04-20 21:03:11 +02:00
std::this_thread::sleep_for(std::chrono::milliseconds(35));
2013-07-16 23:08:00 +02:00
}
2013-07-16 19:39:22 +02:00
}
2013-07-15 16:14:20 +02:00
#endif // linux