1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
/**
* @file stdio.c Standard Input/Output UI module
*
* Copyright (C) 2010 Alfred E. Heggestad
*/
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <re.h>
#include <baresip.h>
/**
* @defgroup stdio stdio
*
* User-Interface (UI) module for standard input/output
*
* This module sets up the terminal in raw mode, and reads characters from the
* input to the UI subsystem. The module is indented for Unix-based systems.
*/
/** Local constants */
enum {
RELEASE_VAL = 250 /**< Key release value in [ms] */
};
struct ui_st {
struct tmr tmr;
struct termios term;
bool term_set;
};
/* We only allow one instance */
static struct ui_st *ui_state;
static void ui_destructor(void *arg)
{
struct ui_st *st = arg;
fd_close(STDIN_FILENO);
if (st->term_set)
tcsetattr(STDIN_FILENO, TCSANOW, &st->term);
tmr_cancel(&st->tmr);
}
static int print_handler(const char *p, size_t size, void *arg)
{
(void)arg;
return 1 == fwrite(p, size, 1, stderr) ? 0 : ENOMEM;
}
static void report_key(struct ui_st *ui, char key)
{
static struct re_printf pf_stderr = {print_handler, NULL};
(void)ui;
ui_input_key(baresip_uis(), key, &pf_stderr);
}
static void timeout(void *arg)
{
struct ui_st *st = arg;
/* Emulate key-release */
report_key(st, KEYCODE_REL);
}
static void ui_fd_handler(int flags, void *arg)
{
struct ui_st *st = arg;
char key;
(void)flags;
if (1 != read(STDIN_FILENO, &key, 1)) {
return;
}
tmr_start(&st->tmr, RELEASE_VAL, timeout, st);
report_key(st, key);
}
static int term_setup(struct ui_st *st)
{
struct termios now;
if (tcgetattr(STDIN_FILENO, &st->term) < 0)
return errno;
now = st->term;
now.c_lflag |= ISIG;
now.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
/* required on Solaris */
now.c_cc[VMIN] = 1;
now.c_cc[VTIME] = 0;
if (tcsetattr(STDIN_FILENO, TCSANOW, &now) < 0)
return errno;
st->term_set = true;
return 0;
}
static int ui_alloc(struct ui_st **stp)
{
struct ui_st *st;
int err;
if (!stp)
return EINVAL;
st = mem_zalloc(sizeof(*st), ui_destructor);
if (!st)
return ENOMEM;
tmr_init(&st->tmr);
err = fd_listen(STDIN_FILENO, FD_READ, ui_fd_handler, st);
if (err)
goto out;
err = term_setup(st);
if (err) {
info("stdio: could not setup terminal: %m\n", err);
err = 0;
}
out:
if (err)
mem_deref(st);
else
*stp = st;
return err;
}
static int output_handler(const char *str)
{
return print_handler(str, str_len(str), NULL);
}
static struct ui ui_stdio = {
.name = "stdio",
.outputh = output_handler
};
static int module_init(void)
{
int err;
err = ui_alloc(&ui_state);
if (err)
return err;
ui_register(baresip_uis(), &ui_stdio);
return 0;
}
static int module_close(void)
{
ui_unregister(&ui_stdio);
ui_state = mem_deref(ui_state);
return 0;
}
const struct mod_export DECL_EXPORTS(stdio) = {
"stdio",
"ui",
module_init,
module_close
};
|