diff mbox series

ui/sdl: Mouse event optimization

Message ID 20241025065205.1222-1-Lei.Huang@amd.com (mailing list archive)
State New
Headers show
Series ui/sdl: Mouse event optimization | expand

Commit Message

Lei Huang Oct. 25, 2024, 6:52 a.m. UTC
Use a convergence factor to make the VM's input
global coordinates more closely approach the global
coordinates of DOM0.

Change-Id: I2c3f12f1fe7dfb9306d1fc40c4fd4d299937f4c6
Signed-off-by: Lei Huang <Lei.Huang@amd.com>
---
 ui/sdl2.c | 32 ++++++++++++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)

Comments

BALATON Zoltan Oct. 25, 2024, 9:30 a.m. UTC | #1
On Fri, 25 Oct 2024, Lei Huang wrote:
> Use a convergence factor to make the VM's input
> global coordinates more closely approach the global
> coordinates of DOM0.

Dom0 is some Xen terminology. Do you mean "host" which is more often used 
in QEMU?

> Change-Id: I2c3f12f1fe7dfb9306d1fc40c4fd4d299937f4c6
> Signed-off-by: Lei Huang <Lei.Huang@amd.com>
> ---
> ui/sdl2.c | 32 ++++++++++++++++++++++++++++++--
> 1 file changed, 30 insertions(+), 2 deletions(-)
>
> diff --git a/ui/sdl2.c b/ui/sdl2.c
> index bd4f5a9da14..8f504dd8727 100644
> --- a/ui/sdl2.c
> +++ b/ui/sdl2.c
> @@ -303,6 +303,34 @@ static void sdl_mouse_mode_change(Notifier *notify, void *data)
>     }
> }
>
> +/*
> + *guest_x and guest_y represent the global coordinates on the VM side,
> + *while x and y represent the global coordinates on the dom0 side.
> + *The goal of this entire process is to align the global coordinates of
> + *the VM with those of dom0 using dx and dy. The current approach aims
> + *for precise calibration in once attempt; however, because guest_x

"in one attempt" also add a space after the * at the beginning of lines.

> + *and guest_y are non-zero values, they are not accurate values when
> + *they are counted out to become negative.Therefore, achieving perfect
> + *alignment in one attempt is impossible.Since the same calibration method
> + *is used each time, repeated attempts cannot achieve alignment either.
> + *By introducing a convergence factor, guest_x and guest_y can be made to
> + *approach dom0's x and y indefinitely.
> + *
> + *                   QEMU                       (dx,dy)  VM
> + *calculates dx and dy using guest_x and guest_y ---->  input driver
> + *           ^                                            |
> + *           |                                            |
> + *           |                                            V
> + *           |     update
> + *           | guest_x,guest_y              input dispatcher ---> WindowManager
> + *           |                                            |                 |
> + *           |                                            |                 |
> + *           |                 libdrm                     V                 |
> + *       virtio-gpu  <------ drmModeMoveCursor <------ compositor <-------  |
> + *                           (guest_x,guest_y)   calculates guest_x and
> + *                                               guest_y dy using dx and dy
> + */

What about other display devices than virtio-gpu? Does this work with 
those or do they need some update? If this is independent of graphics 
device maybe add a note that virtio-gpu is an example and could be any 
graphics device.

Regards,
BALATON Zoltan

> +#define CONVERGENCE_FACTOR 3
> static void sdl_send_mouse_event(struct sdl2_console *scon, int dx, int dy,
>                                  int x, int y, int state)
> {
> @@ -331,8 +359,8 @@ static void sdl_send_mouse_event(struct sdl2_console *scon, int dx, int dy,
>             y -= guest_y;
>             guest_x += x;
>             guest_y += y;
> -            dx = x;
> -            dy = y;
> +            dx = x / CONVERGENCE_FACTOR;
> +            dy = y / CONVERGENCE_FACTOR;
>         }
>         qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_X, dx);
>         qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_Y, dy);
> -- 
> 2.45.2
>
>
>
Lei Huang Oct. 29, 2024, 6:08 a.m. UTC | #2
> On Fri, 25 Oct 2024, Lei Huang wrote:
> > Use a convergence factor to make the VM's input
> > global coordinates more closely approach the global
> > coordinates of DOM0.
> 
> Dom0 is some Xen terminology. Do you mean "host" which is more often used 
> in QEMU?

