Skip to contentSkip to conversation

ps2: add support of auto-repeat

PATCH Superseded Amos Kong 6 participants 22 messages May 16, 2013 – May 21, 2013

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Amos Kong

author
#0 lore raw

PATCH 1/1 ps2: add support of auto-repeat

Guest driver sets repeat rate and delay time by KBD_CMD_SET_RATE, but ps2 backend doesn't process it and no auto-repeat implementation. This patch adds support of auto-repeat feature.

Guest ps2 driver sets autorepeat to fastest possible in reset,
period: 250ms, delay: 33ms

Tested by 'sendkey' monitor command.

referenced: http://www.computer-engineering.org/ps2keyboard/

---
 hw/input/ps2.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
hw/input/ps2.c +42 -0
index 3412079..1cfe055 100644
@@ -94,6 +94,9 @@ typedef struct {
     int translate;
     int scancode_set; /* 1=XT, 2=AT, 3=PS/2 */
     int ledstate;
+    int repeat_period; /* typematic period, ms */
+    int repeat_delay; /* typematic delay, ms */
+    int repeat_key; /* keycode to repeat */
 } PS2KbdState;
 
 typedef struct {
@@ -146,6 +149,22 @@ void ps2_queue(void *opaque, int b)
     s->update_irq(s->update_arg, 1);
 }
 
+static QEMUTimer *repeat_timer;
+static bool auto_repeat;
+
+static void repeat_ps2_queue(void *opaque)
+{
+    PS2KbdState *s = opaque;
+
+    if (auto_repeat) {
+        qemu_mod_timer(repeat_timer, qemu_get_clock_ns(vm_clock) +
+                       muldiv64(get_ticks_per_sec(), s->repeat_period,
+                       1000));
+        ps2_queue(&s->common, s->repeat_key);
+    }
+}
+
+
 /*
    keycode is expressed as follow:
    bit 7    - 0 key pressed, 1 = key released
@@ -167,7 +186,17 @@ static void ps2_put_keycode(void *opaque, int keycode)
             keycode = ps2_raw_keycode_set3[keycode & 0x7f];
         }
       }
+
+    /* only auto-repeat press event */
+    auto_repeat = ~keycode & 0x80;
     ps2_queue(&s->common, keycode);
+
+    if (auto_repeat) {
+        s->repeat_key = keycode;
+        /* delay a while before first repeat */
+        qemu_mod_timer(repeat_timer, qemu_get_clock_ns(vm_clock) +
+                       muldiv64(get_ticks_per_sec(), s->repeat_delay, 1000));
+    }
 }
 
 uint32_t ps2_read_data(void *opaque)
@@ -213,6 +242,11 @@ static void ps2_reset_keyboard(PS2KbdState *s)
 
 void ps2_write_keyboard(void *opaque, int val)
 {
+    /* repeat period/delay table from kernel (drivers/input/keyboard/atkbd.c) */
+    const short period[32] = { 33,  37,  42,  46,  50,  54,  58,  63,  67,  75,
+                83,  92, 100, 109, 116, 125, 133, 149, 167, 182, 200, 217, 232,
+                250, 270, 303, 333, 370, 400, 435, 470, 500 };
+    const short delay[4] = { 250, 500, 750, 1000 };
     PS2KbdState *s = (PS2KbdState *)opaque;
 
     switch(s->common.write_cmd) {
@@ -288,6 +322,11 @@ void ps2_write_keyboard(void *opaque, int val)
         s->common.write_cmd = -1;
         break;
     case KBD_CMD_SET_RATE:
+       /* Bit0-4 specifies the repeat rate */
+        s->repeat_period = period[val & 0x1f];
+       /* Bit5-6 bit specifies the delay time */
+        s->repeat_delay = delay[(val & 0x60) >> 5];
+
         ps2_queue(&s->common, KBD_REPLY_ACK);
         s->common.write_cmd = -1;
         break;
@@ -536,6 +575,8 @@ static void ps2_kbd_reset(void *opaque)
     s->scan_enabled = 0;
     s->translate = 0;
     s->scancode_set = 0;
+    s->repeat_period = 92;
+    s->repeat_delay = 500;
 }
 
 static void ps2_mouse_reset(void *opaque)
@@ -660,6 +701,7 @@ void *ps2_kbd_init(void (*update_irq)(void *, int), void *update_arg)
     vmstate_register(NULL, 0, &vmstate_ps2_keyboard, s);
     qemu_add_kbd_event_handler(ps2_put_keycode, s);
     qemu_register_reset(ps2_kbd_reset, s);
+    repeat_timer = qemu_new_timer_ns(vm_clock, repeat_ps2_queue, s);
     return s;
 }
 
Signature · 1 line
1.8.1.4
#1 lore raw
Quoted 31 lines · 在 2013-05-16四的 12:30 +0800,Amos Kong写道:

Guest driver sets repeat rate and delay time by KBD_CMD_SET_RATE, but ps2 backend doesn't process it and no auto-repeat implementation. This patch adds support of auto-repeat feature.

Guest ps2 driver sets autorepeat to fastest possible in reset,
period: 250ms, delay: 33ms

Tested by 'sendkey' monitor command.

referenced: http://www.computer-engineering.org/ps2keyboard/

---
 hw/input/ps2.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
hw/input/ps2.c +3 -0
index 3412079..1cfe055 100644
@@ -94,6 +94,9 @@ typedef struct {
     int translate;
     int scancode_set; /* 1=XT, 2=AT, 3=PS/2 */
     int ledstate;
+    int repeat_period; /* typematic period, ms */
+    int repeat_delay; /* typematic delay, ms */
+    int repeat_key; /* keycode to repeat */
 } PS2KbdState;
 
 typedef struct {

hw/input/ps2.c void ps2_queue(void *opaque, int b) · line 158

Quoted 10 lines
hw/input/ps2.c +7 -0
@@ -146,6 +149,22 @@ void ps2_queue(void *opaque, int b)
     s->update_irq(s->update_arg, 1);
 }
 
+static QEMUTimer *repeat_timer;
+static bool auto_repeat;
+
+static void repeat_ps2_queue(void *opaque)
+{
+    PS2KbdState *s = opaque;
+

theoretically, we have to check if the typematic key is in break now, if so, we will not do repeat anymore.
don't you think we have a chance that new key can come in during waiting?

Quoted 43 lines
hw/input/ps2.c +24 -0
+    if (auto_repeat) {
+        qemu_mod_timer(repeat_timer, qemu_get_clock_ns(vm_clock) +
+                       muldiv64(get_ticks_per_sec(), s->repeat_period,
+                       1000));
+        ps2_queue(&s->common, s->repeat_key);
+    }
+}
+
+
 /*
    keycode is expressed as follow:
    bit 7    - 0 key pressed, 1 = key released
@@ -167,7 +186,17 @@ static void ps2_put_keycode(void *opaque, int keycode)
             keycode = ps2_raw_keycode_set3[keycode & 0x7f];
         }
       }
+
+    /* only auto-repeat press event */
+    auto_repeat = ~keycode & 0x80;
     ps2_queue(&s->common, keycode);
+
+    if (auto_repeat) {
+        s->repeat_key = keycode;
+        /* delay a while before first repeat */
+        qemu_mod_timer(repeat_timer, qemu_get_clock_ns(vm_clock) +
+                       muldiv64(get_ticks_per_sec(), s->repeat_delay, 1000));
+    }
 }
 
 uint32_t ps2_read_data(void *opaque)
