Re: SIGINT with gtk_main
- From: zz excite it
- To: gtk-app-devel-list gnome org
- Subject: Re: SIGINT with gtk_main
- Date: Wed, 21 Mar 2007 21:59:34 +0100
On Wednesday 21 March 2007 18:50:38 Michiel Jan Laurens de Hoon wrote:
For my application, I need to run gtk_main but I want to quit gtk_main
when either input is available on stdin or the user presses Ctrl-C. The
former is easy (using g_io_add_watch), but I am not sure what the best
approach is to handle Ctrl-C. Right now I am using g_timeout_add to
check every 100 milliseconds if Ctrl-C has been hit. But I'm wondering
if there is a better way to do this, preferably without checking
periodically for a Ctrl-C.
The relevant code looks as follows:
static gboolean
_main_quit(GIOChannel* source, GIOCondition condition, gpointer data)
{
gtk_main_quit();
return FALSE;
}
static int _interrupt_occurred = 0;
static gboolean _check_interrupt(gpointer data)
{
if (_interrupt_occurred)
{
gtk_main_quit();
return FALSE;
}
return TRUE;
}
static void _interrupt_handler(int sig)
{
_interrupt_occurred = 1;
}
int main(void)
{
/* ... some other code up here ... */
signal(SIGINT, _interrupt_handler);
GIOChannel* channel = g_io_channel_unix_new(fileno(stdin));
g_io_add_watch(channel, G_IO_IN, _main_quit, NULL);
g_io_channel_unref(channel);
g_timeout_add(100, _check_interrupt, NULL);
gtk_main();
if (_interrupt_occurred)
{
_interrupt_occurred = 0;
/* Do something with the interrupt */
}
return 0;
}
Many thanks in advance,
--Michiel.
Hi,
maybe adding this to main
/*signal(SIGHUP,SIG_IGN); */ /* ignore SIGHUP */
/*signal(SIGTERM,signal_handler);*/ /* catch SIGTERM */
signal(SIGINT,signal_handler); /* catch SIGINT */
and using this handler
static void signal_handler(int sig) __attribute__(( __noreturn__ ));
static void signal_handler(int sig)
{
switch (sig) {
case SIGINT:
puts("SIGINT signal catched");
break;
case SIGTERM:
puts("SIGTERM signal catched");
break;
}
/* Do something useful here */
exit(EXIT_FAILURE);
}
Ciao,
Tito
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]