Yes, I will change it to host

> 
> > Change-Id: I2c3f12f1fe7dfb9306d1fc40c4fd4d299937f4c6
> > Signed-off-by: Lei Huang <Lei.Huang@amd.com>
> > ---
> > ui/sdl2.c | 32 ++++++++++++++++++++++++++++++--
> > 1 file changed, 30 insertions(+), 2 deletions(-)
> >
> > diff --git a/ui/sdl2.c b/ui/sdl2.c
> > index bd4f5a9da14..8f504dd8727 100644
> > --- a/ui/sdl2.c
> > +++ b/ui/sdl2.c
> > @@ -303,6 +303,34 @@ static void sdl_mouse_mode_change(Notifier *notify, void *data)
> >     }
> > }
> >
> > +/*
> > + *guest_x and guest_y represent the global coordinates on the VM side,
> > + *while x and y represent the global coordinates on the dom0 side.
> > + *The goal of this entire process is to align the global coordinates of
> > + *the VM with those of dom0 using dx and dy. The current approach aims
> > + *for precise calibration in once attempt; however, because guest_x
> 
> "in one attempt" also add a space after the * at the beginning of lines.

okay, I will change it

> 
> > + *and guest_y are non-zero values, they are not accurate values when
> > + *they are counted out to become negative.Therefore, achieving perfect
> > + *alignment in one attempt is impossible.Since the same calibration method
> > + *is used each time, repeated attempts cannot achieve alignment either.
> > + *By introducing a convergence factor, guest_x and guest_y can be made to
> > + *approach dom0's x and y indefinitely.
> > + *
> > + *                   QEMU                       (dx,dy)  VM
> > + *calculates dx and dy using guest_x and guest_y ---->  input driver
> > + *           ^                                            |
> > + *           |                                            |
> > + *           |                                            V
> > + *           |     update
> > + *           | guest_x,guest_y              input dispatcher ---> WindowManager
> > + *           |                                            |                 |
> > + *           |                                            |                 |
> > + *           |                 libdrm                     V                 |
> > + *       virtio-gpu  <------ drmModeMoveCursor <------ compositor <-------  |
> > + *                           (guest_x,guest_y)   calculates guest_x and
> > + *                                               guest_y dy using dx and dy
> > + */
> 
> What about other display devices than virtio-gpu? Does this work with 
> those or do they need some update? If this is independent of graphics 
> device maybe add a note that virtio-gpu is an example and could be any 
> graphics device.

Yes,this applies to any device using SDL where the VM utilizes cursor plane acceleration;
virtio-gpu is just an example. I will add it to the explanation.

