Only build in gettext support when gettext is available.
[synfig.git] / synfig-core / trunk / src / modules / mod_dv / trgt_dv.cpp
index 9b27e4b..a820c33 100644 (file)
@@ -1,8 +1,10 @@
-/*! ========================================================================
-** Synfig
-** ppm Target Module
-** $Id: trgt_dv.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $
+/* === S Y N F I G ========================================================= */
+/*!    \file trgt_dv.cpp
+**     \brief ppm Target Module
 **
+**     $Id$
+**
+**     \legal
 **     Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
 **
 **     This package is free software; you can redistribute it and/or
@@ -14,6 +16,7 @@
 **     but WITHOUT ANY WARRANTY; without even the implied warranty of
 **     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 **     General Public License for more details.
+**     \endlegal
 **
 ** === N O T E S ===========================================================
 **
@@ -33,6 +36,9 @@
 #include <ETL/stringf>
 #include "trgt_dv.h"
 #include <stdio.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
 #include <algorithm>
 #include <functional>
 #include <ETL/clock>
@@ -51,13 +57,14 @@ SYNFIG_TARGET_INIT(dv_trgt);
 SYNFIG_TARGET_SET_NAME(dv_trgt,"dv");
 SYNFIG_TARGET_SET_EXT(dv_trgt,"dv");
 SYNFIG_TARGET_SET_VERSION(dv_trgt,"0.1");
-SYNFIG_TARGET_SET_CVS_ID(dv_trgt,"$Id: trgt_dv.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $");
+SYNFIG_TARGET_SET_CVS_ID(dv_trgt,"$Id$");
 
 /* === M E T H O D S ======================================================= */
 
 
 dv_trgt::dv_trgt(const char *Filename)
 {
+       pid=-1;
        file=NULL;
        filename=Filename;
        buffer=NULL;
@@ -69,8 +76,11 @@ dv_trgt::dv_trgt(const char *Filename)
 
 dv_trgt::~dv_trgt()
 {
-       if(file)
-               pclose(file);
+       if(file){
+               fclose(file);
+               int status;
+               waitpid(pid,&status,0);
+       }
        file=NULL;
        delete [] buffer;
        delete [] color_buffer;
@@ -117,20 +127,64 @@ dv_trgt::init()
 {
        imagecount=desc.get_frame_start();
 
-       string command;
-
-       if(wide_aspect)
-               command=strprintf("encodedv -w 1 - > \"%s\"\n",filename.c_str());
-       else
-               command=strprintf("encodedv - > \"%s\"\n",filename.c_str());
-
-       // Open the pipe to encodedv
-       file=popen(command.c_str(),"w");
-
-       if(!file)
-       {
+       int p[2];
+  
+       if (pipe(p)) {
+               synfig::error(_("Unable to open pipe to encodedv"));
+               return false;
+       };
+  
+       pid_t pid = fork();
+  
+       if (pid == -1) {
+               synfig::error(_("Unable to open pipe to encodedv"));
+               return false;
+       }
+  
+       if (pid == 0){
+               // Child process
+               // Close pipeout, not needed
+               close(p[1]);
+               // Dup pipeout to stdin
+               if( dup2( p[0], STDIN_FILENO ) == -1 ){
+                       synfig::error(_("Unable to open pipe to encodedv"));
+                       return false;
+               }
+               // Close the unneeded pipeout
+               close(p[0]);
+               // Open filename to stdout
+               FILE* outfile = fopen(filename.c_str(),"wb");
+               if( outfile == NULL ){
+                       synfig::error(_("Unable to open pipe to encodedv"));
+                       return false;
+               }
+               int outfilefd = fileno(outfile);
+               if( outfilefd == -1 ){
+                       synfig::error(_("Unable to open pipe to encodedv"));
+                       return false;
+               }
+               if( dup2( outfilefd, STDOUT_FILENO ) == -1 ){
+                       synfig::error(_("Unable to open pipe to encodedv"));
+                       return false;
+               }
+               
+               if(wide_aspect)
+                       execlp("encodedv", "encodedv", "-w", "1", "-", (const char *)NULL);
+               else
+                       execlp("encodedv", "encodedv", "-", (const char *)NULL);
+               // We should never reach here unless the exec failed
                synfig::error(_("Unable to open pipe to encodedv"));
                return false;
+       } else {
+               // Parent process
+               // Close pipein, not needed
+               close(p[0]);
+               // Save pipeout to file handle, will write to it later
+               file = fdopen(p[1], "wb");
+               if (file == NULL) {
+                       synfig::error(_("Unable to open pipe to encodedv"));
+                       return false;
+               }
        }
 
        // Sleep for a moment to let the pipe catch up
@@ -148,7 +202,7 @@ dv_trgt::end_frame()
 }
 
 bool
-dv_trgt::start_frame(synfig::ProgressCallback *callback)
+dv_trgt::start_frame(synfig::ProgressCallback */*callback*/)
 {
        int w=desc.get_w(),h=desc.get_h();
 
@@ -169,7 +223,7 @@ dv_trgt::start_frame(synfig::ProgressCallback *callback)
 }
 
 Color *
-dv_trgt::start_scanline(int scanline)
+dv_trgt::start_scanline(int /*scanline*/)
 {
        return color_buffer;
 }