[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

RE: gdk_input_add looping




As long as I'm going to try this with pipes, I have another question.  When
reading from a pipe, the only way I can prevent looping (as with reading
from a file) is to call gdk_input_remove, close and reopen the pipe for
reading, and the call gdk_input_add.  Any idea why this is?  Here is some
relevant code:

IN MAIN:

/* create pipe needed for IPC */
	pid = getpid();
	strcpy(pipe, PIPEPATH);
	sprintf(pipe, "%s%d", pipe, pid);
	if((access(pipe, F_OK))){   /* if pipe DOES NOT exist */
		sprintf(command, "%s%s", "mkfifo -m 666 ", pipe);    /* any
better way to do this??? an internal mkfifo? */
		system(command);
	}
	if((access(pipe, R_OK))){   /* if pipe is NOT readable */
		printf("Error: can't read my own PID pipe?? Damn!\n");
		exit(1);
	}
	
	/* open pipe and attach gdk_input_add to monitor for readable data
availablity */
	if((fd_tag[0] = open(pipe, O_RDONLY | O_NONBLOCK)) == -1){
		printf("Error: can't open pipe %s\n", pipe);
		exit(1);
	}
	fd_tag[1] = gdk_input_add(fd_tag[0], GDK_INPUT_READ,
(GdkInputFunction)read_pipe, fd_tag);

IN READ_PIPE:


void read_pipe(gpointer data, int source, GdkInputCondition condition){
	char piperead[MAXPIPE + 1];
	int readbytes, *fd_tag;
	pid_t pid;
	char pipe[25];
	
	fd_tag = (int *)data;
/* create pipe needed for IPC */
	pid = getpid();
	strcpy(pipe, PIPEPATH);
	sprintf(pipe, "%s%d", pipe, pid);
	
/* initialize our buffer to nil */
	strcpy(piperead, "");

/* read the data from the pipe */
	readbytes = read(fd_tag[0], piperead, MAXPIPE);
	piperead[readbytes] = '\0';    /* terminate new string w/null */
/* close pipe, detach input watch, reopen, reattach to prevent looping.
Better way??? */
	gdk_input_remove(fd_tag[1]);
	close(fd_tag[0]);
	if((fd_tag[0] = open(pipe, O_RDONLY | O_NONBLOCK)) == -1){
		printf("Error: can't open pipe %s\n", pipe);
		exit(1);
	}
	fd_tag[1] = gdk_input_add(fd_tag[0], GDK_INPUT_READ,
(GdkInputFunction)read_pipe, fd_tag);
	

	if(!(strcmp(piperead, ""))){
		printf("Nothing sent throught pipe... ignoring.\n");
	}else{
		printf("Read from pipe:%s\n", piperead);
	}
}
	



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]