-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
139 lines (112 loc) · 3.5 KB
/
main.c
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
132
133
134
135
136
137
138
139
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <assert.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void Reparent(Display *d, Window child, Window new_parent) {
XUnmapWindow(d, child);
XMapWindow(d, new_parent);
XSync(d, False);
XReparentWindow(d, child, new_parent, 0, 0);
XMapWindow(d, child);
usleep(1e3);
XSync(d, False);
}
Window focused, new_parent;
Display *d;
int s;
volatile sig_atomic_t done = 0;
// handler to recover focus from the window it was spawned from
void term(int signum) {
done = 1;
XClientMessageEvent event; // dummy event to wake up XNextEvent
memset(&event, 0, sizeof(XClientMessageEvent));
event.type = ClientMessage;
event.format = 32;
XSendEvent(d, new_parent, 0, 0, (XEvent *)&event);
XFlush(d);
}
int main(int argc, char **argv) {
assert(argc == 4);
Display *display = XOpenDisplay(NULL);
Screen *screen = DefaultScreenOfDisplay(display);
int dwidth = screen->width;
int dheight = screen->height;
XCloseDisplay(display);
int width;
int height;
sscanf(argv[1], "%d", &width);
sscanf(argv[2], "%d", &height);
int left = (dwidth - width) / 2;
int top = (dheight - height) / 2;
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_handler = term;
sigaction(SIGTERM, &action, NULL);
d = XOpenDisplay(XDisplayName(NULL));
s = DefaultScreen(d);
Window root = RootWindow(d, s);
int param = 0;
XGetInputFocus(d, &focused, ¶m);
Window child;
sscanf(argv[3], "%d", &child);
XSetWindowAttributes attrs = {ParentRelative,
0L,
0,
0L,
0,
0,
Always,
0L,
0L,
False,
StructureNotifyMask | ExposureMask |
ButtonPressMask | ButtonReleaseMask,
0L,
True,
0,
0};
attrs.border_pixel = 4288570538; // purple
int border_width = 4;
new_parent = XCreateWindow(
d, DefaultRootWindow(d), left, top, width, height, border_width, 0,
InputOutput, DefaultVisual(d, DefaultScreen(d)),
CWColormap | CWBorderPixel | CWBackPixel | CWOverrideRedirect, &attrs);
XClassHint classhint = {"above_all", "above_all"};
XSetClassHint(d, new_parent, &classhint);
XAddToSaveSet(d, child);
Reparent(d, child, new_parent);
XSetInputFocus(d, child, RevertToNone, 0);
XResizeWindow(d, child, width, height);
XSelectInput(d, new_parent,
ExposureMask | EnterWindowMask | LeaveWindowMask |
FocusChangeMask);
XEvent e;
while (!done) {
XEvent e;
XNextEvent(d, &e);
// focus with mouse pointer
if (e.type == EnterNotify) {
XSetInputFocus(d, child, RevertToNone, 0);
}
// focus with mouse pointer
if (e.type == LeaveNotify) {
XSetInputFocus(d, s, RevertToNone, 0);
}
// ignore focus with mod key
if (e.type == FocusOut) {
XSetInputFocus(d, child, RevertToNone, 0);
XNextEvent(d, &e); // ignore FocusOut
XNextEvent(d, &e); // ignore FocusIn
}
}
XSetInputFocus(d, focused, RevertToNone, 0);
XCloseDisplay(d);
return 0;
}