forked from mackstann/tinywm
-
Notifications
You must be signed in to change notification settings - Fork 4
/
tinywm.h
239 lines (225 loc) · 6.85 KB
/
tinywm.h
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// tinywm, 8l, zlib license, 2016-2018
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>
#include <algorithm>
#include <vector>
#include <stdio.h>
#define SCREEN_WIDTH 1920
#define SCREEN_HEIGHT 1080
#define SIDE_WIN_RESIZE 0
Display * dpy;
XEvent ev;
Window w = None, zoom = None, focus = None, root = None;
XWindowAttributes attr;
XButtonEvent start{0};
unsigned short rect[4]={0};
bool close;
bool list_toggle;
void ListWindow(std::vector<Window> &l)
{
unsigned int cnt = 0;
Window r, p, *wlist;
XWindowAttributes attr;
XQueryTree(dpy, root, &r, &p, &wlist, &cnt);
for(unsigned int i = 0; i < cnt; ++i)
{
XGetWindowAttributes(dpy, wlist[i], &attr);
if(attr.map_state == IsViewable)
l.push_back(wlist[i]);
}
}
void SetInput(Display *dpy)
{
root = RootWindow(dpy, DefaultScreen(dpy));
XGrabKey(dpy, XKeysymToKeycode(dpy, XStringToKeysym("a")), Mod1Mask,DefaultRootWindow(dpy), True, GrabModeAsync, GrabModeAsync);
XGrabKey(dpy, XKeysymToKeycode(dpy, XStringToKeysym("c")), Mod1Mask,DefaultRootWindow(dpy), True, GrabModeAsync, GrabModeAsync);
XGrabKey(dpy, XKeysymToKeycode(dpy, XK_Escape), Mod1Mask,DefaultRootWindow(dpy), True, GrabModeAsync, GrabModeAsync);
XGrabKey(dpy, XKeysymToKeycode(dpy, XStringToKeysym("f")), Mod1Mask,DefaultRootWindow(dpy), True, GrabModeAsync, GrabModeAsync);
XGrabKey(dpy, XKeysymToKeycode(dpy, XStringToKeysym("h")), Mod1Mask,DefaultRootWindow(dpy), True, GrabModeAsync, GrabModeAsync);
XGrabKey(dpy, XKeysymToKeycode(dpy, XStringToKeysym("r")), Mod1Mask,DefaultRootWindow(dpy), True, GrabModeAsync, GrabModeAsync);
XGrabKey(dpy, XKeysymToKeycode(dpy, XStringToKeysym("t")), Mod1Mask,DefaultRootWindow(dpy), True, GrabModeAsync, GrabModeAsync);
XGrabKey(dpy, XKeysymToKeycode(dpy, XK_Tab), Mod1Mask,DefaultRootWindow(dpy), True, GrabModeAsync, GrabModeAsync);
XGrabKey(dpy, XKeysymToKeycode(dpy, XStringToKeysym("x")), Mod1Mask,DefaultRootWindow(dpy), True, GrabModeAsync, GrabModeAsync);
XGrabKey(dpy, XKeysymToKeycode(dpy, XStringToKeysym("z")), Mod1Mask,DefaultRootWindow(dpy), True, GrabModeAsync, GrabModeAsync);
XGrabButton(dpy, 1, Mod1Mask, DefaultRootWindow(dpy), True,
ButtonPressMask|ButtonReleaseMask|PointerMotionMask, GrabModeAsync, GrabModeAsync,
None, None);
XGrabButton(dpy, 3, Mod1Mask, DefaultRootWindow(dpy), True,
ButtonPressMask|ButtonReleaseMask|PointerMotionMask, GrabModeAsync, GrabModeAsync,
None, None);
XGrabButton(dpy, 8, 0, DefaultRootWindow(dpy), True,
ButtonPressMask|ButtonReleaseMask|PointerMotionMask, GrabModeAsync, GrabModeAsync,
None, None);
XGrabButton(dpy, 9, 0, DefaultRootWindow(dpy), True,
ButtonPressMask|ButtonReleaseMask|PointerMotionMask, GrabModeAsync, GrabModeAsync,
None, None);
XSetWindowBackground(dpy, root, 0x0192c6);
XClearWindow(dpy, root);
}
void ProcessWin()
{
XNextEvent(dpy,&ev);
std::vector<Window> list;
ListWindow(list);
for(Window w : list)
{
XSetWindowBorder(dpy, w, BlackPixel(dpy, DefaultScreen(dpy)));
XSetWindowBorderWidth(dpy, w, 1);
}
}
inline void ProcessKey(const XEvent &ev)
{
switch(ev.xkey.keycode)
{
case 23:
{
std::vector<Window> list;
ListWindow(list);
for(Window w : list)
if(focus != w)
{
focus = w;
XRaiseWindow(dpy, focus);
XSetInputFocus(dpy, focus, RevertToParent, CurrentTime);
break;
}
}
break;
case 27:
system("dmenu_run &");
break;
case 28:
system("term &");
break;
case 38:
{
std::vector<Window> list;
ListWindow(list);
switch(list.size())
case 2:
if(list_toggle)
{
list_toggle = false;
XMoveResizeWindow(dpy,list[1],0,0,SCREEN_WIDTH*2/3-1,SCREEN_HEIGHT);
XMoveResizeWindow(dpy,list[0],SCREEN_WIDTH*2/3,0,SCREEN_WIDTH*1/3,SCREEN_HEIGHT);
}
else
{
list_toggle = true;
XMoveResizeWindow(dpy,list[0],0,0,SCREEN_WIDTH*2/3-1,SCREEN_HEIGHT);
XMoveResizeWindow(dpy,list[1],SCREEN_WIDTH*2/3,0,SCREEN_WIDTH*1/3,SCREEN_HEIGHT);
}
break;
}
break;
case 41:
system("firefox &");
break;
case 43:
if(ev.xkey.subwindow != None && w == None)
{
w = ev.xkey.subwindow;
XUnmapWindow(dpy, ev.xkey.subwindow);
}
else
if(w != None)
{
XMapWindow(dpy, w);
w = None;
}
break;
case 52:
if(ev.xkey.subwindow != None && zoom == None)
{
zoom = ev.xkey.subwindow;
XGetWindowAttributes(dpy, zoom, &attr);
rect[0]=attr.x;
rect[1]=attr.y;
rect[2]=attr.width;
rect[3]=attr.height;
XMoveResizeWindow(dpy,zoom,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
}
else
if(zoom != None)
{
XMoveResizeWindow(dpy,zoom,rect[0],rect[1],rect[2],rect[3]);
zoom = None;
}
break;
case 53:
if(ev.xkey.subwindow != None && close == 0)
{
close = 1;
XKillClient(dpy, ev.xkey.subwindow);
}
break;
case 54:
system("chrome &");
break;
//todo: create little window
case 9:
exit(0);
}
ProcessWin();
}
inline void ProcessMouse(const XEvent &ev)
{
switch(ev.type)
{
case ButtonPress:
if(ev.xbutton.subwindow != None)
{
switch(ev.xbutton.button)
{
//page up
case 8:
XTestFakeKeyEvent(dpy,XKeysymToKeycode(dpy, XK_Page_Up), 1, CurrentTime);
XTestFakeKeyEvent(dpy,XKeysymToKeycode(dpy, XK_Page_Up), 0, CurrentTime);
break;
//page down
case 9:
XTestFakeKeyEvent(dpy,XKeysymToKeycode(dpy, XK_Page_Down), 1, CurrentTime);
XTestFakeKeyEvent(dpy,XKeysymToKeycode(dpy, XK_Page_Down), 0, CurrentTime);
break;
default:
focus = ev.xbutton.subwindow;
XRaiseWindow(dpy, focus);
XSetInputFocus(dpy, focus, RevertToParent, CurrentTime);
XGetWindowAttributes(dpy, ev.xbutton.subwindow, &attr);
start = ev.xbutton;
break;
}
}
break;
case ButtonRelease:
{
start.subwindow = None;
#ifdef SIDE_WIN_RESIZE
if(ev.xbutton.x_root > SCREEN_WIDTH - 5)
XMoveResizeWindow(dpy,ev.xbutton.subwindow,SCREEN_WIDTH*2/3,0,SCREEN_WIDTH*1/3,SCREEN_HEIGHT);
else if(ev.xbutton.x_root < 5)
XMoveResizeWindow(dpy,ev.xbutton.subwindow,0,0,SCREEN_WIDTH*2/3,SCREEN_HEIGHT);
#endif
}
break;
case MotionNotify:
if(start.subwindow != None)
{
int xdiff = ev.xbutton.x_root - start.x_root;
int ydiff = ev.xbutton.y_root - start.y_root;
XMoveResizeWindow(dpy, start.subwindow,
attr.x + (start.button==1 ? xdiff : 0),
attr.y + (start.button==1 ? ydiff : 0),
std::max(1, attr.width + (start.button==3 ? xdiff : 0)),
std::max(1, attr.height + (start.button==3 ? ydiff : 0)));
XEvent event;
event.type = Expose;
event.xexpose.window = root;
XSendEvent(dpy, root, False, ExposureMask, &event);
XSync(dpy, 0);
}
break;
}
}