Skip to content

Commit 396bcbd

Browse files
committed
pygame.Window.get_info()
1 parent 74043a6 commit 396bcbd

File tree

10 files changed

+297
-115
lines changed

10 files changed

+297
-115
lines changed

buildconfig/stubs/pygame/display.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ def get_wm_info() -> dict[str, int]:
399399
key with the value set to the system id for the current display.
400400
401401
.. versionaddedold:: 1.7.1
402+
.. versionchanged:: 2.X.X added a new key ``subsystem`` used to identify windowing system - exists in most cases.
402403
"""
403404

404405
def get_desktop_sizes() -> list[tuple[int, int]]:

buildconfig/stubs/pygame/window.pyi

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,4 +487,17 @@ class Window:
487487
488488
.. versionadded:: 2.5.2
489489
"""
490+
491+
def get_info(self) -> dict[str, int | str]:
492+
"""Return system-specific information about a given window.
493+
494+
Creates a dictionary filled with string keys. It's empty if failed to query the system
495+
info (eg.: the window was destroyed). Otherwise it contains at least one key ``subsystem``
496+
containing the name of the window's subsystem. The rest of values are integes.
497+
Names of the rest keys and meaning of their values is system-dependent,
498+
but most platforms will return a "window" key with the value
499+
set to the system id for the current window.
500+
501+
.. versionadded:: 2.X.X
502+
"""
490503
relative_mouse: bool

docs/reST/c_api/window.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,10 @@ Header file: src_c/include/pygame.h
2828
2929
Will return false if *x* is not a subclass of `Window`.
3030
This is a macro. No check is made that *x* is not *NULL*.
31+
32+
.. c:function:: PyObject* pgWindow_GetInfo(SDL_Window *window)
33+
34+
Return a python dict containing system-specific information about a window.
35+
36+
.. seealso::
37+
:py:func:`pygame.window.Window.get_info`

