summaryrefslogtreecommitdiff
path: root/src/globalshortcuts/globalshortcuts_linux.cpp
blob: 76179f1c6567a70329ab8bbb03fef7982d187505 (plain)
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
/*  smplayer, GUI front-end for mplayer.
    Copyright (C) 2006-2017 Ricardo Villalba <rvm@users.sourceforge.net>

    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; either version 2 of the License, or
    (at your option) any later version.

    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, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <X11/Xlib.h>
#include <X11/XF86keysym.h>
#include <qpa/qplatformnativeinterface.h>
#include <xcb/xcb.h>

class X11Data {
public:
	X11Data() {
		QPlatformNativeInterface *native = qApp->platformNativeInterface();
		void *display = native->nativeResourceForScreen(QByteArray("display"),
                                                        QGuiApplication::primaryScreen());
		disp = reinterpret_cast<Display *>(display);
	}

	Display *display() {
		return disp;
	}

	Window rootWindow() {
		return DefaultRootWindow(display());
	}

	bool isValid() { return disp != 0; }

	quint32 keysymToKeycode(KeySym keysym) {
		if (isValid()) {
			return XKeysymToKeycode(display(), keysym);
		}
		return 0;
	}

private:
	Display * disp;
};


bool GlobalShortcuts::nativeEventFilter(const QByteArray & eventType, void * message, long * /*result*/) {
	//qDebug() << "GlobalShortcuts::nativeEventFilter";

	if (!isEnabled()) return false;

	xcb_key_press_event_t *kev = 0;
	if (eventType == "xcb_generic_event_t") {
		xcb_generic_event_t *ev = static_cast<xcb_generic_event_t *>(message);
		if ((ev->response_type & 127) == XCB_KEY_PRESS) {
			kev = static_cast<xcb_key_press_event_t *>(message);
		}

		if (kev != 0) {
			unsigned int keycode = kev->detail;
			qDebug() << "GlobalShortcuts::nativeEventFilter: keycode:" << keycode;

			if (key_list.contains(keycode)) {
				activateShortcut(key_list[keycode]);
				return true;
			}
		}
	}

	return false;
}

bool GlobalShortcuts::registerShortcut(quint32 nativeKey, quint32 nativeMods) {
	qDebug() << "GlobalShortcuts::registerShortcut: nativeKey:" << nativeKey;

	X11Data x11;
	if (x11.isValid()) {
		Display * display = x11.display();
		Window window = x11.rootWindow();

		Bool owner = True;
		int pointer = GrabModeAsync;
		int keyboard = GrabModeAsync;

		XGrabKey(display, nativeKey, nativeMods, window, owner, pointer, keyboard);
		XGrabKey(display, nativeKey, nativeMods | Mod2Mask, window, owner, pointer, keyboard);
		XSync(display, False);

		return true;
	}
	return false;
}

bool GlobalShortcuts::unregisterShortcut(quint32 nativeKey, quint32 nativeMods) {
	qDebug() << "GlobalShortcuts::unregisterShortcut: nativeKey:" << nativeKey;

	X11Data x11;
	if (x11.isValid()) {
		Display * display = x11.display();
		Window window = x11.rootWindow();

		XUngrabKey(display, nativeKey, nativeMods, window);
		XUngrabKey(display, nativeKey, nativeMods | Mod2Mask, window);
		XSync(display, False);
		return true;
	}
	return false;
}

void GlobalShortcuts::createKeysList() {
	X11Data x11;
	key_list.clear();
	key_list[x11.keysymToKeycode(XF86XK_AudioLowerVolume)] = Qt::Key_VolumeDown;
	key_list[x11.keysymToKeycode(XF86XK_AudioRaiseVolume)] = Qt::Key_VolumeUp;
	key_list[x11.keysymToKeycode(XF86XK_AudioMute)] = Qt::Key_VolumeMute;
	key_list[x11.keysymToKeycode(XF86XK_AudioPlay)] = Qt::Key_MediaPlay;
	key_list[x11.keysymToKeycode(XF86XK_AudioStop)] = Qt::Key_MediaStop;
	key_list[x11.keysymToKeycode(XF86XK_AudioPrev)] = Qt::Key_MediaPrevious;
	key_list[x11.keysymToKeycode(XF86XK_AudioNext)] = Qt::Key_MediaNext;
	key_list[x11.keysymToKeycode(XF86XK_AudioPause)] = Qt::Key_MediaPause;
	key_list[x11.keysymToKeycode(XF86XK_AudioRecord)] = Qt::Key_MediaRecord;
	//key_list[x11.keysymToKeycode(0xffc8)] = Qt::Key_F11; // Test
}