Add tool to display emergency log message full-screen on boot failure.
This commit is contained in:
committed by
Luca Boccassi
parent
5a087ba25c
commit
fc7eb1325b
+136
-34
@@ -36,60 +36,138 @@ int dlopen_qrencode(void) {
|
||||
return r;
|
||||
}
|
||||
|
||||
static void print_border(FILE *output, unsigned width) {
|
||||
/* Four rows of border */
|
||||
for (unsigned y = 0; y < 4; y += 2) {
|
||||
fputs(ANSI_WHITE_ON_BLACK, output);
|
||||
static void print_border(FILE *output, unsigned width, unsigned row, unsigned column) {
|
||||
assert(output);
|
||||
assert(width);
|
||||
|
||||
for (unsigned x = 0; x < 4 + width + 4; x++)
|
||||
fputs(UNICODE_FULL_BLOCK, output);
|
||||
if (row != UINT_MAX && column != UINT_MAX) {
|
||||
int r, fd;
|
||||
|
||||
fputs(ANSI_NORMAL "\n", output);
|
||||
fd = fileno(output);
|
||||
if (fd < 0)
|
||||
return (void)log_debug_errno(errno, "Failed to get file descriptor from the file stream: %m");
|
||||
|
||||
r = set_terminal_cursor_position(fd, row, column);
|
||||
if (r < 0)
|
||||
log_warning_errno(r, "Failed to move terminal cursor position, ignoring: %m");
|
||||
|
||||
/* Four rows of border */
|
||||
for (unsigned y = 0; y < 4; y += 2) {
|
||||
fputs(ANSI_WHITE_ON_BLACK, output);
|
||||
|
||||
for (unsigned x = 0; x < 4 + width + 4; x++)
|
||||
fputs(UNICODE_FULL_BLOCK, output);
|
||||
|
||||
fputs(ANSI_NORMAL "\n", output);
|
||||
r = set_terminal_cursor_position(fd, row + 1, column);
|
||||
if (r < 0)
|
||||
log_warning_errno(r, "Failed to move terminal cursor position, ignoring: %m");
|
||||
}
|
||||
} else {
|
||||
/* Four rows of border */
|
||||
for (unsigned y = 0; y < 4; y += 2) {
|
||||
fputs(ANSI_WHITE_ON_BLACK, output);
|
||||
|
||||
for (unsigned x = 0; x < 4 + width + 4; x++)
|
||||
fputs(UNICODE_FULL_BLOCK, output);
|
||||
|
||||
fputs(ANSI_NORMAL "\n", output);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void write_qrcode(FILE *output, QRcode *qr) {
|
||||
static void write_qrcode(FILE *output, QRcode *qr, unsigned int row, unsigned int column) {
|
||||
assert(qr);
|
||||
|
||||
if (!output)
|
||||
output = stdout;
|
||||
|
||||
print_border(output, qr->width);
|
||||
print_border(output, qr->width, row, column);
|
||||
|
||||
for (unsigned y = 0; y < (unsigned) qr->width; y += 2) {
|
||||
const uint8_t *row1 = qr->data + qr->width * y;
|
||||
const uint8_t *row2 = row1 + qr->width;
|
||||
if (row != UINT_MAX && column != UINT_MAX) {
|
||||
/* After printing two rows of top border, we need to move the cursor down two rows before starting to print the actual QR code */
|
||||
int r, fd, move_down = 2;
|
||||
fd = fileno(output);
|
||||
if (fd < 0)
|
||||
return (void)log_debug_errno(errno, "Failed to get file descriptor from the file stream: %m");
|
||||
|
||||
fputs(ANSI_WHITE_ON_BLACK, output);
|
||||
for (unsigned x = 0; x < 4; x++)
|
||||
fputs(UNICODE_FULL_BLOCK, output);
|
||||
r = set_terminal_cursor_position(fd, row + move_down, column);
|
||||
if (r < 0)
|
||||
log_warning_errno(r, "Failed to move terminal cursor position, ignoring: %m");
|
||||
|
||||
for (unsigned x = 0; x < (unsigned) qr->width; x++) {
|
||||
bool a, b;
|
||||
for (unsigned y = 0; y < (unsigned) qr->width; y += 2) {
|
||||
const uint8_t *row1 = qr->data + qr->width * y;
|
||||
const uint8_t *row2 = row1 + qr->width;
|
||||
|
||||
a = row1[x] & 1;
|
||||
b = (y+1) < (unsigned) qr->width ? (row2[x] & 1) : false;
|
||||
fputs(ANSI_WHITE_ON_BLACK, output);
|
||||
|
||||
if (a && b)
|
||||
fputc(' ', output);
|
||||
else if (a)
|
||||
fputs(UNICODE_LOWER_HALF_BLOCK, output);
|
||||
else if (b)
|
||||
fputs(UNICODE_UPPER_HALF_BLOCK, output);
|
||||
else
|
||||
for (unsigned x = 0; x < 4; x++)
|
||||
fputs(UNICODE_FULL_BLOCK, output);
|
||||
|
||||
for (unsigned x = 0; x < (unsigned) qr->width; x++) {
|
||||
bool a, b;
|
||||
|
||||
a = row1[x] & 1;
|
||||
b = (y+1) < (unsigned) qr->width ? (row2[x] & 1) : false;
|
||||
|
||||
if (a && b)
|
||||
fputc(' ', output);
|
||||
else if (a)
|
||||
fputs(UNICODE_LOWER_HALF_BLOCK, output);
|
||||
else if (b)
|
||||
fputs(UNICODE_UPPER_HALF_BLOCK, output);
|
||||
else
|
||||
fputs(UNICODE_FULL_BLOCK, output);
|
||||
}
|
||||
|
||||
for (unsigned x = 0; x < 4; x++)
|
||||
fputs(UNICODE_FULL_BLOCK, output);
|
||||
r = set_terminal_cursor_position(fd, row + move_down, column);
|
||||
if (r < 0)
|
||||
log_warning_errno(r, "Failed to move terminal cursor position, ignoring: %m");
|
||||
move_down += 1;
|
||||
fputs(ANSI_NORMAL "\n", output);
|
||||
}
|
||||
|
||||
for (unsigned x = 0; x < 4; x++)
|
||||
fputs(UNICODE_FULL_BLOCK, output);
|
||||
fputs(ANSI_NORMAL "\n", output);
|
||||
print_border(output, qr->width, row + move_down, column);
|
||||
} else {
|
||||
|
||||
for (unsigned y = 0; y < (unsigned) qr->width; y += 2) {
|
||||
const uint8_t *row1 = qr->data + qr->width * y;
|
||||
const uint8_t *row2 = row1 + qr->width;
|
||||
|
||||
fputs(ANSI_WHITE_ON_BLACK, output);
|
||||
for (unsigned x = 0; x < 4; x++)
|
||||
fputs(UNICODE_FULL_BLOCK, output);
|
||||
|
||||
for (unsigned x = 0; x < (unsigned) qr->width; x++) {
|
||||
bool a, b;
|
||||
|
||||
a = row1[x] & 1;
|
||||
b = (y+1) < (unsigned) qr->width ? (row2[x] & 1) : false;
|
||||
|
||||
if (a && b)
|
||||
fputc(' ', output);
|
||||
else if (a)
|
||||
fputs(UNICODE_LOWER_HALF_BLOCK, output);
|
||||
else if (b)
|
||||
fputs(UNICODE_UPPER_HALF_BLOCK, output);
|
||||
else
|
||||
fputs(UNICODE_FULL_BLOCK, output);
|
||||
}
|
||||
|
||||
for (unsigned x = 0; x < 4; x++)
|
||||
fputs(UNICODE_FULL_BLOCK, output);
|
||||
fputs(ANSI_NORMAL "\n", output);
|
||||
}
|
||||
|
||||
print_border(output, qr->width, row, column);
|
||||
}
|
||||
|
||||
print_border(output, qr->width);
|
||||
fflush(output);
|
||||
}
|
||||
|
||||
int print_qrcode(FILE *out, const char *header, const char *string) {
|
||||
int print_qrcode_full(FILE *out, const char *header, const char *string, unsigned row, unsigned column, unsigned tty_width, unsigned tty_height) {
|
||||
QRcode* qr;
|
||||
int r;
|
||||
|
||||
@@ -106,10 +184,34 @@ int print_qrcode(FILE *out, const char *header, const char *string) {
|
||||
if (!qr)
|
||||
return -ENOMEM;
|
||||
|
||||
if (header)
|
||||
fprintf(out, "\n%s:\n\n", header);
|
||||
if (row != UINT_MAX && column != UINT_MAX) {
|
||||
int fd;
|
||||
unsigned qr_code_width, qr_code_height;
|
||||
fd = fileno(out);
|
||||
if (fd < 0)
|
||||
return log_debug_errno(errno, "Failed to get file descriptor from the file stream: %m");
|
||||
qr_code_width = qr_code_height = qr->width + 8;
|
||||
|
||||
write_qrcode(out, qr);
|
||||
if (column + qr_code_width > tty_width)
|
||||
column = tty_width - qr_code_width;
|
||||
|
||||
/* Terminal characters are twice as high as they are wide so it's qr_code_height / 2,
|
||||
* our QR code prints an extra new line, so we have -1 as well */
|
||||
if (row + qr_code_height > tty_height)
|
||||
row = tty_height - (qr_code_height / 2 ) - 1;
|
||||
|
||||
if (header) {
|
||||
r = set_terminal_cursor_position(fd, row - 2, tty_width - qr_code_width - 2);
|
||||
if (r < 0)
|
||||
log_warning_errno(r, "Failed to move terminal cursor position, ignoring: %m");
|
||||
|
||||
fprintf(out, "%s:\n\n", header);
|
||||
}
|
||||
} else
|
||||
if (header)
|
||||
fprintf(out, "\n%s:\n\n", header);
|
||||
|
||||
write_qrcode(out, qr, row, column);
|
||||
|
||||
fputc('\n', out);
|
||||
|
||||
|
||||
@@ -3,12 +3,19 @@
|
||||
#pragma once
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
|
||||
#if HAVE_QRENCODE
|
||||
int dlopen_qrencode(void);
|
||||
|
||||
int print_qrcode(FILE *out, const char *header, const char *string);
|
||||
int print_qrcode_full(FILE *out, const char *header, const char *string, unsigned row, unsigned column, unsigned tty_width, unsigned tty_height);
|
||||
static inline int print_qrcode(FILE *out, const char *header, const char *string) {
|
||||
return print_qrcode_full(out, header, string, UINT_MAX, UINT_MAX, UINT_MAX, UINT_MAX);
|
||||
}
|
||||
#else
|
||||
static inline int print_qrcode_full(FILE *out, const char *header, const char *string, unsigned row, unsigned column, unsigned tty_width, unsigned tty_height) {
|
||||
return -EOPNOTSUPP;
|
||||
}
|
||||
static inline int print_qrcode(FILE *out, const char *header, const char *string) {
|
||||
return -EOPNOTSUPP;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user