-
Notifications
You must be signed in to change notification settings - Fork 0
/
ckEvent.c
1293 lines (1169 loc) · 33 KB
/
ckEvent.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
/*
* ckEvent.c --
*
* This file provides basic event-managing facilities,
* whereby procedure callbacks may be attached to
* certain events.
*
* Copyright (c) 1990-1994 The Regents of the University of California.
* Copyright (c) 1994-1995 Sun Microsystems, Inc.
* Copyright (c) 1995-2002 Christian Werner
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "ckPort.h"
#include "ck.h"
#ifdef HAVE_GPM
#include "gpm.h"
#endif
#include <pty.h>
typedef struct {
Tcl_Event header; /* Standard event header. */
CkEvent event; /* Ck event data. */
CkMainInfo *mainPtr; /* Pointer to Ck main info. */
} CkQEvt;
static int Ck_HandleQEvent _ANSI_ARGS_((Tcl_Event *evPtr, int flags));
/*
* There's a potential problem if a handler is deleted while it's
* current (i.e. its procedure is executing), since Ck_HandleEvent
* will need to read the handler's "nextPtr" field when the procedure
* returns. To handle this problem, structures of the type below
* indicate the next handler to be processed for any (recursively
* nested) dispatches in progress. The nextHandler fields get
* updated if the handlers pointed to are deleted. Ck_HandleEvent
* also needs to know if the entire window gets deleted; the winPtr
* field is set to zero if that particular window gets deleted.
*/
typedef struct InProgress {
CkEvent *eventPtr; /* Event currently being handled. */
CkWindow *winPtr; /* Window for event. Gets set to NULL if
* window is deleted while event is being
* handled. */
CkEventHandler *nextHandler; /* Next handler in search. */
struct InProgress *nextPtr; /* Next higher nested search. */
} InProgress;
static InProgress *pendingPtr = NULL;
/* Topmost search in progress, or
* NULL if none. */
/*
* For each call to Ck_CreateGenericHandler, an instance of the following
* structure will be created. All of the active handlers are linked into a
* list.
*/
typedef struct GenericHandler {
Ck_GenericProc *proc; /* Procedure to dispatch on all events. */
ClientData clientData; /* Client data to pass to procedure. */
int deleteFlag; /* Flag to set when this handler is deleted. */
struct GenericHandler *nextPtr;
/* Next handler in list of all generic
* handlers, or NULL for end of list. */
} GenericHandler;
static GenericHandler *genericList = NULL;
/* First handler in the list, or NULL. */
static GenericHandler *lastGenericPtr = NULL;
/* Last handler in list. */
/*
* There's a potential problem if Ck_HandleEvent is entered recursively.
* A handler cannot be deleted physically until we have returned from
* calling it. Otherwise, we're looking at unallocated memory in advancing to
* its `next' entry. We deal with the problem by using the `delete flag' and
* deleting handlers only when it's known that there's no handler active.
*
* The following variable has a non-zero value when a handler is active.
*/
static int genericHandlersActive = 0;
/*
* For barcode readers an instance of the following structure is linked
* to mainInfo. The supported DATA LOGIC barcode readers are connected
* between PC keyboard and PC keyboard controller and generate a data
* packet surrounded by start and end characters. If the start character
* is received a timer is started and the following keystrokes are
* collected into the buffer until the end character is received or the
* timer expires.
*/
#define DEFAULT_BARCODE_TIMEOUT 1000
typedef struct barcodeData {
Tk_TimerToken timer;/* Barcode packet timer. */
int pkttime; /* Timeout value. */
int startChar; /* Start of barcode packet character. */
int endChar; /* End of barcode packet character. */
int delivered; /* BarCode event has been delivered. */
int index; /* Current index into buffer. */
#if CK_USE_UTF
char buffer[256]; /* Here the barcode packet is assembled. */
#else
char buffer[128]; /* Here the barcode packet is assembled. */
#endif
} BarcodeData;
/*
* Timeout procedure for reading barcode packet:
*/
static void BarcodeTimeout _ANSI_ARGS_((ClientData clientData));
/*
*--------------------------------------------------------------
*
* Ck_CreateEventHandler --
*
* Arrange for a given procedure to be invoked whenever
* events from a given class occur in a given window.
*
* Results:
* None.
*
* Side effects:
* From now on, whenever an event of the type given by
* mask occurs for token and is processed by Ck_HandleEvent,
* proc will be called. See the manual entry for details
* of the calling sequence and return value for proc.
*
*--------------------------------------------------------------
*/
void
Ck_CreateEventHandler(winPtr, mask, proc, clientData)
CkWindow *winPtr; /* Window in which to create handler. */
long mask; /* Events for which proc should be called. */
Ck_EventProc *proc; /* Procedure to call for each
* selected event */
ClientData clientData; /* Arbitrary data to pass to proc. */
{
CkEventHandler *handlerPtr;
int found;
/*
* Skim through the list of existing handlers to see if there's
* already a handler declared with the same callback and clientData
* (if so, just change the mask). If no existing handler matches,
* then create a new handler.
*/
found = 0;
if (winPtr->handlerList == NULL) {
handlerPtr = (CkEventHandler *) ckalloc(sizeof (CkEventHandler));
winPtr->handlerList = handlerPtr;
goto initHandler;
} else {
for (handlerPtr = winPtr->handlerList; ;
handlerPtr = handlerPtr->nextPtr) {
if ((handlerPtr->proc == proc)
&& (handlerPtr->clientData == clientData)) {
handlerPtr->mask = mask;
found = 1;
}
if (handlerPtr->nextPtr == NULL) {
break;
}
}
}
/*
* Create a new handler if no matching old handler was found.
*/
if (!found) {
handlerPtr->nextPtr = (CkEventHandler *) ckalloc(
sizeof (CkEventHandler));
handlerPtr = handlerPtr->nextPtr;
initHandler:
handlerPtr->mask = mask;
handlerPtr->proc = proc;
handlerPtr->clientData = clientData;
handlerPtr->nextPtr = NULL;
}
}
/*
*--------------------------------------------------------------
*
* Ck_DeleteEventHandler --
*
* Delete a previously-created handler.
*
* Results:
* None.
*
* Side effects:
* If there existed a handler as described by the
* parameters, the handler is deleted so that proc
* will not be invoked again.
*
*--------------------------------------------------------------
*/
void
Ck_DeleteEventHandler(winPtr, mask, proc, clientData)
CkWindow *winPtr; /* Same as corresponding arguments passed */
long mask; /* previously to Ck_CreateEventHandler. */
Ck_EventProc *proc;
ClientData clientData;
{
CkEventHandler *handlerPtr;
InProgress *ipPtr;
CkEventHandler *prevPtr;
/*
* Find the event handler to be deleted, or return
* immediately if it doesn't exist.
*/
for (handlerPtr = winPtr->handlerList, prevPtr = NULL; ;
prevPtr = handlerPtr, handlerPtr = handlerPtr->nextPtr) {
if (handlerPtr == NULL) {
return;
}
if ((handlerPtr->mask == mask) && (handlerPtr->proc == proc)
&& (handlerPtr->clientData == clientData)) {
break;
}
}
/*
* If Ck_HandleEvent is about to process this handler, tell it to
* process the next one instead.
*/
for (ipPtr = pendingPtr; ipPtr != NULL; ipPtr = ipPtr->nextPtr) {
if (ipPtr->nextHandler == handlerPtr) {
ipPtr->nextHandler = handlerPtr->nextPtr;
}
}
/*
* Free resources associated with the handler.
*/
if (prevPtr == NULL) {
winPtr->handlerList = handlerPtr->nextPtr;
} else {
prevPtr->nextPtr = handlerPtr->nextPtr;
}
ckfree((char *) handlerPtr);
}
/*--------------------------------------------------------------
*
* Ck_CreateGenericHandler --
*
* Register a procedure to be called on each event, regardless
* of window. Generic handlers are useful for capturing
* events that aren't associated with windows, or events for windows
* not managed by Ck.
*
* Results:
* None.
*
* Side Effects:
* From now on, whenever an event is given to Ck_HandleEvent,
* invoke proc, giving it clientData and the event as arguments.
*
*--------------------------------------------------------------
*/
void
Ck_CreateGenericHandler(proc, clientData)
Ck_GenericProc *proc; /* Procedure to call on every event. */
ClientData clientData; /* One-word value to pass to proc. */
{
GenericHandler *handlerPtr;
handlerPtr = (GenericHandler *) ckalloc (sizeof (GenericHandler));
handlerPtr->proc = proc;
handlerPtr->clientData = clientData;
handlerPtr->deleteFlag = 0;
handlerPtr->nextPtr = NULL;
if (genericList == NULL) {
genericList = handlerPtr;
} else {
lastGenericPtr->nextPtr = handlerPtr;
}
lastGenericPtr = handlerPtr;
}
/*
*--------------------------------------------------------------
*
* Ck_DeleteGenericHandler --
*
* Delete a previously-created generic handler.
*
* Results:
* None.
*
* Side Effects:
* If there existed a handler as described by the parameters,
* that handler is logically deleted so that proc will not be
* invoked again. The physical deletion happens in the event
* loop in Ck_HandleEvent.
*
*--------------------------------------------------------------
*/
void
Ck_DeleteGenericHandler(proc, clientData)
Ck_GenericProc *proc;
ClientData clientData;
{
GenericHandler * handler;
for (handler = genericList; handler; handler = handler->nextPtr) {
if ((handler->proc == proc) && (handler->clientData == clientData)) {
handler->deleteFlag = 1;
}
}
}
/*
*--------------------------------------------------------------
*
* Ck_HandleEvent --
*
* Given an event, invoke all the handlers that have
* been registered for the event.
*
* Results:
* None.
*
* Side effects:
* Depends on the handlers.
*
*--------------------------------------------------------------
*/
void
Ck_HandleEvent(mainPtr, eventPtr)
CkMainInfo *mainPtr;
CkEvent *eventPtr; /* Event to dispatch. */
{
CkEventHandler *handlerPtr;
GenericHandler *genericPtr;
GenericHandler *genPrevPtr;
CkWindow *winPtr;
InProgress ip;
/*
* Invoke all the generic event handlers (those that are
* invoked for all events). If a generic event handler reports that
* an event is fully processed, go no further.
*/
for (genPrevPtr = NULL, genericPtr = genericList; genericPtr != NULL; ) {
if (genericPtr->deleteFlag) {
if (!genericHandlersActive) {
GenericHandler *tmpPtr;
/*
* This handler needs to be deleted and there are no
* calls pending through the handler, so now is a safe
* time to delete it.
*/
tmpPtr = genericPtr->nextPtr;
if (genPrevPtr == NULL) {
genericList = tmpPtr;
} else {
genPrevPtr->nextPtr = tmpPtr;
}
if (tmpPtr == NULL) {
lastGenericPtr = genPrevPtr;
}
(void) ckfree((char *) genericPtr);
genericPtr = tmpPtr;
continue;
}
} else {
int done;
genericHandlersActive++;
done = (*genericPtr->proc)(genericPtr->clientData, eventPtr);
genericHandlersActive--;
if (done) {
return;
}
}
genPrevPtr = genericPtr;
genericPtr = genPrevPtr->nextPtr;
}
if (Tcl_FindHashEntry(&mainPtr->winTable, (char *) eventPtr->any.winPtr)
== NULL) {
/*
* There isn't a CkWindow structure for this window.
*/
return;
}
winPtr = eventPtr->any.winPtr;
ip.eventPtr = eventPtr;
ip.winPtr = winPtr;
ip.nextHandler = NULL;
ip.nextPtr = pendingPtr;
pendingPtr = &ip;
for (handlerPtr = winPtr->handlerList; handlerPtr != NULL; ) {
if ((handlerPtr->mask & eventPtr->type) != 0) {
ip.nextHandler = handlerPtr->nextPtr;
(*(handlerPtr->proc))(handlerPtr->clientData, eventPtr);
handlerPtr = ip.nextHandler;
} else {
handlerPtr = handlerPtr->nextPtr;
}
}
/*
* Pass the event to the "bind" command mechanism.
*/
CkBindEventProc(winPtr, eventPtr);
pendingPtr = ip.nextPtr;
}
/*
*--------------------------------------------------------------
*
* CkEventDeadWindow --
*
* This procedure is invoked when it is determined that
* a window is dead. It cleans up event-related information
* about the window.
*
* Results:
* None.
*
* Side effects:
* Various things get cleaned up and recycled.
*
*--------------------------------------------------------------
*/
void
CkEventDeadWindow(winPtr)
CkWindow *winPtr; /* Information about the window
* that is being deleted. */
{
CkEventHandler *handlerPtr;
InProgress *ipPtr;
/*
* While deleting all the handlers, be careful to check for
* Ck_HandleEvent being about to process one of the deleted
* handlers. If it is, tell it to quit (all of the handlers
* are being deleted).
*/
while (winPtr->handlerList != NULL) {
handlerPtr = winPtr->handlerList;
winPtr->handlerList = handlerPtr->nextPtr;
for (ipPtr = pendingPtr; ipPtr != NULL; ipPtr = ipPtr->nextPtr) {
if (ipPtr->nextHandler == handlerPtr) {
ipPtr->nextHandler = NULL;
}
if (ipPtr->winPtr == winPtr) {
ipPtr->winPtr = NULL;
}
}
ckfree((char *) handlerPtr);
}
}
/*
*--------------------------------------------------------------
*
* handleFullResize --
*
* Invoked to process resize event
*
* Results:
* None.
*
* Side effects:
* Various things get cleaned up and recycled.
*
*--------------------------------------------------------------
*/
void handleFullResize(clientData, eventPtr)
ClientData clientData;
CkEvent *eventPtr;
{
CkMainInfo *mainPtr = (CkMainInfo *) clientData;
struct winsize winsz;
mainPtr->flags &= ~CK_RESIZING;
ioctl(0, TIOCGWINSZ, &winsz);
resizeterm (winsz.ws_row, winsz.ws_col);
if ( COLS > mainPtr->maxWidth ) {
mainPtr->maxWidth = COLS;
}
if ( LINES > mainPtr->maxHeight ) {
mainPtr->maxHeight = LINES;
}
Ck_GeometryRequest(mainPtr->winPtr, COLS, LINES);
Ck_ResizeWindow(mainPtr->winPtr, COLS, LINES);
/* refresh (required for mintty) */
}
/*
*--------------------------------------------------------------
*
* CkHandleInput --
*
* Process keyboard events from curses.
*
* Results:
* The return value is TK_FILE_HANDLED if the procedure
* actually found an event to process. If no event was found
* then TK_READABLE is returned.
*
* Side effects:
* The handling of the event could cause additional
* side effects.
*
*--------------------------------------------------------------
*/
void
CkHandleInput(clientData, mask)
ClientData clientData; /* Pointer to main info. */
int mask; /* OR-ed combination of the bits TK_READABLE,
* TK_WRITABLE, and TK_EXCEPTION, indicating
* current state of file. */
{
CkEvent event;
CkQEvt *qev;
CkMainInfo *mainPtr = (CkMainInfo *) clientData;
int code = 0;
static int buttonpressed = 0;
static int errCount = 0;
#if CK_USE_UTF
int ucp = 0;
char ucbuf[16];
Tcl_UniChar uch = 0;
wint_t w;
int rc;
#endif
if (!(mask & TCL_READABLE))
return;
readagain:
code = rc = get_wch(&w);
if (code == OK) {
/* nothing */
}
else if (code == KEY_CODE_YES) {
if ((w != KEY_MOUSE) && (w != KEY_RESIZE)) {
goto keyEvent;
}
if ( code == KEY_RESIZE ) {
goto readagain;
}
}
else if (code == ERR) {
return;
}
else {
/* -- this should never happen -- */
return;
}
/*
* Barcode reader handling.
*/
#if 0
if (mainPtr->flags & CK_HAS_BARCODE) {
BarcodeData *bd = (BarcodeData *) mainPtr->barcodeData;
/*
* Here, special handling for nested event loops:
* If BarCode event has been delivered already, we must
* reset the buffer index in order to get normal Key events.
*/
if (bd->delivered && bd->index >= 0) {
bd->delivered = 0;
bd->index = -1;
}
if (bd->index >= 0 || code == bd->startChar) {
if (code == bd->startChar) {
Tk_DeleteTimerHandler(bd->timer);
bd->timer = Tk_CreateTimerHandler(bd->pkttime, BarcodeTimeout,
(ClientData) mainPtr);
bd->index = 0;
} else if (code == bd->endChar) {
Tk_DeleteTimerHandler(bd->timer);
bd->timer = (Tk_TimerToken) NULL;
bd->delivered = 1;
event.key.type = CK_EV_BARCODE;
event.key.winPtr = mainPtr->focusPtr;
event.key.keycode = 0;
Ck_HandleEvent(mainPtr, &event);
/*
* Careful, event handler could turn barcode off.
* Only reset buffer index if BarCode event delivered
* flag is set.
*/
bd = (BarcodeData *) mainPtr->barcodeData;
if (bd != NULL && bd->delivered) {
bd->delivered = 0;
bd->index = -1;
}
goto readagain;
} else {
/* Leave space for one NUL byte. */
if (bd->index < sizeof (bd->buffer) - 1) {
#if CK_USE_UTF
char c, utfb[8];
int numc, i;
c = code;
if (mainPtr->isoEncoding) {
Tcl_ExternalToUtf(NULL, mainPtr->isoEncoding,
&c, 1, 0, NULL, utfb, sizeof (utfb),
NULL, &numc, NULL);
if (bd->index + numc < sizeof (bd->buffer) - 1) {
for (i = 0; i < numc; i++)
bd->buffer[bd->index + i] = utfb[i];
} else
bd->buffer[bd->index] = '\0';
bd->index += numc - 1;
} else {
if (code != 0) {
bd->buffer[bd->index] = code;
} else if (uch < 0x100) {
bd->buffer[bd->index] = uch;
}
}
#else
bd->buffer[bd->index] = code;
#endif
}
bd->index++;
}
goto readagain;
}
}
#endif
#ifdef NCURSES_MOUSE_VERSION
/*
* ncurses-1.9.8a has builtin mouse support for at least xterm.
*/
if ((code == KEY_MOUSE) || ((code == KEY_CODE_YES) && (w == KEY_MOUSE))) {
MEVENT mEvent;
int i, button = 0, modifiers = 0;
if (mainPtr->flags & CK_MOUSE_XTERM) {
goto getMouse;
}
if (getmouse(&mEvent) == ERR) {
return;
}
/* compute modifiers */
modifiers |= (mEvent.bstate & BUTTON_CTRL) ? CK_MOD_CONTROL : 0;
modifiers |= (mEvent.bstate & BUTTON_SHIFT) ? CK_MOD_SHIFT : 0;
modifiers |= (mEvent.bstate & BUTTON_ALT) ? CK_MOD_ALT : 0;
// @vca: to handle mouse motion events with button(s) pressed
// we will need to track the mouse state between events
// otherwise motion events with button(s) pressed
// will be reported as several MousePressed events
for (i = 1; i <= 5; i++) {
int mods = modifiers;
/* update modifiers */
// @todo: these modifiers don't seem to be reported by curses
mods |= (BUTTON_TRIPLE_CLICK(mEvent.bstate, i)) ? CK_MOD_TRIPLE: 0;
mods |= (BUTTON_DOUBLE_CLICK(mEvent.bstate, i)) ? CK_MOD_DOUBLE: 0;
if (BUTTON_PRESS(mEvent.bstate, i)) {
if (!(mEvent.bstate & REPORT_MOUSE_POSITION)) {
event.mouse.type = CK_EV_MOUSE_DOWN;
goto mouseEventNC;
}
else {
/* this is a mouse move with button pressed
* report it */
// @todo: this code is never executed
button = i;
break;
}
}
else if (BUTTON_RELEASE(mEvent.bstate, i)) {
event.mouse.type = CK_EV_MOUSE_UP;
mouseEventNC:
event.mouse.button = i;
event.mouse.modifiers = mods;
event.mouse.rootx = mEvent.x;
event.mouse.rooty = mEvent.y;
event.mouse.x = mEvent.x;
event.mouse.y = mEvent.y;
event.mouse.winPtr = Ck_GetWindowXY(mainPtr, &event.mouse.x,
&event.mouse.y, 1);
goto mkEvent;
}
}
// reached for mouse motion events (no buttons down)
if (mEvent.bstate & REPORT_MOUSE_POSITION) {
event.mouse.type = CK_EV_MOUSE_MOVE;
event.mouse.button = button;
event.mouse.modifiers = modifiers;
event.mouse.rootx = mEvent.x;
event.mouse.rooty = mEvent.y;
event.mouse.x = mEvent.x;
event.mouse.y = mEvent.y;
event.mouse.winPtr = Ck_GetWindowXY(mainPtr, &event.mouse.x,
&event.mouse.y, 1);
goto mkEvent;
}
goto readagain;
}
#endif
#ifdef __WIN32__
if ((mainPtr->flags & CK_HAS_MOUSE) && code == KEY_MOUSE) {
int i;
request_mouse_pos();
for (i = 0; i < 5; i++) {
if (Mouse_status.button[i] == BUTTON_PRESSED) {
event.mouse.type = CK_EV_MOUSE_DOWN;
goto mouseEvt;
} else if (Mouse_status.button[i] == BUTTON_RELEASED) {
event.mouse.type = CK_EV_MOUSE_UP;
mouseEvt:
event.mouse.button = i + 1;
event.mouse.x = Mouse_status.x;
event.mouse.y = Mouse_status.y;
event.mouse.winPtr = Ck_GetWindowXY(mainPtr, &event.mouse.x,
&event.mouse.y, 1);
goto mkEvent;
}
}
}
#endif
/*
* Xterm mouse report handling: Although GPM has an xterm module
* this is separately done here, since I want to be as independent
* as possible from GPM.
* It is assumed that the entire mouse report comes in one piece
* ie without any delay between the 6 relevant characters.
* Only a single button down/up event is generated.
*/
#ifndef __WIN32__
if ((mainPtr->flags & CK_MOUSE_XTERM) && (code == 0x1b || code == 0x9b || w == 0x1b || w == 0x9b)) {
int code2;
if (code == 0x9b)
goto getM;
code2 = getch();
if (code2 != ERR) {
if (code2 == '[')
goto getM;
ungetch(code2);
} else
errCount++;
goto keyEvent;
getM:
code2 = getch();
if (code2 != ERR) {
if (code2 == 'M')
goto getMouse;
ungetch(code2);
} else
errCount++;
goto keyEvent;
getMouse:
code2 = getch();
if (code2 == ERR) {
errCount++;
return;
}
event.mouse.button = ((code2 - 0x20) & 0x03) + 1;
code2 = getch();
if (code2 == ERR) {
errCount++;
return;
}
event.mouse.x = event.mouse.rootx = code2 - 0x20 - 1;
code2 = getch();
if (code2 == ERR) {
errCount++;
return;
}
event.mouse.y = event.mouse.rooty = code2 - 0x20 - 1;
if (event.mouse.button > 3) {
event.mouse.button = buttonpressed;
buttonpressed = 0;
event.mouse.type = CK_EV_MOUSE_UP;
goto mouseEvent;
} else if (buttonpressed == 0) {
buttonpressed = event.mouse.button;
event.mouse.type = CK_EV_MOUSE_DOWN;
mouseEvent:
event.mouse.winPtr = Ck_GetWindowXY(mainPtr, &event.mouse.x,
&event.mouse.y, 1);
goto mkEvent;
}
return;
}
#endif
keyEvent:
event.key.type = CK_EV_KEYPRESS;
event.key.winPtr = mainPtr->focusPtr;
event.key.curses_rc = rc;
event.key.curses_w = w;
event.key.is_uch = 0;
if ( rc == OK ) {
event.key.keycode = w;
event.key.uch = w;
event.key.is_uch = 1;
if ( w < 0x20 ) {
event.key.is_uch = 0;
}
if ( w > 0x100 ) {
event.key.keycode = 0;
}
}
if ( rc == KEY_CODE_YES ) {
event.key.keycode = w;
event.key.uch = 0;
}
#if 0
if ( code != OK ) {
event.key.keycode = code;
}
else {
event.key.keycode = w;
}
if (event.key.keycode < 0) {
event.key.keycode &= 0xff;
}
#if CK_USE_UTF
event.key.is_uch = 0;
if ( (code == OK) && (w > 0x100)) {
event.key.is_uch = 1;
event.key.keycode = 0;
event.key.uch = w;
} else {
event.key.uch = w;
}
if ( (code == OK) && (mainPtr->isoEncoding == NULL) &&
(w >= 0x20 && w < 0x100))
event.key.is_uch = 1;
#endif
#endif
mkEvent:
qev = (CkQEvt *) ckalloc(sizeof (CkQEvt));
qev->header.proc = Ck_HandleQEvent;
qev->event = event;
qev->mainPtr = mainPtr;
Tcl_QueueEvent(&qev->header, TCL_QUEUE_TAIL);
goto readagain;
}
static int
Ck_HandleQEvent(evPtr, flags)
Tcl_Event *evPtr;
int flags;
{
CkQEvt *qev = (CkQEvt *) evPtr;
if (!(flags & TCL_WINDOW_EVENTS)) {
return 0;
}
Ck_HandleEvent(qev->mainPtr, &qev->event);
//@todo : is it the right place ?
if ( qev->event.any.type == CK_EV_VIRTUAL ) {
free( qev->event.virt.evtype );
free( qev->event.virt.detail );
qev->event.virt.evtype = NULL;
qev->event.virt.detail = NULL;
}
return 1;
}
#ifdef HAVE_GPM
/*
*--------------------------------------------------------------
*
* CkHandleGPMInput --
*
* Process mouse events from GPM.
*
* Results:
* The return value is TK_FILE_HANDLED if the procedure
* actually found an event to process. If no event was found
* then TK_READABLE is returned.
*
* Side effects:
* The handling of the event could cause additional
* side effects.
*
*--------------------------------------------------------------
*/
void
CkHandleGPMInput(clientData, mask)
ClientData clientData; /* Pointer to main info. */
int mask; /* OR-ed combination of the bits TK_READABLE,
* TK_WRITABLE, and TK_EXCEPTION, indicating
* current state of file. */
{
Gpm_Event gpmEvent;
CkEvent event;
CkQEvt *qev;
CkMainInfo *mainPtr = (CkMainInfo *) clientData;
int ret, type;
if (!(mask & TCL_READABLE))
return;
ret = Gpm_GetEvent(&gpmEvent);
if (ret == 0) {
/*
* GPM connection is closed; delete this file handler.
*/
Tcl_DeleteFileHandler((int) mainPtr->mouseData);
mainPtr->mouseData = 0;
return;
} else if (ret == -1)
return;
GPM_DRAWPOINTER(&gpmEvent);
type = gpmEvent.type & (GPM_DOWN | GPM_UP);
if (type == GPM_DOWN || type == GPM_UP) {
event.mouse.type = type == GPM_DOWN ? CK_EV_MOUSE_DOWN :
CK_EV_MOUSE_UP;
if (gpmEvent.buttons & GPM_B_LEFT)
event.mouse.button = 1;
else if (gpmEvent.buttons & GPM_B_MIDDLE)
event.mouse.button = 2;
else if (gpmEvent.buttons & GPM_B_RIGHT)
event.mouse.button = 3;
event.mouse.x = event.mouse.rootx = gpmEvent.x - 1;
event.mouse.y = event.mouse.rooty = gpmEvent.y - 1;
event.mouse.winPtr = Ck_GetWindowXY(mainPtr, &event.mouse.x,
&event.mouse.y, 1);
qev = (CkQEvt *) ckalloc(sizeof (CkQEvt));
qev->header.proc = Ck_HandleQEvent;
qev->event = event;
qev->mainPtr = mainPtr;
Tcl_QueueEvent(&qev->header, TCL_QUEUE_TAIL);
}
}
#endif /* HAVE_GPM */
/*
*--------------------------------------------------------------
*
* Ck_QueueFullResizeEvent --
*
* Builds a resize event and queue it.
*