-
Notifications
You must be signed in to change notification settings - Fork 19
/
__init__.py
1970 lines (1345 loc) · 52.1 KB
/
__init__.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
# -*- coding: utf-8 -*-
# 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/>.
# CONTRIBUTOR https://github.com/GiorgosXou
# import Curses (either natively if supported or via PDCurses using FFI if on MS Windows)
from distutils.version import LooseVersion
from glob import glob
import locale, platform, sys, os, re
global lib1
global NCURSES # Not needed but makes things more visually appealing? it could just be 'if not PDCURSES'
global PDCURSES
global PYCURSES # Not sure if i would want to keep the support for the native python's module-ncurses
global PDC_LEAVEOK
global IS_CURSES_LIBRARY_UTF8
PDC_LEAVEOK = False # LeaveOK emulation in PDC
NCURSES = False # Native curses support
PDCURSES = False # Public Domain Curses support
UCS_DEFAULT_WRAPPER = "" # A constant for the default wrapper (ucs_reconfigure)
IS_CURSES_LIBRARY_UTF8 = True # Determine if the library iscompiled with UTF-8 support
stdscr = -1 # A pointer to the standard screen
lib1 = None # PD\NCurses library (dll/.so)
lib2 = None # For NCurses's panel.so library else lib2=lib1
OPERATING_SYSTEM = platform.system()
locale.setlocale(locale.LC_ALL, '')
code = locale.getpreferredencoding() # TODO: fix this to actually work on native ncurses
#region +++ Main ++
def parse_ld_conf_file(fn):
paths = []
for l in open(fn).read().splitlines():
l = l.strip()
if not l:
continue
if l.startswith("#"):
continue
if l.startswith("include "):
for sub_fn in glob(l[len("include "):]):
paths.extend(parse_ld_conf_file(sub_fn))
continue
paths.append(l)
return paths
def get_libncursesw_paths():
from ctypes.util import find_library
lib_paths = [find_library('ncursesw'),find_library('panelw')]
if not lib_paths[0] or not lib_paths[1]:
msg = ''
if OPERATING_SYSTEM == 'Darwin':
msg = 'No version of shared-libraries of ncurses found on this system, please try `brew install ncurses` if this won\'t work please create an issue'
elif OPERATING_SYSTEM == 'FreeBSD':
msg = 'try `pkg search ncurses` then `pkg install ncurses-X.Y`'
else:
msg = 'No version of shared-libraries of ncurses found on this system, please try installing one\n try pgk\\apt\\etc. search ncurses and then install'
raise Exception('NCursesNotFound: ' + msg)
return lib_paths
try:
import ctypes
except ImportError:
raise ImportError("""
Fatal error: this Python release does not support ctypes.
Please upgrade your Python distribution
if you want to use UniCurses on a {} platform.
""".format(sys.platform))
if OPERATING_SYSTEM == 'Windows':
import platform
if platform.architecture()[0] == '64bit':
pdcurses = "64 bit binaries/pdcdllu/pdcurses.dll" # wide-character (Unicode) & UTF-8
else:
pdcurses = "32 bit binaries/pdcdllu/pdcurses.dll" # wide-character (Unicode) & UTF-8
current_dir = os.path.dirname(os.path.realpath(__file__))
path_to_pdcurses = current_dir + "/" + pdcurses
print("Expecting pdcurses at: " + path_to_pdcurses)
if not (os.access(pdcurses, os.F_OK)
or os.access(path_to_pdcurses, os.F_OK)):
raise ImportError("""
Fatal error: can't find pdcurses.dll for linking.
Make sure PDCurses is in the same folder as UniCurses
if you want to use UniCurses on a {} platform.
""".format(sys.platform))
# We're on winXX, use pdcurses instead of native ncurses
lib2 = lib1 = ctypes.CDLL(path_to_pdcurses)
PDCURSES = True
NCURSES = False
else:
ncurses,panel = get_libncursesw_paths()
lib1 = ctypes.CDLL(ncurses,mode=ctypes.RTLD_GLOBAL)
lib2 = ctypes.CDLL(panel)
PDCURSES = False
NCURSES = True
# 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_pdcurses = None, wrapper_ncurses = None, wrapper_ncurses_panel = None, is_utf8_dll = True):
global IS_CURSES_LIBRARY_UTF8
global lib1
global lib2
IS_CURSES_LIBRARY_UTF8 = is_utf8_dll
if wrapper_ncurses and wrapper_ncurses_panel:
lib1 = ctypes.CDLL(wrapper_ncurses,mode=ctypes.RTLD_GLOBAL)
lib2 = ctypes.CDLL(wrapper_ncurses_panel)
if wrapper_pdcurses:
lib2 = lib1 = ctypes.CDLL(wrapper_pdcurses)
#endregion +++ Main ++
#region +++ PDCurses/NCurses ncurses.h marco wrappers and other prereqs +++
#region ++ STRUCTS ++
# A PDC/NC structure for the mouse events
class MEVENT(ctypes.Structure):
_fields_ = [("id", ctypes.c_short),
("x", ctypes.c_int),
("y", ctypes.c_int),
("z", ctypes.c_int),
("bstate", ctypes.c_ulong)]
# A NC structure for cchar_t
if NCURSES:
CCHARW_MAX = 5
WCHAR_5 = ctypes.c_wchar * CCHARW_MAX
class cchar_t(ctypes.Structure):
_fields_ = [("attr", ctypes.c_int),
("chars", WCHAR_5)]
#endregion -- STRUCTS --
def CSTR(s):
"""
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.
"""
global IS_CURSES_LIBRARY_UTF8
if IS_CURSES_LIBRARY_UTF8:
return str(s).encode()
else:
return str(s).encode(code)
if PDCURSES:
def PD_COLOR_PAIR(n):
"""Choose a color pair"""
return (n << PDC_COLOR_SHIFT) & A_COLOR
def PD_PAIR_NUMBER(n):
"""Pair number from curses.h"""
return (n & A_COLOR) >> PDC_COLOR_SHIFT
def PD_GET_CURSCR():
"""Get the PDC curscr (NOT PORTABLE!)"""
return ctypes.c_int.in_dll(lib1, "curscr")
else:
NCURSES_ATTR_SHIFT = 8
def NCURSES_BITS(mask,shift):
return (mask << ((shift) + NCURSES_ATTR_SHIFT))
def NC_COLOR_PAIR(n):
return (NCURSES_BITS((n), 0) & A_COLOR)
#endregion --- PDCurses/NCurses ncurses.h macro wrappers and other prereqs ---
#region +++ CONSTANTS: PDCurses\Ncurses ncurses.h +++
if PDCURSES:
#region ++ Attributs & Colors (PDC) ++
PDC_COLOR_SHIFT = 24
PDC_ATTR_SHIFT = 19
# Attributes
A_ALTCHARSET = 0x00010000
A_RIGHT = 0x00020000
A_LEFT = 0x00040000
A_ITALIC = 0x00080000
A_UNDERLINE = 0x00100000
A_REVERSE = 0x00200000
A_BLINK = 0x00400000
A_BOLD = 0x00800000
A_NORMAL = 0
A_DIM = 0
A_INVIS = 0
A_HORIZONTAL = 0
A_LOW = 0
A_TOP = 0
A_VERTICAL = 0
A_STANDOUT = (A_REVERSE | A_BOLD)
A_PROTECT = (A_UNDERLINE | A_LEFT | A_RIGHT)
A_ATTRIBUTES = 0xffff0000
A_COLOR = 0xff000000
A_CHARTEXT = 0x0000ffff
A_RIGHTLINE = A_RIGHT
A_LEFTLINE = A_LEFT
# Colors
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
#endregion -- Attributs & Colors (PDC) --
#region ++ Key mapping (PDC) ++
KEY_CODE_YES = 0x100 # If get_wch() gives a key code
KEY_BREAK = 0x101 # Not on PC KBD
KEY_MIN = KEY_BREAK
KEY_DOWN = 0x102 # Down arrow key
KEY_UP = 0x103 # Up arrow key
KEY_LEFT = 0x104 # Left arrow key
KEY_RIGHT = 0x105 # Right arrow key
KEY_HOME = 0x106 # home key
KEY_BACKSPACE = 0x107 # not on pc
KEY_F0 = 0x108 # function keys; 64 reserved
KEY_DL = 0x148 # delete line
KEY_IL = 0x149 # insert line
KEY_DC = 0x14a # delete character
KEY_IC = 0x14b # insert char or enter ins mode
KEY_EIC = 0x14c # exit insert char mode
KEY_CLEAR = 0x14d # clear screen
KEY_EOS = 0x14e # clear to end of screen
KEY_EOL = 0x14f # clear to end of line
KEY_SF = 0x150 # scroll 1 line forward
KEY_SR = 0x151 # scroll 1 line back (reverse)
KEY_NPAGE = 0x152 # next page
KEY_PPAGE = 0x153 # previous page
KEY_STAB = 0x154 # set tab
KEY_CTAB = 0x155 # clear tab
KEY_CATAB = 0x156 # clear all tabs
KEY_ENTER = 0x157 # enter or send (unreliable)
KEY_SRESET = 0x158 # soft/reset (partial/unreliable)
KEY_RESET = 0x159 # reset/hard reset (unreliable)
KEY_PRINT = 0x15a # print/copy
KEY_LL = 0x15b # home down/bottom (lower left)
KEY_ABORT = 0x15c # abort/terminate key (any)
KEY_SHELP = 0x15d # short help
KEY_LHELP = 0x15e # long help
KEY_BTAB = 0x15f # Back tab key
KEY_BEG = 0x160 # beg(inning) key
KEY_CANCEL = 0x161 # cancel key
KEY_CLOSE = 0x162 # close key
KEY_COMMAND = 0x163 # cmd (command) key
KEY_COPY = 0x164 # copy key
KEY_CREATE = 0x165 # create key
KEY_END = 0x166 # end key
KEY_EXIT = 0x167 # exit key
KEY_FIND = 0x168 # find key
KEY_HELP = 0x169 # help key
KEY_MARK = 0x16a # mark key
KEY_MESSAGE = 0x16b # message key
KEY_MOVE = 0x16c # move key
KEY_NEXT = 0x16d # next object key
KEY_OPEN = 0x16e # open key
KEY_OPTIONS = 0x16f # options key
KEY_PREVIOUS = 0x170 # previous object key
KEY_REDO = 0x171 # redo key
KEY_REFERENCE = 0x172 # ref(erence) key
KEY_REFRESH = 0x173 # refresh key
KEY_REPLACE = 0x174 # replace key
KEY_RESTART = 0x175 # restart key
KEY_RESUME = 0x176 # resume key
KEY_SAVE = 0x177 # save key
KEY_SBEG = 0x178 # shifted beginning key
KEY_SCANCEL = 0x179 # shifted cancel key
KEY_SCOMMAND = 0x17a # shifted command key
KEY_SCOPY = 0x17b # shifted copy key
KEY_SCREATE = 0x17c # shifted create key
KEY_SDC = 0x17d # shifted delete char key
KEY_SDL = 0x17e # shifted delete line key
KEY_SELECT = 0x17f # select key
KEY_SEND = 0x180 # shifted end key
KEY_SEOL = 0x181 # shifted clear line key
KEY_SEXIT = 0x182 # shifted exit key
KEY_SFIND = 0x183 # shifted find key
KEY_SHOME = 0x184 # shifted home key
KEY_SIC = 0x185 # shifted input key
KEY_SLEFT = 0x187 # shifted left arrow key
KEY_SMESSAGE = 0x188 # shifted message key
KEY_SMOVE = 0x189 # shifted move key
KEY_SNEXT = 0x18a # shifted next key
KEY_SOPTIONS = 0x18b # shifted options key
KEY_SPREVIOUS = 0x18c # shifted prev key
KEY_SPRINT = 0x18d # shifted print key
KEY_SREDO = 0x18e # shifted redo key
KEY_SREPLACE = 0x18f # shifted replace key
KEY_SRIGHT = 0x190 # shifted right arrow
KEY_SRSUME = 0x191 # shifted resume key
KEY_SSAVE = 0x192 # shifted save key
KEY_SSUSPEND = 0x193 # shifted suspend key
KEY_SUNDO = 0x194 # shifted undo key
KEY_SUSPEND = 0x195 # suspend key
KEY_UNDO = 0x196 # undo key
KEY_A1 = 0x1c1
KEY_A3 = 0x1c3
KEY_B2 = 0x1c5
KEY_C1 = 0x1c7
KEY_C3 = 0x1c9
KEY_MOUSE = 0x21b
KEY_RESIZE = 0x222
KEY_SDOWN = 0x224
KEY_MAX = KEY_SDOWN
#KEY_EVENT = PDC_KEY_EVENT
#endregion -- Key mapping (PDC) --
#region ++ Mouse mapping (PDC) ++
BUTTON1_RELEASED = 0x00000001
BUTTON1_PRESSED = 0x00000002
BUTTON1_CLICKED = 0x00000004
BUTTON1_DOUBLE_CLICKED = 0x00000008
BUTTON1_TRIPLE_CLICKED = 0x00000010
BUTTON2_RELEASED = 0x00000020
BUTTON2_PRESSED = 0x00000040
BUTTON2_CLICKED = 0x00000080
BUTTON2_DOUBLE_CLICKED = 0x00000100
BUTTON2_TRIPLE_CLICKED = 0x00000200
BUTTON3_RELEASED = 0x00000400
BUTTON3_PRESSED = 0x00000800
BUTTON3_CLICKED = 0x00001000
BUTTON3_DOUBLE_CLICKED = 0x00002000
BUTTON3_TRIPLE_CLICKED = 0x00004000
BUTTON4_RELEASED = 0x00008000
BUTTON4_PRESSED = 0x00010000
BUTTON4_CLICKED = 0x00020000
BUTTON4_DOUBLE_CLICKED = 0x00040000
BUTTON4_TRIPLE_CLICKED = 0x00080000
BUTTON5_RELEASED = 0x00100000
BUTTON5_PRESSED = 0x00200000
BUTTON5_CLICKED = 0x00400000
BUTTON5_DOUBLE_CLICKED = 0x00800000
BUTTON5_TRIPLE_CLICKED = 0x01000000
BUTTON_SHIFT = 0x04000000
BUTTON_CTRL = 0x08000000
BUTTON_ALT = 0x10000000
ALL_MOUSE_EVENTS = 0x1fffffff
REPORT_MOUSE_POSITION = 0x20000000
#endregion -- Mouse mapping (PDC) --
else:
#region ++ Attributs & Colors (NC) ++
# Attributes
A_NORMAL = 0
A_ATTRIBUTES = NCURSES_BITS(~(1 - 1),0)
A_CHARTEXT = NCURSES_BITS(1,0) - 1
A_COLOR = NCURSES_BITS(((1) << 8) - 1,0)
A_STANDOUT = NCURSES_BITS(1,8)
A_UNDERLINE = NCURSES_BITS(1,9)
A_REVERSE = NCURSES_BITS(1,10)
A_BLINK = NCURSES_BITS(1,11)
A_DIM = NCURSES_BITS(1,12)
A_BOLD = NCURSES_BITS(1,13)
A_ALTCHARSET = NCURSES_BITS(1,14)
A_INVIS = NCURSES_BITS(1,15)
A_PROTECT = NCURSES_BITS(1,16)
A_HORIZONTAL = NCURSES_BITS(1,17)
A_LEFT = NCURSES_BITS(1,18)
A_LOW = NCURSES_BITS(1,19)
A_RIGHT = NCURSES_BITS(1,20)
A_TOP = NCURSES_BITS(1,21)
A_VERTICAL = NCURSES_BITS(1,22)
A_ITALIC = NCURSES_BITS(1,23)
# Colors
COLOR_BLACK = 0
COLOR_RED = 1
COLOR_GREEN = 2
COLOR_YELLOW = 3
COLOR_BLUE = 4
COLOR_MAGENTA = 5
COLOR_CYAN = 6
COLOR_WHITE = 7
#endregion -- Attributs & Colors (NC) --
#region ++ Key mapping (NC) ++
KEY_CODE_YES = 0o400 # A wchar_t contains a key code
KEY_MIN = 0o401 # Minimum curses key
KEY_BREAK = 0o401 # Break key (unreliable)
KEY_SRESET = 0o530 # Soft (partial) reset (unreliable)
KEY_RESET = 0o531 # Reset or hard reset (unreliable)
KEY_DOWN = 0o402 # down-arrow key
KEY_UP = 0o403 # up-arrow key
KEY_LEFT = 0o404 # left-arrow key
KEY_RIGHT = 0o405 # right-arrow key
KEY_HOME = 0o406 # home key
KEY_BACKSPACE = 0o407 # backspace key
KEY_F0 = 0o410 # Function keys. Space for 64
KEY_DL = 0o510 # delete-line key
KEY_IL = 0o511 # insert-line key
KEY_DC = 0o512 # delete-character key
KEY_IC = 0o513 # insert-character key
KEY_EIC = 0o514 # sent by rmir or smir in insert mode
KEY_CLEAR = 0o515 # clear-screen or erase key
KEY_EOS = 0o516 # clear-to-end-of-screen key
KEY_EOL = 0o517 # clear-to-end-of-line key
KEY_SF = 0o520 # scroll-forward key
KEY_SR = 0o521 # scroll-backward key
KEY_NPAGE = 0o522 # next-page key
KEY_PPAGE = 0o523 # previous-page key
KEY_STAB = 0o524 # set-tab key
KEY_CTAB = 0o525 # clear-tab key
KEY_CATAB = 0o526 # clear-all-tabs key
KEY_ENTER = 0o527 # enter/send key
KEY_PRINT = 0o532 # print key
KEY_LL = 0o533 # lower-left key (home down)
KEY_A1 = 0o534 # upper left of keypad
KEY_A3 = 0o535 # upper right of keypad
KEY_B2 = 0o536 # center of keypad
KEY_C1 = 0o537 # lower left of keypad
KEY_C3 = 0o540 # lower right of keypad
KEY_BTAB = 0o541 # back-tab key
KEY_BEG = 0o542 # begin key
KEY_CANCEL = 0o543 # cancel key
KEY_CLOSE = 0o544 # close key
KEY_COMMAND = 0o545 # command key
KEY_COPY = 0o546 # copy key
KEY_CREATE = 0o547 # create key
KEY_END = 0o550 # end key
KEY_EXIT = 0o551 # exit key
KEY_FIND = 0o552 # find key
KEY_HELP = 0o553 # help key
KEY_MARK = 0o554 # mark key
KEY_MESSAGE = 0o555 # message key
KEY_MOVE = 0o556 # move key
KEY_NEXT = 0o557 # next key
KEY_OPEN = 0o560 # open key
KEY_OPTIONS = 0o561 # options key
KEY_PREVIOUS = 0o562 # previous key
KEY_REDO = 0o563 # redo key
KEY_REFERENCE = 0o564 # reference key
KEY_REFRESH = 0o565 # refresh key
KEY_REPLACE = 0o566 # replace key
KEY_RESTART = 0o567 # restart key
KEY_RESUME = 0o570 # resume key
KEY_SAVE = 0o571 # save key
KEY_SBEG = 0o572 # shifted begin key
KEY_SCANCEL = 0o573 # shifted cancel key
KEY_SCOMMAND = 0o574 # shifted command key
KEY_SCOPY = 0o575 # shifted copy key
KEY_SCREATE = 0o576 # shifted create key
KEY_SDC = 0o577 # shifted delete-character key
KEY_SDL = 0o600 # shifted delete-line key
KEY_SELECT = 0o601 # select key
KEY_SEND = 0o602 # shifted end key
KEY_SEOL = 0o603 # shifted clear-to-end-of-line key
KEY_SEXIT = 0o604 # shifted exit key
KEY_SFIND = 0o605 # shifted find key
KEY_SHELP = 0o606 # shifted help key
KEY_SHOME = 0o607 # shifted home key
KEY_SIC = 0o610 # shifted insert-character key
KEY_SLEFT = 0o611 # shifted left-arrow key
KEY_SMESSAGE = 0o612 # shifted message key
KEY_SMOVE = 0o613 # shifted move key
KEY_SNEXT = 0o614 # shifted next key
KEY_SOPTIONS = 0o615 # shifted options key
KEY_SPREVIOUS = 0o616 # shifted previous key
KEY_SPRINT = 0o617 # shifted print key
KEY_SREDO = 0o620 # shifted redo key
KEY_SREPLACE = 0o621 # shifted replace key
KEY_SRIGHT = 0o622 # shifted right-arrow key
KEY_SRSUME = 0o623 # shifted resume key
KEY_SSAVE = 0o624 # shifted save key
KEY_SSUSPEND = 0o625 # shifted suspend key
KEY_SUNDO = 0o626 # shifted undo key
KEY_SUSPEND = 0o627 # suspend key
KEY_UNDO = 0o630 # undo key
KEY_MOUSE = 0o631 # Mouse event has occurred
KEY_RESIZE = 0o632 # Terminal resize event
KEY_EVENT = 0o633 # We were interrupted by an event
KEY_MAX = 0o777 # Maximum key value is 0633
#endregion -- Key mapping (NC) --
#region ++ Mouse mapping (NC) ++
NCURSES_MOUSE_VERSION = 2 # TODO: Understand how it is defined
def NCURSES_MOUSE_MASK(b,m):
if NCURSES_MOUSE_VERSION > 1:
return ((m) << (((b) - 1) * 5))
else:
return ((m) << (((b) - 1) * 6))
NCURSES_BUTTON_RELEASED = 1
NCURSES_BUTTON_PRESSED = 2
NCURSES_BUTTON_CLICKED = 4
NCURSES_DOUBLE_CLICKED = 10
NCURSES_TRIPLE_CLICKED = 20
NCURSES_RESERVED_EVENT = 40
BUTTON1_RELEASED = NCURSES_MOUSE_MASK(1, NCURSES_BUTTON_RELEASED)
BUTTON1_PRESSED = NCURSES_MOUSE_MASK(1, NCURSES_BUTTON_PRESSED)
BUTTON1_CLICKED = NCURSES_MOUSE_MASK(1, NCURSES_BUTTON_CLICKED)
BUTTON1_DOUBLE_CLICKED = NCURSES_MOUSE_MASK(1, NCURSES_DOUBLE_CLICKED)
BUTTON1_TRIPLE_CLICKED = NCURSES_MOUSE_MASK(1, NCURSES_TRIPLE_CLICKED)
BUTTON2_RELEASED = NCURSES_MOUSE_MASK(2, NCURSES_BUTTON_RELEASED)
BUTTON2_PRESSED = NCURSES_MOUSE_MASK(2, NCURSES_BUTTON_PRESSED)
BUTTON2_CLICKED = NCURSES_MOUSE_MASK(2, NCURSES_BUTTON_CLICKED)
BUTTON2_DOUBLE_CLICKED = NCURSES_MOUSE_MASK(2, NCURSES_DOUBLE_CLICKED)
BUTTON2_TRIPLE_CLICKED = NCURSES_MOUSE_MASK(2, NCURSES_TRIPLE_CLICKED)
BUTTON3_RELEASED = NCURSES_MOUSE_MASK(3, NCURSES_BUTTON_RELEASED)
BUTTON3_PRESSED = NCURSES_MOUSE_MASK(3, NCURSES_BUTTON_PRESSED)
BUTTON3_CLICKED = NCURSES_MOUSE_MASK(3, NCURSES_BUTTON_CLICKED)
BUTTON3_DOUBLE_CLICKED = NCURSES_MOUSE_MASK(3, NCURSES_DOUBLE_CLICKED)
BUTTON3_TRIPLE_CLICKED = NCURSES_MOUSE_MASK(3, NCURSES_TRIPLE_CLICKED)
BUTTON4_RELEASED = NCURSES_MOUSE_MASK(4, NCURSES_BUTTON_RELEASED)
BUTTON4_PRESSED = NCURSES_MOUSE_MASK(4, NCURSES_BUTTON_PRESSED)
BUTTON4_CLICKED = NCURSES_MOUSE_MASK(4, NCURSES_BUTTON_CLICKED)
BUTTON4_DOUBLE_CLICKED = NCURSES_MOUSE_MASK(4, NCURSES_DOUBLE_CLICKED)
BUTTON4_TRIPLE_CLICKED = NCURSES_MOUSE_MASK(4, NCURSES_TRIPLE_CLICKED)
BUTTON5_RELEASED = NCURSES_MOUSE_MASK(5, NCURSES_BUTTON_RELEASED)
BUTTON5_PRESSED = NCURSES_MOUSE_MASK(5, NCURSES_BUTTON_PRESSED)
BUTTON5_CLICKED = NCURSES_MOUSE_MASK(5, NCURSES_BUTTON_CLICKED)
BUTTON5_DOUBLE_CLICKED = NCURSES_MOUSE_MASK(5, NCURSES_DOUBLE_CLICKED)
BUTTON5_TRIPLE_CLICKED = NCURSES_MOUSE_MASK(5, NCURSES_TRIPLE_CLICKED)
if NCURSES_MOUSE_VERSION > 1:
BUTTON_CTRL = NCURSES_MOUSE_MASK(6, 1)
BUTTON_SHIFT = NCURSES_MOUSE_MASK(6, 2)
BUTTON_ALT = NCURSES_MOUSE_MASK(6, 4)
REPORT_MOUSE_POSITION = NCURSES_MOUSE_MASK(6, 10)
else:
BUTTON_CTRL = NCURSES_MOUSE_MASK(5, 1)
BUTTON_SHIFT = NCURSES_MOUSE_MASK(5, 2)
BUTTON_ALT = NCURSES_MOUSE_MASK(5, 4)
REPORT_MOUSE_POSITION = NCURSES_MOUSE_MASK(5, 10)
ALL_MOUSE_EVENTS = (REPORT_MOUSE_POSITION - 1)
#endregion -- Mouse mapping (NC) --
#endregion --- CONSTANTS: PDCurses\Ncurses ncurses.h ---
#region +++ CONSTANTS: Platform-independent +++
# General
OK = 0
ERR = -1
def RCCHAR(ch):
"""Reverse of CCHAR function"""
if type(ch) == int: return chr(ch)
elif type(ch) == str: return ch
else:
raise Exception("RCCHAR: can't parse a non-char/non-int value.")
def CCHAR(ch):
"""Get a C character"""
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.")
def ALTCHAR(ch):
"""Alternate character set"""
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.")
def CTRL(ch): #1
"""returns CTRL + KEY"""
return CCHAR(ch) & 0x1f
def KEY_F(n):
return KEY_F0 + n # function keys 1-64
# 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
#endregion --- CONSTANTS: Platform-independent ---
#region +++ FUNCTION DEFINITIONS (PDC)\(NC) +++
lib1.erasechar.restype = ctypes.c_char
lib1.keyname.restype = ctypes.c_char_p
lib1.killchar.restype = ctypes.c_char
lib1.longname.restype = ctypes.c_char_p
lib1.termattrs.restype = ctypes.c_ulong
lib1.termname.restype = ctypes.c_char_p
lib1.winch.restype = ctypes.c_uint
lib1.mvwinch.restype = ctypes.c_uint
lib1.unctrl.restype = ctypes.c_char_p
lib1.is_wintouched.restype = ctypes.c_bool
lib1.is_linetouched.restype = ctypes.c_bool
lib1.can_change_color.restype = ctypes.c_bool
lib1.has_colors.restype = ctypes.c_bool
lib1.has_ic.restype = ctypes.c_bool
lib1.has_il.restype = ctypes.c_bool
lib1.has_key.restype = ctypes.c_bool
lib1.isendwin.restype = ctypes.c_bool
lib2.new_panel.restype = ctypes.c_void_p
lib1.derwin.restype = ctypes.c_void_p
lib1.initscr.restype = ctypes.c_void_p
lib1.newpad.restype = ctypes.c_void_p
lib1.newwin.restype = ctypes.c_void_p
lib1.subpad.restype = ctypes.c_void_p
lib1.subwin.restype = ctypes.c_void_p
lib2.panel_above.restype = ctypes.c_void_p
lib2.panel_below.restype = ctypes.c_void_p
lib2.panel_userptr.restype = ctypes.c_void_p
lib2.panel_window.restype = ctypes.c_void_p
#endregion --- FUNCTION DEFINITIONS (PDC)\(NC) ---
#region +++ UNIFIED CURSES +++
#region ++ Functions ++
def waddch(scr_id, ch, attr=A_NORMAL):
return lib1.waddch(scr_id, CCHAR(ch) | attr)
def wadd_wch(scr_id, wch, attr=A_NORMAL): # NEEDS_CHECK?
if NCURSES:
return lib1.wadd_wch(scr_id, ctypes.byref(cchar_t(attr, RCCHAR(wch))))
else:
oldattr = lib1.getattrs(scr_id)
lib1.wattrset(scr_id, attr)
ret = lib1.wadd_wch(scr_id, RCCHAR(wch))
lib1.wattrset(scr_id, oldattr)
return ret
#return lib1.wadd_wch(scr_id, CCHAR(wch) | attr ) # ??? Why no working
def waddstr(scr_id, cstr, attr="NO_USE"):
if attr != "NO_USE":
oldattr = lib1.getattrs(scr_id)
lib1.wattrset(scr_id, attr)
ret = lib1.waddstr(scr_id, CSTR(cstr))
if attr != "NO_USE":
lib1.wattrset(scr_id, oldattr)
return ret
def waddwstr(scr_id, wstr, attr="NO_USE"):
if wstr == '':
return None
if attr != "NO_USE":
oldattr = lib1.getattrs(scr_id)
lib1.wattrset(scr_id, attr)
#wstr = ctypes.create_unicode_buffer(wstr)
ret = lib1.waddwstr(scr_id, wstr)
if attr != "NO_USE":
lib1.wattrset(scr_id, oldattr)
return ret
def waddnstr(scr_id, cstr, n, attr="NO_USE"):
if attr != "NO_USE":
oldattr = lib1.getattrs(scr_id)
lib1.wattrset(scr_id, attr)
ret = lib1.waddnstr(scr_id, CSTR(cstr), n)
if attr != "NO_USE":
lib1.wattrset(scr_id, oldattr)
return ret
def wattroff(scr_id, attr):
return lib1.wattroff(scr_id, attr)
def wattron(scr_id, attr):
return lib1.wattron(scr_id, attr)
def wattrset(scr_id, attr):
return lib1.wattrset(scr_id, attr)
def baudrate():
return lib1.baudrate()
def beep():
return lib1.beep()
def COLOR_PAIR(n):
return color_pair(n)
def copywin(src_id, dest_id, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol, overlay):
return lib1.copywin(src_id, dest_id, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol, overlay)
def wclear(scr_id):
return lib1.wclear(scr_id)
def wclrtobot(scr_id):
return lib1.wclrtobot(scr_id)
def wclrtoeol(scr_id):
return lib1.wclrtoeol(scr_id)
def clearok(scr_id, yes):
return lib1.clearok(scr_id, yes)
def curs_set(visibility):
return lib1.curs_set(visibility)
def cursyncup(scr_id):
return lib1.wcursyncup(scr_id)
def def_prog_mode():
return lib1.def_prog_mode()
def def_shell_mode():
return lib1.def_shell_mode()
def delay_output(ms):
return lib1.delay_output(ms)
def wdelch(scr_id):
return lib1.wdelch(scr_id)
def wdeleteln(scr_id):
return lib1.wdeleteln(scr_id)
def wbkgd(scr_id, ch, attr=A_NORMAL):
return lib1.wbkgd(scr_id, ch | attr)
def wbkgdset(scr_id, ch, attr=A_NORMAL):
return lib1.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):
return lib1.wborder(scr_id, ls, rs, ts, bs, tl, tr, bl, br) # same as wborder_set (it supports unicode by default)
def box(scr_id, verch=ACS_VLINE, horch=ACS_HLINE):
return lib1.box(scr_id, verch, horch)
def can_change_color():
return lib1.can_change_color() == 1
def cbreak():
return lib1.cbreak()
def wchgat(scr_id, num, attr, color, opts=None):
return lib1.wchgat(scr_id, num, attr, color, None)
def color_content(color_number):
r = ctypes.c_short()
g = ctypes.c_short()
b = ctypes.c_short()
lib1.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 PDCURSES:
return PD_COLOR_PAIR(color_number)
else:
return NC_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):
return lib1.copywin(src_id, dest_id, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol, overlay)
def wclear(scr_id):
return lib1.wclear(scr_id)
def wclrtobot(scr_id):
return lib1.wclrtobot(scr_id)
def wclrtoeol(scr_id):
return lib1.wclrtoeol(scr_id)
def clearok(scr_id, yes):
return lib1.clearok(scr_id, yes)
def curs_set(visibility):
return lib1.curs_set(visibility)
def cursyncup(scr_id):
return lib1.wcursyncup(scr_id)
def def_prog_mode():
return lib1.def_prog_mode()
def def_shell_mode():
return lib1.def_shell_mode()
def delay_output(ms):
return lib1.delay_output(ms)
def wdelch(scr_id):
return lib1.wdelch(scr_id)
def wdeleteln(scr_id):
return lib1.wdeleteln(scr_id)
def delwin(scr_id):
return lib1.delwin(scr_id)
def derwin(srcwin, nlines, ncols, begin_y, begin_x):
return ctypes.c_void_p(lib1.derwin(srcwin, nlines, ncols, begin_y, begin_x))
def doupdate():
return lib1.doupdate()
def echo():
return lib1.echo()
def wechochar(scr_id, ch, attr=A_NORMAL):