From 0e2ac66f4b47fc51421ef7911cf15e22b07b0342 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Wed, 29 Oct 2025 20:33:43 +0100 Subject: [PATCH] move some initialization code to application.c --- .gitignore | 1 + application/Makefile | 1 + application/application.c | 93 +++++++++++++++++++++++++++++++++++++++ application/application.h | 61 +++++++++++++++++++++++++ application/main.c | 81 ++++++++-------------------------- application/main.h | 3 -- application/player.c | 6 +-- application/window.h | 14 +++--- 8 files changed, 185 insertions(+), 75 deletions(-) create mode 100644 application/application.c create mode 100644 application/application.h diff --git a/.gitignore b/.gitignore index 5a5d528..478ee0c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build config.mk +gmon.out diff --git a/application/Makefile b/application/Makefile index 011717c..6569066 100644 --- a/application/Makefile +++ b/application/Makefile @@ -32,6 +32,7 @@ include $(BUILD_ROOT)/config.mk CFLAGS += -I../ui/ -I../ucx -I.. SRC = main.c +SRC += application.c SRC += Fsb.c SRC += window.c SRC += player.c diff --git a/application/application.c b/application/application.c new file mode 100644 index 0000000..34b9ceb --- /dev/null +++ b/application/application.c @@ -0,0 +1,93 @@ +/* + * Copyright 2025 Olaf Wintermann + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + + +#include + +#include "application.h" +#include "window.h" +#include "player.h" +#include "playlist.h" + +XtAppContext ui_motif_get_app(void); +Display* ui_motif_get_display(void); + +static Widget toplevel_window; + +static Window app_player_window = 0; + + +void AppInitMenu(void) { + // TODO +} + +void AppStart(UiEvent *event, AppStartupSettings *settings) { + AppInitMenu(); + + Display *display = ui_motif_get_display(); + MainWindow *window = WindowCreate(display); + toplevel_window = window->window; + if(settings->disable_adjust_window_size) { + window->adjustWindowSize = FALSE; + XtVaSetValues(window->viewAdjustWindowSize, XmNset, FALSE, NULL); + } + + // random numbers used for creating tmp dirs and for random playback + srand(time(NULL)); + + if(settings->playback != 0) { + PlayListSetMode(window, settings->playback); + } + + if(settings->single_instance) { + // this will trigger the Single Instance menu button event handler + // and enable the single instance mode if possible + XmToggleButtonGadgetSetState(window->prefSingleInstanceButton, True, True); + } + + WindowShow(window); + +#ifdef UI_MOTIF + XtAppContext app = ui_motif_get_app(); + AppMainLoop(app); +#endif +} + +/* + * Extended Xt main loop, that also handles external window events + */ +void AppMainLoop(XtAppContext app) { + while(!XtAppGetExitFlag(app)) { + XEvent event; + XtAppNextEvent(app, &event); + + if(app_player_window != 0 && event.xany.window == app_player_window) { + WindowHandlePlayerEvent(GetMainWindow(), &event); + } else { + XtDispatchEvent(&event); + } + } +} + +void AppSetPlayerWindow(Window w) { + app_player_window = w; +} diff --git a/application/application.h b/application/application.h new file mode 100644 index 0000000..6f17ee1 --- /dev/null +++ b/application/application.h @@ -0,0 +1,61 @@ +/* + * Copyright 2025 Olaf Wintermann + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#ifndef UWP_APPLICATION_H +#define UWP_APPLICATION_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum PlaybackMode { + PLAYBACK_STOP = 0, + PLAYBACK_REPEAT, + PLAYBACK_REPEAT_LIST, + PLAYBACK_RANDOM, + PLAYBACK_AUTOPLAY +} PlaybackMode; + +typedef struct AppStartupSettings { + PlaybackMode playback; + bool fullscreen; + bool disable_adjust_window_size; + bool single_instance; +} AppStartupSettings; + + +void AppSetPlayerWindow(Window w); + +void AppInitMenu(void); + +void AppStart(UiEvent *event, AppStartupSettings *settings); + +void AppMainLoop(XtAppContext app); + +#ifdef __cplusplus +} +#endif + +#endif /* UWP_APPLICATION_H */ + diff --git a/application/main.c b/application/main.c index 163e557..3bc6101 100644 --- a/application/main.c +++ b/application/main.c @@ -29,6 +29,7 @@ #include #include +#include "application.h" #include "window.h" #include "main.h" #include "playlist.h" @@ -41,7 +42,9 @@ static XtAppContext app; static Display *display; -static Widget toplevel_window; + +XtAppContext ui_motif_get_app(void); +Display* ui_motif_get_display(void); static CxList *open_file_arg; @@ -89,8 +92,7 @@ static void input_proc(XtPointer data, int *source, XtInputId *iid) { } } -XtAppContext ui_motif_get_app(void); -Display* ui_motif_get_display(void); + int main(int argc, char** argv) { // disable stdout buffering, because the netbeans's internal terminal @@ -115,10 +117,12 @@ int main(int argc, char** argv) { //display = XtOpenDisplay(app, NULL, APP_NAME, APP_CLASS, NULL, 0, &argc, argv); display = ui_motif_get_display(); - PlaybackMode playback = PLAYBACK_STOP; - bool fullscreen = FALSE; - bool disable_adjust_window_size = FALSE; - bool single_instance = FALSE; + AppStartupSettings settings = { + .playback = PLAYBACK_STOP, + .fullscreen = FALSE, + .disable_adjust_window_size = FALSE, + .single_instance = FALSE + }; bool file_args = 0; if(argc > 1) { @@ -142,37 +146,37 @@ int main(int argc, char** argv) { switch(arg[a]) { case 't': { // repeat (Track) - playback = PLAYBACK_REPEAT; + settings.playback = PLAYBACK_REPEAT; break; } case 'l': { // repeat List - playback = PLAYBACK_REPEAT_LIST; + settings.playback = PLAYBACK_REPEAT_LIST; break; } case 'r': { // Random playback - playback = PLAYBACK_RANDOM; + settings.playback = PLAYBACK_RANDOM; break; } case 'a': { // Autoplay - playback = PLAYBACK_AUTOPLAY; + settings.playback = PLAYBACK_AUTOPLAY; break; } case 'w': { // disable adjust Window size - disable_adjust_window_size = TRUE; + settings.disable_adjust_window_size = TRUE; break; } case 'f': { // Fullscreen - fullscreen = TRUE; + settings.fullscreen = TRUE; break; } case 's': { // single instance - single_instance = TRUE; + settings.single_instance = TRUE; break; } } @@ -212,34 +216,8 @@ int main(int argc, char** argv) { input_proc, NULL); - MainWindow *window = WindowCreate(display); - toplevel_window = window->window; - if(disable_adjust_window_size) { - window->adjustWindowSize = FALSE; - XtVaSetValues(window->viewAdjustWindowSize, XmNset, FALSE, NULL); - } - - // random numbers used for creating tmp dirs and for random playback - srand(time(NULL)); - - if(playback != 0) { - PlayListSetMode(window, playback); - } - - if(single_instance) { - // this will trigger the Single Instance menu button event handler - // and enable the single instance mode if possible - XmToggleButtonGadgetSetState(window->prefSingleInstanceButton, True, True); - } - - WindowShow(window); - -#ifndef UI_MOTIF + ui_onstartup((ui_callback)AppStart, &settings); ui_main(); -#else - AppMainLoop(app); -#endif - return 0; } @@ -268,24 +246,3 @@ void CleanOpenFileArgs(void) { open_file_arg = NULL; } -static Window app_player_window = 0; - -void SetPlayerWindow(Window w) { - app_player_window = w; -} - -/* - * Extended Xt main loop, that also handles external window events - */ -void AppMainLoop(XtAppContext app) { - while(!XtAppGetExitFlag(app)) { - XEvent event; - XtAppNextEvent(app, &event); - - if(app_player_window != 0 && event.xany.window == app_player_window) { - WindowHandlePlayerEvent(GetMainWindow(), &event); - } else { - XtDispatchEvent(&event); - } - } -} diff --git a/application/main.h b/application/main.h index de79d8a..6d2b642 100644 --- a/application/main.h +++ b/application/main.h @@ -42,9 +42,6 @@ void AppExecProc(XtWorkProc proc, XtPointer data); CxList* GetOpenFileArgs(void); void CleanOpenFileArgs(void); -void SetPlayerWindow(Window w); - -void AppMainLoop(XtAppContext app); #ifdef __cplusplus diff --git a/application/player.c b/application/player.c index 6ff7377..a6a1f46 100644 --- a/application/player.c +++ b/application/player.c @@ -117,7 +117,7 @@ static void* wait_for_process(void *data) { printf("wait_for_process player: %p\n", player); player->isactive = FALSE; player->status = status; - SetPlayerWindow(0); + AppSetPlayerWindow(0); PlayerUnref(player); return NULL; @@ -445,7 +445,7 @@ static Boolean get_player_window(XtPointer data) { p->window = child[0]; XFree(child); - SetPlayerWindow(p->window); + AppSetPlayerWindow(p->window); XSelectInput(XtDisplay(win->player_widget), p->window, PointerMotionMask); } @@ -523,7 +523,7 @@ void PlayerUnref(Player *p) { free(p->tmp); } - SetPlayerWindow(0); + AppSetPlayerWindow(0); free(p); } diff --git a/application/window.h b/application/window.h index 7df9825..90207cb 100644 --- a/application/window.h +++ b/application/window.h @@ -24,6 +24,8 @@ #ifndef UWP_WINDOW_H #define UWP_WINDOW_H +#include + #include #include #include @@ -31,17 +33,13 @@ #include #include +#include "application.h" + #ifdef __cplusplus extern "C" { #endif -typedef enum PlaybackMode { - PLAYBACK_STOP = 0, - PLAYBACK_REPEAT, - PLAYBACK_REPEAT_LIST, - PLAYBACK_RANDOM, - PLAYBACK_AUTOPLAY -} PlaybackMode; + typedef struct Player { char *tmp; @@ -68,6 +66,8 @@ typedef struct { } PlayList; typedef struct MainWindow { + UiObject *obj; + Widget window; Widget menubar; Widget player_widget; -- 2.47.3