-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathperWindowLayoutD.cpp
More file actions
127 lines (97 loc) · 2.99 KB
/
perWindowLayoutD.cpp
File metadata and controls
127 lines (97 loc) · 2.99 KB
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
#include <iostream>
#include <map>
#include <string>
#include <stdio.h>
#include <unistd.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/XKBlib.h>
#include <X11/Xmd.h>
#include <X11/Xutil.h>
using namespace std;
Display *display;
int xkbEventType;
Window root;
typedef int Layout;
const Layout Unknown = -1;
typedef map<Window, Layout> WindowLayouts;
WindowLayouts windowLayouts;
const char *prefix = "perWindowLayout:: ";
const static long rootEvents = StructureNotifyMask | SubstructureNotifyMask | FocusChangeMask | KeymapStateMask;
void init() {
int xkbError, reason;
int major = XkbMajorVersion;
int minor = XkbMinorVersion;
display = XkbOpenDisplay(0, &xkbEventType, &xkbError, &major, &minor, &reason);
XkbSelectEventDetails(display, XkbUseCoreKbd, XkbStateNotify, XkbAllStateComponentsMask, XkbGroupStateMask);
root = DefaultRootWindow(display);
XSelectInput(display, root, rootEvents);
cout << prefix << "init: major=" << major << " minor=" << minor << " root=" << root << "\n";
}
Layout getCurrentLayout() {
XkbStateRec state;
if (XkbGetState(display, XkbUseCoreKbd, &state) == Success)
return state.locked_group;
return Unknown;
}
bool wrongWindow(Window window) {
return window == PointerRoot || window == None;
}
Window focusedWindow() {
Window window;
int param;
XGetInputFocus(display, &window, ¶m);
if (wrongWindow(window))
return window;
for (;;) {
unsigned numChildren;
Window *children, parent, root2;
XQueryTree(display, window, &root2, &parent, &children, &numChildren);
if (numChildren)
XFree(children);
if (parent == root)
break;
window = parent;
}
return window;
}
void proceedEvent(XEvent ev) {
if (ev.type == ConfigureNotify) {
Window window = focusedWindow();
if (wrongWindow(window))
return;
Layout layout = getCurrentLayout();
WindowLayouts::const_iterator i = windowLayouts.find(window);
Layout saved = i != windowLayouts.end() ? i->second : 0;
cout << prefix << "ConfigureNotify: window=" << window << " layout=" << layout << " saved=" << saved << "\n";
if (layout != saved)
XkbLockGroup(display, XkbUseCoreKbd, saved);
} else if (ev.type == xkbEventType) {
Window window = focusedWindow();
Layout layout = getCurrentLayout();
windowLayouts[window] = layout;
cout << prefix << "xkbEventType: window=" << window << " layout=" << layout << "\n";
}
}
void mainLoop() {
for (;;) {
XEvent ev = { 0 };
XNextEvent(display, &ev);
proceedEvent(ev);
}
}
int main(int argc, char **argv) {
string noDaemonArg = "-n", helpArg = "-h";
if (argc > 2 || (argc == 2 && argv[1] != noDaemonArg) || (argc == 2 && argv[1] == helpArg)) {
cout << "Keep per-window keyboard layout\n\nUsage: perWindowLayout [ -n ]\n\t-n\tdo not daemonize\n";
return 1;
}
bool noDaemon = argc == 2 && argv[1] == noDaemonArg;
if (!noDaemon && daemon(0, 0) != 0) {
cerr << "Failed to daemonize\n";
return 1;
}
init();
mainLoop();
return 0;
}