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