load file from arg
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 15 Jan 2022 12:31:00 +0000 (13:31 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 15 Jan 2022 12:31:00 +0000 (13:31 +0100)
application/main.c
application/main.h
application/window.c
application/window.h

index 6aaac89..18d534b 100644 (file)
@@ -26,6 +26,7 @@
 #include <locale.h>
 #include <time.h>
 #include <inttypes.h>
+#include <sys/stat.h>
 
 #include "window.h"
 #include "main.h"
@@ -38,6 +39,8 @@ static XtAppContext app;
 static Display *display;
 static Widget toplevel_window;
 
+static char *open_file_arg;
+
 static int event_pipe[2];
 
 static String fallback[] = {
@@ -101,6 +104,16 @@ int main(int argc, char** argv) {
        
     display =  XtOpenDisplay(app, NULL, APP_NAME, APP_CLASS, NULL, 0, &argc, argv);
     
+    if(argc > 1) {
+        struct stat s;
+        if(stat(argv[1], &s)) {
+            fprintf(stderr, "Cannot open file: %s\n", argv[1]);
+            perror("");
+            return 1;
+        }
+        open_file_arg = argv[1];
+    }
+    
     XtAppAddInput(
             app,
             event_pipe[0],
@@ -139,3 +152,11 @@ void AppExecProc(XtWorkProc proc, XtPointer data) {
     cb.data = data;
     write(event_pipe[1], &cb, sizeof(cb));
 }
+
+char* GetOpenFileArg(void) {
+    return open_file_arg;
+}
+
+void CleanOpenFileArg(void) {
+    open_file_arg = NULL;
+}
index 4198772..be957b8 100644 (file)
@@ -38,6 +38,9 @@ void ApplicationExit(void);
 
 void AppExecProc(XtWorkProc proc, XtPointer data);
 
+char* GetOpenFileArg(void);
+void CleanOpenFileArg(void);
+
 
 #ifdef __cplusplus
 }
index da0b04c..e7c85df 100644 (file)
@@ -37,6 +37,8 @@ static void WindowCreateMenu(MainWindow *win, Widget parent, Arg *args, int narg
 static void FileOpenCB(Widget w, void *udata, void *cdata);
 static void ViewFullscreenCB(Widget w, void *udata, void *cdata);
 
+static void WindowRealized(MainWindow *win);
+
 static void window_close_handler(Widget window, void *udata, void *cdata) {
     WindowClosePlayer(main_window);
     ApplicationExit();
@@ -52,10 +54,31 @@ static void windowKeyEH(Widget widget, XtPointer data, XEvent *event, Boolean *d
     }
 }
 
+static int main_window_is_realized = 0;
+
 static void resizeEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
+    if(!main_window_is_realized) {
+        if(XtIsRealized(widget)) {
+            main_window_is_realized = 1;
+            WindowRealized(data);
+        }
+    }
     WindowAdjustAspectRatio(data);
 }
 
+static void WindowRealized(MainWindow *win) {
+    char *open_file = GetOpenFileArg();
+    if(open_file) {
+        size_t len = strlen(open_file);
+        char *file = XtMalloc(len+1);
+        memcpy(file, open_file, len);
+        file[len] = 0;
+        WindowSetFile(win, file);
+        PlayerOpenFile(win);
+        CleanOpenFileArg();
+    }
+}
+
 static void playerWidgetInputCB(Widget widget, XtPointer u, XtPointer c) {
     MainWindow *win = u;
     XmDrawingAreaCallbackStruct *cb = c;
@@ -143,7 +166,7 @@ MainWindow* WindowCreate(Display *display) {
     
     // resize handler
     XtAddEventHandler(window->window, StructureNotifyMask, False, resizeEH, window);
-       
+    
     n = 0;
     XtSetArg(args[n], XmNwidth, 360); n++;
     XtSetArg(args[n], XmNheight, 220); n++;
@@ -334,7 +357,12 @@ void WindowMenubarSetVisible(MainWindow *win, bool visible) {
     }
 }
 
-
+void WindowSetFile(MainWindow *win, char *file) {
+    if(win->file) {
+        XtFree(win->file);
+    }
+    win->file = file;
+}
 
 static void filedialog_end(
         Widget widget,
@@ -354,10 +382,7 @@ static void filedialog_select(
     if(selection->value) {
         XmStringGetLtoR(selection->value, XmSTRING_DEFAULT_CHARSET, &value);
         if(value) {
-            if(data->file) {
-                XtFree(data->file);
-            }
-            data->file = value;
+            WindowSetFile(data, value);
             // no need to free the value, because it is stored in MainWindow
             
             PlayerOpenFile(data);
index 03da44e..82623e7 100644 (file)
@@ -66,6 +66,8 @@ void WindowFullscreen(MainWindow *win, bool enableFullscreen);
 
 void WindowMenubarSetVisible(MainWindow *win, bool visible);
 
+void WindowSetFile(MainWindow *win, char *file);
+
 void WindowAdjustAspectRatio(MainWindow *win);
 
 void WindowClosePlayer(MainWindow *win);