2.5
[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_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_utildefines.h"
54 #include "BKE_packedFile.h"
55
56 #include "BMF_Api.h"
57 #ifdef INTERNATIONAL
58 #include "FTF_Api.h"
59 #endif
60
61 #include "BLI_blenlib.h"
62
63 #include "RE_pipeline.h"                /* RE_ free stuff */
64
65 #include "radio.h"
66
67 #include "BPY_extern.h"
68
69 #include "SYS_System.h"
70
71 #include "UI_interface.h"
72
73 #include "RNA_define.h"
74
75 #include "WM_api.h"
76 #include "WM_types.h"
77
78 #include "wm_cursors.h"
79 #include "wm_event_system.h"
80 #include "wm.h"
81 #include "wm_files.h"
82 #include "wm_window.h"
83
84 #include "ED_screen.h"
85
86 static void initbuttons(void)
87 {
88         UI_init();
89         
90 //      glClearColor(.7f, .7f, .6f, 0.0);
91         
92         G.font= BMF_GetFont(BMF_kHelvetica12);
93         G.fonts= BMF_GetFont(BMF_kHelvetica10);
94         G.fontss= BMF_GetFont(BMF_kHelveticaBold8);
95         
96 //      clear_matcopybuf(); /* XXX */
97         
98 //      glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
99 }
100
101 /* XXX */
102 static void sound_init_listener(void)
103 {
104         G.listener = MEM_callocN(sizeof(bSoundListener), "soundlistener");
105         G.listener->gain = 1.0;
106         G.listener->dopplerfactor = 1.0;
107         G.listener->dopplervelocity = 340.29f;
108 }
109
110 /* only called once, for startup */
111 void WM_init(bContext *C)
112 {
113         
114         wm_ghost_init(C);       /* note: it assigns C to ghost! */
115         wm_init_cursor_data();
116         wm_operatortype_init();
117         
118         set_free_windowmanager_cb(wm_close_and_free);   /* library.c */
119
120         ED_spacetypes_init();   /* editors/area/spacetype.c */
121                 
122         /* get the default database, plus a wm */
123         WM_read_homefile(C, 0);
124         
125         wm_check(C); /* opens window(s), checks keymaps */
126         
127 //      initscreen();   /* for (visual) speed, this first, then setscreen */
128         initbuttons();
129 //      InitCursorData();
130         sound_init_listener();
131 //      init_node_butfuncs();
132         
133 // XXX  BIF_preview_init_dbase();
134         
135 // XXX  UI_filelist_init_icons();
136         
137 //      init_gl_stuff();        /* drawview.c, after homefile */
138         read_Blog();
139         BLI_strncpy(G.lib, G.sce, FILE_MAX);
140 }
141
142 /* free strings of open recent files */
143 static void free_openrecent(void)
144 {
145         struct RecentFile *recent;
146         
147         for(recent = G.recent_files.first; recent; recent=recent->next)
148                 MEM_freeN(recent->filename);
149         
150         BLI_freelistN(&(G.recent_files));
151 }
152
153
154 /* bad stuff*/
155
156 extern ListBase editNurb;
157 extern ListBase editelems;
158 extern wchar_t *copybuf;
159 extern wchar_t *copybufinfo;
160
161 /* called in creator.c even... tsk, split this! */
162 void WM_exit(bContext *C)
163 {
164         wmWindow *win;
165         
166         /* first wrap up running stuff, we assume only the active WM is running */
167         /* modal handlers are on window level freed, others too? */
168         if(C && C->wm) {
169                 for(win= C->wm->windows.first; win; win= win->next) {
170                         
171                         C->window= win; /* needed by operator close callbacks */
172                         WM_event_remove_handlers(C, &win->handlers);
173                         ED_screen_exit(C, win, win->screen);
174                 }
175         }
176         wm_operatortype_free();
177         
178         free_ttfont(); /* bke_font.h */
179         
180 #ifdef WITH_VERSE
181         end_all_verse_sessions();
182 #endif
183         free_openrecent();
184         
185         freeAllRad();
186         BKE_freecubetable();
187         
188 //      if (G.background == 0)
189 //              sound_end_all_sounds();
190         
191         if(G.obedit) {
192                 if(G.obedit->type==OB_FONT) {
193 //                      free_editText();
194                 }
195 //              else if(G.obedit->type==OB_MBALL) BLI_freelistN(&editelems);
196 //              free_editMesh(G.editMesh);
197         }
198         
199 //      free_editLatt();
200 //      free_editArmature();
201 //      free_posebuf();
202         
203         /* before free_blender so py's gc happens while library still exists */
204         /* needed at least for a rare sigsegv that can happen in pydrivers */
205 //      BPY_end_python();
206         
207 //      fastshade_free_render();        /* shaded view */
208         free_blender();                         /* blender.c, does entire library */
209 //      free_matcopybuf();
210 //      free_ipocopybuf();
211 //      free_actcopybuf();
212 //      free_vertexpaint();
213 //      free_imagepaint();
214         
215         /* editnurb can remain to exist outside editmode */
216         freeNurblist(&editNurb);
217         
218 //      fsmenu_free();
219         
220 #ifdef INTERNATIONAL
221 //      free_languagemenu();
222 #endif
223         
224         RE_FreeAllRender();
225         
226 //      free_txt_data();
227         
228 //      sound_exit_audio();
229         if(G.listener) MEM_freeN(G.listener);
230         
231         
232         libtiff_exit();
233         
234 #ifdef WITH_QUICKTIME
235         quicktime_exit();
236 #endif
237         
238         if (!G.background) {
239 // XXX          UI_filelist_free_icons();
240         }
241         
242 #ifdef INTERNATIONAL
243         FTF_End();
244 #endif
245         
246 //      if (copybuf) MEM_freeN(copybuf);
247 //      if (copybufinfo) MEM_freeN(copybufinfo);
248         
249         /* undo free stuff */
250 //      undo_editmode_clear();
251         
252         BKE_undo_save_quit();   // saves quit.blend if global undo is on
253         BKE_reset_undo(); 
254         
255         UI_exit();
256         BLI_freelistN(&U.themes);
257 // XXX  BIF_preview_free_dbase();
258
259         RNA_exit();
260         
261         MEM_freeN(C);
262         
263         if(MEM_get_memory_blocks_in_use()!=0) {
264                 printf("Error Totblock: %d\n", MEM_get_memory_blocks_in_use());
265                 MEM_printmemlist();
266         }
267 //      delete_autosave();
268         
269         printf("\nBlender quit\n");
270         
271 #ifdef WIN32   
272         /* ask user to press enter when in debug mode */
273         if(G.f & G_DEBUG) {
274                 printf("press enter key to exit...\n\n");
275                 getchar();
276         }
277 #endif 
278         
279         
280         SYS_DeleteSystem(SYS_GetSystem());
281         
282         exit(G.afbreek==1);
283 }
284