-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic_input.c
More file actions
55 lines (49 loc) · 1.26 KB
/
dynamic_input.c
File metadata and controls
55 lines (49 loc) · 1.26 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
#include <Python.h>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
/* reads from keypress, doesn't echo */
char _getch(void)
{
struct termios oldattr, newattr;
char ch;
tcgetattr( STDIN_FILENO, &oldattr );
newattr = oldattr;
newattr.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
return ch;
}
/* gets a char and builds a Python bytes class */
static PyObject *
get(PyObject *self, PyObject *args)
{
char c = _getch();
return Py_BuildValue("c", c);
}
/*
list of module methods and their metadata
only one needed here, but can expand
just add another {} after the {} with get's data
*/
static PyMethodDef dynamicInputMethods[] = {
{
"get",
get,
METH_VARARGS,
"Method allows to silently read a character without user having to press enter."
}
};
/* definition of the module */
static struct PyModuleDef dynamicInputModule = {
PyModuleDef_HEAD_INIT,
"dynamic_input",
"Python module for dynamic command line input, written in C.",
-1,
dynamicInputMethods
};
/* module export */
PyMODINIT_FUNC PyInit_dynamic_input(void) {
return PyModule_Create(&dynamicInputModule);
}