Skip to contentSkip to conversation

Fix shift key for graphical vnc display

PATCH New Kasai Takanori 1 participant 1 message Jul 20, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix shift key for graphical vnc display

Hi All,

There is a problem in the input of the key in the VNC connection on the PV domain. When client's keyboard is not the same as the kind of the keyboard of PVFB and GuestOS, it is not possible to input it correctly.

This patch handled the state of shift from the set keymap.
When client's keyboard is not same as the kind of PVFB/GuestOS, it is possible to input it correctly.
It was confirmed to input it correctly mutually with this patch between en-us and ja.

Best Regards,

--
Takanori Kasai

----- Original Message ----- From: "Kasai Takanori" <[email protected]>
To: "xen-devel" <[email protected]>
Sent: Friday, July 20, 2007 6:45 PM
Subject: [Xen-devel] [PATCH] [IOEMU] Fix shift key for graphical vnc display

Quoted 42 lines

Hi All,

There is a problem in the input of the key in the VNC connection on the HVM domain.
When client's keyboard is not the same as the kind of the keyboard of qemu-dm and GuestOS, it is not possible to input it correctly.

VNC client qemu-dm & GuestOS
--------------+-----------------------

   ja             en-us               ==> NG
   en-us          en-us               ==> OK

Originally, the same keysym-code between client and qemu-dm is transmitted. However, even if it is the same character, the state of shift is different according to the kind of keyboard.

ex.
"=" charactor
---------------------

en-us :  "="
ja    :  shift + "-"

Therefore, it is necessary to handle the state of the shift by setting qemu-dm and GuestOS. There is information on whether shift is necessary for each key for the keymap of qemu-dm.

ex.
VNC client : ja
qemu-dm & GuestOS : en-us
input key : "="
event client to qemu-dm :
shift(push) >> "="(push) >> "="(release) >> shift(release)
event qemu-dm to guest :
shift(push) >> shift(release) >> "="(push) >> "="(release) >> shift(push) >>
shift(release)

This patch handled the state of shift from the set keymap.
When client's keyboard is not same as the kind of qemu-dm/GuestOS, it is possible to input it correctly.
It was confirmed to input it correctly mutually with this patch between en-us and ja.

Quoted 7 lines

Best Regards,

--
Takanori Kasai

# HG changeset patch
# User [email protected]
# Date 1184920987 -32400
# Node ID e7bdc3a8c5b2977f00aec1e01637314add6927df # Parent 4fdd61d2134594aea67e9cc2450d8d15810358bc [PVFB] Fix shift key for vnc display

tools/xenfb/vncfb.c +105 -5
diff -r 4fdd61d21345 -r e7bdc3a8c5b2 tools/xenfb/vncfb.c
@@ -55,11 +55,46 @@ unsigned char keycode_table[512];
 unsigned char keycode_table[512];
 
 static void *kbd_layout;
+uint8_t modifiers_state[256];
 
 static int btnmap[] = {
 	BTN_LEFT, BTN_MIDDLE, BTN_RIGHT, BTN_SIDE,
 	BTN_EXTRA, BTN_FORWARD, BTN_BACK, BTN_TASK
 };
