diff --git a/common/init.c b/common/init.c index 199b4c55..cc868646 100644 --- a/common/init.c +++ b/common/init.c @@ -5,6 +5,8 @@ #include "ui.h" #include "uipriv.h" +// TODO rename this file to main.c + static bool initialized = false; bool uiInit(void *options, uiInitError *err) @@ -47,6 +49,15 @@ bool uiprivInitReturnErrorf(uiInitError *err, const char *msg, ...) return false; } +void uiQueueMain(void (*f)(void *data), void *data) +{ + if (!initialized) { + uiprivProgrammerError(uiprivProgrammerErrorNotInitialized, uiprivFunc); + return; + } + uiprivSysQueueMain(f, data); +} + bool uiprivCheckInitializedAndThreadImpl(const char *func) { // While it does seem risky to not lock this, if this changes during the execution of this function it means only that it was changed from a different thread, and since it can only change from false to true, an error will be reported anyway. diff --git a/common/uipriv.h b/common/uipriv.h index 48f02574..0b84a74a 100644 --- a/common/uipriv.h +++ b/common/uipriv.h @@ -22,6 +22,7 @@ extern "C" { // init.c extern bool uiprivSysInit(void *options, uiInitError *err); extern bool uiprivInitReturnErrorf(uiInitError *err, const char *msg, ...); +extern void uiprivSysQueueMain(void (*f)(void *data), void *data); extern bool uiprivCheckInitializedAndThreadImpl(const char *func); #define uiprivCheckInitializedAndThread() uiprivCheckInitializedAndThreadImpl(uiprivFunc) extern bool uiprivSysCheckThread(void); diff --git a/darwin/main.m b/darwin/main.m index 78e01c57..c1d9b6cb 100644 --- a/darwin/main.m +++ b/darwin/main.m @@ -37,12 +37,12 @@ static uiprivApplicationDelegate *uiprivAppDelegate; @end static pthread_t mainThread; -static BOOL initialized = NO; // TODO deduplicate this from common/init.c bool uiprivSysInit(void *options, uiInitError *err) { uiprivApp = [uiprivApplication sharedApplication]; if (![NSApp isKindOfClass:[uiprivApplication class]]) + // TODO verify if Info.plist (both in a proper .app bundle and embedded into a standalone binary) can override this and, if so, add that tidbit to this message return uiprivInitReturnErrorf(err, "NSApp is not of type uiprivApplication; was likely already initialized beforehand"); // don't check for a NO return; something (launch services?) causes running from application bundles to always return NO when asking to change activation policy, even if the change is to the same activation policy! @@ -53,7 +53,6 @@ bool uiprivSysInit(void *options, uiInitError *err) [uiprivApp setDelegate:uiprivAppDelegate]; mainThread = pthread_self(); - initialized = YES; return true; } @@ -88,12 +87,8 @@ void uiQuit(void) } // thanks to mikeash in irc.freenode.net/#macdev for suggesting the use of Grand Central Dispatch for this -void uiQueueMain(void (*f)(void *data), void *data) +void uiprivSysQueueMain(void (*f)(void *data), void *data) { - if (!initialized) { - uiprivProgrammerError(uiprivProgrammerErrorNotInitialized, uiprivFunc); - return; - } // dispatch_get_main_queue() is a serial queue so it will not execute multiple uiQueueMain() functions concurrently // the signature of f matches dispatch_function_t dispatch_async_f(dispatch_get_main_queue(), data, f); diff --git a/unix/main.c b/unix/main.c index 6904ef52..02099917 100644 --- a/unix/main.c +++ b/unix/main.c @@ -2,7 +2,6 @@ #include "uipriv_unix.h" static pthread_t mainThread; -static gboolean initialized = FALSE; // TODO deduplicate this from common/init.c bool uiprivSysInit(void *options, uiInitError *err) { @@ -14,7 +13,6 @@ bool uiprivSysInit(void *options, uiInitError *err) return false; } mainThread = pthread_self(); - initialized = TRUE; return true; } @@ -46,14 +44,10 @@ static gboolean doqueued(gpointer data) return FALSE; } -void uiQueueMain(void (*f)(void *data), void *data) +void uiprivSysQueueMain(void (*f)(void *data), void *data) { struct queued *q; - if (!initialized) { - uiprivProgrammerError(uiprivProgrammerErrorNotInitialized, uiprivFunc); - return; - } q = g_new0(struct queued, 1); q->f = f; q->data = data; diff --git a/windows/main.cpp b/windows/main.cpp index 3f2f4acf..8fd276ad 100644 --- a/windows/main.cpp +++ b/windows/main.cpp @@ -45,7 +45,6 @@ static inline void setHInstance(void) #define uiprivInitReturnHRESULT(err, msg, hr) uiprivInitReturnErrorf(err, "%s: 0x%08I32X", msg, hr) static DWORD mainThread; -static BOOL initialized = FALSE; // TODO deduplicate this from common/init.c bool uiprivSysInit(void *options, uiInitError *err) { @@ -92,7 +91,6 @@ bool uiprivSysInit(void *options, uiInitError *err) // LONGTERM turn off COM exception handling */ mainThread = GetCurrentThreadId(); - initialized = TRUE; return true; } @@ -124,14 +122,10 @@ void uiQuit(void) PostQuitMessage(0); } -void uiQueueMain(void (*f)(void *data), void *data) +void uiprivSysQueueMain(void (*f)(void *data), void *data) { HRESULT hr; - if (!initialized) { - uiprivProgrammerError(uiprivProgrammerErrorNotInitialized, uiprivFunc); - return; - } hr = uiprivHrPostMessageW(uiprivUtilWindow, uiprivUtilWindowMsgQueueMain, (WPARAM) f, (LPARAM) data); if (hr != S_OK) { // TODO handle error