-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
290 lines (257 loc) · 8.69 KB
/
client.c
File metadata and controls
290 lines (257 loc) · 8.69 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xrender.h>
#include <cairo/cairo-xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <evec.h>
#include <string.h>
#include "client.h"
#include "window.h"
#include "core.h"
evec_t client_frame_map;
void client__initialize_map(void)
{
client_frame_map = evec__new(sizeof(client_t));
return;
}
void client__free_map(void)
{
evec__free(&client_frame_map);
return;
}
void client__forget(client_t* client)
{
cairo_destroy(client->cr);
cairo_surface_destroy(client->surface);
free(client->name);
free(client->icon);
memset(client, 0, sizeof(client_t));
u16 client_place = ((((u8*)(client)) - client_frame_map.data)/sizeof(client_t))+1;
if (client_place < client_frame_map.size)
memmove(client, client+1, (client_frame_map.size - client_place)*sizeof(client_t));
--client_frame_map.size;
return;
}
void client__store(client_t* client)
{
evec__push(&client_frame_map, client);
return /*(client_t*)evec__at(&client_frame_map, client_frame_map.size-1)*/;
}
client_t* client__retrieve_from(Window window, bool frame)
{
for (u8 i = 0; i < client_frame_map.size; ++i)
{
if (frame)
{
if (((client_t*)evec__at(&client_frame_map, i))->frame == window)
return ((client_t*)evec__at(&client_frame_map, i));
}
else
{
if (((client_t*)evec__at(&client_frame_map, i))->client == window)
return ((client_t*)evec__at(&client_frame_map, i));
}
}
return NULL;
}
Visual* get_argb_visual(Display* dpy)
{
XVisualInfo vinfo_template = {0};
vinfo_template.screen = DefaultScreen(dpy);
int nitems;
XVisualInfo* info = XGetVisualInfo(dpy, VisualScreenMask, &vinfo_template, &nitems);
if (!XMatchVisualInfo(dpy, DefaultScreen(dpy), 32, TrueColor, info))
{
fprintf(stderr, "error: no ARGB visual found\n");
exit(1);
}
for (int i = 0; i < nitems; ++i)
{
XRenderPictFormat* pict = XRenderFindVisualFormat(dpy, info[i].visual);
if (pict && pict->type == PictTypeDirect && pict->direct.alphaMask)
{
Visual* v = info[i].visual;
XFree(info);
return v;
}
}
XFree(info);
return DefaultVisual(dpy, DefaultScreen(dpy));
}
cairo_surface_t* client__get_cairo_surface(Drawable frame, Display* dpy, u32 w, u32 h, Visual* visual)
{
Visual* _visual = visual;
if (!_visual)
_visual = get_argb_visual(dpy);
return cairo_xlib_surface_create(
dpy,
frame,
_visual,
w,
h
);
}
void client__create(Window window, Display* dpy, Window root)
{
XWindowAttributes attr;
XGetWindowAttributes(dpy, window, &attr);
XMoveWindow(dpy, window, attr.x + CORNER_RADIUS, attr.y + CORNER_RADIUS);
XGetWindowAttributes(dpy, window, &attr);
Visual* visual = get_argb_visual(dpy);
u8 depth = 32;
Colormap colormap = XCreateColormap(dpy, root, visual, AllocNone);
XSetWindowAttributes sattr;
sattr.colormap = colormap;
sattr.background_pixel = 0;
sattr.border_pixel = 0;
sattr.override_redirect = true;
Window frame = XCreateWindow(
dpy,
root,
attr.x - CORNER_RADIUS,
attr.y - CORNER_RADIUS,
attr.width + CORNER_RADIUS * 2,
attr.height + CORNER_RADIUS * 2 + TITLEBAR_HEIGHT,
0,
depth,
InputOutput,
visual,
CWColormap | CWBackPixel | CWBorderPixel | CWOverrideRedirect,
&sattr
);
XAddToSaveSet(dpy, window);
XSelectInput(dpy, frame,
SubstructureRedirectMask | SubstructureNotifyMask |
ButtonPressMask | ButtonReleaseMask |
PointerMotionMask | StructureNotifyMask |
ExposureMask | KeyPressMask |
KeyReleaseMask
);
XReparentWindow(dpy, window, frame, CORNER_RADIUS, CORNER_RADIUS + TITLEBAR_HEIGHT);
XMapWindow(dpy, window);
XMapWindow(dpy, frame);
char* name = window__get_name(window, dpy);
u32* icon = window__get_icon(window, dpy);
cairo_surface_t* surface = client__get_cairo_surface(
frame,
dpy,
XDisplayWidth(dpy, DefaultScreen(dpy)),
XDisplayWidth(dpy, DefaultScreen(dpy)),
visual
);
cairo_t* cr = cairo_create(surface);
client_t client = {
.client = window,
.frame = frame,
.frame_x = attr.x - CORNER_RADIUS,
.frame_y = attr.y - CORNER_RADIUS,
.frame_w = attr.width + CORNER_RADIUS * 2,
.frame_h = attr.height + CORNER_RADIUS * 2 + TITLEBAR_HEIGHT,
.client_w = attr.width,
.client_h = attr.height,
.surface = surface,
.cr = cr,
.name = name,
.icon = icon
};
logf("client.frame_x = %u", client.frame_x);
logf("client.frame_y = %u", client.frame_y);
logf("client.frame_w = %u", client.frame_w);
logf("client.frame_h = %u", client.frame_h);
client__store(&client);
client__redraw_all_decorations(dpy);
return;
}
void client__redraw_all_decorations(Display* dpy)
{
for (u16 i = 0; i < client_frame_map.size; ++i)
{
if (evec__at(&client_frame_map, i) && ((client_t*) evec__at(&client_frame_map, i))->frame)
{
/*XClearArea(dpy, ((client_t*) evec__at(&client_frame_map, i))->frame, 0, 0, 0, 0, True);
XClearWindow(dpy, ((client_t*) evec__at(&client_frame_map, i))->frame);
XClearArea(dpy, ((client_t*) evec__at(&client_frame_map, i))->client, 0, 0, 0, 0, True);
XClearWindow(dpy, ((client_t*) evec__at(&client_frame_map, i))->client);*/
window__draw_decorations(
((client_t*) evec__at(&client_frame_map, i)),
dpy,
0,
0,
true
);
}
}
}
bool client__can_close(XEvent* ev, client_t* client, Display* dpy)
{
int click_x = ev->xbutton.x;
int click_y = ev->xbutton.y;
int w = client->frame_w;
if (click_y < TITLEBAR_HEIGHT && click_x >= w - TITLEBAR_HEIGHT)
{
Atom* protocols;
int n;
Atom wm_delete = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
Atom wm_protocols = XInternAtom(dpy, "WM_PROTOCOLS", False);
if (XGetWMProtocols(dpy, client->client, &protocols, &n))
{
for (int i = 0; i < n; ++i)
{
if (protocols[i] == wm_delete)
{
XEvent msg = {0};
msg.xclient.type = ClientMessage;
msg.xclient.message_type = wm_protocols;
msg.xclient.display = dpy;
msg.xclient.window = client->client;
msg.xclient.format = 32;
msg.xclient.data.l[0] = wm_delete;
msg.xclient.data.l[1] = CurrentTime;
logf("Closing client");
XSendEvent(dpy, client->client, False, NoEventMask, &msg);
XFree(protocols);
return true;
}
}
XFree(protocols);
}
XKillClient(dpy, client->client);
return true;
}
else
return false;
}
bool client__can_resize(XEvent* ev, client_t* client, Display* dpy, bool* is_resizing)
{
int click_x = ev->xbutton.x;
int click_y = ev->xbutton.y;
int w = client->frame_w;
int h = client->frame_h;
if (
(click_x < TITLEBAR_HEIGHT && (click_y > h - TITLEBAR_HEIGHT && click_y < h)) ||
(click_x > w - TITLEBAR_HEIGHT && (click_y > h - TITLEBAR_HEIGHT && click_y < h)) ||
(click_x > w - TITLEBAR_HEIGHT && (click_y > TITLEBAR_HEIGHT && click_y < TITLEBAR_HEIGHT + CORNER_RADIUS)) ||
(click_x < TITLEBAR_HEIGHT && (click_y > TITLEBAR_HEIGHT && click_y < TITLEBAR_HEIGHT + CORNER_RADIUS))
)
{
client->drag_x = ev->xbutton.x_root;
client->drag_y = ev->xbutton.y_root;
client->state |= IS_RESIZING_MASK; //TODO: replace with top level bool
client->state &= ~(RESIZE_REGION_MASK & ~IS_RESIZING_MASK);
client->state |= (
(click_x > w - TITLEBAR_HEIGHT && (click_y > h - TITLEBAR_HEIGHT && click_y < h)) ?
BOTTOM_RIGHT_RESIZE_MASK
: (click_x > w - TITLEBAR_HEIGHT && (click_y > TITLEBAR_HEIGHT && click_y < TITLEBAR_HEIGHT + CORNER_RADIUS)) ?
TOP_RIGHT_RESIZE_MASK
: (click_x < TITLEBAR_HEIGHT && (click_y > TITLEBAR_HEIGHT && click_y < TITLEBAR_HEIGHT + CORNER_RADIUS)) ?
TOP_LEFT_RESIZE_MASK
:0
);
*is_resizing = true;
return true;
}
*is_resizing = false;
return false;
}