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