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
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <gtk/gtk.h>
#include <vte/vte.h>
#include "qemu-gtk.h"
#include "tcp.h"
/* ----------------------------------------------------------------- */
static int monitor_parse(struct qemu_window *win, char *buf, int len)
{
char *reply, *prompt, *cmd;
int off;
prompt = strstr(buf, "(qemu) ");
if (NULL == prompt)
return 0;
off = prompt - buf;
if (prompt > buf+1 && prompt[-2] == '\r' && prompt[-1] == '\n')
prompt -= 2;
*prompt = 0;
if (!win->mrun)
goto out;
cmd = win->mrun->cmd;
reply = strstr(buf, "\r\n");
if (!reply)
goto out;
reply += 2;
#if 0
fprintf(stderr, "\"%s\" -> [%s]\n--\n", cmd, reply);
#endif
if (0 == strcmp(cmd, "info version")) {
snprintf(win->version, sizeof(win->version), "%s", reply);
update_status(win);
} else if (0 == strcmp(cmd, "info name")) {
snprintf(win->name, sizeof(win->name), "%s", reply);
update_status(win);
} else if (0 == strcmp(cmd, "info vnc")) {
if (1 == sscanf(reply, "VNC server active on: %127[^\r\n]",
win->vnc_display) &&
strstr(reply, "No client connected"))
vnc_connect(win);
}
out:
return off + 7;
}
/* ----------------------------------------------------------------- */
static void monitor_next(struct qemu_window *win)
{
char buf[256];
int len;
if (win->mrun)
free(win->mrun);
win->mrun = win->mqueue;
if (!win->mrun)
return;
win->mqueue = win->mrun->next;
len = snprintf(buf, sizeof(buf), "%s\n", win->mrun->cmd);
write(win->monitor.handle, buf, len);
}
void monitor_append(struct qemu_window *win, char *cmd)
{
struct qemu_mq *mq;
if (win->mqueue) {
for (mq = win->mqueue; mq->next; mq = mq->next)
;
mq->next = malloc(sizeof(*mq));
mq = mq->next;
} else {
win->mqueue = malloc(sizeof(*mq));
mq = win->mqueue;
}
mq->next = NULL;
snprintf(mq->cmd, sizeof(mq->cmd), "%s", cmd);
if (0 == win->mused)
monitor_next(win);
}
static gboolean monitor_watch(GIOChannel *source, GIOCondition condition,
gpointer userdata)
{
struct qemu_window *win = userdata;
int rc;
if (win->mused == win->msize) {
if (!win->msize)
win->msize = 4;
win->msize *= 2;
win->mbuf = realloc(win->mbuf, win->msize +1);
}
rc = read(win->monitor.handle, win->mbuf + win->mused, win->msize - win->mused);
switch(rc) {
case -1:
if (EINTR == errno)
break;
perror("monitor: read");
goto close;
case 0:
fprintf(stderr, "monitor: EOF\n");
goto close;
default:
if (win->monitor.vte)
vte_terminal_feed(VTE_TERMINAL(win->monitor.vte), win->mbuf + win->mused, rc);
win->mused += rc;
win->mbuf[win->mused] = 0;
rc = monitor_parse(win, win->mbuf, win->mused);
if (rc) {
if (rc < win->mused)
memmove(win->mbuf, win->mbuf + rc, win->mused - rc);
win->mused -= rc;
}
break;
}
if (0 == win->mused)
monitor_next(win);
return TRUE;
close:
if (win->monitor.vte)
vte_terminal_feed(VTE_TERMINAL(win->monitor.vte), "\r\n=== CLOSED ===", 16);
close(win->monitor.handle);
win->monitor.handle = -1;
// gtk_widget_destroy(win->toplevel);
return FALSE;
}
/* ----------------------------------------------------------------- */
int monitor_connect(struct qemu_window *win, char *dest)
{
int fd;
fd = conn_init(&win->monitor, "monitor", dest);
if (-1 == fd)
return -1;
win->monitor.ch = g_io_channel_unix_new(fd);
win->monitor.id = g_io_add_watch(win->monitor.ch, G_IO_IN, monitor_watch, win);
monitor_append(win, "info version");
monitor_append(win, "info name");
monitor_append(win, "info vnc");
return fd;
}
|