Re: What's the difference between the UNIX signal and gtk signal?



> there is no relationship *whatsoever* between the two except the name.

Yes, in fact, AFAICT, gtk doesn't even provide a mechanism for handling
Unix signals.  The best you can do is set up a traditional unix signal
handler which does nothing but catch the signal and write something to
a pipe, and write a gtk input handler for the other end of the pipe.
(You need to do this because it's not safe to call gtk functions from
within a unix signal handler.)

	void	sig_watcher(int) ;
	void	sig_catcher(gpointer, gint, GdkInputCondition) ;
	int	signal_fds[2] ;

		:

	/* Set up to catch signals */

	if( pipe(signal_fds) == 0 )
	{
	  long arg ;
	  (void) signal(SIGIO, sig_watcher) ;
	  (void) signal(SIGUSR1, sig_watcher) ;
	  (void) signal(SIGCHLD, sig_watcher) ;
	  /* etc */
	  gdk_input_add( signal_fds[0], GDK_INPUT_READ, sig_catcher, NULL) ;
	  arg = fcntl(signal_fds[0], F_GETFL) ;
	  fcntl(signal_fds[0], F_SETFL, arg | O_NONBLOCK) ;
	}

		:
	
	/* Catch unix signals, write them to the pipe */
	void
	sig_watcher(int signum)
	{
	  write(signal_fds[1], &signum, sizeof(signum)) ;
	}


	/* Get signals from the pipe.  *Now* it is safe to call gtk */
	void
	sig_catcher(gpointer d, gint fd, GdkInputCondition cond)
	{
	  int	signum ;

	  while( read(signal_fds[0], &signum, sizeof(signum)) > 0 )
	  {
	    printf("Look!  caught signal %d\n", signum) ;
	  }
	}


(This isn't 100% correct though, now that I think about it.  The signal
watcher needs to use a non-blocking write in case the pipe ever fills up.
In that case, signals should be discarded.  I'll work on it some more.)



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