-
Notifications
You must be signed in to change notification settings - Fork 0
/
ckWindow.c
2810 lines (2551 loc) · 70.3 KB
/
ckWindow.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
/*
* ckWindow.c --
*
* This file provides basic window-manipulation procedures.
*
* 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
/*
* Main information.
*/
CkMainInfo *ckMainInfo = NULL;
#ifdef __WIN32__
/*
* Curses input event handling information.
*/
typedef struct {
HANDLE stdinHandle;
HWND hwnd;
HANDLE thread;
CkMainInfo *mainPtr;
} InputInfo;
static InputInfo inputInfo = {
INVALID_HANDLE_VALUE,
NULL,
INVALID_HANDLE_VALUE,
NULL
};
static void InputSetup _ANSI_ARGS_((InputInfo *inputInfo));
static void InputExit _ANSI_ARGS_((ClientData clientData));
static void InputThread _ANSI_ARGS_((void *arg));
static LRESULT CALLBACK InputHandler _ANSI_ARGS_((HWND hwnd, UINT message,
WPARAM wParam,
LPARAM lParam));
static void InputHandler2 _ANSI_ARGS_((ClientData clientData));
#endif
static void CkEvtExit _ANSI_ARGS_((ClientData clientData));
static void CkEvtSetup _ANSI_ARGS_((ClientData clientData,
int flags));
static void CkEvtCheck _ANSI_ARGS_((ClientData clientData,
int flags));
/*
* The variables below hold several uid's that are used in many places
* in the toolkit.
*/
Ck_Uid ckDisabledUid = NULL;
Ck_Uid ckActiveUid = NULL;
Ck_Uid ckNormalUid = NULL;
/*
* The following structure defines all of the commands supported by
* the toolkit, and the C procedures that execute them.
*/
typedef int (CkCmdProc) _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp,
int argc, char **argv));
typedef int (CkCmdObjProc) _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]));
typedef struct {
char *name; /* Name of command. */
CkCmdProc *cmdProc; /* Command procedure. */
} CkCmd;
typedef struct {
char *name; /* Name of command. */
CkCmdObjProc *cmdProc; /* Command procedure. */
} CkCmdObj;
CkCmdObj objCommands[] =
{
{"bell", Ck_BellCmdObj},
{"bind", Ck_BindCmdObj},
{"bindtags", Ck_BindtagsCmdObj},
{"curses", Ck_CursesCmdObj},
{"destroy", Ck_DestroyCmdObj},
{"focus", Ck_FocusCmdObj},
{"exit", Ck_ExitCmdObj},
{"lower", Ck_LowerCmdObj},
{"option", Ck_OptionCmdObj},
{"raise", Ck_RaiseCmdObj},
{"recorder", Ck_RecorderCmdObj},
{"tkwait", Ck_TkwaitCmdObj},
{"update", Ck_UpdateCmdObj},
{"winfo", Ck_WinfoCmdObj},
{(char *) NULL, (CkCmdObjProc *) NULL}
};
CkCmd commands[] =
{
/*
* Commands that are part of the intrinsics:
*/
// {"bell", Ck_BellCmd},
// {"bind", Ck_BindCmd},
// {"bindtags", Ck_BindtagsCmd},
// {"curses", Ck_CursesCmd},
// {"destroy", Ck_DestroyCmd},
// {"exit", Ck_ExitCmd},
// {"focus", Ck_FocusCmd},
{"grid", Ck_GridCmd},
// {"lower", Ck_LowerCmd},
// {"option", Ck_OptionCmd},
{"pack", Ck_PackCmd},
{"place", Ck_PlaceCmd},
// {"raise", Ck_RaiseCmd},
// {"recorder", Ck_RecorderCmd},
// {"tkwait", Ck_TkwaitCmd},
// {"update", Ck_UpdateCmd},
// {"winfo", Ck_WinfoCmd},
/*
* Widget-creation commands.
*/
{"button", Ck_ButtonCmd},
{"checkbutton", Ck_ButtonCmd},
{"entry", Ck_EntryCmd},
{"frame", Ck_FrameCmd},
{"label", Ck_ButtonCmd},
{"listbox", Ck_ListboxCmd},
{"menu", Ck_MenuCmd},
{"menubutton", Ck_MenubuttonCmd},
{"message", Ck_MessageCmd},
{"radiobutton", Ck_ButtonCmd},
{"scrollbar", Ck_ScrollbarCmd},
{"text", Ck_TextCmd},
{"toplevel", Ck_FrameCmd},
{"tree", Ck_TreeCmd},
{"terminal", Ck_TerminalCmd},
{"progress", Ck_ProgressCmd},
{"playcard", Ck_PlaycardCmd},
{"spinbox", Ck_SpinboxCmd},
{"color", Ck_ColorCmd},
{(char *) NULL, (CkCmdProc *) NULL}
};
/*
* Static procedures of this module.
*/
static void UnlinkWindow _ANSI_ARGS_((CkWindow *winPtr));
static void UnlinkToplevel _ANSI_ARGS_((CkWindow *winPtr));
static void ChangeToplevelFocus _ANSI_ARGS_((CkWindow *winPtr));
static void DoRefresh _ANSI_ARGS_((ClientData clientData));
static void RefreshToplevels _ANSI_ARGS_((CkWindow *winPtr));
static void RefreshThem _ANSI_ARGS_((CkWindow *winPtr));
static void UpdateHWCursor _ANSI_ARGS_((CkMainInfo *mainPtr));
static CkWindow *GetWindowXY _ANSI_ARGS_((CkWindow *winPtr, int *xPtr,
int *yPtr));
static int DeadAppCmdObj _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp, int objc, Tcl_Obj* CONST objv[]));
static int ExecCmd _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp, int argc, char **argv));
static int PutsCmd _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp, int argc, char **argv));
static int CloseCmd _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp, int argc, char **argv));
static int FlushCmd _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp, int argc, char **argv));
static int ReadCmd _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp, int argc, char **argv));
static int GetsCmd _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp, int argc, char **argv));
/*
* Some plain Tcl commands are handled specially.
*/
CkCmd redirCommands[] = {
#ifndef __WIN32__
{"exec", ExecCmd},
#endif
{"puts", PutsCmd},
{"close", CloseCmd},
{"flush", FlushCmd},
{"read", ReadCmd},
{"gets", GetsCmd},
{(char *) NULL, (CkCmdProc *) NULL}
};
/*
* The following structure is used as ClientData for redirected
* plain Tcl commands.
*/
typedef struct {
CkMainInfo *mainPtr;
Tcl_CmdInfo cmdInfo;
} RedirInfo;
/*
*--------------------------------------------------------------
*
* CkpStartMouse --
*
* Called to start mouse reporting
*
* Results:
* Nothing
*
* Side effects:
* None
*
*--------------------------------------------------------------
*/
void CkpStartMouse()
{
if ((ckMainInfo != NULL) && (ckMainInfo->flags & CK_HAS_MOUSE)) {
fflush(stdout);
fputs("\033[?1003h", stdout);
fflush(stdout);
}
}
/*
*--------------------------------------------------------------
*
* CkpEndMouse --
*
* Called to stop mouse reporting
*
* Results:
* Nothing
*
* Side effects:
* None
*
*--------------------------------------------------------------
*/
void CkpEndMouse()
{
fflush(stdout);
fputs("\033[?1003l", stdout);
fflush(stdout);
}
/*
*--------------------------------------------------------------
*
* onSigwinch --
*
* Called when SIGWINCH is received
*
* Results:
* Nothing
*
* Side effects:
* Sets a flags telling that the main window was resized.
*
*--------------------------------------------------------------
*/
static
void onSigwinch( int dummy )
{
if ( ckMainInfo != NULL ) {
ckMainInfo->flags |= CK_RESIZING;
}
}
/*
*--------------------------------------------------------------
*
* NewWindow --
*
* This procedure creates and initializes a CkWindow structure.
*
* Results:
* The return value is a pointer to the new window.
*
* Side effects:
* A new window structure is allocated and all its fields are
* initialized.
*
*--------------------------------------------------------------
*/
static CkWindow *
NewWindow(parentPtr)
CkWindow *parentPtr;
{
CkWindow *winPtr;
winPtr = (CkWindow *) ckalloc(sizeof (CkWindow));
winPtr->window = NULL;
winPtr->childList = NULL;
winPtr->lastChildPtr = NULL;
winPtr->parentPtr = NULL;
winPtr->nextPtr = NULL;
winPtr->topLevPtr = NULL;
winPtr->mainPtr = NULL;
winPtr->pathName = NULL;
winPtr->nameUid = NULL;
winPtr->classUid = NULL;
winPtr->handlerList = NULL;
winPtr->tagPtr = NULL;
winPtr->numTags = 0;
winPtr->focusPtr = NULL;
winPtr->geomMgrPtr = NULL;
winPtr->geomData = NULL;
winPtr->optionLevel = -1;
winPtr->reqWidth = winPtr->reqHeight = 1;
winPtr->x = winPtr->y = 0;
winPtr->width = winPtr->height = 1;
winPtr->fg = COLOR_WHITE;
winPtr->bg = COLOR_BLACK;
winPtr->attr = A_NORMAL;
winPtr->flags = 0;
return winPtr;
}
/*
*----------------------------------------------------------------------
*
* NameWindow --
*
* This procedure is invoked to give a window a name and insert
* the window into the hierarchy associated with a particular
* application.
*
* Results:
* A standard Tcl return value.
*
* Side effects:
* See above.
*
*----------------------------------------------------------------------
*/
static int
NameWindow(interp, winPtr, parentPtr, name)
Tcl_Interp *interp; /* Interpreter to use for error reporting. */
CkWindow *winPtr; /* Window that is to be named and inserted. */
CkWindow *parentPtr; /* Pointer to logical parent for winPtr
* (used for naming, options, etc.). */
char *name; /* Name for winPtr; must be unique among
* parentPtr's children. */
{
char *pathName;
int new;
Tcl_HashEntry *hPtr;
int length1, length2;
/*
* Setup all the stuff except name right away, then do the name stuff
* last. This is so that if the name stuff fails, everything else
* will be properly initialized (needed to destroy the window cleanly
* after the naming failure).
*/
winPtr->parentPtr = parentPtr;
winPtr->nextPtr = NULL;
if (parentPtr->childList == NULL) {
parentPtr->lastChildPtr = winPtr;
parentPtr->childList = winPtr;
} else {
parentPtr->lastChildPtr->nextPtr = winPtr;
parentPtr->lastChildPtr = winPtr;
}
winPtr->mainPtr = parentPtr->mainPtr;
winPtr->nameUid = Ck_GetUid(name);
/*
* Don't permit names that start with an upper-case letter: this
* will just cause confusion with class names in the option database.
*/
if (isupper((unsigned char) name[0])) {
Tcl_AppendResult(interp,
"window name starts with an upper-case letter: \"",
name, "\"", (char *) NULL);
return TCL_ERROR;
}
length1 = strlen(parentPtr->pathName);
length2 = strlen(name);
pathName = (char *) ckalloc((unsigned) (length1+length2+2));
if (length1 == 1) {
pathName[0] = '.';
strcpy(pathName+1, name);
} else {
strcpy(pathName, parentPtr->pathName);
pathName[length1] = '.';
strcpy(pathName+length1+1, name);
}
hPtr = Tcl_CreateHashEntry(&parentPtr->mainPtr->nameTable, pathName, &new);
if (!new) {
Tcl_AppendResult(interp, "window name \"", name,
"\" already exists in parent", (char *) NULL);
return TCL_ERROR;
}
Tcl_SetHashValue(hPtr, winPtr);
winPtr->pathName = Tcl_GetHashKey(&parentPtr->mainPtr->nameTable, hPtr);
Tcl_CreateHashEntry(&parentPtr->mainPtr->winTable, (char *) winPtr, &new);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* Ck_MainWindow --
*
* Returns the main window for an application.
*
* Results:
* If interp is associated with the main window, the main
* window is returned. Otherwise NULL is returned and an
* error message is left in interp->result.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
CkWindow *
Ck_MainWindow(interp)
Tcl_Interp *interp; /* Interpreter that embodies application,
* also used for error reporting. */
{
if (ckMainInfo == NULL || ckMainInfo->interp != interp) {
if (interp != NULL)
Tcl_SetObjResult( interp, Tcl_NewStringObj("no main window for application.",-1));
return NULL;
}
return ckMainInfo->winPtr;
}
/*
*----------------------------------------------------------------------
*
* Ck_CreateMainWindow --
*
* Make the main window.
*
* Results:
* The return value is a token for the new window, or NULL if
* an error prevented the new window from being created. If
* NULL is returned, an error message will be left in
* interp->result.
*
* Side effects:
* A new window structure is allocated locally.
*
*----------------------------------------------------------------------
*/
CkWindow *
Ck_CreateMainWindow(interp, className)
Tcl_Interp *interp; /* Interpreter to use for error reporting. */
char *className; /* Class name of the new main window. */
{
int dummy, mask;
Tcl_HashEntry *hPtr;
CkMainInfo *mainPtr;
CkWindow *winPtr;
CkCmdObj *cmdObjPtr;
CkCmd *cmdPtr;
#ifdef SIGTSTP
#ifdef HAVE_SIGACTION
struct sigaction oldsig, newsig;
#else
Ck_SignalProc sigproc;
#endif
#endif
#ifdef SIGWINCH
#ifdef HAVE_SIGACTION
struct sigaction oldsigwinch, newsigwinch;
#else
Ck_SignalProc sigwinchproc;
#endif
#endif
#ifdef NCURSES_MOUSE_VERSION
MEVENT mEvent;
#endif
char *term;
int isxterm = 0;
/*
* For now, only one main window may exists for the application.
*/
if (ckMainInfo != NULL)
return NULL;
/*
* Create the basic CkWindow structure.
*/
winPtr = NewWindow(NULL);
/*
* Create the CkMainInfo structure for this application, and set
* up name-related information for the new window.
*/
mainPtr = (CkMainInfo *) ckalloc(sizeof(CkMainInfo));
mainPtr->winPtr = winPtr;
mainPtr->interp = interp;
Tcl_InitHashTable(&mainPtr->nameTable, TCL_STRING_KEYS);
Tcl_InitHashTable(&mainPtr->winTable, TCL_ONE_WORD_KEYS);
mainPtr->topLevPtr = NULL;
mainPtr->focusPtr = winPtr;
mainPtr->bindingTable = Ck_CreateBindingTable(interp);
mainPtr->optionRootPtr = NULL;
mainPtr->refreshCount = 0;
mainPtr->refreshDelay = 0;
mainPtr->lastRefresh = 0;
mainPtr->refreshTimer = NULL;
mainPtr->flags = 0;
ckMainInfo = mainPtr;
winPtr->mainPtr = mainPtr;
winPtr->nameUid = Ck_GetUid(".");
winPtr->classUid = Ck_GetUid("Main"); /* ??? */
winPtr->flags |= CK_TOPLEVEL;
hPtr = Tcl_CreateHashEntry(&mainPtr->nameTable, (char *) winPtr->nameUid,
&dummy);
Tcl_SetHashValue(hPtr, winPtr);
winPtr->pathName = Tcl_GetHashKey(&mainPtr->nameTable, hPtr);
Tcl_CreateHashEntry(&mainPtr->winTable, (char *) winPtr, &dummy);
ckNormalUid = Ck_GetUid("normal");
ckDisabledUid = Ck_GetUid("disabled");
ckActiveUid = Ck_GetUid("active");
#if CK_USE_UTF
#ifdef __WIN32__
{
char enc[32], *envcp = getenv("CK_USE_ENCODING");
unsigned int cp = GetConsoleCP();
if (envcp && strncmp(envcp, "cp", 2) == 0) {
cp = atoi(envcp + 2);
SetConsoleCP(cp);
cp = GetConsoleCP();
}
if (GetConsoleOutputCP() != cp) {
SetConsoleOutputCP(cp);
}
sprintf(enc, "cp%d", cp);
mainPtr->isoEncoding = Tcl_GetEncoding(NULL, enc);
}
#else
/*
* Use default system encoding as suggested by
* Anton Kovalenko <[email protected]>.
* May be overriden by environment variable.
*/
mainPtr->isoEncoding = Tcl_GetEncoding(NULL, getenv("CK_USE_ENCODING"));
if (mainPtr->isoEncoding == NULL) {
mainPtr->isoEncoding = Tcl_GetEncoding(NULL, NULL);
}
#endif
if (mainPtr->isoEncoding == NULL) {
panic("standard encoding not found");
}
if (strcmp(Tcl_GetEncodingName(mainPtr->isoEncoding), "utf-8") == 0) {
Tcl_FreeEncoding(mainPtr->isoEncoding);
mainPtr->isoEncoding = NULL;
}
#endif
/* Curses related initialization */
#ifdef SIGTSTP
/* This is essential for ncurses-1.9.4 */
#ifdef HAVE_SIGACTION
newsig.sa_handler = SIG_IGN;
sigfillset(&newsig.sa_mask);
newsig.sa_flags = 0;
sigaction(SIGTSTP, &newsig, &oldsig);
#else
sigproc = (Ck_SignalProc) signal(SIGTSTP, SIG_IGN);
#endif
#endif
#ifndef __WIN32__
/*
* Fix for problem when X server left linux console
* in non-blocking mode
*/
fcntl(0, F_SETFL, fcntl(0, F_GETFL) & (~O_NDELAY));
#endif
if (initscr() == (WINDOW *) ERR) {
ckfree((char *) winPtr);
return NULL;
}
#ifdef SIGTSTP
/* This is essential for ncurses-1.9.4 */
#ifdef HAVE_SIGACTION
sigaction(SIGTSTP, &oldsig, NULL);
#else
signal(SIGTSTP, sigproc);
#endif
#endif
raw();
noecho();
nonl();
idlok(stdscr, TRUE);
intrflush(stdscr, FALSE);
scrollok(stdscr, FALSE);
keypad(stdscr, TRUE);
nodelay(stdscr, TRUE);
meta(stdscr, TRUE);
notimeout(stdscr, TRUE);
mainPtr->maxWidth = COLS;
mainPtr->maxHeight = LINES;
winPtr->width = mainPtr->maxWidth;
winPtr->height = mainPtr->maxHeight;
winPtr->window = newwin(winPtr->height, winPtr->width, 0, 0);
if (has_colors()) {
start_color();
use_default_colors();
mainPtr->flags |= CK_HAS_COLOR;
}
Ck_InitColor();
Ck_InitKeys();
#ifdef NCURSES_MOUSE_VERSION
mouseinterval(1);
// @vca: capture all mouse events
mask = mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
mainPtr->flags |= (getmouse(&mEvent) != ERR) ? CK_HAS_MOUSE : 0;
// @vca: according to ncurses manual, getmouse will return an error if there is no event in the queue
// @vca: as a consequence, CK_HAS_MOUSE will not be set and mouse reporting will fail in the application
// @vca: for the moment I force mouse reporting. It seems to be a good solution with curses 6.0 under cygwin running mintty
// @vca: as a terminal emulator. mintty advertises itself with TERM=xterm
mainPtr->flags |= CK_HAS_MOUSE;
CkpStartMouse();
#endif /* NCURSES_MOUSE_VERSION */
#ifdef __WIN32__
mouse_set(BUTTON1_PRESSED | BUTTON1_RELEASED |
BUTTON2_PRESSED | BUTTON2_RELEASED |
BUTTON3_PRESSED | BUTTON3_RELEASED);
mainPtr->flags |= CK_HAS_MOUSE;
term = "win32";
#else
term = getenv("TERM");
isxterm = strncmp(term, "xterm", 5) == 0 ||
strncmp(term, "mintty", 6) == 0 ||
strncmp(term, "rxvt", 4) == 0 ||
strncmp(term, "kterm", 5) == 0 ||
strncmp(term, "mtm", 3) == 0 ||
(term[0] != '\0' && strncmp(term + 1, "xterm", 5) == 0);
if (!(mainPtr->flags & CK_HAS_MOUSE) && isxterm) {
mainPtr->flags |= CK_HAS_MOUSE | CK_MOUSE_XTERM;
CkpStartMouse();
}
#endif /* __WIN32__ */
#ifdef HAVE_GPM
/*
* Some ncurses aren't compiled with GPM support built in,
* therefore by setting the following environment variable
* usage of GPM can be turned on.
*/
if (!isxterm && (mainPtr->flags & CK_HAS_MOUSE)) {
char *forcegpm = getenv("CK_USE_GPM");
if (forcegpm && strchr("YyTt123456789", forcegpm[0])) {
mainPtr->flags &= ~CK_HAS_MOUSE;
}
}
if (!isxterm && !(mainPtr->flags & CK_HAS_MOUSE)) {
int fd;
Gpm_Connect conn;
EXTERN void CkHandleGPMInput _ANSI_ARGS_((ClientData clientData,
int mask));
conn.eventMask = GPM_DOWN | GPM_UP | GPM_MOVE;
conn.defaultMask = 0;
conn.minMod = 0;
conn.maxMod = 0;
fd = Gpm_Open(&conn, 0);
if (fd >= 0) {
mainPtr->flags |= CK_HAS_MOUSE;
mainPtr->mouseData = fd;
Tcl_CreateFileHandler(fd, TCL_READABLE,
CkHandleGPMInput, (ClientData) mainPtr);
}
}
#endif /* HAVE_GPM */
#ifdef __WIN32__
/* PDCurses specific !!! */
inputInfo.mainPtr = mainPtr;
inputInfo.stdinHandle = GetStdHandle(STD_INPUT_HANDLE);
typeahead(-1);
SetConsoleMode(inputInfo.stdinHandle,
ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT);
InputSetup(&inputInfo);
#else
Tcl_CreateFileHandler(0,
TCL_READABLE, CkHandleInput, (ClientData) mainPtr);
#endif
Tcl_CreateEventSource(CkEvtSetup, CkEvtCheck, (ClientData) mainPtr);
Tcl_CreateExitHandler(CkEvtExit, (ClientData) mainPtr);
idlok(winPtr->window, TRUE);
scrollok(winPtr->window, FALSE);
keypad(winPtr->window, TRUE);
nodelay(winPtr->window, TRUE);
meta(winPtr->window, TRUE);
curs_set(0);
while (getch() != ERR) {
/* empty loop body. */
}
winPtr->flags |= CK_MAPPED;
Ck_SetWindowAttr(winPtr, winPtr->fg, winPtr->bg, winPtr->attr);
Ck_ClearToBot(winPtr, 0, 0);
Ck_EventuallyRefresh(winPtr);
//@vca : create resize event handler
EXTERN void handleFullResize _ANSI_ARGS_((ClientData clientData,
CkEvent *eventPtr));
Ck_CreateEventHandler(winPtr, CK_EV_RESIZE, handleFullResize, mainPtr);
#ifdef SIGWINCH
#ifdef HAVE_SIGACTION
newsigwinch.sa_handler = onSigwinch;
sigfillset(&newsigwinch.sa_mask);
newsigwinch.sa_flags = 0;
sigaction(SIGWINCH, &newsigwinch, &oldsigwinch);
#else
sigwinchproc = (Ck_SignalProc) signal(SIGWINCH, onSigwinch);
#endif
#endif
/*
* Bind in Ck's commands.
*/
for (cmdObjPtr = objCommands; cmdObjPtr->name != NULL; cmdObjPtr++) {
Tcl_CreateObjCommand(interp, cmdObjPtr->name, cmdObjPtr->cmdProc,
(ClientData) winPtr, (Tcl_CmdDeleteProc *) NULL);
}
for (cmdPtr = commands; cmdPtr->name != NULL; cmdPtr++) {
Tcl_CreateCommand(interp, cmdPtr->name, cmdPtr->cmdProc,
(ClientData) winPtr, (Tcl_CmdDeleteProc *) NULL);
}
/*
* Redirect some critical Tcl commands to our own procedures
*/
for (cmdPtr = redirCommands; cmdPtr->name != NULL; cmdPtr++) {
RedirInfo *redirInfo;
Tcl_DString cmdName;
extern int TclRenameCommand _ANSI_ARGS_((Tcl_Interp *interp,
char *oldName, char *newName));
redirInfo = (RedirInfo *) ckalloc(sizeof (RedirInfo));
redirInfo->mainPtr = mainPtr;
Tcl_GetCommandInfo(interp, cmdPtr->name, &redirInfo->cmdInfo);
Tcl_DStringInit(&cmdName);
Tcl_DStringAppend(&cmdName, "____", -1);
Tcl_DStringAppend(&cmdName, cmdPtr->name, -1);
TclRenameCommand(interp, cmdPtr->name, Tcl_DStringValue(&cmdName));
Tcl_DStringFree(&cmdName);
Tcl_CreateCommand(interp, cmdPtr->name, cmdPtr->cmdProc,
(ClientData) redirInfo, (Tcl_CmdDeleteProc *) free);
}
/*
* Set variables for the intepreter.
*/
Tcl_SetVar(interp, "ck_version", CK_VERSION, TCL_GLOBAL_ONLY);
/*
* Make main window into a frame widget.
*/
Ck_SetClass(winPtr, className);
CkInitFrame(interp, winPtr, 0, NULL);
mainPtr->topLevPtr = winPtr;
winPtr->focusPtr = winPtr;
return winPtr;
}
/*
*----------------------------------------------------------------------
*
* Ck_Init --
*
* This procedure is invoked to add Ck to an interpreter. It
* incorporates all of Ck's commands into the interpreter and
* creates the main window for a new Ck application.
*
* Results:
* Returns a standard Tcl completion code and sets interp->result
* if there is an error.
*
* Side effects:
* Depends on what's in the ck.tcl script.
*
*----------------------------------------------------------------------
*/
int
Ck_Init(interp)
Tcl_Interp *interp; /* Interpreter to initialize. */
{
CkWindow *mainWindow;
char *p, *name, *class;
int code;
static char initCmd[] =
"proc init {} {\n\
global ck_library ck_version\n\
rename init {}\n\
tcl_findLibrary ck $ck_version 0 ck.tcl CK_LIBRARY ck_library\n\
}\n\
init";
p = Tcl_GetVar(interp, "argv0", TCL_GLOBAL_ONLY);
if (p == NULL || *p == '\0')
p = "Ck";
name = strrchr(p, '/');
if (name != NULL)
name++;
else
name = p;
class = (char *) ckalloc((unsigned) (strlen(name) + 1));
strcpy(class, name);
class[0] = toupper((unsigned char) class[0]);
mainWindow = Ck_CreateMainWindow(interp, class);
ckfree(class);
return Tcl_Eval(interp, initCmd);
}
/*
*--------------------------------------------------------------
*
* Ck_CreateWindow --
*
* Create a new window as a child of an existing window.
*
* Results:
* The return value is the pointer to the new window.
* If an error occurred in creating the window, then an
* error message is left in interp->result and NULL is
* returned.
*
* Side effects:
* A new window structure is allocated locally. A curses
* window is not initially created, but will be created
* the first time the window is mapped.
*
*--------------------------------------------------------------
*/
CkWindow *
Ck_CreateWindow(interp, parentPtr, name, toplevel)
Tcl_Interp *interp; /* Interpreter to use for error reporting.
* Interp->result is assumed to be
* initialized by the caller. */
CkWindow *parentPtr; /* Parent of new window. */
char *name; /* Name for new window. Must be unique
* among parent's children. */
int toplevel; /* If true, create toplevel window. */
{
CkWindow *winPtr;
winPtr = NewWindow(parentPtr);
if (NameWindow(interp, winPtr, parentPtr, name) != TCL_OK) {
Ck_DestroyWindow(winPtr);
return NULL;
}
if (toplevel) {
CkWindow *wPtr;
winPtr->flags |= CK_TOPLEVEL;
winPtr->focusPtr = winPtr;
if (winPtr->mainPtr->topLevPtr == NULL) {
winPtr->topLevPtr = winPtr->mainPtr->topLevPtr;
winPtr->mainPtr->topLevPtr = winPtr;
} else {
for (wPtr = winPtr->mainPtr->topLevPtr; wPtr->topLevPtr != NULL;
wPtr = wPtr->topLevPtr) {
/* Empty loop body. */
}
winPtr->topLevPtr = wPtr->topLevPtr;
wPtr->topLevPtr = winPtr;
}
}
return winPtr;
}
/*
*----------------------------------------------------------------------
*
* Ck_CreateWindowFromPath --
*
* This procedure is similar to Ck_CreateWindow except that
* it uses a path name to create the window, rather than a
* parent and a child name.
*
* Results:
* The return value is the pointer to the new window.
* If an error occurred in creating the window, then an
* error message is left in interp->result and NULL is
* returned.
*
* Side effects:
* A new window structure is allocated locally. A curses
* window is not initially created, but will be created
* the first time the window is mapped.
*
*----------------------------------------------------------------------
*/
CkWindow *
Ck_CreateWindowFromPath(interp, anywin, pathName, toplevel)
Tcl_Interp *interp; /* Interpreter to use for error reporting.
* Interp->result is assumed to be
* initialized by the caller. */
CkWindow *anywin; /* Pointer to any window in application
* that is to contain new window. */
char *pathName; /* Path name for new window within the
* application of anywin. The parent of
* this window must already exist, but
* the window itself must not exist. */
int toplevel; /* If true, create toplevel window. */
{
#define FIXED_SPACE 5
char fixedSpace[FIXED_SPACE+1];
char *p;
CkWindow *parentPtr, *winPtr;
int numChars;
/*
* Strip the parent's name out of pathName (it's everything up
* to the last dot). There are two tricky parts: (a) must
* copy the parent's name somewhere else to avoid modifying
* the pathName string (for large names, space for the copy
* will have to be malloc'ed); (b) must special-case the
* situation where the parent is ".".
*/
p = strrchr(pathName, '.');
if (p == NULL) {
Tcl_AppendResult(interp, "bad window path name \"", pathName,
"\"", (char *) NULL);
return NULL;
}
numChars = p - pathName;
if (numChars > FIXED_SPACE) {
p = (char *) ckalloc((unsigned) (numChars+1));
} else {
p = fixedSpace;
}
if (numChars == 0) {
*p = '.';
p[1] = '\0';
} else {
strncpy(p, pathName, numChars);
p[numChars] = '\0';
}
/*
* Find the parent window.
*/
parentPtr = Ck_NameToWindow(interp, p, anywin);
if (p != fixedSpace) {
ckfree(p);