From a3bbad0060cf511b93381a25d88d901d90d4903c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 6 Oct 2020 16:30:23 +0530 Subject: [PATCH] Allow specifying the optional actions for tcsetattr --- kitty/data-types.c | 16 ++++++++-------- kitty/fast_data_types.pyi | 7 ++++--- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/kitty/data-types.c b/kitty/data-types.c index 2ff656c1e..9eec5b447 100644 --- a/kitty/data-types.c +++ b/kitty/data-types.c @@ -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; diff --git a/kitty/fast_data_types.pyi b/kitty/fast_data_types.pyi index 8e400aa27..2bca31ce7 100644 --- a/kitty/fast_data_types.pyi +++ b/kitty/fast_data_types.pyi @@ -4,6 +4,7 @@ from typing import ( Union ) +import termios from kitty.boss import Boss from kitty.fonts.render import FontObject from kitty.options_stub import Options @@ -865,15 +866,15 @@ def viewport_for_window( TermiosPtr = NewType('TermiosPtr', int) -def raw_tty(fd: int, termios_ptr: TermiosPtr) -> None: +def raw_tty(fd: int, termios_ptr: TermiosPtr, optional_actions: int = termios.TCSAFLUSH) -> None: pass -def close_tty(fd: int, termios_ptr: TermiosPtr) -> None: +def close_tty(fd: int, termios_ptr: TermiosPtr, optional_actions: int = termios.TCSAFLUSH) -> None: pass -def normal_tty(fd: int, termios_ptr: TermiosPtr) -> None: +def normal_tty(fd: int, termios_ptr: TermiosPtr, optional_actions: int = termios.TCSAFLUSH) -> None: pass