]> git.proxmox.com Git - spiceterm.git/blobdiff - spiceterm.c
remove unneeded assertion
[spiceterm.git] / spiceterm.c
index afbda2b3c06fa3744459ca2f4f5a9ab3ba7cb53a..680a5c7d952c09c8308daf2eee88d659eedebc9b 100644 (file)
@@ -79,6 +79,8 @@ unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
                                8,12,10,14, 9,13,11,15 };
 
 
+static void spiceterm_resize(spiceTerm *vt, uint32_t width, uint32_t height);
+
 static void vdagent_grab_clipboard(spiceTerm *vt, uint8_t selection);
 static void vdagent_request_clipboard(spiceTerm *vt, uint8_t selection);
 
@@ -537,7 +539,7 @@ spiceterm_set_alternate_buffer(spiceTerm *vt, int on_off)
         /* clear screen */
         for (y = 0; y <= vt->height; y++) {
             for (x = 0; x < vt->width; x++) {
-                spiceterm_clear_xy(vt, x, y);
+                //     spiceterm_clear_xy(vt, x, y);
             }
         }
 
@@ -1336,9 +1338,8 @@ my_kbd_push_key(SpiceKbdInstance *sin, uint8_t frag)
 }
 
 static void
-my_kbd_push_keyval(SpiceKbdInstance *sin, uint32_t keySym, int flags)
+spiceterm_push_keyval(spiceTerm *vt, uint32_t keySym, uint32_t flags)
 {
-    spiceTerm *vt = SPICE_CONTAINEROF(sin, spiceTerm, keyboard_sin);
     static int control = 0;
     static int shift = 0;
     char *esc = NULL;
@@ -1347,7 +1348,7 @@ my_kbd_push_keyval(SpiceKbdInstance *sin, uint32_t keySym, int flags)
 
     DPRINTF(1, "flags=%d keySym=%08x", flags, keySym);
 
-    if (flags & 1) {
+    if (flags & VD_AGENT_KEYVAL_FLAG_DOWN) {
         if (keySym == GDK_KEY_Shift_L || keySym == GDK_KEY_Shift_R) {
             shift = 1;
         } if (keySym == GDK_KEY_Control_L || keySym == GDK_KEY_Control_R) {
@@ -1444,7 +1445,7 @@ my_kbd_push_keyval(SpiceKbdInstance *sin, uint32_t keySym, int flags)
 
             if (vt->y_displ != vt->y_base) {
                 vt->y_displ = vt->y_base;
-                spiceterm_refresh (vt);
+                spiceterm_refresh(vt);
             }
 
             if (esc) {
@@ -1455,10 +1456,9 @@ my_kbd_push_keyval(SpiceKbdInstance *sin, uint32_t keySym, int flags)
         }
     }
 
-
 ret:
 
-    if (flags & 2) { // UP
+    if (!(flags & VD_AGENT_KEYVAL_FLAG_DOWN)) { // UP
         if (keySym == GDK_KEY_Shift_L || keySym == GDK_KEY_Shift_R) {
             shift = 0;
         } else if (keySym == GDK_KEY_Control_L || keySym == GDK_KEY_Control_R) {
@@ -1480,7 +1480,6 @@ static SpiceKbdInterface my_keyboard_sif = {
     .base.description   = "spiceterm keyboard device",
     .base.major_version = SPICE_INTERFACE_KEYBOARD_MAJOR,
     .base.minor_version = SPICE_INTERFACE_KEYBOARD_MINOR,
-    .push_keyval        = my_kbd_push_keyval,
     .push_scan_freg     = my_kbd_push_key,
     .get_leds           = my_kbd_get_leds,
 };
@@ -1621,6 +1620,50 @@ spiceterm_motion_event(spiceTerm *vt, uint32_t x, uint32_t y, uint32_t buttons)
     }
 }
 
+static void  
+vdagent_reply(spiceTerm *vt, uint32_t type, uint32_t error)
+{
+    uint32_t size;
+    
+    size = sizeof(VDAgentReply);
+
+    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];
+    VDAgentReply *reply = (VDAgentReply *)&msg[1];
+    reply->type = type;
+    reply->error = error;
+
+    hdr->port = VDP_CLIENT_PORT;
+    hdr->size = sizeof(VDAgentMessage) + size;
+
+    msg->protocol = VD_AGENT_PROTOCOL;
+    msg->type = VD_AGENT_REPLY;
+    msg->opaque = 0;
+    msg->size = size;
+
+    spice_server_char_device_wakeup(&vt->vdagent_sin);
+}
+
+static void
+dump_message(unsigned char *buf, int size)
+{
+    int i;
+
+    for (i = 0; i < size; i++) {
+        printf("%d  %02X\n", i, buf[i]);
+    }
+
+    // exit(0);
+}
+
 static void vdagent_send_capabilities(spiceTerm *vt, uint32_t request)
 {
     VDAgentAnnounceCapabilities *caps;
@@ -1638,6 +1681,7 @@ static void vdagent_send_capabilities(spiceTerm *vt, uint32_t request)
     VD_AGENT_SET_CAPABILITY(caps->caps, VD_AGENT_CAP_CLIPBOARD_SELECTION);
     VD_AGENT_SET_CAPABILITY(caps->caps, VD_AGENT_CAP_SPARSE_MONITORS_CONFIG);
     VD_AGENT_SET_CAPABILITY(caps->caps, VD_AGENT_CAP_GUEST_LINEEND_LF);
+    VD_AGENT_SET_CAPABILITY(caps->caps, VD_AGENT_CAP_KEYVAL);
 
     int msg_size =  sizeof(VDIChunkHeader) + sizeof(VDAgentMessage) + size;
     g_assert((vdagent_write_buffer_pos + msg_size) < VDAGENT_WBUF_SIZE);
@@ -1656,22 +1700,12 @@ static void vdagent_send_capabilities(spiceTerm *vt, uint32_t request)
     msg->size = size;
 
     memcpy(buf + sizeof(VDIChunkHeader) + sizeof(VDAgentMessage), (uint8_t *)caps, size);
-    
-    spice_server_char_device_wakeup(&vt->vdagent_sin);
-
-    free(caps);
-}
 
-static void
-dump_message(unsigned char *buf, int size)
-{
-    int i;
+    if (0) dump_message(buf, msg_size);
 
-    for (i = 0; i < size; i++) {
-        printf("%d  %02X\n", i, buf[i]);
-    }
+    spice_server_char_device_wakeup(&vt->vdagent_sin);
 
-    // exit(0);
+    free(caps);
 }
 
 static void vdagent_grab_clipboard(spiceTerm *vt, uint8_t selection)
@@ -1812,6 +1846,11 @@ vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
     DPRINTF(1, "%d %d %d %d", len, hdr->port, msg->protocol, msg->type);
 
     switch (msg->type) {
+    case VD_AGENT_KEYVAL: {
+        VDAgentKeyval *info = (VDAgentKeyval *)&msg[1];
+        spiceterm_push_keyval(vt, info->keyval, info->flags);
+        break;
+    } 
     case VD_AGENT_MOUSE_STATE: { 
         VDAgentMouseState *info = (VDAgentMouseState *)&msg[1];
         spiceterm_motion_event(vt, info->x, info->y, info->buttons);
@@ -1876,9 +1915,17 @@ vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
      
         break;
     }
-    case VD_AGENT_MONITORS_CONFIG:
-        /* ignore for now */
+    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);
     }
@@ -1889,7 +1936,7 @@ vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
 static int
 vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
 {
-    DPRINTF(0, "%d %d", len,  vdagent_write_buffer_pos);
+    DPRINTF(1, "%d %d", len,  vdagent_write_buffer_pos);
     g_assert(len >= 8);
 
     if (!vdagent_write_buffer_pos) {
@@ -1899,13 +1946,12 @@ vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
     int size = (len >= vdagent_write_buffer_pos) ? vdagent_write_buffer_pos : len;
     memcpy(buf, vdagent_write_buffer, size);
     if (size < vdagent_write_buffer_pos) {
-        DPRINTF(0, "MOVE %d", size);
         memmove(vdagent_write_buffer, vdagent_write_buffer + size, 
                 vdagent_write_buffer_pos - size);
     }
     vdagent_write_buffer_pos -= size;
 
-    DPRINTF(0, "RET %d %d", size,  vdagent_write_buffer_pos);
+    DPRINTF(1, "RET %d %d", size,  vdagent_write_buffer_pos);
     return size;
 }
 
@@ -1925,36 +1971,16 @@ static SpiceCharDeviceInterface my_vdagent_sif = {
     .read               = vmc_read,
 };
 
-static spiceTerm *
-create_spiceterm(int argc, char** argv, int maxx, int maxy, guint timeout)
+static void
+init_spiceterm(spiceTerm *vt, uint32_t width, uint32_t height)
 {
     int i;
 
-    SpiceScreen *spice_screen;
-
-    SpiceCoreInterface *core = basic_event_loop_init();
-    spice_screen = spice_screen_new(core, 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);
-
-    // screen->setXCutText = spiceterm_set_xcut_text;
-    // screen->ptrAddEvent = spiceterm_pointer_event;
-    // screen->newClientHook = new_client;
-    // screen->desktopName = "SPICE Command Terminal";
-
-    vt->maxx = spice_screen->width;
-    vt->maxy = spice_screen->height;
+    g_assert(vt != NULL);
+    g_assert(vt->screen != NULL);
 
-    vt->width = vt->maxx / 8;
-    vt->height = vt->maxy / 16;
+    vt->width = width / 8;
+    vt->height = height / 16;
 
     vt->total_height = vt->height * 20;
     vt->scroll_height = 0;
@@ -1980,17 +2006,70 @@ create_spiceterm(int argc, char** argv, int maxx, int maxy, guint timeout)
 
     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);
+    
+    spice_screen->image_cache = g_hash_table_new(g_int_hash, g_int_equal);
+
+    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;
 }
 
@@ -2110,6 +2189,8 @@ main (int argc, char** argv)
         exit (-1);
     }
 
+    vt->pty = master;
+
     /* watch for errors - we need to use glib directly because spice
      * does not have SPICE_WATCH_EVENT for this */
     GIOChannel *channel = g_io_channel_unix_new(master);