From 9873d2732fb513737a2a17dfdfb9a6f2a6722ff8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Mon, 10 Jan 2022 20:24:46 +0100 Subject: [PATCH 1/5] main: present invalid locale errors as a user-notification MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Foot does not support running under non-UTF8 locales. If we detect a non-UTF8 locale, we log this and exit with an error. However, it appears a fairly common situation is this: user (knowingly or unknowingly) only configures his/hers locale in e.g. the shell RC files. These aren’t sourced when the compositor is started via a display manager. Thus, compositor key binds will fail to launch, and the user typically has no way of seeing foot’s output. So, the user proceeds to start another terminal, and from that one tries launching foot. And it works... (because the shell in the other terminal sourced the locale configuration). User is left confused and often don’t know how to debug. This patch is somewhat hackish; in addition to logging the locale error, it also pushes a user notification. For this to be visible, we need to actually start a terminal window. So, we ignore the configured shell, and we ignore any custom command line, and instead spawns “/bin/sh -c ‘’”. This allows us to get something running (that hopefully doesn’t produce too much output we can’t decode due to the non-UTF8 locale). But, it will exit immediately. So we also set the --hold flag. This works, and may be a good enough “solution”. However, it only works for standalone foot instances, not footclients. --- main.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/main.c b/main.c index 34f72a7c..15ddbb00 100644 --- a/main.c +++ b/main.c @@ -424,10 +424,18 @@ main(int argc, char *const *argv) LOG_ERR("setlocale() failed"); return ret; } + LOG_INFO("locale: %s", locale); - if (!locale_is_utf8()) { - LOG_ERR("locale is not UTF-8"); - return ret; + + bool bad_locale = !locale_is_utf8(); + if (bad_locale) { + LOG_ERR("locale '%s' is not UTF-8", locale); + + if (check_config) + return ret; + + user_notification_add_fmt(&user_notifications, USER_NOTIFICATION_ERROR, + "locale '%s' is not UTF-8", locale); } struct config conf = {NULL}; @@ -502,6 +510,14 @@ main(int argc, char *const *argv) conf.fonts[0].arr[0].pattern, &conf.notifications); } + + if (bad_locale) { + static char *const bad_locale_fake_argv[] = {"/bin/sh", "-c", "", NULL}; + argc = 1; + argv = bad_locale_fake_argv; + conf.hold_at_exit = true; + } + struct fdm *fdm = NULL; struct reaper *reaper = NULL; struct wayland *wayl = NULL; From 827bfef5508ba454992011a61ca6c6e3ed4767f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 11 Jan 2022 21:37:03 +0100 Subject: [PATCH 2/5] =?UTF-8?q?main:=20try=20to=20force=20an=20UTF-8=20loc?= =?UTF-8?q?ale=20if=20user=E2=80=99s=20locale=20isn=E2=80=99t=20UTF-8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/main.c b/main.c index 15ddbb00..b90c4b25 100644 --- a/main.c +++ b/main.c @@ -436,6 +436,25 @@ main(int argc, char *const *argv) user_notification_add_fmt(&user_notifications, USER_NOTIFICATION_ERROR, "locale '%s' is not UTF-8", locale); + + /* + * Try to force an UTF-8 locale. If we succeed, launch the + * user’s shell as usual, but add a user-notification saying + * the locale has been changed. + */ + if (setlocale(LC_CTYPE, "C.UTF-8") != NULL) { + user_notification_add( + &user_notifications, USER_NOTIFICATION_WARNING, + xstrdup("locale forcibly changed to C.UTF-8")); + bad_locale = false; + } + + else if (setlocale(LC_CTYPE, "en_US.UTF-8") != NULL) { + user_notification_add( + &user_notifications, USER_NOTIFICATION_WARNING, + xstrdup("locale forcibly changed to en_US.UTF-8")); + bad_locale = false; + } } struct config conf = {NULL}; From 820b15b844b32850a9274032de0ca30ce70703cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 11 Jan 2022 21:37:41 +0100 Subject: [PATCH 3/5] changelog: improved handling of incompatible locales --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85906524..07205da0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,7 @@ * [SGR-Pixels (1016) mouse extended coordinates](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Extended-coordinates) is now supported (https://codeberg.org/dnkl/foot/issues/762). + ### Changed * PaperColorDark and PaperColorLight themes renamed to @@ -58,6 +59,10 @@ * The width of the block cursor’s outline in an unfocused window is now scaled by the output scaling factor (“desktop scaling”). Previously, it was always 1px. +* Foot will now try to change the locale to either “C.UTF-8” or + “en_US.UTF-8” if started with a non-UTF8 locale. If this fails, foot + will start, but only to display a window with an error (user’s shell + is not executed). ### Deprecated From 2bc77ebf096a7b96b5bd4553b83af42fe4084440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Wed, 12 Jan 2022 15:53:26 +0100 Subject: [PATCH 4/5] main: display warning only, when we succeed in enabling a fallback locale --- main.c | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/main.c b/main.c index b90c4b25..eb7a5c83 100644 --- a/main.c +++ b/main.c @@ -434,26 +434,35 @@ main(int argc, char *const *argv) if (check_config) return ret; - user_notification_add_fmt(&user_notifications, USER_NOTIFICATION_ERROR, - "locale '%s' is not UTF-8", locale); + static const char fallback_locales[][12] = { + "C.UTF-8", + "en_US.UTF-8", + }; /* * Try to force an UTF-8 locale. If we succeed, launch the * user’s shell as usual, but add a user-notification saying * the locale has been changed. */ - if (setlocale(LC_CTYPE, "C.UTF-8") != NULL) { - user_notification_add( - &user_notifications, USER_NOTIFICATION_WARNING, - xstrdup("locale forcibly changed to C.UTF-8")); - bad_locale = false; + for (size_t i = 0; i < ALEN(fallback_locales); i++) { + const char *const fallback_locale = fallback_locales[i]; + + if (setlocale(LC_CTYPE, fallback_locale) != NULL) { + user_notification_add_fmt( + &user_notifications, USER_NOTIFICATION_WARNING, + "locale '%s' is not UTF-8, using '%s' instead", + locale, fallback_locale); + bad_locale = false; + break; + } } - else if (setlocale(LC_CTYPE, "en_US.UTF-8") != NULL) { - user_notification_add( - &user_notifications, USER_NOTIFICATION_WARNING, - xstrdup("locale forcibly changed to en_US.UTF-8")); - bad_locale = false; + if (bad_locale) { + user_notification_add_fmt( + &user_notifications, USER_NOTIFICATION_ERROR, + "locale '%s' is not UTF-8, " + "and failed to enable a fallback locale", + locale); } } From d85feb02ed5d16fe55c7573bb4006b414b97d188 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Wed, 12 Jan 2022 18:59:15 +0100 Subject: [PATCH 5/5] main: log locale errors (in addition to adding a user notification) --- main.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/main.c b/main.c index eb7a5c83..7d57c641 100644 --- a/main.c +++ b/main.c @@ -429,11 +429,6 @@ main(int argc, char *const *argv) bool bad_locale = !locale_is_utf8(); if (bad_locale) { - LOG_ERR("locale '%s' is not UTF-8", locale); - - if (check_config) - return ret; - static const char fallback_locales[][12] = { "C.UTF-8", "en_US.UTF-8", @@ -448,16 +443,23 @@ main(int argc, char *const *argv) const char *const fallback_locale = fallback_locales[i]; if (setlocale(LC_CTYPE, fallback_locale) != NULL) { + LOG_WARN("locale '%s' is not UTF-8, using '%s' instead", + locale, fallback_locale); + user_notification_add_fmt( &user_notifications, USER_NOTIFICATION_WARNING, "locale '%s' is not UTF-8, using '%s' instead", locale, fallback_locale); + bad_locale = false; break; } } if (bad_locale) { + LOG_ERR("locale '%s' is not UTF-8, " + "and failed to enable a fallback locale", locale); + user_notification_add_fmt( &user_notifications, USER_NOTIFICATION_ERROR, "locale '%s' is not UTF-8, "