-
Notifications
You must be signed in to change notification settings - Fork 851
Expand file tree
/
Copy pathwin32consolemodule.cpp
More file actions
2116 lines (1976 loc) · 100 KB
/
win32consolemodule.cpp
File metadata and controls
2116 lines (1976 loc) · 100 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// @doc
#include "PyWinTypes.h"
#include "PyWinObjects.h"
#include "structmember.h"
#include "malloc.h"
#define PyW32_BEGIN_ALLOW_THREADS PyThreadState *_save = PyEval_SaveThread()
#define PyW32_END_ALLOW_THREADS PyEval_RestoreThread(_save)
#define PyW32_BLOCK_THREADS Py_BLOCK_THREADS
// function pointers
#define CHECK_PFN(fname) \
if (pfn##fname == NULL) \
return PyErr_Format(PyExc_NotImplementedError, "%s is not available on this platform", #fname);
typedef DWORD(WINAPI *GetNumberOfConsoleFontsfunc)(VOID);
static GetNumberOfConsoleFontsfunc pfnGetNumberOfConsoleFonts = NULL;
typedef BOOL(WINAPI *SetConsoleFontfunc)(HANDLE, DWORD);
static SetConsoleFontfunc pfnSetConsoleFont = NULL;
// convert python object to array of WORDS/USHORTS
// ?????? should move this into Pywintypes, similar code used in win32security_ds.cpp
// to create an array of USHORTS
BOOL PyWinObject_AsUSHORTArray(PyObject *obushorts, USHORT **pushorts, DWORD *item_cnt, BOOL bNoneOk = TRUE)
{
BOOL ret = TRUE;
DWORD bufsize, tuple_index;
long short_candidate;
PyObject *ushorts_tuple = NULL, *tuple_item;
*pushorts = NULL;
if (obushorts == Py_None) {
if (bNoneOk)
return TRUE;
PyErr_SetString(PyExc_ValueError, "Sequence of unsigned shorts cannot be None");
return FALSE;
}
if ((ushorts_tuple = PyWinSequence_Tuple(obushorts, item_cnt)) == NULL)
return FALSE; // last exit without cleaning up
bufsize = *item_cnt * sizeof(USHORT);
*pushorts = (USHORT *)malloc(bufsize);
if (*pushorts == NULL) {
PyErr_Format(PyExc_MemoryError, "Unable to allocate %d bytes", bufsize);
ret = FALSE;
}
else
for (tuple_index = 0; tuple_index < *item_cnt; tuple_index++) {
tuple_item = PyTuple_GET_ITEM(ushorts_tuple, tuple_index);
short_candidate = PyLong_AsLong(tuple_item);
if (short_candidate == -1 && PyErr_Occurred()) {
ret = FALSE;
break;
}
else if (short_candidate < 0) {
PyErr_Format(PyExc_ValueError, "Unsigned short cannot be negative");
ret = FALSE;
break;
}
else if (short_candidate > USHRT_MAX) {
PyErr_Format(PyExc_ValueError, "Unsigned short cannot exceed %d", USHRT_MAX);
ret = FALSE;
break;
}
else
(*pushorts)[tuple_index] = (USHORT)short_candidate;
}
if (!ret)
if (*pushorts != NULL) {
free(*pushorts);
*pushorts = NULL;
}
Py_XDECREF(ushorts_tuple);
return ret;
}
// convert python object to single unicode character
// object *must* be unicode, and onechar should be allocated for a single WCHAR
// used mostly for putting a WCHAR inside an existing struct - would be nice if the
// structmember framework provided a format code for this
BOOL PyWinObject_AsSingleWCHAR(PyObject *obchar, WCHAR *onechar)
{
if (!PyUnicode_Check(obchar) || (PyUnicode_GetLength(obchar) != 1)) {
PyErr_SetString(PyExc_ValueError, "Object must be a single unicode character");
return FALSE;
}
#define PUAWC_TYPE PyObject *
if (PyUnicode_AsWideChar((PUAWC_TYPE)obchar, onechar, 1) == -1)
return FALSE;
return TRUE;
}
// @object PySMALL_RECT|Wrapper for a SMALL_RECT struct
// Create using PySMALL_RECTType(Left, Top, Right, Bottom). All params optional, defaulting to 0
class PySMALL_RECT : public PyObject {
public:
static struct PyMemberDef members[];
// static struct PyMethodDef methods[];
static void tp_dealloc(PyObject *ob);
SMALL_RECT rect;
PySMALL_RECT(SMALL_RECT *);
PySMALL_RECT(void);
static PyObject *tp_new(PyTypeObject *tp, PyObject *args, PyObject *kwargs);
static PyObject *tp_str(PyObject *self);
};
/*
static struct PyMethodDef PySMALL_RECT::methods[] =
{
{NULL}
};
*/
struct PyMemberDef PySMALL_RECT::members[] = {
{"Left", T_SHORT, offsetof(PySMALL_RECT, rect.Left), 0, NULL}, // @prop int|Left|Left side of rectangle
{"Top", T_SHORT, offsetof(PySMALL_RECT, rect.Top), 0, NULL}, // @prop int|Top|Top edge of rectangle
{"Right", T_SHORT, offsetof(PySMALL_RECT, rect.Right), 0, NULL}, // @prop int|Right|Right edge of rectangle
{"Bottom", T_SHORT, offsetof(PySMALL_RECT, rect.Bottom), 0, NULL}, // @prop int|Bottom|Bottome edge of rectangle
{NULL}};
static PyTypeObject PySMALL_RECTType = {
PYWIN_OBJECT_HEAD "PySMALL_RECT",
sizeof(PySMALL_RECT),
0,
PySMALL_RECT::tp_dealloc,
0, // tp_print
0, // tp_getattr
0, // tp_setattr
0, // tp_compare
PySMALL_RECT::tp_str, // tp_repr
0, // tp_as_number
0, // tp_as_sequence
0, // tp_as_mapping
0, // tp_hash
0, // tp_call
PySMALL_RECT::tp_str, // tp_str
PyObject_GenericGetAttr, // tp_getattro
PyObject_GenericSetAttr, // tp_setattro
0, // tp_as_buffer;
Py_TPFLAGS_DEFAULT, // tp_flags;
"Wrapper for a SMALL_RECT struct. Create using PySMALL_RECTType(Left, Top, Right, Bottom)", // tp_doc
0, // traverseproc tp_traverse;
0, // tp_clear;
0, // tp_richcompare;
0, // tp_weaklistoffset;
0, // tp_iter
0, // tp_iternext
0, // PySMALL_RECT::methods
PySMALL_RECT::members,
0,
0,
0,
0,
0,
0,
0,
0,
PySMALL_RECT::tp_new};
PyObject *PySMALL_RECT::tp_new(PyTypeObject *tp, PyObject *args, PyObject *kwargs)
{
SMALL_RECT sr;
ZeroMemory(&sr, sizeof(SMALL_RECT));
static char *keywords[] = {"Left", "Top", "Right", "Bottom", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|HHHH:PySMALL_RECTType", keywords, &sr.Left, &sr.Top, &sr.Right,
&sr.Bottom))
return NULL;
return new PySMALL_RECT(&sr);
}
PyObject *PySMALL_RECT::tp_str(PyObject *self)
{
char buf[100];
int chars_printed;
SMALL_RECT sr = ((PySMALL_RECT *)self)->rect;
chars_printed = _snprintf(buf, 100, "PySMALL_RECTType(Left=%d,Top=%d,Right=%d,Bottom=%d)", sr.Left, sr.Top,
sr.Right, sr.Bottom);
if (chars_printed < 0) {
PyErr_SetString(PyExc_SystemError, "String representation of PySMALL_RECT too long for buffer");
return NULL;
}
return PyWinCoreString_FromString(buf, chars_printed);
}
PySMALL_RECT::PySMALL_RECT(SMALL_RECT *psr)
{
ob_type = &PySMALL_RECTType;
rect = *psr;
_Py_NewReference(this);
}
PySMALL_RECT::PySMALL_RECT(void)
{
ob_type = &PySMALL_RECTType;
ZeroMemory(&rect, sizeof(SMALL_RECT));
_Py_NewReference(this);
}
void PySMALL_RECT::tp_dealloc(PyObject *ob) { delete (PySMALL_RECT *)ob; }
BOOL PySMALL_RECT_check(PyObject *ob)
{
if (Py_TYPE(ob) != &PySMALL_RECTType) {
PyErr_SetString(PyExc_TypeError, "Object must be a PySMALL_RECT");
return FALSE;
}
return TRUE;
}
BOOL PyWinObject_AsSMALL_RECT(PyObject *obrect, PSMALL_RECT *pprect, BOOL bNoneOk = TRUE)
{
*pprect = NULL;
if (obrect == Py_None) {
if (bNoneOk)
return TRUE;
PyErr_SetString(PyExc_ValueError, "SMALL_RECT cannot be None");
return FALSE;
}
if (!PySMALL_RECT_check(obrect))
return FALSE;
*pprect = &((PySMALL_RECT *)obrect)->rect;
return TRUE;
}
PyObject *PyWinObject_FromSMALL_RECT(PSMALL_RECT psr)
{
PyObject *ret = new PySMALL_RECT(psr);
if (ret == NULL)
PyErr_SetString(PyExc_MemoryError, "Unable to create PySMALL_RECT instance");
return ret;
}
// @object PyCOORD|Wrapper for a COORD struct. Create using PyCOORDType(X,Y)
class PyCOORD : public PyObject {
public:
static struct PyMemberDef members[];
// static struct PyMethodDef methods[];
static void deallocFunc(PyObject *ob);
COORD coord;
PyCOORD(COORD *);
PyCOORD(void);
static PyObject *tp_new(PyTypeObject *tp, PyObject *args, PyObject *kwargs);
static PyObject *tp_str(PyObject *self);
protected:
~PyCOORD();
};
/*
static struct PyMethodDef PyCOORD::methods[] =
{
{NULL}
};
*/
struct PyMemberDef PyCOORD::members[] = {
{"X", T_SHORT, offsetof(PyCOORD, coord.X), 0, "Horizontal coordinate"}, // @prop int|X|Horizontal coordinate
{"Y", T_SHORT, offsetof(PyCOORD, coord.Y), 0, "Vertical coordinate"}, // @prop int|Y|Vertical coordinate
{NULL}};
static PyTypeObject PyCOORDType = {PYWIN_OBJECT_HEAD "PyCOORD",
sizeof(PyCOORD),
0,
PyCOORD::deallocFunc,
0, // tp_print
0, // tp_getattr
0, // tp_setattr
0, // tp_compare
PyCOORD::tp_str, // tp_repr
0, // tp_as_number
0, // tp_as_sequence
0, // tp_as_mapping
0, // tp_hash
0, // tp_call
PyCOORD::tp_str, // tp_str
PyObject_GenericGetAttr, // tp_getattro
PyObject_GenericSetAttr, // tp_setattro
0, // tp_as_buffer;
Py_TPFLAGS_DEFAULT, // tp_flags;
"Wrapper for a COORD struct. Create using PyCOORDType(X,Y)", // tp_doc
0, // traverseproc tp_traverse;
0, // tp_clear;
0, // tp_richcompare;
0, // tp_weaklistoffset;
0, // tp_iter
0, // tp_iternext
0, // PyCOORD::methods // tp_methods
PyCOORD::members, // tp_members
0, // tp_getset
0, // tp_base
0, // tp_dict
0, // tp_descr_get
0, // tp_descr_set
0, // tp_dictoffset
0, // tp_init
0, // tp_alloc
PyCOORD::tp_new};
PyObject *PyCOORD::tp_new(PyTypeObject *tp, PyObject *args, PyObject *kwargs)
{
COORD coord;
ZeroMemory(&coord, sizeof(COORD));
static char *keywords[] = {"X", "Y", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|HH:PyCOORDType", keywords, &coord.X, &coord.Y))
return NULL;
return new PyCOORD(&coord);
}
PyObject *PyCOORD::tp_str(PyObject *self)
{
char buf[60];
int chars_printed;
COORD coord = ((PyCOORD *)self)->coord;
chars_printed = _snprintf(buf, 60, "PyCOORDType(X=%d,Y=%d)", coord.X, coord.Y);
if (chars_printed < 0) {
PyErr_SetString(PyExc_SystemError, "String representation of PyCOORD too long for buffer");
return NULL;
}
return PyWinCoreString_FromString(buf, chars_printed);
}
PyCOORD::PyCOORD(COORD *pcoord)
{
ob_type = &PyCOORDType;
coord = *pcoord;
_Py_NewReference(this);
}
PyCOORD::PyCOORD(void)
{
ob_type = &PyCOORDType;
ZeroMemory(&coord, sizeof(COORD));
_Py_NewReference(this);
}
PyCOORD::~PyCOORD() {}
void PyCOORD::deallocFunc(PyObject *ob) { delete (PyCOORD *)ob; }
BOOL PyCOORD_Check(PyObject *ob)
{
if (Py_TYPE(ob) != &PyCOORDType) {
PyErr_SetString(PyExc_TypeError, "Object must be a PyCOORD");
return FALSE;
}
return TRUE;
}
BOOL PyWinObject_AsCOORD(PyObject *obcoord, COORD **ppcoord, BOOL bNoneOk = TRUE)
{
*ppcoord = NULL;
if (obcoord == Py_None) {
if (bNoneOk)
return TRUE;
PyErr_SetString(PyExc_ValueError, "COORD must not be None in this context");
return FALSE;
}
if (!PyCOORD_Check(obcoord))
return FALSE;
*ppcoord = &((PyCOORD *)obcoord)->coord;
return TRUE;
}
PyObject *PyWinObject_FromCOORD(COORD *pcoord)
{
PyObject *ret = new PyCOORD(pcoord);
if (ret == NULL)
PyErr_SetString(PyExc_MemoryError, "Unable to create PyCOORD object");
return ret;
}
// @object PyINPUT_RECORD|Interface to the INPUT_RECORD struct used with console IO functions. Create using
// PyINPUT_RECORDType(EventType)
// @comm Only attributes that apply to each particular EventType can be accessed:<nl>
// KEY_EVENT: KeyDown, RepeatCount, VirtualKeyCode, VirtualScanCode, ControlKeyState<nl>
// MOUSE_EVENT: MousePosition, ButtonState, ControlKeyState, EventFlags<nl>
// WINDOW_BUFFER_SIZE_EVENT: Size<nl>
// FOCUS_EVENT: SetFocus<nl>
// MENU_EVENT: CommandId<nl>
class PyINPUT_RECORD : public PyObject {
public:
static struct PyMemberDef members[];
// static struct PyMethodDef methods[];
static void tp_dealloc(PyObject *self);
INPUT_RECORD input_record;
PyINPUT_RECORD(INPUT_RECORD *);
PyINPUT_RECORD(WORD EventType);
PyCOORD *obcoord;
static PyObject *tp_new(PyTypeObject *tp, PyObject *args, PyObject *kwargs);
static PyObject *tp_str(PyObject *self);
static PyObject *tp_getattro(PyObject *self, PyObject *obname);
static int tp_setattro(PyObject *self, PyObject *obname, PyObject *obvalue);
};
// Many of these are handled manually in PyINPUT_RECORD::tp_setattro and tp_getattro,
// but kept here so they are visible
struct PyMemberDef PyINPUT_RECORD::members[] = {
// @prop int|EventType|One of KEY_EVENT, MOUSE_EVENT, WINDOW_BUFFER_SIZE_EVENT, MENU_EVENT, FOCUS_EVENT. Cannot be
// changed after object is created
{"EventType", T_USHORT, offsetof(PyINPUT_RECORD, input_record.EventType), READONLY,
"One of KEY_EVENT, MOUSE_EVENT, WINDOW_BUFFER_SIZE_EVENT, MENU_EVENT, FOCUS_EVENT. Cannot be changed after "
"object is created"},
// @prop boolean|KeyDown|True for a key press, False for key release
{"KeyDown", T_LONG, offsetof(PyINPUT_RECORD, input_record.Event.KeyEvent.bKeyDown), 0,
"True for a key press, False for key release"},
// @prop int|RepeatCount|Nbr of repeats generated (key was held down if >1)
{"RepeatCount", T_USHORT, offsetof(PyINPUT_RECORD, input_record.Event.KeyEvent.wRepeatCount), 0,
"Nbr of repeats generated (key was held down if >1)"},
// @prop int|VirtualKeyCode|Device-independent key code, win32con.VK_*
{"VirtualKeyCode", T_USHORT, offsetof(PyINPUT_RECORD, input_record.Event.KeyEvent.wVirtualKeyCode), 0,
"Device-independent key code, win32con.VK_*"},
// @prop int|VirtualScanCode|Device-dependent scan code generated by keyboard
{"VirtualScanCode", T_USHORT, offsetof(PyINPUT_RECORD, input_record.Event.KeyEvent.wVirtualScanCode), 0,
"Device-dependent scan code generated by keyboard"},
// @prop <o PyUnicode>|Char|Single unicode character generated by the keypress
{"Char", T_LONG, 0, 0, "Single unicode character generated by the keypress"},
// @prop int|ControlKeyState|State of modifier keys, combination of CAPSLOCK_ON, ENHANCED_KEY, LEFT_ALT_PRESSED,
// LEFT_CTRL_PRESSED, NUMLOCK_ON, RIGHT_ALT_PRESSED, RIGHT_CTRL_PRESSED, SCROLLLOCK_ON, SHIFT_PRESSED
{"ControlKeyState", T_ULONG, 0, 0,
"State of modifier keys, combination of CAPSLOCK_ON, ENHANCED_KEY, LEFT_ALT_PRESSED, LEFT_CTRL_PRESSED,"
"NUMLOCK_ON, RIGHT_ALT_PRESSED, RIGHT_CTRL_PRESSED, SCROLLLOCK_ON, SHIFT_PRESSED"},
// @prop int|ButtonState|Bitmask representing which mouse buttons were pressed.
{"ButtonState", T_ULONG, offsetof(PyINPUT_RECORD, input_record.Event.MouseEvent.dwButtonState), 0,
"Bitmask representing which mouse buttons were pressed"},
// @prop int|EventFlags|DOUBLE_CLICK, MOUSE_MOVED or MOUSE_WHEELED, or 0. If 0, indicates a mouse button press
{"EventFlags", T_ULONG, offsetof(PyINPUT_RECORD, input_record.Event.MouseEvent.dwEventFlags), 0,
"DOUBLE_CLICK, MOUSE_MOVED or MOUSE_WHEELED, or 0. If 0, indicates a mouse button press"},
// @prop <o PyCOORD>|MousePosition|Position in character coordinates
{"MousePosition", T_ULONG, 0, 0, "Position in character coordinates"},
// @prop <o PyCOORD>|Size|New size of screen buffer in character rows/columns
{"Size", T_ULONG, 0, 0, "New size of screen buffer in character rows/columns"},
// @prop boolean|SetFocus|Reserved - Used only with type FOCUS_EVENT. This event is Reserved, and should be
// ignored.
{"SetFocus", T_ULONG, offsetof(PyINPUT_RECORD, input_record.Event.FocusEvent.bSetFocus), 0, "Reserved"},
// @prop int|CommandId|Used only with event type MENU_EVENT, which is reserved and should not be used
{"CommandId", T_ULONG, offsetof(PyINPUT_RECORD, input_record.Event.MenuEvent.dwCommandId), 0, "Reserved"},
{NULL}};
PyObject *PyINPUT_RECORD::tp_getattro(PyObject *self, PyObject *obname)
{
INPUT_RECORD *pir = &((PyINPUT_RECORD *)self)->input_record;
char *name = PYWIN_ATTR_CONVERT(obname);
if (name == NULL)
return NULL;
if (strcmp(name, "ControlKeyState") == 0) {
DWORD *src_ptr;
if (pir->EventType == KEY_EVENT)
src_ptr = &pir->Event.KeyEvent.dwControlKeyState;
else if (pir->EventType == MOUSE_EVENT)
src_ptr = &pir->Event.MouseEvent.dwControlKeyState;
else {
PyErr_SetString(PyExc_AttributeError, "'ConrolKeyState' is only valid for KEY_EVENT or MOUSE_EVENT");
return NULL;
}
return PyLong_FromUnsignedLong(*src_ptr);
}
if (strcmp(name, "Char") == 0) {
if (pir->EventType != KEY_EVENT) {
PyErr_SetString(PyExc_AttributeError, "'Char' is only valid for type KEY_EVENT");
return NULL;
}
return PyWinObject_FromWCHAR(&pir->Event.KeyEvent.uChar.UnicodeChar, 1);
}
if (strcmp(name, "Size") == 0) {
if (pir->EventType != WINDOW_BUFFER_SIZE_EVENT) {
PyErr_SetString(PyExc_AttributeError, "'Size' is only valid for type WINDOW_BUFFER_SIZE_EVENT");
return NULL;
}
Py_INCREF(((PyINPUT_RECORD *)self)->obcoord);
return ((PyINPUT_RECORD *)self)->obcoord;
}
if (strcmp(name, "MousePosition") == 0) {
if (pir->EventType != MOUSE_EVENT) {
PyErr_SetString(PyExc_AttributeError, "'MousePosition' is only valid for type MOUSE_EVENT");
return NULL;
}
Py_INCREF(((PyINPUT_RECORD *)self)->obcoord);
return ((PyINPUT_RECORD *)self)->obcoord;
}
return PyObject_GenericGetAttr(self, obname);
}
int PyINPUT_RECORD::tp_setattro(PyObject *self, PyObject *obname, PyObject *obvalue)
{
INPUT_RECORD *pir = &((PyINPUT_RECORD *)self)->input_record;
char *name;
name = PYWIN_ATTR_CONVERT(obname);
if (name == NULL)
return -1;
if (obvalue == NULL) {
PyErr_SetString(PyExc_AttributeError, "PyINPUT_RECORD members can't be removed");
return -1;
}
// ??? should probably add some EventType/attribute validation for everything done thru
// the normal structmember api also ???
if (strcmp(name, "ControlKeyState") == 0) {
// Event union contains 2 different ConrolKeyState's at different offsets depending on event type
DWORD *dest_ptr;
if (pir->EventType == KEY_EVENT)
dest_ptr = &pir->Event.KeyEvent.dwControlKeyState;
else if (pir->EventType == MOUSE_EVENT)
dest_ptr = &pir->Event.MouseEvent.dwControlKeyState;
else {
PyErr_SetString(PyExc_AttributeError, "'ConrolKeyState' is only valid for KEY_EVENT or MOUSE_EVENT");
return -1;
}
*dest_ptr = PyLong_AsUnsignedLongMask(obvalue);
if ((*dest_ptr == (DWORD)-1) && PyErr_Occurred())
return -1;
return 0;
}
if (strcmp(name, "Char") == 0) {
if (pir->EventType != KEY_EVENT) {
PyErr_SetString(PyExc_AttributeError, "'Char' is only valid for type KEY_EVENT");
return -1;
}
if (!PyWinObject_AsSingleWCHAR(obvalue, &pir->Event.KeyEvent.uChar.UnicodeChar))
return -1;
return 0;
}
if (strcmp(name, "Size") == 0) {
if (pir->EventType != WINDOW_BUFFER_SIZE_EVENT) {
PyErr_SetString(PyExc_AttributeError, "'Size' is only valid for type WINDOW_BUFFER_SIZE_EVENT");
return -1;
}
if (!PyCOORD_Check(obvalue))
return -1;
((PyINPUT_RECORD *)self)->input_record.Event.WindowBufferSizeEvent.dwSize = ((PyCOORD *)obvalue)->coord;
Py_DECREF(((PyINPUT_RECORD *)self)->obcoord);
Py_INCREF(obvalue);
((PyINPUT_RECORD *)self)->obcoord = (PyCOORD *)obvalue;
return 0;
}
if (strcmp(name, "MousePosition") == 0) {
if (pir->EventType != MOUSE_EVENT) {
PyErr_SetString(PyExc_AttributeError, "'MousePosition' is only valid for type MOUSE_EVENT");
return -1;
}
if (!PyCOORD_Check(obvalue))
return -1;
((PyINPUT_RECORD *)self)->input_record.Event.MouseEvent.dwMousePosition = ((PyCOORD *)obvalue)->coord;
Py_DECREF(((PyINPUT_RECORD *)self)->obcoord);
Py_INCREF(obvalue);
((PyINPUT_RECORD *)self)->obcoord = (PyCOORD *)obvalue;
return 0;
}
return PyObject_GenericSetAttr(self, obname, obvalue);
}
static PyTypeObject PyINPUT_RECORDType = {
PYWIN_OBJECT_HEAD "PyINPUT_RECORD",
sizeof(PyINPUT_RECORD),
0,
PyINPUT_RECORD::tp_dealloc,
0, // tp_print
0, // tp_getattr
0, // tp_setattr
0, // tp_compare
PyINPUT_RECORD::tp_str, // tp_repr
0, // tp_as_number
0, // tp_as_sequence
0, // tp_as_mapping
0, // tp_hash
0, // tp_call
PyINPUT_RECORD::tp_str, // tp_str
PyINPUT_RECORD::tp_getattro, // tp_getattro
PyINPUT_RECORD::tp_setattro, // tp_setattro
0, // tp_as_buffer;
Py_TPFLAGS_DEFAULT, // tp_flags;
"Wrapper for a INPUT_RECORD struct. Create using PyINPUT_RECORDType(EventType)", // tp_doc
0, // traverseproc tp_traverse;
0, // tp_clear;
0, // tp_richcompare;
0, // tp_weaklistoffset;
0, // tp_iter
0, // tp_iternext
0, // PySMALL_RECT::methods
PyINPUT_RECORD::members,
0,
0,
0,
0,
0,
0,
0,
0,
PyINPUT_RECORD::tp_new};
PyINPUT_RECORD::PyINPUT_RECORD(WORD EventType)
{
// EventType can't be changed after object is created
ob_type = &PyINPUT_RECORDType;
ZeroMemory(&input_record, sizeof(INPUT_RECORD));
input_record.EventType = EventType;
// keep a reference to a PyCOORD, used by 2 different types of events
if ((EventType == MOUSE_EVENT) || (EventType == WINDOW_BUFFER_SIZE_EVENT))
obcoord = new PyCOORD();
else
obcoord = NULL;
_Py_NewReference(this);
}
PyINPUT_RECORD::PyINPUT_RECORD(INPUT_RECORD *pinput_record)
{
ob_type = &PyINPUT_RECORDType;
input_record = *pinput_record;
if (input_record.EventType == MOUSE_EVENT)
obcoord = new PyCOORD(&input_record.Event.MouseEvent.dwMousePosition);
else if (input_record.EventType == WINDOW_BUFFER_SIZE_EVENT)
obcoord = new PyCOORD(&input_record.Event.WindowBufferSizeEvent.dwSize);
else
obcoord = NULL;
_Py_NewReference(this);
}
void PyINPUT_RECORD::tp_dealloc(PyObject *self)
{
Py_XDECREF(((PyINPUT_RECORD *)self)->obcoord);
delete (PyINPUT_RECORD *)self;
}
BOOL PyINPUT_RECORD_Check(PyObject *ob)
{
if (Py_TYPE(ob) != &PyINPUT_RECORDType) {
PyErr_SetString(PyExc_TypeError, "Object must be a PyINPUT_RECORD");
return FALSE;
}
return TRUE;
}
BOOL PyWinObject_AsINPUT_RECORD(PyObject *obir, INPUT_RECORD **ppir)
{
if (!PyINPUT_RECORD_Check(obir))
return FALSE;
*ppir = &((PyINPUT_RECORD *)obir)->input_record;
// pick up any changes to the PyCOORD associated with the input record
if ((*ppir)->EventType == MOUSE_EVENT)
(*ppir)->Event.MouseEvent.dwMousePosition = ((PyINPUT_RECORD *)obir)->obcoord->coord;
else if ((*ppir)->EventType == WINDOW_BUFFER_SIZE_EVENT)
(*ppir)->Event.WindowBufferSizeEvent.dwSize = ((PyINPUT_RECORD *)obir)->obcoord->coord;
return TRUE;
}
PyObject *PyWinObject_FromINPUT_RECORD(INPUT_RECORD *pinput_record)
{
PyObject *ret = new PyINPUT_RECORD(pinput_record);
if (ret == NULL)
PyErr_SetString(PyExc_MemoryError, "Unable to create PyINPUT_RECORD");
return ret;
}
PyObject *PyINPUT_RECORD::tp_new(PyTypeObject *tp, PyObject *args, PyObject *kwargs)
{
static char *keywords[] = {"EventType", NULL};
WORD EventType;
PyObject *ret;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "H:PyINPUT_RECORDType", keywords, &EventType))
return NULL;
ret = new PyINPUT_RECORD(EventType);
if (ret == NULL)
PyErr_SetString(PyExc_MemoryError, "Unable to create PyINPUT_RECORD object");
return ret;
}
PyObject *PyINPUT_RECORD::tp_str(PyObject *self)
{
char buf[100];
char *rec_type;
int chars_printed;
if (((PyINPUT_RECORD *)self)->input_record.EventType == KEY_EVENT)
rec_type = "KEY_EVENT";
else if (((PyINPUT_RECORD *)self)->input_record.EventType == MOUSE_EVENT)
rec_type = "MOUSE_EVENT";
else if (((PyINPUT_RECORD *)self)->input_record.EventType == WINDOW_BUFFER_SIZE_EVENT)
rec_type = "WINDOW_BUFFER_SIZE_EVENT";
else if (((PyINPUT_RECORD *)self)->input_record.EventType == MENU_EVENT)
rec_type = "MENU_EVENT";
else if (((PyINPUT_RECORD *)self)->input_record.EventType == FOCUS_EVENT)
rec_type = "FOCUS_EVENT";
else
rec_type = "<Unknown>";
chars_printed = _snprintf(buf, 100, "PyINPUT_RECORD(EventType=%d) (%s)",
((PyINPUT_RECORD *)self)->input_record.EventType, rec_type);
if (chars_printed < 0) {
PyErr_SetString(PyExc_SystemError, "String representation of PyINPUT_RECORD too long for buffer");
return NULL;
}
return PyWinCoreString_FromString(buf, chars_printed);
}
// @object PyConsoleScreenBuffer|Handle to a console screen buffer
// Create using <om win32console.CreateConsoleScreenBuffer> or <om win32console.GetStdHandle>
// Use PyConsoleScreenBufferType(Handle) to wrap a pre-existing handle as returned by <om win32api.GetStdHandle>.
// Will also accept a handle created by <om win32file.CreateFile> for CONIN$ or CONOUT$.
// When an existing handle is wrapped, a copy is made using DuplicateHandle, and caller is still responsible
// for any cleanup of original handle.
class PyConsoleScreenBuffer : public PyHANDLE {
public:
PyConsoleScreenBuffer(HANDLE hconsole);
~PyConsoleScreenBuffer(void);
static void tp_dealloc(PyObject *ob);
const char *GetTypeName() { return "PyConsoleScreenBuffer"; }
// static struct PyMemberDef members[];
static struct PyMethodDef methods[];
static PyObject *tp_new(PyTypeObject *tp, PyObject *args, PyObject *kwargs);
static PyObject *PySetConsoleActiveScreenBuffer(PyObject *self, PyObject *args);
static PyObject *PyGetConsoleCursorInfo(PyObject *self, PyObject *args);
static PyObject *PySetConsoleCursorInfo(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *PyGetConsoleMode(PyObject *self, PyObject *args);
static PyObject *PySetConsoleMode(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *PyReadConsole(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *PyWriteConsole(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *PyFlushConsoleInputBuffer(PyObject *self, PyObject *args);
static PyObject *PySetConsoleTextAttribute(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *PySetConsoleCursorPosition(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *PySetConsoleScreenBufferSize(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *PySetConsoleWindowInfo(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *PyGetConsoleScreenBufferInfo(PyObject *self, PyObject *args);
static PyObject *PyGetLargestConsoleWindowSize(PyObject *self, PyObject *args);
static PyObject *PyFillConsoleOutputAttribute(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *PyFillConsoleOutputCharacter(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *PyReadConsoleOutputCharacter(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *PyReadConsoleOutputAttribute(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *PyWriteConsoleOutputCharacter(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *PyWriteConsoleOutputAttribute(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *PyScrollConsoleScreenBuffer(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *PyGetCurrentConsoleFont(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *PyGetConsoleFontSize(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *PySetConsoleFont(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *PySetStdHandle(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *PySetConsoleDisplayMode(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *PyWriteConsoleInput(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *PyReadConsoleInput(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *PyPeekConsoleInput(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *PyGetNumberOfConsoleInputEvents(PyObject *self, PyObject *args);
};
struct PyMethodDef PyConsoleScreenBuffer::methods[] = {
//@pymeth Detach|Releases reference to handle without closing it
{"Detach", PyHANDLE::Detach, METH_VARARGS, "Releases reference to handle without closing it"},
//@pymeth Close|Closes the handle
{"Close", PyHANDLE::Close, METH_VARARGS, "Closes the handle"},
// @pymeth SetConsoleActiveScreenBuffer|Sets this handle as the currently display screen buffer
{"SetConsoleActiveScreenBuffer", PyConsoleScreenBuffer::PySetConsoleActiveScreenBuffer, METH_VARARGS,
"Sets this handle as the currently displayed screen buffer"},
// @pymeth GetConsoleCursorInfo|Retrieves size and visibility of console's cursor
{"GetConsoleCursorInfo", PyConsoleScreenBuffer::PyGetConsoleCursorInfo, METH_VARARGS,
"Retrieves size and visibility of console's cursor"},
// @pymeth SetConsoleCursorInfo|Sets the size and visibility of console's cursor
{"SetConsoleCursorInfo", (PyCFunction)PyConsoleScreenBuffer::PySetConsoleCursorInfo, METH_VARARGS | METH_KEYWORDS,
"Sets the size and visibility of console's cursor"},
// @pymeth GetConsoleMode|Returns the input or output mode of the console buffer
{"GetConsoleMode", PyConsoleScreenBuffer::PyGetConsoleMode, METH_VARARGS,
"Returns the input or output mode of the console buffer"},
// @pymeth SetConsoleMode|Sets the input or output mode of the console buffer
{"SetConsoleMode", (PyCFunction)PyConsoleScreenBuffer::PySetConsoleMode, METH_VARARGS | METH_KEYWORDS,
"Sets the input or output mode of the console buffer"},
// @pymeth ReadConsole|Reads characters from the console input buffer
{"ReadConsole", (PyCFunction)PyConsoleScreenBuffer::PyReadConsole, METH_VARARGS | METH_KEYWORDS,
"Reads characters from the console input buffer"},
// @pymeth WriteConsole|Writes characters at current cursor position
{"WriteConsole", (PyCFunction)PyConsoleScreenBuffer::PyWriteConsole, METH_VARARGS | METH_KEYWORDS,
"Writes characters at current cursor position"},
// @pymeth FlushConsoleInputBuffer|Flush input buffer for console
{"FlushConsoleInputBuffer", PyConsoleScreenBuffer::PyFlushConsoleInputBuffer, METH_VARARGS,
"Flush input buffer for console"},
// @pymeth SetConsoleTextAttribute|Sets character attributes for subsequent write operations
{"SetConsoleTextAttribute", (PyCFunction)PyConsoleScreenBuffer::PySetConsoleTextAttribute,
METH_VARARGS | METH_KEYWORDS, "Sets character attributes for subsequent write operations"},
// @pymeth SetConsoleCursorPosition|Sets the console screen buffer's cursor position
{"SetConsoleCursorPosition", (PyCFunction)PyConsoleScreenBuffer::PySetConsoleCursorPosition,
METH_VARARGS | METH_KEYWORDS, "Sets the console screen buffer's cursor position"},
// @pymeth SetConsoleScreenBufferSize|Sets the size of the console screen buffer
{"SetConsoleScreenBufferSize", (PyCFunction)PyConsoleScreenBuffer::PySetConsoleScreenBufferSize,
METH_VARARGS | METH_KEYWORDS, "Sets the size of the console screen buffer"},
// @pymeth SetConsoleWindowInfo|Changes size and position of a console's window
{"SetConsoleWindowInfo", (PyCFunction)PyConsoleScreenBuffer::PySetConsoleWindowInfo, METH_VARARGS | METH_KEYWORDS,
"Changes size and position of a console's window"},
// @pymeth GetConsoleScreenBufferInfo|Returns the state of the screen buffer
{"GetConsoleScreenBufferInfo", PyConsoleScreenBuffer::PyGetConsoleScreenBufferInfo, METH_VARARGS,
"Returns the state of the screen buffer"},
// @pymeth GetLargestConsoleWindowSize|Returns the largest possible size for the console's window
{"GetLargestConsoleWindowSize", PyConsoleScreenBuffer::PyGetLargestConsoleWindowSize, METH_VARARGS,
"Returns the largest possible size for the console's window"},
// @pymeth FillConsoleOutputAttribute|Set text attributes for a consecutive series of characters
{"FillConsoleOutputAttribute", (PyCFunction)PyConsoleScreenBuffer::PyFillConsoleOutputAttribute,
METH_VARARGS | METH_KEYWORDS, "Sets text attributes for a consecutive series of characters"},
// @pymeth FillConsoleOutputCharacter|Sets consecutive character positions to a specified character
{"FillConsoleOutputCharacter", (PyCFunction)PyConsoleScreenBuffer::PyFillConsoleOutputCharacter,
METH_VARARGS | METH_KEYWORDS, "Sets consecutive character positions to a specified character"},
// @pymeth ReadConsoleOutputCharacter|Reads consecutive characters from a starting position
{"ReadConsoleOutputCharacter", (PyCFunction)PyConsoleScreenBuffer::PyReadConsoleOutputCharacter,
METH_VARARGS | METH_KEYWORDS, "Reads consecutive characters from a starting position"},
// @pymeth ReadConsoleOutputAttribute|Retrieves attributes from consecutive character cells
{"ReadConsoleOutputAttribute", (PyCFunction)PyConsoleScreenBuffer::PyReadConsoleOutputAttribute,
METH_VARARGS | METH_KEYWORDS, "Retrieves attributes from consecutive character cells"},
// @pymeth WriteConsoleOutputCharacter|Writes a string of characters at a specified position
{"WriteConsoleOutputCharacter", (PyCFunction)PyConsoleScreenBuffer::PyWriteConsoleOutputCharacter,
METH_VARARGS | METH_KEYWORDS, "Writes a string of characters at a specified position"},
// @pymeth WriteConsoleOutputAttribute|Sets the attributes of a range of character cells
{"WriteConsoleOutputAttribute", (PyCFunction)PyConsoleScreenBuffer::PyWriteConsoleOutputAttribute,
METH_VARARGS | METH_KEYWORDS, "Sets the attributes of a range of character cells"},
// @pymeth ScrollConsoleScreenBuffer|Scrolls a region of the display
{"ScrollConsoleScreenBuffer", (PyCFunction)PyConsoleScreenBuffer::PyScrollConsoleScreenBuffer,
METH_VARARGS | METH_KEYWORDS, "Scrolls a region of the display"},
// @pymeth GetCurrentConsoleFont|Returns the currently displayed font
{"GetCurrentConsoleFont", (PyCFunction)PyConsoleScreenBuffer::PyGetCurrentConsoleFont, METH_VARARGS | METH_KEYWORDS,
"Returns the currently displayed font"},
// @pymeth GetConsoleFontSize|Returns size of specified font for the console
{"GetConsoleFontSize", (PyCFunction)PyConsoleScreenBuffer::PyGetConsoleFontSize, METH_VARARGS | METH_KEYWORDS,
"Returns size of specified font for the console"},
// @pymeth SetConsoleFont|Changes the font used by the screen buffer
{"SetConsoleFont", (PyCFunction)PyConsoleScreenBuffer::PySetConsoleFont, METH_VARARGS | METH_KEYWORDS,
"Changes the font used by the screen buffer"},
// @pymeth SetStdHandle|Replaces one of calling process's standard handles with this handle
{"SetStdHandle", (PyCFunction)PyConsoleScreenBuffer::PySetStdHandle, METH_VARARGS | METH_KEYWORDS,
"Replaces one of calling process's standard handles with this handle"},
// @pymeth SetConsoleDisplayMode|Sets the display mode of the console buffer
{"SetConsoleDisplayMode", (PyCFunction)PyConsoleScreenBuffer::PySetConsoleDisplayMode, METH_VARARGS | METH_KEYWORDS,
"Sets the display mode of the console buffer"},
// @pymeth WriteConsoleInput|Places input records in the console's input queue
{"WriteConsoleInput", (PyCFunction)PyConsoleScreenBuffer::PyWriteConsoleInput, METH_VARARGS | METH_KEYWORDS,
"Places input records in the console's input queue"},
// @pymeth ReadConsoleInput|Reads input records and removes them from the input queue
{"ReadConsoleInput", (PyCFunction)PyConsoleScreenBuffer::PyReadConsoleInput, METH_VARARGS | METH_KEYWORDS,
"Reads input records and removes them from the input queue"},
// @pymeth PeekConsoleInput|Returns pending input records without removing them from the input queue
{"PeekConsoleInput", (PyCFunction)PyConsoleScreenBuffer::PyPeekConsoleInput, METH_VARARGS | METH_KEYWORDS,
"Returns pending input records without removing them from the input queue"},
// @pymeth GetNumberOfConsoleInputEvents|Returns the number of unread records in the input queue
{"GetNumberOfConsoleInputEvents", PyConsoleScreenBuffer::PyGetNumberOfConsoleInputEvents, METH_VARARGS,
"Returns the number of unread records in the input queue"},
{NULL}};
// @pymethod |PyConsoleScreenBuffer|SetConsoleActiveScreenBuffer|Sets this handle as the currently displayed screen
// buffer
PyObject *PyConsoleScreenBuffer::PySetConsoleActiveScreenBuffer(PyObject *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, ":SetConsoleActiveScreenBuffer"))
return NULL;
if (!SetConsoleActiveScreenBuffer(((PyConsoleScreenBuffer *)self)->m_handle))
return PyWin_SetAPIError("SetConsoleActiveScreenBuffer");
Py_INCREF(Py_None);
return Py_None;
}
// @pymethod (Size, bVisible)|PyConsoleScreenBuffer|GetConsoleCursorInfo|Retrieves size and visibility of console's
// cursor
// @rdesc Returns the size of the console's cursor expressed as a percentage of character size, and a boolen indicating
// if cursor is visible
PyObject *PyConsoleScreenBuffer::PyGetConsoleCursorInfo(PyObject *self, PyObject *args)
{
CONSOLE_CURSOR_INFO cci;
if (!PyArg_ParseTuple(args, ":GetConsoleCursorInfo"))
return NULL;
if (!GetConsoleCursorInfo(((PyConsoleScreenBuffer *)self)->m_handle, &cci))
return PyWin_SetAPIError("GetConsoleCursorInfo");
return Py_BuildValue("ll", cci.dwSize, cci.bVisible);
}
// @pymethod |PyConsoleScreenBuffer|SetConsoleCursorInfo|Sets the size and visibility of console's cursor
PyObject *PyConsoleScreenBuffer::PySetConsoleCursorInfo(PyObject *self, PyObject *args, PyObject *kwargs)
{
static char *keywords[] = {"Size", "Visible", NULL};
CONSOLE_CURSOR_INFO cci;
if (!PyArg_ParseTupleAndKeywords(
args, kwargs, "kk:SetConsoleCursorInfo", keywords,
&cci.dwSize, // @pyparm int|Size||Percentage of character size that cursor will occupy
&cci.bVisible)) // @pyparm boolen|Visible||Determines if cursor is visible
return NULL;
if (!SetConsoleCursorInfo(((PyConsoleScreenBuffer *)self)->m_handle, &cci))
return PyWin_SetAPIError("SetConsoleCursorInfo");
Py_INCREF(Py_None);
return Py_None;
}
// @pymethod int|PyConsoleScreenBuffer|GetConsoleMode|Returns the input or output mode of the console buffer
// @rdesc Returns a combination of ENABLE_*_INPUT or ENABLE_*_OUTPUT constants
PyObject *PyConsoleScreenBuffer::PyGetConsoleMode(PyObject *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, ":GetConsoleMode"))
return NULL;
DWORD mode;
if (!GetConsoleMode(((PyConsoleScreenBuffer *)self)->m_handle, &mode))
return PyWin_SetAPIError("GetConsoleMode");
return PyLong_FromLong(mode);
}
// @pymethod |PyConsoleScreenBuffer|SetConsoleMode|Sets the input or output mode of the console buffer
PyObject *PyConsoleScreenBuffer::PySetConsoleMode(PyObject *self, PyObject *args, PyObject *kwargs)
{
static char *keywords[] = {"Mode", NULL};
DWORD mode;
if (!PyArg_ParseTupleAndKeywords(
args, kwargs, "k:SetConsoleMode", keywords,
&mode)) // @pyparm int|Mode||Combination of ENABLE_*_INPUT or ENABLE_*_OUTPUT constants
return NULL;
if (!SetConsoleMode(((PyConsoleScreenBuffer *)self)->m_handle, mode))
return PyWin_SetAPIError("SetConsoleMode");
Py_INCREF(Py_None);
return Py_None;
}
// @pymethod <o PyUNICODE>|PyConsoleScreenBuffer|ReadConsole|Reads characters from the console input buffer
PyObject *PyConsoleScreenBuffer::PyReadConsole(PyObject *self, PyObject *args, PyObject *kwargs)
{
PyObject *ret = NULL;
WCHAR *buf = NULL;
LPVOID reserved = NULL;
DWORD nbrtoread, nbrread;
static char *keywords[] = {"NumberOfCharsToRead", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "l:ReadConsole", keywords,
&nbrtoread)) // @pyparm int|NumberOfCharsToRead||Characters to read
return NULL;
buf = (WCHAR *)malloc(nbrtoread * sizeof(WCHAR));
if (buf == NULL)
return PyErr_Format(PyExc_MemoryError, "ReadConsole: Unable to allocate buffer of %d bytes",
nbrtoread * sizeof(WCHAR));
if (!ReadConsole(((PyConsoleScreenBuffer *)self)->m_handle, (LPVOID)buf, nbrtoread, &nbrread,
(PCONSOLE_READCONSOLE_CONTROL)reserved))
PyWin_SetAPIError("ReadConsole");
else
ret = PyWinObject_FromWCHAR(buf, nbrread);
free(buf);
return ret;
}
// @pymethod int|PyConsoleScreenBuffer|WriteConsole|Writes characters at current cursor position
// @rdesc Returns the number of characters written
PyObject *PyConsoleScreenBuffer::PyWriteConsole(PyObject *self, PyObject *args, PyObject *kwargs)
{
WCHAR *buf = NULL;
PyObject *obbuf, *ret = NULL;
LPVOID reserved = NULL;
DWORD nbrtowrite, nbrwritten;
static char *keywords[] = {"Buffer", NULL};
if (!PyArg_ParseTupleAndKeywords(
args, kwargs, "O:WriteConsole", keywords,
&obbuf)) // @pyparm <o PyUNICODE>|Buffer||String or Unicode to be written to console
return NULL;
if (!PyWinObject_AsWCHAR(obbuf, &buf, FALSE, &nbrtowrite))
return NULL;
if (!WriteConsole(((PyConsoleScreenBuffer *)self)->m_handle, (LPVOID)buf, nbrtowrite, &nbrwritten, reserved))
PyWin_SetAPIError("WriteConsole");
else
ret = PyLong_FromLong(nbrwritten);
PyWinObject_FreeWCHAR(buf);
return ret;
}
// @pymethod |PyConsoleScreenBuffer|FlushConsoleInputBuffer|Flush input buffer
PyObject *PyConsoleScreenBuffer::PyFlushConsoleInputBuffer(PyObject *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, ":FlushConsoleInputBuffer"))
return NULL;
if (!FlushConsoleInputBuffer(((PyConsoleScreenBuffer *)self)->m_handle))
return PyWin_SetAPIError("FlushConsoleInputBuffer");
Py_INCREF(Py_None);
return Py_None;
}
// @pymethod |PyConsoleScreenBuffer|SetConsoleTextAttribute|Sets character attributes for subsequent write operations
PyObject *PyConsoleScreenBuffer::PySetConsoleTextAttribute(PyObject *self, PyObject *args, PyObject *kwargs)
{
static char *keywords[] = {"Attributes", NULL};
WORD Attributes;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "H:SetConsoleTextAttribute", keywords,
&Attributes)) // @pyparm int|Attributes||Attributes to be set, combination of
// FOREGROUND_*, BACKGROUND_*, and COMMON_LVB_* constants
return NULL;
if (!SetConsoleTextAttribute(((PyConsoleScreenBuffer *)self)->m_handle, Attributes))
return PyWin_SetAPIError("SetConsoleTextAttribute");
Py_INCREF(Py_None);
return Py_None;
}
// @pymethod |PyConsoleScreenBuffer|SetConsoleCursorPosition|Sets the console screen buffer's cursor position
PyObject *PyConsoleScreenBuffer::PySetConsoleCursorPosition(PyObject *self, PyObject *args, PyObject *kwargs)
{
static char *keywords[] = {"CursorPosition", NULL};
PyObject *obcoord;
PCOORD pcoord;
if (!PyArg_ParseTupleAndKeywords(
args, kwargs, "O:SetConsoleCursorPosition", keywords,
&obcoord)) // @pyparm <o PyCOORD>|CursorPosition||A PyCOORD containing the new cursor position
return NULL;
if (!PyWinObject_AsCOORD(obcoord, &pcoord, FALSE))