doxygen: prevent GPL license block from being parsed as doxygen comment.
[blender.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 #include <Python.h>
26
27 #include "bpy_app.h"
28 #include "bpy_driver.h"
29
30 #include "BLI_path_util.h"
31 #include "BLI_utildefines.h"
32
33
34 #include "BKE_blender.h"
35 #include "BKE_global.h"
36 #include "structseq.h"
37
38 #include "../generic/py_capi_utils.h"
39
40 #ifdef BUILD_DATE
41 extern char build_date[];
42 extern char build_time[];
43 extern char build_rev[];
44 extern char build_platform[];
45 extern char build_type[];
46 extern char build_cflags[];
47 extern char build_cxxflags[];
48 extern char build_linkflags[];
49 extern char build_system[];
50 #endif
51
52 static PyTypeObject BlenderAppType;
53
54 static PyStructSequence_Field app_info_fields[] = {
55         {(char *)"version", (char *)"The Blender version as a tuple of 3 numbers. eg. (2, 50, 11)"},
56         {(char *)"version_string", (char *)"The Blender version formatted as a string"},
57         {(char *)"binary_path", (char *)"The location of blenders executable, useful for utilities that spawn new instances"},
58         {(char *)"background", (char *)"Boolean, True when blender is running without a user interface (started with -b)"},
59
60         /* buildinfo */
61         {(char *)"build_date", (char *)"The date this blender instance was built"},
62         {(char *)"build_time", (char *)"The time this blender instance was built"},
63         {(char *)"build_revision", (char *)"The subversion revision this blender instance was built with"},
64         {(char *)"build_platform", (char *)"The platform this blender instance was built for"},
65         {(char *)"build_type", (char *)"The type of build (Release, Debug)"},
66         {(char *)"build_cflags", (char *)"C compiler flags"},
67         {(char *)"build_cxxflags", (char *)"C++ compiler flags"},
68         {(char *)"build_linkflags", (char *)"Binary linking flags"},
69         {(char *)"build_system", (char *)"Build system used"},
70         {NULL}
71 };
72
73 static PyStructSequence_Desc app_info_desc = {
74         (char *)"bpy.app",     /* name */
75         (char *)"This module contains application values that remain unchanged during runtime.",    /* doc */
76         app_info_fields,    /* fields */
77         (sizeof(app_info_fields)/sizeof(PyStructSequence_Field)) - 1
78 };
79
80 static PyObject *make_app_info(void)
81 {
82         extern char bprogname[]; /* argv[0] from creator.c */
83
84         PyObject *app_info;
85         int pos = 0;
86
87         app_info = PyStructSequence_New(&BlenderAppType);
88         if (app_info == NULL) {
89                 return NULL;
90         }
91
92 #define SetIntItem(flag) \
93         PyStructSequence_SET_ITEM(app_info, pos++, PyLong_FromLong(flag))
94 #define SetStrItem(str) \
95         PyStructSequence_SET_ITEM(app_info, pos++, PyUnicode_FromString(str))
96 #define SetObjItem(obj) \
97         PyStructSequence_SET_ITEM(app_info, pos++, obj)
98
99         SetObjItem(Py_BuildValue("(iii)", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION));
100         SetObjItem(PyUnicode_FromFormat("%d.%02d (sub %d)", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION));
101         SetStrItem(bprogname);
102         SetObjItem(PyBool_FromLong(G.background));
103
104         /* build info */
105 #ifdef BUILD_DATE
106         SetStrItem(build_date);
107         SetStrItem(build_time);
108         SetStrItem(build_rev);
109         SetStrItem(build_platform);
110         SetStrItem(build_type);
111         SetStrItem(build_cflags);
112         SetStrItem(build_cxxflags);
113         SetStrItem(build_linkflags);
114         SetStrItem(build_system);
115 #else
116         SetStrItem("Unknown");
117         SetStrItem("Unknown");
118         SetStrItem("Unknown");
119         SetStrItem("Unknown");
120         SetStrItem("Unknown");
121         SetStrItem("Unknown");
122         SetStrItem("Unknown");
123         SetStrItem("Unknown");
124         SetStrItem("Unknown");
125 #endif
126
127 #undef SetIntItem
128 #undef SetStrItem
129 #undef SetObjItem
130
131         if (PyErr_Occurred()) {
132                 Py_CLEAR(app_info);
133                 return NULL;
134         }
135         return app_info;
136 }
137
138 /* a few getsets because it makes sense for them to be in bpy.app even though
139  * they are not static */
140 static PyObject *bpy_app_debug_get(PyObject *UNUSED(self), void *UNUSED(closure))
141 {
142         return PyBool_FromLong(G.f & G_DEBUG);
143 }
144
145 static int bpy_app_debug_set(PyObject *UNUSED(self), PyObject *value, void *UNUSED(closure))
146 {
147         int param= PyObject_IsTrue(value);
148
149         if(param < 0) {
150                 PyErr_SetString(PyExc_TypeError, "bpy.app.debug can only be True/False");
151                 return -1;
152         }
153         
154         if(param)       G.f |=  G_DEBUG;
155         else            G.f &= ~G_DEBUG;
156         
157         return 0;
158 }
159
160 static PyObject *bpy_app_debug_value_get(PyObject *UNUSED(self), void *UNUSED(closure))
161 {
162         return PyLong_FromSsize_t(G.rt);
163 }
164
165 static int bpy_app_debug_value_set(PyObject *UNUSED(self), PyObject *value, void *UNUSED(closure))
166 {
167         int param= PyLong_AsSsize_t(value);
168
169         if (param == -1 && PyErr_Occurred()) {
170                 PyErr_SetString(PyExc_TypeError, "bpy.app.debug_value can only be set to a whole number");
171                 return -1;
172         }
173         
174         G.rt= param;
175
176         return 0;
177 }
178
179 static PyObject *bpy_app_tempdir_get(PyObject *UNUSED(self), void *UNUSED(closure))
180 {
181         extern char btempdir[];
182         return PyC_UnicodeFromByte(btempdir);
183 }
184
185 static PyObject *bpy_app_driver_dict_get(PyObject *UNUSED(self), void *UNUSED(closure))
186 {
187         if (bpy_pydriver_Dict == NULL)
188                 if (bpy_pydriver_create_dict() != 0) {
189                         PyErr_SetString(PyExc_RuntimeError, "bpy.app.driver_namespace failed to create dictionary");
190                         return NULL;
191         }
192
193         Py_INCREF(bpy_pydriver_Dict);
194         return bpy_pydriver_Dict;
195 }
196
197
198 static PyGetSetDef bpy_app_getsets[]= {
199         {(char *)"debug", bpy_app_debug_get, bpy_app_debug_set, (char *)"Boolean, set when blender is running in debug mode (started with -d)", NULL},
200         {(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},
201         {(char *)"tempdir", bpy_app_tempdir_get, NULL, (char *)"String, the temp directory used by blender (read-only)", NULL},
202         {(char *)"driver_namespace", bpy_app_driver_dict_get, NULL, (char *)"Dictionary for drivers namespace, editable in-place, reset on file load (read-only)", NULL},
203         {NULL, NULL, NULL, NULL, NULL}
204 };
205
206 static void py_struct_seq_getset_init(void)
207 {
208         /* tricky dynamic members, not to py-spec! */
209         PyGetSetDef *getset;
210
211         for(getset= bpy_app_getsets; getset->name; getset++) {
212                 PyDict_SetItemString(BlenderAppType.tp_dict, getset->name, PyDescr_NewGetSet(&BlenderAppType, getset));
213         }
214 }
215 /* end dynamic bpy.app */
216
217
218 PyObject *BPY_app_struct(void)
219 {
220         PyObject *ret;
221         
222         PyStructSequence_InitType(&BlenderAppType, &app_info_desc);
223
224         ret= make_app_info();
225
226         /* prevent user from creating new instances */
227         BlenderAppType.tp_init = NULL;
228         BlenderAppType.tp_new = NULL;
229
230         /* kindof a hack ontop of PyStructSequence */
231         py_struct_seq_getset_init();
232
233         return ret;
234 }
235