GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

GtkTextView woes

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
Tommo
Familiar Face


Joined: 31 Aug 2007
Posts: 9

PostPosted: Fri Aug 31, 2007 1:54 pm    Post subject: GtkTextView woes Reply with quote

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.

Code: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

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

void closeApp ( GtkWidget *window, gpointer data ) {

      gtk_main_quit();

}

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_POLICY_AUTOMATIC,
                                  GTK_POLICY_AUTOMATIC);

    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_widget_show_all(window);

    pclose(read_fp);

      gtk_main ();

      return 0;

}



Any ideas?
Back to top
Tommo
Familiar Face


Joined: 31 Aug 2007
Posts: 9

PostPosted: Sat Sep 01, 2007 1:21 pm    Post subject: Reply with quote

Nobody? I tracked down this thread which is essentially the same thing as I am trying to do.
Back to top
skoona
Familiar Face


Joined: 12 Jan 2007
Posts: 15
Location: Indiana, USA

PostPosted: Fri Oct 12, 2007 6:59 pm    Post subject: Maybe not too late! Reply with quote

Tommo,

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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP