mirror of
https://github.com/troglobit/libite.git
synced 2026-11-06 15:50:57 +00:00
conio.h: Move initscr() function to conio.c
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
1 parent
94800e7d5c
commit
bbb951dc23
3 files changed
+84
-59
No files matched your search
+1
-1
@@ -6,7 +6,7 @@ libite_la_LDFLAGS = $(AM_LDFLAGS) -version-info 4:1:0
|
|||||||
libite_la_SOURCES = chomp.c copyfile.c dir.c fexist.c fisdir.c fmode.c fparseln.c \
|
libite_la_SOURCES = chomp.c copyfile.c dir.c fexist.c fisdir.c fmode.c fparseln.c \
|
||||||
fsendfile.c ifconfig.c lfile.c makepath.c pidfile.c pidfilefn.c \
|
fsendfile.c ifconfig.c lfile.c makepath.c pidfile.c pidfilefn.c \
|
||||||
progress.c reallocarray.c rsync.c strlcpy.c strlcat.c strtonum.c \
|
progress.c reallocarray.c rsync.c strlcpy.c strlcat.c strtonum.c \
|
||||||
tempfile.c tree.c which.c
|
tempfile.c tree.c which.c conio.c
|
||||||
|
|
||||||
## Distribute these header files in the <lite/...> name space
|
## Distribute these header files in the <lite/...> name space
|
||||||
pkgincludedir = $(includedir)/lite
|
pkgincludedir = $(includedir)/lite
|
||||||
|
|||||||
+83
@@ -0,0 +1,83 @@
|
|||||||
|
/* A conio.h like implementation for VTANSI displays.
|
||||||
|
*
|
||||||
|
* Copyright (c) 2009-2017 Joachim Nilsson <troglobit@gmail.com>
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <poll.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <termios.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
void initscr(int *row, int *col)
|
||||||
|
{
|
||||||
|
if (!row || !col)
|
||||||
|
return;
|
||||||
|
|
||||||
|
#if defined(TCSANOW) && defined(POLLIN)
|
||||||
|
struct termios tc, saved;
|
||||||
|
struct pollfd fd = { STDIN_FILENO, POLLIN, 0 };
|
||||||
|
|
||||||
|
if (!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO)) {
|
||||||
|
*row = 24;
|
||||||
|
*col = 80;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Disable echo to terminal while probing */
|
||||||
|
tcgetattr(STDOUT_FILENO, &tc);
|
||||||
|
saved = tc;
|
||||||
|
tc.c_cflag |= (CLOCAL | CREAD);
|
||||||
|
tc.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
|
||||||
|
tcsetattr(STDOUT_FILENO, TCSANOW, &tc);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Save cursor pos+attr
|
||||||
|
* Diable top+bottom margins
|
||||||
|
* Set cursor at the far bottom,right pos
|
||||||
|
* Query term for resulting pos
|
||||||
|
*/
|
||||||
|
fprintf(stdout, "\e7" "\e[r" "\e[999;999H" "\e[6n");
|
||||||
|
fflush(stdout);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Wait here for terminal to echo back \e[row,lineR ...
|
||||||
|
*/
|
||||||
|
if (poll(&fd, 1, 300) <= 0 || scanf("\e[%d;%dR", row, col) != 2) {
|
||||||
|
*row = 24;
|
||||||
|
*col = 80;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Restore above saved cursor pos+attr
|
||||||
|
*/
|
||||||
|
fprintf(stdout, "\e8");
|
||||||
|
fflush(stdout);
|
||||||
|
|
||||||
|
/* Restore terminal */
|
||||||
|
tcsetattr(STDOUT_FILENO, TCSANOW, &saved);
|
||||||
|
|
||||||
|
return;
|
||||||
|
#else
|
||||||
|
*row = 24;
|
||||||
|
*col = 80;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
-58
@@ -19,8 +19,6 @@
|
|||||||
#define CONIO_H_
|
#define CONIO_H_
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <termios.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
/* Attributes */
|
/* Attributes */
|
||||||
#define RESETATTR 0
|
#define RESETATTR 0
|
||||||
@@ -86,62 +84,6 @@ static inline void printheader(FILE *fp, const char *line, int nl)
|
|||||||
line, SCREEN_WIDTH - (int)strlen(line), "");
|
line, SCREEN_WIDTH - (int)strlen(line), "");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Requries: #inlude <termios.h> and #include <poll.h> */
|
|
||||||
static inline void initscr(int *row, int *col)
|
|
||||||
{
|
|
||||||
if (!row || !col)
|
|
||||||
return;
|
|
||||||
|
|
||||||
#if defined(TCSANOW) && defined(POLLIN)
|
|
||||||
struct termios tc, saved;
|
|
||||||
struct pollfd fd = { STDIN_FILENO, POLLIN, 0 };
|
|
||||||
|
|
||||||
if (!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO)) {
|
|
||||||
*row = 24;
|
|
||||||
*col = 80;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Disable echo to terminal while probing */
|
|
||||||
tcgetattr(STDOUT_FILENO, &tc);
|
|
||||||
saved = tc;
|
|
||||||
tc.c_cflag |= (CLOCAL | CREAD);
|
|
||||||
tc.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
|
|
||||||
tcsetattr(STDOUT_FILENO, TCSANOW, &tc);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Save cursor pos+attr
|
|
||||||
* Diable top+bottom margins
|
|
||||||
* Set cursor at the far bottom,right pos
|
|
||||||
* Query term for resulting pos
|
|
||||||
*/
|
|
||||||
fprintf(stdout, "\e7" "\e[r" "\e[999;999H" "\e[6n");
|
|
||||||
fflush(stdout);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Wait here for terminal to echo back \e[row,lineR ...
|
|
||||||
*/
|
|
||||||
if (poll(&fd, 1, 300) <= 0 || scanf("\e[%d;%dR", row, col) != 2) {
|
|
||||||
*row = 24;
|
|
||||||
*col = 80;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Restore above saved cursor pos+attr
|
|
||||||
*/
|
|
||||||
fprintf(stdout, "\e8");
|
|
||||||
fflush(stdout);
|
|
||||||
|
|
||||||
/* Restore terminal */
|
|
||||||
tcsetattr(STDOUT_FILENO, TCSANOW, &saved);
|
|
||||||
|
|
||||||
return;
|
|
||||||
#else
|
|
||||||
*row = 24;
|
|
||||||
*col = 80;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif /* CONIO_H_ */
|
#endif /* CONIO_H_ */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in new issue
Block a user