redirect key events to the player window
[uwplayer.git] / application / window.c
index e5fc815..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();
 }
 
@@ -55,6 +56,59 @@ static void resizeEH(Widget widget, XtPointer data, XEvent *event, Boolean *disp
     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;
@@ -113,7 +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"));
@@ -364,3 +423,10 @@ void WindowAdjustAspectRatio(MainWindow *win) {
     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;
+}