move startup/exit func stuff to common

Sun, 07 Dec 2025 11:05:22 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 07 Dec 2025 11:05:22 +0100
changeset 956
4cdfbfd2038d
parent 955
ea9a999b4fc8
child 957
774ee4d7bb1f

move startup/exit func stuff to common

ui/common/app.c file | annotate | diff | comparison | revisions
ui/common/app.h file | annotate | diff | comparison | revisions
ui/common/objs.mk file | annotate | diff | comparison | revisions
ui/gtk/toolkit.c file | annotate | diff | comparison | revisions
ui/motif/toolkit.c file | annotate | diff | comparison | revisions
ui/qt/toolkit.cpp file | annotate | diff | comparison | revisions
ui/server/toolkit.c file | annotate | diff | comparison | revisions
ui/win32/toolkit.c file | annotate | diff | comparison | revisions
ui/winui/toolkit.cpp file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/common/app.c	Sun Dec 07 11:05:22 2025 +0100
@@ -0,0 +1,71 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2025 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "app.h"
+
+static ui_callback   startup_func;
+static void          *startup_data;
+static ui_callback   open_func;
+void                 *open_data;
+static ui_callback   exit_func;
+void                 *exit_data;
+
+
+void ui_onstartup(ui_callback f, void *userdata) {
+    startup_func = f;
+    startup_data = userdata;
+}
+
+void ui_onopen(ui_callback f, void *userdata) {
+    open_func = f;
+    open_data = userdata;
+}
+
+void ui_onexit(ui_callback f, void *userdata) {
+    exit_func = f;
+    exit_data = userdata;
+}
+
+
+void uic_application_startup(UiEvent *event) {
+    if(startup_func) {
+        startup_func(event, startup_data);
+    }
+}
+
+void uic_application_open(UiEvent *event) {
+    if(open_func) {
+        open_func(event, open_data);
+    }
+}
+
+void uic_application_exit(UiEvent *event) {
+    if(exit_func) {
+        exit_func(event, exit_data);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/common/app.h	Sun Dec 07 11:05:22 2025 +0100
@@ -0,0 +1,48 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2025 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef UIC_APP_H
+#define UIC_APP_H
+
+#include "../ui/toolkit.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void uic_application_startup(UiEvent *event);
+void uic_application_open(UiEvent *event);
+void uic_application_exit(UiEvent *event);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* UIC_APP_H */
+
--- a/ui/common/objs.mk	Sun Dec 07 10:51:47 2025 +0100
+++ b/ui/common/objs.mk	Sun Dec 07 11:05:22 2025 +0100
@@ -34,6 +34,7 @@
 COMMON_OBJ += object$(OBJ_EXT)
 COMMON_OBJ += container$(OBJ_EXT)
 COMMON_OBJ += types$(OBJ_EXT)
+COMMON_OBJ += app$(OBJ_EXT)
 COMMON_OBJ += properties$(OBJ_EXT)
 COMMON_OBJ += menu$(OBJ_EXT)
 COMMON_OBJ += toolbar$(OBJ_EXT)
--- a/ui/gtk/toolkit.c	Sun Dec 07 10:51:47 2025 +0100
+++ b/ui/gtk/toolkit.c	Sun Dec 07 11:05:22 2025 +0100
@@ -39,6 +39,7 @@
 #include "../common/menu.h"
 #include "../common/toolbar.h"
 #include "../common/threadpool.h"
+#include "../common/app.h"
 
 #include <cx/string.h>
 #include <cx/printf.h>
@@ -51,13 +52,6 @@
 
 static const char *application_name;
 
-static ui_callback   startup_func;
-static void          *startup_data;
-static ui_callback   open_func;
-void                 *open_data;
-static ui_callback   exit_func;
-void                 *exit_data;
-
 static ui_callback   appclose_fnc;
 static void          *appclose_udata;
 
@@ -95,30 +89,13 @@
     return application_name;
 }
 
-void ui_onstartup(ui_callback f, void *userdata) {
-    startup_func = f;
-    startup_data = userdata;
-}
-
-void ui_onopen(ui_callback f, void *userdata) {
-    open_func = f;
-    open_data = userdata;
-}
-
-void ui_onexit(ui_callback f, void *userdata) {
-    exit_func = f;
-    exit_data = userdata;
-}
-
 void ui_app_exit_on_shutdown(UiBool exitapp) {
     exit_on_shutdown = exitapp;
 }
 
 #ifdef UI_APPLICATION
 static void app_startup(GtkApplication* app, gpointer userdata) {
-    if(startup_func) {
-        startup_func(NULL, startup_data);
-    }
+    uic_application_startup(NULL);
 }
 
 static void app_activate(GtkApplication* app, gpointer userdata) {
@@ -126,9 +103,7 @@
 }
 
 static void app_shutdown(GtkApplication *app, gpointer userdata) {
-    if(exit_func) {
-        exit_func(NULL, exit_data);
-    }
+    uic_application_exit(NULL);
     ui_app_save_settings();
 }
 
@@ -148,13 +123,9 @@
     
     free(appid.ptr);
 #else
-    if(startup_func) {
-        startup_func(NULL, startup_data);
-    }
+    uic_application_startup(NULL);
     gtk_main();
-    if(exit_func) {
-        exit_func(NULL, exit_data);
-    }
+    uic_application_exit(NULL);
     ui_app_save_settings();
 #endif
     if(exit_on_shutdown) {
--- a/ui/motif/toolkit.c	Sun Dec 07 10:51:47 2025 +0100
+++ b/ui/motif/toolkit.c	Sun Dec 07 11:05:22 2025 +0100
@@ -39,6 +39,7 @@
 #include "../common/toolbar.h"
 #include "../common/document.h"
 #include "../common/properties.h"
+#include "../common/app.h"
 #include <cx/buffer.h>
 
 #include <X11/Intrinsic.h>
@@ -49,13 +50,6 @@
 static Widget active_window;
 static const char *application_name;
 
-static ui_callback   startup_func;
-static void          *startup_data;
-static ui_callback   open_func;
-static void          *open_data;
-static ui_callback   exit_func;
-static void          *exit_data;
-
 static ui_callback appclose_fnc;
 static void *appclose_udata;
 
@@ -134,33 +128,14 @@
     return display;
 }
 
-void ui_onstartup(ui_callback f, void *userdata) {
-    startup_func = f;
-    startup_data = userdata;
-}
-
-void ui_onopen(ui_callback f, void *userdata) {
-    open_func = f;
-    open_data = userdata;
-}
-
-void ui_onexit(ui_callback f, void *userdata) {
-    exit_func = f;
-    exit_data = userdata;
-}
-
 void ui_app_exit_on_shutdown(UiBool exitapp) {
     exit_on_shutdown = exitapp;
 }
 
 void ui_main() {
-    if(startup_func) {
-        startup_func(NULL, startup_data);
-    }
+    uic_application_startup(NULL);
     XtAppMainLoop(app);
-    if(exit_func) {
-        exit_func(NULL, exit_data);
-    }
+    uic_application_exit(NULL);
     uic_store_app_properties();
     if(exit_on_shutdown) {
         exit(0);
--- a/ui/qt/toolkit.cpp	Sun Dec 07 10:51:47 2025 +0100
+++ b/ui/qt/toolkit.cpp	Sun Dec 07 11:05:22 2025 +0100
@@ -37,16 +37,10 @@
 #include "../common/properties.h"
 #include "../common/menu.h"
 #include "../common/toolbar.h"
+#include "../common/app.h"
 
 static const char *application_name;
 
-static ui_callback   startup_func;
-static void          *startup_data;
-static ui_callback   open_func;
-static void          *open_data;
-static ui_callback   exit_func;
-static void          *exit_data;
-
 static int is_toplevel_realized = 0;
 
 static int app_argc;
@@ -73,33 +67,14 @@
     return application_name;
 }
 
-void ui_onstartup(ui_callback f, void *userdata) {
-    startup_func = f;
-    startup_data = userdata;
-}
-
-void ui_onopen(ui_callback f, void *userdata) {
-    open_func = f;
-    open_data = userdata;
-}
-
-void ui_onexit(ui_callback f, void *userdata) {
-    exit_func = f;
-    exit_data = userdata;
-}
-
 void ui_app_exit_on_shutdown(UiBool exitapp) {
     exit_on_shutdown = exitapp;
 }
 
 void ui_main() {
-    if(startup_func) {
-        startup_func(NULL, startup_data);
-    }
+    uic_application_startup(NULL);
     application->exec();
-    if(exit_func) {
-        exit_func(NULL, exit_data);
-    }
+    uic_application_exit(NULL);
     uic_store_app_properties();
     
     delete application;
--- a/ui/server/toolkit.c	Sun Dec 07 10:51:47 2025 +0100
+++ b/ui/server/toolkit.c	Sun Dec 07 11:05:22 2025 +0100
@@ -35,6 +35,7 @@
 
 #include "../common/message.h"
 #include "../common/threadpool.h"
+#include "../common/app.h"
 
 static const char *ui_app_name;
 
@@ -55,11 +56,6 @@
     return ui_app_name;
 }
 
-void ui_onstartup(ui_callback f, void *userdata) {
-    onstartup = f;
-    onstartupdata = userdata;
-}
-
 void ui_add_styledata(const char *styledata, int len) {
     // NOOP
 }
--- a/ui/win32/toolkit.c	Sun Dec 07 10:51:47 2025 +0100
+++ b/ui/win32/toolkit.c	Sun Dec 07 11:05:22 2025 +0100
@@ -35,6 +35,7 @@
 #include "../common/toolbar.h"
 #include "../common/document.h"
 #include "../common/properties.h"
+#include "../common/app.h"
 
 #include "../ui/widget.h"
 
@@ -45,13 +46,6 @@
 
 static const char *application_name;
 
-static ui_callback   startup_func;
-static void          *startup_data;
-static ui_callback   open_func;
-void                 *open_data;
-static ui_callback   exit_func;
-void                 *exit_data;
-
 static HFONT ui_font = NULL;
 
 void ui_init(const char *appname, int argc, char **argv) {
@@ -91,25 +85,8 @@
     return application_name;
 }
 
-void ui_onstartup(ui_callback f, void *userdata) {
-    startup_func = f;
-    startup_data = userdata;
-}
-
-void ui_onopen(ui_callback f, void *userdata) {
-    open_func = f;
-    open_data = userdata;
-}
-
-void ui_onexit(ui_callback f, void *userdata) {
-    exit_func = f;
-    exit_data = userdata;
-}
-
 void ui_main() {
-    if(startup_func) {
-        startup_func(NULL, startup_data);
-    }
+    uic_application_startup(NULL);
 
     // event loop
     MSG msg;
@@ -118,9 +95,7 @@
         DispatchMessage(&msg);
     }
 
-    if(exit_func) {
-        exit_func(NULL, exit_data);
-    }
+    uic_application_exit(NULL);
     uic_store_app_properties();
 }
 
--- a/ui/winui/toolkit.cpp	Sun Dec 07 10:51:47 2025 +0100
+++ b/ui/winui/toolkit.cpp	Sun Dec 07 11:05:22 2025 +0100
@@ -37,6 +37,7 @@
 #include "../common/document.h"
 #include "../common/toolbar.h"
 #include "../common/properties.h"
+#include "../common/app.h"
 
 #include "icons.h"
 
@@ -55,15 +56,6 @@
 
 static const char* application_name;
 
-static ui_callback   startup_func;
-static void* startup_data;
-
-static ui_callback   open_func;
-void* open_data;
-
-static ui_callback   exit_func;
-void* exit_data;
-
 static ui_callback   appclose_fnc;
 
 static void* appclose_udata;
@@ -75,19 +67,14 @@
 
 void ui_app_run_startup() {
 	uiDispatcherQueue = winrt::Microsoft::UI::Dispatching::DispatcherQueue::GetForCurrentThread();
-	
-	if (startup_func) {
-		startup_func(NULL, startup_data);
-	}
+	uic_application_startup(NULL);
 }
 
 class App : public ApplicationT<App, IXamlMetadataProvider> {
 public:
 	void OnLaunched(LaunchActivatedEventArgs const&) {
 		Resources().MergedDictionaries().Append(XamlControlsResources());
-		if (startup_func) {
-			startup_func(NULL, startup_data);
-		}
+		uic_application_startup(NULL);
 
 		//auto window = make<winui::implementation::MainWindow>();
 		//window.Activate();
@@ -175,21 +162,6 @@
 	return application_name;
 }
 
-void ui_onstartup(ui_callback f, void* userdata) {
-	startup_func = f;
-	startup_data = userdata;
-}
-
-void ui_onopen(ui_callback f, void* userdata) {
-	open_func = f;
-	open_data = userdata;
-}
-
-void ui_onexit(ui_callback f, void* userdata) {
-	exit_func = f;
-	exit_data = userdata;
-}
-
 void ui_main() {
 	/*
 	init_apartment();

mercurial