[MPlayer-dev-eng] [PATCH] subtitle reload, slave mode - 5th try

Salvatore Falco sfalco at studenti.ing.uniroma1.it
Mon Mar 7 14:40:32 CET 2005


Ok, again with the slave commands that load and reaload the subtitles.
This pathc adds three commands for slave mode to load and reload and
remove subtitles at runtime.
-- 
	Saluti, Salvatore Falco
-- 
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
-------------- next part --------------
diff -Nur -x '*.o' -x '0*' -x 'conf*' -x 'codec*' -x '*.a' -x '*depend' -x '*.so' -x 'pci*' MPlayer-20050307/AUTHORS MPlayer-20050307.patch/AUTHORS
--- MPlayer-20050307/AUTHORS	2005-02-21 10:26:06.000000000 +0100
+++ MPlayer-20050307.patch/AUTHORS	2005-03-07 14:36:48.793663240 +0100
@@ -220,6 +220,7 @@
     * jacosub parsing & dump support
     * overlapping subtitles & sub sorting support
     * SAMI subtitles dump support
+    * subtitles reload slave commands
 
 Feigl, Johannes (jaf) <johannes.feigl at aon.at>
     * original German docs translation (outdated)
diff -Nur -x '*.o' -x '0*' -x 'conf*' -x 'codec*' -x '*.a' -x '*depend' -x '*.so' -x 'pci*' MPlayer-20050307/DOCS/tech/slave.txt MPlayer-20050307.patch/DOCS/tech/slave.txt
--- MPlayer-20050307/DOCS/tech/slave.txt	2005-02-13 13:31:47.000000000 +0100
+++ MPlayer-20050307.patch/DOCS/tech/slave.txt	2005-03-07 14:30:58.060982712 +0100
@@ -65,6 +65,20 @@
     Step forward in the subtitle list by <value> steps or backwards if <value>
     is negative.
 
+sub_load <subtitles_file>
+    Loads the subtitles from file <subtitles_file>, and adds them to the
+    subtitles list.
+
+sub_reload <add_subtitles_file> [<remove_subtitles_file>]
+    If only <add_subtitles_file> argument is provided, subtitles from this
+    file are reloaded; if <remove_subtitles_file> argument is provided,
+    subtitles from <add_subtitles_file> replace those from <remove_subtitles_file>.
+
+sub_remove [<subtitles_file>]
+    If <subtitles_file> argument is present, the command removes the subtitles
+    loaded from this file; if no argument is provided, all the subtitles
+    are removed.
+
 osd [<level>]
     Toggle OSD mode or set it to level when <level> >= 0.
 
diff -Nur -x '*.o' -x '0*' -x 'conf*' -x 'codec*' -x '*.a' -x '*depend' -x '*.so' -x 'pci*' MPlayer-20050307/help/help_mp-en.h MPlayer-20050307.patch/help/help_mp-en.h
--- MPlayer-20050307/help/help_mp-en.h	2005-03-01 21:21:58.000000000 +0100
+++ MPlayer-20050307.patch/help/help_mp-en.h	2005-03-07 14:30:58.061982560 +0100
@@ -151,7 +151,11 @@
 "  DOCS/HTML/en/bugreports.html and follow the instructions there. We can't and\n"\
 "  won't help unless you provide this information when reporting a possible bug.\n"
 #define MSGTR_LoadingConfig "Loading config '%s'\n"
-#define MSGTR_AddedSubtitleFile "SUB: added subtitle file (%d): %s\n"
+#define MSGTR_AddedSubtitleFile "SUB: added (position %d) subtitles from file [%s]\n"
+#define MSGTR_SubtitleFileNotFound "SUB: subtitles from file [%s] not found\n"
+#define MSGTR_SubtitleFileReloaded "SUB: reloaded subtitles file [%s] (in place of [%s])\n"
+#define MSGTR_SubtitleFileRemoved "SUB: removed subtitles file [%s]\n"
+#define MSGTR_SubtitleFilesRemoved "SUB: removed all subtitles files\n"
 #define MSGTR_ErrorOpeningOutputFile "Error opening file [%s] for writing!\n"
 #define MSGTR_CommandLine "CommandLine:"
 #define MSGTR_RTCDeviceNotOpenable "Failed to open %s: %s (it should be readable by the user.)\n"
diff -Nur -x '*.o' -x '0*' -x 'conf*' -x 'codec*' -x '*.a' -x '*depend' -x '*.so' -x 'pci*' MPlayer-20050307/help/help_mp-it.h MPlayer-20050307.patch/help/help_mp-it.h
--- MPlayer-20050307/help/help_mp-it.h	2004-10-07 02:14:23.000000000 +0200
+++ MPlayer-20050307.patch/help/help_mp-it.h	2005-03-07 14:30:58.062982408 +0100
@@ -137,6 +137,11 @@
 "  versione di gcc. Se ritieni sia colpa di MPlayer, leggi DOCS/it/bugreports.html\n"\
 "  e segui le istruzioni. Non possiamo aiutarti, e non lo faremo, se non\n"\
 "  fornisci queste informazioni quando segnali un possibile problema.\n"
