-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathbutton.c
6241 lines (5625 loc) · 157 KB
/
button.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
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
/* $XTermId: button.c,v 1.669 2025/01/03 00:20:00 tom Exp $ */
/*
* Copyright 1999-2024,2025 by Thomas E. Dickey
*
* All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name(s) of the above copyright
* holders shall not be used in advertising or otherwise to promote the
* sale, use or other dealings in this Software without prior written
* authorization.
*
*
* Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
*
* All Rights Reserved
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation, and that the name of Digital Equipment
* Corporation not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior permission.
*
*
* DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
* DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
* ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
/*
button.c Handles button events in the terminal emulator.
does cut/paste operations, change modes via menu,
passes button events through to some applications.
J. Gettys.
*/
#include <xterm.h>
#include <stdio.h>
#include <ctype.h>
#include <assert.h>
#include <X11/Xatom.h>
#include <X11/Xmu/Atoms.h>
#include <X11/Xmu/StdSel.h>
#include <xutf8.h>
#include <fontutils.h>
#include <data.h>
#include <error.h>
#include <menu.h>
#include <charclass.h>
#include <xstrings.h>
#include <xterm_io.h>
#if OPT_SELECT_REGEX
#if defined(HAVE_PCRE2POSIX_H)
#include <pcre2posix.h>
/* pcre2 used to provide its "POSIX" entrypoints using the same names as the
* standard ones in the C runtime, but that never worked because the linker
* would use the C runtime. Debian patched the library to fix this symbol
* conflict, but overlooked the header file, and Debian's patch was made
* obsolete when pcre2 was changed early in 2019 to provide different names.
*
* Here is a workaround to make the older version of Debian's package work.
*/
#if !defined(PCRE2regcomp) && defined(HAVE_PCRE2REGCOMP)
#undef regcomp
#undef regexec
#undef regfree
#ifdef __cplusplus
extern "C" {
#endif
PCRE2POSIX_EXP_DECL int PCRE2regcomp(regex_t *, const char *, int);
PCRE2POSIX_EXP_DECL int PCRE2regexec(const regex_t *, const char *, size_t,
regmatch_t *, int);
PCRE2POSIX_EXP_DECL void PCRE2regfree(regex_t *);
#ifdef __cplusplus
} /* extern "C" */
#endif
#define regcomp(r,s,n) PCRE2regcomp(r,s,n)
#define regexec(r,s,n,m,x) PCRE2regexec(r,s,n,m,x)
#define regfree(r) PCRE2regfree(r)
#endif
/* end workaround... */
#elif defined(HAVE_PCREPOSIX_H)
#include <pcreposix.h>
#else /* POSIX regex.h */
#include <sys/types.h>
#include <regex.h>
#endif
#endif /* OPT_SELECT_REGEX */
#ifdef HAVE_X11_TRANSLATEI_H
#include <X11/ConvertI.h>
#include <X11/TranslateI.h>
#else
extern String _XtPrintXlations(Widget w,
XtTranslations xlations,
Widget accelWidget,
_XtBoolean includeRHS);
#endif
#define PRIMARY_NAME "PRIMARY"
#define CLIPBOARD_NAME "CLIPBOARD"
#define SECONDARY_NAME "SECONDARY"
#define AtomToSelection(d,n) \
(((n) == XA_CLIPBOARD(d)) \
? CLIPBOARD_CODE \
: (((n) == XA_SECONDARY) \
? SECONDARY_CODE \
: PRIMARY_CODE))
#define isSelectionCode(n) ((n) >= PRIMARY_CODE)
#define CutBufferToCode(n) ((n) + MAX_SELECTION_CODES)
#define okSelectionCode(n) (isSelectionCode(n) ? (n) : PRIMARY_CODE)
#if OPT_WIDE_CHARS
#include <ctype.h>
#include <wcwidth.h>
#else
#define CharacterClass(value) \
charClass[(value) & (int)((sizeof(charClass)/sizeof(charClass[0]))-1)]
#endif
/*
* We'll generally map rows to indices when doing selection.
* Simplify that with a macro.
*
* Note that ROW2INX() is safe to use with auto increment/decrement for
* the row expression since that is evaluated once.
*/
#define GET_LINEDATA(screen, row) \
getLineData(screen, ROW2INX(screen, row))
#define MaxMouseBtn 5
#define IsBtnEvent(event) ((event)->type == ButtonPress || (event)->type == ButtonRelease)
#define IsKeyEvent(event) ((event)->type == KeyPress || (event)->type == KeyRelease)
#define Coordinate(s,c) ((c)->row * MaxCols(s) + (c)->col)
static const CELL zeroCELL =
{0, 0};
#if OPT_DEC_LOCATOR
static Bool SendLocatorPosition(XtermWidget xw, XButtonEvent *event);
static void CheckLocatorPosition(XtermWidget xw, XButtonEvent *event);
#endif /* OPT_DEC_LOCATOR */
/* Multi-click handling */
#if OPT_READLINE
static Time lastButtonDownTime = 0;
static int ExtendingSelection = 0;
static Time lastButton3UpTime = 0;
static Time lastButton3DoubleDownTime = 0;
static CELL lastButton3; /* At the release time */
#endif /* OPT_READLINE */
static Char *SaveText(TScreen *screen, int row, int scol, int ecol,
Char *lp, int *eol);
static int Length(TScreen *screen, int row, int scol, int ecol);
static void ComputeSelect(XtermWidget xw, const CELL *startc, const CELL *endc,
Bool extend, Bool normal);
static void EditorButton(XtermWidget xw, XButtonEvent *event);
static void EndExtend(XtermWidget w, XEvent *event, String *params, Cardinal
num_params, Bool use_cursor_loc);
static void ExtendExtend(XtermWidget xw, const CELL *cell);
static void PointToCELL(TScreen *screen, int y, int x, CELL *cell);
static void ReHiliteText(XtermWidget xw, const CELL *first, const CELL *last);
static void SaltTextAway(XtermWidget xw, int which, const CELL *cellc, const CELL *cell);
static void SelectSet(XtermWidget xw, XEvent *event, String *params, Cardinal num_params);
static void SelectionReceived PROTO_XT_SEL_CB_ARGS;
static void StartSelect(XtermWidget xw, const CELL *cell);
static void TrackDown(XtermWidget xw, XButtonEvent *event);
static void TrackText(XtermWidget xw, const CELL *first, const CELL *last);
static void UnHiliteText(XtermWidget xw);
static void _OwnSelection(XtermWidget xw, String *selections, Cardinal count);
static void do_select_end(XtermWidget xw, XEvent *event, String *params,
Cardinal *num_params, Bool use_cursor_loc);
#define MOUSE_LIMIT (255 - 32)
/* Send SET_EXT_SIZE_MOUSE to enable offsets up to EXT_MOUSE_LIMIT */
#define EXT_MOUSE_LIMIT (2047 - 32)
#define EXT_MOUSE_START (127 - 32)
static int
MouseLimit(TScreen *screen)
{
int mouse_limit;
switch (screen->extend_coords) {
default:
mouse_limit = MOUSE_LIMIT;
break;
case SET_EXT_MODE_MOUSE:
mouse_limit = EXT_MOUSE_LIMIT;
break;
case SET_SGR_EXT_MODE_MOUSE:
case SET_URXVT_EXT_MODE_MOUSE:
case SET_PIXEL_POSITION_MOUSE:
mouse_limit = -1;
break;
}
return mouse_limit;
}
static unsigned
EmitMousePosition(TScreen *screen, Char line[], unsigned count, int value)
{
int mouse_limit = MouseLimit(screen);
/*
* Add pointer position to key sequence
*
* In extended mode we encode large positions as two-byte UTF-8.
*
* NOTE: historically, it was possible to emit 256, which became
* zero by truncation to 8 bits. While this was arguably a bug,
* it's also somewhat useful as a past-end marker. We preserve
* this behavior for both normal and extended mouse modes.
*/
switch (screen->extend_coords) {
default:
if (value == mouse_limit) {
line[count++] = CharOf(0);
} else {
line[count++] = CharOf(' ' + value + 1);
}
break;
case SET_EXT_MODE_MOUSE:
if (value == mouse_limit) {
line[count++] = CharOf(0);
} else if (value < EXT_MOUSE_START) {
line[count++] = CharOf(' ' + value + 1);
} else {
value += ' ' + 1;
line[count++] = CharOf(0xC0 + (value >> 6));
line[count++] = CharOf(0x80 + (value & 0x3F));
}
break;
case SET_SGR_EXT_MODE_MOUSE:
case SET_URXVT_EXT_MODE_MOUSE:
case SET_PIXEL_POSITION_MOUSE:
count += (unsigned) sprintf((char *) line + count, "%d", value + 1);
break;
}
return count;
}
static unsigned
EmitMousePositionSeparator(TScreen *screen, Char line[], unsigned count)
{
switch (screen->extend_coords) {
case SET_SGR_EXT_MODE_MOUSE:
case SET_URXVT_EXT_MODE_MOUSE:
case SET_PIXEL_POSITION_MOUSE:
line[count++] = ';';
break;
}
return count;
}
enum {
scanMods,
scanKey,
scanColon,
scanFunc,
scanArgs
};
#if OPT_TRACE > 1
static const char *
visibleScan(int mode)
{
const char *result = "?";
#define DATA(name) case name: result = #name; break
switch (mode) {
DATA(scanMods);
DATA(scanKey);
DATA(scanColon);
DATA(scanFunc);
DATA(scanArgs);
}
#undef DATA
return result;
}
#endif
#define L_BRACK '<'
#define R_BRACK '>'
#define L_PAREN '('
#define R_PAREN ')'
static char *
scanTrans(char *source, int *this_is, int *next_is, unsigned *first, unsigned *last)
{
char *target = source;
*first = *last = 0;
if (IsEmpty(target)) {
target = NULL;
} else {
do {
char ch;
while (IsSpace(*target))
target++;
*first = (unsigned) (target - source);
switch (*this_is = *next_is) {
case scanMods:
while ((ch = *target)) {
if (IsSpace(ch)) {
break;
} else if (ch == L_BRACK) {
*next_is = scanKey;
break;
} else if (ch == ':') {
*next_is = scanColon;
break;
} else if (ch == '~' && target != source) {
break;
}
target++;
}
break;
case scanKey:
while ((ch = *target)) {
if (IsSpace(ch)) {
break;
} else if (ch == ':') {
*next_is = scanColon;
break;
}
target++;
if (ch == R_BRACK)
break;
}
break;
case scanColon:
*next_is = scanFunc;
target++;
break;
case scanFunc:
while ((ch = *target)) {
if (IsSpace(ch)) {
break;
} else if (ch == L_PAREN) {
*next_is = scanArgs;
break;
}
target++;
}
break;
case scanArgs:
while ((ch = *target)) {
if (ch == R_PAREN) {
target++;
*next_is = scanFunc;
break;
}
target++;
}
break;
}
*last = (unsigned) (target - source);
if (*target == '\n') {
*next_is = scanMods;
target++;
}
} while (*first == *last);
}
return target;
}
void
xtermButtonInit(XtermWidget xw)
{
Widget w = (Widget) xw;
XErrorHandler save = XSetErrorHandler(ignore_x11_error);
XtTranslations xlations;
Widget xcelerat;
String result;
XtVaGetValues(w,
XtNtranslations, &xlations,
XtNaccelerators, &xcelerat,
(XtPointer) 0);
result = _XtPrintXlations(w, xlations, xcelerat, True);
if (result) {
static const char *table[] =
{
"insert-selection",
"select-end",
"select-extend",
"select-start",
"start-extend",
};
char *data = x_strdup(result);
char *next;
int state = scanMods;
int state2 = scanMods;
unsigned first;
unsigned last;
int have_button = -1;
Bool want_button = False;
Bool have_shift = False;
unsigned allowed = 0;
unsigned disallow = 0;
TRACE(("xtermButtonInit length %ld\n", (long) strlen(result)));
xw->keyboard.print_translations = data;
while ((next = scanTrans(data, &state, &state2, &first, &last)) != NULL) {
unsigned len = (last - first);
TRACE2(("parse %s:%d..%d '%.*s'\n",
visibleScan(state), first, last,
len, data + first));
if (state == scanMods) {
if (len > 1 && data[first] == '~') {
len--;
first++;
}
if (len == 7 && !x_strncasecmp(data + first, "button", len - 1)) {
have_button = data[first + 6] - '0';
} else if (len == 5 && !x_strncasecmp(data + first, "shift", len)) {
have_shift = True;
}
} else if (state == scanKey) {
if (!x_strncasecmp(data + first, "<buttonpress>", len) ||
!x_strncasecmp(data + first, "<buttonrelease>", len)) {
want_button = True;
} else if (want_button) {
have_button = data[first] - '0';
want_button = False;
}
} else if (state == scanFunc && have_button > 0) {
Cardinal n;
unsigned bmask = 1U << (have_button - 1);
for (n = 0; n < XtNumber(table); ++n) {
if (!x_strncasecmp(table[n], data + first, len)) {
TRACE(("...button %d: %s%s\n",
have_button, table[n],
have_shift ? " (disallow)" : ""));
if (have_shift)
disallow |= bmask;
else
allowed |= bmask;
break;
}
}
}
if (state2 == scanMods && state >= scanColon) {
have_button = -1;
want_button = False;
have_shift = False;
}
state = state2;
data = next;
}
XFree((char *) result);
xw->keyboard.shift_buttons = allowed & ~disallow;
#if OPT_TRACE
if (xw->keyboard.shift_buttons) {
int button = 0;
unsigned mask = xw->keyboard.shift_buttons;
TRACE(("...Buttons used for selection that can be overridden:"));
while (mask != 0) {
++button;
if ((mask & 1) != 0)
TRACE((" %d", button));
mask >>= 1;
}
TRACE(("\n"));
} else {
TRACE(("...No buttons used with selection can be overridden\n"));
}
#endif
}
XSetErrorHandler(save);
}
/*
* Shift and control are regular X11 modifiers, but meta is not:
* + X10 (which had no xmodmap utility) had a meta mask, but X11 did not.
* + X11R1 introduced xmodmap, along with the current set of modifier masks.
* The meta key has been assumed to be mod1 since X11R1.
* The initial xterm logic in X11 was different, but gave the same result.
* + X11R2 modified xterm was to eliminate the X10 table which provided part of
* the meta logic.
* + X11R3 modified Xt, making Meta_L and Meta_R assignable via xmodmap, and
* equating Alt with Meta. Neither Alt/Meta are modifiers, but Alt is more
* likely to be on the keyboard. This release also added keymap tables for
* the server; Meta was used frequently in HP keymaps, which were the most
* extensive set of keymaps.
* + X11R4 mentions Meta in the ICCCM, stating that if Meta_L or Meta_R are
* found in the keysyms for a given modifier, that the client should use
* that modifier.
*
* This function follows the ICCCM, picking the modifier which contains the
* Meta_L/Meta_R keysyms (if available), falling back to the Alt_L/Alt_R
* (as per X11R3), and ultimately to mod1 (per X11R1).
*/
static unsigned
MetaMask(XtermWidget xw)
{
#if OPT_NUM_LOCK
unsigned meta = xw->work.meta_mods;
if (meta == 0)
meta = xw->work.alt_mods;
if (meta == 0)
meta = Mod1Mask;
#else
unsigned meta = Mod1Mask;
(void) xw;
#endif
return meta;
}
/*
* Returns a mask of the modifiers we may use for modifying the mouse protocol
* response strings.
*/
static unsigned
OurModifiers(XtermWidget xw)
{
return (ShiftMask
| ControlMask
| MetaMask(xw));
}
/*
* The actual check for the shift-mask, to see if it should tell xterm to
* override mouse-protocol in favor of select/paste actions depends upon
* whether the shiftEscape resource is set to true/always vs false/never.
*/
static Boolean
ShiftOverride(XtermWidget xw, unsigned state, int button)
{
unsigned check = (state & OurModifiers(xw));
Boolean result = False;
if (check & ShiftMask) {
if (xw->keyboard.shift_escape == ssFalse ||
xw->keyboard.shift_escape == ssNever) {
result = True;
} else if (xw->keyboard.shift_escape == ssTrue) {
/*
* Check if the button is one that we found does not directly use
* the shift-modifier in its bindings to select/copy actions.
*/
if (button > 0 && button <= MaxMouseBtn) {
if (xw->keyboard.shift_buttons & (1U << (button - 1))) {
result = True;
}
} else {
result = True; /* unlikely, and we don't care */
}
}
}
TRACE2(("ShiftOverride ( %#x -> %#x ) %d\n", state, check, result));
return result;
}
/*
* Normally xterm treats the shift-modifier specially when the mouse protocol
* is active. The translations resource binds otherwise unmodified button
* for these mouse-related events:
*
* ~Meta <Btn1Down>:select-start() \n\
* ~Meta <Btn1Motion>:select-extend() \n\
* ~Ctrl ~Meta <Btn2Up>:insert-selection(SELECT, CUT_BUFFER0) \n\
* ~Ctrl ~Meta <Btn3Down>:start-extend() \n\
* ~Meta <Btn3Motion>:select-extend() \n\
* <BtnUp>:select-end(SELECT, CUT_BUFFER0) \n\
*
* There is no API in the X libraries which would tell us if a given mouse
* button is bound to one of these actions. These functions make the choice
* configurable.
*/
static Bool
InterpretButton(XtermWidget xw, XButtonEvent *event)
{
Bool result = False;
if (ShiftOverride(xw, event->state, (int) event->button)) {
TRACE(("...shift-button #%d overrides mouse-protocol\n", event->button));
result = True;
}
return result;
}
#define Button1Index 8 /* X.h should have done this */
static int
MotionButton(unsigned state)
{
unsigned bmask = state >> Button1Index;
int result = 1;
if (bmask != 0) {
while (!(bmask & 1)) {
++result;
bmask >>= 1;
}
}
return result;
}
static Bool
InterpretEvent(XtermWidget xw, XEvent *event)
{
Bool result = False; /* if not a button, is motion */
if (IsBtnEvent(event)) {
result = InterpretButton(xw, (XButtonEvent *) event);
} else if (event->type == MotionNotify) {
unsigned state = event->xmotion.state;
int button = MotionButton(state);
if (ShiftOverride(xw, state, button)) {
TRACE(("...shift-motion #%d (%d,%d) overrides mouse-protocol\n",
button,
event->xmotion.y,
event->xmotion.x));
result = True;
}
}
return result;
}
#define OverrideEvent(event) InterpretEvent(xw, event)
#define OverrideButton(event) InterpretButton(xw, event)
/*
* Returns true if we handled the event here, and nothing more is needed.
*/
Bool
SendMousePosition(XtermWidget xw, XEvent *event)
{
XButtonEvent *my_event = (XButtonEvent *) event;
Bool result = False;
switch (okSendMousePos(xw)) {
case MOUSE_OFF:
/* If send_mouse_pos mode isn't on, we shouldn't be here */
break;
case BTN_EVENT_MOUSE:
case ANY_EVENT_MOUSE:
if (!OverrideEvent(event)) {
/* xterm extension for motion reporting. June 1998 */
/* EditorButton() will distinguish between the modes */
switch (event->type) {
case MotionNotify:
my_event->button = 0;
/* FALLTHRU */
case ButtonPress:
/* FALLTHRU */
case ButtonRelease:
EditorButton(xw, my_event);
result = True;
break;
}
}
break;
case X10_MOUSE: /* X10 compatibility sequences */
if (IsBtnEvent(event)) {
if (!OverrideButton(my_event)) {
if (my_event->type == ButtonPress)
EditorButton(xw, my_event);
result = True;
}
}
break;
case VT200_HIGHLIGHT_MOUSE: /* DEC vt200 hilite tracking */
if (IsBtnEvent(event)) {
if (!OverrideButton(my_event)) {
if (my_event->type == ButtonPress &&
my_event->button == Button1) {
TrackDown(xw, my_event);
} else {
EditorButton(xw, my_event);
}
result = True;
}
}
break;
case VT200_MOUSE: /* DEC vt200 compatible */
if (IsBtnEvent(event)) {
if (!OverrideButton(my_event)) {
EditorButton(xw, my_event);
result = True;
}
}
break;
case DEC_LOCATOR:
#if OPT_DEC_LOCATOR
if (IsBtnEvent(event) || event->type == MotionNotify) {
result = SendLocatorPosition(xw, my_event);
}
#endif /* OPT_DEC_LOCATOR */
break;
}
return result;
}
#if OPT_DEC_LOCATOR
#define LocatorCoords( row, col, x, y, oor ) \
if( screen->locator_pixels ) { \
(oor)=False; (row) = (y)+1; (col) = (x)+1; \
/* Limit to screen dimensions */ \
if ((row) < 1) (row) = 1,(oor)=True; \
else if ((row) > screen->border*2+Height(screen)) \
(row) = screen->border*2+Height(screen),(oor)=True; \
if ((col) < 1) (col) = 1,(oor)=True; \
else if ((col) > OriginX(screen)*2+Width(screen)) \
(col) = OriginX(screen)*2+Width(screen),(oor)=True; \
} else { \
(oor)=False; \
/* Compute character position of mouse pointer */ \
(row) = ((y) - screen->border) / FontHeight(screen); \
(col) = ((x) - OriginX(screen)) / FontWidth(screen); \
/* Limit to screen dimensions */ \
if ((row) < 0) (row) = 0,(oor)=True; \
else if ((row) > screen->max_row) \
(row) = screen->max_row,(oor)=True; \
if ((col) < 0) (col) = 0,(oor)=True; \
else if ((col) > screen->max_col) \
(col) = screen->max_col,(oor)=True; \
(row)++; (col)++; \
}
static Bool
SendLocatorPosition(XtermWidget xw, XButtonEvent *event)
{
ANSI reply;
TScreen *screen = TScreenOf(xw);
int row, col;
Bool oor;
int button;
unsigned state;
/* Make sure the event is an appropriate type */
if (IsBtnEvent(event)) {
if (OverrideButton(event))
return (False);
} else {
if (!screen->loc_filter)
return (False);
}
if ((event->type == ButtonPress &&
!(screen->locator_events & LOC_BTNS_DN)) ||
(event->type == ButtonRelease &&
!(screen->locator_events & LOC_BTNS_UP)))
return (True);
if (event->type == MotionNotify) {
CheckLocatorPosition(xw, event);
return (True);
}
/* get button # */
button = (int) event->button - 1;
LocatorCoords(row, col, event->x, event->y, oor);
/*
* DECterm mouse:
*
* ESCAPE '[' event ; mask ; row ; column '&' 'w'
*/
memset(&reply, 0, sizeof(reply));
reply.a_type = ANSI_CSI;
if (oor) {
reply.a_nparam = 1;
reply.a_param[0] = 0; /* Event - 0 = locator unavailable */
reply.a_inters = '&';
reply.a_final = 'w';
unparseseq(xw, &reply);
if (screen->locator_reset) {
MotionOff(screen, xw);
screen->send_mouse_pos = MOUSE_OFF;
}
return (True);
}
/*
* event:
* 1 no buttons
* 2 left button down
* 3 left button up
* 4 middle button down
* 5 middle button up
* 6 right button down
* 7 right button up
* 8 M4 down
* 9 M4 up
*/
reply.a_nparam = 4;
switch (event->type) {
case ButtonPress:
reply.a_param[0] = (ParmType) (2 + (button << 1));
break;
case ButtonRelease:
reply.a_param[0] = (ParmType) (3 + (button << 1));
break;
default:
return (True);
}
/*
* mask:
* bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
* M4 down left down middle down right down
*
* Notice that Button1 (left) and Button3 (right) are swapped in the mask.
* Also, mask should be the state after the button press/release,
* X provides the state not including the button press/release.
*/
state = (event->state
& (Button1Mask | Button2Mask | Button3Mask | Button4Mask)) >> 8;
/* update mask to "after" state */
state ^= ((unsigned) (1 << button));
/* swap Button1 & Button3 */
state = ((state & (unsigned) ~(4 | 1))
| ((state & 1) ? 4 : 0)
| ((state & 4) ? 1 : 0));
reply.a_param[1] = (ParmType) state;
reply.a_param[2] = (ParmType) row;
reply.a_param[3] = (ParmType) col;
reply.a_inters = '&';
reply.a_final = 'w';
unparseseq(xw, &reply);
if (screen->locator_reset) {
MotionOff(screen, xw);
screen->send_mouse_pos = MOUSE_OFF;
}
/*
* DECterm turns the Locator off if a button is pressed while a filter
* rectangle is active. This might be a bug, but I don't know, so I'll
* emulate it anyway.
*/
if (screen->loc_filter) {
screen->send_mouse_pos = MOUSE_OFF;
screen->loc_filter = False;
screen->locator_events = 0;
MotionOff(screen, xw);
}
return (True);
}
/*
* mask:
* bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0
* M4 down left down middle down right down
*
* Button1 (left) and Button3 (right) are swapped in the mask relative to X.
*/
#define ButtonState(state, mask) \
{ int stemp = (int) (((mask) & (Button1Mask | Button2Mask | Button3Mask | Button4Mask)) >> 8); \
/* swap Button1 & Button3 */ \
(state) = (stemp & ~(4|1)) | ((stemp & 1) ? 4 : 0) | ((stemp & 4) ? 1 : 0); \
}
void
GetLocatorPosition(XtermWidget xw)
{
ANSI reply;
TScreen *screen = TScreenOf(xw);
Window root, child;
int rx, ry, x, y;
unsigned int mask = 0;
int row = 0, col = 0;
Bool oor = False;
Bool ret = False;
int state;
/*
* DECterm turns the Locator off if the position is requested while a
* filter rectangle is active. This might be a bug, but I don't know, so
* I'll emulate it anyways.
*/
if (screen->loc_filter) {
screen->send_mouse_pos = MOUSE_OFF;
screen->loc_filter = False;
screen->locator_events = 0;
MotionOff(screen, xw);
}
memset(&reply, 0, sizeof(reply));
reply.a_type = ANSI_CSI;
if (okSendMousePos(xw) == DEC_LOCATOR) {
ret = XQueryPointer(screen->display, VWindow(screen), &root,
&child, &rx, &ry, &x, &y, &mask);
if (ret) {
LocatorCoords(row, col, x, y, oor);
}
}
if (ret == False || oor) {
reply.a_nparam = 1;
reply.a_param[0] = 0; /* Event - 0 = locator unavailable */
reply.a_inters = '&';
reply.a_final = 'w';
unparseseq(xw, &reply);
if (screen->locator_reset) {
MotionOff(screen, xw);
screen->send_mouse_pos = MOUSE_OFF;
}
return;
}
ButtonState(state, mask);
reply.a_nparam = 4;
reply.a_param[0] = 1; /* Event - 1 = response to locator request */
reply.a_param[1] = (ParmType) state;
reply.a_param[2] = (ParmType) row;
reply.a_param[3] = (ParmType) col;
reply.a_inters = '&';
reply.a_final = 'w';
unparseseq(xw, &reply);
if (screen->locator_reset) {
MotionOff(screen, xw);
screen->send_mouse_pos = MOUSE_OFF;
}
}
void
InitLocatorFilter(XtermWidget xw)
{
ANSI reply;
TScreen *screen = TScreenOf(xw);
Window root, child;
int rx, ry, x, y;
unsigned int mask;
int row = 0, col = 0;
Bool oor = 0;
Bool ret;
ret = XQueryPointer(screen->display, VWindow(screen),
&root, &child, &rx, &ry, &x, &y, &mask);
if (ret) {
LocatorCoords(row, col, x, y, oor);
}
if (ret == False || oor) {
/* Locator is unavailable */
if (screen->loc_filter_top != LOC_FILTER_POS ||
screen->loc_filter_left != LOC_FILTER_POS ||
screen->loc_filter_bottom != LOC_FILTER_POS ||
screen->loc_filter_right != LOC_FILTER_POS) {
/*
* If any explicit coordinates were received,
* report immediately with no coordinates.
*/