Skip to content

Commit a1b5835

Browse files
committed
mac port same level as linux
1 parent 3fb1b37 commit a1b5835

File tree

7 files changed

+251
-11
lines changed

7 files changed

+251
-11
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616
*.pro
1717
*.user
1818
*.directory
19-
build-*
19+
build-*
20+
*build-*

XInputSimulator/main.cpp

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

21-
#include <chrono>
22-
#include <thread>
21+
//#include <chrono>
22+
//#include <thread>
23+
24+
#include <unistd.h>
2325

2426
using namespace std;
2527

@@ -30,6 +32,8 @@ int main()
3032
cout << "Hello World!" << endl;
3133

3234
XInputSimulator &sim = XInputSimulator::getInstance();
35+
waitABit();
36+
waitABit();
3337
sim.mouseMoveTo(500,400);
3438
waitABit();
3539
sim.mouseMoveRelative(400, -100);
@@ -50,10 +54,13 @@ int main()
5054
waitABit();
5155
sim.mouseScrollX(-10);
5256

57+
58+
waitABit();
5359
return 0;
5460
}
5561

5662
void waitABit()
5763
{
58-
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
64+
//std::this_thread::sleep_for(std::chrono::milliseconds(1000));
65+
sleep(2);
5966
}

XInputSimulator/xinputsimulator.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919
#define XINPUTSIMULATOR_H
2020

2121
#include <memory>
22-
#include <mutex>
22+
//#include <mutex>
2323
#include <iostream>
2424
#include "xinputsimulatorimpl.h"
2525
#include "notimplementedexception.h"
2626

2727
#ifdef __linux__
2828
#include "xinputsimulatorimpllinux.h"
2929
#elif __APPLE__
30-
// apple implementation
30+
#include "xinputsimulatorimplmacos.h"
3131
#elif _WIN32
3232
// win implementation
3333
#endif
@@ -61,8 +61,7 @@ class XInputSimulator
6161
//instance.implementation = std::move(std::unique_ptr<XInputSimulatorImpl>(new XInputSimulatorImplLinux));
6262
instance.implementation = new XInputSimulatorImplLinux;
6363
#elif __APPLE__
64-
// apple implementation
65-
throw NotImplementedException();
64+
instance.implementation = new XInputSimulatorImplMacOs;
6665
#elif _WIN32
6766
// win implementation
6867
throw NotImplementedException();

XInputSimulator/xinputsimulatorimpllinux.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
// You should have received a copy of the GNU Lesser Public License
1616
// along with XInputSimulator. If not, see <http://www.gnu.org/licenses/>.
1717

18+
#ifdef __linux__
19+
1820
#include "xinputsimulatorimpllinux.h"
1921
#include "notimplementedexception.h"
2022
#include <iostream>
@@ -24,8 +26,8 @@
2426
#include <cstring>
2527

2628
//sleep
27-
#include <chrono>
28-
#include <thread>
29+
//#include <chrono>
30+
//#include <thread>
2931

3032
XInputSimulatorImplLinux::XInputSimulatorImplLinux()
3133
{
@@ -109,7 +111,7 @@ void XInputSimulatorImplLinux::mouseClick(int button)
109111
{
110112
//throw NotImplementedException();
111113
this->mouseDown(button);
112-
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
114+
//std::this_thread::sleep_for(std::chrono::milliseconds(1000));
113115
this->mouseUp(button);
114116
}
115117
//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
@@ -166,3 +168,6 @@ void XInputSimulatorImplLinux::keyUp(int key)
166168
{
167169
throw NotImplementedException();
168170
}
171+
172+
173+
#endif // linux

XInputSimulator/xinputsimulatorimpllinux.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
// You should have received a copy of the GNU Lesser Public License
1616
// along with XInputSimulator. If not, see <http://www.gnu.org/licenses/>.
1717

18+
#ifdef __linux__
19+
1820
#ifndef XINPUTSIMULATORIMPLLINUX_H
1921
#define XINPUTSIMULATORIMPLLINUX_H
2022

@@ -50,3 +52,5 @@ class XInputSimulatorImplLinux : public XInputSimulatorImpl
5052
};
5153