+#define MSGTR_AddedSubtitleFile "SUB: aggiunti (in posizione %d) i sottotitoli dal file [%s]\n"
+#define MSGTR_SubtitleFileNotFound "SUB: sottotitoli del file [%s] non trovati\n"
+#define MSGTR_SubtitleFileReloaded "SUB: ricaricati i sottotitoli del file [%s] (al posto di [%s])\n"
+#define MSGTR_SubtitleFileRemoved "SUB: rimossi i sottotitoli del file [%s]\n"
+#define MSGTR_SubtitleFilesRemoved "SUB: rimossi tutti i sottotitoli\n"
 
 // mencoder.c:
 
diff -Nur -x '*.o' -x '0*' -x 'conf*' -x 'codec*' -x '*.a' -x '*depend' -x '*.so' -x 'pci*' MPlayer-20050307/input/input.c MPlayer-20050307.patch/input/input.c
--- MPlayer-20050307/input/input.c	2005-02-17 16:56:25.000000000 +0100
+++ MPlayer-20050307.patch/input/input.c	2005-03-07 14:30:58.066981800 +0100
@@ -77,6 +77,9 @@
   { MP_CMD_SUB_POS, "sub_pos", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
   { MP_CMD_SUB_ALIGNMENT, "sub_alignment",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
   { MP_CMD_SUB_VISIBILITY, "sub_visibility", 0, { {-1,{0}} } },
+  { MP_CMD_SUB_LOAD, "sub_load", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
+  { MP_CMD_SUB_RELOAD, "sub_reload", 1, { {MP_CMD_ARG_STRING,{0}}, {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
+  { MP_CMD_SUB_REMOVE, "sub_remove", 0, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
   { MP_CMD_SUB_SELECT, "vobsub_lang", 0, { {-1,{0}} } }, // for compatibility
   { MP_CMD_SUB_SELECT, "sub_select", 0, { {-1,{0}} } },
   { MP_CMD_SUB_LOG, "sub_log", 0, { {-1,{0}} } },
diff -Nur -x '*.o' -x '0*' -x 'conf*' -x 'codec*' -x '*.a' -x '*depend' -x '*.so' -x 'pci*' MPlayer-20050307/input/input.h MPlayer-20050307.patch/input/input.h
--- MPlayer-20050307/input/input.h	2005-02-12 15:45:54.000000000 +0100
+++ MPlayer-20050307.patch/input/input.h	2005-03-07 14:33:32.897443992 +0100
@@ -63,6 +63,9 @@
 #define MP_CMD_SPEED_SET 59
 #define MP_CMD_RUN 60
 #define MP_CMD_SUB_LOG 61
+#define MP_CMD_SUB_LOAD 62
+#define MP_CMD_SUB_RELOAD 63
+#define MP_CMD_SUB_REMOVE 64
 
 #define MP_CMD_GUI_EVENTS       5000
 #define MP_CMD_GUI_LOADFILE     5001
diff -Nur -x '*.o' -x '0*' -x 'conf*' -x 'codec*' -x '*.a' -x '*depend' -x '*.so' -x 'pci*' MPlayer-20050307/mplayer.c MPlayer-20050307.patch/mplayer.c
--- MPlayer-20050307/mplayer.c	2005-02-25 12:11:07.000000000 +0100
+++ MPlayer-20050307.patch/mplayer.c	2005-03-07 14:34:28.172040976 +0100
@@ -776,6 +776,120 @@
     mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_AddedSubtitleFile, set_of_sub_size, filename);
 }
 
+/**
+    \brief adds a subtitles file during playback - only used in slave mode.
+    \param add_filename is the path to the subtitles file to load; if not found, no subtitles are added.
+    After a successful load, the command displays the loaded subtitles.
+ */
+void load_subtitles_file(char *add_filename)
+{
+    sub_data *subd;
+
+    if (set_of_sub_size >= MAX_SUBTITLE_FILES || add_filename == NULL) return;
+
+    subd = sub_read_file(strdup(add_filename), sh_video->fps);
+    if(subd == NULL) {
+	mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_CantLoadSub, add_filename);
+	return;
+    }
+    subdata = subd;
+    set_of_subtitles[set_of_sub_pos = set_of_sub_size] = subdata;
+    ++set_of_sub_size;
+    ++global_sub_size;
+    mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_AddedSubtitleFile, set_of_sub_size, add_filename);
+
+    return;
+}
+
+/**
+    \brief reloads a subtitles file during playback - only used in slave mode.
+    \param search is the path to the subtitle file which needs to be updated; if not found, no subtitles are added.
+    \param replace is the path to the new subtitle file; if equal to NULL, search is used in its place.
+    After a successful reload, the command displays the reloaded subtitles.
+ */
+void reload_subtitles_file(char *search, char *replace)
+{
+    sub_data *subd, *sub_search;
+    int counter;
+
+    if (search == NULL) return;
+    if (replace == NULL) replace = search;
+
+    for (counter = 0; counter < set_of_sub_size; ++counter) {
+	sub_search = set_of_subtitles[counter];
+	if (strcmp(search, sub_search->filename) != 0) continue;
+
+	subd = sub_read_file(strdup(replace), sh_video->fps);
+	if(!subd) {
+	    mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_CantLoadSub, replace);
+	    return;
+	}
+	subdata = subd;
+	sub_free(set_of_subtitles[counter]);
+	set_of_subtitles[set_of_sub_pos = counter] = subdata;
+	mp_msg(MSGT_CPLAYER, MSGL_STATUS, MSGTR_SubtitleFileReloaded, replace, search);
+	return;
+    }
+    /* we didn't find the subtitles to reload */
+    mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_SubtitleFileNotFound, search);
+    return;
+}
+
+/**
+    \brief remove subtitles file/files during playback - only used in slave mode
+    \param remove_filename is the path the subtitles file to remove; if equal to NULL, all subtitles are removed
+    If only one subtitles file is removed, the \c set_of_subtitles structure is re-arranged, so all subtitles are consecutive,
+    and the first subtitles structure is displayed.
+ */
+void remove_subtitles_file(char *remove_filename)
+{
+    sub_data *sub_search;
+    int counter;
+    short remove_all = 0; /* false */
+    char *removed_file;
+
+    if (remove_filename == NULL) remove_all = 1;
+
+    for (counter = 0; counter < set_of_sub_size; ++counter) {
+	sub_search = set_of_subtitles[counter];
+	removed_file = strdup(sub_search->filename);
+	if ((remove_all) || (strcmp(remove_filename, sub_search->filename) == 0)) {
+	    sub_free(sub_search);
+	    set_of_subtitles[counter] = NULL;
+	    if (remove_all) mp_msg(MSGT_CPLAYER, MSGL_V, MSGTR_SubtitleFileRemoved, removed_file);
+	    else {
+		mp_msg(MSGT_CPLAYER, MSGL_STATUS, MSGTR_SubtitleFileRemoved, removed_file);
+		break;
+	    }
+	}
+    }
+
+    if (remove_all) {
+	set_of_sub_size = 0;
+	mp_msg(MSGT_CPLAYER, MSGL_STATUS, MSGTR_SubtitleFilesRemoved);
+    } else {
+	if (counter >= set_of_sub_size) {
+	    mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_SubtitleFileNotFound, remove_filename);
+	    return;
+	}
+	/* re-arrange the set_of_subtitles structure, so that it does not contain a hole were the removed subtitles were */
+	for ( ; counter + 1 < set_of_sub_size ; ++counter) {
+	    set_of_subtitles[counter] = set_of_subtitles[counter + 1];
+	    set_of_subtitles[counter + 1] = NULL;
+	}
+	--set_of_sub_size;
+	--global_sub_size;
+    }
+    if (set_of_sub_size > 0) {
+	subdata = set_of_subtitles[set_of_sub_pos=0];
+    } else {
+	subdata = NULL;
+	set_of_sub_size = 0;
+	set_of_sub_pos = -1;
+    }
+    return;
+}
+
 // FIXME: if/when the GUI calls this, global sub numbering gets (potentially) broken.
 void update_set_of_subtitles()
     // subdata was changed, set_of_sub... have to be updated.
