handle key events in the player widget and pass them to mpv
[uwplayer.git] / application / player.c
index d553b81..381132e 100644 (file)
@@ -21,6 +21,7 @@
  */
 
 #include "player.h"
+#include "main.h"
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -37,7 +38,7 @@
 #include <sys/un.h>
 #include <pthread.h>
 
-
+#include "json.h"
 
 extern char **environ;
 
@@ -47,10 +48,16 @@ extern char **environ;
 #define PLAYER_POLL_TIMEOUT 500
 #define PLAYER_IN_BUFSIZE   8192
 
+static void json_print(JSONValue *value, char *name, int indent);
+
 static void* start_player(void *data);
 
 static void player_io(Player *p);
 
+static void handle_json_rpc_msg(Player *player, JSONValue *v);
+static void handle_json_rpc_reqid(Player *player, JSONValue *v, int reqid);
+static void handle_json_rpc_event(Player *player, JSONValue *v, JSONValue *event);
+
 void PlayerOpenFile(MainWindow *win) {
     pthread_t tid;
     if(pthread_create(&tid, NULL, start_player, win)) {
@@ -95,6 +102,17 @@ static int prepare_player(Player *player, char *log_arg, char *ipc_arg) {
     return 0;
 }
 
+static void* wait_for_process(void *data) {
+    Player *player = data;
+    int status = 0;
+    waitpid(player->process, &status, 0);
+
+    player->isactive = FALSE;
+    player->status = status;
+    
+    return NULL;
+}
+
 static int start_player_process(Player *player, MainWindow *win) {
     char log_arg[STR_BUFSIZE];
     char ipc_arg[STR_BUFSIZE];
@@ -140,6 +158,11 @@ static int start_player_process(Player *player, MainWindow *win) {
     player->process = player_pid;
     player->isactive = TRUE;
     
+    pthread_t tid;
+    if(pthread_create(&tid, NULL, wait_for_process, player)) {
+        perror("pthread_create");
+    }
+    
     return 0;
 }
 
@@ -224,6 +247,7 @@ static void* start_player(void *data) {
         PlayerDestroy(player);
         return NULL;
     }
+    close(player->log);
      
     if(connect_to_ipc(player)) {
         PlayerDestroy(player);
@@ -243,36 +267,150 @@ static void* start_player(void *data) {
 }
 
 static void player_io(Player *p) {
-    int flags = fcntl(p->log, F_GETFL, 0);
-    fcntl(p->log, F_SETFL, flags | O_NONBLOCK);
-    
     struct pollfd fds[2];
-    fds[0].fd = p->log;
+    fds[0].fd = p->ipc;
     fds[0].events = POLLIN;
     fds[0].revents = 0;
-    fds[1].fd = p->ipc;
-    fds[1].events = POLLIN;
-    fds[1].revents = 0;
+    JSONParser *js = json_parser_new();
     
     char buf[PLAYER_IN_BUFSIZE];
-    while(poll(fds, 2, PLAYER_POLL_TIMEOUT)) {
+    while(p->isactive && poll(fds, 2, PLAYER_POLL_TIMEOUT)) {
         if(fds[0].revents == POLLIN) {
-            // clean up fifo
-            read(fds[0].fd, buf, PLAYER_IN_BUFSIZE);
-        }
-        
-        if(fds[1].revents == POLLIN) {
             ssize_t r;
-            if((r = read(fds[1].fd, buf, PLAYER_IN_BUFSIZE)) <= 0) {
+            if((r = read(fds[0].fd, buf, PLAYER_IN_BUFSIZE)) <= 0) {
                 break;
             }
+            //fwrite(buf, 1, r, stdout);
+            fflush(stdout);
+            json_parser_fill(js, buf, r);
             
-            write(1, buf, r);
+            JSONValue *value;
+            int ret;
+            while((ret = json_read_value(js, &value)) == 1) {
+                handle_json_rpc_msg(p, value);
+                json_value_free(value);
+            }
             
+            if(ret == -1) {
+                fprintf(stderr, "JSON-RPC error\n");
+                break;
+            }
         }
         
-        char *cmd = "{ \"command\": [\"get_property\", \"playback-time\"] }\n";
+        char *cmd = "{ \"command\": [\"get_property\", \"playback-time\"], request_id=\"" REQ_ID_PLAYBACK_TIME "\" }\n";
+        //write(p->ipc, cmd, strlen(cmd));
+    }
+    
+    
+    printf("PlayerEnd: %s\n", strerror(errno));
+    fflush(stdout);
+}
+
+
+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) {
+        int request_id = 0;
+        if(request_id_v->value.string.length == 2) {
+            request_id = 10 * (request_id_v->value.string.string[0] - '0') + (request_id_v->value.string.string[1] - '0');
+            handle_json_rpc_reqid(player, v, request_id);
+            return;
+        }
+    } else if ((event = json_obj_get(&v->value.object, "event")) != NULL) {
+        handle_json_rpc_event(player, v, event);
+    }
+    
+    //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;
+    }
+    if(height >= 0) {
+        player->height = height;
+    }
+    if(player->width > 0 && player->height > 0) {
+        AppExecProc(player_widget_set_size, player);
+    }
+}
+
+static void handle_json_rpc_reqid(Player *player, JSONValue *v, int reqid) {
+    JSONValue *data = json_obj_get(&v->value.object, "data");
+    if(!data) return;
+    
+    switch(reqid) {
+        case REQ_ID_PLAYBACK_TIME_INT: {
+            if(data->type == JSON_NUMBER) {
+                player->playback_time = data->value.number.value;
+            }
+            break;
+        }
+        case REQ_ID_WIDTH_INT: {
+            if(data->type == JSON_INTEGER) {
+                player_set_size(player, data->value.integer.value, -1);
+            }
+            break;
+        }
+        case REQ_ID_HEIGHT_INT: {
+            if(data->type == JSON_INTEGER) {
+                player_set_size(player, -1, data->value.integer.value);
+            }
+            break;
+        }
+    }
+}
+
+static void handle_json_rpc_event(Player *p, JSONValue *v, JSONValue *event) {
+    if(!json_strcmp(event, "property-change")) {
+        JSONValue *name = json_obj_get(&v->value.object, "name");
+        JSONValue *data = json_obj_get(&v->value.object, "data");
+        if(!json_strcmp(name, "eof-reached")) {
+            if(data && data->type == JSON_LITERAL && data->value.literal.literal == JSON_TRUE) {
+                PlayerEOF(p);
+            }
+        }
+    } 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;
     }
 }
 
@@ -295,3 +433,111 @@ void PlayerDestroy(Player *p) {
     free(p);
 }
 
+
+static void json_print(JSONValue *value, char *name, int indent) {
+    if(name) {
+        printf("%*s%s: ", indent*4, "", name);
+    } else {
+        printf("%*s", indent*4, "");
+    }
+    
+    
+    switch(value->type) {
+        case JSON_OBJECT: {
+            printf("{\n");
+            
+            for(int i=0;i<value->value.object.size;i++) {
+                JSONObjValue val = value->value.object.values[i];
+                json_print(val.value, val.name, indent+1);
+                if(i+1 < value->value.object.size) {
+                    printf(",\n");
+                } else {
+                    printf("\n");
+                }
+            }
+            
+            printf("%*s}", indent*4, "");
+            break;
+        }
+        case JSON_ARRAY: {
+            printf("[\n");
+            
+            for(int i=0;i<value->value.object.size;i++) {
+                JSONValue *v = value->value.array.array[i];
+                json_print(v, NULL, indent+1);
+                if(i+1 < value->value.array.size) {
+                    printf(",\n");
+                } else {
+                    printf("\n");
+                }
+            }
+            
+            printf("%*s]", indent*4, "");
+            break;
+        }
+        case JSON_STRING: {
+            printf("\"%s\"", value->value.string.string);
+            break;
+        }
+        case JSON_INTEGER: {
+            printf("%i", (int)value->value.integer.value);
+            break;
+        }
+        case JSON_NUMBER: {
+            printf("%f", value->value.number.value);
+            break;
+        }
+        case JSON_LITERAL: {
+            char *lit = "NULL";
+            switch(value->value.literal.literal) {
+                case JSON_NULL: break;
+                case JSON_TRUE: lit = "true"; break;
+                case JSON_FALSE: lit = "false"; break;
+            }
+            printf("%s\n", lit);
+            break;
+        }
+    }
+    
+    if(indent == 0) {
+        putchar('\n');
+    }
+}
+
+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));
+        }
+    } 
+}