+ if (0) dump_message(buf, msg_size);
+
+ spice_server_char_device_wakeup(&vt->vdagent_sin);
+}
+
+static void vdagent_request_clipboard(spiceTerm *vt, uint8_t selection)
+{
+ uint32_t size;
+
+ agent_owns_clipboard[selection] = 1;
+
+ size = 4 + sizeof(VDAgentClipboardRequest);
+
+ int msg_size = sizeof(VDIChunkHeader) + sizeof(VDAgentMessage) + size;
+ g_assert((vdagent_write_buffer_pos + msg_size) < VDAGENT_WBUF_SIZE);
+
+ unsigned char *buf = vdagent_write_buffer + vdagent_write_buffer_pos;
+ vdagent_write_buffer_pos += msg_size;
+
+ memset(buf, 0, msg_size);
+
+ VDIChunkHeader *hdr = (VDIChunkHeader *)buf;
+ VDAgentMessage *msg = (VDAgentMessage *)&hdr[1];
+ uint8_t *data = (uint8_t *)&msg[1];
+ *((uint32_t *)data) = 0;
+ data[0] = selection;
+ ((uint32_t *)data)[1] = VD_AGENT_CLIPBOARD_UTF8_TEXT;
+
+ hdr->port = VDP_CLIENT_PORT;
+ hdr->size = sizeof(VDAgentMessage) + size;
+
+ msg->protocol = VD_AGENT_PROTOCOL;
+ msg->type = VD_AGENT_CLIPBOARD_REQUEST;
+ msg->opaque = 0;
+ msg->size = size;
+
+ if (0) dump_message(buf, msg_size);
+
+ spice_server_char_device_wakeup(&vt->vdagent_sin);
+}
+
+static void vdagent_send_clipboard(spiceTerm *vt, uint8_t selection)
+{
+ uint32_t size;
+
+ if (selection != VD_AGENT_CLIPBOARD_SELECTION_PRIMARY) {
+ fprintf(stderr, "clipboard select %d is not supported\n", selection);
+ return;
+ }
+
+ gchar *sel_data;
+ glong sel_len;
+ if (vt->utf8) {
+ sel_data = g_utf16_to_utf8(vt->selection, vt->selection_len, NULL, &sel_len, NULL);
+ } else {
+ sel_len = vt->selection_len;
+ sel_data = g_malloc(sel_len);
+ int i;
+ for (i = 0; i < sel_len; i++) { sel_data[i] = (char)vt->selection[i]; }
+ sel_data[sel_len] = 0;
+ }
+
+ size = 8 + sel_len;
+
+ int msg_size = sizeof(VDIChunkHeader) + sizeof(VDAgentMessage) + size;
+ g_assert((vdagent_write_buffer_pos + msg_size) < VDAGENT_WBUF_SIZE);
+
+ unsigned char *buf = vdagent_write_buffer + vdagent_write_buffer_pos;
+ vdagent_write_buffer_pos += msg_size;
+
+ memset(buf, 0, msg_size);
+
+ VDIChunkHeader *hdr = (VDIChunkHeader *)buf;
+ VDAgentMessage *msg = (VDAgentMessage *)&hdr[1];
+ uint8_t *data = (uint8_t *)&msg[1];
+ *((uint8_t *)data) = selection;
+ data += 4;
+ *((uint32_t *)data) = VD_AGENT_CLIPBOARD_UTF8_TEXT;
+ data += 4;
+
+ memcpy(data, sel_data, sel_len);
+ g_free(sel_data);
+
+ hdr->port = VDP_CLIENT_PORT;
+ hdr->size = sizeof(VDAgentMessage) + size;
+
+ msg->protocol = VD_AGENT_PROTOCOL;
+ msg->type = VD_AGENT_CLIPBOARD;
+ msg->opaque = 0;
+ msg->size = size;
+
+ spice_server_char_device_wakeup(&vt->vdagent_sin);
+}
+
+static int
+vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
+{
+ spiceTerm *vt = SPICE_CONTAINEROF(sin, spiceTerm, vdagent_sin);
+
+ VDIChunkHeader *hdr = (VDIChunkHeader *)buf;
+ VDAgentMessage *msg = (VDAgentMessage *)&hdr[1];
+
+ //g_assert(hdr->port == VDP_SERVER_PORT);
+ g_assert(msg->protocol == VD_AGENT_PROTOCOL);
+
+ DPRINTF(1, "%d %d %d %d", len, hdr->port, msg->protocol, msg->type);
+
+ switch (msg->type) {
+ case VD_AGENT_MOUSE_STATE: {
+ VDAgentMouseState *info = (VDAgentMouseState *)&msg[1];
+ spiceterm_motion_event(vt, info->x, info->y, info->buttons);
+ break;
+ }
+ case VD_AGENT_ANNOUNCE_CAPABILITIES: {
+ VDAgentAnnounceCapabilities *caps = (VDAgentAnnounceCapabilities *)&msg[1];
+ DPRINTF(1, "VD_AGENT_ANNOUNCE_CAPABILITIES %d", caps->request);
+ int i;
+
+ int caps_size = VD_AGENT_CAPS_SIZE_FROM_MSG_SIZE(hdr->size);
+ for (i = 0; i < VD_AGENT_END_CAP; i++) {
+ DPRINTF(1, "CAPABILITIES %d %d", i, VD_AGENT_HAS_CAPABILITY(caps->caps, caps_size, i));
+ }
+
+ vdagent_send_capabilities(vt, 0);
+ break;
+ }
+ case VD_AGENT_CLIPBOARD_GRAB: {
+ VDAgentClipboardGrab *grab = (VDAgentClipboardGrab *)&msg[1];
+ uint8_t selection = *((uint8_t *)grab);
+ DPRINTF(1, "VD_AGENT_CLIPBOARD_GRAB %d", selection);
+ agent_owns_clipboard[selection] = 0;
+ break;
+ }
+ case VD_AGENT_CLIPBOARD_REQUEST: {
+ uint8_t *req = (uint8_t *)&msg[1];
+ uint8_t selection = *((uint8_t *)req);
+ uint32_t type = *((uint32_t *)(req + 4));
+
+ DPRINTF(1, "VD_AGENT_CLIPBOARD_REQUEST %d %d", selection, type);
+
+ vdagent_send_clipboard(vt, selection);
+
+ break;
+ }
+ case VD_AGENT_CLIPBOARD: {
+ uint8_t *data = (uint8_t *)&msg[1];
+ uint8_t selection = data[0];
+ uint32_t type = *(uint32_t *)(data + 4);
+ int size = msg->size - 8;
+ DPRINTF(1, "VD_AGENT_CLIPBOARD %d %d %d", selection, type, size);
+
+ if (type == VD_AGENT_CLIPBOARD_UTF8_TEXT) {
+ int i;
+ for (i = 0; i < size; i++) {
+ if ((vt->ibuf_count + 1) < IBUFSIZE) {
+ vt->ibuf[vt->ibuf_count++] = *(char *)(data + 8 + i);
+ } else {
+ fprintf(stderr, "warning: input buffer overflow\n");
+ }
+ }
+ spiceterm_update_watch_mask(vt, TRUE);
+ }
+ break;
+ }
+ case VD_AGENT_CLIPBOARD_RELEASE: {
+ uint8_t *data = (uint8_t *)&msg[1];
+ uint8_t selection = data[0];
+
+ DPRINTF(0, "VD_AGENT_CLIPBOARD_RELEASE %d", selection);
+
+ break;
+ }
+ case VD_AGENT_MONITORS_CONFIG: {
+ VDAgentMonitorsConfig *list = (VDAgentMonitorsConfig *)&msg[1];
+ g_assert(list->num_of_monitors > 0);
+ DPRINTF(0, "VD_AGENT_MONITORS_CONFIG %d %d %d", list->num_of_monitors,
+ list->monitors[0].width, list->monitors[0].height);
+
+ spiceterm_resize(vt, list->monitors[0].width, list->monitors[0].height);
+
+ vdagent_reply(vt, VD_AGENT_MONITORS_CONFIG, VD_AGENT_SUCCESS);
+ break;
+ }
+ default:
+ DPRINTF(0, "got uknown vdagent message type %d\n", msg->type);
+ }
+
+ return len;
+}
+
+static int
+vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
+{
+ DPRINTF(1, "%d %d", len, vdagent_write_buffer_pos);
+ g_assert(len >= 8);
+
+ if (!vdagent_write_buffer_pos) {
+ return 0;
+ }
+
+ int size = (len >= vdagent_write_buffer_pos) ? vdagent_write_buffer_pos : len;
+ memcpy(buf, vdagent_write_buffer, size);
+ if (size < vdagent_write_buffer_pos) {
+ memmove(vdagent_write_buffer, vdagent_write_buffer + size,
+ vdagent_write_buffer_pos - size);
+ }
+ vdagent_write_buffer_pos -= size;
+
+ DPRINTF(1, "RET %d %d", size, vdagent_write_buffer_pos);
+ return size;
+}
+
+static void
+vmc_state(SpiceCharDeviceInstance *sin, int connected)
+{
+ /* IGNORE */
+}
+
+static SpiceCharDeviceInterface my_vdagent_sif = {
+ .base.type = SPICE_INTERFACE_CHAR_DEVICE,
+ .base.description = "spice virtual channel char device",
+ .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
+ .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
+ .state = vmc_state,
+ .write = vmc_write,
+ .read = vmc_read,
+};
+
+static void
+init_spiceterm(spiceTerm *vt, uint32_t width, uint32_t height)
+{
+ int i;
+
+ g_assert(vt != NULL);
+ g_assert(vt->screen != NULL);
+
+ vt->width = width / 8;
+ vt->height = height / 16;
+
+ vt->total_height = vt->height * 20;
+ vt->scroll_height = 0;
+ vt->y_base = 0;
+ vt->y_displ = 0;
+
+ vt->region_top = 0;
+ vt->region_bottom = vt->height;
+
+ vt->g0enc = LAT1_MAP;
+ vt->g1enc = GRAF_MAP;
+ vt->cur_enc = vt->g0enc;
+ vt->charset = 0;
+
+ /* default text attributes */
+ vt->default_attrib.bold = 0;
+ vt->default_attrib.uline = 0;
+ vt->default_attrib.blink = 0;
+ vt->default_attrib.invers = 0;
+ vt->default_attrib.unvisible = 0;
+ vt->default_attrib.fgcol = 7;
+ vt->default_attrib.bgcol = 0;
+
+ vt->cur_attrib = vt->default_attrib;
+
+ if (vt->cells) {
+ vt->cx = 0;
+ vt->cy = 0;
+ vt->cx_saved = 0;
+ vt->cy_saved = 0;
+ g_free(vt->cells);
+ }
+
+ vt->cells = (TextCell *)calloc (sizeof (TextCell), vt->width*vt->total_height);
+
+ for (i = 0; i < vt->width*vt->total_height; i++) {
+ vt->cells[i].ch = ' ';
+ vt->cells[i].attrib = vt->default_attrib;
+ }
+
+ if (vt->altcells) {
+ g_free(vt->altcells);
+ }
+
+ vt->altcells = (TextCell *)calloc (sizeof (TextCell), vt->width*vt->height);
+}
+
+static void
+spiceterm_resize(spiceTerm *vt, uint32_t width, uint32_t height)
+{
+ DPRINTF(0, "width=%u height=%u", width, height);
+
+ if (vt->screen->width == width && vt->screen->height == height) {
+ return;
+ }
+
+ spice_screen_resize(vt->screen, width, height);
+
+ init_spiceterm(vt, width, height);
+
+ struct winsize dimensions;
+ dimensions.ws_col = vt->width;
+ dimensions.ws_row = vt->height;
+
+ ioctl(vt->pty, TIOCSWINSZ, &dimensions);
+}
+
+static spiceTerm *
+create_spiceterm(int argc, char** argv, uint32_t maxx, uint32_t maxy, guint timeout)
+{
+ SpiceCoreInterface *core = basic_event_loop_init();
+ SpiceScreen *spice_screen = spice_screen_new(core, maxx, maxy, timeout);
+
+ //spice_server_set_image_compression(server, SPICE_IMAGE_COMPRESS_OFF);
+
+ spiceTerm *vt = (spiceTerm *)calloc (sizeof(spiceTerm), 1);
+
+ vt->keyboard_sin.base.sif = &my_keyboard_sif.base;
+ spice_server_add_interface(spice_screen->server, &vt->keyboard_sin.base);
+
+ vt->vdagent_sin.base.sif = &my_vdagent_sif.base;
+ vt->vdagent_sin.subtype = "vdagent";
+ spice_server_add_interface(spice_screen->server, &vt->vdagent_sin.base);
+ vt->screen = spice_screen;
+
+ init_spiceterm(vt, maxx, maxy);
+
+ return vt;