add single instance mode
[uwplayer.git] / application / main.c
index 6aaac89..422b6ee 100644 (file)
 #include <locale.h>
 #include <time.h>
 #include <inttypes.h>
+#include <sys/stat.h>
 
 #include "window.h"
 #include "main.h"
 #include "settings.h"
 
-#include <ucx/buffer.h>
-#include <ucx/utils.h>
+#include <cx/buffer.h>
+#include <cx/utils.h>
 
 static XtAppContext app;
 static Display *display;
 static Widget toplevel_window;
 
+static char *open_file_arg;
+
 static int event_pipe[2];
 
 static String fallback[] = {
@@ -48,7 +51,7 @@ static String fallback[] = {
         
         "*pbbutton.shadowThickness: 1",
         "*pbbutton.highlightThickness: 1",
-    
+        
         "*XmText.baseTranslations: #override\\n" \
                                 "Ctrl~Alt~Meta<KeyPress>v: paste-clipboard()\\n" \
                                 "Ctrl~Alt~Meta<KeyPress>c: copy-clipboard()\\n" \
@@ -101,6 +104,36 @@ 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];
+    }
+    
+    // load settings
+    if(load_config()) {
+        return 1;
+    }
+    
+    // try single instance open
+    if(open_file_arg) {
+        char *instance_path = InstanceFilePath(display);
+        int instance_fd = ConnectToInstance(instance_path);
+        free(instance_path);
+        
+        if(instance_fd >= 0) {
+            write(instance_fd, "open ", 5);
+            write(instance_fd, open_file_arg, strlen(open_file_arg));
+            write(instance_fd, "\n", 1);
+            close(instance_fd);
+            return 0;
+        }
+    }
+    
     XtAppAddInput(
             app,
             event_pipe[0],
@@ -108,19 +141,14 @@ int main(int argc, char** argv) {
             input_proc,
             NULL);
     
-    // load settings
-    if(load_settings()) {
-        return 1;
-    }
-    
     MainWindow *window = WindowCreate(display);
     toplevel_window = window->window;
     
-    // random numbers only used for creating tmp dirs
+    // random numbers used for creating tmp dirs and for random playback
     srand(time(NULL));
     
     WindowShow(window);
-    XtAppMainLoop(app);
+    AppMainLoop(app);
     
     return 0;
 }
@@ -139,3 +167,33 @@ 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;
+}
+
+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);
+        }
+    }
+}