-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.c
More file actions
1503 lines (1409 loc) · 38.6 KB
/
Copy pathinput.c
File metadata and controls
1503 lines (1409 loc) · 38.6 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
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-3.0-only
/*
* Copyright (C) 2026 robinpie <robin@dreamstation.systems>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/* input.c - input routing: keyboard (WM chords vs. forward-to-app) and mouse
* (focus, title-bar drag-move, border-resize, content forwarding, taskbar).
* notcurses and GPM (bare console) both feed one source-agnostic mouse_event.
*/
#define _GNU_SOURCE
#include "viewpoint.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#define MOVE_STEP 2
#define RESIZE_STEP 1
/* Effective modifier mask of a key event (defined below, used by the keymap
* helpers above it). */
static unsigned eff_mods(const ncinput *ni);
static void toggle_mode(WM *wm)
{
wm->mode = (wm->mode == MODE_INTERPRET) ? MODE_PASSTHROUGH :
MODE_INTERPRET;
taskbar_damage(wm);
for (int i = 0; i < wm->nwins; i++) {
window_damage_frame(
wm->wins[i]); /* per-window mode indicator */
}
vp_log("mode=%s\n",
wm->mode == MODE_PASSTHROUGH ? "PASSTHRU" : "INTERPRET");
}
/* The built-in keymap, in one editable table - these are the *defaults*; the
* config file (bind/unbind lines) edits the live copy in WM.config. 'mods' is
* matched exactly against the SHIFT/ALT/CTRL bits (lock bits are ignored). */
#define A NCKEY_MOD_ALT
#define AS (NCKEY_MOD_ALT | NCKEY_MOD_SHIFT)
#define S NCKEY_MOD_SHIFT
static const keychord g_default_keymap[] = {
{ NCKEY_TAB, A, ACT_FOCUS_NEXT },
{ NCKEY_TAB, AS, ACT_FOCUS_PREV },
{ NCKEY_F04, A, ACT_CLOSE },
{ 'n', A, ACT_NEW },
{ 'm', A, ACT_MIN },
{ 'x', A, ACT_MAXTOGGLE },
{ NCKEY_LEFT, A, ACT_MOVE_L },
{ NCKEY_RIGHT, A, ACT_MOVE_R },
{ NCKEY_UP, A, ACT_MOVE_U },
{ NCKEY_DOWN, A, ACT_MOVE_D },
{ NCKEY_LEFT, AS, ACT_RESIZE_L },
{ NCKEY_RIGHT, AS, ACT_RESIZE_R },
{ NCKEY_UP, AS, ACT_RESIZE_U },
{ NCKEY_DOWN, AS, ACT_RESIZE_D },
{ NCKEY_PGUP, S, ACT_SCROLL_UP },
{ NCKEY_PGDOWN, S, ACT_SCROLL_DOWN },
{ 'c', A, ACT_COPY },
{ 'v', A, ACT_PASTE },
{ '1', A, ACT_SLOT_1 },
{ '2', A, ACT_SLOT_2 },
{ '3', A, ACT_SLOT_3 },
{ '4', A, ACT_SLOT_4 },
{ '5', A, ACT_SLOT_5 },
{ '6', A, ACT_SLOT_6 },
{ '7', A, ACT_SLOT_7 },
{ '8', A, ACT_SLOT_8 },
{ '9', A, ACT_SLOT_9 },
{ ',', A, ACT_TASKBAR_L },
{ '.', A, ACT_TASKBAR_R },
};
#undef A
#undef AS
#undef S
/* Action names accepted in `bind` lines, paired with a human label (shown in
* the in-app settings editor) and the enum. Row order here is the order the
* settings editor lists actions in. */
static const struct {
const char *name;
const char *label;
vp_action act;
} g_action_names[] = {
{ "focus_next", "Focus next window", ACT_FOCUS_NEXT },
{ "focus_prev", "Focus previous window", ACT_FOCUS_PREV },
{ "new", "New window", ACT_NEW },
{ "close", "Close window", ACT_CLOSE },
{ "minimize", "Minimize window", ACT_MIN },
{ "maximize", "Maximize / restore", ACT_MAXTOGGLE },
{ "move_left", "Move window left", ACT_MOVE_L },
{ "move_right", "Move window right", ACT_MOVE_R },
{ "move_up", "Move window up", ACT_MOVE_U },
{ "move_down", "Move window down", ACT_MOVE_D },
{ "resize_left", "Resize window left", ACT_RESIZE_L },
{ "resize_right", "Resize window right", ACT_RESIZE_R },
{ "resize_up", "Resize window up", ACT_RESIZE_U },
{ "resize_down", "Resize window down", ACT_RESIZE_D },
{ "scroll_up", "Scroll back (history)", ACT_SCROLL_UP },
{ "scroll_down", "Scroll forward", ACT_SCROLL_DOWN },
{ "copy", "Copy selection", ACT_COPY },
{ "paste", "Paste clipboard", ACT_PASTE },
{ "slot1", "Focus taskbar slot 1", ACT_SLOT_1 },
{ "slot2", "Focus taskbar slot 2", ACT_SLOT_2 },
{ "slot3", "Focus taskbar slot 3", ACT_SLOT_3 },
{ "slot4", "Focus taskbar slot 4", ACT_SLOT_4 },
{ "slot5", "Focus taskbar slot 5", ACT_SLOT_5 },
{ "slot6", "Focus taskbar slot 6", ACT_SLOT_6 },
{ "slot7", "Focus taskbar slot 7", ACT_SLOT_7 },
{ "slot8", "Focus taskbar slot 8", ACT_SLOT_8 },
{ "slot9", "Focus taskbar slot 9", ACT_SLOT_9 },
{ "taskbar_left", "Scroll taskbar left", ACT_TASKBAR_L },
{ "taskbar_right", "Scroll taskbar right", ACT_TASKBAR_R },
};
#define ACTION_COUNT ((int)(sizeof(g_action_names) / sizeof(g_action_names[0])))
/* Named non-printable keys accepted in chord strings. Single printable
* characters (e.g. "n", "x", "/") and function keys ("f1".."f60") are handled
* separately in key_from_name(). */
static const struct {
const char *name;
uint32_t id;
} g_key_names[] = {
{ "tab", NCKEY_TAB },
{ "enter", NCKEY_ENTER },
{ "return", NCKEY_ENTER },
{ "esc", NCKEY_ESC },
{ "escape", NCKEY_ESC },
{ "space", ' ' },
{ "backspace", NCKEY_BACKSPACE },
{ "delete", NCKEY_DEL },
{ "del", NCKEY_DEL },
{ "insert", NCKEY_INS },
{ "ins", NCKEY_INS },
{ "home", NCKEY_HOME },
{ "end", NCKEY_END },
{ "pgup", NCKEY_PGUP },
{ "pageup", NCKEY_PGUP },
{ "pgdn", NCKEY_PGDOWN },
{ "pagedown", NCKEY_PGDOWN },
{ "up", NCKEY_UP },
{ "down", NCKEY_DOWN },
{ "left", NCKEY_LEFT },
{ "right", NCKEY_RIGHT },
};
static bool ci_eq(const char *a, const char *b)
{
for (; *a && *b; a++, b++) {
if (tolower((unsigned char)*a) != tolower((unsigned char)*b)) {
return false;
}
}
return *a == *b;
}
static bool parse_action(const char *name, vp_action *out)
{
for (size_t i = 0;
i < sizeof(g_action_names) / sizeof(g_action_names[0]); i++) {
if (ci_eq(name, g_action_names[i].name)) {
*out = g_action_names[i].act;
return true;
}
}
return false;
}
static bool key_from_name(const char *name, uint32_t *id)
{
if (name[0] == '\0') {
return false;
}
/* single printable character: use its codepoint (lowercased if a letter, so
* the SHIFT modifier alone expresses the upper case). */
if (name[1] == '\0') {
unsigned char c = (unsigned char)name[0];
*id = isalpha(c) ? (uint32_t)tolower(c) : (uint32_t)c;
return true;
}
if ((name[0] == 'f' || name[0] == 'F') &&
isdigit((unsigned char)name[1])) {
char *end = NULL;
long n = strtol(name + 1, &end, 10);
if (end && *end == '\0' && n >= 1 && n <= 60) {
*id = (uint32_t)(NCKEY_F01 + (n - 1));
return true;
}
}
for (size_t i = 0; i < sizeof(g_key_names) / sizeof(g_key_names[0]);
i++) {
if (ci_eq(name, g_key_names[i].name)) {
*id = g_key_names[i].id;
return true;
}
}
return false;
}
/* Parse "alt+shift+left" into an id + modifier mask. The last '+'-separated
* token is the key; the earlier ones are modifiers. */
static bool parse_chord(const char *str, uint32_t *id_out, unsigned *mods_out)
{
char buf[64];
size_t len = strlen(str);
if (len >= sizeof(buf)) {
return false;
}
memcpy(buf, str, len + 1); /* length already bounded above */
unsigned mods = 0;
char *tok = buf;
for (;;) {
char *plus = strchr(tok, '+');
if (!plus) {
break; /* tok is the final segment: the key itself */
}
*plus = '\0';
if (ci_eq(tok, "alt") || ci_eq(tok, "meta")) {
mods |= NCKEY_MOD_ALT;
} else if (ci_eq(tok, "shift")) {
mods |= NCKEY_MOD_SHIFT;
} else if (ci_eq(tok, "ctrl") || ci_eq(tok, "control") ||
ci_eq(tok, "ctl")) {
mods |= NCKEY_MOD_CTRL;
} else {
return false; /* unknown modifier */
}
tok = plus + 1;
}
uint32_t id;
if (!key_from_name(tok, &id)) {
return false;
}
*id_out = id;
*mods_out = mods;
return true;
}
static void keymap_put(VpConfig *cfg, uint32_t id, unsigned mods, vp_action act)
{
for (int i = 0; i < cfg->nkeys; i++) {
if (cfg->keymap[i].id == id && cfg->keymap[i].mods == mods) {
cfg->keymap[i].act = act;
return;
}
}
if (cfg->nkeys == cfg->keycap) {
int ncap = cfg->keycap ? cfg->keycap * 2 : 16;
keychord *n = realloc(cfg->keymap, (size_t)ncap * sizeof(*n));
if (!n) {
return;
}
cfg->keymap = n;
cfg->keycap = ncap;
}
cfg->keymap[cfg->nkeys++] = (keychord){ id, mods, act };
}
void keymap_load_defaults(VpConfig *cfg)
{
int n = (int)(sizeof(g_default_keymap) / sizeof(g_default_keymap[0]));
cfg->keymap = malloc((size_t)n * sizeof(keychord));
if (cfg->keymap) {
memcpy(cfg->keymap, g_default_keymap,
(size_t)n * sizeof(keychord));
cfg->nkeys = n;
cfg->keycap = n;
} else {
cfg->nkeys = 0;
cfg->keycap = 0;
}
cfg->toggle_key = VP_TOGGLE_KEY;
}
bool keymap_bind(VpConfig *cfg, const char *chord, const char *action)
{
uint32_t id;
unsigned mods;
vp_action act;
if (!parse_chord(chord, &id, &mods)) {
vp_log("config: bad key chord '%s'\n", chord);
return false;
}
if (!parse_action(action, &act)) {
vp_log("config: unknown action '%s'\n", action);
return false;
}
keymap_put(cfg, id, mods, act);
return true;
}
bool keymap_unbind(VpConfig *cfg, const char *chord)
{
/* "unbind = all" clears the whole table - used by the in-app save so the
* written file is a complete snapshot rather than an overlay on defaults. */
if (ci_eq(chord, "all") || strcmp(chord, "*") == 0) {
cfg->nkeys = 0;
return true;
}
uint32_t id;
unsigned mods;
if (!parse_chord(chord, &id, &mods)) {
vp_log("config: bad key chord '%s'\n", chord);
return false;
}
for (int i = 0; i < cfg->nkeys; i++) {
if (cfg->keymap[i].id == id && cfg->keymap[i].mods == mods) {
for (int j = i; j < cfg->nkeys - 1; j++) {
cfg->keymap[j] = cfg->keymap[j + 1];
}
cfg->nkeys--;
return true;
}
}
return true; /* nothing bound to that chord - not an error */
}
bool keymap_set_toggle(VpConfig *cfg, const char *chord)
{
uint32_t id;
unsigned mods;
if (!parse_chord(chord, &id, &mods)) {
vp_log("config: bad toggle chord '%s'\n", chord);
return false;
}
cfg->toggle_key = id; /* modifiers are ignored for the toggle */
return true;
}
int keymap_action_count(void)
{
return ACTION_COUNT;
}
bool keymap_action_info(int row, vp_action *act, const char **label)
{
if (row < 0 || row >= ACTION_COUNT) {
return false;
}
if (act)
*act = g_action_names[row].act;
if (label)
*label = g_action_names[row].label;
return true;
}
const char *keymap_action_name(vp_action act)
{
for (int i = 0; i < ACTION_COUNT; i++) {
if (g_action_names[i].act == act) {
return g_action_names[i].name;
}
}
return "?";
}
void keymap_format_chord(uint32_t id, unsigned mods, char *buf, size_t n)
{
if (n == 0) {
return;
}
buf[0] = '\0';
size_t len = 0;
#define APPEND(s) \
do { \
int _w = snprintf(buf + len, n - len, "%s", (s)); \
if (_w > 0) { \
len += (size_t)_w; \
if (len >= n) \
len = n - 1; \
} \
} while (0)
if (mods & NCKEY_MOD_CTRL)
APPEND("ctrl+");
if (mods & NCKEY_MOD_ALT)
APPEND("alt+");
if (mods & NCKEY_MOD_SHIFT)
APPEND("shift+");
if (id >= NCKEY_F01 && id <= NCKEY_F60) {
char fb[8];
snprintf(fb, sizeof(fb), "f%u", (unsigned)(id - NCKEY_F01 + 1));
APPEND(fb);
} else {
const char *named = NULL;
for (size_t i = 0;
i < sizeof(g_key_names) / sizeof(g_key_names[0]); i++) {
if (g_key_names[i].id == id) {
named = g_key_names[i].name;
break;
}
}
if (named) {
APPEND(named);
} else if (id >= 0x21 && id < 0x7f) { /* printable ASCII */
char cb[2] = { (char)id, '\0' };
APPEND(cb);
} else {
char hb[16];
snprintf(hb, sizeof(hb), "0x%x", id);
APPEND(hb);
}
}
#undef APPEND
}
bool keymap_chord_for_action(const VpConfig *cfg, vp_action act, char *buf,
size_t n)
{
for (int i = 0; i < cfg->nkeys; i++) {
if (cfg->keymap[i].act == act) {
keymap_format_chord(cfg->keymap[i].id,
cfg->keymap[i].mods, buf, n);
return true;
}
}
if (n) {
snprintf(buf, n, "%s", "(unbound)");
}
return false;
}
/* Translate a live keypress into a chord, rejecting bare modifier/lock keys and
* mouse events. */
bool input_key_is_modifier(const ncinput *ni)
{
switch (ni->id) {
case NCKEY_LSHIFT:
case NCKEY_RSHIFT:
case NCKEY_LCTRL:
case NCKEY_RCTRL:
case NCKEY_LALT:
case NCKEY_RALT:
case NCKEY_LSUPER:
case NCKEY_RSUPER:
case NCKEY_LHYPER:
case NCKEY_RHYPER:
case NCKEY_LMETA:
case NCKEY_RMETA:
case NCKEY_CAPS_LOCK:
case NCKEY_NUM_LOCK:
case NCKEY_SCROLL_LOCK:
return true;
default:
return false;
}
}
bool keymap_chord_from_input(const ncinput *ni, uint32_t *id_out,
unsigned *mods_out)
{
uint32_t id = ni->id;
if (id == 0 || nckey_mouse_p(id)) {
return false;
}
if (input_key_is_modifier(ni)) {
return false;
}
if (id < 0x80 && isalpha((int)id)) {
id = (uint32_t)tolower((int)id);
}
*id_out = id;
*mods_out = eff_mods(ni);
return true;
}
/* Make `act` reachable solely via (id,mods): drop any other chords currently
* bound to it, then set this chord (overriding whatever it pointed at before). */
void keymap_rebind_action(VpConfig *cfg, vp_action act, uint32_t id,
unsigned mods)
{
for (int i = 0; i < cfg->nkeys;) {
if (cfg->keymap[i].act == act) {
for (int j = i; j < cfg->nkeys - 1; j++) {
cfg->keymap[j] = cfg->keymap[j + 1];
}
cfg->nkeys--;
} else {
i++;
}
}
keymap_put(cfg, id, mods, act);
}
void keymap_unbind_action(VpConfig *cfg, vp_action act)
{
for (int i = 0; i < cfg->nkeys;) {
if (cfg->keymap[i].act == act) {
for (int j = i; j < cfg->nkeys - 1; j++) {
cfg->keymap[j] = cfg->keymap[j + 1];
}
cfg->nkeys--;
} else {
i++;
}
}
}
/* Effective modifier mask. notcurses carries modifiers two ways that aren't
* kept in sync (legacy ni->alt/shift/ctrl bools vs. the kitty/CSI-u
* ni->modifiers bitmask) - consult both or chords vanish on some terminals. */
static unsigned eff_mods(const ncinput *ni)
{
unsigned mods = ni->modifiers;
if (ni->alt)
mods |= NCKEY_MOD_ALT;
if (ni->shift)
mods |= NCKEY_MOD_SHIFT;
if (ni->ctrl)
mods |= NCKEY_MOD_CTRL;
return mods & (NCKEY_MOD_ALT | NCKEY_MOD_SHIFT | NCKEY_MOD_CTRL);
}
static void focus_slot(WM *wm, int slot)
{
if (slot < 0 || slot >= wm->nwins) {
return;
}
Window *win = wm->wins[slot];
if (win->minimized) {
wm_restore(wm, win);
} else {
wm_focus_window(wm, win);
}
}
static void do_action(WM *wm, vp_action act)
{
switch (act) {
case ACT_FOCUS_NEXT:
wm_focus_next(wm, +1);
break;
case ACT_FOCUS_PREV:
wm_focus_next(wm, -1);
break;
case ACT_CLOSE:
wm_close_focused(wm);
break;
case ACT_NEW:
wm_spawn_window(wm);
break;
case ACT_MIN:
wm_minimize(wm, wm_focused(wm));
break;
case ACT_MAXTOGGLE:
wm_toggle_maximize(wm, wm_focused(wm));
break;
case ACT_MOVE_L:
wm_move_focused(wm, -MOVE_STEP, 0);
break;
case ACT_MOVE_R:
wm_move_focused(wm, +MOVE_STEP, 0);
break;
case ACT_MOVE_U:
wm_move_focused(wm, 0, -MOVE_STEP);
break;
case ACT_MOVE_D:
wm_move_focused(wm, 0, +MOVE_STEP);
break;
case ACT_RESIZE_L:
wm_resize_focused(wm, -RESIZE_STEP, 0);
break;
case ACT_RESIZE_R:
wm_resize_focused(wm, +RESIZE_STEP, 0);
break;
case ACT_RESIZE_U:
wm_resize_focused(wm, 0, -RESIZE_STEP);
break;
case ACT_RESIZE_D:
wm_resize_focused(wm, 0, +RESIZE_STEP);
break;
case ACT_SCROLL_UP:
case ACT_SCROLL_DOWN: {
Window *win = wm_focused(wm);
if (win) {
int page = win->rows > 1 ? win->rows - 1 : 1;
vt_scroll(win, act == ACT_SCROLL_UP ? +page : -page);
}
break;
}
case ACT_COPY:
sel_copy(wm, wm_focused(wm));
break;
case ACT_PASTE:
clipboard_paste(wm, wm_focused(wm));
break;
case ACT_SLOT_1:
case ACT_SLOT_2:
case ACT_SLOT_3:
case ACT_SLOT_4:
case ACT_SLOT_5:
case ACT_SLOT_6:
case ACT_SLOT_7:
case ACT_SLOT_8:
case ACT_SLOT_9:
focus_slot(wm, (int)(act - ACT_SLOT_1));
break;
case ACT_TASKBAR_L:
taskbar_scroll_by(wm, -1);
break;
case ACT_TASKBAR_R:
taskbar_scroll_by(wm, +1);
break;
}
}
bool input_handle_key(WM *wm, const ncinput *ni)
{
if (wm->settings.open) {
settings_handle_key(wm, ni);
return true;
}
if (ni->id == wm->config.toggle_key) {
toggle_mode(wm);
return true;
}
if (wm->mode != MODE_INTERPRET) {
return false;
}
unsigned mods = eff_mods(ni);
/* Fold back to lowercase, matching keymap_chord_from_input: notcurses
* uppercases ASCII letters when Ctrl/Shift is held, but the keymap stores
* them lowercased so SHIFT alone expresses the upper case. */
uint32_t id = ni->id;
if (id < 0x80 && isalpha((int)id)) {
id = (uint32_t)tolower((int)id);
}
const VpConfig *cfg = &wm->config;
for (int i = 0; i < cfg->nkeys; i++) {
if (cfg->keymap[i].id == id && cfg->keymap[i].mods == mods) {
do_action(wm, cfg->keymap[i].act);
return true;
}
}
return false;
}
static Window *find_by_id(WM *wm, int id)
{
for (int i = 0; i < wm->nwins; i++) {
if (wm->wins[i]->id == id) {
return wm->wins[i];
}
}
return NULL;
}
/* Source-agnostic mouse model. notcurses funnels every backend (terminal mouse
* protocols and GPM on the console alike) into ncinput, which input_route_mouse
* turns into mouse_event() - so the WM mouse logic lives in exactly one place. */
typedef enum {
MEV_PRESS,
MEV_RELEASE,
MEV_MOTION,
MEV_SCROLL_UP,
MEV_SCROLL_DOWN,
MEV_SCROLL_LEFT,
MEV_SCROLL_RIGHT,
} mev_type;
static VTermModifier to_vmod(unsigned mods)
{
VTermModifier m = VTERM_MOD_NONE;
if (mods & NCKEY_MOD_SHIFT)
m |= VTERM_MOD_SHIFT;
if (mods & NCKEY_MOD_ALT)
m |= VTERM_MOD_ALT;
if (mods & NCKEY_MOD_CTRL)
m |= VTERM_MOD_CTRL;
return m;
}
/* ----- title-bar hit testing (must mirror frame_paint's layout) ----- */
typedef enum { HIT_MOVE, HIT_MIN, HIT_MAX, HIT_CLOSE } titlehit;
static titlehit titlebar_hit(const Window *win, int relx)
{
int w = win->w;
int btnx = w - 1 - 9; /* "[_][▢][x]" is 9 columns wide */
if (btnx > 4) {
if (relx >= btnx && relx < btnx + 3)
return HIT_MIN;
if (relx >= btnx + 3 && relx < btnx + 6)
return HIT_MAX;
if (relx >= btnx + 6 && relx < btnx + 9)
return HIT_CLOSE;
}
return HIT_MOVE;
}
/* Which resize edges (if any) the interior border cell (rely,relx) sits on.
* rely==0 is the title bar and is handled before this is consulted. */
static int border_edge(const Window *win, int rely, int relx)
{
int edge = 0;
if (relx == 0)
edge |= RZ_LEFT;
if (relx == win->w - 1)
edge |= RZ_RIGHT;
if (rely == win->h - 1)
edge |= RZ_BOTTOM;
return edge;
}
static void snap_geom(WM *wm, vp_snapzone z, int *gx, int *gy, int *gw, int *gh)
{
int W = (int)wm->scr_cols;
int H = (int)wm->scr_rows - (wm->taskbar ? 1 : 0);
int hw = W / 2, hh = H / 2;
switch (z) {
case SNAP_LEFT:
*gx = 0;
*gy = 0;
*gw = hw;
*gh = H;
break;
case SNAP_RIGHT:
*gx = hw;
*gy = 0;
*gw = W - hw;
*gh = H;
break;
case SNAP_TL:
*gx = 0;
*gy = 0;
*gw = hw;
*gh = hh;
break;
case SNAP_TR:
*gx = hw;
*gy = 0;
*gw = W - hw;
*gh = hh;
break;
case SNAP_BL:
*gx = 0;
*gy = hh;
*gw = hw;
*gh = H - hh;
break;
case SNAP_BR:
*gx = hw;
*gy = hh;
*gw = W - hw;
*gh = H - hh;
break;
case SNAP_MAX:
*gx = 0;
*gy = 0;
*gw = W;
*gh = H;
break;
default:
*gx = 0;
*gy = 0;
*gw = W;
*gh = H;
break;
}
}
static vp_snapzone snap_zone_at(WM *wm, int y, int x)
{
int W = (int)wm->scr_cols;
int H = (int)wm->scr_rows - (wm->taskbar ? 1 : 0);
bool L = x <= VP_SNAP_EDGE;
bool R = x >= W - 1 - VP_SNAP_EDGE;
bool top = y < H / 4;
bool bot = y > H - H / 4;
if (L)
return top ? SNAP_TL : bot ? SNAP_BL : SNAP_LEFT;
if (R)
return top ? SNAP_TR : bot ? SNAP_BR : SNAP_RIGHT;
if (y <= VP_SNAP_EDGE)
return SNAP_MAX; /* top edge (not a corner) maximizes */
return SNAP_NONE;
}
static void snap_hide(WM *wm)
{
if (wm->snap_layer) {
comp_layer_show(wm->snap_layer, false);
}
wm->snap_preview = SNAP_NONE;
}
/* The preview outline is a hollow rectangle, so it must not be treated as
* covering what it is drawn over - a bitmap under the outline stays visible. */
static void snap_paint(struct ncplane *p, bool full, void *user)
{
(void)full;
WM *wm = user;
vp_rect r = comp_layer_rect(wm->snap_layer);
int gh = r.h, gw = r.w;
ncplane_erase(p);
vp_setfg(p, wm->theme.snap_outline);
ncplane_set_bg_alpha(p, NCALPHA_TRANSPARENT);
ncplane_putegc_yx(p, 0, 0, "╔", NULL);
ncplane_putegc_yx(p, 0, gw - 1, "╗", NULL);
ncplane_putegc_yx(p, gh - 1, 0, "╚", NULL);
ncplane_putegc_yx(p, gh - 1, gw - 1, "╝", NULL);
for (int c = 1; c < gw - 1; c++) {
ncplane_putegc_yx(p, 0, c, "═", NULL);
ncplane_putegc_yx(p, gh - 1, c, "═", NULL);
}
for (int r2 = 1; r2 < gh - 1; r2++) {
ncplane_putegc_yx(p, r2, 0, "║", NULL);
ncplane_putegc_yx(p, r2, gw - 1, "║", NULL);
}
}
static void snap_show(WM *wm, vp_snapzone z)
{
if (z == SNAP_NONE) {
snap_hide(wm);
return;
}
int gx, gy, gw, gh;
snap_geom(wm, z, &gx, &gy, &gw, &gh);
if (gw < 2 || gh < 2) {
return;
}
if (!wm->snap_layer) {
vp_rect r = { gy, gx, gh, gw };
wm->snap_layer = comp_layer_new(wm->comp, VP_BAND_OVERLAY, r,
snap_paint, wm);
if (!wm->snap_layer) {
return;
}
uint64_t base =
0; /* transparent interior so windows show through */
ncchannels_set_fg_alpha(&base, NCALPHA_TRANSPARENT);
ncchannels_set_bg_alpha(&base, NCALPHA_TRANSPARENT);
ncplane_set_base(comp_layer_plane(wm->snap_layer), "", 0, base);
comp_layer_set_opaque(wm->snap_layer, false);
} else {
comp_layer_resize(wm->snap_layer, gh, gw);
comp_layer_move(wm->snap_layer, gy, gx);
comp_layer_show(wm->snap_layer, true);
comp_layer_damage(wm->snap_layer);
}
comp_layer_raise(
wm->snap_layer); /* above the taskbar, within the band */
wm->snap_preview = z;
}
/* True if absolute cell (y,x) is inside the window's content grid (i.e. on the
* text, not on the border or title bar). */
static bool in_content(const Window *win, int y, int x)
{
if (!win) {
return false;
}
int crow = y - (win->y + VP_BORDER);
int ccol = x - (win->x + VP_BORDER);
return crow >= 0 && ccol >= 0 && crow < win->rows && ccol < win->cols;
}
static void content_forward(Window *win, mev_type t, int btn, int y, int x,
unsigned mods)
{
if (!win) {
return;
}
int crow = y - (win->y + VP_BORDER);
int ccol = x - (win->x + VP_BORDER);
if (crow < 0 || ccol < 0 || crow >= win->rows || ccol >= win->cols) {
return;
}
VTermModifier vm = to_vmod(mods);
/* libvterm only emits a report if the app enabled mouse tracking, so it's
* always safe to forward; quiet apps simply ignore it. */
vt_mouse_move(win, crow, ccol, vm);
switch (t) {
case MEV_PRESS:
vt_mouse_button(win, btn, true, vm);
break;
case MEV_RELEASE:
vt_mouse_button(win, btn, false, vm);
break;
case MEV_SCROLL_UP:
vt_mouse_button(win, 4, true, vm);
break;
case MEV_SCROLL_DOWN:
vt_mouse_button(win, 5, true, vm);
break;
case MEV_SCROLL_LEFT:
vt_mouse_button(win, 6, true, vm);
break;
case MEV_SCROLL_RIGHT:
vt_mouse_button(win, 7, true, vm);
break;
case MEV_MOTION:
break; /* the move above is the event */
}
}
static uint64_t now_ns(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
}
static void update_drag(WM *wm, int y, int x)
{
/* Desktop-icon drag: just slide the tile under the pointer, clamped on the
* screen. Handled before the window lookup below, which doesn't apply. */
if (wm->drag == DRAG_ICON) {
vp_layer *icon = wm->drag_icon;
if (!icon) {
wm->drag = DRAG_NONE;
return;
}
vp_rect ir = comp_layer_rect(icon);
int ny = y - wm->drag_off_y;
int nx = x - wm->drag_off_x;
int maxy = (int)wm->scr_rows - (wm->taskbar ? 1 : 0) - ir.h;
int maxx = (int)wm->scr_cols - ir.w;
if (ny > maxy)
ny = maxy;
if (nx > maxx)
nx = maxx;
if (ny < 0)
ny = 0;
if (nx < 0)
nx = 0;
comp_layer_move(icon, ny, nx);
return;
}
Window *win = find_by_id(wm, wm->drag_win);
if (!win) {
wm->drag = DRAG_NONE;
return;
}
if (wm->drag == DRAG_MOVE) {
/* Dragging a maximized window's title bar un-maximizes it and rescales
* the grab offset proportionally. Requires real displacement, so a
* stationary click (incl. a double-click) leaves it maximized. */
if (win->maximized && (x - wm->drag_off_x != win->x ||
y - wm->drag_off_y != win->y)) {
wm->drag_off_x = (win->w > 1) ? wm->drag_off_x *
(win->sw - 1) /
(win->w - 1) :
0;
win->maximized = false;
window_set_geometry(win, win->x, win->y, win->sw,
win->sh);
}
int nx = x - wm->drag_off_x;
int ny = y - wm->drag_off_y;
if (ny < 0)
ny = 0;
if (ny > (int)wm->scr_rows - 1)
ny = (int)wm->scr_rows - 1;
if (nx < -(win->w - 2))
nx = -(win->w - 2);
if (nx > (int)wm->scr_cols - 2)
nx = (int)wm->scr_cols - 2;
window_set_geometry(win, nx, ny, win->w, win->h);
snap_show(wm, snap_zone_at(wm, y, x));
comp_layer_raise(win->frame);
return;
}
if (wm->drag == DRAG_RESIZE) {
int nx = win->x, ny = win->y, nw = win->w, nh = win->h;