summaryrefslogtreecommitdiff
path: root/gtk/snappy.c
blob: 79486a86b8014bfbfe4b25758e5a8795201c1bdf (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
#include "spice-client.h"

/* config */
static char *host      = "localhost";
static char *port      = "5920";
static char *tls_port  = "5921";
static char *password;
static char *ca_file;
static char *outf      = "snappy.ppm";

/* state */
static SpiceSession  *session;
static GMainLoop     *mainloop;

enum SpiceSurfaceFmt d_format;
gint                 d_width, d_height, d_stride;
gpointer             d_data;

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

static void primary_create(SpiceChannel *channel, gint format,
                           gint width, gint height, gint stride,
                           gint shmid, gpointer imgdata, gpointer data)
{
    fprintf(stderr, "%s: %dx%d, format %d\n", __FUNCTION__, width, height, format);
    d_format = format;
    d_width  = width;
    d_height = height;
    d_stride = stride;
    d_data   = imgdata;
}

static int write_ppm_32(void)
{
    FILE *fp;
    uint8_t *p;
    int n;

    fp = fopen(outf,"w");
    if (NULL == fp) {
	fprintf(stderr,"snappy: can't open %s: %s\n", outf, strerror(errno));
	return -1;
    }
    fprintf(fp,"P6\n%d %d\n255\n",
            d_width, d_height);
    n = d_width * d_height;
    p = d_data;
    while (n > 0) {
        fputc(p[2], fp);
        fputc(p[1], fp);
        fputc(p[0], fp);
        p += 4;
        n--;
    }
    fclose(fp);
    return 0;
}

static void invalidate(SpiceChannel *channel,
                       gint x, gint y, gint w, gint h, gpointer *data)
{
    int rc;

    switch (d_format) {
    case SPICE_SURFACE_FMT_32_xRGB:
        rc = write_ppm_32();
        break;
    default:
        fprintf(stderr, "unsupported spice surface format %d\n", d_format);
        rc = -1;
        break;
    }
    if (rc == 0)
        fprintf(stderr, "wrote screen shot to %s\n", outf);
    g_main_loop_quit(mainloop);
}

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

    if (type != SPICE_CHANNEL_DISPLAY)
        return;
    if (id != 0)
        return;

    g_signal_connect(channel, "spice-display-primary-create",
                     G_CALLBACK(primary_create), NULL);
    g_signal_connect(channel, "spice-display-invalidate",
                     G_CALLBACK(invalidate), NULL);
    spice_channel_connect(channel);
}

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

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

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

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

    g_type_init();
    mainloop = g_main_loop_new(NULL, false);

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

    if (host)
        g_object_set(session, "host", host, NULL);
    if (port)
        g_object_set(session, "port", port, NULL);
    if (tls_port)
        g_object_set(session, "tls-port", tls_port, NULL);
    if (password)
        g_object_set(session, "password", password, NULL);
    if (ca_file)
        g_object_set(session, "ca-file", ca_file, NULL);

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

    g_main_loop_run(mainloop);
    return 0;
}