d62ed7dd13a3bbd650cf15d2a8d6cccfb1044b88
[blender.git] / source / blender / windowmanager / intern / wm_init_exit.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2007 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32
33 #include "MEM_guardedalloc.h"
34 #include "MEM_CacheLimiterC-Api.h"
35
36 #include "IMB_imbuf_types.h"
37 #include "IMB_imbuf.h"
38
39 #include "DNA_object_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_userdef_types.h"
42 #include "DNA_windowmanager_types.h"
43
44 #include "BKE_blender.h"
45 #include "BKE_context.h"
46 #include "BKE_curve.h"
47 #include "BKE_displist.h"
48 #include "BKE_DerivedMesh.h"
49 #include "BKE_font.h"
50 #include "BKE_global.h"
51 #include "BKE_library.h"
52 #include "BKE_mball.h"
53 #include "BKE_report.h"
54 #include "BKE_utildefines.h"
55 #include "BKE_packedFile.h"
56
57 #include "BLI_blenlib.h"
58
59 #include "RE_pipeline.h"                /* RE_ free stuff */
60
61 #ifndef DISABLE_PYTHON
62 #include "BPY_extern.h"
63 #endif
64
65 #include "SYS_System.h"
66
67 #include "RNA_define.h"
68
69 #include "WM_api.h"
70 #include "WM_types.h"
71
72 #include "wm_cursors.h"
73 #include "wm_event_system.h"
74 #include "wm.h"
75 #include "wm_files.h"
76 #include "wm_window.h"
77
78 #include "ED_armature.h"
79 #include "ED_keyframing.h"
80 #include "ED_node.h"
81 #include "ED_render.h"
82 #include "ED_space_api.h"
83 #include "ED_screen.h"
84 #include "ED_util.h"
85
86 #include "UI_interface.h"
87 #include "BLF_api.h"
88
89 #include "gpu_buffers.h"
90 #include "GPU_extensions.h"
91 #include "GPU_draw.h"
92
93 #include "BKE_depsgraph.h"
94 #include "BKE_sound.h"
95
96 static void wm_init_reports(bContext *C)
97 {
98         BKE_reports_init(CTX_wm_reports(C), RPT_STORE);
99 }
100 static void wm_free_reports(bContext *C)
101 {
102         BKE_reports_clear(CTX_wm_reports(C));
103 }
104
105
106
107 /* only called once, for startup */
108 void WM_init(bContext *C)
109 {
110         
111         if (!G.background) {
112                 wm_ghost_init(C);       /* note: it assigns C to ghost! */
113                 wm_init_cursor_data();
114         }
115         wm_operatortype_init();
116         
117         set_free_windowmanager_cb(wm_close_and_free);   /* library.c */
118         set_blender_test_break_cb(wm_window_testbreak); /* blender.c */
119         DAG_editors_update_cb(ED_render_id_flush_update); /* depsgraph.c */
120         
121         ED_spacetypes_init();   /* editors/space_api/spacetype.c */
122         
123         ED_file_init();                 /* for fsmenu */
124         ED_init_node_butfuncs();        
125         
126         BLF_init(11, U.dpi);
127         BLF_lang_init();
128         
129         init_builtin_keyingsets(); /* editors/animation/keyframing.c */
130         
131         /* get the default database, plus a wm */
132         WM_read_homefile(C, NULL);
133         
134         wm_init_reports(C); /* reports cant be initialized before the wm */
135
136         if (!G.background) {
137                 GPU_extensions_init();
138         
139                 UI_init();
140         }
141         
142         //      clear_matcopybuf(); /* XXX */
143         
144         //      glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
145         
146 //      init_node_butfuncs();
147         
148         ED_preview_init_dbase();
149         
150         G.ndofdevice = -1;      /* XXX bad initializer, needs set otherwise buttons show! */
151         
152         read_Blog();
153         BLI_strncpy(G.lib, G.sce, FILE_MAX);
154
155 }
156
157 void WM_init_splash(bContext *C)
158 {
159         wmWindowManager *wm= CTX_wm_manager(C);
160         wmWindow *prevwin= CTX_wm_window(C);
161         
162         if(wm->windows.first) {
163                 CTX_wm_window_set(C, wm->windows.first);
164
165                 /* needed to get the right screen size for centering the splash */
166                 wm_window_process_events(C);
167
168                 WM_operator_name_call(C, "WM_OT_splash", WM_OP_INVOKE_DEFAULT, NULL);
169                 CTX_wm_window_set(C, prevwin);
170         }
171 }
172
173 /* free strings of open recent files */
174 static void free_openrecent(void)
175 {
176         struct RecentFile *recent;
177         
178         for(recent = G.recent_files.first; recent; recent=recent->next)
179                 MEM_freeN(recent->filename);
180         
181         BLI_freelistN(&(G.recent_files));
182 }
183
184
185 /* bad stuff*/
186
187 extern ListBase editelems;
188 extern wchar_t *copybuf;
189 extern wchar_t *copybufinfo;
190
191         // XXX copy/paste buffer stuff...
192 extern void free_anim_copybuf(); 
193 extern void free_anim_drivers_copybuf(); 
194 extern void free_posebuf(); 
195
196 /* called in creator.c even... tsk, split this! */
197 void WM_exit(bContext *C)
198 {
199         wmWindow *win;
200
201         sound_exit();
202
203         /* first wrap up running stuff, we assume only the active WM is running */
204         /* modal handlers are on window level freed, others too? */
205         /* note; same code copied in wm_files.c */
206         if(C && CTX_wm_manager(C)) {
207                 
208                 WM_jobs_stop_all(CTX_wm_manager(C));
209                 
210                 for(win= CTX_wm_manager(C)->windows.first; win; win= win->next) {
211                         
212                         CTX_wm_window_set(C, win);      /* needed by operator close callbacks */
213                         WM_event_remove_handlers(C, &win->handlers);
214                         WM_event_remove_handlers(C, &win->modalhandlers);
215                         ED_screen_exit(C, win, win->screen);
216                 }
217         }
218         wm_operatortype_free();
219         WM_menutype_free();
220         
221         /* all non-screen and non-space stuff editors did, like editmode */
222         if(C)
223                 ED_editors_exit(C);
224
225 //      XXX     
226 //      BIF_GlobalReebFree();
227 //      BIF_freeRetarget();
228         BIF_freeTemplates(C);
229         
230         free_ttfont(); /* bke_font.h */
231         
232         free_openrecent();
233         
234         BKE_freecubetable();
235         
236         fastshade_free_render();        /* shaded view */
237         ED_preview_free_dbase();        /* frees a Main dbase, before free_blender! */
238
239         if(C && CTX_wm_manager(C))
240                 wm_free_reports(C);                     /* before free_blender! - since the ListBases get freed there */
241                 
242         free_blender();                         /* blender.c, does entire library and spacetypes */
243 //      free_matcopybuf();
244         free_anim_copybuf();
245         free_anim_drivers_copybuf();
246         free_posebuf();
247 //      free_vertexpaint();
248 //      free_imagepaint();
249         
250 //      fsmenu_free();
251
252         BLF_exit();
253
254         RE_FreeAllRender();
255         RE_engines_exit();
256         
257 //      free_txt_data();
258         
259
260 #ifndef DISABLE_PYTHON
261         /* XXX - old note */
262         /* before free_blender so py's gc happens while library still exists */
263         /* needed at least for a rare sigsegv that can happen in pydrivers */
264
265         /* Update for blender 2.5, move after free_blender because blender now holds references to PyObject's
266          * so decref'ing them after python ends causes bad problems every time
267          * the pyDriver bug can be fixed if it happens again we can deal with it then */
268         BPY_end_python();
269 #endif
270
271         libtiff_exit();
272         
273 #ifdef WITH_QUICKTIME
274         quicktime_exit();
275 #endif
276         
277         if (!G.background) {
278 // XXX          UI_filelist_free_icons();
279         }
280         
281         GPU_buffer_pool_free(0);
282         GPU_extensions_exit();
283         
284 //      if (copybuf) MEM_freeN(copybuf);
285 //      if (copybufinfo) MEM_freeN(copybufinfo);
286         
287         BKE_undo_save_quit();   // saves quit.blend if global undo is on
288         BKE_reset_undo(); 
289         
290         ED_file_exit(); /* for fsmenu */
291
292         UI_exit();
293         BKE_userdef_free();
294
295         RNA_exit(); /* should be after BPY_end_python so struct python slots are cleared */
296         
297         wm_ghost_exit();
298
299         CTX_free(C);
300         
301         SYS_DeleteSystem(SYS_GetSystem());
302
303         if(MEM_get_memory_blocks_in_use()!=0) {
304                 printf("Error Totblock: %d\n", MEM_get_memory_blocks_in_use());
305                 MEM_printmemlist();
306         }
307         wm_autosave_delete();
308         
309         printf("\nBlender quit\n");
310         
311 #ifdef WIN32   
312         /* ask user to press enter when in debug mode */
313         if(G.f & G_DEBUG) {
314                 printf("press enter key to exit...\n\n");
315                 getchar();
316         }
317 #endif 
318         
319         exit(G.afbreek==1);
320 }
321