From 547b5816f07b91c51ecc65a4fdd87b8378c7be95 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Fri, 14 Jan 2022 12:14:43 +0100 Subject: [PATCH] handle key events in the player widget and pass them to mpv --- application/player.c | 33 +++++++++++++++++++++++++++++++++ application/player.h | 1 + application/window.c | 10 ++++++++++ 3 files changed, 44 insertions(+) diff --git a/application/player.c b/application/player.c index 7620a63..381132e 100644 --- a/application/player.c +++ b/application/player.c @@ -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)); + } + } +} diff --git a/application/player.h b/application/player.h index 9858491..8f47e83 100644 --- a/application/player.h +++ b/application/player.h @@ -44,6 +44,7 @@ void PlayerDestroy(Player *p); void PlayerEOF(Player *p); +void PlayerHandleInput(MainWindow *win, Player *p, XmDrawingAreaCallbackStruct *cb); #ifdef __cplusplus } diff --git a/application/window.c b/application/window.c index dd8700c..8132f86 100644 --- a/application/window.c +++ b/application/window.c @@ -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 -- 1.8.3.1