From 290290fcc2d092c33b6479a59ebc147ff5d60ffa Mon Sep 17 00:00:00 2001
From: Nic Boet <nic@boet.cc>
Date: Fri, 16 Oct 2026 18:04:55 -0500
Subject: [PATCH 1/2] cmake: check for consolechars and setfont

The -l option is only compiled in when HAVE_CONSOLECHARS or HAVE_SETFONT
is defined; otherwise it always dies with "Unable to use both setfont
and consolechars". configure.ac checks for both programs, but the CMake
build never did, so -l could never work in CMake builds.

Add the same checks to CMakeLists.txt, preferring consolechars as
configure.ac does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
 CMakeLists.txt | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index fba6fed..3340965 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -51,6 +51,18 @@ if	(HAVE_GETOPT_H)
 	add_definitions(-DHAVE_GETOPT_H)
 endif	()
 
+# -l switches the Linux console to the matrix font with consolechars or
+# setfont, and is only compiled in when one of them was found; otherwise it
+# always fails with "Unable to use both setfont and consolechars".
+# configure.ac has always checked for them, the CMake build did not.
+find_program(CONSOLECHARS consolechars)
+find_program(SETFONT setfont)
+if (CONSOLECHARS)
+    add_definitions(-DHAVE_CONSOLECHARS)
+elseif (SETFONT)
+    add_definitions(-DHAVE_SETFONT)
+endif()
+
 Set(CURSES_NEED_NCURSES TRUE)
 Set(CURSES_NEED_WIDE TRUE)
 find_package(Curses)
-- 
2.55.0


From b6ae8c5c1c1afd6d5fbce5c12418a8fefe80cef1 Mon Sep 17 00:00:00 2001
From: Nic Boet <nic@boet.cc>
Date: Fri, 16 Oct 2026 18:12:40 -0500
Subject: [PATCH 2/2] Explain that -l needs a Linux virtual console

In a terminal window, cmatrix -l printed setfont's "Couldn't get a file
descriptor referring to the console" followed by advice to check that
setfont is in $PATH, which it is.

On Linux, check with KDGKBTYPE that stdin is a virtual console before
starting curses, and say so if it is not. Other platforms are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
 cmatrix.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/cmatrix.c b/cmatrix.c
index 7d8ca39..076f04a 100644
--- a/cmatrix.c
+++ b/cmatrix.c
@@ -61,6 +61,10 @@
 #include <sys/ioctl.h>
 #endif
 
+#ifdef __linux__
+#include <linux/kd.h>
+#endif
+
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
@@ -431,6 +435,20 @@ int main(int argc, char *argv[]) {
         }
     }
 
+#if defined(__linux__) && defined(KDGKBTYPE)
+    /* setfont and consolechars only work on a Linux virtual console. In a
+       terminal window they fail with a message that says nothing useful. */
+    if (console) {
+        char kbtype;
+        if (ioctl(STDIN_FILENO, KDGKBTYPE, &kbtype) != 0) {
+            fprintf(stderr, "cmatrix: error: -l only works on a Linux virtual "
+                    "console (e.g. Ctrl+Alt+F2),\nnot in a terminal window. "
+                    "Run cmatrix without -l there.\n");
+            exit(EXIT_FAILURE);
+        }
+    }
+#endif
+
     /* Clear TERM variable on Windows */
 #ifdef _WIN32
     _putenv_s("TERM", "");
-- 
2.55.0

