diff --git a/meson.build b/meson.build index 1ed5d9a..b5bbca6 100644 --- a/meson.build +++ b/meson.build @@ -11,8 +11,15 @@ version_date = '2022-02-15' # Test for dynamic baudrate configuration interface compiler = meson.get_compiler('c') enable_setspeed2 = false -if compiler.check_header('asm-generic/ioctls.h') - enable_setspeed2 = compiler.has_header_symbol('asm-generic/ioctls.h', 'TCGETS2') +enable_iossiospeed = false +if host_machine.system() != 'darwin' + if compiler.check_header('asm-generic/ioctls.h') + enable_setspeed2 = compiler.has_header_symbol('asm-generic/ioctls.h', 'TCGETS2') + endif +else + if compiler.check_header('IOKit/serial/ioss.h') + enable_iossiospeed = compiler.has_header_symbol('IOKit/serial/ioss.h', 'IOSSIOSPEED') + endif endif # Test for supported baudrates diff --git a/src/iossiospeed.c b/src/iossiospeed.c new file mode 100644 index 0000000..3b0c62f --- /dev/null +++ b/src/iossiospeed.c @@ -0,0 +1,28 @@ +/* + * tio - a simple TTY terminal I/O application + * + * Copyright (c) 2017 Martin Lund + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#include +#include + +int iossiospeed(int fd, int baudrate) +{ + return ioctl(fd, IOSSIOSPEED, (char *)&baudrate); +} diff --git a/src/meson.build b/src/meson.build index a1d5502..6aed1fc 100644 --- a/src/meson.build +++ b/src/meson.build @@ -20,6 +20,11 @@ if enable_setspeed2 tio_c_args += '-DHAVE_TERMIOS2' endif +if enable_iossiospeed + tio_sources += 'iossiospeed.c' + tio_c_args += '-DHAVE_IOSSIOSPEED' +endif + executable('tio', tio_sources, c_args: tio_c_args, diff --git a/src/tty.c b/src/tty.c index 64098b4..5cec626 100644 --- a/src/tty.c +++ b/src/tty.c @@ -49,6 +49,10 @@ extern int setspeed2(int fd, int baudrate); #endif +#ifdef HAVE_IOSSIOSPEED +extern int iossiospeed(int fd, int baudrate); +#endif + #ifdef __APPLE__ #define PATH_SERIAL_DEVICES "/dev/" #else @@ -347,7 +351,7 @@ void tty_configure(void) BAUDRATE_CASES default: -#ifdef HAVE_TERMIOS2 +#if defined (HAVE_TERMIOS2) || defined (HAVE_IOSSIOSPEED) standard_baudrate = false; break; #else @@ -647,6 +651,15 @@ int tty_connect(void) if (tcgetattr(fd, &tio_old) < 0) goto error_tcgetattr; +#ifdef HAVE_IOSSIOSPEED + if (!standard_baudrate) + { + /* OS X wants these fields left alone. We'll set baudrate with iossiospeed below. */ + tio.c_ispeed = tio_old.c_ispeed; + tio.c_ospeed = tio_old.c_ospeed; + } +#endif + /* Make sure we restore tty settings on exit */ if (first) { @@ -668,7 +681,18 @@ int tty_connect(void) if (setspeed2(fd, option.baudrate) != 0) { error_printf_silent("Could not set baudrate speed (%s)", strerror(errno)); - goto error_setspeed2; + goto error_setspeed; + } + } +#endif + +#ifdef HAVE_IOSSIOSPEED + if (!standard_baudrate) + { + if (iossiospeed(fd, option.baudrate) != 0) + { + error_printf_silent("Could not set baudrate speed (%s)", strerror(errno)); + goto error_setspeed; } } #endif @@ -815,8 +839,8 @@ int tty_connect(void) return TIO_SUCCESS; -#ifdef HAVE_TERMIOS2 -error_setspeed2: +#if defined (HAVE_TERMIOS2) || defined (HAVE_IOSSIOSPEED) +error_setspeed: #endif error_tcsetattr: error_tcgetattr: