mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-15 13:04:38 +02:00
Allow specifying the optional actions for tcsetattr
This commit is contained in:
@@ -91,7 +91,7 @@ stop_profiler(PyObject UNUSED *self, PyObject *args UNUSED) {
|
||||
#endif
|
||||
|
||||
static inline bool
|
||||
put_tty_in_raw_mode(int fd, const struct termios* termios_p, bool read_with_timeout) {
|
||||
put_tty_in_raw_mode(int fd, const struct termios* termios_p, bool read_with_timeout, int optional_actions) {
|
||||
struct termios raw_termios = *termios_p;
|
||||
cfmakeraw(&raw_termios);
|
||||
if (read_with_timeout) {
|
||||
@@ -99,7 +99,7 @@ put_tty_in_raw_mode(int fd, const struct termios* termios_p, bool read_with_time
|
||||
} else {
|
||||
raw_termios.c_cc[VMIN] = 1; raw_termios.c_cc[VTIME] = 0;
|
||||
}
|
||||
if (tcsetattr(fd, TCSAFLUSH, &raw_termios) != 0) { PyErr_SetFromErrno(PyExc_OSError); return false; }
|
||||
if (tcsetattr(fd, optional_actions, &raw_termios) != 0) { PyErr_SetFromErrno(PyExc_OSError); return false; }
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -115,26 +115,26 @@ open_tty(PyObject *self UNUSED, PyObject *args) {
|
||||
struct termios *termios_p = calloc(1, sizeof(struct termios));
|
||||
if (!termios_p) return PyErr_NoMemory();
|
||||
if (tcgetattr(fd, termios_p) != 0) { free(termios_p); PyErr_SetFromErrno(PyExc_OSError); return NULL; }
|
||||
if (!put_tty_in_raw_mode(fd, termios_p, read_with_timeout != 0)) { free(termios_p); return NULL; }
|
||||
if (!put_tty_in_raw_mode(fd, termios_p, read_with_timeout != 0, TCSAFLUSH)) { free(termios_p); return NULL; }
|
||||
return Py_BuildValue("iN", fd, PyLong_FromVoidPtr(termios_p));
|
||||
}
|
||||
|
||||
#define TTY_ARGS \
|
||||
PyObject *tp; int fd; \
|
||||
if (!PyArg_ParseTuple(args, "iO!", &fd, &PyLong_Type, &tp)) return NULL; \
|
||||
PyObject *tp; int fd; int optional_actions = TCSAFLUSH; \
|
||||
if (!PyArg_ParseTuple(args, "iO!|", &fd, &PyLong_Type, &tp, &optional_actions)) return NULL; \
|
||||
struct termios *termios_p = PyLong_AsVoidPtr(tp);
|
||||
|
||||
static PyObject*
|
||||
normal_tty(PyObject *self UNUSED, PyObject *args) {
|
||||
TTY_ARGS
|
||||
if (tcsetattr(fd, TCSAFLUSH, termios_p) != 0) { PyErr_SetFromErrno(PyExc_OSError); return NULL; }
|
||||
if (tcsetattr(fd, optional_actions, termios_p) != 0) { PyErr_SetFromErrno(PyExc_OSError); return NULL; }
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyObject*
|
||||
raw_tty(PyObject *self UNUSED, PyObject *args) {
|
||||
TTY_ARGS
|
||||
if (!put_tty_in_raw_mode(fd, termios_p, false)) return NULL;
|
||||
if (!put_tty_in_raw_mode(fd, termios_p, false, optional_actions)) return NULL;
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
@@ -142,7 +142,7 @@ raw_tty(PyObject *self UNUSED, PyObject *args) {
|
||||
static PyObject*
|
||||
close_tty(PyObject *self UNUSED, PyObject *args) {
|
||||
TTY_ARGS
|
||||
tcsetattr(fd, TCSAFLUSH, termios_p); // deliberately ignore failure
|
||||
tcsetattr(fd, optional_actions, termios_p); // deliberately ignore failure
|
||||
free(termios_p);
|
||||
safe_close(fd, __FILE__, __LINE__);
|
||||
Py_RETURN_NONE;
|
||||
|
||||
Reference in New Issue
Block a user