summaryrefslogtreecommitdiffstats
path: root/src/input/x11FakeMouseHandler.cpp
blob: 605fe2c4616c09f1f475d82969e346ee2d2769fb (plain) (blame)
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
/*
 # Copyright (c) 2009 - OpenSLX Project, Computer Center University of Freiburg
 #
 # This program is free software distributed under the GPL version 2.
 # See http://openslx.org/COPYING
 #
 # If you have any feedback please consult http://openslx.org/feedback and
 # send your suggestions, praise, or complaints to feedback@openslx.org
 #
 # General information about OpenSLX can be found at http://openslx.org/
 # --------------------------------------------------------------------------
 # x11FakeMouseHandler.h:
 #  - Handle mouse events on X11 - implementation
 # --------------------------------------------------------------------------
 */

#include "x11FakeMouseHandler.h" // need to include before X headers
#include <src/util/consoleLogger.h>
#include <X11/extensions/XTest.h>
#include "x11InputUtils.h"

void X11FakeMouseButtonHandler::doHandle(InputEvent const& evt, InputEventContext const*)
{
	quint16 pressedButton = evt.pressedButton();

	Display* dpy = X11InputUtils::display();

	XTestGrabControl(dpy, 1);
	for(int i = 0; i < 16; i++) {
		if((1<<i) == pressedButton)
		{
			ConsoleLog writeLine(QString("Got mouse button event: button %1 %2").arg(i + 1).arg(evt.isPress() ? "pressed" : "released"));
			if(!XTestFakeButtonEvent(dpy, i + 1, evt.isPress(), CurrentTime))
			{
				ConsoleLog writeLine("[ERROR] XTestFakeButtonEvent failed");
			}
		}
	}
	XTestGrabControl(dpy, 0);

	// Since there may not be a mainloop running, we need to manually flush the event queue
	XFlush(dpy);
}

void X11FakeMouseMovementHandler::doHandle(InputEvent const& evt, InputEventContext const*)
{
	ConsoleLog writeLine(QString("Received mouse motion event (%1,%2)").arg(evt.xCoord()).arg(evt.yCoord()));
	Display* dpy = X11InputUtils::display();
	int screen = 0 /* DefaultScreen(dpy) */;
	XTestGrabControl(dpy, 1);
	if(!XTestFakeMotionEvent(dpy, screen, evt.xCoord(), evt.yCoord(), CurrentTime))
	{
		ConsoleLog writeLine("[ERROR] XTestFakeMotionEvent failed");
	}
	XTestGrabControl(dpy, 0);

	// Since there may not be a mainloop running, we need to manually flush the event queue
	XFlush(dpy);
}