summaryrefslogtreecommitdiff
path: root/test/previewer.c
blob: bc78b57f9ca9733e1904f27be02bad8f1c49ea86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * Copyright (C) 2011, 2012  Dan Nicholson <dbn.lists@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.

 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 */

#include <config.h>
#include <stdlib.h>
#include <glib.h>
#include <gtk/gtk.h>
#if GTK_MAJOR_VERSION > 2
#include <gtk/gtkx.h>
#endif
#include <gdk/gdkx.h>

#ifndef PLUG_PROCESS
#define PLUG_PROCESS "./previewer-plug"
#endif

static inline void
no_debug(const gchar *domain, GLogLevelFlags level,
         const gchar *message, gpointer data)
{
}

int
main(int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *socket;
    Window win;
    gchar *cmd;
    GError *error = NULL;

    gtk_init(&argc, &argv);

    if (!getenv("EVBP_DEBUG"))
        g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, no_debug, NULL);

    if (argc < 2) {
        g_printerr("No file supplied to open\n");
        exit(EXIT_FAILURE);
    }

    g_set_application_name("Evince Previewer Socket Test");
    gtk_window_set_default_icon_name("evince");

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), 800, 800);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    /* create a socket to embed the viewer in */
    socket = gtk_socket_new();
    g_signal_connect(socket, "plug-removed", G_CALLBACK(gtk_main_quit), NULL);
    gtk_container_add(GTK_CONTAINER(window), socket);
    gtk_widget_show_all(window);

    /* get the socket window ID to pass to the plug process */
    win = gtk_socket_get_id(GTK_SOCKET(socket));
    g_debug("The ID of the socket window is %lu\n", (unsigned long)win);

    /* spawn the plug process with the file */
    cmd = g_strdup_printf("\"%s\" %lu \"%s\"", PLUG_PROCESS,
                          (unsigned long)win, argv[1]);
    if (!g_spawn_command_line_async(cmd, &error)) {
        g_printerr("Failed to spawn plug process '%s': %s\n", cmd,
                   error->message);
        g_error_free(error);
        exit(EXIT_FAILURE);
    }
    g_free(cmd);

    gtk_main();

    return 0;
}