The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 28 | |
| 29 | #pragma once |
| 30 | |
| 31 | /** |
| 32 | * @file termios.h |
| 33 | * @brief General terminal interfaces. |
| 34 | */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 35 | |
| 36 | #include <sys/cdefs.h> |
| 37 | #include <sys/ioctl.h> |
| 38 | #include <sys/types.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 39 | #include <linux/termios.h> |
| 40 | |
| 41 | __BEGIN_DECLS |
| 42 | |
Elliott Hughes | f39b085 | 2020-01-09 16:28:22 -0800 | [diff] [blame] | 43 | #if __ANDROID_API__ >= 28 |
| 44 | // This file is implemented as static inlines before API level 28. |
| 45 | // Strictly these functions were introduced in API level 21, but there were bugs |
| 46 | // in cfmakeraw() and cfsetspeed() until 28. |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 47 | |
| 48 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame^] | 49 | * [cfgetispeed(3)](https://man7.org/linux/man-pages/man3/cfgetispeed.3.html) |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 50 | * returns the terminal input baud rate. |
| 51 | */ |
Elliott Hughes | 655e430 | 2023-06-16 12:39:33 -0700 | [diff] [blame] | 52 | speed_t cfgetispeed(const struct termios* _Nonnull __t); |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame^] | 55 | * [cfgetospeed(3)](https://man7.org/linux/man-pages/man3/cfgetospeed.3.html) |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 56 | * returns the terminal output baud rate. |
| 57 | */ |
Elliott Hughes | 655e430 | 2023-06-16 12:39:33 -0700 | [diff] [blame] | 58 | speed_t cfgetospeed(const struct termios* _Nonnull __t); |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 59 | |
| 60 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame^] | 61 | * [cfmakeraw(3)](https://man7.org/linux/man-pages/man3/cfmakeraw.3.html) |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 62 | * configures the terminal for "raw" mode. |
| 63 | */ |
Elliott Hughes | 655e430 | 2023-06-16 12:39:33 -0700 | [diff] [blame] | 64 | void cfmakeraw(struct termios* _Nonnull __t); |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 65 | |
| 66 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame^] | 67 | * [cfsetspeed(3)](https://man7.org/linux/man-pages/man3/cfsetspeed.3.html) |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 68 | * sets the terminal input and output baud rate. |
| 69 | * |
| 70 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
| 71 | */ |
Elliott Hughes | 655e430 | 2023-06-16 12:39:33 -0700 | [diff] [blame] | 72 | int cfsetspeed(struct termios* _Nonnull __t, speed_t __speed); |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame^] | 75 | * [cfsetispeed(3)](https://man7.org/linux/man-pages/man3/cfsetispeed.3.html) |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 76 | * sets the terminal input baud rate. |
| 77 | * |
| 78 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
| 79 | */ |
Elliott Hughes | 655e430 | 2023-06-16 12:39:33 -0700 | [diff] [blame] | 80 | int cfsetispeed(struct termios* _Nonnull _t, speed_t __speed); |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 81 | |
| 82 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame^] | 83 | * [cfsetospeed(3)](https://man7.org/linux/man-pages/man3/cfsetospeed.3.html) |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 84 | * sets the terminal output baud rate. |
| 85 | * |
| 86 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
| 87 | */ |
Elliott Hughes | 655e430 | 2023-06-16 12:39:33 -0700 | [diff] [blame] | 88 | int cfsetospeed(struct termios* _Nonnull __t, speed_t __speed); |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 89 | |
| 90 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame^] | 91 | * [tcdrain(3)](https://man7.org/linux/man-pages/man3/tcdrain.3.html) |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 92 | * waits until all output has been written. |
| 93 | * |
| 94 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
| 95 | */ |
Elliott Hughes | 655e430 | 2023-06-16 12:39:33 -0700 | [diff] [blame] | 96 | int tcdrain(int __fd); |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 97 | |
| 98 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame^] | 99 | * [tcflow(3)](https://man7.org/linux/man-pages/man3/tcflow.3.html) |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 100 | * suspends (`TCOOFF`) or resumes (`TCOON`) output, or transmits a |
| 101 | * stop (`TCIOFF`) or start (`TCION`) to suspend or resume input. |
| 102 | * |
| 103 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
| 104 | */ |
Elliott Hughes | 655e430 | 2023-06-16 12:39:33 -0700 | [diff] [blame] | 105 | int tcflow(int __fd, int __action); |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 106 | |
| 107 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame^] | 108 | * [tcflush(3)](https://man7.org/linux/man-pages/man3/tcflush.3.html) |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 109 | * discards pending input (`TCIFLUSH`), output (`TCOFLUSH`), or |
| 110 | * both (`TCIOFLUSH`). (In `<stdio.h>` terminology, this is a purge rather |
| 111 | * than a flush.) |
| 112 | * |
| 113 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
| 114 | */ |
Elliott Hughes | 655e430 | 2023-06-16 12:39:33 -0700 | [diff] [blame] | 115 | int tcflush(int __fd, int __queue); |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 116 | |
| 117 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame^] | 118 | * [tcgetattr(3)](https://man7.org/linux/man-pages/man3/tcgetattr.3.html) |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 119 | * reads the configuration of the given terminal. |
| 120 | * |
| 121 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
| 122 | */ |
Elliott Hughes | 655e430 | 2023-06-16 12:39:33 -0700 | [diff] [blame] | 123 | int tcgetattr(int __fd, struct termios* _Nonnull __t); |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 124 | |
| 125 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame^] | 126 | * [tcgetsid(3)](https://man7.org/linux/man-pages/man3/tcgetsid.3.html) |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 127 | * returns the session id corresponding to the given fd. |
| 128 | * |
| 129 | * Returns a non-negative session id on success and |
| 130 | * returns -1 and sets `errno` on failure. |
| 131 | */ |
Elliott Hughes | 655e430 | 2023-06-16 12:39:33 -0700 | [diff] [blame] | 132 | pid_t tcgetsid(int __fd); |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 133 | |
| 134 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame^] | 135 | * [tcsendbreak(3)](https://man7.org/linux/man-pages/man3/tcsendbreak.3.html) |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 136 | * sends a break. |
| 137 | * |
| 138 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
| 139 | */ |
Elliott Hughes | 655e430 | 2023-06-16 12:39:33 -0700 | [diff] [blame] | 140 | int tcsendbreak(int __fd, int __duration); |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 141 | |
| 142 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame^] | 143 | * [tcsetattr(3)](https://man7.org/linux/man-pages/man3/tcsetattr.3.html) |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 144 | * writes the configuration of the given terminal. |
| 145 | * |
| 146 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
| 147 | */ |
Elliott Hughes | 655e430 | 2023-06-16 12:39:33 -0700 | [diff] [blame] | 148 | int tcsetattr(int __fd, int __optional_actions, const struct termios* _Nonnull __t); |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 149 | |
Josh Gao | 8778d64 | 2016-07-22 15:26:36 -0700 | [diff] [blame] | 150 | #endif |
| 151 | |
Elliott Hughes | 647472d | 2023-08-28 16:55:07 -0700 | [diff] [blame] | 152 | #if __ANDROID_API__ >= 35 |
| 153 | // These two functions were POSIX Issue 8 additions that we can also trivially |
| 154 | // implement as inlines for older OS version. |
| 155 | |
| 156 | /** |
| 157 | * tcgetwinsize(3) gets the window size of the given terminal. |
| 158 | * |
| 159 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
| 160 | */ |
| 161 | int tcgetwinsize(int __fd, struct winsize* _Nonnull __size); |
| 162 | |
| 163 | /** |
| 164 | * tcsetwinsize(3) sets the window size of the given terminal. |
| 165 | * |
| 166 | * Returns 0 on success and returns -1 and sets `errno` on failure. |
| 167 | */ |
| 168 | int tcsetwinsize(int __fd, const struct winsize* _Nonnull __size); |
| 169 | #endif |
| 170 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 171 | __END_DECLS |
| 172 | |
Josh Gao | b8e1b705 | 2016-04-13 17:18:20 -0700 | [diff] [blame] | 173 | #include <android/legacy_termios_inlines.h> |