4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2007 Blender Foundation.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
29 /** \file blender/windowmanager/intern/wm_init_exit.c
38 #include "MEM_guardedalloc.h"
39 #include "MEM_CacheLimiterC-Api.h"
41 #include "IMB_imbuf_types.h"
42 #include "IMB_imbuf.h"
44 #include "DNA_object_types.h"
45 #include "DNA_scene_types.h"
46 #include "DNA_userdef_types.h"
47 #include "DNA_windowmanager_types.h"
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"
56 #include "BKE_global.h"
57 #include "BKE_library.h"
59 #include "BKE_mball.h"
60 #include "BKE_report.h"
62 #include "BKE_packedFile.h"
63 #include "BKE_sequencer.h" /* free seq clipboard */
64 #include "BKE_material.h" /* clear_matcopybuf */
66 #include "BLI_blenlib.h"
67 #include "BLI_winstuff.h"
69 #include "RE_pipeline.h" /* RE_ free stuff */
72 #include "BPY_extern.h"
75 #ifdef WITH_GAMEENGINE
76 #include "BL_System.h"
78 #include "GHOST_Path-api.h"
79 #include "GHOST_C-api.h"
81 #include "RNA_define.h"
86 #include "wm_cursors.h"
87 #include "wm_event_system.h"
90 #include "wm_window.h"
92 #include "ED_armature.h"
93 #include "ED_keyframing.h"
95 #include "ED_render.h"
96 #include "ED_space_api.h"
97 #include "ED_screen.h"
100 #include "UI_interface.h"
103 #include "GPU_buffers.h"
104 #include "GPU_extensions.h"
105 #include "GPU_draw.h"
107 #include "BKE_depsgraph.h"
108 #include "BKE_sound.h"
110 static void wm_init_reports(bContext *C)
112 BKE_reports_init(CTX_wm_reports(C), RPT_STORE);
114 static void wm_free_reports(bContext *C)
116 BKE_reports_clear(CTX_wm_reports(C));
119 int wm_start_with_console = 0;
121 /* only called once, for startup */
122 void WM_init(bContext *C, int argc, const char **argv)
125 wm_ghost_init(C); /* note: it assigns C to ghost! */
126 wm_init_cursor_data();
128 GHOST_CreateSystemPaths();
129 wm_operatortype_init();
131 set_free_windowmanager_cb(wm_close_and_free); /* library.c */
132 set_blender_test_break_cb(wm_window_testbreak); /* blender.c */
133 DAG_editors_update_cb(ED_render_id_flush_update); /* depsgraph.c */
135 ED_spacetypes_init(); /* editors/space_api/spacetype.c */
137 ED_file_init(); /* for fsmenu */
138 ED_init_node_butfuncs();
140 BLF_init(11, U.dpi); /* Please update source/gamengine/GamePlayer/GPG_ghost.cpp if you change this */
142 /* get the default database, plus a wm */
143 WM_read_homefile(C, NULL, G.factory_startup);
145 /* note: there is a bug where python needs initializing before loading the
146 * startup.blend because it may contain PyDrivers. It also needs to be after
147 * initializing space types and other internal data.
149 * However cant redo this at the moment. Solution is to load python
150 * before WM_read_homefile() or make py-drivers check if python is running.
151 * Will try fix when the crash can be repeated. - campbell. */
154 BPY_context_set(C); /* necessary evil */
155 BPY_python_start(argc, argv);
158 BPY_modules_load_user(C);
160 (void)argc; /* unused */
161 (void)argv; /* unused */
164 if (!G.background && !wm_start_with_console)
165 GHOST_toggleConsole(3);
167 wm_init_reports(C); /* reports cant be initialized before the wm */
170 GPU_extensions_init();
171 GPU_set_mipmap(!(U.gameflags & USER_DISABLE_MIPMAP));
172 GPU_set_anisotropic(U.anisotropic_filter);
178 ED_render_clear_mtex_copybuf();
180 // glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
182 ED_preview_init_dbase();
184 G.ndofdevice = -1; /* XXX bad initializer, needs set otherwise buttons show! */
188 /* allow a path of "", this is what happens when making a new file */
190 if(G.main->name[0] == 0)
191 BLI_make_file_string("/", G.main->name, BLI_getDefaultDocumentFolder(), "untitled.blend");
194 BLI_strncpy(G.lib, G.main->name, FILE_MAX);
197 void WM_init_splash(bContext *C)
199 if((U.uiflag & USER_SPLASH_DISABLE) == 0) {
200 wmWindowManager *wm= CTX_wm_manager(C);
201 wmWindow *prevwin= CTX_wm_window(C);
203 if(wm->windows.first) {
204 CTX_wm_window_set(C, wm->windows.first);
205 WM_operator_name_call(C, "WM_OT_splash", WM_OP_INVOKE_DEFAULT, NULL);
206 CTX_wm_window_set(C, prevwin);
211 static ScrArea *biggest_view3d(bContext *C)
213 bScreen *sc= CTX_wm_screen(C);
214 ScrArea *sa, *big= NULL;
215 int size, maxsize= 0;
217 for(sa= sc->areabase.first; sa; sa= sa->next) {
218 if(sa->spacetype==SPACE_VIEW3D) {
219 size= sa->winx * sa->winy;
229 int WM_init_game(bContext *C)
231 wmWindowManager *wm= CTX_wm_manager(C);
237 Scene *scene= CTX_data_scene(C);
240 // XXX, this should not be needed.
241 Main *bmain = CTX_data_main(C);
242 scene= bmain->scene.first;
245 win = wm->windows.first;
247 //first to get a valid window
249 CTX_wm_window_set(C, win);
251 sa = biggest_view3d(C);
252 ar= BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
254 // if we have a valid 3D view
258 CTX_wm_area_set(C, sa);
259 CTX_wm_region_set(C, ar);
261 /* disable quad view */
262 if(ar->alignment == RGN_ALIGN_QSPLIT)
263 WM_operator_name_call(C, "SCREEN_OT_region_quadview", WM_OP_EXEC_DEFAULT, NULL);
265 /* toolbox, properties panel and header are hidden */
266 for(arhide=sa->regionbase.first; arhide; arhide=arhide->next) {
267 if(arhide->regiontype != RGN_TYPE_WINDOW) {
268 if(!(arhide->flag & RGN_FLAG_HIDDEN)) {
269 ED_region_toggle_hidden(C, arhide);
274 /* full screen the area */
276 ED_screen_full_toggle(C, win, sa);
280 if(scene->gm.fullscreen) {
281 WM_operator_name_call(C, "WM_OT_window_fullscreen_toggle", WM_OP_EXEC_DEFAULT, NULL);
282 wm_get_screensize(&ar->winrct.xmax, &ar->winrct.ymax);
283 ar->winx = ar->winrct.xmax + 1;
284 ar->winy = ar->winrct.ymax + 1;
288 GHOST_RectangleHandle rect = GHOST_GetClientBounds(win->ghostwin);
289 ar->winrct.ymax = GHOST_GetHeightRectangle(rect);
290 ar->winrct.xmax = GHOST_GetWidthRectangle(rect);
291 ar->winx = ar->winrct.xmax + 1;
292 ar->winy = ar->winrct.ymax + 1;
293 GHOST_DisposeRectangle(rect);
296 WM_operator_name_call(C, "VIEW3D_OT_game_start", WM_OP_EXEC_DEFAULT, NULL);
302 ReportTimerInfo *rti;
304 BKE_report(&wm->reports, RPT_ERROR, "No valid 3D View found. Game auto start is not possible.");
306 /* After adding the report to the global list, reset the report timer. */
307 WM_event_remove_timer(wm, NULL, wm->reports.reporttimer);
309 /* Records time since last report was added */
310 wm->reports.reporttimer = WM_event_add_timer(wm, CTX_wm_window(C), TIMER, 0.02);
312 rti = MEM_callocN(sizeof(ReportTimerInfo), "ReportTimerInfo");
313 wm->reports.reporttimer->customdata = rti;
318 /* free strings of open recent files */
319 static void free_openrecent(void)
321 struct RecentFile *recent;
323 for(recent = G.recent_files.first; recent; recent=recent->next)
324 MEM_freeN(recent->filepath);
326 BLI_freelistN(&(G.recent_files));
332 extern wchar_t *copybuf;
333 extern wchar_t *copybufinfo;
335 // XXX copy/paste buffer stuff...
336 extern void free_anim_copybuf(void);
337 extern void free_anim_drivers_copybuf(void);
338 extern void free_fmodifiers_copybuf(void);
339 extern void free_posebuf(void);
341 /* called in creator.c even... tsk, split this! */
342 void WM_exit(bContext *C)
349 /* first wrap up running stuff, we assume only the active WM is running */
350 /* modal handlers are on window level freed, others too? */
351 /* note; same code copied in wm_files.c */
352 if(C && CTX_wm_manager(C)) {
354 WM_jobs_stop_all(CTX_wm_manager(C));
356 for(win= CTX_wm_manager(C)->windows.first; win; win= win->next) {
358 CTX_wm_window_set(C, win); /* needed by operator close callbacks */
359 WM_event_remove_handlers(C, &win->handlers);
360 WM_event_remove_handlers(C, &win->modalhandlers);
361 ED_screen_exit(C, win, win->screen);
364 wm_operatortype_free();
368 /* all non-screen and non-space stuff editors did, like editmode */
373 // BIF_GlobalReebFree();
374 // BIF_freeRetarget();
375 BIF_freeTemplates(C);
377 free_ttfont(); /* bke_font.h */
383 ED_preview_free_dbase(); /* frees a Main dbase, before free_blender! */
385 if(C && CTX_wm_manager(C))
386 wm_free_reports(C); /* before free_blender! - since the ListBases get freed there */
388 seq_free_clipboard(); /* sequencer.c */
390 free_blender(); /* blender.c, does entire library and spacetypes */
391 // free_matcopybuf();
393 free_anim_drivers_copybuf();
394 free_fmodifiers_copybuf();
399 ANIM_keyingset_infos_exit();
409 /* before free_blender so py's gc happens while library still exists */
410 /* needed at least for a rare sigsegv that can happen in pydrivers */
412 /* Update for blender 2.5, move after free_blender because blender now holds references to PyObject's
413 * so decref'ing them after python ends causes bad problems every time
414 * the pyDriver bug can be fixed if it happens again we can deal with it then */
418 GPU_buffer_pool_free(NULL);
419 GPU_free_unused_buffers();
420 GPU_extensions_exit();
422 // if (copybuf) MEM_freeN(copybuf);
423 // if (copybufinfo) MEM_freeN(copybufinfo);
425 BKE_undo_save_quit(); // saves quit.blend if global undo is on
429 ED_file_exit(); /* for fsmenu */
434 RNA_exit(); /* should be after BPY_python_end so struct python slots are cleared */
439 #ifdef WITH_GAMEENGINE
440 SYS_DeleteSystem(SYS_GetSystem());
443 GHOST_DisposeSystemPaths();
445 if(MEM_get_memory_blocks_in_use()!=0) {
446 printf("Error: Not freed memory blocks: %d\n", MEM_get_memory_blocks_in_use());
449 wm_autosave_delete();
451 printf("\nBlender quit\n");
454 /* ask user to press enter when in debug mode */
456 printf("press enter key to exit...\n\n");