Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/Modules/_cursesmodule.c
12 views
1
/*
2
* This is a curses module for Python.
3
*
4
* Based on prior work by Lance Ellinghaus and Oliver Andrich
5
* Version 1.2 of this module: Copyright 1994 by Lance Ellinghouse,
6
* Cathedral City, California Republic, United States of America.
7
*
8
* Version 1.5b1, heavily extended for ncurses by Oliver Andrich:
9
* Copyright 1996,1997 by Oliver Andrich, Koblenz, Germany.
10
*
11
* Tidied for Python 1.6, and currently maintained by <[email protected]>.
12
*
13
* Permission is hereby granted, free of charge, to any person obtaining
14
* a copy of this source file to use, copy, modify, merge, or publish it
15
* subject to the following conditions:
16
*
17
* The above copyright notice and this permission notice shall be included
18
* in all copies or in any new file that contains a substantial portion of
19
* this file.
20
*
21
* THE AUTHOR MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF
22
* THE SOFTWARE FOR ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT
23
* EXPRESS OR IMPLIED WARRANTY. THE AUTHOR DISCLAIMS ALL WARRANTIES
24
* WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
25
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26
* NON-INFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE
27
* AUTHOR BE LIABLE TO YOU OR ANY OTHER PARTY FOR ANY SPECIAL,
28
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
29
* WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE, STRICT LIABILITY OR
30
* ANY OTHER ACTION ARISING OUT OF OR IN CONNECTION WITH THE USE OR
31
* PERFORMANCE OF THIS SOFTWARE.
32
*/
33
34
/*
35
36
A number of SysV or ncurses functions don't have wrappers yet; if you
37
need a given function, add it and send a patch. See
38
https://www.python.org/dev/patches/ for instructions on how to submit
39
patches to Python.
40
41
Here's a list of currently unsupported functions:
42
43
addchnstr addchstr color_set define_key
44
del_curterm delscreen dupwin inchnstr inchstr innstr keyok
45
mcprint mvaddchnstr mvaddchstr mvcur mvinchnstr
46
mvinchstr mvinnstr mmvwaddchnstr mvwaddchstr
47
mvwinchnstr mvwinchstr mvwinnstr newterm
48
restartterm ripoffline scr_dump
49
scr_init scr_restore scr_set scrl set_curterm set_term setterm
50
tgetent tgetflag tgetnum tgetstr tgoto timeout tputs
51
vidattr vidputs waddchnstr waddchstr
52
wcolor_set winchnstr winchstr winnstr wmouse_trafo wscrl
53
54
Low-priority:
55
slk_attr slk_attr_off slk_attr_on slk_attr_set slk_attroff
56
slk_attron slk_attrset slk_clear slk_color slk_init slk_label
57
slk_noutrefresh slk_refresh slk_restore slk_set slk_touch
58
59
Menu extension (ncurses and probably SYSV):
60
current_item free_item free_menu item_count item_description
61
item_index item_init item_name item_opts item_opts_off
62
item_opts_on item_term item_userptr item_value item_visible
63
menu_back menu_driver menu_fore menu_format menu_grey
64
menu_init menu_items menu_mark menu_opts menu_opts_off
65
menu_opts_on menu_pad menu_pattern menu_request_by_name
66
menu_request_name menu_spacing menu_sub menu_term menu_userptr
67
menu_win new_item new_menu pos_menu_cursor post_menu
68
scale_menu set_current_item set_item_init set_item_opts
69
set_item_term set_item_userptr set_item_value set_menu_back
70
set_menu_fore set_menu_format set_menu_grey set_menu_init
71
set_menu_items set_menu_mark set_menu_opts set_menu_pad
72
set_menu_pattern set_menu_spacing set_menu_sub set_menu_term
73
set_menu_userptr set_menu_win set_top_row top_row unpost_menu
74
75
Form extension (ncurses and probably SYSV):
76
current_field data_ahead data_behind dup_field
77
dynamic_fieldinfo field_arg field_back field_buffer
78
field_count field_fore field_index field_info field_init
79
field_just field_opts field_opts_off field_opts_on field_pad
80
field_status field_term field_type field_userptr form_driver
81
form_fields form_init form_opts form_opts_off form_opts_on
82
form_page form_request_by_name form_request_name form_sub
83
form_term form_userptr form_win free_field free_form
84
link_field link_fieldtype move_field new_field new_form
85
new_page pos_form_cursor post_form scale_form
86
set_current_field set_field_back set_field_buffer
87
set_field_fore set_field_init set_field_just set_field_opts
88
set_field_pad set_field_status set_field_term set_field_type
89
set_field_userptr set_fieldtype_arg set_fieldtype_choice
90
set_form_fields set_form_init set_form_opts set_form_page
91
set_form_sub set_form_term set_form_userptr set_form_win
92
set_max_field set_new_page unpost_form
93
94
95
*/
96
97
/* Release Number */
98
99
static const char PyCursesVersion[] = "2.2";
100
101
/* Includes */
102
103
#ifndef Py_BUILD_CORE_BUILTIN
104
# define Py_BUILD_CORE_MODULE 1
105
#endif
106
107
#include "Python.h"
108
#include "pycore_long.h" // _PyLong_GetZero()
109
#include "pycore_structseq.h" // _PyStructSequence_NewType()
110
111
#ifdef __hpux
112
#define STRICT_SYSV_CURSES
113
#endif
114
115
#define CURSES_MODULE
116
#include "py_curses.h"
117
118
#if defined(HAVE_TERM_H) || defined(__sgi)
119
/* For termname, longname, putp, tigetflag, tigetnum, tigetstr, tparm
120
which are not declared in SysV curses and for setupterm. */
121
#include <term.h>
122
/* Including <term.h> #defines many common symbols. */
123
#undef lines
124
#undef columns
125
#endif
126
127
#ifdef HAVE_LANGINFO_H
128
#include <langinfo.h>
129
#endif
130
131
#if !defined(HAVE_NCURSES_H) && (defined(sgi) || defined(__sun) || defined(SCO5))
132
#define STRICT_SYSV_CURSES /* Don't use ncurses extensions */
133
typedef chtype attr_t; /* No attr_t type is available */
134
#endif
135
136
#if defined(_AIX)
137
#define STRICT_SYSV_CURSES
138
#endif
139
140
#if NCURSES_EXT_FUNCS+0 >= 20170401 && NCURSES_EXT_COLORS+0 >= 20170401
141
#define _NCURSES_EXTENDED_COLOR_FUNCS 1
142
#else
143
#define _NCURSES_EXTENDED_COLOR_FUNCS 0
144
#endif
145
146
#if _NCURSES_EXTENDED_COLOR_FUNCS
147
#define _CURSES_COLOR_VAL_TYPE int
148
#define _CURSES_COLOR_NUM_TYPE int
149
#define _CURSES_INIT_COLOR_FUNC init_extended_color
150
#define _CURSES_INIT_PAIR_FUNC init_extended_pair
151
#define _COLOR_CONTENT_FUNC extended_color_content
152
#define _CURSES_PAIR_CONTENT_FUNC extended_pair_content
153
#else
154
#define _CURSES_COLOR_VAL_TYPE short
155
#define _CURSES_COLOR_NUM_TYPE short
156
#define _CURSES_INIT_COLOR_FUNC init_color
157
#define _CURSES_INIT_PAIR_FUNC init_pair
158
#define _COLOR_CONTENT_FUNC color_content
159
#define _CURSES_PAIR_CONTENT_FUNC pair_content
160
#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */
161
162
/*[clinic input]
163
module _curses
164
class _curses.window "PyCursesWindowObject *" "&PyCursesWindow_Type"
165
[clinic start generated code]*/
166
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=43265c372c2887d6]*/
167
168
/* Definition of exception curses.error */
169
170
static PyObject *PyCursesError;
171
172
/* Tells whether setupterm() has been called to initialise terminfo. */
173
static int initialised_setupterm = FALSE;
174
175
/* Tells whether initscr() has been called to initialise curses. */
176
static int initialised = FALSE;
177
178
/* Tells whether start_color() has been called to initialise color usage. */
179
static int initialisedcolors = FALSE;
180
181
static char *screen_encoding = NULL;
182
183
/* Utility Macros */
184
#define PyCursesSetupTermCalled \
185
if (initialised_setupterm != TRUE) { \
186
PyErr_SetString(PyCursesError, \
187
"must call (at least) setupterm() first"); \
188
return 0; }
189
190
#define PyCursesInitialised \
191
if (initialised != TRUE) { \
192
PyErr_SetString(PyCursesError, \
193
"must call initscr() first"); \
194
return 0; }
195
196
#define PyCursesInitialisedColor \
197
if (initialisedcolors != TRUE) { \
198
PyErr_SetString(PyCursesError, \
199
"must call start_color() first"); \
200
return 0; }
201
202
/* Utility Functions */
203
204
/*
205
* Check the return code from a curses function and return None
206
* or raise an exception as appropriate. These are exported using the
207
* capsule API.
208
*/
209
210
static PyObject *
211
PyCursesCheckERR(int code, const char *fname)
212
{
213
if (code != ERR) {
214
Py_RETURN_NONE;
215
} else {
216
if (fname == NULL) {
217
PyErr_SetString(PyCursesError, catchall_ERR);
218
} else {
219
PyErr_Format(PyCursesError, "%s() returned ERR", fname);
220
}
221
return NULL;
222
}
223
}
224
225
/* Convert an object to a byte (an integer of type chtype):
226
227
- int
228
- bytes of length 1
229
- str of length 1
230
231
Return 1 on success, 0 on error (invalid type or integer overflow). */
232
static int
233
PyCurses_ConvertToChtype(PyCursesWindowObject *win, PyObject *obj, chtype *ch)
234
{
235
long value;
236
if(PyBytes_Check(obj) && PyBytes_Size(obj) == 1) {
237
value = (unsigned char)PyBytes_AsString(obj)[0];
238
}
239
else if (PyUnicode_Check(obj)) {
240
if (PyUnicode_GetLength(obj) != 1) {
241
PyErr_Format(PyExc_TypeError,
242
"expect bytes or str of length 1, or int, "
243
"got a str of length %zi",
244
PyUnicode_GET_LENGTH(obj));
245
return 0;
246
}
247
value = PyUnicode_READ_CHAR(obj, 0);
248
if (128 < value) {
249
PyObject *bytes;
250
const char *encoding;
251
if (win)
252
encoding = win->encoding;
253
else
254
encoding = screen_encoding;
255
bytes = PyUnicode_AsEncodedString(obj, encoding, NULL);
256
if (bytes == NULL)
257
return 0;
258
if (PyBytes_GET_SIZE(bytes) == 1)
259
value = (unsigned char)PyBytes_AS_STRING(bytes)[0];
260
else
261
value = -1;
262
Py_DECREF(bytes);
263
if (value < 0)
264
goto overflow;
265
}
266
}
267
else if (PyLong_CheckExact(obj)) {
268
int long_overflow;
269
value = PyLong_AsLongAndOverflow(obj, &long_overflow);
270
if (long_overflow)
271
goto overflow;
272
}
273
else {
274
PyErr_Format(PyExc_TypeError,
275
"expect bytes or str of length 1, or int, got %s",
276
Py_TYPE(obj)->tp_name);
277
return 0;
278
}
279
*ch = (chtype)value;
280
if ((long)*ch != value)
281
goto overflow;
282
return 1;
283
284
overflow:
285
PyErr_SetString(PyExc_OverflowError,
286
"byte doesn't fit in chtype");
287
return 0;
288
}
289
290
/* Convert an object to a byte (chtype) or a character (cchar_t):
291
292
- int
293
- bytes of length 1
294
- str of length 1
295
296
Return:
297
298
- 2 if obj is a character (written into *wch)
299
- 1 if obj is a byte (written into *ch)
300
- 0 on error: raise an exception */
301
static int
302
PyCurses_ConvertToCchar_t(PyCursesWindowObject *win, PyObject *obj,
303
chtype *ch
304
#ifdef HAVE_NCURSESW
305
, wchar_t *wch
306
#endif
307
)
308
{
309
long value;
310
#ifdef HAVE_NCURSESW
311
wchar_t buffer[2];
312
#endif
313
314
if (PyUnicode_Check(obj)) {
315
#ifdef HAVE_NCURSESW
316
if (PyUnicode_AsWideChar(obj, buffer, 2) != 1) {
317
PyErr_Format(PyExc_TypeError,
318
"expect bytes or str of length 1, or int, "
319
"got a str of length %zi",
320
PyUnicode_GET_LENGTH(obj));
321
return 0;
322
}
323
*wch = buffer[0];
324
return 2;
325
#else
326
return PyCurses_ConvertToChtype(win, obj, ch);
327
#endif
328
}
329
else if(PyBytes_Check(obj) && PyBytes_Size(obj) == 1) {
330
value = (unsigned char)PyBytes_AsString(obj)[0];
331
}
332
else if (PyLong_CheckExact(obj)) {
333
int overflow;
334
value = PyLong_AsLongAndOverflow(obj, &overflow);
335
if (overflow) {
336
PyErr_SetString(PyExc_OverflowError,
337
"int doesn't fit in long");
338
return 0;
339
}
340
}
341
else {
342
PyErr_Format(PyExc_TypeError,
343
"expect bytes or str of length 1, or int, got %s",
344
Py_TYPE(obj)->tp_name);
345
return 0;
346
}
347
348
*ch = (chtype)value;
349
if ((long)*ch != value) {
350
PyErr_Format(PyExc_OverflowError,
351
"byte doesn't fit in chtype");
352
return 0;
353
}
354
return 1;
355
}
356
357
/* Convert an object to a byte string (char*) or a wide character string
358
(wchar_t*). Return:
359
360
- 2 if obj is a character string (written into *wch)
361
- 1 if obj is a byte string (written into *bytes)
362
- 0 on error: raise an exception */
363
static int
364
PyCurses_ConvertToString(PyCursesWindowObject *win, PyObject *obj,
365
PyObject **bytes, wchar_t **wstr)
366
{
367
char *str;
368
if (PyUnicode_Check(obj)) {
369
#ifdef HAVE_NCURSESW
370
assert (wstr != NULL);
371
372
*wstr = PyUnicode_AsWideCharString(obj, NULL);
373
if (*wstr == NULL)
374
return 0;
375
return 2;
376
#else
377
assert (wstr == NULL);
378
*bytes = PyUnicode_AsEncodedString(obj, win->encoding, NULL);
379
if (*bytes == NULL)
380
return 0;
381
/* check for embedded null bytes */
382
if (PyBytes_AsStringAndSize(*bytes, &str, NULL) < 0) {
383
Py_CLEAR(*bytes);
384
return 0;
385
}
386
return 1;
387
#endif
388
}
389
else if (PyBytes_Check(obj)) {
390
*bytes = Py_NewRef(obj);
391
/* check for embedded null bytes */
392
if (PyBytes_AsStringAndSize(*bytes, &str, NULL) < 0) {
393
Py_DECREF(obj);
394
return 0;
395
}
396
return 1;
397
}
398
399
PyErr_Format(PyExc_TypeError, "expect bytes or str, got %s",
400
Py_TYPE(obj)->tp_name);
401
return 0;
402
}
403
404
static int
405
color_allow_default_converter(PyObject *arg, void *ptr)
406
{
407
long color_number;
408
int overflow;
409
410
color_number = PyLong_AsLongAndOverflow(arg, &overflow);
411
if (color_number == -1 && PyErr_Occurred())
412
return 0;
413
414
if (overflow > 0 || color_number >= COLORS) {
415
PyErr_Format(PyExc_ValueError,
416
"Color number is greater than COLORS-1 (%d).",
417
COLORS - 1);
418
return 0;
419
}
420
else if (overflow < 0 || color_number < 0) {
421
color_number = -1;
422
}
423
424
*(int *)ptr = (int)color_number;
425
return 1;
426
}
427
428
static int
429
color_converter(PyObject *arg, void *ptr)
430
{
431
if (!color_allow_default_converter(arg, ptr)) {
432
return 0;
433
}
434
if (*(int *)ptr < 0) {
435
PyErr_SetString(PyExc_ValueError,
436
"Color number is less than 0.");
437
return 0;
438
}
439
return 1;
440
}
441
442
/*[python input]
443
class color_converter(CConverter):
444
type = 'int'
445
converter = 'color_converter'
446
[python start generated code]*/
447
/*[python end generated code: output=da39a3ee5e6b4b0d input=4260d2b6e66b3709]*/
448
449
/*[python input]
450
class color_allow_default_converter(CConverter):
451
type = 'int'
452
converter = 'color_allow_default_converter'
453
[python start generated code]*/
454
/*[python end generated code: output=da39a3ee5e6b4b0d input=975602bc058a872d]*/
455
456
static int
457
pair_converter(PyObject *arg, void *ptr)
458
{
459
long pair_number;
460
int overflow;
461
462
pair_number = PyLong_AsLongAndOverflow(arg, &overflow);
463
if (pair_number == -1 && PyErr_Occurred())
464
return 0;
465
466
#if _NCURSES_EXTENDED_COLOR_FUNCS
467
if (overflow > 0 || pair_number > INT_MAX) {
468
PyErr_Format(PyExc_ValueError,
469
"Color pair is greater than maximum (%d).",
470
INT_MAX);
471
return 0;
472
}
473
#else
474
if (overflow > 0 || pair_number >= COLOR_PAIRS) {
475
PyErr_Format(PyExc_ValueError,
476
"Color pair is greater than COLOR_PAIRS-1 (%d).",
477
COLOR_PAIRS - 1);
478
return 0;
479
}
480
#endif
481
else if (overflow < 0 || pair_number < 0) {
482
PyErr_SetString(PyExc_ValueError,
483
"Color pair is less than 0.");
484
return 0;
485
}
486
487
*(int *)ptr = (int)pair_number;
488
return 1;
489
}
490
491
/*[python input]
492
class pair_converter(CConverter):
493
type = 'int'
494
converter = 'pair_converter'
495
[python start generated code]*/
496
/*[python end generated code: output=da39a3ee5e6b4b0d input=1a918ae6a1b32af7]*/
497
498
static int
499
component_converter(PyObject *arg, void *ptr)
500
{
501
long component;
502
int overflow;
503
504
component = PyLong_AsLongAndOverflow(arg, &overflow);
505
if (component == -1 && PyErr_Occurred())
506
return 0;
507
508
if (overflow > 0 || component > 1000) {
509
PyErr_SetString(PyExc_ValueError,
510
"Color component is greater than 1000");
511
return 0;
512
}
513
else if (overflow < 0 || component < 0) {
514
PyErr_SetString(PyExc_ValueError,
515
"Color component is less than 0");
516
return 0;
517
}
518
519
*(short *)ptr = (short)component;
520
return 1;
521
}
522
523
/*[python input]
524
class component_converter(CConverter):
525
type = 'short'
526
converter = 'component_converter'
527
[python start generated code]*/
528
/*[python end generated code: output=da39a3ee5e6b4b0d input=38e9be01d33927fb]*/
529
530
/* Function versions of the 3 functions for testing whether curses has been
531
initialised or not. */
532
533
static int func_PyCursesSetupTermCalled(void)
534
{
535
PyCursesSetupTermCalled;
536
return 1;
537
}
538
539
static int func_PyCursesInitialised(void)
540
{
541
PyCursesInitialised;
542
return 1;
543
}
544
545
static int func_PyCursesInitialisedColor(void)
546
{
547
PyCursesInitialisedColor;
548
return 1;
549
}
550
551
/*****************************************************************************
552
The Window Object
553
******************************************************************************/
554
555
/* Definition of the window type */
556
557
PyTypeObject PyCursesWindow_Type;
558
559
/* Function prototype macros for Window object
560
561
X - function name
562
TYPE - parameter Type
563
ERGSTR - format string for construction of the return value
564
PARSESTR - format string for argument parsing
565
*/
566
567
#define Window_NoArgNoReturnFunction(X) \
568
static PyObject *PyCursesWindow_ ## X \
569
(PyCursesWindowObject *self, PyObject *Py_UNUSED(ignored)) \
570
{ return PyCursesCheckERR(X(self->win), # X); }
571
572
#define Window_NoArgTrueFalseFunction(X) \
573
static PyObject * PyCursesWindow_ ## X \
574
(PyCursesWindowObject *self, PyObject *Py_UNUSED(ignored)) \
575
{ \
576
return PyBool_FromLong(X(self->win)); }
577
578
#define Window_NoArgNoReturnVoidFunction(X) \
579
static PyObject * PyCursesWindow_ ## X \
580
(PyCursesWindowObject *self, PyObject *Py_UNUSED(ignored)) \
581
{ \
582
X(self->win); Py_RETURN_NONE; }
583
584
#define Window_NoArg2TupleReturnFunction(X, TYPE, ERGSTR) \
585
static PyObject * PyCursesWindow_ ## X \
586
(PyCursesWindowObject *self, PyObject *Py_UNUSED(ignored)) \
587
{ \
588
TYPE arg1, arg2; \
589
X(self->win,arg1,arg2); return Py_BuildValue(ERGSTR, arg1, arg2); }
590
591
#define Window_OneArgNoReturnVoidFunction(X, TYPE, PARSESTR) \
592
static PyObject * PyCursesWindow_ ## X \
593
(PyCursesWindowObject *self, PyObject *args) \
594
{ \
595
TYPE arg1; \
596
if (!PyArg_ParseTuple(args, PARSESTR, &arg1)) return NULL; \
597
X(self->win,arg1); Py_RETURN_NONE; }
598
599
#define Window_OneArgNoReturnFunction(X, TYPE, PARSESTR) \
600
static PyObject * PyCursesWindow_ ## X \
601
(PyCursesWindowObject *self, PyObject *args) \
602
{ \
603
TYPE arg1; \
604
if (!PyArg_ParseTuple(args,PARSESTR, &arg1)) return NULL; \
605
return PyCursesCheckERR(X(self->win, arg1), # X); }
606
607
#define Window_TwoArgNoReturnFunction(X, TYPE, PARSESTR) \
608
static PyObject * PyCursesWindow_ ## X \
609
(PyCursesWindowObject *self, PyObject *args) \
610
{ \
611
TYPE arg1, arg2; \
612
if (!PyArg_ParseTuple(args,PARSESTR, &arg1, &arg2)) return NULL; \
613
return PyCursesCheckERR(X(self->win, arg1, arg2), # X); }
614
615
/* ------------- WINDOW routines --------------- */
616
617
Window_NoArgNoReturnFunction(untouchwin)
618
Window_NoArgNoReturnFunction(touchwin)
619
Window_NoArgNoReturnFunction(redrawwin)
620
Window_NoArgNoReturnFunction(winsertln)
621
Window_NoArgNoReturnFunction(werase)
622
Window_NoArgNoReturnFunction(wdeleteln)
623
624
Window_NoArgTrueFalseFunction(is_wintouched)
625
626
Window_NoArgNoReturnVoidFunction(wsyncup)
627
Window_NoArgNoReturnVoidFunction(wsyncdown)
628
Window_NoArgNoReturnVoidFunction(wstandend)
629
Window_NoArgNoReturnVoidFunction(wstandout)
630
Window_NoArgNoReturnVoidFunction(wcursyncup)
631
Window_NoArgNoReturnVoidFunction(wclrtoeol)
632
Window_NoArgNoReturnVoidFunction(wclrtobot)
633
Window_NoArgNoReturnVoidFunction(wclear)
634
635
Window_OneArgNoReturnVoidFunction(idcok, int, "i;True(1) or False(0)")
636
#ifdef HAVE_CURSES_IMMEDOK
637
Window_OneArgNoReturnVoidFunction(immedok, int, "i;True(1) or False(0)")
638
#endif
639
Window_OneArgNoReturnVoidFunction(wtimeout, int, "i;delay")
640
641
Window_NoArg2TupleReturnFunction(getyx, int, "ii")
642
Window_NoArg2TupleReturnFunction(getbegyx, int, "ii")
643
Window_NoArg2TupleReturnFunction(getmaxyx, int, "ii")
644
Window_NoArg2TupleReturnFunction(getparyx, int, "ii")
645
646
Window_OneArgNoReturnFunction(clearok, int, "i;True(1) or False(0)")
647
Window_OneArgNoReturnFunction(idlok, int, "i;True(1) or False(0)")
648
Window_OneArgNoReturnFunction(keypad, int, "i;True(1) or False(0)")
649
Window_OneArgNoReturnFunction(leaveok, int, "i;True(1) or False(0)")
650
Window_OneArgNoReturnFunction(nodelay, int, "i;True(1) or False(0)")
651
Window_OneArgNoReturnFunction(notimeout, int, "i;True(1) or False(0)")
652
Window_OneArgNoReturnFunction(scrollok, int, "i;True(1) or False(0)")
653
Window_OneArgNoReturnFunction(winsdelln, int, "i;nlines")
654
#ifdef HAVE_CURSES_SYNCOK
655
Window_OneArgNoReturnFunction(syncok, int, "i;True(1) or False(0)")
656
#endif
657
658
Window_TwoArgNoReturnFunction(mvwin, int, "ii;y,x")
659
Window_TwoArgNoReturnFunction(mvderwin, int, "ii;y,x")
660
Window_TwoArgNoReturnFunction(wmove, int, "ii;y,x")
661
#ifndef STRICT_SYSV_CURSES
662
Window_TwoArgNoReturnFunction(wresize, int, "ii;lines,columns")
663
#endif
664
665
/* Allocation and deallocation of Window Objects */
666
667
static PyObject *
668
PyCursesWindow_New(WINDOW *win, const char *encoding)
669
{
670
PyCursesWindowObject *wo;
671
672
if (encoding == NULL) {
673
#if defined(MS_WINDOWS)
674
char *buffer[100];
675
UINT cp;
676
cp = GetConsoleOutputCP();
677
if (cp != 0) {
678
PyOS_snprintf(buffer, sizeof(buffer), "cp%u", cp);
679
encoding = buffer;
680
}
681
#elif defined(CODESET)
682
const char *codeset = nl_langinfo(CODESET);
683
if (codeset != NULL && codeset[0] != 0)
684
encoding = codeset;
685
#endif
686
if (encoding == NULL)
687
encoding = "utf-8";
688
}
689
690
wo = PyObject_New(PyCursesWindowObject, &PyCursesWindow_Type);
691
if (wo == NULL) return NULL;
692
wo->win = win;
693
wo->encoding = _PyMem_Strdup(encoding);
694
if (wo->encoding == NULL) {
695
Py_DECREF(wo);
696
PyErr_NoMemory();
697
return NULL;
698
}
699
return (PyObject *)wo;
700
}
701
702
static void
703
PyCursesWindow_Dealloc(PyCursesWindowObject *wo)
704
{
705
if (wo->win != stdscr) delwin(wo->win);
706
if (wo->encoding != NULL)
707
PyMem_Free(wo->encoding);
708
PyObject_Free(wo);
709
}
710
711
/* Addch, Addstr, Addnstr */
712
713
/*[clinic input]
714
_curses.window.addch
715
716
[
717
y: int
718
Y-coordinate.
719
x: int
720
X-coordinate.
721
]
722
723
ch: object
724
Character to add.
725
726
[
727
attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
728
Attributes for the character.
729
]
730
/
731
732
Paint the character.
733
734
Paint character ch at (y, x) with attributes attr,
735
overwriting any character previously painted at that location.
736
By default, the character position and attributes are the
737
current settings for the window object.
738
[clinic start generated code]*/
739
740
static PyObject *
741
_curses_window_addch_impl(PyCursesWindowObject *self, int group_left_1,
742
int y, int x, PyObject *ch, int group_right_1,
743
long attr)
744
/*[clinic end generated code: output=00f4c37af3378f45 input=95ce131578458196]*/
745
{
746
int coordinates_group = group_left_1;
747
int rtn;
748
int type;
749
chtype cch = 0;
750
#ifdef HAVE_NCURSESW
751
wchar_t wstr[2];
752
cchar_t wcval;
753
#endif
754
const char *funcname;
755
756
#ifdef HAVE_NCURSESW
757
type = PyCurses_ConvertToCchar_t(self, ch, &cch, wstr);
758
if (type == 2) {
759
funcname = "add_wch";
760
wstr[1] = L'\0';
761
setcchar(&wcval, wstr, attr, PAIR_NUMBER(attr), NULL);
762
if (coordinates_group)
763
rtn = mvwadd_wch(self->win,y,x, &wcval);
764
else {
765
rtn = wadd_wch(self->win, &wcval);
766
}
767
}
768
else
769
#else
770
type = PyCurses_ConvertToCchar_t(self, ch, &cch);
771
#endif
772
if (type == 1) {
773
funcname = "addch";
774
if (coordinates_group)
775
rtn = mvwaddch(self->win,y,x, cch | (attr_t) attr);
776
else {
777
rtn = waddch(self->win, cch | (attr_t) attr);
778
}
779
}
780
else {
781
return NULL;
782
}
783
return PyCursesCheckERR(rtn, funcname);
784
}
785
786
/*[clinic input]
787
_curses.window.addstr
788
789
[
790
y: int
791
Y-coordinate.
792
x: int
793
X-coordinate.
794
]
795
796
str: object
797
String to add.
798
799
[
800
attr: long
801
Attributes for characters.
802
]
803
/
804
805
Paint the string.
806
807
Paint the string str at (y, x) with attributes attr,
808
overwriting anything previously on the display.
809
By default, the character position and attributes are the
810
current settings for the window object.
811
[clinic start generated code]*/
812
813
static PyObject *
814
_curses_window_addstr_impl(PyCursesWindowObject *self, int group_left_1,
815
int y, int x, PyObject *str, int group_right_1,
816
long attr)
817
/*[clinic end generated code: output=65a928ea85ff3115 input=ff6cbb91448a22a3]*/
818
{
819
int rtn;
820
int strtype;
821
PyObject *bytesobj = NULL;
822
#ifdef HAVE_NCURSESW
823
wchar_t *wstr = NULL;
824
#endif
825
attr_t attr_old = A_NORMAL;
826
int use_xy = group_left_1, use_attr = group_right_1;
827
const char *funcname;
828
829
#ifdef HAVE_NCURSESW
830
strtype = PyCurses_ConvertToString(self, str, &bytesobj, &wstr);
831
#else
832
strtype = PyCurses_ConvertToString(self, str, &bytesobj, NULL);
833
#endif
834
if (strtype == 0) {
835
return NULL;
836
}
837
if (use_attr) {
838
attr_old = getattrs(self->win);
839
(void)wattrset(self->win,attr);
840
}
841
#ifdef HAVE_NCURSESW
842
if (strtype == 2) {
843
funcname = "addwstr";
844
if (use_xy)
845
rtn = mvwaddwstr(self->win,y,x,wstr);
846
else
847
rtn = waddwstr(self->win,wstr);
848
PyMem_Free(wstr);
849
}
850
else
851
#endif
852
{
853
const char *str = PyBytes_AS_STRING(bytesobj);
854
funcname = "addstr";
855
if (use_xy)
856
rtn = mvwaddstr(self->win,y,x,str);
857
else
858
rtn = waddstr(self->win,str);
859
Py_DECREF(bytesobj);
860
}
861
if (use_attr)
862
(void)wattrset(self->win,attr_old);
863
return PyCursesCheckERR(rtn, funcname);
864
}
865
866
/*[clinic input]
867
_curses.window.addnstr
868
869
[
870
y: int
871
Y-coordinate.
872
x: int
873
X-coordinate.
874
]
875
876
str: object
877
String to add.
878
879
n: int
880
Maximal number of characters.
881
882
[
883
attr: long
884
Attributes for characters.
885
]
886
/
887
888
Paint at most n characters of the string.
889
890
Paint at most n characters of the string str at (y, x) with
891
attributes attr, overwriting anything previously on the display.
892
By default, the character position and attributes are the
893
current settings for the window object.
894
[clinic start generated code]*/
895
896
static PyObject *
897
_curses_window_addnstr_impl(PyCursesWindowObject *self, int group_left_1,
898
int y, int x, PyObject *str, int n,
899
int group_right_1, long attr)
900
/*[clinic end generated code: output=6d21cee2ce6876d9 input=72718415c2744a2a]*/
901
{
902
int rtn;
903
int strtype;
904
PyObject *bytesobj = NULL;
905
#ifdef HAVE_NCURSESW
906
wchar_t *wstr = NULL;
907
#endif
908
attr_t attr_old = A_NORMAL;
909
int use_xy = group_left_1, use_attr = group_right_1;
910
const char *funcname;
911
912
#ifdef HAVE_NCURSESW
913
strtype = PyCurses_ConvertToString(self, str, &bytesobj, &wstr);
914
#else
915
strtype = PyCurses_ConvertToString(self, str, &bytesobj, NULL);
916
#endif
917
if (strtype == 0)
918
return NULL;
919
920
if (use_attr) {
921
attr_old = getattrs(self->win);
922
(void)wattrset(self->win,attr);
923
}
924
#ifdef HAVE_NCURSESW
925
if (strtype == 2) {
926
funcname = "addnwstr";
927
if (use_xy)
928
rtn = mvwaddnwstr(self->win,y,x,wstr,n);
929
else
930
rtn = waddnwstr(self->win,wstr,n);
931
PyMem_Free(wstr);
932
}
933
else
934
#endif
935
{
936
const char *str = PyBytes_AS_STRING(bytesobj);
937
funcname = "addnstr";
938
if (use_xy)
939
rtn = mvwaddnstr(self->win,y,x,str,n);
940
else
941
rtn = waddnstr(self->win,str,n);
942
Py_DECREF(bytesobj);
943
}
944
if (use_attr)
945
(void)wattrset(self->win,attr_old);
946
return PyCursesCheckERR(rtn, funcname);
947
}
948
949
/*[clinic input]
950
_curses.window.bkgd
951
952
ch: object
953
Background character.
954
attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
955
Background attributes.
956
/
957
958
Set the background property of the window.
959
[clinic start generated code]*/
960
961
static PyObject *
962
_curses_window_bkgd_impl(PyCursesWindowObject *self, PyObject *ch, long attr)
963
/*[clinic end generated code: output=058290afb2cf4034 input=634015bcb339283d]*/
964
{
965
chtype bkgd;
966
967
if (!PyCurses_ConvertToChtype(self, ch, &bkgd))
968
return NULL;
969
970
return PyCursesCheckERR(wbkgd(self->win, bkgd | attr), "bkgd");
971
}
972
973
/*[clinic input]
974
_curses.window.attroff
975
976
attr: long
977
/
978
979
Remove attribute attr from the "background" set.
980
[clinic start generated code]*/
981
982
static PyObject *
983
_curses_window_attroff_impl(PyCursesWindowObject *self, long attr)
984
/*[clinic end generated code: output=8a2fcd4df682fc64 input=786beedf06a7befe]*/
985
{
986
return PyCursesCheckERR(wattroff(self->win, (attr_t)attr), "attroff");
987
}
988
989
/*[clinic input]
990
_curses.window.attron
991
992
attr: long
993
/
994
995
Add attribute attr from the "background" set.
996
[clinic start generated code]*/
997
998
static PyObject *
999
_curses_window_attron_impl(PyCursesWindowObject *self, long attr)
1000
/*[clinic end generated code: output=7afea43b237fa870 input=5a88fba7b1524f32]*/
1001
{
1002
return PyCursesCheckERR(wattron(self->win, (attr_t)attr), "attron");
1003
}
1004
1005
/*[clinic input]
1006
_curses.window.attrset
1007
1008
attr: long
1009
/
1010
1011
Set the "background" set of attributes.
1012
[clinic start generated code]*/
1013
1014
static PyObject *
1015
_curses_window_attrset_impl(PyCursesWindowObject *self, long attr)
1016
/*[clinic end generated code: output=84e379bff20c0433 input=42e400c0d0154ab5]*/
1017
{
1018
return PyCursesCheckERR(wattrset(self->win, (attr_t)attr), "attrset");
1019
}
1020
1021
/*[clinic input]
1022
_curses.window.bkgdset
1023
1024
ch: object
1025
Background character.
1026
attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
1027
Background attributes.
1028
/
1029
1030
Set the window's background.
1031
[clinic start generated code]*/
1032
1033
static PyObject *
1034
_curses_window_bkgdset_impl(PyCursesWindowObject *self, PyObject *ch,
1035
long attr)
1036
/*[clinic end generated code: output=8cb994fc4d7e2496 input=e09c682425c9e45b]*/
1037
{
1038
chtype bkgd;
1039
1040
if (!PyCurses_ConvertToChtype(self, ch, &bkgd))
1041
return NULL;
1042
1043
wbkgdset(self->win, bkgd | attr);
1044
return PyCursesCheckERR(0, "bkgdset");
1045
}
1046
1047
/*[clinic input]
1048
_curses.window.border
1049
1050
ls: object(c_default="NULL") = _curses.ACS_VLINE
1051
Left side.
1052
rs: object(c_default="NULL") = _curses.ACS_VLINE
1053
Right side.
1054
ts: object(c_default="NULL") = _curses.ACS_HLINE
1055
Top side.
1056
bs: object(c_default="NULL") = _curses.ACS_HLINE
1057
Bottom side.
1058
tl: object(c_default="NULL") = _curses.ACS_ULCORNER
1059
Upper-left corner.
1060
tr: object(c_default="NULL") = _curses.ACS_URCORNER
1061
Upper-right corner.
1062
bl: object(c_default="NULL") = _curses.ACS_LLCORNER
1063
Bottom-left corner.
1064
br: object(c_default="NULL") = _curses.ACS_LRCORNER
1065
Bottom-right corner.
1066
/
1067
1068
Draw a border around the edges of the window.
1069
1070
Each parameter specifies the character to use for a specific part of the
1071
border. The characters can be specified as integers or as one-character
1072
strings. A 0 value for any parameter will cause the default character to be
1073
used for that parameter.
1074
[clinic start generated code]*/
1075
1076
static PyObject *
1077
_curses_window_border_impl(PyCursesWindowObject *self, PyObject *ls,
1078
PyObject *rs, PyObject *ts, PyObject *bs,
1079
PyObject *tl, PyObject *tr, PyObject *bl,
1080
PyObject *br)
1081
/*[clinic end generated code: output=670ef38d3d7c2aa3 input=e015f735d67a240b]*/
1082
{
1083
chtype ch[8];
1084
int i;
1085
1086
/* Clear the array of parameters */
1087
for(i=0; i<8; i++)
1088
ch[i] = 0;
1089
1090
#define CONVERTTOCHTYPE(obj, i) \
1091
if ((obj) != NULL && !PyCurses_ConvertToChtype(self, (obj), &ch[(i)])) \
1092
return NULL;
1093
1094
CONVERTTOCHTYPE(ls, 0);
1095
CONVERTTOCHTYPE(rs, 1);
1096
CONVERTTOCHTYPE(ts, 2);
1097
CONVERTTOCHTYPE(bs, 3);
1098
CONVERTTOCHTYPE(tl, 4);
1099
CONVERTTOCHTYPE(tr, 5);
1100
CONVERTTOCHTYPE(bl, 6);
1101
CONVERTTOCHTYPE(br, 7);
1102
1103
#undef CONVERTTOCHTYPE
1104
1105
wborder(self->win,
1106
ch[0], ch[1], ch[2], ch[3],
1107
ch[4], ch[5], ch[6], ch[7]);
1108
Py_RETURN_NONE;
1109
}
1110
1111
/*[clinic input]
1112
_curses.window.box
1113
1114
[
1115
verch: object(c_default="_PyLong_GetZero()") = 0
1116
Left and right side.
1117
horch: object(c_default="_PyLong_GetZero()") = 0
1118
Top and bottom side.
1119
]
1120
/
1121
1122
Draw a border around the edges of the window.
1123
1124
Similar to border(), but both ls and rs are verch and both ts and bs are
1125
horch. The default corner characters are always used by this function.
1126
[clinic start generated code]*/
1127
1128
static PyObject *
1129
_curses_window_box_impl(PyCursesWindowObject *self, int group_right_1,
1130
PyObject *verch, PyObject *horch)
1131
/*[clinic end generated code: output=f3fcb038bb287192 input=f00435f9c8c98f60]*/
1132
{
1133
chtype ch1 = 0, ch2 = 0;
1134
if (group_right_1) {
1135
if (!PyCurses_ConvertToChtype(self, verch, &ch1)) {
1136
return NULL;
1137
}
1138
if (!PyCurses_ConvertToChtype(self, horch, &ch2)) {
1139
return NULL;
1140
}
1141
}
1142
box(self->win,ch1,ch2);
1143
Py_RETURN_NONE;
1144
}
1145
1146
#if defined(HAVE_NCURSES_H) || defined(MVWDELCH_IS_EXPRESSION)
1147
#define py_mvwdelch mvwdelch
1148
#else
1149
int py_mvwdelch(WINDOW *w, int y, int x)
1150
{
1151
mvwdelch(w,y,x);
1152
/* On HP/UX, mvwdelch already returns. On other systems,
1153
we may well run into this return statement. */
1154
return 0;
1155
}
1156
#endif
1157
1158
#if defined(HAVE_CURSES_IS_PAD)
1159
#define py_is_pad(win) is_pad(win)
1160
#elif defined(WINDOW_HAS_FLAGS)
1161
#define py_is_pad(win) ((win) ? ((win)->_flags & _ISPAD) != 0 : FALSE)
1162
#endif
1163
1164
/* chgat, added by Fabian Kreutz <fabian.kreutz at gmx.net> */
1165
#ifdef HAVE_CURSES_WCHGAT
1166
/*[-clinic input]
1167
_curses.window.chgat
1168
1169
[
1170
y: int
1171
Y-coordinate.
1172
x: int
1173
X-coordinate.
1174
]
1175
1176
n: int = -1
1177
Number of characters.
1178
1179
attr: long
1180
Attributes for characters.
1181
/
1182
1183
Set the attributes of characters.
1184
1185
Set the attributes of num characters at the current cursor position, or at
1186
position (y, x) if supplied. If no value of num is given or num = -1, the
1187
attribute will be set on all the characters to the end of the line. This
1188
function does not move the cursor. The changed line will be touched using
1189
the touchline() method so that the contents will be redisplayed by the next
1190
window refresh.
1191
[-clinic start generated code]*/
1192
static PyObject *
1193
PyCursesWindow_ChgAt(PyCursesWindowObject *self, PyObject *args)
1194
{
1195
int rtn;
1196
int x, y;
1197
int num = -1;
1198
short color;
1199
attr_t attr = A_NORMAL;
1200
long lattr;
1201
int use_xy = FALSE;
1202
1203
switch (PyTuple_Size(args)) {
1204
case 1:
1205
if (!PyArg_ParseTuple(args,"l;attr", &lattr))
1206
return NULL;
1207
attr = lattr;
1208
break;
1209
case 2:
1210
if (!PyArg_ParseTuple(args,"il;n,attr", &num, &lattr))
1211
return NULL;
1212
attr = lattr;
1213
break;
1214
case 3:
1215
if (!PyArg_ParseTuple(args,"iil;int,int,attr", &y, &x, &lattr))
1216
return NULL;
1217
attr = lattr;
1218
use_xy = TRUE;
1219
break;
1220
case 4:
1221
if (!PyArg_ParseTuple(args,"iiil;int,int,n,attr", &y, &x, &num, &lattr))
1222
return NULL;
1223
attr = lattr;
1224
use_xy = TRUE;
1225
break;
1226
default:
1227
PyErr_SetString(PyExc_TypeError, "chgat requires 1 to 4 arguments");
1228
return NULL;
1229
}
1230
1231
color = (short) PAIR_NUMBER(attr);
1232
attr = attr & A_ATTRIBUTES;
1233
1234
if (use_xy) {
1235
rtn = mvwchgat(self->win,y,x,num,attr,color,NULL);
1236
touchline(self->win,y,1);
1237
} else {
1238
getyx(self->win,y,x);
1239
rtn = wchgat(self->win,num,attr,color,NULL);
1240
touchline(self->win,y,1);
1241
}
1242
return PyCursesCheckERR(rtn, "chgat");
1243
}
1244
#endif
1245
1246
/*[clinic input]
1247
_curses.window.delch
1248
1249
[
1250
y: int
1251
Y-coordinate.
1252
x: int
1253
X-coordinate.
1254
]
1255
/
1256
1257
Delete any character at (y, x).
1258
[clinic start generated code]*/
1259
1260
static PyObject *
1261
_curses_window_delch_impl(PyCursesWindowObject *self, int group_right_1,
1262
int y, int x)
1263
/*[clinic end generated code: output=22e77bb9fa11b461 input=d2f79e630a4fc6d0]*/
1264
{
1265
if (!group_right_1) {
1266
return PyCursesCheckERR(wdelch(self->win), "wdelch");
1267
}
1268
else {
1269
return PyCursesCheckERR(py_mvwdelch(self->win, y, x), "mvwdelch");
1270
}
1271
}
1272
1273
/*[clinic input]
1274
_curses.window.derwin
1275
1276
[
1277
nlines: int = 0
1278
Height.
1279
ncols: int = 0
1280
Width.
1281
]
1282
begin_y: int
1283
Top side y-coordinate.
1284
begin_x: int
1285
Left side x-coordinate.
1286
/
1287
1288
Create a sub-window (window-relative coordinates).
1289
1290
derwin() is the same as calling subwin(), except that begin_y and begin_x
1291
are relative to the origin of the window, rather than relative to the entire
1292
screen.
1293
[clinic start generated code]*/
1294
1295
static PyObject *
1296
_curses_window_derwin_impl(PyCursesWindowObject *self, int group_left_1,
1297
int nlines, int ncols, int begin_y, int begin_x)
1298
/*[clinic end generated code: output=7924b112d9f70d6e input=966d9481f7f5022e]*/
1299
{
1300
WINDOW *win;
1301
1302
win = derwin(self->win,nlines,ncols,begin_y,begin_x);
1303
1304
if (win == NULL) {
1305
PyErr_SetString(PyCursesError, catchall_NULL);
1306
return NULL;
1307
}
1308
1309
return (PyObject *)PyCursesWindow_New(win, NULL);
1310
}
1311
1312
/*[clinic input]
1313
_curses.window.echochar
1314
1315
ch: object
1316
Character to add.
1317
1318
attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
1319
Attributes for the character.
1320
/
1321
1322
Add character ch with attribute attr, and refresh.
1323
[clinic start generated code]*/
1324
1325
static PyObject *
1326
_curses_window_echochar_impl(PyCursesWindowObject *self, PyObject *ch,
1327
long attr)
1328
/*[clinic end generated code: output=13e7dd875d4b9642 input=e7f34b964e92b156]*/
1329
{
1330
chtype ch_;
1331
1332
if (!PyCurses_ConvertToChtype(self, ch, &ch_))
1333
return NULL;
1334
1335
#ifdef py_is_pad
1336
if (py_is_pad(self->win)) {
1337
return PyCursesCheckERR(pechochar(self->win, ch_ | (attr_t)attr),
1338
"echochar");
1339
}
1340
else
1341
#endif
1342
return PyCursesCheckERR(wechochar(self->win, ch_ | (attr_t)attr),
1343
"echochar");
1344
}
1345
1346
#ifdef NCURSES_MOUSE_VERSION
1347
/*[clinic input]
1348
_curses.window.enclose
1349
1350
y: int
1351
Y-coordinate.
1352
x: int
1353
X-coordinate.
1354
/
1355
1356
Return True if the screen-relative coordinates are enclosed by the window.
1357
[clinic start generated code]*/
1358
1359
static PyObject *
1360
_curses_window_enclose_impl(PyCursesWindowObject *self, int y, int x)
1361
/*[clinic end generated code: output=8679beef50502648 input=4fd3355d723f7bc9]*/
1362
{
1363
return PyBool_FromLong(wenclose(self->win, y, x));
1364
}
1365
#endif
1366
1367
/*[clinic input]
1368
_curses.window.getbkgd -> long
1369
1370
Return the window's current background character/attribute pair.
1371
[clinic start generated code]*/
1372
1373
static long
1374
_curses_window_getbkgd_impl(PyCursesWindowObject *self)
1375
/*[clinic end generated code: output=c52b25dc16b215c3 input=a69db882fa35426c]*/
1376
{
1377
return (long) getbkgd(self->win);
1378
}
1379
1380
/*[clinic input]
1381
_curses.window.getch -> int
1382
1383
[
1384
y: int
1385
Y-coordinate.
1386
x: int
1387
X-coordinate.
1388
]
1389
/
1390
1391
Get a character code from terminal keyboard.
1392
1393
The integer returned does not have to be in ASCII range: function keys,
1394
keypad keys and so on return numbers higher than 256. In no-delay mode, -1
1395
is returned if there is no input, else getch() waits until a key is pressed.
1396
[clinic start generated code]*/
1397
1398
static int
1399
_curses_window_getch_impl(PyCursesWindowObject *self, int group_right_1,
1400
int y, int x)
1401
/*[clinic end generated code: output=980aa6af0c0ca387 input=bb24ebfb379f991f]*/
1402
{
1403
int rtn;
1404
1405
Py_BEGIN_ALLOW_THREADS
1406
if (!group_right_1) {
1407
rtn = wgetch(self->win);
1408
}
1409
else {
1410
rtn = mvwgetch(self->win, y, x);
1411
}
1412
Py_END_ALLOW_THREADS
1413
1414
return rtn;
1415
}
1416
1417
/*[clinic input]
1418
_curses.window.getkey
1419
1420
[
1421
y: int
1422
Y-coordinate.
1423
x: int
1424
X-coordinate.
1425
]
1426
/
1427
1428
Get a character (string) from terminal keyboard.
1429
1430
Returning a string instead of an integer, as getch() does. Function keys,
1431
keypad keys and other special keys return a multibyte string containing the
1432
key name. In no-delay mode, an exception is raised if there is no input.
1433
[clinic start generated code]*/
1434
1435
static PyObject *
1436
_curses_window_getkey_impl(PyCursesWindowObject *self, int group_right_1,
1437
int y, int x)
1438
/*[clinic end generated code: output=8490a182db46b10f input=be2dee34f5cf57f8]*/
1439
{
1440
int rtn;
1441
1442
Py_BEGIN_ALLOW_THREADS
1443
if (!group_right_1) {
1444
rtn = wgetch(self->win);
1445
}
1446
else {
1447
rtn = mvwgetch(self->win, y, x);
1448
}
1449
Py_END_ALLOW_THREADS
1450
1451
if (rtn == ERR) {
1452
/* getch() returns ERR in nodelay mode */
1453
PyErr_CheckSignals();
1454
if (!PyErr_Occurred())
1455
PyErr_SetString(PyCursesError, "no input");
1456
return NULL;
1457
} else if (rtn <= 255) {
1458
#ifdef NCURSES_VERSION_MAJOR
1459
#if NCURSES_VERSION_MAJOR*100+NCURSES_VERSION_MINOR <= 507
1460
/* Work around a bug in ncurses 5.7 and earlier */
1461
if (rtn < 0) {
1462
rtn += 256;
1463
}
1464
#endif
1465
#endif
1466
return PyUnicode_FromOrdinal(rtn);
1467
} else {
1468
const char *knp = keyname(rtn);
1469
return PyUnicode_FromString((knp == NULL) ? "" : knp);
1470
}
1471
}
1472
1473
#ifdef HAVE_NCURSESW
1474
/*[clinic input]
1475
_curses.window.get_wch
1476
1477
[
1478
y: int
1479
Y-coordinate.
1480
x: int
1481
X-coordinate.
1482
]
1483
/
1484
1485
Get a wide character from terminal keyboard.
1486
1487
Return a character for most keys, or an integer for function keys,
1488
keypad keys, and other special keys.
1489
[clinic start generated code]*/
1490
1491
static PyObject *
1492
_curses_window_get_wch_impl(PyCursesWindowObject *self, int group_right_1,
1493
int y, int x)
1494
/*[clinic end generated code: output=9f4f86e91fe50ef3 input=dd7e5367fb49dc48]*/
1495
{
1496
int ct;
1497
wint_t rtn;
1498
1499
Py_BEGIN_ALLOW_THREADS
1500
if (!group_right_1) {
1501
ct = wget_wch(self->win ,&rtn);
1502
}
1503
else {
1504
ct = mvwget_wch(self->win, y, x, &rtn);
1505
}
1506
Py_END_ALLOW_THREADS
1507
1508
if (ct == ERR) {
1509
if (PyErr_CheckSignals())
1510
return NULL;
1511
1512
/* get_wch() returns ERR in nodelay mode */
1513
PyErr_SetString(PyCursesError, "no input");
1514
return NULL;
1515
}
1516
if (ct == KEY_CODE_YES)
1517
return PyLong_FromLong(rtn);
1518
else
1519
return PyUnicode_FromOrdinal(rtn);
1520
}
1521
#endif
1522
1523
/*[-clinic input]
1524
_curses.window.getstr
1525
1526
[
1527
y: int
1528
Y-coordinate.
1529
x: int
1530
X-coordinate.
1531
]
1532
n: int = 1023
1533
Maximal number of characters.
1534
/
1535
1536
Read a string from the user, with primitive line editing capacity.
1537
[-clinic start generated code]*/
1538
1539
static PyObject *
1540
PyCursesWindow_GetStr(PyCursesWindowObject *self, PyObject *args)
1541
{
1542
int x, y, n;
1543
char rtn[1024]; /* This should be big enough.. I hope */
1544
int rtn2;
1545
1546
switch (PyTuple_Size(args)) {
1547
case 0:
1548
Py_BEGIN_ALLOW_THREADS
1549
rtn2 = wgetnstr(self->win,rtn, 1023);
1550
Py_END_ALLOW_THREADS
1551
break;
1552
case 1:
1553
if (!PyArg_ParseTuple(args,"i;n", &n))
1554
return NULL;
1555
if (n < 0) {
1556
PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1557
return NULL;
1558
}
1559
Py_BEGIN_ALLOW_THREADS
1560
rtn2 = wgetnstr(self->win, rtn, Py_MIN(n, 1023));
1561
Py_END_ALLOW_THREADS
1562
break;
1563
case 2:
1564
if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1565
return NULL;
1566
Py_BEGIN_ALLOW_THREADS
1567
#ifdef STRICT_SYSV_CURSES
1568
rtn2 = wmove(self->win,y,x)==ERR ? ERR : wgetnstr(self->win, rtn, 1023);
1569
#else
1570
rtn2 = mvwgetnstr(self->win,y,x,rtn, 1023);
1571
#endif
1572
Py_END_ALLOW_THREADS
1573
break;
1574
case 3:
1575
if (!PyArg_ParseTuple(args,"iii;y,x,n", &y, &x, &n))
1576
return NULL;
1577
if (n < 0) {
1578
PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1579
return NULL;
1580
}
1581
#ifdef STRICT_SYSV_CURSES
1582
Py_BEGIN_ALLOW_THREADS
1583
rtn2 = wmove(self->win,y,x)==ERR ? ERR :
1584
wgetnstr(self->win, rtn, Py_MIN(n, 1023));
1585
Py_END_ALLOW_THREADS
1586
#else
1587
Py_BEGIN_ALLOW_THREADS
1588
rtn2 = mvwgetnstr(self->win, y, x, rtn, Py_MIN(n, 1023));
1589
Py_END_ALLOW_THREADS
1590
#endif
1591
break;
1592
default:
1593
PyErr_SetString(PyExc_TypeError, "getstr requires 0 to 3 arguments");
1594
return NULL;
1595
}
1596
if (rtn2 == ERR)
1597
rtn[0] = 0;
1598
return PyBytes_FromString(rtn);
1599
}
1600
1601
/*[clinic input]
1602
_curses.window.hline
1603
1604
[
1605
y: int
1606
Starting Y-coordinate.
1607
x: int
1608
Starting X-coordinate.
1609
]
1610
1611
ch: object
1612
Character to draw.
1613
n: int
1614
Line length.
1615
1616
[
1617
attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
1618
Attributes for the characters.
1619
]
1620
/
1621
1622
Display a horizontal line.
1623
[clinic start generated code]*/
1624
1625
static PyObject *
1626
_curses_window_hline_impl(PyCursesWindowObject *self, int group_left_1,
1627
int y, int x, PyObject *ch, int n,
1628
int group_right_1, long attr)
1629
/*[clinic end generated code: output=c00d489d61fc9eef input=81a4dea47268163e]*/
1630
{
1631
chtype ch_;
1632
1633
if (!PyCurses_ConvertToChtype(self, ch, &ch_))
1634
return NULL;
1635
if (group_left_1) {
1636
if (wmove(self->win, y, x) == ERR) {
1637
return PyCursesCheckERR(ERR, "wmove");
1638
}
1639
}
1640
return PyCursesCheckERR(whline(self->win, ch_ | (attr_t)attr, n), "hline");
1641
}
1642
1643
/*[clinic input]
1644
_curses.window.insch
1645
1646
[
1647
y: int
1648
Y-coordinate.
1649
x: int
1650
X-coordinate.
1651
]
1652
1653
ch: object
1654
Character to insert.
1655
1656
[
1657
attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
1658
Attributes for the character.
1659
]
1660
/
1661
1662
Insert a character before the current or specified position.
1663
1664
All characters to the right of the cursor are shifted one position right, with
1665
the rightmost characters on the line being lost.
1666
[clinic start generated code]*/
1667
1668
static PyObject *
1669
_curses_window_insch_impl(PyCursesWindowObject *self, int group_left_1,
1670
int y, int x, PyObject *ch, int group_right_1,
1671
long attr)
1672
/*[clinic end generated code: output=ade8cfe3a3bf3e34 input=336342756ee19812]*/
1673
{
1674
int rtn;
1675
chtype ch_ = 0;
1676
1677
if (!PyCurses_ConvertToChtype(self, ch, &ch_))
1678
return NULL;
1679
1680
if (!group_left_1) {
1681
rtn = winsch(self->win, ch_ | (attr_t)attr);
1682
}
1683
else {
1684
rtn = mvwinsch(self->win, y, x, ch_ | (attr_t)attr);
1685
}
1686
1687
return PyCursesCheckERR(rtn, "insch");
1688
}
1689
1690
/*[clinic input]
1691
_curses.window.inch -> unsigned_long
1692
1693
[
1694
y: int
1695
Y-coordinate.
1696
x: int
1697
X-coordinate.
1698
]
1699
/
1700
1701
Return the character at the given position in the window.
1702
1703
The bottom 8 bits are the character proper, and upper bits are the attributes.
1704
[clinic start generated code]*/
1705
1706
static unsigned long
1707
_curses_window_inch_impl(PyCursesWindowObject *self, int group_right_1,
1708
int y, int x)
1709
/*[clinic end generated code: output=6c4719fe978fe86a input=fac23ee11e3b3a66]*/
1710
{
1711
unsigned long rtn;
1712
1713
if (!group_right_1) {
1714
rtn = winch(self->win);
1715
}
1716
else {
1717
rtn = mvwinch(self->win, y, x);
1718
}
1719
1720
return rtn;
1721
}
1722
1723
/*[-clinic input]
1724
_curses.window.instr
1725
1726
[
1727
y: int
1728
Y-coordinate.
1729
x: int
1730
X-coordinate.
1731
]
1732
n: int = 1023
1733
Maximal number of characters.
1734
/
1735
1736
Return a string of characters, extracted from the window.
1737
1738
Return a string of characters, extracted from the window starting at the
1739
current cursor position, or at y, x if specified. Attributes are stripped
1740
from the characters. If n is specified, instr() returns a string at most
1741
n characters long (exclusive of the trailing NUL).
1742
[-clinic start generated code]*/
1743
static PyObject *
1744
PyCursesWindow_InStr(PyCursesWindowObject *self, PyObject *args)
1745
{
1746
int x, y, n;
1747
char rtn[1024]; /* This should be big enough.. I hope */
1748
int rtn2;
1749
1750
switch (PyTuple_Size(args)) {
1751
case 0:
1752
rtn2 = winnstr(self->win,rtn, 1023);
1753
break;
1754
case 1:
1755
if (!PyArg_ParseTuple(args,"i;n", &n))
1756
return NULL;
1757
if (n < 0) {
1758
PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1759
return NULL;
1760
}
1761
rtn2 = winnstr(self->win, rtn, Py_MIN(n, 1023));
1762
break;
1763
case 2:
1764
if (!PyArg_ParseTuple(args,"ii;y,x",&y,&x))
1765
return NULL;
1766
rtn2 = mvwinnstr(self->win,y,x,rtn,1023);
1767
break;
1768
case 3:
1769
if (!PyArg_ParseTuple(args, "iii;y,x,n", &y, &x, &n))
1770
return NULL;
1771
if (n < 0) {
1772
PyErr_SetString(PyExc_ValueError, "'n' must be nonnegative");
1773
return NULL;
1774
}
1775
rtn2 = mvwinnstr(self->win, y, x, rtn, Py_MIN(n,1023));
1776
break;
1777
default:
1778
PyErr_SetString(PyExc_TypeError, "instr requires 0 or 3 arguments");
1779
return NULL;
1780
}
1781
if (rtn2 == ERR)
1782
rtn[0] = 0;
1783
return PyBytes_FromString(rtn);
1784
}
1785
1786
/*[clinic input]
1787
_curses.window.insstr
1788
1789
[
1790
y: int
1791
Y-coordinate.
1792
x: int
1793
X-coordinate.
1794
]
1795
1796
str: object
1797
String to insert.
1798
1799
[
1800
attr: long
1801
Attributes for characters.
1802
]
1803
/
1804
1805
Insert the string before the current or specified position.
1806
1807
Insert a character string (as many characters as will fit on the line)
1808
before the character under the cursor. All characters to the right of
1809
the cursor are shifted right, with the rightmost characters on the line
1810
being lost. The cursor position does not change (after moving to y, x,
1811
if specified).
1812
[clinic start generated code]*/
1813
1814
static PyObject *
1815
_curses_window_insstr_impl(PyCursesWindowObject *self, int group_left_1,
1816
int y, int x, PyObject *str, int group_right_1,
1817
long attr)
1818
/*[clinic end generated code: output=c259a5265ad0b777 input=6827cddc6340a7f3]*/
1819
{
1820
int rtn;
1821
int strtype;
1822
PyObject *bytesobj = NULL;
1823
#ifdef HAVE_NCURSESW
1824
wchar_t *wstr = NULL;
1825
#endif
1826
attr_t attr_old = A_NORMAL;
1827
int use_xy = group_left_1, use_attr = group_right_1;
1828
const char *funcname;
1829
1830
#ifdef HAVE_NCURSESW
1831
strtype = PyCurses_ConvertToString(self, str, &bytesobj, &wstr);
1832
#else
1833
strtype = PyCurses_ConvertToString(self, str, &bytesobj, NULL);
1834
#endif
1835
if (strtype == 0)
1836
return NULL;
1837
1838
if (use_attr) {
1839
attr_old = getattrs(self->win);
1840
(void)wattrset(self->win, (attr_t)attr);
1841
}
1842
#ifdef HAVE_NCURSESW
1843
if (strtype == 2) {
1844
funcname = "inswstr";
1845
if (use_xy)
1846
rtn = mvwins_wstr(self->win,y,x,wstr);
1847
else
1848
rtn = wins_wstr(self->win,wstr);
1849
PyMem_Free(wstr);
1850
}
1851
else
1852
#endif
1853
{
1854
const char *str = PyBytes_AS_STRING(bytesobj);
1855
funcname = "insstr";
1856
if (use_xy)
1857
rtn = mvwinsstr(self->win,y,x,str);
1858
else
1859
rtn = winsstr(self->win,str);
1860
Py_DECREF(bytesobj);
1861
}
1862
if (use_attr)
1863
(void)wattrset(self->win,attr_old);
1864
return PyCursesCheckERR(rtn, funcname);
1865
}
1866
1867
/*[clinic input]
1868
_curses.window.insnstr
1869
1870
[
1871
y: int
1872
Y-coordinate.
1873
x: int
1874
X-coordinate.
1875
]
1876
1877
str: object
1878
String to insert.
1879
1880
n: int
1881
Maximal number of characters.
1882
1883
[
1884
attr: long
1885
Attributes for characters.
1886
]
1887
/
1888
1889
Insert at most n characters of the string.
1890
1891
Insert a character string (as many characters as will fit on the line)
1892
before the character under the cursor, up to n characters. If n is zero
1893
or negative, the entire string is inserted. All characters to the right
1894
of the cursor are shifted right, with the rightmost characters on the line
1895
being lost. The cursor position does not change (after moving to y, x, if
1896
specified).
1897
[clinic start generated code]*/
1898
1899
static PyObject *
1900
_curses_window_insnstr_impl(PyCursesWindowObject *self, int group_left_1,
1901
int y, int x, PyObject *str, int n,
1902
int group_right_1, long attr)
1903
/*[clinic end generated code: output=971a32ea6328ec8b input=70fa0cd543901a4c]*/
1904
{
1905
int rtn;
1906
int strtype;
1907
PyObject *bytesobj = NULL;
1908
#ifdef HAVE_NCURSESW
1909
wchar_t *wstr = NULL;
1910
#endif
1911
attr_t attr_old = A_NORMAL;
1912
int use_xy = group_left_1, use_attr = group_right_1;
1913
const char *funcname;
1914
1915
#ifdef HAVE_NCURSESW
1916
strtype = PyCurses_ConvertToString(self, str, &bytesobj, &wstr);
1917
#else
1918
strtype = PyCurses_ConvertToString(self, str, &bytesobj, NULL);
1919
#endif
1920
if (strtype == 0)
1921
return NULL;
1922
1923
if (use_attr) {
1924
attr_old = getattrs(self->win);
1925
(void)wattrset(self->win, (attr_t)attr);
1926
}
1927
#ifdef HAVE_NCURSESW
1928
if (strtype == 2) {
1929
funcname = "insn_wstr";
1930
if (use_xy)
1931
rtn = mvwins_nwstr(self->win,y,x,wstr,n);
1932
else
1933
rtn = wins_nwstr(self->win,wstr,n);
1934
PyMem_Free(wstr);
1935
}
1936
else
1937
#endif
1938
{
1939
const char *str = PyBytes_AS_STRING(bytesobj);
1940
funcname = "insnstr";
1941
if (use_xy)
1942
rtn = mvwinsnstr(self->win,y,x,str,n);
1943
else
1944
rtn = winsnstr(self->win,str,n);
1945
Py_DECREF(bytesobj);
1946
}
1947
if (use_attr)
1948
(void)wattrset(self->win,attr_old);
1949
return PyCursesCheckERR(rtn, funcname);
1950
}
1951
1952
/*[clinic input]
1953
_curses.window.is_linetouched
1954
1955
line: int
1956
Line number.
1957
/
1958
1959
Return True if the specified line was modified, otherwise return False.
1960
1961
Raise a curses.error exception if line is not valid for the given window.
1962
[clinic start generated code]*/
1963
1964
static PyObject *
1965
_curses_window_is_linetouched_impl(PyCursesWindowObject *self, int line)
1966
/*[clinic end generated code: output=ad4a4edfee2db08c input=a7be0c189f243914]*/
1967
{
1968
int erg;
1969
erg = is_linetouched(self->win, line);
1970
if (erg == ERR) {
1971
PyErr_SetString(PyExc_TypeError,
1972
"is_linetouched: line number outside of boundaries");
1973
return NULL;
1974
}
1975
return PyBool_FromLong(erg);
1976
}
1977
1978
#ifdef py_is_pad
1979
/*[clinic input]
1980
_curses.window.noutrefresh
1981
1982
[
1983
pminrow: int
1984
pmincol: int
1985
sminrow: int
1986
smincol: int
1987
smaxrow: int
1988
smaxcol: int
1989
]
1990
/
1991
1992
Mark for refresh but wait.
1993
1994
This function updates the data structure representing the desired state of the
1995
window, but does not force an update of the physical screen. To accomplish
1996
that, call doupdate().
1997
[clinic start generated code]*/
1998
1999
static PyObject *
2000
_curses_window_noutrefresh_impl(PyCursesWindowObject *self,
2001
int group_right_1, int pminrow, int pmincol,
2002
int sminrow, int smincol, int smaxrow,
2003
int smaxcol)
2004
/*[clinic end generated code: output=809a1f3c6a03e23e input=3e56898388cd739e]*/
2005
#else
2006
/*[clinic input]
2007
_curses.window.noutrefresh
2008
2009
Mark for refresh but wait.
2010
2011
This function updates the data structure representing the desired state of the
2012
window, but does not force an update of the physical screen. To accomplish
2013
that, call doupdate().
2014
[clinic start generated code]*/
2015
2016
static PyObject *
2017
_curses_window_noutrefresh_impl(PyCursesWindowObject *self)
2018
/*[clinic end generated code: output=6ef6dec666643fee input=876902e3fa431dbd]*/
2019
#endif
2020
{
2021
int rtn;
2022
2023
#ifdef py_is_pad
2024
if (py_is_pad(self->win)) {
2025
if (!group_right_1) {
2026
PyErr_SetString(PyCursesError,
2027
"noutrefresh() called for a pad "
2028
"requires 6 arguments");
2029
return NULL;
2030
}
2031
Py_BEGIN_ALLOW_THREADS
2032
rtn = pnoutrefresh(self->win, pminrow, pmincol,
2033
sminrow, smincol, smaxrow, smaxcol);
2034
Py_END_ALLOW_THREADS
2035
return PyCursesCheckERR(rtn, "pnoutrefresh");
2036
}
2037
if (group_right_1) {
2038
PyErr_SetString(PyExc_TypeError,
2039
"noutrefresh() takes no arguments (6 given)");
2040
return NULL;
2041
}
2042
#endif
2043
Py_BEGIN_ALLOW_THREADS
2044
rtn = wnoutrefresh(self->win);
2045
Py_END_ALLOW_THREADS
2046
return PyCursesCheckERR(rtn, "wnoutrefresh");
2047
}
2048
2049
/*[clinic input]
2050
_curses.window.overlay
2051
2052
destwin: object(type="PyCursesWindowObject *", subclass_of="&PyCursesWindow_Type")
2053
2054
[
2055
sminrow: int
2056
smincol: int
2057
dminrow: int
2058
dmincol: int
2059
dmaxrow: int
2060
dmaxcol: int
2061
]
2062
/
2063
2064
Overlay the window on top of destwin.
2065
2066
The windows need not be the same size, only the overlapping region is copied.
2067
This copy is non-destructive, which means that the current background
2068
character does not overwrite the old contents of destwin.
2069
2070
To get fine-grained control over the copied region, the second form of
2071
overlay() can be used. sminrow and smincol are the upper-left coordinates
2072
of the source window, and the other variables mark a rectangle in the
2073
destination window.
2074
[clinic start generated code]*/
2075
2076
static PyObject *
2077
_curses_window_overlay_impl(PyCursesWindowObject *self,
2078
PyCursesWindowObject *destwin, int group_right_1,
2079
int sminrow, int smincol, int dminrow,
2080
int dmincol, int dmaxrow, int dmaxcol)
2081
/*[clinic end generated code: output=82bb2c4cb443ca58 input=7edd23ad22cc1984]*/
2082
{
2083
int rtn;
2084
2085
if (group_right_1) {
2086
rtn = copywin(self->win, destwin->win, sminrow, smincol,
2087
dminrow, dmincol, dmaxrow, dmaxcol, TRUE);
2088
return PyCursesCheckERR(rtn, "copywin");
2089
}
2090
else {
2091
rtn = overlay(self->win, destwin->win);
2092
return PyCursesCheckERR(rtn, "overlay");
2093
}
2094
}
2095
2096
/*[clinic input]
2097
_curses.window.overwrite
2098
2099
destwin: object(type="PyCursesWindowObject *", subclass_of="&PyCursesWindow_Type")
2100
2101
[
2102
sminrow: int
2103
smincol: int
2104
dminrow: int
2105
dmincol: int
2106
dmaxrow: int
2107
dmaxcol: int
2108
]
2109
/
2110
2111
Overwrite the window on top of destwin.
2112
2113
The windows need not be the same size, in which case only the overlapping
2114
region is copied. This copy is destructive, which means that the current
2115
background character overwrites the old contents of destwin.
2116
2117
To get fine-grained control over the copied region, the second form of
2118
overwrite() can be used. sminrow and smincol are the upper-left coordinates
2119
of the source window, the other variables mark a rectangle in the destination
2120
window.
2121
[clinic start generated code]*/
2122
2123
static PyObject *
2124
_curses_window_overwrite_impl(PyCursesWindowObject *self,
2125
PyCursesWindowObject *destwin,
2126
int group_right_1, int sminrow, int smincol,
2127
int dminrow, int dmincol, int dmaxrow,
2128
int dmaxcol)
2129
/*[clinic end generated code: output=12ae007d1681be28 input=ea5de1b35cd948e0]*/
2130
{
2131
int rtn;
2132
2133
if (group_right_1) {
2134
rtn = copywin(self->win, destwin->win, sminrow, smincol,
2135
dminrow, dmincol, dmaxrow, dmaxcol, FALSE);
2136
return PyCursesCheckERR(rtn, "copywin");
2137
}
2138
else {
2139
rtn = overwrite(self->win, destwin->win);
2140
return PyCursesCheckERR(rtn, "overwrite");
2141
}
2142
}
2143
2144
/*[clinic input]
2145
_curses.window.putwin
2146
2147
file: object
2148
/
2149
2150
Write all data associated with the window into the provided file object.
2151
2152
This information can be later retrieved using the getwin() function.
2153
[clinic start generated code]*/
2154
2155
static PyObject *
2156
_curses_window_putwin(PyCursesWindowObject *self, PyObject *file)
2157
/*[clinic end generated code: output=3a25e2a5e7a040ac input=0608648e09c8ea0a]*/
2158
{
2159
/* We have to simulate this by writing to a temporary FILE*,
2160
then reading back, then writing to the argument file. */
2161
FILE *fp;
2162
PyObject *res = NULL;
2163
2164
fp = tmpfile();
2165
if (fp == NULL)
2166
return PyErr_SetFromErrno(PyExc_OSError);
2167
if (_Py_set_inheritable(fileno(fp), 0, NULL) < 0)
2168
goto exit;
2169
res = PyCursesCheckERR(putwin(self->win, fp), "putwin");
2170
if (res == NULL)
2171
goto exit;
2172
fseek(fp, 0, 0);
2173
while (1) {
2174
char buf[BUFSIZ];
2175
Py_ssize_t n = fread(buf, 1, BUFSIZ, fp);
2176
2177
if (n <= 0)
2178
break;
2179
Py_DECREF(res);
2180
res = PyObject_CallMethod(file, "write", "y#", buf, n);
2181
if (res == NULL)
2182
break;
2183
}
2184
2185
exit:
2186
fclose(fp);
2187
return res;
2188
}
2189
2190
/*[clinic input]
2191
_curses.window.redrawln
2192
2193
beg: int
2194
Starting line number.
2195
num: int
2196
The number of lines.
2197
/
2198
2199
Mark the specified lines corrupted.
2200
2201
They should be completely redrawn on the next refresh() call.
2202
[clinic start generated code]*/
2203
2204
static PyObject *
2205
_curses_window_redrawln_impl(PyCursesWindowObject *self, int beg, int num)
2206
/*[clinic end generated code: output=ea216e334f9ce1b4 input=152155e258a77a7a]*/
2207
{
2208
return PyCursesCheckERR(wredrawln(self->win,beg,num), "redrawln");
2209
}
2210
2211
/*[clinic input]
2212
_curses.window.refresh
2213
2214
[
2215
pminrow: int
2216
pmincol: int
2217
sminrow: int
2218
smincol: int
2219
smaxrow: int
2220
smaxcol: int
2221
]
2222
/
2223
2224
Update the display immediately.
2225
2226
Synchronize actual screen with previous drawing/deleting methods.
2227
The 6 optional arguments can only be specified when the window is a pad
2228
created with newpad(). The additional parameters are needed to indicate
2229
what part of the pad and screen are involved. pminrow and pmincol specify
2230
the upper left-hand corner of the rectangle to be displayed in the pad.
2231
sminrow, smincol, smaxrow, and smaxcol specify the edges of the rectangle to
2232
be displayed on the screen. The lower right-hand corner of the rectangle to
2233
be displayed in the pad is calculated from the screen coordinates, since the
2234
rectangles must be the same size. Both rectangles must be entirely contained
2235
within their respective structures. Negative values of pminrow, pmincol,
2236
sminrow, or smincol are treated as if they were zero.
2237
[clinic start generated code]*/
2238
2239
static PyObject *
2240
_curses_window_refresh_impl(PyCursesWindowObject *self, int group_right_1,
2241
int pminrow, int pmincol, int sminrow,
2242
int smincol, int smaxrow, int smaxcol)
2243
/*[clinic end generated code: output=42199543115e6e63 input=95e01cb5ffc635d0]*/
2244
{
2245
int rtn;
2246
2247
#ifdef py_is_pad
2248
if (py_is_pad(self->win)) {
2249
if (!group_right_1) {
2250
PyErr_SetString(PyCursesError,
2251
"refresh() for a pad requires 6 arguments");
2252
return NULL;
2253
}
2254
Py_BEGIN_ALLOW_THREADS
2255
rtn = prefresh(self->win, pminrow, pmincol,
2256
sminrow, smincol, smaxrow, smaxcol);
2257
Py_END_ALLOW_THREADS
2258
return PyCursesCheckERR(rtn, "prefresh");
2259
}
2260
#endif
2261
if (group_right_1) {
2262
PyErr_SetString(PyExc_TypeError,
2263
"refresh() takes no arguments (6 given)");
2264
return NULL;
2265
}
2266
Py_BEGIN_ALLOW_THREADS
2267
rtn = wrefresh(self->win);
2268
Py_END_ALLOW_THREADS
2269
return PyCursesCheckERR(rtn, "prefresh");
2270
}
2271
2272
/*[clinic input]
2273
_curses.window.setscrreg
2274
2275
top: int
2276
First line number.
2277
bottom: int
2278
Last line number.
2279
/
2280
2281
Define a software scrolling region.
2282
2283
All scrolling actions will take place in this region.
2284
[clinic start generated code]*/
2285
2286
static PyObject *
2287
_curses_window_setscrreg_impl(PyCursesWindowObject *self, int top,
2288
int bottom)
2289
/*[clinic end generated code: output=486ab5db218d2b1a input=1b517b986838bf0e]*/
2290
{
2291
return PyCursesCheckERR(wsetscrreg(self->win, top, bottom), "wsetscrreg");
2292
}
2293
2294
/*[clinic input]
2295
_curses.window.subwin
2296
2297
[
2298
nlines: int = 0
2299
Height.
2300
ncols: int = 0
2301
Width.
2302
]
2303
begin_y: int
2304
Top side y-coordinate.
2305
begin_x: int
2306
Left side x-coordinate.
2307
/
2308
2309
Create a sub-window (screen-relative coordinates).
2310
2311
By default, the sub-window will extend from the specified position to the
2312
lower right corner of the window.
2313
[clinic start generated code]*/
2314
2315
static PyObject *
2316
_curses_window_subwin_impl(PyCursesWindowObject *self, int group_left_1,
2317
int nlines, int ncols, int begin_y, int begin_x)
2318
/*[clinic end generated code: output=93e898afc348f59a input=2129fa47fd57721c]*/
2319
{
2320
WINDOW *win;
2321
2322
/* printf("Subwin: %i %i %i %i \n", nlines, ncols, begin_y, begin_x); */
2323
#ifdef py_is_pad
2324
if (py_is_pad(self->win)) {
2325
win = subpad(self->win, nlines, ncols, begin_y, begin_x);
2326
}
2327
else
2328
#endif
2329
win = subwin(self->win, nlines, ncols, begin_y, begin_x);
2330
2331
if (win == NULL) {
2332
PyErr_SetString(PyCursesError, catchall_NULL);
2333
return NULL;
2334
}
2335
2336
return (PyObject *)PyCursesWindow_New(win, self->encoding);
2337
}
2338
2339
/*[clinic input]
2340
_curses.window.scroll
2341
2342
[
2343
lines: int = 1
2344
Number of lines to scroll.
2345
]
2346
/
2347
2348
Scroll the screen or scrolling region.
2349
2350
Scroll upward if the argument is positive and downward if it is negative.
2351
[clinic start generated code]*/
2352
2353
static PyObject *
2354
_curses_window_scroll_impl(PyCursesWindowObject *self, int group_right_1,
2355
int lines)
2356
/*[clinic end generated code: output=4541a8a11852d360 input=c969ca0cfabbdbec]*/
2357
{
2358
if (!group_right_1) {
2359
return PyCursesCheckERR(scroll(self->win), "scroll");
2360
}
2361
else {
2362
return PyCursesCheckERR(wscrl(self->win, lines), "scroll");
2363
}
2364
}
2365
2366
/*[clinic input]
2367
_curses.window.touchline
2368
2369
start: int
2370
count: int
2371
[
2372
changed: bool = True
2373
]
2374
/
2375
2376
Pretend count lines have been changed, starting with line start.
2377
2378
If changed is supplied, it specifies whether the affected lines are marked
2379
as having been changed (changed=True) or unchanged (changed=False).
2380
[clinic start generated code]*/
2381
2382
static PyObject *
2383
_curses_window_touchline_impl(PyCursesWindowObject *self, int start,
2384
int count, int group_right_1, int changed)
2385
/*[clinic end generated code: output=65d05b3f7438c61d input=a98aa4f79b6be845]*/
2386
{
2387
if (!group_right_1) {
2388
return PyCursesCheckERR(touchline(self->win, start, count), "touchline");
2389
}
2390
else {
2391
return PyCursesCheckERR(wtouchln(self->win, start, count, changed), "touchline");
2392
}
2393
}
2394
2395
/*[clinic input]
2396
_curses.window.vline
2397
2398
[
2399
y: int
2400
Starting Y-coordinate.
2401
x: int
2402
Starting X-coordinate.
2403
]
2404
2405
ch: object
2406
Character to draw.
2407
n: int
2408
Line length.
2409
2410
[
2411
attr: long(c_default="A_NORMAL") = _curses.A_NORMAL
2412
Attributes for the character.
2413
]
2414
/
2415
2416
Display a vertical line.
2417
[clinic start generated code]*/
2418
2419
static PyObject *
2420
_curses_window_vline_impl(PyCursesWindowObject *self, int group_left_1,
2421
int y, int x, PyObject *ch, int n,
2422
int group_right_1, long attr)
2423
/*[clinic end generated code: output=287ad1cc8982217f input=a6f2dc86a4648b32]*/
2424
{
2425
chtype ch_;
2426
2427
if (!PyCurses_ConvertToChtype(self, ch, &ch_))
2428
return NULL;
2429
if (group_left_1) {
2430
if (wmove(self->win, y, x) == ERR)
2431
return PyCursesCheckERR(ERR, "wmove");
2432
}
2433
return PyCursesCheckERR(wvline(self->win, ch_ | (attr_t)attr, n), "vline");
2434
}
2435
2436
static PyObject *
2437
PyCursesWindow_get_encoding(PyCursesWindowObject *self, void *closure)
2438
{
2439
return PyUnicode_FromString(self->encoding);
2440
}
2441
2442
static int
2443
PyCursesWindow_set_encoding(PyCursesWindowObject *self, PyObject *value, void *Py_UNUSED(ignored))
2444
{
2445
PyObject *ascii;
2446
char *encoding;
2447
2448
/* It is illegal to del win.encoding */
2449
if (value == NULL) {
2450
PyErr_SetString(PyExc_TypeError,
2451
"encoding may not be deleted");
2452
return -1;
2453
}
2454
2455
if (!PyUnicode_Check(value)) {
2456
PyErr_SetString(PyExc_TypeError,
2457
"setting encoding to a non-string");
2458
return -1;
2459
}
2460
ascii = PyUnicode_AsASCIIString(value);
2461
if (ascii == NULL)
2462
return -1;
2463
encoding = _PyMem_Strdup(PyBytes_AS_STRING(ascii));
2464
Py_DECREF(ascii);
2465
if (encoding == NULL) {
2466
PyErr_NoMemory();
2467
return -1;
2468
}
2469
PyMem_Free(self->encoding);
2470
self->encoding = encoding;
2471
return 0;
2472
}
2473
2474
#include "clinic/_cursesmodule.c.h"
2475
2476
static PyMethodDef PyCursesWindow_Methods[] = {
2477
_CURSES_WINDOW_ADDCH_METHODDEF
2478
_CURSES_WINDOW_ADDNSTR_METHODDEF
2479
_CURSES_WINDOW_ADDSTR_METHODDEF
2480
_CURSES_WINDOW_ATTROFF_METHODDEF
2481
_CURSES_WINDOW_ATTRON_METHODDEF
2482
_CURSES_WINDOW_ATTRSET_METHODDEF
2483
_CURSES_WINDOW_BKGD_METHODDEF
2484
#ifdef HAVE_CURSES_WCHGAT
2485
{"chgat", (PyCFunction)PyCursesWindow_ChgAt, METH_VARARGS},
2486
#endif
2487
_CURSES_WINDOW_BKGDSET_METHODDEF
2488
_CURSES_WINDOW_BORDER_METHODDEF
2489
_CURSES_WINDOW_BOX_METHODDEF
2490
{"clear", (PyCFunction)PyCursesWindow_wclear, METH_NOARGS},
2491
{"clearok", (PyCFunction)PyCursesWindow_clearok, METH_VARARGS},
2492
{"clrtobot", (PyCFunction)PyCursesWindow_wclrtobot, METH_NOARGS},
2493
{"clrtoeol", (PyCFunction)PyCursesWindow_wclrtoeol, METH_NOARGS},
2494
{"cursyncup", (PyCFunction)PyCursesWindow_wcursyncup, METH_NOARGS},
2495
_CURSES_WINDOW_DELCH_METHODDEF
2496
{"deleteln", (PyCFunction)PyCursesWindow_wdeleteln, METH_NOARGS},
2497
_CURSES_WINDOW_DERWIN_METHODDEF
2498
_CURSES_WINDOW_ECHOCHAR_METHODDEF
2499
_CURSES_WINDOW_ENCLOSE_METHODDEF
2500
{"erase", (PyCFunction)PyCursesWindow_werase, METH_NOARGS},
2501
{"getbegyx", (PyCFunction)PyCursesWindow_getbegyx, METH_NOARGS},
2502
_CURSES_WINDOW_GETBKGD_METHODDEF
2503
_CURSES_WINDOW_GETCH_METHODDEF
2504
_CURSES_WINDOW_GETKEY_METHODDEF
2505
_CURSES_WINDOW_GET_WCH_METHODDEF
2506
{"getmaxyx", (PyCFunction)PyCursesWindow_getmaxyx, METH_NOARGS},
2507
{"getparyx", (PyCFunction)PyCursesWindow_getparyx, METH_NOARGS},
2508
{"getstr", (PyCFunction)PyCursesWindow_GetStr, METH_VARARGS},
2509
{"getyx", (PyCFunction)PyCursesWindow_getyx, METH_NOARGS},
2510
_CURSES_WINDOW_HLINE_METHODDEF
2511
{"idcok", (PyCFunction)PyCursesWindow_idcok, METH_VARARGS},
2512
{"idlok", (PyCFunction)PyCursesWindow_idlok, METH_VARARGS},
2513
#ifdef HAVE_CURSES_IMMEDOK
2514
{"immedok", (PyCFunction)PyCursesWindow_immedok, METH_VARARGS},
2515
#endif
2516
_CURSES_WINDOW_INCH_METHODDEF
2517
_CURSES_WINDOW_INSCH_METHODDEF
2518
{"insdelln", (PyCFunction)PyCursesWindow_winsdelln, METH_VARARGS},
2519
{"insertln", (PyCFunction)PyCursesWindow_winsertln, METH_NOARGS},
2520
_CURSES_WINDOW_INSNSTR_METHODDEF
2521
_CURSES_WINDOW_INSSTR_METHODDEF
2522
{"instr", (PyCFunction)PyCursesWindow_InStr, METH_VARARGS},
2523
_CURSES_WINDOW_IS_LINETOUCHED_METHODDEF
2524
{"is_wintouched", (PyCFunction)PyCursesWindow_is_wintouched, METH_NOARGS},
2525
{"keypad", (PyCFunction)PyCursesWindow_keypad, METH_VARARGS},
2526
{"leaveok", (PyCFunction)PyCursesWindow_leaveok, METH_VARARGS},
2527
{"move", (PyCFunction)PyCursesWindow_wmove, METH_VARARGS},
2528
{"mvderwin", (PyCFunction)PyCursesWindow_mvderwin, METH_VARARGS},
2529
{"mvwin", (PyCFunction)PyCursesWindow_mvwin, METH_VARARGS},
2530
{"nodelay", (PyCFunction)PyCursesWindow_nodelay, METH_VARARGS},
2531
{"notimeout", (PyCFunction)PyCursesWindow_notimeout, METH_VARARGS},
2532
_CURSES_WINDOW_NOUTREFRESH_METHODDEF
2533
_CURSES_WINDOW_OVERLAY_METHODDEF
2534
_CURSES_WINDOW_OVERWRITE_METHODDEF
2535
_CURSES_WINDOW_PUTWIN_METHODDEF
2536
_CURSES_WINDOW_REDRAWLN_METHODDEF
2537
{"redrawwin", (PyCFunction)PyCursesWindow_redrawwin, METH_NOARGS},
2538
_CURSES_WINDOW_REFRESH_METHODDEF
2539
#ifndef STRICT_SYSV_CURSES
2540
{"resize", (PyCFunction)PyCursesWindow_wresize, METH_VARARGS},
2541
#endif
2542
_CURSES_WINDOW_SCROLL_METHODDEF
2543
{"scrollok", (PyCFunction)PyCursesWindow_scrollok, METH_VARARGS},
2544
_CURSES_WINDOW_SETSCRREG_METHODDEF
2545
{"standend", (PyCFunction)PyCursesWindow_wstandend, METH_NOARGS},
2546
{"standout", (PyCFunction)PyCursesWindow_wstandout, METH_NOARGS},
2547
{"subpad", (PyCFunction)_curses_window_subwin, METH_VARARGS, _curses_window_subwin__doc__},
2548
_CURSES_WINDOW_SUBWIN_METHODDEF
2549
{"syncdown", (PyCFunction)PyCursesWindow_wsyncdown, METH_NOARGS},
2550
#ifdef HAVE_CURSES_SYNCOK
2551
{"syncok", (PyCFunction)PyCursesWindow_syncok, METH_VARARGS},
2552
#endif
2553
{"syncup", (PyCFunction)PyCursesWindow_wsyncup, METH_NOARGS},
2554
{"timeout", (PyCFunction)PyCursesWindow_wtimeout, METH_VARARGS},
2555
_CURSES_WINDOW_TOUCHLINE_METHODDEF
2556
{"touchwin", (PyCFunction)PyCursesWindow_touchwin, METH_NOARGS},
2557
{"untouchwin", (PyCFunction)PyCursesWindow_untouchwin, METH_NOARGS},
2558
_CURSES_WINDOW_VLINE_METHODDEF
2559
{NULL, NULL} /* sentinel */
2560
};
2561
2562
static PyGetSetDef PyCursesWindow_getsets[] = {
2563
{"encoding",
2564
(getter)PyCursesWindow_get_encoding,
2565
(setter)PyCursesWindow_set_encoding,
2566
"the typecode character used to create the array"},
2567
{NULL, NULL, NULL, NULL } /* sentinel */
2568
};
2569
2570
/* -------------------------------------------------------*/
2571
2572
PyTypeObject PyCursesWindow_Type = {
2573
PyVarObject_HEAD_INIT(NULL, 0)
2574
"_curses.window", /*tp_name*/
2575
sizeof(PyCursesWindowObject), /*tp_basicsize*/
2576
0, /*tp_itemsize*/
2577
/* methods */
2578
(destructor)PyCursesWindow_Dealloc, /*tp_dealloc*/
2579
0, /*tp_vectorcall_offset*/
2580
(getattrfunc)0, /*tp_getattr*/
2581
(setattrfunc)0, /*tp_setattr*/
2582
0, /*tp_as_async*/
2583
0, /*tp_repr*/
2584
0, /*tp_as_number*/
2585
0, /*tp_as_sequence*/
2586
0, /*tp_as_mapping*/
2587
0, /*tp_hash*/
2588
0, /*tp_call*/
2589
0, /*tp_str*/
2590
0, /*tp_getattro*/
2591
0, /*tp_setattro*/
2592
0, /*tp_as_buffer*/
2593
Py_TPFLAGS_DEFAULT, /*tp_flags*/
2594
0, /*tp_doc*/
2595
0, /*tp_traverse*/
2596
0, /*tp_clear*/
2597
0, /*tp_richcompare*/
2598
0, /*tp_weaklistoffset*/
2599
0, /*tp_iter*/
2600
0, /*tp_iternext*/
2601
PyCursesWindow_Methods, /*tp_methods*/
2602
0, /* tp_members */
2603
PyCursesWindow_getsets, /* tp_getset */
2604
};
2605
2606
/* Function Prototype Macros - They are ugly but very, very useful. ;-)
2607
2608
X - function name
2609
TYPE - parameter Type
2610
ERGSTR - format string for construction of the return value
2611
PARSESTR - format string for argument parsing
2612
*/
2613
2614
#define NoArgNoReturnFunctionBody(X) \
2615
{ \
2616
PyCursesInitialised \
2617
return PyCursesCheckERR(X(), # X); }
2618
2619
#define NoArgOrFlagNoReturnFunctionBody(X, flag) \
2620
{ \
2621
PyCursesInitialised \
2622
if (flag) \
2623
return PyCursesCheckERR(X(), # X); \
2624
else \
2625
return PyCursesCheckERR(no ## X(), # X); \
2626
}
2627
2628
#define NoArgReturnIntFunctionBody(X) \
2629
{ \
2630
PyCursesInitialised \
2631
return PyLong_FromLong((long) X()); }
2632
2633
2634
#define NoArgReturnStringFunctionBody(X) \
2635
{ \
2636
PyCursesInitialised \
2637
return PyBytes_FromString(X()); }
2638
2639
#define NoArgTrueFalseFunctionBody(X) \
2640
{ \
2641
PyCursesInitialised \
2642
return PyBool_FromLong(X()); }
2643
2644
#define NoArgNoReturnVoidFunctionBody(X) \
2645
{ \
2646
PyCursesInitialised \
2647
X(); \
2648
Py_RETURN_NONE; }
2649
2650
/*********************************************************************
2651
Global Functions
2652
**********************************************************************/
2653
2654
#ifdef HAVE_CURSES_FILTER
2655
/*[clinic input]
2656
_curses.filter
2657
2658
[clinic start generated code]*/
2659
2660
static PyObject *
2661
_curses_filter_impl(PyObject *module)
2662
/*[clinic end generated code: output=fb5b8a3642eb70b5 input=668c75a6992d3624]*/
2663
{
2664
/* not checking for PyCursesInitialised here since filter() must
2665
be called before initscr() */
2666
filter();
2667
Py_RETURN_NONE;
2668
}
2669
#endif
2670
2671
/*[clinic input]
2672
_curses.baudrate
2673
2674
Return the output speed of the terminal in bits per second.
2675
[clinic start generated code]*/
2676
2677
static PyObject *
2678
_curses_baudrate_impl(PyObject *module)
2679
/*[clinic end generated code: output=3c63c6c401d7d9c0 input=921f022ed04a0fd9]*/
2680
NoArgReturnIntFunctionBody(baudrate)
2681
2682
/*[clinic input]
2683
_curses.beep
2684
2685
Emit a short attention sound.
2686
[clinic start generated code]*/
2687
2688
static PyObject *
2689
_curses_beep_impl(PyObject *module)
2690
/*[clinic end generated code: output=425274962abe49a2 input=a35698ca7d0162bc]*/
2691
NoArgNoReturnFunctionBody(beep)
2692
2693
/*[clinic input]
2694
_curses.can_change_color
2695
2696
Return True if the programmer can change the colors displayed by the terminal.
2697
[clinic start generated code]*/
2698
2699
static PyObject *
2700
_curses_can_change_color_impl(PyObject *module)
2701
/*[clinic end generated code: output=359df8c3c77d8bf1 input=d7718884de0092f2]*/
2702
NoArgTrueFalseFunctionBody(can_change_color)
2703
2704
/*[clinic input]
2705
_curses.cbreak
2706
2707
flag: bool = True
2708
If false, the effect is the same as calling nocbreak().
2709
/
2710
2711
Enter cbreak mode.
2712
2713
In cbreak mode (sometimes called "rare" mode) normal tty line buffering is
2714
turned off and characters are available to be read one by one. However,
2715
unlike raw mode, special characters (interrupt, quit, suspend, and flow
2716
control) retain their effects on the tty driver and calling program.
2717
Calling first raw() then cbreak() leaves the terminal in cbreak mode.
2718
[clinic start generated code]*/
2719
2720
static PyObject *
2721
_curses_cbreak_impl(PyObject *module, int flag)
2722
/*[clinic end generated code: output=9f9dee9664769751 input=c7d0bddda93016c1]*/
2723
NoArgOrFlagNoReturnFunctionBody(cbreak, flag)
2724
2725
/*[clinic input]
2726
_curses.color_content
2727
2728
color_number: color
2729
The number of the color (0 - (COLORS-1)).
2730
/
2731
2732
Return the red, green, and blue (RGB) components of the specified color.
2733
2734
A 3-tuple is returned, containing the R, G, B values for the given color,
2735
which will be between 0 (no component) and 1000 (maximum amount of component).
2736
[clinic start generated code]*/
2737
2738
static PyObject *
2739
_curses_color_content_impl(PyObject *module, int color_number)
2740
/*[clinic end generated code: output=17b466df7054e0de input=03b5ed0472662aea]*/
2741
{
2742
_CURSES_COLOR_VAL_TYPE r,g,b;
2743
2744
PyCursesInitialised;
2745
PyCursesInitialisedColor;
2746
2747
if (_COLOR_CONTENT_FUNC(color_number, &r, &g, &b) == ERR) {
2748
PyErr_Format(PyCursesError, "%s() returned ERR",
2749
Py_STRINGIFY(_COLOR_CONTENT_FUNC));
2750
return NULL;
2751
}
2752
2753
return Py_BuildValue("(iii)", r, g, b);
2754
}
2755
2756
/*[clinic input]
2757
_curses.color_pair
2758
2759
pair_number: int
2760
The number of the color pair.
2761
/
2762
2763
Return the attribute value for displaying text in the specified color.
2764
2765
This attribute value can be combined with A_STANDOUT, A_REVERSE, and the
2766
other A_* attributes. pair_number() is the counterpart to this function.
2767
[clinic start generated code]*/
2768
2769
static PyObject *
2770
_curses_color_pair_impl(PyObject *module, int pair_number)
2771
/*[clinic end generated code: output=60718abb10ce9feb input=6034e9146f343802]*/
2772
{
2773
PyCursesInitialised;
2774
PyCursesInitialisedColor;
2775
2776
return PyLong_FromLong(COLOR_PAIR(pair_number));
2777
}
2778
2779
/*[clinic input]
2780
_curses.curs_set
2781
2782
visibility: int
2783
0 for invisible, 1 for normal visible, or 2 for very visible.
2784
/
2785
2786
Set the cursor state.
2787
2788
If the terminal supports the visibility requested, the previous cursor
2789
state is returned; otherwise, an exception is raised. On many terminals,
2790
the "visible" mode is an underline cursor and the "very visible" mode is
2791
a block cursor.
2792
[clinic start generated code]*/
2793
2794
static PyObject *
2795
_curses_curs_set_impl(PyObject *module, int visibility)
2796
/*[clinic end generated code: output=ee8e62483b1d6cd4 input=81a7924a65d29504]*/
2797
{
2798
int erg;
2799
2800
PyCursesInitialised;
2801
2802
erg = curs_set(visibility);
2803
if (erg == ERR) return PyCursesCheckERR(erg, "curs_set");
2804
2805
return PyLong_FromLong((long) erg);
2806
}
2807
2808
/*[clinic input]
2809
_curses.def_prog_mode
2810
2811
Save the current terminal mode as the "program" mode.
2812
2813
The "program" mode is the mode when the running program is using curses.
2814
2815
Subsequent calls to reset_prog_mode() will restore this mode.
2816
[clinic start generated code]*/
2817
2818
static PyObject *
2819
_curses_def_prog_mode_impl(PyObject *module)
2820
/*[clinic end generated code: output=05d5a351fff874aa input=768b9cace620dda5]*/
2821
NoArgNoReturnFunctionBody(def_prog_mode)
2822
2823
/*[clinic input]
2824
_curses.def_shell_mode
2825
2826
Save the current terminal mode as the "shell" mode.
2827
2828
The "shell" mode is the mode when the running program is not using curses.
2829
2830
Subsequent calls to reset_shell_mode() will restore this mode.
2831
[clinic start generated code]*/
2832
2833
static PyObject *
2834
_curses_def_shell_mode_impl(PyObject *module)
2835
/*[clinic end generated code: output=d6e42f5c768f860f input=5ead21f6f0baa894]*/
2836
NoArgNoReturnFunctionBody(def_shell_mode)
2837
2838
/*[clinic input]
2839
_curses.delay_output
2840
2841
ms: int
2842
Duration in milliseconds.
2843
/
2844
2845
Insert a pause in output.
2846
[clinic start generated code]*/
2847
2848
static PyObject *
2849
_curses_delay_output_impl(PyObject *module, int ms)
2850
/*[clinic end generated code: output=b6613a67f17fa4f4 input=5316457f5f59196c]*/
2851
{
2852
PyCursesInitialised;
2853
2854
return PyCursesCheckERR(delay_output(ms), "delay_output");
2855
}
2856
2857
/*[clinic input]
2858
_curses.doupdate
2859
2860
Update the physical screen to match the virtual screen.
2861
[clinic start generated code]*/
2862
2863
static PyObject *
2864
_curses_doupdate_impl(PyObject *module)
2865
/*[clinic end generated code: output=f34536975a75680c input=8da80914432a6489]*/
2866
NoArgNoReturnFunctionBody(doupdate)
2867
2868
/*[clinic input]
2869
_curses.echo
2870
2871
flag: bool = True
2872
If false, the effect is the same as calling noecho().
2873
/
2874
2875
Enter echo mode.
2876
2877
In echo mode, each character input is echoed to the screen as it is entered.
2878
[clinic start generated code]*/
2879
2880
static PyObject *
2881
_curses_echo_impl(PyObject *module, int flag)
2882
/*[clinic end generated code: output=03acb2ddfa6c8729 input=86cd4d5bb1d569c0]*/
2883
NoArgOrFlagNoReturnFunctionBody(echo, flag)
2884
2885
/*[clinic input]
2886
_curses.endwin
2887
2888
De-initialize the library, and return terminal to normal status.
2889
[clinic start generated code]*/
2890
2891
static PyObject *
2892
_curses_endwin_impl(PyObject *module)
2893
/*[clinic end generated code: output=c0150cd96d2f4128 input=e172cfa43062f3fa]*/
2894
NoArgNoReturnFunctionBody(endwin)
2895
2896
/*[clinic input]
2897
_curses.erasechar
2898
2899
Return the user's current erase character.
2900
[clinic start generated code]*/
2901
2902
static PyObject *
2903
_curses_erasechar_impl(PyObject *module)
2904
/*[clinic end generated code: output=3df305dc6b926b3f input=628c136c3c5758d3]*/
2905
{
2906
char ch;
2907
2908
PyCursesInitialised;
2909
2910
ch = erasechar();
2911
2912
return PyBytes_FromStringAndSize(&ch, 1);
2913
}
2914
2915
/*[clinic input]
2916
_curses.flash
2917
2918
Flash the screen.
2919
2920
That is, change it to reverse-video and then change it back in a short interval.
2921
[clinic start generated code]*/
2922
2923
static PyObject *
2924
_curses_flash_impl(PyObject *module)
2925
/*[clinic end generated code: output=488b8a0ebd9ea9b8 input=02fdfb06c8fc3171]*/
2926
NoArgNoReturnFunctionBody(flash)
2927
2928
/*[clinic input]
2929
_curses.flushinp
2930
2931
Flush all input buffers.
2932
2933
This throws away any typeahead that has been typed by the user and has not
2934
yet been processed by the program.
2935
[clinic start generated code]*/
2936
2937
static PyObject *
2938
_curses_flushinp_impl(PyObject *module)
2939
/*[clinic end generated code: output=7e7a1fc1473960f5 input=59d042e705cef5ec]*/
2940
NoArgNoReturnVoidFunctionBody(flushinp)
2941
2942
#ifdef getsyx
2943
/*[clinic input]
2944
_curses.getsyx
2945
2946
Return the current coordinates of the virtual screen cursor.
2947
2948
Return a (y, x) tuple. If leaveok is currently true, return (-1, -1).
2949
[clinic start generated code]*/
2950
2951
static PyObject *
2952
_curses_getsyx_impl(PyObject *module)
2953
/*[clinic end generated code: output=c8e6c3f42349a038 input=9e1f862f3b4f7cba]*/
2954
{
2955
int x = 0;
2956
int y = 0;
2957
2958
PyCursesInitialised;
2959
2960
getsyx(y, x);
2961
2962
return Py_BuildValue("(ii)", y, x);
2963
}
2964
#endif
2965
2966
#ifdef NCURSES_MOUSE_VERSION
2967
/*[clinic input]
2968
_curses.getmouse
2969
2970
Retrieve the queued mouse event.
2971
2972
After getch() returns KEY_MOUSE to signal a mouse event, this function
2973
returns a 5-tuple (id, x, y, z, bstate).
2974
[clinic start generated code]*/
2975
2976
static PyObject *
2977
_curses_getmouse_impl(PyObject *module)
2978
/*[clinic end generated code: output=ccf4242546b9cfa8 input=5b756ee6f5b481b1]*/
2979
{
2980
int rtn;
2981
MEVENT event;
2982
2983
PyCursesInitialised;
2984
2985
rtn = getmouse( &event );
2986
if (rtn == ERR) {
2987
PyErr_SetString(PyCursesError, "getmouse() returned ERR");
2988
return NULL;
2989
}
2990
return Py_BuildValue("(hiiik)",
2991
(short)event.id,
2992
(int)event.x, (int)event.y, (int)event.z,
2993
(unsigned long) event.bstate);
2994
}
2995
2996
/*[clinic input]
2997
_curses.ungetmouse
2998
2999
id: short
3000
x: int
3001
y: int
3002
z: int
3003
bstate: unsigned_long(bitwise=True)
3004
/
3005
3006
Push a KEY_MOUSE event onto the input queue.
3007
3008
The following getmouse() will return the given state data.
3009
[clinic start generated code]*/
3010
3011
static PyObject *
3012
_curses_ungetmouse_impl(PyObject *module, short id, int x, int y, int z,
3013
unsigned long bstate)
3014
/*[clinic end generated code: output=3430c9b0fc5c4341 input=fd650b2ca5a01e8f]*/
3015
{
3016
MEVENT event;
3017
3018
PyCursesInitialised;
3019
3020
event.id = id;
3021
event.x = x;
3022
event.y = y;
3023
event.z = z;
3024
event.bstate = bstate;
3025
return PyCursesCheckERR(ungetmouse(&event), "ungetmouse");
3026
}
3027
#endif
3028
3029
/*[clinic input]
3030
_curses.getwin
3031
3032
file: object
3033
/
3034
3035
Read window related data stored in the file by an earlier putwin() call.
3036
3037
The routine then creates and initializes a new window using that data,
3038
returning the new window object.
3039
[clinic start generated code]*/
3040
3041
static PyObject *
3042
_curses_getwin(PyObject *module, PyObject *file)
3043
/*[clinic end generated code: output=a79e0df3379af756 input=f713d2bba0e4c929]*/
3044
{
3045
FILE *fp;
3046
PyObject *data;
3047
size_t datalen;
3048
WINDOW *win;
3049
PyObject *res = NULL;
3050
3051
PyCursesInitialised;
3052
3053
fp = tmpfile();
3054
if (fp == NULL)
3055
return PyErr_SetFromErrno(PyExc_OSError);
3056
3057
if (_Py_set_inheritable(fileno(fp), 0, NULL) < 0)
3058
goto error;
3059
3060
data = PyObject_CallMethod(file, "read", NULL);
3061
if (data == NULL)
3062
goto error;
3063
if (!PyBytes_Check(data)) {
3064
PyErr_Format(PyExc_TypeError,
3065
"f.read() returned %.100s instead of bytes",
3066
Py_TYPE(data)->tp_name);
3067
Py_DECREF(data);
3068
goto error;
3069
}
3070
datalen = PyBytes_GET_SIZE(data);
3071
if (fwrite(PyBytes_AS_STRING(data), 1, datalen, fp) != datalen) {
3072
Py_DECREF(data);
3073
PyErr_SetFromErrno(PyExc_OSError);
3074
goto error;
3075
}
3076
Py_DECREF(data);
3077
3078
fseek(fp, 0, 0);
3079
win = getwin(fp);
3080
if (win == NULL) {
3081
PyErr_SetString(PyCursesError, catchall_NULL);
3082
goto error;
3083
}
3084
res = PyCursesWindow_New(win, NULL);
3085
3086
error:
3087
fclose(fp);
3088
return res;
3089
}
3090
3091
/*[clinic input]
3092
_curses.halfdelay
3093
3094
tenths: byte
3095
Maximal blocking delay in tenths of seconds (1 - 255).
3096
/
3097
3098
Enter half-delay mode.
3099
3100
Use nocbreak() to leave half-delay mode.
3101
[clinic start generated code]*/
3102
3103
static PyObject *
3104
_curses_halfdelay_impl(PyObject *module, unsigned char tenths)
3105
/*[clinic end generated code: output=e92cdf0ef33c0663 input=e42dce7259c15100]*/
3106
{
3107
PyCursesInitialised;
3108
3109
return PyCursesCheckERR(halfdelay(tenths), "halfdelay");
3110
}
3111
3112
/*[clinic input]
3113
_curses.has_colors
3114
3115
Return True if the terminal can display colors; otherwise, return False.
3116
[clinic start generated code]*/
3117
3118
static PyObject *
3119
_curses_has_colors_impl(PyObject *module)
3120
/*[clinic end generated code: output=db5667483139e3e2 input=b2ec41b739d896c6]*/
3121
NoArgTrueFalseFunctionBody(has_colors)
3122
3123
/*[clinic input]
3124
_curses.has_ic
3125
3126
Return True if the terminal has insert- and delete-character capabilities.
3127
[clinic start generated code]*/
3128
3129
static PyObject *
3130
_curses_has_ic_impl(PyObject *module)
3131
/*[clinic end generated code: output=6be24da9cb1268fe input=9bc2d3a797cc7324]*/
3132
NoArgTrueFalseFunctionBody(has_ic)
3133
3134
/*[clinic input]
3135
_curses.has_il
3136
3137
Return True if the terminal has insert- and delete-line capabilities.
3138
[clinic start generated code]*/
3139
3140
static PyObject *
3141
_curses_has_il_impl(PyObject *module)
3142
/*[clinic end generated code: output=d45bd7788ff9f5f4 input=cd939d5607ee5427]*/
3143
NoArgTrueFalseFunctionBody(has_il)
3144
3145
#ifdef HAVE_CURSES_HAS_KEY
3146
/*[clinic input]
3147
_curses.has_key
3148
3149
key: int
3150
Key number.
3151
/
3152
3153
Return True if the current terminal type recognizes a key with that value.
3154
[clinic start generated code]*/
3155
3156
static PyObject *
3157
_curses_has_key_impl(PyObject *module, int key)
3158
/*[clinic end generated code: output=19ad48319414d0b1 input=78bd44acf1a4997c]*/
3159
{
3160
PyCursesInitialised;
3161
3162
return PyBool_FromLong(has_key(key));
3163
}
3164
#endif
3165
3166
/*[clinic input]
3167
_curses.init_color
3168
3169
color_number: color
3170
The number of the color to be changed (0 - (COLORS-1)).
3171
r: component
3172
Red component (0 - 1000).
3173
g: component
3174
Green component (0 - 1000).
3175
b: component
3176
Blue component (0 - 1000).
3177
/
3178
3179
Change the definition of a color.
3180
3181
When init_color() is used, all occurrences of that color on the screen
3182
immediately change to the new definition. This function is a no-op on
3183
most terminals; it is active only if can_change_color() returns true.
3184
[clinic start generated code]*/
3185
3186
static PyObject *
3187
_curses_init_color_impl(PyObject *module, int color_number, short r, short g,
3188
short b)
3189
/*[clinic end generated code: output=d7ed71b2d818cdf2 input=ae2b8bea0f152c80]*/
3190
{
3191
PyCursesInitialised;
3192
PyCursesInitialisedColor;
3193
3194
return PyCursesCheckERR(_CURSES_INIT_COLOR_FUNC(color_number, r, g, b),
3195
Py_STRINGIFY(_CURSES_INIT_COLOR_FUNC));
3196
}
3197
3198
/*[clinic input]
3199
_curses.init_pair
3200
3201
pair_number: pair
3202
The number of the color-pair to be changed (1 - (COLOR_PAIRS-1)).
3203
fg: color_allow_default
3204
Foreground color number (-1 - (COLORS-1)).
3205
bg: color_allow_default
3206
Background color number (-1 - (COLORS-1)).
3207
/
3208
3209
Change the definition of a color-pair.
3210
3211
If the color-pair was previously initialized, the screen is refreshed and
3212
all occurrences of that color-pair are changed to the new definition.
3213
[clinic start generated code]*/
3214
3215
static PyObject *
3216
_curses_init_pair_impl(PyObject *module, int pair_number, int fg, int bg)
3217
/*[clinic end generated code: output=a0bba03d2bbc3ee6 input=54b421b44c12c389]*/
3218
{
3219
PyCursesInitialised;
3220
PyCursesInitialisedColor;
3221
3222
if (_CURSES_INIT_PAIR_FUNC(pair_number, fg, bg) == ERR) {
3223
if (pair_number >= COLOR_PAIRS) {
3224
PyErr_Format(PyExc_ValueError,
3225
"Color pair is greater than COLOR_PAIRS-1 (%d).",
3226
COLOR_PAIRS - 1);
3227
}
3228
else {
3229
PyErr_Format(PyCursesError, "%s() returned ERR",
3230
Py_STRINGIFY(_CURSES_INIT_PAIR_FUNC));
3231
}
3232
return NULL;
3233
}
3234
3235
Py_RETURN_NONE;
3236
}
3237
3238
static PyObject *ModDict;
3239
3240
/*[clinic input]
3241
_curses.initscr
3242
3243
Initialize the library.
3244
3245
Return a WindowObject which represents the whole screen.
3246
[clinic start generated code]*/
3247
3248
static PyObject *
3249
_curses_initscr_impl(PyObject *module)
3250
/*[clinic end generated code: output=619fb68443810b7b input=514f4bce1821f6b5]*/
3251
{
3252
WINDOW *win;
3253
PyCursesWindowObject *winobj;
3254
3255
if (initialised) {
3256
wrefresh(stdscr);
3257
return (PyObject *)PyCursesWindow_New(stdscr, NULL);
3258
}
3259
3260
win = initscr();
3261
3262
if (win == NULL) {
3263
PyErr_SetString(PyCursesError, catchall_NULL);
3264
return NULL;
3265
}
3266
3267
initialised = initialised_setupterm = TRUE;
3268
3269
/* This was moved from initcurses() because it core dumped on SGI,
3270
where they're not defined until you've called initscr() */
3271
#define SetDictInt(string,ch) \
3272
do { \
3273
PyObject *o = PyLong_FromLong((long) (ch)); \
3274
if (o && PyDict_SetItemString(ModDict, string, o) == 0) { \
3275
Py_DECREF(o); \
3276
} \
3277
} while (0)
3278
3279
/* Here are some graphic symbols you can use */
3280
SetDictInt("ACS_ULCORNER", (ACS_ULCORNER));
3281
SetDictInt("ACS_LLCORNER", (ACS_LLCORNER));
3282
SetDictInt("ACS_URCORNER", (ACS_URCORNER));
3283
SetDictInt("ACS_LRCORNER", (ACS_LRCORNER));
3284
SetDictInt("ACS_LTEE", (ACS_LTEE));
3285
SetDictInt("ACS_RTEE", (ACS_RTEE));
3286
SetDictInt("ACS_BTEE", (ACS_BTEE));
3287
SetDictInt("ACS_TTEE", (ACS_TTEE));
3288
SetDictInt("ACS_HLINE", (ACS_HLINE));
3289
SetDictInt("ACS_VLINE", (ACS_VLINE));
3290
SetDictInt("ACS_PLUS", (ACS_PLUS));
3291
#if !defined(__hpux) || defined(HAVE_NCURSES_H)
3292
/* On HP/UX 11, these are of type cchar_t, which is not an
3293
integral type. If this is a problem on more platforms, a
3294
configure test should be added to determine whether ACS_S1
3295
is of integral type. */
3296
SetDictInt("ACS_S1", (ACS_S1));
3297
SetDictInt("ACS_S9", (ACS_S9));
3298
SetDictInt("ACS_DIAMOND", (ACS_DIAMOND));
3299
SetDictInt("ACS_CKBOARD", (ACS_CKBOARD));
3300
SetDictInt("ACS_DEGREE", (ACS_DEGREE));
3301
SetDictInt("ACS_PLMINUS", (ACS_PLMINUS));
3302
SetDictInt("ACS_BULLET", (ACS_BULLET));
3303
SetDictInt("ACS_LARROW", (ACS_LARROW));
3304
SetDictInt("ACS_RARROW", (ACS_RARROW));
3305
SetDictInt("ACS_DARROW", (ACS_DARROW));
3306
SetDictInt("ACS_UARROW", (ACS_UARROW));
3307
SetDictInt("ACS_BOARD", (ACS_BOARD));
3308
SetDictInt("ACS_LANTERN", (ACS_LANTERN));
3309
SetDictInt("ACS_BLOCK", (ACS_BLOCK));
3310
#endif
3311
SetDictInt("ACS_BSSB", (ACS_ULCORNER));
3312
SetDictInt("ACS_SSBB", (ACS_LLCORNER));
3313
SetDictInt("ACS_BBSS", (ACS_URCORNER));
3314
SetDictInt("ACS_SBBS", (ACS_LRCORNER));
3315
SetDictInt("ACS_SBSS", (ACS_RTEE));
3316
SetDictInt("ACS_SSSB", (ACS_LTEE));
3317
SetDictInt("ACS_SSBS", (ACS_BTEE));
3318
SetDictInt("ACS_BSSS", (ACS_TTEE));
3319
SetDictInt("ACS_BSBS", (ACS_HLINE));
3320
SetDictInt("ACS_SBSB", (ACS_VLINE));
3321
SetDictInt("ACS_SSSS", (ACS_PLUS));
3322
3323
/* The following are never available with strict SYSV curses */
3324
#ifdef ACS_S3
3325
SetDictInt("ACS_S3", (ACS_S3));
3326
#endif
3327
#ifdef ACS_S7
3328
SetDictInt("ACS_S7", (ACS_S7));
3329
#endif
3330
#ifdef ACS_LEQUAL
3331
SetDictInt("ACS_LEQUAL", (ACS_LEQUAL));
3332
#endif
3333
#ifdef ACS_GEQUAL
3334
SetDictInt("ACS_GEQUAL", (ACS_GEQUAL));
3335
#endif
3336
#ifdef ACS_PI
3337
SetDictInt("ACS_PI", (ACS_PI));
3338
#endif
3339
#ifdef ACS_NEQUAL
3340
SetDictInt("ACS_NEQUAL", (ACS_NEQUAL));
3341
#endif
3342
#ifdef ACS_STERLING
3343
SetDictInt("ACS_STERLING", (ACS_STERLING));
3344
#endif
3345
3346
SetDictInt("LINES", LINES);
3347
SetDictInt("COLS", COLS);
3348
3349
winobj = (PyCursesWindowObject *)PyCursesWindow_New(win, NULL);
3350
screen_encoding = winobj->encoding;
3351
return (PyObject *)winobj;
3352
}
3353
3354
/*[clinic input]
3355
_curses.setupterm
3356
3357
term: str(accept={str, NoneType}) = None
3358
Terminal name.
3359
If omitted, the value of the TERM environment variable will be used.
3360
fd: int = -1
3361
File descriptor to which any initialization sequences will be sent.
3362
If not supplied, the file descriptor for sys.stdout will be used.
3363
3364
Initialize the terminal.
3365
[clinic start generated code]*/
3366
3367
static PyObject *
3368
_curses_setupterm_impl(PyObject *module, const char *term, int fd)
3369
/*[clinic end generated code: output=4584e587350f2848 input=4511472766af0c12]*/
3370
{
3371
int err;
3372
3373
if (fd == -1) {
3374
PyObject* sys_stdout;
3375
3376
sys_stdout = PySys_GetObject("stdout");
3377
3378
if (sys_stdout == NULL || sys_stdout == Py_None) {
3379
PyErr_SetString(
3380
PyCursesError,
3381
"lost sys.stdout");
3382
return NULL;
3383
}
3384
3385
fd = PyObject_AsFileDescriptor(sys_stdout);
3386
3387
if (fd == -1) {
3388
return NULL;
3389
}
3390
}
3391
3392
if (!initialised_setupterm && setupterm((char *)term, fd, &err) == ERR) {
3393
const char* s = "setupterm: unknown error";
3394
3395
if (err == 0) {
3396
s = "setupterm: could not find terminal";
3397
} else if (err == -1) {
3398
s = "setupterm: could not find terminfo database";
3399
}
3400
3401
PyErr_SetString(PyCursesError,s);
3402
return NULL;
3403
}
3404
3405
initialised_setupterm = TRUE;
3406
3407
Py_RETURN_NONE;
3408
}
3409
3410
#if defined(NCURSES_EXT_FUNCS) && NCURSES_EXT_FUNCS >= 20081102
3411
// https://invisible-island.net/ncurses/NEWS.html#index-t20080119
3412
3413
/*[clinic input]
3414
_curses.get_escdelay
3415
3416
Gets the curses ESCDELAY setting.
3417
3418
Gets the number of milliseconds to wait after reading an escape character,
3419
to distinguish between an individual escape character entered on the
3420
keyboard from escape sequences sent by cursor and function keys.
3421
[clinic start generated code]*/
3422
3423
static PyObject *
3424
_curses_get_escdelay_impl(PyObject *module)
3425
/*[clinic end generated code: output=222fa1a822555d60 input=be2d5b3dd974d0a4]*/
3426
{
3427
return PyLong_FromLong(ESCDELAY);
3428
}
3429
/*[clinic input]
3430
_curses.set_escdelay
3431
ms: int
3432
length of the delay in milliseconds.
3433
/
3434
3435
Sets the curses ESCDELAY setting.
3436
3437
Sets the number of milliseconds to wait after reading an escape character,
3438
to distinguish between an individual escape character entered on the
3439
keyboard from escape sequences sent by cursor and function keys.
3440
[clinic start generated code]*/
3441
3442
static PyObject *
3443
_curses_set_escdelay_impl(PyObject *module, int ms)
3444
/*[clinic end generated code: output=43818efbf7980ac4 input=7796fe19f111e250]*/
3445
{
3446
if (ms <= 0) {
3447
PyErr_SetString(PyExc_ValueError, "ms must be > 0");
3448
return NULL;
3449
}
3450
3451
return PyCursesCheckERR(set_escdelay(ms), "set_escdelay");
3452
}
3453
3454
/*[clinic input]
3455
_curses.get_tabsize
3456
3457
Gets the curses TABSIZE setting.
3458
3459
Gets the number of columns used by the curses library when converting a tab
3460
character to spaces as it adds the tab to a window.
3461
[clinic start generated code]*/
3462
3463
static PyObject *
3464
_curses_get_tabsize_impl(PyObject *module)
3465
/*[clinic end generated code: output=7e9e51fb6126fbdf input=74af86bf6c9f5d7e]*/
3466
{
3467
return PyLong_FromLong(TABSIZE);
3468
}
3469
/*[clinic input]
3470
_curses.set_tabsize
3471
size: int
3472
rendered cell width of a tab character.
3473
/
3474
3475
Sets the curses TABSIZE setting.
3476
3477
Sets the number of columns used by the curses library when converting a tab
3478
character to spaces as it adds the tab to a window.
3479
[clinic start generated code]*/
3480
3481
static PyObject *
3482
_curses_set_tabsize_impl(PyObject *module, int size)
3483
/*[clinic end generated code: output=c1de5a76c0daab1e input=78cba6a3021ad061]*/
3484
{
3485
if (size <= 0) {
3486
PyErr_SetString(PyExc_ValueError, "size must be > 0");
3487
return NULL;
3488
}
3489
3490
return PyCursesCheckERR(set_tabsize(size), "set_tabsize");
3491
}
3492
#endif
3493
3494
/*[clinic input]
3495
_curses.intrflush
3496
3497
flag: bool
3498
/
3499
3500
[clinic start generated code]*/
3501
3502
static PyObject *
3503
_curses_intrflush_impl(PyObject *module, int flag)
3504
/*[clinic end generated code: output=c1986df35e999a0f input=c65fe2ef973fe40a]*/
3505
{
3506
PyCursesInitialised;
3507
3508
return PyCursesCheckERR(intrflush(NULL, flag), "intrflush");
3509
}
3510
3511
/*[clinic input]
3512
_curses.isendwin
3513
3514
Return True if endwin() has been called.
3515
[clinic start generated code]*/
3516
3517
static PyObject *
3518
_curses_isendwin_impl(PyObject *module)
3519
/*[clinic end generated code: output=d73179e4a7e1eb8c input=6cdb01a7ebf71397]*/
3520
NoArgTrueFalseFunctionBody(isendwin)
3521
3522
#ifdef HAVE_CURSES_IS_TERM_RESIZED
3523
/*[clinic input]
3524
_curses.is_term_resized
3525
3526
nlines: int
3527
Height.
3528
ncols: int
3529
Width.
3530
/
3531
3532
Return True if resize_term() would modify the window structure, False otherwise.
3533
[clinic start generated code]*/
3534
3535
static PyObject *
3536
_curses_is_term_resized_impl(PyObject *module, int nlines, int ncols)
3537
/*[clinic end generated code: output=aafe04afe50f1288 input=ca9c0bd0fb8ab444]*/
3538
{
3539
PyCursesInitialised;
3540
3541
return PyBool_FromLong(is_term_resized(nlines, ncols));
3542
}
3543
#endif /* HAVE_CURSES_IS_TERM_RESIZED */
3544
3545
/*[clinic input]
3546
_curses.keyname
3547
3548
key: int
3549
Key number.
3550
/
3551
3552
Return the name of specified key.
3553
[clinic start generated code]*/
3554
3555
static PyObject *
3556
_curses_keyname_impl(PyObject *module, int key)
3557
/*[clinic end generated code: output=fa2675ab3f4e056b input=ee4b1d0f243a2a2b]*/
3558
{
3559
const char *knp;
3560
3561
PyCursesInitialised;
3562
3563
if (key < 0) {
3564
PyErr_SetString(PyExc_ValueError, "invalid key number");
3565
return NULL;
3566
}
3567
knp = keyname(key);
3568
3569
return PyBytes_FromString((knp == NULL) ? "" : knp);
3570
}
3571
3572
/*[clinic input]
3573
_curses.killchar
3574
3575
Return the user's current line kill character.
3576
[clinic start generated code]*/
3577
3578
static PyObject *
3579
_curses_killchar_impl(PyObject *module)
3580
/*[clinic end generated code: output=31c3a45b2c528269 input=1ff171c38df5ccad]*/
3581
{
3582
char ch;
3583
3584
ch = killchar();
3585
3586
return PyBytes_FromStringAndSize(&ch, 1);
3587
}
3588
3589
/*[clinic input]
3590
_curses.longname
3591
3592
Return the terminfo long name field describing the current terminal.
3593
3594
The maximum length of a verbose description is 128 characters. It is defined
3595
only after the call to initscr().
3596
[clinic start generated code]*/
3597
3598
static PyObject *
3599
_curses_longname_impl(PyObject *module)
3600
/*[clinic end generated code: output=fdf30433727ef568 input=84c3f20201b1098e]*/
3601
NoArgReturnStringFunctionBody(longname)
3602
3603
/*[clinic input]
3604
_curses.meta
3605
3606
yes: bool
3607
/
3608
3609
Enable/disable meta keys.
3610
3611
If yes is True, allow 8-bit characters to be input. If yes is False,
3612
allow only 7-bit characters.
3613
[clinic start generated code]*/
3614
3615
static PyObject *
3616
_curses_meta_impl(PyObject *module, int yes)
3617
/*[clinic end generated code: output=22f5abda46a605d8 input=cfe7da79f51d0e30]*/
3618
{
3619
PyCursesInitialised;
3620
3621
return PyCursesCheckERR(meta(stdscr, yes), "meta");
3622
}
3623
3624
#ifdef NCURSES_MOUSE_VERSION
3625
/*[clinic input]
3626
_curses.mouseinterval
3627
3628
interval: int
3629
Time in milliseconds.
3630
/
3631
3632
Set and retrieve the maximum time between press and release in a click.
3633
3634
Set the maximum time that can elapse between press and release events in
3635
order for them to be recognized as a click, and return the previous interval
3636
value.
3637
[clinic start generated code]*/
3638
3639
static PyObject *
3640
_curses_mouseinterval_impl(PyObject *module, int interval)
3641
/*[clinic end generated code: output=c4f5ff04354634c5 input=75aaa3f0db10ac4e]*/
3642
{
3643
PyCursesInitialised;
3644
3645
return PyCursesCheckERR(mouseinterval(interval), "mouseinterval");
3646
}
3647
3648
/*[clinic input]
3649
_curses.mousemask
3650
3651
newmask: unsigned_long(bitwise=True)
3652
/
3653
3654
Set the mouse events to be reported, and return a tuple (availmask, oldmask).
3655
3656
Return a tuple (availmask, oldmask). availmask indicates which of the
3657
specified mouse events can be reported; on complete failure it returns 0.
3658
oldmask is the previous value of the given window's mouse event mask.
3659
If this function is never called, no mouse events are ever reported.
3660
[clinic start generated code]*/
3661
3662
static PyObject *
3663
_curses_mousemask_impl(PyObject *module, unsigned long newmask)
3664
/*[clinic end generated code: output=9406cf1b8a36e485 input=bdf76b7568a3c541]*/
3665
{
3666
mmask_t oldmask, availmask;
3667
3668
PyCursesInitialised;
3669
availmask = mousemask((mmask_t)newmask, &oldmask);
3670
return Py_BuildValue("(kk)",
3671
(unsigned long)availmask, (unsigned long)oldmask);
3672
}
3673
#endif
3674
3675
/*[clinic input]
3676
_curses.napms
3677
3678
ms: int
3679
Duration in milliseconds.
3680
/
3681
3682
Sleep for specified time.
3683
[clinic start generated code]*/
3684
3685
static PyObject *
3686
_curses_napms_impl(PyObject *module, int ms)
3687
/*[clinic end generated code: output=a40a1da2e39ea438 input=20cd3af2b6900f56]*/
3688
{
3689
PyCursesInitialised;
3690
3691
return Py_BuildValue("i", napms(ms));
3692
}
3693
3694
3695
/*[clinic input]
3696
_curses.newpad
3697
3698
nlines: int
3699
Height.
3700
ncols: int
3701
Width.
3702
/
3703
3704
Create and return a pointer to a new pad data structure.
3705
[clinic start generated code]*/
3706
3707
static PyObject *
3708
_curses_newpad_impl(PyObject *module, int nlines, int ncols)
3709
/*[clinic end generated code: output=de52a56eb1098ec9 input=93f1272f240d8894]*/
3710
{
3711
WINDOW *win;
3712
3713
PyCursesInitialised;
3714
3715
win = newpad(nlines, ncols);
3716
3717
if (win == NULL) {
3718
PyErr_SetString(PyCursesError, catchall_NULL);
3719
return NULL;
3720
}
3721
3722
return (PyObject *)PyCursesWindow_New(win, NULL);
3723
}
3724
3725
/*[clinic input]
3726
_curses.newwin
3727
3728
nlines: int
3729
Height.
3730
ncols: int
3731
Width.
3732
[
3733
begin_y: int = 0
3734
Top side y-coordinate.
3735
begin_x: int = 0
3736
Left side x-coordinate.
3737
]
3738
/
3739
3740
Return a new window.
3741
3742
By default, the window will extend from the specified position to the lower
3743
right corner of the screen.
3744
[clinic start generated code]*/
3745
3746
static PyObject *
3747
_curses_newwin_impl(PyObject *module, int nlines, int ncols,
3748
int group_right_1, int begin_y, int begin_x)
3749
/*[clinic end generated code: output=c1e0a8dc8ac2826c input=29312c15a72a003d]*/
3750
{
3751
WINDOW *win;
3752
3753
PyCursesInitialised;
3754
3755
win = newwin(nlines,ncols,begin_y,begin_x);
3756
if (win == NULL) {
3757
PyErr_SetString(PyCursesError, catchall_NULL);
3758
return NULL;
3759
}
3760
3761
return (PyObject *)PyCursesWindow_New(win, NULL);
3762
}
3763
3764
/*[clinic input]
3765
_curses.nl
3766
3767
flag: bool = True
3768
If false, the effect is the same as calling nonl().
3769
/
3770
3771
Enter newline mode.
3772
3773
This mode translates the return key into newline on input, and translates
3774
newline into return and line-feed on output. Newline mode is initially on.
3775
[clinic start generated code]*/
3776
3777
static PyObject *
3778
_curses_nl_impl(PyObject *module, int flag)
3779
/*[clinic end generated code: output=b39cc0ffc9015003 input=18e3e9c6e8cfcf6f]*/
3780
NoArgOrFlagNoReturnFunctionBody(nl, flag)
3781
3782
/*[clinic input]
3783
_curses.nocbreak
3784
3785
Leave cbreak mode.
3786
3787
Return to normal "cooked" mode with line buffering.
3788
[clinic start generated code]*/
3789
3790
static PyObject *
3791
_curses_nocbreak_impl(PyObject *module)
3792
/*[clinic end generated code: output=eabf3833a4fbf620 input=e4b65f7d734af400]*/
3793
NoArgNoReturnFunctionBody(nocbreak)
3794
3795
/*[clinic input]
3796
_curses.noecho
3797
3798
Leave echo mode.
3799
3800
Echoing of input characters is turned off.
3801
[clinic start generated code]*/
3802
3803
static PyObject *
3804
_curses_noecho_impl(PyObject *module)
3805
/*[clinic end generated code: output=cc95ab45bc98f41b input=76714df529e614c3]*/
3806
NoArgNoReturnFunctionBody(noecho)
3807
3808
/*[clinic input]
3809
_curses.nonl
3810
3811
Leave newline mode.
3812
3813
Disable translation of return into newline on input, and disable low-level
3814
translation of newline into newline/return on output.
3815
[clinic start generated code]*/
3816
3817
static PyObject *
3818
_curses_nonl_impl(PyObject *module)
3819
/*[clinic end generated code: output=99e917e9715770c6 input=9d37dd122d3022fc]*/
3820
NoArgNoReturnFunctionBody(nonl)
3821
3822
/*[clinic input]
3823
_curses.noqiflush
3824
3825
Disable queue flushing.
3826
3827
When queue flushing is disabled, normal flush of input and output queues
3828
associated with the INTR, QUIT and SUSP characters will not be done.
3829
[clinic start generated code]*/
3830
3831
static PyObject *
3832
_curses_noqiflush_impl(PyObject *module)
3833
/*[clinic end generated code: output=8b95a4229bbf0877 input=ba3e6b2e3e54c4df]*/
3834
NoArgNoReturnVoidFunctionBody(noqiflush)
3835
3836
/*[clinic input]
3837
_curses.noraw
3838
3839
Leave raw mode.
3840
3841
Return to normal "cooked" mode with line buffering.
3842
[clinic start generated code]*/
3843
3844
static PyObject *
3845
_curses_noraw_impl(PyObject *module)
3846
/*[clinic end generated code: output=39894e5524c430cc input=6ec86692096dffb5]*/
3847
NoArgNoReturnFunctionBody(noraw)
3848
3849
/*[clinic input]
3850
_curses.pair_content
3851
3852
pair_number: pair
3853
The number of the color pair (0 - (COLOR_PAIRS-1)).
3854
/
3855
3856
Return a tuple (fg, bg) containing the colors for the requested color pair.
3857
[clinic start generated code]*/
3858
3859
static PyObject *
3860
_curses_pair_content_impl(PyObject *module, int pair_number)
3861
/*[clinic end generated code: output=4a726dd0e6885f3f input=03970f840fc7b739]*/
3862
{
3863
_CURSES_COLOR_NUM_TYPE f, b;
3864
3865
PyCursesInitialised;
3866
PyCursesInitialisedColor;
3867
3868
if (_CURSES_PAIR_CONTENT_FUNC(pair_number, &f, &b) == ERR) {
3869
if (pair_number >= COLOR_PAIRS) {
3870
PyErr_Format(PyExc_ValueError,
3871
"Color pair is greater than COLOR_PAIRS-1 (%d).",
3872
COLOR_PAIRS - 1);
3873
}
3874
else {
3875
PyErr_Format(PyCursesError, "%s() returned ERR",
3876
Py_STRINGIFY(_CURSES_PAIR_CONTENT_FUNC));
3877
}
3878
return NULL;
3879
}
3880
3881
return Py_BuildValue("(ii)", f, b);
3882
}
3883
3884
/*[clinic input]
3885
_curses.pair_number
3886
3887
attr: int
3888
/
3889
3890
Return the number of the color-pair set by the specified attribute value.
3891
3892
color_pair() is the counterpart to this function.
3893
[clinic start generated code]*/
3894
3895
static PyObject *
3896
_curses_pair_number_impl(PyObject *module, int attr)
3897
/*[clinic end generated code: output=85bce7d65c0aa3f4 input=d478548e33f5e61a]*/
3898
{
3899
PyCursesInitialised;
3900
PyCursesInitialisedColor;
3901
3902
return PyLong_FromLong(PAIR_NUMBER(attr));
3903
}
3904
3905
/*[clinic input]
3906
_curses.putp
3907
3908
string: str(accept={robuffer})
3909
/
3910
3911
Emit the value of a specified terminfo capability for the current terminal.
3912
3913
Note that the output of putp() always goes to standard output.
3914
[clinic start generated code]*/
3915
3916
static PyObject *
3917
_curses_putp_impl(PyObject *module, const char *string)
3918
/*[clinic end generated code: output=e98081d1b8eb5816 input=1601faa828b44cb3]*/
3919
{
3920
return PyCursesCheckERR(putp(string), "putp");
3921
}
3922
3923
/*[clinic input]
3924
_curses.qiflush
3925
3926
flag: bool = True
3927
If false, the effect is the same as calling noqiflush().
3928
/
3929
3930
Enable queue flushing.
3931
3932
If queue flushing is enabled, all output in the display driver queue
3933
will be flushed when the INTR, QUIT and SUSP characters are read.
3934
[clinic start generated code]*/
3935
3936
static PyObject *
3937
_curses_qiflush_impl(PyObject *module, int flag)
3938
/*[clinic end generated code: output=9167e862f760ea30 input=6ec8b3e2b717ec40]*/
3939
{
3940
PyCursesInitialised;
3941
3942
if (flag) {
3943
qiflush();
3944
}
3945
else {
3946
noqiflush();
3947
}
3948
Py_RETURN_NONE;
3949
}
3950
3951
/* Internal helper used for updating curses.LINES, curses.COLS, _curses.LINES
3952
* and _curses.COLS */
3953
#if defined(HAVE_CURSES_RESIZETERM) || defined(HAVE_CURSES_RESIZE_TERM)
3954
static int
3955
update_lines_cols(void)
3956
{
3957
PyObject *o;
3958
PyObject *m = PyImport_ImportModule("curses");
3959
3960
if (!m)
3961
return 0;
3962
3963
o = PyLong_FromLong(LINES);
3964
if (!o) {
3965
Py_DECREF(m);
3966
return 0;
3967
}
3968
if (PyObject_SetAttrString(m, "LINES", o)) {
3969
Py_DECREF(m);
3970
Py_DECREF(o);
3971
return 0;
3972
}
3973
if (PyDict_SetItemString(ModDict, "LINES", o)) {
3974
Py_DECREF(m);
3975
Py_DECREF(o);
3976
return 0;
3977
}
3978
Py_DECREF(o);
3979
o = PyLong_FromLong(COLS);
3980
if (!o) {
3981
Py_DECREF(m);
3982
return 0;
3983
}
3984
if (PyObject_SetAttrString(m, "COLS", o)) {
3985
Py_DECREF(m);
3986
Py_DECREF(o);
3987
return 0;
3988
}
3989
if (PyDict_SetItemString(ModDict, "COLS", o)) {
3990
Py_DECREF(m);
3991
Py_DECREF(o);
3992
return 0;
3993
}
3994
Py_DECREF(o);
3995
Py_DECREF(m);
3996
return 1;
3997
}
3998
3999
/*[clinic input]
4000
_curses.update_lines_cols
4001
4002
[clinic start generated code]*/
4003
4004
static PyObject *
4005
_curses_update_lines_cols_impl(PyObject *module)
4006
/*[clinic end generated code: output=423f2b1e63ed0f75 input=5f065ab7a28a5d90]*/
4007
{
4008
if (!update_lines_cols()) {
4009
return NULL;
4010
}
4011
Py_RETURN_NONE;
4012
}
4013
4014
#endif
4015
4016
/*[clinic input]
4017
_curses.raw
4018
4019
flag: bool = True
4020
If false, the effect is the same as calling noraw().
4021
/
4022
4023
Enter raw mode.
4024
4025
In raw mode, normal line buffering and processing of interrupt, quit,
4026
suspend, and flow control keys are turned off; characters are presented to
4027
curses input functions one by one.
4028
[clinic start generated code]*/
4029
4030
static PyObject *
4031
_curses_raw_impl(PyObject *module, int flag)
4032
/*[clinic end generated code: output=a750e4b342be015b input=4b447701389fb4df]*/
4033
NoArgOrFlagNoReturnFunctionBody(raw, flag)
4034
4035
/*[clinic input]
4036
_curses.reset_prog_mode
4037
4038
Restore the terminal to "program" mode, as previously saved by def_prog_mode().
4039
[clinic start generated code]*/
4040
4041
static PyObject *
4042
_curses_reset_prog_mode_impl(PyObject *module)
4043
/*[clinic end generated code: output=15eb765abf0b6575 input=3d82bea2b3243471]*/
4044
NoArgNoReturnFunctionBody(reset_prog_mode)
4045
4046
/*[clinic input]
4047
_curses.reset_shell_mode
4048
4049
Restore the terminal to "shell" mode, as previously saved by def_shell_mode().
4050
[clinic start generated code]*/
4051
4052
static PyObject *
4053
_curses_reset_shell_mode_impl(PyObject *module)
4054
/*[clinic end generated code: output=0238de2962090d33 input=1c738fa64bd1a24f]*/
4055
NoArgNoReturnFunctionBody(reset_shell_mode)
4056
4057
/*[clinic input]
4058
_curses.resetty
4059
4060
Restore terminal mode.
4061
[clinic start generated code]*/
4062
4063
static PyObject *
4064
_curses_resetty_impl(PyObject *module)
4065
/*[clinic end generated code: output=ff4b448e80a7cd63 input=940493de03624bb0]*/
4066
NoArgNoReturnFunctionBody(resetty)
4067
4068
#ifdef HAVE_CURSES_RESIZETERM
4069
/*[clinic input]
4070
_curses.resizeterm
4071
4072
nlines: int
4073
Height.
4074
ncols: int
4075
Width.
4076
/
4077
4078
Resize the standard and current windows to the specified dimensions.
4079
4080
Adjusts other bookkeeping data used by the curses library that record the
4081
window dimensions (in particular the SIGWINCH handler).
4082
[clinic start generated code]*/
4083
4084
static PyObject *
4085
_curses_resizeterm_impl(PyObject *module, int nlines, int ncols)
4086
/*[clinic end generated code: output=56d6bcc5194ad055 input=0fca02ebad5ffa82]*/
4087
{
4088
PyObject *result;
4089
4090
PyCursesInitialised;
4091
4092
result = PyCursesCheckERR(resizeterm(nlines, ncols), "resizeterm");
4093
if (!result)
4094
return NULL;
4095
if (!update_lines_cols()) {
4096
Py_DECREF(result);
4097
return NULL;
4098
}
4099
return result;
4100
}
4101
4102
#endif
4103
4104
#ifdef HAVE_CURSES_RESIZE_TERM
4105
/*[clinic input]
4106
_curses.resize_term
4107
4108
nlines: int
4109
Height.
4110
ncols: int
4111
Width.
4112
/
4113
4114
Backend function used by resizeterm(), performing most of the work.
4115
4116
When resizing the windows, resize_term() blank-fills the areas that are
4117
extended. The calling application should fill in these areas with appropriate
4118
data. The resize_term() function attempts to resize all windows. However,
4119
due to the calling convention of pads, it is not possible to resize these
4120
without additional interaction with the application.
4121
[clinic start generated code]*/
4122
4123
static PyObject *
4124
_curses_resize_term_impl(PyObject *module, int nlines, int ncols)
4125
/*[clinic end generated code: output=9e26d8b9ea311ed2 input=2197edd05b049ed4]*/
4126
{
4127
PyObject *result;
4128
4129
PyCursesInitialised;
4130
4131
result = PyCursesCheckERR(resize_term(nlines, ncols), "resize_term");
4132
if (!result)
4133
return NULL;
4134
if (!update_lines_cols()) {
4135
Py_DECREF(result);
4136
return NULL;
4137
}
4138
return result;
4139
}
4140
#endif /* HAVE_CURSES_RESIZE_TERM */
4141
4142
/*[clinic input]
4143
_curses.savetty
4144
4145
Save terminal mode.
4146
[clinic start generated code]*/
4147
4148
static PyObject *
4149
_curses_savetty_impl(PyObject *module)
4150
/*[clinic end generated code: output=6babc49f12b42199 input=fce6b2b7d2200102]*/
4151
NoArgNoReturnFunctionBody(savetty)
4152
4153
#ifdef getsyx
4154
/*[clinic input]
4155
_curses.setsyx
4156
4157
y: int
4158
Y-coordinate.
4159
x: int
4160
X-coordinate.
4161
/
4162
4163
Set the virtual screen cursor.
4164
4165
If y and x are both -1, then leaveok is set.
4166
[clinic start generated code]*/
4167
4168
static PyObject *
4169
_curses_setsyx_impl(PyObject *module, int y, int x)
4170
/*[clinic end generated code: output=23dcf753511a2464 input=fa7f2b208e10a557]*/
4171
{
4172
PyCursesInitialised;
4173
4174
setsyx(y,x);
4175
4176
Py_RETURN_NONE;
4177
}
4178
#endif
4179
4180
/*[clinic input]
4181
_curses.start_color
4182
4183
Initializes eight basic colors and global variables COLORS and COLOR_PAIRS.
4184
4185
Must be called if the programmer wants to use colors, and before any other
4186
color manipulation routine is called. It is good practice to call this
4187
routine right after initscr().
4188
4189
It also restores the colors on the terminal to the values they had when the
4190
terminal was just turned on.
4191
[clinic start generated code]*/
4192
4193
static PyObject *
4194
_curses_start_color_impl(PyObject *module)
4195
/*[clinic end generated code: output=8b772b41d8090ede input=0ca0ecb2b77e1a12]*/
4196
{
4197
int code;
4198
PyObject *c, *cp;
4199
4200
PyCursesInitialised;
4201
4202
code = start_color();
4203
if (code != ERR) {
4204
initialisedcolors = TRUE;
4205
c = PyLong_FromLong((long) COLORS);
4206
if (c == NULL)
4207
return NULL;
4208
if (PyDict_SetItemString(ModDict, "COLORS", c) < 0) {
4209
Py_DECREF(c);
4210
return NULL;
4211
}
4212
Py_DECREF(c);
4213
cp = PyLong_FromLong((long) COLOR_PAIRS);
4214
if (cp == NULL)
4215
return NULL;
4216
if (PyDict_SetItemString(ModDict, "COLOR_PAIRS", cp) < 0) {
4217
Py_DECREF(cp);
4218
return NULL;
4219
}
4220
Py_DECREF(cp);
4221
Py_RETURN_NONE;
4222
} else {
4223
PyErr_SetString(PyCursesError, "start_color() returned ERR");
4224
return NULL;
4225
}
4226
}
4227
4228
/*[clinic input]
4229
_curses.termattrs
4230
4231
Return a logical OR of all video attributes supported by the terminal.
4232
[clinic start generated code]*/
4233
4234
static PyObject *
4235
_curses_termattrs_impl(PyObject *module)
4236
/*[clinic end generated code: output=b06f437fce1b6fc4 input=0559882a04f84d1d]*/
4237
NoArgReturnIntFunctionBody(termattrs)
4238
4239
/*[clinic input]
4240
_curses.termname
4241
4242
Return the value of the environment variable TERM, truncated to 14 characters.
4243
[clinic start generated code]*/
4244
4245
static PyObject *
4246
_curses_termname_impl(PyObject *module)
4247
/*[clinic end generated code: output=96375577ebbd67fd input=33c08d000944f33f]*/
4248
NoArgReturnStringFunctionBody(termname)
4249
4250
/*[clinic input]
4251
_curses.tigetflag
4252
4253
capname: str
4254
The terminfo capability name.
4255
/
4256
4257
Return the value of the Boolean capability.
4258
4259
The value -1 is returned if capname is not a Boolean capability, or 0 if
4260
it is canceled or absent from the terminal description.
4261
[clinic start generated code]*/
4262
4263
static PyObject *
4264
_curses_tigetflag_impl(PyObject *module, const char *capname)
4265
/*[clinic end generated code: output=8853c0e55542195b input=b0787af9e3e9a6ce]*/
4266
{
4267
PyCursesSetupTermCalled;
4268
4269
return PyLong_FromLong( (long) tigetflag( (char *)capname ) );
4270
}
4271
4272
/*[clinic input]
4273
_curses.tigetnum
4274
4275
capname: str
4276
The terminfo capability name.
4277
/
4278
4279
Return the value of the numeric capability.
4280
4281
The value -2 is returned if capname is not a numeric capability, or -1 if
4282
it is canceled or absent from the terminal description.
4283
[clinic start generated code]*/
4284
4285
static PyObject *
4286
_curses_tigetnum_impl(PyObject *module, const char *capname)
4287
/*[clinic end generated code: output=46f8b0a1b5dff42f input=5cdf2f410b109720]*/
4288
{
4289
PyCursesSetupTermCalled;
4290
4291
return PyLong_FromLong( (long) tigetnum( (char *)capname ) );
4292
}
4293
4294
/*[clinic input]
4295
_curses.tigetstr
4296
4297
capname: str
4298
The terminfo capability name.
4299
/
4300
4301
Return the value of the string capability.
4302
4303
None is returned if capname is not a string capability, or is canceled or
4304
absent from the terminal description.
4305
[clinic start generated code]*/
4306
4307
static PyObject *
4308
_curses_tigetstr_impl(PyObject *module, const char *capname)
4309
/*[clinic end generated code: output=f22b576ad60248f3 input=36644df25c73c0a7]*/
4310
{
4311
PyCursesSetupTermCalled;
4312
4313
capname = tigetstr( (char *)capname );
4314
if (capname == NULL || capname == (char*) -1) {
4315
Py_RETURN_NONE;
4316
}
4317
return PyBytes_FromString( capname );
4318
}
4319
4320
/*[clinic input]
4321
_curses.tparm
4322
4323
str: str(accept={robuffer})
4324
Parameterized byte string obtained from the terminfo database.
4325
i1: int = 0
4326
i2: int = 0
4327
i3: int = 0
4328
i4: int = 0
4329
i5: int = 0
4330
i6: int = 0
4331
i7: int = 0
4332
i8: int = 0
4333
i9: int = 0
4334
/
4335
4336
Instantiate the specified byte string with the supplied parameters.
4337
[clinic start generated code]*/
4338
4339
static PyObject *
4340
_curses_tparm_impl(PyObject *module, const char *str, int i1, int i2, int i3,
4341
int i4, int i5, int i6, int i7, int i8, int i9)
4342
/*[clinic end generated code: output=599f62b615c667ff input=5e30b15786f032aa]*/
4343
{
4344
char* result = NULL;
4345
4346
PyCursesSetupTermCalled;
4347
4348
result = tparm((char *)str,i1,i2,i3,i4,i5,i6,i7,i8,i9);
4349
if (!result) {
4350
PyErr_SetString(PyCursesError, "tparm() returned NULL");
4351
return NULL;
4352
}
4353
4354
return PyBytes_FromString(result);
4355
}
4356
4357
#ifdef HAVE_CURSES_TYPEAHEAD
4358
/*[clinic input]
4359
_curses.typeahead
4360
4361
fd: int
4362
File descriptor.
4363
/
4364
4365
Specify that the file descriptor fd be used for typeahead checking.
4366
4367
If fd is -1, then no typeahead checking is done.
4368
[clinic start generated code]*/
4369
4370
static PyObject *
4371
_curses_typeahead_impl(PyObject *module, int fd)
4372
/*[clinic end generated code: output=084bb649d7066583 input=f2968d8e1805051b]*/
4373
{
4374
PyCursesInitialised;
4375
4376
return PyCursesCheckERR(typeahead( fd ), "typeahead");
4377
}
4378
#endif
4379
4380
/*[clinic input]
4381
_curses.unctrl
4382
4383
ch: object
4384
/
4385
4386
Return a string which is a printable representation of the character ch.
4387
4388
Control characters are displayed as a caret followed by the character,
4389
for example as ^C. Printing characters are left as they are.
4390
[clinic start generated code]*/
4391
4392
static PyObject *
4393
_curses_unctrl(PyObject *module, PyObject *ch)
4394
/*[clinic end generated code: output=8e07fafc430c9434 input=cd1e35e16cd1ace4]*/
4395
{
4396
chtype ch_;
4397
4398
PyCursesInitialised;
4399
4400
if (!PyCurses_ConvertToChtype(NULL, ch, &ch_))
4401
return NULL;
4402
4403
return PyBytes_FromString(unctrl(ch_));
4404
}
4405
4406
/*[clinic input]
4407
_curses.ungetch
4408
4409
ch: object
4410
/
4411
4412
Push ch so the next getch() will return it.
4413
[clinic start generated code]*/
4414
4415
static PyObject *
4416
_curses_ungetch(PyObject *module, PyObject *ch)
4417
/*[clinic end generated code: output=9b19d8268376d887 input=6681e6ae4c42e5eb]*/
4418
{
4419
chtype ch_;
4420
4421
PyCursesInitialised;
4422
4423
if (!PyCurses_ConvertToChtype(NULL, ch, &ch_))
4424
return NULL;
4425
4426
return PyCursesCheckERR(ungetch(ch_), "ungetch");
4427
}
4428
4429
#ifdef HAVE_NCURSESW
4430
/* Convert an object to a character (wchar_t):
4431
4432
- int
4433
- str of length 1
4434
4435
Return 1 on success, 0 on error. */
4436
static int
4437
PyCurses_ConvertToWchar_t(PyObject *obj,
4438
wchar_t *wch)
4439
{
4440
if (PyUnicode_Check(obj)) {
4441
wchar_t buffer[2];
4442
if (PyUnicode_AsWideChar(obj, buffer, 2) != 1) {
4443
PyErr_Format(PyExc_TypeError,
4444
"expect str of length 1 or int, "
4445
"got a str of length %zi",
4446
PyUnicode_GET_LENGTH(obj));
4447
return 0;
4448
}
4449
*wch = buffer[0];
4450
return 2;
4451
}
4452
else if (PyLong_CheckExact(obj)) {
4453
long value;
4454
int overflow;
4455
value = PyLong_AsLongAndOverflow(obj, &overflow);
4456
if (overflow) {
4457
PyErr_SetString(PyExc_OverflowError,
4458
"int doesn't fit in long");
4459
return 0;
4460
}
4461
*wch = (wchar_t)value;
4462
if ((long)*wch != value) {
4463
PyErr_Format(PyExc_OverflowError,
4464
"character doesn't fit in wchar_t");
4465
return 0;
4466
}
4467
return 1;
4468
}
4469
else {
4470
PyErr_Format(PyExc_TypeError,
4471
"expect str of length 1 or int, got %s",
4472
Py_TYPE(obj)->tp_name);
4473
return 0;
4474
}
4475
}
4476
4477
/*[clinic input]
4478
_curses.unget_wch
4479
4480
ch: object
4481
/
4482
4483
Push ch so the next get_wch() will return it.
4484
[clinic start generated code]*/
4485
4486
static PyObject *
4487
_curses_unget_wch(PyObject *module, PyObject *ch)
4488
/*[clinic end generated code: output=1974c9fb01d37863 input=0d56dc65a46feebb]*/
4489
{
4490
wchar_t wch;
4491
4492
PyCursesInitialised;
4493
4494
if (!PyCurses_ConvertToWchar_t(ch, &wch))
4495
return NULL;
4496
return PyCursesCheckERR(unget_wch(wch), "unget_wch");
4497
}
4498
#endif
4499
4500
#ifdef HAVE_CURSES_USE_ENV
4501
/*[clinic input]
4502
_curses.use_env
4503
4504
flag: bool
4505
/
4506
4507
Use environment variables LINES and COLUMNS.
4508
4509
If used, this function should be called before initscr() or newterm() are
4510
called.
4511
4512
When flag is False, the values of lines and columns specified in the terminfo
4513
database will be used, even if environment variables LINES and COLUMNS (used
4514
by default) are set, or if curses is running in a window (in which case
4515
default behavior would be to use the window size if LINES and COLUMNS are
4516
not set).
4517
[clinic start generated code]*/
4518
4519
static PyObject *
4520
_curses_use_env_impl(PyObject *module, int flag)
4521
/*[clinic end generated code: output=b2c445e435c0b164 input=06ac30948f2d78e4]*/
4522
{
4523
use_env(flag);
4524
Py_RETURN_NONE;
4525
}
4526
#endif
4527
4528
#ifndef STRICT_SYSV_CURSES
4529
/*[clinic input]
4530
_curses.use_default_colors
4531
4532
Allow use of default values for colors on terminals supporting this feature.
4533
4534
Use this to support transparency in your application. The default color
4535
is assigned to the color number -1.
4536
[clinic start generated code]*/
4537
4538
static PyObject *
4539
_curses_use_default_colors_impl(PyObject *module)
4540
/*[clinic end generated code: output=a3b81ff71dd901be input=656844367470e8fc]*/
4541
{
4542
int code;
4543
4544
PyCursesInitialised;
4545
PyCursesInitialisedColor;
4546
4547
code = use_default_colors();
4548
if (code != ERR) {
4549
Py_RETURN_NONE;
4550
} else {
4551
PyErr_SetString(PyCursesError, "use_default_colors() returned ERR");
4552
return NULL;
4553
}
4554
}
4555
#endif /* STRICT_SYSV_CURSES */
4556
4557
4558
#ifdef NCURSES_VERSION
4559
4560
PyDoc_STRVAR(ncurses_version__doc__,
4561
"curses.ncurses_version\n\
4562
\n\
4563
Ncurses version information as a named tuple.");
4564
4565
static PyStructSequence_Field ncurses_version_fields[] = {
4566
{"major", "Major release number"},
4567
{"minor", "Minor release number"},
4568
{"patch", "Patch release number"},
4569
{0}
4570
};
4571
4572
static PyStructSequence_Desc ncurses_version_desc = {
4573
"curses.ncurses_version", /* name */
4574
ncurses_version__doc__, /* doc */
4575
ncurses_version_fields, /* fields */
4576
3
4577
};
4578
4579
static PyObject *
4580
make_ncurses_version(PyTypeObject *type)
4581
{
4582
PyObject *ncurses_version;
4583
int pos = 0;
4584
4585
ncurses_version = PyStructSequence_New(type);
4586
if (ncurses_version == NULL) {
4587
return NULL;
4588
}
4589
4590
#define SetIntItem(flag) \
4591
PyStructSequence_SET_ITEM(ncurses_version, pos++, PyLong_FromLong(flag)); \
4592
if (PyErr_Occurred()) { \
4593
Py_CLEAR(ncurses_version); \
4594
return NULL; \
4595
}
4596
4597
SetIntItem(NCURSES_VERSION_MAJOR)
4598
SetIntItem(NCURSES_VERSION_MINOR)
4599
SetIntItem(NCURSES_VERSION_PATCH)
4600
#undef SetIntItem
4601
4602
return ncurses_version;
4603
}
4604
4605
#endif /* NCURSES_VERSION */
4606
4607
/*[clinic input]
4608
_curses.has_extended_color_support
4609
4610
Return True if the module supports extended colors; otherwise, return False.
4611
4612
Extended color support allows more than 256 color-pairs for terminals
4613
that support more than 16 colors (e.g. xterm-256color).
4614
[clinic start generated code]*/
4615
4616
static PyObject *
4617
_curses_has_extended_color_support_impl(PyObject *module)
4618
/*[clinic end generated code: output=68f1be2b57d92e22 input=4b905f046e35ee9f]*/
4619
{
4620
return PyBool_FromLong(_NCURSES_EXTENDED_COLOR_FUNCS);
4621
}
4622
4623
/* List of functions defined in the module */
4624
4625
static PyMethodDef PyCurses_methods[] = {
4626
_CURSES_BAUDRATE_METHODDEF
4627
_CURSES_BEEP_METHODDEF
4628
_CURSES_CAN_CHANGE_COLOR_METHODDEF
4629
_CURSES_CBREAK_METHODDEF
4630
_CURSES_COLOR_CONTENT_METHODDEF
4631
_CURSES_COLOR_PAIR_METHODDEF
4632
_CURSES_CURS_SET_METHODDEF
4633
_CURSES_DEF_PROG_MODE_METHODDEF
4634
_CURSES_DEF_SHELL_MODE_METHODDEF
4635
_CURSES_DELAY_OUTPUT_METHODDEF
4636
_CURSES_DOUPDATE_METHODDEF
4637
_CURSES_ECHO_METHODDEF
4638
_CURSES_ENDWIN_METHODDEF
4639
_CURSES_ERASECHAR_METHODDEF
4640
_CURSES_FILTER_METHODDEF
4641
_CURSES_FLASH_METHODDEF
4642
_CURSES_FLUSHINP_METHODDEF
4643
_CURSES_GETMOUSE_METHODDEF
4644
_CURSES_UNGETMOUSE_METHODDEF
4645
_CURSES_GETSYX_METHODDEF
4646
_CURSES_GETWIN_METHODDEF
4647
_CURSES_HAS_COLORS_METHODDEF
4648
_CURSES_HAS_EXTENDED_COLOR_SUPPORT_METHODDEF
4649
_CURSES_HAS_IC_METHODDEF
4650
_CURSES_HAS_IL_METHODDEF
4651
_CURSES_HAS_KEY_METHODDEF
4652
_CURSES_HALFDELAY_METHODDEF
4653
_CURSES_INIT_COLOR_METHODDEF
4654
_CURSES_INIT_PAIR_METHODDEF
4655
_CURSES_INITSCR_METHODDEF
4656
_CURSES_INTRFLUSH_METHODDEF
4657
_CURSES_ISENDWIN_METHODDEF
4658
_CURSES_IS_TERM_RESIZED_METHODDEF
4659
_CURSES_KEYNAME_METHODDEF
4660
_CURSES_KILLCHAR_METHODDEF
4661
_CURSES_LONGNAME_METHODDEF
4662
_CURSES_META_METHODDEF
4663
_CURSES_MOUSEINTERVAL_METHODDEF
4664
_CURSES_MOUSEMASK_METHODDEF
4665
_CURSES_NAPMS_METHODDEF
4666
_CURSES_NEWPAD_METHODDEF
4667
_CURSES_NEWWIN_METHODDEF
4668
_CURSES_NL_METHODDEF
4669
_CURSES_NOCBREAK_METHODDEF
4670
_CURSES_NOECHO_METHODDEF
4671
_CURSES_NONL_METHODDEF
4672
_CURSES_NOQIFLUSH_METHODDEF
4673
_CURSES_NORAW_METHODDEF
4674
_CURSES_PAIR_CONTENT_METHODDEF
4675
_CURSES_PAIR_NUMBER_METHODDEF
4676
_CURSES_PUTP_METHODDEF
4677
_CURSES_QIFLUSH_METHODDEF
4678
_CURSES_RAW_METHODDEF
4679
_CURSES_RESET_PROG_MODE_METHODDEF
4680
_CURSES_RESET_SHELL_MODE_METHODDEF
4681
_CURSES_RESETTY_METHODDEF
4682
_CURSES_RESIZETERM_METHODDEF
4683
_CURSES_RESIZE_TERM_METHODDEF
4684
_CURSES_SAVETTY_METHODDEF
4685
#if defined(NCURSES_EXT_FUNCS) && NCURSES_EXT_FUNCS >= 20081102
4686
_CURSES_GET_ESCDELAY_METHODDEF
4687
_CURSES_SET_ESCDELAY_METHODDEF
4688
#endif
4689
_CURSES_GET_TABSIZE_METHODDEF
4690
_CURSES_SET_TABSIZE_METHODDEF
4691
_CURSES_SETSYX_METHODDEF
4692
_CURSES_SETUPTERM_METHODDEF
4693
_CURSES_START_COLOR_METHODDEF
4694
_CURSES_TERMATTRS_METHODDEF
4695
_CURSES_TERMNAME_METHODDEF
4696
_CURSES_TIGETFLAG_METHODDEF
4697
_CURSES_TIGETNUM_METHODDEF
4698
_CURSES_TIGETSTR_METHODDEF
4699
_CURSES_TPARM_METHODDEF
4700
_CURSES_TYPEAHEAD_METHODDEF
4701
_CURSES_UNCTRL_METHODDEF
4702
_CURSES_UNGETCH_METHODDEF
4703
_CURSES_UPDATE_LINES_COLS_METHODDEF
4704
_CURSES_UNGET_WCH_METHODDEF
4705
_CURSES_USE_ENV_METHODDEF
4706
_CURSES_USE_DEFAULT_COLORS_METHODDEF
4707
{NULL, NULL} /* sentinel */
4708
};
4709
4710
/* Initialization function for the module */
4711
4712
4713
static struct PyModuleDef _cursesmodule = {
4714
PyModuleDef_HEAD_INIT,
4715
"_curses",
4716
NULL,
4717
-1,
4718
PyCurses_methods,
4719
NULL,
4720
NULL,
4721
NULL,
4722
NULL
4723
};
4724
4725
static void
4726
curses_destructor(PyObject *op)
4727
{
4728
void *ptr = PyCapsule_GetPointer(op, PyCurses_CAPSULE_NAME);
4729
Py_DECREF(*(void **)ptr);
4730
PyMem_Free(ptr);
4731
}
4732
4733
PyMODINIT_FUNC
4734
PyInit__curses(void)
4735
{
4736
PyObject *m, *d, *v, *c_api_object;
4737
4738
/* Initialize object type */
4739
if (PyType_Ready(&PyCursesWindow_Type) < 0)
4740
return NULL;
4741
4742
/* Create the module and add the functions */
4743
m = PyModule_Create(&_cursesmodule);
4744
if (m == NULL)
4745
return NULL;
4746
4747
/* Add some symbolic constants to the module */
4748
d = PyModule_GetDict(m);
4749
if (d == NULL)
4750
return NULL;
4751
ModDict = d; /* For PyCurses_InitScr to use later */
4752
4753
void **PyCurses_API = PyMem_Calloc(PyCurses_API_pointers, sizeof(void *));
4754
if (PyCurses_API == NULL) {
4755
PyErr_NoMemory();
4756
return NULL;
4757
}
4758
/* Initialize the C API pointer array */
4759
PyCurses_API[0] = (void *)Py_NewRef(&PyCursesWindow_Type);
4760
PyCurses_API[1] = (void *)func_PyCursesSetupTermCalled;
4761
PyCurses_API[2] = (void *)func_PyCursesInitialised;
4762
PyCurses_API[3] = (void *)func_PyCursesInitialisedColor;
4763
4764
/* Add a capsule for the C API */
4765
c_api_object = PyCapsule_New(PyCurses_API, PyCurses_CAPSULE_NAME,
4766
curses_destructor);
4767
if (c_api_object == NULL) {
4768
Py_DECREF(PyCurses_API[0]);
4769
PyMem_Free(PyCurses_API);
4770
return NULL;
4771
}
4772
if (PyDict_SetItemString(d, "_C_API", c_api_object) < 0) {
4773
Py_DECREF(c_api_object);
4774
return NULL;
4775
}
4776
Py_DECREF(c_api_object);
4777
4778
/* For exception curses.error */
4779
PyCursesError = PyErr_NewException("_curses.error", NULL, NULL);
4780
PyDict_SetItemString(d, "error", PyCursesError);
4781
4782
/* Make the version available */
4783
v = PyBytes_FromString(PyCursesVersion);
4784
PyDict_SetItemString(d, "version", v);
4785
PyDict_SetItemString(d, "__version__", v);
4786
Py_DECREF(v);
4787
4788
#ifdef NCURSES_VERSION
4789
/* ncurses_version */
4790
PyTypeObject *version_type;
4791
version_type = _PyStructSequence_NewType(&ncurses_version_desc,
4792
Py_TPFLAGS_DISALLOW_INSTANTIATION);
4793
if (version_type == NULL) {
4794
return NULL;
4795
}
4796
v = make_ncurses_version(version_type);
4797
Py_DECREF(version_type);
4798
if (v == NULL) {
4799
return NULL;
4800
}
4801
PyDict_SetItemString(d, "ncurses_version", v);
4802
Py_DECREF(v);
4803
#endif /* NCURSES_VERSION */
4804
4805
SetDictInt("ERR", ERR);
4806
SetDictInt("OK", OK);
4807
4808
/* Here are some attributes you can add to chars to print */
4809
4810
SetDictInt("A_ATTRIBUTES", A_ATTRIBUTES);
4811
SetDictInt("A_NORMAL", A_NORMAL);
4812
SetDictInt("A_STANDOUT", A_STANDOUT);
4813
SetDictInt("A_UNDERLINE", A_UNDERLINE);
4814
SetDictInt("A_REVERSE", A_REVERSE);
4815
SetDictInt("A_BLINK", A_BLINK);
4816
SetDictInt("A_DIM", A_DIM);
4817
SetDictInt("A_BOLD", A_BOLD);
4818
SetDictInt("A_ALTCHARSET", A_ALTCHARSET);
4819
SetDictInt("A_INVIS", A_INVIS);
4820
SetDictInt("A_PROTECT", A_PROTECT);
4821
SetDictInt("A_CHARTEXT", A_CHARTEXT);
4822
SetDictInt("A_COLOR", A_COLOR);
4823
4824
/* The following are never available with strict SYSV curses */
4825
#ifdef A_HORIZONTAL
4826
SetDictInt("A_HORIZONTAL", A_HORIZONTAL);
4827
#endif
4828
#ifdef A_LEFT
4829
SetDictInt("A_LEFT", A_LEFT);
4830
#endif
4831
#ifdef A_LOW
4832
SetDictInt("A_LOW", A_LOW);
4833
#endif
4834
#ifdef A_RIGHT
4835
SetDictInt("A_RIGHT", A_RIGHT);
4836
#endif
4837
#ifdef A_TOP
4838
SetDictInt("A_TOP", A_TOP);
4839
#endif
4840
#ifdef A_VERTICAL
4841
SetDictInt("A_VERTICAL", A_VERTICAL);
4842
#endif
4843
4844
/* ncurses extension */
4845
#ifdef A_ITALIC
4846
SetDictInt("A_ITALIC", A_ITALIC);
4847
#endif
4848
4849
SetDictInt("COLOR_BLACK", COLOR_BLACK);
4850
SetDictInt("COLOR_RED", COLOR_RED);
4851
SetDictInt("COLOR_GREEN", COLOR_GREEN);
4852
SetDictInt("COLOR_YELLOW", COLOR_YELLOW);
4853
SetDictInt("COLOR_BLUE", COLOR_BLUE);
4854
SetDictInt("COLOR_MAGENTA", COLOR_MAGENTA);
4855
SetDictInt("COLOR_CYAN", COLOR_CYAN);
4856
SetDictInt("COLOR_WHITE", COLOR_WHITE);
4857
4858
#ifdef NCURSES_MOUSE_VERSION
4859
/* Mouse-related constants */
4860
SetDictInt("BUTTON1_PRESSED", BUTTON1_PRESSED);
4861
SetDictInt("BUTTON1_RELEASED", BUTTON1_RELEASED);
4862
SetDictInt("BUTTON1_CLICKED", BUTTON1_CLICKED);
4863
SetDictInt("BUTTON1_DOUBLE_CLICKED", BUTTON1_DOUBLE_CLICKED);
4864
SetDictInt("BUTTON1_TRIPLE_CLICKED", BUTTON1_TRIPLE_CLICKED);
4865
4866
SetDictInt("BUTTON2_PRESSED", BUTTON2_PRESSED);
4867
SetDictInt("BUTTON2_RELEASED", BUTTON2_RELEASED);
4868
SetDictInt("BUTTON2_CLICKED", BUTTON2_CLICKED);
4869
SetDictInt("BUTTON2_DOUBLE_CLICKED", BUTTON2_DOUBLE_CLICKED);
4870
SetDictInt("BUTTON2_TRIPLE_CLICKED", BUTTON2_TRIPLE_CLICKED);
4871
4872
SetDictInt("BUTTON3_PRESSED", BUTTON3_PRESSED);
4873
SetDictInt("BUTTON3_RELEASED", BUTTON3_RELEASED);
4874
SetDictInt("BUTTON3_CLICKED", BUTTON3_CLICKED);
4875
SetDictInt("BUTTON3_DOUBLE_CLICKED", BUTTON3_DOUBLE_CLICKED);
4876
SetDictInt("BUTTON3_TRIPLE_CLICKED", BUTTON3_TRIPLE_CLICKED);
4877
4878
SetDictInt("BUTTON4_PRESSED", BUTTON4_PRESSED);
4879
SetDictInt("BUTTON4_RELEASED", BUTTON4_RELEASED);
4880
SetDictInt("BUTTON4_CLICKED", BUTTON4_CLICKED);
4881
SetDictInt("BUTTON4_DOUBLE_CLICKED", BUTTON4_DOUBLE_CLICKED);
4882
SetDictInt("BUTTON4_TRIPLE_CLICKED", BUTTON4_TRIPLE_CLICKED);
4883
4884
#if NCURSES_MOUSE_VERSION > 1
4885
SetDictInt("BUTTON5_PRESSED", BUTTON5_PRESSED);
4886
SetDictInt("BUTTON5_RELEASED", BUTTON5_RELEASED);
4887
SetDictInt("BUTTON5_CLICKED", BUTTON5_CLICKED);
4888
SetDictInt("BUTTON5_DOUBLE_CLICKED", BUTTON5_DOUBLE_CLICKED);
4889
SetDictInt("BUTTON5_TRIPLE_CLICKED", BUTTON5_TRIPLE_CLICKED);
4890
#endif
4891
4892
SetDictInt("BUTTON_SHIFT", BUTTON_SHIFT);
4893
SetDictInt("BUTTON_CTRL", BUTTON_CTRL);
4894
SetDictInt("BUTTON_ALT", BUTTON_ALT);
4895
4896
SetDictInt("ALL_MOUSE_EVENTS", ALL_MOUSE_EVENTS);
4897
SetDictInt("REPORT_MOUSE_POSITION", REPORT_MOUSE_POSITION);
4898
#endif
4899
/* Now set everything up for KEY_ variables */
4900
{
4901
int key;
4902
char *key_n;
4903
char *key_n2;
4904
for (key=KEY_MIN;key < KEY_MAX; key++) {
4905
key_n = (char *)keyname(key);
4906
if (key_n == NULL || strcmp(key_n,"UNKNOWN KEY")==0)
4907
continue;
4908
if (strncmp(key_n,"KEY_F(",6)==0) {
4909
char *p1, *p2;
4910
key_n2 = PyMem_Malloc(strlen(key_n)+1);
4911
if (!key_n2) {
4912
PyErr_NoMemory();
4913
break;
4914
}
4915
p1 = key_n;
4916
p2 = key_n2;
4917
while (*p1) {
4918
if (*p1 != '(' && *p1 != ')') {
4919
*p2 = *p1;
4920
p2++;
4921
}
4922
p1++;
4923
}
4924
*p2 = (char)0;
4925
} else
4926
key_n2 = key_n;
4927
SetDictInt(key_n2,key);
4928
if (key_n2 != key_n)
4929
PyMem_Free(key_n2);
4930
}
4931
SetDictInt("KEY_MIN", KEY_MIN);
4932
SetDictInt("KEY_MAX", KEY_MAX);
4933
}
4934
4935
if (PyModule_AddType(m, &PyCursesWindow_Type) < 0) {
4936
return NULL;
4937
}
4938
return m;
4939
}
4940
4941