subterm.c revision 553acb7b6b8d4f16a4747b1f978e8b7888fbfb2c
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright (C) 2014 David Herrmann <dh.herrmann@gmail.com>
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
/*
* Stacked Terminal-Emulator
* This is an interactive test of the term_screen implementation. It runs a
* fully-fletched terminal-emulator inside of a parent TTY. That is, instead of
* rendering the terminal as X11-window, it renders it as sub-window in the
* parent TTY. Think of this like what "GNU-screen" does.
*/
#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include "macro.h"
#include "pty.h"
#include "ring.h"
#include "sd-event.h"
#include "term-internal.h"
#include "util.h"
struct Output {
int fd;
unsigned int width;
unsigned int height;
unsigned int in_width;
unsigned int in_height;
unsigned int cursor_x;
unsigned int cursor_y;
char obuf[4096];
bool resized : 1;
bool in_menu : 1;
};
struct Terminal {
int in_fd;
int out_fd;
struct termios saved_in_attr;
struct termios saved_out_attr;
bool is_scheduled : 1;
bool is_dirty : 1;
bool is_menu : 1;
};
/*
* Output Handling
*/
#define BORDER_HORIZ "\xe2\x94\x81"
#define BORDER_VERT "\xe2\x94\x83"
#define BORDER_VERT_RIGHT "\xe2\x94\xa3"
#define BORDER_VERT_LEFT "\xe2\x94\xab"
#define BORDER_DOWN_RIGHT "\xe2\x94\x8f"
#define BORDER_DOWN_LEFT "\xe2\x94\x93"
#define BORDER_UP_RIGHT "\xe2\x94\x97"
#define BORDER_UP_LEFT "\xe2\x94\x9b"
static int output_winch(Output *o) {
int r;
assert_return(o, -EINVAL);
if (r < 0)
o->resized = true;
}
return 0;
}
static int output_flush(Output *o) {
int r;
if (o->n_obuf < 1)
return 0;
if (r < 0)
return log_error_errno(r, "error: cannot write to TTY: %m");
o->n_obuf = 0;
return 0;
}
int r;
assert_return(o, -EINVAL);
if (size < 1)
return 0;
return 0;
}
r = output_flush(o);
if (r < 0)
return r;
if (len < 0)
return 0;
}
_printf_(3,0)
char buf[4096];
int r;
assert_return(o, -EINVAL);
r = max;
return output_write(o, buf, r);
}
int r;
return r;
}
_printf_(2,0)
char buf[4096];
int r;
assert_return(o, -EINVAL);
return output_write(o, buf, r);
}
int r;
return r;
}
static int output_move_to(Output *o, unsigned int x, unsigned int y) {
int r;
assert_return(o, -EINVAL);
/* force the \e[H code as o->cursor_x/y might be out-of-date */
if (r < 0)
return r;
o->cursor_x = x;
o->cursor_y = y;
return 0;
}
const char line[] =
BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ
BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ
BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ
BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ
BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ
BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ BORDER_HORIZ
size_t i;
int r = 0;
assert_return(o, -EINVAL);
if (r < 0)
break;
}
return r;
}
int r;
assert(o);
/* out of frame? */
return 0;
if (r < 0)
return r;
}
if (!o)
return NULL;
/* re-enable cursor */
output_printf(o, "\e[?25h");
/* disable alternate screen buffer */
output_printf(o, "\e[?1049l");
output_flush(o);
/* o->fd is owned by the caller */
free(o);
return NULL;
}
Output *o;
int r;
if (!o)
return log_oom();
r = output_winch(o);
if (r < 0)
goto error;
/* enable alternate screen buffer */
r = output_printf(o, "\e[?1049h");
if (r < 0)
goto error;
/* always hide cursor */
r = output_printf(o, "\e[?25l");
if (r < 0)
goto error;
r = output_flush(o);
if (r < 0)
goto error;
*out = o;
return 0;
output_free(o);
return r;
}
static void output_draw_frame(Output *o) {
unsigned int i;
assert(o);
/* print header-frame */
"\r\n"
"\e[2;%uH" /* cursor-position: 2/x */
"\r\n"
o->width);
"\r\n");
/* print body-frame */
for (i = 0; i < o->in_height; ++i) {
"\e[%u;%uH" /* cursor-position: 2/x */
"\r\n",
i + 4, o->width);
}
/* print footer-frame */
"\r\n"
"\e[%u;%uH" /* cursor-position: 2/x */
"\r\n"
output_printf(o, "\e[2;3H");
}
static void output_draw_menu(Output *o) {
assert(o);
output_frame_printl(o, " Menu: (the following keys are recognized)");
output_frame_printl(o, " q: quit");
output_frame_printl(o, " ^C: send ^C to the PTY");
}
void *userdata,
unsigned int x,
unsigned int y,
unsigned int ch_width) {
char utf8[4];
return 0;
if (x == 0 && y != 0)
case TERM_CCODE_DEFAULT:
output_printf(o, "\e[39m");
break;
case TERM_CCODE_256:
break;
case TERM_CCODE_RGB:
break;
case TERM_CCODE_BLACK ... TERM_CCODE_WHITE:
break;
case TERM_CCODE_LIGHT_BLACK ... TERM_CCODE_LIGHT_WHITE:
break;
}
case TERM_CCODE_DEFAULT:
output_printf(o, "\e[49m");
break;
case TERM_CCODE_256:
break;
case TERM_CCODE_RGB:
break;
case TERM_CCODE_BLACK ... TERM_CCODE_WHITE:
break;
case TERM_CCODE_LIGHT_BLACK ... TERM_CCODE_LIGHT_WHITE:
break;
}
output_printf(o, "\e[%u;%u;%u;%u;%u;%um",
if (n_ch < 1) {
output_printf(o, " ");
} else {
for (k = 0; k < n_ch; ++k) {
}
}
return 0;
}
assert(o);
assert(s);
output_printf(o, "\e[m");
}
assert(o);
/*
* This renders the contenst of the terminal. The layout contains a
* header, the main body and a footer. Around all areas we draw a
* border. It looks something like this:
*
* +----------------------------------------------------+
* | Header |
* +----------------------------------------------------+
* | |
* | |
* | |
* | Body |
* | |
* | |
* ~ ~
* ~ ~
* +----------------------------------------------------+
* | Footer |
* +----------------------------------------------------+
*
* The body is the part that grows vertically.
*
* We need at least 6 vertical lines to render the screen. This would
* leave 0 lines for the body. Therefore, we require 7 lines so there's
* at least one body line. Similarly, we need 2 horizontal cells for the
* frame, so we require 3.
* If the window is too small, we print an error message instead.
*/
"\e[H"); /* cursor-position: home */
output_printf(o, "error: screen too small, need at least 3x7 cells");
output_flush(o);
return;
}
/* hide cursor */
output_printf(o, "\e[?25l");
/* frame-content is contant; only resizes can change it */
"\e[H"); /* cursor-position: home */
o->resized = false;
}
/* move cursor to child's position */
if (menu)
output_draw_menu(o);
else
output_draw_screen(o, screen);
/*
* Hack: sd-term was not written to support TTY as output-objects, thus
* expects callers to use term_screen_feed_keyboard(). However, we
* forward TTY input directly. Hence, we're not notified about keypad
* changes. Update the related modes djring redraw to keep them at least
* in sync.
*/
output_printf(o, "\e[?1h");
else
output_printf(o, "\e[?1l");
output_printf(o, "\e=");
else
output_printf(o, "\e>");
output_flush(o);
}
/*
* Terminal Handling
*/
static void terminal_dirty(Terminal *t) {
int r;
assert(t);
if (t->is_scheduled) {
t->is_dirty = true;
return;
}
/* 16ms timer */
assert(r >= 0);
if (r >= 0) {
if (r >= 0)
t->is_scheduled = true;
}
t->is_dirty = false;
}
t->is_scheduled = false;
if (t->is_dirty)
terminal_dirty(t);
return 0;
}
static int terminal_winch_fn(sd_event_source *source, const struct signalfd_siginfo *ssi, void *userdata) {
int r;
output_winch(t->output);
if (t->pty) {
if (r < 0)
log_error_errno(r, "error: pty_resize() (%d): %m", r);
}
if (r < 0)
log_error_errno(r, "error: term_screen_resize() (%d): %m", r);
terminal_dirty(t);
return 0;
}
char buf[4];
int r;
assert(t);
if (len < 1)
return 0;
if (r < 0)
log_oom();
return r;
}
static int terminal_write_tmp(Terminal *t) {
int r;
assert(t);
if (num < 1)
return 0;
if (t->pty) {
for (i = 0; i < num; ++i) {
if (r < 0)
return log_error_errno(r, "error: cannot write to PTY (%d): %m", r);
}
}
ring_flush(&t->out_ring);
return 0;
}
static void terminal_discard_tmp(Terminal *t) {
assert(t);
ring_flush(&t->out_ring);
}
case TERM_SEQ_IGNORE:
break;
case TERM_SEQ_GRAPHIC:
switch (seq->terminator) {
case 'q':
sd_event_exit(t->event, 0);
return 0;
}
break;
case TERM_SEQ_CONTROL:
switch (seq->terminator) {
case 0x03:
terminal_push_tmp(t, 0x03);
break;
}
break;
}
t->is_menu = false;
terminal_dirty(t);
return 0;
}
char buf[4096];
int r, type;
if (len < 0) {
return 0;
return -errno;
}
for (i = 0; i < len; ++i) {
for (j = 0; j < n_str; ++j) {
if (type < 0)
if (!t->is_menu) {
r = terminal_push_tmp(t, str[j]);
if (r < 0)
return r;
}
if (type == TERM_SEQ_NONE) {
/* We only intercept one-char sequences, so in
* case term_parser_feed() couldn't parse a
* sequence, it is waiting for more data. We
* know it can never be a one-char sequence
* then, so we can safely forward the data.
* This avoids withholding ESC or other values
* that may be one-shot depending on the
* application. */
r = terminal_write_tmp(t);
if (r < 0)
return r;
} else if (t->is_menu) {
r = terminal_menu(t, seq);
if (r < 0)
return r;
t->is_menu = true;
terminal_dirty(t);
} else {
r = terminal_write_tmp(t);
if (r < 0)
return r;
}
}
}
return 0;
}
static int terminal_pty_fn(Pty *pty, void *userdata, unsigned int event, const void *ptr, size_t size) {
int r;
switch (event) {
case PTY_CHILD:
sd_event_exit(t->event, 0);
break;
case PTY_DATA:
if (r < 0)
return log_error_errno(r, "error: term_screen_feed_text() (%d): %m", r);
terminal_dirty(t);
break;
}
return 0;
}
int r;
if (!t->pty)
return 0;
if (r < 0)
log_oom();
return r;
}
static int terminal_cmd_fn(term_screen *screen, void *userdata, unsigned int cmd, const term_seq *seq) {
return 0;
}
if (!t)
return NULL;
ring_clear(&t->out_ring);
term_screen_unref(t->screen);
term_parser_free(t->parser);
output_free(t->output);
sd_event_unref(t->event);
free(t);
return NULL;
}
Terminal *t;
int r;
if (r < 0)
if (r < 0)
if (!t)
return log_oom();
if (r < 0) {
log_error_errno(r, "error: tcsetattr() (%d): %m", r);
goto error;
}
if (r < 0) {
log_error_errno(r, "error: tcsetattr() (%d): %m", r);
goto error;
}
r = sd_event_default(&t->event);
if (r < 0) {
log_error_errno(r, "error: sd_event_default() (%d): %m", r);
goto error;
}
if (r < 0) {
log_error_errno(r, "error: sigprocmask_many() (%d): %m", r);
goto error;
}
if (r < 0) {
log_error_errno(r, "error: sd_event_add_signal() (%d): %m", r);
goto error;
}
if (r < 0) {
log_error_errno(r, "error: sd_event_add_signal() (%d): %m", r);
goto error;
}
if (r < 0) {
log_error_errno(r, "error: sd_event_add_signal() (%d): %m", r);
goto error;
}
if (r < 0) {
log_error_errno(r, "error: sd_event_add_signal() (%d): %m", r);
goto error;
}
/* force initial redraw on event-loop enter */
t->is_dirty = true;
r = sd_event_add_time(t->event, &t->frame_timer, CLOCK_MONOTONIC, 0, 0, terminal_frame_timer_fn, t);
if (r < 0) {
log_error_errno(r, "error: sd_event_add_time() (%d): %m", r);
goto error;
}
if (r < 0)
goto error;
r = term_parser_new(&t->parser, true);
if (r < 0)
goto error;
if (r < 0)
goto error;
if (r < 0)
goto error;
if (r < 0) {
log_error_errno(r, "error: term_screen_resize() (%d): %m", r);
goto error;
}
if (r < 0)
goto error;
*out = t;
return 0;
terminal_free(t);
return r;
}
static int terminal_run(Terminal *t) {
assert_return(t, -EINVAL);
if (pid < 0)
else if (pid == 0) {
/* child */
char **argv = (char*[]){
};
_exit(1);
}
/* parent */
return sd_event_loop(t->event);
}
/*
* Context Handling
*/
int r;
r = terminal_new(&t, 0, 1);
if (r < 0)
goto out;
r = terminal_run(t);
if (r < 0)
goto out;
out:
if (r < 0)
log_error_errno(r, "error: terminal failed (%d): %m", r);
terminal_free(t);
return -r;
}