Add util.c:screen_init() to probe screen size once per boot

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson committed 2017-07-04 20:58:43 +02:00
1 parent 405cf4d997
commit be6f0de0df
6 files changed
+40 -12

No files matched your search

+4
View File
@@ -44,6 +44,7 @@
#include "sig.h" #include "sig.h"
#include "sm.h" #include "sm.h"
#include "tty.h" #include "tty.h"
#include "util.h"
#include "utmp-api.h" #include "utmp-api.h"
#include "watchdog.h" #include "watchdog.h"
@@ -228,6 +229,9 @@ int main(int argc, char* argv[])
if (getpid() != 1) if (getpid() != 1)
return client(argc, argv); return client(argc, argv);
/* Set up canvas */
screen_init();
/* /*
* In case of emergency. * In case of emergency.
*/ */
+3 -2
View File
@@ -32,13 +32,14 @@
#include <stdarg.h> #include <stdarg.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <lite/lite.h> #include <lite/lite.h>
#include <lite/conio.h>
#include "finit.h" #include "finit.h"
#include "helpers.h" #include "helpers.h"
#include "private.h" #include "private.h"
#include "util.h"
#include "utmp-api.h" #include "utmp-api.h"
char *strip_line(char *line) char *strip_line(char *line)
{ {
char *ptr; char *ptr;
@@ -79,7 +80,7 @@ static int print_timestamp(void)
void printv(const char *fmt, va_list ap) void printv(const char *fmt, va_list ap)
{ {
char buf[80]; char buf[SCREEN_WIDTH];
size_t len; size_t len;
const char dots[] = " ....................................................................."; const char dots[] = " .....................................................................";
+1 -3
View File
@@ -37,9 +37,6 @@
#include <sys/un.h> #include <sys/un.h>
#include <lite/lite.h> #include <lite/lite.h>
#define SCREEN_WIDTH screen_width()
#include <lite/conio.h>
#include "finit.h" #include "finit.h"
#include "cond.h" #include "cond.h"
#include "serv.h" #include "serv.h"
@@ -662,6 +659,7 @@ int main(int argc, char *argv[])
} }
} }
screen_init();
if (optind < argc) { if (optind < argc) {
char *cmd = argv[optind++]; char *cmd = argv[optind++];
char arg[120] = ""; char arg[120] = "";
-2
View File
@@ -28,8 +28,6 @@
#include <stdio.h> #include <stdio.h>
#include <lite/lite.h> #include <lite/lite.h>
#define SCREEN_WIDTH screen_width()
#include <lite/conio.h>
#include "util.h" #include "util.h"
static const char *cwd; static const char *cwd;
+25 -5
View File
@@ -32,6 +32,9 @@
#include <termios.h> #include <termios.h>
#endif #endif
int screen_setup = -1;
int screen_rows = 24;
int screen_cols = 80;
char *prognm = NULL; char *prognm = NULL;
char *progname(char *arg0) char *progname(char *arg0)
@@ -77,14 +80,16 @@ char *sanitize(char *arg, size_t len)
} }
#define ESC "\033" #define ESC "\033"
int screen_width(void) void screen_init(void)
{ {
int ret = 80;
#ifdef HAVE_TERMIOS_H #ifdef HAVE_TERMIOS_H
char buf[42]; char buf[42];
struct termios tc, saved; struct termios tc, saved;
struct pollfd fd = { STDIN_FILENO, POLLIN, 0 }; struct pollfd fd = { STDIN_FILENO, POLLIN, 0 };
if (screen_setup != -1)
return;
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
tcgetattr(STDERR_FILENO, &tc); tcgetattr(STDERR_FILENO, &tc);
saved = tc; saved = tc;
@@ -96,14 +101,29 @@ int screen_width(void)
if (poll(&fd, 1, 300) > 0) { if (poll(&fd, 1, 300) > 0) {
int row, col; int row, col;
if (scanf(ESC "[%d;%dR", &row, &col) == 2) if (scanf(ESC "[%d;%dR", &row, &col) == 2) {
ret = col; screen_rows = row;
screen_cols = col;
screen_setup = 1;
}
} }
fprintf(stderr, ESC "8"); fprintf(stderr, ESC "8");
tcsetattr(STDERR_FILENO, TCSANOW, &saved); tcsetattr(STDERR_FILENO, TCSANOW, &saved);
#endif #endif
return ret;
if (screen_cols > 132)
screen_cols = 132;
}
int screen_width(void)
{
screen_init();
if (screen_cols < 1)
return 80;
return screen_cols;
} }
/** /**
+7
View File
@@ -24,11 +24,18 @@
#ifndef FINIT_UTIL_H_ #ifndef FINIT_UTIL_H_
#define FINIT_UTIL_H_ #define FINIT_UTIL_H_
#define SCREEN_WIDTH screen_cols
#include <lite/conio.h>
extern int screen_rows;
extern int screen_cols;
extern char *prognm; extern char *prognm;
char *progname (char *arg0); char *progname (char *arg0);
void do_sleep (unsigned int sec); void do_sleep (unsigned int sec);
char *sanitize (char *arg, size_t len); char *sanitize (char *arg, size_t len);
void screen_init (void);
int screen_width (void); int screen_width (void);
#endif /* FINIT_UTIL_H_ */ #endif /* FINIT_UTIL_H_ */