diff mbox series

[2/3] ui/console: replace kbd_timer with chr_accept_input callback

Message ID 20210912125203.7114-2-vr_qemu@t-online.de
State New
Headers show
Series ui/console: chardev backend improvements | expand

Commit Message

Volker Rümelin Sept. 12, 2021, 12:52 p.m. UTC
There's a ChardevClass chr_accept_input() callback function that
can replace the write retry timer.

Signed-off-by: Volker Rümelin <vr_qemu@t-online.de>
---
 ui/console.c | 28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)

Comments

Marc-André Lureau Sept. 12, 2021, 3:59 p.m. UTC | #1
On Sun, Sep 12, 2021 at 5:03 PM Volker Rümelin <vr_qemu@t-online.de> wrote:

> There's a ChardevClass chr_accept_input() callback function that
> can replace the write retry timer.
>
> Signed-off-by: Volker Rümelin <vr_qemu@t-online.de>
>

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>

---
>  ui/console.c | 28 +++++++++++++---------------
>  1 file changed, 13 insertions(+), 15 deletions(-)
>
> diff --git a/ui/console.c b/ui/console.c
> index e6ce29024c..7b276bfc6c 100644
> --- a/ui/console.c
> +++ b/ui/console.c
> @@ -116,7 +116,6 @@ struct QemuConsole {
>      Chardev *chr;
>      /* fifo for key pressed */
>      Fifo8 out_fifo;
> -    QEMUTimer *kbd_timer;
>      CoQueue dump_queue;
>
>      QTAILQ_ENTRY(QemuConsole) next;
> @@ -1106,30 +1105,21 @@ static int vc_chr_write(Chardev *chr, const
> uint8_t *buf, int len)
>      return len;
>  }
>
> -static void kbd_send_chars(void *opaque)
> +static void kbd_send_chars(QemuConsole *s)
>  {
> -    QemuConsole *s = opaque;
>      uint32_t len, avail;
>
>      len = qemu_chr_be_can_write(s->chr);
>      avail = fifo8_num_used(&s->out_fifo);
> -    if (len > avail) {
> -        len = avail;
> -    }
> -    while (len > 0) {
> +    while (len > 0 && avail > 0) {
>          const uint8_t *buf;
>          uint32_t size;
>
> -        buf = fifo8_pop_buf(&s->out_fifo, len, &size);
> +        buf = fifo8_pop_buf(&s->out_fifo, MIN(len, avail), &size);
>          qemu_chr_be_write(s->chr, (uint8_t *)buf, size);
> -        len -= size;
> +        len = qemu_chr_be_can_write(s->chr);
>          avail -= size;
>      }
> -    /* characters are pending: we send them a bit later (XXX:
> -       horrible, should change char device API) */
> -    if (avail > 0) {
> -        timer_mod(s->kbd_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) +
> 1);
> -    }
>  }
>
>  /* called when an ascii key is pressed */
> @@ -2141,6 +2131,14 @@ int qemu_console_get_height(QemuConsole *con, int
> fallback)
>      return con ? surface_height(con->surface) : fallback;
>  }
>
> +static void vc_chr_accept_input(Chardev *chr)
> +{
> +    VCChardev *drv = VC_CHARDEV(chr);
> +    QemuConsole *s = drv->console;
> +
> +    kbd_send_chars(s);
> +}
> +
>  static void vc_chr_set_echo(Chardev *chr, bool echo)
>  {
>      VCChardev *drv = VC_CHARDEV(chr);
> @@ -2189,7 +2187,6 @@ static void text_console_do_init(Chardev *chr,
> DisplayState *ds)
>      int g_height = 24 * FONT_HEIGHT;
>
>      fifo8_create(&s->out_fifo, 16);
> -    s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s);
>      s->ds = ds;
>
>      s->y_displayed = 0;
> @@ -2439,6 +2436,7 @@ static void char_vc_class_init(ObjectClass *oc, void
> *data)
>      cc->parse = qemu_chr_parse_vc;
>      cc->open = vc_chr_open;
>      cc->chr_write = vc_chr_write;
> +    cc->chr_accept_input = vc_chr_accept_input;
>      cc->chr_set_echo = vc_chr_set_echo;
>  }
>
> --
> 2.31.1
>
>
>
diff mbox series

Patch

diff --git a/ui/console.c b/ui/console.c
index e6ce29024c..7b276bfc6c 100644
--- a/ui/console.c
+++ b/ui/console.c
@@ -116,7 +116,6 @@  struct QemuConsole {
     Chardev *chr;
     /* fifo for key pressed */
     Fifo8 out_fifo;
-    QEMUTimer *kbd_timer;
     CoQueue dump_queue;
 
     QTAILQ_ENTRY(QemuConsole) next;
@@ -1106,30 +1105,21 @@  static int vc_chr_write(Chardev *chr, const uint8_t *buf, int len)
     return len;
 }
 
-static void kbd_send_chars(void *opaque)
+static void kbd_send_chars(QemuConsole *s)
 {
-    QemuConsole *s = opaque;
     uint32_t len, avail;
 
     len = qemu_chr_be_can_write(s->chr);
     avail = fifo8_num_used(&s->out_fifo);
-    if (len > avail) {
-        len = avail;
-    }
-    while (len > 0) {
+    while (len > 0 && avail > 0) {
         const uint8_t *buf;
         uint32_t size;
 
-        buf = fifo8_pop_buf(&s->out_fifo, len, &size);
+        buf = fifo8_pop_buf(&s->out_fifo, MIN(len, avail), &size);
         qemu_chr_be_write(s->chr, (uint8_t *)buf, size);
-        len -= size;
+        len = qemu_chr_be_can_write(s->chr);
         avail -= size;
     }
-    /* characters are pending: we send them a bit later (XXX:
-       horrible, should change char device API) */
-    if (avail > 0) {
-        timer_mod(s->kbd_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1);
-    }
 }
 
 /* called when an ascii key is pressed */
@@ -2141,6 +2131,14 @@  int qemu_console_get_height(QemuConsole *con, int fallback)
     return con ? surface_height(con->surface) : fallback;
 }
 
+static void vc_chr_accept_input(Chardev *chr)
+{
+    VCChardev *drv = VC_CHARDEV(chr);
+    QemuConsole *s = drv->console;
+
+    kbd_send_chars(s);
+}
+
 static void vc_chr_set_echo(Chardev *chr, bool echo)
 {
     VCChardev *drv = VC_CHARDEV(chr);
@@ -2189,7 +2187,6 @@  static void text_console_do_init(Chardev *chr, DisplayState *ds)
     int g_height = 24 * FONT_HEIGHT;
 
     fifo8_create(&s->out_fifo, 16);
-    s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s);
     s->ds = ds;
 
     s->y_displayed = 0;
@@ -2439,6 +2436,7 @@  static void char_vc_class_init(ObjectClass *oc, void *data)
     cc->parse = qemu_chr_parse_vc;
     cc->open = vc_chr_open;
     cc->chr_write = vc_chr_write;
+    cc->chr_accept_input = vc_chr_accept_input;
     cc->chr_set_echo = vc_chr_set_echo;
 }