Bevel mod: cleanup flags and extra data.
[blender.git] / source / blender / python / intern / bpy_app_sdl.c
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
17 /** \file
18  * \ingroup pythonintern
19  */
20
21 #include <Python.h>
22 #include "BLI_utildefines.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         {(char *)"supported", (char *)("Boolean, True when Blender is built with SDL support")},
50         {(char *)"version", (char *)("The SDL version as a tuple of 3 numbers")},
51         {(char *)"version_string", (char *)("The SDL version formatted as a string")},
52         {(char *)"available", (char *)("Boolean, True when SDL is available. This is False when "
53                                        "either *supported* is False, or *dynload* is True and "
54                                        "Blender cannot find the correct library.")},
55         {NULL},
56 };
57
58 static PyStructSequence_Desc app_sdl_info_desc = {
59         (char *)"bpy.app.sdl",     /* name */
60         (char *)"This module contains information about SDL blender is linked against",    /* doc */
61         app_sdl_info_fields,    /* fields */
62         ARRAY_SIZE(app_sdl_info_fields) - 1,
63 };
64
65 static PyObject *make_sdl_info(void)
66 {
67         PyObject *sdl_info;
68         int pos = 0;
69 #ifdef WITH_SDL
70         bool sdl_available = false;
71         SDL_version version = {0, 0, 0};
72 #endif
73
74         sdl_info = PyStructSequence_New(&BlenderAppSDLType);
75         if (sdl_info == NULL) {
76                 return NULL;
77         }
78
79 #define SetStrItem(str) \
80         PyStructSequence_SET_ITEM(sdl_info, pos++, PyUnicode_FromString(str))
81
82 #define SetObjItem(obj) \
83         PyStructSequence_SET_ITEM(sdl_info, pos++, obj)
84
85 #ifdef WITH_SDL
86         SetObjItem(PyBool_FromLong(1));
87
88 # ifdef WITH_SDL_DYNLOAD
89         if (sdlewInit() == SDLEW_SUCCESS) {
90                 SDL_GetVersion(&version);
91                 sdl_available = true;
92         }
93 # else // WITH_SDL_DYNLOAD=OFF
94         sdl_available = true;
95 #  if SDL_MAJOR_VERSION >= 2
96         SDL_GetVersion(&version);
97 #  else
98         SDL_VERSION(&version);
99 #  endif
100 # endif
101
102         SetObjItem(PyC_Tuple_Pack_I32(version.major, version.minor, version.patch));
103         if (sdl_available) {
104                 SetObjItem(PyUnicode_FromFormat("%d.%d.%d", version.major, version.minor, version.patch));
105         }
106         else {
107                 SetStrItem("Unknown");
108         }
109         SetObjItem(PyBool_FromLong(sdl_available));
110
111 #else // WITH_SDL=OFF
112         SetObjItem(PyBool_FromLong(0));
113         SetObjItem(PyC_Tuple_Pack_I32(0, 0, 0));
114         SetStrItem("Unknown");
115         SetObjItem(PyBool_FromLong(0));
116 #endif
117
118         if (PyErr_Occurred()) {
119                 Py_CLEAR(sdl_info);
120                 return NULL;
121         }
122
123 #undef SetStrItem
124 #undef SetObjItem
125
126         return sdl_info;
127 }
128
129 PyObject *BPY_app_sdl_struct(void)
130 {
131         PyObject *ret;
132
133         PyStructSequence_InitType(&BlenderAppSDLType, &app_sdl_info_desc);
134
135         ret = make_sdl_info();
136
137         /* prevent user from creating new instances */
138         BlenderAppSDLType.tp_init = NULL;
139         BlenderAppSDLType.tp_new = NULL;
140         BlenderAppSDLType.tp_hash = (hashfunc)_Py_HashPointer; /* without this we can't do set(sys.modules) [#29635] */
141
142         return ret;
143 }