Blender V5.3
bpy_app_sdl.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_utildefines.hh"
10#include <Python.h>
11
12#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
13
14#include "bpy_app_sdl.hh"
15
17
18namespace blender {
19
20#ifdef WITH_SDL
21# include <SDL3/SDL.h>
22#endif
23
24static PyTypeObject BlenderAppSDLType;
25
26static PyStructSequence_Field app_sdl_info_fields[] = {
27 {"supported", ("Boolean, True when Blender is built with SDL support")},
28 {"version", ("The SDL version as a tuple of 3 numbers")},
29 {"version_string", ("The SDL version formatted as a string")},
30 {nullptr},
31};
32
33static PyStructSequence_Desc app_sdl_info_desc = {
34 /*name*/ "bpy.app.sdl",
35 /*doc*/ "This module contains information about SDL blender is linked against",
36 /*fields*/ app_sdl_info_fields,
37 /*n_in_sequence*/ ARRAY_SIZE(app_sdl_info_fields) - 1,
38};
39
40static PyObject *make_sdl_info()
41{
42 PyObject *sdl_info;
43 int pos = 0;
44
45 sdl_info = PyStructSequence_New(&BlenderAppSDLType);
46 if (sdl_info == nullptr) {
47 return nullptr;
48 }
49
50#define SetStrItem(str) PyStructSequence_SET_ITEM(sdl_info, pos++, PyUnicode_FromString(str))
51
52#define SetObjItem(obj) PyStructSequence_SET_ITEM(sdl_info, pos++, obj)
53
54#ifdef WITH_SDL
55 SetObjItem(PyBool_FromLong(1));
56 {
57 int sdl_ver = SDL_GetVersion();
58 const int major = SDL_VERSIONNUM_MAJOR(sdl_ver);
59 const int minor = SDL_VERSIONNUM_MINOR(sdl_ver);
60 const int patch = SDL_VERSIONNUM_MICRO(sdl_ver);
61 SetObjItem(PyC_Tuple_Pack_I32({major, minor, patch}));
62 SetObjItem(PyUnicode_FromFormat("%d.%d.%d", major, minor, patch));
63 }
64
65#else /* WITH_SDL=OFF */
66 SetObjItem(PyBool_FromLong(0));
68 SetStrItem("Unknown");
69#endif
70
71 if (PyErr_Occurred()) [[unlikely]] {
72 Py_DECREF(sdl_info);
73 return nullptr;
74 }
75
76#undef SetStrItem
77#undef SetObjItem
78
79 return sdl_info;
80}
81
83{
84 PyObject *ret;
85
86 PyStructSequence_InitType(&BlenderAppSDLType, &app_sdl_info_desc);
87
89
90 /* prevent user from creating new instances */
91 BlenderAppSDLType.tp_init = nullptr;
92 BlenderAppSDLType.tp_new = nullptr;
93 /* Without this we can't do `set(sys.modules)` #29635. */
94 BlenderAppSDLType.tp_hash = reinterpret_cast<hashfunc>(Py_HashPointer);
95
96 return ret;
97}
98
99} // namespace blender
#define SetStrItem(str)
#define SetObjItem(obj)
BMesh Iterators.
static PyObject * make_sdl_info()
static PyStructSequence_Field app_sdl_info_fields[]
PyObject * PyC_Tuple_Pack_I32(const Span< int > values)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
static PyStructSequence_Desc app_sdl_info_desc
static PyTypeObject BlenderAppSDLType
PyObject * BPY_app_sdl_struct()
header-only compatibility defines.
#define Py_HashPointer