Skip to content

Commit

Permalink
X11App: Add prestartup() method
Browse files Browse the repository at this point in the history
prestartup() is called before initializing X11. Use this in Xmoji to
load the runtime config and check for single instance as early as
possible.
  • Loading branch information
Zirias committed Oct 8, 2024
1 parent 079b681 commit 63d82ed
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
23 changes: 18 additions & 5 deletions src/bin/xmoji/x11app.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/bin/xmoji/x11app.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
}
Expand Down
28 changes: 21 additions & 7 deletions src/bin/xmoji/xmoji.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);

Expand All @@ -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(
Expand Down

0 comments on commit 63d82ed

Please sign in to comment.