SIGINT with gtk_main



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.


--
Michiel de Hoon
Center for Computational Biology and Bioinformatics
Columbia University
1130 St Nicholas Avenue
New York, NY 10032



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