fix for crash generating messages.txt, I wasn't getting this crash because I had...
[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., 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 /** \file blender/windowmanager/intern/wm_init_exit.c
30  *  \ingroup wm
31  */
32
33
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37
38 #include "MEM_guardedalloc.h"
39 #include "MEM_CacheLimiterC-Api.h"
40
41 #include "IMB_imbuf_types.h"
42 #include "IMB_imbuf.h"
43
44 #include "DNA_object_types.h"
45 #include "DNA_scene_types.h"
46 #include "DNA_userdef_types.h"
47 #include "DNA_windowmanager_types.h"
48
49 #include "BKE_blender.h"
50 #include "BKE_context.h"
51 #include "BKE_screen.h"
52 #include "BKE_curve.h"
53 #include "BKE_displist.h"
54 #include "BKE_DerivedMesh.h"
55 #include "BKE_font.h"
56 #include "BKE_global.h"
57 #include "BKE_library.h"
58 #include "BKE_main.h"
59 #include "BKE_mball.h"
60 #include "BKE_report.h"
61
62 #include "BKE_packedFile.h"
63 #include "BKE_sequencer.h" /* free seq clipboard */
64 #include "BKE_material.h" /* clear_matcopybuf */
65
66 #include "BLI_blenlib.h"
67 #include "BLI_winstuff.h"
68
69 #include "RE_pipeline.h"                /* RE_ free stuff */
70
71 #ifdef WITH_PYTHON
72 #include "BPY_extern.h"
73 #endif
74
75 #ifdef WITH_GAMEENGINE
76 #include "BL_System.h"
77 #endif
78 #include "GHOST_Path-api.h"
79 #include "GHOST_C-api.h"
80
81 #include "RNA_define.h"
82
83 #include "WM_api.h"
84 #include "WM_types.h"
85
86 #include "wm_cursors.h"
87 #include "wm_event_system.h"
88 #include "wm.h"
89 #include "wm_files.h"
90 #include "wm_window.h"
91
92 #include "ED_armature.h"
93 #include "ED_keyframing.h"
94 #include "ED_node.h"
95 #include "ED_render.h"
96 #include "ED_space_api.h"
97 #include "ED_screen.h"
98 #include "ED_util.h"
99
100 #include "UI_interface.h"
101 #include "BLF_api.h"
102 #include "BLF_translation.h"
103
104 #include "GPU_buffers.h"
105 #include "GPU_extensions.h"
106 #include "GPU_draw.h"
107
108 #include "BKE_depsgraph.h"
109 #include "BKE_sound.h"
110
111 static void wm_init_reports(bContext *C)
112 {
113         BKE_reports_init(CTX_wm_reports(C), RPT_STORE);
114 }
115 static void wm_free_reports(bContext *C)
116 {
117         BKE_reports_clear(CTX_wm_reports(C));
118 }
119
120 int wm_start_with_console = 0; /* used in creator.c */
121
122 /* only called once, for startup */
123 void WM_init(bContext *C, int argc, const char **argv)
124 {
125         if (!G.background) {
126                 wm_ghost_init(C);       /* note: it assigns C to ghost! */
127                 wm_init_cursor_data();
128         }
129         GHOST_CreateSystemPaths();
130         wm_operatortype_init();
131         WM_menutype_init();
132
133         set_free_windowmanager_cb(wm_close_and_free);   /* library.c */
134         set_blender_test_break_cb(wm_window_testbreak); /* blender.c */
135         DAG_editors_update_cb(ED_render_id_flush_update); /* depsgraph.c */
136         
137         ED_spacetypes_init();   /* editors/space_api/spacetype.c */
138         
139         ED_file_init();                 /* for fsmenu */
140         ED_init_node_butfuncs();        
141         
142         BLF_init(11, U.dpi); /* Please update source/gamengine/GamePlayer/GPG_ghost.cpp if you change this */
143         BLF_lang_init();
144         /* get the default database, plus a wm */
145         WM_read_homefile(C, NULL, G.factory_startup);
146
147         BLF_lang_set(NULL);
148
149         /* note: there is a bug where python needs initializing before loading the
150          * startup.blend because it may contain PyDrivers. It also needs to be after
151          * initializing space types and other internal data.
152          *
153          * However cant redo this at the moment. Solution is to load python
154          * before WM_read_homefile() or make py-drivers check if python is running.
155          * Will try fix when the crash can be repeated. - campbell. */
156
157 #ifdef WITH_PYTHON
158         BPY_context_set(C); /* necessary evil */
159         BPY_python_start(argc, argv);
160
161         BPY_driver_reset();
162         BPY_app_handlers_reset(); /* causes addon callbacks to be freed [#28068],
163                                    * but this is actually what we want. */
164         BPY_modules_load_user(C);
165 #else
166         (void)argc; /* unused */
167         (void)argv; /* unused */
168 #endif
169
170         if (!G.background && !wm_start_with_console)
171                 GHOST_toggleConsole(3);
172
173         wm_init_reports(C); /* reports cant be initialized before the wm */
174
175         if (!G.background) {
176                 GPU_extensions_init();
177                 GPU_set_mipmap(!(U.gameflags & USER_DISABLE_MIPMAP));
178                 GPU_set_anisotropic(U.anisotropic_filter);
179         
180                 UI_init();
181         }
182         
183         clear_matcopybuf();
184         ED_render_clear_mtex_copybuf();
185
186         //      glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
187                 
188         ED_preview_init_dbase();
189         
190         WM_read_history();
191
192         /* allow a path of "", this is what happens when making a new file */
193         /*
194         if(G.main->name[0] == 0)
195                 BLI_make_file_string("/", G.main->name, BLI_getDefaultDocumentFolder(), "untitled.blend");
196         */
197
198         BLI_strncpy(G.lib, G.main->name, FILE_MAX);
199 }
200
201 void WM_init_splash(bContext *C)
202 {
203         if((U.uiflag & USER_SPLASH_DISABLE) == 0) {
204                 wmWindowManager *wm= CTX_wm_manager(C);
205                 wmWindow *prevwin= CTX_wm_window(C);
206         
207                 if(wm->windows.first) {
208                         CTX_wm_window_set(C, wm->windows.first);
209                         WM_operator_name_call(C, "WM_OT_splash", WM_OP_INVOKE_DEFAULT, NULL);
210                         CTX_wm_window_set(C, prevwin);
211                 }
212         }
213 }
214
215 static ScrArea *biggest_view3d(bContext *C)
216 {
217         bScreen *sc= CTX_wm_screen(C);
218         ScrArea *sa, *big= NULL;
219         int size, maxsize= 0;
220
221         for(sa= sc->areabase.first; sa; sa= sa->next) {
222                 if(sa->spacetype==SPACE_VIEW3D) {
223                         size= sa->winx * sa->winy;
224                         if(size > maxsize) {
225                                 maxsize= size;
226                                 big= sa;
227                         }
228                 }
229         }
230         return big;
231 }
232
233 int WM_init_game(bContext *C)
234 {
235         wmWindowManager *wm= CTX_wm_manager(C);
236         wmWindow* win;
237
238         ScrArea *sa;
239         ARegion *ar= NULL;
240
241         Scene *scene= CTX_data_scene(C);
242
243         if (!scene) {
244                 // XXX, this should not be needed.
245                 Main *bmain = CTX_data_main(C);
246                 scene= bmain->scene.first;
247         }
248
249         win = wm->windows.first;
250
251         //first to get a valid window
252         if(win)
253                 CTX_wm_window_set(C, win);
254
255         sa = biggest_view3d(C);
256         ar= BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
257
258         // if we have a valid 3D view
259         if (sa && ar) {
260                 ARegion *arhide;
261
262                 CTX_wm_area_set(C, sa);
263                 CTX_wm_region_set(C, ar);
264
265                 /* disable quad view */
266                 if(ar->alignment == RGN_ALIGN_QSPLIT)
267                         WM_operator_name_call(C, "SCREEN_OT_region_quadview", WM_OP_EXEC_DEFAULT, NULL);
268
269                 /* toolbox, properties panel and header are hidden */
270                 for(arhide=sa->regionbase.first; arhide; arhide=arhide->next) {
271                         if(arhide->regiontype != RGN_TYPE_WINDOW) {
272                                 if(!(arhide->flag & RGN_FLAG_HIDDEN)) {
273                                         ED_region_toggle_hidden(C, arhide);
274                                 }
275                         }
276                 }
277
278                 /* full screen the area */
279                 if(!sa->full) {
280                         ED_screen_full_toggle(C, win, sa);
281                 }
282
283                 /* Fullscreen */
284                 if(scene->gm.fullscreen) {
285                         WM_operator_name_call(C, "WM_OT_window_fullscreen_toggle", WM_OP_EXEC_DEFAULT, NULL);
286                         wm_get_screensize(&ar->winrct.xmax, &ar->winrct.ymax);
287                         ar->winx = ar->winrct.xmax + 1;
288                         ar->winy = ar->winrct.ymax + 1;
289                 }
290                 else
291                 {
292                         GHOST_RectangleHandle rect = GHOST_GetClientBounds(win->ghostwin);
293                         ar->winrct.ymax = GHOST_GetHeightRectangle(rect);
294                         ar->winrct.xmax = GHOST_GetWidthRectangle(rect);
295                         ar->winx = ar->winrct.xmax + 1;
296                         ar->winy = ar->winrct.ymax + 1;
297                         GHOST_DisposeRectangle(rect);
298                 }
299
300                 WM_operator_name_call(C, "VIEW3D_OT_game_start", WM_OP_EXEC_DEFAULT, NULL);
301
302                 sound_exit();
303
304                 return 1;
305         }
306         else
307         {
308                 ReportTimerInfo *rti;
309
310                 BKE_report(&wm->reports, RPT_ERROR, "No valid 3D View found. Game auto start is not possible.");
311
312                 /* After adding the report to the global list, reset the report timer. */
313                 WM_event_remove_timer(wm, NULL, wm->reports.reporttimer);
314
315                 /* Records time since last report was added */
316                 wm->reports.reporttimer = WM_event_add_timer(wm, CTX_wm_window(C), TIMER, 0.02);
317
318                 rti = MEM_callocN(sizeof(ReportTimerInfo), "ReportTimerInfo");
319                 wm->reports.reporttimer->customdata = rti;
320         }
321         return 0;
322 }
323
324 /* free strings of open recent files */
325 static void free_openrecent(void)
326 {
327         struct RecentFile *recent;
328         
329         for(recent = G.recent_files.first; recent; recent=recent->next)
330                 MEM_freeN(recent->filepath);
331         
332         BLI_freelistN(&(G.recent_files));
333 }
334
335
336 /* bad stuff*/
337
338 extern wchar_t *copybuf;
339 extern wchar_t *copybufinfo;
340
341         // XXX copy/paste buffer stuff...
342 extern void free_anim_copybuf(void); 
343 extern void free_anim_drivers_copybuf(void); 
344 extern void free_fmodifiers_copybuf(void); 
345 extern void free_posebuf(void); 
346
347 /* called in creator.c even... tsk, split this! */
348 /* note, doesnt run exit() call WM_exit() for that */
349 void WM_exit_ext(bContext *C, const short do_python)
350 {
351         wmWindow *win;
352
353         sound_exit();
354
355
356         /* first wrap up running stuff, we assume only the active WM is running */
357         /* modal handlers are on window level freed, others too? */
358         /* note; same code copied in wm_files.c */
359         if(C && CTX_wm_manager(C)) {
360                 
361                 WM_jobs_stop_all(CTX_wm_manager(C));
362                 
363                 for(win= CTX_wm_manager(C)->windows.first; win; win= win->next) {
364                         
365                         CTX_wm_window_set(C, win);      /* needed by operator close callbacks */
366                         WM_event_remove_handlers(C, &win->handlers);
367                         WM_event_remove_handlers(C, &win->modalhandlers);
368                         ED_screen_exit(C, win, win->screen);
369                 }
370         }
371         wm_operatortype_free();
372         wm_dropbox_free();
373         WM_menutype_free();
374         
375         /* all non-screen and non-space stuff editors did, like editmode */
376         if(C)
377                 ED_editors_exit(C);
378
379 //      XXX     
380 //      BIF_GlobalReebFree();
381 //      BIF_freeRetarget();
382         BIF_freeTemplates(C);
383         
384         free_ttfont(); /* bke_font.h */
385
386         free_openrecent();
387         
388         BKE_freecubetable();
389         
390         ED_preview_free_dbase();        /* frees a Main dbase, before free_blender! */
391
392         if(C && CTX_wm_manager(C))
393                 wm_free_reports(C);                     /* before free_blender! - since the ListBases get freed there */
394
395         seq_free_clipboard(); /* sequencer.c */
396                 
397         free_blender();                         /* blender.c, does entire library and spacetypes */
398 //      free_matcopybuf();
399         free_anim_copybuf();
400         free_anim_drivers_copybuf();
401         free_fmodifiers_copybuf();
402         free_posebuf();
403
404         BLF_exit();
405
406 #ifdef WITH_INTERNATIONAL
407         BLF_free_unifont();
408 #endif
409         
410         ANIM_keyingset_infos_exit();
411         
412         RE_FreeAllRender();
413         RE_engines_exit();
414         
415 //      free_txt_data();
416         
417
418 #ifdef WITH_PYTHON
419         /* option not to close python so we can use 'atexit' */
420         if(do_python) {
421                 /* XXX - old note */
422                 /* before free_blender so py's gc happens while library still exists */
423                 /* needed at least for a rare sigsegv that can happen in pydrivers */
424
425                 /* Update for blender 2.5, move after free_blender because blender now holds references to PyObject's
426                  * so decref'ing them after python ends causes bad problems every time
427                  * the pyDriver bug can be fixed if it happens again we can deal with it then */
428                 BPY_python_end();
429         }
430 #else
431         (void)do_python;
432 #endif
433
434         GPU_global_buffer_pool_free();
435         GPU_free_unused_buffers();
436         GPU_extensions_exit();
437         
438 //      if (copybuf) MEM_freeN(copybuf);
439 //      if (copybufinfo) MEM_freeN(copybufinfo);
440         if (!G.background) {
441                 BKE_undo_save_quit();   // saves quit.blend if global undo is on
442         }
443         BKE_reset_undo(); 
444         
445         ED_file_exit(); /* for fsmenu */
446
447         UI_exit();
448         BKE_userdef_free();
449
450         RNA_exit(); /* should be after BPY_python_end so struct python slots are cleared */
451         
452         wm_ghost_exit();
453
454         CTX_free(C);
455 #ifdef WITH_GAMEENGINE
456         SYS_DeleteSystem(SYS_GetSystem());
457 #endif
458         
459         GHOST_DisposeSystemPaths();
460
461         if(MEM_get_memory_blocks_in_use()!=0) {
462                 printf("Error: Not freed memory blocks: %d\n", MEM_get_memory_blocks_in_use());
463                 MEM_printmemlist();
464         }
465         wm_autosave_delete();
466         
467         printf("\nBlender quit\n");
468         
469 #ifdef WIN32   
470         /* ask user to press enter when in debug mode */
471         if(G.f & G_DEBUG) {
472                 printf("press enter key to exit...\n\n");
473                 getchar();
474         }
475 #endif 
476 }
477
478 void WM_exit(bContext *C)
479 {
480         WM_exit_ext(C, 1);
481         exit(G.afbreek==1);
482 }