Next: , Previous: I/O Callback Example, Up: Using External Event Loops


7.7.2.4 I/O Callback Example GTK+

The I/O callback interface can be used to integrate GPGME with the GTK+ event loop. The following code snippets shows how this can be done using the appropriate register and remove I/O callback functions. In this example, the private data of the register I/O callback function is unused. The event notifications is missing because it does not require any GTK+ specific setup.

     #include <gtk/gtk.h>
     
     struct my_gpgme_io_cb
     {
       gpgme_io_cb_t fnc;
       void *fnc_data;
       guint input_handler_id
     };
     
     void
     my_gpgme_io_cb (gpointer data, gint source, GdkInputCondition condition)
     {
       struct my_gpgme_io_cb *iocb = data;
       (*(iocb->fnc)) (iocb->data, source);
     }
     
     void
     my_gpgme_remove_io_cb (void *data)
     {
       struct my_gpgme_io_cb *iocb = data;
       gtk_input_remove (data->input_handler_id);
     }
     
     void
     my_gpgme_register_io_callback (void *data, int fd, int dir, gpgme_io_cb_t fnc,
                                    void *fnc_data, void **tag)
     {
       struct my_gpgme_io_cb *iocb = g_malloc (sizeof (struct my_gpgme_io_cb));
       iocb->fnc = fnc;
       iocb->data = fnc_data;
       iocb->input_handler_id = gtk_input_add_full (fd, dir
                                                        ? GDK_INPUT_READ
                                                        : GDK_INPUT_WRITE,
                                                    my_gpgme_io_callback,
                                                    0, iocb, NULL);
       *tag = iocb;
       return 0;
     }