2019-06-12 20:08:54 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
2019-09-21 20:01:55 +02:00
|
|
|
#include <ctype.h>
|
2019-06-12 20:08:54 +02:00
|
|
|
#include <stdbool.h>
|
2019-06-13 15:19:10 +02:00
|
|
|
#include <unistd.h>
|
2019-06-12 20:08:54 +02:00
|
|
|
#include <assert.h>
|
2019-06-13 15:19:10 +02:00
|
|
|
#include <fcntl.h>
|
2019-06-13 21:23:52 +02:00
|
|
|
#include <locale.h>
|
2019-07-03 09:46:13 +02:00
|
|
|
#include <getopt.h>
|
2019-06-12 20:08:54 +02:00
|
|
|
#include <poll.h>
|
2019-07-03 15:16:38 +02:00
|
|
|
#include <errno.h>
|
2019-06-12 20:08:54 +02:00
|
|
|
|
2019-07-21 19:14:19 +02:00
|
|
|
#include <sys/timerfd.h>
|
2019-07-29 20:13:26 +02:00
|
|
|
#include <sys/sysinfo.h>
|
2019-08-23 17:23:47 +02:00
|
|
|
#include <sys/wait.h>
|
2019-10-27 11:46:18 +01:00
|
|
|
#include <sys/epoll.h>
|
2019-07-21 19:14:19 +02:00
|
|
|
|
2019-06-12 20:08:54 +02:00
|
|
|
#include <wayland-client.h>
|
2019-07-05 10:44:09 +02:00
|
|
|
#include <wayland-cursor.h>
|
2019-06-12 20:08:54 +02:00
|
|
|
#include <xdg-shell.h>
|
|
|
|
|
|
2019-08-12 21:22:38 +02:00
|
|
|
#include <xdg-output-unstable-v1.h>
|
2019-08-30 17:55:45 +02:00
|
|
|
#include <xdg-decoration-unstable-v1.h>
|
2019-08-12 21:22:38 +02:00
|
|
|
|
2019-06-12 20:08:54 +02:00
|
|
|
#define LOG_MODULE "main"
|
2019-10-27 11:46:18 +01:00
|
|
|
#define LOG_ENABLE_DBG 1
|
2019-06-12 20:08:54 +02:00
|
|
|
#include "log.h"
|
2019-06-13 15:19:10 +02:00
|
|
|
|
2019-07-16 11:52:22 +02:00
|
|
|
#include "config.h"
|
2019-10-27 11:46:18 +01:00
|
|
|
#include "fdm.h"
|
2019-06-13 16:24:35 +02:00
|
|
|
#include "font.h"
|
2019-07-05 10:16:56 +02:00
|
|
|
#include "grid.h"
|
2019-10-27 19:28:23 +01:00
|
|
|
#include "render.h"
|
2019-06-12 20:08:54 +02:00
|
|
|
#include "shm.h"
|
2019-06-13 15:19:10 +02:00
|
|
|
#include "slave.h"
|
2019-06-15 22:22:44 +02:00
|
|
|
#include "terminal.h"
|
2019-07-17 09:55:36 +02:00
|
|
|
#include "tokenize.h"
|
2019-10-19 22:09:52 +02:00
|
|
|
#include "version.h"
|
2019-06-15 22:22:44 +02:00
|
|
|
#include "vt.h"
|
2019-06-19 14:17:43 +02:00
|
|
|
|
|
|
|
|
#define min(x, y) ((x) < (y) ? (x) : (y))
|
2019-06-26 19:33:39 +02:00
|
|
|
#define max(x, y) ((x) > (y) ? (x) : (y))
|
2019-06-12 20:08:54 +02:00
|
|
|
|
2019-10-27 11:46:18 +01:00
|
|
|
static bool
|
|
|
|
|
fdm_ptmx(struct fdm *fdm, int fd, int events, void *data)
|
|
|
|
|
{
|
|
|
|
|
struct terminal *term = data;
|
|
|
|
|
|
|
|
|
|
if (events & EPOLLHUP) {
|
|
|
|
|
term->quit = true;
|
|
|
|
|
|
|
|
|
|
if (!(events & EPOLLIN))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert(events & EPOLLIN);
|
|
|
|
|
|
|
|
|
|
uint8_t buf[24 * 1024];
|
|
|
|
|
ssize_t count = read(term->ptmx, buf, sizeof(buf));
|
|
|
|
|
|
|
|
|
|
if (count < 0) {
|
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
LOG_ERRNO("failed to read from pseudo terminal");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vt_from_slave(term, buf, count);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* We likely need to re-render. But, we don't want to
|
|
|
|
|
* do it immediately. Often, a single client operation
|
|
|
|
|
* is done through multiple writes. Many times, we're
|
|
|
|
|
* so fast that we render mid-operation frames.
|
|
|
|
|
*
|
|
|
|
|
* For example, we might end up rendering a frame
|
|
|
|
|
* where the client just erased a line, while in the
|
|
|
|
|
* next frame, the client wrote to the same line. This
|
|
|
|
|
* causes screen "flashes".
|
|
|
|
|
*
|
|
|
|
|
* Mitigate by always incuring a small delay before
|
|
|
|
|
* rendering the next frame. This gives the client
|
|
|
|
|
* some time to finish the operation (and thus gives
|
|
|
|
|
* us time to receive the last writes before doing any
|
|
|
|
|
* actual rendering).
|
|
|
|
|
*
|
|
|
|
|
* We incur this delay *every* time we receive
|
|
|
|
|
* input. To ensure we don't delay rendering
|
|
|
|
|
* indefinitely, we start a second timer that is only
|
|
|
|
|
* reset when we render.
|
|
|
|
|
*
|
|
|
|
|
* Note that when the client is producing data at a
|
|
|
|
|
* very high pace, we're rate limited by the wayland
|
|
|
|
|
* compositor anyway. The delay we introduce here only
|
|
|
|
|
* has any effect when the renderer is idle.
|
|
|
|
|
*
|
|
|
|
|
* TODO: this adds input latency. Can we somehow hint
|
|
|
|
|
* ourselves we just received keyboard input, and in
|
|
|
|
|
* this case *not* delay rendering?
|
|
|
|
|
*/
|
2019-10-27 19:08:48 +01:00
|
|
|
if (term->window->frame_callback == NULL) {
|
2019-10-27 11:46:18 +01:00
|
|
|
/* First timeout - reset each time we receive input. */
|
|
|
|
|
timerfd_settime(
|
|
|
|
|
term->delayed_render_timer.lower_fd, 0,
|
|
|
|
|
&(struct itimerspec){.it_value = {.tv_nsec = 1000000}},
|
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
|
|
/* Second timeout - only reset when we render. Set to one
|
|
|
|
|
* frame (assuming 60Hz) */
|
|
|
|
|
if (!term->delayed_render_timer.is_armed) {
|
|
|
|
|
timerfd_settime(
|
|
|
|
|
term->delayed_render_timer.upper_fd, 0,
|
|
|
|
|
&(struct itimerspec){.it_value = {.tv_nsec = 16666666}},
|
|
|
|
|
NULL);
|
|
|
|
|
term->delayed_render_timer.is_armed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return !(events & EPOLLHUP);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
fdm_flash(struct fdm *fdm, int fd, int events, void *data)
|
|
|
|
|
{
|
|
|
|
|
if (events & EPOLLHUP)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
struct terminal *term = data;
|
|
|
|
|
uint64_t expiration_count;
|
|
|
|
|
ssize_t ret = read(
|
|
|
|
|
term->flash.fd, &expiration_count, sizeof(expiration_count));
|
|
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
LOG_ERRNO("failed to read flash timer");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOG_DBG("flash timer expired %llu times",
|
|
|
|
|
(unsigned long long)expiration_count);
|
|
|
|
|
|
|
|
|
|
term->flash.active = false;
|
|
|
|
|
term_damage_view(term);
|
|
|
|
|
render_refresh(term);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
fdm_blink(struct fdm *fdm, int fd, int events, void *data)
|
|
|
|
|
{
|
|
|
|
|
if (events & EPOLLHUP)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
struct terminal *term = data;
|
|
|
|
|
uint64_t expiration_count;
|
|
|
|
|
ssize_t ret = read(
|
|
|
|
|
term->blink.fd, &expiration_count, sizeof(expiration_count));
|
|
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
LOG_ERRNO("failed to read blink timer");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOG_DBG("blink timer expired %llu times",
|
|
|
|
|
(unsigned long long)expiration_count);
|
|
|
|
|
|
|
|
|
|
term->blink.state = term->blink.state == BLINK_ON
|
|
|
|
|
? BLINK_OFF : BLINK_ON;
|
|
|
|
|
|
|
|
|
|
/* Scan all visible cells and mark rows with blinking cells dirty */
|
|
|
|
|
for (int r = 0; r < term->rows; r++) {
|
|
|
|
|
struct row *row = grid_row_in_view(term->grid, r);
|
|
|
|
|
for (int col = 0; col < term->cols; col++) {
|
|
|
|
|
struct cell *cell = &row->cells[col];
|
|
|
|
|
|
|
|
|
|
if (cell->attrs.blink) {
|
|
|
|
|
cell->attrs.clean = 0;
|
|
|
|
|
row->dirty = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render_refresh(term);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
fdm_delayed_render(struct fdm *fdm, int fd, int events, void *data)
|
|
|
|
|
{
|
|
|
|
|
if (events & EPOLLHUP)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
struct terminal *term = data;
|
|
|
|
|
assert(term->delayed_render_timer.is_armed);
|
|
|
|
|
|
|
|
|
|
uint64_t unused;
|
|
|
|
|
ssize_t ret1 = 0;
|
|
|
|
|
ssize_t ret2 = 0;
|
|
|
|
|
|
|
|
|
|
if (fd == term->delayed_render_timer.lower_fd)
|
|
|
|
|
ret1 = read(term->delayed_render_timer.lower_fd, &unused, sizeof(unused));
|
|
|
|
|
if (fd == term->delayed_render_timer.upper_fd)
|
|
|
|
|
ret2 = read(term->delayed_render_timer.upper_fd, &unused, sizeof(unused));
|
|
|
|
|
|
|
|
|
|
if ((ret1 < 0 || ret2 < 0) && errno != EAGAIN)
|
|
|
|
|
LOG_ERRNO("failed to read timeout timer");
|
|
|
|
|
else if (ret1 > 0 || ret2 > 0) {
|
|
|
|
|
render_refresh(term);
|
|
|
|
|
|
|
|
|
|
/* Reset timers */
|
|
|
|
|
term->delayed_render_timer.is_armed = false;
|
|
|
|
|
timerfd_settime(term->delayed_render_timer.lower_fd, 0, &(struct itimerspec){.it_value = {0}}, NULL);
|
|
|
|
|
timerfd_settime(term->delayed_render_timer.upper_fd, 0, &(struct itimerspec){.it_value = {0}}, NULL);
|
|
|
|
|
} else
|
|
|
|
|
assert(false);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-11 16:03:29 +02:00
|
|
|
static void
|
|
|
|
|
print_usage(const char *prog_name)
|
|
|
|
|
{
|
|
|
|
|
printf("Usage: %s [OPTION]...\n", prog_name);
|
|
|
|
|
printf("\n");
|
|
|
|
|
printf("Options:\n");
|
2019-09-21 20:01:55 +02:00
|
|
|
printf(" -f,--font=FONT comma separated list of fonts in fontconfig format (monospace)\n"
|
2019-08-23 17:26:41 +02:00
|
|
|
" -t,--term=TERM value to set the environment variable TERM to (foot)\n"
|
|
|
|
|
" -g,--geometry=WIDTHxHEIGHT set initial width and height\n"
|
|
|
|
|
" -v,--version show the version number and quit\n");
|
2019-08-11 16:03:29 +02:00
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-12 20:08:54 +02:00
|
|
|
int
|
2019-07-03 09:46:13 +02:00
|
|
|
main(int argc, char *const *argv)
|
2019-06-12 20:08:54 +02:00
|
|
|
{
|
|
|
|
|
int ret = EXIT_FAILURE;
|
|
|
|
|
|
2019-09-26 18:41:39 +02:00
|
|
|
/* Startup notifications; we don't support it, but must ensure we
|
|
|
|
|
* don't pass this on to programs launched by us */
|
|
|
|
|
unsetenv("DESKTOP_STARTUP_ID");
|
|
|
|
|
|
2019-07-17 10:12:14 +02:00
|
|
|
struct config conf = {NULL};
|
|
|
|
|
if (!config_load(&conf))
|
|
|
|
|
return ret;
|
2019-07-16 11:52:22 +02:00
|
|
|
|
2019-08-11 16:03:29 +02:00
|
|
|
const char *const prog_name = argv[0];
|
|
|
|
|
|
2019-07-03 09:46:13 +02:00
|
|
|
static const struct option longopts[] = {
|
2019-08-23 17:26:41 +02:00
|
|
|
{"term", required_argument, 0, 't'},
|
|
|
|
|
{"font", required_argument, 0, 'f'},
|
|
|
|
|
{"geometry", required_argument, 0, 'g'},
|
|
|
|
|
{"version", no_argument, 0, 'v'},
|
|
|
|
|
{"help", no_argument, 0, 'h'},
|
|
|
|
|
{NULL, no_argument, 0, 0},
|
2019-07-03 09:46:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
while (true) {
|
2019-08-23 17:26:41 +02:00
|
|
|
int c = getopt_long(argc, argv, ":t:f:g:vh", longopts, NULL);
|
2019-07-03 09:46:13 +02:00
|
|
|
if (c == -1)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
switch (c) {
|
2019-07-18 14:34:45 +02:00
|
|
|
case 't':
|
|
|
|
|
free(conf.term);
|
|
|
|
|
conf.term = strdup(optarg);
|
|
|
|
|
break;
|
|
|
|
|
|
2019-07-03 09:46:13 +02:00
|
|
|
case 'f':
|
2019-07-30 18:04:28 +02:00
|
|
|
tll_free_and_free(conf.fonts, free);
|
2019-09-21 20:01:55 +02:00
|
|
|
for (char *font = strtok(optarg, ","); font != NULL; font = strtok(NULL, ",")) {
|
|
|
|
|
|
|
|
|
|
/* Strip leading spaces */
|
|
|
|
|
while (*font != '\0' && isspace(*font))
|
|
|
|
|
font++;
|
|
|
|
|
|
|
|
|
|
/* Strip trailing spaces */
|
|
|
|
|
char *end = font + strlen(font);
|
|
|
|
|
assert(*end == '\0');
|
|
|
|
|
end--;
|
|
|
|
|
while (end > font && isspace(*end))
|
|
|
|
|
*(end--) = '\0';
|
|
|
|
|
|
|
|
|
|
if (strlen(font) == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
tll_push_back(conf.fonts, strdup(font));
|
|
|
|
|
}
|
2019-07-03 09:46:13 +02:00
|
|
|
break;
|
|
|
|
|
|
2019-08-23 17:26:41 +02:00
|
|
|
case 'g': {
|
|
|
|
|
unsigned width, height;
|
|
|
|
|
if (sscanf(optarg, "%ux%u", &width, &height) != 2 || width == 0 || height == 0) {
|
|
|
|
|
fprintf(stderr, "error: invalid geometry: %s\n", optarg);
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conf.width = width;
|
|
|
|
|
conf.height = height;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-11 16:03:29 +02:00
|
|
|
case 'v':
|
|
|
|
|
printf("foot version %s\n", FOOT_VERSION);
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
|
2019-07-03 09:46:13 +02:00
|
|
|
case 'h':
|
2019-08-11 16:03:29 +02:00
|
|
|
print_usage(prog_name);
|
|
|
|
|
return EXIT_SUCCESS;
|
2019-07-03 09:46:13 +02:00
|
|
|
|
|
|
|
|
case ':':
|
|
|
|
|
fprintf(stderr, "error: -%c: missing required argument\n", optopt);
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
|
fprintf(stderr, "error: -%c: invalid option\n", optopt);
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-17 09:55:36 +02:00
|
|
|
argc -= optind;
|
|
|
|
|
argv += optind;
|
|
|
|
|
|
2019-06-13 21:23:52 +02:00
|
|
|
setlocale(LC_ALL, "");
|
2019-07-18 14:29:40 +02:00
|
|
|
setenv("TERM", conf.term, 1);
|
2019-06-13 21:23:52 +02:00
|
|
|
|
2019-08-21 17:56:21 +02:00
|
|
|
|
2019-10-27 19:08:48 +01:00
|
|
|
struct fdm *fdm = NULL;
|
|
|
|
|
struct wayland *wayl = NULL;
|
2019-10-28 18:25:19 +01:00
|
|
|
struct terminal *term = NULL;
|
2019-10-27 19:08:48 +01:00
|
|
|
|
|
|
|
|
if ((fdm = fdm_init()) == NULL)
|
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
if ((wayl = wayl_init(fdm)) == NULL)
|
|
|
|
|
goto out;
|
|
|
|
|
|
2019-10-28 18:25:19 +01:00
|
|
|
if ((term = term_init(&conf, fdm, wayl, argc, argv)) == NULL)
|
2019-06-12 20:08:54 +02:00
|
|
|
goto out;
|
2019-08-12 21:49:17 +02:00
|
|
|
|
2019-10-28 18:25:19 +01:00
|
|
|
fdm_add(fdm, term->ptmx, EPOLLIN, &fdm_ptmx, term);
|
|
|
|
|
fdm_add(fdm, term->flash.fd, EPOLLIN, &fdm_flash, term);
|
|
|
|
|
fdm_add(fdm, term->blink.fd, EPOLLIN, &fdm_blink, term);
|
|
|
|
|
fdm_add(fdm, term->delayed_render_timer.lower_fd, EPOLLIN, &fdm_delayed_render, term);
|
|
|
|
|
fdm_add(fdm, term->delayed_render_timer.upper_fd, EPOLLIN, &fdm_delayed_render, term);
|
2019-09-20 22:27:27 +02:00
|
|
|
|
2019-10-27 11:46:18 +01:00
|
|
|
while (true) {
|
2019-10-28 18:25:19 +01:00
|
|
|
wl_display_flush(term->wl->display); /* TODO: figure out how to get rid of this */
|
2019-10-27 19:21:36 +01:00
|
|
|
|
2019-10-27 11:46:18 +01:00
|
|
|
if (!fdm_poll(fdm))
|
2019-06-12 20:08:54 +02:00
|
|
|
break;
|
2019-10-27 11:46:18 +01:00
|
|
|
}
|
2019-07-21 19:14:19 +02:00
|
|
|
|
2019-10-28 18:25:19 +01:00
|
|
|
if (term->quit)
|
2019-10-27 11:46:18 +01:00
|
|
|
ret = EXIT_SUCCESS;
|
2019-07-21 19:14:19 +02:00
|
|
|
|
2019-10-27 11:46:18 +01:00
|
|
|
out:
|
|
|
|
|
if (fdm != NULL) {
|
2019-10-28 18:25:19 +01:00
|
|
|
fdm_del(fdm, term->ptmx);
|
|
|
|
|
fdm_del(fdm, term->flash.fd);
|
|
|
|
|
fdm_del(fdm, term->blink.fd);
|
|
|
|
|
fdm_del(fdm, term->delayed_render_timer.lower_fd);
|
|
|
|
|
fdm_del(fdm, term->delayed_render_timer.upper_fd);
|
2019-07-29 20:13:26 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-12 20:08:54 +02:00
|
|
|
shm_fini();
|
2019-08-12 21:22:38 +02:00
|
|
|
|
2019-10-28 18:25:19 +01:00
|
|
|
int child_ret = term_destroy(term);
|
2019-10-27 19:08:48 +01:00
|
|
|
wayl_destroy(wayl);
|
2019-10-27 11:46:18 +01:00
|
|
|
fdm_destroy(fdm);
|
2019-07-16 11:52:22 +02:00
|
|
|
config_free(conf);
|
2019-10-28 18:25:19 +01:00
|
|
|
|
|
|
|
|
return ret == EXIT_SUCCESS ? child_ret : ret;
|
2019-07-28 21:03:38 +02:00
|
|
|
|
2019-06-12 20:08:54 +02:00
|
|
|
}
|