From 27bcca18fa61480ef63b636521295c56e727d994 Mon Sep 17 00:00:00 2001
From: Elias Gabriel Amaral da Silva <tolkiendili@gmail.com>
Date: Tue, 21 Dec 2010 10:15:56 -0300
Subject: [PATCH] command.c: setting environment variables on 'run' command

This patch sets environment variables when running an external program,
enabling it to take some action that depend on such data. The variables
set are:

MPLAYER_FILENAME, for the filename of current playing media (or an empty
string if there is no such media)

MPLAYER_POS, for current position of playback (in seconds, with
fractional part)

MPLAYER_LEN, for total length of media (same format as MPLAYER_POS)

I wrote a rationale for this feature at mailing list:

http://lists.mplayerhq.hu/pipermail/mplayer-dev-eng/2010-December/067097.html
http://lists.mplayerhq.hu/pipermail/mplayer-dev-eng/2010-December/067104.html

Suggestions welcome.
---
 DOCS/tech/slave.txt |    6 ++++++
 command.c           |   17 +++++++++++++++++
 2 files changed, 23 insertions(+), 0 deletions(-)

diff --git a/DOCS/tech/slave.txt b/DOCS/tech/slave.txt
index e31a9f4..ffbf3e8 100644
--- a/DOCS/tech/slave.txt
+++ b/DOCS/tech/slave.txt
@@ -508,6 +508,12 @@ run <value>
     Run <value> as shell command. In OSD menu console mode stdout and stdin
     are through the video output driver.
 
+    It sets the following environment variables:
+
+    MPLAYER_FILENAME      current playing filename
+    MPLAYER_POS           current media position
+    MPLAYER_LEN           total media duration
+
 
 Available properties:
 
diff --git a/command.c b/command.c
index b95b750..ce1705d 100644
--- a/command.c
+++ b/command.c
@@ -3375,7 +3375,24 @@ int run_command(MPContext *mpctx, mp_cmd_t *cmd)
         case MP_CMD_RUN:
 #ifndef __MINGW32__
             if (!fork()) {
+                char* media_len;
+                char* media_pos;
+
+                if(mp_property_do("length", M_PROPERTY_TO_STRING,
+                                  &media_len, mpctx) <= 0 ||
+                   mp_property_do("time_pos", M_PROPERTY_TO_STRING,
+                                  &media_pos, mpctx) <= 0) {
+
+                    media_len = "";
+                    media_pos = "";
+                }
+
+                setenv("MPLAYER_LEN", media_len, 1);
+                setenv("MPLAYER_POS", media_pos, 1);
+                setenv("MPLAYER_FILENAME", filename ? filename : "", 1);
+
                 execl("/bin/sh", "sh", "-c", cmd->args[0].v.s, NULL);
+
                 exit(0);
             }
 #endif
-- 
1.7.3.2

