Blender V5.0
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.h"
10#include <Python.h>
11
12#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
13
14#include "bpy_app_sdl.hh"
15
17
18#ifdef WITH_SDL
19/* SDL force defines __SSE__ and __SSE2__ flags, which generates warnings
20 * because we pass those defines via command line as well. For until there's
21 * proper `ifndef` added to SDL headers we ignore the redefinition warning.
22 */
23# ifdef _MSC_VER
24# pragma warning(push)
25# pragma warning(disable : 4005)
26# endif
27# include "SDL.h"
28# ifdef _MSC_VER
29# pragma warning(pop)
30# endif
31#endif
32
33static PyTypeObject BlenderAppSDLType;
34
35static PyStructSequence_Field app_sdl_info_fields[] = {
36 {"supported", ("Boolean, True when Blender is built with SDL support")},
37 {"version", ("The SDL version as a tuple of 3 numbers")},
38 {"version_string", ("The SDL version formatted as a string")},
39 {nullptr},
40};
41
42static PyStructSequence_Desc app_sdl_info_desc = {
43 /*name*/ "bpy.app.sdl",
44 /*doc*/ "This module contains information about SDL blender is linked against",
45 /*fields*/ app_sdl_info_fields,
46 /*n_in_sequence*/ ARRAY_SIZE(app_sdl_info_fields) - 1,
47};
48
49static PyObject *make_sdl_info()
50{
51 PyObject *sdl_info;
52 int pos = 0;
53#ifdef WITH_SDL
54 SDL_version version = {0, 0, 0};
55#endif
56
57 sdl_info = PyStructSequence_New(&BlenderAppSDLType);
58 if (sdl_info == nullptr) {
59 return nullptr;
60 }
61
62#define SetStrItem(str) PyStructSequence_SET_ITEM(sdl_info, pos++, PyUnicode_FromString(str))
63
64#define SetObjItem(obj) PyStructSequence_SET_ITEM(sdl_info, pos++, obj)
65
66#ifdef WITH_SDL
67 SetObjItem(PyBool_FromLong(1));
68# if SDL_MAJOR_VERSION >= 2
69 SDL_GetVersion(&version);
70# else
71 SDL_VERSION(&version);
72# endif
73
74 SetObjItem(PyC_Tuple_Pack_I32({version.major, version.minor, version.patch}));
75 SetObjItem(PyUnicode_FromFormat("%d.%d.%d", version.major, version.minor, version.patch));
76
77#else /* WITH_SDL=OFF */
78 SetObjItem(PyBool_FromLong(0));
80 SetStrItem("Unknown");
81#endif
82
83 if (UNLIKELY(PyErr_Occurred())) {
84 Py_DECREF(sdl_info);
85 return nullptr;
86 }
87
88#undef SetStrItem
89#undef SetObjItem
90
91 return sdl_info;
92}
93
95{
96 PyObject *ret;
97
98 PyStructSequence_InitType(&BlenderAppSDLType, &app_sdl_info_desc);
99
100 ret = make_sdl_info();
101
102 /* prevent user from creating new instances */
103 BlenderAppSDLType.tp_init = nullptr;
104 BlenderAppSDLType.tp_new = nullptr;
105 /* Without this we can't do `set(sys.modules)` #29635. */
106 BlenderAppSDLType.tp_hash = (hashfunc)Py_HashPointer;
107
108 return ret;
109}
#define ARRAY_SIZE(arr)
#define UNLIKELY(x)
static PyStructSequence_Desc app_sdl_info_desc
PyObject * BPY_app_sdl_struct()
static PyStructSequence_Field app_sdl_info_fields[]
static PyTypeObject BlenderAppSDLType
#define SetStrItem(str)
#define SetObjItem(obj)
static PyObject * make_sdl_info()
uint pos
PyObject * PyC_Tuple_Pack_I32(const blender::Span< int > values)
header-only compatibility defines.
#define Py_HashPointer
Py_DECREF(oname)
return ret