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