enable/disable fullscreen with double click
[uwplayer.git] / application / window.c
index 6ee3bf8..17bda64 100644 (file)
@@ -37,7 +37,28 @@ 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 int blank_cursor_init = 0;
+static Pixmap blank_cursor_pixmap;
+static Cursor blank_cursor;
+
+static void init_blank_cursor(Widget w) {
+    char data = 0;
+    
+    XColor c;
+    
+    blank_cursor_pixmap = XCreateBitmapFromData(XtDisplay(w), XtWindow(w), &data, 1, 1);
+    if(!blank_cursor_pixmap) return;
+    
+    blank_cursor = XCreatePixmapCursor(XtDisplay(w), blank_cursor_pixmap, blank_cursor_pixmap, &c, &c, 0, 0);
+    
+    XFreePixmap(XtDisplay(w), blank_cursor_pixmap);
+    blank_cursor_init = 1;
+}
+
 static void window_close_handler(Widget window, void *udata, void *cdata) {
+    WindowClosePlayer(main_window);
     ApplicationExit();
 }
 
@@ -51,10 +72,144 @@ 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();
+    }
+    
+    if(!blank_cursor_init) {
+        init_blank_cursor(win->player_widget);
+    }
+}
+
+static void playerWidgetInputCB(Widget widget, XtPointer u, XtPointer c) {
+    MainWindow *win = u;
+    XmDrawingAreaCallbackStruct *cb = c;
+    
+    if(win->player && win->player->isactive) {
+        PlayerHandleInput(win, win->player, cb);
+    }
+}
+
+static void windowGrabButton(MainWindow *win) {
+    //printf("grab\n");
+    XtGrabButton(
+                win->player_widget,
+                AnyButton,
+                AnyModifier,
+                True,
+                ButtonPressMask | ButtonReleaseMask | PointerMotionMask | FocusChangeMask | EnterWindowMask | LeaveWindowMask,
+                GrabModeAsync,
+                GrabModeAsync,
+                None,
+                None);
+    win->buttongrab = True;
+}
+
+static void playerEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
+    MainWindow *win = data;
+    int etype = event->type;
+    
+    ///*
+    if(etype == EnterNotify) {
+        //printf("enter\n");
+        windowGrabButton(win);
+        return;
+    }
+    if(etype == LeaveNotify) {
+        //printf("leave\n");
+        //XtUngrabButton(win->player_widget, AnyButton, AnyModifier); 
+        //win->buttongrab = False;
+        return;
+    }
+    
+    int pass = 0;
+    if(etype == ButtonPress || etype == ButtonRelease || etype == KeyPress || etype == KeyRelease) {
+        //printf("button press\n");
+        pass = 1;
+    }
+    
+    if(!win->player || win->player->window == 0) return;
+    
+    WindowHandlePlayerEvent(win, event);
+    
+    if(pass) {
+        // redirect key events to the player window
+        //printf("redirect\n");
+        event->xkey.window = win->player->window;
+        XSendEvent(
+                XtDisplay(win->player_widget),
+                win->player->window,
+                True,
+                0,
+                event);
+    }
+}
+
+#define IGNORE_MOTION_THRESHOLD_MS 1000
+#define MOTION_POS_THRESHOLD_PIX   5
+
+#define DOUBLE_CLICK_TIME_MS       500
+
+void WindowHandlePlayerEvent(MainWindow *win, XEvent *event) {
+    // event handler for intercepted player mouse events
+    // win->player is not NULL
+    
+    int etype = event->type;
+    
+    if(etype == MotionNotify) {
+        Time cur_motion_time = event->xmotion.time;
+        int x = event->xmotion.x;
+        int y = event->xmotion.y;
+        if(win->cursorhidden && cur_motion_time - win->player_event_time < IGNORE_MOTION_THRESHOLD_MS) {
+            int diff_x = abs(x - win->mouse_x_orig);
+            int diff_y = abs(y - win->mouse_y_orig);
+            if(diff_x > MOTION_POS_THRESHOLD_PIX || diff_y > MOTION_POS_THRESHOLD_PIX) {
+                WindowShowPlayerCursor(win);
+            }
+        } else {
+            win->mouse_x_orig = x;
+            win->mouse_y_orig = y;
+        }
+        win->player_event_time = cur_motion_time;
+        win->motion_playback_time = win->player->playback_time;
+    } else if(etype == ButtonPress) {
+        Time t = event->xbutton.time;
+        if(t - win->button_press_time < DOUBLE_CLICK_TIME_MS) {
+            // double click
+            WindowFullscreen(main_window, !win->fullscreen);
+            win->button_press_time = 0;
+        } else {
+            win->button_press_time = t;
+        }
+    } else if(etype == ButtonRelease) {
+        win->player_event_time = event->xbutton.time;
+    }
+}
+
 MainWindow* WindowCreate(Display *display) {
     Arg args[32];
     int n;
-    
+     
     MainWindow *window = malloc(sizeof(MainWindow));
     memset(window, 0, sizeof(MainWindow));
     main_window = window;
@@ -71,7 +226,7 @@ MainWindow* WindowCreate(Display *display) {
             args,
             n);
     
-    
+    // close handler
     Atom wm_delete_window;
     wm_delete_window = XmInternAtom(
             display,
@@ -83,6 +238,8 @@ MainWindow* WindowCreate(Display *display) {
             window_close_handler,
             window);
     
+    // resize handler
+    XtAddEventHandler(window->window, StructureNotifyMask, False, resizeEH, window);
     
     n = 0;
     XtSetArg(args[n], XmNwidth, 360); n++;
@@ -107,6 +264,12 @@ MainWindow* WindowCreate(Display *display) {
     XtSetArg(args[n], XmNbackground, BlackPixelOfScreen(XtScreen(window->window))); n++;
     window->player_widget = XmCreateDrawingArea(container, "player", args, n);
     XtManageChild(window->player_widget);
+    XtAddCallback(window->player_widget, XmNinputCallback, playerWidgetInputCB, window);
+    XmProcessTraversal(window->player_widget, XmTRAVERSE_CURRENT);
+    XtAddEventHandler(window->player_widget, PointerMotionMask | ButtonPressMask | ButtonReleaseMask | FocusChangeMask |
+                 EnterWindowMask | KeyPressMask | KeyReleaseMask |
+                  LeaveWindowMask, FALSE, playerEH, window);
+    
     
     // get F keycode
     keycodeF = XKeysymToKeycode(XtDisplay(window->window), XStringToKeysym("F"));
@@ -114,6 +277,10 @@ MainWindow* WindowCreate(Display *display) {
     return window;
 }
 
+MainWindow* GetMainWindow(void) {
+    return main_window;
+}
+
 void WindowShow(MainWindow *win) {
     XtRealizeWidget(win->window);
 }
@@ -264,7 +431,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,
@@ -284,10 +456,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);
@@ -301,9 +470,18 @@ static void filedialog_select(
 
 static void FileOpenCB(Widget w, void *udata, void *cdata) {
     MainWindow *win = main_window;
-    Widget dialog = XnCreateFileSelectionDialog(win->window, "dialog", NULL, 0);
+    
+    Arg args[16];
+    int n = 0;
+    
+    XtSetArg(args[n], XnNshowViewMenu, 1); n++;
+    Widget dialog = XnCreateFileSelectionDialog(win->window, "dialog", args, n);
     XtAddCallback(dialog, XmNokCallback, (XtCallbackProc)filedialog_select, win);
     XtAddCallback(dialog, XmNcancelCallback, (XtCallbackProc)filedialog_end, win);
+    
+    Widget dirUp = XnFileSelectionBoxGetChild(dialog, XnFSB_DIR_UP_BUTTON);
+    XtUnmanageChild(dirUp);
+    
     XtManageChild(dialog);
 }
 
@@ -315,3 +493,56 @@ static void ViewFullscreenCB(Widget w, void *udata, void *cdata) {
     }
     
 }
+
+void WindowAdjustAspectRatio(MainWindow *win) {
+    if(!win->player) return;
+    if(!win->player->isactive || win->player->width <= 0 || win->player->height <= 0) return;
+      
+    // we have a running player width video
+    // adjust window aspect ratio (the window aspect ratio is different from
+    // the video, because of window decoration, menubar and other extra controls)
+    
+    Dimension win_width, win_height;
+    XtVaGetValues(win->window, XmNwidth, &win_width, XmNheight, &win_height, NULL);
+    Dimension player_width, player_height;
+    XtVaGetValues(win->player_widget, XmNwidth, &player_width, XmNheight, &player_height, NULL);
+    
+    double r = (double)win->player->width / (double)win->player->height;
+    double p_width = player_width;
+    double p_height = p_width / r;
+    
+    Dimension new_width = p_width + win_width - player_width;
+    Dimension new_height = p_height + win_height - player_height;
+    
+    XSizeHints hints;
+    hints.flags = PAspect;
+    hints.min_aspect.x = new_width;
+    hints.min_aspect.y = new_height;
+    hints.max_aspect.x = new_width;
+    hints.max_aspect.y = new_height;
+    XSetWMNormalHints(XtDisplay(win->window), XtWindow(win->window), &hints);
+}
+
+void WindowClosePlayer(MainWindow *win) {
+    if(win->player) {
+        PlayerDestroy(win->player);
+    }
+    win->player = NULL;
+    WindowShowPlayerCursor(win);
+}
+
+void WindowHidePlayerCursor(MainWindow *win) {
+    if(!win->cursorhidden && win->player && win->player->window != 0) {
+        XDefineCursor(XtDisplay(win->player_widget), XtWindow(win->player_widget), blank_cursor);
+        win->cursorhidden = True;
+        XFlush(XtDisplay(win->player_widget));
+    }
+}
+
+void WindowShowPlayerCursor(MainWindow *win) {
+    if(win->cursorhidden && win->player && win->player->window != 0) {
+        XDefineCursor(XtDisplay(win->player_widget), XtWindow(win->player_widget), None);
+        XFlush(XtDisplay(win->player_widget));
+    }
+    win->cursorhidden = False;
+}