@@ -213,6 +242,11 @@ static void ps2_reset_keyboard(PS2KbdState *s)
 
 void ps2_write_keyboard(void *opaque, int val)
 {
+    /* repeat period/delay table from kernel (drivers/input/keyboard/atkbd.c) */
+    const short period[32] = { 33,  37,  42,  46,  50,  54,  58,  63,  67,  75,
+                83,  92, 100, 109, 116, 125, 133, 149, 167, 182, 200, 217, 232,
+                250, 270, 303, 333, 370, 400, 435, 470, 500 };
+    const short delay[4] = { 250, 500, 750, 1000 };
     PS2KbdState *s = (PS2KbdState *)opaque;
 
     switch(s->common.write_cmd) {

hw/input/ps2.c void ps2_write_keyboard(void *opaque, int val) · line 328

Quoted 7 lines
hw/input/ps2.c +4 -0
@@ -288,6 +322,11 @@ void ps2_write_keyboard(void *opaque, int val)
         s->common.write_cmd = -1;
         break;
     case KBD_CMD_SET_RATE:
+       /* Bit0-4 specifies the repeat rate */
+        s->repeat_period = period[val & 0x1f];
+       /* Bit5-6 bit specifies the delay time */
+        s->repeat_delay = delay[(val & 0x60) >> 5];

s/(val & 0x60) >> 5/(val & 0x60) >> 5 & 0x4/

Quoted 21 lines
hw/input/ps2.c +4 -0
+
         ps2_queue(&s->common, KBD_REPLY_ACK);
         s->common.write_cmd = -1;
         break;
@@ -536,6 +575,8 @@ static void ps2_kbd_reset(void *opaque)
     s->scan_enabled = 0;
     s->translate = 0;
     s->scancode_set = 0;
+    s->repeat_period = 92;
+    s->repeat_delay = 500;
 }
 
 static void ps2_mouse_reset(void *opaque)
@@ -660,6 +701,7 @@ void *ps2_kbd_init(void (*update_irq)(void *, int), void *update_arg)
     vmstate_register(NULL, 0, &vmstate_ps2_keyboard, s);
     qemu_add_kbd_event_handler(ps2_put_keycode, s);
     qemu_register_reset(ps2_kbd_reset, s);
+    repeat_timer = qemu_new_timer_ns(vm_clock, repeat_ps2_queue, s);
     return s;
 }
 
author
#2 lore raw
Quoted 35 lines · li guang
Quoted 34 lines · 在 2013-05-16四的 12:30 +0800,Amos Kong写道:

Guest driver sets repeat rate and delay time by KBD_CMD_SET_RATE, but ps2 backend doesn't process it and no auto-repeat implementation. This patch adds support of auto-repeat feature.

Guest ps2 driver sets autorepeat to fastest possible in reset,
period: 250ms, delay: 33ms

Tested by 'sendkey' monitor command.

referenced: http://www.computer-engineering.org/ps2keyboard/

---
 hw/input/ps2.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
hw/input/ps2.c +3 -0
index 3412079..1cfe055 100644
@@ -94,6 +94,9 @@ typedef struct {
     int translate;
     int scancode_set; /* 1=XT, 2=AT, 3=PS/2 */
     int ledstate;
+    int repeat_period; /* typematic period, ms */
+    int repeat_delay; /* typematic delay, ms */
+    int repeat_key; /* keycode to repeat */
 } PS2KbdState;
 
 typedef struct {
@@ -146,6 +149,22 @@ void ps2_queue(void *opaque, int b)
     s->update_irq(s->update_arg, 1);
 }
 
Quoted 7 lines

hw/input/ps2.c void ps2_queue(void *opaque, int b) · line 158

Quoted 7 lines
hw/input/ps2.c +7 -0
+static QEMUTimer *repeat_timer;
+static bool auto_repeat;
+
+static void repeat_ps2_queue(void *opaque)
+{
+    PS2KbdState *s = opaque;
+

Hi, Li guang

Quoted 2 lines

theoretically, we have to check if the typematic key is in break now, if so, we will not do repeat anymore.

You mean key is released? I checked by '~keycode & 0x80', stop repeat when key is release.

Quoted 2 lines

don't you think we have a chance that new key can come in during waiting?

When the new key (press) comes, repeat_timer will be modified by qemu_mod_timer(), original repate will be end.

Quoted 11 lines
Quoted 11 lines
hw/input/ps2.c +9 -0
+    if (auto_repeat) {
+        qemu_mod_timer(repeat_timer, qemu_get_clock_ns(vm_clock) +
+                       muldiv64(get_ticks_per_sec(), s->repeat_period,
+                       1000));
+        ps2_queue(&s->common, s->repeat_key);
+    }
+}
+
+
 /*
    keycode is expressed as follow:
Quoted 8 lines

hw/input/ps2.c static void ps2_put_keycode(void *opaque, int keycode) · line 191

Quoted 8 lines
hw/input/ps2.c +3 -0
    bit 7    - 0 key pressed, 1 = key released
@@ -167,7 +186,17 @@ static void ps2_put_keycode(void *opaque, int keycode)
             keycode = ps2_raw_keycode_set3[keycode & 0x7f];
         }
       }
+
+    /* only auto-repeat press event */
+    auto_repeat = ~keycode & 0x80;

^^^

Quoted 22 lines
Quoted 22 lines
hw/input/ps2.c +12 -0
     ps2_queue(&s->common, keycode);
+
+    if (auto_repeat) {
+        s->repeat_key = keycode;
+        /* delay a while before first repeat */
+        qemu_mod_timer(repeat_timer, qemu_get_clock_ns(vm_clock) +
+                       muldiv64(get_ticks_per_sec(), s->repeat_delay, 1000));
+    }
 }
 
 uint32_t ps2_read_data(void *opaque)
