handle key events in the player widget and pass them to mpv
[uwplayer.git] / application / player.c
index dbaca58..381132e 100644 (file)
@@ -21,6 +21,7 @@
  */
 
 #include "player.h"
+#include "main.h"
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -106,7 +107,7 @@ static void* wait_for_process(void *data) {
     int status = 0;
     waitpid(player->process, &status, 0);
 
-    //player->isactive = FALSE;
+    player->isactive = FALSE;
     player->status = status;
     
     return NULL;
@@ -308,7 +309,7 @@ static void player_io(Player *p) {
 
 static void handle_json_rpc_msg(Player *player, JSONValue *v) {
     if(v->type != JSON_OBJECT) return;
-    
+      
     JSONValue *request_id_v = json_obj_get(&v->value.object, "request_id");
     JSONValue *event = NULL;
     if(request_id_v && request_id_v->type == JSON_STRING) {
@@ -322,10 +323,39 @@ static void handle_json_rpc_msg(Player *player, JSONValue *v) {
         handle_json_rpc_event(player, v, event);
     }
     
-    json_print(v, NULL, 0);
+    //json_print(v, NULL, 0);
     fflush(stdout);
 }
 
+static Boolean player_widget_set_size(XtPointer data) {
+    Player *player = data;
+    MainWindow *win = GetMainWindow();
+        
+    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);
+
+    Dimension new_width = player->width + win_width - player_width;
+    Dimension new_height = player->height + win_height - player_height;
+    
+    // set window size
+    XtVaSetValues(win->window, XmNwidth, new_width, XmNheight, new_height, NULL);
+    
+    // set window aspect ratio
+    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);
+    
+    return 0;
+}
+
+
+
 static void player_set_size(Player *player, int width, int height) {
     if(width >= 0) {
         player->width = width;
@@ -334,7 +364,7 @@ static void player_set_size(Player *player, int width, int height) {
         player->height = height;
     }
     if(player->width > 0 && player->height > 0) {
-        printf("TODO: set player size\n");
+        AppExecProc(player_widget_set_size, player);
     }
 }
 
@@ -373,13 +403,14 @@ static void handle_json_rpc_event(Player *p, JSONValue *v, JSONValue *event) {
                 PlayerEOF(p);
             }
         }
-    } else if(!json_strcmp(event, "playback-restart")) {
+    } else if(!p->isstarted && !json_strcmp(event, "playback-restart")) {
         char *cmd = "{ \"command\": [\"observe_property\", 1, \"playback-time\"] }\n"
                     "{ \"command\": [\"observe_property\", 1, \"eof-reached\"] }\n"
                     "{ \"command\": [\"get_property\", \"width\"], request_id=\"" REQ_ID_WIDTH "\" }\n"
                     "{ \"command\": [\"get_property\", \"height\"], request_id=\"" REQ_ID_HEIGHT "\" }\n"
                     "{ \"command\": [\"set_property\", \"keep-open\", true] }\n";
         write(p->ipc, cmd, strlen(cmd));
+        p->isstarted = TRUE;
     }
 }
 
@@ -477,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));
+        }
+    } 
+}