GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sun Aug 28, 2016 5:09 am

All times are UTC

Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: GtkTextView woes
PostPosted: Fri Aug 31, 2007 1:54 pm 
Familiar Face

Joined: Fri Aug 31, 2007 1:45 pm
Posts: 9
Hi there, I am trying to use popen() to flush the output of a command to a GtkTextView widget. It works fine for some commands, however, when I try flush the output of mencoder, it doesn't do its job.

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>

void closeApp ( GtkWidget *window, gpointer data ) {



int main( int argc, char *argv[] ) {

   GtkWidget *window, *swindow;
   GtkWidget *text_view;
   GtkWidget *hbox;
   GtkTextBuffer *buffer;

   FILE *read_fp;
   char pbuffer[BUFSIZ + 1];
   int chars_read;
   char *command = "mencoder -ovc xvid -oac mp3lame -xvidencopts bitrate=300 -o ~/output.avi ~/input.avi";

   gtk_init (&argc, &argv);

   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   text_view = gtk_text_view_new();
   hbox = gtk_hbox_new ( FALSE, 2 );

   gtk_window_set_default_size(GTK_WINDOW(window), 400, 400);
   gtk_window_set_title(GTK_WINDOW(window), "Command Output");

   g_signal_connect ( GTK_OBJECT (window), "destroy",
                       GTK_SIGNAL_FUNC ( closeApp), NULL);

   buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
   gtk_text_view_set_editable(GTK_TEXT_VIEW (text_view), FALSE);

   memset(pbuffer, '\0', sizeof(pbuffer));
   read_fp = popen(command, "r");

   if (read_fp != NULL) {

          chars_read = fread( pbuffer, sizeof( char ), BUFSIZ, read_fp );
          while (chars_read > 0) {

              pbuffer[chars_read - 1] = '\0';
              // printf("Reading:-\n %s\n", pbuffer);
              chars_read = fread(pbuffer, sizeof(char), BUFSIZ, read_fp);


      gtk_text_buffer_set_text (buffer, pbuffer, -1);   


   swindow = gtk_scrolled_window_new (NULL, NULL);
   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),

   gtk_box_pack_start(GTK_BOX(hbox), swindow, TRUE, TRUE, 5);
   gtk_container_add(GTK_CONTAINER(swindow), text_view);
   gtk_container_add(GTK_CONTAINER(window), hbox);



     gtk_main ();

     return 0;


Any ideas?

 Post subject:
PostPosted: Sat Sep 01, 2007 1:21 pm 
Familiar Face

Joined: Fri Aug 31, 2007 1:45 pm
Posts: 9
Nobody? I tracked down this thread which is essentially the same thing as I am trying to do.

 Post subject: Maybe not too late!
PostPosted: Fri Oct 12, 2007 6:59 pm 
Familiar Face

Joined: Fri Jan 12, 2007 2:20 am
Posts: 15
Location: Indiana, USA

You did not say how the program failed. My guess is it stalls.

"char *fgets(char *s, int size, FILE *stream);"
This api may work better than fread() for you in this situation. It reads lines of data where lines are termintated with nl; plus its stops after eof is reached. Then insert each line inside the loop. popen() returns the data one line at time, just like a real console.

I would consider restructuring your prorgam. It may work the way it is but I would do it differently.

First I prefer to let gtk settle down and finish all its creation steps, like realizing windows, etc. I do this by creating the interface as normal, and saving the proccessing "popen() and reading" for later.

Next wrap the processing step into a g_timeout_add() function and after creating the last window and showing it - I call g_timeout_add(1sec, myfunc, userdata).

Let this "static gboolean myfunc (gpointer gp)' routine do the processing work - popen(), wile(read,insert) - and return FALSE; to stop the timer from calling the routine again.

For me this type of general approach assures me that the window is there , visible, and ready to do what I expect. However, most of your orignal issue is related to the buffering mechanism you ignored by using fread() vs fgets().

You could even go further to use threads instead of a g_timeout_add().

Hope this helps

Registered Linux User #270764
FC8 on Intel Quad
FC9 on AMD 4800+ X2
FC9 on Dual AMD-MP 2400+
Author: {gfhcm, gkrellfah2,gapcmon}

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC

Who is online

Users browsing this forum: Google [Bot] and 4 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group