5254
#endif // XINPUTSIMULATORIMPLLINUX_H
55+
56+
#endif // linux
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
#ifdef __APPLE__
2+
3+
#include <iostream>
4+
5+
//quartz CG... stuff
6+
#include <ApplicationServices/ApplicationServices.h>
7+
8+
//sleep
9+
#include <unistd.h>
10+
11+
#include "xinputsimulatorimplmacos.h"
12+
#include "notimplementedexception.h"
13+
14+
15+
16+
XInputSimulatorImplMacOs::XInputSimulatorImplMacOs()
17+
{
18+
//this->initCurrentMousePosition();
19+
20+
this->currentX = 500;
21+
this->currentY = 500;
22+
}
23+
24+
//void XInputSimulatorImplMacOs::initMouseEvent(int button)
25+
//{
26+
27+
//}
28+
29+
void XInputSimulatorImplMacOs::initCurrentMousePosition()
30+
{
31+
throw NotImplementedException();
32+
}
33+
34+
void XInputSimulatorImplMacOs::mouseMoveTo(int x, int y)
35+
{
36+
std::cout << "moveTo mac\n";
37+
38+
CGEventRef mouseEv = CGEventCreateMouseEvent(
39+
NULL, kCGEventMouseMoved,
40+
CGPointMake(x, y),
41+
kCGMouseButtonLeft);
42+
43+
std::cout << "mv: " << mouseEv << std::endl;
44+
45+
CGEventPost(kCGHIDEventTap, mouseEv);
46+
47+
CFRelease(mouseEv);
48+
49+
this->currentX = x;
50+
this->currentY = y;
51+
}
52+
53+
void XInputSimulatorImplMacOs::mouseMoveRelative(int x, int y)
54+
{
55+
std::cout << "moveRelative mac\n";
56+
57+
int newX = this->currentX + x;
58+
int newY = this->currentY + y;
59+
60+
std::cout << "newx: " << newX << " newy: " << newY << std::endl;
61+
62+
CGEventRef mouseEv = CGEventCreateMouseEvent(
63+
NULL, kCGEventMouseMoved,
64+
CGPointMake(newX, newY),
65+
kCGMouseButtonLeft);
66+
67+
CGEventPost(kCGHIDEventTap, mouseEv);
68+
69+
CFRelease(mouseEv);
70+
71+
this->currentX = newX;
72+
this->currentY = newY;
73+
}
74+
75+
//TODO use the button from parameter list
76+
void XInputSimulatorImplMacOs::mouseDown(int button)
77+
{
78+
//throw NotImplementedException();
79+
80+
std::cout << "mouseDown mac\n";
81+
82+
CGEventRef mouseEv = CGEventCreateMouseEvent(
83+
NULL, kCGEventLeftMouseDown,
84+
CGPointMake(this->currentX, this->currentY),
85+
kCGMouseButtonLeft); // use int buttn from parameter
86+
87+
CGEventPost(kCGHIDEventTap, mouseEv);
88+
89+
CFRelease(mouseEv);
90+
91+
}
92+
93+
//TODO use the button from parameter list
94+
void XInputSimulatorImplMacOs::mouseUp(int button)
95+
{
96+
//throw NotImplementedException();
97+
98+
std::cout << "mouseDown mac\n";
99+
100+
CGEventRef mouseEv = CGEventCreateMouseEvent(
101+
NULL, kCGEventLeftMouseUp,
102+
CGPointMake(this->currentX, this->currentY),
103+
kCGMouseButtonLeft); // use int buttn from parameter
104+
105+
CGEventPost(kCGHIDEventTap, mouseEv);
106+
107+
CFRelease(mouseEv);
108+
}
109+
110+
void XInputSimulatorImplMacOs::mouseClick(int button)
111+
{
112+
//throw NotImplementedException();
113+
this->mouseDown(button);
114+
this->mouseUp(button);
115+
}
116+
//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
117+
void XInputSimulatorImplMacOs::mouseScrollX(int length)
118+
{
119+
//throw NotImplementedException();
120+
121+
std::cout << "scroll x mac " << length << std::endl;
122+
123+
int scrollDirection = -1; // 1 left -1 right
124+
125+
if(length < 0){
126+
length *= -1;
127+
scrollDirection *= -1;
128+
}
129+
130+
//length *= 100;
131+
132+
for(int cnt = 0; cnt < length; cnt++){
133+
134+
std::cout << "scroll y mac" << std::endl;
135+
136+
CGEventRef scrollEv = CGEventCreateScrollWheelEvent (
137+
NULL, kCGScrollEventUnitLine, // kCGScrollEventUnitLine //kCGScrollEventUnitPixel
138+
2, //CGWheelCount 1 = y 2 = xy 3 = xyz
139+
0,
140+
scrollDirection); // length of scroll from -10 to 10 higher values lead to undef behaviour
141+
142+
CGEventPost(kCGHIDEventTap, scrollEv);
143+
144+
CFRelease(scrollEv);
145+
//sleep(1);
146+
}
147+
}
148+
149+
void XInputSimulatorImplMacOs::mouseScrollY(int length)
150+
{
151+
//throw NotImplementedException();
152+
153+
std::cout << "scroll y mac " << length << std::endl;
154+
155+
int scrollDirection = -1; // 1 down -1 up
156+
157+
if(length < 0){
158+
length *= -1;
159+
scrollDirection *= -1;
160+
}
161+
162+
//length *= 100;
163+
164+
for(int cnt = 0; cnt < length; cnt++){
165+
166+
std::cout << "scroll y mac" << std::endl;
167+
168+
CGEventRef scrollEv = CGEventCreateScrollWheelEvent (
169+
NULL, kCGScrollEventUnitLine, // kCGScrollEventUnitLine //kCGScrollEventUnitPixel
170+
1, //CGWheelCount 1 = y 2 = xy 3 = xyz
171+
scrollDirection); // length of scroll from -10 to 10 higher values lead to undef behaviour
172+
173+
CGEventPost(kCGHIDEventTap, scrollEv);
174+
175+
CFRelease(scrollEv);
176+
//sleep(1);
177+
}
178+
}
179+
180+
void XInputSimulatorImplMacOs::keyDown(int key)
181+
{
182+
throw NotImplementedException();
183+
}
184+
185+
void XInputSimulatorImplMacOs::keyUp(int key)
186+
{
187+
throw NotImplementedException();
188+
}
189+
190+
#endif //apple
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef XINPUTSIMULATORIMPLMACOS_H
2+
#define XINPUTSIMULATORIMPLMACOS_H
3+
4+
#ifdef __APPLE__
5+
6+
#include "xinputsimulatorimpl.h"
7+
8+
class XInputSimulatorImplMacOs : public XInputSimulatorImpl
9+
{
10+
private:
11+
int currentX;
12+
int currentY;
13+
14+
void initCurrentMousePosition();
15+
16+
public:
17+
XInputSimulatorImplMacOs();
18+
~XInputSimulatorImplMacOs(){}
19+
20+
virtual void mouseMoveTo(int x, int y) override;
21+
virtual void mouseMoveRelative(int x, int y) override;
22+
virtual void mouseDown(int button) override;
23+
virtual void mouseUp(int button) override;
24+
virtual void mouseClick(int button) override;
25+
virtual void mouseScrollX(int length) override;
26+
virtual void mouseScrollY(int length) override;
27+
28+
virtual void keyDown(int key) override;
29+
virtual void keyUp(int key) override;
30+
};
31+
32+
#endif //apple
33+
34+
#endif // XINPUTSIMULATORIMPLMACOS_H

0 commit comments

Comments
 (0)