@@ -3353,6 +3467,39 @@
       }
 #endif
     } break;
+    case MP_CMD_SUB_LOAD:
+    {
+#ifdef USE_SUB
+	if (sh_video) {
+	    load_subtitles_file(cmd->args[0].v.s);
+	    vo_osd_changed(OSDTYPE_SUBTITLE);
+	}
+#endif
+    } break;
+    case MP_CMD_SUB_RELOAD:
+    {
+#ifdef USE_SUB
+	if (sh_video) {
+	    if (cmd->nargs > 1) reload_subtitles_file(cmd->args[1].v.s, cmd->args[0].v.s);
+	    else reload_subtitles_file(cmd->args[0].v.s, NULL);
+	    vo_osd_changed(OSDTYPE_SUBTITLE);
+	    // the following could be necessary
+	    vo_update_osd(sh_video->disp_w, sh_video->disp_h);
+	}
+#endif
+    } break;
+    case MP_CMD_SUB_REMOVE:
+    {
+#ifdef USE_SUB
+	if (sh_video) {
+	    if (cmd->nargs > 0) remove_subtitles_file(cmd->args[0].v.s);
+	    else remove_subtitles_file(NULL);
+	    vo_osd_changed(OSDTYPE_SUBTITLE);
+	    // the following should be necessary
+	    vo_update_osd(sh_video->disp_w, sh_video->disp_h);
+	}
+#endif
+    } break;
     case MP_CMD_GET_SUB_VISIBILITY:
 	{
 #ifdef USE_SUB


More information about the MPlayer-dev-eng mailing list