-
Notifications
You must be signed in to change notification settings - Fork 54
/
unicurses.py
2445 lines (2205 loc) · 70.7 KB
/
unicurses.py
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
# UniCurses -- A unified multiplatform Curses provider library for Python 2.x/3.x
# Copyright (C) 2010 by Michael Kamensky.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# import Curses (either natively if supported or via PDCurses using FFI if on MS Windows)
import sys
import os
import locale
global pdlib
global NCURSES
global PDC_LEAVEOK
global pdlib
PDC_LEAVEOK = False # LeaveOK emulation in PDC
NCURSES = False # Native curses support
NCURSES_AVAILABLE = False # True if the NCurses is available natively
pdlib = None # PD library, if applicable
UCS_DEFAULT_WRAPPER = "" # A constant for the default wrapper (ucs_reconfigure)
stdscr = -1 # A pointer to the standard screen
locale.setlocale(locale.LC_ALL,'')
code = locale.getpreferredencoding() # TODO: fix this to actually work on native ncurses
try:
import ctypes
except ImportError:
print("Fatal error: this Python release does not support ctypes, please upgrade your Python distribution if you want to use UniCurses on a "+sys.platform+" platform.\n")
raise ImportError("UniCurses initialization error - ctypes FFI not supported.")
try:
import curses # see if the platform supports curses natively
import curses.panel
NCURSES_AVAILABLE = True
NCURSES = True
except ImportError:
if sys.platform.find('win') == -1:
print("Fatal error: this platform is not supported by UniCurses (either you're running a very old Python distribution below v2.6, or you're using an exotic operating system that's neither Win nor *nix).\n")
raise ImportError("UniCurses initialization error - unsupported platform.")
else:
pdcursesdll = "pdcurses.dll"
try:
if hasattr(sys, '_MEIPASS'):
pdcursesdll = os.path.join(sys._MEIPASS, "pdcurses.dll")
except:
pass
if not os.access(pdcursesdll,os.F_OK):
print("Fatal error: can't find pdcurses.dll for linking, make sure PDCurses v3.4+ is in the same folder as UniCurses if you want to use UniCurses on a "+sys.platform+" platform.\n")
raise ImportError("UniCurses initialization error - pdcurses.dll not found.")
pdlib = ctypes.CDLL(pdcursesdll) # we're on winXX, use pdcurses instead of native ncurses
# +++ PDCurses/NCurses curses.h marco wrappers and other prereqs +++
# A PDC structure for the mouse events
if not NCURSES:
class MEVENT(ctypes.Structure):
_fields_ = [("id", ctypes.c_short),
("x", ctypes.c_int),
("y", ctypes.c_int),
("z", ctypes.c_int),
("mmask_t", ctypes.c_ulong)]
# Reconfigure the UniCurses wrapper to use a certain library instead of the default
# PDCurses and the default NCurses. This must be called before initscr().
# Pass an empty string or UCS_DEFAULT_WRAPPER to use the default wrapper.
# !!! THIS IS NOT FOR GENERAL USE AND WILL IN MOST CASES BREAK UNICURSES !!!
# !!! EVEN IF IT DOESN'T MAKE YOUR APP CRASH OR HANG, IT MAY BREAK PORTABILITY !!!
# !!! IF YOU DON'T KNOW WHAT THIS MAY BE USED FOR, YOU DON'T NEED TO USE IT !!!
def ucs_reconfigure(wrapper_ncurses, wrapper_pdcurses):
global NCURSES
global NCURSES_AVAILABLE
global pdlib
if NCURSES_AVAILABLE:
if wrapper_ncurses == UCS_DEFAULT_WRAPPER:
NCURSES = True
else:
NCURSES = False
pdlib = ctypes.CDLL(wrapper_ncurses)
try:
pdlib = ctypes.CDLL(wrapper_ncurses)
except:
raise Exception("UCS_CONFIGURE: There was an error configuring the NCurses wrapper using the library "+wrapper_ncurses+".")
else:
if wrapper_pdcurses == UCS_DEFAULT_WRAPPER:
NCURSES = False
try:
pdlib = ctypes.CDLL("pdcurses.dll")
except:
raise Exception("UCS_CONFIGURE: There was an error configuring the default PDCurses wrapper, make sure pdcurses.dll is available in the same folder as UniCurses.")
else:
NCURSES = False
try:
pdlib = ctypes.CDLL(wrapper_pdcurses)
except:
raise Exception("UCS_CONFIGURE: There was an error configuring the PDCurses wrappr using th library "+wrapper_pdcurses+".")
# Return a bytes-encoded C style string from anything that's convertable with str.
# It is used to pass strings to PDCurses which expects a C-formatted string.
def CSTR(s):
return str(s).encode(code)
# Choose a color pair
def PD_COLOR_PAIR(n):
return (n << PDC_COLOR_SHIFT) & PDC_A_COLOR
# Pair number from curses.h
def PD_PAIR_NUMBER(n):
return (n & PDC_A_COLOR) >> PDC_COLOR_SHIFT
# Get the PDC curscr (NOT PORTABLE!)
def PD_GET_CURSCR():
return ctypes.c_int.in_dll(pdlib, "curscr")
# --- PDCurses/NCurses curses.h macro wrappers and other prereqs ---
# +++ CONSTANTS: PDCurses curses.h +++
if not NCURSES:
PDC_COLOR_SHIFT = 24
PDC_ATTR_SHIFT = 19
PDC_A_NORMAL = 0
PDC_A_ALTCHARSET = 0x00010000
PDC_A_BLINK = 0x00400000
PDC_A_BOLD = 0x00800000
PDC_A_COLOR = 0xff000000
PDC_A_DIM = 0
PDC_A_NORMAL = 0
PDC_A_REVERSE = 0x00200000
PDC_A_UNDERLINE = 0x00100000
PDC_A_STANDOUT = (PDC_A_REVERSE | PDC_A_BOLD)
PDC_A_RIGHTLINE = 0x00020000
PDC_A_LEFTLINE = 0x00040000
PDC_A_INVIS = 0x00080000
PDC_A_ATTRIBUTES = 0xffff0000
PDC_A_CHARTEXT = 0x0000ffff
PDC_A_COLOR = 0xff000000
PDC_A_ITALIC = PDC_A_INVIS
PDC_A_PROTECT = (PDC_A_UNDERLINE | PDC_A_LEFTLINE | PDC_A_RIGHTLINE)
# Key mapping (PDC)
if not NCURSES:
PDC_KEY_CODE_YES = 0x100 # If get_wch() gives a key code
PDC_KEY_BREAK = 0x101 # Not on PC KBD
PDC_KEY_DOWN = 0x102 # Down arrow key
PDC_KEY_UP = 0x103 # Up arrow key
PDC_KEY_LEFT = 0x104 # Left arrow key
PDC_KEY_RIGHT = 0x105 # Right arrow key
PDC_KEY_HOME = 0x106 # home key
PDC_KEY_BACKSPACE= 0x107 # not on pc
PDC_KEY_F0 = 0x108 # function keys; 64 reserved
PDC_KEY_DL = 0x148 # delete line
PDC_KEY_IL = 0x149 # insert line
PDC_KEY_DC = 0x14a # delete character
PDC_KEY_IC = 0x14b # insert char or enter ins mode
PDC_KEY_EIC = 0x14c # exit insert char mode
PDC_KEY_CLEAR = 0x14d # clear screen
PDC_KEY_EOS = 0x14e # clear to end of screen
PDC_KEY_EOL = 0x14f # clear to end of line
PDC_KEY_SF = 0x150 # scroll 1 line forward
PDC_KEY_SR = 0x151 # scroll 1 line back (reverse)
PDC_KEY_NPAGE = 0x152 # next page
PDC_KEY_PPAGE = 0x153 # previous page
PDC_KEY_STAB = 0x154 # set tab
PDC_KEY_CTAB = 0x155 # clear tab
PDC_KEY_CATAB = 0x156 # clear all tabs
PDC_KEY_ENTER = 0x157 # enter or send (unreliable)
PDC_KEY_SRESET = 0x158 # soft/reset (partial/unreliable)
PDC_KEY_RESET = 0x159 # reset/hard reset (unreliable)
PDC_KEY_PRINT = 0x15a # print/copy
PDC_KEY_LL = 0x15b # home down/bottom (lower left)
PDC_KEY_ABORT = 0x15c # abort/terminate key (any)
PDC_KEY_SHELP = 0x15d # short help
PDC_KEY_LHELP = 0x15e # long help
PDC_KEY_BTAB = 0x15f # Back tab key
PDC_KEY_BEG = 0x160 # beg(inning) key
PDC_KEY_CANCEL = 0x161 # cancel key
PDC_KEY_CLOSE = 0x162 # close key
PDC_KEY_COMMAND = 0x163 # cmd (command) key
PDC_KEY_COPY = 0x164 # copy key
PDC_KEY_CREATE = 0x165 # create key
PDC_KEY_END = 0x166 # end key
PDC_KEY_EXIT = 0x167 # exit key
PDC_KEY_FIND = 0x168 # find key
PDC_KEY_HELP = 0x169 # help key
PDC_KEY_MARK = 0x16a # mark key
PDC_KEY_MESSAGE = 0x16b # message key
PDC_KEY_MOVE = 0x16c # move key
PDC_KEY_NEXT = 0x16d # next object key
PDC_KEY_OPEN = 0x16e # open key
PDC_KEY_OPTIONS = 0x16f # options key
PDC_KEY_PREVIOUS = 0x170 # previous object key
PDC_KEY_REDO = 0x171 # redo key
PDC_KEY_REFERENCE= 0x172 # ref(erence) key
PDC_KEY_REFRESH = 0x173 # refresh key
PDC_KEY_REPLACE = 0x174 # replace key
PDC_KEY_RESTART = 0x175 # restart key
PDC_KEY_RESUME = 0x176 # resume key
PDC_KEY_SAVE = 0x177 # save key
PDC_KEY_SBEG = 0x178 # shifted beginning key
PDC_KEY_SCANCEL = 0x179 # shifted cancel key
PDC_KEY_SCOMMAND = 0x17a # shifted command key
PDC_KEY_SCOPY = 0x17b # shifted copy key
PDC_KEY_SCREATE = 0x17c # shifted create key
PDC_KEY_SDC = 0x17d # shifted delete char key
PDC_KEY_SDL = 0x17e # shifted delete line key
PDC_KEY_SELECT = 0x17f # select key
PDC_KEY_SEND = 0x180 # shifted end key
PDC_KEY_SEOL = 0x181 # shifted clear line key
PDC_KEY_SEXIT = 0x182 # shifted exit key
PDC_KEY_SFIND = 0x183 # shifted find key
PDC_KEY_SHOME = 0x184 # shifted home key
PDC_KEY_SIC = 0x185 # shifted input key
PDC_KEY_SLEFT = 0x187 # shifted left arrow key
PDC_KEY_SMESSAGE = 0x188 # shifted message key
PDC_KEY_SMOVE = 0x189 # shifted move key
PDC_KEY_SNEXT = 0x18a # shifted next key
PDC_KEY_SOPTIONS = 0x18b # shifted options key
PDC_KEY_SPREVIOUS= 0x18c # shifted prev key
PDC_KEY_SPRINT = 0x18d # shifted print key
PDC_KEY_SREDO = 0x18e # shifted redo key
PDC_KEY_SREPLACE = 0x18f # shifted replace key
PDC_KEY_SRIGHT = 0x190 # shifted right arrow
PDC_KEY_SRSUME = 0x191 # shifted resume key
PDC_KEY_SSAVE = 0x192 # shifted save key
PDC_KEY_SSUSPEND = 0x193 # shifted suspend key
PDC_KEY_SUNDO = 0x194 # shifted undo key
PDC_KEY_SUSPEND = 0x195 # suspend key
PDC_KEY_UNDO = 0x196 # undo key
PDC_KEY_A1 = 0x1c1
PDC_KEY_A3 = 0x1c3
PDC_KEY_B2 = 0x1c5
PDC_KEY_C1 = 0x1c7
PDC_KEY_C3 = 0x1c9
PDC_KEY_MOUSE = 0x21b
PDC_KEY_RESIZE = 0x222
# Mouse mapping (PDC)
if not NCURSES:
PDC_BUTTON1_RELEASED = 0x00000001
PDC_BUTTON1_PRESSED = 0x00000002
PDC_BUTTON1_CLICKED = 0x00000004
PDC_BUTTON1_DOUBLE_CLICKED = 0x00000008
PDC_BUTTON1_TRIPLE_CLICKED = 0x00000010
PDC_BUTTON2_RELEASED = 0x00000020
PDC_BUTTON2_PRESSED = 0x00000040
PDC_BUTTON2_CLICKED = 0x00000080
PDC_BUTTON2_DOUBLE_CLICKED = 0x00000100
PDC_BUTTON2_TRIPLE_CLICKED = 0x00000200
PDC_BUTTON3_RELEASED = 0x00000400
PDC_BUTTON3_PRESSED = 0x00000800
PDC_BUTTON3_CLICKED = 0x00001000
PDC_BUTTON3_DOUBLE_CLICKED = 0x00002000
PDC_BUTTON3_TRIPLE_CLICKED = 0x00004000
PDC_BUTTON4_RELEASED = 0x00008000
PDC_BUTTON4_PRESSED = 0x00010000
PDC_BUTTON4_CLICKED = 0x00020000
PDC_BUTTON4_DOUBLE_CLICKED = 0x00040000
PDC_BUTTON4_TRIPLE_CLICKED = 0x00080000
PDC_BUTTON_SHIFT = 0x04000000
PDC_BUTTON_CTRL = 0x08000000
PDC_BUTTON_ALT = 0x10000000
PDC_ALL_MOUSE_EVENTS = 0x1fffffff
PDC_REPORT_MOUSE_POSITION = 0x20000000
# +++ CONSTANTS: Platform-independent +++
# General
OK = 0
ERR = -1
# Attributes
if NCURSES:
A_ALTCHARSET = curses.A_ALTCHARSET
A_BLINK = curses.A_BLINK
A_BOLD = curses.A_BOLD
A_DIM = curses.A_DIM
A_NORMAL = curses.A_NORMAL
A_STANDOUT = curses.A_STANDOUT
A_UNDERLINE = curses.A_UNDERLINE
A_REVERSE = curses.A_REVERSE
A_PROTECT = curses.A_PROTECT
A_ATTRIBUTES = curses.A_ATTRIBUTES
A_COLOR = curses.A_COLOR
A_CHARTEXT = curses.A_CHARTEXT
try:
A_INVIS = curses.A_INVIS
except:
A_INVIS = A_NORMAL
else:
A_ALTCHARSET = PDC_A_ALTCHARSET
A_BLINK = PDC_A_BLINK
A_BOLD = PDC_A_BOLD
A_DIM = PDC_A_DIM
A_NORMAL = PDC_A_NORMAL
A_STANDOUT = PDC_A_STANDOUT
A_UNDERLINE = PDC_A_UNDERLINE
A_REVERSE = PDC_A_REVERSE
A_PROTECT = PDC_A_PROTECT
A_ATTRIBUTES = PDC_A_ATTRIBUTES
A_INVIS = PDC_A_INVIS
A_COLOR = PDC_A_COLOR
A_CHARTEXT = PDC_A_CHARTEXT
# Colors
if NCURSES:
COLOR_BLACK=curses.COLOR_BLACK
COLOR_BLUE=curses.COLOR_BLUE
COLOR_CYAN=curses.COLOR_CYAN
COLOR_GREEN=curses.COLOR_GREEN
COLOR_MAGENTA=curses.COLOR_MAGENTA
COLOR_RED=curses.COLOR_RED
COLOR_WHITE=curses.COLOR_WHITE
COLOR_YELLOW=curses.COLOR_YELLOW
else:
COLOR_BLACK = 0
COLOR_BLUE = 1
COLOR_GREEN = 2
COLOR_RED = 4
COLOR_CYAN = (COLOR_BLUE | COLOR_GREEN)
COLOR_MAGENTA = (COLOR_RED | COLOR_BLUE)
COLOR_YELLOW = (COLOR_RED | COLOR_GREEN)
COLOR_WHITE = 7
# Get a C character
def CCHAR(ch):
if type(ch)==str:
return ord(ch)
elif type(ch)==int:
return ch
else:
raise Exception("CCHAR: can't parse a non-char/non-int value.")
# Alternate character set
def ALTCHAR(ch):
if type(ch)==str:
return ord(ch) | A_ALTCHARSET
elif type(ch)==int:
return ch | A_ALTCHARSET
else:
raise Exception("ALTCHAR: can't parse a non-char/non-int value.")
# ACS Alternate Character Set Symbols
ACS_ULCORNER = ALTCHAR('l')
ACS_LLCORNER = ALTCHAR('m')
ACS_URCORNER = ALTCHAR('k')
ACS_LRCORNER = ALTCHAR('j')
ACS_LTEE = ALTCHAR('t')
ACS_RTEE = ALTCHAR('u')
ACS_BTEE = ALTCHAR('v')
ACS_TTEE = ALTCHAR('w')
ACS_HLINE = ALTCHAR('q')
ACS_VLINE = ALTCHAR('x')
ACS_PLUS = ALTCHAR('n')
ACS_S1 = ALTCHAR('o')
ACS_S9 = ALTCHAR('s')
ACS_DIAMOND = ALTCHAR('`')
ACS_CKBOARD = ALTCHAR('a')
ACS_DEGREE = ALTCHAR('f')
ACS_PLMINUS = ALTCHAR('g')
ACS_BULLET = ALTCHAR('~')
ACS_LARROW = ALTCHAR(',')
ACS_RARROW = ALTCHAR('+')
ACS_DARROW = ALTCHAR('.')
ACS_UARROW = ALTCHAR('-')
ACS_BOARD = ALTCHAR('h')
ACS_LANTERN = ALTCHAR('i')
ACS_BLOCK = ALTCHAR('0')
ACS_S3 = ALTCHAR('p')
ACS_S7 = ALTCHAR('r')
ACS_LEQUAL = ALTCHAR('y')
ACS_GEQUAL = ALTCHAR('z')
ACS_PI = ALTCHAR('{')
ACS_NEQUAL = ALTCHAR('|')
ACS_STERLING = ALTCHAR('}')
ACS_BSSB = ACS_ULCORNER
ACS_SSBB = ACS_LLCORNER
ACS_BBSS = ACS_URCORNER
ACS_SBBS = ACS_LRCORNER
ACS_SBSS = ACS_RTEE
ACS_SSSB = ACS_LTEE
ACS_SSBS = ACS_BTEE
ACS_BSSS = ACS_TTEE
ACS_BSBS = ACS_HLINE
ACS_SBSB = ACS_VLINE
ACS_SSSS = ACS_PLUS
# Unicurses-specific: pseudographic mode
SCS_ULCORNER = CCHAR('+')
SCS_LLCORNER = CCHAR('+')
SCS_URCORNER = CCHAR('+')
SCS_LRCORNER = CCHAR('+')
SCS_LTEE = CCHAR('+')
SCS_RTEE = CCHAR('+')
SCS_BTEE = CCHAR('+')
SCS_TTEE = CCHAR('+')
SCS_HLINE = CCHAR('-')
SCS_VLINE = CCHAR('|')
SCS_PLUS = CCHAR('+')
SCS_S1 = CCHAR('-')
SCS_S9 = CCHAR('_')
SCS_DIAMOND = CCHAR('+')
SCS_CKBOARD = CCHAR(':')
SCS_DEGREE = CCHAR('\\')
SCS_PLMINUS = CCHAR('#')
SCS_BULLET = CCHAR('o')
SCS_LARROW = CCHAR('<')
SCS_RARROW = CCHAR('>')
SCS_DARROW = CCHAR('v')
SCS_UARROW = CCHAR('^')
SCS_BOARD = CCHAR('#')
SCS_LANTERN = CCHAR('*')
SCS_BLOCK = CCHAR('#')
SCS_S3 = CCHAR('-')
SCS_S7 = CCHAR('-')
SCS_LEQUAL = CCHAR('<')
SCS_GEQUAL = CCHAR('>')
SCS_PI = CCHAR('n')
SCS_NEQUAL = CCHAR('+')
SCS_STERLING = CCHAR('L')
SCS_BSSB = ACS_ULCORNER
SCS_SSBB = ACS_LLCORNER
SCS_BBSS = ACS_URCORNER
SCS_SBBS = ACS_LRCORNER
SCS_SBSS = ACS_RTEE
SCS_SSSB = ACS_LTEE
SCS_SSBS = ACS_BTEE
SCS_BSSS = ACS_TTEE
SCS_BSBS = ACS_HLINE
SCS_SBSB = ACS_VLINE
SCS_SSSS = ACS_PLUS
# Keymap
if NCURSES:
# KEY_CODE_YES = curses.KEY_CODE_YES
KEY_MIN = curses.KEY_MIN
KEY_BREAK = curses.KEY_BREAK
KEY_SRESET = curses.KEY_SRESET
KEY_RESET = curses.KEY_RESET
KEY_DOWN = curses.KEY_DOWN
KEY_UP = curses.KEY_UP
KEY_LEFT = curses.KEY_LEFT
KEY_RIGHT = curses.KEY_RIGHT
KEY_HOME = curses.KEY_HOME
KEY_BACKSPACE= curses.KEY_BACKSPACE
KEY_F0 = curses.KEY_F0
KEY_DL = curses.KEY_DL
KEY_IL = curses.KEY_IL
KEY_DC = curses.KEY_DC
KEY_IC = curses.KEY_IC
KEY_EIC = curses.KEY_EIC
KEY_CLEAR = curses.KEY_CLEAR
KEY_EOS = curses.KEY_EOS
KEY_EOL = curses.KEY_EOL
KEY_SF = curses.KEY_SF
KEY_SR = curses.KEY_SR
KEY_NPAGE = curses.KEY_NPAGE
KEY_PPAGE = curses.KEY_PPAGE
KEY_STAB = curses.KEY_STAB
KEY_CTAB = curses.KEY_CTAB
KEY_CATAB = curses.KEY_CATAB
KEY_ENTER = curses.KEY_ENTER
KEY_PRINT = curses.KEY_PRINT
KEY_LL = curses.KEY_LL
KEY_A1 = curses.KEY_A1
KEY_A3 = curses.KEY_A3
KEY_B2 = curses.KEY_B2
KEY_C1 = curses.KEY_C1
KEY_C3 = curses.KEY_C3
KEY_BTAB = curses.KEY_BTAB
KEY_BEG = curses.KEY_BEG
KEY_CANCEL = curses.KEY_CANCEL
KEY_CLOSE = curses.KEY_CLOSE
KEY_COMMAND = curses.KEY_COMMAND
KEY_COPY = curses.KEY_COPY
KEY_CREATE = curses.KEY_CREATE
KEY_END = curses.KEY_END
KEY_EXIT = curses.KEY_EXIT
KEY_FIND = curses.KEY_FIND
KEY_HELP = curses.KEY_HELP
KEY_MARK = curses.KEY_MARK
KEY_MESSAGE = curses.KEY_MESSAGE
KEY_MOVE = curses.KEY_MOVE
KEY_NEXT = curses.KEY_NEXT
KEY_OPEN = curses.KEY_OPEN
KEY_OPTIONS = curses.KEY_OPTIONS
KEY_PREVIOUS = curses.KEY_PREVIOUS
KEY_REDO = curses.KEY_REDO
KEY_REFERENCE= curses.KEY_REFERENCE
KEY_REFRESH = curses.KEY_REFRESH
KEY_REPLACE = curses.KEY_REPLACE
KEY_RESTART = curses.KEY_RESTART
KEY_RESUME = curses.KEY_RESUME
KEY_SAVE = curses.KEY_SAVE
KEY_SBEG = curses.KEY_SBEG
KEY_SCANCEL = curses.KEY_SCANCEL
KEY_SCOMMAND = curses.KEY_SCOMMAND
KEY_SCOPY = curses.KEY_SCOPY
KEY_SCREATE = curses.KEY_SCREATE
KEY_SDC = curses.KEY_SDC
KEY_SDL = curses.KEY_SDL
KEY_SELECT = curses.KEY_SELECT
KEY_SEND = curses.KEY_SEND
KEY_SEOL = curses.KEY_SEOL
KEY_SEXIT = curses.KEY_SEXIT
KEY_SFIND = curses.KEY_SFIND
KEY_SHELP = curses.KEY_SHELP
KEY_SHOME = curses.KEY_SHOME
KEY_SIC = curses.KEY_SIC
KEY_SLEFT = curses.KEY_SLEFT
KEY_SMESSAGE = curses.KEY_SMESSAGE
KEY_SMOVE = curses.KEY_SMOVE
KEY_SNEXT = curses.KEY_SNEXT
KEY_SOPTIONS = curses.KEY_SOPTIONS
KEY_SPREVIOUS= curses.KEY_SPREVIOUS
KEY_SPRINT = curses.KEY_SPRINT
KEY_SREDO = curses.KEY_SREDO
KEY_SREPLACE = curses.KEY_SREPLACE
KEY_SRIGHT = curses.KEY_SRIGHT
KEY_SRSUME = curses.KEY_SRSUME
KEY_SSAVE = curses.KEY_SSAVE
KEY_SSUSPEND = curses.KEY_SSUSPEND
KEY_SUNDO = curses.KEY_SUNDO
KEY_SUSPEND = curses.KEY_SUSPEND
KEY_UNDO = curses.KEY_UNDO
KEY_MOUSE = curses.KEY_MOUSE
KEY_RESIZE = curses.KEY_RESIZE
# KEY_EVENT = curses.KEY_EVENT
KEY_MAX = curses.KEY_MAX
else:
# KEY_CODE_YES = PDC_KEY_CODE_YES
KEY_MIN = PDC_KEY_BREAK
KEY_BREAK = PDC_KEY_BREAK
KEY_SRESET = PDC_KEY_SRESET
KEY_RESET = PDC_KEY_RESET
KEY_DOWN = PDC_KEY_DOWN
KEY_UP = PDC_KEY_UP
KEY_LEFT = PDC_KEY_LEFT
KEY_RIGHT = PDC_KEY_RIGHT
KEY_HOME = PDC_KEY_HOME
KEY_BACKSPACE= PDC_KEY_BACKSPACE
KEY_F0 = PDC_KEY_F0
KEY_DL = PDC_KEY_DL
KEY_IL = PDC_KEY_IL
KEY_DC = PDC_KEY_DC
KEY_IC = PDC_KEY_IC
KEY_EIC = PDC_KEY_EIC
KEY_CLEAR = PDC_KEY_CLEAR
KEY_EOS = PDC_KEY_EOS
KEY_EOL = PDC_KEY_EOL
KEY_SF = PDC_KEY_SF
KEY_SR = PDC_KEY_SR
KEY_NPAGE = PDC_KEY_NPAGE
KEY_PPAGE = PDC_KEY_PPAGE
KEY_STAB = PDC_KEY_STAB
KEY_CTAB = PDC_KEY_CTAB
KEY_CATAB = PDC_KEY_CATAB
KEY_ENTER = PDC_KEY_ENTER
KEY_PRINT = PDC_KEY_PRINT
KEY_LL = PDC_KEY_LL
KEY_A1 = PDC_KEY_A1
KEY_A3 = PDC_KEY_A3
KEY_B2 = PDC_KEY_B2
KEY_C1 = PDC_KEY_C1
KEY_C3 = PDC_KEY_C3
KEY_BTAB = PDC_KEY_BTAB
KEY_BEG = PDC_KEY_BEG
KEY_CANCEL = PDC_KEY_CANCEL
KEY_CLOSE = PDC_KEY_CLOSE
KEY_COMMAND = PDC_KEY_COMMAND
KEY_COPY = PDC_KEY_COPY
KEY_CREATE = PDC_KEY_CREATE
KEY_END = PDC_KEY_END
KEY_EXIT = PDC_KEY_EXIT
KEY_FIND = PDC_KEY_FIND
KEY_HELP = PDC_KEY_HELP
KEY_MARK = PDC_KEY_MARK
KEY_MESSAGE = PDC_KEY_MESSAGE
KEY_MOVE = PDC_KEY_MOVE
KEY_NEXT = PDC_KEY_NEXT
KEY_OPEN = PDC_KEY_OPEN
KEY_OPTIONS = PDC_KEY_OPTIONS
KEY_PREVIOUS = PDC_KEY_PREVIOUS
KEY_REDO = PDC_KEY_REDO
KEY_REFERENCE= PDC_KEY_REFERENCE
KEY_REFRESH = PDC_KEY_REFRESH
KEY_REPLACE = PDC_KEY_REPLACE
KEY_RESTART = PDC_KEY_RESTART
KEY_RESUME = PDC_KEY_RESUME
KEY_SAVE = PDC_KEY_SAVE
KEY_SBEG = PDC_KEY_SBEG
KEY_SCANCEL = PDC_KEY_SCANCEL
KEY_SCOMMAND = PDC_KEY_SCOMMAND
KEY_SCOPY = PDC_KEY_SCOPY
KEY_SCREATE = PDC_KEY_SCREATE
KEY_SDC = PDC_KEY_SDC
KEY_SDL = PDC_KEY_SDL
KEY_SELECT = PDC_KEY_SELECT
KEY_SEND = PDC_KEY_SEND
KEY_SEOL = PDC_KEY_SEOL
KEY_SEXIT = PDC_KEY_SEXIT
KEY_SFIND = PDC_KEY_SFIND
KEY_SHELP = PDC_KEY_SHELP
KEY_SHOME = PDC_KEY_SHOME
KEY_SIC = PDC_KEY_SIC
KEY_SLEFT = PDC_KEY_SLEFT
KEY_SMESSAGE = PDC_KEY_SMESSAGE
KEY_SMOVE = PDC_KEY_SMOVE
KEY_SNEXT = PDC_KEY_SNEXT
KEY_SOPTIONS = PDC_KEY_SOPTIONS
KEY_SPREVIOUS= PDC_KEY_SPREVIOUS
KEY_SPRINT = PDC_KEY_SPRINT
KEY_SREDO = PDC_KEY_SREDO
KEY_SREPLACE = PDC_KEY_SREPLACE
KEY_SRIGHT = PDC_KEY_SRIGHT
KEY_SRSUME = PDC_KEY_SRSUME
KEY_SSAVE = PDC_KEY_SSAVE
KEY_SSUSPEND = PDC_KEY_SSUSPEND
KEY_SUNDO = PDC_KEY_SUNDO
KEY_SUSPEND = PDC_KEY_SUSPEND
KEY_UNDO = PDC_KEY_UNDO
KEY_MOUSE = PDC_KEY_MOUSE
KEY_RESIZE = PDC_KEY_RESIZE
# KEY_EVENT = PDC_KEY_EVENT
KEY_MAX = 0x224 # == KEY_SDOWN, take that into account
def KEY_F(n): return KEY_F0 + n # function keys 1-64
# Mouse mapping
if NCURSES:
BUTTON1_RELEASED = curses.BUTTON1_RELEASED
BUTTON1_PRESSED = curses.BUTTON1_PRESSED
BUTTON1_CLICKED = curses.BUTTON1_CLICKED
BUTTON1_DOUBLE_CLICKED = curses.BUTTON1_DOUBLE_CLICKED
BUTTON1_TRIPLE_CLICKED = curses.BUTTON1_TRIPLE_CLICKED
BUTTON2_RELEASED = curses.BUTTON2_RELEASED
BUTTON2_PRESSED = curses.BUTTON2_PRESSED
BUTTON2_CLICKED = curses.BUTTON2_CLICKED
BUTTON2_DOUBLE_CLICKED = curses.BUTTON2_DOUBLE_CLICKED
BUTTON2_TRIPLE_CLICKED = curses.BUTTON2_TRIPLE_CLICKED
BUTTON3_RELEASED = curses.BUTTON3_RELEASED
BUTTON3_PRESSED = curses.BUTTON3_PRESSED
BUTTON3_CLICKED = curses.BUTTON3_CLICKED
BUTTON3_DOUBLE_CLICKED = curses.BUTTON3_DOUBLE_CLICKED
BUTTON3_TRIPLE_CLICKED = curses.BUTTON3_TRIPLE_CLICKED
BUTTON4_RELEASED = curses.BUTTON4_RELEASED
BUTTON4_PRESSED = curses.BUTTON4_PRESSED
BUTTON4_CLICKED = curses.BUTTON4_CLICKED
BUTTON4_DOUBLE_CLICKED = curses.BUTTON4_DOUBLE_CLICKED
BUTTON4_TRIPLE_CLICKED = curses.BUTTON4_TRIPLE_CLICKED
BUTTON_SHIFT = curses.BUTTON_SHIFT
BUTTON_CTRL = curses.BUTTON_CTRL
BUTTON_ALT = curses.BUTTON_ALT
ALL_MOUSE_EVENTS = curses.ALL_MOUSE_EVENTS
REPORT_MOUSE_POSITION = curses.REPORT_MOUSE_POSITION
else:
BUTTON1_RELEASED = PDC_BUTTON1_RELEASED
BUTTON1_PRESSED = PDC_BUTTON1_PRESSED
BUTTON1_CLICKED = PDC_BUTTON1_CLICKED
BUTTON1_DOUBLE_CLICKED = PDC_BUTTON1_DOUBLE_CLICKED
BUTTON1_TRIPLE_CLICKED = PDC_BUTTON1_TRIPLE_CLICKED
BUTTON2_RELEASED = PDC_BUTTON2_RELEASED
BUTTON2_PRESSED = PDC_BUTTON2_PRESSED
BUTTON2_CLICKED = PDC_BUTTON2_CLICKED
BUTTON2_DOUBLE_CLICKED = PDC_BUTTON2_DOUBLE_CLICKED
BUTTON2_TRIPLE_CLICKED = PDC_BUTTON2_TRIPLE_CLICKED
BUTTON3_RELEASED = PDC_BUTTON3_RELEASED
BUTTON3_PRESSED = PDC_BUTTON3_PRESSED
BUTTON3_CLICKED = PDC_BUTTON3_CLICKED
BUTTON3_DOUBLE_CLICKED = PDC_BUTTON3_DOUBLE_CLICKED
BUTTON3_TRIPLE_CLICKED = PDC_BUTTON3_TRIPLE_CLICKED
BUTTON4_RELEASED = PDC_BUTTON4_RELEASED
BUTTON4_PRESSED = PDC_BUTTON4_PRESSED
BUTTON4_CLICKED = PDC_BUTTON4_CLICKED
BUTTON4_DOUBLE_CLICKED = PDC_BUTTON4_DOUBLE_CLICKED
BUTTON4_TRIPLE_CLICKED = PDC_BUTTON4_TRIPLE_CLICKED
BUTTON_SHIFT = PDC_BUTTON_SHIFT
BUTTON_CTRL = PDC_BUTTON_CTRL
BUTTON_ALT = PDC_BUTTON_ALT
ALL_MOUSE_EVENTS = PDC_ALL_MOUSE_EVENTS
REPORT_MOUSE_POSITION = PDC_REPORT_MOUSE_POSITION
# --- CONSTANTS ---
# +++ FUNCTION DEFINITIONS (PDC) +++
if not NCURSES:
pdlib.erasechar.restype = ctypes.c_char
pdlib.keyname.restype = ctypes.c_char_p
pdlib.killchar.restype = ctypes.c_char
pdlib.longname.restype = ctypes.c_char_p
pdlib.nc_getmouse.restype = MEVENT
pdlib.termattrs.restype = ctypes.c_ulong
pdlib.termname.restype = ctypes.c_char_p
pdlib.winch.restype = ctypes.c_uint
pdlib.mvwinch.restype = ctypes.c_uint
pdlib.unctrl.restype = ctypes.c_char_p
pdlib.is_wintouched.restype = ctypes.c_bool
pdlib.is_linetouched.restype = ctypes.c_bool
pdlib.can_change_color.restype = ctypes.c_bool
pdlib.has_colors.restype = ctypes.c_bool
pdlib.has_ic.restype = ctypes.c_bool
pdlib.has_il.restype = ctypes.c_bool
pdlib.has_key.restype = ctypes.c_bool
pdlib.isendwin.restype = ctypes.c_bool
# --- FUNCTION DEFINITIONS (PDC) ---
# +++ UNIFIED CURSES +++
# functions
def waddch(scr_id, ch, attr=A_NORMAL):
if NCURSES:
try:
return scr_id.addch(ch, attr)
except curses.error:
return ERR
else:
return pdlib.waddch(scr_id, ch | attr)
def waddstr(scr_id, cstr, attr="NO_USE"):
if NCURSES:
try:
if (attr != "NO_USE"): return scr_id.addstr(str(cstr), attr)
return scr_id.addstr(str(cstr))
except curses.error:
return ERR
else:
if (attr != "NO_USE"): oldattr = pdlib.getattrs(scr_id); pdlib.wattrset(scr_id, attr)
ret = pdlib.waddstr(scr_id, CSTR(cstr))
if (attr != "NO_USE"): pdlib.wattrset(scr_id, oldattr)
return ret
def waddnstr(scr_id, cstr, n, attr="NO_USE"):
if NCURSES:
try:
if (attr != "NO_USE"): return scr_id.addnstr(str(cstr), n, int(attr))
return scr_id.addnstr(str(cstr), n)
except curses.error:
return ERR
else:
if (attr != "NO_USE"): oldattr = pdlib.getattrs(scr_id); pdlib.wattrset(scr_id, attr)
ret = pdlib.waddnstr(scr_id, CSTR(cstr), n)
if (attr != "NO_USE"): pdlib.wattrset(scr_id, oldattr)
return ret
def wattroff(scr_id, attr):
if NCURSES:
try:
return scr_id.attroff(attr)
except curses.error:
return ERR
else:
return pdlib.wattroff(scr_id, attr)
def wattron(scr_id, attr):
if NCURSES:
try:
return scr_id.attron(attr)
except curses.error:
return ERR
else:
return pdlib.wattron(scr_id, attr)
def wattrset(scr_id, attr):
if NCURSES:
try:
return scr_id.attrset(attr)
except curses.error:
return ERR
else:
return pdlib.wattrset(scr_id, attr)
def baudrate():
if NCURSES:
try:
return curses.baudrate()
except curses.error:
return ERR
else:
return pdlib.baudrate()
def beep():
if NCURSES:
try:
return curses.beep()
except curses.error:
return ERR
else:
return pdlib.beep()
def wbkgd(scr_id, ch, attr=A_NORMAL):
if NCURSES:
try:
return scr_id.bkgd(ch, attr)
except curses.error:
return ERR
else:
return pdlib.wbkgd(scr_id, ch | attr)
def wbkgdset(scr_id, ch, attr=A_NORMAL):
if NCURSES:
try:
return scr_id.bkgdset(ch, attr)
except curses.error:
return ERR
else:
return pdlib.wbkgdset(scr_id, ch | attr)
def wborder(scr_id, ls=ACS_VLINE, rs=ACS_VLINE, ts=ACS_HLINE, bs=ACS_HLINE, tl=ACS_ULCORNER, tr=ACS_URCORNER, bl=ACS_LLCORNER, br=ACS_LRCORNER):
if NCURSES:
try:
return scr_id.border(ls, rs, ts, bs, tl, tr, bl, br)
except curses.error:
return ERR
else:
return pdlib.wborder(scr_id, ls, rs, ts, bs, tl, tr, bl, br)
def box(scr_id, verch=ACS_VLINE, horch=ACS_HLINE):
if NCURSES:
try:
return scr_id.box(verch, horch)
except curses.error:
return ERR
else:
return pdlib.box(scr_id, verch, horch)
def can_change_color():
if NCURSES:
try:
return curses.can_change_color()
except curses.error:
return ERR
else:
return (pdlib.can_change_color() == 1)
def cbreak():
if NCURSES:
try:
return curses.cbreak()
except curses.error:
return ERR
else:
return pdlib.cbreak()
def wchgat(scr_id, num, attr, color, opts=None):
if NCURSES:
try:
return scr_id.chgat(num, attr | color_pair(color))
except curses.error:
return ERR
else:
return pdlib.wchgat(scr_id, num, attr, color, None)
def color_content(color_number):
if NCURSES:
try:
return curses.color_content(color_number)
except curses.error:
return ERR
else:
r = ctypes.c_short()
g = ctypes.c_short()
b = ctypes.c_short()
pdlib.color_content(color_number, ctypes.byref(r), ctypes.byref(g), ctypes.byref(b))
return (r.value, g.value, b.value)
def color_pair(color_number):
if NCURSES:
try:
return curses.color_pair(color_number)
except curses.error:
return ERR
else:
return PD_COLOR_PAIR(color_number)
def COLOR_PAIR(n): return color_pair(n)
def copywin(src_id, dest_id, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol, overlay):
if NCURSES:
try:
if overlay:
return src_id.overlay(dest_id, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol)
else:
return src_id.overwrite(dest_id, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol)
except curses.error:
return ERR
else:
return pdlib.copywin(src_id, dest_id, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol, overlay)
def wclear(scr_id):
if NCURSES:
try:
return scr_id.clear()
except curses.error:
return ERR
else:
return pdlib.wclear(scr_id)
def wclrtobot(scr_id):
if NCURSES:
try:
return scr_id.clrtobot()
except curses.error:
return ERR
else:
return pdlib.wclrtobot(scr_id)
def wclrtoeol(scr_id):
if NCURSES:
try:
return scr_id.clrtoeol()
except curses.error:
return ERR
else:
return pdlib.wclrtoeol(scr_id)
def clearok(scr_id, yes):
if NCURSES:
try:
return scr_id.clearok(yes)
except curses.error:
return ERR
else:
return pdlib.clearok(scr_id, yes)
def curs_set(visibility):
if NCURSES:
try:
return curses.curs_set(visibility)
except curses.error:
return ERR
else:
return pdlib.curs_set(visibility)
def cursyncup(scr_id):
if NCURSES:
try:
return scr_id.cursyncup()
except curses.error:
return ERR
else:
return pdlib.wcursyncup(scr_id)
def def_prog_mode():
if NCURSES:
try:
return curses.def_prog_mode()
except curses.error:
return ERR
else:
return pdlib.def_prog_mode()
def def_shell_mode():
if NCURSES:
try:
return curses.def_shell_mode()
except curses.error:
return ERR