Refactor BKE_blender into separate headers
[blender.git] / source / blender / blenkernel / intern / blender.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  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/blender.c
29  *  \ingroup bke
30  *
31  * Application level startup/shutdown functionality.
32  */
33
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_string.h"
41 #include "BLI_listbase.h"
42 #include "BLI_utildefines.h"
43 #include "BLI_callbacks.h"
44
45 #include "IMB_imbuf.h"
46 #include "IMB_moviecache.h"
47
48 #include "BKE_blender.h"  /* own include */
49 #include "BKE_blender_version.h"  /* own include */
50 #include "BKE_blendfile.h"
51 #include "BKE_brush.h"
52 #include "BKE_context.h"
53 #include "BKE_depsgraph.h"
54 #include "BKE_global.h"
55 #include "BKE_idprop.h"
56 #include "BKE_image.h"
57 #include "BKE_library.h"
58 #include "BKE_node.h"
59 #include "BKE_report.h"
60 #include "BKE_scene.h"
61 #include "BKE_screen.h"
62 #include "BKE_sequencer.h"
63
64 #include "RE_pipeline.h"
65 #include "RE_render_ext.h"
66
67 #include "BLF_api.h"
68
69
70 Global G;
71 UserDef U;
72
73 char versionstr[48] = "";
74
75 /* ********** free ********** */
76
77 /* only to be called on exit blender */
78 void BKE_blender_free(void)
79 {
80         /* samples are in a global list..., also sets G.main->sound->sample NULL */
81         BKE_main_free(G.main);
82         G.main = NULL;
83
84         BKE_spacetypes_free();      /* after free main, it uses space callbacks */
85         
86         IMB_exit();
87         BKE_images_exit();
88         DAG_exit();
89
90         BKE_brush_system_exit();
91         RE_texture_rng_exit();  
92
93         BLI_callback_global_finalize();
94
95         BKE_sequencer_cache_destruct();
96         IMB_moviecache_destruct();
97         
98         free_nodesystem();
99 }
100
101 void BKE_blender_globals_init(void)
102 {
103         memset(&G, 0, sizeof(Global));
104         
105         U.savetime = 1;
106
107         G.main = BKE_main_new();
108
109         strcpy(G.ima, "//");
110
111         if (BLENDER_SUBVERSION)
112                 BLI_snprintf(versionstr, sizeof(versionstr), "v%d.%02d.%d", BLENDER_VERSION / 100, BLENDER_VERSION % 100, BLENDER_SUBVERSION);
113         else
114                 BLI_snprintf(versionstr, sizeof(versionstr), "v%d.%02d", BLENDER_VERSION / 100, BLENDER_VERSION % 100);
115
116 #ifndef WITH_PYTHON_SECURITY /* default */
117         G.f |= G_SCRIPT_AUTOEXEC;
118 #else
119         G.f &= ~G_SCRIPT_AUTOEXEC;
120 #endif
121 }
122
123 void BKE_blender_globals_clear(void)
124 {
125         BKE_main_free(G.main);          /* free all lib data */
126
127         G.main = NULL;
128 }
129
130 /***/
131
132 static void keymap_item_free(wmKeyMapItem *kmi)
133 {
134         if (kmi->properties) {
135                 IDP_FreeProperty(kmi->properties);
136                 MEM_freeN(kmi->properties);
137         }
138         if (kmi->ptr)
139                 MEM_freeN(kmi->ptr);
140 }
141
142 /**
143  * When loading a new userdef from file,
144  * or when exiting Blender.
145  */
146 void BKE_blender_userdef_free(void)
147 {
148         wmKeyMap *km;
149         wmKeyMapItem *kmi;
150         wmKeyMapDiffItem *kmdi;
151         bAddon *addon, *addon_next;
152         uiFont *font;
153
154         for (km = U.user_keymaps.first; km; km = km->next) {
155                 for (kmdi = km->diff_items.first; kmdi; kmdi = kmdi->next) {
156                         if (kmdi->add_item) {
157                                 keymap_item_free(kmdi->add_item);
158                                 MEM_freeN(kmdi->add_item);
159                         }
160                         if (kmdi->remove_item) {
161                                 keymap_item_free(kmdi->remove_item);
162                                 MEM_freeN(kmdi->remove_item);
163                         }
164                 }
165
166                 for (kmi = km->items.first; kmi; kmi = kmi->next)
167                         keymap_item_free(kmi);
168
169                 BLI_freelistN(&km->diff_items);
170                 BLI_freelistN(&km->items);
171         }
172         
173         for (addon = U.addons.first; addon; addon = addon_next) {
174                 addon_next = addon->next;
175                 if (addon->prop) {
176                         IDP_FreeProperty(addon->prop);
177                         MEM_freeN(addon->prop);
178                 }
179                 MEM_freeN(addon);
180         }
181
182         for (font = U.uifonts.first; font; font = font->next) {
183                 BLF_unload_id(font->blf_id);
184         }
185
186         BLF_default_set(-1);
187
188         BLI_freelistN(&U.autoexec_paths);
189
190         BLI_freelistN(&U.uistyles);
191         BLI_freelistN(&U.uifonts);
192         BLI_freelistN(&U.themes);
193         BLI_freelistN(&U.user_keymaps);
194 }
195
196 /**
197  * Handle changes in settings that need refreshing.
198  */
199 void BKE_blender_userdef_refresh(void)
200 {
201         /* prevent accidents */
202         if (U.pixelsize == 0) U.pixelsize = 1;
203         
204         BLF_default_dpi(U.pixelsize * U.dpi);
205         U.widget_unit = (U.pixelsize * U.dpi * 20 + 36) / 72;
206
207 }
208
209 /* *****************  testing for break ************* */
210
211 static void (*blender_test_break_cb)(void) = NULL;
212
213 void BKE_blender_callback_test_break_set(void (*func)(void))
214 {
215         blender_test_break_cb = func;
216 }
217
218
219 int BKE_blender_test_break(void)
220 {
221         if (!G.background) {
222                 if (blender_test_break_cb)
223                         blender_test_break_cb();
224         }
225         
226         return (G.is_break == true);
227 }
228