Commit f7400c92 authored by Yuanle Song's avatar Yuanle Song
Browse files

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.
parent cfe2e72b
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -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);
+1 −1
Original line number Diff line number Diff line
# -*- 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: [
+78 −1
Original line number Diff line number Diff line
* 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
+4 −0
Original line number Diff line number Diff line
@@ -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);