#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if HAVE_SYS_TIME_H
# include <sys/time.h>
#endif
#if HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#if HAVE_UNISTD_H
# include <unistd.h>
#endif
#if HAVE_SYS_UNISTD_H
# include <sys/unistd.h>
#endif
#if HAVE_SYS_SELECT_H
# include <sys/select.h>
#endif
#if HAVE_ERRNO_H
# include <errno.h>
#endif
#if HAVE_TERMIOS_H
# include <termios.h>
#endif
#if HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
#endif
#include <sixel.h>
#if HAVE_TERMIOS_H && HAVE_SYS_IOCTL_H && HAVE_ISATTY
SIXELSTATUS
sixel_tty_cbreak(struct termios *old_termios, struct termios *new_termios)
{
SIXELSTATUS status = SIXEL_FALSE;
int ret;
ret = tcgetattr(STDIN_FILENO, old_termios);
if (ret != 0) {
status = (SIXEL_LIBC_ERROR | (errno & 0xff));
sixel_helper_set_additional_message(
"sixel_tty_cbreak: tcgetattr() failed.");
goto end;
}
(void) memcpy(new_termios, old_termios, sizeof(*old_termios));
new_termios->c_lflag &= (tcflag_t)~(ECHO | ICANON);
new_termios->c_cc[VMIN] = 1;
new_termios->c_cc[VTIME] = 0;
ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, new_termios);
if (ret != 0) {
status = (SIXEL_LIBC_ERROR | (errno & 0xff));
sixel_helper_set_additional_message(
"sixel_tty_cbreak: tcsetattr() failed.");
goto end;
}
status = SIXEL_OK;
end:
return status;
}
#endif
#if HAVE_TERMIOS_H && HAVE_SYS_IOCTL_H && HAVE_ISATTY
SIXELSTATUS
sixel_tty_restore(struct termios *old_termios)
{
SIXELSTATUS status = SIXEL_FALSE;
int ret;
ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, old_termios);
if (ret != 0) {
status = (SIXEL_LIBC_ERROR | (errno & 0xff));
sixel_helper_set_additional_message(
"sixel_tty_restore: tcsetattr() failed.");
goto end;
}
status = SIXEL_OK;
end:
return status;
}
#endif
SIXELSTATUS
sixel_tty_wait_stdin(int usec)
{
#if HAVE_SYS_SELECT_H
fd_set rfds;
struct timeval tv;
int ret = 0;
#endif
SIXELSTATUS status = SIXEL_FALSE;
#if HAVE_SYS_SELECT_H
tv.tv_sec = usec / 1000000;
tv.tv_usec = usec % 1000000;
FD_ZERO(&rfds);
FD_SET(STDIN_FILENO, &rfds);
ret = select(STDIN_FILENO + 1, &rfds, NULL, NULL, &tv);
if (ret < 0) {
status = (SIXEL_LIBC_ERROR | (errno & 0xff));
sixel_helper_set_additional_message(
"sixel_tty_wait_stdin: select() failed.");
goto end;
}
status = SIXEL_OK;
#else
(void) usec;
goto end;
#endif
end:
return status;
}
SIXELSTATUS
sixel_tty_scroll(
sixel_write_function f_write,
int outfd,
int height,
int is_animation)
{
SIXELSTATUS status = SIXEL_FALSE;
int nwrite;
#if HAVE_TERMIOS_H && HAVE_SYS_IOCTL_H && HAVE_ISATTY
struct winsize size = {0, 0, 0, 0};
struct termios old_termios;
struct termios new_termios;
int row = 0;
int col = 0;
int cellheight;
int scroll;
char buffer[256];
int result;
if (!isatty(STDIN_FILENO) || !isatty(outfd)) {
nwrite = f_write("\033[H", 3, &outfd);
if (nwrite < 0) {
status = (SIXEL_LIBC_ERROR | (errno & 0xff));
sixel_helper_set_additional_message(
"sixel_tty_scroll: f_write() failed.");
goto end;
}
status = SIXEL_OK;
goto end;
}
result = ioctl(outfd, TIOCGWINSZ, &size);
if (result != 0) {
status = (SIXEL_LIBC_ERROR | (errno & 0xff));
sixel_helper_set_additional_message("ioctl() failed.");
goto end;
}
if (size.ws_ypixel <= 0) {
nwrite = f_write("\033[H", 3, &outfd);
if (nwrite < 0) {
status = (SIXEL_LIBC_ERROR | (errno & 0xff));
sixel_helper_set_additional_message(
"sixel_tty_scroll: f_write() failed.");
goto end;
}
status = SIXEL_OK;
goto end;
}
if (is_animation) {
nwrite = f_write("\0338", 2, &outfd);
if (nwrite < 0) {
status = (SIXEL_LIBC_ERROR | (errno & 0xff));
sixel_helper_set_additional_message(
"sixel_tty_scroll: f_write() failed.");
goto end;
}
status = SIXEL_OK;
goto end;
}
status = sixel_tty_cbreak(&old_termios, &new_termios);
if (SIXEL_FAILED(status)) {
goto end;
}
nwrite = f_write("\033[6n", 4, &outfd);
if (nwrite < 0) {
status = (SIXEL_LIBC_ERROR | (errno & 0xff));
sixel_helper_set_additional_message(
"sixel_tty_scroll: f_write() failed.");
goto end;
}
if (SIXEL_FAILED(sixel_tty_wait_stdin(1000 * 1000))) {
nwrite = f_write("\033[H", 3, &outfd);
if (nwrite < 0) {
status = (SIXEL_LIBC_ERROR | (errno & 0xff));
sixel_helper_set_additional_message(
"sixel_tty_scroll: f_write() failed.");
goto end;
}
status = SIXEL_OK;
goto end;
}
if (scanf("\033[%d;%dR", &row, &col) != 2) {
nwrite = f_write("\033[H", 3, &outfd);
if (nwrite < 0) {
status = (SIXEL_LIBC_ERROR | (errno & 0xff));
sixel_helper_set_additional_message(
"sixel_tty_scroll: f_write() failed.");
goto end;
}
status = SIXEL_OK;
goto end;
}
status = sixel_tty_restore(&old_termios);
if (SIXEL_FAILED(status)) {
goto end;
}
cellheight = height * size.ws_row / size.ws_ypixel + 1;
scroll = cellheight + row - size.ws_row + 1;
if (scroll > 0) {
nwrite = sprintf(buffer, "\033[%dS\033[%dA", scroll, scroll);
if (nwrite < 0) {
status = (SIXEL_LIBC_ERROR | (errno & 0xff));
sixel_helper_set_additional_message(
"sixel_tty_scroll: sprintf() failed.");
}
nwrite = f_write(buffer, (int)strlen(buffer), &outfd);
if (nwrite < 0) {
status = (SIXEL_LIBC_ERROR | (errno & 0xff));
sixel_helper_set_additional_message(
"sixel_tty_scroll: f_write() failed.");
goto end;
}
}
nwrite = f_write("\0337", 2, &outfd);
if (nwrite < 0) {
status = (SIXEL_LIBC_ERROR | (errno & 0xff));
sixel_helper_set_additional_message(
"sixel_tty_scroll: f_write() failed.");
goto end;
}
#else
(void) height;
(void) is_animation;
nwrite = f_write("\033[H", 3, &outfd);
if (nwrite < 0) {
status = (SIXEL_LIBC_ERROR | (errno & 0xff));
sixel_helper_set_additional_message(
"sixel_tty_scroll: f_write() failed.");
goto end;
}
#endif
status = SIXEL_OK;
end:
return status;
}