@@ -213,6 +242,11 @@ static void ps2_reset_keyboard(PS2KbdState *s)
 
 void ps2_write_keyboard(void *opaque, int val)
 {
+    /* repeat period/delay table from kernel (drivers/input/keyboard/atkbd.c) */
+    const short period[32] = { 33,  37,  42,  46,  50,  54,  58,  63,  67,  75,
+                83,  92, 100, 109, 116, 125, 133, 149, 167, 182, 200, 217, 232,
+                250, 270, 303, 333, 370, 400, 435, 470, 500 };
+    const short delay[4] = { 250, 500, 750, 1000 };
     PS2KbdState *s = (PS2KbdState *)opaque;
 
Quoted 11 lines

hw/input/ps2.c void ps2_write_keyboard(void *opaque, int val) · line 328

Quoted 9 lines
hw/input/ps2.c +4 -0
     switch(s->common.write_cmd) {
@@ -288,6 +322,11 @@ void ps2_write_keyboard(void *opaque, int val)
         s->common.write_cmd = -1;
         break;
     case KBD_CMD_SET_RATE:
+       /* Bit0-4 specifies the repeat rate */
+        s->repeat_period = period[val & 0x1f];
+       /* Bit5-6 bit specifies the delay time */
+        s->repeat_delay = delay[(val & 0x60) >> 5];

s/(val & 0x60) >> 5/(val & 0x60) >> 5 & 0x4/

                                          ^^ 0x3 ?

    val >> 5 & 0x3
Signature · 1 line
			Amos.
#3 lore raw
Quoted 71 lines · 在 2013-05-16四的 14:58 +0800,Amos Kong写道:
Quoted 35 lines · li guang
Quoted 34 lines · 在 2013-05-16四的 12:30 +0800,Amos Kong写道:

Guest driver sets repeat rate and delay time by KBD_CMD_SET_RATE, but ps2 backend doesn't process it and no auto-repeat implementation. This patch adds support of auto-repeat feature.

Guest ps2 driver sets autorepeat to fastest possible in reset,
period: 250ms, delay: 33ms

Tested by 'sendkey' monitor command.

referenced: http://www.computer-engineering.org/ps2keyboard/

---
 hw/input/ps2.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
hw/input/ps2.c +3 -0
index 3412079..1cfe055 100644
@@ -94,6 +94,9 @@ typedef struct {
     int translate;
     int scancode_set; /* 1=XT, 2=AT, 3=PS/2 */
     int ledstate;
+    int repeat_period; /* typematic period, ms */
+    int repeat_delay; /* typematic delay, ms */
+    int repeat_key; /* keycode to repeat */
 } PS2KbdState;
 
 typedef struct {
@@ -146,6 +149,22 @@ void ps2_queue(void *opaque, int b)
     s->update_irq(s->update_arg, 1);
 }
 
Quoted 7 lines

hw/input/ps2.c void ps2_queue(void *opaque, int b) · line 158

Quoted 7 lines
hw/input/ps2.c +7 -0
+static QEMUTimer *repeat_timer;
+static bool auto_repeat;
+
+static void repeat_ps2_queue(void *opaque)
+{
+    PS2KbdState *s = opaque;
+

Hi, Li guang

Quoted 2 lines

theoretically, we have to check if the typematic key is in break now, if so, we will not do repeat anymore.

You mean key is released? I checked by '~keycode & 0x80', stop repeat when key is release.

Quoted 2 lines

don't you think we have a chance that new key can come in during waiting?

When the new key (press) comes, repeat_timer will be modified by qemu_mod_timer(), original repate will be end.

Quoted 13 lines
Quoted 13 lines
hw/input/ps2.c +9 -0
+    if (auto_repeat) {
+        qemu_mod_timer(repeat_timer, qemu_get_clock_ns(vm_clock) +
+                       muldiv64(get_ticks_per_sec(), s->repeat_period,
+                       1000));
+        ps2_queue(&s->common, s->repeat_key);
+    }
+}
+
+
 /*
    keycode is expressed as follow:
    bit 7    - 0 key pressed, 1 = key released
@@ -167,7 +186,17 @@ static void ps2_put_keycode(void *opaque, int keycode)
Quoted 8 lines
Quoted 6 lines

hw/input/ps2.c static void ps2_put_keycode(void *opaque, int keycode) · line 191

Quoted 6 lines
hw/input/ps2.c +3 -0
             keycode = ps2_raw_keycode_set3[keycode & 0x7f];
         }
       }
+
+    /* only auto-repeat press event */
+    auto_repeat = ~keycode & 0x80;

^^^

case:

1. new key pressed
2. enter ps2_put_keycode
3. previous timer timeout
4. enter repeat_ps2_queue
5. put previous keycode in queue
6. back to ps2_put_keycode
7. check auto_repeat

so, an obsolete key comes up.
can timer interrupt ps2_put_keycode?

Quoted 34 lines
Quoted 23 lines
Quoted 23 lines
hw/input/ps2.c +12 -0
     ps2_queue(&s->common, keycode);
+
+    if (auto_repeat) {
+        s->repeat_key = keycode;
+        /* delay a while before first repeat */
+        qemu_mod_timer(repeat_timer, qemu_get_clock_ns(vm_clock) +
+                       muldiv64(get_ticks_per_sec(), s->repeat_delay, 1000));
+    }
 }
 
 uint32_t ps2_read_data(void *opaque)
@@ -213,6 +242,11 @@ static void ps2_reset_keyboard(PS2KbdState *s)
 
 void ps2_write_keyboard(void *opaque, int val)
 {
+    /* repeat period/delay table from kernel (drivers/input/keyboard/atkbd.c) */
+    const short period[32] = { 33,  37,  42,  46,  50,  54,  58,  63,  67,  75,
+                83,  92, 100, 109, 116, 125, 133, 149, 167, 182, 200, 217, 232,
+                250, 270, 303, 333, 370, 400, 435, 470, 500 };
+    const short delay[4] = { 250, 500, 750, 1000 };
     PS2KbdState *s = (PS2KbdState *)opaque;
 
Quoted 11 lines

hw/input/ps2.c void ps2_write_keyboard(void *opaque, int val) · line 328

Quoted 9 lines
hw/input/ps2.c +4 -0
     switch(s->common.write_cmd) {
@@ -288,6 +322,11 @@ void ps2_write_keyboard(void *opaque, int val)
         s->common.write_cmd = -1;
         break;
     case KBD_CMD_SET_RATE:
+       /* Bit0-4 specifies the repeat rate */
+        s->repeat_period = period[val & 0x1f];
+       /* Bit5-6 bit specifies the delay time */
+        s->repeat_delay = delay[(val & 0x60) >> 5];

s/(val & 0x60) >> 5/(val & 0x60) >> 5 & 0x4/

Quoted 3 lines
                                          ^^ 0x3 ?

    val >> 5 & 0x3

Oh, yes should be '& 0x3'

author
#4 lore raw
Quoted 52 lines · li guang
Quoted 42 lines · 在 2013-05-16四的 14:58 +0800,Amos Kong写道:
Quoted 5 lines · li guang
Quoted 4 lines · 在 2013-05-16四的 12:30 +0800,Amos Kong写道:

Guest driver sets repeat rate and delay time by KBD_CMD_SET_RATE, but ps2 backend doesn't process it and no auto-repeat implementation. This patch adds support of auto-repeat feature.

Quoted 8 lines
Quoted 8 lines

Guest ps2 driver sets autorepeat to fastest possible in reset,
period: 250ms, delay: 33ms

Tested by 'sendkey' monitor command.

referenced: http://www.computer-engineering.org/ps2keyboard/

Hi, Li guang

Quoted 2 lines

theoretically, we have to check if the typematic key is in break now, if so, we will not do repeat anymore.

You mean key is released? I checked by '~keycode & 0x80', stop repeat when key is release.

Quoted 2 lines

don't you think we have a chance that new key can come in during waiting?

When the new key (press) comes, repeat_timer will be modified by qemu_mod_timer(), original repate will be end.

Quoted 13 lines
Quoted 13 lines
hw/input/ps2.c +9 -0
+    if (auto_repeat) {
+        qemu_mod_timer(repeat_timer, qemu_get_clock_ns(vm_clock) +
+                       muldiv64(get_ticks_per_sec(), s->repeat_period,
+                       1000));
+        ps2_queue(&s->common, s->repeat_key);
+    }
+}
+
+
 /*
    keycode is expressed as follow:
    bit 7    - 0 key pressed, 1 = key released
@@ -167,7 +186,17 @@ static void ps2_put_keycode(void *opaque, int keycode)
Quoted 8 lines
Quoted 6 lines

hw/input/ps2.c static void ps2_put_keycode(void *opaque, int keycode) · line 191

Quoted 6 lines
hw/input/ps2.c +3 -0
             keycode = ps2_raw_keycode_set3[keycode & 0x7f];
         }
       }
+
+    /* only auto-repeat press event */
+    auto_repeat = ~keycode & 0x80;

^^^

Quoted 5 lines

case:

1. new key pressed
2. enter ps2_put_keycode
3. previous timer timeout

I guess it's repeat_timer

Quoted 3 lines

4. enter repeat_ps2_queue
5. put previous keycode in queue
6. back to ps2_put_keycode

back? repeat_ps_queue() is called in background.

Quoted 4 lines

7. check auto_repeat

so, an obsolete key comes up.
can timer interrupt ps2_put_keycode?

no.

Signature · 1 line
			Amos.
#5 lore raw
In reply to Amos Kong

Il 16/05/2013 08:58, Amos Kong ha scritto:

Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

theoretically, we have to check if the typematic key is in break now, if so, we will not do repeat anymore.

You mean key is released? I checked by '~keycode & 0x80', stop repeat when key is release.

BTW, !(keycode & 0x80) is more readable.

Paolo

#6 lore raw
In reply to Amos Kong
Quoted 59 lines · Amos Kong

Guest driver sets repeat rate and delay time by KBD_CMD_SET_RATE, but ps2 backend doesn't process it and no auto-repeat implementation. This patch adds support of auto-repeat feature.

Guest ps2 driver sets autorepeat to fastest possible in reset,
period: 250ms, delay: 33ms

Tested by 'sendkey' monitor command.

referenced: http://www.computer-engineering.org/ps2keyboard/

---
 hw/input/ps2.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
hw/input/ps2.c +21 -0
index 3412079..1cfe055 100644
@@ -94,6 +94,9 @@ typedef struct {
     int translate;
     int scancode_set; /* 1=XT, 2=AT, 3=PS/2 */
     int ledstate;
+    int repeat_period; /* typematic period, ms */
+    int repeat_delay; /* typematic delay, ms */
+    int repeat_key; /* keycode to repeat */
 } PS2KbdState;
 
 typedef struct {
@@ -146,6 +149,22 @@ void ps2_queue(void *opaque, int b)
     s->update_irq(s->update_arg, 1);
 }
 
+static QEMUTimer *repeat_timer;
+static bool auto_repeat;
+
+static void repeat_ps2_queue(void *opaque)
+{
+    PS2KbdState *s = opaque;
+
+    if (auto_repeat) {
+        qemu_mod_timer(repeat_timer, qemu_get_clock_ns(vm_clock) +
+                       muldiv64(get_ticks_per_sec(), s->repeat_period,
+                       1000));
+        ps2_queue(&s->common, s->repeat_key);
+    }
+}
+
+
 /*
    keycode is expressed as follow:
    bit 7    - 0 key pressed, 1 = key released
@@ -167,7 +186,17 @@ static void ps2_put_keycode(void *opaque, int keycode)
             keycode = ps2_raw_keycode_set3[keycode & 0x7f];
         }
       }
+
+    /* only auto-repeat press event */

hw/input/ps2.c static void ps2_put_keycode(void *opaque, int keycode) · line 200

Quoted 10 lines
hw/input/ps2.c +8 -0
+    auto_repeat = ~keycode & 0x80;
     ps2_queue(&s->common, keycode);
+
+    if (auto_repeat) {
+        s->repeat_key = keycode;
+        /* delay a while before first repeat */
+        qemu_mod_timer(repeat_timer, qemu_get_clock_ns(vm_clock) +
+                       muldiv64(get_ticks_per_sec(), s->repeat_delay, 1000));
+    }
 }

Not familiar with ps2, but don't we need something to make this safe after migration?

Quoted 43 lines
hw/input/ps2.c +13 -0
 uint32_t ps2_read_data(void *opaque)
@@ -213,6 +242,11 @@ static void ps2_reset_keyboard(PS2KbdState *s)
 
 void ps2_write_keyboard(void *opaque, int val)
 {
+    /* repeat period/delay table from kernel (drivers/input/keyboard/atkbd.c) */
+    const short period[32] = { 33,  37,  42,  46,  50,  54,  58,  63,  67,  75,
+                83,  92, 100, 109, 116, 125, 133, 149, 167, 182, 200, 217, 232,
+                250, 270, 303, 333, 370, 400, 435, 470, 500 };
+    const short delay[4] = { 250, 500, 750, 1000 };
     PS2KbdState *s = (PS2KbdState *)opaque;
 
     switch(s->common.write_cmd) {
@@ -288,6 +322,11 @@ void ps2_write_keyboard(void *opaque, int val)
         s->common.write_cmd = -1;
         break;
     case KBD_CMD_SET_RATE:
+       /* Bit0-4 specifies the repeat rate */
+        s->repeat_period = period[val & 0x1f];
+       /* Bit5-6 bit specifies the delay time */
+        s->repeat_delay = delay[(val & 0x60) >> 5];
+
         ps2_queue(&s->common, KBD_REPLY_ACK);
         s->common.write_cmd = -1;
         break;
@@ -536,6 +575,8 @@ static void ps2_kbd_reset(void *opaque)
     s->scan_enabled = 0;
     s->translate = 0;
     s->scancode_set = 0;
+    s->repeat_period = 92;
+    s->repeat_delay = 500;
 }
 
 static void ps2_mouse_reset(void *opaque)
@@ -660,6 +701,7 @@ void *ps2_kbd_init(void (*update_irq)(void *, int), void *update_arg)
     vmstate_register(NULL, 0, &vmstate_ps2_keyboard, s);
     qemu_add_kbd_event_handler(ps2_put_keycode, s);
     qemu_register_reset(ps2_kbd_reset, s);
+    repeat_timer = qemu_new_timer_ns(vm_clock, repeat_ps2_queue, s);
     return s;
 }
 
#7 lore raw
In reply to Amos Kong
Quoted 19 lines · Amos Kong

Guest driver sets repeat rate and delay time by KBD_CMD_SET_RATE, but ps2 backend doesn't process it and no auto-repeat implementation. This patch adds support of auto-repeat feature.

hw/input/ps2.c +3 -0
index 3412079..1cfe055 100644
@@ -94,6 +94,9 @@ typedef struct {
     int translate;
     int scancode_set; /* 1=XT, 2=AT, 3=PS/2 */
     int ledstate;
+    int repeat_period; /* typematic period, ms */
+    int repeat_delay; /* typematic delay, ms */
+    int repeat_key; /* keycode to repeat */
 } PS2KbdState;
 
 typedef struct {

hw/input/ps2.c void ps2_queue(void *opaque, int b) · line 153

Quoted 5 lines
hw/input/ps2.c +2 -0
@@ -146,6 +149,22 @@ void ps2_queue(void *opaque, int b)
     s->update_irq(s->update_arg, 1);
 }
 
+static QEMUTimer *repeat_timer;
+static bool auto_repeat;

These shouldn't be static -- what would happen on a system with two ps2 keyboard models in it?

You need to reset your qemu_timer in the ps2 reset handler, as well; otherwise it could go off unexpectedly after a reset. (Though perhaps not if we're simulating a human with their finger held down on the key...)

thanks
-- PMM

author
#8 lore raw
Quoted 23 lines · Peter Maydell
Quoted 22 lines · Amos Kong

Guest driver sets repeat rate and delay time by KBD_CMD_SET_RATE, but ps2 backend doesn't process it and no auto-repeat implementation. This patch adds support of auto-repeat feature.

hw/input/ps2.c +3 -0
index 3412079..1cfe055 100644
@@ -94,6 +94,9 @@ typedef struct {
     int translate;
     int scancode_set; /* 1=XT, 2=AT, 3=PS/2 */
     int ledstate;
+    int repeat_period; /* typematic period, ms */
+    int repeat_delay; /* typematic delay, ms */
+    int repeat_key; /* keycode to repeat */
 } PS2KbdState;
 
 typedef struct {
@@ -146,6 +149,22 @@ void ps2_queue(void *opaque, int b)
     s->update_irq(s->update_arg, 1);
 }
 
Quoted 5 lines

hw/input/ps2.c void ps2_queue(void *opaque, int b) · line 153

Quoted 2 lines
hw/input/ps2.c +2 -0
+static QEMUTimer *repeat_timer;
+static bool auto_repeat;

These shouldn't be static -- what would happen on a system with two ps2 keyboard models in it?

I will move them to PS2KbdState.

For the migrate issue mentioned by jason, the keyboard state (repeat_period/repeat_delay/etc) are configured by guest, it should be saved to vmstate and migrated to dest vm.

If we want the unfinished repeat continue, repeat-timer also should be migrated, but another key_timer in ui/input.c for send-key could not be migrated, it means the release event will be lost.

Do we need to migrate the keyboard state?

Quoted 4 lines

You need to reset your qemu_timer in the ps2 reset handler, as well; otherwise it could go off unexpectedly after a reset. (Though perhaps not if we're simulating a human with their finger held down on the key...)

Ok

Quoted 2 lines

thanks
-- PMM

Signature · 1 line
			Amos.
#9 lore raw
In reply to Amos Kong
Quoted 54 lines · Amos Kong

Guest driver sets repeat rate and delay time by KBD_CMD_SET_RATE, but ps2 backend doesn't process it and no auto-repeat implementation. This patch adds support of auto-repeat feature.

Guest ps2 driver sets autorepeat to fastest possible in reset,
period: 250ms, delay: 33ms

Tested by 'sendkey' monitor command.

referenced: http://www.computer-engineering.org/ps2keyboard/

---
  hw/input/ps2.c | 42 ++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 42 insertions(+)
hw/input/ps2.c +19 -0
index 3412079..1cfe055 100644
@@ -94,6 +94,9 @@ typedef struct {
      int translate;
      int scancode_set; /* 1=XT, 2=AT, 3=PS/2 */
      int ledstate;
+    int repeat_period; /* typematic period, ms */
+    int repeat_delay; /* typematic delay, ms */
+    int repeat_key; /* keycode to repeat */
  } PS2KbdState;
 
  typedef struct {
@@ -146,6 +149,22 @@ void ps2_queue(void *opaque, int b)
      s->update_irq(s->update_arg, 1);
  }
 
+static QEMUTimer *repeat_timer;
+static bool auto_repeat;
+
+static void repeat_ps2_queue(void *opaque)
+{
+    PS2KbdState *s = opaque;
+
+    if (auto_repeat) {
+        qemu_mod_timer(repeat_timer, qemu_get_clock_ns(vm_clock) +
+                       muldiv64(get_ticks_per_sec(), s->repeat_period,
+                       1000));
+        ps2_queue(&s->common, s->repeat_key);
+    }
+}
+
+
  /*
     keycode is expressed as follow:
     bit 7    - 0 key pressed, 1 = key released

hw/input/ps2.c static void ps2_put_keycode(void *opaque, int keycode) · line 191

Quoted 6 lines
hw/input/ps2.c +3 -0
@@ -167,7 +186,17 @@ static void ps2_put_keycode(void *opaque, int keycode)
              keycode = ps2_raw_keycode_set3[keycode & 0x7f];
          }
        }
+
+    /* only auto-repeat press event */
+    auto_repeat = ~keycode & 0x80;

Does this check allow to distinguish the difference between auto-repeat and actual repeated entry by the user?

Quoted 52 lines
hw/input/ps2.c +20 -0
      ps2_queue(&s->common, keycode);
+
+    if (auto_repeat) {
+        s->repeat_key = keycode;
+        /* delay a while before first repeat */
+        qemu_mod_timer(repeat_timer, qemu_get_clock_ns(vm_clock) +
+                       muldiv64(get_ticks_per_sec(), s->repeat_delay, 1000));
+    }
  }
 
  uint32_t ps2_read_data(void *opaque)
@@ -213,6 +242,11 @@ static void ps2_reset_keyboard(PS2KbdState *s)
 
  void ps2_write_keyboard(void *opaque, int val)
  {
+    /* repeat period/delay table from kernel (drivers/input/keyboard/atkbd.c) */
+    const short period[32] = { 33,  37,  42,  46,  50,  54,  58,  63,  67,  75,
+                83,  92, 100, 109, 116, 125, 133, 149, 167, 182, 200, 217, 232,
+                250, 270, 303, 333, 370, 400, 435, 470, 500 };
+    const short delay[4] = { 250, 500, 750, 1000 };
      PS2KbdState *s = (PS2KbdState *)opaque;
 
      switch(s->common.write_cmd) {
@@ -288,6 +322,11 @@ void ps2_write_keyboard(void *opaque, int val)
          s->common.write_cmd = -1;
          break;
      case KBD_CMD_SET_RATE:
+       /* Bit0-4 specifies the repeat rate */
+        s->repeat_period = period[val & 0x1f];
+       /* Bit5-6 bit specifies the delay time */
+        s->repeat_delay = delay[(val & 0x60) >> 5];
+
          ps2_queue(&s->common, KBD_REPLY_ACK);
          s->common.write_cmd = -1;
          break;
@@ -536,6 +575,8 @@ static void ps2_kbd_reset(void *opaque)
      s->scan_enabled = 0;
      s->translate = 0;
      s->scancode_set = 0;
+    s->repeat_period = 92;
+    s->repeat_delay = 500;
  }
 
  static void ps2_mouse_reset(void *opaque)
@@ -660,6 +701,7 @@ void *ps2_kbd_init(void (*update_irq)(void *, int), void *update_arg)
      vmstate_register(NULL, 0, &vmstate_ps2_keyboard, s);
      qemu_add_kbd_event_handler(ps2_put_keycode, s);
      qemu_register_reset(ps2_kbd_reset, s);
+    repeat_timer = qemu_new_timer_ns(vm_clock, repeat_ps2_queue, s);
      return s;
  }
 
Signature · 1 line
Lei
author
#10 lore raw
Quoted 15 lines · Lei Li
Quoted 12 lines · Amos Kong

Guest driver sets repeat rate and delay time by KBD_CMD_SET_RATE, but ps2 backend doesn't process it and no auto-repeat implementation. This patch adds support of auto-repeat feature.

Guest ps2 driver sets autorepeat to fastest possible in reset,
period: 250ms, delay: 33ms

Tested by 'sendkey' monitor command.

referenced: http://www.computer-engineering.org/ps2keyboard/

Quoted 10 lines

hw/input/ps2.c static void ps2_put_keycode(void *opaque, int keycode) · line 191

Quoted 10 lines

/*
keycode is expressed as follow:
bit 7 - 0 key pressed, 1 = key released

hw/input/ps2.c +3 -0
@@ -167,7 +186,17 @@ static void ps2_put_keycode(void *opaque, int keycode)
             keycode = ps2_raw_keycode_set3[keycode & 0x7f];
         }
       }
+
+    /* only auto-repeat press event */
+    auto_repeat = ~keycode & 0x80;

Hi Lei,

Quoted 2 lines

Does this check allow to distinguish the difference between auto-repeat and actual repeated entry by the user?

Actual repeat by user:
press event
release event
press event
release event
press event
release event

Auto-repeat example:
press event
press event
press event
release event

so here we check if it's a press event, only set repeat_timer for press event. When we get release event, we just stop repeat action.

Quoted 9 lines
Quoted 9 lines
hw/input/ps2.c +7 -0
     ps2_queue(&s->common, keycode);
+
+    if (auto_repeat) {
+        s->repeat_key = keycode;
+        /* delay a while before first repeat */
+        qemu_mod_timer(repeat_timer, qemu_get_clock_ns(vm_clock) +
+                       muldiv64(get_ticks_per_sec(), s->repeat_delay, 1000));
+    }
 }
Signature · 1 line
			Amos.
#11 lore raw
Quoted 37 lines · Amos Kong
Quoted 14 lines · Lei Li
Quoted 12 lines · Amos Kong

Guest driver sets repeat rate and delay time by KBD_CMD_SET_RATE, but ps2 backend doesn't process it and no auto-repeat implementation. This patch adds support of auto-repeat feature.

Guest ps2 driver sets autorepeat to fastest possible in reset,
period: 250ms, delay: 33ms

Tested by 'sendkey' monitor command.

referenced: http://www.computer-engineering.org/ps2keyboard/

Quoted 10 lines

hw/input/ps2.c static void ps2_put_keycode(void *opaque, int keycode) · line 191

Quoted 10 lines

/*
keycode is expressed as follow:
bit 7 - 0 key pressed, 1 = key released

hw/input/ps2.c +3 -0
@@ -167,7 +186,17 @@ static void ps2_put_keycode(void *opaque, int keycode)
              keycode = ps2_raw_keycode_set3[keycode & 0x7f];
          }
        }
+
+    /* only auto-repeat press event */
+    auto_repeat = ~keycode & 0x80;

Hi Lei,

Quoted 2 lines

Does this check allow to distinguish the difference between auto-repeat and actual repeated entry by the user?

Actual repeat by user:
press event
release event
press event
release event
press event
release event

Quoted 5 lines

Auto-repeat example:
press event
press event
press event
release event

On what platform?

AFAIK, the Auto-repeat event is like below on some GTK-based ||||||||||||environments,||||||||||||

keydown
keypress
keyup
keydown
keypress
keyup|||||||||||||
...
as reference link:

https://developer.mozilla.org/zh-CN/docs/DOM/KeyboardEvent

And on Xwindows:

keypress
keyrelease
keypress
keyrelease
...
as reference link:

http://www.ypass.net/blog/2009/06/detecting-xlibs-keyboard-auto-repeat-functionality-and-how-to-fix-it/

This would cause it's hard to distinguish them. But looks like the links above is a little out of time, and I am not sure if the auto-repeat behaviour on such platforms has been changed. :)
|||||||||||||

Quoted 14 lines

so here we check if it's a press event, only set repeat_timer for press event. When we get release event, we just stop repeat action.

Quoted 9 lines
Quoted 9 lines
hw/input/ps2.c +7 -0
      ps2_queue(&s->common, keycode);
+
+    if (auto_repeat) {
+        s->repeat_key = keycode;
+        /* delay a while before first repeat */
+        qemu_mod_timer(repeat_timer, qemu_get_clock_ns(vm_clock) +
+                       muldiv64(get_ticks_per_sec(), s->repeat_delay, 1000));
+    }
  }
