Blender  V2.93
bpy_app_sdl.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include "BLI_utildefines.h"
22 #include <Python.h>
23 
24 #include "bpy_app_sdl.h"
25 
26 #include "../generic/py_capi_utils.h"
27 
28 #ifdef WITH_SDL
29 /* SDL force defines __SSE__ and __SSE2__ flags, which generates warnings
30  * because we pass those defines via command line as well. For until there's
31  * proper ifndef added to SDL headers we ignore the redefinition warning.
32  */
33 # ifdef _MSC_VER
34 # pragma warning(push)
35 # pragma warning(disable : 4005)
36 # endif
37 # include "SDL.h"
38 # ifdef _MSC_VER
39 # pragma warning(pop)
40 # endif
41 # ifdef WITH_SDL_DYNLOAD
42 # include "sdlew.h"
43 # endif
44 #endif
45 
46 static PyTypeObject BlenderAppSDLType;
47 
48 static PyStructSequence_Field app_sdl_info_fields[] = {
49  {"supported", ("Boolean, True when Blender is built with SDL support")},
50  {"version", ("The SDL version as a tuple of 3 numbers")},
51  {"version_string", ("The SDL version formatted as a string")},
52  {"available",
53  ("Boolean, True when SDL is available. This is False when "
54  "either *supported* is False, or *dynload* is True and "
55  "Blender cannot find the correct library.")},
56  {NULL},
57 };
58 
59 static PyStructSequence_Desc app_sdl_info_desc = {
60  "bpy.app.sdl", /* name */
61  "This module contains information about SDL blender is linked against", /* doc */
62  app_sdl_info_fields, /* fields */
64 };
65 
66 static PyObject *make_sdl_info(void)
67 {
68  PyObject *sdl_info;
69  int pos = 0;
70 #ifdef WITH_SDL
71  bool sdl_available = false;
72  SDL_version version = {0, 0, 0};
73 #endif
74 
75  sdl_info = PyStructSequence_New(&BlenderAppSDLType);
76  if (sdl_info == NULL) {
77  return NULL;
78  }
79 
80 #define SetStrItem(str) PyStructSequence_SET_ITEM(sdl_info, pos++, PyUnicode_FromString(str))
81 
82 #define SetObjItem(obj) PyStructSequence_SET_ITEM(sdl_info, pos++, obj)
83 
84 #ifdef WITH_SDL
85  SetObjItem(PyBool_FromLong(1));
86 
87 # ifdef WITH_SDL_DYNLOAD
88  if (sdlewInit() == SDLEW_SUCCESS) {
89  SDL_GetVersion(&version);
90  sdl_available = true;
91  }
92 # else /* WITH_SDL_DYNLOAD=OFF */
93  sdl_available = true;
94 # if SDL_MAJOR_VERSION >= 2
95  SDL_GetVersion(&version);
96 # else
97  SDL_VERSION(&version);
98 # endif
99 # endif
100 
101  SetObjItem(PyC_Tuple_Pack_I32(version.major, version.minor, version.patch));
102  if (sdl_available) {
103  SetObjItem(PyUnicode_FromFormat("%d.%d.%d", version.major, version.minor, version.patch));
104  }
105  else {
106  SetStrItem("Unknown");
107  }
108  SetObjItem(PyBool_FromLong(sdl_available));
109 
110 #else /* WITH_SDL=OFF */
111  SetObjItem(PyBool_FromLong(0));
112  SetObjItem(PyC_Tuple_Pack_I32(0, 0, 0));
113  SetStrItem("Unknown");
114  SetObjItem(PyBool_FromLong(0));
115 #endif
116 
117  if (UNLIKELY(PyErr_Occurred())) {
118  Py_DECREF(sdl_info);
119  return NULL;
120  }
121 
122 #undef SetStrItem
123 #undef SetObjItem
124 
125  return sdl_info;
126 }
127 
128 PyObject *BPY_app_sdl_struct(void)
129 {
130  PyObject *ret;
131 
132  PyStructSequence_InitType(&BlenderAppSDLType, &app_sdl_info_desc);
133 
134  ret = make_sdl_info();
135 
136  /* prevent user from creating new instances */
137  BlenderAppSDLType.tp_init = NULL;
138  BlenderAppSDLType.tp_new = NULL;
139  BlenderAppSDLType.tp_hash = (hashfunc)
140  _Py_HashPointer; /* without this we can't do set(sys.modules) T29635. */
141 
142  return ret;
143 }
#define ARRAY_SIZE(arr)
#define UNLIKELY(x)
static PyStructSequence_Desc app_sdl_info_desc
Definition: bpy_app_sdl.c:59
static PyStructSequence_Field app_sdl_info_fields[]
Definition: bpy_app_sdl.c:48
static PyTypeObject BlenderAppSDLType
Definition: bpy_app_sdl.c:46
#define SetStrItem(str)
PyObject * BPY_app_sdl_struct(void)
Definition: bpy_app_sdl.c:128
static PyObject * make_sdl_info(void)
Definition: bpy_app_sdl.c:66
#define SetObjItem(obj)
uint pos
#define PyC_Tuple_Pack_I32(...)
Definition: py_capi_utils.h:67
return ret