Bonjour à tous,

J'essaye depuis un moment, d'afficher le résultat d'une commande dans un textview. J''utilise pour cela un pipe. Mais, j'ai deux problèmes.
  • Le premier est qu'il n'y a pas de synchronisation entre la sortie et l'affichage dans la textview (j'aimerais que ça se fasse simultanément). Le

  • Le second est que l'affichage ne se fait pas correctement;lorsque la fenêtre contenant la textview est bougé, etc le contenu est effacé, et le contenu n'est pas intégrale


Voici mon code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
void RunPipeline(GtkWidget *widget, Window_main *Wm)
{
	gchar* cmd;
	gchar* outfile;
	gchar path;
	int mis=0;
	int i;
	GtkWidget* pDialog;
	GtkWidget* pDial;
	GtkWidget* label;
	GtkWidget* vbox;
	GtkWidget* stop;
	GtkWidget* scrollbar;
	gboolean st=FALSE;
	char** arg;
	GError* error=NULL;
 
	GPid ppid;
	pid_t pid;
	pid_t pid2;
	RunSt Rs;
 
	Rs.buffer2 =(gchar *)malloc(2000*sizeof(gchar));
	cmd =(char *)malloc(500*sizeof(char));
	arg =(char **)malloc(500*sizeof(char));
 
	arg[0]=g_strdup_printf("/mnt/home/pgm");
	arg[1]=g_strdup_printf(Wm->cfile);
	for(i=0;i<12;i++)
	{
		arg[i+2]=g_strdup_printf("%0.2f", Wm->prm[i]);
	}
	arg[14] = NULL; 																					//must finish by a NULL
 
	for(i=0; i<12; i++)
	{
		if(Wm->prm[i]==0)
		{
			mis++;
		}
	}
	if(strlen(Wm->cfile)==0 || mis!=0)
	{
		pDialog = gtk_message_dialog_new(GTK_WINDOW(Wm->main_window), GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_CLOSE, "directories or parameters missing !");
		gtk_dialog_run(GTK_DIALOG(pDialog));
		gtk_widget_destroy(pDialog);
	}
 
	cmd=g_strdup_printf("cd %s && ../prog %s %0.2f %0.2f %0.2f %0.2f %0.2f %0.2f %0.2f %0.2f %0.2f %0.2f %0.2f %0.2f",Wm->cRep, Wm->cfile , Wm->prm[0], Wm->prm[1], Wm->prm[2], Wm->prm[3], Wm->prm[4],  Wm->prm[5], Wm->prm[6], Wm->prm[7], Wm->prm[8], Wm->prm[9], Wm->prm[10], Wm->prm[11]);
	if(Wm->cfile!=NULL && mis==0)
	{
		/* Creation of Report Window */
		pDial = gtk_window_new(GTK_WINDOW_TOPLEVEL);
		gtk_window_set_title (GTK_WINDOW(pDial), "Report");
		gtk_window_set_default_size(GTK_WINDOW(pDial), WIDTH, HEIGHT);
		gtk_window_set_position(GTK_WINDOW(pDial), GTK_WIN_POS_CENTER);
		gtk_container_set_border_width(GTK_CONTAINER(pDial),5);
		scrollbar=gtk_scrolled_window_new(NULL, NULL);
		gtk_container_add(GTK_CONTAINER(pDial), scrollbar);
		vbox=gtk_vbox_new(FALSE, 0);
		gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrollbar), vbox);
		gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollbar), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
		Rs.txtview= gtk_text_view_new();
		gtk_text_view_set_editable(GTK_TEXT_VIEW(Rs.txtview), FALSE);
		gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(Rs.txtview), GTK_WRAP_WORD);
		/* set the text in the buffer to the output */
		Rs.buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (Rs.txtview));
		gtk_box_pack_start(GTK_BOX(vbox), Rs.txtview, TRUE, TRUE, 0);
		gtk_widget_show_all(pDial);
 
 
		//st=g_spawn_sync(Wm->cRep, arg, NULL, (GSpawnFlags) G_SPAWN_SEARCH_PATH, NULL, &ppid, &outfile , NULL, NULL, &error);
		//st=g_spawn_async_with_pipes(Wm->cRep, arg, NULL, (GSpawnFlags) G_SPAWN_SEARCH_PATH, NULL, &outfile, NULL, NULL, &ppid, NULL, &error);
		//st=g_spawn_command_line_sync(cmd, &outfile , NULL, NULL, &error);
 
		// create the pipe 
		char* line;
		int nb;		  
		if (pipe(Rs.pfd) == -1)
		{
			printf("pipe failed\n");
			exit(1);
		}		   
		// create the child
		pid = fork();
 
		if (pid < 0)
		{
			printf("fork failed\n");
			exit(1);
		}
		else  if (pid == 0)
		{
			GMainLoop *loop;
			// child  
			close(Rs.pfd[1]); // close the write side
			dup2(Rs.pfd[0], 0); // connect the read side with stdin
 
			/* Create a new event loop to run in */
			//loop = g_main_loop_new (NULL, FALSE);
 
			gint func_ref = g_timeout_add (10, (GSourceFunc)ChildCall, &Rs);
 
 
			/* Start the event loop */
			//g_main_loop_run (loop); 
 
			g_source_remove (func_ref);
			close(Rs.pfd[0]); // close the read side/	
		}
		else
		{
			close(Rs.pfd[0]); // close the unused read side 
			dup2(Rs.pfd[1], 1); // connect the write side with stdout 
			close(Rs.pfd[1]); // close the write side
			system(cmd);
		}
 
		kill(&pid2,15);
		kill(&pid,15);
	}
 
    /* free the lines of command */
	g_free(cmd);
	g_free(arg);
	g_free(Rs.buffer2);
}
 
gboolean ChildCall(gpointer data)
{
	RunSt* rs = (RunSt*) data;
	int nb;
 
	nb=read(rs->pfd[0], rs->buffer2, 1999);
	if (nb > 0 ) {
		printf("size: %d, buffer %s\n", nb, UTF8(rs->buffer2));
		gtk_text_buffer_set_text (GTK_TEXT_BUFFER(rs->buffer), UTF8(rs->buffer2), -1);
		gtk_text_view_set_buffer(GTK_TEXT_VIEW (rs->txtview), rs->buffer);
		gtk_widget_show (rs->txtview);
 
	} 
        else
        {
		printf("nothing to read\n");	
		return FALSE;
	}
	return TRUE;
}
Je vous remercie, si vous avez des suggestions !