Signature · 1 line
Lei
author
#12 lore raw
Quoted 38 lines · Lei Li
Quoted 37 lines · Amos Kong
Quoted 14 lines · Lei Li
Quoted 12 lines · Amos Kong

Guest driver sets repeat rate and delay time by KBD_CMD_SET_RATE, but ps2 backend doesn't process it and no auto-repeat implementation. This patch adds support of auto-repeat feature.

Guest ps2 driver sets autorepeat to fastest possible in reset,
period: 250ms, delay: 33ms

Tested by 'sendkey' monitor command.

referenced: http://www.computer-engineering.org/ps2keyboard/

Quoted 10 lines

hw/input/ps2.c static void ps2_put_keycode(void *opaque, int keycode) · line 191

Quoted 10 lines

/*
keycode is expressed as follow:
bit 7 - 0 key pressed, 1 = key released

hw/input/ps2.c +3 -0
@@ -167,7 +186,17 @@ static void ps2_put_keycode(void *opaque, int keycode)
             keycode = ps2_raw_keycode_set3[keycode & 0x7f];
         }
       }
+
+    /* only auto-repeat press event */
+    auto_repeat = ~keycode & 0x80;

Hi Lei,

Quoted 2 lines

Does this check allow to distinguish the difference between auto-repeat and actual repeated entry by the user?

