Code cleanup: don't use btempdir/bprogdir/bprogname globals anymore, but wrap
[blender-staging.git] / source / blender / python / intern / bpy_app.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Contributor(s): Campbell Barton
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file blender/python/intern/bpy_app.c
26  *  \ingroup pythonintern
27  */
28
29
30 #include <Python.h>
31
32 #include "bpy_app.h"
33 #include "bpy_app_handlers.h"
34 #include "bpy_driver.h"
35
36 #include "BLI_path_util.h"
37 #include "BLI_utildefines.h"
38
39
40 #include "BKE_blender.h"
41 #include "BKE_global.h"
42 #include "structseq.h"
43
44 #include "../generic/py_capi_utils.h"
45
46 #ifdef BUILD_DATE
47 extern char build_date[];
48 extern char build_time[];
49 extern char build_rev[];
50 extern char build_platform[];
51 extern char build_type[];
52 extern char build_cflags[];
53 extern char build_cxxflags[];
54 extern char build_linkflags[];
55 extern char build_system[];
56 #endif
57
58 static PyTypeObject BlenderAppType;
59
60 static PyStructSequence_Field app_info_fields[]= {
61         {(char *)"version", (char *)"The Blender version as a tuple of 3 numbers. eg. (2, 50, 11)"},
62         {(char *)"version_string", (char *)"The Blender version formatted as a string"},
63         {(char *)"version_char", (char *)"The Blender version character (for minor releases)"},
64         {(char *)"version_cycle", (char *)"The release status of this build alpha/beta/rc/release"},
65         {(char *)"binary_path", (char *)"The location of blenders executable, useful for utilities that spawn new instances"},
66         {(char *)"background", (char *)"Boolean, True when blender is running without a user interface (started with -b)"},
67
68         /* buildinfo */
69         {(char *)"build_date", (char *)"The date this blender instance was built"},
70         {(char *)"build_time", (char *)"The time this blender instance was built"},
71         {(char *)"build_revision", (char *)"The subversion revision this blender instance was built with"},
72         {(char *)"build_platform", (char *)"The platform this blender instance was built for"},
73         {(char *)"build_type", (char *)"The type of build (Release, Debug)"},
74         {(char *)"build_cflags", (char *)"C compiler flags"},
75         {(char *)"build_cxxflags", (char *)"C++ compiler flags"},
76         {(char *)"build_linkflags", (char *)"Binary linking flags"},
77         {(char *)"build_system", (char *)"Build system used"},
78
79         /* submodules */
80         {(char *)"handlers", (char *)"Application handler callbacks"},
81         {NULL}
82 };
83
84 static PyStructSequence_Desc app_info_desc= {
85         (char *)"bpy.app",     /* name */
86         (char *)"This module contains application values that remain unchanged during runtime.",    /* doc */
87         app_info_fields,    /* fields */
88         (sizeof(app_info_fields)/sizeof(PyStructSequence_Field)) - 1
89 };
90
91 #define DO_EXPAND(VAL)  VAL ## 1
92 #define EXPAND(VAL)     DO_EXPAND(VAL)
93
94 static PyObject *make_app_info(void)
95 {
96         PyObject *app_info;
97         int pos= 0;
98
99         app_info= PyStructSequence_New(&BlenderAppType);
100         if (app_info == NULL) {
101                 return NULL;
102         }
103
104 #define SetIntItem(flag) \
105         PyStructSequence_SET_ITEM(app_info, pos++, PyLong_FromLong(flag))
106 #define SetStrItem(str) \
107         PyStructSequence_SET_ITEM(app_info, pos++, PyUnicode_FromString(str))
108 #define SetObjItem(obj) \
109         PyStructSequence_SET_ITEM(app_info, pos++, obj)
110
111         SetObjItem(Py_BuildValue("(iii)", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION));
112         SetObjItem(PyUnicode_FromFormat("%d.%02d (sub %d)", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION));
113 #if defined(BLENDER_VERSION_CHAR) && EXPAND(BLENDER_VERSION_CHAR) != 1
114         SetStrItem(STRINGIFY(BLENDER_VERSION_CHAR));
115 #else
116         SetStrItem("");
117 #endif
118         SetStrItem(STRINGIFY(BLENDER_VERSION_CYCLE));
119         SetStrItem(BLI_program_path());
120         SetObjItem(PyBool_FromLong(G.background));
121
122         /* build info */
123 #ifdef BUILD_DATE
124         SetStrItem(build_date);
125         SetStrItem(build_time);
126         SetStrItem(build_rev);
127         SetStrItem(build_platform);
128         SetStrItem(build_type);
129         SetStrItem(build_cflags);
130         SetStrItem(build_cxxflags);
131         SetStrItem(build_linkflags);
132         SetStrItem(build_system);
133 #else
134         SetStrItem("Unknown");
135         SetStrItem("Unknown");
136         SetStrItem("Unknown");
137         SetStrItem("Unknown");
138         SetStrItem("Unknown");
139         SetStrItem("Unknown");
140         SetStrItem("Unknown");
141         SetStrItem("Unknown");
142         SetStrItem("Unknown");
143 #endif
144
145         SetObjItem(BPY_app_handlers_struct());
146
147 #undef SetIntItem
148 #undef SetStrItem
149 #undef SetObjItem
150
151         if (PyErr_Occurred()) {
152                 Py_CLEAR(app_info);
153                 return NULL;
154         }
155         return app_info;
156 }
157
158 /* a few getsets because it makes sense for them to be in bpy.app even though
159  * they are not static */
160 static PyObject *bpy_app_debug_get(PyObject *UNUSED(self), void *UNUSED(closure))
161 {
162         return PyBool_FromLong(G.f & G_DEBUG);
163 }
164
165 static int bpy_app_debug_set(PyObject *UNUSED(self), PyObject *value, void *UNUSED(closure))
166 {
167         int param= PyObject_IsTrue(value);
168
169         if (param < 0) {
170                 PyErr_SetString(PyExc_TypeError, "bpy.app.debug can only be True/False");
171                 return -1;
172         }
173         
174         if (param)  G.f |=  G_DEBUG;
175         else        G.f &= ~G_DEBUG;
176         
177         return 0;
178 }
179
180 static PyObject *bpy_app_debug_value_get(PyObject *UNUSED(self), void *UNUSED(closure))
181 {
182         return PyLong_FromSsize_t(G.rt);
183 }
184
185 static int bpy_app_debug_value_set(PyObject *UNUSED(self), PyObject *value, void *UNUSED(closure))
186 {
187         int param= PyLong_AsSsize_t(value);
188
189         if (param == -1 && PyErr_Occurred()) {
190                 PyErr_SetString(PyExc_TypeError, "bpy.app.debug_value can only be set to a whole number");
191                 return -1;
192         }
193         
194         G.rt= param;
195
196         return 0;
197 }
198
199 static PyObject *bpy_app_tempdir_get(PyObject *UNUSED(self), void *UNUSED(closure))
200 {
201         return PyC_UnicodeFromByte(BLI_temporary_dir());
202 }
203
204 static PyObject *bpy_app_driver_dict_get(PyObject *UNUSED(self), void *UNUSED(closure))
205 {
206         if (bpy_pydriver_Dict == NULL)
207                 if (bpy_pydriver_create_dict() != 0) {
208                         PyErr_SetString(PyExc_RuntimeError, "bpy.app.driver_namespace failed to create dictionary");
209                         return NULL;
210         }
211
212         Py_INCREF(bpy_pydriver_Dict);
213         return bpy_pydriver_Dict;
214 }
215
216
217 static PyGetSetDef bpy_app_getsets[]= {
218         {(char *)"debug", bpy_app_debug_get, bpy_app_debug_set, (char *)"Boolean, set when blender is running in debug mode (started with -d)", NULL},
219         {(char *)"debug_value", bpy_app_debug_value_get, bpy_app_debug_value_set, (char *)"Int, number which can be set to non-zero values for testing purposes.", NULL},
220         {(char *)"tempdir", bpy_app_tempdir_get, NULL, (char *)"String, the temp directory used by blender (read-only)", NULL},
221         {(char *)"driver_namespace", bpy_app_driver_dict_get, NULL, (char *)"Dictionary for drivers namespace, editable in-place, reset on file load (read-only)", NULL},
222         {NULL, NULL, NULL, NULL, NULL}
223 };
224
225 static void py_struct_seq_getset_init(void)
226 {
227         /* tricky dynamic members, not to py-spec! */
228         PyGetSetDef *getset;
229
230         for (getset= bpy_app_getsets; getset->name; getset++) {
231                 PyDict_SetItemString(BlenderAppType.tp_dict, getset->name, PyDescr_NewGetSet(&BlenderAppType, getset));
232         }
233 }
234 /* end dynamic bpy.app */
235
236
237 PyObject *BPY_app_struct(void)
238 {
239         PyObject *ret;
240         
241         PyStructSequence_InitType(&BlenderAppType, &app_info_desc);
242
243         ret= make_app_info();
244
245         /* prevent user from creating new instances */
246         BlenderAppType.tp_init= NULL;
247         BlenderAppType.tp_new= NULL;
248
249         /* kindof a hack ontop of PyStructSequence */
250         py_struct_seq_getset_init();
251
252         return ret;
253 }
254