[MPlayer-dev-eng] [PATCH] subtitle reload, slave mode - 3rd try

Salvatore Falco sfalco at studenti.ing.uniroma1.it
Mon Oct 18 11:39:20 CEST 2004


Hi all.

Here is the (hopefully) definitive version of the 'sub_reload' slave mode patch.
The aim of this patch is to add a new slave mode command, which is now called
'sub_add'.

'sub_add' syntax is the following:

    sub_add <search_filename> [<replace_filename>]
            Both arguments are names of files containing subtitles; if the optional
	    argument <replace_filename> is not present, <search_filename> is used in
	    its place, when possible.
	    If the subtitles from the <search_filename> file have been already loaded,
	    they are substituted with those loaded from <replace_filename> file; if
	    they have not been loaded, subtitles from <replace_filename> file are
	    added and visualized.
						    
As you can see, the subtitles structures present inside the code are referenced
through the name of the file they were loaded from. I prefer this approach to the
other one proposed for this command, that is addressing the internal structures
through their index (0 for the first subtitles, 1 for the seconds and so on). This
because of the following considerations:
							    
        1) the internal subtitle structure is never exported to the user, therefore the
           user should not rely on its knowledge: the command syntax should be transparent
           to the command implementation;
	2) through the use of the indices it could be possible to add subtitles to non
	   consecutive position of the structure: allowing the user to manipulate the way
	   data and memory structures are handled is not a good idea, in my opinion, from
	   the security point of view.
								       
Furthermore, it's me who is writing this patch, and I am not convinced by the other
approach: if you think it's better, show me the code! :-)
												       
       Best regards,
           Salvatore Falco
														   
														   
-------------- next part --------------
diff -Naur -x '*.o' MPlayer-20041012/DOCS/tech/slave.txt MPlayer-20041012.patched/DOCS/tech/slave.txt
--- MPlayer-20041012/DOCS/tech/slave.txt	2004-09-19 20:36:18.000000000 +0200
+++ MPlayer-20041012.patched/DOCS/tech/slave.txt	2004-10-14 00:02:23.000000000 +0200
@@ -48,6 +48,10 @@
 sub_step
     ???
 
+sub_load <search> [<replace>]
+    Subtitles contained into file <replace> are added, or substitute those loaded from <search>, if present.
+    When only <search> argument is given, <search> is used instead of <replace>.
+
 osd [<level>]
     Toggle OSD mode or set it to level when <level> >= 0.
 
diff -Naur -x '*.o' MPlayer-20041012/help/help_mp-en.h MPlayer-20041012.patched/help/help_mp-en.h
--- MPlayer-20041012/help/help_mp-en.h	2004-10-09 22:04:27.000000000 +0200
+++ MPlayer-20041012.patched/help/help_mp-en.h	2004-10-12 14:18:12.000000000 +0200
@@ -152,6 +152,7 @@
 "  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_SubFileReloaded "SUB: removed subtitle file %s, added subtitle file %s\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 -Naur -x '*.o' MPlayer-20041012/help/help_mp-it.h MPlayer-20041012.patched/help/help_mp-it.h
--- MPlayer-20041012/help/help_mp-it.h	2004-10-07 02:14:23.000000000 +0200
+++ MPlayer-20041012.patched/help/help_mp-it.h	2004-10-12 14:18:12.000000000 +0200
@@ -137,6 +137,7 @@
 "  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_SubFileReloaded "SUB: rimossi sottotitoli del file %s, aggiunti sottotitoli del file %s\n"
 
 // mencoder.c:
 
diff -Naur -x '*.o' MPlayer-20041012/input/input.c MPlayer-20041012.patched/input/input.c
--- MPlayer-20041012/input/input.c	2004-10-10 19:39:06.000000000 +0200
+++ MPlayer-20041012.patched/input/input.c	2004-10-14 00:22:50.000000000 +0200
@@ -77,6 +77,7 @@
   { 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}}, {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_GET_PERCENT_POS, "get_percent_pos", 0, { {-1,{0}} } },
diff -Naur -x '*.o' MPlayer-20041012/input/input.h MPlayer-20041012.patched/input/input.h
--- MPlayer-20041012/input/input.h	2004-10-10 19:39:06.000000000 +0200
+++ MPlayer-20041012.patched/input/input.h	2004-10-13 23:40:17.000000000 +0200
@@ -57,6 +57,7 @@
 #define MP_CMD_VO_ROOTWIN 53
 #define MP_CMD_SWITCH_VSYNC 54
 #define MP_CMD_SWITCH_RATIO 55
+#define MP_CMD_SUB_LOAD 56
 
 #define MP_CMD_GUI_EVENTS       5000
 #define MP_CMD_GUI_LOADFILE     5001
diff -Naur -x '*.o' MPlayer-20041012/mplayer.c MPlayer-20041012.patched/mplayer.c
--- MPlayer-20041012/mplayer.c	2004-10-10 19:39:07.000000000 +0200
+++ MPlayer-20041012.patched/mplayer.c	2004-10-16 00:30:58.850057952 +0200
@@ -725,6 +725,44 @@
     mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_AddedSubtitleFile, set_of_sub_size, filename);
 }
 
+/**
+    \brief adds or reloads the subtitles file during playback - only used in slave mode
+    \param search the path to the old subtitle file, which needs to be updated; if not found, a new subtitle is added
+    \param replace the path to the new subtitle file; if equal to NULL, \a search is used instead
+ */
+void load_subtitles_file(char *search, char *replace)
+{
+    sub_data *subd, *sub_search;
+    int counter;
+
+    if (search == NULL) return;
+    else 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);
+	    break;
+	}
+	if (subdata == set_of_subtitles[counter]) subdata = subd;
+	sub_free(set_of_subtitles[counter]);
+	set_of_subtitles[counter] = subd;
+	mp_msg(MSGT_CPLAYER, MSGL_STATUS, MSGTR_SubFileReloaded, search, replace);
+	return;
+    }
+    subd = sub_read_file(strdup(replace), sh_video->fps);
+    if(!subd) mp_msg(MSGT_CPLAYER, MSGL_ERR, MSGTR_CantLoadSub, replace);
+    if (subd == NULL || set_of_sub_size >= MAX_SUBTITLE_FILES) return;
+    set_of_subtitles[set_of_sub_size] = subd;
+    ++set_of_sub_size;
+    subdata = subd;
+    mp_msg(MSGT_FIXME, MSGL_FIXME, MSGTR_AddedSubtitleFile, set_of_sub_size, replace);
+    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.
@@ -3143,6 +3181,20 @@
       }
 #endif
     } break;
+    case MP_CMD_SUB_LOAD:
+    {
+#ifdef USE_SUB
+	if (sh_video) {
+	    if (cmd->nargs > 1) {
+		load_subtitles_file(cmd->args[0].v.s, cmd->args[1].v.s);
+	}
+	    else {
+		load_subtitles_file(cmd->args[0].v.s, NULL);
+}
+	    vo_osd_changed(OSDTYPE_SUBTITLE);
+	}
+#endif
+    } break;
     case MP_CMD_SUB_VISIBILITY:
     {
 #ifdef USE_SUB


More information about the MPlayer-dev-eng mailing list