-
Notifications
You must be signed in to change notification settings - Fork 1
/
xmain.c
455 lines (376 loc) · 9.01 KB
/
xmain.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>
#include <err.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/XKBlib.h>
#include <X11/keysymdef.h>
#include <X11/Xatom.h>
#include <cairo-xlib.h>
#include "mace.h"
#include "resources/xlib-keysyms.h"
static Display *display;
static Window win;
static int screen;
static int width, height;
static cairo_surface_t *sfc;
static cairo_t *cr;
static Atom XA_TARGETS;
static Atom clipatom;
static uint8_t *clip;
static size_t cliplen;
void
setclipboard(uint8_t *data, size_t len)
{
if (clip != NULL) {
free(clip);
}
clip = data;
cliplen = len;
XSetSelectionOwner(display, clipatom, win, CurrentTime);
}
uint8_t *
getclipboard(size_t *len)
{
Atom actualtype;
int actualformat;
size_t bytesafter;
uint8_t *ret = NULL;
Window w;
XEvent e;
w = XGetSelectionOwner(display, clipatom);
if (w == win) {
*len = cliplen;
return clip;
} else if (w == BadWindow) {
*len = 0;
return NULL;
}
XConvertSelection(display, clipatom, XA_STRING, clipatom,
win, CurrentTime);
while (true) {
if (!XCheckTypedEvent(display, SelectionNotify, &e)) {
continue;
} else if (e.type != SelectionNotify) {
continue;
}
if (e.xselection.property != None
&& e.xselection.target == XA_STRING) {
XGetWindowProperty(display, win, e.xselection.property, 0,
16 * 1024, False, AnyPropertyType,
&actualtype, &actualformat, &cliplen,
&bytesafter, &ret);
if (clip != NULL) {
free(clip);
}
clip = malloc(cliplen);
if (clip == NULL) {
return NULL;
}
memmove(clip, ret, cliplen);
XFree(ret);
*len = cliplen;
return clip;
} else {
return NULL;
}
}
}
static void
xresize(struct mace *m, int w, int h)
{
width = w;
height = h;
cairo_xlib_surface_set_size(sfc, w, h);
if (!maceresize(m, w, h)) {
/* What should actually happen here? */
errx(1, "Failed to resize!");
}
}
static int32_t
symtounicode(KeySym sym)
{
int32_t code = 0;
int i;
/* Fuck Xorg */
/* Keysyms under 0x100 are ASCII so unicode. */
if (0x20 <= sym && sym < 0x100) {
code = sym;
/* At this point unicode was around and people decieded to
just shift the values. */
} else if (0x01000100 <= sym && sym <= 0x0110ffff) {
code = sym - 0x01000000;
/* But before that weird things happened. */
} else if (0x0100 <= sym && sym <= 0x20ff) {
for (i = 0;
i < sizeof(keymappings) / sizeof(keymappings[0]); i++) {
if (keymappings[i].keysym == sym) {
code = keymappings[i].unicode;
break;
}
}
}
return code;
}
static size_t
encodekey(KeySym sym, uint8_t *s, size_t n, bool *special)
{
int32_t code;
*special = true;
switch (sym) {
case XK_Shift_L:
case XK_Shift_R:
case XK_Alt_L:
case XK_Alt_R:
case XK_Meta_L:
case XK_Meta_R:
case XK_Super_L:
case XK_Super_R:
case XK_Control_L:
case XK_Control_R:
return 0;
case XK_Left:
strcpy((char *) s, "Left");
break;
case XK_Right:
strcpy((char *) s, "Right");
break;
case XK_Up:
strcpy((char *) s, "Up");
break;
case XK_Down:
strcpy((char *) s, "Down");
break;
case XK_Page_Up:
strcpy((char *) s, "PageUp");
break;
case XK_Page_Down:
strcpy((char *) s, "PageDown");
break;
case XK_Home:
strcpy((char *) s, "Home");
break;
case XK_End:
strcpy((char *) s, "End");
break;
case XK_Return:
strcpy((char *) s, "Return");
break;
case XK_Tab:
strcpy((char *) s, "Tab");
break;
case XK_BackSpace:
strcpy((char *) s, "BackSpace");
break;
case XK_Delete:
strcpy((char *) s, "Delete");
break;
case XK_Escape:
strcpy((char *) s, "Escape");
break;
default:
*special = false;
code = symtounicode(sym);
if (code == 0) {
return 0;
}
return utf8encode(s, n, code);
}
return strlen((char *) s);
}
static bool
xhandlekeypress(struct mace *m, XKeyEvent *e)
{
size_t kn, nn = 0;
uint8_t s[32], k[32];
bool special;
KeySym sym;
sym = XkbKeycodeToKeysym(display, e->keycode, 0, 0);
kn = encodekey(sym, k, sizeof(k), &special);
if (!special) {
sym = XkbKeycodeToKeysym(display, e->keycode, 0,
e->state & (ShiftMask | LockMask));
kn = encodekey(sym, k, sizeof(k), &special);
}
if (special && (e->state & ShiftMask) != 0) {
memmove(s + nn, "S-", 2);
nn += 2;
special = true;
}
if ((e->state & ControlMask) != 0) {
memmove(s + nn, "C-", 2);
nn += 2;
special = true;
}
if ((e->state & Mod1Mask) != 0) {
memmove(s + nn, "A-", 2);
nn += 2;
special = true;
}
if ((e->state & Mod4Mask) != 0) {
memmove(s + nn, "M-", 2);
nn += 2;
special = true;
}
memmove(s + nn, k, kn);
if (nn + kn > 0) {
return handlekey(m, s, nn + kn, special);
} else {
return false;
}
}
static bool
xhandlebuttonpress(struct mace *m, XButtonEvent *e)
{
switch (e->button) {
case 1:
case 2:
case 3:
return handlebuttonpress(m, e->x, e->y, e->button);
case 4:
return handlescroll(m, e->x, e->y, 0, -m->font->lineheight);
case 5:
return handlescroll(m, e->x, e->y, 0, m->font->lineheight);
default:
return false;
}
}
static bool
xhandlebuttonrelease(struct mace *m, XButtonEvent *e)
{
switch (e->button) {
case 1:
case 2:
case 3:
return handlebuttonrelease(m, e->x, e->y, e->button);
default:
return false;
}
}
static bool
xhandlemotion(struct mace *m, XMotionEvent *e)
{
return handlemotion(m, e->x, e->y);
}
/* This is not a very good implimentation but it works. */
static void
xhandleselectionrequest(XSelectionRequestEvent *e)
{
Atom targets[] = { XA_TARGETS, XA_STRING };
XSelectionEvent *r;
XEvent rr;
r = &rr.xselection;
r->type = SelectionNotify;
r->requestor = e->requestor;
r->selection = e->selection;
r->target = e->target;
r->time = CurrentTime;
if (e->target == XA_TARGETS) {
XChangeProperty(display, e->requestor, e->property,
XA_ATOM, 32, PropModeReplace,
(const uint8_t *) targets, sizeof(targets) / sizeof(Atom));
r->property = e->property;
} else if (e->target == XA_STRING) {
r->property = e->property;
XChangeProperty(display, e->requestor, e->property,
e->target, 8, PropModeReplace,
clip, cliplen);
} else {
r->property = None;
}
XSendEvent(display, e->requestor, True, 0, &rr);
}
static void
eventLoop(struct mace *m)
{
bool redraw;
XEvent e;
while (m->running) {
XNextEvent(display, &e);
redraw = false;
switch (e.type) {
case ConfigureNotify:
if (e.xconfigure.width != width
|| e.xconfigure.height != height) {
xresize(m, e.xconfigure.width, e.xconfigure.height);
redraw = true;
}
break;
case SelectionRequest:
xhandleselectionrequest(&e.xselectionrequest);
break;
case Expose:
redraw = true;
break;
case KeyPress:
redraw = xhandlekeypress(m, &e.xkey);
break;
case ButtonPress:
redraw = xhandlebuttonpress(m, &e.xbutton);
break;
case ButtonRelease:
redraw = xhandlebuttonrelease(m, &e.xbutton);
break;
case MotionNotify:
redraw = xhandlemotion(m, &e.xmotion);
break;
/* Handle window close event? */
case ClientMessage:
return;
}
if (redraw) {
cairo_push_group(cr);
macedraw(m, cr);
cairo_pop_group_to_source(cr);
cairo_paint(cr);
cairo_surface_flush(sfc);
}
}
}
int
displayinit(struct mace *m)
{
int width, height;
width = 800;
height = 500;
display = XOpenDisplay(NULL);
if (display == NULL) {
fprintf(stderr, "Failed to open X display!");
return EXIT_FAILURE;
}
screen = DefaultScreen(display);
XA_TARGETS = XInternAtom(display, "TARGETS", False);
clipatom = XInternAtom(display, "CLIPBOARD", 0);
win = XCreateSimpleWindow(display, RootWindow(display,
screen),
0, 0, width, height, 5, 0, 0);
XSelectInput(display, win,
ExposureMask | StructureNotifyMask
| KeyPressMask | KeyReleaseMask | PointerMotionMask
| ButtonPressMask | ButtonReleaseMask);
XSetStandardProperties(display, win, "Mace", "Mace",
None, NULL, 0, NULL);
XMapWindow(display, win);
sfc = cairo_xlib_surface_create(display, win,
DefaultVisual(display, screen),
width, height);
cr = cairo_create(sfc);
xresize(m, width, height);
cairo_push_group(cr);
macedraw(m, cr);
cairo_pop_group_to_source(cr);
cairo_paint(cr);
cairo_surface_flush(sfc);
return EXIT_SUCCESS;
}
int
displayloop(struct mace *m)
{
eventLoop(m);
cairo_destroy(cr);
cairo_surface_destroy(sfc);
XDestroyWindow(display, win);
XCloseDisplay(display);
return EXIT_SUCCESS;
}