> 
> Regards,
> BALATON Zoltan
> 
> > +#define CONVERGENCE_FACTOR 3
> > static void sdl_send_mouse_event(struct sdl2_console *scon, int dx, int dy,
> >                                  int x, int y, int state)
> > {
> > @@ -331,8 +359,8 @@ static void sdl_send_mouse_event(struct sdl2_console *scon, int dx, int dy,
> >             y -= guest_y;
> >             guest_x += x;
> >             guest_y += y;
> > -            dx = x;
> > -            dy = y;
> > +            dx = x / CONVERGENCE_FACTOR;
> > +            dy = y / CONVERGENCE_FACTOR;
> >         }
> >         qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_X, dx);
> >         qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_Y, dy);
> > -- 
> > 2.45.2
> >
> >
> >
> 
>
BALATON Zoltan Oct. 29, 2024, 12:51 p.m. UTC | #3
On Tue, 29 Oct 2024, Lei Huang wrote:
>> On Fri, 25 Oct 2024, Lei Huang wrote:
>>> Use a convergence factor to make the VM's input
>>> global coordinates more closely approach the global
>>> coordinates of DOM0.
>>
>> Dom0 is some Xen terminology. Do you mean "host" which is more often used
>> in QEMU?
>
> Yes, I will change it to host
>
>>
>>> Change-Id: I2c3f12f1fe7dfb9306d1fc40c4fd4d299937f4c6
>>> Signed-off-by: Lei Huang <Lei.Huang@amd.com>
>>> ---
>>> ui/sdl2.c | 32 ++++++++++++++++++++++++++++++--
>>> 1 file changed, 30 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/ui/sdl2.c b/ui/sdl2.c
>>> index bd4f5a9da14..8f504dd8727 100644
>>> --- a/ui/sdl2.c
>>> +++ b/ui/sdl2.c
>>> @@ -303,6 +303,34 @@ static void sdl_mouse_mode_change(Notifier *notify, void *data)
>>>     }
>>> }
>>>
>>> +/*
>>> + *guest_x and guest_y represent the global coordinates on the VM side,
>>> + *while x and y represent the global coordinates on the dom0 side.
>>> + *The goal of this entire process is to align the global coordinates of
>>> + *the VM with those of dom0 using dx and dy. The current approach aims
>>> + *for precise calibration in once attempt; however, because guest_x
>>
>> "in one attempt" also add a space after the * at the beginning of lines.
>
> okay, I will change it
>
>>
>>> + *and guest_y are non-zero values, they are not accurate values when
>>> + *they are counted out to become negative.Therefore, achieving perfect
>>> + *alignment in one attempt is impossible.Since the same calibration method
>>> + *is used each time, repeated attempts cannot achieve alignment either.
>>> + *By introducing a convergence factor, guest_x and guest_y can be made to
>>> + *approach dom0's x and y indefinitely.
>>> + *
>>> + *                   QEMU                       (dx,dy)  VM
>>> + *calculates dx and dy using guest_x and guest_y ---->  input driver
>>> + *           ^                                            |
>>> + *           |                                            |
>>> + *           |                                            V
>>> + *           |     update
>>> + *           | guest_x,guest_y              input dispatcher ---> WindowManager
>>> + *           |                                            |                 |
>>> + *           |                                            |                 |
>>> + *           |                 libdrm                     V                 |
>>> + *       virtio-gpu  <------ drmModeMoveCursor <------ compositor <-------  |
>>> + *                           (guest_x,guest_y)   calculates guest_x and
>>> + *                                               guest_y dy using dx and dy
>>> + */
>>
>> What about other display devices than virtio-gpu? Does this work with
>> those or do they need some update? If this is independent of graphics
>> device maybe add a note that virtio-gpu is an example and could be any
>> graphics device.
>
> Yes,this applies to any device using SDL where the VM utilizes cursor plane acceleration;
> virtio-gpu is just an example. I will add it to the explanation.

You could change the figure in the comment to say instead of virtio-gpu

  display device
(e.g. virtio-gpu)

Which may be enough to clearify this.

Regards,
BALATON Zoltan
Lei Huang Oct. 30, 2024, 3:20 a.m. UTC | #4
>On Tue, 29 Oct 2024, Lei Huang wrote:
>>> On Fri, 25 Oct 2024, Lei Huang wrote:
>>>> Use a convergence factor to make the VM's input
>>>> global coordinates more closely approach the global
>>>> coordinates of DOM0.
>>>
>>> Dom0 is some Xen terminology. Do you mean "host" which is more often used
>>> in QEMU?
>>
>> Yes, I will change it to host
>>
>>>
>>>> Change-Id: I2c3f12f1fe7dfb9306d1fc40c4fd4d299937f4c6
>>>> Signed-off-by: Lei Huang <Lei.Huang@amd.com>
>>>> ---
>>>> ui/sdl2.c | 32 ++++++++++++++++++++++++++++++--
>>>> 1 file changed, 30 insertions(+), 2 deletions(-)
>>>>
>>>> diff --git a/ui/sdl2.c b/ui/sdl2.c
>>>> index bd4f5a9da14..8f504dd8727 100644
>>>> --- a/ui/sdl2.c
>>>> +++ b/ui/sdl2.c
>>>> @@ -303,6 +303,34 @@ static void sdl_mouse_mode_change(Notifier *notify, void *data)
>>>>     }
>>>> }
>>>>
>>>> +/*
>>>> + *guest_x and guest_y represent the global coordinates on the VM side,
>>>> + *while x and y represent the global coordinates on the dom0 side.
>>>> + *The goal of this entire process is to align the global coordinates of
>>>> + *the VM with those of dom0 using dx and dy. The current approach aims
>>>> + *for precise calibration in once attempt; however, because guest_x
>>>
>>> "in one attempt" also add a space after the * at the beginning of lines.
>>
>> okay, I will change it
>>
>>>
>>>> + *and guest_y are non-zero values, they are not accurate values when
>>>> + *they are counted out to become negative.Therefore, achieving perfect
>>>> + *alignment in one attempt is impossible.Since the same calibration method
>>>> + *is used each time, repeated attempts cannot achieve alignment either.
>>>> + *By introducing a convergence factor, guest_x and guest_y can be made to
>>>> + *approach dom0's x and y indefinitely.
>>>> + *
>>>> + *                   QEMU                       (dx,dy)  VM
>>>> + *calculates dx and dy using guest_x and guest_y ---->  input driver
>>>> + *           ^                                            |
>>>> + *           |                                            |
>>>> + *           |                                            V
>>>> + *           |     update
>>>> + *           | guest_x,guest_y              input dispatcher ---> WindowManager
>>>> + *           |                                            |                 |
>>>> + *           |                                            |                 |
>>>> + *           |                 libdrm                     V                 |
>>>> + *       virtio-gpu  <------ drmModeMoveCursor <------ compositor <-------  |
>>>> + *                           (guest_x,guest_y)   calculates guest_x and
>>>> + *                                               guest_y dy using dx and dy
>>>> + */
>>>
>>> What about other display devices than virtio-gpu? Does this work with
>>> those or do they need some update? If this is independent of graphics
>>> device maybe add a note that virtio-gpu is an example and could be any
>>> graphics device.
>>
>> Yes,this applies to any device using SDL where the VM utilizes cursor plane acceleration;
>> virtio-gpu is just an example. I will add it to the explanation.
>
>You could change the figure in the comment to say instead of virtio-gpu
>
>  display device
>(e.g. virtio-gpu)
>
>Which may be enough to clearify this.

