trivial: Enable VT sequences on Win32

This commit is contained in:
Richard Hughes 2022-01-06 16:49:36 +00:00
parent 9cd9a4b8f7
commit 25ee969a27
4 changed files with 79 additions and 6 deletions

View File

@ -3080,6 +3080,7 @@ main(int argc, char *argv[])
gboolean ignore_vid_pid = FALSE; gboolean ignore_vid_pid = FALSE;
g_auto(GStrv) plugin_glob = NULL; g_auto(GStrv) plugin_glob = NULL;
g_autoptr(FuUtilPrivate) priv = g_new0(FuUtilPrivate, 1); g_autoptr(FuUtilPrivate) priv = g_new0(FuUtilPrivate, 1);
g_autoptr(GError) error_console = NULL;
g_autoptr(GError) error = NULL; g_autoptr(GError) error = NULL;
g_autoptr(GPtrArray) cmd_array = fu_util_cmd_array_new(); g_autoptr(GPtrArray) cmd_array = fu_util_cmd_array_new();
g_autofree gchar *cmd_descriptions = NULL; g_autofree gchar *cmd_descriptions = NULL;
@ -3545,11 +3546,12 @@ main(int argc, char *argv[])
fu_util_cmd_array_sort(cmd_array); fu_util_cmd_array_sort(cmd_array);
/* non-TTY consoles cannot answer questions */ /* non-TTY consoles cannot answer questions */
priv->interactive = isatty(fileno(stdout)) != 0; if (!fu_util_setup_interactive_console(&error_console)) {
if (!priv->interactive) { g_debug("failed to initialize interactive console: %s", error_console->message);
priv->no_reboot_check = TRUE; priv->no_reboot_check = TRUE;
priv->no_safety_check = TRUE; priv->no_safety_check = TRUE;
} else { } else {
priv->interactive = TRUE;
/* set our implemented feature set */ /* set our implemented feature set */
fu_engine_request_set_feature_flags( fu_engine_request_set_feature_flags(
priv->request, priv->request,

View File

@ -9,6 +9,7 @@
#include <config.h> #include <config.h>
#include <glib/gi18n.h> #include <glib/gi18n.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
#ifdef HAVE_GUSB #ifdef HAVE_GUSB
#include <gusb.h> #include <gusb.h>
#endif #endif
@ -18,6 +19,11 @@
#include <curl/curl.h> #include <curl/curl.h>
#endif #endif
#ifdef _WIN32
#include <wchar.h>
#include <windows.h>
#endif
#include "fu-common.h" #include "fu-common.h"
#include "fu-device-private.h" #include "fu-device-private.h"
#include "fu-device.h" #include "fu-device.h"
@ -2487,3 +2493,63 @@ fu_util_is_url(const gchar *perhaps_url)
g_str_has_prefix(perhaps_url, "https://"); g_str_has_prefix(perhaps_url, "https://");
#endif #endif
} }
gboolean
fu_util_setup_interactive_console(GError **error)
{
#ifdef _WIN32
HANDLE hOut;
DWORD dwMode = 0;
/* enable VT sequences */
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hOut == INVALID_HANDLE_VALUE) {
g_set_error(error,
G_IO_ERROR,
G_IO_ERROR_NOT_SUPPORTED,
"failed to get stdout [%u]",
(guint)GetLastError());
return FALSE;
}
if (!GetConsoleMode(hOut, &dwMode)) {
g_set_error(error,
G_IO_ERROR,
G_IO_ERROR_NOT_SUPPORTED,
"failed to get mode [%u]",
(guint)GetLastError());
return FALSE;
}
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if (!SetConsoleMode(hOut, dwMode)) {
g_set_error(error,
G_IO_ERROR,
G_IO_ERROR_NOT_SUPPORTED,
"failed to set mode [%u]",
(guint)GetLastError());
return FALSE;
}
if (!SetConsoleOutputCP(CP_UTF8)) {
g_set_error(error,
G_IO_ERROR,
G_IO_ERROR_NOT_SUPPORTED,
"failed to set output UTF-8 [%u]",
(guint)GetLastError());
return FALSE;
}
if (!SetConsoleCP(CP_UTF8)) {
g_set_error(error,
G_IO_ERROR,
G_IO_ERROR_NOT_SUPPORTED,
"failed to set UTF-8 [%u]",
(guint)GetLastError());
return FALSE;
}
#else
if (isatty(fileno(stdout)) == 0) {
g_set_error_literal(error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "not a TTY");
return FALSE;
}
#endif
/* success */
return TRUE;
}

View File

@ -147,3 +147,5 @@ void
fu_util_show_unsupported_warn(void); fu_util_show_unsupported_warn(void);
gboolean gboolean
fu_util_is_url(const gchar *perhaps_url); fu_util_is_url(const gchar *perhaps_url);
gboolean
fu_util_setup_interactive_console(GError **error);

View File

@ -3712,7 +3712,7 @@ main(int argc, char *argv[])
gboolean allow_older = FALSE; gboolean allow_older = FALSE;
gboolean allow_reinstall = FALSE; gboolean allow_reinstall = FALSE;
gboolean enable_ipfs = FALSE; gboolean enable_ipfs = FALSE;
gboolean is_interactive = TRUE; gboolean is_interactive = FALSE;
gboolean no_history = FALSE; gboolean no_history = FALSE;
gboolean offline = FALSE; gboolean offline = FALSE;
gboolean ret; gboolean ret;
@ -3721,6 +3721,7 @@ main(int argc, char *argv[])
g_autoptr(FuUtilPrivate) priv = g_new0(FuUtilPrivate, 1); g_autoptr(FuUtilPrivate) priv = g_new0(FuUtilPrivate, 1);
g_autoptr(GDateTime) dt_now = g_date_time_new_now_utc(); g_autoptr(GDateTime) dt_now = g_date_time_new_now_utc();
g_autoptr(GError) error = NULL; g_autoptr(GError) error = NULL;
g_autoptr(GError) error_console = NULL;
g_autoptr(GPtrArray) cmd_array = fu_util_cmd_array_new(); g_autoptr(GPtrArray) cmd_array = fu_util_cmd_array_new();
g_autofree gchar *cmd_descriptions = NULL; g_autofree gchar *cmd_descriptions = NULL;
g_autofree gchar *filter = NULL; g_autofree gchar *filter = NULL;
@ -4209,15 +4210,17 @@ main(int argc, char *argv[])
} }
/* non-TTY consoles cannot answer questions */ /* non-TTY consoles cannot answer questions */
if (isatty(fileno(stdout)) == 0) { if (!fu_util_setup_interactive_console(&error_console)) {
is_interactive = FALSE; g_debug("failed to initialize interactive console: %s", error_console->message);
priv->no_unreported_check = TRUE; priv->no_unreported_check = TRUE;
priv->no_metadata_check = TRUE; priv->no_metadata_check = TRUE;
priv->no_reboot_check = TRUE; priv->no_reboot_check = TRUE;
priv->no_safety_check = TRUE; priv->no_safety_check = TRUE;
priv->no_remote_check = TRUE; priv->no_remote_check = TRUE;
fu_progressbar_set_interactive(priv->progressbar, FALSE); } else {
is_interactive = TRUE;
} }
fu_progressbar_set_interactive(priv->progressbar, is_interactive);
/* parse filter flags */ /* parse filter flags */
if (filter != NULL) { if (filter != NULL) {