Blender  V3.3
bpy_app_sdl.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include "BLI_utildefines.h"
8 #include <Python.h>
9 
10 #include "bpy_app_sdl.h"
11 
12 #include "../generic/py_capi_utils.h"
13 
14 #ifdef WITH_SDL
15 /* SDL force defines __SSE__ and __SSE2__ flags, which generates warnings
16  * because we pass those defines via command line as well. For until there's
17  * proper ifndef added to SDL headers we ignore the redefinition warning.
18  */
19 # ifdef _MSC_VER
20 # pragma warning(push)
21 # pragma warning(disable : 4005)
22 # endif
23 # include "SDL.h"
24 # ifdef _MSC_VER
25 # pragma warning(pop)
26 # endif
27 # ifdef WITH_SDL_DYNLOAD
28 # include "sdlew.h"
29 # endif
30 #endif
31 
32 static PyTypeObject BlenderAppSDLType;
33 
34 static PyStructSequence_Field app_sdl_info_fields[] = {
35  {"supported", ("Boolean, True when Blender is built with SDL support")},
36  {"version", ("The SDL version as a tuple of 3 numbers")},
37  {"version_string", ("The SDL version formatted as a string")},
38  {"available",
39  ("Boolean, True when SDL is available. This is False when "
40  "either *supported* is False, or *dynload* is True and "
41  "Blender cannot find the correct library.")},
42  {NULL},
43 };
44 
45 static PyStructSequence_Desc app_sdl_info_desc = {
46  "bpy.app.sdl", /* name */
47  "This module contains information about SDL blender is linked against", /* doc */
48  app_sdl_info_fields, /* fields */
50 };
51 
52 static PyObject *make_sdl_info(void)
53 {
54  PyObject *sdl_info;
55  int pos = 0;
56 #ifdef WITH_SDL
57  bool sdl_available = false;
58  SDL_version version = {0, 0, 0};
59 #endif
60 
61  sdl_info = PyStructSequence_New(&BlenderAppSDLType);
62  if (sdl_info == NULL) {
63  return NULL;
64  }
65 
66 #define SetStrItem(str) PyStructSequence_SET_ITEM(sdl_info, pos++, PyUnicode_FromString(str))
67 
68 #define SetObjItem(obj) PyStructSequence_SET_ITEM(sdl_info, pos++, obj)
69 
70 #ifdef WITH_SDL
71  SetObjItem(PyBool_FromLong(1));
72 
73 # ifdef WITH_SDL_DYNLOAD
74  if (sdlewInit() == SDLEW_SUCCESS) {
75  SDL_GetVersion(&version);
76  sdl_available = true;
77  }
78 # else /* WITH_SDL_DYNLOAD=OFF */
79  sdl_available = true;
80 # if SDL_MAJOR_VERSION >= 2
81  SDL_GetVersion(&version);
82 # else
83  SDL_VERSION(&version);
84 # endif
85 # endif
86 
87  SetObjItem(PyC_Tuple_Pack_I32(version.major, version.minor, version.patch));
88  if (sdl_available) {
89  SetObjItem(PyUnicode_FromFormat("%d.%d.%d", version.major, version.minor, version.patch));
90  }
91  else {
92  SetStrItem("Unknown");
93  }
94  SetObjItem(PyBool_FromLong(sdl_available));
95 
96 #else /* WITH_SDL=OFF */
97  SetObjItem(PyBool_FromLong(0));
99  SetStrItem("Unknown");
100  SetObjItem(PyBool_FromLong(0));
101 #endif
102 
103  if (UNLIKELY(PyErr_Occurred())) {
104  Py_DECREF(sdl_info);
105  return NULL;
106  }
107 
108 #undef SetStrItem
109 #undef SetObjItem
110 
111  return sdl_info;
112 }
113 
114 PyObject *BPY_app_sdl_struct(void)
115 {
116  PyObject *ret;
117 
118  PyStructSequence_InitType(&BlenderAppSDLType, &app_sdl_info_desc);
119 
120  ret = make_sdl_info();
121 
122  /* prevent user from creating new instances */
123  BlenderAppSDLType.tp_init = NULL;
124  BlenderAppSDLType.tp_new = NULL;
125  BlenderAppSDLType.tp_hash = (hashfunc)
126  _Py_HashPointer; /* without this we can't do set(sys.modules) T29635. */
127 
128  return ret;
129 }
#define ARRAY_SIZE(arr)
#define UNLIKELY(x)
static PyStructSequence_Desc app_sdl_info_desc
Definition: bpy_app_sdl.c:45
static PyStructSequence_Field app_sdl_info_fields[]
Definition: bpy_app_sdl.c:34
static PyTypeObject BlenderAppSDLType
Definition: bpy_app_sdl.c:32
#define SetStrItem(str)
PyObject * BPY_app_sdl_struct(void)
Definition: bpy_app_sdl.c:114
static PyObject * make_sdl_info(void)
Definition: bpy_app_sdl.c:52
#define SetObjItem(obj)
uint pos
#define PyC_Tuple_Pack_I32(...)
Definition: py_capi_utils.h:88
return ret