This repository has been archived by the owner on Jul 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
button.c
4990 lines (4467 loc) · 125 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.451 2013/02/06 09:56:15 tom Exp $ */
/*
* Copyright 1999-2012,2013 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 <xcharmouse.h>
#include <charclass.h>
#include <xstrings.h>
#if OPT_SELECT_REGEX
#ifdef HAVE_PCREPOSIX_H
#include <pcreposix.h>
#else /* POSIX regex.h */
#include <sys/types.h>
#include <regex.h>
#endif
#endif
#if OPT_WIDE_CHARS
#include <ctype.h>
#include <wcwidth.h>
#else
#define CharacterClass(value) \
charClass[value & ((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))
/*
* We reserve shift modifier for cut/paste operations. In principle we
* can pass through control and meta modifiers, but in practice, the
* popup menu uses control, and the window manager is likely to use meta,
* so those events are not delivered to SendMousePosition.
*/
#define OurModifiers (ShiftMask | ControlMask | Mod1Mask)
#define AllModifiers (ShiftMask | LockMask | ControlMask | Mod1Mask | \
Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask)
#define BtnModifiers(event) (event->state & OurModifiers)
#define KeyModifiers(event) (event->xbutton.state & OurModifiers)
#define IsBtnEvent(event) ((event)->type == ButtonPress || (event)->type == ButtonRelease)
#define IsKeyEvent(event) ((event)->type == KeyPress || (event)->type == KeyRelease)
#define KeyState(x) (((int) ((x) & (ShiftMask|ControlMask))) \
+ (((x) & Mod1Mask) ? 2 : 0))
/* adds together the bits:
shift key -> 1
meta key -> 2
control key -> 4 */
#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, CELL * startc, CELL * endc, Bool extend);
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, CELL * first, CELL * last);
static void SaltTextAway(XtermWidget xw, CELL * cellc, 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 _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:
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:
/* FALLTHRU */
case SET_URXVT_EXT_MODE_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:
line[count++] = ';';
break;
}
return count;
}
Bool
SendMousePosition(XtermWidget xw, XEvent * event)
{
TScreen *screen = TScreenOf(xw);
XButtonEvent *my_event = (XButtonEvent *) event;
Bool result = False;
switch (screen->send_mouse_pos) {
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 (KeyModifiers(event) == 0 || KeyModifiers(event) == ControlMask) {
/* 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;
default:
/* Make sure the event is an appropriate type */
if (IsBtnEvent(event)) {
switch (screen->send_mouse_pos) {
case X10_MOUSE: /* X10 compatibility sequences */
if (BtnModifiers(my_event) == 0) {
if (my_event->type == ButtonPress)
EditorButton(xw, my_event);
result = True;
}
break;
case VT200_HIGHLIGHT_MOUSE: /* DEC vt200 hilite tracking */
if (my_event->type == ButtonPress &&
BtnModifiers(my_event) == 0 &&
my_event->button == Button1) {
TrackDown(xw, my_event);
result = True;
} else if (BtnModifiers(my_event) == 0
|| BtnModifiers(my_event) == ControlMask) {
EditorButton(xw, my_event);
result = True;
}
break;
case VT200_MOUSE: /* DEC vt200 compatible */
if (BtnModifiers(my_event) == 0
|| BtnModifiers(my_event) == ControlMask) {
EditorButton(xw, my_event);
result = True;
}
break;
#if OPT_DEC_LOCATOR
case DEC_LOCATOR:
result = SendLocatorPosition(xw, my_event);
break;
#endif /* OPT_DEC_LOCATOR */
}
}
}
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) &&
!screen->loc_filter) ||
(BtnModifiers(event) != 0 && BtnModifiers(event) != ControlMask))
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:
* bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0
* 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 anyways.
*/
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) \
{ (state) = (int) (((mask) & (Button1Mask | Button2Mask | Button3Mask | Button4Mask)) >> 8); \
/* swap Button1 & Button3 */ \
(state) = ((state) & ~(4|1)) | (((state)&1)?4:0) | (((state)&4)?1:0); \
}
void
GetLocatorPosition(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 = 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 (screen->send_mouse_pos == 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;
int state;
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.
*/
memset(&reply, 0, sizeof(reply));
reply.a_type = ANSI_CSI;
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;
}
} else {
/*
* No explicit coordinates were received, and the pointer is
* unavailable. Report when the pointer re-enters the window.
*/
screen->loc_filter = True;
MotionOn(screen, xw);
}
return;
}
/*
* Adjust rectangle coordinates:
* 1. Replace "LOC_FILTER_POS" with current coordinates
* 2. Limit coordinates to screen size
* 3. make sure top and left are less than bottom and right, resp.
*/
if (screen->locator_pixels) {
rx = OriginX(screen) * 2 + Width(screen);
ry = screen->border * 2 + Height(screen);
} else {
rx = screen->max_col;
ry = screen->max_row;
}
#define Adjust( coord, def, max ) \
if( (coord) == LOC_FILTER_POS ) (coord) = (def); \
else if ((coord) < 1) (coord) = 1; \
else if ((coord) > (max)) (coord) = (max)
Adjust(screen->loc_filter_top, row, ry);
Adjust(screen->loc_filter_left, col, rx);
Adjust(screen->loc_filter_bottom, row, ry);
Adjust(screen->loc_filter_right, col, rx);
if (screen->loc_filter_top > screen->loc_filter_bottom) {
ry = screen->loc_filter_top;
screen->loc_filter_top = screen->loc_filter_bottom;
screen->loc_filter_bottom = ry;
}
if (screen->loc_filter_left > screen->loc_filter_right) {
rx = screen->loc_filter_left;
screen->loc_filter_left = screen->loc_filter_right;
screen->loc_filter_right = rx;
}
if ((col < screen->loc_filter_left) ||
(col > screen->loc_filter_right) ||
(row < screen->loc_filter_top) ||
(row > screen->loc_filter_bottom)) {
/* Pointer is already outside the rectangle - report immediately */
ButtonState(state, mask);
memset(&reply, 0, sizeof(reply));
reply.a_type = ANSI_CSI;
reply.a_nparam = 4;
reply.a_param[0] = 10; /* Event - 10 = locator outside filter */
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;
}
return;
}
/*
* Rectangle is set up. Allow pointer tracking
* to detect if the mouse leaves the rectangle.
*/
screen->loc_filter = True;
MotionOn(screen, xw);
}
static void
CheckLocatorPosition(XtermWidget xw, XButtonEvent * event)
{
ANSI reply;
TScreen *screen = TScreenOf(xw);
int row, col;
Bool oor;
int state;
LocatorCoords(row, col, event->x, event->y, oor);
/*
* Send report if the pointer left the filter rectangle, if
* the pointer left the window, or if the filter rectangle
* had no coordinates and the pointer re-entered the window.
*/
if (oor || (screen->loc_filter_top == LOC_FILTER_POS) ||
(col < screen->loc_filter_left) ||
(col > screen->loc_filter_right) ||
(row < screen->loc_filter_top) ||
(row > screen->loc_filter_bottom)) {
/* Filter triggered - disable it */
screen->loc_filter = False;
MotionOff(screen, xw);
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 */
} else {
ButtonState(state, event->state);
reply.a_nparam = 4;
reply.a_param[0] = 10; /* Event - 10 = locator outside filter */
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;
}
}
}
#endif /* OPT_DEC_LOCATOR */
#if OPT_READLINE
static int
isClick1_clean(TScreen * screen, XButtonEvent * event)
{
int delta;
if (!IsBtnEvent(event)
/* Disable on Shift-Click-1, including the application-mouse modes */
|| (BtnModifiers(event) & ShiftMask)
|| (screen->send_mouse_pos != MOUSE_OFF) /* Kinda duplicate... */
||ExtendingSelection) /* Was moved */
return 0;
if (event->type != ButtonRelease)
return 0;
if (lastButtonDownTime == (Time) 0) {
/* first time or once in a blue moon */
delta = screen->multiClickTime + 1;
} else if (event->time > lastButtonDownTime) {
/* most of the time */
delta = (int) (event->time - lastButtonDownTime);
} else {
/* time has rolled over since lastButtonUpTime */
delta = (int) ((((Time) ~ 0) - lastButtonDownTime) + event->time);
}
return delta <= screen->multiClickTime;
}
static int
isDoubleClick3(TScreen * screen, XButtonEvent * event)
{
int delta;
if (event->type != ButtonRelease
|| (BtnModifiers(event) & ShiftMask)
|| event->button != Button3) {
lastButton3UpTime = 0; /* Disable the cached info */
return 0;
}
/* Process Btn3Release. */
if (lastButton3DoubleDownTime == (Time) 0) {
/* No previous click or once in a blue moon */
delta = screen->multiClickTime + 1;
} else if (event->time > lastButton3DoubleDownTime) {
/* most of the time */
delta = (int) (event->time - lastButton3DoubleDownTime);
} else {
/* time has rolled over since lastButton3DoubleDownTime */
delta = (int) ((((Time) ~ 0) - lastButton3DoubleDownTime) + event->time);
}
if (delta <= screen->multiClickTime) {
/* Double click */
CELL cell;
/* Cannot check ExtendingSelection, since mouse-3 always sets it */
PointToCELL(screen, event->y, event->x, &cell);
if (isSameCELL(&cell, &lastButton3)) {
lastButton3DoubleDownTime = 0; /* Disable the third click */
return 1;
}
}
/* Not a double click, memorize for future check. */
lastButton3UpTime = event->time;
PointToCELL(screen, event->y, event->x, &lastButton3);
return 0;
}
static int
CheckSecondPress3(TScreen * screen, XEvent * event)
{
int delta;
if (event->type != ButtonPress
|| (KeyModifiers(event) & ShiftMask)
|| event->xbutton.button != Button3) {
lastButton3DoubleDownTime = 0; /* Disable the cached info */
return 0;
}
/* Process Btn3Press. */
if (lastButton3UpTime == (Time) 0) {
/* No previous click or once in a blue moon */
delta = screen->multiClickTime + 1;
} else if (event->xbutton.time > lastButton3UpTime) {
/* most of the time */
delta = (int) (event->xbutton.time - lastButton3UpTime);
} else {
/* time has rolled over since lastButton3UpTime */
delta = (int) ((((Time) ~ 0) - lastButton3UpTime) + event->xbutton.time);
}
if (delta <= screen->multiClickTime) {
CELL cell;
PointToCELL(screen, event->xbutton.y, event->xbutton.x, &cell);
if (isSameCELL(&cell, &lastButton3)) {
/* A candidate for a double-click */
lastButton3DoubleDownTime = event->xbutton.time;
PointToCELL(screen, event->xbutton.y, event->xbutton.x, &lastButton3);
return 1;
}
lastButton3UpTime = 0; /* Disable the info about the previous click */
}
/* Either too long, or moved, disable. */
lastButton3DoubleDownTime = 0;
return 0;
}
static int
rowOnCurrentLine(TScreen * screen,
int line,
int *deltap) /* must be XButtonEvent */
{
int result = 1;
int l1, l2;
*deltap = 0;
if (line != screen->cur_row) {
if (line < screen->cur_row)
l1 = line, l2 = screen->cur_row;
else
l2 = line, l1 = screen->cur_row;
l1--;
while (++l1 < l2) {
LineData *ld = GET_LINEDATA(screen, l1);
if (!LineTstWrapped(ld)) {
result = 0;
break;
}
}
if (result) {
/* Everything is on one "wrapped line" now */
*deltap = line - screen->cur_row;
}
}
return result;
}
static int
eventRow(TScreen * screen, XEvent * event) /* must be XButtonEvent */
{
return (event->xbutton.y - screen->border) / FontHeight(screen);
}
static int
eventColBetween(TScreen * screen, XEvent * event) /* must be XButtonEvent */
{
/* Correct by half a width - we are acting on a boundary, not on a cell. */
return ((event->xbutton.x - OriginX(screen) + (FontWidth(screen) - 1) / 2)
/ FontWidth(screen));
}
static int
ReadLineMovePoint(TScreen * screen, int col, int ldelta)
{
Char line[6];
unsigned count = 0;
col += ldelta * MaxCols(screen) - screen->cur_col;
if (col == 0)
return 0;
if (screen->control_eight_bits) {
line[count++] = ANSI_CSI;
} else {
line[count++] = ANSI_ESC;
line[count++] = '['; /* XXX maybe sometimes O is better? */
}
line[count] = CharOf(col > 0 ? 'C' : 'D');
if (col < 0)
col = -col;
while (col--)
v_write(screen->respond, line, 3);
return 1;
}
static int
ReadLineDelete(TScreen * screen, CELL * cell1, CELL * cell2)
{
int del;
del = (cell2->col - cell1->col) + ((cell2->row - cell1->row) * MaxCols(screen));
if (del <= 0) /* Just in case... */
return 0;
while (del--)
v_write(screen->respond, (const Char *) "\177", 1);
return 1;
}
static void
readlineExtend(TScreen * screen, XEvent * event)
{
int ldelta1, ldelta2;
if (IsBtnEvent(event)) {
XButtonEvent *my_event = (XButtonEvent *) event;
if (isClick1_clean(screen, my_event)
&& SCREEN_FLAG(screen, click1_moves)
&& rowOnCurrentLine(screen, eventRow(screen, event), &ldelta1)) {
ReadLineMovePoint(screen, eventColBetween(screen, event), ldelta1);
}
if (isDoubleClick3(screen, my_event)
&& SCREEN_FLAG(screen, dclick3_deletes)
&& rowOnCurrentLine(screen, screen->startSel.row, &ldelta1)
&& rowOnCurrentLine(screen, screen->endSel.row, &ldelta2)) {
ReadLineMovePoint(screen, screen->endSel.col, ldelta2);
ReadLineDelete(screen, &screen->startSel, &(screen->endSel));
}
}
}
#endif /* OPT_READLINE */
/* ^XM-G<line+' '><col+' '> */
void
DiredButton(Widget w,
XEvent * event, /* must be XButtonEvent */
String * params GCC_UNUSED, /* selections */
Cardinal *num_params GCC_UNUSED)
{
XtermWidget xw;
if ((xw = getXtermWidget(w)) != 0) {
TScreen *screen = TScreenOf(xw);
Char Line[6];
unsigned line, col;
if (IsBtnEvent(event)
&& (event->xbutton.y >= screen->border)
&& (event->xbutton.x >= OriginX(screen))) {
line = (unsigned) ((event->xbutton.y - screen->border)
/ FontHeight(screen));
col = (unsigned) ((event->xbutton.x - OriginX(screen))
/ FontWidth(screen));
Line[0] = CONTROL('X');
Line[1] = ANSI_ESC;
Line[2] = 'G';
Line[3] = CharOf(' ' + col);
Line[4] = CharOf(' ' + line);
v_write(screen->respond, Line, 5);
}
}
}
#if OPT_READLINE
void
ReadLineButton(Widget w,
XEvent * event, /* must be XButtonEvent */
String * params GCC_UNUSED, /* selections */
Cardinal *num_params GCC_UNUSED)
{
XtermWidget xw;
if ((xw = getXtermWidget(w)) != 0) {
TScreen *screen = TScreenOf(xw);
Char Line[6];
int line, col, ldelta = 0;
if (!IsBtnEvent(event)
|| (screen->send_mouse_pos != MOUSE_OFF) || ExtendingSelection)
goto finish;
if (event->type == ButtonRelease) {
int delta;
if (lastButtonDownTime == (Time) 0) {
/* first time and once in a blue moon */
delta = screen->multiClickTime + 1;
} else if (event->xbutton.time > lastButtonDownTime) {
/* most of the time */
delta = (int) (event->xbutton.time - lastButtonDownTime);
} else {
/* time has rolled over since lastButtonUpTime */
delta = (int) ((((Time) ~ 0) - lastButtonDownTime) + event->xbutton.time);
}