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