src_c/_pygame.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ typedef enum {
623623
#define PYGAMEAPI_MATH_NUMSLOTS 2
624624
#define PYGAMEAPI_BASE_NUMSLOTS 30
625625
#define PYGAMEAPI_EVENT_NUMSLOTS 10
626-
#define PYGAMEAPI_WINDOW_NUMSLOTS 1
626+
#define PYGAMEAPI_WINDOW_NUMSLOTS 2
627627
#define PYGAMEAPI_RENDER_NUMSLOTS 3
628628
#define PYGAMEAPI_GEOMETRY_NUMSLOTS 2
629629
#define PYGAMEAPI_BUFFERPROXY_NUMSLOTS 4

src_c/display.c

Lines changed: 1 addition & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -467,122 +467,9 @@ pgInfo(PyObject *self, PyObject *_null)
467467
static PyObject *
468468
pg_get_wm_info(PyObject *self, PyObject *_null)
469469
{
470-
PyObject *dict;
471-
PyObject *tmp;
472-
SDL_SysWMinfo info;
473-
SDL_Window *win;
474-
475470
VIDEO_INIT_CHECK();
476471

477-
SDL_VERSION(&(info.version))
478-
dict = PyDict_New();
479-
if (!dict) {
480-
return NULL;
481-
}
482-
483-
win = pg_GetDefaultWindow();
484-
if (!win) {
485-
return dict;
486-
}
487-
if (!SDL_GetWindowWMInfo(win, &info)) {
488-
return dict;
489-
}
490-
491-
(void)tmp;
492-
#if defined(SDL_VIDEO_DRIVER_WINDOWS)
493-
tmp = PyLong_FromLongLong((long long)info.info.win.window);
494-
PyDict_SetItemString(dict, "window", tmp);
495-
Py_DECREF(tmp);
496-
497-
tmp = PyLong_FromLongLong((long long)info.info.win.hdc);
498-
PyDict_SetItemString(dict, "hdc", tmp);
499-
Py_DECREF(tmp);
500-
tmp = PyLong_FromLongLong((long long)info.info.win.hinstance);
501-
PyDict_SetItemString(dict, "hinstance", tmp);
502-
Py_DECREF(tmp);
503-
#endif
504-
#if defined(SDL_VIDEO_DRIVER_WINRT)
505-
tmp = PyCapsule_New(info.info.winrt.window, "window", NULL);
506-
PyDict_SetItemString(dict, "window", tmp);
507-
Py_DECREF(tmp);
508-
#endif
509-
#if defined(SDL_VIDEO_DRIVER_X11)
510-
tmp = PyLong_FromLong(info.info.x11.window);
511-
PyDict_SetItemString(dict, "window", tmp);
512-
Py_DECREF(tmp);
513-
514-
tmp = PyCapsule_New(info.info.x11.display, "display", NULL);
515-
PyDict_SetItemString(dict, "display", tmp);
516-
Py_DECREF(tmp);
517-
#endif
518-
#if defined(SDL_VIDEO_DRIVER_DIRECTFB)
519-
tmp = PyCapsule_New(info.info.dfb.dfb, "dfb", NULL);
520-
PyDict_SetItemString(dict, "dfb", tmp);
521-
Py_DECREF(tmp);
522-
523-
tmp = PyCapsule_New(info.info.dfb.window, "window", NULL);
524-
PyDict_SetItemString(dict, "window", tmp);
525-
Py_DECREF(tmp);
526-
527-
tmp = PyCapsule_New(info.info.dfb.surface, "surface", NULL);
528-
PyDict_SetItemString(dict, "surface", tmp);
529-
Py_DECREF(tmp);
530-
#endif
531-
#if defined(SDL_VIDEO_DRIVER_COCOA)
532-
tmp = PyCapsule_New(info.info.cocoa.window, "window", NULL);
533-
PyDict_SetItemString(dict, "window", tmp);
534-
Py_DECREF(tmp);
535-
#endif
536-
#if defined(SDL_VIDEO_DRIVER_UIKIT)
537-
tmp = PyCapsule_New(info.info.uikit.window, "window", NULL);
538-
PyDict_SetItemString(dict, "window", tmp);
539-
Py_DECREF(tmp);
540-
541-
tmp = PyLong_FromLong(info.info.uikit.framebuffer);
542-
PyDict_SetItemString(dict, "framebuffer", tmp);
543-
Py_DECREF(tmp);
544-
545-
tmp = PyLong_FromLong(info.info.uikit.colorbuffer);
546-
PyDict_SetItemString(dict, "colorbuffer", tmp);
547-
Py_DECREF(tmp);
548-
549-
tmp = PyLong_FromLong(info.info.uikit.resolveFramebuffer);
550-
PyDict_SetItemString(dict, "resolveFramebuffer", tmp);
551-
Py_DECREF(tmp);
552-
#endif
553-
#if defined(SDL_VIDEO_DRIVER_WAYLAND)
554-
tmp = PyCapsule_New(info.info.wl.display, "display", NULL);
555-
PyDict_SetItemString(dict, "display", tmp);
556-
Py_DECREF(tmp);
557-
558-
tmp = PyCapsule_New(info.info.wl.surface, "surface", NULL);
559-
PyDict_SetItemString(dict, "surface", tmp);
560-
Py_DECREF(tmp);
561-
562-
tmp = PyCapsule_New(info.info.wl.shell_surface, "shell_surface", NULL);
563-
PyDict_SetItemString(dict, "shell_surface", tmp);
564-
Py_DECREF(tmp);
565-
#endif
566-
#if defined(SDL_VIDEO_DRIVER_ANDROID)
567-
tmp = PyCapsule_New(info.info.android.window, "window", NULL);
568-
PyDict_SetItemString(dict, "window", tmp);
569-
Py_DECREF(tmp);
570-
571-
tmp = PyLong_FromLong((long)info.info.android.surface);
572-
PyDict_SetItemString(dict, "surface", tmp);
573-
Py_DECREF(tmp);
574-
#endif
575-
#if defined(SDL_VIDEO_DRIVER_VIVANTE)
576-
tmp = PyLong_FromLong((long)info.info.vivante.display);
577-
PyDict_SetItemString(dict, "display", tmp);
578-
Py_DECREF(tmp);
579-
580-
tmp = PyLong_FromLong((long)info.info.vivante.window);
581-
PyDict_SetItemString(dict, "window", tmp);
582-
Py_DECREF(tmp);
583-
#endif
584-
585-
return dict;
472+
return pgWindow_GetInfo(pg_GetDefaultWindow());
586473
}
587474

588475
/* display functions */

src_c/doc/window_doc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@
3333
#define DOC_WINDOW_SETICON "set_icon(icon, /) -> None\nSet the window icon."
3434
#define DOC_WINDOW_SETMODALFOR "set_modal_for(parent, /) -> None\nSet the window as a modal for a parent window."
3535
#define DOC_WINDOW_FLASH "flash(operation, /) -> None\nFlash a window to demand attention from the user."
36+
#define DOC_WINDOW_GETINFO "get_info() -> dict[str, int | str]\nReturn system-specific information about a given window."

src_c/include/_pygame.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,8 @@ typedef struct {
546546
#define pgWindow_Check(x) \
547547
(PyObject_IsInstance((x), (PyObject *)&pgWindow_Type))
548548
#define import_pygame_window() IMPORT_PYGAME_MODULE(window)
549+
#define pgWindow_GetInfo \
550+
(*(PyObject * (*)(SDL_Window *)) PYGAMEAPI_GET_SLOT(window, 1))
549551
#endif
550552

551553
typedef struct pgTextureObject pgTextureObject;

0 commit comments

Comments
 (0)