Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions buildconfig/stubs/pygame/mouse.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,6 @@ def set_relative_mode(enable: bool, /) -> None:
``True`` will exit relative mouse mode.

.. versionadded:: 2.4.0
.. versionchanged:: 2.5.6 calling this function before calling
:func:`pygame.display.set_mode` is deprecated and may error in the future.
"""
6 changes: 5 additions & 1 deletion src_c/key.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,11 @@ static const struct {
{1073742054, "right alt"}, /* K_RALT */
{1073742055, "right meta"}, /* K_RGUI, K_RMETA, K_RSUPER */
{1073742081, "alt gr"}, /* K_MODE */
{1073742094, "AC Back"}, /* K_AC_BACK */
#if SDL_VERSION_ATLEAST(3, 0, 0)
{1073742106, "AC Back"}, /* K_AC_BACK */
#else
{1073742094, "AC Back"}, /* K_AC_BACK */
#endif
};

/* Get name from keycode using pygame compat table */
Expand Down
16 changes: 13 additions & 3 deletions src_c/mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,9 @@ mouse_get_visible(PyObject *self, PyObject *_null)

#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_Window *win = pg_GetDefaultWindow();
result =
win ? (PG_CursorVisible() && !SDL_GetWindowRelativeMouseMode(win)) : 0;

/* If win is NULL, SDL_GetWindowRelativeMouseMode returns false */
result = (PG_CursorVisible() && !SDL_GetWindowRelativeMouseMode(win));
#else
result = (PG_CursorVisible() && !SDL_GetRelativeMouseMode());
#endif
Expand Down Expand Up @@ -618,8 +619,8 @@ mouse_set_relative_mode(PyObject *self, PyObject *arg)
if (mode == -1) {
return NULL;
}
#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_Window *win = pg_GetDefaultWindow();
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (!win) {
return RAISE(pgExc_SDLError,
"display.set_mode has not been called yet.");
Expand All @@ -628,6 +629,15 @@ mouse_set_relative_mode(PyObject *self, PyObject *arg)
return RAISE(pgExc_SDLError, SDL_GetError());
}
#else
if (!win) {
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"Calling mouse.set_relative_mode before calling "
"display.set_mode has been deprecated and may raise "
"errors in the future.",
1) == -1) {
return NULL;
}
}
if (SDL_SetRelativeMouseMode((SDL_bool)mode)) {
return RAISE(pgExc_SDLError, SDL_GetError());
}
Expand Down
8 changes: 6 additions & 2 deletions src_c/pgcompat_rect.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#include "pgcompat_rect.h"

/* SDL 2.0.22 provides some utility functions for FRects */
#if !(SDL_VERSION_ATLEAST(2, 0, 22))
/* SDL3 changed how the edges are handled. Previously right/bottom edges were
* considered excluded from the FRect but now they aren't.
* For now do SDL2 compat, but consider changing this in the future.
* See: https://github.com/pygame-community/pygame-ce/issues/3571 */
#if !(SDL_VERSION_ATLEAST(2, 0, 22)) || SDL_VERSION_ATLEAST(3, 0, 0)

#ifndef CODE_BOTTOM
#define CODE_BOTTOM 1
Expand Down Expand Up @@ -176,4 +180,4 @@ PG_IntersectFRectAndLine(SDL_FRect *rect, float *X1, float *Y1, float *X2,
*Y2 = y2;
return SDL_TRUE;
}
#endif /* !(SDL_VERSION_ATLEAST(2, 0, 22)) */
#endif /* !(SDL_VERSION_ATLEAST(2, 0, 22)) || SDL_VERSION_ATLEAST(3, 0, 0) */
4 changes: 2 additions & 2 deletions src_c/pgcompat_rect.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
#endif

/* SDL 2.0.22 provides some utility functions for FRects */
#if !(SDL_VERSION_ATLEAST(2, 0, 22))
#if !(SDL_VERSION_ATLEAST(2, 0, 22)) || SDL_VERSION_ATLEAST(3, 0, 0)

SDL_bool
PG_IntersectFRectAndLine(SDL_FRect *rect, float *X1, float *Y1, float *X2,
float *Y2);
#else
#define PG_IntersectFRectAndLine SDL_IntersectFRectAndLine
#endif /* !(SDL_VERSION_ATLEAST(2, 0, 22)) */
#endif /* !(SDL_VERSION_ATLEAST(2, 0, 22)) || SDL_VERSION_ATLEAST(3, 0, 0) */

#define pg_PyFloat_FromFloat(x) (PyFloat_FromDouble((double)x))

Expand Down
5 changes: 4 additions & 1 deletion test/key_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

# keys that are not tested for const-name match
SKIPPED_KEYS = {"K_UNKNOWN"}
SKIPPED_KEYS_NEW = {"K_MODE"}

# This is the expected compat output
KEY_NAME_COMPAT = {
Expand Down Expand Up @@ -169,6 +170,7 @@
class KeyModuleTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
pygame.quit()
pygame.init()

@classmethod
Expand Down Expand Up @@ -286,7 +288,8 @@ def test_name_and_key_code(self):
# This is a test for an implementation detail of name with use_compat=False
# If this test breaks in the future for any key, it is safe to put skips on
# failing keys (the implementation detail is documented as being unreliable)
self.assertEqual(pygame.key.key_code(alt_name), const_val)
if const_name not in SKIPPED_KEYS_NEW:
self.assertEqual(pygame.key.key_code(alt_name), const_val)

self.assertRaises(TypeError, pygame.key.name, "fizzbuzz")
self.assertRaises(TypeError, pygame.key.key_code, pygame.K_a)
Expand Down
10 changes: 10 additions & 0 deletions test/mouse_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,15 @@ def test_set_visible__invalid_value(self):
)
def test_set_relative_mode(self):
"""Tests that set_relative_mode hides the cursor."""
for val in (True, False):
if pygame.version.SDL >= (3, 0, 0):
with self.assertRaises(pygame.error):
pygame.mouse.set_relative_mode(val)
else:
with self.assertWarns(DeprecationWarning):
pygame.mouse.set_relative_mode(val)

pygame.display.set_mode((100, 100))
pygame.mouse.set_visible(True)
pygame.mouse.set_relative_mode(True) # sets the mouse invisible
visible = pygame.mouse.get_visible()
Expand All @@ -398,6 +407,7 @@ def test_set_relative_mode(self):
)
def test_get_relative_mode(self):
"""Tests that get_relative_mode correctly reports the relative mode"""
pygame.display.set_mode((100, 100))
pygame.mouse.set_relative_mode(True)
self.assertEqual(pygame.mouse.get_relative_mode(), True)
pygame.mouse.set_relative_mode(False)
Expand Down
Loading