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_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_screen.h"
86 #include "ED_util.h"
87
88 #include "UI_interface.h"
89
90 #include "GPU_extensions.h"
91 #include "GPU_draw.h"
92
93
94 static void initbuttons(void)
95 {
96         UI_init();
97         
98 //      glClearColor(.7f, .7f, .6f, 0.0);
99         
100         G.font= BMF_GetFont(BMF_kHelvetica12);
101         G.fonts= BMF_GetFont(BMF_kHelvetica10);
102         G.fontss= BMF_GetFont(BMF_kHelveticaBold8);
103         
104 //      clear_matcopybuf(); /* XXX */
105         
106 //      glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
107 }
108
109 /* XXX */
110 static void sound_init_listener(void)
111 {
112         G.listener = MEM_callocN(sizeof(bSoundListener), "soundlistener");
113         G.listener->gain = 1.0;
114         G.listener->dopplerfactor = 1.0;
115         G.listener->dopplervelocity = 340.29f;
116 }
117
118 /* only called once, for startup */
119 void WM_init(bContext *C)
120 {
121         
122         wm_ghost_init(C);       /* note: it assigns C to ghost! */
123         wm_init_cursor_data();
124         wm_operatortype_init();
125         
126         set_free_windowmanager_cb(wm_close_and_free);   /* library.c */
127         set_blender_test_break_cb(wm_window_testbreak); /* blender.c */
128         
129         ED_spacetypes_init();   /* editors/area/spacetype.c */
130                 
131         /* get the default database, plus a wm */
132         WM_read_homefile(C, 0);
133         
134         wm_check(C); /* opens window(s), checks keymaps */
135         
136 //      initscreen();   /* for (visual) speed, this first, then setscreen */
137         initbuttons();
138 //      InitCursorData();
139         sound_init_listener();
140 //      init_node_butfuncs();
141         
142 // XXX  BIF_preview_init_dbase();
143         
144 // XXX  UI_filelist_init_icons();
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 editNurb;
169 extern ListBase editelems;
170 extern wchar_t *copybuf;
171 extern wchar_t *copybufinfo;
172
173         // XXX copy/paste buffer stuff...
174 extern void free_actcopybuf(); 
175
176 /* called in creator.c even... tsk, split this! */
177 void WM_exit(bContext *C)
178 {
179         wmWindow *win;
180         
181         /* first wrap up running stuff, we assume only the active WM is running */
182         /* modal handlers are on window level freed, others too? */
183         /* note; same code copied in wm_files.c */
184         if(C && CTX_wm_manager(C)) {
185                 for(win= CTX_wm_manager(C)->windows.first; win; win= win->next) {
186                         
187                         CTX_wm_window_set(C, win);      /* needed by operator close callbacks */
188                         WM_event_remove_handlers(C, &win->handlers);
189                         ED_screen_exit(C, win, win->screen);
190                 }
191         }
192         wm_operatortype_free();
193         
194         /* all non-screen and non-space stuff editors did, like editmode */
195         if(C)
196                 ED_editors_exit(C);
197         
198         free_ttfont(); /* bke_font.h */
199         
200 #ifdef WITH_VERSE
201         end_all_verse_sessions();
202 #endif
203         free_openrecent();
204         
205         freeAllRad();
206         BKE_freecubetable();
207         
208 //      if (G.background == 0)
209 //              sound_end_all_sounds();
210         
211         
212         /* before free_blender so py's gc happens while library still exists */
213         /* needed at least for a rare sigsegv that can happen in pydrivers */
214 #ifndef DISABLE_PYTHON
215         BPY_end_python();
216 #endif
217         
218 //      fastshade_free_render();        /* shaded view */
219         free_blender();                         /* blender.c, does entire library */
220 //      free_matcopybuf();
221 //      free_ipocopybuf();
222         free_actcopybuf();
223 //      free_vertexpaint();
224 //      free_imagepaint();
225         
226         /* editnurb can remain to exist outside editmode */
227         freeNurblist(&editNurb);
228         
229 //      fsmenu_free();
230         
231 #ifdef INTERNATIONAL
232 //      free_languagemenu();
233 #endif
234         
235         RE_FreeAllRender();
236         
237 //      free_txt_data();
238         
239 //      sound_exit_audio();
240         if(G.listener) MEM_freeN(G.listener);
241         
242         
243         libtiff_exit();
244         
245 #ifdef WITH_QUICKTIME
246         quicktime_exit();
247 #endif
248         
249         if (!G.background) {
250 // XXX          UI_filelist_free_icons();
251         }
252         
253 #ifdef INTERNATIONAL
254         FTF_End();
255 #endif
256         
257 //      if (copybuf) MEM_freeN(copybuf);
258 //      if (copybufinfo) MEM_freeN(copybufinfo);
259         
260         /* undo free stuff */
261 //      undo_editmode_clear();
262         
263         BKE_undo_save_quit();   // saves quit.blend if global undo is on
264         BKE_reset_undo(); 
265         
266         UI_exit();
267         BLI_freelistN(&U.themes);
268 // XXX  BIF_preview_free_dbase();
269
270         RNA_exit();
271         
272         CTX_free(C);
273         
274         if(MEM_get_memory_blocks_in_use()!=0) {
275                 printf("Error Totblock: %d\n", MEM_get_memory_blocks_in_use());
276                 MEM_printmemlist();
277         }
278 //      delete_autosave();
279         
280         printf("\nBlender quit\n");
281         
282 #ifdef WIN32   
283         /* ask user to press enter when in debug mode */
284         if(G.f & G_DEBUG) {
285                 printf("press enter key to exit...\n\n");
286                 getchar();
287         }
288 #endif 
289         
290         
291         SYS_DeleteSystem(SYS_GetSystem());
292         
293         exit(G.afbreek==1);
294 }
295