yaft: use c++ YaFT_p implementation in yaft_class

This commit is contained in:
Stefan Seyfried
2018-01-24 22:47:10 +01:00
committed by Thilo Graf
parent 145b03eab6
commit d2f6f7a359
2 changed files with 74 additions and 75 deletions

View File

@@ -21,37 +21,34 @@
* The above copyright notice and this permission notice shall be included * The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software. * in all copies or substantial portions of the Software.
*/ */
#ifdef DEBUG
/* maybe on command line? */ #include <errno.h>
#undef DEBUG #include <signal.h>
#endif #include <sys/types.h>
static int CELL_WIDTH, CELL_HEIGHT; #include <sys/wait.h>
#include "yaft.h" #include <pty.h>
#include "conf.h" #include <fcntl.h>
#include "util.h" #include <unistd.h>
#include "fb/common.h" #include <string.h>
#include "terminal.h"
#include "ctrlseq/esc.h"
#include "ctrlseq/csi.h"
#include "ctrlseq/osc.h"
#include "parse.h"
#include "yaft_class.h" #include "yaft_class.h"
#include "yaft_priv.h"
#include <driver/framebuffer.h> #include <driver/framebuffer.h>
#ifdef DEBUG #include <driver/abstime.h>
#warning DEBUG redefined!
#undef DEBUG const char *term_name = "linux";
#endif
/* this will not fly with more than one instance */ /* this will not fly with more than one instance */
static pid_t childpid; static pid_t childpid;
static int exitcode; static int exitcode;
static volatile sig_atomic_t child_alive;
static void sig_handler(int signo) static void sig_handler(int signo)
{ {
/* global */ /* global */
extern volatile sig_atomic_t child_alive; extern volatile sig_atomic_t child_alive;
logging(DEBUG, "caught signal! no:%d\n", signo); logging(INFO, "caught signal! no:%d\n", signo);
if (signo != SIGCHLD) if (signo != SIGCHLD)
return; return;
@@ -60,7 +57,7 @@ static void sig_handler(int signo)
int ret = waitpid(childpid, &wstatus, 0); int ret = waitpid(childpid, &wstatus, 0);
if (ret < 0) { if (ret < 0) {
int e = errno; int e = errno;
logging(ERROR, "terminal: wait for %d returned %m\n", childpid); logging(NORMAL, "wait for child %d returned %m\n", childpid);
if (e == ECHILD) if (e == ECHILD)
return; /* don_t reset child_alive */ return; /* don_t reset child_alive */
} }
@@ -68,7 +65,7 @@ static void sig_handler(int signo)
exitcode = WEXITSTATUS(wstatus); exitcode = WEXITSTATUS(wstatus);
else if (WIFSIGNALED(wstatus)) else if (WIFSIGNALED(wstatus))
exitcode = 128 + WTERMSIG(wstatus); /* simulate shell behaviour */ exitcode = 128 + WTERMSIG(wstatus); /* simulate shell behaviour */
logging(WARN, "terminal: %d exited with %d\n", childpid, exitcode); logging(NORMAL, "child %d exited with %d\n", childpid, exitcode);
child_alive = false; child_alive = false;
} }
@@ -80,13 +77,17 @@ static pid_t fork_and_exec(int *master, int lines, int cols)
ws.ws_col = cols; ws.ws_col = cols;
/* XXX: this variables are UNUSED (man tty_ioctl), /* XXX: this variables are UNUSED (man tty_ioctl),
but useful for calculating terminal cell size */ but useful for calculating terminal cell size */
#if 0
ws.ws_ypixel = CELL_HEIGHT * lines; ws.ws_ypixel = CELL_HEIGHT * lines;
ws.ws_xpixel = CELL_WIDTH * cols; ws.ws_xpixel = CELL_WIDTH * cols;
#endif
pid_t pid; pid_t pid;
pid = eforkpty(master, NULL, NULL, &ws); pid = forkpty(master, NULL, NULL, &ws);
if (pid == 0) { /* child */ if (pid == 0) { /* child */
esetenv("TERM", term_name, 1); for (int i = 3; i < getdtablesize(); i++)
close(i);
setenv("TERM", term_name, 1);
execvp(yaft_argv[0], (char * const *)yaft_argv); execvp(yaft_argv[0], (char * const *)yaft_argv);
/* never reach here */ /* never reach here */
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@@ -101,8 +102,8 @@ static int check_fds(fd_set *fds, struct timeval *tv, int input, int master)
FD_SET(input, fds); FD_SET(input, fds);
FD_SET(master, fds); FD_SET(master, fds);
tv->tv_sec = 0; tv->tv_sec = 0;
tv->tv_usec = SELECT_TIMEOUT; tv->tv_usec = 100000;
return eselect(master + 1, fds, NULL, NULL, tv); return select(master + 1, fds, NULL, NULL, tv);
} }
YaFT::YaFT(const char * const *argv, int *Res, bool Paint, sigc::signal<void, std::string*, int*, bool*>func) YaFT::YaFT(const char * const *argv, int *Res, bool Paint, sigc::signal<void, std::string*, int*, bool*>func)
@@ -123,56 +124,47 @@ int YaFT::run(void)
ssize_t size; ssize_t size;
fd_set fds; fd_set fds;
struct timeval tv; struct timeval tv;
struct framebuffer_t fb;
struct terminal_t term;
bool ok = true; bool ok = true;
/* global */ bool need_redraw = false;
extern volatile sig_atomic_t need_redraw;
extern volatile sig_atomic_t child_alive;
fb.info.cfb = CFrameBuffer::getInstance();
term.txt.push("");
term.lines_available = 0;
term.nlseen = false;
exitcode = 0; exitcode = 0;
YaFT_p *term = new YaFT_p(paint);
int flags;
/* init */ /* init */
if (setlocale(LC_ALL, "") == NULL) /* for wcwidth() */ if (setlocale(LC_ALL, "") == NULL) /* for wcwidth() */
logging(WARN, "setlocale falied\n"); logging(NORMAL, "setlocale falied\n");
if (!term->init())
if (!fb_init(&fb)) { goto init_failed;
logging(FATAL, "framebuffer initialize failed\n");
goto fb_init_failed;
}
if (!term_init(&term, fb.info.width, fb.info.height)) {
logging(FATAL, "terminal initialize failed\n");
goto term_init_failed;
}
struct sigaction sigact; struct sigaction sigact;
memset(&sigact, 0, sizeof(struct sigaction)); memset(&sigact, 0, sizeof(struct sigaction));
sigact.sa_handler = sig_handler; sigact.sa_handler = sig_handler;
sigact.sa_flags = SA_RESTART; sigact.sa_flags = SA_RESTART;
esigaction(SIGCHLD, &sigact, NULL); sigaction(SIGCHLD, &sigact, NULL);
/* fork and exec shell */ /* fork and exec shell */
if ((childpid = fork_and_exec(&term.fd, term.lines, term.cols)) < 0) { if ((childpid = fork_and_exec(&term->fd, term->lines, term->cols)) < 0) {
logging(FATAL, "forkpty failed\n"); logging(NORMAL, "forkpty failed\n");
goto tty_init_failed; goto init_failed;
} }
child_alive = true; child_alive = true;
flags = fcntl(term->fd, F_GETFL);
fcntl(term->fd, F_SETFL, flags | O_NONBLOCK);
# if 0
fprintf(stderr, "forked child %d, command: '", childpid);
const char * const *p = yaft_argv;
while (*p) {
fprintf(stderr, "%s ", *p);
p++;
}
fprintf(stderr, "'\n");
#endif
/* main loop */ /* main loop */
while (child_alive) { while (child_alive) {
if (need_redraw) { if (need_redraw) {
need_redraw = false; need_redraw = false;
if (paint) { term->refresh();
redraw(&term);
refresh(&fb, &term);
} }
} if (check_fds(&fds, &tv, STDIN_FILENO, term->fd) == -1)
if (check_fds(&fds, &tv, STDIN_FILENO, term.fd) == -1)
continue; continue;
#if 0 #if 0
if (FD_ISSET(STDIN_FILENO, &fds)) { if (FD_ISSET(STDIN_FILENO, &fds)) {
@@ -180,13 +172,11 @@ int YaFT::run(void)
ewrite(term.fd, buf, size); ewrite(term.fd, buf, size);
} }
#endif #endif
if (FD_ISSET(term.fd, &fds)) { if (FD_ISSET(term->fd, &fds)) {
while ((size = read(term.fd, buf, BUFSIZE)) > 0) { while ((size = read(term->fd, buf, BUFSIZE)) > 0) {
if (VERBOSE) term->parse(buf, size);
ewrite(STDOUT_FILENO, buf, size); while (term->lines_available > 0) {
parse(&term, buf, size); std::string s = term->txt.front();
while (term.lines_available > 0) {
std::string s = term.txt.front();
OnShellOutputLoop(&s, res, &ok); OnShellOutputLoop(&s, res, &ok);
#if 0 #if 0
if (res) if (res)
@@ -195,28 +185,36 @@ int YaFT::run(void)
printf("[CTermWindow] [%s:%d] res=NULL ok=%d\n", __func__, __LINE__, ok); printf("[CTermWindow] [%s:%d] res=NULL ok=%d\n", __func__, __LINE__, ok);
#endif #endif
// fprintf(stderr, "%d %s\n", term.lines_available, term.txt.front().c_str()); // fprintf(stderr, "%d %s\n", term.lines_available, term.txt.front().c_str());
term.txt.pop(); term->txt.pop();
term.lines_available--; term->lines_available--;
} }
if (! paint) if (! paint)
continue; continue;
if (LAZY_DRAW && size == BUFSIZE) need_redraw = true;
if (/*LAZY_DRAW && */size == BUFSIZE)
continue; /* maybe more data arrives soon */ continue; /* maybe more data arrives soon */
refresh(&fb, &term); time_t now = time_monotonic_ms();
if (now - term->last_paint < 100)
continue;
term->refresh();
need_redraw = false;
} }
} }
} }
if (paint) term->refresh();
refresh(&fb, &term); /* get the last partial line if there was no newline. The loop should only ever run once... */
while (!term->txt.empty()) {
std::string s = term->txt.front();
OnShellOutputLoop(&s, res, &ok);
term->txt.pop();
}
/* normal exit */ /* normal exit */
term_die(&term); delete term;
return exitcode; return exitcode;
/* error exit */ /* error exit */
tty_init_failed: init_failed:
term_die(&term); delete term;
term_init_failed:
fb_init_failed:
return EXIT_FAILURE; return EXIT_FAILURE;
} }

View File

@@ -9,6 +9,7 @@
*/ */
#ifndef __yaft_class__ #ifndef __yaft_class__
#define __yaft_class__ #define __yaft_class__
#include <string>
#include <sigc++/signal.h> #include <sigc++/signal.h>
class YaFT : public sigc::trackable class YaFT : public sigc::trackable