redirect key events to the player window
[uwplayer.git] / application / window.c
index 6ee3bf8..da0b04c 100644 (file)
@@ -38,6 +38,7 @@ static void FileOpenCB(Widget w, void *udata, void *cdata);
 static void ViewFullscreenCB(Widget w, void *udata, void *cdata);
 
 static void window_close_handler(Widget window, void *udata, void *cdata) {
+    WindowClosePlayer(main_window);
     ApplicationExit();
 }
 
@@ -51,6 +52,63 @@ static void windowKeyEH(Widget widget, XtPointer data, XEvent *event, Boolean *d
     }
 }
 
+static void resizeEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
+    WindowAdjustAspectRatio(data);
+}
+
+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 playerEH(Widget widget, XtPointer data, XEvent *event, Boolean *dispatch) {
+    MainWindow *win = data;
+    if(!win->player || win->player->window == 0) return;
+    
+    /*
+    if(event->type == EnterNotify) {
+        printf("enter: grab pointer\n");
+        
+        XtGrabPointer(
+                win->player_widget,
+                True,
+                ButtonPressMask | ButtonReleaseMask | PointerMotionMask | FocusChangeMask | EnterWindowMask | LeaveWindowMask,
+                GrabModeAsync,
+                GrabModeAsync,
+                None,
+                None,
+                CurrentTime);
+        
+        return;
+    }
+    if(event->type == LeaveNotify) {
+        printf("leave\n");
+        XtUngrabPointer(win->player_widget, CurrentTime); 
+        return;
+    }
+    
+    if(event->type == MotionNotify) {
+        static int testv = 0;
+        printf("test %d\n", testv++);
+    }
+    */
+    
+    if(event->type == KeyPress || event->type == KeyRelease) {
+        // redirect key events to the player window
+        event->xkey.window = win->player->window;
+        XSendEvent(
+                XtDisplay(win->player_widget),
+                win->player->window,
+                True,
+                0,
+                event);
+    }
+}
+
 MainWindow* WindowCreate(Display *display) {
     Arg args[32];
     int n;
@@ -71,7 +129,7 @@ MainWindow* WindowCreate(Display *display) {
             args,
             n);
     
-    
+    // close handler
     Atom wm_delete_window;
     wm_delete_window = XmInternAtom(
             display,
@@ -83,7 +141,9 @@ 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++;
     XtSetArg(args[n], XmNheight, 220); n++;
@@ -107,6 +167,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 +180,10 @@ MainWindow* WindowCreate(Display *display) {
     return window;
 }
 
+MainWindow* GetMainWindow(void) {
+    return main_window;
+}
+
 void WindowShow(MainWindow *win) {
     XtRealizeWidget(win->window);
 }
@@ -301,9 +371,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 +394,39 @@ 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;
+}