Bringing back "Auto Start" option in the Game Menu.
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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_userdef_types.h"
42 #include "DNA_windowmanager_types.h"
43
44 #include "BKE_blender.h"
45 #include "BKE_context.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_report.h"
54 #include "BKE_utildefines.h"
55 #include "BKE_packedFile.h"
56 #include "BKE_sequencer.h" /* free seq clipboard */
57
58 #include "BLI_blenlib.h"
59
60 #include "RE_pipeline.h"                /* RE_ free stuff */
61
62 #ifndef DISABLE_PYTHON
63 #include "BPY_extern.h"
64 #endif
65
66 #include "SYS_System.h"
67
68 #include "RNA_define.h"
69
70 #include "WM_api.h"
71 #include "WM_types.h"
72
73 #include "wm_cursors.h"
74 #include "wm_event_system.h"
75 #include "wm.h"
76 #include "wm_files.h"
77 #include "wm_window.h"
78
79 #include "ED_armature.h"
80 #include "ED_keyframing.h"
81 #include "ED_node.h"
82 #include "ED_render.h"
83 #include "ED_space_api.h"
84 #include "ED_screen.h"
85 #include "ED_util.h"
86
87 #include "UI_interface.h"
88 #include "BLF_api.h"
89
90 #include "gpu_buffers.h"
91 #include "GPU_extensions.h"
92 #include "GPU_draw.h"
93
94 #include "BKE_depsgraph.h"
95 #include "BKE_sound.h"
96
97 static void wm_init_reports(bContext *C)
98 {
99         BKE_reports_init(CTX_wm_reports(C), RPT_STORE);
100 }
101 static void wm_free_reports(bContext *C)
102 {
103         BKE_reports_clear(CTX_wm_reports(C));
104 }
105
106
107
108 /* only called once, for startup */
109 void WM_init(bContext *C, int argc, char **argv)
110 {
111
112         if (!G.background) {
113                 wm_ghost_init(C);       /* note: it assigns C to ghost! */
114                 wm_init_cursor_data();
115         }
116         wm_operatortype_init();
117         
118         set_free_windowmanager_cb(wm_close_and_free);   /* library.c */
119         set_blender_test_break_cb(wm_window_testbreak); /* blender.c */
120         DAG_editors_update_cb(ED_render_id_flush_update); /* depsgraph.c */
121         
122         ED_spacetypes_init();   /* editors/space_api/spacetype.c */
123         
124         ED_file_init();                 /* for fsmenu */
125         ED_init_node_butfuncs();        
126         
127         BLF_init(11, U.dpi);
128         BLF_lang_init();
129         
130         init_builtin_keyingsets(); /* editors/animation/keyframing.c */
131         
132         /* get the default database, plus a wm */
133         WM_read_homefile(C, NULL);
134
135         /* note: there is a bug where python needs initializing before loading the
136          * .B25.blend because it may contain PyDrivers. It also needs to be after
137          * initializing space types and other internal data.
138          *
139          * However cant redo this at the moment. Solution is to load python
140          * before WM_read_homefile() or make py-drivers check if python is running.
141          * Will try fix when the crash can be repeated. - campbell. */
142
143 #ifndef DISABLE_PYTHON
144         BPY_set_context(C); /* necessary evil */
145         BPY_start_python(argc, argv);
146         BPY_load_user_modules(C);
147 #endif
148
149         wm_init_reports(C); /* reports cant be initialized before the wm */
150
151         if (!G.background) {
152                 GPU_extensions_init();
153         
154                 UI_init();
155         }
156         
157         //      clear_matcopybuf(); /* XXX */
158         
159         //      glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
160         
161 //      init_node_butfuncs();
162         
163         ED_preview_init_dbase();
164         
165         G.ndofdevice = -1;      /* XXX bad initializer, needs set otherwise buttons show! */
166         
167         read_Blog();
168         BLI_strncpy(G.lib, G.sce, FILE_MAX);
169
170 }
171
172 void WM_init_splash(bContext *C)
173 {
174         wmWindowManager *wm= CTX_wm_manager(C);
175         wmWindow *prevwin= CTX_wm_window(C);
176         
177         if(wm->windows.first) {
178                 CTX_wm_window_set(C, wm->windows.first);
179                 WM_operator_name_call(C, "WM_OT_splash", WM_OP_INVOKE_DEFAULT, NULL);
180                 CTX_wm_window_set(C, prevwin);
181         }
182 }
183
184 void WM_init_game(bContext *C)
185 {
186         //XXX copied from WM_init_splash we may not even need those "window" related code
187         //XXX not working yet, it fails at the game_start_operator pool (it needs an area)
188         wmWindowManager *wm= CTX_wm_manager(C);
189         wmWindow *prevwin= CTX_wm_window(C);
190         
191         if(wm->windows.first) {
192                 CTX_wm_window_set(C, wm->windows.first);
193                 WM_operator_name_call(C, "VIEW3D_OT_game_start", WM_OP_EXEC_DEFAULT, NULL);
194                 CTX_wm_window_set(C, prevwin);
195         }
196 }
197
198 /* free strings of open recent files */
199 static void free_openrecent(void)
200 {
201         struct RecentFile *recent;
202         
203         for(recent = G.recent_files.first; recent; recent=recent->next)
204                 MEM_freeN(recent->filename);
205         
206         BLI_freelistN(&(G.recent_files));
207 }
208
209
210 /* bad stuff*/
211
212 extern ListBase editelems;
213 extern wchar_t *copybuf;
214 extern wchar_t *copybufinfo;
215
216         // XXX copy/paste buffer stuff...
217 extern void free_anim_copybuf(); 
218 extern void free_anim_drivers_copybuf(); 
219 extern void free_posebuf(); 
220
221 /* called in creator.c even... tsk, split this! */
222 void WM_exit(bContext *C)
223 {
224         wmWindow *win;
225
226         sound_exit();
227
228         /* first wrap up running stuff, we assume only the active WM is running */
229         /* modal handlers are on window level freed, others too? */
230         /* note; same code copied in wm_files.c */
231         if(C && CTX_wm_manager(C)) {
232                 
233                 WM_jobs_stop_all(CTX_wm_manager(C));
234                 
235                 for(win= CTX_wm_manager(C)->windows.first; win; win= win->next) {
236                         
237                         CTX_wm_window_set(C, win);      /* needed by operator close callbacks */
238                         WM_event_remove_handlers(C, &win->handlers);
239                         WM_event_remove_handlers(C, &win->modalhandlers);
240                         ED_screen_exit(C, win, win->screen);
241                 }
242         }
243         wm_operatortype_free();
244         wm_dropbox_free();
245         WM_menutype_free();
246         
247         /* all non-screen and non-space stuff editors did, like editmode */
248         if(C)
249                 ED_editors_exit(C);
250
251 //      XXX     
252 //      BIF_GlobalReebFree();
253 //      BIF_freeRetarget();
254         BIF_freeTemplates(C);
255         
256         free_ttfont(); /* bke_font.h */
257         
258         free_openrecent();
259         
260         BKE_freecubetable();
261         
262         fastshade_free_render();        /* shaded view */
263         ED_preview_free_dbase();        /* frees a Main dbase, before free_blender! */
264
265         if(C && CTX_wm_manager(C))
266                 wm_free_reports(C);                     /* before free_blender! - since the ListBases get freed there */
267
268         seq_free_clipboard(); /* sequencer.c */
269                 
270         free_blender();                         /* blender.c, does entire library and spacetypes */
271 //      free_matcopybuf();
272         free_anim_copybuf();
273         free_anim_drivers_copybuf();
274         free_posebuf();
275 //      free_vertexpaint();
276 //      free_imagepaint();
277         
278 //      fsmenu_free();
279
280         BLF_exit();
281
282         RE_FreeAllRender();
283         RE_engines_exit();
284         
285 //      free_txt_data();
286         
287
288 #ifndef DISABLE_PYTHON
289         /* XXX - old note */
290         /* before free_blender so py's gc happens while library still exists */
291         /* needed at least for a rare sigsegv that can happen in pydrivers */
292
293         /* Update for blender 2.5, move after free_blender because blender now holds references to PyObject's
294          * so decref'ing them after python ends causes bad problems every time
295          * the pyDriver bug can be fixed if it happens again we can deal with it then */
296         BPY_end_python();
297 #endif
298
299         libtiff_exit();
300         
301 #ifdef WITH_QUICKTIME
302         quicktime_exit();
303 #endif
304         
305         if (!G.background) {
306 // XXX          UI_filelist_free_icons();
307         }
308         
309         GPU_buffer_pool_free(0);
310         GPU_extensions_exit();
311         
312 //      if (copybuf) MEM_freeN(copybuf);
313 //      if (copybufinfo) MEM_freeN(copybufinfo);
314         
315         BKE_undo_save_quit();   // saves quit.blend if global undo is on
316         BKE_reset_undo(); 
317         
318         ED_file_exit(); /* for fsmenu */
319
320         UI_exit();
321         BKE_userdef_free();
322
323         RNA_exit(); /* should be after BPY_end_python so struct python slots are cleared */
324         
325         wm_ghost_exit();
326
327         CTX_free(C);
328         
329         SYS_DeleteSystem(SYS_GetSystem());
330
331         if(MEM_get_memory_blocks_in_use()!=0) {
332                 printf("Error Totblock: %d\n", MEM_get_memory_blocks_in_use());
333                 MEM_printmemlist();
334         }
335         wm_autosave_delete();
336         
337         printf("\nBlender quit\n");
338         
339 #ifdef WIN32   
340         /* ask user to press enter when in debug mode */
341         if(G.f & G_DEBUG) {
342                 printf("press enter key to exit...\n\n");
343                 getchar();
344         }
345 #endif 
346         
347         exit(G.afbreek==1);
348 }
349