#include <stdio.h>

#include <gtk/gtk.h>
#include <gtk/gtkprinter.h>

GMainLoop *loop = NULL;

static gboolean single_printer(GtkPrinter *printer, gpointer data) {
    printf("Printer name: %s\n", gtk_printer_get_name(printer));
    return FALSE;
}

static void destroy_notify(gpointer data) {
    /* loop should be passed as an argument but used global for simplicity */
    printf("destroy_notify called\n");
    if (loop) {
        if (g_main_loop_is_running(loop))
            g_main_loop_quit(loop);

        g_main_loop_unref(loop);
        loop = NULL;
    }
}

int main(int argc, char *argv[])
{
    gtk_init(&argc, &argv);
    GDK_THREADS_ENTER();
    loop = g_main_loop_new(NULL, FALSE);

    printf("This program hangs if CUPS subsystem is not running correctly\n");

    printf("Enumerating your printers:\n");
    gtk_enumerate_printers(single_printer, NULL, destroy_notify, TRUE);

    /* destroy_notify quits and destroys the loop, if it is not NULL
       then destroy_notify has not yet been called */
    if (loop) {
        printf("destroy_notify not yet called, waiting\n");
        GDK_THREADS_LEAVE();
        g_main_loop_run(loop);
        GDK_THREADS_ENTER();
        printf("destroy_notify has quit the loop\n");
    } else
        printf("destroy_notify already called, end of the program\n");

    GDK_THREADS_LEAVE();
}

