implement basic autoplay
[uwplayer.git] / application / player.c
index dbaca58..37c1b98 100644 (file)
@@ -21,6 +21,7 @@
  */
 
 #include "player.h"
+#include "main.h"
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -38,6 +39,8 @@
 #include <pthread.h>
 
 #include "json.h"
+#include "utils.h"
+#include "settings.h"
 
 extern char **environ;
 
@@ -106,8 +109,9 @@ static void* wait_for_process(void *data) {
     int status = 0;
     waitpid(player->process, &status, 0);
 
-    //player->isactive = FALSE;
+    player->isactive = FALSE;
     player->status = status;
+    SetPlayerWindow(0);
     
     return NULL;
 }
@@ -120,7 +124,11 @@ static int start_player_process(Player *player, MainWindow *win) {
         return 1;
     }
     
-    char *player_bin = "/usr/local/bin/mpv"; // TODO: get bin from settings
+    char *player_bin = SettingsGetPlayerBin();
+    if(!player_bin) {
+        fprintf(stderr, "No mpv binary available\n");
+        return 1;
+    }
     
     // -wid parameter value for embedding the player in the player_widget
     Window wid = XtWindow(win->player_widget);
@@ -308,7 +316,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 +330,38 @@ static void handle_json_rpc_msg(Player *player, JSONValue *v) {
         handle_json_rpc_event(player, v, event);
     }
     
-    json_print(v, NULL, 0);
-    fflush(stdout);
+    //json_print(v, NULL, 0);
+}
+
+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 +370,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);
     }
 }
 
@@ -364,22 +400,73 @@ static void handle_json_rpc_reqid(Player *player, JSONValue *v, int reqid) {
     }
 }
 
+static Boolean get_player_window(XtPointer data) {
+    Player *p = data;
+    MainWindow *win = GetMainWindow();
+    
+    Widget player_wid = win->player_widget;
+    Window root, parent;
+    Window *child;
+    unsigned int nchild;
+    XQueryTree(XtDisplay(player_wid), XtWindow(player_wid), &root, &parent, &child, &nchild);
+    if(nchild > 0) {
+        p->window = child[0];
+        XFree(child);
+        
+        SetPlayerWindow(p->window);
+        XSelectInput(XtDisplay(win->player_widget), p->window, PointerMotionMask);
+    }
+    
+    return 0;
+}
+
+#define CURSOR_AUTOHIDE_THRESHOLD_SEC  4
+
+static Boolean hide_cursor(XtPointer data) {
+    MainWindow *win = data;
+    WindowHidePlayerCursor(win);
+    return 0;
+}
+
+static void check_hide_cursor(Player *p) {
+    MainWindow *win = GetMainWindow();
+    if(win->cursorhidden) return;
+    
+    if(p->playback_time - win->motion_playback_time > CURSOR_AUTOHIDE_THRESHOLD_SEC) {
+        AppExecProc(hide_cursor, win);
+    }
+}
+
 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(!json_strcmp(name, "playback-time")) {
+            if(data && data->type == JSON_NUMBER) {
+                p->playback_time = data->value.number.value;
+                //printf("playback-time: %f\n", p->playback_time);
+                check_hide_cursor(p);
+            }
+        } else if(!json_strcmp(name, "eof-reached")) {
             if(data && data->type == JSON_LITERAL && data->value.literal.literal == JSON_TRUE) {
                 PlayerEOF(p);
             }
+        } else if(!json_strcmp(name, "osd-height")) {
+            if(data->type == JSON_NUMBER) {
+                p->osd_height = data->value.number.value;
+            }
         }
-    } 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\": [\"observe_property\", 1, \"osd-height\"] }\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;
+        
+        AppExecProc(get_player_window, p);
     }
 }
 
@@ -399,6 +486,7 @@ void PlayerDestroy(Player *p) {
         kill(p->process, SIGTERM);
     }
     
+    SetPlayerWindow(0);
     free(p);
 }
 
@@ -473,7 +561,30 @@ static void json_print(JSONValue *value, char *name, int indent) {
     }
 }
 
+static Boolean open_next_file(XtPointer data) {
+    char *file = data;
+    MainWindow *win = GetMainWindow();
+    if(win->file) {
+        free(file);
+    }
+    win->file = file;
+    PlayerOpenFile(win);
+    return 0;
+}
+
 void PlayerEOF(Player *p) {
-    char *cmd = "{ \"command\": [\"set_property\", \"playback-time\", 0] }\n";
-    write(p->ipc, cmd, strlen(cmd));
+    MainWindow *win = GetMainWindow();
+    if(win->repeatTrack) {
+        char *cmd = "{ \"command\": [\"set_property\", \"playback-time\", 0] }\n";
+        write(p->ipc, cmd, strlen(cmd));
+    } else if(win->autoplayFolder) {
+        char *next_file = util_find_next_file(win->file);
+        if(next_file) {
+            AppExecProc(open_next_file, next_file);
+        }
+    }
+}
+
+void PlayerHandleInput(MainWindow *win, Player *p, XmDrawingAreaCallbackStruct *cb) {
+    
 }