From b1f33ff951050991d4bb80161dda556392b459d4 Mon Sep 17 00:00:00 2001 From: Po Lu Date: Mon, 10 Jan 2022 09:59:17 +0000 Subject: [PATCH] Load X resources from a settings file like other programs on Haiku * doc/emacs/haiku.texi (Haiku Basics): Document how X resources are discovered on Haiku. * src/haiku_support.cc (class Emacs): Load settings file. (be_find_setting): New function. * src/haiku_support.h (be_find_setting): New prototype. * src/haikuterm.c (get_string_resource): Look in the settings file if there's nothing in the in-memory resource database. --- doc/emacs/haiku.texi | 13 +++++++++++++ src/haiku_support.cc | 37 +++++++++++++++++++++++++++++++++++++ src/haiku_support.h | 3 +++ src/haikuterm.c | 5 +++++ 4 files changed, 58 insertions(+) diff --git a/doc/emacs/haiku.texi b/doc/emacs/haiku.texi index 0c76990caed..5fc084d9229 100644 --- a/doc/emacs/haiku.texi +++ b/doc/emacs/haiku.texi @@ -96,6 +96,19 @@ the nil value, and Emacs will use its own implementation of tooltips. the menu bar, so help text in the menu bar will display in the echo area instead. +@cindex X resources on Haiku + Unlike the X window system, Haiku does not have a system-wide +resource database. Since many important options are are specified via +X resources (@pxref{X Resources}), an emulation is provided: upon +startup, Emacs will load a file named @file{GNU Emacs} inside the user +configuration directory (normally @file{/boot/home/config/settings}), +which should be a flattened system message where keys and values are +both strings, and correspond to attributes and their values +respectively. + +You can create such a file with the tool @code{xmlbmessage}, which is +free software. + @subsection What to do when Emacs crashes @cindex crashes, Haiku @cindex haiku debugger diff --git a/src/haiku_support.cc b/src/haiku_support.cc index 459f8c2be40..531dfb5c642 100644 --- a/src/haiku_support.cc +++ b/src/haiku_support.cc @@ -291,8 +291,25 @@ map_normal (uint32_t kc, uint32_t *ch) class Emacs : public BApplication { public: + BMessage settings; + bool settings_valid_p = false; + Emacs () : BApplication ("application/x-vnd.GNU-emacs") { + BPath settings_path; + + if (find_directory (B_USER_SETTINGS_DIRECTORY, &settings_path) != B_OK) + return; + + settings_path.Append (PACKAGE_NAME); + + BEntry entry (settings_path.Path ()); + BFile settings_file (&entry, B_READ_ONLY | B_CREATE_FILE); + + if (settings.Unflatten (&settings_file) != B_OK) + return; + + settings_valid_p = true; } void @@ -3131,3 +3148,23 @@ BWindow_set_override_redirect (void *window, bool override_redirect_p) w->UnlockLooper (); } } + +/* Find a resource by the name NAME inside the settings file. The + string returned is in UTF-8 encoding, and will stay allocated as + long as the BApplication (a.k.a display) is alive. */ +const char * +be_find_setting (const char *name) +{ + Emacs *app = (Emacs *) be_app; + const char *value; + + /* Note that this is thread-safe since the constructor of `Emacs' + runs in the main thread. */ + if (!app->settings_valid_p) + return NULL; + + if (app->settings.FindString (name, 0, &value) != B_OK) + return NULL; + + return value; +} diff --git a/src/haiku_support.h b/src/haiku_support.h index d39e30735d3..83f22972ce2 100644 --- a/src/haiku_support.h +++ b/src/haiku_support.h @@ -855,6 +855,9 @@ extern "C" extern void BWindow_set_override_redirect (void *window, bool override_redirect_p); + extern const char * + be_find_setting (const char *name); + #ifdef __cplusplus extern void * find_appropriate_view_for_draw (void *vw); diff --git a/src/haikuterm.c b/src/haikuterm.c index b2337fe1047..b6d105bf2b6 100644 --- a/src/haikuterm.c +++ b/src/haikuterm.c @@ -107,6 +107,8 @@ haiku_delete_terminal (struct terminal *terminal) static const char * get_string_resource (void *ignored, const char *name, const char *class) { + const char *native; + if (!name) return NULL; @@ -115,6 +117,9 @@ get_string_resource (void *ignored, const char *name, const char *class) if (!NILP (lval)) return SSDATA (XCDR (lval)); + if ((native = be_find_setting (name))) + return native; + return NULL; } -- 2.39.2