Skip to content

Commit 73d19c6

Browse files
committed
added impl of linux mousemoveto and mousemoverelative
1 parent df79f22 commit 73d19c6

File tree

3 files changed

+51
-20
lines changed

3 files changed

+51
-20
lines changed

XInputSimulator/main.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@
1818
#include <iostream>
1919
#include "xinputsimulator.h"
2020

21+
#include <chrono>
22+
#include <thread>
23+
2124
using namespace std;
2225

2326
int main()
2427
{
2528
cout << "Hello World!" << endl;
2629

2730
XInputSimulator &sim = XInputSimulator::getInstance();
28-
sim.mouseMoveTo(1,2);
29-
sim.mouseClick(1);
31+
sim.mouseMoveTo(100,200);
32+
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
33+
sim.mouseMoveRelative(400, -100);
3034

3135
return 0;
3236
}

XInputSimulator/xinputsimulatorimpllinux.cpp

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,57 @@
1+
// Copyright 2013 Dustin Bensing
2+
3+
// This file is part of XInputSimulator.
4+
5+
// XInputSimulator is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Lesser Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// any later version.
9+
10+
// XInputSimulator is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Lesser Public License for more details.
14+
15+
// You should have received a copy of the GNU Lesser Public License
16+
// along with XInputSimulator. If not, see <http://www.gnu.org/licenses/>.
17+
118
#include "xinputsimulatorimpllinux.h"
219
#include "notimplementedexception.h"
320
#include <iostream>
421

522
XInputSimulatorImplLinux::XInputSimulatorImplLinux()
623
{
24+
if((display = XOpenDisplay(NULL)) == NULL) {
25+
std::cout << "can not access display server!" << std::endl;
26+
return;
27+
}
28+
29+
root = DefaultRootWindow(display);
730
}
831

932
void XInputSimulatorImplLinux::mouseMoveTo(int x, int y)
1033
{
1134
std::cout << "move the mouse!\n";
35+
36+
if(!display){
37+
return;
38+
}
39+
40+
XWarpPointer(display, None, root, 0, 0, 0, 0, x, y);
41+
XFlush(display);
1242
}
1343

1444
void XInputSimulatorImplLinux::mouseMoveRelative(int x, int y)
1545
{
16-
throw NotImplementedException();
17-
}
18-
19-
// Copyright 2013 Dustin Bensing
46+
//throw NotImplementedException();
2047

21-
// This file is part of XInputSimulator.
48+
if(!display){
49+
return;
50+
}
2251

23-
// XInputSimulator is free software: you can redistribute it and/or modify
24-
// it under the terms of the GNU Lesser Public License as published by
25-
// the Free Software Foundation, either version 3 of the License, or
26-
// any later version.
27-
28-
// XInputSimulator is distributed in the hope that it will be useful,
29-
// but WITHOUT ANY WARRANTY; without even the implied warranty of
30-
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31-
// GNU Lesser Public License for more details.
32-
33-
// You should have received a copy of the GNU Lesser Public License
34-
// along with XInputSimulator. If not, see <http://www.gnu.org/licenses/>.
52+
XWarpPointer(display, None, None, 0, 0, 0, 0, x, y);
53+
XFlush(display);
54+
}
3555

3656
void XInputSimulatorImplLinux::mouseDown(int button)
3757
{

XInputSimulator/xinputsimulatorimpllinux.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,20 @@
1818
#ifndef XINPUTSIMULATORIMPLLINUX_H
1919
#define XINPUTSIMULATORIMPLLINUX_H
2020

21+
#include <X11/Xlib.h>
22+
#include <X11/Xutil.h>
23+
2124
#include "xinputsimulatorimpl.h"
2225

2326
class XInputSimulatorImplLinux : public XInputSimulatorImpl
2427
{
28+
private:
29+
Display *display;
30+
Window root;
31+
2532
public:
2633
XInputSimulatorImplLinux();
27-
~XInputSimulatorImplLinux(){}
34+
~XInputSimulatorImplLinux(){ delete display; }
2835

2936
virtual void mouseMoveTo(int x, int y) override;
3037
virtual void mouseMoveRelative(int x, int y) override;

0 commit comments

Comments
 (0)