Actual repeat by user:
press event
release event
press event
release event
press event
release event

Quoted 5 lines
Quoted 5 lines

Auto-repeat example:
press event
press event
press event
release event

Hi Lei,

Quoted 1 line

On what platform?

Fedora 18 @ thinkpad t430s

[root@t430s amos]# showkey (hold 'a')
akeycode 30 press
aaaaaaaaaaakeycode 30 press

keycode  30 press
keycode  30 press
keycode  30 press
keycode  30 press
keycode  30 press
keycode  30 press
keycode  30 press
keycode  30 press
keycode  30 press
keycode  30 press
keycode  30 press
keycode  30 press

aakeycode 30 press
keycode 30 press
keycode 30 release <----(one release event in the end)

Qemu VM (rhel6, using vnc/ SDL) can get same result.

Quoted 12 lines

AFAIK, the Auto-repeat event is like below on some GTK-based ||||||||||||environments,||||||||||||

keydown
keypress
keyup
keydown
keypress
keyup|||||||||||||
...
as reference link:

Quoted 1 line

https://developer.mozilla.org/zh-CN/docs/DOM/KeyboardEvent

===== Auto-repeat handling (it's also mentioned in mozilla page)

When a key is pressed and held down, it begins to auto-repeat. This results in a sequence of events similar to the following being dispatched:

keydown
keypress
keydown
keypress
<<repeating until the user releases the key>>
keyup <----(only one up event in the end)

Quoted 10 lines

And on Xwindows:

keypress
keyrelease
keypress
keyrelease
...
as reference link:

http://www.ypass.net/blog/2009/06/detecting-xlibs-keyboard-auto-repeat-functionality-and-how-to-fix-it/

"""Just what we’d expect, a bunch of KeyPress Events and one KeyRelease event. But that’s not how it works in X."""

So pppppppR is expected.

From: http://www.computer-engineering.org/ps2keyboard/

When you press and hold down a key, that key becomes typematic, which means the keyboard will _keep_ sending that key's _make code_ until the key is released or another key is pressed.

(repeatedly send the make code, only one break code in the end)

Quoted 4 lines

This would cause it's hard to distinguish them. But looks like the links above is a little out of time, and I am not sure if the auto-repeat behaviour on such platforms has been changed. :)
|||||||||||||

Both of them works, but the repeated release events are redundant (no risk).

Signature · 1 line
			Amos.
#13 lore raw

Il 16/05/2013 22:37, Amos Kong ha scritto:

Quoted 110 lines · Lei Li
Quoted 38 lines · Amos Kong
Quoted 37 lines · Lei Li
Quoted 14 lines · Amos Kong
Quoted 12 lines

Guest driver sets repeat rate and delay time by KBD_CMD_SET_RATE, but ps2 backend doesn't process it and no auto-repeat implementation. This patch adds support of auto-repeat feature.

Guest ps2 driver sets autorepeat to fastest possible in reset,
period: 250ms, delay: 33ms

Tested by 'sendkey' monitor command.

referenced: http://www.computer-engineering.org/ps2keyboard/

Quoted 10 lines

hw/input/ps2.c static void ps2_put_keycode(void *opaque, int keycode) · line 191

Quoted 10 lines

/*
keycode is expressed as follow:
bit 7 - 0 key pressed, 1 = key released

hw/input/ps2.c +3 -0
@@ -167,7 +186,17 @@ static void ps2_put_keycode(void *opaque, int keycode)
             keycode = ps2_raw_keycode_set3[keycode & 0x7f];
         }
       }
+
+    /* only auto-repeat press event */
+    auto_repeat = ~keycode & 0x80;

Hi Lei,

Quoted 2 lines

Does this check allow to distinguish the difference between auto-repeat and actual repeated entry by the user?

Actual repeat by user:
press event
release event
press event
release event
press event
release event

Quoted 5 lines
Quoted 5 lines

Auto-repeat example:
press event
press event
press event
release event

Hi Lei,

Quoted 1 line

On what platform?

Fedora 18 @ thinkpad t430s

[root@t430s amos]# showkey (hold 'a')
akeycode 30 press
aaaaaaaaaaakeycode 30 press

keycode  30 press
keycode  30 press
keycode  30 press
keycode  30 press
keycode  30 press
keycode  30 press
keycode  30 press
keycode  30 press
keycode  30 press
keycode  30 press
keycode  30 press
keycode  30 press

aakeycode 30 press
keycode 30 press
keycode 30 release <----(one release event in the end)

Qemu VM (rhel6, using vnc/ SDL) can get same result.

Quoted 12 lines

AFAIK, the Auto-repeat event is like below on some GTK-based ||||||||||||environments,||||||||||||

keydown
keypress
keyup
keydown
keypress
keyup|||||||||||||
...
as reference link:

Quoted 1 line

https://developer.mozilla.org/zh-CN/docs/DOM/KeyboardEvent

===== Auto-repeat handling (it's also mentioned in mozilla page)

When a key is pressed and held down, it begins to auto-repeat. This results in a sequence of events similar to the following being dispatched:

keydown
keypress
keydown
keypress
<<repeating until the user releases the key>>
keyup <----(only one up event in the end)

Quoted 9 lines

And on Xwindows:

keypress
keyrelease
keypress
keyrelease
...
as reference link:

Quoted 4 lines
Quoted 1 line

http://www.ypass.net/blog/2009/06/detecting-xlibs-keyboard-auto-repeat-functionality-and-how-to-fix-it/

"""Just what we’d expect, a bunch of KeyPress Events and one KeyRelease event. But that’s not how it works in X."""

... In XWindows, you get a KeyRelease for every KeyPress Event. In X, it looks something like this:

PRPRPRPRPRPRPRPR

Can you test your patch with all of VNC, SDL and GTK+?

Paolo

#14 lore raw
Quoted 4 lines · Paolo Bonzini

... In XWindows, you get a KeyRelease for every KeyPress Event. In X, it looks something like this:

PRPRPRPRPRPRPRPR

Shouldn't we be abstracting this platform difference out in the ui layer, rather than having to deal with it in the ps2 device model? That is, we should define what our key-repeat model is for the QEMU keyboard-event-handler API, and then make sure all our UI frontends (gtk, sdl, cocoa, etc) do what we require...

thanks
-- PMM

#15 lore raw

Il 16/05/2013 17:17, Peter Maydell ha scritto:

Quoted 12 lines · Paolo Bonzini
Quoted 4 lines

... In XWindows, you get a KeyRelease for every KeyPress Event. In X, it looks something like this:

PRPRPRPRPRPRPRPR

Shouldn't we be abstracting this platform difference out in the ui layer, rather than having to deal with it in the ps2 device model? That is, we should define what our key-repeat model is for the QEMU keyboard-event-handler API, and then make sure all our UI frontends (gtk, sdl, cocoa, etc) do what we require...

Yes, I am asking Amos to check which of our frontends comply.

It needs to be checked in the host, because Linux guests emulate autorepeat anyway. Or you can test with FreeDOS.

Paolo

author
#16 lore raw
Quoted 14 lines · Paolo Bonzini

Il 16/05/2013 17:17, Peter Maydell ha scritto:

Quoted 12 lines · Paolo Bonzini
Quoted 4 lines

... In XWindows, you get a KeyRelease for every KeyPress Event. In X, it looks something like this:

PRPRPRPRPRPRPRPR

Shouldn't we be abstracting this platform difference out in the ui layer, rather than having to deal with it in the ps2 device model? That is, we should define what our key-repeat model is for the QEMU keyboard-event-handler API, and then make sure all our UI frontends (gtk, sdl, cocoa, etc) do what we require...

Quoted 4 lines

Yes, I am asking Amos to check which of our frontends comply.

It needs to be checked in the host, because Linux guests emulate autorepeat anyway. Or you can test with FreeDOS.

Please correct me if something is wrong, thanks.

When we use VNC/SPICE/SDL, vm Window will captured the key events, then qemu process the events and transfer to guest through emulated PS2 device.

When we hold the key in keyboard of host, real keyboard or host OS will do auto-repeat. vm Window will transfer repeated events to guest. In this case, it seems the auto-repeat of emulated PS2 device doesn't needed.

But when keyboard/host os doesn't support auto-repeat, or we directly send event from monitor by 'sendkey'. held key could not be repeated without auto-repeat support of emulated PS2 device.

When I use (Lenovo t430s & Fedoar 18 host), when I hold the real key, qemu will get PPPPPPPR style events queue from host.

Guest (RHEL6/Win7/WinXp) & (SDL & VNC & SPICE) works well.

Others:
1. I read the keyboard driver in linux kernel, soft_auto-repeat was implemented in linux ps2 driver is PPPPPPPR style.

2. PPPPPPPPR style is described in freescale hardware manual:
http://www.freescale.com/files/microcontrollers/doc/ref_manual/DRM014.pdf
"""
1.5.9 PS/2 Scan Codes

Make code or break code is sent when any key is pressed or released. While a key is pressed, its _make code_ is sent out repeatedly and the rate depends on the typematic repeat value.
"""

I will update patches to fix other problem, keep using PPPPPPPR style.

Signature · 1 line
			Amos.
#17 lore raw

Il 21/05/2013 10:33, Amos Kong ha scritto:

Quoted 22 lines · Paolo Bonzini
Quoted 14 lines

Il 16/05/2013 17:17, Peter Maydell ha scritto:

Quoted 12 lines · Paolo Bonzini
Quoted 4 lines

... In XWindows, you get a KeyRelease for every KeyPress Event. In X, it looks something like this:

PRPRPRPRPRPRPRPR

Shouldn't we be abstracting this platform difference out in the ui layer, rather than having to deal with it in the ps2 device model? That is, we should define what our key-repeat model is for the QEMU keyboard-event-handler API, and then make sure all our UI frontends (gtk, sdl, cocoa, etc) do what we require...

Quoted 4 lines

Yes, I am asking Amos to check which of our frontends comply.

It needs to be checked in the host, because Linux guests emulate autorepeat anyway. Or you can test with FreeDOS.

Please correct me if something is wrong, thanks.

Quoted 8 lines

When we use VNC/SPICE/SDL, vm Window will captured the key events, then qemu process the events and transfer to guest through emulated PS2 device.

When we hold the key in keyboard of host, real keyboard or host OS will do auto-repeat. vm Window will transfer repeated events to guest. In this case, it seems the auto-repeat of emulated PS2 device doesn't needed.

If you can make emulated autorepeat work also with VNC/SDL/SPICE, it would be much better, because then the guest can choose to enable or disable the autorepeat as desired.

That's why I mentioned testing with FreeDOS, which does no emulation. You can find DOS programs to change the typematic rate.

Paolo

Quoted 27 lines

But when keyboard/host os doesn't support auto-repeat, or we directly send event from monitor by 'sendkey'. held key could not be repeated without auto-repeat support of emulated PS2 device.

When I use (Lenovo t430s & Fedoar 18 host), when I hold the real key, qemu will get PPPPPPPR style events queue from host.

Guest (RHEL6/Win7/WinXp) & (SDL & VNC & SPICE) works well.

Others:
1. I read the keyboard driver in linux kernel, soft_auto-repeat was implemented in linux ps2 driver is PPPPPPPR style.

2. PPPPPPPPR style is described in freescale hardware manual:
http://www.freescale.com/files/microcontrollers/doc/ref_manual/DRM014.pdf
"""
1.5.9 PS/2 Scan Codes

Make code or break code is sent when any key is pressed or released. While a key is pressed, its _make code_ is sent out repeatedly and the rate depends on the typematic repeat value.
"""

I will update patches to fix other problem, keep using PPPPPPPR style.

author
#18 lore raw
Quoted 32 lines · Paolo Bonzini

Il 21/05/2013 10:33, Amos Kong ha scritto:

Quoted 22 lines · Paolo Bonzini
Quoted 14 lines

Il 16/05/2013 17:17, Peter Maydell ha scritto:

Quoted 12 lines · Paolo Bonzini
Quoted 4 lines

... In XWindows, you get a KeyRelease for every KeyPress Event. In X, it looks something like this:

PRPRPRPRPRPRPRPR

Shouldn't we be abstracting this platform difference out in the ui layer, rather than having to deal with it in the ps2 device model? That is, we should define what our key-repeat model is for the QEMU keyboard-event-handler API, and then make sure all our UI frontends (gtk, sdl, cocoa, etc) do what we require...

Quoted 4 lines

Yes, I am asking Amos to check which of our frontends comply.

It needs to be checked in the host, because Linux guests emulate autorepeat anyway. Or you can test with FreeDOS.

Please correct me if something is wrong, thanks.

Quoted 8 lines

When we use VNC/SPICE/SDL, vm Window will captured the key events, then qemu process the events and transfer to guest through emulated PS2 device.

When we hold the key in keyboard of host, real keyboard or host OS will do auto-repeat. vm Window will transfer repeated events to guest. In this case, it seems the auto-repeat of emulated PS2 device doesn't needed.

Quoted 6 lines

If you can make emulated autorepeat work also with VNC/SDL/SPICE, it would be much better, because then the guest can choose to enable or disable the autorepeat as desired.

That's why I mentioned testing with FreeDOS, which does no emulation. You can find DOS programs to change the typematic rate.

Yes, if we don't process events from host, the rate set in guest doesn't work for SDL/VNC/SPICE/..

I have fixed it by ignoring continual/repated(same keycode) press events. It works now :)

I just tested by Linux guest (set rate by 'kbdrate -s ..'), will test with FreeDOS.

Amos.

author
#19 lore raw
Quoted 18 lines · Amos Kong
Quoted 15 lines · Paolo Bonzini
Quoted 10 lines

Please correct me if something is wrong, thanks.

When we use VNC/SPICE/SDL, vm Window will captured the key events, then qemu process the events and transfer to guest through emulated PS2 device.

When we hold the key in keyboard of host, real keyboard or host OS will do auto-repeat. vm Window will transfer repeated events to guest. In this case, it seems the auto-repeat of emulated PS2 device doesn't needed.

If you can make emulated autorepeat work also with VNC/SDL/SPICE, it would be much better, because then the guest can choose to enable or disable the autorepeat as desired.

Quoted 8 lines
Quoted 2 lines

That's why I mentioned testing with FreeDOS, which does no emulation. You can find DOS programs to change the typematic rate.

Yes, if we don't process events from host, the rate set in guest doesn't work for SDL/VNC/SPICE/..

I have fixed it by ignoring continual/repated(same keycode) press events. It works now :)

