-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib-input.c
555 lines (488 loc) · 11.4 KB
/
lib-input.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/*
* Copyright Neil Brown ©2015-2023 <[email protected]>
* May be distributed under terms of GPLv2 - see file:COPYING
*
* Core input translation.
* This module transalates keystrokes and mouse events into commands.
* This involves tracking the current 'mode' state.
*
*/
#define _GNU_SOURCE /* for asprintf */
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
#define PANE_DATA_TYPE struct input_mode
#include "core.h"
#define LOGSIZE 128
struct input_mode {
const char *mode safe;
const char *context safe; /* for status lines */
int num, num2;
struct pane *focus, *source, *grab;
struct mark *point;
struct mouse_state {
struct timespec last_action;
int last_x, last_y;
int click_count;
int click_on_up;
char *mod;
} buttons[3];
char *log[LOGSIZE];
int head;
};
#include "core-pane.h"
/* 'head' is 1 more than the last key added. */
static void log_add(struct input_mode *im safe,
const char *type safe, const char *key safe)
{
free(im->log[im->head]);
im->log[im->head] = strconcat(NULL, type, key);
im->head = (im->head + 1) % LOGSIZE;
}
static void log_dump(struct pane *p safe)
{
struct input_mode *im = p->data;
int i;
int cnt = 0;
char *line = NULL;
i = im->head;
do {
if (!im->log[i])
continue;
if (line)
line = strconcat(p, line, ", ", im->log[i]);
else
line = strconcat(p, im->log[i]);
cnt += 1;
if (cnt % 10 == 0) {
LOG("Input %d: %s", cnt/10, line);
line = NULL;
}
} while ((i = (i+1) % LOGSIZE) != im->head);
if (line)
LOG("Input: %s", line);
}
DEF_CMD(log_input)
{
log_dump(ci->home);
return 1;
}
static void report_status(struct pane *focus safe, struct input_mode *im safe)
{
char *st = NULL;
if (im->num == NO_NUMERIC && im->mode[0] == 0)
return;
if (im->num == NO_NUMERIC)
asprintf(&st, " %s", im->mode);
else if (im->num == -NO_NUMERIC)
asprintf(&st, " - %s", im->mode);
else
asprintf(&st, " %d %s", im->num, im->mode);
call("Message:modal", focus, 0, NULL, st);
}
DEF_CMD(set_mode)
{
struct input_mode *im = ci->home->data;
if (ci->str) {
free((void*)im->mode);
im->mode = strdup(ci->str);
}
if (ci->str2) {
free((void*)im->context);
im->context = strdup(ci->str2);
attr_set_str(&ci->home->attrs, "display-context", im->context);
call("Window:notify:display-context", ci->home);
}
report_status(ci->focus, im);
return 1;
}
DEF_CMD(set_num)
{
struct input_mode *im = ci->home->data;
im->num = ci->num;
report_status(ci->focus, im);
return 1;
}
DEF_CMD(set_num2)
{
struct input_mode *im = ci->home->data;
im->num2 = ci->num;
return 1;
}
DEF_CMD(set_all)
{
struct input_mode *im = ci->home->data;
if (ci->str) {
free((void*)im->mode);
im->mode = strdup(ci->str);
}
if (ci->str2) {
free((void*)im->context);
im->context = strdup(ci->str2);
attr_set_str(&ci->home->attrs, "display-context", im->context);
call("Window:notify:display-context", ci->home);
}
im->num = ci->num;
im->num2 = ci->num;
report_status(ci->focus, im);
return 1;
}
static const char *safe ctrl_map[][2] = {
{ ":Backspace", ":C-H" },
{ ":Enter", ":C-M" },
{ ":ESC", ":C-[" },
{ ":LF", ":C-J" },
{ ":Tab", ":C-I" },
{ ":Del", ":C-?" },
{ ":A:Backspace",":A:C-H" },
{ ":A:Enter", ":A:C-M" },
{ ":A:ESC", ":A:C-[" },
{ ":A:LF", ":A:C-J" },
{ ":A:Tab", ":A:C-I" },
{ ":A:Del", ":A:C-?" },
{ ":SPC", "- " },
};
static const char *map_key(const char *key safe)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(ctrl_map) ; i++) {
if (strcmp(key, ctrl_map[i][0]) == 0)
return ctrl_map[i][1];
}
return NULL;
}
DEF_CMD(keystroke)
{
const char *key;
struct input_mode *im = ci->home->data;
struct pane *p;
int ret;
int num = im->num;
int num2 = im->num2;
const char *mode = im->mode;
const char *alt;
struct mark *m;
if (!ci->str)
return Enoarg;
call("Window:notify:Keystroke-notify", ci->home, 0, NULL, ci->str);
log_add(im, "K", ci->str);
im->mode = strdup("");
im->num = NO_NUMERIC;
im->num2 = 0;
if (im->source != ci->focus) {
im->source = ci->focus;
im->focus = NULL;
im->point = NULL;
}
if (!im->focus || im->focus->focus) {
p = pane_focus(ci->focus);
im->focus = p;
pane_add_notify(ci->home, p, "Notify:Close");
}
p = im->focus;
if (!im->point)
im->point = call_ret(mark, "doc:point", p);
m = im->point;
key = strconcat(ci->home, "K", mode, ci->str);
time_starts(ci->home);
ret = call(key, p, num, m, NULL, num2);
if (ret == Efallthrough && (alt = map_key(ci->str)) != NULL) {
key = strconcat(ci->home, "K", mode, alt);
ret = call(key, p, num, m, NULL, num2);
}
time_ends(ci->home);
if (ret <= Efail)
call("Message:default", p, 0, NULL,
strconcat(ci->home, "** Command ", key, " Failed **"));
else if (ret == Efallthrough) {
key = strconcat(ci->home, "K", mode, ci->str);
call("Message:modal", p, 0, NULL,
strconcat(ci->home, "** Command ", key, " not known **"));
}
return Efallthrough;
}
DEF_CMD(keystroke_sequence)
{
struct pane *home = ci->home;
const char *c = ci->str;
const char *e, *dash;
int ret;
if (!c)
return Enoarg;
while ((e = strchr(c, ' ')) != NULL) {
dash = "";
if (*c != ':' || e == c+1)
dash = "-";
ret = call("Keystroke", home, 0, NULL,
strconcat(home, dash,
strnsave(home, c, e - c)));
if (ret < 0)
return Efail;
c = e+1;
}
if (!*c)
return 1;
dash = "";
if (*c != ':' || c[1] == '\0')
dash = "-";
ret = call("Keystroke", home, 0, NULL, strconcat(home, dash, c));
if (ret < 0)
return Efail;
return 1;
}
static int tspec_diff_ms(struct timespec *a safe, struct timespec *b safe)
{
return ((a->tv_sec - b->tv_sec) * 1000 +
(a->tv_nsec - b->tv_nsec) / 1000000);
}
DEF_CMD(mouse_event)
{
struct input_mode *im = ci->home->data;
struct xy xy;
int num, ex;
struct pane *focus;
char *key;
struct timespec now;
unsigned int b;
int press;
int r;
const char *mode;
const char *cmd;
char *mod; /* :A:C:S modifiers - optional */
struct mouse_state *ms = NULL;
clock_gettime(CLOCK_MONOTONIC, &now);
if (!ci->str)
return Enoarg;
call("Window:notify:Mouse-event-notify", ci->home,
ci->num, NULL, ci->str,
ci->num2);
log_add(im, "M", ci->str);
if (ci->num2 == 1) {
/* Press */
press = 1;
b = ci->num - 1;
} else if (ci->num2 == 2) {
/* Release */
press = 0;
b = ci->num - 1;
} else {
/* 3 is Motion */
press = 0;
b = 100;
}
if (b < 3) {
bool repeat = False;
ms = &im->buttons[b];
/* FIXME the max movement for a double-click should be
* about a char width - maybe something based on scale
*/
if (tspec_diff_ms(&now, &ms->last_action) <= 500 &&
(abs(ci->x - ms->last_x) +
abs(ci->y - ms->last_y)) <= 2)
/* FIXME should I let 'release' know that
* it was close to the 'press'??
*/
repeat = True;
else
/* Too much time or space has elapsed.
* This cannot be a click.
*/
ms->click_on_up = 0;
if (press) {
if (!repeat)
ms->click_count = 1;
else if (ms->click_count < 3)
ms->click_count += 1;
}
ms->last_action = now;
ms->last_x = ci->x; ms->last_y = ci->y;
}
focus = ci->focus;
num = im->num;
ex = im->num2;
/* FIXME is there any point in this? */
xy = pane_mapxy(ci->focus, focus, ci->x, ci->y, False);
mode = strsave(ci->home, im->mode);
free((void*)im->mode);
im->mode = strdup("");
im->num = NO_NUMERIC;
im->num2 = 0;
if (im->grab && !press) {
/* Release and Motion should go to the same
* place as Press went.
*/
focus = im->grab;
xy = pane_mapxy(focus, ci->focus, 0, 0, False);
xy.x = ci->x - xy.x;
xy.y = ci->y - xy.y;
} else while (1) {
struct pane *t, *chld = NULL;
list_for_each_entry(t, &focus->children, siblings) {
if (t->z < 0)
continue;
if (xy.x < t->x || xy.x >= t->x + t->w)
continue;
if (xy.y < t->y || xy.y >= t->y + t->h)
continue;
if (chld == NULL || t->z > chld->z)
chld = t;
}
/* descend into chld */
if (!chld)
break;
xy.x -= chld->x;
xy.y -= chld->y;
focus = chld;
}
if (im->grab != focus) {
im->grab = focus;
pane_add_notify(ci->home, focus, "Notify:Close");
}
if (!ms) {
int ret;
key = strconcat(ci->home, "M", mode, ci->str);
time_starts(ci->home);
ret = call(key, focus, num, NULL, NULL, ex, NULL, NULL,
xy.x, xy.y);
time_ends(ci->home);
return ret;
}
if (press) {
/* identify and save modifiers */
free(ms->mod);
if (ci->str2)
mod = strdup(ci->str2);
else {
char *c = strrchr(ci->str, ':');
if (c)
mod = strndup(ci->str, c - ci->str);
else
mod = strdup("");
}
ms->mod = mod;
}
if (press)
cmd = "Press-";
else if (ms->click_on_up)
cmd = "Click-";
else
cmd = "Release-";
/* Try :nPress :(n-1)Press ... (or :nRelease or :nClick)
* until something gets a result.
* 'n' is T (triple) or D(double) or ""(Single).
* If nothing got a result for a Press,, register for
* 'click' on release.
*/
ms->click_on_up = 1;
for (r = ms->click_count; r >= 1 ; r--) {
int ret;
char *mult = "\0\0D\0T" + (r-1)*2;
char n[2];
n[0] = '1' + b;
n[1] = 0;
key = strconcat(ci->home, "M", mode, ms->mod, ":", mult,
cmd, n);
time_starts(ci->home);
ret = call(key, focus, num, NULL, NULL, ex,
NULL, NULL, xy.x, xy.y);
time_ends(ci->home);
if (ret > 0) {
/* If this is 'press', then don't want
* click_on_up. If this is release, it
* don't matter what we set.
*/
ms->click_on_up = 0;
return ret;
}
}
return Efalse;
}
DEF_CMD(mouse_grab)
{
struct input_mode *im = ci->home->data;
im->grab = ci->focus;
pane_add_notify(ci->home, ci->focus, "Notify:Close");
return 1;
}
DEF_CMD(input_cancel)
{
/* Other handlers fall-through. We catch so that
* it doesn't appear to be ignored
*/
return 1;
}
DEF_CMD(refocus)
{
struct input_mode *im = ci->home->data;
im->focus = NULL;
im->point = NULL;
im->source = NULL;
return Efallthrough;
}
DEF_CMD(close_focus)
{
struct input_mode *im = ci->home->data;
if (im->focus == ci->focus) {
im->focus = NULL;
im->point = NULL;
im->source = NULL;
}
if (im->grab == ci->focus)
im->grab = NULL;
return 1;
}
DEF_CMD_CLOSED(input_close)
{
struct input_mode *im = ci->home->data;
int i;
for (i = 0; i < 3; i++) {
free(im->buttons[i].mod);
im->buttons[i].mod = NULL;
}
free((void*)im->mode);
free((void*)im->context);
return 1;
}
static struct map *im_map;
static void register_map(void)
{
if (im_map)
return;
im_map = key_alloc();
key_add(im_map, "Keystroke", &keystroke);
key_add(im_map, "Keystroke-sequence", &keystroke_sequence);
key_add(im_map, "Mouse-event", &mouse_event);
key_add(im_map, "Mouse-grab", &mouse_grab);
key_add(im_map, "Mode:set-mode", &set_mode);
key_add(im_map, "Mode:set-num", &set_num);
key_add(im_map, "Mode:set-num2", &set_num2);
key_add(im_map, "Mode:set-all", &set_all);
key_add(im_map, "pane:refocus", &refocus);
key_add(im_map, "Notify:Close", &close_focus);
key_add(im_map, "input:log", &log_input);
key_add(im_map, "Close", &input_close);
key_add(im_map, "Cancel", &input_cancel);
key_add(im_map, "Abort", &input_cancel);
}
DEF_LOOKUP_CMD(input_handle, im_map);
DEF_CMD(input_attach)
{
struct input_mode *im;
struct pane *p;
register_map();
p = pane_register(ci->focus, 0, &input_handle.c);
if (!p)
return Efail;
im = p->data;
im->mode = strdup("");
im->context = strdup("");
im->num = NO_NUMERIC;
im->num2 = 0;
return comm_call(ci->comm2, "callback:attach", p);
}
void edlib_init(struct pane *ed safe)
{
call_comm("global-set-command", ed, &input_attach, 0, NULL, "attach-input");
}