-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestxhotkeys.cpp
65 lines (58 loc) · 1.57 KB
/
testxhotkeys.cpp
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
#include "testxhotkeys.h"
#include <iostream>
inline static displayptr_t make_displayptr(char *display_name)
{
return { XOpenDisplay(display_name), &XCloseDisplay };
}
TestHotkeys::TestHotkeys() :
_displayptr(make_displayptr(nullptr)),
_root(DefaultRootWindow(_displayptr.get())),
_hotkeys(_displayptr.get(), _root)
{ }
int TestHotkeys::run()
{
add_action(SayHello, XK_H, ControlMask | ShiftMask);
add_action(Quit, XK_F11, ControlMask | ShiftMask);
std::cout << "Waiting for hotkey..." << std::endl;
event_loop();
std::cout << "successful" << std::endl;
return 0;
}
void TestHotkeys::add_action(Action action, KeySym keysym, unsigned modifiers)
{
XHotkeys::keyid_t id = _hotkeys.grab(keysym, modifiers);
_idaction.emplace(id, action);
std::cout << std::hex;
std::cout << "Mapped action " << action << " to id " << id << std::endl;
std::cout << std::dec;
}
void TestHotkeys::event_loop() const
{
XSelectInput(_displayptr.get(), _root, KeyPressMask);
while(1) {
XEvent ev;
XNextEvent(_displayptr.get(), &ev);
XHotkeys::keyid_t id;
if (_hotkeys.is_hotkey_event(ev, &id)) {
if (handle_action(_idaction.at(id)))
break;
}
}
}
bool TestHotkeys::handle_action(Action action) const
{
switch (action) {
case SayHello:
std::cout << "Hello!" << std::endl;
break;
case Quit:
break;
default:
std::cout << "No action for that hotkey" << std::endl;
};
return action == Quit;
}
int main()
{
return TestHotkeys().run();
}