From f7400c92134c06fd931d53148a9a9337772d5d08 Mon Sep 17 00:00:00 2001
From: Yuanle Song <sylecn@gmail.com>
Date: Wed, 16 Aug 2023 14:13:11 +0800
Subject: [PATCH] v1.3.3 add setlocale() call to set locale properly

when it is not called, default locale is C, which will result in g_message,
g_print etc print garbage (???) on utf-8 string.
---
 client-demo.c |  3 ++
 meson.build   |  2 +-
 operational   | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 server.c      |  4 +++
 4 files changed, 86 insertions(+), 2 deletions(-)

diff --git a/client-demo.c b/client-demo.c
index 2cab911..1544425 100644
--- a/client-demo.c
+++ b/client-demo.c
@@ -12,6 +12,7 @@
  * limitations under the License.
  */
 #include <gio/gio.h>
+#include <locale.h>
 #include <glib.h>
 #ifdef G_OS_UNIX
 #include <glib-unix.h>
@@ -101,6 +102,7 @@ startup(GApplication *app,
 	/* show panel, wait 3s, then hide panel and exit app */
 	err = NULL;
 	const gchar *const candidates[] = {"第一个", "第二个", "第三个"};
+	/* g_message("first: %s", candidates[0]); */
 	GVariant *hints = NULL;
 	GVariantBuilder b = {0};
 	g_variant_builder_init(&b, G_VARIANT_TYPE("a{sv}"));
@@ -147,6 +149,7 @@ main(int argc, char *argv[])
 	GApplication *app = NULL;
 	int status = 0;
 
+	setlocale(LC_ALL, "");
 	app = g_application_new("com.emacsos.zero.ZeroPanelClientDemo",
 				G_APPLICATION_FLAGS_NONE);
 	g_assert_nonnull(app);
diff --git a/meson.build b/meson.build
index bdb40f1..3f47a64 100644
--- a/meson.build
+++ b/meson.build
@@ -1,6 +1,6 @@
 # -*- mode: conf -*-
 project('zero-panel', 'c',
-  version: '1.3.2',
+  version: '1.3.3',
   license: 'EPL-2.0',
   meson_version: '>=0.46.0',
   default_options: [
diff --git a/operational b/operational
index a6d69d9..614c9cc 100644
--- a/operational
+++ b/operational
@@ -1,6 +1,6 @@
 * COMMENT -*- mode: org -*-
 #+Date: 2019-09-02
-Time-stamp: <2023-08-15>
+Time-stamp: <2023-08-16>
 #+STARTUP: content
 * notes                                                               :entry:
 ** 2023-08-15 how to build from source?                                 :doc:
@@ -208,9 +208,86 @@ I already set this in ui file:
 They don't work properly in Wayland.
 
 - see minimal test application at ~/c/testing-only/do-not-focus/main.c
+- 2023-08-16 search: how to mark a window as not focus on gtk window on wayland?
+  search: does gtk3 support wayland properly?
 
 * current                                                             :entry:
 ** 
+** 2023-08-16 bug: ./build/zero-panel-client-demo segfault on RHEL9.
+- it runs fine on lat21. but has a few warnings. fix the warnings.
+  #+BEGIN_SRC sh
+    sylecn@lat21:~/projects/zero-panel$ ./build/zero-panel-client-demo
+
+    (zero-panel-client-demo:56547): GLib-CRITICAL **: 12:11:37.488: g_variant_new_string: assertion 'g_utf8_validate (string, -1, NULL)' failed
+
+    (zero-panel-client-demo:56547): GLib-CRITICAL **: 12:11:37.488: g_variant_ref_sink: assertion 'value != NULL' failed
+
+    (zero-panel-client-demo:56547): GLib-CRITICAL **: 12:11:37.488: g_variant_ref: assertion 'value != NULL' failed
+
+    (zero-panel-client-demo:56547): GLib-CRITICAL **: 12:11:37.490: g_variant_unref: assertion 'value != NULL' failed
+    ,** Message: 12:11:37.490: call ShowCandidates() okay
+    ,** Message: 12:11:37.490: activate()
+    ,** Message: 12:11:41.111: call Hide() okay
+    ,** Message: 12:11:41.111: shutdown()
+  #+END_SRC
+- search: GLib-CRITICAL assert how to debug get source code line?
+
+  rm -rf build/
+  meson setup --buildtype debug build/
+  cd build && ninja
+  ./build/zero-panel-client-demo
+  the warning is gone when I use debug build.
+
+  env G_DEBUG=fatal-criticals ./build/zero-panel-client-demo
+
+- I think the Chinese char* literal is most suspicious.
+  try use ascii string there.
+  yes. debugoptimized build warning is gone when only use ascii string.
+
+  search: how to use unicode utf-8 string literal in gtk gchar
+
+  all char* is assumed to be utf-8. it is supported out of the box.
+
+- I got a minimal reproducible demo.
+  #+BEGIN_SRC c
+    const gchar* first = "第一个";
+    g_assert_true(g_utf8_validate(first, -1, NULL));
+    const gchar* second = "第二个";
+    const gchar* third = "第三个";
+
+    const gchar *const candidates[] = {first, second, third};
+    g_message("first is: %s", first);
+    g_message("first is: %s", candidates[0]);
+  #+END_SRC
+  the g_message() call output garbage.
+
+  add setlocale(LC_ALL, "en_US.UTF-8"); worked.
+  default locale must be C.    // confirmed. default locale is C.
+
+- TODO if default locale is C, always set locale for all my glib programs.
+
+  #include <locale.h>
+
+  setlocale(LC_ALL, "en_US.UTF-8");
+
+  is there a method call to use system locale instead of "C" locale?
+  can I assume en_US.UTF-8 exist? I don't think so.
+  any UTF-8 will do.
+
+  https://docs.gtk.org/glib/running.html#locale
+
+  A number of interfaces in GLib depend on the current locale in which an
+  application is running. Therefore, most GLib-using applications should call
+  setlocale (LC_ALL, "") to set up the current locale.
+
+  // good.
+
+- warning still persist after adding setlocale() call.
+
+** 2023-08-16 what's new in gtk4? should I upgrade?
+- Gtk – 4.0: Migrating from GTK 3.x to GTK 4
+  https://docs.gtk.org/gtk4/migrating-3to4.html
+- 
 ** 2019-09-02 support user configuration of theme and panel orientation.
 is it supported by zero-el?
 - 2023-08-01
diff --git a/server.c b/server.c
index 7e58e92..3895ef7 100644
--- a/server.c
+++ b/server.c
@@ -12,6 +12,7 @@
  * limitations under the License.
  */
 #include <stdlib.h>
+#include <locale.h>
 #include <gtk/gtk.h>
 #ifdef G_OS_UNIX
 #include <glib-unix.h>
@@ -651,6 +652,9 @@ main(int argc, char *argv[])
 	GtkApplication *app = NULL;
 	int status = 0;
 
+	// use current system locale instead of C locale.
+	setlocale(LC_ALL, "");
+
 	parse_command_line_args(argc, argv, &appdata);
 	app = gtk_application_new(ZERO_PANEL_GTK_APP_ID,
 				  G_APPLICATION_FLAGS_NONE);
-- 
GitLab