Console Space Type
[blender-staging.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_sound_types.h"
42 #include "DNA_userdef_types.h"
43 #include "DNA_windowmanager_types.h"
44
45 #include "BKE_blender.h"
46 #include "BKE_context.h"
47 #include "BKE_curve.h"
48 #include "BKE_displist.h"
49 #include "BKE_DerivedMesh.h"
50 #include "BKE_font.h"
51 #include "BKE_global.h"
52 #include "BKE_library.h"
53 #include "BKE_mball.h"
54 #include "BKE_report.h"
55 #include "BKE_utildefines.h"
56 #include "BKE_packedFile.h"
57
58 #include "BLI_blenlib.h"
59
60 #include "RE_pipeline.h"                /* RE_ free stuff */
61
62 #ifndef DISABLE_PYTHON
63 #include "BPY_extern.h"
64 #endif
65
66 #include "SYS_System.h"
67
68 #include "RNA_define.h"
69
70 #include "WM_api.h"
71 #include "WM_types.h"
72
73 #include "wm_cursors.h"
74 #include "wm_event_system.h"
75 #include "wm.h"
76 #include "wm_files.h"
77 #include "wm_window.h"
78
79 #include "ED_armature.h"
80 #include "ED_keyframing.h"
81 #include "ED_node.h"
82 #include "ED_previewrender.h"
83 #include "ED_space_api.h"
84 #include "ED_screen.h"
85 #include "ED_util.h"
86
87 #include "UI_interface.h"
88 #include "BLF_api.h"
89
90 #include "GPU_extensions.h"
91 #include "GPU_draw.h"
92
93
94
95 /* XXX */
96 static void sound_init_listener(void)
97 {
98         G.listener = MEM_callocN(sizeof(bSoundListener), "soundlistener");
99         G.listener->gain = 1.0;
100         G.listener->dopplerfactor = 1.0;
101         G.listener->dopplervelocity = 340.29f;
102 }
103
104
105 static void wm_init_reports(bContext *C)
106 {
107         wmWindowManager *wm= CTX_wm_manager(C);
108         wm->reports= MEM_callocN(sizeof(ReportList), "wmReportList");
109         BKE_reports_init(wm->reports, RPT_STORE);
110 }
111 static void wm_free_reports(bContext *C)
112 {
113         wmWindowManager *wm= CTX_wm_manager(C);
114         BKE_reports_clear(wm->reports);
115         MEM_freeN(wm->reports);
116 }
117
118
119
120 /* only called once, for startup */
121 void WM_init(bContext *C)
122 {
123         
124         wm_ghost_init(C);       /* note: it assigns C to ghost! */
125         wm_init_cursor_data();
126         wm_operatortype_init();
127         
128         set_free_windowmanager_cb(wm_close_and_free);   /* library.c */
129         set_blender_test_break_cb(wm_window_testbreak); /* blender.c */
130         
131         ED_spacetypes_init();   /* editors/space_api/spacetype.c */
132         
133         ED_file_init();                 /* for fsmenu */
134         ED_init_node_butfuncs();        
135         
136         BLF_init(11, U.dpi);
137         BLF_lang_init();
138         
139         init_builtin_keyingsets(); /* editors/animation/keyframing.c */
140         
141         /* get the default database, plus a wm */
142         WM_read_homefile(C, NULL);
143         
144         wm_init_reports(C); /* reports cant be initialized before the wm */
145         
146         UI_init();
147         
148         //      clear_matcopybuf(); /* XXX */
149         
150         //      glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
151         
152         sound_init_listener();
153 //      init_node_butfuncs();
154         
155         ED_preview_init_dbase();
156         
157         GPU_extensions_init();
158         
159         G.ndofdevice = -1;      /* XXX bad initializer, needs set otherwise buttons show! */
160         
161         read_Blog();
162         BLI_strncpy(G.lib, G.sce, FILE_MAX);
163 }
164
165 /* free strings of open recent files */
166 static void free_openrecent(void)
167 {
168         struct RecentFile *recent;
169         
170         for(recent = G.recent_files.first; recent; recent=recent->next)
171                 MEM_freeN(recent->filename);
172         
173         BLI_freelistN(&(G.recent_files));
174 }
175
176
177 /* bad stuff*/
178
179 extern ListBase editelems;
180 extern wchar_t *copybuf;
181 extern wchar_t *copybufinfo;
182
183         // XXX copy/paste buffer stuff...
184 extern void free_anim_copybuf(); 
185
186 /* called in creator.c even... tsk, split this! */
187 void WM_exit(bContext *C)
188 {
189         wmWindow *win;
190         
191         /* first wrap up running stuff, we assume only the active WM is running */
192         /* modal handlers are on window level freed, others too? */
193         /* note; same code copied in wm_files.c */
194         if(C && CTX_wm_manager(C)) {
195                 
196                 WM_jobs_stop_all(CTX_wm_manager(C));
197                 
198                 for(win= CTX_wm_manager(C)->windows.first; win; win= win->next) {
199                         
200                         CTX_wm_window_set(C, win);      /* needed by operator close callbacks */
201                         WM_event_remove_handlers(C, &win->handlers);
202                         ED_screen_exit(C, win, win->screen);
203                 }
204         }
205         wm_operatortype_free();
206         
207         /* all non-screen and non-space stuff editors did, like editmode */
208         if(C)
209                 ED_editors_exit(C);
210
211 //      XXX     
212 //      BIF_GlobalReebFree();
213 //      BIF_freeRetarget();
214         BIF_freeTemplates(C);
215         BIF_freeSketch(C);
216         
217         free_ttfont(); /* bke_font.h */
218         
219         free_openrecent();
220         
221         BKE_freecubetable();
222         
223 //      if (G.background == 0)
224 //              sound_end_all_sounds();
225         
226         
227         /* before free_blender so py's gc happens while library still exists */
228         /* needed at least for a rare sigsegv that can happen in pydrivers */
229 #ifndef DISABLE_PYTHON
230         BPY_end_python();
231 #endif
232         
233         fastshade_free_render();        /* shaded view */
234         ED_preview_free_dbase();        /* frees a Main dbase, before free_blender! */
235         free_blender();                         /* blender.c, does entire library and spacetypes */
236 //      free_matcopybuf();
237         free_anim_copybuf();
238 //      free_vertexpaint();
239 //      free_imagepaint();
240         
241 //      fsmenu_free();
242
243         BLF_exit();
244
245         RE_FreeAllRender();
246         
247 //      free_txt_data();
248         
249 //      sound_exit_audio();
250         if(G.listener) MEM_freeN(G.listener);
251         
252         
253         libtiff_exit();
254         
255 #ifdef WITH_QUICKTIME
256         quicktime_exit();
257 #endif
258         
259         if (!G.background) {
260 // XXX          UI_filelist_free_icons();
261         }
262         
263         GPU_extensions_exit();
264         
265 //      if (copybuf) MEM_freeN(copybuf);
266 //      if (copybufinfo) MEM_freeN(copybufinfo);
267         
268         BKE_undo_save_quit();   // saves quit.blend if global undo is on
269         BKE_reset_undo(); 
270         
271         ED_file_exit(); /* for fsmenu */
272
273         UI_exit();
274         BKE_userdef_free();
275
276         RNA_exit();
277         
278         wm_free_reports(C);
279         
280         CTX_free(C);
281         
282         if(MEM_get_memory_blocks_in_use()!=0) {
283                 printf("Error Totblock: %d\n", MEM_get_memory_blocks_in_use());
284                 MEM_printmemlist();
285         }
286 //      delete_autosave();
287         
288         printf("\nBlender quit\n");
289         
290 #ifdef WIN32   
291         /* ask user to press enter when in debug mode */
292         if(G.f & G_DEBUG) {
293                 printf("press enter key to exit...\n\n");
294                 getchar();
295         }
296 #endif 
297         
298         
299         SYS_DeleteSystem(SYS_GetSystem());
300         
301         exit(G.afbreek==1);
302 }
303