handle key events in the player widget and pass them to mpv
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Fri, 14 Jan 2022 11:14:43 +0000 (12:14 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Fri, 14 Jan 2022 11:14:43 +0000 (12:14 +0100)
application/player.c
application/player.h
application/window.c

index 7620a63..381132e 100644 (file)
@@ -508,3 +508,36 @@ void PlayerEOF(Player *p) {
     char *cmd = "{ \"command\": [\"set_property\", \"playback-time\", 0] }\n";
     write(p->ipc, cmd, strlen(cmd));
 }
+
+void PlayerHandleInput(MainWindow *win, Player *p, XmDrawingAreaCallbackStruct *cb) {
+    if(cb->event->type == KeyPress) {
+        XKeyEvent *xkey = &cb->event->xkey;
+
+        static XComposeStatus compose = {NULL, 0};
+        char chars[8];
+        KeySym keysym;
+        int nchars;
+        
+        char keystr[64];
+        keystr[0] = 0;
+
+        nchars = XLookupString(xkey, chars, 8, &keysym, &compose);
+        if(nchars == 1) {
+            if(chars[0] >= 'a' && chars[0] <= 'z') {
+                keystr[0] = chars[0];
+                keystr[1] = 0;
+            } else if(chars[0] == ' ') {
+                memcpy(keystr, "space", 6);
+            } 
+        }
+        
+        if(keystr[0] != 0) {
+            char cmdbuf[STR_BUFSIZE];
+            if(snprintf(cmdbuf, STR_BUFSIZE, "{ \"command\": [\"keypress\", \"%s\"] }\n", keystr) >= STR_BUFSIZE) {
+                // error: buffer to small
+                return;
+            }
+            write(p->ipc, cmdbuf, strlen(cmdbuf));
+        }
+    } 
+}
index 9858491..8f47e83 100644 (file)
@@ -44,6 +44,7 @@ void PlayerDestroy(Player *p);
 
 void PlayerEOF(Player *p);
 
+void PlayerHandleInput(MainWindow *win, Player *p, XmDrawingAreaCallbackStruct *cb);
 
 #ifdef __cplusplus
 }
index dd8700c..8132f86 100644 (file)
@@ -56,6 +56,15 @@ 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);
+    }
+}
+
 MainWindow* WindowCreate(Display *display) {
     Arg args[32];
     int n;
@@ -114,6 +123,7 @@ 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);
     
     // get F keycode