From 63d82ed9d9be20022b558a51ee2ec262d1e74eca Mon Sep 17 00:00:00 2001 From: Felix Palmen Date: Tue, 8 Oct 2024 10:17:40 +0200 Subject: [PATCH] X11App: Add prestartup() method prestartup() is called before initializing X11. Use this in Xmoji to load the runtime config and check for single instance as early as possible. --- src/bin/xmoji/x11app.c | 23 ++++++++++++++++++----- src/bin/xmoji/x11app.h | 4 +++- src/bin/xmoji/xmoji.c | 28 +++++++++++++++++++++------- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/bin/xmoji/x11app.c b/src/bin/xmoji/x11app.c index 52c7b66..50fdac6 100644 --- a/src/bin/xmoji/x11app.c +++ b/src/bin/xmoji/x11app.c @@ -21,7 +21,7 @@ static void destroy(void *obj); static X11App *instance; -static MetaX11App mo = MetaX11App_init(0, 0, "X11App", destroy); +static MetaX11App mo = MetaX11App_init(0, 0, 0, "X11App", destroy); typedef struct AppLocale { @@ -58,21 +58,34 @@ struct X11Error static void svprestartup(void *receiver, void *sender, void *args) { - (void)receiver; (void)sender; + X11App *self = receiver; PSC_Service_setTickInterval(0); PSC_EAStartup *ea = args; - PSC_EAStartup_return(ea, X11Adapter_init(instance->argc, instance->argv, + int rc = 0; + Object_vcall(rc, X11App, prestartup, instance); + if (rc == EXIT_FAILURE) PSC_EAStartup_return(ea, EXIT_FAILURE); + else if (rc != 0) self->quitting = 1; + else PSC_EAStartup_return(ea, + X11Adapter_init(instance->argc, instance->argv, instance->locale.lc_ctype, instance->name, - Object_className(instance)) ? EXIT_FAILURE : EXIT_SUCCESS); + Object_className(instance)) + ? EXIT_FAILURE : EXIT_SUCCESS); } static void svstartup(void *receiver, void *sender, void *args) { - (void)receiver; (void)sender; + X11App *self = receiver; + if (self->quitting) + { + self->quitting = 0; + PSC_Service_quit(); + return; + } + #ifndef DEBUG PSC_Log_setAsync(1); #endif diff --git a/src/bin/xmoji/x11app.h b/src/bin/xmoji/x11app.h index 2c63c3e..41e7936 100644 --- a/src/bin/xmoji/x11app.h +++ b/src/bin/xmoji/x11app.h @@ -14,12 +14,14 @@ C_CLASS_DECL(X11App); typedef struct MetaX11App { MetaObject base; + int (*prestartup)(void *app); int (*startup)(void *app); void (*shutdown)(void *app); } MetaX11App; -#define MetaX11App_init(mstartup, mshutdown, ...) { \ +#define MetaX11App_init(mprestartup, mstartup, mshutdown, ...) { \ .base = MetaObject_init(__VA_ARGS__), \ + .prestartup = mprestartup, \ .startup = mstartup, \ .shutdown = mshutdown, \ } diff --git a/src/bin/xmoji/xmoji.c b/src/bin/xmoji/xmoji.c index 6726903..1e49d0b 100644 --- a/src/bin/xmoji/xmoji.c +++ b/src/bin/xmoji/xmoji.c @@ -36,10 +36,12 @@ #define MAXSEARCHRESULTS 100 #define SEARCHRESULTSZ 1024 +static int prestartup(void *app); static int startup(void *app); static void destroy(void *app); -static MetaX11App mo = MetaX11App_init(startup, 0, "Xmoji", destroy); +static MetaX11App mo = MetaX11App_init(prestartup, startup, 0, + "Xmoji", destroy); typedef struct Xmoji { @@ -405,7 +407,7 @@ static void onsearchmodeboxchanged(void *receiver, void *sender, void *args) Config_setEmojiSearchMode(self->config, *val + (*val > 2) + 1); } -static int startup(void *app) +static int prestartup(void *app) { Xmoji *self = Object_instance(app); @@ -428,15 +430,27 @@ static int startup(void *app) PSC_Event_register(SingleInstance_secondary(self->instance), self, onsecondary, 0); if (Config_singleInstance(self->config) - && !SingleInstance_start(self->instance)) return -1; + && !SingleInstance_start(self->instance)) + { + PSC_Log_setMaxLogLevel(PSC_L_INFO); + PSC_Log_msg(PSC_L_INFO, "Found running instance, quitting."); + return -1; + } /* Load translations */ - Translator *tr = Translator_create("xmoji-ui", + self->uitexts = Translator_create("xmoji-ui", X11App_lcMessages(), XMU_get); - self->uitexts = tr; - Translator *etr = Translator_create("xmoji-emojis", + self->emojitexts = Translator_create("xmoji-emojis", X11App_lcMessages(), XME_get); - self->emojitexts = etr; + + return 0; +} + +static int startup(void *app) +{ + Xmoji *self = Object_instance(app); + Translator *tr = self->uitexts; + Translator *etr = self->emojitexts; /* Initialize commands */ Command *aboutCommand = Command_create(