summaryrefslogtreecommitdiff
path: root/gtk/spicy.c
blob: 7fe945539ea4e46cc8cfd59867c3bde04837a49f (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "spice-widget.h"

typedef struct spice_window spice_window;
struct spice_window {
    int            id;
    GtkWidget      *toplevel, *spice;
    GtkWidget      *fstatus, *status;
};

/* config */
static char *host = "localhost";
static char *port = "5920";

/* state */
static SpiceSession *session;
static spice_window *wins[4];

/* ------------------------------------------------------------------ */

static void destroy_cb(GtkWidget *widget, gpointer data)
{
    struct spice_window *win = data;

    if (win->id == 0) {
        gtk_main_quit();
    }
}

static spice_window *create_spice_window(SpiceSession *s, int id)
{
    char title[32];
    struct spice_window *win;
    GtkWidget *vbox;

    win = malloc(sizeof(*win));
    if (NULL == win)
        return NULL;
    memset(win,0,sizeof(*win));
    win->id = id;

    /* toplevel */
    win->toplevel = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    snprintf(title, sizeof(title), "spice display %d", id);
    gtk_window_set_title(GTK_WINDOW(win->toplevel), title);
    gtk_window_set_default_size(GTK_WINDOW(win->toplevel), 320, 280);
    g_signal_connect(G_OBJECT(win->toplevel), "destroy",
                     G_CALLBACK(destroy_cb), win);

    /* spice display */
    win->spice = spice_display_new(s, id);

    /* status line */
    win->status = gtk_label_new("status line");
    gtk_misc_set_alignment(GTK_MISC(win->status), 0, 0.5);
    gtk_misc_set_padding(GTK_MISC(win->status), 3, 1);

    /* Make a vbox and put stuff in */
    vbox = gtk_vbox_new(FALSE, 1);
    gtk_container_set_border_width(GTK_CONTAINER(vbox), 1);
    gtk_container_add(GTK_CONTAINER(win->toplevel), vbox);
#if 0
    menubar = gtk_ui_manager_get_widget(win->ui, "/MainMenu");
    gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
#endif
    gtk_box_pack_start(GTK_BOX(vbox), win->spice, TRUE, TRUE, 0);

    win->fstatus = gtk_frame_new(NULL);
    gtk_box_pack_end(GTK_BOX(vbox), win->fstatus, FALSE, TRUE, 0);
    gtk_container_add(GTK_CONTAINER(win->fstatus), win->status);

    /* show window */
    gtk_widget_show_all(win->toplevel);
    return win;
}

/* ------------------------------------------------------------------ */

static void main_channel_event(SpiceChannel *channel, enum SpiceChannelEvent event,
                               gpointer *data)
{
    fprintf(stderr, "main channel event: %d\n", event);

    switch (event) {
    case SPICE_CHANNEL_OPENED:
        /* nothing */
        break;
    default:
        /* TODO: more sophisticated error handling */
        gtk_main_quit();
        break;
    }
}

static void channel_new(SpiceSession *s, SpiceChannel *channel, gpointer *data)
{
    int type = spice_channel_type(channel);
    int id = spice_channel_id(channel);

    switch (type) {
    case SPICE_CHANNEL_MAIN:
        g_signal_connect(channel, "spice-channel-event",
                         G_CALLBACK(main_channel_event), NULL);
        fprintf(stderr, "new main channel\n");
        break;
    case SPICE_CHANNEL_DISPLAY:
        if (id >= SPICE_N_ELEMENTS(wins))
            return;
        if (wins[id] != NULL)
            return;
        fprintf(stderr, "new display channel (#%d), creating window\n", id);
        wins[id] = create_spice_window(s, id);
        break;
    }
}

/* ------------------------------------------------------------------ */

static void usage(FILE *fp)
{
    fprintf(fp,
            "usage:\n"
            "  spice-gtk [ options ]\n"
            "\n"
            "options:\n"
            "  -h    host [ %s ]\n"
            "  -p    port [ %s ]\n"
            "\n",
            host, port);
}

int main(int argc, char *argv[])
{
    int c;

    /* parse opts */
    gtk_init(&argc, &argv);
    for (;;) {
        if (-1 == (c = getopt(argc, argv, "h:p:")))
            break;
        switch (c) {
	case 'h':
            host = optarg;
	    break;
	case 'p':
            port = optarg;
	    break;
#if 0 /* --help */
        case 'h':
            usage(stdout);
            exit(0);
#endif
        default:
            usage(stderr);
            exit(1);
        }
    }


    session = spice_session_new();
    g_signal_connect(session, "spice-session-channel-new",
                     G_CALLBACK(channel_new), NULL);

    spice_session_set_host(session, host);
    spice_session_set_port(session, port);
    if (!spice_session_connect(session)) {
        fprintf(stderr, "spice_session_connect failed\n");
        exit(1);
    }

    gtk_main();
    return 0;
}