]> git.ipfire.org Git - thirdparty/plymouth.git/commitdiff
libply-splash-graphics: Introduce new ply-console-viewer control
authorn3rdopolis <bluescreen_avenger@verizon.net>
Mon, 20 Mar 2023 02:41:32 +0000 (22:41 -0400)
committerRay Strode <rstrode@redhat.com>
Sat, 2 Dec 2023 23:19:33 +0000 (18:19 -0500)
src/libply-splash-graphics/meson.build
src/libply-splash-graphics/ply-console-viewer.c [new file with mode: 0644]
src/libply-splash-graphics/ply-console-viewer.h [new file with mode: 0644]

index 32fad963f2355a1fe1bb8e2e68b2168fb5987a0b..22d1092e8079b7a30bb2be5985d28f48b3b66134 100644 (file)
@@ -1,6 +1,7 @@
 libply_splash_graphics_sources = files(
   'ply-animation.c',
   'ply-capslock-icon.c',
+  'ply-console-viewer.c',
   'ply-entry.c',
   'ply-image.c',
   'ply-keymap-icon.c',
@@ -40,6 +41,7 @@ libply_splash_graphics_dep = declare_dependency(
 libply_splash_graphics_headers = files(
   'ply-animation.h',
   'ply-capslock-icon.h',
+  'ply-console-viewer.h',
   'ply-entry.h',
   'ply-image.h',
   'ply-keymap-icon.h',
diff --git a/src/libply-splash-graphics/ply-console-viewer.c b/src/libply-splash-graphics/ply-console-viewer.c
new file mode 100644 (file)
index 0000000..274e389
--- /dev/null
@@ -0,0 +1,324 @@
+/* ply-console-view.c - console message viewer
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ *
+ */
+
+#include <stdlib.h>
+#include <assert.h>
+
+#include "ply-label.h"
+#include "ply-array.h"
+#include "ply-pixel-display.h"
+#include "ply-image.h"
+#include "ply-kmsg-reader.h"
+#include "ply-console-viewer.h"
+#include "ply-rich-text.h"
+
+#define TERMINAL_OUTPUT_UPDATE_INTERVAL (1.0 / 60)
+
+struct _ply_console_viewer
+{
+        ply_event_loop_t        *loop;
+
+        ply_terminal_emulator_t *terminal_emulator;
+
+        ply_pixel_display_t     *display;
+        ply_rectangle_t          area;
+
+        ply_list_t              *message_labels;
+
+        uint32_t                 is_hidden : 1;
+        uint32_t                 output_queued : 1;
+        uint32_t                 needs_redraw : 1;
+
+        char                    *font;
+        long                     font_height;
+        long                     font_width;
+        int                      line_max_chars;
+
+        uint32_t                 text_color;
+};
+
+static void update_console_messages (ply_console_viewer_t *console_viewer);
+static void on_terminal_emulator_output (ply_console_viewer_t *console_viewer);
+
+bool ply_console_viewer_preferred (void)
+{
+        return !ply_kernel_command_line_has_argument ("plymouth.prefer-fbcon");
+}
+
+ply_console_viewer_t *
+ply_console_viewer_new (ply_pixel_display_t *display,
+                        const char          *font)
+{
+        ply_console_viewer_t *console_viewer;
+        ply_label_t *console_message_label, *measure_label;
+        size_t line_count;
+
+        console_viewer = calloc (1, sizeof(struct _ply_console_viewer));
+
+        console_viewer->message_labels = ply_list_new ();
+        console_viewer->is_hidden = true;
+
+        console_viewer->font = strdup (font);
+
+        measure_label = ply_label_new ();
+        ply_label_set_text (measure_label, " ");
+        ply_label_set_font (measure_label, console_viewer->font);
+
+        console_viewer->text_color = PLY_CONSOLE_VIEWER_LOG_TEXT_COLOR;
+
+        console_viewer->font_height = ply_label_get_height (measure_label);
+        console_viewer->font_width = ply_label_get_width (measure_label);
+        /* Allow the label to be the size of how many characters can fit in the width of the screeen, minus one for larger fonts that have some size overhead */
+        console_viewer->line_max_chars = ply_pixel_display_get_width (display) / console_viewer->font_width - 1;
+        line_count = ply_pixel_display_get_height (display) / console_viewer->font_height;
+
+        /* Display at least one line */
+        if (line_count < 0)
+                line_count = 1;
+
+        ply_label_free (measure_label);
+
+        for (size_t label_index = 0; label_index < line_count; label_index++) {
+                console_message_label = ply_label_new ();
+                ply_label_set_font (console_message_label, console_viewer->font);
+                ply_list_append_data (console_viewer->message_labels, console_message_label);
+        }
+
+        console_viewer->terminal_emulator = ply_terminal_emulator_new (line_count);
+
+        ply_terminal_emulator_watch_for_output (console_viewer->terminal_emulator,
+                                                (ply_terminal_emulator_output_handler_t)
+                                                on_terminal_emulator_output,
+                                                console_viewer);
+
+        return console_viewer;
+}
+
+void
+ply_console_viewer_free (ply_console_viewer_t *console_viewer)
+{
+        ply_list_node_t *node;
+        ply_label_t *console_message_label;
+
+        ply_list_foreach (console_viewer->message_labels, node) {
+                console_message_label = ply_list_node_get_data (node);
+                ply_label_free (console_message_label);
+        }
+        ply_list_free (console_viewer->message_labels);
+        ply_terminal_emulator_free (console_viewer->terminal_emulator);
+
+        free (console_viewer->font);
+        free (console_viewer);
+}
+
+static void
+update_console_messages (ply_console_viewer_t *console_viewer)
+{
+        ply_list_node_t *node;
+        ply_label_t *console_message_label;
+        size_t message_number;
+        ssize_t characters_left;
+        ply_rich_text_span_t span;
+
+        console_viewer->output_queued = false;
+
+        if (console_viewer->terminal_emulator == NULL)
+                return;
+
+        if (console_viewer->display == NULL)
+                return;
+
+        message_number = ply_terminal_emulator_get_line_count (console_viewer->terminal_emulator) - 1;
+
+        if (message_number < 0)
+                return;
+
+        ply_pixel_display_pause_updates (console_viewer->display);
+        node = ply_list_get_first_node (console_viewer->message_labels);
+        while (node != NULL) {
+                ply_rich_text_t *line = NULL;
+
+                characters_left = 0;
+                if (message_number >= 0) {
+                        line = ply_terminal_emulator_get_nth_line (console_viewer->terminal_emulator, message_number);
+
+                        if (line != NULL) {
+                                ply_rich_text_take_reference (line);
+                                characters_left = ply_rich_text_get_length (line);
+                        }
+                }
+
+                span.offset = characters_left;
+                while (characters_left >= 0) {
+                        console_message_label = ply_list_node_get_data (node);
+
+                        span.range = span.offset % console_viewer->line_max_chars;
+                        if (span.range == 0)
+                                span.range = console_viewer->line_max_chars;
+
+                        characters_left = span.offset - span.range - 1;
+
+                        if (span.offset - span.range >= 0)
+                                span.offset -= span.range;
+                        else
+                                span.offset = 0;
+
+                        if (line != NULL) {
+                                ply_label_set_rich_text (console_message_label, line, &span);
+                        } else {
+                                ply_label_set_text (console_message_label, "");
+                        }
+
+                        node = ply_list_get_next_node (console_viewer->message_labels, node);
+                        if (node == NULL)
+                                break;
+                }
+
+                if (line != NULL)
+                        ply_rich_text_drop_reference (line);
+
+                if (message_number <= 0)
+                        break;
+
+                message_number--;
+        }
+        console_viewer->needs_redraw = true;
+        ply_pixel_display_draw_area (console_viewer->display, 0, 0,
+                                     ply_pixel_display_get_width (console_viewer->display),
+                                     ply_pixel_display_get_height (console_viewer->display));
+        ply_pixel_display_unpause_updates (console_viewer->display);
+}
+
+void
+ply_console_viewer_show (ply_console_viewer_t *console_viewer,
+                         ply_pixel_display_t  *display)
+{
+        uint32_t label_color;
+        size_t label_index;
+        ply_list_node_t *node;
+
+        assert (console_viewer != NULL);
+
+        console_viewer->display = display;
+        console_viewer->is_hidden = false;
+
+        label_color = console_viewer->text_color;
+
+        label_index = 0;
+        ply_list_foreach (console_viewer->message_labels, node) {
+                ply_label_t *console_message_label;
+                console_message_label = ply_list_node_get_data (node);
+                ply_label_show (console_message_label, console_viewer->display,
+                                console_viewer->font_width / 2,
+                                (ply_pixel_display_get_height (console_viewer->display) - (console_viewer->font_height * label_index) - console_viewer->font_height));
+                ply_label_set_hex_color (console_message_label, label_color);
+                label_index++;
+        }
+
+        update_console_messages (console_viewer);
+}
+
+void
+ply_console_viewer_draw_area (ply_console_viewer_t *console_viewer,
+                              ply_pixel_buffer_t   *buffer,
+                              long                  x,
+                              long                  y,
+                              unsigned long         width,
+                              unsigned long         height)
+{
+        ply_list_node_t *node;
+        size_t label_index;
+        ply_label_t *console_message_label;
+
+        if (!console_viewer->needs_redraw)
+                return;
+
+        if (console_viewer->is_hidden)
+                return;
+
+        label_index = 0;
+        ply_list_foreach (console_viewer->message_labels, node) {
+                console_message_label = ply_list_node_get_data (node);
+                ply_label_draw_area (console_message_label, buffer,
+                                     MAX (x, console_viewer->font_width / 2),
+                                     MAX (y, (ply_pixel_display_get_height (console_viewer->display) - (console_viewer->font_height * label_index) - console_viewer->font_height)),
+                                     MIN (ply_label_get_width (console_message_label), width),
+                                     MIN (height, console_viewer->font_height));
+                label_index++;
+        }
+
+        console_viewer->needs_redraw = false;
+}
+
+void
+ply_console_viewer_hide (ply_console_viewer_t *console_viewer)
+{
+        ply_list_node_t *node;
+        ply_label_t *console_message_label;
+
+        if (console_viewer->is_hidden)
+                return;
+
+        console_viewer->is_hidden = true;
+
+        ply_list_foreach (console_viewer->message_labels, node) {
+                console_message_label = ply_list_node_get_data (node);
+                ply_label_hide (console_message_label);
+        }
+
+        console_viewer->display = NULL;
+}
+
+static void
+on_terminal_emulator_output (ply_console_viewer_t *console_viewer)
+{
+        if (console_viewer->output_queued)
+                return;
+
+        if (console_viewer->is_hidden)
+                return;
+
+        ply_event_loop_watch_for_timeout (ply_event_loop_get_default (),
+                                          TERMINAL_OUTPUT_UPDATE_INTERVAL,
+                                          (ply_event_loop_timeout_handler_t)
+                                          update_console_messages, console_viewer);
+        console_viewer->output_queued = true;
+}
+
+void
+ply_console_viewer_set_text_color (ply_console_viewer_t *console_viewer,
+                                   uint32_t              hex_color)
+{
+        console_viewer->text_color = hex_color;
+}
+
+void
+ply_console_viewer_convert_boot_buffer (ply_console_viewer_t *console_viewer,
+                                        ply_buffer_t         *boot_buffer)
+{
+        ply_terminal_emulator_convert_boot_buffer (console_viewer->terminal_emulator, boot_buffer);
+}
+
+void
+ply_console_viewer_parse_lines (ply_console_viewer_t *console_viewer,
+                                const char           *text,
+                                size_t                size)
+{
+        ply_terminal_emulator_parse_lines (console_viewer->terminal_emulator, text, size);
+}
diff --git a/src/libply-splash-graphics/ply-console-viewer.h b/src/libply-splash-graphics/ply-console-viewer.h
new file mode 100644 (file)
index 0000000..88b93b0
--- /dev/null
@@ -0,0 +1,55 @@
+/* ply-console-viewer.h - console message viewer
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ *
+ */
+#ifndef PLY_CONSOLE_VIEWER_H
+#define PLY_CONSOLE_VIEWER_H
+
+#include "ply-key-file.h"
+#include "ply-list.h"
+#include "ply-event-loop.h"
+#include "ply-pixel-display.h"
+#include "ply-terminal-emulator.h"
+
+typedef struct _ply_console_viewer ply_console_viewer_t;
+
+#define PLY_CONSOLE_VIEWER_LOG_TEXT_COLOR 0xffffffff   /* white */
+
+#ifndef PLY_HIDE_FUNCTION_DECLARATIONS
+bool ply_console_viewer_preferred (void);
+ply_console_viewer_t *ply_console_viewer_new (ply_pixel_display_t *display,
+                                              const char          *fontdesc);
+void ply_console_viewer_free (ply_console_viewer_t *console_viewer);
+void ply_console_viewer_show (ply_console_viewer_t *console_viewer,
+                              ply_pixel_display_t  *display);
+void ply_console_viewer_draw_area (ply_console_viewer_t *console_viewer,
+                                   ply_pixel_buffer_t   *buffer,
+                                   long                  x,
+                                   long                  y,
+                                   unsigned long         width,
+                                   unsigned long         height);
+void ply_console_viewer_hide (ply_console_viewer_t *console_viewer);
+void ply_console_viewer_set_text_color (ply_console_viewer_t *console_viewer,
+                                        uint32_t              hex_color);
+void ply_console_viewer_convert_boot_buffer (ply_console_viewer_t *console_viewer,
+                                             ply_buffer_t         *boot_buffer);
+void ply_console_viewer_parse_lines (ply_console_viewer_t *console_viewer,
+                                     const char           *text,
+                                     size_t                size);
+#endif //PLY_HIDE_FUNCTION_DECLARATIONS
+
+#endif //PLY_CONSOLE_VIEWER_H