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