From 6127c3f6baec98912b83075530a8e4ca7a635940 Mon Sep 17 00:00:00 2001 From: Damiano Ricciardi Date: Tue, 20 Aug 2024 14:42:50 +0200 Subject: [PATCH 1/7] Add pygame.mouse.get_desktop_pos --- buildconfig/stubs/pygame/mouse.pyi | 1 + docs/reST/ref/mouse.rst | 15 +++++++++++++++ src_c/doc/mouse_doc.h | 1 + src_c/mouse.c | 12 ++++++++++++ test/mouse_test.py | 11 +++++++++++ 5 files changed, 40 insertions(+) diff --git a/buildconfig/stubs/pygame/mouse.pyi b/buildconfig/stubs/pygame/mouse.pyi index 42ea298b45..5724392827 100644 --- a/buildconfig/stubs/pygame/mouse.pyi +++ b/buildconfig/stubs/pygame/mouse.pyi @@ -13,6 +13,7 @@ def get_pressed(num_buttons: Literal[5]) -> Tuple[bool, bool, bool, bool, bool]: def get_just_pressed() -> Tuple[bool, bool, bool, bool, bool]: ... def get_just_released() -> Tuple[bool, bool, bool, bool, bool]: ... def get_pos() -> Tuple[int, int]: ... +def get_desktop_pos() -> Tuple[int, int]: ... def get_rel() -> Tuple[int, int]: ... @overload def set_pos(pos: Coordinate, /) -> None: ... diff --git a/docs/reST/ref/mouse.rst b/docs/reST/ref/mouse.rst index c01a70997c..13d6dbb093 100644 --- a/docs/reST/ref/mouse.rst +++ b/docs/reST/ref/mouse.rst @@ -169,6 +169,21 @@ scroll, such as ``which`` (it will tell you what exact mouse device trigger the .. ## pygame.mouse.get_pos ## +.. function:: get_desktop_pos + + | :sl:`get the mouse cursor position relative to the desktop` + | :sg:`get_desktop_pos() -> (x, y)` + + Returns the ``x`` and ``y`` position of the mouse cursor relative to the + top-left corner of the desktop. The position is always constrained to the + screen. + + .. note:: While :func:`pygame.mouse.get_pos` retrieves the position from the + last event pump, :func:`pygame.mouse.get_desktop_pos` queries the OS, which is + slightly less efficient. + + .. # pygame.mouse.get_desktop_pos + .. function:: get_rel | :sl:`get the amount of mouse movement` diff --git a/src_c/doc/mouse_doc.h b/src_c/doc/mouse_doc.h index 77ab749c39..a6f9840ac9 100644 --- a/src_c/doc/mouse_doc.h +++ b/src_c/doc/mouse_doc.h @@ -4,6 +4,7 @@ #define DOC_MOUSE_GETJUSTPRESSED "get_just_pressed() -> (left_button, middle_button, right_button, x1_button, x2_button)\nget the most recently pressed buttons" #define DOC_MOUSE_GETJUSTRELEASED "get_just_released() -> (left_button, middle_button, right_button, x1_button, x2_button)\nget the most recently released buttons" #define DOC_MOUSE_GETPOS "get_pos() -> (x, y)\nget the mouse cursor position" +#define DOC_MOUSE_GETDESKTOPPOS "get_desktop_pos() -> (x, y)\nget the mouse cursor position relative to the desktop" #define DOC_MOUSE_GETREL "get_rel() -> (x, y)\nget the amount of mouse movement" #define DOC_MOUSE_SETPOS "set_pos([x, y], /) -> None\nset the mouse cursor position" #define DOC_MOUSE_SETVISIBLE "set_visible(bool, /) -> bool\nhide or show the mouse cursor" diff --git a/src_c/mouse.c b/src_c/mouse.c index c6af99056e..a96308e48e 100644 --- a/src_c/mouse.c +++ b/src_c/mouse.c @@ -100,6 +100,17 @@ mouse_get_pos(PyObject *self, PyObject *_null) return pg_tuple_couple_from_values_int(x, y); } +static PyObject * +mouse_get_desktop_pos(PyObject *self, PyObject *_null) +{ + int x, y; + + VIDEO_INIT_CHECK(); + SDL_GetGlobalMouseState(&x, &y); + + return pg_tuple_couple_from_values_int(x, y); +} + static PyObject * mouse_get_rel(PyObject *self, PyObject *_null) { @@ -531,6 +542,7 @@ mouse_set_relative_mode(PyObject *self, PyObject *arg) static PyMethodDef _mouse_methods[] = { {"set_pos", mouse_set_pos, METH_VARARGS, DOC_MOUSE_SETPOS}, {"get_pos", (PyCFunction)mouse_get_pos, METH_NOARGS, DOC_MOUSE_GETPOS}, + {"get_desktop_pos", (PyCFunction)mouse_get_desktop_pos, METH_NOARGS, DOC_MOUSE_GETDESKTOPPOS}, {"get_rel", (PyCFunction)mouse_get_rel, METH_NOARGS, DOC_MOUSE_GETREL}, {"get_pressed", (PyCFunction)mouse_get_pressed, METH_VARARGS | METH_KEYWORDS, DOC_MOUSE_GETPRESSED}, diff --git a/test/mouse_test.py b/test/mouse_test.py index 3373bd2853..4404eb0da8 100644 --- a/test/mouse_test.py +++ b/test/mouse_test.py @@ -315,6 +315,17 @@ def test_get_pos(self): for value in pos: self.assertIsInstance(value, int) + def test_get_desktop_pos(self): + """Ensures get_desktop_pos returns the correct types.""" + expected_length = 2 + + pos = pygame.mouse.get_desktop_pos() + + self.assertIsInstance(pos, tuple) + self.assertEqual(len(pos), expected_length) + for value in pos: + self.assertIsInstance(value, int) + def test_set_pos__invalid_pos(self): """Ensures set_pos handles invalid positions correctly.""" for invalid_pos in ((1,), [1, 2, 3], 1, "1", (1, "1"), []): From 87ccf2be739e9d2708558339ecc806f996ccbabb Mon Sep 17 00:00:00 2001 From: Damiano Ricciardi Date: Tue, 20 Aug 2024 14:46:02 +0200 Subject: [PATCH 2/7] Run pre-commit --- src_c/mouse.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src_c/mouse.c b/src_c/mouse.c index a96308e48e..05b8351f77 100644 --- a/src_c/mouse.c +++ b/src_c/mouse.c @@ -542,7 +542,8 @@ mouse_set_relative_mode(PyObject *self, PyObject *arg) static PyMethodDef _mouse_methods[] = { {"set_pos", mouse_set_pos, METH_VARARGS, DOC_MOUSE_SETPOS}, {"get_pos", (PyCFunction)mouse_get_pos, METH_NOARGS, DOC_MOUSE_GETPOS}, - {"get_desktop_pos", (PyCFunction)mouse_get_desktop_pos, METH_NOARGS, DOC_MOUSE_GETDESKTOPPOS}, + {"get_desktop_pos", (PyCFunction)mouse_get_desktop_pos, METH_NOARGS, + DOC_MOUSE_GETDESKTOPPOS}, {"get_rel", (PyCFunction)mouse_get_rel, METH_NOARGS, DOC_MOUSE_GETREL}, {"get_pressed", (PyCFunction)mouse_get_pressed, METH_VARARGS | METH_KEYWORDS, DOC_MOUSE_GETPRESSED}, From 895d39a25d160f93cd7f6d182b6cecd7036e6345 Mon Sep 17 00:00:00 2001 From: Damiano Ricciardi Date: Tue, 20 Aug 2024 15:03:16 +0200 Subject: [PATCH 3/7] Add versionadded tag --- docs/reST/ref/mouse.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/reST/ref/mouse.rst b/docs/reST/ref/mouse.rst index 13d6dbb093..31b80d8441 100644 --- a/docs/reST/ref/mouse.rst +++ b/docs/reST/ref/mouse.rst @@ -182,6 +182,8 @@ scroll, such as ``which`` (it will tell you what exact mouse device trigger the last event pump, :func:`pygame.mouse.get_desktop_pos` queries the OS, which is slightly less efficient. + .. versionadded:: 2.5.2 + .. # pygame.mouse.get_desktop_pos .. function:: get_rel From 4d48be74ec6005f7f2ce997250b563ebb194c436 Mon Sep 17 00:00:00 2001 From: Damiano Ricciardi Date: Wed, 21 Aug 2024 15:01:50 +0200 Subject: [PATCH 4/7] Use floats instead of ints and update documentation --- buildconfig/stubs/pygame/mouse.pyi | 2 +- docs/reST/ref/mouse.rst | 6 +++--- src_c/mouse.c | 2 +- test/mouse_test.py | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/buildconfig/stubs/pygame/mouse.pyi b/buildconfig/stubs/pygame/mouse.pyi index 5724392827..fcecfeabd9 100644 --- a/buildconfig/stubs/pygame/mouse.pyi +++ b/buildconfig/stubs/pygame/mouse.pyi @@ -13,7 +13,7 @@ def get_pressed(num_buttons: Literal[5]) -> Tuple[bool, bool, bool, bool, bool]: def get_just_pressed() -> Tuple[bool, bool, bool, bool, bool]: ... def get_just_released() -> Tuple[bool, bool, bool, bool, bool]: ... def get_pos() -> Tuple[int, int]: ... -def get_desktop_pos() -> Tuple[int, int]: ... +def get_desktop_pos() -> Tuple[float, float]: ... def get_rel() -> Tuple[int, int]: ... @overload def set_pos(pos: Coordinate, /) -> None: ... diff --git a/docs/reST/ref/mouse.rst b/docs/reST/ref/mouse.rst index 31b80d8441..8ca2d4eb27 100644 --- a/docs/reST/ref/mouse.rst +++ b/docs/reST/ref/mouse.rst @@ -174,9 +174,9 @@ scroll, such as ``which`` (it will tell you what exact mouse device trigger the | :sl:`get the mouse cursor position relative to the desktop` | :sg:`get_desktop_pos() -> (x, y)` - Returns the ``x`` and ``y`` position of the mouse cursor relative to the - top-left corner of the desktop. The position is always constrained to the - screen. + Returns the ``x`` and ``y`` float position of the mouse cursor relative to the + top-left corner of the primary monitor. The position might be negative or exceed + the desktop bounds if multiple monitors are present. .. note:: While :func:`pygame.mouse.get_pos` retrieves the position from the last event pump, :func:`pygame.mouse.get_desktop_pos` queries the OS, which is diff --git a/src_c/mouse.c b/src_c/mouse.c index 05b8351f77..e32006925a 100644 --- a/src_c/mouse.c +++ b/src_c/mouse.c @@ -108,7 +108,7 @@ mouse_get_desktop_pos(PyObject *self, PyObject *_null) VIDEO_INIT_CHECK(); SDL_GetGlobalMouseState(&x, &y); - return pg_tuple_couple_from_values_int(x, y); + return pg_tuple_couple_from_values_double((double)x, (double)y); } static PyObject * diff --git a/test/mouse_test.py b/test/mouse_test.py index 4404eb0da8..c9001ebdad 100644 --- a/test/mouse_test.py +++ b/test/mouse_test.py @@ -324,7 +324,7 @@ def test_get_desktop_pos(self): self.assertIsInstance(pos, tuple) self.assertEqual(len(pos), expected_length) for value in pos: - self.assertIsInstance(value, int) + self.assertIsInstance(value, float) def test_set_pos__invalid_pos(self): """Ensures set_pos handles invalid positions correctly.""" From 6a6e67177ee078ba5411a82f0ff90aff26c07c30 Mon Sep 17 00:00:00 2001 From: Damiano Ricciardi Date: Thu, 29 Aug 2024 09:18:31 +0200 Subject: [PATCH 5/7] Remove the note and add the warning --- docs/reST/ref/mouse.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/reST/ref/mouse.rst b/docs/reST/ref/mouse.rst index 8ca2d4eb27..a77ca408c0 100644 --- a/docs/reST/ref/mouse.rst +++ b/docs/reST/ref/mouse.rst @@ -178,9 +178,8 @@ scroll, such as ``which`` (it will tell you what exact mouse device trigger the top-left corner of the primary monitor. The position might be negative or exceed the desktop bounds if multiple monitors are present. - .. note:: While :func:`pygame.mouse.get_pos` retrieves the position from the - last event pump, :func:`pygame.mouse.get_desktop_pos` queries the OS, which is - slightly less efficient. + .. warning:: Due to OS limitations it is impossible to retrieve the global mouse + state on Wayland. The relative mouse position is returned instead. .. versionadded:: 2.5.2 From 7c9934582b18aba8ccbf68602733617de762c933 Mon Sep 17 00:00:00 2001 From: Damiano Ricciardi Date: Thu, 29 Aug 2024 09:34:05 +0200 Subject: [PATCH 6/7] Change wording --- docs/reST/ref/mouse.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/reST/ref/mouse.rst b/docs/reST/ref/mouse.rst index a77ca408c0..961e6366ae 100644 --- a/docs/reST/ref/mouse.rst +++ b/docs/reST/ref/mouse.rst @@ -178,8 +178,8 @@ scroll, such as ``which`` (it will tell you what exact mouse device trigger the top-left corner of the primary monitor. The position might be negative or exceed the desktop bounds if multiple monitors are present. - .. warning:: Due to OS limitations it is impossible to retrieve the global mouse - state on Wayland. The relative mouse position is returned instead. + .. warning:: Due to design constraints it is impossible to retrieve the global + mouse state on Wayland. The relative mouse position is returned instead. .. versionadded:: 2.5.2 From 3c3a0ead5d345dcdf2be02eb58748f3ea25fa917 Mon Sep 17 00:00:00 2001 From: Damus666 Date: Sun, 15 Sep 2024 18:10:46 +0200 Subject: [PATCH 7/7] Useful commit to make the comment consistent --- docs/reST/ref/mouse.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reST/ref/mouse.rst b/docs/reST/ref/mouse.rst index 961e6366ae..a4a4e6e576 100644 --- a/docs/reST/ref/mouse.rst +++ b/docs/reST/ref/mouse.rst @@ -183,7 +183,7 @@ scroll, such as ``which`` (it will tell you what exact mouse device trigger the .. versionadded:: 2.5.2 - .. # pygame.mouse.get_desktop_pos + .. ## pygame.mouse.get_desktop_pos ## .. function:: get_rel