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