| 1 |
/* |
| 2 |
term_posix: POSIX-specifc terminal functionality |
| 3 |
|
| 4 |
HAVE_TERMIOS is a prerequisite. |
| 5 |
|
| 6 |
copyright 2008-2023 by the mpg123 project - free software under the terms of the LGPL 2.1 |
| 7 |
see COPYING and AUTHORS files in distribution or http://mpg123.org |
| 8 |
initially written by Thomas Orgis |
| 9 |
*/ |
| 10 |
|
| 11 |
// ctermid |
| 12 |
#define _XOPEN_SOURCE 600 |
| 13 |
#define _POSIX_C_SOURCE 200112L |
| 14 |
|
| 15 |
#include "config.h" |
| 16 |
|
| 17 |
#ifdef __OS2__ |
| 18 |
// Hoping for properly working termios in some future (?!), but until then, |
| 19 |
// we need keyboard access bypassing that. |
| 20 |
#define INCL_KBD |
| 21 |
#define INCL_DOSPROCESS |
| 22 |
#include <os2.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
#include "compat/compat.h" |
| 26 |
|
| 27 |
#ifndef HAVE_TERMIOS |
| 28 |
#error "No TERMIOS? Here?" |
| 29 |
#endif |
| 30 |
|
| 31 |
// for param struct |
| 32 |
#include "mpg123app.h" |
| 33 |
|
| 34 |
#include <termios.h> |
| 35 |
#include <sys/ioctl.h> |
| 36 |
|
| 37 |
#include "terms.h" |
| 38 |
|
| 39 |
#include "common/debug.h" |
| 40 |
|
| 41 |
static int term_is_fun = -1; |
| 42 |
// This now always refers to a ;freshly opened terminal descriptor (e.g. /dev/tty). |
| 43 |
// Printouts to stderr are independent of this. |
| 44 |
static int term_fd = -1; |
| 45 |
static struct termios old_tio; |
| 46 |
|
| 47 |
/* Buffered key from a signal or whatnot. |
| 48 |
We ignore the null character... */ |
| 49 |
static char prekey = 0; |
| 50 |
|
| 51 |
int term_have_fun(int fd, int want_visuals) |
| 52 |
{ |
| 53 |
if(term_is_fun > -1) |
| 54 |
return term_is_fun; |
| 55 |
else |
| 56 |
term_is_fun = 0; |
| 57 |
if(term_width(fd) > 0 && want_visuals) |
| 58 |
{ |
| 59 |
/* Only play with non-dumb terminals. */ |
| 60 |
char *tname = INT123_compat_getenv("TERM"); |
| 61 |
if(tname) |
| 62 |
{ |
| 63 |
if(strcmp(tname, "") && strcmp(tname, "dumb")) |
| 64 |
term_is_fun = 1; |
| 65 |
free(tname); |
| 66 |
} |
| 67 |
} |
| 68 |
return term_is_fun; |
| 69 |
} |
| 70 |
|
| 71 |
/* Also serves as a way to detect if we have an interactive terminal. */ |
| 72 |
int term_width(int fd) |
| 73 |
{ |
| 74 |
#ifdef __OS2__ |
| 75 |
int s[2]; |
| 76 |
_scrsize (s); |
| 77 |
// It seems like we cannot really use the last character of the |
| 78 |
// term and have to stop one short to avoid advancing a line. |
| 79 |
if (s[0] >= 0) |
| 80 |
return s[0] - 1; |
| 81 |
#else |
| 82 |
struct winsize geometry; |
| 83 |
geometry.ws_col = 0; |
| 84 |
if(ioctl(fd, TIOCGWINSZ, &geometry) >= 0) |
| 85 |
return (int)geometry.ws_col; |
| 86 |
#endif |
| 87 |
return -1; |
| 88 |
} |
| 89 |
|
| 90 |
static int term_setup_detail(struct termios *pattern); |
| 91 |
|
| 92 |
static void term_sigcont(int sig) |
| 93 |
{ |
| 94 |
if(term_setup_detail(&old_tio) < 0) |
| 95 |
{ |
| 96 |
debug("Can't set terminal attributes"); |
| 97 |
return; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
static void term_sigusr(int sig) |
| 102 |
{ |
| 103 |
switch(sig) |
| 104 |
{ |
| 105 |
case SIGUSR1: prekey=*param.term_usr1; break; |
| 106 |
case SIGUSR2: prekey=*param.term_usr2; break; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/* This must call only functions safe inside a signal handler. */ |
| 111 |
static int term_setup_detail(struct termios *pattern) |
| 112 |
{ |
| 113 |
mdebug("setup on fd %d", term_fd); |
| 114 |
|
| 115 |
INT123_catchsignal(SIGCONT, term_sigcont); |
| 116 |
INT123_catchsignal(SIGUSR1, term_sigusr); |
| 117 |
INT123_catchsignal(SIGUSR2, term_sigusr); |
| 118 |
|
| 119 |
struct termios tio = *pattern; |
| 120 |
tio.c_lflag &= ~(ICANON|ECHO); |
| 121 |
tio.c_cc[VMIN] = 1; |
| 122 |
tio.c_cc[VTIME] = 0; |
| 123 |
#ifdef __OS2__ |
| 124 |
// Do not care for the error until OS/2 is known to work. |
| 125 |
tcsetattr(term_fd,TCSANOW,&tio); |
| 126 |
return 0; |
| 127 |
#else |
| 128 |
return tcsetattr(term_fd,TCSANOW,&tio); |
| 129 |
#endif |
| 130 |
} |
| 131 |
|
| 132 |
int term_setup(void) |
| 133 |
{ |
| 134 |
if(term_fd < 0) |
| 135 |
{ |
| 136 |
const char *term_name; |
| 137 |
#ifdef HAVE_CTERMID |
| 138 |
term_name = ctermid(NULL); |
| 139 |
#else |
| 140 |
term_name = "/dev/tty"; |
| 141 |
#endif |
| 142 |
if(term_name) |
| 143 |
mdebug("accessing terminal for control via %s", term_name); |
| 144 |
else |
| 145 |
{ |
| 146 |
error("no controlling terminal"); |
| 147 |
return -1; |
| 148 |
} |
| 149 |
term_fd = open(term_name, O_RDONLY); |
| 150 |
if(term_fd < 0) |
| 151 |
{ |
| 152 |
merror("failed to open terminal: %s", INT123_strerror(errno)); |
| 153 |
return -1; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
if(tcgetattr(term_fd, &old_tio) < 0) |
| 158 |
{ |
| 159 |
// For now, this always fails on OS/2, but they might fix things. |
| 160 |
// So just try to move on. |
| 161 |
#ifndef __OS2__ |
| 162 |
merror("failed to get terminal attributes: %s", INT123_strerror(errno)); |
| 163 |
return -1; |
| 164 |
#endif |
| 165 |
} |
| 166 |
|
| 167 |
errno = 0; |
| 168 |
if(term_setup_detail(&old_tio) < 0) |
| 169 |
{ |
| 170 |
close(term_fd); |
| 171 |
term_fd = -1; |
| 172 |
if(errno) |
| 173 |
merror("failure setting terminal attributes: %s", INT123_strerror(errno)); |
| 174 |
else |
| 175 |
error("failure setting terminal attributes"); |
| 176 |
return -1; |
| 177 |
} |
| 178 |
return 0; |
| 179 |
} |
| 180 |
|
| 181 |
/* Get the next pressed key, if any. |
| 182 |
Returns 1 when there is a key, 0 if not. */ |
| 183 |
int term_get_key(int stopped, int do_delay, char *val) |
| 184 |
{ |
| 185 |
#ifdef __OS2__ |
| 186 |
KBDKEYINFO key; |
| 187 |
key.chChar = 0; |
| 188 |
key.chScan = 0; |
| 189 |
if(do_delay) |
| 190 |
DosSleep(10); |
| 191 |
if(!KbdCharIn(&key,(stopped) ? IO_WAIT : IO_NOWAIT,0) && key.chChar) |
| 192 |
{ |
| 193 |
*val = key.chChar; |
| 194 |
return 1; |
| 195 |
} |
| 196 |
#else |
| 197 |
fd_set r; |
| 198 |
struct timeval t; |
| 199 |
|
| 200 |
/* Shortcut: If some other means sent a key, use it. */ |
| 201 |
if(prekey) |
| 202 |
{ |
| 203 |
debug1("Got prekey: %c\n", prekey); |
| 204 |
*val = prekey; |
| 205 |
prekey = 0; |
| 206 |
return 1; |
| 207 |
} |
| 208 |
|
| 209 |
t.tv_sec=0; |
| 210 |
t.tv_usec = do_delay==1 ? 10*1000 : (do_delay==-1 ? 1*1000 : 0); |
| 211 |
|
| 212 |
FD_ZERO(&r); |
| 213 |
FD_SET(term_fd,&r); |
| 214 |
/* No timeout if stopped */ |
| 215 |
if(select(term_fd+1,&r,NULL,NULL,(stopped) ? NULL : &t) > 0 && FD_ISSET(term_fd,&r)) |
| 216 |
{ |
| 217 |
if(read(term_fd,val,1) <= 0) |
| 218 |
return 0; /* Well, we couldn't read the key, so there is none. */ |
| 219 |
else |
| 220 |
return 1; |
| 221 |
} |
| 222 |
#endif |
| 223 |
else return 0; |
| 224 |
} |
| 225 |
|
| 226 |
void term_restore(void) |
| 227 |
{ |
| 228 |
debug("reset attrbutes"); |
| 229 |
tcsetattr(term_fd,TCSAFLUSH,&old_tio); |
| 230 |
|
| 231 |
if(term_fd > -1) |
| 232 |
close(term_fd); |
| 233 |
term_fd = -1; |
| 234 |
} |