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