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