okay, got it, thank you very much.

Additionally, I noticed in GTK's implementation, it resolves this issue by using the
gdk_window_get_root_coords method in the gd_mouse_set function. however, I could not
find a similar method in SDL2, so I am not sure if there are better solutions available.
Neverthless, the current modification dose indeed fix the mouse drift issue on the VM side.

>
>Regards,
>BALATON Zoltan
diff mbox series

Patch

diff --git a/ui/sdl2.c b/ui/sdl2.c
index bd4f5a9da14..8f504dd8727 100644
--- a/ui/sdl2.c
+++ b/ui/sdl2.c
@@ -303,6 +303,34 @@  static void sdl_mouse_mode_change(Notifier *notify, void *data)
     }
 }
 
+/*
+ *guest_x and guest_y represent the global coordinates on the VM side,
+ *while x and y represent the global coordinates on the dom0 side.
+ *The goal of this entire process is to align the global coordinates of
+ *the VM with those of dom0 using dx and dy. The current approach aims
+ *for precise calibration in once attempt; however, because guest_x
+ *and guest_y are non-zero values, they are not accurate values when
+ *they are counted out to become negative.Therefore, achieving perfect
+ *alignment in one attempt is impossible.Since the same calibration method
+ *is used each time, repeated attempts cannot achieve alignment either.
+ *By introducing a convergence factor, guest_x and guest_y can be made to
+ *approach dom0's x and y indefinitely.
+ *
+ *                   QEMU                       (dx,dy)  VM
+ *calculates dx and dy using guest_x and guest_y ---->  input driver
+ *           ^                                            |
+ *           |                                            |
+ *           |                                            V
+ *           |     update
+ *           | guest_x,guest_y              input dispatcher ---> WindowManager
+ *           |                                            |                 |
+ *           |                                            |                 |
+ *           |                 libdrm                     V                 |
+ *       virtio-gpu  <------ drmModeMoveCursor <------ compositor <-------  |
+ *                           (guest_x,guest_y)   calculates guest_x and
+ *                                               guest_y dy using dx and dy
+ */
+#define CONVERGENCE_FACTOR 3
 static void sdl_send_mouse_event(struct sdl2_console *scon, int dx, int dy,
                                  int x, int y, int state)
 {
@@ -331,8 +359,8 @@  static void sdl_send_mouse_event(struct sdl2_console *scon, int dx, int dy,
             y -= guest_y;
             guest_x += x;
             guest_y += y;
-            dx = x;
-            dy = y;
+            dx = x / CONVERGENCE_FACTOR;
+            dy = y / CONVERGENCE_FACTOR;
         }
         qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_X, dx);
         qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_Y, dy);