2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2007 Blender Foundation.
19 * All rights reserved.
22 * Contributor(s): Blender Foundation
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/windowmanager/intern/wm_init_exit.c
39 #include "MEM_guardedalloc.h"
40 #include "MEM_CacheLimiterC-Api.h"
42 #include "IMB_imbuf_types.h"
43 #include "IMB_imbuf.h"
45 #include "DNA_object_types.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_userdef_types.h"
48 #include "DNA_windowmanager_types.h"
50 #include "BKE_blender.h"
51 #include "BKE_context.h"
52 #include "BKE_screen.h"
53 #include "BKE_curve.h"
54 #include "BKE_displist.h"
55 #include "BKE_DerivedMesh.h"
57 #include "BKE_global.h"
58 #include "BKE_library.h"
60 #include "BKE_mball.h"
62 #include "BKE_report.h"
64 #include "BKE_packedFile.h"
65 #include "BKE_sequencer.h" /* free seq clipboard */
66 #include "BKE_material.h" /* clear_matcopybuf */
67 #include "BKE_tracking.h" /* free tracking clipboard */
69 #include "BLI_listbase.h"
70 #include "BLI_string.h"
71 #include "BLI_utildefines.h"
73 #include "RE_engine.h"
74 #include "RE_pipeline.h" /* RE_ free stuff */
77 #include "BPY_extern.h"
80 #ifdef WITH_GAMEENGINE
81 # include "BL_System.h"
83 #include "GHOST_Path-api.h"
84 #include "GHOST_C-api.h"
86 #include "RNA_define.h"
91 #include "wm_cursors.h"
92 #include "wm_event_system.h"
95 #include "wm_window.h"
97 #include "ED_armature.h"
98 #include "ED_keyframing.h"
100 #include "ED_render.h"
101 #include "ED_space_api.h"
102 #include "ED_screen.h"
105 #include "UI_interface.h"
107 #include "BLF_translation.h"
109 #include "GPU_buffers.h"
110 #include "GPU_extensions.h"
111 #include "GPU_draw.h"
113 #include "BKE_depsgraph.h"
114 #include "BKE_sound.h"
116 #include "IMB_colormanagement.h"
118 static void wm_init_reports(bContext *C)
120 BKE_reports_init(CTX_wm_reports(C), RPT_STORE);
122 static void wm_free_reports(bContext *C)
124 BKE_reports_clear(CTX_wm_reports(C));
127 int wm_start_with_console = 0; /* used in creator.c */
129 /* only called once, for startup */
130 void WM_init(bContext *C, int argc, const char **argv)
133 wm_ghost_init(C); /* note: it assigns C to ghost! */
134 wm_init_cursor_data();
136 GHOST_CreateSystemPaths();
137 wm_operatortype_init();
140 set_free_windowmanager_cb(wm_close_and_free); /* library.c */
141 set_blender_test_break_cb(wm_window_testbreak); /* blender.c */
142 DAG_editors_update_cb(ED_render_id_flush_update, ED_render_scene_update); /* depsgraph.c */
144 ED_spacetypes_init(); /* editors/space_api/spacetype.c */
146 ED_file_init(); /* for fsmenu */
147 ED_node_init_butfuncs();
149 BLF_init(11, U.dpi); /* Please update source/gamengine/GamePlayer/GPG_ghost.cpp if you change this */
152 /* initialize color management stuff */
153 IMB_colormanagement_init();
155 /* get the default database, plus a wm */
156 WM_read_homefile(C, NULL, G.factory_startup);
160 /* note: there is a bug where python needs initializing before loading the
161 * startup.blend because it may contain PyDrivers. It also needs to be after
162 * initializing space types and other internal data.
164 * However cant redo this at the moment. Solution is to load python
165 * before WM_read_homefile() or make py-drivers check if python is running.
166 * Will try fix when the crash can be repeated. - campbell. */
169 BPY_context_set(C); /* necessary evil */
170 BPY_python_start(argc, argv);
173 BPY_app_handlers_reset(FALSE); /* causes addon callbacks to be freed [#28068],
174 * but this is actually what we want. */
175 BPY_modules_load_user(C);
177 (void)argc; /* unused */
178 (void)argv; /* unused */
181 if (!G.background && !wm_start_with_console)
182 GHOST_toggleConsole(3);
184 wm_init_reports(C); /* reports cant be initialized before the wm */
187 GPU_extensions_init();
188 GPU_set_mipmap(!(U.gameflags & USER_DISABLE_MIPMAP));
189 GPU_set_anisotropic(U.anisotropic_filter);
190 GPU_set_gpu_mipmapping(U.use_gpu_mipmap);
196 ED_render_clear_mtex_copybuf();
198 // glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
200 ED_preview_init_dbase();
204 /* allow a path of "", this is what happens when making a new file */
206 if (G.main->name[0] == 0)
207 BLI_make_file_string("/", G.main->name, BLI_getDefaultDocumentFolder(), "untitled.blend");
210 BLI_strncpy(G.lib, G.main->name, FILE_MAX);
212 #ifdef WITH_COMPOSITOR
214 extern void *COM_linker_hack;
215 extern void *COM_execute;
216 COM_linker_hack = COM_execute;
221 void WM_init_splash(bContext *C)
223 if ((U.uiflag & USER_SPLASH_DISABLE) == 0) {
224 wmWindowManager *wm = CTX_wm_manager(C);
225 wmWindow *prevwin = CTX_wm_window(C);
227 if (wm->windows.first) {
228 CTX_wm_window_set(C, wm->windows.first);
229 WM_operator_name_call(C, "WM_OT_splash", WM_OP_INVOKE_DEFAULT, NULL);
230 CTX_wm_window_set(C, prevwin);
235 int WM_init_game(bContext *C)
237 wmWindowManager *wm = CTX_wm_manager(C);
243 Scene *scene = CTX_data_scene(C);
246 /* XXX, this should not be needed. */
247 Main *bmain = CTX_data_main(C);
248 scene = bmain->scene.first;
251 win = wm->windows.first;
253 /* first to get a valid window */
255 CTX_wm_window_set(C, win);
257 sa = BKE_screen_find_big_area(CTX_wm_screen(C), SPACE_VIEW3D, 0);
258 ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
260 /* if we have a valid 3D view */
264 CTX_wm_area_set(C, sa);
265 CTX_wm_region_set(C, ar);
267 /* disable quad view */
268 if (ar->alignment == RGN_ALIGN_QSPLIT)
269 WM_operator_name_call(C, "SCREEN_OT_region_quadview", WM_OP_EXEC_DEFAULT, NULL);
271 /* toolbox, properties panel and header are hidden */
272 for (arhide = sa->regionbase.first; arhide; arhide = arhide->next) {
273 if (arhide->regiontype != RGN_TYPE_WINDOW) {
274 if (!(arhide->flag & RGN_FLAG_HIDDEN)) {
275 ED_region_toggle_hidden(C, arhide);
280 /* full screen the area */
282 ED_screen_full_toggle(C, win, sa);
286 if ((scene->gm.playerflag & GAME_PLAYER_FULLSCREEN)) {
287 WM_operator_name_call(C, "WM_OT_window_fullscreen_toggle", WM_OP_EXEC_DEFAULT, NULL);
288 wm_get_screensize(&ar->winrct.xmax, &ar->winrct.ymax);
289 ar->winx = ar->winrct.xmax + 1;
290 ar->winy = ar->winrct.ymax + 1;
293 GHOST_RectangleHandle rect = GHOST_GetClientBounds(win->ghostwin);
294 ar->winrct.ymax = GHOST_GetHeightRectangle(rect);
295 ar->winrct.xmax = GHOST_GetWidthRectangle(rect);
296 ar->winx = ar->winrct.xmax + 1;
297 ar->winy = ar->winrct.ymax + 1;
298 GHOST_DisposeRectangle(rect);
301 WM_operator_name_call(C, "VIEW3D_OT_game_start", WM_OP_EXEC_DEFAULT, NULL);
308 ReportTimerInfo *rti;
310 BKE_report(&wm->reports, RPT_ERROR, "No valid 3D View found. Game auto start is not possible.");
312 /* After adding the report to the global list, reset the report timer. */
313 WM_event_remove_timer(wm, NULL, wm->reports.reporttimer);
315 /* Records time since last report was added */
316 wm->reports.reporttimer = WM_event_add_timer(wm, CTX_wm_window(C), TIMER, 0.02);
318 rti = MEM_callocN(sizeof(ReportTimerInfo), "ReportTimerInfo");
319 wm->reports.reporttimer->customdata = rti;
324 /* free strings of open recent files */
325 static void free_openrecent(void)
327 struct RecentFile *recent;
329 for (recent = G.recent_files.first; recent; recent = recent->next)
330 MEM_freeN(recent->filepath);
332 BLI_freelistN(&(G.recent_files));
338 // XXX copy/paste buffer stuff...
339 extern void free_anim_copybuf(void);
340 extern void free_anim_drivers_copybuf(void);
341 extern void free_fmodifiers_copybuf(void);
342 extern void free_posebuf(void);
345 /* Read console events until there is a key event. Also returns on any error. */
346 static void wait_for_console_key(void)
348 HANDLE hConsoleInput = GetStdHandle(STD_INPUT_HANDLE);
350 if (!ELEM(hConsoleInput, NULL, INVALID_HANDLE_VALUE) && FlushConsoleInputBuffer(hConsoleInput)) {
355 if (!ReadConsoleInput(hConsoleInput, &buffer, 1, &ignored)) {
359 if (buffer.EventType == KEY_EVENT) {
367 /* called in creator.c even... tsk, split this! */
368 /* note, doesnt run exit() call WM_exit() for that */
369 void WM_exit_ext(bContext *C, const short do_python)
375 IMB_colormanagement_exit();
377 /* first wrap up running stuff, we assume only the active WM is running */
378 /* modal handlers are on window level freed, others too? */
379 /* note; same code copied in wm_files.c */
380 if (C && CTX_wm_manager(C)) {
382 WM_jobs_stop_all(CTX_wm_manager(C));
384 for (win = CTX_wm_manager(C)->windows.first; win; win = win->next) {
386 CTX_wm_window_set(C, win); /* needed by operator close callbacks */
387 WM_event_remove_handlers(C, &win->handlers);
388 WM_event_remove_handlers(C, &win->modalhandlers);
389 ED_screen_exit(C, win, win->screen);
392 wm_operatortype_free();
396 /* all non-screen and non-space stuff editors did, like editmode */
401 // BIF_GlobalReebFree();
402 // BIF_freeRetarget();
403 BIF_freeTemplates(C);
405 BKE_vfont_free_global_ttf(); /* bke_font.h */
409 BKE_mball_cubeTable_free();
411 ED_preview_free_dbase(); /* frees a Main dbase, before free_blender! */
413 if (C && CTX_wm_manager(C))
414 wm_free_reports(C); /* before free_blender! - since the ListBases get freed there */
416 seq_free_clipboard(); /* sequencer.c */
417 BKE_tracking_clipboard_free();
419 free_blender(); /* blender.c, does entire library and spacetypes */
420 // free_matcopybuf();
422 free_anim_drivers_copybuf();
423 free_fmodifiers_copybuf();
425 BKE_node_clipboard_clear();
429 #ifdef WITH_INTERNATIONAL
433 ANIM_keyingset_infos_exit();
442 /* option not to close python so we can use 'atexit' */
445 /* before free_blender so py's gc happens while library still exists */
446 /* needed at least for a rare sigsegv that can happen in pydrivers */
448 /* Update for blender 2.5, move after free_blender because blender now holds references to PyObject's
449 * so decref'ing them after python ends causes bad problems every time
450 * the pyDriver bug can be fixed if it happens again we can deal with it then */
457 GPU_global_buffer_pool_free();
458 GPU_free_unused_buffers();
459 GPU_extensions_exit();
462 BKE_undo_save_quit(); /* saves quit.blend if global undo is on */
466 ED_file_exit(); /* for fsmenu */
471 RNA_exit(); /* should be after BPY_python_end so struct python slots are cleared */
476 #ifdef WITH_GAMEENGINE
477 SYS_DeleteSystem(SYS_GetSystem());
480 GHOST_DisposeSystemPaths();
482 if (MEM_get_memory_blocks_in_use() != 0) {
483 printf("Error: Not freed memory blocks: %d\n", MEM_get_memory_blocks_in_use());
486 wm_autosave_delete();
488 printf("\nBlender quit\n");
491 /* ask user to press a key when in debug mode */
492 if (G.debug & G_DEBUG) {
493 printf("Press any key to exit . . .\n\n");
494 wait_for_console_key();
499 void WM_exit(bContext *C)
502 exit(G.afbreek == 1);