When I test with linux guest, set rate by 'kbdrate -r ..', emulated PS2 device can get a keyboard_write (cmd: 0xf3), rate will be set, auto-repeat rate can be controlled.

I also tested by Win7, set rate by 'mode con rate=2',emulated PS2 device can get a keyboard_write (cmd: 0xf3), rate will be set, auto-repeat rate can be controlled.

Is it enough to prove the auto-repeat implemented in ps2 is ok?

Quoted 2 lines

I just tested by Linux guest (set rate by 'kbdrate -s ..'), will test with FreeDOS.

In FreeDOS, I set rate by 'mode con rate=2', got a success prompt. but emulated PS2 device can't get a keyboard_write (cmd: 0xf3) in init stage & when I execute mode command. Auto-repeat always use default rate in qemu-ps2 code.

FreeDOS bug?

Will study it tomorrow.

Signature · 1 line
			Amos.
#20 lore raw

Il 21/05/2013 11:51, Amos Kong ha scritto:

Quoted 39 lines · Amos Kong
Quoted 16 lines · Paolo Bonzini
Quoted 15 lines
Quoted 10 lines

Please correct me if something is wrong, thanks.

When we use VNC/SPICE/SDL, vm Window will captured the key events, then qemu process the events and transfer to guest through emulated PS2 device.