+
+static void press_key_shift_down(struct xenfb* xenfb, int down, int scancode)
+{
+	if (down)
+		xenfb_send_key(xenfb, 1, keycode_table[0x2a]);
+
+	if (xenfb_send_key(xenfb, down, keycode_table[scancode]) < 0)
+		fprintf(stderr, "Key %d %s lost (%s)\n",
+			scancode, "down", strerror(errno));
+
+	if (!down)
+		xenfb_send_key(xenfb, 0, keycode_table[0x2a]);
+}
+
+static void press_key_shift_up(struct xenfb* xenfb, int down, int scancode)
+{
+	if (down) {
+		if (modifiers_state[0x2a])
+			xenfb_send_key(xenfb, 0, keycode_table[0x2a]);
+		if (modifiers_state[0x36])
+			xenfb_send_key(xenfb, 0, keycode_table[0x36]);
+	}
+
+	if (xenfb_send_key(xenfb, down, keycode_table[scancode]) < 0)
+		fprintf(stderr, "Key %d %s lost (%s)\n",
+			scancode, "down", strerror(errno));
+
+	if (!down) {
+		if (modifiers_state[0x2a])
+			xenfb_send_key(xenfb, 1, keycode_table[0x2a]);
+		if (modifiers_state[0x36])
+			xenfb_send_key(xenfb, 1, keycode_table[0x36]);
+	}
+}
 
 static void on_kbd_event(rfbBool down, rfbKeySym keycode, rfbClientPtr cl)
 {
@@ -75,14 +110,75 @@ static void on_kbd_event(rfbBool down, r
 	rfbScreenInfoPtr server = cl->screen;
 	struct xenfb *xenfb = server->screenData;
 	int scancode;
-
-	if (keycode >= 'A' && keycode <= 'Z')
+	int shift = 0;
+	int shift_keys = 0;
+
+	if (keycode >= 'A' && keycode <= 'Z') {
 		keycode += 'a' - 'A';
-
-	scancode = keycode_table[keysym2scancode(kbd_layout, keycode)];
+		shift = 1;
+	}
+	else {
+		shift = keysymIsShift(kbd_layout, keycode);
+	}
+	shift_keys = modifiers_state[0x2a] | modifiers_state[0x36];	
+
+	scancode = keysym2scancode(kbd_layout, keycode);
 	if (scancode == 0)
 		return;
-	if (xenfb_send_key(xenfb, down, scancode) < 0)
+
+	switch(scancode) {
+	case 0x2a:			/* Left Shift */
+	case 0x36:			/* Right Shift */
+	case 0x1d:			/* Left CTRL */
+	case 0x9d:			/* Right CTRL */
+	case 0x38:			/* Left ALT */
+	case 0xb8:			/* Right ALT */
+		if (down)
+			modifiers_state[scancode] = 1;
+		else
+			modifiers_state[scancode] = 0;
+		xenfb_send_key(xenfb, down, keycode_table[scancode]); 
+		return;
+	case 0x45:			/* NumLock */
+		if (!down)
+			modifiers_state[scancode] ^= 1;
+		xenfb_send_key(xenfb, down, keycode_table[scancode]);
+		return;
+	}
+
+	if (keycodeIsKeypad(kbd_layout, scancode)) {
+	/* If the numlock state needs to change then simulate an additional
+	   keypress before sending this one.  This will happen if the user
+	   toggles numlock away from the VNC window.
+	*/
+		if (keysymIsNumlock(kbd_layout, keycode)) {
+			if (!modifiers_state[0x45]) {
+				modifiers_state[0x45] = 1;
+				xenfb_send_key(xenfb, 1, keycode_table[0x45]);
+				xenfb_send_key(xenfb, 0, keycode_table[0x45]);
+			}
+		} else {
+			if (modifiers_state[0x45]) {
+				modifiers_state[0x45] = 0;
+				xenfb_send_key(xenfb, 1, keycode_table[0x45]);
+				xenfb_send_key(xenfb, 0, keycode_table[0x45]);
+			}
+		}
+	}
+
+	/* If the shift state needs to change then simulate an additional
+	   keypress before sending this one.
+	*/
+	if (shift && !shift_keys) {
+		press_key_shift_down(xenfb, down, scancode);
+		return;
+	}
+	else if (!shift && shift_keys) {
+		press_key_shift_up(xenfb, down, scancode);
+		return;
+	}
+
+	if (xenfb_send_key(xenfb, down, keycode_table[scancode]) < 0)
 		fprintf(stderr, "Key %d %s lost (%s)\n",
 			scancode, down ? "down" : "up",
 			strerror(errno));
@@ -314,6 +410,10 @@ int main(int argc, char **argv)
 			atkbd_set2_keycode[atkbd_unxlate_table[i] | 0x80];
 	}
 
+	for (i = 0; i < 256; i++ ) {
+		modifiers_state[i] = 0;
+	}
+
 	fake_argv[2] = portstr;
 
         if (title != NULL)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel