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