When we hold the key in keyboard of host, real keyboard or host OS will do auto-repeat. vm Window will transfer repeated events to guest. In this case, it seems the auto-repeat of emulated PS2 device doesn't needed.

If you can make emulated autorepeat work also with VNC/SDL/SPICE, it would be much better, because then the guest can choose to enable or disable the autorepeat as desired.

Quoted 8 lines
Quoted 2 lines

That's why I mentioned testing with FreeDOS, which does no emulation. You can find DOS programs to change the typematic rate.

Yes, if we don't process events from host, the rate set in guest doesn't work for SDL/VNC/SPICE/..

I have fixed it by ignoring continual/repated(same keycode) press events. It works now :)

When I test with linux guest, set rate by 'kbdrate -r ..', emulated PS2 device can get a keyboard_write (cmd: 0xf3), rate will be set, auto-repeat rate can be controlled.

I also tested by Win7, set rate by 'mode con rate=2',emulated PS2 device can get a keyboard_write (cmd: 0xf3), rate will be set, auto-repeat rate can be controlled.

Is it enough to prove the auto-repeat implemented in ps2 is ok?

Quoted 2 lines

I just tested by Linux guest (set rate by 'kbdrate -s ..'), will test with FreeDOS.

Quoted 6 lines

In FreeDOS, I set rate by 'mode con rate=2', got a success prompt. but emulated PS2 device can't get a keyboard_write (cmd: 0xf3) in init stage & when I execute mode command. Auto-repeat always use default rate in qemu-ps2 code.

FreeDOS bug?

Probably. Testing Windows is enough, I didn't think of it.

Which backends did you test among SDL/VNC/SPICE/GTK+?

Paolo

#21 lore raw
In reply to Amos Kong

Il 16/05/2013 06:30, Amos Kong ha scritto:

Quoted 21 lines

Guest driver sets repeat rate and delay time by KBD_CMD_SET_RATE, but ps2 backend doesn't process it and no auto-repeat implementation. This patch adds support of auto-repeat feature.

Guest ps2 driver sets autorepeat to fastest possible in reset,
period: 250ms, delay: 33ms

Tested by 'sendkey' monitor command.

referenced: http://www.computer-engineering.org/ps2keyboard/

---
 hw/input/ps2.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
hw/input/ps2.c +0 -0
index 3412079..1cfe055 100644

hw/input/ps2.c typedef struct { · line 98

Quoted 5 lines
hw/input/ps2.c +2 -0
@@ -94,6 +94,9 @@ typedef struct {
     int translate;
     int scancode_set; /* 1=XT, 2=AT, 3=PS/2 */
     int ledstate;
+    int repeat_period; /* typematic period, ms */
+    int repeat_delay; /* typematic delay, ms */

These have to be migrated in a subsection.

hw/input/ps2.c void ps2_queue(void *opaque, int b) · line 152

Quoted 9 lines
hw/input/ps2.c +2 -0
+    int repeat_key; /* keycode to repeat */
 } PS2KbdState;
 
 typedef struct {
@@ -146,6 +149,22 @@ void ps2_queue(void *opaque, int b)
     s->update_irq(s->update_arg, 1);
 }
 
+static QEMUTimer *repeat_timer;

In theory, this timer and repeat_key also need to be migrated. But perhaps not migrating anything is fine. Migration will then stop the autorepeat, which is not a particularly bad thing and may even avoid stuck keys. However, please add a comment, and move the timer into Ps2KbdState instead of having a global.

hw/input/ps2.c void ps2_queue(void *opaque, int b) · line 153

Quoted 1 line
hw/input/ps2.c +1 -0
+static bool auto_repeat;

See below about removing this other global.

Quoted 21 lines
hw/input/ps2.c +14 -0
+static void repeat_ps2_queue(void *opaque)
+{
+    PS2KbdState *s = opaque;
+
+    if (auto_repeat) {
+        qemu_mod_timer(repeat_timer, qemu_get_clock_ns(vm_clock) +
+                       muldiv64(get_ticks_per_sec(), s->repeat_period,
+                       1000));
+        ps2_queue(&s->common, s->repeat_key);
+    }
+}
+
+
 /*
    keycode is expressed as follow:
    bit 7    - 0 key pressed, 1 = key released
@@ -167,7 +186,17 @@ static void ps2_put_keycode(void *opaque, int keycode)
             keycode = ps2_raw_keycode_set3[keycode & 0x7f];
         }
       }
+

hw/input/ps2.c static void ps2_put_keycode(void *opaque, int keycode) · line 199

Quoted 10 lines
hw/input/ps2.c +9 -0
+    /* only auto-repeat press event */
+    auto_repeat = ~keycode & 0x80;
     ps2_queue(&s->common, keycode);
+
+    if (auto_repeat) {
+        s->repeat_key = keycode;
+        /* delay a while before first repeat */
+        qemu_mod_timer(repeat_timer, qemu_get_clock_ns(vm_clock) +
+                       muldiv64(get_ticks_per_sec(), s->repeat_delay, 1000));
+    }

Please del_timer here so you can remove the if above.

Paolo

Quoted 45 lines
hw/input/ps2.c +13 -0
 }
 
 uint32_t ps2_read_data(void *opaque)
@@ -213,6 +242,11 @@ static void ps2_reset_keyboard(PS2KbdState *s)
 
 void ps2_write_keyboard(void *opaque, int val)
 {
+    /* repeat period/delay table from kernel (drivers/input/keyboard/atkbd.c) */
+    const short period[32] = { 33,  37,  42,  46,  50,  54,  58,  63,  67,  75,
+                83,  92, 100, 109, 116, 125, 133, 149, 167, 182, 200, 217, 232,
+                250, 270, 303, 333, 370, 400, 435, 470, 500 };
+    const short delay[4] = { 250, 500, 750, 1000 };
     PS2KbdState *s = (PS2KbdState *)opaque;
 
     switch(s->common.write_cmd) {
@@ -288,6 +322,11 @@ void ps2_write_keyboard(void *opaque, int val)
         s->common.write_cmd = -1;
         break;
     case KBD_CMD_SET_RATE:
+       /* Bit0-4 specifies the repeat rate */
+        s->repeat_period = period[val & 0x1f];
+       /* Bit5-6 bit specifies the delay time */
+        s->repeat_delay = delay[(val & 0x60) >> 5];
+
         ps2_queue(&s->common, KBD_REPLY_ACK);
         s->common.write_cmd = -1;
         break;
@@ -536,6 +575,8 @@ static void ps2_kbd_reset(void *opaque)
     s->scan_enabled = 0;
     s->translate = 0;
     s->scancode_set = 0;
+    s->repeat_period = 92;
+    s->repeat_delay = 500;
 }
 
 static void ps2_mouse_reset(void *opaque)
@@ -660,6 +701,7 @@ void *ps2_kbd_init(void (*update_irq)(void *, int), void *update_arg)
     vmstate_register(NULL, 0, &vmstate_ps2_keyboard, s);
     qemu_add_kbd_event_handler(ps2_put_keycode, s);
     qemu_register_reset(ps2_kbd_reset, s);
+    repeat_timer = qemu_new_timer_ns(vm_clock, repeat_ps2_queue, s);
     return s;
 }